@envelop/response-cache 4.0.4 → 4.0.5-alpha-20230201174535-b50c7f1d

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.
@@ -1,66 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.hashSHA256 = void 0;
4
- const webCrypto = globalThis.crypto;
5
- const sha256Browser = (bytes) => {
6
- const hash = webCrypto.subtle.digest({ name: 'SHA-256' }, bytes);
7
- return new Promise((resolve, reject) => {
8
- Promise.resolve(hash)
9
- .then(result => {
10
- resolve(new Uint8Array(result));
11
- })
12
- .catch(error => {
13
- reject(error);
14
- });
15
- });
16
- };
17
- let nodeCrypto;
18
- if (typeof window === 'undefined' && !webCrypto) {
19
- // Indirect eval'd require/import to guarantee no side-effects in module scope
20
- // (optimization for minifier)
21
- try {
22
- // eslint-disable-next-line no-new-func
23
- nodeCrypto = Promise.resolve(new Function('require', 'return require("crypto")')(require));
24
- }
25
- catch (_error) {
26
- try {
27
- // eslint-disable-next-line no-new-func
28
- nodeCrypto = new Function('return import("crypto")')();
29
- }
30
- catch (_error) { }
31
- }
32
- }
4
+ const fetch_1 = require("@whatwg-node/fetch");
33
5
  const hashSHA256 = async (text) => {
34
- // Node.js support
35
- if (nodeCrypto) {
36
- return nodeCrypto.then(crypto => crypto.createHash('sha256').update(text).digest('hex'));
37
- }
38
- if (webCrypto) {
39
- let buf;
40
- if (typeof TextEncoder !== 'undefined') {
41
- buf = new TextEncoder().encode(text);
42
- }
43
- else {
44
- buf = new Uint8Array(text.length);
45
- for (let i = 0, l = text.length; i < l; i++) {
46
- // NOTE: We assume that the input GraphQL Query only uses UTF-8 at most
47
- // since GraphQL mostly consists of ASCII, this is completely fine
48
- buf[i] = text.charCodeAt(i);
49
- }
50
- }
51
- const out = await sha256Browser(buf);
52
- let hash = '';
53
- for (let i = 0, l = out.length; i < l; i++) {
54
- const hex = out[i].toString(16);
55
- hash += '00'.slice(0, Math.max(0, 2 - hex.length)) + hex;
56
- }
57
- return hash;
58
- }
59
- if (process.env.NODE_ENV !== 'production') {
60
- // eslint-disable-next-line no-console
61
- console.warn('[@envelop/response-cache]: The Node Crypto and Web Crypto APIs are not available.\n' +
62
- 'This is an unexpected error. Please report it by filing a GitHub Issue.');
6
+ const inputUint8Array = new fetch_1.TextEncoder().encode(text);
7
+ const arrayBuf = await fetch_1.crypto.subtle.digest({ name: 'SHA-256' }, inputUint8Array);
8
+ const outputUint8Array = new Uint8Array(arrayBuf);
9
+ let hash = '';
10
+ for (let i = 0, l = outputUint8Array.length; i < l; i++) {
11
+ const hex = outputUint8Array[i].toString(16);
12
+ hash += '00'.slice(0, Math.max(0, 2 - hex.length)) + hex;
63
13
  }
64
- return Promise.resolve('');
14
+ return hash;
65
15
  };
66
16
  exports.hashSHA256 = hashSHA256;
@@ -1,62 +1,12 @@
1
- const webCrypto = globalThis.crypto;
2
- const sha256Browser = (bytes) => {
3
- const hash = webCrypto.subtle.digest({ name: 'SHA-256' }, bytes);
4
- return new Promise((resolve, reject) => {
5
- Promise.resolve(hash)
6
- .then(result => {
7
- resolve(new Uint8Array(result));
8
- })
9
- .catch(error => {
10
- reject(error);
11
- });
12
- });
13
- };
14
- let nodeCrypto;
15
- if (typeof window === 'undefined' && !webCrypto) {
16
- // Indirect eval'd require/import to guarantee no side-effects in module scope
17
- // (optimization for minifier)
18
- try {
19
- // eslint-disable-next-line no-new-func
20
- nodeCrypto = Promise.resolve(new Function('require', 'return require("crypto")')(require));
21
- }
22
- catch (_error) {
23
- try {
24
- // eslint-disable-next-line no-new-func
25
- nodeCrypto = new Function('return import("crypto")')();
26
- }
27
- catch (_error) { }
28
- }
29
- }
1
+ import { crypto, TextEncoder } from '@whatwg-node/fetch';
30
2
  export const hashSHA256 = async (text) => {
31
- // Node.js support
32
- if (nodeCrypto) {
33
- return nodeCrypto.then(crypto => crypto.createHash('sha256').update(text).digest('hex'));
34
- }
35
- if (webCrypto) {
36
- let buf;
37
- if (typeof TextEncoder !== 'undefined') {
38
- buf = new TextEncoder().encode(text);
39
- }
40
- else {
41
- buf = new Uint8Array(text.length);
42
- for (let i = 0, l = text.length; i < l; i++) {
43
- // NOTE: We assume that the input GraphQL Query only uses UTF-8 at most
44
- // since GraphQL mostly consists of ASCII, this is completely fine
45
- buf[i] = text.charCodeAt(i);
46
- }
47
- }
48
- const out = await sha256Browser(buf);
49
- let hash = '';
50
- for (let i = 0, l = out.length; i < l; i++) {
51
- const hex = out[i].toString(16);
52
- hash += '00'.slice(0, Math.max(0, 2 - hex.length)) + hex;
53
- }
54
- return hash;
55
- }
56
- if (process.env.NODE_ENV !== 'production') {
57
- // eslint-disable-next-line no-console
58
- console.warn('[@envelop/response-cache]: The Node Crypto and Web Crypto APIs are not available.\n' +
59
- 'This is an unexpected error. Please report it by filing a GitHub Issue.');
60
- }
61
- return Promise.resolve('');
3
+ const inputUint8Array = new TextEncoder().encode(text);
4
+ const arrayBuf = await crypto.subtle.digest({ name: 'SHA-256' }, inputUint8Array);
5
+ const outputUint8Array = new Uint8Array(arrayBuf);
6
+ let hash = '';
7
+ for (let i = 0, l = outputUint8Array.length; i < l; i++) {
8
+ const hex = outputUint8Array[i].toString(16);
9
+ hash += '00'.slice(0, Math.max(0, 2 - hex.length)) + hex;
10
+ }
11
+ return hash;
62
12
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@envelop/response-cache",
3
- "version": "4.0.4",
3
+ "version": "4.0.5-alpha-20230201174535-b50c7f1d",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
6
  "@envelop/core": "^3.0.4",
@@ -8,6 +8,7 @@
8
8
  },
9
9
  "dependencies": {
10
10
  "@graphql-tools/utils": "^8.8.0",
11
+ "@whatwg-node/fetch": "^0.6.5",
11
12
  "lru-cache": "^6.0.0",
12
13
  "fast-json-stable-stringify": "^2.1.0",
13
14
  "tslib": "^2.4.0"