@reckona/mreact-dom 0.0.65 → 0.0.67

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reckona/mreact-dom",
3
- "version": "0.0.65",
3
+ "version": "0.0.67",
4
4
  "description": "React DOM-compatible entrypoints for mreact.",
5
5
  "keywords": [
6
6
  "dom",
@@ -24,7 +24,8 @@
24
24
  "dist/**/*.js",
25
25
  "dist/**/*.js.map",
26
26
  "dist/**/*.d.ts",
27
- "dist/**/*.d.ts.map"
27
+ "dist/**/*.d.ts.map",
28
+ "src/**/*"
28
29
  ],
29
30
  "type": "module",
30
31
  "sideEffects": false,
@@ -47,6 +48,6 @@
47
48
  "access": "public"
48
49
  },
49
50
  "dependencies": {
50
- "@reckona/mreact-compat": "0.0.65"
51
+ "@reckona/mreact-compat": "0.0.67"
51
52
  }
52
53
  }
package/src/client.ts ADDED
@@ -0,0 +1,4 @@
1
+ export { createRoot, hydrateRoot } from "@reckona/mreact-compat";
2
+ export type { HydrateRootOptions, Root, RootOptions } from "@reckona/mreact-compat";
3
+
4
+ export const version = "19.2.6";
package/src/index.ts ADDED
@@ -0,0 +1,240 @@
1
+ export {
2
+ createPortal,
3
+ flushSync,
4
+ render,
5
+ unmountComponentAtNode,
6
+ useActionState as useFormState,
7
+ } from "@reckona/mreact-compat";
8
+ import { runWithEventPriority } from "@reckona/mreact-compat/event-priority";
9
+ export { createRoot, hydrateRoot } from "./client.js";
10
+ export type { HydrateRootOptions, Root, RootOptions } from "./client.js";
11
+
12
+ export const version = "19.2.6";
13
+
14
+ export interface FormStatusNotPending {
15
+ pending: false;
16
+ data: null;
17
+ method: null;
18
+ action: null;
19
+ }
20
+
21
+ export interface FormStatusPending {
22
+ pending: true;
23
+ data: FormData;
24
+ method: string;
25
+ action: string | ((formData: FormData) => void | Promise<void>);
26
+ }
27
+
28
+ export type FormStatus = FormStatusPending | FormStatusNotPending;
29
+
30
+ export interface PreconnectOptions {
31
+ crossOrigin?: "anonymous" | "use-credentials" | "";
32
+ }
33
+
34
+ export type PreloadAs =
35
+ | "audio"
36
+ | "document"
37
+ | "embed"
38
+ | "fetch"
39
+ | "font"
40
+ | "image"
41
+ | "object"
42
+ | "track"
43
+ | "script"
44
+ | "style"
45
+ | "video"
46
+ | "worker";
47
+
48
+ export interface PreloadOptions {
49
+ as: PreloadAs;
50
+ crossOrigin?: "anonymous" | "use-credentials" | "";
51
+ fetchPriority?: "high" | "low" | "auto";
52
+ imageSizes?: string;
53
+ imageSrcSet?: string;
54
+ integrity?: string;
55
+ type?: string;
56
+ nonce?: string;
57
+ referrerPolicy?: ReferrerPolicy;
58
+ media?: string;
59
+ }
60
+
61
+ export interface PreloadModuleOptions {
62
+ as?: RequestDestination;
63
+ crossOrigin?: "anonymous" | "use-credentials" | "";
64
+ integrity?: string;
65
+ nonce?: string;
66
+ }
67
+
68
+ export interface PreinitOptions {
69
+ as: "script" | "style";
70
+ crossOrigin?: "anonymous" | "use-credentials" | "";
71
+ fetchPriority?: "high" | "low" | "auto";
72
+ precedence?: string;
73
+ integrity?: string;
74
+ nonce?: string;
75
+ }
76
+
77
+ export interface PreinitModuleOptions {
78
+ as?: "script";
79
+ crossOrigin?: "anonymous" | "use-credentials" | "";
80
+ integrity?: string;
81
+ nonce?: string;
82
+ }
83
+
84
+ type LinkAttributes = Record<string, string | undefined>;
85
+
86
+ const notPendingFormStatus: FormStatusNotPending = {
87
+ pending: false,
88
+ data: null,
89
+ method: null,
90
+ action: null,
91
+ };
92
+
93
+ export function useFormStatus(): FormStatus {
94
+ return notPendingFormStatus;
95
+ }
96
+
97
+ export function requestFormReset(form: HTMLFormElement): void {
98
+ form.reset();
99
+ }
100
+
101
+ export function unstable_batchedUpdates<T>(callback: () => T): T;
102
+ export function unstable_batchedUpdates<TArgument, TResult>(
103
+ callback: (argument: TArgument) => TResult,
104
+ argument: TArgument,
105
+ ): TResult;
106
+ export function unstable_batchedUpdates<TArgument, TResult>(
107
+ callback: ((argument: TArgument) => TResult) | (() => TResult),
108
+ argument?: TArgument,
109
+ ): TResult {
110
+ return runWithEventPriority("discrete", () =>
111
+ argument === undefined
112
+ ? (callback as () => TResult)()
113
+ : (callback as (argument: TArgument) => TResult)(argument)
114
+ );
115
+ }
116
+
117
+ export function prefetchDNS(href: string): void {
118
+ upsertHeadLink("dns-prefetch", href, {});
119
+ }
120
+
121
+ export function preconnect(href: string, options: PreconnectOptions = {}): void {
122
+ upsertHeadLink("preconnect", href, {
123
+ crossorigin: options.crossOrigin,
124
+ });
125
+ }
126
+
127
+ export function preload(href: string, options: PreloadOptions): void {
128
+ upsertHeadLink("preload", href, {
129
+ as: options.as,
130
+ crossorigin: options.crossOrigin,
131
+ fetchpriority: options.fetchPriority,
132
+ imagesrcset: options.imageSrcSet,
133
+ imagesizes: options.imageSizes,
134
+ integrity: options.integrity,
135
+ media: options.media,
136
+ nonce: options.nonce,
137
+ referrerpolicy: options.referrerPolicy,
138
+ type: options.type,
139
+ });
140
+ }
141
+
142
+ export function preloadModule(href: string, options: PreloadModuleOptions = {}): void {
143
+ upsertHeadLink("modulepreload", href, {
144
+ as: options.as,
145
+ crossorigin: options.crossOrigin,
146
+ integrity: options.integrity,
147
+ nonce: options.nonce,
148
+ });
149
+ }
150
+
151
+ export function preinit(href: string, options: PreinitOptions): void {
152
+ if (options.as === "style") {
153
+ upsertHeadLink("stylesheet", href, {
154
+ "data-precedence": options.precedence,
155
+ crossorigin: options.crossOrigin,
156
+ fetchpriority: options.fetchPriority,
157
+ integrity: options.integrity,
158
+ nonce: options.nonce,
159
+ });
160
+ return;
161
+ }
162
+
163
+ upsertHeadScript(href, {
164
+ async: "",
165
+ crossorigin: options.crossOrigin,
166
+ fetchpriority: options.fetchPriority,
167
+ integrity: options.integrity,
168
+ nonce: options.nonce,
169
+ });
170
+ }
171
+
172
+ export function preinitModule(href: string, options: PreinitModuleOptions = {}): void {
173
+ upsertHeadScript(href, {
174
+ type: "module",
175
+ async: "",
176
+ crossorigin: options.crossOrigin,
177
+ integrity: options.integrity,
178
+ nonce: options.nonce,
179
+ });
180
+ }
181
+
182
+ function upsertHeadLink(
183
+ rel: string,
184
+ href: string,
185
+ attributes: LinkAttributes,
186
+ ): void {
187
+ if (typeof document === "undefined") {
188
+ return;
189
+ }
190
+
191
+ const selector = `link[rel="${escapeSelectorValue(rel)}"][href="${escapeSelectorValue(href)}"]`;
192
+ if (document.head.querySelector(selector) !== null) {
193
+ return;
194
+ }
195
+
196
+ const link = document.createElement("link");
197
+ link.setAttribute("rel", rel);
198
+ link.setAttribute("href", href);
199
+ applyAttributes(link, attributes);
200
+ document.head.append(link);
201
+ }
202
+
203
+ function upsertHeadScript(
204
+ src: string,
205
+ attributes: LinkAttributes,
206
+ ): void {
207
+ if (typeof document === "undefined") {
208
+ return;
209
+ }
210
+
211
+ const selector = `script[src="${escapeSelectorValue(src)}"]`;
212
+ if (document.head.querySelector(selector) !== null) {
213
+ return;
214
+ }
215
+
216
+ const script = document.createElement("script");
217
+ if (attributes.type === "module") {
218
+ script.setAttribute("type", "module");
219
+ script.setAttribute("src", src);
220
+ applyAttributes(script, { ...attributes, type: undefined });
221
+ } else {
222
+ script.setAttribute("src", src);
223
+ applyAttributes(script, attributes);
224
+ }
225
+
226
+ document.head.append(script);
227
+ }
228
+
229
+ function applyAttributes(element: Element, attributes: LinkAttributes): void {
230
+ for (const [name, value] of Object.entries(attributes)) {
231
+ if (value === undefined) {
232
+ continue;
233
+ }
234
+ element.setAttribute(name, value);
235
+ }
236
+ }
237
+
238
+ function escapeSelectorValue(value: string): string {
239
+ return value.replaceAll("\\", "\\\\").replaceAll('"', '\\"');
240
+ }
package/src/server.ts ADDED
@@ -0,0 +1,259 @@
1
+ import {
2
+ renderToString as renderCompatToString,
3
+ type ReactCompatNode,
4
+ } from "@reckona/mreact-compat";
5
+
6
+ export const version = "19.2.6";
7
+
8
+ export interface PipeableStreamDestination {
9
+ write(chunk: string | Uint8Array): unknown;
10
+ end?(): unknown;
11
+ destroy?(error?: unknown): unknown;
12
+ }
13
+
14
+ export interface PipeableStream {
15
+ pipe<TDestination extends PipeableStreamDestination>(destination: TDestination): TDestination;
16
+ abort(reason?: unknown): void;
17
+ }
18
+
19
+ export interface BootstrapScriptDescriptor {
20
+ src: string;
21
+ integrity?: string;
22
+ crossOrigin?: string;
23
+ }
24
+
25
+ export interface ReactImportMap {
26
+ imports?: Record<string, string>;
27
+ integrity?: Record<string, string>;
28
+ scopes?: Record<string, Record<string, string>>;
29
+ }
30
+
31
+ export interface RenderBootstrapOptions {
32
+ nonce?: string;
33
+ importMap?: ReactImportMap;
34
+ bootstrapScriptContent?: string;
35
+ bootstrapScripts?: Array<string | BootstrapScriptDescriptor>;
36
+ bootstrapModules?: Array<string | BootstrapScriptDescriptor>;
37
+ }
38
+
39
+ export interface ServerOptions {
40
+ identifierPrefix?: string;
41
+ }
42
+
43
+ export interface RenderToPipeableStreamOptions {
44
+ identifierPrefix?: string;
45
+ namespaceURI?: string;
46
+ nonce?: string;
47
+ importMap?: ReactImportMap;
48
+ bootstrapScriptContent?: string;
49
+ bootstrapScripts?: Array<string | BootstrapScriptDescriptor>;
50
+ bootstrapModules?: Array<string | BootstrapScriptDescriptor>;
51
+ headersLengthHint?: number;
52
+ progressiveChunkSize?: number;
53
+ onHeaders?(headers: Headers): void;
54
+ onShellReady?(): void;
55
+ onAllReady?(): void;
56
+ onShellError?(error: unknown): void;
57
+ onError?(error: unknown, errorInfo?: { componentStack: string }): string | void;
58
+ formState?: unknown;
59
+ }
60
+
61
+ export interface RenderToReadableStreamOptions {
62
+ identifierPrefix?: string;
63
+ namespaceURI?: string;
64
+ nonce?: string;
65
+ importMap?: ReactImportMap;
66
+ bootstrapScriptContent?: string;
67
+ bootstrapScripts?: Array<string | BootstrapScriptDescriptor>;
68
+ bootstrapModules?: Array<string | BootstrapScriptDescriptor>;
69
+ headersLengthHint?: number;
70
+ progressiveChunkSize?: number;
71
+ signal?: AbortSignal;
72
+ onHeaders?(headers: Headers): void;
73
+ onError?(error: unknown, errorInfo?: { componentStack: string }): string | void;
74
+ formState?: unknown;
75
+ }
76
+
77
+ export interface ReactDOMServerReadableStream extends ReadableStream<Uint8Array> {
78
+ allReady: Promise<void>;
79
+ }
80
+
81
+ export type ResumeOptions = RenderToReadableStreamOptions & RenderToPipeableStreamOptions;
82
+ export type PostponedState = unknown;
83
+
84
+ export function renderToString(element: ReactCompatNode, _options?: ServerOptions): string {
85
+ return renderCompatToString(() => element);
86
+ }
87
+
88
+ export function renderToStaticMarkup(element: ReactCompatNode, options?: ServerOptions): string {
89
+ void options;
90
+ return renderToString(element);
91
+ }
92
+
93
+ export async function renderToReadableStream(
94
+ element: ReactCompatNode,
95
+ options: RenderToReadableStreamOptions = {},
96
+ ): Promise<ReactDOMServerReadableStream> {
97
+ const encoder = new TextEncoder();
98
+ const html = renderServerHtml(element, options);
99
+ notifyHeaders(options);
100
+ let resolveAllReady: () => void;
101
+ let rejectAllReady: (error: unknown) => void;
102
+ const allReady = new Promise<void>((resolve, reject) => {
103
+ resolveAllReady = resolve;
104
+ rejectAllReady = reject;
105
+ });
106
+
107
+ const stream = new ReadableStream<Uint8Array>({
108
+ start(controller) {
109
+ if (options.signal?.aborted === true) {
110
+ const reason = options.signal.reason;
111
+ rejectAllReady(reason);
112
+ controller.error(reason);
113
+ return;
114
+ }
115
+
116
+ const abort = () => {
117
+ const reason = options.signal?.reason;
118
+ rejectAllReady(reason);
119
+ controller.error(reason);
120
+ };
121
+ options.signal?.addEventListener("abort", abort, { once: true });
122
+
123
+ try {
124
+ controller.enqueue(encoder.encode(html));
125
+ controller.close();
126
+ resolveAllReady();
127
+ } catch (error) {
128
+ options.onError?.(error, { componentStack: "" });
129
+ rejectAllReady(error);
130
+ controller.error(error);
131
+ } finally {
132
+ options.signal?.removeEventListener("abort", abort);
133
+ }
134
+ },
135
+ });
136
+
137
+ return Object.assign(stream, { allReady });
138
+ }
139
+
140
+ export function renderToPipeableStream(
141
+ element: ReactCompatNode,
142
+ options: RenderToPipeableStreamOptions = {},
143
+ ): PipeableStream {
144
+ let aborted = false;
145
+
146
+ return {
147
+ pipe(destination) {
148
+ queueMicrotask(() => {
149
+ if (aborted) {
150
+ return;
151
+ }
152
+
153
+ try {
154
+ notifyHeaders(options);
155
+ options.onShellReady?.();
156
+ const html = renderServerHtml(element, options);
157
+ options.onAllReady?.();
158
+ destination.write(html);
159
+ destination.end?.();
160
+ } catch (error) {
161
+ options.onError?.(error, { componentStack: "" });
162
+ options.onShellError?.(error);
163
+ destination.destroy?.(error);
164
+ }
165
+ });
166
+
167
+ return destination;
168
+ },
169
+ abort(reason) {
170
+ aborted = true;
171
+ options.onError?.(reason ?? new Error("renderToPipeableStream was aborted."), {
172
+ componentStack: "",
173
+ });
174
+ },
175
+ };
176
+ }
177
+
178
+ export async function resume(
179
+ element: ReactCompatNode,
180
+ _postponedState: PostponedState,
181
+ options: ResumeOptions = {},
182
+ ): Promise<ReactDOMServerReadableStream> {
183
+ return renderToReadableStream(element, options);
184
+ }
185
+
186
+ export async function resumeToPipeableStream(
187
+ element: ReactCompatNode,
188
+ _postponedState: PostponedState,
189
+ options: ResumeOptions = {},
190
+ ): Promise<PipeableStream> {
191
+ return renderToPipeableStream(element, options);
192
+ }
193
+
194
+ function renderServerHtml(element: ReactCompatNode, options: RenderBootstrapOptions): string {
195
+ return `${renderToString(element)}${renderBootstrapResources(options)}`;
196
+ }
197
+
198
+ function renderBootstrapResources(options: RenderBootstrapOptions): string {
199
+ const nonce = options.nonce === undefined ? "" : ` nonce="${escapeAttribute(options.nonce)}"`;
200
+ const importMap =
201
+ options.importMap === undefined
202
+ ? ""
203
+ : `<script type="importmap"${nonce}>${escapeScriptContent(JSON.stringify(options.importMap))}</script>`;
204
+ const inlineScript =
205
+ options.bootstrapScriptContent === undefined
206
+ ? ""
207
+ : `<script${nonce}>${escapeScriptContent(options.bootstrapScriptContent)}</script>`;
208
+ const scripts = (options.bootstrapScripts ?? [])
209
+ .map((script) => renderExternalScript(script, nonce, undefined))
210
+ .join("");
211
+ const modules = (options.bootstrapModules ?? [])
212
+ .map((script) => renderExternalScript(script, nonce, "module"))
213
+ .join("");
214
+
215
+ return `${importMap}${inlineScript}${scripts}${modules}`;
216
+ }
217
+
218
+ function renderExternalScript(
219
+ script: string | BootstrapScriptDescriptor,
220
+ nonce: string,
221
+ type: "module" | undefined,
222
+ ): string {
223
+ const descriptor = typeof script === "string" ? { src: script } : script;
224
+ const typeAttribute = type === undefined ? "" : ` type="${type}"`;
225
+ const src = ` src="${escapeAttribute(descriptor.src)}"`;
226
+ const integrity =
227
+ descriptor.integrity === undefined ? "" : ` integrity="${escapeAttribute(descriptor.integrity)}"`;
228
+ const crossOrigin =
229
+ descriptor.crossOrigin === undefined
230
+ ? ""
231
+ : ` crossorigin="${escapeAttribute(descriptor.crossOrigin)}"`;
232
+
233
+ return `<script${typeAttribute}${src}${nonce}${integrity}${crossOrigin}></script>`;
234
+ }
235
+
236
+ function escapeAttribute(value: string): string {
237
+ return value
238
+ .replaceAll("&", "&amp;")
239
+ .replaceAll('"', "&quot;")
240
+ .replaceAll("<", "&lt;")
241
+ .replaceAll(">", "&gt;");
242
+ }
243
+
244
+ function escapeScriptContent(value: string): string {
245
+ return value
246
+ .replaceAll("<", "\\u003c")
247
+ .replaceAll("\u2028", "\\u2028")
248
+ .replaceAll("\u2029", "\\u2029");
249
+ }
250
+
251
+ function notifyHeaders(options: { onHeaders?(headers: Headers): void }): void {
252
+ if (options.onHeaders === undefined) {
253
+ return;
254
+ }
255
+
256
+ const headers = new Headers();
257
+ headers.set("content-type", "text/html; charset=utf-8");
258
+ options.onHeaders(headers);
259
+ }