@shivaedev/http 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ShivaeDev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,63 @@
1
- # Temporary Holding Version
1
+ # @shivaedev/http
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Bounded bodies, typed status failures and paginated requests for Effect's HTTP client and server. Calls to outside APIs and native routes stay on `effect/http`, without unbounded reads or untyped status checks.
4
+
5
+ ## Why you want this
6
+
7
+ A remote API that answers with a 2 GB body, a 429 or a `Link` header to page 2 is ordinary, and every integration ends up writing the same size checks, status mapping and pagination loop. This package writes them once: responses become typed failures you can match on, and no body is read past the limit you set.
8
+
9
+ ```ts
10
+ import { Effect, Schema, Stream } from "effect";
11
+ import { HttpClient, HttpClientRequest } from "effect/http";
12
+ import { readBoundedJson } from "@shivaedev/http/body.ts";
13
+ import { filterStatus } from "@shivaedev/http/filterStatus.ts";
14
+ import { paginate } from "@shivaedev/http/paginate.ts";
15
+
16
+ const items = Effect.gen(function* () {
17
+ const client = filterStatus(yield* HttpClient.HttpClient);
18
+ return yield* paginate(client, HttpClientRequest.get("https://api.example/items")).pipe(
19
+ Stream.mapEffect((response) => readBoundedJson(response, 64 * 1024, Schema.Array(Schema.String))),
20
+ Stream.runCollect,
21
+ Effect.map((pages) => pages.flat()),
22
+ );
23
+ });
24
+ ```
25
+
26
+ `items` collects every page, fails with `RemoteRateLimited` when the API throttles, and never reads more than 64 KiB per page.
27
+
28
+ ## Using it
29
+
30
+ ### How to think about it
31
+
32
+ Everything here wraps a native `HttpClient`, `HttpClientResponse` or `HttpServerRequest`; nothing replaces them. Provide a native client Layer such as `FetchHttpClient.layer` from `effect/http`, compose the helpers onto it, and handle the typed failures they add to the error channel.
33
+
34
+ ### Calling outside APIs
35
+
36
+ `filterStatus(client)` passes 2xx responses through and fails other statuses with Schema tagged errors from `errors.ts`: `RemoteUnauthorized` (401), `RemoteNotFound` (404), `RemoteGone` (410), `RemoteRateLimited` (429, or 503 carrying `Retry-After`) and `RemoteHttpError` for other statuses. Errors carry the method, the URL without its query string and the status, without headers or body. `RemoteRateLimited.retryAfter` is an optional UTC `DateTime`: delay-seconds use Effect's clock, and HTTP dates name the instant directly. Missing or invalid values leave it undefined; `parseRetryAfter(header, now)` exposes the same parsing.
37
+
38
+ `paginate(client, request)` follows quoted or unquoted `rel=next`, including headers with multiple links and relations, resolves relative links and stops at the last page. Taking one page sends one request. A next link on another origin fails with `PaginationLinkError` before that request is sent. `nextLink(header)` exposes the link parsing.
39
+
40
+ `readBoundedBody(response, limit)` returns bytes; `readBoundedText` decodes UTF-8, and `readBoundedJson(response, limit, schema)` decodes JSON through the supplied Schema. Limits are bytes. An oversized `Content-Length` fails with `ResponseBodyTooLarge` before reading the body; a chunked body that exceeds the limit fails during the read. The error carries `limit`.
41
+
42
+ ### Reading request bodies on a native route
43
+
44
+ For a native Node HTTP route, use `readBoundedBody` from `server/boundedBody.ts` with Effect's `HttpIncomingMessage.MaxBodySize`. It returns an `ArrayBuffer` without decoding the bytes:
45
+
46
+ ```ts
47
+ import { ByteSize, Effect } from "effect";
48
+ import { HttpIncomingMessage, HttpRouter, HttpServerResponse } from "effect/http";
49
+ import { readBoundedBody } from "@shivaedev/http/server/boundedBody.ts";
50
+
51
+ const routes = HttpRouter.add("POST", "/webhook", (request) =>
52
+ readBoundedBody(request).pipe(
53
+ Effect.map((body) => HttpServerResponse.uint8Array(new Uint8Array(body))),
54
+ Effect.provideService(HttpIncomingMessage.MaxBodySize, ByteSize.bytes(64 * 1024)),
55
+ ),
56
+ );
57
+ ```
58
+
59
+ Oversized `content-length` headers are rejected before reading; chunked overflow drains the remaining body and preserves the connection for another request. `RequestBodyTooLarge` is a typed failure that implements Effect's `HttpServerRespondable` protocol with status 413; other read errors remain native `HttpServerError` failures. Consume the body once through this reader and decode the result in the application.
60
+
61
+ ### Install and limits
62
+
63
+ Install `@shivaedev/http` with `effect` as its peer. `server/boundedBody.ts` reads Node requests and needs the optional peer `@effect/platform-node`; the client modules work without it. The package has no root entry; import the module that defines the name.
package/dist/body.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { Effect, Schema } from "effect";
2
+ import type { HttpClientResponse } from "effect/http";
3
+ import { ResponseBodyTooLarge } from "./errors.ts";
4
+ export declare const readBoundedBody: (response: HttpClientResponse.HttpClientResponse, limit: number) => Effect.Effect<Uint8Array<ArrayBuffer>, import("effect/http/HttpClientError").HttpClientError | ResponseBodyTooLarge, never>;
5
+ export declare function readBoundedText(response: HttpClientResponse.HttpClientResponse, limit: number): Effect.Effect<string, import("effect/http/HttpClientError").HttpClientError | ResponseBodyTooLarge, never>;
6
+ export declare function readBoundedJson<TSchema extends Schema.Constraint>(response: HttpClientResponse.HttpClientResponse, limit: number, schema: TSchema): Effect.Effect<TSchema["Type"], import("effect/http/HttpClientError").HttpClientError | ResponseBodyTooLarge | Schema.SchemaError, TSchema["DecodingServices"]>;
7
+ //# sourceMappingURL=body.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"body.d.ts","sourceRoot":"","sources":["../src/body.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAU,MAAM,QAAQ,CAAC;AAChD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAEnD,eAAO,MAAM,eAAe,iMAwB1B,CAAC;AAEH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,kBAAkB,CAAC,kBAAkB,EAAE,KAAK,EAAE,MAAM,8GAE7F;AAED,wBAAgB,eAAe,CAAC,OAAO,SAAS,MAAM,CAAC,UAAU,EAAE,QAAQ,EAAE,kBAAkB,CAAC,kBAAkB,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,kKAEjJ"}
package/dist/body.js ADDED
@@ -0,0 +1,32 @@
1
+ import { Effect, Schema, Stream } from "effect";
2
+ import { ResponseBodyTooLarge } from "./errors.js";
3
+ export const readBoundedBody = Effect.fnUntraced(function* (response, limit) {
4
+ const length = response.headers["content-length"];
5
+ if (length !== undefined && Number(length) > limit) {
6
+ return yield* new ResponseBodyTooLarge({ limit });
7
+ }
8
+ const chunks = [];
9
+ let size = 0;
10
+ yield* response.stream.pipe(Stream.runForEach((chunk) => {
11
+ size += chunk.byteLength;
12
+ if (size > limit) {
13
+ return Effect.fail(new ResponseBodyTooLarge({ limit }));
14
+ }
15
+ chunks.push(chunk);
16
+ return Effect.void;
17
+ }));
18
+ const bytes = new Uint8Array(size);
19
+ let offset = 0;
20
+ for (const chunk of chunks) {
21
+ bytes.set(chunk, offset);
22
+ offset += chunk.byteLength;
23
+ }
24
+ return bytes;
25
+ });
26
+ export function readBoundedText(response, limit) {
27
+ return Effect.map(readBoundedBody(response, limit), (bytes) => new TextDecoder().decode(bytes));
28
+ }
29
+ export function readBoundedJson(response, limit, schema) {
30
+ return Effect.flatMap(readBoundedText(response, limit), Schema.decodeUnknownEffect(Schema.fromJsonString(schema)));
31
+ }
32
+ //# sourceMappingURL=body.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"body.js","sourceRoot":"","sources":["../src/body.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhD,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAEnD,MAAM,CAAC,MAAM,eAAe,GAAG,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,QAA+C,EAAE,KAAa;IACzH,MAAM,MAAM,GAAG,QAAQ,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAC;IAClD,IAAI,MAAM,KAAK,SAAS,IAAI,MAAM,CAAC,MAAM,CAAC,GAAG,KAAK,EAAE,CAAC;QACpD,OAAO,KAAK,CAAC,CAAC,IAAI,oBAAoB,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;IACnD,CAAC;IACD,MAAM,MAAM,GAAiB,EAAE,CAAC;IAChC,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,KAAK,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAC1B,MAAM,CAAC,UAAU,CAAC,CAAC,KAAK,EAAE,EAAE;QAC3B,IAAI,IAAI,KAAK,CAAC,UAAU,CAAC;QACzB,IAAI,IAAI,GAAG,KAAK,EAAE,CAAC;YAClB,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,oBAAoB,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC;QACzD,CAAC;QACD,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACnB,OAAO,MAAM,CAAC,IAAI,CAAC;IACpB,CAAC,CAAC,CACF,CAAC;IACF,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC;IACnC,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QAC5B,KAAK,CAAC,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;QACzB,MAAM,IAAI,KAAK,CAAC,UAAU,CAAC;IAC5B,CAAC;IACD,OAAO,KAAK,CAAC;AACd,CAAC,CAAC,CAAC;AAEH,MAAM,UAAU,eAAe,CAAC,QAA+C,EAAE,KAAa;IAC7F,OAAO,MAAM,CAAC,GAAG,CAAC,eAAe,CAAC,QAAQ,EAAE,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;AACjG,CAAC;AAED,MAAM,UAAU,eAAe,CAAoC,QAA+C,EAAE,KAAa,EAAE,MAAe;IACjJ,OAAO,MAAM,CAAC,OAAO,CAAC,eAAe,CAAC,QAAQ,EAAE,KAAK,CAAC,EAAE,MAAM,CAAC,mBAAmB,CAAC,MAAM,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;AACpH,CAAC"}
@@ -0,0 +1,51 @@
1
+ import { Schema } from "effect";
2
+ declare const RemoteUnauthorized_base: Schema.Class<RemoteUnauthorized, Schema.TaggedStruct<"RemoteUnauthorized", {
3
+ method: Schema.String;
4
+ status: Schema.Number;
5
+ url: Schema.String;
6
+ }>, import("effect/Cause").YieldableError>;
7
+ export declare class RemoteUnauthorized extends RemoteUnauthorized_base {
8
+ }
9
+ declare const RemoteNotFound_base: Schema.Class<RemoteNotFound, Schema.TaggedStruct<"RemoteNotFound", {
10
+ method: Schema.String;
11
+ status: Schema.Number;
12
+ url: Schema.String;
13
+ }>, import("effect/Cause").YieldableError>;
14
+ export declare class RemoteNotFound extends RemoteNotFound_base {
15
+ }
16
+ declare const RemoteGone_base: Schema.Class<RemoteGone, Schema.TaggedStruct<"RemoteGone", {
17
+ method: Schema.String;
18
+ status: Schema.Number;
19
+ url: Schema.String;
20
+ }>, import("effect/Cause").YieldableError>;
21
+ export declare class RemoteGone extends RemoteGone_base {
22
+ }
23
+ declare const RemoteRateLimited_base: Schema.Class<RemoteRateLimited, Schema.TaggedStruct<"RemoteRateLimited", {
24
+ readonly method: Schema.String;
25
+ readonly status: Schema.Number;
26
+ readonly url: Schema.String;
27
+ readonly retryAfter: Schema.optionalKey<Schema.DateTimeUtc>;
28
+ }>, import("effect/Cause").YieldableError>;
29
+ export declare class RemoteRateLimited extends RemoteRateLimited_base {
30
+ }
31
+ declare const RemoteHttpError_base: Schema.Class<RemoteHttpError, Schema.TaggedStruct<"RemoteHttpError", {
32
+ method: Schema.String;
33
+ status: Schema.Number;
34
+ url: Schema.String;
35
+ }>, import("effect/Cause").YieldableError>;
36
+ export declare class RemoteHttpError extends RemoteHttpError_base {
37
+ }
38
+ export type RemoteStatusError = RemoteUnauthorized | RemoteNotFound | RemoteGone | RemoteRateLimited | RemoteHttpError;
39
+ declare const PaginationLinkError_base: Schema.Class<PaginationLinkError, Schema.TaggedStruct<"PaginationLinkError", {
40
+ readonly reason: Schema.Literals<readonly ["cross-origin", "invalid"]>;
41
+ readonly url: Schema.String;
42
+ }>, import("effect/Cause").YieldableError>;
43
+ export declare class PaginationLinkError extends PaginationLinkError_base {
44
+ }
45
+ declare const ResponseBodyTooLarge_base: Schema.Class<ResponseBodyTooLarge, Schema.TaggedStruct<"ResponseBodyTooLarge", {
46
+ readonly limit: Schema.Number;
47
+ }>, import("effect/Cause").YieldableError>;
48
+ export declare class ResponseBodyTooLarge extends ResponseBodyTooLarge_base {
49
+ }
50
+ export {};
51
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;;;;;;AAIhC,qBAAa,kBAAmB,SAAQ,uBAAsE;CAAG;;;;;;AACjH,qBAAa,cAAe,SAAQ,mBAA8D;CAAG;;;;;;AACrG,qBAAa,UAAW,SAAQ,eAAsD;CAAG;;;;;;;AACzF,qBAAa,iBAAkB,SAAQ,sBAGrC;CAAG;;;;;;AACL,qBAAa,eAAgB,SAAQ,oBAAgE;CAAG;AAExG,MAAM,MAAM,iBAAiB,GAAG,kBAAkB,GAAG,cAAc,GAAG,UAAU,GAAG,iBAAiB,GAAG,eAAe,CAAC;;;;;AAEvH,qBAAa,mBAAoB,SAAQ,wBAGvC;CAAG;;;;AAEL,qBAAa,oBAAqB,SAAQ,yBAExC;CAAG"}
package/dist/errors.js ADDED
@@ -0,0 +1,25 @@
1
+ import { Schema } from "effect";
2
+ const answer = { method: Schema.String, status: Schema.Number, url: Schema.String };
3
+ export class RemoteUnauthorized extends Schema.TaggedError()("RemoteUnauthorized", answer) {
4
+ }
5
+ export class RemoteNotFound extends Schema.TaggedError()("RemoteNotFound", answer) {
6
+ }
7
+ export class RemoteGone extends Schema.TaggedError()("RemoteGone", answer) {
8
+ }
9
+ export class RemoteRateLimited extends Schema.TaggedError()("RemoteRateLimited", {
10
+ ...answer,
11
+ retryAfter: Schema.optionalKey(Schema.DateTimeUtc),
12
+ }) {
13
+ }
14
+ export class RemoteHttpError extends Schema.TaggedError()("RemoteHttpError", answer) {
15
+ }
16
+ export class PaginationLinkError extends Schema.TaggedError()("PaginationLinkError", {
17
+ reason: Schema.Literals(["cross-origin", "invalid"]),
18
+ url: Schema.String,
19
+ }) {
20
+ }
21
+ export class ResponseBodyTooLarge extends Schema.TaggedError()("ResponseBodyTooLarge", {
22
+ limit: Schema.Number,
23
+ }) {
24
+ }
25
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,MAAM,MAAM,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC;AAEpF,MAAM,OAAO,kBAAmB,SAAQ,MAAM,CAAC,WAAW,EAAsB,CAAC,oBAAoB,EAAE,MAAM,CAAC;CAAG;AACjH,MAAM,OAAO,cAAe,SAAQ,MAAM,CAAC,WAAW,EAAkB,CAAC,gBAAgB,EAAE,MAAM,CAAC;CAAG;AACrG,MAAM,OAAO,UAAW,SAAQ,MAAM,CAAC,WAAW,EAAc,CAAC,YAAY,EAAE,MAAM,CAAC;CAAG;AACzF,MAAM,OAAO,iBAAkB,SAAQ,MAAM,CAAC,WAAW,EAAqB,CAAC,mBAAmB,EAAE;IACnG,GAAG,MAAM;IACT,UAAU,EAAE,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,WAAW,CAAC;CAClD,CAAC;CAAG;AACL,MAAM,OAAO,eAAgB,SAAQ,MAAM,CAAC,WAAW,EAAmB,CAAC,iBAAiB,EAAE,MAAM,CAAC;CAAG;AAIxG,MAAM,OAAO,mBAAoB,SAAQ,MAAM,CAAC,WAAW,EAAuB,CAAC,qBAAqB,EAAE;IACzG,MAAM,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC,cAAc,EAAE,SAAS,CAAC,CAAC;IACpD,GAAG,EAAE,MAAM,CAAC,MAAM;CAClB,CAAC;CAAG;AAEL,MAAM,OAAO,oBAAqB,SAAQ,MAAM,CAAC,WAAW,EAAwB,CAAC,sBAAsB,EAAE;IAC5G,KAAK,EAAE,MAAM,CAAC,MAAM;CACpB,CAAC;CAAG"}
@@ -0,0 +1,4 @@
1
+ import { HttpClient } from "effect/http";
2
+ import { type RemoteStatusError } from "./errors.ts";
3
+ export declare function filterStatus<E, R>(client: HttpClient.HttpClient.With<E, R>): HttpClient.HttpClient.With<E | RemoteStatusError, R>;
4
+ //# sourceMappingURL=filterStatus.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"filterStatus.d.ts","sourceRoot":"","sources":["../src/filterStatus.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAA2B,MAAM,aAAa,CAAC;AAClE,OAAO,EAAkE,KAAK,iBAAiB,EAAsB,MAAM,aAAa,CAAC;AAiCzI,wBAAgB,YAAY,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,UAAU,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,UAAU,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,GAAG,iBAAiB,EAAE,CAAC,CAAC,CAEjI"}
@@ -0,0 +1,38 @@
1
+ import { DateTime, Effect } from "effect";
2
+ import { HttpClient } from "effect/http";
3
+ import { RemoteGone, RemoteHttpError, RemoteNotFound, RemoteRateLimited, RemoteUnauthorized } from "./errors.js";
4
+ import { parseRetryAfter } from "./parseRetryAfter.js";
5
+ const checkStatus = Effect.fnUntraced(function* (response) {
6
+ const { status, request } = response;
7
+ if (status >= 200 && status < 300) {
8
+ return response;
9
+ }
10
+ const fields = { method: request.method, status, url: request.url.split(/[?#]/u)[0] ?? request.url };
11
+ let error;
12
+ switch (status) {
13
+ case 401:
14
+ error = new RemoteUnauthorized(fields);
15
+ break;
16
+ case 404:
17
+ error = new RemoteNotFound(fields);
18
+ break;
19
+ case 410:
20
+ error = new RemoteGone(fields);
21
+ break;
22
+ default: {
23
+ const header = response.headers["retry-after"];
24
+ if (status === 429 || (status === 503 && header !== undefined)) {
25
+ const retryAfter = parseRetryAfter(header, yield* DateTime.now);
26
+ error = new RemoteRateLimited({ ...fields, ...(retryAfter === undefined ? {} : { retryAfter }) });
27
+ }
28
+ else {
29
+ error = new RemoteHttpError(fields);
30
+ }
31
+ }
32
+ }
33
+ return yield* Effect.fail(error);
34
+ });
35
+ export function filterStatus(client) {
36
+ return HttpClient.transformResponse(client, Effect.flatMap(checkStatus));
37
+ }
38
+ //# sourceMappingURL=filterStatus.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"filterStatus.js","sourceRoot":"","sources":["../src/filterStatus.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC1C,OAAO,EAAE,UAAU,EAA2B,MAAM,aAAa,CAAC;AAClE,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,cAAc,EAAE,iBAAiB,EAA0B,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACzI,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAEvD,MAAM,WAAW,GAAG,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,QAA+C;IAC/F,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,QAAQ,CAAC;IACrC,IAAI,MAAM,IAAI,GAAG,IAAI,MAAM,GAAG,GAAG,EAAE,CAAC;QACnC,OAAO,QAAQ,CAAC;IACjB,CAAC;IACD,MAAM,MAAM,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,OAAO,CAAC,GAAG,EAAE,CAAC;IACrG,IAAI,KAAwB,CAAC;IAC7B,QAAQ,MAAM,EAAE,CAAC;QAChB,KAAK,GAAG;YACP,KAAK,GAAG,IAAI,kBAAkB,CAAC,MAAM,CAAC,CAAC;YACvC,MAAM;QACP,KAAK,GAAG;YACP,KAAK,GAAG,IAAI,cAAc,CAAC,MAAM,CAAC,CAAC;YACnC,MAAM;QACP,KAAK,GAAG;YACP,KAAK,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;YAC/B,MAAM;QACP,SAAS,CAAC;YACT,MAAM,MAAM,GAAG,QAAQ,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC;YAC/C,IAAI,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,KAAK,GAAG,IAAI,MAAM,KAAK,SAAS,CAAC,EAAE,CAAC;gBAChE,MAAM,UAAU,GAAG,eAAe,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;gBAChE,KAAK,GAAG,IAAI,iBAAiB,CAAC,EAAE,GAAG,MAAM,EAAE,GAAG,CAAC,UAAU,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,CAAC,CAAC;YACnG,CAAC;iBAAM,CAAC;gBACP,KAAK,GAAG,IAAI,eAAe,CAAC,MAAM,CAAC,CAAC;YACrC,CAAC;QACF,CAAC;IACF,CAAC;IACD,OAAO,KAAK,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAClC,CAAC,CAAC,CAAC;AAEH,MAAM,UAAU,YAAY,CAAO,MAAwC;IAC1E,OAAO,UAAU,CAAC,iBAAiB,CAAC,MAAM,EAAE,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC;AAC1E,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare function nextLink(header: string | undefined): string | undefined;
2
+ //# sourceMappingURL=nextLink.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nextLink.d.ts","sourceRoot":"","sources":["../src/nextLink.ts"],"names":[],"mappings":"AAAA,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,CASvE"}
@@ -0,0 +1,11 @@
1
+ export function nextLink(header) {
2
+ for (const link of header?.matchAll(/<(?<url>[^>]*)>(?<parameters>(?:[^<",]|"(?:\\.|[^"\\])*")*)/gu) ?? []) {
3
+ const relation = link.groups?.parameters?.match(/(?:^|;)\s*rel\s*=\s*(?:"(?<quoted>[^"]*)"|(?<unquoted>[^;\s,]+))/iu);
4
+ const relations = relation?.groups?.quoted ?? relation?.groups?.unquoted;
5
+ if (relations?.toLowerCase().split(/\s+/u).includes("next")) {
6
+ return link.groups?.url;
7
+ }
8
+ }
9
+ return undefined;
10
+ }
11
+ //# sourceMappingURL=nextLink.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nextLink.js","sourceRoot":"","sources":["../src/nextLink.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,QAAQ,CAAC,MAA0B;IAClD,KAAK,MAAM,IAAI,IAAI,MAAM,EAAE,QAAQ,CAAC,+DAA+D,CAAC,IAAI,EAAE,EAAE,CAAC;QAC5G,MAAM,QAAQ,GAAG,IAAI,CAAC,MAAM,EAAE,UAAU,EAAE,KAAK,CAAC,oEAAoE,CAAC,CAAC;QACtH,MAAM,SAAS,GAAG,QAAQ,EAAE,MAAM,EAAE,MAAM,IAAI,QAAQ,EAAE,MAAM,EAAE,QAAQ,CAAC;QACzE,IAAI,SAAS,EAAE,WAAW,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC;YAC7D,OAAO,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC;QACzB,CAAC;IACF,CAAC;IACD,OAAO,SAAS,CAAC;AAClB,CAAC"}
@@ -0,0 +1,5 @@
1
+ import { Stream } from "effect";
2
+ import { type HttpClient, HttpClientRequest, type HttpClientResponse } from "effect/http";
3
+ import { PaginationLinkError } from "./errors.ts";
4
+ export declare function paginate<E, R>(client: HttpClient.HttpClient.With<E, R>, first: HttpClientRequest.HttpClientRequest): Stream.Stream<HttpClientResponse.HttpClientResponse, E | PaginationLinkError, R>;
5
+ //# sourceMappingURL=paginate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"paginate.d.ts","sourceRoot":"","sources":["../src/paginate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,MAAM,EAAE,MAAM,QAAQ,CAAC;AACxC,OAAO,EAAE,KAAK,UAAU,EAAE,iBAAiB,EAAE,KAAK,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAC1F,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAoBlD,wBAAgB,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,UAAU,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,iBAAiB,CAAC,iBAAiB,oFAgBlH"}
@@ -0,0 +1,31 @@
1
+ import { Effect, Stream } from "effect";
2
+ import { HttpClientRequest } from "effect/http";
3
+ import { PaginationLinkError } from "./errors.js";
4
+ import { nextLink } from "./nextLink.js";
5
+ const nextRequest = Effect.fnUntraced(function* (response) {
6
+ const link = nextLink(response.headers.link);
7
+ if (link === undefined) {
8
+ return undefined;
9
+ }
10
+ const current = response.url || response.request.url;
11
+ const url = current.split(/[?#]/u)[0] ?? current;
12
+ const next = yield* Effect.try({
13
+ catch: () => new PaginationLinkError({ reason: "invalid", url }),
14
+ try: () => new URL(link, current),
15
+ });
16
+ if (next.origin !== new URL(current).origin) {
17
+ return yield* new PaginationLinkError({ reason: "cross-origin", url });
18
+ }
19
+ return HttpClientRequest.setUrl(response.request, next);
20
+ });
21
+ export function paginate(client, first) {
22
+ return Stream.unfold(first, (state) => Effect.gen(function* () {
23
+ const request = HttpClientRequest.isHttpClientRequest(state) ? state : yield* nextRequest(state);
24
+ if (request === undefined) {
25
+ return undefined;
26
+ }
27
+ const response = yield* client.execute(request);
28
+ return [response, response];
29
+ }));
30
+ }
31
+ //# sourceMappingURL=paginate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"paginate.js","sourceRoot":"","sources":["../src/paginate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACxC,OAAO,EAAmB,iBAAiB,EAA2B,MAAM,aAAa,CAAC;AAC1F,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEzC,MAAM,WAAW,GAAG,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,QAA+C;IAC/F,MAAM,IAAI,GAAG,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7C,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;QACxB,OAAO,SAAS,CAAC;IAClB,CAAC;IACD,MAAM,OAAO,GAAG,QAAQ,CAAC,GAAG,IAAI,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC;IACrD,MAAM,GAAG,GAAG,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,OAAO,CAAC;IACjD,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC;QAC9B,KAAK,EAAE,GAAG,EAAE,CAAC,IAAI,mBAAmB,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,GAAG,EAAE,CAAC;QAChE,GAAG,EAAE,GAAG,EAAE,CAAC,IAAI,GAAG,CAAC,IAAI,EAAE,OAAO,CAAC;KACjC,CAAC,CAAC;IACH,IAAI,IAAI,CAAC,MAAM,KAAK,IAAI,GAAG,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,CAAC;QAC7C,OAAO,KAAK,CAAC,CAAC,IAAI,mBAAmB,CAAC,EAAE,MAAM,EAAE,cAAc,EAAE,GAAG,EAAE,CAAC,CAAC;IACxE,CAAC;IACD,OAAO,iBAAiB,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;AACzD,CAAC,CAAC,CAAC;AAEH,MAAM,UAAU,QAAQ,CAAO,MAAwC,EAAE,KAA0C;IAClH,OAAO,MAAM,CAAC,MAAM,CAKlB,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE,CAClB,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;QACnB,MAAM,OAAO,GAAG,iBAAiB,CAAC,mBAAmB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;QACjG,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;YAC3B,OAAO,SAAS,CAAC;QAClB,CAAC;QACD,MAAM,QAAQ,GAAG,KAAK,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAChD,OAAO,CAAC,QAAQ,EAAE,QAAQ,CAAU,CAAC;IACtC,CAAC,CAAC,CACF,CAAC;AACH,CAAC"}
@@ -0,0 +1,3 @@
1
+ import { DateTime } from "effect";
2
+ export declare function parseRetryAfter(header: string | undefined, now: DateTime.Utc): DateTime.Utc | undefined;
3
+ //# sourceMappingURL=parseRetryAfter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parseRetryAfter.d.ts","sourceRoot":"","sources":["../src/parseRetryAfter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAU,MAAM,QAAQ,CAAC;AAI1C,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,EAAE,GAAG,EAAE,QAAQ,CAAC,GAAG,GAAG,QAAQ,CAAC,GAAG,GAAG,SAAS,CAYvG"}
@@ -0,0 +1,17 @@
1
+ import { DateTime, Option } from "effect";
2
+ const httpDate = /^(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun), \d{2} [A-Z][a-z]{2} \d{4} \d{2}:\d{2}:\d{2} GMT$/u;
3
+ export function parseRetryAfter(header, now) {
4
+ if (header === undefined) {
5
+ return undefined;
6
+ }
7
+ const value = header.trim();
8
+ let millis = Number.NaN;
9
+ if (/^\d+$/u.test(value)) {
10
+ millis = DateTime.toEpochMillis(now) + Number(value) * 1000;
11
+ }
12
+ else if (httpDate.test(value)) {
13
+ millis = Date.parse(value);
14
+ }
15
+ return Option.getOrUndefined(DateTime.make(millis));
16
+ }
17
+ //# sourceMappingURL=parseRetryAfter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parseRetryAfter.js","sourceRoot":"","sources":["../src/parseRetryAfter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAE1C,MAAM,QAAQ,GAAG,qFAAqF,CAAC;AAEvG,MAAM,UAAU,eAAe,CAAC,MAA0B,EAAE,GAAiB;IAC5E,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QAC1B,OAAO,SAAS,CAAC;IAClB,CAAC;IACD,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,EAAE,CAAC;IAC5B,IAAI,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC;IACxB,IAAI,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;QAC1B,MAAM,GAAG,QAAQ,CAAC,aAAa,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC;IAC7D,CAAC;SAAM,IAAI,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;QACjC,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAC5B,CAAC;IACD,OAAO,MAAM,CAAC,cAAc,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;AACrD,CAAC"}
@@ -0,0 +1,14 @@
1
+ import { Effect } from "effect";
2
+ import { HttpServerError, type HttpServerRequest, HttpServerRespondable, HttpServerResponse } from "effect/http";
3
+ declare const RequestBodyTooLarge_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
4
+ readonly _tag: "RequestBodyTooLarge";
5
+ } & Readonly<A>;
6
+ export declare class RequestBodyTooLarge extends RequestBodyTooLarge_base<{
7
+ readonly request: HttpServerRequest.HttpServerRequest;
8
+ readonly limit: bigint;
9
+ }> implements HttpServerRespondable.Respondable {
10
+ [HttpServerRespondable.symbol](): Effect.Effect<HttpServerResponse.HttpServerResponse, never, never>;
11
+ }
12
+ export declare const readBoundedBody: (request: HttpServerRequest.HttpServerRequest) => Effect.Effect<ArrayBuffer, HttpServerError.HttpServerError | RequestBodyTooLarge, never>;
13
+ export {};
14
+ //# sourceMappingURL=boundedBody.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"boundedBody.d.ts","sourceRoot":"","sources":["../../src/server/boundedBody.ts"],"names":[],"mappings":"AACA,OAAO,EAAQ,MAAM,EAAU,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAuB,eAAe,EAAE,KAAK,iBAAiB,EAAE,qBAAqB,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;;;;AAEtI,qBAAa,mBACZ,SAAQ,yBAAwC;IAC/C,QAAQ,CAAC,OAAO,EAAE,iBAAiB,CAAC,iBAAiB,CAAC;IACtD,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACvB,CACD,YAAW,qBAAqB,CAAC,WAAW;IAE5C,CAAC,qBAAqB,CAAC,MAAM,CAAC,uEAE7B;CACD;AAED,eAAO,MAAM,eAAe,4IA4B1B,CAAC"}
@@ -0,0 +1,36 @@
1
+ import { NodeHttpServerRequest, NodeStream } from "@effect/platform-node";
2
+ import { Data, Effect, Stream } from "effect";
3
+ import { HttpIncomingMessage, HttpServerError, HttpServerRespondable, HttpServerResponse } from "effect/http";
4
+ export class RequestBodyTooLarge extends Data.TaggedError("RequestBodyTooLarge") {
5
+ [HttpServerRespondable.symbol]() {
6
+ return Effect.succeed(HttpServerResponse.empty({ status: 413 }));
7
+ }
8
+ }
9
+ export const readBoundedBody = Effect.fnUntraced(function* (request) {
10
+ const incoming = NodeHttpServerRequest.toIncomingMessage(request);
11
+ const limit = yield* HttpIncomingMessage.MaxBodySize;
12
+ const read = Effect.gen(function* () {
13
+ const length = request.headers["content-length"];
14
+ if (limit !== undefined && length !== undefined && BigInt(length) > limit) {
15
+ return yield* new RequestBodyTooLarge({ limit, request });
16
+ }
17
+ const chunks = [];
18
+ let size = 0n;
19
+ yield* NodeStream.fromReadable({
20
+ closeOnDone: false,
21
+ evaluate: () => incoming,
22
+ onError: (cause) => new HttpServerError.HttpServerError({ reason: new HttpServerError.RequestParseError({ cause, request }) }),
23
+ }).pipe(Stream.runForEach((chunk) => {
24
+ size += BigInt(chunk.byteLength);
25
+ if (limit !== undefined && size > limit) {
26
+ return Effect.fail(new RequestBodyTooLarge({ limit, request }));
27
+ }
28
+ chunks.push(chunk);
29
+ return Effect.void;
30
+ }));
31
+ return Uint8Array.from(Buffer.concat(chunks)).buffer;
32
+ });
33
+ // Drain unread bytes without destroying the socket so the server can send a response.
34
+ return yield* read.pipe(Effect.onError(() => Effect.sync(() => incoming.resume())));
35
+ });
36
+ //# sourceMappingURL=boundedBody.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"boundedBody.js","sourceRoot":"","sources":["../../src/server/boundedBody.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAC1E,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAA0B,qBAAqB,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEtI,MAAM,OAAO,mBACZ,SAAQ,IAAI,CAAC,WAAW,CAAC,qBAAqB,CAG5C;IAGF,CAAC,qBAAqB,CAAC,MAAM,CAAC;QAC7B,OAAO,MAAM,CAAC,OAAO,CAAC,kBAAkB,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC;IAClE,CAAC;CACD;AAED,MAAM,CAAC,MAAM,eAAe,GAAG,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,OAA4C;IACvG,MAAM,QAAQ,GAAG,qBAAqB,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC;IAClE,MAAM,KAAK,GAAG,KAAK,CAAC,CAAC,mBAAmB,CAAC,WAAW,CAAC;IACrD,MAAM,IAAI,GAAG,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;QAChC,MAAM,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAC;QACjD,IAAI,KAAK,KAAK,SAAS,IAAI,MAAM,KAAK,SAAS,IAAI,MAAM,CAAC,MAAM,CAAC,GAAG,KAAK,EAAE,CAAC;YAC3E,OAAO,KAAK,CAAC,CAAC,IAAI,mBAAmB,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC;QAC3D,CAAC;QACD,MAAM,MAAM,GAAiB,EAAE,CAAC;QAChC,IAAI,IAAI,GAAG,EAAE,CAAC;QACd,KAAK,CAAC,CAAC,UAAU,CAAC,YAAY,CAA8C;YAC3E,WAAW,EAAE,KAAK;YAClB,QAAQ,EAAE,GAAG,EAAE,CAAC,QAAQ;YACxB,OAAO,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,eAAe,CAAC,eAAe,CAAC,EAAE,MAAM,EAAE,IAAI,eAAe,CAAC,iBAAiB,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC;SAC9H,CAAC,CAAC,IAAI,CACN,MAAM,CAAC,UAAU,CAAC,CAAC,KAAK,EAAE,EAAE;YAC3B,IAAI,IAAI,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;YACjC,IAAI,KAAK,KAAK,SAAS,IAAI,IAAI,GAAG,KAAK,EAAE,CAAC;gBACzC,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,mBAAmB,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC;YACjE,CAAC;YACD,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YACnB,OAAO,MAAM,CAAC,IAAI,CAAC;QACpB,CAAC,CAAC,CACF,CAAC;QACF,OAAO,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC;IACtD,CAAC,CAAC,CAAC;IACH,sFAAsF;IACtF,OAAO,KAAK,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC;AACrF,CAAC,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,77 @@
1
1
  {
2
2
  "name": "@shivaedev/http",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "Bounded HTTP bodies, status failures and paginated clients over Effect HTTP",
5
+ "keywords": [
6
+ "effect"
7
+ ],
8
+ "homepage": "https://github.com/ShivaeDev/platform/tree/main/packages/http#readme",
9
+ "bugs": {
10
+ "url": "https://github.com/ShivaeDev/platform/issues"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/ShivaeDev/platform.git",
15
+ "directory": "packages/http"
16
+ },
17
+ "license": "MIT",
18
+ "sideEffects": false,
19
+ "type": "module",
20
+ "imports": {
21
+ "#test/*": "./src/test-support/*",
22
+ "#*.ts": {
23
+ "source": "./src/*.ts",
24
+ "types": "./dist/*.d.ts",
25
+ "default": "./dist/*.js"
26
+ }
27
+ },
28
+ "exports": {
29
+ "./*.ts": {
30
+ "source": "./src/*.ts",
31
+ "types": "./dist/*.d.ts",
32
+ "default": "./dist/*.js"
33
+ },
34
+ "./package.json": "./package.json"
35
+ },
36
+ "files": [
37
+ "dist",
38
+ "src",
39
+ "!src/**/*.test.ts",
40
+ "!src/**/*.test.tsx",
41
+ "!src/**/*.spec.ts",
42
+ "!src/**/*.spec.tsx",
43
+ "!src/**/test-support",
44
+ "README.md"
45
+ ],
46
+ "devDependencies": {
47
+ "@effect/platform-node": "4.0.5",
48
+ "@shivaedev/quality": "0.42.0",
49
+ "@shivaedev/test-story": "0.21.0",
50
+ "@types/node": "24.19.2",
51
+ "effect": "4.0.5",
52
+ "typescript": "7.0.2",
53
+ "vitest": "5.0.5"
54
+ },
55
+ "peerDependencies": {
56
+ "@effect/platform-node": "4.0.5",
57
+ "effect": "4.0.5"
58
+ },
59
+ "peerDependenciesMeta": {
60
+ "@effect/platform-node": {
61
+ "optional": true
62
+ }
63
+ },
64
+ "engines": {
65
+ "node": ">=24"
66
+ },
67
+ "publishConfig": {
68
+ "access": "public",
69
+ "provenance": true
70
+ },
71
+ "scripts": {
72
+ "build": "node --eval \"import('node:fs').then(({ rmSync }) => rmSync('dist', { force: true, recursive: true }))\" && tsc --project tsconfig.build.json",
73
+ "ready": "pnpm typecheck && pnpm test && pnpm build",
74
+ "test": "vitest run",
75
+ "typecheck": "tsc --noEmit"
76
+ }
6
77
  }
package/src/body.ts ADDED
@@ -0,0 +1,37 @@
1
+ import { Effect, Schema, Stream } from "effect";
2
+ import type { HttpClientResponse } from "effect/http";
3
+ import { ResponseBodyTooLarge } from "./errors.ts";
4
+
5
+ export const readBoundedBody = Effect.fnUntraced(function* (response: HttpClientResponse.HttpClientResponse, limit: number) {
6
+ const length = response.headers["content-length"];
7
+ if (length !== undefined && Number(length) > limit) {
8
+ return yield* new ResponseBodyTooLarge({ limit });
9
+ }
10
+ const chunks: Uint8Array[] = [];
11
+ let size = 0;
12
+ yield* response.stream.pipe(
13
+ Stream.runForEach((chunk) => {
14
+ size += chunk.byteLength;
15
+ if (size > limit) {
16
+ return Effect.fail(new ResponseBodyTooLarge({ limit }));
17
+ }
18
+ chunks.push(chunk);
19
+ return Effect.void;
20
+ }),
21
+ );
22
+ const bytes = new Uint8Array(size);
23
+ let offset = 0;
24
+ for (const chunk of chunks) {
25
+ bytes.set(chunk, offset);
26
+ offset += chunk.byteLength;
27
+ }
28
+ return bytes;
29
+ });
30
+
31
+ export function readBoundedText(response: HttpClientResponse.HttpClientResponse, limit: number) {
32
+ return Effect.map(readBoundedBody(response, limit), (bytes) => new TextDecoder().decode(bytes));
33
+ }
34
+
35
+ export function readBoundedJson<TSchema extends Schema.Constraint>(response: HttpClientResponse.HttpClientResponse, limit: number, schema: TSchema) {
36
+ return Effect.flatMap(readBoundedText(response, limit), Schema.decodeUnknownEffect(Schema.fromJsonString(schema)));
37
+ }
package/src/errors.ts ADDED
@@ -0,0 +1,23 @@
1
+ import { Schema } from "effect";
2
+
3
+ const answer = { method: Schema.String, status: Schema.Number, url: Schema.String };
4
+
5
+ export class RemoteUnauthorized extends Schema.TaggedError<RemoteUnauthorized>()("RemoteUnauthorized", answer) {}
6
+ export class RemoteNotFound extends Schema.TaggedError<RemoteNotFound>()("RemoteNotFound", answer) {}
7
+ export class RemoteGone extends Schema.TaggedError<RemoteGone>()("RemoteGone", answer) {}
8
+ export class RemoteRateLimited extends Schema.TaggedError<RemoteRateLimited>()("RemoteRateLimited", {
9
+ ...answer,
10
+ retryAfter: Schema.optionalKey(Schema.DateTimeUtc),
11
+ }) {}
12
+ export class RemoteHttpError extends Schema.TaggedError<RemoteHttpError>()("RemoteHttpError", answer) {}
13
+
14
+ export type RemoteStatusError = RemoteUnauthorized | RemoteNotFound | RemoteGone | RemoteRateLimited | RemoteHttpError;
15
+
16
+ export class PaginationLinkError extends Schema.TaggedError<PaginationLinkError>()("PaginationLinkError", {
17
+ reason: Schema.Literals(["cross-origin", "invalid"]),
18
+ url: Schema.String,
19
+ }) {}
20
+
21
+ export class ResponseBodyTooLarge extends Schema.TaggedError<ResponseBodyTooLarge>()("ResponseBodyTooLarge", {
22
+ limit: Schema.Number,
23
+ }) {}
@@ -0,0 +1,38 @@
1
+ import { DateTime, Effect } from "effect";
2
+ import { HttpClient, type HttpClientResponse } from "effect/http";
3
+ import { RemoteGone, RemoteHttpError, RemoteNotFound, RemoteRateLimited, type RemoteStatusError, RemoteUnauthorized } from "./errors.ts";
4
+ import { parseRetryAfter } from "./parseRetryAfter.ts";
5
+
6
+ const checkStatus = Effect.fnUntraced(function* (response: HttpClientResponse.HttpClientResponse) {
7
+ const { status, request } = response;
8
+ if (status >= 200 && status < 300) {
9
+ return response;
10
+ }
11
+ const fields = { method: request.method, status, url: request.url.split(/[?#]/u)[0] ?? request.url };
12
+ let error: RemoteStatusError;
13
+ switch (status) {
14
+ case 401:
15
+ error = new RemoteUnauthorized(fields);
16
+ break;
17
+ case 404:
18
+ error = new RemoteNotFound(fields);
19
+ break;
20
+ case 410:
21
+ error = new RemoteGone(fields);
22
+ break;
23
+ default: {
24
+ const header = response.headers["retry-after"];
25
+ if (status === 429 || (status === 503 && header !== undefined)) {
26
+ const retryAfter = parseRetryAfter(header, yield* DateTime.now);
27
+ error = new RemoteRateLimited({ ...fields, ...(retryAfter === undefined ? {} : { retryAfter }) });
28
+ } else {
29
+ error = new RemoteHttpError(fields);
30
+ }
31
+ }
32
+ }
33
+ return yield* Effect.fail(error);
34
+ });
35
+
36
+ export function filterStatus<E, R>(client: HttpClient.HttpClient.With<E, R>): HttpClient.HttpClient.With<E | RemoteStatusError, R> {
37
+ return HttpClient.transformResponse(client, Effect.flatMap(checkStatus));
38
+ }
@@ -0,0 +1,10 @@
1
+ export function nextLink(header: string | undefined): string | undefined {
2
+ for (const link of header?.matchAll(/<(?<url>[^>]*)>(?<parameters>(?:[^<",]|"(?:\\.|[^"\\])*")*)/gu) ?? []) {
3
+ const relation = link.groups?.parameters?.match(/(?:^|;)\s*rel\s*=\s*(?:"(?<quoted>[^"]*)"|(?<unquoted>[^;\s,]+))/iu);
4
+ const relations = relation?.groups?.quoted ?? relation?.groups?.unquoted;
5
+ if (relations?.toLowerCase().split(/\s+/u).includes("next")) {
6
+ return link.groups?.url;
7
+ }
8
+ }
9
+ return undefined;
10
+ }
@@ -0,0 +1,39 @@
1
+ import { Effect, Stream } from "effect";
2
+ import { type HttpClient, HttpClientRequest, type HttpClientResponse } from "effect/http";
3
+ import { PaginationLinkError } from "./errors.ts";
4
+ import { nextLink } from "./nextLink.ts";
5
+
6
+ const nextRequest = Effect.fnUntraced(function* (response: HttpClientResponse.HttpClientResponse) {
7
+ const link = nextLink(response.headers.link);
8
+ if (link === undefined) {
9
+ return undefined;
10
+ }
11
+ const current = response.url || response.request.url;
12
+ const url = current.split(/[?#]/u)[0] ?? current;
13
+ const next = yield* Effect.try({
14
+ catch: () => new PaginationLinkError({ reason: "invalid", url }),
15
+ try: () => new URL(link, current),
16
+ });
17
+ if (next.origin !== new URL(current).origin) {
18
+ return yield* new PaginationLinkError({ reason: "cross-origin", url });
19
+ }
20
+ return HttpClientRequest.setUrl(response.request, next);
21
+ });
22
+
23
+ export function paginate<E, R>(client: HttpClient.HttpClient.With<E, R>, first: HttpClientRequest.HttpClientRequest) {
24
+ return Stream.unfold<
25
+ HttpClientRequest.HttpClientRequest | HttpClientResponse.HttpClientResponse,
26
+ HttpClientResponse.HttpClientResponse,
27
+ E | PaginationLinkError,
28
+ R
29
+ >(first, (state) =>
30
+ Effect.gen(function* () {
31
+ const request = HttpClientRequest.isHttpClientRequest(state) ? state : yield* nextRequest(state);
32
+ if (request === undefined) {
33
+ return undefined;
34
+ }
35
+ const response = yield* client.execute(request);
36
+ return [response, response] as const;
37
+ }),
38
+ );
39
+ }
@@ -0,0 +1,17 @@
1
+ import { DateTime, Option } from "effect";
2
+
3
+ const httpDate = /^(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun), \d{2} [A-Z][a-z]{2} \d{4} \d{2}:\d{2}:\d{2} GMT$/u;
4
+
5
+ export function parseRetryAfter(header: string | undefined, now: DateTime.Utc): DateTime.Utc | undefined {
6
+ if (header === undefined) {
7
+ return undefined;
8
+ }
9
+ const value = header.trim();
10
+ let millis = Number.NaN;
11
+ if (/^\d+$/u.test(value)) {
12
+ millis = DateTime.toEpochMillis(now) + Number(value) * 1000;
13
+ } else if (httpDate.test(value)) {
14
+ millis = Date.parse(value);
15
+ }
16
+ return Option.getOrUndefined(DateTime.make(millis));
17
+ }
@@ -0,0 +1,45 @@
1
+ import { NodeHttpServerRequest, NodeStream } from "@effect/platform-node";
2
+ import { Data, Effect, Stream } from "effect";
3
+ import { HttpIncomingMessage, HttpServerError, type HttpServerRequest, HttpServerRespondable, HttpServerResponse } from "effect/http";
4
+
5
+ export class RequestBodyTooLarge
6
+ extends Data.TaggedError("RequestBodyTooLarge")<{
7
+ readonly request: HttpServerRequest.HttpServerRequest;
8
+ readonly limit: bigint;
9
+ }>
10
+ implements HttpServerRespondable.Respondable
11
+ {
12
+ [HttpServerRespondable.symbol]() {
13
+ return Effect.succeed(HttpServerResponse.empty({ status: 413 }));
14
+ }
15
+ }
16
+
17
+ export const readBoundedBody = Effect.fnUntraced(function* (request: HttpServerRequest.HttpServerRequest) {
18
+ const incoming = NodeHttpServerRequest.toIncomingMessage(request);
19
+ const limit = yield* HttpIncomingMessage.MaxBodySize;
20
+ const read = Effect.gen(function* () {
21
+ const length = request.headers["content-length"];
22
+ if (limit !== undefined && length !== undefined && BigInt(length) > limit) {
23
+ return yield* new RequestBodyTooLarge({ limit, request });
24
+ }
25
+ const chunks: Uint8Array[] = [];
26
+ let size = 0n;
27
+ yield* NodeStream.fromReadable<Uint8Array, HttpServerError.HttpServerError>({
28
+ closeOnDone: false,
29
+ evaluate: () => incoming,
30
+ onError: (cause) => new HttpServerError.HttpServerError({ reason: new HttpServerError.RequestParseError({ cause, request }) }),
31
+ }).pipe(
32
+ Stream.runForEach((chunk) => {
33
+ size += BigInt(chunk.byteLength);
34
+ if (limit !== undefined && size > limit) {
35
+ return Effect.fail(new RequestBodyTooLarge({ limit, request }));
36
+ }
37
+ chunks.push(chunk);
38
+ return Effect.void;
39
+ }),
40
+ );
41
+ return Uint8Array.from(Buffer.concat(chunks)).buffer;
42
+ });
43
+ // Drain unread bytes without destroying the socket so the server can send a response.
44
+ return yield* read.pipe(Effect.onError(() => Effect.sync(() => incoming.resume())));
45
+ });