@moostjs/otel 0.5.21 → 0.5.22

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