@upstash/qstash 0.0.13 → 0.0.18

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 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 a HTTP
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 server via
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
- ### Activate qStash
57
+ ### Get your authorization token
51
58
 
52
- Go to [upstash](https://console.upstash.com/qstash) and activate qStash.
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
- const q = new Client({
62
- token: <QSTASH_TOKEN>,
63
- })
64
-
65
- const res = await q.publishJSON({
66
- body: { hello: "world" },
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
- console.log(res.messageId)
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
- ### Consuming a message
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 { Consumer } from "@upstash/qstash";
94
+ import { Receiver } from "@upstash/qstash";
80
95
 
81
- const c = new Consumer({
96
+ const r = new Receiver({
82
97
  currentSigningKey: "..",
83
98
  nextSigningKey: "..",
84
99
  });
85
100
 
86
- const isValid = await c.verify({
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/features/qstash) for details.
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 c from "../pkg/consumer.js";
2
+ import * as r from "../pkg/receiver.js";
3
3
  export * from "../pkg/client/client.js";
4
- export class Consumer extends c.Consumer {
4
+ export class Receiver extends r.Receiver {
5
5
  constructor(config) {
6
6
  super({
7
7
  ...config,
@@ -1,7 +1,5 @@
1
1
  import * as dntShim from "../_dnt.shims.js";
2
- // @ts-ignore Deno can't compile
3
- import { buffer } from "micro";
4
- import { Consumer } from "../pkg/consumer.js";
2
+ import { Receiver } from "../pkg/receiver.js";
5
3
  export function verifySignature(handler, config) {
6
4
  const currentSigningKey = config?.currentSigningKey ??
7
5
  // @ts-ignore Deno can't compile
@@ -15,7 +13,7 @@ export function verifySignature(handler, config) {
15
13
  if (!nextSigningKey) {
16
14
  throw new Error("nextSigningKey is required, either in the config or as env variable QSTASH_NEXT_SIGNING_KEY");
17
15
  }
18
- const consumer = new Consumer({
16
+ const receiver = new Receiver({
19
17
  currentSigningKey,
20
18
  nextSigningKey,
21
19
  subtleCrypto: dntShim.crypto.subtle,
@@ -28,10 +26,16 @@ export function verifySignature(handler, config) {
28
26
  if (typeof signature !== "string") {
29
27
  throw new Error("`Upstash-Signature` header is not a string");
30
28
  }
31
- const body = (await buffer(req)).toString();
32
- console.log(req.headers);
33
- const url = config?.url ?? new URL(req.url, `https://${process.env.VERCEL_URL}`).href;
34
- const isValid = await consumer.verify({ signature, body, url });
29
+ const chunks = [];
30
+ for await (const chunk of req) {
31
+ chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
32
+ }
33
+ const body = Buffer.concat(chunks).toString("utf-8");
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 });
35
39
  if (!isValid) {
36
40
  res.status(400);
37
41
  res.send("Invalid signature");
@@ -47,7 +51,6 @@ export function verifySignature(handler, config) {
47
51
  }
48
52
  catch {
49
53
  req.body = body;
50
- console.log("body is not json");
51
54
  }
52
55
  return handler(req, res);
53
56
  };
@@ -1,11 +1,12 @@
1
1
  // dnt-shim-ignore
2
2
  import * as dntShim from "../_dnt.shims.js";
3
- import * as c from "../pkg/consumer.js";
3
+ import * as r from "../pkg/receiver.js";
4
4
  export * from "../pkg/client/client.js";
5
- export class Consumer extends c.Consumer {
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
- * Consumer offers a simlpe way to verify the signature of a request.
9
+ * Receiver offers a simlpe way to verify the signature of a request.
10
10
  */
11
- export class Consumer {
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
@@ -3,7 +3,7 @@
3
3
  "main": "./script/entrypoints/nodejs.js",
4
4
  "types": "./types/entrypoints/nodejs.d.ts",
5
5
  "name": "@upstash/qstash",
6
- "version": "v0.0.13",
6
+ "version": "v0.0.18",
7
7
  "description": "Official Deno/Typescript client for qStash",
8
8
  "repository": {
9
9
  "type": "git",
@@ -22,8 +22,18 @@
22
22
  "url": "https://github.com/upstash/sdk-qstash-ts/issues"
23
23
  },
24
24
  "homepage": "https://github.com/upstash/sdk-qstash-ts#readme",
25
- "peerDependencies": {
26
- "micro": "latest"
25
+ "typesVersions": {
26
+ "*": {
27
+ "nodejs": [
28
+ "./types/entrypoints/nodejs.d.ts"
29
+ ],
30
+ "cloudflare": [
31
+ "./types/entrypoints/cloudflare.d.ts"
32
+ ],
33
+ "nextjs": [
34
+ "./types/entrypoints/nextjs.d.ts"
35
+ ]
36
+ }
27
37
  },
28
38
  "exports": {
29
39
  ".": {
@@ -36,18 +46,18 @@
36
46
  "require": "./script/entrypoints/nodejs.js",
37
47
  "types": "./types/entrypoints/nodejs.d.ts"
38
48
  },
39
- "./nextjs": {
40
- "import": "./esm/entrypoints/nextjs.js",
41
- "require": "./script/entrypoints/nextjs.js",
42
- "types": "./types/entrypoints/nextjs.d.ts"
43
- },
44
49
  "./cloudflare": {
45
50
  "import": "./esm/entrypoints/cloudflare.js",
46
51
  "require": "./script/entrypoints/cloudflare.js",
47
52
  "types": "./types/entrypoints/cloudflare.d.ts"
53
+ },
54
+ "./nextjs": {
55
+ "import": "./esm/entrypoints/nextjs.js",
56
+ "require": "./script/entrypoints/nextjs.js",
57
+ "types": "./types/entrypoints/nextjs.d.ts"
48
58
  }
49
59
  },
50
60
  "dependencies": {
51
- "@deno/shim-crypto": "~0.3.1"
61
+ "@deno/shim-crypto": "~0.3.0"
52
62
  }
53
63
  }
@@ -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.Consumer = void 0;
31
- const c = __importStar(require("../pkg/consumer.js"));
30
+ exports.Receiver = void 0;
31
+ const r = __importStar(require("../pkg/receiver.js"));
32
32
  __exportStar(require("../pkg/client/client.js"), exports);
33
- class Consumer extends c.Consumer {
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.Consumer = Consumer;
42
+ exports.Receiver = Receiver;
@@ -25,9 +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
- // @ts-ignore Deno can't compile
29
- const micro_1 = require("micro");
30
- const consumer_js_1 = require("../pkg/consumer.js");
28
+ const receiver_js_1 = require("../pkg/receiver.js");
31
29
  function verifySignature(handler, config) {
32
30
  const currentSigningKey = config?.currentSigningKey ??
33
31
  // @ts-ignore Deno can't compile
@@ -41,7 +39,7 @@ function verifySignature(handler, config) {
41
39
  if (!nextSigningKey) {
42
40
  throw new Error("nextSigningKey is required, either in the config or as env variable QSTASH_NEXT_SIGNING_KEY");
43
41
  }
44
- const consumer = new consumer_js_1.Consumer({
42
+ const receiver = new receiver_js_1.Receiver({
45
43
  currentSigningKey,
46
44
  nextSigningKey,
47
45
  subtleCrypto: dntShim.crypto.subtle,
@@ -54,10 +52,16 @@ function verifySignature(handler, config) {
54
52
  if (typeof signature !== "string") {
55
53
  throw new Error("`Upstash-Signature` header is not a string");
56
54
  }
57
- const body = (await (0, micro_1.buffer)(req)).toString();
58
- console.log(req.headers);
59
- const url = config?.url ?? new URL(req.url, `https://${process.env.VERCEL_URL}`).href;
60
- const isValid = await consumer.verify({ signature, body, url });
55
+ const chunks = [];
56
+ for await (const chunk of req) {
57
+ chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
58
+ }
59
+ const body = Buffer.concat(chunks).toString("utf-8");
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 });
61
65
  if (!isValid) {
62
66
  res.status(400);
63
67
  res.send("Invalid signature");
@@ -73,7 +77,6 @@ function verifySignature(handler, config) {
73
77
  }
74
78
  catch {
75
79
  req.body = body;
76
- console.log("body is not json");
77
80
  }
78
81
  return handler(req, res);
79
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.Consumer = void 0;
29
+ exports.Receiver = void 0;
30
30
  // dnt-shim-ignore
31
31
  const dntShim = __importStar(require("../_dnt.shims.js"));
32
- const c = __importStar(require("../pkg/consumer.js"));
32
+ const r = __importStar(require("../pkg/receiver.js"));
33
33
  __exportStar(require("../pkg/client/client.js"), exports);
34
- class Consumer extends c.Consumer {
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.Consumer = Consumer;
43
+ exports.Receiver = Receiver;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Consumer = exports.SignatureError = void 0;
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
- * Consumer offers a simlpe way to verify the signature of a request.
13
+ * Receiver offers a simlpe way to verify the signature of a request.
14
14
  */
15
- class Consumer {
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.Consumer = Consumer;
95
+ exports.Receiver = Receiver;
@@ -23,7 +23,7 @@ declare const stdCrypto: {
23
23
  encrypt: (algorithm: dntShim.AlgorithmIdentifier | dntShim.RsaOaepParams | dntShim.AesCtrParams | dntShim.AesCbcParams | dntShim.AesGcmParams, key: dntShim.CryptoKey, data: dntShim.BufferSource) => Promise<any>;
24
24
  exportKey: {
25
25
  (format: "jwk", key: dntShim.CryptoKey): Promise<dntShim.JsonWebKey>;
26
- (format: "pkcs8" | "raw" | "spki", key: dntShim.CryptoKey): Promise<ArrayBuffer>;
26
+ (format: "raw" | "pkcs8" | "spki", key: dntShim.CryptoKey): Promise<ArrayBuffer>;
27
27
  };
28
28
  generateKey: {
29
29
  (algorithm: dntShim.RsaHashedKeyGenParams | dntShim.EcKeyGenParams, extractable: boolean, keyUsages: readonly dntShim.KeyUsage[]): Promise<dntShim.CryptoKeyPair>;
@@ -32,7 +32,7 @@ declare const stdCrypto: {
32
32
  };
33
33
  importKey: {
34
34
  (format: "jwk", keyData: dntShim.JsonWebKey, algorithm: dntShim.AlgorithmIdentifier | dntShim.HmacImportParams | dntShim.RsaHashedImportParams | dntShim.EcKeyImportParams | dntShim.AesKeyAlgorithm, extractable: boolean, keyUsages: readonly dntShim.KeyUsage[]): Promise<dntShim.CryptoKey>;
35
- (format: "pkcs8" | "raw" | "spki", keyData: dntShim.BufferSource, algorithm: dntShim.AlgorithmIdentifier | dntShim.HmacImportParams | dntShim.RsaHashedImportParams | dntShim.EcKeyImportParams | dntShim.AesKeyAlgorithm, extractable: boolean, keyUsages: dntShim.KeyUsage[]): Promise<dntShim.CryptoKey>;
35
+ (format: "raw" | "pkcs8" | "spki", keyData: dntShim.BufferSource, algorithm: dntShim.AlgorithmIdentifier | dntShim.HmacImportParams | dntShim.RsaHashedImportParams | dntShim.EcKeyImportParams | dntShim.AesKeyAlgorithm, extractable: boolean, keyUsages: dntShim.KeyUsage[]): Promise<dntShim.CryptoKey>;
36
36
  };
37
37
  sign: (algorithm: dntShim.AlgorithmIdentifier | dntShim.RsaPssParams | dntShim.EcdsaParams, key: dntShim.CryptoKey, data: dntShim.BufferSource) => Promise<ArrayBuffer>;
38
38
  unwrapKey: (format: dntShim.KeyFormat, wrappedKey: dntShim.BufferSource, unwrappingKey: dntShim.CryptoKey, unwrapAlgorithm: dntShim.AlgorithmIdentifier | dntShim.RsaOaepParams | dntShim.AesCtrParams | dntShim.AesCbcParams | dntShim.AesGcmParams, unwrappedKeyAlgorithm: dntShim.AlgorithmIdentifier | dntShim.HmacImportParams | dntShim.RsaHashedImportParams | dntShim.EcKeyImportParams | dntShim.AesKeyAlgorithm, extractable: boolean, keyUsages: dntShim.KeyUsage[]) => Promise<dntShim.CryptoKey>;
@@ -1,5 +1,5 @@
1
- import * as c from "../pkg/consumer.js";
1
+ import * as r from "../pkg/receiver.js";
2
2
  export * from "../pkg/client/client.js";
3
- export declare class Consumer extends c.Consumer {
4
- constructor(config: Omit<c.ConsumerConfig, "crypto">);
3
+ export declare class Receiver extends r.Receiver {
4
+ constructor(config: Omit<r.ReceiverConfig, "crypto">);
5
5
  }
@@ -1,5 +1,5 @@
1
- import * as c from "../pkg/consumer.js";
1
+ import * as r from "../pkg/receiver.js";
2
2
  export * from "../pkg/client/client.js";
3
- export declare class Consumer extends c.Consumer {
4
- constructor(config: Omit<c.ConsumerConfig, "crypto">);
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 ConsumerConfig = {
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: string;
31
+ url?: string;
30
32
  };
31
33
  export declare class SignatureError extends Error {
32
34
  constructor(message: string);
33
35
  }
34
36
  /**
35
- * Consumer offers a simlpe way to verify the signature of a request.
37
+ * Receiver offers a simlpe way to verify the signature of a request.
36
38
  */
37
- export declare class Consumer {
39
+ export declare class Receiver {
38
40
  private readonly currentSigningKey;
39
41
  private readonly nextSigningKey;
40
42
  private readonly subtleCrypto;
41
- constructor(config: ConsumerConfig);
43
+ constructor(config: ReceiverConfig);
42
44
  /**
43
45
  * Verify the signature of a request.
44
46
  *