@zubyjs/react 1.0.74 → 1.0.75

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zubyjs/react",
3
- "version": "1.0.74",
3
+ "version": "1.0.75",
4
4
  "description": "Zuby.js JsxProvider for react",
5
5
  "type": "module",
6
6
  "main": "index.js",
package/render.d.ts CHANGED
@@ -1,14 +1,10 @@
1
1
  import { RenderToString, RenderToStream } from 'zuby/types.js';
2
- /**
3
- * Converts a ReadableStream to a string.
4
- * @param {NodeJS.ReadableStream} stream
5
- */
6
- export declare const streamToString: (stream: NodeJS.ReadableStream) => Promise<string>;
7
2
  /**
8
3
  * @param {ReactElement} vnode The root JSX element to render (eg: `<App />`)
9
4
  */
10
5
  export declare const renderToStream: RenderToStream;
11
6
  /**
7
+ * Renders a React element to a string using renderToPipeableStream.
12
8
  * @param {ReactElement} vnode The root JSX element to render (eg: `<App />`)
13
9
  */
14
10
  export declare const renderToString: RenderToString;
package/render.js CHANGED
@@ -1,26 +1,60 @@
1
- import { renderToStaticNodeStream } from 'react-dom/server';
2
- /**
3
- * Converts a ReadableStream to a string.
4
- * @param {NodeJS.ReadableStream} stream
5
- */
6
- export const streamToString = (stream) => {
7
- const chunks = [];
8
- return new Promise((resolve, reject) => {
9
- stream.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
10
- stream.on('error', (e) => reject(e));
11
- stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
12
- });
13
- };
1
+ import { renderToPipeableStream } from 'react-dom/server';
2
+ import { Readable, Writable } from 'stream';
14
3
  /**
15
4
  * @param {ReactElement} vnode The root JSX element to render (eg: `<App />`)
16
5
  */
17
6
  export const renderToStream = async (vnode) => {
18
- return renderToStaticNodeStream(vnode);
7
+ return new Promise((resolve, reject) => {
8
+ const { pipe } = renderToPipeableStream(vnode, {
9
+ onAllReady() {
10
+ const chunks = [];
11
+ const writableStream = new Writable({
12
+ write(chunk, encoding, callback) {
13
+ chunks.push(Buffer.from(chunk, encoding));
14
+ callback();
15
+ },
16
+ final(callback) {
17
+ const readableStream = new Readable({
18
+ read() {
19
+ this.push(Buffer.concat(chunks));
20
+ this.push(null);
21
+ },
22
+ });
23
+ resolve(readableStream);
24
+ callback();
25
+ },
26
+ });
27
+ pipe(writableStream);
28
+ },
29
+ onError(error) {
30
+ reject(error);
31
+ },
32
+ });
33
+ });
19
34
  };
20
35
  /**
36
+ * Renders a React element to a string using renderToPipeableStream.
21
37
  * @param {ReactElement} vnode The root JSX element to render (eg: `<App />`)
22
38
  */
23
39
  export const renderToString = async (vnode) => {
24
- const stream = renderToStaticNodeStream(vnode);
25
- return await streamToString(stream);
40
+ return new Promise((resolve, reject) => {
41
+ const { pipe } = renderToPipeableStream(vnode, {
42
+ onAllReady() {
43
+ const writableStream = new Writable({
44
+ write(chunk, encoding, callback) {
45
+ chunks.push(Buffer.from(chunk, encoding));
46
+ callback();
47
+ },
48
+ });
49
+ const chunks = [];
50
+ writableStream.on('finish', () => {
51
+ resolve(Buffer.concat(chunks).toString('utf8'));
52
+ });
53
+ pipe(writableStream);
54
+ },
55
+ onError(error) {
56
+ reject(error);
57
+ },
58
+ });
59
+ });
26
60
  };
package/router.js CHANGED
@@ -78,6 +78,8 @@ function matchTemplate(template, path) {
78
78
  });
79
79
  }
80
80
  export function Link(props) {
81
+ // Shallow copy props to avoid mutation
82
+ props = { ...props };
81
83
  const [isActive] = useRoute(props.href);
82
84
  const { localizePath } = usePageContext();
83
85
  let href = props.href || props.to || '#';