@shivaedev/http 0.1.0 → 0.1.1
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 +11 -11
- package/dist/filterStatus.d.ts.map +1 -1
- package/dist/filterStatus.js +6 -5
- package/dist/filterStatus.js.map +1 -1
- package/dist/server/boundedBody.d.ts.map +1 -1
- package/dist/server/boundedBody.js +4 -2
- package/dist/server/boundedBody.js.map +1 -1
- package/package.json +3 -3
- package/src/filterStatus.ts +6 -5
- package/src/server/boundedBody.ts +4 -2
package/README.md
CHANGED
|
@@ -14,12 +14,12 @@ import { filterStatus } from "@shivaedev/http/filterStatus.ts";
|
|
|
14
14
|
import { paginate } from "@shivaedev/http/paginate.ts";
|
|
15
15
|
|
|
16
16
|
const items = Effect.gen(function* () {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
23
|
});
|
|
24
24
|
```
|
|
25
25
|
|
|
@@ -49,14 +49,14 @@ import { HttpIncomingMessage, HttpRouter, HttpServerResponse } from "effect/http
|
|
|
49
49
|
import { readBoundedBody } from "@shivaedev/http/server/boundedBody.ts";
|
|
50
50
|
|
|
51
51
|
const routes = HttpRouter.add("POST", "/webhook", (request) =>
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
52
|
+
readBoundedBody(request).pipe(
|
|
53
|
+
Effect.map((body) => HttpServerResponse.uint8Array(new Uint8Array(body))),
|
|
54
|
+
Effect.provideService(HttpIncomingMessage.MaxBodySize, ByteSize.bytes(64 * 1024)),
|
|
55
|
+
),
|
|
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
|
|
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.
|
|
60
60
|
|
|
61
61
|
### Install and limits
|
|
62
62
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"filterStatus.d.ts","sourceRoot":"","sources":["../src/filterStatus.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAA2B,MAAM,aAAa,CAAC;
|
|
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"}
|
package/dist/filterStatus.js
CHANGED
|
@@ -1,27 +1,28 @@
|
|
|
1
1
|
import { DateTime, Effect } from "effect";
|
|
2
2
|
import { HttpClient } from "effect/http";
|
|
3
|
+
import * as HttpStatus from "effect/http/HttpStatus";
|
|
3
4
|
import { RemoteGone, RemoteHttpError, RemoteNotFound, RemoteRateLimited, RemoteUnauthorized } from "./errors.js";
|
|
4
5
|
import { parseRetryAfter } from "./parseRetryAfter.js";
|
|
5
6
|
const checkStatus = Effect.fnUntraced(function* (response) {
|
|
6
7
|
const { status, request } = response;
|
|
7
|
-
if (status >=
|
|
8
|
+
if (status >= HttpStatus.fromLiteral("OK") && status < HttpStatus.fromLiteral("MultipleChoices")) {
|
|
8
9
|
return response;
|
|
9
10
|
}
|
|
10
11
|
const fields = { method: request.method, status, url: request.url.split(/[?#]/u)[0] ?? request.url };
|
|
11
12
|
let error;
|
|
12
13
|
switch (status) {
|
|
13
|
-
case
|
|
14
|
+
case HttpStatus.fromLiteral("Unauthorized"):
|
|
14
15
|
error = new RemoteUnauthorized(fields);
|
|
15
16
|
break;
|
|
16
|
-
case
|
|
17
|
+
case HttpStatus.fromLiteral("NotFound"):
|
|
17
18
|
error = new RemoteNotFound(fields);
|
|
18
19
|
break;
|
|
19
|
-
case
|
|
20
|
+
case HttpStatus.fromLiteral("Gone"):
|
|
20
21
|
error = new RemoteGone(fields);
|
|
21
22
|
break;
|
|
22
23
|
default: {
|
|
23
24
|
const header = response.headers["retry-after"];
|
|
24
|
-
if (status ===
|
|
25
|
+
if (status === HttpStatus.fromLiteral("TooManyRequests") || (status === HttpStatus.fromLiteral("ServiceUnavailable") && header !== undefined)) {
|
|
25
26
|
const retryAfter = parseRetryAfter(header, yield* DateTime.now);
|
|
26
27
|
error = new RemoteRateLimited({ ...fields, ...(retryAfter === undefined ? {} : { retryAfter }) });
|
|
27
28
|
}
|
package/dist/filterStatus.js.map
CHANGED
|
@@ -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,EAAE,UAAU,EAAE,eAAe,EAAE,cAAc,EAAE,iBAAiB,EAA0B,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACzI,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AA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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,9 +1,11 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as NodeHttpServerRequest from "@effect/platform-node/NodeHttpServerRequest";
|
|
2
|
+
import * as NodeStream from "@effect/platform-node/NodeStream";
|
|
2
3
|
import { Data, Effect, Stream } from "effect";
|
|
3
4
|
import { HttpIncomingMessage, HttpServerError, HttpServerRespondable, HttpServerResponse } from "effect/http";
|
|
5
|
+
import * as HttpStatus from "effect/http/HttpStatus";
|
|
4
6
|
export class RequestBodyTooLarge extends Data.TaggedError("RequestBodyTooLarge") {
|
|
5
7
|
[HttpServerRespondable.symbol]() {
|
|
6
|
-
return Effect.succeed(HttpServerResponse.empty({ status:
|
|
8
|
+
return Effect.succeed(HttpServerResponse.empty({ status: HttpStatus.fromLiteral("PayloadTooLarge") }));
|
|
7
9
|
}
|
|
8
10
|
}
|
|
9
11
|
export const readBoundedBody = Effect.fnUntraced(function* (request) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"boundedBody.js","sourceRoot":"","sources":["../../src/server/boundedBody.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"boundedBody.js","sourceRoot":"","sources":["../../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4
4
|
"description": "Bounded HTTP bodies, status failures and paginated 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.5",
|
|
48
|
-
"@shivaedev/quality": "0.
|
|
49
|
-
"@shivaedev/test-story": "0.
|
|
48
|
+
"@shivaedev/quality": "0.44.0",
|
|
49
|
+
"@shivaedev/test-story": "0.22.0",
|
|
50
50
|
"@types/node": "24.19.2",
|
|
51
51
|
"effect": "4.0.5",
|
|
52
52
|
"typescript": "7.0.2",
|
package/src/filterStatus.ts
CHANGED
|
@@ -1,28 +1,29 @@
|
|
|
1
1
|
import { DateTime, Effect } from "effect";
|
|
2
2
|
import { HttpClient, type HttpClientResponse } from "effect/http";
|
|
3
|
+
import * as HttpStatus from "effect/http/HttpStatus";
|
|
3
4
|
import { RemoteGone, RemoteHttpError, RemoteNotFound, RemoteRateLimited, type RemoteStatusError, RemoteUnauthorized } from "./errors.ts";
|
|
4
5
|
import { parseRetryAfter } from "./parseRetryAfter.ts";
|
|
5
6
|
|
|
6
7
|
const checkStatus = Effect.fnUntraced(function* (response: HttpClientResponse.HttpClientResponse) {
|
|
7
8
|
const { status, request } = response;
|
|
8
|
-
if (status >=
|
|
9
|
+
if (status >= HttpStatus.fromLiteral("OK") && status < HttpStatus.fromLiteral("MultipleChoices")) {
|
|
9
10
|
return response;
|
|
10
11
|
}
|
|
11
12
|
const fields = { method: request.method, status, url: request.url.split(/[?#]/u)[0] ?? request.url };
|
|
12
13
|
let error: RemoteStatusError;
|
|
13
14
|
switch (status) {
|
|
14
|
-
case
|
|
15
|
+
case HttpStatus.fromLiteral("Unauthorized"):
|
|
15
16
|
error = new RemoteUnauthorized(fields);
|
|
16
17
|
break;
|
|
17
|
-
case
|
|
18
|
+
case HttpStatus.fromLiteral("NotFound"):
|
|
18
19
|
error = new RemoteNotFound(fields);
|
|
19
20
|
break;
|
|
20
|
-
case
|
|
21
|
+
case HttpStatus.fromLiteral("Gone"):
|
|
21
22
|
error = new RemoteGone(fields);
|
|
22
23
|
break;
|
|
23
24
|
default: {
|
|
24
25
|
const header = response.headers["retry-after"];
|
|
25
|
-
if (status ===
|
|
26
|
+
if (status === HttpStatus.fromLiteral("TooManyRequests") || (status === HttpStatus.fromLiteral("ServiceUnavailable") && header !== undefined)) {
|
|
26
27
|
const retryAfter = parseRetryAfter(header, yield* DateTime.now);
|
|
27
28
|
error = new RemoteRateLimited({ ...fields, ...(retryAfter === undefined ? {} : { retryAfter }) });
|
|
28
29
|
} else {
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as NodeHttpServerRequest from "@effect/platform-node/NodeHttpServerRequest";
|
|
2
|
+
import * as NodeStream from "@effect/platform-node/NodeStream";
|
|
2
3
|
import { Data, Effect, Stream } from "effect";
|
|
3
4
|
import { HttpIncomingMessage, HttpServerError, type HttpServerRequest, HttpServerRespondable, HttpServerResponse } from "effect/http";
|
|
5
|
+
import * as HttpStatus from "effect/http/HttpStatus";
|
|
4
6
|
|
|
5
7
|
export class RequestBodyTooLarge
|
|
6
8
|
extends Data.TaggedError("RequestBodyTooLarge")<{
|
|
@@ -10,7 +12,7 @@ export class RequestBodyTooLarge
|
|
|
10
12
|
implements HttpServerRespondable.Respondable
|
|
11
13
|
{
|
|
12
14
|
[HttpServerRespondable.symbol]() {
|
|
13
|
-
return Effect.succeed(HttpServerResponse.empty({ status:
|
|
15
|
+
return Effect.succeed(HttpServerResponse.empty({ status: HttpStatus.fromLiteral("PayloadTooLarge") }));
|
|
14
16
|
}
|
|
15
17
|
}
|
|
16
18
|
|