Skip to content

chore: feature detection for gzip and endpoint override #1429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/silent-rockets-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@smithy/middleware-compression": minor
"@smithy/middleware-endpoint": minor
"@smithy/core": minor
---

feature detection for custom endpoint and gzip
8 changes: 8 additions & 0 deletions .changeset/unlucky-books-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@smithy/middleware-compression": patch
"@smithy/middleware-endpoint": patch
"@smithy/smithy-client": patch
"@smithy/core": patch
---

reorganize smithy/core to be upstream of smithy/smithy-client
13 changes: 10 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
"import": "./dist-es/submodules/cbor/index.js",
"require": "./dist-cjs/submodules/cbor/index.js",
"types": "./dist-types/submodules/cbor/index.d.ts"
},
"./protocols": {
"module": "./dist-es/submodules/protocols/index.js",
"node": "./dist-cjs/submodules/protocols/index.js",
"import": "./dist-es/submodules/protocols/index.js",
"require": "./dist-cjs/submodules/protocols/index.js",
"types": "./dist-types/submodules/protocols/index.d.ts"
}
},
"author": {
Expand All @@ -46,14 +53,12 @@
},
"license": "Apache-2.0",
"dependencies": {
"@smithy/middleware-endpoint": "workspace:^",
"@smithy/middleware-retry": "workspace:^",
"@smithy/middleware-serde": "workspace:^",
"@smithy/protocol-http": "workspace:^",
"@smithy/smithy-client": "workspace:^",
"@smithy/types": "workspace:^",
"@smithy/util-body-length-browser": "workspace:^",
"@smithy/util-middleware": "workspace:^",
"@smithy/util-stream": "workspace:^",
"@smithy/util-utf8": "workspace:^",
"tslib": "^2.6.2"
},
Expand All @@ -70,6 +75,8 @@
"files": [
"./cbor.d.ts",
"./cbor.js",
"./protocols.d.ts",
"./protocols.js",
"dist-*/**"
],
"homepage": "https://github.com/awslabs/smithy-typescript/tree/main/packages/core",
Expand Down
7 changes: 7 additions & 0 deletions packages/core/protocols.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Do not edit:
* This is a compatibility redirect for contexts that do not understand package.json exports field.
*/
declare module "@smithy/core/protocols" {
export * from "@smithy/core/dist-types/submodules/protocols/index.d";
}
6 changes: 6 additions & 0 deletions packages/core/protocols.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

/**
* Do not edit:
* This is a compatibility redirect for contexts that do not understand package.json exports field.
*/
module.exports = require("./dist-cjs/submodules/protocols/index.js");
7 changes: 4 additions & 3 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export * from "./getSmithyContext";
export * from "./middleware-http-auth-scheme";
export * from "./middleware-http-signing";
export * from "./util-identity-and-auth";
export * from "./getSmithyContext";
export * from "./normalizeProvider";
export * from "./protocols/requestBuilder";
export { createPaginator } from "./pagination/createPaginator";
export * from "./protocols/requestBuilder";
export * from "./setFeature";
export * from "./util-identity-and-auth";
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { endpointMiddlewareOptions } from "@smithy/middleware-endpoint";
import {
HandlerExecutionContext,
HttpAuthSchemeParameters,
Expand All @@ -20,7 +19,7 @@ export const httpAuthSchemeEndpointRuleSetMiddlewareOptions: SerializeHandlerOpt
name: "httpAuthSchemeMiddleware",
override: true,
relation: "before",
toMiddleware: endpointMiddlewareOptions.name!,
toMiddleware: "endpointV2Middleware",
};

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { retryMiddlewareOptions } from "@smithy/middleware-retry";
import { FinalizeRequestHandlerOptions, Pluggable, RelativeMiddlewareOptions } from "@smithy/types";

import { httpSigningMiddleware } from "./httpSigningMiddleware";
Expand All @@ -13,7 +12,7 @@ export const httpSigningMiddlewareOptions: FinalizeRequestHandlerOptions & Relat
aliases: ["apiKeyMiddleware", "tokenMiddleware", "awsAuthMiddleware"],
override: true,
relation: "after",
toMiddleware: retryMiddlewareOptions.name!,
toMiddleware: "retryMiddleware",
};

/**
Expand Down
107 changes: 2 additions & 105 deletions packages/core/src/protocols/requestBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,108 +1,5 @@
import { HttpRequest } from "@smithy/protocol-http";
import { resolvedPath } from "@smithy/smithy-client";
import type { SerdeContext } from "@smithy/types";

/**
* @internal
* used in code-generated serde.
* Backwards compatibility re-export.
*/
export function requestBuilder(input: any, context: SerdeContext): RequestBuilder {
return new RequestBuilder(input, context);
}

/**
* @internal
*/
export class RequestBuilder {
private query: Record<string, string> = {};
private method = "";
private headers: Record<string, string> = {};
private path = "";
private body: any = null;
private hostname = "";

private resolvePathStack: Array<(path: string) => void> = [];

public constructor(
private input: any,
private context: SerdeContext
) {}

public async build() {
const { hostname, protocol = "https", port, path: basePath } = await this.context.endpoint();
this.path = basePath;
for (const resolvePath of this.resolvePathStack) {
resolvePath(this.path);
}
return new HttpRequest({
protocol,
hostname: this.hostname || hostname,
port,
method: this.method,
path: this.path,
query: this.query,
body: this.body,
headers: this.headers,
});
}

/**
* Brevity setter for "hostname".
*/
public hn(hostname: string) {
this.hostname = hostname;
return this;
}

/**
* Brevity initial builder for "basepath".
*/
public bp(uriLabel: string) {
this.resolvePathStack.push((basePath: string) => {
this.path = `${basePath?.endsWith("/") ? basePath.slice(0, -1) : basePath || ""}` + uriLabel;
});
return this;
}

/**
* Brevity incremental builder for "path".
*/
public p(memberName: string, labelValueProvider: () => string | undefined, uriLabel: string, isGreedyLabel: boolean) {
this.resolvePathStack.push((path: string) => {
this.path = resolvedPath(path, this.input, memberName, labelValueProvider, uriLabel, isGreedyLabel);
});
return this;
}

/**
* Brevity setter for "headers".
*/
public h(headers: Record<string, string>) {
this.headers = headers;
return this;
}

/**
* Brevity setter for "query".
*/
public q(query: Record<string, string>) {
this.query = query;
return this;
}

/**
* Brevity setter for "body".
*/
public b(body: any) {
this.body = body;
return this;
}

/**
* Brevity setter for "method".
*/
public m(method: string) {
this.method = method;
return this;
}
}
export { requestBuilder } from "@smithy/core/protocols";
2 changes: 1 addition & 1 deletion packages/core/src/submodules/cbor/parseCborBody.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { collectBody } from "@smithy/core/protocols";
import { HttpRequest as __HttpRequest } from "@smithy/protocol-http";
import { collectBody } from "@smithy/smithy-client";
import { HeaderBag as __HeaderBag, HttpResponse, SerdeContext as __SerdeContext, SerdeContext } from "@smithy/types";
import { calculateBodyLength } from "@smithy/util-body-length-browser";

Expand Down
26 changes: 26 additions & 0 deletions packages/core/src/submodules/protocols/collect-stream-body.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { SerdeContext } from "@smithy/types";
import { Uint8ArrayBlobAdapter } from "@smithy/util-stream";

/**
* @internal
*
* Collect low-level response body stream to Uint8Array.
*/
export const collectBody = async (
streamBody: any = new Uint8Array(),
context: {
streamCollector: SerdeContext["streamCollector"];
}
): Promise<Uint8ArrayBlobAdapter> => {
if (streamBody instanceof Uint8Array) {
return Uint8ArrayBlobAdapter.mutate(streamBody);
}

if (!streamBody) {
return Uint8ArrayBlobAdapter.mutate(new Uint8Array());
}

const fromContext = context.streamCollector(streamBody);

return Uint8ArrayBlobAdapter.mutate(await fromContext);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @internal
*
* Function that wraps encodeURIComponent to encode additional characters
* to fully adhere to RFC 3986.
*/
export function extendedEncodeURIComponent(str: string): string {
return encodeURIComponent(str).replace(/[!'()*]/g, function (c) {
return "%" + c.charCodeAt(0).toString(16).toUpperCase();
});
}
4 changes: 4 additions & 0 deletions packages/core/src/submodules/protocols/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "./collect-stream-body";
export * from "./extended-encode-uri-component";
export * from "./requestBuilder";
export * from "./resolve-path";
108 changes: 108 additions & 0 deletions packages/core/src/submodules/protocols/requestBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { resolvedPath } from "@smithy/core/protocols";
import { HttpRequest } from "@smithy/protocol-http";
import type { SerdeContext } from "@smithy/types";

/**
* @internal
* used in code-generated serde.
*/
export function requestBuilder(input: any, context: SerdeContext): RequestBuilder {
return new RequestBuilder(input, context);
}

/**
* @internal
*/
export class RequestBuilder {
private query: Record<string, string> = {};
private method = "";
private headers: Record<string, string> = {};
private path = "";
private body: any = null;
private hostname = "";

private resolvePathStack: Array<(path: string) => void> = [];

public constructor(
private input: any,
private context: SerdeContext
) {}

public async build() {
const { hostname, protocol = "https", port, path: basePath } = await this.context.endpoint();
this.path = basePath;
for (const resolvePath of this.resolvePathStack) {
resolvePath(this.path);
}
return new HttpRequest({
protocol,
hostname: this.hostname || hostname,
port,
method: this.method,
path: this.path,
query: this.query,
body: this.body,
headers: this.headers,
});
}

/**
* Brevity setter for "hostname".
*/
public hn(hostname: string) {
this.hostname = hostname;
return this;
}

/**
* Brevity initial builder for "basepath".
*/
public bp(uriLabel: string) {
this.resolvePathStack.push((basePath: string) => {
this.path = `${basePath?.endsWith("/") ? basePath.slice(0, -1) : basePath || ""}` + uriLabel;
});
return this;
}

/**
* Brevity incremental builder for "path".
*/
public p(memberName: string, labelValueProvider: () => string | undefined, uriLabel: string, isGreedyLabel: boolean) {
this.resolvePathStack.push((path: string) => {
this.path = resolvedPath(path, this.input, memberName, labelValueProvider, uriLabel, isGreedyLabel);
});
return this;
}

/**
* Brevity setter for "headers".
*/
public h(headers: Record<string, string>) {
this.headers = headers;
return this;
}

/**
* Brevity setter for "query".
*/
public q(query: Record<string, string>) {
this.query = query;
return this;
}

/**
* Brevity setter for "body".
*/
public b(body: any) {
this.body = body;
return this;
}

/**
* Brevity setter for "method".
*/
public m(method: string) {
this.method = method;
return this;
}
}
Loading
Loading