Skip to content

Instantly share code, notes, and snippets.

@forivall
Created February 28, 2026 03:30
Show Gist options
  • Select an option

  • Save forivall/528991d94dc9315fa6683b3c2402daee to your computer and use it in GitHub Desktop.

Select an option

Save forivall/528991d94dc9315fa6683b3c2402daee to your computer and use it in GitHub Desktop.
/**
* @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 = [];
stream.on('data', (chunk) => results.push(chunk));
if (!done) {
throw new Error('Stream did not flush after write');
}
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment