Skip to content

Commit 89f97b5

Browse files
authored
fix(signature-v4-crt): remove dynamic imports (!) (#5225)
* 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
1 parent e0f2d91 commit 89f97b5

File tree

20 files changed

+371
-172
lines changed

20 files changed

+371
-172
lines changed

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,11 @@ If the required AWS Common Runtime components are not installed you will receive
562562
```console
563563
Cannot find module '@aws-sdk/signature-v4-crt'
564564
...
565-
Please check if you have installed "@aws-sdk/signature-v4-crt" package explicitly.
566-
For more information please go to https://github.com/aws/aws-sdk-js-v3#known-issues
565+
Please check whether you have installed the "@aws-sdk/signature-v4-crt" package explicitly.
566+
You must also register the package by calling [require("@aws-sdk/signature-v4-crt");]
567+
or an ESM equivalent such as [import "@aws-sdk/signature-v4-crt";].
568+
For more information please go to
569+
https://github.com/aws/aws-sdk-js-v3#functionality-requiring-aws-common-runtime-crt"
567570
```
568571

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

590+
Additionally, load the signature-v4-crt package by importing it.
591+
592+
```js
593+
require("@aws-sdk/signature-v4-crt");
594+
// or ESM
595+
import "@aws-sdk/signature-v4-crt";
596+
```
597+
598+
Only the import statement is needed. The implementation then registers itself with `@aws-sdk/signature-v4-multi-region`
599+
and becomes available for its use. You do not need to use any imported objects directly.
600+
587601
#### Related issues
588602

589603
1. [S3 Multi-Region Access Point(MRAP) is not available unless with additional dependency](https://github.com/aws/aws-sdk-js-v3/issues/2822)

clients/client-eventbridge/test/EventBridge.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/// <reference types="mocha" />
2+
import "@aws-sdk/signature-v4-crt";
3+
24
import { FinalizeRequestMiddleware } from "@aws-sdk/types";
35
import chai from "chai";
46
import chaiAsPromised from "chai-as-promised";

clients/client-s3/jest.config.e2e.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
preset: "ts-jest",
3+
testMatch: ["**/*.e2e.spec.ts"],
4+
bail: true,
5+
};

clients/client-s3/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
"extract:docs": "api-extractor run --local",
1515
"generate:client": "node ../../scripts/generate-clients/single-service --solo s3",
1616
"test": "yarn test:unit",
17-
"test:e2e": "ts-mocha test/**/*.ispec.ts && karma start karma.conf.js",
18-
"test:unit": "ts-mocha test/**/*.spec.ts"
17+
"test:e2e": "yarn test:e2e:node && yarn test:e2e:browser",
18+
"test:e2e:browser": "ts-mocha test/**/*.browser.ispec.ts && karma start karma.conf.js",
19+
"test:e2e:node": "jest --c jest.config.e2e.js",
20+
"test:unit": "ts-mocha test/unit/**/*.spec.ts"
1921
},
2022
"main": "./dist-cjs/index.js",
2123
"types": "./dist-types/index.d.ts",

clients/client-s3/test/e2e/S3.ispec.ts renamed to clients/client-s3/test/e2e/S3.browser.ispec.ts

Lines changed: 47 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import { S3, SelectObjectContentEventStream } from "../../src/index";
1111
import { createBuffer } from "./helpers";
1212
chai.use(chaiAsPromised);
1313
const { expect } = chai;
14-
// There will be default values of defaultRegion, credentials, and isBrowser variable in browser tests.
15-
// Define the values for Node.js tests
14+
1615
const region: string | undefined = (globalThis as any).defaultRegion || process?.env?.AWS_SMOKE_TEST_REGION;
1716
const credentials: Credentials | undefined = (globalThis as any).credentials || undefined;
1817
const isBrowser: boolean | undefined = (globalThis as any).isBrowser || false;
@@ -21,7 +20,7 @@ const mrapArn = (globalThis as any)?.window?.__env__?.AWS_SMOKE_TEST_MRAP_ARN ||
2120

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

24-
describe("@aws-sdk/client-s3", () => {
23+
(isBrowser ? describe : xdescribe)("@aws-sdk/client-s3", () => {
2524
const client = new S3({
2625
region: region,
2726
credentials,
@@ -34,81 +33,51 @@ describe("@aws-sdk/client-s3", () => {
3433
after(async () => {
3534
await client.deleteObject({ Bucket, Key });
3635
});
37-
if (isBrowser) {
38-
const buf = createBuffer("1KB");
39-
it("should succeed with blob body", async () => {
40-
const result = await client.putObject({
41-
Bucket,
42-
Key,
43-
Body: new Blob([buf]),
44-
});
45-
expect(result.$metadata.httpStatusCode).to.equal(200);
36+
const buf = createBuffer("1KB");
37+
it("should succeed with blob body", async () => {
38+
const result = await client.putObject({
39+
Bucket,
40+
Key,
41+
Body: new Blob([buf]),
4642
});
43+
expect(result.$metadata.httpStatusCode).to.equal(200);
44+
});
4745

48-
it("should succeed with TypedArray body", async () => {
49-
const result = await client.putObject({
50-
Bucket,
51-
Key,
52-
Body: buf,
53-
});
54-
expect(result.$metadata.httpStatusCode).to.equal(200);
46+
it("should succeed with TypedArray body", async () => {
47+
const result = await client.putObject({
48+
Bucket,
49+
Key,
50+
Body: buf,
5551
});
52+
expect(result.$metadata.httpStatusCode).to.equal(200);
53+
});
5654

57-
// todo: fix needed
58-
// todo: TypeError: Failed to construct 'Request': The `duplex` member must
59-
// todo: be specified for a request with a streaming body
60-
it.skip("should succeed with ReadableStream body", async () => {
61-
const length = 10 * 1000; // 10KB
62-
const chunkSize = 10;
63-
const readableStream = new ReadableStream({
64-
start(controller) {
65-
let sizeLeft = length;
66-
while (sizeLeft > 0) {
67-
let chunk = "";
68-
for (let i = 0; i < Math.min(sizeLeft, chunkSize); i++) {
69-
chunk += "x";
70-
}
71-
controller.enqueue(chunk);
72-
sizeLeft -= chunk.length;
73-
}
74-
},
75-
});
76-
const result = await client.putObject({
77-
Bucket,
78-
Key,
79-
Body: readableStream,
80-
});
81-
expect(result.$metadata.httpStatusCode).to.equal(200);
82-
});
83-
} else {
84-
it("should succeed with Node.js readable stream body", async () => {
85-
const length = 10 * 1000; // 10KB
86-
const chunkSize = 10;
87-
const { Readable } = require("stream");
88-
let sizeLeft = length;
89-
const inputStream = new Readable({
90-
read() {
91-
if (sizeLeft <= 0) {
92-
this.push(null); //end stream;
93-
return;
94-
}
55+
// todo: fix needed
56+
// todo: TypeError: Failed to construct 'Request': The `duplex` member must
57+
// todo: be specified for a request with a streaming body
58+
it.skip("should succeed with ReadableStream body", async () => {
59+
const length = 10 * 1000; // 10KB
60+
const chunkSize = 10;
61+
const readableStream = new ReadableStream({
62+
start(controller) {
63+
let sizeLeft = length;
64+
while (sizeLeft > 0) {
9565
let chunk = "";
9666
for (let i = 0; i < Math.min(sizeLeft, chunkSize); i++) {
9767
chunk += "x";
9868
}
99-
this.push(chunk);
69+
controller.enqueue(chunk);
10070
sizeLeft -= chunk.length;
101-
},
102-
});
103-
inputStream.size = length; // This is required
104-
const result = await client.putObject({
105-
Bucket,
106-
Key,
107-
Body: inputStream,
108-
});
109-
expect(result.$metadata.httpStatusCode).to.equal(200);
71+
}
72+
},
73+
});
74+
const result = await client.putObject({
75+
Bucket,
76+
Key,
77+
Body: readableStream,
11078
});
111-
}
79+
expect(result.$metadata.httpStatusCode).to.equal(200);
80+
});
11281
});
11382

11483
describe("GetObject", function () {
@@ -141,12 +110,7 @@ describe("@aws-sdk/client-s3", () => {
141110
}
142111

143112
expect(result.$metadata.httpStatusCode).to.equal(200);
144-
if (isBrowser) {
145-
expect(result.Body).to.be.instanceOf(ReadableStream);
146-
} else {
147-
const { Readable } = require("stream");
148-
expect(result.Body).to.be.instanceOf(Readable);
149-
}
113+
expect(result.Body).to.be.instanceOf(ReadableStream);
150114
});
151115
});
152116

@@ -310,34 +274,17 @@ esfuture,29`;
310274
describe("Multi-region access point", () => {
311275
before(async () => {
312276
Key = `${Date.now()}`;
313-
if (!isBrowser) {
314-
await client.putObject({ Bucket: mrapArn, Key, Body: "foo" });
315-
}
316277
});
317-
after(async () => {
318-
if (!isBrowser) {
319-
await client.deleteObject({ Bucket: mrapArn, Key });
320-
}
321-
});
322-
if (isBrowser) {
323-
it("should throw for aws-crt no available in browser", async () => {
324-
try {
325-
await client.listObjects({
326-
Bucket: mrapArn,
327-
});
328-
expect.fail("MRAP call in browser should throw");
329-
} catch (e) {
330-
expect(e.message).include("only available in Node.js");
331-
}
332-
});
333-
} else {
334-
it("should succeed with valid MRAP ARN", async () => {
335-
const result = await client.listObjects({
278+
after(async () => {});
279+
it("should throw for aws-crt no available in browser", async () => {
280+
try {
281+
await client.listObjects({
336282
Bucket: mrapArn,
337283
});
338-
expect(result.$metadata.httpStatusCode).to.equal(200);
339-
expect(result.Contents).to.be.instanceOf(Array);
340-
});
341-
}
284+
expect.fail("MRAP call in browser should throw");
285+
} catch (e) {
286+
expect(e.message).include("only available in Node.js");
287+
}
288+
});
342289
});
343290
});

0 commit comments

Comments
 (0)