effect 4.0.0-beta.27 → 4.0.0-beta.29

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 (208) hide show
  1. package/dist/Brand.d.ts +1 -1
  2. package/dist/Brand.d.ts.map +1 -1
  3. package/dist/Brand.js +1 -1
  4. package/dist/Brand.js.map +1 -1
  5. package/dist/ConfigProvider.d.ts +1 -1
  6. package/dist/Cron.d.ts +1 -1
  7. package/dist/Data.d.ts +1 -1
  8. package/dist/Data.d.ts.map +1 -1
  9. package/dist/Data.js.map +1 -1
  10. package/dist/Effect.d.ts +300 -184
  11. package/dist/Effect.d.ts.map +1 -1
  12. package/dist/Effect.js +99 -61
  13. package/dist/Effect.js.map +1 -1
  14. package/dist/Encoding.d.ts +1 -1
  15. package/dist/Exit.d.ts +24 -12
  16. package/dist/Exit.d.ts.map +1 -1
  17. package/dist/Exit.js +8 -4
  18. package/dist/Exit.js.map +1 -1
  19. package/dist/Fiber.d.ts +1 -0
  20. package/dist/Fiber.d.ts.map +1 -1
  21. package/dist/Fiber.js.map +1 -1
  22. package/dist/Function.d.ts +1 -9
  23. package/dist/Function.d.ts.map +1 -1
  24. package/dist/Function.js +2 -10
  25. package/dist/Function.js.map +1 -1
  26. package/dist/Graph.d.ts +1 -1
  27. package/dist/Newtype.d.ts +291 -0
  28. package/dist/Newtype.d.ts.map +1 -0
  29. package/dist/Newtype.js +161 -0
  30. package/dist/Newtype.js.map +1 -0
  31. package/dist/PlatformError.d.ts +2 -2
  32. package/dist/References.d.ts +6 -1
  33. package/dist/References.d.ts.map +1 -1
  34. package/dist/References.js +6 -1
  35. package/dist/References.js.map +1 -1
  36. package/dist/RequestResolver.d.ts +19 -19
  37. package/dist/RequestResolver.js +10 -10
  38. package/dist/RequestResolver.js.map +1 -1
  39. package/dist/Schedule.d.ts +142 -80
  40. package/dist/Schedule.d.ts.map +1 -1
  41. package/dist/Schedule.js +58 -32
  42. package/dist/Schedule.js.map +1 -1
  43. package/dist/Scheduler.d.ts +9 -0
  44. package/dist/Scheduler.d.ts.map +1 -1
  45. package/dist/Scheduler.js +11 -0
  46. package/dist/Scheduler.js.map +1 -1
  47. package/dist/Schema.d.ts +1 -1
  48. package/dist/Schema.d.ts.map +1 -1
  49. package/dist/Schema.js +3 -1
  50. package/dist/Schema.js.map +1 -1
  51. package/dist/SchemaAST.d.ts.map +1 -1
  52. package/dist/SchemaAST.js +1 -1
  53. package/dist/SchemaAST.js.map +1 -1
  54. package/dist/Stdio.d.ts +6 -2
  55. package/dist/Stdio.d.ts.map +1 -1
  56. package/dist/Stdio.js +2 -2
  57. package/dist/Stdio.js.map +1 -1
  58. package/dist/Stream.d.ts +8 -4
  59. package/dist/Stream.d.ts.map +1 -1
  60. package/dist/Stream.js +8 -4
  61. package/dist/Stream.js.map +1 -1
  62. package/dist/Types.d.ts +1 -22
  63. package/dist/Types.d.ts.map +1 -1
  64. package/dist/index.d.ts +71 -3
  65. package/dist/index.d.ts.map +1 -1
  66. package/dist/index.js +71 -3
  67. package/dist/index.js.map +1 -1
  68. package/dist/internal/effect.js +3 -1
  69. package/dist/internal/effect.js.map +1 -1
  70. package/dist/unstable/ai/LanguageModel.d.ts +12 -28
  71. package/dist/unstable/ai/LanguageModel.d.ts.map +1 -1
  72. package/dist/unstable/ai/LanguageModel.js +4 -18
  73. package/dist/unstable/ai/LanguageModel.js.map +1 -1
  74. package/dist/unstable/ai/McpSchema.d.ts +20 -1
  75. package/dist/unstable/ai/McpSchema.d.ts.map +1 -1
  76. package/dist/unstable/ai/McpSchema.js +8 -0
  77. package/dist/unstable/ai/McpSchema.js.map +1 -1
  78. package/dist/unstable/ai/McpServer.d.ts +65 -12
  79. package/dist/unstable/ai/McpServer.d.ts.map +1 -1
  80. package/dist/unstable/ai/McpServer.js +159 -45
  81. package/dist/unstable/ai/McpServer.js.map +1 -1
  82. package/dist/unstable/ai/Toolkit.d.ts +1 -1
  83. package/dist/unstable/ai/Toolkit.d.ts.map +1 -1
  84. package/dist/unstable/ai/Toolkit.js +4 -11
  85. package/dist/unstable/ai/Toolkit.js.map +1 -1
  86. package/dist/unstable/ai/internal/codec-transformer.js +0 -5
  87. package/dist/unstable/ai/internal/codec-transformer.js.map +1 -1
  88. package/dist/unstable/cli/Prompt.js +35 -8
  89. package/dist/unstable/cli/Prompt.js.map +1 -1
  90. package/dist/unstable/cluster/Message.d.ts +5 -5
  91. package/dist/unstable/cluster/Reply.d.ts +3 -3
  92. package/dist/unstable/encoding/Msgpack.d.ts +1 -1
  93. package/dist/unstable/encoding/Ndjson.d.ts +1 -1
  94. package/dist/unstable/encoding/Sse.d.ts +1 -1
  95. package/dist/unstable/eventlog/EventJournal.d.ts +1 -1
  96. package/dist/unstable/eventlog/EventLogRemote.d.ts +1 -1
  97. package/dist/unstable/http/Cookies.d.ts +45 -1
  98. package/dist/unstable/http/Cookies.d.ts.map +1 -1
  99. package/dist/unstable/http/Cookies.js +22 -0
  100. package/dist/unstable/http/Cookies.js.map +1 -1
  101. package/dist/unstable/http/HttpBody.d.ts +1 -1
  102. package/dist/unstable/http/HttpClient.d.ts.map +1 -1
  103. package/dist/unstable/http/HttpClient.js +3 -7
  104. package/dist/unstable/http/HttpClient.js.map +1 -1
  105. package/dist/unstable/http/HttpClientError.d.ts +7 -7
  106. package/dist/unstable/http/HttpClientRequest.d.ts +5 -0
  107. package/dist/unstable/http/HttpClientRequest.d.ts.map +1 -1
  108. package/dist/unstable/http/HttpClientRequest.js +21 -17
  109. package/dist/unstable/http/HttpClientRequest.js.map +1 -1
  110. package/dist/unstable/http/HttpEffect.d.ts +7 -0
  111. package/dist/unstable/http/HttpEffect.d.ts.map +1 -1
  112. package/dist/unstable/http/HttpEffect.js +6 -0
  113. package/dist/unstable/http/HttpEffect.js.map +1 -1
  114. package/dist/unstable/http/HttpServerError.d.ts +6 -6
  115. package/dist/unstable/http/HttpServerRequest.d.ts +11 -0
  116. package/dist/unstable/http/HttpServerRequest.d.ts.map +1 -1
  117. package/dist/unstable/http/HttpServerRequest.js +291 -1
  118. package/dist/unstable/http/HttpServerRequest.js.map +1 -1
  119. package/dist/unstable/http/HttpServerResponse.d.ts +47 -1
  120. package/dist/unstable/http/HttpServerResponse.d.ts.map +1 -1
  121. package/dist/unstable/http/HttpServerResponse.js +227 -0
  122. package/dist/unstable/http/HttpServerResponse.js.map +1 -1
  123. package/dist/unstable/http/HttpStaticServer.d.ts +69 -0
  124. package/dist/unstable/http/HttpStaticServer.d.ts.map +1 -0
  125. package/dist/unstable/http/HttpStaticServer.js +353 -0
  126. package/dist/unstable/http/HttpStaticServer.js.map +1 -0
  127. package/dist/unstable/http/Multipart.d.ts +1 -1
  128. package/dist/unstable/http/UrlParams.d.ts +1 -1
  129. package/dist/unstable/http/index.d.ts +4 -0
  130. package/dist/unstable/http/index.d.ts.map +1 -1
  131. package/dist/unstable/http/index.js +4 -0
  132. package/dist/unstable/http/index.js.map +1 -1
  133. package/dist/unstable/httpapi/HttpApiGroup.d.ts +1 -0
  134. package/dist/unstable/httpapi/HttpApiGroup.d.ts.map +1 -1
  135. package/dist/unstable/httpapi/HttpApiGroup.js.map +1 -1
  136. package/dist/unstable/persistence/KeyValueStore.d.ts +1 -1
  137. package/dist/unstable/reactivity/Atom.d.ts +7 -2
  138. package/dist/unstable/reactivity/Atom.d.ts.map +1 -1
  139. package/dist/unstable/reactivity/Atom.js +33 -15
  140. package/dist/unstable/reactivity/Atom.js.map +1 -1
  141. package/dist/unstable/reactivity/AtomHttpApi.d.ts +4 -6
  142. package/dist/unstable/reactivity/AtomHttpApi.d.ts.map +1 -1
  143. package/dist/unstable/reactivity/AtomHttpApi.js +39 -9
  144. package/dist/unstable/reactivity/AtomHttpApi.js.map +1 -1
  145. package/dist/unstable/reactivity/AtomRegistry.js +26 -2
  146. package/dist/unstable/reactivity/AtomRegistry.js.map +1 -1
  147. package/dist/unstable/reactivity/AtomRpc.d.ts +8 -8
  148. package/dist/unstable/reactivity/AtomRpc.d.ts.map +1 -1
  149. package/dist/unstable/reactivity/AtomRpc.js +46 -20
  150. package/dist/unstable/reactivity/AtomRpc.js.map +1 -1
  151. package/dist/unstable/rpc/Rpc.d.ts +1 -1
  152. package/dist/unstable/rpc/Rpc.d.ts.map +1 -1
  153. package/dist/unstable/rpc/Rpc.js.map +1 -1
  154. package/dist/unstable/rpc/RpcMiddleware.d.ts +5 -5
  155. package/dist/unstable/rpc/RpcMiddleware.d.ts.map +1 -1
  156. package/dist/unstable/rpc/RpcMiddleware.js.map +1 -1
  157. package/dist/unstable/rpc/RpcServer.js +2 -2
  158. package/dist/unstable/rpc/RpcServer.js.map +1 -1
  159. package/dist/unstable/rpc/Utils.js +1 -1
  160. package/dist/unstable/rpc/Utils.js.map +1 -1
  161. package/dist/unstable/socket/SocketServer.d.ts +3 -3
  162. package/dist/unstable/sql/Migrator.d.ts +1 -1
  163. package/dist/unstable/sql/SqlResolver.js +2 -2
  164. package/dist/unstable/sql/SqlResolver.js.map +1 -1
  165. package/dist/unstable/workflow/Workflow.d.ts +1 -1
  166. package/package.json +1 -1
  167. package/src/Brand.ts +1 -1
  168. package/src/Data.ts +1 -2
  169. package/src/Effect.ts +300 -184
  170. package/src/Exit.ts +24 -12
  171. package/src/Fiber.ts +1 -0
  172. package/src/Function.ts +2 -10
  173. package/src/Newtype.ts +308 -0
  174. package/src/References.ts +6 -1
  175. package/src/RequestResolver.ts +20 -20
  176. package/src/Schedule.ts +142 -80
  177. package/src/Scheduler.ts +12 -0
  178. package/src/Schema.ts +4 -2
  179. package/src/SchemaAST.ts +1 -4
  180. package/src/Stdio.ts +8 -4
  181. package/src/Stream.ts +8 -4
  182. package/src/Types.ts +1 -23
  183. package/src/index.ts +72 -3
  184. package/src/internal/effect.ts +3 -0
  185. package/src/unstable/ai/LanguageModel.ts +16 -37
  186. package/src/unstable/ai/McpSchema.ts +14 -0
  187. package/src/unstable/ai/McpServer.ts +224 -53
  188. package/src/unstable/ai/Toolkit.ts +5 -14
  189. package/src/unstable/ai/internal/codec-transformer.ts +0 -7
  190. package/src/unstable/cli/Prompt.ts +31 -9
  191. package/src/unstable/http/Cookies.ts +84 -0
  192. package/src/unstable/http/HttpClient.ts +5 -6
  193. package/src/unstable/http/HttpClientRequest.ts +21 -17
  194. package/src/unstable/http/HttpEffect.ts +8 -0
  195. package/src/unstable/http/HttpServerRequest.ts +388 -1
  196. package/src/unstable/http/HttpServerResponse.ts +328 -1
  197. package/src/unstable/http/HttpStaticServer.ts +456 -0
  198. package/src/unstable/http/index.ts +5 -0
  199. package/src/unstable/httpapi/HttpApiGroup.ts +1 -0
  200. package/src/unstable/reactivity/Atom.ts +62 -35
  201. package/src/unstable/reactivity/AtomHttpApi.ts +45 -11
  202. package/src/unstable/reactivity/AtomRegistry.ts +30 -2
  203. package/src/unstable/reactivity/AtomRpc.ts +48 -17
  204. package/src/unstable/rpc/Rpc.ts +1 -3
  205. package/src/unstable/rpc/RpcMiddleware.ts +12 -6
  206. package/src/unstable/rpc/RpcServer.ts +2 -2
  207. package/src/unstable/rpc/Utils.ts +1 -1
  208. package/src/unstable/sql/SqlResolver.ts +2 -2
@@ -5,7 +5,7 @@ import * as Effect from "../../Effect.ts"
5
5
  import * as ErrorReporter from "../../ErrorReporter.ts"
6
6
  import type * as FileSystem from "../../FileSystem.ts"
7
7
  import { dual } from "../../Function.ts"
8
- import type * as Inspectable from "../../Inspectable.ts"
8
+ import * as Inspectable from "../../Inspectable.ts"
9
9
  import { PipeInspectableProto } from "../../internal/core.ts"
10
10
  import type { Pipeable } from "../../Pipeable.ts"
11
11
  import type { PlatformError } from "../../PlatformError.ts"
@@ -19,6 +19,10 @@ import type { Mutable } from "../../Types.ts"
19
19
  import * as Cookies from "./Cookies.ts"
20
20
  import * as Headers from "./Headers.ts"
21
21
  import * as Body from "./HttpBody.ts"
22
+ import * as HttpClientError from "./HttpClientError.ts"
23
+ import * as HttpClientRequest from "./HttpClientRequest.ts"
24
+ import * as HttpClientResponse from "./HttpClientResponse.ts"
25
+ import * as HttpIncomingMessage from "./HttpIncomingMessage.ts"
22
26
  import type { HttpPlatform } from "./HttpPlatform.ts"
23
27
  import * as Template from "./Template.ts"
24
28
  import * as UrlParams from "./UrlParams.ts"
@@ -524,6 +528,47 @@ export const setCookie: {
524
528
  )
525
529
  )
526
530
 
531
+ /**
532
+ * @since 4.0.0
533
+ * @category combinators
534
+ */
535
+ export const expireCookie: {
536
+ /**
537
+ * @since 4.0.0
538
+ * @category combinators
539
+ */
540
+ (
541
+ name: string,
542
+ options?: Omit<NonNullable<Cookies.Cookie["options"]>, "expires" | "maxAge">
543
+ ): (
544
+ self: HttpServerResponse
545
+ ) => Effect.Effect<HttpServerResponse, Cookies.CookiesError>
546
+ /**
547
+ * @since 4.0.0
548
+ * @category combinators
549
+ */
550
+ (
551
+ self: HttpServerResponse,
552
+ name: string,
553
+ options?: Omit<NonNullable<Cookies.Cookie["options"]>, "expires" | "maxAge">
554
+ ): Effect.Effect<HttpServerResponse, Cookies.CookiesError>
555
+ } = dual(
556
+ (args) => isHttpServerResponse(args[0]),
557
+ (
558
+ self: HttpServerResponse,
559
+ name: string,
560
+ options?: Omit<NonNullable<Cookies.Cookie["options"]>, "expires" | "maxAge">
561
+ ): Effect.Effect<HttpServerResponse, Cookies.CookiesError> =>
562
+ Effect.map(
563
+ Cookies.expireCookie(self.cookies, name, options).asEffect(),
564
+ (cookies) =>
565
+ makeResponse({
566
+ ...self,
567
+ cookies
568
+ })
569
+ )
570
+ )
571
+
527
572
  /**
528
573
  * @since 4.0.0
529
574
  * @category combinators
@@ -558,6 +603,41 @@ export const setCookieUnsafe: {
558
603
  })
559
604
  )
560
605
 
606
+ /**
607
+ * @since 4.0.0
608
+ * @category combinators
609
+ */
610
+ export const expireCookieUnsafe: {
611
+ /**
612
+ * @since 4.0.0
613
+ * @category combinators
614
+ */
615
+ (
616
+ name: string,
617
+ options?: Omit<NonNullable<Cookies.Cookie["options"]>, "expires" | "maxAge">
618
+ ): (self: HttpServerResponse) => HttpServerResponse
619
+ /**
620
+ * @since 4.0.0
621
+ * @category combinators
622
+ */
623
+ (
624
+ self: HttpServerResponse,
625
+ name: string,
626
+ options?: Omit<NonNullable<Cookies.Cookie["options"]>, "expires" | "maxAge">
627
+ ): HttpServerResponse
628
+ } = dual(
629
+ (args) => isHttpServerResponse(args[0]),
630
+ (
631
+ self: HttpServerResponse,
632
+ name: string,
633
+ options?: Omit<NonNullable<Cookies.Cookie["options"]>, "expires" | "maxAge">
634
+ ): HttpServerResponse =>
635
+ makeResponse({
636
+ ...self,
637
+ cookies: Cookies.expireCookieUnsafe(self.cookies, name, options)
638
+ })
639
+ )
640
+
561
641
  /**
562
642
  * @since 4.0.0
563
643
  * @category combinators
@@ -829,6 +909,253 @@ export const toWeb = (
829
909
  }
830
910
  }
831
911
 
912
+ /**
913
+ * @since 4.0.0
914
+ * @category conversions
915
+ */
916
+ export const toClientResponse = (
917
+ response: HttpServerResponse,
918
+ options?: {
919
+ readonly request?: HttpClientRequest.HttpClientRequest | undefined
920
+ }
921
+ ): HttpClientResponse.HttpClientResponse =>
922
+ new ServerHttpClientResponse(
923
+ options?.request ?? HttpClientRequest.empty,
924
+ response
925
+ )
926
+
927
+ class ServerHttpClientResponse extends Inspectable.Class implements HttpClientResponse.HttpClientResponse {
928
+ readonly [HttpIncomingMessage.TypeId]: typeof HttpIncomingMessage.TypeId
929
+ readonly [HttpClientResponse.TypeId]: typeof HttpClientResponse.TypeId
930
+
931
+ readonly request: HttpClientRequest.HttpClientRequest
932
+ private readonly response: HttpServerResponse
933
+
934
+ constructor(
935
+ request: HttpClientRequest.HttpClientRequest,
936
+ response: HttpServerResponse
937
+ ) {
938
+ super()
939
+ this.request = request
940
+ this.response = response
941
+ this[HttpIncomingMessage.TypeId] = HttpIncomingMessage.TypeId
942
+ this[HttpClientResponse.TypeId] = HttpClientResponse.TypeId
943
+ }
944
+
945
+ toJSON(): unknown {
946
+ return HttpIncomingMessage.inspect(this, {
947
+ _id: "HttpClientResponse",
948
+ request: this.request.toJSON(),
949
+ status: this.status
950
+ })
951
+ }
952
+
953
+ get status(): number {
954
+ return this.response.status
955
+ }
956
+
957
+ private cachedHeaders?: Headers.Headers
958
+ get headers(): Headers.Headers {
959
+ return this.cachedHeaders ??= this.response.body._tag === "FormData"
960
+ ? Headers.merge(this.response.headers, Headers.fromInput(this.getFormDataResponse().headers))
961
+ : this.response.headers
962
+ }
963
+
964
+ get cookies(): Cookies.Cookies {
965
+ return this.response.cookies
966
+ }
967
+
968
+ get remoteAddress(): string | undefined {
969
+ return undefined
970
+ }
971
+
972
+ get stream(): Stream.Stream<Uint8Array, HttpClientError.HttpClientError> {
973
+ const body = this.response.body
974
+ switch (body._tag) {
975
+ case "Empty": {
976
+ return Stream.empty
977
+ }
978
+ case "Stream": {
979
+ return Stream.mapError(body.stream, (cause) => this.decodeError(cause))
980
+ }
981
+ case "Uint8Array": {
982
+ return Stream.succeed(body.body)
983
+ }
984
+ case "Raw": {
985
+ const rawBody = body.body
986
+ if (rawBody instanceof Response) {
987
+ return rawBody.body
988
+ ? Stream.fromReadableStream({
989
+ evaluate: () => rawBody.body!,
990
+ onError: (cause) => this.decodeError(cause)
991
+ })
992
+ : Stream.empty
993
+ }
994
+ if (isReadableStream(rawBody)) {
995
+ return Stream.fromReadableStream({
996
+ evaluate: () => rawBody,
997
+ onError: (cause) => this.decodeError(cause)
998
+ })
999
+ }
1000
+ if (rawBody instanceof Blob) {
1001
+ return Stream.fromReadableStream({
1002
+ evaluate: () => rawBody.stream(),
1003
+ onError: (cause) => this.decodeError(cause)
1004
+ })
1005
+ }
1006
+ return Stream.unwrap(Effect.map(this.bytes, Stream.succeed))
1007
+ }
1008
+ case "FormData": {
1009
+ const response = this.getFormDataResponse()
1010
+ return Stream.fromReadableStream({
1011
+ evaluate: () => response.body!,
1012
+ onError: (cause) => this.decodeError(cause)
1013
+ })
1014
+ }
1015
+ }
1016
+ }
1017
+
1018
+ get json(): Effect.Effect<unknown, HttpClientError.HttpClientError> {
1019
+ return Effect.flatMap(this.text, (text) =>
1020
+ Effect.try({
1021
+ try: () => text === "" ? null : JSON.parse(text) as unknown,
1022
+ catch: (cause) =>
1023
+ new HttpClientError.HttpClientError({
1024
+ reason: new HttpClientError.DecodeError({
1025
+ request: this.request,
1026
+ response: this,
1027
+ cause
1028
+ })
1029
+ })
1030
+ }))
1031
+ }
1032
+
1033
+ private get bytes(): Effect.Effect<Uint8Array, HttpClientError.HttpClientError> {
1034
+ const body = this.response.body
1035
+ switch (body._tag) {
1036
+ case "Empty": {
1037
+ return Effect.succeed(new Uint8Array(0))
1038
+ }
1039
+ case "Uint8Array": {
1040
+ return Effect.succeed(body.body)
1041
+ }
1042
+ case "Stream": {
1043
+ return Stream.mkUint8Array(this.stream)
1044
+ }
1045
+ case "Raw": {
1046
+ const rawBody = body.body
1047
+ if (rawBody instanceof Response) {
1048
+ return Effect.tryPromise({
1049
+ try: () => rawBody.arrayBuffer().then((buffer) => new Uint8Array(buffer)),
1050
+ catch: (cause) => this.decodeError(cause)
1051
+ })
1052
+ }
1053
+ return Effect.tryPromise({
1054
+ try: () => new Response(rawBody as any).arrayBuffer().then((buffer) => new Uint8Array(buffer)),
1055
+ catch: (cause) => this.decodeError(cause)
1056
+ })
1057
+ }
1058
+ case "FormData": {
1059
+ return Effect.tryPromise({
1060
+ try: () => new Response(body.formData).arrayBuffer().then((buffer) => new Uint8Array(buffer)),
1061
+ catch: (cause) => this.decodeError(cause)
1062
+ })
1063
+ }
1064
+ }
1065
+ }
1066
+
1067
+ get text(): Effect.Effect<string, HttpClientError.HttpClientError> {
1068
+ return Effect.map(this.bytes, (bytes) => textDecoder.decode(bytes))
1069
+ }
1070
+
1071
+ get urlParamsBody(): Effect.Effect<UrlParams.UrlParams, HttpClientError.HttpClientError> {
1072
+ return Effect.flatMap(this.text, (_) =>
1073
+ Effect.try({
1074
+ try: () => UrlParams.fromInput(new URLSearchParams(_)),
1075
+ catch: (cause) =>
1076
+ new HttpClientError.HttpClientError({
1077
+ reason: new HttpClientError.DecodeError({
1078
+ request: this.request,
1079
+ response: this,
1080
+ cause
1081
+ })
1082
+ })
1083
+ }))
1084
+ }
1085
+
1086
+ get formData(): Effect.Effect<FormData, HttpClientError.HttpClientError> {
1087
+ const body = this.response.body
1088
+ if (body._tag === "FormData") {
1089
+ return Effect.succeed(body.formData)
1090
+ }
1091
+ return Effect.servicesWith((services: ServiceMap.ServiceMap<never>) => {
1092
+ const readableStream = Stream.toReadableStreamWith(this.stream, services)
1093
+ return Effect.tryPromise({
1094
+ try: () => new Response(readableStream, { headers: this.headers }).formData(),
1095
+ catch: (cause) => this.decodeError(cause)
1096
+ })
1097
+ })
1098
+ }
1099
+
1100
+ get arrayBuffer(): Effect.Effect<ArrayBuffer, HttpClientError.HttpClientError> {
1101
+ return Effect.map(this.bytes, (bytes) => bytes.slice().buffer)
1102
+ }
1103
+
1104
+ private decodeError(cause: unknown): HttpClientError.HttpClientError {
1105
+ return new HttpClientError.HttpClientError({
1106
+ reason: new HttpClientError.DecodeError({
1107
+ request: this.request,
1108
+ response: this,
1109
+ cause
1110
+ })
1111
+ })
1112
+ }
1113
+
1114
+ private formDataResponse?: Response
1115
+ private getFormDataResponse(): Response {
1116
+ return this.formDataResponse ??= new Response((this.response.body as Body.FormData).formData)
1117
+ }
1118
+ }
1119
+
1120
+ const textDecoder = new TextDecoder()
1121
+
1122
+ /**
1123
+ * @since 4.0.0
1124
+ * @category conversions
1125
+ */
1126
+ export const fromClientResponse = (
1127
+ response: HttpClientResponse.HttpClientResponse
1128
+ ): HttpServerResponse => {
1129
+ const headers = Headers.remove(response.headers, "set-cookie")
1130
+ return makeResponse({
1131
+ status: response.status,
1132
+ headers,
1133
+ cookies: response.cookies,
1134
+ body: Body.stream(
1135
+ Stream.catchIf(response.stream, isEmptyBodyError, () => Stream.empty),
1136
+ Headers.get(headers, "content-type"),
1137
+ getContentLength(headers)
1138
+ )
1139
+ })
1140
+ }
1141
+
1142
+ const isReadableStream = (u: unknown): u is ReadableStream<Uint8Array> =>
1143
+ typeof ReadableStream !== "undefined" && u instanceof ReadableStream
1144
+
1145
+ const isEmptyBodyError = (
1146
+ error: HttpClientError.HttpClientError
1147
+ ): error is HttpClientError.HttpClientError =>
1148
+ HttpClientError.isHttpClientError(error) && error.reason._tag === "EmptyBodyError"
1149
+
1150
+ const getContentLength = (headers: Headers.Headers): number | undefined => {
1151
+ const contentLength = Headers.get(headers, "content-length")
1152
+ if (contentLength === undefined) {
1153
+ return undefined
1154
+ }
1155
+ const parsed = Number(contentLength)
1156
+ return Number.isFinite(parsed) && parsed >= 0 ? parsed : undefined
1157
+ }
1158
+
832
1159
  const Proto: Omit<
833
1160
  HttpServerResponse,
834
1161
  "status" | "statusText" | "headers" | "cookies" | "body"