@tanstack/start-server-core
Version:
Modern and scalable routing for React applications
218 lines (217 loc) • 7.75 kB
JavaScript
import { AsyncLocalStorage } from "node:async_hooks";
import { H3Event, clearSession, deleteCookie, getRequestHost, getRequestIP, getRequestProtocol, getRequestURL, getSession, getValidatedQuery, parseCookies, sanitizeStatusCode, sanitizeStatusMessage, sealSession, setCookie, toResponse, unsealSession, updateSession, useSession } from "h3-v2";
//#region src/request-response.ts
var GLOBAL_EVENT_STORAGE_KEY = Symbol.for("tanstack-start:event-storage");
var globalObj = globalThis;
if (!globalObj[GLOBAL_EVENT_STORAGE_KEY]) globalObj[GLOBAL_EVENT_STORAGE_KEY] = new AsyncLocalStorage();
var eventStorage = globalObj[GLOBAL_EVENT_STORAGE_KEY];
function isPromiseLike(value) {
return typeof value.then === "function";
}
function getSetCookieValues(headers) {
const headersWithSetCookie = headers;
if (typeof headersWithSetCookie.getSetCookie === "function") return headersWithSetCookie.getSetCookie();
const value = headers.get("set-cookie");
return value ? [value] : [];
}
function mergeEventResponseHeaders(response, event) {
if (response.ok) return;
const eventSetCookies = getSetCookieValues(event.res.headers);
if (eventSetCookies.length === 0) return;
const responseSetCookies = getSetCookieValues(response.headers);
response.headers.delete("set-cookie");
for (const cookie of responseSetCookies) response.headers.append("set-cookie", cookie);
for (const cookie of eventSetCookies) response.headers.append("set-cookie", cookie);
}
function attachResponseHeaders(value, event) {
if (isPromiseLike(value)) return value.then((resolved) => {
if (resolved instanceof Response) mergeEventResponseHeaders(resolved, event);
return resolved;
});
if (value instanceof Response) mergeEventResponseHeaders(value, event);
return value;
}
function requestHandler(handler) {
return (request, requestOpts) => {
const h3Event = new H3Event(request);
return toResponse(attachResponseHeaders(eventStorage.run({ h3Event }, () => handler(request, requestOpts)), h3Event), h3Event);
};
}
function getH3Event() {
const event = eventStorage.getStore();
if (!event) throw new Error(`No StartEvent found in AsyncLocalStorage. Make sure you are using the function within the server runtime.`);
return event.h3Event;
}
function getRequest() {
return getH3Event().req;
}
function getRequestHeaders() {
return getH3Event().req.headers;
}
function getRequestHeader(name) {
return getRequestHeaders().get(name) || void 0;
}
function getRequestIP$1(opts) {
return getRequestIP(getH3Event(), opts);
}
/**
* Get the request hostname.
*
* If `xForwardedHost` is `true`, it will use the `x-forwarded-host` header if it exists.
*
* If no host header is found, it will default to "localhost".
*/
function getRequestHost$1(opts) {
return getRequestHost(getH3Event(), opts);
}
/**
* Get the full incoming request URL.
*
* If `xForwardedHost` is `true`, it will use the `x-forwarded-host` header if it exists.
*
* If `xForwardedProto` is `false`, it will not use the `x-forwarded-proto` header.
*/
function getRequestUrl(opts) {
return getRequestURL(getH3Event(), opts);
}
/**
* Get the request protocol.
*
* If `x-forwarded-proto` header is set to "https", it will return "https". You can disable this behavior by setting `xForwardedProto` to `false`.
*
* If protocol cannot be determined, it will default to "http".
*/
function getRequestProtocol$1(opts) {
return getRequestProtocol(getH3Event(), opts);
}
function setResponseHeaders(headers) {
const event = getH3Event();
for (const [name, value] of Object.entries(headers)) event.res.headers.set(name, value);
}
function getResponseHeaders() {
return getH3Event().res.headers;
}
function getResponseHeader(name) {
return getH3Event().res.headers.get(name) || void 0;
}
function setResponseHeader(name, value) {
const event = getH3Event();
if (Array.isArray(value)) {
event.res.headers.delete(name);
for (const valueItem of value) event.res.headers.append(name, valueItem);
} else event.res.headers.set(name, value);
}
function removeResponseHeader(name) {
getH3Event().res.headers.delete(name);
}
function clearResponseHeaders(headerNames) {
const event = getH3Event();
if (headerNames && headerNames.length > 0) for (const name of headerNames) event.res.headers.delete(name);
else for (const name of event.res.headers.keys()) event.res.headers.delete(name);
}
function getResponseStatus() {
return getH3Event().res.status || 200;
}
function setResponseStatus(code, text) {
const event = getH3Event();
if (code) event.res.status = sanitizeStatusCode(code, event.res.status);
if (text) event.res.statusText = sanitizeStatusMessage(text);
}
/**
* Parse the request to get HTTP Cookie header string and return an object of all cookie name-value pairs.
* @returns Object of cookie name-value pairs
* ```ts
* const cookies = getCookies()
* ```
*/
function getCookies() {
return parseCookies(getH3Event());
}
/**
* Get a cookie value by name.
* @param name Name of the cookie to get
* @returns {*} Value of the cookie (String or undefined)
* ```ts
* const authorization = getCookie('Authorization')
* ```
*/
function getCookie(name) {
return getCookies()[name] || void 0;
}
/**
* Set a cookie value by name.
* @param name Name of the cookie to set
* @param value Value of the cookie to set
* @param options {CookieSerializeOptions} Options for serializing the cookie
* ```ts
* setCookie('Authorization', '1234567')
* ```
*/
function setCookie$1(name, value, options) {
setCookie(getH3Event(), name, value, options);
}
/**
* Remove a cookie by name.
* @param name Name of the cookie to delete
* @param serializeOptions {CookieSerializeOptions} Cookie options
* ```ts
* deleteCookie('SessionId')
* ```
*/
function deleteCookie$1(name, options) {
deleteCookie(getH3Event(), name, options);
}
function getDefaultSessionConfig(config) {
return {
name: "start",
...config
};
}
/**
* Create a session manager for the current request.
*/
function useSession$1(config) {
return useSession(getH3Event(), getDefaultSessionConfig(config));
}
/**
* Get the session for the current request
*/
function getSession$1(config) {
return getSession(getH3Event(), getDefaultSessionConfig(config));
}
/**
* Update the session data for the current request.
*/
function updateSession$1(config, update) {
return updateSession(getH3Event(), getDefaultSessionConfig(config), update);
}
/**
* Encrypt and sign the session data for the current request.
*/
function sealSession$1(config) {
return sealSession(getH3Event(), getDefaultSessionConfig(config));
}
/**
* Decrypt and verify the session data for the current request.
*/
function unsealSession$1(config, sealed) {
return unsealSession(getH3Event(), getDefaultSessionConfig(config), sealed);
}
/**
* Clear the session data for the current request.
*/
function clearSession$1(config) {
return clearSession(getH3Event(), {
name: "start",
...config
});
}
function getResponse() {
return getH3Event().res;
}
function getValidatedQuery$1(schema) {
return getValidatedQuery(getH3Event(), schema);
}
//#endregion
export { clearResponseHeaders, clearSession$1 as clearSession, deleteCookie$1 as deleteCookie, getCookie, getCookies, getRequest, getRequestHeader, getRequestHeaders, getRequestHost$1 as getRequestHost, getRequestIP$1 as getRequestIP, getRequestProtocol$1 as getRequestProtocol, getRequestUrl, getResponse, getResponseHeader, getResponseHeaders, getResponseStatus, getSession$1 as getSession, getValidatedQuery$1 as getValidatedQuery, removeResponseHeader, requestHandler, sealSession$1 as sealSession, setCookie$1 as setCookie, setResponseHeader, setResponseHeaders, setResponseStatus, unsealSession$1 as unsealSession, updateSession$1 as updateSession, useSession$1 as useSession };
//# sourceMappingURL=request-response.js.map