Skip to content

Commit f30e2ee

Browse files
authored
fix: replace debug with weald to remove CJS deps (#2648)
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.
1 parent 944935f commit f30e2ee

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

packages/logger/package.json

+8-3
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,21 @@
5656
"dependencies": {
5757
"@libp2p/interface": "^1.6.2",
5858
"@multiformats/multiaddr": "^12.2.3",
59-
"debug": "^4.3.4",
6059
"interface-datastore": "^8.2.11",
61-
"multiformats": "^13.1.0"
60+
"multiformats": "^13.1.0",
61+
"weald": "^1.0.2"
6262
},
6363
"devDependencies": {
6464
"@libp2p/peer-id": "^4.2.2",
65-
"@types/debug": "^4.1.12",
6665
"aegir": "^44.0.1",
6766
"sinon": "^18.0.0",
6867
"uint8arrays": "^5.1.0"
6968
},
69+
"browser": {
70+
"./dist/src/debug/node.js": "./dist/src/debug/browser.js"
71+
},
72+
"react-native": {
73+
"./dist/src/debug/node.js": "./dist/src/debug/browser.js"
74+
},
7075
"sideEffects": false
7176
}

packages/logger/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
* ```
3333
*/
3434

35-
import debug from 'debug'
3635
import { base32 } from 'multiformats/bases/base32'
3736
import { base58btc } from 'multiformats/bases/base58'
3837
import { base64 } from 'multiformats/bases/base64'
38+
import debug from 'weald'
3939
import { truncatePeerId } from './utils.js'
4040
import type { PeerId } from '@libp2p/interface'
4141
import type { Multiaddr } from '@multiformats/multiaddr'
@@ -194,7 +194,7 @@ export function logger (name: string): Logger {
194194
let trace: debug.Debugger = createDisabledLogger(`${name}:trace`)
195195

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

packages/logger/test/index.spec.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import { peerIdFromString } from '@libp2p/peer-id'
22
import { multiaddr } from '@multiformats/multiaddr'
33
import { expect } from 'aegir/chai'
4-
import debug from 'debug'
54
import { Key } from 'interface-datastore'
65
import { base32 } from 'multiformats/bases/base32'
76
import { base58btc } from 'multiformats/bases/base58'
87
import { base64 } from 'multiformats/bases/base64'
98
import sinon from 'sinon'
109
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
1110
import { toString as unint8ArrayToString } from 'uint8arrays/to-string'
11+
import debug from 'weald'
1212
import { logger, peerLogger } from '../src/index.js'
1313

1414
describe('logger', () => {
1515
it('creates a logger', () => {
1616
const log = logger('hello')
1717

1818
expect(log).to.be.a('function')
19-
expect(log).to.a.property('enabled').that.is.not.true()
19+
expect(log).to.have.property('enabled').that.is.not.true()
2020
expect(log).to.have.property('error').that.is.a('function')
2121
expect(log).to.have.nested.property('error.enabled').that.is.not.true()
2222
expect(log).to.have.property('trace').that.is.a('function')
@@ -29,7 +29,7 @@ describe('logger', () => {
2929
const log = logger.forComponent('hello')
3030

3131
expect(log).to.be.a('function')
32-
expect(log).to.a.property('enabled').that.is.not.true()
32+
expect(log).to.have.property('enabled').that.is.not.true()
3333
expect(log).to.have.property('error').that.is.a('function')
3434
expect(log).to.have.nested.property('error.enabled').that.is.not.true()
3535
expect(log).to.have.property('trace').that.is.a('function')
@@ -42,7 +42,7 @@ describe('logger', () => {
4242
const log = logger('enabled-logger')
4343

4444
expect(log).to.be.a('function')
45-
expect(log).to.a.property('enabled').that.is.true()
45+
expect(log).to.have.property('enabled').that.is.true()
4646
expect(log).to.have.property('error').that.is.a('function')
4747
expect(log).to.have.nested.property('error.enabled').that.is.not.true()
4848
expect(log).to.have.property('trace').that.is.a('function')
@@ -55,7 +55,7 @@ describe('logger', () => {
5555
const log = logger('enabled-with-error-logger')
5656

5757
expect(log).to.be.a('function')
58-
expect(log).to.a.property('enabled').that.is.true()
58+
expect(log).to.have.property('enabled').that.is.true()
5959
expect(log).to.have.property('error').that.is.a('function')
6060
expect(log).to.have.nested.property('error.enabled').that.is.true()
6161
expect(log).to.have.property('trace').that.is.a('function')
@@ -68,7 +68,7 @@ describe('logger', () => {
6868
const log = logger('enabled-with-trace-logger')
6969

7070
expect(log).to.be.a('function')
71-
expect(log).to.a.property('enabled').that.is.true()
71+
expect(log).to.have.property('enabled').that.is.true()
7272
expect(log).to.have.property('error').that.is.a('function')
7373
expect(log).to.have.nested.property('error.enabled').that.is.true()
7474
expect(log).to.have.property('trace').that.is.a('function')

0 commit comments

Comments
 (0)