@upstash/qstash 2.5.0 → 2.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/chunk-CP4IU45K.mjs +59 -0
- package/chunk-UUR7N6E6.js +59 -0
- package/{dist/index.d.mts → index.d.mts} +22 -20
- package/{dist/index.d.ts → index.d.ts} +22 -20
- package/index.js +583 -0
- package/index.mjs +583 -0
- package/{dist/nextjs.d.mts → nextjs.d.mts} +4 -4
- package/{dist/nextjs.d.ts → nextjs.d.ts} +4 -4
- package/nextjs.js +142 -0
- package/nextjs.mjs +142 -0
- package/package.json +1 -54
- package/dist/chunk-EROSIHWE.js +0 -111
- package/dist/chunk-FK4ORXI6.mjs +0 -111
- package/dist/index.js +0 -634
- package/dist/index.mjs +0 -634
- package/dist/nextjs.js +0 -164
- package/dist/nextjs.mjs +0 -164
package/nextjs.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
|
+
|
|
3
|
+
var _chunkUUR7N6E6js = require('./chunk-UUR7N6E6.js');
|
|
4
|
+
|
|
5
|
+
// src/nextjs.ts
|
|
6
|
+
var _server = require('next/server');
|
|
7
|
+
var BAD_REQUEST = 400;
|
|
8
|
+
function verifySignature(handler, config) {
|
|
9
|
+
const currentSigningKey = _nullishCoalesce(_optionalChain([config, 'optionalAccess', _ => _.currentSigningKey]), () => ( process.env.QSTASH_CURRENT_SIGNING_KEY));
|
|
10
|
+
if (!currentSigningKey) {
|
|
11
|
+
throw new Error(
|
|
12
|
+
"currentSigningKey is required, either in the config or as env variable QSTASH_CURRENT_SIGNING_KEY"
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
const nextSigningKey = _nullishCoalesce(_optionalChain([config, 'optionalAccess', _2 => _2.nextSigningKey]), () => ( process.env.QSTASH_NEXT_SIGNING_KEY));
|
|
16
|
+
if (!nextSigningKey) {
|
|
17
|
+
throw new Error(
|
|
18
|
+
"nextSigningKey is required, either in the config or as env variable QSTASH_NEXT_SIGNING_KEY"
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
const receiver = new (0, _chunkUUR7N6E6js.Receiver)({
|
|
22
|
+
currentSigningKey,
|
|
23
|
+
nextSigningKey
|
|
24
|
+
});
|
|
25
|
+
return async (request, response) => {
|
|
26
|
+
const signature = request.headers["upstash-signature"];
|
|
27
|
+
if (!signature) {
|
|
28
|
+
response.status(BAD_REQUEST);
|
|
29
|
+
response.send("`Upstash-Signature` header is missing");
|
|
30
|
+
response.end();
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (typeof signature !== "string") {
|
|
34
|
+
throw new TypeError("`Upstash-Signature` header is not a string");
|
|
35
|
+
}
|
|
36
|
+
const chunks = [];
|
|
37
|
+
for await (const chunk of request) {
|
|
38
|
+
chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
|
|
39
|
+
}
|
|
40
|
+
const body = Buffer.concat(chunks).toString("utf8");
|
|
41
|
+
const isValid = await receiver.verify({
|
|
42
|
+
signature,
|
|
43
|
+
body,
|
|
44
|
+
clockTolerance: _optionalChain([config, 'optionalAccess', _3 => _3.clockTolerance])
|
|
45
|
+
});
|
|
46
|
+
if (!isValid) {
|
|
47
|
+
response.status(BAD_REQUEST);
|
|
48
|
+
response.send("Invalid signature");
|
|
49
|
+
response.end();
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
try {
|
|
53
|
+
request.body = request.headers["content-type"] === "application/json" ? JSON.parse(body) : body;
|
|
54
|
+
} catch (e) {
|
|
55
|
+
request.body = body;
|
|
56
|
+
}
|
|
57
|
+
return handler(request, response);
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function verifySignatureEdge(handler, config) {
|
|
61
|
+
const currentSigningKey = _nullishCoalesce(_optionalChain([config, 'optionalAccess', _4 => _4.currentSigningKey]), () => ( process.env.QSTASH_CURRENT_SIGNING_KEY));
|
|
62
|
+
if (!currentSigningKey) {
|
|
63
|
+
throw new Error(
|
|
64
|
+
"currentSigningKey is required, either in the config or as env variable QSTASH_CURRENT_SIGNING_KEY"
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
const nextSigningKey = _nullishCoalesce(_optionalChain([config, 'optionalAccess', _5 => _5.nextSigningKey]), () => ( process.env.QSTASH_NEXT_SIGNING_KEY));
|
|
68
|
+
if (!nextSigningKey) {
|
|
69
|
+
throw new Error(
|
|
70
|
+
"nextSigningKey is required, either in the config or as env variable QSTASH_NEXT_SIGNING_KEY"
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
const receiver = new (0, _chunkUUR7N6E6js.Receiver)({
|
|
74
|
+
currentSigningKey,
|
|
75
|
+
nextSigningKey
|
|
76
|
+
});
|
|
77
|
+
return async (request, nfe) => {
|
|
78
|
+
const signature = request.headers.get("upstash-signature");
|
|
79
|
+
if (!signature) {
|
|
80
|
+
return new (0, _server.NextResponse)(new TextEncoder().encode("`Upstash-Signature` header is missing"), {
|
|
81
|
+
status: 403
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
if (typeof signature !== "string") {
|
|
85
|
+
throw new TypeError("`Upstash-Signature` header is not a string");
|
|
86
|
+
}
|
|
87
|
+
const body = await request.text();
|
|
88
|
+
const isValid = await receiver.verify({
|
|
89
|
+
signature,
|
|
90
|
+
body,
|
|
91
|
+
clockTolerance: _optionalChain([config, 'optionalAccess', _6 => _6.clockTolerance])
|
|
92
|
+
});
|
|
93
|
+
if (!isValid) {
|
|
94
|
+
return new (0, _server.NextResponse)(new TextEncoder().encode("invalid signature"), { status: 403 });
|
|
95
|
+
}
|
|
96
|
+
return handler(request, nfe);
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
function verifySignatureAppRouter(handler, config) {
|
|
100
|
+
const currentSigningKey = _nullishCoalesce(_optionalChain([config, 'optionalAccess', _7 => _7.currentSigningKey]), () => ( process.env.QSTASH_CURRENT_SIGNING_KEY));
|
|
101
|
+
if (!currentSigningKey) {
|
|
102
|
+
throw new Error(
|
|
103
|
+
"currentSigningKey is required, either in the config or as env variable QSTASH_CURRENT_SIGNING_KEY"
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
const nextSigningKey = _nullishCoalesce(_optionalChain([config, 'optionalAccess', _8 => _8.nextSigningKey]), () => ( process.env.QSTASH_NEXT_SIGNING_KEY));
|
|
107
|
+
if (!nextSigningKey) {
|
|
108
|
+
throw new Error(
|
|
109
|
+
"nextSigningKey is required, either in the config or as env variable QSTASH_NEXT_SIGNING_KEY"
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
const receiver = new (0, _chunkUUR7N6E6js.Receiver)({
|
|
113
|
+
currentSigningKey,
|
|
114
|
+
nextSigningKey
|
|
115
|
+
});
|
|
116
|
+
return async (request) => {
|
|
117
|
+
const signature = request.headers.get("upstash-signature");
|
|
118
|
+
if (!signature) {
|
|
119
|
+
return new (0, _server.NextResponse)(new TextEncoder().encode("`Upstash-Signature` header is missing"), {
|
|
120
|
+
status: 403
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
if (typeof signature !== "string") {
|
|
124
|
+
throw new TypeError("`Upstash-Signature` header is not a string");
|
|
125
|
+
}
|
|
126
|
+
const body = await request.text();
|
|
127
|
+
const isValid = await receiver.verify({
|
|
128
|
+
signature,
|
|
129
|
+
body,
|
|
130
|
+
clockTolerance: _optionalChain([config, 'optionalAccess', _9 => _9.clockTolerance])
|
|
131
|
+
});
|
|
132
|
+
if (!isValid) {
|
|
133
|
+
return new (0, _server.NextResponse)(new TextEncoder().encode("invalid signature"), { status: 403 });
|
|
134
|
+
}
|
|
135
|
+
return handler(request);
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
exports.verifySignature = verifySignature; exports.verifySignatureAppRouter = verifySignatureAppRouter; exports.verifySignatureEdge = verifySignatureEdge;
|
package/nextjs.mjs
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Receiver
|
|
3
|
+
} from "./chunk-CP4IU45K.mjs";
|
|
4
|
+
|
|
5
|
+
// src/nextjs.ts
|
|
6
|
+
import { NextResponse } from "next/server";
|
|
7
|
+
var BAD_REQUEST = 400;
|
|
8
|
+
function verifySignature(handler, config) {
|
|
9
|
+
const currentSigningKey = config?.currentSigningKey ?? process.env.QSTASH_CURRENT_SIGNING_KEY;
|
|
10
|
+
if (!currentSigningKey) {
|
|
11
|
+
throw new Error(
|
|
12
|
+
"currentSigningKey is required, either in the config or as env variable QSTASH_CURRENT_SIGNING_KEY"
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
const nextSigningKey = config?.nextSigningKey ?? process.env.QSTASH_NEXT_SIGNING_KEY;
|
|
16
|
+
if (!nextSigningKey) {
|
|
17
|
+
throw new Error(
|
|
18
|
+
"nextSigningKey is required, either in the config or as env variable QSTASH_NEXT_SIGNING_KEY"
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
const receiver = new Receiver({
|
|
22
|
+
currentSigningKey,
|
|
23
|
+
nextSigningKey
|
|
24
|
+
});
|
|
25
|
+
return async (request, response) => {
|
|
26
|
+
const signature = request.headers["upstash-signature"];
|
|
27
|
+
if (!signature) {
|
|
28
|
+
response.status(BAD_REQUEST);
|
|
29
|
+
response.send("`Upstash-Signature` header is missing");
|
|
30
|
+
response.end();
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (typeof signature !== "string") {
|
|
34
|
+
throw new TypeError("`Upstash-Signature` header is not a string");
|
|
35
|
+
}
|
|
36
|
+
const chunks = [];
|
|
37
|
+
for await (const chunk of request) {
|
|
38
|
+
chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
|
|
39
|
+
}
|
|
40
|
+
const body = Buffer.concat(chunks).toString("utf8");
|
|
41
|
+
const isValid = await receiver.verify({
|
|
42
|
+
signature,
|
|
43
|
+
body,
|
|
44
|
+
clockTolerance: config?.clockTolerance
|
|
45
|
+
});
|
|
46
|
+
if (!isValid) {
|
|
47
|
+
response.status(BAD_REQUEST);
|
|
48
|
+
response.send("Invalid signature");
|
|
49
|
+
response.end();
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
try {
|
|
53
|
+
request.body = request.headers["content-type"] === "application/json" ? JSON.parse(body) : body;
|
|
54
|
+
} catch {
|
|
55
|
+
request.body = body;
|
|
56
|
+
}
|
|
57
|
+
return handler(request, response);
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function verifySignatureEdge(handler, config) {
|
|
61
|
+
const currentSigningKey = config?.currentSigningKey ?? process.env.QSTASH_CURRENT_SIGNING_KEY;
|
|
62
|
+
if (!currentSigningKey) {
|
|
63
|
+
throw new Error(
|
|
64
|
+
"currentSigningKey is required, either in the config or as env variable QSTASH_CURRENT_SIGNING_KEY"
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
const nextSigningKey = config?.nextSigningKey ?? process.env.QSTASH_NEXT_SIGNING_KEY;
|
|
68
|
+
if (!nextSigningKey) {
|
|
69
|
+
throw new Error(
|
|
70
|
+
"nextSigningKey is required, either in the config or as env variable QSTASH_NEXT_SIGNING_KEY"
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
const receiver = new Receiver({
|
|
74
|
+
currentSigningKey,
|
|
75
|
+
nextSigningKey
|
|
76
|
+
});
|
|
77
|
+
return async (request, nfe) => {
|
|
78
|
+
const signature = request.headers.get("upstash-signature");
|
|
79
|
+
if (!signature) {
|
|
80
|
+
return new NextResponse(new TextEncoder().encode("`Upstash-Signature` header is missing"), {
|
|
81
|
+
status: 403
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
if (typeof signature !== "string") {
|
|
85
|
+
throw new TypeError("`Upstash-Signature` header is not a string");
|
|
86
|
+
}
|
|
87
|
+
const body = await request.text();
|
|
88
|
+
const isValid = await receiver.verify({
|
|
89
|
+
signature,
|
|
90
|
+
body,
|
|
91
|
+
clockTolerance: config?.clockTolerance
|
|
92
|
+
});
|
|
93
|
+
if (!isValid) {
|
|
94
|
+
return new NextResponse(new TextEncoder().encode("invalid signature"), { status: 403 });
|
|
95
|
+
}
|
|
96
|
+
return handler(request, nfe);
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
function verifySignatureAppRouter(handler, config) {
|
|
100
|
+
const currentSigningKey = config?.currentSigningKey ?? process.env.QSTASH_CURRENT_SIGNING_KEY;
|
|
101
|
+
if (!currentSigningKey) {
|
|
102
|
+
throw new Error(
|
|
103
|
+
"currentSigningKey is required, either in the config or as env variable QSTASH_CURRENT_SIGNING_KEY"
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
const nextSigningKey = config?.nextSigningKey ?? process.env.QSTASH_NEXT_SIGNING_KEY;
|
|
107
|
+
if (!nextSigningKey) {
|
|
108
|
+
throw new Error(
|
|
109
|
+
"nextSigningKey is required, either in the config or as env variable QSTASH_NEXT_SIGNING_KEY"
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
const receiver = new Receiver({
|
|
113
|
+
currentSigningKey,
|
|
114
|
+
nextSigningKey
|
|
115
|
+
});
|
|
116
|
+
return async (request) => {
|
|
117
|
+
const signature = request.headers.get("upstash-signature");
|
|
118
|
+
if (!signature) {
|
|
119
|
+
return new NextResponse(new TextEncoder().encode("`Upstash-Signature` header is missing"), {
|
|
120
|
+
status: 403
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
if (typeof signature !== "string") {
|
|
124
|
+
throw new TypeError("`Upstash-Signature` header is not a string");
|
|
125
|
+
}
|
|
126
|
+
const body = await request.text();
|
|
127
|
+
const isValid = await receiver.verify({
|
|
128
|
+
signature,
|
|
129
|
+
body,
|
|
130
|
+
clockTolerance: config?.clockTolerance
|
|
131
|
+
});
|
|
132
|
+
if (!isValid) {
|
|
133
|
+
return new NextResponse(new TextEncoder().encode("invalid signature"), { status: 403 });
|
|
134
|
+
}
|
|
135
|
+
return handler(request);
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
export {
|
|
139
|
+
verifySignature,
|
|
140
|
+
verifySignatureAppRouter,
|
|
141
|
+
verifySignatureEdge
|
|
142
|
+
};
|
package/package.json
CHANGED
|
@@ -1,54 +1 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@upstash/qstash",
|
|
3
|
-
"version": "v2.5.0",
|
|
4
|
-
"description": "Official Typescript client for QStash",
|
|
5
|
-
"repository": {
|
|
6
|
-
"type": "git",
|
|
7
|
-
"url": "git+https://github.com/upstash/sdk-qstash-ts.git"
|
|
8
|
-
},
|
|
9
|
-
"module": "./dist/index.js",
|
|
10
|
-
"main": "./dist/index.js",
|
|
11
|
-
"types": "./dist/index.d.ts",
|
|
12
|
-
"keywords": [
|
|
13
|
-
"qstash",
|
|
14
|
-
"queue",
|
|
15
|
-
"events",
|
|
16
|
-
"serverless",
|
|
17
|
-
"upstash"
|
|
18
|
-
],
|
|
19
|
-
"author": "Andreas Thomas <dev@chronark.com>",
|
|
20
|
-
"license": "MIT",
|
|
21
|
-
"bugs": {
|
|
22
|
-
"url": "https://github.com/upstash/sdk-qstash-ts/issues"
|
|
23
|
-
},
|
|
24
|
-
"homepage": "https://github.com/upstash/sdk-qstash-ts#readme",
|
|
25
|
-
"files": [
|
|
26
|
-
"dist"
|
|
27
|
-
],
|
|
28
|
-
"devDependencies": {
|
|
29
|
-
"@biomejs/biome": "^1.3.3",
|
|
30
|
-
"@types/crypto-js": "^4.2.0",
|
|
31
|
-
"@types/node": "^20.5.7",
|
|
32
|
-
"next": "^14.0.2",
|
|
33
|
-
"tsup": "^7.2.0",
|
|
34
|
-
"typescript": "^5.2.2"
|
|
35
|
-
},
|
|
36
|
-
"dependencies": {
|
|
37
|
-
"crypto-js": ">=4.2.0",
|
|
38
|
-
"jose": "^ 5.2.3"
|
|
39
|
-
},
|
|
40
|
-
"typesVersions": {
|
|
41
|
-
"*": {
|
|
42
|
-
".": [
|
|
43
|
-
"./dist/index.d.ts"
|
|
44
|
-
],
|
|
45
|
-
"nextjs": [
|
|
46
|
-
"./dist/nextjs.d.ts"
|
|
47
|
-
]
|
|
48
|
-
}
|
|
49
|
-
},
|
|
50
|
-
"scripts": {
|
|
51
|
-
"build": "tsup",
|
|
52
|
-
"fmt": "pnpm rome check . --apply-unsafe && pnpm rome format . --write"
|
|
53
|
-
}
|
|
54
|
-
}
|
|
1
|
+
{"version":"v2.5.1","name":"@upstash/qstash","description":"Official Typescript client for QStash","author":"Andreas Thomas <dev@chronark.com>","license":"MIT","homepage":"https://github.com/upstash/sdk-qstash-ts#readme","repository":{"type":"git","url":"git+https://github.com/upstash/sdk-qstash-ts.git"},"bugs":{"url":"https://github.com/upstash/sdk-qstash-ts/issues"},"main":"./index.js","module":"./index.mjs","types":"./index.d.ts","files":["./**"],"exports":{".":{"import":"./index.mjs","require":"./index.js"},"./nextjs":{"import":"./nextjs.mjs","require":"./nextjs.js"},"./dist/nextjs":{"import":"./nextjs.mjs","require":"./nextjs.js"}},"typesVersions":{"*":{"nextjs":["./nextjs.d.ts"]}},"keywords":["qstash","queue","events","serverless","upstash"],"scripts":{"build":"tsup && cp README.md ./dist/ && cp package.json ./dist/ && cp LICENSE ./dist/","test":"bun test","fmt":"prettier --write .","lint":"tsc && eslint \"src/**/*.{js,ts,tsx}\" --quiet --fix"},"devDependencies":{"@biomejs/biome":"^1.3.3","@commitlint/cli":"^19.2.2","@commitlint/config-conventional":"^19.2.2","@types/crypto-js":"^4.2.0","@types/node":"^20.5.7","@typescript-eslint/eslint-plugin":"^7.0.1","@typescript-eslint/parser":"^7.0.1","bun-types":"latest","eslint":"^8","eslint-plugin-unicorn":"^51.0.1","husky":"^9.0.10","next":"^14.0.2","prettier":"^3.2.5","tsup":"latest","typescript":"^5.4.5","vitest":"latest"},"dependencies":{"crypto-js":">=4.2.0","jose":"^ 5.2.3","undici":"^6.16.0"}}
|
package/dist/chunk-EROSIHWE.js
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } }var __defProp = Object.defineProperty;
|
|
2
|
-
var __defProps = Object.defineProperties;
|
|
3
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
-
var __knownSymbol = (name, symbol) => {
|
|
8
|
-
if (symbol = Symbol[name])
|
|
9
|
-
return symbol;
|
|
10
|
-
throw Error("Symbol." + name + " is not defined");
|
|
11
|
-
};
|
|
12
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
13
|
-
var __spreadValues = (a, b) => {
|
|
14
|
-
for (var prop in b || (b = {}))
|
|
15
|
-
if (__hasOwnProp.call(b, prop))
|
|
16
|
-
__defNormalProp(a, prop, b[prop]);
|
|
17
|
-
if (__getOwnPropSymbols)
|
|
18
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
19
|
-
if (__propIsEnum.call(b, prop))
|
|
20
|
-
__defNormalProp(a, prop, b[prop]);
|
|
21
|
-
}
|
|
22
|
-
return a;
|
|
23
|
-
};
|
|
24
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
25
|
-
var __async = (__this, __arguments, generator) => {
|
|
26
|
-
return new Promise((resolve, reject) => {
|
|
27
|
-
var fulfilled = (value) => {
|
|
28
|
-
try {
|
|
29
|
-
step(generator.next(value));
|
|
30
|
-
} catch (e) {
|
|
31
|
-
reject(e);
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
var rejected = (value) => {
|
|
35
|
-
try {
|
|
36
|
-
step(generator.throw(value));
|
|
37
|
-
} catch (e) {
|
|
38
|
-
reject(e);
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
42
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
43
|
-
});
|
|
44
|
-
};
|
|
45
|
-
var __forAwait = (obj, it, method) => (it = obj[__knownSymbol("asyncIterator")]) ? it.call(obj) : (obj = obj[__knownSymbol("iterator")](), it = {}, method = (key, fn) => (fn = obj[key]) && (it[key] = (arg) => new Promise((yes, no, done) => (arg = fn.call(obj, arg), done = arg.done, Promise.resolve(arg.value).then((value) => yes({ value, done }), no)))), method("next"), method("return"), it);
|
|
46
|
-
|
|
47
|
-
// src/receiver.ts
|
|
48
|
-
var _jose = require('jose'); var jose = _interopRequireWildcard(_jose);
|
|
49
|
-
var _cryptojs = require('crypto-js'); var crypto = _interopRequireWildcard(_cryptojs);
|
|
50
|
-
var SignatureError = class extends Error {
|
|
51
|
-
constructor(message) {
|
|
52
|
-
super(message);
|
|
53
|
-
this.name = "SignatureError";
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
var Receiver = class {
|
|
57
|
-
constructor(config) {
|
|
58
|
-
this.currentSigningKey = config.currentSigningKey;
|
|
59
|
-
this.nextSigningKey = config.nextSigningKey;
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Verify the signature of a request.
|
|
63
|
-
*
|
|
64
|
-
* Tries to verify the signature with the current signing key.
|
|
65
|
-
* If that fails, maybe because you have rotated the keys recently, it will
|
|
66
|
-
* try to verify the signature with the next signing key.
|
|
67
|
-
*
|
|
68
|
-
* If that fails, the signature is invalid and a `SignatureError` is thrown.
|
|
69
|
-
*/
|
|
70
|
-
verify(req) {
|
|
71
|
-
return __async(this, null, function* () {
|
|
72
|
-
const isValid = yield this.verifyWithKey(this.currentSigningKey, req);
|
|
73
|
-
if (isValid) {
|
|
74
|
-
return true;
|
|
75
|
-
}
|
|
76
|
-
return this.verifyWithKey(this.nextSigningKey, req);
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Verify signature with a specific signing key
|
|
81
|
-
*/
|
|
82
|
-
verifyWithKey(key, req) {
|
|
83
|
-
return __async(this, null, function* () {
|
|
84
|
-
const jwt = yield jose.jwtVerify(req.signature, new TextEncoder().encode(key), {
|
|
85
|
-
issuer: "Upstash",
|
|
86
|
-
clockTolerance: req.clockTolerance
|
|
87
|
-
}).catch((e) => {
|
|
88
|
-
throw new SignatureError(e.message);
|
|
89
|
-
});
|
|
90
|
-
const p = jwt.payload;
|
|
91
|
-
if (typeof req.url !== "undefined" && p.sub !== req.url) {
|
|
92
|
-
throw new SignatureError(`invalid subject: ${p.sub}, want: ${req.url}`);
|
|
93
|
-
}
|
|
94
|
-
const bodyHash = crypto.SHA256(req.body).toString(crypto.enc.Base64url);
|
|
95
|
-
const padding = new RegExp(/=+$/);
|
|
96
|
-
if (p.body.replace(padding, "") !== bodyHash.replace(padding, "")) {
|
|
97
|
-
throw new SignatureError(`body hash does not match, want: ${p.body}, got: ${bodyHash}`);
|
|
98
|
-
}
|
|
99
|
-
return true;
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
exports.__spreadValues = __spreadValues; exports.__spreadProps = __spreadProps; exports.__async = __async; exports.__forAwait = __forAwait; exports.SignatureError = SignatureError; exports.Receiver = Receiver;
|
package/dist/chunk-FK4ORXI6.mjs
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defProps = Object.defineProperties;
|
|
3
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
-
var __knownSymbol = (name, symbol) => {
|
|
8
|
-
if (symbol = Symbol[name])
|
|
9
|
-
return symbol;
|
|
10
|
-
throw Error("Symbol." + name + " is not defined");
|
|
11
|
-
};
|
|
12
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
13
|
-
var __spreadValues = (a, b) => {
|
|
14
|
-
for (var prop in b || (b = {}))
|
|
15
|
-
if (__hasOwnProp.call(b, prop))
|
|
16
|
-
__defNormalProp(a, prop, b[prop]);
|
|
17
|
-
if (__getOwnPropSymbols)
|
|
18
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
19
|
-
if (__propIsEnum.call(b, prop))
|
|
20
|
-
__defNormalProp(a, prop, b[prop]);
|
|
21
|
-
}
|
|
22
|
-
return a;
|
|
23
|
-
};
|
|
24
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
25
|
-
var __async = (__this, __arguments, generator) => {
|
|
26
|
-
return new Promise((resolve, reject) => {
|
|
27
|
-
var fulfilled = (value) => {
|
|
28
|
-
try {
|
|
29
|
-
step(generator.next(value));
|
|
30
|
-
} catch (e) {
|
|
31
|
-
reject(e);
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
var rejected = (value) => {
|
|
35
|
-
try {
|
|
36
|
-
step(generator.throw(value));
|
|
37
|
-
} catch (e) {
|
|
38
|
-
reject(e);
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
42
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
43
|
-
});
|
|
44
|
-
};
|
|
45
|
-
var __forAwait = (obj, it, method) => (it = obj[__knownSymbol("asyncIterator")]) ? it.call(obj) : (obj = obj[__knownSymbol("iterator")](), it = {}, method = (key, fn) => (fn = obj[key]) && (it[key] = (arg) => new Promise((yes, no, done) => (arg = fn.call(obj, arg), done = arg.done, Promise.resolve(arg.value).then((value) => yes({ value, done }), no)))), method("next"), method("return"), it);
|
|
46
|
-
|
|
47
|
-
// src/receiver.ts
|
|
48
|
-
import * as jose from "jose";
|
|
49
|
-
import * as crypto from "crypto-js";
|
|
50
|
-
var SignatureError = class extends Error {
|
|
51
|
-
constructor(message) {
|
|
52
|
-
super(message);
|
|
53
|
-
this.name = "SignatureError";
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
var Receiver = class {
|
|
57
|
-
constructor(config) {
|
|
58
|
-
this.currentSigningKey = config.currentSigningKey;
|
|
59
|
-
this.nextSigningKey = config.nextSigningKey;
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Verify the signature of a request.
|
|
63
|
-
*
|
|
64
|
-
* Tries to verify the signature with the current signing key.
|
|
65
|
-
* If that fails, maybe because you have rotated the keys recently, it will
|
|
66
|
-
* try to verify the signature with the next signing key.
|
|
67
|
-
*
|
|
68
|
-
* If that fails, the signature is invalid and a `SignatureError` is thrown.
|
|
69
|
-
*/
|
|
70
|
-
verify(req) {
|
|
71
|
-
return __async(this, null, function* () {
|
|
72
|
-
const isValid = yield this.verifyWithKey(this.currentSigningKey, req);
|
|
73
|
-
if (isValid) {
|
|
74
|
-
return true;
|
|
75
|
-
}
|
|
76
|
-
return this.verifyWithKey(this.nextSigningKey, req);
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Verify signature with a specific signing key
|
|
81
|
-
*/
|
|
82
|
-
verifyWithKey(key, req) {
|
|
83
|
-
return __async(this, null, function* () {
|
|
84
|
-
const jwt = yield jose.jwtVerify(req.signature, new TextEncoder().encode(key), {
|
|
85
|
-
issuer: "Upstash",
|
|
86
|
-
clockTolerance: req.clockTolerance
|
|
87
|
-
}).catch((e) => {
|
|
88
|
-
throw new SignatureError(e.message);
|
|
89
|
-
});
|
|
90
|
-
const p = jwt.payload;
|
|
91
|
-
if (typeof req.url !== "undefined" && p.sub !== req.url) {
|
|
92
|
-
throw new SignatureError(`invalid subject: ${p.sub}, want: ${req.url}`);
|
|
93
|
-
}
|
|
94
|
-
const bodyHash = crypto.SHA256(req.body).toString(crypto.enc.Base64url);
|
|
95
|
-
const padding = new RegExp(/=+$/);
|
|
96
|
-
if (p.body.replace(padding, "") !== bodyHash.replace(padding, "")) {
|
|
97
|
-
throw new SignatureError(`body hash does not match, want: ${p.body}, got: ${bodyHash}`);
|
|
98
|
-
}
|
|
99
|
-
return true;
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
export {
|
|
105
|
-
__spreadValues,
|
|
106
|
-
__spreadProps,
|
|
107
|
-
__async,
|
|
108
|
-
__forAwait,
|
|
109
|
-
SignatureError,
|
|
110
|
-
Receiver
|
|
111
|
-
};
|