Skip to content

Commit d4719d6

Browse files
committed
node: change NodeFileSystem types for streams, add createReadStream
1 parent daba2f1 commit d4719d6

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

packages/node/src/storage/FsNodeFileSystem.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { BacktraceAttachment } from '@backtrace/sdk-core';
22
import fs from 'fs';
3+
import { Readable, Writable } from 'stream';
34
import { BacktraceFileAttachment } from '../attachment';
4-
import { NodeFileSystem, WritableStream } from './interfaces/NodeFileSystem';
5+
import { NodeFileSystem } from './interfaces/NodeFileSystem';
56

67
export class FsNodeFileSystem implements NodeFileSystem {
78
public readDir(dir: string): Promise<string[]> {
@@ -52,10 +53,12 @@ export class FsNodeFileSystem implements NodeFileSystem {
5253
fs.renameSync(oldPath, newPath);
5354
}
5455

55-
public createWriteStream(path: string): WritableStream {
56-
const stream = fs.createWriteStream(path, 'utf-8');
57-
(stream as Partial<WritableStream>).writeSync = (chunk) => stream.write(chunk);
58-
return stream as unknown as WritableStream;
56+
public createWriteStream(path: string): Writable {
57+
return fs.createWriteStream(path, 'utf-8');
58+
}
59+
60+
public createReadStream(path: string): Readable {
61+
return fs.createReadStream(path, 'utf-8');
5962
}
6063

6164
public async exists(path: string): Promise<boolean> {
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
import { FileSystem } from '@backtrace/sdk-core';
2-
3-
export interface WritableStream {
4-
write(chunk: string, callback?: (err?: Error | null) => void): void;
5-
writeSync(chunk: string): void;
6-
close(): void;
7-
}
2+
import { Readable, Writable } from 'stream';
83

94
export interface NodeFileSystem extends FileSystem {
10-
createWriteStream(path: string): WritableStream;
5+
createReadStream(path: string): Readable;
6+
createWriteStream(path: string): Writable;
117
rename(oldPath: string, newPath: string): Promise<void>;
128
renameSync(oldPath: string, newPath: string): void;
139
}

packages/node/tests/_mocks/fileSystem.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { MockedFileSystem, mockFileSystem } from '@backtrace/sdk-core/tests/_mocks/fileSystem';
22
import path from 'path';
3-
import { Writable } from 'stream';
4-
import { NodeFileSystem, WritableStream } from '../../src/storage/interfaces/NodeFileSystem';
3+
import { Readable, Writable } from 'stream';
4+
import { NodeFileSystem } from '../../src/storage/interfaces/NodeFileSystem';
55

66
export function mockStreamFileSystem(files?: Record<string, string>): MockedFileSystem<NodeFileSystem> {
77
const fs = mockFileSystem(files);
@@ -22,7 +22,7 @@ export function mockStreamFileSystem(files?: Record<string, string>): MockedFile
2222
}),
2323

2424
createWriteStream: jest.fn().mockImplementation((p: string) => {
25-
const writable = new Writable({
25+
return new Writable({
2626
write(chunk, encoding, callback) {
2727
const str = Buffer.isBuffer(chunk)
2828
? chunk.toString('utf-8')
@@ -40,11 +40,27 @@ export function mockStreamFileSystem(files?: Record<string, string>): MockedFile
4040
callback && callback();
4141
},
4242
});
43+
}),
4344

44-
(writable as Partial<WritableStream>).close = () => writable.end();
45-
(writable as Partial<WritableStream>).writeSync = (chunk) => writable.write(chunk);
45+
createReadStream: jest.fn().mockImplementation((p: string) => {
46+
const fullPath = path.resolve(p);
47+
const file = fs.files[fullPath];
48+
if (!file) {
49+
throw new Error(`File ${p} does not exist`);
50+
}
4651

47-
return writable;
52+
let position = 0;
53+
return new Readable({
54+
read(size) {
55+
const chunk = file.substring(position, position + size);
56+
if (!chunk) {
57+
this.push(null);
58+
} else {
59+
this.push(Buffer.from(chunk));
60+
position += size;
61+
}
62+
},
63+
});
4864
}),
4965
};
5066
}

0 commit comments

Comments
 (0)