Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

Commit 518abfe

Browse files
authored
deps: update it-length-prefixed and protons for no-copy ops (#357)
Updates to latest modules so we don't copy memory unnecessarily.
1 parent d944d81 commit 518abfe

File tree

6 files changed

+22
-17
lines changed

6 files changed

+22
-17
lines changed

package.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@
164164
"it-drain": "^1.0.5",
165165
"it-first": "^1.0.6",
166166
"it-length": "^1.0.3",
167-
"it-length-prefixed": "^7.0.1",
167+
"it-length-prefixed": "^8.0.2",
168168
"it-map": "^1.0.6",
169169
"it-merge": "^1.0.3",
170170
"it-parallel": "^2.0.1",
@@ -176,13 +176,14 @@
176176
"p-defer": "^4.0.0",
177177
"p-queue": "^7.2.0",
178178
"private-ip": "^2.3.3",
179-
"protons-runtime": "^1.0.4",
179+
"protons-runtime": "^2.0.2",
180180
"timeout-abort-controller": "^3.0.0",
181+
"uint8arraylist": "^2.0.0",
181182
"uint8arrays": "^3.0.0",
182183
"varint": "^6.0.0"
183184
},
184185
"devDependencies": {
185-
"@libp2p/interface-mocks": "^2.0.0",
186+
"@libp2p/interface-mocks": "^3.0.1",
186187
"@libp2p/peer-id-factory": "^1.0.9",
187188
"@libp2p/peer-store": "^3.0.0",
188189
"@types/lodash.random": "^3.2.6",
@@ -199,7 +200,7 @@
199200
"lodash.random": "^3.2.0",
200201
"lodash.range": "^3.2.0",
201202
"p-retry": "^5.0.0",
202-
"protons": "^3.0.4",
203+
"protons": "^4.0.1",
203204
"sinon": "^14.0.0",
204205
"ts-sinon": "^2.0.2",
205206
"which": "^2.0.2"

src/message/dht.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import { encodeMessage, decodeMessage, message, bytes, string, enumeration, int32 } from 'protons-runtime'
55
import type { Codec } from 'protons-runtime'
6+
import type { Uint8ArrayList } from 'uint8arraylist'
67

78
export interface Record {
89
key?: Uint8Array
@@ -23,11 +24,11 @@ export namespace Record {
2324
})
2425
}
2526

26-
export const encode = (obj: Record): Uint8Array => {
27+
export const encode = (obj: Record): Uint8ArrayList => {
2728
return encodeMessage(obj, Record.codec())
2829
}
2930

30-
export const decode = (buf: Uint8Array): Record => {
31+
export const decode = (buf: Uint8Array | Uint8ArrayList): Record => {
3132
return decodeMessage(buf, Record.codec())
3233
}
3334
}
@@ -101,11 +102,11 @@ export namespace Message {
101102
})
102103
}
103104

104-
export const encode = (obj: Peer): Uint8Array => {
105+
export const encode = (obj: Peer): Uint8ArrayList => {
105106
return encodeMessage(obj, Peer.codec())
106107
}
107108

108-
export const decode = (buf: Uint8Array): Peer => {
109+
export const decode = (buf: Uint8Array | Uint8ArrayList): Peer => {
109110
return decodeMessage(buf, Peer.codec())
110111
}
111112
}
@@ -121,11 +122,11 @@ export namespace Message {
121122
})
122123
}
123124

124-
export const encode = (obj: Message): Uint8Array => {
125+
export const encode = (obj: Message): Uint8ArrayList => {
125126
return encodeMessage(obj, Message.codec())
126127
}
127128

128-
export const decode = (buf: Uint8Array): Message => {
129+
export const decode = (buf: Uint8Array | Uint8ArrayList): Message => {
129130
return decodeMessage(buf, Message.codec())
130131
}
131132
}

src/message/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Multiaddr } from '@multiformats/multiaddr'
33
import { Libp2pRecord } from '@libp2p/record'
44
import { Message as PBMessage } from './dht.js'
55
import type { PeerInfo } from '@libp2p/interface-peer-info'
6+
import type { Uint8ArrayList } from 'uint8arraylist'
67

78
export const MESSAGE_TYPE = PBMessage.MessageType
89
export const CONNECTION_TYPE = PBMessage.ConnectionType
@@ -71,7 +72,7 @@ export class Message {
7172
/**
7273
* Decode from protobuf
7374
*/
74-
static deserialize (raw: Uint8Array) {
75+
static deserialize (raw: Uint8ArrayList | Uint8Array) {
7576
const dec = PBMessage.decode(raw)
7677

7778
const msg = new Message(dec.type ?? PBMessage.MessageType.PUT_VALUE, dec.key ?? Uint8Array.from([]), dec.clusterLevelRaw ?? 0)

src/network.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import type { PeerInfo } from '@libp2p/interface-peer-info'
2121
import { Components, Initializable } from '@libp2p/components'
2222
import type { Stream } from '@libp2p/interface-connection'
2323
import { abortableDuplex } from 'abortable-iterator'
24+
import type { Uint8ArrayList } from 'uint8arraylist'
2425

2526
export interface NetworkInit {
2627
protocol: string
@@ -150,7 +151,7 @@ export class Network extends EventEmitter<NetworkEvents> implements Startable, I
150151
/**
151152
* Write a message to the given stream
152153
*/
153-
async _writeMessage (stream: Duplex<Uint8Array>, msg: Uint8Array, options: AbortOptions) {
154+
async _writeMessage (stream: Duplex<Uint8Array>, msg: Uint8Array | Uint8ArrayList, options: AbortOptions) {
154155
if (options.signal != null) {
155156
stream = abortableDuplex(stream, options.signal)
156157
}
@@ -168,7 +169,7 @@ export class Network extends EventEmitter<NetworkEvents> implements Startable, I
168169
* If no response is received after the specified timeout
169170
* this will error out.
170171
*/
171-
async _writeReadMessage (stream: Duplex<Uint8Array>, msg: Uint8Array, options: AbortOptions) {
172+
async _writeReadMessage (stream: Duplex<Uint8Array>, msg: Uint8Array | Uint8ArrayList, options: AbortOptions) {
172173
if (options.signal != null) {
173174
stream = abortableDuplex(stream, options.signal)
174175
}

src/utils.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,15 @@ export function keyForPublicKey (peer: PeerId) {
9292
}
9393

9494
export function isPublicKeyKey (key: Uint8Array) {
95-
return uint8ArrayToString(key.slice(0, 4)) === '/pk/'
95+
return uint8ArrayToString(key.subarray(0, 4)) === '/pk/'
9696
}
9797

9898
export function isIPNSKey (key: Uint8Array) {
99-
return uint8ArrayToString(key.slice(0, 4)) === '/ipns/'
99+
return uint8ArrayToString(key.subarray(0, 4)) === '/ipns/'
100100
}
101101

102102
export function fromPublicKeyKey (key: Uint8Array) {
103-
return peerIdFromBytes(key.slice(4))
103+
return peerIdFromBytes(key.subarray(4))
104104
}
105105

106106
/**

test/rpc/index.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import type { Duplex } from 'it-stream-types'
2222
import { mockStream, mockConnection, mockMultiaddrConnection } from '@libp2p/interface-mocks'
2323
import { Components } from '@libp2p/components'
2424
import { start } from '@libp2p/interfaces/startable'
25+
import type { Uint8ArrayList } from 'uint8arraylist'
2526

2627
describe('rpc', () => {
2728
let peerId: PeerId
@@ -65,7 +66,7 @@ describe('rpc', () => {
6566
const defer = pDefer()
6667
const msg = new Message(MESSAGE_TYPE.GET_VALUE, uint8ArrayFromString('hello'), 5)
6768

68-
const validateMessage = (res: Uint8Array[]) => {
69+
const validateMessage = (res: Uint8ArrayList[]) => {
6970
const msg = Message.deserialize(res[0])
7071
expect(msg).to.have.property('key').eql(uint8ArrayFromString('hello'))
7172
expect(msg).to.have.property('closerPeers').eql([])

0 commit comments

Comments
 (0)