@shivaedev/http 0.3.1 → 0.5.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/CHANGELOG.md CHANGED
@@ -1,5 +1,34 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.5.0 - 2026-11-03
4
+
5
+ ### Added
6
+
7
+ - Export `checkStatus` from `checkStatus.ts` to check an existing response with the same typed failures as `filterStatus`.
8
+ - Export `isRemoteStatusError` from `errors.ts` to recognize the shared HTTP status failures.
9
+
10
+ ### Breaking
11
+
12
+ - in `server/boundedBody.ts`, call `readBoundedBody()` instead of `readBoundedBody(request)`. The reader requires the native `HttpServerRequest.HttpServerRequest` in the Effect environment. Native router handlers already provide it; other callers must use `Effect.provideService(HttpServerRequest.HttpServerRequest, request)`.
13
+
14
+ ## 0.4.0 - 2026-11-02
15
+
16
+ ### Fixed
17
+
18
+ - Read HEAD responses and statuses 204, 205 and 304 as empty bytes or text; JSON decoding still rejects empty input.
19
+ - Limit encoded responses by bytes exposed by the native client stream, without comparing compressed Content-Length to decoded bytes. Fetch exposes decompressed bytes; Node's native HTTP transport exposes encoded bytes.
20
+ - Remove userinfo, query strings and fragments from status-error URLs.
21
+ - Keep pagination links on the request's origin.
22
+ - Release response bodies before returning status or body-size failures, and keep schema failures from holding HTTP connections.
23
+ - Release GitHub response bodies on rejection and before refreshing a rejected installation token, so repeated failures do not exhaust the connection pool.
24
+
25
+ ### Breaking
26
+
27
+ - Bounded response readers reject NaN, infinite and negative limits with the typed `InvalidBodyLimit` error from `errors.ts`. Pass a finite nonnegative byte limit and handle the new failure in the error channel.
28
+ - GitHub methods using the bounded response reader also expose `InvalidBodyLimit` from `@shivaedev/http/errors.ts` for invalid byte limits. Supply finite nonnegative limits and handle that failure alongside existing response failures.
29
+ - Pagination rejects automatic fetch redirects so custom headers cannot reach another origin. Pass a direct same-origin page URL instead of a redirecting URL; links to later pages must also use direct URLs. Other fetch options in the current Effect context are preserved. Use a client without redirect-following middleware.
30
+ - GitHub listing methods inherit the redirect restriction. List the repository's current API path instead of an endpoint that redirects after a rename or transfer.
31
+
3
32
  ## 0.3.1 - 2026-10-31
4
33
 
5
34
  ### Fixed
package/README.md CHANGED
@@ -33,23 +33,27 @@ Everything here wraps a native `HttpClient`, `HttpClientResponse` or `HttpServer
33
33
 
34
34
  ### Calling outside APIs
35
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.
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 userinfo, query string or fragment 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
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.
38
+ `checkStatus(response)` from `checkStatus.ts` applies the same status mapping to an existing native response. `filterStatus` delegates to it. Use `isRemoteStatusError(error)` from `errors.ts` to recognize any of these typed status failures.
39
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`.
40
+ `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. Pagination disables automatic fetch redirects with `redirect: "error"`, preserving other fetch options from the current Effect context: a redirect fails through the native client's error channel before sending custom headers to its destination. Use direct page URLs and a client without redirect-following middleware. `nextLink(header)` exposes the link parsing.
41
+
42
+ `readBoundedBody(response, limit)` returns bytes; `readBoundedText` decodes UTF-8, and `readBoundedJson(response, limit, schema)` decodes JSON through the supplied Schema. The limit counts bytes exposed by the native client's `response.stream`, including decompressed bytes with `FetchHttpClient`. The reader does no decompression of its own; Node's native HTTP transport exposes encoded bytes. Responses with `Content-Encoding` are limited during streaming, without comparing their wire `Content-Length` to the limit. For unencoded responses, an oversized `Content-Length` fails before reading the body. Crossing the stream-byte limit fails with `ResponseBodyTooLarge`, carrying `limit`.
43
+
44
+ HEAD responses and statuses 204, 205 and 304 read as empty bytes or text, regardless of metadata lengths. JSON decoding still rejects empty input. Limits must be finite and nonnegative; NaN, either infinity and negative limits fail with `InvalidBodyLimit` from `errors.ts`, carrying `limit`. Zero accepts only an empty body.
41
45
 
42
46
  ### Reading request bodies on a native route
43
47
 
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:
48
+ For a native Node HTTP route, use `readBoundedBody()` from `server/boundedBody.ts` with Effect's `HttpIncomingMessage.MaxBodySize`. It reads the native `HttpServerRequest.HttpServerRequest` from the Effect environment and returns an `ArrayBuffer` without decoding the bytes:
45
49
 
46
50
  ```ts
47
51
  import { ByteSize, Effect } from "effect";
48
52
  import { HttpIncomingMessage, HttpRouter, HttpServerResponse } from "effect/http";
49
53
  import { readBoundedBody } from "@shivaedev/http/server/boundedBody.ts";
50
54
 
51
- const routes = HttpRouter.add("POST", "/webhook", (request) =>
52
- readBoundedBody(request).pipe(
55
+ const routes = HttpRouter.add("POST", "/webhook", () =>
56
+ readBoundedBody().pipe(
53
57
  Effect.map((body) => HttpServerResponse.uint8Array(new Uint8Array(body))),
54
58
  Effect.provideService(HttpIncomingMessage.MaxBodySize, ByteSize.bytes(64 * 1024)),
55
59
  ),
package/dist/body.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Effect, Schema } from "effect";
2
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"]>;
3
+ import { InvalidBodyLimit, ResponseBodyTooLarge } from "./errors.ts";
4
+ export declare const readBoundedBody: (response: HttpClientResponse.HttpClientResponse, limit: number) => Effect.Effect<Uint8Array<ArrayBuffer>, import("effect/http/HttpClientError").HttpClientError | InvalidBodyLimit | ResponseBodyTooLarge, never>;
5
+ export declare function readBoundedText(response: HttpClientResponse.HttpClientResponse, limit: number): Effect.Effect<string, import("effect/http/HttpClientError").HttpClientError | InvalidBodyLimit | 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 | InvalidBodyLimit | ResponseBodyTooLarge | Schema.SchemaError, TSchema["DecodingServices"]>;
7
7
  //# sourceMappingURL=body.d.ts.map
@@ -1 +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"}
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,gBAAgB,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAErE,eAAO,MAAM,eAAe,oNAkC1B,CAAC;AAEH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,kBAAkB,CAAC,kBAAkB,EAAE,KAAK,EAAE,MAAM,iIAE7F;AAED,wBAAgB,eAAe,CAAC,OAAO,SAAS,MAAM,CAAC,UAAU,EAAE,QAAQ,EAAE,kBAAkB,CAAC,kBAAkB,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,qLAEjJ"}
package/dist/body.js CHANGED
@@ -1,8 +1,18 @@
1
1
  import { Effect, Schema, Stream } from "effect";
2
- import { ResponseBodyTooLarge } from "./errors.js";
2
+ import { InvalidBodyLimit, ResponseBodyTooLarge } from "./errors.js";
3
3
  export const readBoundedBody = Effect.fnUntraced(function* (response, limit) {
4
+ const empty = response.request.method === "HEAD" || response.status === 204 || response.status === 205 || response.status === 304;
5
+ const invalid = !Number.isFinite(limit) || limit < 0;
4
6
  const length = response.headers["content-length"];
5
- if (length !== undefined && Number(length) > limit) {
7
+ const oversized = response.headers["content-encoding"] === undefined && length !== undefined && Number(length) > limit;
8
+ if (invalid || empty || oversized) {
9
+ yield* Effect.scoped(Stream.toPull(response.stream));
10
+ if (invalid) {
11
+ return yield* new InvalidBodyLimit({ limit });
12
+ }
13
+ if (empty) {
14
+ return new Uint8Array();
15
+ }
6
16
  return yield* new ResponseBodyTooLarge({ limit });
7
17
  }
8
18
  const chunks = [];
package/dist/body.js.map CHANGED
@@ -1 +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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AAC,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,5 @@
1
+ import { Effect } from "effect";
2
+ import type { HttpClientResponse } from "effect/http";
3
+ import { RemoteGone, RemoteHttpError, RemoteNotFound, RemoteRateLimited, RemoteUnauthorized } from "./errors.ts";
4
+ export declare const checkStatus: (response: HttpClientResponse.HttpClientResponse) => Effect.Effect<HttpClientResponse.HttpClientResponse, RemoteGone | RemoteHttpError | RemoteNotFound | RemoteRateLimited | RemoteUnauthorized, never>;
5
+ //# sourceMappingURL=checkStatus.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"checkStatus.d.ts","sourceRoot":"","sources":["../src/checkStatus.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,MAAM,EAAU,MAAM,QAAQ,CAAC;AAClD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEtD,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,cAAc,EAAE,iBAAiB,EAA0B,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAGzI,eAAO,MAAM,WAAW,0MA8BtB,CAAC"}
@@ -0,0 +1,37 @@
1
+ import { DateTime, Effect, Stream } from "effect";
2
+ import * as HttpStatus from "effect/http/HttpStatus";
3
+ import { RemoteGone, RemoteHttpError, RemoteNotFound, RemoteRateLimited, RemoteUnauthorized } from "./errors.js";
4
+ import { parseRetryAfter } from "./parseRetryAfter.js";
5
+ export const checkStatus = Effect.fnUntraced(function* (response) {
6
+ const { status, request } = response;
7
+ if (status >= HttpStatus.fromLiteral("OK") && status < HttpStatus.fromLiteral("MultipleChoices")) {
8
+ return response;
9
+ }
10
+ const url = new URL(request.url);
11
+ const fields = { method: request.method, status, url: `${url.origin}${url.pathname}` };
12
+ let error;
13
+ switch (status) {
14
+ case HttpStatus.fromLiteral("Unauthorized"):
15
+ error = new RemoteUnauthorized(fields);
16
+ break;
17
+ case HttpStatus.fromLiteral("NotFound"):
18
+ error = new RemoteNotFound(fields);
19
+ break;
20
+ case HttpStatus.fromLiteral("Gone"):
21
+ error = new RemoteGone(fields);
22
+ break;
23
+ default: {
24
+ const header = response.headers["retry-after"];
25
+ if (status === HttpStatus.fromLiteral("TooManyRequests") || (status === HttpStatus.fromLiteral("ServiceUnavailable") && header !== undefined)) {
26
+ const retryAfter = parseRetryAfter(header, yield* DateTime.now);
27
+ error = new RemoteRateLimited({ ...fields, ...(retryAfter === undefined ? {} : { retryAfter }) });
28
+ }
29
+ else {
30
+ error = new RemoteHttpError(fields);
31
+ }
32
+ }
33
+ }
34
+ yield* Effect.scoped(Stream.toPull(response.stream));
35
+ return yield* Effect.fail(error);
36
+ });
37
+ //# sourceMappingURL=checkStatus.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"checkStatus.js","sourceRoot":"","sources":["../src/checkStatus.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAElD,OAAO,KAAK,UAAU,MAAM,wBAAwB,CAAC;AACrD,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,CAAC,MAAM,WAAW,GAAG,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,QAA+C;IACtG,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,QAAQ,CAAC;IACrC,IAAI,MAAM,IAAI,UAAU,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,MAAM,GAAG,UAAU,CAAC,WAAW,CAAC,iBAAiB,CAAC,EAAE,CAAC;QAClG,OAAO,QAAQ,CAAC;IACjB,CAAC;IACD,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IACjC,MAAM,MAAM,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,GAAG,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC,QAAQ,EAAE,EAAE,CAAC;IACvF,IAAI,KAAwB,CAAC;IAC7B,QAAQ,MAAM,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-36,7 +36,9 @@ declare const RemoteHttpError_base: Schema.Class<RemoteHttpError, Schema.TaggedS
36
36
  export declare class RemoteHttpError extends RemoteHttpError_base {
37
37
  get message(): string;
38
38
  }
39
- export type RemoteStatusError = RemoteUnauthorized | RemoteNotFound | RemoteGone | RemoteRateLimited | RemoteHttpError;
39
+ declare const RemoteStatusError: Schema.Union<readonly [typeof RemoteUnauthorized, typeof RemoteNotFound, typeof RemoteGone, typeof RemoteRateLimited, typeof RemoteHttpError]>;
40
+ export type RemoteStatusError = typeof RemoteStatusError.Type;
41
+ export declare const isRemoteStatusError: <I>(input: I) => input is I & (RemoteGone | RemoteHttpError | RemoteNotFound | RemoteRateLimited | RemoteUnauthorized);
40
42
  declare const PaginationLinkError_base: Schema.Class<PaginationLinkError, Schema.TaggedStruct<"PaginationLinkError", {
41
43
  readonly reason: Schema.Literals<readonly ["cross-origin", "invalid"]>;
42
44
  readonly url: Schema.String;
@@ -48,5 +50,10 @@ declare const ResponseBodyTooLarge_base: Schema.Class<ResponseBodyTooLarge, Sche
48
50
  }>, import("effect/Cause").YieldableError>;
49
51
  export declare class ResponseBodyTooLarge extends ResponseBodyTooLarge_base {
50
52
  }
53
+ declare const InvalidBodyLimit_base: Schema.Class<InvalidBodyLimit, Schema.TaggedStruct<"InvalidBodyLimit", {
54
+ readonly limit: Schema.Number;
55
+ }>, import("effect/Cause").YieldableError>;
56
+ export declare class InvalidBodyLimit extends InvalidBodyLimit_base {
57
+ }
51
58
  export {};
52
59
  //# sourceMappingURL=errors.d.ts.map
@@ -1 +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;IACpG,IAAa,OAAO,IAAI,MAAM,CAG7B;CACD;AAED,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"}
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;IACpG,IAAa,OAAO,IAAI,MAAM,CAG7B;CACD;AAED,QAAA,MAAM,iBAAiB,gJAAqG,CAAC;AAC7H,MAAM,MAAM,iBAAiB,GAAG,OAAO,iBAAiB,CAAC,IAAI,CAAC;AAC9D,eAAO,MAAM,mBAAmB,wHAA+B,CAAC;;;;;AAEhE,qBAAa,mBAAoB,SAAQ,wBAGvC;CAAG;;;;AAEL,qBAAa,oBAAqB,SAAQ,yBAExC;CAAG;;;;AAEL,qBAAa,gBAAiB,SAAQ,qBAEpC;CAAG"}
package/dist/errors.js CHANGED
@@ -17,6 +17,8 @@ export class RemoteHttpError extends Schema.TaggedError()("RemoteHttpError", ans
17
17
  return `HTTP ${this.status}: ${this.method} ${url.host}${url.pathname}`;
18
18
  }
19
19
  }
20
+ const RemoteStatusError = Schema.Union([RemoteUnauthorized, RemoteNotFound, RemoteGone, RemoteRateLimited, RemoteHttpError]);
21
+ export const isRemoteStatusError = Schema.is(RemoteStatusError);
20
22
  export class PaginationLinkError extends Schema.TaggedError()("PaginationLinkError", {
21
23
  reason: Schema.Literals(["cross-origin", "invalid"]),
22
24
  url: Schema.String,
@@ -26,4 +28,8 @@ export class ResponseBodyTooLarge extends Schema.TaggedError()("ResponseBodyTooL
26
28
  limit: Schema.Number,
27
29
  }) {
28
30
  }
31
+ export class InvalidBodyLimit extends Schema.TaggedError()("InvalidBodyLimit", {
32
+ limit: Schema.Number,
33
+ }) {
34
+ }
29
35
  //# sourceMappingURL=errors.js.map
@@ -1 +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;IACpG,IAAa,OAAO;QACnB,MAAM,GAAG,GAAG,IAAI,G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AAC;CAClD,CAAC;CAAG;AACL,MAAM,OAAO,eAAgB,SAAQ,MAAM,CAAC,WAAW,EAAmB,CAAC,iBAAiB,EAAE,MAAM,CAAC;IACpG,IAAa,OAAO;QACnB,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAC9B,OAAO,QAAQ,IAAI,CAAC,MAAM,KAAK,IAAI,CAAC,MAAM,IAAI,GAAG,CAAC,IAAI,GAAG,GAAG,CAAC,QAAQ,EAAE,CAAC;IACzE,CAAC;CACD;AAED,MAAM,iBAAiB,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,kBAAkB,EAAE,cAAc,EAAE,UAAU,EAAE,iBAAiB,EAAE,eAAe,CAAC,CAAC,CAAC;AAE7H,MAAM,CAAC,MAAM,mBAAmB,GAAG,MAAM,CAAC,EAAE,CAAC,iBAAiB,CAAC,CAAC;AAEhE,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;AAEL,MAAM,OAAO,gBAAiB,SAAQ,MAAM,CAAC,WAAW,EAAoB,CAAC,kBAAkB,EAAE;IAChG,KAAK,EAAE,MAAM,CAAC,MAAM;CACpB,CAAC;CAAG"}
@@ -1,4 +1,4 @@
1
1
  import { HttpClient } from "effect/http";
2
- import { type RemoteStatusError } from "./errors.ts";
2
+ import type { RemoteStatusError } from "./errors.ts";
3
3
  export declare function filterStatus<E, R>(client: HttpClient.HttpClient.With<E, R>): HttpClient.HttpClient.With<E | RemoteStatusError, R>;
4
4
  //# sourceMappingURL=filterStatus.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"filterStatus.d.ts","sourceRoot":"","sources":["../src/filterStatus.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAA2B,MAAM,aAAa,CAAC;AAElE,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"}
1
+ {"version":3,"file":"filterStatus.d.ts","sourceRoot":"","sources":["../src/filterStatus.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAErD,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"}
@@ -1,38 +1,6 @@
1
- import { DateTime, Effect } from "effect";
1
+ import { Effect } from "effect";
2
2
  import { HttpClient } from "effect/http";
3
- import * as HttpStatus from "effect/http/HttpStatus";
4
- import { RemoteGone, RemoteHttpError, RemoteNotFound, RemoteRateLimited, RemoteUnauthorized } from "./errors.js";
5
- import { parseRetryAfter } from "./parseRetryAfter.js";
6
- const checkStatus = Effect.fnUntraced(function* (response) {
7
- const { status, request } = response;
8
- if (status >= HttpStatus.fromLiteral("OK") && status < HttpStatus.fromLiteral("MultipleChoices")) {
9
- return response;
10
- }
11
- const fields = { method: request.method, status, url: request.url.split(/[?#]/u)[0] ?? request.url };
12
- let error;
13
- switch (status) {
14
- case HttpStatus.fromLiteral("Unauthorized"):
15
- error = new RemoteUnauthorized(fields);
16
- break;
17
- case HttpStatus.fromLiteral("NotFound"):
18
- error = new RemoteNotFound(fields);
19
- break;
20
- case HttpStatus.fromLiteral("Gone"):
21
- error = new RemoteGone(fields);
22
- break;
23
- default: {
24
- const header = response.headers["retry-after"];
25
- if (status === HttpStatus.fromLiteral("TooManyRequests") || (status === HttpStatus.fromLiteral("ServiceUnavailable") && header !== undefined)) {
26
- const retryAfter = parseRetryAfter(header, yield* DateTime.now);
27
- error = new RemoteRateLimited({ ...fields, ...(retryAfter === undefined ? {} : { retryAfter }) });
28
- }
29
- else {
30
- error = new RemoteHttpError(fields);
31
- }
32
- }
33
- }
34
- return yield* Effect.fail(error);
35
- });
3
+ import { checkStatus } from "./checkStatus.js";
36
4
  export function filterStatus(client) {
37
5
  return HttpClient.transformResponse(client, Effect.flatMap(checkStatus));
38
6
  }
@@ -1 +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,KAAK,UAAU,MAAM,wBAAwB,CAAC;AACrD,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,UAAU,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,MAAM,GAAG,UAAU,CAAC,W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AK,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"}
1
+ {"version":3,"file":"filterStatus.js","sourceRoot":"","sources":["../src/filterStatus.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAG/C,MAAM,UAAU,YAAY,CAAO,MAAwC;IAC1E,OAAO,UAAU,CAAC,iBAAiB,CAAC,MAAM,EAAE,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC;AAC1E,CAAC"}
@@ -1 +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"}
1
+ {"version":3,"file":"paginate.d.ts","sourceRoot":"","sources":["../src/paginate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChD,OAAO,EAAmB,KAAK,UAAU,EAAE,iBAAiB,EAAE,KAAK,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAC3G,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,oFAmBlH"}
package/dist/paginate.js CHANGED
@@ -1,5 +1,5 @@
1
- import { Effect, Stream } from "effect";
2
- import { HttpClientRequest } from "effect/http";
1
+ import { Effect, Option, Stream } from "effect";
2
+ import { FetchHttpClient, HttpClientRequest } from "effect/http";
3
3
  import { PaginationLinkError } from "./errors.js";
4
4
  import { nextLink } from "./nextLink.js";
5
5
  const nextRequest = Effect.fnUntraced(function* (response) {
@@ -13,7 +13,7 @@ const nextRequest = Effect.fnUntraced(function* (response) {
13
13
  catch: () => new PaginationLinkError({ reason: "invalid", url }),
14
14
  try: () => new URL(link, current),
15
15
  });
16
- if (next.origin !== new URL(current).origin) {
16
+ if (next.origin !== new URL(response.request.url).origin) {
17
17
  return yield* new PaginationLinkError({ reason: "cross-origin", url });
18
18
  }
19
19
  return HttpClientRequest.setUrl(response.request, next);
@@ -24,7 +24,10 @@ export function paginate(client, first) {
24
24
  if (request === undefined) {
25
25
  return undefined;
26
26
  }
27
- const response = yield* client.execute(request);
27
+ const options = yield* Effect.serviceOption(FetchHttpClient.RequestInit);
28
+ const response = yield* client
29
+ .execute(request)
30
+ .pipe(Effect.provideService(FetchHttpClient.RequestInit, { ...Option.getOrUndefined(options), redirect: "error" }));
28
31
  return [response, response];
29
32
  }));
30
33
  }
@@ -1 +1 @@
1
- {"version":3,"file":"paginate.js","sourceRoot":"","sources":["../src/paginate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE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,CAAC,MAAM,CAAC,cAAc,CAAC,eAAe,CAAC,WAAW,EAAE,EAAE,GAAG,MAAM,CAAC,cAAc,CAAC,OAAO,CAAC,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC;QACrH,OAAO,CAAC,QAAQ,EAAE,QAAQ,CAAU,CAAC;IACtC,CAAC,CAAC,CACF,CAAC;AACH,CAAC"}
@@ -1,11 +1,11 @@
1
1
  import { Effect, Schema } from "effect";
2
- import { type HttpServerRequest, HttpServerRespondable, HttpServerResponse } from "effect/http";
2
+ import { HttpServerRequest, HttpServerRespondable, HttpServerResponse } from "effect/http";
3
3
  declare const RequestBodyTooLarge_base: Schema.Class<RequestBodyTooLarge, Schema.TaggedStruct<"RequestBodyTooLarge", {
4
4
  readonly limit: Schema.Number;
5
5
  }>, import("effect/Cause").YieldableError>;
6
6
  export declare class RequestBodyTooLarge extends RequestBodyTooLarge_base implements HttpServerRespondable.Respondable {
7
7
  [HttpServerRespondable.symbol](): Effect.Effect<HttpServerResponse.HttpServerResponse, never, never>;
8
8
  }
9
- export declare const readBoundedBody: (request: HttpServerRequest.HttpServerRequest) => Effect.Effect<ArrayBuffer, import("effect/http/HttpServerError").HttpServerError | RequestBodyTooLarge, never>;
9
+ export declare const readBoundedBody: () => Effect.Effect<ArrayBuffer, import("effect/http/HttpServerError").HttpServerError | RequestBodyTooLarge, HttpServerRequest.HttpServerRequest>;
10
10
  export {};
11
11
  //# sourceMappingURL=boundedBody.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"boundedBody.d.ts","sourceRoot":"","sources":["../../src/server/boundedBody.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACxC,OAAO,EAAuB,KAAK,iBAAiB,EAAE,qBAAqB,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;;;;AAGrH,qBAAa,mBACZ,SAAQ,wBACR,YAAW,qBAAqB,CAAC,WAAW;IAE5C,CAAC,qBAAqB,CAAC,MAAM,CAAC,uEAE7B;CACD;AAED,eAAO,MAAM,eAAe,kKAgB1B,CAAC"}
1
+ {"version":3,"file":"boundedBody.d.ts","sourceRoot":"","sources":["../../src/server/boundedBody.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACxC,OAAO,EAAuB,iBAAiB,EAAE,qBAAqB,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;;;;AAGhH,qBAAa,mBACZ,SAAQ,wBACR,YAAW,qBAAqB,CAAC,WAAW;IAE5C,CAAC,qBAAqB,CAAC,MAAM,CAAC,uEAE7B;CACD;AAED,eAAO,MAAM,eAAe,oJAiB1B,CAAC"}
@@ -1,12 +1,13 @@
1
1
  import { Effect, Schema } from "effect";
2
- import { HttpIncomingMessage, HttpServerRespondable, HttpServerResponse } from "effect/http";
2
+ import { HttpIncomingMessage, HttpServerRequest, HttpServerRespondable, HttpServerResponse } from "effect/http";
3
3
  import * as HttpStatus from "effect/http/HttpStatus";
4
4
  export class RequestBodyTooLarge extends Schema.TaggedError()("RequestBodyTooLarge", { limit: Schema.Number }) {
5
5
  [HttpServerRespondable.symbol]() {
6
6
  return Effect.succeed(HttpServerResponse.empty({ status: HttpStatus.fromLiteral("PayloadTooLarge") }));
7
7
  }
8
8
  }
9
- export const readBoundedBody = Effect.fnUntraced(function* (request) {
9
+ export const readBoundedBody = Effect.fnUntraced(function* () {
10
+ const request = yield* HttpServerRequest.HttpServerRequest;
10
11
  const maxBodySize = yield* HttpIncomingMessage.MaxBodySize;
11
12
  if (maxBodySize === undefined) {
12
13
  return yield* new RequestBodyTooLarge({ limit: 0 });
@@ -1 +1 @@
1
- {"version":3,"file":"boundedBody.js","sourceRoot":"","sources":["../../src/server/boundedBody.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACxC,OAAO,EAAE,mBAAmB,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S,IAAI,MAAM,CAAC,MAAM,CAAC,GAAG,WAAW,EAAE,CAAC;QAC1D,OAAO,KAAK,CAAC,CAAC,IAAI,mBAAmB,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;IAClD,CAAC;IACD,OAAO,KAAK,CAAC,CAAC,OAAO,CAAC,WAAW,CAAC,IAAI,CACrC,MAAM,CAAC,OAAO,CACb,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,KAAK,mBAAmB,IAAI,KAAK,CAAC,MAAM,CAAC,MAAM,KAAK,cAAc,EAC9F,GAAG,EAAE,CAAC,IAAI,mBAAmB,CAAC,EAAE,KAAK,EAAE,CAAC,CACxC,CACD,CAAC;AACH,CAAC,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shivaedev/http",
3
- "version": "0.3.1",
3
+ "version": "0.5.0",
4
4
  "description": "Bounded HTTP bodies, status failures and paginated clients over Effect HTTP",
5
5
  "keywords": [
6
6
  "effect"
@@ -46,8 +46,8 @@
46
46
  ],
47
47
  "devDependencies": {
48
48
  "@effect/platform-node": "4.0.6",
49
- "@shivaedev/quality": "0.48.1",
50
- "@shivaedev/test-story": "0.27.0",
49
+ "@shivaedev/quality": "0.57.0",
50
+ "@shivaedev/test-story": "0.35.0",
51
51
  "@types/node": "24.19.2",
52
52
  "effect": "4.0.6",
53
53
  "typescript": "7.0.2",
package/src/body.ts CHANGED
@@ -1,10 +1,20 @@
1
1
  import { Effect, Schema, Stream } from "effect";
2
2
  import type { HttpClientResponse } from "effect/http";
3
- import { ResponseBodyTooLarge } from "./errors.ts";
3
+ import { InvalidBodyLimit, ResponseBodyTooLarge } from "./errors.ts";
4
4
 
5
5
  export const readBoundedBody = Effect.fnUntraced(function* (response: HttpClientResponse.HttpClientResponse, limit: number) {
6
+ const empty = response.request.method === "HEAD" || response.status === 204 || response.status === 205 || response.status === 304;
7
+ const invalid = !Number.isFinite(limit) || limit < 0;
6
8
  const length = response.headers["content-length"];
7
- if (length !== undefined && Number(length) > limit) {
9
+ const oversized = response.headers["content-encoding"] === undefined && length !== undefined && Number(length) > limit;
10
+ if (invalid || empty || oversized) {
11
+ yield* Effect.scoped(Stream.toPull(response.stream));
12
+ if (invalid) {
13
+ return yield* new InvalidBodyLimit({ limit });
14
+ }
15
+ if (empty) {
16
+ return new Uint8Array();
17
+ }
8
18
  return yield* new ResponseBodyTooLarge({ limit });
9
19
  }
10
20
  const chunks: Uint8Array[] = [];
@@ -0,0 +1,37 @@
1
+ import { DateTime, Effect, Stream } from "effect";
2
+ import type { HttpClientResponse } from "effect/http";
3
+ import * as HttpStatus from "effect/http/HttpStatus";
4
+ import { RemoteGone, RemoteHttpError, RemoteNotFound, RemoteRateLimited, type RemoteStatusError, RemoteUnauthorized } from "./errors.ts";
5
+ import { parseRetryAfter } from "./parseRetryAfter.ts";
6
+
7
+ export const checkStatus = Effect.fnUntraced(function* (response: HttpClientResponse.HttpClientResponse) {
8
+ const { status, request } = response;
9
+ if (status >= HttpStatus.fromLiteral("OK") && status < HttpStatus.fromLiteral("MultipleChoices")) {
10
+ return response;
11
+ }
12
+ const url = new URL(request.url);
13
+ const fields = { method: request.method, status, url: `${url.origin}${url.pathname}` };
14
+ let error: RemoteStatusError;
15
+ switch (status) {
16
+ case HttpStatus.fromLiteral("Unauthorized"):
17
+ error = new RemoteUnauthorized(fields);
18
+ break;
19
+ case HttpStatus.fromLiteral("NotFound"):
20
+ error = new RemoteNotFound(fields);
21
+ break;
22
+ case HttpStatus.fromLiteral("Gone"):
23
+ error = new RemoteGone(fields);
24
+ break;
25
+ default: {
26
+ const header = response.headers["retry-after"];
27
+ if (status === HttpStatus.fromLiteral("TooManyRequests") || (status === HttpStatus.fromLiteral("ServiceUnavailable") && header !== undefined)) {
28
+ const retryAfter = parseRetryAfter(header, yield* DateTime.now);
29
+ error = new RemoteRateLimited({ ...fields, ...(retryAfter === undefined ? {} : { retryAfter }) });
30
+ } else {
31
+ error = new RemoteHttpError(fields);
32
+ }
33
+ }
34
+ }
35
+ yield* Effect.scoped(Stream.toPull(response.stream));
36
+ return yield* Effect.fail(error);
37
+ });
package/src/errors.ts CHANGED
@@ -16,7 +16,9 @@ export class RemoteHttpError extends Schema.TaggedError<RemoteHttpError>()("Remo
16
16
  }
17
17
  }
18
18
 
19
- export type RemoteStatusError = RemoteUnauthorized | RemoteNotFound | RemoteGone | RemoteRateLimited | RemoteHttpError;
19
+ const RemoteStatusError = Schema.Union([RemoteUnauthorized, RemoteNotFound, RemoteGone, RemoteRateLimited, RemoteHttpError]);
20
+ export type RemoteStatusError = typeof RemoteStatusError.Type;
21
+ export const isRemoteStatusError = Schema.is(RemoteStatusError);
20
22
 
21
23
  export class PaginationLinkError extends Schema.TaggedError<PaginationLinkError>()("PaginationLinkError", {
22
24
  reason: Schema.Literals(["cross-origin", "invalid"]),
@@ -26,3 +28,7 @@ export class PaginationLinkError extends Schema.TaggedError<PaginationLinkError>
26
28
  export class ResponseBodyTooLarge extends Schema.TaggedError<ResponseBodyTooLarge>()("ResponseBodyTooLarge", {
27
29
  limit: Schema.Number,
28
30
  }) {}
31
+
32
+ export class InvalidBodyLimit extends Schema.TaggedError<InvalidBodyLimit>()("InvalidBodyLimit", {
33
+ limit: Schema.Number,
34
+ }) {}
@@ -1,38 +1,7 @@
1
- import { DateTime, Effect } from "effect";
2
- import { HttpClient, type HttpClientResponse } from "effect/http";
3
- import * as HttpStatus from "effect/http/HttpStatus";
4
- import { RemoteGone, RemoteHttpError, RemoteNotFound, RemoteRateLimited, type RemoteStatusError, RemoteUnauthorized } from "./errors.ts";
5
- import { parseRetryAfter } from "./parseRetryAfter.ts";
6
-
7
- const checkStatus = Effect.fnUntraced(function* (response: HttpClientResponse.HttpClientResponse) {
8
- const { status, request } = response;
9
- if (status >= HttpStatus.fromLiteral("OK") && status < HttpStatus.fromLiteral("MultipleChoices")) {
10
- return response;
11
- }
12
- const fields = { method: request.method, status, url: request.url.split(/[?#]/u)[0] ?? request.url };
13
- let error: RemoteStatusError;
14
- switch (status) {
15
- case HttpStatus.fromLiteral("Unauthorized"):
16
- error = new RemoteUnauthorized(fields);
17
- break;
18
- case HttpStatus.fromLiteral("NotFound"):
19
- error = new RemoteNotFound(fields);
20
- break;
21
- case HttpStatus.fromLiteral("Gone"):
22
- error = new RemoteGone(fields);
23
- break;
24
- default: {
25
- const header = response.headers["retry-after"];
26
- if (status === HttpStatus.fromLiteral("TooManyRequests") || (status === HttpStatus.fromLiteral("ServiceUnavailable") && header !== undefined)) {
27
- const retryAfter = parseRetryAfter(header, yield* DateTime.now);
28
- error = new RemoteRateLimited({ ...fields, ...(retryAfter === undefined ? {} : { retryAfter }) });
29
- } else {
30
- error = new RemoteHttpError(fields);
31
- }
32
- }
33
- }
34
- return yield* Effect.fail(error);
35
- });
1
+ import { Effect } from "effect";
2
+ import { HttpClient } from "effect/http";
3
+ import { checkStatus } from "./checkStatus.ts";
4
+ import type { RemoteStatusError } from "./errors.ts";
36
5
 
37
6
  export function filterStatus<E, R>(client: HttpClient.HttpClient.With<E, R>): HttpClient.HttpClient.With<E | RemoteStatusError, R> {
38
7
  return HttpClient.transformResponse(client, Effect.flatMap(checkStatus));
package/src/paginate.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { Effect, Stream } from "effect";
2
- import { type HttpClient, HttpClientRequest, type HttpClientResponse } from "effect/http";
1
+ import { Effect, Option, Stream } from "effect";
2
+ import { FetchHttpClient, type HttpClient, HttpClientRequest, type HttpClientResponse } from "effect/http";
3
3
  import { PaginationLinkError } from "./errors.ts";
4
4
  import { nextLink } from "./nextLink.ts";
5
5
 
@@ -14,7 +14,7 @@ const nextRequest = Effect.fnUntraced(function* (response: HttpClientResponse.Ht
14
14
  catch: () => new PaginationLinkError({ reason: "invalid", url }),
15
15
  try: () => new URL(link, current),
16
16
  });
17
- if (next.origin !== new URL(current).origin) {
17
+ if (next.origin !== new URL(response.request.url).origin) {
18
18
  return yield* new PaginationLinkError({ reason: "cross-origin", url });
19
19
  }
20
20
  return HttpClientRequest.setUrl(response.request, next);
@@ -32,7 +32,10 @@ export function paginate<E, R>(client: HttpClient.HttpClient.With<E, R>, first:
32
32
  if (request === undefined) {
33
33
  return undefined;
34
34
  }
35
- const response = yield* client.execute(request);
35
+ const options = yield* Effect.serviceOption(FetchHttpClient.RequestInit);
36
+ const response = yield* client
37
+ .execute(request)
38
+ .pipe(Effect.provideService(FetchHttpClient.RequestInit, { ...Option.getOrUndefined(options), redirect: "error" }));
36
39
  return [response, response] as const;
37
40
  }),
38
41
  );
@@ -1,5 +1,5 @@
1
1
  import { Effect, Schema } from "effect";
2
- import { HttpIncomingMessage, type HttpServerRequest, HttpServerRespondable, HttpServerResponse } from "effect/http";
2
+ import { HttpIncomingMessage, HttpServerRequest, HttpServerRespondable, HttpServerResponse } from "effect/http";
3
3
  import * as HttpStatus from "effect/http/HttpStatus";
4
4
 
5
5
  export class RequestBodyTooLarge
@@ -11,7 +11,8 @@ export class RequestBodyTooLarge
11
11
  }
12
12
  }
13
13
 
14
- export const readBoundedBody = Effect.fnUntraced(function* (request: HttpServerRequest.HttpServerRequest) {
14
+ export const readBoundedBody = Effect.fnUntraced(function* () {
15
+ const request = yield* HttpServerRequest.HttpServerRequest;
15
16
  const maxBodySize = yield* HttpIncomingMessage.MaxBodySize;
16
17
  if (maxBodySize === undefined) {
17
18
  return yield* new RequestBodyTooLarge({ limit: 0 });