From d47897c5cfbdd0698c081704f222f9cade0cde22 Mon Sep 17 00:00:00 2001 From: Dimitris Klouvas Date: Sat, 18 Nov 2023 02:18:06 +0200 Subject: [PATCH 01/15] chore(backend): Drop conditional import of crypto --- packages/backend/package.json | 9 --------- packages/backend/src/runtime/browser/crypto.mjs | 1 - packages/backend/src/runtime/index.ts | 16 +++++++++++----- packages/backend/src/runtime/node/crypto.js | 11 ----------- packages/backend/tsup.config.ts | 1 - 5 files changed, 11 insertions(+), 27 deletions(-) delete mode 100644 packages/backend/src/runtime/browser/crypto.mjs delete mode 100644 packages/backend/src/runtime/node/crypto.js diff --git a/packages/backend/package.json b/packages/backend/package.json index 3ae46239cd0..516d760fdcc 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -12,15 +12,6 @@ "directory": "packages/backend" }, "license": "MIT", - "imports": { - "#crypto": { - "edge-light": "./dist/runtime/browser/crypto.mjs", - "worker": "./dist/runtime/browser/crypto.mjs", - "browser": "./dist/runtime/browser/crypto.mjs", - "node": "./dist/runtime/node/crypto.js", - "default": "./dist/runtime/browser/crypto.mjs" - } - }, "main": "./dist/index.js", "module": "./dist/esm/index.js", "types": "./dist/types/index.d.ts", diff --git a/packages/backend/src/runtime/browser/crypto.mjs b/packages/backend/src/runtime/browser/crypto.mjs deleted file mode 100644 index fe98f115451..00000000000 --- a/packages/backend/src/runtime/browser/crypto.mjs +++ /dev/null @@ -1 +0,0 @@ -export default crypto; diff --git a/packages/backend/src/runtime/index.ts b/packages/backend/src/runtime/index.ts index 8de0004e8a2..0c0b28d9f27 100644 --- a/packages/backend/src/runtime/index.ts +++ b/packages/backend/src/runtime/index.ts @@ -11,12 +11,18 @@ * * TODO: Support TS runtime modules */ - -// @ts-ignore - These are package subpaths -import crypto from '#crypto'; +let webcrypto; +try { + webcrypto = require('node:crypto'); + if (!webcrypto) { + webcrypto = globalThis.crypto; + } +} catch (e) { + webcrypto = globalThis.crypto; +} type Runtime = { - crypto: Crypto; + crypto: typeof webcrypto; fetch: typeof globalThis.fetch; AbortController: typeof globalThis.AbortController; Blob: typeof globalThis.Blob; @@ -36,7 +42,7 @@ const globalFetch = fetch.bind(globalThis); // DO NOT CHANGE: Runtime needs to be imported as a default export so that we can stub its dependencies with Sinon.js // For more information refer to https://sinonjs.org/how-to/stub-dependency/ const runtime: Runtime = { - crypto, + crypto: webcrypto, fetch: globalFetch, AbortController: globalThis.AbortController, Blob: globalThis.Blob, diff --git a/packages/backend/src/runtime/node/crypto.js b/packages/backend/src/runtime/node/crypto.js deleted file mode 100644 index e192b979353..00000000000 --- a/packages/backend/src/runtime/node/crypto.js +++ /dev/null @@ -1,11 +0,0 @@ -let webcrypto; -try { - webcrypto = require('node:crypto').webcrypto; - if (!webcrypto) { - webcrypto = global.crypto; - } -} catch (e) { - webcrypto = global.crypto; -} - -module.exports = webcrypto; diff --git a/packages/backend/tsup.config.ts b/packages/backend/tsup.config.ts index 294370b7aff..04e243ceb06 100644 --- a/packages/backend/tsup.config.ts +++ b/packages/backend/tsup.config.ts @@ -18,7 +18,6 @@ export default defineConfig(overrideOptions => { PACKAGE_VERSION: `"${version}"`, __DEV__: `${isWatch}`, }, - external: ['#crypto'], legacyOutput: true, bundle: true, clean: true, From 3bb02b3ea0af2dd1f8204a06869ae3a367750b1a Mon Sep 17 00:00:00 2001 From: Dimitris Klouvas Date: Sat, 18 Nov 2023 02:19:31 +0200 Subject: [PATCH 02/15] chore(backend): Sort imports in edge-runtime runner --- packages/backend/tests/edge-runtime/run.mjs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/backend/tests/edge-runtime/run.mjs b/packages/backend/tests/edge-runtime/run.mjs index c46f836a4b2..108891c2e08 100644 --- a/packages/backend/tests/edge-runtime/run.mjs +++ b/packages/backend/tests/edge-runtime/run.mjs @@ -1,8 +1,9 @@ -import { EdgeRuntime } from 'edge-runtime'; -import { exit } from 'node:process'; import fs from 'node:fs'; -import * as url from 'url'; +import { exit } from 'node:process'; + +import { EdgeRuntime } from 'edge-runtime'; import * as path from 'path'; +import * as url from 'url'; const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); From 168522202260b2aca35e7732a8da144ed3cba398 Mon Sep 17 00:00:00 2001 From: Dimitris Klouvas Date: Sat, 18 Nov 2023 03:48:23 +0200 Subject: [PATCH 03/15] chore(backend): Move tests & convert runtime folder to module --- .../src/{ => __tests__}/exports.test.ts | 2 +- .../src/{ => __tests__}/redirections.test.ts | 2 +- .../backend/src/{ => __tests__}/utils.test.ts | 2 +- .../src/api/{ => __tests__}/factory.test.ts | 14 +++--- .../src/fixtures/{responses => }/email.json | 0 .../src/fixtures/{responses => }/user.json | 0 .../src/{runtime/index.ts => runtime.ts} | 0 .../{ => __tests__}/authObjects.test.ts | 2 +- .../tokens/{ => __tests__}/factory.test.ts | 4 +- .../src/tokens/{ => __tests__}/keys.test.ts | 18 +++++-- .../tokens/{ => __tests__}/request.test.ts | 22 ++++----- .../src/tokens/{ => __tests__}/verify.test.ts | 8 ++-- .../jwt/{ => __tests__}/assertions.test.ts | 2 +- .../jwt/{ => __tests__}/cryptoKeys.test.ts | 4 +- .../jwt/{ => __tests__}/signJwt.test.ts | 6 +-- .../jwt/{ => __tests__}/verifyJwt.test.ts | 4 +- packages/backend/src/tokens/keys.ts | 2 +- .../src/util/{ => __tests__}/path.test.ts | 2 +- .../src/util/{ => __tests__}/request.test.ts | 2 +- packages/backend/tests/suites.ts | 47 ++++++++++--------- packages/backend/tsconfig.json | 2 +- packages/backend/tsconfig.test.json | 4 +- 22 files changed, 78 insertions(+), 71 deletions(-) rename packages/backend/src/{ => __tests__}/exports.test.ts (97%) rename packages/backend/src/{ => __tests__}/redirections.test.ts (99%) rename packages/backend/src/{ => __tests__}/utils.test.ts (98%) rename packages/backend/src/api/{ => __tests__}/factory.test.ts (96%) rename packages/backend/src/fixtures/{responses => }/email.json (100%) rename packages/backend/src/fixtures/{responses => }/user.json (100%) rename packages/backend/src/{runtime/index.ts => runtime.ts} (100%) rename packages/backend/src/tokens/{ => __tests__}/authObjects.test.ts (96%) rename packages/backend/src/tokens/{ => __tests__}/factory.test.ts (96%) rename packages/backend/src/tokens/{ => __tests__}/keys.test.ts (95%) rename packages/backend/src/tokens/{ => __tests__}/request.test.ts (97%) rename packages/backend/src/tokens/{ => __tests__}/verify.test.ts (89%) rename packages/backend/src/tokens/jwt/{ => __tests__}/assertions.test.ts (99%) rename packages/backend/src/tokens/jwt/{ => __tests__}/cryptoKeys.test.ts (95%) rename packages/backend/src/tokens/jwt/{ => __tests__}/signJwt.test.ts (91%) rename packages/backend/src/tokens/jwt/{ => __tests__}/verifyJwt.test.ts (97%) rename packages/backend/src/util/{ => __tests__}/path.test.ts (88%) rename packages/backend/src/util/{ => __tests__}/request.test.ts (99%) diff --git a/packages/backend/src/exports.test.ts b/packages/backend/src/__tests__/exports.test.ts similarity index 97% rename from packages/backend/src/exports.test.ts rename to packages/backend/src/__tests__/exports.test.ts index 97bed0993d5..fa5233c0d27 100644 --- a/packages/backend/src/exports.test.ts +++ b/packages/backend/src/__tests__/exports.test.ts @@ -1,6 +1,6 @@ import type QUnit from 'qunit'; -import * as publicExports from './index'; +import * as publicExports from '../index'; export default (QUnit: QUnit) => { const { module, test } = QUnit; diff --git a/packages/backend/src/redirections.test.ts b/packages/backend/src/__tests__/redirections.test.ts similarity index 99% rename from packages/backend/src/redirections.test.ts rename to packages/backend/src/__tests__/redirections.test.ts index 0085781c7fe..d0054cca6fa 100644 --- a/packages/backend/src/redirections.test.ts +++ b/packages/backend/src/__tests__/redirections.test.ts @@ -1,7 +1,7 @@ import type QUnit from 'qunit'; import sinon from 'sinon'; -import { redirect } from './redirections'; +import { redirect } from '../redirections'; export default (QUnit: QUnit) => { const { module, test } = QUnit; diff --git a/packages/backend/src/utils.test.ts b/packages/backend/src/__tests__/utils.test.ts similarity index 98% rename from packages/backend/src/utils.test.ts rename to packages/backend/src/__tests__/utils.test.ts index d76615d66a6..de8e53dd281 100644 --- a/packages/backend/src/utils.test.ts +++ b/packages/backend/src/__tests__/utils.test.ts @@ -1,6 +1,6 @@ import type QUnit from 'qunit'; -import { buildOrigin, buildRequestUrl } from './utils'; +import { buildOrigin, buildRequestUrl } from '../utils'; export default (QUnit: QUnit) => { const { module, test } = QUnit; diff --git a/packages/backend/src/api/factory.test.ts b/packages/backend/src/api/__tests__/factory.test.ts similarity index 96% rename from packages/backend/src/api/factory.test.ts rename to packages/backend/src/api/__tests__/factory.test.ts index b5e0612c35f..075ac4441d5 100644 --- a/packages/backend/src/api/factory.test.ts +++ b/packages/backend/src/api/__tests__/factory.test.ts @@ -1,12 +1,14 @@ import type QUnit from 'qunit'; import sinon from 'sinon'; -import emailJson from '../fixtures/responses/email.json'; -import userJson from '../fixtures/responses/user.json'; -import runtime from '../runtime'; -import { assertErrorResponse, assertResponse } from '../util/assertResponse'; -import { jsonError, jsonNotOk, jsonOk, jsonPaginatedOk } from '../util/mockFetch'; -import { createBackendApiClient } from './factory'; +// @ts-ignore +import emailJson from '../../fixtures/email.json'; +// @ts-ignore +import userJson from '../../fixtures/user.json'; +import runtime from '../../runtime'; +import { assertErrorResponse, assertResponse } from '../../util/assertResponse'; +import { jsonError, jsonNotOk, jsonOk, jsonPaginatedOk } from '../../util/mockFetch'; +import { createBackendApiClient } from '../factory'; export default (QUnit: QUnit) => { const { module, test } = QUnit; diff --git a/packages/backend/src/fixtures/responses/email.json b/packages/backend/src/fixtures/email.json similarity index 100% rename from packages/backend/src/fixtures/responses/email.json rename to packages/backend/src/fixtures/email.json diff --git a/packages/backend/src/fixtures/responses/user.json b/packages/backend/src/fixtures/user.json similarity index 100% rename from packages/backend/src/fixtures/responses/user.json rename to packages/backend/src/fixtures/user.json diff --git a/packages/backend/src/runtime/index.ts b/packages/backend/src/runtime.ts similarity index 100% rename from packages/backend/src/runtime/index.ts rename to packages/backend/src/runtime.ts diff --git a/packages/backend/src/tokens/authObjects.test.ts b/packages/backend/src/tokens/__tests__/authObjects.test.ts similarity index 96% rename from packages/backend/src/tokens/authObjects.test.ts rename to packages/backend/src/tokens/__tests__/authObjects.test.ts index 0f29fbefcc8..039276bf061 100644 --- a/packages/backend/src/tokens/authObjects.test.ts +++ b/packages/backend/src/tokens/__tests__/authObjects.test.ts @@ -1,6 +1,6 @@ import type QUnit from 'qunit'; -import { makeAuthObjectSerializable, signedOutAuthObject } from './authObjects'; +import { makeAuthObjectSerializable, signedOutAuthObject } from '../authObjects'; export default (QUnit: QUnit) => { const { module, test } = QUnit; diff --git a/packages/backend/src/tokens/factory.test.ts b/packages/backend/src/tokens/__tests__/factory.test.ts similarity index 96% rename from packages/backend/src/tokens/factory.test.ts rename to packages/backend/src/tokens/__tests__/factory.test.ts index 91831161076..63c25c79589 100644 --- a/packages/backend/src/tokens/factory.test.ts +++ b/packages/backend/src/tokens/__tests__/factory.test.ts @@ -1,7 +1,7 @@ import type QUnit from 'qunit'; -import type { ApiClient } from '../api'; -import { createAuthenticateRequest } from './factory'; +import type { ApiClient } from '../../api'; +import { createAuthenticateRequest } from '../factory'; const TEST_PK = 'pk_test_Y2xlcmsuaW5jbHVkZWQua2F0eWRpZC05Mi5sY2wuZGV2JA'; diff --git a/packages/backend/src/tokens/keys.test.ts b/packages/backend/src/tokens/__tests__/keys.test.ts similarity index 95% rename from packages/backend/src/tokens/keys.test.ts rename to packages/backend/src/tokens/__tests__/keys.test.ts index 682d95d7cf1..fa66eed1235 100644 --- a/packages/backend/src/tokens/keys.test.ts +++ b/packages/backend/src/tokens/__tests__/keys.test.ts @@ -1,11 +1,19 @@ import type QUnit from 'qunit'; import sinon from 'sinon'; -import runtime from '../runtime'; -import { jsonError, jsonOk } from '../util/mockFetch'; -import { TokenVerificationError, TokenVerificationErrorAction, TokenVerificationErrorReason } from './errors'; -import { mockJwks, mockJwtPayload, mockPEMJwk, mockPEMJwtKey, mockPEMKey, mockRsaJwk, mockRsaJwkKid } from './fixtures'; -import { loadClerkJWKFromLocal, loadClerkJWKFromRemote } from './keys'; +import runtime from '../../runtime'; +import { jsonError, jsonOk } from '../../util/mockFetch'; +import { TokenVerificationError, TokenVerificationErrorAction, TokenVerificationErrorReason } from '../errors'; +import { + mockJwks, + mockJwtPayload, + mockPEMJwk, + mockPEMJwtKey, + mockPEMKey, + mockRsaJwk, + mockRsaJwkKid, +} from '../fixtures'; +import { loadClerkJWKFromLocal, loadClerkJWKFromRemote } from '../keys'; export default (QUnit: QUnit) => { const { module, test } = QUnit; diff --git a/packages/backend/src/tokens/request.test.ts b/packages/backend/src/tokens/__tests__/request.test.ts similarity index 97% rename from packages/backend/src/tokens/request.test.ts rename to packages/backend/src/tokens/__tests__/request.test.ts index 66dc24b3e36..67b5db6569c 100644 --- a/packages/backend/src/tokens/request.test.ts +++ b/packages/backend/src/tokens/__tests__/request.test.ts @@ -1,13 +1,13 @@ import type QUnit from 'qunit'; import sinon from 'sinon'; -import runtime from '../runtime'; -import { jsonOk } from '../util/mockFetch'; -import { AuthErrorReason, type AuthReason, AuthStatus, type RequestState } from './authStatus'; -import { TokenVerificationErrorReason } from './errors'; -import { mockInvalidSignatureJwt, mockJwks, mockJwt, mockJwtPayload, mockMalformedJwt } from './fixtures'; -import type { AuthenticateRequestOptions } from './request'; -import { authenticateRequest, loadOptionsFromHeaders } from './request'; +import runtime from '../../runtime'; +import { jsonOk } from '../../util/mockFetch'; +import { AuthErrorReason, type AuthReason, AuthStatus, type RequestState } from '../authStatus'; +import { TokenVerificationErrorReason } from '../errors'; +import { mockInvalidSignatureJwt, mockJwks, mockJwt, mockJwtPayload, mockMalformedJwt } from '../fixtures'; +import type { AuthenticateRequestOptions } from '../request'; +import { authenticateRequest, loadOptionsFromHeaders } from '../request'; function assertSignedOut( assert, @@ -181,12 +181,8 @@ export default (QUnit: QUnit) => { test('returns signed out state if jwk fails to load from remote', async assert => { fakeFetch.onCall(0).returns(jsonOk({})); - const requestState = await authenticateRequest( - mockRequestWithHeaderAuth(), - mockOptions({ - skipJwksCache: false, - }), - ); + + const requestState = await authenticateRequest(mockRequestWithHeaderAuth(), mockOptions()); const errMessage = 'The JWKS endpoint did not contain any signing keys. Contact support@clerk.com. Contact support@clerk.com (reason=jwk-remote-failed-to-load, token-carrier=header)'; diff --git a/packages/backend/src/tokens/verify.test.ts b/packages/backend/src/tokens/__tests__/verify.test.ts similarity index 89% rename from packages/backend/src/tokens/verify.test.ts rename to packages/backend/src/tokens/__tests__/verify.test.ts index c9d89e279a3..677adc491c6 100644 --- a/packages/backend/src/tokens/verify.test.ts +++ b/packages/backend/src/tokens/__tests__/verify.test.ts @@ -1,10 +1,10 @@ import type QUnit from 'qunit'; import sinon from 'sinon'; -import runtime from '../runtime'; -import { jsonOk } from '../util/mockFetch'; -import { mockJwks, mockJwt, mockJwtPayload } from './fixtures'; -import { verifyToken } from './verify'; +import runtime from '../../runtime'; +import { jsonOk } from '../../util/mockFetch'; +import { mockJwks, mockJwt, mockJwtPayload } from '../fixtures'; +import { verifyToken } from '../verify'; export default (QUnit: QUnit) => { const { module, test } = QUnit; diff --git a/packages/backend/src/tokens/jwt/assertions.test.ts b/packages/backend/src/tokens/jwt/__tests__/assertions.test.ts similarity index 99% rename from packages/backend/src/tokens/jwt/assertions.test.ts rename to packages/backend/src/tokens/jwt/__tests__/assertions.test.ts index 97296209867..d4c27350d1a 100644 --- a/packages/backend/src/tokens/jwt/assertions.test.ts +++ b/packages/backend/src/tokens/jwt/__tests__/assertions.test.ts @@ -10,7 +10,7 @@ import { assertHeaderType, assertIssuedAtClaim, assertSubClaim, -} from './assertions'; +} from '../assertions'; export default (QUnit: QUnit) => { const { module, test } = QUnit; diff --git a/packages/backend/src/tokens/jwt/cryptoKeys.test.ts b/packages/backend/src/tokens/jwt/__tests__/cryptoKeys.test.ts similarity index 95% rename from packages/backend/src/tokens/jwt/cryptoKeys.test.ts rename to packages/backend/src/tokens/jwt/__tests__/cryptoKeys.test.ts index ad707d3c1f6..3662cd70ef8 100644 --- a/packages/backend/src/tokens/jwt/cryptoKeys.test.ts +++ b/packages/backend/src/tokens/jwt/__tests__/cryptoKeys.test.ts @@ -1,7 +1,7 @@ import type QUnit from 'qunit'; -import { pemEncodedPublicKey, pemEncodedSignKey, publicJwks, signingJwks } from '../fixtures'; -import { importKey } from './cryptoKeys'; +import { pemEncodedPublicKey, pemEncodedSignKey, publicJwks, signingJwks } from '../../fixtures'; +import { importKey } from '../cryptoKeys'; export default (QUnit: QUnit) => { const { module, test } = QUnit; diff --git a/packages/backend/src/tokens/jwt/signJwt.test.ts b/packages/backend/src/tokens/jwt/__tests__/signJwt.test.ts similarity index 91% rename from packages/backend/src/tokens/jwt/signJwt.test.ts rename to packages/backend/src/tokens/jwt/__tests__/signJwt.test.ts index 36c7db01f7e..22fa071cd3b 100644 --- a/packages/backend/src/tokens/jwt/signJwt.test.ts +++ b/packages/backend/src/tokens/jwt/__tests__/signJwt.test.ts @@ -8,9 +8,9 @@ import { pemEncodedSignKey, publicJwks, signingJwks, -} from '../fixtures'; -import { signJwt } from './signJwt'; -import { verifyJwt } from './verifyJwt'; +} from '../../fixtures'; +import { signJwt } from '../signJwt'; +import { verifyJwt } from '../verifyJwt'; export default (QUnit: QUnit) => { const { module, test } = QUnit; diff --git a/packages/backend/src/tokens/jwt/verifyJwt.test.ts b/packages/backend/src/tokens/jwt/__tests__/verifyJwt.test.ts similarity index 97% rename from packages/backend/src/tokens/jwt/verifyJwt.test.ts rename to packages/backend/src/tokens/jwt/__tests__/verifyJwt.test.ts index 8777cb655c1..39c9e9007f2 100644 --- a/packages/backend/src/tokens/jwt/verifyJwt.test.ts +++ b/packages/backend/src/tokens/jwt/__tests__/verifyJwt.test.ts @@ -10,8 +10,8 @@ import { publicJwks, signedJwt, someOtherPublicKey, -} from '../fixtures'; -import { decodeJwt, hasValidSignature, verifyJwt } from './verifyJwt'; +} from '../../fixtures'; +import { decodeJwt, hasValidSignature, verifyJwt } from '../verifyJwt'; export default (QUnit: QUnit) => { const { module, test } = QUnit; diff --git a/packages/backend/src/tokens/keys.ts b/packages/backend/src/tokens/keys.ts index f6de7713b23..c65d2cf32ab 100644 --- a/packages/backend/src/tokens/keys.ts +++ b/packages/backend/src/tokens/keys.ts @@ -147,7 +147,7 @@ export async function loadClerkJWKFromRemote({ if (!jwk) { const cacheValues = getCacheValues(); - const jwkKeys = cacheValues.map(jwk => jwk.kid).join(', '); + const jwkKeys = cacheValues.map(jwk => jwk.kid).sort().join(', '); throw new TokenVerificationError({ action: TokenVerificationErrorAction.ContactSupport, diff --git a/packages/backend/src/util/path.test.ts b/packages/backend/src/util/__tests__/path.test.ts similarity index 88% rename from packages/backend/src/util/path.test.ts rename to packages/backend/src/util/__tests__/path.test.ts index 8ae7d9f95ae..ce632d73a82 100644 --- a/packages/backend/src/util/path.test.ts +++ b/packages/backend/src/util/__tests__/path.test.ts @@ -1,6 +1,6 @@ import type QUnit from 'qunit'; -import { joinPaths } from './path'; +import { joinPaths } from '../path'; export default (QUnit: QUnit) => { const { module, test } = QUnit; diff --git a/packages/backend/src/util/request.test.ts b/packages/backend/src/util/__tests__/request.test.ts similarity index 99% rename from packages/backend/src/util/request.test.ts rename to packages/backend/src/util/__tests__/request.test.ts index e24528685ee..8ef856e58ad 100644 --- a/packages/backend/src/util/request.test.ts +++ b/packages/backend/src/util/__tests__/request.test.ts @@ -1,6 +1,6 @@ import type QUnit from 'qunit'; -import { checkCrossOrigin } from './request'; +import { checkCrossOrigin } from '../request'; export default (QUnit: QUnit) => { const { module, test } = QUnit; diff --git a/packages/backend/tests/suites.ts b/packages/backend/tests/suites.ts index c83309eedcc..40a7dfd84b8 100644 --- a/packages/backend/tests/suites.ts +++ b/packages/backend/tests/suites.ts @@ -1,38 +1,39 @@ // Import all suites // TODO: Automate this step using dynamic imports -import factoryTest from './dist/api/factory.test.js'; -import exportsTest from './dist/exports.test.js'; -import redirectTest from './dist/redirections.test.js'; -import authObjectsTest from './dist/tokens/authObjects.test.js'; -import tokenFactoryTest from './dist/tokens/factory.test.js'; -import jwtAssertionsTest from './dist/tokens/jwt/assertions.test.js'; -import cryptoKeysTest from './dist/tokens/jwt/cryptoKeys.test.js'; -import signJwtTest from './dist/tokens/jwt/signJwt.test.js'; -import verifyJwtTest from './dist/tokens/jwt/verifyJwt.test.js'; -import keysTest from './dist/tokens/keys.test.js'; -import requestTest from './dist/tokens/request.test.js'; -import verifyTest from './dist/tokens/verify.test.js'; -import pathTest from './dist/util/path.test.js'; -import utilRequestTest from './dist/util/request.test.js'; -import utilsTest from './dist/utils.test.js'; + +import authObjectsTest from './dist/tokens/__tests__/authObjects.test.js'; +import cryptoKeysTest from './dist/tokens/jwt/__tests__/cryptoKeys.test.js'; +import exportsTest from './dist/__tests__/exports.test.js'; +import factoryTest from './dist/api/__tests__/factory.test.js'; +import jwtAssertionsTest from './dist/tokens/jwt/__tests__/assertions.test.js'; +import keysTest from './dist/tokens/__tests__/keys.test.js'; +import pathTest from './dist/util/__tests__/path.test.js'; +import redirectTest from './dist/__tests__/redirections.test.js'; +import requestTest from './dist/tokens/__tests__/request.test.js'; +import signJwtTest from './dist/tokens/jwt/__tests__/signJwt.test.js'; +import tokenFactoryTest from './dist/tokens/__tests__/factory.test.js'; +import utilRequestTest from './dist/util/__tests__/request.test.js'; +import utilsTest from './dist/__tests__/utils.test.js'; +import verifyJwtTest from './dist/tokens/jwt/__tests__/verifyJwt.test.js'; +import verifyTest from './dist/tokens/__tests__/verify.test.js'; // Add them to the suite array const suites = [ authObjectsTest, + cryptoKeysTest, exportsTest, + factoryTest, jwtAssertionsTest, - requestTest, - utilRequestTest, keysTest, - verifyTest, pathTest, - verifyJwtTest, - signJwtTest, - cryptoKeysTest, - factoryTest, redirectTest, - utilsTest, + requestTest, + signJwtTest, tokenFactoryTest, + utilRequestTest, + utilsTest, + verifyJwtTest, + verifyTest, ]; export default suites; diff --git a/packages/backend/tsconfig.json b/packages/backend/tsconfig.json index 6c79dfa9a5b..e93e0bf52d4 100644 --- a/packages/backend/tsconfig.json +++ b/packages/backend/tsconfig.json @@ -22,5 +22,5 @@ "isolatedModules": true }, "include": ["src"], - "exclude": ["node_modules", "dist", "/src/runtime/*", "src/**/*.spec.ts", "src/**/*.test.ts", "src/tests"] + "exclude": ["node_modules", "dist", "/src/runtime/*", "src/**/__tests__/*.test.ts", "src/tests"] } diff --git a/packages/backend/tsconfig.test.json b/packages/backend/tsconfig.test.json index c1959296aa0..8c600f20dd7 100644 --- a/packages/backend/tsconfig.test.json +++ b/packages/backend/tsconfig.test.json @@ -12,6 +12,6 @@ "@clerk/shared/*": ["../shared/dist/*.js"] } }, - "include": ["src/**/*.test.ts"], - "exclude": ["node_modules", "dist", "src/__tests__"] + "include": ["src/**/__tests__"], + "exclude": ["node_modules", "dist"] } From ed5d208d4327d6d0bc9468af82c8ec83203d88d2 Mon Sep 17 00:00:00 2001 From: Dimitris Klouvas Date: Sat, 18 Nov 2023 04:00:41 +0200 Subject: [PATCH 04/15] chore(backend): Merge test utils to util/testUtils.ts module --- packages/backend/src/api/__tests__/factory.test.ts | 3 +-- packages/backend/src/tokens/__tests__/keys.test.ts | 2 +- packages/backend/src/tokens/__tests__/request.test.ts | 2 +- packages/backend/src/tokens/__tests__/verify.test.ts | 2 +- packages/backend/src/util/assertResponse.ts | 9 --------- .../backend/src/util/{mockFetch.ts => testUtils.ts} | 10 ++++++++++ 6 files changed, 14 insertions(+), 14 deletions(-) delete mode 100644 packages/backend/src/util/assertResponse.ts rename packages/backend/src/util/{mockFetch.ts => testUtils.ts} (76%) diff --git a/packages/backend/src/api/__tests__/factory.test.ts b/packages/backend/src/api/__tests__/factory.test.ts index 075ac4441d5..5e0d9ac9bbd 100644 --- a/packages/backend/src/api/__tests__/factory.test.ts +++ b/packages/backend/src/api/__tests__/factory.test.ts @@ -6,8 +6,7 @@ import emailJson from '../../fixtures/email.json'; // @ts-ignore import userJson from '../../fixtures/user.json'; import runtime from '../../runtime'; -import { assertErrorResponse, assertResponse } from '../../util/assertResponse'; -import { jsonError, jsonNotOk, jsonOk, jsonPaginatedOk } from '../../util/mockFetch'; +import { assertErrorResponse, assertResponse, jsonError, jsonNotOk, jsonOk, jsonPaginatedOk } from '../../util/testUtils'; import { createBackendApiClient } from '../factory'; export default (QUnit: QUnit) => { diff --git a/packages/backend/src/tokens/__tests__/keys.test.ts b/packages/backend/src/tokens/__tests__/keys.test.ts index fa66eed1235..e6b60afcdca 100644 --- a/packages/backend/src/tokens/__tests__/keys.test.ts +++ b/packages/backend/src/tokens/__tests__/keys.test.ts @@ -2,7 +2,7 @@ import type QUnit from 'qunit'; import sinon from 'sinon'; import runtime from '../../runtime'; -import { jsonError, jsonOk } from '../../util/mockFetch'; +import { jsonError, jsonOk } from '../../util/testUtils'; import { TokenVerificationError, TokenVerificationErrorAction, TokenVerificationErrorReason } from '../errors'; import { mockJwks, diff --git a/packages/backend/src/tokens/__tests__/request.test.ts b/packages/backend/src/tokens/__tests__/request.test.ts index 67b5db6569c..6a9b48d2a0d 100644 --- a/packages/backend/src/tokens/__tests__/request.test.ts +++ b/packages/backend/src/tokens/__tests__/request.test.ts @@ -2,7 +2,7 @@ import type QUnit from 'qunit'; import sinon from 'sinon'; import runtime from '../../runtime'; -import { jsonOk } from '../../util/mockFetch'; +import { jsonOk } from '../../util/testUtils'; import { AuthErrorReason, type AuthReason, AuthStatus, type RequestState } from '../authStatus'; import { TokenVerificationErrorReason } from '../errors'; import { mockInvalidSignatureJwt, mockJwks, mockJwt, mockJwtPayload, mockMalformedJwt } from '../fixtures'; diff --git a/packages/backend/src/tokens/__tests__/verify.test.ts b/packages/backend/src/tokens/__tests__/verify.test.ts index 677adc491c6..97f0d10936c 100644 --- a/packages/backend/src/tokens/__tests__/verify.test.ts +++ b/packages/backend/src/tokens/__tests__/verify.test.ts @@ -2,7 +2,7 @@ import type QUnit from 'qunit'; import sinon from 'sinon'; import runtime from '../../runtime'; -import { jsonOk } from '../../util/mockFetch'; +import { jsonOk } from '../../util/testUtils'; import { mockJwks, mockJwt, mockJwtPayload } from '../fixtures'; import { verifyToken } from '../verify'; diff --git a/packages/backend/src/util/assertResponse.ts b/packages/backend/src/util/assertResponse.ts deleted file mode 100644 index 8cdb760ff28..00000000000 --- a/packages/backend/src/util/assertResponse.ts +++ /dev/null @@ -1,9 +0,0 @@ -type ApiResponse = { data: T | null; errors: null | any[] }; -type SuccessApiResponse = { data: T; errors: null }; -type ErrorApiResponse = { data: null; errors: any[]; clerkTraceId: string; status: number; statusText: string }; -export function assertResponse(assert: Assert, resp: ApiResponse): asserts resp is SuccessApiResponse { - assert.equal(resp.errors, null); -} -export function assertErrorResponse(assert: Assert, resp: ApiResponse): asserts resp is ErrorApiResponse { - assert.notEqual(resp.errors, null); -} diff --git a/packages/backend/src/util/mockFetch.ts b/packages/backend/src/util/testUtils.ts similarity index 76% rename from packages/backend/src/util/mockFetch.ts rename to packages/backend/src/util/testUtils.ts index 8524a845fef..b12f26fcd7a 100644 --- a/packages/backend/src/util/mockFetch.ts +++ b/packages/backend/src/util/testUtils.ts @@ -1,5 +1,15 @@ import { constants } from '../constants'; +type ApiResponse = { data: T | null; errors: null | any[] }; +type SuccessApiResponse = { data: T; errors: null }; +type ErrorApiResponse = { data: null; errors: any[]; clerkTraceId: string; status: number; statusText: string }; +export function assertResponse(assert: Assert, resp: ApiResponse): asserts resp is SuccessApiResponse { + assert.equal(resp.errors, null); +} +export function assertErrorResponse(assert: Assert, resp: ApiResponse): asserts resp is ErrorApiResponse { + assert.notEqual(resp.errors, null); +} + export function jsonOk(body: unknown, status = 200) { // Mock response object that satisfies the window.Response interface const mockResponse = { From a8ad36cbb5b6f9c6ab9398496f84b757c43329c5 Mon Sep 17 00:00:00 2001 From: Dimitris Klouvas Date: Sat, 18 Nov 2023 04:07:06 +0200 Subject: [PATCH 05/15] chore(backend): Expose /errors subpath export --- packages/backend/package.json | 23 +++++++++++++++++ .../backend/src/__tests__/exports.test.ts | 15 +++++++++-- packages/backend/src/{tokens => }/errors.ts | 0 .../backend/src/tokens/__tests__/keys.test.ts | 2 +- .../src/tokens/__tests__/request.test.ts | 2 +- packages/backend/src/tokens/authStatus.ts | 2 +- packages/backend/src/tokens/handshake.ts | 2 +- packages/backend/src/tokens/index.ts | 1 - packages/backend/src/tokens/jwt/assertions.ts | 2 +- packages/backend/src/tokens/jwt/verifyJwt.ts | 2 +- packages/backend/src/tokens/keys.ts | 12 ++++----- packages/backend/src/tokens/request.ts | 4 +-- packages/backend/src/tokens/verify.ts | 2 +- packages/backend/tsup.config.ts | 25 +++++-------------- .../__snapshots__/exports.test.ts.snap | 2 -- .../__snapshots__/exports.test.ts.snap | 2 -- 16 files changed, 57 insertions(+), 41 deletions(-) rename packages/backend/src/{tokens => }/errors.ts (100%) diff --git a/packages/backend/package.json b/packages/backend/package.json index 516d760fdcc..4a6ad2eb032 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -12,6 +12,29 @@ "directory": "packages/backend" }, "license": "MIT", + "exports": { + ".": { + "import": { + "types": "./dist/index.d.mts", + "default": "./dist/index.mjs" + }, + "require": { + "types": "./dist/index.d.ts", + "default": "./dist/index.js" + } + }, + "./errors": { + "import": { + "types": "./dist/errors.d.mts", + "default": "./dist/errors.mjs" + }, + "require": { + "types": "./dist/errors.d.ts", + "default": "./dist/errors.js" + } + }, + "./package.json": "./package.json" + }, "main": "./dist/index.js", "module": "./dist/esm/index.js", "types": "./dist/types/index.d.ts", diff --git a/packages/backend/src/__tests__/exports.test.ts b/packages/backend/src/__tests__/exports.test.ts index fa5233c0d27..feed0701b6e 100644 --- a/packages/backend/src/__tests__/exports.test.ts +++ b/packages/backend/src/__tests__/exports.test.ts @@ -1,5 +1,6 @@ import type QUnit from 'qunit'; +import * as errorExports from '../errors'; import * as publicExports from '../index'; export default (QUnit: QUnit) => { @@ -29,8 +30,6 @@ export default (QUnit: QUnit) => { 'Session', 'SignInToken', 'Token', - 'TokenVerificationError', - 'TokenVerificationErrorReason', 'User', 'Verification', 'buildRequestUrl', @@ -55,4 +54,16 @@ export default (QUnit: QUnit) => { assert.deepEqual(Object.keys(publicExports).sort(), exportedApiKeys); }); }); + + module('subpath /errors exports', () => { + test('should not include a breaking change', assert => { + const exportedApiKeys = [ + 'TokenVerificationError', + 'TokenVerificationErrorAction', + 'TokenVerificationErrorCode', + 'TokenVerificationErrorReason', + ]; + assert.deepEqual(Object.keys(errorExports).sort(), exportedApiKeys); + }); + }); }; diff --git a/packages/backend/src/tokens/errors.ts b/packages/backend/src/errors.ts similarity index 100% rename from packages/backend/src/tokens/errors.ts rename to packages/backend/src/errors.ts diff --git a/packages/backend/src/tokens/__tests__/keys.test.ts b/packages/backend/src/tokens/__tests__/keys.test.ts index e6b60afcdca..eb60a7a2d3e 100644 --- a/packages/backend/src/tokens/__tests__/keys.test.ts +++ b/packages/backend/src/tokens/__tests__/keys.test.ts @@ -1,9 +1,9 @@ import type QUnit from 'qunit'; import sinon from 'sinon'; +import { TokenVerificationError, TokenVerificationErrorAction, TokenVerificationErrorReason } from '../../errors'; import runtime from '../../runtime'; import { jsonError, jsonOk } from '../../util/testUtils'; -import { TokenVerificationError, TokenVerificationErrorAction, TokenVerificationErrorReason } from '../errors'; import { mockJwks, mockJwtPayload, diff --git a/packages/backend/src/tokens/__tests__/request.test.ts b/packages/backend/src/tokens/__tests__/request.test.ts index 6a9b48d2a0d..a2a03e9ce82 100644 --- a/packages/backend/src/tokens/__tests__/request.test.ts +++ b/packages/backend/src/tokens/__tests__/request.test.ts @@ -1,10 +1,10 @@ import type QUnit from 'qunit'; import sinon from 'sinon'; +import { TokenVerificationErrorReason } from '../../errors'; import runtime from '../../runtime'; import { jsonOk } from '../../util/testUtils'; import { AuthErrorReason, type AuthReason, AuthStatus, type RequestState } from '../authStatus'; -import { TokenVerificationErrorReason } from '../errors'; import { mockInvalidSignatureJwt, mockJwks, mockJwt, mockJwtPayload, mockMalformedJwt } from '../fixtures'; import type { AuthenticateRequestOptions } from '../request'; import { authenticateRequest, loadOptionsFromHeaders } from '../request'; diff --git a/packages/backend/src/tokens/authStatus.ts b/packages/backend/src/tokens/authStatus.ts index a44e67f3a94..37552107b33 100644 --- a/packages/backend/src/tokens/authStatus.ts +++ b/packages/backend/src/tokens/authStatus.ts @@ -1,9 +1,9 @@ import type { JwtPayload } from '@clerk/types'; import { createBackendApiClient } from '../api'; +import type { TokenVerificationErrorReason } from '../errors'; import type { SignedInAuthObject, SignedInAuthObjectOptions, SignedOutAuthObject } from './authObjects'; import { signedInAuthObject, signedOutAuthObject } from './authObjects'; -import type { TokenVerificationErrorReason } from './errors'; export enum AuthStatus { SignedIn = 'signed-in', diff --git a/packages/backend/src/tokens/handshake.ts b/packages/backend/src/tokens/handshake.ts index 5c867e4a804..fb993ce6ffd 100644 --- a/packages/backend/src/tokens/handshake.ts +++ b/packages/backend/src/tokens/handshake.ts @@ -1,4 +1,4 @@ -import { TokenVerificationError, TokenVerificationErrorAction, TokenVerificationErrorReason } from './errors'; +import { TokenVerificationError, TokenVerificationErrorAction, TokenVerificationErrorReason } from '../errors'; import { decodeJwt, hasValidSignature, type VerifyJwtOptions } from './jwt'; import { assertHeaderAlgorithm, assertHeaderType } from './jwt/assertions'; import { loadClerkJWKFromLocal, loadClerkJWKFromRemote } from './keys'; diff --git a/packages/backend/src/tokens/index.ts b/packages/backend/src/tokens/index.ts index 50642216e4b..511068c5d83 100644 --- a/packages/backend/src/tokens/index.ts +++ b/packages/backend/src/tokens/index.ts @@ -1,7 +1,6 @@ export * from './authObjects'; export { AuthStatus } from './authStatus'; export type { RequestState } from './authStatus'; -export { TokenVerificationError, TokenVerificationErrorReason } from './errors'; export * from './factory'; export { debugRequestState } from './request'; export type { AuthenticateRequestOptions, OptionalVerifyTokenOptions } from './request'; diff --git a/packages/backend/src/tokens/jwt/assertions.ts b/packages/backend/src/tokens/jwt/assertions.ts index f91e9b2636f..4417597895c 100644 --- a/packages/backend/src/tokens/jwt/assertions.ts +++ b/packages/backend/src/tokens/jwt/assertions.ts @@ -1,4 +1,4 @@ -import { TokenVerificationError, TokenVerificationErrorAction, TokenVerificationErrorReason } from '../errors'; +import { TokenVerificationError, TokenVerificationErrorAction, TokenVerificationErrorReason } from '../../errors'; import { algs } from './algorithms'; export type IssuerResolver = string | ((iss: string) => boolean); diff --git a/packages/backend/src/tokens/jwt/verifyJwt.ts b/packages/backend/src/tokens/jwt/verifyJwt.ts index a0b4d218082..4e7c89b3fbc 100644 --- a/packages/backend/src/tokens/jwt/verifyJwt.ts +++ b/packages/backend/src/tokens/jwt/verifyJwt.ts @@ -1,10 +1,10 @@ import type { Jwt, JwtPayload } from '@clerk/types'; +import { TokenVerificationError, TokenVerificationErrorAction, TokenVerificationErrorReason } from '../../errors'; // DO NOT CHANGE: Runtime needs to be imported as a default export so that we can stub its dependencies with Sinon.js // For more information refer to https://sinonjs.org/how-to/stub-dependency/ import runtime from '../../runtime'; import { base64url } from '../../util/rfc4648'; -import { TokenVerificationError, TokenVerificationErrorAction, TokenVerificationErrorReason } from '../errors'; import { getCryptoAlgorithm } from './algorithms'; import { assertActivationClaim, diff --git a/packages/backend/src/tokens/keys.ts b/packages/backend/src/tokens/keys.ts index c65d2cf32ab..3411d140ff8 100644 --- a/packages/backend/src/tokens/keys.ts +++ b/packages/backend/src/tokens/keys.ts @@ -1,16 +1,16 @@ import { API_URL, API_VERSION, JWKS_CACHE_TTL_MS, MAX_CACHE_LAST_UPDATED_AT_SECONDS } from '../constants'; +import { + TokenVerificationError, + TokenVerificationErrorAction, + TokenVerificationErrorCode, + TokenVerificationErrorReason, +} from '../errors'; // DO NOT CHANGE: Runtime needs to be imported as a default export so that we can stub its dependencies with Sinon.js // For more information refer to https://sinonjs.org/how-to/stub-dependency/ import runtime from '../runtime'; import { joinPaths } from '../util/path'; import { getErrorObjectByCode } from '../util/request'; import { callWithRetry } from '../util/shared'; -import { - TokenVerificationError, - TokenVerificationErrorAction, - TokenVerificationErrorCode, - TokenVerificationErrorReason, -} from './errors'; type JsonWebKeyWithKid = JsonWebKey & { kid: string }; diff --git a/packages/backend/src/tokens/request.ts b/packages/backend/src/tokens/request.ts index fa65c36cf25..aa5fc506b28 100644 --- a/packages/backend/src/tokens/request.ts +++ b/packages/backend/src/tokens/request.ts @@ -2,13 +2,13 @@ import { parsePublishableKey } from '@clerk/shared/keys'; import type { JwtPayload } from '@clerk/types'; import { constants } from '../constants'; +import type { TokenCarrier } from '../errors'; +import { TokenVerificationError, TokenVerificationErrorReason } from '../errors'; import { assertValidSecretKey } from '../util/assertValidSecretKey'; import { buildRequest, stripAuthorizationHeader } from '../util/IsomorphicRequest'; import { isDevelopmentFromSecretKey } from '../util/shared'; import type { AuthStatusOptionsType, RequestState } from './authStatus'; import { AuthErrorReason, handshake, signedIn, signedOut } from './authStatus'; -import type { TokenCarrier } from './errors'; -import { TokenVerificationError, TokenVerificationErrorReason } from './errors'; import { verifyHandshakeToken } from './handshake'; import { decodeJwt } from './jwt'; import { verifyToken, type VerifyTokenOptions } from './verify'; diff --git a/packages/backend/src/tokens/verify.ts b/packages/backend/src/tokens/verify.ts index 9801ed30e42..ae31cce8abb 100644 --- a/packages/backend/src/tokens/verify.ts +++ b/packages/backend/src/tokens/verify.ts @@ -1,6 +1,6 @@ import type { JwtPayload } from '@clerk/types'; -import { TokenVerificationError, TokenVerificationErrorAction, TokenVerificationErrorReason } from './errors'; +import { TokenVerificationError, TokenVerificationErrorAction, TokenVerificationErrorReason } from '../errors'; import type { VerifyJwtOptions } from './jwt'; import { decodeJwt, verifyJwt } from './jwt'; import type { LoadClerkJWKFromRemoteOptions } from './keys'; diff --git a/packages/backend/tsup.config.ts b/packages/backend/tsup.config.ts index 04e243ceb06..56a1c04e72c 100644 --- a/packages/backend/tsup.config.ts +++ b/packages/backend/tsup.config.ts @@ -1,7 +1,5 @@ -import type { Options } from 'tsup'; import { defineConfig } from 'tsup'; -import { runAfterLast } from '../../scripts/utils'; // @ts-ignore import { name, version } from './package.json'; @@ -9,30 +7,19 @@ export default defineConfig(overrideOptions => { const isWatch = !!overrideOptions.watch; const shouldPublish = !!overrideOptions.env?.publish; - const common: Options = { - entry: ['src/index.ts'], - onSuccess: `cpy 'src/runtime/**/*.{mjs,js,cjs}' dist/runtime`, - sourcemap: true, + return { + entry: ['src/index.ts', 'src/errors.ts'], define: { PACKAGE_NAME: `"${name}"`, PACKAGE_VERSION: `"${version}"`, __DEV__: `${isWatch}`, }, - legacyOutput: true, + onSuccess: shouldPublish ? 'npm run publish:local' : undefined, + format: ['cjs', 'esm'], bundle: true, + sourcemap: true, clean: true, minify: false, + dts: true, }; - - const esm: Options = { - ...common, - format: 'esm', - }; - - const cjs: Options = { - ...common, - format: 'cjs', - }; - - return runAfterLast(['npm run build:declarations', shouldPublish && 'npm run publish:local'])(esm, cjs); }); diff --git a/packages/nextjs/src/server/__tests__/__snapshots__/exports.test.ts.snap b/packages/nextjs/src/server/__tests__/__snapshots__/exports.test.ts.snap index f14079b5887..6ed550f9e77 100644 --- a/packages/nextjs/src/server/__tests__/__snapshots__/exports.test.ts.snap +++ b/packages/nextjs/src/server/__tests__/__snapshots__/exports.test.ts.snap @@ -23,8 +23,6 @@ exports[`/server public exports should not include a breaking change 1`] = ` "Session", "SignInToken", "Token", - "TokenVerificationError", - "TokenVerificationErrorReason", "User", "Verification", "auth", diff --git a/packages/sdk-node/src/__tests__/__snapshots__/exports.test.ts.snap b/packages/sdk-node/src/__tests__/__snapshots__/exports.test.ts.snap index f847f40c962..1c1222dcb2d 100644 --- a/packages/sdk-node/src/__tests__/__snapshots__/exports.test.ts.snap +++ b/packages/sdk-node/src/__tests__/__snapshots__/exports.test.ts.snap @@ -25,8 +25,6 @@ exports[`module exports should not change unless explicitly set 1`] = ` "Session", "SignInToken", "Token", - "TokenVerificationError", - "TokenVerificationErrorReason", "User", "Verification", "allowlistIdentifiers", From 57fdb4a3be009f48946472c7ab74523665e236d3 Mon Sep 17 00:00:00 2001 From: Dimitris Klouvas Date: Thu, 14 Dec 2023 11:29:54 +0200 Subject: [PATCH 06/15] chore(backend): Drop unused build:declarations - depends on tsup dts --- packages/backend/package.json | 1 - packages/backend/tsconfig.declarations.json | 12 ------------ 2 files changed, 13 deletions(-) delete mode 100644 packages/backend/tsconfig.declarations.json diff --git a/packages/backend/package.json b/packages/backend/package.json index 4a6ad2eb032..96777599f23 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -45,7 +45,6 @@ "build": "tsup", "dev": "tsup --watch", "dev:publish": "npm run dev -- --env.publish", - "build:declarations": "tsc -p tsconfig.declarations.json", "publish:local": "npx yalc push --replace --sig", "build:lib": "tsup --env.NODE_ENV production", "build:tests": "tsc -p tsconfig.test.json", diff --git a/packages/backend/tsconfig.declarations.json b/packages/backend/tsconfig.declarations.json deleted file mode 100644 index c42a5efd18e..00000000000 --- a/packages/backend/tsconfig.declarations.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "extends": "./tsconfig.json", - "compilerOptions": { - "skipLibCheck": true, - "noEmit": false, - "declaration": true, - "emitDeclarationOnly": true, - "declarationMap": true, - "sourceMap": false, - "declarationDir": "./dist/types" - } -} From 4ec8b277796284061cf4fc8ccd74faed383635f4 Mon Sep 17 00:00:00 2001 From: Dimitris Klouvas Date: Thu, 14 Dec 2023 11:33:21 +0200 Subject: [PATCH 07/15] chore(backend): Apply format fixes --- packages/backend/src/api/__tests__/factory.test.ts | 9 ++++++++- packages/backend/src/tokens/keys.ts | 5 ++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/backend/src/api/__tests__/factory.test.ts b/packages/backend/src/api/__tests__/factory.test.ts index 5e0d9ac9bbd..93ff7b29add 100644 --- a/packages/backend/src/api/__tests__/factory.test.ts +++ b/packages/backend/src/api/__tests__/factory.test.ts @@ -6,7 +6,14 @@ import emailJson from '../../fixtures/email.json'; // @ts-ignore import userJson from '../../fixtures/user.json'; import runtime from '../../runtime'; -import { assertErrorResponse, assertResponse, jsonError, jsonNotOk, jsonOk, jsonPaginatedOk } from '../../util/testUtils'; +import { + assertErrorResponse, + assertResponse, + jsonError, + jsonNotOk, + jsonOk, + jsonPaginatedOk, +} from '../../util/testUtils'; import { createBackendApiClient } from '../factory'; export default (QUnit: QUnit) => { diff --git a/packages/backend/src/tokens/keys.ts b/packages/backend/src/tokens/keys.ts index 3411d140ff8..553da86f546 100644 --- a/packages/backend/src/tokens/keys.ts +++ b/packages/backend/src/tokens/keys.ts @@ -147,7 +147,10 @@ export async function loadClerkJWKFromRemote({ if (!jwk) { const cacheValues = getCacheValues(); - const jwkKeys = cacheValues.map(jwk => jwk.kid).sort().join(', '); + const jwkKeys = cacheValues + .map(jwk => jwk.kid) + .sort() + .join(', '); throw new TokenVerificationError({ action: TokenVerificationErrorAction.ContactSupport, From cafe54de291570303b26e0191d56ebc79576daea Mon Sep 17 00:00:00 2001 From: Dimitris Klouvas Date: Thu, 14 Dec 2023 14:40:37 +0200 Subject: [PATCH 08/15] fix(backend): Fix type issue with exported type resolution --- packages/backend/package.json | 2 -- packages/backend/src/index.ts | 11 +++++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/backend/package.json b/packages/backend/package.json index 96777599f23..287819242b6 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -36,8 +36,6 @@ "./package.json": "./package.json" }, "main": "./dist/index.js", - "module": "./dist/esm/index.js", - "types": "./dist/types/index.d.ts", "files": [ "dist" ], diff --git a/packages/backend/src/index.ts b/packages/backend/src/index.ts index 4b1bcafe9c5..e6273107ed1 100644 --- a/packages/backend/src/index.ts +++ b/packages/backend/src/index.ts @@ -2,7 +2,7 @@ import type { TelemetryCollectorOptions } from '@clerk/shared/telemetry'; import { TelemetryCollector } from '@clerk/shared/telemetry'; import type { SDKMetadata } from '@clerk/types'; -import type { CreateBackendApiOptions } from './api'; +import type { ApiClient, CreateBackendApiOptions } from './api'; import { createBackendApiClient } from './api'; import type { CreateAuthenticateRequestOptions } from './tokens'; import { createAuthenticateRequest } from './tokens'; @@ -25,7 +25,14 @@ export type ClerkOptions = CreateBackendApiOptions & > > & { sdkMetadata?: SDKMetadata; telemetry?: Pick }; -export function createClerkClient(options: ClerkOptions) { +// The current exported type resolves the following issue in packages importing createClerkClient +// TS4023: Exported variable 'clerkClient' has or is using name 'AuthErrorReason' from external module "/packages/backend/dist/index" but cannot be named. +export type ClerkClient = { + telemetry: TelemetryCollector; +} & ApiClient & + ReturnType; + +export function createClerkClient(options: ClerkOptions): ClerkClient { const opts = { ...options }; const apiClient = createBackendApiClient(opts); const requestState = createAuthenticateRequest({ options: opts, apiClient }); From 6c3463f542d5f56d3f750bf8a3a9b5e2bf6dcb26 Mon Sep 17 00:00:00 2001 From: Dimitris Klouvas Date: Thu, 14 Dec 2023 14:46:53 +0200 Subject: [PATCH 09/15] chore(clerk-sdk-node): Drop top-level re-exported BAPI client methods Removing those, resolves some issues with our type resolution and also makes our exposed API cleaner. ``` // Before import { users } from "@clerk/clerk-sdk-node" // After import { clerkClient } from "@clerk/clerk-sdk-node" clerkClient.users --- .../__snapshots__/exports.test.ts.snap | 10 ------- packages/sdk-node/src/index.ts | 28 ------------------- 2 files changed, 38 deletions(-) diff --git a/packages/sdk-node/src/__tests__/__snapshots__/exports.test.ts.snap b/packages/sdk-node/src/__tests__/__snapshots__/exports.test.ts.snap index 1c1222dcb2d..9128626cdbb 100644 --- a/packages/sdk-node/src/__tests__/__snapshots__/exports.test.ts.snap +++ b/packages/sdk-node/src/__tests__/__snapshots__/exports.test.ts.snap @@ -27,10 +27,8 @@ exports[`module exports should not change unless explicitly set 1`] = ` "Token", "User", "Verification", - "allowlistIdentifiers", "buildRequestUrl", "clerkClient", - "clients", "constants", "createAuthenticateRequest", "createClerkClient", @@ -40,23 +38,15 @@ exports[`module exports should not change unless explicitly set 1`] = ` "debugRequestState", "decodeJwt", "deserialize", - "domains", - "emailAddresses", - "emails", "hasValidSignature", - "invitations", "makeAuthObjectSerializable", - "organizations", - "phoneNumbers", "prunePrivateMetadata", "redirect", "requireAuth", "sanitizeAuthObject", - "sessions", "signJwt", "signedInAuthObject", "signedOutAuthObject", - "users", "verifyJwt", "verifyToken", "withAuth", diff --git a/packages/sdk-node/src/index.ts b/packages/sdk-node/src/index.ts index e235d41e9c0..7aa1b40e041 100644 --- a/packages/sdk-node/src/index.ts +++ b/packages/sdk-node/src/index.ts @@ -10,42 +10,14 @@ import type { WithAuthProp, } from './types'; -// eslint-disable-next-line import/export export * from '@clerk/backend'; /** * The order of these exports is important, as we want Clerk from clerk/sdk-node * to shadow the Clerk export from clerk/backend, because it needs to support * 2 additional apis: clerk.expressWithAuth, clerk.expressRequireAuth */ -// eslint-disable-next-line import/export export { clerkClient, ClerkExpressRequireAuth, ClerkExpressWithAuth, createClerkClient }; -const { - users, - sessions, - emailAddresses, - phoneNumbers, - emails, - invitations, - organizations, - clients, - allowlistIdentifiers, - domains, -} = clerkClient; - -export { - allowlistIdentifiers, - clients, - domains, - emailAddresses, - emails, - invitations, - organizations, - phoneNumbers, - sessions, - users, -}; - export type { ClerkMiddleware, ClerkMiddlewareOptions, LooseAuthProp, RequireAuthProp, StrictAuthProp, WithAuthProp }; export { createClerkExpressRequireAuth, createClerkExpressWithAuth }; From 1e78c6e312c10f268fa02634771969409dcec8fd Mon Sep 17 00:00:00 2001 From: Dimitris Klouvas Date: Thu, 14 Dec 2023 14:59:51 +0200 Subject: [PATCH 10/15] chore(backend): Drop `deserialize` from top-level export --- packages/backend/src/__tests__/exports.test.ts | 1 - packages/backend/src/api/resources/index.ts | 1 - 2 files changed, 2 deletions(-) diff --git a/packages/backend/src/__tests__/exports.test.ts b/packages/backend/src/__tests__/exports.test.ts index feed0701b6e..b1e16ba037b 100644 --- a/packages/backend/src/__tests__/exports.test.ts +++ b/packages/backend/src/__tests__/exports.test.ts @@ -39,7 +39,6 @@ export default (QUnit: QUnit) => { 'createIsomorphicRequest', 'debugRequestState', 'decodeJwt', - 'deserialize', 'hasValidSignature', 'makeAuthObjectSerializable', 'prunePrivateMetadata', diff --git a/packages/backend/src/api/resources/index.ts b/packages/backend/src/api/resources/index.ts index 936494c56eb..1ec010007d5 100644 --- a/packages/backend/src/api/resources/index.ts +++ b/packages/backend/src/api/resources/index.ts @@ -1,7 +1,6 @@ export * from './AllowlistIdentifier'; export * from './Client'; export * from './DeletedObject'; -export * from './Deserializer'; export * from './Email'; export * from './EmailAddress'; From 63c495a5e02e418495fba62c0479821aff0db123 Mon Sep 17 00:00:00 2001 From: Dimitris Klouvas Date: Thu, 14 Dec 2023 20:06:09 +0200 Subject: [PATCH 11/15] chore(backend): Expose /internal subpath export --- packages/backend/package.json | 10 ++++++ .../backend/src/__tests__/exports.test.ts | 33 ++++++++++++------- packages/backend/src/index.ts | 10 ++---- packages/backend/src/internal.ts | 28 ++++++++++++++++ packages/backend/src/tokens/index.ts | 6 ---- packages/backend/tsup.config.ts | 2 +- packages/fastify/src/constants.ts | 2 +- packages/fastify/src/getAuth.ts | 2 +- packages/fastify/src/withClerkMiddleware.ts | 2 +- packages/gatsby-plugin-clerk/src/ssr/types.ts | 3 +- packages/gatsby-plugin-clerk/src/ssr/utils.ts | 5 ++- .../src/ssr/withServerAuth.ts | 2 +- packages/nextjs/src/app-router/server/auth.ts | 2 +- .../__snapshots__/exports.test.ts.snap | 13 -------- .../nextjs/src/server/authMiddleware.test.ts | 2 +- packages/nextjs/src/server/authMiddleware.ts | 4 +-- packages/nextjs/src/server/getAuth.ts | 7 ++-- packages/nextjs/src/server/redirect.ts | 2 +- packages/nextjs/src/server/types.ts | 2 +- packages/nextjs/src/server/utils.ts | 4 +-- packages/remix/src/ssr/authenticateRequest.ts | 5 +-- packages/remix/src/ssr/getAuth.ts | 2 +- packages/remix/src/ssr/rootAuthLoader.ts | 2 +- packages/remix/src/ssr/types.ts | 3 +- packages/remix/src/ssr/utils.ts | 4 +-- .../__snapshots__/exports.test.ts.snap | 13 -------- .../src/__tests__/authenticateRequest.test.ts | 2 +- .../sdk-node/src/__tests__/middleware.test.ts | 2 +- packages/sdk-node/src/authenticateRequest.ts | 4 +-- .../sdk-node/src/clerkExpressRequireAuth.ts | 2 +- packages/sdk-node/src/clerkExpressWithAuth.ts | 2 +- packages/sdk-node/src/types.ts | 3 +- 32 files changed, 99 insertions(+), 86 deletions(-) create mode 100644 packages/backend/src/internal.ts delete mode 100644 packages/backend/src/tokens/index.ts diff --git a/packages/backend/package.json b/packages/backend/package.json index 287819242b6..21715b1ff7e 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -33,6 +33,16 @@ "default": "./dist/errors.js" } }, + "./internal": { + "import": { + "types": "./dist/internal.d.mts", + "default": "./dist/internal.mjs" + }, + "require": { + "types": "./dist/internal.d.ts", + "default": "./dist/internal.js" + } + }, "./package.json": "./package.json" }, "main": "./dist/index.js", diff --git a/packages/backend/src/__tests__/exports.test.ts b/packages/backend/src/__tests__/exports.test.ts index b1e16ba037b..3b21dc4e0c6 100644 --- a/packages/backend/src/__tests__/exports.test.ts +++ b/packages/backend/src/__tests__/exports.test.ts @@ -2,6 +2,7 @@ import type QUnit from 'qunit'; import * as errorExports from '../errors'; import * as publicExports from '../index'; +import * as internalExports from '../internal'; export default (QUnit: QUnit) => { const { module, test } = QUnit; @@ -10,7 +11,6 @@ export default (QUnit: QUnit) => { test('should not include a breaking change', assert => { const exportedApiKeys = [ 'AllowlistIdentifier', - 'AuthStatus', 'Client', 'DeletedObject', 'Email', @@ -32,21 +32,10 @@ export default (QUnit: QUnit) => { 'Token', 'User', 'Verification', - 'buildRequestUrl', - 'constants', - 'createAuthenticateRequest', 'createClerkClient', - 'createIsomorphicRequest', - 'debugRequestState', 'decodeJwt', 'hasValidSignature', - 'makeAuthObjectSerializable', - 'prunePrivateMetadata', - 'redirect', - 'sanitizeAuthObject', 'signJwt', - 'signedInAuthObject', - 'signedOutAuthObject', 'verifyJwt', 'verifyToken', ]; @@ -65,4 +54,24 @@ export default (QUnit: QUnit) => { assert.deepEqual(Object.keys(errorExports).sort(), exportedApiKeys); }); }); + + module('subpath /internal exports', () => { + test('should not include a breaking change', assert => { + const exportedApiKeys = [ + 'AuthStatus', + 'buildRequestUrl', + 'constants', + 'createAuthenticateRequest', + 'createIsomorphicRequest', + 'debugRequestState', + 'makeAuthObjectSerializable', + 'prunePrivateMetadata', + 'redirect', + 'sanitizeAuthObject', + 'signedInAuthObject', + 'signedOutAuthObject', + ]; + assert.deepEqual(Object.keys(internalExports).sort(), exportedApiKeys); + }); + }); }; diff --git a/packages/backend/src/index.ts b/packages/backend/src/index.ts index e6273107ed1..d9bf992bc8f 100644 --- a/packages/backend/src/index.ts +++ b/packages/backend/src/index.ts @@ -4,18 +4,12 @@ import type { SDKMetadata } from '@clerk/types'; import type { ApiClient, CreateBackendApiOptions } from './api'; import { createBackendApiClient } from './api'; -import type { CreateAuthenticateRequestOptions } from './tokens'; -import { createAuthenticateRequest } from './tokens'; - -export { createIsomorphicRequest } from './util/IsomorphicRequest'; +import type { CreateAuthenticateRequestOptions } from './tokens/factory'; +import { createAuthenticateRequest } from './tokens/factory'; export * from './api/resources'; -export * from './tokens'; export * from './tokens/jwt'; export * from './tokens/verify'; -export { constants } from './constants'; -export { redirect } from './redirections'; -export { buildRequestUrl } from './utils'; export type ClerkOptions = CreateBackendApiOptions & Partial< diff --git a/packages/backend/src/internal.ts b/packages/backend/src/internal.ts new file mode 100644 index 00000000000..7327bfe5a22 --- /dev/null +++ b/packages/backend/src/internal.ts @@ -0,0 +1,28 @@ +export { constants } from './constants'; +export { redirect } from './redirections'; +export { buildRequestUrl } from './utils'; + +export type { CreateAuthenticateRequestOptions } from './tokens/factory'; +export { createAuthenticateRequest } from './tokens/factory'; + +export { debugRequestState } from './tokens/request'; + +export type { AuthenticateRequestOptions, OptionalVerifyTokenOptions } from './tokens/request'; + +export type { + SignedInAuthObjectOptions, + SignedInAuthObject, + SignedOutAuthObject, + AuthObject, +} from './tokens/authObjects'; +export { + makeAuthObjectSerializable, + sanitizeAuthObject, + prunePrivateMetadata, + signedOutAuthObject, + signedInAuthObject, +} from './tokens/authObjects'; +export { createIsomorphicRequest } from './util/IsomorphicRequest'; + +export { AuthStatus } from './tokens/authStatus'; +export type { RequestState } from './tokens/authStatus'; diff --git a/packages/backend/src/tokens/index.ts b/packages/backend/src/tokens/index.ts deleted file mode 100644 index 511068c5d83..00000000000 --- a/packages/backend/src/tokens/index.ts +++ /dev/null @@ -1,6 +0,0 @@ -export * from './authObjects'; -export { AuthStatus } from './authStatus'; -export type { RequestState } from './authStatus'; -export * from './factory'; -export { debugRequestState } from './request'; -export type { AuthenticateRequestOptions, OptionalVerifyTokenOptions } from './request'; diff --git a/packages/backend/tsup.config.ts b/packages/backend/tsup.config.ts index 56a1c04e72c..e0bc62498da 100644 --- a/packages/backend/tsup.config.ts +++ b/packages/backend/tsup.config.ts @@ -8,7 +8,7 @@ export default defineConfig(overrideOptions => { const shouldPublish = !!overrideOptions.env?.publish; return { - entry: ['src/index.ts', 'src/errors.ts'], + entry: ['src/index.ts', 'src/errors.ts', 'src/internal.ts'], define: { PACKAGE_NAME: `"${name}"`, PACKAGE_VERSION: `"${version}"`, diff --git a/packages/fastify/src/constants.ts b/packages/fastify/src/constants.ts index 725f748d28e..1f0b27991ac 100644 --- a/packages/fastify/src/constants.ts +++ b/packages/fastify/src/constants.ts @@ -1,4 +1,4 @@ -import { constants } from '@clerk/backend'; +import { constants } from '@clerk/backend/internal'; import { apiUrlFromPublishableKey } from '@clerk/shared/apiUrlFromPublishableKey'; export const API_VERSION = process.env.CLERK_API_VERSION || 'v1'; diff --git a/packages/fastify/src/getAuth.ts b/packages/fastify/src/getAuth.ts index 7acb04aefc2..1b6c24138ef 100644 --- a/packages/fastify/src/getAuth.ts +++ b/packages/fastify/src/getAuth.ts @@ -1,4 +1,4 @@ -import type { AuthObject } from '@clerk/backend'; +import type { AuthObject } from '@clerk/backend/internal'; import type { FastifyRequest } from 'fastify'; import { pluginRegistrationRequired } from './errors'; diff --git a/packages/fastify/src/withClerkMiddleware.ts b/packages/fastify/src/withClerkMiddleware.ts index c3fe1c1ade5..0a2c94a2ea8 100644 --- a/packages/fastify/src/withClerkMiddleware.ts +++ b/packages/fastify/src/withClerkMiddleware.ts @@ -1,4 +1,4 @@ -import { AuthStatus } from '@clerk/backend'; +import { AuthStatus } from '@clerk/backend/internal'; import type { FastifyRequest } from 'fastify'; import { clerkClient } from './clerkClient'; diff --git a/packages/gatsby-plugin-clerk/src/ssr/types.ts b/packages/gatsby-plugin-clerk/src/ssr/types.ts index aad0a2f6d5b..f823ca971b6 100644 --- a/packages/gatsby-plugin-clerk/src/ssr/types.ts +++ b/packages/gatsby-plugin-clerk/src/ssr/types.ts @@ -1,4 +1,5 @@ -import type { AuthenticateRequestOptions, AuthObject, Organization, Session, User } from '@clerk/backend'; +import type { Organization, Session, User } from '@clerk/backend'; +import type { AuthenticateRequestOptions, AuthObject } from '@clerk/backend/internal'; import type { GetServerDataProps } from 'gatsby'; export type WithServerAuthResult = (props: GetServerDataProps) => Promise>; diff --git a/packages/gatsby-plugin-clerk/src/ssr/utils.ts b/packages/gatsby-plugin-clerk/src/ssr/utils.ts index fed608ad9ff..575cbc82c9a 100644 --- a/packages/gatsby-plugin-clerk/src/ssr/utils.ts +++ b/packages/gatsby-plugin-clerk/src/ssr/utils.ts @@ -1,10 +1,9 @@ -import type { AuthObject } from '@clerk/backend'; -import { prunePrivateMetadata } from '@clerk/backend'; +import type { AuthObject } from '@clerk/backend/internal'; +import { constants, prunePrivateMetadata } from '@clerk/backend/internal'; import cookie from 'cookie'; import type { GetServerDataProps } from 'gatsby'; import { SECRET_KEY } from '../constants'; -import { constants } from './clerkClient'; /** * @internal diff --git a/packages/gatsby-plugin-clerk/src/ssr/withServerAuth.ts b/packages/gatsby-plugin-clerk/src/ssr/withServerAuth.ts index f69e290ddde..eb5e5948ce6 100644 --- a/packages/gatsby-plugin-clerk/src/ssr/withServerAuth.ts +++ b/packages/gatsby-plugin-clerk/src/ssr/withServerAuth.ts @@ -1,4 +1,4 @@ -import { AuthStatus } from '@clerk/backend'; +import { AuthStatus } from '@clerk/backend/internal'; import type { GetServerDataProps, GetServerDataReturn } from 'gatsby'; import { PUBLISHABLE_KEY, SECRET_KEY } from '../constants'; diff --git a/packages/nextjs/src/app-router/server/auth.ts b/packages/nextjs/src/app-router/server/auth.ts index d3f3f21579f..6d0d4df10ac 100644 --- a/packages/nextjs/src/app-router/server/auth.ts +++ b/packages/nextjs/src/app-router/server/auth.ts @@ -1,4 +1,4 @@ -import type { SignedInAuthObject, SignedOutAuthObject } from '@clerk/backend'; +import type { SignedInAuthObject, SignedOutAuthObject } from '@clerk/backend/internal'; import type { CheckAuthorizationParamsWithCustomPermissions, CheckAuthorizationWithCustomPermissions, diff --git a/packages/nextjs/src/server/__tests__/__snapshots__/exports.test.ts.snap b/packages/nextjs/src/server/__tests__/__snapshots__/exports.test.ts.snap index 6ed550f9e77..5ad2c4b6993 100644 --- a/packages/nextjs/src/server/__tests__/__snapshots__/exports.test.ts.snap +++ b/packages/nextjs/src/server/__tests__/__snapshots__/exports.test.ts.snap @@ -3,7 +3,6 @@ exports[`/server public exports should not include a breaking change 1`] = ` [ "AllowlistIdentifier", - "AuthStatus", "Client", "DeletedObject", "Email", @@ -28,27 +27,15 @@ exports[`/server public exports should not include a breaking change 1`] = ` "auth", "authMiddleware", "buildClerkProps", - "buildRequestUrl", "clerkClient", - "constants", - "createAuthenticateRequest", "createClerkClient", - "createIsomorphicRequest", "currentUser", - "debugRequestState", "decodeJwt", - "deserialize", "getAuth", "hasValidSignature", - "makeAuthObjectSerializable", - "prunePrivateMetadata", - "redirect", "redirectToSignIn", "redirectToSignUp", - "sanitizeAuthObject", "signJwt", - "signedInAuthObject", - "signedOutAuthObject", "verifyJwt", "verifyToken", ] diff --git a/packages/nextjs/src/server/authMiddleware.test.ts b/packages/nextjs/src/server/authMiddleware.test.ts index ee6f08de283..1e6559c654a 100644 --- a/packages/nextjs/src/server/authMiddleware.test.ts +++ b/packages/nextjs/src/server/authMiddleware.test.ts @@ -1,6 +1,6 @@ // There is no need to execute the complete authenticateRequest to test authMiddleware // This mock SHOULD exist before the import of authenticateRequest -import { AuthStatus } from '@clerk/backend'; +import { AuthStatus } from '@clerk/backend/internal'; import { expectTypeOf } from 'expect-type'; import { NextURL } from 'next/dist/server/web/next-url'; import type { NextFetchEvent, NextRequest } from 'next/server'; diff --git a/packages/nextjs/src/server/authMiddleware.ts b/packages/nextjs/src/server/authMiddleware.ts index b2a8474a275..c89fcbeebb9 100644 --- a/packages/nextjs/src/server/authMiddleware.ts +++ b/packages/nextjs/src/server/authMiddleware.ts @@ -1,5 +1,5 @@ -import type { AuthenticateRequestOptions, AuthObject } from '@clerk/backend'; -import { AuthStatus, buildRequestUrl, constants } from '@clerk/backend'; +import type { AuthenticateRequestOptions, AuthObject } from '@clerk/backend/internal'; +import { AuthStatus, buildRequestUrl, constants } from '@clerk/backend/internal'; import { DEV_BROWSER_JWT_MARKER, setDevBrowserJWTInURL } from '@clerk/shared/devBrowser'; import { isDevelopmentFromSecretKey } from '@clerk/shared/keys'; import { eventMethodCalled } from '@clerk/shared/telemetry'; diff --git a/packages/nextjs/src/server/getAuth.ts b/packages/nextjs/src/server/getAuth.ts index f6deeeee0d1..63b7d351da2 100644 --- a/packages/nextjs/src/server/getAuth.ts +++ b/packages/nextjs/src/server/getAuth.ts @@ -1,13 +1,14 @@ -import type { Organization, Session, SignedInAuthObject, SignedOutAuthObject, User } from '@clerk/backend'; +import type { Organization, Session, User } from '@clerk/backend'; +import { decodeJwt } from '@clerk/backend'; +import type { SignedInAuthObject, SignedOutAuthObject } from '@clerk/backend/internal'; import { AuthStatus, constants, - decodeJwt, makeAuthObjectSerializable, sanitizeAuthObject, signedInAuthObject, signedOutAuthObject, -} from '@clerk/backend'; +} from '@clerk/backend/internal'; import { withLogger } from '../utils/debugLogger'; import { API_URL, API_VERSION, SECRET_KEY } from './constants'; diff --git a/packages/nextjs/src/server/redirect.ts b/packages/nextjs/src/server/redirect.ts index cc955aa5d37..46d76740d4c 100644 --- a/packages/nextjs/src/server/redirect.ts +++ b/packages/nextjs/src/server/redirect.ts @@ -1,4 +1,4 @@ -import { constants, redirect } from '@clerk/backend'; +import { constants, redirect } from '@clerk/backend/internal'; import { NextResponse } from 'next/server'; import { setHeader } from '../utils'; diff --git a/packages/nextjs/src/server/types.ts b/packages/nextjs/src/server/types.ts index b960002590d..767e0ac73d3 100644 --- a/packages/nextjs/src/server/types.ts +++ b/packages/nextjs/src/server/types.ts @@ -1,4 +1,4 @@ -import type { AuthObject, OptionalVerifyTokenOptions } from '@clerk/backend'; +import type { AuthObject, OptionalVerifyTokenOptions } from '@clerk/backend/internal'; import type { MultiDomainAndOrProxy } from '@clerk/types'; import type { IncomingMessage } from 'http'; import type { NextApiRequest } from 'next'; diff --git a/packages/nextjs/src/server/utils.ts b/packages/nextjs/src/server/utils.ts index 6f244db5963..4cc1e388cc9 100644 --- a/packages/nextjs/src/server/utils.ts +++ b/packages/nextjs/src/server/utils.ts @@ -1,5 +1,5 @@ -import type { AuthenticateRequestOptions, RequestState } from '@clerk/backend'; -import { buildRequestUrl, constants } from '@clerk/backend'; +import type { AuthenticateRequestOptions, RequestState } from '@clerk/backend/internal'; +import { buildRequestUrl, constants } from '@clerk/backend/internal'; import { handleValueOrFn } from '@clerk/shared/handleValueOrFn'; import { isDevelopmentFromSecretKey } from '@clerk/shared/keys'; import { isHttpOrHttps } from '@clerk/shared/proxy'; diff --git a/packages/remix/src/ssr/authenticateRequest.ts b/packages/remix/src/ssr/authenticateRequest.ts index 51d203da2e1..1bf35a96245 100644 --- a/packages/remix/src/ssr/authenticateRequest.ts +++ b/packages/remix/src/ssr/authenticateRequest.ts @@ -1,5 +1,6 @@ -import type { RequestState } from '@clerk/backend'; -import { buildRequestUrl, createClerkClient } from '@clerk/backend'; +import { createClerkClient } from '@clerk/backend'; +import type { RequestState } from '@clerk/backend/internal'; +import { buildRequestUrl } from '@clerk/backend/internal'; import { apiUrlFromPublishableKey } from '@clerk/shared/apiUrlFromPublishableKey'; import { handleValueOrFn } from '@clerk/shared/handleValueOrFn'; import { isDevelopmentFromSecretKey } from '@clerk/shared/keys'; diff --git a/packages/remix/src/ssr/getAuth.ts b/packages/remix/src/ssr/getAuth.ts index 8871a5cb1fd..c13836b8c3c 100644 --- a/packages/remix/src/ssr/getAuth.ts +++ b/packages/remix/src/ssr/getAuth.ts @@ -1,4 +1,4 @@ -import { AuthStatus, sanitizeAuthObject } from '@clerk/backend'; +import { AuthStatus, sanitizeAuthObject } from '@clerk/backend/internal'; import { redirect } from '@remix-run/server-runtime'; import { noLoaderArgsPassedInGetAuth } from '../errors'; diff --git a/packages/remix/src/ssr/rootAuthLoader.ts b/packages/remix/src/ssr/rootAuthLoader.ts index f91094893aa..0e2753a2e02 100644 --- a/packages/remix/src/ssr/rootAuthLoader.ts +++ b/packages/remix/src/ssr/rootAuthLoader.ts @@ -1,4 +1,4 @@ -import { AuthStatus, sanitizeAuthObject } from '@clerk/backend'; +import { AuthStatus, sanitizeAuthObject } from '@clerk/backend/internal'; import type { defer } from '@remix-run/server-runtime'; import { redirect } from '@remix-run/server-runtime'; import { isDeferredData } from '@remix-run/server-runtime/dist/responses'; diff --git a/packages/remix/src/ssr/types.ts b/packages/remix/src/ssr/types.ts index ec2fd551abf..dc7b6c2f0aa 100644 --- a/packages/remix/src/ssr/types.ts +++ b/packages/remix/src/ssr/types.ts @@ -1,4 +1,5 @@ -import type { AuthObject, Organization, Session, User, VerifyTokenOptions } from '@clerk/backend'; +import type { Organization, Session, User, VerifyTokenOptions } from '@clerk/backend'; +import type { AuthObject } from '@clerk/backend/internal'; import type { MultiDomainAndOrProxy } from '@clerk/types'; import type { DataFunctionArgs, LoaderFunction } from '@remix-run/server-runtime'; diff --git a/packages/remix/src/ssr/utils.ts b/packages/remix/src/ssr/utils.ts index dd48b5c3d81..4273b42b808 100644 --- a/packages/remix/src/ssr/utils.ts +++ b/packages/remix/src/ssr/utils.ts @@ -1,5 +1,5 @@ -import type { AuthObject, RequestState } from '@clerk/backend'; -import { constants, debugRequestState } from '@clerk/backend'; +import type { AuthObject, RequestState } from '@clerk/backend/internal'; +import { constants, debugRequestState } from '@clerk/backend/internal'; import { isTruthy } from '@clerk/shared/underscore'; import type { AppLoadContext, defer } from '@remix-run/server-runtime'; import { json } from '@remix-run/server-runtime'; diff --git a/packages/sdk-node/src/__tests__/__snapshots__/exports.test.ts.snap b/packages/sdk-node/src/__tests__/__snapshots__/exports.test.ts.snap index 9128626cdbb..5a123110874 100644 --- a/packages/sdk-node/src/__tests__/__snapshots__/exports.test.ts.snap +++ b/packages/sdk-node/src/__tests__/__snapshots__/exports.test.ts.snap @@ -3,7 +3,6 @@ exports[`module exports should not change unless explicitly set 1`] = ` [ "AllowlistIdentifier", - "AuthStatus", "ClerkExpressRequireAuth", "ClerkExpressWithAuth", "Client", @@ -27,26 +26,14 @@ exports[`module exports should not change unless explicitly set 1`] = ` "Token", "User", "Verification", - "buildRequestUrl", "clerkClient", - "constants", - "createAuthenticateRequest", "createClerkClient", "createClerkExpressRequireAuth", "createClerkExpressWithAuth", - "createIsomorphicRequest", - "debugRequestState", "decodeJwt", - "deserialize", "hasValidSignature", - "makeAuthObjectSerializable", - "prunePrivateMetadata", - "redirect", "requireAuth", - "sanitizeAuthObject", "signJwt", - "signedInAuthObject", - "signedOutAuthObject", "verifyJwt", "verifyToken", "withAuth", diff --git a/packages/sdk-node/src/__tests__/authenticateRequest.test.ts b/packages/sdk-node/src/__tests__/authenticateRequest.test.ts index 44b602357b3..1d1751d43e3 100644 --- a/packages/sdk-node/src/__tests__/authenticateRequest.test.ts +++ b/packages/sdk-node/src/__tests__/authenticateRequest.test.ts @@ -1,4 +1,4 @@ -import { constants } from '@clerk/backend'; +import { constants } from '@clerk/backend/internal'; import { Request } from 'express'; import { authenticateRequest } from '../authenticateRequest'; diff --git a/packages/sdk-node/src/__tests__/middleware.test.ts b/packages/sdk-node/src/__tests__/middleware.test.ts index 417148b7a26..74d7f7ca6ca 100644 --- a/packages/sdk-node/src/__tests__/middleware.test.ts +++ b/packages/sdk-node/src/__tests__/middleware.test.ts @@ -1,4 +1,4 @@ -import type { RequestState } from '@clerk/backend'; +import type { RequestState } from '@clerk/backend/internal'; import type { NextFunction, Request, Response } from 'express'; import { createClerkExpressRequireAuth } from '../clerkExpressRequireAuth'; diff --git a/packages/sdk-node/src/authenticateRequest.ts b/packages/sdk-node/src/authenticateRequest.ts index 17db4085c23..cc04de10970 100644 --- a/packages/sdk-node/src/authenticateRequest.ts +++ b/packages/sdk-node/src/authenticateRequest.ts @@ -1,5 +1,5 @@ -import type { RequestState } from '@clerk/backend'; -import { buildRequestUrl, constants } from '@clerk/backend'; +import type { RequestState } from '@clerk/backend/internal'; +import { buildRequestUrl, constants } from '@clerk/backend/internal'; import { handleValueOrFn } from '@clerk/shared/handleValueOrFn'; import { isDevelopmentFromSecretKey } from '@clerk/shared/keys'; import { isHttpOrHttps, isProxyUrlRelative, isValidProxyUrl } from '@clerk/shared/proxy'; diff --git a/packages/sdk-node/src/clerkExpressRequireAuth.ts b/packages/sdk-node/src/clerkExpressRequireAuth.ts index 47f3eef0810..fa9880ab199 100644 --- a/packages/sdk-node/src/clerkExpressRequireAuth.ts +++ b/packages/sdk-node/src/clerkExpressRequireAuth.ts @@ -1,5 +1,5 @@ import type { createClerkClient } from '@clerk/backend'; -import { AuthStatus } from '@clerk/backend'; +import { AuthStatus } from '@clerk/backend/internal'; import { authenticateRequest, decorateResponseWithObservabilityHeaders } from './authenticateRequest'; import type { ClerkMiddlewareOptions, MiddlewareRequireAuthProp, RequireAuthProp } from './types'; diff --git a/packages/sdk-node/src/clerkExpressWithAuth.ts b/packages/sdk-node/src/clerkExpressWithAuth.ts index aba87f14eba..bf3ce796e99 100644 --- a/packages/sdk-node/src/clerkExpressWithAuth.ts +++ b/packages/sdk-node/src/clerkExpressWithAuth.ts @@ -1,4 +1,4 @@ -import { AuthStatus } from '@clerk/backend'; +import { AuthStatus } from '@clerk/backend/internal'; import { authenticateRequest, decorateResponseWithObservabilityHeaders } from './authenticateRequest'; import type { CreateClerkExpressMiddlewareOptions } from './clerkExpressRequireAuth'; diff --git a/packages/sdk-node/src/types.ts b/packages/sdk-node/src/types.ts index a4e45e80ad7..63bf6b209fa 100644 --- a/packages/sdk-node/src/types.ts +++ b/packages/sdk-node/src/types.ts @@ -1,4 +1,5 @@ -import type { AuthenticateRequestOptions, AuthObject, createClerkClient, SignedInAuthObject } from '@clerk/backend'; +import type { createClerkClient } from '@clerk/backend'; +import type { AuthenticateRequestOptions, AuthObject, SignedInAuthObject } from '@clerk/backend/internal'; import type { MultiDomainAndOrProxy } from '@clerk/types'; import type { NextFunction, Request, Response } from 'express'; import type { IncomingMessage } from 'http'; From ff060fcdf02ed92e4b04ed3a9d977d825531e814 Mon Sep 17 00:00:00 2001 From: Dimitris Klouvas Date: Thu, 14 Dec 2023 20:23:39 +0200 Subject: [PATCH 12/15] chore(backend): Generate declarations using tsc and use generated d.ts for both esm and cjs This change resolves issues with inferred types. Eg: ``` The inferred type of 'auth' cannot be named without a reference to ... ``` --- packages/backend/package.json | 7 ++++--- packages/backend/tsconfig.declarations.json | 12 +++++++++++ packages/backend/tsup.config.ts | 22 ++++++++++++++++----- 3 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 packages/backend/tsconfig.declarations.json diff --git a/packages/backend/package.json b/packages/backend/package.json index 21715b1ff7e..c998d942b5b 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -15,7 +15,7 @@ "exports": { ".": { "import": { - "types": "./dist/index.d.mts", + "types": "./dist/index.d.ts", "default": "./dist/index.mjs" }, "require": { @@ -25,7 +25,7 @@ }, "./errors": { "import": { - "types": "./dist/errors.d.mts", + "types": "./dist/errors.d.ts", "default": "./dist/errors.mjs" }, "require": { @@ -35,7 +35,7 @@ }, "./internal": { "import": { - "types": "./dist/internal.d.mts", + "types": "./dist/internal.d.ts", "default": "./dist/internal.mjs" }, "require": { @@ -53,6 +53,7 @@ "build": "tsup", "dev": "tsup --watch", "dev:publish": "npm run dev -- --env.publish", + "build:declarations": "tsc -p tsconfig.declarations.json", "publish:local": "npx yalc push --replace --sig", "build:lib": "tsup --env.NODE_ENV production", "build:tests": "tsc -p tsconfig.test.json", diff --git a/packages/backend/tsconfig.declarations.json b/packages/backend/tsconfig.declarations.json new file mode 100644 index 00000000000..4a7735336e2 --- /dev/null +++ b/packages/backend/tsconfig.declarations.json @@ -0,0 +1,12 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "skipLibCheck": true, + "noEmit": false, + "declaration": true, + "emitDeclarationOnly": true, + "declarationMap": true, + "sourceMap": false, + "declarationDir": "./dist" + } +} diff --git a/packages/backend/tsup.config.ts b/packages/backend/tsup.config.ts index e0bc62498da..5ed281e635a 100644 --- a/packages/backend/tsup.config.ts +++ b/packages/backend/tsup.config.ts @@ -1,5 +1,7 @@ +import type { Options } from 'tsup'; import { defineConfig } from 'tsup'; +import { runAfterLast } from '../../scripts/utils'; // @ts-ignore import { name, version } from './package.json'; @@ -7,19 +9,29 @@ export default defineConfig(overrideOptions => { const isWatch = !!overrideOptions.watch; const shouldPublish = !!overrideOptions.env?.publish; - return { + const common: Options = { entry: ['src/index.ts', 'src/errors.ts', 'src/internal.ts'], + onSuccess: `cpy 'src/runtime/**/*.{mjs,js,cjs}' dist/runtime`, + sourcemap: true, define: { PACKAGE_NAME: `"${name}"`, PACKAGE_VERSION: `"${version}"`, __DEV__: `${isWatch}`, }, - onSuccess: shouldPublish ? 'npm run publish:local' : undefined, - format: ['cjs', 'esm'], bundle: true, - sourcemap: true, clean: true, minify: false, - dts: true, }; + + const esm: Options = { + ...common, + format: 'esm', + }; + + const cjs: Options = { + ...common, + format: 'cjs', + }; + + return runAfterLast(['npm run build:declarations', shouldPublish && 'npm run publish:local'])(esm, cjs); }); From 58ff6f006e78c56eb0519fc31e0bd04fda2ef1c6 Mon Sep 17 00:00:00 2001 From: Dimitris Klouvas Date: Thu, 14 Dec 2023 21:46:41 +0200 Subject: [PATCH 13/15] chore(backend): Expose /jwt subpath export for jwt related utils --- packages/backend/package.json | 10 ++++++++++ packages/backend/src/__tests__/exports.test.ts | 12 ++++++++---- packages/backend/src/index.ts | 4 ++-- packages/backend/src/jwt.ts | 5 +++++ packages/backend/tsup.config.ts | 2 +- .../__tests__/__snapshots__/exports.test.ts.snap | 4 ---- packages/nextjs/src/server/getAuth.ts | 2 +- .../src/__tests__/__snapshots__/exports.test.ts.snap | 4 ---- 8 files changed, 27 insertions(+), 16 deletions(-) create mode 100644 packages/backend/src/jwt.ts diff --git a/packages/backend/package.json b/packages/backend/package.json index c998d942b5b..e872a584987 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -43,6 +43,16 @@ "default": "./dist/internal.js" } }, + "./jwt": { + "import": { + "types": "./dist/jwt.d.ts", + "default": "./dist/jwt.mjs" + }, + "require": { + "types": "./dist/jwt.d.ts", + "default": "./dist/jwt.js" + } + }, "./package.json": "./package.json" }, "main": "./dist/index.js", diff --git a/packages/backend/src/__tests__/exports.test.ts b/packages/backend/src/__tests__/exports.test.ts index 3b21dc4e0c6..218ca170fe9 100644 --- a/packages/backend/src/__tests__/exports.test.ts +++ b/packages/backend/src/__tests__/exports.test.ts @@ -3,6 +3,7 @@ import type QUnit from 'qunit'; import * as errorExports from '../errors'; import * as publicExports from '../index'; import * as internalExports from '../internal'; +import * as jwtExports from '../jwt'; export default (QUnit: QUnit) => { const { module, test } = QUnit; @@ -33,10 +34,6 @@ export default (QUnit: QUnit) => { 'User', 'Verification', 'createClerkClient', - 'decodeJwt', - 'hasValidSignature', - 'signJwt', - 'verifyJwt', 'verifyToken', ]; assert.deepEqual(Object.keys(publicExports).sort(), exportedApiKeys); @@ -74,4 +71,11 @@ export default (QUnit: QUnit) => { assert.deepEqual(Object.keys(internalExports).sort(), exportedApiKeys); }); }); + + module('subpath /jwt exports', () => { + test('should not include a breaking change', assert => { + const exportedApiKeys = ['decodeJwt', 'hasValidSignature', 'signJwt', 'verifyJwt']; + assert.deepEqual(Object.keys(jwtExports).sort(), exportedApiKeys); + }); + }); }; diff --git a/packages/backend/src/index.ts b/packages/backend/src/index.ts index d9bf992bc8f..6247e2ae826 100644 --- a/packages/backend/src/index.ts +++ b/packages/backend/src/index.ts @@ -8,8 +8,8 @@ import type { CreateAuthenticateRequestOptions } from './tokens/factory'; import { createAuthenticateRequest } from './tokens/factory'; export * from './api/resources'; -export * from './tokens/jwt'; -export * from './tokens/verify'; +export type { VerifyTokenOptions } from './tokens/verify'; +export { verifyToken } from './tokens/verify'; export type ClerkOptions = CreateBackendApiOptions & Partial< diff --git a/packages/backend/src/jwt.ts b/packages/backend/src/jwt.ts new file mode 100644 index 00000000000..7829a53e06a --- /dev/null +++ b/packages/backend/src/jwt.ts @@ -0,0 +1,5 @@ +export { hasValidSignature, decodeJwt, verifyJwt } from './tokens/jwt/verifyJwt'; +export { signJwt } from './tokens/jwt/signJwt'; + +export type { VerifyJwtOptions } from './tokens/jwt/verifyJwt'; +export type { SignJwtOptions } from './tokens/jwt/signJwt'; diff --git a/packages/backend/tsup.config.ts b/packages/backend/tsup.config.ts index 5ed281e635a..01063f59c45 100644 --- a/packages/backend/tsup.config.ts +++ b/packages/backend/tsup.config.ts @@ -10,7 +10,7 @@ export default defineConfig(overrideOptions => { const shouldPublish = !!overrideOptions.env?.publish; const common: Options = { - entry: ['src/index.ts', 'src/errors.ts', 'src/internal.ts'], + entry: ['src/index.ts', 'src/errors.ts', 'src/internal.ts', 'src/jwt.ts'], onSuccess: `cpy 'src/runtime/**/*.{mjs,js,cjs}' dist/runtime`, sourcemap: true, define: { diff --git a/packages/nextjs/src/server/__tests__/__snapshots__/exports.test.ts.snap b/packages/nextjs/src/server/__tests__/__snapshots__/exports.test.ts.snap index 5ad2c4b6993..73edd5bb4c5 100644 --- a/packages/nextjs/src/server/__tests__/__snapshots__/exports.test.ts.snap +++ b/packages/nextjs/src/server/__tests__/__snapshots__/exports.test.ts.snap @@ -30,13 +30,9 @@ exports[`/server public exports should not include a breaking change 1`] = ` "clerkClient", "createClerkClient", "currentUser", - "decodeJwt", "getAuth", - "hasValidSignature", "redirectToSignIn", "redirectToSignUp", - "signJwt", - "verifyJwt", "verifyToken", ] `; diff --git a/packages/nextjs/src/server/getAuth.ts b/packages/nextjs/src/server/getAuth.ts index 63b7d351da2..9f372fde0ce 100644 --- a/packages/nextjs/src/server/getAuth.ts +++ b/packages/nextjs/src/server/getAuth.ts @@ -1,5 +1,4 @@ import type { Organization, Session, User } from '@clerk/backend'; -import { decodeJwt } from '@clerk/backend'; import type { SignedInAuthObject, SignedOutAuthObject } from '@clerk/backend/internal'; import { AuthStatus, @@ -9,6 +8,7 @@ import { signedInAuthObject, signedOutAuthObject, } from '@clerk/backend/internal'; +import { decodeJwt } from '@clerk/backend/jwt'; import { withLogger } from '../utils/debugLogger'; import { API_URL, API_VERSION, SECRET_KEY } from './constants'; diff --git a/packages/sdk-node/src/__tests__/__snapshots__/exports.test.ts.snap b/packages/sdk-node/src/__tests__/__snapshots__/exports.test.ts.snap index 5a123110874..fc8b0ee9659 100644 --- a/packages/sdk-node/src/__tests__/__snapshots__/exports.test.ts.snap +++ b/packages/sdk-node/src/__tests__/__snapshots__/exports.test.ts.snap @@ -30,11 +30,7 @@ exports[`module exports should not change unless explicitly set 1`] = ` "createClerkClient", "createClerkExpressRequireAuth", "createClerkExpressWithAuth", - "decodeJwt", - "hasValidSignature", "requireAuth", - "signJwt", - "verifyJwt", "verifyToken", "withAuth", ] From 972e4e6cb999fc9df52f093d74fd78b317d0c8b0 Mon Sep 17 00:00:00 2001 From: Dimitris Klouvas Date: Thu, 14 Dec 2023 22:03:47 +0200 Subject: [PATCH 14/15] chore(backend): Move /tokens/jwt to top-level and replace jwt.ts --- packages/backend/package.json | 8 ++++---- .../{tokens/fixtures.ts => fixtures/index.ts} | 0 packages/backend/src/jwt.ts | 5 ----- .../jwt/__tests__/assertions.test.ts | 0 .../jwt/__tests__/cryptoKeys.test.ts | 0 .../jwt/__tests__/signJwt.test.ts | 0 .../jwt/__tests__/verifyJwt.test.ts | 0 .../src/{tokens => }/jwt/algorithms.ts | 0 .../src/{tokens => }/jwt/assertions.ts | 2 +- .../src/{tokens => }/jwt/cryptoKeys.ts | 2 +- .../backend/src/{tokens => }/jwt/index.ts | 0 .../backend/src/{tokens => }/jwt/signJwt.ts | 4 ++-- .../backend/src/{tokens => }/jwt/verifyJwt.ts | 6 +++--- .../backend/src/tokens/__tests__/keys.test.ts | 6 +++--- .../src/tokens/__tests__/request.test.ts | 2 +- .../src/tokens/__tests__/verify.test.ts | 2 +- packages/backend/src/tokens/handshake.ts | 5 +++-- packages/backend/src/tokens/request.ts | 2 +- packages/backend/src/tokens/verify.ts | 4 ++-- packages/backend/tests/suites.ts | 20 +++++++++---------- packages/backend/tsup.config.ts | 2 +- 21 files changed, 33 insertions(+), 37 deletions(-) rename packages/backend/src/{tokens/fixtures.ts => fixtures/index.ts} (100%) delete mode 100644 packages/backend/src/jwt.ts rename packages/backend/src/{tokens => }/jwt/__tests__/assertions.test.ts (100%) rename packages/backend/src/{tokens => }/jwt/__tests__/cryptoKeys.test.ts (100%) rename packages/backend/src/{tokens => }/jwt/__tests__/signJwt.test.ts (100%) rename packages/backend/src/{tokens => }/jwt/__tests__/verifyJwt.test.ts (100%) rename packages/backend/src/{tokens => }/jwt/algorithms.ts (100%) rename packages/backend/src/{tokens => }/jwt/assertions.ts (99%) rename packages/backend/src/{tokens => }/jwt/cryptoKeys.ts (96%) rename packages/backend/src/{tokens => }/jwt/index.ts (100%) rename packages/backend/src/{tokens => }/jwt/signJwt.ts (95%) rename packages/backend/src/{tokens => }/jwt/verifyJwt.ts (96%) diff --git a/packages/backend/package.json b/packages/backend/package.json index e872a584987..67229c832ec 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -45,12 +45,12 @@ }, "./jwt": { "import": { - "types": "./dist/jwt.d.ts", - "default": "./dist/jwt.mjs" + "types": "./dist/jwt/index.d.ts", + "default": "./dist/jwt/index.mjs" }, "require": { - "types": "./dist/jwt.d.ts", - "default": "./dist/jwt.js" + "types": "./dist/jwt/index.d.ts", + "default": "./dist/jwt/index.js" } }, "./package.json": "./package.json" diff --git a/packages/backend/src/tokens/fixtures.ts b/packages/backend/src/fixtures/index.ts similarity index 100% rename from packages/backend/src/tokens/fixtures.ts rename to packages/backend/src/fixtures/index.ts diff --git a/packages/backend/src/jwt.ts b/packages/backend/src/jwt.ts deleted file mode 100644 index 7829a53e06a..00000000000 --- a/packages/backend/src/jwt.ts +++ /dev/null @@ -1,5 +0,0 @@ -export { hasValidSignature, decodeJwt, verifyJwt } from './tokens/jwt/verifyJwt'; -export { signJwt } from './tokens/jwt/signJwt'; - -export type { VerifyJwtOptions } from './tokens/jwt/verifyJwt'; -export type { SignJwtOptions } from './tokens/jwt/signJwt'; diff --git a/packages/backend/src/tokens/jwt/__tests__/assertions.test.ts b/packages/backend/src/jwt/__tests__/assertions.test.ts similarity index 100% rename from packages/backend/src/tokens/jwt/__tests__/assertions.test.ts rename to packages/backend/src/jwt/__tests__/assertions.test.ts diff --git a/packages/backend/src/tokens/jwt/__tests__/cryptoKeys.test.ts b/packages/backend/src/jwt/__tests__/cryptoKeys.test.ts similarity index 100% rename from packages/backend/src/tokens/jwt/__tests__/cryptoKeys.test.ts rename to packages/backend/src/jwt/__tests__/cryptoKeys.test.ts diff --git a/packages/backend/src/tokens/jwt/__tests__/signJwt.test.ts b/packages/backend/src/jwt/__tests__/signJwt.test.ts similarity index 100% rename from packages/backend/src/tokens/jwt/__tests__/signJwt.test.ts rename to packages/backend/src/jwt/__tests__/signJwt.test.ts diff --git a/packages/backend/src/tokens/jwt/__tests__/verifyJwt.test.ts b/packages/backend/src/jwt/__tests__/verifyJwt.test.ts similarity index 100% rename from packages/backend/src/tokens/jwt/__tests__/verifyJwt.test.ts rename to packages/backend/src/jwt/__tests__/verifyJwt.test.ts diff --git a/packages/backend/src/tokens/jwt/algorithms.ts b/packages/backend/src/jwt/algorithms.ts similarity index 100% rename from packages/backend/src/tokens/jwt/algorithms.ts rename to packages/backend/src/jwt/algorithms.ts diff --git a/packages/backend/src/tokens/jwt/assertions.ts b/packages/backend/src/jwt/assertions.ts similarity index 99% rename from packages/backend/src/tokens/jwt/assertions.ts rename to packages/backend/src/jwt/assertions.ts index 4417597895c..f91e9b2636f 100644 --- a/packages/backend/src/tokens/jwt/assertions.ts +++ b/packages/backend/src/jwt/assertions.ts @@ -1,4 +1,4 @@ -import { TokenVerificationError, TokenVerificationErrorAction, TokenVerificationErrorReason } from '../../errors'; +import { TokenVerificationError, TokenVerificationErrorAction, TokenVerificationErrorReason } from '../errors'; import { algs } from './algorithms'; export type IssuerResolver = string | ((iss: string) => boolean); diff --git a/packages/backend/src/tokens/jwt/cryptoKeys.ts b/packages/backend/src/jwt/cryptoKeys.ts similarity index 96% rename from packages/backend/src/tokens/jwt/cryptoKeys.ts rename to packages/backend/src/jwt/cryptoKeys.ts index ae4e3caa1be..fa4200f6c44 100644 --- a/packages/backend/src/tokens/jwt/cryptoKeys.ts +++ b/packages/backend/src/jwt/cryptoKeys.ts @@ -1,6 +1,6 @@ import { isomorphicAtob } from '@clerk/shared/isomorphicAtob'; -import runtime from '../../runtime'; +import runtime from '../runtime'; // https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey#pkcs_8_import function pemToBuffer(secret: string): ArrayBuffer { diff --git a/packages/backend/src/tokens/jwt/index.ts b/packages/backend/src/jwt/index.ts similarity index 100% rename from packages/backend/src/tokens/jwt/index.ts rename to packages/backend/src/jwt/index.ts diff --git a/packages/backend/src/tokens/jwt/signJwt.ts b/packages/backend/src/jwt/signJwt.ts similarity index 95% rename from packages/backend/src/tokens/jwt/signJwt.ts rename to packages/backend/src/jwt/signJwt.ts index 25d2ac08cfa..5f3d2f8ef44 100644 --- a/packages/backend/src/tokens/jwt/signJwt.ts +++ b/packages/backend/src/jwt/signJwt.ts @@ -1,5 +1,5 @@ -import runtime from '../../runtime'; -import { base64url } from '../../util/rfc4648'; +import runtime from '../runtime'; +import { base64url } from '../util/rfc4648'; import { getCryptoAlgorithm } from './algorithms'; import { importKey } from './cryptoKeys'; diff --git a/packages/backend/src/tokens/jwt/verifyJwt.ts b/packages/backend/src/jwt/verifyJwt.ts similarity index 96% rename from packages/backend/src/tokens/jwt/verifyJwt.ts rename to packages/backend/src/jwt/verifyJwt.ts index 4e7c89b3fbc..8e0dc3a81bb 100644 --- a/packages/backend/src/tokens/jwt/verifyJwt.ts +++ b/packages/backend/src/jwt/verifyJwt.ts @@ -1,10 +1,10 @@ import type { Jwt, JwtPayload } from '@clerk/types'; -import { TokenVerificationError, TokenVerificationErrorAction, TokenVerificationErrorReason } from '../../errors'; +import { TokenVerificationError, TokenVerificationErrorAction, TokenVerificationErrorReason } from '../errors'; // DO NOT CHANGE: Runtime needs to be imported as a default export so that we can stub its dependencies with Sinon.js // For more information refer to https://sinonjs.org/how-to/stub-dependency/ -import runtime from '../../runtime'; -import { base64url } from '../../util/rfc4648'; +import runtime from '../runtime'; +import { base64url } from '../util/rfc4648'; import { getCryptoAlgorithm } from './algorithms'; import { assertActivationClaim, diff --git a/packages/backend/src/tokens/__tests__/keys.test.ts b/packages/backend/src/tokens/__tests__/keys.test.ts index eb60a7a2d3e..e1e161d5b5e 100644 --- a/packages/backend/src/tokens/__tests__/keys.test.ts +++ b/packages/backend/src/tokens/__tests__/keys.test.ts @@ -2,8 +2,6 @@ import type QUnit from 'qunit'; import sinon from 'sinon'; import { TokenVerificationError, TokenVerificationErrorAction, TokenVerificationErrorReason } from '../../errors'; -import runtime from '../../runtime'; -import { jsonError, jsonOk } from '../../util/testUtils'; import { mockJwks, mockJwtPayload, @@ -12,7 +10,9 @@ import { mockPEMKey, mockRsaJwk, mockRsaJwkKid, -} from '../fixtures'; +} from '../../fixtures'; +import runtime from '../../runtime'; +import { jsonError, jsonOk } from '../../util/testUtils'; import { loadClerkJWKFromLocal, loadClerkJWKFromRemote } from '../keys'; export default (QUnit: QUnit) => { diff --git a/packages/backend/src/tokens/__tests__/request.test.ts b/packages/backend/src/tokens/__tests__/request.test.ts index a2a03e9ce82..4926c43adfd 100644 --- a/packages/backend/src/tokens/__tests__/request.test.ts +++ b/packages/backend/src/tokens/__tests__/request.test.ts @@ -2,10 +2,10 @@ import type QUnit from 'qunit'; import sinon from 'sinon'; import { TokenVerificationErrorReason } from '../../errors'; +import { mockInvalidSignatureJwt, mockJwks, mockJwt, mockJwtPayload, mockMalformedJwt } from '../../fixtures'; import runtime from '../../runtime'; import { jsonOk } from '../../util/testUtils'; import { AuthErrorReason, type AuthReason, AuthStatus, type RequestState } from '../authStatus'; -import { mockInvalidSignatureJwt, mockJwks, mockJwt, mockJwtPayload, mockMalformedJwt } from '../fixtures'; import type { AuthenticateRequestOptions } from '../request'; import { authenticateRequest, loadOptionsFromHeaders } from '../request'; diff --git a/packages/backend/src/tokens/__tests__/verify.test.ts b/packages/backend/src/tokens/__tests__/verify.test.ts index 97f0d10936c..bd32177b7c9 100644 --- a/packages/backend/src/tokens/__tests__/verify.test.ts +++ b/packages/backend/src/tokens/__tests__/verify.test.ts @@ -1,9 +1,9 @@ import type QUnit from 'qunit'; import sinon from 'sinon'; +import { mockJwks, mockJwt, mockJwtPayload } from '../../fixtures'; import runtime from '../../runtime'; import { jsonOk } from '../../util/testUtils'; -import { mockJwks, mockJwt, mockJwtPayload } from '../fixtures'; import { verifyToken } from '../verify'; export default (QUnit: QUnit) => { diff --git a/packages/backend/src/tokens/handshake.ts b/packages/backend/src/tokens/handshake.ts index fb993ce6ffd..1dfd8b41bb9 100644 --- a/packages/backend/src/tokens/handshake.ts +++ b/packages/backend/src/tokens/handshake.ts @@ -1,6 +1,7 @@ import { TokenVerificationError, TokenVerificationErrorAction, TokenVerificationErrorReason } from '../errors'; -import { decodeJwt, hasValidSignature, type VerifyJwtOptions } from './jwt'; -import { assertHeaderAlgorithm, assertHeaderType } from './jwt/assertions'; +import type { VerifyJwtOptions } from '../jwt'; +import { decodeJwt, hasValidSignature } from '../jwt'; +import { assertHeaderAlgorithm, assertHeaderType } from '../jwt/assertions'; import { loadClerkJWKFromLocal, loadClerkJWKFromRemote } from './keys'; import type { VerifyTokenOptions } from './verify'; diff --git a/packages/backend/src/tokens/request.ts b/packages/backend/src/tokens/request.ts index aa5fc506b28..5b7ba05f322 100644 --- a/packages/backend/src/tokens/request.ts +++ b/packages/backend/src/tokens/request.ts @@ -4,13 +4,13 @@ import type { JwtPayload } from '@clerk/types'; import { constants } from '../constants'; import type { TokenCarrier } from '../errors'; import { TokenVerificationError, TokenVerificationErrorReason } from '../errors'; +import { decodeJwt } from '../jwt'; import { assertValidSecretKey } from '../util/assertValidSecretKey'; import { buildRequest, stripAuthorizationHeader } from '../util/IsomorphicRequest'; import { isDevelopmentFromSecretKey } from '../util/shared'; import type { AuthStatusOptionsType, RequestState } from './authStatus'; import { AuthErrorReason, handshake, signedIn, signedOut } from './authStatus'; import { verifyHandshakeToken } from './handshake'; -import { decodeJwt } from './jwt'; import { verifyToken, type VerifyTokenOptions } from './verify'; export type OptionalVerifyTokenOptions = Partial< diff --git a/packages/backend/src/tokens/verify.ts b/packages/backend/src/tokens/verify.ts index ae31cce8abb..90d1d716cc0 100644 --- a/packages/backend/src/tokens/verify.ts +++ b/packages/backend/src/tokens/verify.ts @@ -1,8 +1,8 @@ import type { JwtPayload } from '@clerk/types'; import { TokenVerificationError, TokenVerificationErrorAction, TokenVerificationErrorReason } from '../errors'; -import type { VerifyJwtOptions } from './jwt'; -import { decodeJwt, verifyJwt } from './jwt'; +import type { VerifyJwtOptions } from '../jwt'; +import { decodeJwt, verifyJwt } from '../jwt'; import type { LoadClerkJWKFromRemoteOptions } from './keys'; import { loadClerkJWKFromLocal, loadClerkJWKFromRemote } from './keys'; diff --git a/packages/backend/tests/suites.ts b/packages/backend/tests/suites.ts index 40a7dfd84b8..cbb96ac9db2 100644 --- a/packages/backend/tests/suites.ts +++ b/packages/backend/tests/suites.ts @@ -1,21 +1,21 @@ // Import all suites // TODO: Automate this step using dynamic imports -import authObjectsTest from './dist/tokens/__tests__/authObjects.test.js'; -import cryptoKeysTest from './dist/tokens/jwt/__tests__/cryptoKeys.test.js'; import exportsTest from './dist/__tests__/exports.test.js'; +import redirectTest from './dist/__tests__/redirections.test.js'; +import utilsTest from './dist/__tests__/utils.test.js'; import factoryTest from './dist/api/__tests__/factory.test.js'; -import jwtAssertionsTest from './dist/tokens/jwt/__tests__/assertions.test.js'; +import jwtAssertionsTest from './dist/jwt/__tests__/assertions.test.js'; +import cryptoKeysTest from './dist/jwt/__tests__/cryptoKeys.test.js'; +import signJwtTest from './dist/jwt/__tests__/signJwt.test.js'; +import verifyJwtTest from './dist/jwt/__tests__/verifyJwt.test.js'; +import authObjectsTest from './dist/tokens/__tests__/authObjects.test.js'; +import tokenFactoryTest from './dist/tokens/__tests__/factory.test.js'; import keysTest from './dist/tokens/__tests__/keys.test.js'; -import pathTest from './dist/util/__tests__/path.test.js'; -import redirectTest from './dist/__tests__/redirections.test.js'; import requestTest from './dist/tokens/__tests__/request.test.js'; -import signJwtTest from './dist/tokens/jwt/__tests__/signJwt.test.js'; -import tokenFactoryTest from './dist/tokens/__tests__/factory.test.js'; -import utilRequestTest from './dist/util/__tests__/request.test.js'; -import utilsTest from './dist/__tests__/utils.test.js'; -import verifyJwtTest from './dist/tokens/jwt/__tests__/verifyJwt.test.js'; import verifyTest from './dist/tokens/__tests__/verify.test.js'; +import pathTest from './dist/util/__tests__/path.test.js'; +import utilRequestTest from './dist/util/__tests__/request.test.js'; // Add them to the suite array const suites = [ diff --git a/packages/backend/tsup.config.ts b/packages/backend/tsup.config.ts index 01063f59c45..4e21d2e0d22 100644 --- a/packages/backend/tsup.config.ts +++ b/packages/backend/tsup.config.ts @@ -10,7 +10,7 @@ export default defineConfig(overrideOptions => { const shouldPublish = !!overrideOptions.env?.publish; const common: Options = { - entry: ['src/index.ts', 'src/errors.ts', 'src/internal.ts', 'src/jwt.ts'], + entry: ['src/index.ts', 'src/errors.ts', 'src/internal.ts', 'src/jwt/index.ts'], onSuccess: `cpy 'src/runtime/**/*.{mjs,js,cjs}' dist/runtime`, sourcemap: true, define: { From 07b154dedb49a902957a6c08bb1108fdcc5a3238 Mon Sep 17 00:00:00 2001 From: Dimitris Klouvas Date: Thu, 14 Dec 2023 22:38:38 +0200 Subject: [PATCH 15/15] chore(backend): Drop api/resources from exports --- .../backend/src/__tests__/exports.test.ts | 27 +------------------ packages/backend/src/index.ts | 2 +- .../__snapshots__/exports.test.ts.snap | 22 --------------- .../__snapshots__/exports.test.ts.snap | 22 --------------- 4 files changed, 2 insertions(+), 71 deletions(-) diff --git a/packages/backend/src/__tests__/exports.test.ts b/packages/backend/src/__tests__/exports.test.ts index 218ca170fe9..44aaa2935d7 100644 --- a/packages/backend/src/__tests__/exports.test.ts +++ b/packages/backend/src/__tests__/exports.test.ts @@ -10,32 +10,7 @@ export default (QUnit: QUnit) => { module('public exports', () => { test('should not include a breaking change', assert => { - const exportedApiKeys = [ - 'AllowlistIdentifier', - 'Client', - 'DeletedObject', - 'Email', - 'EmailAddress', - 'ExternalAccount', - 'IdentificationLink', - 'Invitation', - 'OauthAccessToken', - 'ObjectType', - 'Organization', - 'OrganizationInvitation', - 'OrganizationMembership', - 'OrganizationMembershipPublicUserData', - 'PhoneNumber', - 'RedirectUrl', - 'SMSMessage', - 'Session', - 'SignInToken', - 'Token', - 'User', - 'Verification', - 'createClerkClient', - 'verifyToken', - ]; + const exportedApiKeys = ['createClerkClient', 'verifyToken']; assert.deepEqual(Object.keys(publicExports).sort(), exportedApiKeys); }); }); diff --git a/packages/backend/src/index.ts b/packages/backend/src/index.ts index 6247e2ae826..2feccea049a 100644 --- a/packages/backend/src/index.ts +++ b/packages/backend/src/index.ts @@ -7,7 +7,7 @@ import { createBackendApiClient } from './api'; import type { CreateAuthenticateRequestOptions } from './tokens/factory'; import { createAuthenticateRequest } from './tokens/factory'; -export * from './api/resources'; +export type { Organization, Session, User } from './api/resources'; export type { VerifyTokenOptions } from './tokens/verify'; export { verifyToken } from './tokens/verify'; diff --git a/packages/nextjs/src/server/__tests__/__snapshots__/exports.test.ts.snap b/packages/nextjs/src/server/__tests__/__snapshots__/exports.test.ts.snap index 73edd5bb4c5..17c471ccec7 100644 --- a/packages/nextjs/src/server/__tests__/__snapshots__/exports.test.ts.snap +++ b/packages/nextjs/src/server/__tests__/__snapshots__/exports.test.ts.snap @@ -2,28 +2,6 @@ exports[`/server public exports should not include a breaking change 1`] = ` [ - "AllowlistIdentifier", - "Client", - "DeletedObject", - "Email", - "EmailAddress", - "ExternalAccount", - "IdentificationLink", - "Invitation", - "OauthAccessToken", - "ObjectType", - "Organization", - "OrganizationInvitation", - "OrganizationMembership", - "OrganizationMembershipPublicUserData", - "PhoneNumber", - "RedirectUrl", - "SMSMessage", - "Session", - "SignInToken", - "Token", - "User", - "Verification", "auth", "authMiddleware", "buildClerkProps", diff --git a/packages/sdk-node/src/__tests__/__snapshots__/exports.test.ts.snap b/packages/sdk-node/src/__tests__/__snapshots__/exports.test.ts.snap index fc8b0ee9659..aa792c20c1f 100644 --- a/packages/sdk-node/src/__tests__/__snapshots__/exports.test.ts.snap +++ b/packages/sdk-node/src/__tests__/__snapshots__/exports.test.ts.snap @@ -2,30 +2,8 @@ exports[`module exports should not change unless explicitly set 1`] = ` [ - "AllowlistIdentifier", "ClerkExpressRequireAuth", "ClerkExpressWithAuth", - "Client", - "DeletedObject", - "Email", - "EmailAddress", - "ExternalAccount", - "IdentificationLink", - "Invitation", - "OauthAccessToken", - "ObjectType", - "Organization", - "OrganizationInvitation", - "OrganizationMembership", - "OrganizationMembershipPublicUserData", - "PhoneNumber", - "RedirectUrl", - "SMSMessage", - "Session", - "SignInToken", - "Token", - "User", - "Verification", "clerkClient", "createClerkClient", "createClerkExpressRequireAuth",