Skip to content

Commit

Permalink
Add ESLint typescript configuration and address warnings in src folder
Browse files Browse the repository at this point in the history
  • Loading branch information
oleiade committed Nov 17, 2023
1 parent 6bf3253 commit 49eb388
Show file tree
Hide file tree
Showing 12 changed files with 1,238 additions and 112 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.cache/
public/
node_modules/
1,071 changes: 1,070 additions & 1 deletion package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
"@types/k6": "^0.45.0",
"@types/uuid": "^3.4.0",
"@types/webpack": "^5.28.0",
"@typescript-eslint/eslint-plugin": "^6.11.0",
"@typescript-eslint/parser": "^6.11.0",
"babel-loader": "^8.0.6",
"chai": "4.3.4",
"clean-webpack-plugin": "^4.0.0",
"copy-webpack-plugin": "^10.2.4",
"eslint": "^8.53.0",
"terser-webpack-plugin": "^5.3.1",
"typescript": "^4.6.3",
"typescript": "^4.9.5",
"uuid": "^3.4.0",
"webpack": "^5.72.0",
"webpack-cli": "^4.9.2",
Expand Down
12 changes: 12 additions & 0 deletions src/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* As per typescript-eslint docs, this is the configuration recommended
* by the k6 frontend team.
*
* See: https://typescript-eslint.io/getting-started/
*/
{
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"root": true
}
13 changes: 9 additions & 4 deletions src/internal/error.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { JSONObject } from './json'
import { parseHTML } from 'k6/html'
import { Response } from 'k6/http'

Expand Down Expand Up @@ -39,12 +40,16 @@ export class AWSError extends Error {

static parse(response: Response): AWSError {
if (response.headers['Content-Type'] === 'application/json') {
const error = response.json() as any;
const message = error.Message || error.message || error.__type || 'An error occurred on the server side';
const error = (response.json() as JSONObject) || {}
const message =
error.Message ||
error.message ||
error.__type ||
'An error occurred on the server side'
const code = response.headers['X-Amzn-Errortype'] || error.__type
return new AWSError(message, code)
return new AWSError(message as string, code as string)
} else {
return AWSError.parseXML(response.body as string);
return AWSError.parseXML(response.body as string)
}
}
}
115 changes: 66 additions & 49 deletions src/internal/kinesis.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { JSONObject } from 'k6'
import http, { RefinedResponse, ResponseType } from 'k6/http'

import { AWSClient } from './client'

import { AWSConfig } from './config'
import { AMZ_TARGET_HEADER } from './constants'
import { AWSError } from './error'
import { JSONObject } from './json'
import { HTTPHeaders } from './http'
import { InvalidSignatureError, SignatureV4 } from './signature'

Expand Down Expand Up @@ -82,7 +82,7 @@ export class KinesisClient extends AWSClient {
streamName: string,
options: { shardCount?: number; streamModeDetails?: { streamMode: StreamMode } } = {}
): Promise<void> {
const body: any = {
const body = {
StreamName: streamName,
...(options.shardCount && { ShardCount: options.shardCount }),
...(options.streamModeDetails && { StreamMode: options.streamModeDetails.streamMode }),
Expand All @@ -109,7 +109,7 @@ export class KinesisClient extends AWSClient {
streamName: string,
parameters: { streamARN?: string; enforceConsumerDeletion?: boolean } = {}
): Promise<void> {
const body: any = {
const body = {
StreamName: streamName,
...(parameters.streamARN && { StreamARN: parameters.streamARN }),
...(parameters.enforceConsumerDeletion && {
Expand Down Expand Up @@ -142,7 +142,7 @@ export class KinesisClient extends AWSClient {
nextToken?: string
} = {}
): Promise<ListStreamsResponse> {
const body: any = {
const body = {
...(parameters.exclusiveStartStreamName && {
ExclusiveStartStreamName: parameters.exclusiveStartStreamName,
}),
Expand All @@ -151,7 +151,7 @@ export class KinesisClient extends AWSClient {
}

const res = await this._send_request('ListStreams', body)
return ListStreamsResponse.fromJson(res?.json())
return ListStreamsResponse.fromJson(res?.json() as JSONObject)
}

/**
Expand All @@ -171,14 +171,14 @@ export class KinesisClient extends AWSClient {
throw new Error('Either streamName or streamARN must be provided')
}

const body: any = {
const body = {
Records: records,
...(parameters.streamName && { StreamName: parameters.streamName }),
...(parameters.streamARN && { StreamARN: parameters.streamARN }),
}

const res = await this._send_request('PutRecords', body)
return PutRecordsResponse.fromJson(res?.json())
return PutRecordsResponse.fromJson(res?.json() as JSONObject)
}

/**
Expand All @@ -194,14 +194,14 @@ export class KinesisClient extends AWSClient {
shardIterator: string,
parameters: { limit?: number; streamARN?: string } = {}
): Promise<GetRecordsResponse> {
const body: any = {
const body = {
ShardIterator: shardIterator,
...(parameters.limit && { Limit: parameters.limit }),
...(parameters.streamARN && { StreamARN: parameters.streamARN }),
}

const res = await this._send_request('GetRecords', body)
return GetRecordsResponse.fromJson(res?.json())
return GetRecordsResponse.fromJson(res?.json() as JSONObject)
}

/**
Expand All @@ -222,7 +222,7 @@ export class KinesisClient extends AWSClient {
streamName: string,
parameters: { nextToken?: string; maxResults?: number } = {}
): Promise<ListShardsResponse> {
const body: any = {
const body = {
StreamName: streamName,
...(parameters.nextToken && { NextToken: parameters.nextToken }),
...(parameters.maxResults && {
Expand All @@ -231,7 +231,7 @@ export class KinesisClient extends AWSClient {
}

const res = await this._send_request('ListShards', body)
return ListShardsResponse.fromJson(res?.json())
return ListShardsResponse.fromJson(res?.json() as JSONObject)
}

/**
Expand All @@ -256,7 +256,7 @@ export class KinesisClient extends AWSClient {
shardIteratorType: ShardIteratorKind,
parameters: { startingSequenceNumber?: string; timestamp?: number } = {}
): Promise<GetShardIteratorResponse> {
const body: any = {
const body = {
StreamName: streamName,
ShardId: shardId,
ShardIteratorType: shardIteratorType,
Expand All @@ -267,10 +267,13 @@ export class KinesisClient extends AWSClient {
}

const res = await this._send_request('GetShardIterator', body)
return GetShardIteratorResponse.fromJson(res?.json())
return GetShardIteratorResponse.fromJson(res?.json() as JSONObject)
}

private async _send_request(action: string, body: any): Promise<any> {
private async _send_request<R extends ResponseType>(
action: string,
body: unknown
): Promise<RefinedResponse<R>> {
const signedRequest = this.signature.sign(
{
method: 'POST',
Expand Down Expand Up @@ -410,7 +413,7 @@ export class ListStreamsResponse {
this.streamSummaries = StreamSummaries
}

static fromJson(result: any): ListStreamsResponse {
static fromJson(result: JSONObject): ListStreamsResponse {
const {
HasMoreStreams = false,
NextToken = '',
Expand All @@ -419,10 +422,10 @@ export class ListStreamsResponse {
} = result

return new ListStreamsResponse(
HasMoreStreams,
NextToken,
StreamNames.map((s: any) => String(s)),
StreamSummaries.map(StreamSummary.fromJson)
HasMoreStreams as boolean,
NextToken as string,
StreamNames as string[],
(StreamSummaries as JSONObject[])?.map(StreamSummary.fromJson) as StreamSummary[]
)
}
}
Expand Down Expand Up @@ -470,7 +473,7 @@ export class StreamSummary {
this.streamStatus = StreamStatus
}

static fromJson(summary: any): StreamSummary {
static fromJson(summary: JSONObject): StreamSummary {
const {
StreamARN = '',
StreamCreationTimestamp = 0,
Expand All @@ -480,11 +483,11 @@ export class StreamSummary {
} = summary

return new StreamSummary(
StreamARN,
StreamCreationTimestamp,
StreamModeDetails,
StreamName,
StreamStatus
StreamARN as string,
StreamCreationTimestamp as number,
StreamModeDetails as StreamModeDetails,
StreamName as string,
StreamStatus as StreamStatus
)
}
}
Expand All @@ -511,7 +514,7 @@ export class PutRecordsResponse {
* - NONE: Do not encrypt the records.
* - KMS: Use server-side encryption on the records using a customer-managed AWS KMS key.
*/
encryptionType: 'NONE' | 'KMS'
encryptionType: EncryptionType

/**
* The number of unsuccessfully processed records in a PutRecords request.
Expand All @@ -533,14 +536,20 @@ export class PutRecordsResponse {
this.records = records
}

static fromJson(json: any): PutRecordsResponse {
static fromJson(json: JSONObject): PutRecordsResponse {
const { EncryptionType = 'NONE', FailedRecordCount = 0, Records = [] } = json
const records = Records.map((record: any) => PutRecordsResultEntry.fromJson(record))
const records = (Records as JSONObject[]).map(PutRecordsResultEntry.fromJson)

return new PutRecordsResponse(EncryptionType, FailedRecordCount, records)
return new PutRecordsResponse(
EncryptionType as EncryptionType,
FailedRecordCount as number,
records
)
}
}

type EncryptionType = 'NONE' | 'KMS'

/**
* Represents the result of an individual record from a PutRecords request.
*/
Expand All @@ -560,8 +569,8 @@ export class PutRecordsResultEntry {
this.shardId = shardId
}

static fromJson(json: any): PutRecordsResultEntry {
return new PutRecordsResultEntry(json.SequenceNumber, json.ShardId)
static fromJson(json: JSONObject): PutRecordsResultEntry {
return new PutRecordsResultEntry(json.SequenceNumber as string, json.ShardId as string)
}
}

Expand All @@ -583,8 +592,8 @@ export class GetRecordsResponse {
* The number of milliseconds the GetRecords response is from the
* tip of the stream, indicating how far behind current time the
* consumer is.
*
* A value of zero indicates that record processing is caught
*
* A value of zero indicates that record processing is caught
* up, and there are no new records to process at this moment.
*/
millisBehindLatest: number
Expand All @@ -595,11 +604,15 @@ export class GetRecordsResponse {
this.millisBehindLatest = millisBehindLatest
}

static fromJson(json: any): GetRecordsResponse {
static fromJson(json: JSONObject): GetRecordsResponse {
const { NextShardIterator = '', Records = [], MillisBehindLatest = 0 } = json
const records = Records.map((record: Record) => Record.fromJson(record))
const records = (Records as JSONObject[]).map(Record.fromJson)

return new GetRecordsResponse(NextShardIterator, records, MillisBehindLatest)
return new GetRecordsResponse(
NextShardIterator as string,
records as Record[],
MillisBehindLatest as number
)
}
}

Expand Down Expand Up @@ -629,8 +642,12 @@ class Record {
this.sequenceNumber = sequenceNumber
}

static fromJson(json: any): Record {
return new Record(json.Data, json.PartitionKey, json.SequenceNumber)
static fromJson(json: JSONObject): Record {
return new Record(
json.Data as string | ArrayBuffer,
json.PartitionKey as string,
json.SequenceNumber as string
)
}
}

Expand All @@ -657,11 +674,11 @@ export class ListShardsResponse {
this.nextToken = nextToken
}

static fromJson(json: any): ListShardsResponse {
static fromJson(json: JSONObject): ListShardsResponse {
const { Shards = [], NextToken } = json
const shards = Shards.map((shard: Shard) => Shard.fromJson(shard))
const shards = (Shards as JSONObject[]).map(Shard.fromJson)

return new ListShardsResponse(shards, NextToken)
return new ListShardsResponse(shards, NextToken as string | undefined)
}
}

Expand Down Expand Up @@ -705,13 +722,13 @@ export class Shard {
this.sequenceNumberRange = sequenceNumberRange
}

static fromJson(json: any): Shard {
static fromJson(json: JSONObject): Shard {
return new Shard(
json.ShardId,
json.HashKeyRange,
json.SequenceNumberRange,
json.ParentShardId,
json.AdjacentParentShardId
json.ShardId as string,
json.HashKeyRange as unknown as HashKeyRange,
json.SequenceNumberRange as unknown as SequenceNumberRange,
json.ParentShardId as string | undefined,
json.AdjacentParentShardId as string | undefined
)
}
}
Expand Down Expand Up @@ -762,7 +779,7 @@ class GetShardIteratorResponse {
this.shardIterator = shardIterator
}

static fromJson(json: any): GetShardIteratorResponse {
return new GetShardIteratorResponse(json.ShardIterator)
static fromJson(json: JSONObject): GetShardIteratorResponse {
return new GetShardIteratorResponse(json.ShardIterator as string)
}
}
Loading

0 comments on commit 49eb388

Please sign in to comment.