Last active
March 2, 2026 15:11
-
-
Save forivall/528991d94dc9315fa6683b3c2402daee to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /** | |
| * @param {import('stream').Transform} stream | |
| * @param {string} data | |
| */ | |
| function transformSync(stream, data) { | |
| if (data.length > stream.writableHighWaterMark) { | |
| throw new Error('Data is too large for synchronous transform'); | |
| } | |
| let done = false; | |
| stream.write(data, (err) => { | |
| if (err) { | |
| throw err; | |
| } else { | |
| done = true; | |
| } | |
| }); | |
| /** @type Array<string | object> */ | |
| const results = []; | |
| let dataReceived = false; | |
| stream.on('data', (chunk) => { | |
| results.push(chunk); | |
| dataReceived = true; | |
| }); | |
| if (!done) { | |
| throw new Error('Stream did not flush after write'); | |
| } | |
| if (!dataReceived) { | |
| throw new Error('Transform did not return data synchronously'); | |
| } | |
| return results; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment