diff --git a/demo/fsa/main.ts b/demo/fsa/main.ts index bfa278e8e..5c0c66be9 100644 --- a/demo/fsa/main.ts +++ b/demo/fsa/main.ts @@ -11,6 +11,12 @@ const demo = async (dir: fsa.IFileSystemDirectoryHandle) => { await fs.promises.writeFile('test.txt', 'Hello world!'); await fs.promises.mkdir('storage/a/b/c', {recursive: true}); await fs.promises.rm('storage/a/b', {recursive: true}); + + + const stream = fs.createWriteStream('stream.txt'); + stream.write('abc'); + stream.write('def'); + stream.end('ghi'); }; const main = async () => { diff --git a/demo/fsa/webpack.config.js b/demo/fsa/webpack.config.js index 97f1acbf5..dcad5f068 100644 --- a/demo/fsa/webpack.config.js +++ b/demo/fsa/webpack.config.js @@ -28,9 +28,11 @@ module.exports = { buffer: require.resolve('buffer'), path: require.resolve('path-browserify'), process: require.resolve('process/browser'), - stream: require.resolve('streamx'), + // stream: require.resolve('streamx'), + stream: require.resolve('readable-stream'), url: require.resolve('url'), util: require.resolve('util'), + // fs: path.resolve(__dirname, '../../src/index.ts'), }, }, output: { diff --git a/package.json b/package.json index 37ff943bb..d70b20b11 100644 --- a/package.json +++ b/package.json @@ -96,9 +96,9 @@ "prettier": "^2.7.1", "pretty-quick": "^3.1.3", "process": "^0.11.10", + "readable-stream": "^4.4.0", "rimraf": "^3.0.2", "semantic-release": "^19.0.3", - "streamx": "^2.15.0", "ts-jest": "^28.0.5", "ts-loader": "^9.4.3", "ts-node": "^10.8.1", diff --git a/src/fsa-to-node/FsaNodeWriteStream.ts b/src/fsa-to-node/FsaNodeWriteStream.ts index eaf4b821c..c1c991a99 100644 --- a/src/fsa-to-node/FsaNodeWriteStream.ts +++ b/src/fsa-to-node/FsaNodeWriteStream.ts @@ -118,20 +118,20 @@ export class FsaNodeWriteStream extends Writable implements IWriteStream { _write(chunk: any, encoding: string, callback: (error?: Error | null) => void): void { this.___write___([chunk]) - .then(() => callback(null)) - .catch(error => callback(error)); + .then(() => { if (callback) callback(null); }) + .catch(error => { if (callback) callback(error); }); } _writev(chunks: Array<{ chunk: any; encoding: string }>, callback: (error?: Error | null) => void): void { const buffers = chunks.map(({ chunk }) => chunk); this.___write___(buffers) - .then(() => callback(null)) - .catch(error => callback(error)); + .then(() => { if (callback) callback(null); }) + .catch(error => { if (callback) callback(error); }); } _final(callback: (error?: Error | null) => void): void { this.__close__() - .then(() => callback(null)) - .catch(error => callback(error)); + .then(() => { if (callback) callback(null); }) + .catch(error => { if (callback) callback(error); }); } } diff --git a/src/fsa-to-node/__tests__/FsaNodeFs.test.ts b/src/fsa-to-node/__tests__/FsaNodeFs.test.ts index 576a50696..3fba306e3 100644 --- a/src/fsa-to-node/__tests__/FsaNodeFs.test.ts +++ b/src/fsa-to-node/__tests__/FsaNodeFs.test.ts @@ -653,6 +653,23 @@ onlyOnNode20('FsaNodeFs', () => { }); }); + test('can use stream to write to a new file using strings', async () => { + const { fs, mfs } = setup({ folder: { file: 'test' }, 'empty-folder': null, 'f.html': 'test' }); + const stream = fs.createWriteStream('/folder/file2'); + stream.write('A'); + stream.write('BC'); + stream.write('DEF'); + stream.end(); + await new Promise(resolve => stream.once('close', resolve)); + expect(stream.bytesWritten).toBe(6); + expect(mfs.__vol.toJSON()).toStrictEqual({ + '/mountpoint/folder/file': 'test', + '/mountpoint/folder/file2': 'ABCDEF', + '/mountpoint/empty-folder': null, + '/mountpoint/f.html': 'test', + }); + }); + test('can use stream to overwrite existing file', async () => { const { fs, mfs } = setup({ folder: { file: 'test' }, 'empty-folder': null, 'f.html': 'test' }); const stream = fs.createWriteStream('/folder/file'); diff --git a/yarn.lock b/yarn.lock index eee6bb81e..5057de49e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1477,6 +1477,13 @@ abbrev@^1.0.0, abbrev@~1.1.1: resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8" integrity sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q== +abort-controller@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/abort-controller/-/abort-controller-3.0.0.tgz#eaf54d53b62bae4138e809ca225c8439a6efb392" + integrity sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg== + dependencies: + event-target-shim "^5.0.0" + accepts@~1.3.4, accepts@~1.3.5, accepts@~1.3.8: version "1.3.8" resolved "https://registry.yarnpkg.com/accepts/-/accepts-1.3.8.tgz#0bf0be125b67014adcb0b0921e62db7bffe16b2e" @@ -2815,12 +2822,17 @@ etag@~1.8.1: resolved "https://registry.yarnpkg.com/etag/-/etag-1.8.1.tgz#41ae2eeb65efa62268aebfea83ac7d79299b0887" integrity sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg== +event-target-shim@^5.0.0: + version "5.0.1" + resolved "https://registry.yarnpkg.com/event-target-shim/-/event-target-shim-5.0.1.tgz#5d4d3ebdf9583d63a5333ce2deb7480ab2b05789" + integrity sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ== + eventemitter3@^4.0.0: version "4.0.7" resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-4.0.7.tgz#2de9b68f6528d5644ef5c59526a1b4a07306169f" integrity sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw== -events@^3.2.0: +events@^3.2.0, events@^3.3.0: version "3.3.0" resolved "https://registry.yarnpkg.com/events/-/events-3.3.0.tgz#31a95ad0a924e2d2c419a813aeb2c4e878ea7400" integrity sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q== @@ -2913,11 +2925,6 @@ fast-deep-equal@^3.1.1, fast-deep-equal@^3.1.3: resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525" integrity sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q== -fast-fifo@^1.1.0: - version "1.2.0" - resolved "https://registry.yarnpkg.com/fast-fifo/-/fast-fifo-1.2.0.tgz#2ee038da2468e8623066dee96958b0c1763aa55a" - integrity sha512-NcvQXt7Cky1cNau15FWy64IjuO8X0JijhTBBrJj1YlxlDfRkJXNaK9RFUjwpfDPzMdv7wB38jr53l9tkNLxnWg== - fast-glob@^3.2.9: version "3.2.12" resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.2.12.tgz#7f39ec99c2e6ab030337142da9e0c18f37afae80" @@ -5675,11 +5682,6 @@ queue-microtask@^1.2.2: resolved "https://registry.yarnpkg.com/queue-microtask/-/queue-microtask-1.2.3.tgz#4929228bbc724dfac43e0efb058caf7b6cfb6243" integrity sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A== -queue-tick@^1.0.1: - version "1.0.1" - resolved "https://registry.yarnpkg.com/queue-tick/-/queue-tick-1.0.1.tgz#f6f07ac82c1fd60f82e098b417a80e52f1f4c142" - integrity sha512-kJt5qhMxoszgU/62PLP1CJytzd2NKetjSRnyuj31fDd3Rlcz3fzlFdFLD1SItunPwyqEOkca6GbV612BWfaBag== - quick-lru@^4.0.1: version "4.0.1" resolved "https://registry.yarnpkg.com/quick-lru/-/quick-lru-4.0.1.tgz#5b8878f113a58217848c6482026c73e1ba57727f" @@ -5798,6 +5800,16 @@ readable-stream@^2.0.0, readable-stream@^2.0.1, readable-stream@^2.0.2, readable string_decoder "~1.1.1" util-deprecate "~1.0.1" +readable-stream@^4.4.0: + version "4.4.0" + resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-4.4.0.tgz#55ce132d60a988c460d75c631e9ccf6a7229b468" + integrity sha512-kDMOq0qLtxV9f/SQv522h8cxZBqNZXuXNyjyezmfAAuribMyVXziljpQ/uQhfE1XLg2/TLTW2DsnoE4VAi/krg== + dependencies: + abort-controller "^3.0.0" + buffer "^6.0.3" + events "^3.3.0" + process "^0.11.10" + readdir-scoped-modules@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/readdir-scoped-modules/-/readdir-scoped-modules-1.1.0.tgz#8d45407b4f870a0dcaebc0e28670d18e74514309" @@ -6329,14 +6341,6 @@ stream-combiner2@~1.1.1: duplexer2 "~0.1.0" readable-stream "^2.0.2" -streamx@^2.15.0: - version "2.15.0" - resolved "https://registry.yarnpkg.com/streamx/-/streamx-2.15.0.tgz#f58c92e6f726b5390dcabd6dd9094d29a854d698" - integrity sha512-HcxY6ncGjjklGs1xsP1aR71INYcsXFJet5CU1CHqihQ2J5nOsbd4OjgjHO42w/4QNv9gZb3BueV+Vxok5pLEXg== - dependencies: - fast-fifo "^1.1.0" - queue-tick "^1.0.1" - string-length@^4.0.1: version "4.0.2" resolved "https://registry.yarnpkg.com/string-length/-/string-length-4.0.2.tgz#a8a8dc7bd5c1a82b9b3c8b87e125f66871b6e57a"