one
Version:
One is a new React Framework that makes Vite serve both native and web.
135 lines (134 loc) • 4.2 kB
JavaScript
import { SERVER_CONTEXT_KEY } from "../constants.mjs";
import { AsyncLocalStorage } from "node:async_hooks";
const _ctxKey = Symbol.for("__oneCtx");
const key = "__vxrnrequestAsyncLocalStore";
const read = () => globalThis[key];
const ASYNC_LOCAL_STORE = {
get current() {
if (read()) return read();
const _ = new AsyncLocalStorage();
globalThis[key] = _;
return _;
}
};
const requestAsyncLocalStore = process.env.VITE_ENVIRONMENT === "ssr" ? ASYNC_LOCAL_STORE.current : null;
const newCache = /* @__PURE__ */new WeakMap();
const asyncHeadersCache = globalThis["__vxrnasyncHeadersCache"] ?? newCache;
globalThis["__vxrnasyncHeadersCache"] ||= asyncHeadersCache;
async function runWithAsyncLocalContext(cb) {
const id = {
_id: Math.random()
};
const store = ASYNC_LOCAL_STORE.current;
if (!store) {
return cb(id);
}
let out = null;
await store.run(id, async () => {
out = await cb(id);
});
return out;
}
async function setResponseHeaders(cb) {
const id = ensureAsyncLocalID();
const cache = globalThis["__vxrnasyncHeadersCache"] ?? asyncHeadersCache;
const headers = cache.get(id) ?? new Headers();
cache.set(id, headers);
cb(headers);
}
function mergeHeaders(onto, from) {
const setCookies = from.getSetCookie?.();
if (setCookies?.length) {
for (const cookie of setCookies) {
onto.append("set-cookie", cookie);
}
}
from.forEach((value, key2) => {
if (key2 === "set-cookie") return;
if (value === void 0 || value === "undefined") {
onto.delete(key2);
} else {
onto.set(key2, value);
}
});
}
const globalId = {
_id: Math.random()
};
const GLOBAL_ID_KEY = "__oneGlobalContextId";
if (!globalThis[GLOBAL_ID_KEY]) {
globalThis[GLOBAL_ID_KEY] = globalId;
}
function ensureAsyncLocalID() {
const globalIdKey = GLOBAL_ID_KEY || "__oneGlobalContextId";
const store = requestAsyncLocalStore ?? globalThis["__vxrnrequestAsyncLocalStore"];
const id = process.env.VERCEL ? globalThis[globalIdKey] : store?.getStore();
if (!id) {
throw new Error(`Internal One error, no AsyncLocalStorage id!`);
}
return id;
}
const SERVER_CONTEXTS_KEY = "__oneServerContexts";
if (!globalThis[SERVER_CONTEXTS_KEY]) {
globalThis[SERVER_CONTEXTS_KEY] = /* @__PURE__ */new WeakMap();
}
const serverContexts = globalThis[SERVER_CONTEXTS_KEY];
function setServerContext(data) {
if (process.env.VITE_ENVIRONMENT === "ssr") {
const id = ensureAsyncLocalID();
let context = id[_ctxKey];
if (!context) {
context = {};
id[_ctxKey] = context;
serverContexts.set(id, context);
}
Object.assign(context, data);
} else {
throw new Error(`Don't call setServerContext on client`);
}
}
function getServerContext() {
const out = (() => {
if (process.env.VITE_ENVIRONMENT === "ssr") {
const id = ensureAsyncLocalID();
return id[_ctxKey] || serverContexts.get(id);
}
return globalThis[SERVER_CONTEXT_KEY];
})();
return out;
}
function useServerContext() {
if (process.env.VITE_ENVIRONMENT === "ssr") {
try {
const useContext = globalThis["__vxrnGetContextFromReactContext"];
if (useContext) {
const id = useContext();
if (id) return id[_ctxKey] || serverContexts.get(id);
}
} catch {}
}
return getServerContext();
}
function setServerData(key2, value) {
if (process.env.VITE_ENVIRONMENT === "ssr") {
const context = getServerContext();
setServerContext({
postRenderData: {
...context?.postRenderData,
[key2]: value
}
});
} else {
throw new Error(`Cannot setServerData in ${process.env.VITE_ENVIRONMENT} environment!`);
}
}
function getServerData(key2) {
if (process.env.VITE_ENVIRONMENT === "ssr") {
throw new Error(`Cannot getServerData on the server`);
}
if (process.env.VITE_ENVIRONMENT !== "ssr") {
return getServerContext()?.postRenderData?.[key2];
}
}
export { asyncHeadersCache, ensureAsyncLocalID, getServerContext, getServerData, mergeHeaders, requestAsyncLocalStore, runWithAsyncLocalContext, setResponseHeaders, setServerContext, setServerData, useServerContext };
//# sourceMappingURL=one-server-only.mjs.map