Skip to content

Commit

Permalink
fix(middleware-sdk-s3): use console warn if logger is no-op (#5222)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe authored Sep 15, 2023
1 parent b9d59e8 commit db97698
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/middleware-sdk-s3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@aws-sdk/types": "*",
"@aws-sdk/util-arn-parser": "*",
"@smithy/protocol-http": "^3.0.3",
"@smithy/smithy-client": "^2.1.4",
"@smithy/types": "^2.3.1",
"tslib": "^2.5.0"
},
Expand Down
24 changes: 24 additions & 0 deletions packages/middleware-sdk-s3/src/check-content-length-header.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { NoOpLogger } from "@smithy/smithy-client";

import { checkContentLengthHeader } from "./check-content-length-header";

describe("checkContentLengthHeaderMiddleware", () => {
Expand Down Expand Up @@ -33,6 +35,28 @@ describe("checkContentLengthHeaderMiddleware", () => {
);
});

it("warns with console if logger is the default NoOpLogger", async () => {
const handler = checkContentLengthHeader()(mockNextHandler, {
logger: new NoOpLogger(),
});

await handler({
request: {
method: null,
protocol: null,
hostname: null,
path: null,
query: {},
headers: {},
},
input: {},
});

expect(spy).toHaveBeenCalledWith(
"Are you using a Stream of unknown length as the Body of a PutObject request? Consider using Upload instead from @aws-sdk/lib-storage."
);
});

it("uses the context logger if available", async () => {
const context = {
logger: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { HttpRequest } from "@smithy/protocol-http";
import { NoOpLogger } from "@smithy/smithy-client";
import {
FinalizeHandler,
FinalizeHandlerArguments,
Expand Down Expand Up @@ -29,7 +30,7 @@ export function checkContentLengthHeader(): FinalizeRequestMiddleware<any, any>
if (HttpRequest.isInstance(request)) {
if (!request.headers[CONTENT_LENGTH_HEADER]) {
const message = `Are you using a Stream of unknown length as the Body of a PutObject request? Consider using Upload instead from @aws-sdk/lib-storage.`;
if (typeof context?.logger?.warn === "function") {
if (typeof context?.logger?.warn === "function" && !(context.logger instanceof NoOpLogger)) {
context.logger.warn(message);
} else {
console.warn(message);
Expand Down

0 comments on commit db97698

Please sign in to comment.