@clerk/express
Version:
Clerk server SDK for usage with Express
87 lines (85 loc) • 3.46 kB
JavaScript
import { isTruthy } from "@clerk/shared/underscore";
import { Readable } from "stream";
//#region src/utils.ts
/**
* Brand attached to the `req.auth` handler installed by Clerk middleware.
* `req.auth` is a property name shared with other auth libraries (express-jwt,
* passport, express-openid-connect), so its mere presence proves nothing about
* whether Clerk authenticated the request. `Symbol.for` registers the brand
* globally so it stays stable across multiple copies of this package loaded in
* the same process.
*/
const clerkAuthBrand = Symbol.for("@clerk/express.auth");
const brandRequestAuth = (authHandler) => Object.assign(authHandler, { [clerkAuthBrand]: true });
const requestHasAuthObject = (req) => {
const auth = req.auth;
return typeof auth === "function" && auth[clerkAuthBrand] === true;
};
const loadClientEnv = () => {
return {
publishableKey: process.env.CLERK_PUBLISHABLE_KEY || "",
__internal_clerkJSUrl: process.env.CLERK_JS || process.env.CLERK_JS_URL || "",
__internal_clerkJSVersion: process.env.CLERK_JS_VERSION || "",
__internal_clerkUIUrl: process.env.CLERK_UI_URL || "",
__internal_clerkUIVersion: process.env.CLERK_UI_VERSION || "",
prefetchUI: process.env.CLERK_PREFETCH_UI === "false" ? false : void 0
};
};
const loadApiEnv = () => {
return {
secretKey: process.env.CLERK_SECRET_KEY || "",
machineSecretKey: process.env.CLERK_MACHINE_SECRET_KEY || "",
apiUrl: process.env.CLERK_API_URL || "https://api.clerk.com",
apiVersion: process.env.CLERK_API_VERSION || "v1",
domain: process.env.CLERK_DOMAIN || "",
proxyUrl: process.env.CLERK_PROXY_URL || "",
signInUrl: process.env.CLERK_SIGN_IN_URL || "",
isSatellite: isTruthy(process.env.CLERK_IS_SATELLITE),
jwtKey: process.env.CLERK_JWT_KEY || "",
sdkMetadata: {
name: "@clerk/express",
version: "2.1.40",
environment: process.env.NODE_ENV
},
telemetry: {
disabled: isTruthy(process.env.CLERK_TELEMETRY_DISABLED),
debug: isTruthy(process.env.CLERK_TELEMETRY_DEBUG)
}
};
};
const incomingMessageToRequest = (req) => {
const headers = Object.keys(req.headers).reduce((acc, key) => Object.assign(acc, { [key]: req?.headers[key] }), {});
const protocol = req.connection?.encrypted ? "https" : "http";
const dummyOriginReqUrl = new URL(req.originalUrl || req.url || "", `${protocol}://clerk-dummy`);
return new Request(dummyOriginReqUrl, {
method: req.method,
headers: new Headers(headers)
});
};
/**
* Converts an Express request to a Fetch API Request with body streaming support.
* This is used for proxying requests where the body needs to be forwarded.
*/
const requestToProxyRequest = (req) => {
const headers = new Headers();
Object.entries(req.headers).forEach(([key, value]) => {
if (value) headers.set(key, Array.isArray(value) ? value.join(", ") : value);
});
const protocol = req.protocol || (req.secure ? "https" : "http");
const host = req.get("host") || "localhost";
const url = new URL(req.originalUrl || req.url, `${protocol}://${host}`);
const hasBody = [
"POST",
"PUT",
"PATCH"
].includes(req.method);
return new Request(url.toString(), {
method: req.method,
headers,
body: hasBody ? Readable.toWeb(req) : void 0,
duplex: hasBody ? "half" : void 0
});
};
//#endregion
export { requestHasAuthObject as a, loadClientEnv as i, incomingMessageToRequest as n, requestToProxyRequest as o, loadApiEnv as r, brandRequestAuth as t };
//# sourceMappingURL=utils-DtX1zZA7.mjs.map