@parcel/rsc 2.13.1-canary.3398

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017-present Devon Govett
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,12 @@
1
+ import { ReactNode } from 'react';
2
+ import { HydrationOptions } from 'react-dom/client';
3
+ export type CallServerCallback = <T>(id: string, args: any[]) => Promise<T>;
4
+ export interface HydrateOptions extends HydrationOptions {
5
+ handleServerAction?: CallServerCallback;
6
+ onHmrReload?: () => void;
7
+ }
8
+ export declare function hydrate(options?: HydrateOptions): (value: ReactNode, cb?: () => void) => void;
9
+ export interface RSCRequestInit extends Omit<RequestInit, 'body'> {
10
+ body?: any;
11
+ }
12
+ export declare function fetchRSC<T>(url: string | URL | Request, options?: RSCRequestInit): Promise<T>;
package/lib/node.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import type { IncomingMessage, ServerResponse } from 'http';
2
+ import { Readable } from 'stream';
3
+ import { RSCToHTMLOptions, RSCOptions } from './server';
4
+ export declare function renderRSC(root: any, options?: RSCOptions): Readable;
5
+ export declare function renderHTML(root: any, options?: RSCToHTMLOptions): Promise<Readable>;
6
+ export declare function renderRequest(request: IncomingMessage, response: ServerResponse, root: any, options?: RSCToHTMLOptions): Promise<void>;
7
+ export declare function callAction(request: IncomingMessage, id: string | null | undefined): Promise<{
8
+ result: any;
9
+ }>;
@@ -0,0 +1,32 @@
1
+ import type { ErrorInfo } from 'react-dom/client';
2
+ import { ComponentType } from 'react' with {
3
+ env: 'react-client'
4
+ };
5
+ export interface RSCOptions {
6
+ // environmentName?: string | (() => string),
7
+ // filterStackFrame?: (url: string, functionName: string) => boolean,
8
+ identifierPrefix?: string;
9
+ signal?: AbortSignal;
10
+ temporaryReferences?: any;
11
+ onError?: (error: unknown) => void;
12
+ onPostpone?: (reason: string) => void;
13
+ }
14
+ export declare function renderRSC(root: any, options?: RSCOptions): ReadableStream;
15
+ export interface RSCToHTMLOptions {
16
+ component?: ComponentType;
17
+ identifierPrefix?: string;
18
+ namespaceURI?: string;
19
+ nonce?: string;
20
+ progressiveChunkSize?: number;
21
+ signal?: AbortSignal;
22
+ temporaryReferences?: any;
23
+ onError?: (error: unknown, errorInfo?: ErrorInfo) => string | void;
24
+ }
25
+ export declare function renderRSCToHTML(root: any, options?: RSCToHTMLOptions): Promise<ReadableStream>;
26
+ export interface RenderRequestOptions extends RSCToHTMLOptions {
27
+ headers?: HeadersInit;
28
+ }
29
+ export declare function renderRequest(request: Request, root: any, options?: RenderRequestOptions): Promise<Response>;
30
+ export declare function callAction(request: Request, id: string | null | undefined): Promise<{
31
+ result: any;
32
+ }>;
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@parcel/rsc",
3
+ "version": "2.13.1-canary.3398+735a635f7",
4
+ "license": "MIT",
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "funding": {
9
+ "type": "opencollective",
10
+ "url": "https://opencollective.com/parcel"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/parcel-bundler/parcel.git"
15
+ },
16
+ "sideEffects": false,
17
+ "exports": {
18
+ "./client": {
19
+ "types": "./lib/client.d.ts",
20
+ "default": "./src/client.tsx"
21
+ },
22
+ "./server": {
23
+ "types": "./lib/server.d.ts",
24
+ "default": "./src/server.tsx"
25
+ },
26
+ "./node": {
27
+ "types": "./lib/node.d.ts",
28
+ "default": "./src/node.tsx"
29
+ }
30
+ },
31
+ "dependencies": {
32
+ "react-server-dom-parcel": "canary",
33
+ "rsc-html-stream": "^0.0.4"
34
+ },
35
+ "devDependencies": {
36
+ "@types/node": ">= 18",
37
+ "@types/react": "^19",
38
+ "@types/react-dom": "^19"
39
+ },
40
+ "peerDependencies": {
41
+ "react": "^19",
42
+ "react-dom": "^19"
43
+ },
44
+ "gitHead": "735a635f7eed538c449f37517b26247e0b19c1a4"
45
+ }
package/src/client.tsx ADDED
@@ -0,0 +1,64 @@
1
+ import { ReactNode, startTransition, useInsertionEffect } from 'react';
2
+ import {createFromReadableStream, createFromFetch, encodeReply, setServerCallback, createTemporaryReferenceSet} from 'react-server-dom-parcel/client';
3
+ import {rscStream} from 'rsc-html-stream/client';
4
+ import { hydrateRoot, HydrationOptions, Root } from 'react-dom/client';
5
+
6
+ // Stream in initial RSC payload embedded in the HTML.
7
+ let initialRSCPayload: Promise<ReactNode>;
8
+ function RSCRoot({value, cb}: {value?: ReactNode, cb?: () => void}) {
9
+ initialRSCPayload ??= createFromReadableStream<ReactNode>(rscStream);
10
+ useInsertionEffect(() => {
11
+ cb?.();
12
+ });
13
+ return value === undefined ? initialRSCPayload : value;
14
+ }
15
+
16
+ export type CallServerCallback = <T>(id: string, args: any[]) => Promise<T>;
17
+ export interface HydrateOptions extends HydrationOptions {
18
+ handleServerAction?: CallServerCallback,
19
+ onHmrReload?: () => void
20
+ }
21
+
22
+ export function hydrate(options?: HydrateOptions): (value: ReactNode, cb?: () => void) => void {
23
+ if (options?.handleServerAction) {
24
+ setServerCallback(options.handleServerAction);
25
+ }
26
+
27
+ if (options?.onHmrReload) {
28
+ window.addEventListener('parcelhmrreload', e => {
29
+ e.preventDefault();
30
+ options?.onHmrReload?.();
31
+ });
32
+ }
33
+
34
+ let root: Root;
35
+ startTransition(() => {
36
+ root = hydrateRoot(document, <RSCRoot />, options);
37
+ });
38
+
39
+ return (value: ReactNode, cb?: () => void) => {
40
+ startTransition(() => {
41
+ root?.render(<RSCRoot value={value} cb={() => {cb?.(); cb = undefined}} />);
42
+ });
43
+ };
44
+ }
45
+
46
+ export interface RSCRequestInit extends Omit<RequestInit, 'body'> {
47
+ body?: any
48
+ }
49
+
50
+ export async function fetchRSC<T>(url: string | URL | Request, options?: RSCRequestInit): Promise<T> {
51
+ const temporaryReferences = createTemporaryReferenceSet();
52
+ const response = fetch(url, {
53
+ ...options,
54
+ headers: {
55
+ Accept: 'text/x-component',
56
+ ...options?.headers,
57
+ },
58
+ body: options && 'body' in options
59
+ ? await encodeReply(options.body, {temporaryReferences, signal: options?.signal})
60
+ : undefined,
61
+ });
62
+
63
+ return createFromFetch<T>(response, {temporaryReferences});
64
+ }
package/src/node.tsx ADDED
@@ -0,0 +1,48 @@
1
+ import type { IncomingMessage, ServerResponse } from 'http';
2
+ import type { ReadableStream as NodeReadableStream } from 'stream/web';
3
+
4
+ import {Readable} from 'stream';
5
+ import {renderToReadableStream, createTemporaryReferenceSet} from 'react-server-dom-parcel/server.edge';
6
+ import {RSCToHTMLOptions, RSCOptions, renderRSCToHTML as renderRSCToHTMLBase, callAction as callActionBase} from './server';
7
+
8
+ export function renderRSC(root: any, options?: RSCOptions): Readable {
9
+ return Readable.fromWeb(renderToReadableStream(root, options) as NodeReadableStream);
10
+ }
11
+
12
+ export async function renderHTML(root: any, options?: RSCToHTMLOptions): Promise<Readable> {
13
+ let htmlStream = await renderRSCToHTMLBase(root, options);
14
+ return Readable.fromWeb(htmlStream as NodeReadableStream);
15
+ }
16
+
17
+ const temporaryReferencesSymbol = Symbol.for('temporaryReferences')
18
+
19
+ export async function renderRequest(request: IncomingMessage, response: ServerResponse, root: any, options?: RSCToHTMLOptions): Promise<void> {
20
+ options = {
21
+ ...options,
22
+ temporaryReferences: options?.temporaryReferences ?? (request as any)[temporaryReferencesSymbol]
23
+ };
24
+
25
+ if (request.headers.accept?.includes('text/html')) {
26
+ let html = await renderHTML(root, options);
27
+ response.setHeader('Content-Type', 'text/html');
28
+ html.pipe(response);
29
+ } else {
30
+ response.setHeader('Content-Type', 'text/x-component');
31
+ renderRSC(root, options).pipe(response);
32
+ }
33
+ }
34
+
35
+ export async function callAction(request: IncomingMessage, id: string | null | undefined): Promise<{result: any}> {
36
+ (request as any)[temporaryReferencesSymbol] ??= createTemporaryReferenceSet();
37
+
38
+ let req = new Request('http://localhost' + request.url, {
39
+ method: 'POST',
40
+ headers: request.headers as any,
41
+ body: Readable.toWeb(request) as ReadableStream,
42
+ // @ts-ignore
43
+ duplex: 'half'
44
+ });
45
+
46
+ (req as any)[temporaryReferencesSymbol] = (request as any)[temporaryReferencesSymbol];
47
+ return callActionBase(req, id);
48
+ }
package/src/server.tsx ADDED
@@ -0,0 +1,117 @@
1
+ import type { ErrorInfo } from 'react-dom/client';
2
+
3
+ // Server dependencies.
4
+ import {renderToReadableStream, loadServerAction, decodeReply, decodeAction, createTemporaryReferenceSet} from 'react-server-dom-parcel/server.edge';
5
+ import {injectRSCPayload} from 'rsc-html-stream/server';
6
+
7
+ // Client dependencies, used for SSR.
8
+ // These must run in the same environment as client components (e.g. same instance of React).
9
+ import {createFromReadableStream} from 'react-server-dom-parcel/client.edge' with {env: 'react-client'};
10
+ import {renderToReadableStream as renderHTMLToReadableStream} from 'react-dom/server.edge' with {env: 'react-client'};
11
+ import {ComponentType, ReactNode} from 'react' with {env: 'react-client'};
12
+
13
+ export interface RSCOptions {
14
+ // environmentName?: string | (() => string),
15
+ // filterStackFrame?: (url: string, functionName: string) => boolean,
16
+ identifierPrefix?: string,
17
+ signal?: AbortSignal,
18
+ temporaryReferences?: any,
19
+ onError?: (error: unknown) => void,
20
+ onPostpone?: (reason: string) => void,
21
+ }
22
+
23
+ export function renderRSC(root: any, options?: RSCOptions): ReadableStream {
24
+ return renderToReadableStream(root, options);
25
+ }
26
+
27
+ export interface RSCToHTMLOptions {
28
+ component?: ComponentType,
29
+ identifierPrefix?: string;
30
+ namespaceURI?: string;
31
+ nonce?: string;
32
+ progressiveChunkSize?: number;
33
+ signal?: AbortSignal;
34
+ temporaryReferences?: any,
35
+ onError?: (error: unknown, errorInfo?: ErrorInfo) => string | void;
36
+ }
37
+
38
+ export async function renderRSCToHTML(root: any, options?: RSCToHTMLOptions): Promise<ReadableStream> {
39
+ let rscStream = renderToReadableStream(root, options);
40
+
41
+ // Use client react to render the RSC payload to HTML.
42
+ let [s1, s2] = rscStream.tee();
43
+ let data: Promise<ReactNode>;
44
+ function Content() {
45
+ // Important: this must be constructed inside a component for preinit scripts to be inserted.
46
+ data ??= createFromReadableStream<ReactNode>(s1);
47
+ return data;
48
+ }
49
+
50
+ let htmlStream = await renderHTMLToReadableStream(<Content />, {
51
+ ...options,
52
+ bootstrapScriptContent: (options?.component as any)?.bootstrapScript
53
+ });
54
+
55
+ return htmlStream.pipeThrough(injectRSCPayload(s2));
56
+ }
57
+
58
+ export interface RenderRequestOptions extends RSCToHTMLOptions {
59
+ headers?: HeadersInit
60
+ }
61
+
62
+ const temporaryReferencesSymbol = Symbol.for('temporaryReferences')
63
+
64
+ export async function renderRequest(request: Request, root: any, options?: RenderRequestOptions): Promise<Response> {
65
+ options = {
66
+ ...options,
67
+ temporaryReferences: options?.temporaryReferences ?? (request as any)[temporaryReferencesSymbol]
68
+ };
69
+
70
+ if (request.headers.get('Accept')?.includes('text/html')) {
71
+ let html = await renderRSCToHTML(root, options);
72
+ return new Response(html, {
73
+ headers: {
74
+ ...options?.headers,
75
+ 'Content-Type': 'text/html'
76
+ }
77
+ });
78
+ } else {
79
+ let rscStream = renderToReadableStream(root, options);
80
+ return new Response(rscStream, {
81
+ headers: {
82
+ ...options?.headers,
83
+ 'Content-Type': 'text/x-component'
84
+ }
85
+ });
86
+ }
87
+ }
88
+
89
+ export async function callAction(request: Request, id: string | null | undefined): Promise<{result: any}> {
90
+ (request as any)[temporaryReferencesSymbol] ??= createTemporaryReferenceSet();
91
+
92
+ if (id) {
93
+ let action = await loadServerAction(id);
94
+ let body = request.headers.get('content-type')?.includes('multipart/form-data')
95
+ ? await request.formData()
96
+ : await request.text();
97
+ let args = await decodeReply<any[]>(body, {
98
+ temporaryReferences: (request as any)[temporaryReferencesSymbol]
99
+ });
100
+
101
+ let result = action.apply(null, args);
102
+ try {
103
+ // Wait for any mutations
104
+ await result;
105
+ } catch {
106
+ // Handle the error on the client
107
+ }
108
+ return {result};
109
+ } else {
110
+ // Form submitted by browser (progressive enhancement).
111
+ let formData = await request.formData();
112
+ let action = await decodeAction(formData);
113
+ // Don't catch error here: this should be handled by the caller (e.g. render an error page).
114
+ let result = await action();
115
+ return {result};
116
+ }
117
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "compilerOptions": {
3
+ /* Visit https://aka.ms/tsconfig to read more about this file */
4
+ "target": "ES2020",
5
+ "lib": ["ES2020", "DOM", "DOM.Iterable"],
6
+ "jsx": "react-jsx",
7
+ "useDefineForClassFields": true,
8
+
9
+ /* Modules */
10
+ "module": "ESNext",
11
+ "moduleResolution": "bundler",
12
+
13
+ /* Emit */
14
+ "noEmit": true,
15
+ "declaration": true,
16
+
17
+ /* Interop Constraints */
18
+ "isolatedModules": true,
19
+ "isolatedDeclarations": true,
20
+ "allowSyntheticDefaultImports": true,
21
+ "allowImportingTsExtensions": true,
22
+ "esModuleInterop": true,
23
+ "forceConsistentCasingInFileNames": true,
24
+
25
+ /* Type Checking */
26
+ "strict": true,
27
+
28
+ /* Completeness */
29
+ "skipLibCheck": true
30
+ }
31
+ }
package/types.d.ts ADDED
@@ -0,0 +1,81 @@
1
+ declare module 'react-server-dom-parcel/client' {
2
+ type TemporaryReferenceSet = {__ref: true};
3
+ type ReactCustomFormAction = {
4
+ name?: string;
5
+ action?: string;
6
+ encType?: string;
7
+ method?: string;
8
+ target?: string;
9
+ data?: null | FormData;
10
+ };
11
+ type EncodeFormActionCallback = <A>(
12
+ id: any,
13
+ args: Promise<A>,
14
+ ) => ReactCustomFormAction;
15
+ type Options = {
16
+ nonce?: string;
17
+ encodeFormAction?: EncodeFormActionCallback;
18
+ temporaryReferences?: TemporaryReferenceSet;
19
+ replayConsoleLogs?: boolean;
20
+ environmentName?: string;
21
+ };
22
+
23
+ export function createFromFetch<T>(
24
+ res: Promise<Response>,
25
+ options?: Options,
26
+ ): Promise<T>;
27
+ export function createFromReadableStream<T>(
28
+ stream: ReadableStream,
29
+ options?: Options,
30
+ ): Promise<T>;
31
+ export function encodeReply(
32
+ value: any,
33
+ options?: {
34
+ temporaryReferences?: TemporaryReferenceSet;
35
+ signal?: AbortSignal | null | undefined;
36
+ },
37
+ ): Promise<string | URLSearchParams | FormData>;
38
+ export function createTemporaryReferenceSet(): TemporaryReferenceSet;
39
+
40
+ type CallServerCallback = <T>(id: string, args: any[]) => Promise<T>;
41
+ export function setServerCallback(cb: CallServerCallback): void;
42
+ }
43
+
44
+ declare module 'react-server-dom-parcel/client.edge' {
45
+ export function createFromReadableStream<T>(
46
+ stream: ReadableStream,
47
+ ): Promise<T>;
48
+ }
49
+
50
+ declare module 'react-server-dom-parcel/server.edge' {
51
+ type TemporaryReferenceSet = {__ref: true};
52
+ type Options = {
53
+ environmentName?: string | (() => string);
54
+ filterStackFrame?: (url: string, functionName: string) => boolean;
55
+ identifierPrefix?: string;
56
+ signal?: AbortSignal;
57
+ temporaryReferences?: TemporaryReferenceSet;
58
+ onError?: (error: unknown) => void;
59
+ onPostpone?: (reason: string) => void;
60
+ };
61
+
62
+ export function renderToReadableStream(
63
+ value: any,
64
+ options?: Options,
65
+ ): ReadableStream;
66
+ export function loadServerAction(
67
+ id: string,
68
+ ): Promise<(...args: any[]) => Promise<any>>;
69
+ export function decodeReply<T>(
70
+ body: string | FormData,
71
+ options?: {temporaryReferences?: TemporaryReferenceSet},
72
+ ): Promise<T>;
73
+ export function decodeAction(
74
+ body: FormData,
75
+ ): Promise<(...args: any[]) => Promise<any>>;
76
+ export function createTemporaryReferenceSet(): TemporaryReferenceSet;
77
+ }
78
+
79
+ declare module 'react-dom/server.edge' {
80
+ export * from 'react-dom/server';
81
+ }