Skip to content

Commit 6abcd22

Browse files
authored
deps: switch varint for uint8-varint (#1973)
It's faster and is used elsewhere saving us a dep.
1 parent 4ef9c79 commit 6abcd22

File tree

9 files changed

+25
-30
lines changed

9 files changed

+25
-30
lines changed

packages/kad-dht/package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,16 @@
8383
"private-ip": "^3.0.0",
8484
"progress-events": "^1.0.0",
8585
"protons-runtime": "^5.0.0",
86+
"uint8-varint": "^2.0.0",
8687
"uint8arraylist": "^2.4.3",
87-
"uint8arrays": "^4.0.6",
88-
"varint": "^6.0.0"
88+
"uint8arrays": "^4.0.6"
8989
},
9090
"devDependencies": {
9191
"@libp2p/interface-compliance-tests": "^4.0.3",
9292
"@libp2p/peer-id-factory": "^3.0.3",
9393
"@libp2p/peer-store": "^9.0.3",
9494
"@types/lodash.random": "^3.2.6",
9595
"@types/lodash.range": "^3.2.6",
96-
"@types/varint": "^6.0.0",
9796
"@types/which": "^3.0.0",
9897
"aegir": "^40.0.8",
9998
"datastore-level": "^10.0.0",

packages/kad-dht/src/providers.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { peerIdFromString } from '@libp2p/peer-id'
33
import cache from 'hashlru'
44
import { Key } from 'interface-datastore/key'
55
import Queue from 'p-queue'
6+
import * as varint from 'uint8-varint'
67
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
7-
import varint from 'varint'
88
import {
99
PROVIDERS_CLEANUP_INTERVAL,
1010
PROVIDERS_VALIDITY,
@@ -245,7 +245,7 @@ async function writeProviderEntry (store: Datastore, cid: CID, peer: PeerId, tim
245245
].join('')
246246

247247
const key = new Key(dsKey)
248-
const buffer = Uint8Array.from(varint.encode(time.getTime()))
248+
const buffer = varint.encode(time.getTime())
249249

250250
await store.put(key, buffer)
251251
}

packages/libp2p/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@
180180
"@libp2p/mplex": "^9.0.3",
181181
"@libp2p/tcp": "^8.0.3",
182182
"@libp2p/websockets": "^7.0.3",
183-
"@types/varint": "^6.0.0",
184183
"@types/xsalsa20": "^1.1.0",
185184
"aegir": "^40.0.8",
186185
"delay": "^6.0.0",

packages/multistream-select/package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,16 @@
6363
"it-pushable": "^3.2.0",
6464
"it-reader": "^6.0.1",
6565
"it-stream-types": "^2.0.1",
66+
"uint8-varint": "^2.0.0",
6667
"uint8arraylist": "^2.4.3",
6768
"uint8arrays": "^4.0.6"
6869
},
6970
"devDependencies": {
70-
"@types/varint": "^6.0.0",
7171
"aegir": "^40.0.8",
7272
"iso-random-stream": "^2.0.2",
7373
"it-all": "^3.0.1",
7474
"it-map": "^3.0.3",
7575
"it-pair": "^2.0.6",
76-
"p-timeout": "^6.0.0",
77-
"varint": "^6.0.0"
76+
"p-timeout": "^6.0.0"
7877
}
7978
}

packages/multistream-select/test/multistream.spec.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import { expect } from 'aegir/chai'
55
import all from 'it-all'
66
import { pushable } from 'it-pushable'
77
import { reader } from 'it-reader'
8+
import * as varint from 'uint8-varint'
89
import { Uint8ArrayList } from 'uint8arraylist'
910
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
1011
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
11-
import * as Varint from 'varint'
1212
import * as Multistream from '../src/multistream.js'
1313

1414
describe('Multistream', () => {
@@ -18,7 +18,7 @@ describe('Multistream', () => {
1818
const output = Multistream.encode(input)
1919

2020
const expected = uint8ArrayConcat([
21-
Uint8Array.from(Varint.encode(input.length + 1)), // +1 to include newline
21+
varint.encode(input.length + 1), // +1 to include newline
2222
input,
2323
uint8ArrayFromString('\n')
2424
])
@@ -31,7 +31,7 @@ describe('Multistream', () => {
3131
const output = Multistream.encode(input.slice())
3232

3333
const expected = uint8ArrayConcat([
34-
Uint8Array.from(Varint.encode(input.length + 1)), // +1 to include newline
34+
varint.encode(input.length + 1), // +1 to include newline
3535
input.slice(),
3636
uint8ArrayFromString('\n')
3737
])
@@ -48,7 +48,7 @@ describe('Multistream', () => {
4848
Multistream.write(writer, input)
4949

5050
const expected = uint8ArrayConcat([
51-
Uint8Array.from(Varint.encode(input.length + 1)), // +1 to include newline
51+
varint.encode(input.length + 1), // +1 to include newline
5252
input,
5353
uint8ArrayFromString('\n')
5454
])
@@ -66,7 +66,7 @@ describe('Multistream', () => {
6666
const input = uint8ArrayFromString(`TEST${Date.now()}`)
6767

6868
const source = reader([uint8ArrayConcat([
69-
Uint8Array.from(Varint.encode(input.length + 1)), // +1 to include newline
69+
varint.encode(input.length + 1), // +1 to include newline
7070
input,
7171
uint8ArrayFromString('\n')
7272
])])
@@ -79,7 +79,7 @@ describe('Multistream', () => {
7979
const input = uint8ArrayFromString(`TEST${Date.now()}`)
8080

8181
const source = reader([uint8ArrayConcat([
82-
Uint8Array.from(Varint.encode(input.length)),
82+
varint.encode(input.length),
8383
input
8484
])])
8585

@@ -92,7 +92,7 @@ describe('Multistream', () => {
9292
input[input.length - 1] = '\n'.charCodeAt(0)
9393

9494
const source = reader([uint8ArrayConcat([
95-
Uint8Array.from(Varint.encode(input.length)),
95+
varint.encode(input.length),
9696
input
9797
])])
9898

@@ -104,7 +104,7 @@ describe('Multistream', () => {
104104
const input = new Uint8Array(0)
105105

106106
const source = reader([uint8ArrayConcat([
107-
Uint8Array.from(Varint.encode(input.length)),
107+
varint.encode(input.length),
108108
input
109109
])])
110110

@@ -116,7 +116,7 @@ describe('Multistream', () => {
116116
const input = uint8ArrayFromString(`TEST${Date.now()}`)
117117

118118
const source = reader([uint8ArrayConcat([
119-
Uint8Array.from(Varint.encode(input.length + 1)), // +1 to include newline
119+
varint.encode(input.length + 1), // +1 to include newline
120120
input,
121121
uint8ArrayFromString('\n')
122122
])])

packages/peer-record/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
},
6868
"devDependencies": {
6969
"@libp2p/peer-id-factory": "^3.0.3",
70-
"@types/varint": "^6.0.0",
7170
"aegir": "^40.0.8",
7271
"protons": "^7.0.2"
7372
}

packages/peer-record/src/envelope/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { unmarshalPrivateKey, unmarshalPublicKey } from '@libp2p/crypto/keys'
22
import { CodeError } from '@libp2p/interface/errors'
33
import { peerIdFromKeys } from '@libp2p/peer-id'
4-
import * as unsigned from 'uint8-varint'
4+
import * as varint from 'uint8-varint'
55
import { Uint8ArrayList } from 'uint8arraylist'
66
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
77
import { fromString as uint8arraysFromString } from 'uint8arrays/from-string'
@@ -147,9 +147,9 @@ const formatSignaturePayload = (domain: string, payloadType: Uint8Array, payload
147147
// - The value of the payload field
148148

149149
const domainUint8Array = uint8arraysFromString(domain)
150-
const domainLength = unsigned.encode(domainUint8Array.byteLength)
151-
const payloadTypeLength = unsigned.encode(payloadType.length)
152-
const payloadLength = unsigned.encode(payload.length)
150+
const domainLength = varint.encode(domainUint8Array.byteLength)
151+
const payloadTypeLength = varint.encode(payloadType.length)
152+
const payloadLength = varint.encode(payload.length)
153153

154154
return new Uint8ArrayList(
155155
domainLength,

packages/stream-multiplexer-mplex/package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,12 @@
6464
"it-pushable": "^3.2.0",
6565
"it-stream-types": "^2.0.1",
6666
"rate-limiter-flexible": "^2.3.11",
67+
"uint8-varint": "^2.0.0",
6768
"uint8arraylist": "^2.4.3",
68-
"uint8arrays": "^4.0.6",
69-
"varint": "^6.0.0"
69+
"uint8arrays": "^4.0.6"
7070
},
7171
"devDependencies": {
7272
"@libp2p/interface-compliance-tests": "^4.0.3",
73-
"@types/varint": "^6.0.0",
7473
"aegir": "^40.0.8",
7574
"cborg": "^2.0.1",
7675
"delay": "^6.0.0",

packages/stream-multiplexer-mplex/src/encode.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import batchedBytes from 'it-batched-bytes'
2+
import * as varint from 'uint8-varint'
23
import { Uint8ArrayList } from 'uint8arraylist'
3-
import varint from 'varint'
44
import { allocUnsafe } from './alloc-unsafe.js'
55
import { type Message, MessageTypes } from './message-types.js'
66
import type { Source } from 'it-stream-types'
@@ -24,16 +24,16 @@ class Encoder {
2424
let offset = this._poolOffset
2525

2626
varint.encode(msg.id << 3 | msg.type, pool, offset)
27-
offset += varint.encode.bytes ?? 0
27+
offset += varint.encodingLength(msg.id << 3 | msg.type)
2828

2929
if ((msg.type === MessageTypes.NEW_STREAM || msg.type === MessageTypes.MESSAGE_INITIATOR || msg.type === MessageTypes.MESSAGE_RECEIVER) && msg.data != null) {
3030
varint.encode(msg.data.length, pool, offset)
31+
offset += varint.encodingLength(msg.data.length)
3132
} else {
3233
varint.encode(0, pool, offset)
34+
offset += varint.encodingLength(0)
3335
}
3436

35-
offset += varint.encode.bytes ?? 0
36-
3737
const header = pool.subarray(this._poolOffset, offset)
3838

3939
if (POOL_SIZE - offset < 100) {

0 commit comments

Comments
 (0)