@nodora/js 0.2.3 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.d.ts +54 -21
- package/lib/index.js +76 -60
- package/lib/nodora.wasm.gz +0 -0
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ export type EvaluationResult = {
|
|
|
9
9
|
};
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
* A compiled Nodora ruleset
|
|
12
|
+
* A compiled Nodora ruleset.
|
|
13
13
|
*/
|
|
14
14
|
export type Ruleset = Record<string, any>;
|
|
15
15
|
|
|
@@ -52,21 +52,6 @@ export interface Evaluator {
|
|
|
52
52
|
destroy(): void;
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
-
/**
|
|
56
|
-
* Create a new evaluator
|
|
57
|
-
* @param ruleset - The compiled ruleset, either as the object returned by
|
|
58
|
-
* compile() or as a JSON string
|
|
59
|
-
* @returns Promise resolving to an Evaluator instance
|
|
60
|
-
*/
|
|
61
|
-
export function createEvaluator(ruleset: Ruleset | string): Promise<Evaluator>;
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Compile a ruleset
|
|
65
|
-
* @param src - Source code
|
|
66
|
-
* @returns Promise resolving to the compiled ruleset
|
|
67
|
-
*/
|
|
68
|
-
export function compile(src: string): Promise<Ruleset>;
|
|
69
|
-
|
|
70
55
|
type BaseType = "string" | "number" | "bool" | "object" | "any";
|
|
71
56
|
type ArrayType = `array<${string}>`;
|
|
72
57
|
type UnionType = `${string}|${string}`;
|
|
@@ -87,9 +72,57 @@ export interface RegisterFunctionOptions {
|
|
|
87
72
|
}
|
|
88
73
|
|
|
89
74
|
/**
|
|
90
|
-
*
|
|
91
|
-
|
|
75
|
+
* A position in the source. Lines start at 1, columns at 0.
|
|
76
|
+
*/
|
|
77
|
+
export interface Position {
|
|
78
|
+
line: number;
|
|
79
|
+
col: number;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Thrown by Engine.compile(). The message includes the positions, with columns
|
|
84
|
+
* starting at 1.
|
|
85
|
+
*/
|
|
86
|
+
export class CompileError extends Error {
|
|
87
|
+
/** Position of a syntax error */
|
|
88
|
+
pos?: Position;
|
|
89
|
+
|
|
90
|
+
/** Semantic errors, in case there are any */
|
|
91
|
+
errors?: { message: string; span?: { start: Position; end: Position } }[];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface Engine {
|
|
95
|
+
/**
|
|
96
|
+
* Register a custom function. Must be called before the rulesets that use it
|
|
97
|
+
* are compiled and evaluated. Built-in functions cannot be replaced.
|
|
98
|
+
* @param options - Function specification and implementation
|
|
99
|
+
* @returns Engine instance for chaining
|
|
100
|
+
*/
|
|
101
|
+
registerFunction(options: RegisterFunctionOptions): Engine;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Compile a ruleset. Throws a CompileError if the source is not valid
|
|
105
|
+
* @param src - Source code
|
|
106
|
+
* @returns The compiled ruleset. It holds references to functions, not the
|
|
107
|
+
* functions themselves: they are provided by the engine it is evaluated with
|
|
108
|
+
*/
|
|
109
|
+
compile(src: string): Ruleset;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Create a new evaluator, which calls this engine's functions
|
|
113
|
+
* @param ruleset - The compiled ruleset, either as the object returned by
|
|
114
|
+
* compile() or as a JSON string
|
|
115
|
+
*/
|
|
116
|
+
createEvaluator(ruleset: Ruleset | string): Evaluator;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Clean up engine resources. Evaluators created by it keep working.
|
|
120
|
+
*/
|
|
121
|
+
destroy(): void;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Create a new engine
|
|
126
|
+
* @returns Promise resolving to an Engine instance
|
|
92
127
|
*/
|
|
93
|
-
export function
|
|
94
|
-
options: RegisterFunctionOptions,
|
|
95
|
-
): Promise<void>;
|
|
128
|
+
export function createEngine(): Promise<Engine>;
|
package/lib/index.js
CHANGED
|
@@ -67,78 +67,94 @@ class Evaluator {
|
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
async function createEvaluator(ruleset) {
|
|
71
|
-
if (!wasmInstance) {
|
|
72
|
-
await init();
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const rulesetJSON =
|
|
76
|
-
typeof ruleset === "string" ? ruleset : JSON.stringify(ruleset);
|
|
77
|
-
|
|
78
|
-
const result = globalThis.__nodoraCreateEvaluator(rulesetJSON);
|
|
79
|
-
if (result.error) {
|
|
80
|
-
throw new Error(result.error);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
return new Evaluator(result);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
async function compile(src) {
|
|
87
|
-
if (!wasmInstance) {
|
|
88
|
-
await init();
|
|
89
|
-
}
|
|
90
|
-
const result = globalThis.__nodoraCompile(src);
|
|
91
|
-
if (result.error) throw result.error;
|
|
92
|
-
return result;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
70
|
function assertType(x, type, err) {
|
|
96
71
|
if (!x || typeof x !== type) {
|
|
97
72
|
throw new Error(err);
|
|
98
73
|
}
|
|
99
74
|
}
|
|
100
75
|
|
|
101
|
-
|
|
102
|
-
|
|
76
|
+
class CompileError extends Error {
|
|
77
|
+
constructor({ message, pos, errors }) {
|
|
78
|
+
super(message);
|
|
79
|
+
this.name = "CompileError";
|
|
80
|
+
if (pos) this.pos = pos;
|
|
81
|
+
if (errors) this.errors = errors;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
103
84
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
"returnType is required and must be a string",
|
|
109
|
-
);
|
|
110
|
-
assertType(fn, "function", "fn is required and must be a function");
|
|
85
|
+
class Engine {
|
|
86
|
+
constructor(engineId) {
|
|
87
|
+
this.engineId = engineId;
|
|
88
|
+
}
|
|
111
89
|
|
|
112
|
-
|
|
113
|
-
assertType(
|
|
90
|
+
registerFunction({ name, namespace, args, returnType, fn }) {
|
|
91
|
+
assertType(name, "string", "name is required and must be a string");
|
|
114
92
|
assertType(
|
|
115
|
-
|
|
93
|
+
returnType,
|
|
116
94
|
"string",
|
|
117
|
-
|
|
95
|
+
"returnType is required and must be a string",
|
|
118
96
|
);
|
|
119
|
-
assertType(
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
`args[${i}]
|
|
97
|
+
assertType(fn, "function", "fn is required and must be a function");
|
|
98
|
+
|
|
99
|
+
args = (args || []).map((arg, i) => {
|
|
100
|
+
assertType(arg, "object", `args[${i}] must be an object`);
|
|
101
|
+
assertType(
|
|
102
|
+
arg.name,
|
|
103
|
+
"string",
|
|
104
|
+
`args[${i}].name is required and must be a string`,
|
|
105
|
+
);
|
|
106
|
+
assertType(
|
|
107
|
+
arg.type,
|
|
108
|
+
"string",
|
|
109
|
+
`args[${i}].type is required and must be a string`,
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
name: arg.name,
|
|
114
|
+
type: arg.type,
|
|
115
|
+
required: !!(arg.required ?? true),
|
|
116
|
+
};
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
namespace = typeof namespace === "string" ? namespace : "";
|
|
120
|
+
const spec = { name, namespace, args, returnType };
|
|
121
|
+
|
|
122
|
+
const result = globalThis.__nodoraRegisterFunction(this.engineId, spec, fn);
|
|
123
|
+
if (result && result.error) throw new Error(result.error);
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
compile(src) {
|
|
128
|
+
const result = globalThis.__nodoraCompile(this.engineId, src);
|
|
129
|
+
if (result.error) {
|
|
130
|
+
throw typeof result.error === "string"
|
|
131
|
+
? new Error(result.error)
|
|
132
|
+
: new CompileError(result.error);
|
|
133
|
+
}
|
|
134
|
+
return result;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
createEvaluator(ruleset) {
|
|
138
|
+
const rulesetJSON =
|
|
139
|
+
typeof ruleset === "string" ? ruleset : JSON.stringify(ruleset);
|
|
140
|
+
|
|
141
|
+
const result = globalThis.__nodoraCreateEvaluator(
|
|
142
|
+
this.engineId,
|
|
143
|
+
rulesetJSON,
|
|
123
144
|
);
|
|
145
|
+
if (result.error) throw new Error(result.error);
|
|
146
|
+
|
|
147
|
+
return new Evaluator(result);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
destroy() {
|
|
151
|
+
globalThis.__nodoraDestroyEngine(this.engineId);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
124
154
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
required: !!(arg.required ?? true),
|
|
129
|
-
};
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
namespace = typeof namespace === "string" ? namespace : "";
|
|
133
|
-
const spec = {
|
|
134
|
-
name,
|
|
135
|
-
namespace,
|
|
136
|
-
args: args,
|
|
137
|
-
returnType,
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
const result = globalThis.__nodoraRegisterFunction(spec, fn);
|
|
141
|
-
if (result && result.error) throw new Error(result.error);
|
|
155
|
+
async function createEngine() {
|
|
156
|
+
if (!wasmInstance) await init();
|
|
157
|
+
return new Engine(globalThis.__nodoraCreateEngine());
|
|
142
158
|
}
|
|
143
159
|
|
|
144
|
-
export {
|
|
160
|
+
export { CompileError, createEngine, Evaluator };
|
package/lib/nodora.wasm.gz
CHANGED
|
Binary file
|