nodejs-map 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (238) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +95 -2
  3. package/dist/app/assets/LearnView-Cqz9Ux8w.js +3 -0
  4. package/dist/app/assets/LearnView-Cqz9Ux8w.js.map +1 -0
  5. package/dist/app/assets/LearnView-Nrw6YQU8.css +1 -0
  6. package/dist/app/assets/app.worker-DZKurLgd.js +8 -0
  7. package/dist/app/assets/app.worker-DZKurLgd.js.map +1 -0
  8. package/dist/app/assets/atlas.worker-DGHewGIv.js +2 -0
  9. package/dist/app/assets/atlas.worker-DGHewGIv.js.map +1 -0
  10. package/dist/app/assets/cm-BTWjV_Gb.js +13 -0
  11. package/dist/app/assets/cm-BTWjV_Gb.js.map +1 -0
  12. package/dist/app/assets/dist-B37Ex44M.js +2 -0
  13. package/dist/app/assets/dist-B37Ex44M.js.map +1 -0
  14. package/dist/app/assets/dist-Dv4HNg5q.js +2 -0
  15. package/dist/app/assets/dist-Dv4HNg5q.js.map +1 -0
  16. package/dist/app/assets/index-BRoted4h.css +1 -0
  17. package/dist/app/assets/index-eLItdROg.js +18 -0
  18. package/dist/app/assets/index-eLItdROg.js.map +1 -0
  19. package/dist/app/assets/map-CISW7_cc.css +1 -0
  20. package/dist/app/assets/map-mCCk9DTc.js +132 -0
  21. package/dist/app/assets/map-mCCk9DTc.js.map +1 -0
  22. package/dist/app/assets/sdf-label-font-1nAa0TP8.png +0 -0
  23. package/dist/app/config.json +5 -0
  24. package/dist/app/favicon.svg +1 -0
  25. package/dist/app/index.html +21 -0
  26. package/dist/atlas/v26.13.1/build-report.json +178 -0
  27. package/dist/atlas/v26.13.1/chunks/lib.bootstrap.0.json +1 -0
  28. package/dist/atlas/v26.13.1/chunks/lib.core.0.json +1 -0
  29. package/dist/atlas/v26.13.1/chunks/lib.crypto.0.json +1 -0
  30. package/dist/atlas/v26.13.1/chunks/lib.devtools.0.json +1 -0
  31. package/dist/atlas/v26.13.1/chunks/lib.events.0.json +1 -0
  32. package/dist/atlas/v26.13.1/chunks/lib.fs.0.json +1 -0
  33. package/dist/atlas/v26.13.1/chunks/lib.modules.0.json +1 -0
  34. package/dist/atlas/v26.13.1/chunks/lib.net.0.json +1 -0
  35. package/dist/atlas/v26.13.1/chunks/lib.net.1.json +1 -0
  36. package/dist/atlas/v26.13.1/chunks/lib.platform.0.json +1 -0
  37. package/dist/atlas/v26.13.1/chunks/lib.process.0.json +1 -0
  38. package/dist/atlas/v26.13.1/chunks/lib.rest.0.json +1 -0
  39. package/dist/atlas/v26.13.1/chunks/lib.streams.0.json +1 -0
  40. package/dist/atlas/v26.13.1/chunks/lib.timers.0.json +1 -0
  41. package/dist/atlas/v26.13.1/chunks/lib.workers.0.json +1 -0
  42. package/dist/atlas/v26.13.1/chunks/lib.zlib.0.json +1 -0
  43. package/dist/atlas/v26.13.1/chunks/src.callbacks.0.json +1 -0
  44. package/dist/atlas/v26.13.1/chunks/src.crypto.0.json +1 -0
  45. package/dist/atlas/v26.13.1/chunks/src.diag.0.json +1 -0
  46. package/dist/atlas/v26.13.1/chunks/src.fs.0.json +1 -0
  47. package/dist/atlas/v26.13.1/chunks/src.inspector.0.json +1 -0
  48. package/dist/atlas/v26.13.1/chunks/src.loop.0.json +1 -0
  49. package/dist/atlas/v26.13.1/chunks/src.modules.0.json +1 -0
  50. package/dist/atlas/v26.13.1/chunks/src.napi.0.json +1 -0
  51. package/dist/atlas/v26.13.1/chunks/src.net.0.json +1 -0
  52. package/dist/atlas/v26.13.1/chunks/src.net.1.json +1 -0
  53. package/dist/atlas/v26.13.1/chunks/src.process.0.json +1 -0
  54. package/dist/atlas/v26.13.1/chunks/src.rest.0.json +1 -0
  55. package/dist/atlas/v26.13.1/chunks/src.support.0.json +1 -0
  56. package/dist/atlas/v26.13.1/chunks/src.zlib.0.json +1 -0
  57. package/dist/atlas/v26.13.1/chunks/uv.api.0.json +1 -0
  58. package/dist/atlas/v26.13.1/chunks/uv.backends.0.json +1 -0
  59. package/dist/atlas/v26.13.1/chunks/uv.fs.0.json +1 -0
  60. package/dist/atlas/v26.13.1/chunks/uv.loop.0.json +1 -0
  61. package/dist/atlas/v26.13.1/chunks/uv.net.0.json +1 -0
  62. package/dist/atlas/v26.13.1/chunks/uv.rest.0.json +1 -0
  63. package/dist/atlas/v26.13.1/chunks/uv.threadpool.0.json +1 -0
  64. package/dist/atlas/v26.13.1/chunks/v8.api.0.json +1 -0
  65. package/dist/atlas/v26.13.1/chunks/v8.asmjs.0.json +1 -0
  66. package/dist/atlas/v26.13.1/chunks/v8.ast.0.json +1 -0
  67. package/dist/atlas/v26.13.1/chunks/v8.base.0.json +1 -0
  68. package/dist/atlas/v26.13.1/chunks/v8.baseline.0.json +1 -0
  69. package/dist/atlas/v26.13.1/chunks/v8.bigint.0.json +1 -0
  70. package/dist/atlas/v26.13.1/chunks/v8.builtins.0.json +1 -0
  71. package/dist/atlas/v26.13.1/chunks/v8.codegen.0.json +1 -0
  72. package/dist/atlas/v26.13.1/chunks/v8.codegen.riscv.0.json +1 -0
  73. package/dist/atlas/v26.13.1/chunks/v8.common.0.json +1 -0
  74. package/dist/atlas/v26.13.1/chunks/v8.compiler-dispatcher.0.json +1 -0
  75. package/dist/atlas/v26.13.1/chunks/v8.compiler.0.json +1 -0
  76. package/dist/atlas/v26.13.1/chunks/v8.compiler.backend.0.json +1 -0
  77. package/dist/atlas/v26.13.1/chunks/v8.compiler.turboshaft.0.json +1 -0
  78. package/dist/atlas/v26.13.1/chunks/v8.d8.0.json +1 -0
  79. package/dist/atlas/v26.13.1/chunks/v8.date.0.json +1 -0
  80. package/dist/atlas/v26.13.1/chunks/v8.debug.0.json +1 -0
  81. package/dist/atlas/v26.13.1/chunks/v8.deoptimizer.0.json +1 -0
  82. package/dist/atlas/v26.13.1/chunks/v8.diagnostics.0.json +1 -0
  83. package/dist/atlas/v26.13.1/chunks/v8.dumpling.0.json +1 -0
  84. package/dist/atlas/v26.13.1/chunks/v8.execution.0.json +1 -0
  85. package/dist/atlas/v26.13.1/chunks/v8.extensions.0.json +1 -0
  86. package/dist/atlas/v26.13.1/chunks/v8.flags.0.json +1 -0
  87. package/dist/atlas/v26.13.1/chunks/v8.fuzzilli.0.json +1 -0
  88. package/dist/atlas/v26.13.1/chunks/v8.handles.0.json +1 -0
  89. package/dist/atlas/v26.13.1/chunks/v8.heap.0.json +1 -0
  90. package/dist/atlas/v26.13.1/chunks/v8.heap.cppgc.0.json +1 -0
  91. package/dist/atlas/v26.13.1/chunks/v8.ic.0.json +1 -0
  92. package/dist/atlas/v26.13.1/chunks/v8.init.0.json +1 -0
  93. package/dist/atlas/v26.13.1/chunks/v8.inspector.0.json +1 -0
  94. package/dist/atlas/v26.13.1/chunks/v8.interpreter.0.json +1 -0
  95. package/dist/atlas/v26.13.1/chunks/v8.json.0.json +1 -0
  96. package/dist/atlas/v26.13.1/chunks/v8.libplatform.0.json +1 -0
  97. package/dist/atlas/v26.13.1/chunks/v8.libsampler.0.json +1 -0
  98. package/dist/atlas/v26.13.1/chunks/v8.logging.0.json +1 -0
  99. package/dist/atlas/v26.13.1/chunks/v8.maglev.0.json +1 -0
  100. package/dist/atlas/v26.13.1/chunks/v8.numbers.0.json +1 -0
  101. package/dist/atlas/v26.13.1/chunks/v8.objects.0.json +1 -0
  102. package/dist/atlas/v26.13.1/chunks/v8.parsing.0.json +1 -0
  103. package/dist/atlas/v26.13.1/chunks/v8.profiler.0.json +1 -0
  104. package/dist/atlas/v26.13.1/chunks/v8.regexp.0.json +1 -0
  105. package/dist/atlas/v26.13.1/chunks/v8.root.0.json +1 -0
  106. package/dist/atlas/v26.13.1/chunks/v8.roots.0.json +1 -0
  107. package/dist/atlas/v26.13.1/chunks/v8.runtime.0.json +1 -0
  108. package/dist/atlas/v26.13.1/chunks/v8.sandbox.0.json +1 -0
  109. package/dist/atlas/v26.13.1/chunks/v8.snapshot.0.json +1 -0
  110. package/dist/atlas/v26.13.1/chunks/v8.strings.0.json +1 -0
  111. package/dist/atlas/v26.13.1/chunks/v8.tasks.0.json +1 -0
  112. package/dist/atlas/v26.13.1/chunks/v8.torque.0.json +1 -0
  113. package/dist/atlas/v26.13.1/chunks/v8.tracing.0.json +1 -0
  114. package/dist/atlas/v26.13.1/chunks/v8.trap-handler.0.json +1 -0
  115. package/dist/atlas/v26.13.1/chunks/v8.utils.0.json +1 -0
  116. package/dist/atlas/v26.13.1/chunks/v8.wasm.0.json +1 -0
  117. package/dist/atlas/v26.13.1/chunks/v8.zone.0.json +1 -0
  118. package/dist/atlas/v26.13.1/edges.json +1 -0
  119. package/dist/atlas/v26.13.1/files.json +1 -0
  120. package/dist/atlas/v26.13.1/index.json +1 -0
  121. package/dist/atlas/v26.13.1/search.json +1 -0
  122. package/dist/cli.js +6268 -0
  123. package/dist/examples.json +1 -0
  124. package/dist/learn/async-await.json +1 -0
  125. package/dist/learn/backpressure.json +1 -0
  126. package/dist/learn/big-picture.json +1 -0
  127. package/dist/learn/blocking-the-loop.json +1 -0
  128. package/dist/learn/breakpoints.json +1 -0
  129. package/dist/learn/call-stack.json +1 -0
  130. package/dist/learn/first-run.json +1 -0
  131. package/dist/learn/follow-readfile.json +1 -0
  132. package/dist/learn/four-threads-six-jobs.json +1 -0
  133. package/dist/learn/glossary.json +1 -0
  134. package/dist/learn/hiding-areas.json +1 -0
  135. package/dist/learn/http-one-request.json +1 -0
  136. package/dist/learn/http-two-requests.json +1 -0
  137. package/dist/learn/img/big-picture.webp +0 -0
  138. package/dist/learn/img/first-run-queues.webp +0 -0
  139. package/dist/learn/img/hide-restore-strip.webp +0 -0
  140. package/dist/learn/img/hide-view-menu.webp +0 -0
  141. package/dist/learn/img/map-call-path.webp +0 -0
  142. package/dist/learn/img/marks-in-a-run.webp +0 -0
  143. package/dist/learn/img/paused-at-breakpoint.webp +0 -0
  144. package/dist/learn/img/paused-in-node-fs.webp +0 -0
  145. package/dist/learn/img/playback-bar.webp +0 -0
  146. package/dist/learn/img/save-and-share.webp +0 -0
  147. package/dist/learn/img/tour-areas.webp +0 -0
  148. package/dist/learn/index.json +390 -0
  149. package/dist/learn/keep-alive.json +1 -0
  150. package/dist/learn/loop-phases.json +1 -0
  151. package/dist/learn/many-clients.json +1 -0
  152. package/dist/learn/microtasks-belong-to-v8.json +1 -0
  153. package/dist/learn/next-tick.json +1 -0
  154. package/dist/learn/observed-and-inferred.json +1 -0
  155. package/dist/learn/one-libuv-timer.json +1 -0
  156. package/dist/learn/order-and-esm.json +1 -0
  157. package/dist/learn/pipeline-errors.json +1 -0
  158. package/dist/learn/playback.json +1 -0
  159. package/dist/learn/pool-size.json +1 -0
  160. package/dist/learn/prerequisites.json +1 -0
  161. package/dist/learn/probe-build.json +1 -0
  162. package/dist/learn/promises-and-microtasks.json +1 -0
  163. package/dist/learn/reading-a-file.json +1 -0
  164. package/dist/learn/rec/async-await-chain.nmrec.gz +0 -0
  165. package/dist/learn/rec/backpressure.nmrec.gz +0 -0
  166. package/dist/learn/rec/beforeexit-and-exit.nmrec.gz +0 -0
  167. package/dist/learn/rec/block-loop.nmrec.gz +0 -0
  168. package/dist/learn/rec/dns-lookup-thread-pool.nmrec.gz +0 -0
  169. package/dist/learn/rec/esm-twist.nmrec.gz +0 -0
  170. package/dist/learn/rec/eventemitter-is-synchronous.nmrec.gz +0 -0
  171. package/dist/learn/rec/first-run.nmrec.gz +0 -0
  172. package/dist/learn/rec/fs-promises-await.nmrec.gz +0 -0
  173. package/dist/learn/rec/fs-sync-vs-async.nmrec.gz +0 -0
  174. package/dist/learn/rec/http-blocking-fixed.nmrec.gz +0 -0
  175. package/dist/learn/rec/http-blocking.nmrec.gz +0 -0
  176. package/dist/learn/rec/http-keep-alive.nmrec.gz +0 -0
  177. package/dist/learn/rec/http-roundtrip.nmrec.gz +0 -0
  178. package/dist/learn/rec/http-two-requests.nmrec.gz +0 -0
  179. package/dist/learn/rec/interval-and-clear.nmrec.gz +0 -0
  180. package/dist/learn/rec/nested-ticks-and-microtasks.nmrec.gz +0 -0
  181. package/dist/learn/rec/next-tick.nmrec.gz +0 -0
  182. package/dist/learn/rec/pbkdf2-six-jobs-four-threads.nmrec.gz +0 -0
  183. package/dist/learn/rec/phase-check.nmrec.gz +0 -0
  184. package/dist/learn/rec/phase-close.nmrec.gz +0 -0
  185. package/dist/learn/rec/phase-io.nmrec.gz +0 -0
  186. package/dist/learn/rec/pipeline-error.nmrec.gz +0 -0
  187. package/dist/learn/rec/pool-users.nmrec.gz +0 -0
  188. package/dist/learn/rec/pool-with-two-threads.nmrec.gz +0 -0
  189. package/dist/learn/rec/promise-microtasks.nmrec.gz +0 -0
  190. package/dist/learn/rec/readfile-thread-pool.nmrec.gz +0 -0
  191. package/dist/learn/rec/run-to-completion.nmrec.gz +0 -0
  192. package/dist/learn/rec/split-work.nmrec.gz +0 -0
  193. package/dist/learn/rec/stream-pipeline-file.nmrec.gz +0 -0
  194. package/dist/learn/rec/streams-memory.nmrec.gz +0 -0
  195. package/dist/learn/rec/tcp-many-clients.nmrec.gz +0 -0
  196. package/dist/learn/rec/tcp-round-trip.nmrec.gz +0 -0
  197. package/dist/learn/rec/tick-before-promise.nmrec.gz +0 -0
  198. package/dist/learn/rec/timeout-vs-immediate-io.nmrec.gz +0 -0
  199. package/dist/learn/rec/timeout-vs-immediate-main.nmrec.gz +0 -0
  200. package/dist/learn/rec/timer-lateness.nmrec.gz +0 -0
  201. package/dist/learn/rec/timer-vs-file-read.nmrec.gz +0 -0
  202. package/dist/learn/rec/timers-phase.nmrec.gz +0 -0
  203. package/dist/learn/rec/unref-timer.nmrec.gz +0 -0
  204. package/dist/learn/save-and-share.json +1 -0
  205. package/dist/learn/slow-handler.json +1 -0
  206. package/dist/learn/splitting-work.json +1 -0
  207. package/dist/learn/staying-alive.json +1 -0
  208. package/dist/learn/stepping-into-node.json +1 -0
  209. package/dist/learn/streams-basics.json +1 -0
  210. package/dist/learn/tcp-step-by-step.json +1 -0
  211. package/dist/learn/the-map.json +1 -0
  212. package/dist/learn/three-marks.json +1 -0
  213. package/dist/learn/timeout-vs-immediate.json +1 -0
  214. package/dist/learn/timers-are-not-now.json +1 -0
  215. package/dist/learn/tour.json +1 -0
  216. package/dist/learn/what-it-is.json +1 -0
  217. package/dist/learn/what-uses-the-pool.json +1 -0
  218. package/dist/lessons/async-await-chain.nmrec.gz +0 -0
  219. package/dist/lessons/beforeexit-and-exit.nmrec.gz +0 -0
  220. package/dist/lessons/dns-lookup-thread-pool.nmrec.gz +0 -0
  221. package/dist/lessons/esm-twist.nmrec.gz +0 -0
  222. package/dist/lessons/eventemitter-is-synchronous.nmrec.gz +0 -0
  223. package/dist/lessons/fs-promises-await.nmrec.gz +0 -0
  224. package/dist/lessons/fs-sync-vs-async.nmrec.gz +0 -0
  225. package/dist/lessons/index.json +621 -0
  226. package/dist/lessons/interval-and-clear.nmrec.gz +0 -0
  227. package/dist/lessons/nested-ticks-and-microtasks.nmrec.gz +0 -0
  228. package/dist/lessons/pbkdf2-six-jobs-four-threads.nmrec.gz +0 -0
  229. package/dist/lessons/pool-with-two-threads.nmrec.gz +0 -0
  230. package/dist/lessons/readfile-thread-pool.nmrec.gz +0 -0
  231. package/dist/lessons/tcp-round-trip.nmrec.gz +0 -0
  232. package/dist/lessons/tick-before-promise.nmrec.gz +0 -0
  233. package/dist/lessons/timeout-vs-immediate-io.nmrec.gz +0 -0
  234. package/dist/lessons/timeout-vs-immediate-main.nmrec.gz +0 -0
  235. package/dist/lessons/timer-vs-file-read.nmrec.gz +0 -0
  236. package/dist/lessons/timers-phase.nmrec.gz +0 -0
  237. package/dist/preload.cjs +507 -0
  238. package/package.json +25 -4
@@ -0,0 +1,507 @@
1
+ 'use strict';
2
+ // nodejs-map preload. Loaded into the target with `node -r`.
3
+ //
4
+ // Plain CommonJS on purpose: it runs inside the user's Node, before their
5
+ // program, and must not need a build step or type stripping.
6
+ //
7
+ // What it does: turns async_hooks callbacks, stdout/stderr writes and libuv's
8
+ // loop counters into fixed-size binary records and hands them to the helper
9
+ // over a pipe.
10
+ //
11
+ // Rules it follows:
12
+ // - it adds no handle and no request to the event loop: the only I/O is
13
+ // fs.writeSync on a blocking descriptor;
14
+ // - it creates no timer, promise, nextTick or microtask;
15
+ // - the channel descriptor is parked above the numbers a program normally
16
+ // sees, so the first file the program opens is still descriptor 3;
17
+ // - it does nothing in worker threads;
18
+ // - it removes its own flags and variables from process.execArgv and
19
+ // process.env, so a forked child starts clean;
20
+ // - it runs no hot loop at start-up (a loop of a few hundred clock reads was
21
+ // found to delay the first event loop turn by about a millisecond).
22
+ //
23
+ // Record = 4 float64 (32 bytes): [code, a, b, tsMs], code = kind + 16 * x
24
+ // 1 init a=asyncId b=triggerAsyncId x = type index
25
+ // 2 before a=asyncId x = 1: the close callback of a handle,
26
+ // 2: a handle's callback that is not its close callback
27
+ // 3 after a=asyncId
28
+ // 4 gone a=asyncId (destroy hook, or a cleared timer found by polling)
29
+ // 5 promiseResolve a=asyncId
30
+ // 6 output a=fd (1|2; 0 = hello) b=index into this frame's strings
31
+ // 7 loop a=loopCount b=events
32
+ // 8 loopWaiting a=eventsWaiting
33
+ // 9 exit a=exit code
34
+ // 10 timer a=asyncId b=delayMs x = 1 when repeating
35
+ // 11 where a=asyncId b=index into this frame's strings ("url\nline\ncol";
36
+ // only with the opt-in "loc" option: where the item was created)
37
+ // 13 outputWhere b=index into this frame's strings ("url\nline\ncol"; only with "loc":
38
+ // the line of the program that made the write just recorded)
39
+ // 12 poolEnv a=asyncId b=index into this frame's strings, or -1 when the variable
40
+ // is not set: UV_THREADPOOL_SIZE as it was when the first
41
+ // thread-pool-backed item was created (libuv reads it at first use)
42
+ // tsMs = performance.now(): the clock of process.hrtime and of trace events.
43
+ // Frame = 10 float64 header + records + optional UTF-8 JSON {"types":[],"strs":[]}
44
+ // header: [MAGIC, nRecords, jsonBytes, tokensLeft, prevFlushStartMs,
45
+ // prevFlushEndMs, frameNo, reason, flushTsMs, depth]
46
+ // reason: 0 hello, 1 depth zero, 2 size, 3 age, 4 unbuffered, 5 pull, 9 exit
47
+
48
+ const MAGIC = 0x4e4d4631; // "NMF1"
49
+ const ENV = 'NODEJS_MAP_PRELOAD';
50
+ const optsText = process.env[ENV];
51
+ delete process.env[ENV];
52
+
53
+ // Scrub our flags from execArgv (child_process.fork copies it).
54
+ for (let i = process.execArgv.length - 1; i >= 0; i--) {
55
+ const a = process.execArgv[i];
56
+ if (a === __filename && i > 0 && (process.execArgv[i - 1] === '-r' || process.execArgv[i - 1] === '--require')) {
57
+ process.execArgv.splice(i - 1, 2);
58
+ i--;
59
+ } else if (/^--inspect(-brk|-wait|-port|-publish-uid)?(=|$)/.test(a)) {
60
+ process.execArgv.splice(i, 1);
61
+ }
62
+ }
63
+
64
+ if (optsText && require('worker_threads').isMainThread) {
65
+ let opts = null;
66
+ try { opts = JSON.parse(optsText); } catch { opts = null; }
67
+ if (opts !== null && typeof opts === 'object') start(opts);
68
+ }
69
+
70
+ function start(opts) {
71
+ const fs = require('fs');
72
+ const async_hooks = require('async_hooks');
73
+ const writeSync = fs.writeSync;
74
+ const now = performance.now.bind(performance);
75
+ const nodeTiming = performance.nodeTiming;
76
+
77
+ const CAP = opts.capacity || 2048; // records per frame (64 KiB)
78
+ const MAX_AGE_MS = opts.maxAgeMs || 20; // flush even inside a callback after this long
79
+ const BUCKET_CAP = opts.bucketCap || 64; // burst of depth-zero flushes
80
+ const REFILL_PER_MS = opts.refillPerMs || 2; // sustained depth-zero flushes per ms
81
+ const policy = opts.flush || 'bucket'; // 'bucket' | 'always' | 'size'
82
+ const HDR = 10;
83
+
84
+ const ab = new ArrayBuffer((HDR + CAP * 4) * 8);
85
+ const f = new Float64Array(ab);
86
+ const u8 = new Uint8Array(ab);
87
+ let n = HDR;
88
+ const END = HDR + CAP * 4;
89
+ let frameNo = 0;
90
+ let prevFlushStart = 0, prevFlushEnd = 0;
91
+ let lastFlushTs = now();
92
+ let tokens = BUCKET_CAP, bucketTs = lastFlushTs;
93
+ let depth = 0;
94
+ let flushing = false;
95
+ let dead = false;
96
+ let unbuffered = false;
97
+ let strs = null;
98
+ let newTypes = null;
99
+ const typeIds = new Map();
100
+ // Types whose work runs on the libuv thread pool (the same list as
101
+ // isPoolBackedType in @nodejs-map/event-format).
102
+ const POOL_TYPE = /^(?:FSREQCALLBACK|FSREQPROMISE|FILEHANDLECLOSEREQ|GETADDRINFOREQWRAP|GETNAMEINFOREQWRAP|ZLIB|ARGON2REQUEST|CHECKPRIMEREQUEST|CIPHERREQUEST|DERIVEBITSREQUEST|HASHREQUEST|KEYEXPORTREQUEST|KEYGENREQUEST|KEYPAIRGENREQUEST|PBKDF2REQUEST|RANDOMBYTESREQUEST|RANDOMPRIMEREQUEST|SCRYPTREQUEST|SIGNREQUEST|VERIFYREQUEST)$/;
103
+ const poolFlags = [];
104
+ let poolSeen = false;
105
+ // Handles (HandleWrap objects): which of their callbacks is the close callback.
106
+ // Node sets the property "handle_onclose" (a symbol) on the handle object when
107
+ // close(callback) is called and calls it from HandleWrap::OnClose; after
108
+ // close() a handle gets no other callback.
109
+ const HANDLE_TYPE = /^(?:TCPWRAP|TCPSERVERWRAP|PIPEWRAP|PIPESERVERWRAP|TTYWRAP|UDPWRAP|PROCESSWRAP|SIGNALWRAP|FSEVENTWRAP|STATWATCHER|MESSAGEPORT)$/;
110
+ const handleFlags = [];
111
+ const handles = new Map(); // asyncId -> WeakRef(handle object)
112
+ const HANDLES_MAX = 20000;
113
+ const ownSymbols = Object.getOwnPropertySymbols;
114
+ const protoOf = Object.getPrototypeOf;
115
+ const hasOwn = Function.prototype.call.bind(Object.prototype.hasOwnProperty);
116
+ let oncloseSym; // Node's symbol, once one handle has shown it
117
+ function findOnclose(obj) {
118
+ const syms = ownSymbols(obj);
119
+ for (let i = 0; i < syms.length; i++) if (syms[i].description === 'handle_onclose') return syms[i];
120
+ return undefined;
121
+ }
122
+ // 1: this is the close callback; 2: it is not; 0: cannot tell
123
+ function closeState(obj) {
124
+ if (obj === undefined || obj === null) return 0;
125
+ try {
126
+ if (oncloseSym === undefined) oncloseSym = findOnclose(obj);
127
+ if (oncloseSym !== undefined && hasOwn(obj, oncloseSym)) return 1;
128
+ // a kind of handle that has the property on its prototype (a message port): nothing can be said
129
+ for (let p = protoOf(obj), i = 0; p !== null && i < 8; p = protoOf(p), i++) {
130
+ if (oncloseSym !== undefined ? hasOwn(p, oncloseSym) : findOnclose(p) !== undefined) return 0;
131
+ }
132
+ return 2;
133
+ } catch (_) {
134
+ return 0;
135
+ }
136
+ }
137
+ // The real environment: the program may replace process.env with an object of its own.
138
+ const realEnv = process.env;
139
+ let lastLoopCount = -1;
140
+
141
+ // ---- channel ------------------------------------------------------------
142
+ let fd = -1;
143
+ let send = () => {};
144
+ if (typeof opts.pipe === 'string') {
145
+ try {
146
+ if (opts.highFd !== false) {
147
+ // Park the channel above the low descriptor numbers: open
148
+ // placeholders, open the channel, close the placeholders.
149
+ // Windows: the device is '\\.\NUL'. Plain 'NUL' is an ordinary file
150
+ // name to Node's fs.
151
+ const nul = process.platform === 'win32' ? '\\\\.\\NUL' : '/dev/null';
152
+ const dummies = [];
153
+ for (let i = 0; i < 16; i++) dummies.push(fs.openSync(nul, 'r'));
154
+ try { fd = fs.openSync(opts.pipe, 'w'); } finally { for (let i = 0; i < dummies.length; i++) fs.closeSync(dummies[i]); }
155
+ } else {
156
+ fd = fs.openSync(opts.pipe, 'w');
157
+ }
158
+ } catch {
159
+ return; // no helper on the other end: run the program untouched
160
+ }
161
+ send = (bytes, json) => {
162
+ let off = 0;
163
+ while (off < bytes) off += writeSync(fd, u8, off, bytes - off);
164
+ if (json !== null) writeSync(fd, json);
165
+ };
166
+ }
167
+
168
+ // ---- flush ----------------------------------------------------------------
169
+ function flush(reason) {
170
+ if (flushing || dead) return;
171
+ const records = (n - HDR) / 4;
172
+ if (records === 0 && reason !== 9) return;
173
+ flushing = true;
174
+ let json = null;
175
+ if (strs !== null || newTypes !== null) {
176
+ json = JSON.stringify({ types: newTypes || undefined, strs: strs || undefined });
177
+ strs = null; newTypes = null;
178
+ }
179
+ f[0] = MAGIC; f[1] = records;
180
+ f[2] = json === null ? 0 : Buffer.byteLength(json);
181
+ f[3] = tokens; f[4] = prevFlushStart; f[5] = prevFlushEnd; f[6] = frameNo++; f[7] = reason;
182
+ const bytes = n * 8;
183
+ n = HDR;
184
+ const t0 = now();
185
+ f[8] = t0; f[9] = depth;
186
+ try { send(bytes, json); } catch { dead = true; }
187
+ const t1 = now();
188
+ prevFlushStart = t0; prevFlushEnd = t1; lastFlushTs = t1;
189
+ flushing = false;
190
+ }
191
+
192
+ function flushAtDepthZero(ts) {
193
+ if (pending.size !== 0) pollCancelled(ts);
194
+ if (policy === 'size') return;
195
+ if (policy === 'always') { flush(1); return; }
196
+ const add = (ts - bucketTs) * REFILL_PER_MS;
197
+ if (add >= 1) { tokens = tokens + add > BUCKET_CAP ? BUCKET_CAP : tokens + add; bucketTs = ts; }
198
+ if (tokens >= 1) { tokens -= 1; flush(1); }
199
+ }
200
+
201
+ function addType(type) {
202
+ const id = typeIds.size;
203
+ typeIds.set(type, id);
204
+ poolFlags[id] = POOL_TYPE.test(type);
205
+ handleFlags[id] = HANDLE_TYPE.test(type);
206
+ (newTypes || (newTypes = [])).push(type);
207
+ return id;
208
+ }
209
+
210
+ function afterRecord(ts) {
211
+ if (n >= END) flush(2);
212
+ // "always" (probe builds): nothing may stay behind outside a callback, because the
213
+ // target can be stopped at a gate at any moment and no JavaScript runs there to hand it over
214
+ else if (policy === 'always' && depth === 0) flush(1);
215
+ else if (ts - lastFlushTs > MAX_AGE_MS && policy !== 'size') flush(3);
216
+ else if (unbuffered) flush(4);
217
+ }
218
+
219
+ // ---- cleared timers -----------------------------------------------------
220
+ // The destroy hook is off by default (it makes Node track every promise for
221
+ // garbage collection). Without it a cleared timer or immediate would look
222
+ // as if it waited for ever, so we keep a weak reference to the few pending
223
+ // ones and look at their `_destroyed` flag when a callback has ended.
224
+ const pending = new Map(); // asyncId -> WeakRef(Timeout | Immediate)
225
+ const POLL_MAX = opts.cancelPollMax === undefined ? 256 : opts.cancelPollMax;
226
+ let lastPoll = 0;
227
+ function pollCancelled(ts) {
228
+ if (pending.size > POLL_MAX || ts - lastPoll < 1) return;
229
+ lastPoll = ts;
230
+ for (const [id, ref] of pending) {
231
+ const r = ref.deref();
232
+ if (r === undefined || r._destroyed === true) {
233
+ pending.delete(id);
234
+ if (n >= END - 4) flush(2);
235
+ const i = n;
236
+ f[i] = 4; f[i + 1] = id; f[i + 2] = 0; f[i + 3] = ts;
237
+ n = i + 4;
238
+ }
239
+ }
240
+ }
241
+
242
+ // ---- hooks ----------------------------------------------------------------
243
+ // Each hook reads the clock first and then stores its slots with no call in
244
+ // between, so a flush requested by the helper (it runs as a V8 interrupt at
245
+ // a call or loop boundary) never sees half a record.
246
+ const destroyOn = opts.destroy === true;
247
+ let TIMEOUT = -1, IMMEDIATE = -1;
248
+ function init(asyncId, type, triggerAsyncId, resource) {
249
+ let t = typeIds.get(type);
250
+ if (t === undefined) {
251
+ t = addType(type);
252
+ if (type === 'Timeout') TIMEOUT = t; else if (type === 'Immediate') IMMEDIATE = t;
253
+ }
254
+ const ts = now();
255
+ const i = n;
256
+ f[i] = 1 + 16 * t; f[i + 1] = asyncId; f[i + 2] = triggerAsyncId; f[i + 3] = ts;
257
+ n = i + 4;
258
+ if (captureLoc) where(asyncId, ts);
259
+ if (!poolSeen && poolFlags[t] === true) poolFirstUse(asyncId, ts);
260
+ if (handleFlags[t] === true && handles.size < HANDLES_MAX && resource !== null && typeof resource === 'object') handles.set(asyncId, new WeakRef(resource));
261
+ if (t === TIMEOUT) {
262
+ const delay = resource._idleTimeout;
263
+ const j = n;
264
+ f[j] = resource._repeat ? 26 : 10; f[j + 1] = asyncId; f[j + 2] = typeof delay === 'number' ? delay : -1; f[j + 3] = ts;
265
+ n = j + 4;
266
+ if (!destroyOn && pending.size <= POLL_MAX) pending.set(asyncId, new WeakRef(resource));
267
+ } else if (t === IMMEDIATE) {
268
+ if (!destroyOn && pending.size <= POLL_MAX) pending.set(asyncId, new WeakRef(resource));
269
+ }
270
+ if (n >= END - 8) flush(2);
271
+ else afterRecord(ts);
272
+ }
273
+ // libuv starts its pool threads at the first submission and reads
274
+ // UV_THREADPOOL_SIZE only then, so a program can still set it before that.
275
+ // Report what the variable holds at the first pool-backed item.
276
+ function poolFirstUse(asyncId, ts) {
277
+ poolSeen = true;
278
+ let v;
279
+ try { v = realEnv.UV_THREADPOOL_SIZE; } catch (_) { return; }
280
+ const text = typeof v === 'string' ? v.slice(0, 64) : undefined;
281
+ if (n >= END - 8) flush(2);
282
+ // from here to the end: no call (see "strings" at the output hook)
283
+ const i = n;
284
+ if (text !== undefined) {
285
+ const list = strs || (strs = []);
286
+ f[i] = 12; f[i + 1] = asyncId; f[i + 2] = list.length; f[i + 3] = ts;
287
+ list[list.length] = text;
288
+ } else {
289
+ f[i] = 12; f[i + 1] = asyncId; f[i + 2] = -1; f[i + 3] = ts;
290
+ }
291
+ n = i + 4;
292
+ }
293
+ // Opt-in ("loc"): the line of the user's program behind an async item or a
294
+ // write. One structured stack capture, at most 12 frames deep, no text parsing:
295
+ // V8 hands the call sites to pickUserFrame, which returns the first one that
296
+ // is neither Node's own code nor this file.
297
+ const captureLoc = opts.loc === true;
298
+ const captureStackTrace = Error.captureStackTrace;
299
+ const LOC_DEPTH = 12;
300
+ function pickUserFrame(_error, sites) {
301
+ for (let i = 0; i < sites.length; i++) {
302
+ const site = sites[i];
303
+ const file = site.getFileName();
304
+ if (typeof file !== 'string' || file === '' || file === __filename) continue;
305
+ if (file.charCodeAt(0) === 110 && file.startsWith('node:')) continue;
306
+ const line = site.getLineNumber();
307
+ if (typeof line !== 'number' || line < 1) continue;
308
+ return file + '\n' + line + '\n' + (site.getColumnNumber() || 0);
309
+ }
310
+ return undefined;
311
+ }
312
+ const holder = { stack: undefined };
313
+ // "path-or-url\nline\ncol" of the first user frame above `below`, or undefined.
314
+ function userFrame(below) {
315
+ const limit = Error.stackTraceLimit, prepare = Error.prepareStackTrace;
316
+ let found;
317
+ try {
318
+ Error.stackTraceLimit = LOC_DEPTH;
319
+ Error.prepareStackTrace = pickUserFrame;
320
+ captureStackTrace(holder, below);
321
+ found = holder.stack;
322
+ } catch (_) {
323
+ found = undefined;
324
+ } finally {
325
+ Error.prepareStackTrace = prepare;
326
+ Error.stackTraceLimit = limit;
327
+ }
328
+ return typeof found === 'string' ? found : undefined;
329
+ }
330
+ function where(asyncId, ts) {
331
+ const text = userFrame(init);
332
+ if (text === undefined) return;
333
+ if (n >= END - 8) flush(2);
334
+ // from here to the end: no call (see "strings" at the output hook)
335
+ const list = strs || (strs = []);
336
+ const i = n;
337
+ f[i] = 11; f[i + 1] = asyncId; f[i + 2] = list.length; f[i + 3] = ts;
338
+ n = i + 4;
339
+ list[list.length] = text;
340
+ }
341
+ const sampleLoop = opts.loop !== false;
342
+ function before(asyncId) {
343
+ if (depth === 0 && sampleLoop) loopSample();
344
+ let x = 0;
345
+ if (handles.size !== 0) {
346
+ const ref = handles.get(asyncId);
347
+ if (ref !== undefined) {
348
+ x = closeState(ref.deref());
349
+ if (x === 1) handles.delete(asyncId);
350
+ }
351
+ }
352
+ const ts = now();
353
+ const i = n;
354
+ f[i] = 2 + 16 * x; f[i + 1] = asyncId; f[i + 2] = 0; f[i + 3] = ts;
355
+ n = i + 4;
356
+ depth++;
357
+ if (n >= END - 8) flush(2);
358
+ }
359
+ function after(asyncId) {
360
+ const ts = now();
361
+ const i = n;
362
+ f[i] = 3; f[i + 1] = asyncId; f[i + 2] = 0; f[i + 3] = ts;
363
+ n = i + 4;
364
+ if (depth > 0) depth--;
365
+ if (pending.size !== 0) {
366
+ // a one-shot timer or an immediate that has run is no longer pending
367
+ const ref = pending.get(asyncId);
368
+ if (ref !== undefined) { const r = ref.deref(); if (r === undefined || !r._repeat) pending.delete(asyncId); }
369
+ }
370
+ if (n >= END - 8) flush(2);
371
+ else if (depth === 0) flushAtDepthZero(ts);
372
+ }
373
+ function destroy(asyncId) {
374
+ const ts = now();
375
+ const i = n;
376
+ f[i] = 4; f[i + 1] = asyncId; f[i + 2] = 0; f[i + 3] = ts;
377
+ n = i + 4;
378
+ afterRecord(ts);
379
+ }
380
+ function promiseResolve(asyncId) {
381
+ const ts = now();
382
+ const i = n;
383
+ f[i] = 5; f[i + 1] = asyncId; f[i + 2] = 0; f[i + 3] = ts;
384
+ n = i + 4;
385
+ afterRecord(ts);
386
+ }
387
+ function loopSample() {
388
+ const m = nodeTiming.uvMetricsInfo;
389
+ if (m.loopCount === lastLoopCount) return;
390
+ lastLoopCount = m.loopCount;
391
+ const ts = now();
392
+ const i = n;
393
+ f[i] = 7; f[i + 1] = m.loopCount; f[i + 2] = m.events; f[i + 3] = ts;
394
+ f[i + 4] = 8; f[i + 5] = m.eventsWaiting; f[i + 6] = 0; f[i + 7] = ts;
395
+ n = i + 8;
396
+ if (n >= END - 8) flush(2);
397
+ }
398
+
399
+ // ---- output -----------------------------------------------------------------
400
+ // process.stdout and process.stderr are lazy getters. We wrap the getter, so
401
+ // the stream is created exactly when the program first touches it, and put
402
+ // a thin wrapper on .write that records the text before the real write.
403
+ function output(fdNo, chunk, encoding) {
404
+ let entry;
405
+ if (typeof chunk === 'string') entry = chunk;
406
+ else if (chunk && typeof chunk.length === 'number' && chunk.buffer) entry = { latin1: Buffer.from(chunk.buffer, chunk.byteOffset, chunk.byteLength).toString('latin1') };
407
+ else entry = String(chunk);
408
+ const at = captureLoc ? userFrame(output) : undefined;
409
+ if (n >= END - 12) flush(2);
410
+ const ts = now();
411
+ // Strings: a record names its text by its place in this frame's string list.
412
+ // The helper's pull runs as a V8 interrupt at a call boundary and sends the
413
+ // frame, list included. So the list is taken, the record stored and the text
414
+ // added with no call in between (an element store, not push): record and
415
+ // text always travel in the same frame.
416
+ const list = strs || (strs = []);
417
+ const i = n;
418
+ f[i] = 6; f[i + 1] = fdNo; f[i + 2] = list.length; f[i + 3] = ts;
419
+ n = i + 4;
420
+ list[list.length] = entry;
421
+ if (at !== undefined) {
422
+ const j = n;
423
+ f[j] = 13; f[j + 1] = fdNo; f[j + 2] = list.length; f[j + 3] = ts;
424
+ n = j + 4;
425
+ list[list.length] = at;
426
+ }
427
+ if (unbuffered) flush(4);
428
+ else if (depth === 0) flushAtDepthZero(ts);
429
+ }
430
+ function hookStream(name, fdNo) {
431
+ const desc = Object.getOwnPropertyDescriptor(process, name);
432
+ if (!desc || typeof desc.get !== 'function') return;
433
+ let patched = false;
434
+ Object.defineProperty(process, name, {
435
+ __proto__: null,
436
+ configurable: desc.configurable,
437
+ enumerable: desc.enumerable,
438
+ get() {
439
+ const stream = desc.get.call(process);
440
+ if (!patched && stream) {
441
+ patched = true;
442
+ const orig = stream.write;
443
+ stream.write = function write(chunk, encoding, cb) {
444
+ output(fdNo, chunk, encoding);
445
+ return orig.call(this, chunk, encoding, cb);
446
+ };
447
+ }
448
+ return stream;
449
+ },
450
+ });
451
+ }
452
+ if (opts.output !== false) { hookStream('stdout', 1); hookStream('stderr', 2); }
453
+
454
+ // ---- helper pull and exit ---------------------------------------------------
455
+ // The helper calls this through the inspector (at every pause, and when it
456
+ // suspects an unflushed tail).
457
+ Object.defineProperty(process, Symbol.for('nodejs-map.flush'), {
458
+ __proto__: null, configurable: true, enumerable: false, writable: false,
459
+ // Returns [next frame number, callback depth, loop turns so far, clock]: with
460
+ // depth 0 and at least one loop turn, the call itself woke an idle loop.
461
+ value: function nodejsMapFlush() {
462
+ const d = depth;
463
+ const ts = now();
464
+ flush(5);
465
+ return [frameNo, d, nodeTiming.uvMetricsInfo.loopCount, ts];
466
+ },
467
+ });
468
+ process.on('exit', (code) => {
469
+ // Later 'exit' listeners of the program may still print: from here on
470
+ // every record is flushed at once.
471
+ unbuffered = true;
472
+ const ts = now();
473
+ const i = n;
474
+ f[i] = 9; f[i + 1] = typeof code === 'number' ? code : 0; f[i + 2] = 0; f[i + 3] = ts;
475
+ n = i + 4;
476
+ flush(9);
477
+ });
478
+
479
+ // ---- hello --------------------------------------------------------------------
480
+ // Clock offset: process.hrtime (ns) minus performance.now() (ms), from the
481
+ // tightest of three bracketed reads. Not a loop of hundreds (see the top).
482
+ let best = Infinity, offsetMs = 0;
483
+ for (let i = 0; i < 3; i++) {
484
+ const a = now(); const h = process.hrtime.bigint(); const b = now();
485
+ if (b - a < best) { best = b - a; offsetMs = Number(h) / 1e6 - (a + b) / 2; }
486
+ }
487
+ strs = [JSON.stringify({
488
+ hello: 1, pid: process.pid, node: process.versions.node, v8: process.versions.v8, uv: process.versions.uv,
489
+ platform: process.platform, arch: process.arch,
490
+ hrtimeMinusPerfNowMs: offsetMs, bracketMs: best, wallMs: Date.now(),
491
+ poolSize: typeof process.env.UV_THREADPOOL_SIZE === 'string' ? process.env.UV_THREADPOOL_SIZE.slice(0, 64) : null,
492
+ })];
493
+ f[n] = 6; f[n + 1] = 0; f[n + 2] = 0; f[n + 3] = now(); n += 4;
494
+ flush(0);
495
+
496
+ if (opts.hooks !== false) {
497
+ const hooks = { init, before, after };
498
+ if (destroyOn) hooks.destroy = destroy;
499
+ // On unless switched off: rule QP needs to know when a promise settles.
500
+ if (opts.promiseResolve !== false) hooks.promiseResolve = promiseResolve;
501
+ async_hooks.createHook(hooks).enable();
502
+ // No warm-up of the hook path here. Two were tried (see the investigation
503
+ // folder): calling the hook functions directly changes nothing, and running
504
+ // a throw-away AsyncResource helps but uses up an async id, which would
505
+ // shift every id the program itself can print.
506
+ }
507
+ }
package/package.json CHANGED
@@ -1,6 +1,27 @@
1
1
  {
2
2
  "name": "nodejs-map",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.1",
4
+ "description": "See what happens inside real Node.js while your program runs: event loop phases, queues, timers, the thread pool, with a debugger and a map of Node's own source.",
5
+ "type": "module",
6
+ "bin": {
7
+ "nodejs-map": "./dist/cli.js"
8
+ },
9
+ "files": [
10
+ "dist",
11
+ "README.md",
12
+ "LICENSE"
13
+ ],
14
+ "engines": {
15
+ "node": ">=26.0.0"
16
+ },
17
+ "keywords": [
18
+ "node",
19
+ "event loop",
20
+ "libuv",
21
+ "debugger",
22
+ "async_hooks",
23
+ "visualization",
24
+ "learning"
25
+ ],
26
+ "license": "MIT"
27
+ }