@ocap/gql 1.30.25 → 1.31.0
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/esm/http.d.mts +2 -0
- package/esm/http.mjs +134 -7
- package/lib/http.cjs +134 -7
- package/lib/http.d.cts +2 -0
- package/package.json +3 -3
package/esm/http.d.mts
CHANGED
|
@@ -32,6 +32,8 @@ interface HttpHandlerOptions {
|
|
|
32
32
|
introspection?: boolean;
|
|
33
33
|
showStack?: boolean;
|
|
34
34
|
maxQueryDepth?: number;
|
|
35
|
+
maxTotalSelections?: number;
|
|
36
|
+
maxAliases?: number;
|
|
35
37
|
}
|
|
36
38
|
/**
|
|
37
39
|
* Transport-agnostic view of an incoming GraphQL request, so the same engine serves express (`createHttpHandler`)
|
package/esm/http.mjs
CHANGED
|
@@ -217,11 +217,27 @@ const resolvers = [
|
|
|
217
217
|
}
|
|
218
218
|
];
|
|
219
219
|
const createResolvers = (resolver) => resolvers.reduce((acc, { fn, dataKey }) => {
|
|
220
|
-
acc[fn] = (args, ctx) =>
|
|
221
|
-
const
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
220
|
+
acc[fn] = async (args, ctx) => {
|
|
221
|
+
const res = await wrapResolver(fn, dataKey, () => {
|
|
222
|
+
const method = resolver[fn];
|
|
223
|
+
if (typeof method === "function") return method.call(resolver, args, formatContext(ctx));
|
|
224
|
+
return null;
|
|
225
|
+
});
|
|
226
|
+
if (fn === "sendTx") {
|
|
227
|
+
if (res.hash && typeof res.hash === "object" && "hash" in res.hash) {
|
|
228
|
+
const inner = res.hash;
|
|
229
|
+
res.hash = inner.hash;
|
|
230
|
+
if (inner.warning !== void 0) res.warning = inner.warning;
|
|
231
|
+
} else if (typeof res.hash === "string") {
|
|
232
|
+
const getWarning = resolver.getTxSecurityWarning;
|
|
233
|
+
if (typeof getWarning === "function") {
|
|
234
|
+
const warning = getWarning.call(resolver, res.hash);
|
|
235
|
+
if (warning !== void 0) res.warning = warning;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return res;
|
|
240
|
+
};
|
|
225
241
|
return acc;
|
|
226
242
|
}, {});
|
|
227
243
|
const defaultErrorHandler = (err) => {
|
|
@@ -229,6 +245,8 @@ const defaultErrorHandler = (err) => {
|
|
|
229
245
|
};
|
|
230
246
|
const MAX_BATCH_SIZE = 40;
|
|
231
247
|
const MAX_QUERY_DEPTH = 15;
|
|
248
|
+
const MAX_TOTAL_SELECTIONS = 1e3;
|
|
249
|
+
const MAX_ALIASES = 100;
|
|
232
250
|
const BODY_SIZE_LIMIT = 100 * 1024;
|
|
233
251
|
/**
|
|
234
252
|
* Read the raw express request body as a utf-8 string, enforcing the size limit.
|
|
@@ -471,25 +489,127 @@ function getMaxSelectionDepth(documentAST, limit = MAX_QUERY_DEPTH + 1) {
|
|
|
471
489
|
}
|
|
472
490
|
return max;
|
|
473
491
|
}
|
|
492
|
+
/**
|
|
493
|
+
* Count total expanded field selections and field aliases across all operations
|
|
494
|
+
* in the document, recursively expanding inline and named fragments with memoization.
|
|
495
|
+
* Bounds fragment + alias width amplification (e.g. K^D nested fragment spreads) in O(|AST|) time.
|
|
496
|
+
*/
|
|
497
|
+
function countTotalSelectionsAndAliases(documentAST, selectionLimit = MAX_TOTAL_SELECTIONS + 1, aliasLimit = MAX_ALIASES + 1) {
|
|
498
|
+
const fragments = /* @__PURE__ */ new Map();
|
|
499
|
+
const operations = [];
|
|
500
|
+
for (const def of documentAST.definitions) if (def.kind === "FragmentDefinition" && def.name?.value) fragments.set(def.name.value, def.selectionSet?.selections || []);
|
|
501
|
+
else if (def.kind === "OperationDefinition") operations.push(def);
|
|
502
|
+
const fragMemo = /* @__PURE__ */ new Map();
|
|
503
|
+
const measure = (selections, visited = /* @__PURE__ */ new Set()) => {
|
|
504
|
+
let fields = 0;
|
|
505
|
+
let aliases = 0;
|
|
506
|
+
for (const sel of selections) {
|
|
507
|
+
if (fields >= selectionLimit && aliases >= aliasLimit) return {
|
|
508
|
+
fields: selectionLimit,
|
|
509
|
+
aliases: aliasLimit,
|
|
510
|
+
hitCycle: false
|
|
511
|
+
};
|
|
512
|
+
if (sel.kind === "Field") {
|
|
513
|
+
fields = Math.min(selectionLimit, fields + 1);
|
|
514
|
+
if (sel.alias?.value) aliases = Math.min(aliasLimit, aliases + 1);
|
|
515
|
+
const childSelections = sel.selectionSet?.selections || [];
|
|
516
|
+
if (childSelections.length > 0) {
|
|
517
|
+
const sub = measure(childSelections, visited);
|
|
518
|
+
if (sub.hitCycle) return {
|
|
519
|
+
fields: selectionLimit,
|
|
520
|
+
aliases: aliasLimit,
|
|
521
|
+
hitCycle: true
|
|
522
|
+
};
|
|
523
|
+
fields = Math.min(selectionLimit, fields + sub.fields);
|
|
524
|
+
aliases = Math.min(aliasLimit, aliases + sub.aliases);
|
|
525
|
+
}
|
|
526
|
+
} else if (sel.kind === "InlineFragment") {
|
|
527
|
+
const sub = measure(sel.selectionSet?.selections || [], visited);
|
|
528
|
+
if (sub.hitCycle) return {
|
|
529
|
+
fields: selectionLimit,
|
|
530
|
+
aliases: aliasLimit,
|
|
531
|
+
hitCycle: true
|
|
532
|
+
};
|
|
533
|
+
fields = Math.min(selectionLimit, fields + sub.fields);
|
|
534
|
+
aliases = Math.min(aliasLimit, aliases + sub.aliases);
|
|
535
|
+
} else if (sel.kind === "FragmentSpread" && sel.name?.value) {
|
|
536
|
+
const fragName = sel.name.value;
|
|
537
|
+
if (visited.has(fragName)) return {
|
|
538
|
+
fields: selectionLimit,
|
|
539
|
+
aliases: aliasLimit,
|
|
540
|
+
hitCycle: true
|
|
541
|
+
};
|
|
542
|
+
const cached = fragMemo.get(fragName);
|
|
543
|
+
if (cached !== void 0) {
|
|
544
|
+
fields = Math.min(selectionLimit, fields + cached.fields);
|
|
545
|
+
aliases = Math.min(aliasLimit, aliases + cached.aliases);
|
|
546
|
+
continue;
|
|
547
|
+
}
|
|
548
|
+
const nextVisited = new Set(visited);
|
|
549
|
+
nextVisited.add(fragName);
|
|
550
|
+
const sub = measure(fragments.get(fragName) || [], nextVisited);
|
|
551
|
+
if (sub.hitCycle) return {
|
|
552
|
+
fields: selectionLimit,
|
|
553
|
+
aliases: aliasLimit,
|
|
554
|
+
hitCycle: true
|
|
555
|
+
};
|
|
556
|
+
const clamped = {
|
|
557
|
+
fields: Math.min(selectionLimit, sub.fields),
|
|
558
|
+
aliases: Math.min(aliasLimit, sub.aliases)
|
|
559
|
+
};
|
|
560
|
+
fragMemo.set(fragName, clamped);
|
|
561
|
+
fields = Math.min(selectionLimit, fields + clamped.fields);
|
|
562
|
+
aliases = Math.min(aliasLimit, aliases + clamped.aliases);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
return {
|
|
566
|
+
fields,
|
|
567
|
+
aliases,
|
|
568
|
+
hitCycle: false
|
|
569
|
+
};
|
|
570
|
+
};
|
|
571
|
+
let totalFields = 0;
|
|
572
|
+
let totalAliases = 0;
|
|
573
|
+
for (const op of operations) {
|
|
574
|
+
const sub = measure(op.selectionSet?.selections || []);
|
|
575
|
+
totalFields = Math.min(selectionLimit, totalFields + sub.fields);
|
|
576
|
+
totalAliases = Math.min(aliasLimit, totalAliases + sub.aliases);
|
|
577
|
+
if (totalFields >= selectionLimit && totalAliases >= aliasLimit) break;
|
|
578
|
+
}
|
|
579
|
+
return {
|
|
580
|
+
fields: totalFields,
|
|
581
|
+
aliases: totalAliases
|
|
582
|
+
};
|
|
583
|
+
}
|
|
584
|
+
const parseEnvPositiveInt = (raw) => {
|
|
585
|
+
if (raw === void 0 || raw.trim() === "") return void 0;
|
|
586
|
+
const parsed = Number.parseInt(raw.trim(), 10);
|
|
587
|
+
return Number.isFinite(parsed) && parsed >= 0 ? parsed : void 0;
|
|
588
|
+
};
|
|
474
589
|
const parseEnvBoolean = (raw) => {
|
|
475
590
|
if (raw === void 0 || raw.trim() === "") return void 0;
|
|
476
591
|
const normalized = raw.trim().toLowerCase();
|
|
477
592
|
if (normalized === "1" || normalized === "true") return true;
|
|
478
593
|
if (normalized === "0" || normalized === "false") return false;
|
|
479
594
|
};
|
|
480
|
-
const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, graphiql = parseEnvBoolean(process.env.GQL_GRAPHIQL ?? process.env.GRAPHQL_GRAPHIQL) ?? process.env.NODE_ENV !== "production", introspection = parseEnvBoolean(process.env.GQL_INTROSPECTION ?? process.env.GRAPHQL_INTROSPECTION) ?? process.env.NODE_ENV !== "production", showStack = process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test", maxQueryDepth = MAX_QUERY_DEPTH }) => {
|
|
595
|
+
const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, graphiql = parseEnvBoolean(process.env.GQL_GRAPHIQL ?? process.env.GRAPHQL_GRAPHIQL) ?? process.env.NODE_ENV !== "production", introspection = parseEnvBoolean(process.env.GQL_INTROSPECTION ?? process.env.GRAPHQL_INTROSPECTION) ?? process.env.NODE_ENV !== "production", showStack = process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test", maxQueryDepth = MAX_QUERY_DEPTH, maxTotalSelections = parseEnvPositiveInt(process.env.GQL_MAX_TOTAL_SELECTIONS) ?? MAX_TOTAL_SELECTIONS, maxAliases = parseEnvPositiveInt(process.env.GQL_MAX_ALIASES) ?? MAX_ALIASES }) => {
|
|
481
596
|
const schema = buildSchema(schemaSource);
|
|
482
597
|
const rootValue = createResolvers(resolver);
|
|
483
598
|
const formatGraphQLError = (err) => {
|
|
484
599
|
onError(err);
|
|
485
600
|
const includeStack = showStack && process.env.NODE_ENV !== "production";
|
|
486
|
-
|
|
601
|
+
const formatted = {
|
|
487
602
|
code: get(err, "originalError.code", "INTERNAL"),
|
|
488
603
|
message: err.message,
|
|
489
604
|
locations: err.locations || [],
|
|
490
605
|
stack: includeStack ? get(err, "originalError.stack", "").split("\n") : [],
|
|
491
606
|
path: err.path
|
|
492
607
|
};
|
|
608
|
+
const warning = get(err, "originalError.warning") ?? get(err, "originalError.props.warning");
|
|
609
|
+
if (warning !== void 0) formatted.warning = warning;
|
|
610
|
+
const slashEvidence = get(err, "originalError.slashEvidence") ?? get(err, "originalError.props.slashEvidence");
|
|
611
|
+
if (slashEvidence !== void 0) formatted.slashEvidence = slashEvidence;
|
|
612
|
+
return formatted;
|
|
493
613
|
};
|
|
494
614
|
const formatAnyError = (input) => {
|
|
495
615
|
const message = typeof input === "string" ? input : input.message;
|
|
@@ -536,6 +656,13 @@ const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, gra
|
|
|
536
656
|
if (maxQueryDepth > 0) {
|
|
537
657
|
if (getMaxSelectionDepth(documentAST, maxQueryDepth + 1) > maxQueryDepth) throw new Error(`Query depth exceeded allowed maximum depth of ${maxQueryDepth}`);
|
|
538
658
|
}
|
|
659
|
+
if (maxTotalSelections > 0 || maxAliases > 0) {
|
|
660
|
+
const selLimit = maxTotalSelections > 0 ? maxTotalSelections + 1 : Number.MAX_SAFE_INTEGER;
|
|
661
|
+
const aliasLimit = maxAliases > 0 ? maxAliases + 1 : Number.MAX_SAFE_INTEGER;
|
|
662
|
+
const { fields, aliases } = countTotalSelectionsAndAliases(documentAST, selLimit, aliasLimit);
|
|
663
|
+
if (maxTotalSelections > 0 && fields > maxTotalSelections) throw new Error(`Query complexity exceeded allowed maximum of ${maxTotalSelections} total field selections`);
|
|
664
|
+
if (maxAliases > 0 && aliases > maxAliases) throw new Error(`Query aliases exceeded allowed maximum of ${maxAliases} aliases`);
|
|
665
|
+
}
|
|
539
666
|
} catch (batchError) {
|
|
540
667
|
return sendJSON(500, { errors: [formatAnyError(batchError)] });
|
|
541
668
|
}
|
package/lib/http.cjs
CHANGED
|
@@ -220,11 +220,27 @@ const resolvers = [
|
|
|
220
220
|
}
|
|
221
221
|
];
|
|
222
222
|
const createResolvers = (resolver) => resolvers.reduce((acc, { fn, dataKey }) => {
|
|
223
|
-
acc[fn] = (args, ctx) =>
|
|
224
|
-
const
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
223
|
+
acc[fn] = async (args, ctx) => {
|
|
224
|
+
const res = await wrapResolver(fn, dataKey, () => {
|
|
225
|
+
const method = resolver[fn];
|
|
226
|
+
if (typeof method === "function") return method.call(resolver, args, formatContext(ctx));
|
|
227
|
+
return null;
|
|
228
|
+
});
|
|
229
|
+
if (fn === "sendTx") {
|
|
230
|
+
if (res.hash && typeof res.hash === "object" && "hash" in res.hash) {
|
|
231
|
+
const inner = res.hash;
|
|
232
|
+
res.hash = inner.hash;
|
|
233
|
+
if (inner.warning !== void 0) res.warning = inner.warning;
|
|
234
|
+
} else if (typeof res.hash === "string") {
|
|
235
|
+
const getWarning = resolver.getTxSecurityWarning;
|
|
236
|
+
if (typeof getWarning === "function") {
|
|
237
|
+
const warning = getWarning.call(resolver, res.hash);
|
|
238
|
+
if (warning !== void 0) res.warning = warning;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return res;
|
|
243
|
+
};
|
|
228
244
|
return acc;
|
|
229
245
|
}, {});
|
|
230
246
|
const defaultErrorHandler = (err) => {
|
|
@@ -232,6 +248,8 @@ const defaultErrorHandler = (err) => {
|
|
|
232
248
|
};
|
|
233
249
|
const MAX_BATCH_SIZE = 40;
|
|
234
250
|
const MAX_QUERY_DEPTH = 15;
|
|
251
|
+
const MAX_TOTAL_SELECTIONS = 1e3;
|
|
252
|
+
const MAX_ALIASES = 100;
|
|
235
253
|
const BODY_SIZE_LIMIT = 100 * 1024;
|
|
236
254
|
/**
|
|
237
255
|
* Read the raw express request body as a utf-8 string, enforcing the size limit.
|
|
@@ -474,25 +492,127 @@ function getMaxSelectionDepth(documentAST, limit = MAX_QUERY_DEPTH + 1) {
|
|
|
474
492
|
}
|
|
475
493
|
return max;
|
|
476
494
|
}
|
|
495
|
+
/**
|
|
496
|
+
* Count total expanded field selections and field aliases across all operations
|
|
497
|
+
* in the document, recursively expanding inline and named fragments with memoization.
|
|
498
|
+
* Bounds fragment + alias width amplification (e.g. K^D nested fragment spreads) in O(|AST|) time.
|
|
499
|
+
*/
|
|
500
|
+
function countTotalSelectionsAndAliases(documentAST, selectionLimit = MAX_TOTAL_SELECTIONS + 1, aliasLimit = MAX_ALIASES + 1) {
|
|
501
|
+
const fragments = /* @__PURE__ */ new Map();
|
|
502
|
+
const operations = [];
|
|
503
|
+
for (const def of documentAST.definitions) if (def.kind === "FragmentDefinition" && def.name?.value) fragments.set(def.name.value, def.selectionSet?.selections || []);
|
|
504
|
+
else if (def.kind === "OperationDefinition") operations.push(def);
|
|
505
|
+
const fragMemo = /* @__PURE__ */ new Map();
|
|
506
|
+
const measure = (selections, visited = /* @__PURE__ */ new Set()) => {
|
|
507
|
+
let fields = 0;
|
|
508
|
+
let aliases = 0;
|
|
509
|
+
for (const sel of selections) {
|
|
510
|
+
if (fields >= selectionLimit && aliases >= aliasLimit) return {
|
|
511
|
+
fields: selectionLimit,
|
|
512
|
+
aliases: aliasLimit,
|
|
513
|
+
hitCycle: false
|
|
514
|
+
};
|
|
515
|
+
if (sel.kind === "Field") {
|
|
516
|
+
fields = Math.min(selectionLimit, fields + 1);
|
|
517
|
+
if (sel.alias?.value) aliases = Math.min(aliasLimit, aliases + 1);
|
|
518
|
+
const childSelections = sel.selectionSet?.selections || [];
|
|
519
|
+
if (childSelections.length > 0) {
|
|
520
|
+
const sub = measure(childSelections, visited);
|
|
521
|
+
if (sub.hitCycle) return {
|
|
522
|
+
fields: selectionLimit,
|
|
523
|
+
aliases: aliasLimit,
|
|
524
|
+
hitCycle: true
|
|
525
|
+
};
|
|
526
|
+
fields = Math.min(selectionLimit, fields + sub.fields);
|
|
527
|
+
aliases = Math.min(aliasLimit, aliases + sub.aliases);
|
|
528
|
+
}
|
|
529
|
+
} else if (sel.kind === "InlineFragment") {
|
|
530
|
+
const sub = measure(sel.selectionSet?.selections || [], visited);
|
|
531
|
+
if (sub.hitCycle) return {
|
|
532
|
+
fields: selectionLimit,
|
|
533
|
+
aliases: aliasLimit,
|
|
534
|
+
hitCycle: true
|
|
535
|
+
};
|
|
536
|
+
fields = Math.min(selectionLimit, fields + sub.fields);
|
|
537
|
+
aliases = Math.min(aliasLimit, aliases + sub.aliases);
|
|
538
|
+
} else if (sel.kind === "FragmentSpread" && sel.name?.value) {
|
|
539
|
+
const fragName = sel.name.value;
|
|
540
|
+
if (visited.has(fragName)) return {
|
|
541
|
+
fields: selectionLimit,
|
|
542
|
+
aliases: aliasLimit,
|
|
543
|
+
hitCycle: true
|
|
544
|
+
};
|
|
545
|
+
const cached = fragMemo.get(fragName);
|
|
546
|
+
if (cached !== void 0) {
|
|
547
|
+
fields = Math.min(selectionLimit, fields + cached.fields);
|
|
548
|
+
aliases = Math.min(aliasLimit, aliases + cached.aliases);
|
|
549
|
+
continue;
|
|
550
|
+
}
|
|
551
|
+
const nextVisited = new Set(visited);
|
|
552
|
+
nextVisited.add(fragName);
|
|
553
|
+
const sub = measure(fragments.get(fragName) || [], nextVisited);
|
|
554
|
+
if (sub.hitCycle) return {
|
|
555
|
+
fields: selectionLimit,
|
|
556
|
+
aliases: aliasLimit,
|
|
557
|
+
hitCycle: true
|
|
558
|
+
};
|
|
559
|
+
const clamped = {
|
|
560
|
+
fields: Math.min(selectionLimit, sub.fields),
|
|
561
|
+
aliases: Math.min(aliasLimit, sub.aliases)
|
|
562
|
+
};
|
|
563
|
+
fragMemo.set(fragName, clamped);
|
|
564
|
+
fields = Math.min(selectionLimit, fields + clamped.fields);
|
|
565
|
+
aliases = Math.min(aliasLimit, aliases + clamped.aliases);
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
return {
|
|
569
|
+
fields,
|
|
570
|
+
aliases,
|
|
571
|
+
hitCycle: false
|
|
572
|
+
};
|
|
573
|
+
};
|
|
574
|
+
let totalFields = 0;
|
|
575
|
+
let totalAliases = 0;
|
|
576
|
+
for (const op of operations) {
|
|
577
|
+
const sub = measure(op.selectionSet?.selections || []);
|
|
578
|
+
totalFields = Math.min(selectionLimit, totalFields + sub.fields);
|
|
579
|
+
totalAliases = Math.min(aliasLimit, totalAliases + sub.aliases);
|
|
580
|
+
if (totalFields >= selectionLimit && totalAliases >= aliasLimit) break;
|
|
581
|
+
}
|
|
582
|
+
return {
|
|
583
|
+
fields: totalFields,
|
|
584
|
+
aliases: totalAliases
|
|
585
|
+
};
|
|
586
|
+
}
|
|
587
|
+
const parseEnvPositiveInt = (raw) => {
|
|
588
|
+
if (raw === void 0 || raw.trim() === "") return void 0;
|
|
589
|
+
const parsed = Number.parseInt(raw.trim(), 10);
|
|
590
|
+
return Number.isFinite(parsed) && parsed >= 0 ? parsed : void 0;
|
|
591
|
+
};
|
|
477
592
|
const parseEnvBoolean = (raw) => {
|
|
478
593
|
if (raw === void 0 || raw.trim() === "") return void 0;
|
|
479
594
|
const normalized = raw.trim().toLowerCase();
|
|
480
595
|
if (normalized === "1" || normalized === "true") return true;
|
|
481
596
|
if (normalized === "0" || normalized === "false") return false;
|
|
482
597
|
};
|
|
483
|
-
const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, graphiql = parseEnvBoolean(process.env.GQL_GRAPHIQL ?? process.env.GRAPHQL_GRAPHIQL) ?? process.env.NODE_ENV !== "production", introspection = parseEnvBoolean(process.env.GQL_INTROSPECTION ?? process.env.GRAPHQL_INTROSPECTION) ?? process.env.NODE_ENV !== "production", showStack = process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test", maxQueryDepth = MAX_QUERY_DEPTH }) => {
|
|
598
|
+
const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, graphiql = parseEnvBoolean(process.env.GQL_GRAPHIQL ?? process.env.GRAPHQL_GRAPHIQL) ?? process.env.NODE_ENV !== "production", introspection = parseEnvBoolean(process.env.GQL_INTROSPECTION ?? process.env.GRAPHQL_INTROSPECTION) ?? process.env.NODE_ENV !== "production", showStack = process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test", maxQueryDepth = MAX_QUERY_DEPTH, maxTotalSelections = parseEnvPositiveInt(process.env.GQL_MAX_TOTAL_SELECTIONS) ?? MAX_TOTAL_SELECTIONS, maxAliases = parseEnvPositiveInt(process.env.GQL_MAX_ALIASES) ?? MAX_ALIASES }) => {
|
|
484
599
|
const schema = (0, graphql.buildSchema)(_ocap_schema.default);
|
|
485
600
|
const rootValue = createResolvers(resolver);
|
|
486
601
|
const formatGraphQLError = (err) => {
|
|
487
602
|
onError(err);
|
|
488
603
|
const includeStack = showStack && process.env.NODE_ENV !== "production";
|
|
489
|
-
|
|
604
|
+
const formatted = {
|
|
490
605
|
code: (0, lodash_get.default)(err, "originalError.code", "INTERNAL"),
|
|
491
606
|
message: err.message,
|
|
492
607
|
locations: err.locations || [],
|
|
493
608
|
stack: includeStack ? (0, lodash_get.default)(err, "originalError.stack", "").split("\n") : [],
|
|
494
609
|
path: err.path
|
|
495
610
|
};
|
|
611
|
+
const warning = (0, lodash_get.default)(err, "originalError.warning") ?? (0, lodash_get.default)(err, "originalError.props.warning");
|
|
612
|
+
if (warning !== void 0) formatted.warning = warning;
|
|
613
|
+
const slashEvidence = (0, lodash_get.default)(err, "originalError.slashEvidence") ?? (0, lodash_get.default)(err, "originalError.props.slashEvidence");
|
|
614
|
+
if (slashEvidence !== void 0) formatted.slashEvidence = slashEvidence;
|
|
615
|
+
return formatted;
|
|
496
616
|
};
|
|
497
617
|
const formatAnyError = (input) => {
|
|
498
618
|
const message = typeof input === "string" ? input : input.message;
|
|
@@ -539,6 +659,13 @@ const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, gra
|
|
|
539
659
|
if (maxQueryDepth > 0) {
|
|
540
660
|
if (getMaxSelectionDepth(documentAST, maxQueryDepth + 1) > maxQueryDepth) throw new Error(`Query depth exceeded allowed maximum depth of ${maxQueryDepth}`);
|
|
541
661
|
}
|
|
662
|
+
if (maxTotalSelections > 0 || maxAliases > 0) {
|
|
663
|
+
const selLimit = maxTotalSelections > 0 ? maxTotalSelections + 1 : Number.MAX_SAFE_INTEGER;
|
|
664
|
+
const aliasLimit = maxAliases > 0 ? maxAliases + 1 : Number.MAX_SAFE_INTEGER;
|
|
665
|
+
const { fields, aliases } = countTotalSelectionsAndAliases(documentAST, selLimit, aliasLimit);
|
|
666
|
+
if (maxTotalSelections > 0 && fields > maxTotalSelections) throw new Error(`Query complexity exceeded allowed maximum of ${maxTotalSelections} total field selections`);
|
|
667
|
+
if (maxAliases > 0 && aliases > maxAliases) throw new Error(`Query aliases exceeded allowed maximum of ${maxAliases} aliases`);
|
|
668
|
+
}
|
|
542
669
|
} catch (batchError) {
|
|
543
670
|
return sendJSON(500, { errors: [formatAnyError(batchError)] });
|
|
544
671
|
}
|
package/lib/http.d.cts
CHANGED
|
@@ -32,6 +32,8 @@ interface HttpHandlerOptions {
|
|
|
32
32
|
introspection?: boolean;
|
|
33
33
|
showStack?: boolean;
|
|
34
34
|
maxQueryDepth?: number;
|
|
35
|
+
maxTotalSelections?: number;
|
|
36
|
+
maxAliases?: number;
|
|
35
37
|
}
|
|
36
38
|
/**
|
|
37
39
|
* Transport-agnostic view of an incoming GraphQL request, so the same engine serves express (`createHttpHandler`)
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.
|
|
6
|
+
"version": "1.31.0",
|
|
7
7
|
"description": "Resolver middleware for ocap adapters",
|
|
8
8
|
"type": "module",
|
|
9
9
|
"main": "./lib/index.cjs",
|
|
@@ -51,8 +51,8 @@
|
|
|
51
51
|
],
|
|
52
52
|
"license": "MIT",
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"@arcblock/ws": "1.
|
|
55
|
-
"@ocap/schema": "1.
|
|
54
|
+
"@arcblock/ws": "1.31.0",
|
|
55
|
+
"@ocap/schema": "1.31.0",
|
|
56
56
|
"graphql": "^16.13.2",
|
|
57
57
|
"lodash": "^4.17.23"
|
|
58
58
|
},
|