@degenerate/generic 0.6.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/errors/index.d.ts +1 -0
- package/dist/errors/index.d.ts.map +1 -1
- package/dist/errors/index.js +1 -0
- package/dist/errors/library/library-errors.d.ts +101 -0
- package/dist/errors/library/library-errors.d.ts.map +1 -0
- package/dist/errors/library/library-errors.js +133 -0
- package/dist/errors/share-data/share-data-errors.d.ts +21 -0
- package/dist/errors/share-data/share-data-errors.d.ts.map +1 -1
- package/dist/errors/share-data/share-data-errors.js +30 -1
- package/dist/helpers/apply-share-function.d.ts +23 -0
- package/dist/helpers/apply-share-function.d.ts.map +1 -0
- package/dist/helpers/apply-share-function.js +32 -0
- package/dist/helpers/index.d.ts +1 -0
- package/dist/helpers/index.d.ts.map +1 -1
- package/dist/helpers/index.js +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/library/authorize-library.d.ts +17 -0
- package/dist/library/authorize-library.d.ts.map +1 -0
- package/dist/library/authorize-library.js +39 -0
- package/dist/library/build-library.d.ts +49 -0
- package/dist/library/build-library.d.ts.map +1 -0
- package/dist/library/build-library.js +119 -0
- package/dist/library/caching-library-store.d.ts +19 -0
- package/dist/library/caching-library-store.d.ts.map +1 -0
- package/dist/library/caching-library-store.js +56 -0
- package/dist/library/canonical-json.d.ts +12 -0
- package/dist/library/canonical-json.d.ts.map +1 -0
- package/dist/library/canonical-json.js +30 -0
- package/dist/library/compile-rule-source.d.ts +21 -0
- package/dist/library/compile-rule-source.d.ts.map +1 -0
- package/dist/library/compile-rule-source.js +91 -0
- package/dist/library/evaluate-library-rules.d.ts +36 -0
- package/dist/library/evaluate-library-rules.d.ts.map +1 -0
- package/dist/library/evaluate-library-rules.js +47 -0
- package/dist/library/evaluate-library.d.ts +34 -0
- package/dist/library/evaluate-library.d.ts.map +1 -0
- package/dist/library/evaluate-library.js +66 -0
- package/dist/library/in-memory-library-store.d.ts +22 -0
- package/dist/library/in-memory-library-store.d.ts.map +1 -0
- package/dist/library/in-memory-library-store.js +92 -0
- package/dist/library/index.d.ts +18 -0
- package/dist/library/index.d.ts.map +1 -0
- package/dist/library/index.js +33 -0
- package/dist/library/library-hash.d.ts +9 -0
- package/dist/library/library-hash.d.ts.map +1 -0
- package/dist/library/library-hash.js +14 -0
- package/dist/library/library-models.d.ts +134 -0
- package/dist/library/library-models.d.ts.map +1 -0
- package/dist/library/library-models.js +84 -0
- package/dist/library/library-proof.d.ts +40 -0
- package/dist/library/library-proof.d.ts.map +1 -0
- package/dist/library/library-proof.js +44 -0
- package/dist/library/library-reference.d.ts +75 -0
- package/dist/library/library-reference.d.ts.map +1 -0
- package/dist/library/library-reference.js +98 -0
- package/dist/library/library-server.d.ts +17 -0
- package/dist/library/library-server.d.ts.map +1 -0
- package/dist/library/library-server.js +45 -0
- package/dist/library/library-store.d.ts +37 -0
- package/dist/library/library-store.d.ts.map +1 -0
- package/dist/library/library-store.js +2 -0
- package/dist/library/remote-library-models.d.ts +135 -0
- package/dist/library/remote-library-models.d.ts.map +1 -0
- package/dist/library/remote-library-models.js +26 -0
- package/dist/library/remote-library-store.d.ts +15 -0
- package/dist/library/remote-library-store.d.ts.map +1 -0
- package/dist/library/remote-library-store.js +59 -0
- package/dist/library/verify-library.d.ts +21 -0
- package/dist/library/verify-library.d.ts.map +1 -0
- package/dist/library/verify-library.js +86 -0
- package/dist/models/functions/share-data-function-models.d.ts +6 -74
- package/dist/models/functions/share-data-function-models.d.ts.map +1 -1
- package/dist/models/functions/share-data-function-models.js +7 -65
- package/dist/models/share-data/share-data-models.d.ts +12 -0
- package/dist/models/share-data/share-data-models.d.ts.map +1 -1
- package/dist/models/shareblock/shareblock-models.js +6 -6
- package/dist/sandbox/in-process-executor.d.ts +13 -0
- package/dist/sandbox/in-process-executor.d.ts.map +1 -0
- package/dist/sandbox/in-process-executor.js +31 -0
- package/dist/sandbox/index.d.ts +8 -0
- package/dist/sandbox/index.d.ts.map +1 -0
- package/dist/sandbox/index.js +23 -0
- package/dist/sandbox/remote-sandbox-executor.d.ts +12 -0
- package/dist/sandbox/remote-sandbox-executor.d.ts.map +1 -0
- package/dist/sandbox/remote-sandbox-executor.js +49 -0
- package/dist/sandbox/remote-sandbox-models.d.ts +37 -0
- package/dist/sandbox/remote-sandbox-models.d.ts.map +1 -0
- package/dist/sandbox/remote-sandbox-models.js +2 -0
- package/dist/sandbox/rule-evaluator.d.ts +32 -0
- package/dist/sandbox/rule-evaluator.d.ts.map +1 -0
- package/dist/sandbox/rule-evaluator.js +23 -0
- package/dist/sandbox/run-sandboxed.d.ts +24 -0
- package/dist/sandbox/run-sandboxed.d.ts.map +1 -0
- package/dist/sandbox/run-sandboxed.js +88 -0
- package/dist/sandbox/sandbox-models.d.ts +40 -0
- package/dist/sandbox/sandbox-models.d.ts.map +1 -0
- package/dist/sandbox/sandbox-models.js +2 -0
- package/dist/sandbox/sandbox-server.d.ts +22 -0
- package/dist/sandbox/sandbox-server.d.ts.map +1 -0
- package/dist/sandbox/sandbox-server.js +62 -0
- package/package.json +2 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"share-data-function-models.d.ts","sourceRoot":"","sources":["../../../src/models/functions/share-data-function-models.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB
|
|
1
|
+
{"version":3,"file":"share-data-function-models.d.ts","sourceRoot":"","sources":["../../../src/models/functions/share-data-function-models.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB;;;;;;;GAOG;AAKH;;GAEG;AACH,oBAAY,iBAAiB;IACzB,uBAAuB,4BAA4B;CACtD;AAED,eAAO,MAAM,uBAAuB;;EAElC,CAAC;AAIH;;GAEG;AACH,eAAO,MAAM,mBAAmB,wCAAoC,CAAC;AACrE,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAEpD;;GAEG;AACH,eAAO,MAAM,kCAAkC;;;iBAG7C,CAAC;AAEH,MAAM,MAAM,4BAA4B,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,kCAAkC,CAAC,CAAC;AAE9F;;GAEG;AACH,eAAO,MAAM,mBAAmB;;;iBAAqC,CAAC;AACtE,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,mBAAmB,CAAC,CAAC;AAEhE;;GAEG;AACH,eAAO,MAAM,6BAA6B;;;;;iBAKxC,CAAC;AAEH,MAAM,MAAM,uBAAuB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,6BAA6B,CAAC,CAAC;AAEpF;;GAEG;AACH,eAAO,MAAM,wBAAwB;;;;;;;;;;;;iBAInC,CAAC;AAEH,MAAM,MAAM,kBAAkB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,wBAAwB,CAAC,CAAC"}
|
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.IShareDataFunctionSchema = exports.IShareDataFunctionBasicSchema = exports.INetworkProofSchema = exports.ICryptographicSignatureProofSchema = exports.ITargetObjectSchema = exports.INetworkProofTypeSchema = exports.INetworkProofType = void 0;
|
|
4
4
|
const zod_1 = require("zod");
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* Function record + proof types still referenced by the runtime (the rules
|
|
7
|
+
* engine's `ITargetObject` and the `IShareDataFunction` shape used by
|
|
8
|
+
* `share-data-errors`). The earlier network-function/rule "logic library"
|
|
9
|
+
* sketches (`IShareDataNetworkFunction` / `IShareDataNetworkRule` / …) were
|
|
10
|
+
* removed — the live, signed, versioned library entities in
|
|
11
|
+
* `@degenerate/generic`'s `library/` module supersede them.
|
|
7
12
|
*/
|
|
8
13
|
//#region enum
|
|
9
14
|
/**
|
|
@@ -41,19 +46,6 @@ exports.IShareDataFunctionBasicSchema = zod_1.z.object({
|
|
|
41
46
|
stringifiedFunction: zod_1.z.string(), // the stringified function
|
|
42
47
|
currentHash: zod_1.z.string(), // the hash of the stringified function + previous hash
|
|
43
48
|
});
|
|
44
|
-
// const shareDataFunction: IShareDataFunction = {
|
|
45
|
-
// createdAt: 1234567890,
|
|
46
|
-
// previousHash: 'example-previous-hash',
|
|
47
|
-
// stringifiedFunction: 'example-stringified-function',
|
|
48
|
-
// currentHash: 'example-current-hash',
|
|
49
|
-
// // Properties specific to IShareDataFunction
|
|
50
|
-
// proof: {
|
|
51
|
-
// signature: 'example-signature',
|
|
52
|
-
// publicKey: 'example-public-key',
|
|
53
|
-
// // Add other properties of ICryptographicSignatureProof as needed
|
|
54
|
-
// },
|
|
55
|
-
// proofType: INetworkProofType.CRYPTOGRAPHIC_SIGNATURE,
|
|
56
|
-
// };
|
|
57
49
|
/**
|
|
58
50
|
* This contains a share data function
|
|
59
51
|
*/
|
|
@@ -62,53 +54,3 @@ exports.IShareDataFunctionSchema = zod_1.z.object({
|
|
|
62
54
|
proof: exports.INetworkProofSchema, // the proof that the function is valid
|
|
63
55
|
proofType: exports.INetworkProofTypeSchema, // the type of proof
|
|
64
56
|
});
|
|
65
|
-
/**
|
|
66
|
-
* This contains a share data network function
|
|
67
|
-
*/
|
|
68
|
-
exports.IShareDataNetworkFunctionSchema = zod_1.z.object({
|
|
69
|
-
id: zod_1.z.string(), // the id of the function (this will be the original hex representation of the following fields)
|
|
70
|
-
name: zod_1.z.string(), // the name of the function
|
|
71
|
-
description: zod_1.z.string(), // the description of the function
|
|
72
|
-
shareDataFunctions: zod_1.z.array(exports.IShareDataFunctionSchema), // the stringified function
|
|
73
|
-
proof: exports.INetworkProofSchema, // the proof that the function is valid
|
|
74
|
-
proofType: exports.INetworkProofTypeSchema, // the type of proof
|
|
75
|
-
});
|
|
76
|
-
/**
|
|
77
|
-
* This contains the data for a share data network function descriptor.
|
|
78
|
-
*/
|
|
79
|
-
exports.IShareDataNetworkFunctionDescriptorSchema = zod_1.z.object({
|
|
80
|
-
functionNumber: zod_1.z.number(), // the number of the function in the list
|
|
81
|
-
name: zod_1.z.string(), // the name of the function
|
|
82
|
-
description: zod_1.z.string(), // the description of the function
|
|
83
|
-
});
|
|
84
|
-
/**
|
|
85
|
-
* This contains the data for a share data network rule that is the basic shape.
|
|
86
|
-
*/
|
|
87
|
-
exports.IShareDataRuleBasicSchema = zod_1.z.object({
|
|
88
|
-
createdAt: zod_1.z.number(), // the time the rule was created (in milliseconds)
|
|
89
|
-
previousHash: zod_1.z.string(), // the previous hash of the rule
|
|
90
|
-
stringifiedRule: zod_1.z.string(), // the stringified rule
|
|
91
|
-
currentHash: zod_1.z.string(), // the hash of the stringified rule + previous hash
|
|
92
|
-
});
|
|
93
|
-
/**
|
|
94
|
-
* This contains the interface for a share function record in the share data object
|
|
95
|
-
*/
|
|
96
|
-
exports.IShareDataNetworkRuleSchema = zod_1.z.object({
|
|
97
|
-
id: zod_1.z.string(), // the id of the rule (this will be the original hex representation of the following fields)
|
|
98
|
-
shareDataRules: zod_1.z.array(exports.IShareDataRuleBasicSchema), // the stringified rule
|
|
99
|
-
applicationId: zod_1.z.string(), // the application id that owns the rule
|
|
100
|
-
applicationIdProof: zod_1.z.string(), // the proof that the application id owns the rule
|
|
101
|
-
});
|
|
102
|
-
/**
|
|
103
|
-
* This contains the data for a share data network rule.
|
|
104
|
-
*/
|
|
105
|
-
exports.IShareDataRuleSchema = zod_1.z.object({
|
|
106
|
-
createdAt: zod_1.z.number(), // the time the rule was created (in milliseconds)
|
|
107
|
-
previousHash: zod_1.z.string(), // the previous hash of the rule
|
|
108
|
-
stringifiedRule: zod_1.z.string(), // the stringified rule
|
|
109
|
-
currentHash: zod_1.z.string(), // the hash of the stringified rule + previous hash
|
|
110
|
-
});
|
|
111
|
-
/// Use of user defined function in the share data
|
|
112
|
-
/// Storage / Retrieval of the user defined functions in the network. (logic library)
|
|
113
|
-
// Use of user defined rules in the share data (product)
|
|
114
|
-
// Storage / Retrieval of the user defined rules in the network. (logic library)
|
|
@@ -60,6 +60,18 @@ export interface IShareFunction<TInput, TFunction extends (...args: any[]) => an
|
|
|
60
60
|
input: TInput;
|
|
61
61
|
func: TFunction;
|
|
62
62
|
output: TOutput;
|
|
63
|
+
/**
|
|
64
|
+
* Optional Zod schema for the runtime input (the `input` arm of the
|
|
65
|
+
* `func` argument). When present, the input is validated against it at
|
|
66
|
+
* application time, BEFORE `func` runs; a failure throws
|
|
67
|
+
* {@link ShareFunctionInputInvalidError}. When absent, behavior is
|
|
68
|
+
* unchanged (opt-in, additive).
|
|
69
|
+
*
|
|
70
|
+
* Governs input WELL-FORMEDNESS only (types, ranges, required fields).
|
|
71
|
+
* Stateful legality (membership, role, balance, phase) depends on
|
|
72
|
+
* `current` and stays in `func` / the rule engine.
|
|
73
|
+
*/
|
|
74
|
+
inputSchema?: z.ZodType<unknown>;
|
|
63
75
|
}
|
|
64
76
|
/**
|
|
65
77
|
* This contains the interface for the rule failures in the share data object
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"share-data-models.d.ts","sourceRoot":"","sources":["../../../src/models/share-data/share-data-models.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,KAAK,CAAC;AAOpB,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AAGxE;;GAEG;AACH,oBAAY,cAAc;IACtB,aAAa,kBAAkB;IAC/B,IAAI,SAAS;IACb,QAAQ,aAAa;IACrB,mBAAmB,wBAAwB;IAC3C,IAAI,SAAS;CAChB;AAED,eAAO,MAAM,oBAAoB;;;;;;EAM/B,CAAC;AAMH;;GAEG;AACH,eAAO,MAAM,gBAAgB,oCAAgC,CAAC;AAC9D,MAAM,MAAM,UAAU,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,gBAAgB,CAAC,CAAC;AAE1D;;GAEG;AACH,eAAO,MAAM,0BAA0B;;;;;;;;iBAQrC,CAAC;AAEH,MAAM,MAAM,oBAAoB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,0BAA0B,CAAC,CAAC;AAE9E;;GAEG;AACH,eAAO,MAAM,oBAAoB;;;;iBAI/B,CAAC;AAGH,MAAM,WAAW,cAAc,CAAC,MAAM,EAAE,SAAS,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,EAAE,OAAO;IACtF;;;;;;;;;;;OAWG;IACH,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,SAAS,CAAC;IAChB,MAAM,EAAE,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"share-data-models.d.ts","sourceRoot":"","sources":["../../../src/models/share-data/share-data-models.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,KAAK,CAAC;AAOpB,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AAGxE;;GAEG;AACH,oBAAY,cAAc;IACtB,aAAa,kBAAkB;IAC/B,IAAI,SAAS;IACb,QAAQ,aAAa;IACrB,mBAAmB,wBAAwB;IAC3C,IAAI,SAAS;CAChB;AAED,eAAO,MAAM,oBAAoB;;;;;;EAM/B,CAAC;AAMH;;GAEG;AACH,eAAO,MAAM,gBAAgB,oCAAgC,CAAC;AAC9D,MAAM,MAAM,UAAU,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,gBAAgB,CAAC,CAAC;AAE1D;;GAEG;AACH,eAAO,MAAM,0BAA0B;;;;;;;;iBAQrC,CAAC;AAEH,MAAM,MAAM,oBAAoB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,0BAA0B,CAAC,CAAC;AAE9E;;GAEG;AACH,eAAO,MAAM,oBAAoB;;;;iBAI/B,CAAC;AAGH,MAAM,WAAW,cAAc,CAAC,MAAM,EAAE,SAAS,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,EAAE,OAAO;IACtF;;;;;;;;;;;OAWG;IACH,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,SAAS,CAAC;IAChB,MAAM,EAAE,OAAO,CAAC;IAChB;;;;;;;;;;OAUG;IACH,WAAW,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;CACpC;AAGD;;GAEG;AACH,eAAO,MAAM,4BAA4B;;iBAEvC,CAAC;AAEH,MAAM,MAAM,sBAAsB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,4BAA4B,CAAC,CAAC;AAElF;;GAEG;AACH,eAAO,MAAM,+BAA+B;;;;;;;;;;iBAI1C,CAAC;AAEH,MAAM,MAAM,yBAAyB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,+BAA+B,CAAC,CAAC;AAExF;;;GAGG;AACH,eAAO,MAAM,kCAAkC;;;;;;;;;;;;iBAG7C,CAAC;AAEH;;;;;;;GAOG;AACH,MAAM,MAAM,4BAA4B,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,kCAAkC,CAAC,GAAG;IAC5F,KAAK,CAAC,EAAE,qBAAqB,CAAC;CACjC,CAAC"}
|
|
@@ -316,11 +316,11 @@ exports.IShareBlockInitialStateInputSchema = zod_1.default.object({
|
|
|
316
316
|
*/
|
|
317
317
|
exports.IShareBlockDepositInputSchema = zod_1.default.object({
|
|
318
318
|
userId: zod_1.default.string(),
|
|
319
|
-
amount: zod_1.default.number().positive(),
|
|
319
|
+
amount: zod_1.default.number().positive().finite(),
|
|
320
320
|
});
|
|
321
321
|
exports.IShareBlockWithdrawInputSchema = zod_1.default.object({
|
|
322
322
|
userId: zod_1.default.string(),
|
|
323
|
-
amount: zod_1.default.number().positive(),
|
|
323
|
+
amount: zod_1.default.number().positive().finite(),
|
|
324
324
|
});
|
|
325
325
|
exports.IShareBlockCloseInputSchema = zod_1.default.object({
|
|
326
326
|
userId: zod_1.default.string(), // Who is closing the block — used for audit attribution.
|
|
@@ -356,7 +356,7 @@ exports.IShareBlockRegisterLenderInputSchema = zod_1.default.object({
|
|
|
356
356
|
*/
|
|
357
357
|
exports.IShareBlockFundInputSchema = zod_1.default.object({
|
|
358
358
|
fromBlockId: zod_1.default.string().optional(), // upstream block id when known
|
|
359
|
-
amount: zod_1.default.number().positive(),
|
|
359
|
+
amount: zod_1.default.number().positive().finite(),
|
|
360
360
|
});
|
|
361
361
|
/**
|
|
362
362
|
* Input for the Distribution-specific `disburse` function. `userId` must
|
|
@@ -364,7 +364,7 @@ exports.IShareBlockFundInputSchema = zod_1.default.object({
|
|
|
364
364
|
*/
|
|
365
365
|
exports.IShareBlockDisburseInputSchema = zod_1.default.object({
|
|
366
366
|
userId: zod_1.default.string(),
|
|
367
|
-
amount: zod_1.default.number().positive(),
|
|
367
|
+
amount: zod_1.default.number().positive().finite(),
|
|
368
368
|
});
|
|
369
369
|
/**
|
|
370
370
|
* Input for the Distribution-specific `markFullyDistributed` function.
|
|
@@ -460,8 +460,8 @@ exports.IShareBlockReserveForSettlementInputSchema = zod_1.default.object({
|
|
|
460
460
|
settlementId: zod_1.default.string(),
|
|
461
461
|
direction: exports.SettlementLegDirectionSchema,
|
|
462
462
|
amountCents: zod_1.default.number().int().positive(),
|
|
463
|
-
currency: zod_1.default.string(),
|
|
464
|
-
expectedHash: zod_1.default.string(),
|
|
463
|
+
currency: zod_1.default.string().min(1),
|
|
464
|
+
expectedHash: zod_1.default.string().min(1),
|
|
465
465
|
});
|
|
466
466
|
/**
|
|
467
467
|
* Input for the generic `commitSettlement` function. Phase-2 finalize for one
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { FunctionExecutor } from './sandbox-models';
|
|
2
|
+
/**
|
|
3
|
+
* An in-process {@link FunctionExecutor} over the shared sandbox core
|
|
4
|
+
* ({@link runSandboxed}). It runs `({ current, input }) => state` and requires a
|
|
5
|
+
* plain state object back; a Promise or any non-object is rejected as
|
|
6
|
+
* `invalid-output` (async is not permitted — the timeout cannot bound it and it
|
|
7
|
+
* would break reproducibility).
|
|
8
|
+
*
|
|
9
|
+
* NOT a security sandbox (see {@link runSandboxed}); for trusted standard
|
|
10
|
+
* functions and tests. It enforces the gas ceiling and determinism.
|
|
11
|
+
*/
|
|
12
|
+
export declare const createInProcessFunctionExecutor: () => FunctionExecutor;
|
|
13
|
+
//# sourceMappingURL=in-process-executor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"in-process-executor.d.ts","sourceRoot":"","sources":["../../src/sandbox/in-process-executor.ts"],"names":[],"mappings":"AAEA,OAAO,EAGH,gBAAgB,EACnB,MAAM,kBAAkB,CAAC;AAE1B;;;;;;;;;GASG;AACH,eAAO,MAAM,+BAA+B,QAAO,gBAejD,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createInProcessFunctionExecutor = void 0;
|
|
4
|
+
const run_sandboxed_1 = require("./run-sandboxed");
|
|
5
|
+
/**
|
|
6
|
+
* An in-process {@link FunctionExecutor} over the shared sandbox core
|
|
7
|
+
* ({@link runSandboxed}). It runs `({ current, input }) => state` and requires a
|
|
8
|
+
* plain state object back; a Promise or any non-object is rejected as
|
|
9
|
+
* `invalid-output` (async is not permitted — the timeout cannot bound it and it
|
|
10
|
+
* would break reproducibility).
|
|
11
|
+
*
|
|
12
|
+
* NOT a security sandbox (see {@link runSandboxed}); for trusted standard
|
|
13
|
+
* functions and tests. It enforces the gas ceiling and determinism.
|
|
14
|
+
*/
|
|
15
|
+
const createInProcessFunctionExecutor = () => ({
|
|
16
|
+
async execute(request) {
|
|
17
|
+
const run = (0, run_sandboxed_1.runSandboxed)(request.source, { current: request.current, input: request.input }, request.gasLimit);
|
|
18
|
+
if (!run.ok) {
|
|
19
|
+
return { ok: false, failure: run.failure, error: run.error, gasUsed: run.gasUsed, executionTime: run.executionTime };
|
|
20
|
+
}
|
|
21
|
+
if (!isPlainStateObject(run.value)) {
|
|
22
|
+
return { ok: false, failure: 'invalid-output', gasUsed: run.gasUsed, executionTime: run.executionTime };
|
|
23
|
+
}
|
|
24
|
+
return { ok: true, output: run.value, gasUsed: run.gasUsed, executionTime: run.executionTime };
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
exports.createInProcessFunctionExecutor = createInProcessFunctionExecutor;
|
|
28
|
+
const isPlainStateObject = (value) => value !== null &&
|
|
29
|
+
typeof value === 'object' &&
|
|
30
|
+
!Array.isArray(value) &&
|
|
31
|
+
typeof value.then !== 'function'; // reject Promises (async not allowed)
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from './sandbox-models';
|
|
2
|
+
export * from './run-sandboxed';
|
|
3
|
+
export * from './in-process-executor';
|
|
4
|
+
export * from './rule-evaluator';
|
|
5
|
+
export * from './remote-sandbox-models';
|
|
6
|
+
export * from './remote-sandbox-executor';
|
|
7
|
+
export * from './sandbox-server';
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/sandbox/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,uBAAuB,CAAC;AACtC,cAAc,kBAAkB,CAAC;AACjC,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./sandbox-models"), exports);
|
|
18
|
+
__exportStar(require("./run-sandboxed"), exports);
|
|
19
|
+
__exportStar(require("./in-process-executor"), exports);
|
|
20
|
+
__exportStar(require("./rule-evaluator"), exports);
|
|
21
|
+
__exportStar(require("./remote-sandbox-models"), exports);
|
|
22
|
+
__exportStar(require("./remote-sandbox-executor"), exports);
|
|
23
|
+
__exportStar(require("./sandbox-server"), exports);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { FunctionExecutor } from './sandbox-models';
|
|
2
|
+
import { RuleEvaluator } from './rule-evaluator';
|
|
3
|
+
import { SandboxTransport } from './remote-sandbox-models';
|
|
4
|
+
/**
|
|
5
|
+
* A {@link FunctionExecutor} that runs source on a remote sandbox via a
|
|
6
|
+
* {@link SandboxTransport}. Drop-in interchangeable with the in-process
|
|
7
|
+
* executor — the application path does not know or care which it holds.
|
|
8
|
+
*/
|
|
9
|
+
export declare const createRemoteSandboxFunctionExecutor: (transport: SandboxTransport) => FunctionExecutor;
|
|
10
|
+
/** A {@link RuleEvaluator} that runs rule source on a remote sandbox. */
|
|
11
|
+
export declare const createRemoteSandboxRuleEvaluator: (transport: SandboxTransport) => RuleEvaluator;
|
|
12
|
+
//# sourceMappingURL=remote-sandbox-executor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"remote-sandbox-executor.d.ts","sourceRoot":"","sources":["../../src/sandbox/remote-sandbox-executor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqD,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACvG,OAAO,EAA+C,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAC9F,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAE3D;;;;GAIG;AACH,eAAO,MAAM,mCAAmC,GAAI,WAAW,gBAAgB,KAAG,gBAkBhF,CAAC;AAEH,yEAAyE;AACzE,eAAO,MAAM,gCAAgC,GAAI,WAAW,gBAAgB,KAAG,aAkB7E,CAAC"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createRemoteSandboxRuleEvaluator = exports.createRemoteSandboxFunctionExecutor = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* A {@link FunctionExecutor} that runs source on a remote sandbox via a
|
|
6
|
+
* {@link SandboxTransport}. Drop-in interchangeable with the in-process
|
|
7
|
+
* executor — the application path does not know or care which it holds.
|
|
8
|
+
*/
|
|
9
|
+
const createRemoteSandboxFunctionExecutor = (transport) => ({
|
|
10
|
+
async execute(request) {
|
|
11
|
+
const response = await transport.send({
|
|
12
|
+
kind: 'function',
|
|
13
|
+
source: request.source,
|
|
14
|
+
current: request.current,
|
|
15
|
+
input: request.input,
|
|
16
|
+
gasLimit: request.gasLimit,
|
|
17
|
+
});
|
|
18
|
+
return {
|
|
19
|
+
ok: response.ok,
|
|
20
|
+
output: response.output,
|
|
21
|
+
failure: response.failure,
|
|
22
|
+
error: response.error,
|
|
23
|
+
gasUsed: response.gasUsed,
|
|
24
|
+
executionTime: response.executionTime,
|
|
25
|
+
};
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
exports.createRemoteSandboxFunctionExecutor = createRemoteSandboxFunctionExecutor;
|
|
29
|
+
/** A {@link RuleEvaluator} that runs rule source on a remote sandbox. */
|
|
30
|
+
const createRemoteSandboxRuleEvaluator = (transport) => ({
|
|
31
|
+
async evaluate(request) {
|
|
32
|
+
const response = await transport.send({
|
|
33
|
+
kind: 'rule',
|
|
34
|
+
source: request.source,
|
|
35
|
+
current: request.current,
|
|
36
|
+
proposed: request.proposed,
|
|
37
|
+
gasLimit: request.gasLimit,
|
|
38
|
+
});
|
|
39
|
+
return {
|
|
40
|
+
ok: response.ok,
|
|
41
|
+
passed: response.passed,
|
|
42
|
+
failure: response.failure,
|
|
43
|
+
error: response.error,
|
|
44
|
+
gasUsed: response.gasUsed,
|
|
45
|
+
executionTime: response.executionTime,
|
|
46
|
+
};
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
exports.createRemoteSandboxRuleEvaluator = createRemoteSandboxRuleEvaluator;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { IShareData } from '../models/share-data/share-data-models';
|
|
2
|
+
import { RuleEvaluationFailure } from './rule-evaluator';
|
|
3
|
+
/**
|
|
4
|
+
* A portable-JSON request sent to a sandbox server. Discriminated by `kind`:
|
|
5
|
+
* a function carries `input`, a rule carries `proposed`.
|
|
6
|
+
*/
|
|
7
|
+
export interface RemoteSandboxRequest {
|
|
8
|
+
kind: 'function' | 'rule';
|
|
9
|
+
source: string;
|
|
10
|
+
gasLimit: number;
|
|
11
|
+
current: IShareData;
|
|
12
|
+
input?: unknown;
|
|
13
|
+
proposed?: IShareData;
|
|
14
|
+
}
|
|
15
|
+
/** A portable-JSON response from a sandbox server. */
|
|
16
|
+
export interface RemoteSandboxResponse {
|
|
17
|
+
ok: boolean;
|
|
18
|
+
output?: IShareData;
|
|
19
|
+
passed?: boolean;
|
|
20
|
+
failure?: RuleEvaluationFailure;
|
|
21
|
+
error?: string;
|
|
22
|
+
gasUsed: number;
|
|
23
|
+
executionTime: number;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The pluggable transport (a "port") that carries a request to a remote sandbox
|
|
27
|
+
* and returns its response. The consumer wires this to their HTTP client, so
|
|
28
|
+
* `@degenerate/generic` stays dependency-free — no `http`/`fetch` import here,
|
|
29
|
+
* the same discipline as the realtime Redis port.
|
|
30
|
+
*
|
|
31
|
+
* `send` may reject on a transport-level failure (network, timeout); that
|
|
32
|
+
* propagates to the caller rather than being mapped to an execution failure.
|
|
33
|
+
*/
|
|
34
|
+
export interface SandboxTransport {
|
|
35
|
+
send(request: RemoteSandboxRequest): Promise<RemoteSandboxResponse>;
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=remote-sandbox-models.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"remote-sandbox-models.d.ts","sourceRoot":"","sources":["../../src/sandbox/remote-sandbox-models.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,wCAAwC,CAAC;AACpE,OAAO,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AAEzD;;;GAGG;AACH,MAAM,WAAW,oBAAoB;IACjC,IAAI,EAAE,UAAU,GAAG,MAAM,CAAC;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,UAAU,CAAC;IACpB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,QAAQ,CAAC,EAAE,UAAU,CAAC;CACzB;AAED,sDAAsD;AACtD,MAAM,WAAW,qBAAqB;IAClC,EAAE,EAAE,OAAO,CAAC;IACZ,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,OAAO,CAAC,EAAE,qBAAqB,CAAC;IAChC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,aAAa,EAAE,MAAM,CAAC;CACzB;AAED;;;;;;;;GAQG;AACH,MAAM,WAAW,gBAAgB;IAC7B,IAAI,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAAC;CACvE"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { IShareData } from '../models/share-data/share-data-models';
|
|
2
|
+
/**
|
|
3
|
+
* A request to evaluate a rule's `source` against a proposed transition. A
|
|
4
|
+
* library rule is a pure predicate `({ current, proposed }) => boolean`.
|
|
5
|
+
*/
|
|
6
|
+
export interface RuleEvaluationRequest {
|
|
7
|
+
source: string;
|
|
8
|
+
current: IShareData;
|
|
9
|
+
proposed: IShareData;
|
|
10
|
+
gasLimit: number;
|
|
11
|
+
}
|
|
12
|
+
export type RuleEvaluationFailure = 'gas-exceeded' | 'threw' | 'invalid-output';
|
|
13
|
+
export interface RuleEvaluationResult {
|
|
14
|
+
ok: boolean;
|
|
15
|
+
passed?: boolean;
|
|
16
|
+
failure?: RuleEvaluationFailure;
|
|
17
|
+
error?: string;
|
|
18
|
+
gasUsed: number;
|
|
19
|
+
executionTime: number;
|
|
20
|
+
}
|
|
21
|
+
/** Evaluates rule source out of the host call stack — the rule counterpart of {@link FunctionExecutor}. */
|
|
22
|
+
export interface RuleEvaluator {
|
|
23
|
+
evaluate(request: RuleEvaluationRequest): Promise<RuleEvaluationResult>;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* In-process {@link RuleEvaluator} over the shared sandbox core. Runs
|
|
27
|
+
* `({ current, proposed }) => boolean`; a non-boolean return is rejected as
|
|
28
|
+
* `invalid-output`. Same gas + determinism guarantees (and same non-isolation
|
|
29
|
+
* caveat) as the in-process function executor.
|
|
30
|
+
*/
|
|
31
|
+
export declare const createInProcessRuleEvaluator: () => RuleEvaluator;
|
|
32
|
+
//# sourceMappingURL=rule-evaluator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rule-evaluator.d.ts","sourceRoot":"","sources":["../../src/sandbox/rule-evaluator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,wCAAwC,CAAC;AAGpE;;;GAGG;AACH,MAAM,WAAW,qBAAqB;IAClC,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,UAAU,CAAC;IACpB,QAAQ,EAAE,UAAU,CAAC;IACrB,QAAQ,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,MAAM,qBAAqB,GAAG,cAAc,GAAG,OAAO,GAAG,gBAAgB,CAAC;AAEhF,MAAM,WAAW,oBAAoB;IACjC,EAAE,EAAE,OAAO,CAAC;IACZ,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,OAAO,CAAC,EAAE,qBAAqB,CAAC;IAChC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,aAAa,EAAE,MAAM,CAAC;CACzB;AAED,2GAA2G;AAC3G,MAAM,WAAW,aAAa;IAC1B,QAAQ,CAAC,OAAO,EAAE,qBAAqB,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAAC;CAC3E;AAED;;;;;GAKG;AACH,eAAO,MAAM,4BAA4B,QAAO,aAe9C,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createInProcessRuleEvaluator = void 0;
|
|
4
|
+
const run_sandboxed_1 = require("./run-sandboxed");
|
|
5
|
+
/**
|
|
6
|
+
* In-process {@link RuleEvaluator} over the shared sandbox core. Runs
|
|
7
|
+
* `({ current, proposed }) => boolean`; a non-boolean return is rejected as
|
|
8
|
+
* `invalid-output`. Same gas + determinism guarantees (and same non-isolation
|
|
9
|
+
* caveat) as the in-process function executor.
|
|
10
|
+
*/
|
|
11
|
+
const createInProcessRuleEvaluator = () => ({
|
|
12
|
+
async evaluate(request) {
|
|
13
|
+
const run = (0, run_sandboxed_1.runSandboxed)(request.source, { current: request.current, proposed: request.proposed }, request.gasLimit);
|
|
14
|
+
if (!run.ok) {
|
|
15
|
+
return { ok: false, failure: run.failure, error: run.error, gasUsed: run.gasUsed, executionTime: run.executionTime };
|
|
16
|
+
}
|
|
17
|
+
if (typeof run.value !== 'boolean') {
|
|
18
|
+
return { ok: false, failure: 'invalid-output', gasUsed: run.gasUsed, executionTime: run.executionTime };
|
|
19
|
+
}
|
|
20
|
+
return { ok: true, passed: run.value, gasUsed: run.gasUsed, executionTime: run.executionTime };
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
exports.createInProcessRuleEvaluator = createInProcessRuleEvaluator;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** Raw outcome of running source in the sandbox, before any output typing. */
|
|
2
|
+
export interface SandboxRunResult {
|
|
3
|
+
ok: boolean;
|
|
4
|
+
value?: unknown;
|
|
5
|
+
failure?: 'gas-exceeded' | 'threw';
|
|
6
|
+
error?: string;
|
|
7
|
+
gasUsed: number;
|
|
8
|
+
executionTime: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The shared sandbox primitive: runs `(source)(arg)` in a deterministic,
|
|
12
|
+
* gas-bounded `vm` context and returns the raw return value. Output typing
|
|
13
|
+
* (must-be-a-state-object for functions, must-be-a-boolean for rules) is left to
|
|
14
|
+
* the caller.
|
|
15
|
+
*
|
|
16
|
+
* NOT a security sandbox — `vm` shares the heap. It enforces the gas ceiling
|
|
17
|
+
* (`gasLimit * 100ms`, terminating runaway synchronous code) and determinism
|
|
18
|
+
* (`Date` / `Math.random` neutralised; Node globals are absent). Source must be
|
|
19
|
+
* a pure synchronous `(arg) => result`.
|
|
20
|
+
*/
|
|
21
|
+
export declare const runSandboxed: (source: string, arg: unknown, gasLimit: number) => SandboxRunResult;
|
|
22
|
+
export declare const clone: <T>(value: T) => T;
|
|
23
|
+
export declare const gasFrom: (executionTime: number) => number;
|
|
24
|
+
//# sourceMappingURL=run-sandboxed.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run-sandboxed.d.ts","sourceRoot":"","sources":["../../src/sandbox/run-sandboxed.ts"],"names":[],"mappings":"AAGA,8EAA8E;AAC9E,MAAM,WAAW,gBAAgB;IAC7B,EAAE,EAAE,OAAO,CAAC;IACZ,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC;IACnC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,aAAa,EAAE,MAAM,CAAC;CACzB;AAQD;;;;;;;;;;GAUG;AACH,eAAO,MAAM,YAAY,GAAI,QAAQ,MAAM,EAAE,KAAK,OAAO,EAAE,UAAU,MAAM,KAAG,gBAmB7E,CAAC;AAEF,eAAO,MAAM,KAAK,GAAI,CAAC,EAAE,OAAO,CAAC,KAAG,CAAsC,CAAC;AAE3E,eAAO,MAAM,OAAO,GAAI,eAAe,MAAM,KAAG,MAA6D,CAAC"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.gasFrom = exports.clone = exports.runSandboxed = void 0;
|
|
37
|
+
const vm = __importStar(require("vm"));
|
|
38
|
+
const library_models_1 = require("../library/library-models");
|
|
39
|
+
// Runs inside the context (where the intrinsics live) to remove the
|
|
40
|
+
// non-deterministic ones. `globalThis` resolves to the context's global, so
|
|
41
|
+
// these assignments definitely take effect (unlike pre-seeding the sandbox,
|
|
42
|
+
// which the intrinsics may shadow).
|
|
43
|
+
const DETERMINISM_PRELUDE = 'globalThis.Date=undefined;globalThis.Math.random=undefined;';
|
|
44
|
+
/**
|
|
45
|
+
* The shared sandbox primitive: runs `(source)(arg)` in a deterministic,
|
|
46
|
+
* gas-bounded `vm` context and returns the raw return value. Output typing
|
|
47
|
+
* (must-be-a-state-object for functions, must-be-a-boolean for rules) is left to
|
|
48
|
+
* the caller.
|
|
49
|
+
*
|
|
50
|
+
* NOT a security sandbox — `vm` shares the heap. It enforces the gas ceiling
|
|
51
|
+
* (`gasLimit * 100ms`, terminating runaway synchronous code) and determinism
|
|
52
|
+
* (`Date` / `Math.random` neutralised; Node globals are absent). Source must be
|
|
53
|
+
* a pure synchronous `(arg) => result`.
|
|
54
|
+
*/
|
|
55
|
+
const runSandboxed = (source, arg, gasLimit) => {
|
|
56
|
+
const timeoutMs = gasLimit * library_models_1.GAS_UNIT_MS;
|
|
57
|
+
// Deep-clone the arg into the context so the function cannot mutate the
|
|
58
|
+
// caller's objects, and so nothing leaks in by reference.
|
|
59
|
+
const context = vm.createContext({ __arg: arg === undefined ? undefined : (0, exports.clone)(arg) });
|
|
60
|
+
const code = `${DETERMINISM_PRELUDE}(${source})(__arg)`;
|
|
61
|
+
const startedAt = Date.now();
|
|
62
|
+
try {
|
|
63
|
+
const value = vm.runInContext(code, context, { timeout: timeoutMs });
|
|
64
|
+
const executionTime = Date.now() - startedAt;
|
|
65
|
+
return { ok: true, value, gasUsed: (0, exports.gasFrom)(executionTime), executionTime };
|
|
66
|
+
}
|
|
67
|
+
catch (err) {
|
|
68
|
+
const executionTime = Date.now() - startedAt;
|
|
69
|
+
if (isTimeout(err)) {
|
|
70
|
+
return { ok: false, failure: 'gas-exceeded', gasUsed: gasLimit, executionTime };
|
|
71
|
+
}
|
|
72
|
+
return { ok: false, failure: 'threw', error: messageOf(err), gasUsed: (0, exports.gasFrom)(executionTime), executionTime };
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
exports.runSandboxed = runSandboxed;
|
|
76
|
+
const clone = (value) => JSON.parse(JSON.stringify(value));
|
|
77
|
+
exports.clone = clone;
|
|
78
|
+
const gasFrom = (executionTime) => Math.max(1, Math.ceil(executionTime / library_models_1.GAS_UNIT_MS));
|
|
79
|
+
exports.gasFrom = gasFrom;
|
|
80
|
+
const isTimeout = (err) => {
|
|
81
|
+
if (typeof err !== 'object' || err === null) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
const code = err.code;
|
|
85
|
+
const message = String(err.message ?? '');
|
|
86
|
+
return code === 'ERR_SCRIPT_EXECUTION_TIMEOUT' || /timed out/i.test(message);
|
|
87
|
+
};
|
|
88
|
+
const messageOf = (err) => (err instanceof Error ? err.message : String(err));
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { IShareData } from '../models/share-data/share-data-models';
|
|
2
|
+
/**
|
|
3
|
+
* A request to execute a stored function's `source` against `current` state and
|
|
4
|
+
* an `input`, bounded by `gasLimit`. See `plans/FUNCTION_LEDGER_PLAN.html` and
|
|
5
|
+
* `documentation/functions/functions.md`.
|
|
6
|
+
*/
|
|
7
|
+
export interface FunctionExecutionRequest {
|
|
8
|
+
/** Stringified function source — a pure `({ current, input }) => state`. */
|
|
9
|
+
source: string;
|
|
10
|
+
current: IShareData;
|
|
11
|
+
input: unknown;
|
|
12
|
+
/** Gas units (100ms each). The hard wall-clock ceiling is gasLimit * 100ms. */
|
|
13
|
+
gasLimit: number;
|
|
14
|
+
}
|
|
15
|
+
/** Why an execution did not produce a usable proposed state. */
|
|
16
|
+
export type FunctionExecutionFailure = 'gas-exceeded' | 'threw' | 'invalid-output';
|
|
17
|
+
/**
|
|
18
|
+
* The outcome of an execution. `ok` is true only when a plain proposed-state
|
|
19
|
+
* object was produced; otherwise `failure` says why. `gasUsed` and
|
|
20
|
+
* `executionTime` are always reported so a verifier can reproduce with an
|
|
21
|
+
* adequate budget.
|
|
22
|
+
*/
|
|
23
|
+
export interface FunctionExecutionResult {
|
|
24
|
+
ok: boolean;
|
|
25
|
+
output?: IShareData;
|
|
26
|
+
failure?: FunctionExecutionFailure;
|
|
27
|
+
error?: string;
|
|
28
|
+
gasUsed: number;
|
|
29
|
+
executionTime: number;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Executes stored function source out of the host call stack. Pluggable: an
|
|
33
|
+
* in-process implementation for trusted standard functions and tests, and a
|
|
34
|
+
* remote-sandbox implementation for untrusted library source. `execute` is
|
|
35
|
+
* async so both implementations share one interface.
|
|
36
|
+
*/
|
|
37
|
+
export interface FunctionExecutor {
|
|
38
|
+
execute(request: FunctionExecutionRequest): Promise<FunctionExecutionResult>;
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=sandbox-models.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sandbox-models.d.ts","sourceRoot":"","sources":["../../src/sandbox/sandbox-models.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,wCAAwC,CAAC;AAEpE;;;;GAIG;AACH,MAAM,WAAW,wBAAwB;IACrC,4EAA4E;IAC5E,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,UAAU,CAAC;IACpB,KAAK,EAAE,OAAO,CAAC;IACf,+EAA+E;IAC/E,QAAQ,EAAE,MAAM,CAAC;CACpB;AAED,gEAAgE;AAChE,MAAM,MAAM,wBAAwB,GAC9B,cAAc,GACd,OAAO,GACP,gBAAgB,CAAC;AAEvB;;;;;GAKG;AACH,MAAM,WAAW,uBAAuB;IACpC,EAAE,EAAE,OAAO,CAAC;IACZ,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB,OAAO,CAAC,EAAE,wBAAwB,CAAC;IACnC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,aAAa,EAAE,MAAM,CAAC;CACzB;AAED;;;;;GAKG;AACH,MAAM,WAAW,gBAAgB;IAC7B,OAAO,CAAC,OAAO,EAAE,wBAAwB,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAAC;CAChF"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { RemoteSandboxRequest, RemoteSandboxResponse } from './remote-sandbox-models';
|
|
2
|
+
export interface SandboxServerOptions {
|
|
3
|
+
/**
|
|
4
|
+
* The hard ceiling on gas the server will honour, regardless of what the
|
|
5
|
+
* client requests. Defaults to (and is itself capped at) {@link MAX_GAS_UNITS}.
|
|
6
|
+
*/
|
|
7
|
+
maxGasUnits?: number;
|
|
8
|
+
}
|
|
9
|
+
export interface SandboxServer {
|
|
10
|
+
handle(request: RemoteSandboxRequest): Promise<RemoteSandboxResponse>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The transport-agnostic sandbox server: it receives a {@link RemoteSandboxRequest},
|
|
14
|
+
* runs it through the in-process executor/evaluator, and returns a
|
|
15
|
+
* {@link RemoteSandboxResponse}. The CONSUMER wires this handler to their HTTP
|
|
16
|
+
* server (an Express route, etc.) — the server itself takes no transport dep.
|
|
17
|
+
*
|
|
18
|
+
* The server is the authority on gas: it clamps the requested `gasLimit` to its
|
|
19
|
+
* own cap, so a client cannot ask for more than the server permits.
|
|
20
|
+
*/
|
|
21
|
+
export declare const createSandboxServer: (options?: SandboxServerOptions) => SandboxServer;
|
|
22
|
+
//# sourceMappingURL=sandbox-server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sandbox-server.d.ts","sourceRoot":"","sources":["../../src/sandbox/sandbox-server.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAGtF,MAAM,WAAW,oBAAoB;IACjC;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;CACxB;AAED,MAAM,WAAW,aAAa;IAC1B,MAAM,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAAC;CACzE;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,mBAAmB,GAAI,UAAS,oBAAyB,KAAG,aA0CxE,CAAC"}
|