@zaly/agent 0.0.5 → 0.0.9
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/{agent-BaD2kax9.mjs → agent-B7cQOuMZ.mjs} +2 -2
- package/dist/agent-B7cQOuMZ.mjs.map +1 -0
- package/dist/{agent-EkPynZRJ.mjs → agent-D3wVIibN.mjs} +4 -3
- package/dist/agent-D3wVIibN.mjs.map +1 -0
- package/dist/{compactions-3AjPqjeZ.mjs → compactions-B8VdZJoU.mjs} +2 -2
- package/dist/{compactions-3AjPqjeZ.mjs.map → compactions-B8VdZJoU.mjs.map} +1 -1
- package/dist/{edit-Qxl4w1C2.mjs → edit-D4yaBK1K.mjs} +9 -17
- package/dist/edit-D4yaBK1K.mjs.map +1 -0
- package/dist/{fetch-DknuSrSX.mjs → fetch-VKhSWdEh.mjs} +2 -2
- package/dist/{fetch-DknuSrSX.mjs.map → fetch-VKhSWdEh.mjs.map} +1 -1
- package/dist/{find-Bah9SKWX.mjs → find-CimUGn-L.mjs} +6 -20
- package/dist/find-CimUGn-L.mjs.map +1 -0
- package/dist/{grep-B4GF0svc.mjs → grep-6O3mjSvV.mjs} +10 -78
- package/dist/grep-6O3mjSvV.mjs.map +1 -0
- package/dist/index-node-esm-CJsN8G51.mjs +2116 -0
- package/dist/index-node-esm-CJsN8G51.mjs.map +1 -0
- package/dist/index.d.mts +25 -37
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +3 -953
- package/dist/{manager-f-WjFTMX.mjs → manager-BqlbCGYV.mjs} +12 -2
- package/dist/{manager-f-WjFTMX.mjs.map → manager-BqlbCGYV.mjs.map} +1 -1
- package/dist/{masker-Bv9KjkDF.mjs → masker-DRFztUwc.mjs} +2 -2
- package/dist/{masker-Bv9KjkDF.mjs.map → masker-DRFztUwc.mjs.map} +1 -1
- package/dist/{read-Djsx2NZl.mjs → read-CSL_8nAk.mjs} +47 -63
- package/dist/read-CSL_8nAk.mjs.map +1 -0
- package/dist/{registry-BVf0aZEA.mjs → registry-L9GDccS0.mjs} +89 -43
- package/dist/registry-L9GDccS0.mjs.map +1 -0
- package/dist/{session-Bv8JiT8P.mjs → session-BSvUrr12.mjs} +2 -2
- package/dist/{session-Bv8JiT8P.mjs.map → session-BSvUrr12.mjs.map} +1 -1
- package/dist/session.mjs +1 -1
- package/dist/src-fK6DL2bg.mjs +953 -0
- package/dist/src-fK6DL2bg.mjs.map +1 -0
- package/dist/{subagent-CX7Zjcws.mjs → subagent-C8XCEecM.mjs} +2 -2
- package/dist/{subagent-CX7Zjcws.mjs.map → subagent-C8XCEecM.mjs.map} +1 -1
- package/dist/{tools-Bl-YI890.mjs → tools-wvkkuXWH.mjs} +76 -20
- package/dist/tools-wvkkuXWH.mjs.map +1 -0
- package/dist/{write-D3L-92vj.mjs → write-DwGiDNeG.mjs} +3 -11
- package/dist/write-DwGiDNeG.mjs.map +1 -0
- package/package.json +12 -12
- package/dist/agent-BaD2kax9.mjs.map +0 -1
- package/dist/agent-EkPynZRJ.mjs.map +0 -1
- package/dist/edit-Qxl4w1C2.mjs.map +0 -1
- package/dist/find-Bah9SKWX.mjs.map +0 -1
- package/dist/grep-B4GF0svc.mjs.map +0 -1
- package/dist/index-node-esm-D6YQRKEz.mjs +0 -1537
- package/dist/index-node-esm-D6YQRKEz.mjs.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/dist/read-Djsx2NZl.mjs.map +0 -1
- package/dist/registry-BVf0aZEA.mjs.map +0 -1
- package/dist/tools-Bl-YI890.mjs.map +0 -1
- package/dist/write-D3L-92vj.mjs.map +0 -1
|
@@ -1,1537 +0,0 @@
|
|
|
1
|
-
import vm from "vm";
|
|
2
|
-
//#region ../../node_modules/.bun/jsonpath-plus@10.4.0/node_modules/jsonpath-plus/dist/index-node-esm.js
|
|
3
|
-
/**
|
|
4
|
-
* @implements {IHooks}
|
|
5
|
-
*/
|
|
6
|
-
var Hooks = class {
|
|
7
|
-
/**
|
|
8
|
-
* @callback HookCallback
|
|
9
|
-
* @this {*|Jsep} this
|
|
10
|
-
* @param {Jsep} env
|
|
11
|
-
* @returns: void
|
|
12
|
-
*/
|
|
13
|
-
/**
|
|
14
|
-
* Adds the given callback to the list of callbacks for the given hook.
|
|
15
|
-
*
|
|
16
|
-
* The callback will be invoked when the hook it is registered for is run.
|
|
17
|
-
*
|
|
18
|
-
* One callback function can be registered to multiple hooks and the same hook multiple times.
|
|
19
|
-
*
|
|
20
|
-
* @param {string|object} name The name of the hook, or an object of callbacks keyed by name
|
|
21
|
-
* @param {HookCallback|boolean} callback The callback function which is given environment variables.
|
|
22
|
-
* @param {?boolean} [first=false] Will add the hook to the top of the list (defaults to the bottom)
|
|
23
|
-
* @public
|
|
24
|
-
*/
|
|
25
|
-
add(name, callback, first) {
|
|
26
|
-
if (typeof arguments[0] != "string") for (let name in arguments[0]) this.add(name, arguments[0][name], arguments[1]);
|
|
27
|
-
else (Array.isArray(name) ? name : [name]).forEach(function(name) {
|
|
28
|
-
this[name] = this[name] || [];
|
|
29
|
-
if (callback) this[name][first ? "unshift" : "push"](callback);
|
|
30
|
-
}, this);
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Runs a hook invoking all registered callbacks with the given environment variables.
|
|
34
|
-
*
|
|
35
|
-
* Callbacks will be invoked synchronously and in the order in which they were registered.
|
|
36
|
-
*
|
|
37
|
-
* @param {string} name The name of the hook.
|
|
38
|
-
* @param {Object<string, any>} env The environment variables of the hook passed to all callbacks registered.
|
|
39
|
-
* @public
|
|
40
|
-
*/
|
|
41
|
-
run(name, env) {
|
|
42
|
-
this[name] = this[name] || [];
|
|
43
|
-
this[name].forEach(function(callback) {
|
|
44
|
-
callback.call(env && env.context ? env.context : env, env);
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
/**
|
|
49
|
-
* @implements {IPlugins}
|
|
50
|
-
*/
|
|
51
|
-
var Plugins = class {
|
|
52
|
-
constructor(jsep) {
|
|
53
|
-
this.jsep = jsep;
|
|
54
|
-
this.registered = {};
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* @callback PluginSetup
|
|
58
|
-
* @this {Jsep} jsep
|
|
59
|
-
* @returns: void
|
|
60
|
-
*/
|
|
61
|
-
/**
|
|
62
|
-
* Adds the given plugin(s) to the registry
|
|
63
|
-
*
|
|
64
|
-
* @param {object} plugins
|
|
65
|
-
* @param {string} plugins.name The name of the plugin
|
|
66
|
-
* @param {PluginSetup} plugins.init The init function
|
|
67
|
-
* @public
|
|
68
|
-
*/
|
|
69
|
-
register(...plugins) {
|
|
70
|
-
plugins.forEach((plugin) => {
|
|
71
|
-
if (typeof plugin !== "object" || !plugin.name || !plugin.init) throw new Error("Invalid JSEP plugin format");
|
|
72
|
-
if (this.registered[plugin.name]) return;
|
|
73
|
-
plugin.init(this.jsep);
|
|
74
|
-
this.registered[plugin.name] = plugin;
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
var Jsep = class Jsep {
|
|
79
|
-
/**
|
|
80
|
-
* @returns {string}
|
|
81
|
-
*/
|
|
82
|
-
static get version() {
|
|
83
|
-
return "1.4.0";
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* @returns {string}
|
|
87
|
-
*/
|
|
88
|
-
static toString() {
|
|
89
|
-
return "JavaScript Expression Parser (JSEP) v" + Jsep.version;
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* @method addUnaryOp
|
|
93
|
-
* @param {string} op_name The name of the unary op to add
|
|
94
|
-
* @returns {Jsep}
|
|
95
|
-
*/
|
|
96
|
-
static addUnaryOp(op_name) {
|
|
97
|
-
Jsep.max_unop_len = Math.max(op_name.length, Jsep.max_unop_len);
|
|
98
|
-
Jsep.unary_ops[op_name] = 1;
|
|
99
|
-
return Jsep;
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* @method jsep.addBinaryOp
|
|
103
|
-
* @param {string} op_name The name of the binary op to add
|
|
104
|
-
* @param {number} precedence The precedence of the binary op (can be a float). Higher number = higher precedence
|
|
105
|
-
* @param {boolean} [isRightAssociative=false] whether operator is right-associative
|
|
106
|
-
* @returns {Jsep}
|
|
107
|
-
*/
|
|
108
|
-
static addBinaryOp(op_name, precedence, isRightAssociative) {
|
|
109
|
-
Jsep.max_binop_len = Math.max(op_name.length, Jsep.max_binop_len);
|
|
110
|
-
Jsep.binary_ops[op_name] = precedence;
|
|
111
|
-
if (isRightAssociative) Jsep.right_associative.add(op_name);
|
|
112
|
-
else Jsep.right_associative.delete(op_name);
|
|
113
|
-
return Jsep;
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* @method addIdentifierChar
|
|
117
|
-
* @param {string} char The additional character to treat as a valid part of an identifier
|
|
118
|
-
* @returns {Jsep}
|
|
119
|
-
*/
|
|
120
|
-
static addIdentifierChar(char) {
|
|
121
|
-
Jsep.additional_identifier_chars.add(char);
|
|
122
|
-
return Jsep;
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* @method addLiteral
|
|
126
|
-
* @param {string} literal_name The name of the literal to add
|
|
127
|
-
* @param {*} literal_value The value of the literal
|
|
128
|
-
* @returns {Jsep}
|
|
129
|
-
*/
|
|
130
|
-
static addLiteral(literal_name, literal_value) {
|
|
131
|
-
Jsep.literals[literal_name] = literal_value;
|
|
132
|
-
return Jsep;
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* @method removeUnaryOp
|
|
136
|
-
* @param {string} op_name The name of the unary op to remove
|
|
137
|
-
* @returns {Jsep}
|
|
138
|
-
*/
|
|
139
|
-
static removeUnaryOp(op_name) {
|
|
140
|
-
delete Jsep.unary_ops[op_name];
|
|
141
|
-
if (op_name.length === Jsep.max_unop_len) Jsep.max_unop_len = Jsep.getMaxKeyLen(Jsep.unary_ops);
|
|
142
|
-
return Jsep;
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
145
|
-
* @method removeAllUnaryOps
|
|
146
|
-
* @returns {Jsep}
|
|
147
|
-
*/
|
|
148
|
-
static removeAllUnaryOps() {
|
|
149
|
-
Jsep.unary_ops = {};
|
|
150
|
-
Jsep.max_unop_len = 0;
|
|
151
|
-
return Jsep;
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* @method removeIdentifierChar
|
|
155
|
-
* @param {string} char The additional character to stop treating as a valid part of an identifier
|
|
156
|
-
* @returns {Jsep}
|
|
157
|
-
*/
|
|
158
|
-
static removeIdentifierChar(char) {
|
|
159
|
-
Jsep.additional_identifier_chars.delete(char);
|
|
160
|
-
return Jsep;
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* @method removeBinaryOp
|
|
164
|
-
* @param {string} op_name The name of the binary op to remove
|
|
165
|
-
* @returns {Jsep}
|
|
166
|
-
*/
|
|
167
|
-
static removeBinaryOp(op_name) {
|
|
168
|
-
delete Jsep.binary_ops[op_name];
|
|
169
|
-
if (op_name.length === Jsep.max_binop_len) Jsep.max_binop_len = Jsep.getMaxKeyLen(Jsep.binary_ops);
|
|
170
|
-
Jsep.right_associative.delete(op_name);
|
|
171
|
-
return Jsep;
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* @method removeAllBinaryOps
|
|
175
|
-
* @returns {Jsep}
|
|
176
|
-
*/
|
|
177
|
-
static removeAllBinaryOps() {
|
|
178
|
-
Jsep.binary_ops = {};
|
|
179
|
-
Jsep.max_binop_len = 0;
|
|
180
|
-
return Jsep;
|
|
181
|
-
}
|
|
182
|
-
/**
|
|
183
|
-
* @method removeLiteral
|
|
184
|
-
* @param {string} literal_name The name of the literal to remove
|
|
185
|
-
* @returns {Jsep}
|
|
186
|
-
*/
|
|
187
|
-
static removeLiteral(literal_name) {
|
|
188
|
-
delete Jsep.literals[literal_name];
|
|
189
|
-
return Jsep;
|
|
190
|
-
}
|
|
191
|
-
/**
|
|
192
|
-
* @method removeAllLiterals
|
|
193
|
-
* @returns {Jsep}
|
|
194
|
-
*/
|
|
195
|
-
static removeAllLiterals() {
|
|
196
|
-
Jsep.literals = {};
|
|
197
|
-
return Jsep;
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* @returns {string}
|
|
201
|
-
*/
|
|
202
|
-
get char() {
|
|
203
|
-
return this.expr.charAt(this.index);
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* @returns {number}
|
|
207
|
-
*/
|
|
208
|
-
get code() {
|
|
209
|
-
return this.expr.charCodeAt(this.index);
|
|
210
|
-
}
|
|
211
|
-
/**
|
|
212
|
-
* @param {string} expr a string with the passed in express
|
|
213
|
-
* @returns Jsep
|
|
214
|
-
*/
|
|
215
|
-
constructor(expr) {
|
|
216
|
-
this.expr = expr;
|
|
217
|
-
this.index = 0;
|
|
218
|
-
}
|
|
219
|
-
/**
|
|
220
|
-
* static top-level parser
|
|
221
|
-
* @returns {jsep.Expression}
|
|
222
|
-
*/
|
|
223
|
-
static parse(expr) {
|
|
224
|
-
return new Jsep(expr).parse();
|
|
225
|
-
}
|
|
226
|
-
/**
|
|
227
|
-
* Get the longest key length of any object
|
|
228
|
-
* @param {object} obj
|
|
229
|
-
* @returns {number}
|
|
230
|
-
*/
|
|
231
|
-
static getMaxKeyLen(obj) {
|
|
232
|
-
return Math.max(0, ...Object.keys(obj).map((k) => k.length));
|
|
233
|
-
}
|
|
234
|
-
/**
|
|
235
|
-
* `ch` is a character code in the next three functions
|
|
236
|
-
* @param {number} ch
|
|
237
|
-
* @returns {boolean}
|
|
238
|
-
*/
|
|
239
|
-
static isDecimalDigit(ch) {
|
|
240
|
-
return ch >= 48 && ch <= 57;
|
|
241
|
-
}
|
|
242
|
-
/**
|
|
243
|
-
* Returns the precedence of a binary operator or `0` if it isn't a binary operator. Can be float.
|
|
244
|
-
* @param {string} op_val
|
|
245
|
-
* @returns {number}
|
|
246
|
-
*/
|
|
247
|
-
static binaryPrecedence(op_val) {
|
|
248
|
-
return Jsep.binary_ops[op_val] || 0;
|
|
249
|
-
}
|
|
250
|
-
/**
|
|
251
|
-
* Looks for start of identifier
|
|
252
|
-
* @param {number} ch
|
|
253
|
-
* @returns {boolean}
|
|
254
|
-
*/
|
|
255
|
-
static isIdentifierStart(ch) {
|
|
256
|
-
return ch >= 65 && ch <= 90 || ch >= 97 && ch <= 122 || ch >= 128 && !Jsep.binary_ops[String.fromCharCode(ch)] || Jsep.additional_identifier_chars.has(String.fromCharCode(ch));
|
|
257
|
-
}
|
|
258
|
-
/**
|
|
259
|
-
* @param {number} ch
|
|
260
|
-
* @returns {boolean}
|
|
261
|
-
*/
|
|
262
|
-
static isIdentifierPart(ch) {
|
|
263
|
-
return Jsep.isIdentifierStart(ch) || Jsep.isDecimalDigit(ch);
|
|
264
|
-
}
|
|
265
|
-
/**
|
|
266
|
-
* throw error at index of the expression
|
|
267
|
-
* @param {string} message
|
|
268
|
-
* @throws
|
|
269
|
-
*/
|
|
270
|
-
throwError(message) {
|
|
271
|
-
const error = /* @__PURE__ */ new Error(message + " at character " + this.index);
|
|
272
|
-
error.index = this.index;
|
|
273
|
-
error.description = message;
|
|
274
|
-
throw error;
|
|
275
|
-
}
|
|
276
|
-
/**
|
|
277
|
-
* Run a given hook
|
|
278
|
-
* @param {string} name
|
|
279
|
-
* @param {jsep.Expression|false} [node]
|
|
280
|
-
* @returns {?jsep.Expression}
|
|
281
|
-
*/
|
|
282
|
-
runHook(name, node) {
|
|
283
|
-
if (Jsep.hooks[name]) {
|
|
284
|
-
const env = {
|
|
285
|
-
context: this,
|
|
286
|
-
node
|
|
287
|
-
};
|
|
288
|
-
Jsep.hooks.run(name, env);
|
|
289
|
-
return env.node;
|
|
290
|
-
}
|
|
291
|
-
return node;
|
|
292
|
-
}
|
|
293
|
-
/**
|
|
294
|
-
* Runs a given hook until one returns a node
|
|
295
|
-
* @param {string} name
|
|
296
|
-
* @returns {?jsep.Expression}
|
|
297
|
-
*/
|
|
298
|
-
searchHook(name) {
|
|
299
|
-
if (Jsep.hooks[name]) {
|
|
300
|
-
const env = { context: this };
|
|
301
|
-
Jsep.hooks[name].find(function(callback) {
|
|
302
|
-
callback.call(env.context, env);
|
|
303
|
-
return env.node;
|
|
304
|
-
});
|
|
305
|
-
return env.node;
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
/**
|
|
309
|
-
* Push `index` up to the next non-space character
|
|
310
|
-
*/
|
|
311
|
-
gobbleSpaces() {
|
|
312
|
-
let ch = this.code;
|
|
313
|
-
while (ch === Jsep.SPACE_CODE || ch === Jsep.TAB_CODE || ch === Jsep.LF_CODE || ch === Jsep.CR_CODE) ch = this.expr.charCodeAt(++this.index);
|
|
314
|
-
this.runHook("gobble-spaces");
|
|
315
|
-
}
|
|
316
|
-
/**
|
|
317
|
-
* Top-level method to parse all expressions and returns compound or single node
|
|
318
|
-
* @returns {jsep.Expression}
|
|
319
|
-
*/
|
|
320
|
-
parse() {
|
|
321
|
-
this.runHook("before-all");
|
|
322
|
-
const nodes = this.gobbleExpressions();
|
|
323
|
-
const node = nodes.length === 1 ? nodes[0] : {
|
|
324
|
-
type: Jsep.COMPOUND,
|
|
325
|
-
body: nodes
|
|
326
|
-
};
|
|
327
|
-
return this.runHook("after-all", node);
|
|
328
|
-
}
|
|
329
|
-
/**
|
|
330
|
-
* top-level parser (but can be reused within as well)
|
|
331
|
-
* @param {number} [untilICode]
|
|
332
|
-
* @returns {jsep.Expression[]}
|
|
333
|
-
*/
|
|
334
|
-
gobbleExpressions(untilICode) {
|
|
335
|
-
let nodes = [], ch_i, node;
|
|
336
|
-
while (this.index < this.expr.length) {
|
|
337
|
-
ch_i = this.code;
|
|
338
|
-
if (ch_i === Jsep.SEMCOL_CODE || ch_i === Jsep.COMMA_CODE) this.index++;
|
|
339
|
-
else if (node = this.gobbleExpression()) nodes.push(node);
|
|
340
|
-
else if (this.index < this.expr.length) {
|
|
341
|
-
if (ch_i === untilICode) break;
|
|
342
|
-
this.throwError("Unexpected \"" + this.char + "\"");
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
return nodes;
|
|
346
|
-
}
|
|
347
|
-
/**
|
|
348
|
-
* The main parsing function.
|
|
349
|
-
* @returns {?jsep.Expression}
|
|
350
|
-
*/
|
|
351
|
-
gobbleExpression() {
|
|
352
|
-
const node = this.searchHook("gobble-expression") || this.gobbleBinaryExpression();
|
|
353
|
-
this.gobbleSpaces();
|
|
354
|
-
return this.runHook("after-expression", node);
|
|
355
|
-
}
|
|
356
|
-
/**
|
|
357
|
-
* Search for the operation portion of the string (e.g. `+`, `===`)
|
|
358
|
-
* Start by taking the longest possible binary operations (3 characters: `===`, `!==`, `>>>`)
|
|
359
|
-
* and move down from 3 to 2 to 1 character until a matching binary operation is found
|
|
360
|
-
* then, return that binary operation
|
|
361
|
-
* @returns {string|boolean}
|
|
362
|
-
*/
|
|
363
|
-
gobbleBinaryOp() {
|
|
364
|
-
this.gobbleSpaces();
|
|
365
|
-
let to_check = this.expr.substr(this.index, Jsep.max_binop_len);
|
|
366
|
-
let tc_len = to_check.length;
|
|
367
|
-
while (tc_len > 0) {
|
|
368
|
-
if (Jsep.binary_ops.hasOwnProperty(to_check) && (!Jsep.isIdentifierStart(this.code) || this.index + to_check.length < this.expr.length && !Jsep.isIdentifierPart(this.expr.charCodeAt(this.index + to_check.length)))) {
|
|
369
|
-
this.index += tc_len;
|
|
370
|
-
return to_check;
|
|
371
|
-
}
|
|
372
|
-
to_check = to_check.substr(0, --tc_len);
|
|
373
|
-
}
|
|
374
|
-
return false;
|
|
375
|
-
}
|
|
376
|
-
/**
|
|
377
|
-
* This function is responsible for gobbling an individual expression,
|
|
378
|
-
* e.g. `1`, `1+2`, `a+(b*2)-Math.sqrt(2)`
|
|
379
|
-
* @returns {?jsep.BinaryExpression}
|
|
380
|
-
*/
|
|
381
|
-
gobbleBinaryExpression() {
|
|
382
|
-
let node, biop, prec, stack, biop_info, left, right, i, cur_biop;
|
|
383
|
-
left = this.gobbleToken();
|
|
384
|
-
if (!left) return left;
|
|
385
|
-
biop = this.gobbleBinaryOp();
|
|
386
|
-
if (!biop) return left;
|
|
387
|
-
biop_info = {
|
|
388
|
-
value: biop,
|
|
389
|
-
prec: Jsep.binaryPrecedence(biop),
|
|
390
|
-
right_a: Jsep.right_associative.has(biop)
|
|
391
|
-
};
|
|
392
|
-
right = this.gobbleToken();
|
|
393
|
-
if (!right) this.throwError("Expected expression after " + biop);
|
|
394
|
-
stack = [
|
|
395
|
-
left,
|
|
396
|
-
biop_info,
|
|
397
|
-
right
|
|
398
|
-
];
|
|
399
|
-
while (biop = this.gobbleBinaryOp()) {
|
|
400
|
-
prec = Jsep.binaryPrecedence(biop);
|
|
401
|
-
if (prec === 0) {
|
|
402
|
-
this.index -= biop.length;
|
|
403
|
-
break;
|
|
404
|
-
}
|
|
405
|
-
biop_info = {
|
|
406
|
-
value: biop,
|
|
407
|
-
prec,
|
|
408
|
-
right_a: Jsep.right_associative.has(biop)
|
|
409
|
-
};
|
|
410
|
-
cur_biop = biop;
|
|
411
|
-
const comparePrev = (prev) => biop_info.right_a && prev.right_a ? prec > prev.prec : prec <= prev.prec;
|
|
412
|
-
while (stack.length > 2 && comparePrev(stack[stack.length - 2])) {
|
|
413
|
-
right = stack.pop();
|
|
414
|
-
biop = stack.pop().value;
|
|
415
|
-
left = stack.pop();
|
|
416
|
-
node = {
|
|
417
|
-
type: Jsep.BINARY_EXP,
|
|
418
|
-
operator: biop,
|
|
419
|
-
left,
|
|
420
|
-
right
|
|
421
|
-
};
|
|
422
|
-
stack.push(node);
|
|
423
|
-
}
|
|
424
|
-
node = this.gobbleToken();
|
|
425
|
-
if (!node) this.throwError("Expected expression after " + cur_biop);
|
|
426
|
-
stack.push(biop_info, node);
|
|
427
|
-
}
|
|
428
|
-
i = stack.length - 1;
|
|
429
|
-
node = stack[i];
|
|
430
|
-
while (i > 1) {
|
|
431
|
-
node = {
|
|
432
|
-
type: Jsep.BINARY_EXP,
|
|
433
|
-
operator: stack[i - 1].value,
|
|
434
|
-
left: stack[i - 2],
|
|
435
|
-
right: node
|
|
436
|
-
};
|
|
437
|
-
i -= 2;
|
|
438
|
-
}
|
|
439
|
-
return node;
|
|
440
|
-
}
|
|
441
|
-
/**
|
|
442
|
-
* An individual part of a binary expression:
|
|
443
|
-
* e.g. `foo.bar(baz)`, `1`, `"abc"`, `(a % 2)` (because it's in parenthesis)
|
|
444
|
-
* @returns {boolean|jsep.Expression}
|
|
445
|
-
*/
|
|
446
|
-
gobbleToken() {
|
|
447
|
-
let ch, to_check, tc_len, node;
|
|
448
|
-
this.gobbleSpaces();
|
|
449
|
-
node = this.searchHook("gobble-token");
|
|
450
|
-
if (node) return this.runHook("after-token", node);
|
|
451
|
-
ch = this.code;
|
|
452
|
-
if (Jsep.isDecimalDigit(ch) || ch === Jsep.PERIOD_CODE) return this.gobbleNumericLiteral();
|
|
453
|
-
if (ch === Jsep.SQUOTE_CODE || ch === Jsep.DQUOTE_CODE) node = this.gobbleStringLiteral();
|
|
454
|
-
else if (ch === Jsep.OBRACK_CODE) node = this.gobbleArray();
|
|
455
|
-
else {
|
|
456
|
-
to_check = this.expr.substr(this.index, Jsep.max_unop_len);
|
|
457
|
-
tc_len = to_check.length;
|
|
458
|
-
while (tc_len > 0) {
|
|
459
|
-
if (Jsep.unary_ops.hasOwnProperty(to_check) && (!Jsep.isIdentifierStart(this.code) || this.index + to_check.length < this.expr.length && !Jsep.isIdentifierPart(this.expr.charCodeAt(this.index + to_check.length)))) {
|
|
460
|
-
this.index += tc_len;
|
|
461
|
-
const argument = this.gobbleToken();
|
|
462
|
-
if (!argument) this.throwError("missing unaryOp argument");
|
|
463
|
-
return this.runHook("after-token", {
|
|
464
|
-
type: Jsep.UNARY_EXP,
|
|
465
|
-
operator: to_check,
|
|
466
|
-
argument,
|
|
467
|
-
prefix: true
|
|
468
|
-
});
|
|
469
|
-
}
|
|
470
|
-
to_check = to_check.substr(0, --tc_len);
|
|
471
|
-
}
|
|
472
|
-
if (Jsep.isIdentifierStart(ch)) {
|
|
473
|
-
node = this.gobbleIdentifier();
|
|
474
|
-
if (Jsep.literals.hasOwnProperty(node.name)) node = {
|
|
475
|
-
type: Jsep.LITERAL,
|
|
476
|
-
value: Jsep.literals[node.name],
|
|
477
|
-
raw: node.name
|
|
478
|
-
};
|
|
479
|
-
else if (node.name === Jsep.this_str) node = { type: Jsep.THIS_EXP };
|
|
480
|
-
} else if (ch === Jsep.OPAREN_CODE) node = this.gobbleGroup();
|
|
481
|
-
}
|
|
482
|
-
if (!node) return this.runHook("after-token", false);
|
|
483
|
-
node = this.gobbleTokenProperty(node);
|
|
484
|
-
return this.runHook("after-token", node);
|
|
485
|
-
}
|
|
486
|
-
/**
|
|
487
|
-
* Gobble properties of of identifiers/strings/arrays/groups.
|
|
488
|
-
* e.g. `foo`, `bar.baz`, `foo['bar'].baz`
|
|
489
|
-
* It also gobbles function calls:
|
|
490
|
-
* e.g. `Math.acos(obj.angle)`
|
|
491
|
-
* @param {jsep.Expression} node
|
|
492
|
-
* @returns {jsep.Expression}
|
|
493
|
-
*/
|
|
494
|
-
gobbleTokenProperty(node) {
|
|
495
|
-
this.gobbleSpaces();
|
|
496
|
-
let ch = this.code;
|
|
497
|
-
while (ch === Jsep.PERIOD_CODE || ch === Jsep.OBRACK_CODE || ch === Jsep.OPAREN_CODE || ch === Jsep.QUMARK_CODE) {
|
|
498
|
-
let optional;
|
|
499
|
-
if (ch === Jsep.QUMARK_CODE) {
|
|
500
|
-
if (this.expr.charCodeAt(this.index + 1) !== Jsep.PERIOD_CODE) break;
|
|
501
|
-
optional = true;
|
|
502
|
-
this.index += 2;
|
|
503
|
-
this.gobbleSpaces();
|
|
504
|
-
ch = this.code;
|
|
505
|
-
}
|
|
506
|
-
this.index++;
|
|
507
|
-
if (ch === Jsep.OBRACK_CODE) {
|
|
508
|
-
node = {
|
|
509
|
-
type: Jsep.MEMBER_EXP,
|
|
510
|
-
computed: true,
|
|
511
|
-
object: node,
|
|
512
|
-
property: this.gobbleExpression()
|
|
513
|
-
};
|
|
514
|
-
if (!node.property) this.throwError("Unexpected \"" + this.char + "\"");
|
|
515
|
-
this.gobbleSpaces();
|
|
516
|
-
ch = this.code;
|
|
517
|
-
if (ch !== Jsep.CBRACK_CODE) this.throwError("Unclosed [");
|
|
518
|
-
this.index++;
|
|
519
|
-
} else if (ch === Jsep.OPAREN_CODE) node = {
|
|
520
|
-
type: Jsep.CALL_EXP,
|
|
521
|
-
"arguments": this.gobbleArguments(Jsep.CPAREN_CODE),
|
|
522
|
-
callee: node
|
|
523
|
-
};
|
|
524
|
-
else if (ch === Jsep.PERIOD_CODE || optional) {
|
|
525
|
-
if (optional) this.index--;
|
|
526
|
-
this.gobbleSpaces();
|
|
527
|
-
node = {
|
|
528
|
-
type: Jsep.MEMBER_EXP,
|
|
529
|
-
computed: false,
|
|
530
|
-
object: node,
|
|
531
|
-
property: this.gobbleIdentifier()
|
|
532
|
-
};
|
|
533
|
-
}
|
|
534
|
-
if (optional) node.optional = true;
|
|
535
|
-
this.gobbleSpaces();
|
|
536
|
-
ch = this.code;
|
|
537
|
-
}
|
|
538
|
-
return node;
|
|
539
|
-
}
|
|
540
|
-
/**
|
|
541
|
-
* Parse simple numeric literals: `12`, `3.4`, `.5`. Do this by using a string to
|
|
542
|
-
* keep track of everything in the numeric literal and then calling `parseFloat` on that string
|
|
543
|
-
* @returns {jsep.Literal}
|
|
544
|
-
*/
|
|
545
|
-
gobbleNumericLiteral() {
|
|
546
|
-
let number = "", ch, chCode;
|
|
547
|
-
while (Jsep.isDecimalDigit(this.code)) number += this.expr.charAt(this.index++);
|
|
548
|
-
if (this.code === Jsep.PERIOD_CODE) {
|
|
549
|
-
number += this.expr.charAt(this.index++);
|
|
550
|
-
while (Jsep.isDecimalDigit(this.code)) number += this.expr.charAt(this.index++);
|
|
551
|
-
}
|
|
552
|
-
ch = this.char;
|
|
553
|
-
if (ch === "e" || ch === "E") {
|
|
554
|
-
number += this.expr.charAt(this.index++);
|
|
555
|
-
ch = this.char;
|
|
556
|
-
if (ch === "+" || ch === "-") number += this.expr.charAt(this.index++);
|
|
557
|
-
while (Jsep.isDecimalDigit(this.code)) number += this.expr.charAt(this.index++);
|
|
558
|
-
if (!Jsep.isDecimalDigit(this.expr.charCodeAt(this.index - 1))) this.throwError("Expected exponent (" + number + this.char + ")");
|
|
559
|
-
}
|
|
560
|
-
chCode = this.code;
|
|
561
|
-
if (Jsep.isIdentifierStart(chCode)) this.throwError("Variable names cannot start with a number (" + number + this.char + ")");
|
|
562
|
-
else if (chCode === Jsep.PERIOD_CODE || number.length === 1 && number.charCodeAt(0) === Jsep.PERIOD_CODE) this.throwError("Unexpected period");
|
|
563
|
-
return {
|
|
564
|
-
type: Jsep.LITERAL,
|
|
565
|
-
value: parseFloat(number),
|
|
566
|
-
raw: number
|
|
567
|
-
};
|
|
568
|
-
}
|
|
569
|
-
/**
|
|
570
|
-
* Parses a string literal, staring with single or double quotes with basic support for escape codes
|
|
571
|
-
* e.g. `"hello world"`, `'this is\nJSEP'`
|
|
572
|
-
* @returns {jsep.Literal}
|
|
573
|
-
*/
|
|
574
|
-
gobbleStringLiteral() {
|
|
575
|
-
let str = "";
|
|
576
|
-
const startIndex = this.index;
|
|
577
|
-
const quote = this.expr.charAt(this.index++);
|
|
578
|
-
let closed = false;
|
|
579
|
-
while (this.index < this.expr.length) {
|
|
580
|
-
let ch = this.expr.charAt(this.index++);
|
|
581
|
-
if (ch === quote) {
|
|
582
|
-
closed = true;
|
|
583
|
-
break;
|
|
584
|
-
} else if (ch === "\\") {
|
|
585
|
-
ch = this.expr.charAt(this.index++);
|
|
586
|
-
switch (ch) {
|
|
587
|
-
case "n":
|
|
588
|
-
str += "\n";
|
|
589
|
-
break;
|
|
590
|
-
case "r":
|
|
591
|
-
str += "\r";
|
|
592
|
-
break;
|
|
593
|
-
case "t":
|
|
594
|
-
str += " ";
|
|
595
|
-
break;
|
|
596
|
-
case "b":
|
|
597
|
-
str += "\b";
|
|
598
|
-
break;
|
|
599
|
-
case "f":
|
|
600
|
-
str += "\f";
|
|
601
|
-
break;
|
|
602
|
-
case "v":
|
|
603
|
-
str += "\v";
|
|
604
|
-
break;
|
|
605
|
-
default: str += ch;
|
|
606
|
-
}
|
|
607
|
-
} else str += ch;
|
|
608
|
-
}
|
|
609
|
-
if (!closed) this.throwError("Unclosed quote after \"" + str + "\"");
|
|
610
|
-
return {
|
|
611
|
-
type: Jsep.LITERAL,
|
|
612
|
-
value: str,
|
|
613
|
-
raw: this.expr.substring(startIndex, this.index)
|
|
614
|
-
};
|
|
615
|
-
}
|
|
616
|
-
/**
|
|
617
|
-
* Gobbles only identifiers
|
|
618
|
-
* e.g.: `foo`, `_value`, `$x1`
|
|
619
|
-
* Also, this function checks if that identifier is a literal:
|
|
620
|
-
* (e.g. `true`, `false`, `null`) or `this`
|
|
621
|
-
* @returns {jsep.Identifier}
|
|
622
|
-
*/
|
|
623
|
-
gobbleIdentifier() {
|
|
624
|
-
let ch = this.code, start = this.index;
|
|
625
|
-
if (Jsep.isIdentifierStart(ch)) this.index++;
|
|
626
|
-
else this.throwError("Unexpected " + this.char);
|
|
627
|
-
while (this.index < this.expr.length) {
|
|
628
|
-
ch = this.code;
|
|
629
|
-
if (Jsep.isIdentifierPart(ch)) this.index++;
|
|
630
|
-
else break;
|
|
631
|
-
}
|
|
632
|
-
return {
|
|
633
|
-
type: Jsep.IDENTIFIER,
|
|
634
|
-
name: this.expr.slice(start, this.index)
|
|
635
|
-
};
|
|
636
|
-
}
|
|
637
|
-
/**
|
|
638
|
-
* Gobbles a list of arguments within the context of a function call
|
|
639
|
-
* or array literal. This function also assumes that the opening character
|
|
640
|
-
* `(` or `[` has already been gobbled, and gobbles expressions and commas
|
|
641
|
-
* until the terminator character `)` or `]` is encountered.
|
|
642
|
-
* e.g. `foo(bar, baz)`, `my_func()`, or `[bar, baz]`
|
|
643
|
-
* @param {number} termination
|
|
644
|
-
* @returns {jsep.Expression[]}
|
|
645
|
-
*/
|
|
646
|
-
gobbleArguments(termination) {
|
|
647
|
-
const args = [];
|
|
648
|
-
let closed = false;
|
|
649
|
-
let separator_count = 0;
|
|
650
|
-
while (this.index < this.expr.length) {
|
|
651
|
-
this.gobbleSpaces();
|
|
652
|
-
let ch_i = this.code;
|
|
653
|
-
if (ch_i === termination) {
|
|
654
|
-
closed = true;
|
|
655
|
-
this.index++;
|
|
656
|
-
if (termination === Jsep.CPAREN_CODE && separator_count && separator_count >= args.length) this.throwError("Unexpected token " + String.fromCharCode(termination));
|
|
657
|
-
break;
|
|
658
|
-
} else if (ch_i === Jsep.COMMA_CODE) {
|
|
659
|
-
this.index++;
|
|
660
|
-
separator_count++;
|
|
661
|
-
if (separator_count !== args.length) {
|
|
662
|
-
if (termination === Jsep.CPAREN_CODE) this.throwError("Unexpected token ,");
|
|
663
|
-
else if (termination === Jsep.CBRACK_CODE) for (let arg = args.length; arg < separator_count; arg++) args.push(null);
|
|
664
|
-
}
|
|
665
|
-
} else if (args.length !== separator_count && separator_count !== 0) this.throwError("Expected comma");
|
|
666
|
-
else {
|
|
667
|
-
const node = this.gobbleExpression();
|
|
668
|
-
if (!node || node.type === Jsep.COMPOUND) this.throwError("Expected comma");
|
|
669
|
-
args.push(node);
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
|
-
if (!closed) this.throwError("Expected " + String.fromCharCode(termination));
|
|
673
|
-
return args;
|
|
674
|
-
}
|
|
675
|
-
/**
|
|
676
|
-
* Responsible for parsing a group of things within parentheses `()`
|
|
677
|
-
* that have no identifier in front (so not a function call)
|
|
678
|
-
* This function assumes that it needs to gobble the opening parenthesis
|
|
679
|
-
* and then tries to gobble everything within that parenthesis, assuming
|
|
680
|
-
* that the next thing it should see is the close parenthesis. If not,
|
|
681
|
-
* then the expression probably doesn't have a `)`
|
|
682
|
-
* @returns {boolean|jsep.Expression}
|
|
683
|
-
*/
|
|
684
|
-
gobbleGroup() {
|
|
685
|
-
this.index++;
|
|
686
|
-
let nodes = this.gobbleExpressions(Jsep.CPAREN_CODE);
|
|
687
|
-
if (this.code === Jsep.CPAREN_CODE) {
|
|
688
|
-
this.index++;
|
|
689
|
-
if (nodes.length === 1) return nodes[0];
|
|
690
|
-
else if (!nodes.length) return false;
|
|
691
|
-
else return {
|
|
692
|
-
type: Jsep.SEQUENCE_EXP,
|
|
693
|
-
expressions: nodes
|
|
694
|
-
};
|
|
695
|
-
} else this.throwError("Unclosed (");
|
|
696
|
-
}
|
|
697
|
-
/**
|
|
698
|
-
* Responsible for parsing Array literals `[1, 2, 3]`
|
|
699
|
-
* This function assumes that it needs to gobble the opening bracket
|
|
700
|
-
* and then tries to gobble the expressions as arguments.
|
|
701
|
-
* @returns {jsep.ArrayExpression}
|
|
702
|
-
*/
|
|
703
|
-
gobbleArray() {
|
|
704
|
-
this.index++;
|
|
705
|
-
return {
|
|
706
|
-
type: Jsep.ARRAY_EXP,
|
|
707
|
-
elements: this.gobbleArguments(Jsep.CBRACK_CODE)
|
|
708
|
-
};
|
|
709
|
-
}
|
|
710
|
-
};
|
|
711
|
-
const hooks = new Hooks();
|
|
712
|
-
Object.assign(Jsep, {
|
|
713
|
-
hooks,
|
|
714
|
-
plugins: new Plugins(Jsep),
|
|
715
|
-
COMPOUND: "Compound",
|
|
716
|
-
SEQUENCE_EXP: "SequenceExpression",
|
|
717
|
-
IDENTIFIER: "Identifier",
|
|
718
|
-
MEMBER_EXP: "MemberExpression",
|
|
719
|
-
LITERAL: "Literal",
|
|
720
|
-
THIS_EXP: "ThisExpression",
|
|
721
|
-
CALL_EXP: "CallExpression",
|
|
722
|
-
UNARY_EXP: "UnaryExpression",
|
|
723
|
-
BINARY_EXP: "BinaryExpression",
|
|
724
|
-
ARRAY_EXP: "ArrayExpression",
|
|
725
|
-
TAB_CODE: 9,
|
|
726
|
-
LF_CODE: 10,
|
|
727
|
-
CR_CODE: 13,
|
|
728
|
-
SPACE_CODE: 32,
|
|
729
|
-
PERIOD_CODE: 46,
|
|
730
|
-
COMMA_CODE: 44,
|
|
731
|
-
SQUOTE_CODE: 39,
|
|
732
|
-
DQUOTE_CODE: 34,
|
|
733
|
-
OPAREN_CODE: 40,
|
|
734
|
-
CPAREN_CODE: 41,
|
|
735
|
-
OBRACK_CODE: 91,
|
|
736
|
-
CBRACK_CODE: 93,
|
|
737
|
-
QUMARK_CODE: 63,
|
|
738
|
-
SEMCOL_CODE: 59,
|
|
739
|
-
COLON_CODE: 58,
|
|
740
|
-
unary_ops: {
|
|
741
|
-
"-": 1,
|
|
742
|
-
"!": 1,
|
|
743
|
-
"~": 1,
|
|
744
|
-
"+": 1
|
|
745
|
-
},
|
|
746
|
-
binary_ops: {
|
|
747
|
-
"||": 1,
|
|
748
|
-
"??": 1,
|
|
749
|
-
"&&": 2,
|
|
750
|
-
"|": 3,
|
|
751
|
-
"^": 4,
|
|
752
|
-
"&": 5,
|
|
753
|
-
"==": 6,
|
|
754
|
-
"!=": 6,
|
|
755
|
-
"===": 6,
|
|
756
|
-
"!==": 6,
|
|
757
|
-
"<": 7,
|
|
758
|
-
">": 7,
|
|
759
|
-
"<=": 7,
|
|
760
|
-
">=": 7,
|
|
761
|
-
"<<": 8,
|
|
762
|
-
">>": 8,
|
|
763
|
-
">>>": 8,
|
|
764
|
-
"+": 9,
|
|
765
|
-
"-": 9,
|
|
766
|
-
"*": 10,
|
|
767
|
-
"/": 10,
|
|
768
|
-
"%": 10,
|
|
769
|
-
"**": 11
|
|
770
|
-
},
|
|
771
|
-
right_associative: /* @__PURE__ */ new Set(["**"]),
|
|
772
|
-
additional_identifier_chars: /* @__PURE__ */ new Set(["$", "_"]),
|
|
773
|
-
literals: {
|
|
774
|
-
"true": true,
|
|
775
|
-
"false": false,
|
|
776
|
-
"null": null
|
|
777
|
-
},
|
|
778
|
-
this_str: "this"
|
|
779
|
-
});
|
|
780
|
-
Jsep.max_unop_len = Jsep.getMaxKeyLen(Jsep.unary_ops);
|
|
781
|
-
Jsep.max_binop_len = Jsep.getMaxKeyLen(Jsep.binary_ops);
|
|
782
|
-
const jsep = (expr) => new Jsep(expr).parse();
|
|
783
|
-
const stdClassProps = Object.getOwnPropertyNames(class Test {});
|
|
784
|
-
Object.getOwnPropertyNames(Jsep).filter((prop) => !stdClassProps.includes(prop) && jsep[prop] === void 0).forEach((m) => {
|
|
785
|
-
jsep[m] = Jsep[m];
|
|
786
|
-
});
|
|
787
|
-
jsep.Jsep = Jsep;
|
|
788
|
-
const CONDITIONAL_EXP = "ConditionalExpression";
|
|
789
|
-
jsep.plugins.register({
|
|
790
|
-
name: "ternary",
|
|
791
|
-
init(jsep) {
|
|
792
|
-
jsep.hooks.add("after-expression", function gobbleTernary(env) {
|
|
793
|
-
if (env.node && this.code === jsep.QUMARK_CODE) {
|
|
794
|
-
this.index++;
|
|
795
|
-
const test = env.node;
|
|
796
|
-
const consequent = this.gobbleExpression();
|
|
797
|
-
if (!consequent) this.throwError("Expected expression");
|
|
798
|
-
this.gobbleSpaces();
|
|
799
|
-
if (this.code === jsep.COLON_CODE) {
|
|
800
|
-
this.index++;
|
|
801
|
-
const alternate = this.gobbleExpression();
|
|
802
|
-
if (!alternate) this.throwError("Expected expression");
|
|
803
|
-
env.node = {
|
|
804
|
-
type: CONDITIONAL_EXP,
|
|
805
|
-
test,
|
|
806
|
-
consequent,
|
|
807
|
-
alternate
|
|
808
|
-
};
|
|
809
|
-
if (test.operator && jsep.binary_ops[test.operator] <= .9) {
|
|
810
|
-
let newTest = test;
|
|
811
|
-
while (newTest.right.operator && jsep.binary_ops[newTest.right.operator] <= .9) newTest = newTest.right;
|
|
812
|
-
env.node.test = newTest.right;
|
|
813
|
-
newTest.right = env.node;
|
|
814
|
-
env.node = test;
|
|
815
|
-
}
|
|
816
|
-
} else this.throwError("Expected :");
|
|
817
|
-
}
|
|
818
|
-
});
|
|
819
|
-
}
|
|
820
|
-
});
|
|
821
|
-
const FSLASH_CODE = 47;
|
|
822
|
-
const BSLASH_CODE = 92;
|
|
823
|
-
var index = {
|
|
824
|
-
name: "regex",
|
|
825
|
-
init(jsep) {
|
|
826
|
-
jsep.hooks.add("gobble-token", function gobbleRegexLiteral(env) {
|
|
827
|
-
if (this.code === FSLASH_CODE) {
|
|
828
|
-
const patternIndex = ++this.index;
|
|
829
|
-
let inCharSet = false;
|
|
830
|
-
while (this.index < this.expr.length) {
|
|
831
|
-
if (this.code === FSLASH_CODE && !inCharSet) {
|
|
832
|
-
const pattern = this.expr.slice(patternIndex, this.index);
|
|
833
|
-
let flags = "";
|
|
834
|
-
while (++this.index < this.expr.length) {
|
|
835
|
-
const code = this.code;
|
|
836
|
-
if (code >= 97 && code <= 122 || code >= 65 && code <= 90 || code >= 48 && code <= 57) flags += this.char;
|
|
837
|
-
else break;
|
|
838
|
-
}
|
|
839
|
-
let value;
|
|
840
|
-
try {
|
|
841
|
-
value = new RegExp(pattern, flags);
|
|
842
|
-
} catch (e) {
|
|
843
|
-
this.throwError(e.message);
|
|
844
|
-
}
|
|
845
|
-
env.node = {
|
|
846
|
-
type: jsep.LITERAL,
|
|
847
|
-
value,
|
|
848
|
-
raw: this.expr.slice(patternIndex - 1, this.index)
|
|
849
|
-
};
|
|
850
|
-
env.node = this.gobbleTokenProperty(env.node);
|
|
851
|
-
return env.node;
|
|
852
|
-
}
|
|
853
|
-
if (this.code === jsep.OBRACK_CODE) inCharSet = true;
|
|
854
|
-
else if (inCharSet && this.code === jsep.CBRACK_CODE) inCharSet = false;
|
|
855
|
-
this.index += this.code === BSLASH_CODE ? 2 : 1;
|
|
856
|
-
}
|
|
857
|
-
this.throwError("Unclosed Regex");
|
|
858
|
-
}
|
|
859
|
-
});
|
|
860
|
-
}
|
|
861
|
-
};
|
|
862
|
-
const PLUS_CODE = 43;
|
|
863
|
-
const plugin = {
|
|
864
|
-
name: "assignment",
|
|
865
|
-
assignmentOperators: /* @__PURE__ */ new Set([
|
|
866
|
-
"=",
|
|
867
|
-
"*=",
|
|
868
|
-
"**=",
|
|
869
|
-
"/=",
|
|
870
|
-
"%=",
|
|
871
|
-
"+=",
|
|
872
|
-
"-=",
|
|
873
|
-
"<<=",
|
|
874
|
-
">>=",
|
|
875
|
-
">>>=",
|
|
876
|
-
"&=",
|
|
877
|
-
"^=",
|
|
878
|
-
"|=",
|
|
879
|
-
"||=",
|
|
880
|
-
"&&=",
|
|
881
|
-
"??="
|
|
882
|
-
]),
|
|
883
|
-
updateOperators: [PLUS_CODE, 45],
|
|
884
|
-
assignmentPrecedence: .9,
|
|
885
|
-
init(jsep) {
|
|
886
|
-
const updateNodeTypes = [jsep.IDENTIFIER, jsep.MEMBER_EXP];
|
|
887
|
-
plugin.assignmentOperators.forEach((op) => jsep.addBinaryOp(op, plugin.assignmentPrecedence, true));
|
|
888
|
-
jsep.hooks.add("gobble-token", function gobbleUpdatePrefix(env) {
|
|
889
|
-
const code = this.code;
|
|
890
|
-
if (plugin.updateOperators.some((c) => c === code && c === this.expr.charCodeAt(this.index + 1))) {
|
|
891
|
-
this.index += 2;
|
|
892
|
-
env.node = {
|
|
893
|
-
type: "UpdateExpression",
|
|
894
|
-
operator: code === PLUS_CODE ? "++" : "--",
|
|
895
|
-
argument: this.gobbleTokenProperty(this.gobbleIdentifier()),
|
|
896
|
-
prefix: true
|
|
897
|
-
};
|
|
898
|
-
if (!env.node.argument || !updateNodeTypes.includes(env.node.argument.type)) this.throwError(`Unexpected ${env.node.operator}`);
|
|
899
|
-
}
|
|
900
|
-
});
|
|
901
|
-
jsep.hooks.add("after-token", function gobbleUpdatePostfix(env) {
|
|
902
|
-
if (env.node) {
|
|
903
|
-
const code = this.code;
|
|
904
|
-
if (plugin.updateOperators.some((c) => c === code && c === this.expr.charCodeAt(this.index + 1))) {
|
|
905
|
-
if (!updateNodeTypes.includes(env.node.type)) this.throwError(`Unexpected ${env.node.operator}`);
|
|
906
|
-
this.index += 2;
|
|
907
|
-
env.node = {
|
|
908
|
-
type: "UpdateExpression",
|
|
909
|
-
operator: code === PLUS_CODE ? "++" : "--",
|
|
910
|
-
argument: env.node,
|
|
911
|
-
prefix: false
|
|
912
|
-
};
|
|
913
|
-
}
|
|
914
|
-
}
|
|
915
|
-
});
|
|
916
|
-
jsep.hooks.add("after-expression", function gobbleAssignment(env) {
|
|
917
|
-
if (env.node) updateBinariesToAssignments(env.node);
|
|
918
|
-
});
|
|
919
|
-
function updateBinariesToAssignments(node) {
|
|
920
|
-
if (plugin.assignmentOperators.has(node.operator)) {
|
|
921
|
-
node.type = "AssignmentExpression";
|
|
922
|
-
updateBinariesToAssignments(node.left);
|
|
923
|
-
updateBinariesToAssignments(node.right);
|
|
924
|
-
} else if (!node.operator) Object.values(node).forEach((val) => {
|
|
925
|
-
if (val && typeof val === "object") updateBinariesToAssignments(val);
|
|
926
|
-
});
|
|
927
|
-
}
|
|
928
|
-
}
|
|
929
|
-
};
|
|
930
|
-
jsep.plugins.register(index, plugin);
|
|
931
|
-
jsep.addUnaryOp("typeof");
|
|
932
|
-
jsep.addUnaryOp("void");
|
|
933
|
-
jsep.addLiteral("null", null);
|
|
934
|
-
jsep.addLiteral("undefined", void 0);
|
|
935
|
-
const BLOCKED_PROTO_PROPERTIES = /* @__PURE__ */ new Set([
|
|
936
|
-
"constructor",
|
|
937
|
-
"__proto__",
|
|
938
|
-
"__defineGetter__",
|
|
939
|
-
"__defineSetter__",
|
|
940
|
-
"__lookupGetter__",
|
|
941
|
-
"__lookupSetter__"
|
|
942
|
-
]);
|
|
943
|
-
const SafeEval = {
|
|
944
|
-
/**
|
|
945
|
-
* @param {jsep.Expression} ast
|
|
946
|
-
* @param {Record<string, any>} subs
|
|
947
|
-
*/
|
|
948
|
-
evalAst(ast, subs) {
|
|
949
|
-
switch (ast.type) {
|
|
950
|
-
case "BinaryExpression":
|
|
951
|
-
case "LogicalExpression": return SafeEval.evalBinaryExpression(ast, subs);
|
|
952
|
-
case "Compound": return SafeEval.evalCompound(ast, subs);
|
|
953
|
-
case "ConditionalExpression": return SafeEval.evalConditionalExpression(ast, subs);
|
|
954
|
-
case "Identifier": return SafeEval.evalIdentifier(ast, subs);
|
|
955
|
-
case "Literal": return SafeEval.evalLiteral(ast, subs);
|
|
956
|
-
case "MemberExpression": return SafeEval.evalMemberExpression(ast, subs);
|
|
957
|
-
case "UnaryExpression": return SafeEval.evalUnaryExpression(ast, subs);
|
|
958
|
-
case "ArrayExpression": return SafeEval.evalArrayExpression(ast, subs);
|
|
959
|
-
case "CallExpression": return SafeEval.evalCallExpression(ast, subs);
|
|
960
|
-
case "AssignmentExpression": return SafeEval.evalAssignmentExpression(ast, subs);
|
|
961
|
-
default: throw SyntaxError("Unexpected expression", ast);
|
|
962
|
-
}
|
|
963
|
-
},
|
|
964
|
-
evalBinaryExpression(ast, subs) {
|
|
965
|
-
return {
|
|
966
|
-
"||": (a, b) => a || b(),
|
|
967
|
-
"&&": (a, b) => a && b(),
|
|
968
|
-
"|": (a, b) => a | b(),
|
|
969
|
-
"^": (a, b) => a ^ b(),
|
|
970
|
-
"&": (a, b) => a & b(),
|
|
971
|
-
"==": (a, b) => a == b(),
|
|
972
|
-
"!=": (a, b) => a != b(),
|
|
973
|
-
"===": (a, b) => a === b(),
|
|
974
|
-
"!==": (a, b) => a !== b(),
|
|
975
|
-
"<": (a, b) => a < b(),
|
|
976
|
-
">": (a, b) => a > b(),
|
|
977
|
-
"<=": (a, b) => a <= b(),
|
|
978
|
-
">=": (a, b) => a >= b(),
|
|
979
|
-
"<<": (a, b) => a << b(),
|
|
980
|
-
">>": (a, b) => a >> b(),
|
|
981
|
-
">>>": (a, b) => a >>> b(),
|
|
982
|
-
"+": (a, b) => a + b(),
|
|
983
|
-
"-": (a, b) => a - b(),
|
|
984
|
-
"*": (a, b) => a * b(),
|
|
985
|
-
"/": (a, b) => a / b(),
|
|
986
|
-
"%": (a, b) => a % b()
|
|
987
|
-
}[ast.operator](SafeEval.evalAst(ast.left, subs), () => SafeEval.evalAst(ast.right, subs));
|
|
988
|
-
},
|
|
989
|
-
evalCompound(ast, subs) {
|
|
990
|
-
let last;
|
|
991
|
-
for (let i = 0; i < ast.body.length; i++) {
|
|
992
|
-
if (ast.body[i].type === "Identifier" && [
|
|
993
|
-
"var",
|
|
994
|
-
"let",
|
|
995
|
-
"const"
|
|
996
|
-
].includes(ast.body[i].name) && ast.body[i + 1] && ast.body[i + 1].type === "AssignmentExpression") i += 1;
|
|
997
|
-
const expr = ast.body[i];
|
|
998
|
-
last = SafeEval.evalAst(expr, subs);
|
|
999
|
-
}
|
|
1000
|
-
return last;
|
|
1001
|
-
},
|
|
1002
|
-
evalConditionalExpression(ast, subs) {
|
|
1003
|
-
if (SafeEval.evalAst(ast.test, subs)) return SafeEval.evalAst(ast.consequent, subs);
|
|
1004
|
-
return SafeEval.evalAst(ast.alternate, subs);
|
|
1005
|
-
},
|
|
1006
|
-
evalIdentifier(ast, subs) {
|
|
1007
|
-
if (Object.hasOwn(subs, ast.name)) return subs[ast.name];
|
|
1008
|
-
throw ReferenceError(`${ast.name} is not defined`);
|
|
1009
|
-
},
|
|
1010
|
-
evalLiteral(ast) {
|
|
1011
|
-
return ast.value;
|
|
1012
|
-
},
|
|
1013
|
-
evalMemberExpression(ast, subs) {
|
|
1014
|
-
const prop = String(ast.computed ? SafeEval.evalAst(ast.property) : ast.property.name);
|
|
1015
|
-
const obj = SafeEval.evalAst(ast.object, subs);
|
|
1016
|
-
if (obj === void 0 || obj === null) throw TypeError(`Cannot read properties of ${obj} (reading '${prop}')`);
|
|
1017
|
-
if (!Object.hasOwn(obj, prop) && BLOCKED_PROTO_PROPERTIES.has(prop)) throw TypeError(`Cannot read properties of ${obj} (reading '${prop}')`);
|
|
1018
|
-
const result = obj[prop];
|
|
1019
|
-
if (typeof result === "function") return result.bind(obj);
|
|
1020
|
-
return result;
|
|
1021
|
-
},
|
|
1022
|
-
evalUnaryExpression(ast, subs) {
|
|
1023
|
-
return {
|
|
1024
|
-
"-": (a) => -SafeEval.evalAst(a, subs),
|
|
1025
|
-
"!": (a) => !SafeEval.evalAst(a, subs),
|
|
1026
|
-
"~": (a) => ~SafeEval.evalAst(a, subs),
|
|
1027
|
-
"+": (a) => +SafeEval.evalAst(a, subs),
|
|
1028
|
-
typeof: (a) => typeof SafeEval.evalAst(a, subs),
|
|
1029
|
-
void: (a) => void SafeEval.evalAst(a, subs)
|
|
1030
|
-
}[ast.operator](ast.argument);
|
|
1031
|
-
},
|
|
1032
|
-
evalArrayExpression(ast, subs) {
|
|
1033
|
-
return ast.elements.map((el) => SafeEval.evalAst(el, subs));
|
|
1034
|
-
},
|
|
1035
|
-
evalCallExpression(ast, subs) {
|
|
1036
|
-
const args = ast.arguments.map((arg) => SafeEval.evalAst(arg, subs));
|
|
1037
|
-
const func = SafeEval.evalAst(ast.callee, subs);
|
|
1038
|
-
/* c8 ignore start */
|
|
1039
|
-
if (func === Function) throw new Error("Function constructor is disabled");
|
|
1040
|
-
/* c8 ignore end */
|
|
1041
|
-
return func(...args);
|
|
1042
|
-
},
|
|
1043
|
-
evalAssignmentExpression(ast, subs) {
|
|
1044
|
-
if (ast.left.type !== "Identifier") throw SyntaxError("Invalid left-hand side in assignment");
|
|
1045
|
-
const id = ast.left.name;
|
|
1046
|
-
subs[id] = SafeEval.evalAst(ast.right, subs);
|
|
1047
|
-
return subs[id];
|
|
1048
|
-
}
|
|
1049
|
-
};
|
|
1050
|
-
/**
|
|
1051
|
-
* A replacement for NodeJS' VM.Script which is also {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP | Content Security Policy} friendly.
|
|
1052
|
-
*/
|
|
1053
|
-
var SafeScript = class {
|
|
1054
|
-
/**
|
|
1055
|
-
* @param {string} expr Expression to evaluate
|
|
1056
|
-
*/
|
|
1057
|
-
constructor(expr) {
|
|
1058
|
-
this.code = expr;
|
|
1059
|
-
this.ast = jsep(this.code);
|
|
1060
|
-
}
|
|
1061
|
-
/**
|
|
1062
|
-
* @param {object} context Object whose items will be added
|
|
1063
|
-
* to evaluation
|
|
1064
|
-
* @returns {EvaluatedResult} Result of evaluated code
|
|
1065
|
-
*/
|
|
1066
|
-
runInNewContext(context) {
|
|
1067
|
-
const keyMap = Object.assign(Object.create(null), context);
|
|
1068
|
-
return SafeEval.evalAst(this.ast, keyMap);
|
|
1069
|
-
}
|
|
1070
|
-
};
|
|
1071
|
-
/**
|
|
1072
|
-
* @typedef {null|boolean|number|string|object|GenericArray} JSONObject
|
|
1073
|
-
*/
|
|
1074
|
-
/**
|
|
1075
|
-
* @typedef {any} AnyItem
|
|
1076
|
-
*/
|
|
1077
|
-
/**
|
|
1078
|
-
* @typedef {any} AnyResult
|
|
1079
|
-
*/
|
|
1080
|
-
/**
|
|
1081
|
-
* Copies array and then pushes item into it.
|
|
1082
|
-
* @param {GenericArray} arr Array to copy and into which to push
|
|
1083
|
-
* @param {AnyItem} item Array item to add (to end)
|
|
1084
|
-
* @returns {GenericArray} Copy of the original array
|
|
1085
|
-
*/
|
|
1086
|
-
function push(arr, item) {
|
|
1087
|
-
arr = arr.slice();
|
|
1088
|
-
arr.push(item);
|
|
1089
|
-
return arr;
|
|
1090
|
-
}
|
|
1091
|
-
/**
|
|
1092
|
-
* Copies array and then unshifts item into it.
|
|
1093
|
-
* @param {AnyItem} item Array item to add (to beginning)
|
|
1094
|
-
* @param {GenericArray} arr Array to copy and into which to unshift
|
|
1095
|
-
* @returns {GenericArray} Copy of the original array
|
|
1096
|
-
*/
|
|
1097
|
-
function unshift(item, arr) {
|
|
1098
|
-
arr = arr.slice();
|
|
1099
|
-
arr.unshift(item);
|
|
1100
|
-
return arr;
|
|
1101
|
-
}
|
|
1102
|
-
/**
|
|
1103
|
-
* Caught when JSONPath is used without `new` but rethrown if with `new`
|
|
1104
|
-
* @extends Error
|
|
1105
|
-
*/
|
|
1106
|
-
var NewError = class extends Error {
|
|
1107
|
-
/**
|
|
1108
|
-
* @param {AnyResult} value The evaluated scalar value
|
|
1109
|
-
*/
|
|
1110
|
-
constructor(value) {
|
|
1111
|
-
super("JSONPath should not be called with \"new\" (it prevents return of (unwrapped) scalar values)");
|
|
1112
|
-
this.avoidNew = true;
|
|
1113
|
-
this.value = value;
|
|
1114
|
-
this.name = "NewError";
|
|
1115
|
-
}
|
|
1116
|
-
};
|
|
1117
|
-
/**
|
|
1118
|
-
* @typedef {object} ReturnObject
|
|
1119
|
-
* @property {string} path
|
|
1120
|
-
* @property {JSONObject} value
|
|
1121
|
-
* @property {object|GenericArray} parent
|
|
1122
|
-
* @property {string} parentProperty
|
|
1123
|
-
*/
|
|
1124
|
-
/**
|
|
1125
|
-
* @callback JSONPathCallback
|
|
1126
|
-
* @param {string|object} preferredOutput
|
|
1127
|
-
* @param {"value"|"property"} type
|
|
1128
|
-
* @param {ReturnObject} fullRetObj
|
|
1129
|
-
* @returns {void}
|
|
1130
|
-
*/
|
|
1131
|
-
/**
|
|
1132
|
-
* @callback OtherTypeCallback
|
|
1133
|
-
* @param {JSONObject} val
|
|
1134
|
-
* @param {string} path
|
|
1135
|
-
* @param {object|GenericArray} parent
|
|
1136
|
-
* @param {string} parentPropName
|
|
1137
|
-
* @returns {boolean}
|
|
1138
|
-
*/
|
|
1139
|
-
/**
|
|
1140
|
-
* @typedef {any} ContextItem
|
|
1141
|
-
*/
|
|
1142
|
-
/**
|
|
1143
|
-
* @typedef {any} EvaluatedResult
|
|
1144
|
-
*/
|
|
1145
|
-
/**
|
|
1146
|
-
* @callback EvalCallback
|
|
1147
|
-
* @param {string} code
|
|
1148
|
-
* @param {ContextItem} context
|
|
1149
|
-
* @returns {EvaluatedResult}
|
|
1150
|
-
*/
|
|
1151
|
-
/**
|
|
1152
|
-
* @typedef {typeof SafeScript} EvalClass
|
|
1153
|
-
*/
|
|
1154
|
-
/**
|
|
1155
|
-
* @typedef {object} JSONPathOptions
|
|
1156
|
-
* @property {JSON} json
|
|
1157
|
-
* @property {string|string[]} path
|
|
1158
|
-
* @property {"value"|"path"|"pointer"|"parent"|"parentProperty"|
|
|
1159
|
-
* "all"} [resultType="value"]
|
|
1160
|
-
* @property {boolean} [flatten=false]
|
|
1161
|
-
* @property {boolean} [wrap=true]
|
|
1162
|
-
* @property {object} [sandbox={}]
|
|
1163
|
-
* @property {EvalCallback|EvalClass|'safe'|'native'|
|
|
1164
|
-
* boolean} [eval = 'safe']
|
|
1165
|
-
* @property {object|GenericArray|null} [parent=null]
|
|
1166
|
-
* @property {string|null} [parentProperty=null]
|
|
1167
|
-
* @property {JSONPathCallback} [callback]
|
|
1168
|
-
* @property {OtherTypeCallback} [otherTypeCallback] Defaults to
|
|
1169
|
-
* function which throws on encountering `@other`
|
|
1170
|
-
* @property {boolean} [autostart=true]
|
|
1171
|
-
*/
|
|
1172
|
-
/**
|
|
1173
|
-
* @param {string|JSONPathOptions} opts If a string, will be treated as `expr`
|
|
1174
|
-
* @param {string} [expr] JSON path to evaluate
|
|
1175
|
-
* @param {JSON} [obj] JSON object to evaluate against
|
|
1176
|
-
* @param {JSONPathCallback} [callback] Passed 3 arguments: 1) desired payload
|
|
1177
|
-
* per `resultType`, 2) `"value"|"property"`, 3) Full returned object with
|
|
1178
|
-
* all payloads
|
|
1179
|
-
* @param {OtherTypeCallback} [otherTypeCallback] If `@other()` is at the end
|
|
1180
|
-
* of one's query, this will be invoked with the value of the item, its
|
|
1181
|
-
* path, its parent, and its parent's property name, and it should return
|
|
1182
|
-
* a boolean indicating whether the supplied value belongs to the "other"
|
|
1183
|
-
* type or not (or it may handle transformations and return `false`).
|
|
1184
|
-
* @returns {JSONPath}
|
|
1185
|
-
* @class
|
|
1186
|
-
*/
|
|
1187
|
-
function JSONPath(opts, expr, obj, callback, otherTypeCallback) {
|
|
1188
|
-
if (!(this instanceof JSONPath)) try {
|
|
1189
|
-
return new JSONPath(opts, expr, obj, callback, otherTypeCallback);
|
|
1190
|
-
} catch (e) {
|
|
1191
|
-
if (!e.avoidNew) throw e;
|
|
1192
|
-
return e.value;
|
|
1193
|
-
}
|
|
1194
|
-
if (typeof opts === "string") {
|
|
1195
|
-
otherTypeCallback = callback;
|
|
1196
|
-
callback = obj;
|
|
1197
|
-
obj = expr;
|
|
1198
|
-
expr = opts;
|
|
1199
|
-
opts = null;
|
|
1200
|
-
}
|
|
1201
|
-
const optObj = opts && typeof opts === "object";
|
|
1202
|
-
opts = opts || {};
|
|
1203
|
-
this.json = opts.json || obj;
|
|
1204
|
-
this.path = opts.path || expr;
|
|
1205
|
-
this.resultType = opts.resultType || "value";
|
|
1206
|
-
this.flatten = opts.flatten || false;
|
|
1207
|
-
this.wrap = Object.hasOwn(opts, "wrap") ? opts.wrap : true;
|
|
1208
|
-
this.sandbox = opts.sandbox || {};
|
|
1209
|
-
this.eval = opts.eval === void 0 ? "safe" : opts.eval;
|
|
1210
|
-
this.ignoreEvalErrors = typeof opts.ignoreEvalErrors === "undefined" ? false : opts.ignoreEvalErrors;
|
|
1211
|
-
this.parent = opts.parent || null;
|
|
1212
|
-
this.parentProperty = opts.parentProperty || null;
|
|
1213
|
-
this.callback = opts.callback || callback || null;
|
|
1214
|
-
this.otherTypeCallback = opts.otherTypeCallback || otherTypeCallback || function() {
|
|
1215
|
-
throw new TypeError("You must supply an otherTypeCallback callback option with the @other() operator.");
|
|
1216
|
-
};
|
|
1217
|
-
if (opts.autostart !== false) {
|
|
1218
|
-
const args = { path: optObj ? opts.path : expr };
|
|
1219
|
-
if (!optObj) args.json = obj;
|
|
1220
|
-
else if ("json" in opts) args.json = opts.json;
|
|
1221
|
-
const ret = this.evaluate(args);
|
|
1222
|
-
if (!ret || typeof ret !== "object") throw new NewError(ret);
|
|
1223
|
-
return ret;
|
|
1224
|
-
}
|
|
1225
|
-
}
|
|
1226
|
-
JSONPath.prototype.evaluate = function(expr, json, callback, otherTypeCallback) {
|
|
1227
|
-
let currParent = this.parent, currParentProperty = this.parentProperty;
|
|
1228
|
-
let { flatten, wrap } = this;
|
|
1229
|
-
this.currResultType = this.resultType;
|
|
1230
|
-
this.currEval = this.eval;
|
|
1231
|
-
this.currSandbox = this.sandbox;
|
|
1232
|
-
callback = callback || this.callback;
|
|
1233
|
-
this.currOtherTypeCallback = otherTypeCallback || this.otherTypeCallback;
|
|
1234
|
-
json = json || this.json;
|
|
1235
|
-
expr = expr || this.path;
|
|
1236
|
-
if (expr && typeof expr === "object" && !Array.isArray(expr)) {
|
|
1237
|
-
if (!expr.path && expr.path !== "") throw new TypeError("You must supply a \"path\" property when providing an object argument to JSONPath.evaluate().");
|
|
1238
|
-
if (!Object.hasOwn(expr, "json")) throw new TypeError("You must supply a \"json\" property when providing an object argument to JSONPath.evaluate().");
|
|
1239
|
-
({json} = expr);
|
|
1240
|
-
flatten = Object.hasOwn(expr, "flatten") ? expr.flatten : flatten;
|
|
1241
|
-
this.currResultType = Object.hasOwn(expr, "resultType") ? expr.resultType : this.currResultType;
|
|
1242
|
-
this.currSandbox = Object.hasOwn(expr, "sandbox") ? expr.sandbox : this.currSandbox;
|
|
1243
|
-
wrap = Object.hasOwn(expr, "wrap") ? expr.wrap : wrap;
|
|
1244
|
-
this.currEval = Object.hasOwn(expr, "eval") ? expr.eval : this.currEval;
|
|
1245
|
-
callback = Object.hasOwn(expr, "callback") ? expr.callback : callback;
|
|
1246
|
-
this.currOtherTypeCallback = Object.hasOwn(expr, "otherTypeCallback") ? expr.otherTypeCallback : this.currOtherTypeCallback;
|
|
1247
|
-
currParent = Object.hasOwn(expr, "parent") ? expr.parent : currParent;
|
|
1248
|
-
currParentProperty = Object.hasOwn(expr, "parentProperty") ? expr.parentProperty : currParentProperty;
|
|
1249
|
-
expr = expr.path;
|
|
1250
|
-
}
|
|
1251
|
-
currParent = currParent || null;
|
|
1252
|
-
currParentProperty = currParentProperty || null;
|
|
1253
|
-
if (Array.isArray(expr)) expr = JSONPath.toPathString(expr);
|
|
1254
|
-
if (!expr && expr !== "" || !json) return;
|
|
1255
|
-
const exprList = JSONPath.toPathArray(expr);
|
|
1256
|
-
if (exprList[0] === "$" && exprList.length > 1) exprList.shift();
|
|
1257
|
-
this._hasParentSelector = null;
|
|
1258
|
-
const result = this._trace(exprList, json, ["$"], currParent, currParentProperty, callback).filter(function(ea) {
|
|
1259
|
-
return ea && !ea.isParentSelector;
|
|
1260
|
-
});
|
|
1261
|
-
if (!result.length) return wrap ? [] : void 0;
|
|
1262
|
-
if (!wrap && result.length === 1 && !result[0].hasArrExpr) return this._getPreferredOutput(result[0]);
|
|
1263
|
-
return result.reduce((rslt, ea) => {
|
|
1264
|
-
const valOrPath = this._getPreferredOutput(ea);
|
|
1265
|
-
if (flatten && Array.isArray(valOrPath)) rslt = rslt.concat(valOrPath);
|
|
1266
|
-
else rslt.push(valOrPath);
|
|
1267
|
-
return rslt;
|
|
1268
|
-
}, []);
|
|
1269
|
-
};
|
|
1270
|
-
JSONPath.prototype._getPreferredOutput = function(ea) {
|
|
1271
|
-
const resultType = this.currResultType;
|
|
1272
|
-
switch (resultType) {
|
|
1273
|
-
case "all": {
|
|
1274
|
-
const path = Array.isArray(ea.path) ? ea.path : JSONPath.toPathArray(ea.path);
|
|
1275
|
-
ea.pointer = JSONPath.toPointer(path);
|
|
1276
|
-
ea.path = typeof ea.path === "string" ? ea.path : JSONPath.toPathString(ea.path);
|
|
1277
|
-
return ea;
|
|
1278
|
-
}
|
|
1279
|
-
case "value":
|
|
1280
|
-
case "parent":
|
|
1281
|
-
case "parentProperty": return ea[resultType];
|
|
1282
|
-
case "path": return JSONPath.toPathString(ea[resultType]);
|
|
1283
|
-
case "pointer": return JSONPath.toPointer(ea.path);
|
|
1284
|
-
default: throw new TypeError("Unknown result type");
|
|
1285
|
-
}
|
|
1286
|
-
};
|
|
1287
|
-
JSONPath.prototype._handleCallback = function(fullRetObj, callback, type) {
|
|
1288
|
-
if (callback) {
|
|
1289
|
-
const preferredOutput = this._getPreferredOutput(fullRetObj);
|
|
1290
|
-
fullRetObj.path = typeof fullRetObj.path === "string" ? fullRetObj.path : JSONPath.toPathString(fullRetObj.path);
|
|
1291
|
-
callback(preferredOutput, type, fullRetObj);
|
|
1292
|
-
}
|
|
1293
|
-
};
|
|
1294
|
-
/**
|
|
1295
|
-
*
|
|
1296
|
-
* @param {string} expr
|
|
1297
|
-
* @param {JSONObject} val
|
|
1298
|
-
* @param {string} path
|
|
1299
|
-
* @param {object|GenericArray} parent
|
|
1300
|
-
* @param {string} parentPropName
|
|
1301
|
-
* @param {JSONPathCallback} callback
|
|
1302
|
-
* @param {boolean} hasArrExpr
|
|
1303
|
-
* @param {boolean} literalPriority
|
|
1304
|
-
* @returns {ReturnObject|ReturnObject[]}
|
|
1305
|
-
*/
|
|
1306
|
-
JSONPath.prototype._trace = function(expr, val, path, parent, parentPropName, callback, hasArrExpr, literalPriority) {
|
|
1307
|
-
let retObj;
|
|
1308
|
-
if (!expr.length) {
|
|
1309
|
-
retObj = {
|
|
1310
|
-
path,
|
|
1311
|
-
value: val,
|
|
1312
|
-
parent,
|
|
1313
|
-
parentProperty: parentPropName,
|
|
1314
|
-
hasArrExpr
|
|
1315
|
-
};
|
|
1316
|
-
this._handleCallback(retObj, callback, "value");
|
|
1317
|
-
return retObj;
|
|
1318
|
-
}
|
|
1319
|
-
const loc = expr[0], x = expr.slice(1);
|
|
1320
|
-
const ret = [];
|
|
1321
|
-
/**
|
|
1322
|
-
*
|
|
1323
|
-
* @param {ReturnObject|ReturnObject[]} elems
|
|
1324
|
-
* @returns {void}
|
|
1325
|
-
*/
|
|
1326
|
-
function addRet(elems) {
|
|
1327
|
-
if (Array.isArray(elems)) elems.forEach((t) => {
|
|
1328
|
-
ret.push(t);
|
|
1329
|
-
});
|
|
1330
|
-
else ret.push(elems);
|
|
1331
|
-
}
|
|
1332
|
-
if ((typeof loc !== "string" || literalPriority) && val && Object.hasOwn(val, loc)) addRet(this._trace(x, val[loc], push(path, loc), val, loc, callback, hasArrExpr));
|
|
1333
|
-
else if (loc === "*") this._walk(val, (m) => {
|
|
1334
|
-
addRet(this._trace(x, val[m], push(path, m), val, m, callback, true, true));
|
|
1335
|
-
});
|
|
1336
|
-
else if (loc === "..") {
|
|
1337
|
-
addRet(this._trace(x, val, path, parent, parentPropName, callback, hasArrExpr));
|
|
1338
|
-
this._walk(val, (m) => {
|
|
1339
|
-
if (typeof val[m] === "object") addRet(this._trace(expr.slice(), val[m], push(path, m), val, m, callback, true));
|
|
1340
|
-
});
|
|
1341
|
-
} else if (loc === "^") {
|
|
1342
|
-
this._hasParentSelector = true;
|
|
1343
|
-
return {
|
|
1344
|
-
path: path.slice(0, -1),
|
|
1345
|
-
expr: x,
|
|
1346
|
-
isParentSelector: true
|
|
1347
|
-
};
|
|
1348
|
-
} else if (loc === "~") {
|
|
1349
|
-
retObj = {
|
|
1350
|
-
path: push(path, loc),
|
|
1351
|
-
value: parentPropName,
|
|
1352
|
-
parent,
|
|
1353
|
-
parentProperty: null
|
|
1354
|
-
};
|
|
1355
|
-
this._handleCallback(retObj, callback, "property");
|
|
1356
|
-
return retObj;
|
|
1357
|
-
} else if (loc === "$") addRet(this._trace(x, val, path, null, null, callback, hasArrExpr));
|
|
1358
|
-
else if (/^(-?\d*):(-?\d*):?(\d*)$/u.test(loc)) addRet(this._slice(loc, x, val, path, parent, parentPropName, callback));
|
|
1359
|
-
else if (loc.indexOf("?(") === 0) {
|
|
1360
|
-
if (this.currEval === false) throw new Error("Eval [?(expr)] prevented in JSONPath expression.");
|
|
1361
|
-
const safeLoc = loc.replace(/^\?\((.*?)\)$/u, "$1");
|
|
1362
|
-
const nested = /@.?([^?]*)[['](\??\(.*?\))(?!.\)\])[\]']/gu.exec(safeLoc);
|
|
1363
|
-
if (nested) this._walk(val, (m) => {
|
|
1364
|
-
const npath = [nested[2]];
|
|
1365
|
-
const nvalue = nested[1] ? val[m][nested[1]] : val[m];
|
|
1366
|
-
if (this._trace(npath, nvalue, path, parent, parentPropName, callback, true).length > 0) addRet(this._trace(x, val[m], push(path, m), val, m, callback, true));
|
|
1367
|
-
});
|
|
1368
|
-
else this._walk(val, (m) => {
|
|
1369
|
-
if (this._eval(safeLoc, val[m], m, path, parent, parentPropName)) addRet(this._trace(x, val[m], push(path, m), val, m, callback, true));
|
|
1370
|
-
});
|
|
1371
|
-
} else if (loc[0] === "(") {
|
|
1372
|
-
if (this.currEval === false) throw new Error("Eval [(expr)] prevented in JSONPath expression.");
|
|
1373
|
-
addRet(this._trace(unshift(this._eval(loc, val, path.at(-1), path.slice(0, -1), parent, parentPropName), x), val, path, parent, parentPropName, callback, hasArrExpr));
|
|
1374
|
-
} else if (loc[0] === "@") {
|
|
1375
|
-
let addType = false;
|
|
1376
|
-
const valueType = loc.slice(1, -2);
|
|
1377
|
-
switch (valueType) {
|
|
1378
|
-
case "scalar":
|
|
1379
|
-
if (!val || !["object", "function"].includes(typeof val)) addType = true;
|
|
1380
|
-
break;
|
|
1381
|
-
case "boolean":
|
|
1382
|
-
case "string":
|
|
1383
|
-
case "undefined":
|
|
1384
|
-
case "function":
|
|
1385
|
-
if (typeof val === valueType) addType = true;
|
|
1386
|
-
break;
|
|
1387
|
-
case "integer":
|
|
1388
|
-
if (Number.isFinite(val) && !(val % 1)) addType = true;
|
|
1389
|
-
break;
|
|
1390
|
-
case "number":
|
|
1391
|
-
if (Number.isFinite(val)) addType = true;
|
|
1392
|
-
break;
|
|
1393
|
-
case "nonFinite":
|
|
1394
|
-
if (typeof val === "number" && !Number.isFinite(val)) addType = true;
|
|
1395
|
-
break;
|
|
1396
|
-
case "object":
|
|
1397
|
-
if (val && typeof val === valueType) addType = true;
|
|
1398
|
-
break;
|
|
1399
|
-
case "array":
|
|
1400
|
-
if (Array.isArray(val)) addType = true;
|
|
1401
|
-
break;
|
|
1402
|
-
case "other":
|
|
1403
|
-
addType = this.currOtherTypeCallback(val, path, parent, parentPropName);
|
|
1404
|
-
break;
|
|
1405
|
-
case "null":
|
|
1406
|
-
if (val === null) addType = true;
|
|
1407
|
-
break;
|
|
1408
|
-
/* c8 ignore next 2 */
|
|
1409
|
-
default: throw new TypeError("Unknown value type " + valueType);
|
|
1410
|
-
}
|
|
1411
|
-
if (addType) {
|
|
1412
|
-
retObj = {
|
|
1413
|
-
path,
|
|
1414
|
-
value: val,
|
|
1415
|
-
parent,
|
|
1416
|
-
parentProperty: parentPropName
|
|
1417
|
-
};
|
|
1418
|
-
this._handleCallback(retObj, callback, "value");
|
|
1419
|
-
return retObj;
|
|
1420
|
-
}
|
|
1421
|
-
} else if (loc[0] === "`" && val && Object.hasOwn(val, loc.slice(1))) {
|
|
1422
|
-
const locProp = loc.slice(1);
|
|
1423
|
-
addRet(this._trace(x, val[locProp], push(path, locProp), val, locProp, callback, hasArrExpr, true));
|
|
1424
|
-
} else if (loc.includes(",")) {
|
|
1425
|
-
const parts = loc.split(",");
|
|
1426
|
-
for (const part of parts) addRet(this._trace(unshift(part, x), val, path, parent, parentPropName, callback, true));
|
|
1427
|
-
} else if (!literalPriority && val && Object.hasOwn(val, loc)) addRet(this._trace(x, val[loc], push(path, loc), val, loc, callback, hasArrExpr, true));
|
|
1428
|
-
if (this._hasParentSelector) for (let t = 0; t < ret.length; t++) {
|
|
1429
|
-
const rett = ret[t];
|
|
1430
|
-
if (rett && rett.isParentSelector) {
|
|
1431
|
-
const tmp = this._trace(rett.expr, val, rett.path, parent, parentPropName, callback, hasArrExpr);
|
|
1432
|
-
if (Array.isArray(tmp)) {
|
|
1433
|
-
ret[t] = tmp[0];
|
|
1434
|
-
const tl = tmp.length;
|
|
1435
|
-
for (let tt = 1; tt < tl; tt++) {
|
|
1436
|
-
t++;
|
|
1437
|
-
ret.splice(t, 0, tmp[tt]);
|
|
1438
|
-
}
|
|
1439
|
-
} else ret[t] = tmp;
|
|
1440
|
-
}
|
|
1441
|
-
}
|
|
1442
|
-
return ret;
|
|
1443
|
-
};
|
|
1444
|
-
JSONPath.prototype._walk = function(val, f) {
|
|
1445
|
-
if (Array.isArray(val)) {
|
|
1446
|
-
const n = val.length;
|
|
1447
|
-
for (let i = 0; i < n; i++) f(i);
|
|
1448
|
-
} else if (val && typeof val === "object") Object.keys(val).forEach((m) => {
|
|
1449
|
-
f(m);
|
|
1450
|
-
});
|
|
1451
|
-
};
|
|
1452
|
-
JSONPath.prototype._slice = function(loc, expr, val, path, parent, parentPropName, callback) {
|
|
1453
|
-
if (!Array.isArray(val)) return;
|
|
1454
|
-
const len = val.length, parts = loc.split(":"), step = parts[2] && Number.parseInt(parts[2]) || 1;
|
|
1455
|
-
let start = parts[0] && Number.parseInt(parts[0]) || 0, end = parts[1] && Number.parseInt(parts[1]) || len;
|
|
1456
|
-
start = start < 0 ? Math.max(0, start + len) : Math.min(len, start);
|
|
1457
|
-
end = end < 0 ? Math.max(0, end + len) : Math.min(len, end);
|
|
1458
|
-
const ret = [];
|
|
1459
|
-
for (let i = start; i < end; i += step) this._trace(unshift(i, expr), val, path, parent, parentPropName, callback, true).forEach((t) => {
|
|
1460
|
-
ret.push(t);
|
|
1461
|
-
});
|
|
1462
|
-
return ret;
|
|
1463
|
-
};
|
|
1464
|
-
JSONPath.prototype._eval = function(code, _v, _vname, path, parent, parentPropName) {
|
|
1465
|
-
this.currSandbox._$_parentProperty = parentPropName;
|
|
1466
|
-
this.currSandbox._$_parent = parent;
|
|
1467
|
-
this.currSandbox._$_property = _vname;
|
|
1468
|
-
this.currSandbox._$_root = this.json;
|
|
1469
|
-
this.currSandbox._$_v = _v;
|
|
1470
|
-
const containsPath = code.includes("@path");
|
|
1471
|
-
if (containsPath) this.currSandbox._$_path = JSONPath.toPathString(path.concat([_vname]));
|
|
1472
|
-
const scriptCacheKey = this.currEval + "Script:" + code;
|
|
1473
|
-
if (!JSONPath.cache[scriptCacheKey]) {
|
|
1474
|
-
let script = code.replaceAll("@parentProperty", "_$_parentProperty").replaceAll("@parent", "_$_parent").replaceAll("@property", "_$_property").replaceAll("@root", "_$_root").replaceAll(/@([.\s)[])/gu, "_$_v$1");
|
|
1475
|
-
if (containsPath) script = script.replaceAll("@path", "_$_path");
|
|
1476
|
-
if (this.currEval === "safe" || this.currEval === true || this.currEval === void 0) JSONPath.cache[scriptCacheKey] = new this.safeVm.Script(script);
|
|
1477
|
-
else if (this.currEval === "native") JSONPath.cache[scriptCacheKey] = new this.vm.Script(script);
|
|
1478
|
-
else if (typeof this.currEval === "function" && this.currEval.prototype && Object.hasOwn(this.currEval.prototype, "runInNewContext")) {
|
|
1479
|
-
const CurrEval = this.currEval;
|
|
1480
|
-
JSONPath.cache[scriptCacheKey] = new CurrEval(script);
|
|
1481
|
-
} else if (typeof this.currEval === "function") JSONPath.cache[scriptCacheKey] = { runInNewContext: (context) => this.currEval(script, context) };
|
|
1482
|
-
else throw new TypeError(`Unknown "eval" property "${this.currEval}"`);
|
|
1483
|
-
}
|
|
1484
|
-
try {
|
|
1485
|
-
return JSONPath.cache[scriptCacheKey].runInNewContext(this.currSandbox);
|
|
1486
|
-
} catch (e) {
|
|
1487
|
-
if (this.ignoreEvalErrors) return false;
|
|
1488
|
-
throw new Error("jsonPath: " + e.message + ": " + code);
|
|
1489
|
-
}
|
|
1490
|
-
};
|
|
1491
|
-
JSONPath.cache = {};
|
|
1492
|
-
/**
|
|
1493
|
-
* @param {string[]} pathArr Array to convert
|
|
1494
|
-
* @returns {string} The path string
|
|
1495
|
-
*/
|
|
1496
|
-
JSONPath.toPathString = function(pathArr) {
|
|
1497
|
-
const x = pathArr, n = x.length;
|
|
1498
|
-
let p = "$";
|
|
1499
|
-
for (let i = 1; i < n; i++) if (!/^(~|\^|@.*?\(\))$/u.test(x[i])) p += /^[0-9*]+$/u.test(x[i]) ? "[" + x[i] + "]" : "['" + x[i] + "']";
|
|
1500
|
-
return p;
|
|
1501
|
-
};
|
|
1502
|
-
/**
|
|
1503
|
-
* @param {string} pointer JSON Path
|
|
1504
|
-
* @returns {string} JSON Pointer
|
|
1505
|
-
*/
|
|
1506
|
-
JSONPath.toPointer = function(pointer) {
|
|
1507
|
-
const x = pointer, n = x.length;
|
|
1508
|
-
let p = "";
|
|
1509
|
-
for (let i = 1; i < n; i++) if (!/^(~|\^|@.*?\(\))$/u.test(x[i])) p += "/" + x[i].toString().replaceAll("~", "~0").replaceAll("/", "~1");
|
|
1510
|
-
return p;
|
|
1511
|
-
};
|
|
1512
|
-
/**
|
|
1513
|
-
* @param {string} expr Expression to convert
|
|
1514
|
-
* @returns {string[]}
|
|
1515
|
-
*/
|
|
1516
|
-
JSONPath.toPathArray = function(expr) {
|
|
1517
|
-
const { cache } = JSONPath;
|
|
1518
|
-
if (cache[expr]) return cache[expr].concat();
|
|
1519
|
-
const subx = [];
|
|
1520
|
-
cache[expr] = expr.replaceAll(/@(?:null|boolean|number|string|integer|undefined|nonFinite|scalar|array|object|function|other)\(\)/gu, ";$&;").replaceAll(/[['](\??\(.*?\))[\]'](?!.\])/gu, function($0, $1) {
|
|
1521
|
-
return "[#" + (subx.push($1) - 1) + "]";
|
|
1522
|
-
}).replaceAll(/\[['"]([^'\]]*)['"]\]/gu, function($0, prop) {
|
|
1523
|
-
return "['" + prop.replaceAll(".", "%@%").replaceAll("~", "%%@@%%") + "']";
|
|
1524
|
-
}).replaceAll("~", ";~;").replaceAll(/['"]?\.['"]?(?![^[]*\])|\[['"]?/gu, ";").replaceAll("%@%", ".").replaceAll("%%@@%%", "~").replaceAll(/(?:;)?(\^+)(?:;)?/gu, function($0, ups) {
|
|
1525
|
-
return ";" + ups.split("").join(";") + ";";
|
|
1526
|
-
}).replaceAll(/;;;|;;/gu, ";..;").replaceAll(/;$|'?\]|'$/gu, "").split(";").map(function(exp) {
|
|
1527
|
-
const match = exp.match(/#(\d+)/u);
|
|
1528
|
-
return !match || !match[1] ? exp : subx[match[1]];
|
|
1529
|
-
});
|
|
1530
|
-
return cache[expr].concat();
|
|
1531
|
-
};
|
|
1532
|
-
JSONPath.prototype.safeVm = { Script: SafeScript };
|
|
1533
|
-
JSONPath.prototype.vm = vm;
|
|
1534
|
-
//#endregion
|
|
1535
|
-
export { JSONPath };
|
|
1536
|
-
|
|
1537
|
-
//# sourceMappingURL=index-node-esm-D6YQRKEz.mjs.map
|