Skip to content

Commit

Permalink
Merge pull request #367 from preactjs/fix/stream-types
Browse files Browse the repository at this point in the history
fix: Add types for renderStream & pipeableStream
  • Loading branch information
rschristian authored May 26, 2024
2 parents ae55a6c + 8c7e08f commit 2e14e1a
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/slimy-eagles-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-render-to-string': patch
---

Add types for `/stream` and `/stream-node` exports
9 changes: 7 additions & 2 deletions jsx.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ interface Options {
skipFalseAttributes?: boolean;
}

export default function render(
export default function renderToStringPretty(
vnode: VNode,
context?: any,
options?: Options
): string;
export function render(vnode: VNode, context?: any, options?: Options): string;

export function shallowRender(vnode: VNode, context?: any): string;
export function shallowRender(
vnode: VNode,
context?: any,
options?: Options
): string;
31 changes: 24 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"bench": "BABEL_ENV=test node -r @babel/register benchmarks index.js",
"bench:v8": "BABEL_ENV=test microbundle benchmarks/index.js -f modern --alias benchmarkjs-pretty=benchmarks/lib/benchmark-lite.js --external none --target node --no-compress --no-sourcemap --raw -o benchmarks/.v8.mjs && v8 --module benchmarks/.v8.modern.js",
"build": "npm run -s transpile && npm run -s transpile:jsx && npm run -s transpile:stream && npm run -s transpile:stream-node && npm run -s copy-typescript-definition",
"postbuild": "node ./config/node-13-exports.js && node ./config/node-commonjs.js && node ./config/node-verify-exports.js",
"postbuild": "node ./config/node-13-exports.js && node ./config/node-commonjs.js && node ./config/node-verify-exports.js && check-export-map",
"transpile": "microbundle src/index.js -f es,cjs,umd",
"transpile:stream": "microbundle src/stream.js -o dist/stream/index.js -f es,cjs,umd",
"transpile:stream-node": "microbundle src/stream-node.js -o dist/stream/node/index.js -f es,cjs,umd --target node",
Expand Down Expand Up @@ -132,16 +132,17 @@
"@babel/register": "^7.12.10",
"@changesets/changelog-github": "^0.4.1",
"@changesets/cli": "^2.18.0",
"baseline-rts": "npm:preact-render-to-string@latest",
"benchmarkjs-pretty": "^2.0.1",
"chai": "^4.2.0",
"check-export-map": "^1.3.1",
"copyfiles": "^2.4.1",
"eslint": "^7.16.0",
"eslint-config-developit": "^1.2.0",
"husky": "^4.3.6",
"lint-staged": "^10.5.3",
"microbundle": "^0.15.1",
"mocha": "^8.2.1",
"baseline-rts": "npm:preact-render-to-string@latest",
"preact": "^10.13.0",
"prettier": "^2.2.1",
"pretty-format": "^3.8.0",
Expand Down
19 changes: 19 additions & 0 deletions src/stream-node.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { VNode } from 'preact';
import { WritableStream } from 'node:stream';

interface RenderToPipeableStreamOptions {
onShellReady?: () => void;
onAllReady?: () => void;
onError?: (error: any) => void;
}

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

export function renderToReadableStream(
vnode: VNode,
options: RenderToPipeableStreamOptions,
context?: any
): PipeableStream;
8 changes: 7 additions & 1 deletion src/stream-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ import { renderToChunks } from './lib/chunked.js';
* @property {(error) => void} [onError]
*/

/**
* @typedef {object} PipeableStream
* @property {() => void} abort
* @property {(writable: import('stream').Writable) => void} pipe
*/

/**
* @param {import('preact').VNode} vnode
* @param {RenderToPipeableStreamOptions} options
* @param {any} [context]
* @returns {{}}
* @returns {PipeableStream}
*/
export function renderToPipeableStream(vnode, options, context) {
const encoder = new TextEncoder('utf-8');
Expand Down
10 changes: 10 additions & 0 deletions src/stream.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { VNode } from 'preact';

interface RenderStream extends ReadableStream<Uint8Array> {
allReady: Promise<void>;
}

export function renderToReadableStream(
vnode: VNode,
context?: any
): RenderStream;

0 comments on commit 2e14e1a

Please sign in to comment.