Skip to content

Commit

Permalink
fix(middleware-flexible-checksum): remove stream collection code (#6846)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe authored Jan 24, 2025
1 parent 65c6ae9 commit 9f9fe77
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 63 deletions.
13 changes: 6 additions & 7 deletions packages/middleware-flexible-checksums/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,20 @@
"build:types:downlevel": "downlevel-dts dist-types dist-types/ts3.4",
"clean": "rimraf ./dist-* && rimraf *.tsbuildinfo",
"test": "yarn g:vitest run",
"test:integration": "yarn g:vitest run -c vitest.config.integ.ts",
"test:watch": "yarn g:vitest watch",
"test:integration:watch": "yarn g:vitest watch -c vitest.config.integ.ts"
"test:integration": "yarn g:vitest run -c vitest.config.integ.ts",
"test:integration:watch": "yarn g:vitest watch -c vitest.config.integ.ts",
"test:e2e": "yarn g:vitest run -c vitest.config.e2e.ts",
"test:e2e:watch": "yarn g:vitest watch -c vitest.config.e2e.ts"
},
"main": "./dist-cjs/index.js",
"module": "./dist-es/index.js",
"browser": {
"./dist-es/getCrc32ChecksumAlgorithmFunction": "./dist-es/getCrc32ChecksumAlgorithmFunction.browser",
"./dist-es/streams/create-read-stream-on-buffer": "./dist-es/streams/create-read-stream-on-buffer.browser"
"./dist-es/getCrc32ChecksumAlgorithmFunction": "./dist-es/getCrc32ChecksumAlgorithmFunction.browser"
},
"react-native": {
"./dist-es/getCrc32ChecksumAlgorithmFunction": "./dist-es/getCrc32ChecksumAlgorithmFunction.browser",
"./dist-es/streams/create-read-stream-on-buffer": "./dist-es/streams/create-read-stream-on-buffer.browser",
"./dist-cjs/getCrc32ChecksumAlgorithmFunction": "./dist-cjs/getCrc32ChecksumAlgorithmFunction.browser",
"./dist-cjs/streams/create-read-stream-on-buffer": "./dist-cjs/streams/create-read-stream-on-buffer.browser"
"./dist-cjs/getCrc32ChecksumAlgorithmFunction": "./dist-cjs/getCrc32ChecksumAlgorithmFunction.browser"
},
"types": "./dist-types/index.d.ts",
"author": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import { ChecksumAlgorithm } from "./constants";
import { getChecksumAlgorithmListForResponse } from "./getChecksumAlgorithmListForResponse";
import { getChecksumLocationName } from "./getChecksumLocationName";
import { isChecksumWithPartNumber } from "./isChecksumWithPartNumber";
import { isStreaming } from "./isStreaming";
import { createReadStreamOnBuffer } from "./streams/create-read-stream-on-buffer";
import { validateChecksumFromResponse } from "./validateChecksumFromResponse";

export interface FlexibleChecksumsResponseMiddlewareConfig {
Expand Down Expand Up @@ -66,7 +64,6 @@ export const flexibleChecksumsResponseMiddleware =
const result = await next(args);

const response = result.response as HttpResponse;
let collectedStream: Uint8Array | undefined = undefined;

const { requestValidationModeMember, responseAlgorithms } = middlewareConfig;
// @ts-ignore Element implicitly has an 'any' type for input[requestValidationModeMember]
Expand All @@ -84,22 +81,11 @@ export const flexibleChecksumsResponseMiddleware =
return result;
}

const isStreamingBody = isStreaming(response.body);

if (isStreamingBody) {
collectedStream = await config.streamCollector(response.body);
response.body = createReadStreamOnBuffer(collectedStream);
}

await validateChecksumFromResponse(result.response as HttpResponse, {
await validateChecksumFromResponse(response as HttpResponse, {
config,
responseAlgorithms,
logger: context.logger,
});

if (isStreamingBody && collectedStream) {
response.body = createReadStreamOnBuffer(collectedStream);
}
}

return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { S3 } from "@aws-sdk/client-s3";
import type { HttpRequest, HttpResponse } from "@smithy/types";
import { headStream } from "@smithy/util-stream";
import { Readable } from "node:stream";
import { beforeAll, describe, expect, test as it } from "vitest";

import { getIntegTestResources } from "../../../tests/e2e/get-integ-test-resources";

describe("S3 checksums", () => {
let s3: S3;
let s3_noChecksum: S3;
let Bucket: string;
let Key: string;
let region: string;
const expected = new Uint8Array([97, 98, 99, 100]);

beforeAll(async () => {
const integTestResourcesEnv = await getIntegTestResources();
Object.assign(process.env, integTestResourcesEnv);

region = process?.env?.AWS_SMOKE_TEST_REGION as string;
Bucket = process?.env?.AWS_SMOKE_TEST_BUCKET as string;

s3 = new S3({ region });
s3_noChecksum = new S3({
region,
requestChecksumCalculation: "WHEN_REQUIRED",
responseChecksumValidation: "WHEN_REQUIRED",
});
Key = "middleware-flexible-checksum.txt";

s3.middlewareStack.add(
(next) => async (args) => {
const r = await next(args);
const reqHeader = (args.request as HttpRequest).headers["x-amz-sdk-checksum-algorithm"];
const resHeader = (r.response as HttpResponse).headers["x-amz-checksum-crc32"];
if (reqHeader) {
expect(reqHeader).toEqual("CRC32");
}
if (resHeader) {
expect(resHeader).toEqual("7YLNEQ==");
}
return r;
},
{
step: "build",
override: true,
name: "assert",
}
);

await s3.putObject({ Bucket, Key, Body: "abcd" });
});

it("an object should have checksum by default", async () => {
await s3.getObject({ Bucket, Key });
});

describe("the stream returned by S3::getObject should function interchangeably between ChecksumStream and default streams", () => {
it("when collecting the stream", async () => {
const defaultStream = (await s3_noChecksum.getObject({ Bucket, Key })).Body as Readable;
const checksumStream = (await s3.getObject({ Bucket, Key })).Body as Readable;

expect(defaultStream.constructor.name).not.toEqual("ChecksumStream");
expect(checksumStream.constructor.name).toEqual("ChecksumStream");

const defaultStreamContents = await headStream(defaultStream, Infinity);
const checksumStreamContents = await headStream(checksumStream, Infinity);

expect(defaultStreamContents).toEqual(expected);
expect(defaultStream.closed).toBe(true);
expect(checksumStreamContents).toEqual(expected);
expect(checksumStream.closed).toBe(true);
});

it("when piping the stream to web stream api", async () => {
const defaultStream = Readable.toWeb((await s3_noChecksum.getObject({ Bucket, Key })).Body as Readable);
const checksumStream = Readable.toWeb((await s3.getObject({ Bucket, Key })).Body as Readable);

const defaultStreamContents = await headStream(defaultStream as ReadableStream<any>, Infinity);
const checksumStreamContents = await headStream(checksumStream as ReadableStream<any>, Infinity);

expect(defaultStreamContents).toEqual(expected);
await expect(defaultStream.getReader().closed).resolves.toBe(undefined);
expect(checksumStreamContents).toEqual(expected);
await expect(checksumStream.getReader().closed).resolves.toBe(undefined);
});
});
});

This file was deleted.

This file was deleted.

This file was deleted.

8 changes: 8 additions & 0 deletions packages/middleware-flexible-checksums/vitest.config.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
include: ["**/*.e2e.spec.ts"],
environment: "node",
},
});

0 comments on commit 9f9fe77

Please sign in to comment.