@moostjs/event-cli 0.2.0 → 0.2.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/index.cjs +33 -2675
- package/dist/index.mjs +26 -2668
- package/package.json +9 -2
package/dist/index.cjs
CHANGED
|
@@ -1,2285 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var eventCli = require('@wooksjs/event-cli');
|
|
3
4
|
var crypto = require('crypto');
|
|
4
|
-
require('
|
|
5
|
-
require('stream');
|
|
6
|
-
require('http');
|
|
7
|
-
|
|
8
|
-
/******************************************************************************
|
|
9
|
-
Copyright (c) Microsoft Corporation.
|
|
10
|
-
|
|
11
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
12
|
-
purpose with or without fee is hereby granted.
|
|
13
|
-
|
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
15
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
16
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
17
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
18
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
19
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
20
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
21
|
-
***************************************************************************** */
|
|
22
|
-
|
|
23
|
-
function __awaiter$1(thisArg, _arguments, P, generator) {
|
|
24
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
25
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
26
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
27
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
28
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
29
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const banner$4 = () => `[${"@wooksjs/event-core"}][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `;
|
|
34
|
-
|
|
35
|
-
/* istanbul ignore file */
|
|
36
|
-
function logError$2(error) {
|
|
37
|
-
console.error('[91m' + '[1m' + banner$4() + error + '[0m');
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function panic$1(error) {
|
|
41
|
-
logError$2(error);
|
|
42
|
-
return new Error(error);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
46
|
-
function attachHook(target, opts, name) {
|
|
47
|
-
Object.defineProperty(target, name || 'value', {
|
|
48
|
-
get: opts.get,
|
|
49
|
-
set: opts.set,
|
|
50
|
-
});
|
|
51
|
-
return target;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
let currentContext = null;
|
|
55
|
-
/**
|
|
56
|
-
* Create a new event context
|
|
57
|
-
*
|
|
58
|
-
* @param data
|
|
59
|
-
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
60
|
-
*/
|
|
61
|
-
function createEventContext(data) {
|
|
62
|
-
const newContext = Object.assign({}, data);
|
|
63
|
-
currentContext = newContext;
|
|
64
|
-
return _getCtxHelpers(newContext);
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Use existing event context
|
|
68
|
-
*
|
|
69
|
-
* !Must be called syncronously while context is reachable
|
|
70
|
-
*
|
|
71
|
-
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
72
|
-
*/
|
|
73
|
-
function useEventContext(expectedTypes) {
|
|
74
|
-
var _a;
|
|
75
|
-
if (!currentContext) {
|
|
76
|
-
throw panic$1('Event context does not exist. Use event context synchronously within the runtime of the event.');
|
|
77
|
-
}
|
|
78
|
-
const cc = currentContext;
|
|
79
|
-
if (expectedTypes || typeof expectedTypes === 'string') {
|
|
80
|
-
const type = (_a = cc.event) === null || _a === void 0 ? void 0 : _a.type;
|
|
81
|
-
const types = typeof expectedTypes === 'string' ? [expectedTypes] : expectedTypes;
|
|
82
|
-
if (!types.includes(type))
|
|
83
|
-
panic$1(`Event context type mismatch: expected ${types.map(t => `"${t}"`).join(', ')}, received "${type}"`);
|
|
84
|
-
}
|
|
85
|
-
return _getCtxHelpers(cc);
|
|
86
|
-
}
|
|
87
|
-
function _getCtxHelpers(cc) {
|
|
88
|
-
/**
|
|
89
|
-
* Hook to an event store property
|
|
90
|
-
*
|
|
91
|
-
* @param key store property key
|
|
92
|
-
* @returns a hook { value: <prop value>, hook: (key2: keyof <prop value>) => { value: <nested prop value> }, ... }
|
|
93
|
-
*/
|
|
94
|
-
function store(key) {
|
|
95
|
-
const obj = {
|
|
96
|
-
value: null,
|
|
97
|
-
hook,
|
|
98
|
-
init,
|
|
99
|
-
set: setNested,
|
|
100
|
-
get: getNested,
|
|
101
|
-
has: hasNested,
|
|
102
|
-
del: delNested,
|
|
103
|
-
entries,
|
|
104
|
-
clear,
|
|
105
|
-
};
|
|
106
|
-
attachHook(obj, {
|
|
107
|
-
set: v => set(key, v),
|
|
108
|
-
get: () => get(key),
|
|
109
|
-
});
|
|
110
|
-
function init(key2, getter) {
|
|
111
|
-
if (hasNested(key2))
|
|
112
|
-
return getNested(key2);
|
|
113
|
-
return setNested(key2, getter());
|
|
114
|
-
}
|
|
115
|
-
function hook(key2) {
|
|
116
|
-
const obj = {
|
|
117
|
-
value: null,
|
|
118
|
-
isDefined: null,
|
|
119
|
-
};
|
|
120
|
-
attachHook(obj, {
|
|
121
|
-
set: v => setNested(key2, v),
|
|
122
|
-
get: () => getNested(key2),
|
|
123
|
-
});
|
|
124
|
-
attachHook(obj, {
|
|
125
|
-
get: () => hasNested(key2),
|
|
126
|
-
}, 'isDefined');
|
|
127
|
-
return obj;
|
|
128
|
-
}
|
|
129
|
-
function setNested(key2, v) {
|
|
130
|
-
if (typeof obj.value === 'undefined') {
|
|
131
|
-
obj.value = {};
|
|
132
|
-
}
|
|
133
|
-
obj.value[key2] = v;
|
|
134
|
-
return v;
|
|
135
|
-
}
|
|
136
|
-
function delNested(key2) {
|
|
137
|
-
setNested(key2, undefined);
|
|
138
|
-
}
|
|
139
|
-
function getNested(key2) { return (obj.value || {})[key2]; }
|
|
140
|
-
function hasNested(key2) { return typeof (obj.value || {})[key2] !== 'undefined'; }
|
|
141
|
-
function entries() { return Object.entries((obj.value || {})); }
|
|
142
|
-
function clear() { obj.value = {}; }
|
|
143
|
-
return obj;
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Get event context object
|
|
147
|
-
*
|
|
148
|
-
* @returns whole context object
|
|
149
|
-
*/
|
|
150
|
-
function getCtx() { return cc; }
|
|
151
|
-
/**
|
|
152
|
-
* Get value of event store property
|
|
153
|
-
*
|
|
154
|
-
* @param key property name
|
|
155
|
-
* @returns value of property by name
|
|
156
|
-
*/
|
|
157
|
-
function get(key) { return getCtx()[key]; }
|
|
158
|
-
/**
|
|
159
|
-
* Set value of event store property
|
|
160
|
-
*
|
|
161
|
-
* @param key property name
|
|
162
|
-
* @param v property value
|
|
163
|
-
*/
|
|
164
|
-
function set(key, v) {
|
|
165
|
-
(getCtx())[key] = v;
|
|
166
|
-
}
|
|
167
|
-
return {
|
|
168
|
-
getCtx,
|
|
169
|
-
restoreCtx: () => currentContext = cc,
|
|
170
|
-
clearCtx: () => cc === currentContext ? currentContext = null : null,
|
|
171
|
-
store,
|
|
172
|
-
getStore: get,
|
|
173
|
-
setStore: set,
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
function useEventId() {
|
|
178
|
-
const { store } = useEventContext();
|
|
179
|
-
const { init } = store('event');
|
|
180
|
-
const getId = () => init('id', () => crypto.randomUUID());
|
|
181
|
-
return { getId };
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
class ProstoCache {
|
|
185
|
-
constructor(options) {
|
|
186
|
-
this.data = {};
|
|
187
|
-
this.limits = [];
|
|
188
|
-
this.expireOrder = [];
|
|
189
|
-
this.expireSeries = {};
|
|
190
|
-
this.options = {
|
|
191
|
-
limit: 1000,
|
|
192
|
-
...options,
|
|
193
|
-
};
|
|
194
|
-
}
|
|
195
|
-
set(key, value) {
|
|
196
|
-
var _a, _b, _c;
|
|
197
|
-
if (((_a = this.options) === null || _a === void 0 ? void 0 : _a.limit) === 0)
|
|
198
|
-
return;
|
|
199
|
-
const expires = ((_b = this.options) === null || _b === void 0 ? void 0 : _b.ttl) ? Math.round(new Date().getTime() / 1) + ((_c = this.options) === null || _c === void 0 ? void 0 : _c.ttl) : null;
|
|
200
|
-
if (expires) {
|
|
201
|
-
this.del(key);
|
|
202
|
-
}
|
|
203
|
-
this.data[key] = {
|
|
204
|
-
value: value,
|
|
205
|
-
expires,
|
|
206
|
-
};
|
|
207
|
-
if (expires) {
|
|
208
|
-
this.pushExpires(key, expires);
|
|
209
|
-
}
|
|
210
|
-
this.pushLimit(key);
|
|
211
|
-
}
|
|
212
|
-
get(key) {
|
|
213
|
-
var _a;
|
|
214
|
-
return (_a = this.data[key]) === null || _a === void 0 ? void 0 : _a.value;
|
|
215
|
-
}
|
|
216
|
-
del(key) {
|
|
217
|
-
const entry = this.data[key];
|
|
218
|
-
if (entry) {
|
|
219
|
-
delete this.data[key];
|
|
220
|
-
if (entry.expires) {
|
|
221
|
-
let es = this.expireSeries[entry.expires];
|
|
222
|
-
if (es) {
|
|
223
|
-
es = this.expireSeries[entry.expires] = es.filter(k => k !== key);
|
|
224
|
-
}
|
|
225
|
-
if (!es || !es.length) {
|
|
226
|
-
delete this.expireSeries[entry.expires];
|
|
227
|
-
const { found, index } = this.searchExpireOrder(entry.expires);
|
|
228
|
-
if (found) {
|
|
229
|
-
this.expireOrder.splice(index, 1);
|
|
230
|
-
if (index === 0) {
|
|
231
|
-
console.log('calling prepareTimeout');
|
|
232
|
-
this.prepareTimeout();
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
reset() {
|
|
240
|
-
this.data = {};
|
|
241
|
-
if (this.nextTimeout) {
|
|
242
|
-
clearTimeout(this.nextTimeout);
|
|
243
|
-
}
|
|
244
|
-
this.expireOrder = [];
|
|
245
|
-
this.expireSeries = {};
|
|
246
|
-
this.limits = [];
|
|
247
|
-
}
|
|
248
|
-
searchExpireOrder(time) {
|
|
249
|
-
return binarySearch(this.expireOrder, time);
|
|
250
|
-
}
|
|
251
|
-
pushLimit(key) {
|
|
252
|
-
var _a;
|
|
253
|
-
const limit = (_a = this.options) === null || _a === void 0 ? void 0 : _a.limit;
|
|
254
|
-
if (limit) {
|
|
255
|
-
const newObj = [key, ...(this.limits.filter(item => item !== key && this.data[item]))];
|
|
256
|
-
const tail = newObj.slice(limit);
|
|
257
|
-
this.limits = newObj.slice(0, limit);
|
|
258
|
-
if (tail.length) {
|
|
259
|
-
tail.forEach(tailItem => {
|
|
260
|
-
this.del(tailItem);
|
|
261
|
-
});
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
prepareTimeout() {
|
|
266
|
-
if (this.nextTimeout) {
|
|
267
|
-
clearTimeout(this.nextTimeout);
|
|
268
|
-
}
|
|
269
|
-
const time = this.expireOrder[0];
|
|
270
|
-
const del = (time) => {
|
|
271
|
-
for (const key of (this.expireSeries[time] || [])) {
|
|
272
|
-
delete this.data[key];
|
|
273
|
-
}
|
|
274
|
-
delete this.expireSeries[time];
|
|
275
|
-
this.expireOrder = this.expireOrder.slice(1);
|
|
276
|
-
this.prepareTimeout();
|
|
277
|
-
};
|
|
278
|
-
if (time) {
|
|
279
|
-
const delta = time - Math.round(new Date().getTime() / 1);
|
|
280
|
-
if (delta > 0) {
|
|
281
|
-
this.nextTimeout = setTimeout(() => {
|
|
282
|
-
del(time);
|
|
283
|
-
}, delta);
|
|
284
|
-
}
|
|
285
|
-
else {
|
|
286
|
-
del(time);
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
pushExpires(key, time) {
|
|
291
|
-
const { found, index } = this.searchExpireOrder(time);
|
|
292
|
-
if (!found) {
|
|
293
|
-
this.expireOrder.splice(index, 0, time);
|
|
294
|
-
}
|
|
295
|
-
const e = this.expireSeries[time] = this.expireSeries[time] || [];
|
|
296
|
-
e.push(key);
|
|
297
|
-
if (!found && index === 0) {
|
|
298
|
-
this.prepareTimeout();
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
function binarySearch(a, n) {
|
|
303
|
-
let start = 0;
|
|
304
|
-
let end = a.length - 1;
|
|
305
|
-
let mid = 0;
|
|
306
|
-
while (start <= end) {
|
|
307
|
-
mid = Math.floor((start + end) / 2);
|
|
308
|
-
if (a[mid] === n) {
|
|
309
|
-
return {
|
|
310
|
-
found: true,
|
|
311
|
-
index: mid,
|
|
312
|
-
};
|
|
313
|
-
}
|
|
314
|
-
if (n < a[mid]) {
|
|
315
|
-
end = mid - 1;
|
|
316
|
-
mid--;
|
|
317
|
-
}
|
|
318
|
-
else {
|
|
319
|
-
start = mid + 1;
|
|
320
|
-
mid++;
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
return {
|
|
324
|
-
found: false,
|
|
325
|
-
index: mid,
|
|
326
|
-
};
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
const dim = '[2m';
|
|
330
|
-
const reset = '[0m';
|
|
331
|
-
class ProstoTree {
|
|
332
|
-
constructor(options) {
|
|
333
|
-
var _a, _b, _c, _d;
|
|
334
|
-
const label = (options === null || options === void 0 ? void 0 : options.label) || 'label';
|
|
335
|
-
const children = (options === null || options === void 0 ? void 0 : options.children) || 'children';
|
|
336
|
-
const branchWidth = typeof (options === null || options === void 0 ? void 0 : options.branchWidth) === 'number' ? options === null || options === void 0 ? void 0 : options.branchWidth : 2;
|
|
337
|
-
const hLine = (((_a = options === null || options === void 0 ? void 0 : options.branches) === null || _a === void 0 ? void 0 : _a.hLine) || '─').repeat(branchWidth - 1);
|
|
338
|
-
const branches = {
|
|
339
|
-
end: ((_b = options === null || options === void 0 ? void 0 : options.branches) === null || _b === void 0 ? void 0 : _b.end) || dim + '└',
|
|
340
|
-
middle: ((_c = options === null || options === void 0 ? void 0 : options.branches) === null || _c === void 0 ? void 0 : _c.middle) || dim + '├',
|
|
341
|
-
vLine: ((_d = options === null || options === void 0 ? void 0 : options.branches) === null || _d === void 0 ? void 0 : _d.vLine) || dim + '│',
|
|
342
|
-
hLine,
|
|
343
|
-
};
|
|
344
|
-
this.options = {
|
|
345
|
-
label: label,
|
|
346
|
-
children: children,
|
|
347
|
-
renderLabel: (options === null || options === void 0 ? void 0 : options.renderLabel) || (n => typeof n === 'string' ? n : n[label]),
|
|
348
|
-
branches,
|
|
349
|
-
branchWidth,
|
|
350
|
-
};
|
|
351
|
-
}
|
|
352
|
-
_render(root, opts) {
|
|
353
|
-
const { children, renderLabel } = this.options;
|
|
354
|
-
let s = `${renderLabel(root, '')}\n`;
|
|
355
|
-
const { end, middle, vLine, hLine } = this.options.branches;
|
|
356
|
-
const endBranch = end + hLine + reset + ' ';
|
|
357
|
-
const middleBranch = middle + hLine + reset + ' ';
|
|
358
|
-
const { branchWidth } = this.options;
|
|
359
|
-
if (root) {
|
|
360
|
-
treeNode(root);
|
|
361
|
-
}
|
|
362
|
-
function treeNode(node, behind = '', level = 1) {
|
|
363
|
-
const items = (node && node[children]);
|
|
364
|
-
const count = items && items.length || 0;
|
|
365
|
-
if (items) {
|
|
366
|
-
if ((opts === null || opts === void 0 ? void 0 : opts.level) && opts.level < level) {
|
|
367
|
-
s += behind + endBranch + renderCollapsedChildren(items.length) + '\n';
|
|
368
|
-
}
|
|
369
|
-
else {
|
|
370
|
-
let itemsToRender = items;
|
|
371
|
-
const collapsedCount = Math.max(0, count - ((opts === null || opts === void 0 ? void 0 : opts.childrenLimit) || count));
|
|
372
|
-
if ((opts === null || opts === void 0 ? void 0 : opts.childrenLimit) && count > opts.childrenLimit) {
|
|
373
|
-
itemsToRender = opts.showLast ? items.slice(count - opts.childrenLimit) : items.slice(0, opts.childrenLimit);
|
|
374
|
-
}
|
|
375
|
-
if (collapsedCount && (opts === null || opts === void 0 ? void 0 : opts.showLast))
|
|
376
|
-
s += behind + middleBranch + renderCollapsedChildren(collapsedCount) + '\n';
|
|
377
|
-
itemsToRender.forEach((childNode, i) => {
|
|
378
|
-
const last = i + 1 === count;
|
|
379
|
-
const branch = last ? endBranch : middleBranch;
|
|
380
|
-
const nextBehind = behind + (last ? ' ' : vLine) + ' '.repeat(branchWidth);
|
|
381
|
-
s += behind + branch + renderLabel(childNode, nextBehind) + '\n';
|
|
382
|
-
if (typeof childNode === 'object') {
|
|
383
|
-
treeNode(childNode, nextBehind, level + 1);
|
|
384
|
-
}
|
|
385
|
-
});
|
|
386
|
-
if (collapsedCount && !(opts === null || opts === void 0 ? void 0 : opts.showLast))
|
|
387
|
-
s += behind + endBranch + renderCollapsedChildren(collapsedCount) + '\n';
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
return s;
|
|
392
|
-
}
|
|
393
|
-
render(root, opts) {
|
|
394
|
-
return this._render(root, opts);
|
|
395
|
-
}
|
|
396
|
-
print(root, opts) {
|
|
397
|
-
const s = this.render(root, opts);
|
|
398
|
-
console.log(s);
|
|
399
|
-
return s;
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
function renderCollapsedChildren(count) {
|
|
403
|
-
return dim + '+ ' + '[3m' + count.toString() + ` item${count === 1 ? '' : 's'}` + reset;
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
function renderCodeFragment(lines, options) {
|
|
407
|
-
const row = (options.row || 1) - 1;
|
|
408
|
-
const rowEnd = options.rowEnd ? options.rowEnd - 1 : -1;
|
|
409
|
-
const limit = Math.min(options.limit || 6, lines.length);
|
|
410
|
-
const offset = Math.min(options.offset || 3, lines.length);
|
|
411
|
-
const error = options.error;
|
|
412
|
-
const errorEnd = options.errorEnd;
|
|
413
|
-
let output = '';
|
|
414
|
-
const delta = rowEnd - row;
|
|
415
|
-
const maxIndex = lines.length;
|
|
416
|
-
if (delta > limit) {
|
|
417
|
-
let longestLine = 0;
|
|
418
|
-
const newLimit = Math.floor(limit / 2);
|
|
419
|
-
for (let i = 0; i < newLimit + offset; i++) {
|
|
420
|
-
const index = row + i - offset;
|
|
421
|
-
const line = lines[index] || '';
|
|
422
|
-
longestLine = Math.max(line.length, longestLine);
|
|
423
|
-
output += renderLine(line, index < maxIndex ? index + 1 : '', index === row ? error : undefined, index === row || index === rowEnd ? 'bold' : '');
|
|
424
|
-
}
|
|
425
|
-
let output2 = '';
|
|
426
|
-
for (let i = newLimit + offset; i > 0; i--) {
|
|
427
|
-
const index = rowEnd - i + offset;
|
|
428
|
-
const line = lines[index] || '';
|
|
429
|
-
longestLine = Math.max(line.length, longestLine);
|
|
430
|
-
output2 += renderLine(line, index < maxIndex ? index + 1 : '', index === rowEnd ? errorEnd : undefined, index === row || index === rowEnd ? 'bold' : '');
|
|
431
|
-
}
|
|
432
|
-
output += renderLine('—'.repeat(longestLine), '———', undefined, 'dim') + output2;
|
|
433
|
-
}
|
|
434
|
-
else {
|
|
435
|
-
for (let i = 0; i < limit + offset; i++) {
|
|
436
|
-
const index = row + i - offset;
|
|
437
|
-
if (index <= lines.length + 1) {
|
|
438
|
-
const errorCol = index === row ? error : index === rowEnd ? errorEnd : undefined;
|
|
439
|
-
output += renderLine(lines[index], index < maxIndex ? index + 1 : '', errorCol, index === row || index === rowEnd ? 'bold' : '');
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
return output;
|
|
444
|
-
}
|
|
445
|
-
function lineStyles(s) {
|
|
446
|
-
return '[94m' + (s || '')
|
|
447
|
-
.replace(/([a-z_]+)/ig, '[32m' + '$1' + '\x1b[39;33m')
|
|
448
|
-
.replace(/([\=\.\/'"`\:\+]+)/ig, '[36m' + '$1' + '[39m');
|
|
449
|
-
}
|
|
450
|
-
function lineStylesError(s) {
|
|
451
|
-
return '[31m' + (s || '') + '[0m';
|
|
452
|
-
}
|
|
453
|
-
function renderLine(line, index, error, style = '') {
|
|
454
|
-
const st = (style === 'bold' ? '[1m' : '') + (style === 'dim' ? '[2m' : '');
|
|
455
|
-
if (typeof error === 'number') {
|
|
456
|
-
const errorLength = (/[\.-\s\(\)\*\/\+\{\}\[\]\?\'\"\`\<\>]/.exec(line.slice(error + 1)) || { index: line.length - error }).index + 1;
|
|
457
|
-
return renderLineNumber(index, true) +
|
|
458
|
-
st +
|
|
459
|
-
lineStyles(line.slice(0, error)) +
|
|
460
|
-
lineStylesError(line.slice(error, error + errorLength)) +
|
|
461
|
-
lineStyles(line.slice(error + errorLength)) +
|
|
462
|
-
renderLineNumber('', true) + ' '.repeat(error) + '[31m' + '[1m' + '~'.repeat(Math.max(1, errorLength));
|
|
463
|
-
}
|
|
464
|
-
return renderLineNumber(index, undefined, style === 'bold') + st + lineStyles(line);
|
|
465
|
-
}
|
|
466
|
-
function renderLineNumber(i, isError = false, bold = false) {
|
|
467
|
-
let s = ' ';
|
|
468
|
-
const sep = i === '———';
|
|
469
|
-
if (i && i > 0 || typeof i === 'string') {
|
|
470
|
-
s = ' ' + String(i);
|
|
471
|
-
}
|
|
472
|
-
s = s.slice(s.length - 5);
|
|
473
|
-
return '\n' + '[0m' + (sep ? '[34m' : '') +
|
|
474
|
-
(isError ? '[31m' + '[1m' : (bold ? '[1m' : '[2m')) +
|
|
475
|
-
s + (sep ? i : ' | ') + '[0m';
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
function escapeRegex$1(s) {
|
|
479
|
-
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
class ProstoParserNodeBase {
|
|
483
|
-
addRecognizes(...args) {
|
|
484
|
-
for (const node of args) {
|
|
485
|
-
if (!this.options.recognizes)
|
|
486
|
-
this.options.recognizes = [];
|
|
487
|
-
if (!this.options.recognizes.includes(node)) {
|
|
488
|
-
this.options.recognizes.push(node);
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
return this;
|
|
492
|
-
}
|
|
493
|
-
addAbsorbs(node, rule = 'append') {
|
|
494
|
-
this.options.absorbs = this.options.absorbs || {};
|
|
495
|
-
if (Array.isArray(node)) {
|
|
496
|
-
node.forEach(n => {
|
|
497
|
-
this.options.absorbs[n.id] = rule;
|
|
498
|
-
this.addRecognizes(n);
|
|
499
|
-
});
|
|
500
|
-
}
|
|
501
|
-
else {
|
|
502
|
-
this.options.absorbs[node.id] = rule;
|
|
503
|
-
this.addRecognizes(node);
|
|
504
|
-
}
|
|
505
|
-
return this;
|
|
506
|
-
}
|
|
507
|
-
addPopsAfterNode(...args) {
|
|
508
|
-
for (const node of args) {
|
|
509
|
-
if (!this.options.popsAfterNode)
|
|
510
|
-
this.options.popsAfterNode = [];
|
|
511
|
-
if (!this.options.popsAfterNode.includes(node)) {
|
|
512
|
-
this.options.popsAfterNode.push(node);
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
this.addRecognizes(...args);
|
|
516
|
-
return this;
|
|
517
|
-
}
|
|
518
|
-
addHoistChildren(...args) {
|
|
519
|
-
if (!this.options.hoistChildren)
|
|
520
|
-
this.options.hoistChildren = [];
|
|
521
|
-
this.options.hoistChildren.push(...args);
|
|
522
|
-
return this;
|
|
523
|
-
}
|
|
524
|
-
set icon(value) {
|
|
525
|
-
this.options.icon = value;
|
|
526
|
-
}
|
|
527
|
-
set label(value) {
|
|
528
|
-
this.options.label = value;
|
|
529
|
-
}
|
|
530
|
-
get startsWith() {
|
|
531
|
-
return this.options.startsWith;
|
|
532
|
-
}
|
|
533
|
-
set startsWith(value) {
|
|
534
|
-
this.options.startsWith = value;
|
|
535
|
-
}
|
|
536
|
-
get endsWith() {
|
|
537
|
-
return this.options.endsWith;
|
|
538
|
-
}
|
|
539
|
-
set endsWith(value) {
|
|
540
|
-
this.options.endsWith = value;
|
|
541
|
-
}
|
|
542
|
-
getPopsAtEOFSource() {
|
|
543
|
-
return this.options.popsAtEOFSource;
|
|
544
|
-
}
|
|
545
|
-
popsAtEOFSource(value) {
|
|
546
|
-
this.options.popsAtEOFSource = value;
|
|
547
|
-
return this;
|
|
548
|
-
}
|
|
549
|
-
get absorbs() {
|
|
550
|
-
return this.options.absorbs;
|
|
551
|
-
}
|
|
552
|
-
get badToken() {
|
|
553
|
-
return this.options.badToken;
|
|
554
|
-
}
|
|
555
|
-
set badToken(value) {
|
|
556
|
-
this.options.badToken = value;
|
|
557
|
-
}
|
|
558
|
-
get skipToken() {
|
|
559
|
-
return this.options.skipToken;
|
|
560
|
-
}
|
|
561
|
-
set skipToken(value) {
|
|
562
|
-
this.options.skipToken = value;
|
|
563
|
-
}
|
|
564
|
-
get recognizes() {
|
|
565
|
-
return this.options.recognizes || [];
|
|
566
|
-
}
|
|
567
|
-
set recognizes(value) {
|
|
568
|
-
this.options.recognizes = value;
|
|
569
|
-
}
|
|
570
|
-
get hoistChildren() {
|
|
571
|
-
return this.options.hoistChildren || [];
|
|
572
|
-
}
|
|
573
|
-
set hoistChildren(value) {
|
|
574
|
-
this.options.hoistChildren = value;
|
|
575
|
-
}
|
|
576
|
-
getMapContent() {
|
|
577
|
-
return this.options.mapContent;
|
|
578
|
-
}
|
|
579
|
-
mapContent(key, value = 'copy') {
|
|
580
|
-
this.options.mapContent = this.options.mapContent || {};
|
|
581
|
-
this.options.mapContent[key] = value;
|
|
582
|
-
return this;
|
|
583
|
-
}
|
|
584
|
-
onMatch(value) {
|
|
585
|
-
this.options.onMatch = value;
|
|
586
|
-
return this;
|
|
587
|
-
}
|
|
588
|
-
onAppendContent(value) {
|
|
589
|
-
this.options.onAppendContent = value;
|
|
590
|
-
return this;
|
|
591
|
-
}
|
|
592
|
-
onAfterChildParse(value) {
|
|
593
|
-
this.options.onAfterChildParse = value;
|
|
594
|
-
return this;
|
|
595
|
-
}
|
|
596
|
-
onBeforeChildParse(value) {
|
|
597
|
-
this.options.onBeforeChildParse = value;
|
|
598
|
-
return this;
|
|
599
|
-
}
|
|
600
|
-
onMatchStartToken(value) {
|
|
601
|
-
if (this.options.startsWith) {
|
|
602
|
-
this.options.startsWith.onMatchToken = value;
|
|
603
|
-
}
|
|
604
|
-
return this;
|
|
605
|
-
}
|
|
606
|
-
onMatchEndToken(value) {
|
|
607
|
-
if (this.options.endsWith) {
|
|
608
|
-
this.options.endsWith.onMatchToken = value;
|
|
609
|
-
}
|
|
610
|
-
return this;
|
|
611
|
-
}
|
|
612
|
-
initCustomData(value) {
|
|
613
|
-
this.options.initCustomData = value;
|
|
614
|
-
return this;
|
|
615
|
-
}
|
|
616
|
-
onPop(value) {
|
|
617
|
-
this.options.onPop = value;
|
|
618
|
-
return this;
|
|
619
|
-
}
|
|
620
|
-
get popsAfterNode() {
|
|
621
|
-
return this.options.popsAfterNode || [];
|
|
622
|
-
}
|
|
623
|
-
set popsAfterNode(nodes) {
|
|
624
|
-
this.options.popsAfterNode = nodes;
|
|
625
|
-
}
|
|
626
|
-
clearStartsWith() {
|
|
627
|
-
delete this.options.startsWith;
|
|
628
|
-
return this;
|
|
629
|
-
}
|
|
630
|
-
clearEndsWith() {
|
|
631
|
-
delete this.options.endsWith;
|
|
632
|
-
return this;
|
|
633
|
-
}
|
|
634
|
-
clearPopsAtEOFSource() {
|
|
635
|
-
delete this.options.popsAtEOFSource;
|
|
636
|
-
return this;
|
|
637
|
-
}
|
|
638
|
-
clearBadToken() {
|
|
639
|
-
delete this.options.badToken;
|
|
640
|
-
return this;
|
|
641
|
-
}
|
|
642
|
-
clearSkipToken() {
|
|
643
|
-
delete this.options.skipToken;
|
|
644
|
-
return this;
|
|
645
|
-
}
|
|
646
|
-
clearAbsorbs(node) {
|
|
647
|
-
if (this.options.absorbs) {
|
|
648
|
-
if (node && Array.isArray(node)) {
|
|
649
|
-
for (const n of node) {
|
|
650
|
-
delete this.options.absorbs[n.id];
|
|
651
|
-
}
|
|
652
|
-
}
|
|
653
|
-
else if (node) {
|
|
654
|
-
delete this.options.absorbs[node.id];
|
|
655
|
-
}
|
|
656
|
-
else {
|
|
657
|
-
this.options.absorbs = {};
|
|
658
|
-
}
|
|
659
|
-
}
|
|
660
|
-
return this;
|
|
661
|
-
}
|
|
662
|
-
clearRecognizes(...args) {
|
|
663
|
-
var _a;
|
|
664
|
-
if (args.length) {
|
|
665
|
-
this.options.recognizes = (_a = this.options.recognizes) === null || _a === void 0 ? void 0 : _a.filter(n => !args.includes(n));
|
|
666
|
-
}
|
|
667
|
-
else {
|
|
668
|
-
this.options.recognizes = [];
|
|
669
|
-
}
|
|
670
|
-
return this;
|
|
671
|
-
}
|
|
672
|
-
clearHoistChildren() {
|
|
673
|
-
delete this.options.hoistChildren;
|
|
674
|
-
return this;
|
|
675
|
-
}
|
|
676
|
-
clearMapContent() {
|
|
677
|
-
delete this.options.mapContent;
|
|
678
|
-
return this;
|
|
679
|
-
}
|
|
680
|
-
removeOnPop() {
|
|
681
|
-
delete this.options.onPop;
|
|
682
|
-
return this;
|
|
683
|
-
}
|
|
684
|
-
removeOnMatch() {
|
|
685
|
-
delete this.options.onMatch;
|
|
686
|
-
return this;
|
|
687
|
-
}
|
|
688
|
-
removeOnAppendContent() {
|
|
689
|
-
delete this.options.onAppendContent;
|
|
690
|
-
return this;
|
|
691
|
-
}
|
|
692
|
-
removeOnBeforeChildParse() {
|
|
693
|
-
delete this.options.onBeforeChildParse;
|
|
694
|
-
return this;
|
|
695
|
-
}
|
|
696
|
-
removeOnAfterChildParse() {
|
|
697
|
-
delete this.options.onAfterChildParse;
|
|
698
|
-
return this;
|
|
699
|
-
}
|
|
700
|
-
fireNodeMatched(matched, cbData) {
|
|
701
|
-
return this.options.startsWith ? this.fireMatched(this.options.startsWith, matched, cbData) : { confirmed: true };
|
|
702
|
-
}
|
|
703
|
-
fireNodeEndMatched(matched, cbData) {
|
|
704
|
-
return this.options.endsWith ? this.fireMatched(this.options.endsWith, matched, cbData) : { confirmed: true };
|
|
705
|
-
}
|
|
706
|
-
fireMatched(descr, matched, cbData) {
|
|
707
|
-
const { omit, eject, onMatchToken } = descr;
|
|
708
|
-
let cbResult = true;
|
|
709
|
-
if (onMatchToken) {
|
|
710
|
-
cbResult = onMatchToken({
|
|
711
|
-
...cbData,
|
|
712
|
-
matched,
|
|
713
|
-
});
|
|
714
|
-
}
|
|
715
|
-
const cbOmit = typeof cbResult === 'object' ? cbResult.omit : undefined;
|
|
716
|
-
const cbEject = typeof cbResult === 'object' ? cbResult.eject : undefined;
|
|
717
|
-
return {
|
|
718
|
-
omit: cbOmit !== undefined ? cbOmit : omit,
|
|
719
|
-
eject: cbEject !== undefined ? cbEject : eject,
|
|
720
|
-
confirmed: cbResult !== false,
|
|
721
|
-
};
|
|
722
|
-
}
|
|
723
|
-
getStartTokenRg() {
|
|
724
|
-
return this.getRgOutOfTokenDescriptor(this.options.startsWith);
|
|
725
|
-
}
|
|
726
|
-
getEndTokenRg() {
|
|
727
|
-
return this.getRgOutOfTokenDescriptor(this.options.endsWith);
|
|
728
|
-
}
|
|
729
|
-
getConstraintTokens() {
|
|
730
|
-
return {
|
|
731
|
-
skip: this.getRgOutOfTokenDescriptor(this.options.skipToken ? { token: this.options.skipToken } : undefined) || undefined,
|
|
732
|
-
bad: this.getRgOutOfTokenDescriptor(this.options.badToken ? { token: this.options.badToken } : undefined) || undefined,
|
|
733
|
-
};
|
|
734
|
-
}
|
|
735
|
-
getRgOutOfTokenDescriptor(descr) {
|
|
736
|
-
if (descr) {
|
|
737
|
-
const prefix = descr.ignoreBackSlashed ? /(?<=(?:^|[^\\])(?:\\\\)*)/.source : '';
|
|
738
|
-
let token;
|
|
739
|
-
if (typeof descr.token === 'function') {
|
|
740
|
-
token = descr.token(this);
|
|
741
|
-
}
|
|
742
|
-
else {
|
|
743
|
-
token = descr.token;
|
|
744
|
-
}
|
|
745
|
-
if (token instanceof RegExp) {
|
|
746
|
-
return new RegExp(prefix + token.source, token.flags);
|
|
747
|
-
}
|
|
748
|
-
else {
|
|
749
|
-
return new RegExp(`${prefix}(?:${[token].flat().map(t => escapeRegex$1(t)).join('|')})`);
|
|
750
|
-
}
|
|
751
|
-
}
|
|
752
|
-
}
|
|
753
|
-
}
|
|
754
|
-
|
|
755
|
-
class ProstoHoistManager {
|
|
756
|
-
constructor() {
|
|
757
|
-
this.data = {};
|
|
758
|
-
}
|
|
759
|
-
addHoistOptions(ctx) {
|
|
760
|
-
if (ctx.hoistChildren) {
|
|
761
|
-
ctx.hoistChildren.forEach(options => {
|
|
762
|
-
const nodeId = typeof options.node === 'object' ? options.node.id : options.node;
|
|
763
|
-
const hoist = this.data[nodeId] = (this.data[nodeId] || {});
|
|
764
|
-
if (hoist) {
|
|
765
|
-
hoist[ctx.index] = {
|
|
766
|
-
options,
|
|
767
|
-
context: ctx,
|
|
768
|
-
};
|
|
769
|
-
}
|
|
770
|
-
});
|
|
771
|
-
}
|
|
772
|
-
}
|
|
773
|
-
removeHoistOptions(ctx) {
|
|
774
|
-
if (ctx.hoistChildren) {
|
|
775
|
-
ctx.hoistChildren.forEach(options => {
|
|
776
|
-
const nodeId = typeof options.node === 'object' ? options.node.id : options.node;
|
|
777
|
-
const hoist = this.data[nodeId];
|
|
778
|
-
if (hoist) {
|
|
779
|
-
delete hoist[ctx.index];
|
|
780
|
-
}
|
|
781
|
-
});
|
|
782
|
-
}
|
|
783
|
-
}
|
|
784
|
-
processHoistOptions(ctx) {
|
|
785
|
-
const id = ctx.node.id;
|
|
786
|
-
const hoist = this.data[id];
|
|
787
|
-
if (hoist) {
|
|
788
|
-
Object.keys(hoist).map(i => hoist[i]).forEach(({ options, context }) => {
|
|
789
|
-
const customData = context.getCustomData();
|
|
790
|
-
if (options.deep === true || Number(options.deep) >= (ctx.level - context.level)) {
|
|
791
|
-
if (options.asArray) {
|
|
792
|
-
const hoisted = customData[options.as] = (customData[options.as] || []);
|
|
793
|
-
if (!Array.isArray(hoisted)) {
|
|
794
|
-
if (!options.onConflict || options.onConflict === 'error') {
|
|
795
|
-
throw new Error(`Can not hoist "${ctx.node.name}" to "${context.node.name}" as "${options.as}". "${options.as}" already exists and it is not an Array Type.`);
|
|
796
|
-
}
|
|
797
|
-
else if (options.onConflict === 'overwrite') {
|
|
798
|
-
customData[options.as] = [doTheMapRule(options, ctx)];
|
|
799
|
-
}
|
|
800
|
-
else if (options.onConflict !== 'ignore') {
|
|
801
|
-
throw new Error(`Unsupported hoisting option onConflict "${options.onConflict}"`);
|
|
802
|
-
}
|
|
803
|
-
}
|
|
804
|
-
else {
|
|
805
|
-
hoisted.push(doTheMapRule(options, ctx));
|
|
806
|
-
}
|
|
807
|
-
}
|
|
808
|
-
else {
|
|
809
|
-
if (customData[options.as]) {
|
|
810
|
-
if (!options.onConflict || options.onConflict === 'error') {
|
|
811
|
-
throw new Error(`Can not hoist multiple "${ctx.node.name}" to "${context.node.name}" as "${options.as}". "${options.as}" already exists.`);
|
|
812
|
-
}
|
|
813
|
-
else if (options.onConflict === 'overwrite') {
|
|
814
|
-
customData[options.as] = doTheMapRule(options, ctx);
|
|
815
|
-
}
|
|
816
|
-
else if (options.onConflict !== 'ignore') {
|
|
817
|
-
throw new Error(`Unsupported hoisting option onConflict "${options.onConflict}"`);
|
|
818
|
-
}
|
|
819
|
-
}
|
|
820
|
-
else {
|
|
821
|
-
customData[options.as] = doTheMapRule(options, ctx);
|
|
822
|
-
}
|
|
823
|
-
}
|
|
824
|
-
if (options.removeChildFromContent) {
|
|
825
|
-
context.content = context.content.filter(c => c !== ctx);
|
|
826
|
-
}
|
|
827
|
-
}
|
|
828
|
-
});
|
|
829
|
-
}
|
|
830
|
-
function doTheMapRule(options, ctx) {
|
|
831
|
-
var _a;
|
|
832
|
-
if (typeof options.mapRule === 'function') {
|
|
833
|
-
return options.mapRule(ctx);
|
|
834
|
-
}
|
|
835
|
-
if (options.mapRule === 'content.join') {
|
|
836
|
-
return ctx.content.join('');
|
|
837
|
-
}
|
|
838
|
-
if ((_a = options.mapRule) === null || _a === void 0 ? void 0 : _a.startsWith('customData')) {
|
|
839
|
-
const key = options.mapRule.slice(11);
|
|
840
|
-
if (key) {
|
|
841
|
-
return ctx.getCustomData()[key];
|
|
842
|
-
}
|
|
843
|
-
else {
|
|
844
|
-
return ctx.getCustomData();
|
|
845
|
-
}
|
|
846
|
-
}
|
|
847
|
-
return ctx;
|
|
848
|
-
}
|
|
849
|
-
}
|
|
850
|
-
}
|
|
851
|
-
|
|
852
|
-
const banner$3 = '[31m' + '[parser]' + '[39m';
|
|
853
|
-
class ProstoParserContext {
|
|
854
|
-
constructor(root) {
|
|
855
|
-
this.root = root;
|
|
856
|
-
this.nodes = {};
|
|
857
|
-
this.pos = 0;
|
|
858
|
-
this.index = 0;
|
|
859
|
-
this.behind = '';
|
|
860
|
-
this.here = '';
|
|
861
|
-
this.src = '';
|
|
862
|
-
this.stack = [];
|
|
863
|
-
this.l = 0;
|
|
864
|
-
this.hoistManager = new ProstoHoistManager();
|
|
865
|
-
this.context = root;
|
|
866
|
-
}
|
|
867
|
-
parse(src) {
|
|
868
|
-
this.src = src,
|
|
869
|
-
this.here = src,
|
|
870
|
-
this.l = src.length;
|
|
871
|
-
const cache = {};
|
|
872
|
-
while (this.pos < this.l) {
|
|
873
|
-
const searchTokens = this.context.getSearchTokens();
|
|
874
|
-
let closestIndex = Number.MAX_SAFE_INTEGER;
|
|
875
|
-
let closestToken;
|
|
876
|
-
let matched;
|
|
877
|
-
for (const t of searchTokens) {
|
|
878
|
-
const key = t.g.source;
|
|
879
|
-
t.g.lastIndex = this.pos;
|
|
880
|
-
let cached = cache[key];
|
|
881
|
-
if (cached === null)
|
|
882
|
-
continue;
|
|
883
|
-
if (cached && cached.index < this.pos) {
|
|
884
|
-
cached = null;
|
|
885
|
-
delete cache[key];
|
|
886
|
-
}
|
|
887
|
-
if (!cached) {
|
|
888
|
-
cached = t.g.exec(this.src);
|
|
889
|
-
if (cached || (this.pos === 0 && !cached)) {
|
|
890
|
-
cache[key] = cached;
|
|
891
|
-
}
|
|
892
|
-
}
|
|
893
|
-
if (cached && cached.index < closestIndex) {
|
|
894
|
-
closestIndex = cached.index;
|
|
895
|
-
matched = cached;
|
|
896
|
-
closestToken = t;
|
|
897
|
-
if (closestIndex === this.pos)
|
|
898
|
-
break;
|
|
899
|
-
}
|
|
900
|
-
}
|
|
901
|
-
if (closestToken && matched) {
|
|
902
|
-
const toAppend = this.src.slice(this.pos, closestIndex);
|
|
903
|
-
if (toAppend) {
|
|
904
|
-
this.context.appendContent(toAppend);
|
|
905
|
-
this.jump(toAppend.length);
|
|
906
|
-
}
|
|
907
|
-
const matchedToken = matched[0];
|
|
908
|
-
if (closestToken.node) {
|
|
909
|
-
const { omit, eject, confirmed } = closestToken.node.fireNodeMatched(matched, this.getCallbackData(matched));
|
|
910
|
-
if (!confirmed)
|
|
911
|
-
continue;
|
|
912
|
-
let toAppend = '';
|
|
913
|
-
if (eject) {
|
|
914
|
-
this.context.appendContent(matchedToken);
|
|
915
|
-
}
|
|
916
|
-
else if (!omit) {
|
|
917
|
-
toAppend = matchedToken;
|
|
918
|
-
}
|
|
919
|
-
this.jump(matchedToken.length);
|
|
920
|
-
this.pushNewContext(closestToken.node, toAppend ? [toAppend] : []);
|
|
921
|
-
this.context.fireOnMatch(matched);
|
|
922
|
-
continue;
|
|
923
|
-
}
|
|
924
|
-
else {
|
|
925
|
-
const { omit, eject, confirmed } = this.context.fireNodeEndMatched(matched, this.getCallbackData(matched));
|
|
926
|
-
if (!confirmed)
|
|
927
|
-
continue;
|
|
928
|
-
if (!eject && !omit) {
|
|
929
|
-
this.context.appendContent(matchedToken);
|
|
930
|
-
}
|
|
931
|
-
if (!eject) {
|
|
932
|
-
this.jump(matchedToken.length);
|
|
933
|
-
}
|
|
934
|
-
this.context.mapNamedGroups(matched);
|
|
935
|
-
this.pop();
|
|
936
|
-
continue;
|
|
937
|
-
}
|
|
938
|
-
}
|
|
939
|
-
else {
|
|
940
|
-
this.context.appendContent(this.here);
|
|
941
|
-
this.jump(this.here.length);
|
|
942
|
-
}
|
|
943
|
-
}
|
|
944
|
-
if (this.context !== this.root) {
|
|
945
|
-
while (this.context.getPopsAtEOFSource() && this.stack.length > 0)
|
|
946
|
-
this.pop();
|
|
947
|
-
}
|
|
948
|
-
if (this.context !== this.root) {
|
|
949
|
-
this.panicBlock(`Unexpected end of the source string while parsing "${this.context.node.name}" (${this.context.index}) node.`);
|
|
950
|
-
}
|
|
951
|
-
return this.root;
|
|
952
|
-
}
|
|
953
|
-
pop() {
|
|
954
|
-
const parentContext = this.stack.pop();
|
|
955
|
-
this.context.fireOnPop();
|
|
956
|
-
if (parentContext) {
|
|
957
|
-
parentContext.fireAfterChildParse(this.context);
|
|
958
|
-
parentContext.fireAbsorb(this.context);
|
|
959
|
-
this.context.cleanup();
|
|
960
|
-
const node = this.context.node;
|
|
961
|
-
this.context = parentContext;
|
|
962
|
-
if (parentContext.popsAfterNode.includes(node)) {
|
|
963
|
-
this.pop();
|
|
964
|
-
}
|
|
965
|
-
}
|
|
966
|
-
else {
|
|
967
|
-
this.context.cleanup();
|
|
968
|
-
}
|
|
969
|
-
}
|
|
970
|
-
pushNewContext(newNode, content) {
|
|
971
|
-
this.index++;
|
|
972
|
-
const ctx = newNode.createContext(this.index, this.stack.length + 1, this);
|
|
973
|
-
ctx.content = content;
|
|
974
|
-
this.context.fireBeforeChildParse(ctx);
|
|
975
|
-
this.context.pushChild(ctx);
|
|
976
|
-
this.stack.push(this.context);
|
|
977
|
-
this.hoistManager.addHoistOptions(this.context);
|
|
978
|
-
this.context = ctx;
|
|
979
|
-
return ctx;
|
|
980
|
-
}
|
|
981
|
-
fromStack(depth = 0) {
|
|
982
|
-
return this.stack[this.stack.length - depth - 1];
|
|
983
|
-
}
|
|
984
|
-
jump(n = 1) {
|
|
985
|
-
this.pos += n;
|
|
986
|
-
this.behind = this.src.slice(0, this.pos);
|
|
987
|
-
this.here = this.src.slice(this.pos, this.l);
|
|
988
|
-
return this.pos;
|
|
989
|
-
}
|
|
990
|
-
getCallbackData(matched) {
|
|
991
|
-
return {
|
|
992
|
-
parserContext: this,
|
|
993
|
-
context: this.context,
|
|
994
|
-
matched,
|
|
995
|
-
customData: this.context.getCustomData(),
|
|
996
|
-
};
|
|
997
|
-
}
|
|
998
|
-
getPosition(offset = 0) {
|
|
999
|
-
var _a;
|
|
1000
|
-
const past = this.src.slice(0, this.pos + offset).split('\n');
|
|
1001
|
-
const row = past.length;
|
|
1002
|
-
const col = ((_a = past.pop()) === null || _a === void 0 ? void 0 : _a.length) || 0;
|
|
1003
|
-
return {
|
|
1004
|
-
row, col, pos: this.pos,
|
|
1005
|
-
};
|
|
1006
|
-
}
|
|
1007
|
-
panic(message, errorBackOffset = 0) {
|
|
1008
|
-
if (this.pos > 0) {
|
|
1009
|
-
const { row, col } = this.getPosition(-errorBackOffset);
|
|
1010
|
-
console.error(banner$3 + '[91m', message, '[0m');
|
|
1011
|
-
console.log(this.context.toTree({ childrenLimit: 5, showLast: true, level: 1 }));
|
|
1012
|
-
console.error(renderCodeFragment(this.src.split('\n'), {
|
|
1013
|
-
row: row,
|
|
1014
|
-
error: col,
|
|
1015
|
-
}));
|
|
1016
|
-
}
|
|
1017
|
-
throw new Error(message);
|
|
1018
|
-
}
|
|
1019
|
-
panicBlock(message, topBackOffset = 0, bottomBackOffset = 0) {
|
|
1020
|
-
if (this.pos > 0) {
|
|
1021
|
-
const { row, col } = this.getPosition(-bottomBackOffset);
|
|
1022
|
-
console.error(banner$3 + '[91m', message, '[0m');
|
|
1023
|
-
console.log(this.context.toTree({ childrenLimit: 13, showLast: true, level: 12 }));
|
|
1024
|
-
console.error(renderCodeFragment(this.src.split('\n'), {
|
|
1025
|
-
row: this.context.startPos.row,
|
|
1026
|
-
error: this.context.startPos.col - topBackOffset,
|
|
1027
|
-
rowEnd: row,
|
|
1028
|
-
errorEnd: col,
|
|
1029
|
-
}));
|
|
1030
|
-
}
|
|
1031
|
-
throw new Error(message);
|
|
1032
|
-
}
|
|
1033
|
-
}
|
|
1034
|
-
|
|
1035
|
-
const styles = {
|
|
1036
|
-
banner: (s) => '[31m' + s + '[39m',
|
|
1037
|
-
text: (s) => '[32m' + s + '[39m',
|
|
1038
|
-
valuesDim: (s) => '[36m' + '[2m' + s + '[39m' + '[22m',
|
|
1039
|
-
boolean: (s) => '[94m' + s + '[39m',
|
|
1040
|
-
booleanDim: (s) => '[94m' + '[2m' + s + '[39m' + '[22m',
|
|
1041
|
-
underscore: (s) => '[4m' + s + '[24m',
|
|
1042
|
-
values: (s) => (typeof s === 'string' ? '[96m' : '[93m') + cut(s.toString(), 30) + '[39m',
|
|
1043
|
-
nodeDim: (s) => '[33m' + '[2m' + s + '[39m' + '[22m',
|
|
1044
|
-
node: (s) => '[33m' + s + '[39m',
|
|
1045
|
-
};
|
|
1046
|
-
const stringOutputLimit = 70;
|
|
1047
|
-
const parserTree = new ProstoTree({
|
|
1048
|
-
children: 'content',
|
|
1049
|
-
renderLabel: (context) => {
|
|
1050
|
-
if (typeof context === 'string') {
|
|
1051
|
-
return styles.text('«' + cut(context, stringOutputLimit) + '[32m' + '»');
|
|
1052
|
-
}
|
|
1053
|
-
else if (typeof context === 'object' && context instanceof ProstoParserNodeContext) {
|
|
1054
|
-
let keys = '';
|
|
1055
|
-
const data = context.getCustomData();
|
|
1056
|
-
Object.keys(data).forEach(key => {
|
|
1057
|
-
const val = data[key];
|
|
1058
|
-
if (typeof val === 'string' || typeof val === 'number') {
|
|
1059
|
-
keys += ' ' + styles.valuesDim(key + '(') + styles.values(val) + styles.valuesDim(')');
|
|
1060
|
-
}
|
|
1061
|
-
else if (Array.isArray(val)) {
|
|
1062
|
-
keys += ' ' + styles.valuesDim(key + `[${val.length}]`);
|
|
1063
|
-
}
|
|
1064
|
-
else if (typeof val === 'object') {
|
|
1065
|
-
keys += ' ' + styles.valuesDim(`{ ${key} }`);
|
|
1066
|
-
}
|
|
1067
|
-
else if (typeof val === 'boolean' && val) {
|
|
1068
|
-
const st = key ? styles.boolean : styles.booleanDim;
|
|
1069
|
-
keys += ' ' + `${styles.underscore(st(key))}${st(val ? '☑' : '☐')}`;
|
|
1070
|
-
}
|
|
1071
|
-
});
|
|
1072
|
-
return styles.node(context.icon + (context.label ? ' ' : '')) + styles.nodeDim(context.label) + keys;
|
|
1073
|
-
}
|
|
1074
|
-
return '';
|
|
1075
|
-
},
|
|
1076
|
-
});
|
|
1077
|
-
function cut(s, n) {
|
|
1078
|
-
const c = s.replace(/\n/g, '\\n');
|
|
1079
|
-
if (c.length <= n)
|
|
1080
|
-
return c;
|
|
1081
|
-
return c.slice(0, n) + '[33m' + '…';
|
|
1082
|
-
}
|
|
1083
|
-
|
|
1084
|
-
class ProstoParserNodeContext extends ProstoParserNodeBase {
|
|
1085
|
-
constructor(_node, index, level, parserContext) {
|
|
1086
|
-
super();
|
|
1087
|
-
this._node = _node;
|
|
1088
|
-
this.index = index;
|
|
1089
|
-
this.level = level;
|
|
1090
|
-
this.content = [];
|
|
1091
|
-
this._customData = {};
|
|
1092
|
-
this.hasNodes = [];
|
|
1093
|
-
this.count = {};
|
|
1094
|
-
this.mapContentRules = {
|
|
1095
|
-
'first': (content) => content[0],
|
|
1096
|
-
'shift': (content) => content.shift(),
|
|
1097
|
-
'pop': (content) => content.pop(),
|
|
1098
|
-
'last': (content) => content[content.length - 1],
|
|
1099
|
-
'join': (content) => content.join(''),
|
|
1100
|
-
'join-clear': (content) => content.splice(0).join(''),
|
|
1101
|
-
'copy': (content) => content,
|
|
1102
|
-
};
|
|
1103
|
-
this.options = _node.getOptions();
|
|
1104
|
-
if (this.options.initCustomData) {
|
|
1105
|
-
this._customData = this.options.initCustomData();
|
|
1106
|
-
}
|
|
1107
|
-
this._label = this.options.label || '';
|
|
1108
|
-
this._icon = this.options.icon || '◦';
|
|
1109
|
-
this.parserContext = parserContext || new ProstoParserContext(this);
|
|
1110
|
-
if (parserContext) {
|
|
1111
|
-
this.parent = parserContext.context || parserContext.root;
|
|
1112
|
-
}
|
|
1113
|
-
this.startPos = this.parserContext.getPosition();
|
|
1114
|
-
this.endPos = this.parserContext.getPosition();
|
|
1115
|
-
}
|
|
1116
|
-
getOptions() {
|
|
1117
|
-
return this.options;
|
|
1118
|
-
}
|
|
1119
|
-
extractCustomDataTree() {
|
|
1120
|
-
let content = this.content;
|
|
1121
|
-
if (this.contentCopiedTo) {
|
|
1122
|
-
content = this.customData[this.contentCopiedTo];
|
|
1123
|
-
}
|
|
1124
|
-
if (Array.isArray(content)) {
|
|
1125
|
-
return content.map(c => {
|
|
1126
|
-
if (typeof c === 'string') {
|
|
1127
|
-
return c;
|
|
1128
|
-
}
|
|
1129
|
-
else {
|
|
1130
|
-
return extract(c);
|
|
1131
|
-
}
|
|
1132
|
-
});
|
|
1133
|
-
}
|
|
1134
|
-
else {
|
|
1135
|
-
const c = content;
|
|
1136
|
-
if (c instanceof ProstoParserNodeContext) {
|
|
1137
|
-
return extract(c);
|
|
1138
|
-
}
|
|
1139
|
-
else {
|
|
1140
|
-
return content;
|
|
1141
|
-
}
|
|
1142
|
-
}
|
|
1143
|
-
function extract(c) {
|
|
1144
|
-
const cd = { ...c.getCustomData() };
|
|
1145
|
-
if (c.contentCopiedTo) {
|
|
1146
|
-
cd[c.contentCopiedTo] = c.extractCustomDataTree();
|
|
1147
|
-
}
|
|
1148
|
-
return cd;
|
|
1149
|
-
}
|
|
1150
|
-
}
|
|
1151
|
-
getPrevNode(n = 1) {
|
|
1152
|
-
if (this.parent) {
|
|
1153
|
-
const index = this.parent.content.findIndex(n => n === this) - n;
|
|
1154
|
-
if (index >= 0)
|
|
1155
|
-
return this.parent.content[index];
|
|
1156
|
-
}
|
|
1157
|
-
}
|
|
1158
|
-
getPrevContext(n = 1) {
|
|
1159
|
-
if (this.parent) {
|
|
1160
|
-
const contexts = this.parent.content.filter(n => n instanceof ProstoParserNodeContext);
|
|
1161
|
-
const index = contexts.findIndex(n => n === this) - n;
|
|
1162
|
-
if (index >= 0)
|
|
1163
|
-
return contexts[index];
|
|
1164
|
-
}
|
|
1165
|
-
}
|
|
1166
|
-
set icon(value) {
|
|
1167
|
-
this._icon = value;
|
|
1168
|
-
}
|
|
1169
|
-
get icon() {
|
|
1170
|
-
return this._icon;
|
|
1171
|
-
}
|
|
1172
|
-
set label(value) {
|
|
1173
|
-
this._label = value;
|
|
1174
|
-
}
|
|
1175
|
-
get label() {
|
|
1176
|
-
return this._label;
|
|
1177
|
-
}
|
|
1178
|
-
getCustomData() {
|
|
1179
|
-
return this._customData;
|
|
1180
|
-
}
|
|
1181
|
-
get customData() {
|
|
1182
|
-
return this._customData;
|
|
1183
|
-
}
|
|
1184
|
-
get nodeId() {
|
|
1185
|
-
return this._node.id;
|
|
1186
|
-
}
|
|
1187
|
-
get node() {
|
|
1188
|
-
return this._node;
|
|
1189
|
-
}
|
|
1190
|
-
toTree(options) {
|
|
1191
|
-
return parserTree.render(this, options);
|
|
1192
|
-
}
|
|
1193
|
-
getSearchTokens() {
|
|
1194
|
-
var _a;
|
|
1195
|
-
const rg = this.getEndTokenRg();
|
|
1196
|
-
const tokens = rg ? [{
|
|
1197
|
-
rg,
|
|
1198
|
-
y: addFlag(rg, 'y'),
|
|
1199
|
-
g: addFlag(rg, 'g'),
|
|
1200
|
-
}] : [];
|
|
1201
|
-
(_a = this.options.recognizes) === null || _a === void 0 ? void 0 : _a.forEach(node => {
|
|
1202
|
-
const rg = node.getStartTokenRg();
|
|
1203
|
-
if (rg) {
|
|
1204
|
-
tokens.push({
|
|
1205
|
-
rg,
|
|
1206
|
-
y: addFlag(rg, 'y'),
|
|
1207
|
-
g: addFlag(rg, 'g'),
|
|
1208
|
-
node,
|
|
1209
|
-
});
|
|
1210
|
-
}
|
|
1211
|
-
});
|
|
1212
|
-
function addFlag(rg, f) {
|
|
1213
|
-
return new RegExp(rg.source, rg.flags + f);
|
|
1214
|
-
}
|
|
1215
|
-
return tokens;
|
|
1216
|
-
}
|
|
1217
|
-
appendContent(input) {
|
|
1218
|
-
let s = input;
|
|
1219
|
-
this.endPos = this.parserContext.getPosition();
|
|
1220
|
-
let { skip, bad } = this.getConstraintTokens();
|
|
1221
|
-
skip = skip ? new RegExp(skip.source, skip.flags + 'g') : skip;
|
|
1222
|
-
bad = bad ? new RegExp(bad.source, bad.flags + 'g') : bad;
|
|
1223
|
-
if (skip) {
|
|
1224
|
-
s = s.replace(skip, '');
|
|
1225
|
-
}
|
|
1226
|
-
if (bad) {
|
|
1227
|
-
const m = bad.exec(s);
|
|
1228
|
-
if (m) {
|
|
1229
|
-
this.parserContext.jump(m.index);
|
|
1230
|
-
this.parserContext.panic(`The token "${m[0].replace(/"/g, '\\"')}" is not allowed in "${this.node.name}".`);
|
|
1231
|
-
}
|
|
1232
|
-
}
|
|
1233
|
-
s = this.fireOnAppendContent(s);
|
|
1234
|
-
if (s) {
|
|
1235
|
-
this.content.push(s);
|
|
1236
|
-
}
|
|
1237
|
-
}
|
|
1238
|
-
cleanup() {
|
|
1239
|
-
this.options = null;
|
|
1240
|
-
}
|
|
1241
|
-
pushChild(child) {
|
|
1242
|
-
const absorbRule = this.options.absorbs && this.options.absorbs[child.node.id];
|
|
1243
|
-
if (!absorbRule) {
|
|
1244
|
-
this.content.push(child);
|
|
1245
|
-
}
|
|
1246
|
-
}
|
|
1247
|
-
fireAbsorb(child) {
|
|
1248
|
-
const absorbRule = this.options.absorbs && this.options.absorbs[child.node.id];
|
|
1249
|
-
if (absorbRule) {
|
|
1250
|
-
switch (absorbRule) {
|
|
1251
|
-
case 'append':
|
|
1252
|
-
this.content.push(...child.content);
|
|
1253
|
-
break;
|
|
1254
|
-
case 'join':
|
|
1255
|
-
this.appendContent(child.content.join(''));
|
|
1256
|
-
break;
|
|
1257
|
-
default:
|
|
1258
|
-
const [action, target] = absorbRule.split('->');
|
|
1259
|
-
const cd = this.getCustomData();
|
|
1260
|
-
if (action === 'copy') {
|
|
1261
|
-
cd[target] = child.content;
|
|
1262
|
-
}
|
|
1263
|
-
else if (action === 'join') {
|
|
1264
|
-
cd[target] = child.content.join('');
|
|
1265
|
-
}
|
|
1266
|
-
else {
|
|
1267
|
-
this.parserContext.panic(`Absorb action "${action}" is not supported.`);
|
|
1268
|
-
}
|
|
1269
|
-
}
|
|
1270
|
-
}
|
|
1271
|
-
}
|
|
1272
|
-
has(node) {
|
|
1273
|
-
return this.hasNodes.includes(node);
|
|
1274
|
-
}
|
|
1275
|
-
countOf(node) {
|
|
1276
|
-
return this.count[node.id] || 0;
|
|
1277
|
-
}
|
|
1278
|
-
mapNamedGroups(matched) {
|
|
1279
|
-
if (matched.groups) {
|
|
1280
|
-
const cd = this.getCustomData();
|
|
1281
|
-
for (const [key, value] of Object.entries(matched.groups)) {
|
|
1282
|
-
if (key === 'content') {
|
|
1283
|
-
this.appendContent(value);
|
|
1284
|
-
}
|
|
1285
|
-
else {
|
|
1286
|
-
cd[key] = value;
|
|
1287
|
-
}
|
|
1288
|
-
}
|
|
1289
|
-
}
|
|
1290
|
-
}
|
|
1291
|
-
fireOnPop() {
|
|
1292
|
-
this.endPos = this.parserContext.getPosition();
|
|
1293
|
-
this.processMappings();
|
|
1294
|
-
const data = this.parserContext.getCallbackData();
|
|
1295
|
-
this.node.beforeOnPop(data);
|
|
1296
|
-
if (this.options.onPop) {
|
|
1297
|
-
this.options.onPop(data);
|
|
1298
|
-
}
|
|
1299
|
-
}
|
|
1300
|
-
fireOnMatch(matched) {
|
|
1301
|
-
this.mapNamedGroups(matched);
|
|
1302
|
-
const data = this.parserContext.getCallbackData(matched);
|
|
1303
|
-
this.node.beforeOnMatch(data);
|
|
1304
|
-
if (this.options.onMatch) {
|
|
1305
|
-
return this.options.onMatch(data);
|
|
1306
|
-
}
|
|
1307
|
-
}
|
|
1308
|
-
fireBeforeChildParse(child) {
|
|
1309
|
-
const data = this.parserContext.getCallbackData();
|
|
1310
|
-
this.node.beforeOnBeforeChildParse(child, data);
|
|
1311
|
-
if (this.options.onBeforeChildParse) {
|
|
1312
|
-
return this.options.onBeforeChildParse(child, data);
|
|
1313
|
-
}
|
|
1314
|
-
}
|
|
1315
|
-
fireAfterChildParse(child) {
|
|
1316
|
-
if (!this.hasNodes.includes(child.node)) {
|
|
1317
|
-
this.hasNodes.push(child.node);
|
|
1318
|
-
}
|
|
1319
|
-
this.count[child.node.id] = this.count[child.node.id] || 0;
|
|
1320
|
-
this.count[child.node.id]++;
|
|
1321
|
-
const data = this.parserContext.getCallbackData();
|
|
1322
|
-
this.node.beforeOnAfterChildParse(child, data);
|
|
1323
|
-
if (this.options.onAfterChildParse) {
|
|
1324
|
-
return this.options.onAfterChildParse(child, data);
|
|
1325
|
-
}
|
|
1326
|
-
}
|
|
1327
|
-
fireOnAppendContent(s) {
|
|
1328
|
-
let _s = s;
|
|
1329
|
-
const data = this.parserContext.getCallbackData();
|
|
1330
|
-
_s = this.node.beforeOnAppendContent(_s, data);
|
|
1331
|
-
if (this.options.onAppendContent) {
|
|
1332
|
-
_s = this.options.onAppendContent(_s, data);
|
|
1333
|
-
}
|
|
1334
|
-
return _s;
|
|
1335
|
-
}
|
|
1336
|
-
processMappings() {
|
|
1337
|
-
this.parserContext.hoistManager.removeHoistOptions(this);
|
|
1338
|
-
this.parserContext.hoistManager.processHoistOptions(this);
|
|
1339
|
-
this.processMapContent();
|
|
1340
|
-
}
|
|
1341
|
-
processMapContent() {
|
|
1342
|
-
const targetNodeOptions = this.options;
|
|
1343
|
-
if (targetNodeOptions.mapContent) {
|
|
1344
|
-
Object.keys(targetNodeOptions.mapContent).forEach((key) => {
|
|
1345
|
-
const keyOfT = key;
|
|
1346
|
-
if (targetNodeOptions.mapContent && targetNodeOptions.mapContent[keyOfT]) {
|
|
1347
|
-
const mapRule = targetNodeOptions.mapContent[keyOfT];
|
|
1348
|
-
if (typeof mapRule === 'function') {
|
|
1349
|
-
this._customData[keyOfT] = mapRule(this.content);
|
|
1350
|
-
}
|
|
1351
|
-
else {
|
|
1352
|
-
const ruleKey = mapRule;
|
|
1353
|
-
if (ruleKey === 'copy')
|
|
1354
|
-
this.contentCopiedTo = keyOfT;
|
|
1355
|
-
this._customData[keyOfT] = this.mapContentRules[ruleKey](this.content);
|
|
1356
|
-
}
|
|
1357
|
-
if (!this.contentCopiedTo && (typeof mapRule === 'function' || ['first', 'shift', 'pop', 'last'].includes(mapRule))) {
|
|
1358
|
-
this.contentCopiedTo = keyOfT;
|
|
1359
|
-
}
|
|
1360
|
-
}
|
|
1361
|
-
});
|
|
1362
|
-
}
|
|
1363
|
-
}
|
|
1364
|
-
}
|
|
1365
|
-
|
|
1366
|
-
let idCounter = 0;
|
|
1367
|
-
class ProstoParserNode extends ProstoParserNodeBase {
|
|
1368
|
-
constructor(options) {
|
|
1369
|
-
super();
|
|
1370
|
-
this.options = options;
|
|
1371
|
-
this.id = idCounter++;
|
|
1372
|
-
}
|
|
1373
|
-
getOptions() {
|
|
1374
|
-
return {
|
|
1375
|
-
label: this.options.label || '',
|
|
1376
|
-
icon: this.options.icon || '',
|
|
1377
|
-
startsWith: (this.options.startsWith ? { ...this.options.startsWith } : this.options.startsWith),
|
|
1378
|
-
endsWith: (this.options.endsWith ? { ...this.options.endsWith } : this.options.endsWith),
|
|
1379
|
-
popsAfterNode: [...(this.options.popsAfterNode || [])],
|
|
1380
|
-
popsAtEOFSource: this.options.popsAtEOFSource || false,
|
|
1381
|
-
badToken: this.options.badToken || '',
|
|
1382
|
-
skipToken: this.options.skipToken || '',
|
|
1383
|
-
recognizes: [...(this.options.recognizes || [])],
|
|
1384
|
-
hoistChildren: [...(this.options.hoistChildren || [])],
|
|
1385
|
-
mapContent: {
|
|
1386
|
-
...this.options.mapContent,
|
|
1387
|
-
},
|
|
1388
|
-
onPop: this.options.onPop,
|
|
1389
|
-
onMatch: this.options.onMatch,
|
|
1390
|
-
onAppendContent: this.options.onAppendContent,
|
|
1391
|
-
onAfterChildParse: this.options.onAfterChildParse,
|
|
1392
|
-
onBeforeChildParse: this.options.onBeforeChildParse,
|
|
1393
|
-
initCustomData: this.options.initCustomData,
|
|
1394
|
-
absorbs: this.options.absorbs,
|
|
1395
|
-
};
|
|
1396
|
-
}
|
|
1397
|
-
createContext(index, level, rootContext) {
|
|
1398
|
-
return new ProstoParserNodeContext(this, index, level, rootContext);
|
|
1399
|
-
}
|
|
1400
|
-
get name() {
|
|
1401
|
-
return this.constructor.name + '[' + this.id.toString() + ']' + '(' + (this.options.label || this.options.icon || '') + ')';
|
|
1402
|
-
}
|
|
1403
|
-
parse(source) {
|
|
1404
|
-
return this.createContext(0, 0).parserContext.parse(source);
|
|
1405
|
-
}
|
|
1406
|
-
beforeOnPop(data) {
|
|
1407
|
-
}
|
|
1408
|
-
beforeOnMatch(data) {
|
|
1409
|
-
}
|
|
1410
|
-
beforeOnAppendContent(s, data) {
|
|
1411
|
-
return s;
|
|
1412
|
-
}
|
|
1413
|
-
beforeOnAfterChildParse(child, data) {
|
|
1414
|
-
}
|
|
1415
|
-
beforeOnBeforeChildParse(child, data) {
|
|
1416
|
-
}
|
|
1417
|
-
}
|
|
1418
|
-
|
|
1419
|
-
class BasicNode extends ProstoParserNode {
|
|
1420
|
-
constructor(options) {
|
|
1421
|
-
var _a, _b;
|
|
1422
|
-
const startsWith = (options === null || options === void 0 ? void 0 : options.tokens) ? { token: options === null || options === void 0 ? void 0 : options.tokens[0] } : undefined;
|
|
1423
|
-
const endsWith = (options === null || options === void 0 ? void 0 : options.tokens) ? { token: options === null || options === void 0 ? void 0 : options.tokens[1] } : undefined;
|
|
1424
|
-
const [startOption, endOption] = ((_a = options.tokenOE) === null || _a === void 0 ? void 0 : _a.split('-')) || [];
|
|
1425
|
-
const [startBSlash, endBSlash] = ((_b = options.backSlash) === null || _b === void 0 ? void 0 : _b.split('-')) || [];
|
|
1426
|
-
if (startsWith) {
|
|
1427
|
-
startsWith.omit = startOption === 'omit';
|
|
1428
|
-
startsWith.eject = startOption === 'eject';
|
|
1429
|
-
startsWith.ignoreBackSlashed = startBSlash === 'ignore';
|
|
1430
|
-
}
|
|
1431
|
-
if (endsWith) {
|
|
1432
|
-
endsWith.omit = endOption === 'omit';
|
|
1433
|
-
endsWith.eject = endOption === 'eject';
|
|
1434
|
-
endsWith.ignoreBackSlashed = endBSlash === 'ignore';
|
|
1435
|
-
}
|
|
1436
|
-
super({
|
|
1437
|
-
icon: options.icon || '',
|
|
1438
|
-
label: typeof options.label === 'string' ? options.label : '',
|
|
1439
|
-
startsWith,
|
|
1440
|
-
endsWith,
|
|
1441
|
-
badToken: options.badToken,
|
|
1442
|
-
skipToken: options.skipToken,
|
|
1443
|
-
});
|
|
1444
|
-
if (options.recursive) {
|
|
1445
|
-
this.addAbsorbs(this, 'join');
|
|
1446
|
-
}
|
|
1447
|
-
}
|
|
1448
|
-
}
|
|
1449
|
-
|
|
1450
|
-
function parsePath(expr) {
|
|
1451
|
-
return parser.parse(expr).extractCustomDataTree();
|
|
1452
|
-
}
|
|
1453
|
-
class ParametricNodeWithRegex extends BasicNode {
|
|
1454
|
-
constructor(options, rgNode) {
|
|
1455
|
-
super(options);
|
|
1456
|
-
const hoistRegex = {
|
|
1457
|
-
as: 'regex',
|
|
1458
|
-
node: regexNode,
|
|
1459
|
-
onConflict: 'overwrite',
|
|
1460
|
-
removeChildFromContent: true,
|
|
1461
|
-
deep: 1,
|
|
1462
|
-
mapRule: ({ content }) => content.join('').replace(/^\(\^/, '(').replace(/\$\)$/, ')'),
|
|
1463
|
-
};
|
|
1464
|
-
this.mapContent('value', content => content.shift())
|
|
1465
|
-
.popsAtEOFSource(true)
|
|
1466
|
-
.addRecognizes(rgNode)
|
|
1467
|
-
.addPopsAfterNode(rgNode)
|
|
1468
|
-
.addHoistChildren(hoistRegex);
|
|
1469
|
-
}
|
|
1470
|
-
}
|
|
1471
|
-
const regexNode = new BasicNode({
|
|
1472
|
-
label: 'RegEx',
|
|
1473
|
-
tokens: ['(', ')'],
|
|
1474
|
-
backSlash: 'ignore-ignore',
|
|
1475
|
-
recursive: true,
|
|
1476
|
-
}).onMatch(({ parserContext, context }) => {
|
|
1477
|
-
var _a;
|
|
1478
|
-
if (((_a = parserContext.fromStack()) === null || _a === void 0 ? void 0 : _a.node) === context.node) {
|
|
1479
|
-
if (!parserContext.here.startsWith('?:')) {
|
|
1480
|
-
context.content[0] += '?:';
|
|
1481
|
-
}
|
|
1482
|
-
}
|
|
1483
|
-
});
|
|
1484
|
-
const paramNode = new ParametricNodeWithRegex({
|
|
1485
|
-
label: 'Parameter',
|
|
1486
|
-
tokens: [':', /[\/\-]/],
|
|
1487
|
-
tokenOE: 'omit-eject',
|
|
1488
|
-
backSlash: 'ignore-',
|
|
1489
|
-
}, regexNode).initCustomData(() => ({ type: EPathSegmentType.VARIABLE, value: '', regex: '([^\\/]*)' }));
|
|
1490
|
-
const wildcardNode = new ParametricNodeWithRegex({
|
|
1491
|
-
label: 'Wildcard',
|
|
1492
|
-
tokens: ['*', /[^*\()]/],
|
|
1493
|
-
tokenOE: '-eject',
|
|
1494
|
-
}, regexNode).initCustomData(() => ({ type: EPathSegmentType.WILDCARD, value: '*', regex: '(.*)' }));
|
|
1495
|
-
const staticNode = new BasicNode({
|
|
1496
|
-
label: 'Static',
|
|
1497
|
-
tokens: [/[^:\*]/, /[:\*]/],
|
|
1498
|
-
backSlash: '-ignore',
|
|
1499
|
-
tokenOE: '-eject',
|
|
1500
|
-
}).initCustomData(() => ({ type: EPathSegmentType.STATIC, value: '' }))
|
|
1501
|
-
.mapContent('value', content => content.splice(0).join('').replace(/\\:/g, ':'))
|
|
1502
|
-
.popsAtEOFSource(true);
|
|
1503
|
-
const parser = new BasicNode({}).addRecognizes(staticNode, paramNode, wildcardNode);
|
|
1504
|
-
|
|
1505
|
-
var EPathSegmentType;
|
|
1506
|
-
(function (EPathSegmentType) {
|
|
1507
|
-
EPathSegmentType[EPathSegmentType["STATIC"] = 0] = "STATIC";
|
|
1508
|
-
EPathSegmentType[EPathSegmentType["VARIABLE"] = 1] = "VARIABLE";
|
|
1509
|
-
EPathSegmentType[EPathSegmentType["REGEX"] = 2] = "REGEX";
|
|
1510
|
-
EPathSegmentType[EPathSegmentType["WILDCARD"] = 3] = "WILDCARD";
|
|
1511
|
-
})(EPathSegmentType || (EPathSegmentType = {}));
|
|
1512
|
-
|
|
1513
|
-
function safeDecode(f, v) {
|
|
1514
|
-
try {
|
|
1515
|
-
return f(v);
|
|
1516
|
-
}
|
|
1517
|
-
catch (e) {
|
|
1518
|
-
return v;
|
|
1519
|
-
}
|
|
1520
|
-
}
|
|
1521
|
-
function safeDecodeURIComponent(uri) {
|
|
1522
|
-
if (uri.indexOf('%') < 0)
|
|
1523
|
-
return uri;
|
|
1524
|
-
return safeDecode(decodeURIComponent, uri);
|
|
1525
|
-
}
|
|
1526
|
-
function safeDecodeURI(uri) {
|
|
1527
|
-
if (uri.indexOf('%') < 0)
|
|
1528
|
-
return uri;
|
|
1529
|
-
return safeDecode(decodeURI, uri);
|
|
1530
|
-
}
|
|
1531
|
-
|
|
1532
|
-
function countOfSlashes(s) {
|
|
1533
|
-
let last = 0;
|
|
1534
|
-
let count = 0;
|
|
1535
|
-
let index = s.indexOf('/');
|
|
1536
|
-
last = index + 1;
|
|
1537
|
-
while (index >= 0) {
|
|
1538
|
-
count++;
|
|
1539
|
-
index = s.indexOf('/', last);
|
|
1540
|
-
last = index + 1;
|
|
1541
|
-
}
|
|
1542
|
-
return count;
|
|
1543
|
-
}
|
|
1544
|
-
|
|
1545
|
-
class CodeString {
|
|
1546
|
-
constructor() {
|
|
1547
|
-
this.code = '';
|
|
1548
|
-
}
|
|
1549
|
-
append(s, newLine = false) {
|
|
1550
|
-
this.code += ['', s].flat().join(newLine ? '\n' : '');
|
|
1551
|
-
}
|
|
1552
|
-
prepend(s, newLine = false) {
|
|
1553
|
-
this.code = [s, ''].flat().join(newLine ? '\n' : '') + this.code;
|
|
1554
|
-
}
|
|
1555
|
-
generateFunction(...args) {
|
|
1556
|
-
return new Function(args.join(','), this.code);
|
|
1557
|
-
}
|
|
1558
|
-
toString() {
|
|
1559
|
-
return this.code;
|
|
1560
|
-
}
|
|
1561
|
-
}
|
|
1562
|
-
|
|
1563
|
-
function escapeRegex(s) {
|
|
1564
|
-
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
|
|
1565
|
-
}
|
|
1566
|
-
|
|
1567
|
-
function generateFullMatchRegex(segments, nonCapturing = false) {
|
|
1568
|
-
let regex = '';
|
|
1569
|
-
segments.forEach(segment => {
|
|
1570
|
-
switch (segment.type) {
|
|
1571
|
-
case EPathSegmentType.STATIC:
|
|
1572
|
-
regex += escapeRegex(segment.value);
|
|
1573
|
-
break;
|
|
1574
|
-
case EPathSegmentType.VARIABLE:
|
|
1575
|
-
case EPathSegmentType.WILDCARD:
|
|
1576
|
-
regex += nonCapturing ? segment.regex.replace(/^\(/, '(?:') : segment.regex;
|
|
1577
|
-
}
|
|
1578
|
-
});
|
|
1579
|
-
return regex;
|
|
1580
|
-
}
|
|
1581
|
-
function generateFullMatchFunc(segments, ignoreCase = false) {
|
|
1582
|
-
const str = new CodeString();
|
|
1583
|
-
const regex = generateFullMatchRegex(segments);
|
|
1584
|
-
let index = 0;
|
|
1585
|
-
const obj = {};
|
|
1586
|
-
segments.forEach(segment => {
|
|
1587
|
-
switch (segment.type) {
|
|
1588
|
-
case EPathSegmentType.VARIABLE:
|
|
1589
|
-
case EPathSegmentType.WILDCARD:
|
|
1590
|
-
index++;
|
|
1591
|
-
obj[segment.value] = obj[segment.value] || [];
|
|
1592
|
-
obj[segment.value].push(index);
|
|
1593
|
-
}
|
|
1594
|
-
});
|
|
1595
|
-
Object.keys(obj).forEach(key => {
|
|
1596
|
-
str.append(obj[key].length > 1
|
|
1597
|
-
? `\tparams['${key}'] = [${obj[key].map(i => `utils.safeDecodeURIComponent(a[${i}])`).join(', ')}]`
|
|
1598
|
-
: `\tparams['${key}'] = utils.safeDecodeURIComponent(a[${obj[key][0]}])`, true);
|
|
1599
|
-
});
|
|
1600
|
-
str.prepend([`const a = path.match(/^${regex}$/${ignoreCase ? 'i' : ''})`, 'if (a) {'], true);
|
|
1601
|
-
str.append(['}', 'return a'], true);
|
|
1602
|
-
return str.generateFunction('path', 'params', 'utils');
|
|
1603
|
-
}
|
|
1604
|
-
function generatePathBuilder(segments) {
|
|
1605
|
-
const str = new CodeString();
|
|
1606
|
-
const obj = {};
|
|
1607
|
-
const index = {};
|
|
1608
|
-
segments.forEach(segment => {
|
|
1609
|
-
switch (segment.type) {
|
|
1610
|
-
case EPathSegmentType.VARIABLE:
|
|
1611
|
-
case EPathSegmentType.WILDCARD:
|
|
1612
|
-
obj[segment.value] = obj[segment.value] || 0;
|
|
1613
|
-
obj[segment.value]++;
|
|
1614
|
-
index[segment.value] = 0;
|
|
1615
|
-
}
|
|
1616
|
-
});
|
|
1617
|
-
str.append('return `');
|
|
1618
|
-
segments.forEach(segment => {
|
|
1619
|
-
switch (segment.type) {
|
|
1620
|
-
case EPathSegmentType.STATIC:
|
|
1621
|
-
str.append(segment.value.replace(/`/g, '\\`'));
|
|
1622
|
-
break;
|
|
1623
|
-
case EPathSegmentType.VARIABLE:
|
|
1624
|
-
case EPathSegmentType.WILDCARD:
|
|
1625
|
-
if (obj[segment.value] > 1) {
|
|
1626
|
-
str.append('${ params[\'' + segment.value + `\'][${index[segment.value]}] }`);
|
|
1627
|
-
index[segment.value]++;
|
|
1628
|
-
}
|
|
1629
|
-
else {
|
|
1630
|
-
str.append('${ params[\'' + segment.value + '\'] }');
|
|
1631
|
-
}
|
|
1632
|
-
}
|
|
1633
|
-
});
|
|
1634
|
-
str.append('`');
|
|
1635
|
-
return str.generateFunction('params');
|
|
1636
|
-
}
|
|
1637
|
-
|
|
1638
|
-
const banner$2 = () => `[prostojs/router][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `;
|
|
1639
|
-
|
|
1640
|
-
const methods = ['GET', 'PUT', 'POST', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'];
|
|
1641
|
-
const matcherFuncUtils = {
|
|
1642
|
-
safeDecodeURIComponent,
|
|
1643
|
-
};
|
|
1644
|
-
class ProstoRouter {
|
|
1645
|
-
constructor(_options) {
|
|
1646
|
-
this.root = {};
|
|
1647
|
-
this.routes = [];
|
|
1648
|
-
this.routesRegistry = {};
|
|
1649
|
-
this._options = {
|
|
1650
|
-
..._options,
|
|
1651
|
-
};
|
|
1652
|
-
if (!this._options.silent) {
|
|
1653
|
-
consoleInfo('The Router Initialized');
|
|
1654
|
-
}
|
|
1655
|
-
const cacheOpts = {
|
|
1656
|
-
limit: (_options === null || _options === void 0 ? void 0 : _options.cacheLimit) || 0,
|
|
1657
|
-
};
|
|
1658
|
-
if (_options === null || _options === void 0 ? void 0 : _options.cacheLimit) {
|
|
1659
|
-
this.cache = {
|
|
1660
|
-
GET: new ProstoCache(cacheOpts),
|
|
1661
|
-
PUT: new ProstoCache(cacheOpts),
|
|
1662
|
-
POST: new ProstoCache(cacheOpts),
|
|
1663
|
-
PATCH: new ProstoCache(cacheOpts),
|
|
1664
|
-
DELETE: new ProstoCache(cacheOpts),
|
|
1665
|
-
HEAD: new ProstoCache(cacheOpts),
|
|
1666
|
-
OPTIONS: new ProstoCache(cacheOpts),
|
|
1667
|
-
};
|
|
1668
|
-
}
|
|
1669
|
-
}
|
|
1670
|
-
refreshCache(method) {
|
|
1671
|
-
if (this._options.cacheLimit && this.cache) {
|
|
1672
|
-
if (method === '*') {
|
|
1673
|
-
this.cache.GET.reset();
|
|
1674
|
-
this.cache.PUT.reset();
|
|
1675
|
-
this.cache.POST.reset();
|
|
1676
|
-
this.cache.PATCH.reset();
|
|
1677
|
-
this.cache.DELETE.reset();
|
|
1678
|
-
this.cache.HEAD.reset();
|
|
1679
|
-
this.cache.OPTIONS.reset();
|
|
1680
|
-
}
|
|
1681
|
-
else if (this.cache && this.cache[method]) {
|
|
1682
|
-
this.cache[method].reset();
|
|
1683
|
-
}
|
|
1684
|
-
}
|
|
1685
|
-
}
|
|
1686
|
-
registerRoute(method, path, options, handler) {
|
|
1687
|
-
this.refreshCache(method);
|
|
1688
|
-
const opts = this.mergeOptions(options);
|
|
1689
|
-
const normalPath = ('/' + path)
|
|
1690
|
-
.replace(/^\/\//, '/')
|
|
1691
|
-
.replace(/\/$/, '')
|
|
1692
|
-
.replace(/%/g, '%25');
|
|
1693
|
-
const { root } = this;
|
|
1694
|
-
const segments = parsePath(normalPath);
|
|
1695
|
-
if (!root[method]) {
|
|
1696
|
-
root[method] = {
|
|
1697
|
-
statics: {},
|
|
1698
|
-
parametrics: {
|
|
1699
|
-
byParts: [],
|
|
1700
|
-
},
|
|
1701
|
-
wildcards: [],
|
|
1702
|
-
};
|
|
1703
|
-
}
|
|
1704
|
-
const rootMethod = root[method];
|
|
1705
|
-
const generalized = method + ':' + segments.map(s => {
|
|
1706
|
-
switch (s.type) {
|
|
1707
|
-
case EPathSegmentType.STATIC: return s.value;
|
|
1708
|
-
case EPathSegmentType.VARIABLE: return '<VAR' + (s.regex === '([^-\\/]*)' ? '' : s.regex) + '>';
|
|
1709
|
-
case EPathSegmentType.WILDCARD: return s.value;
|
|
1710
|
-
}
|
|
1711
|
-
}).join('');
|
|
1712
|
-
let route = this.routesRegistry[generalized];
|
|
1713
|
-
if (route) {
|
|
1714
|
-
if (this._options.disableDuplicatePath) {
|
|
1715
|
-
const error = `Attempt to register duplicated path: "${path}". Duplicate paths are disabled.\nYou can enable duplicated paths removing 'disableDuplicatePath' option.`;
|
|
1716
|
-
consoleError(error);
|
|
1717
|
-
throw new Error(error);
|
|
1718
|
-
}
|
|
1719
|
-
if (route.handlers.includes(handler)) {
|
|
1720
|
-
consoleError('Duplicate route with same handler ignored ' + generalized);
|
|
1721
|
-
}
|
|
1722
|
-
else {
|
|
1723
|
-
consoleWarn('Duplicate route registered ' + generalized);
|
|
1724
|
-
route.handlers.push(handler);
|
|
1725
|
-
}
|
|
1726
|
-
}
|
|
1727
|
-
else {
|
|
1728
|
-
const isStatic = segments.length === 1 && segments[0].type === EPathSegmentType.STATIC || segments.length === 0;
|
|
1729
|
-
const isParametric = !!segments.find(p => p.type === EPathSegmentType.VARIABLE);
|
|
1730
|
-
const isWildcard = !!segments.find(p => p.type === EPathSegmentType.WILDCARD);
|
|
1731
|
-
const lengths = segments.map(s => s.type === EPathSegmentType.STATIC ? s.value.length : 0);
|
|
1732
|
-
const normalPathCase = segments[0] ? (this._options.ignoreCase ? segments[0].value.toLowerCase() : segments[0].value) : '/';
|
|
1733
|
-
this.routesRegistry[generalized] = route = {
|
|
1734
|
-
method,
|
|
1735
|
-
options: opts,
|
|
1736
|
-
path: normalPath,
|
|
1737
|
-
handlers: [handler],
|
|
1738
|
-
isStatic,
|
|
1739
|
-
isParametric,
|
|
1740
|
-
isWildcard,
|
|
1741
|
-
segments,
|
|
1742
|
-
lengths,
|
|
1743
|
-
minLength: lengths.reduce((a, b) => a + b, 0),
|
|
1744
|
-
firstLength: lengths[0],
|
|
1745
|
-
firstStatic: normalPathCase.slice(0, lengths[0]),
|
|
1746
|
-
generalized,
|
|
1747
|
-
fullMatch: generateFullMatchFunc(segments, this._options.ignoreCase),
|
|
1748
|
-
pathBuilder: generatePathBuilder(segments),
|
|
1749
|
-
};
|
|
1750
|
-
this.routes.push(route);
|
|
1751
|
-
if (route.isStatic) {
|
|
1752
|
-
rootMethod.statics[normalPathCase] = route;
|
|
1753
|
-
}
|
|
1754
|
-
else {
|
|
1755
|
-
if (route.isParametric && !route.isWildcard) {
|
|
1756
|
-
const countOfParts = route.segments
|
|
1757
|
-
.filter(s => s.type === EPathSegmentType.STATIC)
|
|
1758
|
-
.map(s => countOfSlashes(s.value)).reduce((a, b) => a + b, 1);
|
|
1759
|
-
const byParts = rootMethod.parametrics.byParts[countOfParts] = rootMethod.parametrics.byParts[countOfParts] || [];
|
|
1760
|
-
byParts.push(route);
|
|
1761
|
-
rootMethod.parametrics.byParts[countOfParts] = byParts.sort(routeSorter);
|
|
1762
|
-
}
|
|
1763
|
-
else if (route.isWildcard) {
|
|
1764
|
-
rootMethod.wildcards.push(route);
|
|
1765
|
-
rootMethod.wildcards = rootMethod.wildcards.sort(routeSorter);
|
|
1766
|
-
}
|
|
1767
|
-
}
|
|
1768
|
-
}
|
|
1769
|
-
return route.pathBuilder;
|
|
1770
|
-
}
|
|
1771
|
-
mergeOptions(options) {
|
|
1772
|
-
return {
|
|
1773
|
-
...options,
|
|
1774
|
-
};
|
|
1775
|
-
}
|
|
1776
|
-
sanitizePath(path) {
|
|
1777
|
-
const end = path.indexOf('?');
|
|
1778
|
-
let slicedPath = end >= 0 ? path.slice(0, end) : path;
|
|
1779
|
-
if (this._options.ignoreTrailingSlash && slicedPath[slicedPath.length - 1] === '/') {
|
|
1780
|
-
slicedPath = slicedPath.slice(0, slicedPath.length - 1);
|
|
1781
|
-
}
|
|
1782
|
-
const normalPath = safeDecodeURI(slicedPath
|
|
1783
|
-
.replace(/%25/g, '%2525'));
|
|
1784
|
-
return {
|
|
1785
|
-
normalPath,
|
|
1786
|
-
normalPathWithCase: this._options.ignoreCase ? normalPath.toLowerCase() : normalPath,
|
|
1787
|
-
};
|
|
1788
|
-
}
|
|
1789
|
-
lookup(method, path) {
|
|
1790
|
-
if (this._options.cacheLimit && this.cache && this.cache[method]) {
|
|
1791
|
-
const cached = this.cache[method].get(path);
|
|
1792
|
-
if (cached)
|
|
1793
|
-
return cached;
|
|
1794
|
-
}
|
|
1795
|
-
const { normalPath, normalPathWithCase } = this.sanitizePath(path);
|
|
1796
|
-
const rootMethod = this.root[method];
|
|
1797
|
-
const lookupResult = {
|
|
1798
|
-
route: null,
|
|
1799
|
-
ctx: { params: {} },
|
|
1800
|
-
};
|
|
1801
|
-
const cache = (result) => {
|
|
1802
|
-
if (this._options.cacheLimit && this.cache && this.cache[method]) {
|
|
1803
|
-
this.cache[method].set(path, result);
|
|
1804
|
-
}
|
|
1805
|
-
return result;
|
|
1806
|
-
};
|
|
1807
|
-
if (rootMethod) {
|
|
1808
|
-
lookupResult.route = rootMethod.statics[normalPathWithCase];
|
|
1809
|
-
if (lookupResult.route)
|
|
1810
|
-
return cache(lookupResult);
|
|
1811
|
-
const pathSegmentsCount = countOfSlashes(normalPath) + 1;
|
|
1812
|
-
const pathLength = normalPath.length;
|
|
1813
|
-
const { parametrics } = rootMethod;
|
|
1814
|
-
const bySegments = parametrics.byParts[pathSegmentsCount];
|
|
1815
|
-
if (bySegments) {
|
|
1816
|
-
for (let i = 0; i < bySegments.length; i++) {
|
|
1817
|
-
lookupResult.route = bySegments[i];
|
|
1818
|
-
if (pathLength >= lookupResult.route.minLength) {
|
|
1819
|
-
if (normalPathWithCase.startsWith(lookupResult.route.firstStatic)
|
|
1820
|
-
&& lookupResult.route.fullMatch(normalPath, lookupResult.ctx.params, matcherFuncUtils)) {
|
|
1821
|
-
return cache(lookupResult);
|
|
1822
|
-
}
|
|
1823
|
-
}
|
|
1824
|
-
}
|
|
1825
|
-
}
|
|
1826
|
-
const { wildcards } = rootMethod;
|
|
1827
|
-
for (let i = 0; i < wildcards.length; i++) {
|
|
1828
|
-
lookupResult.route = wildcards[i];
|
|
1829
|
-
if (pathLength >= lookupResult.route.minLength) {
|
|
1830
|
-
if (normalPathWithCase.startsWith(lookupResult.route.firstStatic)
|
|
1831
|
-
&& lookupResult.route.fullMatch(normalPath, lookupResult.ctx.params, matcherFuncUtils)) {
|
|
1832
|
-
return cache(lookupResult);
|
|
1833
|
-
}
|
|
1834
|
-
}
|
|
1835
|
-
}
|
|
1836
|
-
}
|
|
1837
|
-
}
|
|
1838
|
-
find(method, path) {
|
|
1839
|
-
return this.lookup(method, path);
|
|
1840
|
-
}
|
|
1841
|
-
on(method, path, options, handler) {
|
|
1842
|
-
const { opts, func } = extractOptionsAndHandler(options, handler);
|
|
1843
|
-
if (method === '*') {
|
|
1844
|
-
return methods.map(m => this.registerRoute(m, path, opts, func))[0];
|
|
1845
|
-
}
|
|
1846
|
-
return this.registerRoute(method, path, opts, func);
|
|
1847
|
-
}
|
|
1848
|
-
all(path, options, handler) {
|
|
1849
|
-
const { opts, func } = extractOptionsAndHandler(options, handler);
|
|
1850
|
-
return this.on('*', path, opts, func);
|
|
1851
|
-
}
|
|
1852
|
-
get(path, options, handler) {
|
|
1853
|
-
const { opts, func } = extractOptionsAndHandler(options, handler);
|
|
1854
|
-
return this.on('GET', path, opts, func);
|
|
1855
|
-
}
|
|
1856
|
-
put(path, options, handler) {
|
|
1857
|
-
const { opts, func } = extractOptionsAndHandler(options, handler);
|
|
1858
|
-
return this.on('PUT', path, opts, func);
|
|
1859
|
-
}
|
|
1860
|
-
post(path, options, handler) {
|
|
1861
|
-
const { opts, func } = extractOptionsAndHandler(options, handler);
|
|
1862
|
-
return this.on('POST', path, opts, func);
|
|
1863
|
-
}
|
|
1864
|
-
patch(path, options, handler) {
|
|
1865
|
-
const { opts, func } = extractOptionsAndHandler(options, handler);
|
|
1866
|
-
return this.on('PATCH', path, opts, func);
|
|
1867
|
-
}
|
|
1868
|
-
delete(path, options, handler) {
|
|
1869
|
-
const { opts, func } = extractOptionsAndHandler(options, handler);
|
|
1870
|
-
return this.on('DELETE', path, opts, func);
|
|
1871
|
-
}
|
|
1872
|
-
options(path, options, handler) {
|
|
1873
|
-
const { opts, func } = extractOptionsAndHandler(options, handler);
|
|
1874
|
-
return this.on('OPTIONS', path, opts, func);
|
|
1875
|
-
}
|
|
1876
|
-
head(path, options, handler) {
|
|
1877
|
-
const { opts, func } = extractOptionsAndHandler(options, handler);
|
|
1878
|
-
return this.on('HEAD', path, opts, func);
|
|
1879
|
-
}
|
|
1880
|
-
getRoutes() {
|
|
1881
|
-
return this.routes;
|
|
1882
|
-
}
|
|
1883
|
-
toTree() {
|
|
1884
|
-
const rootStyle = (v) => '[1m' + v + '[22m';
|
|
1885
|
-
const paramStyle = (v) => '[36m' + '[1m' + ':' + v + '[39m' + '[22m';
|
|
1886
|
-
const regexStyle = (v) => '[36m' + '[2m' + v + '[22m' + '[39m';
|
|
1887
|
-
const handlerStyle = (v) => '[1m' + '[92m' + '→ ' + '[39m' + '[22m' + v;
|
|
1888
|
-
const methodStyle = (v) => '[22m' + '[92m' + '• (' + v + ') ' + '[39m';
|
|
1889
|
-
const data = {
|
|
1890
|
-
label: '⁕ Router',
|
|
1891
|
-
stylist: rootStyle,
|
|
1892
|
-
methods: [],
|
|
1893
|
-
children: [],
|
|
1894
|
-
};
|
|
1895
|
-
function toChild(d, label, stylist) {
|
|
1896
|
-
let found = d.children.find(c => c.label === label);
|
|
1897
|
-
if (!found) {
|
|
1898
|
-
found = {
|
|
1899
|
-
label,
|
|
1900
|
-
stylist,
|
|
1901
|
-
methods: [],
|
|
1902
|
-
children: [],
|
|
1903
|
-
};
|
|
1904
|
-
d.children.push(found);
|
|
1905
|
-
}
|
|
1906
|
-
return found;
|
|
1907
|
-
}
|
|
1908
|
-
this.routes.sort((a, b) => a.path > b.path ? 1 : -1).forEach(route => {
|
|
1909
|
-
let cur = data;
|
|
1910
|
-
let last = '';
|
|
1911
|
-
route.segments.forEach(s => {
|
|
1912
|
-
switch (s.type) {
|
|
1913
|
-
case EPathSegmentType.STATIC:
|
|
1914
|
-
const parts = s.value.split('/');
|
|
1915
|
-
last += parts.shift();
|
|
1916
|
-
for (let i = 0; i < parts.length; i++) {
|
|
1917
|
-
if (last) {
|
|
1918
|
-
cur = toChild(cur, last);
|
|
1919
|
-
}
|
|
1920
|
-
last = '/' + parts[i];
|
|
1921
|
-
}
|
|
1922
|
-
break;
|
|
1923
|
-
case EPathSegmentType.VARIABLE:
|
|
1924
|
-
case EPathSegmentType.WILDCARD:
|
|
1925
|
-
last += `${paramStyle(s.value)}${regexStyle(s.regex)}`;
|
|
1926
|
-
}
|
|
1927
|
-
});
|
|
1928
|
-
if (last) {
|
|
1929
|
-
cur = toChild(cur, last, handlerStyle);
|
|
1930
|
-
cur.methods.push(route.method);
|
|
1931
|
-
}
|
|
1932
|
-
});
|
|
1933
|
-
new ProstoTree({
|
|
1934
|
-
renderLabel: (node, behind) => {
|
|
1935
|
-
const styledLabel = node.stylist ? node.stylist(node.label) : node.label;
|
|
1936
|
-
if (node.methods.length) {
|
|
1937
|
-
return styledLabel + '\n' + behind + node.methods.map(m => methodStyle(m)).join('\n' + behind);
|
|
1938
|
-
}
|
|
1939
|
-
return styledLabel;
|
|
1940
|
-
},
|
|
1941
|
-
}).print(data);
|
|
1942
|
-
}
|
|
1943
|
-
}
|
|
1944
|
-
function extractOptionsAndHandler(options, handler) {
|
|
1945
|
-
let opts = {};
|
|
1946
|
-
let func = handler;
|
|
1947
|
-
if (typeof options === 'function') {
|
|
1948
|
-
func = options;
|
|
1949
|
-
}
|
|
1950
|
-
else {
|
|
1951
|
-
opts = options;
|
|
1952
|
-
}
|
|
1953
|
-
return { opts, func };
|
|
1954
|
-
}
|
|
1955
|
-
function routeSorter(a, b) {
|
|
1956
|
-
if (a.isWildcard !== b.isWildcard) {
|
|
1957
|
-
return a.isWildcard ? 1 : -1;
|
|
1958
|
-
}
|
|
1959
|
-
const len = b.minLength - a.minLength;
|
|
1960
|
-
if (len)
|
|
1961
|
-
return len;
|
|
1962
|
-
for (let i = 0; i < a.lengths.length; i++) {
|
|
1963
|
-
const len = b.lengths[i] - a.lengths[i];
|
|
1964
|
-
if (len)
|
|
1965
|
-
return len;
|
|
1966
|
-
}
|
|
1967
|
-
return 0;
|
|
1968
|
-
}
|
|
1969
|
-
function consoleError(v) {
|
|
1970
|
-
console.info('[91m' + banner$2() + v + '[39m');
|
|
1971
|
-
}
|
|
1972
|
-
function consoleWarn(v) {
|
|
1973
|
-
console.info('[33m' + banner$2() + v + '[39m');
|
|
1974
|
-
}
|
|
1975
|
-
function consoleInfo(v) {
|
|
1976
|
-
console.info('[32m' + '[2m' + banner$2() + v + '[39m' + '[22m');
|
|
1977
|
-
}
|
|
1978
|
-
|
|
1979
|
-
class Wooks {
|
|
1980
|
-
constructor() {
|
|
1981
|
-
this.router = new ProstoRouter({
|
|
1982
|
-
silent: true,
|
|
1983
|
-
});
|
|
1984
|
-
}
|
|
1985
|
-
getRouter() {
|
|
1986
|
-
return this.router;
|
|
1987
|
-
}
|
|
1988
|
-
lookup(method, path) {
|
|
1989
|
-
var _a, _b;
|
|
1990
|
-
const found = this.getRouter().lookup(method, path || '');
|
|
1991
|
-
useEventContext().store('routeParams').value = ((_a = found === null || found === void 0 ? void 0 : found.ctx) === null || _a === void 0 ? void 0 : _a.params) || {};
|
|
1992
|
-
return ((_b = found === null || found === void 0 ? void 0 : found.route) === null || _b === void 0 ? void 0 : _b.handlers) || null;
|
|
1993
|
-
}
|
|
1994
|
-
on(method, path, handler) {
|
|
1995
|
-
return this.router.on(method, path, handler);
|
|
1996
|
-
}
|
|
1997
|
-
}
|
|
1998
|
-
let gWooks;
|
|
1999
|
-
function getGlobalWooks() {
|
|
2000
|
-
if (!gWooks) {
|
|
2001
|
-
gWooks = new Wooks();
|
|
2002
|
-
}
|
|
2003
|
-
return gWooks;
|
|
2004
|
-
}
|
|
2005
|
-
class WooksAdapterBase {
|
|
2006
|
-
constructor(wooks) {
|
|
2007
|
-
if (wooks && wooks instanceof WooksAdapterBase) {
|
|
2008
|
-
this.wooks = wooks.getWooks();
|
|
2009
|
-
}
|
|
2010
|
-
else if (wooks && wooks instanceof Wooks) {
|
|
2011
|
-
this.wooks = wooks;
|
|
2012
|
-
}
|
|
2013
|
-
else {
|
|
2014
|
-
this.wooks = getGlobalWooks();
|
|
2015
|
-
}
|
|
2016
|
-
}
|
|
2017
|
-
getWooks() {
|
|
2018
|
-
return this.wooks;
|
|
2019
|
-
}
|
|
2020
|
-
on(method, path, handler) {
|
|
2021
|
-
return this.wooks.on(method, path, handler);
|
|
2022
|
-
}
|
|
2023
|
-
}
|
|
2024
|
-
|
|
2025
|
-
var minimist = function (args, opts) {
|
|
2026
|
-
if (!opts) opts = {};
|
|
2027
|
-
|
|
2028
|
-
var flags = { bools : {}, strings : {}, unknownFn: null };
|
|
2029
|
-
|
|
2030
|
-
if (typeof opts['unknown'] === 'function') {
|
|
2031
|
-
flags.unknownFn = opts['unknown'];
|
|
2032
|
-
}
|
|
2033
|
-
|
|
2034
|
-
if (typeof opts['boolean'] === 'boolean' && opts['boolean']) {
|
|
2035
|
-
flags.allBools = true;
|
|
2036
|
-
} else {
|
|
2037
|
-
[].concat(opts['boolean']).filter(Boolean).forEach(function (key) {
|
|
2038
|
-
flags.bools[key] = true;
|
|
2039
|
-
});
|
|
2040
|
-
}
|
|
2041
|
-
|
|
2042
|
-
var aliases = {};
|
|
2043
|
-
Object.keys(opts.alias || {}).forEach(function (key) {
|
|
2044
|
-
aliases[key] = [].concat(opts.alias[key]);
|
|
2045
|
-
aliases[key].forEach(function (x) {
|
|
2046
|
-
aliases[x] = [key].concat(aliases[key].filter(function (y) {
|
|
2047
|
-
return x !== y;
|
|
2048
|
-
}));
|
|
2049
|
-
});
|
|
2050
|
-
});
|
|
2051
|
-
|
|
2052
|
-
[].concat(opts.string).filter(Boolean).forEach(function (key) {
|
|
2053
|
-
flags.strings[key] = true;
|
|
2054
|
-
if (aliases[key]) {
|
|
2055
|
-
flags.strings[aliases[key]] = true;
|
|
2056
|
-
}
|
|
2057
|
-
});
|
|
2058
|
-
|
|
2059
|
-
var defaults = opts['default'] || {};
|
|
2060
|
-
|
|
2061
|
-
var argv = { _ : [] };
|
|
2062
|
-
Object.keys(flags.bools).forEach(function (key) {
|
|
2063
|
-
setArg(key, defaults[key] === undefined ? false : defaults[key]);
|
|
2064
|
-
});
|
|
2065
|
-
|
|
2066
|
-
var notFlags = [];
|
|
2067
|
-
|
|
2068
|
-
if (args.indexOf('--') !== -1) {
|
|
2069
|
-
notFlags = args.slice(args.indexOf('--')+1);
|
|
2070
|
-
args = args.slice(0, args.indexOf('--'));
|
|
2071
|
-
}
|
|
2072
|
-
|
|
2073
|
-
function argDefined(key, arg) {
|
|
2074
|
-
return (flags.allBools && /^--[^=]+$/.test(arg)) ||
|
|
2075
|
-
flags.strings[key] || flags.bools[key] || aliases[key];
|
|
2076
|
-
}
|
|
2077
|
-
|
|
2078
|
-
function setArg (key, val, arg) {
|
|
2079
|
-
if (arg && flags.unknownFn && !argDefined(key, arg)) {
|
|
2080
|
-
if (flags.unknownFn(arg) === false) return;
|
|
2081
|
-
}
|
|
2082
|
-
|
|
2083
|
-
var value = !flags.strings[key] && isNumber(val)
|
|
2084
|
-
? Number(val) : val
|
|
2085
|
-
;
|
|
2086
|
-
setKey(argv, key.split('.'), value);
|
|
2087
|
-
|
|
2088
|
-
(aliases[key] || []).forEach(function (x) {
|
|
2089
|
-
setKey(argv, x.split('.'), value);
|
|
2090
|
-
});
|
|
2091
|
-
}
|
|
2092
|
-
|
|
2093
|
-
function setKey (obj, keys, value) {
|
|
2094
|
-
var o = obj;
|
|
2095
|
-
for (var i = 0; i < keys.length-1; i++) {
|
|
2096
|
-
var key = keys[i];
|
|
2097
|
-
if (isConstructorOrProto(o, key)) return;
|
|
2098
|
-
if (o[key] === undefined) o[key] = {};
|
|
2099
|
-
if (o[key] === Object.prototype || o[key] === Number.prototype
|
|
2100
|
-
|| o[key] === String.prototype) o[key] = {};
|
|
2101
|
-
if (o[key] === Array.prototype) o[key] = [];
|
|
2102
|
-
o = o[key];
|
|
2103
|
-
}
|
|
2104
|
-
|
|
2105
|
-
var key = keys[keys.length - 1];
|
|
2106
|
-
if (isConstructorOrProto(o, key)) return;
|
|
2107
|
-
if (o === Object.prototype || o === Number.prototype
|
|
2108
|
-
|| o === String.prototype) o = {};
|
|
2109
|
-
if (o === Array.prototype) o = [];
|
|
2110
|
-
if (o[key] === undefined || flags.bools[key] || typeof o[key] === 'boolean') {
|
|
2111
|
-
o[key] = value;
|
|
2112
|
-
}
|
|
2113
|
-
else if (Array.isArray(o[key])) {
|
|
2114
|
-
o[key].push(value);
|
|
2115
|
-
}
|
|
2116
|
-
else {
|
|
2117
|
-
o[key] = [ o[key], value ];
|
|
2118
|
-
}
|
|
2119
|
-
}
|
|
2120
|
-
|
|
2121
|
-
function aliasIsBoolean(key) {
|
|
2122
|
-
return aliases[key].some(function (x) {
|
|
2123
|
-
return flags.bools[x];
|
|
2124
|
-
});
|
|
2125
|
-
}
|
|
2126
|
-
|
|
2127
|
-
for (var i = 0; i < args.length; i++) {
|
|
2128
|
-
var arg = args[i];
|
|
2129
|
-
|
|
2130
|
-
if (/^--.+=/.test(arg)) {
|
|
2131
|
-
// Using [\s\S] instead of . because js doesn't support the
|
|
2132
|
-
// 'dotall' regex modifier. See:
|
|
2133
|
-
// http://stackoverflow.com/a/1068308/13216
|
|
2134
|
-
var m = arg.match(/^--([^=]+)=([\s\S]*)$/);
|
|
2135
|
-
var key = m[1];
|
|
2136
|
-
var value = m[2];
|
|
2137
|
-
if (flags.bools[key]) {
|
|
2138
|
-
value = value !== 'false';
|
|
2139
|
-
}
|
|
2140
|
-
setArg(key, value, arg);
|
|
2141
|
-
}
|
|
2142
|
-
else if (/^--no-.+/.test(arg)) {
|
|
2143
|
-
var key = arg.match(/^--no-(.+)/)[1];
|
|
2144
|
-
setArg(key, false, arg);
|
|
2145
|
-
}
|
|
2146
|
-
else if (/^--.+/.test(arg)) {
|
|
2147
|
-
var key = arg.match(/^--(.+)/)[1];
|
|
2148
|
-
var next = args[i + 1];
|
|
2149
|
-
if (next !== undefined && !/^-/.test(next)
|
|
2150
|
-
&& !flags.bools[key]
|
|
2151
|
-
&& !flags.allBools
|
|
2152
|
-
&& (aliases[key] ? !aliasIsBoolean(key) : true)) {
|
|
2153
|
-
setArg(key, next, arg);
|
|
2154
|
-
i++;
|
|
2155
|
-
}
|
|
2156
|
-
else if (/^(true|false)$/.test(next)) {
|
|
2157
|
-
setArg(key, next === 'true', arg);
|
|
2158
|
-
i++;
|
|
2159
|
-
}
|
|
2160
|
-
else {
|
|
2161
|
-
setArg(key, flags.strings[key] ? '' : true, arg);
|
|
2162
|
-
}
|
|
2163
|
-
}
|
|
2164
|
-
else if (/^-[^-]+/.test(arg)) {
|
|
2165
|
-
var letters = arg.slice(1,-1).split('');
|
|
2166
|
-
|
|
2167
|
-
var broken = false;
|
|
2168
|
-
for (var j = 0; j < letters.length; j++) {
|
|
2169
|
-
var next = arg.slice(j+2);
|
|
2170
|
-
|
|
2171
|
-
if (next === '-') {
|
|
2172
|
-
setArg(letters[j], next, arg);
|
|
2173
|
-
continue;
|
|
2174
|
-
}
|
|
2175
|
-
|
|
2176
|
-
if (/[A-Za-z]/.test(letters[j]) && /=/.test(next)) {
|
|
2177
|
-
setArg(letters[j], next.split('=')[1], arg);
|
|
2178
|
-
broken = true;
|
|
2179
|
-
break;
|
|
2180
|
-
}
|
|
2181
|
-
|
|
2182
|
-
if (/[A-Za-z]/.test(letters[j])
|
|
2183
|
-
&& /-?\d+(\.\d*)?(e-?\d+)?$/.test(next)) {
|
|
2184
|
-
setArg(letters[j], next, arg);
|
|
2185
|
-
broken = true;
|
|
2186
|
-
break;
|
|
2187
|
-
}
|
|
2188
|
-
|
|
2189
|
-
if (letters[j+1] && letters[j+1].match(/\W/)) {
|
|
2190
|
-
setArg(letters[j], arg.slice(j+2), arg);
|
|
2191
|
-
broken = true;
|
|
2192
|
-
break;
|
|
2193
|
-
}
|
|
2194
|
-
else {
|
|
2195
|
-
setArg(letters[j], flags.strings[letters[j]] ? '' : true, arg);
|
|
2196
|
-
}
|
|
2197
|
-
}
|
|
2198
|
-
|
|
2199
|
-
var key = arg.slice(-1)[0];
|
|
2200
|
-
if (!broken && key !== '-') {
|
|
2201
|
-
if (args[i+1] && !/^(-|--)[^-]/.test(args[i+1])
|
|
2202
|
-
&& !flags.bools[key]
|
|
2203
|
-
&& (aliases[key] ? !aliasIsBoolean(key) : true)) {
|
|
2204
|
-
setArg(key, args[i+1], arg);
|
|
2205
|
-
i++;
|
|
2206
|
-
}
|
|
2207
|
-
else if (args[i+1] && /^(true|false)$/.test(args[i+1])) {
|
|
2208
|
-
setArg(key, args[i+1] === 'true', arg);
|
|
2209
|
-
i++;
|
|
2210
|
-
}
|
|
2211
|
-
else {
|
|
2212
|
-
setArg(key, flags.strings[key] ? '' : true, arg);
|
|
2213
|
-
}
|
|
2214
|
-
}
|
|
2215
|
-
}
|
|
2216
|
-
else {
|
|
2217
|
-
if (!flags.unknownFn || flags.unknownFn(arg) !== false) {
|
|
2218
|
-
argv._.push(
|
|
2219
|
-
flags.strings['_'] || !isNumber(arg) ? arg : Number(arg)
|
|
2220
|
-
);
|
|
2221
|
-
}
|
|
2222
|
-
if (opts.stopEarly) {
|
|
2223
|
-
argv._.push.apply(argv._, args.slice(i + 1));
|
|
2224
|
-
break;
|
|
2225
|
-
}
|
|
2226
|
-
}
|
|
2227
|
-
}
|
|
2228
|
-
|
|
2229
|
-
Object.keys(defaults).forEach(function (key) {
|
|
2230
|
-
if (!hasKey(argv, key.split('.'))) {
|
|
2231
|
-
setKey(argv, key.split('.'), defaults[key]);
|
|
2232
|
-
|
|
2233
|
-
(aliases[key] || []).forEach(function (x) {
|
|
2234
|
-
setKey(argv, x.split('.'), defaults[key]);
|
|
2235
|
-
});
|
|
2236
|
-
}
|
|
2237
|
-
});
|
|
2238
|
-
|
|
2239
|
-
if (opts['--']) {
|
|
2240
|
-
argv['--'] = new Array();
|
|
2241
|
-
notFlags.forEach(function(key) {
|
|
2242
|
-
argv['--'].push(key);
|
|
2243
|
-
});
|
|
2244
|
-
}
|
|
2245
|
-
else {
|
|
2246
|
-
notFlags.forEach(function(key) {
|
|
2247
|
-
argv._.push(key);
|
|
2248
|
-
});
|
|
2249
|
-
}
|
|
2250
|
-
|
|
2251
|
-
return argv;
|
|
2252
|
-
};
|
|
2253
|
-
|
|
2254
|
-
function hasKey (obj, keys) {
|
|
2255
|
-
var o = obj;
|
|
2256
|
-
keys.slice(0,-1).forEach(function (key) {
|
|
2257
|
-
o = (o[key] || {});
|
|
2258
|
-
});
|
|
2259
|
-
|
|
2260
|
-
var key = keys[keys.length - 1];
|
|
2261
|
-
return key in o;
|
|
2262
|
-
}
|
|
2263
|
-
|
|
2264
|
-
function isNumber (x) {
|
|
2265
|
-
if (typeof x === 'number') return true;
|
|
2266
|
-
if (/^0x[0-9a-f]+$/i.test(x)) return true;
|
|
2267
|
-
return /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(x);
|
|
2268
|
-
}
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
function isConstructorOrProto (obj, key) {
|
|
2272
|
-
return key === 'constructor' && typeof obj[key] === 'function' || key === '__proto__';
|
|
2273
|
-
}
|
|
2274
|
-
|
|
2275
|
-
function createCliContext(data) {
|
|
2276
|
-
return createEventContext({
|
|
2277
|
-
event: Object.assign(Object.assign({}, data), { type: 'CLI' }),
|
|
2278
|
-
});
|
|
2279
|
-
}
|
|
2280
|
-
function useCliContext() {
|
|
2281
|
-
return useEventContext('CLI');
|
|
2282
|
-
}
|
|
5
|
+
var moost = require('moost');
|
|
2283
6
|
|
|
2284
7
|
/******************************************************************************
|
|
2285
8
|
Copyright (c) Microsoft Corporation.
|
|
@@ -2306,79 +29,47 @@ function __awaiter(thisArg, _arguments, P, generator) {
|
|
|
2306
29
|
});
|
|
2307
30
|
}
|
|
2308
31
|
|
|
2309
|
-
const banner
|
|
32
|
+
const banner = () => `[${"@wooksjs/event-core"}][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `;
|
|
2310
33
|
|
|
2311
34
|
/* istanbul ignore file */
|
|
2312
|
-
function logError
|
|
2313
|
-
console.error('[91m' + '[1m' + banner
|
|
35
|
+
function logError(error) {
|
|
36
|
+
console.error('[91m' + '[1m' + banner() + error + '[0m');
|
|
2314
37
|
}
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
if (handlers) {
|
|
2331
|
-
try {
|
|
2332
|
-
for (const handler of handlers) {
|
|
2333
|
-
restoreCtx();
|
|
2334
|
-
const response = yield handler();
|
|
2335
|
-
if (typeof response === 'string') {
|
|
2336
|
-
console.log(response);
|
|
2337
|
-
}
|
|
2338
|
-
else if (Array.isArray(response)) {
|
|
2339
|
-
response.forEach(r => console.log(typeof r === 'string' ? r : JSON.stringify(r, null, ' ')));
|
|
2340
|
-
}
|
|
2341
|
-
else {
|
|
2342
|
-
console.log(JSON.stringify(response, null, ' '));
|
|
2343
|
-
}
|
|
2344
|
-
}
|
|
2345
|
-
}
|
|
2346
|
-
catch (e) {
|
|
2347
|
-
logError$1(e.message);
|
|
2348
|
-
process.exit(1);
|
|
2349
|
-
}
|
|
2350
|
-
clearCtx();
|
|
2351
|
-
}
|
|
2352
|
-
else {
|
|
2353
|
-
logError$1('Unknown command parameters');
|
|
2354
|
-
process.exit(1);
|
|
2355
|
-
}
|
|
2356
|
-
});
|
|
38
|
+
|
|
39
|
+
function panic(error) {
|
|
40
|
+
logError(error);
|
|
41
|
+
return new Error(error);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Use existing event context
|
|
45
|
+
*
|
|
46
|
+
* !Must be called syncronously while context is reachable
|
|
47
|
+
*
|
|
48
|
+
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
49
|
+
*/
|
|
50
|
+
function useEventContext(expectedTypes) {
|
|
51
|
+
{
|
|
52
|
+
throw panic('Event context does not exist. Use event context synchronously within the runtime of the event.');
|
|
2357
53
|
}
|
|
2358
54
|
}
|
|
2359
|
-
function createCliApp(opts, wooks) {
|
|
2360
|
-
return new WooksCli(opts, wooks);
|
|
2361
|
-
}
|
|
2362
55
|
|
|
2363
|
-
function
|
|
2364
|
-
const { store } =
|
|
2365
|
-
const
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
}
|
|
2369
|
-
return flags.value;
|
|
56
|
+
function useEventId() {
|
|
57
|
+
const { store } = useEventContext();
|
|
58
|
+
const { init } = store('event');
|
|
59
|
+
const getId = () => init('id', () => crypto.randomUUID());
|
|
60
|
+
return { getId };
|
|
2370
61
|
}
|
|
2371
62
|
|
|
2372
63
|
class MoostCli {
|
|
2373
64
|
constructor(cliApp) {
|
|
2374
|
-
if (cliApp && cliApp instanceof WooksCli) {
|
|
65
|
+
if (cliApp && cliApp instanceof eventCli.WooksCli) {
|
|
2375
66
|
this.cliApp = cliApp;
|
|
2376
67
|
}
|
|
2377
68
|
else if (cliApp) {
|
|
2378
|
-
this.cliApp = createCliApp(cliApp);
|
|
69
|
+
this.cliApp = eventCli.createCliApp(cliApp);
|
|
2379
70
|
}
|
|
2380
71
|
else {
|
|
2381
|
-
this.cliApp = createCliApp();
|
|
72
|
+
this.cliApp = eventCli.createCliApp();
|
|
2382
73
|
}
|
|
2383
74
|
}
|
|
2384
75
|
onInit() {
|
|
@@ -2392,8 +83,8 @@ class MoostCli {
|
|
|
2392
83
|
const path = typeof handler.path === 'string' ? handler.path : typeof opts.method === 'string' ? opts.method : '';
|
|
2393
84
|
const targetPath = `${opts.prefix || ''}/${path}`.replace(/\/\/+/g, '/');
|
|
2394
85
|
if (!fn) {
|
|
2395
|
-
fn = () => __awaiter
|
|
2396
|
-
const { restoreCtx } = useCliContext();
|
|
86
|
+
fn = () => __awaiter(this, void 0, void 0, function* () {
|
|
87
|
+
const { restoreCtx } = eventCli.useCliContext();
|
|
2397
88
|
const scopeId = useEventId().getId();
|
|
2398
89
|
const unscope = opts.registerEventScope(scopeId);
|
|
2399
90
|
const instance = yield opts.getInstance();
|
|
@@ -2438,339 +129,6 @@ class MoostCli {
|
|
|
2438
129
|
}
|
|
2439
130
|
}
|
|
2440
131
|
|
|
2441
|
-
var EHttpStatusCode;
|
|
2442
|
-
(function (EHttpStatusCode) {
|
|
2443
|
-
EHttpStatusCode[EHttpStatusCode["Continue"] = 100] = "Continue";
|
|
2444
|
-
EHttpStatusCode[EHttpStatusCode["SwitchingProtocols"] = 101] = "SwitchingProtocols";
|
|
2445
|
-
EHttpStatusCode[EHttpStatusCode["Processing"] = 102] = "Processing";
|
|
2446
|
-
EHttpStatusCode[EHttpStatusCode["EarlyHints"] = 103] = "EarlyHints";
|
|
2447
|
-
EHttpStatusCode[EHttpStatusCode["OK"] = 200] = "OK";
|
|
2448
|
-
EHttpStatusCode[EHttpStatusCode["Created"] = 201] = "Created";
|
|
2449
|
-
EHttpStatusCode[EHttpStatusCode["Accepted"] = 202] = "Accepted";
|
|
2450
|
-
EHttpStatusCode[EHttpStatusCode["NonAuthoritativeInformation"] = 203] = "NonAuthoritativeInformation";
|
|
2451
|
-
EHttpStatusCode[EHttpStatusCode["NoContent"] = 204] = "NoContent";
|
|
2452
|
-
EHttpStatusCode[EHttpStatusCode["ResetContent"] = 205] = "ResetContent";
|
|
2453
|
-
EHttpStatusCode[EHttpStatusCode["PartialContent"] = 206] = "PartialContent";
|
|
2454
|
-
EHttpStatusCode[EHttpStatusCode["MultiStatus"] = 207] = "MultiStatus";
|
|
2455
|
-
EHttpStatusCode[EHttpStatusCode["AlreadyReported"] = 208] = "AlreadyReported";
|
|
2456
|
-
EHttpStatusCode[EHttpStatusCode["IMUsed"] = 226] = "IMUsed";
|
|
2457
|
-
EHttpStatusCode[EHttpStatusCode["MultipleChoices"] = 300] = "MultipleChoices";
|
|
2458
|
-
EHttpStatusCode[EHttpStatusCode["MovedPermanently"] = 301] = "MovedPermanently";
|
|
2459
|
-
EHttpStatusCode[EHttpStatusCode["Found"] = 302] = "Found";
|
|
2460
|
-
EHttpStatusCode[EHttpStatusCode["SeeOther"] = 303] = "SeeOther";
|
|
2461
|
-
EHttpStatusCode[EHttpStatusCode["NotModified"] = 304] = "NotModified";
|
|
2462
|
-
EHttpStatusCode[EHttpStatusCode["UseProxy"] = 305] = "UseProxy";
|
|
2463
|
-
EHttpStatusCode[EHttpStatusCode["SwitchProxy"] = 306] = "SwitchProxy";
|
|
2464
|
-
EHttpStatusCode[EHttpStatusCode["TemporaryRedirect"] = 307] = "TemporaryRedirect";
|
|
2465
|
-
EHttpStatusCode[EHttpStatusCode["PermanentRedirect"] = 308] = "PermanentRedirect";
|
|
2466
|
-
EHttpStatusCode[EHttpStatusCode["BadRequest"] = 400] = "BadRequest";
|
|
2467
|
-
EHttpStatusCode[EHttpStatusCode["Unauthorized"] = 401] = "Unauthorized";
|
|
2468
|
-
EHttpStatusCode[EHttpStatusCode["PaymentRequired"] = 402] = "PaymentRequired";
|
|
2469
|
-
EHttpStatusCode[EHttpStatusCode["Forbidden"] = 403] = "Forbidden";
|
|
2470
|
-
EHttpStatusCode[EHttpStatusCode["NotFound"] = 404] = "NotFound";
|
|
2471
|
-
EHttpStatusCode[EHttpStatusCode["MethodNotAllowed"] = 405] = "MethodNotAllowed";
|
|
2472
|
-
EHttpStatusCode[EHttpStatusCode["NotAcceptable"] = 406] = "NotAcceptable";
|
|
2473
|
-
EHttpStatusCode[EHttpStatusCode["ProxyAuthenticationRequired"] = 407] = "ProxyAuthenticationRequired";
|
|
2474
|
-
EHttpStatusCode[EHttpStatusCode["RequestTimeout"] = 408] = "RequestTimeout";
|
|
2475
|
-
EHttpStatusCode[EHttpStatusCode["Conflict"] = 409] = "Conflict";
|
|
2476
|
-
EHttpStatusCode[EHttpStatusCode["Gone"] = 410] = "Gone";
|
|
2477
|
-
EHttpStatusCode[EHttpStatusCode["LengthRequired"] = 411] = "LengthRequired";
|
|
2478
|
-
EHttpStatusCode[EHttpStatusCode["PreconditionFailed"] = 412] = "PreconditionFailed";
|
|
2479
|
-
EHttpStatusCode[EHttpStatusCode["PayloadTooLarge"] = 413] = "PayloadTooLarge";
|
|
2480
|
-
EHttpStatusCode[EHttpStatusCode["URITooLong"] = 414] = "URITooLong";
|
|
2481
|
-
EHttpStatusCode[EHttpStatusCode["UnsupportedMediaType"] = 415] = "UnsupportedMediaType";
|
|
2482
|
-
EHttpStatusCode[EHttpStatusCode["RangeNotSatisfiable"] = 416] = "RangeNotSatisfiable";
|
|
2483
|
-
EHttpStatusCode[EHttpStatusCode["ExpectationFailed"] = 417] = "ExpectationFailed";
|
|
2484
|
-
EHttpStatusCode[EHttpStatusCode["ImATeapot"] = 418] = "ImATeapot";
|
|
2485
|
-
EHttpStatusCode[EHttpStatusCode["MisdirectedRequest"] = 421] = "MisdirectedRequest";
|
|
2486
|
-
EHttpStatusCode[EHttpStatusCode["UnprocessableEntity"] = 422] = "UnprocessableEntity";
|
|
2487
|
-
EHttpStatusCode[EHttpStatusCode["Locked"] = 423] = "Locked";
|
|
2488
|
-
EHttpStatusCode[EHttpStatusCode["FailedDependency"] = 424] = "FailedDependency";
|
|
2489
|
-
EHttpStatusCode[EHttpStatusCode["TooEarly"] = 425] = "TooEarly";
|
|
2490
|
-
EHttpStatusCode[EHttpStatusCode["UpgradeRequired"] = 426] = "UpgradeRequired";
|
|
2491
|
-
EHttpStatusCode[EHttpStatusCode["PreconditionRequired"] = 428] = "PreconditionRequired";
|
|
2492
|
-
EHttpStatusCode[EHttpStatusCode["TooManyRequests"] = 429] = "TooManyRequests";
|
|
2493
|
-
EHttpStatusCode[EHttpStatusCode["RequestHeaderFieldsTooLarge"] = 431] = "RequestHeaderFieldsTooLarge";
|
|
2494
|
-
EHttpStatusCode[EHttpStatusCode["UnavailableForLegalReasons"] = 451] = "UnavailableForLegalReasons";
|
|
2495
|
-
EHttpStatusCode[EHttpStatusCode["InternalServerError"] = 500] = "InternalServerError";
|
|
2496
|
-
EHttpStatusCode[EHttpStatusCode["NotImplemented"] = 501] = "NotImplemented";
|
|
2497
|
-
EHttpStatusCode[EHttpStatusCode["BadGateway"] = 502] = "BadGateway";
|
|
2498
|
-
EHttpStatusCode[EHttpStatusCode["ServiceUnavailable"] = 503] = "ServiceUnavailable";
|
|
2499
|
-
EHttpStatusCode[EHttpStatusCode["GatewayTimeout"] = 504] = "GatewayTimeout";
|
|
2500
|
-
EHttpStatusCode[EHttpStatusCode["HTTPVersionNotSupported"] = 505] = "HTTPVersionNotSupported";
|
|
2501
|
-
EHttpStatusCode[EHttpStatusCode["VariantAlsoNegotiates"] = 506] = "VariantAlsoNegotiates";
|
|
2502
|
-
EHttpStatusCode[EHttpStatusCode["InsufficientStorage"] = 507] = "InsufficientStorage";
|
|
2503
|
-
EHttpStatusCode[EHttpStatusCode["LoopDetected"] = 508] = "LoopDetected";
|
|
2504
|
-
EHttpStatusCode[EHttpStatusCode["NotExtended"] = 510] = "NotExtended";
|
|
2505
|
-
EHttpStatusCode[EHttpStatusCode["NetworkAuthenticationRequired"] = 511] = "NetworkAuthenticationRequired";
|
|
2506
|
-
})(EHttpStatusCode || (EHttpStatusCode = {}));
|
|
2507
|
-
|
|
2508
|
-
({
|
|
2509
|
-
GET: EHttpStatusCode.OK,
|
|
2510
|
-
POST: EHttpStatusCode.Created,
|
|
2511
|
-
PUT: EHttpStatusCode.Created,
|
|
2512
|
-
PATCH: EHttpStatusCode.Accepted,
|
|
2513
|
-
DELETE: EHttpStatusCode.Accepted,
|
|
2514
|
-
});
|
|
2515
|
-
|
|
2516
|
-
function getConstructor(instance) {
|
|
2517
|
-
return isConstructor(instance) ?
|
|
2518
|
-
instance : instance.constructor ?
|
|
2519
|
-
instance.constructor : Object.getPrototypeOf(instance).constructor;
|
|
2520
|
-
}
|
|
2521
|
-
function isConstructor(v) {
|
|
2522
|
-
return typeof v === 'function' && Object.getOwnPropertyNames(v).includes('prototype') && !Object.getOwnPropertyNames(v).includes('caller') && !!v.name;
|
|
2523
|
-
}
|
|
2524
|
-
|
|
2525
|
-
const banner = () => `[prostojs/mate][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `;
|
|
2526
|
-
|
|
2527
|
-
function warn(text) {
|
|
2528
|
-
console.log('[33m' + banner() + text + '[0m');
|
|
2529
|
-
}
|
|
2530
|
-
function logError(error) {
|
|
2531
|
-
console.error('[91m' + '[1m' + banner() + error + '[0m');
|
|
2532
|
-
}
|
|
2533
|
-
|
|
2534
|
-
const classMetadata = {};
|
|
2535
|
-
const paramMetadata = {};
|
|
2536
|
-
const root = typeof global === 'object' ? global : typeof self === 'object' ? self : {};
|
|
2537
|
-
function getMetaObject(target, prop) {
|
|
2538
|
-
const isParam = typeof prop !== 'undefined';
|
|
2539
|
-
const metadata = isParam ? paramMetadata : classMetadata;
|
|
2540
|
-
const targetKey = Symbol.for(getConstructor(target));
|
|
2541
|
-
let meta = metadata[targetKey] = metadata[targetKey] || {};
|
|
2542
|
-
if (isParam)
|
|
2543
|
-
meta = (meta[prop] = meta[prop] || {});
|
|
2544
|
-
return meta;
|
|
2545
|
-
}
|
|
2546
|
-
const _reflect = {
|
|
2547
|
-
getOwnMetadata(key, target, prop) {
|
|
2548
|
-
return getMetaObject(target, prop)[key];
|
|
2549
|
-
},
|
|
2550
|
-
defineMetadata(key, data, target, prop) {
|
|
2551
|
-
const meta = getMetaObject(target, prop);
|
|
2552
|
-
meta[key] = data;
|
|
2553
|
-
},
|
|
2554
|
-
metadata(key, data) {
|
|
2555
|
-
return ((target, propKey) => {
|
|
2556
|
-
Reflect$1.defineMetadata(key, data, target, propKey);
|
|
2557
|
-
});
|
|
2558
|
-
},
|
|
2559
|
-
};
|
|
2560
|
-
if (!root.Reflect) {
|
|
2561
|
-
root.Reflect = _reflect;
|
|
2562
|
-
}
|
|
2563
|
-
else {
|
|
2564
|
-
const funcs = [
|
|
2565
|
-
'getOwnMetadata',
|
|
2566
|
-
'defineMetadata',
|
|
2567
|
-
'metadata',
|
|
2568
|
-
];
|
|
2569
|
-
const target = root.Reflect;
|
|
2570
|
-
let isOriginalReflectMetadata = true;
|
|
2571
|
-
for (const func of funcs) {
|
|
2572
|
-
if (typeof target[func] !== 'function') {
|
|
2573
|
-
Object.defineProperty(target, func, { configurable: true, writable: true, value: _reflect[func] });
|
|
2574
|
-
isOriginalReflectMetadata = false;
|
|
2575
|
-
}
|
|
2576
|
-
}
|
|
2577
|
-
if (!isOriginalReflectMetadata) {
|
|
2578
|
-
warn('A limited \'reflect-metadata\' implementation is used. In case of any issues include original \'reflect-metadata\' package and require it before any @prostojs/mate import.');
|
|
2579
|
-
}
|
|
2580
|
-
}
|
|
2581
|
-
const Reflect$1 = _reflect;
|
|
2582
|
-
|
|
2583
|
-
function panic(error) {
|
|
2584
|
-
logError(error);
|
|
2585
|
-
return new Error(error);
|
|
2586
|
-
}
|
|
2587
|
-
|
|
2588
|
-
const Reflect = (global === null || global === void 0 ? void 0 : global.Reflect) || (self === null || self === void 0 ? void 0 : self.Reflect) || Reflect$1;
|
|
2589
|
-
class Mate {
|
|
2590
|
-
constructor(workspace, options = {}) {
|
|
2591
|
-
this.workspace = workspace;
|
|
2592
|
-
this.options = options;
|
|
2593
|
-
}
|
|
2594
|
-
set(args, key, value, isArray) {
|
|
2595
|
-
var _a;
|
|
2596
|
-
const newArgs = args.level === 'CLASS' ? { target: args.target }
|
|
2597
|
-
: args.level === 'PROPERTY' ? { target: args.target, propKey: args.propKey }
|
|
2598
|
-
: args;
|
|
2599
|
-
let meta = Reflect.getOwnMetadata(this.workspace, newArgs.target, newArgs.propKey) || {};
|
|
2600
|
-
if (newArgs.propKey && this.options.readReturnType && !meta.returnType) {
|
|
2601
|
-
meta.returnType = Reflect.getOwnMetadata('design:returntype', newArgs.target, newArgs.propKey);
|
|
2602
|
-
}
|
|
2603
|
-
if (newArgs.propKey && this.options.readType && !meta.type) {
|
|
2604
|
-
meta.type = Reflect.getOwnMetadata('design:type', newArgs.target, newArgs.propKey);
|
|
2605
|
-
}
|
|
2606
|
-
const { index } = newArgs;
|
|
2607
|
-
const cb = typeof key === 'function' ? key : undefined;
|
|
2608
|
-
let data = meta;
|
|
2609
|
-
if (!data.params) {
|
|
2610
|
-
data.params = (_a = Reflect.getOwnMetadata('design:paramtypes', newArgs.target, newArgs.propKey)) === null || _a === void 0 ? void 0 : _a.map((f) => ({ type: f }));
|
|
2611
|
-
}
|
|
2612
|
-
if (typeof index === 'number') {
|
|
2613
|
-
data.params = data.params || [];
|
|
2614
|
-
data.params[index] = data.params[index] || {
|
|
2615
|
-
type: undefined,
|
|
2616
|
-
};
|
|
2617
|
-
if (cb) {
|
|
2618
|
-
data.params[index] = cb(data.params[index], args.propKey, typeof args.index === 'number' ? args.index : undefined);
|
|
2619
|
-
}
|
|
2620
|
-
else {
|
|
2621
|
-
data = data.params[index];
|
|
2622
|
-
}
|
|
2623
|
-
}
|
|
2624
|
-
if (typeof key !== 'function') {
|
|
2625
|
-
if (isArray) {
|
|
2626
|
-
const newArray = (data[key] || []);
|
|
2627
|
-
if (!Array.isArray(newArray)) {
|
|
2628
|
-
panic('Mate.add (isArray=true) called for non-array metadata');
|
|
2629
|
-
}
|
|
2630
|
-
newArray.unshift(value);
|
|
2631
|
-
data[key] = newArray;
|
|
2632
|
-
}
|
|
2633
|
-
else {
|
|
2634
|
-
data[key] = value;
|
|
2635
|
-
}
|
|
2636
|
-
}
|
|
2637
|
-
else if (cb && typeof index !== 'number') {
|
|
2638
|
-
meta = cb(data, args.propKey, typeof args.index === 'number' ? args.index : undefined);
|
|
2639
|
-
}
|
|
2640
|
-
Reflect.defineMetadata(this.workspace, meta, newArgs.target, newArgs.propKey);
|
|
2641
|
-
}
|
|
2642
|
-
read(target, propKey, index) {
|
|
2643
|
-
const isConstr = isConstructor(target);
|
|
2644
|
-
const constructor = isConstr ? target : getConstructor(target);
|
|
2645
|
-
const proto = constructor.prototype;
|
|
2646
|
-
let ownMeta = Reflect.getOwnMetadata(this.workspace, typeof propKey === 'string' ? proto : constructor, propKey);
|
|
2647
|
-
if (this.options.inherit) {
|
|
2648
|
-
const inheritFn = typeof this.options.inherit === 'function' ? this.options.inherit : undefined;
|
|
2649
|
-
let shouldInherit = this.options.inherit;
|
|
2650
|
-
if (inheritFn) {
|
|
2651
|
-
if (typeof propKey === 'string') {
|
|
2652
|
-
const classMeta = Reflect.getOwnMetadata(this.workspace, constructor);
|
|
2653
|
-
shouldInherit = inheritFn(classMeta, propKey, ownMeta);
|
|
2654
|
-
}
|
|
2655
|
-
else {
|
|
2656
|
-
shouldInherit = inheritFn(ownMeta);
|
|
2657
|
-
}
|
|
2658
|
-
}
|
|
2659
|
-
if (shouldInherit) {
|
|
2660
|
-
const parent = Object.getPrototypeOf(constructor);
|
|
2661
|
-
if (typeof parent === 'function' && parent !== fnProto && parent !== constructor) {
|
|
2662
|
-
const inheritedMeta = this.read(parent, propKey);
|
|
2663
|
-
ownMeta = { ...inheritedMeta, ...ownMeta, params: ownMeta === null || ownMeta === void 0 ? void 0 : ownMeta.params };
|
|
2664
|
-
}
|
|
2665
|
-
}
|
|
2666
|
-
}
|
|
2667
|
-
return ownMeta;
|
|
2668
|
-
}
|
|
2669
|
-
apply(...decorators) {
|
|
2670
|
-
return ((target, propKey, descriptor) => {
|
|
2671
|
-
for (const d of decorators) {
|
|
2672
|
-
d(target, propKey, descriptor);
|
|
2673
|
-
}
|
|
2674
|
-
});
|
|
2675
|
-
}
|
|
2676
|
-
decorate(key, value, isArray, level) {
|
|
2677
|
-
return ((target, propKey, descriptor) => {
|
|
2678
|
-
const args = {
|
|
2679
|
-
target,
|
|
2680
|
-
propKey,
|
|
2681
|
-
descriptor: typeof descriptor === 'number' ? undefined : descriptor,
|
|
2682
|
-
index: typeof descriptor === 'number' ? descriptor : undefined,
|
|
2683
|
-
level,
|
|
2684
|
-
};
|
|
2685
|
-
this.set(args, key, value, isArray);
|
|
2686
|
-
});
|
|
2687
|
-
}
|
|
2688
|
-
decorateClass(key, value, isArray) {
|
|
2689
|
-
return this.decorate(key, value, isArray, 'CLASS');
|
|
2690
|
-
}
|
|
2691
|
-
}
|
|
2692
|
-
const fnProto = Object.getPrototypeOf(Function);
|
|
2693
|
-
|
|
2694
|
-
const METADATA_WORKSPACE = 'moost';
|
|
2695
|
-
const moostMate = new Mate(METADATA_WORKSPACE, {
|
|
2696
|
-
readType: true,
|
|
2697
|
-
readReturnType: true,
|
|
2698
|
-
});
|
|
2699
|
-
function getMoostMate() {
|
|
2700
|
-
return moostMate;
|
|
2701
|
-
}
|
|
2702
|
-
|
|
2703
|
-
var TPipePriority;
|
|
2704
|
-
(function (TPipePriority) {
|
|
2705
|
-
TPipePriority[TPipePriority["BEFORE_RESOLVE"] = 0] = "BEFORE_RESOLVE";
|
|
2706
|
-
TPipePriority[TPipePriority["RESOLVE"] = 1] = "RESOLVE";
|
|
2707
|
-
TPipePriority[TPipePriority["AFTER_RESOLVE"] = 2] = "AFTER_RESOLVE";
|
|
2708
|
-
TPipePriority[TPipePriority["BEFORE_TRANSFORM"] = 3] = "BEFORE_TRANSFORM";
|
|
2709
|
-
TPipePriority[TPipePriority["TRANSFORM"] = 4] = "TRANSFORM";
|
|
2710
|
-
TPipePriority[TPipePriority["AFTER_TRANSFORM"] = 5] = "AFTER_TRANSFORM";
|
|
2711
|
-
TPipePriority[TPipePriority["BEFORE_VALIDATE"] = 6] = "BEFORE_VALIDATE";
|
|
2712
|
-
TPipePriority[TPipePriority["VALIDATE"] = 7] = "VALIDATE";
|
|
2713
|
-
TPipePriority[TPipePriority["AFTER_VALIDATE"] = 8] = "AFTER_VALIDATE";
|
|
2714
|
-
})(TPipePriority || (TPipePriority = {}));
|
|
2715
|
-
|
|
2716
|
-
const resolvePipe = (_value, meta) => {
|
|
2717
|
-
if (meta === null || meta === void 0 ? void 0 : meta.resolver) {
|
|
2718
|
-
return meta.resolver();
|
|
2719
|
-
}
|
|
2720
|
-
return undefined;
|
|
2721
|
-
};
|
|
2722
|
-
resolvePipe.priority = TPipePriority.RESOLVE;
|
|
2723
|
-
|
|
2724
|
-
[
|
|
2725
|
-
{
|
|
2726
|
-
handler: resolvePipe,
|
|
2727
|
-
priority: TPipePriority.RESOLVE,
|
|
2728
|
-
},
|
|
2729
|
-
];
|
|
2730
|
-
|
|
2731
|
-
function Label(value) {
|
|
2732
|
-
return getMoostMate().decorate('label', value);
|
|
2733
|
-
}
|
|
2734
|
-
|
|
2735
|
-
/**
|
|
2736
|
-
* Hook to the Response Status
|
|
2737
|
-
* @decorator
|
|
2738
|
-
* @param resolver - resolver function
|
|
2739
|
-
* @param label - field label
|
|
2740
|
-
* @paramType unknown
|
|
2741
|
-
*/
|
|
2742
|
-
function Resolve(resolver, label) {
|
|
2743
|
-
return (target, key, index) => {
|
|
2744
|
-
fillLabel(target, key, index, label);
|
|
2745
|
-
getMoostMate().decorate('resolver', resolver)(target, key, index);
|
|
2746
|
-
};
|
|
2747
|
-
}
|
|
2748
|
-
function fillLabel(target, key, index, name) {
|
|
2749
|
-
if (name) {
|
|
2750
|
-
const meta = getMoostMate().read(target, key);
|
|
2751
|
-
if (!(meta === null || meta === void 0 ? void 0 : meta.params) || !(meta === null || meta === void 0 ? void 0 : meta.params[index]) || !(meta === null || meta === void 0 ? void 0 : meta.params[index].label)) {
|
|
2752
|
-
Label(name)(target, key, index);
|
|
2753
|
-
}
|
|
2754
|
-
}
|
|
2755
|
-
}
|
|
2756
|
-
|
|
2757
|
-
getMoostMate().decorate((meta) => {
|
|
2758
|
-
if (!meta.injectable)
|
|
2759
|
-
meta.injectable = true;
|
|
2760
|
-
return meta;
|
|
2761
|
-
});
|
|
2762
|
-
|
|
2763
|
-
var TInterceptorPriority;
|
|
2764
|
-
(function (TInterceptorPriority) {
|
|
2765
|
-
TInterceptorPriority[TInterceptorPriority["BEFORE_ALL"] = 0] = "BEFORE_ALL";
|
|
2766
|
-
TInterceptorPriority[TInterceptorPriority["BEFORE_GUARD"] = 1] = "BEFORE_GUARD";
|
|
2767
|
-
TInterceptorPriority[TInterceptorPriority["GUARD"] = 2] = "GUARD";
|
|
2768
|
-
TInterceptorPriority[TInterceptorPriority["AFTER_GUARD"] = 3] = "AFTER_GUARD";
|
|
2769
|
-
TInterceptorPriority[TInterceptorPriority["INTERCEPTOR"] = 4] = "INTERCEPTOR";
|
|
2770
|
-
TInterceptorPriority[TInterceptorPriority["CATCH_ERROR"] = 5] = "CATCH_ERROR";
|
|
2771
|
-
TInterceptorPriority[TInterceptorPriority["AFTER_ALL"] = 6] = "AFTER_ALL";
|
|
2772
|
-
})(TInterceptorPriority || (TInterceptorPriority = {}));
|
|
2773
|
-
|
|
2774
132
|
/**
|
|
2775
133
|
* Get Cli Flag
|
|
2776
134
|
* @decorator
|
|
@@ -2778,7 +136,7 @@ var TInterceptorPriority;
|
|
|
2778
136
|
* @paramType string
|
|
2779
137
|
*/
|
|
2780
138
|
function Flag(name) {
|
|
2781
|
-
return Resolve(() => useFlags()[name], name);
|
|
139
|
+
return moost.Resolve(() => eventCli.useFlags()[name], name);
|
|
2782
140
|
}
|
|
2783
141
|
/**
|
|
2784
142
|
* Get Cli Flags
|
|
@@ -2786,11 +144,11 @@ function Flag(name) {
|
|
|
2786
144
|
* @paramType object
|
|
2787
145
|
*/
|
|
2788
146
|
function Flags() {
|
|
2789
|
-
return Resolve(() => useFlags(), 'flags');
|
|
147
|
+
return moost.Resolve(() => eventCli.useFlags(), 'flags');
|
|
2790
148
|
}
|
|
2791
149
|
|
|
2792
150
|
function Cli(path) {
|
|
2793
|
-
return getMoostMate().decorate('handlers', { path, type: 'CLI' }, true);
|
|
151
|
+
return moost.getMoostMate().decorate('handlers', { path, type: 'CLI' }, true);
|
|
2794
152
|
}
|
|
2795
153
|
|
|
2796
154
|
exports.Cli = Cli;
|