@graphql-yoga/plugin-apq 3.18.0 → 3.18.1-alpha-20260116132831-dc9fc0ad2f1ad6ee99bc438c173cf8496ae505a7
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/dist/index.cjs +58 -0
- package/dist/index.d.cts +37 -0
- package/dist/index.d.mts +37 -0
- package/dist/index.mjs +56 -0
- package/package.json +46 -21
- package/LICENSE +0 -23
- package/cjs/index.js +0 -81
- package/cjs/package.json +0 -1
- package/esm/index.js +0 -76
- package/typings/index.d.cts +0 -33
- package/typings/index.d.ts +0 -33
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
let graphql_yoga = require("graphql-yoga");
|
|
2
|
+
let _whatwg_node_promise_helpers = require("@whatwg-node/promise-helpers");
|
|
3
|
+
|
|
4
|
+
//#region src/index.ts
|
|
5
|
+
function hashSHA256(text, api = globalThis) {
|
|
6
|
+
const inputUint8Array = new api.TextEncoder().encode(text);
|
|
7
|
+
return (0, _whatwg_node_promise_helpers.handleMaybePromise)(() => api.crypto.subtle.digest({ name: "SHA-256" }, inputUint8Array), (arrayBuf) => {
|
|
8
|
+
const outputUint8Array = new Uint8Array(arrayBuf);
|
|
9
|
+
let hash = "";
|
|
10
|
+
for (const byte of outputUint8Array) {
|
|
11
|
+
const hex = byte.toString(16);
|
|
12
|
+
hash += "00".slice(0, Math.max(0, 2 - hex.length)) + hex;
|
|
13
|
+
}
|
|
14
|
+
return hash;
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
function createInMemoryAPQStore(options = {}) {
|
|
18
|
+
return (0, graphql_yoga.createLRUCache)({
|
|
19
|
+
max: options.max ?? 1e3,
|
|
20
|
+
ttl: options.ttl ?? 36e3
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
function isAPQExtension(input) {
|
|
24
|
+
return input != null && typeof input === "object" && "version" in input && input?.version === 1 && "sha256Hash" in input && typeof input?.sha256Hash === "string";
|
|
25
|
+
}
|
|
26
|
+
function decodeAPQExtension(input) {
|
|
27
|
+
if (isAPQExtension(input)) return input;
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
function useAPQ(options = {}) {
|
|
31
|
+
const { store = createInMemoryAPQStore(), hash = hashSHA256, responseConfig = {} } = options;
|
|
32
|
+
return { onParams({ params, setParams, fetchAPI }) {
|
|
33
|
+
const persistedQueryData = decodeAPQExtension(params.extensions?.["persistedQuery"]);
|
|
34
|
+
if (persistedQueryData === null) return;
|
|
35
|
+
if (params.query == null) return (0, _whatwg_node_promise_helpers.handleMaybePromise)(() => store.get(persistedQueryData.sha256Hash), (persistedQuery) => {
|
|
36
|
+
if (persistedQuery == null) throw (0, graphql_yoga.createGraphQLError)("PersistedQueryNotFound", { extensions: {
|
|
37
|
+
http: { status: responseConfig.forceStatusCodeOk ? 200 : 404 },
|
|
38
|
+
code: "PERSISTED_QUERY_NOT_FOUND"
|
|
39
|
+
} });
|
|
40
|
+
setParams({
|
|
41
|
+
...params,
|
|
42
|
+
query: persistedQuery
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
return (0, _whatwg_node_promise_helpers.handleMaybePromise)(() => hash(params.query, fetchAPI), (expectedHash) => {
|
|
46
|
+
if (persistedQueryData.sha256Hash !== expectedHash) throw (0, graphql_yoga.createGraphQLError)("PersistedQueryMismatch", { extensions: {
|
|
47
|
+
http: { status: responseConfig.forceStatusCodeOk ? 200 : 400 },
|
|
48
|
+
code: "PERSISTED_QUERY_MISMATCH"
|
|
49
|
+
} });
|
|
50
|
+
return store.set(persistedQueryData.sha256Hash, params.query);
|
|
51
|
+
});
|
|
52
|
+
} };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
//#endregion
|
|
56
|
+
exports.createInMemoryAPQStore = createInMemoryAPQStore;
|
|
57
|
+
exports.hashSHA256 = hashSHA256;
|
|
58
|
+
exports.useAPQ = useAPQ;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Plugin, PromiseOrValue } from "graphql-yoga";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
declare function hashSHA256(text: string, api?: {
|
|
5
|
+
crypto: Crypto;
|
|
6
|
+
TextEncoder: (typeof globalThis)['TextEncoder'];
|
|
7
|
+
}): PromiseOrValue<string>;
|
|
8
|
+
interface APQStoreOptions {
|
|
9
|
+
max?: number;
|
|
10
|
+
ttl?: number;
|
|
11
|
+
}
|
|
12
|
+
declare function createInMemoryAPQStore(options?: APQStoreOptions): APQStore;
|
|
13
|
+
interface APQOptions {
|
|
14
|
+
store?: APQStore;
|
|
15
|
+
hash?: (str: string, api: {
|
|
16
|
+
crypto: Crypto;
|
|
17
|
+
TextEncoder: typeof TextEncoder;
|
|
18
|
+
}) => PromiseOrValue<string>;
|
|
19
|
+
responseConfig?: {
|
|
20
|
+
/**
|
|
21
|
+
* If set true, status code of the response (if the query
|
|
22
|
+
* is not found or mismatched) will be 200.
|
|
23
|
+
*/
|
|
24
|
+
forceStatusCodeOk?: boolean;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
interface APQStore {
|
|
28
|
+
get(key: string): PromiseOrValue<string | null | undefined>;
|
|
29
|
+
set(key: string, query: string): PromiseOrValue<any>;
|
|
30
|
+
}
|
|
31
|
+
interface APQExtension {
|
|
32
|
+
version: 1;
|
|
33
|
+
sha256Hash: string;
|
|
34
|
+
}
|
|
35
|
+
declare function useAPQ(options?: APQOptions): Plugin;
|
|
36
|
+
//#endregion
|
|
37
|
+
export { APQExtension, APQOptions, APQStore, APQStoreOptions, createInMemoryAPQStore, hashSHA256, useAPQ };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Plugin, PromiseOrValue } from "graphql-yoga";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
declare function hashSHA256(text: string, api?: {
|
|
5
|
+
crypto: Crypto;
|
|
6
|
+
TextEncoder: (typeof globalThis)['TextEncoder'];
|
|
7
|
+
}): PromiseOrValue<string>;
|
|
8
|
+
interface APQStoreOptions {
|
|
9
|
+
max?: number;
|
|
10
|
+
ttl?: number;
|
|
11
|
+
}
|
|
12
|
+
declare function createInMemoryAPQStore(options?: APQStoreOptions): APQStore;
|
|
13
|
+
interface APQOptions {
|
|
14
|
+
store?: APQStore;
|
|
15
|
+
hash?: (str: string, api: {
|
|
16
|
+
crypto: Crypto;
|
|
17
|
+
TextEncoder: typeof TextEncoder;
|
|
18
|
+
}) => PromiseOrValue<string>;
|
|
19
|
+
responseConfig?: {
|
|
20
|
+
/**
|
|
21
|
+
* If set true, status code of the response (if the query
|
|
22
|
+
* is not found or mismatched) will be 200.
|
|
23
|
+
*/
|
|
24
|
+
forceStatusCodeOk?: boolean;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
interface APQStore {
|
|
28
|
+
get(key: string): PromiseOrValue<string | null | undefined>;
|
|
29
|
+
set(key: string, query: string): PromiseOrValue<any>;
|
|
30
|
+
}
|
|
31
|
+
interface APQExtension {
|
|
32
|
+
version: 1;
|
|
33
|
+
sha256Hash: string;
|
|
34
|
+
}
|
|
35
|
+
declare function useAPQ(options?: APQOptions): Plugin;
|
|
36
|
+
//#endregion
|
|
37
|
+
export { APQExtension, APQOptions, APQStore, APQStoreOptions, createInMemoryAPQStore, hashSHA256, useAPQ };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { createGraphQLError, createLRUCache } from "graphql-yoga";
|
|
2
|
+
import { handleMaybePromise } from "@whatwg-node/promise-helpers";
|
|
3
|
+
|
|
4
|
+
//#region src/index.ts
|
|
5
|
+
function hashSHA256(text, api = globalThis) {
|
|
6
|
+
const inputUint8Array = new api.TextEncoder().encode(text);
|
|
7
|
+
return handleMaybePromise(() => api.crypto.subtle.digest({ name: "SHA-256" }, inputUint8Array), (arrayBuf) => {
|
|
8
|
+
const outputUint8Array = new Uint8Array(arrayBuf);
|
|
9
|
+
let hash = "";
|
|
10
|
+
for (const byte of outputUint8Array) {
|
|
11
|
+
const hex = byte.toString(16);
|
|
12
|
+
hash += "00".slice(0, Math.max(0, 2 - hex.length)) + hex;
|
|
13
|
+
}
|
|
14
|
+
return hash;
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
function createInMemoryAPQStore(options = {}) {
|
|
18
|
+
return createLRUCache({
|
|
19
|
+
max: options.max ?? 1e3,
|
|
20
|
+
ttl: options.ttl ?? 36e3
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
function isAPQExtension(input) {
|
|
24
|
+
return input != null && typeof input === "object" && "version" in input && input?.version === 1 && "sha256Hash" in input && typeof input?.sha256Hash === "string";
|
|
25
|
+
}
|
|
26
|
+
function decodeAPQExtension(input) {
|
|
27
|
+
if (isAPQExtension(input)) return input;
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
function useAPQ(options = {}) {
|
|
31
|
+
const { store = createInMemoryAPQStore(), hash = hashSHA256, responseConfig = {} } = options;
|
|
32
|
+
return { onParams({ params, setParams, fetchAPI }) {
|
|
33
|
+
const persistedQueryData = decodeAPQExtension(params.extensions?.["persistedQuery"]);
|
|
34
|
+
if (persistedQueryData === null) return;
|
|
35
|
+
if (params.query == null) return handleMaybePromise(() => store.get(persistedQueryData.sha256Hash), (persistedQuery) => {
|
|
36
|
+
if (persistedQuery == null) throw createGraphQLError("PersistedQueryNotFound", { extensions: {
|
|
37
|
+
http: { status: responseConfig.forceStatusCodeOk ? 200 : 404 },
|
|
38
|
+
code: "PERSISTED_QUERY_NOT_FOUND"
|
|
39
|
+
} });
|
|
40
|
+
setParams({
|
|
41
|
+
...params,
|
|
42
|
+
query: persistedQuery
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
return handleMaybePromise(() => hash(params.query, fetchAPI), (expectedHash) => {
|
|
46
|
+
if (persistedQueryData.sha256Hash !== expectedHash) throw createGraphQLError("PersistedQueryMismatch", { extensions: {
|
|
47
|
+
http: { status: responseConfig.forceStatusCodeOk ? 200 : 400 },
|
|
48
|
+
code: "PERSISTED_QUERY_MISMATCH"
|
|
49
|
+
} });
|
|
50
|
+
return store.set(persistedQueryData.sha256Hash, params.query);
|
|
51
|
+
});
|
|
52
|
+
} };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
//#endregion
|
|
56
|
+
export { createInMemoryAPQStore, hashSHA256, useAPQ };
|
package/package.json
CHANGED
|
@@ -1,13 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-yoga/plugin-apq",
|
|
3
|
-
"version": "3.18.
|
|
3
|
+
"version": "3.18.1-alpha-20260116132831-dc9fc0ad2f1ad6ee99bc438c173cf8496ae505a7",
|
|
4
|
+
"type": "module",
|
|
4
5
|
"description": "APQ plugin for GraphQL Yoga.",
|
|
5
|
-
"peerDependencies": {
|
|
6
|
-
"graphql-yoga": "^5.18.0"
|
|
7
|
-
},
|
|
8
|
-
"dependencies": {
|
|
9
|
-
"@whatwg-node/promise-helpers": "^1.3.2"
|
|
10
|
-
},
|
|
11
6
|
"repository": {
|
|
12
7
|
"type": "git",
|
|
13
8
|
"url": "https://github.com/graphql-hive/graphql-yoga.git",
|
|
@@ -18,28 +13,58 @@
|
|
|
18
13
|
"engines": {
|
|
19
14
|
"node": ">=18.0.0"
|
|
20
15
|
},
|
|
21
|
-
"main": "
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"typescript": {
|
|
25
|
-
"definition": "typings/index.d.ts"
|
|
26
|
-
},
|
|
27
|
-
"type": "module",
|
|
16
|
+
"main": "dist/index.cjs",
|
|
17
|
+
"typings": "dist/index.d.mts",
|
|
18
|
+
"module": "dist/index.mjs",
|
|
28
19
|
"exports": {
|
|
29
20
|
".": {
|
|
30
21
|
"require": {
|
|
31
|
-
"types": "./
|
|
32
|
-
"default": "./
|
|
22
|
+
"types": "./dist/index.d.cts",
|
|
23
|
+
"default": "./dist/index.cjs"
|
|
33
24
|
},
|
|
34
25
|
"import": {
|
|
35
|
-
"types": "./
|
|
36
|
-
"default": "./
|
|
26
|
+
"types": "./dist/index.d.mts",
|
|
27
|
+
"default": "./dist/index.mjs"
|
|
37
28
|
},
|
|
38
29
|
"default": {
|
|
39
|
-
"types": "./
|
|
40
|
-
"default": "./
|
|
30
|
+
"types": "./dist/index.d.mts",
|
|
31
|
+
"default": "./dist/index.mjs"
|
|
41
32
|
}
|
|
42
33
|
},
|
|
43
34
|
"./package.json": "./package.json"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist"
|
|
38
|
+
],
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsdown",
|
|
41
|
+
"check": "tsc --pretty --noEmit",
|
|
42
|
+
"watch": "tsdown --watch"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"graphql": "^15.2.0 || ^16.0.0",
|
|
46
|
+
"graphql-yoga": "workspace:^",
|
|
47
|
+
"rxjs": "^7.3.0"
|
|
48
|
+
},
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"@whatwg-node/promise-helpers": "^1.3.2"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@apollo/client": "4.0.9",
|
|
54
|
+
"crypto-hash": "4.0.0",
|
|
55
|
+
"graphql": "^16.12.0",
|
|
56
|
+
"graphql-yoga": "workspace:*",
|
|
57
|
+
"rxjs": "^7.8.2",
|
|
58
|
+
"tsdown": "^0.20.0-beta.1"
|
|
59
|
+
},
|
|
60
|
+
"publishConfig": {
|
|
61
|
+
"access": "public"
|
|
62
|
+
},
|
|
63
|
+
"sideEffects": false,
|
|
64
|
+
"buildOptions": {
|
|
65
|
+
"input": "./src/index.ts"
|
|
66
|
+
},
|
|
67
|
+
"typescript": {
|
|
68
|
+
"definition": "dist/index.d.mts"
|
|
44
69
|
}
|
|
45
|
-
}
|
|
70
|
+
}
|
package/LICENSE
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2018-2020 Graphcool
|
|
4
|
-
Copyright (c) 2020-2021 Prisma
|
|
5
|
-
Copyright (c) 2021- The Guild
|
|
6
|
-
|
|
7
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
-
in the Software without restriction, including without limitation the rights
|
|
10
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
-
furnished to do so, subject to the following conditions:
|
|
13
|
-
|
|
14
|
-
The above copyright notice and this permission notice shall be included in all
|
|
15
|
-
copies or substantial portions of the Software.
|
|
16
|
-
|
|
17
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
-
SOFTWARE.
|
package/cjs/index.js
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.hashSHA256 = hashSHA256;
|
|
4
|
-
exports.createInMemoryAPQStore = createInMemoryAPQStore;
|
|
5
|
-
exports.useAPQ = useAPQ;
|
|
6
|
-
const graphql_yoga_1 = require("graphql-yoga");
|
|
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
|
-
});
|
|
19
|
-
}
|
|
20
|
-
function createInMemoryAPQStore(options = {}) {
|
|
21
|
-
return (0, graphql_yoga_1.createLRUCache)({
|
|
22
|
-
max: options.max ?? 1000,
|
|
23
|
-
ttl: options.ttl ?? 36_000,
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
function isAPQExtension(input) {
|
|
27
|
-
return (input != null &&
|
|
28
|
-
typeof input === 'object' &&
|
|
29
|
-
'version' in input &&
|
|
30
|
-
input?.version === 1 &&
|
|
31
|
-
'sha256Hash' in input &&
|
|
32
|
-
typeof input?.sha256Hash === 'string');
|
|
33
|
-
}
|
|
34
|
-
function decodeAPQExtension(input) {
|
|
35
|
-
if (isAPQExtension(input)) {
|
|
36
|
-
return input;
|
|
37
|
-
}
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
|
-
function useAPQ(options = {}) {
|
|
41
|
-
const { store = createInMemoryAPQStore(), hash = hashSHA256, responseConfig = {} } = options;
|
|
42
|
-
return {
|
|
43
|
-
onParams({ params, setParams, fetchAPI }) {
|
|
44
|
-
const persistedQueryData = decodeAPQExtension(params.extensions?.['persistedQuery']);
|
|
45
|
-
if (persistedQueryData === null) {
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
if (params.query == null) {
|
|
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',
|
|
57
|
-
},
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
setParams({
|
|
61
|
-
...params,
|
|
62
|
-
query: persistedQuery,
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
return (0, promise_helpers_1.handleMaybePromise)(() => hash(params.query, fetchAPI), expectedHash => {
|
|
67
|
-
if (persistedQueryData.sha256Hash !== expectedHash) {
|
|
68
|
-
throw (0, graphql_yoga_1.createGraphQLError)('PersistedQueryMismatch', {
|
|
69
|
-
extensions: {
|
|
70
|
-
http: {
|
|
71
|
-
status: responseConfig.forceStatusCodeOk ? 200 : 400,
|
|
72
|
-
},
|
|
73
|
-
code: 'PERSISTED_QUERY_MISMATCH',
|
|
74
|
-
},
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
return store.set(persistedQueryData.sha256Hash, params.query);
|
|
78
|
-
});
|
|
79
|
-
},
|
|
80
|
-
};
|
|
81
|
-
}
|
package/cjs/package.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"type":"commonjs"}
|
package/esm/index.js
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import { createGraphQLError, createLRUCache } from 'graphql-yoga';
|
|
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
|
-
});
|
|
14
|
-
}
|
|
15
|
-
export function createInMemoryAPQStore(options = {}) {
|
|
16
|
-
return createLRUCache({
|
|
17
|
-
max: options.max ?? 1000,
|
|
18
|
-
ttl: options.ttl ?? 36_000,
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
function isAPQExtension(input) {
|
|
22
|
-
return (input != null &&
|
|
23
|
-
typeof input === 'object' &&
|
|
24
|
-
'version' in input &&
|
|
25
|
-
input?.version === 1 &&
|
|
26
|
-
'sha256Hash' in input &&
|
|
27
|
-
typeof input?.sha256Hash === 'string');
|
|
28
|
-
}
|
|
29
|
-
function decodeAPQExtension(input) {
|
|
30
|
-
if (isAPQExtension(input)) {
|
|
31
|
-
return input;
|
|
32
|
-
}
|
|
33
|
-
return null;
|
|
34
|
-
}
|
|
35
|
-
export function useAPQ(options = {}) {
|
|
36
|
-
const { store = createInMemoryAPQStore(), hash = hashSHA256, responseConfig = {} } = options;
|
|
37
|
-
return {
|
|
38
|
-
onParams({ params, setParams, fetchAPI }) {
|
|
39
|
-
const persistedQueryData = decodeAPQExtension(params.extensions?.['persistedQuery']);
|
|
40
|
-
if (persistedQueryData === null) {
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
if (params.query == null) {
|
|
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',
|
|
52
|
-
},
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
setParams({
|
|
56
|
-
...params,
|
|
57
|
-
query: persistedQuery,
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
return handleMaybePromise(() => hash(params.query, fetchAPI), expectedHash => {
|
|
62
|
-
if (persistedQueryData.sha256Hash !== expectedHash) {
|
|
63
|
-
throw createGraphQLError('PersistedQueryMismatch', {
|
|
64
|
-
extensions: {
|
|
65
|
-
http: {
|
|
66
|
-
status: responseConfig.forceStatusCodeOk ? 200 : 400,
|
|
67
|
-
},
|
|
68
|
-
code: 'PERSISTED_QUERY_MISMATCH',
|
|
69
|
-
},
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
return store.set(persistedQueryData.sha256Hash, params.query);
|
|
73
|
-
});
|
|
74
|
-
},
|
|
75
|
-
};
|
|
76
|
-
}
|
package/typings/index.d.cts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { Plugin, PromiseOrValue } from 'graphql-yoga';
|
|
2
|
-
export declare function hashSHA256(text: string, api?: {
|
|
3
|
-
crypto: Crypto;
|
|
4
|
-
TextEncoder: (typeof globalThis)['TextEncoder'];
|
|
5
|
-
}): PromiseOrValue<string>;
|
|
6
|
-
export interface APQStoreOptions {
|
|
7
|
-
max?: number;
|
|
8
|
-
ttl?: number;
|
|
9
|
-
}
|
|
10
|
-
export declare function createInMemoryAPQStore(options?: APQStoreOptions): APQStore;
|
|
11
|
-
export interface APQOptions {
|
|
12
|
-
store?: APQStore;
|
|
13
|
-
hash?: (str: string, api: {
|
|
14
|
-
crypto: Crypto;
|
|
15
|
-
TextEncoder: typeof TextEncoder;
|
|
16
|
-
}) => PromiseOrValue<string>;
|
|
17
|
-
responseConfig?: {
|
|
18
|
-
/**
|
|
19
|
-
* If set true, status code of the response (if the query
|
|
20
|
-
* is not found or mismatched) will be 200.
|
|
21
|
-
*/
|
|
22
|
-
forceStatusCodeOk?: boolean;
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
export interface APQStore {
|
|
26
|
-
get(key: string): PromiseOrValue<string | null | undefined>;
|
|
27
|
-
set(key: string, query: string): PromiseOrValue<any>;
|
|
28
|
-
}
|
|
29
|
-
export interface APQExtension {
|
|
30
|
-
version: 1;
|
|
31
|
-
sha256Hash: string;
|
|
32
|
-
}
|
|
33
|
-
export declare function useAPQ(options?: APQOptions): Plugin;
|
package/typings/index.d.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { Plugin, PromiseOrValue } from 'graphql-yoga';
|
|
2
|
-
export declare function hashSHA256(text: string, api?: {
|
|
3
|
-
crypto: Crypto;
|
|
4
|
-
TextEncoder: (typeof globalThis)['TextEncoder'];
|
|
5
|
-
}): PromiseOrValue<string>;
|
|
6
|
-
export interface APQStoreOptions {
|
|
7
|
-
max?: number;
|
|
8
|
-
ttl?: number;
|
|
9
|
-
}
|
|
10
|
-
export declare function createInMemoryAPQStore(options?: APQStoreOptions): APQStore;
|
|
11
|
-
export interface APQOptions {
|
|
12
|
-
store?: APQStore;
|
|
13
|
-
hash?: (str: string, api: {
|
|
14
|
-
crypto: Crypto;
|
|
15
|
-
TextEncoder: typeof TextEncoder;
|
|
16
|
-
}) => PromiseOrValue<string>;
|
|
17
|
-
responseConfig?: {
|
|
18
|
-
/**
|
|
19
|
-
* If set true, status code of the response (if the query
|
|
20
|
-
* is not found or mismatched) will be 200.
|
|
21
|
-
*/
|
|
22
|
-
forceStatusCodeOk?: boolean;
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
export interface APQStore {
|
|
26
|
-
get(key: string): PromiseOrValue<string | null | undefined>;
|
|
27
|
-
set(key: string, query: string): PromiseOrValue<any>;
|
|
28
|
-
}
|
|
29
|
-
export interface APQExtension {
|
|
30
|
-
version: 1;
|
|
31
|
-
sha256Hash: string;
|
|
32
|
-
}
|
|
33
|
-
export declare function useAPQ(options?: APQOptions): Plugin;
|