one
Version:
One is a new React Framework that makes Vite serve both native and web.
95 lines (94 loc) • 3.17 kB
JavaScript
import { AsyncLocalStorage } from "node:async_hooks";
import { SERVER_CONTEXT_KEY } from "../constants";
const key = "__vxrnrequestAsyncLocalStore", read = () => globalThis[key], ASYNC_LOCAL_STORE = {
get current() {
if (read()) return read();
const _ = new AsyncLocalStorage();
return globalThis[key] = _, _;
}
}, requestAsyncLocalStore = process.env.VITE_ENVIRONMENT === "ssr" ? ASYNC_LOCAL_STORE.current : null, newCache = /* @__PURE__ */ new WeakMap(), asyncHeadersCache = globalThis.__vxrnasyncHeadersCache ?? newCache;
globalThis.__vxrnasyncHeadersCache ||= asyncHeadersCache;
async function runWithAsyncLocalContext(cb) {
const id = { _id: Math.random() };
let out = null;
return await ASYNC_LOCAL_STORE.current.run(id, async () => {
out = await cb(id);
}), out;
}
async function setResponseHeaders(cb) {
const id = ensureAsyncLocalID(), headers = asyncHeadersCache.get(id) ?? new Headers();
asyncHeadersCache.set(id, headers), cb(headers);
}
function mergeHeaders(onto, from) {
from.forEach((value, key2) => {
value === void 0 || value === "undefined" ? onto.delete(key2) : onto.append(key2, value);
});
}
const globalId = { _id: Math.random() };
function ensureAsyncLocalID() {
const id = process.env.VERCEL ? globalId : requestAsyncLocalStore?.getStore();
if (!id)
throw new Error("Internal One error, no AsyncLocalStorage id!");
return id;
}
const serverContexts = /* @__PURE__ */ new WeakMap();
function setServerContext(data) {
if (process.env.VITE_ENVIRONMENT === "ssr") {
const id = ensureAsyncLocalID();
serverContexts.has(id) || serverContexts.set(id, {});
const context = serverContexts.get(id);
Object.assign(context, data);
} else
throw new Error("Don't call setServerContext on client");
}
function getServerContext() {
return (() => {
if (process.env.VITE_ENVIRONMENT === "ssr") {
const id = ensureAsyncLocalID();
return serverContexts.get(id);
}
return globalThis[SERVER_CONTEXT_KEY];
})();
}
function useServerContext() {
if (process.env.VITE_ENVIRONMENT === "ssr")
try {
const useContext = globalThis.__vxrnGetContextFromReactContext;
if (useContext)
return serverContexts.get(useContext());
} 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.js.map