@clerk/fastify 3.1.28-snapshot.v20260516134813 → 3.1.28
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-C5pGnL7a.d.mts → index-CWnSaB0B.d.mts} +1 -14
- package/dist/{index-C5pGnL7a.d.ts → index-CWnSaB0B.d.ts} +1 -14
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +24 -59
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +25 -60
- 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
|
@@ -1,12 +1,7 @@
|
|
|
1
1
|
import '@clerk/shared/types';
|
|
2
|
-
import {
|
|
2
|
+
import { ClerkOptions } from '@clerk/backend';
|
|
3
3
|
import { ShouldProxyFn } from '@clerk/shared/proxy';
|
|
4
4
|
|
|
5
|
-
declare module 'fastify' {
|
|
6
|
-
interface FastifyRequest {
|
|
7
|
-
clerk: ClerkClient;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
5
|
declare const ALLOWED_HOOKS: readonly ["onRequest", "preHandler"];
|
|
11
6
|
/**
|
|
12
7
|
* Options for the built-in Frontend API proxy.
|
|
@@ -25,14 +20,6 @@ interface FrontendApiProxyOptions {
|
|
|
25
20
|
type ClerkFastifyOptions = ClerkOptions & {
|
|
26
21
|
hookName?: (typeof ALLOWED_HOOKS)[number];
|
|
27
22
|
frontendApiProxy?: FrontendApiProxyOptions;
|
|
28
|
-
/**
|
|
29
|
-
* Whether to enable the handshake flow for session verification.
|
|
30
|
-
* Disable this when using Clerk with a first-party API backend (e.g. a SPA calling
|
|
31
|
-
* a Fastify server) to prevent handshake nonce cookies set during OAuth callbacks
|
|
32
|
-
* from blocking authentication on subsequent API requests.
|
|
33
|
-
* @default true
|
|
34
|
-
*/
|
|
35
|
-
enableHandshake?: boolean;
|
|
36
23
|
};
|
|
37
24
|
|
|
38
25
|
export type { ClerkFastifyOptions as C, FrontendApiProxyOptions as F };
|
|
@@ -1,12 +1,7 @@
|
|
|
1
1
|
import '@clerk/shared/types';
|
|
2
|
-
import {
|
|
2
|
+
import { ClerkOptions } from '@clerk/backend';
|
|
3
3
|
import { ShouldProxyFn } from '@clerk/shared/proxy';
|
|
4
4
|
|
|
5
|
-
declare module 'fastify' {
|
|
6
|
-
interface FastifyRequest {
|
|
7
|
-
clerk: ClerkClient;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
5
|
declare const ALLOWED_HOOKS: readonly ["onRequest", "preHandler"];
|
|
11
6
|
/**
|
|
12
7
|
* Options for the built-in Frontend API proxy.
|
|
@@ -25,14 +20,6 @@ interface FrontendApiProxyOptions {
|
|
|
25
20
|
type ClerkFastifyOptions = ClerkOptions & {
|
|
26
21
|
hookName?: (typeof ALLOWED_HOOKS)[number];
|
|
27
22
|
frontendApiProxy?: FrontendApiProxyOptions;
|
|
28
|
-
/**
|
|
29
|
-
* Whether to enable the handshake flow for session verification.
|
|
30
|
-
* Disable this when using Clerk with a first-party API backend (e.g. a SPA calling
|
|
31
|
-
* a Fastify server) to prevent handshake nonce cookies set during OAuth callbacks
|
|
32
|
-
* from blocking authentication on subsequent API requests.
|
|
33
|
-
* @default true
|
|
34
|
-
*/
|
|
35
|
-
enableHandshake?: boolean;
|
|
36
23
|
};
|
|
37
24
|
|
|
38
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
|
@@ -45,11 +45,13 @@ var import_fastify_plugin = __toESM(require("fastify-plugin"));
|
|
|
45
45
|
var ALLOWED_HOOKS = ["onRequest", "preHandler"];
|
|
46
46
|
|
|
47
47
|
// src/withClerkMiddleware.ts
|
|
48
|
-
var import_backend = require("@clerk/backend");
|
|
49
48
|
var import_internal2 = require("@clerk/backend/internal");
|
|
50
49
|
var import_proxy = require("@clerk/backend/proxy");
|
|
51
50
|
var import_stream2 = require("stream");
|
|
52
51
|
|
|
52
|
+
// src/clerkClient.ts
|
|
53
|
+
var import_backend = require("@clerk/backend");
|
|
54
|
+
|
|
53
55
|
// src/constants.ts
|
|
54
56
|
var import_internal = require("@clerk/backend/internal");
|
|
55
57
|
var import_apiUrlFromPublishableKey = require("@clerk/shared/apiUrlFromPublishableKey");
|
|
@@ -61,11 +63,22 @@ var API_URL = process.env.CLERK_API_URL || (0, import_apiUrlFromPublishableKey.a
|
|
|
61
63
|
var JWT_KEY = process.env.CLERK_JWT_KEY || "";
|
|
62
64
|
var SDK_METADATA = {
|
|
63
65
|
name: "@clerk/fastify",
|
|
64
|
-
version: "3.1.28
|
|
66
|
+
version: "3.1.28",
|
|
65
67
|
environment: "production"
|
|
66
68
|
};
|
|
67
69
|
var { Cookies, Headers: Headers2 } = import_internal.constants;
|
|
68
70
|
|
|
71
|
+
// src/clerkClient.ts
|
|
72
|
+
var clerkClient = (0, import_backend.createClerkClient)({
|
|
73
|
+
secretKey: SECRET_KEY,
|
|
74
|
+
machineSecretKey: MACHINE_SECRET_KEY,
|
|
75
|
+
apiUrl: API_URL,
|
|
76
|
+
apiVersion: API_VERSION,
|
|
77
|
+
jwtKey: JWT_KEY,
|
|
78
|
+
userAgent: `${"@clerk/fastify"}@${"3.1.28"}`,
|
|
79
|
+
sdkMetadata: SDK_METADATA
|
|
80
|
+
});
|
|
81
|
+
|
|
69
82
|
// src/utils.ts
|
|
70
83
|
var import_stream = require("stream");
|
|
71
84
|
var fastifyRequestToRequest = (req) => {
|
|
@@ -115,41 +128,12 @@ var requestToProxyRequest = (req) => {
|
|
|
115
128
|
};
|
|
116
129
|
|
|
117
130
|
// src/withClerkMiddleware.ts
|
|
118
|
-
function stripHandshakeCookiesAndParams(req, cookieNames) {
|
|
119
|
-
const url = new URL(req.url);
|
|
120
|
-
for (const name of cookieNames) {
|
|
121
|
-
url.searchParams.delete(name);
|
|
122
|
-
}
|
|
123
|
-
const headers = new Headers(req.headers);
|
|
124
|
-
const cookieHeader = headers.get("cookie");
|
|
125
|
-
if (cookieHeader) {
|
|
126
|
-
const filtered = cookieHeader.split(";").map((c) => c.trim()).filter((c) => !cookieNames.some((name) => c === name || c.startsWith(`${name}=`))).join("; ");
|
|
127
|
-
if (filtered) {
|
|
128
|
-
headers.set("cookie", filtered);
|
|
129
|
-
} else {
|
|
130
|
-
headers.delete("cookie");
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
return new Request(url.toString(), { method: req.method, headers });
|
|
134
|
-
}
|
|
135
131
|
var withClerkMiddleware = (options) => {
|
|
136
132
|
const frontendApiProxy = options.frontendApiProxy;
|
|
137
133
|
const proxyPath = (0, import_proxy.stripTrailingSlashes)(frontendApiProxy?.path ?? import_proxy.DEFAULT_PROXY_PATH) || import_proxy.DEFAULT_PROXY_PATH;
|
|
138
|
-
const publishableKey = options.publishableKey || PUBLISHABLE_KEY;
|
|
139
|
-
const secretKey = options.secretKey || SECRET_KEY;
|
|
140
|
-
const clerkClient2 = (0, import_backend.createClerkClient)({
|
|
141
|
-
...options,
|
|
142
|
-
publishableKey,
|
|
143
|
-
secretKey,
|
|
144
|
-
machineSecretKey: options.machineSecretKey || MACHINE_SECRET_KEY,
|
|
145
|
-
apiUrl: options.apiUrl || API_URL,
|
|
146
|
-
apiVersion: options.apiVersion || API_VERSION,
|
|
147
|
-
jwtKey: options.jwtKey || JWT_KEY,
|
|
148
|
-
userAgent: options.userAgent || `${SDK_METADATA.name}@${SDK_METADATA.version}`,
|
|
149
|
-
sdkMetadata: options.sdkMetadata || SDK_METADATA
|
|
150
|
-
});
|
|
151
|
-
const enableHandshake = options.enableHandshake ?? true;
|
|
152
134
|
return async (fastifyRequest, reply) => {
|
|
135
|
+
const publishableKey = options.publishableKey || PUBLISHABLE_KEY;
|
|
136
|
+
const secretKey = options.secretKey || SECRET_KEY;
|
|
153
137
|
let resolvedProxyUrl = options.proxyUrl;
|
|
154
138
|
if (frontendApiProxy) {
|
|
155
139
|
const requestUrl = new URL(
|
|
@@ -194,11 +178,8 @@ var withClerkMiddleware = (options) => {
|
|
|
194
178
|
}
|
|
195
179
|
}
|
|
196
180
|
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
req = stripHandshakeCookiesAndParams(req, [Cookies.Handshake, Cookies.HandshakeNonce]);
|
|
200
|
-
}
|
|
201
|
-
const requestState = await clerkClient2.authenticateRequest(req, {
|
|
181
|
+
const req = fastifyRequestToRequest(fastifyRequest);
|
|
182
|
+
const requestState = await clerkClient.authenticateRequest(req, {
|
|
202
183
|
...options,
|
|
203
184
|
secretKey,
|
|
204
185
|
publishableKey,
|
|
@@ -206,23 +187,19 @@ var withClerkMiddleware = (options) => {
|
|
|
206
187
|
acceptsToken: "any"
|
|
207
188
|
});
|
|
208
189
|
requestState.headers.forEach((value, key) => reply.header(key, value));
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
throw new Error("Clerk: handshake status without redirect");
|
|
215
|
-
}
|
|
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");
|
|
216
195
|
}
|
|
217
196
|
fastifyRequest.auth = requestState.toAuth();
|
|
218
|
-
fastifyRequest.clerk = clerkClient2;
|
|
219
197
|
};
|
|
220
198
|
};
|
|
221
199
|
|
|
222
200
|
// src/clerkPlugin.ts
|
|
223
201
|
var plugin = (instance, opts, done) => {
|
|
224
202
|
instance.decorateRequest("auth", null);
|
|
225
|
-
instance.decorateRequest("clerk", null);
|
|
226
203
|
const hookName = opts.hookName || "preHandler";
|
|
227
204
|
if (!ALLOWED_HOOKS.includes(hookName)) {
|
|
228
205
|
throw new Error(`Unsupported hookName: ${hookName}`);
|
|
@@ -263,18 +240,6 @@ var getAuth = ((req, options) => {
|
|
|
263
240
|
}
|
|
264
241
|
return (0, import_internal3.getAuthObjectForAcceptedToken)({ authObject: authReq.auth, acceptsToken: options?.acceptsToken });
|
|
265
242
|
});
|
|
266
|
-
|
|
267
|
-
// src/clerkClient.ts
|
|
268
|
-
var import_backend2 = require("@clerk/backend");
|
|
269
|
-
var clerkClient = (0, import_backend2.createClerkClient)({
|
|
270
|
-
secretKey: SECRET_KEY,
|
|
271
|
-
machineSecretKey: MACHINE_SECRET_KEY,
|
|
272
|
-
apiUrl: API_URL,
|
|
273
|
-
apiVersion: API_VERSION,
|
|
274
|
-
jwtKey: JWT_KEY,
|
|
275
|
-
userAgent: `${"@clerk/fastify"}@${"3.1.28-snapshot.v20260516134813"}`,
|
|
276
|
-
sdkMetadata: SDK_METADATA
|
|
277
|
-
});
|
|
278
243
|
// Annotate the CommonJS export names for ESM import in node:
|
|
279
244
|
0 && (module.exports = {
|
|
280
245
|
clerkClient,
|
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/constants.ts","../src/utils.ts","../src/getAuth.ts","../src/errors.ts","../src/clerkClient.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 // eslint-disable-next-line @typescript-eslint/no-explicit-any\n instance.decorateRequest('clerk', null as any);\n\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 { ClerkClient, ClerkOptions } from '@clerk/backend';\nimport type { ShouldProxyFn } from '@clerk/shared/proxy';\n\ndeclare module 'fastify' {\n interface FastifyRequest {\n clerk: ClerkClient;\n }\n}\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 { createClerkClient } from '@clerk/backend';\nimport { 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 * as constants from './constants';\nimport type { ClerkFastifyOptions } from './types';\nimport { fastifyRequestToRequest, requestToProxyRequest } from './utils';\n\nfunction stripHandshakeCookiesAndParams(req: Request, cookieNames: string[]): Request {\n const url = new URL(req.url);\n for (const name of cookieNames) {\n url.searchParams.delete(name);\n }\n\n const headers = new Headers(req.headers);\n const cookieHeader = headers.get('cookie');\n if (cookieHeader) {\n const filtered = cookieHeader\n .split(';')\n .map(c => c.trim())\n .filter(c => !cookieNames.some(name => c === name || c.startsWith(`${name}=`)))\n .join('; ');\n if (filtered) {\n headers.set('cookie', filtered);\n } else {\n headers.delete('cookie');\n }\n }\n\n return new Request(url.toString(), { method: req.method, headers });\n}\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 publishableKey = options.publishableKey || constants.PUBLISHABLE_KEY;\n const secretKey = options.secretKey || constants.SECRET_KEY;\n const clerkClient = createClerkClient({\n ...options,\n publishableKey,\n secretKey,\n machineSecretKey: options.machineSecretKey || constants.MACHINE_SECRET_KEY,\n apiUrl: options.apiUrl || constants.API_URL,\n apiVersion: options.apiVersion || constants.API_VERSION,\n jwtKey: options.jwtKey || constants.JWT_KEY,\n userAgent: options.userAgent || `${constants.SDK_METADATA.name}@${constants.SDK_METADATA.version}`,\n sdkMetadata: options.sdkMetadata || constants.SDK_METADATA,\n });\n const enableHandshake = options.enableHandshake ?? true;\n\n return async (fastifyRequest: FastifyRequest, reply: FastifyReply) => {\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 let req = fastifyRequestToRequest(fastifyRequest);\n\n if (!enableHandshake) {\n req = stripHandshakeCookiesAndParams(req, [constants.Cookies.Handshake, constants.Cookies.HandshakeNonce]);\n }\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 fastifyRequest.clerk = clerkClient;\n };\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","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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wBAAc,2BAAd;;;ACCA,4BAAe;;;ACQR,IAAM,gBAAgB,CAAC,aAAa,YAAY;;;ACTvD,qBAAkC;AAClC,IAAAA,mBAA2B;AAC3B,mBAAgF;AAEhF,IAAAC,iBAAyB;;;ACJzB,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;AAC7D,IAAM,UAAU,QAAQ,IAAI,qBAAiB,0DAAyB,eAAe;AACrF,IAAM,UAAU,QAAQ,IAAI,i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,IAAW;AAG7C,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,sBAAAE,SAAG,QAAQ;AAAA,EACpC,MAAM;AAAA,EACN,SAAS;AACX,CAAC;;;AK7BD,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;;;AEdA,IAAAC,kBAAkC;AAI3B,IAAM,kBAAc,mCAAkB;AAAA,EAC3C,WAAW;AAAA,EACX,kBAAkB;AAAA,EAClB,QAAQ;AAAA,EACR,YAAY;AAAA,EACZ,QAAQ;AAAA,EACR,WAAW,GAAG,gBAAY,IAAI,iCAAe;AAAA,EAC7C,aAAa;AACf,CAAC;","names":["import_internal","import_stream","Headers","clerkClient","Headers","fp","import_internal","import_backend"]}
|
|
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;AAC7D,IAAM,UAAU,QAAQ,IAAI,qBAAiB,0DAAyB,eAAe;AACrF,IAAM,UAAU,QAAQ,IAAI,iBAAiB;AAC7C,IAAM,eAAe;AAAA,EAC1B,MAAM;AAAA,EACN,SAAS;AAAA,EACT,aAAa;AACf;AAEO,IAAM,EAAE,SAAS,SAAAC,SAAQ,IAAI;;;ADX7B,IAAM,kBAAc,kCAAkB;AAAA,EAC3C,WAAW;AAAA,EACX,kBAAkB;AAAA,EAClB,QAAQ;AAAA,EACR,YAAY;AAAA,EACZ,QA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,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
|
@@ -13,11 +13,13 @@ import fp from "fastify-plugin";
|
|
|
13
13
|
var ALLOWED_HOOKS = ["onRequest", "preHandler"];
|
|
14
14
|
|
|
15
15
|
// src/withClerkMiddleware.ts
|
|
16
|
-
import { createClerkClient } from "@clerk/backend";
|
|
17
16
|
import { AuthStatus } from "@clerk/backend/internal";
|
|
18
17
|
import { clerkFrontendApiProxy, DEFAULT_PROXY_PATH, stripTrailingSlashes } from "@clerk/backend/proxy";
|
|
19
18
|
import { Readable } from "stream";
|
|
20
19
|
|
|
20
|
+
// src/clerkClient.ts
|
|
21
|
+
import { createClerkClient } from "@clerk/backend";
|
|
22
|
+
|
|
21
23
|
// src/constants.ts
|
|
22
24
|
import { constants } from "@clerk/backend/internal";
|
|
23
25
|
import { apiUrlFromPublishableKey } from "@clerk/shared/apiUrlFromPublishableKey";
|
|
@@ -29,47 +31,29 @@ var API_URL = process.env.CLERK_API_URL || apiUrlFromPublishableKey(PUBLISHABLE_
|
|
|
29
31
|
var JWT_KEY = process.env.CLERK_JWT_KEY || "";
|
|
30
32
|
var SDK_METADATA = {
|
|
31
33
|
name: "@clerk/fastify",
|
|
32
|
-
version: "3.1.28
|
|
34
|
+
version: "3.1.28",
|
|
33
35
|
environment: "production"
|
|
34
36
|
};
|
|
35
|
-
var { Cookies, Headers
|
|
37
|
+
var { Cookies, Headers } = constants;
|
|
38
|
+
|
|
39
|
+
// src/clerkClient.ts
|
|
40
|
+
var clerkClient = createClerkClient({
|
|
41
|
+
secretKey: SECRET_KEY,
|
|
42
|
+
machineSecretKey: MACHINE_SECRET_KEY,
|
|
43
|
+
apiUrl: API_URL,
|
|
44
|
+
apiVersion: API_VERSION,
|
|
45
|
+
jwtKey: JWT_KEY,
|
|
46
|
+
userAgent: `${"@clerk/fastify"}@${"3.1.28"}`,
|
|
47
|
+
sdkMetadata: SDK_METADATA
|
|
48
|
+
});
|
|
36
49
|
|
|
37
50
|
// src/withClerkMiddleware.ts
|
|
38
|
-
function stripHandshakeCookiesAndParams(req, cookieNames) {
|
|
39
|
-
const url = new URL(req.url);
|
|
40
|
-
for (const name of cookieNames) {
|
|
41
|
-
url.searchParams.delete(name);
|
|
42
|
-
}
|
|
43
|
-
const headers = new Headers(req.headers);
|
|
44
|
-
const cookieHeader = headers.get("cookie");
|
|
45
|
-
if (cookieHeader) {
|
|
46
|
-
const filtered = cookieHeader.split(";").map((c) => c.trim()).filter((c) => !cookieNames.some((name) => c === name || c.startsWith(`${name}=`))).join("; ");
|
|
47
|
-
if (filtered) {
|
|
48
|
-
headers.set("cookie", filtered);
|
|
49
|
-
} else {
|
|
50
|
-
headers.delete("cookie");
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
return new Request(url.toString(), { method: req.method, headers });
|
|
54
|
-
}
|
|
55
51
|
var withClerkMiddleware = (options) => {
|
|
56
52
|
const frontendApiProxy = options.frontendApiProxy;
|
|
57
53
|
const proxyPath = stripTrailingSlashes(frontendApiProxy?.path ?? DEFAULT_PROXY_PATH) || DEFAULT_PROXY_PATH;
|
|
58
|
-
const publishableKey = options.publishableKey || PUBLISHABLE_KEY;
|
|
59
|
-
const secretKey = options.secretKey || SECRET_KEY;
|
|
60
|
-
const clerkClient2 = createClerkClient({
|
|
61
|
-
...options,
|
|
62
|
-
publishableKey,
|
|
63
|
-
secretKey,
|
|
64
|
-
machineSecretKey: options.machineSecretKey || MACHINE_SECRET_KEY,
|
|
65
|
-
apiUrl: options.apiUrl || API_URL,
|
|
66
|
-
apiVersion: options.apiVersion || API_VERSION,
|
|
67
|
-
jwtKey: options.jwtKey || JWT_KEY,
|
|
68
|
-
userAgent: options.userAgent || `${SDK_METADATA.name}@${SDK_METADATA.version}`,
|
|
69
|
-
sdkMetadata: options.sdkMetadata || SDK_METADATA
|
|
70
|
-
});
|
|
71
|
-
const enableHandshake = options.enableHandshake ?? true;
|
|
72
54
|
return async (fastifyRequest, reply) => {
|
|
55
|
+
const publishableKey = options.publishableKey || PUBLISHABLE_KEY;
|
|
56
|
+
const secretKey = options.secretKey || SECRET_KEY;
|
|
73
57
|
let resolvedProxyUrl = options.proxyUrl;
|
|
74
58
|
if (frontendApiProxy) {
|
|
75
59
|
const requestUrl = new URL(
|
|
@@ -114,11 +98,8 @@ var withClerkMiddleware = (options) => {
|
|
|
114
98
|
}
|
|
115
99
|
}
|
|
116
100
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
req = stripHandshakeCookiesAndParams(req, [Cookies.Handshake, Cookies.HandshakeNonce]);
|
|
120
|
-
}
|
|
121
|
-
const requestState = await clerkClient2.authenticateRequest(req, {
|
|
101
|
+
const req = fastifyRequestToRequest(fastifyRequest);
|
|
102
|
+
const requestState = await clerkClient.authenticateRequest(req, {
|
|
122
103
|
...options,
|
|
123
104
|
secretKey,
|
|
124
105
|
publishableKey,
|
|
@@ -126,23 +107,19 @@ var withClerkMiddleware = (options) => {
|
|
|
126
107
|
acceptsToken: "any"
|
|
127
108
|
});
|
|
128
109
|
requestState.headers.forEach((value, key) => reply.header(key, value));
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
throw new Error("Clerk: handshake status without redirect");
|
|
135
|
-
}
|
|
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");
|
|
136
115
|
}
|
|
137
116
|
fastifyRequest.auth = requestState.toAuth();
|
|
138
|
-
fastifyRequest.clerk = clerkClient2;
|
|
139
117
|
};
|
|
140
118
|
};
|
|
141
119
|
|
|
142
120
|
// src/clerkPlugin.ts
|
|
143
121
|
var plugin = (instance, opts, done) => {
|
|
144
122
|
instance.decorateRequest("auth", null);
|
|
145
|
-
instance.decorateRequest("clerk", null);
|
|
146
123
|
const hookName = opts.hookName || "preHandler";
|
|
147
124
|
if (!ALLOWED_HOOKS.includes(hookName)) {
|
|
148
125
|
throw new Error(`Unsupported hookName: ${hookName}`);
|
|
@@ -183,18 +160,6 @@ var getAuth = ((req, options) => {
|
|
|
183
160
|
}
|
|
184
161
|
return getAuthObjectForAcceptedToken({ authObject: authReq.auth, acceptsToken: options?.acceptsToken });
|
|
185
162
|
});
|
|
186
|
-
|
|
187
|
-
// src/clerkClient.ts
|
|
188
|
-
import { createClerkClient as createClerkClient2 } from "@clerk/backend";
|
|
189
|
-
var clerkClient = createClerkClient2({
|
|
190
|
-
secretKey: SECRET_KEY,
|
|
191
|
-
machineSecretKey: MACHINE_SECRET_KEY,
|
|
192
|
-
apiUrl: API_URL,
|
|
193
|
-
apiVersion: API_VERSION,
|
|
194
|
-
jwtKey: JWT_KEY,
|
|
195
|
-
userAgent: `${"@clerk/fastify"}@${"3.1.28-snapshot.v20260516134813"}`,
|
|
196
|
-
sdkMetadata: SDK_METADATA
|
|
197
|
-
});
|
|
198
163
|
export {
|
|
199
164
|
clerkClient,
|
|
200
165
|
clerkPlugin,
|
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/constants.ts","../src/getAuth.ts","../src/errors.ts","../src/clerkClient.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 // eslint-disable-next-line @typescript-eslint/no-explicit-any\n instance.decorateRequest('clerk', null as any);\n\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 { ClerkClient, ClerkOptions } from '@clerk/backend';\nimport type { ShouldProxyFn } from '@clerk/shared/proxy';\n\ndeclare module 'fastify' {\n interface FastifyRequest {\n clerk: ClerkClient;\n }\n}\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 { createClerkClient } from '@clerk/backend';\nimport { 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 * as constants from './constants';\nimport type { ClerkFastifyOptions } from './types';\nimport { fastifyRequestToRequest, requestToProxyRequest } from './utils';\n\nfunction stripHandshakeCookiesAndParams(req: Request, cookieNames: string[]): Request {\n const url = new URL(req.url);\n for (const name of cookieNames) {\n url.searchParams.delete(name);\n }\n\n const headers = new Headers(req.headers);\n const cookieHeader = headers.get('cookie');\n if (cookieHeader) {\n const filtered = cookieHeader\n .split(';')\n .map(c => c.trim())\n .filter(c => !cookieNames.some(name => c === name || c.startsWith(`${name}=`)))\n .join('; ');\n if (filtered) {\n headers.set('cookie', filtered);\n } else {\n headers.delete('cookie');\n }\n }\n\n return new Request(url.toString(), { method: req.method, headers });\n}\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 publishableKey = options.publishableKey || constants.PUBLISHABLE_KEY;\n const secretKey = options.secretKey || constants.SECRET_KEY;\n const clerkClient = createClerkClient({\n ...options,\n publishableKey,\n secretKey,\n machineSecretKey: options.machineSecretKey || constants.MACHINE_SECRET_KEY,\n apiUrl: options.apiUrl || constants.API_URL,\n apiVersion: options.apiVersion || constants.API_VERSION,\n jwtKey: options.jwtKey || constants.JWT_KEY,\n userAgent: options.userAgent || `${constants.SDK_METADATA.name}@${constants.SDK_METADATA.version}`,\n sdkMetadata: options.sdkMetadata || constants.SDK_METADATA,\n });\n const enableHandshake = options.enableHandshake ?? true;\n\n return async (fastifyRequest: FastifyRequest, reply: FastifyReply) => {\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 let req = fastifyRequestToRequest(fastifyRequest);\n\n if (!enableHandshake) {\n req = stripHandshakeCookiesAndParams(req, [constants.Cookies.Handshake, constants.Cookies.HandshakeNonce]);\n }\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 fastifyRequest.clerk = clerkClient;\n };\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","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"],"mappings":";;;;;;AAAA,cAAc;;;ACCd,OAAO,QAAQ;;;ACQR,IAAM,gBAAgB,CAAC,aAAa,YAAY;;;ACTvD,SAAS,yBAAyB;AAClC,SAAS,kBAAkB;AAC3B,SAAS,uBAAuB,oBAAoB,4BAA4B;AAEhF,SAAS,gBAAgB;;;ACJzB,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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AAgB;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;;;AEdA,SAAS,qBAAAE,0BAAyB;AAI3B,IAAM,cAAcC,mBAAkB;AAAA,EAC3C,WAAW;AAAA,EACX,kBAAkB;AAAA,EAClB,QAAQ;AAAA,EACR,YAAY;AAAA,EACZ,QAAQ;AAAA,EACR,WAAW,GAAG,gBAAY,IAAI,iCAAe;AAAA,EAC7C,aAAa;AACf,CAAC;","names":["Headers","clerkClient","Headers","createClerkClient","createClerkClient"]}
|
|
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;AAAA,EAC7C,aAAa;AACf,CAAC;;;ADFM,IAAM,sBAAsB,CAAC,YAAiC;AACnE,QAAM,mBAAmB,QAAQ;AACjC,QAAM,YAAY,qBAAqB,kBAAkB,QAAQ,kBAAkB,KAAK;AAExF,SAAO,OAAO,gBAAgC,UAAwB;AACpE,UAAM,iBAAiB,QAAQ,kBAA4B;AAC3D,UAAM,YAAY,QAAQ,aAAuB;AAGjD,QAAI,mBAAmB,QAAQ;AAC/B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CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clerk/fastify",
|
|
3
|
-
"version": "3.1.28
|
|
3
|
+
"version": "3.1.28",
|
|
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.10
|
|
66
|
-
"@clerk/shared": "4.12.1
|
|
65
|
+
"@clerk/backend": "^3.4.10",
|
|
66
|
+
"@clerk/shared": "^4.12.1"
|
|
67
67
|
},
|
|
68
68
|
"devDependencies": {
|
|
69
69
|
"fastify": "^5.8.5"
|