Skip to content

Commit

Permalink
fix: replace debug with weald to remove CJS deps (#2648)
Browse files Browse the repository at this point in the history
debug is a CJS module with CJS dependencies.

It will be an ESM module in it's [next major release](debug-js/debug#656), however that release issue has been open for over five years so when that will ship is anyone's guess.

weald is an API compatible port written in TypeScript and published as ESM.

We can revert this change if/when `debug` finally ships an ESM version.
  • Loading branch information
achingbrain authored Aug 2, 2024
1 parent 944935f commit f30e2ee
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
11 changes: 8 additions & 3 deletions packages/logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,21 @@
"dependencies": {
"@libp2p/interface": "^1.6.2",
"@multiformats/multiaddr": "^12.2.3",
"debug": "^4.3.4",
"interface-datastore": "^8.2.11",
"multiformats": "^13.1.0"
"multiformats": "^13.1.0",
"weald": "^1.0.2"
},
"devDependencies": {
"@libp2p/peer-id": "^4.2.2",
"@types/debug": "^4.1.12",
"aegir": "^44.0.1",
"sinon": "^18.0.0",
"uint8arrays": "^5.1.0"
},
"browser": {
"./dist/src/debug/node.js": "./dist/src/debug/browser.js"
},
"react-native": {
"./dist/src/debug/node.js": "./dist/src/debug/browser.js"
},
"sideEffects": false
}
4 changes: 2 additions & 2 deletions packages/logger/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
* ```
*/

import debug from 'debug'
import { base32 } from 'multiformats/bases/base32'
import { base58btc } from 'multiformats/bases/base58'
import { base64 } from 'multiformats/bases/base64'
import debug from 'weald'
import { truncatePeerId } from './utils.js'
import type { PeerId } from '@libp2p/interface'
import type { Multiaddr } from '@multiformats/multiaddr'
Expand Down Expand Up @@ -194,7 +194,7 @@ export function logger (name: string): Logger {
let trace: debug.Debugger = createDisabledLogger(`${name}:trace`)

// look at all the debug names and see if trace logging has explicitly been enabled
if (debug.enabled(`${name}:trace`) && debug.names.map(r => r.toString()).find(n => n.includes(':trace')) != null) {
if (debug.enabled(`${name}:trace`) && debug.names.map((r: any) => r.toString()).find((n: string) => n.includes(':trace')) != null) {
trace = debug(`${name}:trace`)
}

Expand Down
12 changes: 6 additions & 6 deletions packages/logger/test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { peerIdFromString } from '@libp2p/peer-id'
import { multiaddr } from '@multiformats/multiaddr'
import { expect } from 'aegir/chai'
import debug from 'debug'
import { Key } from 'interface-datastore'
import { base32 } from 'multiformats/bases/base32'
import { base58btc } from 'multiformats/bases/base58'
import { base64 } from 'multiformats/bases/base64'
import sinon from 'sinon'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { toString as unint8ArrayToString } from 'uint8arrays/to-string'
import debug from 'weald'
import { logger, peerLogger } from '../src/index.js'

describe('logger', () => {
it('creates a logger', () => {
const log = logger('hello')

expect(log).to.be.a('function')
expect(log).to.a.property('enabled').that.is.not.true()
expect(log).to.have.property('enabled').that.is.not.true()
expect(log).to.have.property('error').that.is.a('function')
expect(log).to.have.nested.property('error.enabled').that.is.not.true()
expect(log).to.have.property('trace').that.is.a('function')
Expand All @@ -29,7 +29,7 @@ describe('logger', () => {
const log = logger.forComponent('hello')

expect(log).to.be.a('function')
expect(log).to.a.property('enabled').that.is.not.true()
expect(log).to.have.property('enabled').that.is.not.true()
expect(log).to.have.property('error').that.is.a('function')
expect(log).to.have.nested.property('error.enabled').that.is.not.true()
expect(log).to.have.property('trace').that.is.a('function')
Expand All @@ -42,7 +42,7 @@ describe('logger', () => {
const log = logger('enabled-logger')

expect(log).to.be.a('function')
expect(log).to.a.property('enabled').that.is.true()
expect(log).to.have.property('enabled').that.is.true()
expect(log).to.have.property('error').that.is.a('function')
expect(log).to.have.nested.property('error.enabled').that.is.not.true()
expect(log).to.have.property('trace').that.is.a('function')
Expand All @@ -55,7 +55,7 @@ describe('logger', () => {
const log = logger('enabled-with-error-logger')

expect(log).to.be.a('function')
expect(log).to.a.property('enabled').that.is.true()
expect(log).to.have.property('enabled').that.is.true()
expect(log).to.have.property('error').that.is.a('function')
expect(log).to.have.nested.property('error.enabled').that.is.true()
expect(log).to.have.property('trace').that.is.a('function')
Expand All @@ -68,7 +68,7 @@ describe('logger', () => {
const log = logger('enabled-with-trace-logger')

expect(log).to.be.a('function')
expect(log).to.a.property('enabled').that.is.true()
expect(log).to.have.property('enabled').that.is.true()
expect(log).to.have.property('error').that.is.a('function')
expect(log).to.have.nested.property('error.enabled').that.is.true()
expect(log).to.have.property('trace').that.is.a('function')
Expand Down

0 comments on commit f30e2ee

Please sign in to comment.