@effect-aws/http-handler 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.
@@ -0,0 +1,6 @@
1
+ {
2
+ "main": "../dist/cjs/HttpHandler.js",
3
+ "module": "../dist/esm/HttpHandler.js",
4
+ "types": "../dist/dts/HttpHandler.d.ts",
5
+ "sideEffects": []
6
+ }
package/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2025 Victor Korzunin
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # @effect-aws/http-handler
2
+
3
+ [![npm version](https://img.shields.io/npm/v/%40effect-aws%2Fhttp-handler?color=brightgreen&label=npm%20package)](https://www.npmjs.com/package/@effect-aws/http-handler)
4
+ [![npm downloads](https://img.shields.io/npm/dm/%40effect-aws%2Fhttp-handler)](https://www.npmjs.com/package/@effect-aws/http-handler)
5
+
6
+ This package provides the effectful `HttpHandler` implementation for the AWS SDK v3, allowing developers to control HttpClient request handler usage in effect platform way for better tracing and error handling.
7
+
8
+ ## Installation
9
+
10
+ ```bash
11
+ npm install --save @effect-aws/http-handler
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ ```typescript
17
+ import { S3 } from "@effect-aws/client-s3"
18
+ import { HttpHandler } from "@effect-aws/http-handler"
19
+ import { FetchHttpClient } from "@effect/platform"
20
+
21
+ const program = S3.headObject(args)
22
+
23
+ const result = pipe(
24
+ program,
25
+ Effect.provide(S3.defaultLayer),
26
+ Effect.provide(HttpHandler.layer({ requestTimeout: 1000 })),
27
+ Effect.provide(FetchHttpClient.layer),
28
+ Effect.runPromise
29
+ )
30
+ ```
@@ -0,0 +1,12 @@
1
+ /**
2
+ * @since 0.1.0
3
+ */
4
+ import type { HttpHandlerOptions } from "@effect-aws/commons";
5
+ import { HttpHandler } from "@effect-aws/commons";
6
+ import { Layer } from "effect";
7
+ /**
8
+ * @since 0.1.0
9
+ * @category layers
10
+ */
11
+ export declare const layer: (config?: HttpHandlerOptions) => Layer.Layer<HttpHandler.RequestHandler, never, import("@effect/platform/HttpClient").HttpClient>;
12
+ //# sourceMappingURL=HttpHandler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"HttpHandler.d.ts","sourceRoot":"","sources":["../../src/HttpHandler.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAC9D,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAC;AAG/B;;;GAGG;AACH,eAAO,MAAM,KAAK,YAAa,kBAAkB,qGACqC,CAAC"}
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.layer = void 0;
4
+ const commons_1 = require("@effect-aws/commons");
5
+ const effect_1 = require("effect");
6
+ const httpHandler_js_1 = require("./internal/httpHandler.js");
7
+ /**
8
+ * @since 0.1.0
9
+ * @category layers
10
+ */
11
+ const layer = (config) => effect_1.Layer.effect(commons_1.HttpHandler.RequestHandler, (0, httpHandler_js_1.makeHttpClientRequestHandler)(config ?? {}));
12
+ exports.layer = layer;
13
+ //# sourceMappingURL=HttpHandler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"HttpHandler.js","sourceRoot":"","sources":["../../src/HttpHandler.ts"],"names":[],"mappings":";;;AAIA,iDAAkD;AAClD,mCAA+B;AAC/B,8DAAyE;AAEzE;;;GAGG;AACI,MAAM,KAAK,GAAG,CAAC,MAA2B,EAAE,EAAE,CACnD,cAAK,CAAC,MAAM,CAAC,qBAAW,CAAC,cAAc,EAAE,IAAA,6CAA4B,EAAC,MAAM,IAAI,EAAE,CAAC,CAAC,CAAC;AAD1E,QAAA,KAAK,SACqE"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * @since 0.1.0
3
+ */
4
+ export * as HttpHandler from "./HttpHandler.js";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,WAAW,MAAM,kBAAkB,CAAC"}
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.HttpHandler = void 0;
27
+ /**
28
+ * @since 0.1.0
29
+ */
30
+ exports.HttpHandler = __importStar(require("./HttpHandler.js"));
31
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;GAEG;AACH,gEAAgD"}
@@ -0,0 +1,15 @@
1
+ declare module "@effect/platform/HttpClientResponse" {
2
+ interface HttpClientResponse {
3
+ /**
4
+ * @private
5
+ */
6
+ source: globalThis.Response;
7
+ /**
8
+ * @private
9
+ * @since 0.79.0
10
+ */
11
+ original?: HttpClientResponse;
12
+ }
13
+ }
14
+ export {};
15
+ //# sourceMappingURL=httpHandler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"httpHandler.d.ts","sourceRoot":"","sources":["../../../src/internal/httpHandler.ts"],"names":[],"mappings":"AAWA,OAAO,QAAQ,qCAAqC,CAAC;IACnD,UAAU,kBAAkB;QAC1B;;WAEG;QACH,MAAM,EAAE,UAAU,CAAC,QAAQ,CAAC;QAE5B;;;WAGG;QACH,QAAQ,CAAC,EAAE,kBAAkB,CAAC;KAC/B;CACF"}
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.makeHttpClientRequestHandler = void 0;
4
+ const commons_1 = require("@effect-aws/commons");
5
+ const platform_1 = require("@effect/platform");
6
+ const protocol_http_1 = require("@smithy/protocol-http");
7
+ const querystring_builder_1 = require("@smithy/querystring-builder");
8
+ const effect_1 = require("effect");
9
+ const makeUrl = (request) => {
10
+ let path = request.path;
11
+ const queryString = (0, querystring_builder_1.buildQueryString)(request.query || {});
12
+ if (queryString) {
13
+ path += `?${queryString}`;
14
+ }
15
+ if (request.fragment) {
16
+ path += `#${request.fragment}`;
17
+ }
18
+ let auth = "";
19
+ if (request.username != null || request.password != null) {
20
+ const username = request.username ?? "";
21
+ const password = request.password ?? "";
22
+ auth = `${username}:${password}@`;
23
+ }
24
+ return `${request.protocol}//${auth}${request.hostname}${request.port ? `:${request.port}` : ""}${path}`;
25
+ };
26
+ const tryToReadableStream = (stream) => effect_1.Stream.peel(stream, effect_1.Sink.head()).pipe(effect_1.Effect.map(([head, tailStream]) => effect_1.Option.isNone(head) ? effect_1.Stream.empty : effect_1.Stream.merge(effect_1.Stream.make(head.value), tailStream)), effect_1.Effect.flatMap(effect_1.Stream.toReadableStreamEffect()));
27
+ /** @internal */
28
+ const makeHttpClientRequestHandler = (config) => effect_1.Effect.gen(function* () {
29
+ const client = yield* platform_1.HttpClient.HttpClient;
30
+ return commons_1.HttpHandler.make({
31
+ handle: (request, handlerOptions) => effect_1.Effect.gen(function* () {
32
+ const requestTimeoutInMs = effect_1.Option.fromNullable(handlerOptions?.requestTimeout ?? config.requestTimeout).pipe(effect_1.Option.map(effect_1.Duration.millis), effect_1.Option.getOrElse(() => effect_1.Duration.infinity));
33
+ const url = makeUrl(request);
34
+ // Request constructor doesn't allow GET/HEAD request with body
35
+ // ref: https://github.com/whatwg/fetch/issues/551
36
+ const body = request.method === "GET" || request.method === "HEAD"
37
+ ? platform_1.HttpBody.empty
38
+ : platform_1.HttpBody.raw(request.body);
39
+ const fetch = platform_1.HttpClientRequest.make(request.method);
40
+ const httpRequest = fetch(url, { body, headers: request.headers });
41
+ const response = yield* client.execute(httpRequest).pipe(effect_1.Effect.flatMap((res) => tryToReadableStream(res.stream).pipe(effect_1.Effect.catchTag("ResponseError", (error) => error.reason === "EmptyBody" ? res.arrayBuffer : effect_1.Effect.fail(error)), effect_1.Effect.map((body) => new protocol_http_1.HttpResponse({
42
+ headers: res.headers,
43
+ reason: (res.original ?? res).source.statusText, // changed since @effect/platform@0.79.0
44
+ statusCode: res.status,
45
+ body,
46
+ })))), effect_1.Effect.orDie, effect_1.Effect.timeout(requestTimeoutInMs));
47
+ return { response };
48
+ }),
49
+ });
50
+ });
51
+ exports.makeHttpClientRequestHandler = makeHttpClientRequestHandler;
52
+ //# sourceMappingURL=httpHandler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"httpHandler.js","sourceRoot":"","sources":["../../../src/internal/httpHandler.ts"],"names":[],"mappings":";;;AACA,iDAAkD;AAElD,+CAA2E;AAE3E,yDAAqD;AACrD,qEAA+D;AAG/D,mCAAgE;AAiBhE,MAAM,OAAO,GAAG,CAAC,OAAoB,EAAU,EAAE;IAC/C,IAAI,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IACxB,MAAM,WAAW,GAAG,IAAA,sCAAgB,EAAC,OAAO,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC;IAC1D,IAAI,WAAW,EAAE,CAAC;QAChB,IAAI,IAAI,IAAI,WAAW,EAAE,CAAC;IAC5B,CAAC;IACD,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC;QACrB,IAAI,IAAI,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC;IACjC,CAAC;IAED,IAAI,IAAI,GAAG,EAAE,CAAC;IACd,IAAI,OAAO,CAAC,QAAQ,IAAI,IAAI,IAAI,OAAO,CAAC,QAAQ,IAAI,IAAI,EAAE,CAAC;QACzD,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,EAAE,CAAC;QACxC,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,EAAE,CAAC;QACxC,IAAI,GAAG,GAAG,QAAQ,IAAI,QAAQ,GAAG,CAAC;IACpC,CAAC;IAED,OAAO,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Z,eAAM,CAAC,OAAO,CAAC,kBAAkB,CAAC,CACnC,CAAC;YAEF,OAAO,EAAE,QAAQ,EAAE,CAAC;QACtB,CAAC,CAAC;KACL,CAAC,CAAC;AACL,CAAC,CAAC,CAAC;AArDQ,QAAA,4BAA4B,gCAqDpC"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * @since 0.1.0
3
+ */
4
+ import type { HttpHandlerOptions } from "@effect-aws/commons";
5
+ import { HttpHandler } from "@effect-aws/commons";
6
+ import { Layer } from "effect";
7
+ /**
8
+ * @since 0.1.0
9
+ * @category layers
10
+ */
11
+ export declare const layer: (config?: HttpHandlerOptions) => Layer.Layer<HttpHandler.RequestHandler, never, import("@effect/platform/HttpClient").HttpClient>;
12
+ //# sourceMappingURL=HttpHandler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"HttpHandler.d.ts","sourceRoot":"","sources":["../../src/HttpHandler.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAC9D,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAC;AAG/B;;;GAGG;AACH,eAAO,MAAM,KAAK,YAAa,kBAAkB,qGACqC,CAAC"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * @since 0.1.0
3
+ */
4
+ export * as HttpHandler from "./HttpHandler.js";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,WAAW,MAAM,kBAAkB,CAAC"}
@@ -0,0 +1,15 @@
1
+ declare module "@effect/platform/HttpClientResponse" {
2
+ interface HttpClientResponse {
3
+ /**
4
+ * @private
5
+ */
6
+ source: globalThis.Response;
7
+ /**
8
+ * @private
9
+ * @since 0.79.0
10
+ */
11
+ original?: HttpClientResponse;
12
+ }
13
+ }
14
+ export {};
15
+ //# sourceMappingURL=httpHandler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"httpHandler.d.ts","sourceRoot":"","sources":["../../../src/internal/httpHandler.ts"],"names":[],"mappings":"AAWA,OAAO,QAAQ,qCAAqC,CAAC;IACnD,UAAU,kBAAkB;QAC1B;;WAEG;QACH,MAAM,EAAE,UAAU,CAAC,QAAQ,CAAC;QAE5B;;;WAGG;QACH,QAAQ,CAAC,EAAE,kBAAkB,CAAC;KAC/B;CACF"}
@@ -0,0 +1,9 @@
1
+ import { HttpHandler } from "@effect-aws/commons";
2
+ import { Layer } from "effect";
3
+ import { makeHttpClientRequestHandler } from "./internal/httpHandler.js";
4
+ /**
5
+ * @since 0.1.0
6
+ * @category layers
7
+ */
8
+ export const layer = (config) => Layer.effect(HttpHandler.RequestHandler, makeHttpClientRequestHandler(config ?? {}));
9
+ //# sourceMappingURL=HttpHandler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"HttpHandler.js","sourceRoot":"","sources":["../../src/HttpHandler.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAC;AAC/B,OAAO,EAAE,4BAA4B,EAAE,MAAM,2BAA2B,CAAC;AAEzE;;;GAGG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,CAAC,MAA2B,EAAE,EAAE,CACnD,KAAK,CAAC,MAAM,CAAC,WAAW,CAAC,cAAc,EAAE,4BAA4B,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,CAAC"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * @since 0.1.0
3
+ */
4
+ export * as HttpHandler from "./HttpHandler.js";
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,WAAW,MAAM,kBAAkB,CAAC"}
@@ -0,0 +1,48 @@
1
+ import { HttpHandler } from "@effect-aws/commons";
2
+ import { HttpBody, HttpClient, HttpClientRequest } from "@effect/platform";
3
+ import { HttpResponse } from "@smithy/protocol-http";
4
+ import { buildQueryString } from "@smithy/querystring-builder";
5
+ import { Duration, Effect, Option, Sink, Stream } from "effect";
6
+ const makeUrl = (request) => {
7
+ let path = request.path;
8
+ const queryString = buildQueryString(request.query || {});
9
+ if (queryString) {
10
+ path += `?${queryString}`;
11
+ }
12
+ if (request.fragment) {
13
+ path += `#${request.fragment}`;
14
+ }
15
+ let auth = "";
16
+ if (request.username != null || request.password != null) {
17
+ const username = request.username ?? "";
18
+ const password = request.password ?? "";
19
+ auth = `${username}:${password}@`;
20
+ }
21
+ return `${request.protocol}//${auth}${request.hostname}${request.port ? `:${request.port}` : ""}${path}`;
22
+ };
23
+ const tryToReadableStream = (stream) => Stream.peel(stream, Sink.head()).pipe(Effect.map(([head, tailStream]) => Option.isNone(head) ? Stream.empty : Stream.merge(Stream.make(head.value), tailStream)), Effect.flatMap(Stream.toReadableStreamEffect()));
24
+ /** @internal */
25
+ export const makeHttpClientRequestHandler = (config) => Effect.gen(function* () {
26
+ const client = yield* HttpClient.HttpClient;
27
+ return HttpHandler.make({
28
+ handle: (request, handlerOptions) => Effect.gen(function* () {
29
+ const requestTimeoutInMs = Option.fromNullable(handlerOptions?.requestTimeout ?? config.requestTimeout).pipe(Option.map(Duration.millis), Option.getOrElse(() => Duration.infinity));
30
+ const url = makeUrl(request);
31
+ // Request constructor doesn't allow GET/HEAD request with body
32
+ // ref: https://github.com/whatwg/fetch/issues/551
33
+ const body = request.method === "GET" || request.method === "HEAD"
34
+ ? HttpBody.empty
35
+ : HttpBody.raw(request.body);
36
+ const fetch = HttpClientRequest.make(request.method);
37
+ const httpRequest = fetch(url, { body, headers: request.headers });
38
+ const response = yield* client.execute(httpRequest).pipe(Effect.flatMap((res) => tryToReadableStream(res.stream).pipe(Effect.catchTag("ResponseError", (error) => error.reason === "EmptyBody" ? res.arrayBuffer : Effect.fail(error)), Effect.map((body) => new HttpResponse({
39
+ headers: res.headers,
40
+ reason: (res.original ?? res).source.statusText, // changed since @effect/platform@0.79.0
41
+ statusCode: res.status,
42
+ body,
43
+ })))), Effect.orDie, Effect.timeout(requestTimeoutInMs));
44
+ return { response };
45
+ }),
46
+ });
47
+ });
48
+ //# sourceMappingURL=httpHandler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"httpHandler.js","sourceRoot":"","sources":["../../../src/internal/httpHandler.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAElD,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAE3E,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AACrD,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAG/D,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAiBhE,MAAM,OAAO,GAAG,CAAC,OAAoB,EAAU,EAAE;IAC/C,IAAI,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IACxB,MAAM,WAAW,GAAG,gBAAgB,CAAC,OAAO,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC;IAC1D,IAAI,WAAW,EAAE,CAAC;QAChB,IAAI,IAAI,IAAI,WAAW,EAAE,CAAC;IAC5B,CAAC;IACD,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC;QACrB,IAAI,IAAI,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC;IACjC,CAAC;IAED,IAAI,IAAI,GAAG,EAAE,CAAC;IACd,IAAI,OAAO,CAAC,QAAQ,IAAI,IAAI,IAAI,OAAO,CAAC,QAAQ,IAAI,IAAI,EAAE,CA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AAC,OAAO;gBACpB,MAAM,EAAE,CAAC,GAAG,CAAC,QAAQ,IAAI,GAAG,CAAC,CAAC,MAAM,CAAC,UAAU,EAAE,wCAAwC;gBACzF,UAAU,EAAE,GAAG,CAAC,MAAM;gBACtB,IAAI;aACL,CAAC,CACH,CACF,CACF,EACD,MAAM,CAAC,KAAK,EACZ,MAAM,CAAC,OAAO,CAAC,kBAAkB,CAAC,CACnC,CAAC;YAEF,OAAO,EAAE,QAAQ,EAAE,CAAC;QACtB,CAAC,CAAC;KACL,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -0,0 +1,4 @@
1
+ {
2
+ "type": "module",
3
+ "sideEffects": []
4
+ }
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@effect-aws/http-handler",
3
+ "version": "0.1.0",
4
+ "description": "Effectful AWS HTTP handler",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "github:floydspace/effect-aws",
9
+ "directory": "packages/http-handler"
10
+ },
11
+ "sideEffects": [],
12
+ "author": {
13
+ "name": "Victor Korzunin",
14
+ "email": "ifloydrose@gmail.com"
15
+ },
16
+ "homepage": "https://floydspace.github.io/effect-aws/docs/http-handler",
17
+ "dependencies": {
18
+ "@smithy/protocol-http": "^5.1.0",
19
+ "@smithy/querystring-builder": "^4.0.2",
20
+ "@smithy/types": "^4.2.0"
21
+ },
22
+ "peerDependencies": {
23
+ "@effect/platform": ">=0.69.25",
24
+ "effect": ">=3.0.4 <4.0.0",
25
+ "@effect-aws/commons": "^0.3.0"
26
+ },
27
+ "main": "./dist/cjs/index.js",
28
+ "module": "./dist/esm/index.js",
29
+ "types": "./dist/dts/index.d.ts",
30
+ "exports": {
31
+ "./package.json": "./package.json",
32
+ ".": {
33
+ "types": "./dist/dts/index.d.ts",
34
+ "import": "./dist/esm/index.js",
35
+ "default": "./dist/cjs/index.js"
36
+ },
37
+ "./HttpHandler": {
38
+ "types": "./dist/dts/HttpHandler.d.ts",
39
+ "import": "./dist/esm/HttpHandler.js",
40
+ "default": "./dist/cjs/HttpHandler.js"
41
+ }
42
+ },
43
+ "typesVersions": {
44
+ "*": {
45
+ "HttpHandler": [
46
+ "./dist/dts/HttpHandler.d.ts"
47
+ ]
48
+ }
49
+ }
50
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * @since 0.1.0
3
+ */
4
+ import type { HttpHandlerOptions } from "@effect-aws/commons";
5
+ import { HttpHandler } from "@effect-aws/commons";
6
+ import { Layer } from "effect";
7
+ import { makeHttpClientRequestHandler } from "./internal/httpHandler.js";
8
+
9
+ /**
10
+ * @since 0.1.0
11
+ * @category layers
12
+ */
13
+ export const layer = (config?: HttpHandlerOptions) =>
14
+ Layer.effect(HttpHandler.RequestHandler, makeHttpClientRequestHandler(config ?? {}));
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ /**
2
+ * @since 0.1.0
3
+ */
4
+ export * as HttpHandler from "./HttpHandler.js";
@@ -0,0 +1,109 @@
1
+ import type { HttpHandlerOptions } from "@effect-aws/commons";
2
+ import { HttpHandler } from "@effect-aws/commons";
3
+ import type { HttpMethod } from "@effect/platform";
4
+ import { HttpBody, HttpClient, HttpClientRequest } from "@effect/platform";
5
+ import type { HttpRequest } from "@smithy/protocol-http";
6
+ import { HttpResponse } from "@smithy/protocol-http";
7
+ import { buildQueryString } from "@smithy/querystring-builder";
8
+ import type { RequestHandlerOutput } from "@smithy/types";
9
+ import type { Cause, Scope } from "effect";
10
+ import { Duration, Effect, Option, Sink, Stream } from "effect";
11
+
12
+ declare module "@effect/platform/HttpClientResponse" {
13
+ interface HttpClientResponse {
14
+ /**
15
+ * @private
16
+ */
17
+ source: globalThis.Response;
18
+
19
+ /**
20
+ * @private
21
+ * @since 0.79.0
22
+ */
23
+ original?: HttpClientResponse;
24
+ }
25
+ }
26
+
27
+ const makeUrl = (request: HttpRequest): string => {
28
+ let path = request.path;
29
+ const queryString = buildQueryString(request.query || {});
30
+ if (queryString) {
31
+ path += `?${queryString}`;
32
+ }
33
+ if (request.fragment) {
34
+ path += `#${request.fragment}`;
35
+ }
36
+
37
+ let auth = "";
38
+ if (request.username != null || request.password != null) {
39
+ const username = request.username ?? "";
40
+ const password = request.password ?? "";
41
+ auth = `${username}:${password}@`;
42
+ }
43
+
44
+ return `${request.protocol}//${auth}${request.hostname}${request.port ? `:${request.port}` : ""}${path}`;
45
+ };
46
+
47
+ const tryToReadableStream = <A, E = never, R = never>(stream: Stream.Stream<A, E, R>) =>
48
+ Stream.peel(stream, Sink.head()).pipe(
49
+ Effect.map(([head, tailStream]) =>
50
+ Option.isNone(head) ? Stream.empty : Stream.merge(Stream.make(head.value), tailStream)
51
+ ),
52
+ Effect.flatMap(Stream.toReadableStreamEffect()),
53
+ );
54
+
55
+ /** @internal */
56
+ export const makeHttpClientRequestHandler = (config: HttpHandlerOptions) =>
57
+ Effect.gen(function*() {
58
+ const client = yield* HttpClient.HttpClient;
59
+
60
+ return HttpHandler.make({
61
+ handle: (
62
+ request: HttpRequest,
63
+ handlerOptions?: HttpHandlerOptions,
64
+ ): Effect.Effect<
65
+ RequestHandlerOutput<HttpResponse>,
66
+ Cause.TimeoutException,
67
+ Scope.Scope
68
+ > =>
69
+ Effect.gen(function*() {
70
+ const requestTimeoutInMs = Option.fromNullable(handlerOptions?.requestTimeout ?? config.requestTimeout).pipe(
71
+ Option.map(Duration.millis),
72
+ Option.getOrElse(() => Duration.infinity),
73
+ );
74
+
75
+ const url = makeUrl(request);
76
+ // Request constructor doesn't allow GET/HEAD request with body
77
+ // ref: https://github.com/whatwg/fetch/issues/551
78
+ const body = request.method === "GET" || request.method === "HEAD"
79
+ ? HttpBody.empty
80
+ : HttpBody.raw(request.body);
81
+
82
+ const fetch = HttpClientRequest.make(request.method as HttpMethod.HttpMethod);
83
+ const httpRequest = fetch(url, { body, headers: request.headers });
84
+
85
+ const response = yield* client.execute(httpRequest).pipe(
86
+ Effect.flatMap((res) =>
87
+ tryToReadableStream(res.stream).pipe(
88
+ Effect.catchTag(
89
+ "ResponseError",
90
+ (error) => error.reason === "EmptyBody" ? res.arrayBuffer : Effect.fail(error),
91
+ ),
92
+ Effect.map((body) =>
93
+ new HttpResponse({
94
+ headers: res.headers,
95
+ reason: (res.original ?? res).source.statusText, // changed since @effect/platform@0.79.0
96
+ statusCode: res.status,
97
+ body,
98
+ })
99
+ ),
100
+ )
101
+ ),
102
+ Effect.orDie,
103
+ Effect.timeout(requestTimeoutInMs),
104
+ );
105
+
106
+ return { response };
107
+ }),
108
+ });
109
+ });