remote-components 0.0.13 → 0.0.15

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.
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/shared/ssr/dom-flight.ts"],"sourcesContent":["import { isCustomAttribute, possibleStandardNames } from 'react-property';\nimport styleToJs from 'style-to-js';\nimport type {\n ChildNode,\n Element,\n Node,\n TextNode,\n} from 'parse5/dist/tree-adapters/default';\nimport { serializeOuter } from 'parse5';\nimport type { RemoteComponentMetadata } from './types';\n\n// add fetch-priority to the possible standard names\npossibleStandardNames.fetchpriority = 'fetchPriority';\npossibleStandardNames['data-precedence'] = 'precedence';\n\nexport interface Context {\n name?: string;\n url?: URL;\n origin?: string;\n defer?: boolean;\n active?: boolean;\n onMetadata?: (metadata: RemoteComponentMetadata) => void;\n onScript?: (attrs: Record<string, string | boolean>) => void;\n onLink?: (attrs: Record<string, string | boolean>) => void;\n onRSC?: (rsc: string) => void;\n onNextData?: (data: {\n props: {\n pageProps: Record<string, unknown>;\n __REMOTE_COMPONENT__?: {\n bundle: string;\n runtime: string;\n };\n };\n page?: string;\n }) => void;\n onHTML?: (html: string) => void;\n onShared?: (shared: Record<string, string>) => void;\n}\n\ntype RSC =\n | ['$', string, null, Record<string, unknown>, null, null, number]\n | (\n | ['$', string, null, Record<string, unknown>, null, null, number]\n | string\n | null\n )[]\n | string\n | null;\n\nexport function visit(\n node: Node | ChildNode,\n context: Context = {\n active: false,\n },\n): RSC | RSC[] | string | null {\n if (node.nodeName === 'script' || node.nodeName === 'link') {\n if (\n node.attrs.find(\n (attr) => attr.name === 'id' && attr.value.endsWith('_shared'),\n )\n ) {\n context.onShared?.(\n JSON.parse((node.childNodes[0] as TextNode).value) as Record<\n string,\n string\n >,\n );\n return null;\n } else if (\n node.attrs.find(\n (attr) => attr.name === 'id' && attr.value.endsWith('_rsc'),\n ) &&\n 'childNodes' in node\n ) {\n context.onRSC?.((node.childNodes[0] as TextNode).value);\n } else if (\n node.attrs.find(\n (attr) => attr.name === 'id' && attr.value === '__NEXT_DATA__',\n ) &&\n 'childNodes' in node\n ) {\n context.onHTML?.(\n `<script id=\"__REMOTE_NEXT_DATA__\" type=\"application/json\">${(node.childNodes[0] as TextNode).value}</script>`,\n );\n context.onNextData?.(\n JSON.parse((node.childNodes[0] as TextNode).value) as {\n props: { pageProps: Record<string, unknown> };\n },\n );\n } else if (node.childNodes.length === 0) {\n if (\n node.nodeName === 'script' &&\n !node.attrs.find((attr) => attr.name === 'src')\n ) {\n return [\n '$',\n node.nodeName,\n null,\n node.attrs.reduce<Record<string, string>>((props, attr) => {\n props[possibleStandardNames[attr.name] ?? attr.name] = attr.value;\n return props;\n }, {}),\n null,\n null,\n 1,\n ] as RSC;\n }\n if (node.nodeName === 'script') {\n context.onScript?.(\n node.attrs.reduce<Record<string, string | boolean>>((props, attr) => {\n props[attr.name] = attr.value || true;\n return props;\n }, {}),\n );\n const src = node.attrs.find((attr) => attr.name === 'src')?.value;\n if (src) {\n node.attrs = node.attrs.filter((attr) => attr.name !== 'src');\n node.attrs.push({\n name: 'data-src',\n value: src,\n });\n }\n context.onHTML?.(serializeOuter(node));\n } else {\n context.onLink?.(\n node.attrs.reduce<Record<string, string | boolean>>((props, attr) => {\n props[possibleStandardNames[attr.name] ?? attr.name] =\n attr.value || true;\n return props;\n }, {}),\n );\n context.onHTML?.(serializeOuter(node));\n }\n return null;\n }\n }\n\n if (!context.active) {\n if ('childNodes' in node) {\n (node as Element).childNodes.forEach((childNode) => {\n visit(childNode, context);\n });\n } else return null;\n }\n\n switch (node.nodeName) {\n case '#document-fragment':\n return node.childNodes\n .map((childNode) => visit(childNode, context))\n .filter((it) => it !== null) as unknown as RSC;\n case '#text':\n return (node as TextNode).value;\n case '#comment':\n case 'head':\n case 'meta':\n case 'title':\n case 'noscript':\n return null;\n default: {\n if (\n node.nodeName === 'div' &&\n (node.attrs.find((it) => it.name === 'id' && it.value === '__next') ||\n ((context.name\n ? node.attrs.find(\n (it) =>\n it.name === 'id' &&\n it.value.startsWith(\n context.name ?? '__vercel_remote_component',\n ),\n )\n : node.attrs.find((it) => it.name === 'data-bundle' && it.value)) &&\n node.attrs.find((it) => it.name === 'data-route') &&\n node.attrs.find(\n (it) => it.name === 'id' && it.value.endsWith('_ssr'),\n )))\n ) {\n context.onMetadata?.({\n bundle:\n node.attrs.find((it) => it.name === 'data-bundle' && it.value)\n ?.value ?? '',\n route:\n node.attrs.find((it) => it.name === 'data-route')?.value ?? '/',\n runtime: (node.attrs.find((it) => it.name === 'data-runtime')\n ?.value ?? 'webpack') as RemoteComponentMetadata['runtime'],\n id:\n node.attrs.find(\n (it) => it.name === 'id' && it.value.endsWith('_ssr'),\n )?.value ?? '__vercel_remote_component',\n });\n context.onHTML?.(serializeOuter(node));\n return node.childNodes\n .map((childNode) => visit(childNode, { ...context, active: true }))\n .filter((it) => it !== null) as unknown as RSC;\n }\n const childNodes = (node as Element).childNodes\n .map((childNode) => visit(childNode, context))\n .filter((childNode) => childNode !== null);\n const children = childNodes.length > 1 ? childNodes : childNodes[0];\n const nodeProps = (node as Element).attrs.reduce<\n Record<string, string | ReturnType<typeof styleToJs>> & {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n children?: any;\n }\n >((props, attr) => {\n if (attr.name === 'style') {\n props.style = styleToJs(attr.value, {\n reactCompat: true,\n });\n return props;\n }\n if (node.nodeName === 'input' && attr.name === 'value') {\n props.defaultValue = attr.value;\n return props;\n }\n if (isCustomAttribute(attr.name)) {\n props[attr.name] = attr.value;\n return props;\n }\n props[possibleStandardNames[attr.name] ?? attr.name] = attr.value;\n return props;\n }, {});\n if (typeof children !== 'undefined') {\n if (\n node.nodeName === 'script' &&\n typeof children === 'string' &&\n children.startsWith('$')\n ) {\n nodeProps.children = `$${children}`;\n } else {\n nodeProps.children = children;\n }\n }\n if (!context.active) {\n if (childNodes.length > 0) {\n return childNodes as RSC;\n }\n return null;\n }\n return ['$', node.nodeName, null, nodeProps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,cAAc,cAAc,IAAI;AAC3C,YAAM,WAAW,WAAW,SAAS,IAAI,aAAa,WAAW,CAAC;AAClE,YAAM,YAAa,KAAiB,MAAM,OAKxC,CAAC,OAAO,SAAS;AACjB,YAAI,KAAK,SAAS,SAAS;AACzB,gBAAM,YAAQ,mBAAAA,SAAU,KAAK,OAAO;AAAA,YAClC,aAAa;AAAA,UACf,CAAC;AACD,iBAAO;AAAA,QACT;AACA,YAAI,KAAK,aAAa,WAAW,KAAK,SAAS,SAAS;AACtD,gBAAM,eAAe,KAAK;AAC1B,iBAAO;AAAA,QACT;AACA,gBAAI,yCAAkB,KAAK,IAAI,GAAG;AAChC,gBAAM,KAAK,IAAI,IAAI,KAAK;AACxB,iBAAO;AAAA,QACT;AACA,cAAM,4CAAsB,KAAK,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK;AAC5D,eAAO;AAAA,MACT,GAAG,CAAC,CAAC;AACL,UAAI,OAAO,aAAa,aAAa;AACnC,YACE,KAAK,aAAa,YAClB,OAAO,aAAa,YACpB,SAAS,WAAW,GAAG,GACvB;AACA,oBAAU,WAAW,IAAI;AAAA,QAC3B,OAAO;AACL,oBAAU,WAAW;AAAA,QACvB;AAAA,MACF;AACA,UAAI,CAAC,QAAQ,QAAQ;AACnB,YAAI,WAAW,SAAS,GAAG;AACzB,iBAAO;AAAA,QACT;AACA,eAAO;AAAA,MACT;AACA,aAAO,CAAC,KAAK,KAAK,UAAU,MAAM,WAAW,MAAM,MAAM,CAAC;AAAA,IAC5D;AAAA,EACF;AACF;","names":["styleToJs"]}
1
+ {"version":3,"sources":["../../../src/shared/ssr/dom-flight.ts"],"sourcesContent":["import { isCustomAttribute, possibleStandardNames } from 'react-property';\nimport styleToJs from 'style-to-js';\nimport type {\n ChildNode,\n Element,\n Node,\n TextNode,\n} from 'parse5/dist/tree-adapters/default';\nimport { serializeOuter } from 'parse5';\nimport type { RemoteComponentMetadata } from './types';\n\n// add fetch-priority to the possible standard names\npossibleStandardNames.fetchpriority = 'fetchPriority';\npossibleStandardNames['data-precedence'] = 'precedence';\n\nexport interface Context {\n name?: string;\n url?: URL;\n origin?: string;\n defer?: boolean;\n active?: boolean;\n visitedRSCNodes?: Set<Node>;\n onMetadata?: (metadata: RemoteComponentMetadata) => void;\n onScript?: (attrs: Record<string, string | boolean>) => void;\n onLink?: (attrs: Record<string, string | boolean>) => void;\n onRSC?: (rsc: string) => void;\n onNextData?: (data: {\n props: {\n pageProps: Record<string, unknown>;\n __REMOTE_COMPONENT__?: {\n bundle: string;\n runtime: string;\n };\n };\n page?: string;\n }) => void;\n onHTML?: (html: string) => void;\n onShared?: (shared: Record<string, string>) => void;\n}\n\ntype RSC =\n | ['$', string, null, Record<string, unknown>, null, null, number]\n | (\n | ['$', string, null, Record<string, unknown>, null, null, number]\n | string\n | null\n )[]\n | string\n | null;\n\nexport function visit(\n node: Node | ChildNode,\n context: Context = {\n active: false,\n },\n): RSC | RSC[] | string | null {\n if (node.nodeName === 'script' || node.nodeName === 'link') {\n if (\n node.attrs.find(\n (attr) => attr.name === 'id' && attr.value.endsWith('_shared'),\n )\n ) {\n context.onShared?.(\n JSON.parse((node.childNodes[0] as TextNode).value) as Record<\n string,\n string\n >,\n );\n return null;\n } else if (\n node.attrs.find(\n (attr) => attr.name === 'id' && attr.value.endsWith('_rsc'),\n ) &&\n 'childNodes' in node\n ) {\n if (!context.visitedRSCNodes) {\n context.visitedRSCNodes = new Set();\n }\n if (!context.visitedRSCNodes.has(node)) {\n context.visitedRSCNodes.add(node);\n context.onRSC?.((node.childNodes[0] as TextNode).value);\n }\n } else if (\n node.attrs.find(\n (attr) => attr.name === 'id' && attr.value === '__NEXT_DATA__',\n ) &&\n 'childNodes' in node\n ) {\n context.onHTML?.(\n `<script id=\"__REMOTE_NEXT_DATA__\" type=\"application/json\">${(node.childNodes[0] as TextNode).value}</script>`,\n );\n context.onNextData?.(\n JSON.parse((node.childNodes[0] as TextNode).value) as {\n props: { pageProps: Record<string, unknown> };\n },\n );\n } else if (node.childNodes.length === 0) {\n if (\n node.nodeName === 'script' &&\n !node.attrs.find((attr) => attr.name === 'src')\n ) {\n return [\n '$',\n node.nodeName,\n null,\n node.attrs.reduce<Record<string, string>>((props, attr) => {\n props[possibleStandardNames[attr.name] ?? attr.name] = attr.value;\n return props;\n }, {}),\n null,\n null,\n 1,\n ] as RSC;\n }\n if (node.nodeName === 'script') {\n context.onScript?.(\n node.attrs.reduce<Record<string, string | boolean>>((props, attr) => {\n props[attr.name] = attr.value || true;\n return props;\n }, {}),\n );\n const src = node.attrs.find((attr) => attr.name === 'src')?.value;\n if (src) {\n node.attrs = node.attrs.filter((attr) => attr.name !== 'src');\n node.attrs.push({\n name: 'data-src',\n value: src,\n });\n }\n context.onHTML?.(serializeOuter(node));\n } else {\n context.onLink?.(\n node.attrs.reduce<Record<string, string | boolean>>((props, attr) => {\n props[possibleStandardNames[attr.name] ?? attr.name] =\n attr.value || true;\n return props;\n }, {}),\n );\n context.onHTML?.(serializeOuter(node));\n }\n return null;\n }\n }\n\n if (!context.active) {\n if ('childNodes' in node) {\n (node as Element).childNodes.forEach((childNode) => {\n visit(childNode, context);\n });\n } else return null;\n }\n\n switch (node.nodeName) {\n case '#document-fragment':\n return node.childNodes\n .map((childNode) => visit(childNode, context))\n .filter((it) => it !== null) as unknown as RSC;\n case '#text':\n return (node as TextNode).value;\n case '#comment':\n case 'head':\n case 'meta':\n case 'title':\n case 'noscript':\n return null;\n default: {\n if (\n node.nodeName === 'div' &&\n (node.attrs.find((it) => it.name === 'id' && it.value === '__next') ||\n ((context.name\n ? node.attrs.find(\n (it) =>\n it.name === 'id' &&\n it.value.startsWith(\n context.name ?? '__vercel_remote_component',\n ),\n )\n : node.attrs.find((it) => it.name === 'data-bundle' && it.value)) &&\n node.attrs.find((it) => it.name === 'data-route') &&\n node.attrs.find(\n (it) => it.name === 'id' && it.value.endsWith('_ssr'),\n )))\n ) {\n context.onMetadata?.({\n bundle:\n node.attrs.find((it) => it.name === 'data-bundle' && it.value)\n ?.value ?? '',\n route:\n node.attrs.find((it) => it.name === 'data-route')?.value ?? '/',\n runtime: (node.attrs.find((it) => it.name === 'data-runtime')\n ?.value ?? 'webpack') as RemoteComponentMetadata['runtime'],\n id:\n node.attrs.find(\n (it) => it.name === 'id' && it.value.endsWith('_ssr'),\n )?.value ?? '__vercel_remote_component',\n });\n context.onHTML?.(serializeOuter(node));\n return node.childNodes\n .map((childNode) => visit(childNode, { ...context, active: true }))\n .filter((it) => it !== null) as unknown as RSC;\n }\n const childNodes = (node as Element).childNodes\n .map((childNode) => visit(childNode, context))\n .filter((childNode) => childNode !== null);\n const children = childNodes.length > 1 ? childNodes : childNodes[0];\n const nodeProps = (node as Element).attrs.reduce<\n Record<string, string | ReturnType<typeof styleToJs>> & {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n children?: any;\n }\n >((props, attr) => {\n if (attr.name === 'style') {\n props.style = styleToJs(attr.value, {\n reactCompat: true,\n });\n return props;\n }\n if (node.nodeName === 'input' && attr.name === 'value') {\n props.defaultValue = attr.value;\n return props;\n }\n if (isCustomAttribute(attr.name)) {\n props[attr.name] = attr.value;\n return props;\n }\n props[possibleStandardNames[attr.name] ?? attr.name] = attr.value;\n return props;\n }, {});\n if (typeof children !== 'undefined') {\n if (\n node.nodeName === 'script' &&\n typeof children === 'string' &&\n children.startsWith('$')\n ) {\n nodeProps.children = `$${children}`;\n } else {\n nodeProps.children = children;\n }\n }\n if (!context.active) {\n if (childNodes.length > 0) {\n return childNodes as RSC;\n }\n return null;\n }\n return ['$', node.nodeName, null, nodeProps, null, null, 1] as RSC;\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAAyD;AACzD,yBAAsB;AAOtB,oBAA+B;AAI/B,4CAAsB,gBAAgB;AACtC,4CAAsB,iBAAiB,IAAI;AAqCpC,SAAS,MACd,MACA,UAAmB;AAAA,EACjB,QAAQ;AACV,GAC6B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,YAAM,YAAa,KAAiB,MAAM,OAKxC,CAAC,OAAO,SAAS;AACjB,YAAI,KAAK,SAAS,SAAS;AACzB,gBAAM,YAAQ,mBAAAA,SAAU,KAAK,OAAO;AAAA,YAClC,aAAa;AAAA,UACf,CAAC;AACD,iBAAO;AAAA,QACT;AACA,YAAI,KAAK,aAAa,WAAW,KAAK,SAAS,SAAS;AACtD,gBAAM,eAAe,KAAK;AAC1B,iBAAO;AAAA,QACT;AACA,gBAAI,yCAAkB,KAAK,IAAI,GAAG;AAChC,gBAAM,KAAK,IAAI,IAAI,KAAK;AACxB,iBAAO;AAAA,QACT;AACA,cAAM,4CAAsB,KAAK,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK;AAC5D,eAAO;AAAA,MACT,GAAG,CAAC,CAAC;AACL,UAAI,OAAO,aAAa,aAAa;AACnC,YACE,KAAK,aAAa,YAClB,OAAO,aAAa,YACpB,SAAS,WAAW,GAAG,GACvB;AACA,oBAAU,WAAW,IAAI;AAAA,QAC3B,OAAO;AACL,oBAAU,WAAW;AAAA,QACvB;AAAA,MACF;AACA,UAAI,CAAC,QAAQ,QAAQ;AACnB,YAAI,WAAW,SAAS,GAAG;AACzB,iBAAO;AAAA,QACT;AACA,eAAO;AAAA,MACT;AACA,aAAO,CAAC,KAAK,KAAK,UAAU,MAAM,WAAW,MAAM,MAAM,CAAC;AAAA,IAC5D;AAAA,EACF;AACF;","names":["styleToJs"]}
@@ -7,6 +7,7 @@ interface Context {
7
7
  origin?: string;
8
8
  defer?: boolean;
9
9
  active?: boolean;
10
+ visitedRSCNodes?: Set<Node>;
10
11
  onMetadata?: (metadata: RemoteComponentMetadata) => void;
11
12
  onScript?: (attrs: Record<string, string | boolean>) => void;
12
13
  onLink?: (attrs: Record<string, string | boolean>) => void;
@@ -17,7 +17,13 @@ function visit(node, context = {
17
17
  } else if (node.attrs.find(
18
18
  (attr) => attr.name === "id" && attr.value.endsWith("_rsc")
19
19
  ) && "childNodes" in node) {
20
- context.onRSC?.(node.childNodes[0].value);
20
+ if (!context.visitedRSCNodes) {
21
+ context.visitedRSCNodes = /* @__PURE__ */ new Set();
22
+ }
23
+ if (!context.visitedRSCNodes.has(node)) {
24
+ context.visitedRSCNodes.add(node);
25
+ context.onRSC?.(node.childNodes[0].value);
26
+ }
21
27
  } else if (node.attrs.find(
22
28
  (attr) => attr.name === "id" && attr.value === "__NEXT_DATA__"
23
29
  ) && "childNodes" in node) {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/shared/ssr/dom-flight.ts"],"sourcesContent":["import { isCustomAttribute, possibleStandardNames } from 'react-property';\nimport styleToJs from 'style-to-js';\nimport type {\n ChildNode,\n Element,\n Node,\n TextNode,\n} from 'parse5/dist/tree-adapters/default';\nimport { serializeOuter } from 'parse5';\nimport type { RemoteComponentMetadata } from './types';\n\n// add fetch-priority to the possible standard names\npossibleStandardNames.fetchpriority = 'fetchPriority';\npossibleStandardNames['data-precedence'] = 'precedence';\n\nexport interface Context {\n name?: string;\n url?: URL;\n origin?: string;\n defer?: boolean;\n active?: boolean;\n onMetadata?: (metadata: RemoteComponentMetadata) => void;\n onScript?: (attrs: Record<string, string | boolean>) => void;\n onLink?: (attrs: Record<string, string | boolean>) => void;\n onRSC?: (rsc: string) => void;\n onNextData?: (data: {\n props: {\n pageProps: Record<string, unknown>;\n __REMOTE_COMPONENT__?: {\n bundle: string;\n runtime: string;\n };\n };\n page?: string;\n }) => void;\n onHTML?: (html: string) => void;\n onShared?: (shared: Record<string, string>) => void;\n}\n\ntype RSC =\n | ['$', string, null, Record<string, unknown>, null, null, number]\n | (\n | ['$', string, null, Record<string, unknown>, null, null, number]\n | string\n | null\n )[]\n | string\n | null;\n\nexport function visit(\n node: Node | ChildNode,\n context: Context = {\n active: false,\n },\n): RSC | RSC[] | string | null {\n if (node.nodeName === 'script' || node.nodeName === 'link') {\n if (\n node.attrs.find(\n (attr) => attr.name === 'id' && attr.value.endsWith('_shared'),\n )\n ) {\n context.onShared?.(\n JSON.parse((node.childNodes[0] as TextNode).value) as Record<\n string,\n string\n >,\n );\n return null;\n } else if (\n node.attrs.find(\n (attr) => attr.name === 'id' && attr.value.endsWith('_rsc'),\n ) &&\n 'childNodes' in node\n ) {\n context.onRSC?.((node.childNodes[0] as TextNode).value);\n } else if (\n node.attrs.find(\n (attr) => attr.name === 'id' && attr.value === '__NEXT_DATA__',\n ) &&\n 'childNodes' in node\n ) {\n context.onHTML?.(\n `<script id=\"__REMOTE_NEXT_DATA__\" type=\"application/json\">${(node.childNodes[0] as TextNode).value}</script>`,\n );\n context.onNextData?.(\n JSON.parse((node.childNodes[0] as TextNode).value) as {\n props: { pageProps: Record<string, unknown> };\n },\n );\n } else if (node.childNodes.length === 0) {\n if (\n node.nodeName === 'script' &&\n !node.attrs.find((attr) => attr.name === 'src')\n ) {\n return [\n '$',\n node.nodeName,\n null,\n node.attrs.reduce<Record<string, string>>((props, attr) => {\n props[possibleStandardNames[attr.name] ?? attr.name] = attr.value;\n return props;\n }, {}),\n null,\n null,\n 1,\n ] as RSC;\n }\n if (node.nodeName === 'script') {\n context.onScript?.(\n node.attrs.reduce<Record<string, string | boolean>>((props, attr) => {\n props[attr.name] = attr.value || true;\n return props;\n }, {}),\n );\n const src = node.attrs.find((attr) => attr.name === 'src')?.value;\n if (src) {\n node.attrs = node.attrs.filter((attr) => attr.name !== 'src');\n node.attrs.push({\n name: 'data-src',\n value: src,\n });\n }\n context.onHTML?.(serializeOuter(node));\n } else {\n context.onLink?.(\n node.attrs.reduce<Record<string, string | boolean>>((props, attr) => {\n props[possibleStandardNames[attr.name] ?? attr.name] =\n attr.value || true;\n return props;\n }, {}),\n );\n context.onHTML?.(serializeOuter(node));\n }\n return null;\n }\n }\n\n if (!context.active) {\n if ('childNodes' in node) {\n (node as Element).childNodes.forEach((childNode) => {\n visit(childNode, context);\n });\n } else return null;\n }\n\n switch (node.nodeName) {\n case '#document-fragment':\n return node.childNodes\n .map((childNode) => visit(childNode, context))\n .filter((it) => it !== null) as unknown as RSC;\n case '#text':\n return (node as TextNode).value;\n case '#comment':\n case 'head':\n case 'meta':\n case 'title':\n case 'noscript':\n return null;\n default: {\n if (\n node.nodeName === 'div' &&\n (node.attrs.find((it) => it.name === 'id' && it.value === '__next') ||\n ((context.name\n ? node.attrs.find(\n (it) =>\n it.name === 'id' &&\n it.value.startsWith(\n context.name ?? '__vercel_remote_component',\n ),\n )\n : node.attrs.find((it) => it.name === 'data-bundle' && it.value)) &&\n node.attrs.find((it) => it.name === 'data-route') &&\n node.attrs.find(\n (it) => it.name === 'id' && it.value.endsWith('_ssr'),\n )))\n ) {\n context.onMetadata?.({\n bundle:\n node.attrs.find((it) => it.name === 'data-bundle' && it.value)\n ?.value ?? '',\n route:\n node.attrs.find((it) => it.name === 'data-route')?.value ?? '/',\n runtime: (node.attrs.find((it) => it.name === 'data-runtime')\n ?.value ?? 'webpack') as RemoteComponentMetadata['runtime'],\n id:\n node.attrs.find(\n (it) => it.name === 'id' && it.value.endsWith('_ssr'),\n )?.value ?? '__vercel_remote_component',\n });\n context.onHTML?.(serializeOuter(node));\n return node.childNodes\n .map((childNode) => visit(childNode, { ...context, active: true }))\n .filter((it) => it !== null) as unknown as RSC;\n }\n const childNodes = (node as Element).childNodes\n .map((childNode) => visit(childNode, context))\n .filter((childNode) => childNode !== null);\n const children = childNodes.length > 1 ? childNodes : childNodes[0];\n const nodeProps = (node as Element).attrs.reduce<\n Record<string, string | ReturnType<typeof styleToJs>> & {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n children?: any;\n }\n >((props, attr) => {\n if (attr.name === 'style') {\n props.style = styleToJs(attr.value, {\n reactCompat: true,\n });\n return props;\n }\n if (node.nodeName === 'input' && attr.name === 'value') {\n props.defaultValue = attr.value;\n return props;\n }\n if (isCustomAttribute(attr.name)) {\n props[attr.name] = attr.value;\n return props;\n }\n props[possibleStandardNames[attr.name] ?? attr.name] = attr.value;\n return props;\n }, {});\n if (typeof children !== 'undefined') {\n if (\n node.nodeName === 'script' &&\n typeof children === 'string' &&\n children.startsWith('$')\n ) {\n nodeProps.children = `$${children}`;\n } else {\n nodeProps.children = children;\n }\n }\n if (!context.active) {\n if (childNodes.length > 0) {\n return childNodes as RSC;\n }\n return null;\n }\n return ['$', node.nodeName, null, nodeProps, null, null, 1] as RSC;\n }\n }\n}\n"],"mappings":"AAAA,SAAS,mBAAmB,6BAA6B;AACzD,OAAO,eAAe;AAOtB,SAAS,sBAAsB;AAI/B,sBAAsB,gBAAgB;AACtC,sBAAsB,iBAAiB,IAAI;AAoCpC,SAAS,MACd,MACA,UAAmB;AAAA,EACjB,QAAQ;AACV,GAC6B;AAC7B,MAAI,KAAK,aAAa,YAAY,KAAK,aAAa,QAAQ;AAC1D,QACE,KAAK,MAAM;AAAA,MACT,CAAC,SAAS,KAAK,SAAS,QAAQ,KAAK,MAAM,SAAS,SAAS;AAAA,IAC/D,GACA;AACA,cAAQ;AAAA,QACN,KAAK,MAAO,KAAK,WAAW,CAAC,EAAe,KAAK;AAAA,MAInD;AACA,aAAO;AAAA,IACT,WACE,KAAK,MAAM;AAAA,MACT,CAAC,SAAS,KAAK,SAAS,QAAQ,KAAK,MAAM,SAAS,MAAM;AAAA,IAC5D,KACA,gBAAgB,MAChB;AACA,cAAQ,QAAS,KAAK,WAAW,CAAC,EAAe,KAAK;AAAA,IACxD,WACE,KAAK,MAAM;AAAA,MACT,CAAC,SAAS,KAAK,SAAS,QAAQ,KAAK,UAAU;AAAA,IACjD,KACA,gBAAgB,MAChB;AACA,cAAQ;AAAA,QACN,6DAA8D,KAAK,WAAW,CAAC,EAAe;AAAA,MAChG;AACA,cAAQ;AAAA,QACN,KAAK,MAAO,KAAK,WAAW,CAAC,EAAe,KAAK;AAAA,MAGnD;AAAA,IACF,WAAW,KAAK,WAAW,WAAW,GAAG;AACvC,UACE,KAAK,aAAa,YAClB,CAAC,KAAK,MAAM,KAAK,CAAC,SAAS,KAAK,SAAS,KAAK,GAC9C;AACA,eAAO;AAAA,UACL;AAAA,UACA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C,OAAO,OAAO,IAAI;AAAA,MAC/B;AACA,YAAM,aAAc,KAAiB,WAClC,IAAI,CAAC,cAAc,MAAM,WAAW,OAAO,CAAC,EAC5C,OAAO,CAAC,cAAc,cAAc,IAAI;AAC3C,YAAM,WAAW,WAAW,SAAS,IAAI,aAAa,WAAW,CAAC;AAClE,YAAM,YAAa,KAAiB,MAAM,OAKxC,CAAC,OAAO,SAAS;AACjB,YAAI,KAAK,SAAS,SAAS;AACzB,gBAAM,QAAQ,UAAU,KAAK,OAAO;AAAA,YAClC,aAAa;AAAA,UACf,CAAC;AACD,iBAAO;AAAA,QACT;AACA,YAAI,KAAK,aAAa,WAAW,KAAK,SAAS,SAAS;AACtD,gBAAM,eAAe,KAAK;AAC1B,iBAAO;AAAA,QACT;AACA,YAAI,kBAAkB,KAAK,IAAI,GAAG;AAChC,gBAAM,KAAK,IAAI,IAAI,KAAK;AACxB,iBAAO;AAAA,QACT;AACA,cAAM,sBAAsB,KAAK,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK;AAC5D,eAAO;AAAA,MACT,GAAG,CAAC,CAAC;AACL,UAAI,OAAO,aAAa,aAAa;AACnC,YACE,KAAK,aAAa,YAClB,OAAO,aAAa,YACpB,SAAS,WAAW,GAAG,GACvB;AACA,oBAAU,WAAW,IAAI;AAAA,QAC3B,OAAO;AACL,oBAAU,WAAW;AAAA,QACvB;AAAA,MACF;AACA,UAAI,CAAC,QAAQ,QAAQ;AACnB,YAAI,WAAW,SAAS,GAAG;AACzB,iBAAO;AAAA,QACT;AACA,eAAO;AAAA,MACT;AACA,aAAO,CAAC,KAAK,KAAK,UAAU,MAAM,WAAW,MAAM,MAAM,CAAC;AAAA,IAC5D;AAAA,EACF;AACF;","names":[]}
1
+ {"version":3,"sources":["../../../src/shared/ssr/dom-flight.ts"],"sourcesContent":["import { isCustomAttribute, possibleStandardNames } from 'react-property';\nimport styleToJs from 'style-to-js';\nimport type {\n ChildNode,\n Element,\n Node,\n TextNode,\n} from 'parse5/dist/tree-adapters/default';\nimport { serializeOuter } from 'parse5';\nimport type { RemoteComponentMetadata } from './types';\n\n// add fetch-priority to the possible standard names\npossibleStandardNames.fetchpriority = 'fetchPriority';\npossibleStandardNames['data-precedence'] = 'precedence';\n\nexport interface Context {\n name?: string;\n url?: URL;\n origin?: string;\n defer?: boolean;\n active?: boolean;\n visitedRSCNodes?: Set<Node>;\n onMetadata?: (metadata: RemoteComponentMetadata) => void;\n onScript?: (attrs: Record<string, string | boolean>) => void;\n onLink?: (attrs: Record<string, string | boolean>) => void;\n onRSC?: (rsc: string) => void;\n onNextData?: (data: {\n props: {\n pageProps: Record<string, unknown>;\n __REMOTE_COMPONENT__?: {\n bundle: string;\n runtime: string;\n };\n };\n page?: string;\n }) => void;\n onHTML?: (html: string) => void;\n onShared?: (shared: Record<string, string>) => void;\n}\n\ntype RSC =\n | ['$', string, null, Record<string, unknown>, null, null, number]\n | (\n | ['$', string, null, Record<string, unknown>, null, null, number]\n | string\n | null\n )[]\n | string\n | null;\n\nexport function visit(\n node: Node | ChildNode,\n context: Context = {\n active: false,\n },\n): RSC | RSC[] | string | null {\n if (node.nodeName === 'script' || node.nodeName === 'link') {\n if (\n node.attrs.find(\n (attr) => attr.name === 'id' && attr.value.endsWith('_shared'),\n )\n ) {\n context.onShared?.(\n JSON.parse((node.childNodes[0] as TextNode).value) as Record<\n string,\n string\n >,\n );\n return null;\n } else if (\n node.attrs.find(\n (attr) => attr.name === 'id' && attr.value.endsWith('_rsc'),\n ) &&\n 'childNodes' in node\n ) {\n if (!context.visitedRSCNodes) {\n context.visitedRSCNodes = new Set();\n }\n if (!context.visitedRSCNodes.has(node)) {\n context.visitedRSCNodes.add(node);\n context.onRSC?.((node.childNodes[0] as TextNode).value);\n }\n } else if (\n node.attrs.find(\n (attr) => attr.name === 'id' && attr.value === '__NEXT_DATA__',\n ) &&\n 'childNodes' in node\n ) {\n context.onHTML?.(\n `<script id=\"__REMOTE_NEXT_DATA__\" type=\"application/json\">${(node.childNodes[0] as TextNode).value}</script>`,\n );\n context.onNextData?.(\n JSON.parse((node.childNodes[0] as TextNode).value) as {\n props: { pageProps: Record<string, unknown> };\n },\n );\n } else if (node.childNodes.length === 0) {\n if (\n node.nodeName === 'script' &&\n !node.attrs.find((attr) => attr.name === 'src')\n ) {\n return [\n '$',\n node.nodeName,\n null,\n node.attrs.reduce<Record<string, string>>((props, attr) => {\n props[possibleStandardNames[attr.name] ?? attr.name] = attr.value;\n return props;\n }, {}),\n null,\n null,\n 1,\n ] as RSC;\n }\n if (node.nodeName === 'script') {\n context.onScript?.(\n node.attrs.reduce<Record<string, string | boolean>>((props, attr) => {\n props[attr.name] = attr.value || true;\n return props;\n }, {}),\n );\n const src = node.attrs.find((attr) => attr.name === 'src')?.value;\n if (src) {\n node.attrs = node.attrs.filter((attr) => attr.name !== 'src');\n node.attrs.push({\n name: 'data-src',\n value: src,\n });\n }\n context.onHTML?.(serializeOuter(node));\n } else {\n context.onLink?.(\n node.attrs.reduce<Record<string, string | boolean>>((props, attr) => {\n props[possibleStandardNames[attr.name] ?? attr.name] =\n attr.value || true;\n return props;\n }, {}),\n );\n context.onHTML?.(serializeOuter(node));\n }\n return null;\n }\n }\n\n if (!context.active) {\n if ('childNodes' in node) {\n (node as Element).childNodes.forEach((childNode) => {\n visit(childNode, context);\n });\n } else return null;\n }\n\n switch (node.nodeName) {\n case '#document-fragment':\n return node.childNodes\n .map((childNode) => visit(childNode, context))\n .filter((it) => it !== null) as unknown as RSC;\n case '#text':\n return (node as TextNode).value;\n case '#comment':\n case 'head':\n case 'meta':\n case 'title':\n case 'noscript':\n return null;\n default: {\n if (\n node.nodeName === 'div' &&\n (node.attrs.find((it) => it.name === 'id' && it.value === '__next') ||\n ((context.name\n ? node.attrs.find(\n (it) =>\n it.name === 'id' &&\n it.value.startsWith(\n context.name ?? '__vercel_remote_component',\n ),\n )\n : node.attrs.find((it) => it.name === 'data-bundle' && it.value)) &&\n node.attrs.find((it) => it.name === 'data-route') &&\n node.attrs.find(\n (it) => it.name === 'id' && it.value.endsWith('_ssr'),\n )))\n ) {\n context.onMetadata?.({\n bundle:\n node.attrs.find((it) => it.name === 'data-bundle' && it.value)\n ?.value ?? '',\n route:\n node.attrs.find((it) => it.name === 'data-route')?.value ?? '/',\n runtime: (node.attrs.find((it) => it.name === 'data-runtime')\n ?.value ?? 'webpack') as RemoteComponentMetadata['runtime'],\n id:\n node.attrs.find(\n (it) => it.name === 'id' && it.value.endsWith('_ssr'),\n )?.value ?? '__vercel_remote_component',\n });\n context.onHTML?.(serializeOuter(node));\n return node.childNodes\n .map((childNode) => visit(childNode, { ...context, active: true }))\n .filter((it) => it !== null) as unknown as RSC;\n }\n const childNodes = (node as Element).childNodes\n .map((childNode) => visit(childNode, context))\n .filter((childNode) => childNode !== null);\n const children = childNodes.length > 1 ? childNodes : childNodes[0];\n const nodeProps = (node as Element).attrs.reduce<\n Record<string, string | ReturnType<typeof styleToJs>> & {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n children?: any;\n }\n >((props, attr) => {\n if (attr.name === 'style') {\n props.style = styleToJs(attr.value, {\n reactCompat: true,\n });\n return props;\n }\n if (node.nodeName === 'input' && attr.name === 'value') {\n props.defaultValue = attr.value;\n return props;\n }\n if (isCustomAttribute(attr.name)) {\n props[attr.name] = attr.value;\n return props;\n }\n props[possibleStandardNames[attr.name] ?? attr.name] = attr.value;\n return props;\n }, {});\n if (typeof children !== 'undefined') {\n if (\n node.nodeName === 'script' &&\n typeof children === 'string' &&\n children.startsWith('$')\n ) {\n nodeProps.children = `$${children}`;\n } else {\n nodeProps.children = children;\n }\n }\n if (!context.active) {\n if (childNodes.length > 0) {\n return childNodes as RSC;\n }\n return null;\n }\n return ['$', node.nodeName, null, nodeProps, null, null, 1] as RSC;\n }\n }\n}\n"],"mappings":"AAAA,SAAS,mBAAmB,6BAA6B;AACzD,OAAO,eAAe;AAOtB,SAAS,sBAAsB;AAI/B,sBAAsB,gBAAgB;AACtC,sBAAsB,iBAAiB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AAI;AAC3C,YAAM,WAAW,WAAW,SAAS,IAAI,aAAa,WAAW,CAAC;AAClE,YAAM,YAAa,KAAiB,MAAM,OAKxC,CAAC,OAAO,SAAS;AACjB,YAAI,KAAK,SAAS,SAAS;AACzB,gBAAM,QAAQ,UAAU,KAAK,OAAO;AAAA,YAClC,aAAa;AAAA,UACf,CAAC;AACD,iBAAO;AAAA,QACT;AACA,YAAI,KAAK,aAAa,WAAW,KAAK,SAAS,SAAS;AACtD,gBAAM,eAAe,KAAK;AAC1B,iBAAO;AAAA,QACT;AACA,YAAI,kBAAkB,KAAK,IAAI,GAAG;AAChC,gBAAM,KAAK,IAAI,IAAI,KAAK;AACxB,iBAAO;AAAA,QACT;AACA,cAAM,sBAAsB,KAAK,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK;AAC5D,eAAO;AAAA,MACT,GAAG,CAAC,CAAC;AACL,UAAI,OAAO,aAAa,aAAa;AACnC,YACE,KAAK,aAAa,YAClB,OAAO,aAAa,YACpB,SAAS,WAAW,GAAG,GACvB;AACA,oBAAU,WAAW,IAAI;AAAA,QAC3B,OAAO;AACL,oBAAU,WAAW;AAAA,QACvB;AAAA,MACF;AACA,UAAI,CAAC,QAAQ,QAAQ;AACnB,YAAI,WAAW,SAAS,GAAG;AACzB,iBAAO;AAAA,QACT;AACA,eAAO;AAAA,MACT;AACA,aAAO,CAAC,KAAK,KAAK,UAAU,MAAM,WAAW,MAAM,MAAM,CAAC;AAAA,IAC5D;AAAA,EACF;AACF;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "remote-components",
3
- "version": "0.0.13",
3
+ "version": "0.0.15",
4
4
  "private": false,
5
5
  "description": "Compose remote components at runtime between microfrontends applications.",
6
6
  "keywords": [
@@ -119,6 +119,7 @@
119
119
  "react-server-dom-webpack": "^19.1.0",
120
120
  "style-to-js": "^1.1.16",
121
121
  "tsconfig-paths-webpack-plugin": "^4.2.0",
122
+ "web-streams-polyfill": "^4.2.0",
122
123
  "webpack": "5"
123
124
  },
124
125
  "devDependencies": {