@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,314 @@
1
+ import { Time } from "./time.js";
2
+ import { WebSysAbstraction } from "./web/index.js";
3
+ import { TimeMode } from "./sys-abstraction.js";
4
+ import { TraceNode } from "./tracer.js";
5
+ import { MockLogger } from "./test/mock-logger.js";
6
+
7
+ describe("trace", () => {
8
+ let time: Time;
9
+ let refTime: Time;
10
+ let trace: TraceNode;
11
+ const logger = MockLogger().logger.With().Module("trace").Str("value", "important").Logger();
12
+ beforeEach(() => {
13
+ time = WebSysAbstraction({ TimeMode: TimeMode.STEP }).Time();
14
+ trace = TraceNode.root(time, logger);
15
+ refTime = WebSysAbstraction({ TimeMode: TimeMode.STEP }).Time();
16
+ });
17
+ it("a simple trace", () => {
18
+ expect(
19
+ trace.span("test", (trace) => {
20
+ const r1 = trace.span("test.1", () => {
21
+ return 1;
22
+ }) as number;
23
+ const r2 = trace.span("test.2", () => {
24
+ return 1;
25
+ }) as number;
26
+ return r1 + r2;
27
+ }),
28
+ ).toBe(2);
29
+ const childs = Array.from(trace.childs.values());
30
+ expect(childs.map((v) => v.invokes())).toEqual([
31
+ {
32
+ ctx: {
33
+ module: "trace",
34
+ spanId: "test",
35
+ value: "important",
36
+ },
37
+ invokations: [
38
+ {
39
+ start: refTime.Now().getTime(),
40
+ result: "success",
41
+ end: refTime.Now(5).getTime(),
42
+ },
43
+ ],
44
+ },
45
+ ]);
46
+ const layered = Array.from(trace.childs.get("test")?.childs.values() || []);
47
+ refTime = WebSysAbstraction({ TimeMode: TimeMode.STEP }).Time();
48
+ expect(layered.map((v) => v.invokes())).toEqual([
49
+ {
50
+ ctx: {
51
+ module: "trace",
52
+ spanId: "test.1",
53
+ value: "important",
54
+ },
55
+ invokations: [
56
+ {
57
+ start: refTime.Now(2).getTime(),
58
+ result: "success",
59
+ end: refTime.Now().getTime(),
60
+ },
61
+ ],
62
+ },
63
+ {
64
+ ctx: {
65
+ module: "trace",
66
+ spanId: "test.2",
67
+ value: "important",
68
+ },
69
+ invokations: [
70
+ {
71
+ start: refTime.Now().getTime(),
72
+ result: "success",
73
+ end: refTime.Now(1).getTime(),
74
+ },
75
+ ],
76
+ },
77
+ ]);
78
+ });
79
+
80
+ it("a async simple trace", async () => {
81
+ const log = trace.ctx.logger?.With().Str("value", "test").Logger();
82
+ const ret = await trace.span(trace.ctxWith("test", log), async (trace) => {
83
+ const r1 = trace.span(trace.ctxWith("test.1"), () => 1) as number;
84
+ const log2 = trace.ctx.logger?.With().Module("xxx").Str("r2", "test.2").Logger();
85
+ const r2 = (await trace.span(trace.ctxWith("test.2", log2), async () => {
86
+ time.Now();
87
+ await new Promise<void>((resolve) =>
88
+ setTimeout(() => {
89
+ time.Now();
90
+ time.Now();
91
+ resolve();
92
+ }, 100),
93
+ );
94
+ return 1;
95
+ })) as number;
96
+ return r1 + r2;
97
+ });
98
+ expect(ret).toBe(2);
99
+ const childs = Array.from(trace.childs.values());
100
+ const exp = childs.map((v) => v.invokes());
101
+ expect(exp).toEqual([
102
+ {
103
+ ctx: {
104
+ module: "trace",
105
+ spanId: "test",
106
+ value: "test",
107
+ },
108
+ invokations: [
109
+ {
110
+ start: refTime.Now().getTime(),
111
+ result: "success",
112
+ end: refTime.Now(8).getTime(),
113
+ },
114
+ ],
115
+ },
116
+ ]);
117
+ const layered = Array.from(trace.childs.get("test")?.childs.values() || []);
118
+ expect(layered.map((v) => v.invokes())).toEqual([
119
+ {
120
+ ctx: {
121
+ module: "trace",
122
+ spanId: "test.1",
123
+ value: "test",
124
+ },
125
+ invokations: [
126
+ {
127
+ result: "success",
128
+ start: refTime.Now(-2).getTime(),
129
+ end: refTime.Now().getTime(),
130
+ },
131
+ ],
132
+ },
133
+ {
134
+ ctx: {
135
+ module: "xxx",
136
+ r2: "test.2",
137
+ spanId: "test.2",
138
+ value: "test",
139
+ },
140
+ invokations: [
141
+ {
142
+ start: refTime.Now().getTime(),
143
+ end: refTime.Now(4).getTime(),
144
+ result: "success",
145
+ },
146
+ ],
147
+ },
148
+ ]);
149
+ });
150
+
151
+ it("a async exception trace", async () => {
152
+ const ret = await trace.span("test", async (trace) => {
153
+ let r1 = 0;
154
+ let r2 = 0;
155
+ for (let i = 0; i < 3; i++) {
156
+ try {
157
+ r1 += trace.span("test.1", (trace) => {
158
+ if (i % 2) {
159
+ throw new Error("test.1");
160
+ }
161
+ trace.metrics.get("i.1").add([i]);
162
+ return 1;
163
+ }) as number;
164
+ } catch (e) {
165
+ if (i % 2) {
166
+ expect((e as Error).message).toEqual("test.1");
167
+ } else {
168
+ assert(false, "should not happen");
169
+ }
170
+ }
171
+ try {
172
+ r2 += await trace.span("test.2", async (trace) => {
173
+ time.Now();
174
+ await new Promise<void>((resolve, reject) =>
175
+ setTimeout(() => {
176
+ time.Now();
177
+ time.Now();
178
+ if (i % 2) {
179
+ trace.metrics.get("i.2").add(i);
180
+ resolve();
181
+ } else {
182
+ reject("test.2");
183
+ }
184
+ }, 10),
185
+ );
186
+ return 1;
187
+ });
188
+ } catch (e) {
189
+ if (i % 2) {
190
+ assert(false, "should not happen");
191
+ } else {
192
+ expect(e).toEqual("test.2");
193
+ }
194
+ }
195
+ }
196
+ return r1 + r2;
197
+ });
198
+ expect(ret).toBe(3);
199
+ expect(trace.metrics.toJSON()).toEqual({
200
+ "/test/test.1/i.1": [0, 2],
201
+ "/test/test.2/i.2": 1,
202
+ });
203
+ const childs = Array.from(trace.childs.values());
204
+ const exp = childs.map((v) => v.invokes());
205
+ expect(exp).toEqual([
206
+ {
207
+ ctx: {
208
+ module: "trace",
209
+ spanId: "test",
210
+ value: "important",
211
+ },
212
+ invokations: [
213
+ {
214
+ start: refTime.Now(1).getTime(),
215
+ end: refTime.Now(22).getTime(),
216
+ result: "success",
217
+ },
218
+ ],
219
+ },
220
+ ]);
221
+ const layered = Array.from(trace.childs.get("test")?.childs.values() || []);
222
+ expect(layered.map((v) => v.invokes())).toEqual([
223
+ {
224
+ ctx: {
225
+ module: "trace",
226
+ spanId: "test.1",
227
+ value: "important",
228
+ },
229
+ invokations: [
230
+ {
231
+ start: refTime.Now(-2).getTime(),
232
+ end: refTime.Now().getTime(),
233
+ result: "success",
234
+ },
235
+ {
236
+ start: refTime.Now(-9).getTime(),
237
+ end: refTime.Now().getTime(),
238
+ result: "error",
239
+ },
240
+ {
241
+ start: refTime.Now(-16).getTime(),
242
+ end: refTime.Now().getTime(),
243
+ result: "success",
244
+ },
245
+ ],
246
+ metricRefs: {
247
+ "/test/test.1/i.1": [0, 2],
248
+ },
249
+ },
250
+ {
251
+ ctx: {
252
+ module: "trace",
253
+ spanId: "test.2",
254
+ value: "important",
255
+ },
256
+ invokations: [
257
+ {
258
+ start: refTime.Now(-4).getTime(),
259
+ end: refTime.Now(4).getTime(),
260
+ result: "error",
261
+ },
262
+ {
263
+ start: refTime.Now(-11).getTime(),
264
+ end: refTime.Now(4).getTime(),
265
+ result: "success",
266
+ },
267
+ {
268
+ start: refTime.Now(-18).getTime(),
269
+ end: refTime.Now(4).getTime(),
270
+ result: "error",
271
+ },
272
+ ],
273
+ metricRefs: {
274
+ "/test/test.2/i.2": 1,
275
+ },
276
+ },
277
+ ]);
278
+ });
279
+ });
280
+
281
+ describe("metrics", () => {
282
+ let time: Time;
283
+ let trace: TraceNode;
284
+ // const logger = MockLogger().logger.With().Module("trace").Str("value", "important").Logger()
285
+ beforeEach(() => {
286
+ time = WebSysAbstraction({ TimeMode: TimeMode.STEP }).Time();
287
+ trace = TraceNode.root(time);
288
+ });
289
+
290
+ it("a simple metrics", () => {
291
+ ["/test", "test", "/test/wurst", "bla"].forEach((path) => {
292
+ const abs = path.startsWith("/") ? path : "/" + path;
293
+ expect(trace.metrics.get(path).path).toBe(abs);
294
+ expect(trace.metrics.get(path).value).toBeFalsy();
295
+ trace.metrics.get(path).add(4711);
296
+ expect(trace.metrics.get(path).value).toBe(4711);
297
+ trace.metrics.get(path).set(undefined);
298
+ });
299
+ });
300
+ it("create metrics path", () => {
301
+ trace.span("test", (trace) => {
302
+ trace.span("test.1", (trace) => {
303
+ trace.metrics.get("m1.1").add(1);
304
+ trace.metrics.get("/test/test.1/m1.1").add(1);
305
+ expect(trace.metrics.get("m1.1").path).toBe("/test/test.1/m1.1");
306
+ expect(trace.metrics.get("m1.1").value).toBe(2);
307
+ });
308
+ });
309
+ });
310
+ it("typed span promise or literal", async () => {
311
+ expect(trace.span("test", () => "1")).toBe("1");
312
+ expect(await trace.span("test", async () => 1)).toBe(1);
313
+ });
314
+ });
package/src/tracer.ts ADDED
@@ -0,0 +1,222 @@
1
+ import type { MarkWritable } from "ts-essentials";
2
+ import { Time } from "./time.js";
3
+ import { Logger } from "./logger.js";
4
+
5
+ export type TraceCtx = {
6
+ readonly spanId: string;
7
+ readonly time: Time;
8
+ readonly parent: TraceNode;
9
+ readonly metrics: Map<string, Metric<unknown>>;
10
+ readonly logger?: Logger;
11
+ } & Record<string, unknown>;
12
+
13
+ export type CleanCtx = {
14
+ readonly spanId: string;
15
+ } & Record<string, unknown>;
16
+
17
+ export type TraceCtxParam = {
18
+ readonly spanId: string;
19
+ } & Partial<{
20
+ readonly time: Time;
21
+ readonly parent: TraceNode;
22
+ readonly logger: Logger;
23
+ }> &
24
+ Record<string, unknown>;
25
+
26
+ export class Metric<T> {
27
+ value?: T;
28
+ readonly path: string;
29
+
30
+ constructor(path: string) {
31
+ this.path = path;
32
+ }
33
+
34
+ set(value: T): void {
35
+ this.value = value;
36
+ }
37
+
38
+ add<R extends number | ArrayLike<T>>(value: R): void {
39
+ if (typeof value === "number") {
40
+ if (this.value === undefined) {
41
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
42
+ this.value = 0 as any;
43
+ }
44
+ (this.value as number) = ((this.value as number) + value) as number;
45
+ } else if (Array.isArray(value)) {
46
+ if (!Array.isArray(this.value)) {
47
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
48
+ this.value = [] as any;
49
+ }
50
+ (this.value as T[]).push(...value);
51
+ } else {
52
+ throw new Error("add only support number or array");
53
+ }
54
+ }
55
+ }
56
+
57
+ export type MetricMap = Map<string, Metric<unknown>>;
58
+
59
+ export class Metrics {
60
+ readonly tracenode: TraceNode;
61
+ private readonly map: MetricMap;
62
+
63
+ readonly spanRefs: MetricMap = new Map<string, Metric<unknown>>();
64
+ constructor(tracenode: TraceNode) {
65
+ this.tracenode = tracenode;
66
+ this.map = tracenode.ctx.metrics;
67
+ }
68
+
69
+ toJSON(): Record<string, unknown> {
70
+ const obj: Record<string, unknown> = {};
71
+ for (const [key, value] of this.map) {
72
+ obj[key] = value.value;
73
+ }
74
+ return obj;
75
+ }
76
+
77
+ get<T>(ipath: string): Metric<T> {
78
+ const path = ipath.replace(/[/]+/g, "/").trim();
79
+ if (path.startsWith("/")) {
80
+ if (path.slice(1).length === 0) {
81
+ throw new Error(`Metrics path must contain value /:${path}`);
82
+ }
83
+ let metric = this.map.get(path);
84
+ if (!metric) {
85
+ metric = new Metric<T>(path);
86
+ this.map.set(path, metric);
87
+ }
88
+ this.spanRefs.set(path, metric);
89
+ return metric as Metric<T>;
90
+ } else if (path.includes("/")) {
91
+ throw new Error(`Metrics path must start with /:${path}`);
92
+ }
93
+ const rootPath = this.tracenode.getRootPath();
94
+ return this.get(`${rootPath}/${path}`);
95
+ }
96
+ }
97
+
98
+ export interface Invokaction {
99
+ readonly result: "success" | "error";
100
+ readonly start: number;
101
+ readonly end: number;
102
+ readonly metrics?: Metrics;
103
+ }
104
+
105
+ export type TraceNodeMap = Map<string, TraceNode>;
106
+
107
+ export class TraceNode {
108
+ readonly childs: TraceNodeMap = new Map<string, TraceNode>();
109
+
110
+ readonly invokations: Invokaction[] = [];
111
+
112
+ readonly spanId: string;
113
+ readonly ctx: TraceCtx;
114
+ readonly metrics: Metrics;
115
+
116
+ static root(time: Time, logger?: Logger): TraceNode {
117
+ return new TraceNode({
118
+ spanId: "root",
119
+ time,
120
+ logger,
121
+ metrics: new Map(),
122
+ parent: undefined as unknown as TraceNode,
123
+ });
124
+ }
125
+
126
+ constructor(ctx: TraceCtx) {
127
+ this.spanId = ctx.spanId;
128
+ this.ctx = ctx;
129
+ this.metrics = new Metrics(this);
130
+ }
131
+
132
+ getRootPath(rpath: string[] = []): string {
133
+ if (!this.ctx.parent) {
134
+ return "/" + rpath.reverse().join("/");
135
+ }
136
+ return this.ctx.parent.getRootPath(rpath.concat(this.ctx.spanId));
137
+ }
138
+
139
+ invokes(): { ctx: CleanCtx; invokations: Invokaction[] } {
140
+ const cleanCtx = { ...this.ctx } as CleanCtx;
141
+ delete cleanCtx.parent;
142
+ delete cleanCtx.time;
143
+ delete cleanCtx.logger;
144
+ delete cleanCtx.metrics;
145
+ const spanRefs = this.metrics.toJSON.call({ map: this.metrics.spanRefs });
146
+ const metricsRefs = Object.keys(spanRefs).length > 0 ? { metricRefs: spanRefs } : {};
147
+ return {
148
+ ctx: cleanCtx as CleanCtx,
149
+ invokations: this.invokations,
150
+ ...metricsRefs,
151
+ };
152
+ }
153
+
154
+ ctxWith(spanId: string, logger?: Logger): TraceCtxParam {
155
+ const ctx = {
156
+ ...this.ctx,
157
+ spanId,
158
+ };
159
+ if (logger) {
160
+ ctx.logger = logger;
161
+ }
162
+ return ctx;
163
+ }
164
+
165
+ // <V extends () => Promise<T> | T, T>(id: string, fn: V): ReturnType<V>
166
+ span<V extends (trace: TraceNode) => Promise<T> | T, T>(inSpanId: string | TraceCtxParam, fn: V): ReturnType<V> {
167
+ let ctx: TraceCtx;
168
+ if (typeof inSpanId === "string") {
169
+ ctx = {
170
+ ...this.ctx,
171
+ spanId: inSpanId,
172
+ parent: this,
173
+ };
174
+ } else {
175
+ ctx = {
176
+ ...this.ctx,
177
+ ...inSpanId,
178
+ parent: this,
179
+ };
180
+ }
181
+ if (ctx.logger) {
182
+ ctx = {
183
+ ...ctx,
184
+ ...ctx.logger.Attributes(),
185
+ };
186
+ }
187
+ const spanId = ctx.spanId;
188
+ let spanTrace = this.childs.get(spanId);
189
+ if (!spanTrace) {
190
+ spanTrace = new TraceNode(ctx);
191
+ this.childs.set(spanId.toString(), spanTrace);
192
+ }
193
+ const invokation: MarkWritable<MarkWritable<Invokaction, "end">, "result"> = {
194
+ start: this.ctx.time.Now().getTime(),
195
+ end: 0,
196
+ result: "success",
197
+ };
198
+ spanTrace.invokations.push(invokation);
199
+ try {
200
+ const possiblePromise = fn(spanTrace);
201
+ if (possiblePromise instanceof Promise) {
202
+ return possiblePromise
203
+ .then((v) => {
204
+ return v;
205
+ })
206
+ .catch((e) => {
207
+ invokation.result = "error";
208
+ throw e;
209
+ })
210
+ .finally(() => {
211
+ invokation.end = this.ctx.time.Now().getTime();
212
+ }) as ReturnType<V>;
213
+ }
214
+ invokation.end = this.ctx.time.Now().getTime();
215
+ return possiblePromise as ReturnType<V>;
216
+ } catch (e) {
217
+ invokation.result = "error";
218
+ invokation.end = this.ctx.time.Now().getTime();
219
+ throw e;
220
+ }
221
+ }
222
+ }
@@ -0,0 +1,21 @@
1
+ export interface TxtEnDecoder {
2
+ encode(str: string): Uint8Array;
3
+ decode(data: Uint8Array): string;
4
+ }
5
+
6
+ const encoder = new TextEncoder();
7
+ const decoder = new TextDecoder();
8
+
9
+ export class Utf8EnDecoder implements TxtEnDecoder {
10
+ encode(str: string): Uint8Array {
11
+ return encoder.encode(str);
12
+ }
13
+ decode(data: Uint8Array): string {
14
+ return decoder.decode(data);
15
+ }
16
+ }
17
+
18
+ const utf8EnDecoder = new Utf8EnDecoder();
19
+ export function Utf8EnDecoderSingleton(): TxtEnDecoder {
20
+ return utf8EnDecoder;
21
+ }
@@ -0,0 +1,155 @@
1
+ import { BuildURI, MutableURL, URI } from "@adviser/cement";
2
+
3
+ describe("BuildURI", () => {
4
+ let uri: BuildURI;
5
+ beforeEach(() => {
6
+ uri = BuildURI.from(new MutableURL("http://example.com"));
7
+ uri.hostname("example");
8
+ uri.setParam("key", "value");
9
+ });
10
+
11
+ it("toString", () => {
12
+ expect(uri.toString()).toBe("http://example/?key=value");
13
+ });
14
+
15
+ it("build", () => {
16
+ expect(uri.URI().toString()).toBe("http://example/?key=value");
17
+ });
18
+
19
+ it("defParam", () => {
20
+ uri.defParam("key", "value2");
21
+ uri.defParam("key2", "value2");
22
+ expect(uri.toString()).toBe("http://example/?key=value&key2=value2");
23
+ });
24
+
25
+ it("searchParams sorted in toString", () => {
26
+ uri.setParam("z", "value");
27
+ uri.setParam("a", "value");
28
+ uri.setParam("m", "value");
29
+ expect(uri.toString()).toBe("http://example/?a=value&key=value&m=value&z=value");
30
+ });
31
+ });
32
+
33
+ describe("URI", () => {
34
+ // static from(strURLUri: string | URL | URI | NullOrUndef, defaultProtocol = "file:"): URI {
35
+ it("from str with default", () => {
36
+ const uri = URI.from("/example/wurst");
37
+ expect(uri.toString()).toBe("file:///example/wurst");
38
+ });
39
+ // it("from str defect", () => {
40
+ // expect(URI.from("doof:meno")).toBe(null);
41
+ // })
42
+ it("from URL str", () => {
43
+ expect(URI.from("bla://example/com?key=value").toString()).toBe("bla://example/com?key=value");
44
+ });
45
+ it("from URL", () => {
46
+ expect(URI.from(new MutableURL("blix://example.com?key=value")).toString()).toBe("blix://example.com?key=value");
47
+ });
48
+ it("from URI", () => {
49
+ expect(URI.from(URI.from("blix://example.com?key=value")).toString()).toBe("blix://example.com?key=value");
50
+ });
51
+ it("from undef", () => {
52
+ expect(URI.from(null).toString()).toBe("file:///");
53
+ });
54
+
55
+ it("build", () => {
56
+ expect(URI.from("blix://example.com?key=value").build().toString()).toBe("blix://example.com?key=value");
57
+ });
58
+
59
+ it("clone", () => {
60
+ expect(URI.from("blix://example.com?key=value").clone().toString()).toBe("blix://example.com?key=value");
61
+ });
62
+
63
+ it("asURL", () => {
64
+ expect(URI.from("blix://example.com?key=value").asURL().toString()).toBe("blix://example.com?key=value");
65
+ });
66
+
67
+ it("toString", () => {
68
+ expect(URI.from("blix://example.com?key=value").toString()).toBe("blix://example.com?key=value");
69
+ });
70
+
71
+ it("searchParams sorted in toString", () => {
72
+ expect(URI.from("blix://example.com?z=value&a=value&m=value").toString()).toBe("blix://example.com?a=value&m=value&z=value");
73
+ });
74
+ it("searchParams sorted in asURL", () => {
75
+ expect(URI.from("blix://example.com?z=value&a=value&m=value").asURL().toString()).toBe(
76
+ "blix://example.com?a=value&m=value&z=value",
77
+ );
78
+ });
79
+
80
+ it("merge", () => {
81
+ expect(URI.merge("blix://example.com?key=value&into=4", "murk://bla/com?key=from&z=value").toString()).toBe(
82
+ "murk://bla/com?into=4&key=from&z=value",
83
+ );
84
+ });
85
+
86
+ it("merge empty", () => {
87
+ expect(URI.merge("blix://example/com?key=value&into=4", "murk://?key=from&z=value").toString()).toBe(
88
+ "murk://example/com?into=4&key=from&z=value",
89
+ );
90
+ });
91
+
92
+ it("firefox file relative into", () => {
93
+ expect(URI.from("file://./dist/tests/key.bag").toString()).toBe("file://./dist/tests/key.bag");
94
+ });
95
+
96
+ it("from empty", () => {
97
+ expect(URI.merge(`file://./dist/tests/key.bag`, "").toString()).toBe("file://./dist/tests/key.bag");
98
+ });
99
+
100
+ it("merge thing about", () => {
101
+ const result = URI.merge("./dist/what?byKey=4444", "murk://bla.com?key=from&z=value");
102
+ expect(result.toString()).toBe("murk://bla.com?byKey=4444&key=from&z=value");
103
+ });
104
+
105
+ it("isURI real", () => {
106
+ expect(URI.is(URI.from())).toBe(true);
107
+ });
108
+ it("isURI fake", () => {
109
+ expect(
110
+ URI.is({
111
+ asURL: () => new URL("http://example.com"),
112
+ hasParam: () => false,
113
+ getParam: () => "",
114
+ }),
115
+ ).toBe(true);
116
+ });
117
+
118
+ it("safari has a different pathname behavior", () => {
119
+ // chrome -> new URL("indexdb://fp/?name=test&store=meta").pathname -> //fp/
120
+ // safari -> new URL("indexdb://fp/?name=test&store=meta").pathname -> /
121
+ const uri = URI.from("indexdb://fp/?name=test&store=meta");
122
+ expect(uri.pathname).toBe("fp/");
123
+ });
124
+
125
+ it("passing URL to fetch", async () => {
126
+ const uri = URI.from("https://jsonplaceholder.typicode.com/todos/1");
127
+ const res = await fetch(uri.asURL());
128
+ expect(res.status).toBeGreaterThan(199);
129
+ });
130
+
131
+ it("MutableURL is instance of URL", () => {
132
+ expect(new MutableURL("http://example.com") instanceof URL).toBe(true);
133
+ });
134
+
135
+ it("file url", () => {
136
+ const uri = URI.from("file://fp?storagekey=zTvTPEPQRWij8rfb3FrFqBm");
137
+ expect(uri.pathname).toBe("fp");
138
+ });
139
+
140
+ it("unregistered protocol with hostPart", () => {
141
+ const withoutHostpart = URI.from("indexdb://fp:bla/test/?name=test&store=meta");
142
+ expect(() => withoutHostpart.hostname).toThrowError('you can use hostname only if protocol is ["http","https","ws","wss"]');
143
+ });
144
+
145
+ it("register protocol with hostPart", () => {
146
+ const unreg = URI.protocolHasHostpart("indexdb:");
147
+ const withHostpart = URI.from("indexdb://fp1:88/test/wurst?name=test&store=meta");
148
+ expect(withHostpart.host).toBe("fp1:88");
149
+ expect(withHostpart.pathname).toBe("/test/wurst");
150
+ const withHostpartNoPath = URI.from("indexdb://fp2:88?name=test&store=meta");
151
+ expect(withHostpartNoPath.host).toBe("fp2:88");
152
+ expect(withHostpartNoPath.pathname).toBe("/");
153
+ unreg();
154
+ });
155
+ });