Skip to content

Commit

Permalink
fix: correctly imports commonjs "debug" package (#351)
Browse files Browse the repository at this point in the history
Fix for #350 

Changes the imports, so they handle the CommonJS exports from the
"debug" package.

---------

Co-authored-by: Artem Zakharchenko <[email protected]>
  • Loading branch information
thebuilder and kettanaito authored Mar 3, 2023
1 parent fdf1e4b commit 1e2ffd9
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/Interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Debugger, debug } from 'debug'
import { Listener } from 'strict-event-emitter'
import { AsyncEventEmitter } from './utils/AsyncEventEmitter'
import { nextTick } from './utils/nextTick'
import { debug, Debugger } from './utils/debug'

export type InterceptorEventMap = Record<string, any>
export type InterceptorSubscription = () => void
Expand Down
2 changes: 1 addition & 1 deletion src/interceptors/ClientRequest/NodeClientRequest.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { vi, it, expect, beforeAll, afterAll } from 'vitest'
import { debug } from 'debug'
import express from 'express'
import { IncomingMessage } from 'http'
import { HttpServer } from '@open-draft/test-server/http'
Expand All @@ -10,6 +9,7 @@ import { normalizeClientRequestArgs } from './utils/normalizeClientRequestArgs'
import { AsyncEventEmitter } from '../../utils/AsyncEventEmitter'
import { sleep } from '../../../test/helpers'
import { HttpRequestEventMap } from '../../glossary'
import { debug } from '../../utils/debug'

interface ErrorConnectionRefused extends NodeJS.ErrnoException {
address: string
Expand Down
2 changes: 1 addition & 1 deletion src/interceptors/ClientRequest/http.request.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { debug } from 'debug'
import { ClientRequest } from 'http'
import {
NodeClientOptions,
Expand All @@ -9,6 +8,7 @@ import {
normalizeClientRequestArgs,
ClientRequestArgs,
} from './utils/normalizeClientRequestArgs'
import { debug } from '../../utils/debug'

const log = debug('http request')

Expand Down
2 changes: 1 addition & 1 deletion src/interceptors/ClientRequest/utils/createRequest.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { it, expect } from 'vitest'
import { debug } from 'debug'
import { HttpRequestEventMap } from '../../..'
import { AsyncEventEmitter } from '../../../utils/AsyncEventEmitter'
import { NodeClientRequest } from '../NodeClientRequest'
import { createRequest } from './createRequest'
import { debug } from '../../../utils/debug'

const emitter = new AsyncEventEmitter<HttpRequestEventMap>()
const log = debug('test')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { debug } from 'debug'
import { IncomingMessage } from 'http'
import { PassThrough } from 'stream'
import * as zlib from 'zlib'
import { debug } from '../../../utils/debug'

const log = debug('http getIncomingMessageBody')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { debug } from 'debug'
import {
Agent as HttpAgent,
globalAgent as httpGlobalAgent,
Expand All @@ -17,6 +16,7 @@ import {
} from '../../../utils/getUrlByRequestOptions'
import { cloneObject } from '../../../utils/cloneObject'
import { isObject } from '../../../utils/isObject'
import { debug } from '../../../utils/debug'

const log = debug('http normalizeClientRequestArgs')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const debug = require('debug')('http normalizeClientRequestEndArgs')
import { debug } from '../../../utils/debug'

const log = debug('utils getUrlByRequestOptions')

export type ClientRequestEndChunk = string | Buffer
export type ClientRequestEndCallback = () => void
Expand All @@ -22,7 +24,7 @@ type NormalizedHttpRequestEndParams = [
export function normalizeClientRequestEndArgs(
...args: HttpRequestEndArgs
): NormalizedHttpRequestEndParams {
debug('arguments', args)
log('arguments', args)
const normalizedArgs = new Array(3)
.fill(null)
.map((value, index) => args[index] || value)
Expand All @@ -46,6 +48,6 @@ export function normalizeClientRequestEndArgs(
return 0
})

debug('normalized args', normalizedArgs)
log('normalized args', normalizedArgs)
return normalizedArgs as NormalizedHttpRequestEndParams
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { debug } from 'debug'
import { debug } from '../../../utils/debug'

const log = debug('http normalizeWriteArgs')

Expand Down
2 changes: 1 addition & 1 deletion src/utils/AsyncEventEmitter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Debugger, debug } from 'debug'
import { Emitter, EventMap, Listener } from 'strict-event-emitter'
import { nextTick } from './nextTick'
import { debug, Debugger } from './debug'

export interface QueueItem<Args extends Array<unknown>> {
args: Args
Expand Down
14 changes: 8 additions & 6 deletions src/utils/cloneObject.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
const debug = require('debug')('cloneObject')
import { debug } from './debug'

const log = debug('cloneObject')

function isPlainObject(obj?: Record<string, any>): boolean {
debug('is plain object?', obj)
log('is plain object?', obj)

if (obj == null || !obj.constructor?.name) {
debug('given object is undefined, not a plain object...')
log('given object is undefined, not a plain object...')
return false
}

debug('checking the object constructor:', obj.constructor.name)
log('checking the object constructor:', obj.constructor.name)
return obj.constructor.name === 'Object'
}

export function cloneObject<ObjectType extends Record<string, any>>(
obj: ObjectType
): ObjectType {
debug('cloning object:', obj)
log('cloning object:', obj)

const enumerableProperties = Object.entries(obj).reduce<Record<string, any>>(
(acc, [key, value]) => {
debug('analyzing key-value pair:', key, value)
log('analyzing key-value pair:', key, value)

// Recursively clone only plain objects, omitting class instances.
acc[key] = isPlainObject(value) ? cloneObject(value) : value
Expand Down
4 changes: 4 additions & 0 deletions src/utils/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type { Debugger } from 'debug'
import debugCjsImport from 'debug'

export const debug = debugCjsImport
25 changes: 13 additions & 12 deletions src/utils/getUrlByRequestOptions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Agent } from 'http'
import { RequestOptions, Agent as HttpsAgent } from 'https'
import { debug } from './debug'

const debug = require('debug')('utils getUrlByRequestOptions')
const log = debug('utils getUrlByRequestOptions')

// Request instance constructed by the "request" library
// has a "self" property that has a "uri" field. This is
Expand Down Expand Up @@ -123,43 +124,43 @@ function getHostname(host: string, port?: number): string {
* Creates a `URL` instance from a given `RequestOptions` object.
*/
export function getUrlByRequestOptions(options: ResolvedRequestOptions): URL {
debug('request options', options)
log('request options', options)

if (options.uri) {
debug(
log(
'constructing url from explicitly provided "options.uri": %s',
options.uri
)
return new URL(options.uri.href)
}

debug('figuring out url from request options...')
log('figuring out url from request options...')

const protocol = getProtocolByRequestOptions(options)
debug('protocol', protocol)
log('protocol', protocol)

const host = getHostByRequestOptions(options)
debug('host', host)
log('host', host)

const port = getPortByRequestOptions(options)
debug('port', port)
log('port', port)

const hostname = getHostname(host, port)
debug('hostname', hostname)
log('hostname', hostname)

const path = options.path || DEFAULT_PATH
debug('path', path)
log('path', path)

const credentials = getAuthByRequestOptions(options)
debug('credentials', credentials)
log('credentials', credentials)

const authString = credentials
? `${credentials.username}:${credentials.password}@`
: ''
debug('auth string:', authString)
log('auth string:', authString)

const url = new URL(`${protocol}//${authString}${hostname}${path}`)
debug('created url:', url)
log('created url:', url)

return url
}

0 comments on commit 1e2ffd9

Please sign in to comment.