@adviser/cement 0.2.28 → 0.2.30

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.
Files changed (318) hide show
  1. package/{base-sys-abstraction-C9WW3w57.d.cts → base-sys-abstraction-BkEiLHl0.d.ts} +8 -4
  2. package/{base-sys-abstraction-C9WW3w57.d.ts → base-sys-abstraction-Qj7pkY1N.d.cts} +8 -4
  3. package/{chunk-OXD3YZZ7.js → chunk-7KFVMTOS.js} +3 -47
  4. package/chunk-7KFVMTOS.js.map +1 -0
  5. package/{chunk-P5RXA4C4.js → chunk-GES3MUGV.js} +24 -27
  6. package/{chunk-LCPYQVWT.js.map → chunk-GES3MUGV.js.map} +1 -1
  7. package/{chunk-YDIUMYIE.js → chunk-Q65HLCNL.js} +8 -7
  8. package/chunk-Q65HLCNL.js.map +1 -0
  9. package/{chunk-DPIL5UIL.js → chunk-WMMUXBDX.js} +6 -2
  10. package/chunk-WMMUXBDX.js.map +1 -0
  11. package/{index-Dhb2fQiw.d.cts → index-Q3phXzYr.d.cts} +2 -21
  12. package/{index-CcsGcehs.d.ts → index-tIGZMHTc.d.ts} +2 -21
  13. package/index.cjs +288 -7345
  14. package/index.cjs.map +1 -1
  15. package/index.d.cts +74 -12
  16. package/index.d.ts +74 -12
  17. package/index.js +239 -7266
  18. package/index.js.map +1 -1
  19. package/node/index.cjs +220 -39
  20. package/node/index.cjs.map +1 -1
  21. package/node/index.d.cts +22 -4
  22. package/node/index.d.ts +22 -4
  23. package/node/index.js +201 -25
  24. package/node/index.js.map +1 -1
  25. package/package.json +34 -23
  26. package/src/LICENSE +201 -0
  27. package/src/README.md +39 -0
  28. package/src/base-sys-abstraction.ts +242 -0
  29. package/src/bin2text.ts +47 -0
  30. package/src/crypto.ts +125 -0
  31. package/src/file-service.ts +24 -0
  32. package/src/future.ts +27 -0
  33. package/src/index.ts +23 -0
  34. package/src/jsr.json +22 -0
  35. package/src/log-level-impl.ts +87 -0
  36. package/src/log-writer-impl.ts +58 -0
  37. package/src/logger-impl.ts +498 -0
  38. package/src/logger.ts +208 -0
  39. package/src/node/deno-file-service.ts +92 -0
  40. package/src/node/deno-sys-abstraction.ts +133 -0
  41. package/src/node/index.ts +4 -0
  42. package/src/node/mock-file-service.ts +45 -0
  43. package/src/node/node-file-service.ts +91 -0
  44. package/src/node/node-sys-abstraction.ts +121 -0
  45. package/src/option.ts +60 -0
  46. package/src/refcounted.ts +23 -0
  47. package/src/resolve-once.ts +179 -0
  48. package/src/result.ts +165 -0
  49. package/src/runtime.ts +36 -0
  50. package/src/sys-abstraction.ts +53 -0
  51. package/src/sys-env.ts +216 -0
  52. package/src/test/log-write-stream.ts +95 -0
  53. package/src/test/mock-logger.ts +40 -0
  54. package/src/time.ts +20 -0
  55. package/src/tracer.ts +222 -0
  56. package/src/txt-en-decoder.ts +21 -0
  57. package/src/uri.ts +421 -0
  58. package/src/utils/console-write-stream.ts +72 -0
  59. package/src/utils/fanout-write-stream.ts +32 -0
  60. package/src/utils/index.ts +6 -0
  61. package/src/utils/rebuffer.ts +75 -0
  62. package/src/utils/stream-map.ts +67 -0
  63. package/src/utils/stream2string.ts +47 -0
  64. package/src/utils/string2stream.ts +14 -0
  65. package/src/version.ts +3 -0
  66. package/src/web/index.ts +1 -0
  67. package/src/web/web-sys-abstraction.ts +80 -0
  68. package/ts/base-sys-abstraction.d.ts +84 -0
  69. package/ts/base-sys-abstraction.d.ts.map +1 -0
  70. package/ts/base-sys-abstraction.js +178 -0
  71. package/ts/base-sys-abstraction.js.map +1 -0
  72. package/ts/base-sys-abstraction.test.d.ts +2 -0
  73. package/ts/base-sys-abstraction.test.d.ts.map +1 -0
  74. package/ts/base-sys-abstraction.test.js +82 -0
  75. package/ts/base-sys-abstraction.test.js.map +1 -0
  76. package/ts/bin2text.d.ts +3 -0
  77. package/ts/bin2text.d.ts.map +1 -0
  78. package/ts/bin2text.js +43 -0
  79. package/ts/bin2text.js.map +1 -0
  80. package/ts/bin2text.test.d.ts +2 -0
  81. package/ts/bin2text.test.d.ts.map +1 -0
  82. package/ts/bin2text.test.js +51 -0
  83. package/ts/bin2text.test.js.map +1 -0
  84. package/ts/crypto.d.ts +76 -0
  85. package/ts/crypto.d.ts.map +1 -0
  86. package/ts/crypto.js +22 -0
  87. package/ts/crypto.js.map +1 -0
  88. package/ts/crypto.test.d.ts +2 -0
  89. package/ts/crypto.test.d.ts.map +1 -0
  90. package/ts/crypto.test.js +14 -0
  91. package/ts/crypto.test.js.map +1 -0
  92. package/ts/file-service.d.ts +17 -0
  93. package/ts/file-service.d.ts.map +1 -0
  94. package/ts/file-service.js +2 -0
  95. package/ts/file-service.js.map +1 -0
  96. package/ts/future.d.ts +8 -0
  97. package/ts/future.d.ts.map +1 -0
  98. package/ts/future.js +38 -0
  99. package/ts/future.js.map +1 -0
  100. package/ts/future.test.d.ts +2 -0
  101. package/ts/future.test.d.ts.map +1 -0
  102. package/ts/future.test.js +28 -0
  103. package/ts/future.test.js.map +1 -0
  104. package/ts/index.d.ts +24 -0
  105. package/ts/index.d.ts.map +1 -0
  106. package/ts/index.js +24 -0
  107. package/ts/index.js.map +1 -0
  108. package/ts/log-level-impl.d.ts +14 -0
  109. package/ts/log-level-impl.d.ts.map +1 -0
  110. package/ts/log-level-impl.js +72 -0
  111. package/ts/log-level-impl.js.map +1 -0
  112. package/ts/log-writer-impl.d.ts +10 -0
  113. package/ts/log-writer-impl.d.ts.map +1 -0
  114. package/ts/log-writer-impl.js +45 -0
  115. package/ts/log-writer-impl.js.map +1 -0
  116. package/ts/logger-impl.d.ts +71 -0
  117. package/ts/logger-impl.d.ts.map +1 -0
  118. package/ts/logger-impl.js +412 -0
  119. package/ts/logger-impl.js.map +1 -0
  120. package/ts/logger.d.ts +84 -0
  121. package/ts/logger.d.ts.map +1 -0
  122. package/ts/logger.js +114 -0
  123. package/ts/logger.js.map +1 -0
  124. package/ts/logger.test.d.ts +2 -0
  125. package/ts/logger.test.d.ts.map +1 -0
  126. package/ts/logger.test.js +1023 -0
  127. package/ts/logger.test.js.map +1 -0
  128. package/ts/node/deno-file-service.d.ts +17 -0
  129. package/ts/node/deno-file-service.d.ts.map +1 -0
  130. package/ts/node/deno-file-service.js +65 -0
  131. package/ts/node/deno-file-service.js.map +1 -0
  132. package/ts/node/deno-sys-abstraction.d.ts +22 -0
  133. package/ts/node/deno-sys-abstraction.d.ts.map +1 -0
  134. package/ts/node/deno-sys-abstraction.js +101 -0
  135. package/ts/node/deno-sys-abstraction.js.map +1 -0
  136. package/ts/node/index.d.ts +5 -0
  137. package/ts/node/index.d.ts.map +1 -0
  138. package/ts/node/index.js +5 -0
  139. package/ts/node/index.js.map +1 -0
  140. package/ts/node/mock-file-service.d.ts +11 -0
  141. package/ts/node/mock-file-service.d.ts.map +1 -0
  142. package/ts/node/mock-file-service.js +34 -0
  143. package/ts/node/mock-file-service.js.map +1 -0
  144. package/ts/node/mock-file-service.test.d.ts +2 -0
  145. package/ts/node/mock-file-service.test.d.ts.map +1 -0
  146. package/ts/node/mock-file-service.test.js +31 -0
  147. package/ts/node/mock-file-service.test.js.map +1 -0
  148. package/ts/node/node-file-service.d.ts +16 -0
  149. package/ts/node/node-file-service.d.ts.map +1 -0
  150. package/ts/node/node-file-service.js +71 -0
  151. package/ts/node/node-file-service.js.map +1 -0
  152. package/ts/node/node-sys-abstraction.d.ts +22 -0
  153. package/ts/node/node-sys-abstraction.d.ts.map +1 -0
  154. package/ts/node/node-sys-abstraction.js +99 -0
  155. package/ts/node/node-sys-abstraction.js.map +1 -0
  156. package/ts/node/node-sys-abstraction.test.d.ts +2 -0
  157. package/ts/node/node-sys-abstraction.test.d.ts.map +1 -0
  158. package/ts/node/node-sys-abstraction.test.js +83 -0
  159. package/ts/node/node-sys-abstraction.test.js.map +1 -0
  160. package/ts/option.d.ts +25 -0
  161. package/ts/option.d.ts.map +1 -0
  162. package/ts/option.js +47 -0
  163. package/ts/option.js.map +1 -0
  164. package/ts/refcounted.d.ts +2 -0
  165. package/ts/refcounted.d.ts.map +1 -0
  166. package/ts/refcounted.js +19 -0
  167. package/ts/refcounted.js.map +1 -0
  168. package/ts/refcounted.test.d.ts +2 -0
  169. package/ts/refcounted.test.d.ts.map +1 -0
  170. package/ts/refcounted.test.js +39 -0
  171. package/ts/refcounted.test.js.map +1 -0
  172. package/ts/resolve-once.d.ts +46 -0
  173. package/ts/resolve-once.d.ts.map +1 -0
  174. package/ts/resolve-once.js +152 -0
  175. package/ts/resolve-once.js.map +1 -0
  176. package/ts/resolve-once.test.d.ts +2 -0
  177. package/ts/resolve-once.test.d.ts.map +1 -0
  178. package/ts/resolve-once.test.js +283 -0
  179. package/ts/resolve-once.test.js.map +1 -0
  180. package/ts/result.d.ts +34 -0
  181. package/ts/result.d.ts.map +1 -0
  182. package/ts/result.js +85 -0
  183. package/ts/result.js.map +1 -0
  184. package/ts/result.test.d.ts +2 -0
  185. package/ts/result.test.d.ts.map +1 -0
  186. package/ts/result.test.js +79 -0
  187. package/ts/result.test.js.map +1 -0
  188. package/ts/runtime.d.ts +8 -0
  189. package/ts/runtime.d.ts.map +1 -0
  190. package/ts/runtime.js +26 -0
  191. package/ts/runtime.js.map +1 -0
  192. package/ts/sys-abstraction.d.ts +36 -0
  193. package/ts/sys-abstraction.d.ts.map +1 -0
  194. package/ts/sys-abstraction.js +31 -0
  195. package/ts/sys-abstraction.js.map +1 -0
  196. package/ts/sys-env.d.ts +48 -0
  197. package/ts/sys-env.d.ts.map +1 -0
  198. package/ts/sys-env.js +176 -0
  199. package/ts/sys-env.js.map +1 -0
  200. package/ts/sys-env.test.d.ts +2 -0
  201. package/ts/sys-env.test.d.ts.map +1 -0
  202. package/ts/sys-env.test.js +51 -0
  203. package/ts/sys-env.test.js.map +1 -0
  204. package/ts/test/log-write-stream.d.ts +27 -0
  205. package/ts/test/log-write-stream.d.ts.map +1 -0
  206. package/ts/test/log-write-stream.js +74 -0
  207. package/ts/test/log-write-stream.js.map +1 -0
  208. package/ts/test/mock-logger.d.ts +14 -0
  209. package/ts/test/mock-logger.d.ts.map +1 -0
  210. package/ts/test/mock-logger.js +29 -0
  211. package/ts/test/mock-logger.js.map +1 -0
  212. package/ts/test/mock-logger.test.d.ts +2 -0
  213. package/ts/test/mock-logger.test.d.ts.map +1 -0
  214. package/ts/test/mock-logger.test.js +63 -0
  215. package/ts/test/mock-logger.test.js.map +1 -0
  216. package/ts/test/test-exit-handler.d.ts +2 -0
  217. package/ts/test/test-exit-handler.d.ts.map +1 -0
  218. package/ts/test/test-exit-handler.js +57 -0
  219. package/ts/test/test-exit-handler.js.map +1 -0
  220. package/ts/time.d.ts +13 -0
  221. package/ts/time.d.ts.map +1 -0
  222. package/ts/time.js +14 -0
  223. package/ts/time.js.map +1 -0
  224. package/ts/tracer.d.ts +59 -0
  225. package/ts/tracer.d.ts.map +1 -0
  226. package/ts/tracer.js +148 -0
  227. package/ts/tracer.js.map +1 -0
  228. package/ts/tracer.test.d.ts +2 -0
  229. package/ts/tracer.test.d.ts.map +1 -0
  230. package/ts/tracer.test.js +311 -0
  231. package/ts/tracer.test.js.map +1 -0
  232. package/ts/txt-en-decoder.d.ts +10 -0
  233. package/ts/txt-en-decoder.d.ts.map +1 -0
  234. package/ts/txt-en-decoder.js +15 -0
  235. package/ts/txt-en-decoder.js.map +1 -0
  236. package/ts/uri.d.ts +67 -0
  237. package/ts/uri.d.ts.map +1 -0
  238. package/ts/uri.js +283 -0
  239. package/ts/uri.js.map +1 -0
  240. package/ts/uri.test.d.ts +2 -0
  241. package/ts/uri.test.d.ts.map +1 -0
  242. package/ts/uri.test.js +119 -0
  243. package/ts/uri.test.js.map +1 -0
  244. package/ts/utils/console-write-stream.d.ts +21 -0
  245. package/ts/utils/console-write-stream.d.ts.map +1 -0
  246. package/ts/utils/console-write-stream.js +62 -0
  247. package/ts/utils/console-write-stream.js.map +1 -0
  248. package/ts/utils/fanout-write-stream.d.ts +12 -0
  249. package/ts/utils/fanout-write-stream.d.ts.map +1 -0
  250. package/ts/utils/fanout-write-stream.js +24 -0
  251. package/ts/utils/fanout-write-stream.js.map +1 -0
  252. package/ts/utils/index.d.ts +7 -0
  253. package/ts/utils/index.d.ts.map +1 -0
  254. package/ts/utils/index.js +7 -0
  255. package/ts/utils/index.js.map +1 -0
  256. package/ts/utils/rebuffer.d.ts +3 -0
  257. package/ts/utils/rebuffer.d.ts.map +1 -0
  258. package/ts/utils/rebuffer.js +60 -0
  259. package/ts/utils/rebuffer.js.map +1 -0
  260. package/ts/utils/rebuffer.test.d.ts +2 -0
  261. package/ts/utils/rebuffer.test.d.ts.map +1 -0
  262. package/ts/utils/rebuffer.test.js +77 -0
  263. package/ts/utils/rebuffer.test.js.map +1 -0
  264. package/ts/utils/stream-map.d.ts +9 -0
  265. package/ts/utils/stream-map.d.ts.map +1 -0
  266. package/ts/utils/stream-map.js +62 -0
  267. package/ts/utils/stream-map.js.map +1 -0
  268. package/ts/utils/stream-map.test.d.ts +2 -0
  269. package/ts/utils/stream-map.test.d.ts.map +1 -0
  270. package/ts/utils/stream-map.test.js +87 -0
  271. package/ts/utils/stream-map.test.js.map +1 -0
  272. package/ts/utils/stream-test-helper.d.ts +17 -0
  273. package/ts/utils/stream-test-helper.d.ts.map +1 -0
  274. package/ts/utils/stream-test-helper.js +37 -0
  275. package/ts/utils/stream-test-helper.js.map +1 -0
  276. package/ts/utils/stream2string.d.ts +3 -0
  277. package/ts/utils/stream2string.d.ts.map +1 -0
  278. package/ts/utils/stream2string.js +48 -0
  279. package/ts/utils/stream2string.js.map +1 -0
  280. package/ts/utils/stream2string.test.d.ts +2 -0
  281. package/ts/utils/stream2string.test.d.ts.map +1 -0
  282. package/ts/utils/stream2string.test.js +29 -0
  283. package/ts/utils/stream2string.test.js.map +1 -0
  284. package/ts/utils/string2stream.d.ts +4 -0
  285. package/ts/utils/string2stream.d.ts.map +1 -0
  286. package/ts/utils/string2stream.js +13 -0
  287. package/ts/utils/string2stream.js.map +1 -0
  288. package/ts/utils/string2stream.test.d.ts +2 -0
  289. package/ts/utils/string2stream.test.d.ts.map +1 -0
  290. package/ts/utils/string2stream.test.js +6 -0
  291. package/ts/utils/string2stream.test.js.map +1 -0
  292. package/ts/version.d.ts +2 -0
  293. package/ts/version.d.ts.map +1 -0
  294. package/ts/version.js +4 -0
  295. package/ts/version.js.map +1 -0
  296. package/ts/web/index.d.ts +2 -0
  297. package/ts/web/index.d.ts.map +1 -0
  298. package/ts/web/index.js +2 -0
  299. package/ts/web/index.js.map +1 -0
  300. package/ts/web/web-sys-abstraction.d.ts +4 -0
  301. package/ts/web/web-sys-abstraction.d.ts.map +1 -0
  302. package/ts/web/web-sys-abstraction.js +64 -0
  303. package/ts/web/web-sys-abstraction.js.map +1 -0
  304. package/utils/index.cjs +0 -42
  305. package/utils/index.cjs.map +1 -1
  306. package/utils/index.d.cts +1 -2
  307. package/utils/index.d.ts +1 -2
  308. package/utils/index.js +2 -7
  309. package/web/index.cjs +25 -7
  310. package/web/index.cjs.map +1 -1
  311. package/web/index.d.cts +2 -1
  312. package/web/index.d.ts +2 -1
  313. package/web/index.js +3 -3
  314. package/chunk-DPIL5UIL.js.map +0 -1
  315. package/chunk-LCPYQVWT.js +0 -21
  316. package/chunk-OXD3YZZ7.js.map +0 -1
  317. package/chunk-P5RXA4C4.js.map +0 -1
  318. package/chunk-YDIUMYIE.js.map +0 -1
@@ -0,0 +1,498 @@
1
+ // import { v4 } from "uuid";
2
+ import YAML from "yaml";
3
+ import {
4
+ AsError,
5
+ FnSerialized,
6
+ LogSerializable,
7
+ Level,
8
+ Logger,
9
+ logValue,
10
+ Serialized,
11
+ WithLogger,
12
+ removeSelfRef,
13
+ Sized,
14
+ Lengthed,
15
+ LogValue,
16
+ asyncLogValue,
17
+ LevelHandler,
18
+ LogFormatter,
19
+ LogValueArg,
20
+ } from "./logger.js";
21
+ import { WebSysAbstraction } from "./web/web-sys-abstraction.js";
22
+ import { SysAbstraction } from "./sys-abstraction.js";
23
+ import { Result } from "./result.js";
24
+ import { CoerceURI, URI } from "./uri.js";
25
+ import { runtimeFn } from "./runtime.js";
26
+ import { ConsoleWriterStream } from "./utils/console-write-stream.js";
27
+ import { LogWriterStream } from "./log-writer-impl.js";
28
+ import { TxtEnDecoder, Utf8EnDecoderSingleton } from "./txt-en-decoder.js";
29
+ import { LevelHandlerSingleton } from "./log-level-impl.js";
30
+
31
+ function getLen(value: unknown): LogValue {
32
+ if (Array.isArray(value)) {
33
+ return logValue(() => value.length);
34
+ } else if (typeof value === "string") {
35
+ return logValue(() => value.length);
36
+ } else if (typeof value === "object" && value !== null) {
37
+ if (typeof (value as Sized).size === "number") {
38
+ return logValue(() => (value as Sized).size);
39
+ } else if (typeof (value as Lengthed).length === "number") {
40
+ return logValue(() => (value as Lengthed).length);
41
+ }
42
+ return logValue(() => Object.keys(value).length);
43
+ }
44
+ return logValue(() => -1);
45
+ }
46
+
47
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
48
+ function hash(value: unknown): string {
49
+ // const hasher = createHash("sha256");
50
+ // hasher.update(JSON.stringify(value, removeSelfRef()));
51
+ // return hasher.digest("hex");
52
+ return "not implemented";
53
+ }
54
+
55
+ function toLogValue(lop: LogValue | Promise<LogValue>): LogValue | undefined {
56
+ if (lop && typeof (lop as Promise<LogValue>).then === "function") {
57
+ throw new Error("async logValue Not implemented");
58
+ }
59
+ return lop as LogValue;
60
+ }
61
+
62
+ export class JSONFormatter implements LogFormatter {
63
+ private readonly _txtEnDe: TxtEnDecoder;
64
+ private readonly _space?: number;
65
+ constructor(txtEnde: TxtEnDecoder, space?: number) {
66
+ this._txtEnDe = txtEnde;
67
+ this._space = space;
68
+ }
69
+ format(attr: LogSerializable): Uint8Array {
70
+ return this._txtEnDe.encode(JSON.stringify(attr, removeSelfRef(), this._space) + "\n");
71
+ }
72
+ }
73
+
74
+ export class YAMLFormatter implements LogFormatter {
75
+ private readonly _txtEnDe: TxtEnDecoder;
76
+ private readonly _space?: number;
77
+ constructor(txtEnde: TxtEnDecoder, space?: number) {
78
+ this._txtEnDe = txtEnde;
79
+ this._space = space;
80
+ }
81
+ format(attr: LogSerializable): Uint8Array {
82
+ return this._txtEnDe.encode("---\n" + YAML.stringify(attr, removeSelfRef(), this._space) + "\n");
83
+ }
84
+ }
85
+
86
+ export interface LoggerImplParams {
87
+ readonly out?: WritableStream<Uint8Array>;
88
+ readonly logWriter?: LogWriterStream;
89
+ readonly sys?: SysAbstraction;
90
+ readonly withAttributes?: LogSerializable;
91
+ readonly levelHandler?: LevelHandler;
92
+ readonly txtEnDe?: TxtEnDecoder;
93
+ readonly formatter?: LogFormatter;
94
+ }
95
+
96
+ export class LoggerImpl implements Logger {
97
+ readonly _sys: SysAbstraction;
98
+ readonly _attributes: LogSerializable = {};
99
+ readonly _withAttributes: LogSerializable;
100
+ readonly _logWriter: LogWriterStream;
101
+ readonly _levelHandler: LevelHandler;
102
+ readonly _txtEnDe: TxtEnDecoder;
103
+ _formatter: LogFormatter;
104
+ // readonly _id: string = "logger-" + Math.random().toString(36)
105
+
106
+ constructor(params?: LoggerImplParams) {
107
+ if (!params) {
108
+ params = {};
109
+ }
110
+ if (!params.sys) {
111
+ this._sys = WebSysAbstraction();
112
+ } else {
113
+ this._sys = params.sys;
114
+ }
115
+ if (!params.txtEnDe) {
116
+ this._txtEnDe = Utf8EnDecoderSingleton();
117
+ } else {
118
+ this._txtEnDe = params.txtEnDe;
119
+ }
120
+ if (!params.formatter) {
121
+ this._formatter = new JSONFormatter(this._txtEnDe);
122
+ } else {
123
+ this._formatter = params.formatter;
124
+ }
125
+
126
+ if (params.logWriter) {
127
+ this._logWriter = params.logWriter;
128
+ } else {
129
+ if (!params.out) {
130
+ const rt = runtimeFn();
131
+ let stream: WritableStream<Uint8Array>;
132
+ if (rt.isBrowser) {
133
+ stream = new ConsoleWriterStream();
134
+ } else {
135
+ if (rt.isNodeIsh || rt.isReactNative || rt.isDeno) {
136
+ stream = this._sys.Stdout();
137
+ } else {
138
+ throw new Error("No output defined for runtime");
139
+ }
140
+ }
141
+ this._logWriter = new LogWriterStream(stream);
142
+ } else {
143
+ this._logWriter = new LogWriterStream(params.out);
144
+ }
145
+ }
146
+ if (!params.withAttributes) {
147
+ this._withAttributes = {};
148
+ } else {
149
+ this._withAttributes = { ...params.withAttributes };
150
+ }
151
+ this._attributes = { ...this._withAttributes };
152
+ if (params.levelHandler) {
153
+ this._levelHandler = params.levelHandler;
154
+ } else {
155
+ this._levelHandler = LevelHandlerSingleton();
156
+ }
157
+ // console.log("LoggerImpl", this._id, this._attributes, this._withAttributes)
158
+ }
159
+
160
+ TxtEnDe(): TxtEnDecoder {
161
+ return this._txtEnDe;
162
+ }
163
+
164
+ Attributes(): Record<string, unknown> {
165
+ return JSON.parse(JSON.stringify(this._attributes, removeSelfRef()));
166
+ // return Array.from(Object.entries(this._attributes)).reduce(
167
+ // (acc, [key, value]) => {
168
+ // if (value instanceof LogValue) {
169
+ // acc[key] = value.value();
170
+ // }
171
+ // return acc;
172
+ // },
173
+ // {} as Record<string, unknown>,
174
+ // );
175
+ }
176
+
177
+ SetExposeStack(enable?: boolean): Logger {
178
+ this._levelHandler.setExposeStack(enable);
179
+ return this;
180
+ }
181
+
182
+ EnableLevel(level: Level, ...modules: string[]): Logger {
183
+ this._levelHandler.enableLevel(level, ...modules);
184
+ return this;
185
+ }
186
+ DisableLevel(level: Level, ...modules: string[]): Logger {
187
+ this._levelHandler.disableLevel(level, ...modules);
188
+ return this;
189
+ }
190
+
191
+ Module(key: string): Logger {
192
+ this._attributes["module"] = logValue(key);
193
+ this._withAttributes["module"] = logValue(key);
194
+ return this;
195
+ }
196
+ // if the string is "*" it will enable for all modules
197
+ SetDebug(...modules: (string | string[])[]): Logger {
198
+ this._levelHandler.setDebug(...modules);
199
+ return this;
200
+ }
201
+
202
+ SetFormatter(formatter: LogFormatter): Logger {
203
+ this._formatter = formatter;
204
+ return this;
205
+ }
206
+
207
+ Timestamp(): Logger {
208
+ this._attributes["ts"] = logValue(() => this._sys.Time().Now().toISOString());
209
+ return this;
210
+ }
211
+ Warn(): Logger {
212
+ this._attributes["level"] = logValue(Level.WARN);
213
+ return this;
214
+ }
215
+ Log(): Logger {
216
+ return this;
217
+ }
218
+ Debug(): Logger {
219
+ this._attributes["level"] = logValue(Level.DEBUG);
220
+ return this;
221
+ }
222
+ Error(): Logger {
223
+ this._attributes["level"] = logValue(Level.ERROR);
224
+ return this;
225
+ }
226
+ Info(): Logger {
227
+ this._attributes["level"] = logValue(Level.INFO);
228
+ return this;
229
+ }
230
+ Err(err: unknown | Result<unknown> | Error): Logger {
231
+ if (Result.Is(err)) {
232
+ if (err.isOk()) {
233
+ this.Result("noerror", err);
234
+ } else {
235
+ this.Result("error", err);
236
+ }
237
+ } else if (err instanceof Error) {
238
+ this._attributes["error"] = logValue(err.message);
239
+ if (this._levelHandler.isStackExposed) {
240
+ this._attributes["stack"] = logValue(err.stack?.split("\n").map((s) => s.trim()));
241
+ }
242
+ } else {
243
+ this._attributes["error"] = logValue("" + err);
244
+ }
245
+ return this;
246
+ }
247
+ WithLevel(l: Level): Logger {
248
+ this._attributes["level"] = logValue(l);
249
+ return this;
250
+ }
251
+
252
+ Ref(key: string, action: { toString: () => string } | FnSerialized): Logger {
253
+ if (typeof action === "function") {
254
+ this._attributes[key] = logValue(action as FnSerialized);
255
+ } else if (typeof action.toString === "function") {
256
+ this._attributes[key] = logValue(() => action.toString());
257
+ } else {
258
+ this._attributes[key] = logValue("INVALID REF");
259
+ }
260
+ return this;
261
+ }
262
+ Bool(key: string, value: unknown): Logger {
263
+ this._attributes[key] = logValue(!!value);
264
+ return this;
265
+ }
266
+ Result<T>(key: string, res: Result<T, Error>): Logger {
267
+ if (res.isOk()) {
268
+ this._attributes[key] = logValue(res.Ok() as Serialized);
269
+ } else {
270
+ this.Err(res.Err());
271
+ }
272
+ return this;
273
+ }
274
+
275
+ Len(value: unknown, key = "len"): Logger {
276
+ this._attributes[key] = getLen(value);
277
+ return this;
278
+ }
279
+
280
+ Hash(value: unknown, key = "hash"): Logger {
281
+ this._attributes[key] = asyncLogValue(async () => `${getLen(value).value()}:${await hash(value)}`);
282
+ return this;
283
+ }
284
+
285
+ Url(url: CoerceURI, key = "url"): Logger {
286
+ this.Ref(key, () => URI.from(url).toString());
287
+ return this;
288
+ }
289
+
290
+ Str(key: string, value?: string): Logger {
291
+ this._attributes[key] = logValue(value);
292
+ return this;
293
+ }
294
+
295
+ Any(key: string, value?: string | number | boolean | LogSerializable): Logger {
296
+ this._attributes[key] = logValue(value as LogValueArg);
297
+ return this;
298
+ }
299
+ Dur(key: string, nsec: number): Logger {
300
+ this._attributes[key] = logValue(`${nsec}ms`);
301
+ // new Intl.DurationFormat("en", { style: "narrow" }).format(nsec);
302
+ return this;
303
+ }
304
+ Uint64(key: string, value: number): Logger {
305
+ this._attributes[key] = logValue(value);
306
+ return this;
307
+ }
308
+ Int(key: string, value: number): Logger {
309
+ return this.Uint64(key, value);
310
+ }
311
+
312
+ async Flush(): Promise<void> {
313
+ return new Promise((resolve) => {
314
+ this._logWriter._flush(undefined, resolve);
315
+ });
316
+ }
317
+
318
+ With(): WithLogger {
319
+ // console.log("WithLoggerBuilder.With", this._id, this._attributes, this._withAttributes);
320
+ return new WithLoggerBuilder(
321
+ new LoggerImpl({
322
+ logWriter: this._logWriter,
323
+ sys: this._sys,
324
+ levelHandler: this._levelHandler,
325
+ formatter: this._formatter,
326
+ withAttributes: {
327
+ module: this._attributes["module"],
328
+ ...this._withAttributes,
329
+ },
330
+ }),
331
+ );
332
+ }
333
+
334
+ _resetAttributes(fn: () => () => Uint8Array): () => Uint8Array {
335
+ const ret = fn();
336
+ Object.keys(this._attributes).forEach((key) => {
337
+ // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
338
+ delete this._attributes[key];
339
+ });
340
+ Object.assign(this._attributes, this._withAttributes);
341
+ return ret;
342
+ }
343
+
344
+ Msg(...args: string[]): AsError {
345
+ const fnError = this._resetAttributes(() => {
346
+ const doWrite = this._levelHandler.isEnabled(
347
+ toLogValue(this._attributes["level"])?.value(),
348
+ toLogValue(this._attributes["module"])?.value(),
349
+ );
350
+ this._attributes["msg"] = logValue(args.join(" "));
351
+ const msg = this._attributes["msg"].value();
352
+ if (typeof msg === "string" && !msg.trim().length) {
353
+ delete this._attributes["msg"];
354
+ }
355
+ let fnRet = (): Uint8Array => this._formatter.format({ ...this._attributes });
356
+ if (doWrite) {
357
+ const encoded = fnRet();
358
+ this._logWriter.write(encoded);
359
+ fnRet = (): Uint8Array => encoded;
360
+ }
361
+ return fnRet;
362
+ });
363
+ return {
364
+ AsError: () => new Error(this._txtEnDe.decode(fnError())),
365
+ };
366
+ }
367
+ }
368
+
369
+ class WithLoggerBuilder implements WithLogger {
370
+ readonly _li: LoggerImpl;
371
+ constructor(li: LoggerImpl) {
372
+ this._li = li;
373
+ }
374
+
375
+ TxtEnDe(): TxtEnDecoder {
376
+ return this._li.TxtEnDe();
377
+ }
378
+
379
+ Logger(): Logger {
380
+ Object.assign(this._li._withAttributes, this._li._attributes);
381
+ return this._li;
382
+ }
383
+
384
+ Attributes(): Record<string, unknown> {
385
+ return { ...this._li._attributes };
386
+ }
387
+
388
+ SetExposeStack(enable?: boolean): WithLogger {
389
+ this._li._levelHandler.setExposeStack(enable);
390
+ return this;
391
+ }
392
+
393
+ SetFormatter(fmt: LogFormatter): WithLogger {
394
+ this._li.SetFormatter(fmt);
395
+ return this;
396
+ }
397
+
398
+ EnableLevel(level: Level, ...modules: string[]): WithLogger {
399
+ this._li._levelHandler.enableLevel(level, ...modules);
400
+ return this;
401
+ }
402
+ DisableLevel(level: Level, ...modules: string[]): WithLogger {
403
+ this._li._levelHandler.enableLevel(level, ...modules);
404
+ return this;
405
+ }
406
+
407
+ Module(key: string): WithLogger {
408
+ this._li.Module(key);
409
+ return this;
410
+ }
411
+ SetDebug(...modules: (string | string[])[]): WithLogger {
412
+ this._li.SetDebug(...modules);
413
+ return this;
414
+ }
415
+
416
+ Str(key: string, value?: string): WithLogger {
417
+ this._li.Str(key, value);
418
+ return this;
419
+ }
420
+
421
+ Len(value: unknown, key?: string): WithLogger {
422
+ this._li.Len(value, key);
423
+ return this;
424
+ }
425
+
426
+ Hash(value: unknown, key?: string): WithLogger {
427
+ this._li.Hash(value, key);
428
+ return this;
429
+ }
430
+
431
+ Ref(key: string, action: Serialized | FnSerialized): WithLogger {
432
+ this._li.Ref(key, action);
433
+ return this;
434
+ }
435
+ Bool(key: string, value: unknown): WithLogger {
436
+ this._li.Bool(key, value);
437
+ return this;
438
+ }
439
+ Result<T>(key: string, res: Result<T, Error>): WithLogger {
440
+ this._li.Result(key, res);
441
+ return this;
442
+ }
443
+ Url(url: CoerceURI, key?: string): WithLogger {
444
+ this._li.Url(url, key);
445
+ return this;
446
+ }
447
+ Int(key: string, value: number): WithLogger {
448
+ this._li.Int(key, value);
449
+ return this;
450
+ }
451
+
452
+ Log(): WithLogger {
453
+ this._li.Log();
454
+ return this;
455
+ }
456
+
457
+ WithLevel(level: Level): WithLogger {
458
+ this._li.WithLevel(level);
459
+ return this;
460
+ }
461
+
462
+ Error(): WithLogger {
463
+ this._li.Error();
464
+ return this;
465
+ }
466
+ Warn(): WithLogger {
467
+ this._li.Error();
468
+ return this;
469
+ }
470
+ Debug(): WithLogger {
471
+ this._li.Debug();
472
+ return this;
473
+ }
474
+ Err(err: unknown): WithLogger {
475
+ this._li.Err(err);
476
+ return this;
477
+ }
478
+ Info(): WithLogger {
479
+ this._li.Info();
480
+ return this;
481
+ }
482
+ Timestamp(): WithLogger {
483
+ this._li.Timestamp();
484
+ return this;
485
+ }
486
+ Any(key: string, value: LogSerializable): WithLogger {
487
+ this._li.Any(key, value);
488
+ return this;
489
+ }
490
+ Dur(key: string, nsec: number): WithLogger {
491
+ this._li.Dur(key, nsec);
492
+ return this;
493
+ }
494
+ Uint64(key: string, value: number): WithLogger {
495
+ this._li.Uint64(key, value);
496
+ return this;
497
+ }
498
+ }
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
+ }