@upstash/qstash 0.0.0 → 0.0.5

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
@@ -1 +1,119 @@
1
- # sdk-qstash-ts
1
+ # Upstash qStash SDK
2
+
3
+ [![Tests](https://github.com/upstash/upstash-redis/actions/workflows/tests.yaml/badge.svg)](https://github.com/upstash/upstash-redis/actions/workflows/tests.yaml)
4
+ ![npm (scoped)](https://img.shields.io/npm/v/@upstash/redis)
5
+ ![npm bundle size](https://img.shields.io/bundlephobia/minzip/@upstash/redis)
6
+
7
+ **qStash** is a serverless queueing / messaging system, designed to be used with
8
+ serverless functions to consume from the queue.
9
+
10
+ It is the only connectionless (HTTP based) Redis client and designed for:
11
+
12
+ - Serverless functions (AWS Lambda ...)
13
+ - Cloudflare Workers (see
14
+ [the example](https://github.com/upstash/upstash-redis/tree/main/examples/cloudflare-workers))
15
+ - Fastly Compute@Edge (see
16
+ [the example](https://github.com/upstash/upstash-redis/tree/main/examples/fastly))
17
+ - Next.js, Jamstack ...
18
+ - Client side web/mobile applications
19
+ - WebAssembly
20
+ - and other environments where HTTP is preferred over TCP.
21
+
22
+ See
23
+ [the list of APIs](https://docs.upstash.com/features/restapi#rest---redis-api-compatibility)
24
+ supported.
25
+
26
+ ## How does qStash work?
27
+
28
+ qStash is the message broker between your serverless apps. You send a HTTP
29
+ 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
31
+ HTTP. In case the destination is not ready to receive the message, we will retry
32
+ the message later, to guarentee at-least-once delivery.
33
+
34
+ ## Quick Start
35
+
36
+ ### Install
37
+
38
+ #### npm
39
+
40
+ ```bash
41
+ npm install @upstash/qstash
42
+ ```
43
+
44
+ #### Deno
45
+
46
+ ```ts
47
+ import { Redis } from "https://deno.land/x/upstash_qstash/mod.ts";
48
+ ```
49
+
50
+ ### Activate qStash
51
+
52
+ Go to [upstash](https://console.upstash.com/qstash) and activate qStash.
53
+
54
+ ## Basic Usage:
55
+
56
+ ### Publishing a message
57
+
58
+ ```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
+ })
68
+
69
+ console.log(res.messageID)
70
+ ```
71
+
72
+ ### Consuming a message
73
+
74
+ How to consume a message depends on your http server. QStash does not receive
75
+ the http request directly, but should be called by you as the first step in your
76
+ handler function.
77
+
78
+ ```ts
79
+ import { Consumer } from "@upstash/qstash";
80
+
81
+ const c = new Consumer({
82
+ currentSigningKey: "..",
83
+ nextSigningKey: "..",
84
+ });
85
+
86
+ const isValid = await c.verify({
87
+ /**
88
+ * The signature from the `upstash-signature` header.
89
+ */
90
+ signature: "string";
91
+
92
+ /**
93
+ * The raw request body.
94
+ */
95
+ body: "string";
96
+
97
+ /**
98
+ * URL of the endpoint where the request was sent to.
99
+ */
100
+ url: "string";
101
+ })
102
+ ```
103
+
104
+ ## Docs
105
+
106
+ See [the documentation](https://docs.upstash.com/features/qstash) for details.
107
+
108
+ ## Contributing
109
+
110
+ ### [Install Deno](https://deno.land/#installation)
111
+
112
+ ### Running tests
113
+
114
+ ```sh
115
+ QSTASH_TOKEN=".." deno test -A
116
+ ```
117
+
118
+ ```
119
+ ```
@@ -0,0 +1,65 @@
1
+ import { Deno } from "@deno/shim-deno";
2
+ export { Deno } from "@deno/shim-deno";
3
+ import { crypto } from "@deno/shim-crypto";
4
+ export { crypto } from "@deno/shim-crypto";
5
+ const dntGlobals = {
6
+ Deno,
7
+ crypto,
8
+ };
9
+ export const dntGlobalThis = createMergeProxy(globalThis, dntGlobals);
10
+ // deno-lint-ignore ban-types
11
+ function createMergeProxy(baseObj, extObj) {
12
+ return new Proxy(baseObj, {
13
+ get(_target, prop, _receiver) {
14
+ if (prop in extObj) {
15
+ return extObj[prop];
16
+ }
17
+ else {
18
+ return baseObj[prop];
19
+ }
20
+ },
21
+ set(_target, prop, value) {
22
+ if (prop in extObj) {
23
+ delete extObj[prop];
24
+ }
25
+ baseObj[prop] = value;
26
+ return true;
27
+ },
28
+ deleteProperty(_target, prop) {
29
+ let success = false;
30
+ if (prop in extObj) {
31
+ delete extObj[prop];
32
+ success = true;
33
+ }
34
+ if (prop in baseObj) {
35
+ delete baseObj[prop];
36
+ success = true;
37
+ }
38
+ return success;
39
+ },
40
+ ownKeys(_target) {
41
+ const baseKeys = Reflect.ownKeys(baseObj);
42
+ const extKeys = Reflect.ownKeys(extObj);
43
+ const extKeysSet = new Set(extKeys);
44
+ return [...baseKeys.filter((k) => !extKeysSet.has(k)), ...extKeys];
45
+ },
46
+ defineProperty(_target, prop, desc) {
47
+ if (prop in extObj) {
48
+ delete extObj[prop];
49
+ }
50
+ Reflect.defineProperty(baseObj, prop, desc);
51
+ return true;
52
+ },
53
+ getOwnPropertyDescriptor(_target, prop) {
54
+ if (prop in extObj) {
55
+ return Reflect.getOwnPropertyDescriptor(extObj, prop);
56
+ }
57
+ else {
58
+ return Reflect.getOwnPropertyDescriptor(baseObj, prop);
59
+ }
60
+ },
61
+ has(_target, prop) {
62
+ return prop in extObj || prop in baseObj;
63
+ },
64
+ });
65
+ }
@@ -0,0 +1,82 @@
1
+ import * as dntShim from "./_dnt.shims.js";
2
+ import * as base64url from "./deps/deno.land/std@0.144.0/encoding/base64url.js";
3
+ import * as base64 from "./deps/deno.land/std@0.144.0/encoding/base64.js";
4
+ export class SignatureError extends Error {
5
+ constructor(message) {
6
+ super(message);
7
+ this.name = "SignatureError";
8
+ }
9
+ }
10
+ /**
11
+ * Consumer offers a simlpe way to verify the signature of a request.
12
+ */
13
+ export class Consumer {
14
+ constructor(config) {
15
+ Object.defineProperty(this, "currentSigningKey", {
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true,
19
+ value: void 0
20
+ });
21
+ Object.defineProperty(this, "nextSigningKey", {
22
+ enumerable: true,
23
+ configurable: true,
24
+ writable: true,
25
+ value: void 0
26
+ });
27
+ this.currentSigningKey = config.currentSigningKey;
28
+ this.nextSigningKey = config.nextSigningKey;
29
+ }
30
+ /**
31
+ * Verify the signature of a request.
32
+ *
33
+ * Tries to verify the signature with the current signing key.
34
+ * If that fails, maybe because you have rotated the keys recently, it will
35
+ * try to verify the signature with the next signing key.
36
+ *
37
+ * If that fails, the signature is invalid and a `SignatureError` is thrown.
38
+ */
39
+ async verify(req) {
40
+ const isValid = await this.verifyWithKey(this.currentSigningKey, req);
41
+ if (isValid) {
42
+ return true;
43
+ }
44
+ return this.verifyWithKey(this.nextSigningKey, req);
45
+ }
46
+ /**
47
+ * Verify signature with a specific signing key
48
+ */
49
+ async verifyWithKey(key, req) {
50
+ const parts = req.signature.split(".");
51
+ if (parts.length !== 3) {
52
+ throw new SignatureError("`Upstash-Signature` header is not a valid signature");
53
+ }
54
+ const [header, payload, signature] = parts;
55
+ const k = await dntShim.crypto.subtle.importKey("raw", new TextEncoder().encode(key), { name: "HMAC", hash: "SHA-256" }, false, ["sign", "verify"]);
56
+ const isValid = await dntShim.crypto.subtle.verify({ name: "HMAC" }, k, base64url.decode(signature), new TextEncoder().encode(`${header}.${payload}`));
57
+ if (!isValid) {
58
+ throw new SignatureError("signature does not match");
59
+ }
60
+ const p = JSON.parse(new TextDecoder().decode(base64url.decode(payload)));
61
+ console.log(JSON.stringify(p, null, 2));
62
+ if (p.iss !== "Upstash") {
63
+ throw new SignatureError(`invalid issuer: ${p.iss}`);
64
+ }
65
+ if (p.sub !== req.url) {
66
+ throw new SignatureError(`invalid subject: ${p.sub}`);
67
+ }
68
+ const now = Math.floor(Date.now() / 1000);
69
+ if (now > p.exp) {
70
+ console.log({ now, exp: p.exp });
71
+ throw new SignatureError("token has expired");
72
+ }
73
+ if (now < p.nbf) {
74
+ throw new SignatureError("token is not yet valid");
75
+ }
76
+ const bodyHash = await dntShim.crypto.subtle.digest("SHA-256", new TextEncoder().encode(req.body));
77
+ if (p.body != base64.encode(bodyHash)) {
78
+ throw new SignatureError("body hash does not match");
79
+ }
80
+ return true;
81
+ }
82
+ }
@@ -0,0 +1,115 @@
1
+ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
2
+ // This module is browser compatible.
3
+ const base64abc = [
4
+ "A",
5
+ "B",
6
+ "C",
7
+ "D",
8
+ "E",
9
+ "F",
10
+ "G",
11
+ "H",
12
+ "I",
13
+ "J",
14
+ "K",
15
+ "L",
16
+ "M",
17
+ "N",
18
+ "O",
19
+ "P",
20
+ "Q",
21
+ "R",
22
+ "S",
23
+ "T",
24
+ "U",
25
+ "V",
26
+ "W",
27
+ "X",
28
+ "Y",
29
+ "Z",
30
+ "a",
31
+ "b",
32
+ "c",
33
+ "d",
34
+ "e",
35
+ "f",
36
+ "g",
37
+ "h",
38
+ "i",
39
+ "j",
40
+ "k",
41
+ "l",
42
+ "m",
43
+ "n",
44
+ "o",
45
+ "p",
46
+ "q",
47
+ "r",
48
+ "s",
49
+ "t",
50
+ "u",
51
+ "v",
52
+ "w",
53
+ "x",
54
+ "y",
55
+ "z",
56
+ "0",
57
+ "1",
58
+ "2",
59
+ "3",
60
+ "4",
61
+ "5",
62
+ "6",
63
+ "7",
64
+ "8",
65
+ "9",
66
+ "+",
67
+ "/",
68
+ ];
69
+ /**
70
+ * CREDIT: https://gist.github.com/enepomnyaschih/72c423f727d395eeaa09697058238727
71
+ * Encodes a given Uint8Array, ArrayBuffer or string into RFC4648 base64 representation
72
+ * @param data
73
+ */
74
+ export function encode(data) {
75
+ const uint8 = typeof data === "string"
76
+ ? new TextEncoder().encode(data)
77
+ : data instanceof Uint8Array
78
+ ? data
79
+ : new Uint8Array(data);
80
+ let result = "", i;
81
+ const l = uint8.length;
82
+ for (i = 2; i < l; i += 3) {
83
+ result += base64abc[uint8[i - 2] >> 2];
84
+ result += base64abc[((uint8[i - 2] & 0x03) << 4) | (uint8[i - 1] >> 4)];
85
+ result += base64abc[((uint8[i - 1] & 0x0f) << 2) | (uint8[i] >> 6)];
86
+ result += base64abc[uint8[i] & 0x3f];
87
+ }
88
+ if (i === l + 1) {
89
+ // 1 octet yet to write
90
+ result += base64abc[uint8[i - 2] >> 2];
91
+ result += base64abc[(uint8[i - 2] & 0x03) << 4];
92
+ result += "==";
93
+ }
94
+ if (i === l) {
95
+ // 2 octets yet to write
96
+ result += base64abc[uint8[i - 2] >> 2];
97
+ result += base64abc[((uint8[i - 2] & 0x03) << 4) | (uint8[i - 1] >> 4)];
98
+ result += base64abc[(uint8[i - 1] & 0x0f) << 2];
99
+ result += "=";
100
+ }
101
+ return result;
102
+ }
103
+ /**
104
+ * Decodes a given RFC4648 base64 encoded string
105
+ * @param b64
106
+ */
107
+ export function decode(b64) {
108
+ const binString = atob(b64);
109
+ const size = binString.length;
110
+ const bytes = new Uint8Array(size);
111
+ for (let i = 0; i < size; i++) {
112
+ bytes[i] = binString.charCodeAt(i);
113
+ }
114
+ return bytes;
115
+ }
@@ -0,0 +1,42 @@
1
+ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
2
+ // This module is browser compatible.
3
+ import * as base64 from "./base64.js";
4
+ /*
5
+ * Some variants allow or require omitting the padding '=' signs:
6
+ * https://en.wikipedia.org/wiki/Base64#The_URL_applications
7
+ * @param base64url
8
+ */
9
+ export function addPaddingToBase64url(base64url) {
10
+ if (base64url.length % 4 === 2)
11
+ return base64url + "==";
12
+ if (base64url.length % 4 === 3)
13
+ return base64url + "=";
14
+ if (base64url.length % 4 === 1) {
15
+ throw new TypeError("Illegal base64url string!");
16
+ }
17
+ return base64url;
18
+ }
19
+ function convertBase64urlToBase64(b64url) {
20
+ if (!/^[-_A-Z0-9]*?={0,2}$/i.test(b64url)) {
21
+ // Contains characters not part of base64url spec.
22
+ throw new TypeError("Failed to decode base64url: invalid character");
23
+ }
24
+ return addPaddingToBase64url(b64url).replace(/\-/g, "+").replace(/_/g, "/");
25
+ }
26
+ function convertBase64ToBase64url(b64) {
27
+ return b64.replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
28
+ }
29
+ /**
30
+ * Encodes a given ArrayBuffer or string into a base64url representation
31
+ * @param data
32
+ */
33
+ export function encode(data) {
34
+ return convertBase64ToBase64url(base64.encode(data));
35
+ }
36
+ /**
37
+ * Converts given base64url encoded data back to original
38
+ * @param b64url
39
+ */
40
+ export function decode(b64url) {
41
+ return base64.decode(convertBase64urlToBase64(b64url));
42
+ }
@@ -0,0 +1,48 @@
1
+ import { buffer } from "micro";
2
+ import { Consumer } from "../consumer.js";
3
+ export function verifySignature(handler, config) {
4
+ const currentSigningKey = config?.currentSigningKey ??
5
+ process.env.get["QSTASH_CURRENT_SIGNING_KEY"];
6
+ if (!currentSigningKey) {
7
+ throw new Error("currentSigningKey is required, either in the config or as env variable QSTASH_CURRENT_SIGNING_KEY");
8
+ }
9
+ const nextSigningKey = config?.nextSigningKey ??
10
+ process.env.get["QSTASH_NEXT_SIGNING_KEY"];
11
+ if (!nextSigningKey) {
12
+ throw new Error("nextSigningKey is required, either in the config or as env variable QSTASH_NEXT_SIGNING_KEY");
13
+ }
14
+ const consumer = new Consumer({
15
+ currentSigningKey,
16
+ nextSigningKey,
17
+ });
18
+ return async (req, res) => {
19
+ const signature = req.headers["upstash-signature"];
20
+ if (!signature) {
21
+ throw new Error("`Upstash-Signature` header is missing");
22
+ }
23
+ if (typeof signature !== "string") {
24
+ throw new Error("`Upstash-Signature` header is not a string");
25
+ }
26
+ const body = (await buffer(req)).toString();
27
+ const url = new URL(req.url, `http://${req.headers.host}`).href;
28
+ const isValid = await consumer.verify({ signature, body, url });
29
+ if (!isValid) {
30
+ res.status(400);
31
+ res.send("Invalid signature");
32
+ return res.end();
33
+ }
34
+ try {
35
+ if (req.headers["content-type"] === "application/json") {
36
+ req.body = JSON.parse(body);
37
+ }
38
+ else {
39
+ req.body = body;
40
+ }
41
+ }
42
+ catch {
43
+ req.body = body;
44
+ console.log("body is not json");
45
+ }
46
+ return handler(req, res);
47
+ };
48
+ }
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
- "module": "./esm/platforms/nodejs.js",
3
- "main": "./script/platforms/nodejs.js",
4
- "types": "./types/platforms/nodejs.d.ts",
2
+ "module": "./esm/entrypoints/nextjs.js",
3
+ "main": "./script/entrypoints/nextjs.js",
4
+ "types": "./types/entrypoints/nextjs.d.ts",
5
5
  "name": "@upstash/qstash",
6
- "version": "v0.0.0",
6
+ "version": "v0.0.5",
7
7
  "description": "Official Deno/Typescript client for qStash",
8
8
  "repository": {
9
9
  "type": "git",
@@ -23,29 +23,23 @@
23
23
  },
24
24
  "homepage": "https://github.com/upstash/sdk-qstash-ts#readme",
25
25
  "devDependencies": {
26
- "@size-limit/preset-small-lib": "latest",
27
- "size-limit": "latest"
26
+ "micro": "latest",
27
+ "next": "latest"
28
28
  },
29
29
  "typesVersions": {
30
30
  "*": {
31
- "nodejs": "./types/platforms/nodejs.d.ts"
31
+ "nextjs": "./types/entrypoints/nextjs.d.ts"
32
32
  }
33
33
  },
34
- "size-limit": [
35
- {
36
- "path": "esm/platforms/nodejs.js",
37
- "limit": "6 KB"
38
- },
39
- {
40
- "path": "script/platforms/nodejs.js",
41
- "limit": "10 KB"
42
- }
43
- ],
44
34
  "exports": {
45
- ".": {
46
- "import": "./esm/platforms/nodejs.js",
47
- "require": "./script/platforms/nodejs.js",
48
- "types": "./types/platforms/nodejs.d.ts"
35
+ "./nextjs": {
36
+ "import": "./esm/entrypoints/nextjs.js",
37
+ "require": "./script/entrypoints/nextjs.js",
38
+ "types": "./types/entrypoints/nextjs.d.ts"
49
39
  }
40
+ },
41
+ "dependencies": {
42
+ "@deno/shim-crypto": "~0.2.0",
43
+ "@deno/shim-deno": "~0.5.0"
50
44
  }
51
45
  }
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.dntGlobalThis = exports.crypto = exports.Deno = void 0;
4
+ const shim_deno_1 = require("@deno/shim-deno");
5
+ var shim_deno_2 = require("@deno/shim-deno");
6
+ Object.defineProperty(exports, "Deno", { enumerable: true, get: function () { return shim_deno_2.Deno; } });
7
+ const shim_crypto_1 = require("@deno/shim-crypto");
8
+ var shim_crypto_2 = require("@deno/shim-crypto");
9
+ Object.defineProperty(exports, "crypto", { enumerable: true, get: function () { return shim_crypto_2.crypto; } });
10
+ const dntGlobals = {
11
+ Deno: shim_deno_1.Deno,
12
+ crypto: shim_crypto_1.crypto,
13
+ };
14
+ exports.dntGlobalThis = createMergeProxy(globalThis, dntGlobals);
15
+ // deno-lint-ignore ban-types
16
+ function createMergeProxy(baseObj, extObj) {
17
+ return new Proxy(baseObj, {
18
+ get(_target, prop, _receiver) {
19
+ if (prop in extObj) {
20
+ return extObj[prop];
21
+ }
22
+ else {
23
+ return baseObj[prop];
24
+ }
25
+ },
26
+ set(_target, prop, value) {
27
+ if (prop in extObj) {
28
+ delete extObj[prop];
29
+ }
30
+ baseObj[prop] = value;
31
+ return true;
32
+ },
33
+ deleteProperty(_target, prop) {
34
+ let success = false;
35
+ if (prop in extObj) {
36
+ delete extObj[prop];
37
+ success = true;
38
+ }
39
+ if (prop in baseObj) {
40
+ delete baseObj[prop];
41
+ success = true;
42
+ }
43
+ return success;
44
+ },
45
+ ownKeys(_target) {
46
+ const baseKeys = Reflect.ownKeys(baseObj);
47
+ const extKeys = Reflect.ownKeys(extObj);
48
+ const extKeysSet = new Set(extKeys);
49
+ return [...baseKeys.filter((k) => !extKeysSet.has(k)), ...extKeys];
50
+ },
51
+ defineProperty(_target, prop, desc) {
52
+ if (prop in extObj) {
53
+ delete extObj[prop];
54
+ }
55
+ Reflect.defineProperty(baseObj, prop, desc);
56
+ return true;
57
+ },
58
+ getOwnPropertyDescriptor(_target, prop) {
59
+ if (prop in extObj) {
60
+ return Reflect.getOwnPropertyDescriptor(extObj, prop);
61
+ }
62
+ else {
63
+ return Reflect.getOwnPropertyDescriptor(baseObj, prop);
64
+ }
65
+ },
66
+ has(_target, prop) {
67
+ return prop in extObj || prop in baseObj;
68
+ },
69
+ });
70
+ }