Compare commits
8 Commits
7c82dff259
...
v2.4.7
| Author | SHA1 | Date | |
|---|---|---|---|
|
78554d9332
|
|||
|
dff880c02d
|
|||
|
cf24b34f63
|
|||
|
91f30929da
|
|||
|
93082884fa
|
|||
|
a9e83968b9
|
|||
|
4c480d5ac7
|
|||
|
adf522c3ab
|
4
.gitignore
vendored
4
.gitignore
vendored
@@ -145,4 +145,6 @@ dist
|
|||||||
|
|
||||||
# Custom
|
# Custom
|
||||||
/index.js
|
/index.js
|
||||||
/index.d.ts
|
/index.d.ts
|
||||||
|
/utils.js
|
||||||
|
/utils.d.ts
|
||||||
@@ -3,7 +3,9 @@
|
|||||||
.gitignore
|
.gitignore
|
||||||
.nvmrc
|
.nvmrc
|
||||||
coverage/
|
coverage/
|
||||||
jest.config.ts
|
vite.config.ts
|
||||||
src/
|
src/
|
||||||
tests/
|
tests/
|
||||||
tsconfig.json
|
tsconfig.json
|
||||||
|
utils.*
|
||||||
|
*.tgz
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
import type { Config } from 'jest'
|
|
||||||
|
|
||||||
const config: Config = {
|
|
||||||
preset: 'ts-jest',
|
|
||||||
testEnvironment: 'node',
|
|
||||||
verbose: true
|
|
||||||
}
|
|
||||||
|
|
||||||
export default config
|
|
||||||
4207
package-lock.json
generated
4207
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
19
package.json
19
package.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@tsndr/cloudflare-worker-jwt",
|
"name": "@tsndr/cloudflare-worker-jwt",
|
||||||
"version": "2.4.4",
|
"version": "2.4.7",
|
||||||
"description": "A lightweight JWT implementation with ZERO dependencies for Cloudflare Worker",
|
"description": "A lightweight JWT implementation with ZERO dependencies for Cloudflare Worker",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"exports": "./index.js",
|
"exports": "./index.js",
|
||||||
@@ -9,8 +9,8 @@
|
|||||||
"node": ">=18"
|
"node": ">=18"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "esbuild src/index.ts --bundle --outfile=index.js && tsc --emitDeclarationOnly",
|
"build": "tsc & esbuild --bundle --target=esnext --platform=neutral --outfile=index.js src/index.ts & wait",
|
||||||
"test": "jest"
|
"test": "vitest"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -30,14 +30,11 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/tsndr/cloudflare-worker-jwt#readme",
|
"homepage": "https://github.com/tsndr/cloudflare-worker-jwt#readme",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@cloudflare/workers-types": "^4.20240129.0",
|
"@cloudflare/workers-types": "^4.20240208.0",
|
||||||
"@jest/globals": "^29.7.0",
|
"@edge-runtime/vm": "^3.2.0",
|
||||||
"@types/jest": "^29.5.11",
|
"@types/node": "^20.11.19",
|
||||||
"@types/node": "^20.11.14",
|
|
||||||
"esbuild": "^0.20.0",
|
|
||||||
"jest": "^29.7.0",
|
|
||||||
"ts-jest": "^29.1.2",
|
|
||||||
"ts-node": "^10.9.2",
|
"ts-node": "^10.9.2",
|
||||||
"typescript": "^5.3.3"
|
"typescript": "^5.3.3",
|
||||||
|
"vitest": "^1.3.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
28
src/index.ts
28
src/index.ts
@@ -1,10 +1,10 @@
|
|||||||
import {
|
import {
|
||||||
textToArrayBuffer,
|
textToArrayBuffer,
|
||||||
arrayBufferToBase64Url,
|
arrayBufferToBase64Url,
|
||||||
base64UrlToArrayBuffer,
|
base64UrlToArrayBuffer,
|
||||||
textToBase64Url,
|
textToBase64Url,
|
||||||
importKey,
|
importKey,
|
||||||
decodePayload
|
decodePayload
|
||||||
} from "./utils"
|
} from "./utils"
|
||||||
|
|
||||||
if (typeof crypto === 'undefined' || !crypto.subtle)
|
if (typeof crypto === 'undefined' || !crypto.subtle)
|
||||||
@@ -34,6 +34,13 @@ export type JwtHeader<T = {}> = {
|
|||||||
* @default "JWT"
|
* @default "JWT"
|
||||||
*/
|
*/
|
||||||
typ?: string
|
typ?: string
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Algorithm (default: `"HS256"`)
|
||||||
|
*
|
||||||
|
* @default "HS256"
|
||||||
|
*/
|
||||||
|
alg?: JwtAlgorithm
|
||||||
} & T
|
} & T
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -196,7 +203,13 @@ export async function verify(token: string, secret: string | JsonWebKey | Crypto
|
|||||||
if (!algorithm)
|
if (!algorithm)
|
||||||
throw new Error('algorithm not found')
|
throw new Error('algorithm not found')
|
||||||
|
|
||||||
const { payload } = decode(token)
|
const { header, payload } = decode(token)
|
||||||
|
|
||||||
|
if (header?.alg !== options.algorithm) {
|
||||||
|
if (options.throwError)
|
||||||
|
throw new Error('ALG_MISMATCH')
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!payload)
|
if (!payload)
|
||||||
@@ -214,7 +227,6 @@ export async function verify(token: string, secret: string | JsonWebKey | Crypto
|
|||||||
} catch(err) {
|
} catch(err) {
|
||||||
if (options.throwError)
|
if (options.throwError)
|
||||||
throw err
|
throw err
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
3
src/test.ts
Normal file
3
src/test.ts
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
import { sign } from './index'
|
||||||
|
|
||||||
|
console.log(await sign())
|
||||||
@@ -1,7 +1,4 @@
|
|||||||
import crypto from 'node:crypto'
|
import { describe, expect, test } from 'vitest'
|
||||||
Object.defineProperty(global, 'crypto', { value: { subtle: crypto.webcrypto.subtle }})
|
|
||||||
|
|
||||||
import { describe, expect, test } from '@jest/globals'
|
|
||||||
import jwt, { JwtAlgorithm } from '../src/index'
|
import jwt, { JwtAlgorithm } from '../src/index'
|
||||||
|
|
||||||
type Dataset = {
|
type Dataset = {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { describe, expect, test } from '@jest/globals'
|
import { describe, expect, test } from 'vitest'
|
||||||
import {
|
import {
|
||||||
bytesToByteString,
|
bytesToByteString,
|
||||||
byteStringToBytes,
|
byteStringToBytes,
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
"target": "esnext",
|
"target": "esnext",
|
||||||
"lib": ["esnext"],
|
"lib": ["esnext"],
|
||||||
"declaration": true,
|
"declaration": true,
|
||||||
|
"emitDeclarationOnly": true,
|
||||||
"strict": true,
|
"strict": true,
|
||||||
"noImplicitAny": true,
|
"noImplicitAny": true,
|
||||||
"strictNullChecks": true,
|
"strictNullChecks": true,
|
||||||
|
|||||||
9
vite.config.ts
Normal file
9
vite.config.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import { defineConfig } from 'vitest/config'
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
test: {
|
||||||
|
environment: 'edge-runtime',
|
||||||
|
watch: false,
|
||||||
|
reporters: ['verbose']
|
||||||
|
}
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user