@art-ws/common 1.0.3-rc.6 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{cancellation → es/cancellation}/cancellation-token-source.d.ts +1 -2
- package/dist/es/cancellation/cancellation-token-source.js +24 -0
- package/dist/{cancellation → es/cancellation}/cancellation-token.d.ts +1 -2
- package/dist/{cancellation → es/cancellation}/cancellation-token.js +4 -8
- package/dist/{cancellation → es/cancellation}/index.d.ts +0 -1
- package/dist/es/cancellation/index.js +4 -0
- package/dist/{cancellation → es/cancellation}/interfaces.d.ts +1 -2
- package/dist/es/cancellation/interfaces.js +5 -0
- package/dist/{heart-beat → es/heart-beat}/heart-beat.class.d.ts +1 -2
- package/dist/{heart-beat → es/heart-beat}/heart-beat.class.js +10 -15
- package/dist/{heart-beat → es/heart-beat}/heart-beat.types.d.ts +0 -1
- package/dist/es/heart-beat/heart-beat.types.js +1 -0
- package/dist/{heart-beat → es/heart-beat}/index.d.ts +0 -1
- package/dist/es/heart-beat/index.js +3 -0
- package/dist/{hr-time.d.ts → es/hr-time.d.ts} +1 -2
- package/dist/es/hr-time.js +9 -0
- package/dist/{index.d.ts → es/index.d.ts} +1 -1
- package/dist/es/index.js +13 -0
- package/dist/es/interpolate.d.ts +5 -0
- package/dist/es/interpolate.js +150 -0
- package/dist/es/json-file-loader.d.ts +25 -0
- package/dist/es/json-file-loader.js +108 -0
- package/dist/es/json-refs-resolver.d.ts +6 -0
- package/dist/{json-refs-resolver.js → es/json-refs-resolver.js} +13 -18
- package/dist/es/managed-pool/index.d.ts +1 -0
- package/dist/es/managed-pool/index.js +2 -0
- package/dist/{managed-pool → es/managed-pool}/managed-pool.d.ts +0 -1
- package/dist/{managed-pool → es/managed-pool}/managed-pool.js +9 -10
- package/dist/{managed-promise.d.ts → es/managed-promise.d.ts} +1 -1
- package/dist/es/managed-promise.js +24 -0
- package/dist/{mem-cache → es/mem-cache}/cache-stat.d.ts +0 -1
- package/dist/{mem-cache → es/mem-cache}/cache-stat.js +5 -12
- package/dist/{mem-cache → es/mem-cache}/index.d.ts +0 -1
- package/dist/es/mem-cache/index.js +3 -0
- package/dist/{mem-cache → es/mem-cache}/memory-cache.d.ts +3 -4
- package/dist/{mem-cache → es/mem-cache}/memory-cache.js +20 -21
- package/dist/{node-zone.d.ts → es/node-zone.d.ts} +3 -4
- package/dist/{node-zone.js → es/node-zone.js} +14 -22
- package/dist/es/object-hash/index.d.ts +1 -0
- package/dist/es/object-hash/index.js +2 -0
- package/dist/es/object-hash/object-hash.d.ts +1 -0
- package/dist/es/object-hash/object-hash.js +3 -0
- package/dist/es/shared/api-client/api-client.interface.d.ts +63 -0
- package/dist/es/shared/api-client/api-client.interface.js +15 -0
- package/dist/es/shared/api-client/decorators.d.ts +8 -0
- package/dist/es/shared/api-client/decorators.js +73 -0
- package/dist/es/shared/api-client/index.d.ts +2 -0
- package/dist/es/shared/api-client/index.js +3 -0
- package/dist/es/shared/api-client/make-url.d.ts +6 -0
- package/dist/es/shared/api-client/make-url.js +32 -0
- package/dist/{shared → es/shared}/assert-dsl.d.ts +4 -2
- package/dist/{shared → es/shared}/assert-dsl.js +30 -16
- package/dist/es/shared/await-if-async.d.ts +1 -0
- package/dist/es/shared/await-if-async.js +5 -0
- package/dist/{shared → es/shared}/disposable.d.ts +0 -1
- package/dist/es/shared/disposable.js +1 -0
- package/dist/{shared → es/shared}/index.d.ts +2 -1
- package/dist/es/shared/index.js +9 -0
- package/dist/{shared → es/shared}/optional.d.ts +0 -1
- package/dist/{shared → es/shared}/optional.js +6 -10
- package/dist/es/shared/promise-race.d.ts +7 -0
- package/dist/{shared → es/shared}/promise-race.js +10 -18
- package/dist/{shared → es/shared}/runnable.d.ts +1 -2
- package/dist/es/shared/runnable.js +6 -0
- package/dist/{shared → es/shared}/safe-call.d.ts +1 -2
- package/dist/es/shared/safe-call.js +8 -0
- package/package.json +37 -63
- package/dist/cancellation/cancellation-token-source.d.ts.map +0 -1
- package/dist/cancellation/cancellation-token-source.js +0 -31
- package/dist/cancellation/cancellation-token-source.js.map +0 -1
- package/dist/cancellation/cancellation-token.d.ts.map +0 -1
- package/dist/cancellation/cancellation-token.js.map +0 -1
- package/dist/cancellation/index.d.ts.map +0 -1
- package/dist/cancellation/index.js +0 -8
- package/dist/cancellation/index.js.map +0 -1
- package/dist/cancellation/interfaces.d.ts.map +0 -1
- package/dist/cancellation/interfaces.js +0 -10
- package/dist/cancellation/interfaces.js.map +0 -1
- package/dist/heart-beat/heart-beat.class.d.ts.map +0 -1
- package/dist/heart-beat/heart-beat.class.js.map +0 -1
- package/dist/heart-beat/heart-beat.types.d.ts.map +0 -1
- package/dist/heart-beat/heart-beat.types.js +0 -3
- package/dist/heart-beat/heart-beat.types.js.map +0 -1
- package/dist/heart-beat/index.d.ts.map +0 -1
- package/dist/heart-beat/index.js +0 -7
- package/dist/heart-beat/index.js.map +0 -1
- package/dist/hr-time.d.ts.map +0 -1
- package/dist/hr-time.js +0 -15
- package/dist/hr-time.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -16
- package/dist/index.js.map +0 -1
- package/dist/interpolate.d.ts +0 -3
- package/dist/interpolate.d.ts.map +0 -1
- package/dist/interpolate.js +0 -88
- package/dist/interpolate.js.map +0 -1
- package/dist/json-file-loader.d.ts +0 -17
- package/dist/json-file-loader.d.ts.map +0 -1
- package/dist/json-file-loader.js +0 -83
- package/dist/json-file-loader.js.map +0 -1
- package/dist/json-refs-resolver.d.ts +0 -7
- package/dist/json-refs-resolver.d.ts.map +0 -1
- package/dist/json-refs-resolver.js.map +0 -1
- package/dist/managed-pool/index.d.ts +0 -2
- package/dist/managed-pool/index.d.ts.map +0 -1
- package/dist/managed-pool/index.js +0 -6
- package/dist/managed-pool/index.js.map +0 -1
- package/dist/managed-pool/managed-pool.d.ts.map +0 -1
- package/dist/managed-pool/managed-pool.js.map +0 -1
- package/dist/managed-promise.d.ts.map +0 -1
- package/dist/managed-promise.js +0 -26
- package/dist/managed-promise.js.map +0 -1
- package/dist/mem-cache/cache-stat.d.ts.map +0 -1
- package/dist/mem-cache/cache-stat.js.map +0 -1
- package/dist/mem-cache/index.d.ts.map +0 -1
- package/dist/mem-cache/index.js +0 -7
- package/dist/mem-cache/index.js.map +0 -1
- package/dist/mem-cache/memory-cache.d.ts.map +0 -1
- package/dist/mem-cache/memory-cache.js.map +0 -1
- package/dist/node-zone.d.ts.map +0 -1
- package/dist/node-zone.js.map +0 -1
- package/dist/shared/assert-dsl.d.ts.map +0 -1
- package/dist/shared/assert-dsl.js.map +0 -1
- package/dist/shared/disposable.d.ts.map +0 -1
- package/dist/shared/disposable.js +0 -3
- package/dist/shared/disposable.js.map +0 -1
- package/dist/shared/index.d.ts.map +0 -1
- package/dist/shared/index.js +0 -11
- package/dist/shared/index.js.map +0 -1
- package/dist/shared/optional.d.ts.map +0 -1
- package/dist/shared/optional.js.map +0 -1
- package/dist/shared/promise-race.d.ts +0 -8
- package/dist/shared/promise-race.d.ts.map +0 -1
- package/dist/shared/promise-race.js.map +0 -1
- package/dist/shared/runnable.d.ts.map +0 -1
- package/dist/shared/runnable.js +0 -12
- package/dist/shared/runnable.js.map +0 -1
- package/dist/shared/safe-call.d.ts.map +0 -1
- package/dist/shared/safe-call.js +0 -13
- package/dist/shared/safe-call.js.map +0 -1
@@ -1,4 +1,4 @@
|
|
1
|
-
import { ICancellationToken, ICancellationTokenSource, OnCancelCallback } from "./interfaces";
|
1
|
+
import type { ICancellationToken, ICancellationTokenSource, OnCancelCallback } from "./interfaces";
|
2
2
|
export declare class CancellationTokenSource implements ICancellationTokenSource {
|
3
3
|
private _token;
|
4
4
|
isCancellationRequested: boolean;
|
@@ -8,4 +8,3 @@ export declare class CancellationTokenSource implements ICancellationTokenSource
|
|
8
8
|
token(): ICancellationToken;
|
9
9
|
dispose(): void;
|
10
10
|
}
|
11
|
-
//# sourceMappingURL=cancellation-token-source.d.ts.map
|
@@ -0,0 +1,24 @@
|
|
1
|
+
import { EventEmitter } from "events";
|
2
|
+
import { CancellationToken } from "./cancellation-token";
|
3
|
+
const ON_CANCEL = "ON_CANCEL";
|
4
|
+
export class CancellationTokenSource {
|
5
|
+
_token = null;
|
6
|
+
isCancellationRequested = false;
|
7
|
+
eventEmitter = new EventEmitter();
|
8
|
+
cancel(reason) {
|
9
|
+
this.isCancellationRequested = true;
|
10
|
+
this.eventEmitter.emit(ON_CANCEL, { reason });
|
11
|
+
}
|
12
|
+
onCancel(callback) {
|
13
|
+
this.eventEmitter.addListener(ON_CANCEL, callback);
|
14
|
+
}
|
15
|
+
token() {
|
16
|
+
if (!this._token) {
|
17
|
+
this._token = new CancellationToken(this);
|
18
|
+
}
|
19
|
+
return this._token;
|
20
|
+
}
|
21
|
+
dispose() {
|
22
|
+
this.eventEmitter.removeAllListeners();
|
23
|
+
}
|
24
|
+
}
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { ICancellationToken, ICancellationTokenSource, OnCancelCallback } from "./interfaces";
|
1
|
+
import type { ICancellationToken, ICancellationTokenSource, OnCancelCallback } from "./interfaces";
|
2
2
|
export declare class CancellationToken implements ICancellationToken {
|
3
3
|
private readonly source;
|
4
4
|
constructor(source: ICancellationTokenSource);
|
@@ -6,4 +6,3 @@ export declare class CancellationToken implements ICancellationToken {
|
|
6
6
|
onCancel(callback: OnCancelCallback): void;
|
7
7
|
throwIfCancellationRequested(): void;
|
8
8
|
}
|
9
|
-
//# sourceMappingURL=cancellation-token.d.ts.map
|
@@ -1,8 +1,6 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
const interfaces_1 = require("./interfaces");
|
5
|
-
class CancellationToken {
|
1
|
+
import { ErrorCancelled } from "./interfaces";
|
2
|
+
export class CancellationToken {
|
3
|
+
source;
|
6
4
|
constructor(source) {
|
7
5
|
this.source = source;
|
8
6
|
}
|
@@ -14,9 +12,7 @@ class CancellationToken {
|
|
14
12
|
}
|
15
13
|
throwIfCancellationRequested() {
|
16
14
|
if (this.isCancellationRequested()) {
|
17
|
-
throw new
|
15
|
+
throw new ErrorCancelled();
|
18
16
|
}
|
19
17
|
}
|
20
18
|
}
|
21
|
-
exports.CancellationToken = CancellationToken;
|
22
|
-
//# sourceMappingURL=cancellation-token.js.map
|
@@ -1,4 +1,4 @@
|
|
1
|
-
export
|
1
|
+
export type OnCancelCallback = (arg?: {
|
2
2
|
reason?: unknown;
|
3
3
|
}) => void;
|
4
4
|
export interface ICancellationTokenSource {
|
@@ -16,4 +16,3 @@ export interface ICancellationToken {
|
|
16
16
|
export declare class ErrorCancelled extends Error {
|
17
17
|
constructor();
|
18
18
|
}
|
19
|
-
//# sourceMappingURL=interfaces.d.ts.map
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { HeartBeat } from "./heart-beat.types";
|
1
|
+
import type { HeartBeat } from "./heart-beat.types";
|
2
2
|
export declare class HeartBeatBase implements HeartBeat.IHeartBeat {
|
3
3
|
logger: import("@art-ws/slf").Logger;
|
4
4
|
private id;
|
@@ -15,4 +15,3 @@ export declare class HeartBeatBase implements HeartBeat.IHeartBeat {
|
|
15
15
|
isActive(): boolean;
|
16
16
|
onTick(handler: HeartBeat.Handler, options?: Partial<HeartBeat.SubscribeOptions>): HeartBeat.Subscription;
|
17
17
|
}
|
18
|
-
//# sourceMappingURL=heart-beat.class.d.ts.map
|
@@ -1,26 +1,23 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
exports.HeartBeatBase = void 0;
|
4
|
-
const slf_1 = require("@art-ws/slf");
|
5
|
-
const events_1 = require("events");
|
1
|
+
import { getLogger } from "@art-ws/slf";
|
2
|
+
import { EventEmitter } from "events";
|
6
3
|
const TICK_EVENT = "TICK_EVENT";
|
7
4
|
const DEFAULT_INTERVAL = 5 * 1000;
|
8
|
-
class HeartBeatBase {
|
5
|
+
export class HeartBeatBase {
|
6
|
+
logger = getLogger("HeartBeatBase");
|
7
|
+
id = 0;
|
8
|
+
interval = DEFAULT_INTERVAL;
|
9
|
+
active = false;
|
10
|
+
emitter = new EventEmitter();
|
9
11
|
constructor(options) {
|
10
|
-
this.logger = slf_1.getLogger(HeartBeatBase);
|
11
|
-
this.id = 0;
|
12
|
-
this.interval = DEFAULT_INTERVAL;
|
13
|
-
this.active = false;
|
14
|
-
this.emitter = new events_1.EventEmitter();
|
15
|
-
this.timeout = null;
|
16
12
|
const o = options ?? {};
|
17
13
|
if (o.interval) {
|
18
|
-
this.interval = options
|
14
|
+
this.interval = options?.interval ?? DEFAULT_INTERVAL;
|
19
15
|
}
|
20
16
|
if (o.autostart) {
|
21
17
|
this.up();
|
22
18
|
}
|
23
19
|
}
|
20
|
+
timeout = null;
|
24
21
|
stop() {
|
25
22
|
if (this.timeout) {
|
26
23
|
clearTimeout(this.timeout);
|
@@ -75,5 +72,3 @@ class HeartBeatBase {
|
|
75
72
|
};
|
76
73
|
}
|
77
74
|
}
|
78
|
-
exports.HeartBeatBase = HeartBeatBase;
|
79
|
-
//# sourceMappingURL=heart-beat.class.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -2,8 +2,7 @@ interface HighResolutionTimeStruct {
|
|
2
2
|
sec: number;
|
3
3
|
ms: number;
|
4
4
|
}
|
5
|
-
export
|
5
|
+
export type HRTime = number[];
|
6
6
|
export declare function hrTimeToToHighResolutionTime(hrTime: HRTime): HighResolutionTimeStruct;
|
7
7
|
export declare function hrTimeToMilli(hrTime: HRTime): number;
|
8
8
|
export {};
|
9
|
-
//# sourceMappingURL=hr-time.d.ts.map
|
@@ -0,0 +1,9 @@
|
|
1
|
+
export function hrTimeToToHighResolutionTime(hrTime) {
|
2
|
+
const sec = Number(hrTime[0]);
|
3
|
+
const ms = Math.floor(hrTime[1] / 1000000);
|
4
|
+
return { sec, ms };
|
5
|
+
}
|
6
|
+
export function hrTimeToMilli(hrTime) {
|
7
|
+
const t = hrTimeToToHighResolutionTime(hrTime);
|
8
|
+
return t.sec * 1000 + t.ms;
|
9
|
+
}
|
@@ -2,6 +2,7 @@ export * from "./cancellation";
|
|
2
2
|
export * from "./heart-beat";
|
3
3
|
export * from "./managed-pool";
|
4
4
|
export * from "./mem-cache";
|
5
|
+
export * from "./object-hash";
|
5
6
|
export * from "./shared";
|
6
7
|
export * from "./hr-time";
|
7
8
|
export * from "./interpolate";
|
@@ -9,4 +10,3 @@ export * from "./json-file-loader";
|
|
9
10
|
export * from "./json-refs-resolver";
|
10
11
|
export * from "./managed-promise";
|
11
12
|
export * from "./node-zone";
|
12
|
-
//# sourceMappingURL=index.d.ts.map
|
package/dist/es/index.js
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
// created from 'create-ts-index'
|
2
|
+
export * from "./cancellation";
|
3
|
+
export * from "./heart-beat";
|
4
|
+
export * from "./managed-pool";
|
5
|
+
export * from "./mem-cache";
|
6
|
+
export * from "./object-hash";
|
7
|
+
export * from "./shared";
|
8
|
+
export * from "./hr-time";
|
9
|
+
export * from "./interpolate";
|
10
|
+
export * from "./json-file-loader";
|
11
|
+
export * from "./json-refs-resolver";
|
12
|
+
export * from "./managed-promise";
|
13
|
+
export * from "./node-zone";
|
@@ -0,0 +1,5 @@
|
|
1
|
+
export declare function iterpolate(vars: Record<string, any>, template: string): string;
|
2
|
+
export type ResolveVarFunction<T = any> = (key: string) => Promise<T>;
|
3
|
+
export declare function iterpolateObject(data: Record<string, any>, vars?: Record<string, any>): Record<string, any>;
|
4
|
+
export declare function iterpolateAsync(template: string, varsSource: ResolveVarFunction): Promise<string>;
|
5
|
+
export declare function iterpolateObjectAsync(data: Record<string, any>, varsSource?: ResolveVarFunction): Promise<Record<string, any>>;
|
@@ -0,0 +1,150 @@
|
|
1
|
+
import _ from "lodash";
|
2
|
+
const e = {
|
3
|
+
RE: /{{(\w|\.)+}}/gi,
|
4
|
+
RE_ASYNC: /{{([^\{\}])+}}/gi,
|
5
|
+
B: "{{",
|
6
|
+
E: "}}",
|
7
|
+
DEF_DELIMITER: "||",
|
8
|
+
};
|
9
|
+
const isEmpty = (v) => v === undefined || v === null || Number.isNaN(v);
|
10
|
+
export function iterpolate(vars, template) {
|
11
|
+
let tpl = template;
|
12
|
+
let n = 0;
|
13
|
+
const MAX_CYCLES = 100;
|
14
|
+
while (true) {
|
15
|
+
n++;
|
16
|
+
const s = tpl;
|
17
|
+
const keys = {};
|
18
|
+
let match;
|
19
|
+
while ((match = e.RE.exec(tpl))) {
|
20
|
+
let s = match[0];
|
21
|
+
s = s.substring(e.B.length, s.length - e.E.length);
|
22
|
+
keys[s] = true;
|
23
|
+
}
|
24
|
+
Object.keys(keys).forEach((key) => {
|
25
|
+
const expr = e.B + key + e.E;
|
26
|
+
const val = _.get(vars, key);
|
27
|
+
const value = isEmpty(val) ? expr : `${val}`;
|
28
|
+
tpl = tpl.split(expr).join(value);
|
29
|
+
});
|
30
|
+
if (s === tpl)
|
31
|
+
break;
|
32
|
+
if (!isInterpolationRequired(tpl))
|
33
|
+
break;
|
34
|
+
if (n > MAX_CYCLES)
|
35
|
+
break;
|
36
|
+
}
|
37
|
+
return tpl;
|
38
|
+
}
|
39
|
+
function isInterpolationRequired(s) {
|
40
|
+
return s && typeof s === "string" && s.includes(e.B) && s.includes(e.E);
|
41
|
+
}
|
42
|
+
const walk = (o, fn, depth, parentPath) => {
|
43
|
+
if (!o)
|
44
|
+
return;
|
45
|
+
Object.keys(o).forEach((key) => {
|
46
|
+
if (typeof o[key] === "object") {
|
47
|
+
walk(o[key], fn, depth + 1, [...parentPath, key]);
|
48
|
+
}
|
49
|
+
else {
|
50
|
+
fn(o, key, parentPath);
|
51
|
+
}
|
52
|
+
});
|
53
|
+
};
|
54
|
+
export function iterpolateObject(data, vars) {
|
55
|
+
if (vars) {
|
56
|
+
vars = iterpolateObject(vars);
|
57
|
+
}
|
58
|
+
else {
|
59
|
+
vars = data;
|
60
|
+
}
|
61
|
+
const interpolateKeys = () => {
|
62
|
+
walk(data, (o, key) => {
|
63
|
+
if (isInterpolationRequired(key)) {
|
64
|
+
const newKey = iterpolate(vars, key);
|
65
|
+
const val = o[key];
|
66
|
+
delete o[key];
|
67
|
+
o[newKey] = val;
|
68
|
+
}
|
69
|
+
}, 0, []);
|
70
|
+
};
|
71
|
+
const interpolateValues = () => {
|
72
|
+
walk(data, (o, key) => {
|
73
|
+
const val = o[key];
|
74
|
+
if (isInterpolationRequired(val)) {
|
75
|
+
const newVal = iterpolate(vars, val);
|
76
|
+
o[key] = newVal;
|
77
|
+
}
|
78
|
+
}, 0, []);
|
79
|
+
};
|
80
|
+
interpolateKeys();
|
81
|
+
interpolateValues();
|
82
|
+
return data;
|
83
|
+
}
|
84
|
+
const getDefaults = (s) => {
|
85
|
+
let expr = s;
|
86
|
+
let defaults = null;
|
87
|
+
const D = e.DEF_DELIMITER;
|
88
|
+
if (s && typeof s === "string" && s.includes(D)) {
|
89
|
+
const [e1, e2] = s.split(D);
|
90
|
+
expr = (e1 || "").trim();
|
91
|
+
defaults = (e2 || "").trim() || null;
|
92
|
+
}
|
93
|
+
return [expr, defaults];
|
94
|
+
};
|
95
|
+
export async function iterpolateAsync(template, varsSource) {
|
96
|
+
let tpl = template;
|
97
|
+
let n = 0;
|
98
|
+
const MAX_CYCLES = 10;
|
99
|
+
while (true) {
|
100
|
+
n++;
|
101
|
+
const s = tpl;
|
102
|
+
const keys = {};
|
103
|
+
let match;
|
104
|
+
while ((match = e.RE_ASYNC.exec(tpl))) {
|
105
|
+
let s = match[0];
|
106
|
+
s = s.substring(e.B.length, s.length - e.E.length);
|
107
|
+
keys[s] = true;
|
108
|
+
}
|
109
|
+
for (const keyExpr of Object.keys(keys)) {
|
110
|
+
const fullExpr = e.B + keyExpr + e.E;
|
111
|
+
const [e1, e2] = getDefaults(keyExpr);
|
112
|
+
const key = e1;
|
113
|
+
const val = (varsSource ? await varsSource(key) : undefined) || e2;
|
114
|
+
const value = isEmpty(val) ? fullExpr : `${val}`;
|
115
|
+
tpl = tpl.split(fullExpr).join(value);
|
116
|
+
}
|
117
|
+
if (s === tpl)
|
118
|
+
break;
|
119
|
+
if (!isInterpolationRequired(tpl))
|
120
|
+
break;
|
121
|
+
if (n > MAX_CYCLES)
|
122
|
+
break;
|
123
|
+
}
|
124
|
+
return tpl;
|
125
|
+
}
|
126
|
+
const walkAsync = async (o, fn, depth, parentPath) => {
|
127
|
+
if (!o)
|
128
|
+
return;
|
129
|
+
for (const key of Object.keys(o)) {
|
130
|
+
if (typeof o[key] === "object") {
|
131
|
+
await walkAsync(o[key], fn, depth + 1, [...parentPath, key]);
|
132
|
+
}
|
133
|
+
else {
|
134
|
+
await fn(o, key, parentPath);
|
135
|
+
}
|
136
|
+
}
|
137
|
+
};
|
138
|
+
export async function iterpolateObjectAsync(data, varsSource) {
|
139
|
+
const interpolateValues = async () => {
|
140
|
+
await walkAsync(data, async (o, key) => {
|
141
|
+
const val = o[key];
|
142
|
+
if (isInterpolationRequired(val)) {
|
143
|
+
const newVal = await iterpolateAsync(val, varsSource);
|
144
|
+
o[key] = newVal;
|
145
|
+
}
|
146
|
+
}, 0, []);
|
147
|
+
};
|
148
|
+
await interpolateValues();
|
149
|
+
return data;
|
150
|
+
}
|
@@ -0,0 +1,25 @@
|
|
1
|
+
import type { ResolveVarFunction } from "./interpolate";
|
2
|
+
import type { DeReferenceOptions } from "./json-refs-resolver";
|
3
|
+
export declare const YAML_EXT: string[];
|
4
|
+
export declare const JSON_EXT: string[];
|
5
|
+
export declare const JS_EXT: string[];
|
6
|
+
export declare function saveJsonableFile(obj: any, filename: string): void;
|
7
|
+
export declare function loadJsonableFile<T>(filename: string): Promise<T>;
|
8
|
+
export declare function parseJsonableData<T>(args: {
|
9
|
+
source: string;
|
10
|
+
nameWithExt: string;
|
11
|
+
}): Promise<T>;
|
12
|
+
export declare function lookupJsonableFile(cwd: string, candidates: string[]): string | undefined;
|
13
|
+
export declare function lookupAndLoadJsonableFile<T>(cwd: string, candidates: string[]): Promise<{
|
14
|
+
filename: string;
|
15
|
+
data: T;
|
16
|
+
}>;
|
17
|
+
export interface JsonableDataDeReferenceOptions extends DeReferenceOptions {
|
18
|
+
resolveVar?: ResolveVarFunction;
|
19
|
+
}
|
20
|
+
export declare function resolveJsonableData<T>(args: JsonableDataDeReferenceOptions): Promise<T>;
|
21
|
+
export declare function resolveJsonableFile<T>(filename: string): Promise<T>;
|
22
|
+
export declare function lookupAndResolveJsonableFile<T>(cwd: string, candidates: string[]): Promise<{
|
23
|
+
filename: string;
|
24
|
+
data: T;
|
25
|
+
}>;
|
@@ -0,0 +1,108 @@
|
|
1
|
+
import fs from "fs-extra";
|
2
|
+
import yaml from "js-yaml";
|
3
|
+
import JSON5 from "json5";
|
4
|
+
import _ from "lodash";
|
5
|
+
import path from "path";
|
6
|
+
import { iterpolateObjectAsync } from "./interpolate";
|
7
|
+
import { resolveJsonRefs } from "./json-refs-resolver";
|
8
|
+
export const YAML_EXT = ["yaml", "yml"];
|
9
|
+
export const JSON_EXT = ["json", "json5"];
|
10
|
+
export const JS_EXT = ["js"];
|
11
|
+
const isRemoteWithExt = (uri, ext) => uri.startsWith("http") && uri.includes(`.${ext}`);
|
12
|
+
const isAnyExt = (s, extensions) => Boolean(extensions.find((ext) => (typeof s === "string" && s && s.endsWith(`.${ext}`)) ||
|
13
|
+
isRemoteWithExt(s, ext)));
|
14
|
+
const isYamlFile = (s) => isAnyExt(s, YAML_EXT);
|
15
|
+
const isJsonFile = (s) => isAnyExt(s, JSON_EXT);
|
16
|
+
const isJsFile = (s) => isAnyExt(s, JS_EXT);
|
17
|
+
const parseJSON = (data) => (data ? JSON5.parse(data) : null);
|
18
|
+
const parseYaml = (data) => (data ? yaml.load(data) : null);
|
19
|
+
const loadAndParseFile = (filename, parser) => parser(fs.readFileSync(filename, { encoding: "utf-8" }));
|
20
|
+
export function saveJsonableFile(obj, filename) {
|
21
|
+
let content;
|
22
|
+
const indent = 2;
|
23
|
+
if (isYamlFile(filename)) {
|
24
|
+
content = yaml.dump(obj, { indent });
|
25
|
+
}
|
26
|
+
else {
|
27
|
+
content = JSON5.stringify(obj, null, indent);
|
28
|
+
}
|
29
|
+
fs.writeFileSync(filename, content, { encoding: "utf-8" });
|
30
|
+
}
|
31
|
+
export async function loadJsonableFile(filename) {
|
32
|
+
if (isJsFile(filename)) {
|
33
|
+
const module = await import(filename);
|
34
|
+
return module.default || module;
|
35
|
+
}
|
36
|
+
else if (isYamlFile(filename)) {
|
37
|
+
return loadAndParseFile(filename, parseYaml);
|
38
|
+
}
|
39
|
+
else if (isJsonFile(filename)) {
|
40
|
+
return loadAndParseFile(filename, parseJSON);
|
41
|
+
}
|
42
|
+
throw new Error(`File '${filename}' format not supported`);
|
43
|
+
}
|
44
|
+
export async function parseJsonableData(args) {
|
45
|
+
const { source, nameWithExt: ext } = args;
|
46
|
+
if (isYamlFile(ext)) {
|
47
|
+
return parseYaml(source);
|
48
|
+
}
|
49
|
+
else if (isJsonFile(ext)) {
|
50
|
+
return parseJSON(source);
|
51
|
+
}
|
52
|
+
throw new Error(`Extension '${ext}' not supported`);
|
53
|
+
}
|
54
|
+
const EXTENSIONS = [...YAML_EXT, ...JSON_EXT, ...JS_EXT];
|
55
|
+
export function lookupJsonableFile(cwd, candidates) {
|
56
|
+
const filenames = candidates
|
57
|
+
.map((name) => path.extname(name) ? [name] : EXTENSIONS.map((ext) => `${name}.${ext}`))
|
58
|
+
.flat()
|
59
|
+
.map((filename) => path.join(cwd, filename));
|
60
|
+
const filename = filenames.find((filename) => fs.existsSync(filename));
|
61
|
+
return filename;
|
62
|
+
}
|
63
|
+
export async function lookupAndLoadJsonableFile(cwd, candidates) {
|
64
|
+
const filename = lookupJsonableFile(cwd, candidates);
|
65
|
+
if (!filename) {
|
66
|
+
return { filename: null, data: null };
|
67
|
+
}
|
68
|
+
const data = await loadJsonableFile(filename);
|
69
|
+
return { filename, data };
|
70
|
+
}
|
71
|
+
export async function resolveJsonableData(args) {
|
72
|
+
const { data, baseUrl, resolveVar } = args;
|
73
|
+
const varResolver = (context) => {
|
74
|
+
return async (key) => {
|
75
|
+
let val = undefined;
|
76
|
+
if (context)
|
77
|
+
val = _.get(context, key);
|
78
|
+
if (!val && resolveVar) {
|
79
|
+
val = await resolveVar(key);
|
80
|
+
}
|
81
|
+
return val;
|
82
|
+
};
|
83
|
+
};
|
84
|
+
const data1 = await iterpolateObjectAsync(data, varResolver(data));
|
85
|
+
const data2 = await resolveJsonRefs({ baseUrl, data: data1 });
|
86
|
+
const data3 = await iterpolateObjectAsync(data2, varResolver(data2));
|
87
|
+
return data3;
|
88
|
+
}
|
89
|
+
export async function resolveJsonableFile(filename) {
|
90
|
+
const rawData = await loadJsonableFile(filename);
|
91
|
+
const data = await resolveJsonableData({
|
92
|
+
data: rawData,
|
93
|
+
baseUrl: filename,
|
94
|
+
});
|
95
|
+
return data;
|
96
|
+
}
|
97
|
+
export async function lookupAndResolveJsonableFile(cwd, candidates) {
|
98
|
+
const result = await lookupAndLoadJsonableFile(cwd, candidates);
|
99
|
+
if (result.filename) {
|
100
|
+
let { data } = result;
|
101
|
+
data = await resolveJsonableData({
|
102
|
+
data: data,
|
103
|
+
baseUrl: result.filename,
|
104
|
+
});
|
105
|
+
return { filename: result.filename, data: data };
|
106
|
+
}
|
107
|
+
return { filename: null, data: null };
|
108
|
+
}
|
@@ -0,0 +1,6 @@
|
|
1
|
+
export interface DeReferenceOptions {
|
2
|
+
data?: Record<string, any>;
|
3
|
+
baseUrl?: string;
|
4
|
+
}
|
5
|
+
export declare function doDereference(args: DeReferenceOptions): Promise<Record<string, any>>;
|
6
|
+
export declare function resolveJsonRefs(args: DeReferenceOptions): Promise<Record<string, any>>;
|
@@ -1,12 +1,9 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
exports.resolveJsonRefs = exports.doDereference = void 0;
|
4
|
-
const tslib_1 = require("tslib");
|
5
|
-
const json_schema_ref_parser_1 = tslib_1.__importDefault(require("@apidevtools/json-schema-ref-parser"));
|
6
|
-
const lodash_1 = tslib_1.__importDefault(require("lodash"));
|
1
|
+
import $RefParser from "@apidevtools/json-schema-ref-parser";
|
2
|
+
import _ from "lodash";
|
7
3
|
const walk = async (o, fn, depth) => {
|
8
4
|
for (const key in o) {
|
9
|
-
if (typeof o[key] === "object" &&
|
5
|
+
if (typeof o[key] === "object" &&
|
6
|
+
!Array.isArray(o[key])) {
|
10
7
|
await walk(o[key], fn, depth + 1);
|
11
8
|
}
|
12
9
|
else {
|
@@ -33,7 +30,7 @@ async function deRefs(data, baseUrl) {
|
|
33
30
|
});
|
34
31
|
const deRefs = await Promise.all(refsPromises);
|
35
32
|
deRefs.forEach((deRef) => {
|
36
|
-
o =
|
33
|
+
o = _.merge(o, deRef);
|
37
34
|
});
|
38
35
|
}
|
39
36
|
}, 0);
|
@@ -42,27 +39,25 @@ async function deRefs(data, baseUrl) {
|
|
42
39
|
const SUPPORTED_EXTENSIONS = [".json", ".yaml", ".yml"];
|
43
40
|
// https://apitools.dev/json-schema-ref-parser/
|
44
41
|
// https://apitools.dev/json-schema-ref-parser/docs/ref-parser.html#dereferenceschema-options-callback
|
45
|
-
async function doDereference(args) {
|
42
|
+
export async function doDereference(args) {
|
46
43
|
const { data, baseUrl = "" } = args;
|
47
|
-
if (baseUrl && SUPPORTED_EXTENSIONS.find((x) => baseUrl.includes(x)))
|
48
|
-
|
44
|
+
if ((baseUrl && SUPPORTED_EXTENSIONS.find((x) => baseUrl.includes(x))) ||
|
45
|
+
baseUrl === "") {
|
46
|
+
const rp = new $RefParser();
|
49
47
|
if (data) {
|
50
48
|
return await rp.dereference(baseUrl, data, {});
|
51
49
|
}
|
52
50
|
else if (baseUrl) {
|
53
51
|
return await rp.dereference(baseUrl);
|
54
52
|
}
|
55
|
-
return data;
|
53
|
+
return data ?? {};
|
56
54
|
}
|
57
|
-
return data;
|
55
|
+
return data ?? {};
|
58
56
|
}
|
59
|
-
|
60
|
-
async function resolveJsonRefs(args) {
|
57
|
+
export async function resolveJsonRefs(args) {
|
61
58
|
const { data, baseUrl = "" } = args;
|
62
|
-
const json1 = data ? await deRefs(data, baseUrl) :
|
59
|
+
const json1 = data ? await deRefs(data, baseUrl) : {};
|
63
60
|
const json2 = await doDereference({ data: json1, baseUrl });
|
64
61
|
const json3 = await deRefs(json2, baseUrl);
|
65
62
|
return json3;
|
66
63
|
}
|
67
|
-
exports.resolveJsonRefs = resolveJsonRefs;
|
68
|
-
//# sourceMappingURL=json-refs-resolver.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from "./managed-pool";
|
@@ -1,19 +1,20 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
import { MemoryCacheBase } from "../mem-cache";
|
2
|
+
import { getLogger } from "@art-ws/slf";
|
3
|
+
export const DEFAULT_TIME_TO_IDLE = 2 * 60 * 1000;
|
4
|
+
export class ManagedPool {
|
5
|
+
adapter;
|
6
|
+
cache;
|
7
|
+
logger = getLogger("ManagedPool");
|
8
|
+
poolCacheOptions;
|
7
9
|
constructor(adapter, cache) {
|
8
10
|
this.adapter = adapter;
|
9
11
|
this.cache = cache;
|
10
|
-
this.logger = slf_1.getLogger(ManagedPool);
|
11
12
|
const canRemove = (item) => this.adapter.canDestroy(item);
|
12
13
|
const onRemove = (item) => {
|
13
14
|
this.adapter.destroy(item);
|
14
15
|
};
|
15
16
|
const opts = this.adapter.getCacheOptions();
|
16
|
-
opts.timeToIdle = opts.timeToIdle ||
|
17
|
+
opts.timeToIdle = opts.timeToIdle || DEFAULT_TIME_TO_IDLE;
|
17
18
|
this.poolCacheOptions = { ...opts, onRemove, canRemove };
|
18
19
|
}
|
19
20
|
async acquire(args) {
|
@@ -42,5 +43,3 @@ class ManagedPool {
|
|
42
43
|
this.cache.clear();
|
43
44
|
}
|
44
45
|
}
|
45
|
-
exports.ManagedPool = ManagedPool;
|
46
|
-
//# sourceMappingURL=managed-pool.js.map
|
@@ -1,4 +1,5 @@
|
|
1
1
|
export interface ManagedPromise<T> extends Promise<T> {
|
2
|
+
resolvedValue: unknown;
|
2
3
|
resolve: (result?: unknown) => void;
|
3
4
|
reject: (reason?: unknown) => void;
|
4
5
|
cancel: () => void;
|
@@ -6,4 +7,3 @@ export interface ManagedPromise<T> extends Promise<T> {
|
|
6
7
|
export declare function ManagedPromise<T>(args?: {
|
7
8
|
onCancel?: () => void;
|
8
9
|
}): ManagedPromise<T>;
|
9
|
-
//# sourceMappingURL=managed-promise.d.ts.map
|