@effect/platform 0.88.1 → 0.88.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/HttpApiBuilder.js +1 -1
- package/dist/cjs/HttpApiBuilder.js.map +1 -1
- package/dist/cjs/HttpRouter.js +6 -1
- package/dist/cjs/HttpRouter.js.map +1 -1
- package/dist/cjs/internal/httpRouter.js +6 -4
- package/dist/cjs/internal/httpRouter.js.map +1 -1
- package/dist/cjs/internal/path.js +0 -2
- package/dist/cjs/internal/path.js.map +1 -1
- package/dist/dts/HttpRouter.d.ts +5 -0
- package/dist/dts/HttpRouter.d.ts.map +1 -1
- package/dist/dts/internal/httpRouter.d.ts.map +1 -1
- package/dist/esm/HttpApiBuilder.js +1 -1
- package/dist/esm/HttpApiBuilder.js.map +1 -1
- package/dist/esm/HttpRouter.js +5 -0
- package/dist/esm/HttpRouter.js.map +1 -1
- package/dist/esm/internal/httpRouter.js +4 -3
- package/dist/esm/internal/httpRouter.js.map +1 -1
- package/dist/esm/internal/path.js +0 -2
- package/dist/esm/internal/path.js.map +1 -1
- package/package.json +3 -3
- package/src/HttpApiBuilder.ts +1 -1
- package/src/HttpRouter.ts +7 -0
- package/src/internal/httpRouter.ts +84 -83
- package/src/internal/path.ts +2 -2
|
@@ -179,8 +179,8 @@ class RouterImpl<E = never, R = never> extends Effectable.StructuralClass<
|
|
|
179
179
|
) {
|
|
180
180
|
super()
|
|
181
181
|
this[TypeId] = TypeId
|
|
182
|
-
this.httpApp =
|
|
183
|
-
Effect.flatMap((
|
|
182
|
+
this.httpApp = toHttpApp(this).pipe(
|
|
183
|
+
Effect.flatMap((app) => this.httpApp = app as any)
|
|
184
184
|
) as any
|
|
185
185
|
}
|
|
186
186
|
private httpApp: Effect.Effect<
|
|
@@ -206,95 +206,96 @@ class RouterImpl<E = never, R = never> extends Effectable.StructuralClass<
|
|
|
206
206
|
}
|
|
207
207
|
}
|
|
208
208
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
): App.Default<E | Error.RouteNotFound, R
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
[
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
new
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
209
|
+
/** @internal */
|
|
210
|
+
export const toHttpApp = <E, R>(
|
|
211
|
+
self: Router.HttpRouter<E, R>
|
|
212
|
+
): Effect.Effect<App.Default<E | Error.RouteNotFound, R>> =>
|
|
213
|
+
Effect.map(FiberRef.get(currentRouterConfig), (config) => {
|
|
214
|
+
const router = FindMyWay.make<Router.Route<E, R>>(config)
|
|
215
|
+
const mounts = Chunk.toReadonlyArray(self.mounts).map(([path, app, options]) =>
|
|
216
|
+
[
|
|
217
|
+
path,
|
|
218
|
+
new RouteContextImpl(
|
|
219
|
+
new RouteImpl(
|
|
220
|
+
"*",
|
|
221
|
+
options?.includePrefix ? `${path}/*` as Router.PathInput : "/*",
|
|
222
|
+
app,
|
|
223
|
+
options?.includePrefix ? Option.none() : Option.some(path),
|
|
224
|
+
false
|
|
225
|
+
),
|
|
226
|
+
{}
|
|
224
227
|
),
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
228
|
+
options
|
|
229
|
+
] as const
|
|
230
|
+
)
|
|
231
|
+
const mountsLen = mounts.length
|
|
232
|
+
Chunk.forEach(self.routes, (route) => {
|
|
233
|
+
if (route.method === "*") {
|
|
234
|
+
router.all(route.path, route)
|
|
235
|
+
} else {
|
|
236
|
+
router.on(route.method, route.path, route)
|
|
237
|
+
}
|
|
238
|
+
})
|
|
239
|
+
return Effect.withFiberRuntime<
|
|
240
|
+
ServerResponse.HttpServerResponse,
|
|
241
|
+
E | Error.RouteNotFound,
|
|
242
|
+
R | ServerRequest.HttpServerRequest
|
|
243
|
+
>((fiber) => {
|
|
244
|
+
const context = Context.unsafeMake(new Map(fiber.getFiberRef(FiberRef.currentContext).unsafeMap))
|
|
245
|
+
const request = Context.unsafeGet(context, ServerRequest.HttpServerRequest)
|
|
246
|
+
if (mountsLen > 0) {
|
|
247
|
+
const searchIndex = request.url.indexOf("?")
|
|
248
|
+
const pathname = searchIndex === -1 ? request.url : request.url.slice(0, searchIndex)
|
|
249
|
+
|
|
250
|
+
for (let i = 0; i < mountsLen; i++) {
|
|
251
|
+
const [path, routeContext, options] = mounts[i]
|
|
252
|
+
if (pathname === path || pathname.startsWith(path + "/")) {
|
|
253
|
+
context.unsafeMap.set(RouteContext.key, routeContext)
|
|
254
|
+
if (options?.includePrefix !== true) {
|
|
255
|
+
context.unsafeMap.set(ServerRequest.HttpServerRequest.key, sliceRequestUrl(request, path))
|
|
256
|
+
}
|
|
257
|
+
return Effect.locally(
|
|
258
|
+
Effect.flatMap(routeContext.route.handler, Respondable.toResponse) as App.Default<E, R>,
|
|
259
|
+
FiberRef.currentContext,
|
|
260
|
+
context
|
|
261
|
+
)
|
|
255
262
|
}
|
|
256
|
-
return Effect.locally(
|
|
257
|
-
Effect.flatMap(routeContext.route.handler, Respondable.toResponse) as App.Default<E, R>,
|
|
258
|
-
FiberRef.currentContext,
|
|
259
|
-
context
|
|
260
|
-
)
|
|
261
263
|
}
|
|
262
264
|
}
|
|
263
|
-
}
|
|
264
265
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
266
|
+
let result = router.find(request.method, request.url)
|
|
267
|
+
if (result === undefined && request.method === "HEAD") {
|
|
268
|
+
result = router.find("GET", request.url)
|
|
269
|
+
}
|
|
270
|
+
if (result === undefined) {
|
|
271
|
+
return Effect.fail(new Error.RouteNotFound({ request }))
|
|
272
|
+
}
|
|
273
|
+
const route = result.handler
|
|
274
|
+
if (route.prefix._tag === "Some") {
|
|
275
|
+
context.unsafeMap.set(ServerRequest.HttpServerRequest.key, sliceRequestUrl(request, route.prefix.value))
|
|
276
|
+
}
|
|
277
|
+
context.unsafeMap.set(ServerRequest.ParsedSearchParams.key, result.searchParams)
|
|
278
|
+
context.unsafeMap.set(RouteContext.key, new RouteContextImpl(route, result.params))
|
|
278
279
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
280
|
+
const span = Context.getOption(context, Tracer.ParentSpan)
|
|
281
|
+
if (span._tag === "Some" && span.value._tag === "Span") {
|
|
282
|
+
span.value.attribute("http.route", route.path)
|
|
283
|
+
}
|
|
283
284
|
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
285
|
+
const handlerResponse = Effect.flatMap(route.handler, Respondable.toResponse)
|
|
286
|
+
return Effect.locally(
|
|
287
|
+
(route.uninterruptible ?
|
|
288
|
+
handlerResponse :
|
|
289
|
+
Effect.interruptible(handlerResponse)) as Effect.Effect<
|
|
290
|
+
ServerResponse.HttpServerResponse,
|
|
291
|
+
E,
|
|
292
|
+
Router.HttpRouter.ExcludeProvided<R>
|
|
293
|
+
>,
|
|
294
|
+
FiberRef.currentContext,
|
|
295
|
+
context
|
|
296
|
+
)
|
|
297
|
+
})
|
|
296
298
|
})
|
|
297
|
-
}
|
|
298
299
|
|
|
299
300
|
function sliceRequestUrl(request: ServerRequest.HttpServerRequest, prefix: string) {
|
|
300
301
|
const prefexLen = prefix.length
|
package/src/internal/path.ts
CHANGED
|
@@ -83,7 +83,7 @@ function normalizeStringPosix(path: string, allowAboveRoot: boolean) {
|
|
|
83
83
|
lastSlash = i
|
|
84
84
|
dots = 0
|
|
85
85
|
} else if (code === 46 /*.*/ && dots !== -1) {
|
|
86
|
-
|
|
86
|
+
++dots
|
|
87
87
|
} else {
|
|
88
88
|
dots = -1
|
|
89
89
|
}
|
|
@@ -361,7 +361,7 @@ const posixImpl = Path.of({
|
|
|
361
361
|
} else {
|
|
362
362
|
toStart += lastCommonSep
|
|
363
363
|
if (to.charCodeAt(toStart) === 47 /*/*/) {
|
|
364
|
-
|
|
364
|
+
++toStart
|
|
365
365
|
}
|
|
366
366
|
return to.slice(toStart)
|
|
367
367
|
}
|