@lazarv/react-server 0.0.0-experimental-43e79e6-20230928
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 +21 -0
- package/README.md +5 -0
- package/bin/cli.mjs +93 -0
- package/bin/commands/build.mjs +19 -0
- package/bin/commands/dev.mjs +23 -0
- package/bin/commands/start.mjs +16 -0
- package/bin/loader.mjs +38 -0
- package/client/ActionState.mjs +16 -0
- package/client/ClientOnly.jsx +14 -0
- package/client/ClientProvider.jsx +243 -0
- package/client/ErrorBoundary.jsx +45 -0
- package/client/FlightContext.mjs +3 -0
- package/client/Link.jsx +59 -0
- package/client/Params.mjs +15 -0
- package/client/ReactServerComponent.jsx +77 -0
- package/client/Refresh.jsx +52 -0
- package/client/components.mjs +28 -0
- package/client/context.mjs +6 -0
- package/client/entry.client.jsx +146 -0
- package/client/index.jsx +6 -0
- package/client/navigation.jsx +4 -0
- package/config/context.mjs +37 -0
- package/config/index.mjs +114 -0
- package/lib/build/action.mjs +57 -0
- package/lib/build/banner.mjs +13 -0
- package/lib/build/chunks.mjs +26 -0
- package/lib/build/client.mjs +114 -0
- package/lib/build/custom-logger.mjs +13 -0
- package/lib/build/dependencies.mjs +54 -0
- package/lib/build/resolve.mjs +101 -0
- package/lib/build/server.mjs +142 -0
- package/lib/build/static.mjs +89 -0
- package/lib/dev/action.mjs +63 -0
- package/lib/dev/create-logger.mjs +52 -0
- package/lib/dev/create-server.mjs +208 -0
- package/lib/dev/modules.mjs +20 -0
- package/lib/dev/ssr-handler.mjs +135 -0
- package/lib/handlers/error.mjs +153 -0
- package/lib/handlers/not-found.mjs +5 -0
- package/lib/handlers/redirect.mjs +1 -0
- package/lib/handlers/rewrite.mjs +1 -0
- package/lib/handlers/static.mjs +120 -0
- package/lib/handlers/trailing-slash.mjs +12 -0
- package/lib/plugins/react-server.mjs +73 -0
- package/lib/plugins/use-client.mjs +135 -0
- package/lib/plugins/use-server.mjs +175 -0
- package/lib/start/action.mjs +110 -0
- package/lib/start/create-server.mjs +111 -0
- package/lib/start/manifest.mjs +104 -0
- package/lib/start/ssr-handler.mjs +134 -0
- package/lib/sys.mjs +49 -0
- package/lib/utils/merge.mjs +31 -0
- package/lib/utils/server-address.mjs +14 -0
- package/memory-cache/index.mjs +125 -0
- package/package.json +81 -0
- package/react-server.d.ts +209 -0
- package/server/ErrorBoundary.jsx +14 -0
- package/server/RemoteComponent.jsx +210 -0
- package/server/Route.jsx +108 -0
- package/server/actions.mjs +72 -0
- package/server/cache.mjs +19 -0
- package/server/client-component.mjs +62 -0
- package/server/context.mjs +32 -0
- package/server/cookies.mjs +14 -0
- package/server/entry.server.jsx +972 -0
- package/server/error-boundary.jsx +2 -0
- package/server/http-headers.mjs +8 -0
- package/server/http-status.mjs +6 -0
- package/server/index.mjs +14 -0
- package/server/logger.mjs +15 -0
- package/server/module-loader.mjs +20 -0
- package/server/redirects.mjs +45 -0
- package/server/remote-component.jsx +2 -0
- package/server/request.mjs +37 -0
- package/server/revalidate.mjs +22 -0
- package/server/rewrites.mjs +0 -0
- package/server/router.jsx +6 -0
- package/server/runtime.mjs +32 -0
- package/server/symbols.mjs +24 -0
package/server/index.mjs
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export { server$ } from "./actions.mjs";
|
|
2
|
+
export { cache$ } from "./cache.mjs";
|
|
3
|
+
export { redirect } from "./redirects.mjs";
|
|
4
|
+
export {
|
|
5
|
+
useRequest,
|
|
6
|
+
useResponse,
|
|
7
|
+
useUrl,
|
|
8
|
+
useFormData,
|
|
9
|
+
rewrite,
|
|
10
|
+
} from "./request.mjs";
|
|
11
|
+
export { revalidate } from "./revalidate.mjs";
|
|
12
|
+
export { status } from "./http-status.mjs";
|
|
13
|
+
export { headers } from "./http-headers.mjs";
|
|
14
|
+
export { cookie, setCookie, deleteCookie } from "./cookies.mjs";
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { getRuntime } from "./runtime.mjs";
|
|
2
|
+
import { LOGGER_CONTEXT } from "./symbols.mjs";
|
|
3
|
+
|
|
4
|
+
const initialLogger =
|
|
5
|
+
process.env.NODE_ENV === "development"
|
|
6
|
+
? (await import("../lib/dev/create-logger.mjs")).default()
|
|
7
|
+
: console;
|
|
8
|
+
export const logger = new Proxy(
|
|
9
|
+
{},
|
|
10
|
+
{
|
|
11
|
+
get: (_, prop) => {
|
|
12
|
+
return (getRuntime(LOGGER_CONTEXT) ?? initialLogger)[prop];
|
|
13
|
+
},
|
|
14
|
+
}
|
|
15
|
+
);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export async function init$(ssrLoadModule) {
|
|
2
|
+
const moduleCache = new Map();
|
|
3
|
+
globalThis.__webpack_require__ = function (id) {
|
|
4
|
+
if (!moduleCache.has(id)) {
|
|
5
|
+
const modulePromise = ssrLoadModule(id);
|
|
6
|
+
modulePromise.then(
|
|
7
|
+
() => {
|
|
8
|
+
modulePromise.value = modulePromise;
|
|
9
|
+
modulePromise.status = "fulfilled";
|
|
10
|
+
},
|
|
11
|
+
(reason) => {
|
|
12
|
+
modulePromise.reason = reason;
|
|
13
|
+
modulePromise.status = "rejected";
|
|
14
|
+
}
|
|
15
|
+
);
|
|
16
|
+
moduleCache.set(id, modulePromise);
|
|
17
|
+
}
|
|
18
|
+
return moduleCache.get(id);
|
|
19
|
+
};
|
|
20
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { getContext } from "@lazarv/react-server/server/context.mjs";
|
|
2
|
+
import {
|
|
3
|
+
HTTP_CONTEXT,
|
|
4
|
+
REDIRECT_CONTEXT,
|
|
5
|
+
} from "@lazarv/react-server/server/symbols.mjs";
|
|
6
|
+
|
|
7
|
+
export class RedirectError extends Error {
|
|
8
|
+
constructor(url, status) {
|
|
9
|
+
super("Redirect");
|
|
10
|
+
this.url = url;
|
|
11
|
+
this.status = status;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function redirect(url, status = 302) {
|
|
16
|
+
const store = getContext(REDIRECT_CONTEXT);
|
|
17
|
+
if (store) {
|
|
18
|
+
const request = getContext(HTTP_CONTEXT).request;
|
|
19
|
+
store.response =
|
|
20
|
+
request.method !== "GET"
|
|
21
|
+
? new Response(
|
|
22
|
+
`<html><head><meta http-equiv="refresh" content="0; url=${url}" /></head></html>`,
|
|
23
|
+
{
|
|
24
|
+
status,
|
|
25
|
+
headers: {
|
|
26
|
+
"content-type": "text/html",
|
|
27
|
+
Location: url,
|
|
28
|
+
},
|
|
29
|
+
}
|
|
30
|
+
)
|
|
31
|
+
: new Response(null, {
|
|
32
|
+
status,
|
|
33
|
+
headers: {
|
|
34
|
+
Location: url,
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
throw new RedirectError(url, status);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function redirect$(handler) {
|
|
43
|
+
const store = getContext(REDIRECT_CONTEXT);
|
|
44
|
+
store.redirectHandlers.push(handler);
|
|
45
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { getContext } from "@lazarv/react-server/server/context.mjs";
|
|
2
|
+
import {
|
|
3
|
+
FORM_DATA_PARSER,
|
|
4
|
+
HTTP_CONTEXT,
|
|
5
|
+
} from "@lazarv/react-server/server/symbols.mjs";
|
|
6
|
+
|
|
7
|
+
export function useUrl() {
|
|
8
|
+
return getContext(HTTP_CONTEXT).url;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function useRequest() {
|
|
12
|
+
return getContext(HTTP_CONTEXT).request;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function useResponse() {
|
|
16
|
+
return getContext(HTTP_CONTEXT).response;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export async function useFormData(handleFile) {
|
|
20
|
+
const request = getContext(HTTP_CONTEXT).request;
|
|
21
|
+
if (request.headers.get("content-type") !== "multipart/form-data") {
|
|
22
|
+
const body = await request.text();
|
|
23
|
+
const params = new URLSearchParams(body);
|
|
24
|
+
const formData = new FormData();
|
|
25
|
+
for (const [name, value] of params.entries()) {
|
|
26
|
+
formData.append(name, value);
|
|
27
|
+
}
|
|
28
|
+
return formData;
|
|
29
|
+
}
|
|
30
|
+
return getContext(FORM_DATA_PARSER)(request, {
|
|
31
|
+
handleFile,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function rewrite(pathname) {
|
|
36
|
+
getContext(HTTP_CONTEXT).url.pathname = pathname;
|
|
37
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { getContext } from "@lazarv/react-server/server/context.mjs";
|
|
2
|
+
|
|
3
|
+
import { useUrl } from "./request.mjs";
|
|
4
|
+
import { CACHE_CONTEXT } from "./symbols.mjs";
|
|
5
|
+
|
|
6
|
+
const revalidateQueue = [];
|
|
7
|
+
|
|
8
|
+
export function revalidate(key) {
|
|
9
|
+
revalidateQueue.push(async () => {
|
|
10
|
+
const url = useUrl();
|
|
11
|
+
const cache = getContext(CACHE_CONTEXT);
|
|
12
|
+
|
|
13
|
+
const keyToDelete = key ?? url;
|
|
14
|
+
await cache.delete(keyToDelete);
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function init$() {
|
|
19
|
+
while (revalidateQueue.length > 0) {
|
|
20
|
+
await revalidateQueue.shift().call(null);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { useActionState } from "@lazarv/react-server/client/ActionState.mjs";
|
|
2
|
+
import ClientOnly from "@lazarv/react-server/client/ClientOnly.jsx";
|
|
3
|
+
import { useParams } from "@lazarv/react-server/client/Params.mjs";
|
|
4
|
+
|
|
5
|
+
import Route, { useMatch } from "./Route.jsx";
|
|
6
|
+
export { Route, ClientOnly, useMatch, useParams, useActionState };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
|
|
3
|
+
export const RuntimeContextStorage = (globalThis.__react_server_runtime__ =
|
|
4
|
+
globalThis.__react_server_runtime__ || new AsyncLocalStorage());
|
|
5
|
+
|
|
6
|
+
export function getRuntime(type) {
|
|
7
|
+
const store = RuntimeContextStorage.getStore();
|
|
8
|
+
if (!type) return store;
|
|
9
|
+
return store?.[type];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @template T
|
|
14
|
+
* @param {string | Symbol} type
|
|
15
|
+
* @param {T} context
|
|
16
|
+
* */
|
|
17
|
+
export function runtime$(type, context) {
|
|
18
|
+
const store = RuntimeContextStorage.getStore();
|
|
19
|
+
const delta = typeof type === "object" ? type : { [type]: context };
|
|
20
|
+
Reflect.ownKeys(delta).forEach((type) => {
|
|
21
|
+
store[type] = delta[type];
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function init$(callback) {
|
|
26
|
+
return new Promise((resolve) => {
|
|
27
|
+
RuntimeContextStorage.run({}, async () => {
|
|
28
|
+
await callback();
|
|
29
|
+
resolve();
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export const LOGGER_CONTEXT = Symbol.for("LOGGER_CONTEXT");
|
|
2
|
+
export const FORM_DATA_PARSER = Symbol.for("FORM_DATA_PARSER");
|
|
3
|
+
export const MAIN_MODULE = Symbol.for("MAIN_MODULE");
|
|
4
|
+
export const SERVER_CONTEXT = Symbol.for("SERVER_CONTEXT");
|
|
5
|
+
export const COLLECT_STYLESHEETS = Symbol.for("COLLECT_STYLESHEETS");
|
|
6
|
+
export const HTTP_CONTEXT = Symbol.for("HTTP_CONTEXT");
|
|
7
|
+
export const HTTP_STATUS = Symbol.for("HTTP_STATUS");
|
|
8
|
+
export const HTTP_HEADERS = Symbol.for("HTTP_HEADERS");
|
|
9
|
+
export const REDIRECT_CONTEXT = Symbol.for("REDIRECT_CONTEXT");
|
|
10
|
+
export const MODULE_LOADER = Symbol.for("MODULE_LOADER");
|
|
11
|
+
export const ERROR_CONTEXT = Symbol.for("ERROR_CONTEXT");
|
|
12
|
+
export const MEMORY_CACHE_CONTEXT = Symbol.for("MEMORY_CACHE_CONTEXT");
|
|
13
|
+
export const CACHE_CONTEXT = Symbol.for("CACHE_CONTEXT");
|
|
14
|
+
export const FLIGHT_CACHE = Symbol.for("FLIGHT_CACHE");
|
|
15
|
+
export const HTML_CACHE = Symbol.for("HTML_CACHE");
|
|
16
|
+
export const OUTLET_CACHE = Symbol.for("OUTLET_CACHE");
|
|
17
|
+
export const CONFIG_ROOT = Symbol.for("CONFIG_ROOT");
|
|
18
|
+
export const CONFIG_CONTEXT = Symbol.for("CONFIG_CONTEXT");
|
|
19
|
+
export const CONFIG_PARENT = Symbol.for("CONFIG_PARENT");
|
|
20
|
+
export const MANIFEST = Symbol.for("MANIFEST");
|
|
21
|
+
export const CLIENT_COMPONENTS = Symbol.for("CLIENT_COMPONENTS");
|
|
22
|
+
export const SSR_CONTROLLER = Symbol.for("SSR_CONTROLLER");
|
|
23
|
+
export const ROUTE_MATCH = Symbol.for("ROUTE_MATCH");
|
|
24
|
+
export const STYLES_CONTEXT = Symbol.for("STYLES_CONTEXT");
|