@adviser/cement 0.0.0-jsr-t1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (320) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +39 -0
  3. package/base-sys-abstraction-BkEiLHl0.d.ts +193 -0
  4. package/base-sys-abstraction-Qj7pkY1N.d.cts +193 -0
  5. package/chunk-7KFVMTOS.js +311 -0
  6. package/chunk-7KFVMTOS.js.map +1 -0
  7. package/chunk-GES3MUGV.js +92 -0
  8. package/chunk-GES3MUGV.js.map +1 -0
  9. package/chunk-Q65HLCNL.js +601 -0
  10. package/chunk-Q65HLCNL.js.map +1 -0
  11. package/chunk-WMMUXBDX.js +87 -0
  12. package/chunk-WMMUXBDX.js.map +1 -0
  13. package/index-Q3phXzYr.d.cts +75 -0
  14. package/index-tIGZMHTc.d.ts +75 -0
  15. package/index.cjs +2593 -0
  16. package/index.cjs.map +1 -0
  17. package/index.d.cts +532 -0
  18. package/index.d.ts +532 -0
  19. package/index.js +1533 -0
  20. package/index.js.map +1 -0
  21. package/node/index.cjs +924 -0
  22. package/node/index.cjs.map +1 -0
  23. package/node/index.d.cts +65 -0
  24. package/node/index.d.ts +65 -0
  25. package/node/index.js +398 -0
  26. package/node/index.js.map +1 -0
  27. package/package.json +81 -0
  28. package/src/base-sys-abstraction.test.ts +95 -0
  29. package/src/base-sys-abstraction.ts +242 -0
  30. package/src/bin2text.test.ts +59 -0
  31. package/src/bin2text.ts +47 -0
  32. package/src/crypto.test.ts +15 -0
  33. package/src/crypto.ts +125 -0
  34. package/src/file-service.ts +24 -0
  35. package/src/future.test.ts +32 -0
  36. package/src/future.ts +27 -0
  37. package/src/index.ts +22 -0
  38. package/src/jsr.json +20 -0
  39. package/src/log-level-impl.ts +87 -0
  40. package/src/log-writer-impl.ts +58 -0
  41. package/src/logger-impl.ts +498 -0
  42. package/src/logger.test.ts +1132 -0
  43. package/src/logger.ts +208 -0
  44. package/src/node/deno-file-service.ts +92 -0
  45. package/src/node/deno-sys-abstraction.ts +133 -0
  46. package/src/node/index.ts +4 -0
  47. package/src/node/mock-file-service.ts +45 -0
  48. package/src/node/node-file-service.ts +91 -0
  49. package/src/node/node-sys-abstraction.ts +121 -0
  50. package/src/option.ts +60 -0
  51. package/src/resolve-once.test.ts +321 -0
  52. package/src/resolve-once.ts +179 -0
  53. package/src/result.test.ts +102 -0
  54. package/src/result.ts +165 -0
  55. package/src/runtime.ts +36 -0
  56. package/src/sys-abstraction.ts +53 -0
  57. package/src/sys-env.test.ts +53 -0
  58. package/src/sys-env.ts +216 -0
  59. package/src/test/log-write-stream.ts +95 -0
  60. package/src/test/mock-logger.ts +40 -0
  61. package/src/time.ts +20 -0
  62. package/src/tracer.test.ts +314 -0
  63. package/src/tracer.ts +222 -0
  64. package/src/txt-en-decoder.ts +21 -0
  65. package/src/uri.test.ts +155 -0
  66. package/src/uri.ts +421 -0
  67. package/src/utils/console-write-stream.ts +72 -0
  68. package/src/utils/fanout-write-stream.ts +32 -0
  69. package/src/utils/index.ts +6 -0
  70. package/src/utils/rebuffer.ts +75 -0
  71. package/src/utils/stream-map.ts +67 -0
  72. package/src/utils/stream2string.ts +47 -0
  73. package/src/utils/string2stream.ts +14 -0
  74. package/src/version.ts +3 -0
  75. package/src/web/index.ts +1 -0
  76. package/src/web/web-sys-abstraction.ts +80 -0
  77. package/ts/LICENSE +201 -0
  78. package/ts/README.md +39 -0
  79. package/ts/base-sys-abstraction.d.ts +84 -0
  80. package/ts/base-sys-abstraction.d.ts.map +1 -0
  81. package/ts/base-sys-abstraction.js +178 -0
  82. package/ts/base-sys-abstraction.js.map +1 -0
  83. package/ts/base-sys-abstraction.test.d.ts +2 -0
  84. package/ts/base-sys-abstraction.test.d.ts.map +1 -0
  85. package/ts/base-sys-abstraction.test.js +82 -0
  86. package/ts/base-sys-abstraction.test.js.map +1 -0
  87. package/ts/bin2text.d.ts +3 -0
  88. package/ts/bin2text.d.ts.map +1 -0
  89. package/ts/bin2text.js +43 -0
  90. package/ts/bin2text.js.map +1 -0
  91. package/ts/bin2text.test.d.ts +2 -0
  92. package/ts/bin2text.test.d.ts.map +1 -0
  93. package/ts/bin2text.test.js +51 -0
  94. package/ts/bin2text.test.js.map +1 -0
  95. package/ts/crypto.d.ts +76 -0
  96. package/ts/crypto.d.ts.map +1 -0
  97. package/ts/crypto.js +22 -0
  98. package/ts/crypto.js.map +1 -0
  99. package/ts/crypto.test.d.ts +2 -0
  100. package/ts/crypto.test.d.ts.map +1 -0
  101. package/ts/crypto.test.js +14 -0
  102. package/ts/crypto.test.js.map +1 -0
  103. package/ts/file-service.d.ts +17 -0
  104. package/ts/file-service.d.ts.map +1 -0
  105. package/ts/file-service.js +2 -0
  106. package/ts/file-service.js.map +1 -0
  107. package/ts/future.d.ts +8 -0
  108. package/ts/future.d.ts.map +1 -0
  109. package/ts/future.js +38 -0
  110. package/ts/future.js.map +1 -0
  111. package/ts/future.test.d.ts +2 -0
  112. package/ts/future.test.d.ts.map +1 -0
  113. package/ts/future.test.js +28 -0
  114. package/ts/future.test.js.map +1 -0
  115. package/ts/index.d.ts +23 -0
  116. package/ts/index.d.ts.map +1 -0
  117. package/ts/index.js +23 -0
  118. package/ts/index.js.map +1 -0
  119. package/ts/log-level-impl.d.ts +14 -0
  120. package/ts/log-level-impl.d.ts.map +1 -0
  121. package/ts/log-level-impl.js +72 -0
  122. package/ts/log-level-impl.js.map +1 -0
  123. package/ts/log-writer-impl.d.ts +10 -0
  124. package/ts/log-writer-impl.d.ts.map +1 -0
  125. package/ts/log-writer-impl.js +45 -0
  126. package/ts/log-writer-impl.js.map +1 -0
  127. package/ts/logger-impl.d.ts +71 -0
  128. package/ts/logger-impl.d.ts.map +1 -0
  129. package/ts/logger-impl.js +412 -0
  130. package/ts/logger-impl.js.map +1 -0
  131. package/ts/logger.d.ts +84 -0
  132. package/ts/logger.d.ts.map +1 -0
  133. package/ts/logger.js +114 -0
  134. package/ts/logger.js.map +1 -0
  135. package/ts/logger.test.d.ts +2 -0
  136. package/ts/logger.test.d.ts.map +1 -0
  137. package/ts/logger.test.js +1023 -0
  138. package/ts/logger.test.js.map +1 -0
  139. package/ts/node/deno-file-service.d.ts +17 -0
  140. package/ts/node/deno-file-service.d.ts.map +1 -0
  141. package/ts/node/deno-file-service.js +65 -0
  142. package/ts/node/deno-file-service.js.map +1 -0
  143. package/ts/node/deno-sys-abstraction.d.ts +22 -0
  144. package/ts/node/deno-sys-abstraction.d.ts.map +1 -0
  145. package/ts/node/deno-sys-abstraction.js +101 -0
  146. package/ts/node/deno-sys-abstraction.js.map +1 -0
  147. package/ts/node/index.d.ts +5 -0
  148. package/ts/node/index.d.ts.map +1 -0
  149. package/ts/node/index.js +5 -0
  150. package/ts/node/index.js.map +1 -0
  151. package/ts/node/mock-file-service.d.ts +11 -0
  152. package/ts/node/mock-file-service.d.ts.map +1 -0
  153. package/ts/node/mock-file-service.js +34 -0
  154. package/ts/node/mock-file-service.js.map +1 -0
  155. package/ts/node/mock-file-service.test.d.ts +2 -0
  156. package/ts/node/mock-file-service.test.d.ts.map +1 -0
  157. package/ts/node/mock-file-service.test.js +31 -0
  158. package/ts/node/mock-file-service.test.js.map +1 -0
  159. package/ts/node/node-file-service.d.ts +16 -0
  160. package/ts/node/node-file-service.d.ts.map +1 -0
  161. package/ts/node/node-file-service.js +71 -0
  162. package/ts/node/node-file-service.js.map +1 -0
  163. package/ts/node/node-sys-abstraction.d.ts +22 -0
  164. package/ts/node/node-sys-abstraction.d.ts.map +1 -0
  165. package/ts/node/node-sys-abstraction.js +99 -0
  166. package/ts/node/node-sys-abstraction.js.map +1 -0
  167. package/ts/node/node-sys-abstraction.test.d.ts +2 -0
  168. package/ts/node/node-sys-abstraction.test.d.ts.map +1 -0
  169. package/ts/node/node-sys-abstraction.test.js +87 -0
  170. package/ts/node/node-sys-abstraction.test.js.map +1 -0
  171. package/ts/option.d.ts +25 -0
  172. package/ts/option.d.ts.map +1 -0
  173. package/ts/option.js +47 -0
  174. package/ts/option.js.map +1 -0
  175. package/ts/resolve-once.d.ts +46 -0
  176. package/ts/resolve-once.d.ts.map +1 -0
  177. package/ts/resolve-once.js +152 -0
  178. package/ts/resolve-once.js.map +1 -0
  179. package/ts/resolve-once.test.d.ts +2 -0
  180. package/ts/resolve-once.test.d.ts.map +1 -0
  181. package/ts/resolve-once.test.js +283 -0
  182. package/ts/resolve-once.test.js.map +1 -0
  183. package/ts/result.d.ts +34 -0
  184. package/ts/result.d.ts.map +1 -0
  185. package/ts/result.js +85 -0
  186. package/ts/result.js.map +1 -0
  187. package/ts/result.test.d.ts +2 -0
  188. package/ts/result.test.d.ts.map +1 -0
  189. package/ts/result.test.js +79 -0
  190. package/ts/result.test.js.map +1 -0
  191. package/ts/runtime.d.ts +8 -0
  192. package/ts/runtime.d.ts.map +1 -0
  193. package/ts/runtime.js +26 -0
  194. package/ts/runtime.js.map +1 -0
  195. package/ts/sys-abstraction.d.ts +36 -0
  196. package/ts/sys-abstraction.d.ts.map +1 -0
  197. package/ts/sys-abstraction.js +31 -0
  198. package/ts/sys-abstraction.js.map +1 -0
  199. package/ts/sys-env.d.ts +48 -0
  200. package/ts/sys-env.d.ts.map +1 -0
  201. package/ts/sys-env.js +176 -0
  202. package/ts/sys-env.js.map +1 -0
  203. package/ts/sys-env.test.d.ts +2 -0
  204. package/ts/sys-env.test.d.ts.map +1 -0
  205. package/ts/sys-env.test.js +51 -0
  206. package/ts/sys-env.test.js.map +1 -0
  207. package/ts/test/log-write-stream.d.ts +27 -0
  208. package/ts/test/log-write-stream.d.ts.map +1 -0
  209. package/ts/test/log-write-stream.js +74 -0
  210. package/ts/test/log-write-stream.js.map +1 -0
  211. package/ts/test/mock-logger.d.ts +14 -0
  212. package/ts/test/mock-logger.d.ts.map +1 -0
  213. package/ts/test/mock-logger.js +29 -0
  214. package/ts/test/mock-logger.js.map +1 -0
  215. package/ts/test/mock-logger.test.d.ts +2 -0
  216. package/ts/test/mock-logger.test.d.ts.map +1 -0
  217. package/ts/test/mock-logger.test.js +63 -0
  218. package/ts/test/mock-logger.test.js.map +1 -0
  219. package/ts/test/test-exit-handler.d.ts +2 -0
  220. package/ts/test/test-exit-handler.d.ts.map +1 -0
  221. package/ts/test/test-exit-handler.js +57 -0
  222. package/ts/test/test-exit-handler.js.map +1 -0
  223. package/ts/time.d.ts +13 -0
  224. package/ts/time.d.ts.map +1 -0
  225. package/ts/time.js +14 -0
  226. package/ts/time.js.map +1 -0
  227. package/ts/tracer.d.ts +59 -0
  228. package/ts/tracer.d.ts.map +1 -0
  229. package/ts/tracer.js +148 -0
  230. package/ts/tracer.js.map +1 -0
  231. package/ts/tracer.test.d.ts +2 -0
  232. package/ts/tracer.test.d.ts.map +1 -0
  233. package/ts/tracer.test.js +311 -0
  234. package/ts/tracer.test.js.map +1 -0
  235. package/ts/txt-en-decoder.d.ts +10 -0
  236. package/ts/txt-en-decoder.d.ts.map +1 -0
  237. package/ts/txt-en-decoder.js +15 -0
  238. package/ts/txt-en-decoder.js.map +1 -0
  239. package/ts/uri.d.ts +67 -0
  240. package/ts/uri.d.ts.map +1 -0
  241. package/ts/uri.js +283 -0
  242. package/ts/uri.js.map +1 -0
  243. package/ts/uri.test.d.ts +2 -0
  244. package/ts/uri.test.d.ts.map +1 -0
  245. package/ts/uri.test.js +119 -0
  246. package/ts/uri.test.js.map +1 -0
  247. package/ts/utils/console-write-stream.d.ts +21 -0
  248. package/ts/utils/console-write-stream.d.ts.map +1 -0
  249. package/ts/utils/console-write-stream.js +62 -0
  250. package/ts/utils/console-write-stream.js.map +1 -0
  251. package/ts/utils/fanout-write-stream.d.ts +12 -0
  252. package/ts/utils/fanout-write-stream.d.ts.map +1 -0
  253. package/ts/utils/fanout-write-stream.js +24 -0
  254. package/ts/utils/fanout-write-stream.js.map +1 -0
  255. package/ts/utils/index.d.ts +7 -0
  256. package/ts/utils/index.d.ts.map +1 -0
  257. package/ts/utils/index.js +7 -0
  258. package/ts/utils/index.js.map +1 -0
  259. package/ts/utils/rebuffer.d.ts +3 -0
  260. package/ts/utils/rebuffer.d.ts.map +1 -0
  261. package/ts/utils/rebuffer.js +60 -0
  262. package/ts/utils/rebuffer.js.map +1 -0
  263. package/ts/utils/rebuffer.test.d.ts +2 -0
  264. package/ts/utils/rebuffer.test.d.ts.map +1 -0
  265. package/ts/utils/rebuffer.test.js +77 -0
  266. package/ts/utils/rebuffer.test.js.map +1 -0
  267. package/ts/utils/stream-map.d.ts +9 -0
  268. package/ts/utils/stream-map.d.ts.map +1 -0
  269. package/ts/utils/stream-map.js +62 -0
  270. package/ts/utils/stream-map.js.map +1 -0
  271. package/ts/utils/stream-map.test.d.ts +2 -0
  272. package/ts/utils/stream-map.test.d.ts.map +1 -0
  273. package/ts/utils/stream-map.test.js +87 -0
  274. package/ts/utils/stream-map.test.js.map +1 -0
  275. package/ts/utils/stream-test-helper.d.ts +17 -0
  276. package/ts/utils/stream-test-helper.d.ts.map +1 -0
  277. package/ts/utils/stream-test-helper.js +37 -0
  278. package/ts/utils/stream-test-helper.js.map +1 -0
  279. package/ts/utils/stream2string.d.ts +3 -0
  280. package/ts/utils/stream2string.d.ts.map +1 -0
  281. package/ts/utils/stream2string.js +48 -0
  282. package/ts/utils/stream2string.js.map +1 -0
  283. package/ts/utils/stream2string.test.d.ts +2 -0
  284. package/ts/utils/stream2string.test.d.ts.map +1 -0
  285. package/ts/utils/stream2string.test.js +29 -0
  286. package/ts/utils/stream2string.test.js.map +1 -0
  287. package/ts/utils/string2stream.d.ts +4 -0
  288. package/ts/utils/string2stream.d.ts.map +1 -0
  289. package/ts/utils/string2stream.js +13 -0
  290. package/ts/utils/string2stream.js.map +1 -0
  291. package/ts/utils/string2stream.test.d.ts +2 -0
  292. package/ts/utils/string2stream.test.d.ts.map +1 -0
  293. package/ts/utils/string2stream.test.js +6 -0
  294. package/ts/utils/string2stream.test.js.map +1 -0
  295. package/ts/version.d.ts +2 -0
  296. package/ts/version.d.ts.map +1 -0
  297. package/ts/version.js +4 -0
  298. package/ts/version.js.map +1 -0
  299. package/ts/web/index.d.ts +2 -0
  300. package/ts/web/index.d.ts.map +1 -0
  301. package/ts/web/index.js +2 -0
  302. package/ts/web/index.js.map +1 -0
  303. package/ts/web/web-sys-abstraction.d.ts +4 -0
  304. package/ts/web/web-sys-abstraction.d.ts.map +1 -0
  305. package/ts/web/web-sys-abstraction.js +64 -0
  306. package/ts/web/web-sys-abstraction.js.map +1 -0
  307. package/txt-en-decoder-CZYJUju2.d.cts +11 -0
  308. package/txt-en-decoder-CZYJUju2.d.ts +11 -0
  309. package/utils/index.cjs +341 -0
  310. package/utils/index.cjs.map +1 -0
  311. package/utils/index.d.cts +2 -0
  312. package/utils/index.d.ts +2 -0
  313. package/utils/index.js +32 -0
  314. package/utils/index.js.map +1 -0
  315. package/web/index.cjs +593 -0
  316. package/web/index.cjs.map +1 -0
  317. package/web/index.d.cts +6 -0
  318. package/web/index.d.ts +6 -0
  319. package/web/index.js +9 -0
  320. package/web/index.js.map +1 -0
@@ -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
+ }