@shivaedev/http 0.2.0 → 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,7 +56,7 @@ const routes = HttpRouter.add("POST", "/webhook", (request) =>
|
|
|
56
56
|
);
|
|
57
57
|
```
|
|
58
58
|
|
|
59
|
-
Oversized `content-length` headers are rejected before reading. Native Effect owns body collection and connection cleanup after chunked overflow; connection reuse depends on its unread-body drain limit. `RequestBodyTooLarge` is a
|
|
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
|
|
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import { Effect } from "effect";
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
2
|
import { type HttpServerRequest, HttpServerRespondable, HttpServerResponse } from "effect/http";
|
|
3
|
-
declare const RequestBodyTooLarge_base:
|
|
4
|
-
readonly
|
|
5
|
-
}
|
|
6
|
-
export declare class RequestBodyTooLarge extends RequestBodyTooLarge_base
|
|
7
|
-
readonly request: HttpServerRequest.HttpServerRequest;
|
|
8
|
-
readonly limit: bigint;
|
|
9
|
-
}> implements HttpServerRespondable.Respondable {
|
|
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
9
|
export declare const readBoundedBody: (request: HttpServerRequest.HttpServerRequest) => Effect.Effect<ArrayBuffer, import("effect/http/HttpServerError").HttpServerError | RequestBodyTooLarge, never>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"boundedBody.d.ts","sourceRoot":"","sources":["../../src/server/boundedBody.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
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,20 +1,21 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
2
|
import { HttpIncomingMessage, HttpServerRespondable, HttpServerResponse } from "effect/http";
|
|
3
3
|
import * as HttpStatus from "effect/http/HttpStatus";
|
|
4
|
-
export class RequestBodyTooLarge extends
|
|
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
9
|
export const readBoundedBody = Effect.fnUntraced(function* (request) {
|
|
10
|
-
const
|
|
11
|
-
if (
|
|
12
|
-
return yield*
|
|
10
|
+
const maxBodySize = yield* HttpIncomingMessage.MaxBodySize;
|
|
11
|
+
if (maxBodySize === undefined) {
|
|
12
|
+
return yield* new RequestBodyTooLarge({ limit: 0 });
|
|
13
13
|
}
|
|
14
|
+
const limit = Number(maxBodySize);
|
|
14
15
|
const length = request.headers["content-length"];
|
|
15
|
-
if (length !== undefined && BigInt(length) >
|
|
16
|
-
return yield* new RequestBodyTooLarge({ limit
|
|
16
|
+
if (length !== undefined && BigInt(length) > maxBodySize) {
|
|
17
|
+
return yield* new RequestBodyTooLarge({ limit });
|
|
17
18
|
}
|
|
18
|
-
return yield* request.arrayBuffer.pipe(Effect.catchIf((error) => error.reason._tag === "RequestParseError" && error.reason.reason === "BodyTooLarge", () => new RequestBodyTooLarge({ limit
|
|
19
|
+
return yield* request.arrayBuffer.pipe(Effect.catchIf((error) => error.reason._tag === "RequestParseError" && error.reason.reason === "BodyTooLarge", () => new RequestBodyTooLarge({ limit })));
|
|
19
20
|
});
|
|
20
21
|
//# sourceMappingURL=boundedBody.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"boundedBody.js","sourceRoot":"","sources":["../../src/server/boundedBody.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
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,EAA0B,qBAAqB,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACrH,OAAO,KAAK,UAAU,MAAM,wBAAwB,CAAC;AAErD,MAAM,OAAO,mBACZ,SAAQ,MAAM,CAAC,WAAW,EAAuB,CAAC,qBAAqB,EAAE,EAAE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC;IAGlG,CAAC,qBAAqB,CAAC,MAAM,CAAC;QAC7B,OAAO,MAAM,CA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ginated clients over Effect HTTP",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"effect"
|
|
@@ -45,8 +45,8 @@
|
|
|
45
45
|
],
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@effect/platform-node": "4.0.6",
|
|
48
|
-
"@shivaedev/quality": "0.
|
|
49
|
-
"@shivaedev/test-story": "0.
|
|
48
|
+
"@shivaedev/quality": "0.46.0",
|
|
49
|
+
"@shivaedev/test-story": "0.25.0",
|
|
50
50
|
"@types/node": "24.19.2",
|
|
51
51
|
"effect": "4.0.6",
|
|
52
52
|
"typescript": "7.0.2",
|
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
2
|
import { HttpIncomingMessage, type HttpServerRequest, HttpServerRespondable, HttpServerResponse } from "effect/http";
|
|
3
3
|
import * as HttpStatus from "effect/http/HttpStatus";
|
|
4
4
|
|
|
5
5
|
export class RequestBodyTooLarge
|
|
6
|
-
extends
|
|
7
|
-
readonly request: HttpServerRequest.HttpServerRequest;
|
|
8
|
-
readonly limit: bigint;
|
|
9
|
-
}>
|
|
6
|
+
extends Schema.TaggedError<RequestBodyTooLarge>()("RequestBodyTooLarge", { limit: Schema.Number })
|
|
10
7
|
implements HttpServerRespondable.Respondable
|
|
11
8
|
{
|
|
12
9
|
[HttpServerRespondable.symbol]() {
|
|
@@ -15,18 +12,19 @@ export class RequestBodyTooLarge
|
|
|
15
12
|
}
|
|
16
13
|
|
|
17
14
|
export const readBoundedBody = Effect.fnUntraced(function* (request: HttpServerRequest.HttpServerRequest) {
|
|
18
|
-
const
|
|
19
|
-
if (
|
|
20
|
-
return yield*
|
|
15
|
+
const maxBodySize = yield* HttpIncomingMessage.MaxBodySize;
|
|
16
|
+
if (maxBodySize === undefined) {
|
|
17
|
+
return yield* new RequestBodyTooLarge({ limit: 0 });
|
|
21
18
|
}
|
|
19
|
+
const limit = Number(maxBodySize);
|
|
22
20
|
const length = request.headers["content-length"];
|
|
23
|
-
if (length !== undefined && BigInt(length) >
|
|
24
|
-
return yield* new RequestBodyTooLarge({ limit
|
|
21
|
+
if (length !== undefined && BigInt(length) > maxBodySize) {
|
|
22
|
+
return yield* new RequestBodyTooLarge({ limit });
|
|
25
23
|
}
|
|
26
24
|
return yield* request.arrayBuffer.pipe(
|
|
27
25
|
Effect.catchIf(
|
|
28
26
|
(error) => error.reason._tag === "RequestParseError" && error.reason.reason === "BodyTooLarge",
|
|
29
|
-
() => new RequestBodyTooLarge({ limit
|
|
27
|
+
() => new RequestBodyTooLarge({ limit }),
|
|
30
28
|
),
|
|
31
29
|
);
|
|
32
30
|
});
|