houdini-react 1.2.24 → 1.2.26

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.
Files changed (128) hide show
  1. package/build/plugin-cjs/index.js +1308 -1407
  2. package/build/plugin-esm/index.js +1308 -1407
  3. package/build/runtime/index.d.ts +2 -1
  4. package/build/runtime/routing/Router.d.ts +2 -1
  5. package/build/runtime/server/index.d.ts +17 -0
  6. package/build/runtime/server/renderToStream/createBuffer.d.ts +17 -0
  7. package/build/runtime/server/renderToStream/createPipeWrapper.d.ts +12 -0
  8. package/build/runtime/server/renderToStream/createReadableWrapper.d.ts +6 -0
  9. package/build/runtime/server/renderToStream/loadNodeStreamModule.d.ts +10 -0
  10. package/build/runtime/server/renderToStream/resolveSeoStrategy.d.ts +9 -0
  11. package/build/runtime/server/renderToStream.d.ts +28 -0
  12. package/build/runtime/server/shared/initData.d.ts +8 -0
  13. package/build/runtime/server/shared/key.d.ts +4 -0
  14. package/build/runtime/server/shared/utils.d.ts +3 -0
  15. package/build/runtime/server/useStream.d.ts +9 -0
  16. package/build/runtime/server/utils/assert.d.ts +15 -0
  17. package/build/runtime/server/utils/createErrorWithCleanStackTrace.d.ts +2 -0
  18. package/build/runtime/server/utils/debug.d.ts +14 -0
  19. package/build/runtime/server/utils/getGlobalVariable.d.ts +4 -0
  20. package/build/runtime/server/utils/isBrowser.d.ts +2 -0
  21. package/build/runtime/server/utils/isCallable.d.ts +2 -0
  22. package/build/runtime/server/utils/isClientSide.d.ts +2 -0
  23. package/build/runtime/server/utils/isPromise.d.ts +1 -0
  24. package/build/runtime/server/utils/isServerSide.d.ts +2 -0
  25. package/build/runtime/server/utils/objectAssign.d.ts +2 -0
  26. package/build/runtime/server/utils/projectInfo.d.ts +10 -0
  27. package/build/runtime/server/utils.d.ts +4 -0
  28. package/build/runtime-cjs/index.d.ts +2 -1
  29. package/build/runtime-cjs/index.js +11 -2
  30. package/build/runtime-cjs/routing/Router.d.ts +2 -1
  31. package/build/runtime-cjs/routing/Router.js +8 -7
  32. package/build/runtime-cjs/server/index.d.ts +17 -0
  33. package/build/runtime-cjs/server/index.js +28 -0
  34. package/build/runtime-cjs/server/renderToStream/createBuffer.d.ts +17 -0
  35. package/build/runtime-cjs/server/renderToStream/createBuffer.js +101 -0
  36. package/build/runtime-cjs/server/renderToStream/createPipeWrapper.d.ts +12 -0
  37. package/build/runtime-cjs/server/renderToStream/createPipeWrapper.js +90 -0
  38. package/build/runtime-cjs/server/renderToStream/createReadableWrapper.d.ts +6 -0
  39. package/build/runtime-cjs/server/renderToStream/createReadableWrapper.js +85 -0
  40. package/build/runtime-cjs/server/renderToStream/loadNodeStreamModule.d.ts +10 -0
  41. package/build/runtime-cjs/server/renderToStream/loadNodeStreamModule.js +51 -0
  42. package/build/runtime-cjs/server/renderToStream/resolveSeoStrategy.d.ts +9 -0
  43. package/build/runtime-cjs/server/renderToStream/resolveSeoStrategy.js +44 -0
  44. package/build/runtime-cjs/server/renderToStream.d.ts +28 -0
  45. package/build/runtime-cjs/server/renderToStream.js +183 -0
  46. package/build/runtime-cjs/server/shared/initData.d.ts +8 -0
  47. package/build/runtime-cjs/server/shared/initData.js +28 -0
  48. package/build/runtime-cjs/server/shared/key.d.ts +4 -0
  49. package/build/runtime-cjs/server/shared/key.js +45 -0
  50. package/build/runtime-cjs/server/shared/utils.d.ts +3 -0
  51. package/build/runtime-cjs/server/shared/utils.js +20 -0
  52. package/build/runtime-cjs/server/useStream.d.ts +9 -0
  53. package/build/runtime-cjs/server/useStream.js +44 -0
  54. package/build/runtime-cjs/server/utils/assert.d.ts +15 -0
  55. package/build/runtime-cjs/server/utils/assert.js +119 -0
  56. package/build/runtime-cjs/server/utils/createErrorWithCleanStackTrace.d.ts +2 -0
  57. package/build/runtime-cjs/server/utils/createErrorWithCleanStackTrace.js +62 -0
  58. package/build/runtime-cjs/server/utils/debug.d.ts +14 -0
  59. package/build/runtime-cjs/server/utils/debug.js +112 -0
  60. package/build/runtime-cjs/server/utils/getGlobalVariable.d.ts +4 -0
  61. package/build/runtime-cjs/server/utils/getGlobalVariable.js +32 -0
  62. package/build/runtime-cjs/server/utils/isBrowser.d.ts +2 -0
  63. package/build/runtime-cjs/server/utils/isBrowser.js +30 -0
  64. package/build/runtime-cjs/server/utils/isCallable.d.ts +2 -0
  65. package/build/runtime-cjs/server/utils/isCallable.js +30 -0
  66. package/build/runtime-cjs/server/utils/isClientSide.d.ts +2 -0
  67. package/build/runtime-cjs/server/utils/isClientSide.js +30 -0
  68. package/build/runtime-cjs/server/utils/isPromise.d.ts +1 -0
  69. package/build/runtime-cjs/server/utils/isPromise.js +31 -0
  70. package/build/runtime-cjs/server/utils/isServerSide.d.ts +2 -0
  71. package/build/runtime-cjs/server/utils/isServerSide.js +31 -0
  72. package/build/runtime-cjs/server/utils/objectAssign.d.ts +2 -0
  73. package/build/runtime-cjs/server/utils/objectAssign.js +30 -0
  74. package/build/runtime-cjs/server/utils/projectInfo.d.ts +10 -0
  75. package/build/runtime-cjs/server/utils/projectInfo.js +37 -0
  76. package/build/runtime-cjs/server/utils.d.ts +4 -0
  77. package/build/runtime-cjs/server/utils.js +21 -0
  78. package/build/runtime-esm/index.d.ts +2 -1
  79. package/build/runtime-esm/index.js +11 -2
  80. package/build/runtime-esm/routing/Router.d.ts +2 -1
  81. package/build/runtime-esm/routing/Router.js +8 -7
  82. package/build/runtime-esm/server/index.d.ts +17 -0
  83. package/build/runtime-esm/server/index.js +4 -0
  84. package/build/runtime-esm/server/renderToStream/createBuffer.d.ts +17 -0
  85. package/build/runtime-esm/server/renderToStream/createBuffer.js +77 -0
  86. package/build/runtime-esm/server/renderToStream/createPipeWrapper.d.ts +12 -0
  87. package/build/runtime-esm/server/renderToStream/createPipeWrapper.js +66 -0
  88. package/build/runtime-esm/server/renderToStream/createReadableWrapper.d.ts +6 -0
  89. package/build/runtime-esm/server/renderToStream/createReadableWrapper.js +61 -0
  90. package/build/runtime-esm/server/renderToStream/loadNodeStreamModule.d.ts +10 -0
  91. package/build/runtime-esm/server/renderToStream/loadNodeStreamModule.js +20 -0
  92. package/build/runtime-esm/server/renderToStream/resolveSeoStrategy.d.ts +9 -0
  93. package/build/runtime-esm/server/renderToStream/resolveSeoStrategy.js +20 -0
  94. package/build/runtime-esm/server/renderToStream.d.ts +28 -0
  95. package/build/runtime-esm/server/renderToStream.js +152 -0
  96. package/build/runtime-esm/server/shared/initData.d.ts +8 -0
  97. package/build/runtime-esm/server/shared/initData.js +4 -0
  98. package/build/runtime-esm/server/shared/key.d.ts +4 -0
  99. package/build/runtime-esm/server/shared/key.js +20 -0
  100. package/build/runtime-esm/server/shared/utils.d.ts +3 -0
  101. package/build/runtime-esm/server/shared/utils.js +3 -0
  102. package/build/runtime-esm/server/useStream.d.ts +9 -0
  103. package/build/runtime-esm/server/useStream.js +13 -0
  104. package/build/runtime-esm/server/utils/assert.d.ts +15 -0
  105. package/build/runtime-esm/server/utils/assert.js +91 -0
  106. package/build/runtime-esm/server/utils/createErrorWithCleanStackTrace.d.ts +2 -0
  107. package/build/runtime-esm/server/utils/createErrorWithCleanStackTrace.js +38 -0
  108. package/build/runtime-esm/server/utils/debug.d.ts +14 -0
  109. package/build/runtime-esm/server/utils/debug.js +87 -0
  110. package/build/runtime-esm/server/utils/getGlobalVariable.d.ts +4 -0
  111. package/build/runtime-esm/server/utils/getGlobalVariable.js +8 -0
  112. package/build/runtime-esm/server/utils/isBrowser.d.ts +2 -0
  113. package/build/runtime-esm/server/utils/isBrowser.js +6 -0
  114. package/build/runtime-esm/server/utils/isCallable.d.ts +2 -0
  115. package/build/runtime-esm/server/utils/isCallable.js +6 -0
  116. package/build/runtime-esm/server/utils/isClientSide.d.ts +2 -0
  117. package/build/runtime-esm/server/utils/isClientSide.js +6 -0
  118. package/build/runtime-esm/server/utils/isPromise.d.ts +1 -0
  119. package/build/runtime-esm/server/utils/isPromise.js +7 -0
  120. package/build/runtime-esm/server/utils/isServerSide.d.ts +2 -0
  121. package/build/runtime-esm/server/utils/isServerSide.js +7 -0
  122. package/build/runtime-esm/server/utils/objectAssign.d.ts +2 -0
  123. package/build/runtime-esm/server/utils/objectAssign.js +6 -0
  124. package/build/runtime-esm/server/utils/projectInfo.d.ts +10 -0
  125. package/build/runtime-esm/server/utils/projectInfo.js +13 -0
  126. package/build/runtime-esm/server/utils.d.ts +4 -0
  127. package/build/runtime-esm/server/utils.js +4 -0
  128. package/package.json +4 -4
@@ -13,7 +13,8 @@ function Router({
13
13
  pending_cache,
14
14
  last_variables,
15
15
  session,
16
- assetPrefix
16
+ assetPrefix,
17
+ injectToStream
17
18
  }) {
18
19
  return /* @__PURE__ */ jsx(
19
20
  RouterContextProvider,
@@ -26,7 +27,15 @@ function Router({
26
27
  pending_cache,
27
28
  last_variables,
28
29
  session,
29
- children: /* @__PURE__ */ jsx(RouterImpl, { initialURL, manifest, assetPrefix })
30
+ children: /* @__PURE__ */ jsx(
31
+ RouterImpl,
32
+ {
33
+ initialURL,
34
+ manifest,
35
+ assetPrefix,
36
+ injectToStream
37
+ }
38
+ )
30
39
  }
31
40
  );
32
41
  }
@@ -12,10 +12,11 @@ type ComponentType = any;
12
12
  * It is responsible for loading various page sources (including API fetches) and
13
13
  * then rendering when appropriate.
14
14
  */
15
- export declare function Router({ manifest, initialURL, assetPrefix, }: {
15
+ export declare function Router({ manifest, initialURL, assetPrefix, injectToStream, }: {
16
16
  manifest: RouterManifest<ComponentType>;
17
17
  initialURL?: string;
18
18
  assetPrefix: string;
19
+ injectToStream?: undefined | ((chunk: string) => void);
19
20
  }): JSX.Element;
20
21
  export declare function RouterContextProvider({ children, client, cache, artifact_cache, component_cache, data_cache, pending_cache, last_variables, session: ssrSession, }: {
21
22
  children: React.ReactElement;
@@ -2,7 +2,6 @@ import { jsx } from "react/jsx-runtime";
2
2
  import { deepEquals } from "$houdini/runtime/lib/deepEquals";
3
3
  import { find_match } from "$houdini/runtime/router/match";
4
4
  import React from "react";
5
- import { useStream } from "react-streaming";
6
5
  import { useDocumentStore } from "../hooks/useDocumentStore";
7
6
  import { suspense_cache } from "./cache";
8
7
  const PreloadWhich = {
@@ -13,7 +12,8 @@ const PreloadWhich = {
13
12
  function Router({
14
13
  manifest,
15
14
  initialURL,
16
- assetPrefix
15
+ assetPrefix,
16
+ injectToStream
17
17
  }) {
18
18
  const [current, setCurrent] = React.useState(() => {
19
19
  return initialURL || window.location.pathname;
@@ -22,7 +22,8 @@ function Router({
22
22
  const { loadData, loadComponent } = usePageData({
23
23
  page,
24
24
  variables,
25
- assetPrefix
25
+ assetPrefix,
26
+ injectToStream
26
27
  });
27
28
  const { component_cache } = useRouterContext();
28
29
  const PageComponent = component_cache.get(page.id);
@@ -60,7 +61,8 @@ function Router({
60
61
  function usePageData({
61
62
  page,
62
63
  variables,
63
- assetPrefix
64
+ assetPrefix,
65
+ injectToStream
64
66
  }) {
65
67
  const {
66
68
  client,
@@ -71,7 +73,6 @@ function usePageData({
71
73
  pending_cache,
72
74
  last_variables
73
75
  } = useRouterContext();
74
- const stream = useStream();
75
76
  const session = useSession();
76
77
  function load_query({ id, artifact }) {
77
78
  last_variables.set(page.id, variables);
@@ -92,7 +93,7 @@ function usePageData({
92
93
  session
93
94
  }).then(() => {
94
95
  data_cache.set(id, observer);
95
- stream?.injectToStream(`
96
+ injectToStream?.(`
96
97
  <script>
97
98
  window.__houdini__cache__?.hydrate(${cache.serialize()}, window.__houdini__hydration__layer)
98
99
 
@@ -165,7 +166,7 @@ function usePageData({
165
166
  targetPage.documents[artifact_id].artifact().then((mod) => {
166
167
  const artifact = mod.default;
167
168
  artifact_cache.set(artifact_id, artifact);
168
- stream?.injectToStream(`
169
+ injectToStream?.(`
169
170
  <script type="module" src="${assetPrefix}/artifacts/${artifact.name}.js" async=""><\/script>
170
171
  `);
171
172
  load_query({ id: artifact.name, artifact });
@@ -0,0 +1,17 @@
1
+ /**
2
+ This directory is largely copied from https://github.com/brillout/react-streaming and adapted to fit the needs of this project. It is subject to the MIT license, found [here](https://github.com/brillout/react-streaming/blob/main/LICENSE.md). Duplicated below for reference:
3
+
4
+ MIT License
5
+
6
+ Copyright (c) 2022-present Romuald Brillout
7
+
8
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
11
+
12
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13
+ */
14
+ import { renderToStream } from './renderToStream';
15
+ import type { InjectToStream } from './renderToStream/createBuffer';
16
+ export { renderToStream };
17
+ export type { InjectToStream };
@@ -0,0 +1,4 @@
1
+ import { renderToStream } from "./renderToStream";
2
+ export {
3
+ renderToStream
4
+ };
@@ -0,0 +1,17 @@
1
+ export { createBuffer };
2
+ export type { InjectToStream };
3
+ export type { StreamOperations };
4
+ type InjectToStream = (chunk: unknown, options?: {
5
+ flush?: boolean;
6
+ }) => void;
7
+ type StreamOperations = {
8
+ operations: null | {
9
+ writeChunk: (chunk: unknown) => void;
10
+ flush: null | (() => void);
11
+ };
12
+ };
13
+ declare function createBuffer(streamOperations: StreamOperations): {
14
+ injectToStream: InjectToStream;
15
+ onBeforeWrite: (chunk: unknown) => void;
16
+ onBeforeEnd: () => void;
17
+ };
@@ -0,0 +1,77 @@
1
+ import { assert, assertUsage, createDebugger } from "../utils";
2
+ const debug = createDebugger("react-streaming:buffer");
3
+ function createBuffer(streamOperations) {
4
+ const buffer = [];
5
+ let state = "UNSTARTED";
6
+ let writePermission = null;
7
+ return { injectToStream, onBeforeWrite, onBeforeEnd };
8
+ function injectToStream(chunk, options) {
9
+ assertUsage(
10
+ state !== "ENDED",
11
+ `Cannot inject following chunk after stream has ended: \`${chunk}\``
12
+ );
13
+ if (debug.isEnabled) {
14
+ debug("injectToStream()", String(chunk));
15
+ }
16
+ buffer.push({ chunk, flush: options?.flush });
17
+ flushBuffer();
18
+ }
19
+ function flushBuffer() {
20
+ if (!writePermission) {
21
+ return;
22
+ }
23
+ if (buffer.length === 0) {
24
+ return;
25
+ }
26
+ if (state !== "STREAMING") {
27
+ assert(state === "UNSTARTED");
28
+ return;
29
+ }
30
+ let flushStream = false;
31
+ buffer.forEach((bufferEntry) => {
32
+ assert(streamOperations.operations);
33
+ const { writeChunk } = streamOperations.operations;
34
+ writeChunk(bufferEntry.chunk);
35
+ if (bufferEntry.flush) {
36
+ flushStream = true;
37
+ }
38
+ });
39
+ buffer.length = 0;
40
+ assert(streamOperations.operations);
41
+ if (flushStream && streamOperations.operations.flush !== null) {
42
+ streamOperations.operations.flush();
43
+ debug("stream flushed");
44
+ }
45
+ }
46
+ function onBeforeWrite(chunk) {
47
+ state === "UNSTARTED" && debug(">>> START");
48
+ if (debug.isEnabled) {
49
+ debug(`react write${!writePermission ? "" : " (allowed)"}`, String(chunk));
50
+ }
51
+ state = "STREAMING";
52
+ if (writePermission) {
53
+ flushBuffer();
54
+ }
55
+ if (writePermission == true || writePermission === null) {
56
+ writePermission = false;
57
+ debug("writePermission =", writePermission);
58
+ setTimeout(() => {
59
+ debug(">>> setTimeout()");
60
+ writePermission = true;
61
+ debug("writePermission =", writePermission);
62
+ flushBuffer();
63
+ });
64
+ }
65
+ }
66
+ function onBeforeEnd() {
67
+ writePermission = true;
68
+ debug("writePermission =", writePermission);
69
+ flushBuffer();
70
+ assert(buffer.length === 0);
71
+ state = "ENDED";
72
+ debug(">>> END");
73
+ }
74
+ }
75
+ export {
76
+ createBuffer
77
+ };
@@ -0,0 +1,12 @@
1
+ /// <reference types="node" />
2
+ import type { Writable as StreamNodeWritable } from 'stream';
3
+ export { createPipeWrapper };
4
+ export type { Pipe };
5
+ type Pipe = (writable: StreamNodeWritable) => void;
6
+ declare function createPipeWrapper(pipeFromReact: Pipe, { onReactBug }: {
7
+ onReactBug: (err: unknown) => void;
8
+ }): Promise<{
9
+ pipeForUser: Pipe;
10
+ streamEnd: Promise<void>;
11
+ injectToStream: import("./createBuffer").InjectToStream;
12
+ }>;
@@ -0,0 +1,66 @@
1
+ import { createDebugger } from "../utils";
2
+ import { createBuffer } from "./createBuffer";
3
+ import { loadNodeStreamModule } from "./loadNodeStreamModule";
4
+ const debug = createDebugger("react-streaming:createPipeWrapper");
5
+ async function createPipeWrapper(pipeFromReact, { onReactBug }) {
6
+ const { Writable } = await loadNodeStreamModule();
7
+ const { pipeForUser, streamEnd } = createPipeForUser();
8
+ const streamOperations = {
9
+ operations: null
10
+ };
11
+ const { injectToStream, onBeforeWrite, onBeforeEnd } = createBuffer(streamOperations);
12
+ return { pipeForUser, streamEnd, injectToStream };
13
+ function createPipeForUser() {
14
+ debug("createPipeForUser()");
15
+ let onEnded;
16
+ const streamEnd2 = new Promise((r) => {
17
+ onEnded = () => r();
18
+ });
19
+ const pipeForUser2 = (writableFromUser) => {
20
+ const writableForReact = new Writable({
21
+ write(chunk, encoding, callback) {
22
+ debug("write");
23
+ onBeforeWrite(chunk);
24
+ if (!writableFromUser.destroyed) {
25
+ writableFromUser.write(chunk, encoding, callback);
26
+ } else {
27
+ writableForReact.destroy();
28
+ }
29
+ },
30
+ final(callback) {
31
+ debug("final");
32
+ onBeforeEnd();
33
+ writableFromUser.end();
34
+ onEnded();
35
+ callback();
36
+ },
37
+ destroy(err) {
38
+ debug(`destroy (\`!!err === ${!!err}\`)`);
39
+ if (err)
40
+ onReactBug(err);
41
+ writableFromUser.destroy(err ?? void 0);
42
+ onEnded();
43
+ }
44
+ });
45
+ const flush = () => {
46
+ if (typeof writableFromUser.flush === "function") {
47
+ ;
48
+ writableFromUser.flush();
49
+ debug("stream flushed (Node.js Writable)");
50
+ }
51
+ };
52
+ streamOperations.operations = {
53
+ flush,
54
+ writeChunk(chunk) {
55
+ writableFromUser.write(chunk);
56
+ }
57
+ };
58
+ writableForReact.flush = flush;
59
+ pipeFromReact(writableForReact);
60
+ };
61
+ return { pipeForUser: pipeForUser2, streamEnd: streamEnd2 };
62
+ }
63
+ }
64
+ export {
65
+ createPipeWrapper
66
+ };
@@ -0,0 +1,6 @@
1
+ export { createReadableWrapper };
2
+ declare function createReadableWrapper(readableFromReact: ReadableStream): {
3
+ readableForUser: ReadableStream<any>;
4
+ streamEnd: Promise<void>;
5
+ injectToStream: import("./createBuffer").InjectToStream;
6
+ };
@@ -0,0 +1,61 @@
1
+ import { createBuffer } from "./createBuffer";
2
+ function createReadableWrapper(readableFromReact) {
3
+ const streamOperations = {
4
+ operations: null
5
+ };
6
+ let controllerOfUserStream;
7
+ let onEnded;
8
+ const streamEnd = new Promise((r) => {
9
+ onEnded = () => r();
10
+ });
11
+ const readableForUser = new ReadableStream({
12
+ start(controller) {
13
+ controllerOfUserStream = controller;
14
+ onReady(onEnded);
15
+ }
16
+ });
17
+ const { injectToStream, onBeforeWrite, onBeforeEnd } = createBuffer(streamOperations);
18
+ return { readableForUser, streamEnd, injectToStream };
19
+ async function onReady(onEnded2) {
20
+ streamOperations.operations = {
21
+ writeChunk(chunk) {
22
+ controllerOfUserStream.enqueue(encodeForWebStream(chunk));
23
+ },
24
+ flush: null
25
+ };
26
+ const reader = readableFromReact.getReader();
27
+ while (true) {
28
+ let result;
29
+ try {
30
+ result = await reader.read();
31
+ } catch (err) {
32
+ controllerOfUserStream.close();
33
+ throw err;
34
+ }
35
+ const { value, done } = result;
36
+ if (done) {
37
+ break;
38
+ }
39
+ onBeforeWrite(value);
40
+ streamOperations.operations.writeChunk(value);
41
+ }
42
+ setTimeout(() => {
43
+ onBeforeEnd();
44
+ controllerOfUserStream.close();
45
+ onEnded2();
46
+ }, 0);
47
+ }
48
+ }
49
+ let encoder;
50
+ function encodeForWebStream(thing) {
51
+ if (!encoder) {
52
+ encoder = new TextEncoder();
53
+ }
54
+ if (typeof thing === "string") {
55
+ return encoder.encode(thing);
56
+ }
57
+ return thing;
58
+ }
59
+ export {
60
+ createReadableWrapper
61
+ };
@@ -0,0 +1,10 @@
1
+ /// <reference types="node" />
2
+ import type { Readable as StreamNodeReadable, Writable as StreamNodeWritable } from 'stream';
3
+ export { loadNodeStreamModule };
4
+ export { nodeStreamModuleIsAvailable };
5
+ type StreamModule = {
6
+ Readable: typeof StreamNodeReadable;
7
+ Writable: typeof StreamNodeWritable;
8
+ };
9
+ declare function loadNodeStreamModule(): Promise<StreamModule>;
10
+ declare function nodeStreamModuleIsAvailable(): Promise<boolean>;
@@ -0,0 +1,20 @@
1
+ async function loadNodeStreamModule() {
2
+ const streamModule = await loadModule();
3
+ const { Readable, Writable } = streamModule;
4
+ return { Readable, Writable };
5
+ }
6
+ async function nodeStreamModuleIsAvailable() {
7
+ try {
8
+ await loadModule();
9
+ return true;
10
+ } catch (err) {
11
+ return false;
12
+ }
13
+ }
14
+ function loadModule() {
15
+ return import("stream");
16
+ }
17
+ export {
18
+ loadNodeStreamModule,
19
+ nodeStreamModuleIsAvailable
20
+ };
@@ -0,0 +1,9 @@
1
+ export { resolveSeoStrategy };
2
+ export type { SeoStrategy };
3
+ type SeoStrategy = 'conservative' | 'google-speed';
4
+ declare function resolveSeoStrategy(options?: {
5
+ seoStrategy?: SeoStrategy;
6
+ userAgent?: string;
7
+ }): {
8
+ disableStream: boolean;
9
+ };
@@ -0,0 +1,20 @@
1
+ import { assertWarning } from "../utils";
2
+ function resolveSeoStrategy(options = {}) {
3
+ const seoStrategy = options.seoStrategy || "conservative";
4
+ if (!options.userAgent) {
5
+ assertWarning(
6
+ false,
7
+ "Streaming disabled. Provide `options.userAgent` to enable streaming. (react-streaming needs the User Agent string in order to be able to disable streaming for bots, e.g. for Google Bot.) Or set `options.disable` to `true` to get rid of this warning.",
8
+ { onlyOnce: true }
9
+ );
10
+ return { disableStream: true };
11
+ }
12
+ const isGoogleBot = options.userAgent.toLowerCase().includes("googlebot");
13
+ if (seoStrategy === "google-speed" && isGoogleBot) {
14
+ return { disableStream: false };
15
+ }
16
+ return { disableStream: true };
17
+ }
18
+ export {
19
+ resolveSeoStrategy
20
+ };
@@ -0,0 +1,28 @@
1
+ import React from 'react';
2
+ import type { renderToPipeableStream as RenderToPipeableStream, renderToReadableStream as RenderToReadableStream } from 'react-dom/server';
3
+ import { Pipe } from './renderToStream/createPipeWrapper';
4
+ import { SeoStrategy } from './renderToStream/resolveSeoStrategy';
5
+ export { renderToStream };
6
+ export { disable };
7
+ type Options = {
8
+ webStream?: boolean;
9
+ disable?: boolean;
10
+ seoStrategy?: SeoStrategy;
11
+ userAgent?: string;
12
+ onBoundaryError?: (err: unknown) => void;
13
+ renderToReadableStream?: typeof RenderToReadableStream;
14
+ renderToPipeableStream?: typeof RenderToPipeableStream;
15
+ };
16
+ type Result = ({
17
+ pipe: Pipe;
18
+ readable: null;
19
+ } | {
20
+ pipe: null;
21
+ readable: ReadableStream;
22
+ }) & {
23
+ streamEnd: Promise<boolean>;
24
+ disabled: boolean;
25
+ injectToStream: (chunk: unknown) => void;
26
+ };
27
+ declare function disable(): void;
28
+ declare function renderToStream(element: React.ReactNode, options?: Options): Promise<Result>;
@@ -0,0 +1,152 @@
1
+ import React from "react";
2
+ import { createPipeWrapper } from "./renderToStream/createPipeWrapper";
3
+ import { createReadableWrapper } from "./renderToStream/createReadableWrapper";
4
+ import { nodeStreamModuleIsAvailable } from "./renderToStream/loadNodeStreamModule";
5
+ import { resolveSeoStrategy } from "./renderToStream/resolveSeoStrategy";
6
+ import { createDebugger } from "./utils";
7
+ const debug = createDebugger("react-streaming:flow");
8
+ const globalConfig = globalThis.__react_streaming = globalThis.__react_streaming || {
9
+ disable: false
10
+ };
11
+ function disable() {
12
+ globalConfig.disable = true;
13
+ }
14
+ async function renderToStream(element, options = {}) {
15
+ const buffer = [];
16
+ let injectToStream = (chunk) => buffer.push(chunk);
17
+ element = React.cloneElement(element, { injectToStream });
18
+ const disable2 = globalConfig.disable || (options.disable ?? resolveSeoStrategy(options).disableStream);
19
+ const webStream = options.webStream ?? !await nodeStreamModuleIsAvailable();
20
+ debug(`disable === ${disable2} && webStream === ${webStream}`);
21
+ let result;
22
+ const resultPartial = { disabled: disable2 };
23
+ if (!webStream) {
24
+ result = { ...resultPartial, ...await renderToNodeStream(element, disable2, options) };
25
+ } else {
26
+ result = { ...resultPartial, ...await renderToWebStream(element, disable2, options) };
27
+ }
28
+ injectToStream = result.injectToStream;
29
+ buffer.forEach((chunk) => injectToStream(chunk));
30
+ buffer.length = 0;
31
+ debug("promise `await renderToStream()` resolved");
32
+ return result;
33
+ }
34
+ async function renderToNodeStream(element, disable2, options) {
35
+ debug("creating Node.js Stream Pipe");
36
+ let onAllReady;
37
+ const allReady = new Promise((r) => {
38
+ onAllReady = () => r();
39
+ });
40
+ let onShellReady;
41
+ const shellReady = new Promise((r) => {
42
+ onShellReady = () => r();
43
+ });
44
+ let didError = false;
45
+ let firstErr = null;
46
+ let reactBug = null;
47
+ const onError = (err) => {
48
+ debug("[react] onError() / onShellError()");
49
+ didError = true;
50
+ firstErr ??= err;
51
+ onShellReady();
52
+ afterReactBugCatch(() => {
53
+ if (err !== reactBug) {
54
+ options.onBoundaryError?.(err);
55
+ }
56
+ });
57
+ };
58
+ const renderToPipeableStream = options.renderToPipeableStream ?? (await import("react-dom/server.node")).renderToPipeableStream;
59
+ const { pipe: pipeOriginal } = renderToPipeableStream(element, {
60
+ onShellReady() {
61
+ debug("[react] onShellReady()");
62
+ onShellReady();
63
+ },
64
+ onAllReady() {
65
+ debug("[react] onAllReady()");
66
+ onShellReady();
67
+ onAllReady();
68
+ },
69
+ onShellError: onError,
70
+ onError
71
+ });
72
+ let promiseResolved = false;
73
+ const { pipeForUser, injectToStream, streamEnd } = await createPipeWrapper(pipeOriginal, {
74
+ onReactBug(err) {
75
+ debug("react bug");
76
+ didError = true;
77
+ firstErr ??= err;
78
+ reactBug = err;
79
+ if (reactBug !== firstErr || promiseResolved) {
80
+ console.error(reactBug);
81
+ }
82
+ }
83
+ });
84
+ await shellReady;
85
+ if (didError)
86
+ throw firstErr;
87
+ if (disable2)
88
+ await allReady;
89
+ if (didError)
90
+ throw firstErr;
91
+ promiseResolved = true;
92
+ return {
93
+ pipe: pipeForUser,
94
+ readable: null,
95
+ streamEnd: wrapStreamEnd(streamEnd, didError),
96
+ injectToStream
97
+ };
98
+ }
99
+ async function renderToWebStream(element, disable2, options) {
100
+ debug("creating Web Stream Pipe");
101
+ let didError = false;
102
+ let firstErr = null;
103
+ let reactBug = null;
104
+ const onError = (err) => {
105
+ didError = true;
106
+ firstErr = firstErr || err;
107
+ afterReactBugCatch(() => {
108
+ if (err !== reactBug) {
109
+ options.onBoundaryError?.(err);
110
+ }
111
+ });
112
+ };
113
+ const renderToReadableStream = options.renderToReadableStream ?? (await import("react-dom/server.browser")).renderToReadableStream;
114
+ const readableOriginal = await renderToReadableStream(element, { onError });
115
+ const { allReady } = readableOriginal;
116
+ let promiseResolved = false;
117
+ allReady.catch((err) => {
118
+ debug("react bug");
119
+ didError = true;
120
+ firstErr = firstErr || err;
121
+ reactBug = err;
122
+ if (reactBug !== firstErr || promiseResolved) {
123
+ console.error(reactBug);
124
+ }
125
+ });
126
+ if (didError)
127
+ throw firstErr;
128
+ if (disable2)
129
+ await allReady;
130
+ if (didError)
131
+ throw firstErr;
132
+ const { readableForUser, streamEnd, injectToStream } = createReadableWrapper(readableOriginal);
133
+ promiseResolved = true;
134
+ return {
135
+ readable: readableForUser,
136
+ pipe: null,
137
+ streamEnd: wrapStreamEnd(streamEnd, didError),
138
+ injectToStream
139
+ };
140
+ }
141
+ function afterReactBugCatch(fn) {
142
+ setTimeout(() => {
143
+ fn();
144
+ }, 0);
145
+ }
146
+ function wrapStreamEnd(streamEnd, didError) {
147
+ return streamEnd.then(() => new Promise((r) => setTimeout(r, 0))).then(() => !didError);
148
+ }
149
+ export {
150
+ disable,
151
+ renderToStream
152
+ };
@@ -0,0 +1,8 @@
1
+ export type { InitData };
2
+ export { initDataHtmlClass };
3
+ type InitData = {
4
+ value: unknown;
5
+ key: string;
6
+ elementId: string;
7
+ };
8
+ declare const initDataHtmlClass = "react-streaming_initData";
@@ -0,0 +1,4 @@
1
+ const initDataHtmlClass = "react-streaming_initData";
2
+ export {
3
+ initDataHtmlClass
4
+ };
@@ -0,0 +1,4 @@
1
+ export { stringifyKey };
2
+ export { assertKey };
3
+ declare function stringifyKey(key: unknown): string;
4
+ declare function assertKey(keyValue: unknown): void;
@@ -0,0 +1,20 @@
1
+ import { isCallable } from "../utils/isCallable";
2
+ import { assertUsage } from "./utils";
3
+ function stringifyKey(key) {
4
+ const keyString = JSON.stringify(key);
5
+ return keyString;
6
+ }
7
+ function assertKey(keyValue) {
8
+ assertUsage(
9
+ keyValue,
10
+ `[useAsync(key, asyncFn)] You provided a \`key\` with the value \`${keyValue}\` which is forbidden.`
11
+ );
12
+ assertUsage(
13
+ !isCallable(keyValue),
14
+ `[useAsync(key, asyncFn)] You provided a \`key\` that is a function which is forbidden.`
15
+ );
16
+ }
17
+ export {
18
+ assertKey,
19
+ stringifyKey
20
+ };
@@ -0,0 +1,3 @@
1
+ export * from '../utils/assert';
2
+ export * from '../utils/getGlobalVariable';
3
+ export * from '../utils/isPromise';
@@ -0,0 +1,3 @@
1
+ export * from "../utils/assert";
2
+ export * from "../utils/getGlobalVariable";
3
+ export * from "../utils/isPromise";