@adviser/cement 0.0.0-jsr-t1
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/LICENSE +201 -0
- package/README.md +39 -0
- package/base-sys-abstraction-BkEiLHl0.d.ts +193 -0
- package/base-sys-abstraction-Qj7pkY1N.d.cts +193 -0
- package/chunk-7KFVMTOS.js +311 -0
- package/chunk-7KFVMTOS.js.map +1 -0
- package/chunk-GES3MUGV.js +92 -0
- package/chunk-GES3MUGV.js.map +1 -0
- package/chunk-Q65HLCNL.js +601 -0
- package/chunk-Q65HLCNL.js.map +1 -0
- package/chunk-WMMUXBDX.js +87 -0
- package/chunk-WMMUXBDX.js.map +1 -0
- package/index-Q3phXzYr.d.cts +75 -0
- package/index-tIGZMHTc.d.ts +75 -0
- package/index.cjs +2593 -0
- package/index.cjs.map +1 -0
- package/index.d.cts +532 -0
- package/index.d.ts +532 -0
- package/index.js +1533 -0
- package/index.js.map +1 -0
- package/node/index.cjs +924 -0
- package/node/index.cjs.map +1 -0
- package/node/index.d.cts +65 -0
- package/node/index.d.ts +65 -0
- package/node/index.js +398 -0
- package/node/index.js.map +1 -0
- package/package.json +81 -0
- package/src/base-sys-abstraction.test.ts +95 -0
- package/src/base-sys-abstraction.ts +242 -0
- package/src/bin2text.test.ts +59 -0
- package/src/bin2text.ts +47 -0
- package/src/crypto.test.ts +15 -0
- package/src/crypto.ts +125 -0
- package/src/file-service.ts +24 -0
- package/src/future.test.ts +32 -0
- package/src/future.ts +27 -0
- package/src/index.ts +22 -0
- package/src/jsr.json +20 -0
- package/src/log-level-impl.ts +87 -0
- package/src/log-writer-impl.ts +58 -0
- package/src/logger-impl.ts +498 -0
- package/src/logger.test.ts +1132 -0
- package/src/logger.ts +208 -0
- package/src/node/deno-file-service.ts +92 -0
- package/src/node/deno-sys-abstraction.ts +133 -0
- package/src/node/index.ts +4 -0
- package/src/node/mock-file-service.ts +45 -0
- package/src/node/node-file-service.ts +91 -0
- package/src/node/node-sys-abstraction.ts +121 -0
- package/src/option.ts +60 -0
- package/src/resolve-once.test.ts +321 -0
- package/src/resolve-once.ts +179 -0
- package/src/result.test.ts +102 -0
- package/src/result.ts +165 -0
- package/src/runtime.ts +36 -0
- package/src/sys-abstraction.ts +53 -0
- package/src/sys-env.test.ts +53 -0
- package/src/sys-env.ts +216 -0
- package/src/test/log-write-stream.ts +95 -0
- package/src/test/mock-logger.ts +40 -0
- package/src/time.ts +20 -0
- package/src/tracer.test.ts +314 -0
- package/src/tracer.ts +222 -0
- package/src/txt-en-decoder.ts +21 -0
- package/src/uri.test.ts +155 -0
- package/src/uri.ts +421 -0
- package/src/utils/console-write-stream.ts +72 -0
- package/src/utils/fanout-write-stream.ts +32 -0
- package/src/utils/index.ts +6 -0
- package/src/utils/rebuffer.ts +75 -0
- package/src/utils/stream-map.ts +67 -0
- package/src/utils/stream2string.ts +47 -0
- package/src/utils/string2stream.ts +14 -0
- package/src/version.ts +3 -0
- package/src/web/index.ts +1 -0
- package/src/web/web-sys-abstraction.ts +80 -0
- package/ts/LICENSE +201 -0
- package/ts/README.md +39 -0
- package/ts/base-sys-abstraction.d.ts +84 -0
- package/ts/base-sys-abstraction.d.ts.map +1 -0
- package/ts/base-sys-abstraction.js +178 -0
- package/ts/base-sys-abstraction.js.map +1 -0
- package/ts/base-sys-abstraction.test.d.ts +2 -0
- package/ts/base-sys-abstraction.test.d.ts.map +1 -0
- package/ts/base-sys-abstraction.test.js +82 -0
- package/ts/base-sys-abstraction.test.js.map +1 -0
- package/ts/bin2text.d.ts +3 -0
- package/ts/bin2text.d.ts.map +1 -0
- package/ts/bin2text.js +43 -0
- package/ts/bin2text.js.map +1 -0
- package/ts/bin2text.test.d.ts +2 -0
- package/ts/bin2text.test.d.ts.map +1 -0
- package/ts/bin2text.test.js +51 -0
- package/ts/bin2text.test.js.map +1 -0
- package/ts/crypto.d.ts +76 -0
- package/ts/crypto.d.ts.map +1 -0
- package/ts/crypto.js +22 -0
- package/ts/crypto.js.map +1 -0
- package/ts/crypto.test.d.ts +2 -0
- package/ts/crypto.test.d.ts.map +1 -0
- package/ts/crypto.test.js +14 -0
- package/ts/crypto.test.js.map +1 -0
- package/ts/file-service.d.ts +17 -0
- package/ts/file-service.d.ts.map +1 -0
- package/ts/file-service.js +2 -0
- package/ts/file-service.js.map +1 -0
- package/ts/future.d.ts +8 -0
- package/ts/future.d.ts.map +1 -0
- package/ts/future.js +38 -0
- package/ts/future.js.map +1 -0
- package/ts/future.test.d.ts +2 -0
- package/ts/future.test.d.ts.map +1 -0
- package/ts/future.test.js +28 -0
- package/ts/future.test.js.map +1 -0
- package/ts/index.d.ts +23 -0
- package/ts/index.d.ts.map +1 -0
- package/ts/index.js +23 -0
- package/ts/index.js.map +1 -0
- package/ts/log-level-impl.d.ts +14 -0
- package/ts/log-level-impl.d.ts.map +1 -0
- package/ts/log-level-impl.js +72 -0
- package/ts/log-level-impl.js.map +1 -0
- package/ts/log-writer-impl.d.ts +10 -0
- package/ts/log-writer-impl.d.ts.map +1 -0
- package/ts/log-writer-impl.js +45 -0
- package/ts/log-writer-impl.js.map +1 -0
- package/ts/logger-impl.d.ts +71 -0
- package/ts/logger-impl.d.ts.map +1 -0
- package/ts/logger-impl.js +412 -0
- package/ts/logger-impl.js.map +1 -0
- package/ts/logger.d.ts +84 -0
- package/ts/logger.d.ts.map +1 -0
- package/ts/logger.js +114 -0
- package/ts/logger.js.map +1 -0
- package/ts/logger.test.d.ts +2 -0
- package/ts/logger.test.d.ts.map +1 -0
- package/ts/logger.test.js +1023 -0
- package/ts/logger.test.js.map +1 -0
- package/ts/node/deno-file-service.d.ts +17 -0
- package/ts/node/deno-file-service.d.ts.map +1 -0
- package/ts/node/deno-file-service.js +65 -0
- package/ts/node/deno-file-service.js.map +1 -0
- package/ts/node/deno-sys-abstraction.d.ts +22 -0
- package/ts/node/deno-sys-abstraction.d.ts.map +1 -0
- package/ts/node/deno-sys-abstraction.js +101 -0
- package/ts/node/deno-sys-abstraction.js.map +1 -0
- package/ts/node/index.d.ts +5 -0
- package/ts/node/index.d.ts.map +1 -0
- package/ts/node/index.js +5 -0
- package/ts/node/index.js.map +1 -0
- package/ts/node/mock-file-service.d.ts +11 -0
- package/ts/node/mock-file-service.d.ts.map +1 -0
- package/ts/node/mock-file-service.js +34 -0
- package/ts/node/mock-file-service.js.map +1 -0
- package/ts/node/mock-file-service.test.d.ts +2 -0
- package/ts/node/mock-file-service.test.d.ts.map +1 -0
- package/ts/node/mock-file-service.test.js +31 -0
- package/ts/node/mock-file-service.test.js.map +1 -0
- package/ts/node/node-file-service.d.ts +16 -0
- package/ts/node/node-file-service.d.ts.map +1 -0
- package/ts/node/node-file-service.js +71 -0
- package/ts/node/node-file-service.js.map +1 -0
- package/ts/node/node-sys-abstraction.d.ts +22 -0
- package/ts/node/node-sys-abstraction.d.ts.map +1 -0
- package/ts/node/node-sys-abstraction.js +99 -0
- package/ts/node/node-sys-abstraction.js.map +1 -0
- package/ts/node/node-sys-abstraction.test.d.ts +2 -0
- package/ts/node/node-sys-abstraction.test.d.ts.map +1 -0
- package/ts/node/node-sys-abstraction.test.js +87 -0
- package/ts/node/node-sys-abstraction.test.js.map +1 -0
- package/ts/option.d.ts +25 -0
- package/ts/option.d.ts.map +1 -0
- package/ts/option.js +47 -0
- package/ts/option.js.map +1 -0
- package/ts/resolve-once.d.ts +46 -0
- package/ts/resolve-once.d.ts.map +1 -0
- package/ts/resolve-once.js +152 -0
- package/ts/resolve-once.js.map +1 -0
- package/ts/resolve-once.test.d.ts +2 -0
- package/ts/resolve-once.test.d.ts.map +1 -0
- package/ts/resolve-once.test.js +283 -0
- package/ts/resolve-once.test.js.map +1 -0
- package/ts/result.d.ts +34 -0
- package/ts/result.d.ts.map +1 -0
- package/ts/result.js +85 -0
- package/ts/result.js.map +1 -0
- package/ts/result.test.d.ts +2 -0
- package/ts/result.test.d.ts.map +1 -0
- package/ts/result.test.js +79 -0
- package/ts/result.test.js.map +1 -0
- package/ts/runtime.d.ts +8 -0
- package/ts/runtime.d.ts.map +1 -0
- package/ts/runtime.js +26 -0
- package/ts/runtime.js.map +1 -0
- package/ts/sys-abstraction.d.ts +36 -0
- package/ts/sys-abstraction.d.ts.map +1 -0
- package/ts/sys-abstraction.js +31 -0
- package/ts/sys-abstraction.js.map +1 -0
- package/ts/sys-env.d.ts +48 -0
- package/ts/sys-env.d.ts.map +1 -0
- package/ts/sys-env.js +176 -0
- package/ts/sys-env.js.map +1 -0
- package/ts/sys-env.test.d.ts +2 -0
- package/ts/sys-env.test.d.ts.map +1 -0
- package/ts/sys-env.test.js +51 -0
- package/ts/sys-env.test.js.map +1 -0
- package/ts/test/log-write-stream.d.ts +27 -0
- package/ts/test/log-write-stream.d.ts.map +1 -0
- package/ts/test/log-write-stream.js +74 -0
- package/ts/test/log-write-stream.js.map +1 -0
- package/ts/test/mock-logger.d.ts +14 -0
- package/ts/test/mock-logger.d.ts.map +1 -0
- package/ts/test/mock-logger.js +29 -0
- package/ts/test/mock-logger.js.map +1 -0
- package/ts/test/mock-logger.test.d.ts +2 -0
- package/ts/test/mock-logger.test.d.ts.map +1 -0
- package/ts/test/mock-logger.test.js +63 -0
- package/ts/test/mock-logger.test.js.map +1 -0
- package/ts/test/test-exit-handler.d.ts +2 -0
- package/ts/test/test-exit-handler.d.ts.map +1 -0
- package/ts/test/test-exit-handler.js +57 -0
- package/ts/test/test-exit-handler.js.map +1 -0
- package/ts/time.d.ts +13 -0
- package/ts/time.d.ts.map +1 -0
- package/ts/time.js +14 -0
- package/ts/time.js.map +1 -0
- package/ts/tracer.d.ts +59 -0
- package/ts/tracer.d.ts.map +1 -0
- package/ts/tracer.js +148 -0
- package/ts/tracer.js.map +1 -0
- package/ts/tracer.test.d.ts +2 -0
- package/ts/tracer.test.d.ts.map +1 -0
- package/ts/tracer.test.js +311 -0
- package/ts/tracer.test.js.map +1 -0
- package/ts/txt-en-decoder.d.ts +10 -0
- package/ts/txt-en-decoder.d.ts.map +1 -0
- package/ts/txt-en-decoder.js +15 -0
- package/ts/txt-en-decoder.js.map +1 -0
- package/ts/uri.d.ts +67 -0
- package/ts/uri.d.ts.map +1 -0
- package/ts/uri.js +283 -0
- package/ts/uri.js.map +1 -0
- package/ts/uri.test.d.ts +2 -0
- package/ts/uri.test.d.ts.map +1 -0
- package/ts/uri.test.js +119 -0
- package/ts/uri.test.js.map +1 -0
- package/ts/utils/console-write-stream.d.ts +21 -0
- package/ts/utils/console-write-stream.d.ts.map +1 -0
- package/ts/utils/console-write-stream.js +62 -0
- package/ts/utils/console-write-stream.js.map +1 -0
- package/ts/utils/fanout-write-stream.d.ts +12 -0
- package/ts/utils/fanout-write-stream.d.ts.map +1 -0
- package/ts/utils/fanout-write-stream.js +24 -0
- package/ts/utils/fanout-write-stream.js.map +1 -0
- package/ts/utils/index.d.ts +7 -0
- package/ts/utils/index.d.ts.map +1 -0
- package/ts/utils/index.js +7 -0
- package/ts/utils/index.js.map +1 -0
- package/ts/utils/rebuffer.d.ts +3 -0
- package/ts/utils/rebuffer.d.ts.map +1 -0
- package/ts/utils/rebuffer.js +60 -0
- package/ts/utils/rebuffer.js.map +1 -0
- package/ts/utils/rebuffer.test.d.ts +2 -0
- package/ts/utils/rebuffer.test.d.ts.map +1 -0
- package/ts/utils/rebuffer.test.js +77 -0
- package/ts/utils/rebuffer.test.js.map +1 -0
- package/ts/utils/stream-map.d.ts +9 -0
- package/ts/utils/stream-map.d.ts.map +1 -0
- package/ts/utils/stream-map.js +62 -0
- package/ts/utils/stream-map.js.map +1 -0
- package/ts/utils/stream-map.test.d.ts +2 -0
- package/ts/utils/stream-map.test.d.ts.map +1 -0
- package/ts/utils/stream-map.test.js +87 -0
- package/ts/utils/stream-map.test.js.map +1 -0
- package/ts/utils/stream-test-helper.d.ts +17 -0
- package/ts/utils/stream-test-helper.d.ts.map +1 -0
- package/ts/utils/stream-test-helper.js +37 -0
- package/ts/utils/stream-test-helper.js.map +1 -0
- package/ts/utils/stream2string.d.ts +3 -0
- package/ts/utils/stream2string.d.ts.map +1 -0
- package/ts/utils/stream2string.js +48 -0
- package/ts/utils/stream2string.js.map +1 -0
- package/ts/utils/stream2string.test.d.ts +2 -0
- package/ts/utils/stream2string.test.d.ts.map +1 -0
- package/ts/utils/stream2string.test.js +29 -0
- package/ts/utils/stream2string.test.js.map +1 -0
- package/ts/utils/string2stream.d.ts +4 -0
- package/ts/utils/string2stream.d.ts.map +1 -0
- package/ts/utils/string2stream.js +13 -0
- package/ts/utils/string2stream.js.map +1 -0
- package/ts/utils/string2stream.test.d.ts +2 -0
- package/ts/utils/string2stream.test.d.ts.map +1 -0
- package/ts/utils/string2stream.test.js +6 -0
- package/ts/utils/string2stream.test.js.map +1 -0
- package/ts/version.d.ts +2 -0
- package/ts/version.d.ts.map +1 -0
- package/ts/version.js +4 -0
- package/ts/version.js.map +1 -0
- package/ts/web/index.d.ts +2 -0
- package/ts/web/index.d.ts.map +1 -0
- package/ts/web/index.js +2 -0
- package/ts/web/index.js.map +1 -0
- package/ts/web/web-sys-abstraction.d.ts +4 -0
- package/ts/web/web-sys-abstraction.d.ts.map +1 -0
- package/ts/web/web-sys-abstraction.js +64 -0
- package/ts/web/web-sys-abstraction.js.map +1 -0
- package/txt-en-decoder-CZYJUju2.d.cts +11 -0
- package/txt-en-decoder-CZYJUju2.d.ts +11 -0
- package/utils/index.cjs +341 -0
- package/utils/index.cjs.map +1 -0
- package/utils/index.d.cts +2 -0
- package/utils/index.d.ts +2 -0
- package/utils/index.js +32 -0
- package/utils/index.js.map +1 -0
- package/web/index.cjs +593 -0
- package/web/index.cjs.map +1 -0
- package/web/index.d.cts +6 -0
- package/web/index.d.ts +6 -0
- package/web/index.js +9 -0
- package/web/index.js.map +1 -0
package/src/logger.ts
ADDED
@@ -0,0 +1,208 @@
|
|
1
|
+
import { bin2string } from "./bin2text.js";
|
2
|
+
import { Result } from "./result.js";
|
3
|
+
import { TxtEnDecoder } from "./txt-en-decoder.js";
|
4
|
+
import { CoerceURI } from "./uri.js";
|
5
|
+
|
6
|
+
export enum Level {
|
7
|
+
WARN = "warn",
|
8
|
+
DEBUG = "debug",
|
9
|
+
INFO = "info",
|
10
|
+
ERROR = "error",
|
11
|
+
}
|
12
|
+
|
13
|
+
export type Serialized = string | number | boolean;
|
14
|
+
export type FnSerialized = () => Serialized | Serialized[];
|
15
|
+
|
16
|
+
export class LogValue {
|
17
|
+
constructor(readonly fn: FnSerialized) {}
|
18
|
+
value(): Serialized | Serialized[] {
|
19
|
+
return this.fn();
|
20
|
+
}
|
21
|
+
toJSON(): Serialized | Serialized[] {
|
22
|
+
return this.value();
|
23
|
+
}
|
24
|
+
}
|
25
|
+
|
26
|
+
export type LogSerializable = Record<string, LogValue | Promise<LogValue>>;
|
27
|
+
|
28
|
+
export function removeSelfRef(lineEnd?: string): (key: unknown, val: unknown) => unknown {
|
29
|
+
const cache = new Set();
|
30
|
+
return function (key: unknown, value: unknown) {
|
31
|
+
if (typeof value === "object" && value !== null) {
|
32
|
+
// Duplicate reference found, discard key
|
33
|
+
if (cache.has(value)) return "...";
|
34
|
+
cache.add(value);
|
35
|
+
}
|
36
|
+
return lineEnd ? value + lineEnd : value;
|
37
|
+
};
|
38
|
+
}
|
39
|
+
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
41
|
+
export function asyncLogValue(val: () => Promise<Serialized>): Promise<LogValue> {
|
42
|
+
// return Promise.resolve(logValue(val));
|
43
|
+
throw new Error("Not implemented");
|
44
|
+
}
|
45
|
+
|
46
|
+
export type LogValueArg = LogValue | Serialized | Serialized[] | FnSerialized | undefined | null;
|
47
|
+
|
48
|
+
export function logValue(val: LogValueArg, state: Set<unknown> = new Set<unknown>([Math.random()])): LogValue {
|
49
|
+
switch (typeof val) {
|
50
|
+
case "function":
|
51
|
+
return new LogValue(val);
|
52
|
+
case "string": {
|
53
|
+
try {
|
54
|
+
const ret = JSON.parse(val);
|
55
|
+
if (typeof ret === "object" && ret !== null) {
|
56
|
+
return logValue(ret, state);
|
57
|
+
}
|
58
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
59
|
+
} catch (e) {
|
60
|
+
if (val.match(/[\n\r]/)) {
|
61
|
+
const lines = val.trimEnd().split(/[\n\r]/);
|
62
|
+
return new LogValue(() => lines);
|
63
|
+
}
|
64
|
+
}
|
65
|
+
return new LogValue(() => val.toString());
|
66
|
+
}
|
67
|
+
case "number":
|
68
|
+
return new LogValue(() => val);
|
69
|
+
case "boolean":
|
70
|
+
return new LogValue(() => val);
|
71
|
+
case "object": {
|
72
|
+
if (ArrayBuffer.isView(val)) {
|
73
|
+
return logValue(bin2string(val, 512));
|
74
|
+
}
|
75
|
+
if (Array.isArray(val)) {
|
76
|
+
return new LogValue(() => val.map((v) => logValue(v).value() as Serialized));
|
77
|
+
}
|
78
|
+
if (val === null) {
|
79
|
+
return new LogValue(() => "null");
|
80
|
+
}
|
81
|
+
// Duplicate reference found, discard key
|
82
|
+
if (state.has(val)) {
|
83
|
+
return new LogValue(() => "...");
|
84
|
+
}
|
85
|
+
state.add(val);
|
86
|
+
|
87
|
+
const res: Record<string, LogValue> = {};
|
88
|
+
const typedVal = val as unknown as Record<string, LogValueArg>;
|
89
|
+
for (const key in typedVal) {
|
90
|
+
const element = typedVal[key];
|
91
|
+
if (element instanceof LogValue) {
|
92
|
+
res[key] = element;
|
93
|
+
} else {
|
94
|
+
res[key] = logValue(element, state);
|
95
|
+
}
|
96
|
+
}
|
97
|
+
// ugly as hell cast but how declare a self-referencing type?
|
98
|
+
return new LogValue(() => res as unknown as Serialized);
|
99
|
+
}
|
100
|
+
default:
|
101
|
+
if (!val) {
|
102
|
+
return new LogValue(() => "--Falsy--");
|
103
|
+
}
|
104
|
+
throw new Error(`Invalid type:${typeof val}`);
|
105
|
+
}
|
106
|
+
}
|
107
|
+
|
108
|
+
export interface Sized {
|
109
|
+
size: number;
|
110
|
+
}
|
111
|
+
export interface Lengthed {
|
112
|
+
length: number;
|
113
|
+
}
|
114
|
+
export type SizeOrLength = Sized | Lengthed;
|
115
|
+
|
116
|
+
export interface LogFormatter {
|
117
|
+
format(attr: LogSerializable): Uint8Array;
|
118
|
+
}
|
119
|
+
|
120
|
+
export interface LevelHandler {
|
121
|
+
enableLevel(level: Level, ...modules: string[]): void;
|
122
|
+
disableLevel(level: Level, ...modules: string[]): void;
|
123
|
+
setExposeStack(enable?: boolean): void;
|
124
|
+
isStackExposed: boolean;
|
125
|
+
setDebug(...modules: (string | string[])[]): void;
|
126
|
+
isEnabled(ilevel: unknown, module: unknown): boolean;
|
127
|
+
}
|
128
|
+
|
129
|
+
export interface LoggerInterface<R> {
|
130
|
+
TxtEnDe(): TxtEnDecoder;
|
131
|
+
Module(key: string): R;
|
132
|
+
// if modules is empty, set for all Levels
|
133
|
+
EnableLevel(level: Level, ...modules: string[]): R;
|
134
|
+
DisableLevel(level: Level, ...modules: string[]): R;
|
135
|
+
|
136
|
+
Attributes(): Record<string, unknown>;
|
137
|
+
|
138
|
+
SetDebug(...modules: (string | string[])[]): R;
|
139
|
+
SetExposeStack(enable?: boolean): R;
|
140
|
+
SetFormatter(fmt: LogFormatter): R;
|
141
|
+
|
142
|
+
Ref(key: string, action: { toString: () => string } | FnSerialized): R;
|
143
|
+
Result<T>(key: string, res: Result<T>): R;
|
144
|
+
// default key url
|
145
|
+
Url(url: CoerceURI, key?: string): R;
|
146
|
+
// len
|
147
|
+
Len(value: unknown, key?: string): R;
|
148
|
+
|
149
|
+
Hash(value: unknown, key?: string): R;
|
150
|
+
|
151
|
+
Str(key: string, value?: string): R;
|
152
|
+
Error(): R;
|
153
|
+
Warn(): R;
|
154
|
+
Debug(): R;
|
155
|
+
Log(): R;
|
156
|
+
WithLevel(level: Level): R;
|
157
|
+
|
158
|
+
Err(err: unknown | Result<unknown> | Error): R; // could be Error, or something which coerces to string
|
159
|
+
Info(): R;
|
160
|
+
Timestamp(): R;
|
161
|
+
Any(key: string, value: unknown): R;
|
162
|
+
Dur(key: string, nsec: number): R;
|
163
|
+
Uint64(key: string, value: number): R;
|
164
|
+
Int(key: string, value: number): R;
|
165
|
+
Bool(key: string, value: unknown): R;
|
166
|
+
}
|
167
|
+
|
168
|
+
export function IsLogger(obj: unknown): obj is Logger {
|
169
|
+
return (
|
170
|
+
typeof obj === "object" &&
|
171
|
+
[
|
172
|
+
"Module",
|
173
|
+
"EnableLevel",
|
174
|
+
"DisableLevel",
|
175
|
+
"SetDebug",
|
176
|
+
"Str",
|
177
|
+
"Error",
|
178
|
+
"Warn",
|
179
|
+
"Debug",
|
180
|
+
"Log",
|
181
|
+
"WithLevel",
|
182
|
+
"Err",
|
183
|
+
"Info",
|
184
|
+
"Timestamp",
|
185
|
+
"Any",
|
186
|
+
"Dur",
|
187
|
+
"Uint64",
|
188
|
+
]
|
189
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
190
|
+
.map((fn) => typeof (obj as any)[fn] === "function")
|
191
|
+
.reduce((a, b) => a && b, true)
|
192
|
+
);
|
193
|
+
}
|
194
|
+
|
195
|
+
export interface WithLogger extends LoggerInterface<WithLogger> {
|
196
|
+
Logger(): Logger;
|
197
|
+
}
|
198
|
+
|
199
|
+
export interface AsError {
|
200
|
+
AsError(): Error;
|
201
|
+
}
|
202
|
+
|
203
|
+
export interface Logger extends LoggerInterface<Logger> {
|
204
|
+
With(): WithLogger;
|
205
|
+
|
206
|
+
Msg(...args: string[]): AsError;
|
207
|
+
Flush(): Promise<void>;
|
208
|
+
}
|
@@ -0,0 +1,92 @@
|
|
1
|
+
import { FileService, NamedWritableStream } from "../file-service.js";
|
2
|
+
import { TxtEnDecoder, Utf8EnDecoderSingleton } from "../txt-en-decoder.js";
|
3
|
+
import * as path from "node:path";
|
4
|
+
|
5
|
+
const Deno = (globalThis as unknown as { Deno: unknown }).Deno as {
|
6
|
+
cwd(): string;
|
7
|
+
readFile(fname: string): Promise<Uint8Array>;
|
8
|
+
mkdir(base: string, options: { recursive: boolean }): Promise<void>;
|
9
|
+
open(fname: string, options: { write: boolean; create: boolean; truncate: boolean }): Promise<WritableStream>;
|
10
|
+
};
|
11
|
+
|
12
|
+
export class DenoFileService implements FileService {
|
13
|
+
readonly baseDir: string;
|
14
|
+
readonly txtEnde: TxtEnDecoder;
|
15
|
+
constructor(baseDir: string = Deno.cwd(), txtEnde: TxtEnDecoder = Utf8EnDecoderSingleton()) {
|
16
|
+
this.baseDir = this.abs(baseDir);
|
17
|
+
this.txtEnde = txtEnde;
|
18
|
+
}
|
19
|
+
|
20
|
+
// nodeImport(fname: string): string {
|
21
|
+
// // console.log('nodeImport:'+ fname);
|
22
|
+
// if (path.isAbsolute(fname)) {
|
23
|
+
// return fname;
|
24
|
+
// } else {
|
25
|
+
// return "./" + path.normalize(fname);
|
26
|
+
// }
|
27
|
+
// }
|
28
|
+
|
29
|
+
async readFileString(fname: string): Promise<string> {
|
30
|
+
return this.txtEnde.decode(await Deno.readFile(fname));
|
31
|
+
}
|
32
|
+
|
33
|
+
dirname(fname: string): string {
|
34
|
+
return path.dirname(fname);
|
35
|
+
}
|
36
|
+
basename(fname: string): string {
|
37
|
+
return path.basename(fname);
|
38
|
+
}
|
39
|
+
|
40
|
+
join(...paths: string[]): string {
|
41
|
+
return path.join(...paths);
|
42
|
+
}
|
43
|
+
|
44
|
+
relative(from: string, to?: string): string {
|
45
|
+
if (to === undefined) {
|
46
|
+
to = from;
|
47
|
+
from = Deno.cwd();
|
48
|
+
}
|
49
|
+
const ret = path.relative(from, to);
|
50
|
+
// console.log('relative:'+ from + " -> " + to + "= " + ret);
|
51
|
+
return ret;
|
52
|
+
}
|
53
|
+
|
54
|
+
abs(fname: string): string {
|
55
|
+
if (path.isAbsolute(fname)) {
|
56
|
+
return fname;
|
57
|
+
} else {
|
58
|
+
const cwd = Deno.cwd();
|
59
|
+
return path.resolve(cwd, fname);
|
60
|
+
}
|
61
|
+
}
|
62
|
+
|
63
|
+
isAbsolute(fname: string): boolean {
|
64
|
+
return path.isAbsolute(fname);
|
65
|
+
}
|
66
|
+
|
67
|
+
async writeFileString(fname: string, content: string, ende = Utf8EnDecoderSingleton()): Promise<void> {
|
68
|
+
const o = await this.create(fname);
|
69
|
+
const wr = o.stream.getWriter();
|
70
|
+
await wr.write(ende.encode(content));
|
71
|
+
await wr.close();
|
72
|
+
}
|
73
|
+
|
74
|
+
async create(fname: string): Promise<NamedWritableStream> {
|
75
|
+
let oName = fname;
|
76
|
+
if (!path.isAbsolute(fname)) {
|
77
|
+
oName = this.abs(fname);
|
78
|
+
}
|
79
|
+
|
80
|
+
const base = path.dirname(oName);
|
81
|
+
await Deno.mkdir(base, { recursive: true });
|
82
|
+
const out = await Deno.open(oName, {
|
83
|
+
write: true,
|
84
|
+
create: true,
|
85
|
+
truncate: true,
|
86
|
+
});
|
87
|
+
return {
|
88
|
+
name: oName,
|
89
|
+
stream: out,
|
90
|
+
};
|
91
|
+
}
|
92
|
+
}
|
@@ -0,0 +1,133 @@
|
|
1
|
+
import { SysAbstraction, SystemService, VoidFunc } from "../sys-abstraction.js";
|
2
|
+
import {
|
3
|
+
BaseSysAbstraction,
|
4
|
+
ExitHandler,
|
5
|
+
ExitService,
|
6
|
+
WrapperSysAbstraction,
|
7
|
+
WrapperSysAbstractionParams,
|
8
|
+
} from "../base-sys-abstraction.js";
|
9
|
+
import { Env, envFactory } from "../sys-env.js";
|
10
|
+
import { Utf8EnDecoderSingleton } from "../txt-en-decoder.js";
|
11
|
+
import * as process from "node:process";
|
12
|
+
import { DenoFileService } from "./deno-file-service.js";
|
13
|
+
|
14
|
+
const Deno = (globalThis as unknown as { Deno: unknown }).Deno as {
|
15
|
+
addSignalListener(sig: string, hdl: () => void): void;
|
16
|
+
exit(code?: number): void;
|
17
|
+
};
|
18
|
+
|
19
|
+
export class DenoExitServiceImpl implements ExitService {
|
20
|
+
constructor() {
|
21
|
+
globalThis.addEventListener("unhandledrejection", (e) => {
|
22
|
+
e.preventDefault();
|
23
|
+
this.exit(19);
|
24
|
+
});
|
25
|
+
globalThis.addEventListener("error", () => {
|
26
|
+
this.exit(19);
|
27
|
+
});
|
28
|
+
globalThis.addEventListener("uncaughtException", () => {
|
29
|
+
this.exit(19);
|
30
|
+
});
|
31
|
+
|
32
|
+
// process.on("close", () => {
|
33
|
+
// this.exit(0);
|
34
|
+
// });
|
35
|
+
globalThis.addEventListener("unload", () => {
|
36
|
+
this.exit(0);
|
37
|
+
// console.log('goodbye!');
|
38
|
+
});
|
39
|
+
|
40
|
+
// process.on("exit", () => {
|
41
|
+
// });
|
42
|
+
Deno.addSignalListener("SIGQUIT", () => {
|
43
|
+
this.exit(3);
|
44
|
+
});
|
45
|
+
Deno.addSignalListener("SIGINT", () => {
|
46
|
+
this.exit(2);
|
47
|
+
});
|
48
|
+
Deno.addSignalListener("SIGTERM", () => {
|
49
|
+
this.exit(9);
|
50
|
+
});
|
51
|
+
}
|
52
|
+
_exitHandlers: ExitHandler[] = [];
|
53
|
+
injectExitHandlers(hdls: ExitHandler[]): void {
|
54
|
+
// console.log("ExitService: injecting exit handlers", hdls)
|
55
|
+
this._exitHandlers = hdls;
|
56
|
+
}
|
57
|
+
invoked = false;
|
58
|
+
readonly _handleExit = async (): Promise<void> => {
|
59
|
+
if (this.invoked) {
|
60
|
+
// console.error("ExitService: already invoked");
|
61
|
+
return;
|
62
|
+
}
|
63
|
+
this.invoked = true;
|
64
|
+
for (const h of this._exitHandlers) {
|
65
|
+
try {
|
66
|
+
// console.log(`ExitService: calling handler ${h.id}`)
|
67
|
+
const ret = h.hdl();
|
68
|
+
// console.log(`ExitService: called handler ${h.id}`, ret)
|
69
|
+
if (typeof (ret as Promise<void>).then === "function") {
|
70
|
+
await ret;
|
71
|
+
}
|
72
|
+
} finally {
|
73
|
+
// ignore
|
74
|
+
}
|
75
|
+
}
|
76
|
+
};
|
77
|
+
|
78
|
+
exit(code: number): void {
|
79
|
+
// console.log("ExitService: exit called", code)
|
80
|
+
this._handleExit()
|
81
|
+
.then(() => {
|
82
|
+
Deno.exit(code);
|
83
|
+
})
|
84
|
+
.catch((err) => {
|
85
|
+
// eslint-disable-next-line no-console
|
86
|
+
console.error("ExitService: failed to handle exit", err);
|
87
|
+
Deno.exit(code);
|
88
|
+
});
|
89
|
+
}
|
90
|
+
}
|
91
|
+
|
92
|
+
export class DenoSystemService implements SystemService {
|
93
|
+
static readonly _exitHandlers: ExitHandler[] = [];
|
94
|
+
readonly _exitService: ExitService = new DenoExitServiceImpl();
|
95
|
+
constructor() {
|
96
|
+
this._exitService.injectExitHandlers(DenoSystemService._exitHandlers);
|
97
|
+
}
|
98
|
+
|
99
|
+
Env(): Env {
|
100
|
+
return envFactory();
|
101
|
+
}
|
102
|
+
|
103
|
+
Args(): string[] {
|
104
|
+
return process.argv;
|
105
|
+
}
|
106
|
+
|
107
|
+
OnExit(hdl: VoidFunc): VoidFunc {
|
108
|
+
const id = crypto.randomUUID();
|
109
|
+
DenoSystemService._exitHandlers.push({ hdl, id });
|
110
|
+
return () => {
|
111
|
+
const idx = DenoSystemService._exitHandlers.findIndex((h) => h.id === id);
|
112
|
+
if (idx >= 0) {
|
113
|
+
DenoSystemService._exitHandlers.splice(idx, 1);
|
114
|
+
}
|
115
|
+
};
|
116
|
+
}
|
117
|
+
|
118
|
+
Exit(code: number): void {
|
119
|
+
this._exitService.exit(code);
|
120
|
+
}
|
121
|
+
}
|
122
|
+
|
123
|
+
let my: BaseSysAbstraction | undefined = undefined;
|
124
|
+
export function DenoSysAbstraction(param?: WrapperSysAbstractionParams): SysAbstraction {
|
125
|
+
if (!my) {
|
126
|
+
my = new BaseSysAbstraction({
|
127
|
+
TxtEnDecoder: param?.TxtEnDecoder || Utf8EnDecoderSingleton(),
|
128
|
+
FileSystem: new DenoFileService(),
|
129
|
+
SystemService: new DenoSystemService(),
|
130
|
+
});
|
131
|
+
}
|
132
|
+
return new WrapperSysAbstraction(my, param);
|
133
|
+
}
|
@@ -0,0 +1,45 @@
|
|
1
|
+
import { NamedWritableStream } from "../file-service.js";
|
2
|
+
import { NodeFileService } from "./node-file-service.js";
|
3
|
+
|
4
|
+
export interface FileCollector {
|
5
|
+
readonly name: string;
|
6
|
+
content: string;
|
7
|
+
}
|
8
|
+
|
9
|
+
export class MockFileService extends NodeFileService {
|
10
|
+
readonly files = {} as Record<string, FileCollector>;
|
11
|
+
|
12
|
+
// override abs(fname: string): string {
|
13
|
+
// return this.join("/mock/", fname);
|
14
|
+
// }
|
15
|
+
|
16
|
+
override async create(fname: string): Promise<NamedWritableStream> {
|
17
|
+
let oName = fname;
|
18
|
+
if (!this.isAbsolute(fname)) {
|
19
|
+
oName = await this.abs(fname);
|
20
|
+
}
|
21
|
+
|
22
|
+
const fc = {
|
23
|
+
name: oName,
|
24
|
+
content: "",
|
25
|
+
};
|
26
|
+
this.files[oName] = fc;
|
27
|
+
this.files[fname] = fc;
|
28
|
+
const decoder = new TextDecoder();
|
29
|
+
|
30
|
+
return {
|
31
|
+
name: oName,
|
32
|
+
stream: new WritableStream<Uint8Array>({
|
33
|
+
write(chunk): void {
|
34
|
+
fc.content = fc.content + decoder.decode(chunk);
|
35
|
+
},
|
36
|
+
close(): void {
|
37
|
+
// do nothing
|
38
|
+
},
|
39
|
+
abort(): void {
|
40
|
+
throw new Error("not implemented");
|
41
|
+
},
|
42
|
+
}),
|
43
|
+
};
|
44
|
+
}
|
45
|
+
}
|
@@ -0,0 +1,91 @@
|
|
1
|
+
import path from "node:path";
|
2
|
+
import fs from "node:fs";
|
3
|
+
import process from "node:process";
|
4
|
+
import { FileService, NamedWritableStream } from "../file-service.js";
|
5
|
+
import { TxtEnDecoder, Utf8EnDecoderSingleton } from "../txt-en-decoder.js";
|
6
|
+
|
7
|
+
export class NodeFileService implements FileService {
|
8
|
+
readonly baseDir: string;
|
9
|
+
constructor(baseDir: string = process.cwd()) {
|
10
|
+
this.baseDir = this.abs(baseDir);
|
11
|
+
}
|
12
|
+
|
13
|
+
// nodeImport(fname: string): string {
|
14
|
+
// // console.log('nodeImport:'+ fname);
|
15
|
+
// if (path.isAbsolute(fname)) {
|
16
|
+
// return fname;
|
17
|
+
// } else {
|
18
|
+
// return "./" + path.normalize(fname);
|
19
|
+
// }
|
20
|
+
// }
|
21
|
+
|
22
|
+
readFileString(fname: string): Promise<string> {
|
23
|
+
return fs.promises.readFile(fname, { encoding: "utf-8" });
|
24
|
+
}
|
25
|
+
|
26
|
+
dirname(fname: string): string {
|
27
|
+
return path.dirname(fname);
|
28
|
+
}
|
29
|
+
basename(fname: string): string {
|
30
|
+
return path.basename(fname);
|
31
|
+
}
|
32
|
+
|
33
|
+
join(...paths: string[]): string {
|
34
|
+
return path.join(...paths);
|
35
|
+
}
|
36
|
+
|
37
|
+
relative(from: string, to?: string): string {
|
38
|
+
if (to === undefined) {
|
39
|
+
to = from;
|
40
|
+
from = process.cwd();
|
41
|
+
}
|
42
|
+
const ret = path.relative(from, to);
|
43
|
+
// console.log('relative:'+ from + " -> " + to + "= " + ret);
|
44
|
+
return ret;
|
45
|
+
}
|
46
|
+
|
47
|
+
abs(fname: string): string {
|
48
|
+
if (path.isAbsolute(fname)) {
|
49
|
+
return fname;
|
50
|
+
} else {
|
51
|
+
const cwd = process.cwd();
|
52
|
+
return path.resolve(cwd, fname);
|
53
|
+
}
|
54
|
+
}
|
55
|
+
|
56
|
+
isAbsolute(fname: string): boolean {
|
57
|
+
return path.isAbsolute(fname);
|
58
|
+
}
|
59
|
+
|
60
|
+
async writeFileString(fname: string, content: string, ende: TxtEnDecoder = Utf8EnDecoderSingleton()): Promise<void> {
|
61
|
+
const o = await this.create(fname);
|
62
|
+
const wr = o.stream.getWriter();
|
63
|
+
await wr.write(ende.encode(content));
|
64
|
+
await wr.close();
|
65
|
+
}
|
66
|
+
|
67
|
+
async create(fname: string): Promise<NamedWritableStream> {
|
68
|
+
let oName = fname;
|
69
|
+
if (!path.isAbsolute(fname)) {
|
70
|
+
oName = this.abs(fname);
|
71
|
+
}
|
72
|
+
|
73
|
+
const base = path.dirname(oName);
|
74
|
+
await fs.promises.mkdir(base, { recursive: true });
|
75
|
+
const out = fs.createWriteStream(oName);
|
76
|
+
return {
|
77
|
+
name: oName,
|
78
|
+
stream: new WritableStream<Uint8Array>({
|
79
|
+
write(chunk): void {
|
80
|
+
out.write(chunk);
|
81
|
+
},
|
82
|
+
close(): void {
|
83
|
+
out.close();
|
84
|
+
},
|
85
|
+
abort(): void {
|
86
|
+
throw new Error("not implemented");
|
87
|
+
},
|
88
|
+
}),
|
89
|
+
};
|
90
|
+
}
|
91
|
+
}
|
@@ -0,0 +1,121 @@
|
|
1
|
+
import { SysAbstraction, SystemService, VoidFunc } from "../sys-abstraction.js";
|
2
|
+
import {
|
3
|
+
BaseSysAbstraction,
|
4
|
+
ExitHandler,
|
5
|
+
ExitService,
|
6
|
+
WrapperSysAbstraction,
|
7
|
+
WrapperSysAbstractionParams,
|
8
|
+
} from "../base-sys-abstraction.js";
|
9
|
+
import { NodeFileService } from "./node-file-service.js";
|
10
|
+
import { Env, envFactory } from "../sys-env.js";
|
11
|
+
import { Utf8EnDecoderSingleton } from "../txt-en-decoder.js";
|
12
|
+
import process from "node:process";
|
13
|
+
|
14
|
+
export class NodeExitServiceImpl implements ExitService {
|
15
|
+
constructor() {
|
16
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
17
|
+
process.on("unhandledRejection", (reason: string, p: Promise<unknown>) => {
|
18
|
+
this.exit(19);
|
19
|
+
});
|
20
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
21
|
+
process.on("uncaughtException", (error: Error) => {
|
22
|
+
this.exit(18);
|
23
|
+
});
|
24
|
+
process.on("close", () => {
|
25
|
+
this.exit(0);
|
26
|
+
});
|
27
|
+
process.on("exit", () => {
|
28
|
+
this.exit(0);
|
29
|
+
});
|
30
|
+
process.on("SIGQUIT", () => {
|
31
|
+
this.exit(3);
|
32
|
+
});
|
33
|
+
process.on("SIGINT", () => {
|
34
|
+
this.exit(2);
|
35
|
+
});
|
36
|
+
process.on("SIGTERM", () => {
|
37
|
+
this.exit(9);
|
38
|
+
});
|
39
|
+
}
|
40
|
+
_exitHandlers: ExitHandler[] = [];
|
41
|
+
injectExitHandlers(hdls: ExitHandler[]): void {
|
42
|
+
// console.log("ExitService: injecting exit handlers", hdls)
|
43
|
+
this._exitHandlers = hdls;
|
44
|
+
}
|
45
|
+
invoked = false;
|
46
|
+
readonly _handleExit = async (): Promise<void> => {
|
47
|
+
if (this.invoked) {
|
48
|
+
// console.error("ExitService: already invoked");
|
49
|
+
return;
|
50
|
+
}
|
51
|
+
this.invoked = true;
|
52
|
+
for (const h of this._exitHandlers) {
|
53
|
+
try {
|
54
|
+
// console.log(`ExitService: calling handler ${h.id}`)
|
55
|
+
const ret = h.hdl();
|
56
|
+
// console.log(`ExitService: called handler ${h.id}`, ret)
|
57
|
+
if (typeof (ret as Promise<void>).then === "function") {
|
58
|
+
await ret;
|
59
|
+
}
|
60
|
+
} finally {
|
61
|
+
// ignore
|
62
|
+
}
|
63
|
+
}
|
64
|
+
};
|
65
|
+
|
66
|
+
exit(code: number): void {
|
67
|
+
// console.log("ExitService: exit called", code)
|
68
|
+
this._handleExit()
|
69
|
+
.then(() => {
|
70
|
+
process.exit(code);
|
71
|
+
})
|
72
|
+
.catch((err) => {
|
73
|
+
// eslint-disable-next-line no-console
|
74
|
+
console.error("ExitService: failed to handle exit", err);
|
75
|
+
process.exit(code);
|
76
|
+
});
|
77
|
+
}
|
78
|
+
}
|
79
|
+
|
80
|
+
export class NodeSystemService implements SystemService {
|
81
|
+
static readonly _exitHandlers: ExitHandler[] = [];
|
82
|
+
readonly _exitService: ExitService = new NodeExitServiceImpl();
|
83
|
+
constructor() {
|
84
|
+
this._exitService.injectExitHandlers(NodeSystemService._exitHandlers);
|
85
|
+
}
|
86
|
+
|
87
|
+
Env(): Env {
|
88
|
+
return envFactory();
|
89
|
+
}
|
90
|
+
|
91
|
+
Args(): string[] {
|
92
|
+
return process.argv;
|
93
|
+
}
|
94
|
+
|
95
|
+
OnExit(hdl: VoidFunc): VoidFunc {
|
96
|
+
const id = crypto.randomUUID();
|
97
|
+
NodeSystemService._exitHandlers.push({ hdl, id });
|
98
|
+
return () => {
|
99
|
+
const idx = NodeSystemService._exitHandlers.findIndex((h) => h.id === id);
|
100
|
+
if (idx >= 0) {
|
101
|
+
NodeSystemService._exitHandlers.splice(idx, 1);
|
102
|
+
}
|
103
|
+
};
|
104
|
+
}
|
105
|
+
|
106
|
+
Exit(code: number): void {
|
107
|
+
this._exitService.exit(code);
|
108
|
+
}
|
109
|
+
}
|
110
|
+
|
111
|
+
let my: BaseSysAbstraction | undefined = undefined;
|
112
|
+
export function NodeSysAbstraction(param?: WrapperSysAbstractionParams): SysAbstraction {
|
113
|
+
if (!my) {
|
114
|
+
my = new BaseSysAbstraction({
|
115
|
+
TxtEnDecoder: param?.TxtEnDecoder || Utf8EnDecoderSingleton(),
|
116
|
+
FileSystem: new NodeFileService(),
|
117
|
+
SystemService: new NodeSystemService(),
|
118
|
+
});
|
119
|
+
}
|
120
|
+
return new WrapperSysAbstraction(my, param);
|
121
|
+
}
|