@shivaedev/http 0.1.1 → 0.3.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/README.md CHANGED
@@ -56,8 +56,8 @@ const routes = HttpRouter.add("POST", "/webhook", (request) =>
56
56
  );
57
57
  ```
58
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. Consume the body once through this reader and decode the result in the application.
59
+ Oversized `content-length` headers are rejected before reading. If `MaxBodySize` is undefined, the reader refuses before reading with `RequestBodyTooLarge` and `limit: 0`, including on routes mounted outside `appHost`. Native Effect owns body collection and connection cleanup after chunked overflow; connection reuse depends on its unread-body drain limit. `RequestBodyTooLarge` is a Schema tagged error with the string tag `"RequestBodyTooLarge"` and a numeric byte `limit`, without the request or its credentials. It implements Effect's `HttpServerRespondable` protocol with status 413. Consume the body once through this reader and decode the result in the application.
60
60
 
61
61
  ### Install and limits
62
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.
63
+ Install `@shivaedev/http` with `effect` as its peer. The package has no root entry; import the module that defines the name.
@@ -1,14 +1,11 @@
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 {
1
+ import { Effect, Schema } from "effect";
2
+ import { type HttpServerRequest, HttpServerRespondable, HttpServerResponse } from "effect/http";
3
+ declare const RequestBodyTooLarge_base: Schema.Class<RequestBodyTooLarge, Schema.TaggedStruct<"RequestBodyTooLarge", {
4
+ readonly limit: Schema.Number;
5
+ }>, import("effect/Cause").YieldableError>;
6
+ export declare class RequestBodyTooLarge extends RequestBodyTooLarge_base implements HttpServerRespondable.Respondable {
10
7
  [HttpServerRespondable.symbol](): Effect.Effect<HttpServerResponse.HttpServerResponse, never, never>;
11
8
  }
12
- export declare const readBoundedBody: (request: HttpServerRequest.HttpServerRequest) => Effect.Effect<ArrayBuffer, HttpServerError.HttpServerError | RequestBodyTooLarge, never>;
9
+ export declare const readBoundedBody: (request: HttpServerRequest.HttpServerRequest) => Effect.Effect<ArrayBuffer, import("effect/http/HttpServerError").HttpServerError | RequestBodyTooLarge, never>;
13
10
  export {};
14
11
  //# sourceMappingURL=boundedBody.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"boundedBody.d.ts","sourceRoot":"","sources":["../../src/server/boundedBody.ts"],"names":[],"mappings":"AAEA,OAAO,EAAQ,MAAM,EAAU,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAuB,eAAe,EAAE,KAAK,iBAAiB,EAAE,qBAAqB,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;;;;AAGtI,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"}
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,38 +1,21 @@
1
- import * as NodeHttpServerRequest from "@effect/platform-node/NodeHttpServerRequest";
2
- import * as NodeStream from "@effect/platform-node/NodeStream";
3
- import { Data, Effect, Stream } from "effect";
4
- import { HttpIncomingMessage, HttpServerError, HttpServerRespondable, HttpServerResponse } from "effect/http";
1
+ import { Effect, Schema } from "effect";
2
+ import { HttpIncomingMessage, HttpServerRespondable, HttpServerResponse } from "effect/http";
5
3
  import * as HttpStatus from "effect/http/HttpStatus";
6
- export class RequestBodyTooLarge extends Data.TaggedError("RequestBodyTooLarge") {
4
+ export class RequestBodyTooLarge extends Schema.TaggedError()("RequestBodyTooLarge", { limit: Schema.Number }) {
7
5
  [HttpServerRespondable.symbol]() {
8
6
  return Effect.succeed(HttpServerResponse.empty({ status: HttpStatus.fromLiteral("PayloadTooLarge") }));
9
7
  }
10
8
  }
11
9
  export const readBoundedBody = Effect.fnUntraced(function* (request) {
12
- const incoming = NodeHttpServerRequest.toIncomingMessage(request);
13
- const limit = yield* HttpIncomingMessage.MaxBodySize;
14
- const read = Effect.gen(function* () {
15
- const length = request.headers["content-length"];
16
- if (limit !== undefined && length !== undefined && BigInt(length) > limit) {
17
- return yield* new RequestBodyTooLarge({ limit, request });
18
- }
19
- const chunks = [];
20
- let size = 0n;
21
- yield* NodeStream.fromReadable({
22
- closeOnDone: false,
23
- evaluate: () => incoming,
24
- onError: (cause) => new HttpServerError.HttpServerError({ reason: new HttpServerError.RequestParseError({ cause, request }) }),
25
- }).pipe(Stream.runForEach((chunk) => {
26
- size += BigInt(chunk.byteLength);
27
- if (limit !== undefined && size > limit) {
28
- return Effect.fail(new RequestBodyTooLarge({ limit, request }));
29
- }
30
- chunks.push(chunk);
31
- return Effect.void;
32
- }));
33
- return Uint8Array.from(Buffer.concat(chunks)).buffer;
34
- });
35
- // Drain unread bytes without destroying the socket so the server can send a response.
36
- return yield* read.pipe(Effect.onError(() => Effect.sync(() => incoming.resume())));
10
+ const maxBodySize = yield* HttpIncomingMessage.MaxBodySize;
11
+ if (maxBodySize === undefined) {
12
+ return yield* new RequestBodyTooLarge({ limit: 0 });
13
+ }
14
+ const limit = Number(maxBodySize);
15
+ const length = request.headers["content-length"];
16
+ if (length !== undefined && BigInt(length) > maxBodySize) {
17
+ return yield* new RequestBodyTooLarge({ limit });
18
+ }
19
+ return yield* request.arrayBuffer.pipe(Effect.catchIf((error) => error.reason._tag === "RequestParseError" && error.reason.reason === "BodyTooLarge", () => new RequestBodyTooLarge({ limit })));
37
20
  });
38
21
  //# sourceMappingURL=boundedBody.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"boundedBody.js","sourceRoot":"","sources":["../../src/server/boundedBody.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,qBAAqB,MAAM,6CAA6C,CAAC;AACrF,OAAO,KAAK,UAAU,MAAM,kCAAkC,CAAC;AAC/D,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;AACtI,OAAO,KAAK,UAAU,MAAM,wBAAwB,CAAC;AAErD,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,UAAU,CAAC,WAAW,CAAC,iBAAiB,CAAC,EAAE,CAAC,CAAC,CAA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CAAC,WAAW,CAAC;IAC3D,IAAI,WAAW,KAAK,SAAS,EAAE,CAAC;QAC/B,OAAO,KAAK,CAAC,CAAC,IAAI,mBAAmB,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC;IACrD,CAAC;IACD,MAAM,KAAK,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC;IAClC,MAAM,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAC;IACjD,IAAI,MAAM,KAAK,SAAS,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.1.1",
3
+ "version": "0.3.0",
4
4
  "description": "Bounded HTTP bodies, status failures and paginated clients over Effect HTTP",
5
5
  "keywords": [
6
6
  "effect"
@@ -44,22 +44,16 @@
44
44
  "README.md"
45
45
  ],
46
46
  "devDependencies": {
47
- "@effect/platform-node": "4.0.5",
48
- "@shivaedev/quality": "0.44.0",
49
- "@shivaedev/test-story": "0.22.0",
47
+ "@effect/platform-node": "4.0.6",
48
+ "@shivaedev/quality": "0.46.0",
49
+ "@shivaedev/test-story": "0.25.0",
50
50
  "@types/node": "24.19.2",
51
- "effect": "4.0.5",
51
+ "effect": "4.0.6",
52
52
  "typescript": "7.0.2",
53
53
  "vitest": "5.0.5"
54
54
  },
55
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
- }
56
+ "effect": "4.0.6"
63
57
  },
64
58
  "engines": {
65
59
  "node": ">=24"
@@ -1,14 +1,9 @@
1
- import * as NodeHttpServerRequest from "@effect/platform-node/NodeHttpServerRequest";
2
- import * as NodeStream from "@effect/platform-node/NodeStream";
3
- import { Data, Effect, Stream } from "effect";
4
- import { HttpIncomingMessage, HttpServerError, type HttpServerRequest, HttpServerRespondable, HttpServerResponse } from "effect/http";
1
+ import { Effect, Schema } from "effect";
2
+ import { HttpIncomingMessage, type HttpServerRequest, HttpServerRespondable, HttpServerResponse } from "effect/http";
5
3
  import * as HttpStatus from "effect/http/HttpStatus";
6
4
 
7
5
  export class RequestBodyTooLarge
8
- extends Data.TaggedError("RequestBodyTooLarge")<{
9
- readonly request: HttpServerRequest.HttpServerRequest;
10
- readonly limit: bigint;
11
- }>
6
+ extends Schema.TaggedError<RequestBodyTooLarge>()("RequestBodyTooLarge", { limit: Schema.Number })
12
7
  implements HttpServerRespondable.Respondable
13
8
  {
14
9
  [HttpServerRespondable.symbol]() {
@@ -17,31 +12,19 @@ export class RequestBodyTooLarge
17
12
  }
18
13
 
19
14
  export const readBoundedBody = Effect.fnUntraced(function* (request: HttpServerRequest.HttpServerRequest) {
20
- const incoming = NodeHttpServerRequest.toIncomingMessage(request);
21
- const limit = yield* HttpIncomingMessage.MaxBodySize;
22
- const read = Effect.gen(function* () {
23
- const length = request.headers["content-length"];
24
- if (limit !== undefined && length !== undefined && BigInt(length) > limit) {
25
- return yield* new RequestBodyTooLarge({ limit, request });
26
- }
27
- const chunks: Uint8Array[] = [];
28
- let size = 0n;
29
- yield* NodeStream.fromReadable<Uint8Array, HttpServerError.HttpServerError>({
30
- closeOnDone: false,
31
- evaluate: () => incoming,
32
- onError: (cause) => new HttpServerError.HttpServerError({ reason: new HttpServerError.RequestParseError({ cause, request }) }),
33
- }).pipe(
34
- Stream.runForEach((chunk) => {
35
- size += BigInt(chunk.byteLength);
36
- if (limit !== undefined && size > limit) {
37
- return Effect.fail(new RequestBodyTooLarge({ limit, request }));
38
- }
39
- chunks.push(chunk);
40
- return Effect.void;
41
- }),
42
- );
43
- return Uint8Array.from(Buffer.concat(chunks)).buffer;
44
- });
45
- // Drain unread bytes without destroying the socket so the server can send a response.
46
- return yield* read.pipe(Effect.onError(() => Effect.sync(() => incoming.resume())));
15
+ const maxBodySize = yield* HttpIncomingMessage.MaxBodySize;
16
+ if (maxBodySize === undefined) {
17
+ return yield* new RequestBodyTooLarge({ limit: 0 });
18
+ }
19
+ const limit = Number(maxBodySize);
20
+ const length = request.headers["content-length"];
21
+ if (length !== undefined && BigInt(length) > maxBodySize) {
22
+ return yield* new RequestBodyTooLarge({ limit });
23
+ }
24
+ return yield* request.arrayBuffer.pipe(
25
+ Effect.catchIf(
26
+ (error) => error.reason._tag === "RequestParseError" && error.reason.reason === "BodyTooLarge",
27
+ () => new RequestBodyTooLarge({ limit }),
28
+ ),
29
+ );
47
30
  });