Skip to content

Commit

Permalink
fix(signature-v4-crt): remove dynamic imports (!) (#5225)
Browse files Browse the repository at this point in the history
* fix(signature-v4-crt): remove dynamic imports (!)

* test: split s3 ispec into node and browser files

* test(client-s3): move unit tests to own folder
  • Loading branch information
kuhe authored Oct 26, 2023
1 parent e0f2d91 commit 89f97b5
Show file tree
Hide file tree
Showing 20 changed files with 371 additions and 172 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,11 @@ If the required AWS Common Runtime components are not installed you will receive
```console
Cannot find module '@aws-sdk/signature-v4-crt'
...
Please check if you have installed "@aws-sdk/signature-v4-crt" package explicitly.
For more information please go to https://github.com/aws/aws-sdk-js-v3#known-issues
Please check whether you have installed the "@aws-sdk/signature-v4-crt" package explicitly.
You must also register the package by calling [require("@aws-sdk/signature-v4-crt");]
or an ESM equivalent such as [import "@aws-sdk/signature-v4-crt";].
For more information please go to
https://github.com/aws/aws-sdk-js-v3#functionality-requiring-aws-common-runtime-crt"
```

indicating that the required dependency is missing to use the associated functionality. To install this dependency follow
Expand All @@ -584,6 +587,17 @@ If you are using Yarn:
yarn add @aws-sdk/signature-v4-crt
```

Additionally, load the signature-v4-crt package by importing it.

```js
require("@aws-sdk/signature-v4-crt");
// or ESM
import "@aws-sdk/signature-v4-crt";
```

Only the import statement is needed. The implementation then registers itself with `@aws-sdk/signature-v4-multi-region`
and becomes available for its use. You do not need to use any imported objects directly.

#### Related issues

1. [S3 Multi-Region Access Point(MRAP) is not available unless with additional dependency](https://github.com/aws/aws-sdk-js-v3/issues/2822)
2 changes: 2 additions & 0 deletions clients/client-eventbridge/test/EventBridge.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/// <reference types="mocha" />
import "@aws-sdk/signature-v4-crt";

import { FinalizeRequestMiddleware } from "@aws-sdk/types";
import chai from "chai";
import chaiAsPromised from "chai-as-promised";
Expand Down
5 changes: 5 additions & 0 deletions clients/client-s3/jest.config.e2e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
preset: "ts-jest",
testMatch: ["**/*.e2e.spec.ts"],
bail: true,
};
6 changes: 4 additions & 2 deletions clients/client-s3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
"extract:docs": "api-extractor run --local",
"generate:client": "node ../../scripts/generate-clients/single-service --solo s3",
"test": "yarn test:unit",
"test:e2e": "ts-mocha test/**/*.ispec.ts && karma start karma.conf.js",
"test:unit": "ts-mocha test/**/*.spec.ts"
"test:e2e": "yarn test:e2e:node && yarn test:e2e:browser",
"test:e2e:browser": "ts-mocha test/**/*.browser.ispec.ts && karma start karma.conf.js",
"test:e2e:node": "jest --c jest.config.e2e.js",
"test:unit": "ts-mocha test/unit/**/*.spec.ts"
},
"main": "./dist-cjs/index.js",
"types": "./dist-types/index.d.ts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import { S3, SelectObjectContentEventStream } from "../../src/index";
import { createBuffer } from "./helpers";
chai.use(chaiAsPromised);
const { expect } = chai;
// There will be default values of defaultRegion, credentials, and isBrowser variable in browser tests.
// Define the values for Node.js tests

const region: string | undefined = (globalThis as any).defaultRegion || process?.env?.AWS_SMOKE_TEST_REGION;
const credentials: Credentials | undefined = (globalThis as any).credentials || undefined;
const isBrowser: boolean | undefined = (globalThis as any).isBrowser || false;
Expand All @@ -21,7 +20,7 @@ const mrapArn = (globalThis as any)?.window?.__env__?.AWS_SMOKE_TEST_MRAP_ARN ||

let Key = `${Date.now()}`;

describe("@aws-sdk/client-s3", () => {
(isBrowser ? describe : xdescribe)("@aws-sdk/client-s3", () => {
const client = new S3({
region: region,
credentials,
Expand All @@ -34,81 +33,51 @@ describe("@aws-sdk/client-s3", () => {
after(async () => {
await client.deleteObject({ Bucket, Key });
});
if (isBrowser) {
const buf = createBuffer("1KB");
it("should succeed with blob body", async () => {
const result = await client.putObject({
Bucket,
Key,
Body: new Blob([buf]),
});
expect(result.$metadata.httpStatusCode).to.equal(200);
const buf = createBuffer("1KB");
it("should succeed with blob body", async () => {
const result = await client.putObject({
Bucket,
Key,
Body: new Blob([buf]),
});
expect(result.$metadata.httpStatusCode).to.equal(200);
});

it("should succeed with TypedArray body", async () => {
const result = await client.putObject({
Bucket,
Key,
Body: buf,
});
expect(result.$metadata.httpStatusCode).to.equal(200);
it("should succeed with TypedArray body", async () => {
const result = await client.putObject({
Bucket,
Key,
Body: buf,
});
expect(result.$metadata.httpStatusCode).to.equal(200);
});

// todo: fix needed
// todo: TypeError: Failed to construct 'Request': The `duplex` member must
// todo: be specified for a request with a streaming body
it.skip("should succeed with ReadableStream body", async () => {
const length = 10 * 1000; // 10KB
const chunkSize = 10;
const readableStream = new ReadableStream({
start(controller) {
let sizeLeft = length;
while (sizeLeft > 0) {
let chunk = "";
for (let i = 0; i < Math.min(sizeLeft, chunkSize); i++) {
chunk += "x";
}
controller.enqueue(chunk);
sizeLeft -= chunk.length;
}
},
});
const result = await client.putObject({
Bucket,
Key,
Body: readableStream,
});
expect(result.$metadata.httpStatusCode).to.equal(200);
});
} else {
it("should succeed with Node.js readable stream body", async () => {
const length = 10 * 1000; // 10KB
const chunkSize = 10;
const { Readable } = require("stream");
let sizeLeft = length;
const inputStream = new Readable({
read() {
if (sizeLeft <= 0) {
this.push(null); //end stream;
return;
}
// todo: fix needed
// todo: TypeError: Failed to construct 'Request': The `duplex` member must
// todo: be specified for a request with a streaming body
it.skip("should succeed with ReadableStream body", async () => {
const length = 10 * 1000; // 10KB
const chunkSize = 10;
const readableStream = new ReadableStream({
start(controller) {
let sizeLeft = length;
while (sizeLeft > 0) {
let chunk = "";
for (let i = 0; i < Math.min(sizeLeft, chunkSize); i++) {
chunk += "x";
}
this.push(chunk);
controller.enqueue(chunk);
sizeLeft -= chunk.length;
},
});
inputStream.size = length; // This is required
const result = await client.putObject({
Bucket,
Key,
Body: inputStream,
});
expect(result.$metadata.httpStatusCode).to.equal(200);
}
},
});
const result = await client.putObject({
Bucket,
Key,
Body: readableStream,
});
}
expect(result.$metadata.httpStatusCode).to.equal(200);
});
});

describe("GetObject", function () {
Expand Down Expand Up @@ -141,12 +110,7 @@ describe("@aws-sdk/client-s3", () => {
}

expect(result.$metadata.httpStatusCode).to.equal(200);
if (isBrowser) {
expect(result.Body).to.be.instanceOf(ReadableStream);
} else {
const { Readable } = require("stream");
expect(result.Body).to.be.instanceOf(Readable);
}
expect(result.Body).to.be.instanceOf(ReadableStream);
});
});

Expand Down Expand Up @@ -310,34 +274,17 @@ esfuture,29`;
describe("Multi-region access point", () => {
before(async () => {
Key = `${Date.now()}`;
if (!isBrowser) {
await client.putObject({ Bucket: mrapArn, Key, Body: "foo" });
}
});
after(async () => {
if (!isBrowser) {
await client.deleteObject({ Bucket: mrapArn, Key });
}
});
if (isBrowser) {
it("should throw for aws-crt no available in browser", async () => {
try {
await client.listObjects({
Bucket: mrapArn,
});
expect.fail("MRAP call in browser should throw");
} catch (e) {
expect(e.message).include("only available in Node.js");
}
});
} else {
it("should succeed with valid MRAP ARN", async () => {
const result = await client.listObjects({
after(async () => {});
it("should throw for aws-crt no available in browser", async () => {
try {
await client.listObjects({
Bucket: mrapArn,
});
expect(result.$metadata.httpStatusCode).to.equal(200);
expect(result.Contents).to.be.instanceOf(Array);
});
}
expect.fail("MRAP call in browser should throw");
} catch (e) {
expect(e.message).include("only available in Node.js");
}
});
});
});
Loading

0 comments on commit 89f97b5

Please sign in to comment.