-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5871 from NomicFoundation/output-streams
Await output streams
- Loading branch information
Showing
5 changed files
with
92 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Writable } from "node:stream"; | ||
|
||
/** | ||
* Creates a Tansform that writes everything to actualWritable, without closing it | ||
* when finished. | ||
* | ||
* This is useful to pipe things to stdout, without closing it, while being | ||
* able to await for the result of the pipe to finish. | ||
*/ | ||
export function createNonClosingWriter(actualWritable: Writable): Writable { | ||
return new Writable({ | ||
write(chunk, encoding, callback) { | ||
actualWritable.write(chunk, encoding, callback); | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import assert from "node:assert/strict"; | ||
import { Readable, Writable } from "node:stream"; | ||
import { pipeline } from "node:stream/promises"; | ||
import { describe, it } from "node:test"; | ||
|
||
import { createNonClosingWriter } from "../src/stream.js"; | ||
|
||
function createFixedReadable(data: Buffer[]) { | ||
const streamData = [...data]; | ||
|
||
const stream = new Readable({ | ||
encoding: "utf8", | ||
read(_size) { | ||
const chunk = streamData.shift(); | ||
|
||
if (chunk === undefined) { | ||
this.push(null); | ||
return; | ||
} | ||
|
||
this.push(chunk); | ||
}, | ||
}); | ||
|
||
return stream; | ||
} | ||
|
||
function createWritable() { | ||
const data: string[] = []; | ||
const writable = new Writable({ | ||
write(chunk, _encoding, callback) { | ||
data.push(chunk); | ||
|
||
callback(); | ||
}, | ||
}); | ||
|
||
return { writable, data }; | ||
} | ||
|
||
describe("stream", () => { | ||
describe("createNonClosingWriter", () => { | ||
const FIXTURE_DATA = [Buffer.from("a"), Buffer.from("b"), Buffer.from("c")]; | ||
|
||
it("Should not close the actual writable when finished", async () => { | ||
const readable = createFixedReadable(FIXTURE_DATA); | ||
const { writable } = createWritable(); | ||
const writer = createNonClosingWriter(writable); | ||
await pipeline(readable, writer); | ||
|
||
assert.equal(readable.closed, true); | ||
assert.equal(writer.closed, true); | ||
assert.equal(writable.closed, false); | ||
}); | ||
|
||
it("Should write all the data to the actual writable", async () => { | ||
const readable = createFixedReadable(FIXTURE_DATA); | ||
const { writable, data } = createWritable(); | ||
const writer = createNonClosingWriter(writable); | ||
await pipeline(readable, writer); | ||
|
||
assert.deepEqual(data, FIXTURE_DATA); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters