Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Honoring scheme and bucket pathing #44

Merged
merged 2 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/aws.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/aws.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/kms.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/kms.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/s3.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/s3.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/secrets-manager.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/secrets-manager.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/signature.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/sqs.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/sqs.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/ssm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/ssm.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 22 additions & 3 deletions src/internal/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AWSConfig } from './config'
import { HTTPHeaders } from './http'

import { HTTPScheme } from './http'
/**
* Class allowing to build requests targeting AWS APIs
*
Expand All @@ -13,7 +13,7 @@ export class AWSClient {
serviceName: string

private _host?: string

private _scheme?: HTTPScheme
/**
* @param {AWSConfig} awsConfig - configuration attributes to use when interacting with AWS' APIs
* @param {string} serviceName - name of the service to target.
Expand All @@ -30,14 +30,33 @@ export class AWSClient {
*/
public get host() {
if (this._host == undefined) {
return `${this.serviceName}.${this.awsConfig.region}.${this.awsConfig.endpoint}`
this._host = `${this.serviceName}.${this.awsConfig.region}.${this.awsConfig.endpoint}`
}
return this._host
}

public set host(host: string) {
this._host = host
}

/**
* Property computing the scheme to use http or https. Defaults to https as per AWSConfig Defaults
* the specific AWS service the child class implements the functionalities of.
*/

public get scheme() {

if (this._scheme == undefined) {
this._scheme = this.awsConfig.scheme;
}
return this._scheme
}

// Validatiuon should be done by the type declaration
public set scheme(scheme: HTTPScheme) {
this._scheme = scheme
}

}

/**
Expand Down
29 changes: 15 additions & 14 deletions src/internal/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { AWSError } from './error'
import { SignedHTTPRequest } from './http'
import { InvalidSignatureError, SignatureV4 } from './signature'


/** Class allowing to interact with Amazon AWS's S3 service */
export class S3Client extends AWSClient {
signature: SignatureV4
Expand Down Expand Up @@ -48,7 +49,7 @@ export class S3Client extends AWSClient {
const signedRequest: SignedHTTPRequest = this.signature.sign(
{
method: 'GET',
protocol: 'https',
protocol: this.scheme,
hostname: this.host,
path: '/',
headers: {},
Expand Down Expand Up @@ -101,14 +102,14 @@ export class S3Client extends AWSClient {
listObjects(bucketName: string, prefix?: string): Array<S3Object> {
// Prepare request
const method = 'GET'
const host = `${bucketName}.${this.host}`
const host = `${this.host}`

const signedRequest: SignedHTTPRequest = this.signature.sign(
{
method: 'GET',
protocol: 'https',
protocol: this.scheme,
hostname: host,
path: '/',
path: `/${bucketName}/`,
query: {
'list-type': '2',
prefix: prefix || '',
Expand Down Expand Up @@ -170,14 +171,14 @@ export class S3Client extends AWSClient {
getObject(bucketName: string, objectKey: string): S3Object {
// Prepare request
const method = 'GET'
const host = `${bucketName}.${this.host}`
const host = `${this.host}`

const signedRequest = this.signature.sign(
{
method: 'GET',
protocol: 'https',
protocol: this.scheme,
hostname: host,
path: `/${objectKey}`,
path: `/${bucketName}/${objectKey}`,
headers: {},
},
{}
Expand Down Expand Up @@ -215,14 +216,14 @@ export class S3Client extends AWSClient {
putObject(bucketName: string, objectKey: string, data: string | ArrayBuffer) {
// Prepare request
const method = 'PUT'
const host = `${bucketName}.${this.host}`

const host = `${this.host}`
const signedRequest = this.signature.sign(
{
method: method,
protocol: 'https',
protocol: this.scheme,
hostname: host,
path: `/${objectKey}`,
path: `/${bucketName}/${objectKey}`,
headers: {
'Host': host,
},
Expand All @@ -249,14 +250,14 @@ export class S3Client extends AWSClient {
deleteObject(bucketName: string, objectKey: string): void {
// Prepare request
const method = 'DELETE'
const host = `${bucketName}.${this.host}`
const host = `${this.host}`

const signedRequest = this.signature.sign(
{
method: method,
protocol: 'https',
protocol: this.scheme,
hostname: host,
path: `/${objectKey}`,
path: `/${bucketName}/${objectKey}`,
headers: {},
},
{}
Expand Down
3 changes: 2 additions & 1 deletion tests/internal/s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AWSConfig, S3Client, S3ServiceError } from '../../build/s3.js'
export function s3TestSuite(data) {
const s3Client = new S3Client(data.awsConfig)
s3Client.host = `s3.${data.awsConfig.endpoint}`
s3Client.scheme = `https`

describe('list buckets', () => {
// Act
Expand Down Expand Up @@ -94,4 +95,4 @@ export function s3TestSuite(data) {
data.s3.testObjects[2].key,
data.s3.testObjects[2].body
)
}
}