effect-start 0.22.0 → 0.23.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 (313) hide show
  1. package/dist/BlobStore.d.ts +80 -0
  2. package/dist/BlobStore.js +19 -0
  3. package/dist/ChildProcess.d.ts +60 -0
  4. package/dist/ChildProcess.js +30 -0
  5. package/dist/Commander.d.ts +3 -6
  6. package/dist/Commander.js +6 -13
  7. package/dist/ContentNegotiation.d.ts +8 -9
  8. package/dist/ContentNegotiation.js +32 -37
  9. package/dist/Cookies.d.ts +47 -0
  10. package/dist/Cookies.js +273 -363
  11. package/dist/Development.d.ts +2 -2
  12. package/dist/Development.js +3 -4
  13. package/dist/Effectify.d.ts +1 -4
  14. package/dist/FilePathPattern.d.ts +3 -3
  15. package/dist/FileRouter.d.ts +5 -8
  16. package/dist/FileRouter.js +9 -10
  17. package/dist/FileRouterCodegen.d.ts +1 -1
  18. package/dist/FileRouterCodegen.js +33 -13
  19. package/dist/FileSystem.d.ts +158 -0
  20. package/dist/FileSystem.js +64 -125
  21. package/dist/Http.js +2 -6
  22. package/dist/PathPattern.d.ts +7 -7
  23. package/dist/PathPattern.js +1 -3
  24. package/dist/PlatformError.d.ts +24 -32
  25. package/dist/PlatformError.js +3 -21
  26. package/dist/PlatformRuntime.js +5 -10
  27. package/dist/Route.d.ts +14 -19
  28. package/dist/Route.js +8 -11
  29. package/dist/RouteBody.d.ts +6 -12
  30. package/dist/RouteBody.js +2 -2
  31. package/dist/RouteError.d.ts +98 -0
  32. package/dist/RouteError.js +55 -0
  33. package/dist/RouteHook.js +6 -11
  34. package/dist/RouteHttp.d.ts +3 -3
  35. package/dist/RouteHttp.js +27 -22
  36. package/dist/RouteMount.d.ts +16 -50
  37. package/dist/RouteMount.js +6 -20
  38. package/dist/RouteSchema.d.ts +22 -1
  39. package/dist/RouteSchema.js +33 -0
  40. package/dist/RouteSse.js +4 -10
  41. package/dist/RouteTree.d.ts +2 -1
  42. package/dist/RouteTree.js +17 -15
  43. package/dist/RouteTrie.d.ts +2 -2
  44. package/dist/RouteTrie.js +4 -9
  45. package/dist/SchemaExtra.d.ts +1 -1
  46. package/dist/Socket.d.ts +27 -0
  47. package/dist/Socket.js +20 -28
  48. package/dist/Sql.d.ts +34 -0
  49. package/dist/Sql.js +5 -0
  50. package/dist/SqlIntrospect.d.ts +91 -0
  51. package/dist/SqlIntrospect.js +466 -0
  52. package/dist/Start.d.ts +4 -6
  53. package/dist/Start.js +10 -2
  54. package/dist/StreamExtra.d.ts +1 -1
  55. package/dist/StreamExtra.js +9 -9
  56. package/dist/System.d.ts +7 -0
  57. package/dist/System.js +22 -0
  58. package/dist/TuplePathPattern.js +55 -50
  59. package/dist/Unique.js +7 -7
  60. package/dist/Values.d.ts +2 -1
  61. package/dist/Values.js +19 -13
  62. package/dist/bun/BunBlobStoreDisk.d.ts +6 -0
  63. package/dist/bun/BunBlobStoreDisk.js +116 -0
  64. package/dist/bun/BunBlobStoreS3.d.ts +11 -0
  65. package/dist/bun/BunBlobStoreS3.js +89 -0
  66. package/dist/bun/BunBlobWatcherDisk.d.ts +6 -0
  67. package/dist/bun/BunBlobWatcherDisk.js +60 -0
  68. package/dist/bun/BunBlobWatcherQueue.d.ts +6 -0
  69. package/dist/bun/BunBlobWatcherQueue.js +17 -0
  70. package/dist/bun/BunBundle.d.ts +5 -6
  71. package/dist/bun/BunBundle.js +7 -15
  72. package/dist/bun/BunChildProcessSpawner.d.ts +3 -0
  73. package/dist/bun/BunChildProcessSpawner.js +103 -0
  74. package/dist/bun/BunImportTrackerPlugin.d.ts +1 -1
  75. package/dist/bun/BunImportTrackerPlugin.js +3 -5
  76. package/dist/bun/BunRoute.d.ts +3 -2
  77. package/dist/bun/BunRoute.js +5 -7
  78. package/dist/bun/BunRuntime.js +1 -1
  79. package/dist/bun/BunServer.d.ts +11 -4
  80. package/dist/bun/BunServer.js +35 -11
  81. package/dist/bun/BunSql.d.ts +4 -0
  82. package/dist/bun/BunSql.js +81 -0
  83. package/dist/bun/_BunEnhancedResolve.d.ts +3 -3
  84. package/dist/bun/_BunEnhancedResolve.js +2 -4
  85. package/dist/bun/index.d.ts +1 -0
  86. package/dist/bun/index.js +1 -0
  87. package/dist/bundler/Bundle.d.ts +2 -1
  88. package/dist/bundler/Bundle.js +1 -1
  89. package/dist/bundler/BundleFiles.d.ts +5 -5
  90. package/dist/bundler/BundleFiles.js +10 -8
  91. package/dist/bundler/BundleRoute.d.ts +27 -0
  92. package/dist/bundler/BundleRoute.js +51 -0
  93. package/dist/client/ScrollState.js +2 -6
  94. package/dist/client/index.js +6 -8
  95. package/dist/console/Console.d.ts +6 -0
  96. package/dist/console/Console.js +26 -0
  97. package/dist/console/ConsoleErrors.d.ts +3 -0
  98. package/dist/console/ConsoleErrors.js +200 -0
  99. package/dist/console/ConsoleLogger.d.ts +3 -0
  100. package/dist/console/ConsoleLogger.js +47 -0
  101. package/dist/console/ConsoleMetrics.d.ts +3 -0
  102. package/dist/console/ConsoleMetrics.js +61 -0
  103. package/dist/console/ConsoleProcess.d.ts +3 -0
  104. package/dist/console/ConsoleProcess.js +49 -0
  105. package/dist/console/ConsoleStore.d.ts +144 -0
  106. package/dist/console/ConsoleStore.js +61 -0
  107. package/dist/console/ConsoleTracer.d.ts +3 -0
  108. package/dist/console/ConsoleTracer.js +94 -0
  109. package/dist/console/Simulation.d.ts +2 -0
  110. package/dist/console/Simulation.js +633 -0
  111. package/dist/console/index.d.ts +3 -0
  112. package/dist/console/index.js +3 -0
  113. package/dist/console/routes/errors/route.d.ts +10 -0
  114. package/dist/console/routes/errors/route.js +47 -0
  115. package/dist/console/routes/fiberDetail.d.ts +16 -0
  116. package/dist/console/routes/fiberDetail.js +38 -0
  117. package/dist/console/routes/fibers/route.d.ts +10 -0
  118. package/dist/console/routes/fibers/route.js +19 -0
  119. package/dist/console/routes/git/route.d.ts +11 -0
  120. package/dist/console/routes/git/route.js +33 -0
  121. package/dist/console/routes/layout.d.ts +9 -0
  122. package/dist/console/routes/layout.js +3 -0
  123. package/dist/console/routes/logs/route.d.ts +10 -0
  124. package/dist/console/routes/logs/route.js +32 -0
  125. package/dist/console/routes/metrics/route.d.ts +10 -0
  126. package/dist/console/routes/metrics/route.js +17 -0
  127. package/dist/console/routes/route.d.ts +6 -0
  128. package/dist/console/routes/route.js +5 -0
  129. package/dist/console/routes/routes/route.d.ts +6 -0
  130. package/dist/console/routes/routes/route.js +20 -0
  131. package/dist/console/routes/services/route.d.ts +6 -0
  132. package/dist/console/routes/services/route.js +12 -0
  133. package/dist/console/routes/system/route.d.ts +10 -0
  134. package/dist/console/routes/system/route.js +18 -0
  135. package/dist/console/routes/traceDetail.d.ts +16 -0
  136. package/dist/console/routes/traceDetail.js +14 -0
  137. package/dist/console/routes/traces/route.d.ts +10 -0
  138. package/dist/console/routes/traces/route.js +39 -0
  139. package/dist/console/routes/tree.d.ts +153 -0
  140. package/dist/console/routes/tree.js +29 -0
  141. package/dist/console/ui/Errors.d.ts +4 -0
  142. package/dist/console/ui/Errors.js +15 -0
  143. package/dist/console/ui/Fibers.d.ts +24 -0
  144. package/dist/console/ui/Fibers.js +121 -0
  145. package/dist/console/ui/Git.d.ts +20 -0
  146. package/dist/console/ui/Git.js +95 -0
  147. package/dist/console/ui/Logs.d.ts +4 -0
  148. package/dist/console/ui/Logs.js +25 -0
  149. package/dist/console/ui/Metrics.d.ts +4 -0
  150. package/dist/console/ui/Metrics.js +26 -0
  151. package/dist/console/ui/Routes.d.ts +8 -0
  152. package/dist/console/ui/Routes.js +70 -0
  153. package/dist/console/ui/Services.d.ts +10 -0
  154. package/dist/console/ui/Services.js +246 -0
  155. package/dist/console/ui/Shell.d.ts +10 -0
  156. package/dist/console/ui/Shell.js +7 -0
  157. package/dist/console/ui/System.d.ts +4 -0
  158. package/dist/console/ui/System.js +35 -0
  159. package/dist/console/ui/Traces.d.ts +12 -0
  160. package/dist/console/ui/Traces.js +179 -0
  161. package/dist/datastar/actions/fetch.d.ts +1 -1
  162. package/dist/datastar/actions/fetch.js +10 -18
  163. package/dist/datastar/actions/peek.js +1 -2
  164. package/dist/datastar/actions/setAll.js +1 -2
  165. package/dist/datastar/actions/toggleAll.js +1 -2
  166. package/dist/datastar/attributes/attr.js +1 -2
  167. package/dist/datastar/attributes/bind.js +10 -18
  168. package/dist/datastar/attributes/class.js +2 -5
  169. package/dist/datastar/attributes/computed.js +2 -3
  170. package/dist/datastar/attributes/effect.js +1 -2
  171. package/dist/datastar/attributes/indicator.js +2 -4
  172. package/dist/datastar/attributes/init.js +2 -3
  173. package/dist/datastar/attributes/jsonSignals.js +1 -2
  174. package/dist/datastar/attributes/on.js +41 -22
  175. package/dist/datastar/attributes/onIntersect.js +2 -3
  176. package/dist/datastar/attributes/onInterval.js +2 -3
  177. package/dist/datastar/attributes/onSignalPatch.js +2 -4
  178. package/dist/datastar/attributes/ref.js +1 -2
  179. package/dist/datastar/attributes/show.js +1 -2
  180. package/dist/datastar/attributes/signals.js +1 -2
  181. package/dist/datastar/attributes/style.js +6 -12
  182. package/dist/datastar/attributes/text.js +1 -2
  183. package/dist/datastar/engine.d.ts +13 -7
  184. package/dist/datastar/engine.js +76 -48
  185. package/dist/datastar/happydom.d.ts +1 -0
  186. package/dist/datastar/happydom.js +8 -0
  187. package/dist/datastar/index.d.ts +1 -1
  188. package/dist/datastar/index.js +1 -1
  189. package/dist/datastar/utils.js +4 -7
  190. package/dist/datastar/watchers/patchElements.js +24 -45
  191. package/dist/datastar/watchers/patchSignals.js +1 -2
  192. package/dist/experimental/EncryptedCookies.d.ts +2 -5
  193. package/dist/experimental/EncryptedCookies.js +17 -48
  194. package/dist/experimental/index.d.ts +0 -1
  195. package/dist/experimental/index.js +0 -1
  196. package/dist/hyper/Hyper.d.ts +2 -9
  197. package/dist/hyper/Hyper.js +1 -12
  198. package/dist/hyper/HyperHtml.d.ts +1 -1
  199. package/dist/hyper/HyperHtml.js +18 -12
  200. package/dist/hyper/HyperHtml.test.d.ts +1 -0
  201. package/dist/hyper/HyperHtml.test.js +197 -0
  202. package/dist/hyper/HyperRoute.test.js +14 -3
  203. package/dist/hyper/html.d.ts +11 -0
  204. package/dist/hyper/html.js +30 -0
  205. package/dist/hyper/index.d.ts +2 -0
  206. package/dist/hyper/index.js +1 -0
  207. package/dist/hyper/jsx-runtime.d.ts +1 -1
  208. package/dist/hyper/jsx-runtime.js +1 -1
  209. package/dist/index.d.ts +1 -0
  210. package/dist/index.js +1 -0
  211. package/dist/lint/plugin.d.ts +86 -0
  212. package/dist/lint/plugin.js +341 -0
  213. package/dist/node/NodeFileSystem.d.ts +2 -2
  214. package/dist/node/NodeFileSystem.js +4 -14
  215. package/dist/sql/bun/index.d.ts +3 -0
  216. package/dist/sql/bun/index.js +75 -0
  217. package/dist/sql/mssql/docker.d.ts +2 -0
  218. package/dist/sql/mssql/docker.js +67 -0
  219. package/dist/sql/mssql/index.d.ts +21 -0
  220. package/dist/sql/mssql/index.js +113 -0
  221. package/dist/testing/TestLogger.js +4 -1
  222. package/dist/testing/index.d.ts +0 -1
  223. package/dist/testing/index.js +0 -1
  224. package/dist/testing/utils.d.ts +3 -3
  225. package/dist/testing/utils.js +4 -4
  226. package/dist/x/cloudflare/CloudflareTunnel.d.ts +2 -5
  227. package/dist/x/cloudflare/CloudflareTunnel.js +14 -27
  228. package/dist/x/datastar/Datastar.d.ts +1 -1
  229. package/dist/x/datastar/Datastar.js +13 -12
  230. package/dist/x/datastar/index.d.ts +1 -2
  231. package/dist/x/datastar/index.js +1 -2
  232. package/dist/x/tailscale/TailscaleTunnel.d.ts +15 -0
  233. package/dist/x/tailscale/TailscaleTunnel.js +68 -0
  234. package/dist/x/tailscale/index.d.ts +1 -0
  235. package/dist/x/tailscale/index.js +1 -0
  236. package/dist/x/tailwind/TailwindPlugin.js +19 -19
  237. package/dist/x/tailwind/compile.d.ts +2 -2
  238. package/dist/x/tailwind/compile.js +2 -4
  239. package/package.json +22 -10
  240. package/src/ChildProcess.ts +145 -0
  241. package/src/PlatformError.ts +27 -50
  242. package/src/Route.ts +2 -2
  243. package/src/RouteError.ts +76 -0
  244. package/src/RouteHttp.ts +13 -5
  245. package/src/RouteSchema.ts +96 -1
  246. package/src/RouteTree.ts +12 -0
  247. package/src/Sql.ts +51 -0
  248. package/src/SqlIntrospect.ts +620 -0
  249. package/src/Start.ts +15 -3
  250. package/src/System.ts +43 -0
  251. package/src/Values.ts +7 -0
  252. package/src/bun/BunChildProcessSpawner.ts +143 -0
  253. package/src/bun/BunRoute.ts +5 -2
  254. package/src/bun/BunServer.ts +22 -1
  255. package/src/bun/index.ts +1 -0
  256. package/src/bundler/BundleRoute.ts +66 -0
  257. package/src/console/Console.ts +42 -0
  258. package/src/console/ConsoleErrors.ts +213 -0
  259. package/src/console/ConsoleLogger.ts +56 -0
  260. package/src/console/ConsoleMetrics.ts +72 -0
  261. package/src/console/ConsoleProcess.ts +59 -0
  262. package/src/console/ConsoleStore.ts +187 -0
  263. package/src/console/ConsoleTracer.ts +107 -0
  264. package/src/console/Simulation.ts +814 -0
  265. package/src/console/console.html +340 -0
  266. package/src/console/index.ts +3 -0
  267. package/src/console/routes/errors/route.tsx +97 -0
  268. package/src/console/routes/fiberDetail.tsx +54 -0
  269. package/src/console/routes/fibers/route.tsx +45 -0
  270. package/src/console/routes/git/route.tsx +64 -0
  271. package/src/console/routes/layout.tsx +4 -0
  272. package/src/console/routes/logs/route.tsx +77 -0
  273. package/src/console/routes/metrics/route.tsx +36 -0
  274. package/src/console/routes/route.tsx +8 -0
  275. package/src/console/routes/routes/route.tsx +30 -0
  276. package/src/console/routes/services/route.tsx +21 -0
  277. package/src/console/routes/system/route.tsx +43 -0
  278. package/src/console/routes/traceDetail.tsx +22 -0
  279. package/src/console/routes/traces/route.tsx +81 -0
  280. package/src/console/routes/tree.ts +30 -0
  281. package/src/console/ui/Errors.tsx +76 -0
  282. package/src/console/ui/Fibers.tsx +321 -0
  283. package/src/console/ui/Git.tsx +182 -0
  284. package/src/console/ui/Logs.tsx +46 -0
  285. package/src/console/ui/Metrics.tsx +78 -0
  286. package/src/console/ui/Routes.tsx +125 -0
  287. package/src/console/ui/Services.tsx +273 -0
  288. package/src/console/ui/Shell.tsx +62 -0
  289. package/src/console/ui/System.tsx +131 -0
  290. package/src/console/ui/Traces.tsx +426 -0
  291. package/src/datastar/README.md +6 -1
  292. package/src/datastar/actions/fetch.ts +0 -1
  293. package/src/datastar/attributes/on.ts +40 -20
  294. package/src/datastar/engine.ts +51 -0
  295. package/src/datastar/jsx.d.ts +79 -0
  296. package/src/hyper/Hyper.ts +1 -16
  297. package/src/hyper/HyperHtml.ts +6 -4
  298. package/src/hyper/HyperRoute.ts +2 -1
  299. package/src/hyper/html.ts +47 -0
  300. package/src/hyper/index.ts +2 -0
  301. package/src/hyper/jsx.d.ts +5 -3
  302. package/src/index.ts +1 -0
  303. package/src/lint/plugin.js +129 -0
  304. package/src/sql/bun/index.ts +147 -0
  305. package/src/sql/mssql/docker.ts +117 -0
  306. package/src/sql/mssql/index.ts +223 -0
  307. package/src/sql/mssql/mssql.d.ts +41 -0
  308. package/src/x/cloudflare/CloudflareTunnel.ts +8 -36
  309. package/src/x/tailscale/TailscaleTunnel.ts +113 -0
  310. package/src/x/tailscale/index.ts +1 -0
  311. package/src/x/datastar/Datastar.ts +0 -61
  312. package/src/x/datastar/index.ts +0 -2
  313. package/src/x/datastar/jsx-datastar.d.ts +0 -60
@@ -0,0 +1,59 @@
1
+ import * as NOS from "node:os"
2
+ import * as Effect from "effect/Effect"
3
+ import * as Layer from "effect/Layer"
4
+ import * as PubSub from "effect/PubSub"
5
+ import * as Schedule from "effect/Schedule"
6
+ import * as ConsoleStore from "./ConsoleStore.ts"
7
+
8
+ function snapshot(): ConsoleStore.ProcessStats {
9
+ const mem = process.memoryUsage()
10
+ const cpu = process.cpuUsage()
11
+ const res = process.resourceUsage()
12
+ const loadavg = NOS.loadavg() as [number, number, number]
13
+ return {
14
+ pid: process.pid,
15
+ uptime: process.uptime(),
16
+ memory: {
17
+ rss: mem.rss,
18
+ heapUsed: mem.heapUsed,
19
+ heapTotal: mem.heapTotal,
20
+ external: mem.external,
21
+ arrayBuffers: mem.arrayBuffers,
22
+ },
23
+ cpu: { user: cpu.user, system: cpu.system },
24
+ resourceUsage: {
25
+ maxRSS: res.maxRSS,
26
+ minorPageFault: res.minorPageFault,
27
+ majorPageFault: res.majorPageFault,
28
+ fsRead: res.fsRead,
29
+ fsWrite: res.fsWrite,
30
+ voluntaryContextSwitches: res.voluntaryContextSwitches,
31
+ involuntaryContextSwitches: res.involuntaryContextSwitches,
32
+ },
33
+ system: {
34
+ loadavg,
35
+ freemem: NOS.freemem(),
36
+ totalmem: NOS.totalmem(),
37
+ cpuCount: NOS.cpus().length,
38
+ platform: NOS.platform(),
39
+ arch: NOS.arch(),
40
+ },
41
+ }
42
+ }
43
+
44
+ export const layer: Layer.Layer<never, never, ConsoleStore.ConsoleStore> = Layer.scopedDiscard(
45
+ Effect.gen(function* () {
46
+ const store = yield* ConsoleStore.ConsoleStore
47
+
48
+ yield* Effect.forkScoped(
49
+ Effect.schedule(
50
+ Effect.sync(() => {
51
+ const stats = snapshot()
52
+ store.process = stats
53
+ Effect.runSync(PubSub.publish(store.events, { _tag: "ProcessSnapshot", stats }))
54
+ }),
55
+ Schedule.spaced("2 seconds"),
56
+ ),
57
+ )
58
+ }),
59
+ )
@@ -0,0 +1,187 @@
1
+ import * as Context from "effect/Context"
2
+ import * as Effect from "effect/Effect"
3
+ import * as GlobalValue from "effect/GlobalValue"
4
+ import * as Layer from "effect/Layer"
5
+ import * as MutableRef from "effect/MutableRef"
6
+ import * as PubSub from "effect/PubSub"
7
+
8
+ export const store: ConsoleStoreShape = GlobalValue.globalValue(
9
+ Symbol.for("effect-start/ConsoleStore"),
10
+ () => ({
11
+ prefix: "/console",
12
+ spans: new CircularBuffer<ConsoleSpan>(1000),
13
+ logs: new CircularBuffer<ConsoleLog>(5000),
14
+ errors: new CircularBuffer<ConsoleError>(1000),
15
+ metrics: [] as Array<ConsoleMetricSnapshot>,
16
+ process: undefined as ProcessStats | undefined,
17
+ events: Effect.runSync(PubSub.unbounded<ConsoleEvent>()),
18
+ fiberParents: new Map<string, string>(),
19
+ fiberContexts: new Map<string, FiberContext>(),
20
+ }),
21
+ )
22
+
23
+ export interface ConsoleSpan {
24
+ readonly spanId: string
25
+ readonly traceId: string
26
+ readonly name: string
27
+ readonly kind: string
28
+ readonly parentSpanId: string | undefined
29
+ startTime: bigint
30
+ endTime: bigint | undefined
31
+ durationMs: number | undefined
32
+ status: "started" | "ok" | "error"
33
+ readonly attributes: Record<string, unknown>
34
+ readonly events: Array<{ name: string; startTime: bigint; attributes?: Record<string, unknown> }>
35
+ }
36
+
37
+ export interface ConsoleLog {
38
+ readonly id: string
39
+ readonly date: Date
40
+ readonly level: "DEBUG" | "INFO" | "WARNING" | "ERROR" | "FATAL"
41
+ readonly message: string
42
+ readonly fiberId: string
43
+ readonly cause: string | undefined
44
+ readonly spans: Array<string>
45
+ readonly annotations: Record<string, unknown>
46
+ }
47
+
48
+ export interface ProcessStats {
49
+ readonly pid: number
50
+ readonly uptime: number
51
+ readonly memory: {
52
+ readonly rss: number
53
+ readonly heapUsed: number
54
+ readonly heapTotal: number
55
+ readonly external: number
56
+ readonly arrayBuffers: number
57
+ }
58
+ readonly cpu: { readonly user: number; readonly system: number }
59
+ readonly resourceUsage: {
60
+ readonly maxRSS: number
61
+ readonly minorPageFault: number
62
+ readonly majorPageFault: number
63
+ readonly fsRead: number
64
+ readonly fsWrite: number
65
+ readonly voluntaryContextSwitches: number
66
+ readonly involuntaryContextSwitches: number
67
+ }
68
+ readonly system: {
69
+ readonly loadavg: readonly [number, number, number]
70
+ readonly freemem: number
71
+ readonly totalmem: number
72
+ readonly cpuCount: number
73
+ readonly platform: string
74
+ readonly arch: string
75
+ }
76
+ }
77
+
78
+ export interface ConsoleErrorDetail {
79
+ readonly kind: "fail" | "die"
80
+ readonly tag: string | undefined
81
+ readonly message: string
82
+ readonly properties: Record<string, unknown>
83
+ readonly span: string | undefined
84
+ }
85
+
86
+ export interface ConsoleError {
87
+ readonly id: string
88
+ readonly date: Date
89
+ readonly fiberId: string
90
+ readonly interrupted: boolean
91
+ readonly prettyPrint: string
92
+ readonly details: Array<ConsoleErrorDetail>
93
+ }
94
+
95
+ export interface ConsoleMetricSnapshot {
96
+ readonly name: string
97
+ readonly type: "counter" | "gauge" | "histogram" | "summary" | "frequency"
98
+ readonly value: unknown
99
+ readonly tags: ReadonlyArray<{ key: string; value: string }>
100
+ readonly timestamp: number
101
+ }
102
+
103
+ export type ConsoleEvent =
104
+ | { readonly _tag: "SpanStart"; readonly span: ConsoleSpan }
105
+ | { readonly _tag: "SpanEnd"; readonly span: ConsoleSpan }
106
+ | { readonly _tag: "Log"; readonly log: ConsoleLog }
107
+ | { readonly _tag: "Error"; readonly error: ConsoleError }
108
+ | { readonly _tag: "MetricsSnapshot"; readonly metrics: Array<ConsoleMetricSnapshot> }
109
+ | { readonly _tag: "ProcessSnapshot"; readonly stats: ProcessStats }
110
+
111
+ export class CircularBuffer<T> {
112
+ private buffer: Array<T | undefined>
113
+ private head = 0
114
+ private count = 0
115
+ capacity: number
116
+
117
+ constructor(capacity: number) {
118
+ this.capacity = capacity
119
+ this.buffer = Array.from({ length: capacity })
120
+ }
121
+
122
+ push(item: T): void {
123
+ this.buffer[this.head] = item
124
+ this.head = (this.head + 1) % this.capacity
125
+ if (this.count < this.capacity) this.count++
126
+ }
127
+
128
+ toArray(): Array<T> {
129
+ if (this.count === 0) return []
130
+ const result: Array<T> = []
131
+ const start = this.count < this.capacity ? 0 : this.head
132
+ for (let i = 0; i < this.count; i++) {
133
+ result.push(this.buffer[(start + i) % this.capacity]!)
134
+ }
135
+ return result
136
+ }
137
+
138
+ get size(): number {
139
+ return this.count
140
+ }
141
+ }
142
+
143
+ export interface FiberContext {
144
+ readonly spanName: string | undefined
145
+ readonly traceId: string | undefined
146
+ readonly annotations: Record<string, unknown>
147
+ }
148
+
149
+ export interface ConsoleStoreShape {
150
+ prefix: string
151
+ readonly spans: CircularBuffer<ConsoleSpan>
152
+ readonly logs: CircularBuffer<ConsoleLog>
153
+ readonly errors: CircularBuffer<ConsoleError>
154
+ metrics: Array<ConsoleMetricSnapshot>
155
+ process: ProcessStats | undefined
156
+ readonly events: PubSub.PubSub<ConsoleEvent>
157
+ readonly fiberParents: Map<string, string>
158
+ readonly fiberContexts: Map<string, FiberContext>
159
+ }
160
+
161
+ export function fiberIdCounter(): number {
162
+ const counter = GlobalValue.globalValue(Symbol.for("effect/Fiber/Id/_fiberCounter"), () =>
163
+ MutableRef.make(0),
164
+ )
165
+ return MutableRef.get(counter)
166
+ }
167
+
168
+ export class ConsoleStore extends Context.Tag("effect-start/ConsoleStore")<
169
+ ConsoleStore,
170
+ ConsoleStoreShape
171
+ >() {}
172
+
173
+ export interface ConsoleStoreOptions {
174
+ readonly spanCapacity?: number
175
+ readonly logCapacity?: number
176
+ readonly errorCapacity?: number
177
+ }
178
+
179
+ export function layer(options?: ConsoleStoreOptions): Layer.Layer<ConsoleStore> {
180
+ if (options?.spanCapacity)
181
+ (store as any).spans = new CircularBuffer<ConsoleSpan>(options.spanCapacity)
182
+ if (options?.logCapacity)
183
+ (store as any).logs = new CircularBuffer<ConsoleLog>(options.logCapacity)
184
+ if (options?.errorCapacity)
185
+ (store as any).errors = new CircularBuffer<ConsoleError>(options.errorCapacity)
186
+ return Layer.sync(ConsoleStore, () => store)
187
+ }
@@ -0,0 +1,107 @@
1
+ import * as Effect from "effect/Effect"
2
+ import * as Exit from "effect/Exit"
3
+ import * as Fiber from "effect/Fiber"
4
+ import * as FiberId from "effect/FiberId"
5
+ import * as Layer from "effect/Layer"
6
+ import * as Option from "effect/Option"
7
+ import * as PubSub from "effect/PubSub"
8
+ import * as Tracer from "effect/Tracer"
9
+ import * as ConsoleStore from "./ConsoleStore.ts"
10
+
11
+ const publish = (store: ConsoleStore.ConsoleStoreShape, event: ConsoleStore.ConsoleEvent) =>
12
+ Effect.runSync(PubSub.publish(store.events, event))
13
+
14
+ const make = (store: ConsoleStore.ConsoleStoreShape): Tracer.Tracer =>
15
+ Tracer.make({
16
+ span(name, parent, context, links, startTime, kind, options) {
17
+ const parentSpanId =
18
+ Option.isSome(parent) && parent.value._tag === "Span" ? parent.value.spanId : undefined
19
+ const traceId = Option.isSome(parent)
20
+ ? parent.value.traceId
21
+ : Math.random().toString(36).slice(2) + Math.random().toString(36).slice(2)
22
+ const spanId = Math.random().toString(36).slice(2)
23
+
24
+ const attributes: Record<string, unknown> = {}
25
+ const currentFiber = Fiber.getCurrentFiber()
26
+ if (Option.isSome(currentFiber)) {
27
+ attributes["fiber.id"] = FiberId.threadName(currentFiber.value.id())
28
+ }
29
+ if (typeof options?.captureStackTrace === "function") {
30
+ const stacktrace = options.captureStackTrace()
31
+ if (stacktrace) {
32
+ attributes["code.stacktrace"] = stacktrace
33
+ }
34
+ }
35
+
36
+ const consoleSpan: ConsoleStore.ConsoleSpan = {
37
+ spanId,
38
+ traceId,
39
+ name,
40
+ kind,
41
+ parentSpanId,
42
+ startTime,
43
+ endTime: undefined,
44
+ durationMs: undefined,
45
+ status: "started",
46
+ attributes,
47
+ events: [],
48
+ }
49
+
50
+ store.spans.push(consoleSpan)
51
+ publish(store, { _tag: "SpanStart", span: consoleSpan })
52
+
53
+ const attrs = new Map<string, unknown>(Object.entries(attributes))
54
+ const spanLinks = [...links]
55
+
56
+ const span: Tracer.Span = {
57
+ _tag: "Span",
58
+ name,
59
+ spanId,
60
+ traceId,
61
+ parent,
62
+ context,
63
+ get status(): Tracer.SpanStatus {
64
+ if (consoleSpan.endTime != null) {
65
+ return {
66
+ _tag: "Ended",
67
+ startTime: consoleSpan.startTime,
68
+ endTime: consoleSpan.endTime,
69
+ exit: Exit.void,
70
+ }
71
+ }
72
+ return { _tag: "Started", startTime: consoleSpan.startTime }
73
+ },
74
+ attributes: attrs,
75
+ links: spanLinks,
76
+ sampled: true,
77
+ kind,
78
+ end(endTime, exit) {
79
+ consoleSpan.endTime = endTime
80
+ consoleSpan.durationMs = Number(endTime - consoleSpan.startTime) / 1_000_000
81
+ consoleSpan.status = Exit.isSuccess(exit) ? "ok" : "error"
82
+ publish(store, { _tag: "SpanEnd", span: consoleSpan })
83
+ },
84
+ attribute(key, value) {
85
+ attrs.set(key, value)
86
+ ;(consoleSpan.attributes as Record<string, unknown>)[key] = value
87
+ },
88
+ event(name, startTime, attributes) {
89
+ consoleSpan.events.push({ name, startTime, attributes })
90
+ },
91
+ addLinks(newLinks) {
92
+ spanLinks.push(...newLinks)
93
+ },
94
+ }
95
+ return span
96
+ },
97
+ context(f) {
98
+ return f()
99
+ },
100
+ })
101
+
102
+ export const layer: Layer.Layer<never, never, ConsoleStore.ConsoleStore> = Layer.unwrapEffect(
103
+ Effect.gen(function* () {
104
+ const store = yield* ConsoleStore.ConsoleStore
105
+ return Layer.setTracer(make(store))
106
+ }),
107
+ )