diff --git a/src/internal/client.ts b/src/internal/client.ts index 5122647..642de14 100644 --- a/src/internal/client.ts +++ b/src/internal/client.ts @@ -1,4 +1,4 @@ -import { RefinedResponse, ResponseType } from 'k6/http' +import { RefinedResponse, ResponseType, Params } from 'k6/http' import { AWSConfig } from './config' import { Endpoint } from './endpoint' @@ -27,6 +27,22 @@ export class AWSClient { readonly awsConfig: AWSConfig readonly serviceName: string + // Because jslib-aws is mostly used as a way to setup or feed k6 tests, and + // we want the jslib-aws to be able to disregard k6's discardResponseBodies: meaning + // that for instance, even when setting discardResponseBodies to true in the k6 options, using + // s3.getObject still receives the underlying response body and returns data to the user. + // + // To achieve this, we set the responseType to 'text' in the baseRequestParams, as it + // will lead the http module to ignore the discardResponseBodies option. + // + // AWS Client classes can override this value if they want to receive the response body + // as a different type ('binary' for instance, e.g. S3Client.getObject). + // + // See #45: https://github.com/grafana/k6-jslib-aws/issues/45 + readonly baseRequestParams: Params = { + responseType: 'text', + } + private _endpoint?: Endpoint /** diff --git a/src/internal/event-bridge.ts b/src/internal/event-bridge.ts index 38b0bb7..ef06db6 100644 --- a/src/internal/event-bridge.ts +++ b/src/internal/event-bridge.ts @@ -68,6 +68,7 @@ export class EventBridgeClient extends AWSClient { ) const res = await http.asyncRequest(this.method, signedRequest.url, signedRequest.body, { + ...this.baseRequestParams, headers: signedRequest.headers, }) this.handleError(res, EventBridgeOperation.PutEvents) diff --git a/src/internal/kinesis.ts b/src/internal/kinesis.ts index 6cbc18c..75cbc33 100644 --- a/src/internal/kinesis.ts +++ b/src/internal/kinesis.ts @@ -278,6 +278,7 @@ export class KinesisClient extends AWSClient { ) const res = await http.asyncRequest('POST', signedRequest.url, signedRequest.body, { + ...this.baseRequestParams, headers: signedRequest.headers, }) diff --git a/src/internal/kms.ts b/src/internal/kms.ts index 866bf1c..1d6afe0 100644 --- a/src/internal/kms.ts +++ b/src/internal/kms.ts @@ -67,6 +67,7 @@ export class KMSClient extends AWSClient { ) const res = await http.asyncRequest(this.method, signedRequest.url, signedRequest.body, { + ...this.baseRequestParams, headers: signedRequest.headers, }) this.handleError(res, KMSOperation.ListKeys) @@ -112,6 +113,7 @@ export class KMSClient extends AWSClient { ) const res = await http.asyncRequest(this.method, signedRequest.url, signedRequest.body, { + ...this.baseRequestParams, headers: signedRequest.headers, }) this.handleError(res, KMSOperation.GenerateDataKey) diff --git a/src/internal/lambda.ts b/src/internal/lambda.ts index 4a0639e..1c33f67 100644 --- a/src/internal/lambda.ts +++ b/src/internal/lambda.ts @@ -79,6 +79,7 @@ export class LambdaClient extends AWSClient { ) const res = await http.asyncRequest(this.method, signedRequest.url, signedRequest.body, { + ...this.baseRequestParams, headers: signedRequest.headers, }) this.handleError(res) diff --git a/src/internal/s3.ts b/src/internal/s3.ts index 0ae76c8..905dde1 100644 --- a/src/internal/s3.ts +++ b/src/internal/s3.ts @@ -60,6 +60,7 @@ export class S3Client extends AWSClient { ) const res = await http.asyncRequest(method, signedRequest.url, signedRequest.body || null, { + ...this.baseRequestParams, headers: signedRequest.headers, }) this.handleError(res, 'ListBuckets') @@ -119,6 +120,7 @@ export class S3Client extends AWSClient { ) const res = await http.asyncRequest(method, signedRequest.url, signedRequest.body || null, { + ...this.baseRequestParams, headers: signedRequest.headers, }) this.handleError(res, 'ListObjectsV2') @@ -199,6 +201,7 @@ export class S3Client extends AWSClient { } const res = await http.asyncRequest(method, signedRequest.url, null, { + ...this.baseRequestParams, headers: signedRequest.headers, responseType: responseType as ResponseType, }) @@ -258,6 +261,7 @@ export class S3Client extends AWSClient { ) const res = await http.asyncRequest(method, signedRequest.url, signedRequest.body, { + ...this.baseRequestParams, headers: signedRequest.headers, }) this.handleError(res, 'PutObject') @@ -287,6 +291,7 @@ export class S3Client extends AWSClient { ) const res = await http.asyncRequest(method, signedRequest.url, signedRequest.body || null, { + ...this.baseRequestParams, headers: signedRequest.headers, }) this.handleError(res, 'DeleteObject') @@ -326,6 +331,7 @@ export class S3Client extends AWSClient { ) const res = await http.asyncRequest(method, signedRequest.url, signedRequest.body || null, { + ...this.baseRequestParams, headers: signedRequest.headers, }) @@ -360,6 +366,7 @@ export class S3Client extends AWSClient { ) const res = await http.asyncRequest(method, signedRequest.url, signedRequest.body || null, { + ...this.baseRequestParams, headers: signedRequest.headers, }) this.handleError(res, 'CreateMultipartUpload') @@ -410,6 +417,7 @@ export class S3Client extends AWSClient { ) const res = await http.asyncRequest(method, signedRequest.url, signedRequest.body || null, { + ...this.baseRequestParams, headers: signedRequest.headers, }) this.handleError(res, 'UploadPart') @@ -460,6 +468,7 @@ export class S3Client extends AWSClient { ) const res = await http.asyncRequest(method, signedRequest.url, signedRequest.body || null, { + ...this.baseRequestParams, headers: signedRequest.headers, }) this.handleError(res, 'CompleteMultipartUpload') @@ -494,6 +503,7 @@ export class S3Client extends AWSClient { ) const res = await http.asyncRequest(method, signedRequest.url, signedRequest.body || null, { + ...this.baseRequestParams, headers: signedRequest.headers, }) this.handleError(res, 'AbortMultipartUpload') diff --git a/src/internal/secrets-manager.ts b/src/internal/secrets-manager.ts index 1377565..cd5967f 100644 --- a/src/internal/secrets-manager.ts +++ b/src/internal/secrets-manager.ts @@ -68,6 +68,7 @@ export class SecretsManagerClient extends AWSClient { ) const res = await http.asyncRequest(this.method, signedRequest.url, signedRequest.body, { + ...this.baseRequestParams, headers: signedRequest.headers, }) this.handleError(res, SecretsManagerOperation.ListSecrets) @@ -100,6 +101,7 @@ export class SecretsManagerClient extends AWSClient { ) const res = await http.asyncRequest(this.method, signedRequest.url, signedRequest.body, { + ...this.baseRequestParams, headers: signedRequest.headers, }) @@ -160,6 +162,7 @@ export class SecretsManagerClient extends AWSClient { // headers['X-Amz-Target'] = `${this.serviceName}.CreateSecret` const res = await http.asyncRequest(this.method, signedRequest.url, signedRequest.body, { + ...this.baseRequestParams, headers: signedRequest.headers, }) this.handleError(res, SecretsManagerOperation.CreateSecret) @@ -200,6 +203,7 @@ export class SecretsManagerClient extends AWSClient { ) const res = await http.asyncRequest(this.method, signedRequest.url, signedRequest.body, { + ...this.baseRequestParams, headers: signedRequest.headers, }) this.handleError(res, SecretsManagerOperation.PutSecretValue) @@ -249,6 +253,7 @@ export class SecretsManagerClient extends AWSClient { ) const res = await http.asyncRequest(this.method, signedRequest.url, signedRequest.body, { + ...this.baseRequestParams, headers: signedRequest.headers, }) this.handleError(res, SecretsManagerOperation.DeleteSecret) diff --git a/src/internal/sqs.ts b/src/internal/sqs.ts index 35c8f77..51f945c 100644 --- a/src/internal/sqs.ts +++ b/src/internal/sqs.ts @@ -207,6 +207,7 @@ export class SQSClient extends AWSClient { ) const res = await http.asyncRequest('POST', signedRequest.url, signedRequest.body, { + ...this.baseRequestParams, headers: signedRequest.headers, }) diff --git a/src/internal/ssm.ts b/src/internal/ssm.ts index d866d21..76c0303 100644 --- a/src/internal/ssm.ts +++ b/src/internal/ssm.ts @@ -71,6 +71,7 @@ export class SystemsManagerClient extends AWSClient { ) const res = await http.asyncRequest(this.method, signedRequest.url, signedRequest.body, { + ...this.baseRequestParams, headers: signedRequest.headers, }) this.handleError(res, SystemsManagerOperation.GetParameter)