Skip to content

Commit

Permalink
fix(fs): correct typing so fsa.stream can be used with fsa.write
Browse files Browse the repository at this point in the history
  • Loading branch information
blacha committed Feb 20, 2022
1 parent d5dc67b commit 460a596
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
18 changes: 18 additions & 0 deletions packages/fs/src/__test__/fs.abstraction.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FsFile } from '@chunkd/source-file';
import o from 'ospec';
import Sinon from 'sinon';
import { FileSystemAbstraction } from '../fs.abstraction.js';

export class FakeSystem extends FsFile {
Expand Down Expand Up @@ -59,4 +60,21 @@ o.spec('FileSystemAbstraction', () => {
o(fsa.systems.length).equals(1);
o(fsa.systems[0].system).equals(fakeB);
});

o('should stream files between systems', () => {
const fakeA = new FakeSystem('fake');
const fakeB = new FakeSystem('fakeSpecific');
const fsa = new FileSystemAbstraction();

const writeStub = (fakeB.write = Sinon.stub());
const streamStub = (fakeA.stream = Sinon.stub());

fsa.register('fake-a://', fakeA);
fsa.register('fake-b://', fakeB);

fsa.write('fake-b://bar.js', fsa.stream('fake-a://foo.js'));

o(streamStub.callCount).equals(1);
o(writeStub.callCount).equals(1);
});
});
7 changes: 4 additions & 3 deletions packages/fs/src/fs.abstraction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Readable } from 'stream';
import type { Readable } from 'stream';
import { ChunkSource, FileInfo, FileSystem, WriteOptions } from '@chunkd/core';

export type FileWriteTypes = Buffer | Readable | string | Record<string, unknown> | Array<unknown>;
Expand All @@ -9,7 +9,8 @@ function isRecord(obj: unknown): obj is Record<string, unknown> {
return obj.constructor === Object;
}

export class FileSystemAbstraction {
export class FileSystemAbstraction implements FileSystem {
protocol = 'abstract';
/**
* Is the systems array currently ordered
* @see FileSystemAbstraction.sortSystems
Expand Down Expand Up @@ -70,7 +71,7 @@ export class FileSystemAbstraction {
* @param filePath file to read
* @returns Stream of file contents
*/
stream(filePath: string): NodeJS.ReadableStream {
stream(filePath: string): Readable {
return this.get(filePath).stream(filePath);
}

Expand Down

0 comments on commit 460a596

Please sign in to comment.