Skip to content

Commit

Permalink
address pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe committed Jun 24, 2024
1 parent e7d7414 commit ca3fc95
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 69 deletions.
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@smithy/protocol-http": "workspace:^",
"@smithy/smithy-client": "workspace:^",
"@smithy/types": "workspace:^",
"@smithy/util-body-length-browser": "workspace:^",
"@smithy/util-middleware": "workspace:^",
"@smithy/util-utf8": "workspace:^",
"tslib": "^2.6.2"
Expand Down
10 changes: 8 additions & 2 deletions packages/core/src/submodules/cbor/cbor-decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ function bytesToUtf8(bytes: Uint8Array, at: number, to: number): string {
}

function demote(bigInteger: bigint): number {
// cast is safe for string and array lengths, which do not
// exceed safe integer range.
const num = Number(bigInteger);
if (num < Number.MIN_SAFE_INTEGER || Number.MAX_SAFE_INTEGER < num) {
console.warn(new Error(`@smithy/core/cbor - truncating BigInt(${bigInteger}) to ${num} with loss of precision.`));
Expand Down Expand Up @@ -297,7 +299,9 @@ function decodeUtf8StringIndefinite(at: Uint32, to: Uint32): string {
const bytes = decodeUnstructuredByteString(at, to);
const length = _offset;
at += length;
vector.push(...bytes);
for (let i = 0; i < bytes.length; ++i) {
vector.push(bytes[i]);
}
}
throw new Error("expected break marker.");
}
Expand Down Expand Up @@ -340,7 +344,9 @@ function decodeUnstructuredByteStringIndefinite(at: Uint32, to: Uint32): CborUns
const bytes = decodeUnstructuredByteString(at, to);
const length = _offset;
at += length;
vector.push(...bytes);
for (let i = 0; i < bytes.length; ++i) {
vector.push(bytes[i]);
}
}
throw new Error("expected break marker.");
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/submodules/cbor/cbor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const successTests = JSONbig({ useNativeBigInt: true, alwaysParseAsBig: false })
describe("cbor", () => {
const allocByteArray = (dataOrSize: ArrayBuffer | ArrayLike<number> | number, offset?: number, length?: number) => {
if (typeof offset === "number" && typeof length === "number") {
typeof Buffer !== "undefined"
return typeof Buffer !== "undefined"
? Buffer.from(dataOrSize as ArrayBuffer, offset, length)
: new Uint8Array(dataOrSize as ArrayBuffer, offset, length);
}
Expand Down
40 changes: 38 additions & 2 deletions packages/core/src/submodules/cbor/parseCborBody.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { HttpRequest as __HttpRequest } from "@smithy/protocol-http";
import { collectBody } from "@smithy/smithy-client";
import type { HttpResponse, SerdeContext } from "@smithy/types";
import { HeaderBag as __HeaderBag, HttpResponse, SerdeContext as __SerdeContext, SerdeContext } from "@smithy/types";
import { calculateBodyLength } from "@smithy/util-body-length-browser";

import { cbor } from "./cbor";

Expand Down Expand Up @@ -71,8 +73,42 @@ export const loadSmithyRpcV2CborErrorCode = (output: HttpResponse, data: any): s
}
};

/**
* @internal
*/
export const checkCborResponse = (response: HttpResponse): void => {
if (response.headers["smithy-protocol"] !== "rpc-v2-cbor") {
if (String(response.headers["smithy-protocol"]).toLowerCase() !== "rpc-v2-cbor") {
throw new Error("Malformed RPCv2 CBOR response, status: " + response.statusCode);
}
};

/**
* @internal
*/
export const buildHttpRpcRequest = async (
context: __SerdeContext,
headers: __HeaderBag,
path: string,
resolvedHostname: string | undefined,
body: any
): Promise<__HttpRequest> => {
const { hostname, protocol = "https", port, path: basePath } = await context.endpoint();
const contents: any = {
protocol,
hostname,
port,
method: "POST",
path: basePath.endsWith("/") ? basePath.slice(0, -1) + path : basePath + path,
headers,
};
if (resolvedHostname !== undefined) {
contents.hostname = resolvedHostname;
}
if (body !== undefined) {
contents.body = body;
try {
contents.headers["content-length"] = String(calculateBodyLength(body));
} catch (e) {}
}
return new __HttpRequest(contents);
};
29 changes: 1 addition & 28 deletions private/smithy-rpcv2-cbor/src/protocols/Rpcv2cbor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
} from "../models/models_0";
import {
dateToTag as __dateToTag,
buildHttpRpcRequest,
cbor,
checkCborResponse as cr,
loadSmithyRpcV2CborErrorCode,
Expand Down Expand Up @@ -83,7 +84,6 @@ import {
ResponseMetadata as __ResponseMetadata,
SerdeContext as __SerdeContext,
} from "@smithy/types";
import { calculateBodyLength } from "@smithy/util-body-length-browser";

/**
* serializeRpcv2cborEmptyInputOutputCommand
Expand Down Expand Up @@ -1194,33 +1194,6 @@ const deserializeMetadata = (output: __HttpResponse): __ResponseMetadata => ({
});

const throwDefaultError = withBaseException(__BaseException);
const buildHttpRpcRequest = async (
context: __SerdeContext,
headers: __HeaderBag,
path: string,
resolvedHostname: string | undefined,
body: any
): Promise<__HttpRequest> => {
const { hostname, protocol = "https", port, path: basePath } = await context.endpoint();
const contents: any = {
protocol,
hostname,
port,
method: "POST",
path: basePath.endsWith("/") ? basePath.slice(0, -1) + path : basePath + path,
headers,
};
if (resolvedHostname !== undefined) {
contents.hostname = resolvedHostname;
}
if (body !== undefined) {
contents.body = body;
try {
contents.headers["content-length"] = String(calculateBodyLength(body));
} catch (e) {}
}
return new __HttpRequest(contents);
};
const SHARED_HEADERS: __HeaderBag = {
"content-type": "application/cbor",
"smithy-protocol": "rpc-v2-cbor",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ public TypeScriptWriter addImport(String name, String as, PackageContainer from)
* submodule path, for example "@smithy/core/cbor".
*/
public TypeScriptWriter addImportSubmodule(String name, String as, PackageContainer from, String submodule) {
if (from instanceof Dependency) {
addDependency((Dependency) from);
if (from instanceof Dependency dependency) {
addDependency(dependency);
}
return this.addImport(name, as, from.getPackageName() + submodule);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,39 +103,9 @@ public void generateSharedComponents(GenerationContext context) {
writer.addUseImports(requestType);
writer.addImport("SerdeContext", "__SerdeContext", TypeScriptDependency.SMITHY_TYPES);
writer.addImport("HeaderBag", "__HeaderBag", TypeScriptDependency.SMITHY_TYPES);
writer.openBlock("""
const buildHttpRpcRequest = async (
context: __SerdeContext,
headers: __HeaderBag,
path: string,
resolvedHostname: string | undefined,
body: any,
): Promise<$T> => {""", "};", requestType, () -> {
writer.addImport("calculateBodyLength", null, TypeScriptDependency.AWS_SDK_UTIL_BODY_LENGTH_BROWSER);
writer.write("""
const { hostname, protocol = "https", port, path: basePath } = await context.endpoint();
const contents: any = {
protocol,
hostname,
port,
method: "POST",
path: basePath.endsWith("/") ? basePath.slice(0, -1) + path : basePath + path,
headers,
};
if (resolvedHostname !== undefined) {
contents.hostname = resolvedHostname;
}
if (body !== undefined) {
contents.body = body;
try {
contents.headers["content-length"] = String(calculateBodyLength(body));
} catch (e) {}
}
return new $T(contents);
""",
requestType
);
}
writer.addImportSubmodule(
"buildHttpRpcRequest", null,
TypeScriptDependency.SMITHY_CORE, SmithyCoreSubmodules.CBOR
);
writeSharedRequestHeaders(context);
writer.write("");
Expand Down
3 changes: 2 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2276,6 +2276,7 @@ __metadata:
"@smithy/protocol-http": "workspace:^"
"@smithy/smithy-client": "workspace:^"
"@smithy/types": "workspace:^"
"@smithy/util-body-length-browser": "workspace:^"
"@smithy/util-middleware": "workspace:^"
"@smithy/util-utf8": "workspace:^"
"@types/node": ^16.18.96
Expand Down Expand Up @@ -2881,7 +2882,7 @@ __metadata:
languageName: unknown
linkType: soft

"@smithy/util-body-length-browser@^3.0.0, @smithy/util-body-length-browser@workspace:packages/util-body-length-browser":
"@smithy/util-body-length-browser@^3.0.0, @smithy/util-body-length-browser@workspace:^, @smithy/util-body-length-browser@workspace:packages/util-body-length-browser":
version: 0.0.0-use.local
resolution: "@smithy/util-body-length-browser@workspace:packages/util-body-length-browser"
dependencies:
Expand Down

0 comments on commit ca3fc95

Please sign in to comment.