@clerk/express
Version:
Clerk server SDK for usage with Express
122 lines (120 loc) • 4.22 kB
JavaScript
let _clerk_shared_underscore = require("@clerk/shared/underscore");
let stream = require("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: (0, _clerk_shared_underscore.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: (0, _clerk_shared_underscore.isTruthy)(process.env.CLERK_TELEMETRY_DISABLED),
debug: (0, _clerk_shared_underscore.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 ? stream.Readable.toWeb(req) : void 0,
duplex: hasBody ? "half" : void 0
});
};
//#endregion
Object.defineProperty(exports, 'brandRequestAuth', {
enumerable: true,
get: function () {
return brandRequestAuth;
}
});
Object.defineProperty(exports, 'incomingMessageToRequest', {
enumerable: true,
get: function () {
return incomingMessageToRequest;
}
});
Object.defineProperty(exports, 'loadApiEnv', {
enumerable: true,
get: function () {
return loadApiEnv;
}
});
Object.defineProperty(exports, 'loadClientEnv', {
enumerable: true,
get: function () {
return loadClientEnv;
}
});
Object.defineProperty(exports, 'requestHasAuthObject', {
enumerable: true,
get: function () {
return requestHasAuthObject;
}
});
Object.defineProperty(exports, 'requestToProxyRequest', {
enumerable: true,
get: function () {
return requestToProxyRequest;
}
});
//# sourceMappingURL=utils-ChU4G25X.js.map