@vitejs/plugin-rsc 0.5.4 → 0.5.6

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/README.md CHANGED
@@ -228,7 +228,7 @@ The plugin provides an additional helper for multi environment interaction.
228
228
 
229
229
  This allows importing `ssr` environment module specified by `environments.ssr.build.rollupOptions.input[entryName]` inside `rsc` environment and vice versa.
230
230
 
231
- During development, by default, this API assumes both `rsc` and `ssr` environments execute under the main Vite process. When enabling `rsc({ loadModuleDevProxy: true })` plugin option, the loaded module is implemented as a proxy with `fetch`-based RPC to call in node environment on the main Vite process, which for example, allows `rsc` environment inside cloudflare workers to access `ssr` environment on the main Vite process.
231
+ During development, by default, this API assumes both `rsc` and `ssr` environments execute under the main Vite process. When enabling `rsc({ loadModuleDevProxy: true })` plugin option, the loaded module is implemented as a proxy with `fetch`-based RPC to call in node environment on the main Vite process, which for example, allows `rsc` environment inside cloudflare workers to access `ssr` environment on the main Vite process. This proxy mechanism uses [turbo-stream](https://github.com/jacob-ebey/turbo-stream) for serializing data types beyond JSON, with custom encoders/decoders to additionally support `Request` and `Response` instances.
232
232
 
233
233
  During production build, this API will be rewritten into a static import of the specified entry of other environment build and the modules are executed inside the same runtime.
234
234
 
@@ -1,5 +1,5 @@
1
1
  import { t as createDebug } from "./dist-BRSdGcl7.js";
2
- import { n as parseIdQuery } from "./shared-BbMae1Gg.js";
2
+ import { n as parseIdQuery } from "./shared-AtH_QTi7.js";
3
3
  import fs from "node:fs";
4
4
  import path from "node:path";
5
5
  import { fileURLToPath, pathToFileURL } from "node:url";
package/dist/core/rsc.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import "../dist-BRSdGcl7.js";
2
2
  import "../shared-DEpnONZf.js";
3
- import { a as setRequireModule, i as loadServerAction, n as createServerDecodeClientManifest, r as createServerManifest, t as createClientManifest } from "../rsc-F60dNuzq.js";
3
+ import { a as setRequireModule, i as loadServerAction, n as createServerDecodeClientManifest, r as createServerManifest, t as createClientManifest } from "../rsc-BwsRzIFM.js";
4
4
 
5
5
  export { createClientManifest, createServerDecodeClientManifest, createServerManifest, loadServerAction, setRequireModule };
@@ -1,6 +1,6 @@
1
1
  import { r as once } from "./dist-BRSdGcl7.js";
2
2
  import { a as fromBase64, i as encryptBuffer, n as concatArrayStream, r as decryptBuffer, t as arrayToStream } from "./encryption-utils-6p8t4Xqm.js";
3
- import { n as createFromReadableStream, u as renderToReadableStream } from "./rsc-DaS5-eC_.js";
3
+ import { n as createFromReadableStream, u as renderToReadableStream } from "./rsc-BIM0wUrP.js";
4
4
  import encryptionKeySource from "virtual:vite-rsc/encryption-key";
5
5
 
6
6
  //#region src/utils/encryption-runtime.ts
package/dist/index.js CHANGED
@@ -2,9 +2,9 @@ import "./plugin-D1MQNdps.js";
2
2
  import "./dist-BRSdGcl7.js";
3
3
  import "./transforms-BcLQCXiC.js";
4
4
  import "./encryption-utils-6p8t4Xqm.js";
5
- import "./rpc-DgBxsMKW.js";
6
- import { r as vitePluginRsc, t as getPluginApi } from "./plugin-yqduHnqG.js";
7
- import "./cjs-D5eQjp5l.js";
8
- import "./shared-BbMae1Gg.js";
5
+ import "./rpc-CRpYrgKq.js";
6
+ import { r as vitePluginRsc, t as getPluginApi } from "./plugin-BIsCR7Tu.js";
7
+ import "./cjs-DHD_0drE.js";
8
+ import "./shared-AtH_QTi7.js";
9
9
 
10
10
  export { vitePluginRsc as default, getPluginApi };
@@ -3,9 +3,9 @@ import { t as vitePluginRscCore } from "./plugin-D1MQNdps.js";
3
3
  import { t as createDebug } from "./dist-BRSdGcl7.js";
4
4
  import { a as hasDirective, n as transformDirectiveProxyExport, o as transformWrapExport, s as findDirectives, t as transformServerActionServer } from "./transforms-BcLQCXiC.js";
5
5
  import { o as generateEncryptionKey, s as toBase64 } from "./encryption-utils-6p8t4Xqm.js";
6
- import { n as createRpcServer } from "./rpc-DgBxsMKW.js";
7
- import { t as cjsModuleRunnerPlugin } from "./cjs-D5eQjp5l.js";
8
- import { n as parseIdQuery, r as toCssVirtual, t as parseCssVirtual } from "./shared-BbMae1Gg.js";
6
+ import { n as createRpcServer } from "./rpc-CRpYrgKq.js";
7
+ import { t as cjsModuleRunnerPlugin } from "./cjs-DHD_0drE.js";
8
+ import { i as toCssVirtual, n as parseIdQuery, r as parseReferenceValidationVirtual, t as parseCssVirtual } from "./shared-AtH_QTi7.js";
9
9
  import { createRequire } from "node:module";
10
10
  import assert from "node:assert";
11
11
  import fs from "node:fs";
@@ -383,6 +383,23 @@ function vitePluginRscMinimal(rscPluginOptions = {}, manager = new RscPluginMana
383
383
  ...vitePluginUseClient(rscPluginOptions, manager),
384
384
  ...vitePluginUseServer(rscPluginOptions, manager),
385
385
  ...vitePluginDefineEncryptionKey(rscPluginOptions),
386
+ {
387
+ name: "rsc:reference-validation",
388
+ apply: "serve",
389
+ load: { handler(id, _options) {
390
+ if (id.startsWith("\0virtual:vite-rsc/reference-validation?")) {
391
+ const parsed = parseReferenceValidationVirtual(id);
392
+ assert(parsed);
393
+ if (parsed.type === "client") {
394
+ if (Object.values(manager.clientReferenceMetaMap).find((meta) => meta.referenceKey === parsed.id)) return `export {}`;
395
+ }
396
+ if (parsed.type === "server") {
397
+ if (Object.values(manager.serverReferenceMetaMap).find((meta) => meta.referenceKey === parsed.id)) return `export {}`;
398
+ }
399
+ this.error(`[vite-rsc] invalid ${parsed.type} reference '${parsed.id}'`);
400
+ }
401
+ } }
402
+ },
386
403
  scanBuildStripPlugin({ manager })
387
404
  ];
388
405
  }
package/dist/plugin.js CHANGED
@@ -2,9 +2,9 @@ import "./plugin-D1MQNdps.js";
2
2
  import "./dist-BRSdGcl7.js";
3
3
  import "./transforms-BcLQCXiC.js";
4
4
  import "./encryption-utils-6p8t4Xqm.js";
5
- import "./rpc-DgBxsMKW.js";
6
- import { i as vitePluginRscMinimal, n as transformRscCssExport, r as vitePluginRsc, t as getPluginApi } from "./plugin-yqduHnqG.js";
7
- import "./cjs-D5eQjp5l.js";
8
- import "./shared-BbMae1Gg.js";
5
+ import "./rpc-CRpYrgKq.js";
6
+ import { i as vitePluginRscMinimal, n as transformRscCssExport, r as vitePluginRsc, t as getPluginApi } from "./plugin-BIsCR7Tu.js";
7
+ import "./cjs-DHD_0drE.js";
8
+ import "./shared-AtH_QTi7.js";
9
9
 
10
10
  export { vitePluginRsc as default, getPluginApi, transformRscCssExport, vitePluginRscMinimal };
@@ -1,5 +1,5 @@
1
1
  import "../dist-BRSdGcl7.js";
2
- import { t as cjsModuleRunnerPlugin } from "../cjs-D5eQjp5l.js";
3
- import "../shared-BbMae1Gg.js";
2
+ import { t as cjsModuleRunnerPlugin } from "../cjs-DHD_0drE.js";
3
+ import "../shared-AtH_QTi7.js";
4
4
 
5
5
  export { cjsModuleRunnerPlugin };
package/dist/react/rsc.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import "../dist-BRSdGcl7.js";
2
2
  import "../shared-DEpnONZf.js";
3
- import { a as setRequireModule, i as loadServerAction } from "../rsc-F60dNuzq.js";
4
- import { a as decodeFormState, c as registerClientReference, i as decodeAction, l as registerServerReference, n as createFromReadableStream, o as decodeReply, r as createTemporaryReferenceSet, s as encodeReply, t as createClientTemporaryReferenceSet, u as renderToReadableStream } from "../rsc-DaS5-eC_.js";
3
+ import { a as setRequireModule, i as loadServerAction } from "../rsc-BwsRzIFM.js";
4
+ import { a as decodeFormState, c as registerClientReference, i as decodeAction, l as registerServerReference, n as createFromReadableStream, o as decodeReply, r as createTemporaryReferenceSet, s as encodeReply, t as createClientTemporaryReferenceSet, u as renderToReadableStream } from "../rsc-BIM0wUrP.js";
5
5
 
6
6
  export { createClientTemporaryReferenceSet, createFromReadableStream, createTemporaryReferenceSet, decodeAction, decodeFormState, decodeReply, encodeReply, loadServerAction, registerClientReference, registerServerReference, renderToReadableStream, setRequireModule };
@@ -1,10 +1,49 @@
1
1
  import { decode, encode } from "turbo-stream";
2
2
 
3
3
  //#region src/utils/rpc.ts
4
+ const decodePlugins = [(type, ...rest) => {
5
+ switch (type) {
6
+ case "Request": {
7
+ const [method, url, headers, body] = rest;
8
+ return { value: new Request(url, {
9
+ body,
10
+ headers,
11
+ method,
12
+ duplex: body ? "half" : void 0
13
+ }) };
14
+ }
15
+ case "Response": {
16
+ const [status, statusText, headers, body] = rest;
17
+ return { value: new Response(body, {
18
+ headers,
19
+ status,
20
+ statusText
21
+ }) };
22
+ }
23
+ }
24
+ return false;
25
+ }];
26
+ const encodePlugins = [(obj) => {
27
+ if (obj instanceof Request) return [
28
+ "Request",
29
+ obj.method,
30
+ obj.url,
31
+ Array.from(obj.headers),
32
+ obj.body
33
+ ];
34
+ if (obj instanceof Response) return [
35
+ "Response",
36
+ obj.status,
37
+ obj.statusText,
38
+ Array.from(obj.headers),
39
+ obj.body
40
+ ];
41
+ return false;
42
+ }];
4
43
  function createRpcServer(handlers) {
5
44
  return async (request) => {
6
45
  if (!request.body) throw new Error(`loadModuleDevProxy error: missing request body`);
7
- const reqPayload = await decode(request.body.pipeThrough(new TextDecoderStream()));
46
+ const reqPayload = await decode(request.body.pipeThrough(new TextDecoderStream()), { plugins: decodePlugins });
8
47
  const handler = handlers[reqPayload.method];
9
48
  if (!handler) throw new Error(`loadModuleDevProxy error: unknown method ${reqPayload.method}`);
10
49
  const resPayload = {
@@ -17,7 +56,10 @@ function createRpcServer(handlers) {
17
56
  resPayload.ok = false;
18
57
  resPayload.data = e;
19
58
  }
20
- return new Response(encode(resPayload));
59
+ return new Response(encode(resPayload, {
60
+ plugins: encodePlugins,
61
+ redactErrors: false
62
+ }));
21
63
  };
22
64
  }
23
65
  function createRpcClient(options) {
@@ -25,6 +67,9 @@ function createRpcClient(options) {
25
67
  const body = encode({
26
68
  method,
27
69
  args
70
+ }, {
71
+ plugins: encodePlugins,
72
+ redactErrors: false
28
73
  }).pipeThrough(new TextEncoderStream());
29
74
  const res = await fetch(options.endpoint, {
30
75
  method: "POST",
@@ -32,7 +77,7 @@ function createRpcClient(options) {
32
77
  duplex: "half"
33
78
  });
34
79
  if (!res.ok || !res.body) throw new Error(`loadModuleDevProxy error: ${res.status} ${res.statusText}`);
35
- const resPayload = await decode(res.body.pipeThrough(new TextDecoderStream()));
80
+ const resPayload = await decode(res.body.pipeThrough(new TextDecoderStream()), { plugins: decodePlugins });
36
81
  if (!resPayload.ok) throw resPayload.data;
37
82
  return resPayload.data;
38
83
  }
@@ -1,4 +1,4 @@
1
- import { n as createServerDecodeClientManifest, r as createServerManifest, t as createClientManifest } from "./rsc-F60dNuzq.js";
1
+ import { n as createServerDecodeClientManifest, r as createServerManifest, t as createClientManifest } from "./rsc-BwsRzIFM.js";
2
2
  import * as ReactClient from "@vitejs/plugin-rsc/vendor/react-server-dom/client.edge";
3
3
  import * as ReactServer from "@vitejs/plugin-rsc/vendor/react-server-dom/server.edge";
4
4
 
@@ -21,17 +21,11 @@ function setRequireModule(options) {
21
21
  throw new Error(`Unexpectedly client reference export '${name}' is called on server`);
22
22
  }, id, name);
23
23
  };
24
- return new Proxy(target, {
25
- get(_target, name, _receiver) {
26
- if (typeof name !== "string" || name === "then") return;
27
- return getOrCreateClientReference(name);
28
- },
29
- getOwnPropertyDescriptor(_target, name) {
30
- if (typeof name !== "string" || name === "then") return Reflect.getOwnPropertyDescriptor(target, name);
31
- getOrCreateClientReference(name);
32
- return Reflect.getOwnPropertyDescriptor(target, name);
33
- }
34
- });
24
+ return new Proxy(target, { getOwnPropertyDescriptor(_target, name) {
25
+ if (typeof name !== "string" || name === "then") return Reflect.getOwnPropertyDescriptor(target, name);
26
+ getOrCreateClientReference(name);
27
+ return Reflect.getOwnPropertyDescriptor(target, name);
28
+ } });
35
29
  }
36
30
  return requireModule(id);
37
31
  });
package/dist/rsc.js CHANGED
@@ -1,20 +1,29 @@
1
1
  import "./dist-BRSdGcl7.js";
2
2
  import "./encryption-utils-6p8t4Xqm.js";
3
+ import { a as toReferenceValidationVirtual } from "./shared-AtH_QTi7.js";
3
4
  import "./shared-DEpnONZf.js";
4
- import { a as setRequireModule, i as loadServerAction, r as createServerManifest, t as createClientManifest } from "./rsc-F60dNuzq.js";
5
- import { a as decodeFormState, c as registerClientReference, i as decodeAction, l as registerServerReference, n as createFromReadableStream, o as decodeReply, r as createTemporaryReferenceSet, s as encodeReply, t as createClientTemporaryReferenceSet, u as renderToReadableStream } from "./rsc-DaS5-eC_.js";
6
- import { n as encryptActionBoundArgs, t as decryptActionBoundArgs } from "./encryption-runtime-B0Jz_4Y_.js";
5
+ import { a as setRequireModule, i as loadServerAction, r as createServerManifest, t as createClientManifest } from "./rsc-BwsRzIFM.js";
6
+ import { a as decodeFormState, c as registerClientReference, i as decodeAction, l as registerServerReference, n as createFromReadableStream, o as decodeReply, r as createTemporaryReferenceSet, s as encodeReply, t as createClientTemporaryReferenceSet, u as renderToReadableStream } from "./rsc-BIM0wUrP.js";
7
+ import { n as encryptActionBoundArgs, t as decryptActionBoundArgs } from "./encryption-runtime-DlHpEnUc.js";
7
8
  import serverReferences from "virtual:vite-rsc/server-references";
8
9
 
9
10
  //#region src/rsc.tsx
10
11
  initialize();
11
12
  function initialize() {
12
13
  setRequireModule({ load: async (id) => {
13
- if (!import.meta.env.__vite_rsc_build__) return import(
14
- /* @vite-ignore */
15
- id
14
+ if (!import.meta.env.__vite_rsc_build__) {
15
+ await import(
16
+ /* @vite-ignore */
17
+ "/@id/__x00__" + toReferenceValidationVirtual({
18
+ id,
19
+ type: "server"
20
+ })
16
21
  );
17
- else {
22
+ return import(
23
+ /* @vite-ignore */
24
+ id
25
+ );
26
+ } else {
18
27
  const import_ = serverReferences[id];
19
28
  if (!import_) throw new Error(`server reference not found '${id}'`);
20
29
  return import_();
@@ -16,6 +16,12 @@ function parseIdQuery(id) {
16
16
  query: Object.fromEntries(new URLSearchParams(rawQuery))
17
17
  };
18
18
  }
19
+ function toReferenceValidationVirtual({ id, type }) {
20
+ return `virtual:vite-rsc/reference-validation?type=${type}&id=${encodeURIComponent(id)}&lang.js`;
21
+ }
22
+ function parseReferenceValidationVirtual(id) {
23
+ if (id.startsWith("\0virtual:vite-rsc/reference-validation?")) return parseIdQuery(id).query;
24
+ }
19
25
 
20
26
  //#endregion
21
- export { parseIdQuery as n, toCssVirtual as r, parseCssVirtual as t };
27
+ export { toReferenceValidationVirtual as a, toCssVirtual as i, parseIdQuery as n, parseReferenceValidationVirtual as r, parseCssVirtual as t };
package/dist/ssr.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import "./dist-BRSdGcl7.js";
2
- import { r as toCssVirtual } from "./shared-BbMae1Gg.js";
2
+ import { a as toReferenceValidationVirtual, i as toCssVirtual } from "./shared-AtH_QTi7.js";
3
3
  import "./shared-DEpnONZf.js";
4
4
  import { n as setRequireModule, t as createServerConsumerManifest } from "./ssr-CDPTWY93.js";
5
5
  import { i as findSourceMapURL, n as createFromReadableStream, r as createServerReference, t as callServer } from "./ssr-B-bLmlbm.js";
@@ -11,20 +11,28 @@ import * as ReactDOM from "react-dom";
11
11
  initialize();
12
12
  function initialize() {
13
13
  setRequireModule({ load: async (id) => {
14
- if (!import.meta.env.__vite_rsc_build__) return wrapResourceProxy(await import(
15
- /* @vite-ignore */
16
- id
17
- ), {
18
- js: [],
19
- css: (await import(
14
+ if (!import.meta.env.__vite_rsc_build__) {
15
+ await import(
20
16
  /* @vite-ignore */
21
- "/@id/__x00__" + toCssVirtual({
17
+ "/@id/__x00__" + toReferenceValidationVirtual({
22
18
  id,
23
- type: "ssr"
19
+ type: "client"
24
20
  })
21
+ );
22
+ return wrapResourceProxy(await import(
23
+ /* @vite-ignore */
24
+ id
25
+ ), {
26
+ js: [],
27
+ css: (await import(
28
+ /* @vite-ignore */
29
+ "/@id/__x00__" + toCssVirtual({
30
+ id,
31
+ type: "ssr"
32
+ })
25
33
  )).default
26
- });
27
- else {
34
+ });
35
+ } else {
28
36
  const import_ = clientReferences.default[id];
29
37
  if (!import_) throw new Error(`client reference not found '${id}'`);
30
38
  const deps = assetsManifest.clientReferenceDeps[id];
@@ -1,8 +1,8 @@
1
1
  import "../dist-BRSdGcl7.js";
2
2
  import "../encryption-utils-6p8t4Xqm.js";
3
3
  import "../shared-DEpnONZf.js";
4
- import "../rsc-F60dNuzq.js";
5
- import "../rsc-DaS5-eC_.js";
6
- import { n as encryptActionBoundArgs, t as decryptActionBoundArgs } from "../encryption-runtime-B0Jz_4Y_.js";
4
+ import "../rsc-BwsRzIFM.js";
5
+ import "../rsc-BIM0wUrP.js";
6
+ import { n as encryptActionBoundArgs, t as decryptActionBoundArgs } from "../encryption-runtime-DlHpEnUc.js";
7
7
 
8
8
  export { decryptActionBoundArgs, encryptActionBoundArgs };
package/dist/utils/rpc.js CHANGED
@@ -1,3 +1,3 @@
1
- import { n as createRpcServer, t as createRpcClient } from "../rpc-DgBxsMKW.js";
1
+ import { n as createRpcServer, t as createRpcClient } from "../rpc-CRpYrgKq.js";
2
2
 
3
3
  export { createRpcClient, createRpcServer };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitejs/plugin-rsc",
3
- "version": "0.5.4",
3
+ "version": "0.5.6",
4
4
  "description": "React Server Components (RSC) support for Vite.",
5
5
  "keywords": [
6
6
  "vite",