Skip to content
Closed
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
12 changes: 11 additions & 1 deletion src/internal/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as crypto from 'node:crypto'

Check failure on line 1 in src/internal/client.ts

View workflow job for this annotation

GitHub Actions / lint

Run autofix to sort these imports!
import * as fs from 'node:fs'
import type { IncomingHttpHeaders } from 'node:http'
import * as http from 'node:http'
Expand Down Expand Up @@ -128,6 +128,7 @@
uploadPartParser,
} from './xml-parser.ts'
import * as xmlParsers from './xml-parser.ts'
import { Logger } from './logger.ts'

Check failure on line 131 in src/internal/client.ts

View workflow job for this annotation

GitHub Actions / lint

All imports in the declaration are only used as types. Use `import type`

const xml = new xml2js.Builder({ renderOpts: { pretty: false }, headless: true })

Expand Down Expand Up @@ -186,6 +187,7 @@
port?: number
region?: Region
transport?: Transport
logger?: Logger
sessionToken?: string
partSize?: number
pathStyle?: boolean
Expand Down Expand Up @@ -222,6 +224,7 @@

export class TypedClient {
protected transport: Transport
protected logger: Logger
protected host: string
protected port: number
protected protocol: string
Expand Down Expand Up @@ -320,6 +323,12 @@
transportAgent = params.transportAgent
}

if (params.logger) {
this.logger = params.logger
} else {
this.logger = console
}

// User Agent should always following the below style.
// Please open an issue to discuss any new changes here.
//
Expand Down Expand Up @@ -770,6 +779,7 @@
this.retryOptions.disableRetry === true ? 0 : this.retryOptions.maximumRetryCount,
this.retryOptions.baseDelayMs,
this.retryOptions.maximumDelayMs,
this.logger,
)
if (!response.statusCode) {
throw new Error("BUG: response doesn't have a statusCode")
Expand Down Expand Up @@ -2403,7 +2413,7 @@

const res = await this.makeRequestAsync({ method, bucketName, objectName, query }, payload)
const body = await readAsBuffer(res)
return parseSelectObjectContentResponse(body)
return parseSelectObjectContentResponse(body, this.logger)
}

private async applyBucketLifecycle(bucketName: string, policyConfig: LifeCycleConfigParam): Promise<void> {
Expand Down
8 changes: 8 additions & 0 deletions src/internal/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Logger provides a common interface for logging.
* This interface is compatible with the default `console` logger.
*/
export interface Logger {
warn(message: string, ...optionalParams: never[]): void
log(message: string, ...optionalParams: never[]): void
}
5 changes: 3 additions & 2 deletions src/internal/request.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type * as http from 'node:http'

Check failure on line 1 in src/internal/request.ts

View workflow job for this annotation

GitHub Actions / lint

Run autofix to sort these imports!
import type * as https from 'node:https'
import type * as stream from 'node:stream'
import { pipeline } from 'node:stream'
import { promisify } from 'node:util'

import type { Transport } from './type.ts'
import { Logger } from './logger.ts'

Check failure on line 8 in src/internal/request.ts

View workflow job for this annotation

GitHub Actions / lint

All imports in the declaration are only used as types. Use `import type`

const pipelineAsync = promisify(pipeline)

Expand Down Expand Up @@ -65,6 +66,7 @@
maxRetries: number = MAX_RETRIES,
baseDelayMs: number = BASE_DELAY_MS,
maximumDelayMs: number = MAX_DELAY_MS,
logger: Logger,
): Promise<http.IncomingMessage> {
let attempt = 0
let isRetryable = false
Expand All @@ -87,8 +89,7 @@
throw new Error(`Request failed after ${maxRetries} retries: ${err}`)
}
const delay = getExpBackOffDelay(attempt, baseDelayMs, maximumDelayMs)
// eslint-disable-next-line no-console
console.warn(
logger.warn(
`${new Date().toLocaleString()} Retrying request (attempt ${attempt}/${maxRetries}) after ${delay}ms due to: ${err}`,
)
await sleep(delay)
Expand Down
8 changes: 4 additions & 4 deletions src/internal/xml-parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type * as http from 'node:http'

Check failure on line 1 in src/internal/xml-parser.ts

View workflow job for this annotation

GitHub Actions / lint

Run autofix to sort these imports!
import type stream from 'node:stream'

import crc32 from 'buffer-crc32'
Expand All @@ -22,6 +22,7 @@
Tags,
} from './type.ts'
import { RETENTION_VALIDITY_UNITS } from './type.ts'
import { Logger } from './logger.ts'

Check failure on line 25 in src/internal/xml-parser.ts

View workflow job for this annotation

GitHub Actions / lint

All imports in the declaration are only used as types. Use `import type`

// parse XML response for bucket region
export function parseBucketRegion(xml: string): string {
Expand Down Expand Up @@ -459,7 +460,7 @@
return Buffer.from(stream.read(bodyLen)).toString()
}

export function parseSelectObjectContentResponse(res: Buffer) {
export function parseSelectObjectContentResponse(res: Buffer, logger: Logger) {
const selectResults = new SelectResults({}) // will be returned

const responseStream = readableStream(res) // convert byte array to a readable responseStream
Expand Down Expand Up @@ -577,9 +578,8 @@
default: {
// Continuation message: Not sure if it is supported. did not find a reference or any message in response.
// It does not have a payload.
const warningMessage = `Un implemented event detected ${messageType}.`
// eslint-disable-next-line no-console
console.warn(warningMessage)
const warningMessage = `Unimplemented event detected ${messageType}.`
logger.warn(warningMessage)
}
}
}
Expand Down
Loading