@upstash/qstash 0.0.14 → 0.1.0
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 +34 -28
- package/esm/entrypoints/cloudflare.js +2 -2
- package/esm/entrypoints/nextjs.js +8 -8
- package/esm/entrypoints/nodejs.js +3 -2
- package/esm/pkg/{consumer.js → receiver.js} +3 -4
- package/package.json +1 -1
- package/script/entrypoints/cloudflare.js +4 -4
- package/script/entrypoints/nextjs.js +8 -8
- package/script/entrypoints/nodejs.js +5 -4
- package/script/pkg/{consumer.js → receiver.js} +5 -6
- package/types/entrypoints/cloudflare.d.ts +3 -3
- package/types/entrypoints/nodejs.d.ts +3 -3
- package/types/pkg/{consumer.d.ts → receiver.d.ts} +7 -5
package/README.md
CHANGED
|
@@ -23,11 +23,18 @@ See
|
|
|
23
23
|
[the list of APIs](https://docs.upstash.com/features/restapi#rest---redis-api-compatibility)
|
|
24
24
|
supported.
|
|
25
25
|
|
|
26
|
+
## Status of the SDK
|
|
27
|
+
|
|
28
|
+
It is currently in beta and we are actively collecting feedback from the
|
|
29
|
+
community. Please report any issues you encounter or feature requests in the
|
|
30
|
+
[GitHub issues](https://github.com/upstash/sdk-qstash-ts/issues) or talk to us
|
|
31
|
+
on [Discord](https://discord.gg/w9SenAtbme). Thank you!
|
|
32
|
+
|
|
26
33
|
## How does qStash work?
|
|
27
34
|
|
|
28
|
-
qStash is the message broker between your serverless apps. You send
|
|
35
|
+
qStash is the message broker between your serverless apps. You send aa HTTP
|
|
29
36
|
request to qStash, that includes a destination, a payload and optional settings.
|
|
30
|
-
We store your message durable and will deliver it to the destination
|
|
37
|
+
We store your message durable and will deliver it to the destination API via
|
|
31
38
|
HTTP. In case the destination is not ready to receive the message, we will retry
|
|
32
39
|
the message later, to guarentee at-least-once delivery.
|
|
33
40
|
|
|
@@ -47,43 +54,51 @@ npm install @upstash/qstash
|
|
|
47
54
|
import { Redis } from "https://deno.land/x/upstash_qstash/mod.ts";
|
|
48
55
|
```
|
|
49
56
|
|
|
50
|
-
###
|
|
57
|
+
### Get your authorization token
|
|
51
58
|
|
|
52
|
-
Go to [upstash](https://console.upstash.com/qstash) and
|
|
59
|
+
Go to [upstash](https://console.upstash.com/qstash) and copy the token.
|
|
53
60
|
|
|
54
61
|
## Basic Usage:
|
|
55
62
|
|
|
56
63
|
### Publishing a message
|
|
57
64
|
|
|
58
65
|
```ts
|
|
59
|
-
import { Client } from "@upstash/qstash"
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
import { Client } from "@upstash/qstash";
|
|
67
|
+
/**
|
|
68
|
+
* Import a fetch polyfill only if you are using node prior to v18.
|
|
69
|
+
* This is not necessary for nextjs, deno or cloudflare workers.
|
|
70
|
+
*/
|
|
71
|
+
import "isomorphic-fetch";
|
|
72
|
+
|
|
73
|
+
const c = new Client({
|
|
74
|
+
token: "<QSTASH_TOKEN>",
|
|
75
|
+
});
|
|
68
76
|
|
|
69
|
-
|
|
77
|
+
const res = await c.publishJSON({
|
|
78
|
+
destination: "https://my-api...",
|
|
79
|
+
body: {
|
|
80
|
+
hello: "world",
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
console.log(res);
|
|
84
|
+
// { messageId: "msg_xxxxxxxxxxxxxxxx" }
|
|
70
85
|
```
|
|
71
86
|
|
|
72
|
-
###
|
|
87
|
+
### Receiving a message
|
|
73
88
|
|
|
74
89
|
How to consume a message depends on your http server. QStash does not receive
|
|
75
90
|
the http request directly, but should be called by you as the first step in your
|
|
76
91
|
handler function.
|
|
77
92
|
|
|
78
93
|
```ts
|
|
79
|
-
import {
|
|
94
|
+
import { Receiver } from "@upstash/qstash";
|
|
80
95
|
|
|
81
|
-
const
|
|
96
|
+
const r = new Receiver({
|
|
82
97
|
currentSigningKey: "..",
|
|
83
98
|
nextSigningKey: "..",
|
|
84
99
|
});
|
|
85
100
|
|
|
86
|
-
const isValid = await
|
|
101
|
+
const isValid = await r.verify({
|
|
87
102
|
/**
|
|
88
103
|
* The signature from the `Upstash-Signature` header.
|
|
89
104
|
*/
|
|
@@ -103,17 +118,8 @@ const isValid = await c.verify({
|
|
|
103
118
|
|
|
104
119
|
## Docs
|
|
105
120
|
|
|
106
|
-
See [the documentation](https://docs.upstash.com/
|
|
121
|
+
See [the documentation](https://docs.upstash.com/qstash) for details.
|
|
107
122
|
|
|
108
123
|
## Contributing
|
|
109
124
|
|
|
110
125
|
### [Install Deno](https://deno.land/#installation)
|
|
111
|
-
|
|
112
|
-
### Running tests
|
|
113
|
-
|
|
114
|
-
```sh
|
|
115
|
-
QSTASH_TOKEN=".." deno test -A
|
|
116
|
-
```
|
|
117
|
-
|
|
118
|
-
```
|
|
119
|
-
```
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// dnt-shim-ignore
|
|
2
|
-
import * as
|
|
2
|
+
import * as r from "../pkg/receiver.js";
|
|
3
3
|
export * from "../pkg/client/client.js";
|
|
4
|
-
export class
|
|
4
|
+
export class Receiver extends r.Receiver {
|
|
5
5
|
constructor(config) {
|
|
6
6
|
super({
|
|
7
7
|
...config,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as dntShim from "../_dnt.shims.js";
|
|
2
|
-
import {
|
|
2
|
+
import { Receiver } from "../pkg/receiver.js";
|
|
3
3
|
export function verifySignature(handler, config) {
|
|
4
4
|
const currentSigningKey = config?.currentSigningKey ??
|
|
5
5
|
// @ts-ignore Deno can't compile
|
|
@@ -13,7 +13,7 @@ export function verifySignature(handler, config) {
|
|
|
13
13
|
if (!nextSigningKey) {
|
|
14
14
|
throw new Error("nextSigningKey is required, either in the config or as env variable QSTASH_NEXT_SIGNING_KEY");
|
|
15
15
|
}
|
|
16
|
-
const
|
|
16
|
+
const receiver = new Receiver({
|
|
17
17
|
currentSigningKey,
|
|
18
18
|
nextSigningKey,
|
|
19
19
|
subtleCrypto: dntShim.crypto.subtle,
|
|
@@ -28,13 +28,14 @@ export function verifySignature(handler, config) {
|
|
|
28
28
|
}
|
|
29
29
|
const chunks = [];
|
|
30
30
|
for await (const chunk of req) {
|
|
31
|
-
chunks.push(typeof chunk ===
|
|
31
|
+
chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
|
|
32
32
|
}
|
|
33
33
|
const body = Buffer.concat(chunks).toString("utf-8");
|
|
34
|
-
// const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
34
|
+
// const url = config?.url ?? new URL(
|
|
35
|
+
// req.url!,
|
|
36
|
+
// `https://${process.env.VERCEL_URL}`,
|
|
37
|
+
// ).href;
|
|
38
|
+
const isValid = await receiver.verify({ signature, body });
|
|
38
39
|
if (!isValid) {
|
|
39
40
|
res.status(400);
|
|
40
41
|
res.send("Invalid signature");
|
|
@@ -50,7 +51,6 @@ export function verifySignature(handler, config) {
|
|
|
50
51
|
}
|
|
51
52
|
catch {
|
|
52
53
|
req.body = body;
|
|
53
|
-
console.log("body is not json");
|
|
54
54
|
}
|
|
55
55
|
return handler(req, res);
|
|
56
56
|
};
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
// dnt-shim-ignore
|
|
2
2
|
import * as dntShim from "../_dnt.shims.js";
|
|
3
|
-
import * as
|
|
3
|
+
import * as r from "../pkg/receiver.js";
|
|
4
4
|
export * from "../pkg/client/client.js";
|
|
5
|
-
export class
|
|
5
|
+
export class Receiver extends r.Receiver {
|
|
6
6
|
constructor(config) {
|
|
7
7
|
super({
|
|
8
8
|
...config,
|
|
9
|
+
// use the shimmed one from deno
|
|
9
10
|
subtleCrypto: dntShim.crypto.subtle,
|
|
10
11
|
});
|
|
11
12
|
}
|
|
@@ -6,9 +6,9 @@ export class SignatureError extends Error {
|
|
|
6
6
|
}
|
|
7
7
|
}
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
9
|
+
* Receiver offers a simlpe way to verify the signature of a request.
|
|
10
10
|
*/
|
|
11
|
-
export class
|
|
11
|
+
export class Receiver {
|
|
12
12
|
constructor(config) {
|
|
13
13
|
Object.defineProperty(this, "currentSigningKey", {
|
|
14
14
|
enumerable: true,
|
|
@@ -63,11 +63,10 @@ export class Consumer {
|
|
|
63
63
|
throw new SignatureError("signature does not match");
|
|
64
64
|
}
|
|
65
65
|
const p = JSON.parse(new TextDecoder().decode(base64Url.decode(payload)));
|
|
66
|
-
console.log(JSON.stringify(p, null, 2));
|
|
67
66
|
if (p.iss !== "Upstash") {
|
|
68
67
|
throw new SignatureError(`invalid issuer: ${p.iss}`);
|
|
69
68
|
}
|
|
70
|
-
if (p.sub !== req.url) {
|
|
69
|
+
if (typeof req.url !== "undefined" && p.sub !== req.url) {
|
|
71
70
|
throw new SignatureError(`invalid subject: ${p.sub}, want: ${req.url}`);
|
|
72
71
|
}
|
|
73
72
|
const now = Math.floor(Date.now() / 1000);
|
package/package.json
CHANGED
|
@@ -27,10 +27,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
27
27
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
28
28
|
};
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.
|
|
31
|
-
const
|
|
30
|
+
exports.Receiver = void 0;
|
|
31
|
+
const r = __importStar(require("../pkg/receiver.js"));
|
|
32
32
|
__exportStar(require("../pkg/client/client.js"), exports);
|
|
33
|
-
class
|
|
33
|
+
class Receiver extends r.Receiver {
|
|
34
34
|
constructor(config) {
|
|
35
35
|
super({
|
|
36
36
|
...config,
|
|
@@ -39,4 +39,4 @@ class Consumer extends c.Consumer {
|
|
|
39
39
|
});
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
|
-
exports.
|
|
42
|
+
exports.Receiver = Receiver;
|
|
@@ -25,7 +25,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
26
|
exports.verifySignature = void 0;
|
|
27
27
|
const dntShim = __importStar(require("../_dnt.shims.js"));
|
|
28
|
-
const
|
|
28
|
+
const receiver_js_1 = require("../pkg/receiver.js");
|
|
29
29
|
function verifySignature(handler, config) {
|
|
30
30
|
const currentSigningKey = config?.currentSigningKey ??
|
|
31
31
|
// @ts-ignore Deno can't compile
|
|
@@ -39,7 +39,7 @@ function verifySignature(handler, config) {
|
|
|
39
39
|
if (!nextSigningKey) {
|
|
40
40
|
throw new Error("nextSigningKey is required, either in the config or as env variable QSTASH_NEXT_SIGNING_KEY");
|
|
41
41
|
}
|
|
42
|
-
const
|
|
42
|
+
const receiver = new receiver_js_1.Receiver({
|
|
43
43
|
currentSigningKey,
|
|
44
44
|
nextSigningKey,
|
|
45
45
|
subtleCrypto: dntShim.crypto.subtle,
|
|
@@ -54,13 +54,14 @@ function verifySignature(handler, config) {
|
|
|
54
54
|
}
|
|
55
55
|
const chunks = [];
|
|
56
56
|
for await (const chunk of req) {
|
|
57
|
-
chunks.push(typeof chunk ===
|
|
57
|
+
chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
|
|
58
58
|
}
|
|
59
59
|
const body = Buffer.concat(chunks).toString("utf-8");
|
|
60
|
-
// const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
60
|
+
// const url = config?.url ?? new URL(
|
|
61
|
+
// req.url!,
|
|
62
|
+
// `https://${process.env.VERCEL_URL}`,
|
|
63
|
+
// ).href;
|
|
64
|
+
const isValid = await receiver.verify({ signature, body });
|
|
64
65
|
if (!isValid) {
|
|
65
66
|
res.status(400);
|
|
66
67
|
res.send("Invalid signature");
|
|
@@ -76,7 +77,6 @@ function verifySignature(handler, config) {
|
|
|
76
77
|
}
|
|
77
78
|
catch {
|
|
78
79
|
req.body = body;
|
|
79
|
-
console.log("body is not json");
|
|
80
80
|
}
|
|
81
81
|
return handler(req, res);
|
|
82
82
|
};
|
|
@@ -26,17 +26,18 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
26
26
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.
|
|
29
|
+
exports.Receiver = void 0;
|
|
30
30
|
// dnt-shim-ignore
|
|
31
31
|
const dntShim = __importStar(require("../_dnt.shims.js"));
|
|
32
|
-
const
|
|
32
|
+
const r = __importStar(require("../pkg/receiver.js"));
|
|
33
33
|
__exportStar(require("../pkg/client/client.js"), exports);
|
|
34
|
-
class
|
|
34
|
+
class Receiver extends r.Receiver {
|
|
35
35
|
constructor(config) {
|
|
36
36
|
super({
|
|
37
37
|
...config,
|
|
38
|
+
// use the shimmed one from deno
|
|
38
39
|
subtleCrypto: dntShim.crypto.subtle,
|
|
39
40
|
});
|
|
40
41
|
}
|
|
41
42
|
}
|
|
42
|
-
exports.
|
|
43
|
+
exports.Receiver = Receiver;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.Receiver = exports.SignatureError = void 0;
|
|
4
4
|
const deps_js_1 = require("../deps.js");
|
|
5
5
|
class SignatureError extends Error {
|
|
6
6
|
constructor(message) {
|
|
@@ -10,9 +10,9 @@ class SignatureError extends Error {
|
|
|
10
10
|
}
|
|
11
11
|
exports.SignatureError = SignatureError;
|
|
12
12
|
/**
|
|
13
|
-
*
|
|
13
|
+
* Receiver offers a simlpe way to verify the signature of a request.
|
|
14
14
|
*/
|
|
15
|
-
class
|
|
15
|
+
class Receiver {
|
|
16
16
|
constructor(config) {
|
|
17
17
|
Object.defineProperty(this, "currentSigningKey", {
|
|
18
18
|
enumerable: true,
|
|
@@ -67,11 +67,10 @@ class Consumer {
|
|
|
67
67
|
throw new SignatureError("signature does not match");
|
|
68
68
|
}
|
|
69
69
|
const p = JSON.parse(new TextDecoder().decode(deps_js_1.base64Url.decode(payload)));
|
|
70
|
-
console.log(JSON.stringify(p, null, 2));
|
|
71
70
|
if (p.iss !== "Upstash") {
|
|
72
71
|
throw new SignatureError(`invalid issuer: ${p.iss}`);
|
|
73
72
|
}
|
|
74
|
-
if (p.sub !== req.url) {
|
|
73
|
+
if (typeof req.url !== "undefined" && p.sub !== req.url) {
|
|
75
74
|
throw new SignatureError(`invalid subject: ${p.sub}, want: ${req.url}`);
|
|
76
75
|
}
|
|
77
76
|
const now = Math.floor(Date.now() / 1000);
|
|
@@ -93,4 +92,4 @@ class Consumer {
|
|
|
93
92
|
return true;
|
|
94
93
|
}
|
|
95
94
|
}
|
|
96
|
-
exports.
|
|
95
|
+
exports.Receiver = Receiver;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as r from "../pkg/receiver.js";
|
|
2
2
|
export * from "../pkg/client/client.js";
|
|
3
|
-
export declare class
|
|
4
|
-
constructor(config: Omit<
|
|
3
|
+
export declare class Receiver extends r.Receiver {
|
|
4
|
+
constructor(config: Omit<r.ReceiverConfig, "crypto">);
|
|
5
5
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as r from "../pkg/receiver.js";
|
|
2
2
|
export * from "../pkg/client/client.js";
|
|
3
|
-
export declare class
|
|
4
|
-
constructor(config: Omit<
|
|
3
|
+
export declare class Receiver extends r.Receiver {
|
|
4
|
+
constructor(config: Omit<r.ReceiverConfig, "crypto">);
|
|
5
5
|
}
|
|
@@ -3,7 +3,7 @@ export declare type SubtleCrypto = typeof crypto.subtle;
|
|
|
3
3
|
/**
|
|
4
4
|
* Necessary to verify the signature of a request.
|
|
5
5
|
*/
|
|
6
|
-
export declare type
|
|
6
|
+
export declare type ReceiverConfig = {
|
|
7
7
|
/**
|
|
8
8
|
* The current signing key. Get it from `https://console.upstash.com/qstash
|
|
9
9
|
*/
|
|
@@ -25,20 +25,22 @@ export declare type VerifyRequest = {
|
|
|
25
25
|
body: string | Uint8Array;
|
|
26
26
|
/**
|
|
27
27
|
* URL of the endpoint where the request was sent to.
|
|
28
|
+
*
|
|
29
|
+
* Omit empty to disable checking the url.
|
|
28
30
|
*/
|
|
29
|
-
url
|
|
31
|
+
url?: string;
|
|
30
32
|
};
|
|
31
33
|
export declare class SignatureError extends Error {
|
|
32
34
|
constructor(message: string);
|
|
33
35
|
}
|
|
34
36
|
/**
|
|
35
|
-
*
|
|
37
|
+
* Receiver offers a simlpe way to verify the signature of a request.
|
|
36
38
|
*/
|
|
37
|
-
export declare class
|
|
39
|
+
export declare class Receiver {
|
|
38
40
|
private readonly currentSigningKey;
|
|
39
41
|
private readonly nextSigningKey;
|
|
40
42
|
private readonly subtleCrypto;
|
|
41
|
-
constructor(config:
|
|
43
|
+
constructor(config: ReceiverConfig);
|
|
42
44
|
/**
|
|
43
45
|
* Verify the signature of a request.
|
|
44
46
|
*
|