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

Commit

Permalink
deps: update it-length-prefixed and protons for no-copy ops (#357)
Browse files Browse the repository at this point in the history
Updates to latest modules so we don't copy memory unnecessarily.
  • Loading branch information
achingbrain authored Jul 31, 2022
1 parent d944d81 commit 518abfe
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 17 deletions.
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
"it-drain": "^1.0.5",
"it-first": "^1.0.6",
"it-length": "^1.0.3",
"it-length-prefixed": "^7.0.1",
"it-length-prefixed": "^8.0.2",
"it-map": "^1.0.6",
"it-merge": "^1.0.3",
"it-parallel": "^2.0.1",
Expand All @@ -176,13 +176,14 @@
"p-defer": "^4.0.0",
"p-queue": "^7.2.0",
"private-ip": "^2.3.3",
"protons-runtime": "^1.0.4",
"protons-runtime": "^2.0.2",
"timeout-abort-controller": "^3.0.0",
"uint8arraylist": "^2.0.0",
"uint8arrays": "^3.0.0",
"varint": "^6.0.0"
},
"devDependencies": {
"@libp2p/interface-mocks": "^2.0.0",
"@libp2p/interface-mocks": "^3.0.1",
"@libp2p/peer-id-factory": "^1.0.9",
"@libp2p/peer-store": "^3.0.0",
"@types/lodash.random": "^3.2.6",
Expand All @@ -199,7 +200,7 @@
"lodash.random": "^3.2.0",
"lodash.range": "^3.2.0",
"p-retry": "^5.0.0",
"protons": "^3.0.4",
"protons": "^4.0.1",
"sinon": "^14.0.0",
"ts-sinon": "^2.0.2",
"which": "^2.0.2"
Expand Down
13 changes: 7 additions & 6 deletions src/message/dht.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

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

export interface Record {
key?: Uint8Array
Expand All @@ -23,11 +24,11 @@ export namespace Record {
})
}

export const encode = (obj: Record): Uint8Array => {
export const encode = (obj: Record): Uint8ArrayList => {
return encodeMessage(obj, Record.codec())
}

export const decode = (buf: Uint8Array): Record => {
export const decode = (buf: Uint8Array | Uint8ArrayList): Record => {
return decodeMessage(buf, Record.codec())
}
}
Expand Down Expand Up @@ -101,11 +102,11 @@ export namespace Message {
})
}

export const encode = (obj: Peer): Uint8Array => {
export const encode = (obj: Peer): Uint8ArrayList => {
return encodeMessage(obj, Peer.codec())
}

export const decode = (buf: Uint8Array): Peer => {
export const decode = (buf: Uint8Array | Uint8ArrayList): Peer => {
return decodeMessage(buf, Peer.codec())
}
}
Expand All @@ -121,11 +122,11 @@ export namespace Message {
})
}

export const encode = (obj: Message): Uint8Array => {
export const encode = (obj: Message): Uint8ArrayList => {
return encodeMessage(obj, Message.codec())
}

export const decode = (buf: Uint8Array): Message => {
export const decode = (buf: Uint8Array | Uint8ArrayList): Message => {
return decodeMessage(buf, Message.codec())
}
}
3 changes: 2 additions & 1 deletion src/message/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Multiaddr } from '@multiformats/multiaddr'
import { Libp2pRecord } from '@libp2p/record'
import { Message as PBMessage } from './dht.js'
import type { PeerInfo } from '@libp2p/interface-peer-info'
import type { Uint8ArrayList } from 'uint8arraylist'

export const MESSAGE_TYPE = PBMessage.MessageType
export const CONNECTION_TYPE = PBMessage.ConnectionType
Expand Down Expand Up @@ -71,7 +72,7 @@ export class Message {
/**
* Decode from protobuf
*/
static deserialize (raw: Uint8Array) {
static deserialize (raw: Uint8ArrayList | Uint8Array) {
const dec = PBMessage.decode(raw)

const msg = new Message(dec.type ?? PBMessage.MessageType.PUT_VALUE, dec.key ?? Uint8Array.from([]), dec.clusterLevelRaw ?? 0)
Expand Down
5 changes: 3 additions & 2 deletions src/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type { PeerInfo } from '@libp2p/interface-peer-info'
import { Components, Initializable } from '@libp2p/components'
import type { Stream } from '@libp2p/interface-connection'
import { abortableDuplex } from 'abortable-iterator'
import type { Uint8ArrayList } from 'uint8arraylist'

export interface NetworkInit {
protocol: string
Expand Down Expand Up @@ -150,7 +151,7 @@ export class Network extends EventEmitter<NetworkEvents> implements Startable, I
/**
* Write a message to the given stream
*/
async _writeMessage (stream: Duplex<Uint8Array>, msg: Uint8Array, options: AbortOptions) {
async _writeMessage (stream: Duplex<Uint8Array>, msg: Uint8Array | Uint8ArrayList, options: AbortOptions) {
if (options.signal != null) {
stream = abortableDuplex(stream, options.signal)
}
Expand All @@ -168,7 +169,7 @@ export class Network extends EventEmitter<NetworkEvents> implements Startable, I
* If no response is received after the specified timeout
* this will error out.
*/
async _writeReadMessage (stream: Duplex<Uint8Array>, msg: Uint8Array, options: AbortOptions) {
async _writeReadMessage (stream: Duplex<Uint8Array>, msg: Uint8Array | Uint8ArrayList, options: AbortOptions) {
if (options.signal != null) {
stream = abortableDuplex(stream, options.signal)
}
Expand Down
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ export function keyForPublicKey (peer: PeerId) {
}

export function isPublicKeyKey (key: Uint8Array) {
return uint8ArrayToString(key.slice(0, 4)) === '/pk/'
return uint8ArrayToString(key.subarray(0, 4)) === '/pk/'
}

export function isIPNSKey (key: Uint8Array) {
return uint8ArrayToString(key.slice(0, 4)) === '/ipns/'
return uint8ArrayToString(key.subarray(0, 4)) === '/ipns/'
}

export function fromPublicKeyKey (key: Uint8Array) {
return peerIdFromBytes(key.slice(4))
return peerIdFromBytes(key.subarray(4))
}

/**
Expand Down
3 changes: 2 additions & 1 deletion test/rpc/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type { Duplex } from 'it-stream-types'
import { mockStream, mockConnection, mockMultiaddrConnection } from '@libp2p/interface-mocks'
import { Components } from '@libp2p/components'
import { start } from '@libp2p/interfaces/startable'
import type { Uint8ArrayList } from 'uint8arraylist'

describe('rpc', () => {
let peerId: PeerId
Expand Down Expand Up @@ -65,7 +66,7 @@ describe('rpc', () => {
const defer = pDefer()
const msg = new Message(MESSAGE_TYPE.GET_VALUE, uint8ArrayFromString('hello'), 5)

const validateMessage = (res: Uint8Array[]) => {
const validateMessage = (res: Uint8ArrayList[]) => {
const msg = Message.deserialize(res[0])
expect(msg).to.have.property('key').eql(uint8ArrayFromString('hello'))
expect(msg).to.have.property('closerPeers').eql([])
Expand Down

0 comments on commit 518abfe

Please sign in to comment.