@layerzerolabs/function-pointer 1.2.69 → 1.2.71

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.d.ts CHANGED
@@ -2,7 +2,9 @@ import type { SubtractTuple } from '@layerzerolabs/typescript-utils';
2
2
  import { type TuplePrefixUnion } from '@layerzerolabs/typescript-utils';
3
3
  declare const fpSym: unique symbol;
4
4
  declare const dimSym: unique symbol;
5
+ export declare const FUNCTION_POINTER_MARKER: "__IS_FUNCTION_POINTER";
5
6
  export type FunctionPointer<Fn extends (...args: any[]) => Promise<any> = (...args: any[]) => Promise<any>> = {
7
+ [FUNCTION_POINTER_MARKER]: true;
6
8
  factoryName: string;
7
9
  dimKey: string;
8
10
  methodName: string;
@@ -11,6 +13,7 @@ export type FunctionPointer<Fn extends (...args: any[]) => Promise<any> = (...ar
11
13
  [fpSym]: Fn;
12
14
  };
13
15
  export type DimensionlessFunctionPointer<Fn extends (...args: any[]) => Promise<any> = (...args: any[]) => Promise<any>, Dim = any> = {
16
+ [FUNCTION_POINTER_MARKER]: true;
14
17
  factoryName: string;
15
18
  methodName: string;
16
19
  args: any[];
@@ -23,7 +26,19 @@ export type FunctionPointerInput<Pointer extends FunctionPointer | Dimensionless
23
26
  export type FunctionPointerOutput<Pointer extends FunctionPointer | DimensionlessFunctionPointer> = ReturnType<FunctionPointerUnderlying<Pointer>>;
24
27
  export declare const partiallyApplyFunctionPointer: <const Pointer extends FunctionPointer>(pointer: Pointer) => <const Args extends TuplePrefixUnion<FunctionPointerInput<Pointer>>>(...args: Args) => FunctionPointer<(...args: SubtractTuple<FunctionPointerInput<Pointer>, Args>) => FunctionPointerOutput<Pointer>>;
25
28
  export declare const partiallyApplyDimensionlessFunctionPointer: <const Pointer extends DimensionlessFunctionPointer>(pointer: Pointer) => <const Args extends TuplePrefixUnion<FunctionPointerInput<Pointer>>>(...args: Args) => DimensionlessFunctionPointer<(...args: SubtractTuple<FunctionPointerInput<Pointer>, Args>) => FunctionPointerOutput<Pointer>>;
26
- export declare const isFunctionPointer: (o: any) => o is FunctionPointer;
29
+ export declare const createFunctionPointer: <Fn extends (...args: any[]) => Promise<any> = (...args: any[]) => Promise<any>>({ factoryName, dimKey, methodName, args, }: {
30
+ factoryName: string;
31
+ dimKey: string;
32
+ methodName: string;
33
+ args?: any[];
34
+ }) => FunctionPointer<Fn>;
35
+ export declare const isFunctionPointer: (o: unknown) => o is FunctionPointer;
36
+ export declare function assertFunctionPointer(o: unknown): asserts o is FunctionPointer;
37
+ /**
38
+ * Finds every function pointer in POD, including pointers partially applied
39
+ * @throws on malformed pointers, or the value is cyclic or too deep
40
+ */
41
+ export declare const findFunctionPointers: (value: unknown) => FunctionPointer[];
27
42
  export type DeepFunctionPointers<Fn extends (...args: any[]) => Promise<any> = (...args: any[]) => Promise<any>> = {
28
43
  [key: string]: DeepFunctionPointers<Fn> | FunctionPointer<Fn> | null;
29
44
  };
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AACrE,OAAO,EAAE,KAAK,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AAExE,QAAA,MAAM,KAAK,eAAoB,CAAC;AAChC,QAAA,MAAM,MAAM,eAAqB,CAAC;AAElC,MAAM,MAAM,eAAe,CACvB,EAAE,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,IAC9E;IACA,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IAEnB,IAAI,EAAE,GAAG,EAAE,CAAC;CACf,GAAG;IAGA,CAAC,KAAK,CAAC,EAAE,EAAE,CAAC;CACf,CA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AC/C,QAAQ,CAAC,CAAC,CAAC,SAAS,IAAI,GACtB,IAAI,GACJ,KAAK;CAClB,CAAC;AAEF,eAAO,MAAM,iCAAiC,GAC1C,EAAE,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,EAE9E,sBAAsB,oBAAoB,CAAC,EAAE,CAAC,EAC9C,wBAAwB,CAAC,OAAO,EAAE,eAAe,CAAC,EAAE,CAAC,KAAK,UAAU,CAAC,EAAE,CAAC,0EAsBE,CAAC"}
package/dist/index.js CHANGED
@@ -2,6 +2,7 @@ var __defProp = Object.defineProperty;
2
2
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
3
 
4
4
  // src/index.ts
5
+ var FUNCTION_POINTER_MARKER = "__IS_FUNCTION_POINTER";
5
6
  var partiallyApplyFunctionPointer = /* @__PURE__ */ __name((pointer) => (...args) => ({
6
7
  ...pointer,
7
8
  args: [
@@ -16,7 +17,56 @@ var partiallyApplyDimensionlessFunctionPointer = /* @__PURE__ */ __name((pointer
16
17
  ...args
17
18
  ]
18
19
  }), "partiallyApplyDimensionlessFunctionPointer");
19
- var isFunctionPointer = /* @__PURE__ */ __name((o) => o.args && o.dimKey && o.factoryName && o.methodName, "isFunctionPointer");
20
+ var createFunctionPointer = /* @__PURE__ */ __name(({ factoryName, dimKey, methodName, args = [] }) => ({
21
+ [FUNCTION_POINTER_MARKER]: true,
22
+ factoryName,
23
+ dimKey,
24
+ methodName,
25
+ args
26
+ }), "createFunctionPointer");
27
+ var isFunctionPointer = /* @__PURE__ */ __name((o) => {
28
+ if (typeof o !== "object" || o === null) return false;
29
+ const { [FUNCTION_POINTER_MARKER]: marker, factoryName, dimKey, methodName, args } = o;
30
+ return marker === true && typeof factoryName === "string" && typeof dimKey === "string" && typeof methodName === "string" && Array.isArray(args);
31
+ }, "isFunctionPointer");
32
+ var describeValue = /* @__PURE__ */ __name((o) => {
33
+ try {
34
+ return JSON.stringify(o).slice(0, 200);
35
+ } catch {
36
+ return String(o);
37
+ }
38
+ }, "describeValue");
39
+ function assertFunctionPointer(o) {
40
+ if (!isFunctionPointer(o)) {
41
+ throw new Error(`Expected a function pointer, got ${describeValue(o)}`);
42
+ }
43
+ }
44
+ __name(assertFunctionPointer, "assertFunctionPointer");
45
+ var MAX_FUNCTION_POINTER_SEARCH_DEPTH = 64;
46
+ var findFunctionPointers = /* @__PURE__ */ __name((value) => {
47
+ const pointers = [];
48
+ const ancestors = /* @__PURE__ */ new Set();
49
+ const visit = /* @__PURE__ */ __name((v, depth) => {
50
+ if (typeof v !== "object" || v === null) return;
51
+ if (depth > MAX_FUNCTION_POINTER_SEARCH_DEPTH) {
52
+ throw new Error(`Exceeded depth ${MAX_FUNCTION_POINTER_SEARCH_DEPTH} while searching for function pointers`);
53
+ }
54
+ if (ancestors.has(v)) {
55
+ throw new Error("Cannot search a cyclic value for function pointers");
56
+ }
57
+ if (FUNCTION_POINTER_MARKER in v) {
58
+ assertFunctionPointer(v);
59
+ pointers.push(v);
60
+ }
61
+ ancestors.add(v);
62
+ for (const child of Object.values(v)) {
63
+ visit(child, depth + 1);
64
+ }
65
+ ancestors.delete(v);
66
+ }, "visit");
67
+ visit(value, 0);
68
+ return pointers;
69
+ }, "findFunctionPointers");
20
70
  var deeplyResolveDeepFunctionPointers = /* @__PURE__ */ __name(async (deepFunctionPointers, resolveFunctionPointer) => Object.fromEntries(await Promise.all(Object.entries(deepFunctionPointers).map(async ([k, v]) => {
21
71
  if (v === null) {
22
72
  return [
@@ -36,6 +86,6 @@ var deeplyResolveDeepFunctionPointers = /* @__PURE__ */ __name(async (deepFuncti
36
86
  ];
37
87
  }))), "deeplyResolveDeepFunctionPointers");
38
88
 
39
- export { deeplyResolveDeepFunctionPointers, isFunctionPointer, partiallyApplyDimensionlessFunctionPointer, partiallyApplyFunctionPointer };
89
+ export { FUNCTION_POINTER_MARKER, assertFunctionPointer, createFunctionPointer, deeplyResolveDeepFunctionPointers, findFunctionPointers, isFunctionPointer, partiallyApplyDimensionlessFunctionPointer, partiallyApplyFunctionPointer };
40
90
  //# sourceMappingURL=index.js.map
41
91
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"names":["partiallyApplyFunctionPointer","pointer","args","partiallyApplyDimensionlessFunctionPointer","isFunctionPointer","o","dimKey","factoryName","methodName","deeplyResolveDeepFunctionPointers","deepFunctionPointers","resolveFunctionPointer","Object","fromEntries","Promise","all","entries","map","k","v"],"mappings":";;;;AAgDO,IAAMA,6BAAAA,mBACT,MAAA,CAAA,CAAwCC,OAAAA,KACxC,CAAA,GAAwEC,IAAAA,MACnE;EACG,GAAGD,OAAAA;EACHC,IAAAA,EAAM;OAAID,OAAAA,CAAQC,IAAAA;AAASA,IAAAA,GAAAA;;AAC/B,CAAA,CAAA,EALJ,+BAAA;AAWG,IAAMC,0CAAAA,mBACT,MAAA,CAAA,CAAqDF,OAAAA,KACrD,CAAA,GAAwEC,IAAAA,MACnE;EACG,GAAGD,OAAAA;EACHC,IAAAA,EAAM;OAAID,OAAAA,CAAQC,IAAAA;AAASA,IAAAA,GAAAA;;AAC/B,CAAA,CAAA,EALJ,4CAAA;AAWG,IAAME,iBAAAA,mBAAoB,MAAA,CAAA,CAACC,CAAAA,KAC9BA,CAAAA,CAAEH,IAAAA,IAAQG,EAAEC,MAAAA,IAAUD,CAAAA,CAAEE,WAAAA,IAAeF,CAAAA,CAAEG,UAAAA,EADZ,mBAAA;AAmB1B,IAAMC,oDAAoC,MAAA,CAAA,OAG7CC,oBAAAA,EACAC,2BAEAC,MAAAA,CAAOC,WAAAA,CACH,MAAMC,OAAAA,CAAQC,GAAAA,CACVH,MAAAA,CAAOI,OAAAA,CAAQN,oBAAAA,CAAAA,CAAsBO,GAAAA,CAAI,OAAO,CAACC,CAAAA,EAAGC,CAAAA,CAAAA,KAAE;AAClD,EAAA,IAAIA,MAAM,IAAA,EAAM;AACZ,IAAA,OAAO;AAACD,MAAAA,CAAAA;AAAGC,MAAAA;;AACf,EAAA;AAEA,EAAA,IAAIf,iBAAAA,CAAkBe,CAAAA,CAAAA,EAAI;AACtB,IAAA,OAAO;AAACD,MAAAA,CAAAA;AAAG,MAAA,MAAMP,uBAAuBQ,CAAAA;;AAC5C,EAAA;AAEA,EAAA,OAAO;AACHD,IAAAA,CAAAA;IACA,MAAMT,iCAAAA,CACFU,GACAR,sBAAAA;;AAGZ,CAAA,CAAA,CAAA,CAAA,EAxBqC,mCAAA","file":"index.js","sourcesContent":["import type { SubtractTuple } from '@layerzerolabs/typescript-utils';\nimport { type TuplePrefixUnion } from '@layerzerolabs/typescript-utils';\n\nconst fpSym = Symbol('_fp_tag');\nconst dimSym = Symbol('_dim_tag');\n\nexport type FunctionPointer<\n Fn extends (...args: any[]) => Promise<any> = (...args: any[]) => Promise<any>,\n> = {\n factoryName: string;\n dimKey: string;\n methodName: string;\n // an improper subset of the arguments expected by the method this points to\n args: any[];\n} & {\n // we do it this way to preserve contravariance of the input type\n // AND to brand the pointer\n [fpSym]: Fn;\n};\n\nexport type DimensionlessFunctionPointer<\n Fn extends (...args: any[]) => Promise<any> = (...args: any[]) => Promise<any>,\n Dim = any,\n> = {\n factoryName: string;\n methodName: string;\n args: any[];\n} & {\n [fpSym]: Fn;\n [dimSym]: Dim;\n};\n\nexport type FunctionPointerUnderlying<\n Pointer extends FunctionPointer | DimensionlessFunctionPointer,\n> =\n Pointer extends FunctionPointer<infer Fn>\n ? Fn\n : Pointer extends DimensionlessFunctionPointer<infer Fn>\n ? Fn\n : never;\n\nexport type FunctionPointerInput<Pointer extends FunctionPointer | DimensionlessFunctionPointer> =\n Parameters<FunctionPointerUnderlying<Pointer>>;\n\nexport type FunctionPointerOutput<Pointer extends FunctionPointer | DimensionlessFunctionPointer> =\n ReturnType<FunctionPointerUnderlying<Pointer>>;\n\n// Does not and cannot preserve generic functions\nexport const partiallyApplyFunctionPointer =\n <const Pointer extends FunctionPointer>(pointer: Pointer) =>\n <const Args extends TuplePrefixUnion<FunctionPointerInput<Pointer>>>(...args: Args) =>\n ({\n ...pointer,\n args: [...pointer.args, ...args],\n }) as unknown as FunctionPointer<\n (\n ...args: SubtractTuple<FunctionPointerInput<Pointer>, Args>\n ) => FunctionPointerOutput<Pointer>\n >;\n\nexport const partiallyApplyDimensionlessFunctionPointer =\n <const Pointer extends DimensionlessFunctionPointer>(pointer: Pointer) =>\n <const Args extends TuplePrefixUnion<FunctionPointerInput<Pointer>>>(...args: Args) =>\n ({\n ...pointer,\n args: [...pointer.args, ...args],\n }) as unknown as DimensionlessFunctionPointer<\n (\n ...args: SubtractTuple<FunctionPointerInput<Pointer>, Args>\n ) => FunctionPointerOutput<Pointer>\n >;\n\nexport const isFunctionPointer = (o: any): o is FunctionPointer =>\n o.args && o.dimKey && o.factoryName && o.methodName;\n\nexport type DeepFunctionPointers<\n Fn extends (...args: any[]) => Promise<any> = (...args: any[]) => Promise<any>,\n> = {\n [key: string]: DeepFunctionPointers<Fn> | FunctionPointer<Fn> | null;\n};\n\nexport type DeeplyResolvedDeepFunctionPointers<Pointers extends DeepFunctionPointers> = {\n [K in keyof Pointers]: Pointers[K] extends FunctionPointer\n ? Awaited<FunctionPointerOutput<Pointers[K]>>\n : Pointers[K] extends DeepFunctionPointers\n ? DeeplyResolvedDeepFunctionPointers<Pointers[K]>\n : Pointers[K] extends null\n ? null\n : never;\n};\n\nexport const deeplyResolveDeepFunctionPointers = async <\n Fn extends (...args: any[]) => Promise<any> = (...args: any[]) => Promise<any>,\n>(\n deepFunctionPointers: DeepFunctionPointers<Fn>,\n resolveFunctionPointer: (pointer: FunctionPointer<Fn>) => ReturnType<Fn>,\n) =>\n Object.fromEntries(\n await Promise.all(\n Object.entries(deepFunctionPointers).map(async ([k, v]): Promise<any> => {\n if (v === null) {\n return [k, v];\n }\n // this is kind of hacky but if this case occurs we have bigger problems\n if (isFunctionPointer(v)) {\n return [k, await resolveFunctionPointer(v as FunctionPointer<Fn>)];\n }\n\n return [\n k,\n await deeplyResolveDeepFunctionPointers(\n v as DeepFunctionPointers<Fn>,\n resolveFunctionPointer,\n ),\n ];\n }),\n ),\n ) as Promise<DeeplyResolvedDeepFunctionPointers<DeepFunctionPointers<Fn>>>;\n"]}
1
+ {"version":3,"sources":["../src/index.ts"],"names":["FUNCTION_POINTER_MARKER","partiallyApplyFunctionPointer","pointer","args","partiallyApplyDimensionlessFunctionPointer","createFunctionPointer","factoryName","dimKey","methodName","isFunctionPointer","o","marker","Array","isArray","describeValue","JSON","stringify","slice","String","assertFunctionPointer","Error","MAX_FUNCTION_POINTER_SEARCH_DEPTH","findFunctionPointers","value","pointers","ancestors","Set","visit","v","depth","has","push","add","child","Object","values","delete","deeplyResolveDeepFunctionPointers","deepFunctionPointers","resolveFunctionPointer","fromEntries","Promise","all","entries","map","k"],"mappings":";;;;AAOO,IAAMA,uBAAAA,GAA0B;AA8ChC,IAAMC,6BAAAA,mBACT,MAAA,CAAA,CAAwCC,OAAAA,KACxC,CAAA,GAAwEC,IAAAA,MACnE;EACG,GAAGD,OAAAA;EACHC,IAAAA,EAAM;OAAID,OAAAA,CAAQC,IAAAA;AAASA,IAAAA,GAAAA;;AAC/B,CAAA,CAAA,EALJ,+BAAA;AAWG,IAAMC,0CAAAA,mBACT,MAAA,CAAA,CAAqDF,OAAAA,KACrD,CAAA,GAAwEC,IAAAA,MACnE;EACG,GAAGD,OAAAA;EACHC,IAAAA,EAAM;OAAID,OAAAA,CAAQC,IAAAA;AAASA,IAAAA,GAAAA;;AAC/B,CAAA,CAAA,EALJ,4CAAA;AAWG,IAAME,qBAAAA,2BAEX,EACEC,WAAAA,EACAC,QACAC,UAAAA,EACAL,IAAAA,GAAO,EAAA,EAAE,MAOR;AACG,EAAA,CAACH,uBAAAA,GAA0B,IAAA;AAC3BM,EAAAA,WAAAA;AACAC,EAAAA,MAAAA;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;AAClD,EAAA,IAAIA,MAAM,IAAA,EAAM;AACZ,IAAA,OAAO;AAACiB,MAAAA,CAAAA;AAAGjB,MAAAA;;AACf,EAAA;AAEA,EAAA,IAAInB,iBAAAA,CAAkBmB,CAAAA,CAAAA,EAAI;AACtB,IAAA,OAAO;AAACiB,MAAAA,CAAAA;AAAG,MAAA,MAAMN,uBAAuBX,CAAAA;;AAC5C,EAAA;AAEA,EAAA,OAAO;AACHiB,IAAAA,CAAAA;IACA,MAAMR,iCAAAA,CACFT,GACAW,sBAAAA;;AAGZ,CAAA,CAAA,CAAA,CAAA,EAxBqC,mCAAA","file":"index.js","sourcesContent":["import type { SubtractTuple } from '@layerzerolabs/typescript-utils';\nimport { type TuplePrefixUnion } from '@layerzerolabs/typescript-utils';\n\nconst fpSym = Symbol('_fp_tag');\nconst dimSym = Symbol('_dim_tag');\n\n// this is the runtime equivalent of the `fpSym` brand, it's exported so that it can be used for runtime validation\nexport const FUNCTION_POINTER_MARKER = '__IS_FUNCTION_POINTER' as const;\n\nexport type FunctionPointer<\n Fn extends (...args: any[]) => Promise<any> = (...args: any[]) => Promise<any>,\n> = {\n [FUNCTION_POINTER_MARKER]: true;\n factoryName: string;\n dimKey: string;\n methodName: string;\n // an improper subset of the arguments expected by the method this points to\n args: any[];\n} & {\n // we do it this way to preserve contravariance of the input type\n // AND to brand the pointer\n [fpSym]: Fn;\n};\n\nexport type DimensionlessFunctionPointer<\n Fn extends (...args: any[]) => Promise<any> = (...args: any[]) => Promise<any>,\n Dim = any,\n> = {\n [FUNCTION_POINTER_MARKER]: true;\n factoryName: string;\n methodName: string;\n args: any[];\n} & {\n [fpSym]: Fn;\n [dimSym]: Dim;\n};\n\nexport type FunctionPointerUnderlying<\n Pointer extends FunctionPointer | DimensionlessFunctionPointer,\n> =\n Pointer extends FunctionPointer<infer Fn>\n ? Fn\n : Pointer extends DimensionlessFunctionPointer<infer Fn>\n ? Fn\n : never;\n\nexport type FunctionPointerInput<Pointer extends FunctionPointer | DimensionlessFunctionPointer> =\n Parameters<FunctionPointerUnderlying<Pointer>>;\n\nexport type FunctionPointerOutput<Pointer extends FunctionPointer | DimensionlessFunctionPointer> =\n ReturnType<FunctionPointerUnderlying<Pointer>>;\n\n// Does not and cannot preserve generic functions\nexport const partiallyApplyFunctionPointer =\n <const Pointer extends FunctionPointer>(pointer: Pointer) =>\n <const Args extends TuplePrefixUnion<FunctionPointerInput<Pointer>>>(...args: Args) =>\n ({\n ...pointer,\n args: [...pointer.args, ...args],\n }) as unknown as FunctionPointer<\n (\n ...args: SubtractTuple<FunctionPointerInput<Pointer>, Args>\n ) => FunctionPointerOutput<Pointer>\n >;\n\nexport const partiallyApplyDimensionlessFunctionPointer =\n <const Pointer extends DimensionlessFunctionPointer>(pointer: Pointer) =>\n <const Args extends TuplePrefixUnion<FunctionPointerInput<Pointer>>>(...args: Args) =>\n ({\n ...pointer,\n args: [...pointer.args, ...args],\n }) as unknown as DimensionlessFunctionPointer<\n (\n ...args: SubtractTuple<FunctionPointerInput<Pointer>, Args>\n ) => FunctionPointerOutput<Pointer>\n >;\n\nexport const createFunctionPointer = <\n Fn extends (...args: any[]) => Promise<any> = (...args: any[]) => Promise<any>,\n>({\n factoryName,\n dimKey,\n methodName,\n args = [],\n}: {\n factoryName: string;\n dimKey: string;\n methodName: string;\n args?: any[];\n}): FunctionPointer<Fn> =>\n ({\n [FUNCTION_POINTER_MARKER]: true,\n factoryName,\n dimKey,\n methodName,\n args,\n }) as FunctionPointer<Fn>;\n\nexport const isFunctionPointer = (o: unknown): o is FunctionPointer => {\n if (typeof o !== 'object' || o === null) return false;\n const {\n [FUNCTION_POINTER_MARKER]: marker,\n factoryName,\n dimKey,\n methodName,\n args,\n } = o as Record<string, unknown>;\n return (\n marker === true &&\n typeof factoryName === 'string' &&\n typeof dimKey === 'string' &&\n typeof methodName === 'string' &&\n Array.isArray(args)\n );\n};\n\nconst describeValue = (o: unknown) => {\n try {\n return JSON.stringify(o).slice(0, 200);\n } catch {\n return String(o);\n }\n};\n\nexport function assertFunctionPointer(o: unknown): asserts o is FunctionPointer {\n if (!isFunctionPointer(o)) {\n throw new Error(`Expected a function pointer, got ${describeValue(o)}`);\n }\n}\n\nconst MAX_FUNCTION_POINTER_SEARCH_DEPTH = 64;\n\n/**\n * Finds every function pointer in POD, including pointers partially applied\n * @throws on malformed pointers, or the value is cyclic or too deep\n */\nexport const findFunctionPointers = (value: unknown): FunctionPointer[] => {\n const pointers: FunctionPointer[] = [];\n const ancestors = new Set<object>();\n\n const visit = (v: unknown, depth: number) => {\n if (typeof v !== 'object' || v === null) return;\n if (depth > MAX_FUNCTION_POINTER_SEARCH_DEPTH) {\n throw new Error(\n `Exceeded depth ${MAX_FUNCTION_POINTER_SEARCH_DEPTH} while searching for function pointers`,\n );\n }\n if (ancestors.has(v)) {\n throw new Error('Cannot search a cyclic value for function pointers');\n }\n\n if (FUNCTION_POINTER_MARKER in v) {\n assertFunctionPointer(v);\n pointers.push(v);\n }\n\n ancestors.add(v);\n for (const child of Object.values(v)) {\n visit(child, depth + 1);\n }\n ancestors.delete(v);\n };\n\n visit(value, 0);\n return pointers;\n};\n\nexport type DeepFunctionPointers<\n Fn extends (...args: any[]) => Promise<any> = (...args: any[]) => Promise<any>,\n> = {\n [key: string]: DeepFunctionPointers<Fn> | FunctionPointer<Fn> | null;\n};\n\nexport type DeeplyResolvedDeepFunctionPointers<Pointers extends DeepFunctionPointers> = {\n [K in keyof Pointers]: Pointers[K] extends FunctionPointer\n ? Awaited<FunctionPointerOutput<Pointers[K]>>\n : Pointers[K] extends DeepFunctionPointers\n ? DeeplyResolvedDeepFunctionPointers<Pointers[K]>\n : Pointers[K] extends null\n ? null\n : never;\n};\n\nexport const deeplyResolveDeepFunctionPointers = async <\n Fn extends (...args: any[]) => Promise<any> = (...args: any[]) => Promise<any>,\n>(\n deepFunctionPointers: DeepFunctionPointers<Fn>,\n resolveFunctionPointer: (pointer: FunctionPointer<Fn>) => ReturnType<Fn>,\n) =>\n Object.fromEntries(\n await Promise.all(\n Object.entries(deepFunctionPointers).map(async ([k, v]): Promise<any> => {\n if (v === null) {\n return [k, v];\n }\n // this is kind of hacky but if this case occurs we have bigger problems\n if (isFunctionPointer(v)) {\n return [k, await resolveFunctionPointer(v as FunctionPointer<Fn>)];\n }\n\n return [\n k,\n await deeplyResolveDeepFunctionPointers(\n v as DeepFunctionPointers<Fn>,\n resolveFunctionPointer,\n ),\n ];\n }),\n ),\n ) as Promise<DeeplyResolvedDeepFunctionPointers<DeepFunctionPointers<Fn>>>;\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@layerzerolabs/function-pointer",
3
- "version": "1.2.69",
3
+ "version": "1.2.71",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -16,11 +16,11 @@
16
16
  "dist/**/*"
17
17
  ],
18
18
  "dependencies": {
19
- "@layerzerolabs/typescript-utils": "1.2.69"
19
+ "@layerzerolabs/typescript-utils": "1.2.71"
20
20
  },
21
21
  "devDependencies": {
22
- "@layerzerolabs/tsup-configuration": "1.2.69",
23
- "@layerzerolabs/typescript-configuration": "1.2.69",
22
+ "@layerzerolabs/tsup-configuration": "1.2.71",
23
+ "@layerzerolabs/typescript-configuration": "1.2.71",
24
24
  "tsup": "^8.4.0",
25
25
  "vitest": "^4.1.10"
26
26
  },