@upstash/qstash 0.0.14 → 0.0.17

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,5 +1,5 @@
1
1
  import * as dntShim from "../_dnt.shims.js";
2
- import { Consumer } from "../pkg/consumer.js";
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 consumer = new Consumer({
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 === 'string' ? Buffer.from(chunk) : 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 body = (await buffer(req)).toString();
35
- console.log(req.headers, body);
36
- const url = config?.url ?? new URL(req.url, `https://${process.env.VERCEL_URL}`).href;
37
- const isValid = await consumer.verify({ signature, body, url });
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 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
  }
@@ -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.14",
6
+ "version": "v0.0.17",
7
7
  "description": "Official Deno/Typescript client for qStash",
8
8
  "repository": {
9
9
  "type": "git",
@@ -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,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 consumer_js_1 = require("../pkg/consumer.js");
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 consumer = new consumer_js_1.Consumer({
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 === 'string' ? Buffer.from(chunk) : 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 body = (await buffer(req)).toString();
61
- console.log(req.headers, body);
62
- const url = config?.url ?? new URL(req.url, `https://${process.env.VERCEL_URL}`).href;
63
- const isValid = await consumer.verify({ signature, body, url });
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.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;
@@ -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);
@@ -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
  }
@@ -26,7 +26,7 @@ export declare type VerifyRequest = {
26
26
  /**
27
27
  * URL of the endpoint where the request was sent to.
28
28
  */
29
- url: string;
29
+ url?: string;
30
30
  };
31
31
  export declare class SignatureError extends Error {
32
32
  constructor(message: string);