Skip to content

Commit

Permalink
feat: support esm, commonjs and browser build targets
Browse files Browse the repository at this point in the history
  • Loading branch information
Amadeusz Winogrodzki authored and Amadeusz Winogrodzki committed Sep 22, 2024
1 parent 8d9bdb2 commit 93a17bd
Show file tree
Hide file tree
Showing 47 changed files with 745 additions and 258 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ yarn-error.log
.yarn
.next
.env
lib

cjs
tmp
package-lock.json
Expand All @@ -16,3 +16,8 @@ webpack-stats.json
*.log
dist
.fleet

#dist
lib
browser
esm
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
examples
src
!lib
!browser
!esm
12 changes: 12 additions & 0 deletions .swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"legacyDecorator": true,
"decoratorMetadata": true
}
}
}
14 changes: 7 additions & 7 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ module.exports = {
setupFiles: ['dotenv/config'],
testEnvironment: 'node',
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
extensionsToTreatAsEsm: ['.ts', '.tsx'],
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
transform: {
'^.+\\.tsx?$': [
'ts-jest',
{
tsconfig: 'tsconfig.json',
},
],
'^.+\\.jsx?$': 'babel-jest',
'^.+\\.(t|j)sx?$': '@swc/jest',
},
};
111 changes: 106 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,109 @@
"name": "next-firebase-auth-edge",
"version": "1.8.0-canary.1",
"description": "Next.js Firebase Authentication for Edge and server runtimes. Compatible with latest Next.js features.",
"main": "lib/index.js",
"files": [
"/lib"
"lib/**/*.js",
"lib/**/*.d.ts",
"browser/**/*.js",
"browser/**/*.d.ts",
"esm/**/*.js",
"esm/**/*.d.ts"
],
"sideEffects": false,
"main": "./lib/index.js",
"browser": "./browser/index.js",
"types": "./lib/index.d.ts",
"exports": {
".": {
"types": "./lib/index.d.ts",
"bun": "./browser/index.js",
"deno": "./browser/index.js",
"browser": "./browser/index.js",
"worker": "./browser/index.js",
"workerd": "./browser/index.js",
"import": "./esm/index.js",
"require": "./lib/index.js"
},
"./app-check": {
"types": "./lib/app-check/index.d.ts",
"bun": "./browser/app-check/index.js",
"deno": "./browser/app-check/index.js",
"browser": "./browser/app-check/index.js",
"worker": "./browser/app-check/index.js",
"workerd": "./browser/app-check/index.js",
"import": "./esm/app-check/index.js",
"require": "./lib/app-check/index.js"
},
"./auth": {
"types": "./lib/auth/index.d.ts",
"bun": "./browser/auth/index.js",
"deno": "./browser/auth/index.js",
"browser": "./browser/auth/index.js",
"worker": "./browser/auth/index.js",
"workerd": "./browser/auth/index.js",
"import": "./esm/auth/index.js",
"require": "./lib/auth/index.js"
},
"./auth/claims": {
"types": "./lib/auth/claims.d.ts",
"bun": "./browser/auth/claims.js",
"deno": "./browser/auth/claims.js",
"browser": "./browser/auth/claimsjs",
"worker": "./browser/auth/claims.js",
"workerd": "./browser/auth/claims.js",
"import": "./esm/auth/claims.js",
"require": "./lib/auth/claims.js"
},
"./next/utils": {
"types": "./lib/next/utils.d.ts",
"bun": "./browser/next/utils.js",
"deno": "./browser/next/utils.js",
"browser": "./browser/next/utils.js",
"worker": "./browser/next/utils.js",
"workerd": "./browser/next/utils.js",
"import": "./esm/next/utils.js",
"require": "./lib/next/utils.js"
},
"./next/cookies": {
"types": "./lib/next/cookies.d.ts",
"bun": "./browser/next/cookies.js",
"deno": "./browser/next/cookies.js",
"browser": "./browser/next/cookies.js",
"worker": "./browser/next/cookies.js",
"workerd": "./browser/next/cookies.js",
"import": "./esm/next/cookies.js",
"require": "./lib/next/cookies.js"
},
"./next/tokens": {
"types": "./lib/next/tokens.d.ts",
"bun": "./browser/next/tokens.js",
"deno": "./browser/next/tokens.js",
"browser": "./browser/next/tokens.js",
"worker": "./browser/next/tokens.js",
"workerd": "./browser/next/tokens.js",
"import": "./esm/next/tokens.js",
"require": "./lib/next/tokens.js"
},
"./next/client": {
"types": "./lib/next/client.d.ts",
"bun": "./browser/next/client.js",
"deno": "./browser/next/client.js",
"browser": "./browser/next/client.js",
"worker": "./browser/next/client.js",
"workerd": "./browser/next/client.js",
"import": "./esm/next/client.js",
"require": "./lib/next/client.js"
}
},
"scripts": {
"build": "rm -rf ./lib && tsc",
"build": "run-s clear build:*",
"build:cjs": "tsc",
"build:esm": "tsc -p tsconfig.esm.json",
"build:browser": "tsc -p tsconfig.browser.json",
"build:browser-bundle": "esbuild --bundle browser/index.js --format=esm --target=es2020 --outfile=browser/index.bundle.js",
"build:browser-bundle-min": "esbuild --minify --bundle browser/index.js --format=esm --target=es2020 --outfile=browser/index.bundle.min.js",
"build:browser-umd": "rollup browser/index.bundle.js --format umd --name next-firebase-auth-edge -o browser/index.umd.js && rollup browser/index.bundle.min.js --compact --format umd --name next-firebase-auth-edge -o browser/index.umd.min.js",
"clear": "rm -Rf lib esm browser",
"test": "jest src --coverage",
"lint": "eslint src/"
},
Expand All @@ -20,15 +117,18 @@
"jose": "^5.6.3"
},
"devDependencies": {
"@swc/core": "^1.7.26",
"@swc/jest": "^0.2.36",
"@commitlint/cli": "19.4.0",
"@commitlint/config-conventional": "19.2.2",
"@eslint/js": "^9.9.0",
"@types/cookie": "^0.6.0",
"@types/eslint__js": "^8.42.3",
"@types/jest": "^29.5.12",
"@types/jest": "^29.5.13",
"@types/node": "^22.2.0",
"@types/uuid": "^10.0.0",
"dotenv": "^16.4.5",
"esbuild": "^0.24.0",
"eslint": "^9.9.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.2.1",
Expand All @@ -37,10 +137,11 @@
"jest": "^29.2.0",
"jest-fetch-mock": "^3.0.3",
"next": "15.0.0-rc.0",
"npm-run-all2": "^6.2.3",
"prettier": "^3.3.3",
"react": "19.0.0-rc-f994737d14-20240522",
"react-dom": "19.0.0-rc-f994737d14-20240522",
"ts-jest": "^29.2.4",
"rollup": "^4.22.4",
"typescript": "^5.5.4",
"typescript-eslint": "^8.0.1",
"uuid": "^10.0.0"
Expand Down
8 changes: 4 additions & 4 deletions src/app-check/api-client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {getSdkVersion} from '../auth/auth-request-handler';
import {Credential} from '../auth/credential';
import {formatString} from '../auth/utils';
import {AppCheckToken} from './types';
import {getSdkVersion} from '../auth/auth-request-handler.js';
import {Credential} from '../auth/credential.js';
import {formatString} from '../auth/utils.js';
import {AppCheckToken} from './types.js';

const FIREBASE_APP_CHECK_V1_API_URL_FORMAT =
'https://firebaseappcheck.googleapis.com/v1/projects/{projectId}/apps/{appId}:exchangeCustomToken';
Expand Down
12 changes: 6 additions & 6 deletions src/app-check/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import {
ServiceAccount,
ServiceAccountCredential
} from '../auth/credential';
import {getApplicationDefault} from '../auth/default-credential';
import {VerifyOptions} from '../auth/jwt/verify';
import {cryptoSignerFromCredential} from '../auth/token-generator';
import {AppCheckApiClient} from './api-client';
import {AppCheckTokenGenerator} from './token-generator';
import {AppCheckTokenVerifier} from './token-verifier';
import {getApplicationDefault} from '../auth/default-credential.js';
import {VerifyOptions} from '../auth/jwt/verify.js';
import {cryptoSignerFromCredential} from '../auth/token-generator.js';
import {AppCheckApiClient} from './api-client.js';
import {AppCheckTokenGenerator} from './token-generator.js';
import {AppCheckTokenVerifier} from './token-verifier.js';
import {
AppCheckToken,
AppCheckTokenOptions,
Expand Down
4 changes: 2 additions & 2 deletions src/app-check/test/app-check.integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {getAppCheck} from '../index';
import {FirebaseAppCheckError} from '../api-client';
import {getAppCheck} from '../index.js';
import {FirebaseAppCheckError} from '../api-client.js';

const {
FIREBASE_PROJECT_ID,
Expand Down
6 changes: 3 additions & 3 deletions src/app-check/token-generator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {CryptoSigner} from '../auth/jwt/crypto-signer';
import {AppCheckTokenOptions} from './types';
import {FirebaseAppCheckError} from './api-client';
import {CryptoSigner} from '../auth/jwt/crypto-signer.js';
import {AppCheckTokenOptions} from './types.js';
import {FirebaseAppCheckError} from './api-client.js';

const ONE_MINUTE_IN_SECONDS = 60;
const ONE_MINUTE_IN_MILLIS = ONE_MINUTE_IN_SECONDS * 1000;
Expand Down
8 changes: 4 additions & 4 deletions src/app-check/token-verifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import {
JWKSSignatureVerifier,
SignatureVerifier
} from '../auth/signature-verifier';
import {DecodedAppCheckToken} from './types';
import {DecodedAppCheckToken} from './types.js';
import {decodeJwt, decodeProtectedHeader, errors} from 'jose';
import {VerifyOptions} from '../auth/jwt/verify';
import {VerifyOptions} from '../auth/jwt/verify.js';
import {FirebaseAppCheckError} from './api-client.js';
import {Credential} from '../auth/credential.js';
import {JOSEError} from 'jose/dist/types/util/errors';
import {FirebaseAppCheckError} from './api-client';
import {Credential} from '../auth/credential';

const APP_CHECK_ISSUER = 'https://firebaseappcheck.googleapis.com/';
const JWKS_URL = 'https://firebaseappcheck.googleapis.com/v1/jwks';
Expand Down
10 changes: 5 additions & 5 deletions src/auth/auth-request-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import {
FirebaseAccessToken,
getFirebaseAdminTokenProvider
} from './credential';
import {AuthError, AuthErrorCode} from './error';
import {emulatorHost, useEmulator} from './firebase';
import {GetAccountInfoUserResponse} from './user-record';
import {formatString} from './utils';
import {isEmail, isNonNullObject} from './validator';
import {AuthError, AuthErrorCode} from './error.js';
import {emulatorHost, useEmulator} from './firebase.js';
import {GetAccountInfoUserResponse} from './user-record.js';
import {formatString} from './utils.js';
import {isEmail, isNonNullObject} from './validator.js';

export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD';

Expand Down
6 changes: 3 additions & 3 deletions src/auth/cookies/sign.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {CustomTokens} from '../custom-token';
import {InvalidTokenError, InvalidTokenReason} from '../error';
import {parseCookies, parseTokens, signCookies, signTokens} from './sign';
import {CustomTokens} from '../custom-token.js';
import {InvalidTokenError, InvalidTokenReason} from '../error.js';
import {parseCookies, parseTokens, signCookies, signTokens} from './sign.js';

const secret = 'some-secret';
const jwt =
Expand Down
6 changes: 3 additions & 3 deletions src/auth/cookies/sign.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {errors} from 'jose';
import {CustomTokens} from '../custom-token';
import {InvalidTokenError, InvalidTokenReason} from '../error';
import {RotatingCredential} from '../rotating-credential';
import {CustomTokens} from '../custom-token/index.js';
import {InvalidTokenError, InvalidTokenReason} from '../error.js';
import {RotatingCredential} from '../rotating-credential.js';

export async function signTokens(
tokens: CustomTokens,
Expand Down
4 changes: 2 additions & 2 deletions src/auth/credential.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {JWTPayload} from 'jose';
import {sign} from './jwt/sign';
import {fetchJson, fetchText} from './utils';
import {sign} from './jwt/sign.js';
import {fetchJson, fetchText} from './utils.js';

export interface GoogleOAuthAccessToken {
access_token: string;
Expand Down
4 changes: 2 additions & 2 deletions src/auth/custom-token/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
errors,
jwtVerify
} from 'jose';
import {toUint8Array} from '../utils';
import {DecodedIdToken} from '../token-verifier';
import {toUint8Array} from '../utils.js';
import {DecodedIdToken} from '../token-verifier.js';

export interface CustomTokens {
idToken: string;
Expand Down
2 changes: 1 addition & 1 deletion src/auth/default-credential.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ComputeEngineCredential, Credential} from './credential';
import {ComputeEngineCredential, Credential} from './credential.js';

export const getApplicationDefault = (): Credential => {
return new ComputeEngineCredential();
Expand Down
16 changes: 8 additions & 8 deletions src/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {debug} from '../debug';
import {debug} from '../debug/index.js';
import {
AuthRequestHandler,
CreateRequest,
Expand All @@ -9,19 +9,19 @@ import {
ServiceAccount,
ServiceAccountCredential
} from './credential';
import {CustomTokens, VerifiedTokens} from './custom-token';
import {getApplicationDefault} from './default-credential';
import {CustomTokens, VerifiedTokens} from './custom-token/index.js';
import {getApplicationDefault} from './default-credential.js';
import {
AuthError,
AuthErrorCode,
InvalidTokenError,
InvalidTokenReason
} from './error';
import {useEmulator} from './firebase';
import {VerifyOptions} from './jwt/verify';
import {createFirebaseTokenGenerator} from './token-generator';
import {createIdTokenVerifier, DecodedIdToken} from './token-verifier';
import {UserRecord} from './user-record';
import {useEmulator} from './firebase.js';
import {VerifyOptions} from './jwt/verify.js';
import {createFirebaseTokenGenerator} from './token-generator.js';
import {createIdTokenVerifier, DecodedIdToken} from './token-verifier.js';
import {UserRecord} from './user-record.js';

const getCustomTokenEndpoint = (apiKey: string) => {
if (useEmulator() && process.env.FIREBASE_AUTH_EMULATOR_HOST) {
Expand Down
8 changes: 4 additions & 4 deletions src/auth/jwt/crypto-signer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {JWTPayload} from 'jose';
import {Credential, ServiceAccountCredential} from '../credential';
import {ALGORITHM_RS256} from '../signature-verifier';
import {fetchText} from '../utils';
import {sign, signBlob} from './sign';
import {Credential, ServiceAccountCredential} from '../credential.js';
import {ALGORITHM_RS256} from '../signature-verifier.js';
import {fetchText} from '../utils.js';
import {sign, signBlob} from './sign.js';

export interface CryptoSigner {
sign(payload: JWTPayload): Promise<string>;
Expand Down
2 changes: 1 addition & 1 deletion src/auth/jwt/sign.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {sign} from './sign';
import {sign} from './sign.js';

type GlobalAny = {
[key: string]: unknown;
Expand Down
6 changes: 3 additions & 3 deletions src/auth/jwt/sign.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {JWTPayload, KeyLike, SignJWT, base64url, importPKCS8} from 'jose';
import {ALGORITHM_RS256} from '../signature-verifier';
import {fetchAny} from '../utils';
import {AuthError, AuthErrorCode} from '../error';
import {ALGORITHM_RS256} from '../signature-verifier.js';
import {fetchAny} from '../utils.js';
import {AuthError, AuthErrorCode} from '../error.js';

export type SignOptions = {
readonly payload: JWTPayload;
Expand Down
Loading

0 comments on commit 93a17bd

Please sign in to comment.