Skip to content

Commit

Permalink
renderToPipeableStream: expose errors through onError, no longer emit…
Browse files Browse the repository at this point in the history
… un-catchable error event on internal stream (#405)
  • Loading branch information
f0x52 authored Jan 12, 2025
1 parent c819f51 commit 0a698f6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/mighty-keys-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-render-to-string': patch
---

renderToPipeableStream: expose errors through onError, no longer emit un-catchable error event on internal stream
2 changes: 1 addition & 1 deletion src/stream-node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface RenderToPipeableStreamOptions {
}

interface PipeableStream {
abort: () => void;
abort: (reason?: unknown) => void;
pipe: (writable: WritableStream) => void;
}

Expand Down
17 changes: 14 additions & 3 deletions src/stream-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,28 @@ export function renderToPipeableStream(vnode, options, context) {
stream.end();
})
.catch((error) => {
stream.destroy(error);
stream.destroy();
if (options.onError) {
options.onError(error);
} else {
throw error;
}
});

Promise.resolve().then(() => {
options.onShellReady && options.onShellReady();
});

return {
abort() {
/**
* @param {unknown} [reason]
*/
abort(reason = new Error('The render was aborted by the server without a reason.')) {
controller.abort();
stream.destroy(new Error('aborted'));
stream.destroy();
if (options.onError) {
options.onError(reason);
}
},
/**
* @param {import("stream").Writable} writable
Expand Down

0 comments on commit 0a698f6

Please sign in to comment.