backup 2026-08-23T20:24:25.191693 (part 10)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .opencode/node_modules/effect/src/unstable/cluster/internal/entityManager.ts +25 -7
- .opencode/node_modules/effect/src/unstable/cluster/internal/hash.ts +12 -0
- .opencode/node_modules/effect/src/unstable/devtools/DevTools.ts +39 -3
- .opencode/node_modules/effect/src/unstable/devtools/DevToolsClient.ts +65 -5
- .opencode/node_modules/effect/src/unstable/devtools/DevToolsSchema.ts +225 -45
- .opencode/node_modules/effect/src/unstable/devtools/DevToolsServer.ts +25 -2
- .opencode/node_modules/effect/src/unstable/encoding/Msgpack.ts +120 -18
- .opencode/node_modules/effect/src/unstable/encoding/Ndjson.ts +188 -21
- .opencode/node_modules/effect/src/unstable/encoding/Sse.ts +141 -39
- .opencode/node_modules/effect/src/unstable/eventlog/Event.ts +140 -30
- .opencode/node_modules/effect/src/unstable/eventlog/EventGroup.ts +48 -17
- .opencode/node_modules/effect/src/unstable/eventlog/EventJournal.ts +144 -20
- .opencode/node_modules/effect/src/unstable/eventlog/EventLog.ts +188 -31
- .opencode/node_modules/effect/src/unstable/eventlog/EventLogEncryption.ts +34 -6
- .opencode/node_modules/effect/src/unstable/eventlog/EventLogMessage.ts +95 -18
- .opencode/node_modules/effect/src/unstable/eventlog/EventLogRemote.ts +58 -8
- .opencode/node_modules/effect/src/unstable/eventlog/EventLogServer.ts +33 -3
- .opencode/node_modules/effect/src/unstable/eventlog/EventLogServerEncrypted.ts +68 -6
- .opencode/node_modules/effect/src/unstable/eventlog/EventLogServerUnencrypted.ts +165 -16
- .opencode/node_modules/effect/src/unstable/eventlog/EventLogSessionAuth.ts +103 -16
- .opencode/node_modules/effect/src/unstable/eventlog/SqlEventJournal.ts +40 -2
- .opencode/node_modules/effect/src/unstable/eventlog/SqlEventLogServerEncrypted.ts +25 -3
- .opencode/node_modules/effect/src/unstable/eventlog/SqlEventLogServerUnencrypted.ts +20 -2
- .opencode/node_modules/effect/src/unstable/http/Cookies.ts +213 -105
- .opencode/node_modules/effect/src/unstable/http/Etag.ts +51 -7
- .opencode/node_modules/effect/src/unstable/http/FetchHttpClient.ts +56 -4
- .opencode/node_modules/effect/src/unstable/http/FindMyWay.ts +7 -1
- .opencode/node_modules/effect/src/unstable/http/Headers.ts +339 -56
- .opencode/node_modules/effect/src/unstable/http/HttpBody.ts +164 -30
- .opencode/node_modules/effect/src/unstable/http/HttpClient.ts +323 -110
- .opencode/node_modules/effect/src/unstable/http/HttpClientError.ts +78 -13
- .opencode/node_modules/effect/src/unstable/http/HttpClientRequest.ts +339 -102
- .opencode/node_modules/effect/src/unstable/http/HttpClientResponse.ts +56 -16
- .opencode/node_modules/effect/src/unstable/http/HttpEffect.ts +59 -18
- .opencode/node_modules/effect/src/unstable/http/HttpIncomingMessage.ts +32 -7
- .opencode/node_modules/effect/src/unstable/http/HttpMethod.ts +41 -7
- .opencode/node_modules/effect/src/unstable/http/HttpMiddleware.ts +51 -16
- .opencode/node_modules/effect/src/unstable/http/HttpPlatform.ts +24 -4
- .opencode/node_modules/effect/src/unstable/http/HttpRouter.ts +285 -89
- .opencode/node_modules/effect/src/unstable/http/HttpServer.ts +176 -23
- .opencode/node_modules/effect/src/unstable/http/HttpServerError.ts +96 -10
- .opencode/node_modules/effect/src/unstable/http/HttpServerRequest.ts +173 -32
- .opencode/node_modules/effect/src/unstable/http/HttpServerRespondable.ts +47 -6
- .opencode/node_modules/effect/src/unstable/http/HttpServerResponse.ts +486 -72
- .opencode/node_modules/effect/src/unstable/http/HttpStaticServer.ts +16 -8
- .opencode/node_modules/effect/src/unstable/http/HttpTraceContext.ts +54 -6
- .opencode/node_modules/effect/src/unstable/http/Multipart.ts +221 -32
- .opencode/node_modules/effect/src/unstable/http/Multipasta.ts +7 -1
- .opencode/node_modules/effect/src/unstable/http/Multipasta/HeadersParser.ts +6 -1
- .opencode/node_modules/effect/src/unstable/http/Multipasta/Node.ts +6 -1
.opencode/node_modules/effect/src/unstable/cluster/internal/entityManager.ts
CHANGED
|
@@ -15,7 +15,7 @@ import * as Option from "../../../Option.ts"
|
|
| 15 |
import { CurrentLogAnnotations } from "../../../References.ts"
|
| 16 |
import * as Schedule from "../../../Schedule.ts"
|
| 17 |
import * as Schema from "../../../Schema.ts"
|
| 18 |
-
import * as
|
| 19 |
import * as Scope from "../../../Scope.ts"
|
| 20 |
import type * as Rpc from "../../rpc/Rpc.ts"
|
| 21 |
import { RequestId } from "../../rpc/RpcMessage.ts"
|
|
@@ -145,7 +145,8 @@ export const make = Effect.fnUntraced(function*<
|
|
| 145 |
)
|
| 146 |
|
| 147 |
const activeRequests: EntityState["activeRequests"] = new Map()
|
| 148 |
-
let defectRequestIds
|
|
|
|
| 149 |
|
| 150 |
// the server is stored in a ref, so if there is a defect, we can
|
| 151 |
// swap the server without losing the active requests
|
|
@@ -186,6 +187,14 @@ export const make = Effect.fnUntraced(function*<
|
|
| 186 |
|
| 187 |
request.sentReply = true
|
| 188 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
// For durable messages, ignore interrupts during shutdown.
|
| 190 |
// They will be retried when the entity is restarted.
|
| 191 |
// Also, if the request is uninterruptible, we ignore the
|
|
@@ -281,9 +290,11 @@ export const make = Effect.fnUntraced(function*<
|
|
| 281 |
})
|
| 282 |
)
|
| 283 |
|
| 284 |
-
if (defectRequestIds.
|
| 285 |
for (const id of defectRequestIds) {
|
| 286 |
-
const
|
|
|
|
|
|
|
| 287 |
yield* server.write(0, {
|
| 288 |
...message.envelope,
|
| 289 |
id: RequestId(message.envelope.requestId),
|
|
@@ -294,7 +305,7 @@ export const make = Effect.fnUntraced(function*<
|
|
| 294 |
} as any) as any
|
| 295 |
})
|
| 296 |
}
|
| 297 |
-
defectRequestIds
|
| 298 |
}
|
| 299 |
|
| 300 |
return server.write
|
|
@@ -305,11 +316,18 @@ export const make = Effect.fnUntraced(function*<
|
|
| 305 |
if (!activeServers.has(address.entityId)) {
|
| 306 |
return endLatch.open
|
| 307 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
const effect = writeRef.rebuildUnsafe()
|
| 309 |
-
defectRequestIds = Array.from(activeRequests.keys())
|
| 310 |
return Effect.logError("Defect in entity, restarting", cause).pipe(
|
| 311 |
Effect.andThen(Effect.ignore(retryDriver(void 0))),
|
| 312 |
Effect.flatMap(() => activeServers.has(address.entityId) ? effect : endLatch.open),
|
|
|
|
|
|
|
|
|
|
| 313 |
Effect.annotateLogs({
|
| 314 |
module: "EntityManager",
|
| 315 |
address,
|
|
@@ -636,7 +654,7 @@ const makeMessageDecode = <Type extends string, Rpcs extends Rpc.Any>(
|
|
| 636 |
if (!rpc) {
|
| 637 |
return Effect.fail(
|
| 638 |
new Schema.SchemaError(
|
| 639 |
-
new
|
| 640 |
message: `Unknown tag ${message.envelope.tag} for entity type ${entity.type}`
|
| 641 |
})
|
| 642 |
)
|
|
|
|
| 15 |
import { CurrentLogAnnotations } from "../../../References.ts"
|
| 16 |
import * as Schedule from "../../../Schedule.ts"
|
| 17 |
import * as Schema from "../../../Schema.ts"
|
| 18 |
+
import * as SchemaIssue from "../../../SchemaIssue.ts"
|
| 19 |
import * as Scope from "../../../Scope.ts"
|
| 20 |
import type * as Rpc from "../../rpc/Rpc.ts"
|
| 21 |
import { RequestId } from "../../rpc/RpcMessage.ts"
|
|
|
|
| 145 |
)
|
| 146 |
|
| 147 |
const activeRequests: EntityState["activeRequests"] = new Map()
|
| 148 |
+
let defectRequestIds = new Set<bigint>()
|
| 149 |
+
let isRestartingDueToDefect = false
|
| 150 |
|
| 151 |
// the server is stored in a ref, so if there is a defect, we can
|
| 152 |
// swap the server without losing the active requests
|
|
|
|
| 187 |
|
| 188 |
request.sentReply = true
|
| 189 |
|
| 190 |
+
if (
|
| 191 |
+
isShuttingDown &&
|
| 192 |
+
Exit.hasInterrupts(response.exit) &&
|
| 193 |
+
defectRequestIds.has(response.requestId)
|
| 194 |
+
) {
|
| 195 |
+
return Effect.void
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
// For durable messages, ignore interrupts during shutdown.
|
| 199 |
// They will be retried when the entity is restarted.
|
| 200 |
// Also, if the request is uninterruptible, we ignore the
|
|
|
|
| 290 |
})
|
| 291 |
)
|
| 292 |
|
| 293 |
+
if (defectRequestIds.size > 0) {
|
| 294 |
for (const id of defectRequestIds) {
|
| 295 |
+
const request = activeRequests.get(id)
|
| 296 |
+
if (!request) continue
|
| 297 |
+
const { lastSentChunk, message } = request
|
| 298 |
yield* server.write(0, {
|
| 299 |
...message.envelope,
|
| 300 |
id: RequestId(message.envelope.requestId),
|
|
|
|
| 305 |
} as any) as any
|
| 306 |
})
|
| 307 |
}
|
| 308 |
+
defectRequestIds.clear()
|
| 309 |
}
|
| 310 |
|
| 311 |
return server.write
|
|
|
|
| 316 |
if (!activeServers.has(address.entityId)) {
|
| 317 |
return endLatch.open
|
| 318 |
}
|
| 319 |
+
if (isRestartingDueToDefect) {
|
| 320 |
+
return Effect.void
|
| 321 |
+
}
|
| 322 |
+
defectRequestIds = new Set(activeRequests.keys())
|
| 323 |
+
isRestartingDueToDefect = true
|
| 324 |
const effect = writeRef.rebuildUnsafe()
|
|
|
|
| 325 |
return Effect.logError("Defect in entity, restarting", cause).pipe(
|
| 326 |
Effect.andThen(Effect.ignore(retryDriver(void 0))),
|
| 327 |
Effect.flatMap(() => activeServers.has(address.entityId) ? effect : endLatch.open),
|
| 328 |
+
Effect.ensuring(Effect.sync(() => {
|
| 329 |
+
isRestartingDueToDefect = false
|
| 330 |
+
})),
|
| 331 |
Effect.annotateLogs({
|
| 332 |
module: "EntityManager",
|
| 333 |
address,
|
|
|
|
| 654 |
if (!rpc) {
|
| 655 |
return Effect.fail(
|
| 656 |
new Schema.SchemaError(
|
| 657 |
+
new SchemaIssue.InvalidValue(Option.some(message), {
|
| 658 |
message: `Unknown tag ${message.envelope.tag} for entity type ${entity.type}`
|
| 659 |
})
|
| 660 |
)
|
.opencode/node_modules/effect/src/unstable/cluster/internal/hash.ts
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
/** @internal */
|
| 2 |
export const hashOptimize = (n: number): number => (n & 0xbfffffff) | ((n >>> 1) & 0x40000000)
|
| 3 |
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Internal hashing helpers used by cluster routing to map entity identifiers to
|
| 3 |
+
* shard numbers. The hash is intentionally small and deterministic for
|
| 4 |
+
* JavaScript strings so every node that sees the same entity id and sharding
|
| 5 |
+
* configuration derives the same destination shard.
|
| 6 |
+
*
|
| 7 |
+
* **Gotchas**
|
| 8 |
+
*
|
| 9 |
+
* - Changing this implementation changes entity-to-shard assignment.
|
| 10 |
+
* - Strings are hashed as UTF-16 code units; normalize ids before routing if
|
| 11 |
+
* callers may produce equivalent text in different Unicode forms.
|
| 12 |
+
*/
|
| 13 |
/** @internal */
|
| 14 |
export const hashOptimize = (n: number): number => (n & 0xbfffffff) | ((n >>> 1) & 0x40000000)
|
| 15 |
|
.opencode/node_modules/effect/src/unstable/devtools/DevTools.ts
CHANGED
|
@@ -1,4 +1,12 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Layer from "../../Layer.ts"
|
|
@@ -6,14 +14,19 @@ import * as Socket from "../socket/Socket.ts"
|
|
| 6 |
import * as DevToolsClient from "./DevToolsClient.ts"
|
| 7 |
|
| 8 |
/**
|
| 9 |
-
*
|
|
|
|
| 10 |
* @category layers
|
|
|
|
| 11 |
*/
|
| 12 |
export const layerSocket: Layer.Layer<never, never, Socket.Socket> = DevToolsClient.layerTracer
|
| 13 |
|
| 14 |
/**
|
| 15 |
-
*
|
|
|
|
|
|
|
| 16 |
* @category layers
|
|
|
|
| 17 |
*/
|
| 18 |
export const layerWebSocket = (
|
| 19 |
url = "ws://localhost:34437"
|
|
@@ -23,8 +36,31 @@ export const layerWebSocket = (
|
|
| 23 |
)
|
| 24 |
|
| 25 |
/**
|
| 26 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
* @category layers
|
|
|
|
| 28 |
*/
|
| 29 |
export const layer = (url = "ws://localhost:34437"): Layer.Layer<never> =>
|
| 30 |
layerWebSocket(url).pipe(
|
|
|
|
| 1 |
/**
|
| 2 |
+
* High-level layers for connecting an Effect runtime to the unstable devtools
|
| 3 |
+
* tracer.
|
| 4 |
+
*
|
| 5 |
+
* `DevTools` is the application-facing entry point for installing a tracer that
|
| 6 |
+
* mirrors the current tracer and streams spans, span events, span completions,
|
| 7 |
+
* and metric snapshots to an external devtools process. Lower-level socket
|
| 8 |
+
* protocol details live in `DevToolsClient`.
|
| 9 |
+
*
|
| 10 |
* @since 4.0.0
|
| 11 |
*/
|
| 12 |
import * as Layer from "../../Layer.ts"
|
|
|
|
| 14 |
import * as DevToolsClient from "./DevToolsClient.ts"
|
| 15 |
|
| 16 |
/**
|
| 17 |
+
* Layer that installs the devtools tracer using an existing `Socket`.
|
| 18 |
+
*
|
| 19 |
* @category layers
|
| 20 |
+
* @since 4.0.0
|
| 21 |
*/
|
| 22 |
export const layerSocket: Layer.Layer<never, never, Socket.Socket> = DevToolsClient.layerTracer
|
| 23 |
|
| 24 |
/**
|
| 25 |
+
* Layer that installs the devtools tracer over a WebSocket connection to the
|
| 26 |
+
* specified URL, defaulting to `ws://localhost:34437`.
|
| 27 |
+
*
|
| 28 |
* @category layers
|
| 29 |
+
* @since 4.0.0
|
| 30 |
*/
|
| 31 |
export const layerWebSocket = (
|
| 32 |
url = "ws://localhost:34437"
|
|
|
|
| 36 |
)
|
| 37 |
|
| 38 |
/**
|
| 39 |
+
* Layer that installs the devtools tracer over a WebSocket connection using the
|
| 40 |
+
* global WebSocket constructor, defaulting to `ws://localhost:34437`.
|
| 41 |
+
*
|
| 42 |
+
* **When to use**
|
| 43 |
+
*
|
| 44 |
+
* Use to stream Effect tracing and metrics telemetry to a devtools process when
|
| 45 |
+
* the runtime environment already provides a global `WebSocket` constructor.
|
| 46 |
+
*
|
| 47 |
+
* **Details**
|
| 48 |
+
*
|
| 49 |
+
* This is a convenience wrapper around `layerWebSocket(url)` that provides
|
| 50 |
+
* `Socket.layerWebSocketConstructorGlobal`, so the resulting layer has no
|
| 51 |
+
* remaining requirements.
|
| 52 |
+
*
|
| 53 |
+
* **Gotchas**
|
| 54 |
+
*
|
| 55 |
+
* This layer only installs the client-side tracer; it does not start a devtools
|
| 56 |
+
* server, so the configured WebSocket endpoint must already be reachable. It
|
| 57 |
+
* relies on `globalThis.WebSocket` being available in the runtime.
|
| 58 |
+
*
|
| 59 |
+
* @see {@link layerWebSocket} for installing the devtools tracer with an explicit `WebSocketConstructor` requirement
|
| 60 |
+
* @see {@link layerSocket} for installing the devtools tracer over an existing `Socket` transport
|
| 61 |
+
*
|
| 62 |
* @category layers
|
| 63 |
+
* @since 4.0.0
|
| 64 |
*/
|
| 65 |
export const layer = (url = "ws://localhost:34437"): Layer.Layer<never> =>
|
| 66 |
layerWebSocket(url).pipe(
|
.opencode/node_modules/effect/src/unstable/devtools/DevToolsClient.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Cause from "../../Cause.ts"
|
|
@@ -21,8 +28,11 @@ const RequestSchema = Schema.toCodecJson(DevToolsSchema.Request)
|
|
| 21 |
const ResponseSchema = Schema.toCodecJson(DevToolsSchema.Response)
|
| 22 |
|
| 23 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
* @since 4.0.0
|
| 25 |
-
* @category tags
|
| 26 |
*/
|
| 27 |
export class DevToolsClient extends Context.Service<
|
| 28 |
DevToolsClient,
|
|
@@ -103,8 +113,30 @@ const toMetricsSnapshot = (
|
|
| 103 |
})
|
| 104 |
|
| 105 |
/**
|
| 106 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
* @category constructors
|
|
|
|
| 108 |
*/
|
| 109 |
export const make: Effect.Effect<
|
| 110 |
DevToolsClient["Service"],
|
|
@@ -118,8 +150,30 @@ export const make: Effect.Effect<
|
|
| 118 |
)
|
| 119 |
|
| 120 |
/**
|
| 121 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
* @category layers
|
|
|
|
| 123 |
*/
|
| 124 |
export const layer: Layer.Layer<DevToolsClient, never, Socket.Socket> = Layer.effect(DevToolsClient, make)
|
| 125 |
|
|
@@ -157,8 +211,11 @@ const makeTracerEffect = Effect.gen(function*() {
|
|
| 157 |
})
|
| 158 |
|
| 159 |
/**
|
| 160 |
-
*
|
|
|
|
|
|
|
| 161 |
* @category constructors
|
|
|
|
| 162 |
*/
|
| 163 |
export const makeTracer: Effect.Effect<Tracer.Tracer, never, DevToolsClient> = makeTracerEffect.pipe(
|
| 164 |
Effect.annotateLogs({
|
|
@@ -168,8 +225,11 @@ export const makeTracer: Effect.Effect<Tracer.Tracer, never, DevToolsClient> = m
|
|
| 168 |
)
|
| 169 |
|
| 170 |
/**
|
| 171 |
-
*
|
|
|
|
|
|
|
| 172 |
* @category layers
|
|
|
|
| 173 |
*/
|
| 174 |
export const layerTracer: Layer.Layer<never, never, Socket.Socket> = Layer.effect(Tracer.Tracer, makeTracer).pipe(
|
| 175 |
Layer.provide(layer)
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Low-level devtools client and tracer wiring over the current `Socket`.
|
| 3 |
+
*
|
| 4 |
+
* `DevToolsClient` speaks the devtools NDJSON protocol used by the unstable
|
| 5 |
+
* devtools integration. It sends span starts, span events, span completions,
|
| 6 |
+
* ping messages, and metric snapshots through a socket, then exposes tracer
|
| 7 |
+
* layers that forward telemetry while preserving the current tracer's behavior.
|
| 8 |
+
*
|
| 9 |
* @since 4.0.0
|
| 10 |
*/
|
| 11 |
import * as Cause from "../../Cause.ts"
|
|
|
|
| 28 |
const ResponseSchema = Schema.toCodecJson(DevToolsSchema.Response)
|
| 29 |
|
| 30 |
/**
|
| 31 |
+
* Service for sending span and span-event telemetry to the Effect devtools
|
| 32 |
+
* connection.
|
| 33 |
+
*
|
| 34 |
+
* @category services
|
| 35 |
* @since 4.0.0
|
|
|
|
| 36 |
*/
|
| 37 |
export class DevToolsClient extends Context.Service<
|
| 38 |
DevToolsClient,
|
|
|
|
| 113 |
})
|
| 114 |
|
| 115 |
/**
|
| 116 |
+
* Creates a devtools client over the current `Socket`, speaking the devtools
|
| 117 |
+
* NDJSON protocol, sending periodic pings, and responding to metrics snapshot
|
| 118 |
+
* requests.
|
| 119 |
+
*
|
| 120 |
+
* **When to use**
|
| 121 |
+
*
|
| 122 |
+
* Use when you already have a `Socket` and need the low-level `DevToolsClient`
|
| 123 |
+
* service to exchange devtools telemetry directly.
|
| 124 |
+
*
|
| 125 |
+
* **Details**
|
| 126 |
+
*
|
| 127 |
+
* The effect requires `Scope` because it starts background fibers for the socket
|
| 128 |
+
* stream and heartbeat.
|
| 129 |
+
*
|
| 130 |
+
* **Gotchas**
|
| 131 |
+
*
|
| 132 |
+
* `make` creates only the client service; tracing is installed separately.
|
| 133 |
+
*
|
| 134 |
+
* @see {@link layer} for providing the client as a layer
|
| 135 |
+
* @see {@link makeTracer} for creating a tracer after a `DevToolsClient` is available
|
| 136 |
+
* @see {@link layerTracer} for creating the client from the current `Socket` and installing the tracer as a layer
|
| 137 |
+
*
|
| 138 |
* @category constructors
|
| 139 |
+
* @since 4.0.0
|
| 140 |
*/
|
| 141 |
export const make: Effect.Effect<
|
| 142 |
DevToolsClient["Service"],
|
|
|
|
| 150 |
)
|
| 151 |
|
| 152 |
/**
|
| 153 |
+
* Layer that provides `DevToolsClient` using the current `Socket`.
|
| 154 |
+
*
|
| 155 |
+
* **When to use**
|
| 156 |
+
*
|
| 157 |
+
* Use to provide the low-level `DevToolsClient` service over an existing
|
| 158 |
+
* `Socket` for custom devtools integrations that send telemetry through the
|
| 159 |
+
* client directly.
|
| 160 |
+
*
|
| 161 |
+
* **Details**
|
| 162 |
+
*
|
| 163 |
+
* It delegates to `make`, so it speaks the devtools NDJSON protocol over the
|
| 164 |
+
* provided `Socket`, sends periodic pings, responds to metrics snapshot
|
| 165 |
+
* requests, and finalizes its background fibers when the layer scope closes.
|
| 166 |
+
*
|
| 167 |
+
* **Gotchas**
|
| 168 |
+
*
|
| 169 |
+
* This layer only provides the client. It does not install the devtools tracer
|
| 170 |
+
* by itself.
|
| 171 |
+
*
|
| 172 |
+
* @see {@link make} for constructing the client as a scoped effect instead of a layer
|
| 173 |
+
* @see {@link layerTracer} for a higher-level layer that creates the client and installs the devtools tracer
|
| 174 |
+
*
|
| 175 |
* @category layers
|
| 176 |
+
* @since 4.0.0
|
| 177 |
*/
|
| 178 |
export const layer: Layer.Layer<DevToolsClient, never, Socket.Socket> = Layer.effect(DevToolsClient, make)
|
| 179 |
|
|
|
|
| 211 |
})
|
| 212 |
|
| 213 |
/**
|
| 214 |
+
* Creates a tracer that delegates to the current tracer while sending span
|
| 215 |
+
* starts, span events, and span ends to `DevToolsClient`.
|
| 216 |
+
*
|
| 217 |
* @category constructors
|
| 218 |
+
* @since 4.0.0
|
| 219 |
*/
|
| 220 |
export const makeTracer: Effect.Effect<Tracer.Tracer, never, DevToolsClient> = makeTracerEffect.pipe(
|
| 221 |
Effect.annotateLogs({
|
|
|
|
| 225 |
)
|
| 226 |
|
| 227 |
/**
|
| 228 |
+
* Layer that creates a `DevToolsClient` from the current `Socket` and installs
|
| 229 |
+
* the devtools tracer.
|
| 230 |
+
*
|
| 231 |
* @category layers
|
| 232 |
+
* @since 4.0.0
|
| 233 |
*/
|
| 234 |
export const layerTracer: Layer.Layer<never, never, Socket.Socket> = Layer.effect(Tracer.Tracer, makeTracer).pipe(
|
| 235 |
Layer.provide(layer)
|
.opencode/node_modules/effect/src/unstable/devtools/DevToolsSchema.ts
CHANGED
|
@@ -1,4 +1,12 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Exit from "../../Exit.ts"
|
|
@@ -8,8 +16,11 @@ import * as Schema from "../../Schema.ts"
|
|
| 8 |
import * as SchemaTransformation from "../../SchemaTransformation.ts"
|
| 9 |
|
| 10 |
/**
|
| 11 |
-
*
|
|
|
|
|
|
|
| 12 |
* @category schemas
|
|
|
|
| 13 |
*/
|
| 14 |
export const SpanStatusStarted = Schema.Struct({
|
| 15 |
_tag: Schema.tag("Started"),
|
|
@@ -17,20 +28,26 @@ export const SpanStatusStarted = Schema.Struct({
|
|
| 17 |
})
|
| 18 |
|
| 19 |
/**
|
| 20 |
-
*
|
|
|
|
| 21 |
* @category schemas
|
|
|
|
| 22 |
*/
|
| 23 |
export type SpanStatusStarted = Schema.Schema.Type<typeof SpanStatusStarted>
|
| 24 |
|
| 25 |
/**
|
| 26 |
-
*
|
|
|
|
|
|
|
|
|
|
| 27 |
* @category schemas
|
|
|
|
| 28 |
*/
|
| 29 |
export const SpanStatusEnded = Schema.Struct({
|
| 30 |
_tag: Schema.tag("Ended"),
|
| 31 |
startTime: Schema.BigInt,
|
| 32 |
endTime: Schema.BigInt,
|
| 33 |
-
exit: Schema.Exit(Schema.Void, Schema.
|
| 34 |
Schema.decodeTo(
|
| 35 |
Schema.Exit(Schema.Unknown, Schema.Unknown, Schema.Unknown),
|
| 36 |
SchemaTransformation.transform({
|
|
@@ -42,26 +59,36 @@ export const SpanStatusEnded = Schema.Struct({
|
|
| 42 |
})
|
| 43 |
|
| 44 |
/**
|
| 45 |
-
*
|
|
|
|
|
|
|
| 46 |
* @category schemas
|
|
|
|
| 47 |
*/
|
| 48 |
export type SpanStatusEnded = Schema.Schema.Type<typeof SpanStatusEnded>
|
| 49 |
|
| 50 |
/**
|
| 51 |
-
*
|
|
|
|
| 52 |
* @category schemas
|
|
|
|
| 53 |
*/
|
| 54 |
export const SpanStatus = Schema.Union([SpanStatusStarted, SpanStatusEnded])
|
| 55 |
|
| 56 |
/**
|
| 57 |
-
*
|
|
|
|
| 58 |
* @category schemas
|
|
|
|
| 59 |
*/
|
| 60 |
export type SpanStatus = Schema.Schema.Type<typeof SpanStatus>
|
| 61 |
|
| 62 |
/**
|
| 63 |
-
*
|
|
|
|
|
|
|
| 64 |
* @category schemas
|
|
|
|
| 65 |
*/
|
| 66 |
export interface ExternalSpan {
|
| 67 |
readonly _tag: "ExternalSpan"
|
|
@@ -71,8 +98,11 @@ export interface ExternalSpan {
|
|
| 71 |
}
|
| 72 |
|
| 73 |
/**
|
| 74 |
-
*
|
|
|
|
|
|
|
| 75 |
* @category schemas
|
|
|
|
| 76 |
*/
|
| 77 |
export const ExternalSpan: Schema.Codec<ExternalSpan> = Schema.Struct({
|
| 78 |
_tag: Schema.tag("ExternalSpan"),
|
|
@@ -82,8 +112,11 @@ export const ExternalSpan: Schema.Codec<ExternalSpan> = Schema.Struct({
|
|
| 82 |
})
|
| 83 |
|
| 84 |
/**
|
| 85 |
-
*
|
|
|
|
|
|
|
| 86 |
* @category schemas
|
|
|
|
| 87 |
*/
|
| 88 |
export interface Span {
|
| 89 |
readonly _tag: "Span"
|
|
@@ -97,8 +130,10 @@ export interface Span {
|
|
| 97 |
}
|
| 98 |
|
| 99 |
/**
|
| 100 |
-
*
|
|
|
|
| 101 |
* @category schemas
|
|
|
|
| 102 |
*/
|
| 103 |
export const Span: Schema.Codec<Span> = Schema.Struct({
|
| 104 |
_tag: Schema.tag("Span"),
|
|
@@ -112,8 +147,11 @@ export const Span: Schema.Codec<Span> = Schema.Struct({
|
|
| 112 |
})
|
| 113 |
|
| 114 |
/**
|
| 115 |
-
*
|
|
|
|
|
|
|
| 116 |
* @category schemas
|
|
|
|
| 117 |
*/
|
| 118 |
export const SpanEvent = Schema.Struct({
|
| 119 |
_tag: Schema.tag("SpanEvent"),
|
|
@@ -125,68 +163,90 @@ export const SpanEvent = Schema.Struct({
|
|
| 125 |
})
|
| 126 |
|
| 127 |
/**
|
| 128 |
-
*
|
|
|
|
| 129 |
* @category schemas
|
|
|
|
| 130 |
*/
|
| 131 |
export type SpanEvent = Schema.Schema.Type<typeof SpanEvent>
|
| 132 |
|
| 133 |
/**
|
| 134 |
-
*
|
|
|
|
|
|
|
| 135 |
* @category schemas
|
|
|
|
| 136 |
*/
|
| 137 |
export type ParentSpan = Span | ExternalSpan
|
| 138 |
|
| 139 |
/**
|
| 140 |
-
*
|
|
|
|
|
|
|
| 141 |
* @category schemas
|
|
|
|
| 142 |
*/
|
| 143 |
export const ParentSpan = Schema.Union([Span, ExternalSpan])
|
| 144 |
|
| 145 |
/**
|
| 146 |
-
*
|
|
|
|
| 147 |
* @category schemas
|
|
|
|
| 148 |
*/
|
| 149 |
export const Ping = Schema.Struct({
|
| 150 |
_tag: Schema.tag("Ping")
|
| 151 |
})
|
| 152 |
|
| 153 |
/**
|
| 154 |
-
*
|
|
|
|
| 155 |
* @category schemas
|
|
|
|
| 156 |
*/
|
| 157 |
export type Ping = Schema.Schema.Type<typeof Ping>
|
| 158 |
|
| 159 |
/**
|
| 160 |
-
*
|
|
|
|
| 161 |
* @category schemas
|
|
|
|
| 162 |
*/
|
| 163 |
export const Pong = Schema.Struct({
|
| 164 |
_tag: Schema.tag("Pong")
|
| 165 |
})
|
| 166 |
|
| 167 |
/**
|
| 168 |
-
*
|
|
|
|
| 169 |
* @category schemas
|
|
|
|
| 170 |
*/
|
| 171 |
export type Pong = Schema.Schema.Type<typeof Pong>
|
| 172 |
|
| 173 |
/**
|
| 174 |
-
*
|
|
|
|
| 175 |
* @category schemas
|
|
|
|
| 176 |
*/
|
| 177 |
export const MetricsRequest = Schema.Struct({
|
| 178 |
_tag: Schema.tag("MetricsRequest")
|
| 179 |
})
|
| 180 |
|
| 181 |
/**
|
| 182 |
-
*
|
|
|
|
| 183 |
* @category schemas
|
|
|
|
| 184 |
*/
|
| 185 |
export type MetricsRequest = Schema.Schema.Type<typeof MetricsRequest>
|
| 186 |
|
| 187 |
/**
|
| 188 |
-
*
|
|
|
|
| 189 |
* @category schemas
|
|
|
|
| 190 |
*/
|
| 191 |
export const MetricLabel = Schema.Struct({
|
| 192 |
key: Schema.String,
|
|
@@ -194,8 +254,10 @@ export const MetricLabel = Schema.Struct({
|
|
| 194 |
})
|
| 195 |
|
| 196 |
/**
|
| 197 |
-
*
|
|
|
|
| 198 |
* @category schemas
|
|
|
|
| 199 |
*/
|
| 200 |
export type MetricLabel = Schema.Schema.Type<typeof MetricLabel>
|
| 201 |
|
|
@@ -209,8 +271,11 @@ const metric = <Type extends string, State extends Schema.Top>(type: Type, state
|
|
| 209 |
})
|
| 210 |
|
| 211 |
/**
|
| 212 |
-
*
|
|
|
|
|
|
|
| 213 |
* @category schemas
|
|
|
|
| 214 |
*/
|
| 215 |
export const Counter = metric(
|
| 216 |
"Counter",
|
|
@@ -221,14 +286,27 @@ export const Counter = metric(
|
|
| 221 |
)
|
| 222 |
|
| 223 |
/**
|
| 224 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
* @category schemas
|
|
|
|
| 226 |
*/
|
| 227 |
export type Counter = Schema.Schema.Type<typeof Counter>
|
| 228 |
|
| 229 |
/**
|
| 230 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
* @category schemas
|
|
|
|
| 232 |
*/
|
| 233 |
export const Frequency = metric(
|
| 234 |
"Frequency",
|
|
@@ -238,14 +316,26 @@ export const Frequency = metric(
|
|
| 238 |
)
|
| 239 |
|
| 240 |
/**
|
| 241 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
* @category schemas
|
|
|
|
| 243 |
*/
|
| 244 |
export type Frequency = Schema.Schema.Type<typeof Frequency>
|
| 245 |
|
| 246 |
/**
|
| 247 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
* @category schemas
|
|
|
|
| 249 |
*/
|
| 250 |
export const Gauge = metric(
|
| 251 |
"Gauge",
|
|
@@ -255,14 +345,27 @@ export const Gauge = metric(
|
|
| 255 |
)
|
| 256 |
|
| 257 |
/**
|
| 258 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
* @category schemas
|
|
|
|
| 260 |
*/
|
| 261 |
export type Gauge = Schema.Schema.Type<typeof Gauge>
|
| 262 |
|
| 263 |
/**
|
| 264 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
* @category schemas
|
|
|
|
| 266 |
*/
|
| 267 |
export const Histogram = metric(
|
| 268 |
"Histogram",
|
|
@@ -276,14 +379,28 @@ export const Histogram = metric(
|
|
| 276 |
)
|
| 277 |
|
| 278 |
/**
|
| 279 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
* @category schemas
|
|
|
|
| 281 |
*/
|
| 282 |
export type Histogram = Schema.Schema.Type<typeof Histogram>
|
| 283 |
|
| 284 |
/**
|
| 285 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
* @category schemas
|
|
|
|
| 287 |
*/
|
| 288 |
export const Summary = metric(
|
| 289 |
"Summary",
|
|
@@ -297,26 +414,49 @@ export const Summary = metric(
|
|
| 297 |
)
|
| 298 |
|
| 299 |
/**
|
| 300 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
* @category schemas
|
|
|
|
| 302 |
*/
|
| 303 |
export type Summary = Schema.Schema.Type<typeof Summary>
|
| 304 |
|
| 305 |
/**
|
| 306 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 307 |
* @category schemas
|
|
|
|
| 308 |
*/
|
| 309 |
export const Metric = Schema.Union([Counter, Frequency, Gauge, Histogram, Summary])
|
| 310 |
|
| 311 |
/**
|
| 312 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
* @category schemas
|
|
|
|
| 314 |
*/
|
| 315 |
export type Metric = Schema.Schema.Type<typeof Metric>
|
| 316 |
|
| 317 |
/**
|
| 318 |
-
*
|
|
|
|
|
|
|
| 319 |
* @category schemas
|
|
|
|
| 320 |
*/
|
| 321 |
export const MetricsSnapshot = Schema.Struct({
|
| 322 |
_tag: Schema.tag("MetricsSnapshot"),
|
|
@@ -324,55 +464,95 @@ export const MetricsSnapshot = Schema.Struct({
|
|
| 324 |
})
|
| 325 |
|
| 326 |
/**
|
| 327 |
-
*
|
|
|
|
| 328 |
* @category schemas
|
|
|
|
| 329 |
*/
|
| 330 |
export type MetricsSnapshot = Schema.Schema.Type<typeof MetricsSnapshot>
|
| 331 |
|
| 332 |
/**
|
| 333 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 334 |
* @category schemas
|
|
|
|
| 335 |
*/
|
| 336 |
export const Request = Schema.Union([Ping, Span, SpanEvent, MetricsSnapshot])
|
| 337 |
|
| 338 |
/**
|
| 339 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 340 |
* @category schemas
|
|
|
|
| 341 |
*/
|
| 342 |
export type Request = Schema.Schema.Type<typeof Request>
|
| 343 |
|
| 344 |
/**
|
|
|
|
|
|
|
| 345 |
* @since 4.0.0
|
| 346 |
-
* @category schemas
|
| 347 |
*/
|
| 348 |
export declare namespace Request {
|
| 349 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
* @since 4.0.0
|
| 351 |
-
* @category schemas
|
| 352 |
*/
|
| 353 |
export type WithoutPing = Exclude<Request, { readonly _tag: "Ping" }>
|
| 354 |
}
|
| 355 |
|
| 356 |
/**
|
| 357 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
* @category schemas
|
|
|
|
| 359 |
*/
|
| 360 |
export const Response = Schema.Union([Pong, MetricsRequest])
|
| 361 |
|
| 362 |
/**
|
| 363 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
* @category schemas
|
|
|
|
| 365 |
*/
|
| 366 |
export type Response = Schema.Schema.Type<typeof Response>
|
| 367 |
|
| 368 |
/**
|
|
|
|
|
|
|
| 369 |
* @since 4.0.0
|
| 370 |
-
* @category schemas
|
| 371 |
*/
|
| 372 |
export declare namespace Response {
|
| 373 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 374 |
* @since 4.0.0
|
| 375 |
-
* @category schemas
|
| 376 |
*/
|
| 377 |
export type WithoutPong = Exclude<Response, { readonly _tag: "Pong" }>
|
| 378 |
}
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Defines the experimental wire schema used by the Effect devtools protocol.
|
| 3 |
+
*
|
| 4 |
+
* The module contains the serialized message shapes exchanged between devtools
|
| 5 |
+
* clients and servers: span snapshots, span events, metric snapshots,
|
| 6 |
+
* heartbeats, and request/response unions. Use these schemas at protocol
|
| 7 |
+
* boundaries to encode, decode, or validate custom transports that need to
|
| 8 |
+
* interoperate with the built-in unstable devtools client and server.
|
| 9 |
+
*
|
| 10 |
* @since 4.0.0
|
| 11 |
*/
|
| 12 |
import * as Exit from "../../Exit.ts"
|
|
|
|
| 16 |
import * as SchemaTransformation from "../../SchemaTransformation.ts"
|
| 17 |
|
| 18 |
/**
|
| 19 |
+
* Schema for a span status representing a span that has started but not yet
|
| 20 |
+
* ended.
|
| 21 |
+
*
|
| 22 |
* @category schemas
|
| 23 |
+
* @since 4.0.0
|
| 24 |
*/
|
| 25 |
export const SpanStatusStarted = Schema.Struct({
|
| 26 |
_tag: Schema.tag("Started"),
|
|
|
|
| 28 |
})
|
| 29 |
|
| 30 |
/**
|
| 31 |
+
* Type of a span status representing a span that has started but not yet ended.
|
| 32 |
+
*
|
| 33 |
* @category schemas
|
| 34 |
+
* @since 4.0.0
|
| 35 |
*/
|
| 36 |
export type SpanStatusStarted = Schema.Schema.Type<typeof SpanStatusStarted>
|
| 37 |
|
| 38 |
/**
|
| 39 |
+
* Schema for a span status representing an ended span, including start time,
|
| 40 |
+
* end time, and encoded exit status. Encoding drops success values with
|
| 41 |
+
* `Exit.asVoid`.
|
| 42 |
+
*
|
| 43 |
* @category schemas
|
| 44 |
+
* @since 4.0.0
|
| 45 |
*/
|
| 46 |
export const SpanStatusEnded = Schema.Struct({
|
| 47 |
_tag: Schema.tag("Ended"),
|
| 48 |
startTime: Schema.BigInt,
|
| 49 |
endTime: Schema.BigInt,
|
| 50 |
+
exit: Schema.Exit(Schema.Void, Schema.Defect({ includeStack: true }), Schema.Defect({ includeStack: true })).pipe(
|
| 51 |
Schema.decodeTo(
|
| 52 |
Schema.Exit(Schema.Unknown, Schema.Unknown, Schema.Unknown),
|
| 53 |
SchemaTransformation.transform({
|
|
|
|
| 59 |
})
|
| 60 |
|
| 61 |
/**
|
| 62 |
+
* Type of a span status representing an ended span with start time, end time,
|
| 63 |
+
* and exit status.
|
| 64 |
+
*
|
| 65 |
* @category schemas
|
| 66 |
+
* @since 4.0.0
|
| 67 |
*/
|
| 68 |
export type SpanStatusEnded = Schema.Schema.Type<typeof SpanStatusEnded>
|
| 69 |
|
| 70 |
/**
|
| 71 |
+
* Schema for devtools span status, either started or ended.
|
| 72 |
+
*
|
| 73 |
* @category schemas
|
| 74 |
+
* @since 4.0.0
|
| 75 |
*/
|
| 76 |
export const SpanStatus = Schema.Union([SpanStatusStarted, SpanStatusEnded])
|
| 77 |
|
| 78 |
/**
|
| 79 |
+
* Type of a devtools span status, either started or ended.
|
| 80 |
+
*
|
| 81 |
* @category schemas
|
| 82 |
+
* @since 4.0.0
|
| 83 |
*/
|
| 84 |
export type SpanStatus = Schema.Schema.Type<typeof SpanStatus>
|
| 85 |
|
| 86 |
/**
|
| 87 |
+
* Serialized parent span context for a span created outside the current
|
| 88 |
+
* devtools span tree.
|
| 89 |
+
*
|
| 90 |
* @category schemas
|
| 91 |
+
* @since 4.0.0
|
| 92 |
*/
|
| 93 |
export interface ExternalSpan {
|
| 94 |
readonly _tag: "ExternalSpan"
|
|
|
|
| 98 |
}
|
| 99 |
|
| 100 |
/**
|
| 101 |
+
* Schema for an external parent span context containing span id, trace id, and
|
| 102 |
+
* sampling flag.
|
| 103 |
+
*
|
| 104 |
* @category schemas
|
| 105 |
+
* @since 4.0.0
|
| 106 |
*/
|
| 107 |
export const ExternalSpan: Schema.Codec<ExternalSpan> = Schema.Struct({
|
| 108 |
_tag: Schema.tag("ExternalSpan"),
|
|
|
|
| 112 |
})
|
| 113 |
|
| 114 |
/**
|
| 115 |
+
* Telemetry payload for an Effect span sent to devtools, including identity,
|
| 116 |
+
* attributes, status, sampling flag, and optional parent span.
|
| 117 |
+
*
|
| 118 |
* @category schemas
|
| 119 |
+
* @since 4.0.0
|
| 120 |
*/
|
| 121 |
export interface Span {
|
| 122 |
readonly _tag: "Span"
|
|
|
|
| 130 |
}
|
| 131 |
|
| 132 |
/**
|
| 133 |
+
* Schema for an Effect span telemetry payload sent to devtools.
|
| 134 |
+
*
|
| 135 |
* @category schemas
|
| 136 |
+
* @since 4.0.0
|
| 137 |
*/
|
| 138 |
export const Span: Schema.Codec<Span> = Schema.Struct({
|
| 139 |
_tag: Schema.tag("Span"),
|
|
|
|
| 147 |
})
|
| 148 |
|
| 149 |
/**
|
| 150 |
+
* Schema for a named event emitted by a span, including trace id, span id,
|
| 151 |
+
* start time, and optional attributes.
|
| 152 |
+
*
|
| 153 |
* @category schemas
|
| 154 |
+
* @since 4.0.0
|
| 155 |
*/
|
| 156 |
export const SpanEvent = Schema.Struct({
|
| 157 |
_tag: Schema.tag("SpanEvent"),
|
|
|
|
| 163 |
})
|
| 164 |
|
| 165 |
/**
|
| 166 |
+
* Type of a named event emitted by a span and sent to devtools.
|
| 167 |
+
*
|
| 168 |
* @category schemas
|
| 169 |
+
* @since 4.0.0
|
| 170 |
*/
|
| 171 |
export type SpanEvent = Schema.Schema.Type<typeof SpanEvent>
|
| 172 |
|
| 173 |
/**
|
| 174 |
+
* Type of a span parent, represented either by a devtools `Span` payload or an
|
| 175 |
+
* `ExternalSpan` context.
|
| 176 |
+
*
|
| 177 |
* @category schemas
|
| 178 |
+
* @since 4.0.0
|
| 179 |
*/
|
| 180 |
export type ParentSpan = Span | ExternalSpan
|
| 181 |
|
| 182 |
/**
|
| 183 |
+
* Schema for a span parent, either a full devtools `Span` payload or an
|
| 184 |
+
* `ExternalSpan` context.
|
| 185 |
+
*
|
| 186 |
* @category schemas
|
| 187 |
+
* @since 4.0.0
|
| 188 |
*/
|
| 189 |
export const ParentSpan = Schema.Union([Span, ExternalSpan])
|
| 190 |
|
| 191 |
/**
|
| 192 |
+
* Schema for the devtools heartbeat request sent by the client.
|
| 193 |
+
*
|
| 194 |
* @category schemas
|
| 195 |
+
* @since 4.0.0
|
| 196 |
*/
|
| 197 |
export const Ping = Schema.Struct({
|
| 198 |
_tag: Schema.tag("Ping")
|
| 199 |
})
|
| 200 |
|
| 201 |
/**
|
| 202 |
+
* Type of the devtools heartbeat request sent by the client.
|
| 203 |
+
*
|
| 204 |
* @category schemas
|
| 205 |
+
* @since 4.0.0
|
| 206 |
*/
|
| 207 |
export type Ping = Schema.Schema.Type<typeof Ping>
|
| 208 |
|
| 209 |
/**
|
| 210 |
+
* Schema for the devtools heartbeat response.
|
| 211 |
+
*
|
| 212 |
* @category schemas
|
| 213 |
+
* @since 4.0.0
|
| 214 |
*/
|
| 215 |
export const Pong = Schema.Struct({
|
| 216 |
_tag: Schema.tag("Pong")
|
| 217 |
})
|
| 218 |
|
| 219 |
/**
|
| 220 |
+
* Type of the devtools heartbeat response.
|
| 221 |
+
*
|
| 222 |
* @category schemas
|
| 223 |
+
* @since 4.0.0
|
| 224 |
*/
|
| 225 |
export type Pong = Schema.Schema.Type<typeof Pong>
|
| 226 |
|
| 227 |
/**
|
| 228 |
+
* Schema for a devtools request asking the client to send a metrics snapshot.
|
| 229 |
+
*
|
| 230 |
* @category schemas
|
| 231 |
+
* @since 4.0.0
|
| 232 |
*/
|
| 233 |
export const MetricsRequest = Schema.Struct({
|
| 234 |
_tag: Schema.tag("MetricsRequest")
|
| 235 |
})
|
| 236 |
|
| 237 |
/**
|
| 238 |
+
* Type of a devtools request asking the client to send a metrics snapshot.
|
| 239 |
+
*
|
| 240 |
* @category schemas
|
| 241 |
+
* @since 4.0.0
|
| 242 |
*/
|
| 243 |
export type MetricsRequest = Schema.Schema.Type<typeof MetricsRequest>
|
| 244 |
|
| 245 |
/**
|
| 246 |
+
* Schema for a metric label key/value pair in a devtools metrics snapshot.
|
| 247 |
+
*
|
| 248 |
* @category schemas
|
| 249 |
+
* @since 4.0.0
|
| 250 |
*/
|
| 251 |
export const MetricLabel = Schema.Struct({
|
| 252 |
key: Schema.String,
|
|
|
|
| 254 |
})
|
| 255 |
|
| 256 |
/**
|
| 257 |
+
* Type of a metric label key/value pair in a devtools metrics snapshot.
|
| 258 |
+
*
|
| 259 |
* @category schemas
|
| 260 |
+
* @since 4.0.0
|
| 261 |
*/
|
| 262 |
export type MetricLabel = Schema.Schema.Type<typeof MetricLabel>
|
| 263 |
|
|
|
|
| 271 |
})
|
| 272 |
|
| 273 |
/**
|
| 274 |
+
* Schema for a counter metric snapshot, including the count and whether updates
|
| 275 |
+
* are incremental.
|
| 276 |
+
*
|
| 277 |
* @category schemas
|
| 278 |
+
* @since 4.0.0
|
| 279 |
*/
|
| 280 |
export const Counter = metric(
|
| 281 |
"Counter",
|
|
|
|
| 286 |
)
|
| 287 |
|
| 288 |
/**
|
| 289 |
+
* Type of a devtools counter metric snapshot.
|
| 290 |
+
*
|
| 291 |
+
* **Details**
|
| 292 |
+
*
|
| 293 |
+
* The state contains the current count and whether the counter reports
|
| 294 |
+
* incremental updates.
|
| 295 |
+
*
|
| 296 |
* @category schemas
|
| 297 |
+
* @since 4.0.0
|
| 298 |
*/
|
| 299 |
export type Counter = Schema.Schema.Type<typeof Counter>
|
| 300 |
|
| 301 |
/**
|
| 302 |
+
* Schema for a devtools frequency metric snapshot.
|
| 303 |
+
*
|
| 304 |
+
* **Details**
|
| 305 |
+
*
|
| 306 |
+
* The metric state records occurrence counts by string key.
|
| 307 |
+
*
|
| 308 |
* @category schemas
|
| 309 |
+
* @since 4.0.0
|
| 310 |
*/
|
| 311 |
export const Frequency = metric(
|
| 312 |
"Frequency",
|
|
|
|
| 316 |
)
|
| 317 |
|
| 318 |
/**
|
| 319 |
+
* Type of a devtools frequency metric snapshot.
|
| 320 |
+
*
|
| 321 |
+
* **Details**
|
| 322 |
+
*
|
| 323 |
+
* The state maps observed string values to occurrence counts.
|
| 324 |
+
*
|
| 325 |
* @category schemas
|
| 326 |
+
* @since 4.0.0
|
| 327 |
*/
|
| 328 |
export type Frequency = Schema.Schema.Type<typeof Frequency>
|
| 329 |
|
| 330 |
/**
|
| 331 |
+
* Schema for a devtools gauge metric snapshot.
|
| 332 |
+
*
|
| 333 |
+
* **Details**
|
| 334 |
+
*
|
| 335 |
+
* The metric state contains the current numeric or bigint value.
|
| 336 |
+
*
|
| 337 |
* @category schemas
|
| 338 |
+
* @since 4.0.0
|
| 339 |
*/
|
| 340 |
export const Gauge = metric(
|
| 341 |
"Gauge",
|
|
|
|
| 345 |
)
|
| 346 |
|
| 347 |
/**
|
| 348 |
+
* Type of a devtools gauge metric snapshot.
|
| 349 |
+
*
|
| 350 |
+
* **Details**
|
| 351 |
+
*
|
| 352 |
+
* The state contains the current numeric or bigint value.
|
| 353 |
+
*
|
| 354 |
* @category schemas
|
| 355 |
+
* @since 4.0.0
|
| 356 |
*/
|
| 357 |
export type Gauge = Schema.Schema.Type<typeof Gauge>
|
| 358 |
|
| 359 |
/**
|
| 360 |
+
* Schema for a devtools histogram metric snapshot.
|
| 361 |
+
*
|
| 362 |
+
* **Details**
|
| 363 |
+
*
|
| 364 |
+
* The metric state includes bucket counts plus the total count, minimum,
|
| 365 |
+
* maximum, and sum.
|
| 366 |
+
*
|
| 367 |
* @category schemas
|
| 368 |
+
* @since 4.0.0
|
| 369 |
*/
|
| 370 |
export const Histogram = metric(
|
| 371 |
"Histogram",
|
|
|
|
| 379 |
)
|
| 380 |
|
| 381 |
/**
|
| 382 |
+
* Type of a devtools histogram metric snapshot.
|
| 383 |
+
*
|
| 384 |
+
* **Details**
|
| 385 |
+
*
|
| 386 |
+
* The state includes bucket counts plus the total count, minimum, maximum, and
|
| 387 |
+
* sum.
|
| 388 |
+
*
|
| 389 |
* @category schemas
|
| 390 |
+
* @since 4.0.0
|
| 391 |
*/
|
| 392 |
export type Histogram = Schema.Schema.Type<typeof Histogram>
|
| 393 |
|
| 394 |
/**
|
| 395 |
+
* Schema for a devtools summary metric snapshot.
|
| 396 |
+
*
|
| 397 |
+
* **Details**
|
| 398 |
+
*
|
| 399 |
+
* The metric state contains quantile values plus the total count, minimum,
|
| 400 |
+
* maximum, and sum.
|
| 401 |
+
*
|
| 402 |
* @category schemas
|
| 403 |
+
* @since 4.0.0
|
| 404 |
*/
|
| 405 |
export const Summary = metric(
|
| 406 |
"Summary",
|
|
|
|
| 414 |
)
|
| 415 |
|
| 416 |
/**
|
| 417 |
+
* Type of a devtools summary metric snapshot.
|
| 418 |
+
*
|
| 419 |
+
* **Details**
|
| 420 |
+
*
|
| 421 |
+
* The state contains quantile values plus the total count, minimum, maximum,
|
| 422 |
+
* and sum.
|
| 423 |
+
*
|
| 424 |
* @category schemas
|
| 425 |
+
* @since 4.0.0
|
| 426 |
*/
|
| 427 |
export type Summary = Schema.Schema.Type<typeof Summary>
|
| 428 |
|
| 429 |
/**
|
| 430 |
+
* Schema for any devtools metric snapshot.
|
| 431 |
+
*
|
| 432 |
+
* **Details**
|
| 433 |
+
*
|
| 434 |
+
* Accepted metric kinds are counters, frequencies, gauges, histograms, and
|
| 435 |
+
* summaries.
|
| 436 |
+
*
|
| 437 |
* @category schemas
|
| 438 |
+
* @since 4.0.0
|
| 439 |
*/
|
| 440 |
export const Metric = Schema.Union([Counter, Frequency, Gauge, Histogram, Summary])
|
| 441 |
|
| 442 |
/**
|
| 443 |
+
* Type of any devtools metric snapshot.
|
| 444 |
+
*
|
| 445 |
+
* **Details**
|
| 446 |
+
*
|
| 447 |
+
* The union covers counters, frequencies, gauges, histograms, and summaries.
|
| 448 |
+
*
|
| 449 |
* @category schemas
|
| 450 |
+
* @since 4.0.0
|
| 451 |
*/
|
| 452 |
export type Metric = Schema.Schema.Type<typeof Metric>
|
| 453 |
|
| 454 |
/**
|
| 455 |
+
* Schema for a devtools protocol message containing the current metric
|
| 456 |
+
* snapshots.
|
| 457 |
+
*
|
| 458 |
* @category schemas
|
| 459 |
+
* @since 4.0.0
|
| 460 |
*/
|
| 461 |
export const MetricsSnapshot = Schema.Struct({
|
| 462 |
_tag: Schema.tag("MetricsSnapshot"),
|
|
|
|
| 464 |
})
|
| 465 |
|
| 466 |
/**
|
| 467 |
+
* Type of a devtools protocol message containing the current metric snapshots.
|
| 468 |
+
*
|
| 469 |
* @category schemas
|
| 470 |
+
* @since 4.0.0
|
| 471 |
*/
|
| 472 |
export type MetricsSnapshot = Schema.Schema.Type<typeof MetricsSnapshot>
|
| 473 |
|
| 474 |
/**
|
| 475 |
+
* Schema for devtools protocol requests accepted by the server.
|
| 476 |
+
*
|
| 477 |
+
* **Details**
|
| 478 |
+
*
|
| 479 |
+
* Requests include heartbeat pings, spans, span events, and metric snapshots.
|
| 480 |
+
*
|
| 481 |
* @category schemas
|
| 482 |
+
* @since 4.0.0
|
| 483 |
*/
|
| 484 |
export const Request = Schema.Union([Ping, Span, SpanEvent, MetricsSnapshot])
|
| 485 |
|
| 486 |
/**
|
| 487 |
+
* Type of devtools protocol requests accepted by the server.
|
| 488 |
+
*
|
| 489 |
+
* **Details**
|
| 490 |
+
*
|
| 491 |
+
* Requests include heartbeat pings, spans, span events, and metric snapshots.
|
| 492 |
+
*
|
| 493 |
* @category schemas
|
| 494 |
+
* @since 4.0.0
|
| 495 |
*/
|
| 496 |
export type Request = Schema.Schema.Type<typeof Request>
|
| 497 |
|
| 498 |
/**
|
| 499 |
+
* Namespace containing helper types for devtools protocol requests.
|
| 500 |
+
*
|
| 501 |
* @since 4.0.0
|
|
|
|
| 502 |
*/
|
| 503 |
export declare namespace Request {
|
| 504 |
/**
|
| 505 |
+
* Devtools request messages excluding heartbeat pings.
|
| 506 |
+
*
|
| 507 |
+
* **Details**
|
| 508 |
+
*
|
| 509 |
+
* `DevToolsServer` handles `Ping` internally and exposes only these requests
|
| 510 |
+
* to client handlers.
|
| 511 |
+
*
|
| 512 |
* @since 4.0.0
|
|
|
|
| 513 |
*/
|
| 514 |
export type WithoutPing = Exclude<Request, { readonly _tag: "Ping" }>
|
| 515 |
}
|
| 516 |
|
| 517 |
/**
|
| 518 |
+
* Schema for devtools protocol responses sent by the server.
|
| 519 |
+
*
|
| 520 |
+
* **Details**
|
| 521 |
+
*
|
| 522 |
+
* Responses include heartbeat pongs and requests for metric snapshots.
|
| 523 |
+
*
|
| 524 |
* @category schemas
|
| 525 |
+
* @since 4.0.0
|
| 526 |
*/
|
| 527 |
export const Response = Schema.Union([Pong, MetricsRequest])
|
| 528 |
|
| 529 |
/**
|
| 530 |
+
* Type of devtools protocol responses sent by the server.
|
| 531 |
+
*
|
| 532 |
+
* **Details**
|
| 533 |
+
*
|
| 534 |
+
* Responses include heartbeat pongs and requests for metric snapshots.
|
| 535 |
+
*
|
| 536 |
* @category schemas
|
| 537 |
+
* @since 4.0.0
|
| 538 |
*/
|
| 539 |
export type Response = Schema.Schema.Type<typeof Response>
|
| 540 |
|
| 541 |
/**
|
| 542 |
+
* Namespace containing helper types for devtools protocol responses.
|
| 543 |
+
*
|
| 544 |
* @since 4.0.0
|
|
|
|
| 545 |
*/
|
| 546 |
export declare namespace Response {
|
| 547 |
/**
|
| 548 |
+
* Devtools response messages excluding heartbeat pongs.
|
| 549 |
+
*
|
| 550 |
+
* **Details**
|
| 551 |
+
*
|
| 552 |
+
* `DevToolsServer` sends `Pong` internally and accepts only these responses
|
| 553 |
+
* from client handlers.
|
| 554 |
+
*
|
| 555 |
* @since 4.0.0
|
|
|
|
| 556 |
*/
|
| 557 |
export type WithoutPong = Exclude<Response, { readonly _tag: "Pong" }>
|
| 558 |
}
|
.opencode/node_modules/effect/src/unstable/devtools/DevToolsServer.ts
CHANGED
|
@@ -1,4 +1,12 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Effect from "../../Effect.ts"
|
|
@@ -14,8 +22,15 @@ const RequestSchema = Schema.toCodecJson(DevToolsSchema.Request)
|
|
| 14 |
const ResponseSchema = Schema.toCodecJson(DevToolsSchema.Response)
|
| 15 |
|
| 16 |
/**
|
| 17 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
* @category models
|
|
|
|
| 19 |
*/
|
| 20 |
export interface Client {
|
| 21 |
readonly queue: Queue.Dequeue<DevToolsSchema.Request.WithoutPing>
|
|
@@ -23,8 +38,16 @@ export interface Client {
|
|
| 23 |
}
|
| 24 |
|
| 25 |
/**
|
| 26 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
* @category constructors
|
|
|
|
| 28 |
*/
|
| 29 |
export const run: <_, E, R>(
|
| 30 |
handle: (client: Client) => Effect.Effect<_, E, R>
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Runs the server side of the unstable Effect devtools socket protocol.
|
| 3 |
+
*
|
| 4 |
+
* Use this module when an integration needs to accept devtools clients over a
|
| 5 |
+
* `SocketServer`, decode newline-delimited JSON messages, and handle each
|
| 6 |
+
* connection with application-specific logic. It does not interpret spans or
|
| 7 |
+
* metrics itself; it gives handlers a typed surface for the telemetry described
|
| 8 |
+
* by `DevToolsSchema`.
|
| 9 |
+
*
|
| 10 |
* @since 4.0.0
|
| 11 |
*/
|
| 12 |
import * as Effect from "../../Effect.ts"
|
|
|
|
| 22 |
const ResponseSchema = Schema.toCodecJson(DevToolsSchema.Response)
|
| 23 |
|
| 24 |
/**
|
| 25 |
+
* Handle for a connected devtools client.
|
| 26 |
+
*
|
| 27 |
+
* **Details**
|
| 28 |
+
*
|
| 29 |
+
* It exposes a queue of non-ping requests received from the socket and a
|
| 30 |
+
* `send` function for non-pong responses.
|
| 31 |
+
*
|
| 32 |
* @category models
|
| 33 |
+
* @since 4.0.0
|
| 34 |
*/
|
| 35 |
export interface Client {
|
| 36 |
readonly queue: Queue.Dequeue<DevToolsSchema.Request.WithoutPing>
|
|
|
|
| 38 |
}
|
| 39 |
|
| 40 |
/**
|
| 41 |
+
* Runs the devtools socket server.
|
| 42 |
+
*
|
| 43 |
+
* **Details**
|
| 44 |
+
*
|
| 45 |
+
* Each connection is decoded as NDJSON devtools protocol messages, `Ping`
|
| 46 |
+
* requests are answered with `Pong`, and all other requests are delivered
|
| 47 |
+
* through the `Client` passed to the handler.
|
| 48 |
+
*
|
| 49 |
* @category constructors
|
| 50 |
+
* @since 4.0.0
|
| 51 |
*/
|
| 52 |
export const run: <_, E, R>(
|
| 53 |
handle: (client: Client) => Effect.Effect<_, E, R>
|
.opencode/node_modules/effect/src/unstable/encoding/Msgpack.ts
CHANGED
|
@@ -1,4 +1,12 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import { Packr, Unpackr } from "msgpackr"
|
|
@@ -13,25 +21,36 @@ import * as Option from "../../Option.ts"
|
|
| 13 |
import * as Predicate from "../../Predicate.ts"
|
| 14 |
import type * as Pull from "../../Pull.ts"
|
| 15 |
import * as Schema from "../../Schema.ts"
|
| 16 |
-
import * as
|
| 17 |
-
import * as
|
| 18 |
|
| 19 |
const MsgPackErrorTypeId = "~effect/encoding/MsgPack/MsgPackError"
|
| 20 |
|
| 21 |
/**
|
| 22 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
* @category errors
|
|
|
|
| 24 |
*/
|
| 25 |
export class MsgPackError extends Data.TaggedError("MsgPackError")<{
|
| 26 |
readonly kind: "Pack" | "Unpack"
|
| 27 |
readonly cause: unknown
|
| 28 |
}> {
|
| 29 |
/**
|
|
|
|
|
|
|
| 30 |
* @since 4.0.0
|
| 31 |
*/
|
| 32 |
readonly [MsgPackErrorTypeId] = MsgPackErrorTypeId
|
| 33 |
|
| 34 |
/**
|
|
|
|
|
|
|
| 35 |
* @since 4.0.0
|
| 36 |
*/
|
| 37 |
override get message() {
|
|
@@ -40,8 +59,15 @@ export class MsgPackError extends Data.TaggedError("MsgPackError")<{
|
|
| 40 |
}
|
| 41 |
|
| 42 |
/**
|
| 43 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
* @category constructors
|
|
|
|
| 45 |
*/
|
| 46 |
export const encode = <IE = never, Done = unknown>(): Channel.Channel<
|
| 47 |
Arr.NonEmptyReadonlyArray<Uint8Array<ArrayBuffer>>,
|
|
@@ -65,8 +91,15 @@ export const encode = <IE = never, Done = unknown>(): Channel.Channel<
|
|
| 65 |
)
|
| 66 |
|
| 67 |
/**
|
| 68 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
* @category constructors
|
|
|
|
| 70 |
*/
|
| 71 |
export const encodeSchema = <S extends Schema.Top>(
|
| 72 |
schema: S
|
|
@@ -82,8 +115,15 @@ export const encodeSchema = <S extends Schema.Top>(
|
|
| 82 |
> => Channel.pipeTo(ChannelSchema.encode(schema)(), encode())
|
| 83 |
|
| 84 |
/**
|
| 85 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
* @category constructors
|
|
|
|
| 87 |
*/
|
| 88 |
export const decode = <IE = never, Done = unknown>(): Channel.Channel<
|
| 89 |
Arr.NonEmptyReadonlyArray<unknown>,
|
|
@@ -131,8 +171,15 @@ export const decode = <IE = never, Done = unknown>(): Channel.Channel<
|
|
| 131 |
)
|
| 132 |
|
| 133 |
/**
|
| 134 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
* @category constructors
|
|
|
|
| 136 |
*/
|
| 137 |
export const decodeSchema = <S extends Schema.Top>(
|
| 138 |
schema: S
|
|
@@ -148,8 +195,15 @@ export const decodeSchema = <S extends Schema.Top>(
|
|
| 148 |
> => Channel.pipeTo(decode<IE, Done>(), ChannelSchema.decodeUnknown(schema)())
|
| 149 |
|
| 150 |
/**
|
| 151 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
* @category combinators
|
|
|
|
| 153 |
*/
|
| 154 |
export const duplex = <R, IE, OE, OutDone, InDone>(
|
| 155 |
self: Channel.Channel<
|
|
@@ -176,13 +230,31 @@ export const duplex = <R, IE, OE, OutDone, InDone>(
|
|
| 176 |
)
|
| 177 |
|
| 178 |
/**
|
| 179 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
* @category combinators
|
|
|
|
| 181 |
*/
|
| 182 |
export const duplexSchema: {
|
| 183 |
/**
|
| 184 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
* @category combinators
|
|
|
|
| 186 |
*/
|
| 187 |
<In extends Schema.Top, Out extends Schema.Top>(
|
| 188 |
options: {
|
|
@@ -209,8 +281,17 @@ export const duplexSchema: {
|
|
| 209 |
R | In["EncodingServices"] | Out["DecodingServices"]
|
| 210 |
>
|
| 211 |
/**
|
| 212 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
* @category combinators
|
|
|
|
| 214 |
*/
|
| 215 |
<Out extends Schema.Top, In extends Schema.Top, OutErr, OutDone, InErr, InDone, R>(
|
| 216 |
self: Channel.Channel<
|
|
@@ -260,25 +341,39 @@ export const duplexSchema: {
|
|
| 260 |
> => ChannelSchema.duplexUnknown(duplex(self), options))
|
| 261 |
|
| 262 |
/**
|
| 263 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
* @category schemas
|
|
|
|
| 265 |
*/
|
| 266 |
export interface schema<S extends Schema.Top> extends Schema.decodeTo<S, Schema.instanceOf<Uint8Array<ArrayBuffer>>> {}
|
| 267 |
|
| 268 |
/**
|
| 269 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
* @category schemas
|
|
|
|
| 271 |
*/
|
| 272 |
-
export const transformation:
|
| 273 |
unknown,
|
| 274 |
Uint8Array<ArrayBuffer>
|
| 275 |
-
> =
|
| 276 |
decode(e, _options) {
|
| 277 |
try {
|
| 278 |
return Effect.succeed(Msgpackr.decode(e))
|
| 279 |
} catch (cause) {
|
| 280 |
return Effect.fail(
|
| 281 |
-
new
|
| 282 |
message: Predicate.hasProperty(cause, "message") ? String(cause.message) : String(cause)
|
| 283 |
})
|
| 284 |
)
|
|
@@ -289,7 +384,7 @@ export const transformation: Transformation.Transformation<
|
|
| 289 |
return Effect.succeed(Msgpackr.encode(t) as Uint8Array<ArrayBuffer>)
|
| 290 |
} catch (cause) {
|
| 291 |
return Effect.fail(
|
| 292 |
-
new
|
| 293 |
message: Predicate.hasProperty(cause, "message") ? String(cause.message) : String(cause)
|
| 294 |
})
|
| 295 |
)
|
|
@@ -298,8 +393,15 @@ export const transformation: Transformation.Transformation<
|
|
| 298 |
})
|
| 299 |
|
| 300 |
/**
|
| 301 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
* @category schemas
|
|
|
|
| 303 |
*/
|
| 304 |
export const schema = <S extends Schema.Top>(schema: S): schema<S> =>
|
| 305 |
(Schema.Uint8Array as Schema.instanceOf<Uint8Array<ArrayBuffer>>).pipe(
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Encodes and decodes MessagePack frames in Effect channels.
|
| 3 |
+
*
|
| 4 |
+
* MessagePack is a compact binary serialization format for protocols and
|
| 5 |
+
* storage layers that expect bytes instead of JSON text, such as RPC
|
| 6 |
+
* transports, socket streams, caches, or database columns. This module includes
|
| 7 |
+
* raw channel helpers for values whose shape is already agreed on, and
|
| 8 |
+
* schema-based helpers for validating and transforming values at the boundary.
|
| 9 |
+
*
|
| 10 |
* @since 4.0.0
|
| 11 |
*/
|
| 12 |
import { Packr, Unpackr } from "msgpackr"
|
|
|
|
| 21 |
import * as Predicate from "../../Predicate.ts"
|
| 22 |
import type * as Pull from "../../Pull.ts"
|
| 23 |
import * as Schema from "../../Schema.ts"
|
| 24 |
+
import * as SchemaIssue from "../../SchemaIssue.ts"
|
| 25 |
+
import * as SchemaTransformation from "../../SchemaTransformation.ts"
|
| 26 |
|
| 27 |
const MsgPackErrorTypeId = "~effect/encoding/MsgPack/MsgPackError"
|
| 28 |
|
| 29 |
/**
|
| 30 |
+
* Error raised when MessagePack encoding or decoding fails.
|
| 31 |
+
*
|
| 32 |
+
* **Details**
|
| 33 |
+
*
|
| 34 |
+
* The `kind` field identifies whether the failure happened while packing or
|
| 35 |
+
* unpacking, and `cause` preserves the original error.
|
| 36 |
+
*
|
| 37 |
* @category errors
|
| 38 |
+
* @since 4.0.0
|
| 39 |
*/
|
| 40 |
export class MsgPackError extends Data.TaggedError("MsgPackError")<{
|
| 41 |
readonly kind: "Pack" | "Unpack"
|
| 42 |
readonly cause: unknown
|
| 43 |
}> {
|
| 44 |
/**
|
| 45 |
+
* Marks this value as a MessagePack encoding or decoding error for runtime guards.
|
| 46 |
+
*
|
| 47 |
* @since 4.0.0
|
| 48 |
*/
|
| 49 |
readonly [MsgPackErrorTypeId] = MsgPackErrorTypeId
|
| 50 |
|
| 51 |
/**
|
| 52 |
+
* Uses the failed MessagePack operation as the public message.
|
| 53 |
+
*
|
| 54 |
* @since 4.0.0
|
| 55 |
*/
|
| 56 |
override get message() {
|
|
|
|
| 59 |
}
|
| 60 |
|
| 61 |
/**
|
| 62 |
+
* Creates a channel that encodes non-empty chunks of values as MessagePack byte
|
| 63 |
+
* arrays.
|
| 64 |
+
*
|
| 65 |
+
* **Details**
|
| 66 |
+
*
|
| 67 |
+
* The channel fails with `MsgPackError` when any value cannot be packed.
|
| 68 |
+
*
|
| 69 |
* @category constructors
|
| 70 |
+
* @since 4.0.0
|
| 71 |
*/
|
| 72 |
export const encode = <IE = never, Done = unknown>(): Channel.Channel<
|
| 73 |
Arr.NonEmptyReadonlyArray<Uint8Array<ArrayBuffer>>,
|
|
|
|
| 91 |
)
|
| 92 |
|
| 93 |
/**
|
| 94 |
+
* Creates a MessagePack encoder channel for values of a schema.
|
| 95 |
+
*
|
| 96 |
+
* **Details**
|
| 97 |
+
*
|
| 98 |
+
* Values are first encoded with the schema and then packed as MessagePack bytes,
|
| 99 |
+
* so the channel can fail with either schema errors or `MsgPackError`.
|
| 100 |
+
*
|
| 101 |
* @category constructors
|
| 102 |
+
* @since 4.0.0
|
| 103 |
*/
|
| 104 |
export const encodeSchema = <S extends Schema.Top>(
|
| 105 |
schema: S
|
|
|
|
| 115 |
> => Channel.pipeTo(ChannelSchema.encode(schema)(), encode())
|
| 116 |
|
| 117 |
/**
|
| 118 |
+
* Creates a channel that decodes MessagePack byte chunks into values.
|
| 119 |
+
*
|
| 120 |
+
* **Details**
|
| 121 |
+
*
|
| 122 |
+
* Incomplete frames are buffered across chunks, and invalid MessagePack data
|
| 123 |
+
* fails with `MsgPackError`.
|
| 124 |
+
*
|
| 125 |
* @category constructors
|
| 126 |
+
* @since 4.0.0
|
| 127 |
*/
|
| 128 |
export const decode = <IE = never, Done = unknown>(): Channel.Channel<
|
| 129 |
Arr.NonEmptyReadonlyArray<unknown>,
|
|
|
|
| 171 |
)
|
| 172 |
|
| 173 |
/**
|
| 174 |
+
* Creates a MessagePack decoder channel for values of a schema.
|
| 175 |
+
*
|
| 176 |
+
* **Details**
|
| 177 |
+
*
|
| 178 |
+
* The channel unpacks bytes into unknown values and then decodes each value with
|
| 179 |
+
* the schema.
|
| 180 |
+
*
|
| 181 |
* @category constructors
|
| 182 |
+
* @since 4.0.0
|
| 183 |
*/
|
| 184 |
export const decodeSchema = <S extends Schema.Top>(
|
| 185 |
schema: S
|
|
|
|
| 195 |
> => Channel.pipeTo(decode<IE, Done>(), ChannelSchema.decodeUnknown(schema)())
|
| 196 |
|
| 197 |
/**
|
| 198 |
+
* Wraps a bidirectional byte channel with MessagePack encoding and decoding.
|
| 199 |
+
*
|
| 200 |
+
* **Details**
|
| 201 |
+
*
|
| 202 |
+
* Outgoing values are packed as MessagePack bytes before reaching the wrapped
|
| 203 |
+
* channel, and incoming bytes are unpacked into values.
|
| 204 |
+
*
|
| 205 |
* @category combinators
|
| 206 |
+
* @since 4.0.0
|
| 207 |
*/
|
| 208 |
export const duplex = <R, IE, OE, OutDone, InDone>(
|
| 209 |
self: Channel.Channel<
|
|
|
|
| 230 |
)
|
| 231 |
|
| 232 |
/**
|
| 233 |
+
* Wraps a bidirectional byte channel with schema-aware MessagePack encoding and
|
| 234 |
+
* decoding.
|
| 235 |
+
*
|
| 236 |
+
* **Details**
|
| 237 |
+
*
|
| 238 |
+
* Values sent to the wrapped channel are encoded with `inputSchema` and packed
|
| 239 |
+
* as MessagePack bytes; bytes received from it are unpacked and decoded with
|
| 240 |
+
* `outputSchema`.
|
| 241 |
+
*
|
| 242 |
* @category combinators
|
| 243 |
+
* @since 4.0.0
|
| 244 |
*/
|
| 245 |
export const duplexSchema: {
|
| 246 |
/**
|
| 247 |
+
* Wraps a bidirectional byte channel with schema-aware MessagePack encoding and
|
| 248 |
+
* decoding.
|
| 249 |
+
*
|
| 250 |
+
* **Details**
|
| 251 |
+
*
|
| 252 |
+
* Values sent to the wrapped channel are encoded with `inputSchema` and packed
|
| 253 |
+
* as MessagePack bytes; bytes received from it are unpacked and decoded with
|
| 254 |
+
* `outputSchema`.
|
| 255 |
+
*
|
| 256 |
* @category combinators
|
| 257 |
+
* @since 4.0.0
|
| 258 |
*/
|
| 259 |
<In extends Schema.Top, Out extends Schema.Top>(
|
| 260 |
options: {
|
|
|
|
| 281 |
R | In["EncodingServices"] | Out["DecodingServices"]
|
| 282 |
>
|
| 283 |
/**
|
| 284 |
+
* Wraps a bidirectional byte channel with schema-aware MessagePack encoding and
|
| 285 |
+
* decoding.
|
| 286 |
+
*
|
| 287 |
+
* **Details**
|
| 288 |
+
*
|
| 289 |
+
* Values sent to the wrapped channel are encoded with `inputSchema` and packed
|
| 290 |
+
* as MessagePack bytes; bytes received from it are unpacked and decoded with
|
| 291 |
+
* `outputSchema`.
|
| 292 |
+
*
|
| 293 |
* @category combinators
|
| 294 |
+
* @since 4.0.0
|
| 295 |
*/
|
| 296 |
<Out extends Schema.Top, In extends Schema.Top, OutErr, OutDone, InErr, InDone, R>(
|
| 297 |
self: Channel.Channel<
|
|
|
|
| 341 |
> => ChannelSchema.duplexUnknown(duplex(self), options))
|
| 342 |
|
| 343 |
/**
|
| 344 |
+
* Schema type for values encoded as MessagePack bytes.
|
| 345 |
+
*
|
| 346 |
+
* **Details**
|
| 347 |
+
*
|
| 348 |
+
* It decodes a `Uint8Array` MessagePack payload to the target schema type and
|
| 349 |
+
* encodes the target type back to bytes.
|
| 350 |
+
*
|
| 351 |
* @category schemas
|
| 352 |
+
* @since 4.0.0
|
| 353 |
*/
|
| 354 |
export interface schema<S extends Schema.Top> extends Schema.decodeTo<S, Schema.instanceOf<Uint8Array<ArrayBuffer>>> {}
|
| 355 |
|
| 356 |
/**
|
| 357 |
+
* Schema for decoding MessagePack bytes into values and encoding values back to
|
| 358 |
+
* MessagePack bytes.
|
| 359 |
+
*
|
| 360 |
+
* **Details**
|
| 361 |
+
*
|
| 362 |
+
* MessagePack codec failures are converted to `InvalidValue` schema issues.
|
| 363 |
+
*
|
| 364 |
* @category schemas
|
| 365 |
+
* @since 4.0.0
|
| 366 |
*/
|
| 367 |
+
export const transformation: SchemaTransformation.Transformation<
|
| 368 |
unknown,
|
| 369 |
Uint8Array<ArrayBuffer>
|
| 370 |
+
> = SchemaTransformation.transformOrFail({
|
| 371 |
decode(e, _options) {
|
| 372 |
try {
|
| 373 |
return Effect.succeed(Msgpackr.decode(e))
|
| 374 |
} catch (cause) {
|
| 375 |
return Effect.fail(
|
| 376 |
+
new SchemaIssue.InvalidValue(Option.some(e), {
|
| 377 |
message: Predicate.hasProperty(cause, "message") ? String(cause.message) : String(cause)
|
| 378 |
})
|
| 379 |
)
|
|
|
|
| 384 |
return Effect.succeed(Msgpackr.encode(t) as Uint8Array<ArrayBuffer>)
|
| 385 |
} catch (cause) {
|
| 386 |
return Effect.fail(
|
| 387 |
+
new SchemaIssue.InvalidValue(Option.some(t), {
|
| 388 |
message: Predicate.hasProperty(cause, "message") ? String(cause.message) : String(cause)
|
| 389 |
})
|
| 390 |
)
|
|
|
|
| 393 |
})
|
| 394 |
|
| 395 |
/**
|
| 396 |
+
* Builds a schema that stores values as MessagePack bytes.
|
| 397 |
+
*
|
| 398 |
+
* **Details**
|
| 399 |
+
*
|
| 400 |
+
* The resulting schema decodes `Uint8Array` payloads with MessagePack and the
|
| 401 |
+
* provided schema, and encodes values back to MessagePack bytes.
|
| 402 |
+
*
|
| 403 |
* @category schemas
|
| 404 |
+
* @since 4.0.0
|
| 405 |
*/
|
| 406 |
export const schema = <S extends Schema.Top>(schema: S): schema<S> =>
|
| 407 |
(Schema.Uint8Array as Schema.instanceOf<Uint8Array<ArrayBuffer>>).pipe(
|
.opencode/node_modules/effect/src/unstable/encoding/Ndjson.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Arr from "../../Array.ts"
|
|
@@ -14,19 +20,30 @@ const NdjsonErrorTypeId = "~effect/encoding/Ndjson/NdjsonError"
|
|
| 14 |
const encoder = new TextEncoder()
|
| 15 |
|
| 16 |
/**
|
| 17 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
* @category errors
|
|
|
|
| 19 |
*/
|
| 20 |
export class NdjsonError extends Data.TaggedError("NdjsonError")<{
|
| 21 |
readonly kind: "Pack" | "Unpack"
|
| 22 |
readonly cause: unknown
|
| 23 |
}> {
|
| 24 |
/**
|
|
|
|
|
|
|
| 25 |
* @since 4.0.0
|
| 26 |
*/
|
| 27 |
readonly [NdjsonErrorTypeId] = NdjsonErrorTypeId
|
| 28 |
|
| 29 |
/**
|
|
|
|
|
|
|
| 30 |
* @since 4.0.0
|
| 31 |
*/
|
| 32 |
override get message() {
|
|
@@ -35,8 +52,15 @@ export class NdjsonError extends Data.TaggedError("NdjsonError")<{
|
|
| 35 |
}
|
| 36 |
|
| 37 |
/**
|
| 38 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
* @category constructors
|
|
|
|
| 40 |
*/
|
| 41 |
export const encodeString = <IE = never, Done = unknown>(): Channel.Channel<
|
| 42 |
Arr.NonEmptyReadonlyArray<string>,
|
|
@@ -57,8 +81,10 @@ export const encodeString = <IE = never, Done = unknown>(): Channel.Channel<
|
|
| 57 |
)
|
| 58 |
|
| 59 |
/**
|
| 60 |
-
*
|
|
|
|
| 61 |
* @category constructors
|
|
|
|
| 62 |
*/
|
| 63 |
export const encode = <IE = never, Done = unknown>(): Channel.Channel<
|
| 64 |
Arr.NonEmptyReadonlyArray<Uint8Array>,
|
|
@@ -70,8 +96,15 @@ export const encode = <IE = never, Done = unknown>(): Channel.Channel<
|
|
| 70 |
> => Channel.map(encodeString(), Arr.map((_) => encoder.encode(_)))
|
| 71 |
|
| 72 |
/**
|
| 73 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
* @category constructors
|
|
|
|
| 75 |
*/
|
| 76 |
export const encodeSchema = <S extends Schema.Top>(
|
| 77 |
schema: S
|
|
@@ -87,8 +120,15 @@ export const encodeSchema = <S extends Schema.Top>(
|
|
| 87 |
> => Channel.pipeTo(ChannelSchema.encode(schema)(), encode())
|
| 88 |
|
| 89 |
/**
|
| 90 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
* @category constructors
|
|
|
|
| 92 |
*/
|
| 93 |
export const encodeSchemaString = <S extends Schema.Top>(
|
| 94 |
schema: S
|
|
@@ -104,8 +144,24 @@ export const encodeSchemaString = <S extends Schema.Top>(
|
|
| 104 |
> => Channel.pipeTo(ChannelSchema.encode(schema)(), encodeString())
|
| 105 |
|
| 106 |
/**
|
| 107 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
* @category constructors
|
|
|
|
| 109 |
*/
|
| 110 |
export const decodeString = <IE = never, Done = unknown>(options?: {
|
| 111 |
readonly ignoreEmptyLines?: boolean | undefined
|
|
@@ -132,8 +188,15 @@ export const decodeString = <IE = never, Done = unknown>(options?: {
|
|
| 132 |
}
|
| 133 |
|
| 134 |
/**
|
| 135 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
* @category constructors
|
|
|
|
| 137 |
*/
|
| 138 |
export const decode = <IE = never, Done = unknown>(options?: {
|
| 139 |
readonly ignoreEmptyLines?: boolean | undefined
|
|
@@ -149,8 +212,15 @@ export const decode = <IE = never, Done = unknown>(options?: {
|
|
| 149 |
}
|
| 150 |
|
| 151 |
/**
|
| 152 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
* @category constructors
|
|
|
|
| 154 |
*/
|
| 155 |
export const decodeSchema = <S extends Schema.Top>(
|
| 156 |
schema: S
|
|
@@ -168,8 +238,15 @@ export const decodeSchema = <S extends Schema.Top>(
|
|
| 168 |
> => Channel.pipeTo(decode(options), ChannelSchema.decodeUnknown(schema)())
|
| 169 |
|
| 170 |
/**
|
| 171 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
* @category constructors
|
|
|
|
| 173 |
*/
|
| 174 |
export const decodeSchemaString = <S extends Schema.Top>(
|
| 175 |
schema: S
|
|
@@ -187,13 +264,27 @@ export const decodeSchemaString = <S extends Schema.Top>(
|
|
| 187 |
> => Channel.pipeTo(decodeString(options), ChannelSchema.decodeUnknown(schema)())
|
| 188 |
|
| 189 |
/**
|
| 190 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
* @category combinators
|
|
|
|
| 192 |
*/
|
| 193 |
export const duplex: {
|
| 194 |
/**
|
| 195 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
* @category combinators
|
|
|
|
| 197 |
*/
|
| 198 |
(
|
| 199 |
options?: {
|
|
@@ -219,8 +310,15 @@ export const duplex: {
|
|
| 219 |
R
|
| 220 |
>
|
| 221 |
/**
|
| 222 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
* @category combinators
|
|
|
|
| 224 |
*/
|
| 225 |
<R, IE, OE, OutDone, InDone>(
|
| 226 |
self: Channel.Channel<
|
|
@@ -272,13 +370,27 @@ export const duplex: {
|
|
| 272 |
))
|
| 273 |
|
| 274 |
/**
|
| 275 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
* @category combinators
|
|
|
|
| 277 |
*/
|
| 278 |
export const duplexString: {
|
| 279 |
/**
|
| 280 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
* @category combinators
|
|
|
|
| 282 |
*/
|
| 283 |
(
|
| 284 |
options?: {
|
|
@@ -304,8 +416,15 @@ export const duplexString: {
|
|
| 304 |
R
|
| 305 |
>
|
| 306 |
/**
|
| 307 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
* @category combinators
|
|
|
|
| 309 |
*/
|
| 310 |
<R, IE, OE, OutDone, InDone>(
|
| 311 |
self: Channel.Channel<
|
|
@@ -357,13 +476,29 @@ export const duplexString: {
|
|
| 357 |
))
|
| 358 |
|
| 359 |
/**
|
| 360 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 361 |
* @category combinators
|
|
|
|
| 362 |
*/
|
| 363 |
export const duplexSchema: {
|
| 364 |
/**
|
| 365 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 366 |
* @category combinators
|
|
|
|
| 367 |
*/
|
| 368 |
<In extends Schema.Top, Out extends Schema.Top>(
|
| 369 |
options: {
|
|
@@ -391,8 +526,16 @@ export const duplexSchema: {
|
|
| 391 |
R | In["EncodingServices"] | Out["DecodingServices"]
|
| 392 |
>
|
| 393 |
/**
|
| 394 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 395 |
* @category combinators
|
|
|
|
| 396 |
*/
|
| 397 |
<Out extends Schema.Top, In extends Schema.Top, OutErr, OutDone, InErr, InDone, R>(
|
| 398 |
self: Channel.Channel<
|
|
@@ -444,13 +587,29 @@ export const duplexSchema: {
|
|
| 444 |
> => ChannelSchema.duplexUnknown(duplex(self, options), options))
|
| 445 |
|
| 446 |
/**
|
| 447 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 448 |
* @category combinators
|
|
|
|
| 449 |
*/
|
| 450 |
export const duplexSchemaString: {
|
| 451 |
/**
|
| 452 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 453 |
* @category combinators
|
|
|
|
| 454 |
*/
|
| 455 |
<In extends Schema.Top, Out extends Schema.Top>(
|
| 456 |
options: {
|
|
@@ -478,8 +637,16 @@ export const duplexSchemaString: {
|
|
| 478 |
R | In["EncodingServices"] | Out["DecodingServices"]
|
| 479 |
>
|
| 480 |
/**
|
| 481 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 482 |
* @category combinators
|
|
|
|
| 483 |
*/
|
| 484 |
<Out extends Schema.Top, In extends Schema.Top, OutErr, OutDone, InErr, InDone, R>(
|
| 485 |
self: Channel.Channel<
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Encodes and decodes newline-delimited JSON streams in Effect channels.
|
| 3 |
+
*
|
| 4 |
+
* NDJSON stores one complete JSON value on each line. This module has helpers
|
| 5 |
+
* for byte streams, string streams, and schema-checked records, so streaming
|
| 6 |
+
* code can read or write one JSON record at a time.
|
| 7 |
+
*
|
| 8 |
* @since 4.0.0
|
| 9 |
*/
|
| 10 |
import * as Arr from "../../Array.ts"
|
|
|
|
| 20 |
const encoder = new TextEncoder()
|
| 21 |
|
| 22 |
/**
|
| 23 |
+
* Error raised when NDJSON encoding or decoding fails.
|
| 24 |
+
*
|
| 25 |
+
* **Details**
|
| 26 |
+
*
|
| 27 |
+
* The `kind` field identifies whether the failure happened while packing or
|
| 28 |
+
* unpacking, and `cause` preserves the original error.
|
| 29 |
+
*
|
| 30 |
* @category errors
|
| 31 |
+
* @since 4.0.0
|
| 32 |
*/
|
| 33 |
export class NdjsonError extends Data.TaggedError("NdjsonError")<{
|
| 34 |
readonly kind: "Pack" | "Unpack"
|
| 35 |
readonly cause: unknown
|
| 36 |
}> {
|
| 37 |
/**
|
| 38 |
+
* Marks this value as an NDJSON encoding or decoding error for runtime guards.
|
| 39 |
+
*
|
| 40 |
* @since 4.0.0
|
| 41 |
*/
|
| 42 |
readonly [NdjsonErrorTypeId] = NdjsonErrorTypeId
|
| 43 |
|
| 44 |
/**
|
| 45 |
+
* Uses the failed NDJSON operation as the public message.
|
| 46 |
+
*
|
| 47 |
* @since 4.0.0
|
| 48 |
*/
|
| 49 |
override get message() {
|
|
|
|
| 52 |
}
|
| 53 |
|
| 54 |
/**
|
| 55 |
+
* Creates a channel that encodes chunks of values as NDJSON strings.
|
| 56 |
+
*
|
| 57 |
+
* **Details**
|
| 58 |
+
*
|
| 59 |
+
* Each input item is `JSON.stringify`-encoded, separated by newlines, and the
|
| 60 |
+
* output chunk ends with a trailing newline.
|
| 61 |
+
*
|
| 62 |
* @category constructors
|
| 63 |
+
* @since 4.0.0
|
| 64 |
*/
|
| 65 |
export const encodeString = <IE = never, Done = unknown>(): Channel.Channel<
|
| 66 |
Arr.NonEmptyReadonlyArray<string>,
|
|
|
|
| 81 |
)
|
| 82 |
|
| 83 |
/**
|
| 84 |
+
* Creates a channel that encodes chunks of values as UTF-8 NDJSON bytes.
|
| 85 |
+
*
|
| 86 |
* @category constructors
|
| 87 |
+
* @since 4.0.0
|
| 88 |
*/
|
| 89 |
export const encode = <IE = never, Done = unknown>(): Channel.Channel<
|
| 90 |
Arr.NonEmptyReadonlyArray<Uint8Array>,
|
|
|
|
| 96 |
> => Channel.map(encodeString(), Arr.map((_) => encoder.encode(_)))
|
| 97 |
|
| 98 |
/**
|
| 99 |
+
* Creates an NDJSON byte encoder channel for values of a schema.
|
| 100 |
+
*
|
| 101 |
+
* **Details**
|
| 102 |
+
*
|
| 103 |
+
* Values are first encoded with the schema and then written as UTF-8
|
| 104 |
+
* newline-delimited JSON.
|
| 105 |
+
*
|
| 106 |
* @category constructors
|
| 107 |
+
* @since 4.0.0
|
| 108 |
*/
|
| 109 |
export const encodeSchema = <S extends Schema.Top>(
|
| 110 |
schema: S
|
|
|
|
| 120 |
> => Channel.pipeTo(ChannelSchema.encode(schema)(), encode())
|
| 121 |
|
| 122 |
/**
|
| 123 |
+
* Creates an NDJSON string encoder channel for values of a schema.
|
| 124 |
+
*
|
| 125 |
+
* **Details**
|
| 126 |
+
*
|
| 127 |
+
* Values are first encoded with the schema and then written as newline-delimited
|
| 128 |
+
* JSON strings.
|
| 129 |
+
*
|
| 130 |
* @category constructors
|
| 131 |
+
* @since 4.0.0
|
| 132 |
*/
|
| 133 |
export const encodeSchemaString = <S extends Schema.Top>(
|
| 134 |
schema: S
|
|
|
|
| 144 |
> => Channel.pipeTo(ChannelSchema.encode(schema)(), encodeString())
|
| 145 |
|
| 146 |
/**
|
| 147 |
+
* Creates a channel that parses NDJSON string chunks into values.
|
| 148 |
+
*
|
| 149 |
+
* **When to use**
|
| 150 |
+
*
|
| 151 |
+
* Use when NDJSON input arrives as string chunks and each complete line should
|
| 152 |
+
* be parsed into a JSON value.
|
| 153 |
+
*
|
| 154 |
+
* **Details**
|
| 155 |
+
*
|
| 156 |
+
* Lines may span input chunks.
|
| 157 |
+
*
|
| 158 |
+
* **Gotchas**
|
| 159 |
+
*
|
| 160 |
+
* Set `ignoreEmptyLines` to skip blank lines before calling `JSON.parse`;
|
| 161 |
+
* otherwise blank lines are parsed and fail as invalid JSON.
|
| 162 |
+
*
|
| 163 |
* @category constructors
|
| 164 |
+
* @since 4.0.0
|
| 165 |
*/
|
| 166 |
export const decodeString = <IE = never, Done = unknown>(options?: {
|
| 167 |
readonly ignoreEmptyLines?: boolean | undefined
|
|
|
|
| 188 |
}
|
| 189 |
|
| 190 |
/**
|
| 191 |
+
* Creates a channel that decodes UTF-8 byte chunks and parses them as NDJSON.
|
| 192 |
+
*
|
| 193 |
+
* **Details**
|
| 194 |
+
*
|
| 195 |
+
* Lines may span input chunks, and `ignoreEmptyLines` controls whether blank
|
| 196 |
+
* lines are skipped before JSON parsing.
|
| 197 |
+
*
|
| 198 |
* @category constructors
|
| 199 |
+
* @since 4.0.0
|
| 200 |
*/
|
| 201 |
export const decode = <IE = never, Done = unknown>(options?: {
|
| 202 |
readonly ignoreEmptyLines?: boolean | undefined
|
|
|
|
| 212 |
}
|
| 213 |
|
| 214 |
/**
|
| 215 |
+
* Creates an NDJSON byte decoder channel for values of a schema.
|
| 216 |
+
*
|
| 217 |
+
* **Details**
|
| 218 |
+
*
|
| 219 |
+
* The channel decodes UTF-8 bytes, parses each NDJSON line, and then decodes
|
| 220 |
+
* each parsed value with the schema.
|
| 221 |
+
*
|
| 222 |
* @category constructors
|
| 223 |
+
* @since 4.0.0
|
| 224 |
*/
|
| 225 |
export const decodeSchema = <S extends Schema.Top>(
|
| 226 |
schema: S
|
|
|
|
| 238 |
> => Channel.pipeTo(decode(options), ChannelSchema.decodeUnknown(schema)())
|
| 239 |
|
| 240 |
/**
|
| 241 |
+
* Creates an NDJSON string decoder channel for values of a schema.
|
| 242 |
+
*
|
| 243 |
+
* **Details**
|
| 244 |
+
*
|
| 245 |
+
* The channel parses each line as JSON and then decodes each parsed value with
|
| 246 |
+
* the schema.
|
| 247 |
+
*
|
| 248 |
* @category constructors
|
| 249 |
+
* @since 4.0.0
|
| 250 |
*/
|
| 251 |
export const decodeSchemaString = <S extends Schema.Top>(
|
| 252 |
schema: S
|
|
|
|
| 264 |
> => Channel.pipeTo(decodeString(options), ChannelSchema.decodeUnknown(schema)())
|
| 265 |
|
| 266 |
/**
|
| 267 |
+
* Wraps a bidirectional byte channel with NDJSON encoding and decoding.
|
| 268 |
+
*
|
| 269 |
+
* **Details**
|
| 270 |
+
*
|
| 271 |
+
* Outgoing values are written as UTF-8 NDJSON bytes, and incoming bytes are
|
| 272 |
+
* parsed as NDJSON values.
|
| 273 |
+
*
|
| 274 |
* @category combinators
|
| 275 |
+
* @since 4.0.0
|
| 276 |
*/
|
| 277 |
export const duplex: {
|
| 278 |
/**
|
| 279 |
+
* Wraps a bidirectional byte channel with NDJSON encoding and decoding.
|
| 280 |
+
*
|
| 281 |
+
* **Details**
|
| 282 |
+
*
|
| 283 |
+
* Outgoing values are written as UTF-8 NDJSON bytes, and incoming bytes are
|
| 284 |
+
* parsed as NDJSON values.
|
| 285 |
+
*
|
| 286 |
* @category combinators
|
| 287 |
+
* @since 4.0.0
|
| 288 |
*/
|
| 289 |
(
|
| 290 |
options?: {
|
|
|
|
| 310 |
R
|
| 311 |
>
|
| 312 |
/**
|
| 313 |
+
* Wraps a bidirectional byte channel with NDJSON encoding and decoding.
|
| 314 |
+
*
|
| 315 |
+
* **Details**
|
| 316 |
+
*
|
| 317 |
+
* Outgoing values are written as UTF-8 NDJSON bytes, and incoming bytes are
|
| 318 |
+
* parsed as NDJSON values.
|
| 319 |
+
*
|
| 320 |
* @category combinators
|
| 321 |
+
* @since 4.0.0
|
| 322 |
*/
|
| 323 |
<R, IE, OE, OutDone, InDone>(
|
| 324 |
self: Channel.Channel<
|
|
|
|
| 370 |
))
|
| 371 |
|
| 372 |
/**
|
| 373 |
+
* Wraps a bidirectional string channel with NDJSON encoding and decoding.
|
| 374 |
+
*
|
| 375 |
+
* **Details**
|
| 376 |
+
*
|
| 377 |
+
* Outgoing values are written as NDJSON strings, and incoming strings are parsed
|
| 378 |
+
* as NDJSON values.
|
| 379 |
+
*
|
| 380 |
* @category combinators
|
| 381 |
+
* @since 4.0.0
|
| 382 |
*/
|
| 383 |
export const duplexString: {
|
| 384 |
/**
|
| 385 |
+
* Wraps a bidirectional string channel with NDJSON encoding and decoding.
|
| 386 |
+
*
|
| 387 |
+
* **Details**
|
| 388 |
+
*
|
| 389 |
+
* Outgoing values are written as NDJSON strings, and incoming strings are parsed
|
| 390 |
+
* as NDJSON values.
|
| 391 |
+
*
|
| 392 |
* @category combinators
|
| 393 |
+
* @since 4.0.0
|
| 394 |
*/
|
| 395 |
(
|
| 396 |
options?: {
|
|
|
|
| 416 |
R
|
| 417 |
>
|
| 418 |
/**
|
| 419 |
+
* Wraps a bidirectional string channel with NDJSON encoding and decoding.
|
| 420 |
+
*
|
| 421 |
+
* **Details**
|
| 422 |
+
*
|
| 423 |
+
* Outgoing values are written as NDJSON strings, and incoming strings are parsed
|
| 424 |
+
* as NDJSON values.
|
| 425 |
+
*
|
| 426 |
* @category combinators
|
| 427 |
+
* @since 4.0.0
|
| 428 |
*/
|
| 429 |
<R, IE, OE, OutDone, InDone>(
|
| 430 |
self: Channel.Channel<
|
|
|
|
| 476 |
))
|
| 477 |
|
| 478 |
/**
|
| 479 |
+
* Wraps a bidirectional byte channel with schema-aware NDJSON encoding and
|
| 480 |
+
* decoding.
|
| 481 |
+
*
|
| 482 |
+
* **Details**
|
| 483 |
+
*
|
| 484 |
+
* Values sent to the wrapped channel are encoded with `inputSchema`; bytes
|
| 485 |
+
* received from it are parsed as NDJSON and decoded with `outputSchema`.
|
| 486 |
+
*
|
| 487 |
* @category combinators
|
| 488 |
+
* @since 4.0.0
|
| 489 |
*/
|
| 490 |
export const duplexSchema: {
|
| 491 |
/**
|
| 492 |
+
* Wraps a bidirectional byte channel with schema-aware NDJSON encoding and
|
| 493 |
+
* decoding.
|
| 494 |
+
*
|
| 495 |
+
* **Details**
|
| 496 |
+
*
|
| 497 |
+
* Values sent to the wrapped channel are encoded with `inputSchema`; bytes
|
| 498 |
+
* received from it are parsed as NDJSON and decoded with `outputSchema`.
|
| 499 |
+
*
|
| 500 |
* @category combinators
|
| 501 |
+
* @since 4.0.0
|
| 502 |
*/
|
| 503 |
<In extends Schema.Top, Out extends Schema.Top>(
|
| 504 |
options: {
|
|
|
|
| 526 |
R | In["EncodingServices"] | Out["DecodingServices"]
|
| 527 |
>
|
| 528 |
/**
|
| 529 |
+
* Wraps a bidirectional byte channel with schema-aware NDJSON encoding and
|
| 530 |
+
* decoding.
|
| 531 |
+
*
|
| 532 |
+
* **Details**
|
| 533 |
+
*
|
| 534 |
+
* Values sent to the wrapped channel are encoded with `inputSchema`; bytes
|
| 535 |
+
* received from it are parsed as NDJSON and decoded with `outputSchema`.
|
| 536 |
+
*
|
| 537 |
* @category combinators
|
| 538 |
+
* @since 4.0.0
|
| 539 |
*/
|
| 540 |
<Out extends Schema.Top, In extends Schema.Top, OutErr, OutDone, InErr, InDone, R>(
|
| 541 |
self: Channel.Channel<
|
|
|
|
| 587 |
> => ChannelSchema.duplexUnknown(duplex(self, options), options))
|
| 588 |
|
| 589 |
/**
|
| 590 |
+
* Wraps a bidirectional string channel with schema-aware NDJSON encoding and
|
| 591 |
+
* decoding.
|
| 592 |
+
*
|
| 593 |
+
* **Details**
|
| 594 |
+
*
|
| 595 |
+
* Values sent to the wrapped channel are encoded with `inputSchema`; strings
|
| 596 |
+
* received from it are parsed as NDJSON and decoded with `outputSchema`.
|
| 597 |
+
*
|
| 598 |
* @category combinators
|
| 599 |
+
* @since 4.0.0
|
| 600 |
*/
|
| 601 |
export const duplexSchemaString: {
|
| 602 |
/**
|
| 603 |
+
* Wraps a bidirectional string channel with schema-aware NDJSON encoding and
|
| 604 |
+
* decoding.
|
| 605 |
+
*
|
| 606 |
+
* **Details**
|
| 607 |
+
*
|
| 608 |
+
* Values sent to the wrapped channel are encoded with `inputSchema`; strings
|
| 609 |
+
* received from it are parsed as NDJSON and decoded with `outputSchema`.
|
| 610 |
+
*
|
| 611 |
* @category combinators
|
| 612 |
+
* @since 4.0.0
|
| 613 |
*/
|
| 614 |
<In extends Schema.Top, Out extends Schema.Top>(
|
| 615 |
options: {
|
|
|
|
| 637 |
R | In["EncodingServices"] | Out["DecodingServices"]
|
| 638 |
>
|
| 639 |
/**
|
| 640 |
+
* Wraps a bidirectional string channel with schema-aware NDJSON encoding and
|
| 641 |
+
* decoding.
|
| 642 |
+
*
|
| 643 |
+
* **Details**
|
| 644 |
+
*
|
| 645 |
+
* Values sent to the wrapped channel are encoded with `inputSchema`; strings
|
| 646 |
+
* received from it are parsed as NDJSON and decoded with `outputSchema`.
|
| 647 |
+
*
|
| 648 |
* @category combinators
|
| 649 |
+
* @since 4.0.0
|
| 650 |
*/
|
| 651 |
<Out extends Schema.Top, In extends Schema.Top, OutErr, OutDone, InErr, InDone, R>(
|
| 652 |
self: Channel.Channel<
|
.opencode/node_modules/effect/src/unstable/encoding/Sse.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import type { NonEmptyReadonlyArray } from "../../Array.ts"
|
|
@@ -13,11 +20,18 @@ import { hasProperty } from "../../Predicate.ts"
|
|
| 13 |
import * as Pull from "../../Pull.ts"
|
| 14 |
import * as Result from "../../Result.ts"
|
| 15 |
import * as Schema from "../../Schema.ts"
|
| 16 |
-
import * as
|
| 17 |
|
| 18 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
* @since 4.0.0
|
| 20 |
-
* @category Decoding
|
| 21 |
*/
|
| 22 |
export const decode = <IE, Done>(): Channel.Channel<
|
| 23 |
NonEmptyReadonlyArray<Event>,
|
|
@@ -60,28 +74,49 @@ export const decode = <IE, Done>(): Channel.Channel<
|
|
| 60 |
)
|
| 61 |
|
| 62 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
* @since 4.0.0
|
| 64 |
-
* @category Decoding
|
| 65 |
*/
|
| 66 |
export const decodeSchema = <
|
| 67 |
-
|
| 68 |
-
readonly id?: string | undefined
|
| 69 |
-
readonly event: string
|
| 70 |
-
readonly data: string
|
| 71 |
-
},
|
| 72 |
-
DecodingServices,
|
| 73 |
IE,
|
| 74 |
Done
|
| 75 |
>(
|
| 76 |
-
schema:
|
| 77 |
): Channel.Channel<
|
| 78 |
-
NonEmptyReadonlyArray<Type>,
|
| 79 |
IE | Retry | Schema.SchemaError,
|
| 80 |
Done,
|
| 81 |
NonEmptyReadonlyArray<string>,
|
| 82 |
IE,
|
| 83 |
Done,
|
| 84 |
-
DecodingServices
|
| 85 |
> =>
|
| 86 |
Channel.pipeTo(
|
| 87 |
decode<IE, Done>(),
|
|
@@ -91,8 +126,15 @@ export const decodeSchema = <
|
|
| 91 |
)
|
| 92 |
|
| 93 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
* @since 4.0.0
|
| 95 |
-
* @category Decoding
|
| 96 |
*/
|
| 97 |
export const decodeDataSchema = <Type, DecodingServices, IE, Done>(
|
| 98 |
schema: Schema.Decoder<Type, DecodingServices>
|
|
@@ -115,17 +157,23 @@ export const decodeDataSchema = <Type, DecodingServices, IE, Done>(
|
|
| 115 |
})
|
| 116 |
return Channel.pipeTo(
|
| 117 |
decode<IE, Done>(),
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
| 119 |
)
|
| 120 |
}
|
| 121 |
|
| 122 |
/**
|
| 123 |
-
*
|
|
|
|
|
|
|
| 124 |
*
|
| 125 |
-
*
|
|
|
|
| 126 |
*
|
|
|
|
| 127 |
* @since 4.0.0
|
| 128 |
-
* @category Decoding
|
| 129 |
*/
|
| 130 |
export function makeParser(onParse: (event: AnyEvent) => void): Parser {
|
| 131 |
// Processing state
|
|
@@ -286,8 +334,14 @@ function hasBom(buffer: string) {
|
|
| 286 |
}
|
| 287 |
|
| 288 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
* @since 4.0.0
|
| 290 |
-
* @category Decoding
|
| 291 |
*/
|
| 292 |
export interface Parser {
|
| 293 |
feed(chunk: string): void
|
|
@@ -295,8 +349,15 @@ export interface Parser {
|
|
| 295 |
}
|
| 296 |
|
| 297 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
* @since 4.0.0
|
| 299 |
-
* @category Encoding
|
| 300 |
*/
|
| 301 |
export const encode = <IE, Done>(): Channel.Channel<
|
| 302 |
NonEmptyReadonlyArray<string>,
|
|
@@ -322,14 +383,18 @@ export const encode = <IE, Done>(): Channel.Channel<
|
|
| 322 |
)
|
| 323 |
|
| 324 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
* @since 4.0.0
|
| 326 |
-
* @category Encoding
|
| 327 |
*/
|
| 328 |
export const encodeSchema = <
|
| 329 |
-
S extends
|
| 330 |
-
{ readonly id?: string | undefined; readonly event: string; readonly data: string },
|
| 331 |
-
unknown
|
| 332 |
-
>,
|
| 333 |
IE,
|
| 334 |
Done
|
| 335 |
>(schema: S): Channel.Channel<
|
|
@@ -348,16 +413,21 @@ export const encodeSchema = <
|
|
| 348 |
)
|
| 349 |
|
| 350 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 351 |
* @since 4.0.0
|
| 352 |
-
* @category Encoding
|
| 353 |
*/
|
| 354 |
export interface Encoder {
|
| 355 |
write(event: AnyEvent): string
|
| 356 |
}
|
| 357 |
|
| 358 |
/**
|
|
|
|
|
|
|
|
|
|
| 359 |
* @since 4.0.0
|
| 360 |
-
* @category Models
|
| 361 |
*/
|
| 362 |
export interface Event {
|
| 363 |
readonly _tag: "Event"
|
|
@@ -367,22 +437,26 @@ export interface Event {
|
|
| 367 |
}
|
| 368 |
|
| 369 |
/**
|
|
|
|
|
|
|
|
|
|
| 370 |
* @since 4.0.0
|
| 371 |
-
* @category Models
|
| 372 |
*/
|
| 373 |
export const EventEncoded: Schema.Struct<{
|
| 374 |
-
readonly id: Schema.
|
| 375 |
readonly event: Schema.String
|
| 376 |
readonly data: Schema.String
|
| 377 |
}> = Schema.Struct({
|
| 378 |
-
id: Schema.
|
| 379 |
event: Schema.String,
|
| 380 |
data: Schema.String
|
| 381 |
})
|
| 382 |
|
| 383 |
/**
|
|
|
|
|
|
|
|
|
|
| 384 |
* @since 4.0.0
|
| 385 |
-
* @category Models
|
| 386 |
*/
|
| 387 |
export const Event: Schema.Struct<{
|
| 388 |
readonly _tag: Schema.tag<"Event">
|
|
@@ -397,12 +471,15 @@ export const Event: Schema.Struct<{
|
|
| 397 |
})
|
| 398 |
|
| 399 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 400 |
* @since 4.0.0
|
| 401 |
-
* @category Models
|
| 402 |
*/
|
| 403 |
-
export const transformEvent =
|
| 404 |
readonly id?: string | undefined
|
| 405 |
-
readonly event: string
|
| 406 |
readonly data: string
|
| 407 |
}, {
|
| 408 |
readonly _tag: "Event"
|
|
@@ -414,42 +491,57 @@ export const transformEvent = Transformation.transform<{
|
|
| 414 |
encode: (event) => ({
|
| 415 |
_tag: "Event",
|
| 416 |
id: event.id,
|
| 417 |
-
event: event.event,
|
| 418 |
data: event.data
|
| 419 |
})
|
| 420 |
})
|
| 421 |
|
| 422 |
/**
|
|
|
|
|
|
|
|
|
|
| 423 |
* @since 4.0.0
|
| 424 |
-
* @category Models
|
| 425 |
*/
|
| 426 |
export interface EventEncoded {
|
| 427 |
readonly event: string
|
| 428 |
-
readonly id: string | undefined
|
| 429 |
readonly data: string
|
| 430 |
}
|
| 431 |
|
| 432 |
const RetryTypeId = "~effect/encoding/Sse/Retry" as const
|
| 433 |
|
| 434 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 435 |
* @since 4.0.0
|
| 436 |
-
* @category Models
|
| 437 |
*/
|
| 438 |
export class Retry extends Data.TaggedClass("Retry")<{
|
| 439 |
readonly duration: Duration.Duration
|
| 440 |
readonly lastEventId: string | undefined
|
| 441 |
}> {
|
| 442 |
/**
|
|
|
|
|
|
|
| 443 |
* @since 4.0.0
|
| 444 |
*/
|
| 445 |
readonly [RetryTypeId]: typeof RetryTypeId = RetryTypeId
|
| 446 |
/**
|
|
|
|
|
|
|
| 447 |
* @since 4.0.0
|
| 448 |
*/
|
| 449 |
static is(u: unknown): u is Retry {
|
| 450 |
return hasProperty(u, RetryTypeId)
|
| 451 |
}
|
| 452 |
/**
|
|
|
|
|
|
|
| 453 |
* @since 4.0.0
|
| 454 |
*/
|
| 455 |
static filter<A>(u: A): Result.Result<Retry, Exclude<A, Retry>> {
|
|
@@ -458,14 +550,24 @@ export class Retry extends Data.TaggedClass("Retry")<{
|
|
| 458 |
}
|
| 459 |
|
| 460 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 461 |
* @since 4.0.0
|
| 462 |
-
* @category Models
|
| 463 |
*/
|
| 464 |
export type AnyEvent = Event | Retry
|
| 465 |
|
| 466 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 467 |
* @since 4.0.0
|
| 468 |
-
* @category Encoding
|
| 469 |
*/
|
| 470 |
export const encoder: Encoder = {
|
| 471 |
write(event: AnyEvent): string {
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Parses and renders Server-Sent Events text streams.
|
| 3 |
+
*
|
| 4 |
+
* Server-Sent Events, or SSE, are the text format used by `EventSource` for
|
| 5 |
+
* one-way server-to-client updates. This module includes parsers, encoders,
|
| 6 |
+
* channel helpers, and schema-based helpers for the `id`, `event`, and `data`
|
| 7 |
+
* fields of each event.
|
| 8 |
+
*
|
| 9 |
* @since 4.0.0
|
| 10 |
*/
|
| 11 |
import type { NonEmptyReadonlyArray } from "../../Array.ts"
|
|
|
|
| 20 |
import * as Pull from "../../Pull.ts"
|
| 21 |
import * as Result from "../../Result.ts"
|
| 22 |
import * as Schema from "../../Schema.ts"
|
| 23 |
+
import * as SchemaTransformation from "../../SchemaTransformation.ts"
|
| 24 |
|
| 25 |
/**
|
| 26 |
+
* Creates a channel that parses Server-Sent Events text chunks into `Event` values.
|
| 27 |
+
*
|
| 28 |
+
* **Details**
|
| 29 |
+
*
|
| 30 |
+
* SSE `retry` directives are emitted as `Retry` failures so callers can
|
| 31 |
+
* reconnect with the requested delay.
|
| 32 |
+
*
|
| 33 |
+
* @category decoding
|
| 34 |
* @since 4.0.0
|
|
|
|
| 35 |
*/
|
| 36 |
export const decode = <IE, Done>(): Channel.Channel<
|
| 37 |
NonEmptyReadonlyArray<Event>,
|
|
|
|
| 74 |
)
|
| 75 |
|
| 76 |
/**
|
| 77 |
+
* A constraint for schemas that can decode SSE events.
|
| 78 |
+
*
|
| 79 |
+
* @category decoding
|
| 80 |
+
* @since 4.0.0
|
| 81 |
+
*/
|
| 82 |
+
export interface EventCodec extends
|
| 83 |
+
Schema.Codec<
|
| 84 |
+
any,
|
| 85 |
+
{
|
| 86 |
+
readonly id?: string | undefined
|
| 87 |
+
readonly event?: string | undefined
|
| 88 |
+
readonly data: string
|
| 89 |
+
},
|
| 90 |
+
any,
|
| 91 |
+
any
|
| 92 |
+
>
|
| 93 |
+
{}
|
| 94 |
+
|
| 95 |
+
/**
|
| 96 |
+
* Creates an SSE decoder channel that decodes each parsed event with a schema.
|
| 97 |
+
*
|
| 98 |
+
* **Details**
|
| 99 |
+
*
|
| 100 |
+
* The schema receives the untagged event shape containing `id`, `event`, and
|
| 101 |
+
* string `data`.
|
| 102 |
+
*
|
| 103 |
+
* @category decoding
|
| 104 |
* @since 4.0.0
|
|
|
|
| 105 |
*/
|
| 106 |
export const decodeSchema = <
|
| 107 |
+
S extends EventCodec,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
IE,
|
| 109 |
Done
|
| 110 |
>(
|
| 111 |
+
schema: S
|
| 112 |
): Channel.Channel<
|
| 113 |
+
NonEmptyReadonlyArray<S["Type"]>,
|
| 114 |
IE | Retry | Schema.SchemaError,
|
| 115 |
Done,
|
| 116 |
NonEmptyReadonlyArray<string>,
|
| 117 |
IE,
|
| 118 |
Done,
|
| 119 |
+
S["DecodingServices"]
|
| 120 |
> =>
|
| 121 |
Channel.pipeTo(
|
| 122 |
decode<IE, Done>(),
|
|
|
|
| 126 |
)
|
| 127 |
|
| 128 |
/**
|
| 129 |
+
* Creates an SSE decoder channel that JSON-decodes each event `data` field with a schema.
|
| 130 |
+
*
|
| 131 |
+
* **Details**
|
| 132 |
+
*
|
| 133 |
+
* The output preserves the SSE `event` name and optional `id` while replacing
|
| 134 |
+
* `data` with the decoded value.
|
| 135 |
+
*
|
| 136 |
+
* @category decoding
|
| 137 |
* @since 4.0.0
|
|
|
|
| 138 |
*/
|
| 139 |
export const decodeDataSchema = <Type, DecodingServices, IE, Done>(
|
| 140 |
schema: Schema.Decoder<Type, DecodingServices>
|
|
|
|
| 157 |
})
|
| 158 |
return Channel.pipeTo(
|
| 159 |
decode<IE, Done>(),
|
| 160 |
+
Channel.map(
|
| 161 |
+
ChannelSchema.decode(eventSchema)(),
|
| 162 |
+
Arr.map((event) => ({ ...event, id: event.id }))
|
| 163 |
+
)
|
| 164 |
)
|
| 165 |
}
|
| 166 |
|
| 167 |
/**
|
| 168 |
+
* Creates a stateful Server-Sent Events parser.
|
| 169 |
+
*
|
| 170 |
+
* **Details**
|
| 171 |
*
|
| 172 |
+
* Call `feed` with text chunks to parse `Event` and `Retry` values through the
|
| 173 |
+
* callback, and call `reset` to clear any buffered event state.
|
| 174 |
*
|
| 175 |
+
* @category decoding
|
| 176 |
* @since 4.0.0
|
|
|
|
| 177 |
*/
|
| 178 |
export function makeParser(onParse: (event: AnyEvent) => void): Parser {
|
| 179 |
// Processing state
|
|
|
|
| 334 |
}
|
| 335 |
|
| 336 |
/**
|
| 337 |
+
* Stateful Server-Sent Events parser returned by `makeParser`.
|
| 338 |
+
*
|
| 339 |
+
* **Details**
|
| 340 |
+
*
|
| 341 |
+
* `feed` accepts additional text chunks and `reset` clears buffered parser state.
|
| 342 |
+
*
|
| 343 |
+
* @category decoding
|
| 344 |
* @since 4.0.0
|
|
|
|
| 345 |
*/
|
| 346 |
export interface Parser {
|
| 347 |
feed(chunk: string): void
|
|
|
|
| 349 |
}
|
| 350 |
|
| 351 |
/**
|
| 352 |
+
* Creates a channel that encodes `Event` values as Server-Sent Events text.
|
| 353 |
+
*
|
| 354 |
+
* **Details**
|
| 355 |
+
*
|
| 356 |
+
* If the upstream channel fails with `Retry`, the retry directive is written and
|
| 357 |
+
* the encoder completes.
|
| 358 |
+
*
|
| 359 |
+
* @category encoding
|
| 360 |
* @since 4.0.0
|
|
|
|
| 361 |
*/
|
| 362 |
export const encode = <IE, Done>(): Channel.Channel<
|
| 363 |
NonEmptyReadonlyArray<string>,
|
|
|
|
| 383 |
)
|
| 384 |
|
| 385 |
/**
|
| 386 |
+
* Creates an SSE encoder channel for values accepted by a schema.
|
| 387 |
+
*
|
| 388 |
+
* **Details**
|
| 389 |
+
*
|
| 390 |
+
* Values are schema-encoded to the untagged SSE event shape, transformed to
|
| 391 |
+
* `Event`, and then written as Server-Sent Events text.
|
| 392 |
+
*
|
| 393 |
+
* @category encoding
|
| 394 |
* @since 4.0.0
|
|
|
|
| 395 |
*/
|
| 396 |
export const encodeSchema = <
|
| 397 |
+
S extends EventCodec,
|
|
|
|
|
|
|
|
|
|
| 398 |
IE,
|
| 399 |
Done
|
| 400 |
>(schema: S): Channel.Channel<
|
|
|
|
| 413 |
)
|
| 414 |
|
| 415 |
/**
|
| 416 |
+
* Encoder capable of rendering an `Event` or `Retry` value as Server-Sent
|
| 417 |
+
* Events text.
|
| 418 |
+
*
|
| 419 |
+
* @category encoding
|
| 420 |
* @since 4.0.0
|
|
|
|
| 421 |
*/
|
| 422 |
export interface Encoder {
|
| 423 |
write(event: AnyEvent): string
|
| 424 |
}
|
| 425 |
|
| 426 |
/**
|
| 427 |
+
* Tagged model for a Server-Sent Events message containing the event name, optional event ID, and string data payload.
|
| 428 |
+
*
|
| 429 |
+
* @category models
|
| 430 |
* @since 4.0.0
|
|
|
|
| 431 |
*/
|
| 432 |
export interface Event {
|
| 433 |
readonly _tag: "Event"
|
|
|
|
| 437 |
}
|
| 438 |
|
| 439 |
/**
|
| 440 |
+
* Schema for the untagged Server-Sent Events payload shape containing an optional `id`, `event`, and string `data` fields.
|
| 441 |
+
*
|
| 442 |
+
* @category models
|
| 443 |
* @since 4.0.0
|
|
|
|
| 444 |
*/
|
| 445 |
export const EventEncoded: Schema.Struct<{
|
| 446 |
+
readonly id: Schema.optional<Schema.String>
|
| 447 |
readonly event: Schema.String
|
| 448 |
readonly data: Schema.String
|
| 449 |
}> = Schema.Struct({
|
| 450 |
+
id: Schema.optional(Schema.String),
|
| 451 |
event: Schema.String,
|
| 452 |
data: Schema.String
|
| 453 |
})
|
| 454 |
|
| 455 |
/**
|
| 456 |
+
* Schema for the tagged Server-Sent Events message model that adds `_tag: "Event"` to the event name, optional event ID, and string data payload.
|
| 457 |
+
*
|
| 458 |
+
* @category models
|
| 459 |
* @since 4.0.0
|
|
|
|
| 460 |
*/
|
| 461 |
export const Event: Schema.Struct<{
|
| 462 |
readonly _tag: Schema.tag<"Event">
|
|
|
|
| 471 |
})
|
| 472 |
|
| 473 |
/**
|
| 474 |
+
* Schema for transforming untagged SSE event payloads into tagged `Event`
|
| 475 |
+
* models.
|
| 476 |
+
*
|
| 477 |
+
* @category models
|
| 478 |
* @since 4.0.0
|
|
|
|
| 479 |
*/
|
| 480 |
+
export const transformEvent = SchemaTransformation.transform<{
|
| 481 |
readonly id?: string | undefined
|
| 482 |
+
readonly event?: string | undefined
|
| 483 |
readonly data: string
|
| 484 |
}, {
|
| 485 |
readonly _tag: "Event"
|
|
|
|
| 491 |
encode: (event) => ({
|
| 492 |
_tag: "Event",
|
| 493 |
id: event.id,
|
| 494 |
+
event: event.event ?? "message",
|
| 495 |
data: event.data
|
| 496 |
})
|
| 497 |
})
|
| 498 |
|
| 499 |
/**
|
| 500 |
+
* Untagged Server-Sent Events payload shape containing the event name, optional event ID, and string data payload.
|
| 501 |
+
*
|
| 502 |
+
* @category models
|
| 503 |
* @since 4.0.0
|
|
|
|
| 504 |
*/
|
| 505 |
export interface EventEncoded {
|
| 506 |
readonly event: string
|
| 507 |
+
readonly id?: string | undefined
|
| 508 |
readonly data: string
|
| 509 |
}
|
| 510 |
|
| 511 |
const RetryTypeId = "~effect/encoding/Sse/Retry" as const
|
| 512 |
|
| 513 |
/**
|
| 514 |
+
* Represents a Server-Sent Events retry directive.
|
| 515 |
+
*
|
| 516 |
+
* **Details**
|
| 517 |
+
*
|
| 518 |
+
* Decoders surface this value as a failure to request reconnection after
|
| 519 |
+
* `duration`; encoders serialize an upstream `Retry` failure as a `retry:` line.
|
| 520 |
+
*
|
| 521 |
+
* @category models
|
| 522 |
* @since 4.0.0
|
|
|
|
| 523 |
*/
|
| 524 |
export class Retry extends Data.TaggedClass("Retry")<{
|
| 525 |
readonly duration: Duration.Duration
|
| 526 |
readonly lastEventId: string | undefined
|
| 527 |
}> {
|
| 528 |
/**
|
| 529 |
+
* Marks this value as an SSE retry directive for runtime guards.
|
| 530 |
+
*
|
| 531 |
* @since 4.0.0
|
| 532 |
*/
|
| 533 |
readonly [RetryTypeId]: typeof RetryTypeId = RetryTypeId
|
| 534 |
/**
|
| 535 |
+
* Returns `true` when the value is an SSE retry directive.
|
| 536 |
+
*
|
| 537 |
* @since 4.0.0
|
| 538 |
*/
|
| 539 |
static is(u: unknown): u is Retry {
|
| 540 |
return hasProperty(u, RetryTypeId)
|
| 541 |
}
|
| 542 |
/**
|
| 543 |
+
* Separates SSE retry directives from regular event values.
|
| 544 |
+
*
|
| 545 |
* @since 4.0.0
|
| 546 |
*/
|
| 547 |
static filter<A>(u: A): Result.Result<Retry, Exclude<A, Retry>> {
|
|
|
|
| 550 |
}
|
| 551 |
|
| 552 |
/**
|
| 553 |
+
* Union of SSE values that can be rendered by an `Encoder`: regular events and
|
| 554 |
+
* retry directives.
|
| 555 |
+
*
|
| 556 |
+
* @category models
|
| 557 |
* @since 4.0.0
|
|
|
|
| 558 |
*/
|
| 559 |
export type AnyEvent = Event | Retry
|
| 560 |
|
| 561 |
/**
|
| 562 |
+
* Default Server-Sent Events encoder.
|
| 563 |
+
*
|
| 564 |
+
* **Details**
|
| 565 |
+
*
|
| 566 |
+
* It renders `Event` values as `id`, `event`, and `data` lines and renders
|
| 567 |
+
* `Retry` values as `retry:` directives.
|
| 568 |
+
*
|
| 569 |
+
* @category encoding
|
| 570 |
* @since 4.0.0
|
|
|
|
| 571 |
*/
|
| 572 |
export const encoder: Encoder = {
|
| 573 |
write(event: AnyEvent): string {
|
.opencode/node_modules/effect/src/unstable/eventlog/Event.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import { pipeArguments } from "../../Pipeable.ts"
|
|
@@ -7,28 +16,39 @@ import * as Schema from "../../Schema.ts"
|
|
| 7 |
import * as Msgpack from "../encoding/Msgpack.ts"
|
| 8 |
|
| 9 |
/**
|
|
|
|
|
|
|
|
|
|
| 10 |
* @since 4.0.0
|
| 11 |
-
* @category type ids
|
| 12 |
*/
|
| 13 |
export type TypeId = "~effect/eventlog/Event"
|
| 14 |
|
| 15 |
/**
|
|
|
|
|
|
|
|
|
|
| 16 |
* @since 4.0.0
|
| 17 |
-
* @category type ids
|
| 18 |
*/
|
| 19 |
export const TypeId: TypeId = "~effect/eventlog/Event"
|
| 20 |
|
| 21 |
/**
|
| 22 |
-
*
|
|
|
|
| 23 |
* @category guards
|
|
|
|
| 24 |
*/
|
| 25 |
export const isEvent = (u: unknown): u is Event<any, any, any, any> => Predicate.hasProperty(u, TypeId)
|
| 26 |
|
| 27 |
/**
|
| 28 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
*
|
| 30 |
-
* @since 4.0.0
|
| 31 |
* @category models
|
|
|
|
| 32 |
*/
|
| 33 |
export interface Event<
|
| 34 |
out Tag extends string,
|
|
@@ -46,8 +66,15 @@ export interface Event<
|
|
| 46 |
}
|
| 47 |
|
| 48 |
/**
|
| 49 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
* @category models
|
|
|
|
| 51 |
*/
|
| 52 |
export interface EventHandler<in out Tag extends string> {
|
| 53 |
readonly _: unique symbol
|
|
@@ -55,8 +82,15 @@ export interface EventHandler<in out Tag extends string> {
|
|
| 55 |
}
|
| 56 |
|
| 57 |
/**
|
| 58 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
* @category models
|
|
|
|
| 60 |
*/
|
| 61 |
export interface Any {
|
| 62 |
readonly [TypeId]: TypeId
|
|
@@ -69,14 +103,19 @@ export interface Any {
|
|
| 69 |
}
|
| 70 |
|
| 71 |
/**
|
| 72 |
-
*
|
|
|
|
|
|
|
| 73 |
* @category models
|
|
|
|
| 74 |
*/
|
| 75 |
export interface AnyWithProps extends Any {}
|
| 76 |
|
| 77 |
/**
|
| 78 |
-
*
|
|
|
|
| 79 |
* @category models
|
|
|
|
| 80 |
*/
|
| 81 |
export type ToService<A> = A extends Event<
|
| 82 |
infer _Tag,
|
|
@@ -87,8 +126,10 @@ export type ToService<A> = A extends Event<
|
|
| 87 |
never
|
| 88 |
|
| 89 |
/**
|
| 90 |
-
*
|
|
|
|
| 91 |
* @category models
|
|
|
|
| 92 |
*/
|
| 93 |
export type Tag<A> = A extends Event<
|
| 94 |
infer _Tag,
|
|
@@ -99,8 +140,10 @@ export type Tag<A> = A extends Event<
|
|
| 99 |
never
|
| 100 |
|
| 101 |
/**
|
| 102 |
-
*
|
|
|
|
| 103 |
* @category models
|
|
|
|
| 104 |
*/
|
| 105 |
export type ErrorSchema<A extends Any> = A extends Event<
|
| 106 |
infer _Tag,
|
|
@@ -111,14 +154,19 @@ export type ErrorSchema<A extends Any> = A extends Event<
|
|
| 111 |
: never
|
| 112 |
|
| 113 |
/**
|
| 114 |
-
*
|
|
|
|
| 115 |
* @category models
|
|
|
|
| 116 |
*/
|
| 117 |
export type Error<A extends Any> = Schema.Schema.Type<ErrorSchema<A>>
|
| 118 |
|
| 119 |
/**
|
| 120 |
-
*
|
|
|
|
|
|
|
| 121 |
* @category models
|
|
|
|
| 122 |
*/
|
| 123 |
export type AddError<A extends Any, Error extends Schema.Top> = A extends Event<
|
| 124 |
infer _Tag,
|
|
@@ -129,8 +177,10 @@ export type AddError<A extends Any, Error extends Schema.Top> = A extends Event<
|
|
| 129 |
: never
|
| 130 |
|
| 131 |
/**
|
| 132 |
-
*
|
|
|
|
| 133 |
* @category models
|
|
|
|
| 134 |
*/
|
| 135 |
export type PayloadSchema<A extends Any> = A extends Event<
|
| 136 |
infer _Tag,
|
|
@@ -141,8 +191,10 @@ export type PayloadSchema<A extends Any> = A extends Event<
|
|
| 141 |
: never
|
| 142 |
|
| 143 |
/**
|
| 144 |
-
*
|
|
|
|
| 145 |
* @category models
|
|
|
|
| 146 |
*/
|
| 147 |
export type PayloadSchemaWithTag<A extends Any, Tag extends string> = A extends Event<
|
| 148 |
Tag,
|
|
@@ -153,14 +205,23 @@ export type PayloadSchemaWithTag<A extends Any, Tag extends string> = A extends
|
|
| 153 |
: never
|
| 154 |
|
| 155 |
/**
|
| 156 |
-
*
|
|
|
|
| 157 |
* @category models
|
|
|
|
| 158 |
*/
|
| 159 |
export type Payload<A extends Any> = Schema.Schema.Type<PayloadSchema<A>>
|
| 160 |
|
| 161 |
/**
|
| 162 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
* @category models
|
|
|
|
| 164 |
*/
|
| 165 |
export type TaggedPayload<A extends Any> = A extends Event<
|
| 166 |
infer _Tag,
|
|
@@ -174,8 +235,10 @@ export type TaggedPayload<A extends Any> = A extends Event<
|
|
| 174 |
: never
|
| 175 |
|
| 176 |
/**
|
| 177 |
-
*
|
|
|
|
| 178 |
* @category models
|
|
|
|
| 179 |
*/
|
| 180 |
export type SuccessSchema<A extends Any> = A extends Event<
|
| 181 |
infer _Tag,
|
|
@@ -186,14 +249,23 @@ export type SuccessSchema<A extends Any> = A extends Event<
|
|
| 186 |
: never
|
| 187 |
|
| 188 |
/**
|
| 189 |
-
*
|
|
|
|
| 190 |
* @category models
|
|
|
|
| 191 |
*/
|
| 192 |
export type Success<A extends Any> = Schema.Schema.Type<SuccessSchema<A>>
|
| 193 |
|
| 194 |
/**
|
| 195 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
* @category models
|
|
|
|
| 197 |
*/
|
| 198 |
export type ServicesClient<A> = A extends Event<
|
| 199 |
infer _Tag,
|
|
@@ -207,8 +279,15 @@ export type ServicesClient<A> = A extends Event<
|
|
| 207 |
: never
|
| 208 |
|
| 209 |
/**
|
| 210 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
* @category models
|
|
|
|
| 212 |
*/
|
| 213 |
export type ServicesServer<A> = A extends Event<
|
| 214 |
infer _Tag,
|
|
@@ -222,8 +301,11 @@ export type ServicesServer<A> = A extends Event<
|
|
| 222 |
: never
|
| 223 |
|
| 224 |
/**
|
| 225 |
-
*
|
|
|
|
|
|
|
| 226 |
* @category models
|
|
|
|
| 227 |
*/
|
| 228 |
export type Services<A> = A extends Event<
|
| 229 |
infer _Tag,
|
|
@@ -240,38 +322,51 @@ export type Services<A> = A extends Event<
|
|
| 240 |
: never
|
| 241 |
|
| 242 |
/**
|
| 243 |
-
*
|
|
|
|
| 244 |
* @category models
|
|
|
|
| 245 |
*/
|
| 246 |
export type WithTag<Events extends Any, Tag extends string> = Extract<Events, { readonly tag: Tag }>
|
| 247 |
|
| 248 |
/**
|
| 249 |
-
*
|
|
|
|
| 250 |
* @category models
|
|
|
|
| 251 |
*/
|
| 252 |
export type ExcludeTag<Events extends Any, Tag extends string> = Exclude<Events, { readonly tag: Tag }>
|
| 253 |
|
| 254 |
/**
|
| 255 |
-
*
|
|
|
|
| 256 |
* @category models
|
|
|
|
| 257 |
*/
|
| 258 |
export type PayloadWithTag<Events extends Any, Tag extends string> = Payload<WithTag<Events, Tag>>
|
| 259 |
|
| 260 |
/**
|
| 261 |
-
*
|
|
|
|
| 262 |
* @category models
|
|
|
|
| 263 |
*/
|
| 264 |
export type SuccessWithTag<Events extends Any, Tag extends string> = Success<WithTag<Events, Tag>>
|
| 265 |
|
| 266 |
/**
|
| 267 |
-
*
|
|
|
|
| 268 |
* @category models
|
|
|
|
| 269 |
*/
|
| 270 |
export type ErrorWithTag<Events extends Any, Tag extends string> = Error<WithTag<Events, Tag>>
|
| 271 |
|
| 272 |
/**
|
| 273 |
-
*
|
|
|
|
|
|
|
| 274 |
* @category models
|
|
|
|
| 275 |
*/
|
| 276 |
export type ServicesClientWithTag<Events extends Any, Tag extends string> = ServicesClient<WithTag<Events, Tag>>
|
| 277 |
|
|
@@ -283,8 +378,16 @@ const Proto = {
|
|
| 283 |
}
|
| 284 |
|
| 285 |
/**
|
| 286 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
* @category constructors
|
|
|
|
| 288 |
*/
|
| 289 |
export function make<
|
| 290 |
Tag extends string,
|
|
@@ -319,8 +422,15 @@ export function make(options: {
|
|
| 319 |
}
|
| 320 |
|
| 321 |
/**
|
| 322 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
* @category constructors
|
|
|
|
| 324 |
*/
|
| 325 |
export function addError<A extends Any, Error2 extends Schema.Top>(
|
| 326 |
event: A,
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Typed event definitions for the unstable event-log system.
|
| 3 |
+
*
|
| 4 |
+
* An `Event` is the durable contract shared by writers, handlers, journals, and
|
| 5 |
+
* replicas. It gives an event a stable tag, derives the aggregate or entity
|
| 6 |
+
* primary key from the decoded payload, and records the schemas used for the
|
| 7 |
+
* payload, handler result, and handler errors. The payload schema is also used
|
| 8 |
+
* to derive the MessagePack encoding for journal entries and remote
|
| 9 |
+
* replication.
|
| 10 |
+
*
|
| 11 |
* @since 4.0.0
|
| 12 |
*/
|
| 13 |
import { pipeArguments } from "../../Pipeable.ts"
|
|
|
|
| 16 |
import * as Msgpack from "../encoding/Msgpack.ts"
|
| 17 |
|
| 18 |
/**
|
| 19 |
+
* Unique type identifier used to mark event log event definitions.
|
| 20 |
+
*
|
| 21 |
+
* @category type IDs
|
| 22 |
* @since 4.0.0
|
|
|
|
| 23 |
*/
|
| 24 |
export type TypeId = "~effect/eventlog/Event"
|
| 25 |
|
| 26 |
/**
|
| 27 |
+
* Runtime type identifier used to mark event log event definitions.
|
| 28 |
+
*
|
| 29 |
+
* @category type IDs
|
| 30 |
* @since 4.0.0
|
|
|
|
| 31 |
*/
|
| 32 |
export const TypeId: TypeId = "~effect/eventlog/Event"
|
| 33 |
|
| 34 |
/**
|
| 35 |
+
* Returns `true` when a value is an event log event definition.
|
| 36 |
+
*
|
| 37 |
* @category guards
|
| 38 |
+
* @since 4.0.0
|
| 39 |
*/
|
| 40 |
export const isEvent = (u: unknown): u is Event<any, any, any, any> => Predicate.hasProperty(u, TypeId)
|
| 41 |
|
| 42 |
/**
|
| 43 |
+
* Definition of an event type that can be written to an `EventLog`.
|
| 44 |
+
*
|
| 45 |
+
* **Details**
|
| 46 |
+
*
|
| 47 |
+
* An event definition contains its tag, primary-key function, payload schema,
|
| 48 |
+
* MessagePack payload schema, success schema, and error schema.
|
| 49 |
*
|
|
|
|
| 50 |
* @category models
|
| 51 |
+
* @since 4.0.0
|
| 52 |
*/
|
| 53 |
export interface Event<
|
| 54 |
out Tag extends string,
|
|
|
|
| 66 |
}
|
| 67 |
|
| 68 |
/**
|
| 69 |
+
* Marker service associated with the handler for an event tag.
|
| 70 |
+
*
|
| 71 |
+
* **Details**
|
| 72 |
+
*
|
| 73 |
+
* `ToService` derives this service from an `Event` so handler layers can expose
|
| 74 |
+
* which events they implement.
|
| 75 |
+
*
|
| 76 |
* @category models
|
| 77 |
+
* @since 4.0.0
|
| 78 |
*/
|
| 79 |
export interface EventHandler<in out Tag extends string> {
|
| 80 |
readonly _: unique symbol
|
|
|
|
| 82 |
}
|
| 83 |
|
| 84 |
/**
|
| 85 |
+
* Type-erased event log event definition.
|
| 86 |
+
*
|
| 87 |
+
* **Details**
|
| 88 |
+
*
|
| 89 |
+
* It preserves the runtime tag, primary-key function, payload schema, success
|
| 90 |
+
* schema, and error schema without retaining the original type parameters.
|
| 91 |
+
*
|
| 92 |
* @category models
|
| 93 |
+
* @since 4.0.0
|
| 94 |
*/
|
| 95 |
export interface Any {
|
| 96 |
readonly [TypeId]: TypeId
|
|
|
|
| 103 |
}
|
| 104 |
|
| 105 |
/**
|
| 106 |
+
* Type-erased event definition with its runtime properties available
|
| 107 |
+
* structurally.
|
| 108 |
+
*
|
| 109 |
* @category models
|
| 110 |
+
* @since 4.0.0
|
| 111 |
*/
|
| 112 |
export interface AnyWithProps extends Any {}
|
| 113 |
|
| 114 |
/**
|
| 115 |
+
* Derives the handler service marker for an event definition.
|
| 116 |
+
*
|
| 117 |
* @category models
|
| 118 |
+
* @since 4.0.0
|
| 119 |
*/
|
| 120 |
export type ToService<A> = A extends Event<
|
| 121 |
infer _Tag,
|
|
|
|
| 126 |
never
|
| 127 |
|
| 128 |
/**
|
| 129 |
+
* Extracts the tag string from an event definition.
|
| 130 |
+
*
|
| 131 |
* @category models
|
| 132 |
+
* @since 4.0.0
|
| 133 |
*/
|
| 134 |
export type Tag<A> = A extends Event<
|
| 135 |
infer _Tag,
|
|
|
|
| 140 |
never
|
| 141 |
|
| 142 |
/**
|
| 143 |
+
* Extracts the error schema from an event definition.
|
| 144 |
+
*
|
| 145 |
* @category models
|
| 146 |
+
* @since 4.0.0
|
| 147 |
*/
|
| 148 |
export type ErrorSchema<A extends Any> = A extends Event<
|
| 149 |
infer _Tag,
|
|
|
|
| 154 |
: never
|
| 155 |
|
| 156 |
/**
|
| 157 |
+
* Decoded error value type for an event definition.
|
| 158 |
+
*
|
| 159 |
* @category models
|
| 160 |
+
* @since 4.0.0
|
| 161 |
*/
|
| 162 |
export type Error<A extends Any> = Schema.Schema.Type<ErrorSchema<A>>
|
| 163 |
|
| 164 |
/**
|
| 165 |
+
* Returns an event definition type whose error schema also includes the provided
|
| 166 |
+
* error schema.
|
| 167 |
+
*
|
| 168 |
* @category models
|
| 169 |
+
* @since 4.0.0
|
| 170 |
*/
|
| 171 |
export type AddError<A extends Any, Error extends Schema.Top> = A extends Event<
|
| 172 |
infer _Tag,
|
|
|
|
| 177 |
: never
|
| 178 |
|
| 179 |
/**
|
| 180 |
+
* Extracts the payload schema from an event definition.
|
| 181 |
+
*
|
| 182 |
* @category models
|
| 183 |
+
* @since 4.0.0
|
| 184 |
*/
|
| 185 |
export type PayloadSchema<A extends Any> = A extends Event<
|
| 186 |
infer _Tag,
|
|
|
|
| 191 |
: never
|
| 192 |
|
| 193 |
/**
|
| 194 |
+
* Extracts the payload schema for the event in a union with the specified tag.
|
| 195 |
+
*
|
| 196 |
* @category models
|
| 197 |
+
* @since 4.0.0
|
| 198 |
*/
|
| 199 |
export type PayloadSchemaWithTag<A extends Any, Tag extends string> = A extends Event<
|
| 200 |
Tag,
|
|
|
|
| 205 |
: never
|
| 206 |
|
| 207 |
/**
|
| 208 |
+
* Decoded payload value type for an event definition.
|
| 209 |
+
*
|
| 210 |
* @category models
|
| 211 |
+
* @since 4.0.0
|
| 212 |
*/
|
| 213 |
export type Payload<A extends Any> = Schema.Schema.Type<PayloadSchema<A>>
|
| 214 |
|
| 215 |
/**
|
| 216 |
+
* Tagged payload value for an event definition.
|
| 217 |
+
*
|
| 218 |
+
* **Details**
|
| 219 |
+
*
|
| 220 |
+
* The result contains `_tag` set to the event tag and `payload` set to the
|
| 221 |
+
* decoded payload value.
|
| 222 |
+
*
|
| 223 |
* @category models
|
| 224 |
+
* @since 4.0.0
|
| 225 |
*/
|
| 226 |
export type TaggedPayload<A extends Any> = A extends Event<
|
| 227 |
infer _Tag,
|
|
|
|
| 235 |
: never
|
| 236 |
|
| 237 |
/**
|
| 238 |
+
* Extracts the success schema from an event definition.
|
| 239 |
+
*
|
| 240 |
* @category models
|
| 241 |
+
* @since 4.0.0
|
| 242 |
*/
|
| 243 |
export type SuccessSchema<A extends Any> = A extends Event<
|
| 244 |
infer _Tag,
|
|
|
|
| 249 |
: never
|
| 250 |
|
| 251 |
/**
|
| 252 |
+
* Decoded success value type for an event definition.
|
| 253 |
+
*
|
| 254 |
* @category models
|
| 255 |
+
* @since 4.0.0
|
| 256 |
*/
|
| 257 |
export type Success<A extends Any> = Schema.Schema.Type<SuccessSchema<A>>
|
| 258 |
|
| 259 |
/**
|
| 260 |
+
* Schema services required by a client for an event definition.
|
| 261 |
+
*
|
| 262 |
+
* **Details**
|
| 263 |
+
*
|
| 264 |
+
* This includes payload encoding services plus success and error decoding
|
| 265 |
+
* services.
|
| 266 |
+
*
|
| 267 |
* @category models
|
| 268 |
+
* @since 4.0.0
|
| 269 |
*/
|
| 270 |
export type ServicesClient<A> = A extends Event<
|
| 271 |
infer _Tag,
|
|
|
|
| 279 |
: never
|
| 280 |
|
| 281 |
/**
|
| 282 |
+
* Schema services required by a server for an event definition.
|
| 283 |
+
*
|
| 284 |
+
* **Details**
|
| 285 |
+
*
|
| 286 |
+
* This includes payload decoding services plus success and error encoding
|
| 287 |
+
* services.
|
| 288 |
+
*
|
| 289 |
* @category models
|
| 290 |
+
* @since 4.0.0
|
| 291 |
*/
|
| 292 |
export type ServicesServer<A> = A extends Event<
|
| 293 |
infer _Tag,
|
|
|
|
| 301 |
: never
|
| 302 |
|
| 303 |
/**
|
| 304 |
+
* All schema services required to encode and decode the payload, success, and
|
| 305 |
+
* error schemas for an event definition.
|
| 306 |
+
*
|
| 307 |
* @category models
|
| 308 |
+
* @since 4.0.0
|
| 309 |
*/
|
| 310 |
export type Services<A> = A extends Event<
|
| 311 |
infer _Tag,
|
|
|
|
| 322 |
: never
|
| 323 |
|
| 324 |
/**
|
| 325 |
+
* Extracts the event definition with the specified tag from an event union.
|
| 326 |
+
*
|
| 327 |
* @category models
|
| 328 |
+
* @since 4.0.0
|
| 329 |
*/
|
| 330 |
export type WithTag<Events extends Any, Tag extends string> = Extract<Events, { readonly tag: Tag }>
|
| 331 |
|
| 332 |
/**
|
| 333 |
+
* Removes event definitions with the specified tag from an event union.
|
| 334 |
+
*
|
| 335 |
* @category models
|
| 336 |
+
* @since 4.0.0
|
| 337 |
*/
|
| 338 |
export type ExcludeTag<Events extends Any, Tag extends string> = Exclude<Events, { readonly tag: Tag }>
|
| 339 |
|
| 340 |
/**
|
| 341 |
+
* Decoded payload value type for the event in a union with the specified tag.
|
| 342 |
+
*
|
| 343 |
* @category models
|
| 344 |
+
* @since 4.0.0
|
| 345 |
*/
|
| 346 |
export type PayloadWithTag<Events extends Any, Tag extends string> = Payload<WithTag<Events, Tag>>
|
| 347 |
|
| 348 |
/**
|
| 349 |
+
* Decoded success value type for the event in a union with the specified tag.
|
| 350 |
+
*
|
| 351 |
* @category models
|
| 352 |
+
* @since 4.0.0
|
| 353 |
*/
|
| 354 |
export type SuccessWithTag<Events extends Any, Tag extends string> = Success<WithTag<Events, Tag>>
|
| 355 |
|
| 356 |
/**
|
| 357 |
+
* Decoded error value type for the event in a union with the specified tag.
|
| 358 |
+
*
|
| 359 |
* @category models
|
| 360 |
+
* @since 4.0.0
|
| 361 |
*/
|
| 362 |
export type ErrorWithTag<Events extends Any, Tag extends string> = Error<WithTag<Events, Tag>>
|
| 363 |
|
| 364 |
/**
|
| 365 |
+
* Client-side schema services required for the event in a union with the specified
|
| 366 |
+
* tag.
|
| 367 |
+
*
|
| 368 |
* @category models
|
| 369 |
+
* @since 4.0.0
|
| 370 |
*/
|
| 371 |
export type ServicesClientWithTag<Events extends Any, Tag extends string> = ServicesClient<WithTag<Events, Tag>>
|
| 372 |
|
|
|
|
| 378 |
}
|
| 379 |
|
| 380 |
/**
|
| 381 |
+
* Creates an event log event definition.
|
| 382 |
+
*
|
| 383 |
+
* **Details**
|
| 384 |
+
*
|
| 385 |
+
* If omitted, the payload and success schemas default to `Schema.Void`, the error
|
| 386 |
+
* schema defaults to `Schema.Never`, and the MessagePack payload schema is derived
|
| 387 |
+
* from the payload schema.
|
| 388 |
+
*
|
| 389 |
* @category constructors
|
| 390 |
+
* @since 4.0.0
|
| 391 |
*/
|
| 392 |
export function make<
|
| 393 |
Tag extends string,
|
|
|
|
| 422 |
}
|
| 423 |
|
| 424 |
/**
|
| 425 |
+
* Adds another error schema to an event definition.
|
| 426 |
+
*
|
| 427 |
+
* **Details**
|
| 428 |
+
*
|
| 429 |
+
* The returned event keeps the same tag, primary key, payload, and success schema
|
| 430 |
+
* while replacing the error schema with a union of the existing and new errors.
|
| 431 |
+
*
|
| 432 |
* @category constructors
|
| 433 |
+
* @since 4.0.0
|
| 434 |
*/
|
| 435 |
export function addError<A extends Any, Error2 extends Schema.Top>(
|
| 436 |
event: A,
|
.opencode/node_modules/effect/src/unstable/eventlog/EventGroup.ts
CHANGED
|
@@ -1,4 +1,12 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import { type Pipeable, pipeArguments } from "../../Pipeable.ts"
|
|
@@ -8,31 +16,40 @@ import type * as Schema from "../../Schema.ts"
|
|
| 8 |
import * as Event from "./Event.ts"
|
| 9 |
|
| 10 |
/**
|
|
|
|
|
|
|
|
|
|
| 11 |
* @since 4.0.0
|
| 12 |
-
* @category type ids
|
| 13 |
*/
|
| 14 |
export type TypeId = "~effect/eventlog/EventGroup"
|
| 15 |
|
| 16 |
/**
|
|
|
|
|
|
|
|
|
|
| 17 |
* @since 4.0.0
|
| 18 |
-
* @category type ids
|
| 19 |
*/
|
| 20 |
export const TypeId: TypeId = "~effect/eventlog/EventGroup"
|
| 21 |
|
| 22 |
/**
|
| 23 |
-
*
|
|
|
|
| 24 |
* @category guards
|
|
|
|
| 25 |
*/
|
| 26 |
export const isEventGroup = (u: unknown): u is Any => Predicate.hasProperty(u, TypeId)
|
| 27 |
|
| 28 |
/**
|
| 29 |
-
*
|
| 30 |
-
*
|
| 31 |
*
|
| 32 |
-
*
|
|
|
|
|
|
|
|
|
|
| 33 |
*
|
| 34 |
-
* @since 4.0.0
|
| 35 |
* @category models
|
|
|
|
| 36 |
*/
|
| 37 |
export interface EventGroup<
|
| 38 |
out Events extends Event.Any = Event.Any
|
|
@@ -63,42 +80,54 @@ export interface EventGroup<
|
|
| 63 |
}
|
| 64 |
|
| 65 |
/**
|
| 66 |
-
*
|
|
|
|
| 67 |
* @category models
|
|
|
|
| 68 |
*/
|
| 69 |
export interface Any {
|
| 70 |
readonly [TypeId]: TypeId
|
| 71 |
}
|
| 72 |
|
| 73 |
/**
|
| 74 |
-
*
|
|
|
|
| 75 |
* @category models
|
|
|
|
| 76 |
*/
|
| 77 |
export type AnyWithProps = EventGroup<Event.Any>
|
| 78 |
|
| 79 |
/**
|
| 80 |
-
*
|
|
|
|
| 81 |
* @category models
|
|
|
|
| 82 |
*/
|
| 83 |
export type ToService<A> = A extends EventGroup<infer _Events> ? Event.ToService<_Events>
|
| 84 |
: never
|
| 85 |
|
| 86 |
/**
|
| 87 |
-
*
|
|
|
|
| 88 |
* @category models
|
|
|
|
| 89 |
*/
|
| 90 |
export type Events<Group> = Group extends EventGroup<infer _Events> ? _Events
|
| 91 |
: never
|
| 92 |
|
| 93 |
/**
|
| 94 |
-
*
|
|
|
|
| 95 |
* @category models
|
|
|
|
| 96 |
*/
|
| 97 |
export type ServicesClient<Group> = Event.ServicesClient<Events<Group>>
|
| 98 |
|
| 99 |
/**
|
| 100 |
-
*
|
|
|
|
| 101 |
* @category models
|
|
|
|
| 102 |
*/
|
| 103 |
export type ServicesServer<Group> = Event.ServicesServer<Events<Group>>
|
| 104 |
|
|
@@ -151,12 +180,14 @@ const makeProto = <
|
|
| 151 |
}
|
| 152 |
|
| 153 |
/**
|
| 154 |
-
*
|
| 155 |
-
* represent a portion of your domain.
|
| 156 |
*
|
| 157 |
-
*
|
|
|
|
|
|
|
|
|
|
| 158 |
*
|
| 159 |
-
* @since 4.0.0
|
| 160 |
* @category constructors
|
|
|
|
| 161 |
*/
|
| 162 |
export const empty: EventGroup<never> = makeProto({ events: Record.empty() })
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Defines groups of typed events for the unstable event-log system.
|
| 3 |
+
*
|
| 4 |
+
* An `EventGroup` collects event definitions that belong together. Start with
|
| 5 |
+
* `empty`, add events with their payload, success, and error schemas, then use
|
| 6 |
+
* the group to build clients with `EventLog.schema` and server handlers with
|
| 7 |
+
* `EventLog.group`. The group only describes events; it does not write or run
|
| 8 |
+
* them.
|
| 9 |
+
*
|
| 10 |
* @since 4.0.0
|
| 11 |
*/
|
| 12 |
import { type Pipeable, pipeArguments } from "../../Pipeable.ts"
|
|
|
|
| 16 |
import * as Event from "./Event.ts"
|
| 17 |
|
| 18 |
/**
|
| 19 |
+
* Unique type identifier used to mark event log event groups.
|
| 20 |
+
*
|
| 21 |
+
* @category type IDs
|
| 22 |
* @since 4.0.0
|
|
|
|
| 23 |
*/
|
| 24 |
export type TypeId = "~effect/eventlog/EventGroup"
|
| 25 |
|
| 26 |
/**
|
| 27 |
+
* Runtime type identifier used to mark event log event groups.
|
| 28 |
+
*
|
| 29 |
+
* @category type IDs
|
| 30 |
* @since 4.0.0
|
|
|
|
| 31 |
*/
|
| 32 |
export const TypeId: TypeId = "~effect/eventlog/EventGroup"
|
| 33 |
|
| 34 |
/**
|
| 35 |
+
* Returns `true` when a value is an event log event group.
|
| 36 |
+
*
|
| 37 |
* @category guards
|
| 38 |
+
* @since 4.0.0
|
| 39 |
*/
|
| 40 |
export const isEventGroup = (u: unknown): u is Any => Predicate.hasProperty(u, TypeId)
|
| 41 |
|
| 42 |
/**
|
| 43 |
+
* Typed collection of event definitions that represents a portion of an event log
|
| 44 |
+
* domain.
|
| 45 |
*
|
| 46 |
+
* **When to use**
|
| 47 |
+
*
|
| 48 |
+
* Use when build groups from `empty.add(...)`, then provide implementations for the events
|
| 49 |
+
* with `EventLog.group`.
|
| 50 |
*
|
|
|
|
| 51 |
* @category models
|
| 52 |
+
* @since 4.0.0
|
| 53 |
*/
|
| 54 |
export interface EventGroup<
|
| 55 |
out Events extends Event.Any = Event.Any
|
|
|
|
| 80 |
}
|
| 81 |
|
| 82 |
/**
|
| 83 |
+
* Type-erased marker for an event log event group.
|
| 84 |
+
*
|
| 85 |
* @category models
|
| 86 |
+
* @since 4.0.0
|
| 87 |
*/
|
| 88 |
export interface Any {
|
| 89 |
readonly [TypeId]: TypeId
|
| 90 |
}
|
| 91 |
|
| 92 |
/**
|
| 93 |
+
* Type-erased event group with its events record available structurally.
|
| 94 |
+
*
|
| 95 |
* @category models
|
| 96 |
+
* @since 4.0.0
|
| 97 |
*/
|
| 98 |
export type AnyWithProps = EventGroup<Event.Any>
|
| 99 |
|
| 100 |
/**
|
| 101 |
+
* Derives the handler service markers required for all events in an event group.
|
| 102 |
+
*
|
| 103 |
* @category models
|
| 104 |
+
* @since 4.0.0
|
| 105 |
*/
|
| 106 |
export type ToService<A> = A extends EventGroup<infer _Events> ? Event.ToService<_Events>
|
| 107 |
: never
|
| 108 |
|
| 109 |
/**
|
| 110 |
+
* Extracts the union of event definitions contained in an event group.
|
| 111 |
+
*
|
| 112 |
* @category models
|
| 113 |
+
* @since 4.0.0
|
| 114 |
*/
|
| 115 |
export type Events<Group> = Group extends EventGroup<infer _Events> ? _Events
|
| 116 |
: never
|
| 117 |
|
| 118 |
/**
|
| 119 |
+
* Client-side schema services required by all events in an event group.
|
| 120 |
+
*
|
| 121 |
* @category models
|
| 122 |
+
* @since 4.0.0
|
| 123 |
*/
|
| 124 |
export type ServicesClient<Group> = Event.ServicesClient<Events<Group>>
|
| 125 |
|
| 126 |
/**
|
| 127 |
+
* Server-side schema services required by all events in an event group.
|
| 128 |
+
*
|
| 129 |
* @category models
|
| 130 |
+
* @since 4.0.0
|
| 131 |
*/
|
| 132 |
export type ServicesServer<Group> = Event.ServicesServer<Events<Group>>
|
| 133 |
|
|
|
|
| 180 |
}
|
| 181 |
|
| 182 |
/**
|
| 183 |
+
* Creates an empty event group used as the starting point for defining a group.
|
|
|
|
| 184 |
*
|
| 185 |
+
* **When to use**
|
| 186 |
+
*
|
| 187 |
+
* Use when you need the starting `EventGroup` value before adding event
|
| 188 |
+
* definitions with `.add(...)`.
|
| 189 |
*
|
|
|
|
| 190 |
* @category constructors
|
| 191 |
+
* @since 4.0.0
|
| 192 |
*/
|
| 193 |
export const empty: EventGroup<never> = makeProto({ events: Record.empty() })
|
.opencode/node_modules/effect/src/unstable/eventlog/EventJournal.ts
CHANGED
|
@@ -1,4 +1,14 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Uuid from "uuid"
|
|
@@ -17,8 +27,15 @@ import * as Msgpack from "../encoding/Msgpack.ts"
|
|
| 17 |
import type { StoreId } from "./EventLogMessage.ts"
|
| 18 |
|
| 19 |
/**
|
| 20 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
* @category context
|
|
|
|
| 22 |
*/
|
| 23 |
export class EventJournal extends Context.Service<EventJournal, {
|
| 24 |
/**
|
|
@@ -40,6 +57,8 @@ export class EventJournal extends Context.Service<EventJournal, {
|
|
| 40 |
/**
|
| 41 |
* Write events from a remote source to the journal.
|
| 42 |
*
|
|
|
|
|
|
|
| 43 |
* Effects run sequentially in compaction bracket order.
|
| 44 |
*/
|
| 45 |
readonly writeFromRemote: (
|
|
@@ -90,78 +109,116 @@ export class EventJournal extends Context.Service<EventJournal, {
|
|
| 90 |
const TypeId = "effect/eventlog/EventJournal/EventJournalError" as const
|
| 91 |
|
| 92 |
/**
|
| 93 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
* @category errors
|
|
|
|
| 95 |
*/
|
| 96 |
export class EventJournalError extends Data.TaggedError("EventJournalError")<{
|
| 97 |
readonly method: string
|
| 98 |
readonly cause: unknown
|
| 99 |
}> {
|
| 100 |
/**
|
|
|
|
|
|
|
| 101 |
* @since 4.0.0
|
| 102 |
*/
|
| 103 |
readonly [TypeId] = TypeId
|
| 104 |
}
|
| 105 |
|
| 106 |
/**
|
|
|
|
|
|
|
|
|
|
| 107 |
* @since 4.0.0
|
| 108 |
-
* @category remote
|
| 109 |
*/
|
| 110 |
export type RemoteIdTypeId = "effect/eventlog/EventJournal/RemoteId"
|
| 111 |
|
| 112 |
/**
|
|
|
|
|
|
|
|
|
|
| 113 |
* @since 4.0.0
|
| 114 |
-
* @category remote
|
| 115 |
*/
|
| 116 |
export const RemoteIdTypeId: RemoteIdTypeId = "effect/eventlog/EventJournal/RemoteId"
|
| 117 |
|
| 118 |
/**
|
| 119 |
-
*
|
|
|
|
| 120 |
* @category remote
|
|
|
|
| 121 |
*/
|
| 122 |
export type RemoteId = Uint8Array & Brand<RemoteIdTypeId>
|
| 123 |
|
| 124 |
/**
|
| 125 |
-
*
|
|
|
|
| 126 |
* @category remote
|
|
|
|
| 127 |
*/
|
| 128 |
export const RemoteId = Schema.Uint8Array.pipe(Schema.brand(RemoteIdTypeId))
|
| 129 |
|
| 130 |
/**
|
| 131 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
* @category remote
|
|
|
|
| 133 |
*/
|
| 134 |
export const makeRemoteIdUnsafe = (): RemoteId => Uuid.v4({}, new globalThis.Uint8Array(16)) as RemoteId
|
| 135 |
|
| 136 |
/**
|
|
|
|
|
|
|
|
|
|
| 137 |
* @since 4.0.0
|
| 138 |
-
* @category entry
|
| 139 |
*/
|
| 140 |
export const EntryIdTypeId: EntryIdTypeId = "effect/eventlog/EventJournal/EntryId"
|
| 141 |
|
| 142 |
/**
|
|
|
|
|
|
|
|
|
|
| 143 |
* @since 4.0.0
|
| 144 |
-
* @category entry
|
| 145 |
*/
|
| 146 |
export type EntryIdTypeId = "effect/eventlog/EventJournal/EntryId"
|
| 147 |
|
| 148 |
/**
|
| 149 |
-
*
|
|
|
|
| 150 |
* @category entry
|
|
|
|
| 151 |
*/
|
| 152 |
export type EntryId = Uint8Array<ArrayBuffer> & Brand<EntryIdTypeId>
|
| 153 |
|
| 154 |
/**
|
| 155 |
-
*
|
|
|
|
| 156 |
* @category entry
|
|
|
|
| 157 |
*/
|
| 158 |
export const EntryId = (Schema.Uint8Array as Schema.instanceOf<Uint8Array<ArrayBuffer>>).pipe(
|
| 159 |
Schema.brand(EntryIdTypeId)
|
| 160 |
)
|
| 161 |
|
| 162 |
/**
|
| 163 |
-
*
|
|
|
|
| 164 |
* @category entry
|
|
|
|
| 165 |
*/
|
| 166 |
export const EntryIdOrder = Order.make<EntryId>((a, b) => {
|
| 167 |
for (let i = 0; i < 16; i++) {
|
|
@@ -173,15 +230,30 @@ export const EntryIdOrder = Order.make<EntryId>((a, b) => {
|
|
| 173 |
})
|
| 174 |
|
| 175 |
/**
|
| 176 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
* @category entry
|
|
|
|
| 178 |
*/
|
| 179 |
export const makeEntryIdUnsafe = (options: { msecs?: number } = {}): EntryId =>
|
| 180 |
Uuid.v7(options, new globalThis.Uint8Array(16)) as EntryId
|
| 181 |
|
| 182 |
/**
|
| 183 |
-
*
|
|
|
|
| 184 |
* @category entry
|
|
|
|
| 185 |
*/
|
| 186 |
export const entryIdMillis = (entryId: EntryId): number => {
|
| 187 |
const bytes = new Uint8Array(8)
|
|
@@ -190,8 +262,15 @@ export const entryIdMillis = (entryId: EntryId): number => {
|
|
| 190 |
}
|
| 191 |
|
| 192 |
/**
|
| 193 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
* @category entry
|
|
|
|
| 195 |
*/
|
| 196 |
export class Entry extends Schema.Class<Entry>("effect/eventlog/EventJournal/Entry")({
|
| 197 |
id: EntryId,
|
|
@@ -200,26 +279,36 @@ export class Entry extends Schema.Class<Entry>("effect/eventlog/EventJournal/Ent
|
|
| 200 |
payload: Schema.Uint8Array
|
| 201 |
}) {
|
| 202 |
/**
|
|
|
|
|
|
|
| 203 |
* @since 4.0.0
|
| 204 |
*/
|
| 205 |
static arrayMsgpack = Schema.Array(Msgpack.schema(Entry))
|
| 206 |
|
| 207 |
/**
|
|
|
|
|
|
|
| 208 |
* @since 4.0.0
|
| 209 |
*/
|
| 210 |
static encodeArray = Schema.encodeUnknownEffect(Entry.arrayMsgpack)
|
| 211 |
|
| 212 |
/**
|
|
|
|
|
|
|
| 213 |
* @since 4.0.0
|
| 214 |
*/
|
| 215 |
static decodeArray = Schema.decodeUnknownEffect(Entry.arrayMsgpack)
|
| 216 |
|
| 217 |
/**
|
|
|
|
|
|
|
| 218 |
* @since 4.0.0
|
| 219 |
*/
|
| 220 |
static Order = Order.make<Entry>((a, b) => EntryIdOrder(a.id, b.id))
|
| 221 |
|
| 222 |
/**
|
|
|
|
|
|
|
| 223 |
* @since 4.0.0
|
| 224 |
*/
|
| 225 |
get idString(): string {
|
|
@@ -227,6 +316,8 @@ export class Entry extends Schema.Class<Entry>("effect/eventlog/EventJournal/Ent
|
|
| 227 |
}
|
| 228 |
|
| 229 |
/**
|
|
|
|
|
|
|
| 230 |
* @since 4.0.0
|
| 231 |
*/
|
| 232 |
get createdAtMillis(): number {
|
|
@@ -234,6 +325,8 @@ export class Entry extends Schema.Class<Entry>("effect/eventlog/EventJournal/Ent
|
|
| 234 |
}
|
| 235 |
|
| 236 |
/**
|
|
|
|
|
|
|
| 237 |
* @since 4.0.0
|
| 238 |
*/
|
| 239 |
get createdAt(): DateTime.Utc {
|
|
@@ -242,8 +335,14 @@ export class Entry extends Schema.Class<Entry>("effect/eventlog/EventJournal/Ent
|
|
| 242 |
}
|
| 243 |
|
| 244 |
/**
|
| 245 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
* @category entry
|
|
|
|
| 247 |
*/
|
| 248 |
export class RemoteEntry extends Schema.Class<RemoteEntry>("effect/eventlog/EventJournal/RemoteEntry")({
|
| 249 |
remoteSequence: Schema.Number,
|
|
@@ -251,8 +350,15 @@ export class RemoteEntry extends Schema.Class<RemoteEntry>("effect/eventlog/Even
|
|
| 251 |
}) {}
|
| 252 |
|
| 253 |
/**
|
| 254 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
* @category memory
|
|
|
|
| 256 |
*/
|
| 257 |
export const makeMemory: Effect.Effect<EventJournal["Service"]> = Effect.gen(function*() {
|
| 258 |
const journal: Array<Entry> = []
|
|
@@ -384,14 +490,29 @@ export const makeMemory: Effect.Effect<EventJournal["Service"]> = Effect.gen(fun
|
|
| 384 |
})
|
| 385 |
|
| 386 |
/**
|
| 387 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 388 |
* @category memory
|
|
|
|
| 389 |
*/
|
| 390 |
export const layerMemory: Layer.Layer<EventJournal> = Layer.effect(EventJournal, makeMemory)
|
| 391 |
|
| 392 |
/**
|
| 393 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 394 |
* @category indexed db
|
|
|
|
| 395 |
*/
|
| 396 |
export const makeIndexedDb = (options?: {
|
| 397 |
readonly database?: string
|
|
@@ -648,8 +769,11 @@ const EntryIdbArray = Schema.Array(Entry)
|
|
| 648 |
const decodeEntryIdbArray = Schema.decodeUnknownEffect(EntryIdbArray)
|
| 649 |
|
| 650 |
/**
|
| 651 |
-
*
|
|
|
|
|
|
|
| 652 |
* @category indexed db
|
|
|
|
| 653 |
*/
|
| 654 |
export const layerIndexedDb = (options?: {
|
| 655 |
readonly database?: string
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Stores event-log entries and replication state.
|
| 3 |
+
*
|
| 4 |
+
* `EventJournal` records committed entries, exposes them for replay, publishes
|
| 5 |
+
* local changes, and tracks the remote metadata needed to exchange entries with
|
| 6 |
+
* other journals. Higher-level event-log schemas and handlers use this service
|
| 7 |
+
* to rebuild projections, sync offline clients, import remote changes, and
|
| 8 |
+
* coordinate writes per store. This module also defines journal errors, entry
|
| 9 |
+
* and remote identifiers, schemas, and in-memory or IndexedDB-backed journal
|
| 10 |
+
* layers.
|
| 11 |
+
*
|
| 12 |
* @since 4.0.0
|
| 13 |
*/
|
| 14 |
import * as Uuid from "uuid"
|
|
|
|
| 27 |
import type { StoreId } from "./EventLogMessage.ts"
|
| 28 |
|
| 29 |
/**
|
| 30 |
+
* Context service for storing and replaying event journal entries.
|
| 31 |
+
*
|
| 32 |
+
* **Details**
|
| 33 |
+
*
|
| 34 |
+
* The service writes local entries, imports entries from remote journals, exposes
|
| 35 |
+
* a stream of local changes, and provides per-store locking.
|
| 36 |
+
*
|
| 37 |
* @category context
|
| 38 |
+
* @since 4.0.0
|
| 39 |
*/
|
| 40 |
export class EventJournal extends Context.Service<EventJournal, {
|
| 41 |
/**
|
|
|
|
| 57 |
/**
|
| 58 |
* Write events from a remote source to the journal.
|
| 59 |
*
|
| 60 |
+
* **Details**
|
| 61 |
+
*
|
| 62 |
* Effects run sequentially in compaction bracket order.
|
| 63 |
*/
|
| 64 |
readonly writeFromRemote: (
|
|
|
|
| 109 |
const TypeId = "effect/eventlog/EventJournal/EventJournalError" as const
|
| 110 |
|
| 111 |
/**
|
| 112 |
+
* Error raised by event journal operations.
|
| 113 |
+
*
|
| 114 |
+
* **Details**
|
| 115 |
+
*
|
| 116 |
+
* The error records the journal method that failed and the underlying cause.
|
| 117 |
+
*
|
| 118 |
* @category errors
|
| 119 |
+
* @since 4.0.0
|
| 120 |
*/
|
| 121 |
export class EventJournalError extends Data.TaggedError("EventJournalError")<{
|
| 122 |
readonly method: string
|
| 123 |
readonly cause: unknown
|
| 124 |
}> {
|
| 125 |
/**
|
| 126 |
+
* Marks this value as an event journal error for runtime guards.
|
| 127 |
+
*
|
| 128 |
* @since 4.0.0
|
| 129 |
*/
|
| 130 |
readonly [TypeId] = TypeId
|
| 131 |
}
|
| 132 |
|
| 133 |
/**
|
| 134 |
+
* Brand identifier used for `RemoteId` values.
|
| 135 |
+
*
|
| 136 |
+
* @category type IDs
|
| 137 |
* @since 4.0.0
|
|
|
|
| 138 |
*/
|
| 139 |
export type RemoteIdTypeId = "effect/eventlog/EventJournal/RemoteId"
|
| 140 |
|
| 141 |
/**
|
| 142 |
+
* Runtime brand identifier used for `RemoteId` values.
|
| 143 |
+
*
|
| 144 |
+
* @category type IDs
|
| 145 |
* @since 4.0.0
|
|
|
|
| 146 |
*/
|
| 147 |
export const RemoteIdTypeId: RemoteIdTypeId = "effect/eventlog/EventJournal/RemoteId"
|
| 148 |
|
| 149 |
/**
|
| 150 |
+
* Branded byte identifier for a remote event journal source.
|
| 151 |
+
*
|
| 152 |
* @category remote
|
| 153 |
+
* @since 4.0.0
|
| 154 |
*/
|
| 155 |
export type RemoteId = Uint8Array & Brand<RemoteIdTypeId>
|
| 156 |
|
| 157 |
/**
|
| 158 |
+
* Schema for branded remote event journal identifiers.
|
| 159 |
+
*
|
| 160 |
* @category remote
|
| 161 |
+
* @since 4.0.0
|
| 162 |
*/
|
| 163 |
export const RemoteId = Schema.Uint8Array.pipe(Schema.brand(RemoteIdTypeId))
|
| 164 |
|
| 165 |
/**
|
| 166 |
+
* Generates a new random `RemoteId`.
|
| 167 |
+
*
|
| 168 |
+
* **When to use**
|
| 169 |
+
*
|
| 170 |
+
* Use when generating a fresh event-log remote id internally and the UUID bytes
|
| 171 |
+
* are trusted to satisfy the brand.
|
| 172 |
+
*
|
| 173 |
+
* **Gotchas**
|
| 174 |
+
*
|
| 175 |
+
* This is unsafe because the generated UUID bytes are cast to the brand without
|
| 176 |
+
* schema validation.
|
| 177 |
+
*
|
| 178 |
* @category remote
|
| 179 |
+
* @since 4.0.0
|
| 180 |
*/
|
| 181 |
export const makeRemoteIdUnsafe = (): RemoteId => Uuid.v4({}, new globalThis.Uint8Array(16)) as RemoteId
|
| 182 |
|
| 183 |
/**
|
| 184 |
+
* Runtime brand identifier used for `EntryId` values.
|
| 185 |
+
*
|
| 186 |
+
* @category type IDs
|
| 187 |
* @since 4.0.0
|
|
|
|
| 188 |
*/
|
| 189 |
export const EntryIdTypeId: EntryIdTypeId = "effect/eventlog/EventJournal/EntryId"
|
| 190 |
|
| 191 |
/**
|
| 192 |
+
* Brand identifier used for `EntryId` values.
|
| 193 |
+
*
|
| 194 |
+
* @category type IDs
|
| 195 |
* @since 4.0.0
|
|
|
|
| 196 |
*/
|
| 197 |
export type EntryIdTypeId = "effect/eventlog/EventJournal/EntryId"
|
| 198 |
|
| 199 |
/**
|
| 200 |
+
* Branded byte identifier for an event journal entry.
|
| 201 |
+
*
|
| 202 |
* @category entry
|
| 203 |
+
* @since 4.0.0
|
| 204 |
*/
|
| 205 |
export type EntryId = Uint8Array<ArrayBuffer> & Brand<EntryIdTypeId>
|
| 206 |
|
| 207 |
/**
|
| 208 |
+
* Schema for branded event journal entry identifiers.
|
| 209 |
+
*
|
| 210 |
* @category entry
|
| 211 |
+
* @since 4.0.0
|
| 212 |
*/
|
| 213 |
export const EntryId = (Schema.Uint8Array as Schema.instanceOf<Uint8Array<ArrayBuffer>>).pipe(
|
| 214 |
Schema.brand(EntryIdTypeId)
|
| 215 |
)
|
| 216 |
|
| 217 |
/**
|
| 218 |
+
* Provides an Ordering instance for entry identifiers based on their raw UUID bytes.
|
| 219 |
+
*
|
| 220 |
* @category entry
|
| 221 |
+
* @since 4.0.0
|
| 222 |
*/
|
| 223 |
export const EntryIdOrder = Order.make<EntryId>((a, b) => {
|
| 224 |
for (let i = 0; i < 16; i++) {
|
|
|
|
| 230 |
})
|
| 231 |
|
| 232 |
/**
|
| 233 |
+
* Generates a UUID v7 `EntryId`, optionally using the supplied millisecond
|
| 234 |
+
* timestamp.
|
| 235 |
+
*
|
| 236 |
+
* **When to use**
|
| 237 |
+
*
|
| 238 |
+
* Use when generating an event-log entry id internally and the UUID v7 bytes
|
| 239 |
+
* are trusted to satisfy the brand.
|
| 240 |
+
*
|
| 241 |
+
* **Gotchas**
|
| 242 |
+
*
|
| 243 |
+
* This is unsafe because the generated UUID bytes are cast to the brand without
|
| 244 |
+
* schema validation.
|
| 245 |
+
*
|
| 246 |
* @category entry
|
| 247 |
+
* @since 4.0.0
|
| 248 |
*/
|
| 249 |
export const makeEntryIdUnsafe = (options: { msecs?: number } = {}): EntryId =>
|
| 250 |
Uuid.v7(options, new globalThis.Uint8Array(16)) as EntryId
|
| 251 |
|
| 252 |
/**
|
| 253 |
+
* Extracts the millisecond timestamp encoded in a UUID v7 `EntryId`.
|
| 254 |
+
*
|
| 255 |
* @category entry
|
| 256 |
+
* @since 4.0.0
|
| 257 |
*/
|
| 258 |
export const entryIdMillis = (entryId: EntryId): number => {
|
| 259 |
const bytes = new Uint8Array(8)
|
|
|
|
| 262 |
}
|
| 263 |
|
| 264 |
/**
|
| 265 |
+
* Schema for a committed event journal entry.
|
| 266 |
+
*
|
| 267 |
+
* **Details**
|
| 268 |
+
*
|
| 269 |
+
* An entry records its ID, event tag, primary key, and MessagePack-encoded
|
| 270 |
+
* payload, with helpers for array MessagePack encoding and creation timestamps.
|
| 271 |
+
*
|
| 272 |
* @category entry
|
| 273 |
+
* @since 4.0.0
|
| 274 |
*/
|
| 275 |
export class Entry extends Schema.Class<Entry>("effect/eventlog/EventJournal/Entry")({
|
| 276 |
id: EntryId,
|
|
|
|
| 279 |
payload: Schema.Uint8Array
|
| 280 |
}) {
|
| 281 |
/**
|
| 282 |
+
* MessagePack schema for arrays of committed event journal entries.
|
| 283 |
+
*
|
| 284 |
* @since 4.0.0
|
| 285 |
*/
|
| 286 |
static arrayMsgpack = Schema.Array(Msgpack.schema(Entry))
|
| 287 |
|
| 288 |
/**
|
| 289 |
+
* Encodes arrays of committed entries with the MessagePack entry schema.
|
| 290 |
+
*
|
| 291 |
* @since 4.0.0
|
| 292 |
*/
|
| 293 |
static encodeArray = Schema.encodeUnknownEffect(Entry.arrayMsgpack)
|
| 294 |
|
| 295 |
/**
|
| 296 |
+
* Decodes arrays of committed entries with the MessagePack entry schema.
|
| 297 |
+
*
|
| 298 |
* @since 4.0.0
|
| 299 |
*/
|
| 300 |
static decodeArray = Schema.decodeUnknownEffect(Entry.arrayMsgpack)
|
| 301 |
|
| 302 |
/**
|
| 303 |
+
* Ordering for committed entries by their event journal entry id.
|
| 304 |
+
*
|
| 305 |
* @since 4.0.0
|
| 306 |
*/
|
| 307 |
static Order = Order.make<Entry>((a, b) => EntryIdOrder(a.id, b.id))
|
| 308 |
|
| 309 |
/**
|
| 310 |
+
* String representation of the entry id.
|
| 311 |
+
*
|
| 312 |
* @since 4.0.0
|
| 313 |
*/
|
| 314 |
get idString(): string {
|
|
|
|
| 316 |
}
|
| 317 |
|
| 318 |
/**
|
| 319 |
+
* Creation timestamp encoded in the UUID v7 entry id, in epoch milliseconds.
|
| 320 |
+
*
|
| 321 |
* @since 4.0.0
|
| 322 |
*/
|
| 323 |
get createdAtMillis(): number {
|
|
|
|
| 325 |
}
|
| 326 |
|
| 327 |
/**
|
| 328 |
+
* Creation timestamp encoded in the UUID v7 entry id, as a UTC date-time.
|
| 329 |
+
*
|
| 330 |
* @since 4.0.0
|
| 331 |
*/
|
| 332 |
get createdAt(): DateTime.Utc {
|
|
|
|
| 335 |
}
|
| 336 |
|
| 337 |
/**
|
| 338 |
+
* Schema for an event journal entry received from a remote source.
|
| 339 |
+
*
|
| 340 |
+
* **Details**
|
| 341 |
+
*
|
| 342 |
+
* It pairs the remote sequence number with the journal entry payload.
|
| 343 |
+
*
|
| 344 |
* @category entry
|
| 345 |
+
* @since 4.0.0
|
| 346 |
*/
|
| 347 |
export class RemoteEntry extends Schema.Class<RemoteEntry>("effect/eventlog/EventJournal/RemoteEntry")({
|
| 348 |
remoteSequence: Schema.Number,
|
|
|
|
| 350 |
}) {}
|
| 351 |
|
| 352 |
/**
|
| 353 |
+
* Creates an in-memory `EventJournal` service.
|
| 354 |
+
*
|
| 355 |
+
* **Gotchas**
|
| 356 |
+
*
|
| 357 |
+
* Entries, remote tracking state, and locks live only in the current process and
|
| 358 |
+
* are lost when the service is discarded.
|
| 359 |
+
*
|
| 360 |
* @category memory
|
| 361 |
+
* @since 4.0.0
|
| 362 |
*/
|
| 363 |
export const makeMemory: Effect.Effect<EventJournal["Service"]> = Effect.gen(function*() {
|
| 364 |
const journal: Array<Entry> = []
|
|
|
|
| 490 |
})
|
| 491 |
|
| 492 |
/**
|
| 493 |
+
* Layer that provides an in-memory `EventJournal`.
|
| 494 |
+
*
|
| 495 |
+
* **Gotchas**
|
| 496 |
+
*
|
| 497 |
+
* All journal data is stored in process memory and is not persisted across layer
|
| 498 |
+
* lifetimes.
|
| 499 |
+
*
|
| 500 |
* @category memory
|
| 501 |
+
* @since 4.0.0
|
| 502 |
*/
|
| 503 |
export const layerMemory: Layer.Layer<EventJournal> = Layer.effect(EventJournal, makeMemory)
|
| 504 |
|
| 505 |
/**
|
| 506 |
+
* Creates an `EventJournal` backed by IndexedDB.
|
| 507 |
+
*
|
| 508 |
+
* **Details**
|
| 509 |
+
*
|
| 510 |
+
* The journal stores entries and remote replication metadata in the configured
|
| 511 |
+
* browser database, publishes local changes, and requires `Scope` so the database
|
| 512 |
+
* connection can be closed when the scope ends.
|
| 513 |
+
*
|
| 514 |
* @category indexed db
|
| 515 |
+
* @since 4.0.0
|
| 516 |
*/
|
| 517 |
export const makeIndexedDb = (options?: {
|
| 518 |
readonly database?: string
|
|
|
|
| 769 |
const decodeEntryIdbArray = Schema.decodeUnknownEffect(EntryIdbArray)
|
| 770 |
|
| 771 |
/**
|
| 772 |
+
* Provides `EventJournal` using the IndexedDB-backed implementation created by
|
| 773 |
+
* `makeIndexedDb`.
|
| 774 |
+
*
|
| 775 |
* @category indexed db
|
| 776 |
+
* @since 4.0.0
|
| 777 |
*/
|
| 778 |
export const layerIndexedDb = (options?: {
|
| 779 |
readonly database?: string
|
.opencode/node_modules/effect/src/unstable/eventlog/EventLog.ts
CHANGED
|
@@ -1,4 +1,12 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Context from "../../Context.ts"
|
|
@@ -28,8 +36,16 @@ import { StoreId } from "./EventLogMessage.ts"
|
|
| 28 |
import type { EventLogRemote } from "./EventLogRemote.ts"
|
| 29 |
|
| 30 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
* @since 4.0.0
|
| 32 |
-
* @category tags
|
| 33 |
*/
|
| 34 |
export class EventLog extends Context.Service<EventLog, {
|
| 35 |
readonly write: <Groups extends EventGroup.Any, Tag extends Event.Tag<EventGroup.Events<Groups>>>(options: {
|
|
@@ -45,8 +61,11 @@ export class EventLog extends Context.Service<EventLog, {
|
|
| 45 |
}>()("effect/eventlog/EventLog") {}
|
| 46 |
|
| 47 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
* @since 4.0.0
|
| 49 |
-
* @category Registry
|
| 50 |
*/
|
| 51 |
export class Registry extends Context.Service<Registry, {
|
| 52 |
readonly registerHandlerUnsafe: (options: {
|
|
@@ -81,8 +100,11 @@ export class Registry extends Context.Service<Registry, {
|
|
| 81 |
}>()("effect/unstable/eventlog/EventLog/Registry") {}
|
| 82 |
|
| 83 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
* @since 4.0.0
|
| 85 |
-
* @category Registry
|
| 86 |
*/
|
| 87 |
export const layerRegistry = Layer.effect(
|
| 88 |
Registry,
|
|
@@ -145,8 +167,16 @@ export const layerRegistry = Layer.effect(
|
|
| 145 |
)
|
| 146 |
|
| 147 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
* @since 4.0.0
|
| 149 |
-
* @category models
|
| 150 |
*/
|
| 151 |
export class Identity extends Context.Service<Identity, {
|
| 152 |
readonly publicKey: string
|
|
@@ -154,27 +184,35 @@ export class Identity extends Context.Service<Identity, {
|
|
| 154 |
}>()("effect/eventlog/EventLog/Identity") {}
|
| 155 |
|
| 156 |
/**
|
|
|
|
|
|
|
|
|
|
| 157 |
* @since 4.0.0
|
| 158 |
-
* @category schema
|
| 159 |
*/
|
| 160 |
export type SchemaTypeId = "~effect/eventlog/EventLog/Schema"
|
| 161 |
|
| 162 |
/**
|
|
|
|
|
|
|
|
|
|
| 163 |
* @since 4.0.0
|
| 164 |
-
* @category schema
|
| 165 |
*/
|
| 166 |
export const SchemaTypeId: SchemaTypeId = "~effect/eventlog/EventLog/Schema"
|
| 167 |
|
| 168 |
/**
|
|
|
|
|
|
|
|
|
|
| 169 |
* @since 4.0.0
|
| 170 |
-
* @category schema
|
| 171 |
*/
|
| 172 |
export const isEventLogSchema = (u: unknown): u is EventLogSchema<EventGroup.Any> =>
|
| 173 |
Predicate.hasProperty(u, SchemaTypeId)
|
| 174 |
|
| 175 |
/**
|
|
|
|
|
|
|
|
|
|
| 176 |
* @since 4.0.0
|
| 177 |
-
* @category schema
|
| 178 |
*/
|
| 179 |
export interface EventLogSchema<Groups extends EventGroup.Any> {
|
| 180 |
readonly [SchemaTypeId]: SchemaTypeId
|
|
@@ -182,8 +220,10 @@ export interface EventLogSchema<Groups extends EventGroup.Any> {
|
|
| 182 |
}
|
| 183 |
|
| 184 |
/**
|
|
|
|
|
|
|
|
|
|
| 185 |
* @since 4.0.0
|
| 186 |
-
* @category schema
|
| 187 |
*/
|
| 188 |
export const schema = <Groups extends ReadonlyArray<EventGroup.Any>>(
|
| 189 |
...groups: Groups
|
|
@@ -196,22 +236,32 @@ export const schema = <Groups extends ReadonlyArray<EventGroup.Any>>(
|
|
| 196 |
}
|
| 197 |
|
| 198 |
/**
|
|
|
|
|
|
|
|
|
|
| 199 |
* @since 4.0.0
|
| 200 |
-
* @category handlers
|
| 201 |
*/
|
| 202 |
export type HandlersTypeId = "~effect/eventlog/EventLog/Handlers"
|
| 203 |
|
| 204 |
/**
|
|
|
|
|
|
|
|
|
|
| 205 |
* @since 4.0.0
|
| 206 |
-
* @category handlers
|
| 207 |
*/
|
| 208 |
export const HandlersTypeId: HandlersTypeId = "~effect/eventlog/EventLog/Handlers"
|
| 209 |
|
| 210 |
/**
|
| 211 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
*
|
| 213 |
-
* @since 4.0.0
|
| 214 |
* @category handlers
|
|
|
|
| 215 |
*/
|
| 216 |
export interface Handlers<
|
| 217 |
R,
|
|
@@ -245,21 +295,29 @@ export interface Handlers<
|
|
| 245 |
}
|
| 246 |
|
| 247 |
/**
|
|
|
|
|
|
|
|
|
|
| 248 |
* @since 4.0.0
|
| 249 |
-
* @category handlers
|
| 250 |
*/
|
| 251 |
export declare namespace Handlers {
|
| 252 |
/**
|
| 253 |
-
*
|
|
|
|
|
|
|
| 254 |
* @category handlers
|
|
|
|
| 255 |
*/
|
| 256 |
export interface Any {
|
| 257 |
readonly [HandlersTypeId]: unknown
|
| 258 |
}
|
| 259 |
|
| 260 |
/**
|
| 261 |
-
*
|
|
|
|
|
|
|
| 262 |
* @category handlers
|
|
|
|
| 263 |
*/
|
| 264 |
export type Item<R> = {
|
| 265 |
readonly event: Event.AnyWithProps
|
|
@@ -276,8 +334,15 @@ export declare namespace Handlers {
|
|
| 276 |
}
|
| 277 |
|
| 278 |
/**
|
| 279 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
* @category handlers
|
|
|
|
| 281 |
*/
|
| 282 |
export type ValidateReturn<A> = A extends (
|
| 283 |
| Handlers<
|
|
@@ -297,8 +362,10 @@ export declare namespace Handlers {
|
|
| 297 |
`Must return the implemented handlers`
|
| 298 |
|
| 299 |
/**
|
| 300 |
-
*
|
|
|
|
| 301 |
* @category handlers
|
|
|
|
| 302 |
*/
|
| 303 |
export type Error<A> = A extends Effect.Effect<
|
| 304 |
Handlers<
|
|
@@ -311,8 +378,11 @@ export declare namespace Handlers {
|
|
| 311 |
never
|
| 312 |
|
| 313 |
/**
|
| 314 |
-
*
|
|
|
|
|
|
|
| 315 |
* @category handlers
|
|
|
|
| 316 |
*/
|
| 317 |
export type Services<A> = A extends Handlers<
|
| 318 |
infer _R,
|
|
@@ -330,8 +400,15 @@ export declare namespace Handlers {
|
|
| 330 |
}
|
| 331 |
|
| 332 |
/**
|
| 333 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 334 |
* @category models
|
|
|
|
| 335 |
*/
|
| 336 |
export class CurrentStoreId extends Context.Reference<StoreId>("effect/eventlog/EventLog/CurrentStoreId", {
|
| 337 |
defaultValue: constant(StoreId.make("default"))
|
|
@@ -345,8 +422,11 @@ const RedactedUint8Array = Schema.Uint8ArrayFromBase64.pipe(
|
|
| 345 |
)
|
| 346 |
|
| 347 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 348 |
* @since 4.0.0
|
| 349 |
-
* @category schema
|
| 350 |
*/
|
| 351 |
export const IdentitySchema = Schema.Struct({
|
| 352 |
publicKey: Schema.String,
|
|
@@ -363,8 +443,14 @@ const IdentityStringSchema = Schema.StringFromBase64Url.pipe(
|
|
| 363 |
)
|
| 364 |
|
| 365 |
/**
|
| 366 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 367 |
* @category constructors
|
|
|
|
| 368 |
*/
|
| 369 |
export const decodeIdentityString = (value: string): Identity["Service"] => {
|
| 370 |
const decoded = Schema.decodeUnknownSync(IdentityStringSchema)(value)
|
|
@@ -375,8 +461,11 @@ export const decodeIdentityString = (value: string): Identity["Service"] => {
|
|
| 375 |
}
|
| 376 |
|
| 377 |
/**
|
| 378 |
-
*
|
|
|
|
|
|
|
| 379 |
* @category constructors
|
|
|
|
| 380 |
*/
|
| 381 |
export const encodeIdentityString = (identity: Identity["Service"]): string =>
|
| 382 |
Schema.encodeSync(IdentityStringSchema)({
|
|
@@ -385,8 +474,11 @@ export const encodeIdentityString = (identity: Identity["Service"]): string =>
|
|
| 385 |
})
|
| 386 |
|
| 387 |
/**
|
| 388 |
-
*
|
|
|
|
|
|
|
| 389 |
* @category constructors
|
|
|
|
| 390 |
*/
|
| 391 |
export const makeIdentity: Effect.Effect<Identity["Service"], never, EventLogEncryption.EventLogEncryption> =
|
| 392 |
EventLogEncryption.EventLogEncryption.use((_) => _.generateIdentity)
|
|
@@ -425,8 +517,15 @@ const makeHandlers = (options: {
|
|
| 425 |
}): Handlers<any, any> => Object.assign(Object.create(handlersProto), options)
|
| 426 |
|
| 427 |
/**
|
| 428 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 429 |
* @category handlers
|
|
|
|
| 430 |
*/
|
| 431 |
export const group = <Events extends Event.Any, Return>(
|
| 432 |
group: EventGroup.EventGroup<Events>,
|
|
@@ -455,8 +554,15 @@ export const group = <Events extends Event.Any, Return>(
|
|
| 455 |
) as any
|
| 456 |
|
| 457 |
/**
|
| 458 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 459 |
* @category compaction
|
|
|
|
| 460 |
*/
|
| 461 |
export const groupCompaction = <Events extends Event.Any, R>(
|
| 462 |
group: EventGroup.EventGroup<Events>,
|
|
@@ -546,8 +652,15 @@ export const groupCompaction = <Events extends Event.Any, R>(
|
|
| 546 |
)
|
| 547 |
|
| 548 |
/**
|
| 549 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 550 |
* @category reactivity
|
|
|
|
| 551 |
*/
|
| 552 |
export const groupReactivity = <Events extends Event.Any>(
|
| 553 |
group: EventGroup.EventGroup<Events>,
|
|
@@ -571,8 +684,16 @@ export const groupReactivity = <Events extends Event.Any>(
|
|
| 571 |
)
|
| 572 |
|
| 573 |
/**
|
| 574 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 575 |
* @category handlers
|
|
|
|
| 576 |
*/
|
| 577 |
export const makeReplayFromRemote = (options: {
|
| 578 |
readonly handlers: ReadonlyMap<string, Handlers.Item<any>>
|
|
@@ -816,8 +937,11 @@ const make = Effect.gen(function*() {
|
|
| 816 |
})
|
| 817 |
|
| 818 |
/**
|
| 819 |
-
*
|
|
|
|
|
|
|
| 820 |
* @category layers
|
|
|
|
| 821 |
*/
|
| 822 |
export const layerEventLog: Layer.Layer<EventLog | Registry, never, EventJournal | Identity> = Layer.effect(
|
| 823 |
EventLog,
|
|
@@ -828,8 +952,33 @@ export const layerEventLog: Layer.Layer<EventLog | Registry, never, EventJournal
|
|
| 828 |
)
|
| 829 |
|
| 830 |
/**
|
| 831 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 832 |
* @category layers
|
|
|
|
| 833 |
*/
|
| 834 |
export const layer = <Groups extends EventGroup.Any, E, R>(
|
| 835 |
_schema: EventLogSchema<Groups>,
|
|
@@ -844,8 +993,16 @@ export const layer = <Groups extends EventGroup.Any, E, R>(
|
|
| 844 |
)
|
| 845 |
|
| 846 |
/**
|
| 847 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 848 |
* @category client
|
|
|
|
| 849 |
*/
|
| 850 |
export const makeClient = <Groups extends EventGroup.Any>(
|
| 851 |
schema: EventLogSchema<Groups>
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Runtime for writing typed events to an event journal.
|
| 3 |
+
*
|
| 4 |
+
* `EventLog` combines event groups, handlers, a journal, local identity,
|
| 5 |
+
* optional remote replicas, and reactivity hooks. Writers send typed payloads
|
| 6 |
+
* through a client; the matching handler runs first, and the journal entry is
|
| 7 |
+
* committed only after the handler succeeds. This module also contains the
|
| 8 |
+
* layers and helpers needed to assemble that runtime.
|
| 9 |
+
*
|
| 10 |
* @since 4.0.0
|
| 11 |
*/
|
| 12 |
import * as Context from "../../Context.ts"
|
|
|
|
| 36 |
import type { EventLogRemote } from "./EventLogRemote.ts"
|
| 37 |
|
| 38 |
/**
|
| 39 |
+
* Service for writing typed event-log events through registered handlers.
|
| 40 |
+
*
|
| 41 |
+
* **Details**
|
| 42 |
+
*
|
| 43 |
+
* `write` encodes the event payload, runs the matching handler, commits the entry
|
| 44 |
+
* only when the handler succeeds, and exposes access to the underlying journal
|
| 45 |
+
* entries and destroy operation.
|
| 46 |
+
*
|
| 47 |
+
* @category services
|
| 48 |
* @since 4.0.0
|
|
|
|
| 49 |
*/
|
| 50 |
export class EventLog extends Context.Service<EventLog, {
|
| 51 |
readonly write: <Groups extends EventGroup.Any, Tag extends Event.Tag<EventGroup.Events<Groups>>>(options: {
|
|
|
|
| 61 |
}>()("effect/eventlog/EventLog") {}
|
| 62 |
|
| 63 |
/**
|
| 64 |
+
* Service that collects event handlers, compaction handlers, remote replicas,
|
| 65 |
+
* and reactivity invalidation keys.
|
| 66 |
+
*
|
| 67 |
+
* @category services
|
| 68 |
* @since 4.0.0
|
|
|
|
| 69 |
*/
|
| 70 |
export class Registry extends Context.Service<Registry, {
|
| 71 |
readonly registerHandlerUnsafe: (options: {
|
|
|
|
| 100 |
}>()("effect/unstable/eventlog/EventLog/Registry") {}
|
| 101 |
|
| 102 |
/**
|
| 103 |
+
* Provides an in-memory `Registry` for event handlers, compactors, remote
|
| 104 |
+
* replicas, and reactivity keys.
|
| 105 |
+
*
|
| 106 |
+
* @category layers
|
| 107 |
* @since 4.0.0
|
|
|
|
| 108 |
*/
|
| 109 |
export const layerRegistry = Layer.effect(
|
| 110 |
Registry,
|
|
|
|
| 167 |
)
|
| 168 |
|
| 169 |
/**
|
| 170 |
+
* Context service for an event-log identity containing a public key and redacted
|
| 171 |
+
* private key material.
|
| 172 |
+
*
|
| 173 |
+
* **Details**
|
| 174 |
+
*
|
| 175 |
+
* The identity is used by remote replication for authentication and by the
|
| 176 |
+
* encryption service to derive signing and encryption keys.
|
| 177 |
+
*
|
| 178 |
+
* @category services
|
| 179 |
* @since 4.0.0
|
|
|
|
| 180 |
*/
|
| 181 |
export class Identity extends Context.Service<Identity, {
|
| 182 |
readonly publicKey: string
|
|
|
|
| 184 |
}>()("effect/eventlog/EventLog/Identity") {}
|
| 185 |
|
| 186 |
/**
|
| 187 |
+
* Type-level identifier used to brand `EventLogSchema` values.
|
| 188 |
+
*
|
| 189 |
+
* @category type IDs
|
| 190 |
* @since 4.0.0
|
|
|
|
| 191 |
*/
|
| 192 |
export type SchemaTypeId = "~effect/eventlog/EventLog/Schema"
|
| 193 |
|
| 194 |
/**
|
| 195 |
+
* Runtime property key used to identify `EventLogSchema` values.
|
| 196 |
+
*
|
| 197 |
+
* @category type IDs
|
| 198 |
* @since 4.0.0
|
|
|
|
| 199 |
*/
|
| 200 |
export const SchemaTypeId: SchemaTypeId = "~effect/eventlog/EventLog/Schema"
|
| 201 |
|
| 202 |
/**
|
| 203 |
+
* Returns `true` when a value carries the `EventLogSchema` marker.
|
| 204 |
+
*
|
| 205 |
+
* @category schemas
|
| 206 |
* @since 4.0.0
|
|
|
|
| 207 |
*/
|
| 208 |
export const isEventLogSchema = (u: unknown): u is EventLogSchema<EventGroup.Any> =>
|
| 209 |
Predicate.hasProperty(u, SchemaTypeId)
|
| 210 |
|
| 211 |
/**
|
| 212 |
+
* Schema describing the event groups that can be written through an `EventLog`.
|
| 213 |
+
*
|
| 214 |
+
* @category schemas
|
| 215 |
* @since 4.0.0
|
|
|
|
| 216 |
*/
|
| 217 |
export interface EventLogSchema<Groups extends EventGroup.Any> {
|
| 218 |
readonly [SchemaTypeId]: SchemaTypeId
|
|
|
|
| 220 |
}
|
| 221 |
|
| 222 |
/**
|
| 223 |
+
* Creates an `EventLogSchema` from one or more event groups.
|
| 224 |
+
*
|
| 225 |
+
* @category schemas
|
| 226 |
* @since 4.0.0
|
|
|
|
| 227 |
*/
|
| 228 |
export const schema = <Groups extends ReadonlyArray<EventGroup.Any>>(
|
| 229 |
...groups: Groups
|
|
|
|
| 236 |
}
|
| 237 |
|
| 238 |
/**
|
| 239 |
+
* Type-level identifier used to brand `Handlers` values.
|
| 240 |
+
*
|
| 241 |
+
* @category type IDs
|
| 242 |
* @since 4.0.0
|
|
|
|
| 243 |
*/
|
| 244 |
export type HandlersTypeId = "~effect/eventlog/EventLog/Handlers"
|
| 245 |
|
| 246 |
/**
|
| 247 |
+
* Runtime property key used to identify `Handlers` values.
|
| 248 |
+
*
|
| 249 |
+
* @category type IDs
|
| 250 |
* @since 4.0.0
|
|
|
|
| 251 |
*/
|
| 252 |
export const HandlersTypeId: HandlersTypeId = "~effect/eventlog/EventLog/Handlers"
|
| 253 |
|
| 254 |
/**
|
| 255 |
+
* Builder for the handlers associated with an `EventGroup`.
|
| 256 |
+
*
|
| 257 |
+
* **Details**
|
| 258 |
+
*
|
| 259 |
+
* The `Events` type parameter tracks the event tags that still need handlers, and
|
| 260 |
+
* each call to `handle` records a handler while accumulating any required
|
| 261 |
+
* services.
|
| 262 |
*
|
|
|
|
| 263 |
* @category handlers
|
| 264 |
+
* @since 4.0.0
|
| 265 |
*/
|
| 266 |
export interface Handlers<
|
| 267 |
R,
|
|
|
|
| 295 |
}
|
| 296 |
|
| 297 |
/**
|
| 298 |
+
* Namespace containing helper types for `Handlers` values and handler-producing
|
| 299 |
+
* layers.
|
| 300 |
+
*
|
| 301 |
* @since 4.0.0
|
|
|
|
| 302 |
*/
|
| 303 |
export declare namespace Handlers {
|
| 304 |
/**
|
| 305 |
+
* Type that matches any `Handlers` value regardless of its services or remaining
|
| 306 |
+
* events.
|
| 307 |
+
*
|
| 308 |
* @category handlers
|
| 309 |
+
* @since 4.0.0
|
| 310 |
*/
|
| 311 |
export interface Any {
|
| 312 |
readonly [HandlersTypeId]: unknown
|
| 313 |
}
|
| 314 |
|
| 315 |
/**
|
| 316 |
+
* Runtime representation of one registered event handler, including its event
|
| 317 |
+
* metadata, captured context, and handler function.
|
| 318 |
+
*
|
| 319 |
* @category handlers
|
| 320 |
+
* @since 4.0.0
|
| 321 |
*/
|
| 322 |
export type Item<R> = {
|
| 323 |
readonly event: Event.AnyWithProps
|
|
|
|
| 334 |
}
|
| 335 |
|
| 336 |
/**
|
| 337 |
+
* Validates that a handler builder returned all required handlers.
|
| 338 |
+
*
|
| 339 |
+
* **Details**
|
| 340 |
+
*
|
| 341 |
+
* If any event tag remains unhandled, the type evaluates to an explanatory
|
| 342 |
+
* compile-time error string.
|
| 343 |
+
*
|
| 344 |
* @category handlers
|
| 345 |
+
* @since 4.0.0
|
| 346 |
*/
|
| 347 |
export type ValidateReturn<A> = A extends (
|
| 348 |
| Handlers<
|
|
|
|
| 362 |
`Must return the implemented handlers`
|
| 363 |
|
| 364 |
/**
|
| 365 |
+
* Extracts the error type from an effect that produces `Handlers`.
|
| 366 |
+
*
|
| 367 |
* @category handlers
|
| 368 |
+
* @since 4.0.0
|
| 369 |
*/
|
| 370 |
export type Error<A> = A extends Effect.Effect<
|
| 371 |
Handlers<
|
|
|
|
| 378 |
never
|
| 379 |
|
| 380 |
/**
|
| 381 |
+
* Computes the services required by a `Handlers` value or by an effect that
|
| 382 |
+
* produces one, including event schema services.
|
| 383 |
+
*
|
| 384 |
* @category handlers
|
| 385 |
+
* @since 4.0.0
|
| 386 |
*/
|
| 387 |
export type Services<A> = A extends Handlers<
|
| 388 |
infer _R,
|
|
|
|
| 400 |
}
|
| 401 |
|
| 402 |
/**
|
| 403 |
+
* Context reference for the store id used by event-log writes and remote
|
| 404 |
+
* replication.
|
| 405 |
+
*
|
| 406 |
+
* **Details**
|
| 407 |
+
*
|
| 408 |
+
* Defaults to the branded store id `"default"`.
|
| 409 |
+
*
|
| 410 |
* @category models
|
| 411 |
+
* @since 4.0.0
|
| 412 |
*/
|
| 413 |
export class CurrentStoreId extends Context.Reference<StoreId>("effect/eventlog/EventLog/CurrentStoreId", {
|
| 414 |
defaultValue: constant(StoreId.make("default"))
|
|
|
|
| 422 |
)
|
| 423 |
|
| 424 |
/**
|
| 425 |
+
* Schema for an event-log identity with a string public key and redacted
|
| 426 |
+
* base64-encoded private key bytes.
|
| 427 |
+
*
|
| 428 |
+
* @category schemas
|
| 429 |
* @since 4.0.0
|
|
|
|
| 430 |
*/
|
| 431 |
export const IdentitySchema = Schema.Struct({
|
| 432 |
publicKey: Schema.String,
|
|
|
|
| 443 |
)
|
| 444 |
|
| 445 |
/**
|
| 446 |
+
* Decodes a base64url identity string produced by `encodeIdentityString`.
|
| 447 |
+
*
|
| 448 |
+
* **Gotchas**
|
| 449 |
+
*
|
| 450 |
+
* Invalid input throws a schema decoding error.
|
| 451 |
+
*
|
| 452 |
* @category constructors
|
| 453 |
+
* @since 4.0.0
|
| 454 |
*/
|
| 455 |
export const decodeIdentityString = (value: string): Identity["Service"] => {
|
| 456 |
const decoded = Schema.decodeUnknownSync(IdentityStringSchema)(value)
|
|
|
|
| 461 |
}
|
| 462 |
|
| 463 |
/**
|
| 464 |
+
* Encodes an event-log identity as a base64url string containing the public key
|
| 465 |
+
* and private key bytes.
|
| 466 |
+
*
|
| 467 |
* @category constructors
|
| 468 |
+
* @since 4.0.0
|
| 469 |
*/
|
| 470 |
export const encodeIdentityString = (identity: Identity["Service"]): string =>
|
| 471 |
Schema.encodeSync(IdentityStringSchema)({
|
|
|
|
| 474 |
})
|
| 475 |
|
| 476 |
/**
|
| 477 |
+
* Generates a new event-log identity using the configured
|
| 478 |
+
* `EventLogEncryption` service.
|
| 479 |
+
*
|
| 480 |
* @category constructors
|
| 481 |
+
* @since 4.0.0
|
| 482 |
*/
|
| 483 |
export const makeIdentity: Effect.Effect<Identity["Service"], never, EventLogEncryption.EventLogEncryption> =
|
| 484 |
EventLogEncryption.EventLogEncryption.use((_) => _.generateIdentity)
|
|
|
|
| 517 |
}): Handlers<any, any> => Object.assign(Object.create(handlersProto), options)
|
| 518 |
|
| 519 |
/**
|
| 520 |
+
* Creates a layer that registers handlers for every event in an event group.
|
| 521 |
+
*
|
| 522 |
+
* **Details**
|
| 523 |
+
*
|
| 524 |
+
* The callback receives a `Handlers` builder; its return type is checked so every
|
| 525 |
+
* event in the group is handled.
|
| 526 |
+
*
|
| 527 |
* @category handlers
|
| 528 |
+
* @since 4.0.0
|
| 529 |
*/
|
| 530 |
export const group = <Events extends Event.Any, Return>(
|
| 531 |
group: EventGroup.EventGroup<Events>,
|
|
|
|
| 554 |
) as any
|
| 555 |
|
| 556 |
/**
|
| 557 |
+
* Registers a compaction handler for an event group.
|
| 558 |
+
*
|
| 559 |
+
* **Details**
|
| 560 |
+
*
|
| 561 |
+
* During remote replay, matching entries are decoded, grouped by primary key, and
|
| 562 |
+
* passed to the compaction effect, which may write replacement entries.
|
| 563 |
+
*
|
| 564 |
* @category compaction
|
| 565 |
+
* @since 4.0.0
|
| 566 |
*/
|
| 567 |
export const groupCompaction = <Events extends Event.Any, R>(
|
| 568 |
group: EventGroup.EventGroup<Events>,
|
|
|
|
| 652 |
)
|
| 653 |
|
| 654 |
/**
|
| 655 |
+
* Registers reactivity keys to invalidate when events from a group are written or
|
| 656 |
+
* replayed.
|
| 657 |
+
*
|
| 658 |
+
* **Details**
|
| 659 |
+
*
|
| 660 |
+
* Pass a single key list for all events or a mapping from event tag to key list.
|
| 661 |
+
*
|
| 662 |
* @category reactivity
|
| 663 |
+
* @since 4.0.0
|
| 664 |
*/
|
| 665 |
export const groupReactivity = <Events extends Event.Any>(
|
| 666 |
group: EventGroup.EventGroup<Events>,
|
|
|
|
| 684 |
)
|
| 685 |
|
| 686 |
/**
|
| 687 |
+
* Builds the effect used to replay entries received from a remote event log.
|
| 688 |
+
*
|
| 689 |
+
* **Details**
|
| 690 |
+
*
|
| 691 |
+
* The returned handler decodes the entry and conflicts with the registered event
|
| 692 |
+
* schema, runs the matching handler with the supplied identity and store id, logs
|
| 693 |
+
* failures, and invalidates configured reactivity keys.
|
| 694 |
+
*
|
| 695 |
* @category handlers
|
| 696 |
+
* @since 4.0.0
|
| 697 |
*/
|
| 698 |
export const makeReplayFromRemote = (options: {
|
| 699 |
readonly handlers: ReadonlyMap<string, Handlers.Item<any>>
|
|
|
|
| 937 |
})
|
| 938 |
|
| 939 |
/**
|
| 940 |
+
* Provides `EventLog` and `Registry` using the configured `EventJournal` and
|
| 941 |
+
* `Identity`.
|
| 942 |
+
*
|
| 943 |
* @category layers
|
| 944 |
+
* @since 4.0.0
|
| 945 |
*/
|
| 946 |
export const layerEventLog: Layer.Layer<EventLog | Registry, never, EventJournal | Identity> = Layer.effect(
|
| 947 |
EventLog,
|
|
|
|
| 952 |
)
|
| 953 |
|
| 954 |
/**
|
| 955 |
+
* Combines event-group handler layers with the `EventLog` runtime for a schema.
|
| 956 |
+
*
|
| 957 |
+
* **When to use**
|
| 958 |
+
*
|
| 959 |
+
* Use when you need one layer that installs the shared `EventLog` runtime for
|
| 960 |
+
* an `EventLogSchema` and registers an event-group handler layer for typed
|
| 961 |
+
* writes.
|
| 962 |
+
*
|
| 963 |
+
* **Details**
|
| 964 |
+
*
|
| 965 |
+
* The supplied handler layer is provided with `layerEventLog`. The returned
|
| 966 |
+
* layer provides `EventLog | Registry`, preserves the handler layer's error
|
| 967 |
+
* type, and still requires its remaining services plus `EventJournal` and
|
| 968 |
+
* `Identity`.
|
| 969 |
+
*
|
| 970 |
+
* **Gotchas**
|
| 971 |
+
*
|
| 972 |
+
* The schema argument does not register handlers by itself. Handler registration
|
| 973 |
+
* comes from the supplied layer, and writing an event without a registered
|
| 974 |
+
* handler dies with `Event handler not found for: "<tag>"`.
|
| 975 |
+
*
|
| 976 |
+
* @see {@link schema} for creating the schema argument from event groups
|
| 977 |
+
* @see {@link group} for building the handler layer consumed by this layer
|
| 978 |
+
* @see {@link layerEventLog} for installing the runtime and registry without combining a handler layer
|
| 979 |
+
*
|
| 980 |
* @category layers
|
| 981 |
+
* @since 4.0.0
|
| 982 |
*/
|
| 983 |
export const layer = <Groups extends EventGroup.Any, E, R>(
|
| 984 |
_schema: EventLogSchema<Groups>,
|
|
|
|
| 993 |
)
|
| 994 |
|
| 995 |
/**
|
| 996 |
+
* Creates a typed client function for writing events defined by an
|
| 997 |
+
* `EventLogSchema`.
|
| 998 |
+
*
|
| 999 |
+
* **Details**
|
| 1000 |
+
*
|
| 1001 |
+
* The returned function delegates to the `EventLog` service and preserves each
|
| 1002 |
+
* event's success and error types.
|
| 1003 |
+
*
|
| 1004 |
* @category client
|
| 1005 |
+
* @since 4.0.0
|
| 1006 |
*/
|
| 1007 |
export const makeClient = <Groups extends EventGroup.Any>(
|
| 1008 |
schema: EventLogSchema<Groups>
|
.opencode/node_modules/effect/src/unstable/eventlog/EventLogEncryption.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Context from "../../Context.ts"
|
|
@@ -12,8 +19,11 @@ import type { Identity } from "./EventLog.ts"
|
|
| 12 |
import { makeGetIdentityRootSecretMaterial } from "./internal/identityRootSecretDerivation.ts"
|
| 13 |
|
| 14 |
/**
|
| 15 |
-
*
|
|
|
|
|
|
|
| 16 |
* @category models
|
|
|
|
| 17 |
*/
|
| 18 |
export const EncryptedEntry = Schema.Struct({
|
| 19 |
entryId: EntryId,
|
|
@@ -21,14 +31,19 @@ export const EncryptedEntry = Schema.Struct({
|
|
| 21 |
})
|
| 22 |
|
| 23 |
/**
|
| 24 |
-
*
|
|
|
|
|
|
|
| 25 |
* @category models
|
|
|
|
| 26 |
*/
|
| 27 |
export interface EncryptedRemoteEntry extends Schema.Schema.Type<typeof EncryptedRemoteEntry> {}
|
| 28 |
|
| 29 |
/**
|
| 30 |
-
*
|
|
|
|
| 31 |
* @category models
|
|
|
|
| 32 |
*/
|
| 33 |
export const EncryptedRemoteEntry = Schema.Struct({
|
| 34 |
sequence: Schema.Number,
|
|
@@ -46,8 +61,16 @@ const toArrayBuffer = (data: Uint8Array): ArrayBuffer => {
|
|
| 46 |
const toBufferSource = (data: Uint8Array): ArrayBufferView<ArrayBuffer> => new Uint8Array(toArrayBuffer(data))
|
| 47 |
|
| 48 |
/**
|
| 49 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
* @category services
|
|
|
|
| 51 |
*/
|
| 52 |
export class EventLogEncryption extends Context.Service<EventLogEncryption, {
|
| 53 |
readonly encrypt: (
|
|
@@ -67,8 +90,11 @@ export class EventLogEncryption extends Context.Service<EventLogEncryption, {
|
|
| 67 |
}>()("effect/eventlog/EventLogEncryption") {}
|
| 68 |
|
| 69 |
/**
|
| 70 |
-
*
|
|
|
|
|
|
|
| 71 |
* @category encryption
|
|
|
|
| 72 |
*/
|
| 73 |
export const makeEncryptionSubtle = (crypto: Crypto): Effect.Effect<EventLogEncryption["Service"]> =>
|
| 74 |
Effect.sync(() => {
|
|
@@ -132,8 +158,10 @@ export const makeEncryptionSubtle = (crypto: Crypto): Effect.Effect<EventLogEncr
|
|
| 132 |
})
|
| 133 |
|
| 134 |
/**
|
| 135 |
-
*
|
|
|
|
| 136 |
* @category encryption
|
|
|
|
| 137 |
*/
|
| 138 |
export const layerSubtle: Layer.Layer<EventLogEncryption> = Layer.effect(
|
| 139 |
EventLogEncryption,
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Cryptographic service for encrypted event-log replication.
|
| 3 |
+
*
|
| 4 |
+
* `EventLogEncryption` turns local journal entries into encrypted remote
|
| 5 |
+
* payloads and decrypts encrypted changes received from a server. It also
|
| 6 |
+
* hashes byte data and creates event-log identities, so remote replication can
|
| 7 |
+
* use storage or transport that should not see plaintext event data.
|
| 8 |
+
*
|
| 9 |
* @since 4.0.0
|
| 10 |
*/
|
| 11 |
import * as Context from "../../Context.ts"
|
|
|
|
| 19 |
import { makeGetIdentityRootSecretMaterial } from "./internal/identityRootSecretDerivation.ts"
|
| 20 |
|
| 21 |
/**
|
| 22 |
+
* Schema for an encrypted journal entry paired with the id of the original
|
| 23 |
+
* entry.
|
| 24 |
+
*
|
| 25 |
* @category models
|
| 26 |
+
* @since 4.0.0
|
| 27 |
*/
|
| 28 |
export const EncryptedEntry = Schema.Struct({
|
| 29 |
entryId: EntryId,
|
|
|
|
| 31 |
})
|
| 32 |
|
| 33 |
/**
|
| 34 |
+
* Type of an encrypted remote entry, including its remote sequence number,
|
| 35 |
+
* initialization vector, entry id, and encrypted entry bytes.
|
| 36 |
+
*
|
| 37 |
* @category models
|
| 38 |
+
* @since 4.0.0
|
| 39 |
*/
|
| 40 |
export interface EncryptedRemoteEntry extends Schema.Schema.Type<typeof EncryptedRemoteEntry> {}
|
| 41 |
|
| 42 |
/**
|
| 43 |
+
* Schema for encrypted entries exchanged with a remote event-log server.
|
| 44 |
+
*
|
| 45 |
* @category models
|
| 46 |
+
* @since 4.0.0
|
| 47 |
*/
|
| 48 |
export const EncryptedRemoteEntry = Schema.Struct({
|
| 49 |
sequence: Schema.Number,
|
|
|
|
| 61 |
const toBufferSource = (data: Uint8Array): ArrayBufferView<ArrayBuffer> => new Uint8Array(toArrayBuffer(data))
|
| 62 |
|
| 63 |
/**
|
| 64 |
+
* Service that provides identity generation, entry
|
| 65 |
+
* encryption and decryption, and SHA-256 hashing for event-log replication.
|
| 66 |
+
*
|
| 67 |
+
* **When to use**
|
| 68 |
+
*
|
| 69 |
+
* Use to provide cryptographic operations required by encrypted event-log
|
| 70 |
+
* replication.
|
| 71 |
+
*
|
| 72 |
* @category services
|
| 73 |
+
* @since 4.0.0
|
| 74 |
*/
|
| 75 |
export class EventLogEncryption extends Context.Service<EventLogEncryption, {
|
| 76 |
readonly encrypt: (
|
|
|
|
| 90 |
}>()("effect/eventlog/EventLogEncryption") {}
|
| 91 |
|
| 92 |
/**
|
| 93 |
+
* Creates an `EventLogEncryption` service backed by the Web Crypto `SubtleCrypto`
|
| 94 |
+
* APIs from the supplied `Crypto` implementation.
|
| 95 |
+
*
|
| 96 |
* @category encryption
|
| 97 |
+
* @since 4.0.0
|
| 98 |
*/
|
| 99 |
export const makeEncryptionSubtle = (crypto: Crypto): Effect.Effect<EventLogEncryption["Service"]> =>
|
| 100 |
Effect.sync(() => {
|
|
|
|
| 158 |
})
|
| 159 |
|
| 160 |
/**
|
| 161 |
+
* Provides `EventLogEncryption` using `globalThis.crypto`.
|
| 162 |
+
*
|
| 163 |
* @category encryption
|
| 164 |
+
* @since 4.0.0
|
| 165 |
*/
|
| 166 |
export const layerSubtle: Layer.Layer<EventLogEncryption> = Layer.effect(
|
| 167 |
EventLogEncryption,
|
.opencode/node_modules/effect/src/unstable/eventlog/EventLogMessage.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import type { NonEmptyArray, NonEmptyReadonlyArray } from "../../Array.ts"
|
|
@@ -14,32 +21,47 @@ import type { Identity } from "./EventLog.ts"
|
|
| 14 |
import { EncryptedEntry, EncryptedRemoteEntry } from "./EventLogEncryption.ts"
|
| 15 |
|
| 16 |
/**
|
|
|
|
|
|
|
|
|
|
| 17 |
* @since 4.0.0
|
| 18 |
-
* @category StoreId
|
| 19 |
*/
|
| 20 |
export type StoreIdTypeId = "effect/eventlog/EventLog/StoreId"
|
| 21 |
|
| 22 |
/**
|
|
|
|
|
|
|
|
|
|
| 23 |
* @since 4.0.0
|
| 24 |
-
* @category StoreId
|
| 25 |
*/
|
| 26 |
export const StoreIdTypeId: StoreIdTypeId = "effect/eventlog/EventLog/StoreId"
|
| 27 |
|
| 28 |
/**
|
| 29 |
-
*
|
|
|
|
| 30 |
* @category StoreId
|
|
|
|
| 31 |
*/
|
| 32 |
export type StoreId = string & Brand<StoreIdTypeId>
|
| 33 |
|
| 34 |
/**
|
| 35 |
-
*
|
|
|
|
| 36 |
* @category StoreId
|
|
|
|
| 37 |
*/
|
| 38 |
export const StoreId = Schema.String.pipe(Schema.brand(StoreIdTypeId))
|
| 39 |
|
| 40 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
* @since 4.0.0
|
| 42 |
-
* @category protocol
|
| 43 |
*/
|
| 44 |
export class EventLogProtocolError extends Schema.TaggedErrorClass<EventLogProtocolError>(
|
| 45 |
"effect/eventlog/EventLogRemote/ProtocolError"
|
|
@@ -52,8 +74,11 @@ export class EventLogProtocolError extends Schema.TaggedErrorClass<EventLogProto
|
|
| 52 |
}) {}
|
| 53 |
|
| 54 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
* @since 4.0.0
|
| 56 |
-
* @category Middleware
|
| 57 |
*/
|
| 58 |
export class EventLogAuthentication extends RpcMiddleware.Service<EventLogAuthentication, {
|
| 59 |
provides: Identity
|
|
@@ -62,8 +87,15 @@ export class EventLogAuthentication extends RpcMiddleware.Service<EventLogAuthen
|
|
| 62 |
}) {}
|
| 63 |
|
| 64 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
* @since 4.0.0
|
| 66 |
-
* @category protocol
|
| 67 |
*/
|
| 68 |
export class HelloResponse extends Schema.Class<HelloResponse>("effect/eventlog/EventLogRemote/HelloResponse")({
|
| 69 |
remoteId: RemoteId,
|
|
@@ -71,16 +103,22 @@ export class HelloResponse extends Schema.Class<HelloResponse>("effect/eventlog/
|
|
| 71 |
}) {}
|
| 72 |
|
| 73 |
/**
|
|
|
|
|
|
|
|
|
|
| 74 |
* @since 4.0.0
|
| 75 |
-
* @category protocol
|
| 76 |
*/
|
| 77 |
export class HelloRpc extends Rpc.make("EventLog.Hello", {
|
| 78 |
success: HelloResponse
|
| 79 |
}) {}
|
| 80 |
|
| 81 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
* @since 4.0.0
|
| 83 |
-
* @category protocol
|
| 84 |
*/
|
| 85 |
export class Authenticate extends Schema.Class<Authenticate>("effect/eventlog/EventLogRemote/Authenticate")({
|
| 86 |
publicKey: Schema.String,
|
|
@@ -90,8 +128,10 @@ export class Authenticate extends Schema.Class<Authenticate>("effect/eventlog/Ev
|
|
| 90 |
}) {}
|
| 91 |
|
| 92 |
/**
|
|
|
|
|
|
|
|
|
|
| 93 |
* @since 4.0.0
|
| 94 |
-
* @category protocol
|
| 95 |
*/
|
| 96 |
export class AuthenticateRpc extends Rpc.make("EventLog.Authenticate", {
|
| 97 |
payload: Authenticate,
|
|
@@ -99,8 +139,10 @@ export class AuthenticateRpc extends Rpc.make("EventLog.Authenticate", {
|
|
| 99 |
}) {}
|
| 100 |
|
| 101 |
/**
|
|
|
|
|
|
|
|
|
|
| 102 |
* @since 4.0.0
|
| 103 |
-
* @category protocol
|
| 104 |
*/
|
| 105 |
export class SingleMessage
|
| 106 |
extends Schema.TaggedClass<SingleMessage>("effect/eventlog/EventLogRemote/SingleMessage")("Single", {
|
|
@@ -109,8 +151,15 @@ export class SingleMessage
|
|
| 109 |
{}
|
| 110 |
|
| 111 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
* @since 4.0.0
|
| 113 |
-
* @category protocol
|
| 114 |
*/
|
| 115 |
export class ChunkedMessage
|
| 116 |
extends Schema.TaggedClass<ChunkedMessage>("effect/eventlog/EventLogRemote/ChunkedMessage")("Chunked", {
|
|
@@ -130,6 +179,8 @@ export class ChunkedMessage
|
|
| 130 |
}
|
| 131 |
|
| 132 |
/**
|
|
|
|
|
|
|
| 133 |
* @since 4.0.0
|
| 134 |
*/
|
| 135 |
static split(id: number, data: Uint8Array): NonEmptyReadonlyArray<ChunkedMessage> {
|
|
@@ -148,6 +199,8 @@ export class ChunkedMessage
|
|
| 148 |
}
|
| 149 |
|
| 150 |
/**
|
|
|
|
|
|
|
| 151 |
* @since 4.0.0
|
| 152 |
*/
|
| 153 |
static join(
|
|
@@ -186,8 +239,10 @@ export class ChunkedMessage
|
|
| 186 |
}
|
| 187 |
|
| 188 |
/**
|
|
|
|
|
|
|
|
|
|
| 189 |
* @since 4.0.0
|
| 190 |
-
* @category protocol
|
| 191 |
*/
|
| 192 |
export class WriteChunkedRpc extends Rpc.make("EventLog.WriteChunked", {
|
| 193 |
payload: ChunkedMessage,
|
|
@@ -195,8 +250,15 @@ export class WriteChunkedRpc extends Rpc.make("EventLog.WriteChunked", {
|
|
| 195 |
}).middleware(EventLogAuthentication) {}
|
| 196 |
|
| 197 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
* @since 4.0.0
|
| 199 |
-
* @category protocol
|
| 200 |
*/
|
| 201 |
export class WriteEntries extends Schema.Class<WriteEntries>("effect/eventlog/EventLogRemote/WriteEntries")({
|
| 202 |
publicKey: Schema.String,
|
|
@@ -213,8 +275,10 @@ export class WriteEntries extends Schema.Class<WriteEntries>("effect/eventlog/Ev
|
|
| 213 |
}
|
| 214 |
|
| 215 |
/**
|
|
|
|
|
|
|
|
|
|
| 216 |
* @since 4.0.0
|
| 217 |
-
* @category protocol
|
| 218 |
*/
|
| 219 |
export class WriteEntriesUnencrypted
|
| 220 |
extends Schema.Class<WriteEntriesUnencrypted>("effect/eventlog/EventLogRemote/WriteEntriesUnencrypted")({
|
|
@@ -232,8 +296,10 @@ export class WriteEntriesUnencrypted
|
|
| 232 |
}
|
| 233 |
|
| 234 |
/**
|
|
|
|
|
|
|
|
|
|
| 235 |
* @since 4.0.0
|
| 236 |
-
* @category protocol
|
| 237 |
*/
|
| 238 |
export class WriteSingleRpc extends Rpc.make("EventLog.WriteSingle", {
|
| 239 |
payload: {
|
|
@@ -243,8 +309,16 @@ export class WriteSingleRpc extends Rpc.make("EventLog.WriteSingle", {
|
|
| 243 |
}).middleware(EventLogAuthentication) {}
|
| 244 |
|
| 245 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
* @since 4.0.0
|
| 247 |
-
* @category protocol
|
| 248 |
*/
|
| 249 |
export class ChangesRpc extends Rpc.make("EventLog.Changes", {
|
| 250 |
payload: {
|
|
@@ -265,8 +339,11 @@ export class ChangesRpc extends Rpc.make("EventLog.Changes", {
|
|
| 265 |
}
|
| 266 |
|
| 267 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
* @since 4.0.0
|
| 269 |
-
* @category protocol
|
| 270 |
*/
|
| 271 |
export class EventLogRemoteRpcs extends RpcGroup.make(
|
| 272 |
HelloRpc,
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Defines protocol messages for event-log remote clients and servers.
|
| 3 |
+
*
|
| 4 |
+
* This module is the shared boundary between `EventLogRemote` clients and
|
| 5 |
+
* event-log servers. It defines store ids, protocol errors, the
|
| 6 |
+
* hello/authenticate session handshake, remote calls for writes and changes,
|
| 7 |
+
* and message formats for encrypted or plaintext journal entries.
|
| 8 |
+
*
|
| 9 |
* @since 4.0.0
|
| 10 |
*/
|
| 11 |
import type { NonEmptyArray, NonEmptyReadonlyArray } from "../../Array.ts"
|
|
|
|
| 21 |
import { EncryptedEntry, EncryptedRemoteEntry } from "./EventLogEncryption.ts"
|
| 22 |
|
| 23 |
/**
|
| 24 |
+
* Type-level identifier used to brand event-log store ids.
|
| 25 |
+
*
|
| 26 |
+
* @category type IDs
|
| 27 |
* @since 4.0.0
|
|
|
|
| 28 |
*/
|
| 29 |
export type StoreIdTypeId = "effect/eventlog/EventLog/StoreId"
|
| 30 |
|
| 31 |
/**
|
| 32 |
+
* Runtime brand identifier for event-log store ids.
|
| 33 |
+
*
|
| 34 |
+
* @category type IDs
|
| 35 |
* @since 4.0.0
|
|
|
|
| 36 |
*/
|
| 37 |
export const StoreIdTypeId: StoreIdTypeId = "effect/eventlog/EventLog/StoreId"
|
| 38 |
|
| 39 |
/**
|
| 40 |
+
* Branded string identifying a logical event-log store.
|
| 41 |
+
*
|
| 42 |
* @category StoreId
|
| 43 |
+
* @since 4.0.0
|
| 44 |
*/
|
| 45 |
export type StoreId = string & Brand<StoreIdTypeId>
|
| 46 |
|
| 47 |
/**
|
| 48 |
+
* Schema for branded event-log store ids.
|
| 49 |
+
*
|
| 50 |
* @category StoreId
|
| 51 |
+
* @since 4.0.0
|
| 52 |
*/
|
| 53 |
export const StoreId = Schema.String.pipe(Schema.brand(StoreIdTypeId))
|
| 54 |
|
| 55 |
/**
|
| 56 |
+
* Error returned by event-log remote RPCs.
|
| 57 |
+
*
|
| 58 |
+
* **Details**
|
| 59 |
+
*
|
| 60 |
+
* It records the request tag, optional identity and store information, a protocol
|
| 61 |
+
* error code, and a human-readable message.
|
| 62 |
+
*
|
| 63 |
+
* @category protocols
|
| 64 |
* @since 4.0.0
|
|
|
|
| 65 |
*/
|
| 66 |
export class EventLogProtocolError extends Schema.TaggedErrorClass<EventLogProtocolError>(
|
| 67 |
"effect/eventlog/EventLogRemote/ProtocolError"
|
|
|
|
| 74 |
}) {}
|
| 75 |
|
| 76 |
/**
|
| 77 |
+
* RPC middleware that authenticates event-log requests and provides the client
|
| 78 |
+
* `Identity` to authenticated handlers.
|
| 79 |
+
*
|
| 80 |
+
* @category middleware
|
| 81 |
* @since 4.0.0
|
|
|
|
| 82 |
*/
|
| 83 |
export class EventLogAuthentication extends RpcMiddleware.Service<EventLogAuthentication, {
|
| 84 |
provides: Identity
|
|
|
|
| 87 |
}) {}
|
| 88 |
|
| 89 |
/**
|
| 90 |
+
* Response sent by the remote server during the authentication handshake.
|
| 91 |
+
*
|
| 92 |
+
* **Details**
|
| 93 |
+
*
|
| 94 |
+
* It contains the server remote id and a challenge that must be signed by the
|
| 95 |
+
* client.
|
| 96 |
+
*
|
| 97 |
+
* @category protocols
|
| 98 |
* @since 4.0.0
|
|
|
|
| 99 |
*/
|
| 100 |
export class HelloResponse extends Schema.Class<HelloResponse>("effect/eventlog/EventLogRemote/HelloResponse")({
|
| 101 |
remoteId: RemoteId,
|
|
|
|
| 103 |
}) {}
|
| 104 |
|
| 105 |
/**
|
| 106 |
+
* RPC used to start an event-log remote session and receive a `HelloResponse`.
|
| 107 |
+
*
|
| 108 |
+
* @category protocols
|
| 109 |
* @since 4.0.0
|
|
|
|
| 110 |
*/
|
| 111 |
export class HelloRpc extends Rpc.make("EventLog.Hello", {
|
| 112 |
success: HelloResponse
|
| 113 |
}) {}
|
| 114 |
|
| 115 |
/**
|
| 116 |
+
* Schema for an authentication request containing the client public key,
|
| 117 |
+
* Ed25519 signing public key, signature over the session challenge payload, and
|
| 118 |
+
* algorithm name.
|
| 119 |
+
*
|
| 120 |
+
* @category protocols
|
| 121 |
* @since 4.0.0
|
|
|
|
| 122 |
*/
|
| 123 |
export class Authenticate extends Schema.Class<Authenticate>("effect/eventlog/EventLogRemote/Authenticate")({
|
| 124 |
publicKey: Schema.String,
|
|
|
|
| 128 |
}) {}
|
| 129 |
|
| 130 |
/**
|
| 131 |
+
* RPC used to authenticate a remote event-log session after `HelloRpc`.
|
| 132 |
+
*
|
| 133 |
+
* @category protocols
|
| 134 |
* @since 4.0.0
|
|
|
|
| 135 |
*/
|
| 136 |
export class AuthenticateRpc extends Rpc.make("EventLog.Authenticate", {
|
| 137 |
payload: Authenticate,
|
|
|
|
| 139 |
}) {}
|
| 140 |
|
| 141 |
/**
|
| 142 |
+
* Represents an entire encoded event-log payload in one transport frame.
|
| 143 |
+
*
|
| 144 |
+
* @category protocols
|
| 145 |
* @since 4.0.0
|
|
|
|
| 146 |
*/
|
| 147 |
export class SingleMessage
|
| 148 |
extends Schema.TaggedClass<SingleMessage>("effect/eventlog/EventLogRemote/SingleMessage")("Single", {
|
|
|
|
| 151 |
{}
|
| 152 |
|
| 153 |
/**
|
| 154 |
+
* Represents one part of a large encoded event-log payload.
|
| 155 |
+
*
|
| 156 |
+
* **When to use**
|
| 157 |
+
*
|
| 158 |
+
* Use to divide data into chunks and `join` to reassemble all chunks with
|
| 159 |
+
* the same id once every part has arrived.
|
| 160 |
+
*
|
| 161 |
+
* @category protocols
|
| 162 |
* @since 4.0.0
|
|
|
|
| 163 |
*/
|
| 164 |
export class ChunkedMessage
|
| 165 |
extends Schema.TaggedClass<ChunkedMessage>("effect/eventlog/EventLogRemote/ChunkedMessage")("Chunked", {
|
|
|
|
| 179 |
}
|
| 180 |
|
| 181 |
/**
|
| 182 |
+
* Splits binary event-log message data into numbered chunks.
|
| 183 |
+
*
|
| 184 |
* @since 4.0.0
|
| 185 |
*/
|
| 186 |
static split(id: number, data: Uint8Array): NonEmptyReadonlyArray<ChunkedMessage> {
|
|
|
|
| 199 |
}
|
| 200 |
|
| 201 |
/**
|
| 202 |
+
* Reassembles all chunks for a message id into the original binary payload.
|
| 203 |
+
*
|
| 204 |
* @since 4.0.0
|
| 205 |
*/
|
| 206 |
static join(
|
|
|
|
| 239 |
}
|
| 240 |
|
| 241 |
/**
|
| 242 |
+
* RPC used to send one chunk of a large encoded write payload.
|
| 243 |
+
*
|
| 244 |
+
* @category protocols
|
| 245 |
* @since 4.0.0
|
|
|
|
| 246 |
*/
|
| 247 |
export class WriteChunkedRpc extends Rpc.make("EventLog.WriteChunked", {
|
| 248 |
payload: ChunkedMessage,
|
|
|
|
| 250 |
}).middleware(EventLogAuthentication) {}
|
| 251 |
|
| 252 |
/**
|
| 253 |
+
* Schema for encrypted event-log write payloads sent to a remote store.
|
| 254 |
+
*
|
| 255 |
+
* **Details**
|
| 256 |
+
*
|
| 257 |
+
* It includes the client public key, target store id, AES-GCM initialization
|
| 258 |
+
* vector, and encrypted entries.
|
| 259 |
+
*
|
| 260 |
+
* @category protocols
|
| 261 |
* @since 4.0.0
|
|
|
|
| 262 |
*/
|
| 263 |
export class WriteEntries extends Schema.Class<WriteEntries>("effect/eventlog/EventLogRemote/WriteEntries")({
|
| 264 |
publicKey: Schema.String,
|
|
|
|
| 275 |
}
|
| 276 |
|
| 277 |
/**
|
| 278 |
+
* Schema for plaintext event-log write payloads sent to a remote store.
|
| 279 |
+
*
|
| 280 |
+
* @category protocols
|
| 281 |
* @since 4.0.0
|
|
|
|
| 282 |
*/
|
| 283 |
export class WriteEntriesUnencrypted
|
| 284 |
extends Schema.Class<WriteEntriesUnencrypted>("effect/eventlog/EventLogRemote/WriteEntriesUnencrypted")({
|
|
|
|
| 296 |
}
|
| 297 |
|
| 298 |
/**
|
| 299 |
+
* RPC used to send an encoded write payload that fits in one message.
|
| 300 |
+
*
|
| 301 |
+
* @category protocols
|
| 302 |
* @since 4.0.0
|
|
|
|
| 303 |
*/
|
| 304 |
export class WriteSingleRpc extends Rpc.make("EventLog.WriteSingle", {
|
| 305 |
payload: {
|
|
|
|
| 309 |
}).middleware(EventLogAuthentication) {}
|
| 310 |
|
| 311 |
/**
|
| 312 |
+
* RPC used to stream remote event-log changes for a public key and store id
|
| 313 |
+
* starting at a sequence number.
|
| 314 |
+
*
|
| 315 |
+
* **Details**
|
| 316 |
+
*
|
| 317 |
+
* Responses are encoded as either `SingleMessage` values or `ChunkedMessage`
|
| 318 |
+
* parts.
|
| 319 |
+
*
|
| 320 |
+
* @category protocols
|
| 321 |
* @since 4.0.0
|
|
|
|
| 322 |
*/
|
| 323 |
export class ChangesRpc extends Rpc.make("EventLog.Changes", {
|
| 324 |
payload: {
|
|
|
|
| 339 |
}
|
| 340 |
|
| 341 |
/**
|
| 342 |
+
* RPC group containing the event-log remote handshake, authentication, write, and
|
| 343 |
+
* changes endpoints.
|
| 344 |
+
*
|
| 345 |
+
* @category protocols
|
| 346 |
* @since 4.0.0
|
|
|
|
| 347 |
*/
|
| 348 |
export class EventLogRemoteRpcs extends RpcGroup.make(
|
| 349 |
HelloRpc,
|
.opencode/node_modules/effect/src/unstable/eventlog/EventLogRemote.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Cache from "../../Cache.ts"
|
|
@@ -32,8 +41,20 @@ import { encodeSessionAuthPayload, signSessionAuthPayloadBytes } from "./EventLo
|
|
| 32 |
import { makeGetIdentityRootSecretMaterial } from "./internal/identityRootSecretDerivation.ts"
|
| 33 |
|
| 34 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
* @since 4.0.0
|
| 36 |
-
* @category models
|
| 37 |
*/
|
| 38 |
export class EventLogRemote extends Context.Service<EventLogRemote, {
|
| 39 |
readonly id: RemoteId
|
|
@@ -53,8 +74,11 @@ export class EventLogRemote extends Context.Service<EventLogRemote, {
|
|
| 53 |
}>()("effect/eventlog/EventLogRemote") {}
|
| 54 |
|
| 55 |
/**
|
| 56 |
-
*
|
|
|
|
|
|
|
| 57 |
* @category errors
|
|
|
|
| 58 |
*/
|
| 59 |
export class EventLogRemoteError extends Data.TaggedError("EventLogRemoteError")<{
|
| 60 |
readonly method: string
|
|
@@ -88,8 +112,15 @@ const makeAuthenticate = Effect.fnUntraced(function*(options: {
|
|
| 88 |
})
|
| 89 |
|
| 90 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
* @since 4.0.0
|
| 92 |
-
* @category RpcClient
|
| 93 |
*/
|
| 94 |
export class EventLogRemoteClient extends Context.Service<
|
| 95 |
EventLogRemoteClient,
|
|
@@ -106,8 +137,17 @@ export class EventLogRemoteClient extends Context.Service<
|
|
| 106 |
}
|
| 107 |
|
| 108 |
/**
|
| 109 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
* @category constructors
|
|
|
|
| 111 |
*/
|
| 112 |
export const makeWith = Effect.fnUntraced(function*({ encodeWrite, decodeChanges }: {
|
| 113 |
readonly encodeWrite: (options: {
|
|
@@ -251,8 +291,11 @@ class IdentityService extends Context.Service<Identity, Identity["Service"]>()(
|
|
| 251 |
) {}
|
| 252 |
|
| 253 |
/**
|
| 254 |
-
*
|
|
|
|
|
|
|
| 255 |
* @category constructors
|
|
|
|
| 256 |
*/
|
| 257 |
export const makeEncrypted = Effect.gen(function*(): Effect.fn.Return<
|
| 258 |
EventLogRemote["Service"],
|
|
@@ -284,8 +327,10 @@ export const makeEncrypted = Effect.gen(function*(): Effect.fn.Return<
|
|
| 284 |
})
|
| 285 |
|
| 286 |
/**
|
| 287 |
-
*
|
|
|
|
| 288 |
* @category constructors
|
|
|
|
| 289 |
*/
|
| 290 |
export const makeUnencrypted: Effect.Effect<
|
| 291 |
EventLogRemote["Service"],
|
|
@@ -302,8 +347,11 @@ export const makeUnencrypted: Effect.Effect<
|
|
| 302 |
})
|
| 303 |
|
| 304 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
* @since 4.0.0
|
| 306 |
-
* @category Layers
|
| 307 |
*/
|
| 308 |
export const layerEncrypted: Layer.Layer<
|
| 309 |
EventLogRemote,
|
|
@@ -315,8 +363,10 @@ export const layerEncrypted: Layer.Layer<
|
|
| 315 |
)
|
| 316 |
|
| 317 |
/**
|
|
|
|
|
|
|
|
|
|
| 318 |
* @since 4.0.0
|
| 319 |
-
* @category Layers
|
| 320 |
*/
|
| 321 |
export const layerUnencrypted: Layer.Layer<
|
| 322 |
EventLogRemote,
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Connects a local event log to a remote replica.
|
| 3 |
+
*
|
| 4 |
+
* `EventLogRemote` writes local entries to another journal, receives remote
|
| 5 |
+
* change streams from a sequence number, and can wait until the current
|
| 6 |
+
* event-log identity has completed remote authentication. The encrypted
|
| 7 |
+
* constructor is the default for browser, edge, or service replicas crossing an
|
| 8 |
+
* untrusted network. The unencrypted constructor is intended for trusted
|
| 9 |
+
* transports or tests.
|
| 10 |
+
*
|
| 11 |
* @since 4.0.0
|
| 12 |
*/
|
| 13 |
import * as Cache from "../../Cache.ts"
|
|
|
|
| 41 |
import { makeGetIdentityRootSecretMaterial } from "./internal/identityRootSecretDerivation.ts"
|
| 42 |
|
| 43 |
/**
|
| 44 |
+
* Service that represents a remote event-log replica.
|
| 45 |
+
*
|
| 46 |
+
* **When to use**
|
| 47 |
+
*
|
| 48 |
+
* Use to access or provide a remote event-log replica that can write local
|
| 49 |
+
* entries and stream remote changes.
|
| 50 |
+
*
|
| 51 |
+
* **Details**
|
| 52 |
+
*
|
| 53 |
+
* It can write local entries to the remote, stream remote changes from a sequence
|
| 54 |
+
* number, and run effects only after the supplied identity has authenticated.
|
| 55 |
+
*
|
| 56 |
+
* @category services
|
| 57 |
* @since 4.0.0
|
|
|
|
| 58 |
*/
|
| 59 |
export class EventLogRemote extends Context.Service<EventLogRemote, {
|
| 60 |
readonly id: RemoteId
|
|
|
|
| 74 |
}>()("effect/eventlog/EventLogRemote") {}
|
| 75 |
|
| 76 |
/**
|
| 77 |
+
* Error raised by `EventLogRemote` operations, recording the failed method and
|
| 78 |
+
* underlying cause.
|
| 79 |
+
*
|
| 80 |
* @category errors
|
| 81 |
+
* @since 4.0.0
|
| 82 |
*/
|
| 83 |
export class EventLogRemoteError extends Data.TaggedError("EventLogRemoteError")<{
|
| 84 |
readonly method: string
|
|
|
|
| 112 |
})
|
| 113 |
|
| 114 |
/**
|
| 115 |
+
* Service that provides a typed RPC client for the `EventLogRemoteRpcs` protocol.
|
| 116 |
+
*
|
| 117 |
+
* **When to use**
|
| 118 |
+
*
|
| 119 |
+
* Use to provide the RPC client used by remote event-log replicas to
|
| 120 |
+
* authenticate, write entries, and subscribe to changes.
|
| 121 |
+
*
|
| 122 |
+
* @category RPC client
|
| 123 |
* @since 4.0.0
|
|
|
|
| 124 |
*/
|
| 125 |
export class EventLogRemoteClient extends Context.Service<
|
| 126 |
EventLogRemoteClient,
|
|
|
|
| 137 |
}
|
| 138 |
|
| 139 |
/**
|
| 140 |
+
* Creates an `EventLogRemote` from custom write encoding and change decoding
|
| 141 |
+
* functions.
|
| 142 |
+
*
|
| 143 |
+
* **Details**
|
| 144 |
+
*
|
| 145 |
+
* The remote performs the hello/authentication handshake, retries after forbidden
|
| 146 |
+
* responses by re-authenticating, chunks large writes, and registers itself with
|
| 147 |
+
* the `Registry` for the current scope.
|
| 148 |
+
*
|
| 149 |
* @category constructors
|
| 150 |
+
* @since 4.0.0
|
| 151 |
*/
|
| 152 |
export const makeWith = Effect.fnUntraced(function*({ encodeWrite, decodeChanges }: {
|
| 153 |
readonly encodeWrite: (options: {
|
|
|
|
| 291 |
) {}
|
| 292 |
|
| 293 |
/**
|
| 294 |
+
* Creates an `EventLogRemote` that encrypts outgoing entries and decrypts
|
| 295 |
+
* incoming changes with `EventLogEncryption`.
|
| 296 |
+
*
|
| 297 |
* @category constructors
|
| 298 |
+
* @since 4.0.0
|
| 299 |
*/
|
| 300 |
export const makeEncrypted = Effect.gen(function*(): Effect.fn.Return<
|
| 301 |
EventLogRemote["Service"],
|
|
|
|
| 327 |
})
|
| 328 |
|
| 329 |
/**
|
| 330 |
+
* Creates an `EventLogRemote` that sends and receives plaintext entry payloads.
|
| 331 |
+
*
|
| 332 |
* @category constructors
|
| 333 |
+
* @since 4.0.0
|
| 334 |
*/
|
| 335 |
export const makeUnencrypted: Effect.Effect<
|
| 336 |
EventLogRemote["Service"],
|
|
|
|
| 347 |
})
|
| 348 |
|
| 349 |
/**
|
| 350 |
+
* Provides an encrypted `EventLogRemote` using the remote RPC client and the
|
| 351 |
+
* default Web Crypto encryption layer.
|
| 352 |
+
*
|
| 353 |
+
* @category layers
|
| 354 |
* @since 4.0.0
|
|
|
|
| 355 |
*/
|
| 356 |
export const layerEncrypted: Layer.Layer<
|
| 357 |
EventLogRemote,
|
|
|
|
| 363 |
)
|
| 364 |
|
| 365 |
/**
|
| 366 |
+
* Provides an unencrypted `EventLogRemote` using the remote RPC client.
|
| 367 |
+
*
|
| 368 |
+
* @category layers
|
| 369 |
* @since 4.0.0
|
|
|
|
| 370 |
*/
|
| 371 |
export const layerUnencrypted: Layer.Layer<
|
| 372 |
EventLogRemote,
|
.opencode/node_modules/effect/src/unstable/eventlog/EventLogServer.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import type { NonEmptyReadonlyArray } from "../../Array.ts"
|
|
@@ -29,8 +36,15 @@ import {
|
|
| 29 |
import * as EventLogSessionAuth from "./EventLogSessionAuth.ts"
|
| 30 |
|
| 31 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
* @since 4.0.0
|
| 33 |
-
* @category Layers
|
| 34 |
*/
|
| 35 |
export const layerAuthMiddleware: Layer.Layer<
|
| 36 |
EventLogAuthentication
|
|
@@ -48,8 +62,16 @@ export const layerAuthMiddleware: Layer.Layer<
|
|
| 48 |
})
|
| 49 |
|
| 50 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
* @since 4.0.0
|
| 52 |
-
* @category Layers
|
| 53 |
*/
|
| 54 |
export const layerRpcHandlers = (options: {
|
| 55 |
readonly remoteId: RemoteId
|
|
@@ -184,8 +206,16 @@ export const layerRpcHandlers = (options: {
|
|
| 184 |
)
|
| 185 |
|
| 186 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
* @since 4.0.0
|
| 188 |
-
* @category ChunkedMessage state
|
| 189 |
*/
|
| 190 |
export class ChunkedMessageState extends Context.Reference<
|
| 191 |
Map<number, {
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Builds server-side handlers for the event-log remote protocol.
|
| 3 |
+
*
|
| 4 |
+
* Transport modules use these handlers to expose an event journal to remote
|
| 5 |
+
* replicas. The handlers run the hello/authenticate challenge flow, attach the
|
| 6 |
+
* authenticated `EventLog.Identity` to later requests, accept single or chunked
|
| 7 |
+
* writes, and stream changes back as single or chunked messages.
|
| 8 |
+
*
|
| 9 |
* @since 4.0.0
|
| 10 |
*/
|
| 11 |
import type { NonEmptyReadonlyArray } from "../../Array.ts"
|
|
|
|
| 36 |
import * as EventLogSessionAuth from "./EventLogSessionAuth.ts"
|
| 37 |
|
| 38 |
/**
|
| 39 |
+
* Provides RPC authentication middleware that reads the authenticated
|
| 40 |
+
* `EventLog.Identity` from client annotations.
|
| 41 |
+
*
|
| 42 |
+
* **Details**
|
| 43 |
+
*
|
| 44 |
+
* Requests without an identity fail with a forbidden `EventLogProtocolError`.
|
| 45 |
+
*
|
| 46 |
+
* @category layers
|
| 47 |
* @since 4.0.0
|
|
|
|
| 48 |
*/
|
| 49 |
export const layerAuthMiddleware: Layer.Layer<
|
| 50 |
EventLogAuthentication
|
|
|
|
| 62 |
})
|
| 63 |
|
| 64 |
/**
|
| 65 |
+
* Creates the shared RPC handlers for the event-log remote protocol.
|
| 66 |
+
*
|
| 67 |
+
* **Details**
|
| 68 |
+
*
|
| 69 |
+
* The layer manages hello challenges, verifies session authentication, reassembles
|
| 70 |
+
* chunked writes, delegates write and change handling to the supplied callbacks,
|
| 71 |
+
* and frames large change payloads into chunks.
|
| 72 |
+
*
|
| 73 |
+
* @category layers
|
| 74 |
* @since 4.0.0
|
|
|
|
| 75 |
*/
|
| 76 |
export const layerRpcHandlers = (options: {
|
| 77 |
readonly remoteId: RemoteId
|
|
|
|
| 206 |
)
|
| 207 |
|
| 208 |
/**
|
| 209 |
+
* Annotation that stores partial `ChunkedMessage` data while chunked writes are
|
| 210 |
+
* being reassembled.
|
| 211 |
+
*
|
| 212 |
+
* **When to use**
|
| 213 |
+
*
|
| 214 |
+
* Use to keep per-client chunk assembly state while handling chunked event-log
|
| 215 |
+
* writes.
|
| 216 |
+
*
|
| 217 |
+
* @category chunked message state
|
| 218 |
* @since 4.0.0
|
|
|
|
| 219 |
*/
|
| 220 |
export class ChunkedMessageState extends Context.Reference<
|
| 221 |
Map<number, {
|
.opencode/node_modules/effect/src/unstable/eventlog/EventLogServerEncrypted.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Uuid from "uuid"
|
|
@@ -19,8 +28,16 @@ import { ChangesRpc, EventLogProtocolError, EventLogRemoteRpcs, type StoreId, Wr
|
|
| 19 |
import * as EventLogServer from "./EventLogServer.ts"
|
| 20 |
|
| 21 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
* @since 4.0.0
|
| 23 |
-
* @category Layers
|
| 24 |
*/
|
| 25 |
export const layerRpcHandlers = Layer.unwrap(Effect.gen(function*() {
|
| 26 |
const storage = yield* Storage
|
|
@@ -70,16 +87,37 @@ export const layerRpcHandlers = Layer.unwrap(Effect.gen(function*() {
|
|
| 70 |
}))
|
| 71 |
|
| 72 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
* @since 4.0.0
|
| 74 |
-
* @category Layers
|
| 75 |
*/
|
| 76 |
export const layer: Layer.Layer<never, never, RpcServer.Protocol | Storage> = RpcServer.layer(EventLogRemoteRpcs).pipe(
|
| 77 |
Layer.provide(layerRpcHandlers)
|
| 78 |
)
|
| 79 |
|
| 80 |
/**
|
| 81 |
-
*
|
|
|
|
| 82 |
* @category storage
|
|
|
|
| 83 |
*/
|
| 84 |
export class PersistedEntry extends Schema.Class<PersistedEntry>(
|
| 85 |
"effect/eventlog/EventLogServerEncrypted/PersistedEntry"
|
|
@@ -89,6 +127,8 @@ export class PersistedEntry extends Schema.Class<PersistedEntry>(
|
|
| 89 |
encryptedEntry: Transferable.Uint8Array
|
| 90 |
}) {
|
| 91 |
/**
|
|
|
|
|
|
|
| 92 |
* @since 4.0.0
|
| 93 |
*/
|
| 94 |
get entryIdString(): string {
|
|
@@ -97,8 +137,21 @@ export class PersistedEntry extends Schema.Class<PersistedEntry>(
|
|
| 97 |
}
|
| 98 |
|
| 99 |
/**
|
| 100 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
* @category storage
|
|
|
|
| 102 |
*/
|
| 103 |
export class Storage extends Context.Service<Storage, {
|
| 104 |
readonly getId: Effect.Effect<RemoteId>
|
|
@@ -119,8 +172,15 @@ export class Storage extends Context.Service<Storage, {
|
|
| 119 |
}>()("effect/eventlog/EventLogServer/Storage") {}
|
| 120 |
|
| 121 |
/**
|
| 122 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
* @category storage
|
|
|
|
| 124 |
*/
|
| 125 |
export const makeStorageMemory: Effect.Effect<Storage["Service"], never, Scope.Scope> = Effect.gen(function*() {
|
| 126 |
const knownIds = new Map<string, Map<string, number>>()
|
|
@@ -193,8 +253,10 @@ export const makeStorageMemory: Effect.Effect<Storage["Service"], never, Scope.S
|
|
| 193 |
})
|
| 194 |
|
| 195 |
/**
|
| 196 |
-
*
|
|
|
|
| 197 |
* @category storage
|
|
|
|
| 198 |
*/
|
| 199 |
export const layerStorageMemory: Layer.Layer<Storage> = Layer.effect(Storage)(makeStorageMemory)
|
| 200 |
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Serves encrypted event-log replication.
|
| 3 |
+
*
|
| 4 |
+
* Encrypted `EventLogRemote` clients use this module when they need a remote
|
| 5 |
+
* synchronization endpoint that never sees plaintext events. The server stores
|
| 6 |
+
* encrypted entries and replication metadata keyed by client public key and
|
| 7 |
+
* store id, then streams encrypted changes back to clients for local
|
| 8 |
+
* decryption. This module defines the RPC handlers, server layer, storage
|
| 9 |
+
* contract, and in-memory storage layer for that encrypted server path.
|
| 10 |
+
*
|
| 11 |
* @since 4.0.0
|
| 12 |
*/
|
| 13 |
import * as Uuid from "uuid"
|
|
|
|
| 28 |
import * as EventLogServer from "./EventLogServer.ts"
|
| 29 |
|
| 30 |
/**
|
| 31 |
+
* Provides RPC handlers for the encrypted event-log server.
|
| 32 |
+
*
|
| 33 |
+
* **Details**
|
| 34 |
+
*
|
| 35 |
+
* Incoming encrypted write payloads are decoded and persisted through `Storage`;
|
| 36 |
+
* change streams read encrypted entries from storage and encode them for the
|
| 37 |
+
* remote protocol.
|
| 38 |
+
*
|
| 39 |
+
* @category layers
|
| 40 |
* @since 4.0.0
|
|
|
|
| 41 |
*/
|
| 42 |
export const layerRpcHandlers = Layer.unwrap(Effect.gen(function*() {
|
| 43 |
const storage = yield* Storage
|
|
|
|
| 87 |
}))
|
| 88 |
|
| 89 |
/**
|
| 90 |
+
* Provides an encrypted event-log RPC server using `EventLogRemoteRpcs` and the
|
| 91 |
+
* encrypted server RPC handlers.
|
| 92 |
+
*
|
| 93 |
+
* **When to use**
|
| 94 |
+
*
|
| 95 |
+
* Use when you need an encrypted event-log RPC server for encrypted
|
| 96 |
+
* `EventLogRemote` replication over an existing `RpcServer.Protocol`.
|
| 97 |
+
*
|
| 98 |
+
* **Details**
|
| 99 |
+
*
|
| 100 |
+
* This layer installs `EventLogRemoteRpcs` on the provided RPC server protocol
|
| 101 |
+
* and wires those RPCs to `layerRpcHandlers`. Encrypted entries, session
|
| 102 |
+
* authentication bindings, remote ids, and change streams are delegated to
|
| 103 |
+
* `Storage`.
|
| 104 |
+
*
|
| 105 |
+
* @see {@link layerRpcHandlers} for the encrypted handler layer without installing an RPC server protocol
|
| 106 |
+
* @see {@link Storage} for the storage service required by this layer
|
| 107 |
+
* @see {@link layerStorageMemory} for the process-local in-memory storage layer
|
| 108 |
+
*
|
| 109 |
+
* @category layers
|
| 110 |
* @since 4.0.0
|
|
|
|
| 111 |
*/
|
| 112 |
export const layer: Layer.Layer<never, never, RpcServer.Protocol | Storage> = RpcServer.layer(EventLogRemoteRpcs).pipe(
|
| 113 |
Layer.provide(layerRpcHandlers)
|
| 114 |
)
|
| 115 |
|
| 116 |
/**
|
| 117 |
+
* Schema for encrypted entries persisted by the encrypted event-log server.
|
| 118 |
+
*
|
| 119 |
* @category storage
|
| 120 |
+
* @since 4.0.0
|
| 121 |
*/
|
| 122 |
export class PersistedEntry extends Schema.Class<PersistedEntry>(
|
| 123 |
"effect/eventlog/EventLogServerEncrypted/PersistedEntry"
|
|
|
|
| 127 |
encryptedEntry: Transferable.Uint8Array
|
| 128 |
}) {
|
| 129 |
/**
|
| 130 |
+
* String representation of the encrypted entry id.
|
| 131 |
+
*
|
| 132 |
* @since 4.0.0
|
| 133 |
*/
|
| 134 |
get entryIdString(): string {
|
|
|
|
| 137 |
}
|
| 138 |
|
| 139 |
/**
|
| 140 |
+
* Defines the backing store service used by the encrypted event-log server.
|
| 141 |
+
*
|
| 142 |
+
* **When to use**
|
| 143 |
+
*
|
| 144 |
+
* Use to provide durable encrypted event-log persistence for an encrypted
|
| 145 |
+
* event-log server layer.
|
| 146 |
+
*
|
| 147 |
+
* **Details**
|
| 148 |
+
*
|
| 149 |
+
* It provides the server remote id, stores session authentication bindings,
|
| 150 |
+
* persists encrypted entries, and streams encrypted changes for a public key and
|
| 151 |
+
* store id.
|
| 152 |
+
*
|
| 153 |
* @category storage
|
| 154 |
+
* @since 4.0.0
|
| 155 |
*/
|
| 156 |
export class Storage extends Context.Service<Storage, {
|
| 157 |
readonly getId: Effect.Effect<RemoteId>
|
|
|
|
| 172 |
}>()("effect/eventlog/EventLogServer/Storage") {}
|
| 173 |
|
| 174 |
/**
|
| 175 |
+
* Creates an in-memory encrypted server `Storage`.
|
| 176 |
+
*
|
| 177 |
+
* **Details**
|
| 178 |
+
*
|
| 179 |
+
* Data, session authentication bindings, and streams are process-local and are
|
| 180 |
+
* released with the surrounding scope.
|
| 181 |
+
*
|
| 182 |
* @category storage
|
| 183 |
+
* @since 4.0.0
|
| 184 |
*/
|
| 185 |
export const makeStorageMemory: Effect.Effect<Storage["Service"], never, Scope.Scope> = Effect.gen(function*() {
|
| 186 |
const knownIds = new Map<string, Map<string, number>>()
|
|
|
|
| 253 |
})
|
| 254 |
|
| 255 |
/**
|
| 256 |
+
* Provides encrypted server `Storage` using the in-memory implementation.
|
| 257 |
+
*
|
| 258 |
* @category storage
|
| 259 |
+
* @since 4.0.0
|
| 260 |
*/
|
| 261 |
export const layerStorageMemory: Layer.Layer<Storage> = Layer.effect(Storage)(makeStorageMemory)
|
| 262 |
|
.opencode/node_modules/effect/src/unstable/eventlog/EventLogServerUnencrypted.ts
CHANGED
|
@@ -1,4 +1,14 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Arr from "../../Array.ts"
|
|
@@ -33,8 +43,16 @@ import {
|
|
| 33 |
import * as EventLogServer from "./EventLogServer.ts"
|
| 34 |
|
| 35 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
* @since 4.0.0
|
| 37 |
-
* @category EventLogServerUnencrypted
|
| 38 |
*/
|
| 39 |
export class EventLogServerUnencrypted extends Context.Service<EventLogServerUnencrypted, {
|
| 40 |
readonly makeWrite: <Groups extends EventGroup.Any>(
|
|
@@ -53,8 +71,11 @@ export class EventLogServerUnencrypted extends Context.Service<EventLogServerUne
|
|
| 53 |
}>()("effect/eventlog/EventLogServerUnencrypted") {}
|
| 54 |
|
| 55 |
/**
|
| 56 |
-
*
|
|
|
|
|
|
|
| 57 |
* @category EventLogServerUnencrypted
|
|
|
|
| 58 |
*/
|
| 59 |
export const makeWrite = <Groups extends EventGroup.Any>(
|
| 60 |
schema: EventLog.EventLogSchema<Groups>
|
|
@@ -75,8 +96,16 @@ export const makeWrite = <Groups extends EventGroup.Any>(
|
|
| 75 |
> => EventLogServerUnencrypted.useSync((_) => _.makeWrite(schema))
|
| 76 |
|
| 77 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
* @since 4.0.0
|
| 79 |
-
* @category Layers
|
| 80 |
*/
|
| 81 |
export const layerRpcHandlers: Layer.Layer<
|
| 82 |
Rpc.ToHandler<RpcGroup.Rpcs<typeof EventLogRemoteRpcs>> | EventLogAuthentication,
|
|
@@ -198,8 +227,10 @@ export const layerRpcHandlers: Layer.Layer<
|
|
| 198 |
}))
|
| 199 |
|
| 200 |
/**
|
| 201 |
-
*
|
|
|
|
| 202 |
* @category errors
|
|
|
|
| 203 |
*/
|
| 204 |
export class EventLogServerStoreError extends Data.TaggedError("EventLogServerStoreError")<{
|
| 205 |
readonly reason: "NotFound" | "PersistenceFailure"
|
|
@@ -209,8 +240,11 @@ export class EventLogServerStoreError extends Data.TaggedError("EventLogServerSt
|
|
| 209 |
}> {}
|
| 210 |
|
| 211 |
/**
|
| 212 |
-
*
|
|
|
|
|
|
|
| 213 |
* @category errors
|
|
|
|
| 214 |
*/
|
| 215 |
export class EventLogServerAuthError extends Data.TaggedError("EventLogServerAuthError")<{
|
| 216 |
readonly reason: "Unauthorized" | "Forbidden"
|
|
@@ -220,8 +254,16 @@ export class EventLogServerAuthError extends Data.TaggedError("EventLogServerAut
|
|
| 220 |
}> {}
|
| 221 |
|
| 222 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
* @since 4.0.0
|
| 224 |
-
* @category context
|
| 225 |
*/
|
| 226 |
export class EventLogServerAuthorization extends Context.Service<EventLogServerAuthorization, {
|
| 227 |
readonly authorizeWrite: (options: {
|
|
@@ -239,8 +281,16 @@ export class EventLogServerAuthorization extends Context.Service<EventLogServerA
|
|
| 239 |
}>()("effect/eventlog/EventLogServerUnencrypted/EventLogServerAuthorization") {}
|
| 240 |
|
| 241 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
* @since 4.0.0
|
| 243 |
-
* @category context
|
| 244 |
*/
|
| 245 |
export class StoreMapping extends Context.Service<StoreMapping, {
|
| 246 |
readonly resolve: (
|
|
@@ -269,8 +319,11 @@ const toStoreNotFoundError = (options: {
|
|
| 269 |
})
|
| 270 |
|
| 271 |
/**
|
| 272 |
-
*
|
|
|
|
|
|
|
| 273 |
* @category store
|
|
|
|
| 274 |
*/
|
| 275 |
export const layerStoreMappingStatic = (options: {
|
| 276 |
readonly storeId: StoreId
|
|
@@ -286,8 +339,21 @@ export const layerStoreMappingStatic = (options: {
|
|
| 286 |
})
|
| 287 |
|
| 288 |
/**
|
| 289 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
* @category storage
|
|
|
|
| 291 |
*/
|
| 292 |
export class Storage extends Context.Service<Storage, {
|
| 293 |
readonly getId: Effect.Effect<RemoteId>
|
|
@@ -405,8 +471,21 @@ const toCompactedRemoteEntries = (options: {
|
|
| 405 |
}
|
| 406 |
|
| 407 |
/**
|
| 408 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 409 |
* @category compaction
|
|
|
|
| 410 |
*/
|
| 411 |
export const compactBacklog = Effect.fnUntraced(function*(options: {
|
| 412 |
readonly remoteEntries: ReadonlyArray<RemoteEntry>
|
|
@@ -469,8 +548,16 @@ export const compactBacklog = Effect.fnUntraced(function*(options: {
|
|
| 469 |
})
|
| 470 |
|
| 471 |
/**
|
| 472 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 473 |
* @category storage
|
|
|
|
| 474 |
*/
|
| 475 |
export const makeStorageMemory: Effect.Effect<Storage["Service"], never, Scope.Scope> = Effect.gen(function*() {
|
| 476 |
const knownIds = new Map<string, Map<string, number>>()
|
|
@@ -574,14 +661,39 @@ export const makeStorageMemory: Effect.Effect<Storage["Service"], never, Scope.S
|
|
| 574 |
})
|
| 575 |
|
| 576 |
/**
|
| 577 |
-
*
|
|
|
|
| 578 |
* @category storage
|
|
|
|
| 579 |
*/
|
| 580 |
export const layerStorageMemory: Layer.Layer<Storage> = Layer.effect(Storage)(makeStorageMemory)
|
| 581 |
|
| 582 |
/**
|
| 583 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 584 |
* @category constructors
|
|
|
|
| 585 |
*/
|
| 586 |
export const make = Effect.gen(function*() {
|
| 587 |
const storage = yield* Storage
|
|
@@ -640,8 +752,16 @@ export const make = Effect.gen(function*() {
|
|
| 640 |
})
|
| 641 |
|
| 642 |
/**
|
| 643 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 644 |
* @category layers
|
|
|
|
| 645 |
*/
|
| 646 |
export const layerServer: Layer.Layer<
|
| 647 |
EventLogServerUnencrypted | EventLog.Registry,
|
|
@@ -652,8 +772,34 @@ export const layerServer: Layer.Layer<
|
|
| 652 |
)
|
| 653 |
|
| 654 |
/**
|
| 655 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 656 |
* @category layers
|
|
|
|
| 657 |
*/
|
| 658 |
export const layer = <Groups extends EventGroup.Any, E, R>(
|
| 659 |
_schema: EventLog.EventLogSchema<Groups>,
|
|
@@ -674,8 +820,11 @@ export const layer = <Groups extends EventGroup.Any, E, R>(
|
|
| 674 |
)
|
| 675 |
|
| 676 |
/**
|
| 677 |
-
*
|
|
|
|
|
|
|
| 678 |
* @category layers
|
|
|
|
| 679 |
*/
|
| 680 |
export const layerNoRpcServer = <Groups extends EventGroup.Any, E, R>(
|
| 681 |
_schema: EventLog.EventLogSchema<Groups>,
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Plaintext server implementation for the event-log remote protocol.
|
| 3 |
+
*
|
| 4 |
+
* This module accepts unencrypted event batches from remote clients, runs the
|
| 5 |
+
* registered event handlers, stores journal entries, and streams backlog plus
|
| 6 |
+
* live changes through the shared `EventLogServer` RPC protocol. It is intended
|
| 7 |
+
* for trusted deployments, local development, and tests where event data does
|
| 8 |
+
* not need a server-side encryption layer. The module also provides the
|
| 9 |
+
* services and layers needed to authorize requests, map stores, persist entries,
|
| 10 |
+
* and install the plaintext server.
|
| 11 |
+
*
|
| 12 |
* @since 4.0.0
|
| 13 |
*/
|
| 14 |
import * as Arr from "../../Array.ts"
|
|
|
|
| 43 |
import * as EventLogServer from "./EventLogServer.ts"
|
| 44 |
|
| 45 |
/**
|
| 46 |
+
* Service that writes plaintext event-log entries directly to
|
| 47 |
+
* unencrypted storage through registered event handlers.
|
| 48 |
+
*
|
| 49 |
+
* **When to use**
|
| 50 |
+
*
|
| 51 |
+
* Use to access or provide the server service that handles plaintext
|
| 52 |
+
* event-log writes.
|
| 53 |
+
*
|
| 54 |
+
* @category services
|
| 55 |
* @since 4.0.0
|
|
|
|
| 56 |
*/
|
| 57 |
export class EventLogServerUnencrypted extends Context.Service<EventLogServerUnencrypted, {
|
| 58 |
readonly makeWrite: <Groups extends EventGroup.Any>(
|
|
|
|
| 71 |
}>()("effect/eventlog/EventLogServerUnencrypted") {}
|
| 72 |
|
| 73 |
/**
|
| 74 |
+
* Creates a typed server-side write function for events in the supplied
|
| 75 |
+
* `EventLogSchema`.
|
| 76 |
+
*
|
| 77 |
* @category EventLogServerUnencrypted
|
| 78 |
+
* @since 4.0.0
|
| 79 |
*/
|
| 80 |
export const makeWrite = <Groups extends EventGroup.Any>(
|
| 81 |
schema: EventLog.EventLogSchema<Groups>
|
|
|
|
| 96 |
> => EventLogServerUnencrypted.useSync((_) => _.makeWrite(schema))
|
| 97 |
|
| 98 |
/**
|
| 99 |
+
* Provides RPC handlers for the unencrypted event-log server.
|
| 100 |
+
*
|
| 101 |
+
* **Details**
|
| 102 |
+
*
|
| 103 |
+
* Incoming plaintext entries are authorized, mapped to a server store, checked
|
| 104 |
+
* for conflicts, run through registered handlers, and persisted; change streams
|
| 105 |
+
* include compacted backlog entries when compactors are registered.
|
| 106 |
+
*
|
| 107 |
+
* @category layers
|
| 108 |
* @since 4.0.0
|
|
|
|
| 109 |
*/
|
| 110 |
export const layerRpcHandlers: Layer.Layer<
|
| 111 |
Rpc.ToHandler<RpcGroup.Rpcs<typeof EventLogRemoteRpcs>> | EventLogAuthentication,
|
|
|
|
| 227 |
}))
|
| 228 |
|
| 229 |
/**
|
| 230 |
+
* Error raised by unencrypted server storage and store mapping operations.
|
| 231 |
+
*
|
| 232 |
* @category errors
|
| 233 |
+
* @since 4.0.0
|
| 234 |
*/
|
| 235 |
export class EventLogServerStoreError extends Data.TaggedError("EventLogServerStoreError")<{
|
| 236 |
readonly reason: "NotFound" | "PersistenceFailure"
|
|
|
|
| 240 |
}> {}
|
| 241 |
|
| 242 |
/**
|
| 243 |
+
* Error raised when unencrypted server authorization rejects an identity or store
|
| 244 |
+
* operation.
|
| 245 |
+
*
|
| 246 |
* @category errors
|
| 247 |
+
* @since 4.0.0
|
| 248 |
*/
|
| 249 |
export class EventLogServerAuthError extends Data.TaggedError("EventLogServerAuthError")<{
|
| 250 |
readonly reason: "Unauthorized" | "Forbidden"
|
|
|
|
| 254 |
}> {}
|
| 255 |
|
| 256 |
/**
|
| 257 |
+
* Service that validates unencrypted event-log server
|
| 258 |
+
* write access, read access, and identities.
|
| 259 |
+
*
|
| 260 |
+
* **When to use**
|
| 261 |
+
*
|
| 262 |
+
* Use to provide authorization checks for plaintext event-log writes, reads,
|
| 263 |
+
* and identity authentication.
|
| 264 |
+
*
|
| 265 |
+
* @category services
|
| 266 |
* @since 4.0.0
|
|
|
|
| 267 |
*/
|
| 268 |
export class EventLogServerAuthorization extends Context.Service<EventLogServerAuthorization, {
|
| 269 |
readonly authorizeWrite: (options: {
|
|
|
|
| 281 |
}>()("effect/eventlog/EventLogServerUnencrypted/EventLogServerAuthorization") {}
|
| 282 |
|
| 283 |
/**
|
| 284 |
+
* Service that resolves client-requested store ids to server store ids and checks
|
| 285 |
+
* whether a store exists.
|
| 286 |
+
*
|
| 287 |
+
* **When to use**
|
| 288 |
+
*
|
| 289 |
+
* Use to map client-visible store identifiers to server storage identifiers
|
| 290 |
+
* before authorizing or serving unencrypted event-log requests.
|
| 291 |
+
*
|
| 292 |
+
* @category services
|
| 293 |
* @since 4.0.0
|
|
|
|
| 294 |
*/
|
| 295 |
export class StoreMapping extends Context.Service<StoreMapping, {
|
| 296 |
readonly resolve: (
|
|
|
|
| 319 |
})
|
| 320 |
|
| 321 |
/**
|
| 322 |
+
* Provides a `StoreMapping` that accepts only one configured store id and fails
|
| 323 |
+
* all other store ids as not found.
|
| 324 |
+
*
|
| 325 |
* @category store
|
| 326 |
+
* @since 4.0.0
|
| 327 |
*/
|
| 328 |
export const layerStoreMappingStatic = (options: {
|
| 329 |
readonly storeId: StoreId
|
|
|
|
| 339 |
})
|
| 340 |
|
| 341 |
/**
|
| 342 |
+
* Defines the backing store service used by the unencrypted event-log server.
|
| 343 |
+
*
|
| 344 |
+
* **When to use**
|
| 345 |
+
*
|
| 346 |
+
* Use to provide durable event-log persistence for an unencrypted event-log
|
| 347 |
+
* server layer.
|
| 348 |
+
*
|
| 349 |
+
* **Details**
|
| 350 |
+
*
|
| 351 |
+
* It provides the server remote id, stores session authentication bindings,
|
| 352 |
+
* allocates remote sequence numbers, persists entries, streams changes, and
|
| 353 |
+
* exposes a transaction boundary.
|
| 354 |
+
*
|
| 355 |
* @category storage
|
| 356 |
+
* @since 4.0.0
|
| 357 |
*/
|
| 358 |
export class Storage extends Context.Service<Storage, {
|
| 359 |
readonly getId: Effect.Effect<RemoteId>
|
|
|
|
| 471 |
}
|
| 472 |
|
| 473 |
/**
|
| 474 |
+
* Runs the registered compactors over a backlog of remote entries.
|
| 475 |
+
*
|
| 476 |
+
* **When to use**
|
| 477 |
+
*
|
| 478 |
+
* Use to reduce stored remote entries before replaying them to an unencrypted
|
| 479 |
+
* event-log client.
|
| 480 |
+
*
|
| 481 |
+
* **Details**
|
| 482 |
+
*
|
| 483 |
+
* Contiguous entries handled by the same compactor may be replaced with compacted
|
| 484 |
+
* entries when the replacement count can be mapped back to increasing remote
|
| 485 |
+
* sequence numbers; otherwise the original entries are kept.
|
| 486 |
+
*
|
| 487 |
* @category compaction
|
| 488 |
+
* @since 4.0.0
|
| 489 |
*/
|
| 490 |
export const compactBacklog = Effect.fnUntraced(function*(options: {
|
| 491 |
readonly remoteEntries: ReadonlyArray<RemoteEntry>
|
|
|
|
| 548 |
})
|
| 549 |
|
| 550 |
/**
|
| 551 |
+
* Creates an in-memory unencrypted server `Storage`.
|
| 552 |
+
*
|
| 553 |
+
* **Details**
|
| 554 |
+
*
|
| 555 |
+
* The implementation keeps per-store journals and session authentication bindings
|
| 556 |
+
* in memory, publishes live changes, and serializes transactions with a
|
| 557 |
+
* semaphore.
|
| 558 |
+
*
|
| 559 |
* @category storage
|
| 560 |
+
* @since 4.0.0
|
| 561 |
*/
|
| 562 |
export const makeStorageMemory: Effect.Effect<Storage["Service"], never, Scope.Scope> = Effect.gen(function*() {
|
| 563 |
const knownIds = new Map<string, Map<string, number>>()
|
|
|
|
| 661 |
})
|
| 662 |
|
| 663 |
/**
|
| 664 |
+
* Provides unencrypted server `Storage` using the in-memory implementation.
|
| 665 |
+
*
|
| 666 |
* @category storage
|
| 667 |
+
* @since 4.0.0
|
| 668 |
*/
|
| 669 |
export const layerStorageMemory: Layer.Layer<Storage> = Layer.effect(Storage)(makeStorageMemory)
|
| 670 |
|
| 671 |
/**
|
| 672 |
+
* Creates the `EventLogServerUnencrypted` service from the configured storage and
|
| 673 |
+
* registered event handlers.
|
| 674 |
+
*
|
| 675 |
+
* **When to use**
|
| 676 |
+
*
|
| 677 |
+
* Use when you need the unencrypted event-log server service from provided
|
| 678 |
+
* `Storage` and an event-log `Registry`.
|
| 679 |
+
*
|
| 680 |
+
* **Details**
|
| 681 |
+
*
|
| 682 |
+
* The constructed service exposes `makeWrite`, which builds a typed server-side
|
| 683 |
+
* write function from an `EventLogSchema`. Each write encodes the payload with
|
| 684 |
+
* the event schema, runs the registered handler, and persists the generated
|
| 685 |
+
* entry inside `Storage.withTransaction`.
|
| 686 |
+
*
|
| 687 |
+
* **Gotchas**
|
| 688 |
+
*
|
| 689 |
+
* The write function dies if the requested event tag is not present in the
|
| 690 |
+
* schema passed to `makeWrite`; it does not report that case as a typed failure.
|
| 691 |
+
*
|
| 692 |
+
* @see {@link makeWrite} for the accessor that retrieves the typed server-side write function from the service environment
|
| 693 |
+
* @see {@link layerServer} for the layer form that provides this service together with an event-log `Registry`
|
| 694 |
+
*
|
| 695 |
* @category constructors
|
| 696 |
+
* @since 4.0.0
|
| 697 |
*/
|
| 698 |
export const make = Effect.gen(function*() {
|
| 699 |
const storage = yield* Storage
|
|
|
|
| 752 |
})
|
| 753 |
|
| 754 |
/**
|
| 755 |
+
* Provides `EventLogServerUnencrypted` and an event-log `Registry` using the
|
| 756 |
+
* configured unencrypted server `Storage`.
|
| 757 |
+
*
|
| 758 |
+
* **When to use**
|
| 759 |
+
*
|
| 760 |
+
* Use to provide the unencrypted event-log server service together with the
|
| 761 |
+
* registry needed by event handlers.
|
| 762 |
+
*
|
| 763 |
* @category layers
|
| 764 |
+
* @since 4.0.0
|
| 765 |
*/
|
| 766 |
export const layerServer: Layer.Layer<
|
| 767 |
EventLogServerUnencrypted | EventLog.Registry,
|
|
|
|
| 772 |
)
|
| 773 |
|
| 774 |
/**
|
| 775 |
+
* Builds a full unencrypted event-log RPC server for the supplied schema and
|
| 776 |
+
* event-group handler layer.
|
| 777 |
+
*
|
| 778 |
+
* **When to use**
|
| 779 |
+
*
|
| 780 |
+
* Use when you need the full unencrypted event-log RPC server layer with
|
| 781 |
+
* storage, authorization, RPC protocol, and event-group handler dependencies
|
| 782 |
+
* supplied externally.
|
| 783 |
+
*
|
| 784 |
+
* **Details**
|
| 785 |
+
*
|
| 786 |
+
* The layer installs `EventLogRemoteRpcs`, wires `layerRpcHandlers`, registers
|
| 787 |
+
* the supplied event-group handler layer, and provides `layerServer`, leaving
|
| 788 |
+
* only the required infrastructure services in the environment.
|
| 789 |
+
*
|
| 790 |
+
* **Gotchas**
|
| 791 |
+
*
|
| 792 |
+
* Entries are persisted and streamed in plaintext. Protect the backing
|
| 793 |
+
* `Storage` with the surrounding infrastructure, and use durable storage that
|
| 794 |
+
* preserves session authentication bindings when the server must survive
|
| 795 |
+
* restarts.
|
| 796 |
+
*
|
| 797 |
+
* @see {@link layerNoRpcServer} for installing the same unencrypted handlers when an `RpcServer.Protocol` is provided elsewhere
|
| 798 |
+
* @see {@link layerRpcHandlers} for wiring the unencrypted RPC handlers directly
|
| 799 |
+
* @see {@link layerServer} for constructing the server service and event-log registry without RPC handlers
|
| 800 |
+
*
|
| 801 |
* @category layers
|
| 802 |
+
* @since 4.0.0
|
| 803 |
*/
|
| 804 |
export const layer = <Groups extends EventGroup.Any, E, R>(
|
| 805 |
_schema: EventLog.EventLogSchema<Groups>,
|
|
|
|
| 820 |
)
|
| 821 |
|
| 822 |
/**
|
| 823 |
+
* Builds the unencrypted event-log server handlers without installing an
|
| 824 |
+
* `RpcServer.Protocol` implementation.
|
| 825 |
+
*
|
| 826 |
* @category layers
|
| 827 |
+
* @since 4.0.0
|
| 828 |
*/
|
| 829 |
export const layerNoRpcServer = <Groups extends EventGroup.Any, E, R>(
|
| 830 |
_schema: EventLog.EventLogSchema<Groups>,
|
.opencode/node_modules/effect/src/unstable/eventlog/EventLogSessionAuth.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Data from "../../Data.ts"
|
|
@@ -10,38 +17,79 @@ const textDecoder = new TextDecoder("utf-8", { fatal: true })
|
|
| 10 |
const constLengthPrefixBytes = 4
|
| 11 |
|
| 12 |
/**
|
| 13 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
* @category constants
|
|
|
|
| 15 |
*/
|
| 16 |
export const AuthPayloadContext = "eventlog-auth-v1"
|
| 17 |
|
| 18 |
/**
|
| 19 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
* @category constants
|
|
|
|
| 21 |
*/
|
| 22 |
export const Ed25519PublicKeyLength = 32
|
| 23 |
|
| 24 |
/**
|
| 25 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
* @category constants
|
|
|
|
| 27 |
*/
|
| 28 |
export const Ed25519SignatureLength = 64
|
| 29 |
|
| 30 |
/**
|
| 31 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
* @category constants
|
|
|
|
| 33 |
*/
|
| 34 |
export const SessionAuthChallengeLength = 32
|
| 35 |
|
| 36 |
/**
|
| 37 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
* @category constants
|
|
|
|
| 39 |
*/
|
| 40 |
export const SessionAuthChallengeTimeToLiveMillis = 30_000
|
| 41 |
|
| 42 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
* @since 4.0.0
|
| 44 |
-
* @category model
|
| 45 |
*/
|
| 46 |
export interface SessionAuthPayload {
|
| 47 |
readonly remoteId: string | Uint8Array
|
|
@@ -51,8 +99,11 @@ export interface SessionAuthPayload {
|
|
| 51 |
}
|
| 52 |
|
| 53 |
/**
|
| 54 |
-
*
|
|
|
|
|
|
|
| 55 |
* @category errors
|
|
|
|
| 56 |
*/
|
| 57 |
export class EventLogSessionAuthError extends Data.TaggedError("EventLogSessionAuthError")<{
|
| 58 |
readonly reason:
|
|
@@ -206,7 +257,11 @@ const encodeRemoteIdField = (remoteId: string | Uint8Array): Uint8Array =>
|
|
| 206 |
: textEncoder.encode(bytesToHex(remoteId))
|
| 207 |
|
| 208 |
/**
|
| 209 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
*
|
| 211 |
* 1. context (fixed: eventlog-auth-v1)
|
| 212 |
* 2. remoteId
|
|
@@ -214,8 +269,8 @@ const encodeRemoteIdField = (remoteId: string | Uint8Array): Uint8Array =>
|
|
| 214 |
* 4. publicKey
|
| 215 |
* 5. signingPublicKey bytes
|
| 216 |
*
|
| 217 |
-
* @since 4.0.0
|
| 218 |
* @category encoding
|
|
|
|
| 219 |
*/
|
| 220 |
export const encodeSessionAuthPayload = Effect.fnUntraced(function*(payload: SessionAuthPayload) {
|
| 221 |
yield* assertSigningPublicKeyLength(payload.signingPublicKey)
|
|
@@ -245,8 +300,15 @@ export const encodeSessionAuthPayload = Effect.fnUntraced(function*(payload: Ses
|
|
| 245 |
})
|
| 246 |
|
| 247 |
/**
|
| 248 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
* @category encoding
|
|
|
|
| 250 |
*/
|
| 251 |
export const decodeSessionAuthPayload = Effect.fnUntraced(
|
| 252 |
function*(payload: Uint8Array): Effect.fn.Return<SessionAuthPayload, EventLogSessionAuthError> {
|
|
@@ -283,8 +345,14 @@ export const decodeSessionAuthPayload = Effect.fnUntraced(
|
|
| 283 |
)
|
| 284 |
|
| 285 |
/**
|
| 286 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
* @category signing
|
|
|
|
| 288 |
*/
|
| 289 |
export const signSessionAuthPayloadBytes = Effect.fnUntraced(function*(options: {
|
| 290 |
readonly payload: Uint8Array
|
|
@@ -323,8 +391,16 @@ export const signSessionAuthPayloadBytes = Effect.fnUntraced(function*(options:
|
|
| 323 |
})
|
| 324 |
|
| 325 |
/**
|
| 326 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
* @category verification
|
|
|
|
| 328 |
*/
|
| 329 |
export const verifySessionAuthPayloadBytes = Effect.fnUntraced(function*(options: {
|
| 330 |
readonly payload: Uint8Array
|
|
@@ -358,8 +434,11 @@ export const verifySessionAuthPayloadBytes = Effect.fnUntraced(function*(options
|
|
| 358 |
})
|
| 359 |
|
| 360 |
/**
|
| 361 |
-
*
|
|
|
|
|
|
|
| 362 |
* @category signing
|
|
|
|
| 363 |
*/
|
| 364 |
export const signSessionAuthPayload = (
|
| 365 |
options: SessionAuthPayload & {
|
|
@@ -376,8 +455,11 @@ export const signSessionAuthPayload = (
|
|
| 376 |
)
|
| 377 |
|
| 378 |
/**
|
| 379 |
-
*
|
|
|
|
|
|
|
| 380 |
* @category verification
|
|
|
|
| 381 |
*/
|
| 382 |
export const verifySessionAuthPayload = (
|
| 383 |
options: SessionAuthPayload & {
|
|
@@ -395,8 +477,10 @@ export const verifySessionAuthPayload = (
|
|
| 395 |
)
|
| 396 |
|
| 397 |
/**
|
| 398 |
-
*
|
|
|
|
| 399 |
* @category challenge
|
|
|
|
| 400 |
*/
|
| 401 |
export const makeSessionAuthChallenge: Effect.Effect<
|
| 402 |
Uint8Array<ArrayBuffer>,
|
|
@@ -409,8 +493,11 @@ export const makeSessionAuthChallenge: Effect.Effect<
|
|
| 409 |
})
|
| 410 |
|
| 411 |
/**
|
| 412 |
-
*
|
|
|
|
|
|
|
| 413 |
* @category verification
|
|
|
|
| 414 |
*/
|
| 415 |
export const verifySessionAuthenticateRequest = Effect.fnUntraced(function*(options: {
|
| 416 |
readonly remoteId: string | Uint8Array
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Signs and verifies event-log session authentication payloads.
|
| 3 |
+
*
|
| 4 |
+
* Remote peers use this challenge-response flow to prove that they control a
|
| 5 |
+
* session signing key before sending session traffic. The signed payload
|
| 6 |
+
* includes the remote id, a short-lived challenge, the event-log public key, and
|
| 7 |
+
* the signing public key in a stable byte format.
|
| 8 |
+
*
|
| 9 |
* @since 4.0.0
|
| 10 |
*/
|
| 11 |
import * as Data from "../../Data.ts"
|
|
|
|
| 17 |
const constLengthPrefixBytes = 4
|
| 18 |
|
| 19 |
/**
|
| 20 |
+
* Defines the domain-separation string embedded in canonical session
|
| 21 |
+
* authentication payloads.
|
| 22 |
+
*
|
| 23 |
+
* **When to use**
|
| 24 |
+
*
|
| 25 |
+
* Use when you need the domain-separation string used to build canonical
|
| 26 |
+
* event-log session authentication payloads.
|
| 27 |
+
*
|
| 28 |
* @category constants
|
| 29 |
+
* @since 4.0.0
|
| 30 |
*/
|
| 31 |
export const AuthPayloadContext = "eventlog-auth-v1"
|
| 32 |
|
| 33 |
/**
|
| 34 |
+
* Defines the required byte length for raw Ed25519 public keys used in session
|
| 35 |
+
* authentication.
|
| 36 |
+
*
|
| 37 |
+
* **When to use**
|
| 38 |
+
*
|
| 39 |
+
* Use when implementing session-auth serialization or validation that must
|
| 40 |
+
* reject public keys with a non-canonical raw byte length.
|
| 41 |
+
*
|
| 42 |
* @category constants
|
| 43 |
+
* @since 4.0.0
|
| 44 |
*/
|
| 45 |
export const Ed25519PublicKeyLength = 32
|
| 46 |
|
| 47 |
/**
|
| 48 |
+
* Defines the required byte length for Ed25519 signatures used in session authentication.
|
| 49 |
+
*
|
| 50 |
+
* **When to use**
|
| 51 |
+
*
|
| 52 |
+
* Use when implementing session-auth verification that must reject signatures
|
| 53 |
+
* with a non-canonical byte length before cryptographic checking.
|
| 54 |
+
*
|
| 55 |
* @category constants
|
| 56 |
+
* @since 4.0.0
|
| 57 |
*/
|
| 58 |
export const Ed25519SignatureLength = 64
|
| 59 |
|
| 60 |
/**
|
| 61 |
+
* Defines the number of random bytes generated for a session authentication
|
| 62 |
+
* challenge.
|
| 63 |
+
*
|
| 64 |
+
* **When to use**
|
| 65 |
+
*
|
| 66 |
+
* Use when you need the challenge size for event-log session authentication.
|
| 67 |
+
*
|
| 68 |
* @category constants
|
| 69 |
+
* @since 4.0.0
|
| 70 |
*/
|
| 71 |
export const SessionAuthChallengeLength = 32
|
| 72 |
|
| 73 |
/**
|
| 74 |
+
* Defines the time-to-live, in milliseconds, for a pending session
|
| 75 |
+
* authentication challenge.
|
| 76 |
+
*
|
| 77 |
+
* **When to use**
|
| 78 |
+
*
|
| 79 |
+
* Use when you need the timeout for pending event-log session authentication
|
| 80 |
+
* challenges.
|
| 81 |
+
*
|
| 82 |
* @category constants
|
| 83 |
+
* @since 4.0.0
|
| 84 |
*/
|
| 85 |
export const SessionAuthChallengeTimeToLiveMillis = 30_000
|
| 86 |
|
| 87 |
/**
|
| 88 |
+
* Payload fields that are canonicalized and signed during session
|
| 89 |
+
* authentication.
|
| 90 |
+
*
|
| 91 |
+
* @category models
|
| 92 |
* @since 4.0.0
|
|
|
|
| 93 |
*/
|
| 94 |
export interface SessionAuthPayload {
|
| 95 |
readonly remoteId: string | Uint8Array
|
|
|
|
| 99 |
}
|
| 100 |
|
| 101 |
/**
|
| 102 |
+
* Error raised while encoding, decoding, signing, verifying, or generating
|
| 103 |
+
* session authentication challenges.
|
| 104 |
+
*
|
| 105 |
* @category errors
|
| 106 |
+
* @since 4.0.0
|
| 107 |
*/
|
| 108 |
export class EventLogSessionAuthError extends Data.TaggedError("EventLogSessionAuthError")<{
|
| 109 |
readonly reason:
|
|
|
|
| 257 |
: textEncoder.encode(bytesToHex(remoteId))
|
| 258 |
|
| 259 |
/**
|
| 260 |
+
* Encodes a session authentication payload into the canonical byte format.
|
| 261 |
+
*
|
| 262 |
+
* **Details**
|
| 263 |
+
*
|
| 264 |
+
* The canonical payload format uses ordered big-endian length-prefixed fields:
|
| 265 |
*
|
| 266 |
* 1. context (fixed: eventlog-auth-v1)
|
| 267 |
* 2. remoteId
|
|
|
|
| 269 |
* 4. publicKey
|
| 270 |
* 5. signingPublicKey bytes
|
| 271 |
*
|
|
|
|
| 272 |
* @category encoding
|
| 273 |
+
* @since 4.0.0
|
| 274 |
*/
|
| 275 |
export const encodeSessionAuthPayload = Effect.fnUntraced(function*(payload: SessionAuthPayload) {
|
| 276 |
yield* assertSigningPublicKeyLength(payload.signingPublicKey)
|
|
|
|
| 300 |
})
|
| 301 |
|
| 302 |
/**
|
| 303 |
+
* Decodes a canonical session authentication payload.
|
| 304 |
+
*
|
| 305 |
+
* **Details**
|
| 306 |
+
*
|
| 307 |
+
* The decoder validates the context field, UTF-8 fields, signing public key
|
| 308 |
+
* length, and rejects truncated or trailing bytes.
|
| 309 |
+
*
|
| 310 |
* @category encoding
|
| 311 |
+
* @since 4.0.0
|
| 312 |
*/
|
| 313 |
export const decodeSessionAuthPayload = Effect.fnUntraced(
|
| 314 |
function*(payload: Uint8Array): Effect.fn.Return<SessionAuthPayload, EventLogSessionAuthError> {
|
|
|
|
| 345 |
)
|
| 346 |
|
| 347 |
/**
|
| 348 |
+
* Creates a canonical session authentication signature with an Ed25519 private key.
|
| 349 |
+
*
|
| 350 |
+
* **Details**
|
| 351 |
+
*
|
| 352 |
+
* The private key must be PKCS#8-encoded bytes importable by `SubtleCrypto`.
|
| 353 |
+
*
|
| 354 |
* @category signing
|
| 355 |
+
* @since 4.0.0
|
| 356 |
*/
|
| 357 |
export const signSessionAuthPayloadBytes = Effect.fnUntraced(function*(options: {
|
| 358 |
readonly payload: Uint8Array
|
|
|
|
| 391 |
})
|
| 392 |
|
| 393 |
/**
|
| 394 |
+
* Verifies an Ed25519 signature for canonical session authentication payload
|
| 395 |
+
* bytes.
|
| 396 |
+
*
|
| 397 |
+
* **Details**
|
| 398 |
+
*
|
| 399 |
+
* The payload, signing public key, and signature lengths are validated before
|
| 400 |
+
* calling `SubtleCrypto.verify`.
|
| 401 |
+
*
|
| 402 |
* @category verification
|
| 403 |
+
* @since 4.0.0
|
| 404 |
*/
|
| 405 |
export const verifySessionAuthPayloadBytes = Effect.fnUntraced(function*(options: {
|
| 406 |
readonly payload: Uint8Array
|
|
|
|
| 434 |
})
|
| 435 |
|
| 436 |
/**
|
| 437 |
+
* Encodes a session authentication payload in canonical form and signs it with an
|
| 438 |
+
* Ed25519 private key.
|
| 439 |
+
*
|
| 440 |
* @category signing
|
| 441 |
+
* @since 4.0.0
|
| 442 |
*/
|
| 443 |
export const signSessionAuthPayload = (
|
| 444 |
options: SessionAuthPayload & {
|
|
|
|
| 455 |
)
|
| 456 |
|
| 457 |
/**
|
| 458 |
+
* Encodes a session authentication payload in canonical form and verifies its
|
| 459 |
+
* Ed25519 signature.
|
| 460 |
+
*
|
| 461 |
* @category verification
|
| 462 |
+
* @since 4.0.0
|
| 463 |
*/
|
| 464 |
export const verifySessionAuthPayload = (
|
| 465 |
options: SessionAuthPayload & {
|
|
|
|
| 477 |
)
|
| 478 |
|
| 479 |
/**
|
| 480 |
+
* Generates a random session authentication challenge using `globalThis.crypto`.
|
| 481 |
+
*
|
| 482 |
* @category challenge
|
| 483 |
+
* @since 4.0.0
|
| 484 |
*/
|
| 485 |
export const makeSessionAuthChallenge: Effect.Effect<
|
| 486 |
Uint8Array<ArrayBuffer>,
|
|
|
|
| 493 |
})
|
| 494 |
|
| 495 |
/**
|
| 496 |
+
* Verifies an authentication request by requiring the `Ed25519` algorithm and
|
| 497 |
+
* checking the signature over the canonical session authentication payload.
|
| 498 |
+
*
|
| 499 |
* @category verification
|
| 500 |
+
* @since 4.0.0
|
| 501 |
*/
|
| 502 |
export const verifySessionAuthenticateRequest = Effect.fnUntraced(function*(options: {
|
| 503 |
readonly remoteId: string | Uint8Array
|
.opencode/node_modules/effect/src/unstable/eventlog/SqlEventJournal.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Uuid from "uuid"
|
|
@@ -14,8 +21,15 @@ import * as EventJournal from "./EventJournal.ts"
|
|
| 14 |
type WriteFromRemoteOptions = Parameters<EventJournal.EventJournal["Service"]["writeFromRemote"]>[0]
|
| 15 |
|
| 16 |
/**
|
| 17 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
* @category constructors
|
|
|
|
| 19 |
*/
|
| 20 |
export const make = (options?: {
|
| 21 |
readonly entryTable?: string
|
|
@@ -267,8 +281,32 @@ export const make = (options?: {
|
|
| 267 |
})
|
| 268 |
|
| 269 |
/**
|
| 270 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
* @category layers
|
|
|
|
| 272 |
*/
|
| 273 |
export const layer = (options?: {
|
| 274 |
readonly entryTable?: string
|
|
|
|
| 1 |
/**
|
| 2 |
+
* SQL-backed persistence for the unstable event-log journal.
|
| 3 |
+
*
|
| 4 |
+
* This module implements `EventJournal` on top of a `SqlClient`. It stores event
|
| 5 |
+
* entries as encoded bytes and stores per-remote sequence metadata in separate
|
| 6 |
+
* tables, giving event-log programs a durable journal that can be replayed after
|
| 7 |
+
* restart and synchronized with remote journals.
|
| 8 |
+
*
|
| 9 |
* @since 4.0.0
|
| 10 |
*/
|
| 11 |
import * as Uuid from "uuid"
|
|
|
|
| 21 |
type WriteFromRemoteOptions = Parameters<EventJournal.EventJournal["Service"]["writeFromRemote"]>[0]
|
| 22 |
|
| 23 |
/**
|
| 24 |
+
* Creates an `EventJournal` backed by a SQL database.
|
| 25 |
+
*
|
| 26 |
+
* **Details**
|
| 27 |
+
*
|
| 28 |
+
* The constructor creates the entry and remote metadata tables when needed,
|
| 29 |
+
* persists local and remote entries, and uses the configured `SqlClient`.
|
| 30 |
+
*
|
| 31 |
* @category constructors
|
| 32 |
+
* @since 4.0.0
|
| 33 |
*/
|
| 34 |
export const make = (options?: {
|
| 35 |
readonly entryTable?: string
|
|
|
|
| 281 |
})
|
| 282 |
|
| 283 |
/**
|
| 284 |
+
* Provides `EventJournal` using the SQL-backed implementation created by
|
| 285 |
+
* `make`.
|
| 286 |
+
*
|
| 287 |
+
* **When to use**
|
| 288 |
+
*
|
| 289 |
+
* Use when composing a Layer graph that should provide a persistent SQL-backed
|
| 290 |
+
* `EventJournal` from an existing `SqlClient` service.
|
| 291 |
+
*
|
| 292 |
+
* **Details**
|
| 293 |
+
*
|
| 294 |
+
* The layer delegates to `make(options)`, so the same optional `entryTable` and
|
| 295 |
+
* `remotesTable` settings are used and construction requires `SqlClient` and
|
| 296 |
+
* may fail with `SqlError`.
|
| 297 |
+
*
|
| 298 |
+
* **Gotchas**
|
| 299 |
+
*
|
| 300 |
+
* Layer construction performs the same minimal `CREATE TABLE IF NOT EXISTS`
|
| 301 |
+
* setup as `make`; manage indexes and schema migrations outside this layer when
|
| 302 |
+
* your SQL schema needs more than the built-in tables.
|
| 303 |
+
*
|
| 304 |
+
* @see {@link make} for constructing the SQL-backed service directly
|
| 305 |
+
* @see {@link EventJournal.layerMemory} for an in-memory `EventJournal` layer
|
| 306 |
+
* @see {@link EventJournal.layerIndexedDb} for an IndexedDB-backed `EventJournal` layer
|
| 307 |
+
*
|
| 308 |
* @category layers
|
| 309 |
+
* @since 4.0.0
|
| 310 |
*/
|
| 311 |
export const layer = (options?: {
|
| 312 |
readonly entryTable?: string
|
.opencode/node_modules/effect/src/unstable/eventlog/SqlEventLogServerEncrypted.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Effect from "../../Effect.ts"
|
|
@@ -15,8 +24,16 @@ import * as EventLogEncryption from "./EventLogEncryption.ts"
|
|
| 15 |
import * as EventLogServerEncrypted from "./EventLogServerEncrypted.ts"
|
| 16 |
|
| 17 |
/**
|
| 18 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
* @category constructors
|
|
|
|
| 20 |
*/
|
| 21 |
export const makeStorage = (options?: {
|
| 22 |
readonly entryTablePrefix?: string
|
|
@@ -287,8 +304,10 @@ const decodeSessionAuthBindings = (
|
|
| 287 |
): Effect.Effect<ReadonlyArray<SessionAuthBindingSql>, Schema.SchemaError> => decodeSessionAuthBindingRows(rows)
|
| 288 |
|
| 289 |
/**
|
| 290 |
-
*
|
|
|
|
| 291 |
* @category layers
|
|
|
|
| 292 |
*/
|
| 293 |
export const layerStorage = (options?: {
|
| 294 |
readonly entryTablePrefix?: string
|
|
@@ -301,8 +320,11 @@ export const layerStorage = (options?: {
|
|
| 301 |
> => Layer.effect(EventLogServerEncrypted.Storage)(makeStorage(options))
|
| 302 |
|
| 303 |
/**
|
| 304 |
-
*
|
|
|
|
|
|
|
| 305 |
* @category layers
|
|
|
|
| 306 |
*/
|
| 307 |
export const layerStorageSubtle = (options?: {
|
| 308 |
readonly entryTablePrefix?: string
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Stores encrypted event-log server state in SQL.
|
| 3 |
+
*
|
| 4 |
+
* This module provides the durable `Storage` implementation used by
|
| 5 |
+
* `EventLogServerEncrypted` when entries should be stored without exposing
|
| 6 |
+
* plaintext event data to the database. It persists the server remote id,
|
| 7 |
+
* session authentication bindings, and encrypted entry tables, assigns stable
|
| 8 |
+
* sequence numbers, and streams changes. Clients remain responsible for
|
| 9 |
+
* encrypting writes and decrypting reads.
|
| 10 |
+
*
|
| 11 |
* @since 4.0.0
|
| 12 |
*/
|
| 13 |
import * as Effect from "../../Effect.ts"
|
|
|
|
| 24 |
import * as EventLogServerEncrypted from "./EventLogServerEncrypted.ts"
|
| 25 |
|
| 26 |
/**
|
| 27 |
+
* Creates encrypted event-log server `Storage` backed by SQL.
|
| 28 |
+
*
|
| 29 |
+
* **Details**
|
| 30 |
+
*
|
| 31 |
+
* It persists the server remote id, session authentication bindings, and encrypted
|
| 32 |
+
* entries in dialect-specific tables, creating per-identity/store entry tables as
|
| 33 |
+
* needed.
|
| 34 |
+
*
|
| 35 |
* @category constructors
|
| 36 |
+
* @since 4.0.0
|
| 37 |
*/
|
| 38 |
export const makeStorage = (options?: {
|
| 39 |
readonly entryTablePrefix?: string
|
|
|
|
| 304 |
): Effect.Effect<ReadonlyArray<SessionAuthBindingSql>, Schema.SchemaError> => decodeSessionAuthBindingRows(rows)
|
| 305 |
|
| 306 |
/**
|
| 307 |
+
* Provides encrypted server `Storage` using the SQL-backed implementation.
|
| 308 |
+
*
|
| 309 |
* @category layers
|
| 310 |
+
* @since 4.0.0
|
| 311 |
*/
|
| 312 |
export const layerStorage = (options?: {
|
| 313 |
readonly entryTablePrefix?: string
|
|
|
|
| 320 |
> => Layer.effect(EventLogServerEncrypted.Storage)(makeStorage(options))
|
| 321 |
|
| 322 |
/**
|
| 323 |
+
* Provides SQL-backed encrypted server `Storage` and supplies the default Web
|
| 324 |
+
* Crypto `EventLogEncryption` layer.
|
| 325 |
+
*
|
| 326 |
* @category layers
|
| 327 |
+
* @since 4.0.0
|
| 328 |
*/
|
| 329 |
export const layerStorageSubtle = (options?: {
|
| 330 |
readonly entryTablePrefix?: string
|
.opencode/node_modules/effect/src/unstable/eventlog/SqlEventLogServerUnencrypted.ts
CHANGED
|
@@ -1,4 +1,12 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Arr from "../../Array.ts"
|
|
@@ -15,8 +23,16 @@ import { Entry, EntryId, makeRemoteIdUnsafe, RemoteEntry, type RemoteId } from "
|
|
| 15 |
import * as EventLogServerUnencrypted from "./EventLogServerUnencrypted.ts"
|
| 16 |
|
| 17 |
/**
|
| 18 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
* @category constructors
|
|
|
|
| 20 |
*/
|
| 21 |
export const makeStorage = (options?: {
|
| 22 |
readonly entryTablePrefix?: string
|
|
@@ -424,8 +440,10 @@ export const makeStorage = (options?: {
|
|
| 424 |
}).pipe(withTracerDisabled)
|
| 425 |
|
| 426 |
/**
|
| 427 |
-
*
|
|
|
|
| 428 |
* @category layers
|
|
|
|
| 429 |
*/
|
| 430 |
export const layerStorage = (options?: {
|
| 431 |
readonly entryTablePrefix?: string
|
|
|
|
| 1 |
/**
|
| 2 |
+
* SQL-backed storage for unencrypted event-log servers.
|
| 3 |
+
*
|
| 4 |
+
* This module provides the durable `Storage` implementation used by
|
| 5 |
+
* `EventLogServerUnencrypted` when remote entries should be stored in a SQL
|
| 6 |
+
* database and streamed back to clients by store sequence. It creates
|
| 7 |
+
* dialect-specific tables for the server remote id, per-store sequence state,
|
| 8 |
+
* plaintext entries, and session authentication bindings.
|
| 9 |
+
*
|
| 10 |
* @since 4.0.0
|
| 11 |
*/
|
| 12 |
import * as Arr from "../../Array.ts"
|
|
|
|
| 23 |
import * as EventLogServerUnencrypted from "./EventLogServerUnencrypted.ts"
|
| 24 |
|
| 25 |
/**
|
| 26 |
+
* Creates unencrypted event-log server `Storage` backed by SQL.
|
| 27 |
+
*
|
| 28 |
+
* **Details**
|
| 29 |
+
*
|
| 30 |
+
* The implementation creates tables for the server remote id, store sequences,
|
| 31 |
+
* entries, and session authentication bindings, then persists and streams
|
| 32 |
+
* plaintext remote entries.
|
| 33 |
+
*
|
| 34 |
* @category constructors
|
| 35 |
+
* @since 4.0.0
|
| 36 |
*/
|
| 37 |
export const makeStorage = (options?: {
|
| 38 |
readonly entryTablePrefix?: string
|
|
|
|
| 440 |
}).pipe(withTracerDisabled)
|
| 441 |
|
| 442 |
/**
|
| 443 |
+
* Provides unencrypted server `Storage` using the SQL-backed implementation.
|
| 444 |
+
*
|
| 445 |
* @category layers
|
| 446 |
+
* @since 4.0.0
|
| 447 |
*/
|
| 448 |
export const layerStorage = (options?: {
|
| 449 |
readonly entryTablePrefix?: string
|
.opencode/node_modules/effect/src/unstable/http/Cookies.ts
CHANGED
|
@@ -1,4 +1,12 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Data from "../../Data.ts"
|
|
@@ -11,20 +19,24 @@ import * as Predicate from "../../Predicate.ts"
|
|
| 11 |
import * as Record from "../../Record.ts"
|
| 12 |
import * as Result from "../../Result.ts"
|
| 13 |
import * as Schema from "../../Schema.ts"
|
| 14 |
-
import * as
|
| 15 |
import type * as Types from "../../Types.ts"
|
| 16 |
|
| 17 |
const TypeId = "~effect/http/Cookies"
|
| 18 |
|
| 19 |
/**
|
| 20 |
-
*
|
|
|
|
| 21 |
* @category refinements
|
|
|
|
| 22 |
*/
|
| 23 |
export const isCookies = (u: unknown): u is Cookies => Predicate.hasProperty(u, TypeId)
|
| 24 |
|
| 25 |
/**
|
| 26 |
-
*
|
|
|
|
| 27 |
* @category models
|
|
|
|
| 28 |
*/
|
| 29 |
export interface Cookies extends Pipeable, Inspectable.Inspectable {
|
| 30 |
readonly [TypeId]: typeof TypeId
|
|
@@ -32,14 +44,23 @@ export interface Cookies extends Pipeable, Inspectable.Inspectable {
|
|
| 32 |
}
|
| 33 |
|
| 34 |
/**
|
|
|
|
|
|
|
|
|
|
| 35 |
* @since 4.0.0
|
| 36 |
-
* @category Schemas
|
| 37 |
*/
|
| 38 |
export interface CookiesSchema extends Schema.declare<Cookies, Record.ReadonlyRecord<string, Cookie>> {}
|
| 39 |
|
| 40 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
* @since 4.0.0
|
| 42 |
-
* @category Schemas
|
| 43 |
*/
|
| 44 |
export const CookiesSchema: CookiesSchema = Schema.declare(
|
| 45 |
isCookies,
|
|
@@ -57,7 +78,7 @@ export const CookiesSchema: CookiesSchema = Schema.declare(
|
|
| 57 |
toCodecJson: () =>
|
| 58 |
Schema.link<Cookies>()(
|
| 59 |
Schema.Array(Schema.String),
|
| 60 |
-
|
| 61 |
decode: (input) => fromSetCookie(input),
|
| 62 |
encode: (cookies) => toSetCookieHeaders(cookies)
|
| 63 |
})
|
|
@@ -65,7 +86,7 @@ export const CookiesSchema: CookiesSchema = Schema.declare(
|
|
| 65 |
toCodecIso: () =>
|
| 66 |
Schema.link<Cookies>()(
|
| 67 |
Schema.Record(Schema.String, CookieSchema),
|
| 68 |
-
|
| 69 |
decode: (input) => fromReadonlyRecord(input),
|
| 70 |
encode: (cookies) => cookies.cookies
|
| 71 |
})
|
|
@@ -76,8 +97,11 @@ export const CookiesSchema: CookiesSchema = Schema.declare(
|
|
| 76 |
const CookieTypeId = "~effect/http/Cookies/Cookie"
|
| 77 |
|
| 78 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
* @since 4.0.0
|
| 80 |
-
* @category cookie
|
| 81 |
*/
|
| 82 |
export interface Cookie extends Inspectable.Inspectable {
|
| 83 |
readonly [CookieTypeId]: typeof CookieTypeId
|
|
@@ -98,20 +122,26 @@ export interface Cookie extends Inspectable.Inspectable {
|
|
| 98 |
}
|
| 99 |
|
| 100 |
/**
|
|
|
|
|
|
|
|
|
|
| 101 |
* @since 4.0.0
|
| 102 |
-
* @category Guards
|
| 103 |
*/
|
| 104 |
export const isCookie = (u: unknown): u is Cookie => Predicate.hasProperty(u, CookieTypeId)
|
| 105 |
|
| 106 |
/**
|
|
|
|
|
|
|
|
|
|
| 107 |
* @since 4.0.0
|
| 108 |
-
* @category Schemas
|
| 109 |
*/
|
| 110 |
export interface CookieSchema extends Schema.declare<Cookie> {}
|
| 111 |
|
| 112 |
/**
|
|
|
|
|
|
|
|
|
|
| 113 |
* @since 4.0.0
|
| 114 |
-
* @category Schemas
|
| 115 |
*/
|
| 116 |
export const CookieSchema: CookieSchema = Schema.declare(
|
| 117 |
isCookie,
|
|
@@ -131,8 +161,11 @@ export const CookieSchema: CookieSchema = Schema.declare(
|
|
| 131 |
const CookieErrorTypeId = "~effect/http/Cookies/CookieError"
|
| 132 |
|
| 133 |
/**
|
| 134 |
-
*
|
|
|
|
|
|
|
| 135 |
* @category errors
|
|
|
|
| 136 |
*/
|
| 137 |
export class CookiesErrorReason extends Data.Error<{
|
| 138 |
readonly _tag:
|
|
@@ -145,13 +178,21 @@ export class CookiesErrorReason extends Data.Error<{
|
|
| 145 |
}> {}
|
| 146 |
|
| 147 |
/**
|
| 148 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
* @category errors
|
|
|
|
| 150 |
*/
|
| 151 |
export class CookiesError extends Data.TaggedError("CookieError")<{
|
| 152 |
readonly reason: CookiesErrorReason
|
| 153 |
}> {
|
| 154 |
/**
|
|
|
|
|
|
|
| 155 |
* @since 4.0.0
|
| 156 |
*/
|
| 157 |
static fromReason(reason: CookiesError["reason"]["_tag"], cause?: unknown): CookiesError {
|
|
@@ -159,11 +200,15 @@ export class CookiesError extends Data.TaggedError("CookieError")<{
|
|
| 159 |
}
|
| 160 |
|
| 161 |
/**
|
|
|
|
|
|
|
| 162 |
* @since 4.0.0
|
| 163 |
*/
|
| 164 |
readonly [CookieErrorTypeId] = CookieErrorTypeId
|
| 165 |
|
| 166 |
/**
|
|
|
|
|
|
|
| 167 |
* @since 4.0.0
|
| 168 |
*/
|
| 169 |
override get message() {
|
|
@@ -186,10 +231,10 @@ const Proto: Omit<Cookies, "cookies"> = {
|
|
| 186 |
}
|
| 187 |
|
| 188 |
/**
|
| 189 |
-
*
|
| 190 |
*
|
| 191 |
-
* @since 4.0.0
|
| 192 |
* @category constructors
|
|
|
|
| 193 |
*/
|
| 194 |
export const fromReadonlyRecord = (cookies: Record.ReadonlyRecord<string, Cookie>): Cookies => {
|
| 195 |
const self = Object.create(Proto)
|
|
@@ -200,8 +245,8 @@ export const fromReadonlyRecord = (cookies: Record.ReadonlyRecord<string, Cookie
|
|
| 200 |
/**
|
| 201 |
* Create a Cookies object from an Iterable
|
| 202 |
*
|
| 203 |
-
* @since 4.0.0
|
| 204 |
* @category constructors
|
|
|
|
| 205 |
*/
|
| 206 |
export const fromIterable = (cookies: Iterable<Cookie>): Cookies => {
|
| 207 |
const record: Record<string, Cookie> = {}
|
|
@@ -214,8 +259,8 @@ export const fromIterable = (cookies: Iterable<Cookie>): Cookies => {
|
|
| 214 |
/**
|
| 215 |
* Create a Cookies object from a set of Set-Cookie headers
|
| 216 |
*
|
| 217 |
-
* @since 4.0.0
|
| 218 |
* @category constructors
|
|
|
|
| 219 |
*/
|
| 220 |
export const fromSetCookie = (headers: Iterable<string> | string): Cookies => {
|
| 221 |
const arrayHeaders = typeof headers === "string" ? [headers] : headers
|
|
@@ -364,14 +409,16 @@ function parseSetCookie(header: string): Cookie | undefined {
|
|
| 364 |
/**
|
| 365 |
* An empty Cookies object
|
| 366 |
*
|
| 367 |
-
* @since 4.0.0
|
| 368 |
* @category constructors
|
|
|
|
| 369 |
*/
|
| 370 |
export const empty: Cookies = fromIterable([])
|
| 371 |
|
| 372 |
/**
|
| 373 |
-
*
|
|
|
|
| 374 |
* @category refinements
|
|
|
|
| 375 |
*/
|
| 376 |
export const isEmpty = (self: Cookies): boolean => Record.isEmptyRecord(self.cookies)
|
| 377 |
|
|
@@ -392,10 +439,14 @@ const CookieProto = {
|
|
| 392 |
}
|
| 393 |
|
| 394 |
/**
|
| 395 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
*
|
| 397 |
-
* @since 4.0.0
|
| 398 |
* @category constructors
|
|
|
|
| 399 |
*/
|
| 400 |
export function makeCookie(
|
| 401 |
name: string,
|
|
@@ -435,8 +486,8 @@ export function makeCookie(
|
|
| 435 |
/**
|
| 436 |
* Create a new cookie, throwing an error if invalid
|
| 437 |
*
|
| 438 |
-
* @since 4.0.0
|
| 439 |
* @category constructors
|
|
|
|
| 440 |
*/
|
| 441 |
export const makeCookieUnsafe = (
|
| 442 |
name: string,
|
|
@@ -445,24 +496,24 @@ export const makeCookieUnsafe = (
|
|
| 445 |
): Cookie => Result.getOrThrow(makeCookie(name, value, options))
|
| 446 |
|
| 447 |
/**
|
| 448 |
-
*
|
| 449 |
*
|
| 450 |
-
* @since 4.0.0
|
| 451 |
* @category combinators
|
|
|
|
| 452 |
*/
|
| 453 |
export const setCookie: {
|
| 454 |
/**
|
| 455 |
-
*
|
| 456 |
*
|
| 457 |
-
* @since 4.0.0
|
| 458 |
* @category combinators
|
|
|
|
| 459 |
*/
|
| 460 |
(cookie: Cookie): (self: Cookies) => Cookies
|
| 461 |
/**
|
| 462 |
-
*
|
| 463 |
*
|
| 464 |
-
* @since 4.0.0
|
| 465 |
* @category combinators
|
|
|
|
| 466 |
*/
|
| 467 |
(self: Cookies, cookie: Cookie): Cookies
|
| 468 |
} = dual(
|
|
@@ -476,24 +527,24 @@ export const setCookie: {
|
|
| 476 |
)
|
| 477 |
|
| 478 |
/**
|
| 479 |
-
*
|
| 480 |
*
|
| 481 |
-
* @since 4.0.0
|
| 482 |
* @category combinators
|
|
|
|
| 483 |
*/
|
| 484 |
export const setAllCookie: {
|
| 485 |
/**
|
| 486 |
-
*
|
| 487 |
*
|
| 488 |
-
* @since 4.0.0
|
| 489 |
* @category combinators
|
|
|
|
| 490 |
*/
|
| 491 |
(cookies: Iterable<Cookie>): (self: Cookies) => Cookies
|
| 492 |
/**
|
| 493 |
-
*
|
| 494 |
*
|
| 495 |
-
* @since 4.0.0
|
| 496 |
* @category combinators
|
|
|
|
| 497 |
*/
|
| 498 |
(self: Cookies, cookies: Iterable<Cookie>): Cookies
|
| 499 |
} = dual(2, (self: Cookies, cookies: Iterable<Cookie>) => {
|
|
@@ -505,24 +556,24 @@ export const setAllCookie: {
|
|
| 505 |
})
|
| 506 |
|
| 507 |
/**
|
| 508 |
-
*
|
| 509 |
*
|
| 510 |
-
* @since 4.0.0
|
| 511 |
* @category combinators
|
|
|
|
| 512 |
*/
|
| 513 |
export const merge: {
|
| 514 |
/**
|
| 515 |
-
*
|
| 516 |
*
|
| 517 |
-
* @since 4.0.0
|
| 518 |
* @category combinators
|
|
|
|
| 519 |
*/
|
| 520 |
(that: Cookies): (self: Cookies) => Cookies
|
| 521 |
/**
|
| 522 |
-
*
|
| 523 |
*
|
| 524 |
-
* @since 4.0.0
|
| 525 |
* @category combinators
|
|
|
|
| 526 |
*/
|
| 527 |
(self: Cookies, that: Cookies): Cookies
|
| 528 |
} = dual(2, (self: Cookies, that: Cookies) =>
|
|
@@ -532,47 +583,47 @@ export const merge: {
|
|
| 532 |
}))
|
| 533 |
|
| 534 |
/**
|
| 535 |
-
*
|
| 536 |
*
|
| 537 |
-
* @since 4.0.0
|
| 538 |
* @category combinators
|
|
|
|
| 539 |
*/
|
| 540 |
export const remove: {
|
| 541 |
/**
|
| 542 |
-
*
|
| 543 |
*
|
| 544 |
-
* @since 4.0.0
|
| 545 |
* @category combinators
|
|
|
|
| 546 |
*/
|
| 547 |
(name: string): (self: Cookies) => Cookies
|
| 548 |
/**
|
| 549 |
-
*
|
| 550 |
*
|
| 551 |
-
* @since 4.0.0
|
| 552 |
* @category combinators
|
|
|
|
| 553 |
*/
|
| 554 |
(self: Cookies, name: string): Cookies
|
| 555 |
} = dual(2, (self: Cookies, name: string) => fromReadonlyRecord(Record.remove(self.cookies, name)))
|
| 556 |
|
| 557 |
/**
|
| 558 |
-
*
|
| 559 |
*
|
| 560 |
-
* @since 4.0.0
|
| 561 |
* @category combinators
|
|
|
|
| 562 |
*/
|
| 563 |
export const get: {
|
| 564 |
/**
|
| 565 |
-
*
|
| 566 |
*
|
| 567 |
-
* @since 4.0.0
|
| 568 |
* @category combinators
|
|
|
|
| 569 |
*/
|
| 570 |
(name: string): (self: Cookies) => Option.Option<Cookie>
|
| 571 |
/**
|
| 572 |
-
*
|
| 573 |
*
|
| 574 |
-
* @since 4.0.0
|
| 575 |
* @category combinators
|
|
|
|
| 576 |
*/
|
| 577 |
(self: Cookies, name: string): Option.Option<Cookie>
|
| 578 |
} = dual(
|
|
@@ -581,24 +632,36 @@ export const get: {
|
|
| 581 |
)
|
| 582 |
|
| 583 |
/**
|
| 584 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 585 |
*
|
| 586 |
-
* @since 4.0.0
|
| 587 |
* @category combinators
|
|
|
|
| 588 |
*/
|
| 589 |
export const getValue: {
|
| 590 |
/**
|
| 591 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 592 |
*
|
| 593 |
-
* @since 4.0.0
|
| 594 |
* @category combinators
|
|
|
|
| 595 |
*/
|
| 596 |
(name: string): (self: Cookies) => Option.Option<string>
|
| 597 |
/**
|
| 598 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 599 |
*
|
| 600 |
-
* @since 4.0.0
|
| 601 |
* @category combinators
|
|
|
|
| 602 |
*/
|
| 603 |
(self: Cookies, name: string): Option.Option<string>
|
| 604 |
} = dual(
|
|
@@ -607,24 +670,36 @@ export const getValue: {
|
|
| 607 |
)
|
| 608 |
|
| 609 |
/**
|
| 610 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 611 |
*
|
| 612 |
-
* @since 4.0.0
|
| 613 |
* @category combinators
|
|
|
|
| 614 |
*/
|
| 615 |
export const set: {
|
| 616 |
/**
|
| 617 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 618 |
*
|
| 619 |
-
* @since 4.0.0
|
| 620 |
* @category combinators
|
|
|
|
| 621 |
*/
|
| 622 |
(name: string, value: string, options?: Cookie["options"]): (self: Cookies) => Result.Result<Cookies, CookiesError>
|
| 623 |
/**
|
| 624 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 625 |
*
|
| 626 |
-
* @since 4.0.0
|
| 627 |
* @category combinators
|
|
|
|
| 628 |
*/
|
| 629 |
(self: Cookies, name: string, value: string, options?: Cookie["options"]): Result.Result<Cookies, CookiesError>
|
| 630 |
} = dual(
|
|
@@ -637,24 +712,24 @@ export const set: {
|
|
| 637 |
)
|
| 638 |
|
| 639 |
/**
|
| 640 |
-
*
|
| 641 |
*
|
| 642 |
-
* @since 4.0.0
|
| 643 |
* @category combinators
|
|
|
|
| 644 |
*/
|
| 645 |
export const setUnsafe: {
|
| 646 |
/**
|
| 647 |
-
*
|
| 648 |
*
|
| 649 |
-
* @since 4.0.0
|
| 650 |
* @category combinators
|
|
|
|
| 651 |
*/
|
| 652 |
(name: string, value: string, options?: Cookie["options"]): (self: Cookies) => Cookies
|
| 653 |
/**
|
| 654 |
-
*
|
| 655 |
*
|
| 656 |
-
* @since 4.0.0
|
| 657 |
* @category combinators
|
|
|
|
| 658 |
*/
|
| 659 |
(self: Cookies, name: string, value: string, options?: Cookie["options"]): Cookies
|
| 660 |
} = dual(
|
|
@@ -668,27 +743,39 @@ export const setUnsafe: {
|
|
| 668 |
)
|
| 669 |
|
| 670 |
/**
|
| 671 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 672 |
*
|
| 673 |
-
* @since 4.0.0
|
| 674 |
* @category combinators
|
|
|
|
| 675 |
*/
|
| 676 |
export const expireCookie: {
|
| 677 |
/**
|
| 678 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 679 |
*
|
| 680 |
-
* @since 4.0.0
|
| 681 |
* @category combinators
|
|
|
|
| 682 |
*/
|
| 683 |
(
|
| 684 |
name: string,
|
| 685 |
options?: Omit<NonNullable<Cookie["options"]>, "expires" | "maxAge">
|
| 686 |
): (self: Cookies) => Result.Result<Cookies, CookiesError>
|
| 687 |
/**
|
| 688 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 689 |
*
|
| 690 |
-
* @since 4.0.0
|
| 691 |
* @category combinators
|
|
|
|
| 692 |
*/
|
| 693 |
(
|
| 694 |
self: Cookies,
|
|
@@ -710,27 +797,27 @@ export const expireCookie: {
|
|
| 710 |
)
|
| 711 |
|
| 712 |
/**
|
| 713 |
-
*
|
| 714 |
*
|
| 715 |
-
* @since 4.0.0
|
| 716 |
* @category combinators
|
|
|
|
| 717 |
*/
|
| 718 |
export const expireCookieUnsafe: {
|
| 719 |
/**
|
| 720 |
-
*
|
| 721 |
*
|
| 722 |
-
* @since 4.0.0
|
| 723 |
* @category combinators
|
|
|
|
| 724 |
*/
|
| 725 |
(
|
| 726 |
name: string,
|
| 727 |
options?: Omit<NonNullable<Cookie["options"]>, "expires" | "maxAge">
|
| 728 |
): (self: Cookies) => Cookies
|
| 729 |
/**
|
| 730 |
-
*
|
| 731 |
*
|
| 732 |
-
* @since 4.0.0
|
| 733 |
* @category combinators
|
|
|
|
| 734 |
*/
|
| 735 |
(
|
| 736 |
self: Cookies,
|
|
@@ -752,26 +839,38 @@ export const expireCookieUnsafe: {
|
|
| 752 |
)
|
| 753 |
|
| 754 |
/**
|
| 755 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 756 |
*
|
| 757 |
-
* @since 4.0.0
|
| 758 |
* @category combinators
|
|
|
|
| 759 |
*/
|
| 760 |
export const setAll: {
|
| 761 |
/**
|
| 762 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 763 |
*
|
| 764 |
-
* @since 4.0.0
|
| 765 |
* @category combinators
|
|
|
|
| 766 |
*/
|
| 767 |
(
|
| 768 |
cookies: Iterable<readonly [name: string, value: string, options?: Cookie["options"]]>
|
| 769 |
): (self: Cookies) => Result.Result<Cookies, CookiesError>
|
| 770 |
/**
|
| 771 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 772 |
*
|
| 773 |
-
* @since 4.0.0
|
| 774 |
* @category combinators
|
|
|
|
| 775 |
*/
|
| 776 |
(
|
| 777 |
self: Cookies,
|
|
@@ -796,26 +895,26 @@ export const setAll: {
|
|
| 796 |
)
|
| 797 |
|
| 798 |
/**
|
| 799 |
-
*
|
| 800 |
*
|
| 801 |
-
* @since 4.0.0
|
| 802 |
* @category combinators
|
|
|
|
| 803 |
*/
|
| 804 |
export const setAllUnsafe: {
|
| 805 |
/**
|
| 806 |
-
*
|
| 807 |
*
|
| 808 |
-
* @since 4.0.0
|
| 809 |
* @category combinators
|
|
|
|
| 810 |
*/
|
| 811 |
(
|
| 812 |
cookies: Iterable<readonly [name: string, value: string, options?: Cookie["options"]]>
|
| 813 |
): (self: Cookies) => Cookies
|
| 814 |
/**
|
| 815 |
-
*
|
| 816 |
*
|
| 817 |
-
* @since 4.0.0
|
| 818 |
* @category combinators
|
|
|
|
| 819 |
*/
|
| 820 |
(
|
| 821 |
self: Cookies,
|
|
@@ -830,12 +929,14 @@ export const setAllUnsafe: {
|
|
| 830 |
)
|
| 831 |
|
| 832 |
/**
|
| 833 |
-
*
|
|
|
|
|
|
|
| 834 |
*
|
| 835 |
* Adapted from https://github.com/fastify/fastify-cookie under MIT License
|
| 836 |
*
|
| 837 |
-
* @since 4.0.0
|
| 838 |
* @category encoding
|
|
|
|
| 839 |
*/
|
| 840 |
export function serializeCookie(self: Cookie): string {
|
| 841 |
let str = self.name + "=" + self.valueEncoded
|
|
@@ -908,17 +1009,19 @@ export function serializeCookie(self: Cookie): string {
|
|
| 908 |
}
|
| 909 |
|
| 910 |
/**
|
| 911 |
-
*
|
| 912 |
*
|
| 913 |
-
* @since 4.0.0
|
| 914 |
* @category encoding
|
|
|
|
| 915 |
*/
|
| 916 |
export const toCookieHeader = (self: Cookies): string =>
|
| 917 |
Object.values(self.cookies).map((cookie) => `${cookie.name}=${cookie.valueEncoded}`).join("; ")
|
| 918 |
|
| 919 |
/**
|
| 920 |
-
*
|
|
|
|
| 921 |
* @category encoding
|
|
|
|
| 922 |
*/
|
| 923 |
export const toRecord = (self: Cookies): Record<string, string> => {
|
| 924 |
const record: Record<string, string> = {}
|
|
@@ -931,13 +1034,16 @@ export const toRecord = (self: Cookies): Record<string, string> => {
|
|
| 931 |
}
|
| 932 |
|
| 933 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 934 |
* @since 4.0.0
|
| 935 |
-
* @category Schemas
|
| 936 |
*/
|
| 937 |
export const schemaRecord = CookiesSchema.pipe(
|
| 938 |
Schema.decodeTo(
|
| 939 |
Schema.Record(Schema.String, Schema.String),
|
| 940 |
-
|
| 941 |
decode: toRecord,
|
| 942 |
encode: (self) => fromIterable(Object.entries(self).map(([name, value]) => makeCookieUnsafe(name, value)))
|
| 943 |
})
|
|
@@ -945,20 +1051,22 @@ export const schemaRecord = CookiesSchema.pipe(
|
|
| 945 |
)
|
| 946 |
|
| 947 |
/**
|
| 948 |
-
*
|
| 949 |
*
|
| 950 |
-
* @since 4.0.0
|
| 951 |
* @category encoding
|
|
|
|
| 952 |
*/
|
| 953 |
export const toSetCookieHeaders = (self: Cookies): Array<string> => Object.values(self.cookies).map(serializeCookie)
|
| 954 |
|
| 955 |
/**
|
| 956 |
-
*
|
|
|
|
|
|
|
| 957 |
*
|
| 958 |
* Adapted from https://github.com/fastify/fastify-cookie under MIT License
|
| 959 |
*
|
| 960 |
-
* @since 4.0.0
|
| 961 |
* @category decoding
|
|
|
|
| 962 |
*/
|
| 963 |
export function parseHeader(header: string): Record<string, string> {
|
| 964 |
const result: Record<string, string> = {}
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Models HTTP cookies and cookie collections for requests and responses.
|
| 3 |
+
*
|
| 4 |
+
* A `Cookie` stores a name, value, encoded value, and standard cookie
|
| 5 |
+
* attributes. A `Cookies` value is an immutable collection keyed by cookie
|
| 6 |
+
* name. This module parses request `Cookie` headers, builds response
|
| 7 |
+
* `Set-Cookie` headers, and provides helpers for adding, removing, merging, and
|
| 8 |
+
* expiring cookies.
|
| 9 |
+
*
|
| 10 |
* @since 4.0.0
|
| 11 |
*/
|
| 12 |
import * as Data from "../../Data.ts"
|
|
|
|
| 19 |
import * as Record from "../../Record.ts"
|
| 20 |
import * as Result from "../../Result.ts"
|
| 21 |
import * as Schema from "../../Schema.ts"
|
| 22 |
+
import * as SchemaTransformation from "../../SchemaTransformation.ts"
|
| 23 |
import type * as Types from "../../Types.ts"
|
| 24 |
|
| 25 |
const TypeId = "~effect/http/Cookies"
|
| 26 |
|
| 27 |
/**
|
| 28 |
+
* Returns `true` when a value is a `Cookies` collection.
|
| 29 |
+
*
|
| 30 |
* @category refinements
|
| 31 |
+
* @since 4.0.0
|
| 32 |
*/
|
| 33 |
export const isCookies = (u: unknown): u is Cookies => Predicate.hasProperty(u, TypeId)
|
| 34 |
|
| 35 |
/**
|
| 36 |
+
* Immutable collection of HTTP cookies keyed by cookie name.
|
| 37 |
+
*
|
| 38 |
* @category models
|
| 39 |
+
* @since 4.0.0
|
| 40 |
*/
|
| 41 |
export interface Cookies extends Pipeable, Inspectable.Inspectable {
|
| 42 |
readonly [TypeId]: typeof TypeId
|
|
|
|
| 44 |
}
|
| 45 |
|
| 46 |
/**
|
| 47 |
+
* Schema interface for validating and encoding `Cookies` collections.
|
| 48 |
+
*
|
| 49 |
+
* @category schemas
|
| 50 |
* @since 4.0.0
|
|
|
|
| 51 |
*/
|
| 52 |
export interface CookiesSchema extends Schema.declare<Cookies, Record.ReadonlyRecord<string, Cookie>> {}
|
| 53 |
|
| 54 |
/**
|
| 55 |
+
* Schema for `Cookies` collections.
|
| 56 |
+
*
|
| 57 |
+
* **Details**
|
| 58 |
+
*
|
| 59 |
+
* JSON encoding uses `Set-Cookie` header strings, while isomorphic encoding uses
|
| 60 |
+
* a readonly record of cookie values.
|
| 61 |
+
*
|
| 62 |
+
* @category schemas
|
| 63 |
* @since 4.0.0
|
|
|
|
| 64 |
*/
|
| 65 |
export const CookiesSchema: CookiesSchema = Schema.declare(
|
| 66 |
isCookies,
|
|
|
|
| 78 |
toCodecJson: () =>
|
| 79 |
Schema.link<Cookies>()(
|
| 80 |
Schema.Array(Schema.String),
|
| 81 |
+
SchemaTransformation.transform({
|
| 82 |
decode: (input) => fromSetCookie(input),
|
| 83 |
encode: (cookies) => toSetCookieHeaders(cookies)
|
| 84 |
})
|
|
|
|
| 86 |
toCodecIso: () =>
|
| 87 |
Schema.link<Cookies>()(
|
| 88 |
Schema.Record(Schema.String, CookieSchema),
|
| 89 |
+
SchemaTransformation.transform({
|
| 90 |
decode: (input) => fromReadonlyRecord(input),
|
| 91 |
encode: (cookies) => cookies.cookies
|
| 92 |
})
|
|
|
|
| 97 |
const CookieTypeId = "~effect/http/Cookies/Cookie"
|
| 98 |
|
| 99 |
/**
|
| 100 |
+
* HTTP cookie value with its decoded value, encoded value, and optional cookie
|
| 101 |
+
* attributes such as domain, path, expiration, security, and same-site settings.
|
| 102 |
+
*
|
| 103 |
+
* @category cookies
|
| 104 |
* @since 4.0.0
|
|
|
|
| 105 |
*/
|
| 106 |
export interface Cookie extends Inspectable.Inspectable {
|
| 107 |
readonly [CookieTypeId]: typeof CookieTypeId
|
|
|
|
| 122 |
}
|
| 123 |
|
| 124 |
/**
|
| 125 |
+
* Returns `true` when a value is a `Cookie`.
|
| 126 |
+
*
|
| 127 |
+
* @category guards
|
| 128 |
* @since 4.0.0
|
|
|
|
| 129 |
*/
|
| 130 |
export const isCookie = (u: unknown): u is Cookie => Predicate.hasProperty(u, CookieTypeId)
|
| 131 |
|
| 132 |
/**
|
| 133 |
+
* Schema interface for validating `Cookie` values.
|
| 134 |
+
*
|
| 135 |
+
* @category schemas
|
| 136 |
* @since 4.0.0
|
|
|
|
| 137 |
*/
|
| 138 |
export interface CookieSchema extends Schema.declare<Cookie> {}
|
| 139 |
|
| 140 |
/**
|
| 141 |
+
* Schema for `Cookie` values.
|
| 142 |
+
*
|
| 143 |
+
* @category schemas
|
| 144 |
* @since 4.0.0
|
|
|
|
| 145 |
*/
|
| 146 |
export const CookieSchema: CookieSchema = Schema.declare(
|
| 147 |
isCookie,
|
|
|
|
| 161 |
const CookieErrorTypeId = "~effect/http/Cookies/CookieError"
|
| 162 |
|
| 163 |
/**
|
| 164 |
+
* Error reason describing why cookie construction failed, such as invalid name,
|
| 165 |
+
* value, domain, path, or infinite max-age.
|
| 166 |
+
*
|
| 167 |
* @category errors
|
| 168 |
+
* @since 4.0.0
|
| 169 |
*/
|
| 170 |
export class CookiesErrorReason extends Data.Error<{
|
| 171 |
readonly _tag:
|
|
|
|
| 178 |
}> {}
|
| 179 |
|
| 180 |
/**
|
| 181 |
+
* Error returned when a cookie name, value, domain, path, or max-age option is invalid.
|
| 182 |
+
*
|
| 183 |
+
* **Details**
|
| 184 |
+
*
|
| 185 |
+
* Inspect `reason` to determine the specific validation failure.
|
| 186 |
+
*
|
| 187 |
* @category errors
|
| 188 |
+
* @since 4.0.0
|
| 189 |
*/
|
| 190 |
export class CookiesError extends Data.TaggedError("CookieError")<{
|
| 191 |
readonly reason: CookiesErrorReason
|
| 192 |
}> {
|
| 193 |
/**
|
| 194 |
+
* Creates a cookie error from a reason tag and optional cause.
|
| 195 |
+
*
|
| 196 |
* @since 4.0.0
|
| 197 |
*/
|
| 198 |
static fromReason(reason: CookiesError["reason"]["_tag"], cause?: unknown): CookiesError {
|
|
|
|
| 200 |
}
|
| 201 |
|
| 202 |
/**
|
| 203 |
+
* Marks this value as a cookie validation error for runtime guards.
|
| 204 |
+
*
|
| 205 |
* @since 4.0.0
|
| 206 |
*/
|
| 207 |
readonly [CookieErrorTypeId] = CookieErrorTypeId
|
| 208 |
|
| 209 |
/**
|
| 210 |
+
* Uses the concrete cookie error reason as the public message.
|
| 211 |
+
*
|
| 212 |
* @since 4.0.0
|
| 213 |
*/
|
| 214 |
override get message() {
|
|
|
|
| 231 |
}
|
| 232 |
|
| 233 |
/**
|
| 234 |
+
* Creates a `Cookies` collection from an existing readonly record of cookies keyed by cookie name.
|
| 235 |
*
|
|
|
|
| 236 |
* @category constructors
|
| 237 |
+
* @since 4.0.0
|
| 238 |
*/
|
| 239 |
export const fromReadonlyRecord = (cookies: Record.ReadonlyRecord<string, Cookie>): Cookies => {
|
| 240 |
const self = Object.create(Proto)
|
|
|
|
| 245 |
/**
|
| 246 |
* Create a Cookies object from an Iterable
|
| 247 |
*
|
|
|
|
| 248 |
* @category constructors
|
| 249 |
+
* @since 4.0.0
|
| 250 |
*/
|
| 251 |
export const fromIterable = (cookies: Iterable<Cookie>): Cookies => {
|
| 252 |
const record: Record<string, Cookie> = {}
|
|
|
|
| 259 |
/**
|
| 260 |
* Create a Cookies object from a set of Set-Cookie headers
|
| 261 |
*
|
|
|
|
| 262 |
* @category constructors
|
| 263 |
+
* @since 4.0.0
|
| 264 |
*/
|
| 265 |
export const fromSetCookie = (headers: Iterable<string> | string): Cookies => {
|
| 266 |
const arrayHeaders = typeof headers === "string" ? [headers] : headers
|
|
|
|
| 409 |
/**
|
| 410 |
* An empty Cookies object
|
| 411 |
*
|
|
|
|
| 412 |
* @category constructors
|
| 413 |
+
* @since 4.0.0
|
| 414 |
*/
|
| 415 |
export const empty: Cookies = fromIterable([])
|
| 416 |
|
| 417 |
/**
|
| 418 |
+
* Returns `true` when the `Cookies` collection contains no cookies.
|
| 419 |
+
*
|
| 420 |
* @category refinements
|
| 421 |
+
* @since 4.0.0
|
| 422 |
*/
|
| 423 |
export const isEmpty = (self: Cookies): boolean => Record.isEmptyRecord(self.cookies)
|
| 424 |
|
|
|
|
| 439 |
}
|
| 440 |
|
| 441 |
/**
|
| 442 |
+
* Creates a cookie, validating the name, encoded value, domain, path, and finite `maxAge`.
|
| 443 |
+
*
|
| 444 |
+
* **Details**
|
| 445 |
+
*
|
| 446 |
+
* Returns a `CookiesError` in the `Result` failure channel when validation fails.
|
| 447 |
*
|
|
|
|
| 448 |
* @category constructors
|
| 449 |
+
* @since 4.0.0
|
| 450 |
*/
|
| 451 |
export function makeCookie(
|
| 452 |
name: string,
|
|
|
|
| 486 |
/**
|
| 487 |
* Create a new cookie, throwing an error if invalid
|
| 488 |
*
|
|
|
|
| 489 |
* @category constructors
|
| 490 |
+
* @since 4.0.0
|
| 491 |
*/
|
| 492 |
export const makeCookieUnsafe = (
|
| 493 |
name: string,
|
|
|
|
| 496 |
): Cookie => Result.getOrThrow(makeCookie(name, value, options))
|
| 497 |
|
| 498 |
/**
|
| 499 |
+
* Adds a cookie to a Cookies object
|
| 500 |
*
|
|
|
|
| 501 |
* @category combinators
|
| 502 |
+
* @since 4.0.0
|
| 503 |
*/
|
| 504 |
export const setCookie: {
|
| 505 |
/**
|
| 506 |
+
* Adds a cookie to a Cookies object
|
| 507 |
*
|
|
|
|
| 508 |
* @category combinators
|
| 509 |
+
* @since 4.0.0
|
| 510 |
*/
|
| 511 |
(cookie: Cookie): (self: Cookies) => Cookies
|
| 512 |
/**
|
| 513 |
+
* Adds a cookie to a Cookies object
|
| 514 |
*
|
|
|
|
| 515 |
* @category combinators
|
| 516 |
+
* @since 4.0.0
|
| 517 |
*/
|
| 518 |
(self: Cookies, cookie: Cookie): Cookies
|
| 519 |
} = dual(
|
|
|
|
| 527 |
)
|
| 528 |
|
| 529 |
/**
|
| 530 |
+
* Adds multiple cookies to a Cookies object
|
| 531 |
*
|
|
|
|
| 532 |
* @category combinators
|
| 533 |
+
* @since 4.0.0
|
| 534 |
*/
|
| 535 |
export const setAllCookie: {
|
| 536 |
/**
|
| 537 |
+
* Adds multiple cookies to a Cookies object
|
| 538 |
*
|
|
|
|
| 539 |
* @category combinators
|
| 540 |
+
* @since 4.0.0
|
| 541 |
*/
|
| 542 |
(cookies: Iterable<Cookie>): (self: Cookies) => Cookies
|
| 543 |
/**
|
| 544 |
+
* Adds multiple cookies to a Cookies object
|
| 545 |
*
|
|
|
|
| 546 |
* @category combinators
|
| 547 |
+
* @since 4.0.0
|
| 548 |
*/
|
| 549 |
(self: Cookies, cookies: Iterable<Cookie>): Cookies
|
| 550 |
} = dual(2, (self: Cookies, cookies: Iterable<Cookie>) => {
|
|
|
|
| 556 |
})
|
| 557 |
|
| 558 |
/**
|
| 559 |
+
* Combines two Cookies objects, removing duplicates from the first
|
| 560 |
*
|
|
|
|
| 561 |
* @category combinators
|
| 562 |
+
* @since 4.0.0
|
| 563 |
*/
|
| 564 |
export const merge: {
|
| 565 |
/**
|
| 566 |
+
* Combines two Cookies objects, removing duplicates from the first
|
| 567 |
*
|
|
|
|
| 568 |
* @category combinators
|
| 569 |
+
* @since 4.0.0
|
| 570 |
*/
|
| 571 |
(that: Cookies): (self: Cookies) => Cookies
|
| 572 |
/**
|
| 573 |
+
* Combines two Cookies objects, removing duplicates from the first
|
| 574 |
*
|
|
|
|
| 575 |
* @category combinators
|
| 576 |
+
* @since 4.0.0
|
| 577 |
*/
|
| 578 |
(self: Cookies, that: Cookies): Cookies
|
| 579 |
} = dual(2, (self: Cookies, that: Cookies) =>
|
|
|
|
| 583 |
}))
|
| 584 |
|
| 585 |
/**
|
| 586 |
+
* Removes a cookie by name
|
| 587 |
*
|
|
|
|
| 588 |
* @category combinators
|
| 589 |
+
* @since 4.0.0
|
| 590 |
*/
|
| 591 |
export const remove: {
|
| 592 |
/**
|
| 593 |
+
* Removes a cookie by name
|
| 594 |
*
|
|
|
|
| 595 |
* @category combinators
|
| 596 |
+
* @since 4.0.0
|
| 597 |
*/
|
| 598 |
(name: string): (self: Cookies) => Cookies
|
| 599 |
/**
|
| 600 |
+
* Removes a cookie by name
|
| 601 |
*
|
|
|
|
| 602 |
* @category combinators
|
| 603 |
+
* @since 4.0.0
|
| 604 |
*/
|
| 605 |
(self: Cookies, name: string): Cookies
|
| 606 |
} = dual(2, (self: Cookies, name: string) => fromReadonlyRecord(Record.remove(self.cookies, name)))
|
| 607 |
|
| 608 |
/**
|
| 609 |
+
* Gets a cookie from a Cookies object safely.
|
| 610 |
*
|
|
|
|
| 611 |
* @category combinators
|
| 612 |
+
* @since 4.0.0
|
| 613 |
*/
|
| 614 |
export const get: {
|
| 615 |
/**
|
| 616 |
+
* Gets a cookie from a Cookies object safely.
|
| 617 |
*
|
|
|
|
| 618 |
* @category combinators
|
| 619 |
+
* @since 4.0.0
|
| 620 |
*/
|
| 621 |
(name: string): (self: Cookies) => Option.Option<Cookie>
|
| 622 |
/**
|
| 623 |
+
* Gets a cookie from a Cookies object safely.
|
| 624 |
*
|
|
|
|
| 625 |
* @category combinators
|
| 626 |
+
* @since 4.0.0
|
| 627 |
*/
|
| 628 |
(self: Cookies, name: string): Option.Option<Cookie>
|
| 629 |
} = dual(
|
|
|
|
| 632 |
)
|
| 633 |
|
| 634 |
/**
|
| 635 |
+
* Gets the decoded value of a cookie by name safely.
|
| 636 |
+
*
|
| 637 |
+
* **Details**
|
| 638 |
+
*
|
| 639 |
+
* Returns `Option.none()` when the cookie is not present.
|
| 640 |
*
|
|
|
|
| 641 |
* @category combinators
|
| 642 |
+
* @since 4.0.0
|
| 643 |
*/
|
| 644 |
export const getValue: {
|
| 645 |
/**
|
| 646 |
+
* Gets the decoded value of a cookie by name safely.
|
| 647 |
+
*
|
| 648 |
+
* **Details**
|
| 649 |
+
*
|
| 650 |
+
* Returns `Option.none()` when the cookie is not present.
|
| 651 |
*
|
|
|
|
| 652 |
* @category combinators
|
| 653 |
+
* @since 4.0.0
|
| 654 |
*/
|
| 655 |
(name: string): (self: Cookies) => Option.Option<string>
|
| 656 |
/**
|
| 657 |
+
* Gets the decoded value of a cookie by name safely.
|
| 658 |
+
*
|
| 659 |
+
* **Details**
|
| 660 |
+
*
|
| 661 |
+
* Returns `Option.none()` when the cookie is not present.
|
| 662 |
*
|
|
|
|
| 663 |
* @category combinators
|
| 664 |
+
* @since 4.0.0
|
| 665 |
*/
|
| 666 |
(self: Cookies, name: string): Option.Option<string>
|
| 667 |
} = dual(
|
|
|
|
| 670 |
)
|
| 671 |
|
| 672 |
/**
|
| 673 |
+
* Creates and adds a cookie safely by name and value.
|
| 674 |
+
*
|
| 675 |
+
* **Details**
|
| 676 |
+
*
|
| 677 |
+
* The cookie fields are validated first; invalid input returns a `CookiesError` in the `Result` failure channel.
|
| 678 |
*
|
|
|
|
| 679 |
* @category combinators
|
| 680 |
+
* @since 4.0.0
|
| 681 |
*/
|
| 682 |
export const set: {
|
| 683 |
/**
|
| 684 |
+
* Creates and adds a cookie safely by name and value.
|
| 685 |
+
*
|
| 686 |
+
* **Details**
|
| 687 |
+
*
|
| 688 |
+
* The cookie fields are validated first; invalid input returns a `CookiesError` in the `Result` failure channel.
|
| 689 |
*
|
|
|
|
| 690 |
* @category combinators
|
| 691 |
+
* @since 4.0.0
|
| 692 |
*/
|
| 693 |
(name: string, value: string, options?: Cookie["options"]): (self: Cookies) => Result.Result<Cookies, CookiesError>
|
| 694 |
/**
|
| 695 |
+
* Creates and adds a cookie safely by name and value.
|
| 696 |
+
*
|
| 697 |
+
* **Details**
|
| 698 |
+
*
|
| 699 |
+
* The cookie fields are validated first; invalid input returns a `CookiesError` in the `Result` failure channel.
|
| 700 |
*
|
|
|
|
| 701 |
* @category combinators
|
| 702 |
+
* @since 4.0.0
|
| 703 |
*/
|
| 704 |
(self: Cookies, name: string, value: string, options?: Cookie["options"]): Result.Result<Cookies, CookiesError>
|
| 705 |
} = dual(
|
|
|
|
| 712 |
)
|
| 713 |
|
| 714 |
/**
|
| 715 |
+
* Creates and adds a cookie by name and value, throwing if the cookie fields are invalid.
|
| 716 |
*
|
|
|
|
| 717 |
* @category combinators
|
| 718 |
+
* @since 4.0.0
|
| 719 |
*/
|
| 720 |
export const setUnsafe: {
|
| 721 |
/**
|
| 722 |
+
* Creates and adds a cookie by name and value, throwing if the cookie fields are invalid.
|
| 723 |
*
|
|
|
|
| 724 |
* @category combinators
|
| 725 |
+
* @since 4.0.0
|
| 726 |
*/
|
| 727 |
(name: string, value: string, options?: Cookie["options"]): (self: Cookies) => Cookies
|
| 728 |
/**
|
| 729 |
+
* Creates and adds a cookie by name and value, throwing if the cookie fields are invalid.
|
| 730 |
*
|
|
|
|
| 731 |
* @category combinators
|
| 732 |
+
* @since 4.0.0
|
| 733 |
*/
|
| 734 |
(self: Cookies, name: string, value: string, options?: Cookie["options"]): Cookies
|
| 735 |
} = dual(
|
|
|
|
| 743 |
)
|
| 744 |
|
| 745 |
/**
|
| 746 |
+
* Adds an expired cookie safely with an empty value, `Max-Age=0`, and an epoch `Expires` value.
|
| 747 |
+
*
|
| 748 |
+
* **Details**
|
| 749 |
+
*
|
| 750 |
+
* Returns a `CookiesError` in the `Result` failure channel when the name or options are invalid.
|
| 751 |
*
|
|
|
|
| 752 |
* @category combinators
|
| 753 |
+
* @since 4.0.0
|
| 754 |
*/
|
| 755 |
export const expireCookie: {
|
| 756 |
/**
|
| 757 |
+
* Adds an expired cookie safely with an empty value, `Max-Age=0`, and an epoch `Expires` value.
|
| 758 |
+
*
|
| 759 |
+
* **Details**
|
| 760 |
+
*
|
| 761 |
+
* Returns a `CookiesError` in the `Result` failure channel when the name or options are invalid.
|
| 762 |
*
|
|
|
|
| 763 |
* @category combinators
|
| 764 |
+
* @since 4.0.0
|
| 765 |
*/
|
| 766 |
(
|
| 767 |
name: string,
|
| 768 |
options?: Omit<NonNullable<Cookie["options"]>, "expires" | "maxAge">
|
| 769 |
): (self: Cookies) => Result.Result<Cookies, CookiesError>
|
| 770 |
/**
|
| 771 |
+
* Adds an expired cookie safely with an empty value, `Max-Age=0`, and an epoch `Expires` value.
|
| 772 |
+
*
|
| 773 |
+
* **Details**
|
| 774 |
+
*
|
| 775 |
+
* Returns a `CookiesError` in the `Result` failure channel when the name or options are invalid.
|
| 776 |
*
|
|
|
|
| 777 |
* @category combinators
|
| 778 |
+
* @since 4.0.0
|
| 779 |
*/
|
| 780 |
(
|
| 781 |
self: Cookies,
|
|
|
|
| 797 |
)
|
| 798 |
|
| 799 |
/**
|
| 800 |
+
* Adds an expired cookie to a Cookies object, throwing an error if invalid
|
| 801 |
*
|
|
|
|
| 802 |
* @category combinators
|
| 803 |
+
* @since 4.0.0
|
| 804 |
*/
|
| 805 |
export const expireCookieUnsafe: {
|
| 806 |
/**
|
| 807 |
+
* Adds an expired cookie to a Cookies object, throwing an error if invalid
|
| 808 |
*
|
|
|
|
| 809 |
* @category combinators
|
| 810 |
+
* @since 4.0.0
|
| 811 |
*/
|
| 812 |
(
|
| 813 |
name: string,
|
| 814 |
options?: Omit<NonNullable<Cookie["options"]>, "expires" | "maxAge">
|
| 815 |
): (self: Cookies) => Cookies
|
| 816 |
/**
|
| 817 |
+
* Adds an expired cookie to a Cookies object, throwing an error if invalid
|
| 818 |
*
|
|
|
|
| 819 |
* @category combinators
|
| 820 |
+
* @since 4.0.0
|
| 821 |
*/
|
| 822 |
(
|
| 823 |
self: Cookies,
|
|
|
|
| 839 |
)
|
| 840 |
|
| 841 |
/**
|
| 842 |
+
* Creates and adds multiple cookies safely from name/value/options tuples.
|
| 843 |
+
*
|
| 844 |
+
* **Details**
|
| 845 |
+
*
|
| 846 |
+
* If any tuple is invalid, returns the first `CookiesError` and leaves the original collection unchanged.
|
| 847 |
*
|
|
|
|
| 848 |
* @category combinators
|
| 849 |
+
* @since 4.0.0
|
| 850 |
*/
|
| 851 |
export const setAll: {
|
| 852 |
/**
|
| 853 |
+
* Creates and adds multiple cookies safely from name/value/options tuples.
|
| 854 |
+
*
|
| 855 |
+
* **Details**
|
| 856 |
+
*
|
| 857 |
+
* If any tuple is invalid, returns the first `CookiesError` and leaves the original collection unchanged.
|
| 858 |
*
|
|
|
|
| 859 |
* @category combinators
|
| 860 |
+
* @since 4.0.0
|
| 861 |
*/
|
| 862 |
(
|
| 863 |
cookies: Iterable<readonly [name: string, value: string, options?: Cookie["options"]]>
|
| 864 |
): (self: Cookies) => Result.Result<Cookies, CookiesError>
|
| 865 |
/**
|
| 866 |
+
* Creates and adds multiple cookies safely from name/value/options tuples.
|
| 867 |
+
*
|
| 868 |
+
* **Details**
|
| 869 |
+
*
|
| 870 |
+
* If any tuple is invalid, returns the first `CookiesError` and leaves the original collection unchanged.
|
| 871 |
*
|
|
|
|
| 872 |
* @category combinators
|
| 873 |
+
* @since 4.0.0
|
| 874 |
*/
|
| 875 |
(
|
| 876 |
self: Cookies,
|
|
|
|
| 895 |
)
|
| 896 |
|
| 897 |
/**
|
| 898 |
+
* Adds multiple cookies to a Cookies object, throwing an error if invalid
|
| 899 |
*
|
|
|
|
| 900 |
* @category combinators
|
| 901 |
+
* @since 4.0.0
|
| 902 |
*/
|
| 903 |
export const setAllUnsafe: {
|
| 904 |
/**
|
| 905 |
+
* Adds multiple cookies to a Cookies object, throwing an error if invalid
|
| 906 |
*
|
|
|
|
| 907 |
* @category combinators
|
| 908 |
+
* @since 4.0.0
|
| 909 |
*/
|
| 910 |
(
|
| 911 |
cookies: Iterable<readonly [name: string, value: string, options?: Cookie["options"]]>
|
| 912 |
): (self: Cookies) => Cookies
|
| 913 |
/**
|
| 914 |
+
* Adds multiple cookies to a Cookies object, throwing an error if invalid
|
| 915 |
*
|
|
|
|
| 916 |
* @category combinators
|
| 917 |
+
* @since 4.0.0
|
| 918 |
*/
|
| 919 |
(
|
| 920 |
self: Cookies,
|
|
|
|
| 929 |
)
|
| 930 |
|
| 931 |
/**
|
| 932 |
+
* Serializes a cookie into a string.
|
| 933 |
+
*
|
| 934 |
+
* **Details**
|
| 935 |
*
|
| 936 |
* Adapted from https://github.com/fastify/fastify-cookie under MIT License
|
| 937 |
*
|
|
|
|
| 938 |
* @category encoding
|
| 939 |
+
* @since 4.0.0
|
| 940 |
*/
|
| 941 |
export function serializeCookie(self: Cookie): string {
|
| 942 |
let str = self.name + "=" + self.valueEncoded
|
|
|
|
| 1009 |
}
|
| 1010 |
|
| 1011 |
/**
|
| 1012 |
+
* Serializes a `Cookies` object into a Cookie header.
|
| 1013 |
*
|
|
|
|
| 1014 |
* @category encoding
|
| 1015 |
+
* @since 4.0.0
|
| 1016 |
*/
|
| 1017 |
export const toCookieHeader = (self: Cookies): string =>
|
| 1018 |
Object.values(self.cookies).map((cookie) => `${cookie.name}=${cookie.valueEncoded}`).join("; ")
|
| 1019 |
|
| 1020 |
/**
|
| 1021 |
+
* Converts a `Cookies` collection to a record of decoded cookie values keyed by cookie name.
|
| 1022 |
+
*
|
| 1023 |
* @category encoding
|
| 1024 |
+
* @since 4.0.0
|
| 1025 |
*/
|
| 1026 |
export const toRecord = (self: Cookies): Record<string, string> => {
|
| 1027 |
const record: Record<string, string> = {}
|
|
|
|
| 1034 |
}
|
| 1035 |
|
| 1036 |
/**
|
| 1037 |
+
* Schema for transforming `Cookies` into records of decoded string values keyed
|
| 1038 |
+
* by cookie name.
|
| 1039 |
+
*
|
| 1040 |
+
* @category schemas
|
| 1041 |
* @since 4.0.0
|
|
|
|
| 1042 |
*/
|
| 1043 |
export const schemaRecord = CookiesSchema.pipe(
|
| 1044 |
Schema.decodeTo(
|
| 1045 |
Schema.Record(Schema.String, Schema.String),
|
| 1046 |
+
SchemaTransformation.transform({
|
| 1047 |
decode: toRecord,
|
| 1048 |
encode: (self) => fromIterable(Object.entries(self).map(([name, value]) => makeCookieUnsafe(name, value)))
|
| 1049 |
})
|
|
|
|
| 1051 |
)
|
| 1052 |
|
| 1053 |
/**
|
| 1054 |
+
* Serializes a `Cookies` collection into an array of `Set-Cookie` header values.
|
| 1055 |
*
|
|
|
|
| 1056 |
* @category encoding
|
| 1057 |
+
* @since 4.0.0
|
| 1058 |
*/
|
| 1059 |
export const toSetCookieHeaders = (self: Cookies): Array<string> => Object.values(self.cookies).map(serializeCookie)
|
| 1060 |
|
| 1061 |
/**
|
| 1062 |
+
* Parses a cookie header into a record of key-value pairs
|
| 1063 |
+
*
|
| 1064 |
+
* **Details**
|
| 1065 |
*
|
| 1066 |
* Adapted from https://github.com/fastify/fastify-cookie under MIT License
|
| 1067 |
*
|
|
|
|
| 1068 |
* @category decoding
|
| 1069 |
+
* @since 4.0.0
|
| 1070 |
*/
|
| 1071 |
export function parseHeader(header: string): Record<string, string> {
|
| 1072 |
const result: Record<string, string> = {}
|
.opencode/node_modules/effect/src/unstable/http/Etag.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Context from "../../Context.ts"
|
|
@@ -9,14 +16,22 @@ import * as Option from "../../Option.ts"
|
|
| 9 |
import type * as Body from "./HttpBody.ts"
|
| 10 |
|
| 11 |
/**
|
| 12 |
-
*
|
|
|
|
| 13 |
* @category models
|
|
|
|
| 14 |
*/
|
| 15 |
export type Etag = Weak | Strong
|
| 16 |
|
| 17 |
/**
|
| 18 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
* @category models
|
|
|
|
| 20 |
*/
|
| 21 |
export interface Weak {
|
| 22 |
readonly _tag: "Weak"
|
|
@@ -24,8 +39,14 @@ export interface Weak {
|
|
| 24 |
}
|
| 25 |
|
| 26 |
/**
|
| 27 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
* @category models
|
|
|
|
| 29 |
*/
|
| 30 |
export interface Strong {
|
| 31 |
readonly _tag: "Strong"
|
|
@@ -33,8 +54,10 @@ export interface Strong {
|
|
| 33 |
}
|
| 34 |
|
| 35 |
/**
|
|
|
|
|
|
|
|
|
|
| 36 |
* @since 4.0.0
|
| 37 |
-
* @category convertions
|
| 38 |
*/
|
| 39 |
export const toString = (self: Etag): string => {
|
| 40 |
switch (self._tag) {
|
|
@@ -46,8 +69,10 @@ export const toString = (self: Etag): string => {
|
|
| 46 |
}
|
| 47 |
|
| 48 |
/**
|
| 49 |
-
*
|
|
|
|
| 50 |
* @category models
|
|
|
|
| 51 |
*/
|
| 52 |
export class Generator extends Context.Service<Generator, {
|
| 53 |
readonly fromFileInfo: (info: FileSystem.File.Info) => Effect.Effect<Etag>
|
|
@@ -67,8 +92,25 @@ const fromFileWeb = (file: Body.HttpBody.FileLike) => {
|
|
| 67 |
}
|
| 68 |
|
| 69 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
* @since 4.0.0
|
| 71 |
-
* @category Layers
|
| 72 |
*/
|
| 73 |
export const layer: Layer.Layer<Generator> = Layer.succeed(
|
| 74 |
Generator
|
|
@@ -82,8 +124,10 @@ export const layer: Layer.Layer<Generator> = Layer.succeed(
|
|
| 82 |
})
|
| 83 |
|
| 84 |
/**
|
|
|
|
|
|
|
|
|
|
| 85 |
* @since 4.0.0
|
| 86 |
-
* @category Layers
|
| 87 |
*/
|
| 88 |
export const layerWeak: Layer.Layer<Generator> = Layer.succeed(
|
| 89 |
Generator
|
|
|
|
| 1 |
/**
|
| 2 |
+
* HTTP entity tag values and metadata-based generator layers.
|
| 3 |
+
*
|
| 4 |
+
* ETags are validators for a specific representation of a resource. Servers put
|
| 5 |
+
* them in `ETag` response headers so clients and intermediaries can revalidate
|
| 6 |
+
* cached content with `If-None-Match`, or protect writes with preconditions such
|
| 7 |
+
* as `If-Match`.
|
| 8 |
+
*
|
| 9 |
* @since 4.0.0
|
| 10 |
*/
|
| 11 |
import * as Context from "../../Context.ts"
|
|
|
|
| 16 |
import type * as Body from "./HttpBody.ts"
|
| 17 |
|
| 18 |
/**
|
| 19 |
+
* Represents an HTTP entity tag, either weak or strong.
|
| 20 |
+
*
|
| 21 |
* @category models
|
| 22 |
+
* @since 4.0.0
|
| 23 |
*/
|
| 24 |
export type Etag = Weak | Strong
|
| 25 |
|
| 26 |
/**
|
| 27 |
+
* Weak HTTP entity tag.
|
| 28 |
+
*
|
| 29 |
+
* **Details**
|
| 30 |
+
*
|
| 31 |
+
* The `value` is the raw tag value without the surrounding quotes or `W/` prefix.
|
| 32 |
+
*
|
| 33 |
* @category models
|
| 34 |
+
* @since 4.0.0
|
| 35 |
*/
|
| 36 |
export interface Weak {
|
| 37 |
readonly _tag: "Weak"
|
|
|
|
| 39 |
}
|
| 40 |
|
| 41 |
/**
|
| 42 |
+
* Strong HTTP entity tag.
|
| 43 |
+
*
|
| 44 |
+
* **Details**
|
| 45 |
+
*
|
| 46 |
+
* The `value` is the raw tag value without the surrounding quotes.
|
| 47 |
+
*
|
| 48 |
* @category models
|
| 49 |
+
* @since 4.0.0
|
| 50 |
*/
|
| 51 |
export interface Strong {
|
| 52 |
readonly _tag: "Strong"
|
|
|
|
| 54 |
}
|
| 55 |
|
| 56 |
/**
|
| 57 |
+
* Formats an `Etag` as an HTTP header value, including quotes and the `W/` prefix for weak tags.
|
| 58 |
+
*
|
| 59 |
+
* @category converting
|
| 60 |
* @since 4.0.0
|
|
|
|
| 61 |
*/
|
| 62 |
export const toString = (self: Etag): string => {
|
| 63 |
switch (self._tag) {
|
|
|
|
| 69 |
}
|
| 70 |
|
| 71 |
/**
|
| 72 |
+
* Service for generating ETags from filesystem file information or Web `File`-like metadata.
|
| 73 |
+
*
|
| 74 |
* @category models
|
| 75 |
+
* @since 4.0.0
|
| 76 |
*/
|
| 77 |
export class Generator extends Context.Service<Generator, {
|
| 78 |
readonly fromFileInfo: (info: FileSystem.File.Info) => Effect.Effect<Etag>
|
|
|
|
| 92 |
}
|
| 93 |
|
| 94 |
/**
|
| 95 |
+
* Layer that provides a `Generator` which produces strong ETags from file size
|
| 96 |
+
* and modification time metadata.
|
| 97 |
+
*
|
| 98 |
+
* **When to use**
|
| 99 |
+
*
|
| 100 |
+
* Use when you need the `Generator` service to produce strong ETags and file
|
| 101 |
+
* size plus modification time reliably change for every byte-level change.
|
| 102 |
+
*
|
| 103 |
+
* **Gotchas**
|
| 104 |
+
*
|
| 105 |
+
* This layer marks metadata-derived tags as strong. If the underlying storage
|
| 106 |
+
* can update file contents without changing the recorded size or modification
|
| 107 |
+
* time, those tags can stop representing byte-for-byte identity.
|
| 108 |
+
*
|
| 109 |
+
* @see {@link layerWeak} for weak metadata-derived ETags when byte-for-byte identity is not required
|
| 110 |
+
* @see {@link Generator} for the service provided by this layer
|
| 111 |
+
*
|
| 112 |
+
* @category layers
|
| 113 |
* @since 4.0.0
|
|
|
|
| 114 |
*/
|
| 115 |
export const layer: Layer.Layer<Generator> = Layer.succeed(
|
| 116 |
Generator
|
|
|
|
| 124 |
})
|
| 125 |
|
| 126 |
/**
|
| 127 |
+
* Layer that provides a `Generator` which produces weak ETags from file size and modification time metadata.
|
| 128 |
+
*
|
| 129 |
+
* @category layers
|
| 130 |
* @since 4.0.0
|
|
|
|
| 131 |
*/
|
| 132 |
export const layerWeak: Layer.Layer<Generator> = Layer.succeed(
|
| 133 |
Generator
|
.opencode/node_modules/effect/src/unstable/http/FetchHttpClient.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Context from "../../Context.ts"
|
|
@@ -11,16 +18,33 @@ import * as HttpClientError from "./HttpClientError.ts"
|
|
| 11 |
import * as HttpClientResponse from "./HttpClientResponse.ts"
|
| 12 |
|
| 13 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
* @since 4.0.0
|
| 15 |
-
* @category tags
|
| 16 |
*/
|
| 17 |
export const Fetch = Context.Reference<typeof globalThis.fetch>("effect/http/FetchHttpClient/Fetch", {
|
| 18 |
defaultValue: () => globalThis.fetch
|
| 19 |
})
|
| 20 |
|
| 21 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
* @since 4.0.0
|
| 23 |
-
* @category tags
|
| 24 |
*/
|
| 25 |
export class RequestInit extends Context.Service<RequestInit, globalThis.RequestInit>()(
|
| 26 |
"effect/http/FetchHttpClient/RequestInit"
|
|
@@ -29,7 +53,10 @@ export class RequestInit extends Context.Service<RequestInit, globalThis.Request
|
|
| 29 |
const fetch: HttpClient.HttpClient = HttpClient.make((request, url, signal, fiber) => {
|
| 30 |
const fetch = fiber.getRef(Fetch)
|
| 31 |
const options: globalThis.RequestInit = fiber.context.mapUnsafe.get(RequestInit.key) ?? {}
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 33 |
const send = (body: BodyInit | undefined) =>
|
| 34 |
Effect.map(
|
| 35 |
Effect.tryPromise({
|
|
@@ -65,7 +92,32 @@ const fetch: HttpClient.HttpClient = HttpClient.make((request, url, signal, fibe
|
|
| 65 |
})
|
| 66 |
|
| 67 |
/**
|
| 68 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
* @category layers
|
|
|
|
| 70 |
*/
|
| 71 |
export const layer: Layer.Layer<HttpClient.HttpClient> = HttpClient.layerMergedContext(Effect.succeed(fetch))
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Fetch-based implementation of the Effect HTTP client service.
|
| 3 |
+
*
|
| 4 |
+
* This module provides an `HttpClient` layer that executes requests through a
|
| 5 |
+
* Web Fetch API implementation. It is the transport to use in browsers, edge
|
| 6 |
+
* runtimes, and Node.js environments where `globalThis.fetch` is available, or
|
| 7 |
+
* anywhere a compatible fetch function can be supplied.
|
| 8 |
+
*
|
| 9 |
* @since 4.0.0
|
| 10 |
*/
|
| 11 |
import * as Context from "../../Context.ts"
|
|
|
|
| 18 |
import * as HttpClientResponse from "./HttpClientResponse.ts"
|
| 19 |
|
| 20 |
/**
|
| 21 |
+
* Context reference for the `fetch` implementation used by the fetch-based HTTP client.
|
| 22 |
+
*
|
| 23 |
+
* **Details**
|
| 24 |
+
*
|
| 25 |
+
* Defaults to `globalThis.fetch`.
|
| 26 |
+
*
|
| 27 |
+
* @category services
|
| 28 |
* @since 4.0.0
|
|
|
|
| 29 |
*/
|
| 30 |
export const Fetch = Context.Reference<typeof globalThis.fetch>("effect/http/FetchHttpClient/Fetch", {
|
| 31 |
defaultValue: () => globalThis.fetch
|
| 32 |
})
|
| 33 |
|
| 34 |
/**
|
| 35 |
+
* Service that contains default fetch options for the fetch-based HTTP client.
|
| 36 |
+
*
|
| 37 |
+
* **When to use**
|
| 38 |
+
*
|
| 39 |
+
* Use to provide default credentials, cache, redirect, integrity, or other
|
| 40 |
+
* fetch options for outgoing HTTP requests.
|
| 41 |
+
*
|
| 42 |
+
* **Details**
|
| 43 |
+
*
|
| 44 |
+
* Request-specific method, headers, body, and abort signal are supplied by the client when a request is executed.
|
| 45 |
+
*
|
| 46 |
+
* @category services
|
| 47 |
* @since 4.0.0
|
|
|
|
| 48 |
*/
|
| 49 |
export class RequestInit extends Context.Service<RequestInit, globalThis.RequestInit>()(
|
| 50 |
"effect/http/FetchHttpClient/RequestInit"
|
|
|
|
| 53 |
const fetch: HttpClient.HttpClient = HttpClient.make((request, url, signal, fiber) => {
|
| 54 |
const fetch = fiber.getRef(Fetch)
|
| 55 |
const options: globalThis.RequestInit = fiber.context.mapUnsafe.get(RequestInit.key) ?? {}
|
| 56 |
+
let headers = options.headers ? Headers.merge(Headers.fromInput(options.headers), request.headers) : request.headers
|
| 57 |
+
if (headers["content-length"]) {
|
| 58 |
+
headers = Headers.remove(headers, "content-length")
|
| 59 |
+
}
|
| 60 |
const send = (body: BodyInit | undefined) =>
|
| 61 |
Effect.map(
|
| 62 |
Effect.tryPromise({
|
|
|
|
| 92 |
})
|
| 93 |
|
| 94 |
/**
|
| 95 |
+
* Layer that provides an `HttpClient` implementation backed by the configured
|
| 96 |
+
* `Fetch` function.
|
| 97 |
+
*
|
| 98 |
+
* **When to use**
|
| 99 |
+
*
|
| 100 |
+
* Use when an Effect program should execute `HttpClient` requests through the
|
| 101 |
+
* platform `fetch` implementation, especially in browser, edge, or Node.js
|
| 102 |
+
* runtimes with `globalThis.fetch`.
|
| 103 |
+
*
|
| 104 |
+
* **Details**
|
| 105 |
+
*
|
| 106 |
+
* The layer uses the current `Fetch` reference and optional `RequestInit`
|
| 107 |
+
* service for each request. Request-specific method, headers, body, and abort
|
| 108 |
+
* signal are supplied by the client and override matching `RequestInit` fields.
|
| 109 |
+
*
|
| 110 |
+
* **Gotchas**
|
| 111 |
+
*
|
| 112 |
+
* Fetch behavior comes from the runtime's implementation, so CORS, cookies,
|
| 113 |
+
* redirects, abort handling, and streaming support can vary by platform. Stream
|
| 114 |
+
* request bodies are sent as Web streams with `duplex: "half"`, and any
|
| 115 |
+
* `content-length` header is removed before calling `fetch`.
|
| 116 |
+
*
|
| 117 |
+
* @see {@link Fetch} for supplying the fetch implementation used by this layer
|
| 118 |
+
* @see {@link RequestInit} for default `RequestInit` options applied before request-specific fields
|
| 119 |
+
*
|
| 120 |
* @category layers
|
| 121 |
+
* @since 4.0.0
|
| 122 |
*/
|
| 123 |
export const layer: Layer.Layer<HttpClient.HttpClient> = HttpClient.layerMergedContext(Effect.succeed(fetch))
|
.opencode/node_modules/effect/src/unstable/http/FindMyWay.ts
CHANGED
|
@@ -1,9 +1,15 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
|
| 5 |
/**
|
| 6 |
-
* @since 4.0.0
|
| 7 |
* @category re-exports
|
|
|
|
| 8 |
*/
|
| 9 |
export * from "find-my-way-ts"
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Re-exports the `find-my-way-ts` router package used by the unstable HTTP
|
| 3 |
+
* routing modules.
|
| 4 |
+
*
|
| 5 |
+
* This module keeps the router types and helpers available from the Effect HTTP
|
| 6 |
+
* namespace without wrapping or changing them.
|
| 7 |
+
*
|
| 8 |
* @since 4.0.0
|
| 9 |
*/
|
| 10 |
|
| 11 |
/**
|
|
|
|
| 12 |
* @category re-exports
|
| 13 |
+
* @since 4.0.0
|
| 14 |
*/
|
| 15 |
export * from "find-my-way-ts"
|
.opencode/node_modules/effect/src/unstable/http/Headers.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Context from "../../Context.ts"
|
|
@@ -13,41 +20,52 @@ import * as Record from "../../Record.ts"
|
|
| 13 |
import * as Redactable from "../../Redactable.ts"
|
| 14 |
import * as Redacted from "../../Redacted.ts"
|
| 15 |
import * as Schema from "../../Schema.ts"
|
| 16 |
-
import * as
|
| 17 |
import type { Mutable } from "../../Types.ts"
|
| 18 |
|
| 19 |
/**
|
| 20 |
-
*
|
| 21 |
*
|
|
|
|
| 22 |
* @since 4.0.0
|
| 23 |
-
* @category type ids
|
| 24 |
*/
|
| 25 |
export const TypeId: unique symbol = Symbol.for("~effect/http/Headers")
|
| 26 |
|
| 27 |
/**
|
|
|
|
|
|
|
|
|
|
| 28 |
* @since 4.0.0
|
| 29 |
-
* @category type ids
|
| 30 |
*/
|
| 31 |
export type TypeId = typeof TypeId
|
| 32 |
|
| 33 |
/**
|
| 34 |
-
*
|
|
|
|
| 35 |
* @category refinements
|
|
|
|
| 36 |
*/
|
| 37 |
export const isHeaders = (u: unknown): u is Headers => Predicate.hasProperty(u, TypeId)
|
| 38 |
|
| 39 |
/**
|
| 40 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
* @category models
|
|
|
|
| 42 |
*/
|
| 43 |
export interface Headers extends Redactable.Redactable {
|
| 44 |
readonly [TypeId]: TypeId
|
| 45 |
readonly [key: string]: string
|
| 46 |
}
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
[TypeId]: {
|
| 52 |
value: TypeId
|
| 53 |
},
|
|
@@ -83,20 +101,31 @@ const make = (input: Record.ReadonlyRecord<string, string>): Mutable<Headers> =>
|
|
| 83 |
Object.assign(Object.create(Proto), input) as Headers
|
| 84 |
|
| 85 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
* @since 4.0.0
|
| 87 |
-
* @category Equivalence
|
| 88 |
*/
|
| 89 |
export const Equivalence: Equ.Equivalence<Headers> = Record.makeEquivalence(Equ.strictEqual<string>())
|
| 90 |
|
| 91 |
/**
|
| 92 |
-
*
|
|
|
|
| 93 |
* @category schemas
|
|
|
|
| 94 |
*/
|
| 95 |
export interface HeadersSchema extends Schema.declare<Headers, { readonly [x: string]: string }> {}
|
| 96 |
|
| 97 |
/**
|
| 98 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
* @category schemas
|
|
|
|
| 100 |
*/
|
| 101 |
export const HeadersSchema: HeadersSchema = Schema.declare(
|
| 102 |
isHeaders,
|
|
@@ -115,7 +144,7 @@ export const HeadersSchema: HeadersSchema = Schema.declare(
|
|
| 115 |
toCodec: () =>
|
| 116 |
Schema.link<Headers>()(
|
| 117 |
Schema.Record(Schema.String, Schema.String),
|
| 118 |
-
|
| 119 |
decode: (input) => fromInput(input),
|
| 120 |
encode: (headers) => ({ ...headers })
|
| 121 |
})
|
|
@@ -124,22 +153,36 @@ export const HeadersSchema: HeadersSchema = Schema.declare(
|
|
| 124 |
)
|
| 125 |
|
| 126 |
/**
|
| 127 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
* @category models
|
|
|
|
| 129 |
*/
|
| 130 |
export type Input =
|
| 131 |
| Record.ReadonlyRecord<string, string | ReadonlyArray<string> | undefined>
|
| 132 |
| Iterable<readonly [string, string]>
|
| 133 |
|
| 134 |
/**
|
| 135 |
-
*
|
|
|
|
| 136 |
* @category constructors
|
|
|
|
| 137 |
*/
|
| 138 |
export const empty: Headers = Object.create(Proto)
|
| 139 |
|
| 140 |
/**
|
| 141 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
* @category constructors
|
|
|
|
| 143 |
*/
|
| 144 |
export const fromInput: (input?: Input) => Headers = (input) => {
|
| 145 |
if (input === undefined) {
|
|
@@ -163,92 +206,188 @@ export const fromInput: (input?: Input) => Headers = (input) => {
|
|
| 163 |
}
|
| 164 |
|
| 165 |
/**
|
| 166 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
* @category constructors
|
|
|
|
| 168 |
*/
|
| 169 |
export const fromRecordUnsafe = (input: Record.ReadonlyRecord<string, string>): Headers =>
|
| 170 |
Object.setPrototypeOf(input, Proto) as Headers
|
| 171 |
|
| 172 |
/**
|
| 173 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
* @category combinators
|
|
|
|
| 175 |
*/
|
| 176 |
export const has: {
|
| 177 |
/**
|
| 178 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
* @category combinators
|
|
|
|
| 180 |
*/
|
| 181 |
(key: string): (self: Headers) => boolean
|
| 182 |
/**
|
| 183 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
* @category combinators
|
|
|
|
| 185 |
*/
|
| 186 |
(self: Headers, key: string): boolean
|
| 187 |
} = dual<
|
| 188 |
/**
|
| 189 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
* @category combinators
|
|
|
|
| 191 |
*/
|
| 192 |
(key: string) => (self: Headers) => boolean,
|
| 193 |
/**
|
| 194 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
* @category combinators
|
|
|
|
| 196 |
*/
|
| 197 |
(self: Headers, key: string) => boolean
|
| 198 |
>(2, (self, key) => key.toLowerCase() in self)
|
| 199 |
|
| 200 |
/**
|
| 201 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
* @category combinators
|
|
|
|
| 203 |
*/
|
| 204 |
export const get: {
|
| 205 |
/**
|
| 206 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
* @category combinators
|
|
|
|
| 208 |
*/
|
| 209 |
(key: string): (self: Headers) => Option.Option<string>
|
| 210 |
/**
|
| 211 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
* @category combinators
|
|
|
|
| 213 |
*/
|
| 214 |
(self: Headers, key: string): Option.Option<string>
|
| 215 |
} = dual<
|
| 216 |
/**
|
| 217 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
* @category combinators
|
|
|
|
| 219 |
*/
|
| 220 |
(key: string) => (self: Headers) => Option.Option<string>,
|
| 221 |
/**
|
| 222 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
* @category combinators
|
|
|
|
| 224 |
*/
|
| 225 |
(self: Headers, key: string) => Option.Option<string>
|
| 226 |
>(2, (self, key) => Option.fromUndefinedOr(self[key.toLowerCase()]))
|
| 227 |
|
| 228 |
/**
|
| 229 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
* @category combinators
|
|
|
|
| 231 |
*/
|
| 232 |
export const set: {
|
| 233 |
/**
|
| 234 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
* @category combinators
|
|
|
|
| 236 |
*/
|
| 237 |
(key: string, value: string): (self: Headers) => Headers
|
| 238 |
/**
|
| 239 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
* @category combinators
|
|
|
|
| 241 |
*/
|
| 242 |
(self: Headers, key: string, value: string): Headers
|
| 243 |
} = dual<
|
| 244 |
/**
|
| 245 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
* @category combinators
|
|
|
|
| 247 |
*/
|
| 248 |
(key: string, value: string) => (self: Headers) => Headers,
|
| 249 |
/**
|
| 250 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
* @category combinators
|
|
|
|
| 252 |
*/
|
| 253 |
(self: Headers, key: string, value: string) => Headers
|
| 254 |
>(3, (self, key, value) => {
|
|
@@ -258,29 +397,59 @@ export const set: {
|
|
| 258 |
})
|
| 259 |
|
| 260 |
/**
|
| 261 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
* @category combinators
|
|
|
|
| 263 |
*/
|
| 264 |
export const setAll: {
|
| 265 |
/**
|
| 266 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
* @category combinators
|
|
|
|
| 268 |
*/
|
| 269 |
(headers: Input): (self: Headers) => Headers
|
| 270 |
/**
|
| 271 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
* @category combinators
|
|
|
|
| 273 |
*/
|
| 274 |
(self: Headers, headers: Input): Headers
|
| 275 |
} = dual<
|
| 276 |
/**
|
| 277 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
* @category combinators
|
|
|
|
| 279 |
*/
|
| 280 |
(headers: Input) => (self: Headers) => Headers,
|
| 281 |
/**
|
| 282 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
* @category combinators
|
|
|
|
| 284 |
*/
|
| 285 |
(self: Headers, headers: Input) => Headers
|
| 286 |
>(2, (self, headers) =>
|
|
@@ -290,29 +459,59 @@ export const setAll: {
|
|
| 290 |
}))
|
| 291 |
|
| 292 |
/**
|
| 293 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 294 |
* @category combinators
|
|
|
|
| 295 |
*/
|
| 296 |
export const merge: {
|
| 297 |
/**
|
| 298 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
* @category combinators
|
|
|
|
| 300 |
*/
|
| 301 |
(headers: Headers): (self: Headers) => Headers
|
| 302 |
/**
|
| 303 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
* @category combinators
|
|
|
|
| 305 |
*/
|
| 306 |
(self: Headers, headers: Headers): Headers
|
| 307 |
} = dual<
|
| 308 |
/**
|
| 309 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
* @category combinators
|
|
|
|
| 311 |
*/
|
| 312 |
(headers: Headers) => (self: Headers) => Headers,
|
| 313 |
/**
|
| 314 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
* @category combinators
|
|
|
|
| 316 |
*/
|
| 317 |
(self: Headers, headers: Headers) => Headers
|
| 318 |
>(2, (self, headers) => {
|
|
@@ -322,29 +521,59 @@ export const merge: {
|
|
| 322 |
})
|
| 323 |
|
| 324 |
/**
|
| 325 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
* @category combinators
|
|
|
|
| 327 |
*/
|
| 328 |
export const remove: {
|
| 329 |
/**
|
| 330 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 331 |
* @category combinators
|
|
|
|
| 332 |
*/
|
| 333 |
(key: string): (self: Headers) => Headers
|
| 334 |
/**
|
| 335 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
* @category combinators
|
|
|
|
| 337 |
*/
|
| 338 |
(self: Headers, key: string): Headers
|
| 339 |
} = dual<
|
| 340 |
/**
|
| 341 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
* @category combinators
|
|
|
|
| 343 |
*/
|
| 344 |
(key: string) => (self: Headers) => Headers,
|
| 345 |
/**
|
| 346 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
* @category combinators
|
|
|
|
| 348 |
*/
|
| 349 |
(self: Headers, key: string) => Headers
|
| 350 |
>(2, (self, key) => {
|
|
@@ -354,29 +583,59 @@ export const remove: {
|
|
| 354 |
})
|
| 355 |
|
| 356 |
/**
|
| 357 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
* @category combinators
|
|
|
|
| 359 |
*/
|
| 360 |
export const removeMany: {
|
| 361 |
/**
|
| 362 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
* @category combinators
|
|
|
|
| 364 |
*/
|
| 365 |
(keys: Iterable<string>): (self: Headers) => Headers
|
| 366 |
/**
|
| 367 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 368 |
* @category combinators
|
|
|
|
| 369 |
*/
|
| 370 |
(self: Headers, keys: Iterable<string>): Headers
|
| 371 |
} = dual<
|
| 372 |
/**
|
| 373 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 374 |
* @category combinators
|
|
|
|
| 375 |
*/
|
| 376 |
(keys: Iterable<string>) => (self: Headers) => Headers,
|
| 377 |
/**
|
| 378 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 379 |
* @category combinators
|
|
|
|
| 380 |
*/
|
| 381 |
(self: Headers, keys: Iterable<string>) => Headers
|
| 382 |
>(2, (self, keys) => {
|
|
@@ -388,18 +647,36 @@ export const removeMany: {
|
|
| 388 |
})
|
| 389 |
|
| 390 |
/**
|
| 391 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
* @category combinators
|
|
|
|
| 393 |
*/
|
| 394 |
export const redact: {
|
| 395 |
/**
|
| 396 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
* @category combinators
|
|
|
|
| 398 |
*/
|
| 399 |
(key: string | RegExp | ReadonlyArray<string | RegExp>): (self: Headers) => Record<string, string | Redacted.Redacted>
|
| 400 |
/**
|
| 401 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 402 |
* @category combinators
|
|
|
|
| 403 |
*/
|
| 404 |
(self: Headers, key: string | RegExp | ReadonlyArray<string | RegExp>): Record<string, string | Redacted.Redacted>
|
| 405 |
} = dual(
|
|
@@ -435,8 +712,14 @@ export const redact: {
|
|
| 435 |
)
|
| 436 |
|
| 437 |
/**
|
| 438 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 439 |
* @category fiber refs
|
|
|
|
| 440 |
*/
|
| 441 |
export const CurrentRedactedNames = Context.Reference<
|
| 442 |
ReadonlyArray<string | RegExp>
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Models HTTP headers for the unstable HTTP client and server modules.
|
| 3 |
+
*
|
| 4 |
+
* `Headers` values are immutable maps keyed by lowercase header name. This
|
| 5 |
+
* module converts common header inputs into that shape, provides helpers for
|
| 6 |
+
* reading and updating header values, and redacts configured sensitive headers
|
| 7 |
+
* when values are inspected.
|
| 8 |
+
*
|
| 9 |
* @since 4.0.0
|
| 10 |
*/
|
| 11 |
import * as Context from "../../Context.ts"
|
|
|
|
| 20 |
import * as Redactable from "../../Redactable.ts"
|
| 21 |
import * as Redacted from "../../Redacted.ts"
|
| 22 |
import * as Schema from "../../Schema.ts"
|
| 23 |
+
import * as SchemaTransformation from "../../SchemaTransformation.ts"
|
| 24 |
import type { Mutable } from "../../Types.ts"
|
| 25 |
|
| 26 |
/**
|
| 27 |
+
* Runtime type identifier for `Headers` values.
|
| 28 |
*
|
| 29 |
+
* @category type IDs
|
| 30 |
* @since 4.0.0
|
|
|
|
| 31 |
*/
|
| 32 |
export const TypeId: unique symbol = Symbol.for("~effect/http/Headers")
|
| 33 |
|
| 34 |
/**
|
| 35 |
+
* Type of the unique symbol used to brand `Headers` values.
|
| 36 |
+
*
|
| 37 |
+
* @category type IDs
|
| 38 |
* @since 4.0.0
|
|
|
|
| 39 |
*/
|
| 40 |
export type TypeId = typeof TypeId
|
| 41 |
|
| 42 |
/**
|
| 43 |
+
* Returns `true` if the provided value is a `Headers` value.
|
| 44 |
+
*
|
| 45 |
* @category refinements
|
| 46 |
+
* @since 4.0.0
|
| 47 |
*/
|
| 48 |
export const isHeaders = (u: unknown): u is Headers => Predicate.hasProperty(u, TypeId)
|
| 49 |
|
| 50 |
/**
|
| 51 |
+
* Represents an immutable HTTP header collection keyed by lowercase header name.
|
| 52 |
+
*
|
| 53 |
+
* **Details**
|
| 54 |
+
*
|
| 55 |
+
* `Headers` values also support redaction through the `Redactable` protocol.
|
| 56 |
+
*
|
| 57 |
* @category models
|
| 58 |
+
* @since 4.0.0
|
| 59 |
*/
|
| 60 |
export interface Headers extends Redactable.Redactable {
|
| 61 |
readonly [TypeId]: TypeId
|
| 62 |
readonly [key: string]: string
|
| 63 |
}
|
| 64 |
|
| 65 |
+
// the properties are folded into the initializer (rather than a separate
|
| 66 |
+
// `Object.defineProperties(Proto, ...)` statement) so the whole definition
|
| 67 |
+
// is pure-annotated by the build and tree-shakable.
|
| 68 |
+
const Proto = Object.defineProperties(Object.create(null), {
|
| 69 |
[TypeId]: {
|
| 70 |
value: TypeId
|
| 71 |
},
|
|
|
|
| 101 |
Object.assign(Object.create(Proto), input) as Headers
|
| 102 |
|
| 103 |
/**
|
| 104 |
+
* Provides an `Equivalence` instance that compares `Headers` by header names
|
| 105 |
+
* and string values.
|
| 106 |
+
*
|
| 107 |
+
* @category instances
|
| 108 |
* @since 4.0.0
|
|
|
|
| 109 |
*/
|
| 110 |
export const Equivalence: Equ.Equivalence<Headers> = Record.makeEquivalence(Equ.strictEqual<string>())
|
| 111 |
|
| 112 |
/**
|
| 113 |
+
* Schema interface for `Headers` values encoded as records of string header values.
|
| 114 |
+
*
|
| 115 |
* @category schemas
|
| 116 |
+
* @since 4.0.0
|
| 117 |
*/
|
| 118 |
export interface HeadersSchema extends Schema.declare<Headers, { readonly [x: string]: string }> {}
|
| 119 |
|
| 120 |
/**
|
| 121 |
+
* Schema for `Headers` values encoded as records of string header values.
|
| 122 |
+
*
|
| 123 |
+
* **Details**
|
| 124 |
+
*
|
| 125 |
+
* Decoding normalizes header names through `fromInput`; encoding returns a plain record.
|
| 126 |
+
*
|
| 127 |
* @category schemas
|
| 128 |
+
* @since 4.0.0
|
| 129 |
*/
|
| 130 |
export const HeadersSchema: HeadersSchema = Schema.declare(
|
| 131 |
isHeaders,
|
|
|
|
| 144 |
toCodec: () =>
|
| 145 |
Schema.link<Headers>()(
|
| 146 |
Schema.Record(Schema.String, Schema.String),
|
| 147 |
+
SchemaTransformation.transform({
|
| 148 |
decode: (input) => fromInput(input),
|
| 149 |
encode: (headers) => ({ ...headers })
|
| 150 |
})
|
|
|
|
| 153 |
)
|
| 154 |
|
| 155 |
/**
|
| 156 |
+
* Input accepted when constructing headers.
|
| 157 |
+
*
|
| 158 |
+
* **Details**
|
| 159 |
+
*
|
| 160 |
+
* Records may contain string values, string arrays, or `undefined`; arrays are joined with `", "`, and `undefined` values are omitted.
|
| 161 |
+
*
|
| 162 |
* @category models
|
| 163 |
+
* @since 4.0.0
|
| 164 |
*/
|
| 165 |
export type Input =
|
| 166 |
| Record.ReadonlyRecord<string, string | ReadonlyArray<string> | undefined>
|
| 167 |
| Iterable<readonly [string, string]>
|
| 168 |
|
| 169 |
/**
|
| 170 |
+
* An empty `Headers` collection.
|
| 171 |
+
*
|
| 172 |
* @category constructors
|
| 173 |
+
* @since 4.0.0
|
| 174 |
*/
|
| 175 |
export const empty: Headers = Object.create(Proto)
|
| 176 |
|
| 177 |
/**
|
| 178 |
+
* Creates `Headers` from a record or iterable of header entries.
|
| 179 |
+
*
|
| 180 |
+
* **Details**
|
| 181 |
+
*
|
| 182 |
+
* Header names are normalized to lowercase. Array values in record input are joined with `", "`, and `undefined` values are omitted.
|
| 183 |
+
*
|
| 184 |
* @category constructors
|
| 185 |
+
* @since 4.0.0
|
| 186 |
*/
|
| 187 |
export const fromInput: (input?: Input) => Headers = (input) => {
|
| 188 |
if (input === undefined) {
|
|
|
|
| 206 |
}
|
| 207 |
|
| 208 |
/**
|
| 209 |
+
* Treats an existing record as `Headers` unsafely.
|
| 210 |
+
*
|
| 211 |
+
* **Gotchas**
|
| 212 |
+
*
|
| 213 |
+
* This mutates the record's prototype and does not normalize header names; callers must provide the expected lowercase keys.
|
| 214 |
+
*
|
| 215 |
* @category constructors
|
| 216 |
+
* @since 4.0.0
|
| 217 |
*/
|
| 218 |
export const fromRecordUnsafe = (input: Record.ReadonlyRecord<string, string>): Headers =>
|
| 219 |
Object.setPrototypeOf(input, Proto) as Headers
|
| 220 |
|
| 221 |
/**
|
| 222 |
+
* Returns `true` when a header with the given name is present.
|
| 223 |
+
*
|
| 224 |
+
* **Details**
|
| 225 |
+
*
|
| 226 |
+
* The lookup lowercases the provided header name.
|
| 227 |
+
*
|
| 228 |
* @category combinators
|
| 229 |
+
* @since 4.0.0
|
| 230 |
*/
|
| 231 |
export const has: {
|
| 232 |
/**
|
| 233 |
+
* Returns `true` when a header with the given name is present.
|
| 234 |
+
*
|
| 235 |
+
* **Details**
|
| 236 |
+
*
|
| 237 |
+
* The lookup lowercases the provided header name.
|
| 238 |
+
*
|
| 239 |
* @category combinators
|
| 240 |
+
* @since 4.0.0
|
| 241 |
*/
|
| 242 |
(key: string): (self: Headers) => boolean
|
| 243 |
/**
|
| 244 |
+
* Returns `true` when a header with the given name is present.
|
| 245 |
+
*
|
| 246 |
+
* **Details**
|
| 247 |
+
*
|
| 248 |
+
* The lookup lowercases the provided header name.
|
| 249 |
+
*
|
| 250 |
* @category combinators
|
| 251 |
+
* @since 4.0.0
|
| 252 |
*/
|
| 253 |
(self: Headers, key: string): boolean
|
| 254 |
} = dual<
|
| 255 |
/**
|
| 256 |
+
* Returns `true` when a header with the given name is present.
|
| 257 |
+
*
|
| 258 |
+
* **Details**
|
| 259 |
+
*
|
| 260 |
+
* The lookup lowercases the provided header name.
|
| 261 |
+
*
|
| 262 |
* @category combinators
|
| 263 |
+
* @since 4.0.0
|
| 264 |
*/
|
| 265 |
(key: string) => (self: Headers) => boolean,
|
| 266 |
/**
|
| 267 |
+
* Returns `true` when a header with the given name is present.
|
| 268 |
+
*
|
| 269 |
+
* **Details**
|
| 270 |
+
*
|
| 271 |
+
* The lookup lowercases the provided header name.
|
| 272 |
+
*
|
| 273 |
* @category combinators
|
| 274 |
+
* @since 4.0.0
|
| 275 |
*/
|
| 276 |
(self: Headers, key: string) => boolean
|
| 277 |
>(2, (self, key) => key.toLowerCase() in self)
|
| 278 |
|
| 279 |
/**
|
| 280 |
+
* Gets a header value by name safely.
|
| 281 |
+
*
|
| 282 |
+
* **Details**
|
| 283 |
+
*
|
| 284 |
+
* The lookup lowercases the provided header name and returns `Option.none()` when absent.
|
| 285 |
+
*
|
| 286 |
* @category combinators
|
| 287 |
+
* @since 4.0.0
|
| 288 |
*/
|
| 289 |
export const get: {
|
| 290 |
/**
|
| 291 |
+
* Gets a header value by name safely.
|
| 292 |
+
*
|
| 293 |
+
* **Details**
|
| 294 |
+
*
|
| 295 |
+
* The lookup lowercases the provided header name and returns `Option.none()` when absent.
|
| 296 |
+
*
|
| 297 |
* @category combinators
|
| 298 |
+
* @since 4.0.0
|
| 299 |
*/
|
| 300 |
(key: string): (self: Headers) => Option.Option<string>
|
| 301 |
/**
|
| 302 |
+
* Gets a header value by name safely.
|
| 303 |
+
*
|
| 304 |
+
* **Details**
|
| 305 |
+
*
|
| 306 |
+
* The lookup lowercases the provided header name and returns `Option.none()` when absent.
|
| 307 |
+
*
|
| 308 |
* @category combinators
|
| 309 |
+
* @since 4.0.0
|
| 310 |
*/
|
| 311 |
(self: Headers, key: string): Option.Option<string>
|
| 312 |
} = dual<
|
| 313 |
/**
|
| 314 |
+
* Gets a header value by name safely.
|
| 315 |
+
*
|
| 316 |
+
* **Details**
|
| 317 |
+
*
|
| 318 |
+
* The lookup lowercases the provided header name and returns `Option.none()` when absent.
|
| 319 |
+
*
|
| 320 |
* @category combinators
|
| 321 |
+
* @since 4.0.0
|
| 322 |
*/
|
| 323 |
(key: string) => (self: Headers) => Option.Option<string>,
|
| 324 |
/**
|
| 325 |
+
* Gets a header value by name safely.
|
| 326 |
+
*
|
| 327 |
+
* **Details**
|
| 328 |
+
*
|
| 329 |
+
* The lookup lowercases the provided header name and returns `Option.none()` when absent.
|
| 330 |
+
*
|
| 331 |
* @category combinators
|
| 332 |
+
* @since 4.0.0
|
| 333 |
*/
|
| 334 |
(self: Headers, key: string) => Option.Option<string>
|
| 335 |
>(2, (self, key) => Option.fromUndefinedOr(self[key.toLowerCase()]))
|
| 336 |
|
| 337 |
/**
|
| 338 |
+
* Returns a new `Headers` collection with the given header set.
|
| 339 |
+
*
|
| 340 |
+
* **Details**
|
| 341 |
+
*
|
| 342 |
+
* The header name is normalized to lowercase.
|
| 343 |
+
*
|
| 344 |
* @category combinators
|
| 345 |
+
* @since 4.0.0
|
| 346 |
*/
|
| 347 |
export const set: {
|
| 348 |
/**
|
| 349 |
+
* Returns a new `Headers` collection with the given header set.
|
| 350 |
+
*
|
| 351 |
+
* **Details**
|
| 352 |
+
*
|
| 353 |
+
* The header name is normalized to lowercase.
|
| 354 |
+
*
|
| 355 |
* @category combinators
|
| 356 |
+
* @since 4.0.0
|
| 357 |
*/
|
| 358 |
(key: string, value: string): (self: Headers) => Headers
|
| 359 |
/**
|
| 360 |
+
* Returns a new `Headers` collection with the given header set.
|
| 361 |
+
*
|
| 362 |
+
* **Details**
|
| 363 |
+
*
|
| 364 |
+
* The header name is normalized to lowercase.
|
| 365 |
+
*
|
| 366 |
* @category combinators
|
| 367 |
+
* @since 4.0.0
|
| 368 |
*/
|
| 369 |
(self: Headers, key: string, value: string): Headers
|
| 370 |
} = dual<
|
| 371 |
/**
|
| 372 |
+
* Returns a new `Headers` collection with the given header set.
|
| 373 |
+
*
|
| 374 |
+
* **Details**
|
| 375 |
+
*
|
| 376 |
+
* The header name is normalized to lowercase.
|
| 377 |
+
*
|
| 378 |
* @category combinators
|
| 379 |
+
* @since 4.0.0
|
| 380 |
*/
|
| 381 |
(key: string, value: string) => (self: Headers) => Headers,
|
| 382 |
/**
|
| 383 |
+
* Returns a new `Headers` collection with the given header set.
|
| 384 |
+
*
|
| 385 |
+
* **Details**
|
| 386 |
+
*
|
| 387 |
+
* The header name is normalized to lowercase.
|
| 388 |
+
*
|
| 389 |
* @category combinators
|
| 390 |
+
* @since 4.0.0
|
| 391 |
*/
|
| 392 |
(self: Headers, key: string, value: string) => Headers
|
| 393 |
>(3, (self, key, value) => {
|
|
|
|
| 397 |
})
|
| 398 |
|
| 399 |
/**
|
| 400 |
+
* Returns a new `Headers` collection with all provided headers set.
|
| 401 |
+
*
|
| 402 |
+
* **Details**
|
| 403 |
+
*
|
| 404 |
+
* Input headers are normalized with `fromInput` and override existing headers with the same lowercase name.
|
| 405 |
+
*
|
| 406 |
* @category combinators
|
| 407 |
+
* @since 4.0.0
|
| 408 |
*/
|
| 409 |
export const setAll: {
|
| 410 |
/**
|
| 411 |
+
* Returns a new `Headers` collection with all provided headers set.
|
| 412 |
+
*
|
| 413 |
+
* **Details**
|
| 414 |
+
*
|
| 415 |
+
* Input headers are normalized with `fromInput` and override existing headers with the same lowercase name.
|
| 416 |
+
*
|
| 417 |
* @category combinators
|
| 418 |
+
* @since 4.0.0
|
| 419 |
*/
|
| 420 |
(headers: Input): (self: Headers) => Headers
|
| 421 |
/**
|
| 422 |
+
* Returns a new `Headers` collection with all provided headers set.
|
| 423 |
+
*
|
| 424 |
+
* **Details**
|
| 425 |
+
*
|
| 426 |
+
* Input headers are normalized with `fromInput` and override existing headers with the same lowercase name.
|
| 427 |
+
*
|
| 428 |
* @category combinators
|
| 429 |
+
* @since 4.0.0
|
| 430 |
*/
|
| 431 |
(self: Headers, headers: Input): Headers
|
| 432 |
} = dual<
|
| 433 |
/**
|
| 434 |
+
* Returns a new `Headers` collection with all provided headers set.
|
| 435 |
+
*
|
| 436 |
+
* **Details**
|
| 437 |
+
*
|
| 438 |
+
* Input headers are normalized with `fromInput` and override existing headers with the same lowercase name.
|
| 439 |
+
*
|
| 440 |
* @category combinators
|
| 441 |
+
* @since 4.0.0
|
| 442 |
*/
|
| 443 |
(headers: Input) => (self: Headers) => Headers,
|
| 444 |
/**
|
| 445 |
+
* Returns a new `Headers` collection with all provided headers set.
|
| 446 |
+
*
|
| 447 |
+
* **Details**
|
| 448 |
+
*
|
| 449 |
+
* Input headers are normalized with `fromInput` and override existing headers with the same lowercase name.
|
| 450 |
+
*
|
| 451 |
* @category combinators
|
| 452 |
+
* @since 4.0.0
|
| 453 |
*/
|
| 454 |
(self: Headers, headers: Input) => Headers
|
| 455 |
>(2, (self, headers) =>
|
|
|
|
| 459 |
}))
|
| 460 |
|
| 461 |
/**
|
| 462 |
+
* Returns a new `Headers` collection containing headers from both collections.
|
| 463 |
+
*
|
| 464 |
+
* **Details**
|
| 465 |
+
*
|
| 466 |
+
* Headers from the second collection override headers from the first collection with the same name.
|
| 467 |
+
*
|
| 468 |
* @category combinators
|
| 469 |
+
* @since 4.0.0
|
| 470 |
*/
|
| 471 |
export const merge: {
|
| 472 |
/**
|
| 473 |
+
* Returns a new `Headers` collection containing headers from both collections.
|
| 474 |
+
*
|
| 475 |
+
* **Details**
|
| 476 |
+
*
|
| 477 |
+
* Headers from the second collection override headers from the first collection with the same name.
|
| 478 |
+
*
|
| 479 |
* @category combinators
|
| 480 |
+
* @since 4.0.0
|
| 481 |
*/
|
| 482 |
(headers: Headers): (self: Headers) => Headers
|
| 483 |
/**
|
| 484 |
+
* Returns a new `Headers` collection containing headers from both collections.
|
| 485 |
+
*
|
| 486 |
+
* **Details**
|
| 487 |
+
*
|
| 488 |
+
* Headers from the second collection override headers from the first collection with the same name.
|
| 489 |
+
*
|
| 490 |
* @category combinators
|
| 491 |
+
* @since 4.0.0
|
| 492 |
*/
|
| 493 |
(self: Headers, headers: Headers): Headers
|
| 494 |
} = dual<
|
| 495 |
/**
|
| 496 |
+
* Returns a new `Headers` collection containing headers from both collections.
|
| 497 |
+
*
|
| 498 |
+
* **Details**
|
| 499 |
+
*
|
| 500 |
+
* Headers from the second collection override headers from the first collection with the same name.
|
| 501 |
+
*
|
| 502 |
* @category combinators
|
| 503 |
+
* @since 4.0.0
|
| 504 |
*/
|
| 505 |
(headers: Headers) => (self: Headers) => Headers,
|
| 506 |
/**
|
| 507 |
+
* Returns a new `Headers` collection containing headers from both collections.
|
| 508 |
+
*
|
| 509 |
+
* **Details**
|
| 510 |
+
*
|
| 511 |
+
* Headers from the second collection override headers from the first collection with the same name.
|
| 512 |
+
*
|
| 513 |
* @category combinators
|
| 514 |
+
* @since 4.0.0
|
| 515 |
*/
|
| 516 |
(self: Headers, headers: Headers) => Headers
|
| 517 |
>(2, (self, headers) => {
|
|
|
|
| 521 |
})
|
| 522 |
|
| 523 |
/**
|
| 524 |
+
* Returns a new `Headers` collection with the named header removed.
|
| 525 |
+
*
|
| 526 |
+
* **Details**
|
| 527 |
+
*
|
| 528 |
+
* The provided header name is normalized to lowercase before removal.
|
| 529 |
+
*
|
| 530 |
* @category combinators
|
| 531 |
+
* @since 4.0.0
|
| 532 |
*/
|
| 533 |
export const remove: {
|
| 534 |
/**
|
| 535 |
+
* Returns a new `Headers` collection with the named header removed.
|
| 536 |
+
*
|
| 537 |
+
* **Details**
|
| 538 |
+
*
|
| 539 |
+
* The provided header name is normalized to lowercase before removal.
|
| 540 |
+
*
|
| 541 |
* @category combinators
|
| 542 |
+
* @since 4.0.0
|
| 543 |
*/
|
| 544 |
(key: string): (self: Headers) => Headers
|
| 545 |
/**
|
| 546 |
+
* Returns a new `Headers` collection with the named header removed.
|
| 547 |
+
*
|
| 548 |
+
* **Details**
|
| 549 |
+
*
|
| 550 |
+
* The provided header name is normalized to lowercase before removal.
|
| 551 |
+
*
|
| 552 |
* @category combinators
|
| 553 |
+
* @since 4.0.0
|
| 554 |
*/
|
| 555 |
(self: Headers, key: string): Headers
|
| 556 |
} = dual<
|
| 557 |
/**
|
| 558 |
+
* Returns a new `Headers` collection with the named header removed.
|
| 559 |
+
*
|
| 560 |
+
* **Details**
|
| 561 |
+
*
|
| 562 |
+
* The provided header name is normalized to lowercase before removal.
|
| 563 |
+
*
|
| 564 |
* @category combinators
|
| 565 |
+
* @since 4.0.0
|
| 566 |
*/
|
| 567 |
(key: string) => (self: Headers) => Headers,
|
| 568 |
/**
|
| 569 |
+
* Returns a new `Headers` collection with the named header removed.
|
| 570 |
+
*
|
| 571 |
+
* **Details**
|
| 572 |
+
*
|
| 573 |
+
* The provided header name is normalized to lowercase before removal.
|
| 574 |
+
*
|
| 575 |
* @category combinators
|
| 576 |
+
* @since 4.0.0
|
| 577 |
*/
|
| 578 |
(self: Headers, key: string) => Headers
|
| 579 |
>(2, (self, key) => {
|
|
|
|
| 583 |
})
|
| 584 |
|
| 585 |
/**
|
| 586 |
+
* Returns a new `Headers` collection with each named header removed.
|
| 587 |
+
*
|
| 588 |
+
* **Details**
|
| 589 |
+
*
|
| 590 |
+
* Each provided header name is normalized to lowercase before removal.
|
| 591 |
+
*
|
| 592 |
* @category combinators
|
| 593 |
+
* @since 4.0.0
|
| 594 |
*/
|
| 595 |
export const removeMany: {
|
| 596 |
/**
|
| 597 |
+
* Returns a new `Headers` collection with each named header removed.
|
| 598 |
+
*
|
| 599 |
+
* **Details**
|
| 600 |
+
*
|
| 601 |
+
* Each provided header name is normalized to lowercase before removal.
|
| 602 |
+
*
|
| 603 |
* @category combinators
|
| 604 |
+
* @since 4.0.0
|
| 605 |
*/
|
| 606 |
(keys: Iterable<string>): (self: Headers) => Headers
|
| 607 |
/**
|
| 608 |
+
* Returns a new `Headers` collection with each named header removed.
|
| 609 |
+
*
|
| 610 |
+
* **Details**
|
| 611 |
+
*
|
| 612 |
+
* Each provided header name is normalized to lowercase before removal.
|
| 613 |
+
*
|
| 614 |
* @category combinators
|
| 615 |
+
* @since 4.0.0
|
| 616 |
*/
|
| 617 |
(self: Headers, keys: Iterable<string>): Headers
|
| 618 |
} = dual<
|
| 619 |
/**
|
| 620 |
+
* Returns a new `Headers` collection with each named header removed.
|
| 621 |
+
*
|
| 622 |
+
* **Details**
|
| 623 |
+
*
|
| 624 |
+
* Each provided header name is normalized to lowercase before removal.
|
| 625 |
+
*
|
| 626 |
* @category combinators
|
| 627 |
+
* @since 4.0.0
|
| 628 |
*/
|
| 629 |
(keys: Iterable<string>) => (self: Headers) => Headers,
|
| 630 |
/**
|
| 631 |
+
* Returns a new `Headers` collection with each named header removed.
|
| 632 |
+
*
|
| 633 |
+
* **Details**
|
| 634 |
+
*
|
| 635 |
+
* Each provided header name is normalized to lowercase before removal.
|
| 636 |
+
*
|
| 637 |
* @category combinators
|
| 638 |
+
* @since 4.0.0
|
| 639 |
*/
|
| 640 |
(self: Headers, keys: Iterable<string>) => Headers
|
| 641 |
>(2, (self, keys) => {
|
|
|
|
| 647 |
})
|
| 648 |
|
| 649 |
/**
|
| 650 |
+
* Returns a plain record with selected header values wrapped in `Redacted`.
|
| 651 |
+
*
|
| 652 |
+
* **Details**
|
| 653 |
+
*
|
| 654 |
+
* String keys are normalized to lowercase before matching; regular expressions are tested against the stored header names.
|
| 655 |
+
*
|
| 656 |
* @category combinators
|
| 657 |
+
* @since 4.0.0
|
| 658 |
*/
|
| 659 |
export const redact: {
|
| 660 |
/**
|
| 661 |
+
* Returns a plain record with selected header values wrapped in `Redacted`.
|
| 662 |
+
*
|
| 663 |
+
* **Details**
|
| 664 |
+
*
|
| 665 |
+
* String keys are normalized to lowercase before matching; regular expressions are tested against the stored header names.
|
| 666 |
+
*
|
| 667 |
* @category combinators
|
| 668 |
+
* @since 4.0.0
|
| 669 |
*/
|
| 670 |
(key: string | RegExp | ReadonlyArray<string | RegExp>): (self: Headers) => Record<string, string | Redacted.Redacted>
|
| 671 |
/**
|
| 672 |
+
* Returns a plain record with selected header values wrapped in `Redacted`.
|
| 673 |
+
*
|
| 674 |
+
* **Details**
|
| 675 |
+
*
|
| 676 |
+
* String keys are normalized to lowercase before matching; regular expressions are tested against the stored header names.
|
| 677 |
+
*
|
| 678 |
* @category combinators
|
| 679 |
+
* @since 4.0.0
|
| 680 |
*/
|
| 681 |
(self: Headers, key: string | RegExp | ReadonlyArray<string | RegExp>): Record<string, string | Redacted.Redacted>
|
| 682 |
} = dual(
|
|
|
|
| 712 |
)
|
| 713 |
|
| 714 |
/**
|
| 715 |
+
* Context reference listing header names or patterns that should be redacted when `Headers` are inspected or rendered.
|
| 716 |
+
*
|
| 717 |
+
* **Details**
|
| 718 |
+
*
|
| 719 |
+
* Defaults include `authorization`, `cookie`, `set-cookie`, and `x-api-key`.
|
| 720 |
+
*
|
| 721 |
* @category fiber refs
|
| 722 |
+
* @since 4.0.0
|
| 723 |
*/
|
| 724 |
export const CurrentRedactedNames = Context.Reference<
|
| 725 |
ReadonlyArray<string | RegExp>
|
.opencode/node_modules/effect/src/unstable/http/HttpBody.ts
CHANGED
|
@@ -1,4 +1,14 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Data from "../../Data.ts"
|
|
@@ -11,31 +21,47 @@ import * as Predicate from "../../Predicate.ts"
|
|
| 11 |
import * as Schema from "../../Schema.ts"
|
| 12 |
import type { ParseOptions } from "../../SchemaAST.ts"
|
| 13 |
import type { Issue } from "../../SchemaIssue.ts"
|
| 14 |
-
import * as
|
| 15 |
import type * as Stream_ from "../../Stream.ts"
|
| 16 |
import * as UrlParams from "./UrlParams.ts"
|
| 17 |
|
| 18 |
const TypeId = "~effect/http/HttpBody"
|
| 19 |
|
| 20 |
/**
|
| 21 |
-
*
|
|
|
|
| 22 |
* @category refinements
|
|
|
|
| 23 |
*/
|
| 24 |
export const isHttpBody = (u: unknown): u is HttpBody => Predicate.hasProperty(u, TypeId)
|
| 25 |
|
| 26 |
/**
|
| 27 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
* @category models
|
|
|
|
| 29 |
*/
|
| 30 |
export type HttpBody = Empty | Raw | Uint8Array | FormData | Stream
|
| 31 |
|
| 32 |
/**
|
|
|
|
|
|
|
| 33 |
* @since 4.0.0
|
| 34 |
*/
|
| 35 |
export declare namespace HttpBody {
|
| 36 |
/**
|
| 37 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
* @category models
|
|
|
|
| 39 |
*/
|
| 40 |
export interface Proto extends Inspectable.Inspectable {
|
| 41 |
readonly [TypeId]: typeof TypeId
|
|
@@ -45,8 +71,10 @@ export declare namespace HttpBody {
|
|
| 45 |
}
|
| 46 |
|
| 47 |
/**
|
| 48 |
-
*
|
|
|
|
| 49 |
* @category models
|
|
|
|
| 50 |
*/
|
| 51 |
export interface FileLike {
|
| 52 |
readonly name: string
|
|
@@ -60,22 +88,32 @@ export declare namespace HttpBody {
|
|
| 60 |
const HttpBodyErrorTypeId = "~effect/http/HttpBody/HttpBodyError"
|
| 61 |
|
| 62 |
/**
|
| 63 |
-
*
|
|
|
|
| 64 |
* @category errors
|
|
|
|
| 65 |
*/
|
| 66 |
export class HttpBodyError extends Data.TaggedError("HttpBodyError")<{
|
| 67 |
readonly reason: ErrorReason
|
| 68 |
readonly cause?: unknown
|
| 69 |
}> {
|
| 70 |
/**
|
|
|
|
|
|
|
| 71 |
* @since 4.0.0
|
| 72 |
*/
|
| 73 |
readonly [HttpBodyErrorTypeId] = HttpBodyErrorTypeId
|
| 74 |
}
|
| 75 |
|
| 76 |
/**
|
| 77 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
* @category errors
|
|
|
|
| 79 |
*/
|
| 80 |
export type ErrorReason = {
|
| 81 |
readonly _tag: "JsonError"
|
|
@@ -100,8 +138,10 @@ abstract class Proto implements HttpBody.Proto {
|
|
| 100 |
}
|
| 101 |
|
| 102 |
/**
|
| 103 |
-
*
|
|
|
|
| 104 |
* @category models
|
|
|
|
| 105 |
*/
|
| 106 |
export class Empty extends Proto {
|
| 107 |
readonly _tag = "Empty"
|
|
@@ -114,14 +154,22 @@ export class Empty extends Proto {
|
|
| 114 |
}
|
| 115 |
|
| 116 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
* @since 4.0.0
|
| 118 |
-
* @category constructors
|
| 119 |
*/
|
| 120 |
export const empty: Empty = new Empty()
|
| 121 |
|
| 122 |
/**
|
| 123 |
-
*
|
|
|
|
| 124 |
* @category models
|
|
|
|
| 125 |
*/
|
| 126 |
export class Raw extends Proto {
|
| 127 |
readonly _tag = "Raw"
|
|
@@ -151,8 +199,10 @@ export class Raw extends Proto {
|
|
| 151 |
}
|
| 152 |
|
| 153 |
/**
|
| 154 |
-
*
|
|
|
|
| 155 |
* @category constructors
|
|
|
|
| 156 |
*/
|
| 157 |
export const raw = (
|
| 158 |
body: unknown,
|
|
@@ -163,8 +213,14 @@ export const raw = (
|
|
| 163 |
): Raw => new Raw(body, options?.contentType, options?.contentLength)
|
| 164 |
|
| 165 |
/**
|
| 166 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
* @category models
|
|
|
|
| 168 |
*/
|
| 169 |
export class Uint8Array extends Proto {
|
| 170 |
readonly _tag = "Uint8Array"
|
|
@@ -195,8 +251,14 @@ export class Uint8Array extends Proto {
|
|
| 195 |
}
|
| 196 |
|
| 197 |
/**
|
| 198 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
* @category constructors
|
|
|
|
| 200 |
*/
|
| 201 |
export const uint8Array = (body: globalThis.Uint8Array, contentType?: string): Uint8Array =>
|
| 202 |
new Uint8Array(body, contentType ?? "application/octet-stream", body.length)
|
|
@@ -204,22 +266,40 @@ export const uint8Array = (body: globalThis.Uint8Array, contentType?: string): U
|
|
| 204 |
const encoder = new TextEncoder()
|
| 205 |
|
| 206 |
/**
|
| 207 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
* @category constructors
|
|
|
|
| 209 |
*/
|
| 210 |
export const text = (body: string, contentType?: string): Uint8Array =>
|
| 211 |
uint8Array(encoder.encode(body), contentType ?? "text/plain")
|
| 212 |
|
| 213 |
/**
|
| 214 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
* @category constructors
|
|
|
|
| 216 |
*/
|
| 217 |
export const jsonUnsafe = (body: unknown, contentType?: string): Uint8Array =>
|
| 218 |
text(JSON.stringify(body), contentType ?? "application/json")
|
| 219 |
|
| 220 |
/**
|
| 221 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
* @category constructors
|
|
|
|
| 223 |
*/
|
| 224 |
export const json = (body: unknown, contentType?: string): Effect.Effect<Uint8Array, HttpBodyError> =>
|
| 225 |
Effect.try({
|
|
@@ -228,14 +308,20 @@ export const json = (body: unknown, contentType?: string): Effect.Effect<Uint8Ar
|
|
| 228 |
})
|
| 229 |
|
| 230 |
/**
|
| 231 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
* @category constructors
|
|
|
|
| 233 |
*/
|
| 234 |
export const jsonSchema = <S extends Schema.Top>(
|
| 235 |
schema: S,
|
| 236 |
options?: ParseOptions | undefined
|
| 237 |
) => {
|
| 238 |
-
const encode =
|
| 239 |
return (body: S["Type"], contentType?: string): Effect.Effect<Uint8Array, HttpBodyError, S["EncodingServices"]> =>
|
| 240 |
encode(body, options).pipe(
|
| 241 |
Effect.mapError((issue) => new HttpBodyError({ reason: { _tag: "SchemaError", issue }, cause: issue })),
|
|
@@ -244,15 +330,23 @@ export const jsonSchema = <S extends Schema.Top>(
|
|
| 244 |
}
|
| 245 |
|
| 246 |
/**
|
| 247 |
-
*
|
|
|
|
| 248 |
* @category constructors
|
|
|
|
| 249 |
*/
|
| 250 |
export const urlParams = (urlParams: UrlParams.UrlParams, contentType?: string): Uint8Array =>
|
| 251 |
text(UrlParams.toString(urlParams), contentType ?? "application/x-www-form-urlencoded")
|
| 252 |
|
| 253 |
/**
|
| 254 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
* @category models
|
|
|
|
| 256 |
*/
|
| 257 |
export class FormData extends Proto {
|
| 258 |
readonly _tag = "FormData"
|
|
@@ -276,20 +370,34 @@ export class FormData extends Proto {
|
|
| 276 |
}
|
| 277 |
|
| 278 |
/**
|
| 279 |
-
*
|
|
|
|
| 280 |
* @category constructors
|
|
|
|
| 281 |
*/
|
| 282 |
export const formData = (body: globalThis.FormData): FormData => new FormData(body)
|
| 283 |
|
| 284 |
/**
|
| 285 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
* @category models
|
|
|
|
| 287 |
*/
|
| 288 |
export type FormDataInput = Record<string, FormDataCoercible | ReadonlyArray<FormDataCoercible>>
|
| 289 |
|
| 290 |
/**
|
| 291 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
* @category models
|
|
|
|
| 293 |
*/
|
| 294 |
export type FormDataCoercible = string | number | boolean | globalThis.File | globalThis.Blob | null | undefined
|
| 295 |
|
|
@@ -305,8 +413,14 @@ const appendFormDataValue = (formData: globalThis.FormData, key: string, value:
|
|
| 305 |
}
|
| 306 |
|
| 307 |
/**
|
| 308 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
* @category constructors
|
|
|
|
| 310 |
*/
|
| 311 |
export const formDataRecord = (entries: FormDataInput): FormData => {
|
| 312 |
const data = new globalThis.FormData()
|
|
@@ -323,8 +437,10 @@ export const formDataRecord = (entries: FormDataInput): FormData => {
|
|
| 323 |
}
|
| 324 |
|
| 325 |
/**
|
| 326 |
-
*
|
|
|
|
| 327 |
* @category models
|
|
|
|
| 328 |
*/
|
| 329 |
export class Stream extends Proto {
|
| 330 |
readonly _tag = "Stream"
|
|
@@ -353,8 +469,14 @@ export class Stream extends Proto {
|
|
| 353 |
}
|
| 354 |
|
| 355 |
/**
|
| 356 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 357 |
* @category constructors
|
|
|
|
| 358 |
*/
|
| 359 |
export const stream = (
|
| 360 |
body: Stream_.Stream<globalThis.Uint8Array, unknown>,
|
|
@@ -363,8 +485,14 @@ export const stream = (
|
|
| 363 |
): Stream => new Stream(body, contentType ?? "application/octet-stream", contentLength)
|
| 364 |
|
| 365 |
/**
|
| 366 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 367 |
* @category constructors
|
|
|
|
| 368 |
*/
|
| 369 |
export const file = (
|
| 370 |
path: string,
|
|
@@ -376,7 +504,7 @@ export const file = (
|
|
| 376 |
}
|
| 377 |
): Effect.Effect<Stream, PlatformError.PlatformError, FileSystem.FileSystem> =>
|
| 378 |
Effect.flatMap(
|
| 379 |
-
FileSystem.FileSystem
|
| 380 |
(fs) =>
|
| 381 |
Effect.map(fs.stat(path), (info) =>
|
| 382 |
stream(
|
|
@@ -387,8 +515,14 @@ export const file = (
|
|
| 387 |
)
|
| 388 |
|
| 389 |
/**
|
| 390 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 391 |
* @category constructors
|
|
|
|
| 392 |
*/
|
| 393 |
export const fileFromInfo = (
|
| 394 |
path: string,
|
|
@@ -401,7 +535,7 @@ export const fileFromInfo = (
|
|
| 401 |
}
|
| 402 |
): Effect.Effect<Stream, PlatformError.PlatformError, FileSystem.FileSystem> =>
|
| 403 |
Effect.map(
|
| 404 |
-
FileSystem.FileSystem
|
| 405 |
(fs) =>
|
| 406 |
stream(
|
| 407 |
fs.stream(path, options),
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Describes HTTP request and response bodies before they reach a platform
|
| 3 |
+
* adapter.
|
| 4 |
+
*
|
| 5 |
+
* `HttpBody` is the shared body representation used by the HTTP modules. Each
|
| 6 |
+
* variant stores the payload together with metadata that can be known before
|
| 7 |
+
* sending it, such as `contentType` and `contentLength`. This module includes
|
| 8 |
+
* body constructors for common payload shapes, support for schema-encoded JSON
|
| 9 |
+
* bodies, streaming and file-backed bodies, and the error type used when body
|
| 10 |
+
* construction fails.
|
| 11 |
+
*
|
| 12 |
* @since 4.0.0
|
| 13 |
*/
|
| 14 |
import * as Data from "../../Data.ts"
|
|
|
|
| 21 |
import * as Schema from "../../Schema.ts"
|
| 22 |
import type { ParseOptions } from "../../SchemaAST.ts"
|
| 23 |
import type { Issue } from "../../SchemaIssue.ts"
|
| 24 |
+
import * as SchemaParser from "../../SchemaParser.ts"
|
| 25 |
import type * as Stream_ from "../../Stream.ts"
|
| 26 |
import * as UrlParams from "./UrlParams.ts"
|
| 27 |
|
| 28 |
const TypeId = "~effect/http/HttpBody"
|
| 29 |
|
| 30 |
/**
|
| 31 |
+
* Returns `true` if the provided value is an `HttpBody`.
|
| 32 |
+
*
|
| 33 |
* @category refinements
|
| 34 |
+
* @since 4.0.0
|
| 35 |
*/
|
| 36 |
export const isHttpBody = (u: unknown): u is HttpBody => Predicate.hasProperty(u, TypeId)
|
| 37 |
|
| 38 |
/**
|
| 39 |
+
* Represents an HTTP request body.
|
| 40 |
+
*
|
| 41 |
+
* **Details**
|
| 42 |
+
*
|
| 43 |
+
* Supported variants include empty bodies, raw bodies, byte arrays, `FormData`, and streams of bytes.
|
| 44 |
+
*
|
| 45 |
* @category models
|
| 46 |
+
* @since 4.0.0
|
| 47 |
*/
|
| 48 |
export type HttpBody = Empty | Raw | Uint8Array | FormData | Stream
|
| 49 |
|
| 50 |
/**
|
| 51 |
+
* Namespace containing type-level members associated with `HttpBody`.
|
| 52 |
+
*
|
| 53 |
* @since 4.0.0
|
| 54 |
*/
|
| 55 |
export declare namespace HttpBody {
|
| 56 |
/**
|
| 57 |
+
* Common protocol implemented by all HTTP body variants.
|
| 58 |
+
*
|
| 59 |
+
* **Details**
|
| 60 |
+
*
|
| 61 |
+
* It carries the variant tag plus optional `contentType` and `contentLength` metadata.
|
| 62 |
+
*
|
| 63 |
* @category models
|
| 64 |
+
* @since 4.0.0
|
| 65 |
*/
|
| 66 |
export interface Proto extends Inspectable.Inspectable {
|
| 67 |
readonly [TypeId]: typeof TypeId
|
|
|
|
| 71 |
}
|
| 72 |
|
| 73 |
/**
|
| 74 |
+
* Minimal Web `File`-like shape used by HTTP helpers that need file metadata.
|
| 75 |
+
*
|
| 76 |
* @category models
|
| 77 |
+
* @since 4.0.0
|
| 78 |
*/
|
| 79 |
export interface FileLike {
|
| 80 |
readonly name: string
|
|
|
|
| 88 |
const HttpBodyErrorTypeId = "~effect/http/HttpBody/HttpBodyError"
|
| 89 |
|
| 90 |
/**
|
| 91 |
+
* Error produced while constructing an HTTP body from JSON or schema-encoded input.
|
| 92 |
+
*
|
| 93 |
* @category errors
|
| 94 |
+
* @since 4.0.0
|
| 95 |
*/
|
| 96 |
export class HttpBodyError extends Data.TaggedError("HttpBodyError")<{
|
| 97 |
readonly reason: ErrorReason
|
| 98 |
readonly cause?: unknown
|
| 99 |
}> {
|
| 100 |
/**
|
| 101 |
+
* Marks this value as an HTTP body error for runtime guards.
|
| 102 |
+
*
|
| 103 |
* @since 4.0.0
|
| 104 |
*/
|
| 105 |
readonly [HttpBodyErrorTypeId] = HttpBodyErrorTypeId
|
| 106 |
}
|
| 107 |
|
| 108 |
/**
|
| 109 |
+
* Reason for an `HttpBodyError`.
|
| 110 |
+
*
|
| 111 |
+
* **Details**
|
| 112 |
+
*
|
| 113 |
+
* `JsonError` represents a `JSON.stringify` failure; `SchemaError` represents a schema encoding issue.
|
| 114 |
+
*
|
| 115 |
* @category errors
|
| 116 |
+
* @since 4.0.0
|
| 117 |
*/
|
| 118 |
export type ErrorReason = {
|
| 119 |
readonly _tag: "JsonError"
|
|
|
|
| 138 |
}
|
| 139 |
|
| 140 |
/**
|
| 141 |
+
* HTTP body variant representing the absence of request content.
|
| 142 |
+
*
|
| 143 |
* @category models
|
| 144 |
+
* @since 4.0.0
|
| 145 |
*/
|
| 146 |
export class Empty extends Proto {
|
| 147 |
readonly _tag = "Empty"
|
|
|
|
| 154 |
}
|
| 155 |
|
| 156 |
/**
|
| 157 |
+
* Provides the singleton empty HTTP body.
|
| 158 |
+
*
|
| 159 |
+
* **When to use**
|
| 160 |
+
*
|
| 161 |
+
* Use when you need an HTTP body value that represents no body content.
|
| 162 |
+
*
|
| 163 |
+
* @category constants
|
| 164 |
* @since 4.0.0
|
|
|
|
| 165 |
*/
|
| 166 |
export const empty: Empty = new Empty()
|
| 167 |
|
| 168 |
/**
|
| 169 |
+
* HTTP body variant containing an arbitrary runtime body value with optional content metadata.
|
| 170 |
+
*
|
| 171 |
* @category models
|
| 172 |
+
* @since 4.0.0
|
| 173 |
*/
|
| 174 |
export class Raw extends Proto {
|
| 175 |
readonly _tag = "Raw"
|
|
|
|
| 199 |
}
|
| 200 |
|
| 201 |
/**
|
| 202 |
+
* Creates a raw HTTP body from an arbitrary value and optional `contentType` and `contentLength` metadata.
|
| 203 |
+
*
|
| 204 |
* @category constructors
|
| 205 |
+
* @since 4.0.0
|
| 206 |
*/
|
| 207 |
export const raw = (
|
| 208 |
body: unknown,
|
|
|
|
| 213 |
): Raw => new Raw(body, options?.contentType, options?.contentLength)
|
| 214 |
|
| 215 |
/**
|
| 216 |
+
* HTTP body variant backed by a `Uint8Array`.
|
| 217 |
+
*
|
| 218 |
+
* **Details**
|
| 219 |
+
*
|
| 220 |
+
* It stores the bytes, content type, and byte length.
|
| 221 |
+
*
|
| 222 |
* @category models
|
| 223 |
+
* @since 4.0.0
|
| 224 |
*/
|
| 225 |
export class Uint8Array extends Proto {
|
| 226 |
readonly _tag = "Uint8Array"
|
|
|
|
| 251 |
}
|
| 252 |
|
| 253 |
/**
|
| 254 |
+
* Creates a byte-array HTTP body.
|
| 255 |
+
*
|
| 256 |
+
* **Details**
|
| 257 |
+
*
|
| 258 |
+
* The content type defaults to `application/octet-stream`, and the content length is the byte array length.
|
| 259 |
+
*
|
| 260 |
* @category constructors
|
| 261 |
+
* @since 4.0.0
|
| 262 |
*/
|
| 263 |
export const uint8Array = (body: globalThis.Uint8Array, contentType?: string): Uint8Array =>
|
| 264 |
new Uint8Array(body, contentType ?? "application/octet-stream", body.length)
|
|
|
|
| 266 |
const encoder = new TextEncoder()
|
| 267 |
|
| 268 |
/**
|
| 269 |
+
* Creates a UTF-8 encoded text HTTP body.
|
| 270 |
+
*
|
| 271 |
+
* **Details**
|
| 272 |
+
*
|
| 273 |
+
* The content type defaults to `text/plain`.
|
| 274 |
+
*
|
| 275 |
* @category constructors
|
| 276 |
+
* @since 4.0.0
|
| 277 |
*/
|
| 278 |
export const text = (body: string, contentType?: string): Uint8Array =>
|
| 279 |
uint8Array(encoder.encode(body), contentType ?? "text/plain")
|
| 280 |
|
| 281 |
/**
|
| 282 |
+
* Creates a JSON HTTP body using `JSON.stringify`, throwing if serialization fails.
|
| 283 |
+
*
|
| 284 |
+
* **Details**
|
| 285 |
+
*
|
| 286 |
+
* The content type defaults to `application/json`.
|
| 287 |
+
*
|
| 288 |
* @category constructors
|
| 289 |
+
* @since 4.0.0
|
| 290 |
*/
|
| 291 |
export const jsonUnsafe = (body: unknown, contentType?: string): Uint8Array =>
|
| 292 |
text(JSON.stringify(body), contentType ?? "application/json")
|
| 293 |
|
| 294 |
/**
|
| 295 |
+
* Creates a JSON HTTP body in an `Effect`.
|
| 296 |
+
*
|
| 297 |
+
* **Details**
|
| 298 |
+
*
|
| 299 |
+
* `JSON.stringify` failures are captured as `HttpBodyError` values, and the content type defaults to `application/json`.
|
| 300 |
+
*
|
| 301 |
* @category constructors
|
| 302 |
+
* @since 4.0.0
|
| 303 |
*/
|
| 304 |
export const json = (body: unknown, contentType?: string): Effect.Effect<Uint8Array, HttpBodyError> =>
|
| 305 |
Effect.try({
|
|
|
|
| 308 |
})
|
| 309 |
|
| 310 |
/**
|
| 311 |
+
* Creates a JSON body constructor that first encodes values with the schema's JSON codec.
|
| 312 |
+
*
|
| 313 |
+
* **Details**
|
| 314 |
+
*
|
| 315 |
+
* Schema encoding issues and JSON serialization failures are returned as `HttpBodyError` values.
|
| 316 |
+
*
|
| 317 |
* @category constructors
|
| 318 |
+
* @since 4.0.0
|
| 319 |
*/
|
| 320 |
export const jsonSchema = <S extends Schema.Top>(
|
| 321 |
schema: S,
|
| 322 |
options?: ParseOptions | undefined
|
| 323 |
) => {
|
| 324 |
+
const encode = SchemaParser.encodeUnknownEffect(Schema.toCodecJson(schema))
|
| 325 |
return (body: S["Type"], contentType?: string): Effect.Effect<Uint8Array, HttpBodyError, S["EncodingServices"]> =>
|
| 326 |
encode(body, options).pipe(
|
| 327 |
Effect.mapError((issue) => new HttpBodyError({ reason: { _tag: "SchemaError", issue }, cause: issue })),
|
|
|
|
| 330 |
}
|
| 331 |
|
| 332 |
/**
|
| 333 |
+
* Creates an `application/x-www-form-urlencoded` HTTP body from `UrlParams`.
|
| 334 |
+
*
|
| 335 |
* @category constructors
|
| 336 |
+
* @since 4.0.0
|
| 337 |
*/
|
| 338 |
export const urlParams = (urlParams: UrlParams.UrlParams, contentType?: string): Uint8Array =>
|
| 339 |
text(UrlParams.toString(urlParams), contentType ?? "application/x-www-form-urlencoded")
|
| 340 |
|
| 341 |
/**
|
| 342 |
+
* HTTP body variant backed by Web `FormData`.
|
| 343 |
+
*
|
| 344 |
+
* **Details**
|
| 345 |
+
*
|
| 346 |
+
* The content type and content length are left unset so the runtime can supply multipart boundaries.
|
| 347 |
+
*
|
| 348 |
* @category models
|
| 349 |
+
* @since 4.0.0
|
| 350 |
*/
|
| 351 |
export class FormData extends Proto {
|
| 352 |
readonly _tag = "FormData"
|
|
|
|
| 370 |
}
|
| 371 |
|
| 372 |
/**
|
| 373 |
+
* Wraps a Web `FormData` value as an HTTP body.
|
| 374 |
+
*
|
| 375 |
* @category constructors
|
| 376 |
+
* @since 4.0.0
|
| 377 |
*/
|
| 378 |
export const formData = (body: globalThis.FormData): FormData => new FormData(body)
|
| 379 |
|
| 380 |
/**
|
| 381 |
+
* Record input accepted by `formDataRecord`.
|
| 382 |
+
*
|
| 383 |
+
* **Details**
|
| 384 |
+
*
|
| 385 |
+
* Each field may be a single coercible value or an array of coercible values.
|
| 386 |
+
*
|
| 387 |
* @category models
|
| 388 |
+
* @since 4.0.0
|
| 389 |
*/
|
| 390 |
export type FormDataInput = Record<string, FormDataCoercible | ReadonlyArray<FormDataCoercible>>
|
| 391 |
|
| 392 |
/**
|
| 393 |
+
* Value that can be appended by `formDataRecord`.
|
| 394 |
+
*
|
| 395 |
+
* **Details**
|
| 396 |
+
*
|
| 397 |
+
* `File` and `Blob` values are appended directly, primitive values are converted to strings, and `null` or `undefined` values are skipped.
|
| 398 |
+
*
|
| 399 |
* @category models
|
| 400 |
+
* @since 4.0.0
|
| 401 |
*/
|
| 402 |
export type FormDataCoercible = string | number | boolean | globalThis.File | globalThis.Blob | null | undefined
|
| 403 |
|
|
|
|
| 413 |
}
|
| 414 |
|
| 415 |
/**
|
| 416 |
+
* Creates a `FormData` HTTP body from a record.
|
| 417 |
+
*
|
| 418 |
+
* **Details**
|
| 419 |
+
*
|
| 420 |
+
* Array fields append each item under the same key; primitive values are stringified, `File` and `Blob` values are appended directly, and nullish values are skipped.
|
| 421 |
+
*
|
| 422 |
* @category constructors
|
| 423 |
+
* @since 4.0.0
|
| 424 |
*/
|
| 425 |
export const formDataRecord = (entries: FormDataInput): FormData => {
|
| 426 |
const data = new globalThis.FormData()
|
|
|
|
| 437 |
}
|
| 438 |
|
| 439 |
/**
|
| 440 |
+
* HTTP body variant backed by a stream of `Uint8Array` chunks.
|
| 441 |
+
*
|
| 442 |
* @category models
|
| 443 |
+
* @since 4.0.0
|
| 444 |
*/
|
| 445 |
export class Stream extends Proto {
|
| 446 |
readonly _tag = "Stream"
|
|
|
|
| 469 |
}
|
| 470 |
|
| 471 |
/**
|
| 472 |
+
* Creates a streaming HTTP body from a stream of byte chunks.
|
| 473 |
+
*
|
| 474 |
+
* **Details**
|
| 475 |
+
*
|
| 476 |
+
* The content type defaults to `application/octet-stream`; content length is optional.
|
| 477 |
+
*
|
| 478 |
* @category constructors
|
| 479 |
+
* @since 4.0.0
|
| 480 |
*/
|
| 481 |
export const stream = (
|
| 482 |
body: Stream_.Stream<globalThis.Uint8Array, unknown>,
|
|
|
|
| 485 |
): Stream => new Stream(body, contentType ?? "application/octet-stream", contentLength)
|
| 486 |
|
| 487 |
/**
|
| 488 |
+
* Creates a streaming HTTP body for a file path.
|
| 489 |
+
*
|
| 490 |
+
* **Details**
|
| 491 |
+
*
|
| 492 |
+
* The effect requires `FileSystem`, stats the file to set the content length, and can fail with `PlatformError`.
|
| 493 |
+
*
|
| 494 |
* @category constructors
|
| 495 |
+
* @since 4.0.0
|
| 496 |
*/
|
| 497 |
export const file = (
|
| 498 |
path: string,
|
|
|
|
| 504 |
}
|
| 505 |
): Effect.Effect<Stream, PlatformError.PlatformError, FileSystem.FileSystem> =>
|
| 506 |
Effect.flatMap(
|
| 507 |
+
FileSystem.FileSystem,
|
| 508 |
(fs) =>
|
| 509 |
Effect.map(fs.stat(path), (info) =>
|
| 510 |
stream(
|
|
|
|
| 515 |
)
|
| 516 |
|
| 517 |
/**
|
| 518 |
+
* Creates a streaming HTTP body for a file path using already-known file information.
|
| 519 |
+
*
|
| 520 |
+
* **Details**
|
| 521 |
+
*
|
| 522 |
+
* The effect requires `FileSystem`, uses the provided file size as the content length, and can fail with `PlatformError`.
|
| 523 |
+
*
|
| 524 |
* @category constructors
|
| 525 |
+
* @since 4.0.0
|
| 526 |
*/
|
| 527 |
export const fileFromInfo = (
|
| 528 |
path: string,
|
|
|
|
| 535 |
}
|
| 536 |
): Effect.Effect<Stream, PlatformError.PlatformError, FileSystem.FileSystem> =>
|
| 537 |
Effect.map(
|
| 538 |
+
FileSystem.FileSystem,
|
| 539 |
(fs) =>
|
| 540 |
stream(
|
| 541 |
fs.stream(path, options),
|
.opencode/node_modules/effect/src/unstable/http/HttpClient.ts
CHANGED
|
@@ -1,4 +1,14 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import type { NonEmptyReadonlyArray } from "../../Array.ts"
|
|
@@ -35,24 +45,36 @@ import * as UrlParams from "./UrlParams.ts"
|
|
| 35 |
const TypeId = "~effect/http/HttpClient"
|
| 36 |
|
| 37 |
/**
|
|
|
|
|
|
|
|
|
|
| 38 |
* @since 4.0.0
|
| 39 |
-
* @category Guards
|
| 40 |
*/
|
| 41 |
export const isHttpClient = (u: unknown): u is HttpClient => Predicate.hasProperty(u, TypeId)
|
| 42 |
|
| 43 |
/**
|
| 44 |
-
*
|
|
|
|
| 45 |
* @category models
|
|
|
|
| 46 |
*/
|
| 47 |
export interface HttpClient extends HttpClient.With<Error.HttpClientError> {}
|
| 48 |
|
| 49 |
/**
|
|
|
|
|
|
|
| 50 |
* @since 4.0.0
|
| 51 |
*/
|
| 52 |
export declare namespace HttpClient {
|
| 53 |
/**
|
| 54 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
* @category models
|
|
|
|
| 56 |
*/
|
| 57 |
export interface With<E, R = never> extends Pipeable, Inspectable.Inspectable {
|
| 58 |
readonly [TypeId]: typeof TypeId
|
|
@@ -93,16 +115,20 @@ export declare namespace HttpClient {
|
|
| 93 |
}
|
| 94 |
|
| 95 |
/**
|
| 96 |
-
*
|
|
|
|
| 97 |
* @category models
|
|
|
|
| 98 |
*/
|
| 99 |
export type Preprocess<E, R> = (
|
| 100 |
request: HttpClientRequest.HttpClientRequest
|
| 101 |
) => Effect.Effect<HttpClientRequest.HttpClientRequest, E, R>
|
| 102 |
|
| 103 |
/**
|
| 104 |
-
*
|
|
|
|
| 105 |
* @category models
|
|
|
|
| 106 |
*/
|
| 107 |
export type Postprocess<E = never, R = never> = (
|
| 108 |
request: Effect.Effect<HttpClientRequest.HttpClientRequest, E, R>
|
|
@@ -110,8 +136,15 @@ export declare namespace HttpClient {
|
|
| 110 |
}
|
| 111 |
|
| 112 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
* @since 4.0.0
|
| 114 |
-
* @category tags
|
| 115 |
*/
|
| 116 |
export const HttpClient: Context.Service<HttpClient, HttpClient> = Context.Service<HttpClient, HttpClient>(
|
| 117 |
"effect/HttpClient"
|
|
@@ -119,21 +152,25 @@ export const HttpClient: Context.Service<HttpClient, HttpClient> = Context.Servi
|
|
| 119 |
|
| 120 |
const accessor = (method: keyof HttpClient) => (...args: Array<any>): Effect.Effect<any, any, any> =>
|
| 121 |
Effect.flatMap(
|
| 122 |
-
HttpClient
|
| 123 |
(client) => (client as any)[method](...args)
|
| 124 |
)
|
| 125 |
|
| 126 |
/**
|
| 127 |
-
*
|
|
|
|
| 128 |
* @category accessors
|
|
|
|
| 129 |
*/
|
| 130 |
export const execute: (
|
| 131 |
request: HttpClientRequest.HttpClientRequest
|
| 132 |
) => Effect.Effect<HttpClientResponse.HttpClientResponse, Error.HttpClientError, HttpClient> = accessor("execute")
|
| 133 |
|
| 134 |
/**
|
| 135 |
-
*
|
|
|
|
| 136 |
* @category accessors
|
|
|
|
| 137 |
*/
|
| 138 |
export const get: (url: string | URL, options?: HttpClientRequest.Options.NoUrl | undefined) => Effect.Effect<
|
| 139 |
HttpClientResponse.HttpClientResponse,
|
|
@@ -142,8 +179,10 @@ export const get: (url: string | URL, options?: HttpClientRequest.Options.NoUrl
|
|
| 142 |
> = accessor("get")
|
| 143 |
|
| 144 |
/**
|
| 145 |
-
*
|
|
|
|
| 146 |
* @category accessors
|
|
|
|
| 147 |
*/
|
| 148 |
export const head: (url: string | URL, options?: HttpClientRequest.Options.NoUrl | undefined) => Effect.Effect<
|
| 149 |
HttpClientResponse.HttpClientResponse,
|
|
@@ -152,8 +191,10 @@ export const head: (url: string | URL, options?: HttpClientRequest.Options.NoUrl
|
|
| 152 |
> = accessor("head")
|
| 153 |
|
| 154 |
/**
|
| 155 |
-
*
|
|
|
|
| 156 |
* @category accessors
|
|
|
|
| 157 |
*/
|
| 158 |
export const post: (url: string | URL, options?: HttpClientRequest.Options.NoUrl | undefined) => Effect.Effect<
|
| 159 |
HttpClientResponse.HttpClientResponse,
|
|
@@ -162,8 +203,10 @@ export const post: (url: string | URL, options?: HttpClientRequest.Options.NoUrl
|
|
| 162 |
> = accessor("post")
|
| 163 |
|
| 164 |
/**
|
| 165 |
-
*
|
|
|
|
| 166 |
* @category accessors
|
|
|
|
| 167 |
*/
|
| 168 |
export const patch: (url: string | URL, options?: HttpClientRequest.Options.NoUrl | undefined) => Effect.Effect<
|
| 169 |
HttpClientResponse.HttpClientResponse,
|
|
@@ -172,8 +215,10 @@ export const patch: (url: string | URL, options?: HttpClientRequest.Options.NoUr
|
|
| 172 |
> = accessor("patch")
|
| 173 |
|
| 174 |
/**
|
| 175 |
-
*
|
|
|
|
| 176 |
* @category accessors
|
|
|
|
| 177 |
*/
|
| 178 |
export const put: (url: string | URL, options?: HttpClientRequest.Options.NoUrl | undefined) => Effect.Effect<
|
| 179 |
HttpClientResponse.HttpClientResponse,
|
|
@@ -182,8 +227,10 @@ export const put: (url: string | URL, options?: HttpClientRequest.Options.NoUrl
|
|
| 182 |
> = accessor("put")
|
| 183 |
|
| 184 |
/**
|
| 185 |
-
*
|
|
|
|
| 186 |
* @category accessors
|
|
|
|
| 187 |
*/
|
| 188 |
export const del: (url: string | URL, options?: HttpClientRequest.Options.NoUrl | undefined) => Effect.Effect<
|
| 189 |
HttpClientResponse.HttpClientResponse,
|
|
@@ -192,8 +239,10 @@ export const del: (url: string | URL, options?: HttpClientRequest.Options.NoUrl
|
|
| 192 |
> = accessor("del")
|
| 193 |
|
| 194 |
/**
|
| 195 |
-
*
|
|
|
|
| 196 |
* @category accessors
|
|
|
|
| 197 |
*/
|
| 198 |
export const options: (url: string | URL, options?: HttpClientRequest.Options.NoUrl | undefined) => Effect.Effect<
|
| 199 |
HttpClientResponse.HttpClientResponse,
|
|
@@ -202,13 +251,25 @@ export const options: (url: string | URL, options?: HttpClientRequest.Options.No
|
|
| 202 |
> = accessor("options")
|
| 203 |
|
| 204 |
/**
|
| 205 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
* @category mapping & sequencing
|
|
|
|
| 207 |
*/
|
| 208 |
export const transform: {
|
| 209 |
/**
|
| 210 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
* @category mapping & sequencing
|
|
|
|
| 212 |
*/
|
| 213 |
<E, R, E1, R1>(
|
| 214 |
f: (
|
|
@@ -217,8 +278,14 @@ export const transform: {
|
|
| 217 |
) => Effect.Effect<HttpClientResponse.HttpClientResponse, E1, R1>
|
| 218 |
): (self: HttpClient.With<E, R>) => HttpClient.With<E | E1, R | R1>
|
| 219 |
/**
|
| 220 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
* @category mapping & sequencing
|
|
|
|
| 222 |
*/
|
| 223 |
<E, R, E1, R1>(
|
| 224 |
self: HttpClient.With<E, R>,
|
|
@@ -240,13 +307,17 @@ export const transform: {
|
|
| 240 |
))
|
| 241 |
|
| 242 |
/**
|
| 243 |
-
*
|
|
|
|
| 244 |
* @category mapping & sequencing
|
|
|
|
| 245 |
*/
|
| 246 |
export const transformResponse: {
|
| 247 |
/**
|
| 248 |
-
*
|
|
|
|
| 249 |
* @category mapping & sequencing
|
|
|
|
| 250 |
*/
|
| 251 |
<E, R, E1, R1>(
|
| 252 |
f: (
|
|
@@ -254,8 +325,10 @@ export const transformResponse: {
|
|
| 254 |
) => Effect.Effect<HttpClientResponse.HttpClientResponse, E1, R1>
|
| 255 |
): (self: HttpClient.With<E, R>) => HttpClient.With<E1, R1>
|
| 256 |
/**
|
| 257 |
-
*
|
|
|
|
| 258 |
* @category mapping & sequencing
|
|
|
|
| 259 |
*/
|
| 260 |
<E, R, E1, R1>(
|
| 261 |
self: HttpClient.With<E, R>,
|
|
@@ -288,20 +361,26 @@ const catch_: {
|
|
| 288 |
|
| 289 |
export {
|
| 290 |
/**
|
| 291 |
-
*
|
|
|
|
| 292 |
* @category error handling
|
|
|
|
| 293 |
*/
|
| 294 |
catch_ as catch
|
| 295 |
}
|
| 296 |
|
| 297 |
/**
|
| 298 |
-
*
|
|
|
|
| 299 |
* @category error handling
|
|
|
|
| 300 |
*/
|
| 301 |
export const catchTag: {
|
| 302 |
/**
|
| 303 |
-
*
|
|
|
|
| 304 |
* @category error handling
|
|
|
|
| 305 |
*/
|
| 306 |
<K extends Tags<E> | NonEmptyReadonlyArray<Tags<E>>, E, E1, R1>(
|
| 307 |
tag: K,
|
|
@@ -312,8 +391,10 @@ export const catchTag: {
|
|
| 312 |
self: HttpClient.With<E, R>
|
| 313 |
) => HttpClient.With<E1 | ExcludeTag<E, K extends NonEmptyReadonlyArray<string> ? K[number] : K>, R1 | R>
|
| 314 |
/**
|
| 315 |
-
*
|
|
|
|
| 316 |
* @category error handling
|
|
|
|
| 317 |
*/
|
| 318 |
<R, E, K extends Tags<E> | NonEmptyReadonlyArray<Tags<E>>, R1, E1>(
|
| 319 |
self: HttpClient.With<E, R>,
|
|
@@ -331,17 +412,71 @@ export const catchTag: {
|
|
| 331 |
e: ExtractTag<E, K extends NonEmptyReadonlyArray<string> ? K[number] : K>
|
| 332 |
) => Effect.Effect<HttpClientResponse.HttpClientResponse, E1, R1>
|
| 333 |
): HttpClient.With<E1 | ExcludeTag<E, K extends NonEmptyReadonlyArray<string> ? K[number] : K>, R1 | R> =>
|
| 334 |
-
transformResponse(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
)
|
| 336 |
|
| 337 |
/**
|
| 338 |
-
*
|
|
|
|
| 339 |
* @category error handling
|
|
|
|
| 340 |
*/
|
| 341 |
export const catchTags: {
|
| 342 |
/**
|
| 343 |
-
*
|
|
|
|
| 344 |
* @category error handling
|
|
|
|
| 345 |
*/
|
| 346 |
<
|
| 347 |
E,
|
|
@@ -365,8 +500,10 @@ export const catchTags: {
|
|
| 365 |
}[keyof Cases]
|
| 366 |
>
|
| 367 |
/**
|
| 368 |
-
*
|
|
|
|
| 369 |
* @category error handling
|
|
|
|
| 370 |
*/
|
| 371 |
<
|
| 372 |
E extends { _tag: string },
|
|
@@ -432,15 +569,15 @@ export const catchTags: {
|
|
| 432 |
/**
|
| 433 |
* Filters the result of a response, or runs an alternative effect if the predicate fails.
|
| 434 |
*
|
| 435 |
-
* @since 4.0.0
|
| 436 |
* @category filters
|
|
|
|
| 437 |
*/
|
| 438 |
export const filterOrElse: {
|
| 439 |
/**
|
| 440 |
* Filters the result of a response, or runs an alternative effect if the predicate fails.
|
| 441 |
*
|
| 442 |
-
* @since 4.0.0
|
| 443 |
* @category filters
|
|
|
|
| 444 |
*/
|
| 445 |
<B extends HttpClientResponse.HttpClientResponse, E2, R2>(
|
| 446 |
refinement: Predicate.Refinement<NoInfer<HttpClientResponse.HttpClientResponse>, B>,
|
|
@@ -456,8 +593,8 @@ export const filterOrElse: {
|
|
| 456 |
/**
|
| 457 |
* Filters the result of a response, or runs an alternative effect if the predicate fails.
|
| 458 |
*
|
| 459 |
-
* @since 4.0.0
|
| 460 |
* @category filters
|
|
|
|
| 461 |
*/
|
| 462 |
<E2, R2>(
|
| 463 |
predicate: Predicate.Predicate<NoInfer<HttpClientResponse.HttpClientResponse>>,
|
|
@@ -468,8 +605,8 @@ export const filterOrElse: {
|
|
| 468 |
/**
|
| 469 |
* Filters the result of a response, or runs an alternative effect if the predicate fails.
|
| 470 |
*
|
| 471 |
-
* @since 4.0.0
|
| 472 |
* @category filters
|
|
|
|
| 473 |
*/
|
| 474 |
<E, R, B extends HttpClientResponse.HttpClientResponse, E2, R2>(
|
| 475 |
self: HttpClient.With<E, R>,
|
|
@@ -486,8 +623,8 @@ export const filterOrElse: {
|
|
| 486 |
/**
|
| 487 |
* Filters the result of a response, or runs an alternative effect if the predicate fails.
|
| 488 |
*
|
| 489 |
-
* @since 4.0.0
|
| 490 |
* @category filters
|
|
|
|
| 491 |
*/
|
| 492 |
<E, R, E2, R2>(
|
| 493 |
self: HttpClient.With<E, R>,
|
|
@@ -499,37 +636,37 @@ export const filterOrElse: {
|
|
| 499 |
} = dual(3, (self, f, orElse) => transformResponse(self, Effect.filterOrElse(f, orElse)))
|
| 500 |
|
| 501 |
/**
|
| 502 |
-
* Filters
|
| 503 |
*
|
| 504 |
-
* @since 4.0.0
|
| 505 |
* @category filters
|
|
|
|
| 506 |
*/
|
| 507 |
export const filterOrFail: {
|
| 508 |
/**
|
| 509 |
-
* Filters
|
| 510 |
*
|
| 511 |
-
* @since 4.0.0
|
| 512 |
* @category filters
|
|
|
|
| 513 |
*/
|
| 514 |
<B extends HttpClientResponse.HttpClientResponse, E2>(
|
| 515 |
refinement: Predicate.Refinement<NoInfer<HttpClientResponse.HttpClientResponse>, B>,
|
| 516 |
orFailWith: (response: NoInfer<HttpClientResponse.HttpClientResponse>) => E2
|
| 517 |
): <E, R>(self: HttpClient.With<E, R>) => HttpClient.With<E2 | E, R>
|
| 518 |
/**
|
| 519 |
-
* Filters
|
| 520 |
*
|
| 521 |
-
* @since 4.0.0
|
| 522 |
* @category filters
|
|
|
|
| 523 |
*/
|
| 524 |
<E2>(
|
| 525 |
predicate: Predicate.Predicate<NoInfer<HttpClientResponse.HttpClientResponse>>,
|
| 526 |
orFailWith: (response: NoInfer<HttpClientResponse.HttpClientResponse>) => E2
|
| 527 |
): <E, R>(self: HttpClient.With<E, R>) => HttpClient.With<E2 | E, R>
|
| 528 |
/**
|
| 529 |
-
* Filters
|
| 530 |
*
|
| 531 |
-
* @since 4.0.0
|
| 532 |
* @category filters
|
|
|
|
| 533 |
*/
|
| 534 |
<E, R, B extends HttpClientResponse.HttpClientResponse, E2>(
|
| 535 |
self: HttpClient.With<E, R>,
|
|
@@ -537,10 +674,10 @@ export const filterOrFail: {
|
|
| 537 |
orFailWith: (response: NoInfer<HttpClientResponse.HttpClientResponse>) => E2
|
| 538 |
): HttpClient.With<E2 | E, R>
|
| 539 |
/**
|
| 540 |
-
* Filters
|
| 541 |
*
|
| 542 |
-
* @since 4.0.0
|
| 543 |
* @category filters
|
|
|
|
| 544 |
*/
|
| 545 |
<E, R, E2>(
|
| 546 |
self: HttpClient.With<E, R>,
|
|
@@ -552,22 +689,22 @@ export const filterOrFail: {
|
|
| 552 |
/**
|
| 553 |
* Filters responses by HTTP status code.
|
| 554 |
*
|
| 555 |
-
* @since 4.0.0
|
| 556 |
* @category filters
|
|
|
|
| 557 |
*/
|
| 558 |
export const filterStatus: {
|
| 559 |
/**
|
| 560 |
* Filters responses by HTTP status code.
|
| 561 |
*
|
| 562 |
-
* @since 4.0.0
|
| 563 |
* @category filters
|
|
|
|
| 564 |
*/
|
| 565 |
(f: (status: number) => boolean): <E, R>(self: HttpClient.With<E, R>) => HttpClient.With<E | Error.HttpClientError, R>
|
| 566 |
/**
|
| 567 |
* Filters responses by HTTP status code.
|
| 568 |
*
|
| 569 |
-
* @since 4.0.0
|
| 570 |
* @category filters
|
|
|
|
| 571 |
*/
|
| 572 |
<E, R>(self: HttpClient.With<E, R>, f: (status: number) => boolean): HttpClient.With<E | Error.HttpClientError, R>
|
| 573 |
} = dual(
|
|
@@ -579,15 +716,21 @@ export const filterStatus: {
|
|
| 579 |
/**
|
| 580 |
* Filters responses that return a 2xx status code.
|
| 581 |
*
|
| 582 |
-
* @since 4.0.0
|
| 583 |
* @category filters
|
|
|
|
| 584 |
*/
|
| 585 |
export const filterStatusOk: <E, R>(self: HttpClient.With<E, R>) => HttpClient.With<E | Error.HttpClientError, R> =
|
| 586 |
transformResponse(Effect.flatMap(HttpClientResponse.filterStatusOk))
|
| 587 |
|
| 588 |
/**
|
| 589 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 590 |
* @category constructors
|
|
|
|
| 591 |
*/
|
| 592 |
export const makeWith = <E2, R2, E, R>(
|
| 593 |
postprocess: (
|
|
@@ -625,8 +768,14 @@ const Proto = {
|
|
| 625 |
}
|
| 626 |
|
| 627 |
/**
|
| 628 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 629 |
* @category constructors
|
|
|
|
| 630 |
*/
|
| 631 |
export const make = (
|
| 632 |
f: (
|
|
@@ -728,15 +877,15 @@ export const make = (
|
|
| 728 |
/**
|
| 729 |
* Appends a transformation of the request object before sending it.
|
| 730 |
*
|
| 731 |
-
* @since 4.0.0
|
| 732 |
* @category mapping & sequencing
|
|
|
|
| 733 |
*/
|
| 734 |
export const mapRequest: {
|
| 735 |
/**
|
| 736 |
* Appends a transformation of the request object before sending it.
|
| 737 |
*
|
| 738 |
-
* @since 4.0.0
|
| 739 |
* @category mapping & sequencing
|
|
|
|
| 740 |
*/
|
| 741 |
(
|
| 742 |
f: (a: HttpClientRequest.HttpClientRequest) => HttpClientRequest.HttpClientRequest
|
|
@@ -744,8 +893,8 @@ export const mapRequest: {
|
|
| 744 |
/**
|
| 745 |
* Appends a transformation of the request object before sending it.
|
| 746 |
*
|
| 747 |
-
* @since 4.0.0
|
| 748 |
* @category mapping & sequencing
|
|
|
|
| 749 |
*/
|
| 750 |
<E, R>(
|
| 751 |
self: HttpClient.With<E, R>,
|
|
@@ -762,15 +911,15 @@ export const mapRequest: {
|
|
| 762 |
/**
|
| 763 |
* Appends an effectful transformation of the request object before sending it.
|
| 764 |
*
|
| 765 |
-
* @since 4.0.0
|
| 766 |
* @category mapping & sequencing
|
|
|
|
| 767 |
*/
|
| 768 |
export const mapRequestEffect: {
|
| 769 |
/**
|
| 770 |
* Appends an effectful transformation of the request object before sending it.
|
| 771 |
*
|
| 772 |
-
* @since 4.0.0
|
| 773 |
* @category mapping & sequencing
|
|
|
|
| 774 |
*/
|
| 775 |
<E2, R2>(
|
| 776 |
f: (a: HttpClientRequest.HttpClientRequest) => Effect.Effect<HttpClientRequest.HttpClientRequest, E2, R2>
|
|
@@ -778,8 +927,8 @@ export const mapRequestEffect: {
|
|
| 778 |
/**
|
| 779 |
* Appends an effectful transformation of the request object before sending it.
|
| 780 |
*
|
| 781 |
-
* @since 4.0.0
|
| 782 |
* @category mapping & sequencing
|
|
|
|
| 783 |
*/
|
| 784 |
<E, R, E2, R2>(
|
| 785 |
self: HttpClient.With<E, R>,
|
|
@@ -797,15 +946,15 @@ export const mapRequestEffect: {
|
|
| 797 |
/**
|
| 798 |
* Prepends a transformation of the request object before sending it.
|
| 799 |
*
|
| 800 |
-
* @since 4.0.0
|
| 801 |
* @category mapping & sequencing
|
|
|
|
| 802 |
*/
|
| 803 |
export const mapRequestInput: {
|
| 804 |
/**
|
| 805 |
* Prepends a transformation of the request object before sending it.
|
| 806 |
*
|
| 807 |
-
* @since 4.0.0
|
| 808 |
* @category mapping & sequencing
|
|
|
|
| 809 |
*/
|
| 810 |
(
|
| 811 |
f: (a: HttpClientRequest.HttpClientRequest) => HttpClientRequest.HttpClientRequest
|
|
@@ -813,8 +962,8 @@ export const mapRequestInput: {
|
|
| 813 |
/**
|
| 814 |
* Prepends a transformation of the request object before sending it.
|
| 815 |
*
|
| 816 |
-
* @since 4.0.0
|
| 817 |
* @category mapping & sequencing
|
|
|
|
| 818 |
*/
|
| 819 |
<E, R>(
|
| 820 |
self: HttpClient.With<E, R>,
|
|
@@ -831,15 +980,15 @@ export const mapRequestInput: {
|
|
| 831 |
/**
|
| 832 |
* Prepends an effectful transformation of the request object before sending it.
|
| 833 |
*
|
| 834 |
-
* @since 4.0.0
|
| 835 |
* @category mapping & sequencing
|
|
|
|
| 836 |
*/
|
| 837 |
export const mapRequestInputEffect: {
|
| 838 |
/**
|
| 839 |
* Prepends an effectful transformation of the request object before sending it.
|
| 840 |
*
|
| 841 |
-
* @since 4.0.0
|
| 842 |
* @category mapping & sequencing
|
|
|
|
| 843 |
*/
|
| 844 |
<E2, R2>(
|
| 845 |
f: (a: HttpClientRequest.HttpClientRequest) => Effect.Effect<HttpClientRequest.HttpClientRequest, E2, R2>
|
|
@@ -847,8 +996,8 @@ export const mapRequestInputEffect: {
|
|
| 847 |
/**
|
| 848 |
* Prepends an effectful transformation of the request object before sending it.
|
| 849 |
*
|
| 850 |
-
* @since 4.0.0
|
| 851 |
* @category mapping & sequencing
|
|
|
|
| 852 |
*/
|
| 853 |
<E, R, E2, R2>(
|
| 854 |
self: HttpClient.With<E, R>,
|
|
@@ -864,13 +1013,20 @@ export const mapRequestInputEffect: {
|
|
| 864 |
)
|
| 865 |
|
| 866 |
/**
|
|
|
|
|
|
|
| 867 |
* @since 4.0.0
|
| 868 |
-
* @category error handling
|
| 869 |
*/
|
| 870 |
export declare namespace Retry {
|
| 871 |
/**
|
| 872 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 873 |
* @category error handling
|
|
|
|
| 874 |
*/
|
| 875 |
export type Return<R, E, O extends NoExcessProperties<Effect.Retry.Options<E>, O>> = HttpClient.With<
|
| 876 |
| (O extends { schedule: Schedule.Schedule<infer _O, infer _I, infer _E, infer _R> } ? E | _E
|
|
@@ -889,36 +1045,36 @@ export declare namespace Retry {
|
|
| 889 |
/**
|
| 890 |
* Retries the request based on a provided schedule or policy.
|
| 891 |
*
|
| 892 |
-
* @since 4.0.0
|
| 893 |
* @category error handling
|
|
|
|
| 894 |
*/
|
| 895 |
export const retry: {
|
| 896 |
/**
|
| 897 |
* Retries the request based on a provided schedule or policy.
|
| 898 |
*
|
| 899 |
-
* @since 4.0.0
|
| 900 |
* @category error handling
|
|
|
|
| 901 |
*/
|
| 902 |
<E, O extends NoExcessProperties<Effect.Retry.Options<E>, O>>(options: O): <R>(self: HttpClient.With<E, R>) => Retry.Return<R, E, O>
|
| 903 |
/**
|
| 904 |
* Retries the request based on a provided schedule or policy.
|
| 905 |
*
|
| 906 |
-
* @since 4.0.0
|
| 907 |
* @category error handling
|
|
|
|
| 908 |
*/
|
| 909 |
<B, E, ES, R1>(policy: Schedule.Schedule<B, NoInfer<E>, ES, R1>): <R>(self: HttpClient.With<E, R>) => HttpClient.With<E | ES, R1 | R>
|
| 910 |
/**
|
| 911 |
* Retries the request based on a provided schedule or policy.
|
| 912 |
*
|
| 913 |
-
* @since 4.0.0
|
| 914 |
* @category error handling
|
|
|
|
| 915 |
*/
|
| 916 |
<E, R, O extends NoExcessProperties<Effect.Retry.Options<E>, O>>(self: HttpClient.With<E, R>, options: O): Retry.Return<R, E, O>
|
| 917 |
/**
|
| 918 |
* Retries the request based on a provided schedule or policy.
|
| 919 |
*
|
| 920 |
-
* @since 4.0.0
|
| 921 |
* @category error handling
|
|
|
|
| 922 |
*/
|
| 923 |
<E, R, B, ES, R1>(self: HttpClient.With<E, R>, policy: Schedule.Schedule<B, E, ES, R1>): HttpClient.With<E | ES, R1 | R>
|
| 924 |
} = dual(
|
|
@@ -932,25 +1088,33 @@ export const retry: {
|
|
| 932 |
/**
|
| 933 |
* Retries common transient errors, such as rate limiting, timeouts or network issues.
|
| 934 |
*
|
| 935 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 936 |
*
|
| 937 |
* Specifying a `while` predicate allows you to consider other errors as
|
| 938 |
* transient, and is ignored in "response-only" mode.
|
| 939 |
*
|
| 940 |
-
* @since 4.0.0
|
| 941 |
* @category error handling
|
|
|
|
| 942 |
*/
|
| 943 |
export const retryTransient: {
|
| 944 |
/**
|
| 945 |
* Retries common transient errors, such as rate limiting, timeouts or network issues.
|
| 946 |
*
|
| 947 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 948 |
*
|
| 949 |
* Specifying a `while` predicate allows you to consider other errors as
|
| 950 |
* transient, and is ignored in "response-only" mode.
|
| 951 |
*
|
| 952 |
-
* @since 4.0.0
|
| 953 |
* @category error handling
|
|
|
|
| 954 |
*/
|
| 955 |
<
|
| 956 |
E,
|
|
@@ -975,13 +1139,17 @@ export const retryTransient: {
|
|
| 975 |
/**
|
| 976 |
* Retries common transient errors, such as rate limiting, timeouts or network issues.
|
| 977 |
*
|
| 978 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 979 |
*
|
| 980 |
* Specifying a `while` predicate allows you to consider other errors as
|
| 981 |
* transient, and is ignored in "response-only" mode.
|
| 982 |
*
|
| 983 |
-
* @since 4.0.0
|
| 984 |
* @category error handling
|
|
|
|
| 985 |
*/
|
| 986 |
<
|
| 987 |
E,
|
|
@@ -1008,13 +1176,17 @@ export const retryTransient: {
|
|
| 1008 |
/**
|
| 1009 |
* Retries common transient errors, such as rate limiting, timeouts or network issues.
|
| 1010 |
*
|
| 1011 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1012 |
*
|
| 1013 |
* Specifying a `while` predicate allows you to consider other errors as
|
| 1014 |
* transient, and is ignored in "response-only" mode.
|
| 1015 |
*
|
| 1016 |
-
* @since 4.0.0
|
| 1017 |
* @category error handling
|
|
|
|
| 1018 |
*/
|
| 1019 |
<B, E, ES = never, R1 = never>(
|
| 1020 |
options: Schedule.Schedule<B, NoInfer<HttpClientResponse.HttpClientResponse | E>, ES, R1>
|
|
@@ -1022,13 +1194,17 @@ export const retryTransient: {
|
|
| 1022 |
/**
|
| 1023 |
* Retries common transient errors, such as rate limiting, timeouts or network issues.
|
| 1024 |
*
|
| 1025 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1026 |
*
|
| 1027 |
* Specifying a `while` predicate allows you to consider other errors as
|
| 1028 |
* transient, and is ignored in "response-only" mode.
|
| 1029 |
*
|
| 1030 |
-
* @since 4.0.0
|
| 1031 |
* @category error handling
|
|
|
|
| 1032 |
*/
|
| 1033 |
<E, R, B, ES = never, R1 = never>(
|
| 1034 |
self: HttpClient.With<E, R>,
|
|
@@ -1077,13 +1253,20 @@ export const retryTransient: {
|
|
| 1077 |
)
|
| 1078 |
|
| 1079 |
/**
|
|
|
|
|
|
|
| 1080 |
* @since 4.0.0
|
| 1081 |
-
* @category rate limiting
|
| 1082 |
*/
|
| 1083 |
export declare namespace WithRateLimiter {
|
| 1084 |
/**
|
| 1085 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1086 |
* @category rate limiting
|
|
|
|
| 1087 |
*/
|
| 1088 |
export interface Options {
|
| 1089 |
/**
|
|
@@ -1122,23 +1305,27 @@ export declare namespace WithRateLimiter {
|
|
| 1122 |
/**
|
| 1123 |
* Applies request rate limiting using the `RateLimiter` service.
|
| 1124 |
*
|
|
|
|
|
|
|
| 1125 |
* It can update limits by inspecting common rate limit response headers and
|
| 1126 |
* automatically retries HTTP `429` responses (or `HttpClientError` values
|
| 1127 |
* wrapping a `429` response) by forcing the retry back through the limiter.
|
| 1128 |
*
|
| 1129 |
-
* @since 4.0.0
|
| 1130 |
* @category rate limiting
|
|
|
|
| 1131 |
*/
|
| 1132 |
export const withRateLimiter: {
|
| 1133 |
/**
|
| 1134 |
* Applies request rate limiting using the `RateLimiter` service.
|
| 1135 |
*
|
|
|
|
|
|
|
| 1136 |
* It can update limits by inspecting common rate limit response headers and
|
| 1137 |
* automatically retries HTTP `429` responses (or `HttpClientError` values
|
| 1138 |
* wrapping a `429` response) by forcing the retry back through the limiter.
|
| 1139 |
*
|
| 1140 |
-
* @since 4.0.0
|
| 1141 |
* @category rate limiting
|
|
|
|
| 1142 |
*/
|
| 1143 |
(options: WithRateLimiter.Options): <E, R>(
|
| 1144 |
self: HttpClient.With<E, R>
|
|
@@ -1146,12 +1333,14 @@ export const withRateLimiter: {
|
|
| 1146 |
/**
|
| 1147 |
* Applies request rate limiting using the `RateLimiter` service.
|
| 1148 |
*
|
|
|
|
|
|
|
| 1149 |
* It can update limits by inspecting common rate limit response headers and
|
| 1150 |
* automatically retries HTTP `429` responses (or `HttpClientError` values
|
| 1151 |
* wrapping a `429` response) by forcing the retry back through the limiter.
|
| 1152 |
*
|
| 1153 |
-
* @since 4.0.0
|
| 1154 |
* @category rate limiting
|
|
|
|
| 1155 |
*/
|
| 1156 |
<E, R>(self: HttpClient.With<E, R>, options: WithRateLimiter.Options): HttpClient.With<E | RateLimiter.RateLimiterError, R>
|
| 1157 |
} = dual(2, <E, R>(
|
|
@@ -1374,15 +1563,15 @@ const getHeader = (headers: Headers.Headers, ...keys: Array<string>): string | u
|
|
| 1374 |
/**
|
| 1375 |
* Performs an additional effect after a successful request.
|
| 1376 |
*
|
| 1377 |
-
* @since 4.0.0
|
| 1378 |
* @category mapping & sequencing
|
|
|
|
| 1379 |
*/
|
| 1380 |
export const tap: {
|
| 1381 |
/**
|
| 1382 |
* Performs an additional effect after a successful request.
|
| 1383 |
*
|
| 1384 |
-
* @since 4.0.0
|
| 1385 |
* @category mapping & sequencing
|
|
|
|
| 1386 |
*/
|
| 1387 |
<_, E2, R2>(
|
| 1388 |
f: (response: HttpClientResponse.HttpClientResponse) => Effect.Effect<_, E2, R2>
|
|
@@ -1390,8 +1579,8 @@ export const tap: {
|
|
| 1390 |
/**
|
| 1391 |
* Performs an additional effect after a successful request.
|
| 1392 |
*
|
| 1393 |
-
* @since 4.0.0
|
| 1394 |
* @category mapping & sequencing
|
|
|
|
| 1395 |
*/
|
| 1396 |
<E, R, _, E2, R2>(
|
| 1397 |
self: HttpClient.With<E, R>,
|
|
@@ -1408,22 +1597,22 @@ export const tap: {
|
|
| 1408 |
/**
|
| 1409 |
* Performs an additional effect after an unsuccessful request.
|
| 1410 |
*
|
| 1411 |
-
* @since 4.0.0
|
| 1412 |
* @category mapping & sequencing
|
|
|
|
| 1413 |
*/
|
| 1414 |
export const tapError: {
|
| 1415 |
/**
|
| 1416 |
* Performs an additional effect after an unsuccessful request.
|
| 1417 |
*
|
| 1418 |
-
* @since 4.0.0
|
| 1419 |
* @category mapping & sequencing
|
|
|
|
| 1420 |
*/
|
| 1421 |
<_, E, E2, R2>(f: (e: NoInfer<E>) => Effect.Effect<_, E2, R2>): <R>(self: HttpClient.With<E, R>) => HttpClient.With<E | E2, R | R2>
|
| 1422 |
/**
|
| 1423 |
* Performs an additional effect after an unsuccessful request.
|
| 1424 |
*
|
| 1425 |
-
* @since 4.0.0
|
| 1426 |
* @category mapping & sequencing
|
|
|
|
| 1427 |
*/
|
| 1428 |
<E, R, _, E2, R2>(
|
| 1429 |
self: HttpClient.With<E, R>,
|
|
@@ -1440,22 +1629,22 @@ export const tapError: {
|
|
| 1440 |
/**
|
| 1441 |
* Performs an additional effect on the request before sending it.
|
| 1442 |
*
|
| 1443 |
-
* @since 4.0.0
|
| 1444 |
* @category mapping & sequencing
|
|
|
|
| 1445 |
*/
|
| 1446 |
export const tapRequest: {
|
| 1447 |
/**
|
| 1448 |
* Performs an additional effect on the request before sending it.
|
| 1449 |
*
|
| 1450 |
-
* @since 4.0.0
|
| 1451 |
* @category mapping & sequencing
|
|
|
|
| 1452 |
*/
|
| 1453 |
<_, E2, R2>(f: (a: HttpClientRequest.HttpClientRequest) => Effect.Effect<_, E2, R2>): <E, R>(self: HttpClient.With<E, R>) => HttpClient.With<E | E2, R | R2>
|
| 1454 |
/**
|
| 1455 |
* Performs an additional effect on the request before sending it.
|
| 1456 |
*
|
| 1457 |
-
* @since 4.0.0
|
| 1458 |
* @category mapping & sequencing
|
|
|
|
| 1459 |
*/
|
| 1460 |
<E, R, _, E2, R2>(
|
| 1461 |
self: HttpClient.With<E, R>,
|
|
@@ -1471,24 +1660,39 @@ export const tapRequest: {
|
|
| 1471 |
)
|
| 1472 |
|
| 1473 |
/**
|
| 1474 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1475 |
*
|
| 1476 |
-
* @since 4.0.0
|
| 1477 |
* @category cookies
|
|
|
|
| 1478 |
*/
|
| 1479 |
export const withCookiesRef: {
|
| 1480 |
/**
|
| 1481 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1482 |
*
|
| 1483 |
-
* @since 4.0.0
|
| 1484 |
* @category cookies
|
|
|
|
| 1485 |
*/
|
| 1486 |
(ref: Ref.Ref<Cookies.Cookies>): <E, R>(self: HttpClient.With<E, R>) => HttpClient.With<E, R>
|
| 1487 |
/**
|
| 1488 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1489 |
*
|
| 1490 |
-
* @since 4.0.0
|
| 1491 |
* @category cookies
|
|
|
|
| 1492 |
*/
|
| 1493 |
<E, R>(self: HttpClient.With<E, R>, ref: Ref.Ref<Cookies.Cookies>): HttpClient.With<E, R>
|
| 1494 |
} = dual(
|
|
@@ -1516,10 +1720,10 @@ export const withCookiesRef: {
|
|
| 1516 |
)
|
| 1517 |
|
| 1518 |
/**
|
| 1519 |
-
*
|
| 1520 |
*
|
|
|
|
| 1521 |
* @since 4.0.0
|
| 1522 |
-
* @category Scope
|
| 1523 |
*/
|
| 1524 |
export const withScope = <E, R>(
|
| 1525 |
self: HttpClient.With<E, R>
|
|
@@ -1537,24 +1741,24 @@ export const withScope = <E, R>(
|
|
| 1537 |
)
|
| 1538 |
|
| 1539 |
/**
|
| 1540 |
-
*
|
| 1541 |
*
|
| 1542 |
-
* @since 4.0.0
|
| 1543 |
* @category redirects
|
|
|
|
| 1544 |
*/
|
| 1545 |
export const followRedirects: {
|
| 1546 |
/**
|
| 1547 |
-
*
|
| 1548 |
*
|
| 1549 |
-
* @since 4.0.0
|
| 1550 |
* @category redirects
|
|
|
|
| 1551 |
*/
|
| 1552 |
(maxRedirects?: number | undefined): <E, R>(self: HttpClient.With<E, R>) => HttpClient.With<E, R>
|
| 1553 |
/**
|
| 1554 |
-
*
|
| 1555 |
*
|
| 1556 |
-
* @since 4.0.0
|
| 1557 |
* @category redirects
|
|
|
|
| 1558 |
*/
|
| 1559 |
<E, R>(self: HttpClient.With<E, R>, maxRedirects?: number | undefined): HttpClient.With<E, R>
|
| 1560 |
} = dual((args) => isHttpClient(args[0]), <E, R>(
|
|
@@ -1587,8 +1791,10 @@ export const followRedirects: {
|
|
| 1587 |
))
|
| 1588 |
|
| 1589 |
/**
|
|
|
|
|
|
|
|
|
|
| 1590 |
* @since 4.0.0
|
| 1591 |
-
* @category References
|
| 1592 |
*/
|
| 1593 |
export const TracerDisabledWhen = Context.Reference<
|
| 1594 |
Predicate.Predicate<HttpClientRequest.HttpClientRequest>
|
|
@@ -1597,16 +1803,20 @@ export const TracerDisabledWhen = Context.Reference<
|
|
| 1597 |
})
|
| 1598 |
|
| 1599 |
/**
|
|
|
|
|
|
|
|
|
|
| 1600 |
* @since 4.0.0
|
| 1601 |
-
* @category References
|
| 1602 |
*/
|
| 1603 |
export const TracerPropagationEnabled = Context.Reference<boolean>("effect/HttpClient/TracerPropagationEnabled", {
|
| 1604 |
defaultValue: constTrue
|
| 1605 |
})
|
| 1606 |
|
| 1607 |
/**
|
|
|
|
|
|
|
|
|
|
| 1608 |
* @since 4.0.0
|
| 1609 |
-
* @category References
|
| 1610 |
*/
|
| 1611 |
export const SpanNameGenerator = Context.Reference<
|
| 1612 |
(request: HttpClientRequest.HttpClientRequest) => string
|
|
@@ -1615,6 +1825,9 @@ export const SpanNameGenerator = Context.Reference<
|
|
| 1615 |
})
|
| 1616 |
|
| 1617 |
/**
|
|
|
|
|
|
|
|
|
|
| 1618 |
* @since 4.0.0
|
| 1619 |
*/
|
| 1620 |
export const layerMergedContext = <E, R>(
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Provides the service used to run outgoing HTTP requests.
|
| 3 |
+
*
|
| 4 |
+
* `HttpClient` executes immutable `HttpClientRequest` values and returns
|
| 5 |
+
* `HttpClientResponse` values. Keeping HTTP behind this service lets programs,
|
| 6 |
+
* tests, and generated API clients use the same request model without depending
|
| 7 |
+
* on one concrete platform transport. This module includes request accessors,
|
| 8 |
+
* constructors and layers, request and response transformations, status
|
| 9 |
+
* filtering, retries, rate limiting, cookies, redirect handling, scoped request
|
| 10 |
+
* abortion, and tracing support.
|
| 11 |
+
*
|
| 12 |
* @since 4.0.0
|
| 13 |
*/
|
| 14 |
import type { NonEmptyReadonlyArray } from "../../Array.ts"
|
|
|
|
| 45 |
const TypeId = "~effect/http/HttpClient"
|
| 46 |
|
| 47 |
/**
|
| 48 |
+
* Returns `true` if the provided value is an `HttpClient`.
|
| 49 |
+
*
|
| 50 |
+
* @category guards
|
| 51 |
* @since 4.0.0
|
|
|
|
| 52 |
*/
|
| 53 |
export const isHttpClient = (u: unknown): u is HttpClient => Predicate.hasProperty(u, TypeId)
|
| 54 |
|
| 55 |
/**
|
| 56 |
+
* HTTP client whose requests produce `HttpClientResponse` values and can fail with `HttpClientError`.
|
| 57 |
+
*
|
| 58 |
* @category models
|
| 59 |
+
* @since 4.0.0
|
| 60 |
*/
|
| 61 |
export interface HttpClient extends HttpClient.With<Error.HttpClientError> {}
|
| 62 |
|
| 63 |
/**
|
| 64 |
+
* Namespace containing type-level members associated with `HttpClient`.
|
| 65 |
+
*
|
| 66 |
* @since 4.0.0
|
| 67 |
*/
|
| 68 |
export declare namespace HttpClient {
|
| 69 |
/**
|
| 70 |
+
* Parameterized HTTP client that may fail with `E` and require environment `R`.
|
| 71 |
+
*
|
| 72 |
+
* **Details**
|
| 73 |
+
*
|
| 74 |
+
* It exposes preprocessing, postprocessing, direct request execution, and method-specific helpers.
|
| 75 |
+
*
|
| 76 |
* @category models
|
| 77 |
+
* @since 4.0.0
|
| 78 |
*/
|
| 79 |
export interface With<E, R = never> extends Pipeable, Inspectable.Inspectable {
|
| 80 |
readonly [TypeId]: typeof TypeId
|
|
|
|
| 115 |
}
|
| 116 |
|
| 117 |
/**
|
| 118 |
+
* Effectful transformation applied to a request before the client executes it.
|
| 119 |
+
*
|
| 120 |
* @category models
|
| 121 |
+
* @since 4.0.0
|
| 122 |
*/
|
| 123 |
export type Preprocess<E, R> = (
|
| 124 |
request: HttpClientRequest.HttpClientRequest
|
| 125 |
) => Effect.Effect<HttpClientRequest.HttpClientRequest, E, R>
|
| 126 |
|
| 127 |
/**
|
| 128 |
+
* Function that turns a preprocessed request effect into the response effect executed by the client.
|
| 129 |
+
*
|
| 130 |
* @category models
|
| 131 |
+
* @since 4.0.0
|
| 132 |
*/
|
| 133 |
export type Postprocess<E = never, R = never> = (
|
| 134 |
request: Effect.Effect<HttpClientRequest.HttpClientRequest, E, R>
|
|
|
|
| 136 |
}
|
| 137 |
|
| 138 |
/**
|
| 139 |
+
* Service tag for the default outgoing HTTP client service.
|
| 140 |
+
*
|
| 141 |
+
* **When to use**
|
| 142 |
+
*
|
| 143 |
+
* Use to provide the default outgoing HTTP client service used by request
|
| 144 |
+
* accessors such as `execute`, `get`, and `post`.
|
| 145 |
+
*
|
| 146 |
+
* @category services
|
| 147 |
* @since 4.0.0
|
|
|
|
| 148 |
*/
|
| 149 |
export const HttpClient: Context.Service<HttpClient, HttpClient> = Context.Service<HttpClient, HttpClient>(
|
| 150 |
"effect/HttpClient"
|
|
|
|
| 152 |
|
| 153 |
const accessor = (method: keyof HttpClient) => (...args: Array<any>): Effect.Effect<any, any, any> =>
|
| 154 |
Effect.flatMap(
|
| 155 |
+
HttpClient,
|
| 156 |
(client) => (client as any)[method](...args)
|
| 157 |
)
|
| 158 |
|
| 159 |
/**
|
| 160 |
+
* Executes a prebuilt `HttpClientRequest` using the `HttpClient` service from the environment.
|
| 161 |
+
*
|
| 162 |
* @category accessors
|
| 163 |
+
* @since 4.0.0
|
| 164 |
*/
|
| 165 |
export const execute: (
|
| 166 |
request: HttpClientRequest.HttpClientRequest
|
| 167 |
) => Effect.Effect<HttpClientResponse.HttpClientResponse, Error.HttpClientError, HttpClient> = accessor("execute")
|
| 168 |
|
| 169 |
/**
|
| 170 |
+
* Executes a `GET` request using the `HttpClient` service from the environment.
|
| 171 |
+
*
|
| 172 |
* @category accessors
|
| 173 |
+
* @since 4.0.0
|
| 174 |
*/
|
| 175 |
export const get: (url: string | URL, options?: HttpClientRequest.Options.NoUrl | undefined) => Effect.Effect<
|
| 176 |
HttpClientResponse.HttpClientResponse,
|
|
|
|
| 179 |
> = accessor("get")
|
| 180 |
|
| 181 |
/**
|
| 182 |
+
* Executes a `HEAD` request using the `HttpClient` service from the environment.
|
| 183 |
+
*
|
| 184 |
* @category accessors
|
| 185 |
+
* @since 4.0.0
|
| 186 |
*/
|
| 187 |
export const head: (url: string | URL, options?: HttpClientRequest.Options.NoUrl | undefined) => Effect.Effect<
|
| 188 |
HttpClientResponse.HttpClientResponse,
|
|
|
|
| 191 |
> = accessor("head")
|
| 192 |
|
| 193 |
/**
|
| 194 |
+
* Executes a `POST` request using the `HttpClient` service from the environment.
|
| 195 |
+
*
|
| 196 |
* @category accessors
|
| 197 |
+
* @since 4.0.0
|
| 198 |
*/
|
| 199 |
export const post: (url: string | URL, options?: HttpClientRequest.Options.NoUrl | undefined) => Effect.Effect<
|
| 200 |
HttpClientResponse.HttpClientResponse,
|
|
|
|
| 203 |
> = accessor("post")
|
| 204 |
|
| 205 |
/**
|
| 206 |
+
* Executes a `PATCH` request using the `HttpClient` service from the environment.
|
| 207 |
+
*
|
| 208 |
* @category accessors
|
| 209 |
+
* @since 4.0.0
|
| 210 |
*/
|
| 211 |
export const patch: (url: string | URL, options?: HttpClientRequest.Options.NoUrl | undefined) => Effect.Effect<
|
| 212 |
HttpClientResponse.HttpClientResponse,
|
|
|
|
| 215 |
> = accessor("patch")
|
| 216 |
|
| 217 |
/**
|
| 218 |
+
* Executes a `PUT` request using the `HttpClient` service from the environment.
|
| 219 |
+
*
|
| 220 |
* @category accessors
|
| 221 |
+
* @since 4.0.0
|
| 222 |
*/
|
| 223 |
export const put: (url: string | URL, options?: HttpClientRequest.Options.NoUrl | undefined) => Effect.Effect<
|
| 224 |
HttpClientResponse.HttpClientResponse,
|
|
|
|
| 227 |
> = accessor("put")
|
| 228 |
|
| 229 |
/**
|
| 230 |
+
* Executes a `DELETE` request using the `HttpClient` service from the environment.
|
| 231 |
+
*
|
| 232 |
* @category accessors
|
| 233 |
+
* @since 4.0.0
|
| 234 |
*/
|
| 235 |
export const del: (url: string | URL, options?: HttpClientRequest.Options.NoUrl | undefined) => Effect.Effect<
|
| 236 |
HttpClientResponse.HttpClientResponse,
|
|
|
|
| 239 |
> = accessor("del")
|
| 240 |
|
| 241 |
/**
|
| 242 |
+
* Executes an `OPTIONS` request using the `HttpClient` service from the environment.
|
| 243 |
+
*
|
| 244 |
* @category accessors
|
| 245 |
+
* @since 4.0.0
|
| 246 |
*/
|
| 247 |
export const options: (url: string | URL, options?: HttpClientRequest.Options.NoUrl | undefined) => Effect.Effect<
|
| 248 |
HttpClientResponse.HttpClientResponse,
|
|
|
|
| 251 |
> = accessor("options")
|
| 252 |
|
| 253 |
/**
|
| 254 |
+
* Transforms a client by wrapping the response effect for each request.
|
| 255 |
+
*
|
| 256 |
+
* **Details**
|
| 257 |
+
*
|
| 258 |
+
* The transformation receives both the response effect and the original request, allowing it to change success, error, and environment behavior.
|
| 259 |
+
*
|
| 260 |
* @category mapping & sequencing
|
| 261 |
+
* @since 4.0.0
|
| 262 |
*/
|
| 263 |
export const transform: {
|
| 264 |
/**
|
| 265 |
+
* Transforms a client by wrapping the response effect for each request.
|
| 266 |
+
*
|
| 267 |
+
* **Details**
|
| 268 |
+
*
|
| 269 |
+
* The transformation receives both the response effect and the original request, allowing it to change success, error, and environment behavior.
|
| 270 |
+
*
|
| 271 |
* @category mapping & sequencing
|
| 272 |
+
* @since 4.0.0
|
| 273 |
*/
|
| 274 |
<E, R, E1, R1>(
|
| 275 |
f: (
|
|
|
|
| 278 |
) => Effect.Effect<HttpClientResponse.HttpClientResponse, E1, R1>
|
| 279 |
): (self: HttpClient.With<E, R>) => HttpClient.With<E | E1, R | R1>
|
| 280 |
/**
|
| 281 |
+
* Transforms a client by wrapping the response effect for each request.
|
| 282 |
+
*
|
| 283 |
+
* **Details**
|
| 284 |
+
*
|
| 285 |
+
* The transformation receives both the response effect and the original request, allowing it to change success, error, and environment behavior.
|
| 286 |
+
*
|
| 287 |
* @category mapping & sequencing
|
| 288 |
+
* @since 4.0.0
|
| 289 |
*/
|
| 290 |
<E, R, E1, R1>(
|
| 291 |
self: HttpClient.With<E, R>,
|
|
|
|
| 307 |
))
|
| 308 |
|
| 309 |
/**
|
| 310 |
+
* Transforms a client by applying an effectful transformation to each response effect.
|
| 311 |
+
*
|
| 312 |
* @category mapping & sequencing
|
| 313 |
+
* @since 4.0.0
|
| 314 |
*/
|
| 315 |
export const transformResponse: {
|
| 316 |
/**
|
| 317 |
+
* Transforms a client by applying an effectful transformation to each response effect.
|
| 318 |
+
*
|
| 319 |
* @category mapping & sequencing
|
| 320 |
+
* @since 4.0.0
|
| 321 |
*/
|
| 322 |
<E, R, E1, R1>(
|
| 323 |
f: (
|
|
|
|
| 325 |
) => Effect.Effect<HttpClientResponse.HttpClientResponse, E1, R1>
|
| 326 |
): (self: HttpClient.With<E, R>) => HttpClient.With<E1, R1>
|
| 327 |
/**
|
| 328 |
+
* Transforms a client by applying an effectful transformation to each response effect.
|
| 329 |
+
*
|
| 330 |
* @category mapping & sequencing
|
| 331 |
+
* @since 4.0.0
|
| 332 |
*/
|
| 333 |
<E, R, E1, R1>(
|
| 334 |
self: HttpClient.With<E, R>,
|
|
|
|
| 361 |
|
| 362 |
export {
|
| 363 |
/**
|
| 364 |
+
* Handles all client failures with an effectful recovery function and returns a transformed client.
|
| 365 |
+
*
|
| 366 |
* @category error handling
|
| 367 |
+
* @since 4.0.0
|
| 368 |
*/
|
| 369 |
catch_ as catch
|
| 370 |
}
|
| 371 |
|
| 372 |
/**
|
| 373 |
+
* Handles client failures with one or more matching `_tag` values and returns a transformed client.
|
| 374 |
+
*
|
| 375 |
* @category error handling
|
| 376 |
+
* @since 4.0.0
|
| 377 |
*/
|
| 378 |
export const catchTag: {
|
| 379 |
/**
|
| 380 |
+
* Handles client failures with one or more matching `_tag` values and returns a transformed client.
|
| 381 |
+
*
|
| 382 |
* @category error handling
|
| 383 |
+
* @since 4.0.0
|
| 384 |
*/
|
| 385 |
<K extends Tags<E> | NonEmptyReadonlyArray<Tags<E>>, E, E1, R1>(
|
| 386 |
tag: K,
|
|
|
|
| 391 |
self: HttpClient.With<E, R>
|
| 392 |
) => HttpClient.With<E1 | ExcludeTag<E, K extends NonEmptyReadonlyArray<string> ? K[number] : K>, R1 | R>
|
| 393 |
/**
|
| 394 |
+
* Handles client failures with one or more matching `_tag` values and returns a transformed client.
|
| 395 |
+
*
|
| 396 |
* @category error handling
|
| 397 |
+
* @since 4.0.0
|
| 398 |
*/
|
| 399 |
<R, E, K extends Tags<E> | NonEmptyReadonlyArray<Tags<E>>, R1, E1>(
|
| 400 |
self: HttpClient.With<E, R>,
|
|
|
|
| 412 |
e: ExtractTag<E, K extends NonEmptyReadonlyArray<string> ? K[number] : K>
|
| 413 |
) => Effect.Effect<HttpClientResponse.HttpClientResponse, E1, R1>
|
| 414 |
): HttpClient.With<E1 | ExcludeTag<E, K extends NonEmptyReadonlyArray<string> ? K[number] : K>, R1 | R> =>
|
| 415 |
+
transformResponse(
|
| 416 |
+
self,
|
| 417 |
+
(effect) =>
|
| 418 |
+
Effect.catchTag</**
|
| 419 |
+
* Handles client failures with one or more matching `_tag` values and returns a transformed client.
|
| 420 |
+
*
|
| 421 |
+
* @category error handling
|
| 422 |
+
* @since 4.0.0
|
| 423 |
+
*/
|
| 424 |
+
HttpClientResponse.HttpClientResponse, /**
|
| 425 |
+
* Handles client failures with one or more matching `_tag` values and returns a transformed client.
|
| 426 |
+
*
|
| 427 |
+
* @category error handling
|
| 428 |
+
* @since 4.0.0
|
| 429 |
+
*/
|
| 430 |
+
E, /**
|
| 431 |
+
* Handles client failures with one or more matching `_tag` values and returns a transformed client.
|
| 432 |
+
*
|
| 433 |
+
* @category error handling
|
| 434 |
+
* @since 4.0.0
|
| 435 |
+
*/
|
| 436 |
+
R, /**
|
| 437 |
+
* Handles client failures with one or more matching `_tag` values and returns a transformed client.
|
| 438 |
+
*
|
| 439 |
+
* @category error handling
|
| 440 |
+
* @since 4.0.0
|
| 441 |
+
*/
|
| 442 |
+
K, /**
|
| 443 |
+
* Handles client failures with one or more matching `_tag` values and returns a transformed client.
|
| 444 |
+
*
|
| 445 |
+
* @category error handling
|
| 446 |
+
* @since 4.0.0
|
| 447 |
+
*/
|
| 448 |
+
R1, /**
|
| 449 |
+
* Handles client failures with one or more matching `_tag` values and returns a transformed client.
|
| 450 |
+
*
|
| 451 |
+
* @category error handling
|
| 452 |
+
* @since 4.0.0
|
| 453 |
+
*/
|
| 454 |
+
E1, /**
|
| 455 |
+
* Handles client failures with one or more matching `_tag` values and returns a transformed client.
|
| 456 |
+
*
|
| 457 |
+
* @category error handling
|
| 458 |
+
* @since 4.0.0
|
| 459 |
+
*/
|
| 460 |
+
HttpClientResponse.HttpClientResponse>(
|
| 461 |
+
effect,
|
| 462 |
+
tag,
|
| 463 |
+
f
|
| 464 |
+
)
|
| 465 |
+
)
|
| 466 |
)
|
| 467 |
|
| 468 |
/**
|
| 469 |
+
* Handles client failures by matching their `_tag` values against a case map.
|
| 470 |
+
*
|
| 471 |
* @category error handling
|
| 472 |
+
* @since 4.0.0
|
| 473 |
*/
|
| 474 |
export const catchTags: {
|
| 475 |
/**
|
| 476 |
+
* Handles client failures by matching their `_tag` values against a case map.
|
| 477 |
+
*
|
| 478 |
* @category error handling
|
| 479 |
+
* @since 4.0.0
|
| 480 |
*/
|
| 481 |
<
|
| 482 |
E,
|
|
|
|
| 500 |
}[keyof Cases]
|
| 501 |
>
|
| 502 |
/**
|
| 503 |
+
* Handles client failures by matching their `_tag` values against a case map.
|
| 504 |
+
*
|
| 505 |
* @category error handling
|
| 506 |
+
* @since 4.0.0
|
| 507 |
*/
|
| 508 |
<
|
| 509 |
E extends { _tag: string },
|
|
|
|
| 569 |
/**
|
| 570 |
* Filters the result of a response, or runs an alternative effect if the predicate fails.
|
| 571 |
*
|
|
|
|
| 572 |
* @category filters
|
| 573 |
+
* @since 4.0.0
|
| 574 |
*/
|
| 575 |
export const filterOrElse: {
|
| 576 |
/**
|
| 577 |
* Filters the result of a response, or runs an alternative effect if the predicate fails.
|
| 578 |
*
|
|
|
|
| 579 |
* @category filters
|
| 580 |
+
* @since 4.0.0
|
| 581 |
*/
|
| 582 |
<B extends HttpClientResponse.HttpClientResponse, E2, R2>(
|
| 583 |
refinement: Predicate.Refinement<NoInfer<HttpClientResponse.HttpClientResponse>, B>,
|
|
|
|
| 593 |
/**
|
| 594 |
* Filters the result of a response, or runs an alternative effect if the predicate fails.
|
| 595 |
*
|
|
|
|
| 596 |
* @category filters
|
| 597 |
+
* @since 4.0.0
|
| 598 |
*/
|
| 599 |
<E2, R2>(
|
| 600 |
predicate: Predicate.Predicate<NoInfer<HttpClientResponse.HttpClientResponse>>,
|
|
|
|
| 605 |
/**
|
| 606 |
* Filters the result of a response, or runs an alternative effect if the predicate fails.
|
| 607 |
*
|
|
|
|
| 608 |
* @category filters
|
| 609 |
+
* @since 4.0.0
|
| 610 |
*/
|
| 611 |
<E, R, B extends HttpClientResponse.HttpClientResponse, E2, R2>(
|
| 612 |
self: HttpClient.With<E, R>,
|
|
|
|
| 623 |
/**
|
| 624 |
* Filters the result of a response, or runs an alternative effect if the predicate fails.
|
| 625 |
*
|
|
|
|
| 626 |
* @category filters
|
| 627 |
+
* @since 4.0.0
|
| 628 |
*/
|
| 629 |
<E, R, E2, R2>(
|
| 630 |
self: HttpClient.With<E, R>,
|
|
|
|
| 636 |
} = dual(3, (self, f, orElse) => transformResponse(self, Effect.filterOrElse(f, orElse)))
|
| 637 |
|
| 638 |
/**
|
| 639 |
+
* Filters successful responses, or fails with the error produced by `orFailWith` when the predicate does not match.
|
| 640 |
*
|
|
|
|
| 641 |
* @category filters
|
| 642 |
+
* @since 4.0.0
|
| 643 |
*/
|
| 644 |
export const filterOrFail: {
|
| 645 |
/**
|
| 646 |
+
* Filters successful responses, or fails with the error produced by `orFailWith` when the predicate does not match.
|
| 647 |
*
|
|
|
|
| 648 |
* @category filters
|
| 649 |
+
* @since 4.0.0
|
| 650 |
*/
|
| 651 |
<B extends HttpClientResponse.HttpClientResponse, E2>(
|
| 652 |
refinement: Predicate.Refinement<NoInfer<HttpClientResponse.HttpClientResponse>, B>,
|
| 653 |
orFailWith: (response: NoInfer<HttpClientResponse.HttpClientResponse>) => E2
|
| 654 |
): <E, R>(self: HttpClient.With<E, R>) => HttpClient.With<E2 | E, R>
|
| 655 |
/**
|
| 656 |
+
* Filters successful responses, or fails with the error produced by `orFailWith` when the predicate does not match.
|
| 657 |
*
|
|
|
|
| 658 |
* @category filters
|
| 659 |
+
* @since 4.0.0
|
| 660 |
*/
|
| 661 |
<E2>(
|
| 662 |
predicate: Predicate.Predicate<NoInfer<HttpClientResponse.HttpClientResponse>>,
|
| 663 |
orFailWith: (response: NoInfer<HttpClientResponse.HttpClientResponse>) => E2
|
| 664 |
): <E, R>(self: HttpClient.With<E, R>) => HttpClient.With<E2 | E, R>
|
| 665 |
/**
|
| 666 |
+
* Filters successful responses, or fails with the error produced by `orFailWith` when the predicate does not match.
|
| 667 |
*
|
|
|
|
| 668 |
* @category filters
|
| 669 |
+
* @since 4.0.0
|
| 670 |
*/
|
| 671 |
<E, R, B extends HttpClientResponse.HttpClientResponse, E2>(
|
| 672 |
self: HttpClient.With<E, R>,
|
|
|
|
| 674 |
orFailWith: (response: NoInfer<HttpClientResponse.HttpClientResponse>) => E2
|
| 675 |
): HttpClient.With<E2 | E, R>
|
| 676 |
/**
|
| 677 |
+
* Filters successful responses, or fails with the error produced by `orFailWith` when the predicate does not match.
|
| 678 |
*
|
|
|
|
| 679 |
* @category filters
|
| 680 |
+
* @since 4.0.0
|
| 681 |
*/
|
| 682 |
<E, R, E2>(
|
| 683 |
self: HttpClient.With<E, R>,
|
|
|
|
| 689 |
/**
|
| 690 |
* Filters responses by HTTP status code.
|
| 691 |
*
|
|
|
|
| 692 |
* @category filters
|
| 693 |
+
* @since 4.0.0
|
| 694 |
*/
|
| 695 |
export const filterStatus: {
|
| 696 |
/**
|
| 697 |
* Filters responses by HTTP status code.
|
| 698 |
*
|
|
|
|
| 699 |
* @category filters
|
| 700 |
+
* @since 4.0.0
|
| 701 |
*/
|
| 702 |
(f: (status: number) => boolean): <E, R>(self: HttpClient.With<E, R>) => HttpClient.With<E | Error.HttpClientError, R>
|
| 703 |
/**
|
| 704 |
* Filters responses by HTTP status code.
|
| 705 |
*
|
|
|
|
| 706 |
* @category filters
|
| 707 |
+
* @since 4.0.0
|
| 708 |
*/
|
| 709 |
<E, R>(self: HttpClient.With<E, R>, f: (status: number) => boolean): HttpClient.With<E | Error.HttpClientError, R>
|
| 710 |
} = dual(
|
|
|
|
| 716 |
/**
|
| 717 |
* Filters responses that return a 2xx status code.
|
| 718 |
*
|
|
|
|
| 719 |
* @category filters
|
| 720 |
+
* @since 4.0.0
|
| 721 |
*/
|
| 722 |
export const filterStatusOk: <E, R>(self: HttpClient.With<E, R>) => HttpClient.With<E | Error.HttpClientError, R> =
|
| 723 |
transformResponse(Effect.flatMap(HttpClientResponse.filterStatusOk))
|
| 724 |
|
| 725 |
/**
|
| 726 |
+
* Constructs an `HttpClient.With` from a preprocessing function and a postprocessing function.
|
| 727 |
+
*
|
| 728 |
+
* **Details**
|
| 729 |
+
*
|
| 730 |
+
* `execute` applies preprocessing to the request and then passes the resulting request effect to postprocessing.
|
| 731 |
+
*
|
| 732 |
* @category constructors
|
| 733 |
+
* @since 4.0.0
|
| 734 |
*/
|
| 735 |
export const makeWith = <E2, R2, E, R>(
|
| 736 |
postprocess: (
|
|
|
|
| 768 |
}
|
| 769 |
|
| 770 |
/**
|
| 771 |
+
* Constructs an `HttpClient` from a low-level request runner.
|
| 772 |
+
*
|
| 773 |
+
* **Details**
|
| 774 |
+
*
|
| 775 |
+
* The runner receives the request, resolved URL, abort signal, and current fiber. The client wrapper handles URL construction failures, tracing and propagation, header redaction, and aborting non-scoped requests on interruption.
|
| 776 |
+
*
|
| 777 |
* @category constructors
|
| 778 |
+
* @since 4.0.0
|
| 779 |
*/
|
| 780 |
export const make = (
|
| 781 |
f: (
|
|
|
|
| 877 |
/**
|
| 878 |
* Appends a transformation of the request object before sending it.
|
| 879 |
*
|
|
|
|
| 880 |
* @category mapping & sequencing
|
| 881 |
+
* @since 4.0.0
|
| 882 |
*/
|
| 883 |
export const mapRequest: {
|
| 884 |
/**
|
| 885 |
* Appends a transformation of the request object before sending it.
|
| 886 |
*
|
|
|
|
| 887 |
* @category mapping & sequencing
|
| 888 |
+
* @since 4.0.0
|
| 889 |
*/
|
| 890 |
(
|
| 891 |
f: (a: HttpClientRequest.HttpClientRequest) => HttpClientRequest.HttpClientRequest
|
|
|
|
| 893 |
/**
|
| 894 |
* Appends a transformation of the request object before sending it.
|
| 895 |
*
|
|
|
|
| 896 |
* @category mapping & sequencing
|
| 897 |
+
* @since 4.0.0
|
| 898 |
*/
|
| 899 |
<E, R>(
|
| 900 |
self: HttpClient.With<E, R>,
|
|
|
|
| 911 |
/**
|
| 912 |
* Appends an effectful transformation of the request object before sending it.
|
| 913 |
*
|
|
|
|
| 914 |
* @category mapping & sequencing
|
| 915 |
+
* @since 4.0.0
|
| 916 |
*/
|
| 917 |
export const mapRequestEffect: {
|
| 918 |
/**
|
| 919 |
* Appends an effectful transformation of the request object before sending it.
|
| 920 |
*
|
|
|
|
| 921 |
* @category mapping & sequencing
|
| 922 |
+
* @since 4.0.0
|
| 923 |
*/
|
| 924 |
<E2, R2>(
|
| 925 |
f: (a: HttpClientRequest.HttpClientRequest) => Effect.Effect<HttpClientRequest.HttpClientRequest, E2, R2>
|
|
|
|
| 927 |
/**
|
| 928 |
* Appends an effectful transformation of the request object before sending it.
|
| 929 |
*
|
|
|
|
| 930 |
* @category mapping & sequencing
|
| 931 |
+
* @since 4.0.0
|
| 932 |
*/
|
| 933 |
<E, R, E2, R2>(
|
| 934 |
self: HttpClient.With<E, R>,
|
|
|
|
| 946 |
/**
|
| 947 |
* Prepends a transformation of the request object before sending it.
|
| 948 |
*
|
|
|
|
| 949 |
* @category mapping & sequencing
|
| 950 |
+
* @since 4.0.0
|
| 951 |
*/
|
| 952 |
export const mapRequestInput: {
|
| 953 |
/**
|
| 954 |
* Prepends a transformation of the request object before sending it.
|
| 955 |
*
|
|
|
|
| 956 |
* @category mapping & sequencing
|
| 957 |
+
* @since 4.0.0
|
| 958 |
*/
|
| 959 |
(
|
| 960 |
f: (a: HttpClientRequest.HttpClientRequest) => HttpClientRequest.HttpClientRequest
|
|
|
|
| 962 |
/**
|
| 963 |
* Prepends a transformation of the request object before sending it.
|
| 964 |
*
|
|
|
|
| 965 |
* @category mapping & sequencing
|
| 966 |
+
* @since 4.0.0
|
| 967 |
*/
|
| 968 |
<E, R>(
|
| 969 |
self: HttpClient.With<E, R>,
|
|
|
|
| 980 |
/**
|
| 981 |
* Prepends an effectful transformation of the request object before sending it.
|
| 982 |
*
|
|
|
|
| 983 |
* @category mapping & sequencing
|
| 984 |
+
* @since 4.0.0
|
| 985 |
*/
|
| 986 |
export const mapRequestInputEffect: {
|
| 987 |
/**
|
| 988 |
* Prepends an effectful transformation of the request object before sending it.
|
| 989 |
*
|
|
|
|
| 990 |
* @category mapping & sequencing
|
| 991 |
+
* @since 4.0.0
|
| 992 |
*/
|
| 993 |
<E2, R2>(
|
| 994 |
f: (a: HttpClientRequest.HttpClientRequest) => Effect.Effect<HttpClientRequest.HttpClientRequest, E2, R2>
|
|
|
|
| 996 |
/**
|
| 997 |
* Prepends an effectful transformation of the request object before sending it.
|
| 998 |
*
|
|
|
|
| 999 |
* @category mapping & sequencing
|
| 1000 |
+
* @since 4.0.0
|
| 1001 |
*/
|
| 1002 |
<E, R, E2, R2>(
|
| 1003 |
self: HttpClient.With<E, R>,
|
|
|
|
| 1013 |
)
|
| 1014 |
|
| 1015 |
/**
|
| 1016 |
+
* Namespace containing type-level helpers for retrying HTTP clients.
|
| 1017 |
+
*
|
| 1018 |
* @since 4.0.0
|
|
|
|
| 1019 |
*/
|
| 1020 |
export declare namespace Retry {
|
| 1021 |
/**
|
| 1022 |
+
* Computes the client type returned by `retry` for a given set of retry options.
|
| 1023 |
+
*
|
| 1024 |
+
* **Details**
|
| 1025 |
+
*
|
| 1026 |
+
* The result includes errors and requirements introduced by schedules and effectful retry predicates.
|
| 1027 |
+
*
|
| 1028 |
* @category error handling
|
| 1029 |
+
* @since 4.0.0
|
| 1030 |
*/
|
| 1031 |
export type Return<R, E, O extends NoExcessProperties<Effect.Retry.Options<E>, O>> = HttpClient.With<
|
| 1032 |
| (O extends { schedule: Schedule.Schedule<infer _O, infer _I, infer _E, infer _R> } ? E | _E
|
|
|
|
| 1045 |
/**
|
| 1046 |
* Retries the request based on a provided schedule or policy.
|
| 1047 |
*
|
|
|
|
| 1048 |
* @category error handling
|
| 1049 |
+
* @since 4.0.0
|
| 1050 |
*/
|
| 1051 |
export const retry: {
|
| 1052 |
/**
|
| 1053 |
* Retries the request based on a provided schedule or policy.
|
| 1054 |
*
|
|
|
|
| 1055 |
* @category error handling
|
| 1056 |
+
* @since 4.0.0
|
| 1057 |
*/
|
| 1058 |
<E, O extends NoExcessProperties<Effect.Retry.Options<E>, O>>(options: O): <R>(self: HttpClient.With<E, R>) => Retry.Return<R, E, O>
|
| 1059 |
/**
|
| 1060 |
* Retries the request based on a provided schedule or policy.
|
| 1061 |
*
|
|
|
|
| 1062 |
* @category error handling
|
| 1063 |
+
* @since 4.0.0
|
| 1064 |
*/
|
| 1065 |
<B, E, ES, R1>(policy: Schedule.Schedule<B, NoInfer<E>, ES, R1>): <R>(self: HttpClient.With<E, R>) => HttpClient.With<E | ES, R1 | R>
|
| 1066 |
/**
|
| 1067 |
* Retries the request based on a provided schedule or policy.
|
| 1068 |
*
|
|
|
|
| 1069 |
* @category error handling
|
| 1070 |
+
* @since 4.0.0
|
| 1071 |
*/
|
| 1072 |
<E, R, O extends NoExcessProperties<Effect.Retry.Options<E>, O>>(self: HttpClient.With<E, R>, options: O): Retry.Return<R, E, O>
|
| 1073 |
/**
|
| 1074 |
* Retries the request based on a provided schedule or policy.
|
| 1075 |
*
|
|
|
|
| 1076 |
* @category error handling
|
| 1077 |
+
* @since 4.0.0
|
| 1078 |
*/
|
| 1079 |
<E, R, B, ES, R1>(self: HttpClient.With<E, R>, policy: Schedule.Schedule<B, E, ES, R1>): HttpClient.With<E | ES, R1 | R>
|
| 1080 |
} = dual(
|
|
|
|
| 1088 |
/**
|
| 1089 |
* Retries common transient errors, such as rate limiting, timeouts or network issues.
|
| 1090 |
*
|
| 1091 |
+
* **When to use**
|
| 1092 |
+
*
|
| 1093 |
+
* Use to focus on retrying errors, transient responses, or both.
|
| 1094 |
+
*
|
| 1095 |
+
* **Details**
|
| 1096 |
*
|
| 1097 |
* Specifying a `while` predicate allows you to consider other errors as
|
| 1098 |
* transient, and is ignored in "response-only" mode.
|
| 1099 |
*
|
|
|
|
| 1100 |
* @category error handling
|
| 1101 |
+
* @since 4.0.0
|
| 1102 |
*/
|
| 1103 |
export const retryTransient: {
|
| 1104 |
/**
|
| 1105 |
* Retries common transient errors, such as rate limiting, timeouts or network issues.
|
| 1106 |
*
|
| 1107 |
+
* **When to use**
|
| 1108 |
+
*
|
| 1109 |
+
* Use to focus on retrying errors, transient responses, or both.
|
| 1110 |
+
*
|
| 1111 |
+
* **Details**
|
| 1112 |
*
|
| 1113 |
* Specifying a `while` predicate allows you to consider other errors as
|
| 1114 |
* transient, and is ignored in "response-only" mode.
|
| 1115 |
*
|
|
|
|
| 1116 |
* @category error handling
|
| 1117 |
+
* @since 4.0.0
|
| 1118 |
*/
|
| 1119 |
<
|
| 1120 |
E,
|
|
|
|
| 1139 |
/**
|
| 1140 |
* Retries common transient errors, such as rate limiting, timeouts or network issues.
|
| 1141 |
*
|
| 1142 |
+
* **When to use**
|
| 1143 |
+
*
|
| 1144 |
+
* Use to focus on retrying errors, transient responses, or both.
|
| 1145 |
+
*
|
| 1146 |
+
* **Details**
|
| 1147 |
*
|
| 1148 |
* Specifying a `while` predicate allows you to consider other errors as
|
| 1149 |
* transient, and is ignored in "response-only" mode.
|
| 1150 |
*
|
|
|
|
| 1151 |
* @category error handling
|
| 1152 |
+
* @since 4.0.0
|
| 1153 |
*/
|
| 1154 |
<
|
| 1155 |
E,
|
|
|
|
| 1176 |
/**
|
| 1177 |
* Retries common transient errors, such as rate limiting, timeouts or network issues.
|
| 1178 |
*
|
| 1179 |
+
* **When to use**
|
| 1180 |
+
*
|
| 1181 |
+
* Use to focus on retrying errors, transient responses, or both.
|
| 1182 |
+
*
|
| 1183 |
+
* **Details**
|
| 1184 |
*
|
| 1185 |
* Specifying a `while` predicate allows you to consider other errors as
|
| 1186 |
* transient, and is ignored in "response-only" mode.
|
| 1187 |
*
|
|
|
|
| 1188 |
* @category error handling
|
| 1189 |
+
* @since 4.0.0
|
| 1190 |
*/
|
| 1191 |
<B, E, ES = never, R1 = never>(
|
| 1192 |
options: Schedule.Schedule<B, NoInfer<HttpClientResponse.HttpClientResponse | E>, ES, R1>
|
|
|
|
| 1194 |
/**
|
| 1195 |
* Retries common transient errors, such as rate limiting, timeouts or network issues.
|
| 1196 |
*
|
| 1197 |
+
* **When to use**
|
| 1198 |
+
*
|
| 1199 |
+
* Use to focus on retrying errors, transient responses, or both.
|
| 1200 |
+
*
|
| 1201 |
+
* **Details**
|
| 1202 |
*
|
| 1203 |
* Specifying a `while` predicate allows you to consider other errors as
|
| 1204 |
* transient, and is ignored in "response-only" mode.
|
| 1205 |
*
|
|
|
|
| 1206 |
* @category error handling
|
| 1207 |
+
* @since 4.0.0
|
| 1208 |
*/
|
| 1209 |
<E, R, B, ES = never, R1 = never>(
|
| 1210 |
self: HttpClient.With<E, R>,
|
|
|
|
| 1253 |
)
|
| 1254 |
|
| 1255 |
/**
|
| 1256 |
+
* Namespace containing configuration types for `withRateLimiter`.
|
| 1257 |
+
*
|
| 1258 |
* @since 4.0.0
|
|
|
|
| 1259 |
*/
|
| 1260 |
export declare namespace WithRateLimiter {
|
| 1261 |
/**
|
| 1262 |
+
* Options used to configure `withRateLimiter`.
|
| 1263 |
+
*
|
| 1264 |
+
* **Details**
|
| 1265 |
+
*
|
| 1266 |
+
* They define the backing limiter, initial limit window, keying strategy, algorithm, token cost, and whether response headers update future limits.
|
| 1267 |
+
*
|
| 1268 |
* @category rate limiting
|
| 1269 |
+
* @since 4.0.0
|
| 1270 |
*/
|
| 1271 |
export interface Options {
|
| 1272 |
/**
|
|
|
|
| 1305 |
/**
|
| 1306 |
* Applies request rate limiting using the `RateLimiter` service.
|
| 1307 |
*
|
| 1308 |
+
* **Details**
|
| 1309 |
+
*
|
| 1310 |
* It can update limits by inspecting common rate limit response headers and
|
| 1311 |
* automatically retries HTTP `429` responses (or `HttpClientError` values
|
| 1312 |
* wrapping a `429` response) by forcing the retry back through the limiter.
|
| 1313 |
*
|
|
|
|
| 1314 |
* @category rate limiting
|
| 1315 |
+
* @since 4.0.0
|
| 1316 |
*/
|
| 1317 |
export const withRateLimiter: {
|
| 1318 |
/**
|
| 1319 |
* Applies request rate limiting using the `RateLimiter` service.
|
| 1320 |
*
|
| 1321 |
+
* **Details**
|
| 1322 |
+
*
|
| 1323 |
* It can update limits by inspecting common rate limit response headers and
|
| 1324 |
* automatically retries HTTP `429` responses (or `HttpClientError` values
|
| 1325 |
* wrapping a `429` response) by forcing the retry back through the limiter.
|
| 1326 |
*
|
|
|
|
| 1327 |
* @category rate limiting
|
| 1328 |
+
* @since 4.0.0
|
| 1329 |
*/
|
| 1330 |
(options: WithRateLimiter.Options): <E, R>(
|
| 1331 |
self: HttpClient.With<E, R>
|
|
|
|
| 1333 |
/**
|
| 1334 |
* Applies request rate limiting using the `RateLimiter` service.
|
| 1335 |
*
|
| 1336 |
+
* **Details**
|
| 1337 |
+
*
|
| 1338 |
* It can update limits by inspecting common rate limit response headers and
|
| 1339 |
* automatically retries HTTP `429` responses (or `HttpClientError` values
|
| 1340 |
* wrapping a `429` response) by forcing the retry back through the limiter.
|
| 1341 |
*
|
|
|
|
| 1342 |
* @category rate limiting
|
| 1343 |
+
* @since 4.0.0
|
| 1344 |
*/
|
| 1345 |
<E, R>(self: HttpClient.With<E, R>, options: WithRateLimiter.Options): HttpClient.With<E | RateLimiter.RateLimiterError, R>
|
| 1346 |
} = dual(2, <E, R>(
|
|
|
|
| 1563 |
/**
|
| 1564 |
* Performs an additional effect after a successful request.
|
| 1565 |
*
|
|
|
|
| 1566 |
* @category mapping & sequencing
|
| 1567 |
+
* @since 4.0.0
|
| 1568 |
*/
|
| 1569 |
export const tap: {
|
| 1570 |
/**
|
| 1571 |
* Performs an additional effect after a successful request.
|
| 1572 |
*
|
|
|
|
| 1573 |
* @category mapping & sequencing
|
| 1574 |
+
* @since 4.0.0
|
| 1575 |
*/
|
| 1576 |
<_, E2, R2>(
|
| 1577 |
f: (response: HttpClientResponse.HttpClientResponse) => Effect.Effect<_, E2, R2>
|
|
|
|
| 1579 |
/**
|
| 1580 |
* Performs an additional effect after a successful request.
|
| 1581 |
*
|
|
|
|
| 1582 |
* @category mapping & sequencing
|
| 1583 |
+
* @since 4.0.0
|
| 1584 |
*/
|
| 1585 |
<E, R, _, E2, R2>(
|
| 1586 |
self: HttpClient.With<E, R>,
|
|
|
|
| 1597 |
/**
|
| 1598 |
* Performs an additional effect after an unsuccessful request.
|
| 1599 |
*
|
|
|
|
| 1600 |
* @category mapping & sequencing
|
| 1601 |
+
* @since 4.0.0
|
| 1602 |
*/
|
| 1603 |
export const tapError: {
|
| 1604 |
/**
|
| 1605 |
* Performs an additional effect after an unsuccessful request.
|
| 1606 |
*
|
|
|
|
| 1607 |
* @category mapping & sequencing
|
| 1608 |
+
* @since 4.0.0
|
| 1609 |
*/
|
| 1610 |
<_, E, E2, R2>(f: (e: NoInfer<E>) => Effect.Effect<_, E2, R2>): <R>(self: HttpClient.With<E, R>) => HttpClient.With<E | E2, R | R2>
|
| 1611 |
/**
|
| 1612 |
* Performs an additional effect after an unsuccessful request.
|
| 1613 |
*
|
|
|
|
| 1614 |
* @category mapping & sequencing
|
| 1615 |
+
* @since 4.0.0
|
| 1616 |
*/
|
| 1617 |
<E, R, _, E2, R2>(
|
| 1618 |
self: HttpClient.With<E, R>,
|
|
|
|
| 1629 |
/**
|
| 1630 |
* Performs an additional effect on the request before sending it.
|
| 1631 |
*
|
|
|
|
| 1632 |
* @category mapping & sequencing
|
| 1633 |
+
* @since 4.0.0
|
| 1634 |
*/
|
| 1635 |
export const tapRequest: {
|
| 1636 |
/**
|
| 1637 |
* Performs an additional effect on the request before sending it.
|
| 1638 |
*
|
|
|
|
| 1639 |
* @category mapping & sequencing
|
| 1640 |
+
* @since 4.0.0
|
| 1641 |
*/
|
| 1642 |
<_, E2, R2>(f: (a: HttpClientRequest.HttpClientRequest) => Effect.Effect<_, E2, R2>): <E, R>(self: HttpClient.With<E, R>) => HttpClient.With<E | E2, R | R2>
|
| 1643 |
/**
|
| 1644 |
* Performs an additional effect on the request before sending it.
|
| 1645 |
*
|
|
|
|
| 1646 |
* @category mapping & sequencing
|
| 1647 |
+
* @since 4.0.0
|
| 1648 |
*/
|
| 1649 |
<E, R, _, E2, R2>(
|
| 1650 |
self: HttpClient.With<E, R>,
|
|
|
|
| 1660 |
)
|
| 1661 |
|
| 1662 |
/**
|
| 1663 |
+
* Adds a `Ref` of cookies to the client for handling cookies across requests.
|
| 1664 |
+
*
|
| 1665 |
+
* **When to use**
|
| 1666 |
+
*
|
| 1667 |
+
* Use to add shared cookie storage to a client so response cookies are retained
|
| 1668 |
+
* and sent by later requests.
|
| 1669 |
*
|
|
|
|
| 1670 |
* @category cookies
|
| 1671 |
+
* @since 4.0.0
|
| 1672 |
*/
|
| 1673 |
export const withCookiesRef: {
|
| 1674 |
/**
|
| 1675 |
+
* Adds a `Ref` of cookies to the client for handling cookies across requests.
|
| 1676 |
+
*
|
| 1677 |
+
* **When to use**
|
| 1678 |
+
*
|
| 1679 |
+
* Use to add shared cookie storage to a client so response cookies are retained
|
| 1680 |
+
* and sent by later requests.
|
| 1681 |
*
|
|
|
|
| 1682 |
* @category cookies
|
| 1683 |
+
* @since 4.0.0
|
| 1684 |
*/
|
| 1685 |
(ref: Ref.Ref<Cookies.Cookies>): <E, R>(self: HttpClient.With<E, R>) => HttpClient.With<E, R>
|
| 1686 |
/**
|
| 1687 |
+
* Adds a `Ref` of cookies to the client for handling cookies across requests.
|
| 1688 |
+
*
|
| 1689 |
+
* **When to use**
|
| 1690 |
+
*
|
| 1691 |
+
* Use to add shared cookie storage to a client so response cookies are retained
|
| 1692 |
+
* and sent by later requests.
|
| 1693 |
*
|
|
|
|
| 1694 |
* @category cookies
|
| 1695 |
+
* @since 4.0.0
|
| 1696 |
*/
|
| 1697 |
<E, R>(self: HttpClient.With<E, R>, ref: Ref.Ref<Cookies.Cookies>): HttpClient.With<E, R>
|
| 1698 |
} = dual(
|
|
|
|
| 1720 |
)
|
| 1721 |
|
| 1722 |
/**
|
| 1723 |
+
* Attaches the lifetime of the `HttpClientRequest` to a `Scope`.
|
| 1724 |
*
|
| 1725 |
+
* @category resource management
|
| 1726 |
* @since 4.0.0
|
|
|
|
| 1727 |
*/
|
| 1728 |
export const withScope = <E, R>(
|
| 1729 |
self: HttpClient.With<E, R>
|
|
|
|
| 1741 |
)
|
| 1742 |
|
| 1743 |
/**
|
| 1744 |
+
* Enables following HTTP redirects up to a specified number of times.
|
| 1745 |
*
|
|
|
|
| 1746 |
* @category redirects
|
| 1747 |
+
* @since 4.0.0
|
| 1748 |
*/
|
| 1749 |
export const followRedirects: {
|
| 1750 |
/**
|
| 1751 |
+
* Enables following HTTP redirects up to a specified number of times.
|
| 1752 |
*
|
|
|
|
| 1753 |
* @category redirects
|
| 1754 |
+
* @since 4.0.0
|
| 1755 |
*/
|
| 1756 |
(maxRedirects?: number | undefined): <E, R>(self: HttpClient.With<E, R>) => HttpClient.With<E, R>
|
| 1757 |
/**
|
| 1758 |
+
* Enables following HTTP redirects up to a specified number of times.
|
| 1759 |
*
|
|
|
|
| 1760 |
* @category redirects
|
| 1761 |
+
* @since 4.0.0
|
| 1762 |
*/
|
| 1763 |
<E, R>(self: HttpClient.With<E, R>, maxRedirects?: number | undefined): HttpClient.With<E, R>
|
| 1764 |
} = dual((args) => isHttpClient(args[0]), <E, R>(
|
|
|
|
| 1791 |
))
|
| 1792 |
|
| 1793 |
/**
|
| 1794 |
+
* Context reference for a predicate that disables client-side tracing for matching outgoing requests.
|
| 1795 |
+
*
|
| 1796 |
+
* @category references
|
| 1797 |
* @since 4.0.0
|
|
|
|
| 1798 |
*/
|
| 1799 |
export const TracerDisabledWhen = Context.Reference<
|
| 1800 |
Predicate.Predicate<HttpClientRequest.HttpClientRequest>
|
|
|
|
| 1803 |
})
|
| 1804 |
|
| 1805 |
/**
|
| 1806 |
+
* Context reference that controls whether outgoing client spans are propagated to request headers.
|
| 1807 |
+
*
|
| 1808 |
+
* @category references
|
| 1809 |
* @since 4.0.0
|
|
|
|
| 1810 |
*/
|
| 1811 |
export const TracerPropagationEnabled = Context.Reference<boolean>("effect/HttpClient/TracerPropagationEnabled", {
|
| 1812 |
defaultValue: constTrue
|
| 1813 |
})
|
| 1814 |
|
| 1815 |
/**
|
| 1816 |
+
* Context reference for generating the span name used for outgoing client request spans.
|
| 1817 |
+
*
|
| 1818 |
+
* @category references
|
| 1819 |
* @since 4.0.0
|
|
|
|
| 1820 |
*/
|
| 1821 |
export const SpanNameGenerator = Context.Reference<
|
| 1822 |
(request: HttpClientRequest.HttpClientRequest) => string
|
|
|
|
| 1825 |
})
|
| 1826 |
|
| 1827 |
/**
|
| 1828 |
+
* Creates an `HttpClient` layer and merges the layer construction context into client response effects.
|
| 1829 |
+
*
|
| 1830 |
+
* @category layers
|
| 1831 |
* @since 4.0.0
|
| 1832 |
*/
|
| 1833 |
export const layerMergedContext = <E, R>(
|
.opencode/node_modules/effect/src/unstable/http/HttpClientError.ts
CHANGED
|
@@ -1,4 +1,12 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Data from "../../Data.ts"
|
|
@@ -10,14 +18,18 @@ import type * as ClientResponse from "./HttpClientResponse.ts"
|
|
| 10 |
const TypeId = "~effect/http/HttpClientError"
|
| 11 |
|
| 12 |
/**
|
| 13 |
-
*
|
|
|
|
| 14 |
* @category guards
|
|
|
|
| 15 |
*/
|
| 16 |
export const isHttpClientError = (u: unknown): u is HttpClientError => hasProperty(u, TypeId)
|
| 17 |
|
| 18 |
/**
|
|
|
|
|
|
|
|
|
|
| 19 |
* @since 4.0.0
|
| 20 |
-
* @category error
|
| 21 |
*/
|
| 22 |
export class HttpClientError extends Data.TaggedError("HttpClientError")<{
|
| 23 |
readonly reason: HttpClientErrorReason
|
|
@@ -36,11 +48,15 @@ export class HttpClientError extends Data.TaggedError("HttpClientError")<{
|
|
| 36 |
}
|
| 37 |
|
| 38 |
/**
|
|
|
|
|
|
|
| 39 |
* @since 4.0.0
|
| 40 |
*/
|
| 41 |
readonly [TypeId] = TypeId
|
| 42 |
|
| 43 |
/**
|
|
|
|
|
|
|
| 44 |
* @since 4.0.0
|
| 45 |
*/
|
| 46 |
get request(): HttpClientRequest.HttpClientRequest {
|
|
@@ -48,6 +64,8 @@ export class HttpClientError extends Data.TaggedError("HttpClientError")<{
|
|
| 48 |
}
|
| 49 |
|
| 50 |
/**
|
|
|
|
|
|
|
| 51 |
* @since 4.0.0
|
| 52 |
*/
|
| 53 |
get response(): ClientResponse.HttpClientResponse | undefined {
|
|
@@ -65,8 +83,10 @@ const formatMessage = (reason: string, description: string | undefined, info: st
|
|
| 65 |
description ? `${reason}: ${description} (${info})` : `${reason} error (${info})`
|
| 66 |
|
| 67 |
/**
|
|
|
|
|
|
|
|
|
|
| 68 |
* @since 4.0.0
|
| 69 |
-
* @category error
|
| 70 |
*/
|
| 71 |
export class TransportError extends Data.TaggedError("TransportError")<{
|
| 72 |
readonly request: HttpClientRequest.HttpClientRequest
|
|
@@ -74,6 +94,8 @@ export class TransportError extends Data.TaggedError("TransportError")<{
|
|
| 74 |
readonly description?: string
|
| 75 |
}> {
|
| 76 |
/**
|
|
|
|
|
|
|
| 77 |
* @since 4.0.0
|
| 78 |
*/
|
| 79 |
get methodAndUrl() {
|
|
@@ -81,6 +103,8 @@ export class TransportError extends Data.TaggedError("TransportError")<{
|
|
| 81 |
}
|
| 82 |
|
| 83 |
/**
|
|
|
|
|
|
|
| 84 |
* @since 4.0.0
|
| 85 |
*/
|
| 86 |
override get message() {
|
|
@@ -89,8 +113,10 @@ export class TransportError extends Data.TaggedError("TransportError")<{
|
|
| 89 |
}
|
| 90 |
|
| 91 |
/**
|
|
|
|
|
|
|
|
|
|
| 92 |
* @since 4.0.0
|
| 93 |
-
* @category error
|
| 94 |
*/
|
| 95 |
export class EncodeError extends Data.TaggedError("EncodeError")<{
|
| 96 |
readonly request: HttpClientRequest.HttpClientRequest
|
|
@@ -98,6 +124,8 @@ export class EncodeError extends Data.TaggedError("EncodeError")<{
|
|
| 98 |
readonly description?: string
|
| 99 |
}> {
|
| 100 |
/**
|
|
|
|
|
|
|
| 101 |
* @since 4.0.0
|
| 102 |
*/
|
| 103 |
get methodAndUrl() {
|
|
@@ -105,6 +133,8 @@ export class EncodeError extends Data.TaggedError("EncodeError")<{
|
|
| 105 |
}
|
| 106 |
|
| 107 |
/**
|
|
|
|
|
|
|
| 108 |
* @since 4.0.0
|
| 109 |
*/
|
| 110 |
override get message() {
|
|
@@ -113,8 +143,10 @@ export class EncodeError extends Data.TaggedError("EncodeError")<{
|
|
| 113 |
}
|
| 114 |
|
| 115 |
/**
|
|
|
|
|
|
|
|
|
|
| 116 |
* @since 4.0.0
|
| 117 |
-
* @category error
|
| 118 |
*/
|
| 119 |
export class InvalidUrlError extends Data.TaggedError("InvalidUrlError")<{
|
| 120 |
readonly request: HttpClientRequest.HttpClientRequest
|
|
@@ -122,6 +154,8 @@ export class InvalidUrlError extends Data.TaggedError("InvalidUrlError")<{
|
|
| 122 |
readonly description?: string
|
| 123 |
}> {
|
| 124 |
/**
|
|
|
|
|
|
|
| 125 |
* @since 4.0.0
|
| 126 |
*/
|
| 127 |
get methodAndUrl() {
|
|
@@ -129,6 +163,8 @@ export class InvalidUrlError extends Data.TaggedError("InvalidUrlError")<{
|
|
| 129 |
}
|
| 130 |
|
| 131 |
/**
|
|
|
|
|
|
|
| 132 |
* @since 4.0.0
|
| 133 |
*/
|
| 134 |
override get message() {
|
|
@@ -137,8 +173,10 @@ export class InvalidUrlError extends Data.TaggedError("InvalidUrlError")<{
|
|
| 137 |
}
|
| 138 |
|
| 139 |
/**
|
|
|
|
|
|
|
|
|
|
| 140 |
* @since 4.0.0
|
| 141 |
-
* @category error
|
| 142 |
*/
|
| 143 |
export class StatusCodeError extends Data.TaggedError("StatusCodeError")<{
|
| 144 |
readonly request: HttpClientRequest.HttpClientRequest
|
|
@@ -147,6 +185,8 @@ export class StatusCodeError extends Data.TaggedError("StatusCodeError")<{
|
|
| 147 |
readonly description?: string | undefined
|
| 148 |
}> {
|
| 149 |
/**
|
|
|
|
|
|
|
| 150 |
* @since 4.0.0
|
| 151 |
*/
|
| 152 |
get methodAndUrl() {
|
|
@@ -154,6 +194,8 @@ export class StatusCodeError extends Data.TaggedError("StatusCodeError")<{
|
|
| 154 |
}
|
| 155 |
|
| 156 |
/**
|
|
|
|
|
|
|
| 157 |
* @since 4.0.0
|
| 158 |
*/
|
| 159 |
override get message() {
|
|
@@ -163,8 +205,10 @@ export class StatusCodeError extends Data.TaggedError("StatusCodeError")<{
|
|
| 163 |
}
|
| 164 |
|
| 165 |
/**
|
|
|
|
|
|
|
|
|
|
| 166 |
* @since 4.0.0
|
| 167 |
-
* @category error
|
| 168 |
*/
|
| 169 |
export class DecodeError extends Data.TaggedError("DecodeError")<{
|
| 170 |
readonly request: HttpClientRequest.HttpClientRequest
|
|
@@ -173,6 +217,8 @@ export class DecodeError extends Data.TaggedError("DecodeError")<{
|
|
| 173 |
readonly description?: string | undefined
|
| 174 |
}> {
|
| 175 |
/**
|
|
|
|
|
|
|
| 176 |
* @since 4.0.0
|
| 177 |
*/
|
| 178 |
get methodAndUrl() {
|
|
@@ -180,6 +226,8 @@ export class DecodeError extends Data.TaggedError("DecodeError")<{
|
|
| 180 |
}
|
| 181 |
|
| 182 |
/**
|
|
|
|
|
|
|
| 183 |
* @since 4.0.0
|
| 184 |
*/
|
| 185 |
override get message() {
|
|
@@ -189,8 +237,10 @@ export class DecodeError extends Data.TaggedError("DecodeError")<{
|
|
| 189 |
}
|
| 190 |
|
| 191 |
/**
|
|
|
|
|
|
|
|
|
|
| 192 |
* @since 4.0.0
|
| 193 |
-
* @category error
|
| 194 |
*/
|
| 195 |
export class EmptyBodyError extends Data.TaggedError("EmptyBodyError")<{
|
| 196 |
readonly request: HttpClientRequest.HttpClientRequest
|
|
@@ -199,6 +249,8 @@ export class EmptyBodyError extends Data.TaggedError("EmptyBodyError")<{
|
|
| 199 |
readonly description?: string | undefined
|
| 200 |
}> {
|
| 201 |
/**
|
|
|
|
|
|
|
| 202 |
* @since 4.0.0
|
| 203 |
*/
|
| 204 |
get methodAndUrl() {
|
|
@@ -206,6 +258,8 @@ export class EmptyBodyError extends Data.TaggedError("EmptyBodyError")<{
|
|
| 206 |
}
|
| 207 |
|
| 208 |
/**
|
|
|
|
|
|
|
| 209 |
* @since 4.0.0
|
| 210 |
*/
|
| 211 |
override get message() {
|
|
@@ -215,26 +269,35 @@ export class EmptyBodyError extends Data.TaggedError("EmptyBodyError")<{
|
|
| 215 |
}
|
| 216 |
|
| 217 |
/**
|
|
|
|
|
|
|
|
|
|
| 218 |
* @since 4.0.0
|
| 219 |
-
* @category error
|
| 220 |
*/
|
| 221 |
export type RequestError = TransportError | EncodeError | InvalidUrlError
|
| 222 |
|
| 223 |
/**
|
|
|
|
|
|
|
|
|
|
| 224 |
* @since 4.0.0
|
| 225 |
-
* @category error
|
| 226 |
*/
|
| 227 |
export type ResponseError = StatusCodeError | DecodeError | EmptyBodyError
|
| 228 |
|
| 229 |
/**
|
|
|
|
|
|
|
|
|
|
| 230 |
* @since 4.0.0
|
| 231 |
-
* @category error
|
| 232 |
*/
|
| 233 |
export type HttpClientErrorReason = RequestError | ResponseError
|
| 234 |
|
| 235 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
* @since 4.0.0
|
| 237 |
-
* @category Schema
|
| 238 |
*/
|
| 239 |
export class HttpClientErrorSchema extends Schema.ErrorClass<HttpClientErrorSchema>(TypeId)({
|
| 240 |
_tag: Schema.tag("HttpError"),
|
|
@@ -248,9 +311,11 @@ export class HttpClientErrorSchema extends Schema.ErrorClass<HttpClientErrorSche
|
|
| 248 |
"EmptyBodyError"
|
| 249 |
] satisfies ReadonlyArray<HttpClientErrorReason["_tag"]>
|
| 250 |
),
|
| 251 |
-
cause: Schema.optional(Schema.Defect)
|
| 252 |
}) {
|
| 253 |
/**
|
|
|
|
|
|
|
| 254 |
* @since 4.0.0
|
| 255 |
*/
|
| 256 |
static fromHttpClientError(error: HttpClientError): HttpClientErrorSchema {
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Typed failure model for Effect HTTP client operations.
|
| 3 |
+
*
|
| 4 |
+
* HTTP clients wrap request construction, transport, status-code validation, and
|
| 5 |
+
* response decoding failures in `HttpClientError`. The wrapper keeps the failed
|
| 6 |
+
* request and the specific failure reason together, so callers can handle client
|
| 7 |
+
* failures uniformly while still matching on the reason `_tag` for retry
|
| 8 |
+
* policy, logging, metrics, and user-facing messages.
|
| 9 |
+
*
|
| 10 |
* @since 4.0.0
|
| 11 |
*/
|
| 12 |
import * as Data from "../../Data.ts"
|
|
|
|
| 18 |
const TypeId = "~effect/http/HttpClientError"
|
| 19 |
|
| 20 |
/**
|
| 21 |
+
* Returns `true` when a value is an `HttpClientError`.
|
| 22 |
+
*
|
| 23 |
* @category guards
|
| 24 |
+
* @since 4.0.0
|
| 25 |
*/
|
| 26 |
export const isHttpClientError = (u: unknown): u is HttpClientError => hasProperty(u, TypeId)
|
| 27 |
|
| 28 |
/**
|
| 29 |
+
* Error wrapper for HTTP client failures, exposing the failed request and the optional response through its `reason`.
|
| 30 |
+
*
|
| 31 |
+
* @category errors
|
| 32 |
* @since 4.0.0
|
|
|
|
| 33 |
*/
|
| 34 |
export class HttpClientError extends Data.TaggedError("HttpClientError")<{
|
| 35 |
readonly reason: HttpClientErrorReason
|
|
|
|
| 48 |
}
|
| 49 |
|
| 50 |
/**
|
| 51 |
+
* Marks this value as an HTTP client error for runtime guards.
|
| 52 |
+
*
|
| 53 |
* @since 4.0.0
|
| 54 |
*/
|
| 55 |
readonly [TypeId] = TypeId
|
| 56 |
|
| 57 |
/**
|
| 58 |
+
* HTTP request associated with the client failure.
|
| 59 |
+
*
|
| 60 |
* @since 4.0.0
|
| 61 |
*/
|
| 62 |
get request(): HttpClientRequest.HttpClientRequest {
|
|
|
|
| 64 |
}
|
| 65 |
|
| 66 |
/**
|
| 67 |
+
* HTTP response associated with the client failure, when one was received.
|
| 68 |
+
*
|
| 69 |
* @since 4.0.0
|
| 70 |
*/
|
| 71 |
get response(): ClientResponse.HttpClientResponse | undefined {
|
|
|
|
| 83 |
description ? `${reason}: ${description} (${info})` : `${reason} error (${info})`
|
| 84 |
|
| 85 |
/**
|
| 86 |
+
* Error describing transport-level failures that occur while sending an HTTP request.
|
| 87 |
+
*
|
| 88 |
+
* @category errors
|
| 89 |
* @since 4.0.0
|
|
|
|
| 90 |
*/
|
| 91 |
export class TransportError extends Data.TaggedError("TransportError")<{
|
| 92 |
readonly request: HttpClientRequest.HttpClientRequest
|
|
|
|
| 94 |
readonly description?: string
|
| 95 |
}> {
|
| 96 |
/**
|
| 97 |
+
* Formats the request method and URL for transport error messages.
|
| 98 |
+
*
|
| 99 |
* @since 4.0.0
|
| 100 |
*/
|
| 101 |
get methodAndUrl() {
|
|
|
|
| 103 |
}
|
| 104 |
|
| 105 |
/**
|
| 106 |
+
* Builds the transport error message from the optional description and request details.
|
| 107 |
+
*
|
| 108 |
* @since 4.0.0
|
| 109 |
*/
|
| 110 |
override get message() {
|
|
|
|
| 113 |
}
|
| 114 |
|
| 115 |
/**
|
| 116 |
+
* Error describing failures while encoding an HTTP request body.
|
| 117 |
+
*
|
| 118 |
+
* @category errors
|
| 119 |
* @since 4.0.0
|
|
|
|
| 120 |
*/
|
| 121 |
export class EncodeError extends Data.TaggedError("EncodeError")<{
|
| 122 |
readonly request: HttpClientRequest.HttpClientRequest
|
|
|
|
| 124 |
readonly description?: string
|
| 125 |
}> {
|
| 126 |
/**
|
| 127 |
+
* Formats the request method and URL for request encoding error messages.
|
| 128 |
+
*
|
| 129 |
* @since 4.0.0
|
| 130 |
*/
|
| 131 |
get methodAndUrl() {
|
|
|
|
| 133 |
}
|
| 134 |
|
| 135 |
/**
|
| 136 |
+
* Builds the request encoding error message from the optional description and request details.
|
| 137 |
+
*
|
| 138 |
* @since 4.0.0
|
| 139 |
*/
|
| 140 |
override get message() {
|
|
|
|
| 143 |
}
|
| 144 |
|
| 145 |
/**
|
| 146 |
+
* Error describing failures while constructing a URL from an HTTP client request.
|
| 147 |
+
*
|
| 148 |
+
* @category errors
|
| 149 |
* @since 4.0.0
|
|
|
|
| 150 |
*/
|
| 151 |
export class InvalidUrlError extends Data.TaggedError("InvalidUrlError")<{
|
| 152 |
readonly request: HttpClientRequest.HttpClientRequest
|
|
|
|
| 154 |
readonly description?: string
|
| 155 |
}> {
|
| 156 |
/**
|
| 157 |
+
* Formats the request method and URL for invalid URL error messages.
|
| 158 |
+
*
|
| 159 |
* @since 4.0.0
|
| 160 |
*/
|
| 161 |
get methodAndUrl() {
|
|
|
|
| 163 |
}
|
| 164 |
|
| 165 |
/**
|
| 166 |
+
* Builds the invalid URL error message from the optional description and request details.
|
| 167 |
+
*
|
| 168 |
* @since 4.0.0
|
| 169 |
*/
|
| 170 |
override get message() {
|
|
|
|
| 173 |
}
|
| 174 |
|
| 175 |
/**
|
| 176 |
+
* Response error for HTTP responses rejected because of their status code.
|
| 177 |
+
*
|
| 178 |
+
* @category errors
|
| 179 |
* @since 4.0.0
|
|
|
|
| 180 |
*/
|
| 181 |
export class StatusCodeError extends Data.TaggedError("StatusCodeError")<{
|
| 182 |
readonly request: HttpClientRequest.HttpClientRequest
|
|
|
|
| 185 |
readonly description?: string | undefined
|
| 186 |
}> {
|
| 187 |
/**
|
| 188 |
+
* Formats the request method and URL for status code error messages.
|
| 189 |
+
*
|
| 190 |
* @since 4.0.0
|
| 191 |
*/
|
| 192 |
get methodAndUrl() {
|
|
|
|
| 194 |
}
|
| 195 |
|
| 196 |
/**
|
| 197 |
+
* Builds the status code error message from the response status, optional description, and request details.
|
| 198 |
+
*
|
| 199 |
* @since 4.0.0
|
| 200 |
*/
|
| 201 |
override get message() {
|
|
|
|
| 205 |
}
|
| 206 |
|
| 207 |
/**
|
| 208 |
+
* Response error for failures while decoding an HTTP response body.
|
| 209 |
+
*
|
| 210 |
+
* @category errors
|
| 211 |
* @since 4.0.0
|
|
|
|
| 212 |
*/
|
| 213 |
export class DecodeError extends Data.TaggedError("DecodeError")<{
|
| 214 |
readonly request: HttpClientRequest.HttpClientRequest
|
|
|
|
| 217 |
readonly description?: string | undefined
|
| 218 |
}> {
|
| 219 |
/**
|
| 220 |
+
* Formats the request method and URL for response decoding error messages.
|
| 221 |
+
*
|
| 222 |
* @since 4.0.0
|
| 223 |
*/
|
| 224 |
get methodAndUrl() {
|
|
|
|
| 226 |
}
|
| 227 |
|
| 228 |
/**
|
| 229 |
+
* Builds the response decoding error message from the response status, optional description, and request details.
|
| 230 |
+
*
|
| 231 |
* @since 4.0.0
|
| 232 |
*/
|
| 233 |
override get message() {
|
|
|
|
| 237 |
}
|
| 238 |
|
| 239 |
/**
|
| 240 |
+
* Response error for operations that expected a response body but received an empty body.
|
| 241 |
+
*
|
| 242 |
+
* @category errors
|
| 243 |
* @since 4.0.0
|
|
|
|
| 244 |
*/
|
| 245 |
export class EmptyBodyError extends Data.TaggedError("EmptyBodyError")<{
|
| 246 |
readonly request: HttpClientRequest.HttpClientRequest
|
|
|
|
| 249 |
readonly description?: string | undefined
|
| 250 |
}> {
|
| 251 |
/**
|
| 252 |
+
* Formats the request method and URL for empty response body error messages.
|
| 253 |
+
*
|
| 254 |
* @since 4.0.0
|
| 255 |
*/
|
| 256 |
get methodAndUrl() {
|
|
|
|
| 258 |
}
|
| 259 |
|
| 260 |
/**
|
| 261 |
+
* Builds the empty body error message from the response status, optional description, and request details.
|
| 262 |
+
*
|
| 263 |
* @since 4.0.0
|
| 264 |
*/
|
| 265 |
override get message() {
|
|
|
|
| 269 |
}
|
| 270 |
|
| 271 |
/**
|
| 272 |
+
* Union of HTTP client errors that occur before a response is available.
|
| 273 |
+
*
|
| 274 |
+
* @category errors
|
| 275 |
* @since 4.0.0
|
|
|
|
| 276 |
*/
|
| 277 |
export type RequestError = TransportError | EncodeError | InvalidUrlError
|
| 278 |
|
| 279 |
/**
|
| 280 |
+
* Union of HTTP client errors that include an HTTP response.
|
| 281 |
+
*
|
| 282 |
+
* @category errors
|
| 283 |
* @since 4.0.0
|
|
|
|
| 284 |
*/
|
| 285 |
export type ResponseError = StatusCodeError | DecodeError | EmptyBodyError
|
| 286 |
|
| 287 |
/**
|
| 288 |
+
* Union of all specific failure reasons carried by `HttpClientError`.
|
| 289 |
+
*
|
| 290 |
+
* @category errors
|
| 291 |
* @since 4.0.0
|
|
|
|
| 292 |
*/
|
| 293 |
export type HttpClientErrorReason = RequestError | ResponseError
|
| 294 |
|
| 295 |
/**
|
| 296 |
+
* Schema for serializable HTTP client errors, preserving the specific error kind
|
| 297 |
+
* and cause.
|
| 298 |
+
*
|
| 299 |
+
* @category schemas
|
| 300 |
* @since 4.0.0
|
|
|
|
| 301 |
*/
|
| 302 |
export class HttpClientErrorSchema extends Schema.ErrorClass<HttpClientErrorSchema>(TypeId)({
|
| 303 |
_tag: Schema.tag("HttpError"),
|
|
|
|
| 311 |
"EmptyBodyError"
|
| 312 |
] satisfies ReadonlyArray<HttpClientErrorReason["_tag"]>
|
| 313 |
),
|
| 314 |
+
cause: Schema.optional(Schema.Defect())
|
| 315 |
}) {
|
| 316 |
/**
|
| 317 |
+
* Builds the serializable schema representation for an HTTP client error.
|
| 318 |
+
*
|
| 319 |
* @since 4.0.0
|
| 320 |
*/
|
| 321 |
static fromHttpClientError(error: HttpClientError): HttpClientErrorSchema {
|
.opencode/node_modules/effect/src/unstable/http/HttpClientRequest.ts
CHANGED
|
@@ -1,4 +1,12 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Context from "../../Context.ts"
|
|
@@ -25,14 +33,18 @@ import * as UrlParams from "./UrlParams.ts"
|
|
| 25 |
const TypeId = "~effect/http/HttpClientRequest"
|
| 26 |
|
| 27 |
/**
|
|
|
|
|
|
|
|
|
|
| 28 |
* @since 4.0.0
|
| 29 |
-
* @category Guards
|
| 30 |
*/
|
| 31 |
export const isHttpClientRequest = (u: unknown): u is HttpClientRequest => hasProperty(u, TypeId)
|
| 32 |
|
| 33 |
/**
|
| 34 |
-
*
|
|
|
|
| 35 |
* @category models
|
|
|
|
| 36 |
*/
|
| 37 |
export interface HttpClientRequest extends Inspectable.Inspectable, Pipeable {
|
| 38 |
readonly [TypeId]: typeof TypeId
|
|
@@ -45,8 +57,10 @@ export interface HttpClientRequest extends Inspectable.Inspectable, Pipeable {
|
|
| 45 |
}
|
| 46 |
|
| 47 |
/**
|
|
|
|
|
|
|
|
|
|
| 48 |
* @since 4.0.0
|
| 49 |
-
* @category models
|
| 50 |
*/
|
| 51 |
export interface Options {
|
| 52 |
readonly method?: HttpMethod | undefined
|
|
@@ -60,12 +74,16 @@ export interface Options {
|
|
| 60 |
}
|
| 61 |
|
| 62 |
/**
|
|
|
|
|
|
|
| 63 |
* @since 4.0.0
|
| 64 |
*/
|
| 65 |
export declare namespace Options {
|
| 66 |
/**
|
|
|
|
|
|
|
|
|
|
| 67 |
* @since 4.0.0
|
| 68 |
-
* @category models
|
| 69 |
*/
|
| 70 |
export interface NoUrl extends Omit<Options, "method" | "url"> {}
|
| 71 |
}
|
|
@@ -90,8 +108,10 @@ const Proto = {
|
|
| 90 |
}
|
| 91 |
|
| 92 |
/**
|
| 93 |
-
*
|
|
|
|
| 94 |
* @category constructors
|
|
|
|
| 95 |
*/
|
| 96 |
export function makeWith(
|
| 97 |
method: HttpMethod,
|
|
@@ -112,8 +132,10 @@ export function makeWith(
|
|
| 112 |
}
|
| 113 |
|
| 114 |
/**
|
| 115 |
-
*
|
|
|
|
| 116 |
* @category constructors
|
|
|
|
| 117 |
*/
|
| 118 |
export const empty: HttpClientRequest = makeWith(
|
| 119 |
"GET",
|
|
@@ -125,8 +147,10 @@ export const empty: HttpClientRequest = makeWith(
|
|
| 125 |
)
|
| 126 |
|
| 127 |
/**
|
| 128 |
-
*
|
|
|
|
| 129 |
* @category constructors
|
|
|
|
| 130 |
*/
|
| 131 |
export const make = <M extends HttpMethod>(
|
| 132 |
method: M
|
|
@@ -142,26 +166,34 @@ export const make = <M extends HttpMethod>(
|
|
| 142 |
})
|
| 143 |
|
| 144 |
/**
|
| 145 |
-
*
|
|
|
|
| 146 |
* @category constructors
|
|
|
|
| 147 |
*/
|
| 148 |
export const get: (url: string | URL, options?: Options.NoUrl) => HttpClientRequest = make("GET")
|
| 149 |
|
| 150 |
/**
|
| 151 |
-
*
|
|
|
|
| 152 |
* @category constructors
|
|
|
|
| 153 |
*/
|
| 154 |
export const post: (url: string | URL, options?: Options.NoUrl) => HttpClientRequest = make("POST")
|
| 155 |
|
| 156 |
/**
|
| 157 |
-
*
|
|
|
|
| 158 |
* @category constructors
|
|
|
|
| 159 |
*/
|
| 160 |
export const patch: (url: string | URL, options?: Options.NoUrl) => HttpClientRequest = make("PATCH")
|
| 161 |
|
| 162 |
/**
|
| 163 |
-
*
|
|
|
|
| 164 |
* @category constructors
|
|
|
|
| 165 |
*/
|
| 166 |
export const put: (url: string | URL, options?: Options.NoUrl) => HttpClientRequest = make("PUT")
|
| 167 |
|
|
@@ -169,43 +201,57 @@ const del: (url: string | URL, options?: Options.NoUrl) => HttpClientRequest = m
|
|
| 169 |
|
| 170 |
export {
|
| 171 |
/**
|
| 172 |
-
*
|
|
|
|
| 173 |
* @category constructors
|
|
|
|
| 174 |
*/
|
| 175 |
del as delete
|
| 176 |
}
|
| 177 |
|
| 178 |
/**
|
| 179 |
-
*
|
|
|
|
| 180 |
* @category constructors
|
|
|
|
| 181 |
*/
|
| 182 |
export const head: (url: string | URL, options?: Options.NoUrl) => HttpClientRequest = make("HEAD")
|
| 183 |
|
| 184 |
/**
|
| 185 |
-
*
|
|
|
|
| 186 |
* @category constructors
|
|
|
|
| 187 |
*/
|
| 188 |
export const options: (url: string | URL, options?: Options.NoUrl) => HttpClientRequest = make("OPTIONS")
|
| 189 |
|
| 190 |
/**
|
| 191 |
-
*
|
|
|
|
| 192 |
* @category constructors
|
|
|
|
| 193 |
*/
|
| 194 |
export const trace: (url: string | URL, options?: Options.NoUrl) => HttpClientRequest = make("TRACE")
|
| 195 |
|
| 196 |
/**
|
| 197 |
-
*
|
|
|
|
| 198 |
* @category combinators
|
|
|
|
| 199 |
*/
|
| 200 |
export const modify: {
|
| 201 |
/**
|
| 202 |
-
*
|
|
|
|
| 203 |
* @category combinators
|
|
|
|
| 204 |
*/
|
| 205 |
(options: Options): (self: HttpClientRequest) => HttpClientRequest
|
| 206 |
/**
|
| 207 |
-
*
|
|
|
|
| 208 |
* @category combinators
|
|
|
|
| 209 |
*/
|
| 210 |
(self: HttpClientRequest, options: Options): HttpClientRequest
|
| 211 |
} = dual(2, (self: HttpClientRequest, options: Options): HttpClientRequest => {
|
|
@@ -240,18 +286,24 @@ export const modify: {
|
|
| 240 |
})
|
| 241 |
|
| 242 |
/**
|
| 243 |
-
*
|
|
|
|
| 244 |
* @category combinators
|
|
|
|
| 245 |
*/
|
| 246 |
export const setMethod: {
|
| 247 |
/**
|
| 248 |
-
*
|
|
|
|
| 249 |
* @category combinators
|
|
|
|
| 250 |
*/
|
| 251 |
(method: HttpMethod): (self: HttpClientRequest) => HttpClientRequest
|
| 252 |
/**
|
| 253 |
-
*
|
|
|
|
| 254 |
* @category combinators
|
|
|
|
| 255 |
*/
|
| 256 |
(self: HttpClientRequest, method: HttpMethod): HttpClientRequest
|
| 257 |
} = dual(
|
|
@@ -261,18 +313,24 @@ export const setMethod: {
|
|
| 261 |
)
|
| 262 |
|
| 263 |
/**
|
| 264 |
-
*
|
|
|
|
| 265 |
* @category combinators
|
|
|
|
| 266 |
*/
|
| 267 |
export const setHeader: {
|
| 268 |
/**
|
| 269 |
-
*
|
|
|
|
| 270 |
* @category combinators
|
|
|
|
| 271 |
*/
|
| 272 |
(key: string, value: string): (self: HttpClientRequest) => HttpClientRequest
|
| 273 |
/**
|
| 274 |
-
*
|
|
|
|
| 275 |
* @category combinators
|
|
|
|
| 276 |
*/
|
| 277 |
(self: HttpClientRequest, key: string, value: string): HttpClientRequest
|
| 278 |
} = dual(3, (self: HttpClientRequest, key: string, value: string): HttpClientRequest =>
|
|
@@ -286,18 +344,24 @@ export const setHeader: {
|
|
| 286 |
))
|
| 287 |
|
| 288 |
/**
|
| 289 |
-
*
|
|
|
|
| 290 |
* @category combinators
|
|
|
|
| 291 |
*/
|
| 292 |
export const setHeaders: {
|
| 293 |
/**
|
| 294 |
-
*
|
|
|
|
| 295 |
* @category combinators
|
|
|
|
| 296 |
*/
|
| 297 |
(input: Headers.Input): (self: HttpClientRequest) => HttpClientRequest
|
| 298 |
/**
|
| 299 |
-
*
|
|
|
|
| 300 |
* @category combinators
|
|
|
|
| 301 |
*/
|
| 302 |
(self: HttpClientRequest, input: Headers.Input): HttpClientRequest
|
| 303 |
} = dual(2, (self: HttpClientRequest, input: Headers.Input): HttpClientRequest =>
|
|
@@ -311,18 +375,24 @@ export const setHeaders: {
|
|
| 311 |
))
|
| 312 |
|
| 313 |
/**
|
| 314 |
-
*
|
|
|
|
| 315 |
* @category combinators
|
|
|
|
| 316 |
*/
|
| 317 |
export const basicAuth: {
|
| 318 |
/**
|
| 319 |
-
*
|
|
|
|
| 320 |
* @category combinators
|
|
|
|
| 321 |
*/
|
| 322 |
(username: string | Redacted.Redacted, password: string | Redacted.Redacted): (self: HttpClientRequest) => HttpClientRequest
|
| 323 |
/**
|
| 324 |
-
*
|
|
|
|
| 325 |
* @category combinators
|
|
|
|
| 326 |
*/
|
| 327 |
(
|
| 328 |
self: HttpClientRequest,
|
|
@@ -340,18 +410,24 @@ export const basicAuth: {
|
|
| 340 |
)
|
| 341 |
|
| 342 |
/**
|
| 343 |
-
*
|
|
|
|
| 344 |
* @category combinators
|
|
|
|
| 345 |
*/
|
| 346 |
export const bearerToken: {
|
| 347 |
/**
|
| 348 |
-
*
|
|
|
|
| 349 |
* @category combinators
|
|
|
|
| 350 |
*/
|
| 351 |
(token: string | Redacted.Redacted): (self: HttpClientRequest) => HttpClientRequest
|
| 352 |
/**
|
| 353 |
-
*
|
|
|
|
| 354 |
* @category combinators
|
|
|
|
| 355 |
*/
|
| 356 |
(self: HttpClientRequest, token: string | Redacted.Redacted): HttpClientRequest
|
| 357 |
} = dual(
|
|
@@ -361,41 +437,55 @@ export const bearerToken: {
|
|
| 361 |
)
|
| 362 |
|
| 363 |
/**
|
| 364 |
-
*
|
|
|
|
| 365 |
* @category combinators
|
|
|
|
| 366 |
*/
|
| 367 |
export const accept: {
|
| 368 |
/**
|
| 369 |
-
*
|
|
|
|
| 370 |
* @category combinators
|
|
|
|
| 371 |
*/
|
| 372 |
(mediaType: string): (self: HttpClientRequest) => HttpClientRequest
|
| 373 |
/**
|
| 374 |
-
*
|
|
|
|
| 375 |
* @category combinators
|
|
|
|
| 376 |
*/
|
| 377 |
(self: HttpClientRequest, mediaType: string): HttpClientRequest
|
| 378 |
} = dual(2, (self: HttpClientRequest, mediaType: string): HttpClientRequest => setHeader(self, "Accept", mediaType))
|
| 379 |
|
| 380 |
/**
|
| 381 |
-
*
|
|
|
|
| 382 |
* @category combinators
|
|
|
|
| 383 |
*/
|
| 384 |
export const acceptJson: (self: HttpClientRequest) => HttpClientRequest = accept("application/json")
|
| 385 |
|
| 386 |
/**
|
| 387 |
-
*
|
|
|
|
| 388 |
* @category combinators
|
|
|
|
| 389 |
*/
|
| 390 |
export const setUrl: {
|
| 391 |
/**
|
| 392 |
-
*
|
|
|
|
| 393 |
* @category combinators
|
|
|
|
| 394 |
*/
|
| 395 |
(url: string | URL): (self: HttpClientRequest) => HttpClientRequest
|
| 396 |
/**
|
| 397 |
-
*
|
|
|
|
| 398 |
* @category combinators
|
|
|
|
| 399 |
*/
|
| 400 |
(self: HttpClientRequest, url: string | URL): HttpClientRequest
|
| 401 |
} = dual(2, (self: HttpClientRequest, url: string | URL): HttpClientRequest => {
|
|
@@ -425,18 +515,24 @@ export const setUrl: {
|
|
| 425 |
})
|
| 426 |
|
| 427 |
/**
|
| 428 |
-
*
|
|
|
|
| 429 |
* @category combinators
|
|
|
|
| 430 |
*/
|
| 431 |
export const prependUrl: {
|
| 432 |
/**
|
| 433 |
-
*
|
|
|
|
| 434 |
* @category combinators
|
|
|
|
| 435 |
*/
|
| 436 |
(path: string): (self: HttpClientRequest) => HttpClientRequest
|
| 437 |
/**
|
| 438 |
-
*
|
|
|
|
| 439 |
* @category combinators
|
|
|
|
| 440 |
*/
|
| 441 |
(self: HttpClientRequest, path: string): HttpClientRequest
|
| 442 |
} = dual(2, (self: HttpClientRequest, path: string): HttpClientRequest => {
|
|
@@ -452,18 +548,24 @@ export const prependUrl: {
|
|
| 452 |
})
|
| 453 |
|
| 454 |
/**
|
| 455 |
-
*
|
|
|
|
| 456 |
* @category combinators
|
|
|
|
| 457 |
*/
|
| 458 |
export const appendUrl: {
|
| 459 |
/**
|
| 460 |
-
*
|
|
|
|
| 461 |
* @category combinators
|
|
|
|
| 462 |
*/
|
| 463 |
(path: string): (self: HttpClientRequest) => HttpClientRequest
|
| 464 |
/**
|
| 465 |
-
*
|
|
|
|
| 466 |
* @category combinators
|
|
|
|
| 467 |
*/
|
| 468 |
(self: HttpClientRequest, path: string): HttpClientRequest
|
| 469 |
} = dual(2, (self: HttpClientRequest, path: string): HttpClientRequest => {
|
|
@@ -491,18 +593,24 @@ const joinSegments = (first: string, second: string): string => {
|
|
| 491 |
}
|
| 492 |
|
| 493 |
/**
|
| 494 |
-
*
|
|
|
|
| 495 |
* @category combinators
|
|
|
|
| 496 |
*/
|
| 497 |
export const updateUrl: {
|
| 498 |
/**
|
| 499 |
-
*
|
|
|
|
| 500 |
* @category combinators
|
|
|
|
| 501 |
*/
|
| 502 |
(f: (url: string) => string): (self: HttpClientRequest) => HttpClientRequest
|
| 503 |
/**
|
| 504 |
-
*
|
|
|
|
| 505 |
* @category combinators
|
|
|
|
| 506 |
*/
|
| 507 |
(self: HttpClientRequest, f: (url: string) => string): HttpClientRequest
|
| 508 |
} = dual(2, (self: HttpClientRequest, f: (url: string) => string): HttpClientRequest =>
|
|
@@ -516,18 +624,24 @@ export const updateUrl: {
|
|
| 516 |
))
|
| 517 |
|
| 518 |
/**
|
| 519 |
-
*
|
|
|
|
| 520 |
* @category combinators
|
|
|
|
| 521 |
*/
|
| 522 |
export const setUrlParam: {
|
| 523 |
/**
|
| 524 |
-
*
|
|
|
|
| 525 |
* @category combinators
|
|
|
|
| 526 |
*/
|
| 527 |
(key: string, value: string): (self: HttpClientRequest) => HttpClientRequest
|
| 528 |
/**
|
| 529 |
-
*
|
|
|
|
| 530 |
* @category combinators
|
|
|
|
| 531 |
*/
|
| 532 |
(self: HttpClientRequest, key: string, value: string): HttpClientRequest
|
| 533 |
} = dual(3, (self: HttpClientRequest, key: string, value: string): HttpClientRequest =>
|
|
@@ -541,18 +655,24 @@ export const setUrlParam: {
|
|
| 541 |
))
|
| 542 |
|
| 543 |
/**
|
| 544 |
-
*
|
|
|
|
| 545 |
* @category combinators
|
|
|
|
| 546 |
*/
|
| 547 |
export const setUrlParams: {
|
| 548 |
/**
|
| 549 |
-
*
|
|
|
|
| 550 |
* @category combinators
|
|
|
|
| 551 |
*/
|
| 552 |
(input: UrlParams.Input): (self: HttpClientRequest) => HttpClientRequest
|
| 553 |
/**
|
| 554 |
-
*
|
|
|
|
| 555 |
* @category combinators
|
|
|
|
| 556 |
*/
|
| 557 |
(self: HttpClientRequest, input: UrlParams.Input): HttpClientRequest
|
| 558 |
} = dual(2, (self: HttpClientRequest, input: UrlParams.Input): HttpClientRequest =>
|
|
@@ -566,18 +686,24 @@ export const setUrlParams: {
|
|
| 566 |
))
|
| 567 |
|
| 568 |
/**
|
| 569 |
-
*
|
|
|
|
| 570 |
* @category combinators
|
|
|
|
| 571 |
*/
|
| 572 |
export const appendUrlParam: {
|
| 573 |
/**
|
| 574 |
-
*
|
|
|
|
| 575 |
* @category combinators
|
|
|
|
| 576 |
*/
|
| 577 |
(key: string, value: string): (self: HttpClientRequest) => HttpClientRequest
|
| 578 |
/**
|
| 579 |
-
*
|
|
|
|
| 580 |
* @category combinators
|
|
|
|
| 581 |
*/
|
| 582 |
(self: HttpClientRequest, key: string, value: string): HttpClientRequest
|
| 583 |
} = dual(3, (self: HttpClientRequest, key: string, value: string): HttpClientRequest =>
|
|
@@ -591,18 +717,24 @@ export const appendUrlParam: {
|
|
| 591 |
))
|
| 592 |
|
| 593 |
/**
|
| 594 |
-
*
|
|
|
|
| 595 |
* @category combinators
|
|
|
|
| 596 |
*/
|
| 597 |
export const appendUrlParams: {
|
| 598 |
/**
|
| 599 |
-
*
|
|
|
|
| 600 |
* @category combinators
|
|
|
|
| 601 |
*/
|
| 602 |
(input: UrlParams.Input): (self: HttpClientRequest) => HttpClientRequest
|
| 603 |
/**
|
| 604 |
-
*
|
|
|
|
| 605 |
* @category combinators
|
|
|
|
| 606 |
*/
|
| 607 |
(self: HttpClientRequest, input: UrlParams.Input): HttpClientRequest
|
| 608 |
} = dual(2, (self: HttpClientRequest, input: UrlParams.Input): HttpClientRequest =>
|
|
@@ -616,18 +748,24 @@ export const appendUrlParams: {
|
|
| 616 |
))
|
| 617 |
|
| 618 |
/**
|
| 619 |
-
*
|
|
|
|
| 620 |
* @category combinators
|
|
|
|
| 621 |
*/
|
| 622 |
export const setHash: {
|
| 623 |
/**
|
| 624 |
-
*
|
|
|
|
| 625 |
* @category combinators
|
|
|
|
| 626 |
*/
|
| 627 |
(hash: string): (self: HttpClientRequest) => HttpClientRequest
|
| 628 |
/**
|
| 629 |
-
*
|
|
|
|
| 630 |
* @category combinators
|
|
|
|
| 631 |
*/
|
| 632 |
(self: HttpClientRequest, hash: string): HttpClientRequest
|
| 633 |
} = dual(2, (self: HttpClientRequest, hash: string): HttpClientRequest =>
|
|
@@ -641,8 +779,10 @@ export const setHash: {
|
|
| 641 |
))
|
| 642 |
|
| 643 |
/**
|
| 644 |
-
*
|
|
|
|
| 645 |
* @category combinators
|
|
|
|
| 646 |
*/
|
| 647 |
export const removeHash = (self: HttpClientRequest): HttpClientRequest =>
|
| 648 |
makeWith(
|
|
@@ -655,18 +795,24 @@ export const removeHash = (self: HttpClientRequest): HttpClientRequest =>
|
|
| 655 |
)
|
| 656 |
|
| 657 |
/**
|
| 658 |
-
*
|
|
|
|
| 659 |
* @category combinators
|
|
|
|
| 660 |
*/
|
| 661 |
export const setBody: {
|
| 662 |
/**
|
| 663 |
-
*
|
|
|
|
| 664 |
* @category combinators
|
|
|
|
| 665 |
*/
|
| 666 |
(body: HttpBody.HttpBody): (self: HttpClientRequest) => HttpClientRequest
|
| 667 |
/**
|
| 668 |
-
*
|
|
|
|
| 669 |
* @category combinators
|
|
|
|
| 670 |
*/
|
| 671 |
(self: HttpClientRequest, body: HttpBody.HttpBody): HttpClientRequest
|
| 672 |
} = dual(2, (self: HttpClientRequest, body: HttpBody.HttpBody): HttpClientRequest => {
|
|
@@ -692,18 +838,24 @@ export const setBody: {
|
|
| 692 |
})
|
| 693 |
|
| 694 |
/**
|
| 695 |
-
*
|
|
|
|
| 696 |
* @category combinators
|
|
|
|
| 697 |
*/
|
| 698 |
export const bodyUint8Array: {
|
| 699 |
/**
|
| 700 |
-
*
|
|
|
|
| 701 |
* @category combinators
|
|
|
|
| 702 |
*/
|
| 703 |
(body: Uint8Array, contentType?: string): (self: HttpClientRequest) => HttpClientRequest
|
| 704 |
/**
|
| 705 |
-
*
|
|
|
|
| 706 |
* @category combinators
|
|
|
|
| 707 |
*/
|
| 708 |
(self: HttpClientRequest, body: Uint8Array, contentType?: string): HttpClientRequest
|
| 709 |
} = dual(
|
|
@@ -713,18 +865,24 @@ export const bodyUint8Array: {
|
|
| 713 |
)
|
| 714 |
|
| 715 |
/**
|
| 716 |
-
*
|
|
|
|
| 717 |
* @category combinators
|
|
|
|
| 718 |
*/
|
| 719 |
export const bodyText: {
|
| 720 |
/**
|
| 721 |
-
*
|
|
|
|
| 722 |
* @category combinators
|
|
|
|
| 723 |
*/
|
| 724 |
(body: string, contentType?: string): (self: HttpClientRequest) => HttpClientRequest
|
| 725 |
/**
|
| 726 |
-
*
|
|
|
|
| 727 |
* @category combinators
|
|
|
|
| 728 |
*/
|
| 729 |
(self: HttpClientRequest, body: string, contentType?: string): HttpClientRequest
|
| 730 |
} = dual(
|
|
@@ -734,18 +892,24 @@ export const bodyText: {
|
|
| 734 |
)
|
| 735 |
|
| 736 |
/**
|
| 737 |
-
*
|
|
|
|
| 738 |
* @category combinators
|
|
|
|
| 739 |
*/
|
| 740 |
export const bodyJson: {
|
| 741 |
/**
|
| 742 |
-
*
|
|
|
|
| 743 |
* @category combinators
|
|
|
|
| 744 |
*/
|
| 745 |
(body: unknown): (self: HttpClientRequest) => Effect.Effect<HttpClientRequest, HttpBody.HttpBodyError>
|
| 746 |
/**
|
| 747 |
-
*
|
|
|
|
| 748 |
* @category combinators
|
|
|
|
| 749 |
*/
|
| 750 |
(self: HttpClientRequest, body: unknown): Effect.Effect<HttpClientRequest, HttpBody.HttpBodyError>
|
| 751 |
} = dual(
|
|
@@ -755,25 +919,60 @@ export const bodyJson: {
|
|
| 755 |
)
|
| 756 |
|
| 757 |
/**
|
| 758 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 759 |
* @category combinators
|
|
|
|
| 760 |
*/
|
| 761 |
export const bodyJsonUnsafe: {
|
| 762 |
/**
|
| 763 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 764 |
* @category combinators
|
|
|
|
| 765 |
*/
|
| 766 |
(body: unknown): (self: HttpClientRequest) => HttpClientRequest
|
| 767 |
/**
|
| 768 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 769 |
* @category combinators
|
|
|
|
| 770 |
*/
|
| 771 |
(self: HttpClientRequest, body: unknown): HttpClientRequest
|
| 772 |
} = dual(2, (self: HttpClientRequest, body: unknown): HttpClientRequest => setBody(self, HttpBody.jsonUnsafe(body)))
|
| 773 |
|
| 774 |
/**
|
| 775 |
-
*
|
|
|
|
| 776 |
* @category combinators
|
|
|
|
| 777 |
*/
|
| 778 |
export const schemaBodyJson = <S extends Schema.Top>(
|
| 779 |
schema: S,
|
|
@@ -801,18 +1000,24 @@ export const schemaBodyJson = <S extends Schema.Top>(
|
|
| 801 |
}
|
| 802 |
|
| 803 |
/**
|
| 804 |
-
*
|
|
|
|
| 805 |
* @category combinators
|
|
|
|
| 806 |
*/
|
| 807 |
export const bodyUrlParams: {
|
| 808 |
/**
|
| 809 |
-
*
|
|
|
|
| 810 |
* @category combinators
|
|
|
|
| 811 |
*/
|
| 812 |
(input: UrlParams.Input): (self: HttpClientRequest) => HttpClientRequest
|
| 813 |
/**
|
| 814 |
-
*
|
|
|
|
| 815 |
* @category combinators
|
|
|
|
| 816 |
*/
|
| 817 |
(self: HttpClientRequest, input: UrlParams.Input): HttpClientRequest
|
| 818 |
} = dual(
|
|
@@ -822,35 +1027,47 @@ export const bodyUrlParams: {
|
|
| 822 |
)
|
| 823 |
|
| 824 |
/**
|
| 825 |
-
*
|
|
|
|
| 826 |
* @category combinators
|
|
|
|
| 827 |
*/
|
| 828 |
export const bodyFormData: {
|
| 829 |
/**
|
| 830 |
-
*
|
|
|
|
| 831 |
* @category combinators
|
|
|
|
| 832 |
*/
|
| 833 |
(body: FormData): (self: HttpClientRequest) => HttpClientRequest
|
| 834 |
/**
|
| 835 |
-
*
|
|
|
|
| 836 |
* @category combinators
|
|
|
|
| 837 |
*/
|
| 838 |
(self: HttpClientRequest, body: FormData): HttpClientRequest
|
| 839 |
} = dual(2, (self: HttpClientRequest, body: FormData): HttpClientRequest => setBody(self, HttpBody.formData(body)))
|
| 840 |
|
| 841 |
/**
|
| 842 |
-
*
|
|
|
|
| 843 |
* @category combinators
|
|
|
|
| 844 |
*/
|
| 845 |
export const bodyFormDataRecord: {
|
| 846 |
/**
|
| 847 |
-
*
|
|
|
|
| 848 |
* @category combinators
|
|
|
|
| 849 |
*/
|
| 850 |
(entries: HttpBody.FormDataInput): (self: HttpClientRequest) => HttpClientRequest
|
| 851 |
/**
|
| 852 |
-
*
|
|
|
|
| 853 |
* @category combinators
|
|
|
|
| 854 |
*/
|
| 855 |
(self: HttpClientRequest, entries: HttpBody.FormDataInput): HttpClientRequest
|
| 856 |
} = dual(
|
|
@@ -860,21 +1077,27 @@ export const bodyFormDataRecord: {
|
|
| 860 |
)
|
| 861 |
|
| 862 |
/**
|
| 863 |
-
*
|
|
|
|
| 864 |
* @category combinators
|
|
|
|
| 865 |
*/
|
| 866 |
export const bodyStream: {
|
| 867 |
/**
|
| 868 |
-
*
|
|
|
|
| 869 |
* @category combinators
|
|
|
|
| 870 |
*/
|
| 871 |
(
|
| 872 |
body: Stream.Stream<Uint8Array, unknown>,
|
| 873 |
options?: { readonly contentType?: string | undefined; readonly contentLength?: number | undefined } | undefined
|
| 874 |
): (self: HttpClientRequest) => HttpClientRequest
|
| 875 |
/**
|
| 876 |
-
*
|
|
|
|
| 877 |
* @category combinators
|
|
|
|
| 878 |
*/
|
| 879 |
(
|
| 880 |
self: HttpClientRequest,
|
|
@@ -895,13 +1118,17 @@ export const bodyStream: {
|
|
| 895 |
)
|
| 896 |
|
| 897 |
/**
|
| 898 |
-
*
|
|
|
|
| 899 |
* @category combinators
|
|
|
|
| 900 |
*/
|
| 901 |
export const bodyFile: {
|
| 902 |
/**
|
| 903 |
-
*
|
|
|
|
| 904 |
* @category combinators
|
|
|
|
| 905 |
*/
|
| 906 |
(
|
| 907 |
path: string,
|
|
@@ -913,8 +1140,10 @@ export const bodyFile: {
|
|
| 913 |
}
|
| 914 |
): (self: HttpClientRequest) => Effect.Effect<HttpClientRequest, PlatformError.PlatformError, FileSystem.FileSystem>
|
| 915 |
/**
|
| 916 |
-
*
|
|
|
|
| 917 |
* @category combinators
|
|
|
|
| 918 |
*/
|
| 919 |
(
|
| 920 |
self: HttpClientRequest,
|
|
@@ -945,8 +1174,10 @@ export const bodyFile: {
|
|
| 945 |
)
|
| 946 |
|
| 947 |
/**
|
| 948 |
-
*
|
|
|
|
| 949 |
* @category combinators
|
|
|
|
| 950 |
*/
|
| 951 |
export function toUrl(self: HttpClientRequest): Option.Option<URL> {
|
| 952 |
const r = UrlParams.makeUrl(self.url, self.urlParams, Option.getOrUndefined(self.hash))
|
|
@@ -957,8 +1188,10 @@ export function toUrl(self: HttpClientRequest): Option.Option<URL> {
|
|
| 957 |
}
|
| 958 |
|
| 959 |
/**
|
|
|
|
|
|
|
|
|
|
| 960 |
* @since 4.0.0
|
| 961 |
-
* @category conversions
|
| 962 |
*/
|
| 963 |
export const fromWeb = (request: globalThis.Request): HttpClientRequest => {
|
| 964 |
const method = request.method.toUpperCase() as HttpMethod
|
|
@@ -989,8 +1222,10 @@ const parseContentLength = (contentLength: string | null): number | undefined =>
|
|
| 989 |
}
|
| 990 |
|
| 991 |
/**
|
|
|
|
|
|
|
|
|
|
| 992 |
* @since 4.0.0
|
| 993 |
-
* @category conversions
|
| 994 |
*/
|
| 995 |
export const toWebResult = (self: HttpClientRequest, options?: {
|
| 996 |
readonly signal?: AbortSignal | undefined
|
|
@@ -1044,15 +1279,17 @@ const isReadableStream = (u: unknown): u is ReadableStream<Uint8Array> =>
|
|
| 1044 |
typeof ReadableStream !== "undefined" && u instanceof ReadableStream
|
| 1045 |
|
| 1046 |
/**
|
|
|
|
|
|
|
|
|
|
| 1047 |
* @since 4.0.0
|
| 1048 |
-
* @category conversions
|
| 1049 |
*/
|
| 1050 |
export const toWeb = (self: HttpClientRequest, options?: {
|
| 1051 |
readonly signal?: AbortSignal | undefined
|
| 1052 |
}): Effect.Effect<Request, UrlParams.UrlParamsError> =>
|
| 1053 |
Effect.contextWith((context) =>
|
| 1054 |
-
toWebResult(self, {
|
| 1055 |
context: context,
|
| 1056 |
signal: options?.signal
|
| 1057 |
-
})
|
| 1058 |
)
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Describes immutable outgoing HTTP client requests.
|
| 3 |
+
*
|
| 4 |
+
* `HttpClientRequest` is the request model shared by Effect HTTP clients and
|
| 5 |
+
* platform adapters. A request stores its method, URL, query parameters, hash,
|
| 6 |
+
* headers, and body as structured data. This module includes constructors,
|
| 7 |
+
* helpers for updating requests, body encoders for common payloads, and
|
| 8 |
+
* conversions to and from Web `Request` values.
|
| 9 |
+
*
|
| 10 |
* @since 4.0.0
|
| 11 |
*/
|
| 12 |
import * as Context from "../../Context.ts"
|
|
|
|
| 33 |
const TypeId = "~effect/http/HttpClientRequest"
|
| 34 |
|
| 35 |
/**
|
| 36 |
+
* Returns `true` when a value is an `HttpClientRequest`.
|
| 37 |
+
*
|
| 38 |
+
* @category guards
|
| 39 |
* @since 4.0.0
|
|
|
|
| 40 |
*/
|
| 41 |
export const isHttpClientRequest = (u: unknown): u is HttpClientRequest => hasProperty(u, TypeId)
|
| 42 |
|
| 43 |
/**
|
| 44 |
+
* Immutable model of an outgoing HTTP client request, including its method, URL components, headers, and body.
|
| 45 |
+
*
|
| 46 |
* @category models
|
| 47 |
+
* @since 4.0.0
|
| 48 |
*/
|
| 49 |
export interface HttpClientRequest extends Inspectable.Inspectable, Pipeable {
|
| 50 |
readonly [TypeId]: typeof TypeId
|
|
|
|
| 57 |
}
|
| 58 |
|
| 59 |
/**
|
| 60 |
+
* Options for constructing or modifying an `HttpClientRequest`.
|
| 61 |
+
*
|
| 62 |
+
* @category options
|
| 63 |
* @since 4.0.0
|
|
|
|
| 64 |
*/
|
| 65 |
export interface Options {
|
| 66 |
readonly method?: HttpMethod | undefined
|
|
|
|
| 74 |
}
|
| 75 |
|
| 76 |
/**
|
| 77 |
+
* Namespace containing option types associated with `HttpClientRequest` construction.
|
| 78 |
+
*
|
| 79 |
* @since 4.0.0
|
| 80 |
*/
|
| 81 |
export declare namespace Options {
|
| 82 |
/**
|
| 83 |
+
* Request options that omit the method and URL for helpers that already receive those values separately.
|
| 84 |
+
*
|
| 85 |
+
* @category options
|
| 86 |
* @since 4.0.0
|
|
|
|
| 87 |
*/
|
| 88 |
export interface NoUrl extends Omit<Options, "method" | "url"> {}
|
| 89 |
}
|
|
|
|
| 108 |
}
|
| 109 |
|
| 110 |
/**
|
| 111 |
+
* Constructs an `HttpClientRequest` from fully normalized request components.
|
| 112 |
+
*
|
| 113 |
* @category constructors
|
| 114 |
+
* @since 4.0.0
|
| 115 |
*/
|
| 116 |
export function makeWith(
|
| 117 |
method: HttpMethod,
|
|
|
|
| 132 |
}
|
| 133 |
|
| 134 |
/**
|
| 135 |
+
* An empty `GET` request with no URL, query parameters, hash, headers, or body.
|
| 136 |
+
*
|
| 137 |
* @category constructors
|
| 138 |
+
* @since 4.0.0
|
| 139 |
*/
|
| 140 |
export const empty: HttpClientRequest = makeWith(
|
| 141 |
"GET",
|
|
|
|
| 147 |
)
|
| 148 |
|
| 149 |
/**
|
| 150 |
+
* Creates a request constructor for the specified HTTP method.
|
| 151 |
+
*
|
| 152 |
* @category constructors
|
| 153 |
+
* @since 4.0.0
|
| 154 |
*/
|
| 155 |
export const make = <M extends HttpMethod>(
|
| 156 |
method: M
|
|
|
|
| 166 |
})
|
| 167 |
|
| 168 |
/**
|
| 169 |
+
* Creates a `GET` request for the specified URL.
|
| 170 |
+
*
|
| 171 |
* @category constructors
|
| 172 |
+
* @since 4.0.0
|
| 173 |
*/
|
| 174 |
export const get: (url: string | URL, options?: Options.NoUrl) => HttpClientRequest = make("GET")
|
| 175 |
|
| 176 |
/**
|
| 177 |
+
* Creates a `POST` request for the specified URL.
|
| 178 |
+
*
|
| 179 |
* @category constructors
|
| 180 |
+
* @since 4.0.0
|
| 181 |
*/
|
| 182 |
export const post: (url: string | URL, options?: Options.NoUrl) => HttpClientRequest = make("POST")
|
| 183 |
|
| 184 |
/**
|
| 185 |
+
* Creates a `PATCH` request for the specified URL.
|
| 186 |
+
*
|
| 187 |
* @category constructors
|
| 188 |
+
* @since 4.0.0
|
| 189 |
*/
|
| 190 |
export const patch: (url: string | URL, options?: Options.NoUrl) => HttpClientRequest = make("PATCH")
|
| 191 |
|
| 192 |
/**
|
| 193 |
+
* Creates a `PUT` request for the specified URL.
|
| 194 |
+
*
|
| 195 |
* @category constructors
|
| 196 |
+
* @since 4.0.0
|
| 197 |
*/
|
| 198 |
export const put: (url: string | URL, options?: Options.NoUrl) => HttpClientRequest = make("PUT")
|
| 199 |
|
|
|
|
| 201 |
|
| 202 |
export {
|
| 203 |
/**
|
| 204 |
+
* Creates a `DELETE` request for the specified URL.
|
| 205 |
+
*
|
| 206 |
* @category constructors
|
| 207 |
+
* @since 4.0.0
|
| 208 |
*/
|
| 209 |
del as delete
|
| 210 |
}
|
| 211 |
|
| 212 |
/**
|
| 213 |
+
* Creates a `HEAD` request for the specified URL.
|
| 214 |
+
*
|
| 215 |
* @category constructors
|
| 216 |
+
* @since 4.0.0
|
| 217 |
*/
|
| 218 |
export const head: (url: string | URL, options?: Options.NoUrl) => HttpClientRequest = make("HEAD")
|
| 219 |
|
| 220 |
/**
|
| 221 |
+
* Creates an `OPTIONS` request for the specified URL.
|
| 222 |
+
*
|
| 223 |
* @category constructors
|
| 224 |
+
* @since 4.0.0
|
| 225 |
*/
|
| 226 |
export const options: (url: string | URL, options?: Options.NoUrl) => HttpClientRequest = make("OPTIONS")
|
| 227 |
|
| 228 |
/**
|
| 229 |
+
* Creates a `TRACE` request for the specified URL.
|
| 230 |
+
*
|
| 231 |
* @category constructors
|
| 232 |
+
* @since 4.0.0
|
| 233 |
*/
|
| 234 |
export const trace: (url: string | URL, options?: Options.NoUrl) => HttpClientRequest = make("TRACE")
|
| 235 |
|
| 236 |
/**
|
| 237 |
+
* Applies request options to an `HttpClientRequest`, returning a new request.
|
| 238 |
+
*
|
| 239 |
* @category combinators
|
| 240 |
+
* @since 4.0.0
|
| 241 |
*/
|
| 242 |
export const modify: {
|
| 243 |
/**
|
| 244 |
+
* Applies request options to an `HttpClientRequest`, returning a new request.
|
| 245 |
+
*
|
| 246 |
* @category combinators
|
| 247 |
+
* @since 4.0.0
|
| 248 |
*/
|
| 249 |
(options: Options): (self: HttpClientRequest) => HttpClientRequest
|
| 250 |
/**
|
| 251 |
+
* Applies request options to an `HttpClientRequest`, returning a new request.
|
| 252 |
+
*
|
| 253 |
* @category combinators
|
| 254 |
+
* @since 4.0.0
|
| 255 |
*/
|
| 256 |
(self: HttpClientRequest, options: Options): HttpClientRequest
|
| 257 |
} = dual(2, (self: HttpClientRequest, options: Options): HttpClientRequest => {
|
|
|
|
| 286 |
})
|
| 287 |
|
| 288 |
/**
|
| 289 |
+
* Sets the HTTP method on a request, returning a new request.
|
| 290 |
+
*
|
| 291 |
* @category combinators
|
| 292 |
+
* @since 4.0.0
|
| 293 |
*/
|
| 294 |
export const setMethod: {
|
| 295 |
/**
|
| 296 |
+
* Sets the HTTP method on a request, returning a new request.
|
| 297 |
+
*
|
| 298 |
* @category combinators
|
| 299 |
+
* @since 4.0.0
|
| 300 |
*/
|
| 301 |
(method: HttpMethod): (self: HttpClientRequest) => HttpClientRequest
|
| 302 |
/**
|
| 303 |
+
* Sets the HTTP method on a request, returning a new request.
|
| 304 |
+
*
|
| 305 |
* @category combinators
|
| 306 |
+
* @since 4.0.0
|
| 307 |
*/
|
| 308 |
(self: HttpClientRequest, method: HttpMethod): HttpClientRequest
|
| 309 |
} = dual(
|
|
|
|
| 313 |
)
|
| 314 |
|
| 315 |
/**
|
| 316 |
+
* Sets a single request header, replacing any existing value for that header.
|
| 317 |
+
*
|
| 318 |
* @category combinators
|
| 319 |
+
* @since 4.0.0
|
| 320 |
*/
|
| 321 |
export const setHeader: {
|
| 322 |
/**
|
| 323 |
+
* Sets a single request header, replacing any existing value for that header.
|
| 324 |
+
*
|
| 325 |
* @category combinators
|
| 326 |
+
* @since 4.0.0
|
| 327 |
*/
|
| 328 |
(key: string, value: string): (self: HttpClientRequest) => HttpClientRequest
|
| 329 |
/**
|
| 330 |
+
* Sets a single request header, replacing any existing value for that header.
|
| 331 |
+
*
|
| 332 |
* @category combinators
|
| 333 |
+
* @since 4.0.0
|
| 334 |
*/
|
| 335 |
(self: HttpClientRequest, key: string, value: string): HttpClientRequest
|
| 336 |
} = dual(3, (self: HttpClientRequest, key: string, value: string): HttpClientRequest =>
|
|
|
|
| 344 |
))
|
| 345 |
|
| 346 |
/**
|
| 347 |
+
* Sets multiple request headers from an input collection, replacing existing values with matching names.
|
| 348 |
+
*
|
| 349 |
* @category combinators
|
| 350 |
+
* @since 4.0.0
|
| 351 |
*/
|
| 352 |
export const setHeaders: {
|
| 353 |
/**
|
| 354 |
+
* Sets multiple request headers from an input collection, replacing existing values with matching names.
|
| 355 |
+
*
|
| 356 |
* @category combinators
|
| 357 |
+
* @since 4.0.0
|
| 358 |
*/
|
| 359 |
(input: Headers.Input): (self: HttpClientRequest) => HttpClientRequest
|
| 360 |
/**
|
| 361 |
+
* Sets multiple request headers from an input collection, replacing existing values with matching names.
|
| 362 |
+
*
|
| 363 |
* @category combinators
|
| 364 |
+
* @since 4.0.0
|
| 365 |
*/
|
| 366 |
(self: HttpClientRequest, input: Headers.Input): HttpClientRequest
|
| 367 |
} = dual(2, (self: HttpClientRequest, input: Headers.Input): HttpClientRequest =>
|
|
|
|
| 375 |
))
|
| 376 |
|
| 377 |
/**
|
| 378 |
+
* Sets the `Authorization` header using HTTP Basic authentication credentials.
|
| 379 |
+
*
|
| 380 |
* @category combinators
|
| 381 |
+
* @since 4.0.0
|
| 382 |
*/
|
| 383 |
export const basicAuth: {
|
| 384 |
/**
|
| 385 |
+
* Sets the `Authorization` header using HTTP Basic authentication credentials.
|
| 386 |
+
*
|
| 387 |
* @category combinators
|
| 388 |
+
* @since 4.0.0
|
| 389 |
*/
|
| 390 |
(username: string | Redacted.Redacted, password: string | Redacted.Redacted): (self: HttpClientRequest) => HttpClientRequest
|
| 391 |
/**
|
| 392 |
+
* Sets the `Authorization` header using HTTP Basic authentication credentials.
|
| 393 |
+
*
|
| 394 |
* @category combinators
|
| 395 |
+
* @since 4.0.0
|
| 396 |
*/
|
| 397 |
(
|
| 398 |
self: HttpClientRequest,
|
|
|
|
| 410 |
)
|
| 411 |
|
| 412 |
/**
|
| 413 |
+
* Sets the `Authorization` header using a bearer token.
|
| 414 |
+
*
|
| 415 |
* @category combinators
|
| 416 |
+
* @since 4.0.0
|
| 417 |
*/
|
| 418 |
export const bearerToken: {
|
| 419 |
/**
|
| 420 |
+
* Sets the `Authorization` header using a bearer token.
|
| 421 |
+
*
|
| 422 |
* @category combinators
|
| 423 |
+
* @since 4.0.0
|
| 424 |
*/
|
| 425 |
(token: string | Redacted.Redacted): (self: HttpClientRequest) => HttpClientRequest
|
| 426 |
/**
|
| 427 |
+
* Sets the `Authorization` header using a bearer token.
|
| 428 |
+
*
|
| 429 |
* @category combinators
|
| 430 |
+
* @since 4.0.0
|
| 431 |
*/
|
| 432 |
(self: HttpClientRequest, token: string | Redacted.Redacted): HttpClientRequest
|
| 433 |
} = dual(
|
|
|
|
| 437 |
)
|
| 438 |
|
| 439 |
/**
|
| 440 |
+
* Sets the `Accept` header to the specified media type.
|
| 441 |
+
*
|
| 442 |
* @category combinators
|
| 443 |
+
* @since 4.0.0
|
| 444 |
*/
|
| 445 |
export const accept: {
|
| 446 |
/**
|
| 447 |
+
* Sets the `Accept` header to the specified media type.
|
| 448 |
+
*
|
| 449 |
* @category combinators
|
| 450 |
+
* @since 4.0.0
|
| 451 |
*/
|
| 452 |
(mediaType: string): (self: HttpClientRequest) => HttpClientRequest
|
| 453 |
/**
|
| 454 |
+
* Sets the `Accept` header to the specified media type.
|
| 455 |
+
*
|
| 456 |
* @category combinators
|
| 457 |
+
* @since 4.0.0
|
| 458 |
*/
|
| 459 |
(self: HttpClientRequest, mediaType: string): HttpClientRequest
|
| 460 |
} = dual(2, (self: HttpClientRequest, mediaType: string): HttpClientRequest => setHeader(self, "Accept", mediaType))
|
| 461 |
|
| 462 |
/**
|
| 463 |
+
* Sets the `Accept` header to `application/json`.
|
| 464 |
+
*
|
| 465 |
* @category combinators
|
| 466 |
+
* @since 4.0.0
|
| 467 |
*/
|
| 468 |
export const acceptJson: (self: HttpClientRequest) => HttpClientRequest = accept("application/json")
|
| 469 |
|
| 470 |
/**
|
| 471 |
+
* Sets the request URL. When given a `URL`, its search parameters and hash are extracted into the request's structured fields.
|
| 472 |
+
*
|
| 473 |
* @category combinators
|
| 474 |
+
* @since 4.0.0
|
| 475 |
*/
|
| 476 |
export const setUrl: {
|
| 477 |
/**
|
| 478 |
+
* Sets the request URL. When given a `URL`, its search parameters and hash are extracted into the request's structured fields.
|
| 479 |
+
*
|
| 480 |
* @category combinators
|
| 481 |
+
* @since 4.0.0
|
| 482 |
*/
|
| 483 |
(url: string | URL): (self: HttpClientRequest) => HttpClientRequest
|
| 484 |
/**
|
| 485 |
+
* Sets the request URL. When given a `URL`, its search parameters and hash are extracted into the request's structured fields.
|
| 486 |
+
*
|
| 487 |
* @category combinators
|
| 488 |
+
* @since 4.0.0
|
| 489 |
*/
|
| 490 |
(self: HttpClientRequest, url: string | URL): HttpClientRequest
|
| 491 |
} = dual(2, (self: HttpClientRequest, url: string | URL): HttpClientRequest => {
|
|
|
|
| 515 |
})
|
| 516 |
|
| 517 |
/**
|
| 518 |
+
* Prepends a URL segment to the request URL, inserting or trimming one slash as needed.
|
| 519 |
+
*
|
| 520 |
* @category combinators
|
| 521 |
+
* @since 4.0.0
|
| 522 |
*/
|
| 523 |
export const prependUrl: {
|
| 524 |
/**
|
| 525 |
+
* Prepends a URL segment to the request URL, inserting or trimming one slash as needed.
|
| 526 |
+
*
|
| 527 |
* @category combinators
|
| 528 |
+
* @since 4.0.0
|
| 529 |
*/
|
| 530 |
(path: string): (self: HttpClientRequest) => HttpClientRequest
|
| 531 |
/**
|
| 532 |
+
* Prepends a URL segment to the request URL, inserting or trimming one slash as needed.
|
| 533 |
+
*
|
| 534 |
* @category combinators
|
| 535 |
+
* @since 4.0.0
|
| 536 |
*/
|
| 537 |
(self: HttpClientRequest, path: string): HttpClientRequest
|
| 538 |
} = dual(2, (self: HttpClientRequest, path: string): HttpClientRequest => {
|
|
|
|
| 548 |
})
|
| 549 |
|
| 550 |
/**
|
| 551 |
+
* Appends a URL segment to the request URL, inserting or trimming one slash as needed.
|
| 552 |
+
*
|
| 553 |
* @category combinators
|
| 554 |
+
* @since 4.0.0
|
| 555 |
*/
|
| 556 |
export const appendUrl: {
|
| 557 |
/**
|
| 558 |
+
* Appends a URL segment to the request URL, inserting or trimming one slash as needed.
|
| 559 |
+
*
|
| 560 |
* @category combinators
|
| 561 |
+
* @since 4.0.0
|
| 562 |
*/
|
| 563 |
(path: string): (self: HttpClientRequest) => HttpClientRequest
|
| 564 |
/**
|
| 565 |
+
* Appends a URL segment to the request URL, inserting or trimming one slash as needed.
|
| 566 |
+
*
|
| 567 |
* @category combinators
|
| 568 |
+
* @since 4.0.0
|
| 569 |
*/
|
| 570 |
(self: HttpClientRequest, path: string): HttpClientRequest
|
| 571 |
} = dual(2, (self: HttpClientRequest, path: string): HttpClientRequest => {
|
|
|
|
| 593 |
}
|
| 594 |
|
| 595 |
/**
|
| 596 |
+
* Updates the request URL by applying a function to the current URL string.
|
| 597 |
+
*
|
| 598 |
* @category combinators
|
| 599 |
+
* @since 4.0.0
|
| 600 |
*/
|
| 601 |
export const updateUrl: {
|
| 602 |
/**
|
| 603 |
+
* Updates the request URL by applying a function to the current URL string.
|
| 604 |
+
*
|
| 605 |
* @category combinators
|
| 606 |
+
* @since 4.0.0
|
| 607 |
*/
|
| 608 |
(f: (url: string) => string): (self: HttpClientRequest) => HttpClientRequest
|
| 609 |
/**
|
| 610 |
+
* Updates the request URL by applying a function to the current URL string.
|
| 611 |
+
*
|
| 612 |
* @category combinators
|
| 613 |
+
* @since 4.0.0
|
| 614 |
*/
|
| 615 |
(self: HttpClientRequest, f: (url: string) => string): HttpClientRequest
|
| 616 |
} = dual(2, (self: HttpClientRequest, f: (url: string) => string): HttpClientRequest =>
|
|
|
|
| 624 |
))
|
| 625 |
|
| 626 |
/**
|
| 627 |
+
* Sets one query parameter, replacing existing values for that parameter name.
|
| 628 |
+
*
|
| 629 |
* @category combinators
|
| 630 |
+
* @since 4.0.0
|
| 631 |
*/
|
| 632 |
export const setUrlParam: {
|
| 633 |
/**
|
| 634 |
+
* Sets one query parameter, replacing existing values for that parameter name.
|
| 635 |
+
*
|
| 636 |
* @category combinators
|
| 637 |
+
* @since 4.0.0
|
| 638 |
*/
|
| 639 |
(key: string, value: string): (self: HttpClientRequest) => HttpClientRequest
|
| 640 |
/**
|
| 641 |
+
* Sets one query parameter, replacing existing values for that parameter name.
|
| 642 |
+
*
|
| 643 |
* @category combinators
|
| 644 |
+
* @since 4.0.0
|
| 645 |
*/
|
| 646 |
(self: HttpClientRequest, key: string, value: string): HttpClientRequest
|
| 647 |
} = dual(3, (self: HttpClientRequest, key: string, value: string): HttpClientRequest =>
|
|
|
|
| 655 |
))
|
| 656 |
|
| 657 |
/**
|
| 658 |
+
* Sets query parameters from an input collection, replacing existing values for matching names.
|
| 659 |
+
*
|
| 660 |
* @category combinators
|
| 661 |
+
* @since 4.0.0
|
| 662 |
*/
|
| 663 |
export const setUrlParams: {
|
| 664 |
/**
|
| 665 |
+
* Sets query parameters from an input collection, replacing existing values for matching names.
|
| 666 |
+
*
|
| 667 |
* @category combinators
|
| 668 |
+
* @since 4.0.0
|
| 669 |
*/
|
| 670 |
(input: UrlParams.Input): (self: HttpClientRequest) => HttpClientRequest
|
| 671 |
/**
|
| 672 |
+
* Sets query parameters from an input collection, replacing existing values for matching names.
|
| 673 |
+
*
|
| 674 |
* @category combinators
|
| 675 |
+
* @since 4.0.0
|
| 676 |
*/
|
| 677 |
(self: HttpClientRequest, input: UrlParams.Input): HttpClientRequest
|
| 678 |
} = dual(2, (self: HttpClientRequest, input: UrlParams.Input): HttpClientRequest =>
|
|
|
|
| 686 |
))
|
| 687 |
|
| 688 |
/**
|
| 689 |
+
* Appends one query parameter value without removing existing values for the same name.
|
| 690 |
+
*
|
| 691 |
* @category combinators
|
| 692 |
+
* @since 4.0.0
|
| 693 |
*/
|
| 694 |
export const appendUrlParam: {
|
| 695 |
/**
|
| 696 |
+
* Appends one query parameter value without removing existing values for the same name.
|
| 697 |
+
*
|
| 698 |
* @category combinators
|
| 699 |
+
* @since 4.0.0
|
| 700 |
*/
|
| 701 |
(key: string, value: string): (self: HttpClientRequest) => HttpClientRequest
|
| 702 |
/**
|
| 703 |
+
* Appends one query parameter value without removing existing values for the same name.
|
| 704 |
+
*
|
| 705 |
* @category combinators
|
| 706 |
+
* @since 4.0.0
|
| 707 |
*/
|
| 708 |
(self: HttpClientRequest, key: string, value: string): HttpClientRequest
|
| 709 |
} = dual(3, (self: HttpClientRequest, key: string, value: string): HttpClientRequest =>
|
|
|
|
| 717 |
))
|
| 718 |
|
| 719 |
/**
|
| 720 |
+
* Appends query parameters from an input collection without removing existing values for matching names.
|
| 721 |
+
*
|
| 722 |
* @category combinators
|
| 723 |
+
* @since 4.0.0
|
| 724 |
*/
|
| 725 |
export const appendUrlParams: {
|
| 726 |
/**
|
| 727 |
+
* Appends query parameters from an input collection without removing existing values for matching names.
|
| 728 |
+
*
|
| 729 |
* @category combinators
|
| 730 |
+
* @since 4.0.0
|
| 731 |
*/
|
| 732 |
(input: UrlParams.Input): (self: HttpClientRequest) => HttpClientRequest
|
| 733 |
/**
|
| 734 |
+
* Appends query parameters from an input collection without removing existing values for matching names.
|
| 735 |
+
*
|
| 736 |
* @category combinators
|
| 737 |
+
* @since 4.0.0
|
| 738 |
*/
|
| 739 |
(self: HttpClientRequest, input: UrlParams.Input): HttpClientRequest
|
| 740 |
} = dual(2, (self: HttpClientRequest, input: UrlParams.Input): HttpClientRequest =>
|
|
|
|
| 748 |
))
|
| 749 |
|
| 750 |
/**
|
| 751 |
+
* Sets the URL fragment on a request without the leading `#`.
|
| 752 |
+
*
|
| 753 |
* @category combinators
|
| 754 |
+
* @since 4.0.0
|
| 755 |
*/
|
| 756 |
export const setHash: {
|
| 757 |
/**
|
| 758 |
+
* Sets the URL fragment on a request without the leading `#`.
|
| 759 |
+
*
|
| 760 |
* @category combinators
|
| 761 |
+
* @since 4.0.0
|
| 762 |
*/
|
| 763 |
(hash: string): (self: HttpClientRequest) => HttpClientRequest
|
| 764 |
/**
|
| 765 |
+
* Sets the URL fragment on a request without the leading `#`.
|
| 766 |
+
*
|
| 767 |
* @category combinators
|
| 768 |
+
* @since 4.0.0
|
| 769 |
*/
|
| 770 |
(self: HttpClientRequest, hash: string): HttpClientRequest
|
| 771 |
} = dual(2, (self: HttpClientRequest, hash: string): HttpClientRequest =>
|
|
|
|
| 779 |
))
|
| 780 |
|
| 781 |
/**
|
| 782 |
+
* Removes the URL fragment from a request.
|
| 783 |
+
*
|
| 784 |
* @category combinators
|
| 785 |
+
* @since 4.0.0
|
| 786 |
*/
|
| 787 |
export const removeHash = (self: HttpClientRequest): HttpClientRequest =>
|
| 788 |
makeWith(
|
|
|
|
| 795 |
)
|
| 796 |
|
| 797 |
/**
|
| 798 |
+
* Sets the request body and updates `Content-Type` and `Content-Length` headers from the body metadata when available.
|
| 799 |
+
*
|
| 800 |
* @category combinators
|
| 801 |
+
* @since 4.0.0
|
| 802 |
*/
|
| 803 |
export const setBody: {
|
| 804 |
/**
|
| 805 |
+
* Sets the request body and updates `Content-Type` and `Content-Length` headers from the body metadata when available.
|
| 806 |
+
*
|
| 807 |
* @category combinators
|
| 808 |
+
* @since 4.0.0
|
| 809 |
*/
|
| 810 |
(body: HttpBody.HttpBody): (self: HttpClientRequest) => HttpClientRequest
|
| 811 |
/**
|
| 812 |
+
* Sets the request body and updates `Content-Type` and `Content-Length` headers from the body metadata when available.
|
| 813 |
+
*
|
| 814 |
* @category combinators
|
| 815 |
+
* @since 4.0.0
|
| 816 |
*/
|
| 817 |
(self: HttpClientRequest, body: HttpBody.HttpBody): HttpClientRequest
|
| 818 |
} = dual(2, (self: HttpClientRequest, body: HttpBody.HttpBody): HttpClientRequest => {
|
|
|
|
| 838 |
})
|
| 839 |
|
| 840 |
/**
|
| 841 |
+
* Sets a `Uint8Array` request body with an optional content type.
|
| 842 |
+
*
|
| 843 |
* @category combinators
|
| 844 |
+
* @since 4.0.0
|
| 845 |
*/
|
| 846 |
export const bodyUint8Array: {
|
| 847 |
/**
|
| 848 |
+
* Sets a `Uint8Array` request body with an optional content type.
|
| 849 |
+
*
|
| 850 |
* @category combinators
|
| 851 |
+
* @since 4.0.0
|
| 852 |
*/
|
| 853 |
(body: Uint8Array, contentType?: string): (self: HttpClientRequest) => HttpClientRequest
|
| 854 |
/**
|
| 855 |
+
* Sets a `Uint8Array` request body with an optional content type.
|
| 856 |
+
*
|
| 857 |
* @category combinators
|
| 858 |
+
* @since 4.0.0
|
| 859 |
*/
|
| 860 |
(self: HttpClientRequest, body: Uint8Array, contentType?: string): HttpClientRequest
|
| 861 |
} = dual(
|
|
|
|
| 865 |
)
|
| 866 |
|
| 867 |
/**
|
| 868 |
+
* Sets a text request body with an optional content type.
|
| 869 |
+
*
|
| 870 |
* @category combinators
|
| 871 |
+
* @since 4.0.0
|
| 872 |
*/
|
| 873 |
export const bodyText: {
|
| 874 |
/**
|
| 875 |
+
* Sets a text request body with an optional content type.
|
| 876 |
+
*
|
| 877 |
* @category combinators
|
| 878 |
+
* @since 4.0.0
|
| 879 |
*/
|
| 880 |
(body: string, contentType?: string): (self: HttpClientRequest) => HttpClientRequest
|
| 881 |
/**
|
| 882 |
+
* Sets a text request body with an optional content type.
|
| 883 |
+
*
|
| 884 |
* @category combinators
|
| 885 |
+
* @since 4.0.0
|
| 886 |
*/
|
| 887 |
(self: HttpClientRequest, body: string, contentType?: string): HttpClientRequest
|
| 888 |
} = dual(
|
|
|
|
| 892 |
)
|
| 893 |
|
| 894 |
/**
|
| 895 |
+
* Encodes a value as a JSON request body and sets it on the request, failing with `HttpBodyError` if encoding fails.
|
| 896 |
+
*
|
| 897 |
* @category combinators
|
| 898 |
+
* @since 4.0.0
|
| 899 |
*/
|
| 900 |
export const bodyJson: {
|
| 901 |
/**
|
| 902 |
+
* Encodes a value as a JSON request body and sets it on the request, failing with `HttpBodyError` if encoding fails.
|
| 903 |
+
*
|
| 904 |
* @category combinators
|
| 905 |
+
* @since 4.0.0
|
| 906 |
*/
|
| 907 |
(body: unknown): (self: HttpClientRequest) => Effect.Effect<HttpClientRequest, HttpBody.HttpBodyError>
|
| 908 |
/**
|
| 909 |
+
* Encodes a value as a JSON request body and sets it on the request, failing with `HttpBodyError` if encoding fails.
|
| 910 |
+
*
|
| 911 |
* @category combinators
|
| 912 |
+
* @since 4.0.0
|
| 913 |
*/
|
| 914 |
(self: HttpClientRequest, body: unknown): Effect.Effect<HttpClientRequest, HttpBody.HttpBodyError>
|
| 915 |
} = dual(
|
|
|
|
| 919 |
)
|
| 920 |
|
| 921 |
/**
|
| 922 |
+
* Sets a JSON request body using unsafe JSON encoding.
|
| 923 |
+
*
|
| 924 |
+
* **When to use**
|
| 925 |
+
*
|
| 926 |
+
* Use when the request body is known to be JSON-serializable and a synchronous
|
| 927 |
+
* `HttpClientRequest` result is needed.
|
| 928 |
+
*
|
| 929 |
+
* **Gotchas**
|
| 930 |
+
*
|
| 931 |
+
* JSON encoding may throw instead of failing in the Effect error channel.
|
| 932 |
+
*
|
| 933 |
* @category combinators
|
| 934 |
+
* @since 4.0.0
|
| 935 |
*/
|
| 936 |
export const bodyJsonUnsafe: {
|
| 937 |
/**
|
| 938 |
+
* Sets a JSON request body using unsafe JSON encoding.
|
| 939 |
+
*
|
| 940 |
+
* **When to use**
|
| 941 |
+
*
|
| 942 |
+
* Use when the request body is known to be JSON-serializable and a synchronous
|
| 943 |
+
* `HttpClientRequest` result is needed.
|
| 944 |
+
*
|
| 945 |
+
* **Gotchas**
|
| 946 |
+
*
|
| 947 |
+
* JSON encoding may throw instead of failing in the Effect error channel.
|
| 948 |
+
*
|
| 949 |
* @category combinators
|
| 950 |
+
* @since 4.0.0
|
| 951 |
*/
|
| 952 |
(body: unknown): (self: HttpClientRequest) => HttpClientRequest
|
| 953 |
/**
|
| 954 |
+
* Sets a JSON request body using unsafe JSON encoding.
|
| 955 |
+
*
|
| 956 |
+
* **When to use**
|
| 957 |
+
*
|
| 958 |
+
* Use when the request body is known to be JSON-serializable and a synchronous
|
| 959 |
+
* `HttpClientRequest` result is needed.
|
| 960 |
+
*
|
| 961 |
+
* **Gotchas**
|
| 962 |
+
*
|
| 963 |
+
* JSON encoding may throw instead of failing in the Effect error channel.
|
| 964 |
+
*
|
| 965 |
* @category combinators
|
| 966 |
+
* @since 4.0.0
|
| 967 |
*/
|
| 968 |
(self: HttpClientRequest, body: unknown): HttpClientRequest
|
| 969 |
} = dual(2, (self: HttpClientRequest, body: unknown): HttpClientRequest => setBody(self, HttpBody.jsonUnsafe(body)))
|
| 970 |
|
| 971 |
/**
|
| 972 |
+
* Creates a schema-based JSON body encoder that sets the encoded value on a request.
|
| 973 |
+
*
|
| 974 |
* @category combinators
|
| 975 |
+
* @since 4.0.0
|
| 976 |
*/
|
| 977 |
export const schemaBodyJson = <S extends Schema.Top>(
|
| 978 |
schema: S,
|
|
|
|
| 1000 |
}
|
| 1001 |
|
| 1002 |
/**
|
| 1003 |
+
* Sets an `application/x-www-form-urlencoded` request body from URL parameter input.
|
| 1004 |
+
*
|
| 1005 |
* @category combinators
|
| 1006 |
+
* @since 4.0.0
|
| 1007 |
*/
|
| 1008 |
export const bodyUrlParams: {
|
| 1009 |
/**
|
| 1010 |
+
* Sets an `application/x-www-form-urlencoded` request body from URL parameter input.
|
| 1011 |
+
*
|
| 1012 |
* @category combinators
|
| 1013 |
+
* @since 4.0.0
|
| 1014 |
*/
|
| 1015 |
(input: UrlParams.Input): (self: HttpClientRequest) => HttpClientRequest
|
| 1016 |
/**
|
| 1017 |
+
* Sets an `application/x-www-form-urlencoded` request body from URL parameter input.
|
| 1018 |
+
*
|
| 1019 |
* @category combinators
|
| 1020 |
+
* @since 4.0.0
|
| 1021 |
*/
|
| 1022 |
(self: HttpClientRequest, input: UrlParams.Input): HttpClientRequest
|
| 1023 |
} = dual(
|
|
|
|
| 1027 |
)
|
| 1028 |
|
| 1029 |
/**
|
| 1030 |
+
* Sets a `FormData` request body.
|
| 1031 |
+
*
|
| 1032 |
* @category combinators
|
| 1033 |
+
* @since 4.0.0
|
| 1034 |
*/
|
| 1035 |
export const bodyFormData: {
|
| 1036 |
/**
|
| 1037 |
+
* Sets a `FormData` request body.
|
| 1038 |
+
*
|
| 1039 |
* @category combinators
|
| 1040 |
+
* @since 4.0.0
|
| 1041 |
*/
|
| 1042 |
(body: FormData): (self: HttpClientRequest) => HttpClientRequest
|
| 1043 |
/**
|
| 1044 |
+
* Sets a `FormData` request body.
|
| 1045 |
+
*
|
| 1046 |
* @category combinators
|
| 1047 |
+
* @since 4.0.0
|
| 1048 |
*/
|
| 1049 |
(self: HttpClientRequest, body: FormData): HttpClientRequest
|
| 1050 |
} = dual(2, (self: HttpClientRequest, body: FormData): HttpClientRequest => setBody(self, HttpBody.formData(body)))
|
| 1051 |
|
| 1052 |
/**
|
| 1053 |
+
* Creates a `FormData` request body from record-style entries and sets it on the request.
|
| 1054 |
+
*
|
| 1055 |
* @category combinators
|
| 1056 |
+
* @since 4.0.0
|
| 1057 |
*/
|
| 1058 |
export const bodyFormDataRecord: {
|
| 1059 |
/**
|
| 1060 |
+
* Creates a `FormData` request body from record-style entries and sets it on the request.
|
| 1061 |
+
*
|
| 1062 |
* @category combinators
|
| 1063 |
+
* @since 4.0.0
|
| 1064 |
*/
|
| 1065 |
(entries: HttpBody.FormDataInput): (self: HttpClientRequest) => HttpClientRequest
|
| 1066 |
/**
|
| 1067 |
+
* Creates a `FormData` request body from record-style entries and sets it on the request.
|
| 1068 |
+
*
|
| 1069 |
* @category combinators
|
| 1070 |
+
* @since 4.0.0
|
| 1071 |
*/
|
| 1072 |
(self: HttpClientRequest, entries: HttpBody.FormDataInput): HttpClientRequest
|
| 1073 |
} = dual(
|
|
|
|
| 1077 |
)
|
| 1078 |
|
| 1079 |
/**
|
| 1080 |
+
* Sets a streaming `Uint8Array` request body with optional content type and content length metadata.
|
| 1081 |
+
*
|
| 1082 |
* @category combinators
|
| 1083 |
+
* @since 4.0.0
|
| 1084 |
*/
|
| 1085 |
export const bodyStream: {
|
| 1086 |
/**
|
| 1087 |
+
* Sets a streaming `Uint8Array` request body with optional content type and content length metadata.
|
| 1088 |
+
*
|
| 1089 |
* @category combinators
|
| 1090 |
+
* @since 4.0.0
|
| 1091 |
*/
|
| 1092 |
(
|
| 1093 |
body: Stream.Stream<Uint8Array, unknown>,
|
| 1094 |
options?: { readonly contentType?: string | undefined; readonly contentLength?: number | undefined } | undefined
|
| 1095 |
): (self: HttpClientRequest) => HttpClientRequest
|
| 1096 |
/**
|
| 1097 |
+
* Sets a streaming `Uint8Array` request body with optional content type and content length metadata.
|
| 1098 |
+
*
|
| 1099 |
* @category combinators
|
| 1100 |
+
* @since 4.0.0
|
| 1101 |
*/
|
| 1102 |
(
|
| 1103 |
self: HttpClientRequest,
|
|
|
|
| 1118 |
)
|
| 1119 |
|
| 1120 |
/**
|
| 1121 |
+
* Creates a file-backed request body from a filesystem path and sets it on the request.
|
| 1122 |
+
*
|
| 1123 |
* @category combinators
|
| 1124 |
+
* @since 4.0.0
|
| 1125 |
*/
|
| 1126 |
export const bodyFile: {
|
| 1127 |
/**
|
| 1128 |
+
* Creates a file-backed request body from a filesystem path and sets it on the request.
|
| 1129 |
+
*
|
| 1130 |
* @category combinators
|
| 1131 |
+
* @since 4.0.0
|
| 1132 |
*/
|
| 1133 |
(
|
| 1134 |
path: string,
|
|
|
|
| 1140 |
}
|
| 1141 |
): (self: HttpClientRequest) => Effect.Effect<HttpClientRequest, PlatformError.PlatformError, FileSystem.FileSystem>
|
| 1142 |
/**
|
| 1143 |
+
* Creates a file-backed request body from a filesystem path and sets it on the request.
|
| 1144 |
+
*
|
| 1145 |
* @category combinators
|
| 1146 |
+
* @since 4.0.0
|
| 1147 |
*/
|
| 1148 |
(
|
| 1149 |
self: HttpClientRequest,
|
|
|
|
| 1174 |
)
|
| 1175 |
|
| 1176 |
/**
|
| 1177 |
+
* Builds a `URL` from the request URL, query parameters, and hash, returning `Option.none()` if the URL is invalid.
|
| 1178 |
+
*
|
| 1179 |
* @category combinators
|
| 1180 |
+
* @since 4.0.0
|
| 1181 |
*/
|
| 1182 |
export function toUrl(self: HttpClientRequest): Option.Option<URL> {
|
| 1183 |
const r = UrlParams.makeUrl(self.url, self.urlParams, Option.getOrUndefined(self.hash))
|
|
|
|
| 1188 |
}
|
| 1189 |
|
| 1190 |
/**
|
| 1191 |
+
* Converts a Web `Request` into an `HttpClientRequest`, preserving method, URL, headers, and supported request bodies.
|
| 1192 |
+
*
|
| 1193 |
+
* @category converting
|
| 1194 |
* @since 4.0.0
|
|
|
|
| 1195 |
*/
|
| 1196 |
export const fromWeb = (request: globalThis.Request): HttpClientRequest => {
|
| 1197 |
const method = request.method.toUpperCase() as HttpMethod
|
|
|
|
| 1222 |
}
|
| 1223 |
|
| 1224 |
/**
|
| 1225 |
+
* Converts an `HttpClientRequest` safely to a Web `Request` as a `Result`, failing when the request URL is invalid.
|
| 1226 |
+
*
|
| 1227 |
+
* @category converting
|
| 1228 |
* @since 4.0.0
|
|
|
|
| 1229 |
*/
|
| 1230 |
export const toWebResult = (self: HttpClientRequest, options?: {
|
| 1231 |
readonly signal?: AbortSignal | undefined
|
|
|
|
| 1279 |
typeof ReadableStream !== "undefined" && u instanceof ReadableStream
|
| 1280 |
|
| 1281 |
/**
|
| 1282 |
+
* Converts an `HttpClientRequest` to a Web `Request`, failing with `UrlParamsError` when the request URL is invalid.
|
| 1283 |
+
*
|
| 1284 |
+
* @category converting
|
| 1285 |
* @since 4.0.0
|
|
|
|
| 1286 |
*/
|
| 1287 |
export const toWeb = (self: HttpClientRequest, options?: {
|
| 1288 |
readonly signal?: AbortSignal | undefined
|
| 1289 |
}): Effect.Effect<Request, UrlParams.UrlParamsError> =>
|
| 1290 |
Effect.contextWith((context) =>
|
| 1291 |
+
Effect.fromResult(toWebResult(self, {
|
| 1292 |
context: context,
|
| 1293 |
signal: options?.signal
|
| 1294 |
+
}))
|
| 1295 |
)
|
.opencode/node_modules/effect/src/unstable/http/HttpClientResponse.ts
CHANGED
|
@@ -1,4 +1,12 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Effect from "../../Effect.ts"
|
|
@@ -19,31 +27,41 @@ import * as UrlParams from "./UrlParams.ts"
|
|
| 19 |
|
| 20 |
export {
|
| 21 |
/**
|
|
|
|
|
|
|
|
|
|
| 22 |
* @since 4.0.0
|
| 23 |
-
* @category schema
|
| 24 |
*/
|
| 25 |
schemaBodyJson,
|
| 26 |
/**
|
|
|
|
|
|
|
|
|
|
| 27 |
* @since 4.0.0
|
| 28 |
-
* @category schema
|
| 29 |
*/
|
| 30 |
schemaBodyUrlParams,
|
| 31 |
/**
|
|
|
|
|
|
|
|
|
|
| 32 |
* @since 4.0.0
|
| 33 |
-
* @category schema
|
| 34 |
*/
|
| 35 |
schemaHeaders
|
| 36 |
} from "./HttpIncomingMessage.ts"
|
| 37 |
|
| 38 |
/**
|
|
|
|
|
|
|
|
|
|
| 39 |
* @since 4.0.0
|
| 40 |
-
* @category Type IDs
|
| 41 |
*/
|
| 42 |
export const TypeId = "~effect/http/HttpClientResponse"
|
| 43 |
|
| 44 |
/**
|
| 45 |
-
*
|
|
|
|
| 46 |
* @category models
|
|
|
|
| 47 |
*/
|
| 48 |
export interface HttpClientResponse extends HttpIncomingMessage.HttpIncomingMessage<Error.HttpClientError>, Pipeable {
|
| 49 |
readonly [TypeId]: typeof TypeId
|
|
@@ -54,15 +72,19 @@ export interface HttpClientResponse extends HttpIncomingMessage.HttpIncomingMess
|
|
| 54 |
}
|
| 55 |
|
| 56 |
/**
|
| 57 |
-
*
|
|
|
|
| 58 |
* @category constructors
|
|
|
|
| 59 |
*/
|
| 60 |
export const fromWeb = (request: HttpClientRequest.HttpClientRequest, source: Response): HttpClientResponse =>
|
| 61 |
new WebHttpClientResponse(request, source)
|
| 62 |
|
| 63 |
/**
|
|
|
|
|
|
|
|
|
|
| 64 |
* @since 4.0.0
|
| 65 |
-
* @category schema
|
| 66 |
*/
|
| 67 |
export const schemaJson = <
|
| 68 |
A,
|
|
@@ -90,8 +112,10 @@ export const schemaJson = <
|
|
| 90 |
}
|
| 91 |
|
| 92 |
/**
|
|
|
|
|
|
|
|
|
|
| 93 |
* @since 4.0.0
|
| 94 |
-
* @category schema
|
| 95 |
*/
|
| 96 |
export const schemaNoBody = <
|
| 97 |
A,
|
|
@@ -114,21 +138,27 @@ export const schemaNoBody = <
|
|
| 114 |
}
|
| 115 |
|
| 116 |
/**
|
| 117 |
-
*
|
|
|
|
| 118 |
* @category accessors
|
|
|
|
| 119 |
*/
|
| 120 |
export const stream = <E, R>(
|
| 121 |
effect: Effect.Effect<HttpClientResponse, E, R>
|
| 122 |
): Stream.Stream<Uint8Array, Error.HttpClientError | E, R> => Stream.unwrap(Effect.map(effect, (self) => self.stream))
|
| 123 |
|
| 124 |
/**
|
| 125 |
-
*
|
|
|
|
| 126 |
* @category pattern matching
|
|
|
|
| 127 |
*/
|
| 128 |
export const matchStatus: {
|
| 129 |
/**
|
| 130 |
-
*
|
|
|
|
| 131 |
* @category pattern matching
|
|
|
|
| 132 |
*/
|
| 133 |
<
|
| 134 |
const Cases extends {
|
|
@@ -141,8 +171,10 @@ export const matchStatus: {
|
|
| 141 |
}
|
| 142 |
>(cases: Cases): (self: HttpClientResponse) => Cases[keyof Cases] extends (_: any) => infer R ? Unify<R> : never
|
| 143 |
/**
|
| 144 |
-
*
|
|
|
|
| 145 |
* @category pattern matching
|
|
|
|
| 146 |
*/
|
| 147 |
<
|
| 148 |
const Cases extends {
|
|
@@ -180,18 +212,24 @@ export const matchStatus: {
|
|
| 180 |
})
|
| 181 |
|
| 182 |
/**
|
| 183 |
-
*
|
|
|
|
| 184 |
* @category filters
|
|
|
|
| 185 |
*/
|
| 186 |
export const filterStatus: {
|
| 187 |
/**
|
| 188 |
-
*
|
|
|
|
| 189 |
* @category filters
|
|
|
|
| 190 |
*/
|
| 191 |
(f: (status: number) => boolean): (self: HttpClientResponse) => Effect.Effect<HttpClientResponse, Error.HttpClientError>
|
| 192 |
/**
|
| 193 |
-
*
|
|
|
|
| 194 |
* @category filters
|
|
|
|
| 195 |
*/
|
| 196 |
(self: HttpClientResponse, f: (status: number) => boolean): Effect.Effect<HttpClientResponse, Error.HttpClientError>
|
| 197 |
} = dual(
|
|
@@ -211,8 +249,10 @@ export const filterStatus: {
|
|
| 211 |
)
|
| 212 |
|
| 213 |
/**
|
| 214 |
-
*
|
|
|
|
| 215 |
* @category filters
|
|
|
|
| 216 |
*/
|
| 217 |
export const filterStatusOk = (self: HttpClientResponse): Effect.Effect<HttpClientResponse, Error.HttpClientError> =>
|
| 218 |
self.status >= 200 && self.status < 300 ? Effect.succeed(self) : Effect.fail(
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Represents responses returned by the Effect HTTP client.
|
| 3 |
+
*
|
| 4 |
+
* An `HttpClientResponse` keeps the original request together with the response
|
| 5 |
+
* status, headers, cookies, and body accessors from the shared incoming-message
|
| 6 |
+
* model. This module includes constructors, schema-based decoders, helpers for
|
| 7 |
+
* streaming response bodies, and utilities for matching or filtering by HTTP
|
| 8 |
+
* status.
|
| 9 |
+
*
|
| 10 |
* @since 4.0.0
|
| 11 |
*/
|
| 12 |
import * as Effect from "../../Effect.ts"
|
|
|
|
| 27 |
|
| 28 |
export {
|
| 29 |
/**
|
| 30 |
+
* Creates a decoder that reads a response JSON body and decodes it with the supplied schema.
|
| 31 |
+
*
|
| 32 |
+
* @category schemas
|
| 33 |
* @since 4.0.0
|
|
|
|
| 34 |
*/
|
| 35 |
schemaBodyJson,
|
| 36 |
/**
|
| 37 |
+
* Creates a decoder that reads response URL-encoded body parameters and decodes them with the supplied schema.
|
| 38 |
+
*
|
| 39 |
+
* @category schemas
|
| 40 |
* @since 4.0.0
|
|
|
|
| 41 |
*/
|
| 42 |
schemaBodyUrlParams,
|
| 43 |
/**
|
| 44 |
+
* Creates a decoder that validates and decodes response headers with the supplied schema.
|
| 45 |
+
*
|
| 46 |
+
* @category schemas
|
| 47 |
* @since 4.0.0
|
|
|
|
| 48 |
*/
|
| 49 |
schemaHeaders
|
| 50 |
} from "./HttpIncomingMessage.ts"
|
| 51 |
|
| 52 |
/**
|
| 53 |
+
* Type identifier for `HttpClientResponse` values.
|
| 54 |
+
*
|
| 55 |
+
* @category type IDs
|
| 56 |
* @since 4.0.0
|
|
|
|
| 57 |
*/
|
| 58 |
export const TypeId = "~effect/http/HttpClientResponse"
|
| 59 |
|
| 60 |
/**
|
| 61 |
+
* Model of an HTTP client response, including the original request, status, cookies, headers, and body accessors.
|
| 62 |
+
*
|
| 63 |
* @category models
|
| 64 |
+
* @since 4.0.0
|
| 65 |
*/
|
| 66 |
export interface HttpClientResponse extends HttpIncomingMessage.HttpIncomingMessage<Error.HttpClientError>, Pipeable {
|
| 67 |
readonly [TypeId]: typeof TypeId
|
|
|
|
| 72 |
}
|
| 73 |
|
| 74 |
/**
|
| 75 |
+
* Wraps a Web `Response` and its original `HttpClientRequest` as an `HttpClientResponse`.
|
| 76 |
+
*
|
| 77 |
* @category constructors
|
| 78 |
+
* @since 4.0.0
|
| 79 |
*/
|
| 80 |
export const fromWeb = (request: HttpClientRequest.HttpClientRequest, source: Response): HttpClientResponse =>
|
| 81 |
new WebHttpClientResponse(request, source)
|
| 82 |
|
| 83 |
/**
|
| 84 |
+
* Creates a decoder for a response's status, headers, and JSON body using the supplied schema.
|
| 85 |
+
*
|
| 86 |
+
* @category schemas
|
| 87 |
* @since 4.0.0
|
|
|
|
| 88 |
*/
|
| 89 |
export const schemaJson = <
|
| 90 |
A,
|
|
|
|
| 112 |
}
|
| 113 |
|
| 114 |
/**
|
| 115 |
+
* Creates a decoder for a response's status and headers without reading a response body.
|
| 116 |
+
*
|
| 117 |
+
* @category schemas
|
| 118 |
* @since 4.0.0
|
|
|
|
| 119 |
*/
|
| 120 |
export const schemaNoBody = <
|
| 121 |
A,
|
|
|
|
| 138 |
}
|
| 139 |
|
| 140 |
/**
|
| 141 |
+
* Converts an effect producing an `HttpClientResponse` into a stream of response body bytes.
|
| 142 |
+
*
|
| 143 |
* @category accessors
|
| 144 |
+
* @since 4.0.0
|
| 145 |
*/
|
| 146 |
export const stream = <E, R>(
|
| 147 |
effect: Effect.Effect<HttpClientResponse, E, R>
|
| 148 |
): Stream.Stream<Uint8Array, Error.HttpClientError | E, R> => Stream.unwrap(Effect.map(effect, (self) => self.stream))
|
| 149 |
|
| 150 |
/**
|
| 151 |
+
* Pattern matches on a response status, checking exact status handlers before status-class handlers and `orElse`.
|
| 152 |
+
*
|
| 153 |
* @category pattern matching
|
| 154 |
+
* @since 4.0.0
|
| 155 |
*/
|
| 156 |
export const matchStatus: {
|
| 157 |
/**
|
| 158 |
+
* Pattern matches on a response status, checking exact status handlers before status-class handlers and `orElse`.
|
| 159 |
+
*
|
| 160 |
* @category pattern matching
|
| 161 |
+
* @since 4.0.0
|
| 162 |
*/
|
| 163 |
<
|
| 164 |
const Cases extends {
|
|
|
|
| 171 |
}
|
| 172 |
>(cases: Cases): (self: HttpClientResponse) => Cases[keyof Cases] extends (_: any) => infer R ? Unify<R> : never
|
| 173 |
/**
|
| 174 |
+
* Pattern matches on a response status, checking exact status handlers before status-class handlers and `orElse`.
|
| 175 |
+
*
|
| 176 |
* @category pattern matching
|
| 177 |
+
* @since 4.0.0
|
| 178 |
*/
|
| 179 |
<
|
| 180 |
const Cases extends {
|
|
|
|
| 212 |
})
|
| 213 |
|
| 214 |
/**
|
| 215 |
+
* Succeeds with the response when its status satisfies the predicate, otherwise fails with `HttpClientError`.
|
| 216 |
+
*
|
| 217 |
* @category filters
|
| 218 |
+
* @since 4.0.0
|
| 219 |
*/
|
| 220 |
export const filterStatus: {
|
| 221 |
/**
|
| 222 |
+
* Succeeds with the response when its status satisfies the predicate, otherwise fails with `HttpClientError`.
|
| 223 |
+
*
|
| 224 |
* @category filters
|
| 225 |
+
* @since 4.0.0
|
| 226 |
*/
|
| 227 |
(f: (status: number) => boolean): (self: HttpClientResponse) => Effect.Effect<HttpClientResponse, Error.HttpClientError>
|
| 228 |
/**
|
| 229 |
+
* Succeeds with the response when its status satisfies the predicate, otherwise fails with `HttpClientError`.
|
| 230 |
+
*
|
| 231 |
* @category filters
|
| 232 |
+
* @since 4.0.0
|
| 233 |
*/
|
| 234 |
(self: HttpClientResponse, f: (status: number) => boolean): Effect.Effect<HttpClientResponse, Error.HttpClientError>
|
| 235 |
} = dual(
|
|
|
|
| 249 |
)
|
| 250 |
|
| 251 |
/**
|
| 252 |
+
* Succeeds with the response only when its status is in the 2xx range, otherwise fails with `HttpClientError`.
|
| 253 |
+
*
|
| 254 |
* @category filters
|
| 255 |
+
* @since 4.0.0
|
| 256 |
*/
|
| 257 |
export const filterStatusOk = (self: HttpClientResponse): Effect.Effect<HttpClientResponse, Error.HttpClientError> =>
|
| 258 |
self.status >= 200 && self.status < 300 ? Effect.succeed(self) : Effect.fail(
|
.opencode/node_modules/effect/src/unstable/http/HttpEffect.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import type * as Cause from "../../Cause.ts"
|
|
@@ -21,8 +28,10 @@ import * as Response from "./HttpServerResponse.ts"
|
|
| 21 |
import { appendPreResponseHandlerUnsafe, requestPreResponseHandlers } from "./internal/preResponseHandler.ts"
|
| 22 |
|
| 23 |
/**
|
| 24 |
-
*
|
|
|
|
| 25 |
* @category combinators
|
|
|
|
| 26 |
*/
|
| 27 |
export const toHandled = <E, R, EH, RH>(
|
| 28 |
self: Effect.Effect<HttpServerResponse, E, R>,
|
|
@@ -107,19 +116,25 @@ export const toHandled = <E, R, EH, RH>(
|
|
| 107 |
const handledSymbol = Symbol.for("effect/http/HttpEffect/handled")
|
| 108 |
|
| 109 |
/**
|
| 110 |
-
*
|
| 111 |
-
*
|
|
|
|
| 112 |
*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
* @since 4.0.0
|
| 114 |
-
* @category Scope
|
| 115 |
*/
|
| 116 |
export const scopeDisableClose = (scope: Scope.Scope): void => {
|
| 117 |
;(scope as any)[scopeEjected] = true
|
| 118 |
}
|
| 119 |
|
| 120 |
/**
|
|
|
|
|
|
|
|
|
|
| 121 |
* @since 4.0.0
|
| 122 |
-
* @category Scope
|
| 123 |
*/
|
| 124 |
export const scopeTransferToStream = (
|
| 125 |
response: HttpServerResponse
|
|
@@ -155,8 +170,10 @@ const scoped = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
|
|
| 155 |
})
|
| 156 |
|
| 157 |
/**
|
| 158 |
-
*
|
|
|
|
| 159 |
* @category Pre-response handlers
|
|
|
|
| 160 |
*/
|
| 161 |
export type PreResponseHandler = (
|
| 162 |
request: HttpServerRequest,
|
|
@@ -164,8 +181,10 @@ export type PreResponseHandler = (
|
|
| 164 |
) => Effect.Effect<HttpServerResponse, HttpServerError>
|
| 165 |
|
| 166 |
/**
|
| 167 |
-
*
|
|
|
|
| 168 |
* @category fiber refs
|
|
|
|
| 169 |
*/
|
| 170 |
export const appendPreResponseHandler = (handler: PreResponseHandler): Effect.Effect<void, never, HttpServerRequest> =>
|
| 171 |
HttpServerRequest.use((request) => {
|
|
@@ -175,36 +194,48 @@ export const appendPreResponseHandler = (handler: PreResponseHandler): Effect.Ef
|
|
| 175 |
|
| 176 |
export {
|
| 177 |
/**
|
| 178 |
-
*
|
|
|
|
| 179 |
* @category fiber refs
|
|
|
|
| 180 |
*/
|
| 181 |
appendPreResponseHandlerUnsafe
|
| 182 |
}
|
| 183 |
|
| 184 |
/**
|
| 185 |
-
*
|
|
|
|
| 186 |
* @category fiber refs
|
|
|
|
| 187 |
*/
|
| 188 |
export const withPreResponseHandler: {
|
| 189 |
/**
|
| 190 |
-
*
|
|
|
|
| 191 |
* @category fiber refs
|
|
|
|
| 192 |
*/
|
| 193 |
(handler: PreResponseHandler): <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R | HttpServerRequest>
|
| 194 |
/**
|
| 195 |
-
*
|
|
|
|
| 196 |
* @category fiber refs
|
|
|
|
| 197 |
*/
|
| 198 |
<A, E, R>(self: Effect.Effect<A, E, R>, handler: PreResponseHandler): Effect.Effect<A, E, R | HttpServerRequest>
|
| 199 |
} = dual<
|
| 200 |
/**
|
| 201 |
-
*
|
|
|
|
| 202 |
* @category fiber refs
|
|
|
|
| 203 |
*/
|
| 204 |
(handler: PreResponseHandler) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R | HttpServerRequest>,
|
| 205 |
/**
|
| 206 |
-
*
|
|
|
|
| 207 |
* @category fiber refs
|
|
|
|
| 208 |
*/
|
| 209 |
<A, E, R>(self: Effect.Effect<A, E, R>, handler: PreResponseHandler) => Effect.Effect<A, E, R | HttpServerRequest>
|
| 210 |
>(2, (self, handler) =>
|
|
@@ -214,8 +245,10 @@ export const withPreResponseHandler: {
|
|
| 214 |
}))
|
| 215 |
|
| 216 |
/**
|
|
|
|
|
|
|
|
|
|
| 217 |
* @since 4.0.0
|
| 218 |
-
* @category conversions
|
| 219 |
*/
|
| 220 |
export const toWebHandlerWith = <Provided, R = never, ReqR = Exclude<R, Provided | Scope.Scope | HttpServerRequest>>(
|
| 221 |
context: Context.Context<Provided>
|
|
@@ -254,8 +287,10 @@ export const toWebHandlerWith = <Provided, R = never, ReqR = Exclude<R, Provided
|
|
| 254 |
}
|
| 255 |
|
| 256 |
/**
|
|
|
|
|
|
|
|
|
|
| 257 |
* @since 4.0.0
|
| 258 |
-
* @category conversions
|
| 259 |
*/
|
| 260 |
export const toWebHandler: <E>(
|
| 261 |
self: Effect.Effect<HttpServerResponse, E, HttpServerRequest | Scope.Scope>,
|
|
@@ -264,8 +299,10 @@ export const toWebHandler: <E>(
|
|
| 264 |
toWebHandlerWith(Context.empty())
|
| 265 |
|
| 266 |
/**
|
|
|
|
|
|
|
|
|
|
| 267 |
* @since 4.0.0
|
| 268 |
-
* @category conversions
|
| 269 |
*/
|
| 270 |
export const toWebHandlerLayerWith = <
|
| 271 |
E,
|
|
@@ -324,8 +361,10 @@ export const toWebHandlerLayerWith = <
|
|
| 324 |
}
|
| 325 |
|
| 326 |
/**
|
|
|
|
|
|
|
|
|
|
| 327 |
* @since 4.0.0
|
| 328 |
-
* @category conversions
|
| 329 |
*/
|
| 330 |
export const toWebHandlerLayer = <E, R, Provided, LE, ReqR = Exclude<R, Provided | Scope.Scope | HttpServerRequest>>(
|
| 331 |
self: Effect.Effect<HttpServerResponse, E, R>,
|
|
@@ -349,8 +388,10 @@ export const toWebHandlerLayer = <E, R, Provided, LE, ReqR = Exclude<R, Provided
|
|
| 349 |
})
|
| 350 |
|
| 351 |
/**
|
|
|
|
|
|
|
|
|
|
| 352 |
* @since 4.0.0
|
| 353 |
-
* @category conversions
|
| 354 |
*/
|
| 355 |
export const fromWebHandler = (
|
| 356 |
handler: (request: Request) => Promise<Response>
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Runs Effect HTTP server handlers at platform boundaries.
|
| 3 |
+
*
|
| 4 |
+
* This module turns effects that produce `HttpServerResponse` values into Web
|
| 5 |
+
* `Request` handlers and other server adapters. It also applies middleware,
|
| 6 |
+
* converts failures into responses, runs hooks before a response is sent, and
|
| 7 |
+
* manages request scopes for streamed responses.
|
| 8 |
+
*
|
| 9 |
* @since 4.0.0
|
| 10 |
*/
|
| 11 |
import type * as Cause from "../../Cause.ts"
|
|
|
|
| 28 |
import { appendPreResponseHandlerUnsafe, requestPreResponseHandlers } from "./internal/preResponseHandler.ts"
|
| 29 |
|
| 30 |
/**
|
| 31 |
+
* Runs an HTTP server effect, sends the produced response with the supplied handler, and converts failures into HTTP responses.
|
| 32 |
+
*
|
| 33 |
* @category combinators
|
| 34 |
+
* @since 4.0.0
|
| 35 |
*/
|
| 36 |
export const toHandled = <E, R, EH, RH>(
|
| 37 |
self: Effect.Effect<HttpServerResponse, E, R>,
|
|
|
|
| 116 |
const handledSymbol = Symbol.for("effect/http/HttpEffect/handled")
|
| 117 |
|
| 118 |
/**
|
| 119 |
+
* Disables automatic closing for an HTTP request scope.
|
| 120 |
+
*
|
| 121 |
+
* **Gotchas**
|
| 122 |
*
|
| 123 |
+
* Use only when another owner will close the scope; otherwise resources attached
|
| 124 |
+
* to the request scope can leak.
|
| 125 |
+
*
|
| 126 |
+
* @category resource management
|
| 127 |
* @since 4.0.0
|
|
|
|
| 128 |
*/
|
| 129 |
export const scopeDisableClose = (scope: Scope.Scope): void => {
|
| 130 |
;(scope as any)[scopeEjected] = true
|
| 131 |
}
|
| 132 |
|
| 133 |
/**
|
| 134 |
+
* Returns a streaming server response that closes the request scope when the body stream exits.
|
| 135 |
+
*
|
| 136 |
+
* @category resource management
|
| 137 |
* @since 4.0.0
|
|
|
|
| 138 |
*/
|
| 139 |
export const scopeTransferToStream = (
|
| 140 |
response: HttpServerResponse
|
|
|
|
| 170 |
})
|
| 171 |
|
| 172 |
/**
|
| 173 |
+
* Function run with the current request and response just before the response is sent, allowing the response to be replaced or failing with `HttpServerError`.
|
| 174 |
+
*
|
| 175 |
* @category Pre-response handlers
|
| 176 |
+
* @since 4.0.0
|
| 177 |
*/
|
| 178 |
export type PreResponseHandler = (
|
| 179 |
request: HttpServerRequest,
|
|
|
|
| 181 |
) => Effect.Effect<HttpServerResponse, HttpServerError>
|
| 182 |
|
| 183 |
/**
|
| 184 |
+
* Registers an additional pre-response handler for the current HTTP server request.
|
| 185 |
+
*
|
| 186 |
* @category fiber refs
|
| 187 |
+
* @since 4.0.0
|
| 188 |
*/
|
| 189 |
export const appendPreResponseHandler = (handler: PreResponseHandler): Effect.Effect<void, never, HttpServerRequest> =>
|
| 190 |
HttpServerRequest.use((request) => {
|
|
|
|
| 194 |
|
| 195 |
export {
|
| 196 |
/**
|
| 197 |
+
* Registers a pre-response handler for the supplied HTTP server request.
|
| 198 |
+
*
|
| 199 |
* @category fiber refs
|
| 200 |
+
* @since 4.0.0
|
| 201 |
*/
|
| 202 |
appendPreResponseHandlerUnsafe
|
| 203 |
}
|
| 204 |
|
| 205 |
/**
|
| 206 |
+
* Runs an effect after registering a pre-response handler for the current HTTP server request.
|
| 207 |
+
*
|
| 208 |
* @category fiber refs
|
| 209 |
+
* @since 4.0.0
|
| 210 |
*/
|
| 211 |
export const withPreResponseHandler: {
|
| 212 |
/**
|
| 213 |
+
* Runs an effect after registering a pre-response handler for the current HTTP server request.
|
| 214 |
+
*
|
| 215 |
* @category fiber refs
|
| 216 |
+
* @since 4.0.0
|
| 217 |
*/
|
| 218 |
(handler: PreResponseHandler): <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R | HttpServerRequest>
|
| 219 |
/**
|
| 220 |
+
* Runs an effect after registering a pre-response handler for the current HTTP server request.
|
| 221 |
+
*
|
| 222 |
* @category fiber refs
|
| 223 |
+
* @since 4.0.0
|
| 224 |
*/
|
| 225 |
<A, E, R>(self: Effect.Effect<A, E, R>, handler: PreResponseHandler): Effect.Effect<A, E, R | HttpServerRequest>
|
| 226 |
} = dual<
|
| 227 |
/**
|
| 228 |
+
* Runs an effect after registering a pre-response handler for the current HTTP server request.
|
| 229 |
+
*
|
| 230 |
* @category fiber refs
|
| 231 |
+
* @since 4.0.0
|
| 232 |
*/
|
| 233 |
(handler: PreResponseHandler) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R | HttpServerRequest>,
|
| 234 |
/**
|
| 235 |
+
* Runs an effect after registering a pre-response handler for the current HTTP server request.
|
| 236 |
+
*
|
| 237 |
* @category fiber refs
|
| 238 |
+
* @since 4.0.0
|
| 239 |
*/
|
| 240 |
<A, E, R>(self: Effect.Effect<A, E, R>, handler: PreResponseHandler) => Effect.Effect<A, E, R | HttpServerRequest>
|
| 241 |
>(2, (self, handler) =>
|
|
|
|
| 245 |
}))
|
| 246 |
|
| 247 |
/**
|
| 248 |
+
* Converts an HTTP server effect into a Web `Request` handler using the supplied base context and optional middleware.
|
| 249 |
+
*
|
| 250 |
+
* @category converting
|
| 251 |
* @since 4.0.0
|
|
|
|
| 252 |
*/
|
| 253 |
export const toWebHandlerWith = <Provided, R = never, ReqR = Exclude<R, Provided | Scope.Scope | HttpServerRequest>>(
|
| 254 |
context: Context.Context<Provided>
|
|
|
|
| 287 |
}
|
| 288 |
|
| 289 |
/**
|
| 290 |
+
* Converts an HTTP server effect into a Web `Request` handler using an empty base context.
|
| 291 |
+
*
|
| 292 |
+
* @category converting
|
| 293 |
* @since 4.0.0
|
|
|
|
| 294 |
*/
|
| 295 |
export const toWebHandler: <E>(
|
| 296 |
self: Effect.Effect<HttpServerResponse, E, HttpServerRequest | Scope.Scope>,
|
|
|
|
| 299 |
toWebHandlerWith(Context.empty())
|
| 300 |
|
| 301 |
/**
|
| 302 |
+
* Builds a Web `Request` handler from a layer and handler factory, returning the handler with a `dispose` function for the layer scope.
|
| 303 |
+
*
|
| 304 |
+
* @category converting
|
| 305 |
* @since 4.0.0
|
|
|
|
| 306 |
*/
|
| 307 |
export const toWebHandlerLayerWith = <
|
| 308 |
E,
|
|
|
|
| 361 |
}
|
| 362 |
|
| 363 |
/**
|
| 364 |
+
* Builds a Web `Request` handler for an HTTP server effect using a layer to provide its services, returning the handler with a `dispose` function.
|
| 365 |
+
*
|
| 366 |
+
* @category converting
|
| 367 |
* @since 4.0.0
|
|
|
|
| 368 |
*/
|
| 369 |
export const toWebHandlerLayer = <E, R, Provided, LE, ReqR = Exclude<R, Provided | Scope.Scope | HttpServerRequest>>(
|
| 370 |
self: Effect.Effect<HttpServerResponse, E, R>,
|
|
|
|
| 388 |
})
|
| 389 |
|
| 390 |
/**
|
| 391 |
+
* Adapts a Web `Request` handler into an HTTP server effect for the current `HttpServerRequest`.
|
| 392 |
+
*
|
| 393 |
+
* @category converting
|
| 394 |
* @since 4.0.0
|
|
|
|
| 395 |
*/
|
| 396 |
export const fromWebHandler = (
|
| 397 |
handler: (request: Request) => Promise<Response>
|
.opencode/node_modules/effect/src/unstable/http/HttpIncomingMessage.ts
CHANGED
|
@@ -1,4 +1,12 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Context from "../../Context.ts"
|
|
@@ -15,20 +23,26 @@ import type * as Headers from "./Headers.ts"
|
|
| 15 |
import * as UrlParams from "./UrlParams.ts"
|
| 16 |
|
| 17 |
/**
|
|
|
|
|
|
|
|
|
|
| 18 |
* @since 4.0.0
|
| 19 |
-
* @category Type IDs
|
| 20 |
*/
|
| 21 |
export const TypeId = "~effect/http/HttpIncomingMessage"
|
| 22 |
|
| 23 |
/**
|
|
|
|
|
|
|
|
|
|
| 24 |
* @since 4.0.0
|
| 25 |
-
* @category Guards
|
| 26 |
*/
|
| 27 |
export const isHttpIncomingMessage = (u: unknown): u is HttpIncomingMessage => hasProperty(u, TypeId)
|
| 28 |
|
| 29 |
/**
|
| 30 |
-
*
|
|
|
|
| 31 |
* @category models
|
|
|
|
| 32 |
*/
|
| 33 |
export interface HttpIncomingMessage<E = unknown> extends Inspectable.Inspectable {
|
| 34 |
readonly [TypeId]: typeof TypeId
|
|
@@ -42,8 +56,10 @@ export interface HttpIncomingMessage<E = unknown> extends Inspectable.Inspectabl
|
|
| 42 |
}
|
| 43 |
|
| 44 |
/**
|
|
|
|
|
|
|
|
|
|
| 45 |
* @since 4.0.0
|
| 46 |
-
* @category schema
|
| 47 |
*/
|
| 48 |
export const schemaBodyJson = <S extends Schema.Top>(schema: S, options?: ParseOptions | undefined) => {
|
| 49 |
const decode = Schema.decodeEffect(Schema.toCodecJson(schema))
|
|
@@ -54,8 +70,10 @@ export const schemaBodyJson = <S extends Schema.Top>(schema: S, options?: ParseO
|
|
| 54 |
}
|
| 55 |
|
| 56 |
/**
|
|
|
|
|
|
|
|
|
|
| 57 |
* @since 4.0.0
|
| 58 |
-
* @category schema
|
| 59 |
*/
|
| 60 |
export const schemaBodyUrlParams = <
|
| 61 |
A,
|
|
@@ -75,8 +93,10 @@ export const schemaBodyUrlParams = <
|
|
| 75 |
}
|
| 76 |
|
| 77 |
/**
|
|
|
|
|
|
|
|
|
|
| 78 |
* @since 4.0.0
|
| 79 |
-
* @category schema
|
| 80 |
*/
|
| 81 |
export const schemaHeaders = <A, I extends Readonly<Record<string, string | undefined>>, RD, RE>(
|
| 82 |
schema: Schema.Codec<A, I, RD, RE>,
|
|
@@ -87,8 +107,10 @@ export const schemaHeaders = <A, I extends Readonly<Record<string, string | unde
|
|
| 87 |
}
|
| 88 |
|
| 89 |
/**
|
|
|
|
|
|
|
|
|
|
| 90 |
* @since 4.0.0
|
| 91 |
-
* @category References
|
| 92 |
*/
|
| 93 |
export const MaxBodySize = Context.Reference<FileSystem.Size | undefined>(
|
| 94 |
"effect/http/HttpIncomingMessage/MaxBodySize",
|
|
@@ -96,6 +118,9 @@ export const MaxBodySize = Context.Reference<FileSystem.Size | undefined>(
|
|
| 96 |
)
|
| 97 |
|
| 98 |
/**
|
|
|
|
|
|
|
|
|
|
| 99 |
* @since 4.0.0
|
| 100 |
*/
|
| 101 |
export const inspect = <E>(self: HttpIncomingMessage<E>, that: object): object => {
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Common model for incoming HTTP messages.
|
| 3 |
+
*
|
| 4 |
+
* `HttpIncomingMessage` is used by server requests and client responses to
|
| 5 |
+
* expose headers, an optional remote address, and body accessors. This module
|
| 6 |
+
* provides decoders for JSON bodies, URL-encoded bodies, and headers, plus the
|
| 7 |
+
* shared body-size setting and inspection helper used by request and response
|
| 8 |
+
* implementations.
|
| 9 |
+
*
|
| 10 |
* @since 4.0.0
|
| 11 |
*/
|
| 12 |
import * as Context from "../../Context.ts"
|
|
|
|
| 23 |
import * as UrlParams from "./UrlParams.ts"
|
| 24 |
|
| 25 |
/**
|
| 26 |
+
* Type identifier for `HttpIncomingMessage` values.
|
| 27 |
+
*
|
| 28 |
+
* @category type IDs
|
| 29 |
* @since 4.0.0
|
|
|
|
| 30 |
*/
|
| 31 |
export const TypeId = "~effect/http/HttpIncomingMessage"
|
| 32 |
|
| 33 |
/**
|
| 34 |
+
* Returns `true` when a value is an `HttpIncomingMessage`.
|
| 35 |
+
*
|
| 36 |
+
* @category guards
|
| 37 |
* @since 4.0.0
|
|
|
|
| 38 |
*/
|
| 39 |
export const isHttpIncomingMessage = (u: unknown): u is HttpIncomingMessage => hasProperty(u, TypeId)
|
| 40 |
|
| 41 |
/**
|
| 42 |
+
* Common model for incoming HTTP messages, with headers, remote address, and effectful body accessors.
|
| 43 |
+
*
|
| 44 |
* @category models
|
| 45 |
+
* @since 4.0.0
|
| 46 |
*/
|
| 47 |
export interface HttpIncomingMessage<E = unknown> extends Inspectable.Inspectable {
|
| 48 |
readonly [TypeId]: typeof TypeId
|
|
|
|
| 56 |
}
|
| 57 |
|
| 58 |
/**
|
| 59 |
+
* Creates a decoder that reads an incoming message's JSON body and decodes it with the supplied schema.
|
| 60 |
+
*
|
| 61 |
+
* @category schemas
|
| 62 |
* @since 4.0.0
|
|
|
|
| 63 |
*/
|
| 64 |
export const schemaBodyJson = <S extends Schema.Top>(schema: S, options?: ParseOptions | undefined) => {
|
| 65 |
const decode = Schema.decodeEffect(Schema.toCodecJson(schema))
|
|
|
|
| 70 |
}
|
| 71 |
|
| 72 |
/**
|
| 73 |
+
* Creates a decoder that reads an incoming message's URL-encoded body parameters and decodes them with the supplied schema.
|
| 74 |
+
*
|
| 75 |
+
* @category schemas
|
| 76 |
* @since 4.0.0
|
|
|
|
| 77 |
*/
|
| 78 |
export const schemaBodyUrlParams = <
|
| 79 |
A,
|
|
|
|
| 93 |
}
|
| 94 |
|
| 95 |
/**
|
| 96 |
+
* Creates a decoder that validates and decodes an incoming message's headers with the supplied schema.
|
| 97 |
+
*
|
| 98 |
+
* @category schemas
|
| 99 |
* @since 4.0.0
|
|
|
|
| 100 |
*/
|
| 101 |
export const schemaHeaders = <A, I extends Readonly<Record<string, string | undefined>>, RD, RE>(
|
| 102 |
schema: Schema.Codec<A, I, RD, RE>,
|
|
|
|
| 107 |
}
|
| 108 |
|
| 109 |
/**
|
| 110 |
+
* Context reference for the optional maximum size allowed when reading an incoming message body.
|
| 111 |
+
*
|
| 112 |
+
* @category references
|
| 113 |
* @since 4.0.0
|
|
|
|
| 114 |
*/
|
| 115 |
export const MaxBodySize = Context.Reference<FileSystem.Size | undefined>(
|
| 116 |
"effect/http/HttpIncomingMessage/MaxBodySize",
|
|
|
|
| 118 |
)
|
| 119 |
|
| 120 |
/**
|
| 121 |
+
* Builds an inspectable object for an incoming message, redacting headers and including a synchronously readable JSON or text body when available.
|
| 122 |
+
*
|
| 123 |
+
* @category converting
|
| 124 |
* @since 4.0.0
|
| 125 |
*/
|
| 126 |
export const inspect = <E>(self: HttpIncomingMessage<E>, that: object): object => {
|
.opencode/node_modules/effect/src/unstable/http/HttpMethod.ts
CHANGED
|
@@ -1,10 +1,19 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
|
| 5 |
/**
|
| 6 |
-
*
|
|
|
|
| 7 |
* @category models
|
|
|
|
| 8 |
*/
|
| 9 |
export type HttpMethod =
|
| 10 |
| "GET"
|
|
@@ -17,30 +26,46 @@ export type HttpMethod =
|
|
| 17 |
| "TRACE"
|
| 18 |
|
| 19 |
/**
|
|
|
|
|
|
|
| 20 |
* @since 4.0.0
|
| 21 |
-
* @category models
|
| 22 |
*/
|
| 23 |
export declare namespace HttpMethod {
|
| 24 |
/**
|
| 25 |
-
*
|
|
|
|
| 26 |
* @category models
|
|
|
|
| 27 |
*/
|
| 28 |
export type NoBody = "GET" | "HEAD" | "OPTIONS" | "TRACE"
|
| 29 |
|
| 30 |
/**
|
| 31 |
-
*
|
|
|
|
| 32 |
* @category models
|
|
|
|
| 33 |
*/
|
| 34 |
export type WithBody = Exclude<HttpMethod, NoBody>
|
| 35 |
}
|
| 36 |
|
| 37 |
/**
|
|
|
|
|
|
|
|
|
|
| 38 |
* @since 4.0.0
|
| 39 |
*/
|
| 40 |
export const hasBody = (method: HttpMethod): method is HttpMethod.WithBody =>
|
| 41 |
method !== "GET" && method !== "HEAD" && method !== "OPTIONS" && method !== "TRACE"
|
| 42 |
|
| 43 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
* @since 4.0.0
|
| 45 |
*/
|
| 46 |
export const all: ReadonlySet<HttpMethod> = new Set([
|
|
@@ -55,6 +80,15 @@ export const all: ReadonlySet<HttpMethod> = new Set([
|
|
| 55 |
])
|
| 56 |
|
| 57 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
* @since 4.0.0
|
| 59 |
*/
|
| 60 |
export const allShort = [
|
|
@@ -69,9 +103,9 @@ export const allShort = [
|
|
| 69 |
] as const
|
| 70 |
|
| 71 |
/**
|
| 72 |
-
*
|
| 73 |
*
|
| 74 |
-
* **Example**
|
| 75 |
*
|
| 76 |
* ```ts
|
| 77 |
* import { HttpMethod } from "effect/unstable/http"
|
|
@@ -84,7 +118,7 @@ export const allShort = [
|
|
| 84 |
* // false
|
| 85 |
* ```
|
| 86 |
*
|
| 87 |
-
* @since 4.0.0
|
| 88 |
* @category refinements
|
|
|
|
| 89 |
*/
|
| 90 |
export const isHttpMethod = (u: unknown): u is HttpMethod => all.has(u as HttpMethod)
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Defines supported HTTP method names for the unstable HTTP modules.
|
| 3 |
+
*
|
| 4 |
+
* Values are uppercase string literals such as `"GET"` and `"POST"`, matching
|
| 5 |
+
* the method tokens used by HTTP requests and routes. This module also includes
|
| 6 |
+
* helpers for checking whether a method can carry a request body and whether an
|
| 7 |
+
* unknown value is one of the supported methods.
|
| 8 |
+
*
|
| 9 |
* @since 4.0.0
|
| 10 |
*/
|
| 11 |
|
| 12 |
/**
|
| 13 |
+
* Union of supported uppercase HTTP method literals.
|
| 14 |
+
*
|
| 15 |
* @category models
|
| 16 |
+
* @since 4.0.0
|
| 17 |
*/
|
| 18 |
export type HttpMethod =
|
| 19 |
| "GET"
|
|
|
|
| 26 |
| "TRACE"
|
| 27 |
|
| 28 |
/**
|
| 29 |
+
* Namespace containing subtype helpers associated with `HttpMethod`.
|
| 30 |
+
*
|
| 31 |
* @since 4.0.0
|
|
|
|
| 32 |
*/
|
| 33 |
export declare namespace HttpMethod {
|
| 34 |
/**
|
| 35 |
+
* HTTP methods that this module treats as not carrying a request body.
|
| 36 |
+
*
|
| 37 |
* @category models
|
| 38 |
+
* @since 4.0.0
|
| 39 |
*/
|
| 40 |
export type NoBody = "GET" | "HEAD" | "OPTIONS" | "TRACE"
|
| 41 |
|
| 42 |
/**
|
| 43 |
+
* HTTP methods that this module treats as capable of carrying a request body.
|
| 44 |
+
*
|
| 45 |
* @category models
|
| 46 |
+
* @since 4.0.0
|
| 47 |
*/
|
| 48 |
export type WithBody = Exclude<HttpMethod, NoBody>
|
| 49 |
}
|
| 50 |
|
| 51 |
/**
|
| 52 |
+
* Returns `true` when a method can carry a request body and narrows it to `HttpMethod.WithBody`.
|
| 53 |
+
*
|
| 54 |
+
* @category predicates
|
| 55 |
* @since 4.0.0
|
| 56 |
*/
|
| 57 |
export const hasBody = (method: HttpMethod): method is HttpMethod.WithBody =>
|
| 58 |
method !== "GET" && method !== "HEAD" && method !== "OPTIONS" && method !== "TRACE"
|
| 59 |
|
| 60 |
/**
|
| 61 |
+
* Provides a readonly set containing every supported `HttpMethod` literal.
|
| 62 |
+
*
|
| 63 |
+
* **When to use**
|
| 64 |
+
*
|
| 65 |
+
* Use when you need to iterate over or test membership against every supported
|
| 66 |
+
* HTTP method literal.
|
| 67 |
+
*
|
| 68 |
+
* @category constants
|
| 69 |
* @since 4.0.0
|
| 70 |
*/
|
| 71 |
export const all: ReadonlySet<HttpMethod> = new Set([
|
|
|
|
| 80 |
])
|
| 81 |
|
| 82 |
/**
|
| 83 |
+
* Provides tuples mapping each supported HTTP method to its short
|
| 84 |
+
* request-constructor name.
|
| 85 |
+
*
|
| 86 |
+
* **When to use**
|
| 87 |
+
*
|
| 88 |
+
* Use when you need the mapping from supported HTTP method literals to their
|
| 89 |
+
* short request-constructor names.
|
| 90 |
+
*
|
| 91 |
+
* @category constants
|
| 92 |
* @since 4.0.0
|
| 93 |
*/
|
| 94 |
export const allShort = [
|
|
|
|
| 103 |
] as const
|
| 104 |
|
| 105 |
/**
|
| 106 |
+
* Checks whether a value is a `HttpMethod`.
|
| 107 |
*
|
| 108 |
+
* **Example** (Checking HTTP method values)
|
| 109 |
*
|
| 110 |
* ```ts
|
| 111 |
* import { HttpMethod } from "effect/unstable/http"
|
|
|
|
| 118 |
* // false
|
| 119 |
* ```
|
| 120 |
*
|
|
|
|
| 121 |
* @category refinements
|
| 122 |
+
* @since 4.0.0
|
| 123 |
*/
|
| 124 |
export const isHttpMethod = (u: unknown): u is HttpMethod => all.has(u as HttpMethod)
|
.opencode/node_modules/effect/src/unstable/http/HttpMiddleware.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import { Clock } from "../../Clock.ts"
|
|
@@ -22,18 +31,25 @@ import * as TraceContext from "./HttpTraceContext.ts"
|
|
| 22 |
import { appendPreResponseHandlerUnsafe } from "./internal/preResponseHandler.ts"
|
| 23 |
|
| 24 |
/**
|
| 25 |
-
*
|
|
|
|
| 26 |
* @category models
|
|
|
|
| 27 |
*/
|
| 28 |
export interface HttpMiddleware {
|
| 29 |
<E, R>(self: Effect.Effect<HttpServerResponse, E, R | HttpServerRequest>): Effect.Effect<HttpServerResponse, any, any>
|
| 30 |
}
|
| 31 |
|
| 32 |
/**
|
|
|
|
|
|
|
| 33 |
* @since 4.0.0
|
| 34 |
*/
|
| 35 |
export declare namespace HttpMiddleware {
|
| 36 |
/**
|
|
|
|
|
|
|
|
|
|
| 37 |
* @since 4.0.0
|
| 38 |
*/
|
| 39 |
export interface Applied<A extends Effect.Effect<HttpServerResponse, any, any>, E, R> {
|
|
@@ -42,8 +58,10 @@ export declare namespace HttpMiddleware {
|
|
| 42 |
}
|
| 43 |
|
| 44 |
/**
|
| 45 |
-
*
|
|
|
|
| 46 |
* @category constructors
|
|
|
|
| 47 |
*/
|
| 48 |
export const make = <M extends HttpMiddleware>(middleware: M): M => middleware
|
| 49 |
|
|
@@ -63,8 +81,10 @@ const stripSearchAndHash = (url: string): string => {
|
|
| 63 |
}
|
| 64 |
|
| 65 |
/**
|
| 66 |
-
*
|
|
|
|
| 67 |
* @category Logger
|
|
|
|
| 68 |
*/
|
| 69 |
export const withLoggerDisabled = <A, E, R>(self: Effect.Effect<A, E, R>): Effect.Effect<A, E, R | HttpServerRequest> =>
|
| 70 |
Effect.withFiber((fiber) => {
|
|
@@ -74,8 +94,10 @@ export const withLoggerDisabled = <A, E, R>(self: Effect.Effect<A, E, R>): Effec
|
|
| 74 |
})
|
| 75 |
|
| 76 |
/**
|
| 77 |
-
*
|
|
|
|
| 78 |
* @category Tracer
|
|
|
|
| 79 |
*/
|
| 80 |
export const TracerDisabledWhen = Context.Reference<Predicate<HttpServerRequest>>(
|
| 81 |
"effect/http/HttpMiddleware/TracerDisabledWhen",
|
|
@@ -83,16 +105,20 @@ export const TracerDisabledWhen = Context.Reference<Predicate<HttpServerRequest>
|
|
| 83 |
)
|
| 84 |
|
| 85 |
/**
|
| 86 |
-
*
|
|
|
|
| 87 |
* @category Tracer
|
|
|
|
| 88 |
*/
|
| 89 |
export const layerTracerDisabledForUrls = (
|
| 90 |
urls: ReadonlyArray<string>
|
| 91 |
): Layer.Layer<never> => Layer.succeed(TracerDisabledWhen)((req) => urls.includes(req.url))
|
| 92 |
|
| 93 |
/**
|
| 94 |
-
*
|
|
|
|
| 95 |
* @category Tracer
|
|
|
|
| 96 |
*/
|
| 97 |
export const SpanNameGenerator = Context.Reference<(request: HttpServerRequest) => string>(
|
| 98 |
"@effect/platform/HttpMiddleware/SpanNameGenerator",
|
|
@@ -100,14 +126,15 @@ export const SpanNameGenerator = Context.Reference<(request: HttpServerRequest)
|
|
| 100 |
)
|
| 101 |
|
| 102 |
/**
|
| 103 |
-
*
|
|
|
|
| 104 |
* @category Logger
|
|
|
|
| 105 |
*/
|
| 106 |
export const logger: <E, R>(
|
| 107 |
httpApp: Effect.Effect<HttpServerResponse, E, HttpServerRequest | R>
|
| 108 |
-
) => Effect.Effect<HttpServerResponse, E, HttpServerRequest | R> = make((httpApp) =>
|
| 109 |
-
|
| 110 |
-
return Effect.withFiber((fiber) => {
|
| 111 |
const request = Context.getUnsafe(fiber.context, HttpServerRequest)
|
| 112 |
const path = stripSearchAndHash(request.url)
|
| 113 |
return Effect.withLogSpan(
|
|
@@ -134,14 +161,16 @@ export const logger: <E, R>(
|
|
| 134 |
exit
|
| 135 |
)
|
| 136 |
}),
|
| 137 |
-
|
| 138 |
)
|
| 139 |
})
|
| 140 |
-
|
| 141 |
|
| 142 |
/**
|
| 143 |
-
*
|
|
|
|
| 144 |
* @category Tracer
|
|
|
|
| 145 |
*/
|
| 146 |
export const tracer: <E, R>(
|
| 147 |
httpApp: Effect.Effect<HttpServerResponse, E, HttpServerRequest | R>
|
|
@@ -203,8 +232,10 @@ export const tracer: <E, R>(
|
|
| 203 |
)
|
| 204 |
|
| 205 |
/**
|
| 206 |
-
*
|
|
|
|
| 207 |
* @category Proxying
|
|
|
|
| 208 |
*/
|
| 209 |
export const xForwardedHeaders = make((httpApp) =>
|
| 210 |
Effect.updateService(httpApp, HttpServerRequest, (request) =>
|
|
@@ -221,8 +252,10 @@ export const xForwardedHeaders = make((httpApp) =>
|
|
| 221 |
)
|
| 222 |
|
| 223 |
/**
|
|
|
|
|
|
|
|
|
|
| 224 |
* @since 4.0.0
|
| 225 |
-
* @category Search params
|
| 226 |
*/
|
| 227 |
export const searchParamsParser = <E, R>(
|
| 228 |
httpApp: Effect.Effect<HttpServerResponse, E, R>
|
|
@@ -239,8 +272,10 @@ export const searchParamsParser = <E, R>(
|
|
| 239 |
})
|
| 240 |
|
| 241 |
/**
|
| 242 |
-
*
|
|
|
|
| 243 |
* @category CORS
|
|
|
|
| 244 |
*/
|
| 245 |
export const cors = (options?: {
|
| 246 |
readonly allowedOrigins?: ReadonlyArray<string> | Predicate<string> | undefined
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Wraps HTTP server apps with request and response behavior.
|
| 3 |
+
*
|
| 4 |
+
* A middleware is a function from one HTTP server app effect to another. The app
|
| 5 |
+
* runs with the current `HttpServerRequest` in its context, so middleware can
|
| 6 |
+
* inspect or rewrite the request, provide request-scoped services, attach hooks
|
| 7 |
+
* before the response is sent, or observe the app exit. This module includes
|
| 8 |
+
* middleware for response logging, server tracing, forwarded proxy headers,
|
| 9 |
+
* parsed search parameters, and CORS response headers.
|
| 10 |
+
*
|
| 11 |
* @since 4.0.0
|
| 12 |
*/
|
| 13 |
import { Clock } from "../../Clock.ts"
|
|
|
|
| 31 |
import { appendPreResponseHandlerUnsafe } from "./internal/preResponseHandler.ts"
|
| 32 |
|
| 33 |
/**
|
| 34 |
+
* Middleware that transforms an HTTP server app effect into another HTTP server app effect.
|
| 35 |
+
*
|
| 36 |
* @category models
|
| 37 |
+
* @since 4.0.0
|
| 38 |
*/
|
| 39 |
export interface HttpMiddleware {
|
| 40 |
<E, R>(self: Effect.Effect<HttpServerResponse, E, R | HttpServerRequest>): Effect.Effect<HttpServerResponse, any, any>
|
| 41 |
}
|
| 42 |
|
| 43 |
/**
|
| 44 |
+
* Namespace containing types associated with `HttpMiddleware`.
|
| 45 |
+
*
|
| 46 |
* @since 4.0.0
|
| 47 |
*/
|
| 48 |
export declare namespace HttpMiddleware {
|
| 49 |
/**
|
| 50 |
+
* Callable type representing middleware already specialized to a particular transformed app type.
|
| 51 |
+
*
|
| 52 |
+
* @category models
|
| 53 |
* @since 4.0.0
|
| 54 |
*/
|
| 55 |
export interface Applied<A extends Effect.Effect<HttpServerResponse, any, any>, E, R> {
|
|
|
|
| 58 |
}
|
| 59 |
|
| 60 |
/**
|
| 61 |
+
* Defines an `HttpMiddleware` while preserving its precise type.
|
| 62 |
+
*
|
| 63 |
* @category constructors
|
| 64 |
+
* @since 4.0.0
|
| 65 |
*/
|
| 66 |
export const make = <M extends HttpMiddleware>(middleware: M): M => middleware
|
| 67 |
|
|
|
|
| 81 |
}
|
| 82 |
|
| 83 |
/**
|
| 84 |
+
* Runs an effect with HTTP response logging disabled for the current server request.
|
| 85 |
+
*
|
| 86 |
* @category Logger
|
| 87 |
+
* @since 4.0.0
|
| 88 |
*/
|
| 89 |
export const withLoggerDisabled = <A, E, R>(self: Effect.Effect<A, E, R>): Effect.Effect<A, E, R | HttpServerRequest> =>
|
| 90 |
Effect.withFiber((fiber) => {
|
|
|
|
| 94 |
})
|
| 95 |
|
| 96 |
/**
|
| 97 |
+
* Context reference for a predicate that disables server-side tracing for matching requests.
|
| 98 |
+
*
|
| 99 |
* @category Tracer
|
| 100 |
+
* @since 4.0.0
|
| 101 |
*/
|
| 102 |
export const TracerDisabledWhen = Context.Reference<Predicate<HttpServerRequest>>(
|
| 103 |
"effect/http/HttpMiddleware/TracerDisabledWhen",
|
|
|
|
| 105 |
)
|
| 106 |
|
| 107 |
/**
|
| 108 |
+
* Creates a layer that disables server-side tracing for requests whose URL exactly matches one of the supplied URLs.
|
| 109 |
+
*
|
| 110 |
* @category Tracer
|
| 111 |
+
* @since 4.0.0
|
| 112 |
*/
|
| 113 |
export const layerTracerDisabledForUrls = (
|
| 114 |
urls: ReadonlyArray<string>
|
| 115 |
): Layer.Layer<never> => Layer.succeed(TracerDisabledWhen)((req) => urls.includes(req.url))
|
| 116 |
|
| 117 |
/**
|
| 118 |
+
* Context reference for generating server span names from HTTP server requests.
|
| 119 |
+
*
|
| 120 |
* @category Tracer
|
| 121 |
+
* @since 4.0.0
|
| 122 |
*/
|
| 123 |
export const SpanNameGenerator = Context.Reference<(request: HttpServerRequest) => string>(
|
| 124 |
"@effect/platform/HttpMiddleware/SpanNameGenerator",
|
|
|
|
| 126 |
)
|
| 127 |
|
| 128 |
/**
|
| 129 |
+
* Middleware that logs sent HTTP responses with request method, request URL, and response status annotations.
|
| 130 |
+
*
|
| 131 |
* @category Logger
|
| 132 |
+
* @since 4.0.0
|
| 133 |
*/
|
| 134 |
export const logger: <E, R>(
|
| 135 |
httpApp: Effect.Effect<HttpServerResponse, E, HttpServerRequest | R>
|
| 136 |
+
) => Effect.Effect<HttpServerResponse, E, HttpServerRequest | R> = make((httpApp) =>
|
| 137 |
+
Effect.withFiber((fiber) => {
|
|
|
|
| 138 |
const request = Context.getUnsafe(fiber.context, HttpServerRequest)
|
| 139 |
const path = stripSearchAndHash(request.url)
|
| 140 |
return Effect.withLogSpan(
|
|
|
|
| 161 |
exit
|
| 162 |
)
|
| 163 |
}),
|
| 164 |
+
"http.span"
|
| 165 |
)
|
| 166 |
})
|
| 167 |
+
)
|
| 168 |
|
| 169 |
/**
|
| 170 |
+
* Middleware that creates a server trace span for each request and records request and response HTTP attributes.
|
| 171 |
+
*
|
| 172 |
* @category Tracer
|
| 173 |
+
* @since 4.0.0
|
| 174 |
*/
|
| 175 |
export const tracer: <E, R>(
|
| 176 |
httpApp: Effect.Effect<HttpServerResponse, E, HttpServerRequest | R>
|
|
|
|
| 232 |
)
|
| 233 |
|
| 234 |
/**
|
| 235 |
+
* Middleware that trusts `X-Forwarded-Host` and `X-Forwarded-For`, updating the request host header and remote address.
|
| 236 |
+
*
|
| 237 |
* @category Proxying
|
| 238 |
+
* @since 4.0.0
|
| 239 |
*/
|
| 240 |
export const xForwardedHeaders = make((httpApp) =>
|
| 241 |
Effect.updateService(httpApp, HttpServerRequest, (request) =>
|
|
|
|
| 252 |
)
|
| 253 |
|
| 254 |
/**
|
| 255 |
+
* Middleware that parses the current request URL's search parameters and provides them as `ParsedSearchParams`.
|
| 256 |
+
*
|
| 257 |
+
* @category search params
|
| 258 |
* @since 4.0.0
|
|
|
|
| 259 |
*/
|
| 260 |
export const searchParamsParser = <E, R>(
|
| 261 |
httpApp: Effect.Effect<HttpServerResponse, E, R>
|
|
|
|
| 272 |
})
|
| 273 |
|
| 274 |
/**
|
| 275 |
+
* Middleware that handles CORS preflight requests and adds configured CORS headers to HTTP responses.
|
| 276 |
+
*
|
| 277 |
* @category CORS
|
| 278 |
+
* @since 4.0.0
|
| 279 |
*/
|
| 280 |
export const cors = (options?: {
|
| 281 |
readonly allowedOrigins?: ReadonlyArray<string> | Predicate<string> | undefined
|
.opencode/node_modules/effect/src/unstable/http/HttpPlatform.ts
CHANGED
|
@@ -1,4 +1,12 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Context from "../../Context.ts"
|
|
@@ -15,8 +23,10 @@ import type * as Body from "./HttpBody.ts"
|
|
| 15 |
import * as Response from "./HttpServerResponse.ts"
|
| 16 |
|
| 17 |
/**
|
|
|
|
|
|
|
|
|
|
| 18 |
* @since 4.0.0
|
| 19 |
-
* @category tags
|
| 20 |
*/
|
| 21 |
export class HttpPlatform extends Context.Service<HttpPlatform, {
|
| 22 |
readonly fileResponse: (
|
|
@@ -38,8 +48,10 @@ export class HttpPlatform extends Context.Service<HttpPlatform, {
|
|
| 38 |
}>()("effect/http/HttpPlatform") {}
|
| 39 |
|
| 40 |
/**
|
| 41 |
-
*
|
|
|
|
| 42 |
* @category constructors
|
|
|
|
| 43 |
*/
|
| 44 |
export const make: (impl: {
|
| 45 |
readonly fileResponse: (
|
|
@@ -117,11 +129,19 @@ export const make: (impl: {
|
|
| 117 |
})
|
| 118 |
|
| 119 |
/**
|
| 120 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
* @category layers
|
|
|
|
| 122 |
*/
|
| 123 |
export const layer = Layer.effect(HttpPlatform)(
|
| 124 |
-
Effect.flatMap(FileSystem.FileSystem
|
| 125 |
make({
|
| 126 |
fileResponse(path, status, statusText, headers, start, end, contentLength) {
|
| 127 |
return Response.stream(
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Platform-specific support for serving files as HTTP server responses.
|
| 3 |
+
*
|
| 4 |
+
* `HttpPlatform` is the boundary between the portable HTTP response model and
|
| 5 |
+
* the runtime that knows how to stream bytes from the host platform. Server
|
| 6 |
+
* code uses this service when it needs to return local files, static assets,
|
| 7 |
+
* downloads, byte ranges, or Web `File`-like values without constructing the
|
| 8 |
+
* response body by hand.
|
| 9 |
+
*
|
| 10 |
* @since 4.0.0
|
| 11 |
*/
|
| 12 |
import * as Context from "../../Context.ts"
|
|
|
|
| 23 |
import * as Response from "./HttpServerResponse.ts"
|
| 24 |
|
| 25 |
/**
|
| 26 |
+
* Service for platform-specific HTTP response helpers, including file-backed server responses.
|
| 27 |
+
*
|
| 28 |
+
* @category services
|
| 29 |
* @since 4.0.0
|
|
|
|
| 30 |
*/
|
| 31 |
export class HttpPlatform extends Context.Service<HttpPlatform, {
|
| 32 |
readonly fileResponse: (
|
|
|
|
| 48 |
}>()("effect/http/HttpPlatform") {}
|
| 49 |
|
| 50 |
/**
|
| 51 |
+
* Creates an `HttpPlatform` service from platform-specific file response constructors, using `FileSystem` and `Etag.Generator`.
|
| 52 |
+
*
|
| 53 |
* @category constructors
|
| 54 |
+
* @since 4.0.0
|
| 55 |
*/
|
| 56 |
export const make: (impl: {
|
| 57 |
readonly fileResponse: (
|
|
|
|
| 129 |
})
|
| 130 |
|
| 131 |
/**
|
| 132 |
+
* Provides the default `HttpPlatform` implementation for serving file paths and
|
| 133 |
+
* `File`-like values as streamed HTTP responses.
|
| 134 |
+
*
|
| 135 |
+
* **Details**
|
| 136 |
+
*
|
| 137 |
+
* The layer uses the `FileSystem` and weak ETag services to add file metadata
|
| 138 |
+
* headers such as `etag` and `last-modified`.
|
| 139 |
+
*
|
| 140 |
* @category layers
|
| 141 |
+
* @since 4.0.0
|
| 142 |
*/
|
| 143 |
export const layer = Layer.effect(HttpPlatform)(
|
| 144 |
+
Effect.flatMap(FileSystem.FileSystem, (fs) =>
|
| 145 |
make({
|
| 146 |
fileResponse(path, status, statusText, headers, start, end, contentLength) {
|
| 147 |
return Response.stream(
|
.opencode/node_modules/effect/src/unstable/http/HttpRouter.ts
CHANGED
|
@@ -1,4 +1,12 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Arr from "../../Array.ts"
|
|
@@ -25,8 +33,16 @@ import * as HttpServerResponse from "./HttpServerResponse.ts"
|
|
| 25 |
const TypeId = "~effect/http/HttpRouter"
|
| 26 |
|
| 27 |
/**
|
| 28 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
* @category HttpRouter
|
|
|
|
| 30 |
*/
|
| 31 |
export interface HttpRouter {
|
| 32 |
readonly [TypeId]: typeof TypeId
|
|
@@ -77,16 +93,27 @@ export interface HttpRouter {
|
|
| 77 |
}
|
| 78 |
|
| 79 |
/**
|
| 80 |
-
*
|
|
|
|
|
|
|
|
|
|
| 81 |
* @category HttpRouter
|
|
|
|
| 82 |
*/
|
| 83 |
export const HttpRouter: Context.Service<HttpRouter, HttpRouter> = Context.Service<HttpRouter>(
|
| 84 |
"effect/http/HttpRouter"
|
| 85 |
)
|
| 86 |
|
| 87 |
/**
|
| 88 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
* @category HttpRouter
|
|
|
|
| 90 |
*/
|
| 91 |
export const make = Effect.gen(function*() {
|
| 92 |
const router = FindMyWay.make<Route<any, never>>(yield* RouterConfig)
|
|
@@ -148,7 +175,7 @@ export const make = Effect.gen(function*() {
|
|
| 148 |
Effect.succeed(handler) :
|
| 149 |
Effect.isEffect(handler)
|
| 150 |
? handler
|
| 151 |
-
: Effect.flatMap(HttpServerRequest.HttpServerRequest
|
| 152 |
uninterruptible: options?.uninterruptible ?? false,
|
| 153 |
prefix
|
| 154 |
})
|
|
@@ -215,8 +242,15 @@ function sliceRequestUrl(request: HttpServerRequest.HttpServerRequest, prefix: s
|
|
| 215 |
}
|
| 216 |
|
| 217 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
* @since 4.0.0
|
| 219 |
-
* @category Configuration
|
| 220 |
*/
|
| 221 |
export const RouterConfig = Context.Reference<Partial<FindMyWay.RouterConfig>>(
|
| 222 |
"effect/http/HttpRouter/RouterConfig",
|
|
@@ -224,8 +258,20 @@ export const RouterConfig = Context.Reference<Partial<FindMyWay.RouterConfig>>(
|
|
| 224 |
)
|
| 225 |
|
| 226 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
* @since 4.0.0
|
| 228 |
-
* @category RouteContext
|
| 229 |
*/
|
| 230 |
export class RouteContext extends Context.Service<RouteContext, {
|
| 231 |
readonly params: Readonly<Record<string, string | undefined>>
|
|
@@ -233,18 +279,28 @@ export class RouteContext extends Context.Service<RouteContext, {
|
|
| 233 |
}>()("effect/http/HttpRouter/RouteContext") {}
|
| 234 |
|
| 235 |
/**
|
|
|
|
|
|
|
|
|
|
| 236 |
* @since 4.0.0
|
| 237 |
-
* @category RouteContext
|
| 238 |
*/
|
| 239 |
export const params: Effect.Effect<
|
| 240 |
ReadonlyRecord<string, string | undefined>,
|
| 241 |
never,
|
| 242 |
RouteContext
|
| 243 |
-
> = Effect.map(RouteContext
|
| 244 |
|
| 245 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
* @since 4.0.0
|
| 247 |
-
* @category Schema
|
| 248 |
*/
|
| 249 |
export const schemaJson = <
|
| 250 |
A,
|
|
@@ -292,8 +348,15 @@ export const schemaJson = <
|
|
| 292 |
}
|
| 293 |
|
| 294 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 295 |
* @since 4.0.0
|
| 296 |
-
* @category Schema
|
| 297 |
*/
|
| 298 |
export const schemaNoBody = <
|
| 299 |
A,
|
|
@@ -338,8 +401,14 @@ export const schemaNoBody = <
|
|
| 338 |
}
|
| 339 |
|
| 340 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
* @since 4.0.0
|
| 342 |
-
* @category Schema
|
| 343 |
*/
|
| 344 |
export const schemaParams = <A, I extends Readonly<Record<string, string | ReadonlyArray<string> | undefined>>, RD, RE>(
|
| 345 |
schema: Schema.Codec<A, I, RD, RE>,
|
|
@@ -354,8 +423,11 @@ export const schemaParams = <A, I extends Readonly<Record<string, string | Reado
|
|
| 354 |
}
|
| 355 |
|
| 356 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 357 |
* @since 4.0.0
|
| 358 |
-
* @category Schema
|
| 359 |
*/
|
| 360 |
export const schemaPathParams = <A, I extends Readonly<Record<string, string | undefined>>, RD, RE>(
|
| 361 |
schema: Schema.Codec<A, I, RD, RE>,
|
|
@@ -366,12 +438,19 @@ export const schemaPathParams = <A, I extends Readonly<Record<string, string | u
|
|
| 366 |
}
|
| 367 |
|
| 368 |
/**
|
| 369 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 370 |
*
|
| 371 |
* ```ts
|
| 372 |
-
* import { Effect } from "effect"
|
| 373 |
-
* import
|
| 374 |
-
* import * as HttpRouter from "effect/unstable/http/HttpRouter"
|
| 375 |
*
|
| 376 |
* const MyRoute = Layer.effectDiscard(Effect.gen(function*() {
|
| 377 |
* const router = yield* HttpRouter.HttpRouter
|
|
@@ -380,21 +459,21 @@ export const schemaPathParams = <A, I extends Readonly<Record<string, string | u
|
|
| 380 |
* }))
|
| 381 |
* ```
|
| 382 |
*
|
| 383 |
-
* @since 4.0.0
|
| 384 |
* @category HttpRouter
|
|
|
|
| 385 |
*/
|
| 386 |
export const use = <A, E, R>(
|
| 387 |
f: (router: HttpRouter) => Effect.Effect<A, E, R>
|
| 388 |
-
): Layer.Layer<never, E, HttpRouter | Exclude<R, Scope.Scope>> =>
|
| 389 |
-
Layer.effectDiscard(Effect.flatMap(HttpRouter.asEffect(), f))
|
| 390 |
|
| 391 |
/**
|
| 392 |
* Create a layer that adds a single route to the HTTP router.
|
| 393 |
*
|
|
|
|
|
|
|
| 394 |
* ```ts
|
| 395 |
* import { Effect } from "effect"
|
| 396 |
-
* import
|
| 397 |
-
* import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse"
|
| 398 |
*
|
| 399 |
* const Route = HttpRouter.add(
|
| 400 |
* "GET",
|
|
@@ -403,8 +482,8 @@ export const use = <A, E, R>(
|
|
| 403 |
* )
|
| 404 |
* ```
|
| 405 |
*
|
| 406 |
-
* @since 4.0.0
|
| 407 |
* @category HttpRouter
|
|
|
|
| 408 |
*/
|
| 409 |
export const add = <E = never, R = never>(
|
| 410 |
method: "*" | "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS",
|
|
@@ -422,10 +501,11 @@ export const add = <E = never, R = never>(
|
|
| 422 |
/**
|
| 423 |
* Create a layer that adds multiple routes to the HTTP router.
|
| 424 |
*
|
|
|
|
|
|
|
| 425 |
* ```ts
|
| 426 |
* import { Effect } from "effect"
|
| 427 |
-
* import
|
| 428 |
-
* import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse"
|
| 429 |
*
|
| 430 |
* const Routes = HttpRouter.addAll([
|
| 431 |
* HttpRouter.route(
|
|
@@ -436,8 +516,8 @@ export const add = <E = never, R = never>(
|
|
| 436 |
* ])
|
| 437 |
* ```
|
| 438 |
*
|
| 439 |
-
* @since 4.0.0
|
| 440 |
* @category HttpRouter
|
|
|
|
| 441 |
*/
|
| 442 |
export const addAll = <Routes extends ReadonlyArray<Route<any, any>>, EX = never, RX = never>(
|
| 443 |
routes: Routes | Effect.Effect<Routes, EX, RX>,
|
|
@@ -462,14 +542,25 @@ export const addAll = <Routes extends ReadonlyArray<Route<any, any>>, EX = never
|
|
| 462 |
}))
|
| 463 |
|
| 464 |
/**
|
| 465 |
-
*
|
|
|
|
| 466 |
* @category HttpRouter
|
|
|
|
| 467 |
*/
|
| 468 |
export const layer: Layer.Layer<HttpRouter> = Layer.effect(HttpRouter)(make)
|
| 469 |
|
| 470 |
/**
|
| 471 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 472 |
* @category HttpRouter
|
|
|
|
| 473 |
*/
|
| 474 |
export const toHttpEffect = <A, E, R>(
|
| 475 |
appLayer: Layer.Layer<A, E, R>
|
|
@@ -492,8 +583,15 @@ export const toHttpEffect = <A, E, R>(
|
|
| 492 |
const RouteTypeId = "~effect/http/HttpRouter/Route"
|
| 493 |
|
| 494 |
/**
|
| 495 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 496 |
* @category Route
|
|
|
|
| 497 |
*/
|
| 498 |
export interface Route<E = never, R = never> {
|
| 499 |
readonly [RouteTypeId]: typeof RouteTypeId
|
|
@@ -505,19 +603,25 @@ export interface Route<E = never, R = never> {
|
|
| 505 |
}
|
| 506 |
|
| 507 |
/**
|
|
|
|
|
|
|
|
|
|
| 508 |
* @since 4.0.0
|
| 509 |
-
* @category Route
|
| 510 |
*/
|
| 511 |
export declare namespace Route {
|
| 512 |
/**
|
| 513 |
-
*
|
|
|
|
| 514 |
* @category Route
|
|
|
|
| 515 |
*/
|
| 516 |
export type Error<R extends Route<any, any>> = R extends Route<infer E, infer _R> ? E : never
|
| 517 |
|
| 518 |
/**
|
| 519 |
-
*
|
|
|
|
| 520 |
* @category Route
|
|
|
|
| 521 |
*/
|
| 522 |
export type Context<T extends Route<any, any>> = T extends Route<infer _E, infer R> ? R : never
|
| 523 |
}
|
|
@@ -528,17 +632,24 @@ const makeRoute = <E, R>(options: {
|
|
| 528 |
readonly handler: Effect.Effect<HttpServerResponse.HttpServerResponse, E, R>
|
| 529 |
readonly uninterruptible?: boolean | undefined
|
| 530 |
readonly prefix?: Option.Option<string> | string | undefined
|
| 531 |
-
}): Route<E, Exclude<R, Provided>> =>
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
}) as Route<E, Exclude<R, Provided>>)
|
| 538 |
|
| 539 |
/**
|
| 540 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 541 |
* @category Route
|
|
|
|
| 542 |
*/
|
| 543 |
export const route = <E = never, R = never>(
|
| 544 |
method: "*" | HttpMethod.HttpMethod,
|
|
@@ -559,13 +670,16 @@ export const route = <E = never, R = never>(
|
|
| 559 |
Effect.succeed(handler) :
|
| 560 |
Effect.isEffect(handler)
|
| 561 |
? handler
|
| 562 |
-
: Effect.flatMap(HttpServerRequest.HttpServerRequest
|
| 563 |
uninterruptible: options?.uninterruptible ?? false
|
| 564 |
})
|
| 565 |
|
| 566 |
/**
|
| 567 |
-
*
|
|
|
|
|
|
|
| 568 |
* @category PathInput
|
|
|
|
| 569 |
*/
|
| 570 |
export type PathInput = `/${string}` | "*"
|
| 571 |
|
|
@@ -574,18 +688,39 @@ const removeTrailingSlash = (
|
|
| 574 |
): PathInput => (path.endsWith("/") ? path.slice(0, -1) : path) as any
|
| 575 |
|
| 576 |
/**
|
| 577 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 578 |
* @category PathInput
|
|
|
|
| 579 |
*/
|
| 580 |
export const prefixPath: {
|
| 581 |
/**
|
| 582 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 583 |
* @category PathInput
|
|
|
|
| 584 |
*/
|
| 585 |
(prefix: string): (self: string) => string
|
| 586 |
/**
|
| 587 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 588 |
* @category PathInput
|
|
|
|
| 589 |
*/
|
| 590 |
(self: string, prefix: string): string
|
| 591 |
} = dual(2, (self: string, prefix: string) => {
|
|
@@ -596,18 +731,42 @@ export const prefixPath: {
|
|
| 596 |
})
|
| 597 |
|
| 598 |
/**
|
| 599 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 600 |
* @category Route
|
|
|
|
| 601 |
*/
|
| 602 |
export const prefixRoute: {
|
| 603 |
/**
|
| 604 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 605 |
* @category Route
|
|
|
|
| 606 |
*/
|
| 607 |
(prefix: string): <E, R>(self: Route<E, R>) => Route<E, R>
|
| 608 |
/**
|
| 609 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 610 |
* @category Route
|
|
|
|
| 611 |
*/
|
| 612 |
<E, R>(self: Route<E, R>, prefix: string): Route<E, R>
|
| 613 |
} = dual(2, <E, R>(self: Route<E, R>, prefix: string): Route<E, R> =>
|
|
@@ -624,8 +783,8 @@ export const prefixRoute: {
|
|
| 624 |
* Represents a request-level dependency, that needs to be provided by
|
| 625 |
* middleware.
|
| 626 |
*
|
| 627 |
-
* @since 4.0.0
|
| 628 |
* @category Request types
|
|
|
|
| 629 |
*/
|
| 630 |
export interface Request<Kind extends string, T> {
|
| 631 |
readonly _: unique symbol
|
|
@@ -634,25 +793,35 @@ export interface Request<Kind extends string, T> {
|
|
| 634 |
}
|
| 635 |
|
| 636 |
/**
|
|
|
|
|
|
|
|
|
|
| 637 |
* @since 4.0.0
|
| 638 |
-
* @category Request types
|
| 639 |
*/
|
| 640 |
export declare namespace Request {
|
| 641 |
/**
|
| 642 |
-
*
|
|
|
|
| 643 |
* @category Request types
|
|
|
|
| 644 |
*/
|
| 645 |
export type From<Kind extends string, R> = R extends infer T ? Request<Kind, T> : never
|
| 646 |
|
| 647 |
/**
|
| 648 |
-
*
|
|
|
|
|
|
|
| 649 |
* @category Request types
|
|
|
|
| 650 |
*/
|
| 651 |
export type Only<Kind extends string, A> = A extends Request<Kind, infer T> ? T : never
|
| 652 |
|
| 653 |
/**
|
| 654 |
-
*
|
|
|
|
|
|
|
| 655 |
* @category Request types
|
|
|
|
| 656 |
*/
|
| 657 |
export type Without<A> = A extends Request<infer _Kind, infer _> ? never : A
|
| 658 |
}
|
|
@@ -661,8 +830,8 @@ export declare namespace Request {
|
|
| 661 |
* Services provided by the HTTP router, which are available in the
|
| 662 |
* request context.
|
| 663 |
*
|
| 664 |
-
* @since 4.0.0
|
| 665 |
* @category Request types
|
|
|
|
| 666 |
*/
|
| 667 |
export type Provided =
|
| 668 |
| HttpServerRequest.HttpServerRequest
|
|
@@ -673,8 +842,8 @@ export type Provided =
|
|
| 673 |
/**
|
| 674 |
* Services provided to global middleware.
|
| 675 |
*
|
| 676 |
-
* @since 4.0.0
|
| 677 |
* @category Request types
|
|
|
|
| 678 |
*/
|
| 679 |
export type GlobalProvided =
|
| 680 |
| HttpServerRequest.HttpServerRequest
|
|
@@ -683,8 +852,16 @@ export type GlobalProvided =
|
|
| 683 |
const MiddlewareTypeId = "~effect/http/HttpRouter/Middleware"
|
| 684 |
|
| 685 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 686 |
* @since 4.0.0
|
| 687 |
-
* @category Middleware
|
| 688 |
*/
|
| 689 |
export interface Middleware<
|
| 690 |
Config extends {
|
|
@@ -729,18 +906,18 @@ export interface Middleware<
|
|
| 729 |
/**
|
| 730 |
* Create a middleware layer that can be used to modify requests and responses.
|
| 731 |
*
|
|
|
|
|
|
|
| 732 |
* By default, the middleware only affects the routes that it is provided to.
|
| 733 |
*
|
| 734 |
* If you want to create a middleware that applies globally to all routes, pass
|
| 735 |
* the `global` option as `true`.
|
| 736 |
*
|
|
|
|
|
|
|
| 737 |
* ```ts
|
| 738 |
-
* import { Effect } from "effect"
|
| 739 |
-
* import
|
| 740 |
-
* import * as Context from "effect/Context"
|
| 741 |
-
* import * as HttpMiddleware from "effect/unstable/http/HttpMiddleware"
|
| 742 |
-
* import * as HttpRouter from "effect/unstable/http/HttpRouter"
|
| 743 |
-
* import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse"
|
| 744 |
*
|
| 745 |
* // Here we are defining a CORS middleware
|
| 746 |
* const CorsMiddleware = HttpRouter.middleware(HttpMiddleware.cors()).layer
|
|
@@ -784,8 +961,8 @@ export interface Middleware<
|
|
| 784 |
* )
|
| 785 |
* ```
|
| 786 |
*
|
|
|
|
| 787 |
* @since 4.0.0
|
| 788 |
-
* @category Middleware
|
| 789 |
*/
|
| 790 |
export const middleware:
|
| 791 |
& middleware.Make<never, never>
|
|
@@ -908,13 +1085,22 @@ const getMiddleware = (context: Context.Context<never>): Array<middleware.Fn> =>
|
|
| 908 |
}
|
| 909 |
|
| 910 |
/**
|
|
|
|
|
|
|
| 911 |
* @since 4.0.0
|
| 912 |
-
* @category Middleware
|
| 913 |
*/
|
| 914 |
export declare namespace middleware {
|
| 915 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 916 |
* @since 4.0.0
|
| 917 |
-
* @category Middleware
|
| 918 |
*/
|
| 919 |
export type Make<Provides = never, Handles = never> = {
|
| 920 |
<E, R, EX, RX, const Global extends boolean = false>(
|
|
@@ -995,8 +1181,11 @@ export declare namespace middleware {
|
|
| 995 |
}
|
| 996 |
|
| 997 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 998 |
* @since 4.0.0
|
| 999 |
-
* @category Middleware
|
| 1000 |
*/
|
| 1001 |
export type Fn = (
|
| 1002 |
effect: Effect.Effect<HttpServerResponse.HttpServerResponse>
|
|
@@ -1004,10 +1193,10 @@ export declare namespace middleware {
|
|
| 1004 |
}
|
| 1005 |
|
| 1006 |
/**
|
| 1007 |
-
*
|
| 1008 |
*
|
|
|
|
| 1009 |
* @since 4.0.0
|
| 1010 |
-
* @category Middleware
|
| 1011 |
*/
|
| 1012 |
export const cors = (
|
| 1013 |
options?: {
|
|
@@ -1021,13 +1210,13 @@ export const cors = (
|
|
| 1021 |
): Layer.Layer<never, never, HttpRouter> => middleware(HttpMiddleware.cors(options), { global: true })
|
| 1022 |
|
| 1023 |
/**
|
| 1024 |
-
*
|
|
|
|
|
|
|
| 1025 |
*
|
| 1026 |
* ```ts
|
| 1027 |
-
* import { Effect } from "effect"
|
| 1028 |
-
* import
|
| 1029 |
-
* import * as HttpRouter from "effect/unstable/http/HttpRouter"
|
| 1030 |
-
* import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse"
|
| 1031 |
*
|
| 1032 |
* const Route = HttpRouter.add(
|
| 1033 |
* "GET",
|
|
@@ -1039,16 +1228,16 @@ export const cors = (
|
|
| 1039 |
* )
|
| 1040 |
* ```
|
| 1041 |
*
|
|
|
|
| 1042 |
* @since 4.0.0
|
| 1043 |
-
* @category Middleware
|
| 1044 |
*/
|
| 1045 |
export const disableLogger: Layer.Layer<never> = middleware(HttpMiddleware.withLoggerDisabled).layer
|
| 1046 |
|
| 1047 |
/**
|
| 1048 |
* Provides request-level dependencies to some routes.
|
| 1049 |
*
|
|
|
|
| 1050 |
* @since 4.0.0
|
| 1051 |
-
* @category Middleware
|
| 1052 |
*/
|
| 1053 |
export const provideRequest =
|
| 1054 |
<A2, E2, R2>(layer: Layer.Layer<A2, E2, R2>) =>
|
|
@@ -1070,10 +1259,10 @@ export const provideRequest =
|
|
| 1070 |
)
|
| 1071 |
|
| 1072 |
/**
|
| 1073 |
-
*
|
| 1074 |
*
|
|
|
|
| 1075 |
* @since 4.0.0
|
| 1076 |
-
* @category Server
|
| 1077 |
*/
|
| 1078 |
export const serve = <A, E, R, HE, HR = Request.Only<"Requires", R> | Request.Only<"GlobalRequires", R>>(
|
| 1079 |
appLayer: Layer.Layer<A, E, R>,
|
|
@@ -1084,13 +1273,12 @@ export const serve = <A, E, R, HE, HR = Request.Only<"Requires", R> | Request.On
|
|
| 1084 |
/**
|
| 1085 |
* Middleware to apply to the HTTP server.
|
| 1086 |
*
|
| 1087 |
-
*
|
| 1088 |
-
* including the sending of the response. This means that modifications
|
| 1089 |
-
* to the response **WILL NOT** be reflected in the final response sent to the
|
| 1090 |
-
* client.
|
| 1091 |
*
|
| 1092 |
-
*
|
| 1093 |
-
* response.
|
|
|
|
|
|
|
| 1094 |
*/
|
| 1095 |
readonly middleware?: (
|
| 1096 |
effect: Effect.Effect<
|
|
@@ -1128,8 +1316,17 @@ export const serve = <A, E, R, HE, HR = Request.Only<"Requires", R> | Request.On
|
|
| 1128 |
}
|
| 1129 |
|
| 1130 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1131 |
* @since 4.0.0
|
| 1132 |
-
* @category Server
|
| 1133 |
*/
|
| 1134 |
export const toWebHandler = <
|
| 1135 |
A,
|
|
@@ -1151,13 +1348,12 @@ export const toWebHandler = <
|
|
| 1151 |
/**
|
| 1152 |
* Middleware to apply to the HTTP server.
|
| 1153 |
*
|
| 1154 |
-
*
|
| 1155 |
-
* including the sending of the response. This means that modifications
|
| 1156 |
-
* to the response **WILL NOT** be reflected in the final response sent to the
|
| 1157 |
-
* client.
|
| 1158 |
*
|
| 1159 |
-
*
|
| 1160 |
-
* response.
|
|
|
|
|
|
|
| 1161 |
*/
|
| 1162 |
readonly middleware?: (
|
| 1163 |
effect: Effect.Effect<
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Builds server-side routers for Effect HTTP applications.
|
| 3 |
+
*
|
| 4 |
+
* `HttpRouter` collects routes and middleware while an application layer is
|
| 5 |
+
* being built. Once the router is complete, it handles each
|
| 6 |
+
* `HttpServerRequest` by finding a matching route and producing an
|
| 7 |
+
* `HttpServerResponse`. The module also includes helpers for route definitions,
|
| 8 |
+
* prefixes, parameters, request decoding, CORS, and running the router.
|
| 9 |
+
*
|
| 10 |
* @since 4.0.0
|
| 11 |
*/
|
| 12 |
import * as Arr from "../../Array.ts"
|
|
|
|
| 33 |
const TypeId = "~effect/http/HttpRouter"
|
| 34 |
|
| 35 |
/**
|
| 36 |
+
* Defines the service interface for registering HTTP routes and middleware.
|
| 37 |
+
*
|
| 38 |
+
* **Details**
|
| 39 |
+
*
|
| 40 |
+
* An `HttpRouter` can add routes, apply path prefixes, install global middleware,
|
| 41 |
+
* and expose the registered routes as an Effect that handles the current server
|
| 42 |
+
* request.
|
| 43 |
+
*
|
| 44 |
* @category HttpRouter
|
| 45 |
+
* @since 4.0.0
|
| 46 |
*/
|
| 47 |
export interface HttpRouter {
|
| 48 |
readonly [TypeId]: typeof TypeId
|
|
|
|
| 93 |
}
|
| 94 |
|
| 95 |
/**
|
| 96 |
+
* Service tag for the HTTP router used while constructing an HTTP application.
|
| 97 |
+
* Route and middleware layers require this service to register themselves with
|
| 98 |
+
* the router.
|
| 99 |
+
*
|
| 100 |
* @category HttpRouter
|
| 101 |
+
* @since 4.0.0
|
| 102 |
*/
|
| 103 |
export const HttpRouter: Context.Service<HttpRouter, HttpRouter> = Context.Service<HttpRouter>(
|
| 104 |
"effect/http/HttpRouter"
|
| 105 |
)
|
| 106 |
|
| 107 |
/**
|
| 108 |
+
* Constructs an empty `HttpRouter` service.
|
| 109 |
+
*
|
| 110 |
+
* **Details**
|
| 111 |
+
*
|
| 112 |
+
* The returned router accepts route and middleware registrations and later routes
|
| 113 |
+
* the current `HttpServerRequest` to the matching `HttpServerResponse`.
|
| 114 |
+
*
|
| 115 |
* @category HttpRouter
|
| 116 |
+
* @since 4.0.0
|
| 117 |
*/
|
| 118 |
export const make = Effect.gen(function*() {
|
| 119 |
const router = FindMyWay.make<Route<any, never>>(yield* RouterConfig)
|
|
|
|
| 175 |
Effect.succeed(handler) :
|
| 176 |
Effect.isEffect(handler)
|
| 177 |
? handler
|
| 178 |
+
: Effect.flatMap(HttpServerRequest.HttpServerRequest, handler),
|
| 179 |
uninterruptible: options?.uninterruptible ?? false,
|
| 180 |
prefix
|
| 181 |
})
|
|
|
|
| 242 |
}
|
| 243 |
|
| 244 |
/**
|
| 245 |
+
* Context reference for low-level router configuration.
|
| 246 |
+
*
|
| 247 |
+
* **Details**
|
| 248 |
+
*
|
| 249 |
+
* The value is passed to the route matcher when an `HttpRouter` is created and
|
| 250 |
+
* defaults to an empty configuration.
|
| 251 |
+
*
|
| 252 |
+
* @category configuration
|
| 253 |
* @since 4.0.0
|
|
|
|
| 254 |
*/
|
| 255 |
export const RouterConfig = Context.Reference<Partial<FindMyWay.RouterConfig>>(
|
| 256 |
"effect/http/HttpRouter/RouterConfig",
|
|
|
|
| 258 |
)
|
| 259 |
|
| 260 |
/**
|
| 261 |
+
* Service for the matched HTTP route in the current request.
|
| 262 |
+
*
|
| 263 |
+
* **When to use**
|
| 264 |
+
*
|
| 265 |
+
* Use to read captured path parameters and route metadata while handling a
|
| 266 |
+
* request matched by the router.
|
| 267 |
+
*
|
| 268 |
+
* **Details**
|
| 269 |
+
*
|
| 270 |
+
* It provides the route definition and the path parameters captured by the route
|
| 271 |
+
* matcher.
|
| 272 |
+
*
|
| 273 |
+
* @category services
|
| 274 |
* @since 4.0.0
|
|
|
|
| 275 |
*/
|
| 276 |
export class RouteContext extends Context.Service<RouteContext, {
|
| 277 |
readonly params: Readonly<Record<string, string | undefined>>
|
|
|
|
| 279 |
}>()("effect/http/HttpRouter/RouteContext") {}
|
| 280 |
|
| 281 |
/**
|
| 282 |
+
* Effect that returns the path parameters captured for the current matched route.
|
| 283 |
+
*
|
| 284 |
+
* @category getters
|
| 285 |
* @since 4.0.0
|
|
|
|
| 286 |
*/
|
| 287 |
export const params: Effect.Effect<
|
| 288 |
ReadonlyRecord<string, string | undefined>,
|
| 289 |
never,
|
| 290 |
RouteContext
|
| 291 |
+
> = Effect.map(RouteContext, (_) => _.params)
|
| 292 |
|
| 293 |
/**
|
| 294 |
+
* Decodes a schema from the current request and its JSON body.
|
| 295 |
+
*
|
| 296 |
+
* **Details**
|
| 297 |
+
*
|
| 298 |
+
* The input passed to the schema includes the request method, URL, headers,
|
| 299 |
+
* cookies, path parameters, search parameters, and parsed JSON body. The effect
|
| 300 |
+
* fails if the body cannot be parsed or the schema decode fails.
|
| 301 |
+
*
|
| 302 |
+
* @category schemas
|
| 303 |
* @since 4.0.0
|
|
|
|
| 304 |
*/
|
| 305 |
export const schemaJson = <
|
| 306 |
A,
|
|
|
|
| 348 |
}
|
| 349 |
|
| 350 |
/**
|
| 351 |
+
* Decodes a schema from the current request without reading the request body.
|
| 352 |
+
*
|
| 353 |
+
* **Details**
|
| 354 |
+
*
|
| 355 |
+
* The input passed to the schema includes the request method, URL, headers,
|
| 356 |
+
* cookies, path parameters, and search parameters.
|
| 357 |
+
*
|
| 358 |
+
* @category schemas
|
| 359 |
* @since 4.0.0
|
|
|
|
| 360 |
*/
|
| 361 |
export const schemaNoBody = <
|
| 362 |
A,
|
|
|
|
| 401 |
}
|
| 402 |
|
| 403 |
/**
|
| 404 |
+
* Decodes a schema from the current route path parameters and search parameters.
|
| 405 |
+
*
|
| 406 |
+
* **Details**
|
| 407 |
+
*
|
| 408 |
+
* When the same key appears in both sources, the path parameter value is used.
|
| 409 |
+
*
|
| 410 |
+
* @category schemas
|
| 411 |
* @since 4.0.0
|
|
|
|
| 412 |
*/
|
| 413 |
export const schemaParams = <A, I extends Readonly<Record<string, string | ReadonlyArray<string> | undefined>>, RD, RE>(
|
| 414 |
schema: Schema.Codec<A, I, RD, RE>,
|
|
|
|
| 423 |
}
|
| 424 |
|
| 425 |
/**
|
| 426 |
+
* Decodes a schema from the path parameters captured for the current matched
|
| 427 |
+
* route.
|
| 428 |
+
*
|
| 429 |
+
* @category schemas
|
| 430 |
* @since 4.0.0
|
|
|
|
| 431 |
*/
|
| 432 |
export const schemaPathParams = <A, I extends Readonly<Record<string, string | undefined>>, RD, RE>(
|
| 433 |
schema: Schema.Codec<A, I, RD, RE>,
|
|
|
|
| 438 |
}
|
| 439 |
|
| 440 |
/**
|
| 441 |
+
* Creates a layer that accesses the current `HttpRouter` service and runs the
|
| 442 |
+
* supplied effect.
|
| 443 |
+
*
|
| 444 |
+
* **When to use**
|
| 445 |
+
*
|
| 446 |
+
* Use when you need to register routes or middleware with the router during layer
|
| 447 |
+
* construction.
|
| 448 |
+
*
|
| 449 |
+
* **Example** (Registering routes during layer construction)
|
| 450 |
*
|
| 451 |
* ```ts
|
| 452 |
+
* import { Effect, Layer } from "effect"
|
| 453 |
+
* import { HttpRouter } from "effect/unstable/http"
|
|
|
|
| 454 |
*
|
| 455 |
* const MyRoute = Layer.effectDiscard(Effect.gen(function*() {
|
| 456 |
* const router = yield* HttpRouter.HttpRouter
|
|
|
|
| 459 |
* }))
|
| 460 |
* ```
|
| 461 |
*
|
|
|
|
| 462 |
* @category HttpRouter
|
| 463 |
+
* @since 4.0.0
|
| 464 |
*/
|
| 465 |
export const use = <A, E, R>(
|
| 466 |
f: (router: HttpRouter) => Effect.Effect<A, E, R>
|
| 467 |
+
): Layer.Layer<never, E, HttpRouter | Exclude<R, Scope.Scope>> => Layer.effectDiscard(Effect.flatMap(HttpRouter, f))
|
|
|
|
| 468 |
|
| 469 |
/**
|
| 470 |
* Create a layer that adds a single route to the HTTP router.
|
| 471 |
*
|
| 472 |
+
* **Example** (Adding a GET route)
|
| 473 |
+
*
|
| 474 |
* ```ts
|
| 475 |
* import { Effect } from "effect"
|
| 476 |
+
* import { HttpRouter, HttpServerResponse } from "effect/unstable/http"
|
|
|
|
| 477 |
*
|
| 478 |
* const Route = HttpRouter.add(
|
| 479 |
* "GET",
|
|
|
|
| 482 |
* )
|
| 483 |
* ```
|
| 484 |
*
|
|
|
|
| 485 |
* @category HttpRouter
|
| 486 |
+
* @since 4.0.0
|
| 487 |
*/
|
| 488 |
export const add = <E = never, R = never>(
|
| 489 |
method: "*" | "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS",
|
|
|
|
| 501 |
/**
|
| 502 |
* Create a layer that adds multiple routes to the HTTP router.
|
| 503 |
*
|
| 504 |
+
* **Example** (Adding multiple routes)
|
| 505 |
+
*
|
| 506 |
* ```ts
|
| 507 |
* import { Effect } from "effect"
|
| 508 |
+
* import { HttpRouter, HttpServerResponse } from "effect/unstable/http"
|
|
|
|
| 509 |
*
|
| 510 |
* const Routes = HttpRouter.addAll([
|
| 511 |
* HttpRouter.route(
|
|
|
|
| 516 |
* ])
|
| 517 |
* ```
|
| 518 |
*
|
|
|
|
| 519 |
* @category HttpRouter
|
| 520 |
+
* @since 4.0.0
|
| 521 |
*/
|
| 522 |
export const addAll = <Routes extends ReadonlyArray<Route<any, any>>, EX = never, RX = never>(
|
| 523 |
routes: Routes | Effect.Effect<Routes, EX, RX>,
|
|
|
|
| 542 |
}))
|
| 543 |
|
| 544 |
/**
|
| 545 |
+
* Layer that provides a newly constructed `HttpRouter`.
|
| 546 |
+
*
|
| 547 |
* @category HttpRouter
|
| 548 |
+
* @since 4.0.0
|
| 549 |
*/
|
| 550 |
export const layer: Layer.Layer<HttpRouter> = Layer.effect(HttpRouter)(make)
|
| 551 |
|
| 552 |
/**
|
| 553 |
+
* Builds an application layer with a router and returns the router as an HTTP
|
| 554 |
+
* handler effect.
|
| 555 |
+
*
|
| 556 |
+
* **Details**
|
| 557 |
+
*
|
| 558 |
+
* The returned effect handles the current `HttpServerRequest` in the current
|
| 559 |
+
* `Scope`; route request markers are converted into the ordinary requirements of
|
| 560 |
+
* the returned handler.
|
| 561 |
+
*
|
| 562 |
* @category HttpRouter
|
| 563 |
+
* @since 4.0.0
|
| 564 |
*/
|
| 565 |
export const toHttpEffect = <A, E, R>(
|
| 566 |
appLayer: Layer.Layer<A, E, R>
|
|
|
|
| 583 |
const RouteTypeId = "~effect/http/HttpRouter/Route"
|
| 584 |
|
| 585 |
/**
|
| 586 |
+
* Description of a registered HTTP route.
|
| 587 |
+
*
|
| 588 |
+
* **Details**
|
| 589 |
+
*
|
| 590 |
+
* A route pairs an HTTP method and path pattern with a response handler, plus
|
| 591 |
+
* metadata used for prefix handling and interruptibility.
|
| 592 |
+
*
|
| 593 |
* @category Route
|
| 594 |
+
* @since 4.0.0
|
| 595 |
*/
|
| 596 |
export interface Route<E = never, R = never> {
|
| 597 |
readonly [RouteTypeId]: typeof RouteTypeId
|
|
|
|
| 603 |
}
|
| 604 |
|
| 605 |
/**
|
| 606 |
+
* Helper types for extracting the error and context types carried by `Route`
|
| 607 |
+
* values.
|
| 608 |
+
*
|
| 609 |
* @since 4.0.0
|
|
|
|
| 610 |
*/
|
| 611 |
export declare namespace Route {
|
| 612 |
/**
|
| 613 |
+
* Extracts the error type produced by a `Route` handler.
|
| 614 |
+
*
|
| 615 |
* @category Route
|
| 616 |
+
* @since 4.0.0
|
| 617 |
*/
|
| 618 |
export type Error<R extends Route<any, any>> = R extends Route<infer E, infer _R> ? E : never
|
| 619 |
|
| 620 |
/**
|
| 621 |
+
* Extracts the context requirements of a `Route` handler.
|
| 622 |
+
*
|
| 623 |
* @category Route
|
| 624 |
+
* @since 4.0.0
|
| 625 |
*/
|
| 626 |
export type Context<T extends Route<any, any>> = T extends Route<infer _E, infer R> ? R : never
|
| 627 |
}
|
|
|
|
| 632 |
readonly handler: Effect.Effect<HttpServerResponse.HttpServerResponse, E, R>
|
| 633 |
readonly uninterruptible?: boolean | undefined
|
| 634 |
readonly prefix?: Option.Option<string> | string | undefined
|
| 635 |
+
}): Route<E, Exclude<R, Provided>> => (({
|
| 636 |
+
...options,
|
| 637 |
+
uninterruptible: options.uninterruptible ?? false,
|
| 638 |
+
prefix: typeof options.prefix === "string" ? Option.some(options.prefix) : options.prefix ?? Option.none(),
|
| 639 |
+
[RouteTypeId]: RouteTypeId
|
| 640 |
+
}) as Route<E, Exclude<R, Provided>>)
|
|
|
|
| 641 |
|
| 642 |
/**
|
| 643 |
+
* Constructs a `Route` from an HTTP method, path, and handler.
|
| 644 |
+
*
|
| 645 |
+
* **Details**
|
| 646 |
+
*
|
| 647 |
+
* The handler may be a static response, an effect that produces a response, or a
|
| 648 |
+
* function from the current request to a response effect. Set `uninterruptible` to
|
| 649 |
+
* prevent the route handler from being made interruptible while it runs.
|
| 650 |
+
*
|
| 651 |
* @category Route
|
| 652 |
+
* @since 4.0.0
|
| 653 |
*/
|
| 654 |
export const route = <E = never, R = never>(
|
| 655 |
method: "*" | HttpMethod.HttpMethod,
|
|
|
|
| 670 |
Effect.succeed(handler) :
|
| 671 |
Effect.isEffect(handler)
|
| 672 |
? handler
|
| 673 |
+
: Effect.flatMap(HttpServerRequest.HttpServerRequest, handler),
|
| 674 |
uninterruptible: options?.uninterruptible ?? false
|
| 675 |
})
|
| 676 |
|
| 677 |
/**
|
| 678 |
+
* Path pattern accepted by the router. Routes must use an absolute path
|
| 679 |
+
* beginning with `/` or the wildcard `*`.
|
| 680 |
+
*
|
| 681 |
* @category PathInput
|
| 682 |
+
* @since 4.0.0
|
| 683 |
*/
|
| 684 |
export type PathInput = `/${string}` | "*"
|
| 685 |
|
|
|
|
| 688 |
): PathInput => (path.endsWith("/") ? path.slice(0, -1) : path) as any
|
| 689 |
|
| 690 |
/**
|
| 691 |
+
* Adds a path prefix to a route path.
|
| 692 |
+
*
|
| 693 |
+
* **Details**
|
| 694 |
+
*
|
| 695 |
+
* Trailing slashes are removed from the prefix; `/` becomes the prefix itself and
|
| 696 |
+
* `*` becomes a wildcard route under the prefix.
|
| 697 |
+
*
|
| 698 |
* @category PathInput
|
| 699 |
+
* @since 4.0.0
|
| 700 |
*/
|
| 701 |
export const prefixPath: {
|
| 702 |
/**
|
| 703 |
+
* Adds a path prefix to a route path.
|
| 704 |
+
*
|
| 705 |
+
* **Details**
|
| 706 |
+
*
|
| 707 |
+
* Trailing slashes are removed from the prefix; `/` becomes the prefix itself and
|
| 708 |
+
* `*` becomes a wildcard route under the prefix.
|
| 709 |
+
*
|
| 710 |
* @category PathInput
|
| 711 |
+
* @since 4.0.0
|
| 712 |
*/
|
| 713 |
(prefix: string): (self: string) => string
|
| 714 |
/**
|
| 715 |
+
* Adds a path prefix to a route path.
|
| 716 |
+
*
|
| 717 |
+
* **Details**
|
| 718 |
+
*
|
| 719 |
+
* Trailing slashes are removed from the prefix; `/` becomes the prefix itself and
|
| 720 |
+
* `*` becomes a wildcard route under the prefix.
|
| 721 |
+
*
|
| 722 |
* @category PathInput
|
| 723 |
+
* @since 4.0.0
|
| 724 |
*/
|
| 725 |
(self: string, prefix: string): string
|
| 726 |
} = dual(2, (self: string, prefix: string) => {
|
|
|
|
| 731 |
})
|
| 732 |
|
| 733 |
/**
|
| 734 |
+
* Returns a copy of a route with its path prefixed.
|
| 735 |
+
*
|
| 736 |
+
* **Details**
|
| 737 |
+
*
|
| 738 |
+
* The prefix is also tracked on the route so that, when the route handles a
|
| 739 |
+
* request, the matched prefix can be removed from the request URL seen by the
|
| 740 |
+
* handler.
|
| 741 |
+
*
|
| 742 |
* @category Route
|
| 743 |
+
* @since 4.0.0
|
| 744 |
*/
|
| 745 |
export const prefixRoute: {
|
| 746 |
/**
|
| 747 |
+
* Returns a copy of a route with its path prefixed.
|
| 748 |
+
*
|
| 749 |
+
* **Details**
|
| 750 |
+
*
|
| 751 |
+
* The prefix is also tracked on the route so that, when the route handles a
|
| 752 |
+
* request, the matched prefix can be removed from the request URL seen by the
|
| 753 |
+
* handler.
|
| 754 |
+
*
|
| 755 |
* @category Route
|
| 756 |
+
* @since 4.0.0
|
| 757 |
*/
|
| 758 |
(prefix: string): <E, R>(self: Route<E, R>) => Route<E, R>
|
| 759 |
/**
|
| 760 |
+
* Returns a copy of a route with its path prefixed.
|
| 761 |
+
*
|
| 762 |
+
* **Details**
|
| 763 |
+
*
|
| 764 |
+
* The prefix is also tracked on the route so that, when the route handles a
|
| 765 |
+
* request, the matched prefix can be removed from the request URL seen by the
|
| 766 |
+
* handler.
|
| 767 |
+
*
|
| 768 |
* @category Route
|
| 769 |
+
* @since 4.0.0
|
| 770 |
*/
|
| 771 |
<E, R>(self: Route<E, R>, prefix: string): Route<E, R>
|
| 772 |
} = dual(2, <E, R>(self: Route<E, R>, prefix: string): Route<E, R> =>
|
|
|
|
| 783 |
* Represents a request-level dependency, that needs to be provided by
|
| 784 |
* middleware.
|
| 785 |
*
|
|
|
|
| 786 |
* @category Request types
|
| 787 |
+
* @since 4.0.0
|
| 788 |
*/
|
| 789 |
export interface Request<Kind extends string, T> {
|
| 790 |
readonly _: unique symbol
|
|
|
|
| 793 |
}
|
| 794 |
|
| 795 |
/**
|
| 796 |
+
* Helper types for request-level dependency markers used by router layers and
|
| 797 |
+
* middleware.
|
| 798 |
+
*
|
| 799 |
* @since 4.0.0
|
|
|
|
| 800 |
*/
|
| 801 |
export declare namespace Request {
|
| 802 |
/**
|
| 803 |
+
* Wraps a type in a request-level marker of the supplied kind.
|
| 804 |
+
*
|
| 805 |
* @category Request types
|
| 806 |
+
* @since 4.0.0
|
| 807 |
*/
|
| 808 |
export type From<Kind extends string, R> = R extends infer T ? Request<Kind, T> : never
|
| 809 |
|
| 810 |
/**
|
| 811 |
+
* Extracts the payload types from request-level markers that have the supplied
|
| 812 |
+
* kind.
|
| 813 |
+
*
|
| 814 |
* @category Request types
|
| 815 |
+
* @since 4.0.0
|
| 816 |
*/
|
| 817 |
export type Only<Kind extends string, A> = A extends Request<Kind, infer T> ? T : never
|
| 818 |
|
| 819 |
/**
|
| 820 |
+
* Removes request-level markers from a union, leaving only ordinary requirement
|
| 821 |
+
* or error types.
|
| 822 |
+
*
|
| 823 |
* @category Request types
|
| 824 |
+
* @since 4.0.0
|
| 825 |
*/
|
| 826 |
export type Without<A> = A extends Request<infer _Kind, infer _> ? never : A
|
| 827 |
}
|
|
|
|
| 830 |
* Services provided by the HTTP router, which are available in the
|
| 831 |
* request context.
|
| 832 |
*
|
|
|
|
| 833 |
* @category Request types
|
| 834 |
+
* @since 4.0.0
|
| 835 |
*/
|
| 836 |
export type Provided =
|
| 837 |
| HttpServerRequest.HttpServerRequest
|
|
|
|
| 842 |
/**
|
| 843 |
* Services provided to global middleware.
|
| 844 |
*
|
|
|
|
| 845 |
* @category Request types
|
| 846 |
+
* @since 4.0.0
|
| 847 |
*/
|
| 848 |
export type GlobalProvided =
|
| 849 |
| HttpServerRequest.HttpServerRequest
|
|
|
|
| 852 |
const MiddlewareTypeId = "~effect/http/HttpRouter/Middleware"
|
| 853 |
|
| 854 |
/**
|
| 855 |
+
* Composable descriptor for route-scoped HTTP router middleware.
|
| 856 |
+
*
|
| 857 |
+
* **Details**
|
| 858 |
+
*
|
| 859 |
+
* Its `layer` can be provided to route layers, and `combine` composes middleware
|
| 860 |
+
* while tracking provided services, handled errors, and remaining requirements at
|
| 861 |
+
* the type level.
|
| 862 |
+
*
|
| 863 |
+
* @category middleware
|
| 864 |
* @since 4.0.0
|
|
|
|
| 865 |
*/
|
| 866 |
export interface Middleware<
|
| 867 |
Config extends {
|
|
|
|
| 906 |
/**
|
| 907 |
* Create a middleware layer that can be used to modify requests and responses.
|
| 908 |
*
|
| 909 |
+
* **Details**
|
| 910 |
+
*
|
| 911 |
* By default, the middleware only affects the routes that it is provided to.
|
| 912 |
*
|
| 913 |
* If you want to create a middleware that applies globally to all routes, pass
|
| 914 |
* the `global` option as `true`.
|
| 915 |
*
|
| 916 |
+
* **Example** (Applying route and global middleware)
|
| 917 |
+
*
|
| 918 |
* ```ts
|
| 919 |
+
* import { Context, Effect, Layer } from "effect"
|
| 920 |
+
* import { HttpMiddleware, HttpRouter, HttpServerResponse } from "effect/unstable/http"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 921 |
*
|
| 922 |
* // Here we are defining a CORS middleware
|
| 923 |
* const CorsMiddleware = HttpRouter.middleware(HttpMiddleware.cors()).layer
|
|
|
|
| 961 |
* )
|
| 962 |
* ```
|
| 963 |
*
|
| 964 |
+
* @category middleware
|
| 965 |
* @since 4.0.0
|
|
|
|
| 966 |
*/
|
| 967 |
export const middleware:
|
| 968 |
& middleware.Make<never, never>
|
|
|
|
| 1085 |
}
|
| 1086 |
|
| 1087 |
/**
|
| 1088 |
+
* Types used by the `middleware` constructor.
|
| 1089 |
+
*
|
| 1090 |
* @since 4.0.0
|
|
|
|
| 1091 |
*/
|
| 1092 |
export declare namespace middleware {
|
| 1093 |
/**
|
| 1094 |
+
* Overloaded constructor type for router middleware.
|
| 1095 |
+
*
|
| 1096 |
+
* **Details**
|
| 1097 |
+
*
|
| 1098 |
+
* It builds either a route-scoped `Middleware` or, when `global` is `true`, a
|
| 1099 |
+
* layer that installs middleware for all routes. The type tracks provided
|
| 1100 |
+
* services, handled errors, middleware failures, and remaining requirements.
|
| 1101 |
+
*
|
| 1102 |
+
* @category middleware
|
| 1103 |
* @since 4.0.0
|
|
|
|
| 1104 |
*/
|
| 1105 |
export type Make<Provides = never, Handles = never> = {
|
| 1106 |
<E, R, EX, RX, const Global extends boolean = false>(
|
|
|
|
| 1181 |
}
|
| 1182 |
|
| 1183 |
/**
|
| 1184 |
+
* Function that transforms an HTTP response effect into another HTTP response
|
| 1185 |
+
* effect.
|
| 1186 |
+
*
|
| 1187 |
+
* @category middleware
|
| 1188 |
* @since 4.0.0
|
|
|
|
| 1189 |
*/
|
| 1190 |
export type Fn = (
|
| 1191 |
effect: Effect.Effect<HttpServerResponse.HttpServerResponse>
|
|
|
|
| 1193 |
}
|
| 1194 |
|
| 1195 |
/**
|
| 1196 |
+
* Middleware that applies CORS headers to the HTTP response.
|
| 1197 |
*
|
| 1198 |
+
* @category middleware
|
| 1199 |
* @since 4.0.0
|
|
|
|
| 1200 |
*/
|
| 1201 |
export const cors = (
|
| 1202 |
options?: {
|
|
|
|
| 1210 |
): Layer.Layer<never, never, HttpRouter> => middleware(HttpMiddleware.cors(options), { global: true })
|
| 1211 |
|
| 1212 |
/**
|
| 1213 |
+
* Middleware that disables the logger for some routes.
|
| 1214 |
+
*
|
| 1215 |
+
* **Example** (Disabling route logging)
|
| 1216 |
*
|
| 1217 |
* ```ts
|
| 1218 |
+
* import { Effect, Layer } from "effect"
|
| 1219 |
+
* import { HttpRouter, HttpServerResponse } from "effect/unstable/http"
|
|
|
|
|
|
|
| 1220 |
*
|
| 1221 |
* const Route = HttpRouter.add(
|
| 1222 |
* "GET",
|
|
|
|
| 1228 |
* )
|
| 1229 |
* ```
|
| 1230 |
*
|
| 1231 |
+
* @category middleware
|
| 1232 |
* @since 4.0.0
|
|
|
|
| 1233 |
*/
|
| 1234 |
export const disableLogger: Layer.Layer<never> = middleware(HttpMiddleware.withLoggerDisabled).layer
|
| 1235 |
|
| 1236 |
/**
|
| 1237 |
* Provides request-level dependencies to some routes.
|
| 1238 |
*
|
| 1239 |
+
* @category middleware
|
| 1240 |
* @since 4.0.0
|
|
|
|
| 1241 |
*/
|
| 1242 |
export const provideRequest =
|
| 1243 |
<A2, E2, R2>(layer: Layer.Layer<A2, E2, R2>) =>
|
|
|
|
| 1259 |
)
|
| 1260 |
|
| 1261 |
/**
|
| 1262 |
+
* Runs the provided application layer as an HTTP server.
|
| 1263 |
*
|
| 1264 |
+
* @category server
|
| 1265 |
* @since 4.0.0
|
|
|
|
| 1266 |
*/
|
| 1267 |
export const serve = <A, E, R, HE, HR = Request.Only<"Requires", R> | Request.Only<"GlobalRequires", R>>(
|
| 1268 |
appLayer: Layer.Layer<A, E, R>,
|
|
|
|
| 1273 |
/**
|
| 1274 |
* Middleware to apply to the HTTP server.
|
| 1275 |
*
|
| 1276 |
+
* **Gotchas**
|
|
|
|
|
|
|
|
|
|
| 1277 |
*
|
| 1278 |
+
* This middleware is applied to the entire HTTP server chain, including the
|
| 1279 |
+
* sending of the response. Changes to the response are not reflected in the
|
| 1280 |
+
* final response sent to the client. Use `HttpRouter.middleware` when
|
| 1281 |
+
* middleware must modify the response.
|
| 1282 |
*/
|
| 1283 |
readonly middleware?: (
|
| 1284 |
effect: Effect.Effect<
|
|
|
|
| 1316 |
}
|
| 1317 |
|
| 1318 |
/**
|
| 1319 |
+
* Builds a Fetch-compatible request handler from an HTTP router application
|
| 1320 |
+
* layer.
|
| 1321 |
+
*
|
| 1322 |
+
* **Details**
|
| 1323 |
+
*
|
| 1324 |
+
* The result contains a `handler` function that converts Web `Request` values to
|
| 1325 |
+
* Web `Response` values and a `dispose` function for releasing the layer
|
| 1326 |
+
* resources.
|
| 1327 |
+
*
|
| 1328 |
+
* @category server
|
| 1329 |
* @since 4.0.0
|
|
|
|
| 1330 |
*/
|
| 1331 |
export const toWebHandler = <
|
| 1332 |
A,
|
|
|
|
| 1348 |
/**
|
| 1349 |
* Middleware to apply to the HTTP server.
|
| 1350 |
*
|
| 1351 |
+
* **Gotchas**
|
|
|
|
|
|
|
|
|
|
| 1352 |
*
|
| 1353 |
+
* This middleware is applied to the entire HTTP server chain, including the
|
| 1354 |
+
* sending of the response. Changes to the response are not reflected in the
|
| 1355 |
+
* final response sent to the client. Use `HttpRouter.middleware` when
|
| 1356 |
+
* middleware must modify the response.
|
| 1357 |
*/
|
| 1358 |
readonly middleware?: (
|
| 1359 |
effect: Effect.Effect<
|
.opencode/node_modules/effect/src/unstable/http/HttpServer.ts
CHANGED
|
@@ -1,4 +1,12 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Context from "../../Context.ts"
|
|
@@ -17,8 +25,15 @@ import type { HttpServerRequest } from "./HttpServerRequest.ts"
|
|
| 17 |
import type { HttpServerResponse } from "./HttpServerResponse.ts"
|
| 18 |
|
| 19 |
/**
|
| 20 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
* @category models
|
|
|
|
| 22 |
*/
|
| 23 |
export class HttpServer extends Context.Service<HttpServer, {
|
| 24 |
readonly serve: {
|
|
@@ -41,14 +56,22 @@ export class HttpServer extends Context.Service<HttpServer, {
|
|
| 41 |
}>()("effect/http/HttpServer") {}
|
| 42 |
|
| 43 |
/**
|
| 44 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
* @category address
|
|
|
|
| 46 |
*/
|
| 47 |
export type Address = UnixAddress | TcpAddress
|
| 48 |
|
| 49 |
/**
|
| 50 |
-
*
|
|
|
|
| 51 |
* @category address
|
|
|
|
| 52 |
*/
|
| 53 |
export interface TcpAddress {
|
| 54 |
readonly _tag: "TcpAddress"
|
|
@@ -57,8 +80,10 @@ export interface TcpAddress {
|
|
| 57 |
}
|
| 58 |
|
| 59 |
/**
|
| 60 |
-
*
|
|
|
|
| 61 |
* @category address
|
|
|
|
| 62 |
*/
|
| 63 |
export interface UnixAddress {
|
| 64 |
readonly _tag: "UnixAddress"
|
|
@@ -66,8 +91,11 @@ export interface UnixAddress {
|
|
| 66 |
}
|
| 67 |
|
| 68 |
/**
|
| 69 |
-
*
|
|
|
|
|
|
|
| 70 |
* @category constructors
|
|
|
|
| 71 |
*/
|
| 72 |
export const make = (
|
| 73 |
options: {
|
|
@@ -80,20 +108,47 @@ export const make = (
|
|
| 80 |
): HttpServer["Service"] => options
|
| 81 |
|
| 82 |
/**
|
| 83 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
* @category accessors
|
|
|
|
| 85 |
*/
|
| 86 |
export const serve: {
|
| 87 |
/**
|
| 88 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
* @category accessors
|
|
|
|
| 90 |
*/
|
| 91 |
(): <E, R>(
|
| 92 |
effect: Effect.Effect<HttpServerResponse, E, R>
|
| 93 |
) => Layer.Layer<never, never, HttpServer | Exclude<R, HttpServerRequest | Scope.Scope>>
|
| 94 |
/**
|
| 95 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
* @category accessors
|
|
|
|
| 97 |
*/
|
| 98 |
<E, R, App extends Effect.Effect<HttpServerResponse, any, any>>(middleware: Middleware.HttpMiddleware.Applied<App, E, R>): (
|
| 99 |
effect: Effect.Effect<HttpServerResponse, E, R>
|
|
@@ -103,13 +158,31 @@ export const serve: {
|
|
| 103 |
HttpServer | Exclude<Effect.Services<App>, HttpServerRequest | Scope.Scope>
|
| 104 |
>
|
| 105 |
/**
|
| 106 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
* @category accessors
|
|
|
|
| 108 |
*/
|
| 109 |
<E, R>(effect: Effect.Effect<HttpServerResponse, E, R>): Layer.Layer<never, never, HttpServer | Exclude<R, HttpServerRequest | Scope.Scope>>
|
| 110 |
/**
|
| 111 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
* @category accessors
|
|
|
|
| 113 |
*/
|
| 114 |
<E, R, App extends Effect.Effect<HttpServerResponse, any, any>>(
|
| 115 |
effect: Effect.Effect<HttpServerResponse, E, R>,
|
|
@@ -132,20 +205,47 @@ export const serve: {
|
|
| 132 |
) as any)
|
| 133 |
|
| 134 |
/**
|
| 135 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
* @category accessors
|
|
|
|
| 137 |
*/
|
| 138 |
export const serveEffect: {
|
| 139 |
/**
|
| 140 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
* @category accessors
|
|
|
|
| 142 |
*/
|
| 143 |
(): <E, R>(
|
| 144 |
effect: Effect.Effect<HttpServerResponse, E, R>
|
| 145 |
) => Effect.Effect<void, never, Scope.Scope | HttpServer | Exclude<R, HttpServerRequest>>
|
| 146 |
/**
|
| 147 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
* @category accessors
|
|
|
|
| 149 |
*/
|
| 150 |
<E, R, App extends Effect.Effect<HttpServerResponse, any, any>>(middleware: Middleware.HttpMiddleware.Applied<App, E, R>): (
|
| 151 |
effect: Effect.Effect<HttpServerResponse, E, R>
|
|
@@ -155,13 +255,31 @@ export const serveEffect: {
|
|
| 155 |
Scope.Scope | HttpServer | Exclude<Effect.Services<App>, HttpServerRequest>
|
| 156 |
>
|
| 157 |
/**
|
| 158 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
* @category accessors
|
|
|
|
| 160 |
*/
|
| 161 |
<E, R>(effect: Effect.Effect<HttpServerResponse, E, R>): Effect.Effect<void, never, Scope.Scope | HttpServer | Exclude<R, HttpServerRequest>>
|
| 162 |
/**
|
| 163 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
* @category accessors
|
|
|
|
| 165 |
*/
|
| 166 |
<E, R, App extends Effect.Effect<HttpServerResponse, any, any>>(
|
| 167 |
effect: Effect.Effect<HttpServerResponse, E, R>,
|
|
@@ -181,8 +299,15 @@ export const serveEffect: {
|
|
| 181 |
> => HttpServer.use((server) => server.serve(effect, middleware!)) as any)
|
| 182 |
|
| 183 |
/**
|
| 184 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
* @category address
|
|
|
|
| 186 |
*/
|
| 187 |
export const formatAddress = (address: Address): string => {
|
| 188 |
switch (address._tag) {
|
|
@@ -194,28 +319,35 @@ export const formatAddress = (address: Address): string => {
|
|
| 194 |
}
|
| 195 |
|
| 196 |
/**
|
| 197 |
-
*
|
|
|
|
|
|
|
| 198 |
* @category address
|
|
|
|
| 199 |
*/
|
| 200 |
export const addressFormattedWith = <A, E, R>(
|
| 201 |
f: (address: string) => Effect.Effect<A, E, R>
|
| 202 |
): Effect.Effect<A, E, HttpServer | R> =>
|
| 203 |
Effect.flatMap(
|
| 204 |
-
HttpServer
|
| 205 |
(server) => f(formatAddress(server.address))
|
| 206 |
)
|
| 207 |
|
| 208 |
/**
|
| 209 |
-
*
|
|
|
|
| 210 |
* @category address
|
|
|
|
| 211 |
*/
|
| 212 |
export const logAddress: Effect.Effect<void, never, HttpServer> = addressFormattedWith((_) =>
|
| 213 |
Effect.log(`Listening on ${_}`)
|
| 214 |
)
|
| 215 |
|
| 216 |
/**
|
| 217 |
-
*
|
|
|
|
| 218 |
* @category address
|
|
|
|
| 219 |
*/
|
| 220 |
export const withLogAddress = <A, E, R>(
|
| 221 |
layer: Layer.Layer<A, E, R>
|
|
@@ -225,8 +357,19 @@ export const withLogAddress = <A, E, R>(
|
|
| 225 |
)
|
| 226 |
|
| 227 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
* @since 4.0.0
|
| 229 |
-
* @category Testing
|
| 230 |
*/
|
| 231 |
export const makeTestClient: Effect.Effect<
|
| 232 |
HttpClient.HttpClient,
|
|
@@ -245,8 +388,10 @@ export const makeTestClient: Effect.Effect<
|
|
| 245 |
})
|
| 246 |
|
| 247 |
/**
|
|
|
|
|
|
|
|
|
|
| 248 |
* @since 4.0.0
|
| 249 |
-
* @category Testing
|
| 250 |
*/
|
| 251 |
export const layerTestClient: Layer.Layer<
|
| 252 |
HttpClient.HttpClient,
|
|
@@ -255,8 +400,16 @@ export const layerTestClient: Layer.Layer<
|
|
| 255 |
> = Layer.effect(HttpClient.HttpClient)(makeTestClient)
|
| 256 |
|
| 257 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
* @since 4.0.0
|
| 259 |
-
* @category Testing
|
| 260 |
*/
|
| 261 |
export const layerServices: Layer.Layer<
|
| 262 |
| Path.Path
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Service for serving Effect HTTP responses on a concrete HTTP server.
|
| 3 |
+
*
|
| 4 |
+
* Platform adapters provide `HttpServer`, and routers or applications consume
|
| 5 |
+
* it to run an `HttpServerResponse` effect for each incoming request. The
|
| 6 |
+
* service exposes the listening address, while this module also includes helpers
|
| 7 |
+
* for address formatting, server logging, and clients that target the current
|
| 8 |
+
* server in tests.
|
| 9 |
+
*
|
| 10 |
* @since 4.0.0
|
| 11 |
*/
|
| 12 |
import * as Context from "../../Context.ts"
|
|
|
|
| 25 |
import type { HttpServerResponse } from "./HttpServerResponse.ts"
|
| 26 |
|
| 27 |
/**
|
| 28 |
+
* Service tag for an HTTP server runtime.
|
| 29 |
+
*
|
| 30 |
+
* **Details**
|
| 31 |
+
*
|
| 32 |
+
* The service can serve an HTTP response effect and exposes the address where the
|
| 33 |
+
* server is listening.
|
| 34 |
+
*
|
| 35 |
* @category models
|
| 36 |
+
* @since 4.0.0
|
| 37 |
*/
|
| 38 |
export class HttpServer extends Context.Service<HttpServer, {
|
| 39 |
readonly serve: {
|
|
|
|
| 56 |
}>()("effect/http/HttpServer") {}
|
| 57 |
|
| 58 |
/**
|
| 59 |
+
* Address where an HTTP server is listening.
|
| 60 |
+
*
|
| 61 |
+
* **Details**
|
| 62 |
+
*
|
| 63 |
+
* The address is either a TCP host and port or a Unix domain socket path.
|
| 64 |
+
*
|
| 65 |
* @category address
|
| 66 |
+
* @since 4.0.0
|
| 67 |
*/
|
| 68 |
export type Address = UnixAddress | TcpAddress
|
| 69 |
|
| 70 |
/**
|
| 71 |
+
* TCP address for an HTTP server, identified by hostname and port.
|
| 72 |
+
*
|
| 73 |
* @category address
|
| 74 |
+
* @since 4.0.0
|
| 75 |
*/
|
| 76 |
export interface TcpAddress {
|
| 77 |
readonly _tag: "TcpAddress"
|
|
|
|
| 80 |
}
|
| 81 |
|
| 82 |
/**
|
| 83 |
+
* Unix domain socket address for an HTTP server.
|
| 84 |
+
*
|
| 85 |
* @category address
|
| 86 |
+
* @since 4.0.0
|
| 87 |
*/
|
| 88 |
export interface UnixAddress {
|
| 89 |
readonly _tag: "UnixAddress"
|
|
|
|
| 91 |
}
|
| 92 |
|
| 93 |
/**
|
| 94 |
+
* Constructs an `HttpServer` service from a serving implementation and listening
|
| 95 |
+
* address.
|
| 96 |
+
*
|
| 97 |
* @category constructors
|
| 98 |
+
* @since 4.0.0
|
| 99 |
*/
|
| 100 |
export const make = (
|
| 101 |
options: {
|
|
|
|
| 108 |
): HttpServer["Service"] => options
|
| 109 |
|
| 110 |
/**
|
| 111 |
+
* Creates a layer that starts serving an HTTP response effect with the current
|
| 112 |
+
* `HttpServer`.
|
| 113 |
+
*
|
| 114 |
+
* **Details**
|
| 115 |
+
*
|
| 116 |
+
* The request service is supplied by the server for each request; the returned
|
| 117 |
+
* layer still requires the server, a scope, and any non-request dependencies of
|
| 118 |
+
* the response effect or middleware.
|
| 119 |
+
*
|
| 120 |
* @category accessors
|
| 121 |
+
* @since 4.0.0
|
| 122 |
*/
|
| 123 |
export const serve: {
|
| 124 |
/**
|
| 125 |
+
* Creates a layer that starts serving an HTTP response effect with the current
|
| 126 |
+
* `HttpServer`.
|
| 127 |
+
*
|
| 128 |
+
* **Details**
|
| 129 |
+
*
|
| 130 |
+
* The request service is supplied by the server for each request; the returned
|
| 131 |
+
* layer still requires the server, a scope, and any non-request dependencies of
|
| 132 |
+
* the response effect or middleware.
|
| 133 |
+
*
|
| 134 |
* @category accessors
|
| 135 |
+
* @since 4.0.0
|
| 136 |
*/
|
| 137 |
(): <E, R>(
|
| 138 |
effect: Effect.Effect<HttpServerResponse, E, R>
|
| 139 |
) => Layer.Layer<never, never, HttpServer | Exclude<R, HttpServerRequest | Scope.Scope>>
|
| 140 |
/**
|
| 141 |
+
* Creates a layer that starts serving an HTTP response effect with the current
|
| 142 |
+
* `HttpServer`.
|
| 143 |
+
*
|
| 144 |
+
* **Details**
|
| 145 |
+
*
|
| 146 |
+
* The request service is supplied by the server for each request; the returned
|
| 147 |
+
* layer still requires the server, a scope, and any non-request dependencies of
|
| 148 |
+
* the response effect or middleware.
|
| 149 |
+
*
|
| 150 |
* @category accessors
|
| 151 |
+
* @since 4.0.0
|
| 152 |
*/
|
| 153 |
<E, R, App extends Effect.Effect<HttpServerResponse, any, any>>(middleware: Middleware.HttpMiddleware.Applied<App, E, R>): (
|
| 154 |
effect: Effect.Effect<HttpServerResponse, E, R>
|
|
|
|
| 158 |
HttpServer | Exclude<Effect.Services<App>, HttpServerRequest | Scope.Scope>
|
| 159 |
>
|
| 160 |
/**
|
| 161 |
+
* Creates a layer that starts serving an HTTP response effect with the current
|
| 162 |
+
* `HttpServer`.
|
| 163 |
+
*
|
| 164 |
+
* **Details**
|
| 165 |
+
*
|
| 166 |
+
* The request service is supplied by the server for each request; the returned
|
| 167 |
+
* layer still requires the server, a scope, and any non-request dependencies of
|
| 168 |
+
* the response effect or middleware.
|
| 169 |
+
*
|
| 170 |
* @category accessors
|
| 171 |
+
* @since 4.0.0
|
| 172 |
*/
|
| 173 |
<E, R>(effect: Effect.Effect<HttpServerResponse, E, R>): Layer.Layer<never, never, HttpServer | Exclude<R, HttpServerRequest | Scope.Scope>>
|
| 174 |
/**
|
| 175 |
+
* Creates a layer that starts serving an HTTP response effect with the current
|
| 176 |
+
* `HttpServer`.
|
| 177 |
+
*
|
| 178 |
+
* **Details**
|
| 179 |
+
*
|
| 180 |
+
* The request service is supplied by the server for each request; the returned
|
| 181 |
+
* layer still requires the server, a scope, and any non-request dependencies of
|
| 182 |
+
* the response effect or middleware.
|
| 183 |
+
*
|
| 184 |
* @category accessors
|
| 185 |
+
* @since 4.0.0
|
| 186 |
*/
|
| 187 |
<E, R, App extends Effect.Effect<HttpServerResponse, any, any>>(
|
| 188 |
effect: Effect.Effect<HttpServerResponse, E, R>,
|
|
|
|
| 205 |
) as any)
|
| 206 |
|
| 207 |
/**
|
| 208 |
+
* Effect that starts serving an HTTP response effect with the current
|
| 209 |
+
* `HttpServer`.
|
| 210 |
+
*
|
| 211 |
+
* **Details**
|
| 212 |
+
*
|
| 213 |
+
* The request service is supplied by the server for each request; the effect
|
| 214 |
+
* requires a scope and any non-request dependencies of the response effect or
|
| 215 |
+
* middleware.
|
| 216 |
+
*
|
| 217 |
* @category accessors
|
| 218 |
+
* @since 4.0.0
|
| 219 |
*/
|
| 220 |
export const serveEffect: {
|
| 221 |
/**
|
| 222 |
+
* Effect that starts serving an HTTP response effect with the current
|
| 223 |
+
* `HttpServer`.
|
| 224 |
+
*
|
| 225 |
+
* **Details**
|
| 226 |
+
*
|
| 227 |
+
* The request service is supplied by the server for each request; the effect
|
| 228 |
+
* requires a scope and any non-request dependencies of the response effect or
|
| 229 |
+
* middleware.
|
| 230 |
+
*
|
| 231 |
* @category accessors
|
| 232 |
+
* @since 4.0.0
|
| 233 |
*/
|
| 234 |
(): <E, R>(
|
| 235 |
effect: Effect.Effect<HttpServerResponse, E, R>
|
| 236 |
) => Effect.Effect<void, never, Scope.Scope | HttpServer | Exclude<R, HttpServerRequest>>
|
| 237 |
/**
|
| 238 |
+
* Effect that starts serving an HTTP response effect with the current
|
| 239 |
+
* `HttpServer`.
|
| 240 |
+
*
|
| 241 |
+
* **Details**
|
| 242 |
+
*
|
| 243 |
+
* The request service is supplied by the server for each request; the effect
|
| 244 |
+
* requires a scope and any non-request dependencies of the response effect or
|
| 245 |
+
* middleware.
|
| 246 |
+
*
|
| 247 |
* @category accessors
|
| 248 |
+
* @since 4.0.0
|
| 249 |
*/
|
| 250 |
<E, R, App extends Effect.Effect<HttpServerResponse, any, any>>(middleware: Middleware.HttpMiddleware.Applied<App, E, R>): (
|
| 251 |
effect: Effect.Effect<HttpServerResponse, E, R>
|
|
|
|
| 255 |
Scope.Scope | HttpServer | Exclude<Effect.Services<App>, HttpServerRequest>
|
| 256 |
>
|
| 257 |
/**
|
| 258 |
+
* Effect that starts serving an HTTP response effect with the current
|
| 259 |
+
* `HttpServer`.
|
| 260 |
+
*
|
| 261 |
+
* **Details**
|
| 262 |
+
*
|
| 263 |
+
* The request service is supplied by the server for each request; the effect
|
| 264 |
+
* requires a scope and any non-request dependencies of the response effect or
|
| 265 |
+
* middleware.
|
| 266 |
+
*
|
| 267 |
* @category accessors
|
| 268 |
+
* @since 4.0.0
|
| 269 |
*/
|
| 270 |
<E, R>(effect: Effect.Effect<HttpServerResponse, E, R>): Effect.Effect<void, never, Scope.Scope | HttpServer | Exclude<R, HttpServerRequest>>
|
| 271 |
/**
|
| 272 |
+
* Effect that starts serving an HTTP response effect with the current
|
| 273 |
+
* `HttpServer`.
|
| 274 |
+
*
|
| 275 |
+
* **Details**
|
| 276 |
+
*
|
| 277 |
+
* The request service is supplied by the server for each request; the effect
|
| 278 |
+
* requires a scope and any non-request dependencies of the response effect or
|
| 279 |
+
* middleware.
|
| 280 |
+
*
|
| 281 |
* @category accessors
|
| 282 |
+
* @since 4.0.0
|
| 283 |
*/
|
| 284 |
<E, R, App extends Effect.Effect<HttpServerResponse, any, any>>(
|
| 285 |
effect: Effect.Effect<HttpServerResponse, E, R>,
|
|
|
|
| 299 |
> => HttpServer.use((server) => server.serve(effect, middleware!)) as any)
|
| 300 |
|
| 301 |
/**
|
| 302 |
+
* Formats a server address as a display string.
|
| 303 |
+
*
|
| 304 |
+
* **Details**
|
| 305 |
+
*
|
| 306 |
+
* TCP addresses are formatted as `http://host:port`; Unix socket addresses are
|
| 307 |
+
* formatted as `unix://path`.
|
| 308 |
+
*
|
| 309 |
* @category address
|
| 310 |
+
* @since 4.0.0
|
| 311 |
*/
|
| 312 |
export const formatAddress = (address: Address): string => {
|
| 313 |
switch (address._tag) {
|
|
|
|
| 319 |
}
|
| 320 |
|
| 321 |
/**
|
| 322 |
+
* Reads the current server address, formats it with `formatAddress`, and passes
|
| 323 |
+
* the formatted address to the supplied effectful function.
|
| 324 |
+
*
|
| 325 |
* @category address
|
| 326 |
+
* @since 4.0.0
|
| 327 |
*/
|
| 328 |
export const addressFormattedWith = <A, E, R>(
|
| 329 |
f: (address: string) => Effect.Effect<A, E, R>
|
| 330 |
): Effect.Effect<A, E, HttpServer | R> =>
|
| 331 |
Effect.flatMap(
|
| 332 |
+
HttpServer,
|
| 333 |
(server) => f(formatAddress(server.address))
|
| 334 |
)
|
| 335 |
|
| 336 |
/**
|
| 337 |
+
* Logs the formatted address of the current HTTP server.
|
| 338 |
+
*
|
| 339 |
* @category address
|
| 340 |
+
* @since 4.0.0
|
| 341 |
*/
|
| 342 |
export const logAddress: Effect.Effect<void, never, HttpServer> = addressFormattedWith((_) =>
|
| 343 |
Effect.log(`Listening on ${_}`)
|
| 344 |
)
|
| 345 |
|
| 346 |
/**
|
| 347 |
+
* Adds address logging to a layer that provides an `HttpServer`.
|
| 348 |
+
*
|
| 349 |
* @category address
|
| 350 |
+
* @since 4.0.0
|
| 351 |
*/
|
| 352 |
export const withLogAddress = <A, E, R>(
|
| 353 |
layer: Layer.Layer<A, E, R>
|
|
|
|
| 357 |
)
|
| 358 |
|
| 359 |
/**
|
| 360 |
+
* Builds an `HttpClient` that sends requests to the current test HTTP server.
|
| 361 |
+
*
|
| 362 |
+
* **Details**
|
| 363 |
+
*
|
| 364 |
+
* For TCP servers, requests are prefixed with the server URL and `0.0.0.0` is
|
| 365 |
+
* rewritten to `127.0.0.1`.
|
| 366 |
+
*
|
| 367 |
+
* **Gotchas**
|
| 368 |
+
*
|
| 369 |
+
* Unix socket addresses are not supported.
|
| 370 |
+
*
|
| 371 |
+
* @category testing
|
| 372 |
* @since 4.0.0
|
|
|
|
| 373 |
*/
|
| 374 |
export const makeTestClient: Effect.Effect<
|
| 375 |
HttpClient.HttpClient,
|
|
|
|
| 388 |
})
|
| 389 |
|
| 390 |
/**
|
| 391 |
+
* Layer that provides the test `HttpClient` created by `makeTestClient`.
|
| 392 |
+
*
|
| 393 |
+
* @category testing
|
| 394 |
* @since 4.0.0
|
|
|
|
| 395 |
*/
|
| 396 |
export const layerTestClient: Layer.Layer<
|
| 397 |
HttpClient.HttpClient,
|
|
|
|
| 400 |
> = Layer.effect(HttpClient.HttpClient)(makeTestClient)
|
| 401 |
|
| 402 |
/**
|
| 403 |
+
* Layer that provides the platform services commonly needed by HTTP
|
| 404 |
+
* server tests.
|
| 405 |
+
*
|
| 406 |
+
* **Details**
|
| 407 |
+
*
|
| 408 |
+
* It includes `HttpPlatform`, `Path`, a weak ETag generator, and a no-op
|
| 409 |
+
* `FileSystem`.
|
| 410 |
+
*
|
| 411 |
+
* @category testing
|
| 412 |
* @since 4.0.0
|
|
|
|
| 413 |
*/
|
| 414 |
export const layerServices: Layer.Layer<
|
| 415 |
| Path.Path
|
.opencode/node_modules/effect/src/unstable/http/HttpServerError.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Cause from "../../Cause.ts"
|
|
@@ -17,8 +26,16 @@ import * as Response from "./HttpServerResponse.ts"
|
|
| 17 |
const TypeId = "~effect/http/HttpServerError"
|
| 18 |
|
| 19 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
* @since 4.0.0
|
| 21 |
-
* @category error
|
| 22 |
*/
|
| 23 |
export class HttpServerError extends Data.TaggedError("HttpServerError")<{
|
| 24 |
readonly reason: HttpServerErrorReason
|
|
@@ -62,8 +79,14 @@ export class HttpServerError extends Data.TaggedError("HttpServerError")<{
|
|
| 62 |
}
|
| 63 |
|
| 64 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
* @since 4.0.0
|
| 66 |
-
* @category error
|
| 67 |
*/
|
| 68 |
export class RequestParseError extends Data.TaggedError("RequestParseError")<{
|
| 69 |
readonly request: Request.HttpServerRequest
|
|
@@ -71,6 +94,8 @@ export class RequestParseError extends Data.TaggedError("RequestParseError")<{
|
|
| 71 |
readonly cause?: unknown
|
| 72 |
}> implements Respondable.Respondable {
|
| 73 |
/**
|
|
|
|
|
|
|
| 74 |
* @since 4.0.0
|
| 75 |
*/
|
| 76 |
[Respondable.symbol]() {
|
|
@@ -87,8 +112,15 @@ export class RequestParseError extends Data.TaggedError("RequestParseError")<{
|
|
| 87 |
}
|
| 88 |
|
| 89 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
* @since 4.0.0
|
| 91 |
-
* @category error
|
| 92 |
*/
|
| 93 |
export class RouteNotFound extends Data.TaggedError("RouteNotFound")<{
|
| 94 |
readonly request: Request.HttpServerRequest
|
|
@@ -111,8 +143,14 @@ export class RouteNotFound extends Data.TaggedError("RouteNotFound")<{
|
|
| 111 |
}
|
| 112 |
|
| 113 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
* @since 4.0.0
|
| 115 |
-
* @category error
|
| 116 |
*/
|
| 117 |
export class InternalError extends Data.TaggedError("InternalError")<{
|
| 118 |
readonly request: Request.HttpServerRequest
|
|
@@ -120,6 +158,8 @@ export class InternalError extends Data.TaggedError("InternalError")<{
|
|
| 120 |
readonly cause?: unknown
|
| 121 |
}> implements Respondable.Respondable {
|
| 122 |
/**
|
|
|
|
|
|
|
| 123 |
* @since 4.0.0
|
| 124 |
*/
|
| 125 |
[Respondable.symbol]() {
|
|
@@ -136,14 +176,23 @@ export class InternalError extends Data.TaggedError("InternalError")<{
|
|
| 136 |
}
|
| 137 |
|
| 138 |
/**
|
| 139 |
-
*
|
|
|
|
| 140 |
* @category predicates
|
|
|
|
| 141 |
*/
|
| 142 |
export const isHttpServerError = (u: unknown): u is HttpServerError => hasProperty(u, TypeId)
|
| 143 |
|
| 144 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
* @since 4.0.0
|
| 146 |
-
* @category error
|
| 147 |
*/
|
| 148 |
export class ResponseError extends Data.TaggedError("ResponseError")<{
|
| 149 |
readonly request: Request.HttpServerRequest
|
|
@@ -166,28 +215,42 @@ export class ResponseError extends Data.TaggedError("ResponseError")<{
|
|
| 166 |
}
|
| 167 |
|
| 168 |
/**
|
|
|
|
|
|
|
|
|
|
| 169 |
* @since 4.0.0
|
| 170 |
-
* @category error
|
| 171 |
*/
|
| 172 |
export type RequestError = RequestParseError | RouteNotFound | InternalError
|
| 173 |
|
| 174 |
/**
|
|
|
|
|
|
|
|
|
|
| 175 |
* @since 4.0.0
|
| 176 |
-
* @category error
|
| 177 |
*/
|
| 178 |
export type HttpServerErrorReason = RequestError | ResponseError
|
| 179 |
|
| 180 |
/**
|
|
|
|
|
|
|
|
|
|
| 181 |
* @since 4.0.0
|
| 182 |
-
* @category error
|
| 183 |
*/
|
| 184 |
export class ServeError extends Data.TaggedError("ServeError")<{
|
| 185 |
readonly cause: unknown
|
| 186 |
}> {}
|
| 187 |
|
| 188 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
* @since 4.0.0
|
| 190 |
-
* @category Annotations
|
| 191 |
*/
|
| 192 |
export class ClientAbort extends Context.Service<ClientAbort, true>()("effect/http/HttpServerError/ClientAbort") {
|
| 193 |
static annotation = this.context(true).pipe(
|
|
@@ -205,6 +268,16 @@ const formatRequestMessage = (reason: string, description: string | undefined, i
|
|
| 205 |
}
|
| 206 |
|
| 207 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
* @since 4.0.0
|
| 209 |
*/
|
| 210 |
export const causeResponse = <E>(
|
|
@@ -253,6 +326,15 @@ export const causeResponse = <E>(
|
|
| 253 |
}
|
| 254 |
|
| 255 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
* @since 4.0.0
|
| 257 |
*/
|
| 258 |
export const causeResponseStripped = <E>(
|
|
@@ -278,6 +360,10 @@ const clientAbortError = Effect.succeed(Response.empty({ status: 499 }))
|
|
| 278 |
const serverAbortError = Effect.succeed(Response.empty({ status: 503 }))
|
| 279 |
|
| 280 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
* @since 4.0.0
|
| 282 |
*/
|
| 283 |
export const exitResponse = <E>(exit: Exit.Exit<Response.HttpServerResponse, E>): Response.HttpServerResponse => {
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Describes failures raised while handling HTTP server requests.
|
| 3 |
+
*
|
| 4 |
+
* `HttpServerError` covers failures that happen while accepting a request,
|
| 5 |
+
* matching a route, running a handler, or building and sending a response.
|
| 6 |
+
* Request-scoped failures keep the request that caused them, and response
|
| 7 |
+
* failures keep the response being produced. This module also includes helpers
|
| 8 |
+
* for turning failed causes or exits into HTTP responses and an annotation for
|
| 9 |
+
* interrupts caused by client aborts.
|
| 10 |
+
*
|
| 11 |
* @since 4.0.0
|
| 12 |
*/
|
| 13 |
import * as Cause from "../../Cause.ts"
|
|
|
|
| 26 |
const TypeId = "~effect/http/HttpServerError"
|
| 27 |
|
| 28 |
/**
|
| 29 |
+
* Tagged error for failures that occur while handling an HTTP server request.
|
| 30 |
+
*
|
| 31 |
+
* **Details**
|
| 32 |
+
*
|
| 33 |
+
* It wraps a `HttpServerErrorReason`, exposes the associated request and optional
|
| 34 |
+
* response, and can be converted to an HTTP response through the `Respondable`
|
| 35 |
+
* protocol.
|
| 36 |
+
*
|
| 37 |
+
* @category errors
|
| 38 |
* @since 4.0.0
|
|
|
|
| 39 |
*/
|
| 40 |
export class HttpServerError extends Data.TaggedError("HttpServerError")<{
|
| 41 |
readonly reason: HttpServerErrorReason
|
|
|
|
| 79 |
}
|
| 80 |
|
| 81 |
/**
|
| 82 |
+
* Error describing a failure to parse or read an incoming request.
|
| 83 |
+
*
|
| 84 |
+
* **Details**
|
| 85 |
+
*
|
| 86 |
+
* When converted to a response it produces an empty `400` response.
|
| 87 |
+
*
|
| 88 |
+
* @category errors
|
| 89 |
* @since 4.0.0
|
|
|
|
| 90 |
*/
|
| 91 |
export class RequestParseError extends Data.TaggedError("RequestParseError")<{
|
| 92 |
readonly request: Request.HttpServerRequest
|
|
|
|
| 94 |
readonly cause?: unknown
|
| 95 |
}> implements Respondable.Respondable {
|
| 96 |
/**
|
| 97 |
+
* Converts the request error into a `400 Bad Request` response.
|
| 98 |
+
*
|
| 99 |
* @since 4.0.0
|
| 100 |
*/
|
| 101 |
[Respondable.symbol]() {
|
|
|
|
| 112 |
}
|
| 113 |
|
| 114 |
/**
|
| 115 |
+
* Error indicating that no route matched the incoming request.
|
| 116 |
+
*
|
| 117 |
+
* **Details**
|
| 118 |
+
*
|
| 119 |
+
* When converted to a response it produces an empty `404` response, and it is
|
| 120 |
+
* ignored by the error reporter.
|
| 121 |
+
*
|
| 122 |
+
* @category errors
|
| 123 |
* @since 4.0.0
|
|
|
|
| 124 |
*/
|
| 125 |
export class RouteNotFound extends Data.TaggedError("RouteNotFound")<{
|
| 126 |
readonly request: Request.HttpServerRequest
|
|
|
|
| 143 |
}
|
| 144 |
|
| 145 |
/**
|
| 146 |
+
* Error describing an unexpected server-side failure while handling a request.
|
| 147 |
+
*
|
| 148 |
+
* **Details**
|
| 149 |
+
*
|
| 150 |
+
* When converted to a response it produces an empty `500` response.
|
| 151 |
+
*
|
| 152 |
+
* @category errors
|
| 153 |
* @since 4.0.0
|
|
|
|
| 154 |
*/
|
| 155 |
export class InternalError extends Data.TaggedError("InternalError")<{
|
| 156 |
readonly request: Request.HttpServerRequest
|
|
|
|
| 158 |
readonly cause?: unknown
|
| 159 |
}> implements Respondable.Respondable {
|
| 160 |
/**
|
| 161 |
+
* Converts the server error into a `500 Internal Server Error` response.
|
| 162 |
+
*
|
| 163 |
* @since 4.0.0
|
| 164 |
*/
|
| 165 |
[Respondable.symbol]() {
|
|
|
|
| 176 |
}
|
| 177 |
|
| 178 |
/**
|
| 179 |
+
* Returns `true` when the supplied value is an `HttpServerError`.
|
| 180 |
+
*
|
| 181 |
* @category predicates
|
| 182 |
+
* @since 4.0.0
|
| 183 |
*/
|
| 184 |
export const isHttpServerError = (u: unknown): u is HttpServerError => hasProperty(u, TypeId)
|
| 185 |
|
| 186 |
/**
|
| 187 |
+
* Error describing a failure related to an HTTP response.
|
| 188 |
+
*
|
| 189 |
+
* **Details**
|
| 190 |
+
*
|
| 191 |
+
* It carries the request and response involved in the failure. When converted to
|
| 192 |
+
* a response it produces an empty `500` response.
|
| 193 |
+
*
|
| 194 |
+
* @category errors
|
| 195 |
* @since 4.0.0
|
|
|
|
| 196 |
*/
|
| 197 |
export class ResponseError extends Data.TaggedError("ResponseError")<{
|
| 198 |
readonly request: Request.HttpServerRequest
|
|
|
|
| 215 |
}
|
| 216 |
|
| 217 |
/**
|
| 218 |
+
* Union of errors that are tied directly to an incoming server request.
|
| 219 |
+
*
|
| 220 |
+
* @category errors
|
| 221 |
* @since 4.0.0
|
|
|
|
| 222 |
*/
|
| 223 |
export type RequestError = RequestParseError | RouteNotFound | InternalError
|
| 224 |
|
| 225 |
/**
|
| 226 |
+
* Reason carried by an `HttpServerError`, either a request-level error or a response-level error.
|
| 227 |
+
*
|
| 228 |
+
* @category errors
|
| 229 |
* @since 4.0.0
|
|
|
|
| 230 |
*/
|
| 231 |
export type HttpServerErrorReason = RequestError | ResponseError
|
| 232 |
|
| 233 |
/**
|
| 234 |
+
* Error wrapping a low-level failure from the HTTP server implementation.
|
| 235 |
+
*
|
| 236 |
+
* @category errors
|
| 237 |
* @since 4.0.0
|
|
|
|
| 238 |
*/
|
| 239 |
export class ServeError extends Data.TaggedError("ServeError")<{
|
| 240 |
readonly cause: unknown
|
| 241 |
}> {}
|
| 242 |
|
| 243 |
/**
|
| 244 |
+
* Context annotation used to mark an interrupt as caused by the client aborting
|
| 245 |
+
* the request.
|
| 246 |
+
*
|
| 247 |
+
* **Details**
|
| 248 |
+
*
|
| 249 |
+
* `causeResponse` uses this annotation to map a pure client abort to a `499`
|
| 250 |
+
* response instead of a server abort response.
|
| 251 |
+
*
|
| 252 |
+
* @category annotations
|
| 253 |
* @since 4.0.0
|
|
|
|
| 254 |
*/
|
| 255 |
export class ClientAbort extends Context.Service<ClientAbort, true>()("effect/http/HttpServerError/ClientAbort") {
|
| 256 |
static annotation = this.context(true).pipe(
|
|
|
|
| 268 |
}
|
| 269 |
|
| 270 |
/**
|
| 271 |
+
* Converts a failed handler cause into the HTTP response that should be sent and
|
| 272 |
+
* the cause that should be reported.
|
| 273 |
+
*
|
| 274 |
+
* **Details**
|
| 275 |
+
*
|
| 276 |
+
* Respondable failures and defects can choose their own response, defects that
|
| 277 |
+
* are already `HttpServerResponse` values are used directly, and pure interrupts
|
| 278 |
+
* produce either `499` for client aborts or `503` for server aborts.
|
| 279 |
+
*
|
| 280 |
+
* @category error handling
|
| 281 |
* @since 4.0.0
|
| 282 |
*/
|
| 283 |
export const causeResponse = <E>(
|
|
|
|
| 326 |
}
|
| 327 |
|
| 328 |
/**
|
| 329 |
+
* Derives an HTTP response from a failed handler cause synchronously.
|
| 330 |
+
*
|
| 331 |
+
* **Details**
|
| 332 |
+
*
|
| 333 |
+
* If the cause contains a defect that is already an `HttpServerResponse`, that
|
| 334 |
+
* response is used and removed from the remaining cause. Otherwise the response
|
| 335 |
+
* defaults to `500`.
|
| 336 |
+
*
|
| 337 |
+
* @category error handling
|
| 338 |
* @since 4.0.0
|
| 339 |
*/
|
| 340 |
export const causeResponseStripped = <E>(
|
|
|
|
| 360 |
const serverAbortError = Effect.succeed(Response.empty({ status: 503 }))
|
| 361 |
|
| 362 |
/**
|
| 363 |
+
* Extracts the response from a successful handler exit, or derives a response
|
| 364 |
+
* from the failure cause.
|
| 365 |
+
*
|
| 366 |
+
* @category error handling
|
| 367 |
* @since 4.0.0
|
| 368 |
*/
|
| 369 |
export const exitResponse = <E>(exit: Exit.Exit<Response.HttpServerResponse, E>): Response.HttpServerResponse => {
|
.opencode/node_modules/effect/src/unstable/http/HttpServerRequest.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import type * as Arr from "../../Array.ts"
|
|
@@ -28,21 +37,38 @@ import * as UrlParams from "./UrlParams.ts"
|
|
| 28 |
|
| 29 |
export {
|
| 30 |
/**
|
| 31 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
* @category fiber refs
|
|
|
|
| 33 |
*/
|
| 34 |
MaxBodySize
|
| 35 |
} from "./HttpIncomingMessage.ts"
|
| 36 |
|
| 37 |
/**
|
|
|
|
|
|
|
|
|
|
| 38 |
* @since 4.0.0
|
| 39 |
-
* @category Type IDs
|
| 40 |
*/
|
| 41 |
export const TypeId = "~effect/http/HttpServerRequest"
|
| 42 |
|
| 43 |
/**
|
| 44 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
* @category models
|
|
|
|
| 46 |
*/
|
| 47 |
export interface HttpServerRequest extends HttpIncomingMessage.HttpIncomingMessage<HttpServerError> {
|
| 48 |
readonly [TypeId]: typeof TypeId
|
|
@@ -71,16 +97,35 @@ export interface HttpServerRequest extends HttpIncomingMessage.HttpIncomingMessa
|
|
| 71 |
}
|
| 72 |
|
| 73 |
/**
|
| 74 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
* @category context
|
|
|
|
| 76 |
*/
|
| 77 |
export const HttpServerRequest: Context.Service<HttpServerRequest, HttpServerRequest> = Context.Service(
|
| 78 |
"effect/http/HttpServerRequest"
|
| 79 |
)
|
| 80 |
|
| 81 |
/**
|
| 82 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
* @category search params
|
|
|
|
| 84 |
*/
|
| 85 |
export class ParsedSearchParams extends Context.Service<
|
| 86 |
ParsedSearchParams,
|
|
@@ -88,8 +133,14 @@ export class ParsedSearchParams extends Context.Service<
|
|
| 88 |
>()("effect/http/ParsedSearchParams") {}
|
| 89 |
|
| 90 |
/**
|
| 91 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
* @category search params
|
|
|
|
| 93 |
*/
|
| 94 |
export const searchParamsFromURL = (url: URL): ReadonlyRecord<string, string | Array<string>> => {
|
| 95 |
const out: Record<string, string | Array<string>> = {}
|
|
@@ -109,8 +160,15 @@ export const searchParamsFromURL = (url: URL): ReadonlyRecord<string, string | A
|
|
| 109 |
}
|
| 110 |
|
| 111 |
/**
|
| 112 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
* @category accessors
|
|
|
|
| 114 |
*/
|
| 115 |
export const upgradeChannel = <IE = never>(): Channel.Channel<
|
| 116 |
Arr.NonEmptyReadonlyArray<Uint8Array>,
|
|
@@ -121,39 +179,45 @@ export const upgradeChannel = <IE = never>(): Channel.Channel<
|
|
| 121 |
unknown,
|
| 122 |
HttpServerRequest
|
| 123 |
> =>
|
| 124 |
-
HttpServerRequest.
|
| 125 |
Effect.flatMap((_) => _.upgrade),
|
| 126 |
Effect.map(Socket.toChannelWith<IE>()),
|
| 127 |
Channel.unwrap
|
| 128 |
)
|
| 129 |
|
| 130 |
/**
|
|
|
|
|
|
|
|
|
|
| 131 |
* @since 4.0.0
|
| 132 |
-
* @category schema
|
| 133 |
*/
|
| 134 |
export const schemaCookies = <A, I extends Readonly<Record<string, string | undefined>>, RD, RE>(
|
| 135 |
schema: Schema.Codec<A, I, RD, RE>,
|
| 136 |
options?: ParseOptions | undefined
|
| 137 |
): Effect.Effect<A, Schema.SchemaError, RD | HttpServerRequest> => {
|
| 138 |
const parse = Schema.decodeUnknownEffect(schema)
|
| 139 |
-
return Effect.flatMap(HttpServerRequest
|
| 140 |
}
|
| 141 |
|
| 142 |
/**
|
|
|
|
|
|
|
|
|
|
| 143 |
* @since 4.0.0
|
| 144 |
-
* @category schema
|
| 145 |
*/
|
| 146 |
export const schemaHeaders = <A, I extends Readonly<Record<string, string | undefined>>, RD, RE>(
|
| 147 |
schema: Schema.Codec<A, I, RD, RE>,
|
| 148 |
options?: ParseOptions | undefined
|
| 149 |
): Effect.Effect<A, Schema.SchemaError, HttpServerRequest | RD> => {
|
| 150 |
const parse = Schema.decodeUnknownEffect(schema)
|
| 151 |
-
return Effect.flatMap(HttpServerRequest
|
| 152 |
}
|
| 153 |
|
| 154 |
/**
|
|
|
|
|
|
|
|
|
|
| 155 |
* @since 4.0.0
|
| 156 |
-
* @category schema
|
| 157 |
*/
|
| 158 |
export const schemaSearchParams = <
|
| 159 |
A,
|
|
@@ -165,18 +229,25 @@ export const schemaSearchParams = <
|
|
| 165 |
options?: ParseOptions | undefined
|
| 166 |
): Effect.Effect<A, Schema.SchemaError, ParsedSearchParams | RD> => {
|
| 167 |
const parse = Schema.decodeUnknownEffect(schema)
|
| 168 |
-
return Effect.flatMap(ParsedSearchParams
|
| 169 |
}
|
| 170 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
* @since 4.0.0
|
| 172 |
-
* @category schema
|
| 173 |
*/
|
| 174 |
export const schemaBodyJson = <A, I, RD, RE>(
|
| 175 |
schema: Schema.Codec<A, I, RD, RE>,
|
| 176 |
options?: ParseOptions | undefined
|
| 177 |
): Effect.Effect<A, HttpServerError | Schema.SchemaError, HttpServerRequest | RD> => {
|
| 178 |
const parse = HttpIncomingMessage.schemaBodyJson(schema, options)
|
| 179 |
-
return Effect.flatMap(HttpServerRequest
|
| 180 |
}
|
| 181 |
|
| 182 |
const isMultipart = (request: HttpServerRequest) =>
|
|
@@ -184,8 +255,15 @@ const isMultipart = (request: HttpServerRequest) =>
|
|
| 184 |
getFormDataBody(request) !== undefined
|
| 185 |
|
| 186 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
* @since 4.0.0
|
| 188 |
-
* @category schema
|
| 189 |
*/
|
| 190 |
export const schemaBodyForm = <A, I extends Partial<Multipart.Persisted>, RD, RE>(
|
| 191 |
schema: Schema.Codec<A, I, RD, RE>,
|
|
@@ -193,7 +271,7 @@ export const schemaBodyForm = <A, I extends Partial<Multipart.Persisted>, RD, RE
|
|
| 193 |
) => {
|
| 194 |
const parseMultipart = Multipart.schemaPersisted(schema)
|
| 195 |
const parseUrlParams = HttpIncomingMessage.schemaBodyUrlParams(schema as Schema.Codec<A, any, RD, RE>, options)
|
| 196 |
-
return Effect.flatMap(HttpServerRequest
|
| 197 |
A,
|
| 198 |
Multipart.MultipartError | Schema.SchemaError | HttpServerError,
|
| 199 |
RD | HttpServerRequest | Scope.Scope | FileSystem.FileSystem | Path.Path
|
|
@@ -206,8 +284,11 @@ export const schemaBodyForm = <A, I extends Partial<Multipart.Persisted>, RD, RE
|
|
| 206 |
}
|
| 207 |
|
| 208 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
* @since 4.0.0
|
| 210 |
-
* @category schema
|
| 211 |
*/
|
| 212 |
export const schemaBodyUrlParams = <
|
| 213 |
A,
|
|
@@ -219,12 +300,20 @@ export const schemaBodyUrlParams = <
|
|
| 219 |
options?: ParseOptions | undefined
|
| 220 |
): Effect.Effect<A, HttpServerError | Schema.SchemaError, HttpServerRequest | RD> => {
|
| 221 |
const parse = HttpIncomingMessage.schemaBodyUrlParams(schema, options)
|
| 222 |
-
return Effect.flatMap(HttpServerRequest
|
| 223 |
}
|
| 224 |
|
| 225 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
* @since 4.0.0
|
| 227 |
-
* @category schema
|
| 228 |
*/
|
| 229 |
export const schemaBodyMultipart = <A, I extends Partial<Multipart.Persisted>, RD, RE>(
|
| 230 |
schema: Schema.Codec<A, I, RD, RE>,
|
|
@@ -235,15 +324,23 @@ export const schemaBodyMultipart = <A, I extends Partial<Multipart.Persisted>, R
|
|
| 235 |
HttpServerRequest | Scope.Scope | FileSystem.FileSystem | Path.Path | RD
|
| 236 |
> => {
|
| 237 |
const parse = Multipart.schemaPersisted(schema)
|
| 238 |
-
return HttpServerRequest.
|
| 239 |
Effect.flatMap((_) => _.multipart),
|
| 240 |
Effect.flatMap((_) => parse(_, options))
|
| 241 |
)
|
| 242 |
}
|
| 243 |
|
| 244 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
* @since 4.0.0
|
| 246 |
-
* @category schema
|
| 247 |
*/
|
| 248 |
export const schemaBodyFormJson = <A, I, RD, RE>(
|
| 249 |
schema: Schema.Codec<A, I, RD, RE>,
|
|
@@ -256,7 +353,7 @@ export const schemaBodyFormJson = <A, I, RD, RE>(
|
|
| 256 |
Schema.decodeEffect
|
| 257 |
)
|
| 258 |
return Effect.flatMap(
|
| 259 |
-
HttpServerRequest
|
| 260 |
(request): Effect.Effect<
|
| 261 |
A,
|
| 262 |
Schema.SchemaError | HttpServerError,
|
|
@@ -281,8 +378,15 @@ export const schemaBodyFormJson = <A, I, RD, RE>(
|
|
| 281 |
}
|
| 282 |
|
| 283 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
* @since 4.0.0
|
| 285 |
-
* @category conversions
|
| 286 |
*/
|
| 287 |
export const fromClientRequest = (request: HttpClientRequest.HttpClientRequest): HttpServerRequest => {
|
| 288 |
const url = Option.match(HttpClientRequest.toUrl(request), {
|
|
@@ -293,15 +397,29 @@ export const fromClientRequest = (request: HttpClientRequest.HttpClientRequest):
|
|
| 293 |
}
|
| 294 |
|
| 295 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
* @since 4.0.0
|
| 297 |
-
* @category conversions
|
| 298 |
*/
|
| 299 |
export const fromWeb = (request: globalThis.Request): HttpServerRequest =>
|
| 300 |
new ServerRequestImpl(request, removeHost(request.url))
|
| 301 |
|
| 302 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
* @since 4.0.0
|
| 304 |
-
* @category conversions
|
| 305 |
*/
|
| 306 |
export const toClientRequest = (request: HttpServerRequest): HttpClientRequest.HttpClientRequest =>
|
| 307 |
HttpClientRequest.setUrl(
|
|
@@ -878,8 +996,16 @@ const isFormData = (u: unknown): u is FormData => typeof FormData !== "undefined
|
|
| 878 |
const textDecoder = new TextDecoder()
|
| 879 |
|
| 880 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 881 |
* @since 4.0.0
|
| 882 |
-
* @category conversions
|
| 883 |
*/
|
| 884 |
export const toURL = (self: HttpServerRequest): Option.Option<URL> => {
|
| 885 |
const host = self.headers.host ?? "localhost"
|
|
@@ -892,8 +1018,16 @@ export const toURL = (self: HttpServerRequest): Option.Option<URL> => {
|
|
| 892 |
}
|
| 893 |
|
| 894 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 895 |
* @since 4.0.0
|
| 896 |
-
* @category conversions
|
| 897 |
*/
|
| 898 |
export const toWebResult = (self: HttpServerRequest, options?: {
|
| 899 |
readonly signal?: AbortSignal | undefined
|
|
@@ -926,15 +1060,22 @@ export const toWebResult = (self: HttpServerRequest, options?: {
|
|
| 926 |
}
|
| 927 |
|
| 928 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 929 |
* @since 4.0.0
|
| 930 |
-
* @category conversions
|
| 931 |
*/
|
| 932 |
export const toWeb = (self: HttpServerRequest, options?: {
|
| 933 |
readonly signal?: AbortSignal | undefined
|
| 934 |
}): Effect.Effect<Request, RequestError> =>
|
| 935 |
Effect.contextWith((context) =>
|
| 936 |
-
toWebResult(self, {
|
| 937 |
context,
|
| 938 |
signal: options?.signal
|
| 939 |
-
})
|
| 940 |
)
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Provides server-side access to the current incoming HTTP request.
|
| 3 |
+
*
|
| 4 |
+
* `HttpServerRequest` is the context service used by handlers, middleware,
|
| 5 |
+
* schema decoders, multipart parsers, WebSocket upgrades, and adapters. A
|
| 6 |
+
* request stores its method, URL, original URL, headers, cookies, remote
|
| 7 |
+
* address, body stream, and platform source object. This module also includes
|
| 8 |
+
* request conversions and schema decoders for cookies, headers, search
|
| 9 |
+
* parameters, JSON, forms, URL-encoded bodies, and multipart bodies.
|
| 10 |
+
*
|
| 11 |
* @since 4.0.0
|
| 12 |
*/
|
| 13 |
import type * as Arr from "../../Array.ts"
|
|
|
|
| 37 |
|
| 38 |
export {
|
| 39 |
/**
|
| 40 |
+
* Provides the `MaxBodySize` fiber reference for configuring request body limits.
|
| 41 |
+
*
|
| 42 |
+
* **When to use**
|
| 43 |
+
*
|
| 44 |
+
* Use to configure the maximum body size accepted while reading server
|
| 45 |
+
* request bodies.
|
| 46 |
+
*
|
| 47 |
* @category fiber refs
|
| 48 |
+
* @since 4.0.0
|
| 49 |
*/
|
| 50 |
MaxBodySize
|
| 51 |
} from "./HttpIncomingMessage.ts"
|
| 52 |
|
| 53 |
/**
|
| 54 |
+
* Runtime type identifier for `HttpServerRequest` values.
|
| 55 |
+
*
|
| 56 |
+
* @category type IDs
|
| 57 |
* @since 4.0.0
|
|
|
|
| 58 |
*/
|
| 59 |
export const TypeId = "~effect/http/HttpServerRequest"
|
| 60 |
|
| 61 |
/**
|
| 62 |
+
* Server-side representation of an incoming HTTP request.
|
| 63 |
+
*
|
| 64 |
+
* **Details**
|
| 65 |
+
*
|
| 66 |
+
* It extends `HttpIncomingMessage` with request metadata, parsed cookies,
|
| 67 |
+
* multipart accessors, WebSocket upgrade support, and a `modify` method for
|
| 68 |
+
* creating adjusted request views.
|
| 69 |
+
*
|
| 70 |
* @category models
|
| 71 |
+
* @since 4.0.0
|
| 72 |
*/
|
| 73 |
export interface HttpServerRequest extends HttpIncomingMessage.HttpIncomingMessage<HttpServerError> {
|
| 74 |
readonly [TypeId]: typeof TypeId
|
|
|
|
| 97 |
}
|
| 98 |
|
| 99 |
/**
|
| 100 |
+
* Service tag for the active server-side HTTP request.
|
| 101 |
+
*
|
| 102 |
+
* **When to use**
|
| 103 |
+
*
|
| 104 |
+
* Use to access the request currently being handled by HTTP server routes and
|
| 105 |
+
* middleware.
|
| 106 |
+
*
|
| 107 |
* @category context
|
| 108 |
+
* @since 4.0.0
|
| 109 |
*/
|
| 110 |
export const HttpServerRequest: Context.Service<HttpServerRequest, HttpServerRequest> = Context.Service(
|
| 111 |
"effect/http/HttpServerRequest"
|
| 112 |
)
|
| 113 |
|
| 114 |
/**
|
| 115 |
+
* Service that contains decoded URL query parameters for the current request.
|
| 116 |
+
*
|
| 117 |
+
* **When to use**
|
| 118 |
+
*
|
| 119 |
+
* Use to access query parameters that have already been parsed for the current
|
| 120 |
+
* server request.
|
| 121 |
+
*
|
| 122 |
+
* **Details**
|
| 123 |
+
*
|
| 124 |
+
* Each key maps to a string value, or to an array when the parameter appears more
|
| 125 |
+
* than once.
|
| 126 |
+
*
|
| 127 |
* @category search params
|
| 128 |
+
* @since 4.0.0
|
| 129 |
*/
|
| 130 |
export class ParsedSearchParams extends Context.Service<
|
| 131 |
ParsedSearchParams,
|
|
|
|
| 133 |
>()("effect/http/ParsedSearchParams") {}
|
| 134 |
|
| 135 |
/**
|
| 136 |
+
* Converts a `URL` object's search parameters into a record.
|
| 137 |
+
*
|
| 138 |
+
* **Details**
|
| 139 |
+
*
|
| 140 |
+
* Repeated parameters are represented as arrays in insertion order.
|
| 141 |
+
*
|
| 142 |
* @category search params
|
| 143 |
+
* @since 4.0.0
|
| 144 |
*/
|
| 145 |
export const searchParamsFromURL = (url: URL): ReadonlyRecord<string, string | Array<string>> => {
|
| 146 |
const out: Record<string, string | Array<string>> = {}
|
|
|
|
| 160 |
}
|
| 161 |
|
| 162 |
/**
|
| 163 |
+
* Creates a channel backed by the current request's upgraded socket.
|
| 164 |
+
*
|
| 165 |
+
* **Details**
|
| 166 |
+
*
|
| 167 |
+
* The channel reads incoming socket messages and writes byte chunks to the
|
| 168 |
+
* socket, failing if the request cannot be upgraded or the socket fails.
|
| 169 |
+
*
|
| 170 |
* @category accessors
|
| 171 |
+
* @since 4.0.0
|
| 172 |
*/
|
| 173 |
export const upgradeChannel = <IE = never>(): Channel.Channel<
|
| 174 |
Arr.NonEmptyReadonlyArray<Uint8Array>,
|
|
|
|
| 179 |
unknown,
|
| 180 |
HttpServerRequest
|
| 181 |
> =>
|
| 182 |
+
HttpServerRequest.pipe(
|
| 183 |
Effect.flatMap((_) => _.upgrade),
|
| 184 |
Effect.map(Socket.toChannelWith<IE>()),
|
| 185 |
Channel.unwrap
|
| 186 |
)
|
| 187 |
|
| 188 |
/**
|
| 189 |
+
* Decodes a schema from the cookies of the current request.
|
| 190 |
+
*
|
| 191 |
+
* @category schemas
|
| 192 |
* @since 4.0.0
|
|
|
|
| 193 |
*/
|
| 194 |
export const schemaCookies = <A, I extends Readonly<Record<string, string | undefined>>, RD, RE>(
|
| 195 |
schema: Schema.Codec<A, I, RD, RE>,
|
| 196 |
options?: ParseOptions | undefined
|
| 197 |
): Effect.Effect<A, Schema.SchemaError, RD | HttpServerRequest> => {
|
| 198 |
const parse = Schema.decodeUnknownEffect(schema)
|
| 199 |
+
return Effect.flatMap(HttpServerRequest, (req) => parse(req.cookies, options))
|
| 200 |
}
|
| 201 |
|
| 202 |
/**
|
| 203 |
+
* Decodes a schema from the headers of the current request.
|
| 204 |
+
*
|
| 205 |
+
* @category schemas
|
| 206 |
* @since 4.0.0
|
|
|
|
| 207 |
*/
|
| 208 |
export const schemaHeaders = <A, I extends Readonly<Record<string, string | undefined>>, RD, RE>(
|
| 209 |
schema: Schema.Codec<A, I, RD, RE>,
|
| 210 |
options?: ParseOptions | undefined
|
| 211 |
): Effect.Effect<A, Schema.SchemaError, HttpServerRequest | RD> => {
|
| 212 |
const parse = Schema.decodeUnknownEffect(schema)
|
| 213 |
+
return Effect.flatMap(HttpServerRequest, (req) => parse(req.headers, options))
|
| 214 |
}
|
| 215 |
|
| 216 |
/**
|
| 217 |
+
* Decodes a schema from the parsed search parameters of the current request.
|
| 218 |
+
*
|
| 219 |
+
* @category schemas
|
| 220 |
* @since 4.0.0
|
|
|
|
| 221 |
*/
|
| 222 |
export const schemaSearchParams = <
|
| 223 |
A,
|
|
|
|
| 229 |
options?: ParseOptions | undefined
|
| 230 |
): Effect.Effect<A, Schema.SchemaError, ParsedSearchParams | RD> => {
|
| 231 |
const parse = Schema.decodeUnknownEffect(schema)
|
| 232 |
+
return Effect.flatMap(ParsedSearchParams, (params) => parse(params, options))
|
| 233 |
}
|
| 234 |
/**
|
| 235 |
+
* Reads the current request body as JSON and decodes it with the supplied schema.
|
| 236 |
+
*
|
| 237 |
+
* **Details**
|
| 238 |
+
*
|
| 239 |
+
* The effect can fail if the body cannot be read or parsed, or if schema decoding
|
| 240 |
+
* fails.
|
| 241 |
+
*
|
| 242 |
+
* @category schemas
|
| 243 |
* @since 4.0.0
|
|
|
|
| 244 |
*/
|
| 245 |
export const schemaBodyJson = <A, I, RD, RE>(
|
| 246 |
schema: Schema.Codec<A, I, RD, RE>,
|
| 247 |
options?: ParseOptions | undefined
|
| 248 |
): Effect.Effect<A, HttpServerError | Schema.SchemaError, HttpServerRequest | RD> => {
|
| 249 |
const parse = HttpIncomingMessage.schemaBodyJson(schema, options)
|
| 250 |
+
return Effect.flatMap(HttpServerRequest, parse)
|
| 251 |
}
|
| 252 |
|
| 253 |
const isMultipart = (request: HttpServerRequest) =>
|
|
|
|
| 255 |
getFormDataBody(request) !== undefined
|
| 256 |
|
| 257 |
/**
|
| 258 |
+
* Decodes the current request body as form data.
|
| 259 |
+
*
|
| 260 |
+
* **Details**
|
| 261 |
+
*
|
| 262 |
+
* Multipart requests are persisted and decoded as multipart data; other form
|
| 263 |
+
* requests are decoded from URL-encoded body parameters.
|
| 264 |
+
*
|
| 265 |
+
* @category schemas
|
| 266 |
* @since 4.0.0
|
|
|
|
| 267 |
*/
|
| 268 |
export const schemaBodyForm = <A, I extends Partial<Multipart.Persisted>, RD, RE>(
|
| 269 |
schema: Schema.Codec<A, I, RD, RE>,
|
|
|
|
| 271 |
) => {
|
| 272 |
const parseMultipart = Multipart.schemaPersisted(schema)
|
| 273 |
const parseUrlParams = HttpIncomingMessage.schemaBodyUrlParams(schema as Schema.Codec<A, any, RD, RE>, options)
|
| 274 |
+
return Effect.flatMap(HttpServerRequest, (request): Effect.Effect<
|
| 275 |
A,
|
| 276 |
Multipart.MultipartError | Schema.SchemaError | HttpServerError,
|
| 277 |
RD | HttpServerRequest | Scope.Scope | FileSystem.FileSystem | Path.Path
|
|
|
|
| 284 |
}
|
| 285 |
|
| 286 |
/**
|
| 287 |
+
* Reads the current request body as URL-encoded parameters and decodes them with
|
| 288 |
+
* the supplied schema.
|
| 289 |
+
*
|
| 290 |
+
* @category schemas
|
| 291 |
* @since 4.0.0
|
|
|
|
| 292 |
*/
|
| 293 |
export const schemaBodyUrlParams = <
|
| 294 |
A,
|
|
|
|
| 300 |
options?: ParseOptions | undefined
|
| 301 |
): Effect.Effect<A, HttpServerError | Schema.SchemaError, HttpServerRequest | RD> => {
|
| 302 |
const parse = HttpIncomingMessage.schemaBodyUrlParams(schema, options)
|
| 303 |
+
return Effect.flatMap(HttpServerRequest, parse)
|
| 304 |
}
|
| 305 |
|
| 306 |
/**
|
| 307 |
+
* Persists the current multipart request body and decodes it with the supplied
|
| 308 |
+
* schema.
|
| 309 |
+
*
|
| 310 |
+
* **Details**
|
| 311 |
+
*
|
| 312 |
+
* The effect requires the services needed to persist multipart files, including a
|
| 313 |
+
* scope, file system, and path service.
|
| 314 |
+
*
|
| 315 |
+
* @category schemas
|
| 316 |
* @since 4.0.0
|
|
|
|
| 317 |
*/
|
| 318 |
export const schemaBodyMultipart = <A, I extends Partial<Multipart.Persisted>, RD, RE>(
|
| 319 |
schema: Schema.Codec<A, I, RD, RE>,
|
|
|
|
| 324 |
HttpServerRequest | Scope.Scope | FileSystem.FileSystem | Path.Path | RD
|
| 325 |
> => {
|
| 326 |
const parse = Multipart.schemaPersisted(schema)
|
| 327 |
+
return HttpServerRequest.pipe(
|
| 328 |
Effect.flatMap((_) => _.multipart),
|
| 329 |
Effect.flatMap((_) => parse(_, options))
|
| 330 |
)
|
| 331 |
}
|
| 332 |
|
| 333 |
/**
|
| 334 |
+
* Creates a decoder for a JSON value stored in a form field.
|
| 335 |
+
*
|
| 336 |
+
* **Details**
|
| 337 |
+
*
|
| 338 |
+
* For multipart requests, the named multipart field is decoded as JSON. For
|
| 339 |
+
* URL-encoded requests, the named parameter is decoded as JSON and then decoded
|
| 340 |
+
* with the supplied schema.
|
| 341 |
+
*
|
| 342 |
+
* @category schemas
|
| 343 |
* @since 4.0.0
|
|
|
|
| 344 |
*/
|
| 345 |
export const schemaBodyFormJson = <A, I, RD, RE>(
|
| 346 |
schema: Schema.Codec<A, I, RD, RE>,
|
|
|
|
| 353 |
Schema.decodeEffect
|
| 354 |
)
|
| 355 |
return Effect.flatMap(
|
| 356 |
+
HttpServerRequest,
|
| 357 |
(request): Effect.Effect<
|
| 358 |
A,
|
| 359 |
Schema.SchemaError | HttpServerError,
|
|
|
|
| 378 |
}
|
| 379 |
|
| 380 |
/**
|
| 381 |
+
* Creates an `HttpServerRequest` view of an `HttpClientRequest`.
|
| 382 |
+
*
|
| 383 |
+
* **Details**
|
| 384 |
+
*
|
| 385 |
+
* If the client request can be converted to an absolute URL, that URL is used as
|
| 386 |
+
* the original URL.
|
| 387 |
+
*
|
| 388 |
+
* @category converting
|
| 389 |
* @since 4.0.0
|
|
|
|
| 390 |
*/
|
| 391 |
export const fromClientRequest = (request: HttpClientRequest.HttpClientRequest): HttpServerRequest => {
|
| 392 |
const url = Option.match(HttpClientRequest.toUrl(request), {
|
|
|
|
| 397 |
}
|
| 398 |
|
| 399 |
/**
|
| 400 |
+
* Wraps a Web `Request` as an `HttpServerRequest`.
|
| 401 |
+
*
|
| 402 |
+
* **Details**
|
| 403 |
+
*
|
| 404 |
+
* The request's current URL is stored without the scheme and host, while the
|
| 405 |
+
* original Web URL remains available as `originalUrl`.
|
| 406 |
+
*
|
| 407 |
+
* @category converting
|
| 408 |
* @since 4.0.0
|
|
|
|
| 409 |
*/
|
| 410 |
export const fromWeb = (request: globalThis.Request): HttpServerRequest =>
|
| 411 |
new ServerRequestImpl(request, removeHost(request.url))
|
| 412 |
|
| 413 |
/**
|
| 414 |
+
* Converts an `HttpServerRequest` into an `HttpClientRequest`.
|
| 415 |
+
*
|
| 416 |
+
* **Details**
|
| 417 |
+
*
|
| 418 |
+
* The converted request preserves the method, headers, body stream, and a URL
|
| 419 |
+
* derived from the request when possible.
|
| 420 |
+
*
|
| 421 |
+
* @category converting
|
| 422 |
* @since 4.0.0
|
|
|
|
| 423 |
*/
|
| 424 |
export const toClientRequest = (request: HttpServerRequest): HttpClientRequest.HttpClientRequest =>
|
| 425 |
HttpClientRequest.setUrl(
|
|
|
|
| 996 |
const textDecoder = new TextDecoder()
|
| 997 |
|
| 998 |
/**
|
| 999 |
+
* Attempts to construct an absolute `URL` for a server request safely.
|
| 1000 |
+
*
|
| 1001 |
+
* **Details**
|
| 1002 |
+
*
|
| 1003 |
+
* The host comes from the `host` header, defaulting to `localhost`, and the
|
| 1004 |
+
* protocol is `https` only when `x-forwarded-proto` is `https`; invalid URLs
|
| 1005 |
+
* return `Option.none`.
|
| 1006 |
+
*
|
| 1007 |
+
* @category converting
|
| 1008 |
* @since 4.0.0
|
|
|
|
| 1009 |
*/
|
| 1010 |
export const toURL = (self: HttpServerRequest): Option.Option<URL> => {
|
| 1011 |
const host = self.headers.host ?? "localhost"
|
|
|
|
| 1018 |
}
|
| 1019 |
|
| 1020 |
/**
|
| 1021 |
+
* Converts an `HttpServerRequest` safely to a Web `Request` as a `Result`.
|
| 1022 |
+
*
|
| 1023 |
+
* **Details**
|
| 1024 |
+
*
|
| 1025 |
+
* If the source is already a Web `Request`, it is returned unchanged. Otherwise
|
| 1026 |
+
* an absolute URL is derived from the request; invalid URLs fail with a
|
| 1027 |
+
* `RequestParseError`.
|
| 1028 |
+
*
|
| 1029 |
+
* @category converting
|
| 1030 |
* @since 4.0.0
|
|
|
|
| 1031 |
*/
|
| 1032 |
export const toWebResult = (self: HttpServerRequest, options?: {
|
| 1033 |
readonly signal?: AbortSignal | undefined
|
|
|
|
| 1060 |
}
|
| 1061 |
|
| 1062 |
/**
|
| 1063 |
+
* Converts an `HttpServerRequest` to a Web `Request` in `Effect`.
|
| 1064 |
+
*
|
| 1065 |
+
* **Details**
|
| 1066 |
+
*
|
| 1067 |
+
* The current context is used when streaming the request body into the Web
|
| 1068 |
+
* request.
|
| 1069 |
+
*
|
| 1070 |
+
* @category converting
|
| 1071 |
* @since 4.0.0
|
|
|
|
| 1072 |
*/
|
| 1073 |
export const toWeb = (self: HttpServerRequest, options?: {
|
| 1074 |
readonly signal?: AbortSignal | undefined
|
| 1075 |
}): Effect.Effect<Request, RequestError> =>
|
| 1076 |
Effect.contextWith((context) =>
|
| 1077 |
+
Effect.fromResult(toWebResult(self, {
|
| 1078 |
context,
|
| 1079 |
signal: options?.signal
|
| 1080 |
+
}))
|
| 1081 |
)
|
.opencode/node_modules/effect/src/unstable/http/HttpServerRespondable.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Cause from "../../Cause.ts"
|
|
@@ -9,22 +16,34 @@ import type { HttpServerResponse } from "./HttpServerResponse.ts"
|
|
| 9 |
import * as Response from "./HttpServerResponse.ts"
|
| 10 |
|
| 11 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
* @since 4.0.0
|
| 13 |
-
* @category Type IDs
|
| 14 |
*/
|
| 15 |
export const symbol = "~effect/http/HttpServerRespondable"
|
| 16 |
|
| 17 |
/**
|
| 18 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
* @category models
|
|
|
|
| 20 |
*/
|
| 21 |
export interface Respondable {
|
| 22 |
[symbol](): Effect.Effect<HttpServerResponse, unknown>
|
| 23 |
}
|
| 24 |
|
| 25 |
/**
|
| 26 |
-
*
|
|
|
|
| 27 |
* @category guards
|
|
|
|
| 28 |
*/
|
| 29 |
export const isRespondable = (u: unknown): u is Respondable => hasProperty(u, symbol)
|
| 30 |
|
|
@@ -32,8 +51,15 @@ const badRequest = Response.empty({ status: 400 })
|
|
| 32 |
const notFound = Response.empty({ status: 404 })
|
| 33 |
|
| 34 |
/**
|
| 35 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
* @category accessors
|
|
|
|
| 37 |
*/
|
| 38 |
export const toResponse = (self: Respondable): Effect.Effect<HttpServerResponse> => {
|
| 39 |
if (Response.isHttpServerResponse(self)) {
|
|
@@ -43,8 +69,16 @@ export const toResponse = (self: Respondable): Effect.Effect<HttpServerResponse>
|
|
| 43 |
}
|
| 44 |
|
| 45 |
/**
|
| 46 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
* @category accessors
|
|
|
|
| 48 |
*/
|
| 49 |
export const toResponseOrElse = (u: unknown, orElse: HttpServerResponse): Effect.Effect<HttpServerResponse> => {
|
| 50 |
if (Response.isHttpServerResponse(u)) {
|
|
@@ -61,8 +95,15 @@ export const toResponseOrElse = (u: unknown, orElse: HttpServerResponse): Effect
|
|
| 61 |
}
|
| 62 |
|
| 63 |
/**
|
| 64 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
* @category accessors
|
|
|
|
| 66 |
*/
|
| 67 |
export const toResponseOrElseDefect = (u: unknown, orElse: HttpServerResponse): Effect.Effect<HttpServerResponse> => {
|
| 68 |
if (Response.isHttpServerResponse(u)) {
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Converts supported values into HTTP server responses.
|
| 3 |
+
*
|
| 4 |
+
* Server-side errors and helper values can implement `Respondable` when they
|
| 5 |
+
* know which status, headers, cookies, or body should be sent to the client.
|
| 6 |
+
* This module detects those values and converts them to `HttpServerResponse`
|
| 7 |
+
* values, with fallback handling for schema errors and missing values.
|
| 8 |
+
*
|
| 9 |
* @since 4.0.0
|
| 10 |
*/
|
| 11 |
import * as Cause from "../../Cause.ts"
|
|
|
|
| 16 |
import * as Response from "./HttpServerResponse.ts"
|
| 17 |
|
| 18 |
/**
|
| 19 |
+
* Protocol key used by values that can render themselves as
|
| 20 |
+
* `HttpServerResponse` values.
|
| 21 |
+
*
|
| 22 |
+
* @category type IDs
|
| 23 |
* @since 4.0.0
|
|
|
|
| 24 |
*/
|
| 25 |
export const symbol = "~effect/http/HttpServerRespondable"
|
| 26 |
|
| 27 |
/**
|
| 28 |
+
* Protocol for values that can be converted into an `HttpServerResponse`.
|
| 29 |
+
*
|
| 30 |
+
* **Details**
|
| 31 |
+
*
|
| 32 |
+
* Implement the protocol method to describe the response that should be sent for
|
| 33 |
+
* the value.
|
| 34 |
+
*
|
| 35 |
* @category models
|
| 36 |
+
* @since 4.0.0
|
| 37 |
*/
|
| 38 |
export interface Respondable {
|
| 39 |
[symbol](): Effect.Effect<HttpServerResponse, unknown>
|
| 40 |
}
|
| 41 |
|
| 42 |
/**
|
| 43 |
+
* Returns `true` when the supplied value implements the `Respondable` protocol.
|
| 44 |
+
*
|
| 45 |
* @category guards
|
| 46 |
+
* @since 4.0.0
|
| 47 |
*/
|
| 48 |
export const isRespondable = (u: unknown): u is Respondable => hasProperty(u, symbol)
|
| 49 |
|
|
|
|
| 51 |
const notFound = Response.empty({ status: 404 })
|
| 52 |
|
| 53 |
/**
|
| 54 |
+
* Converts a `Respondable` value into an `HttpServerResponse`.
|
| 55 |
+
*
|
| 56 |
+
* **Details**
|
| 57 |
+
*
|
| 58 |
+
* If the value is already an HTTP server response it is returned directly; errors
|
| 59 |
+
* from the response conversion are converted to defects.
|
| 60 |
+
*
|
| 61 |
* @category accessors
|
| 62 |
+
* @since 4.0.0
|
| 63 |
*/
|
| 64 |
export const toResponse = (self: Respondable): Effect.Effect<HttpServerResponse> => {
|
| 65 |
if (Response.isHttpServerResponse(self)) {
|
|
|
|
| 69 |
}
|
| 70 |
|
| 71 |
/**
|
| 72 |
+
* Attempts to convert an unknown value into an `HttpServerResponse`, falling back
|
| 73 |
+
* to the supplied response when no conversion is available.
|
| 74 |
+
*
|
| 75 |
+
* **Details**
|
| 76 |
+
*
|
| 77 |
+
* `HttpServerResponse` and `Respondable` values are used directly, schema errors
|
| 78 |
+
* become `400` responses, and no-such-element errors become `404` responses.
|
| 79 |
+
*
|
| 80 |
* @category accessors
|
| 81 |
+
* @since 4.0.0
|
| 82 |
*/
|
| 83 |
export const toResponseOrElse = (u: unknown, orElse: HttpServerResponse): Effect.Effect<HttpServerResponse> => {
|
| 84 |
if (Response.isHttpServerResponse(u)) {
|
|
|
|
| 95 |
}
|
| 96 |
|
| 97 |
/**
|
| 98 |
+
* Attempts to convert an unknown defect into an `HttpServerResponse`, falling
|
| 99 |
+
* back to the supplied response when no conversion is available.
|
| 100 |
+
*
|
| 101 |
+
* **Details**
|
| 102 |
+
*
|
| 103 |
+
* Only `HttpServerResponse` and `Respondable` values receive special handling.
|
| 104 |
+
*
|
| 105 |
* @category accessors
|
| 106 |
+
* @since 4.0.0
|
| 107 |
*/
|
| 108 |
export const toResponseOrElseDefect = (u: unknown, orElse: HttpServerResponse): Effect.Effect<HttpServerResponse> => {
|
| 109 |
if (Response.isHttpServerResponse(u)) {
|
.opencode/node_modules/effect/src/unstable/http/HttpServerResponse.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Context from "../../Context.ts"
|
|
@@ -31,8 +40,15 @@ import * as UrlParams from "./UrlParams.ts"
|
|
| 31 |
const TypeId = "~effect/http/HttpServerResponse"
|
| 32 |
|
| 33 |
/**
|
| 34 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
* @category models
|
|
|
|
| 36 |
*/
|
| 37 |
export interface HttpServerResponse extends Inspectable.Inspectable, Pipeable, ErrorReporter.Reportable {
|
| 38 |
readonly [TypeId]: typeof TypeId
|
|
@@ -44,8 +60,10 @@ export interface HttpServerResponse extends Inspectable.Inspectable, Pipeable, E
|
|
| 44 |
}
|
| 45 |
|
| 46 |
/**
|
|
|
|
|
|
|
|
|
|
| 47 |
* @since 4.0.0
|
| 48 |
-
* @category models
|
| 49 |
*/
|
| 50 |
export interface Options {
|
| 51 |
readonly status?: number | undefined
|
|
@@ -57,30 +75,48 @@ export interface Options {
|
|
| 57 |
}
|
| 58 |
|
| 59 |
/**
|
|
|
|
|
|
|
|
|
|
| 60 |
* @since 4.0.0
|
| 61 |
*/
|
| 62 |
export declare namespace Options {
|
| 63 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
* @since 4.0.0
|
| 65 |
-
* @category models
|
| 66 |
*/
|
| 67 |
export interface WithContent extends Omit<Options, "contentType" | "contentLength"> {}
|
| 68 |
|
| 69 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
* @since 4.0.0
|
| 71 |
-
* @category models
|
| 72 |
*/
|
| 73 |
export interface WithContentType extends Omit<Options, "contentLength"> {}
|
| 74 |
}
|
| 75 |
|
| 76 |
/**
|
|
|
|
|
|
|
|
|
|
| 77 |
* @since 4.0.0
|
| 78 |
*/
|
| 79 |
export const isHttpServerResponse = (u: unknown): u is HttpServerResponse => hasProperty(u, TypeId)
|
| 80 |
|
| 81 |
/**
|
| 82 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
* @category constructors
|
|
|
|
| 84 |
*/
|
| 85 |
export const empty = (
|
| 86 |
options?: Options.WithContent | undefined
|
|
@@ -93,8 +129,15 @@ export const empty = (
|
|
| 93 |
})
|
| 94 |
|
| 95 |
/**
|
| 96 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
* @category constructors
|
|
|
|
| 98 |
*/
|
| 99 |
export const redirect = (
|
| 100 |
location: string | URL,
|
|
@@ -112,8 +155,10 @@ export const redirect = (
|
|
| 112 |
}
|
| 113 |
|
| 114 |
/**
|
| 115 |
-
*
|
|
|
|
| 116 |
* @category constructors
|
|
|
|
| 117 |
*/
|
| 118 |
export const uint8Array = (
|
| 119 |
body: Uint8Array,
|
|
@@ -143,8 +188,10 @@ const getContentType = (
|
|
| 143 |
}
|
| 144 |
|
| 145 |
/**
|
| 146 |
-
*
|
|
|
|
| 147 |
* @category constructors
|
|
|
|
| 148 |
*/
|
| 149 |
export const text = (
|
| 150 |
body: string,
|
|
@@ -163,13 +210,29 @@ export const text = (
|
|
| 163 |
}
|
| 164 |
|
| 165 |
/**
|
| 166 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
* @category constructors
|
|
|
|
| 168 |
*/
|
| 169 |
export const html: {
|
| 170 |
/**
|
| 171 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
* @category constructors
|
|
|
|
| 173 |
*/
|
| 174 |
<A extends ReadonlyArray<Template.Interpolated>>(strings: TemplateStringsArray, ...args: A): Effect.Effect<
|
| 175 |
HttpServerResponse,
|
|
@@ -177,8 +240,16 @@ export const html: {
|
|
| 177 |
Template.Interpolated.Context<A[number]>
|
| 178 |
>
|
| 179 |
/**
|
| 180 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
* @category constructors
|
|
|
|
| 182 |
*/
|
| 183 |
(html: string): HttpServerResponse
|
| 184 |
} = (
|
|
@@ -193,8 +264,15 @@ export const html: {
|
|
| 193 |
}
|
| 194 |
|
| 195 |
/**
|
| 196 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
* @category constructors
|
|
|
|
| 198 |
*/
|
| 199 |
export const htmlStream = <
|
| 200 |
A extends ReadonlyArray<Template.InterpolatedWithStream>
|
|
@@ -219,8 +297,15 @@ export const htmlStream = <
|
|
| 219 |
)
|
| 220 |
|
| 221 |
/**
|
| 222 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
* @category constructors
|
|
|
|
| 224 |
*/
|
| 225 |
export const json = (
|
| 226 |
body: unknown,
|
|
@@ -238,8 +323,16 @@ export const json = (
|
|
| 238 |
}
|
| 239 |
|
| 240 |
/**
|
| 241 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
* @category constructors
|
|
|
|
| 243 |
*/
|
| 244 |
export const schemaJson = <A, I, RD, RE>(
|
| 245 |
schema: Schema.Codec<A, I, RD, RE>,
|
|
@@ -263,8 +356,20 @@ export const schemaJson = <A, I, RD, RE>(
|
|
| 263 |
}
|
| 264 |
|
| 265 |
/**
|
| 266 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
* @category constructors
|
|
|
|
| 268 |
*/
|
| 269 |
export const jsonUnsafe = (
|
| 270 |
body: unknown,
|
|
@@ -281,8 +386,11 @@ export const jsonUnsafe = (
|
|
| 281 |
}
|
| 282 |
|
| 283 |
/**
|
| 284 |
-
*
|
|
|
|
|
|
|
| 285 |
* @category constructors
|
|
|
|
| 286 |
*/
|
| 287 |
export const urlParams = (
|
| 288 |
body: UrlParams.Input,
|
|
@@ -302,8 +410,16 @@ export const urlParams = (
|
|
| 302 |
}
|
| 303 |
|
| 304 |
/**
|
| 305 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
* @category constructors
|
|
|
|
| 307 |
*/
|
| 308 |
export const raw = (
|
| 309 |
body: unknown,
|
|
@@ -321,8 +437,10 @@ export const raw = (
|
|
| 321 |
})
|
| 322 |
|
| 323 |
/**
|
| 324 |
-
*
|
|
|
|
| 325 |
* @category constructors
|
|
|
|
| 326 |
*/
|
| 327 |
export const formData = (
|
| 328 |
body: FormData,
|
|
@@ -337,8 +455,15 @@ export const formData = (
|
|
| 337 |
})
|
| 338 |
|
| 339 |
/**
|
| 340 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
* @category constructors
|
|
|
|
| 342 |
*/
|
| 343 |
export const stream = <E>(
|
| 344 |
body: Stream.Stream<Uint8Array, E>,
|
|
@@ -366,8 +491,15 @@ const HttpPlatformKey = Context.Service<
|
|
| 366 |
>("effect/http/HttpPlatform" satisfies typeof HttpPlatform.key)
|
| 367 |
|
| 368 |
/**
|
| 369 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 370 |
* @category constructors
|
|
|
|
| 371 |
*/
|
| 372 |
export const file = (
|
| 373 |
path: string,
|
|
@@ -379,11 +511,18 @@ export const file = (
|
|
| 379 |
})
|
| 380 |
| undefined
|
| 381 |
): Effect.Effect<HttpServerResponse, PlatformError, HttpPlatform> =>
|
| 382 |
-
Effect.flatMap(HttpPlatformKey
|
| 383 |
|
| 384 |
/**
|
| 385 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 386 |
* @category constructors
|
|
|
|
| 387 |
*/
|
| 388 |
export const fileWeb = (
|
| 389 |
file: Body.HttpBody.FileLike,
|
|
@@ -395,21 +534,27 @@ export const fileWeb = (
|
|
| 395 |
})
|
| 396 |
| undefined
|
| 397 |
): Effect.Effect<HttpServerResponse, never, HttpPlatform> =>
|
| 398 |
-
Effect.flatMap(HttpPlatformKey
|
| 399 |
|
| 400 |
/**
|
| 401 |
-
*
|
|
|
|
| 402 |
* @category combinators
|
|
|
|
| 403 |
*/
|
| 404 |
export const setHeader: {
|
| 405 |
/**
|
| 406 |
-
*
|
|
|
|
| 407 |
* @category combinators
|
|
|
|
| 408 |
*/
|
| 409 |
(key: string, value: string): (self: HttpServerResponse) => HttpServerResponse
|
| 410 |
/**
|
| 411 |
-
*
|
|
|
|
| 412 |
* @category combinators
|
|
|
|
| 413 |
*/
|
| 414 |
(self: HttpServerResponse, key: string, value: string): HttpServerResponse
|
| 415 |
} = dual(
|
|
@@ -422,18 +567,24 @@ export const setHeader: {
|
|
| 422 |
)
|
| 423 |
|
| 424 |
/**
|
| 425 |
-
*
|
|
|
|
| 426 |
* @category combinators
|
|
|
|
| 427 |
*/
|
| 428 |
export const setHeaders: {
|
| 429 |
/**
|
| 430 |
-
*
|
|
|
|
| 431 |
* @category combinators
|
|
|
|
| 432 |
*/
|
| 433 |
(input: Headers.Input): (self: HttpServerResponse) => HttpServerResponse
|
| 434 |
/**
|
| 435 |
-
*
|
|
|
|
| 436 |
* @category combinators
|
|
|
|
| 437 |
*/
|
| 438 |
(self: HttpServerResponse, input: Headers.Input): HttpServerResponse
|
| 439 |
} = dual(
|
|
@@ -446,18 +597,24 @@ export const setHeaders: {
|
|
| 446 |
)
|
| 447 |
|
| 448 |
/**
|
| 449 |
-
*
|
|
|
|
| 450 |
* @category combinators
|
|
|
|
| 451 |
*/
|
| 452 |
export const removeCookie: {
|
| 453 |
/**
|
| 454 |
-
*
|
|
|
|
| 455 |
* @category combinators
|
|
|
|
| 456 |
*/
|
| 457 |
(name: string): (self: HttpServerResponse) => HttpServerResponse
|
| 458 |
/**
|
| 459 |
-
*
|
|
|
|
| 460 |
* @category combinators
|
|
|
|
| 461 |
*/
|
| 462 |
(self: HttpServerResponse, name: string): HttpServerResponse
|
| 463 |
} = dual(
|
|
@@ -470,18 +627,24 @@ export const removeCookie: {
|
|
| 470 |
)
|
| 471 |
|
| 472 |
/**
|
| 473 |
-
*
|
|
|
|
| 474 |
* @category combinators
|
|
|
|
| 475 |
*/
|
| 476 |
export const replaceCookies: {
|
| 477 |
/**
|
| 478 |
-
*
|
|
|
|
| 479 |
* @category combinators
|
|
|
|
| 480 |
*/
|
| 481 |
(cookies: Cookies.Cookies): (self: HttpServerResponse) => HttpServerResponse
|
| 482 |
/**
|
| 483 |
-
*
|
|
|
|
| 484 |
* @category combinators
|
|
|
|
| 485 |
*/
|
| 486 |
(self: HttpServerResponse, cookies: Cookies.Cookies): HttpServerResponse
|
| 487 |
} = dual(
|
|
@@ -490,20 +653,41 @@ export const replaceCookies: {
|
|
| 490 |
)
|
| 491 |
|
| 492 |
/**
|
| 493 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 494 |
* @category combinators
|
|
|
|
| 495 |
*/
|
| 496 |
export const setCookie: {
|
| 497 |
/**
|
| 498 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 499 |
* @category combinators
|
|
|
|
| 500 |
*/
|
| 501 |
(name: string, value: string, options?: Cookies.Cookie["options"]): (
|
| 502 |
self: HttpServerResponse
|
| 503 |
) => Effect.Effect<HttpServerResponse, Cookies.CookiesError>
|
| 504 |
/**
|
| 505 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 506 |
* @category combinators
|
|
|
|
| 507 |
*/
|
| 508 |
(
|
| 509 |
self: HttpServerResponse,
|
|
@@ -520,7 +704,7 @@ export const setCookie: {
|
|
| 520 |
options?: Cookies.Cookie["options"]
|
| 521 |
): Effect.Effect<HttpServerResponse, Cookies.CookiesError> =>
|
| 522 |
Effect.map(
|
| 523 |
-
Cookies.set(self.cookies, name, value, options)
|
| 524 |
(cookies) =>
|
| 525 |
makeResponse({
|
| 526 |
...self,
|
|
@@ -530,13 +714,27 @@ export const setCookie: {
|
|
| 530 |
)
|
| 531 |
|
| 532 |
/**
|
| 533 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 534 |
* @category combinators
|
|
|
|
| 535 |
*/
|
| 536 |
export const expireCookie: {
|
| 537 |
/**
|
| 538 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 539 |
* @category combinators
|
|
|
|
| 540 |
*/
|
| 541 |
(
|
| 542 |
name: string,
|
|
@@ -545,8 +743,15 @@ export const expireCookie: {
|
|
| 545 |
self: HttpServerResponse
|
| 546 |
) => Effect.Effect<HttpServerResponse, Cookies.CookiesError>
|
| 547 |
/**
|
| 548 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 549 |
* @category combinators
|
|
|
|
| 550 |
*/
|
| 551 |
(
|
| 552 |
self: HttpServerResponse,
|
|
@@ -561,7 +766,7 @@ export const expireCookie: {
|
|
| 561 |
options?: Omit<NonNullable<Cookies.Cookie["options"]>, "expires" | "maxAge">
|
| 562 |
): Effect.Effect<HttpServerResponse, Cookies.CookiesError> =>
|
| 563 |
Effect.map(
|
| 564 |
-
Cookies.expireCookie(self.cookies, name, options)
|
| 565 |
(cookies) =>
|
| 566 |
makeResponse({
|
| 567 |
...self,
|
|
@@ -571,18 +776,42 @@ export const expireCookie: {
|
|
| 571 |
)
|
| 572 |
|
| 573 |
/**
|
| 574 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 575 |
* @category combinators
|
|
|
|
| 576 |
*/
|
| 577 |
export const setCookieUnsafe: {
|
| 578 |
/**
|
| 579 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 580 |
* @category combinators
|
|
|
|
| 581 |
*/
|
| 582 |
(name: string, value: string, options?: Cookies.Cookie["options"]): (self: HttpServerResponse) => HttpServerResponse
|
| 583 |
/**
|
| 584 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 585 |
* @category combinators
|
|
|
|
| 586 |
*/
|
| 587 |
(
|
| 588 |
self: HttpServerResponse,
|
|
@@ -605,21 +834,45 @@ export const setCookieUnsafe: {
|
|
| 605 |
)
|
| 606 |
|
| 607 |
/**
|
| 608 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 609 |
* @category combinators
|
|
|
|
| 610 |
*/
|
| 611 |
export const expireCookieUnsafe: {
|
| 612 |
/**
|
| 613 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 614 |
* @category combinators
|
|
|
|
| 615 |
*/
|
| 616 |
(
|
| 617 |
name: string,
|
| 618 |
options?: Omit<NonNullable<Cookies.Cookie["options"]>, "expires" | "maxAge">
|
| 619 |
): (self: HttpServerResponse) => HttpServerResponse
|
| 620 |
/**
|
| 621 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 622 |
* @category combinators
|
|
|
|
| 623 |
*/
|
| 624 |
(
|
| 625 |
self: HttpServerResponse,
|
|
@@ -640,18 +893,42 @@ export const expireCookieUnsafe: {
|
|
| 640 |
)
|
| 641 |
|
| 642 |
/**
|
| 643 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 644 |
* @category combinators
|
|
|
|
| 645 |
*/
|
| 646 |
export const updateCookies: {
|
| 647 |
/**
|
| 648 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 649 |
* @category combinators
|
|
|
|
| 650 |
*/
|
| 651 |
(f: (cookies: Cookies.Cookies) => Cookies.Cookies): (self: HttpServerResponse) => HttpServerResponse
|
| 652 |
/**
|
| 653 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 654 |
* @category combinators
|
|
|
|
| 655 |
*/
|
| 656 |
(self: HttpServerResponse, f: (cookies: Cookies.Cookies) => Cookies.Cookies): HttpServerResponse
|
| 657 |
} = dual(
|
|
@@ -667,18 +944,42 @@ export const updateCookies: {
|
|
| 667 |
)
|
| 668 |
|
| 669 |
/**
|
| 670 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 671 |
* @category combinators
|
|
|
|
| 672 |
*/
|
| 673 |
export const mergeCookies: {
|
| 674 |
/**
|
| 675 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 676 |
* @category combinators
|
|
|
|
| 677 |
*/
|
| 678 |
(cookies: Cookies.Cookies): (self: HttpServerResponse) => HttpServerResponse
|
| 679 |
/**
|
| 680 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 681 |
* @category combinators
|
|
|
|
| 682 |
*/
|
| 683 |
(self: HttpServerResponse, cookies: Cookies.Cookies): HttpServerResponse
|
| 684 |
} = dual(
|
|
@@ -688,13 +989,27 @@ export const mergeCookies: {
|
|
| 688 |
)
|
| 689 |
|
| 690 |
/**
|
| 691 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 692 |
* @category combinators
|
|
|
|
| 693 |
*/
|
| 694 |
export const setCookies: {
|
| 695 |
/**
|
| 696 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 697 |
* @category combinators
|
|
|
|
| 698 |
*/
|
| 699 |
(
|
| 700 |
cookies: Iterable<
|
|
@@ -708,8 +1023,15 @@ export const setCookies: {
|
|
| 708 |
self: HttpServerResponse
|
| 709 |
) => Effect.Effect<HttpServerResponse, Cookies.CookiesError, never>
|
| 710 |
/**
|
| 711 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 712 |
* @category combinators
|
|
|
|
| 713 |
*/
|
| 714 |
(
|
| 715 |
self: HttpServerResponse,
|
|
@@ -733,7 +1055,7 @@ export const setCookies: {
|
|
| 733 |
]
|
| 734 |
>
|
| 735 |
): Effect.Effect<HttpServerResponse, Cookies.CookiesError> =>
|
| 736 |
-
Effect.map(Cookies.setAll(self.cookies, cookies)
|
| 737 |
makeResponse({
|
| 738 |
...self,
|
| 739 |
cookies
|
|
@@ -741,13 +1063,29 @@ export const setCookies: {
|
|
| 741 |
)
|
| 742 |
|
| 743 |
/**
|
| 744 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 745 |
* @category combinators
|
|
|
|
| 746 |
*/
|
| 747 |
export const setCookiesUnsafe: {
|
| 748 |
/**
|
| 749 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 750 |
* @category combinators
|
|
|
|
| 751 |
*/
|
| 752 |
(
|
| 753 |
cookies: Iterable<
|
|
@@ -759,8 +1097,16 @@ export const setCookiesUnsafe: {
|
|
| 759 |
>
|
| 760 |
): (self: HttpServerResponse) => HttpServerResponse
|
| 761 |
/**
|
| 762 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 763 |
* @category combinators
|
|
|
|
| 764 |
*/
|
| 765 |
(
|
| 766 |
self: HttpServerResponse,
|
|
@@ -791,18 +1137,39 @@ export const setCookiesUnsafe: {
|
|
| 791 |
)
|
| 792 |
|
| 793 |
/**
|
| 794 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 795 |
* @category combinators
|
|
|
|
| 796 |
*/
|
| 797 |
export const setBody: {
|
| 798 |
/**
|
| 799 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 800 |
* @category combinators
|
|
|
|
| 801 |
*/
|
| 802 |
(body: Body.HttpBody): (self: HttpServerResponse) => HttpServerResponse
|
| 803 |
/**
|
| 804 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 805 |
* @category combinators
|
|
|
|
| 806 |
*/
|
| 807 |
(self: HttpServerResponse, body: Body.HttpBody): HttpServerResponse
|
| 808 |
} = dual(
|
|
@@ -811,18 +1178,36 @@ export const setBody: {
|
|
| 811 |
)
|
| 812 |
|
| 813 |
/**
|
| 814 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 815 |
* @category combinators
|
|
|
|
| 816 |
*/
|
| 817 |
export const setStatus: {
|
| 818 |
/**
|
| 819 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 820 |
* @category combinators
|
|
|
|
| 821 |
*/
|
| 822 |
(status: number, statusText?: string | undefined): (self: HttpServerResponse) => HttpServerResponse
|
| 823 |
/**
|
| 824 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 825 |
* @category combinators
|
|
|
|
| 826 |
*/
|
| 827 |
(self: HttpServerResponse, status: number, statusText?: string | undefined): HttpServerResponse
|
| 828 |
} = dual(
|
|
@@ -840,8 +1225,16 @@ export const setStatus: {
|
|
| 840 |
)
|
| 841 |
|
| 842 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 843 |
* @since 4.0.0
|
| 844 |
-
* @category conversions
|
| 845 |
*/
|
| 846 |
export const toWeb = (
|
| 847 |
response: HttpServerResponse,
|
|
@@ -911,8 +1304,15 @@ export const toWeb = (
|
|
| 911 |
}
|
| 912 |
|
| 913 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 914 |
* @since 4.0.0
|
| 915 |
-
* @category conversions
|
| 916 |
*/
|
| 917 |
export const toClientResponse = (
|
| 918 |
response: HttpServerResponse,
|
|
@@ -1125,8 +1525,15 @@ class ServerHttpClientResponse extends Inspectable.Class implements HttpClientRe
|
|
| 1125 |
const textDecoder = new TextDecoder()
|
| 1126 |
|
| 1127 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1128 |
* @since 4.0.0
|
| 1129 |
-
* @category conversions
|
| 1130 |
*/
|
| 1131 |
export const fromClientResponse = (
|
| 1132 |
response: HttpClientResponse.HttpClientResponse
|
|
@@ -1211,8 +1618,15 @@ const makeResponse = (options: {
|
|
| 1211 |
}
|
| 1212 |
|
| 1213 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1214 |
* @since 4.0.0
|
| 1215 |
-
* @category conversions
|
| 1216 |
*/
|
| 1217 |
export const fromWeb = (response: Response): HttpServerResponse => {
|
| 1218 |
const headers = new globalThis.Headers(response.headers)
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Describes immutable responses returned by Effect HTTP handlers.
|
| 3 |
+
*
|
| 4 |
+
* An `HttpServerResponse` stores the status, optional status text, headers,
|
| 5 |
+
* cookies, and body that the server runtime later turns into a platform
|
| 6 |
+
* response such as a Web `Response`. This module includes constructors for
|
| 7 |
+
* common response bodies, helpers for updating response data, file response
|
| 8 |
+
* support through `HttpPlatform`, and conversions to or from Web and Effect
|
| 9 |
+
* HTTP client responses.
|
| 10 |
+
*
|
| 11 |
* @since 4.0.0
|
| 12 |
*/
|
| 13 |
import * as Context from "../../Context.ts"
|
|
|
|
| 40 |
const TypeId = "~effect/http/HttpServerResponse"
|
| 41 |
|
| 42 |
/**
|
| 43 |
+
* Server-side HTTP response model.
|
| 44 |
+
*
|
| 45 |
+
* **Details**
|
| 46 |
+
*
|
| 47 |
+
* A response contains a status, optional status text, headers, cookies, and an
|
| 48 |
+
* HTTP body that can later be converted to platform-specific response types.
|
| 49 |
+
*
|
| 50 |
* @category models
|
| 51 |
+
* @since 4.0.0
|
| 52 |
*/
|
| 53 |
export interface HttpServerResponse extends Inspectable.Inspectable, Pipeable, ErrorReporter.Reportable {
|
| 54 |
readonly [TypeId]: typeof TypeId
|
|
|
|
| 60 |
}
|
| 61 |
|
| 62 |
/**
|
| 63 |
+
* Common options accepted by HTTP server response constructors.
|
| 64 |
+
*
|
| 65 |
+
* @category options
|
| 66 |
* @since 4.0.0
|
|
|
|
| 67 |
*/
|
| 68 |
export interface Options {
|
| 69 |
readonly status?: number | undefined
|
|
|
|
| 75 |
}
|
| 76 |
|
| 77 |
/**
|
| 78 |
+
* Option variants used by response constructors with different body metadata
|
| 79 |
+
* rules.
|
| 80 |
+
*
|
| 81 |
* @since 4.0.0
|
| 82 |
*/
|
| 83 |
export declare namespace Options {
|
| 84 |
/**
|
| 85 |
+
* Response options for constructors whose body determines its own content type
|
| 86 |
+
* and content length.
|
| 87 |
+
*
|
| 88 |
+
* @category options
|
| 89 |
* @since 4.0.0
|
|
|
|
| 90 |
*/
|
| 91 |
export interface WithContent extends Omit<Options, "contentType" | "contentLength"> {}
|
| 92 |
|
| 93 |
/**
|
| 94 |
+
* Response options for constructors that allow overriding the content type while
|
| 95 |
+
* deriving the content length from the body.
|
| 96 |
+
*
|
| 97 |
+
* @category options
|
| 98 |
* @since 4.0.0
|
|
|
|
| 99 |
*/
|
| 100 |
export interface WithContentType extends Omit<Options, "contentLength"> {}
|
| 101 |
}
|
| 102 |
|
| 103 |
/**
|
| 104 |
+
* Returns `true` when the supplied value is an `HttpServerResponse`.
|
| 105 |
+
*
|
| 106 |
+
* @category guards
|
| 107 |
* @since 4.0.0
|
| 108 |
*/
|
| 109 |
export const isHttpServerResponse = (u: unknown): u is HttpServerResponse => hasProperty(u, TypeId)
|
| 110 |
|
| 111 |
/**
|
| 112 |
+
* Creates an empty HTTP response.
|
| 113 |
+
*
|
| 114 |
+
* **Details**
|
| 115 |
+
*
|
| 116 |
+
* The default status is `204`.
|
| 117 |
+
*
|
| 118 |
* @category constructors
|
| 119 |
+
* @since 4.0.0
|
| 120 |
*/
|
| 121 |
export const empty = (
|
| 122 |
options?: Options.WithContent | undefined
|
|
|
|
| 129 |
})
|
| 130 |
|
| 131 |
/**
|
| 132 |
+
* Creates a redirect response with a `Location` header.
|
| 133 |
+
*
|
| 134 |
+
* **Details**
|
| 135 |
+
*
|
| 136 |
+
* The default status is `302`; custom headers are merged with the generated
|
| 137 |
+
* `Location` header.
|
| 138 |
+
*
|
| 139 |
* @category constructors
|
| 140 |
+
* @since 4.0.0
|
| 141 |
*/
|
| 142 |
export const redirect = (
|
| 143 |
location: string | URL,
|
|
|
|
| 155 |
}
|
| 156 |
|
| 157 |
/**
|
| 158 |
+
* Creates an HTTP response whose body is a `Uint8Array`.
|
| 159 |
+
*
|
| 160 |
* @category constructors
|
| 161 |
+
* @since 4.0.0
|
| 162 |
*/
|
| 163 |
export const uint8Array = (
|
| 164 |
body: Uint8Array,
|
|
|
|
| 188 |
}
|
| 189 |
|
| 190 |
/**
|
| 191 |
+
* Creates an HTTP response whose body is a string.
|
| 192 |
+
*
|
| 193 |
* @category constructors
|
| 194 |
+
* @since 4.0.0
|
| 195 |
*/
|
| 196 |
export const text = (
|
| 197 |
body: string,
|
|
|
|
| 210 |
}
|
| 211 |
|
| 212 |
/**
|
| 213 |
+
* Creates an HTML response with the `text/html` content type.
|
| 214 |
+
*
|
| 215 |
+
* **Details**
|
| 216 |
+
*
|
| 217 |
+
* Passing a string returns a response directly. Using it as a template tag returns
|
| 218 |
+
* an effect so interpolated values can be rendered with their required services
|
| 219 |
+
* and errors.
|
| 220 |
+
*
|
| 221 |
* @category constructors
|
| 222 |
+
* @since 4.0.0
|
| 223 |
*/
|
| 224 |
export const html: {
|
| 225 |
/**
|
| 226 |
+
* Creates an HTML response with the `text/html` content type.
|
| 227 |
+
*
|
| 228 |
+
* **Details**
|
| 229 |
+
*
|
| 230 |
+
* Passing a string returns a response directly. Using it as a template tag returns
|
| 231 |
+
* an effect so interpolated values can be rendered with their required services
|
| 232 |
+
* and errors.
|
| 233 |
+
*
|
| 234 |
* @category constructors
|
| 235 |
+
* @since 4.0.0
|
| 236 |
*/
|
| 237 |
<A extends ReadonlyArray<Template.Interpolated>>(strings: TemplateStringsArray, ...args: A): Effect.Effect<
|
| 238 |
HttpServerResponse,
|
|
|
|
| 240 |
Template.Interpolated.Context<A[number]>
|
| 241 |
>
|
| 242 |
/**
|
| 243 |
+
* Creates an HTML response with the `text/html` content type.
|
| 244 |
+
*
|
| 245 |
+
* **Details**
|
| 246 |
+
*
|
| 247 |
+
* Passing a string returns a response directly. Using it as a template tag returns
|
| 248 |
+
* an effect so interpolated values can be rendered with their required services
|
| 249 |
+
* and errors.
|
| 250 |
+
*
|
| 251 |
* @category constructors
|
| 252 |
+
* @since 4.0.0
|
| 253 |
*/
|
| 254 |
(html: string): HttpServerResponse
|
| 255 |
} = (
|
|
|
|
| 264 |
}
|
| 265 |
|
| 266 |
/**
|
| 267 |
+
* Creates a streaming HTML response from a template.
|
| 268 |
+
*
|
| 269 |
+
* **Details**
|
| 270 |
+
*
|
| 271 |
+
* The template is encoded as a byte stream and can use streaming interpolated
|
| 272 |
+
* values from the current context.
|
| 273 |
+
*
|
| 274 |
* @category constructors
|
| 275 |
+
* @since 4.0.0
|
| 276 |
*/
|
| 277 |
export const htmlStream = <
|
| 278 |
A extends ReadonlyArray<Template.InterpolatedWithStream>
|
|
|
|
| 297 |
)
|
| 298 |
|
| 299 |
/**
|
| 300 |
+
* Creates a JSON HTTP response.
|
| 301 |
+
*
|
| 302 |
+
* **Details**
|
| 303 |
+
*
|
| 304 |
+
* The body is serialized with `JSON.stringify`; serialization errors are captured
|
| 305 |
+
* as `HttpBodyError` failures.
|
| 306 |
+
*
|
| 307 |
* @category constructors
|
| 308 |
+
* @since 4.0.0
|
| 309 |
*/
|
| 310 |
export const json = (
|
| 311 |
body: unknown,
|
|
|
|
| 323 |
}
|
| 324 |
|
| 325 |
/**
|
| 326 |
+
* Creates a JSON response constructor backed by a schema encoder.
|
| 327 |
+
*
|
| 328 |
+
* **Details**
|
| 329 |
+
*
|
| 330 |
+
* The returned function encodes the value with the supplied schema before
|
| 331 |
+
* serializing it as JSON, and can fail with `HttpBodyError` if schema encoding or
|
| 332 |
+
* JSON serialization fails.
|
| 333 |
+
*
|
| 334 |
* @category constructors
|
| 335 |
+
* @since 4.0.0
|
| 336 |
*/
|
| 337 |
export const schemaJson = <A, I, RD, RE>(
|
| 338 |
schema: Schema.Codec<A, I, RD, RE>,
|
|
|
|
| 356 |
}
|
| 357 |
|
| 358 |
/**
|
| 359 |
+
* Creates a JSON HTTP response synchronously.
|
| 360 |
+
*
|
| 361 |
+
* **When to use**
|
| 362 |
+
*
|
| 363 |
+
* Use when the response body is known to be JSON-serializable and you need a
|
| 364 |
+
* synchronous `HttpServerResponse`.
|
| 365 |
+
*
|
| 366 |
+
* **Gotchas**
|
| 367 |
+
*
|
| 368 |
+
* Unlike `json`, serialization errors from `JSON.stringify` are not captured in
|
| 369 |
+
* `Effect`.
|
| 370 |
+
*
|
| 371 |
* @category constructors
|
| 372 |
+
* @since 4.0.0
|
| 373 |
*/
|
| 374 |
export const jsonUnsafe = (
|
| 375 |
body: unknown,
|
|
|
|
| 386 |
}
|
| 387 |
|
| 388 |
/**
|
| 389 |
+
* Creates a response from URL parameters using the
|
| 390 |
+
* `application/x-www-form-urlencoded` content type by default.
|
| 391 |
+
*
|
| 392 |
* @category constructors
|
| 393 |
+
* @since 4.0.0
|
| 394 |
*/
|
| 395 |
export const urlParams = (
|
| 396 |
body: UrlParams.Input,
|
|
|
|
| 410 |
}
|
| 411 |
|
| 412 |
/**
|
| 413 |
+
* Creates a response with a raw body value.
|
| 414 |
+
*
|
| 415 |
+
* **When to use**
|
| 416 |
+
*
|
| 417 |
+
* Use when you want to pass through a body value already understood by the
|
| 418 |
+
* underlying runtime, such as a Web `Response`, `Blob`, or `ReadableStream`,
|
| 419 |
+
* for later platform conversion.
|
| 420 |
+
*
|
| 421 |
* @category constructors
|
| 422 |
+
* @since 4.0.0
|
| 423 |
*/
|
| 424 |
export const raw = (
|
| 425 |
body: unknown,
|
|
|
|
| 437 |
})
|
| 438 |
|
| 439 |
/**
|
| 440 |
+
* Creates a response whose body is a Web `FormData` value.
|
| 441 |
+
*
|
| 442 |
* @category constructors
|
| 443 |
+
* @since 4.0.0
|
| 444 |
*/
|
| 445 |
export const formData = (
|
| 446 |
body: FormData,
|
|
|
|
| 455 |
})
|
| 456 |
|
| 457 |
/**
|
| 458 |
+
* Creates a streaming response from a stream of byte chunks.
|
| 459 |
+
*
|
| 460 |
+
* **Details**
|
| 461 |
+
*
|
| 462 |
+
* Optional response metadata can supply the status, headers, content type, and
|
| 463 |
+
* content length.
|
| 464 |
+
*
|
| 465 |
* @category constructors
|
| 466 |
+
* @since 4.0.0
|
| 467 |
*/
|
| 468 |
export const stream = <E>(
|
| 469 |
body: Stream.Stream<Uint8Array, E>,
|
|
|
|
| 491 |
>("effect/http/HttpPlatform" satisfies typeof HttpPlatform.key)
|
| 492 |
|
| 493 |
/**
|
| 494 |
+
* Creates a streamed file response for a file system path.
|
| 495 |
+
*
|
| 496 |
+
* **Details**
|
| 497 |
+
*
|
| 498 |
+
* The effect requires `HttpPlatform`, can fail with a platform error, and supports
|
| 499 |
+
* options for status, headers, offset, and byte range.
|
| 500 |
+
*
|
| 501 |
* @category constructors
|
| 502 |
+
* @since 4.0.0
|
| 503 |
*/
|
| 504 |
export const file = (
|
| 505 |
path: string,
|
|
|
|
| 511 |
})
|
| 512 |
| undefined
|
| 513 |
): Effect.Effect<HttpServerResponse, PlatformError, HttpPlatform> =>
|
| 514 |
+
Effect.flatMap(HttpPlatformKey, (platform) => platform.fileResponse(path, options))
|
| 515 |
|
| 516 |
/**
|
| 517 |
+
* Creates a streamed file response for a Web `File`-like value.
|
| 518 |
+
*
|
| 519 |
+
* **Details**
|
| 520 |
+
*
|
| 521 |
+
* The effect requires `HttpPlatform` and supports options for status, headers,
|
| 522 |
+
* offset, and byte range.
|
| 523 |
+
*
|
| 524 |
* @category constructors
|
| 525 |
+
* @since 4.0.0
|
| 526 |
*/
|
| 527 |
export const fileWeb = (
|
| 528 |
file: Body.HttpBody.FileLike,
|
|
|
|
| 534 |
})
|
| 535 |
| undefined
|
| 536 |
): Effect.Effect<HttpServerResponse, never, HttpPlatform> =>
|
| 537 |
+
Effect.flatMap(HttpPlatformKey, (platform) => platform.fileWebResponse(file, options))
|
| 538 |
|
| 539 |
/**
|
| 540 |
+
* Returns a response with the specified header set to the supplied value.
|
| 541 |
+
*
|
| 542 |
* @category combinators
|
| 543 |
+
* @since 4.0.0
|
| 544 |
*/
|
| 545 |
export const setHeader: {
|
| 546 |
/**
|
| 547 |
+
* Returns a response with the specified header set to the supplied value.
|
| 548 |
+
*
|
| 549 |
* @category combinators
|
| 550 |
+
* @since 4.0.0
|
| 551 |
*/
|
| 552 |
(key: string, value: string): (self: HttpServerResponse) => HttpServerResponse
|
| 553 |
/**
|
| 554 |
+
* Returns a response with the specified header set to the supplied value.
|
| 555 |
+
*
|
| 556 |
* @category combinators
|
| 557 |
+
* @since 4.0.0
|
| 558 |
*/
|
| 559 |
(self: HttpServerResponse, key: string, value: string): HttpServerResponse
|
| 560 |
} = dual(
|
|
|
|
| 567 |
)
|
| 568 |
|
| 569 |
/**
|
| 570 |
+
* Returns a response with all supplied headers set on the existing header map.
|
| 571 |
+
*
|
| 572 |
* @category combinators
|
| 573 |
+
* @since 4.0.0
|
| 574 |
*/
|
| 575 |
export const setHeaders: {
|
| 576 |
/**
|
| 577 |
+
* Returns a response with all supplied headers set on the existing header map.
|
| 578 |
+
*
|
| 579 |
* @category combinators
|
| 580 |
+
* @since 4.0.0
|
| 581 |
*/
|
| 582 |
(input: Headers.Input): (self: HttpServerResponse) => HttpServerResponse
|
| 583 |
/**
|
| 584 |
+
* Returns a response with all supplied headers set on the existing header map.
|
| 585 |
+
*
|
| 586 |
* @category combinators
|
| 587 |
+
* @since 4.0.0
|
| 588 |
*/
|
| 589 |
(self: HttpServerResponse, input: Headers.Input): HttpServerResponse
|
| 590 |
} = dual(
|
|
|
|
| 597 |
)
|
| 598 |
|
| 599 |
/**
|
| 600 |
+
* Returns a response with the cookie of the specified name removed.
|
| 601 |
+
*
|
| 602 |
* @category combinators
|
| 603 |
+
* @since 4.0.0
|
| 604 |
*/
|
| 605 |
export const removeCookie: {
|
| 606 |
/**
|
| 607 |
+
* Returns a response with the cookie of the specified name removed.
|
| 608 |
+
*
|
| 609 |
* @category combinators
|
| 610 |
+
* @since 4.0.0
|
| 611 |
*/
|
| 612 |
(name: string): (self: HttpServerResponse) => HttpServerResponse
|
| 613 |
/**
|
| 614 |
+
* Returns a response with the cookie of the specified name removed.
|
| 615 |
+
*
|
| 616 |
* @category combinators
|
| 617 |
+
* @since 4.0.0
|
| 618 |
*/
|
| 619 |
(self: HttpServerResponse, name: string): HttpServerResponse
|
| 620 |
} = dual(
|
|
|
|
| 627 |
)
|
| 628 |
|
| 629 |
/**
|
| 630 |
+
* Returns a response with its cookie collection replaced by the supplied cookies.
|
| 631 |
+
*
|
| 632 |
* @category combinators
|
| 633 |
+
* @since 4.0.0
|
| 634 |
*/
|
| 635 |
export const replaceCookies: {
|
| 636 |
/**
|
| 637 |
+
* Returns a response with its cookie collection replaced by the supplied cookies.
|
| 638 |
+
*
|
| 639 |
* @category combinators
|
| 640 |
+
* @since 4.0.0
|
| 641 |
*/
|
| 642 |
(cookies: Cookies.Cookies): (self: HttpServerResponse) => HttpServerResponse
|
| 643 |
/**
|
| 644 |
+
* Returns a response with its cookie collection replaced by the supplied cookies.
|
| 645 |
+
*
|
| 646 |
* @category combinators
|
| 647 |
+
* @since 4.0.0
|
| 648 |
*/
|
| 649 |
(self: HttpServerResponse, cookies: Cookies.Cookies): HttpServerResponse
|
| 650 |
} = dual(
|
|
|
|
| 653 |
)
|
| 654 |
|
| 655 |
/**
|
| 656 |
+
* Sets a cookie on the response.
|
| 657 |
+
*
|
| 658 |
+
* **Details**
|
| 659 |
+
*
|
| 660 |
+
* The effect fails with `CookiesError` if the cookie name, value, or options are
|
| 661 |
+
* invalid.
|
| 662 |
+
*
|
| 663 |
* @category combinators
|
| 664 |
+
* @since 4.0.0
|
| 665 |
*/
|
| 666 |
export const setCookie: {
|
| 667 |
/**
|
| 668 |
+
* Sets a cookie on the response.
|
| 669 |
+
*
|
| 670 |
+
* **Details**
|
| 671 |
+
*
|
| 672 |
+
* The effect fails with `CookiesError` if the cookie name, value, or options are
|
| 673 |
+
* invalid.
|
| 674 |
+
*
|
| 675 |
* @category combinators
|
| 676 |
+
* @since 4.0.0
|
| 677 |
*/
|
| 678 |
(name: string, value: string, options?: Cookies.Cookie["options"]): (
|
| 679 |
self: HttpServerResponse
|
| 680 |
) => Effect.Effect<HttpServerResponse, Cookies.CookiesError>
|
| 681 |
/**
|
| 682 |
+
* Sets a cookie on the response.
|
| 683 |
+
*
|
| 684 |
+
* **Details**
|
| 685 |
+
*
|
| 686 |
+
* The effect fails with `CookiesError` if the cookie name, value, or options are
|
| 687 |
+
* invalid.
|
| 688 |
+
*
|
| 689 |
* @category combinators
|
| 690 |
+
* @since 4.0.0
|
| 691 |
*/
|
| 692 |
(
|
| 693 |
self: HttpServerResponse,
|
|
|
|
| 704 |
options?: Cookies.Cookie["options"]
|
| 705 |
): Effect.Effect<HttpServerResponse, Cookies.CookiesError> =>
|
| 706 |
Effect.map(
|
| 707 |
+
Effect.fromResult(Cookies.set(self.cookies, name, value, options)),
|
| 708 |
(cookies) =>
|
| 709 |
makeResponse({
|
| 710 |
...self,
|
|
|
|
| 714 |
)
|
| 715 |
|
| 716 |
/**
|
| 717 |
+
* Sets an expired cookie on an `HttpServerResponse`.
|
| 718 |
+
*
|
| 719 |
+
* **Details**
|
| 720 |
+
*
|
| 721 |
+
* Returns an effect because cookie encoding can fail. The original response is not
|
| 722 |
+
* mutated; the effect succeeds with a response containing the updated cookie set.
|
| 723 |
+
*
|
| 724 |
* @category combinators
|
| 725 |
+
* @since 4.0.0
|
| 726 |
*/
|
| 727 |
export const expireCookie: {
|
| 728 |
/**
|
| 729 |
+
* Sets an expired cookie on an `HttpServerResponse`.
|
| 730 |
+
*
|
| 731 |
+
* **Details**
|
| 732 |
+
*
|
| 733 |
+
* Returns an effect because cookie encoding can fail. The original response is not
|
| 734 |
+
* mutated; the effect succeeds with a response containing the updated cookie set.
|
| 735 |
+
*
|
| 736 |
* @category combinators
|
| 737 |
+
* @since 4.0.0
|
| 738 |
*/
|
| 739 |
(
|
| 740 |
name: string,
|
|
|
|
| 743 |
self: HttpServerResponse
|
| 744 |
) => Effect.Effect<HttpServerResponse, Cookies.CookiesError>
|
| 745 |
/**
|
| 746 |
+
* Sets an expired cookie on an `HttpServerResponse`.
|
| 747 |
+
*
|
| 748 |
+
* **Details**
|
| 749 |
+
*
|
| 750 |
+
* Returns an effect because cookie encoding can fail. The original response is not
|
| 751 |
+
* mutated; the effect succeeds with a response containing the updated cookie set.
|
| 752 |
+
*
|
| 753 |
* @category combinators
|
| 754 |
+
* @since 4.0.0
|
| 755 |
*/
|
| 756 |
(
|
| 757 |
self: HttpServerResponse,
|
|
|
|
| 766 |
options?: Omit<NonNullable<Cookies.Cookie["options"]>, "expires" | "maxAge">
|
| 767 |
): Effect.Effect<HttpServerResponse, Cookies.CookiesError> =>
|
| 768 |
Effect.map(
|
| 769 |
+
Effect.fromResult(Cookies.expireCookie(self.cookies, name, options)),
|
| 770 |
(cookies) =>
|
| 771 |
makeResponse({
|
| 772 |
...self,
|
|
|
|
| 776 |
)
|
| 777 |
|
| 778 |
/**
|
| 779 |
+
* Sets a cookie on an `HttpServerResponse`, throwing if the cookie cannot be
|
| 780 |
+
* encoded.
|
| 781 |
+
*
|
| 782 |
+
* **When to use**
|
| 783 |
+
*
|
| 784 |
+
* Use when you need to set one trusted cookie and want encoding failures to
|
| 785 |
+
* throw instead of being represented as `CookiesError` failures.
|
| 786 |
+
*
|
| 787 |
* @category combinators
|
| 788 |
+
* @since 4.0.0
|
| 789 |
*/
|
| 790 |
export const setCookieUnsafe: {
|
| 791 |
/**
|
| 792 |
+
* Sets a cookie on an `HttpServerResponse`, throwing if the cookie cannot be
|
| 793 |
+
* encoded.
|
| 794 |
+
*
|
| 795 |
+
* **When to use**
|
| 796 |
+
*
|
| 797 |
+
* Use when you need to set one trusted cookie and want encoding failures to
|
| 798 |
+
* throw instead of being represented as `CookiesError` failures.
|
| 799 |
+
*
|
| 800 |
* @category combinators
|
| 801 |
+
* @since 4.0.0
|
| 802 |
*/
|
| 803 |
(name: string, value: string, options?: Cookies.Cookie["options"]): (self: HttpServerResponse) => HttpServerResponse
|
| 804 |
/**
|
| 805 |
+
* Sets a cookie on an `HttpServerResponse`, throwing if the cookie cannot be
|
| 806 |
+
* encoded.
|
| 807 |
+
*
|
| 808 |
+
* **When to use**
|
| 809 |
+
*
|
| 810 |
+
* Use when you need to set one trusted cookie and want encoding failures to
|
| 811 |
+
* throw instead of being represented as `CookiesError` failures.
|
| 812 |
+
*
|
| 813 |
* @category combinators
|
| 814 |
+
* @since 4.0.0
|
| 815 |
*/
|
| 816 |
(
|
| 817 |
self: HttpServerResponse,
|
|
|
|
| 834 |
)
|
| 835 |
|
| 836 |
/**
|
| 837 |
+
* Sets an expired cookie on an `HttpServerResponse`, throwing if the expiration cookie
|
| 838 |
+
* cannot be encoded.
|
| 839 |
+
*
|
| 840 |
+
* **When to use**
|
| 841 |
+
*
|
| 842 |
+
* Use when you need to expire one trusted cookie and want encoding failures to
|
| 843 |
+
* throw instead of being represented as `CookiesError` failures.
|
| 844 |
+
*
|
| 845 |
* @category combinators
|
| 846 |
+
* @since 4.0.0
|
| 847 |
*/
|
| 848 |
export const expireCookieUnsafe: {
|
| 849 |
/**
|
| 850 |
+
* Sets an expired cookie on an `HttpServerResponse`, throwing if the expiration cookie
|
| 851 |
+
* cannot be encoded.
|
| 852 |
+
*
|
| 853 |
+
* **When to use**
|
| 854 |
+
*
|
| 855 |
+
* Use when you need to expire one trusted cookie and want encoding failures to
|
| 856 |
+
* throw instead of being represented as `CookiesError` failures.
|
| 857 |
+
*
|
| 858 |
* @category combinators
|
| 859 |
+
* @since 4.0.0
|
| 860 |
*/
|
| 861 |
(
|
| 862 |
name: string,
|
| 863 |
options?: Omit<NonNullable<Cookies.Cookie["options"]>, "expires" | "maxAge">
|
| 864 |
): (self: HttpServerResponse) => HttpServerResponse
|
| 865 |
/**
|
| 866 |
+
* Sets an expired cookie on an `HttpServerResponse`, throwing if the expiration cookie
|
| 867 |
+
* cannot be encoded.
|
| 868 |
+
*
|
| 869 |
+
* **When to use**
|
| 870 |
+
*
|
| 871 |
+
* Use when you need to expire one trusted cookie and want encoding failures to
|
| 872 |
+
* throw instead of being represented as `CookiesError` failures.
|
| 873 |
+
*
|
| 874 |
* @category combinators
|
| 875 |
+
* @since 4.0.0
|
| 876 |
*/
|
| 877 |
(
|
| 878 |
self: HttpServerResponse,
|
|
|
|
| 893 |
)
|
| 894 |
|
| 895 |
/**
|
| 896 |
+
* Updates the cookies attached to an `HttpServerResponse` using the supplied
|
| 897 |
+
* function.
|
| 898 |
+
*
|
| 899 |
+
* **Details**
|
| 900 |
+
*
|
| 901 |
+
* The original response is not mutated; a new response is returned with the
|
| 902 |
+
* callback result as its cookie collection.
|
| 903 |
+
*
|
| 904 |
* @category combinators
|
| 905 |
+
* @since 4.0.0
|
| 906 |
*/
|
| 907 |
export const updateCookies: {
|
| 908 |
/**
|
| 909 |
+
* Updates the cookies attached to an `HttpServerResponse` using the supplied
|
| 910 |
+
* function.
|
| 911 |
+
*
|
| 912 |
+
* **Details**
|
| 913 |
+
*
|
| 914 |
+
* The original response is not mutated; a new response is returned with the
|
| 915 |
+
* callback result as its cookie collection.
|
| 916 |
+
*
|
| 917 |
* @category combinators
|
| 918 |
+
* @since 4.0.0
|
| 919 |
*/
|
| 920 |
(f: (cookies: Cookies.Cookies) => Cookies.Cookies): (self: HttpServerResponse) => HttpServerResponse
|
| 921 |
/**
|
| 922 |
+
* Updates the cookies attached to an `HttpServerResponse` using the supplied
|
| 923 |
+
* function.
|
| 924 |
+
*
|
| 925 |
+
* **Details**
|
| 926 |
+
*
|
| 927 |
+
* The original response is not mutated; a new response is returned with the
|
| 928 |
+
* callback result as its cookie collection.
|
| 929 |
+
*
|
| 930 |
* @category combinators
|
| 931 |
+
* @since 4.0.0
|
| 932 |
*/
|
| 933 |
(self: HttpServerResponse, f: (cookies: Cookies.Cookies) => Cookies.Cookies): HttpServerResponse
|
| 934 |
} = dual(
|
|
|
|
| 944 |
)
|
| 945 |
|
| 946 |
/**
|
| 947 |
+
* Merges additional cookies into the cookies attached to an
|
| 948 |
+
* `HttpServerResponse`.
|
| 949 |
+
*
|
| 950 |
+
* **Details**
|
| 951 |
+
*
|
| 952 |
+
* The original response is not mutated; a new response is returned with the merged
|
| 953 |
+
* cookie collection.
|
| 954 |
+
*
|
| 955 |
* @category combinators
|
| 956 |
+
* @since 4.0.0
|
| 957 |
*/
|
| 958 |
export const mergeCookies: {
|
| 959 |
/**
|
| 960 |
+
* Merges additional cookies into the cookies attached to an
|
| 961 |
+
* `HttpServerResponse`.
|
| 962 |
+
*
|
| 963 |
+
* **Details**
|
| 964 |
+
*
|
| 965 |
+
* The original response is not mutated; a new response is returned with the merged
|
| 966 |
+
* cookie collection.
|
| 967 |
+
*
|
| 968 |
* @category combinators
|
| 969 |
+
* @since 4.0.0
|
| 970 |
*/
|
| 971 |
(cookies: Cookies.Cookies): (self: HttpServerResponse) => HttpServerResponse
|
| 972 |
/**
|
| 973 |
+
* Merges additional cookies into the cookies attached to an
|
| 974 |
+
* `HttpServerResponse`.
|
| 975 |
+
*
|
| 976 |
+
* **Details**
|
| 977 |
+
*
|
| 978 |
+
* The original response is not mutated; a new response is returned with the merged
|
| 979 |
+
* cookie collection.
|
| 980 |
+
*
|
| 981 |
* @category combinators
|
| 982 |
+
* @since 4.0.0
|
| 983 |
*/
|
| 984 |
(self: HttpServerResponse, cookies: Cookies.Cookies): HttpServerResponse
|
| 985 |
} = dual(
|
|
|
|
| 989 |
)
|
| 990 |
|
| 991 |
/**
|
| 992 |
+
* Sets multiple cookies on an `HttpServerResponse`.
|
| 993 |
+
*
|
| 994 |
+
* **Details**
|
| 995 |
+
*
|
| 996 |
+
* Each input entry contains a cookie name, value, and optional cookie options. The
|
| 997 |
+
* returned effect fails with `CookiesError` if any cookie cannot be encoded.
|
| 998 |
+
*
|
| 999 |
* @category combinators
|
| 1000 |
+
* @since 4.0.0
|
| 1001 |
*/
|
| 1002 |
export const setCookies: {
|
| 1003 |
/**
|
| 1004 |
+
* Sets multiple cookies on an `HttpServerResponse`.
|
| 1005 |
+
*
|
| 1006 |
+
* **Details**
|
| 1007 |
+
*
|
| 1008 |
+
* Each input entry contains a cookie name, value, and optional cookie options. The
|
| 1009 |
+
* returned effect fails with `CookiesError` if any cookie cannot be encoded.
|
| 1010 |
+
*
|
| 1011 |
* @category combinators
|
| 1012 |
+
* @since 4.0.0
|
| 1013 |
*/
|
| 1014 |
(
|
| 1015 |
cookies: Iterable<
|
|
|
|
| 1023 |
self: HttpServerResponse
|
| 1024 |
) => Effect.Effect<HttpServerResponse, Cookies.CookiesError, never>
|
| 1025 |
/**
|
| 1026 |
+
* Sets multiple cookies on an `HttpServerResponse`.
|
| 1027 |
+
*
|
| 1028 |
+
* **Details**
|
| 1029 |
+
*
|
| 1030 |
+
* Each input entry contains a cookie name, value, and optional cookie options. The
|
| 1031 |
+
* returned effect fails with `CookiesError` if any cookie cannot be encoded.
|
| 1032 |
+
*
|
| 1033 |
* @category combinators
|
| 1034 |
+
* @since 4.0.0
|
| 1035 |
*/
|
| 1036 |
(
|
| 1037 |
self: HttpServerResponse,
|
|
|
|
| 1055 |
]
|
| 1056 |
>
|
| 1057 |
): Effect.Effect<HttpServerResponse, Cookies.CookiesError> =>
|
| 1058 |
+
Effect.map(Effect.fromResult(Cookies.setAll(self.cookies, cookies)), (cookies) =>
|
| 1059 |
makeResponse({
|
| 1060 |
...self,
|
| 1061 |
cookies
|
|
|
|
| 1063 |
)
|
| 1064 |
|
| 1065 |
/**
|
| 1066 |
+
* Sets multiple cookies on an `HttpServerResponse`, throwing if any cookie cannot
|
| 1067 |
+
* be encoded.
|
| 1068 |
+
*
|
| 1069 |
+
* **When to use**
|
| 1070 |
+
*
|
| 1071 |
+
* Use when you need to set multiple trusted cookies and want encoding failures
|
| 1072 |
+
* to throw instead of being represented as `CookiesError` failures.
|
| 1073 |
+
*
|
| 1074 |
* @category combinators
|
| 1075 |
+
* @since 4.0.0
|
| 1076 |
*/
|
| 1077 |
export const setCookiesUnsafe: {
|
| 1078 |
/**
|
| 1079 |
+
* Sets multiple cookies on an `HttpServerResponse`, throwing if any cookie cannot
|
| 1080 |
+
* be encoded.
|
| 1081 |
+
*
|
| 1082 |
+
* **When to use**
|
| 1083 |
+
*
|
| 1084 |
+
* Use when you need to set multiple trusted cookies and want encoding failures
|
| 1085 |
+
* to throw instead of being represented as `CookiesError` failures.
|
| 1086 |
+
*
|
| 1087 |
* @category combinators
|
| 1088 |
+
* @since 4.0.0
|
| 1089 |
*/
|
| 1090 |
(
|
| 1091 |
cookies: Iterable<
|
|
|
|
| 1097 |
>
|
| 1098 |
): (self: HttpServerResponse) => HttpServerResponse
|
| 1099 |
/**
|
| 1100 |
+
* Sets multiple cookies on an `HttpServerResponse`, throwing if any cookie cannot
|
| 1101 |
+
* be encoded.
|
| 1102 |
+
*
|
| 1103 |
+
* **When to use**
|
| 1104 |
+
*
|
| 1105 |
+
* Use when you need to set multiple trusted cookies and want encoding failures
|
| 1106 |
+
* to throw instead of being represented as `CookiesError` failures.
|
| 1107 |
+
*
|
| 1108 |
* @category combinators
|
| 1109 |
+
* @since 4.0.0
|
| 1110 |
*/
|
| 1111 |
(
|
| 1112 |
self: HttpServerResponse,
|
|
|
|
| 1137 |
)
|
| 1138 |
|
| 1139 |
/**
|
| 1140 |
+
* Replaces the body of an `HttpServerResponse`.
|
| 1141 |
+
*
|
| 1142 |
+
* **Details**
|
| 1143 |
+
*
|
| 1144 |
+
* When the body carries a content type or content length, the returned response
|
| 1145 |
+
* includes the corresponding headers.
|
| 1146 |
+
*
|
| 1147 |
* @category combinators
|
| 1148 |
+
* @since 4.0.0
|
| 1149 |
*/
|
| 1150 |
export const setBody: {
|
| 1151 |
/**
|
| 1152 |
+
* Replaces the body of an `HttpServerResponse`.
|
| 1153 |
+
*
|
| 1154 |
+
* **Details**
|
| 1155 |
+
*
|
| 1156 |
+
* When the body carries a content type or content length, the returned response
|
| 1157 |
+
* includes the corresponding headers.
|
| 1158 |
+
*
|
| 1159 |
* @category combinators
|
| 1160 |
+
* @since 4.0.0
|
| 1161 |
*/
|
| 1162 |
(body: Body.HttpBody): (self: HttpServerResponse) => HttpServerResponse
|
| 1163 |
/**
|
| 1164 |
+
* Replaces the body of an `HttpServerResponse`.
|
| 1165 |
+
*
|
| 1166 |
+
* **Details**
|
| 1167 |
+
*
|
| 1168 |
+
* When the body carries a content type or content length, the returned response
|
| 1169 |
+
* includes the corresponding headers.
|
| 1170 |
+
*
|
| 1171 |
* @category combinators
|
| 1172 |
+
* @since 4.0.0
|
| 1173 |
*/
|
| 1174 |
(self: HttpServerResponse, body: Body.HttpBody): HttpServerResponse
|
| 1175 |
} = dual(
|
|
|
|
| 1178 |
)
|
| 1179 |
|
| 1180 |
/**
|
| 1181 |
+
* Sets the HTTP status code of an `HttpServerResponse`.
|
| 1182 |
+
*
|
| 1183 |
+
* **Details**
|
| 1184 |
+
*
|
| 1185 |
+
* When `statusText` is omitted, the existing status text is preserved.
|
| 1186 |
+
*
|
| 1187 |
* @category combinators
|
| 1188 |
+
* @since 4.0.0
|
| 1189 |
*/
|
| 1190 |
export const setStatus: {
|
| 1191 |
/**
|
| 1192 |
+
* Sets the HTTP status code of an `HttpServerResponse`.
|
| 1193 |
+
*
|
| 1194 |
+
* **Details**
|
| 1195 |
+
*
|
| 1196 |
+
* When `statusText` is omitted, the existing status text is preserved.
|
| 1197 |
+
*
|
| 1198 |
* @category combinators
|
| 1199 |
+
* @since 4.0.0
|
| 1200 |
*/
|
| 1201 |
(status: number, statusText?: string | undefined): (self: HttpServerResponse) => HttpServerResponse
|
| 1202 |
/**
|
| 1203 |
+
* Sets the HTTP status code of an `HttpServerResponse`.
|
| 1204 |
+
*
|
| 1205 |
+
* **Details**
|
| 1206 |
+
*
|
| 1207 |
+
* When `statusText` is omitted, the existing status text is preserved.
|
| 1208 |
+
*
|
| 1209 |
* @category combinators
|
| 1210 |
+
* @since 4.0.0
|
| 1211 |
*/
|
| 1212 |
(self: HttpServerResponse, status: number, statusText?: string | undefined): HttpServerResponse
|
| 1213 |
} = dual(
|
|
|
|
| 1225 |
)
|
| 1226 |
|
| 1227 |
/**
|
| 1228 |
+
* Converts an `HttpServerResponse` to a Web `Response`.
|
| 1229 |
+
*
|
| 1230 |
+
* **Details**
|
| 1231 |
+
*
|
| 1232 |
+
* Cookies are appended as `Set-Cookie` headers. Stream bodies are converted using
|
| 1233 |
+
* the supplied context, and `withoutBody` can be used for responses such as HEAD
|
| 1234 |
+
* responses.
|
| 1235 |
+
*
|
| 1236 |
+
* @category converting
|
| 1237 |
* @since 4.0.0
|
|
|
|
| 1238 |
*/
|
| 1239 |
export const toWeb = (
|
| 1240 |
response: HttpServerResponse,
|
|
|
|
| 1304 |
}
|
| 1305 |
|
| 1306 |
/**
|
| 1307 |
+
* Wraps an `HttpServerResponse` as an `HttpClientResponse`.
|
| 1308 |
+
*
|
| 1309 |
+
* **Details**
|
| 1310 |
+
*
|
| 1311 |
+
* An optional request can be supplied for client-response metadata and decode
|
| 1312 |
+
* errors.
|
| 1313 |
+
*
|
| 1314 |
+
* @category converting
|
| 1315 |
* @since 4.0.0
|
|
|
|
| 1316 |
*/
|
| 1317 |
export const toClientResponse = (
|
| 1318 |
response: HttpServerResponse,
|
|
|
|
| 1525 |
const textDecoder = new TextDecoder()
|
| 1526 |
|
| 1527 |
/**
|
| 1528 |
+
* Converts an `HttpClientResponse` to an `HttpServerResponse`.
|
| 1529 |
+
*
|
| 1530 |
+
* **Details**
|
| 1531 |
+
*
|
| 1532 |
+
* The response body is streamed from the client response. `Set-Cookie` headers are
|
| 1533 |
+
* removed from the header map and represented in the response cookie collection.
|
| 1534 |
+
*
|
| 1535 |
+
* @category converting
|
| 1536 |
* @since 4.0.0
|
|
|
|
| 1537 |
*/
|
| 1538 |
export const fromClientResponse = (
|
| 1539 |
response: HttpClientResponse.HttpClientResponse
|
|
|
|
| 1618 |
}
|
| 1619 |
|
| 1620 |
/**
|
| 1621 |
+
* Converts a Web `Response` to an `HttpServerResponse`.
|
| 1622 |
+
*
|
| 1623 |
+
* **Details**
|
| 1624 |
+
*
|
| 1625 |
+
* `Set-Cookie` headers are parsed into the response cookie collection and removed
|
| 1626 |
+
* from the header map. A present Web body is exposed as a stream body.
|
| 1627 |
+
*
|
| 1628 |
+
* @category converting
|
| 1629 |
* @since 4.0.0
|
|
|
|
| 1630 |
*/
|
| 1631 |
export const fromWeb = (response: Response): HttpServerResponse => {
|
| 1632 |
const headers = new globalThis.Headers(response.headers)
|
.opencode/node_modules/effect/src/unstable/http/HttpStaticServer.ts
CHANGED
|
@@ -1,4 +1,12 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Effect from "../../Effect.ts"
|
|
@@ -16,10 +24,11 @@ import * as HttpServerResponse from "./HttpServerResponse.ts"
|
|
| 16 |
/**
|
| 17 |
* Creates an `HttpApp` that serves files from a directory.
|
| 18 |
*
|
| 19 |
-
*
|
|
|
|
| 20 |
* ```ts
|
| 21 |
* import { Effect } from "effect"
|
| 22 |
-
* import
|
| 23 |
*
|
| 24 |
* const program = Effect.gen(function*() {
|
| 25 |
* const app = yield* HttpStaticServer.make({ root: "./public" })
|
|
@@ -27,8 +36,8 @@ import * as HttpServerResponse from "./HttpServerResponse.ts"
|
|
| 27 |
* })
|
| 28 |
* ```
|
| 29 |
*
|
| 30 |
-
* @since 4.0.0
|
| 31 |
* @category constructors
|
|
|
|
| 32 |
*/
|
| 33 |
export const make: (options: {
|
| 34 |
readonly root: string
|
|
@@ -168,12 +177,11 @@ export const make: (options: {
|
|
| 168 |
/**
|
| 169 |
* Creates a layer that mounts static files on an `HttpRouter`.
|
| 170 |
*
|
| 171 |
-
*
|
|
|
|
| 172 |
* ```ts
|
| 173 |
* import { Layer } from "effect"
|
| 174 |
-
* import
|
| 175 |
-
* import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse"
|
| 176 |
-
* import * as HttpStaticServer from "effect/unstable/http/HttpStaticServer"
|
| 177 |
*
|
| 178 |
* const ApiLayer = HttpRouter.add("GET", "/health", HttpServerResponse.text("ok"))
|
| 179 |
*
|
|
@@ -185,8 +193,8 @@ export const make: (options: {
|
|
| 185 |
* const AppLayer = Layer.mergeAll(ApiLayer, StaticFilesLayer)
|
| 186 |
* ```
|
| 187 |
*
|
| 188 |
-
* @since 4.0.0
|
| 189 |
* @category layers
|
|
|
|
| 190 |
*/
|
| 191 |
export const layer = (options: {
|
| 192 |
readonly root: string
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Serves static files for Effect HTTP applications.
|
| 3 |
+
*
|
| 4 |
+
* `HttpStaticServer` turns request paths into file responses under a configured
|
| 5 |
+
* root directory. It can be used as an application value or mounted onto an
|
| 6 |
+
* `HttpRouter`, and it handles index files, optional single-page application
|
| 7 |
+
* fallback, MIME type headers, cache-control headers, byte ranges, and
|
| 8 |
+
* conditional `304 Not Modified` responses.
|
| 9 |
+
*
|
| 10 |
* @since 4.0.0
|
| 11 |
*/
|
| 12 |
import * as Effect from "../../Effect.ts"
|
|
|
|
| 24 |
/**
|
| 25 |
* Creates an `HttpApp` that serves files from a directory.
|
| 26 |
*
|
| 27 |
+
* **Example** (Serving files from a directory)
|
| 28 |
+
*
|
| 29 |
* ```ts
|
| 30 |
* import { Effect } from "effect"
|
| 31 |
+
* import { HttpStaticServer } from "effect/unstable/http"
|
| 32 |
*
|
| 33 |
* const program = Effect.gen(function*() {
|
| 34 |
* const app = yield* HttpStaticServer.make({ root: "./public" })
|
|
|
|
| 36 |
* })
|
| 37 |
* ```
|
| 38 |
*
|
|
|
|
| 39 |
* @category constructors
|
| 40 |
+
* @since 4.0.0
|
| 41 |
*/
|
| 42 |
export const make: (options: {
|
| 43 |
readonly root: string
|
|
|
|
| 177 |
/**
|
| 178 |
* Creates a layer that mounts static files on an `HttpRouter`.
|
| 179 |
*
|
| 180 |
+
* **Example** (Mounting static files on a router)
|
| 181 |
+
*
|
| 182 |
* ```ts
|
| 183 |
* import { Layer } from "effect"
|
| 184 |
+
* import { HttpRouter, HttpServerResponse, HttpStaticServer } from "effect/unstable/http"
|
|
|
|
|
|
|
| 185 |
*
|
| 186 |
* const ApiLayer = HttpRouter.add("GET", "/health", HttpServerResponse.text("ok"))
|
| 187 |
*
|
|
|
|
| 193 |
* const AppLayer = Layer.mergeAll(ApiLayer, StaticFilesLayer)
|
| 194 |
* ```
|
| 195 |
*
|
|
|
|
| 196 |
* @category layers
|
| 197 |
+
* @since 4.0.0
|
| 198 |
*/
|
| 199 |
export const layer = (options: {
|
| 200 |
readonly root: string
|
.opencode/node_modules/effect/src/unstable/http/HttpTraceContext.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Option from "../../Option.ts"
|
|
@@ -6,16 +13,30 @@ import * as Tracer from "../../Tracer.ts"
|
|
| 6 |
import * as Headers from "./Headers.ts"
|
| 7 |
|
| 8 |
/**
|
| 9 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
* @category models
|
|
|
|
| 11 |
*/
|
| 12 |
export interface FromHeaders {
|
| 13 |
(headers: Headers.Headers): Option.Option<Tracer.ExternalSpan>
|
| 14 |
}
|
| 15 |
|
| 16 |
/**
|
| 17 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
* @category encoding
|
|
|
|
| 19 |
*/
|
| 20 |
export const toHeaders = (span: Tracer.Span): Headers.Headers =>
|
| 21 |
Headers.fromRecordUnsafe({
|
|
@@ -29,8 +50,15 @@ export const toHeaders = (span: Tracer.Span): Headers.Headers =>
|
|
| 29 |
})
|
| 30 |
|
| 31 |
/**
|
| 32 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
* @category decoding
|
|
|
|
| 34 |
*/
|
| 35 |
export const fromHeaders = (headers: Headers.Headers): Option.Option<Tracer.ExternalSpan> => {
|
| 36 |
let span = w3c(headers)
|
|
@@ -45,8 +73,15 @@ export const fromHeaders = (headers: Headers.Headers): Option.Option<Tracer.Exte
|
|
| 45 |
}
|
| 46 |
|
| 47 |
/**
|
| 48 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
* @category decoding
|
|
|
|
| 50 |
*/
|
| 51 |
export const b3: FromHeaders = (headers) => {
|
| 52 |
if (!("b3" in headers)) {
|
|
@@ -64,8 +99,15 @@ export const b3: FromHeaders = (headers) => {
|
|
| 64 |
}
|
| 65 |
|
| 66 |
/**
|
| 67 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
* @category decoding
|
|
|
|
| 69 |
*/
|
| 70 |
export const xb3: FromHeaders = (headers) => {
|
| 71 |
if (!(headers["x-b3-traceid"]) || !(headers["x-b3-spanid"])) {
|
|
@@ -82,8 +124,14 @@ const w3cTraceId = /^[0-9a-f]{32}$/i
|
|
| 82 |
const w3cSpanId = /^[0-9a-f]{16}$/i
|
| 83 |
|
| 84 |
/**
|
| 85 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
* @category decoding
|
|
|
|
| 87 |
*/
|
| 88 |
export const w3c: FromHeaders = (headers) => {
|
| 89 |
if (!(headers["traceparent"])) {
|
|
|
|
| 1 |
/**
|
| 2 |
+
* HTTP propagation helpers for Effect tracing context.
|
| 3 |
+
*
|
| 4 |
+
* This module converts Effect `Tracer.Span` values into outbound trace headers
|
| 5 |
+
* and decodes inbound propagation headers into `Tracer.ExternalSpan` parents.
|
| 6 |
+
* HTTP clients use it to continue the current span across outgoing requests, and
|
| 7 |
+
* server middleware uses it to parent request spans from upstream services.
|
| 8 |
+
*
|
| 9 |
* @since 4.0.0
|
| 10 |
*/
|
| 11 |
import * as Option from "../../Option.ts"
|
|
|
|
| 13 |
import * as Headers from "./Headers.ts"
|
| 14 |
|
| 15 |
/**
|
| 16 |
+
* Function type for decoding tracing headers into an external span.
|
| 17 |
+
*
|
| 18 |
+
* **Details**
|
| 19 |
+
*
|
| 20 |
+
* Returns `Option.none` when the headers do not contain a supported or valid trace
|
| 21 |
+
* context.
|
| 22 |
+
*
|
| 23 |
* @category models
|
| 24 |
+
* @since 4.0.0
|
| 25 |
*/
|
| 26 |
export interface FromHeaders {
|
| 27 |
(headers: Headers.Headers): Option.Option<Tracer.ExternalSpan>
|
| 28 |
}
|
| 29 |
|
| 30 |
/**
|
| 31 |
+
* Encodes a span into HTTP trace propagation headers.
|
| 32 |
+
*
|
| 33 |
+
* **Details**
|
| 34 |
+
*
|
| 35 |
+
* The generated headers include both compact B3 (`b3`) and W3C `traceparent`
|
| 36 |
+
* formats.
|
| 37 |
+
*
|
| 38 |
* @category encoding
|
| 39 |
+
* @since 4.0.0
|
| 40 |
*/
|
| 41 |
export const toHeaders = (span: Tracer.Span): Headers.Headers =>
|
| 42 |
Headers.fromRecordUnsafe({
|
|
|
|
| 50 |
})
|
| 51 |
|
| 52 |
/**
|
| 53 |
+
* Decodes an external span safely from HTTP trace propagation headers.
|
| 54 |
+
*
|
| 55 |
+
* **Details**
|
| 56 |
+
*
|
| 57 |
+
* W3C `traceparent` is tried first, followed by compact B3 (`b3`) and then
|
| 58 |
+
* multi-header B3 (`x-b3-*`).
|
| 59 |
+
*
|
| 60 |
* @category decoding
|
| 61 |
+
* @since 4.0.0
|
| 62 |
*/
|
| 63 |
export const fromHeaders = (headers: Headers.Headers): Option.Option<Tracer.ExternalSpan> => {
|
| 64 |
let span = w3c(headers)
|
|
|
|
| 73 |
}
|
| 74 |
|
| 75 |
/**
|
| 76 |
+
* Decodes an external span safely from the compact B3 `b3` header.
|
| 77 |
+
*
|
| 78 |
+
* **Details**
|
| 79 |
+
*
|
| 80 |
+
* Returns `Option.none` when the header is missing or does not contain trace and
|
| 81 |
+
* span identifiers.
|
| 82 |
+
*
|
| 83 |
* @category decoding
|
| 84 |
+
* @since 4.0.0
|
| 85 |
*/
|
| 86 |
export const b3: FromHeaders = (headers) => {
|
| 87 |
if (!("b3" in headers)) {
|
|
|
|
| 99 |
}
|
| 100 |
|
| 101 |
/**
|
| 102 |
+
* Decodes an external span safely from multi-header B3 propagation headers.
|
| 103 |
+
*
|
| 104 |
+
* **Details**
|
| 105 |
+
*
|
| 106 |
+
* The decoder reads `x-b3-traceid`, `x-b3-spanid`, and optional `x-b3-sampled`
|
| 107 |
+
* headers.
|
| 108 |
+
*
|
| 109 |
* @category decoding
|
| 110 |
+
* @since 4.0.0
|
| 111 |
*/
|
| 112 |
export const xb3: FromHeaders = (headers) => {
|
| 113 |
if (!(headers["x-b3-traceid"]) || !(headers["x-b3-spanid"])) {
|
|
|
|
| 124 |
const w3cSpanId = /^[0-9a-f]{16}$/i
|
| 125 |
|
| 126 |
/**
|
| 127 |
+
* Decodes an external span safely from the W3C `traceparent` header.
|
| 128 |
+
*
|
| 129 |
+
* **Details**
|
| 130 |
+
*
|
| 131 |
+
* Only version `00` headers with valid trace and span identifiers are accepted.
|
| 132 |
+
*
|
| 133 |
* @category decoding
|
| 134 |
+
* @since 4.0.0
|
| 135 |
*/
|
| 136 |
export const w3c: FromHeaders = (headers) => {
|
| 137 |
if (!(headers["traceparent"])) {
|
.opencode/node_modules/effect/src/unstable/http/Multipart.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
import * as Arr from "../../Array.ts"
|
|
@@ -17,7 +26,7 @@ import * as Predicate from "../../Predicate.ts"
|
|
| 17 |
import * as Pull from "../../Pull.ts"
|
| 18 |
import * as Schema from "../../Schema.ts"
|
| 19 |
import type { ParseOptions } from "../../SchemaAST.ts"
|
| 20 |
-
import * as
|
| 21 |
import type * as Scope from "../../Scope.ts"
|
| 22 |
import * as Stream from "../../Stream.ts"
|
| 23 |
import * as UndefinedOr from "../../UndefinedOr.ts"
|
|
@@ -25,23 +34,41 @@ import * as IncomingMessage from "./HttpIncomingMessage.ts"
|
|
| 25 |
import * as MP from "./Multipasta.ts"
|
| 26 |
|
| 27 |
/**
|
|
|
|
|
|
|
|
|
|
| 28 |
* @since 4.0.0
|
| 29 |
*/
|
| 30 |
export const TypeId = "~effect/http/Multipart"
|
| 31 |
|
| 32 |
/**
|
| 33 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
* @category models
|
|
|
|
| 35 |
*/
|
| 36 |
export type Part = Field | File
|
| 37 |
|
| 38 |
/**
|
|
|
|
|
|
|
| 39 |
* @since 4.0.0
|
| 40 |
*/
|
| 41 |
export declare namespace Part {
|
| 42 |
/**
|
| 43 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
* @category models
|
|
|
|
| 45 |
*/
|
| 46 |
export interface Proto extends Inspectable.Inspectable {
|
| 47 |
readonly [TypeId]: typeof TypeId
|
|
@@ -50,8 +77,15 @@ export declare namespace Part {
|
|
| 50 |
}
|
| 51 |
|
| 52 |
/**
|
| 53 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
* @category models
|
|
|
|
| 55 |
*/
|
| 56 |
export interface Field extends Part.Proto {
|
| 57 |
readonly _tag: "Field"
|
|
@@ -61,20 +95,31 @@ export interface Field extends Part.Proto {
|
|
| 61 |
}
|
| 62 |
|
| 63 |
/**
|
|
|
|
|
|
|
|
|
|
| 64 |
* @since 4.0.0
|
| 65 |
-
* @category Guards
|
| 66 |
*/
|
| 67 |
export const isPart = (u: unknown): u is Part => Predicate.hasProperty(u, TypeId)
|
| 68 |
|
| 69 |
/**
|
|
|
|
|
|
|
|
|
|
| 70 |
* @since 4.0.0
|
| 71 |
-
* @category Guards
|
| 72 |
*/
|
| 73 |
export const isField = (u: unknown): u is Field => isPart(u) && u._tag === "Field"
|
| 74 |
|
| 75 |
/**
|
| 76 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
* @category models
|
|
|
|
| 78 |
*/
|
| 79 |
export interface File extends Part.Proto {
|
| 80 |
readonly _tag: "File"
|
|
@@ -86,14 +131,23 @@ export interface File extends Part.Proto {
|
|
| 86 |
}
|
| 87 |
|
| 88 |
/**
|
|
|
|
|
|
|
|
|
|
| 89 |
* @since 4.0.0
|
| 90 |
-
* @category Guards
|
| 91 |
*/
|
| 92 |
export const isFile = (u: unknown): u is File => isPart(u) && u._tag === "File"
|
| 93 |
|
| 94 |
/**
|
| 95 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
* @category models
|
|
|
|
| 97 |
*/
|
| 98 |
export interface PersistedFile extends Part.Proto {
|
| 99 |
readonly _tag: "PersistedFile"
|
|
@@ -104,15 +158,24 @@ export interface PersistedFile extends Part.Proto {
|
|
| 104 |
}
|
| 105 |
|
| 106 |
/**
|
|
|
|
|
|
|
|
|
|
| 107 |
* @since 4.0.0
|
| 108 |
-
* @category Guards
|
| 109 |
*/
|
| 110 |
export const isPersistedFile = (u: unknown): u is PersistedFile =>
|
| 111 |
Predicate.hasProperty(u, TypeId) && Predicate.isTagged(u, "PersistedFile")
|
| 112 |
|
| 113 |
/**
|
| 114 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
* @category models
|
|
|
|
| 116 |
*/
|
| 117 |
export interface Persisted {
|
| 118 |
readonly [key: string]: ReadonlyArray<PersistedFile> | ReadonlyArray<string> | string
|
|
@@ -121,8 +184,15 @@ export interface Persisted {
|
|
| 121 |
const MultipartErrorTypeId = "~effect/http/Multipart/MultipartError"
|
| 122 |
|
| 123 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
* @since 4.0.0
|
| 125 |
-
* @category Errors
|
| 126 |
*/
|
| 127 |
export class MultipartErrorReason extends Data.Error<{
|
| 128 |
readonly _tag: "FileTooLarge" | "FieldTooLarge" | "BodyTooLarge" | "TooManyParts" | "InternalError" | "Parse"
|
|
@@ -130,13 +200,21 @@ export class MultipartErrorReason extends Data.Error<{
|
|
| 130 |
}> {}
|
| 131 |
|
| 132 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
* @since 4.0.0
|
| 134 |
-
* @category Errors
|
| 135 |
*/
|
| 136 |
export class MultipartError extends Data.TaggedError("MultipartError")<{
|
| 137 |
readonly reason: MultipartErrorReason
|
| 138 |
}> {
|
| 139 |
/**
|
|
|
|
|
|
|
| 140 |
* @since 4.0.0
|
| 141 |
*/
|
| 142 |
static fromReason(reason: MultipartErrorReason["_tag"], cause?: unknown): MultipartError {
|
|
@@ -144,11 +222,15 @@ export class MultipartError extends Data.TaggedError("MultipartError")<{
|
|
| 144 |
}
|
| 145 |
|
| 146 |
/**
|
|
|
|
|
|
|
| 147 |
* @since 4.0.0
|
| 148 |
*/
|
| 149 |
readonly [MultipartErrorTypeId] = MultipartErrorTypeId
|
| 150 |
|
| 151 |
/**
|
|
|
|
|
|
|
| 152 |
* @since 4.0.0
|
| 153 |
*/
|
| 154 |
override get message(): string {
|
|
@@ -157,14 +239,23 @@ export class MultipartError extends Data.TaggedError("MultipartError")<{
|
|
| 157 |
}
|
| 158 |
|
| 159 |
/**
|
|
|
|
|
|
|
|
|
|
| 160 |
* @since 4.0.0
|
| 161 |
-
* @category Schemas
|
| 162 |
*/
|
| 163 |
export interface PersistedFileSchema extends Schema.declare<PersistedFile> {}
|
| 164 |
|
| 165 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
* @since 4.0.0
|
| 167 |
-
* @category Schemas
|
| 168 |
*/
|
| 169 |
export const PersistedFileSchema: PersistedFileSchema = Schema.declare(
|
| 170 |
isPersistedFile,
|
|
@@ -186,7 +277,7 @@ export const PersistedFileSchema: PersistedFileSchema = Schema.declare(
|
|
| 186 |
contentType: Schema.String.annotate({ contentEncoding: "binary" }),
|
| 187 |
path: Schema.String
|
| 188 |
}),
|
| 189 |
-
|
| 190 |
decode: ({ contentType, key, name, path }) => new PersistedFileImpl(key, name, contentType, path),
|
| 191 |
encode: (file) => ({
|
| 192 |
key: file.key,
|
|
@@ -200,14 +291,23 @@ export const PersistedFileSchema: PersistedFileSchema = Schema.declare(
|
|
| 200 |
)
|
| 201 |
|
| 202 |
/**
|
|
|
|
|
|
|
|
|
|
| 203 |
* @since 4.0.0
|
| 204 |
-
* @category Schemas
|
| 205 |
*/
|
| 206 |
export const FilesSchema: Schema.$Array<PersistedFileSchema> = Schema.Array(PersistedFileSchema)
|
| 207 |
|
| 208 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
* @since 4.0.0
|
| 210 |
-
* @category Schemas
|
| 211 |
*/
|
| 212 |
export const SingleFileSchema: Schema.decodeTo<PersistedFileSchema, Schema.$Array<PersistedFileSchema>> = FilesSchema
|
| 213 |
.check(
|
|
@@ -215,7 +315,7 @@ export const SingleFileSchema: Schema.decodeTo<PersistedFileSchema, Schema.$Arra
|
|
| 215 |
).pipe(
|
| 216 |
Schema.decodeTo(
|
| 217 |
PersistedFileSchema,
|
| 218 |
-
|
| 219 |
decode: ([file]) => file,
|
| 220 |
encode: (file) => [file]
|
| 221 |
})
|
|
@@ -223,8 +323,15 @@ export const SingleFileSchema: Schema.decodeTo<PersistedFileSchema, Schema.$Arra
|
|
| 223 |
)
|
| 224 |
|
| 225 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
* @since 4.0.0
|
| 227 |
-
* @category Schemas
|
| 228 |
*/
|
| 229 |
export const schemaPersisted = <A, I extends Partial<Persisted>, RD, RE>(
|
| 230 |
schema: Schema.Codec<A, I, RD, RE>
|
|
@@ -232,8 +339,15 @@ export const schemaPersisted = <A, I extends Partial<Persisted>, RD, RE>(
|
|
| 232 |
Schema.decodeUnknownEffect(schema)
|
| 233 |
|
| 234 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
* @since 4.0.0
|
| 236 |
-
* @category Schemas
|
| 237 |
*/
|
| 238 |
export const schemaJson = <A, I, RD, RE>(schema: Schema.Codec<A, I, RD, RE>, options?: ParseOptions | undefined): {
|
| 239 |
(
|
|
@@ -253,8 +367,16 @@ export const schemaJson = <A, I, RD, RE>(schema: Schema.Codec<A, I, RD, RE>, opt
|
|
| 253 |
}
|
| 254 |
|
| 255 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
* @since 4.0.0
|
| 257 |
-
* @category Config
|
| 258 |
*/
|
| 259 |
export const makeConfig = (
|
| 260 |
headers: Record<string, string>
|
|
@@ -275,8 +397,16 @@ export const makeConfig = (
|
|
| 275 |
})
|
| 276 |
|
| 277 |
/**
|
| 278 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
* @category Parsers
|
|
|
|
| 280 |
*/
|
| 281 |
export const makeChannel = <IE>(headers: Record<string, string>): Channel.Channel<
|
| 282 |
Arr.NonEmptyReadonlyArray<Part>,
|
|
@@ -455,7 +585,7 @@ class FileImpl extends PartBase implements File {
|
|
| 455 |
|
| 456 |
const defaultWriteFile = (path: string, file: File) =>
|
| 457 |
Effect.flatMap(
|
| 458 |
-
FileSystem.FileSystem
|
| 459 |
(fs) =>
|
| 460 |
Effect.mapError(
|
| 461 |
Stream.run(file.content, fs.sink(path)),
|
|
@@ -464,6 +594,14 @@ const defaultWriteFile = (path: string, file: File) =>
|
|
| 464 |
)
|
| 465 |
|
| 466 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 467 |
* @since 4.0.0
|
| 468 |
*/
|
| 469 |
export const collectUint8Array = <OE, OD, R>(
|
|
@@ -482,8 +620,19 @@ export const collectUint8Array = <OE, OD, R>(
|
|
| 482 |
})
|
| 483 |
|
| 484 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 485 |
* @since 4.0.0
|
| 486 |
-
* @category Conversions
|
| 487 |
*/
|
| 488 |
export const toPersisted = (
|
| 489 |
stream: Stream.Stream<Part, MultipartError>,
|
|
@@ -560,8 +709,15 @@ class PersistedFileImpl extends PartBase implements PersistedFile {
|
|
| 560 |
}
|
| 561 |
|
| 562 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 563 |
* @since 4.0.0
|
| 564 |
-
* @category References
|
| 565 |
*/
|
| 566 |
export const limitsServices = (options: {
|
| 567 |
readonly maxParts?: number | undefined
|
|
@@ -590,13 +746,21 @@ export const limitsServices = (options: {
|
|
| 590 |
}
|
| 591 |
|
| 592 |
/**
|
|
|
|
|
|
|
| 593 |
* @since 4.0.0
|
| 594 |
-
* @category fiber refs
|
| 595 |
*/
|
| 596 |
export declare namespace withLimits {
|
| 597 |
/**
|
| 598 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 599 |
* @category fiber refs
|
|
|
|
| 600 |
*/
|
| 601 |
export type Options = {
|
| 602 |
readonly maxParts?: number | undefined
|
|
@@ -608,24 +772,42 @@ export declare namespace withLimits {
|
|
| 608 |
}
|
| 609 |
|
| 610 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 611 |
* @since 4.0.0
|
| 612 |
-
* @category References
|
| 613 |
*/
|
| 614 |
export const MaxParts = Context.Reference<number | undefined>("effect/http/Multipart/MaxParts", {
|
| 615 |
defaultValue: () => undefined
|
| 616 |
})
|
| 617 |
|
| 618 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 619 |
* @since 4.0.0
|
| 620 |
-
* @category References
|
| 621 |
*/
|
| 622 |
export const MaxFieldSize = Context.Reference<FileSystem.SizeInput>("effect/http/Multipart/MaxFieldSize", {
|
| 623 |
defaultValue: constant(FileSystem.Size(10 * 1024 * 1024))
|
| 624 |
})
|
| 625 |
|
| 626 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 627 |
* @since 4.0.0
|
| 628 |
-
* @category References
|
| 629 |
*/
|
| 630 |
export const MaxFileSize = Context.Reference<FileSystem.SizeInput | undefined>(
|
| 631 |
"effect/http/Multipart/MaxFileSize",
|
|
@@ -633,8 +815,15 @@ export const MaxFileSize = Context.Reference<FileSystem.SizeInput | undefined>(
|
|
| 633 |
)
|
| 634 |
|
| 635 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 636 |
* @since 4.0.0
|
| 637 |
-
* @category References
|
| 638 |
*/
|
| 639 |
export const FieldMimeTypes = Context.Reference<ReadonlyArray<string>>("effect/http/Multipart/FieldMimeTypes", {
|
| 640 |
defaultValue: constant(["application/json"])
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Parses and persists HTTP `multipart/form-data` request bodies.
|
| 3 |
+
*
|
| 4 |
+
* `Multipart` turns incoming byte streams into typed form parts. Text parts
|
| 5 |
+
* become decoded fields, while upload parts stay as streamed files until they
|
| 6 |
+
* are collected or written to scoped temporary files. The persisted
|
| 7 |
+
* representation can then be decoded with schemas for handlers that receive
|
| 8 |
+
* fields and uploaded files together. This module also includes multipart error
|
| 9 |
+
* types, schema helpers for persisted files, and parser limit settings.
|
| 10 |
+
*
|
| 11 |
* @since 4.0.0
|
| 12 |
*/
|
| 13 |
import * as Arr from "../../Array.ts"
|
|
|
|
| 26 |
import * as Pull from "../../Pull.ts"
|
| 27 |
import * as Schema from "../../Schema.ts"
|
| 28 |
import type { ParseOptions } from "../../SchemaAST.ts"
|
| 29 |
+
import * as SchemaTransformation from "../../SchemaTransformation.ts"
|
| 30 |
import type * as Scope from "../../Scope.ts"
|
| 31 |
import * as Stream from "../../Stream.ts"
|
| 32 |
import * as UndefinedOr from "../../UndefinedOr.ts"
|
|
|
|
| 34 |
import * as MP from "./Multipasta.ts"
|
| 35 |
|
| 36 |
/**
|
| 37 |
+
* Type identifier used to brand multipart part values.
|
| 38 |
+
*
|
| 39 |
+
* @category type IDs
|
| 40 |
* @since 4.0.0
|
| 41 |
*/
|
| 42 |
export const TypeId = "~effect/http/Multipart"
|
| 43 |
|
| 44 |
/**
|
| 45 |
+
* A parsed multipart part.
|
| 46 |
+
*
|
| 47 |
+
* **Details**
|
| 48 |
+
*
|
| 49 |
+
* A part is either a text `Field` or a streamed `File`.
|
| 50 |
+
*
|
| 51 |
* @category models
|
| 52 |
+
* @since 4.0.0
|
| 53 |
*/
|
| 54 |
export type Part = Field | File
|
| 55 |
|
| 56 |
/**
|
| 57 |
+
* Namespace containing shared multipart part model types.
|
| 58 |
+
*
|
| 59 |
* @since 4.0.0
|
| 60 |
*/
|
| 61 |
export declare namespace Part {
|
| 62 |
/**
|
| 63 |
+
* Common protocol implemented by multipart part values.
|
| 64 |
+
*
|
| 65 |
+
* **Details**
|
| 66 |
+
*
|
| 67 |
+
* It provides the multipart type identifier, tag, and inspectable behavior shared
|
| 68 |
+
* by fields, files, and persisted files.
|
| 69 |
+
*
|
| 70 |
* @category models
|
| 71 |
+
* @since 4.0.0
|
| 72 |
*/
|
| 73 |
export interface Proto extends Inspectable.Inspectable {
|
| 74 |
readonly [TypeId]: typeof TypeId
|
|
|
|
| 77 |
}
|
| 78 |
|
| 79 |
/**
|
| 80 |
+
* Multipart form field containing a decoded text value.
|
| 81 |
+
*
|
| 82 |
+
* **Details**
|
| 83 |
+
*
|
| 84 |
+
* The `key` is the field name, `contentType` is the part media type, and `value`
|
| 85 |
+
* is the decoded field content.
|
| 86 |
+
*
|
| 87 |
* @category models
|
| 88 |
+
* @since 4.0.0
|
| 89 |
*/
|
| 90 |
export interface Field extends Part.Proto {
|
| 91 |
readonly _tag: "Field"
|
|
|
|
| 95 |
}
|
| 96 |
|
| 97 |
/**
|
| 98 |
+
* Returns `true` when a value is a multipart `Part`.
|
| 99 |
+
*
|
| 100 |
+
* @category guards
|
| 101 |
* @since 4.0.0
|
|
|
|
| 102 |
*/
|
| 103 |
export const isPart = (u: unknown): u is Part => Predicate.hasProperty(u, TypeId)
|
| 104 |
|
| 105 |
/**
|
| 106 |
+
* Returns `true` when a value is a multipart text `Field`.
|
| 107 |
+
*
|
| 108 |
+
* @category guards
|
| 109 |
* @since 4.0.0
|
|
|
|
| 110 |
*/
|
| 111 |
export const isField = (u: unknown): u is Field => isPart(u) && u._tag === "Field"
|
| 112 |
|
| 113 |
/**
|
| 114 |
+
* Multipart file part.
|
| 115 |
+
*
|
| 116 |
+
* **Gotchas**
|
| 117 |
+
*
|
| 118 |
+
* The file content is exposed as a byte stream. `contentEffect` collects the full
|
| 119 |
+
* file into memory and should be used only when the file size is acceptable.
|
| 120 |
+
*
|
| 121 |
* @category models
|
| 122 |
+
* @since 4.0.0
|
| 123 |
*/
|
| 124 |
export interface File extends Part.Proto {
|
| 125 |
readonly _tag: "File"
|
|
|
|
| 131 |
}
|
| 132 |
|
| 133 |
/**
|
| 134 |
+
* Returns `true` when a value is a multipart `File`.
|
| 135 |
+
*
|
| 136 |
+
* @category guards
|
| 137 |
* @since 4.0.0
|
|
|
|
| 138 |
*/
|
| 139 |
export const isFile = (u: unknown): u is File => isPart(u) && u._tag === "File"
|
| 140 |
|
| 141 |
/**
|
| 142 |
+
* Multipart file part that has been written to the filesystem.
|
| 143 |
+
*
|
| 144 |
+
* **Details**
|
| 145 |
+
*
|
| 146 |
+
* The `path` points to the persisted file while the scope used to persist the
|
| 147 |
+
* multipart data remains open.
|
| 148 |
+
*
|
| 149 |
* @category models
|
| 150 |
+
* @since 4.0.0
|
| 151 |
*/
|
| 152 |
export interface PersistedFile extends Part.Proto {
|
| 153 |
readonly _tag: "PersistedFile"
|
|
|
|
| 158 |
}
|
| 159 |
|
| 160 |
/**
|
| 161 |
+
* Returns `true` when a value is a persisted multipart file.
|
| 162 |
+
*
|
| 163 |
+
* @category guards
|
| 164 |
* @since 4.0.0
|
|
|
|
| 165 |
*/
|
| 166 |
export const isPersistedFile = (u: unknown): u is PersistedFile =>
|
| 167 |
Predicate.hasProperty(u, TypeId) && Predicate.isTagged(u, "PersistedFile")
|
| 168 |
|
| 169 |
/**
|
| 170 |
+
* Record representation of persisted multipart data.
|
| 171 |
+
*
|
| 172 |
+
* **Details**
|
| 173 |
+
*
|
| 174 |
+
* Field names map to text values, arrays of text values, or arrays of
|
| 175 |
+
* `PersistedFile` values.
|
| 176 |
+
*
|
| 177 |
* @category models
|
| 178 |
+
* @since 4.0.0
|
| 179 |
*/
|
| 180 |
export interface Persisted {
|
| 181 |
readonly [key: string]: ReadonlyArray<PersistedFile> | ReadonlyArray<string> | string
|
|
|
|
| 184 |
const MultipartErrorTypeId = "~effect/http/Multipart/MultipartError"
|
| 185 |
|
| 186 |
/**
|
| 187 |
+
* Error reason carried by a `MultipartError`.
|
| 188 |
+
*
|
| 189 |
+
* **Details**
|
| 190 |
+
*
|
| 191 |
+
* It identifies parser and limit failures such as oversized files or fields, too
|
| 192 |
+
* many parts, total body size limits, parse errors, and internal errors.
|
| 193 |
+
*
|
| 194 |
+
* @category errors
|
| 195 |
* @since 4.0.0
|
|
|
|
| 196 |
*/
|
| 197 |
export class MultipartErrorReason extends Data.Error<{
|
| 198 |
readonly _tag: "FileTooLarge" | "FieldTooLarge" | "BodyTooLarge" | "TooManyParts" | "InternalError" | "Parse"
|
|
|
|
| 200 |
}> {}
|
| 201 |
|
| 202 |
/**
|
| 203 |
+
* Error raised while parsing, streaming, or persisting multipart form data.
|
| 204 |
+
*
|
| 205 |
+
* **Details**
|
| 206 |
+
*
|
| 207 |
+
* The `reason` field contains the concrete `MultipartErrorReason`.
|
| 208 |
+
*
|
| 209 |
+
* @category errors
|
| 210 |
* @since 4.0.0
|
|
|
|
| 211 |
*/
|
| 212 |
export class MultipartError extends Data.TaggedError("MultipartError")<{
|
| 213 |
readonly reason: MultipartErrorReason
|
| 214 |
}> {
|
| 215 |
/**
|
| 216 |
+
* Creates a multipart error from a reason tag and optional cause.
|
| 217 |
+
*
|
| 218 |
* @since 4.0.0
|
| 219 |
*/
|
| 220 |
static fromReason(reason: MultipartErrorReason["_tag"], cause?: unknown): MultipartError {
|
|
|
|
| 222 |
}
|
| 223 |
|
| 224 |
/**
|
| 225 |
+
* Marks this value as a multipart error for runtime guards.
|
| 226 |
+
*
|
| 227 |
* @since 4.0.0
|
| 228 |
*/
|
| 229 |
readonly [MultipartErrorTypeId] = MultipartErrorTypeId
|
| 230 |
|
| 231 |
/**
|
| 232 |
+
* Uses the concrete multipart error reason as the public message.
|
| 233 |
+
*
|
| 234 |
* @since 4.0.0
|
| 235 |
*/
|
| 236 |
override get message(): string {
|
|
|
|
| 239 |
}
|
| 240 |
|
| 241 |
/**
|
| 242 |
+
* Schema type for persisted multipart files.
|
| 243 |
+
*
|
| 244 |
+
* @category schemas
|
| 245 |
* @since 4.0.0
|
|
|
|
| 246 |
*/
|
| 247 |
export interface PersistedFileSchema extends Schema.declare<PersistedFile> {}
|
| 248 |
|
| 249 |
/**
|
| 250 |
+
* Schema for persisted multipart files.
|
| 251 |
+
*
|
| 252 |
+
* **Details**
|
| 253 |
+
*
|
| 254 |
+
* The encoded form contains the field key, original file name, content type, and
|
| 255 |
+
* filesystem path.
|
| 256 |
+
*
|
| 257 |
+
* @category schemas
|
| 258 |
* @since 4.0.0
|
|
|
|
| 259 |
*/
|
| 260 |
export const PersistedFileSchema: PersistedFileSchema = Schema.declare(
|
| 261 |
isPersistedFile,
|
|
|
|
| 277 |
contentType: Schema.String.annotate({ contentEncoding: "binary" }),
|
| 278 |
path: Schema.String
|
| 279 |
}),
|
| 280 |
+
SchemaTransformation.transform({
|
| 281 |
decode: ({ contentType, key, name, path }) => new PersistedFileImpl(key, name, contentType, path),
|
| 282 |
encode: (file) => ({
|
| 283 |
key: file.key,
|
|
|
|
| 291 |
)
|
| 292 |
|
| 293 |
/**
|
| 294 |
+
* Schema for an array of persisted multipart files.
|
| 295 |
+
*
|
| 296 |
+
* @category schemas
|
| 297 |
* @since 4.0.0
|
|
|
|
| 298 |
*/
|
| 299 |
export const FilesSchema: Schema.$Array<PersistedFileSchema> = Schema.Array(PersistedFileSchema)
|
| 300 |
|
| 301 |
/**
|
| 302 |
+
* Schema for exactly one persisted multipart file.
|
| 303 |
+
*
|
| 304 |
+
* **Details**
|
| 305 |
+
*
|
| 306 |
+
* The encoded form is a one-element file array, while the decoded value is the
|
| 307 |
+
* single `PersistedFile`.
|
| 308 |
+
*
|
| 309 |
+
* @category schemas
|
| 310 |
* @since 4.0.0
|
|
|
|
| 311 |
*/
|
| 312 |
export const SingleFileSchema: Schema.decodeTo<PersistedFileSchema, Schema.$Array<PersistedFileSchema>> = FilesSchema
|
| 313 |
.check(
|
|
|
|
| 315 |
).pipe(
|
| 316 |
Schema.decodeTo(
|
| 317 |
PersistedFileSchema,
|
| 318 |
+
SchemaTransformation.transform({
|
| 319 |
decode: ([file]) => file,
|
| 320 |
encode: (file) => [file]
|
| 321 |
})
|
|
|
|
| 323 |
)
|
| 324 |
|
| 325 |
/**
|
| 326 |
+
* Creates a decoder for persisted multipart data using the supplied schema.
|
| 327 |
+
*
|
| 328 |
+
* **Details**
|
| 329 |
+
*
|
| 330 |
+
* The returned function decodes an unknown input into the schema output and fails
|
| 331 |
+
* with `SchemaError` when validation fails.
|
| 332 |
+
*
|
| 333 |
+
* @category schemas
|
| 334 |
* @since 4.0.0
|
|
|
|
| 335 |
*/
|
| 336 |
export const schemaPersisted = <A, I extends Partial<Persisted>, RD, RE>(
|
| 337 |
schema: Schema.Codec<A, I, RD, RE>
|
|
|
|
| 339 |
Schema.decodeUnknownEffect(schema)
|
| 340 |
|
| 341 |
/**
|
| 342 |
+
* Creates a decoder for a JSON-encoded field in persisted multipart data.
|
| 343 |
+
*
|
| 344 |
+
* **Details**
|
| 345 |
+
*
|
| 346 |
+
* The selected field is parsed from a JSON string and decoded with the supplied
|
| 347 |
+
* schema.
|
| 348 |
+
*
|
| 349 |
+
* @category schemas
|
| 350 |
* @since 4.0.0
|
|
|
|
| 351 |
*/
|
| 352 |
export const schemaJson = <A, I, RD, RE>(schema: Schema.Codec<A, I, RD, RE>, options?: ParseOptions | undefined): {
|
| 353 |
(
|
|
|
|
| 367 |
}
|
| 368 |
|
| 369 |
/**
|
| 370 |
+
* Builds the low-level multipart parser configuration from request headers and
|
| 371 |
+
* the current fiber context.
|
| 372 |
+
*
|
| 373 |
+
* **Details**
|
| 374 |
+
*
|
| 375 |
+
* Parser limits are read from the multipart references, including maximum parts,
|
| 376 |
+
* field size, file size, total body size, and field MIME type overrides.
|
| 377 |
+
*
|
| 378 |
+
* @category configuration
|
| 379 |
* @since 4.0.0
|
|
|
|
| 380 |
*/
|
| 381 |
export const makeConfig = (
|
| 382 |
headers: Record<string, string>
|
|
|
|
| 397 |
})
|
| 398 |
|
| 399 |
/**
|
| 400 |
+
* Creates a channel that parses multipart byte chunks into multipart parts.
|
| 401 |
+
*
|
| 402 |
+
* **Details**
|
| 403 |
+
*
|
| 404 |
+
* The channel consumes non-empty batches of `Uint8Array` chunks and emits
|
| 405 |
+
* non-empty batches of parsed `Part` values, failing with `MultipartError` for
|
| 406 |
+
* parser and limit failures.
|
| 407 |
+
*
|
| 408 |
* @category Parsers
|
| 409 |
+
* @since 4.0.0
|
| 410 |
*/
|
| 411 |
export const makeChannel = <IE>(headers: Record<string, string>): Channel.Channel<
|
| 412 |
Arr.NonEmptyReadonlyArray<Part>,
|
|
|
|
| 585 |
|
| 586 |
const defaultWriteFile = (path: string, file: File) =>
|
| 587 |
Effect.flatMap(
|
| 588 |
+
FileSystem.FileSystem,
|
| 589 |
(fs) =>
|
| 590 |
Effect.mapError(
|
| 591 |
Stream.run(file.content, fs.sink(path)),
|
|
|
|
| 594 |
)
|
| 595 |
|
| 596 |
/**
|
| 597 |
+
* Runs a channel of byte chunks and collects all output into a single
|
| 598 |
+
* `Uint8Array`.
|
| 599 |
+
*
|
| 600 |
+
* **Gotchas**
|
| 601 |
+
*
|
| 602 |
+
* This materializes the full content in memory.
|
| 603 |
+
*
|
| 604 |
+
* @category converting
|
| 605 |
* @since 4.0.0
|
| 606 |
*/
|
| 607 |
export const collectUint8Array = <OE, OD, R>(
|
|
|
|
| 620 |
})
|
| 621 |
|
| 622 |
/**
|
| 623 |
+
* Persists a stream of multipart parts into a record.
|
| 624 |
+
*
|
| 625 |
+
* **Details**
|
| 626 |
+
*
|
| 627 |
+
* Text fields are collected as strings, and file parts are written to files in a
|
| 628 |
+
* scoped temporary directory.
|
| 629 |
+
*
|
| 630 |
+
* **Gotchas**
|
| 631 |
+
*
|
| 632 |
+
* Persisted file paths remain valid for the lifetime of the scope.
|
| 633 |
+
*
|
| 634 |
+
* @category converting
|
| 635 |
* @since 4.0.0
|
|
|
|
| 636 |
*/
|
| 637 |
export const toPersisted = (
|
| 638 |
stream: Stream.Stream<Part, MultipartError>,
|
|
|
|
| 709 |
}
|
| 710 |
|
| 711 |
/**
|
| 712 |
+
* Creates a context containing multipart parser limit settings.
|
| 713 |
+
*
|
| 714 |
+
* **Details**
|
| 715 |
+
*
|
| 716 |
+
* The context can provide maximum part count, field size, file size, total body
|
| 717 |
+
* size, and MIME types that should be parsed as fields.
|
| 718 |
+
*
|
| 719 |
+
* @category references
|
| 720 |
* @since 4.0.0
|
|
|
|
| 721 |
*/
|
| 722 |
export const limitsServices = (options: {
|
| 723 |
readonly maxParts?: number | undefined
|
|
|
|
| 746 |
}
|
| 747 |
|
| 748 |
/**
|
| 749 |
+
* Namespace containing multipart parser limit option types.
|
| 750 |
+
*
|
| 751 |
* @since 4.0.0
|
|
|
|
| 752 |
*/
|
| 753 |
export declare namespace withLimits {
|
| 754 |
/**
|
| 755 |
+
* Options for overriding multipart parser limits.
|
| 756 |
+
*
|
| 757 |
+
* **Details**
|
| 758 |
+
*
|
| 759 |
+
* These settings control maximum part count, field size, file size, total body
|
| 760 |
+
* size, and MIME types that should be treated as fields instead of files.
|
| 761 |
+
*
|
| 762 |
* @category fiber refs
|
| 763 |
+
* @since 4.0.0
|
| 764 |
*/
|
| 765 |
export type Options = {
|
| 766 |
readonly maxParts?: number | undefined
|
|
|
|
| 772 |
}
|
| 773 |
|
| 774 |
/**
|
| 775 |
+
* Context reference for the maximum number of multipart parts allowed.
|
| 776 |
+
*
|
| 777 |
+
* **Details**
|
| 778 |
+
*
|
| 779 |
+
* The default is `undefined`, meaning no explicit part-count limit.
|
| 780 |
+
*
|
| 781 |
+
* @category references
|
| 782 |
* @since 4.0.0
|
|
|
|
| 783 |
*/
|
| 784 |
export const MaxParts = Context.Reference<number | undefined>("effect/http/Multipart/MaxParts", {
|
| 785 |
defaultValue: () => undefined
|
| 786 |
})
|
| 787 |
|
| 788 |
/**
|
| 789 |
+
* Context reference for the maximum size of a multipart field value.
|
| 790 |
+
*
|
| 791 |
+
* **Details**
|
| 792 |
+
*
|
| 793 |
+
* The default limit is 10 MiB.
|
| 794 |
+
*
|
| 795 |
+
* @category references
|
| 796 |
* @since 4.0.0
|
|
|
|
| 797 |
*/
|
| 798 |
export const MaxFieldSize = Context.Reference<FileSystem.SizeInput>("effect/http/Multipart/MaxFieldSize", {
|
| 799 |
defaultValue: constant(FileSystem.Size(10 * 1024 * 1024))
|
| 800 |
})
|
| 801 |
|
| 802 |
/**
|
| 803 |
+
* Context reference for the maximum size of a multipart file part.
|
| 804 |
+
*
|
| 805 |
+
* **Details**
|
| 806 |
+
*
|
| 807 |
+
* The default is `undefined`, meaning no explicit per-file limit.
|
| 808 |
+
*
|
| 809 |
+
* @category references
|
| 810 |
* @since 4.0.0
|
|
|
|
| 811 |
*/
|
| 812 |
export const MaxFileSize = Context.Reference<FileSystem.SizeInput | undefined>(
|
| 813 |
"effect/http/Multipart/MaxFileSize",
|
|
|
|
| 815 |
)
|
| 816 |
|
| 817 |
/**
|
| 818 |
+
* Context reference for MIME type fragments that should be parsed as multipart
|
| 819 |
+
* fields instead of files.
|
| 820 |
+
*
|
| 821 |
+
* **Details**
|
| 822 |
+
*
|
| 823 |
+
* The default treats `application/json` parts as fields.
|
| 824 |
+
*
|
| 825 |
+
* @category references
|
| 826 |
* @since 4.0.0
|
|
|
|
| 827 |
*/
|
| 828 |
export const FieldMimeTypes = Context.Reference<ReadonlyArray<string>>("effect/http/Multipart/FieldMimeTypes", {
|
| 829 |
defaultValue: constant(["application/json"])
|
.opencode/node_modules/effect/src/unstable/http/Multipasta.ts
CHANGED
|
@@ -1,9 +1,15 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
|
| 5 |
/**
|
| 6 |
-
* @since 4.0.0
|
| 7 |
* @category re-exports
|
|
|
|
| 8 |
*/
|
| 9 |
export * from "multipasta"
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Re-exports the `multipasta` multipart parser used by the unstable HTTP
|
| 3 |
+
* modules.
|
| 4 |
+
*
|
| 5 |
+
* This module keeps the parser types and helpers available from the Effect HTTP
|
| 6 |
+
* namespace without wrapping or changing them.
|
| 7 |
+
*
|
| 8 |
* @since 4.0.0
|
| 9 |
*/
|
| 10 |
|
| 11 |
/**
|
|
|
|
| 12 |
* @category re-exports
|
| 13 |
+
* @since 4.0.0
|
| 14 |
*/
|
| 15 |
export * from "multipasta"
|
.opencode/node_modules/effect/src/unstable/http/Multipasta/HeadersParser.ts
CHANGED
|
@@ -1,9 +1,14 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
|
| 5 |
/**
|
| 6 |
-
* @since 4.0.0
|
| 7 |
* @category re-exports
|
|
|
|
| 8 |
*/
|
| 9 |
export * from "multipasta/HeadersParser"
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Re-exports the `multipasta` header parser used by multipart HTTP parsing.
|
| 3 |
+
*
|
| 4 |
+
* This module keeps the parser available from the Effect HTTP namespace without
|
| 5 |
+
* wrapping or changing it.
|
| 6 |
+
*
|
| 7 |
* @since 4.0.0
|
| 8 |
*/
|
| 9 |
|
| 10 |
/**
|
|
|
|
| 11 |
* @category re-exports
|
| 12 |
+
* @since 4.0.0
|
| 13 |
*/
|
| 14 |
export * from "multipasta/HeadersParser"
|
.opencode/node_modules/effect/src/unstable/http/Multipasta/Node.ts
CHANGED
|
@@ -1,9 +1,14 @@
|
|
| 1 |
/**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
* @since 4.0.0
|
| 3 |
*/
|
| 4 |
|
| 5 |
/**
|
| 6 |
-
* @since 4.0.0
|
| 7 |
* @category re-exports
|
|
|
|
| 8 |
*/
|
| 9 |
export * from "multipasta/node"
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Re-exports the Node.js multipart helpers from `multipasta`.
|
| 3 |
+
*
|
| 4 |
+
* This module keeps the Node-specific parser helpers available from the Effect
|
| 5 |
+
* HTTP namespace without wrapping or changing them.
|
| 6 |
+
*
|
| 7 |
* @since 4.0.0
|
| 8 |
*/
|
| 9 |
|
| 10 |
/**
|
|
|
|
| 11 |
* @category re-exports
|
| 12 |
+
* @since 4.0.0
|
| 13 |
*/
|
| 14 |
export * from "multipasta/node"
|