Skip to content

Commit

Permalink
feat: 🎸 implement FileHandle.readableWebStream()
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jul 27, 2024
1 parent ad54ddb commit c3ddc6c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/__tests__/volume/FileHandle.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { fromStream } from '@jsonjoy.com/util/lib/streams/fromStream';
import { createFs } from '../util';

describe('FileHandle', () => {
describe('.readableWebStream()', () => {
it('can read contest of a file', async () => {
const fs = createFs();
fs.writeFileSync('/foo', 'bar');
const handle = await fs.promises.open('/foo', 'r');
const stream = handle.readableWebStream();
expect(stream).toBeInstanceOf(ReadableStream);
const data = fromStream(stream);
expect(await data).toEqual(Buffer.from('bar'));
});
});
});
10 changes: 10 additions & 0 deletions src/node/FileHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ export class FileHandle implements IFileHandle {
return promisify(this.fs, 'fdatasync')(this.fd);
}

readableWebStream(options?: opts.IReadableWebStreamOptions): ReadableStream {
return new ReadableStream({
pull: async (controller) => {
const data = await this.readFile();
controller.enqueue(data);
controller.close();
},
});
};

read(buffer: Buffer | Uint8Array, offset: number, length: number, position: number): Promise<TFileHandleReadResult> {
return promisify(this.fs, 'read', bytesRead => ({ bytesRead, buffer }))(this.fd, buffer, offset, length, position);
}
Expand Down
1 change: 0 additions & 1 deletion src/node/types/FsPromisesApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export interface FsPromisesApi {
mkdtemp(prefix: string, options?: opts.IOptions): Promise<misc.TDataOut>;
open(path: misc.PathLike, flags?: misc.TFlags, mode?: misc.TMode): Promise<misc.IFileHandle>;
opendir(path: misc.PathLike, options?: opts.IOpendirOptions): Promise<misc.IDir>;
readableWebStream: (options?: opts.IReadableWebStreamOptions) => ReadableStream;
readdir(path: misc.PathLike, options?: opts.IReaddirOptions | string): Promise<misc.TDataOut[] | misc.IDirent[]>;
readFile(id: misc.TFileHandle, options?: opts.IReadFileOptions | string): Promise<misc.TDataOut>;
readlink(path: misc.PathLike, options?: opts.IOptions): Promise<misc.TDataOut>;
Expand Down
3 changes: 2 additions & 1 deletion src/node/types/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import type { EventEmitter } from 'events';
import type { TSetTimeout } from '../../setTimeoutUnref';
import type {
IAppendFileOptions,
IReadableWebStreamOptions,
IReadFileOptions,
IReadStreamOptions,
IStatOptions,
IWriteFileOptions,
} from './options';
Expand Down Expand Up @@ -129,6 +129,7 @@ export interface IFileHandle {
chown(uid: number, gid: number): Promise<void>;
close(): Promise<void>;
datasync(): Promise<void>;
readableWebStream(options?: IReadableWebStreamOptions): ReadableStream;
read(buffer: Buffer | Uint8Array, offset: number, length: number, position: number): Promise<TFileHandleReadResult>;
readv(buffers: ArrayBufferView[], position?: number | null): Promise<TFileHandleReadvResult>;
readFile(options?: IReadFileOptions | string): Promise<TDataOut>;
Expand Down

0 comments on commit c3ddc6c

Please sign in to comment.