@clerk/fastify 3.1.27-snapshot.v20260515191208 → 3.1.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{index-ogSMFkTC.d.mts → index-CWnSaB0B.d.mts} +0 -8
- package/dist/{index-ogSMFkTC.d.ts → index-CWnSaB0B.d.ts} +0 -8
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +7 -10
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +7 -10
- package/dist/index.mjs.map +1 -1
- package/dist/types.d.mts +1 -1
- package/dist/types.d.ts +1 -1
- package/package.json +3 -3
|
@@ -20,14 +20,6 @@ interface FrontendApiProxyOptions {
|
|
|
20
20
|
type ClerkFastifyOptions = ClerkOptions & {
|
|
21
21
|
hookName?: (typeof ALLOWED_HOOKS)[number];
|
|
22
22
|
frontendApiProxy?: FrontendApiProxyOptions;
|
|
23
|
-
/**
|
|
24
|
-
* Whether to enable the handshake flow for session verification.
|
|
25
|
-
* Disable this when using Clerk with a first-party API backend (e.g. a SPA calling
|
|
26
|
-
* a Fastify server) to prevent handshake nonce cookies set during OAuth callbacks
|
|
27
|
-
* from blocking authentication on subsequent API requests.
|
|
28
|
-
* @default true
|
|
29
|
-
*/
|
|
30
|
-
enableHandshake?: boolean;
|
|
31
23
|
};
|
|
32
24
|
|
|
33
25
|
export type { ClerkFastifyOptions as C, FrontendApiProxyOptions as F };
|
|
@@ -20,14 +20,6 @@ interface FrontendApiProxyOptions {
|
|
|
20
20
|
type ClerkFastifyOptions = ClerkOptions & {
|
|
21
21
|
hookName?: (typeof ALLOWED_HOOKS)[number];
|
|
22
22
|
frontendApiProxy?: FrontendApiProxyOptions;
|
|
23
|
-
/**
|
|
24
|
-
* Whether to enable the handshake flow for session verification.
|
|
25
|
-
* Disable this when using Clerk with a first-party API backend (e.g. a SPA calling
|
|
26
|
-
* a Fastify server) to prevent handshake nonce cookies set during OAuth callbacks
|
|
27
|
-
* from blocking authentication on subsequent API requests.
|
|
28
|
-
* @default true
|
|
29
|
-
*/
|
|
30
|
-
enableHandshake?: boolean;
|
|
31
23
|
};
|
|
32
24
|
|
|
33
25
|
export type { ClerkFastifyOptions as C, FrontendApiProxyOptions as F };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as _clerk_backend from '@clerk/backend';
|
|
2
2
|
export * from '@clerk/backend';
|
|
3
|
-
import { C as ClerkFastifyOptions } from './index-
|
|
4
|
-
export { F as FrontendApiProxyOptions } from './index-
|
|
3
|
+
import { C as ClerkFastifyOptions } from './index-CWnSaB0B.mjs';
|
|
4
|
+
export { F as FrontendApiProxyOptions } from './index-CWnSaB0B.mjs';
|
|
5
5
|
import { FastifyPluginCallback, FastifyRequest } from 'fastify';
|
|
6
6
|
import { GetAuthFn } from '@clerk/backend/internal';
|
|
7
7
|
import '@clerk/shared/types';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as _clerk_backend from '@clerk/backend';
|
|
2
2
|
export * from '@clerk/backend';
|
|
3
|
-
import { C as ClerkFastifyOptions } from './index-
|
|
4
|
-
export { F as FrontendApiProxyOptions } from './index-
|
|
3
|
+
import { C as ClerkFastifyOptions } from './index-CWnSaB0B.js';
|
|
4
|
+
export { F as FrontendApiProxyOptions } from './index-CWnSaB0B.js';
|
|
5
5
|
import { FastifyPluginCallback, FastifyRequest } from 'fastify';
|
|
6
6
|
import { GetAuthFn } from '@clerk/backend/internal';
|
|
7
7
|
import '@clerk/shared/types';
|
package/dist/index.js
CHANGED
|
@@ -63,7 +63,7 @@ var API_URL = process.env.CLERK_API_URL || (0, import_apiUrlFromPublishableKey.a
|
|
|
63
63
|
var JWT_KEY = process.env.CLERK_JWT_KEY || "";
|
|
64
64
|
var SDK_METADATA = {
|
|
65
65
|
name: "@clerk/fastify",
|
|
66
|
-
version: "3.1.27
|
|
66
|
+
version: "3.1.27",
|
|
67
67
|
environment: "production"
|
|
68
68
|
};
|
|
69
69
|
var { Cookies, Headers: Headers2 } = import_internal.constants;
|
|
@@ -75,7 +75,7 @@ var clerkClient = (0, import_backend.createClerkClient)({
|
|
|
75
75
|
apiUrl: API_URL,
|
|
76
76
|
apiVersion: API_VERSION,
|
|
77
77
|
jwtKey: JWT_KEY,
|
|
78
|
-
userAgent: `${"@clerk/fastify"}@${"3.1.27
|
|
78
|
+
userAgent: `${"@clerk/fastify"}@${"3.1.27"}`,
|
|
79
79
|
sdkMetadata: SDK_METADATA
|
|
80
80
|
});
|
|
81
81
|
|
|
@@ -131,7 +131,6 @@ var requestToProxyRequest = (req) => {
|
|
|
131
131
|
var withClerkMiddleware = (options) => {
|
|
132
132
|
const frontendApiProxy = options.frontendApiProxy;
|
|
133
133
|
const proxyPath = (0, import_proxy.stripTrailingSlashes)(frontendApiProxy?.path ?? import_proxy.DEFAULT_PROXY_PATH) || import_proxy.DEFAULT_PROXY_PATH;
|
|
134
|
-
const enableHandshake = options.enableHandshake ?? true;
|
|
135
134
|
return async (fastifyRequest, reply) => {
|
|
136
135
|
const publishableKey = options.publishableKey || PUBLISHABLE_KEY;
|
|
137
136
|
const secretKey = options.secretKey || SECRET_KEY;
|
|
@@ -188,13 +187,11 @@ var withClerkMiddleware = (options) => {
|
|
|
188
187
|
acceptsToken: "any"
|
|
189
188
|
});
|
|
190
189
|
requestState.headers.forEach((value, key) => reply.header(key, value));
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
throw new Error("Clerk: handshake status without redirect");
|
|
197
|
-
}
|
|
190
|
+
const locationHeader = requestState.headers.get(Headers2.Location);
|
|
191
|
+
if (locationHeader) {
|
|
192
|
+
return reply.code(307).send();
|
|
193
|
+
} else if (requestState.status === import_internal2.AuthStatus.Handshake) {
|
|
194
|
+
throw new Error("Clerk: handshake status without redirect");
|
|
198
195
|
}
|
|
199
196
|
fastifyRequest.auth = requestState.toAuth();
|
|
200
197
|
};
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/clerkPlugin.ts","../src/types.ts","../src/withClerkMiddleware.ts","../src/clerkClient.ts","../src/constants.ts","../src/utils.ts","../src/getAuth.ts","../src/errors.ts"],"sourcesContent":["export * from '@clerk/backend';\n\nexport type { ClerkFastifyOptions, FrontendApiProxyOptions } from './types';\n\nexport { clerkPlugin } from './clerkPlugin';\nexport { getAuth } from './getAuth';\nexport { clerkClient } from './clerkClient';\n","import type { FastifyInstance, FastifyPluginCallback } from 'fastify';\nimport fp from 'fastify-plugin';\n\nimport type { ClerkFastifyOptions } from './types';\nimport { ALLOWED_HOOKS } from './types';\nimport { withClerkMiddleware } from './withClerkMiddleware';\n\nconst plugin: FastifyPluginCallback<ClerkFastifyOptions> = (\n instance: FastifyInstance,\n opts: ClerkFastifyOptions,\n done,\n) => {\n instance.decorateRequest('auth', null);\n // run clerk as a middleware to all scoped routes\n const hookName = opts.hookName || 'preHandler';\n if (!ALLOWED_HOOKS.includes(hookName)) {\n throw new Error(`Unsupported hookName: ${hookName}`);\n }\n\n instance.addHook(hookName, withClerkMiddleware(opts));\n\n done();\n};\n\nexport const clerkPlugin = fp(plugin, {\n name: '@clerk/fastify',\n fastify: '5.x',\n});\n","import type { ClerkOptions } from '@clerk/backend';\nimport type { ShouldProxyFn } from '@clerk/shared/proxy';\n\nexport const ALLOWED_HOOKS = ['onRequest', 'preHandler'] as const;\n\n/**\n * Options for the built-in Frontend API proxy.\n *\n * When enabled, the middleware intercepts requests that match the proxy path\n * (default `/__clerk`) and forwards them to the Clerk Frontend API, allowing\n * the Clerk frontend SDKs to communicate with Clerk without third-party\n * cookie or ad-blocker issues.\n */\nexport interface FrontendApiProxyOptions {\n /** Toggle the proxy on/off, or supply a function that decides per-request. */\n enabled: boolean | ShouldProxyFn;\n /** Custom path prefix for the proxy (default: `/__clerk`). */\n path?: string;\n}\n\nexport type ClerkFastifyOptions = ClerkOptions & {\n hookName?: (typeof ALLOWED_HOOKS)[number];\n frontendApiProxy?: FrontendApiProxyOptions;\n /**\n * Whether to enable the handshake flow for session verification.\n * Disable this when using Clerk with a first-party API backend (e.g. a SPA calling\n * a Fastify server) to prevent handshake nonce cookies set during OAuth callbacks\n * from blocking authentication on subsequent API requests.\n * @default true\n */\n enableHandshake?: boolean;\n};\n","import { AuthStatus } from '@clerk/backend/internal';\nimport { clerkFrontendApiProxy, DEFAULT_PROXY_PATH, stripTrailingSlashes } from '@clerk/backend/proxy';\nimport type { FastifyReply, FastifyRequest } from 'fastify';\nimport { Readable } from 'stream';\n\nimport { clerkClient } from './clerkClient';\nimport * as constants from './constants';\nimport type { ClerkFastifyOptions } from './types';\nimport { fastifyRequestToRequest, requestToProxyRequest } from './utils';\n\nexport const withClerkMiddleware = (options: ClerkFastifyOptions) => {\n const frontendApiProxy = options.frontendApiProxy;\n const proxyPath = stripTrailingSlashes(frontendApiProxy?.path ?? DEFAULT_PROXY_PATH) || DEFAULT_PROXY_PATH;\n const enableHandshake = options.enableHandshake ?? true;\n\n return async (fastifyRequest: FastifyRequest, reply: FastifyReply) => {\n const publishableKey = options.publishableKey || constants.PUBLISHABLE_KEY;\n const secretKey = options.secretKey || constants.SECRET_KEY;\n\n // Handle Frontend API proxy requests and auto-derive proxyUrl\n let resolvedProxyUrl = options.proxyUrl;\n if (frontendApiProxy) {\n const requestUrl = new URL(\n fastifyRequest.url,\n `${fastifyRequest.protocol}://${fastifyRequest.hostname || 'localhost'}`,\n );\n const isEnabled =\n typeof frontendApiProxy.enabled === 'function'\n ? frontendApiProxy.enabled(requestUrl)\n : frontendApiProxy.enabled;\n\n if (isEnabled) {\n if (requestUrl.pathname === proxyPath || requestUrl.pathname.startsWith(proxyPath + '/')) {\n const proxyRequest = requestToProxyRequest(fastifyRequest);\n\n const proxyResponse = await clerkFrontendApiProxy(proxyRequest, {\n proxyPath,\n publishableKey,\n secretKey,\n });\n\n reply.code(proxyResponse.status);\n proxyResponse.headers.forEach((value, key) => {\n reply.header(key, value);\n });\n\n if (proxyResponse.body) {\n const reader = proxyResponse.body.getReader();\n const stream = new Readable({\n async read() {\n try {\n const { done, value } = await reader.read();\n if (done) {\n this.push(null);\n } else {\n this.push(Buffer.from(value));\n }\n } catch (error) {\n this.destroy(error instanceof Error ? error : new Error(String(error)));\n }\n },\n });\n return reply.send(stream);\n }\n return reply.send();\n }\n\n // Pass just the path - the backend resolves it against the request's\n // public origin (from x-forwarded-* headers).\n if (!resolvedProxyUrl) {\n resolvedProxyUrl = proxyPath;\n }\n }\n }\n\n const req = fastifyRequestToRequest(fastifyRequest);\n\n const requestState = await clerkClient.authenticateRequest(req, {\n ...options,\n secretKey,\n publishableKey,\n proxyUrl: resolvedProxyUrl,\n acceptsToken: 'any',\n });\n\n requestState.headers.forEach((value, key) => reply.header(key, value));\n\n if (enableHandshake) {\n const locationHeader = requestState.headers.get(constants.Headers.Location);\n if (locationHeader) {\n return reply.code(307).send();\n } else if (requestState.status === AuthStatus.Handshake) {\n throw new Error('Clerk: handshake status without redirect');\n }\n }\n\n // @ts-expect-error Inject auth so getAuth can read it\n fastifyRequest.auth = requestState.toAuth();\n };\n};\n","import { createClerkClient } from '@clerk/backend';\n\nimport { API_URL, API_VERSION, JWT_KEY, MACHINE_SECRET_KEY, SDK_METADATA, SECRET_KEY } from './constants';\n\nexport const clerkClient = createClerkClient({\n secretKey: SECRET_KEY,\n machineSecretKey: MACHINE_SECRET_KEY,\n apiUrl: API_URL,\n apiVersion: API_VERSION,\n jwtKey: JWT_KEY,\n userAgent: `${PACKAGE_NAME}@${PACKAGE_VERSION}`,\n sdkMetadata: SDK_METADATA,\n});\n","import { constants } from '@clerk/backend/internal';\nimport { apiUrlFromPublishableKey } from '@clerk/shared/apiUrlFromPublishableKey';\n\nexport const API_VERSION = process.env.CLERK_API_VERSION || 'v1';\nexport const SECRET_KEY = process.env.CLERK_SECRET_KEY || '';\nexport const MACHINE_SECRET_KEY = process.env.CLERK_MACHINE_SECRET_KEY || '';\nexport const PUBLISHABLE_KEY = process.env.CLERK_PUBLISHABLE_KEY || '';\nexport const API_URL = process.env.CLERK_API_URL || apiUrlFromPublishableKey(PUBLISHABLE_KEY);\nexport const JWT_KEY = process.env.CLERK_JWT_KEY || '';\nexport const SDK_METADATA = {\n name: PACKAGE_NAME,\n version: PACKAGE_VERSION,\n environment: process.env.NODE_ENV,\n};\n\nexport const { Cookies, Headers } = constants;\n","import type { FastifyRequest } from 'fastify';\nimport { Readable } from 'stream';\n\nexport const fastifyRequestToRequest = (req: FastifyRequest): Request => {\n const headers = new Headers(\n Object.keys(req.headers).reduce((acc, key) => {\n const value = req.headers[key];\n if (!value) {\n return acc;\n }\n\n if (typeof value === 'string') {\n acc.set(key, value);\n } else {\n acc.set(key, value.join(','));\n }\n return acc;\n }, new Headers()),\n );\n\n // Making some manual tests it seems that FastifyRequest populates the req protocol / hostname\n // based on the forwarded headers. Nevertheless, we are gonna use a dummy base and the request\n // will be fixed by the internals of the clerk/backend package\n const dummyOriginReqUrl = new URL(req.url || '', `${req.protocol}://clerk-dummy`);\n return new Request(dummyOriginReqUrl, {\n method: req.method,\n headers,\n });\n};\n\n/**\n * Converts a Fastify request to a Fetch API Request with full headers and body streaming,\n * suitable for proxy forwarding.\n */\nexport const requestToProxyRequest = (req: FastifyRequest): Request => {\n const headers = new Headers();\n Object.entries(req.headers).forEach(([key, value]) => {\n if (value) {\n headers.set(key, Array.isArray(value) ? value.join(', ') : value);\n }\n });\n\n const forwardedProto = req.headers['x-forwarded-proto'];\n const protoHeader = Array.isArray(forwardedProto) ? forwardedProto[0] : forwardedProto;\n const proto = (protoHeader || '').split(',')[0].trim();\n const protocol = proto === 'https' || req.protocol === 'https' ? 'https' : 'http';\n\n const forwardedHost = req.headers['x-forwarded-host'];\n const hostHeader = Array.isArray(forwardedHost) ? forwardedHost[0] : forwardedHost;\n const host = (hostHeader || '').split(',')[0].trim() || req.hostname || 'localhost';\n\n const url = new URL(req.url || '', `${protocol}://${host}`);\n\n const hasBody = ['POST', 'PUT', 'PATCH'].includes(req.method);\n\n return new Request(url.toString(), {\n method: req.method,\n headers,\n body: hasBody ? (Readable.toWeb(req.raw) as ReadableStream) : undefined,\n // @ts-expect-error - duplex required for streaming bodies but not in all TS definitions\n duplex: hasBody ? 'half' : undefined,\n });\n};\n","import type { AuthOptions, GetAuthFn, SignedInAuthObject, SignedOutAuthObject } from '@clerk/backend/internal';\nimport { getAuthObjectForAcceptedToken } from '@clerk/backend/internal';\nimport type { FastifyRequest } from 'fastify';\n\nimport { pluginRegistrationRequired } from './errors';\n\nexport const getAuth: GetAuthFn<FastifyRequest> = ((req: FastifyRequest, options?: AuthOptions) => {\n const authReq = req as FastifyRequest & { auth: SignedInAuthObject | SignedOutAuthObject };\n\n if (!authReq.auth) {\n throw new Error(pluginRegistrationRequired);\n }\n\n return getAuthObjectForAcceptedToken({ authObject: authReq.auth, acceptsToken: options?.acceptsToken });\n}) as GetAuthFn<FastifyRequest>;\n","// https://github.com/clerk/javascript/blob/main/packages/remix/src/errors.ts#L1-L0\nconst createErrorMessage = (msg: string) => {\n return `🔒 Clerk: ${msg.trim()}\n\nFor more info, check out the docs: https://clerk.com/docs,\nor come say hi in our discord server: https://clerk.com/discord\n`;\n};\n\nexport const pluginRegistrationRequired =\n createErrorMessage(`The \"clerkPlugin\" should be registered before using the \"getAuth\".\nExample:\n\nimport { clerkPlugin } from '@clerk/fastify';\n\nconst server: FastifyInstance = Fastify({ logger: true });\nserver.register(clerkPlugin);\n`);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wBAAc,2BAAd;;;ACCA,4BAAe;;;ACER,IAAM,gBAAgB,CAAC,aAAa,YAAY;;;ACHvD,IAAAA,mBAA2B;AAC3B,mBAAgF;AAEhF,IAAAC,iBAAyB;;;ACHzB,qBAAkC;;;ACAlC,sBAA0B;AAC1B,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Y,QAAQ,MAAM,cAAc,SAAS,aAAa,CAAC;AACxG;","names":["import_internal","import_stream","Headers","Headers","fp","import_internal"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/clerkPlugin.ts","../src/types.ts","../src/withClerkMiddleware.ts","../src/clerkClient.ts","../src/constants.ts","../src/utils.ts","../src/getAuth.ts","../src/errors.ts"],"sourcesContent":["export * from '@clerk/backend';\n\nexport type { ClerkFastifyOptions, FrontendApiProxyOptions } from './types';\n\nexport { clerkPlugin } from './clerkPlugin';\nexport { getAuth } from './getAuth';\nexport { clerkClient } from './clerkClient';\n","import type { FastifyInstance, FastifyPluginCallback } from 'fastify';\nimport fp from 'fastify-plugin';\n\nimport type { ClerkFastifyOptions } from './types';\nimport { ALLOWED_HOOKS } from './types';\nimport { withClerkMiddleware } from './withClerkMiddleware';\n\nconst plugin: FastifyPluginCallback<ClerkFastifyOptions> = (\n instance: FastifyInstance,\n opts: ClerkFastifyOptions,\n done,\n) => {\n instance.decorateRequest('auth', null);\n // run clerk as a middleware to all scoped routes\n const hookName = opts.hookName || 'preHandler';\n if (!ALLOWED_HOOKS.includes(hookName)) {\n throw new Error(`Unsupported hookName: ${hookName}`);\n }\n\n instance.addHook(hookName, withClerkMiddleware(opts));\n\n done();\n};\n\nexport const clerkPlugin = fp(plugin, {\n name: '@clerk/fastify',\n fastify: '5.x',\n});\n","import type { ClerkOptions } from '@clerk/backend';\nimport type { ShouldProxyFn } from '@clerk/shared/proxy';\n\nexport const ALLOWED_HOOKS = ['onRequest', 'preHandler'] as const;\n\n/**\n * Options for the built-in Frontend API proxy.\n *\n * When enabled, the middleware intercepts requests that match the proxy path\n * (default `/__clerk`) and forwards them to the Clerk Frontend API, allowing\n * the Clerk frontend SDKs to communicate with Clerk without third-party\n * cookie or ad-blocker issues.\n */\nexport interface FrontendApiProxyOptions {\n /** Toggle the proxy on/off, or supply a function that decides per-request. */\n enabled: boolean | ShouldProxyFn;\n /** Custom path prefix for the proxy (default: `/__clerk`). */\n path?: string;\n}\n\nexport type ClerkFastifyOptions = ClerkOptions & {\n hookName?: (typeof ALLOWED_HOOKS)[number];\n frontendApiProxy?: FrontendApiProxyOptions;\n};\n","import { AuthStatus } from '@clerk/backend/internal';\nimport { clerkFrontendApiProxy, DEFAULT_PROXY_PATH, stripTrailingSlashes } from '@clerk/backend/proxy';\nimport type { FastifyReply, FastifyRequest } from 'fastify';\nimport { Readable } from 'stream';\n\nimport { clerkClient } from './clerkClient';\nimport * as constants from './constants';\nimport type { ClerkFastifyOptions } from './types';\nimport { fastifyRequestToRequest, requestToProxyRequest } from './utils';\n\nexport const withClerkMiddleware = (options: ClerkFastifyOptions) => {\n const frontendApiProxy = options.frontendApiProxy;\n const proxyPath = stripTrailingSlashes(frontendApiProxy?.path ?? DEFAULT_PROXY_PATH) || DEFAULT_PROXY_PATH;\n\n return async (fastifyRequest: FastifyRequest, reply: FastifyReply) => {\n const publishableKey = options.publishableKey || constants.PUBLISHABLE_KEY;\n const secretKey = options.secretKey || constants.SECRET_KEY;\n\n // Handle Frontend API proxy requests and auto-derive proxyUrl\n let resolvedProxyUrl = options.proxyUrl;\n if (frontendApiProxy) {\n const requestUrl = new URL(\n fastifyRequest.url,\n `${fastifyRequest.protocol}://${fastifyRequest.hostname || 'localhost'}`,\n );\n const isEnabled =\n typeof frontendApiProxy.enabled === 'function'\n ? frontendApiProxy.enabled(requestUrl)\n : frontendApiProxy.enabled;\n\n if (isEnabled) {\n if (requestUrl.pathname === proxyPath || requestUrl.pathname.startsWith(proxyPath + '/')) {\n const proxyRequest = requestToProxyRequest(fastifyRequest);\n\n const proxyResponse = await clerkFrontendApiProxy(proxyRequest, {\n proxyPath,\n publishableKey,\n secretKey,\n });\n\n reply.code(proxyResponse.status);\n proxyResponse.headers.forEach((value, key) => {\n reply.header(key, value);\n });\n\n if (proxyResponse.body) {\n const reader = proxyResponse.body.getReader();\n const stream = new Readable({\n async read() {\n try {\n const { done, value } = await reader.read();\n if (done) {\n this.push(null);\n } else {\n this.push(Buffer.from(value));\n }\n } catch (error) {\n this.destroy(error instanceof Error ? error : new Error(String(error)));\n }\n },\n });\n return reply.send(stream);\n }\n return reply.send();\n }\n\n // Pass just the path - the backend resolves it against the request's\n // public origin (from x-forwarded-* headers).\n if (!resolvedProxyUrl) {\n resolvedProxyUrl = proxyPath;\n }\n }\n }\n\n const req = fastifyRequestToRequest(fastifyRequest);\n\n const requestState = await clerkClient.authenticateRequest(req, {\n ...options,\n secretKey,\n publishableKey,\n proxyUrl: resolvedProxyUrl,\n acceptsToken: 'any',\n });\n\n requestState.headers.forEach((value, key) => reply.header(key, value));\n\n const locationHeader = requestState.headers.get(constants.Headers.Location);\n if (locationHeader) {\n return reply.code(307).send();\n } else if (requestState.status === AuthStatus.Handshake) {\n throw new Error('Clerk: handshake status without redirect');\n }\n\n // @ts-expect-error Inject auth so getAuth can read it\n fastifyRequest.auth = requestState.toAuth();\n };\n};\n","import { createClerkClient } from '@clerk/backend';\n\nimport { API_URL, API_VERSION, JWT_KEY, MACHINE_SECRET_KEY, SDK_METADATA, SECRET_KEY } from './constants';\n\nexport const clerkClient = createClerkClient({\n secretKey: SECRET_KEY,\n machineSecretKey: MACHINE_SECRET_KEY,\n apiUrl: API_URL,\n apiVersion: API_VERSION,\n jwtKey: JWT_KEY,\n userAgent: `${PACKAGE_NAME}@${PACKAGE_VERSION}`,\n sdkMetadata: SDK_METADATA,\n});\n","import { constants } from '@clerk/backend/internal';\nimport { apiUrlFromPublishableKey } from '@clerk/shared/apiUrlFromPublishableKey';\n\nexport const API_VERSION = process.env.CLERK_API_VERSION || 'v1';\nexport const SECRET_KEY = process.env.CLERK_SECRET_KEY || '';\nexport const MACHINE_SECRET_KEY = process.env.CLERK_MACHINE_SECRET_KEY || '';\nexport const PUBLISHABLE_KEY = process.env.CLERK_PUBLISHABLE_KEY || '';\nexport const API_URL = process.env.CLERK_API_URL || apiUrlFromPublishableKey(PUBLISHABLE_KEY);\nexport const JWT_KEY = process.env.CLERK_JWT_KEY || '';\nexport const SDK_METADATA = {\n name: PACKAGE_NAME,\n version: PACKAGE_VERSION,\n environment: process.env.NODE_ENV,\n};\n\nexport const { Cookies, Headers } = constants;\n","import type { FastifyRequest } from 'fastify';\nimport { Readable } from 'stream';\n\nexport const fastifyRequestToRequest = (req: FastifyRequest): Request => {\n const headers = new Headers(\n Object.keys(req.headers).reduce((acc, key) => {\n const value = req.headers[key];\n if (!value) {\n return acc;\n }\n\n if (typeof value === 'string') {\n acc.set(key, value);\n } else {\n acc.set(key, value.join(','));\n }\n return acc;\n }, new Headers()),\n );\n\n // Making some manual tests it seems that FastifyRequest populates the req protocol / hostname\n // based on the forwarded headers. Nevertheless, we are gonna use a dummy base and the request\n // will be fixed by the internals of the clerk/backend package\n const dummyOriginReqUrl = new URL(req.url || '', `${req.protocol}://clerk-dummy`);\n return new Request(dummyOriginReqUrl, {\n method: req.method,\n headers,\n });\n};\n\n/**\n * Converts a Fastify request to a Fetch API Request with full headers and body streaming,\n * suitable for proxy forwarding.\n */\nexport const requestToProxyRequest = (req: FastifyRequest): Request => {\n const headers = new Headers();\n Object.entries(req.headers).forEach(([key, value]) => {\n if (value) {\n headers.set(key, Array.isArray(value) ? value.join(', ') : value);\n }\n });\n\n const forwardedProto = req.headers['x-forwarded-proto'];\n const protoHeader = Array.isArray(forwardedProto) ? forwardedProto[0] : forwardedProto;\n const proto = (protoHeader || '').split(',')[0].trim();\n const protocol = proto === 'https' || req.protocol === 'https' ? 'https' : 'http';\n\n const forwardedHost = req.headers['x-forwarded-host'];\n const hostHeader = Array.isArray(forwardedHost) ? forwardedHost[0] : forwardedHost;\n const host = (hostHeader || '').split(',')[0].trim() || req.hostname || 'localhost';\n\n const url = new URL(req.url || '', `${protocol}://${host}`);\n\n const hasBody = ['POST', 'PUT', 'PATCH'].includes(req.method);\n\n return new Request(url.toString(), {\n method: req.method,\n headers,\n body: hasBody ? (Readable.toWeb(req.raw) as ReadableStream) : undefined,\n // @ts-expect-error - duplex required for streaming bodies but not in all TS definitions\n duplex: hasBody ? 'half' : undefined,\n });\n};\n","import type { AuthOptions, GetAuthFn, SignedInAuthObject, SignedOutAuthObject } from '@clerk/backend/internal';\nimport { getAuthObjectForAcceptedToken } from '@clerk/backend/internal';\nimport type { FastifyRequest } from 'fastify';\n\nimport { pluginRegistrationRequired } from './errors';\n\nexport const getAuth: GetAuthFn<FastifyRequest> = ((req: FastifyRequest, options?: AuthOptions) => {\n const authReq = req as FastifyRequest & { auth: SignedInAuthObject | SignedOutAuthObject };\n\n if (!authReq.auth) {\n throw new Error(pluginRegistrationRequired);\n }\n\n return getAuthObjectForAcceptedToken({ authObject: authReq.auth, acceptsToken: options?.acceptsToken });\n}) as GetAuthFn<FastifyRequest>;\n","// https://github.com/clerk/javascript/blob/main/packages/remix/src/errors.ts#L1-L0\nconst createErrorMessage = (msg: string) => {\n return `🔒 Clerk: ${msg.trim()}\n\nFor more info, check out the docs: https://clerk.com/docs,\nor come say hi in our discord server: https://clerk.com/discord\n`;\n};\n\nexport const pluginRegistrationRequired =\n createErrorMessage(`The \"clerkPlugin\" should be registered before using the \"getAuth\".\nExample:\n\nimport { clerkPlugin } from '@clerk/fastify';\n\nconst server: FastifyInstance = Fastify({ logger: true });\nserver.register(clerkPlugin);\n`);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wBAAc,2BAAd;;;ACCA,4BAAe;;;ACER,IAAM,gBAAgB,CAAC,aAAa,YAAY;;;ACHvD,IAAAA,mBAA2B;AAC3B,mBAAgF;AAEhF,IAAAC,iBAAyB;;;ACHzB,qBAAkC;;;ACAlC,sBAA0B;AAC1B,sCAAyC;AAElC,IAAM,cAAc,QAAQ,IAAI,qBAAqB;AACrD,IAAM,aAAa,QAAQ,IAAI,oBAAoB;AACnD,IAAM,qBAAqB,QAAQ,IAAI,4BAA4B;AACnE,IAAM,kBAAkB,QAAQ,IAAI,yBAAyB;AAC7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AAM,iBAAiB,aAAa,QAAQ,IAAcC,SAAQ,QAAQ;AAC1E,QAAI,gBAAgB;AAClB,aAAO,MAAM,KAAK,GAAG,EAAE,KAAK;AAAA,IAC9B,WAAW,aAAa,WAAW,4BAAW,WAAW;AACvD,YAAM,IAAI,MAAM,0CAA0C;AAAA,IAC5D;AAGA,mBAAe,OAAO,aAAa,OAAO;AAAA,EAC5C;AACF;;;AFzFA,IAAM,SAAqD,CACzD,UACA,MACA,SACG;AACH,WAAS,gBAAgB,QAAQ,IAAI;AAErC,QAAM,WAAW,KAAK,YAAY;AAClC,MAAI,CAAC,cAAc,SAAS,QAAQ,GAAG;AACrC,UAAM,IAAI,MAAM,yBAAyB,QAAQ,EAAE;AAAA,EACrD;AAEA,WAAS,QAAQ,UAAU,oBAAoB,IAAI,CAAC;AAEpD,OAAK;AACP;AAEO,IAAM,kBAAc,sBAAAC,SAAG,QAAQ;AAAA,EACpC,MAAM;AAAA,EACN,SAAS;AACX,CAAC;;;AM1BD,IAAAC,mBAA8C;;;ACA9C,IAAM,qBAAqB,CAAC,QAAgB;AAC1C,SAAO,oBAAa,IAAI,KAAK,CAAC;AAAA;AAAA;AAAA;AAAA;AAKhC;AAEO,IAAM,6BACX,mBAAmB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,CAOpB;;;ADXM,IAAM,WAAsC,CAAC,KAAqB,YAA0B;AACjG,QAAM,UAAU;AAEhB,MAAI,CAAC,QAAQ,MAAM;AACjB,UAAM,IAAI,MAAM,0BAA0B;AAAA,EAC5C;AAEA,aAAO,gDAA8B,EAAE,YAAY,QAAQ,MAAM,cAAc,SAAS,aAAa,CAAC;AACxG;","names":["import_internal","import_stream","Headers","Headers","fp","import_internal"]}
|
package/dist/index.mjs
CHANGED
|
@@ -31,7 +31,7 @@ var API_URL = process.env.CLERK_API_URL || apiUrlFromPublishableKey(PUBLISHABLE_
|
|
|
31
31
|
var JWT_KEY = process.env.CLERK_JWT_KEY || "";
|
|
32
32
|
var SDK_METADATA = {
|
|
33
33
|
name: "@clerk/fastify",
|
|
34
|
-
version: "3.1.27
|
|
34
|
+
version: "3.1.27",
|
|
35
35
|
environment: "production"
|
|
36
36
|
};
|
|
37
37
|
var { Cookies, Headers } = constants;
|
|
@@ -43,7 +43,7 @@ var clerkClient = createClerkClient({
|
|
|
43
43
|
apiUrl: API_URL,
|
|
44
44
|
apiVersion: API_VERSION,
|
|
45
45
|
jwtKey: JWT_KEY,
|
|
46
|
-
userAgent: `${"@clerk/fastify"}@${"3.1.27
|
|
46
|
+
userAgent: `${"@clerk/fastify"}@${"3.1.27"}`,
|
|
47
47
|
sdkMetadata: SDK_METADATA
|
|
48
48
|
});
|
|
49
49
|
|
|
@@ -51,7 +51,6 @@ var clerkClient = createClerkClient({
|
|
|
51
51
|
var withClerkMiddleware = (options) => {
|
|
52
52
|
const frontendApiProxy = options.frontendApiProxy;
|
|
53
53
|
const proxyPath = stripTrailingSlashes(frontendApiProxy?.path ?? DEFAULT_PROXY_PATH) || DEFAULT_PROXY_PATH;
|
|
54
|
-
const enableHandshake = options.enableHandshake ?? true;
|
|
55
54
|
return async (fastifyRequest, reply) => {
|
|
56
55
|
const publishableKey = options.publishableKey || PUBLISHABLE_KEY;
|
|
57
56
|
const secretKey = options.secretKey || SECRET_KEY;
|
|
@@ -108,13 +107,11 @@ var withClerkMiddleware = (options) => {
|
|
|
108
107
|
acceptsToken: "any"
|
|
109
108
|
});
|
|
110
109
|
requestState.headers.forEach((value, key) => reply.header(key, value));
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
throw new Error("Clerk: handshake status without redirect");
|
|
117
|
-
}
|
|
110
|
+
const locationHeader = requestState.headers.get(Headers.Location);
|
|
111
|
+
if (locationHeader) {
|
|
112
|
+
return reply.code(307).send();
|
|
113
|
+
} else if (requestState.status === AuthStatus.Handshake) {
|
|
114
|
+
throw new Error("Clerk: handshake status without redirect");
|
|
118
115
|
}
|
|
119
116
|
fastifyRequest.auth = requestState.toAuth();
|
|
120
117
|
};
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/clerkPlugin.ts","../src/types.ts","../src/withClerkMiddleware.ts","../src/clerkClient.ts","../src/constants.ts","../src/getAuth.ts","../src/errors.ts"],"sourcesContent":["export * from '@clerk/backend';\n\nexport type { ClerkFastifyOptions, FrontendApiProxyOptions } from './types';\n\nexport { clerkPlugin } from './clerkPlugin';\nexport { getAuth } from './getAuth';\nexport { clerkClient } from './clerkClient';\n","import type { FastifyInstance, FastifyPluginCallback } from 'fastify';\nimport fp from 'fastify-plugin';\n\nimport type { ClerkFastifyOptions } from './types';\nimport { ALLOWED_HOOKS } from './types';\nimport { withClerkMiddleware } from './withClerkMiddleware';\n\nconst plugin: FastifyPluginCallback<ClerkFastifyOptions> = (\n instance: FastifyInstance,\n opts: ClerkFastifyOptions,\n done,\n) => {\n instance.decorateRequest('auth', null);\n // run clerk as a middleware to all scoped routes\n const hookName = opts.hookName || 'preHandler';\n if (!ALLOWED_HOOKS.includes(hookName)) {\n throw new Error(`Unsupported hookName: ${hookName}`);\n }\n\n instance.addHook(hookName, withClerkMiddleware(opts));\n\n done();\n};\n\nexport const clerkPlugin = fp(plugin, {\n name: '@clerk/fastify',\n fastify: '5.x',\n});\n","import type { ClerkOptions } from '@clerk/backend';\nimport type { ShouldProxyFn } from '@clerk/shared/proxy';\n\nexport const ALLOWED_HOOKS = ['onRequest', 'preHandler'] as const;\n\n/**\n * Options for the built-in Frontend API proxy.\n *\n * When enabled, the middleware intercepts requests that match the proxy path\n * (default `/__clerk`) and forwards them to the Clerk Frontend API, allowing\n * the Clerk frontend SDKs to communicate with Clerk without third-party\n * cookie or ad-blocker issues.\n */\nexport interface FrontendApiProxyOptions {\n /** Toggle the proxy on/off, or supply a function that decides per-request. */\n enabled: boolean | ShouldProxyFn;\n /** Custom path prefix for the proxy (default: `/__clerk`). */\n path?: string;\n}\n\nexport type ClerkFastifyOptions = ClerkOptions & {\n hookName?: (typeof ALLOWED_HOOKS)[number];\n frontendApiProxy?: FrontendApiProxyOptions;\n /**\n * Whether to enable the handshake flow for session verification.\n * Disable this when using Clerk with a first-party API backend (e.g. a SPA calling\n * a Fastify server) to prevent handshake nonce cookies set during OAuth callbacks\n * from blocking authentication on subsequent API requests.\n * @default true\n */\n enableHandshake?: boolean;\n};\n","import { AuthStatus } from '@clerk/backend/internal';\nimport { clerkFrontendApiProxy, DEFAULT_PROXY_PATH, stripTrailingSlashes } from '@clerk/backend/proxy';\nimport type { FastifyReply, FastifyRequest } from 'fastify';\nimport { Readable } from 'stream';\n\nimport { clerkClient } from './clerkClient';\nimport * as constants from './constants';\nimport type { ClerkFastifyOptions } from './types';\nimport { fastifyRequestToRequest, requestToProxyRequest } from './utils';\n\nexport const withClerkMiddleware = (options: ClerkFastifyOptions) => {\n const frontendApiProxy = options.frontendApiProxy;\n const proxyPath = stripTrailingSlashes(frontendApiProxy?.path ?? DEFAULT_PROXY_PATH) || DEFAULT_PROXY_PATH;\n const enableHandshake = options.enableHandshake ?? true;\n\n return async (fastifyRequest: FastifyRequest, reply: FastifyReply) => {\n const publishableKey = options.publishableKey || constants.PUBLISHABLE_KEY;\n const secretKey = options.secretKey || constants.SECRET_KEY;\n\n // Handle Frontend API proxy requests and auto-derive proxyUrl\n let resolvedProxyUrl = options.proxyUrl;\n if (frontendApiProxy) {\n const requestUrl = new URL(\n fastifyRequest.url,\n `${fastifyRequest.protocol}://${fastifyRequest.hostname || 'localhost'}`,\n );\n const isEnabled =\n typeof frontendApiProxy.enabled === 'function'\n ? frontendApiProxy.enabled(requestUrl)\n : frontendApiProxy.enabled;\n\n if (isEnabled) {\n if (requestUrl.pathname === proxyPath || requestUrl.pathname.startsWith(proxyPath + '/')) {\n const proxyRequest = requestToProxyRequest(fastifyRequest);\n\n const proxyResponse = await clerkFrontendApiProxy(proxyRequest, {\n proxyPath,\n publishableKey,\n secretKey,\n });\n\n reply.code(proxyResponse.status);\n proxyResponse.headers.forEach((value, key) => {\n reply.header(key, value);\n });\n\n if (proxyResponse.body) {\n const reader = proxyResponse.body.getReader();\n const stream = new Readable({\n async read() {\n try {\n const { done, value } = await reader.read();\n if (done) {\n this.push(null);\n } else {\n this.push(Buffer.from(value));\n }\n } catch (error) {\n this.destroy(error instanceof Error ? error : new Error(String(error)));\n }\n },\n });\n return reply.send(stream);\n }\n return reply.send();\n }\n\n // Pass just the path - the backend resolves it against the request's\n // public origin (from x-forwarded-* headers).\n if (!resolvedProxyUrl) {\n resolvedProxyUrl = proxyPath;\n }\n }\n }\n\n const req = fastifyRequestToRequest(fastifyRequest);\n\n const requestState = await clerkClient.authenticateRequest(req, {\n ...options,\n secretKey,\n publishableKey,\n proxyUrl: resolvedProxyUrl,\n acceptsToken: 'any',\n });\n\n requestState.headers.forEach((value, key) => reply.header(key, value));\n\n if (enableHandshake) {\n const locationHeader = requestState.headers.get(constants.Headers.Location);\n if (locationHeader) {\n return reply.code(307).send();\n } else if (requestState.status === AuthStatus.Handshake) {\n throw new Error('Clerk: handshake status without redirect');\n }\n }\n\n // @ts-expect-error Inject auth so getAuth can read it\n fastifyRequest.auth = requestState.toAuth();\n };\n};\n","import { createClerkClient } from '@clerk/backend';\n\nimport { API_URL, API_VERSION, JWT_KEY, MACHINE_SECRET_KEY, SDK_METADATA, SECRET_KEY } from './constants';\n\nexport const clerkClient = createClerkClient({\n secretKey: SECRET_KEY,\n machineSecretKey: MACHINE_SECRET_KEY,\n apiUrl: API_URL,\n apiVersion: API_VERSION,\n jwtKey: JWT_KEY,\n userAgent: `${PACKAGE_NAME}@${PACKAGE_VERSION}`,\n sdkMetadata: SDK_METADATA,\n});\n","import { constants } from '@clerk/backend/internal';\nimport { apiUrlFromPublishableKey } from '@clerk/shared/apiUrlFromPublishableKey';\n\nexport const API_VERSION = process.env.CLERK_API_VERSION || 'v1';\nexport const SECRET_KEY = process.env.CLERK_SECRET_KEY || '';\nexport const MACHINE_SECRET_KEY = process.env.CLERK_MACHINE_SECRET_KEY || '';\nexport const PUBLISHABLE_KEY = process.env.CLERK_PUBLISHABLE_KEY || '';\nexport const API_URL = process.env.CLERK_API_URL || apiUrlFromPublishableKey(PUBLISHABLE_KEY);\nexport const JWT_KEY = process.env.CLERK_JWT_KEY || '';\nexport const SDK_METADATA = {\n name: PACKAGE_NAME,\n version: PACKAGE_VERSION,\n environment: process.env.NODE_ENV,\n};\n\nexport const { Cookies, Headers } = constants;\n","import type { AuthOptions, GetAuthFn, SignedInAuthObject, SignedOutAuthObject } from '@clerk/backend/internal';\nimport { getAuthObjectForAcceptedToken } from '@clerk/backend/internal';\nimport type { FastifyRequest } from 'fastify';\n\nimport { pluginRegistrationRequired } from './errors';\n\nexport const getAuth: GetAuthFn<FastifyRequest> = ((req: FastifyRequest, options?: AuthOptions) => {\n const authReq = req as FastifyRequest & { auth: SignedInAuthObject | SignedOutAuthObject };\n\n if (!authReq.auth) {\n throw new Error(pluginRegistrationRequired);\n }\n\n return getAuthObjectForAcceptedToken({ authObject: authReq.auth, acceptsToken: options?.acceptsToken });\n}) as GetAuthFn<FastifyRequest>;\n","// https://github.com/clerk/javascript/blob/main/packages/remix/src/errors.ts#L1-L0\nconst createErrorMessage = (msg: string) => {\n return `🔒 Clerk: ${msg.trim()}\n\nFor more info, check out the docs: https://clerk.com/docs,\nor come say hi in our discord server: https://clerk.com/discord\n`;\n};\n\nexport const pluginRegistrationRequired =\n createErrorMessage(`The \"clerkPlugin\" should be registered before using the \"getAuth\".\nExample:\n\nimport { clerkPlugin } from '@clerk/fastify';\n\nconst server: FastifyInstance = Fastify({ logger: true });\nserver.register(clerkPlugin);\n`);\n"],"mappings":";;;;;;AAAA,cAAc;;;ACCd,OAAO,QAAQ;;;ACER,IAAM,gBAAgB,CAAC,aAAa,YAAY;;;ACHvD,SAAS,kBAAkB;AAC3B,SAAS,uBAAuB,oBAAoB,4BAA4B;AAEhF,SAAS,gBAAgB;;;ACH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A,MAC9B,WAAW,aAAa,WAAW,WAAW,WAAW;AACvD,cAAM,IAAI,MAAM,0CAA0C;AAAA,MAC5D;AAAA,IACF;AAGA,mBAAe,OAAO,aAAa,OAAO;AAAA,EAC5C;AACF;;;AF5FA,IAAM,SAAqD,CACzD,UACA,MACA,SACG;AACH,WAAS,gBAAgB,QAAQ,IAAI;AAErC,QAAM,WAAW,KAAK,YAAY;AAClC,MAAI,CAAC,cAAc,SAAS,QAAQ,GAAG;AACrC,UAAM,IAAI,MAAM,yBAAyB,QAAQ,EAAE;AAAA,EACrD;AAEA,WAAS,QAAQ,UAAU,oBAAoB,IAAI,CAAC;AAEpD,OAAK;AACP;AAEO,IAAM,cAAc,GAAG,QAAQ;AAAA,EACpC,MAAM;AAAA,EACN,SAAS;AACX,CAAC;;;AK1BD,SAAS,qCAAqC;;;ACA9C,IAAM,qBAAqB,CAAC,QAAgB;AAC1C,SAAO,oBAAa,IAAI,KAAK,CAAC;AAAA;AAAA;AAAA;AAAA;AAKhC;AAEO,IAAM,6BACX,mBAAmB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,CAOpB;;;ADXM,IAAM,WAAsC,CAAC,KAAqB,YAA0B;AACjG,QAAM,UAAU;AAEhB,MAAI,CAAC,QAAQ,MAAM;AACjB,UAAM,IAAI,MAAM,0BAA0B;AAAA,EAC5C;AAEA,SAAO,8BAA8B,EAAE,YAAY,QAAQ,MAAM,cAAc,SAAS,aAAa,CAAC;AACxG;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/clerkPlugin.ts","../src/types.ts","../src/withClerkMiddleware.ts","../src/clerkClient.ts","../src/constants.ts","../src/getAuth.ts","../src/errors.ts"],"sourcesContent":["export * from '@clerk/backend';\n\nexport type { ClerkFastifyOptions, FrontendApiProxyOptions } from './types';\n\nexport { clerkPlugin } from './clerkPlugin';\nexport { getAuth } from './getAuth';\nexport { clerkClient } from './clerkClient';\n","import type { FastifyInstance, FastifyPluginCallback } from 'fastify';\nimport fp from 'fastify-plugin';\n\nimport type { ClerkFastifyOptions } from './types';\nimport { ALLOWED_HOOKS } from './types';\nimport { withClerkMiddleware } from './withClerkMiddleware';\n\nconst plugin: FastifyPluginCallback<ClerkFastifyOptions> = (\n instance: FastifyInstance,\n opts: ClerkFastifyOptions,\n done,\n) => {\n instance.decorateRequest('auth', null);\n // run clerk as a middleware to all scoped routes\n const hookName = opts.hookName || 'preHandler';\n if (!ALLOWED_HOOKS.includes(hookName)) {\n throw new Error(`Unsupported hookName: ${hookName}`);\n }\n\n instance.addHook(hookName, withClerkMiddleware(opts));\n\n done();\n};\n\nexport const clerkPlugin = fp(plugin, {\n name: '@clerk/fastify',\n fastify: '5.x',\n});\n","import type { ClerkOptions } from '@clerk/backend';\nimport type { ShouldProxyFn } from '@clerk/shared/proxy';\n\nexport const ALLOWED_HOOKS = ['onRequest', 'preHandler'] as const;\n\n/**\n * Options for the built-in Frontend API proxy.\n *\n * When enabled, the middleware intercepts requests that match the proxy path\n * (default `/__clerk`) and forwards them to the Clerk Frontend API, allowing\n * the Clerk frontend SDKs to communicate with Clerk without third-party\n * cookie or ad-blocker issues.\n */\nexport interface FrontendApiProxyOptions {\n /** Toggle the proxy on/off, or supply a function that decides per-request. */\n enabled: boolean | ShouldProxyFn;\n /** Custom path prefix for the proxy (default: `/__clerk`). */\n path?: string;\n}\n\nexport type ClerkFastifyOptions = ClerkOptions & {\n hookName?: (typeof ALLOWED_HOOKS)[number];\n frontendApiProxy?: FrontendApiProxyOptions;\n};\n","import { AuthStatus } from '@clerk/backend/internal';\nimport { clerkFrontendApiProxy, DEFAULT_PROXY_PATH, stripTrailingSlashes } from '@clerk/backend/proxy';\nimport type { FastifyReply, FastifyRequest } from 'fastify';\nimport { Readable } from 'stream';\n\nimport { clerkClient } from './clerkClient';\nimport * as constants from './constants';\nimport type { ClerkFastifyOptions } from './types';\nimport { fastifyRequestToRequest, requestToProxyRequest } from './utils';\n\nexport const withClerkMiddleware = (options: ClerkFastifyOptions) => {\n const frontendApiProxy = options.frontendApiProxy;\n const proxyPath = stripTrailingSlashes(frontendApiProxy?.path ?? DEFAULT_PROXY_PATH) || DEFAULT_PROXY_PATH;\n\n return async (fastifyRequest: FastifyRequest, reply: FastifyReply) => {\n const publishableKey = options.publishableKey || constants.PUBLISHABLE_KEY;\n const secretKey = options.secretKey || constants.SECRET_KEY;\n\n // Handle Frontend API proxy requests and auto-derive proxyUrl\n let resolvedProxyUrl = options.proxyUrl;\n if (frontendApiProxy) {\n const requestUrl = new URL(\n fastifyRequest.url,\n `${fastifyRequest.protocol}://${fastifyRequest.hostname || 'localhost'}`,\n );\n const isEnabled =\n typeof frontendApiProxy.enabled === 'function'\n ? frontendApiProxy.enabled(requestUrl)\n : frontendApiProxy.enabled;\n\n if (isEnabled) {\n if (requestUrl.pathname === proxyPath || requestUrl.pathname.startsWith(proxyPath + '/')) {\n const proxyRequest = requestToProxyRequest(fastifyRequest);\n\n const proxyResponse = await clerkFrontendApiProxy(proxyRequest, {\n proxyPath,\n publishableKey,\n secretKey,\n });\n\n reply.code(proxyResponse.status);\n proxyResponse.headers.forEach((value, key) => {\n reply.header(key, value);\n });\n\n if (proxyResponse.body) {\n const reader = proxyResponse.body.getReader();\n const stream = new Readable({\n async read() {\n try {\n const { done, value } = await reader.read();\n if (done) {\n this.push(null);\n } else {\n this.push(Buffer.from(value));\n }\n } catch (error) {\n this.destroy(error instanceof Error ? error : new Error(String(error)));\n }\n },\n });\n return reply.send(stream);\n }\n return reply.send();\n }\n\n // Pass just the path - the backend resolves it against the request's\n // public origin (from x-forwarded-* headers).\n if (!resolvedProxyUrl) {\n resolvedProxyUrl = proxyPath;\n }\n }\n }\n\n const req = fastifyRequestToRequest(fastifyRequest);\n\n const requestState = await clerkClient.authenticateRequest(req, {\n ...options,\n secretKey,\n publishableKey,\n proxyUrl: resolvedProxyUrl,\n acceptsToken: 'any',\n });\n\n requestState.headers.forEach((value, key) => reply.header(key, value));\n\n const locationHeader = requestState.headers.get(constants.Headers.Location);\n if (locationHeader) {\n return reply.code(307).send();\n } else if (requestState.status === AuthStatus.Handshake) {\n throw new Error('Clerk: handshake status without redirect');\n }\n\n // @ts-expect-error Inject auth so getAuth can read it\n fastifyRequest.auth = requestState.toAuth();\n };\n};\n","import { createClerkClient } from '@clerk/backend';\n\nimport { API_URL, API_VERSION, JWT_KEY, MACHINE_SECRET_KEY, SDK_METADATA, SECRET_KEY } from './constants';\n\nexport const clerkClient = createClerkClient({\n secretKey: SECRET_KEY,\n machineSecretKey: MACHINE_SECRET_KEY,\n apiUrl: API_URL,\n apiVersion: API_VERSION,\n jwtKey: JWT_KEY,\n userAgent: `${PACKAGE_NAME}@${PACKAGE_VERSION}`,\n sdkMetadata: SDK_METADATA,\n});\n","import { constants } from '@clerk/backend/internal';\nimport { apiUrlFromPublishableKey } from '@clerk/shared/apiUrlFromPublishableKey';\n\nexport const API_VERSION = process.env.CLERK_API_VERSION || 'v1';\nexport const SECRET_KEY = process.env.CLERK_SECRET_KEY || '';\nexport const MACHINE_SECRET_KEY = process.env.CLERK_MACHINE_SECRET_KEY || '';\nexport const PUBLISHABLE_KEY = process.env.CLERK_PUBLISHABLE_KEY || '';\nexport const API_URL = process.env.CLERK_API_URL || apiUrlFromPublishableKey(PUBLISHABLE_KEY);\nexport const JWT_KEY = process.env.CLERK_JWT_KEY || '';\nexport const SDK_METADATA = {\n name: PACKAGE_NAME,\n version: PACKAGE_VERSION,\n environment: process.env.NODE_ENV,\n};\n\nexport const { Cookies, Headers } = constants;\n","import type { AuthOptions, GetAuthFn, SignedInAuthObject, SignedOutAuthObject } from '@clerk/backend/internal';\nimport { getAuthObjectForAcceptedToken } from '@clerk/backend/internal';\nimport type { FastifyRequest } from 'fastify';\n\nimport { pluginRegistrationRequired } from './errors';\n\nexport const getAuth: GetAuthFn<FastifyRequest> = ((req: FastifyRequest, options?: AuthOptions) => {\n const authReq = req as FastifyRequest & { auth: SignedInAuthObject | SignedOutAuthObject };\n\n if (!authReq.auth) {\n throw new Error(pluginRegistrationRequired);\n }\n\n return getAuthObjectForAcceptedToken({ authObject: authReq.auth, acceptsToken: options?.acceptsToken });\n}) as GetAuthFn<FastifyRequest>;\n","// https://github.com/clerk/javascript/blob/main/packages/remix/src/errors.ts#L1-L0\nconst createErrorMessage = (msg: string) => {\n return `🔒 Clerk: ${msg.trim()}\n\nFor more info, check out the docs: https://clerk.com/docs,\nor come say hi in our discord server: https://clerk.com/discord\n`;\n};\n\nexport const pluginRegistrationRequired =\n createErrorMessage(`The \"clerkPlugin\" should be registered before using the \"getAuth\".\nExample:\n\nimport { clerkPlugin } from '@clerk/fastify';\n\nconst server: FastifyInstance = Fastify({ logger: true });\nserver.register(clerkPlugin);\n`);\n"],"mappings":";;;;;;AAAA,cAAc;;;ACCd,OAAO,QAAQ;;;ACER,IAAM,gBAAgB,CAAC,aAAa,YAAY;;;ACHvD,SAAS,kBAAkB;AAC3B,SAAS,uBAAuB,oBAAoB,4BAA4B;AAEhF,SAAS,gBAAgB;;;ACHzB,SAAS,yBAAyB;;;ACAlC,SAAS,iBAAiB;AAC1B,SAAS,gCAAgC;AAElC,IAAM,cAAc,QAAQ,IAAI,qBAAqB;AACrD,IAAM,aAAa,QAAQ,IAAI,oBAAoB;AACnD,IAAM,qBAAqB,QAAQ,IAAI,4BAA4B;AACnE,IAAM,kBAAkB,QAAQ,IAAI,yBAAyB;AAC7D,IAAM,UAAU,QAAQ,IAAI,iBAAiB,yBAAyB,eAAe;AACrF,IAAM,UAAU,QAAQ,IAAI,iBAAiB;AAC7C,IAAM,eAAe;AAAA,EAC1B,MAAM;AAAA,EACN,SAAS;AAAA,EACT,aAAa;AACf;AAEO,IAAM,EAAE,SAAS,QAAQ,IAAI;;;ADX7B,IAAM,cAAc,kBAAkB;AAAA,EAC3C,WAAW;AAAA,EACX,kBAAkB;AAAA,EAClB,QAAQ;AAAA,EACR,YAAY;AAAA,EACZ,QAAQ;AAAA,EACR,WAAW,GAAG,gBAAY,IAAI,QAAe;A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;AAAA;AAAA;AAAA,CAOpB;;;ADXM,IAAM,WAAsC,CAAC,KAAqB,YAA0B;AACjG,QAAM,UAAU;AAEhB,MAAI,CAAC,QAAQ,MAAM;AACjB,UAAM,IAAI,MAAM,0BAA0B;AAAA,EAC5C;AAEA,SAAO,8BAA8B,EAAE,YAAY,QAAQ,MAAM,cAAc,SAAS,aAAa,CAAC;AACxG;","names":[]}
|
package/dist/types.d.mts
CHANGED
package/dist/types.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clerk/fastify",
|
|
3
|
-
"version": "3.1.27
|
|
3
|
+
"version": "3.1.27",
|
|
4
4
|
"description": "Clerk SDK for Fastify",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"auth",
|
|
@@ -62,8 +62,8 @@
|
|
|
62
62
|
"dependencies": {
|
|
63
63
|
"cookies": "0.9.1",
|
|
64
64
|
"fastify-plugin": "^5.1.0",
|
|
65
|
-
"@clerk/backend": "3.4.9
|
|
66
|
-
"@clerk/shared": "4.12.0
|
|
65
|
+
"@clerk/backend": "^3.4.9",
|
|
66
|
+
"@clerk/shared": "^4.12.0"
|
|
67
67
|
},
|
|
68
68
|
"devDependencies": {
|
|
69
69
|
"fastify": "^5.8.5"
|