@open-take/compositor 0.5.2 → 0.6.0

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 (439) hide show
  1. package/dist/cdp-CzT5BKNY.js +3 -0
  2. package/dist/cdp-QXbQfXna.js +373 -0
  3. package/dist/cdp-QXbQfXna.js.map +1 -0
  4. package/dist/index.d.ts +4327 -195
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +26282 -160
  7. package/dist/index.js.map +1 -1
  8. package/dist/server-841PMF2H.js +163 -0
  9. package/dist/server-841PMF2H.js.map +1 -0
  10. package/dist/server-C2aVv4-t.js +3 -0
  11. package/kit/audio.mjs +2366 -0
  12. package/kit/camera.js +587 -0
  13. package/kit/cursor.js +264 -0
  14. package/kit/cut.js +427 -0
  15. package/kit/draw.js +346 -0
  16. package/kit/look.js +589 -0
  17. package/kit/map.js +170 -0
  18. package/kit/media.js +3568 -0
  19. package/kit/motion.js +1115 -0
  20. package/kit/seams.js +1006 -0
  21. package/kit/stage.js +923 -0
  22. package/kit/time.js +812 -0
  23. package/kit/world.js +1072 -0
  24. package/package.json +7 -3
  25. package/src/ffmpeg.ts +63 -0
  26. package/src/film/beats.ts +1285 -0
  27. package/src/film/carry.ts +221 -0
  28. package/src/film/changes.ts +330 -0
  29. package/src/film/check.ts +996 -0
  30. package/src/film/clock.ts +2530 -0
  31. package/src/film/components-probe.ts +809 -0
  32. package/src/film/components.ts +1566 -0
  33. package/src/film/frames.ts +389 -0
  34. package/src/film/gallery.ts +3276 -0
  35. package/src/film/kit.ts +515 -0
  36. package/src/film/layout.ts +701 -0
  37. package/src/film/listen.ts +1979 -0
  38. package/src/film/look.ts +3829 -0
  39. package/src/film/materials.ts +976 -0
  40. package/src/film/moving.ts +248 -0
  41. package/src/film/part-files.ts +729 -0
  42. package/src/film/picture.ts +74 -0
  43. package/src/film/probe-text.ts +843 -0
  44. package/src/film/probe.ts +3456 -0
  45. package/src/film/provenance.ts +596 -0
  46. package/src/film/render.ts +2447 -0
  47. package/src/film/report.ts +1631 -0
  48. package/src/film/sheet.ts +156 -0
  49. package/src/film/study-declared.ts +123 -0
  50. package/src/film/study-picture.ts +791 -0
  51. package/src/film/study-sound.ts +416 -0
  52. package/src/film/study.ts +864 -0
  53. package/src/film/tuning.ts +155 -0
  54. package/src/film/viewer.ts +174 -0
  55. package/src/html/cdp.ts +575 -0
  56. package/src/html/server.ts +208 -0
  57. package/src/index.ts +202 -0
  58. package/src/launch-render.ts +3 -2
  59. package/src/math.ts +3 -3
  60. package/src/mjpeg-matroska.ts +228 -0
  61. package/src/render.ts +6 -4
  62. package/src/state-diff.ts +215 -0
  63. package/src/state-index.ts +99 -0
  64. package/src/types.ts +59 -1
  65. package/starter/PLAN.md +84 -0
  66. package/starter/START.md +460 -0
  67. package/starter/lib/anim.js +760 -0
  68. package/starter/lib/devices.js +1402 -0
  69. package/starter/lib/handoff.js +421 -0
  70. package/starter/lib/hud.js +1879 -0
  71. package/starter/lib/measure.mjs +154 -0
  72. package/starter/lib/moves.js +1095 -0
  73. package/starter/lib/palette.mjs +803 -0
  74. package/starter/lib/physics-seeds.mjs +198 -0
  75. package/starter/lib/physics.js +738 -0
  76. package/starter/lib/sampler.mjs +242 -0
  77. package/starter/lib/solid.js +727 -0
  78. package/starter/lib/space.js +1227 -0
  79. package/starter/lib/swarm.js +1398 -0
  80. package/starter/reference/motion.md +276 -0
  81. package/starter/reference/structure.md +466 -0
  82. package/starter/reference/travel/motion.md +433 -0
  83. package/starter/reference/travel/sound.md +241 -0
  84. package/starter/reference/travel/structure.md +518 -0
  85. package/starter/samples/NOTICE.txt +55 -0
  86. package/starter/samples/celli/celli_24_1.opus +0 -0
  87. package/starter/samples/celli/celli_24_3.opus +0 -0
  88. package/starter/samples/celli/celli_28_1.opus +0 -0
  89. package/starter/samples/celli/celli_28_3.opus +0 -0
  90. package/starter/samples/celli/celli_31_1.opus +0 -0
  91. package/starter/samples/celli/celli_31_3.opus +0 -0
  92. package/starter/samples/celli/celli_35_1.opus +0 -0
  93. package/starter/samples/celli/celli_35_3.opus +0 -0
  94. package/starter/samples/celli/celli_38_1.opus +0 -0
  95. package/starter/samples/celli/celli_38_3.opus +0 -0
  96. package/starter/samples/celli/celli_41_1.opus +0 -0
  97. package/starter/samples/celli/celli_41_3.opus +0 -0
  98. package/starter/samples/celli/celli_45_1.opus +0 -0
  99. package/starter/samples/celli/celli_45_3.opus +0 -0
  100. package/starter/samples/celli/celli_48_1.opus +0 -0
  101. package/starter/samples/celli/celli_48_3.opus +0 -0
  102. package/starter/samples/celli/celli_52_1.opus +0 -0
  103. package/starter/samples/celli/celli_52_3.opus +0 -0
  104. package/starter/samples/celli/celli_55_1.opus +0 -0
  105. package/starter/samples/celli/celli_55_3.opus +0 -0
  106. package/starter/samples/celli/celli_59_1.opus +0 -0
  107. package/starter/samples/celli/celli_59_3.opus +0 -0
  108. package/starter/samples/celli/celli_62_1.opus +0 -0
  109. package/starter/samples/celli/celli_62_3.opus +0 -0
  110. package/starter/samples/celli/celli_65_1.opus +0 -0
  111. package/starter/samples/celli/celli_65_3.opus +0 -0
  112. package/starter/samples/crash/crash_f_1.flac +0 -0
  113. package/starter/samples/crash/crash_fff_1.flac +0 -0
  114. package/starter/samples/ebass/ebass_0_x.flac +0 -0
  115. package/starter/samples/ebass/ebass_10_x.flac +0 -0
  116. package/starter/samples/ebass/ebass_11_x.flac +0 -0
  117. package/starter/samples/ebass/ebass_1_x.flac +0 -0
  118. package/starter/samples/ebass/ebass_2_x.flac +0 -0
  119. package/starter/samples/ebass/ebass_3_x.flac +0 -0
  120. package/starter/samples/ebass/ebass_4_x.flac +0 -0
  121. package/starter/samples/ebass/ebass_5_x.flac +0 -0
  122. package/starter/samples/ebass/ebass_6_x.flac +0 -0
  123. package/starter/samples/ebass/ebass_7_x.flac +0 -0
  124. package/starter/samples/ebass/ebass_8_x.flac +0 -0
  125. package/starter/samples/ebass/ebass_9_x.flac +0 -0
  126. package/starter/samples/ek_clap/ek_clap_1_1.flac +0 -0
  127. package/starter/samples/ek_hat/ek_hat_1_1.flac +0 -0
  128. package/starter/samples/ek_hat/ek_hat_1_2.flac +0 -0
  129. package/starter/samples/ek_kick/ek_kick_1_1.flac +0 -0
  130. package/starter/samples/ek_kick/ek_kick_1_2.flac +0 -0
  131. package/starter/samples/glock/glock_67_x.flac +0 -0
  132. package/starter/samples/glock/glock_72_x.flac +0 -0
  133. package/starter/samples/glock/glock_79_x.flac +0 -0
  134. package/starter/samples/glock/glock_84_x.flac +0 -0
  135. package/starter/samples/glock/glock_91_x.flac +0 -0
  136. package/starter/samples/glock/glock_96_x.flac +0 -0
  137. package/starter/samples/harp/harp_101_f.opus +0 -0
  138. package/starter/samples/harp/harp_28_f.opus +0 -0
  139. package/starter/samples/harp/harp_31_mp.opus +0 -0
  140. package/starter/samples/harp/harp_35_mf.opus +0 -0
  141. package/starter/samples/harp/harp_38_mf.opus +0 -0
  142. package/starter/samples/harp/harp_41_mf.opus +0 -0
  143. package/starter/samples/harp/harp_45_mf.opus +0 -0
  144. package/starter/samples/harp/harp_48_mf.opus +0 -0
  145. package/starter/samples/harp/harp_52_mf.opus +0 -0
  146. package/starter/samples/harp/harp_55_mf.opus +0 -0
  147. package/starter/samples/harp/harp_59_mf.opus +0 -0
  148. package/starter/samples/harp/harp_62_mf.opus +0 -0
  149. package/starter/samples/harp/harp_65_mf.opus +0 -0
  150. package/starter/samples/harp/harp_69_mf.opus +0 -0
  151. package/starter/samples/harp/harp_72_mf.opus +0 -0
  152. package/starter/samples/harp/harp_76_mf.opus +0 -0
  153. package/starter/samples/harp/harp_79_mf.opus +0 -0
  154. package/starter/samples/harp/harp_83_mf.opus +0 -0
  155. package/starter/samples/harp/harp_86_mf.opus +0 -0
  156. package/starter/samples/harp/harp_89_mf.opus +0 -0
  157. package/starter/samples/harp/harp_93_mf.opus +0 -0
  158. package/starter/samples/harp/harp_95_mf.opus +0 -0
  159. package/starter/samples/harp/harp_98_f.opus +0 -0
  160. package/starter/samples/manifest.json +2951 -0
  161. package/starter/samples/marimba/marimba_29_x.flac +0 -0
  162. package/starter/samples/marimba/marimba_36_x.flac +0 -0
  163. package/starter/samples/marimba/marimba_43_x.flac +0 -0
  164. package/starter/samples/marimba/marimba_47_x.flac +0 -0
  165. package/starter/samples/marimba/marimba_53_x.flac +0 -0
  166. package/starter/samples/marimba/marimba_60_x.flac +0 -0
  167. package/starter/samples/marimba/marimba_67_x.flac +0 -0
  168. package/starter/samples/marimba/marimba_71_x.flac +0 -0
  169. package/starter/samples/marimba/marimba_77_x.flac +0 -0
  170. package/starter/samples/marimba/marimba_84_x.flac +0 -0
  171. package/starter/samples/piano/piano_102_12.opus +0 -0
  172. package/starter/samples/piano/piano_102_16.opus +0 -0
  173. package/starter/samples/piano/piano_102_4.opus +0 -0
  174. package/starter/samples/piano/piano_102_8.opus +0 -0
  175. package/starter/samples/piano/piano_105_12.opus +0 -0
  176. package/starter/samples/piano/piano_105_16.opus +0 -0
  177. package/starter/samples/piano/piano_105_4.opus +0 -0
  178. package/starter/samples/piano/piano_105_8.opus +0 -0
  179. package/starter/samples/piano/piano_108_12.opus +0 -0
  180. package/starter/samples/piano/piano_108_16.opus +0 -0
  181. package/starter/samples/piano/piano_108_4.opus +0 -0
  182. package/starter/samples/piano/piano_108_8.opus +0 -0
  183. package/starter/samples/piano/piano_21_12.opus +0 -0
  184. package/starter/samples/piano/piano_21_16.opus +0 -0
  185. package/starter/samples/piano/piano_21_4.opus +0 -0
  186. package/starter/samples/piano/piano_21_8.opus +0 -0
  187. package/starter/samples/piano/piano_24_12.opus +0 -0
  188. package/starter/samples/piano/piano_24_16.opus +0 -0
  189. package/starter/samples/piano/piano_24_4.opus +0 -0
  190. package/starter/samples/piano/piano_24_8.opus +0 -0
  191. package/starter/samples/piano/piano_27_12.opus +0 -0
  192. package/starter/samples/piano/piano_27_16.opus +0 -0
  193. package/starter/samples/piano/piano_27_4.opus +0 -0
  194. package/starter/samples/piano/piano_27_8.opus +0 -0
  195. package/starter/samples/piano/piano_30_12.opus +0 -0
  196. package/starter/samples/piano/piano_30_16.opus +0 -0
  197. package/starter/samples/piano/piano_30_4.opus +0 -0
  198. package/starter/samples/piano/piano_30_8.opus +0 -0
  199. package/starter/samples/piano/piano_33_12.opus +0 -0
  200. package/starter/samples/piano/piano_33_16.opus +0 -0
  201. package/starter/samples/piano/piano_33_4.opus +0 -0
  202. package/starter/samples/piano/piano_33_8.opus +0 -0
  203. package/starter/samples/piano/piano_36_12.opus +0 -0
  204. package/starter/samples/piano/piano_36_16.opus +0 -0
  205. package/starter/samples/piano/piano_36_4.opus +0 -0
  206. package/starter/samples/piano/piano_36_8.opus +0 -0
  207. package/starter/samples/piano/piano_39_12.opus +0 -0
  208. package/starter/samples/piano/piano_39_16.opus +0 -0
  209. package/starter/samples/piano/piano_39_4.opus +0 -0
  210. package/starter/samples/piano/piano_39_8.opus +0 -0
  211. package/starter/samples/piano/piano_42_12.opus +0 -0
  212. package/starter/samples/piano/piano_42_16.opus +0 -0
  213. package/starter/samples/piano/piano_42_4.opus +0 -0
  214. package/starter/samples/piano/piano_42_8.opus +0 -0
  215. package/starter/samples/piano/piano_45_12.opus +0 -0
  216. package/starter/samples/piano/piano_45_16.opus +0 -0
  217. package/starter/samples/piano/piano_45_4.opus +0 -0
  218. package/starter/samples/piano/piano_45_8.opus +0 -0
  219. package/starter/samples/piano/piano_48_12.opus +0 -0
  220. package/starter/samples/piano/piano_48_16.opus +0 -0
  221. package/starter/samples/piano/piano_48_4.opus +0 -0
  222. package/starter/samples/piano/piano_48_8.opus +0 -0
  223. package/starter/samples/piano/piano_51_12.opus +0 -0
  224. package/starter/samples/piano/piano_51_16.opus +0 -0
  225. package/starter/samples/piano/piano_51_4.opus +0 -0
  226. package/starter/samples/piano/piano_51_8.opus +0 -0
  227. package/starter/samples/piano/piano_54_12.opus +0 -0
  228. package/starter/samples/piano/piano_54_16.opus +0 -0
  229. package/starter/samples/piano/piano_54_4.opus +0 -0
  230. package/starter/samples/piano/piano_54_8.opus +0 -0
  231. package/starter/samples/piano/piano_57_12.opus +0 -0
  232. package/starter/samples/piano/piano_57_16.opus +0 -0
  233. package/starter/samples/piano/piano_57_4.opus +0 -0
  234. package/starter/samples/piano/piano_57_8.opus +0 -0
  235. package/starter/samples/piano/piano_60_12.opus +0 -0
  236. package/starter/samples/piano/piano_60_16.opus +0 -0
  237. package/starter/samples/piano/piano_60_4.opus +0 -0
  238. package/starter/samples/piano/piano_60_8.opus +0 -0
  239. package/starter/samples/piano/piano_63_12.opus +0 -0
  240. package/starter/samples/piano/piano_63_16.opus +0 -0
  241. package/starter/samples/piano/piano_63_4.opus +0 -0
  242. package/starter/samples/piano/piano_63_8.opus +0 -0
  243. package/starter/samples/piano/piano_66_12.opus +0 -0
  244. package/starter/samples/piano/piano_66_16.opus +0 -0
  245. package/starter/samples/piano/piano_66_4.opus +0 -0
  246. package/starter/samples/piano/piano_66_8.opus +0 -0
  247. package/starter/samples/piano/piano_69_12.opus +0 -0
  248. package/starter/samples/piano/piano_69_16.opus +0 -0
  249. package/starter/samples/piano/piano_69_4.opus +0 -0
  250. package/starter/samples/piano/piano_69_8.opus +0 -0
  251. package/starter/samples/piano/piano_72_12.opus +0 -0
  252. package/starter/samples/piano/piano_72_16.opus +0 -0
  253. package/starter/samples/piano/piano_72_4.opus +0 -0
  254. package/starter/samples/piano/piano_72_8.opus +0 -0
  255. package/starter/samples/piano/piano_75_12.opus +0 -0
  256. package/starter/samples/piano/piano_75_16.opus +0 -0
  257. package/starter/samples/piano/piano_75_4.opus +0 -0
  258. package/starter/samples/piano/piano_75_8.opus +0 -0
  259. package/starter/samples/piano/piano_78_12.opus +0 -0
  260. package/starter/samples/piano/piano_78_16.opus +0 -0
  261. package/starter/samples/piano/piano_78_4.opus +0 -0
  262. package/starter/samples/piano/piano_78_8.opus +0 -0
  263. package/starter/samples/piano/piano_81_12.opus +0 -0
  264. package/starter/samples/piano/piano_81_16.opus +0 -0
  265. package/starter/samples/piano/piano_81_4.opus +0 -0
  266. package/starter/samples/piano/piano_81_8.opus +0 -0
  267. package/starter/samples/piano/piano_84_12.opus +0 -0
  268. package/starter/samples/piano/piano_84_16.opus +0 -0
  269. package/starter/samples/piano/piano_84_4.opus +0 -0
  270. package/starter/samples/piano/piano_84_8.opus +0 -0
  271. package/starter/samples/piano/piano_87_12.opus +0 -0
  272. package/starter/samples/piano/piano_87_16.opus +0 -0
  273. package/starter/samples/piano/piano_87_4.opus +0 -0
  274. package/starter/samples/piano/piano_87_8.opus +0 -0
  275. package/starter/samples/piano/piano_90_12.opus +0 -0
  276. package/starter/samples/piano/piano_90_16.opus +0 -0
  277. package/starter/samples/piano/piano_90_4.opus +0 -0
  278. package/starter/samples/piano/piano_90_8.opus +0 -0
  279. package/starter/samples/piano/piano_93_12.opus +0 -0
  280. package/starter/samples/piano/piano_93_16.opus +0 -0
  281. package/starter/samples/piano/piano_93_4.opus +0 -0
  282. package/starter/samples/piano/piano_93_8.opus +0 -0
  283. package/starter/samples/piano/piano_96_12.opus +0 -0
  284. package/starter/samples/piano/piano_96_16.opus +0 -0
  285. package/starter/samples/piano/piano_96_4.opus +0 -0
  286. package/starter/samples/piano/piano_96_8.opus +0 -0
  287. package/starter/samples/piano/piano_99_12.opus +0 -0
  288. package/starter/samples/piano/piano_99_16.opus +0 -0
  289. package/starter/samples/piano/piano_99_4.opus +0 -0
  290. package/starter/samples/piano/piano_99_8.opus +0 -0
  291. package/starter/samples/piano/piano_rel_102_x.opus +0 -0
  292. package/starter/samples/piano/piano_rel_105_x.opus +0 -0
  293. package/starter/samples/piano/piano_rel_108_x.opus +0 -0
  294. package/starter/samples/piano/piano_rel_21_x.opus +0 -0
  295. package/starter/samples/piano/piano_rel_24_x.opus +0 -0
  296. package/starter/samples/piano/piano_rel_27_x.opus +0 -0
  297. package/starter/samples/piano/piano_rel_30_x.opus +0 -0
  298. package/starter/samples/piano/piano_rel_33_x.opus +0 -0
  299. package/starter/samples/piano/piano_rel_36_x.opus +0 -0
  300. package/starter/samples/piano/piano_rel_39_x.opus +0 -0
  301. package/starter/samples/piano/piano_rel_42_x.opus +0 -0
  302. package/starter/samples/piano/piano_rel_45_x.opus +0 -0
  303. package/starter/samples/piano/piano_rel_48_x.opus +0 -0
  304. package/starter/samples/piano/piano_rel_51_x.opus +0 -0
  305. package/starter/samples/piano/piano_rel_54_x.opus +0 -0
  306. package/starter/samples/piano/piano_rel_57_x.opus +0 -0
  307. package/starter/samples/piano/piano_rel_60_x.opus +0 -0
  308. package/starter/samples/piano/piano_rel_63_x.opus +0 -0
  309. package/starter/samples/piano/piano_rel_66_x.opus +0 -0
  310. package/starter/samples/piano/piano_rel_69_x.opus +0 -0
  311. package/starter/samples/piano/piano_rel_72_x.opus +0 -0
  312. package/starter/samples/piano/piano_rel_75_x.opus +0 -0
  313. package/starter/samples/piano/piano_rel_78_x.opus +0 -0
  314. package/starter/samples/piano/piano_rel_81_x.opus +0 -0
  315. package/starter/samples/piano/piano_rel_84_x.opus +0 -0
  316. package/starter/samples/piano/piano_rel_87_x.opus +0 -0
  317. package/starter/samples/piano/piano_rel_90_x.opus +0 -0
  318. package/starter/samples/piano/piano_rel_93_x.opus +0 -0
  319. package/starter/samples/piano/piano_rel_96_x.opus +0 -0
  320. package/starter/samples/piano/piano_rel_99_x.opus +0 -0
  321. package/starter/samples/pizz/pizz_24_2.flac +0 -0
  322. package/starter/samples/pizz/pizz_28_2.flac +0 -0
  323. package/starter/samples/pizz/pizz_31_2.flac +0 -0
  324. package/starter/samples/pizz/pizz_35_2.flac +0 -0
  325. package/starter/samples/pizz/pizz_38_2.flac +0 -0
  326. package/starter/samples/pizz/pizz_41_2.flac +0 -0
  327. package/starter/samples/pizz/pizz_45_2.flac +0 -0
  328. package/starter/samples/pizz/pizz_48_2.flac +0 -0
  329. package/starter/samples/pizz/pizz_52_2.flac +0 -0
  330. package/starter/samples/pizz/pizz_57_2.flac +0 -0
  331. package/starter/samples/pizz/pizz_60_2.flac +0 -0
  332. package/starter/samples/pizz/pizz_64_2.flac +0 -0
  333. package/starter/samples/pizz/pizz_67_2.flac +0 -0
  334. package/starter/samples/pizz/pizz_71_2.flac +0 -0
  335. package/starter/samples/pizz/pizz_74_2.flac +0 -0
  336. package/starter/samples/pk_hat/pk_hat_2_1.flac +0 -0
  337. package/starter/samples/pk_hat/pk_hat_2_2.flac +0 -0
  338. package/starter/samples/pk_hat/pk_hat_6_1.flac +0 -0
  339. package/starter/samples/pk_hat/pk_hat_6_2.flac +0 -0
  340. package/starter/samples/pk_kick/pk_kick_3_1.flac +0 -0
  341. package/starter/samples/pk_kick/pk_kick_3_2.flac +0 -0
  342. package/starter/samples/pk_kick/pk_kick_7_1.flac +0 -0
  343. package/starter/samples/pk_kick/pk_kick_7_2.flac +0 -0
  344. package/starter/samples/pk_ohat/pk_ohat_3_1.flac +0 -0
  345. package/starter/samples/pk_ohat/pk_ohat_3_2.flac +0 -0
  346. package/starter/samples/pk_ohat/pk_ohat_6_1.flac +0 -0
  347. package/starter/samples/pk_ohat/pk_ohat_6_2.flac +0 -0
  348. package/starter/samples/pk_rim/pk_rim_2_1.flac +0 -0
  349. package/starter/samples/pk_rim/pk_rim_2_2.flac +0 -0
  350. package/starter/samples/pk_rim/pk_rim_5_1.flac +0 -0
  351. package/starter/samples/pk_rim/pk_rim_5_2.flac +0 -0
  352. package/starter/samples/pk_snare/pk_snare_3_1.flac +0 -0
  353. package/starter/samples/pk_snare/pk_snare_3_2.flac +0 -0
  354. package/starter/samples/pk_snare/pk_snare_7_1.flac +0 -0
  355. package/starter/samples/pk_snare/pk_snare_7_2.flac +0 -0
  356. package/starter/samples/shaker/shaker_1_1.flac +0 -0
  357. package/starter/samples/shaker/shaker_2_1.flac +0 -0
  358. package/starter/samples/snap/snap_r_1.flac +0 -0
  359. package/starter/samples/snap/snap_r_2.flac +0 -0
  360. package/starter/samples/snap/snap_r_3.flac +0 -0
  361. package/starter/samples/snap/snap_r_4.flac +0 -0
  362. package/starter/samples/snap/snap_r_5.flac +0 -0
  363. package/starter/samples/snap/snap_r_6.flac +0 -0
  364. package/starter/samples/tom/tom_3_1.flac +0 -0
  365. package/starter/samples/tom/tom_3_2.flac +0 -0
  366. package/starter/samples/tom/tom_4_1.flac +0 -0
  367. package/starter/samples/tom/tom_4_2.flac +0 -0
  368. package/starter/samples/vibes/vibes_41_1.opus +0 -0
  369. package/starter/samples/vibes/vibes_41_2.opus +0 -0
  370. package/starter/samples/vibes/vibes_45_1.opus +0 -0
  371. package/starter/samples/vibes/vibes_45_2.opus +0 -0
  372. package/starter/samples/vibes/vibes_48_1.opus +0 -0
  373. package/starter/samples/vibes/vibes_48_2.opus +0 -0
  374. package/starter/samples/vibes/vibes_52_1.opus +0 -0
  375. package/starter/samples/vibes/vibes_52_2.opus +0 -0
  376. package/starter/samples/vibes/vibes_55_1.opus +0 -0
  377. package/starter/samples/vibes/vibes_55_2.opus +0 -0
  378. package/starter/samples/vibes/vibes_59_1.opus +0 -0
  379. package/starter/samples/vibes/vibes_59_2.opus +0 -0
  380. package/starter/samples/vibes/vibes_62_1.opus +0 -0
  381. package/starter/samples/vibes/vibes_62_2.opus +0 -0
  382. package/starter/samples/vibes/vibes_65_1.opus +0 -0
  383. package/starter/samples/vibes/vibes_65_2.opus +0 -0
  384. package/starter/samples/vibes/vibes_69_1.opus +0 -0
  385. package/starter/samples/vibes/vibes_69_2.opus +0 -0
  386. package/starter/samples/vibes/vibes_72_1.opus +0 -0
  387. package/starter/samples/vibes/vibes_72_2.opus +0 -0
  388. package/starter/samples/vibes/vibes_76_1.opus +0 -0
  389. package/starter/samples/vibes/vibes_76_2.opus +0 -0
  390. package/starter/samples/violas/violas_36_1.opus +0 -0
  391. package/starter/samples/violas/violas_36_2.opus +0 -0
  392. package/starter/samples/violas/violas_38_1.opus +0 -0
  393. package/starter/samples/violas/violas_38_2.opus +0 -0
  394. package/starter/samples/violas/violas_40_1.opus +0 -0
  395. package/starter/samples/violas/violas_40_2.opus +0 -0
  396. package/starter/samples/violas/violas_43_1.opus +0 -0
  397. package/starter/samples/violas/violas_43_2.opus +0 -0
  398. package/starter/samples/violas/violas_47_1.opus +0 -0
  399. package/starter/samples/violas/violas_47_2.opus +0 -0
  400. package/starter/samples/violas/violas_50_1.opus +0 -0
  401. package/starter/samples/violas/violas_50_2.opus +0 -0
  402. package/starter/samples/violas/violas_53_1.opus +0 -0
  403. package/starter/samples/violas/violas_53_2.opus +0 -0
  404. package/starter/samples/violas/violas_57_1.opus +0 -0
  405. package/starter/samples/violas/violas_57_2.opus +0 -0
  406. package/starter/samples/violas/violas_60_1.opus +0 -0
  407. package/starter/samples/violas/violas_60_2.opus +0 -0
  408. package/starter/samples/violas/violas_64_1.opus +0 -0
  409. package/starter/samples/violas/violas_64_2.opus +0 -0
  410. package/starter/samples/violas/violas_67_1.opus +0 -0
  411. package/starter/samples/violas/violas_67_2.opus +0 -0
  412. package/starter/samples/violas/violas_71_1.opus +0 -0
  413. package/starter/samples/violas/violas_71_2.opus +0 -0
  414. package/starter/samples/violas/violas_74_1.opus +0 -0
  415. package/starter/samples/violas/violas_74_2.opus +0 -0
  416. package/starter/samples/violins/violins_43_1.opus +0 -0
  417. package/starter/samples/violins/violins_43_2.opus +0 -0
  418. package/starter/samples/violins/violins_45_1.opus +0 -0
  419. package/starter/samples/violins/violins_45_2.opus +0 -0
  420. package/starter/samples/violins/violins_47_1.opus +0 -0
  421. package/starter/samples/violins/violins_47_2.opus +0 -0
  422. package/starter/samples/violins/violins_50_1.opus +0 -0
  423. package/starter/samples/violins/violins_50_2.opus +0 -0
  424. package/starter/samples/violins/violins_54_1.opus +0 -0
  425. package/starter/samples/violins/violins_54_2.opus +0 -0
  426. package/starter/samples/violins/violins_57_1.opus +0 -0
  427. package/starter/samples/violins/violins_57_2.opus +0 -0
  428. package/starter/samples/violins/violins_60_1.opus +0 -0
  429. package/starter/samples/violins/violins_60_2.opus +0 -0
  430. package/starter/samples/violins/violins_64_1.opus +0 -0
  431. package/starter/samples/violins/violins_64_2.opus +0 -0
  432. package/starter/samples/violins/violins_67_1.opus +0 -0
  433. package/starter/samples/violins/violins_67_2.opus +0 -0
  434. package/starter/samples/violins/violins_71_1.opus +0 -0
  435. package/starter/samples/violins/violins_71_2.opus +0 -0
  436. package/starter/samples/violins/violins_74_1.opus +0 -0
  437. package/starter/samples/violins/violins_74_2.opus +0 -0
  438. package/starter/samples/woodblock/woodblock_f_1.flac +0 -0
  439. package/starter/samples/woodblock/woodblock_f_2.flac +0 -0
@@ -0,0 +1,575 @@
1
+ // A headless Chrome for the seek renderer, driven over the DevTools protocol
2
+ // with nothing but Node 22's global WebSocket and fetch — the shape of
3
+ // packages/runtime/src/cdp.ts, cut down to what a frame renderer needs: one
4
+ // page, evaluate, screenshot, close.
5
+ //
6
+ // The browser is launched at an EXPLICIT path (the caller resolves it; never
7
+ // a system Chrome, which hands the launch to the instance already running and
8
+ // never opens its debug port) with a fresh throwaway profile and the render
9
+ // hygiene flags the html-renderer spike measured: every compositor stage runs
10
+ // before a draw, no checkerboard tiles, one colour profile, no LCD text. The
11
+ // GPU path is kept — software raster costs 2.5× per shot and only buys a
12
+ // bit-exactness that CRF encoding erases anyway (single-LSB jitter otherwise).
13
+ //
14
+ // Every wait has a timeout and every failure is a rejection, never a hang: a
15
+ // page that throws (Runtime.evaluate's exceptionDetails, an uncaught
16
+ // Runtime.exceptionThrown), a Chrome that exits, a socket that closes.
17
+
18
+ import { type ChildProcess, execFile, spawn } from "node:child_process";
19
+ import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
20
+ import { tmpdir } from "node:os";
21
+ import { join } from "node:path";
22
+
23
+ const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
24
+
25
+ export type HtmlBrowser = {
26
+ /** Run `expression` in the page. A returned promise is awaited; a throw or
27
+ * rejection (Runtime.evaluate exceptionDetails) rejects. */
28
+ evaluate<T = unknown>(expression: string, opts?: { timeoutMs?: number }): Promise<T>;
29
+ /** The viewport as PNG (default) or JPEG bytes. */
30
+ screenshot(opts?: { format?: "png" | "jpeg"; quality?: number }): Promise<Buffer>;
31
+ /** Kill Chrome and remove its profile. Idempotent; never throws. */
32
+ close(): Promise<void>;
33
+ };
34
+
35
+ export type LaunchHtmlBrowserOpts = {
36
+ /** the Chrome binary to run — resolved by the caller, never searched for here */
37
+ chromePath: string;
38
+ /** the page to open; resolves once its load event fired */
39
+ url: string;
40
+ width: number;
41
+ height: number;
42
+ /** default for every wait: launch, load, evaluate, screenshot (60 s) */
43
+ timeoutMs?: number;
44
+ /** the throwaway profile's directory name prefix in the temp dir */
45
+ profilePrefix?: string;
46
+ /** runs in every document before any of its own scripts
47
+ * (Page.addScriptToEvaluateOnNewDocument) */
48
+ initScript?: string;
49
+ /** device pixels per CSS pixel, default 1: the page lays out at
50
+ * width×height CSS px and screenshots come out that many times larger
51
+ * (a half-size draft is 0.5) */
52
+ deviceScaleFactor?: number;
53
+ /** the page's console calls: level (log, warning, error…) and text */
54
+ onConsole?: (level: string, text: string) => void;
55
+ /** CSS animations and transitions and the Web Animations API stand still
56
+ * from the moment the page starts, until the page's own code (the film
57
+ * clock) sets them: one shorter than the page's load would otherwise play
58
+ * out in real time before the film begins, and be gone */
59
+ stillAnimations?: boolean;
60
+ /** functions the page can call by name (Runtime.addBinding), in every
61
+ * document it opens: each gets the string the page passed */
62
+ bindings?: Record<string, (payload: string) => void>;
63
+ };
64
+
65
+ // Chrome writes the port of a `--remote-debugging-port=0` launch to
66
+ // <profile>/DevToolsActivePort once the listener is up. The first run of a
67
+ // freshly downloaded bundle can take several seconds to start (macOS validates
68
+ // the code signature) — the runtime measured 4.5 s — so the wait is generous;
69
+ // a crashed Chrome still fails fast through its exit.
70
+ const DEVTOOLS_PORT_WAIT_MS = 30_000;
71
+ const DEFAULT_TIMEOUT_MS = 60_000;
72
+
73
+ /** Launch a headless Chrome at `chromePath`, open `url` in a `width`×`height`
74
+ * viewport and wait for its load event. Fails (with Chrome killed and its
75
+ * profile removed) if the binary is missing, Chrome exits or never opens its
76
+ * port, or the navigation errors. */
77
+ export async function launchHtmlBrowser(opts: LaunchHtmlBrowserOpts): Promise<HtmlBrowser> {
78
+ const { chromePath, url, width, height } = opts;
79
+ const timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;
80
+ if (!existsSync(chromePath)) throw new Error(`html render: no Chrome binary at ${chromePath}`);
81
+ if (!(Number.isInteger(width) && width > 0 && Number.isInteger(height) && height > 0))
82
+ throw new Error(`html render: viewport must be whole positive pixels, got ${width}×${height}`);
83
+
84
+ const userDir = mkdtempSync(join(tmpdir(), opts.profilePrefix ?? "open-take-html-"));
85
+ const proc: ChildProcess = spawn(
86
+ chromePath,
87
+ [
88
+ "--headless=new",
89
+ "--remote-debugging-port=0", // a free port, read back from DevToolsActivePort
90
+ `--user-data-dir=${userDir}`,
91
+ // The runtime capture path found the launch never writes DevToolsActivePort
92
+ // in sandboxed/containerised/CI contexts where Chrome's own sandbox can't
93
+ // init; the page here is our own local file, so the sandbox buys nothing.
94
+ "--no-sandbox",
95
+ "--no-first-run",
96
+ "--no-default-browser-check",
97
+ "--hide-scrollbars",
98
+ "--mute-audio",
99
+ "--disable-extensions",
100
+ "--disable-background-networking",
101
+ "--disable-sync",
102
+ "--metrics-recording-only",
103
+ // never touch the OS keychain (macOS would prompt for Chromium Safe Storage)
104
+ "--password-store=basic",
105
+ "--use-mock-keychain",
106
+ // render hygiene: a screenshot only after every compositor stage has run,
107
+ // no checkerboarded tiles, one colour profile, no LCD subpixel text
108
+ "--run-all-compositor-stages-before-draw",
109
+ "--disable-checker-imaging",
110
+ "--force-color-profile=srgb",
111
+ "--disable-lcd-text",
112
+ "--font-render-hinting=none",
113
+ // the window is never smaller than 500×300: on macOS a headless window
114
+ // of a couple of hundred pixels a side can stop answering
115
+ // Page.captureScreenshot (seen at 64×64 and 200×200, not at 500×300).
116
+ // The viewport itself is set below, by setDeviceMetricsOverride.
117
+ `--window-size=${Math.max(width, 500)},${Math.max(height, 300)}`,
118
+ "about:blank",
119
+ ],
120
+ { stdio: ["ignore", "ignore", "pipe"] },
121
+ );
122
+ let stderr = "";
123
+ proc.stderr?.on("data", (d: Buffer) => {
124
+ stderr = (stderr + d.toString()).slice(-4000);
125
+ });
126
+
127
+ // the process's fate, observable by every wait below
128
+ let exit: { code: number | null; signal: NodeJS.Signals | null } | null = null;
129
+ let spawnError: Error | null = null;
130
+ const exited = new Promise<void>((r) => {
131
+ proc.once("exit", (code, signal) => {
132
+ exit = { code, signal };
133
+ r();
134
+ });
135
+ proc.once("error", (e) => {
136
+ spawnError = e;
137
+ r();
138
+ });
139
+ });
140
+ const exitWithin = (ms: number) =>
141
+ Promise.race([exited.then(() => true), sleep(ms).then(() => false)]);
142
+ const chromeGone = (): Error | null => {
143
+ if (spawnError) return new Error(`Chrome could not start: ${spawnError.message}`);
144
+ if (exit)
145
+ return new Error(
146
+ `Chrome exited (${exit.signal ?? `code ${exit.code}`})${stderr ? `\n${stderr.slice(-600)}` : ""}`,
147
+ );
148
+ return null;
149
+ };
150
+
151
+ // Chrome must not outlive this process: a sync kill on exit (the temp
152
+ // profile goes with it). Removed again on close so listeners never pile up.
153
+ const killOnExit = () => {
154
+ try {
155
+ proc.kill("SIGKILL");
156
+ } catch {
157
+ /* already gone */
158
+ }
159
+ rmSync(userDir, { recursive: true, force: true });
160
+ };
161
+ process.once("exit", killOnExit);
162
+
163
+ let session: Session | null = null;
164
+ let closing: Promise<void> | null = null;
165
+ const close = (): Promise<void> => {
166
+ if (!closing) {
167
+ closing = (async () => {
168
+ process.off("exit", killOnExit);
169
+ // Browser.close lets Chrome retire its GPU and renderer helpers itself;
170
+ // a Chrome that does not go within the grace period is killed.
171
+ if (session?.alive) session.send("Browser.close", {}, 1000).catch(() => {});
172
+ if (!(await exitWithin(session?.alive ? 1500 : 0))) {
173
+ try {
174
+ proc.kill("SIGKILL");
175
+ } catch {
176
+ /* already gone */
177
+ }
178
+ await exitWithin(2000); // don't hang if exit never fires
179
+ }
180
+ session?.close();
181
+ await killSurvivors(userDir);
182
+ // Chrome may still be flushing the profile: retry the rm, and never let
183
+ // a directory that will not go take the place of the render's own error
184
+ try {
185
+ rmSync(userDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
186
+ } catch {
187
+ setTimeout(() => {
188
+ try {
189
+ rmSync(userDir, { recursive: true, force: true });
190
+ } catch {
191
+ /* a profile a dying helper still held: swept with the next launch's tmp */
192
+ }
193
+ }, 1000).unref();
194
+ }
195
+ })();
196
+ }
197
+ return closing;
198
+ };
199
+
200
+ // one uncaught page error poisons the page: every call in flight and every
201
+ // later one rejects with it, so a broken page never renders quietly. The
202
+ // calls in flight are held in a set and let go as they settle — racing each
203
+ // one against a single never-settling "poisoned" promise instead kept every
204
+ // settled call (and its result: a 1 MB screenshot) reachable from that
205
+ // promise's reaction list, and a full film ran Node out of memory.
206
+ let pageError: Error | null = null;
207
+ const inflight = new Set<(e: Error) => void>();
208
+ const poison = (e: Error) => {
209
+ if (pageError) return;
210
+ pageError = e;
211
+ for (const reject of inflight) reject(e);
212
+ inflight.clear();
213
+ };
214
+ const guarded = <T>(p: Promise<T>): Promise<T> => {
215
+ if (pageError) return Promise.reject(pageError);
216
+ return new Promise<T>((resolve, reject) => {
217
+ inflight.add(reject);
218
+ p.then(
219
+ (v) => {
220
+ inflight.delete(reject);
221
+ resolve(v);
222
+ },
223
+ (e) => {
224
+ inflight.delete(reject);
225
+ reject(e);
226
+ },
227
+ );
228
+ });
229
+ };
230
+
231
+ try {
232
+ const port = await readDevtoolsPort(userDir, chromeGone);
233
+ const wsUrl = await pageTargetWs(port, chromeGone);
234
+ session = await Session.connect(wsUrl, timeoutMs);
235
+ const s = session;
236
+ // Chrome going away fails every pending call at once (not on their timeouts)
237
+ void exited.then(() => s.fail(chromeGone() ?? new Error("Chrome exited")));
238
+
239
+ s.on<{ exceptionDetails: ExceptionDetails }>("Runtime.exceptionThrown", (p) =>
240
+ poison(new Error(`page error: ${describeException(p.exceptionDetails)}`)),
241
+ );
242
+ s.on("Inspector.targetCrashed", () => poison(new Error("page crashed")));
243
+ // a dialog would block the page for good; a render page has no one to answer it
244
+ s.on("Page.javascriptDialogOpening", () => {
245
+ s.send("Page.handleJavaScriptDialog", { accept: true }).catch(() => {});
246
+ });
247
+
248
+ await s.send("Page.enable", {}, timeoutMs);
249
+ await s.send("Runtime.enable", {}, timeoutMs);
250
+ if (opts.onConsole) {
251
+ const onConsole = opts.onConsole;
252
+ s.on<{ type: string; args: { value?: unknown; description?: string }[] }>(
253
+ "Runtime.consoleAPICalled",
254
+ (p) =>
255
+ onConsole(p.type, p.args.map((a) => String(a.value ?? a.description ?? "")).join(" ")),
256
+ );
257
+ }
258
+ const bindings = opts.bindings ?? {};
259
+ if (Object.keys(bindings).length) {
260
+ s.on<{ name: string; payload: string }>("Runtime.bindingCalled", (p) =>
261
+ bindings[p.name]?.(p.payload),
262
+ );
263
+ for (const name of Object.keys(bindings))
264
+ await s.send("Runtime.addBinding", { name }, timeoutMs);
265
+ }
266
+ if (opts.initScript)
267
+ await s.send("Page.addScriptToEvaluateOnNewDocument", { source: opts.initScript }, timeoutMs);
268
+ if (opts.stillAnimations) {
269
+ // the rate holds for every document the page opens, frames included
270
+ await s.send("Animation.enable", {}, timeoutMs);
271
+ await s.send("Animation.setPlaybackRate", { playbackRate: 0 }, timeoutMs);
272
+ }
273
+ await s.send("Page.setLifecycleEventsEnabled", { enabled: true }, timeoutMs);
274
+ await s.send(
275
+ "Emulation.setDeviceMetricsOverride",
276
+ { width, height, deviceScaleFactor: opts.deviceScaleFactor ?? 1, mobile: false },
277
+ timeoutMs,
278
+ );
279
+
280
+ // The load event of OUR navigation, matched by loaderId: about:blank's own
281
+ // load, or a redirect chain, must not pass for it. Events are recorded from
282
+ // before the navigate call — several protocol messages can be dispatched in
283
+ // one turn, so a listener registered after the reply could miss the event.
284
+ const loaded = new Set<string>();
285
+ s.on<{ name: string; loaderId: string }>("Page.lifecycleEvent", (p) => {
286
+ if (p.name === "load") loaded.add(p.loaderId);
287
+ });
288
+ const nav = await guarded(
289
+ s.send<{ loaderId?: string; errorText?: string }>("Page.navigate", { url }, timeoutMs),
290
+ );
291
+ if (nav.errorText) throw new Error(`could not load ${url}: ${nav.errorText}`);
292
+ const loaderId = nav.loaderId;
293
+ if (!(loaderId && loaded.has(loaderId)))
294
+ await guarded(
295
+ s.waitFor<{ name: string; loaderId: string }>(
296
+ "Page.lifecycleEvent",
297
+ (p) => p.name === "load" && (!loaderId || p.loaderId === loaderId),
298
+ timeoutMs,
299
+ `load of ${url}`,
300
+ ),
301
+ );
302
+ } catch (e) {
303
+ await close();
304
+ throw new Error(`html render: browser launch failed: ${(e as Error).message}`, { cause: e });
305
+ }
306
+
307
+ const s = session;
308
+ return {
309
+ async evaluate<T = unknown>(
310
+ expression: string,
311
+ evalOpts: { timeoutMs?: number } = {},
312
+ ): Promise<T> {
313
+ const r = await guarded(
314
+ s.send<{ result?: { value?: unknown }; exceptionDetails?: ExceptionDetails }>(
315
+ "Runtime.evaluate",
316
+ { expression, awaitPromise: true, returnByValue: true },
317
+ evalOpts.timeoutMs ?? timeoutMs,
318
+ ),
319
+ );
320
+ if (r.exceptionDetails)
321
+ throw new Error(`page threw: ${describeException(r.exceptionDetails)}`);
322
+ return r.result?.value as T;
323
+ },
324
+ async screenshot(shot: { format?: "png" | "jpeg"; quality?: number } = {}): Promise<Buffer> {
325
+ const format = shot.format ?? "png";
326
+ const r = await guarded(
327
+ s.send<{ data: string }>(
328
+ "Page.captureScreenshot",
329
+ {
330
+ format,
331
+ ...(format === "jpeg" && shot.quality != null ? { quality: shot.quality } : {}),
332
+ optimizeForSpeed: true,
333
+ captureBeyondViewport: false,
334
+ },
335
+ timeoutMs,
336
+ ),
337
+ );
338
+ return Buffer.from(r.data, "base64");
339
+ },
340
+ close,
341
+ };
342
+ }
343
+
344
+ // --- the process's debug port and page target -------------------------------
345
+
346
+ /** Kill every process still carrying this profile's path in its arguments.
347
+ * Chrome hands `--user-data-dir` to its GPU, renderer and utility helpers, and
348
+ * they normally go with the browser — but not always: under load, two helpers
349
+ * were seen alive 5 s after their browser had exited on Browser.close. A
350
+ * survivor would hold the profile open and keep rendering nothing. POSIX only:
351
+ * on Windows the sandbox's job object takes the helpers down with the browser. */
352
+ async function killSurvivors(userDir: string): Promise<void> {
353
+ if (process.platform === "win32") return;
354
+ const table = await new Promise<string>((r) =>
355
+ execFile(
356
+ "ps",
357
+ ["-axo", "pid=,command="],
358
+ { encoding: "utf8", maxBuffer: 1 << 24 },
359
+ (_error, stdout) => r(stdout ?? ""), // no ps (a bare container): nothing to sweep with
360
+ ),
361
+ );
362
+ for (const line of table.split("\n")) {
363
+ if (!line.includes(userDir)) continue;
364
+ const pid = Number(line.trim().split(/\s+/)[0]);
365
+ if (!(pid > 0) || pid === process.pid) continue;
366
+ try {
367
+ process.kill(pid, "SIGKILL");
368
+ } catch {
369
+ /* gone between the listing and now */
370
+ }
371
+ }
372
+ }
373
+
374
+ async function readDevtoolsPort(userDir: string, gone: () => Error | null): Promise<number> {
375
+ const file = join(userDir, "DevToolsActivePort");
376
+ for (let i = 0; i < DEVTOOLS_PORT_WAIT_MS / 50; i++) {
377
+ const dead = gone();
378
+ if (dead) throw dead;
379
+ if (existsSync(file)) {
380
+ const port = Number(readFileSync(file, "utf8").split("\n")[0]?.trim());
381
+ if (Number.isFinite(port) && port > 0) return port;
382
+ }
383
+ await sleep(50);
384
+ }
385
+ throw new Error(`timed out waiting for DevToolsActivePort (${DEVTOOLS_PORT_WAIT_MS / 1000}s)`);
386
+ }
387
+
388
+ async function pageTargetWs(port: number, gone: () => Error | null): Promise<string> {
389
+ for (let i = 0; i < 100; i++) {
390
+ const dead = gone();
391
+ if (dead) throw dead;
392
+ try {
393
+ const list = (await (await fetch(`http://127.0.0.1:${port}/json`)).json()) as Array<{
394
+ type: string;
395
+ webSocketDebuggerUrl?: string;
396
+ }>;
397
+ const page = list.find((t) => t.type === "page");
398
+ if (page?.webSocketDebuggerUrl) return page.webSocketDebuggerUrl;
399
+ } catch {
400
+ /* the listener is up before the target list is */
401
+ }
402
+ await sleep(100);
403
+ }
404
+ throw new Error("no page target appeared");
405
+ }
406
+
407
+ // --- exceptions as text --------------------------------------------------------
408
+
409
+ type ExceptionDetails = {
410
+ text: string;
411
+ lineNumber: number;
412
+ columnNumber: number;
413
+ url?: string;
414
+ exception?: { description?: string; value?: unknown };
415
+ };
416
+
417
+ /** "Error: boom\n at __seek (http://…/index.html:5:20)" plus the location
418
+ * when the description carries no stack of its own. */
419
+ function describeException(d: ExceptionDetails): string {
420
+ const description =
421
+ d.exception?.description ??
422
+ (d.exception && "value" in d.exception ? String(d.exception.value) : d.text);
423
+ const lines = description.split("\n");
424
+ const where =
425
+ lines.length > 1 || !d.url ? "" : ` (${d.url}:${d.lineNumber + 1}:${d.columnNumber + 1})`;
426
+ return lines.slice(0, 4).join("\n") + where;
427
+ }
428
+
429
+ // --- minimal CDP client over a page-target websocket ------------------------
430
+
431
+ type Pending = { resolve: (value: unknown) => void; reject: (error: Error) => void };
432
+
433
+ class Session {
434
+ private ws: WebSocket;
435
+ private nextId = 0;
436
+ private pending = new Map<number, Pending>();
437
+ private handlers = new Map<string, Set<(params: unknown) => void>>();
438
+ private dead: Error | null = null;
439
+
440
+ get alive(): boolean {
441
+ return this.dead === null;
442
+ }
443
+
444
+ private constructor(ws: WebSocket) {
445
+ this.ws = ws;
446
+ ws.addEventListener("message", (ev: MessageEvent) => {
447
+ const msg = JSON.parse(String(ev.data)) as {
448
+ id?: number;
449
+ error?: { message?: string };
450
+ result?: unknown;
451
+ method?: string;
452
+ params?: unknown;
453
+ };
454
+ if (msg.id != null) {
455
+ const call = this.pending.get(msg.id);
456
+ if (!call) return; // answered after its timeout
457
+ this.pending.delete(msg.id);
458
+ if (msg.error) call.reject(new Error(msg.error.message ?? JSON.stringify(msg.error)));
459
+ else call.resolve(msg.result);
460
+ } else if (msg.method) {
461
+ for (const fn of this.handlers.get(msg.method) ?? []) fn(msg.params);
462
+ }
463
+ });
464
+ ws.addEventListener("close", () => this.fail(new Error("the DevTools socket closed")));
465
+ ws.addEventListener("error", () => this.fail(new Error("the DevTools socket failed")));
466
+ }
467
+
468
+ static connect(wsUrl: string, timeoutMs: number): Promise<Session> {
469
+ return new Promise((resolve, reject) => {
470
+ const ws = new WebSocket(wsUrl);
471
+ const timer = setTimeout(() => {
472
+ reject(new Error(`connecting to ${wsUrl} timed out after ${timeoutMs} ms`));
473
+ ws.close();
474
+ }, timeoutMs);
475
+ ws.addEventListener(
476
+ "open",
477
+ () => {
478
+ clearTimeout(timer);
479
+ resolve(new Session(ws));
480
+ },
481
+ { once: true },
482
+ );
483
+ ws.addEventListener(
484
+ "error",
485
+ () => {
486
+ clearTimeout(timer);
487
+ reject(new Error(`CDP connect failed: ${wsUrl}`));
488
+ },
489
+ { once: true },
490
+ );
491
+ });
492
+ }
493
+
494
+ /** Send a command; the reply, or a rejection on a protocol error, a dead
495
+ * session, or `timeoutMs` passing (a late reply is then dropped). */
496
+ send<T = unknown>(
497
+ method: string,
498
+ params: Record<string, unknown> = {},
499
+ timeoutMs?: number,
500
+ ): Promise<T> {
501
+ if (this.dead) return Promise.reject(this.dead);
502
+ const id = ++this.nextId;
503
+ return new Promise<T>((resolve, reject) => {
504
+ let timer: NodeJS.Timeout | undefined;
505
+ const settle = (fn: () => void) => {
506
+ clearTimeout(timer);
507
+ this.pending.delete(id);
508
+ fn();
509
+ };
510
+ if (timeoutMs)
511
+ timer = setTimeout(
512
+ () => settle(() => reject(new Error(`${method} timed out after ${timeoutMs} ms`))),
513
+ timeoutMs,
514
+ );
515
+ this.pending.set(id, {
516
+ resolve: (value) => settle(() => resolve(value as T)),
517
+ reject: (error) => settle(() => reject(error)),
518
+ });
519
+ try {
520
+ this.ws.send(JSON.stringify({ id, method, params }));
521
+ } catch (e) {
522
+ settle(() => reject(e as Error));
523
+ }
524
+ });
525
+ }
526
+
527
+ on<T = unknown>(method: string, fn: (params: T) => void): void {
528
+ let set = this.handlers.get(method);
529
+ if (!set) {
530
+ set = new Set();
531
+ this.handlers.set(method, set);
532
+ }
533
+ set.add(fn as (params: unknown) => void);
534
+ }
535
+
536
+ /** The next `method` event `pred` accepts, or a rejection after `timeoutMs`. */
537
+ waitFor<T>(
538
+ method: string,
539
+ pred: (params: T) => boolean,
540
+ timeoutMs: number,
541
+ what: string,
542
+ ): Promise<T> {
543
+ if (this.dead) return Promise.reject(this.dead);
544
+ return new Promise<T>((resolve, reject) => {
545
+ const fn = (params: T) => {
546
+ if (!pred(params)) return;
547
+ clearTimeout(timer);
548
+ this.handlers.get(method)?.delete(fn as (params: unknown) => void);
549
+ resolve(params);
550
+ };
551
+ const timer = setTimeout(() => {
552
+ this.handlers.get(method)?.delete(fn as (params: unknown) => void);
553
+ reject(new Error(`${what} timed out after ${timeoutMs} ms`));
554
+ }, timeoutMs);
555
+ this.on(method, fn);
556
+ });
557
+ }
558
+
559
+ /** Reject every pending call and every later one with `error`. */
560
+ fail(error: Error): void {
561
+ if (this.dead) return;
562
+ this.dead = error;
563
+ for (const call of this.pending.values()) call.reject(error);
564
+ this.pending.clear();
565
+ }
566
+
567
+ close(): void {
568
+ this.fail(new Error("the browser was closed"));
569
+ try {
570
+ this.ws.close();
571
+ } catch {
572
+ /* already closed */
573
+ }
574
+ }
575
+ }