Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

(persisted-fetch) Replace js-sha256 polyfill for Node with Node's Crypto API (#807)

* Replace js-sha256 polyfill for Node with Native API

* Add Changeset

* Move around warning to cover Node Crypto API being missing

* Remove test

* Replace global with eval'ed require

As it turns out, the `crypto` global is only available in the REPL.

* Flip warning condition

authored by

Phil Plückthun and committed by
GitHub
b118c2ca fbebee72

+35 -40
+5
.changeset/tiny-elephants-push.md
··· 1 + --- 2 + '@urql/exchange-persisted-fetch': patch 3 + --- 4 + 5 + Replace `js-sha256` polyfill for Node.js support with Node's Native Crypto API
-1
exchanges/persisted-fetch/package.json
··· 50 50 }, 51 51 "dependencies": { 52 52 "@urql/core": ">=1.11.7", 53 - "js-sha256": "^0.9.0", 54 53 "wonka": "^4.0.10" 55 54 }, 56 55 "peerDependencies": {
-15
exchanges/persisted-fetch/src/sha256.test.ts
··· 1 - /** 2 - * @jest-environment node 3 - */ 4 - 5 - import { sha256 as sha25Standard } from 'js-sha256'; 6 - import { hash } from './sha256'; 7 - 8 - describe('hash', () => { 9 - it('is spec-compliant', async () => { 10 - expect(await hash('Hello')).toMatchInlineSnapshot( 11 - `"185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969"` 12 - ); 13 - expect(await hash('Hello')).toBe(sha25Standard('Hello')); 14 - }); 15 - });
+30 -19
exchanges/persisted-fetch/src/sha256.ts
··· 1 - import sha256Standard from 'js-sha256'; 2 - 3 1 const jsCrypto = 4 2 typeof window !== 'undefined' 5 3 ? window.crypto || (window as any).msCrypto ··· 8 6 jsCrypto && (jsCrypto.subtle || (jsCrypto as any).webkitSubtle); 9 7 const isIE = !!(jsCrypto && (window as any).msCrypto); 10 8 11 - const sha256 = (bytes: Uint8Array): Promise<Uint8Array> => { 12 - if (!cryptoSubtle) { 13 - if (process.env.NODE_ENV !== 'production') { 14 - console.warn( 15 - '[@urql/exchange-persisted-fetch]: The window.crypto.subtle API is not available.\n' + 16 - 'This is an unexpected error. Please report it by filing a GitHub Issue.' 17 - ); 18 - } 19 - 20 - return Promise.resolve(new Uint8Array(0)); 21 - } 22 - 23 - const hash = cryptoSubtle.digest({ name: 'SHA-256' }, bytes); 9 + const sha256Browser = (bytes: Uint8Array): Promise<Uint8Array> => { 10 + const hash = cryptoSubtle!.digest({ name: 'SHA-256' }, bytes); 24 11 return new Promise((resolve, reject) => { 25 12 if (isIE) { 26 13 // IE11 27 - (hash as any).oncomplete = function onComplete(event) { 14 + (hash as any).oncomplete = function onComplete(event: any) { 28 15 resolve(new Uint8Array(event.target.result)); 29 16 }; 30 - (hash as any).onerror = function onError(error) { 17 + (hash as any).onerror = function onError(error: Error) { 31 18 reject(error); 32 19 }; 33 20 } else { ··· 43 30 }); 44 31 }; 45 32 33 + const nodeCrypto = 34 + typeof window === 'undefined' ? eval("require('crypto')") : null; 35 + 46 36 export const hash = async (query: string): Promise<string> => { 37 + if ( 38 + typeof window === 'undefined' 39 + ? !nodeCrypto || !nodeCrypto.createHash 40 + : !cryptoSubtle 41 + ) { 42 + if (process.env.NODE_ENV !== 'production') { 43 + console.warn( 44 + '[@urql/exchange-persisted-fetch]: The ' + 45 + (typeof window === 'undefined' 46 + ? 'Node Crypto' 47 + : 'window.crypto.subtle') + 48 + ' API is not available.\n' + 49 + 'This is an unexpected error. Please report it by filing a GitHub Issue.' 50 + ); 51 + } 52 + 53 + return Promise.resolve(''); 54 + } 55 + 47 56 // Node.js support 48 57 if (typeof window === 'undefined') { 49 - return Promise.resolve(sha256Standard.sha256(query)); 58 + return Promise.resolve( 59 + '' + nodeCrypto.createHash('sha256').update(query).digest('hex') 60 + ); 50 61 } 51 62 52 63 let buf: Uint8Array; ··· 61 72 } 62 73 } 63 74 64 - const out = await sha256(buf); 75 + const out = await sha256Browser(buf); 65 76 66 77 let hash = ''; 67 78 for (let i = 0, l = out.length; i < l; i++) {
-5
yarn.lock
··· 8308 8308 resolved "https://registry.yarnpkg.com/js-levenshtein/-/js-levenshtein-1.1.6.tgz#c6cee58eb3550372df8deb85fad5ce66ce01d59d" 8309 8309 integrity sha512-X2BB11YZtrRqY4EnQcLX5Rh373zbK4alC1FW7D7MBhL2gtcC17cTnr6DmfHZeS0s2rTHjUTMMHfG7gO8SSdw+g== 8310 8310 8311 - js-sha256@^0.9.0: 8312 - version "0.9.0" 8313 - resolved "https://registry.yarnpkg.com/js-sha256/-/js-sha256-0.9.0.tgz#0b89ac166583e91ef9123644bd3c5334ce9d0966" 8314 - integrity sha512-sga3MHh9sgQN2+pJ9VYZ+1LPwXOxuBJBA5nrR5/ofPfuiJBE2hnjsaN8se8JznOmGLN2p49Pe5U/ttafcs/apA== 8315 - 8316 8311 "js-tokens@^3.0.0 || ^4.0.0", js-tokens@^4.0.0: 8317 8312 version "4.0.0" 8318 8313 resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"