Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Direct IO #133

Merged
merged 23 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 12 additions & 176 deletions lib/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,180 +14,16 @@
* limitations under the License.
*/

import { ReadResult, WriteResult } from 'file-disk';
import * as fs from 'fs';

export async function close(fd: number): Promise<void> {
await new Promise((resolve: () => void, reject: (err: Error) => void) => {
fs.close(fd, (err: Error) => {
if (err) {
reject(err);
return;
}
resolve();
});
});
}

export async function fstat(fd: number): Promise<fs.Stats> {
return await new Promise(
(resolve: (stats: fs.Stats) => void, reject: (err: Error) => void) => {
fs.fstat(fd, (err: Error, stats: fs.Stats) => {
if (err) {
reject(err);
return;
}
resolve(stats);
});
},
);
}

export async function fsync(fd: number): Promise<void> {
await new Promise((resolve: () => void, reject: (err: Error) => void) => {
fs.fsync(fd, (err: Error) => {
if (err) {
reject(err);
return;
}
resolve();
});
});
}

export async function open(
path: string | Buffer,
flags: string | number,
mode = 0o666,
): Promise<number> {
return await new Promise(
(resolve: (fd: number) => void, reject: (err: Error) => void) => {
fs.open(path, flags, mode, (err: Error, fd: number) => {
if (err) {
reject(err);
return;
}
resolve(fd);
});
},
);
}

export async function read(
fd: number,
buf: Buffer,
offset: number,
length: number,
position: number,
): Promise<ReadResult> {
return await new Promise(
(resolve: (result: ReadResult) => void, reject: (err: Error) => void) => {
fs.read(
fd,
buf,
offset,
length,
position,
(err: Error, bytesRead: number, buffer: Buffer) => {
if (err) {
reject(err);
return;
}
resolve({ bytesRead, buffer });
},
);
},
);
}

export async function readFile(
path: string,
options: { encoding: string | null; flag: string } | undefined = {
encoding: null,
flag: 'r',
},
): Promise<string | Buffer> {
return await new Promise(
(
resolve: (data: string | Buffer) => void,
reject: (err: Error) => void,
) => {
fs.readFile(path, options, (err: Error, data: string | Buffer) => {
if (err) {
reject(err);
return;
}
resolve(data);
});
},
);
}

export async function stat(path: string | Buffer): Promise<fs.Stats> {
return await new Promise(
(resolve: (stats: fs.Stats) => void, reject: (err: Error) => void) => {
fs.stat(path, (err: Error, stats: fs.Stats) => {
if (err) {
reject(err);
return;
}
resolve(stats);
});
},
);
}

export async function writeFile(
path: string,
data: string | Buffer,
options = { encoding: 'utf8', mode: 0o666, flag: 'w' },
): Promise<void> {
await new Promise((resolve: () => void, reject: (err: Error) => void) => {
fs.writeFile(path, data, options, (err: Error) => {
if (err) {
reject(err);
return;
}
resolve();
});
});
}

export async function unlink(path: string | Buffer): Promise<void> {
await new Promise((resolve: () => void, reject: (err: Error) => void) => {
fs.unlink(path, (err: Error) => {
if (err) {
reject(err);
return;
}
resolve();
});
});
}

export async function write(
fd: number,
buf: Buffer,
offset: number,
length: number,
position: number,
): Promise<WriteResult> {
return await new Promise(
(resolve: (result: WriteResult) => void, reject: (err: Error) => void) => {
fs.write(
fd,
buf,
offset,
length,
position,
(err: Error, bytesWritten: number, buffer: Buffer) => {
if (err) {
reject(err);
return;
}
resolve({ bytesWritten, buffer });
},
);
},
);
}
import { promisify } from 'util';

export const close = promisify(fs.close);
export const fstat = promisify(fs.fstat);
export const fsync = promisify(fs.fsync);
export const open = promisify(fs.open);
export const read = promisify(fs.read);
export const readFile = promisify(fs.readFile);
export const stat = promisify(fs.stat);
export const unlink = promisify(fs.unlink);
export const write = promisify(fs.write);
export const writeFile = promisify(fs.writeFile);
Page- marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 2 additions & 3 deletions lib/lazy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { once } from 'lodash';
import { promisify } from 'util';
import * as xxhash from 'xxhash';

export type XXHash = typeof xxhash;

import { promisify } from 'bluebird';
import { once } from 'lodash';

export const getRaspberrypiUsbboot = once(() => {
try {
return require('node-raspberrypi-usbboot') as typeof import('node-raspberrypi-usbboot');
Expand Down
2 changes: 1 addition & 1 deletion lib/source-destination/dmg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* limitations under the License.
*/

import { promisify } from 'bluebird';
import * as _ from 'lodash';
import { BLOCK, CHECKSUM_TYPE, Image as UDIFImage, SECTOR_SIZE } from 'udif';
import { promisify } from 'util';

import { Metadata } from './metadata';
import { SourceDestination, SourceDestinationFs } from './source-destination';
Expand Down
3 changes: 1 addition & 2 deletions lib/source-destination/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ import {
} from '../sparse-stream/sparse-write-stream';

export const ProgressWriteStream = makeClassEmitProgressEvents(
// type definitions for node 6 export fs.WriteStream as an interface, but it's a class.
// @ts-ignore
fs.WriteStream,
'bytesWritten',
'bytesWritten',
Expand Down Expand Up @@ -135,6 +133,7 @@ export class File extends SourceDestination {
}

public async createWriteStream(): Promise<NodeJS.WritableStream> {
// @ts-ignore: @types/node is wrong about fs.WriteStream constructor: it takes 2 arguments, the first one is the file path
Page- marked this conversation as resolved.
Show resolved Hide resolved
const stream = new ProgressWriteStream(null, {
fd: this.fd,
autoClose: false,
Expand Down
2 changes: 1 addition & 1 deletion lib/source-destination/xz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* limitations under the License.
*/

import { promisify } from 'bluebird';
import { createDecompressor, parseFileIndex } from 'lzma-native';
import { Transform } from 'stream';
import { promisify } from 'util';

import { CompressedSource } from './compressed-source';
import { SourceDestination } from './source-destination';
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
"winusb-driver-generator": "^1.2.3"
},
"dependencies": {
"@types/node": "^6.0.112",
"axios": "^0.18.0",
"blockmap": "^4.0.0",
"bluebird": "^3.5.1",
Expand Down Expand Up @@ -82,6 +81,7 @@
"@types/file-type": "^5.2.1",
"@types/lodash": "^4.14.108",
"@types/mocha": "^5.2.4",
"@types/node": "^8.10.59",
Page- marked this conversation as resolved.
Show resolved Hide resolved
"@types/progress": "^2.0.1",
"@types/sinon": "^5.0.1",
"@types/yargs": "^11.0.0",
Expand Down
12 changes: 2 additions & 10 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,13 @@
"outDir": "build",
"sourceMap": true,
"declaration": true,
"target": "es6",
"target": "es2017",
Page- marked this conversation as resolved.
Show resolved Hide resolved
"noImplicitAny": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"strictNullChecks": true,
"lib": [
"es2016"
]
"strictNullChecks": true
},
"include": [
"lib"
],
"exclude": [
"node_modules",
"build",
"tests"
]
}
5 changes: 4 additions & 1 deletion typings/lzma-native/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ declare module 'lzma-native' {
cb: (err: Error | null, buffer?: Buffer) => any,
) => void;
},
callback: (error?: Error, metadata?: { uncompressedSize: number }) => void,
callback: (
error: Error | null,
metadata: { uncompressedSize: number },
) => void,
): void;
}
2 changes: 1 addition & 1 deletion typings/mountutils/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
declare module 'mountutils' {
export function unmountDisk(
device: string,
callback: (error: any, result?: any) => void,
callback: (error?: Error | null) => void,
): void;
}
5 changes: 1 addition & 4 deletions typings/readable-stream/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,5 @@ declare module 'readable-stream' {

export class Transform extends NodeJSTransform {}

export class Writable extends NodeJSWritable {
// destroy does not exist in node 6 Writable
public destroy(error?: Error): this;
}
export class Writable extends NodeJSWritable {}
}