@graphql-yoga/plugin-apq 3.13.0 → 3.13.1

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/cjs/index.js CHANGED
@@ -4,16 +4,18 @@ exports.hashSHA256 = hashSHA256;
4
4
  exports.createInMemoryAPQStore = createInMemoryAPQStore;
5
5
  exports.useAPQ = useAPQ;
6
6
  const graphql_yoga_1 = require("graphql-yoga");
7
- async function hashSHA256(str, api = globalThis) {
8
- const { crypto, TextEncoder } = api;
9
- const textEncoder = new TextEncoder();
10
- const utf8 = textEncoder.encode(str);
11
- const hashBuffer = await crypto.subtle.digest('SHA-256', utf8);
12
- let hashHex = '';
13
- for (const bytes of new Uint8Array(hashBuffer)) {
14
- hashHex += bytes.toString(16).padStart(2, '0');
15
- }
16
- return hashHex;
7
+ const promise_helpers_1 = require("@whatwg-node/promise-helpers");
8
+ function hashSHA256(text, api = globalThis) {
9
+ const inputUint8Array = new api.TextEncoder().encode(text);
10
+ return (0, promise_helpers_1.handleMaybePromise)(() => api.crypto.subtle.digest({ name: 'SHA-256' }, inputUint8Array), arrayBuf => {
11
+ const outputUint8Array = new Uint8Array(arrayBuf);
12
+ let hash = '';
13
+ for (const byte of outputUint8Array) {
14
+ const hex = byte.toString(16);
15
+ hash += '00'.slice(0, Math.max(0, 2 - hex.length)) + hex;
16
+ }
17
+ return hash;
18
+ });
17
19
  }
18
20
  function createInMemoryAPQStore(options = {}) {
19
21
  return (0, graphql_yoga_1.createLRUCache)({
@@ -38,30 +40,30 @@ function decodeAPQExtension(input) {
38
40
  function useAPQ(options = {}) {
39
41
  const { store = createInMemoryAPQStore(), hash = hashSHA256, responseConfig = {} } = options;
40
42
  return {
41
- async onParams({ params, setParams, fetchAPI }) {
43
+ onParams({ params, setParams, fetchAPI }) {
42
44
  const persistedQueryData = decodeAPQExtension(params.extensions?.['persistedQuery']);
43
45
  if (persistedQueryData === null) {
44
46
  return;
45
47
  }
46
48
  if (params.query == null) {
47
- const persistedQuery = await store.get(persistedQueryData.sha256Hash);
48
- if (persistedQuery == null) {
49
- throw (0, graphql_yoga_1.createGraphQLError)('PersistedQueryNotFound', {
50
- extensions: {
51
- http: {
52
- status: responseConfig.forceStatusCodeOk ? 200 : 404,
49
+ return (0, promise_helpers_1.handleMaybePromise)(() => store.get(persistedQueryData.sha256Hash), persistedQuery => {
50
+ if (persistedQuery == null) {
51
+ throw (0, graphql_yoga_1.createGraphQLError)('PersistedQueryNotFound', {
52
+ extensions: {
53
+ http: {
54
+ status: responseConfig.forceStatusCodeOk ? 200 : 404,
55
+ },
56
+ code: 'PERSISTED_QUERY_NOT_FOUND',
53
57
  },
54
- code: 'PERSISTED_QUERY_NOT_FOUND',
55
- },
58
+ });
59
+ }
60
+ setParams({
61
+ ...params,
62
+ query: persistedQuery,
56
63
  });
57
- }
58
- setParams({
59
- ...params,
60
- query: persistedQuery,
61
64
  });
62
65
  }
63
- else {
64
- const expectedHash = await hash(params.query, fetchAPI);
66
+ return (0, promise_helpers_1.handleMaybePromise)(() => hash(params.query, fetchAPI), expectedHash => {
65
67
  if (persistedQueryData.sha256Hash !== expectedHash) {
66
68
  throw (0, graphql_yoga_1.createGraphQLError)('PersistedQueryMismatch', {
67
69
  extensions: {
@@ -72,8 +74,8 @@ function useAPQ(options = {}) {
72
74
  },
73
75
  });
74
76
  }
75
- await store.set(persistedQueryData.sha256Hash, params.query);
76
- }
77
+ return store.set(persistedQueryData.sha256Hash, params.query);
78
+ });
77
79
  },
78
80
  };
79
81
  }
package/esm/index.js CHANGED
@@ -1,14 +1,16 @@
1
1
  import { createGraphQLError, createLRUCache } from 'graphql-yoga';
2
- export async function hashSHA256(str, api = globalThis) {
3
- const { crypto, TextEncoder } = api;
4
- const textEncoder = new TextEncoder();
5
- const utf8 = textEncoder.encode(str);
6
- const hashBuffer = await crypto.subtle.digest('SHA-256', utf8);
7
- let hashHex = '';
8
- for (const bytes of new Uint8Array(hashBuffer)) {
9
- hashHex += bytes.toString(16).padStart(2, '0');
10
- }
11
- return hashHex;
2
+ import { handleMaybePromise } from '@whatwg-node/promise-helpers';
3
+ export function hashSHA256(text, api = globalThis) {
4
+ const inputUint8Array = new api.TextEncoder().encode(text);
5
+ return handleMaybePromise(() => api.crypto.subtle.digest({ name: 'SHA-256' }, inputUint8Array), arrayBuf => {
6
+ const outputUint8Array = new Uint8Array(arrayBuf);
7
+ let hash = '';
8
+ for (const byte of outputUint8Array) {
9
+ const hex = byte.toString(16);
10
+ hash += '00'.slice(0, Math.max(0, 2 - hex.length)) + hex;
11
+ }
12
+ return hash;
13
+ });
12
14
  }
13
15
  export function createInMemoryAPQStore(options = {}) {
14
16
  return createLRUCache({
@@ -33,30 +35,30 @@ function decodeAPQExtension(input) {
33
35
  export function useAPQ(options = {}) {
34
36
  const { store = createInMemoryAPQStore(), hash = hashSHA256, responseConfig = {} } = options;
35
37
  return {
36
- async onParams({ params, setParams, fetchAPI }) {
38
+ onParams({ params, setParams, fetchAPI }) {
37
39
  const persistedQueryData = decodeAPQExtension(params.extensions?.['persistedQuery']);
38
40
  if (persistedQueryData === null) {
39
41
  return;
40
42
  }
41
43
  if (params.query == null) {
42
- const persistedQuery = await store.get(persistedQueryData.sha256Hash);
43
- if (persistedQuery == null) {
44
- throw createGraphQLError('PersistedQueryNotFound', {
45
- extensions: {
46
- http: {
47
- status: responseConfig.forceStatusCodeOk ? 200 : 404,
44
+ return handleMaybePromise(() => store.get(persistedQueryData.sha256Hash), persistedQuery => {
45
+ if (persistedQuery == null) {
46
+ throw createGraphQLError('PersistedQueryNotFound', {
47
+ extensions: {
48
+ http: {
49
+ status: responseConfig.forceStatusCodeOk ? 200 : 404,
50
+ },
51
+ code: 'PERSISTED_QUERY_NOT_FOUND',
48
52
  },
49
- code: 'PERSISTED_QUERY_NOT_FOUND',
50
- },
53
+ });
54
+ }
55
+ setParams({
56
+ ...params,
57
+ query: persistedQuery,
51
58
  });
52
- }
53
- setParams({
54
- ...params,
55
- query: persistedQuery,
56
59
  });
57
60
  }
58
- else {
59
- const expectedHash = await hash(params.query, fetchAPI);
61
+ return handleMaybePromise(() => hash(params.query, fetchAPI), expectedHash => {
60
62
  if (persistedQueryData.sha256Hash !== expectedHash) {
61
63
  throw createGraphQLError('PersistedQueryMismatch', {
62
64
  extensions: {
@@ -67,8 +69,8 @@ export function useAPQ(options = {}) {
67
69
  },
68
70
  });
69
71
  }
70
- await store.set(persistedQueryData.sha256Hash, params.query);
71
- }
72
+ return store.set(persistedQueryData.sha256Hash, params.query);
73
+ });
72
74
  },
73
75
  };
74
76
  }
package/package.json CHANGED
@@ -1,10 +1,13 @@
1
1
  {
2
2
  "name": "@graphql-yoga/plugin-apq",
3
- "version": "3.13.0",
3
+ "version": "3.13.1",
4
4
  "description": "APQ plugin for GraphQL Yoga.",
5
5
  "peerDependencies": {
6
6
  "graphql-yoga": "^5.13.0"
7
7
  },
8
+ "dependencies": {
9
+ "@whatwg-node/promise-helpers": "^1.2.4"
10
+ },
8
11
  "repository": {
9
12
  "type": "git",
10
13
  "url": "https://github.com/dotansimha/graphql-yoga.git",
@@ -1,8 +1,8 @@
1
1
  import { Plugin, PromiseOrValue } from 'graphql-yoga';
2
- export declare function hashSHA256(str: string, api?: {
2
+ export declare function hashSHA256(text: string, api?: {
3
3
  crypto: Crypto;
4
4
  TextEncoder: (typeof globalThis)['TextEncoder'];
5
- }): Promise<string>;
5
+ }): PromiseOrValue<string>;
6
6
  export interface APQStoreOptions {
7
7
  max?: number;
8
8
  ttl?: number;
@@ -1,8 +1,8 @@
1
1
  import { Plugin, PromiseOrValue } from 'graphql-yoga';
2
- export declare function hashSHA256(str: string, api?: {
2
+ export declare function hashSHA256(text: string, api?: {
3
3
  crypto: Crypto;
4
4
  TextEncoder: (typeof globalThis)['TextEncoder'];
5
- }): Promise<string>;
5
+ }): PromiseOrValue<string>;
6
6
  export interface APQStoreOptions {
7
7
  max?: number;
8
8
  ttl?: number;