effect-start 0.18.0 → 0.20.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 (203) hide show
  1. package/README.md +3 -3
  2. package/dist/Development.d.ts +8 -3
  3. package/dist/Development.js +14 -7
  4. package/dist/Effectify.d.ts +212 -0
  5. package/dist/Effectify.js +19 -0
  6. package/dist/FilePathPattern.d.ts +29 -0
  7. package/dist/FilePathPattern.js +86 -0
  8. package/dist/FileRouter.d.ts +39 -41
  9. package/dist/FileRouter.js +104 -158
  10. package/dist/FileRouterCodegen.d.ts +7 -8
  11. package/dist/FileRouterCodegen.js +97 -66
  12. package/dist/PlatformError.d.ts +46 -0
  13. package/dist/PlatformError.js +43 -0
  14. package/dist/PlatformRuntime.d.ts +27 -0
  15. package/dist/PlatformRuntime.js +51 -0
  16. package/dist/Route.d.ts +6 -2
  17. package/dist/Route.js +22 -0
  18. package/dist/RouteBody.d.ts +1 -1
  19. package/dist/RouteHttp.d.ts +1 -1
  20. package/dist/RouteHttp.js +12 -19
  21. package/dist/RouteMount.d.ts +2 -1
  22. package/dist/Start.d.ts +33 -6
  23. package/dist/Start.js +31 -13
  24. package/dist/Unique.d.ts +50 -0
  25. package/dist/Unique.js +187 -0
  26. package/dist/bun/BunHttpServer.js +5 -6
  27. package/dist/bun/BunPlatformHttpServer.d.ts +10 -0
  28. package/dist/bun/BunPlatformHttpServer.js +53 -0
  29. package/dist/bun/BunRoute.d.ts +4 -6
  30. package/dist/bun/BunRoute.js +10 -18
  31. package/dist/bun/BunRuntime.d.ts +2 -1
  32. package/dist/bun/BunRuntime.js +10 -5
  33. package/dist/bun/BunServer.d.ts +33 -0
  34. package/dist/bun/BunServer.js +133 -0
  35. package/dist/bun/BunServerRequest.d.ts +60 -0
  36. package/dist/bun/BunServerRequest.js +252 -0
  37. package/dist/bun/index.d.ts +1 -1
  38. package/dist/bun/index.js +1 -1
  39. package/dist/datastar/actions/fetch.d.ts +30 -0
  40. package/dist/datastar/actions/fetch.js +411 -0
  41. package/dist/datastar/actions/peek.d.ts +1 -0
  42. package/dist/datastar/actions/peek.js +14 -0
  43. package/dist/datastar/actions/setAll.d.ts +1 -0
  44. package/dist/datastar/actions/setAll.js +13 -0
  45. package/dist/datastar/actions/toggleAll.d.ts +1 -0
  46. package/dist/datastar/actions/toggleAll.js +13 -0
  47. package/dist/datastar/attributes/attr.d.ts +1 -0
  48. package/dist/datastar/attributes/attr.js +49 -0
  49. package/dist/datastar/attributes/bind.d.ts +1 -0
  50. package/dist/datastar/attributes/bind.js +183 -0
  51. package/dist/datastar/attributes/class.d.ts +1 -0
  52. package/dist/datastar/attributes/class.js +50 -0
  53. package/dist/datastar/attributes/computed.d.ts +1 -0
  54. package/dist/datastar/attributes/computed.js +27 -0
  55. package/dist/datastar/attributes/effect.d.ts +1 -0
  56. package/dist/datastar/attributes/effect.js +10 -0
  57. package/dist/datastar/attributes/indicator.d.ts +1 -0
  58. package/dist/datastar/attributes/indicator.js +32 -0
  59. package/dist/datastar/attributes/init.d.ts +1 -0
  60. package/dist/datastar/attributes/init.js +27 -0
  61. package/dist/datastar/attributes/jsonSignals.d.ts +1 -0
  62. package/dist/datastar/attributes/jsonSignals.js +31 -0
  63. package/dist/datastar/attributes/on.d.ts +1 -0
  64. package/dist/datastar/attributes/on.js +59 -0
  65. package/dist/datastar/attributes/onIntersect.d.ts +1 -0
  66. package/dist/datastar/attributes/onIntersect.js +54 -0
  67. package/dist/datastar/attributes/onInterval.d.ts +1 -0
  68. package/dist/datastar/attributes/onInterval.js +31 -0
  69. package/dist/datastar/attributes/onSignalPatch.d.ts +1 -0
  70. package/dist/datastar/attributes/onSignalPatch.js +44 -0
  71. package/dist/datastar/attributes/ref.d.ts +1 -0
  72. package/dist/datastar/attributes/ref.js +11 -0
  73. package/dist/datastar/attributes/show.d.ts +1 -0
  74. package/dist/datastar/attributes/show.js +32 -0
  75. package/dist/datastar/attributes/signals.d.ts +1 -0
  76. package/dist/datastar/attributes/signals.js +18 -0
  77. package/dist/datastar/attributes/style.d.ts +1 -0
  78. package/dist/datastar/attributes/style.js +56 -0
  79. package/dist/datastar/attributes/text.d.ts +1 -0
  80. package/dist/datastar/attributes/text.js +27 -0
  81. package/dist/datastar/engine.d.ts +156 -0
  82. package/dist/datastar/engine.js +971 -0
  83. package/dist/datastar/index.d.ts +24 -0
  84. package/dist/datastar/index.js +24 -0
  85. package/dist/datastar/load.d.ts +24 -0
  86. package/dist/datastar/load.js +24 -0
  87. package/dist/datastar/utils.d.ts +51 -0
  88. package/dist/datastar/utils.js +205 -0
  89. package/dist/datastar/watchers/patchElements.d.ts +1 -0
  90. package/dist/datastar/watchers/patchElements.js +420 -0
  91. package/dist/datastar/watchers/patchSignals.d.ts +1 -0
  92. package/dist/datastar/watchers/patchSignals.js +15 -0
  93. package/dist/index.d.ts +1 -0
  94. package/dist/index.js +1 -0
  95. package/dist/node/Effectify.d.ts +209 -0
  96. package/dist/node/Effectify.js +19 -0
  97. package/dist/node/FileSystem.d.ts +3 -5
  98. package/dist/node/FileSystem.js +42 -62
  99. package/dist/node/NodeFileSystem.d.ts +7 -0
  100. package/dist/node/NodeFileSystem.js +420 -0
  101. package/dist/node/NodeUtils.d.ts +2 -0
  102. package/dist/node/NodeUtils.js +20 -0
  103. package/dist/node/PlatformError.d.ts +46 -0
  104. package/dist/node/PlatformError.js +43 -0
  105. package/dist/testing/TestLogger.js +1 -1
  106. package/dist/x/tailwind/plugin.js +1 -1
  107. package/package.json +18 -7
  108. package/src/Development.ts +36 -40
  109. package/src/Effectify.ts +269 -0
  110. package/src/FilePathPattern.ts +115 -0
  111. package/src/FileRouter.ts +178 -255
  112. package/src/FileRouterCodegen.ts +135 -92
  113. package/src/PlatformError.ts +117 -0
  114. package/src/PlatformRuntime.ts +108 -0
  115. package/src/Route.ts +31 -2
  116. package/src/RouteBody.ts +1 -1
  117. package/src/RouteHttp.ts +15 -29
  118. package/src/RouteMount.ts +1 -1
  119. package/src/Start.ts +61 -27
  120. package/src/Unique.ts +232 -0
  121. package/src/bun/BunPlatformHttpServer.ts +88 -0
  122. package/src/bun/BunRoute.ts +14 -24
  123. package/src/bun/BunRuntime.ts +21 -5
  124. package/src/bun/BunServer.ts +228 -0
  125. package/src/bun/index.ts +1 -1
  126. package/src/datastar/README.md +18 -0
  127. package/src/datastar/actions/fetch.ts +609 -0
  128. package/src/datastar/actions/peek.ts +17 -0
  129. package/src/datastar/actions/setAll.ts +20 -0
  130. package/src/datastar/actions/toggleAll.ts +20 -0
  131. package/src/datastar/attributes/attr.ts +50 -0
  132. package/src/datastar/attributes/bind.ts +220 -0
  133. package/src/datastar/attributes/class.ts +57 -0
  134. package/src/datastar/attributes/computed.ts +33 -0
  135. package/src/datastar/attributes/effect.ts +11 -0
  136. package/src/datastar/attributes/indicator.ts +39 -0
  137. package/src/datastar/attributes/init.ts +35 -0
  138. package/src/datastar/attributes/jsonSignals.ts +38 -0
  139. package/src/datastar/attributes/on.ts +71 -0
  140. package/src/datastar/attributes/onIntersect.ts +65 -0
  141. package/src/datastar/attributes/onInterval.ts +39 -0
  142. package/src/datastar/attributes/onSignalPatch.ts +63 -0
  143. package/src/datastar/attributes/ref.ts +12 -0
  144. package/src/datastar/attributes/show.ts +33 -0
  145. package/src/datastar/attributes/signals.ts +22 -0
  146. package/src/datastar/attributes/style.ts +63 -0
  147. package/src/datastar/attributes/text.ts +30 -0
  148. package/src/datastar/engine.ts +1341 -0
  149. package/src/datastar/index.ts +25 -0
  150. package/src/datastar/utils.ts +286 -0
  151. package/src/datastar/watchers/patchElements.ts +554 -0
  152. package/src/datastar/watchers/patchSignals.ts +15 -0
  153. package/src/index.ts +1 -0
  154. package/src/node/{FileSystem.ts → NodeFileSystem.ts} +59 -97
  155. package/src/node/{Utils.ts → NodeUtils.ts} +2 -0
  156. package/src/testing/TestLogger.ts +1 -1
  157. package/src/x/tailwind/plugin.ts +1 -1
  158. package/dist/Random.d.ts +0 -5
  159. package/dist/Random.js +0 -49
  160. package/src/Commander.test.ts +0 -1639
  161. package/src/ContentNegotiation.test.ts +0 -603
  162. package/src/Development.test.ts +0 -119
  163. package/src/Entity.test.ts +0 -592
  164. package/src/FileRouterCodegen.todo.ts +0 -1133
  165. package/src/FileRouterPattern.test.ts +0 -147
  166. package/src/FileRouterPattern.ts +0 -59
  167. package/src/FileRouter_files.test.ts +0 -64
  168. package/src/FileRouter_path.test.ts +0 -145
  169. package/src/FileRouter_tree.test.ts +0 -132
  170. package/src/Http.test.ts +0 -319
  171. package/src/HttpAppExtra.test.ts +0 -103
  172. package/src/HttpUtils.test.ts +0 -85
  173. package/src/PathPattern.test.ts +0 -648
  174. package/src/Random.ts +0 -59
  175. package/src/RouteBody.test.ts +0 -232
  176. package/src/RouteHook.test.ts +0 -40
  177. package/src/RouteHttp.test.ts +0 -2909
  178. package/src/RouteMount.test.ts +0 -481
  179. package/src/RouteSchema.test.ts +0 -427
  180. package/src/RouteSse.test.ts +0 -249
  181. package/src/RouteTree.test.ts +0 -494
  182. package/src/RouteTrie.test.ts +0 -322
  183. package/src/RouterPattern.test.ts +0 -676
  184. package/src/RouterPattern.ts +0 -416
  185. package/src/StartApp.ts +0 -47
  186. package/src/Values.test.ts +0 -263
  187. package/src/bun/BunBundle.test.ts +0 -268
  188. package/src/bun/BunBundle_imports.test.ts +0 -48
  189. package/src/bun/BunHttpServer.test.ts +0 -251
  190. package/src/bun/BunHttpServer.ts +0 -306
  191. package/src/bun/BunImportTrackerPlugin.test.ts +0 -77
  192. package/src/bun/BunRoute.test.ts +0 -162
  193. package/src/bundler/BundleHttp.test.ts +0 -132
  194. package/src/effect/HttpRouter.test.ts +0 -548
  195. package/src/experimental/EncryptedCookies.test.ts +0 -488
  196. package/src/hyper/HyperHtml.test.ts +0 -209
  197. package/src/hyper/HyperRoute.test.tsx +0 -197
  198. package/src/middlewares/BasicAuthMiddleware.test.ts +0 -84
  199. package/src/testing/TestHttpClient.test.ts +0 -83
  200. package/src/testing/TestLogger.test.ts +0 -51
  201. package/src/x/datastar/Datastar.test.ts +0 -266
  202. package/src/x/tailwind/TailwindPlugin.test.ts +0 -333
  203. /package/src/bun/{BunHttpServer_web.ts → BunServerRequest.ts} +0 -0
@@ -1,427 +0,0 @@
1
- import * as test from "bun:test"
2
- import {
3
- Effect,
4
- Schema,
5
- } from "effect"
6
- import * as Http from "./Http.ts"
7
- import * as Route from "./Route.ts"
8
- import * as RouteHttp from "./RouteHttp.ts"
9
- import * as RouteMount from "./RouteMount.ts"
10
- import * as RouteSchema from "./RouteSchema.ts"
11
- import * as TestLogger from "./testing/TestLogger.ts"
12
-
13
- test.describe(`${RouteSchema.schemaHeaders.name}()`, () => {
14
- test.it(`${RouteSchema.schemaHeaders.name} merges`, () => {
15
- type ExpectedBindings = {
16
- hello: string
17
- "x-custom-header": string
18
- }
19
- const route = Route
20
- .use(
21
- RouteSchema.schemaHeaders(
22
- Schema.Struct({
23
- "hello": Schema.String,
24
- }),
25
- ),
26
- )
27
- .get(
28
- RouteSchema.schemaHeaders(
29
- Schema.Struct({
30
- "x-custom-header": Schema.String,
31
- }),
32
- ),
33
- Route.html(function*(ctx) {
34
- test
35
- .expectTypeOf(ctx.headers)
36
- .toExtend<ExpectedBindings>()
37
-
38
- return `<h1>Hello, world!</h1>`
39
- }),
40
- )
41
-
42
- test
43
- .expectTypeOf<Route.Route.Context<typeof route>>()
44
- .toExtend<{
45
- headers: ExpectedBindings
46
- }>()
47
- })
48
-
49
- test.it("passes bindings and parses value", async () => {
50
- const headers = {
51
- "x-hello": "test-value",
52
- }
53
- type ExpectedBindings = {
54
- headers: typeof headers
55
- }
56
-
57
- const route = RouteMount.get(
58
- RouteSchema.schemaHeaders(
59
- Schema.Struct({
60
- "x-hello": Schema.String,
61
- }),
62
- ),
63
- Route.text((context) =>
64
- Effect.gen(function*() {
65
- test
66
- .expectTypeOf(context)
67
- .toExtend<ExpectedBindings>()
68
-
69
- test
70
- .expect(context)
71
- .toMatchObject({
72
- headers,
73
- })
74
-
75
- return "Hello, World!"
76
- })
77
- ),
78
- )
79
-
80
- test
81
- .expectTypeOf<Route.Route.Context<typeof route>>()
82
- .toExtend<ExpectedBindings>()
83
- })
84
- })
85
-
86
- test.describe(`${RouteSchema.schemaCookies.name}()`, () => {
87
- test.it("merges cookies from schema", () => {
88
- type ExpectedBindings = {
89
- session: string
90
- token: string
91
- }
92
- const route = Route
93
- .use(
94
- RouteSchema.schemaCookies(
95
- Schema.Struct({
96
- "session": Schema.String,
97
- }),
98
- ),
99
- )
100
- .get(
101
- RouteSchema.schemaCookies(
102
- Schema.Struct({
103
- "token": Schema.String,
104
- }),
105
- ),
106
- Route.html(function*(ctx) {
107
- test
108
- .expectTypeOf(ctx.cookies)
109
- .toExtend<ExpectedBindings>()
110
-
111
- return `<h1>Hello, world!</h1>`
112
- }),
113
- )
114
-
115
- test
116
- .expectTypeOf<Route.Route.Context<typeof route>>()
117
- .toExtend<{
118
- cookies: ExpectedBindings
119
- }>()
120
- })
121
-
122
- test.it("parses cookies from request", async () => {
123
- const handler = RouteHttp.toWebHandler(
124
- Route.get(
125
- RouteSchema.schemaCookies(
126
- Schema.Struct({
127
- "session": Schema.String,
128
- }),
129
- ),
130
- Route.text(function*(ctx) {
131
- return `session=${ctx.cookies.session}`
132
- }),
133
- ),
134
- )
135
- const response = await Http.fetch(handler, {
136
- path: "/test",
137
- headers: {
138
- "Cookie": "session=abc123",
139
- },
140
- })
141
-
142
- test
143
- .expect(response.status)
144
- .toBe(200)
145
- test
146
- .expect(await response.text())
147
- .toBe("session=abc123")
148
- })
149
- })
150
-
151
- test.describe(`${RouteSchema.schemaSearchParams.name}()`, () => {
152
- test.it("merges search params from schema", () => {
153
- type ExpectedBindings = {
154
- page: string
155
- limit: string
156
- }
157
- const route = Route
158
- .use(
159
- RouteSchema.schemaSearchParams(
160
- Schema.Struct({
161
- "page": Schema.String,
162
- }),
163
- ),
164
- )
165
- .get(
166
- RouteSchema.schemaSearchParams(
167
- Schema.Struct({
168
- "limit": Schema.String,
169
- }),
170
- ),
171
- Route.html(function*(ctx) {
172
- test
173
- .expectTypeOf(ctx.searchParams)
174
- .toExtend<ExpectedBindings>()
175
-
176
- return `<h1>Hello, world!</h1>`
177
- }),
178
- )
179
-
180
- test
181
- .expectTypeOf<Route.Route.Context<typeof route>>()
182
- .toExtend<{
183
- searchParams: ExpectedBindings
184
- }>()
185
- })
186
-
187
- test.it("parses search params from request", async () => {
188
- const handler = RouteHttp.toWebHandler(
189
- Route.get(
190
- RouteSchema.schemaSearchParams(
191
- Schema.Struct({
192
- "page": Schema.NumberFromString,
193
- "limit": Schema.NumberFromString,
194
- }),
195
- ),
196
- Route.text(function*(ctx) {
197
- return `page=${ctx.searchParams.page},limit=${ctx.searchParams.limit}`
198
- }),
199
- ),
200
- )
201
- const response = await Http.fetch(handler, {
202
- path: "/test?page=2&limit=10",
203
- })
204
-
205
- test
206
- .expect(response.status)
207
- .toBe(200)
208
- test
209
- .expect(await response.text())
210
- .toBe("page=2,limit=10")
211
- })
212
- })
213
-
214
- test.describe(`${RouteSchema.schemaBodyJson.name}()`, () => {
215
- test.it("parses JSON body from schema", () => {
216
- type ExpectedBindings = {
217
- name: string
218
- age: number
219
- }
220
- const route = Route
221
- .post(
222
- RouteSchema.schemaBodyJson(
223
- Schema.Struct({
224
- name: Schema.String,
225
- age: Schema.Number,
226
- }),
227
- ),
228
- Route.json(function*(ctx) {
229
- test
230
- .expectTypeOf(ctx.body)
231
- .toExtend<ExpectedBindings>()
232
-
233
- return { received: ctx.body }
234
- }),
235
- )
236
-
237
- test
238
- .expectTypeOf<Route.Route.Context<typeof route>>()
239
- .toExtend<{
240
- body: ExpectedBindings
241
- }>()
242
- })
243
-
244
- test.it("parses JSON body from request", async () => {
245
- const handler = RouteHttp.toWebHandler(
246
- Route.post(
247
- RouteSchema.schemaBodyJson(
248
- Schema.Struct({
249
- name: Schema.String,
250
- age: Schema.Number,
251
- }),
252
- ),
253
- Route.json(function*(ctx) {
254
- return { name: ctx.body.name, age: ctx.body.age }
255
- }),
256
- ),
257
- )
258
- const response = await Http.fetch(handler, {
259
- path: "/test",
260
- method: "POST",
261
- headers: {
262
- "Content-Type": "application/json",
263
- },
264
- body: JSON.stringify({ name: "Alice", age: 30 }),
265
- })
266
-
267
- test
268
- .expect(response.status)
269
- .toBe(200)
270
- test
271
- .expect(await response.json())
272
- .toEqual({ name: "Alice", age: 30 })
273
- })
274
-
275
- test.it("returns error for invalid JSON body", () =>
276
- Effect
277
- .gen(function*() {
278
- const runtime = yield* Effect.runtime<TestLogger.TestLogger>()
279
- const handler = RouteHttp.toWebHandlerRuntime(runtime)(
280
- Route.post(
281
- RouteSchema.schemaBodyJson(
282
- Schema.Struct({
283
- name: Schema.String,
284
- age: Schema.Number,
285
- }),
286
- ),
287
- Route.json(function*(ctx) {
288
- return ctx.body
289
- }),
290
- ),
291
- )
292
- const response = yield* Effect.promise(() =>
293
- Http.fetch(handler, {
294
- path: "/test",
295
- method: "POST",
296
- headers: {
297
- "Content-Type": "application/json",
298
- },
299
- body: JSON.stringify({ name: "Alice", age: "not a number" }),
300
- })
301
- )
302
-
303
- test
304
- .expect(response.status)
305
- .toBe(400)
306
-
307
- const messages = yield* TestLogger.messages
308
- test
309
- .expect(messages.some((m) => m.includes("ParseError")))
310
- .toBe(true)
311
- })
312
- .pipe(Effect.provide(TestLogger.layer()), Effect.runPromise))
313
- })
314
-
315
- test.describe(`${RouteSchema.schemaBodyUrlParams.name}()`, () => {
316
- test.it("parses URL params body from schema", () => {
317
- type ExpectedBindings = {
318
- username: string
319
- password: string
320
- }
321
- const route = Route
322
- .post(
323
- RouteSchema.schemaBodyUrlParams(
324
- Schema.Struct({
325
- username: Schema.String,
326
- password: Schema.String,
327
- }),
328
- ),
329
- Route.json(function*(ctx) {
330
- test
331
- .expectTypeOf(ctx.body)
332
- .toExtend<ExpectedBindings>()
333
-
334
- return { received: ctx.body }
335
- }),
336
- )
337
-
338
- test
339
- .expectTypeOf<Route.Route.Context<typeof route>>()
340
- .toExtend<{
341
- body: ExpectedBindings
342
- }>()
343
- })
344
-
345
- test.it("parses URL-encoded body from request", async () => {
346
- const handler = RouteHttp.toWebHandler(
347
- Route.post(
348
- RouteSchema.schemaBodyUrlParams(
349
- Schema.Struct({
350
- username: Schema.String,
351
- password: Schema.String,
352
- }),
353
- ),
354
- Route.json(function*(ctx) {
355
- return { username: ctx.body.username, password: ctx.body.password }
356
- }),
357
- ),
358
- )
359
- const response = await Http.fetch(handler, {
360
- path: "/test",
361
- method: "POST",
362
- headers: {
363
- "Content-Type": "application/x-www-form-urlencoded",
364
- },
365
- body: "username=alice&password=secret123",
366
- })
367
-
368
- test
369
- .expect(response.status)
370
- .toBe(200)
371
- test
372
- .expect(await response.json())
373
- .toEqual({ username: "alice", password: "secret123" })
374
- })
375
- })
376
-
377
- test.describe(`${RouteSchema.schemaBodyMultipart.name}()`, () => {
378
- test.it("has correct type signature for multipart body", () => {
379
- const route = Route
380
- .post(
381
- RouteSchema.schemaBodyMultipart(
382
- Schema.Struct({
383
- name: Schema.String,
384
- }),
385
- ),
386
- Route.json(function*(ctx) {
387
- test
388
- .expectTypeOf(ctx.body)
389
- .toExtend<{ name: string }>()
390
-
391
- return { name: ctx.body.name }
392
- }),
393
- )
394
-
395
- test
396
- .expectTypeOf<Route.Route.Context<typeof route>>()
397
- .toExtend<{
398
- body: { name: string }
399
- }>()
400
- })
401
- })
402
-
403
- test.describe(`${RouteSchema.schemaBodyForm.name}()`, () => {
404
- test.it("has correct type signature for form body", () => {
405
- const route = Route
406
- .post(
407
- RouteSchema.schemaBodyForm(
408
- Schema.Struct({
409
- email: Schema.String,
410
- }),
411
- ),
412
- Route.json(function*(ctx) {
413
- test
414
- .expectTypeOf(ctx.body)
415
- .toExtend<{ email: string }>()
416
-
417
- return { email: ctx.body.email }
418
- }),
419
- )
420
-
421
- test
422
- .expectTypeOf<Route.Route.Context<typeof route>>()
423
- .toExtend<{
424
- body: { email: string }
425
- }>()
426
- })
427
- })
@@ -1,249 +0,0 @@
1
- import * as test from "bun:test"
2
- import * as Context from "effect/Context"
3
- import * as Effect from "effect/Effect"
4
- import * as Layer from "effect/Layer"
5
- import * as Runtime from "effect/Runtime"
6
- import * as Stream from "effect/Stream"
7
- import * as Http from "./Http.ts"
8
- import * as Route from "./Route.ts"
9
- import * as RouteHttp from "./RouteHttp.ts"
10
- import * as RouteMount from "./RouteMount.ts"
11
-
12
- test.describe("Route.sse()", () => {
13
- test.it("infers format as text", () => {
14
- RouteMount.get(
15
- Route.sse(function*(ctx) {
16
- test
17
- .expectTypeOf(ctx)
18
- .toExtend<{
19
- method: "GET"
20
- format: "text"
21
- }>()
22
-
23
- return Stream.make({ data: "hello" })
24
- }),
25
- )
26
- })
27
-
28
- test.it("accepts object events with data only", () => {
29
- RouteMount.get(
30
- Route.sse(() =>
31
- Stream.make(
32
- { data: "hello" },
33
- { data: "world" },
34
- )
35
- ),
36
- )
37
- })
38
-
39
- test.it("accepts object events with data and event", () => {
40
- RouteMount.get(
41
- Route.sse(() =>
42
- Stream.make(
43
- { data: "hello", event: "message" },
44
- { data: "world", event: "update" },
45
- )
46
- ),
47
- )
48
- })
49
-
50
- test.it("accepts events with retry", () => {
51
- RouteMount.get(
52
- Route.sse(() =>
53
- Stream.make(
54
- { data: "hello", retry: 3000 },
55
- )
56
- ),
57
- )
58
- })
59
-
60
- test.it("accepts Effect returning Stream", () => {
61
- RouteMount.get(
62
- Route.sse(
63
- Effect.succeed(Stream.make({ data: "hello" })),
64
- ),
65
- )
66
- })
67
-
68
- test.it("accepts generator returning Stream", () => {
69
- RouteMount.get(
70
- Route.sse(function*() {
71
- const prefix = yield* Effect.succeed("msg: ")
72
- return Stream.make({ data: `${prefix}hello` })
73
- }),
74
- )
75
- })
76
-
77
- test.it("formats data events correctly", async () => {
78
- const handler = RouteHttp.toWebHandler(
79
- Route.get(
80
- Route.sse(() => Stream.make({ data: "hello" }, { data: "world" })),
81
- ),
82
- )
83
- const response = await Http.fetch(handler, { path: "/events" })
84
-
85
- test.expect(response.headers.get("content-type")).toBe("text/event-stream")
86
- test.expect(response.headers.get("cache-control")).toBe("no-cache")
87
- test.expect(response.headers.get("connection")).toBe("keep-alive")
88
-
89
- const text = await response.text()
90
- test.expect(text).toBe("data: hello\n\ndata: world\n\n")
91
- })
92
-
93
- test.it("formats events with event field", async () => {
94
- const handler = RouteHttp.toWebHandler(
95
- Route.get(
96
- Route.sse(() => Stream.make({ data: "payload", event: "custom" })),
97
- ),
98
- )
99
- const response = await Http.fetch(handler, { path: "/events" })
100
-
101
- const text = await response.text()
102
- test.expect(text).toBe("event: custom\ndata: payload\n\n")
103
- })
104
-
105
- test.it("formats events with retry", async () => {
106
- const handler = RouteHttp.toWebHandler(
107
- Route.get(
108
- Route.sse(() => Stream.make({ data: "hello", retry: 5000 })),
109
- ),
110
- )
111
- const response = await Http.fetch(handler, { path: "/events" })
112
-
113
- const text = await response.text()
114
- test.expect(text).toBe("data: hello\nretry: 5000\n\n")
115
- })
116
-
117
- test.it("formats multi-line data with multiple data fields", async () => {
118
- const handler = RouteHttp.toWebHandler(
119
- Route.get(
120
- Route.sse(() =>
121
- Stream.make({
122
- event: "patch",
123
- data: "line1\nline2\nline3",
124
- })
125
- ),
126
- ),
127
- )
128
- const response = await Http.fetch(handler, { path: "/events" })
129
-
130
- const text = await response.text()
131
- test.expect(text).toBe(
132
- "event: patch\ndata: line1\ndata: line2\ndata: line3\n\n",
133
- )
134
- })
135
-
136
- test.it("accepts Stream directly", async () => {
137
- const handler = RouteHttp.toWebHandler(
138
- Route.get(
139
- Route.sse(
140
- Stream.make({ data: "direct" }),
141
- ),
142
- ),
143
- )
144
- const response = await Http.fetch(handler, { path: "/events" })
145
-
146
- const text = await response.text()
147
- test.expect(text).toBe("data: direct\n\n")
148
- })
149
-
150
- test.it("infers error type from Stream", () => {
151
- class MyError {
152
- readonly _tag = "MyError"
153
- }
154
-
155
- const stream: Stream.Stream<{ data: string }, MyError> = Stream.fail(
156
- new MyError(),
157
- )
158
-
159
- const route = Route.get(
160
- Route.sse(stream),
161
- )
162
-
163
- const routes = [...route]
164
- type RouteError = (typeof routes)[0] extends
165
- Route.Route.Route<any, any, any, infer E, any> ? E
166
- : never
167
-
168
- test.expectTypeOf<RouteError>().toEqualTypeOf<MyError>()
169
- })
170
-
171
- test.it("infers context type from Stream", () => {
172
- class Config extends Context.Tag("Config")<Config, { url: string }>() {}
173
-
174
- const stream = Stream.fromEffect(
175
- Effect.map(Config, (cfg) => ({ data: cfg.url })),
176
- )
177
-
178
- const route = Route.get(
179
- Route.sse(stream),
180
- )
181
-
182
- const routes = [...route]
183
- type RouteContext = (typeof routes)[0] extends
184
- Route.Route.Route<any, any, any, any, infer R> ? R
185
- : never
186
-
187
- test.expectTypeOf<RouteContext>().toEqualTypeOf<Config>()
188
- })
189
-
190
- test.it("works with context at runtime", async () => {
191
- class Config extends Context.Tag("Config")<Config, { message: string }>() {}
192
-
193
- const stream = Stream.fromEffect(
194
- Effect.map(Config, (cfg) => ({ data: cfg.message })),
195
- )
196
-
197
- const route = Route.get(Route.sse(stream))
198
- const layer = Layer.succeed(Config, { message: "from context" })
199
- const runtime = Effect.runSync(Layer.toRuntime(layer).pipe(Effect.scoped))
200
- const handler = RouteHttp.toWebHandlerRuntime(runtime)(route)
201
-
202
- const response = await Http.fetch(handler, { path: "/events" })
203
- const text = await response.text()
204
-
205
- test.expect(text).toBe("data: from context\n\n")
206
- })
207
-
208
- test.it("formats tagged struct as event with JSON data", async () => {
209
- const handler = RouteHttp.toWebHandler(
210
- Route.get(
211
- Route.sse(() =>
212
- Stream.make(
213
- { _tag: "UserCreated", id: 123, name: "Alice" },
214
- { _tag: "UserUpdated", id: 123, active: true },
215
- )
216
- ),
217
- ),
218
- )
219
- const response = await Http.fetch(handler, { path: "/events" })
220
-
221
- const text = await response.text()
222
- test.expect(text).toBe(
223
- `event: UserCreated\ndata: {"_tag":"UserCreated","id":123,"name":"Alice"}\n\n`
224
- + `event: UserUpdated\ndata: {"_tag":"UserUpdated","id":123,"active":true}\n\n`,
225
- )
226
- })
227
-
228
- test.it("handles mixed tagged and regular events", async () => {
229
- const handler = RouteHttp.toWebHandler(
230
- Route.get(
231
- Route.sse(() =>
232
- Stream.make(
233
- { data: "plain message" },
234
- { _tag: "Notification", text: "hello" },
235
- { data: "another", event: "custom" },
236
- )
237
- ),
238
- ),
239
- )
240
- const response = await Http.fetch(handler, { path: "/events" })
241
-
242
- const text = await response.text()
243
- test.expect(text).toBe(
244
- `data: plain message\n\n`
245
- + `event: Notification\ndata: {"_tag":"Notification","text":"hello"}\n\n`
246
- + `event: custom\ndata: another\n\n`,
247
- )
248
- })
249
- })