@effect/platform-bun 0.87.0 → 4.0.0-beta.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 (286) hide show
  1. package/dist/BunChildProcessSpawner.d.ts +7 -0
  2. package/dist/BunChildProcessSpawner.d.ts.map +1 -0
  3. package/dist/BunChildProcessSpawner.js +7 -0
  4. package/dist/BunChildProcessSpawner.js.map +1 -0
  5. package/dist/BunClusterHttp.d.ts +45 -0
  6. package/dist/BunClusterHttp.d.ts.map +1 -0
  7. package/dist/BunClusterHttp.js +48 -0
  8. package/dist/BunClusterHttp.js.map +1 -0
  9. package/dist/BunClusterSocket.d.ts +46 -0
  10. package/dist/BunClusterSocket.d.ts.map +1 -0
  11. package/dist/BunClusterSocket.js +60 -0
  12. package/dist/BunClusterSocket.js.map +1 -0
  13. package/dist/BunFileSystem.d.ts +8 -0
  14. package/dist/BunFileSystem.d.ts.map +1 -0
  15. package/dist/BunFileSystem.js.map +1 -0
  16. package/dist/BunHttpClient.d.ts +9 -0
  17. package/dist/BunHttpClient.d.ts.map +1 -0
  18. package/dist/BunHttpClient.js +9 -0
  19. package/dist/BunHttpClient.js.map +1 -0
  20. package/dist/BunHttpPlatform.d.ts +8 -0
  21. package/dist/BunHttpPlatform.d.ts.map +1 -0
  22. package/dist/BunHttpPlatform.js +35 -0
  23. package/dist/BunHttpPlatform.js.map +1 -0
  24. package/dist/BunHttpServer.d.ts +71 -0
  25. package/dist/BunHttpServer.d.ts.map +1 -0
  26. package/dist/{esm/internal/httpServer.js → BunHttpServer.js} +149 -118
  27. package/dist/BunHttpServer.js.map +1 -0
  28. package/dist/BunHttpServerRequest.d.ts +10 -0
  29. package/dist/BunHttpServerRequest.d.ts.map +1 -0
  30. package/dist/BunHttpServerRequest.js +6 -0
  31. package/dist/BunHttpServerRequest.js.map +1 -0
  32. package/dist/BunMultipart.d.ts +20 -0
  33. package/dist/BunMultipart.d.ts.map +1 -0
  34. package/dist/BunMultipart.js +17 -0
  35. package/dist/BunMultipart.js.map +1 -0
  36. package/dist/BunPath.d.ts +18 -0
  37. package/dist/BunPath.d.ts.map +1 -0
  38. package/dist/BunPath.js.map +1 -0
  39. package/dist/BunRedis.d.ts +35 -0
  40. package/dist/BunRedis.d.ts.map +1 -0
  41. package/dist/BunRedis.js +51 -0
  42. package/dist/BunRedis.js.map +1 -0
  43. package/dist/BunRuntime.d.ts +94 -0
  44. package/dist/BunRuntime.d.ts.map +1 -0
  45. package/dist/BunRuntime.js +33 -0
  46. package/dist/BunRuntime.js.map +1 -0
  47. package/dist/BunServices.d.ts +20 -0
  48. package/dist/BunServices.d.ts.map +1 -0
  49. package/dist/BunServices.js +12 -0
  50. package/dist/BunServices.js.map +1 -0
  51. package/dist/BunSink.d.ts.map +1 -0
  52. package/dist/BunSink.js.map +1 -0
  53. package/dist/BunSocket.d.ts +26 -0
  54. package/dist/BunSocket.d.ts.map +1 -0
  55. package/dist/BunSocket.js +18 -0
  56. package/dist/BunSocket.js.map +1 -0
  57. package/dist/BunSocketServer.d.ts.map +1 -0
  58. package/dist/BunSocketServer.js.map +1 -0
  59. package/dist/BunStdio.d.ts +8 -0
  60. package/dist/BunStdio.d.ts.map +1 -0
  61. package/dist/BunStdio.js +10 -0
  62. package/dist/BunStdio.js.map +1 -0
  63. package/dist/BunStream.d.ts +18 -0
  64. package/dist/BunStream.d.ts.map +1 -0
  65. package/dist/BunStream.js +44 -0
  66. package/dist/BunStream.js.map +1 -0
  67. package/dist/{dts/BunTerminal.d.ts → BunTerminal.d.ts} +2 -2
  68. package/dist/BunTerminal.d.ts.map +1 -0
  69. package/dist/{esm/BunTerminal.js → BunTerminal.js} +1 -1
  70. package/dist/BunTerminal.js.map +1 -0
  71. package/dist/BunWorker.d.ts +13 -0
  72. package/dist/BunWorker.d.ts.map +1 -0
  73. package/dist/BunWorker.js +59 -0
  74. package/dist/BunWorker.js.map +1 -0
  75. package/dist/BunWorkerRunner.d.ts +8 -0
  76. package/dist/BunWorkerRunner.d.ts.map +1 -0
  77. package/dist/BunWorkerRunner.js +88 -0
  78. package/dist/BunWorkerRunner.js.map +1 -0
  79. package/dist/index.d.ts +90 -0
  80. package/dist/index.d.ts.map +1 -0
  81. package/dist/{esm/index.js → index.js} +21 -7
  82. package/dist/index.js.map +1 -0
  83. package/package.json +51 -195
  84. package/src/BunChildProcessSpawner.ts +6 -0
  85. package/src/BunClusterHttp.ts +48 -35
  86. package/src/BunClusterSocket.ts +55 -21
  87. package/src/BunFileSystem.ts +3 -3
  88. package/src/BunHttpClient.ts +9 -0
  89. package/src/BunHttpPlatform.ts +28 -9
  90. package/src/BunHttpServer.ts +510 -58
  91. package/src/BunHttpServerRequest.ts +4 -4
  92. package/src/BunMultipart.ts +22 -11
  93. package/src/BunPath.ts +5 -6
  94. package/src/BunRedis.ts +74 -0
  95. package/src/BunRuntime.ts +95 -3
  96. package/src/BunServices.ts +33 -0
  97. package/src/BunSocket.ts +20 -10
  98. package/src/BunStdio.ts +12 -0
  99. package/src/BunStream.ts +51 -0
  100. package/src/BunTerminal.ts +2 -2
  101. package/src/BunWorker.ts +63 -21
  102. package/src/BunWorkerRunner.ts +92 -11
  103. package/src/index.ts +37 -19
  104. package/BunClusterHttp/package.json +0 -6
  105. package/BunClusterSocket/package.json +0 -6
  106. package/BunCommandExecutor/package.json +0 -6
  107. package/BunContext/package.json +0 -6
  108. package/BunFileSystem/ParcelWatcher/package.json +0 -6
  109. package/BunFileSystem/package.json +0 -6
  110. package/BunHttpPlatform/package.json +0 -6
  111. package/BunHttpServer/package.json +0 -6
  112. package/BunHttpServerRequest/package.json +0 -6
  113. package/BunKeyValueStore/package.json +0 -6
  114. package/BunMultipart/package.json +0 -6
  115. package/BunPath/package.json +0 -6
  116. package/BunRuntime/package.json +0 -6
  117. package/BunSink/package.json +0 -6
  118. package/BunSocket/package.json +0 -6
  119. package/BunSocketServer/package.json +0 -6
  120. package/BunStream/package.json +0 -6
  121. package/BunTerminal/package.json +0 -6
  122. package/BunWorker/package.json +0 -6
  123. package/BunWorkerRunner/package.json +0 -6
  124. package/dist/cjs/BunClusterHttp.js +0 -59
  125. package/dist/cjs/BunClusterHttp.js.map +0 -1
  126. package/dist/cjs/BunClusterSocket.js +0 -55
  127. package/dist/cjs/BunClusterSocket.js.map +0 -1
  128. package/dist/cjs/BunCommandExecutor.js +0 -18
  129. package/dist/cjs/BunCommandExecutor.js.map +0 -1
  130. package/dist/cjs/BunContext.js +0 -20
  131. package/dist/cjs/BunContext.js.map +0 -1
  132. package/dist/cjs/BunFileSystem/ParcelWatcher.js +0 -18
  133. package/dist/cjs/BunFileSystem/ParcelWatcher.js.map +0 -1
  134. package/dist/cjs/BunFileSystem.js +0 -18
  135. package/dist/cjs/BunFileSystem.js.map +0 -1
  136. package/dist/cjs/BunHttpPlatform.js +0 -19
  137. package/dist/cjs/BunHttpPlatform.js.map +0 -1
  138. package/dist/cjs/BunHttpServer.js +0 -48
  139. package/dist/cjs/BunHttpServer.js.map +0 -1
  140. package/dist/cjs/BunHttpServerRequest.js +0 -14
  141. package/dist/cjs/BunHttpServerRequest.js.map +0 -1
  142. package/dist/cjs/BunKeyValueStore.js +0 -18
  143. package/dist/cjs/BunKeyValueStore.js.map +0 -1
  144. package/dist/cjs/BunMultipart.js +0 -19
  145. package/dist/cjs/BunMultipart.js.map +0 -1
  146. package/dist/cjs/BunPath.js +0 -28
  147. package/dist/cjs/BunPath.js.map +0 -1
  148. package/dist/cjs/BunRuntime.js +0 -18
  149. package/dist/cjs/BunRuntime.js.map +0 -1
  150. package/dist/cjs/BunSink.js +0 -17
  151. package/dist/cjs/BunSink.js.map +0 -1
  152. package/dist/cjs/BunSocket.js +0 -45
  153. package/dist/cjs/BunSocket.js.map +0 -1
  154. package/dist/cjs/BunSocketServer.js +0 -17
  155. package/dist/cjs/BunSocketServer.js.map +0 -1
  156. package/dist/cjs/BunStream.js +0 -17
  157. package/dist/cjs/BunStream.js.map +0 -1
  158. package/dist/cjs/BunTerminal.js +0 -23
  159. package/dist/cjs/BunTerminal.js.map +0 -1
  160. package/dist/cjs/BunWorker.js +0 -29
  161. package/dist/cjs/BunWorker.js.map +0 -1
  162. package/dist/cjs/BunWorkerRunner.js +0 -21
  163. package/dist/cjs/BunWorkerRunner.js.map +0 -1
  164. package/dist/cjs/index.js +0 -46
  165. package/dist/cjs/index.js.map +0 -1
  166. package/dist/cjs/internal/httpPlatform.js +0 -36
  167. package/dist/cjs/internal/httpPlatform.js.map +0 -1
  168. package/dist/cjs/internal/httpServer.js +0 -356
  169. package/dist/cjs/internal/httpServer.js.map +0 -1
  170. package/dist/cjs/internal/multipart.js +0 -160
  171. package/dist/cjs/internal/multipart.js.map +0 -1
  172. package/dist/cjs/internal/worker.js +0 -63
  173. package/dist/cjs/internal/worker.js.map +0 -1
  174. package/dist/cjs/internal/workerRunner.js +0 -84
  175. package/dist/cjs/internal/workerRunner.js.map +0 -1
  176. package/dist/dts/BunClusterHttp.d.ts +0 -30
  177. package/dist/dts/BunClusterHttp.d.ts.map +0 -1
  178. package/dist/dts/BunClusterSocket.d.ts +0 -35
  179. package/dist/dts/BunClusterSocket.d.ts.map +0 -1
  180. package/dist/dts/BunCommandExecutor.d.ts +0 -9
  181. package/dist/dts/BunCommandExecutor.d.ts.map +0 -1
  182. package/dist/dts/BunContext.d.ts +0 -20
  183. package/dist/dts/BunContext.d.ts.map +0 -1
  184. package/dist/dts/BunFileSystem/ParcelWatcher.d.ts +0 -8
  185. package/dist/dts/BunFileSystem/ParcelWatcher.d.ts.map +0 -1
  186. package/dist/dts/BunFileSystem.d.ts +0 -8
  187. package/dist/dts/BunFileSystem.d.ts.map +0 -1
  188. package/dist/dts/BunHttpPlatform.d.ts +0 -19
  189. package/dist/dts/BunHttpPlatform.d.ts.map +0 -1
  190. package/dist/dts/BunHttpServer.d.ts +0 -72
  191. package/dist/dts/BunHttpServer.d.ts.map +0 -1
  192. package/dist/dts/BunHttpServerRequest.d.ts +0 -10
  193. package/dist/dts/BunHttpServerRequest.d.ts.map +0 -1
  194. package/dist/dts/BunKeyValueStore.d.ts +0 -9
  195. package/dist/dts/BunKeyValueStore.d.ts.map +0 -1
  196. package/dist/dts/BunMultipart.d.ts +0 -20
  197. package/dist/dts/BunMultipart.d.ts.map +0 -1
  198. package/dist/dts/BunPath.d.ts +0 -21
  199. package/dist/dts/BunPath.d.ts.map +0 -1
  200. package/dist/dts/BunRuntime.d.ts +0 -7
  201. package/dist/dts/BunRuntime.d.ts.map +0 -1
  202. package/dist/dts/BunSink.d.ts.map +0 -1
  203. package/dist/dts/BunSocket.d.ts +0 -22
  204. package/dist/dts/BunSocket.d.ts.map +0 -1
  205. package/dist/dts/BunSocketServer.d.ts.map +0 -1
  206. package/dist/dts/BunStream.d.ts +0 -8
  207. package/dist/dts/BunStream.d.ts.map +0 -1
  208. package/dist/dts/BunTerminal.d.ts.map +0 -1
  209. package/dist/dts/BunWorker.d.ts +0 -26
  210. package/dist/dts/BunWorker.d.ts.map +0 -1
  211. package/dist/dts/BunWorkerRunner.d.ts +0 -17
  212. package/dist/dts/BunWorkerRunner.d.ts.map +0 -1
  213. package/dist/dts/index.d.ts +0 -77
  214. package/dist/dts/index.d.ts.map +0 -1
  215. package/dist/dts/internal/httpPlatform.d.ts +0 -2
  216. package/dist/dts/internal/httpPlatform.d.ts.map +0 -1
  217. package/dist/dts/internal/httpServer.d.ts +0 -2
  218. package/dist/dts/internal/httpServer.d.ts.map +0 -1
  219. package/dist/dts/internal/multipart.d.ts +0 -2
  220. package/dist/dts/internal/multipart.d.ts.map +0 -1
  221. package/dist/dts/internal/worker.d.ts +0 -2
  222. package/dist/dts/internal/worker.d.ts.map +0 -1
  223. package/dist/dts/internal/workerRunner.d.ts +0 -2
  224. package/dist/dts/internal/workerRunner.d.ts.map +0 -1
  225. package/dist/esm/BunClusterHttp.js +0 -50
  226. package/dist/esm/BunClusterHttp.js.map +0 -1
  227. package/dist/esm/BunClusterSocket.js +0 -45
  228. package/dist/esm/BunClusterSocket.js.map +0 -1
  229. package/dist/esm/BunCommandExecutor.js +0 -10
  230. package/dist/esm/BunCommandExecutor.js.map +0 -1
  231. package/dist/esm/BunContext.js +0 -13
  232. package/dist/esm/BunContext.js.map +0 -1
  233. package/dist/esm/BunFileSystem/ParcelWatcher.js +0 -10
  234. package/dist/esm/BunFileSystem/ParcelWatcher.js.map +0 -1
  235. package/dist/esm/BunFileSystem.js.map +0 -1
  236. package/dist/esm/BunHttpPlatform.js +0 -12
  237. package/dist/esm/BunHttpPlatform.js.map +0 -1
  238. package/dist/esm/BunHttpServer.js +0 -41
  239. package/dist/esm/BunHttpServer.js.map +0 -1
  240. package/dist/esm/BunHttpServerRequest.js +0 -7
  241. package/dist/esm/BunHttpServerRequest.js.map +0 -1
  242. package/dist/esm/BunKeyValueStore.js +0 -10
  243. package/dist/esm/BunKeyValueStore.js.map +0 -1
  244. package/dist/esm/BunMultipart.js +0 -12
  245. package/dist/esm/BunMultipart.js.map +0 -1
  246. package/dist/esm/BunPath.js.map +0 -1
  247. package/dist/esm/BunRuntime.js +0 -10
  248. package/dist/esm/BunRuntime.js.map +0 -1
  249. package/dist/esm/BunSink.js.map +0 -1
  250. package/dist/esm/BunSocket.js +0 -20
  251. package/dist/esm/BunSocket.js.map +0 -1
  252. package/dist/esm/BunSocketServer.js.map +0 -1
  253. package/dist/esm/BunStream.js +0 -8
  254. package/dist/esm/BunStream.js.map +0 -1
  255. package/dist/esm/BunTerminal.js.map +0 -1
  256. package/dist/esm/BunWorker.js +0 -22
  257. package/dist/esm/BunWorker.js.map +0 -1
  258. package/dist/esm/BunWorkerRunner.js +0 -13
  259. package/dist/esm/BunWorkerRunner.js.map +0 -1
  260. package/dist/esm/index.js.map +0 -1
  261. package/dist/esm/internal/httpPlatform.js +0 -29
  262. package/dist/esm/internal/httpPlatform.js.map +0 -1
  263. package/dist/esm/internal/httpServer.js.map +0 -1
  264. package/dist/esm/internal/multipart.js +0 -151
  265. package/dist/esm/internal/multipart.js.map +0 -1
  266. package/dist/esm/internal/worker.js +0 -54
  267. package/dist/esm/internal/worker.js.map +0 -1
  268. package/dist/esm/internal/workerRunner.js +0 -77
  269. package/dist/esm/internal/workerRunner.js.map +0 -1
  270. package/dist/esm/package.json +0 -4
  271. package/index/package.json +0 -6
  272. package/src/BunCommandExecutor.ts +0 -13
  273. package/src/BunContext.ts +0 -40
  274. package/src/BunFileSystem/ParcelWatcher.ts +0 -12
  275. package/src/BunKeyValueStore.ts +0 -15
  276. package/src/internal/httpPlatform.ts +0 -25
  277. package/src/internal/httpServer.ts +0 -478
  278. package/src/internal/multipart.ts +0 -149
  279. package/src/internal/worker.ts +0 -68
  280. package/src/internal/workerRunner.ts +0 -87
  281. /package/dist/{esm/BunFileSystem.js → BunFileSystem.js} +0 -0
  282. /package/dist/{esm/BunPath.js → BunPath.js} +0 -0
  283. /package/dist/{dts/BunSink.d.ts → BunSink.d.ts} +0 -0
  284. /package/dist/{esm/BunSink.js → BunSink.js} +0 -0
  285. /package/dist/{dts/BunSocketServer.d.ts → BunSocketServer.d.ts} +0 -0
  286. /package/dist/{esm/BunSocketServer.js → BunSocketServer.js} +0 -0
@@ -1,478 +0,0 @@
1
- import * as Cookies from "@effect/platform/Cookies"
2
- import * as Etag from "@effect/platform/Etag"
3
- import * as FetchHttpClient from "@effect/platform/FetchHttpClient"
4
- import type * as FileSystem from "@effect/platform/FileSystem"
5
- import * as Headers from "@effect/platform/Headers"
6
- import * as App from "@effect/platform/HttpApp"
7
- import * as IncomingMessage from "@effect/platform/HttpIncomingMessage"
8
- import type { HttpMethod } from "@effect/platform/HttpMethod"
9
- import * as Server from "@effect/platform/HttpServer"
10
- import * as Error from "@effect/platform/HttpServerError"
11
- import * as ServerRequest from "@effect/platform/HttpServerRequest"
12
- import type * as ServerResponse from "@effect/platform/HttpServerResponse"
13
- import type * as Multipart from "@effect/platform/Multipart"
14
- import type * as Path from "@effect/platform/Path"
15
- import * as Socket from "@effect/platform/Socket"
16
- import * as UrlParams from "@effect/platform/UrlParams"
17
- import type { Server as BunServer, ServerWebSocket, WebSocketServeOptions } from "bun"
18
- import * as Config from "effect/Config"
19
- import * as Deferred from "effect/Deferred"
20
- import * as Effect from "effect/Effect"
21
- import * as Exit from "effect/Exit"
22
- import * as FiberSet from "effect/FiberSet"
23
- import * as Inspectable from "effect/Inspectable"
24
- import * as Layer from "effect/Layer"
25
- import * as Option from "effect/Option"
26
- import type { ReadonlyRecord } from "effect/Record"
27
- import type * as Runtime from "effect/Runtime"
28
- import type * as Scope from "effect/Scope"
29
- import * as Stream from "effect/Stream"
30
- import * as BunContext from "../BunContext.js"
31
- import * as Platform from "../BunHttpPlatform.js"
32
- import type * as BunHttpServer from "../BunHttpServer.js"
33
- import * as MultipartBun from "./multipart.js"
34
-
35
- /** @internal */
36
- export const make = <R extends { [K in keyof R]: Bun.RouterTypes.RouteValue<Extract<K, string>> } = {}>(
37
- options: BunHttpServer.ServeOptions<R>
38
- ): Effect.Effect<Server.HttpServer, never, Scope.Scope> =>
39
- Effect.gen(function*() {
40
- const handlerStack: Array<(request: Request, server: BunServer) => Response | Promise<Response>> = [
41
- function(_request, _server) {
42
- return new Response("not found", { status: 404 })
43
- }
44
- ]
45
- const server = Bun.serve<WebSocketContext, R>({
46
- ...options as WebSocketServeOptions<WebSocketContext>,
47
- fetch: handlerStack[0],
48
- websocket: {
49
- open(ws) {
50
- Deferred.unsafeDone(ws.data.deferred, Exit.succeed(ws))
51
- },
52
- message(ws, message) {
53
- ws.data.run(message)
54
- },
55
- close(ws, code, closeReason) {
56
- Deferred.unsafeDone(
57
- ws.data.closeDeferred,
58
- Socket.defaultCloseCodeIsError(code)
59
- ? Exit.fail(new Socket.SocketCloseError({ reason: "Close", code, closeReason }))
60
- : Exit.void
61
- )
62
- }
63
- }
64
- })
65
-
66
- yield* Effect.addFinalizer(() =>
67
- Effect.sync(() => {
68
- server.stop()
69
- })
70
- )
71
-
72
- return Server.make({
73
- address: { _tag: "TcpAddress", port: server.port!, hostname: server.hostname! },
74
- serve(httpApp, middleware) {
75
- return Effect.gen(function*() {
76
- const runFork = yield* FiberSet.makeRuntime<never>()
77
- const runtime = yield* Effect.runtime<never>()
78
- const app = App.toHandled(httpApp, (request, response) =>
79
- Effect.sync(() => {
80
- ;(request as ServerRequestImpl).resolve(makeResponse(request, response, runtime))
81
- }), middleware)
82
-
83
- function handler(request: Request, server: BunServer) {
84
- return new Promise<Response>((resolve, _reject) => {
85
- const fiber = runFork(Effect.provideService(
86
- app,
87
- ServerRequest.HttpServerRequest,
88
- new ServerRequestImpl(request, resolve, removeHost(request.url), server)
89
- ))
90
- request.signal.addEventListener("abort", () => {
91
- runFork(fiber.interruptAsFork(Error.clientAbortFiberId))
92
- }, { once: true })
93
- })
94
- }
95
-
96
- yield* Effect.acquireRelease(
97
- Effect.sync(() => {
98
- handlerStack.push(handler)
99
- server.reload({ fetch: handler })
100
- }),
101
- () =>
102
- Effect.sync(() => {
103
- handlerStack.pop()
104
- server.reload({ fetch: handlerStack[handlerStack.length - 1] })
105
- })
106
- )
107
- })
108
- }
109
- })
110
- })
111
-
112
- const makeResponse = (
113
- request: ServerRequest.HttpServerRequest,
114
- response: ServerResponse.HttpServerResponse,
115
- runtime: Runtime.Runtime<never>
116
- ): Response => {
117
- const fields: {
118
- headers: globalThis.Headers
119
- status?: number
120
- statusText?: string
121
- } = {
122
- headers: new globalThis.Headers(response.headers),
123
- status: response.status
124
- }
125
-
126
- if (!Cookies.isEmpty(response.cookies)) {
127
- for (const header of Cookies.toSetCookieHeaders(response.cookies)) {
128
- fields.headers.append("set-cookie", header)
129
- }
130
- }
131
-
132
- if (response.statusText !== undefined) {
133
- fields.statusText = response.statusText
134
- }
135
-
136
- if (request.method === "HEAD") {
137
- return new Response(undefined, fields)
138
- }
139
- response = App.unsafeEjectStreamScope(response)
140
- const body = response.body
141
- switch (body._tag) {
142
- case "Empty": {
143
- return new Response(undefined, fields)
144
- }
145
- case "Uint8Array":
146
- case "Raw": {
147
- if (body.body instanceof Response) {
148
- for (const [key, value] of fields.headers.entries()) {
149
- body.body.headers.set(key, value)
150
- }
151
- return body.body
152
- }
153
- return new Response(body.body as any, fields)
154
- }
155
- case "FormData": {
156
- return new Response(body.formData as any, fields)
157
- }
158
- case "Stream": {
159
- return new Response(
160
- Stream.toReadableStreamRuntime(body.stream, runtime),
161
- fields
162
- )
163
- }
164
- }
165
- }
166
-
167
- /** @internal */
168
- export const layerServer = <R extends { [K in keyof R]: Bun.RouterTypes.RouteValue<Extract<K, string>> } = {}>(
169
- options: BunHttpServer.ServeOptions<R>
170
- ) => Layer.scoped(Server.HttpServer, make(options))
171
-
172
- /** @internal */
173
- export const layerContext = Layer.mergeAll(
174
- Platform.layer,
175
- Etag.layerWeak,
176
- BunContext.layer
177
- )
178
-
179
- /** @internal */
180
- export const layer = <R extends { [K in keyof R]: Bun.RouterTypes.RouteValue<Extract<K, string>> } = {}>(
181
- options: BunHttpServer.ServeOptions<R>
182
- ) =>
183
- Layer.mergeAll(
184
- Layer.scoped(Server.HttpServer, make(options)),
185
- layerContext
186
- )
187
-
188
- /** @internal */
189
- export const layerTest = Server.layerTestClient.pipe(
190
- Layer.provide(FetchHttpClient.layer.pipe(
191
- Layer.provide(Layer.succeed(FetchHttpClient.RequestInit, { keepalive: false }))
192
- )),
193
- Layer.provideMerge(layer({ port: 0 }))
194
- )
195
-
196
- /** @internal */
197
- export const layerConfig = <R extends { [K in keyof R]: Bun.RouterTypes.RouteValue<Extract<K, string>> } = {}>(
198
- options: Config.Config.Wrap<BunHttpServer.ServeOptions<R>>
199
- ) =>
200
- Layer.mergeAll(
201
- Layer.scoped(Server.HttpServer, Effect.flatMap(Config.unwrap(options), make)),
202
- layerContext
203
- )
204
-
205
- interface WebSocketContext {
206
- readonly deferred: Deferred.Deferred<ServerWebSocket<WebSocketContext>>
207
- readonly closeDeferred: Deferred.Deferred<void, Socket.SocketError>
208
- readonly buffer: Array<Uint8Array | string>
209
- run: (_: Uint8Array | string) => void
210
- }
211
-
212
- function wsDefaultRun(this: WebSocketContext, _: Uint8Array | string) {
213
- this.buffer.push(_)
214
- }
215
-
216
- class ServerRequestImpl extends Inspectable.Class implements ServerRequest.HttpServerRequest {
217
- readonly [ServerRequest.TypeId]: ServerRequest.TypeId
218
- readonly [IncomingMessage.TypeId]: IncomingMessage.TypeId
219
- constructor(
220
- readonly source: Request,
221
- public resolve: (response: Response) => void,
222
- readonly url: string,
223
- private bunServer: BunServer,
224
- public headersOverride?: Headers.Headers,
225
- private remoteAddressOverride?: string
226
- ) {
227
- super()
228
- this[ServerRequest.TypeId] = ServerRequest.TypeId
229
- this[IncomingMessage.TypeId] = IncomingMessage.TypeId
230
- }
231
- toJSON(): unknown {
232
- return IncomingMessage.inspect(this, {
233
- _id: "@effect/platform/HttpServerRequest",
234
- method: this.method,
235
- url: this.originalUrl
236
- })
237
- }
238
- modify(
239
- options: {
240
- readonly url?: string | undefined
241
- readonly headers?: Headers.Headers | undefined
242
- readonly remoteAddress?: string | undefined
243
- }
244
- ) {
245
- return new ServerRequestImpl(
246
- this.source,
247
- this.resolve,
248
- options.url ?? this.url,
249
- this.bunServer,
250
- options.headers ?? this.headersOverride,
251
- options.remoteAddress ?? this.remoteAddressOverride
252
- )
253
- }
254
- get method(): HttpMethod {
255
- return this.source.method.toUpperCase() as HttpMethod
256
- }
257
- get originalUrl() {
258
- return this.source.url
259
- }
260
- get remoteAddress(): Option.Option<string> {
261
- return this.remoteAddressOverride
262
- ? Option.some(this.remoteAddressOverride)
263
- : Option.fromNullable(this.bunServer.requestIP(this.source)?.address)
264
- }
265
- get headers(): Headers.Headers {
266
- this.headersOverride ??= Headers.fromInput(this.source.headers)
267
- return this.headersOverride
268
- }
269
-
270
- private cachedCookies: ReadonlyRecord<string, string> | undefined
271
- get cookies() {
272
- if (this.cachedCookies) {
273
- return this.cachedCookies
274
- }
275
- return this.cachedCookies = Cookies.parseHeader(this.headers.cookie ?? "")
276
- }
277
-
278
- get stream(): Stream.Stream<Uint8Array, Error.RequestError> {
279
- return this.source.body
280
- ? Stream.fromReadableStream(() => this.source.body as any, (cause) =>
281
- new Error.RequestError({
282
- request: this,
283
- reason: "Decode",
284
- cause
285
- }))
286
- : Stream.fail(
287
- new Error.RequestError({
288
- request: this,
289
- reason: "Decode",
290
- description: "can not create stream from empty body"
291
- })
292
- )
293
- }
294
-
295
- private textEffect: Effect.Effect<string, Error.RequestError> | undefined
296
- get text(): Effect.Effect<string, Error.RequestError> {
297
- if (this.textEffect) {
298
- return this.textEffect
299
- }
300
- this.textEffect = Effect.runSync(Effect.cached(
301
- Effect.tryPromise({
302
- try: () => this.source.text(),
303
- catch: (cause) =>
304
- new Error.RequestError({
305
- request: this,
306
- reason: "Decode",
307
- cause
308
- })
309
- })
310
- ))
311
- return this.textEffect
312
- }
313
-
314
- get json(): Effect.Effect<unknown, Error.RequestError> {
315
- return Effect.tryMap(this.text, {
316
- try: (_) => JSON.parse(_) as unknown,
317
- catch: (cause) =>
318
- new Error.RequestError({
319
- request: this,
320
- reason: "Decode",
321
- cause
322
- })
323
- })
324
- }
325
-
326
- get urlParamsBody(): Effect.Effect<UrlParams.UrlParams, Error.RequestError> {
327
- return Effect.flatMap(this.text, (_) =>
328
- Effect.try({
329
- try: () => UrlParams.fromInput(new URLSearchParams(_)),
330
- catch: (cause) =>
331
- new Error.RequestError({
332
- request: this,
333
- reason: "Decode",
334
- cause
335
- })
336
- }))
337
- }
338
-
339
- private multipartEffect:
340
- | Effect.Effect<
341
- Multipart.Persisted,
342
- Multipart.MultipartError,
343
- Scope.Scope | FileSystem.FileSystem | Path.Path
344
- >
345
- | undefined
346
- get multipart(): Effect.Effect<
347
- Multipart.Persisted,
348
- Multipart.MultipartError,
349
- Scope.Scope | FileSystem.FileSystem | Path.Path
350
- > {
351
- if (this.multipartEffect) {
352
- return this.multipartEffect
353
- }
354
- this.multipartEffect = Effect.runSync(Effect.cached(
355
- MultipartBun.persisted(this.source)
356
- ))
357
- return this.multipartEffect
358
- }
359
-
360
- get multipartStream(): Stream.Stream<Multipart.Part, Multipart.MultipartError> {
361
- return MultipartBun.stream(this.source)
362
- }
363
-
364
- private arrayBufferEffect: Effect.Effect<ArrayBuffer, Error.RequestError> | undefined
365
- get arrayBuffer(): Effect.Effect<ArrayBuffer, Error.RequestError> {
366
- if (this.arrayBufferEffect) {
367
- return this.arrayBufferEffect
368
- }
369
- this.arrayBufferEffect = Effect.runSync(Effect.cached(
370
- Effect.tryPromise({
371
- try: () => this.source.arrayBuffer(),
372
- catch: (cause) =>
373
- new Error.RequestError({
374
- request: this,
375
- reason: "Decode",
376
- cause
377
- })
378
- })
379
- ))
380
- return this.arrayBufferEffect
381
- }
382
-
383
- get upgrade(): Effect.Effect<Socket.Socket, Error.RequestError> {
384
- return Effect.flatMap(
385
- Effect.all([
386
- Deferred.make<ServerWebSocket<WebSocketContext>>(),
387
- Deferred.make<void, Socket.SocketError>(),
388
- Effect.makeSemaphore(1)
389
- ]),
390
- ([deferred, closeDeferred, semaphore]) =>
391
- Effect.async<Socket.Socket, Error.RequestError>((resume) => {
392
- const success = this.bunServer.upgrade<WebSocketContext>(this.source, {
393
- data: {
394
- deferred,
395
- closeDeferred,
396
- buffer: [],
397
- run: wsDefaultRun
398
- }
399
- })
400
- if (!success) {
401
- resume(Effect.fail(
402
- new Error.RequestError({
403
- request: this,
404
- reason: "Decode",
405
- description: "Not an upgradeable ServerRequest"
406
- })
407
- ))
408
- return
409
- }
410
- resume(Effect.map(Deferred.await(deferred), (ws) => {
411
- const write = (chunk: Uint8Array | string | Socket.CloseEvent) =>
412
- Effect.sync(() => {
413
- if (typeof chunk === "string") {
414
- ws.sendText(chunk)
415
- } else if (Socket.isCloseEvent(chunk)) {
416
- ws.close(chunk.code, chunk.reason)
417
- } else {
418
- ws.sendBinary(chunk)
419
- }
420
-
421
- return true
422
- })
423
- const writer = Effect.succeed(write)
424
- const runRaw = Effect.fnUntraced(
425
- function*<R, E, _>(
426
- handler: (_: Uint8Array | string) => Effect.Effect<_, E, R> | void,
427
- opts?: { readonly onOpen?: Effect.Effect<void> | undefined }
428
- ) {
429
- const set = yield* FiberSet.make<any, E>()
430
- const run = yield* FiberSet.runtime(set)<R>()
431
- function runRaw(data: Uint8Array | string) {
432
- const result = handler(data)
433
- if (Effect.isEffect(result)) {
434
- run(result)
435
- }
436
- }
437
- ws.data.run = runRaw
438
- ws.data.buffer.forEach(runRaw)
439
- ws.data.buffer.length = 0
440
- if (opts?.onOpen) yield* opts.onOpen
441
- return yield* FiberSet.join(set)
442
- },
443
- Effect.scoped,
444
- Effect.onExit((exit) => {
445
- ws.close(exit._tag === "Success" ? 1000 : 1011)
446
- return Effect.void
447
- }),
448
- Effect.raceFirst(Deferred.await(closeDeferred)),
449
- semaphore.withPermits(1)
450
- )
451
-
452
- const encoder = new TextEncoder()
453
- const run = <R, E, _>(handler: (_: Uint8Array) => Effect.Effect<_, E, R> | void, opts?: {
454
- readonly onOpen?: Effect.Effect<void> | undefined
455
- }) => runRaw((data) => typeof data === "string" ? handler(encoder.encode(data)) : handler(data), opts)
456
-
457
- return Socket.Socket.of({
458
- [Socket.TypeId]: Socket.TypeId,
459
- run,
460
- runRaw,
461
- writer
462
- })
463
- }))
464
- })
465
- )
466
- }
467
- }
468
-
469
- const removeHost = (url: string) => {
470
- if (url[0] === "/") {
471
- return url
472
- }
473
- const index = url.indexOf("/", url.indexOf("//") + 2)
474
- return index === -1 ? "/" : url.slice(index)
475
- }
476
-
477
- /** @internal */
478
- export const requestSource = (self: ServerRequest.HttpServerRequest) => (self as ServerRequestImpl).source
@@ -1,149 +0,0 @@
1
- import * as Multipart from "@effect/platform/Multipart"
2
- import * as Channel from "effect/Channel"
3
- import * as Effect from "effect/Effect"
4
- import { pipe } from "effect/Function"
5
- import * as Inspectable from "effect/Inspectable"
6
- import * as Stream from "effect/Stream"
7
- import type { MultipartError, PartInfo } from "multipasta"
8
- import { decodeField } from "multipasta"
9
- import * as MP from "multipasta/web"
10
-
11
- /** @internal */
12
- export const stream = (source: Request): Stream.Stream<Multipart.Part, Multipart.MultipartError> =>
13
- pipe(
14
- Multipart.makeConfig({}),
15
- Effect.map((config) => {
16
- const parser = MP.make({
17
- ...config,
18
- headers: source.headers
19
- })
20
- return Stream.fromReadableStream(
21
- () => source.body!.pipeThrough(parser),
22
- (cause) => convertError(cause as MultipartError)
23
- )
24
- }),
25
- Stream.unwrap,
26
- Stream.map(convertPart)
27
- )
28
-
29
- /** @internal */
30
- export const persisted = (source: Request) =>
31
- Multipart.toPersisted(stream(source), (path, file) =>
32
- Effect.tryPromise({
33
- try: async () => {
34
- const fileImpl = file as FileImpl
35
- const writer = Bun.file(path).writer()
36
- const reader = fileImpl.file.readable.getReader()
37
- try {
38
- while (true) {
39
- const { done, value } = await reader.readMany()
40
- if (done) break
41
- for (const chunk of value) {
42
- writer.write(chunk)
43
- }
44
- await writer.flush()
45
- }
46
- } finally {
47
- reader.cancel()
48
- await writer.end()
49
- }
50
- },
51
- catch: (cause) => new Multipart.MultipartError({ reason: "InternalError", cause })
52
- }))
53
-
54
- const convertPart = (part: MP.Part): Multipart.Part =>
55
- part._tag === "Field" ? new FieldImpl(part.info, part.value) : new FileImpl(part)
56
-
57
- abstract class PartBase extends Inspectable.Class {
58
- readonly [Multipart.TypeId]: Multipart.TypeId
59
- constructor() {
60
- super()
61
- this[Multipart.TypeId] = Multipart.TypeId
62
- }
63
- }
64
-
65
- class FieldImpl extends PartBase implements Multipart.Field {
66
- readonly _tag = "Field"
67
- readonly key: string
68
- readonly contentType: string
69
- readonly value: string
70
-
71
- constructor(
72
- info: PartInfo,
73
- value: Uint8Array
74
- ) {
75
- super()
76
- this.key = info.name
77
- this.contentType = info.contentType
78
- this.value = decodeField(info, value)
79
- }
80
-
81
- toJSON(): unknown {
82
- return {
83
- _id: "@effect/platform/Multipart/Part",
84
- _tag: "Field",
85
- key: this.key,
86
- value: this.value,
87
- contentType: this.contentType
88
- }
89
- }
90
- }
91
-
92
- class FileImpl extends PartBase implements Multipart.File {
93
- readonly _tag = "File"
94
- readonly key: string
95
- readonly name: string
96
- readonly contentType: string
97
- readonly content: Stream.Stream<Uint8Array, Multipart.MultipartError>
98
- readonly contentEffect: Effect.Effect<Uint8Array, Multipart.MultipartError>
99
-
100
- constructor(readonly file: MP.File) {
101
- super()
102
- this.key = file.info.name
103
- this.name = file.info.filename ?? file.info.name
104
- this.contentType = file.info.contentType
105
- this.content = Stream.fromReadableStream(
106
- () => file.readable,
107
- (cause) => new Multipart.MultipartError({ reason: "InternalError", cause })
108
- )
109
- this.contentEffect = Stream.toChannel(this.content).pipe(
110
- Channel.pipeTo(Multipart.collectUint8Array),
111
- Channel.run,
112
- Effect.mapError((cause) => new Multipart.MultipartError({ reason: "InternalError", cause }))
113
- )
114
- }
115
-
116
- toJSON(): unknown {
117
- return {
118
- _id: "@effect/platform/Multipart/Part",
119
- _tag: "File",
120
- key: this.key,
121
- name: this.name,
122
- contentType: this.contentType
123
- }
124
- }
125
- }
126
-
127
- function convertError(cause: MultipartError): Multipart.MultipartError {
128
- switch (cause._tag) {
129
- case "ReachedLimit": {
130
- switch (cause.limit) {
131
- case "MaxParts": {
132
- return new Multipart.MultipartError({ reason: "TooManyParts", cause })
133
- }
134
- case "MaxFieldSize": {
135
- return new Multipart.MultipartError({ reason: "FieldTooLarge", cause })
136
- }
137
- case "MaxPartSize": {
138
- return new Multipart.MultipartError({ reason: "FileTooLarge", cause })
139
- }
140
- case "MaxTotalSize": {
141
- return new Multipart.MultipartError({ reason: "BodyTooLarge", cause })
142
- }
143
- }
144
- }
145
- default: {
146
- return new Multipart.MultipartError({ reason: "Parse", cause })
147
- }
148
- }
149
- }
@@ -1,68 +0,0 @@
1
- import * as Worker from "@effect/platform/Worker"
2
- import { WorkerError } from "@effect/platform/WorkerError"
3
- import * as Deferred from "effect/Deferred"
4
- import * as Effect from "effect/Effect"
5
- import * as Exit from "effect/Exit"
6
- import * as Layer from "effect/Layer"
7
- import * as Scope from "effect/Scope"
8
-
9
- const platformWorkerImpl = Worker.makePlatform<globalThis.Worker>()({
10
- setup({ scope, worker }) {
11
- return Effect.flatMap(Deferred.make<void>(), (closeDeferred) => {
12
- worker.addEventListener("close", () => {
13
- Deferred.unsafeDone(closeDeferred, Exit.void)
14
- })
15
- return Effect.as(
16
- Scope.addFinalizer(
17
- scope,
18
- Effect.suspend(() => {
19
- worker.postMessage([1])
20
- return Deferred.await(closeDeferred)
21
- }).pipe(
22
- Effect.interruptible,
23
- Effect.timeout(5000),
24
- Effect.catchAllCause(() => Effect.sync(() => worker.terminate()))
25
- )
26
- ),
27
- worker
28
- )
29
- })
30
- },
31
- listen({ deferred, emit, port, scope }) {
32
- function onMessage(event: MessageEvent) {
33
- emit(event.data)
34
- }
35
- function onError(event: ErrorEvent) {
36
- Deferred.unsafeDone(
37
- deferred,
38
- new WorkerError({ reason: "unknown", cause: event.error ?? event.message })
39
- )
40
- }
41
- port.addEventListener("message", onMessage)
42
- port.addEventListener("error", onError)
43
- return Scope.addFinalizer(
44
- scope,
45
- Effect.sync(() => {
46
- port.removeEventListener("message", onMessage)
47
- port.removeEventListener("error", onError)
48
- })
49
- )
50
- }
51
- })
52
-
53
- /** @internal */
54
- export const layerWorker = Layer.succeed(Worker.PlatformWorker, platformWorkerImpl)
55
-
56
- /** @internal */
57
- export const layerManager = Layer.provide(Worker.layerManager, layerWorker)
58
-
59
- /** @internal */
60
- export const layer = (spawn: (id: number) => globalThis.Worker) =>
61
- Layer.merge(
62
- layerManager,
63
- Worker.layerSpawner(spawn)
64
- )
65
-
66
- /** @internal */
67
- export const layerPlatform = (spawn: (id: number) => globalThis.Worker) =>
68
- Layer.merge(layerWorker, Worker.layerSpawner(spawn))