Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@
},
"dependencies": {
"@chainsafe/is-ip": "^2.0.1",
"multiformats": "^13.0.0",
"uint8-varint": "^2.0.1",
"uint8arrays": "^5.0.0"
"multiformats": "^14.0.0",
"uint8-varint": "^3.0.0",
"uint8arrays": "^6.1.1"
},
"devDependencies": {
"aegir": "^48.0.4"
Expand Down
5 changes: 3 additions & 2 deletions src/components.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as varint from 'uint8-varint'
import { withArrayBuffer } from 'uint8arrays'
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
Expand Down Expand Up @@ -27,7 +28,7 @@ export function bytesToComponents (bytes: Uint8Array): Component[] {
const component: Component = {
code,
name: codec.name,
bytes: bytes.subarray(i, i + componentLength)
bytes: withArrayBuffer(bytes.subarray(i, i + componentLength))
}

if (size > 0) {
Expand All @@ -45,7 +46,7 @@ export function bytesToComponents (bytes: Uint8Array): Component[] {
return components
}

export function componentsToBytes (components: Component[]): Uint8Array {
export function componentsToBytes (components: Component[]): Uint8Array<ArrayBuffer> {
let length = 0
const bytes: Uint8Array[] = []

Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ export interface Component {
* The bytes that make up the component. This will be set if the multiaddr
* was parsed from a `Uint8Array`, or if `.bytes` has been accessed on it.
*/
bytes?: Uint8Array
bytes?: Uint8Array<ArrayBuffer>
}

export interface Multiaddr {
bytes: Uint8Array
bytes: Uint8Array<ArrayBuffer>

/**
* Returns Multiaddr as a String
Expand Down Expand Up @@ -258,7 +258,7 @@ export interface Multiaddr {
* // false
* ```
*/
equals(addr: { bytes: Uint8Array }): boolean
equals(addr?: any): boolean
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/multiaddr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class Multiaddr implements MultiaddrInterface {
// cache string representation
#string: string | undefined
// cache byte representation
#bytes: Uint8Array | undefined
#bytes: Uint8Array<ArrayBuffer> | undefined

constructor (addr: MultiaddrInput | Component[] = '/', options: MultiaddrOptions = {}) {
this.#components = toComponents(addr)
Expand All @@ -64,7 +64,7 @@ export class Multiaddr implements MultiaddrInterface {
}
}

get bytes (): Uint8Array {
get bytes (): Uint8Array<ArrayBuffer> {
if (this.#bytes == null) {
this.#bytes = componentsToBytes(this.#components)
}
Expand Down
2 changes: 1 addition & 1 deletion src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export interface ProtocolCodec {
/**
* To encode the multiaddr as bytes, convert the value to bytes
*/
valueToBytes?(value: string): Uint8Array
valueToBytes?(value: string): Uint8Array<ArrayBuffer>

/**
* To decode bytes to a multiaddr, convert the value bytes to a string
Expand Down
14 changes: 7 additions & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function bytesToString (base: SupportedEncodings): (buf: Uint8Array) => s
}
}

export function stringToBytes (base: SupportedEncodings): (value: string) => Uint8Array {
export function stringToBytes (base: SupportedEncodings): (value: string) => Uint8Array<ArrayBuffer> {
return (buf) => {
return uint8ArrayFromString(buf, base)
}
Expand All @@ -25,15 +25,15 @@ export function bytes2port (buf: Uint8Array): string {
return view.getUint16(buf.byteOffset).toString()
}

export function port2bytes (port: string | number): Uint8Array {
export function port2bytes (port: string | number): Uint8Array<ArrayBuffer> {
const buf = new ArrayBuffer(2)
const view = new DataView(buf)
view.setUint16(0, typeof port === 'string' ? parseInt(port) : port)

return new Uint8Array(buf)
}

export function onion2bytes (str: string): Uint8Array {
export function onion2bytes (str: string): Uint8Array<ArrayBuffer> {
const addr = str.split(':')

if (addr.length !== 2) {
Expand All @@ -59,7 +59,7 @@ export function onion2bytes (str: string): Uint8Array {
return uint8ArrayConcat([buf, portBuf], buf.length + portBuf.length)
}

export function onion32bytes (str: string): Uint8Array {
export function onion32bytes (str: string): Uint8Array<ArrayBuffer> {
const addr = str.split(':')

if (addr.length !== 2) {
Expand Down Expand Up @@ -95,7 +95,7 @@ export function bytes2onion (buf: Uint8Array): string {

// Copied from https://github.com/indutny/node-ip/blob/master/lib/ip.js#L7
// but with buf/offset args removed because we don't use them
export const ip4ToBytes = function (ip: string): Uint8Array {
export const ip4ToBytes = function (ip: string): Uint8Array<ArrayBuffer> {
ip = ip.toString().trim()

const bytes = new Uint8Array(4)
Expand All @@ -115,7 +115,7 @@ export const ip4ToBytes = function (ip: string): Uint8Array {

// Copied from https://github.com/indutny/node-ip/blob/master/lib/ip.js#L7
// but with buf/offset args removed because we don't use them
export const ip6ToBytes = function (ip: string): Uint8Array {
export const ip6ToBytes = function (ip: string): Uint8Array<ArrayBuffer> {
let offset = 0
ip = ip.toString().trim()

Expand Down Expand Up @@ -228,7 +228,7 @@ const anybaseDecoder = (function () {
return acc
})()

export function mb2bytes (mbstr: string): Uint8Array {
export function mb2bytes (mbstr: string): Uint8Array<ArrayBuffer> {
return anybaseDecoder.decode(mbstr)
}

Expand Down
Loading