Skip to content

Commit b69cae6

Browse files
committed
feat: update according to new client interface
1 parent ea35070 commit b69cae6

File tree

4 files changed

+38
-35
lines changed

4 files changed

+38
-35
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const base = require("../../jest.config.base.js");
2+
3+
module.exports = {
4+
...base
5+
};

packages/s3-request-presigner/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"tslib": "^1.8.0"
2222
},
2323
"devDependencies": {
24-
"@aws-sdk/client-s3-node": "^0.1.0-preview.7",
24+
"@aws-sdk/protocol-http": "^0.1.0-preview.1",
25+
"@aws-sdk/hash-node": "^0.1.0-preview.8",
2526
"@types/jest": "^24.0.12",
2627
"@types/node": "^12.0.2",
2728
"jest": "^24.7.1",

packages/s3-request-presigner/src/index.spec.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { S3RequestPresigner } from "./index";
2-
import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3-node";
3-
import { HttpRequest } from "@aws-sdk/types";
1+
import { S3RequestPresigner, S3RequestPresignerOptions } from "./index";
2+
import { HttpRequest } from "@aws-sdk/protocol-http";
3+
import { Hash } from "@aws-sdk/hash-node";
44
import {
55
ALGORITHM_IDENTIFIER,
66
SHA256_HEADER,
@@ -14,33 +14,33 @@ import {
1414
} from "./constants";
1515

1616
describe("s3 presigner", () => {
17-
const s3 = new S3Client({
17+
const s3ResolvedConfig: S3RequestPresignerOptions = {
1818
credentials: {
1919
accessKeyId: "akid",
2020
secretAccessKey: "skey"
2121
},
22-
region: "us-bar-1"
23-
});
22+
region: "us-bar-1",
23+
sha256: Hash.bind(null, "sha256"),
24+
signingName: "s3"
25+
};
2426
const expiration = Math.floor(
2527
(new Date("2000-01-01T00:00:00.000Z").valueOf() + 60 * 60 * 1000) / 1000
2628
);
2729
const presigningOptions = {
2830
signingDate: new Date("2000-01-01T00:00:00.000Z")
2931
};
30-
const minimalRequest: HttpRequest<any> = {
32+
const minimalRequest = new HttpRequest({
3133
method: "GET",
3234
protocol: "https:",
3335
path: "/foo/bar/baz",
3436
headers: {
3537
host: "foo.s3.us-bar-1.amazonaws.com"
3638
},
3739
hostname: "foo.s3.us-bar-1.amazonaws.com"
38-
};
40+
});
3941

4042
it("should not double uri encode the path", async () => {
41-
const signer = new S3RequestPresigner({
42-
...s3.config
43-
});
43+
const signer = new S3RequestPresigner(s3ResolvedConfig);
4444
const signed = await signer.presignRequest(
4545
minimalRequest,
4646
expiration,
@@ -50,9 +50,7 @@ describe("s3 presigner", () => {
5050
});
5151

5252
it("should set the body digest to 'UNSIGNED_PAYLOAD'", async () => {
53-
const signer = new S3RequestPresigner({
54-
...s3.config
55-
});
53+
const signer = new S3RequestPresigner(s3ResolvedConfig);
5654
const signed = await signer.presignRequest(
5755
minimalRequest,
5856
expiration,
@@ -62,9 +60,7 @@ describe("s3 presigner", () => {
6260
});
6361

6462
it("should not change original request", async () => {
65-
const signer = new S3RequestPresigner({
66-
...s3.config
67-
});
63+
const signer = new S3RequestPresigner(s3ResolvedConfig);
6864
const originalRequest = { ...minimalRequest };
6965
const signed = await signer.presignRequest(
7066
minimalRequest,

packages/s3-request-presigner/src/index.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
SignatureV4Init,
99
SignatureV4CryptoInit
1010
} from "@aws-sdk/signature-v4";
11-
import { HttpRequest } from "@aws-sdk/types";
11+
import { HttpRequest as IHttpRequest } from "@aws-sdk/types";
1212
import { UNSIGNED_PAYLOAD, SHA256_HEADER } from "./constants";
1313

1414
/**
@@ -18,28 +18,29 @@ import { UNSIGNED_PAYLOAD, SHA256_HEADER } from "./constants";
1818
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
1919
type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
2020

21+
export type S3RequestPresignerOptions = PartialBy<
22+
SignatureV4Init & SignatureV4CryptoInit,
23+
"service" | "uriEscapePath"
24+
> & { signingName?: string };
25+
2126
export class S3RequestPresigner implements RequestPresigner {
2227
private readonly signer: SignatureV4;
23-
constructor({
24-
service = "s3",
25-
uriEscapePath = false,
26-
...rest
27-
}: PartialBy<
28-
SignatureV4Init & SignatureV4CryptoInit,
29-
"service" | "uriEscapePath"
30-
>) {
31-
this.signer = new SignatureV4({
32-
uriEscapePath,
33-
service,
34-
...rest
35-
});
28+
constructor(options: S3RequestPresignerOptions) {
29+
const resolvedOptions = {
30+
// Allow `signingName` because we want to support usecase of supply client's resolved config
31+
// directly. Where service equals signingName.
32+
service: options.signingName || options.service || "s3",
33+
uriEscapePath: options.uriEscapePath || false,
34+
...options
35+
};
36+
this.signer = new SignatureV4(resolvedOptions);
3637
}
3738

38-
public async presignRequest<StreamType>(
39-
requestToSign: HttpRequest<StreamType>,
39+
public async presignRequest(
40+
requestToSign: IHttpRequest,
4041
expiration: DateInput,
4142
options?: RequestSigningArguments
42-
): Promise<HttpRequest<StreamType>> {
43+
): Promise<IHttpRequest> {
4344
requestToSign.headers[SHA256_HEADER] = UNSIGNED_PAYLOAD;
4445
return this.signer.presignRequest(requestToSign, expiration, options);
4546
}

0 commit comments

Comments
 (0)