@ocap/gql 1.31.3 → 1.31.5

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 CHANGED
@@ -56,6 +56,17 @@ interface GqlResponseLike {
56
56
  headers: Record<string, string>;
57
57
  body: string;
58
58
  }
59
+ /**
60
+ * Normalize legacy digit-leading field aliases (e.g. `{ 2CE271...: listTransactions(...) }`, #1626)
61
+ * strictly inside executable selection sets (`braceDepth >= 1 && parenDepth === 0 && bracketDepth === 0`).
62
+ * String / block-string / comment scanning mirrors `graphql/language/lexer` character-for-character.
63
+ * Standard valid GraphQL queries take the zero-allocation fast path (`aliasMap: null`).
64
+ */
65
+ declare function normalizeNumericFieldAliases(query: string): {
66
+ query: string;
67
+ prefix: string | null;
68
+ aliasMap: Map<string, string> | null;
69
+ };
59
70
  /** Express / connect adapter. Behavior is unchanged from the pre-split `createHttpHandler`. */
60
71
  declare const createHttpHandler: (options: HttpHandlerOptions) => RequestHandler;
61
72
  /**
@@ -65,4 +76,4 @@ declare const createHttpHandler: (options: HttpHandlerOptions) => RequestHandler
65
76
  */
66
77
  declare const createFetchHandler: (options: HttpHandlerOptions) => (request: globalThis.Request) => Promise<globalThis.Response>;
67
78
  //#endregion
68
- export { GqlRequestLike, GqlResponseLike, HttpHandlerOptions, type Resolver, createFetchHandler, createHttpHandler, createResolvers, formatContext, wrapResolver };
79
+ export { GqlRequestLike, GqlResponseLike, HttpHandlerOptions, type Resolver, createFetchHandler, createHttpHandler, createResolvers, formatContext, normalizeNumericFieldAliases, wrapResolver };
package/esm/http.mjs CHANGED
@@ -245,8 +245,9 @@ const defaultErrorHandler = (err) => {
245
245
  };
246
246
  const MAX_BATCH_SIZE = 40;
247
247
  const MAX_QUERY_DEPTH = 15;
248
- const MAX_TOTAL_SELECTIONS = 1e3;
248
+ const MAX_TOTAL_SELECTIONS = 3e3;
249
249
  const MAX_ALIASES = 100;
250
+ const MAX_NUMERIC_ALIAS_REWRITES = 2e3;
250
251
  const BODY_SIZE_LIMIT = 100 * 1024;
251
252
  /**
252
253
  * Read the raw express request body as a utf-8 string, enforcing the size limit.
@@ -581,6 +582,204 @@ function countTotalSelectionsAndAliases(documentAST, selectionLimit = MAX_TOTAL_
581
582
  aliases: totalAliases
582
583
  };
583
584
  }
585
+ const isAsciiDigit = (code) => code >= 48 && code <= 57;
586
+ const isAsciiNameStart = (code) => code >= 65 && code <= 90 || code >= 97 && code <= 122 || code === 95;
587
+ const isAsciiNameContinue = (code) => isAsciiNameStart(code) || isAsciiDigit(code);
588
+ const isBlockedPrevTokenChar = (code) => isAsciiNameContinue(code) || code === 46 || code === 36 || code === 64 || code === 45;
589
+ const skipGraphQLIgnored = (src, start) => {
590
+ const len = src.length;
591
+ let k = start;
592
+ while (k < len) {
593
+ const c = src.charCodeAt(k);
594
+ if (c === 32 || c === 9 || c === 10 || c === 13 || c === 44 || c === 65279) {
595
+ k += 1;
596
+ continue;
597
+ }
598
+ if (c === 35) {
599
+ k += 1;
600
+ while (k < len) {
601
+ const lc = src.charCodeAt(k);
602
+ if (lc === 10 || lc === 13) break;
603
+ k += 1;
604
+ }
605
+ continue;
606
+ }
607
+ break;
608
+ }
609
+ return k;
610
+ };
611
+ let fallbackAliasNonce = 0;
612
+ const makeAliasPrefix = (query) => {
613
+ let randHex = "";
614
+ if (typeof globalThis.crypto?.getRandomValues === "function") {
615
+ const words = new Uint32Array(2);
616
+ globalThis.crypto.getRandomValues(words);
617
+ randHex = words[0].toString(16).padStart(8, "0") + words[1].toString(16).padStart(8, "0");
618
+ } else {
619
+ fallbackAliasNonce = fallbackAliasNonce + 1 >>> 0;
620
+ randHex = fallbackAliasNonce.toString(16);
621
+ }
622
+ let prefix = `_na_${randHex}_`;
623
+ while (query.includes(prefix)) prefix += "x_";
624
+ return prefix;
625
+ };
626
+ /**
627
+ * Normalize legacy digit-leading field aliases (e.g. `{ 2CE271...: listTransactions(...) }`, #1626)
628
+ * strictly inside executable selection sets (`braceDepth >= 1 && parenDepth === 0 && bracketDepth === 0`).
629
+ * String / block-string / comment scanning mirrors `graphql/language/lexer` character-for-character.
630
+ * Standard valid GraphQL queries take the zero-allocation fast path (`aliasMap: null`).
631
+ */
632
+ function normalizeNumericFieldAliases(query) {
633
+ const len = query.length;
634
+ const rewrites = [];
635
+ let braceDepth = 0;
636
+ let parenDepth = 0;
637
+ let bracketDepth = 0;
638
+ let i = 0;
639
+ while (i < len) {
640
+ const code = query.charCodeAt(i);
641
+ if (code === 35) {
642
+ i += 1;
643
+ while (i < len) {
644
+ const lc = query.charCodeAt(i);
645
+ if (lc === 10 || lc === 13) break;
646
+ i += 1;
647
+ }
648
+ continue;
649
+ }
650
+ if (code === 34 && query.charCodeAt(i + 1) === 34 && query.charCodeAt(i + 2) === 34) {
651
+ i += 3;
652
+ while (i < len) {
653
+ const bc = query.charCodeAt(i);
654
+ if (bc === 34 && query.charCodeAt(i + 1) === 34 && query.charCodeAt(i + 2) === 34) {
655
+ i += 3;
656
+ break;
657
+ }
658
+ if (bc === 92 && query.charCodeAt(i + 1) === 34 && query.charCodeAt(i + 2) === 34 && query.charCodeAt(i + 3) === 34) {
659
+ i += 4;
660
+ continue;
661
+ }
662
+ i += 1;
663
+ }
664
+ continue;
665
+ }
666
+ if (code === 34) {
667
+ i += 1;
668
+ while (i < len) {
669
+ const sc = query.charCodeAt(i);
670
+ if (sc === 34) {
671
+ i += 1;
672
+ break;
673
+ }
674
+ if (sc === 92) {
675
+ const next = query.charCodeAt(i + 1);
676
+ if (i + 1 >= len || next === 10 || next === 13) break;
677
+ i += 2;
678
+ continue;
679
+ }
680
+ if (sc === 10 || sc === 13) break;
681
+ i += 1;
682
+ }
683
+ continue;
684
+ }
685
+ if (code === 123) {
686
+ braceDepth += 1;
687
+ i += 1;
688
+ continue;
689
+ }
690
+ if (code === 125) {
691
+ if (braceDepth > 0) braceDepth -= 1;
692
+ i += 1;
693
+ continue;
694
+ }
695
+ if (code === 40) {
696
+ parenDepth += 1;
697
+ i += 1;
698
+ continue;
699
+ }
700
+ if (code === 41) {
701
+ if (parenDepth > 0) parenDepth -= 1;
702
+ i += 1;
703
+ continue;
704
+ }
705
+ if (code === 91) {
706
+ bracketDepth += 1;
707
+ i += 1;
708
+ continue;
709
+ }
710
+ if (code === 93) {
711
+ if (bracketDepth > 0) bracketDepth -= 1;
712
+ i += 1;
713
+ continue;
714
+ }
715
+ if (isAsciiNameStart(code)) {
716
+ i += 1;
717
+ while (i < len && isAsciiNameContinue(query.charCodeAt(i))) i += 1;
718
+ continue;
719
+ }
720
+ if (isAsciiDigit(code)) {
721
+ const canRewriteHere = braceDepth >= 1 && parenDepth === 0 && bracketDepth === 0 && (i === 0 || !isBlockedPrevTokenChar(query.charCodeAt(i - 1)));
722
+ let end = i + 1;
723
+ while (end < len && isAsciiNameContinue(query.charCodeAt(end))) end += 1;
724
+ if (canRewriteHere && query.charCodeAt(end) !== 46 && rewrites.length < MAX_NUMERIC_ALIAS_REWRITES && query.charCodeAt(skipGraphQLIgnored(query, end)) === 58) rewrites.push({
725
+ start: i,
726
+ end,
727
+ raw: query.slice(i, end)
728
+ });
729
+ i = end;
730
+ continue;
731
+ }
732
+ i += 1;
733
+ }
734
+ if (rewrites.length === 0) return {
735
+ query,
736
+ prefix: null,
737
+ aliasMap: null
738
+ };
739
+ const prefix = makeAliasPrefix(query);
740
+ const aliasMap = /* @__PURE__ */ new Map();
741
+ const parts = [];
742
+ let cursor = 0;
743
+ for (const r of rewrites) {
744
+ const synthetic = `${prefix}${r.raw}`;
745
+ aliasMap.set(synthetic, r.raw);
746
+ parts.push(query.slice(cursor, r.start), synthetic);
747
+ cursor = r.end;
748
+ }
749
+ parts.push(query.slice(cursor));
750
+ return {
751
+ query: parts.join(""),
752
+ prefix,
753
+ aliasMap
754
+ };
755
+ }
756
+ const restoreNumericAliasesInData = (value, aliasMap) => {
757
+ if (Array.isArray(value)) return value.map((item) => restoreNumericAliasesInData(item, aliasMap));
758
+ if (value !== null && typeof value === "object" && Object.getPrototypeOf(value) === null) {
759
+ const out = Object.create(null);
760
+ for (const [k, v] of Object.entries(value)) {
761
+ const restoredKey = aliasMap.get(k) ?? k;
762
+ Object.defineProperty(out, restoredKey, {
763
+ value: restoreNumericAliasesInData(v, aliasMap),
764
+ enumerable: true,
765
+ configurable: true,
766
+ writable: true
767
+ });
768
+ }
769
+ return out;
770
+ }
771
+ return value;
772
+ };
773
+ const restoreNumericAliasesInFormattedError = (formatted, prefix, aliasMap) => {
774
+ if (!prefix || !aliasMap) return formatted;
775
+ if (typeof formatted.message === "string" && formatted.message.includes(prefix)) {
776
+ let msg = formatted.message;
777
+ for (const [synthetic, raw] of aliasMap) if (msg.includes(synthetic)) msg = msg.split(synthetic).join(raw);
778
+ formatted.message = msg;
779
+ }
780
+ if (Array.isArray(formatted.path)) formatted.path = formatted.path.map((seg) => typeof seg === "string" ? aliasMap.get(seg) ?? seg : seg);
781
+ return formatted;
782
+ };
584
783
  const parseEnvPositiveInt = (raw) => {
585
784
  if (raw === void 0 || raw.trim() === "") return void 0;
586
785
  const parsed = Number.parseInt(raw.trim(), 10);
@@ -641,15 +840,17 @@ const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, gra
641
840
  if (showGraphiQL) return sendHTML(GRAPHIQL_HTML);
642
841
  return sendJSON(400, { errors: [formatAnyError("Must provide query string.")] });
643
842
  }
843
+ const { query: normalizedQuery, prefix: aliasPrefix, aliasMap } = normalizeNumericFieldAliases(params.query);
844
+ const formatError = (err) => restoreNumericAliasesInFormattedError(formatGraphQLError(err), aliasPrefix, aliasMap);
644
845
  let documentAST;
645
846
  try {
646
- documentAST = parse(params.query);
847
+ documentAST = parse(normalizedQuery);
647
848
  } catch (syntaxError) {
648
- return sendJSON(400, { errors: [formatGraphQLError(syntaxError)] });
849
+ return sendJSON(400, { errors: [formatError(syntaxError)] });
649
850
  }
650
851
  if (!introspection) {
651
852
  const validationErrors = validate(schema, documentAST, [NoSchemaIntrospectionCustomRule]);
652
- if (validationErrors.length > 0) return sendJSON(400, { errors: validationErrors.map(formatGraphQLError) });
853
+ if (validationErrors.length > 0) return sendJSON(400, { errors: validationErrors.map(formatError) });
653
854
  }
654
855
  try {
655
856
  if (countTopLevelSelections(documentAST) > MAX_BATCH_SIZE) throw new Error(`Batch query size exceeded allowed maximum batch size of ${MAX_BATCH_SIZE}`);
@@ -683,8 +884,8 @@ const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, gra
683
884
  } catch (contextError) {
684
885
  return sendJSON(400, { errors: [formatAnyError(contextError)] });
685
886
  }
686
- const formattedResult = { data: result.data };
687
- if (result.errors && result.errors.length > 0) formattedResult.errors = result.errors.map(formatGraphQLError);
887
+ const formattedResult = { data: aliasMap && result.data != null ? restoreNumericAliasesInData(result.data, aliasMap) : result.data };
888
+ if (result.errors && result.errors.length > 0) formattedResult.errors = result.errors.map(formatError);
688
889
  return sendJSON(result.data == null ? 500 : 200, formattedResult);
689
890
  };
690
891
  };
@@ -759,4 +960,4 @@ const createFetchHandler = (options) => {
759
960
  };
760
961
 
761
962
  //#endregion
762
- export { createFetchHandler, createHttpHandler, createResolvers, formatContext, wrapResolver };
963
+ export { createFetchHandler, createHttpHandler, createResolvers, formatContext, normalizeNumericFieldAliases, wrapResolver };
package/esm/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { GqlRequestLike, GqlResponseLike, HttpHandlerOptions, Resolver, createFetchHandler, createHttpHandler, createResolvers, formatContext, wrapResolver } from "./http.mjs";
1
+ import { GqlRequestLike, GqlResponseLike, HttpHandlerOptions, Resolver, createFetchHandler, createHttpHandler, createResolvers, formatContext, normalizeNumericFieldAliases, wrapResolver } from "./http.mjs";
2
2
  import { Server } from "node:http";
3
3
  import { TIndexedAccountState, TIndexedAssetState, TIndexedDelegationState, TIndexedFactoryState, TIndexedRollupBlock, TIndexedRollupState, TIndexedStakeState, TIndexedTokenState, TIndexedTransaction } from "@ocap/types";
4
4
  import { Express } from "express";
@@ -39,4 +39,4 @@ declare const createSocketHandler: ({
39
39
  wsOptions
40
40
  }: SocketHandlerOptions) => Server;
41
41
  //#endregion
42
- export { GqlRequestLike, GqlResponseLike, HttpHandlerOptions, Resolver, createFetchHandler, createHttpHandler, createResolvers, createSocketHandler, formatContext, wrapResolver };
42
+ export { GqlRequestLike, GqlResponseLike, HttpHandlerOptions, Resolver, createFetchHandler, createHttpHandler, createResolvers, createSocketHandler, formatContext, normalizeNumericFieldAliases, wrapResolver };
package/esm/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { createFetchHandler, createHttpHandler, createResolvers, formatContext, wrapResolver } from "./http.mjs";
1
+ import { createFetchHandler, createHttpHandler, createResolvers, formatContext, normalizeNumericFieldAliases, wrapResolver } from "./http.mjs";
2
2
  import createWebsocketServer from "./ws.mjs";
3
3
  import { createServer } from "node:http";
4
4
 
@@ -13,4 +13,4 @@ const createSocketHandler = ({ app, indexdb, wsOptions }) => {
13
13
  };
14
14
 
15
15
  //#endregion
16
- export { createFetchHandler, createHttpHandler, createResolvers, createSocketHandler, formatContext, wrapResolver };
16
+ export { createFetchHandler, createHttpHandler, createResolvers, createSocketHandler, formatContext, normalizeNumericFieldAliases, wrapResolver };
package/lib/http.cjs CHANGED
@@ -248,8 +248,9 @@ const defaultErrorHandler = (err) => {
248
248
  };
249
249
  const MAX_BATCH_SIZE = 40;
250
250
  const MAX_QUERY_DEPTH = 15;
251
- const MAX_TOTAL_SELECTIONS = 1e3;
251
+ const MAX_TOTAL_SELECTIONS = 3e3;
252
252
  const MAX_ALIASES = 100;
253
+ const MAX_NUMERIC_ALIAS_REWRITES = 2e3;
253
254
  const BODY_SIZE_LIMIT = 100 * 1024;
254
255
  /**
255
256
  * Read the raw express request body as a utf-8 string, enforcing the size limit.
@@ -584,6 +585,204 @@ function countTotalSelectionsAndAliases(documentAST, selectionLimit = MAX_TOTAL_
584
585
  aliases: totalAliases
585
586
  };
586
587
  }
588
+ const isAsciiDigit = (code) => code >= 48 && code <= 57;
589
+ const isAsciiNameStart = (code) => code >= 65 && code <= 90 || code >= 97 && code <= 122 || code === 95;
590
+ const isAsciiNameContinue = (code) => isAsciiNameStart(code) || isAsciiDigit(code);
591
+ const isBlockedPrevTokenChar = (code) => isAsciiNameContinue(code) || code === 46 || code === 36 || code === 64 || code === 45;
592
+ const skipGraphQLIgnored = (src, start) => {
593
+ const len = src.length;
594
+ let k = start;
595
+ while (k < len) {
596
+ const c = src.charCodeAt(k);
597
+ if (c === 32 || c === 9 || c === 10 || c === 13 || c === 44 || c === 65279) {
598
+ k += 1;
599
+ continue;
600
+ }
601
+ if (c === 35) {
602
+ k += 1;
603
+ while (k < len) {
604
+ const lc = src.charCodeAt(k);
605
+ if (lc === 10 || lc === 13) break;
606
+ k += 1;
607
+ }
608
+ continue;
609
+ }
610
+ break;
611
+ }
612
+ return k;
613
+ };
614
+ let fallbackAliasNonce = 0;
615
+ const makeAliasPrefix = (query) => {
616
+ let randHex = "";
617
+ if (typeof globalThis.crypto?.getRandomValues === "function") {
618
+ const words = new Uint32Array(2);
619
+ globalThis.crypto.getRandomValues(words);
620
+ randHex = words[0].toString(16).padStart(8, "0") + words[1].toString(16).padStart(8, "0");
621
+ } else {
622
+ fallbackAliasNonce = fallbackAliasNonce + 1 >>> 0;
623
+ randHex = fallbackAliasNonce.toString(16);
624
+ }
625
+ let prefix = `_na_${randHex}_`;
626
+ while (query.includes(prefix)) prefix += "x_";
627
+ return prefix;
628
+ };
629
+ /**
630
+ * Normalize legacy digit-leading field aliases (e.g. `{ 2CE271...: listTransactions(...) }`, #1626)
631
+ * strictly inside executable selection sets (`braceDepth >= 1 && parenDepth === 0 && bracketDepth === 0`).
632
+ * String / block-string / comment scanning mirrors `graphql/language/lexer` character-for-character.
633
+ * Standard valid GraphQL queries take the zero-allocation fast path (`aliasMap: null`).
634
+ */
635
+ function normalizeNumericFieldAliases(query) {
636
+ const len = query.length;
637
+ const rewrites = [];
638
+ let braceDepth = 0;
639
+ let parenDepth = 0;
640
+ let bracketDepth = 0;
641
+ let i = 0;
642
+ while (i < len) {
643
+ const code = query.charCodeAt(i);
644
+ if (code === 35) {
645
+ i += 1;
646
+ while (i < len) {
647
+ const lc = query.charCodeAt(i);
648
+ if (lc === 10 || lc === 13) break;
649
+ i += 1;
650
+ }
651
+ continue;
652
+ }
653
+ if (code === 34 && query.charCodeAt(i + 1) === 34 && query.charCodeAt(i + 2) === 34) {
654
+ i += 3;
655
+ while (i < len) {
656
+ const bc = query.charCodeAt(i);
657
+ if (bc === 34 && query.charCodeAt(i + 1) === 34 && query.charCodeAt(i + 2) === 34) {
658
+ i += 3;
659
+ break;
660
+ }
661
+ if (bc === 92 && query.charCodeAt(i + 1) === 34 && query.charCodeAt(i + 2) === 34 && query.charCodeAt(i + 3) === 34) {
662
+ i += 4;
663
+ continue;
664
+ }
665
+ i += 1;
666
+ }
667
+ continue;
668
+ }
669
+ if (code === 34) {
670
+ i += 1;
671
+ while (i < len) {
672
+ const sc = query.charCodeAt(i);
673
+ if (sc === 34) {
674
+ i += 1;
675
+ break;
676
+ }
677
+ if (sc === 92) {
678
+ const next = query.charCodeAt(i + 1);
679
+ if (i + 1 >= len || next === 10 || next === 13) break;
680
+ i += 2;
681
+ continue;
682
+ }
683
+ if (sc === 10 || sc === 13) break;
684
+ i += 1;
685
+ }
686
+ continue;
687
+ }
688
+ if (code === 123) {
689
+ braceDepth += 1;
690
+ i += 1;
691
+ continue;
692
+ }
693
+ if (code === 125) {
694
+ if (braceDepth > 0) braceDepth -= 1;
695
+ i += 1;
696
+ continue;
697
+ }
698
+ if (code === 40) {
699
+ parenDepth += 1;
700
+ i += 1;
701
+ continue;
702
+ }
703
+ if (code === 41) {
704
+ if (parenDepth > 0) parenDepth -= 1;
705
+ i += 1;
706
+ continue;
707
+ }
708
+ if (code === 91) {
709
+ bracketDepth += 1;
710
+ i += 1;
711
+ continue;
712
+ }
713
+ if (code === 93) {
714
+ if (bracketDepth > 0) bracketDepth -= 1;
715
+ i += 1;
716
+ continue;
717
+ }
718
+ if (isAsciiNameStart(code)) {
719
+ i += 1;
720
+ while (i < len && isAsciiNameContinue(query.charCodeAt(i))) i += 1;
721
+ continue;
722
+ }
723
+ if (isAsciiDigit(code)) {
724
+ const canRewriteHere = braceDepth >= 1 && parenDepth === 0 && bracketDepth === 0 && (i === 0 || !isBlockedPrevTokenChar(query.charCodeAt(i - 1)));
725
+ let end = i + 1;
726
+ while (end < len && isAsciiNameContinue(query.charCodeAt(end))) end += 1;
727
+ if (canRewriteHere && query.charCodeAt(end) !== 46 && rewrites.length < MAX_NUMERIC_ALIAS_REWRITES && query.charCodeAt(skipGraphQLIgnored(query, end)) === 58) rewrites.push({
728
+ start: i,
729
+ end,
730
+ raw: query.slice(i, end)
731
+ });
732
+ i = end;
733
+ continue;
734
+ }
735
+ i += 1;
736
+ }
737
+ if (rewrites.length === 0) return {
738
+ query,
739
+ prefix: null,
740
+ aliasMap: null
741
+ };
742
+ const prefix = makeAliasPrefix(query);
743
+ const aliasMap = /* @__PURE__ */ new Map();
744
+ const parts = [];
745
+ let cursor = 0;
746
+ for (const r of rewrites) {
747
+ const synthetic = `${prefix}${r.raw}`;
748
+ aliasMap.set(synthetic, r.raw);
749
+ parts.push(query.slice(cursor, r.start), synthetic);
750
+ cursor = r.end;
751
+ }
752
+ parts.push(query.slice(cursor));
753
+ return {
754
+ query: parts.join(""),
755
+ prefix,
756
+ aliasMap
757
+ };
758
+ }
759
+ const restoreNumericAliasesInData = (value, aliasMap) => {
760
+ if (Array.isArray(value)) return value.map((item) => restoreNumericAliasesInData(item, aliasMap));
761
+ if (value !== null && typeof value === "object" && Object.getPrototypeOf(value) === null) {
762
+ const out = Object.create(null);
763
+ for (const [k, v] of Object.entries(value)) {
764
+ const restoredKey = aliasMap.get(k) ?? k;
765
+ Object.defineProperty(out, restoredKey, {
766
+ value: restoreNumericAliasesInData(v, aliasMap),
767
+ enumerable: true,
768
+ configurable: true,
769
+ writable: true
770
+ });
771
+ }
772
+ return out;
773
+ }
774
+ return value;
775
+ };
776
+ const restoreNumericAliasesInFormattedError = (formatted, prefix, aliasMap) => {
777
+ if (!prefix || !aliasMap) return formatted;
778
+ if (typeof formatted.message === "string" && formatted.message.includes(prefix)) {
779
+ let msg = formatted.message;
780
+ for (const [synthetic, raw] of aliasMap) if (msg.includes(synthetic)) msg = msg.split(synthetic).join(raw);
781
+ formatted.message = msg;
782
+ }
783
+ if (Array.isArray(formatted.path)) formatted.path = formatted.path.map((seg) => typeof seg === "string" ? aliasMap.get(seg) ?? seg : seg);
784
+ return formatted;
785
+ };
587
786
  const parseEnvPositiveInt = (raw) => {
588
787
  if (raw === void 0 || raw.trim() === "") return void 0;
589
788
  const parsed = Number.parseInt(raw.trim(), 10);
@@ -644,15 +843,17 @@ const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, gra
644
843
  if (showGraphiQL) return sendHTML(GRAPHIQL_HTML);
645
844
  return sendJSON(400, { errors: [formatAnyError("Must provide query string.")] });
646
845
  }
846
+ const { query: normalizedQuery, prefix: aliasPrefix, aliasMap } = normalizeNumericFieldAliases(params.query);
847
+ const formatError = (err) => restoreNumericAliasesInFormattedError(formatGraphQLError(err), aliasPrefix, aliasMap);
647
848
  let documentAST;
648
849
  try {
649
- documentAST = (0, graphql.parse)(params.query);
850
+ documentAST = (0, graphql.parse)(normalizedQuery);
650
851
  } catch (syntaxError) {
651
- return sendJSON(400, { errors: [formatGraphQLError(syntaxError)] });
852
+ return sendJSON(400, { errors: [formatError(syntaxError)] });
652
853
  }
653
854
  if (!introspection) {
654
855
  const validationErrors = (0, graphql.validate)(schema, documentAST, [graphql.NoSchemaIntrospectionCustomRule]);
655
- if (validationErrors.length > 0) return sendJSON(400, { errors: validationErrors.map(formatGraphQLError) });
856
+ if (validationErrors.length > 0) return sendJSON(400, { errors: validationErrors.map(formatError) });
656
857
  }
657
858
  try {
658
859
  if (countTopLevelSelections(documentAST) > MAX_BATCH_SIZE) throw new Error(`Batch query size exceeded allowed maximum batch size of ${MAX_BATCH_SIZE}`);
@@ -686,8 +887,8 @@ const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, gra
686
887
  } catch (contextError) {
687
888
  return sendJSON(400, { errors: [formatAnyError(contextError)] });
688
889
  }
689
- const formattedResult = { data: result.data };
690
- if (result.errors && result.errors.length > 0) formattedResult.errors = result.errors.map(formatGraphQLError);
890
+ const formattedResult = { data: aliasMap && result.data != null ? restoreNumericAliasesInData(result.data, aliasMap) : result.data };
891
+ if (result.errors && result.errors.length > 0) formattedResult.errors = result.errors.map(formatError);
691
892
  return sendJSON(result.data == null ? 500 : 200, formattedResult);
692
893
  };
693
894
  };
@@ -766,4 +967,5 @@ exports.createFetchHandler = createFetchHandler;
766
967
  exports.createHttpHandler = createHttpHandler;
767
968
  exports.createResolvers = createResolvers;
768
969
  exports.formatContext = formatContext;
970
+ exports.normalizeNumericFieldAliases = normalizeNumericFieldAliases;
769
971
  exports.wrapResolver = wrapResolver;
package/lib/http.d.cts CHANGED
@@ -56,6 +56,17 @@ interface GqlResponseLike {
56
56
  headers: Record<string, string>;
57
57
  body: string;
58
58
  }
59
+ /**
60
+ * Normalize legacy digit-leading field aliases (e.g. `{ 2CE271...: listTransactions(...) }`, #1626)
61
+ * strictly inside executable selection sets (`braceDepth >= 1 && parenDepth === 0 && bracketDepth === 0`).
62
+ * String / block-string / comment scanning mirrors `graphql/language/lexer` character-for-character.
63
+ * Standard valid GraphQL queries take the zero-allocation fast path (`aliasMap: null`).
64
+ */
65
+ declare function normalizeNumericFieldAliases(query: string): {
66
+ query: string;
67
+ prefix: string | null;
68
+ aliasMap: Map<string, string> | null;
69
+ };
59
70
  /** Express / connect adapter. Behavior is unchanged from the pre-split `createHttpHandler`. */
60
71
  declare const createHttpHandler: (options: HttpHandlerOptions) => RequestHandler;
61
72
  /**
@@ -65,4 +76,4 @@ declare const createHttpHandler: (options: HttpHandlerOptions) => RequestHandler
65
76
  */
66
77
  declare const createFetchHandler: (options: HttpHandlerOptions) => (request: globalThis.Request) => Promise<globalThis.Response>;
67
78
  //#endregion
68
- export { GqlRequestLike, GqlResponseLike, HttpHandlerOptions, type Resolver, createFetchHandler, createHttpHandler, createResolvers, formatContext, wrapResolver };
79
+ export { GqlRequestLike, GqlResponseLike, HttpHandlerOptions, type Resolver, createFetchHandler, createHttpHandler, createResolvers, formatContext, normalizeNumericFieldAliases, wrapResolver };
package/lib/index.cjs CHANGED
@@ -19,4 +19,5 @@ exports.createHttpHandler = require_http.createHttpHandler;
19
19
  exports.createResolvers = require_http.createResolvers;
20
20
  exports.createSocketHandler = createSocketHandler;
21
21
  exports.formatContext = require_http.formatContext;
22
+ exports.normalizeNumericFieldAliases = require_http.normalizeNumericFieldAliases;
22
23
  exports.wrapResolver = require_http.wrapResolver;
package/lib/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { GqlRequestLike, GqlResponseLike, HttpHandlerOptions, Resolver, createFetchHandler, createHttpHandler, createResolvers, formatContext, wrapResolver } from "./http.cjs";
1
+ import { GqlRequestLike, GqlResponseLike, HttpHandlerOptions, Resolver, createFetchHandler, createHttpHandler, createResolvers, formatContext, normalizeNumericFieldAliases, wrapResolver } from "./http.cjs";
2
2
  import { TIndexedAccountState, TIndexedAssetState, TIndexedDelegationState, TIndexedFactoryState, TIndexedRollupBlock, TIndexedRollupState, TIndexedStakeState, TIndexedTokenState, TIndexedTransaction } from "@ocap/types";
3
3
  import { Express } from "express";
4
4
  import { Server } from "node:http";
@@ -39,4 +39,4 @@ declare const createSocketHandler: ({
39
39
  wsOptions
40
40
  }: SocketHandlerOptions) => Server;
41
41
  //#endregion
42
- export { GqlRequestLike, GqlResponseLike, HttpHandlerOptions, Resolver, createFetchHandler, createHttpHandler, createResolvers, createSocketHandler, formatContext, wrapResolver };
42
+ export { GqlRequestLike, GqlResponseLike, HttpHandlerOptions, Resolver, createFetchHandler, createHttpHandler, createResolvers, createSocketHandler, formatContext, normalizeNumericFieldAliases, wrapResolver };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.31.3",
6
+ "version": "1.31.5",
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.31.3",
55
- "@ocap/schema": "1.31.3",
54
+ "@arcblock/ws": "1.31.5",
55
+ "@ocap/schema": "1.31.5",
56
56
  "graphql": "^16.13.2",
57
57
  "lodash": "^4.17.23"
58
58
  },