@upstash/qstash 0.1.6 → 0.1.8

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.
Files changed (60) hide show
  1. package/README.md +9 -14
  2. package/esm/deps/deno.land/{std@0.149.0 → std@0.161.0}/crypto/_fnv/fnv32.js +0 -0
  3. package/esm/deps/deno.land/{std@0.149.0 → std@0.161.0}/crypto/_fnv/fnv64.js +0 -0
  4. package/esm/deps/deno.land/{std@0.149.0 → std@0.161.0}/crypto/_fnv/index.js +0 -0
  5. package/esm/deps/deno.land/{std@0.149.0 → std@0.161.0}/crypto/_fnv/util.js +0 -0
  6. package/esm/deps/deno.land/std@0.161.0/crypto/_wasm_crypto/lib/deno_std_wasm_crypto.generated.mjs +3253 -0
  7. package/esm/deps/deno.land/{std@0.149.0 → std@0.161.0/crypto}/_wasm_crypto/mod.js +0 -0
  8. package/esm/deps/deno.land/std@0.161.0/crypto/keystack.js +150 -0
  9. package/esm/deps/deno.land/{std@0.149.0 → std@0.161.0}/crypto/mod.js +26 -8
  10. package/esm/deps/deno.land/std@0.161.0/crypto/timing_safe_equal.js +24 -0
  11. package/esm/deps/deno.land/std@0.161.0/crypto/util.js +27 -0
  12. package/esm/deps/deno.land/std@0.161.0/encoding/base64.js +122 -0
  13. package/esm/deps/deno.land/std@0.161.0/encoding/base64url.js +49 -0
  14. package/esm/deps/deno.land/std@0.161.0/encoding/hex.js +60 -0
  15. package/esm/deps/deno.land/std@0.161.0/fmt/colors.js +442 -0
  16. package/esm/deps/deno.land/std@0.161.0/testing/_diff.js +308 -0
  17. package/esm/deps/deno.land/std@0.161.0/testing/_format.js +23 -0
  18. package/esm/deps/deno.land/std@0.161.0/testing/asserts.js +615 -0
  19. package/esm/pkg/client/client.js +1 -1
  20. package/package.json +2 -2
  21. package/script/deps/deno.land/{std@0.149.0 → std@0.161.0}/crypto/_fnv/fnv32.js +0 -0
  22. package/script/deps/deno.land/{std@0.149.0 → std@0.161.0}/crypto/_fnv/fnv64.js +0 -0
  23. package/script/deps/deno.land/{std@0.149.0 → std@0.161.0}/crypto/_fnv/index.js +0 -0
  24. package/script/deps/deno.land/{std@0.149.0 → std@0.161.0}/crypto/_fnv/util.js +0 -0
  25. package/script/deps/deno.land/std@0.161.0/crypto/_wasm_crypto/lib/deno_std_wasm_crypto.generated.mjs +3261 -0
  26. package/script/deps/deno.land/{std@0.149.0 → std@0.161.0/crypto}/_wasm_crypto/mod.js +0 -0
  27. package/script/deps/deno.land/std@0.161.0/crypto/keystack.js +177 -0
  28. package/script/deps/deno.land/{std@0.149.0 → std@0.161.0}/crypto/mod.js +30 -10
  29. package/script/deps/deno.land/std@0.161.0/crypto/timing_safe_equal.js +28 -0
  30. package/script/deps/deno.land/std@0.161.0/crypto/util.js +54 -0
  31. package/script/deps/deno.land/std@0.161.0/encoding/base64.js +127 -0
  32. package/script/deps/deno.land/std@0.161.0/encoding/base64url.js +77 -0
  33. package/script/deps/deno.land/std@0.161.0/encoding/hex.js +65 -0
  34. package/script/deps/deno.land/std@0.161.0/fmt/colors.js +516 -0
  35. package/script/deps/deno.land/std@0.161.0/testing/_diff.js +314 -0
  36. package/script/deps/deno.land/std@0.161.0/testing/_format.js +50 -0
  37. package/script/deps/deno.land/std@0.161.0/testing/asserts.js +641 -0
  38. package/script/pkg/client/client.js +1 -1
  39. package/types/deps/deno.land/{std@0.149.0 → std@0.161.0}/crypto/_fnv/fnv32.d.ts +0 -0
  40. package/types/deps/deno.land/{std@0.149.0 → std@0.161.0}/crypto/_fnv/fnv64.d.ts +0 -0
  41. package/types/deps/deno.land/{std@0.149.0 → std@0.161.0}/crypto/_fnv/index.d.ts +0 -0
  42. package/types/deps/deno.land/{std@0.149.0 → std@0.161.0}/crypto/_fnv/util.d.ts +0 -0
  43. package/types/deps/deno.land/{std@0.149.0 → std@0.161.0/crypto}/_wasm_crypto/lib/deno_std_wasm_crypto.generated.d.mts +0 -0
  44. package/types/deps/deno.land/{std@0.149.0 → std@0.161.0/crypto}/_wasm_crypto/mod.d.ts +0 -0
  45. package/types/deps/deno.land/std@0.161.0/crypto/keystack.d.ts +47 -0
  46. package/types/deps/deno.land/std@0.161.0/crypto/mod.d.ts +56 -0
  47. package/types/deps/deno.land/std@0.161.0/crypto/timing_safe_equal.d.ts +3 -0
  48. package/types/deps/deno.land/std@0.161.0/crypto/util.d.ts +16 -0
  49. package/types/deps/deno.land/std@0.161.0/encoding/base64.d.ts +11 -0
  50. package/types/deps/deno.land/std@0.161.0/encoding/base64url.d.ts +10 -0
  51. package/types/deps/deno.land/std@0.161.0/encoding/hex.d.ts +7 -0
  52. package/types/deps/deno.land/std@0.161.0/fmt/colors.d.ts +270 -0
  53. package/types/deps/deno.land/std@0.161.0/testing/_diff.d.ts +26 -0
  54. package/types/deps/deno.land/std@0.161.0/testing/_format.d.ts +1 -0
  55. package/types/deps/deno.land/std@0.161.0/testing/asserts.d.ts +160 -0
  56. package/types/pkg/client/messages.d.ts +1 -1
  57. package/types/pkg/receiver.d.ts +1 -1
  58. package/esm/deps/deno.land/std@0.149.0/_wasm_crypto/lib/deno_std_wasm_crypto.generated.mjs +0 -3159
  59. package/script/deps/deno.land/std@0.149.0/_wasm_crypto/lib/deno_std_wasm_crypto.generated.mjs +0 -3167
  60. package/types/deps/deno.land/std@0.149.0/crypto/mod.d.ts +0 -53
@@ -0,0 +1,150 @@
1
+ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
2
+ // This module is browser compatible.
3
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
4
+ if (kind === "m") throw new TypeError("Private method is not writable");
5
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
6
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
7
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
8
+ };
9
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
10
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
11
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
12
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
13
+ };
14
+ var _KeyStack_instances, _KeyStack_cryptoKeys, _KeyStack_keys, _KeyStack_toCryptoKey;
15
+ /**
16
+ * Provides the {@linkcode KeyStack} class which implements the
17
+ * {@linkcode KeyRing} interface for managing rotatable keys.
18
+ *
19
+ * @module
20
+ */
21
+ import * as dntShim from "../../../../_dnt.shims.js";
22
+ import { timingSafeEqual } from "./timing_safe_equal.js";
23
+ import * as base64url from "../encoding/base64url.js";
24
+ const encoder = new TextEncoder();
25
+ function importKey(key) {
26
+ if (typeof key === "string") {
27
+ key = encoder.encode(key);
28
+ }
29
+ else if (Array.isArray(key)) {
30
+ key = new Uint8Array(key);
31
+ }
32
+ return dntShim.crypto.subtle.importKey("raw", key, {
33
+ name: "HMAC",
34
+ hash: { name: "SHA-256" },
35
+ }, true, ["sign", "verify"]);
36
+ }
37
+ function sign(data, key) {
38
+ if (typeof data === "string") {
39
+ data = encoder.encode(data);
40
+ }
41
+ else if (Array.isArray(data)) {
42
+ data = Uint8Array.from(data);
43
+ }
44
+ return dntShim.crypto.subtle.sign("HMAC", key, data);
45
+ }
46
+ /** Compare two strings, Uint8Arrays, ArrayBuffers, or arrays of numbers in a
47
+ * way that avoids timing based attacks on the comparisons on the values.
48
+ *
49
+ * The function will return `true` if the values match, or `false`, if they
50
+ * do not match.
51
+ *
52
+ * This was inspired by https://github.com/suryagh/tsscmp which provides a
53
+ * timing safe string comparison to avoid timing attacks as described in
54
+ * https://codahale.com/a-lesson-in-timing-attacks/.
55
+ */
56
+ async function compare(a, b) {
57
+ const key = new Uint8Array(32);
58
+ dntShim.dntGlobalThis.crypto.getRandomValues(key);
59
+ const cryptoKey = await importKey(key);
60
+ const ah = await sign(a, cryptoKey);
61
+ const bh = await sign(b, cryptoKey);
62
+ return timingSafeEqual(ah, bh);
63
+ }
64
+ /** A cryptographic key chain which allows signing of data to prevent tampering,
65
+ * but also allows for easy key rotation without needing to re-sign the data.
66
+ *
67
+ * Data is signed as SHA256 HMAC.
68
+ *
69
+ * This was inspired by [keygrip](https://github.com/crypto-utils/keygrip/).
70
+ *
71
+ * ### Example
72
+ *
73
+ * ```ts
74
+ * import { KeyStack } from "https://deno.land/std@$STD_VERSION/crypto/keystack.ts";
75
+ *
76
+ * const keyStack = new KeyStack(["hello", "world"]);
77
+ * const digest = await keyStack.sign("some data");
78
+ *
79
+ * const rotatedStack = new KeyStack(["deno", "says", "hello", "world"]);
80
+ * await rotatedStack.verify("some data", digest); // true
81
+ * ```
82
+ */
83
+ export class KeyStack {
84
+ /** A class which accepts an array of keys that are used to sign and verify
85
+ * data and allows easy key rotation without invalidation of previously signed
86
+ * data.
87
+ *
88
+ * @param keys An iterable of keys, of which the index 0 will be used to sign
89
+ * data, but verification can happen against any key.
90
+ */
91
+ constructor(keys) {
92
+ _KeyStack_instances.add(this);
93
+ _KeyStack_cryptoKeys.set(this, new Map());
94
+ _KeyStack_keys.set(this, void 0);
95
+ const values = Array.isArray(keys) ? keys : [...keys];
96
+ if (!(values.length)) {
97
+ throw new TypeError("keys must contain at least one value");
98
+ }
99
+ __classPrivateFieldSet(this, _KeyStack_keys, values, "f");
100
+ }
101
+ get length() {
102
+ return __classPrivateFieldGet(this, _KeyStack_keys, "f").length;
103
+ }
104
+ /** Take `data` and return a SHA256 HMAC digest that uses the current 0 index
105
+ * of the `keys` passed to the constructor. This digest is in the form of a
106
+ * URL safe base64 encoded string. */
107
+ async sign(data) {
108
+ const key = await __classPrivateFieldGet(this, _KeyStack_instances, "m", _KeyStack_toCryptoKey).call(this, __classPrivateFieldGet(this, _KeyStack_keys, "f")[0]);
109
+ return base64url.encode(await sign(data, key));
110
+ }
111
+ /** Given `data` and a `digest`, verify that one of the `keys` provided the
112
+ * constructor was used to generate the `digest`. Returns `true` if one of
113
+ * the keys was used, otherwise `false`. */
114
+ async verify(data, digest) {
115
+ return (await this.indexOf(data, digest)) > -1;
116
+ }
117
+ /** Given `data` and a `digest`, return the current index of the key in the
118
+ * `keys` passed the constructor that was used to generate the digest. If no
119
+ * key can be found, the method returns `-1`. */
120
+ async indexOf(data, digest) {
121
+ for (let i = 0; i < __classPrivateFieldGet(this, _KeyStack_keys, "f").length; i++) {
122
+ const cryptoKey = await __classPrivateFieldGet(this, _KeyStack_instances, "m", _KeyStack_toCryptoKey).call(this, __classPrivateFieldGet(this, _KeyStack_keys, "f")[i]);
123
+ if (await compare(digest, base64url.encode(await sign(data, cryptoKey)))) {
124
+ return i;
125
+ }
126
+ }
127
+ return -1;
128
+ }
129
+ [(_KeyStack_cryptoKeys = new WeakMap(), _KeyStack_keys = new WeakMap(), _KeyStack_instances = new WeakSet(), _KeyStack_toCryptoKey = async function _KeyStack_toCryptoKey(key) {
130
+ if (!__classPrivateFieldGet(this, _KeyStack_cryptoKeys, "f").has(key)) {
131
+ __classPrivateFieldGet(this, _KeyStack_cryptoKeys, "f").set(key, await importKey(key));
132
+ }
133
+ return __classPrivateFieldGet(this, _KeyStack_cryptoKeys, "f").get(key);
134
+ }, Symbol.for("Deno.customInspect"))](inspect) {
135
+ const { length } = this;
136
+ return `${this.constructor.name} ${inspect({ length })}`;
137
+ }
138
+ [Symbol.for("nodejs.util.inspect.custom")](depth,
139
+ // deno-lint-ignore no-explicit-any
140
+ options, inspect) {
141
+ if (depth < 0) {
142
+ return options.stylize(`[${this.constructor.name}]`, "special");
143
+ }
144
+ const newOptions = Object.assign({}, options, {
145
+ depth: options.depth === null ? null : options.depth - 1,
146
+ });
147
+ const { length } = this;
148
+ return `${options.stylize(this.constructor.name, "special")} ${inspect({ length }, newOptions)}`;
149
+ }
150
+ }
@@ -1,11 +1,31 @@
1
1
  // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
2
+ /**
3
+ * Extensions to the
4
+ * [Web Crypto](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API)
5
+ * supporting additional encryption APIs.
6
+ *
7
+ * Provides additional digest algorithms that are not part of the WebCrypto
8
+ * standard as well as a `subtle.digest` and `subtle.digestSync` methods. It
9
+ * also provide a `subtle.timingSafeEqual()` method to compare array buffers
10
+ * or data views in a way that isn't prone to timing based attacks.
11
+ *
12
+ * The "polyfill" delegates to `WebCrypto` where possible.
13
+ *
14
+ * The {@linkcode KeyStack} export implements the {@linkcode KeyRing} interface
15
+ * for managing rotatable keys for signing data to prevent tampering, like with
16
+ * HTTP cookies.
17
+ *
18
+ * @module
19
+ */
2
20
  import * as dntShim from "../../../../_dnt.shims.js";
3
- import { digestAlgorithms as wasmDigestAlgorithms, instantiateWasm, } from "../_wasm_crypto/mod.js";
21
+ import { digestAlgorithms as wasmDigestAlgorithms, instantiateWasm, } from "./_wasm_crypto/mod.js";
22
+ import { timingSafeEqual } from "./timing_safe_equal.js";
4
23
  import { fnv } from "./_fnv/index.js";
24
+ export { KeyStack } from "./keystack.js";
25
+ export { toHashString } from "./util.js";
5
26
  /**
6
27
  * A copy of the global WebCrypto interface, with methods bound so they're
7
28
  * safe to re-export.
8
- * @module
9
29
  */
10
30
  const webCrypto = ((crypto) => ({
11
31
  getRandomValues: crypto.getRandomValues?.bind(crypto),
@@ -48,8 +68,8 @@ const stdCrypto = ((x) => x)({
48
68
  subtle: {
49
69
  ...webCrypto.subtle,
50
70
  /**
51
- * Returns a new `Promise` object that will digest `data` using the specified
52
- * `AlgorithmIdentifier`.
71
+ * Polyfills stream support until the Web Crypto API does so:
72
+ * @see {@link https://github.com/wintercg/proposal-webcrypto-streams}
53
73
  */
54
74
  async digest(algorithm, data) {
55
75
  const { name, length } = normalizeAlgorithm(algorithm);
@@ -101,10 +121,6 @@ const stdCrypto = ((x) => x)({
101
121
  throw new TypeError(`unsupported digest algorithm: ${algorithm}`);
102
122
  }
103
123
  },
104
- /**
105
- * Returns a ArrayBuffer with the result of digesting `data` using the
106
- * specified `AlgorithmIdentifier`.
107
- */
108
124
  digestSync(algorithm, data) {
109
125
  algorithm = normalizeAlgorithm(algorithm);
110
126
  const bytes = bufferSourceBytes(data);
@@ -131,6 +147,8 @@ const stdCrypto = ((x) => x)({
131
147
  throw new TypeError("data must be a BufferSource or Iterable<BufferSource>");
132
148
  }
133
149
  },
150
+ // TODO(@kitsonk): rework when https://github.com/w3c/webcrypto/issues/270 resolved
151
+ timingSafeEqual,
134
152
  },
135
153
  });
136
154
  const FNVAlgorithms = ["FNV32", "FNV32A", "FNV64", "FNV64A"];
@@ -0,0 +1,24 @@
1
+ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
2
+ import { assert } from "../testing/asserts.js";
3
+ /** Compare to array buffers or data views in a way that timing based attacks
4
+ * cannot gain information about the platform. */
5
+ export function timingSafeEqual(a, b) {
6
+ if (a.byteLength !== b.byteLength) {
7
+ return false;
8
+ }
9
+ if (!(a instanceof DataView)) {
10
+ a = new DataView(ArrayBuffer.isView(a) ? a.buffer : a);
11
+ }
12
+ if (!(b instanceof DataView)) {
13
+ b = new DataView(ArrayBuffer.isView(b) ? b.buffer : b);
14
+ }
15
+ assert(a instanceof DataView);
16
+ assert(b instanceof DataView);
17
+ const length = a.byteLength;
18
+ let out = 0;
19
+ let i = -1;
20
+ while (++i < length) {
21
+ out |= a.getUint8(i) ^ b.getUint8(i);
22
+ }
23
+ return out === 0;
24
+ }
@@ -0,0 +1,27 @@
1
+ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
2
+ import * as hex from "../encoding/hex.js";
3
+ import * as base64 from "../encoding/base64.js";
4
+ const decoder = new TextDecoder();
5
+ /**
6
+ * Converts a hash to a string with a given encoding.
7
+ * @example
8
+ * ```ts
9
+ * import { crypto, toHashString } from "https://deno.land/std@$STD_VERSION/crypto/mod.ts";
10
+ *
11
+ * const hash = await crypto.subtle.digest("SHA-384", new TextEncoder().encode("You hear that Mr. Anderson?"));
12
+ *
13
+ * // Hex encoding by default
14
+ * console.log(toHashString(hash));
15
+ *
16
+ * // Or with base64 encoding
17
+ * console.log(toHashString(hash, "base64"));
18
+ * ```
19
+ */
20
+ export function toHashString(hash, encoding = "hex") {
21
+ switch (encoding) {
22
+ case "hex":
23
+ return decoder.decode(hex.encode(new Uint8Array(hash)));
24
+ case "base64":
25
+ return base64.encode(hash);
26
+ }
27
+ }
@@ -0,0 +1,122 @@
1
+ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
2
+ /**
3
+ * {@linkcode encode} and {@linkcode decode} for
4
+ * [base64](https://en.wikipedia.org/wiki/Base64) encoding.
5
+ *
6
+ * This module is browser compatible.
7
+ *
8
+ * @module
9
+ */
10
+ const base64abc = [
11
+ "A",
12
+ "B",
13
+ "C",
14
+ "D",
15
+ "E",
16
+ "F",
17
+ "G",
18
+ "H",
19
+ "I",
20
+ "J",
21
+ "K",
22
+ "L",
23
+ "M",
24
+ "N",
25
+ "O",
26
+ "P",
27
+ "Q",
28
+ "R",
29
+ "S",
30
+ "T",
31
+ "U",
32
+ "V",
33
+ "W",
34
+ "X",
35
+ "Y",
36
+ "Z",
37
+ "a",
38
+ "b",
39
+ "c",
40
+ "d",
41
+ "e",
42
+ "f",
43
+ "g",
44
+ "h",
45
+ "i",
46
+ "j",
47
+ "k",
48
+ "l",
49
+ "m",
50
+ "n",
51
+ "o",
52
+ "p",
53
+ "q",
54
+ "r",
55
+ "s",
56
+ "t",
57
+ "u",
58
+ "v",
59
+ "w",
60
+ "x",
61
+ "y",
62
+ "z",
63
+ "0",
64
+ "1",
65
+ "2",
66
+ "3",
67
+ "4",
68
+ "5",
69
+ "6",
70
+ "7",
71
+ "8",
72
+ "9",
73
+ "+",
74
+ "/",
75
+ ];
76
+ /**
77
+ * CREDIT: https://gist.github.com/enepomnyaschih/72c423f727d395eeaa09697058238727
78
+ * Encodes a given Uint8Array, ArrayBuffer or string into RFC4648 base64 representation
79
+ * @param data
80
+ */
81
+ export function encode(data) {
82
+ const uint8 = typeof data === "string"
83
+ ? new TextEncoder().encode(data)
84
+ : data instanceof Uint8Array
85
+ ? data
86
+ : new Uint8Array(data);
87
+ let result = "", i;
88
+ const l = uint8.length;
89
+ for (i = 2; i < l; i += 3) {
90
+ result += base64abc[uint8[i - 2] >> 2];
91
+ result += base64abc[((uint8[i - 2] & 0x03) << 4) | (uint8[i - 1] >> 4)];
92
+ result += base64abc[((uint8[i - 1] & 0x0f) << 2) | (uint8[i] >> 6)];
93
+ result += base64abc[uint8[i] & 0x3f];
94
+ }
95
+ if (i === l + 1) {
96
+ // 1 octet yet to write
97
+ result += base64abc[uint8[i - 2] >> 2];
98
+ result += base64abc[(uint8[i - 2] & 0x03) << 4];
99
+ result += "==";
100
+ }
101
+ if (i === l) {
102
+ // 2 octets yet to write
103
+ result += base64abc[uint8[i - 2] >> 2];
104
+ result += base64abc[((uint8[i - 2] & 0x03) << 4) | (uint8[i - 1] >> 4)];
105
+ result += base64abc[(uint8[i - 1] & 0x0f) << 2];
106
+ result += "=";
107
+ }
108
+ return result;
109
+ }
110
+ /**
111
+ * Decodes a given RFC4648 base64 encoded string
112
+ * @param b64
113
+ */
114
+ export function decode(b64) {
115
+ const binString = atob(b64);
116
+ const size = binString.length;
117
+ const bytes = new Uint8Array(size);
118
+ for (let i = 0; i < size; i++) {
119
+ bytes[i] = binString.charCodeAt(i);
120
+ }
121
+ return bytes;
122
+ }
@@ -0,0 +1,49 @@
1
+ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
2
+ /**
3
+ * {@linkcode encode} and {@linkcode decode} for
4
+ * [base64 URL safe](https://en.wikipedia.org/wiki/Base64#URL_applications) encoding.
5
+ *
6
+ * This module is browser compatible.
7
+ *
8
+ * @module
9
+ */
10
+ import * as base64 from "./base64.js";
11
+ /*
12
+ * Some variants allow or require omitting the padding '=' signs:
13
+ * https://en.wikipedia.org/wiki/Base64#The_URL_applications
14
+ * @param base64url
15
+ */
16
+ function addPaddingToBase64url(base64url) {
17
+ if (base64url.length % 4 === 2)
18
+ return base64url + "==";
19
+ if (base64url.length % 4 === 3)
20
+ return base64url + "=";
21
+ if (base64url.length % 4 === 1) {
22
+ throw new TypeError("Illegal base64url string!");
23
+ }
24
+ return base64url;
25
+ }
26
+ function convertBase64urlToBase64(b64url) {
27
+ if (!/^[-_A-Z0-9]*?={0,2}$/i.test(b64url)) {
28
+ // Contains characters not part of base64url spec.
29
+ throw new TypeError("Failed to decode base64url: invalid character");
30
+ }
31
+ return addPaddingToBase64url(b64url).replace(/\-/g, "+").replace(/_/g, "/");
32
+ }
33
+ function convertBase64ToBase64url(b64) {
34
+ return b64.replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
35
+ }
36
+ /**
37
+ * Encodes a given ArrayBuffer or string into a base64url representation
38
+ * @param data
39
+ */
40
+ export function encode(data) {
41
+ return convertBase64ToBase64url(base64.encode(data));
42
+ }
43
+ /**
44
+ * Converts given base64url encoded data back to original
45
+ * @param b64url
46
+ */
47
+ export function decode(b64url) {
48
+ return base64.decode(convertBase64urlToBase64(b64url));
49
+ }
@@ -0,0 +1,60 @@
1
+ // Copyright 2009 The Go Authors. All rights reserved.
2
+ // https://github.com/golang/go/blob/master/LICENSE
3
+ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
4
+ /** Port of the Go
5
+ * [encoding/hex](https://github.com/golang/go/blob/go1.12.5/src/encoding/hex/hex.go)
6
+ * library.
7
+ *
8
+ * This module is browser compatible.
9
+ *
10
+ * @module
11
+ */
12
+ const hexTable = new TextEncoder().encode("0123456789abcdef");
13
+ function errInvalidByte(byte) {
14
+ return new TypeError(`Invalid byte '${String.fromCharCode(byte)}'`);
15
+ }
16
+ function errLength() {
17
+ return new RangeError("Odd length hex string");
18
+ }
19
+ /** Converts a hex character into its value. */
20
+ function fromHexChar(byte) {
21
+ // '0' <= byte && byte <= '9'
22
+ if (48 <= byte && byte <= 57)
23
+ return byte - 48;
24
+ // 'a' <= byte && byte <= 'f'
25
+ if (97 <= byte && byte <= 102)
26
+ return byte - 97 + 10;
27
+ // 'A' <= byte && byte <= 'F'
28
+ if (65 <= byte && byte <= 70)
29
+ return byte - 65 + 10;
30
+ throw errInvalidByte(byte);
31
+ }
32
+ /** Encodes `src` into `src.length * 2` bytes. */
33
+ export function encode(src) {
34
+ const dst = new Uint8Array(src.length * 2);
35
+ for (let i = 0; i < dst.length; i++) {
36
+ const v = src[i];
37
+ dst[i * 2] = hexTable[v >> 4];
38
+ dst[i * 2 + 1] = hexTable[v & 0x0f];
39
+ }
40
+ return dst;
41
+ }
42
+ /**
43
+ * Decodes `src` into `src.length / 2` bytes.
44
+ * If the input is malformed, an error will be thrown.
45
+ */
46
+ export function decode(src) {
47
+ const dst = new Uint8Array(src.length / 2);
48
+ for (let i = 0; i < dst.length; i++) {
49
+ const a = fromHexChar(src[i * 2]);
50
+ const b = fromHexChar(src[i * 2 + 1]);
51
+ dst[i] = (a << 4) | b;
52
+ }
53
+ if (src.length % 2 == 1) {
54
+ // Check for invalid char before reporting bad length,
55
+ // since the invalid char (if present) is an earlier problem.
56
+ fromHexChar(src[dst.length * 2]);
57
+ throw errLength();
58
+ }
59
+ return dst;
60
+ }