From 097d29f1cbcd55f21ceb8a1718af3c7e55eb9a6f Mon Sep 17 00:00:00 2001 From: Maciej Radzikowski Date: Sun, 26 Jun 2022 17:54:16 +0200 Subject: [PATCH] fix!: remove libStorage helper to remove incompatibilities (#105) * fix!: remove libStorage helper * chore: revert version bump --- README.md | 23 +++---- package.json | 23 +------ src/libStorage.ts | 21 ------ test-e2e/libStorage/jest.config.js | 5 -- test-e2e/libStorage/libStorage.test.ts | 28 -------- test-e2e/libStorage/run.ts | 15 ----- test/libStorage.test.ts | 89 -------------------------- yarn.lock | 32 +-------- 8 files changed, 12 insertions(+), 224 deletions(-) delete mode 100644 src/libStorage.ts delete mode 100644 test-e2e/libStorage/jest.config.js delete mode 100644 test-e2e/libStorage/libStorage.test.ts delete mode 100644 test-e2e/libStorage/run.ts delete mode 100644 test/libStorage.test.ts diff --git a/README.md b/README.md index a240319..955193a 100644 --- a/README.md +++ b/README.md @@ -255,17 +255,16 @@ ddbMock.on(QueryCommand).resolves({ #### Lib Storage Upload -To mock `@aws-sdk/lib-storage` `Upload` you need to call -a helper function `mockLibStorageUpload()` -that will configure required S3Client command mocks: +To mock `@aws-sdk/lib-storage` `Upload` you need to mock +at least two commands: `CreateMultipartUploadCommand` and `UploadPartCommand` +used [under the hood](https://github.com/aws/aws-sdk-js-v3/blob/main/lib/lib-storage/src/Upload.ts): ```typescript -import {mockLibStorageUpload} from 'aws-sdk-client-mock/libStorage'; -import {Upload} from '@aws-sdk/lib-storage'; -import {S3Client} from '@aws-sdk/client-s3'; +import {S3Client, CreateMultipartUploadCommand, UploadPartCommand} from '@aws-sdk/client-s3'; const s3Mock = mockClient(S3Client); -mockLibStorageUpload(s3Mock); +s3Mock.on(CreateMultipartUploadCommand).resolves({UploadId: '1'}); +s3Mock.on(UploadPartCommand).resolves({ETag: '1'}); const s3Upload = new Upload({ client: new S3Client({}), @@ -283,16 +282,10 @@ s3Upload.on('httpUploadProgress', (progress) => { await s3Upload.done(); ``` -When using with Jest, it requires Jest v28.0.0 or higher, -[supporting package exports](https://github.com/facebook/jest/issues/9771). +This way, the `Upload#done()` will complete successfuly. -You can call `mockLibStorageUpload()` without providing an S3Client mock. -In that case, the client mock will be created and returned from the function. -However, you still need to have `@aws-sdk/client-s3` installed as a dependency. - -By default, the `@aws-sdk/lib-storage` `Upload#done()` will complete successfuly. To cause a failure, you need to specify the `rejects()` behavior -of one of the [AWS SDK Commands used under the hood by the `lib-storage`](https://github.com/aws/aws-sdk-js-v3/blob/main/lib/lib-storage/src/Upload.ts). +for one of the AWS SDK Commands used by the `@aws-sdk/lib-storage`. For uploading a small file (under the defined multipart upload single part size), `lib-storage` sends a `PutObjectCommand`. To make it fail: diff --git a/package.json b/package.json index 9a1c00f..8de9881 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "pretest": "rimraf coverage/", "test": "jest --coverage", "test-types": "tsd", - "test-e2e": "ts-node test-e2e/simple/run.ts & ts-node test-e2e/libStorage/run.ts", + "test-e2e": "ts-node test-e2e/simple/run.ts", "lint": "eslint .", "typedoc": "typedoc", "build:cjs": "tsc -p tsconfig.json", @@ -36,26 +36,6 @@ "verdaccio": "rimraf verdaccio-storage && verdaccio -c verdaccio.yml", "local-publish": "npm publish --registry http://localhost:4873/" }, - "exports": { - ".": { - "import": "./dist/es/index.js", - "require": "./dist/cjs/index.js" - }, - "./libStorage": { - "import": "./dist/es/libStorage.js", - "require": "./dist/cjs/libStorage.js" - } - }, - "typesVersions": { - "*": { - "*": [ - "dist/types/index.d.ts" - ], - "libStorage": [ - "dist/types/libStorage.d.ts" - ] - } - }, "module": "dist/es/index.js", "main": "dist/cjs/index.js", "types": "dist/types/index.d.ts", @@ -74,7 +54,6 @@ "@aws-sdk/client-sns": "3.49.0", "@aws-sdk/client-sqs": "3.49.0", "@aws-sdk/lib-dynamodb": "3.49.0", - "@aws-sdk/lib-storage": "3.49.0", "@aws-sdk/types": "3.49.0", "@commitlint/cli": "13.1.0", "@commitlint/config-conventional": "13.1.0", diff --git a/src/libStorage.ts b/src/libStorage.ts deleted file mode 100644 index e69ada6..0000000 --- a/src/libStorage.ts +++ /dev/null @@ -1,21 +0,0 @@ -import {AwsClientStub} from './awsClientStub'; -import {CreateMultipartUploadCommand, S3Client as S3ClientType, UploadPartCommand} from '@aws-sdk/client-s3'; -import {mockClient} from './mockClient'; - -/** - * Configures required command mocks of the S3Client mock to support Lib Storage Upload helper - * for multipart file upload. - * - * If S3Client mocks is not provided, a new one is created. - * @param s3Mock S3Client mock created with {@link mockClient} function - */ -export const mockLibStorageUpload = (s3Mock?: AwsClientStub): AwsClientStub => { - if (!s3Mock) { - s3Mock = mockClient(S3ClientType); - } - - s3Mock.on(CreateMultipartUploadCommand).resolves({UploadId: '1'}); - s3Mock.on(UploadPartCommand).resolves({ETag: '1'}); - - return s3Mock; -}; diff --git a/test-e2e/libStorage/jest.config.js b/test-e2e/libStorage/jest.config.js deleted file mode 100644 index 3204da1..0000000 --- a/test-e2e/libStorage/jest.config.js +++ /dev/null @@ -1,5 +0,0 @@ -module.exports = { - preset: 'ts-jest', - resetMocks: true, - testEnvironment: 'node', -}; diff --git a/test-e2e/libStorage/libStorage.test.ts b/test-e2e/libStorage/libStorage.test.ts deleted file mode 100644 index 81d0b46..0000000 --- a/test-e2e/libStorage/libStorage.test.ts +++ /dev/null @@ -1,28 +0,0 @@ -import {mockClient} from 'aws-sdk-client-mock'; -import {mockLibStorageUpload} from 'aws-sdk-client-mock/libStorage'; -import {S3Client} from '@aws-sdk/client-s3'; -import {Progress, Upload} from '@aws-sdk/lib-storage'; - -it('mocks S3 Client Upload', async () => { - const s3Mock = mockClient(S3Client); - mockLibStorageUpload(s3Mock); - - const s3Upload = new Upload({ - client: new S3Client({}), - partSize: 5 * 1024 * 1024, // 5 MB - params: { - Bucket: 'mock', - Key: 'test', - Body: 'x'.repeat(6 * 1024 * 1024), // 6 MB - }, - }); - - const uploadProgress: Progress[] = []; - s3Upload.on('httpUploadProgress', (progress) => { - uploadProgress.push(progress); - }); - - await s3Upload.done(); - - expect(uploadProgress).toHaveLength(2); -}); diff --git a/test-e2e/libStorage/run.ts b/test-e2e/libStorage/run.ts deleted file mode 100644 index a6e1c6f..0000000 --- a/test-e2e/libStorage/run.ts +++ /dev/null @@ -1,15 +0,0 @@ -import {execSync} from 'child_process'; -import * as packageJson from '../../package.json'; - -const libVersion = packageJson.version; - -const exec = (cmd: string) => execSync(cmd, { - cwd: __dirname, - stdio: 'inherit', -}); - -// use npm because when running this script from yarn and using yarn -// it did not accept private registry parameter set in other way than in a project-level .yarnrc file -exec('npm init -y'); -exec(`npm install --registry=http://localhost:4873 aws-sdk-client-mock@${libVersion} @aws-sdk/client-s3@latest @aws-sdk/lib-storage@latest jest typescript ts-jest`); -exec('npx jest'); diff --git a/test/libStorage.test.ts b/test/libStorage.test.ts deleted file mode 100644 index 8279e34..0000000 --- a/test/libStorage.test.ts +++ /dev/null @@ -1,89 +0,0 @@ -import {Progress, Upload} from '@aws-sdk/lib-storage'; -import {S3Client} from '@aws-sdk/client-s3'; -import {AwsClientStub, mockClient} from '../src'; -import {mockLibStorageUpload} from '../src/libStorage'; - -let s3Mock: AwsClientStub; - -afterEach(() => { - s3Mock.restore(); -}); - -it('mocks small file upload to S3', async () => { - s3Mock = mockClient(S3Client); - mockLibStorageUpload(s3Mock); - - const s3Upload = new Upload({ - client: new S3Client({}), - params: { - Bucket: 'mock', - Key: 'test', - Body: 'qwe', - }, - }); - - const uploadProgress: Progress[] = []; - s3Upload.on('httpUploadProgress', (progress) => { - uploadProgress.push(progress); - }); - - await s3Upload.done(); - - expect(uploadProgress).toHaveLength(1); - expect(uploadProgress[0]).toStrictEqual({ - Bucket: 'mock', - Key: 'test', - loaded: 3, - total: 3, - part: 1, - }); -}); - -it('mocks multipart upload to S3', async () => { - s3Mock = mockClient(S3Client); - mockLibStorageUpload(s3Mock); - - const s3Upload = new Upload({ - client: new S3Client({}), - partSize: 5 * 1024 * 1024, // 5 MB - params: { - Bucket: 'mock', - Key: 'test', - Body: 'x'.repeat(6 * 1024 * 1024), // 6 MB - }, - }); - - const uploadProgress: Progress[] = []; - s3Upload.on('httpUploadProgress', (progress) => { - uploadProgress.push(progress); - }); - - await s3Upload.done(); - - expect(uploadProgress).toHaveLength(2); -}); - -it('mocks multipart upload to S3 without explicit client mock', async () => { - const localS3Mock = mockLibStorageUpload(); - - const s3Upload = new Upload({ - client: new S3Client({}), - partSize: 5 * 1024 * 1024, // 5 MB - params: { - Bucket: 'mock', - Key: 'test', - Body: 'x'.repeat(6 * 1024 * 1024), // 6 MB - }, - }); - - const uploadProgress: Progress[] = []; - s3Upload.on('httpUploadProgress', (progress) => { - uploadProgress.push(progress); - }); - - await s3Upload.done(); - - expect(uploadProgress).toHaveLength(2); - - localS3Mock.restore(); -}); diff --git a/yarn.lock b/yarn.lock index 628b57b..adb3fc0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -564,16 +564,6 @@ "@aws-sdk/util-dynamodb" "3.49.0" tslib "^2.3.0" -"@aws-sdk/lib-storage@3.49.0": - version "3.49.0" - resolved "https://registry.yarnpkg.com/@aws-sdk/lib-storage/-/lib-storage-3.49.0.tgz#12a0b4afa71604717af6a8c7b8d10d481eb5181c" - integrity sha512-3QIfre3IuAbRKBqqRC6hKQD0caVeP/X/+IlWl205o120MgVPIL8SW/sld6QcLRrrvNjzOT2npfn8i0oPYs8Q+w== - dependencies: - buffer "5.6.0" - events "3.3.0" - stream-browserify "3.0.0" - tslib "^2.3.0" - "@aws-sdk/md5-js@3.49.0": version "3.49.0" resolved "https://registry.yarnpkg.com/@aws-sdk/md5-js/-/md5-js-3.49.0.tgz#c9d794c59bb6b7e68b7ff084093c0dafcfd145e4" @@ -3310,14 +3300,6 @@ buffer-xor@^1.0.3: resolved "https://registry.yarnpkg.com/buffer-xor/-/buffer-xor-1.0.3.tgz#26e61ed1422fb70dd42e6e36729ed51d855fe8d9" integrity sha1-JuYe0UIvtw3ULm42cp7VHYVf6Nk= -buffer@5.6.0: - version "5.6.0" - resolved "https://registry.yarnpkg.com/buffer/-/buffer-5.6.0.tgz#a31749dc7d81d84db08abf937b6b8c4033f62786" - integrity sha512-/gDYp/UtU0eA1ys8bOs9J6a+E/KWIY+DZ+Q2WESNUA0jFRsJOc0SNUO6xJ5SGA1xueg3NL65W6s+NY5l9cunuw== - dependencies: - base64-js "^1.0.2" - ieee754 "^1.1.4" - buffer@^4.3.0: version "4.9.2" resolved "https://registry.yarnpkg.com/buffer/-/buffer-4.9.2.tgz#230ead344002988644841ab0244af8c44bbe3ef8" @@ -4886,7 +4868,7 @@ event-emitter@^0.3.5: d "1" es5-ext "~0.10.14" -events@3.3.0, events@^3.0.0: +events@^3.0.0: version "3.3.0" resolved "https://registry.yarnpkg.com/events/-/events-3.3.0.tgz#31a95ad0a924e2d2c419a813aeb2c4e878ea7400" integrity sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q== @@ -5839,7 +5821,7 @@ inflight@^1.0.4: once "^1.3.0" wrappy "1" -inherits@2, inherits@2.0.4, inherits@^2.0.1, inherits@^2.0.3, inherits@^2.0.4, inherits@~2.0.1, inherits@~2.0.3, inherits@~2.0.4: +inherits@2, inherits@2.0.4, inherits@^2.0.1, inherits@^2.0.3, inherits@^2.0.4, inherits@~2.0.1, inherits@~2.0.3: version "2.0.4" resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c" integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ== @@ -8765,7 +8747,7 @@ read-pkg@^5.2.0: string_decoder "~1.1.1" util-deprecate "~1.0.1" -readable-stream@3, readable-stream@^3.0.0, readable-stream@^3.0.2, readable-stream@^3.5.0, readable-stream@^3.6.0: +readable-stream@3, readable-stream@^3.0.0, readable-stream@^3.0.2, readable-stream@^3.6.0: version "3.6.0" resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-3.6.0.tgz#337bbda3adc0706bd3e024426a286d4b4b2c9198" integrity sha512-BViHy7LKeTz4oNnkcLJ+lVSL6vpiFeX6/d3oSH8zCW7UxP2onchk+vTGB143xuFjHS3deTgkKoXXymXqymiIdA== @@ -9522,14 +9504,6 @@ steno@^0.4.1: dependencies: graceful-fs "^4.1.3" -stream-browserify@3.0.0: - version "3.0.0" - resolved "https://registry.yarnpkg.com/stream-browserify/-/stream-browserify-3.0.0.tgz#22b0a2850cdf6503e73085da1fc7b7d0c2122f2f" - integrity sha512-H73RAHsVBapbim0tU2JwwOiXUj+fikfiaoYAKHF3VJfA0pe2BCzkhAHBlLG6REzE+2WNZcxOXjK7lkso+9euLA== - dependencies: - inherits "~2.0.4" - readable-stream "^3.5.0" - stream-browserify@^2.0.1: version "2.0.2" resolved "https://registry.yarnpkg.com/stream-browserify/-/stream-browserify-2.0.2.tgz#87521d38a44aa7ee91ce1cd2a47df0cb49dd660b"