Skip to content

Commit

Permalink
feat: get path and tuples on constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Jul 11, 2023
1 parent 76fa6f5 commit 50ff583
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
28 changes: 18 additions & 10 deletions src/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,25 @@ export function stringTuplesToString (tuples: StringTuple[]): string {
/**
* [[str name, str addr]... ] -> [[int code, Uint8Array]... ]
*/
export function stringTuplesToTuples (tuples: Array<string[] | string>): Tuple[] {
return tuples.map((tup) => {
export function stringTuplesToTuples (stringTuples: Array<string[] | string>): { tuples: Tuple[], path: string | null } {
let path: string | null | undefined
const tuples = stringTuples.map((tup) => {
if (!Array.isArray(tup)) {
tup = [tup]
}
const proto = protoFromTuple(tup)
if (tup.length > 1) {
return [proto.code, convertToBytes(proto.code, tup[1])]
const tuple: Tuple = (tup.length > 1) ? [proto.code, convertToBytes(proto.code, tup[1])] : [proto.code]
if (path === undefined && proto.path === true) {
path = tuple[1] != null ? convertToString(proto.code, tuple[1]) : null
}
return [proto.code]
return tuple
})

if (path == null) {
path = null
}

return { tuples, path }
}

/**
Expand Down Expand Up @@ -171,18 +179,18 @@ export function bytesToString (buf: Uint8Array): string {
/**
* String -> Uint8Array
*/
export function stringToBytes (str: string): Uint8Array {
export function stringToBytes (str: string): { bytes: Uint8Array, tuples: Tuple[], path: string | null } {
str = cleanPath(str)
const a = stringToStringTuples(str)
const b = stringTuplesToTuples(a)
const stringTuples = stringToStringTuples(str)
const { tuples, path } = stringTuplesToTuples(stringTuples)

return tuplesToBytes(b)
return { bytes: tuplesToBytes(tuples), tuples, path }
}

/**
* String -> Uint8Array
*/
export function fromString (str: string): Uint8Array {
export function fromString (str: string): { bytes: Uint8Array, tuples: Tuple[], path: string | null } {
return stringToBytes(str)
}

Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,10 @@ class DefaultMultiaddr implements Multiaddr {
if (addr.length > 0 && addr.charAt(0) !== '/') {
throw new Error(`multiaddr "${addr}" must start with a "/"`)
}
this.bytes = codec.fromString(addr)
const { bytes, tuples, path } = codec.fromString(addr)
this.bytes = bytes
this.#tuples = tuples
this.#path = path
} else if (isMultiaddr(addr)) { // Multiaddr
this.bytes = codec.fromBytes(addr.bytes) // validate + copy buffer
} else {
Expand Down
22 changes: 15 additions & 7 deletions test/codec.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { expect } from 'aegir/chai'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import varint from 'varint'
import * as codec from '../src/codec.js'
import { convertToBytes } from '../src/convert.js'

describe('codec', () => {
describe('.stringToStringTuples', () => {
Expand All @@ -16,13 +17,20 @@ describe('codec', () => {
})

describe('.stringTuplesToTuples', () => {
it('handles non array tuples', () => {
expect(
codec.stringTuplesToTuples([['ip4', '0.0.0.0'], 'utp'])
).to.eql(
[[4, Uint8Array.from([0, 0, 0, 0])], [302]]
)
})
const testCases: Array<{ name: string, stringTuples: Array<string[] | string>, tuples: Array<[number, Uint8Array?]>, path: string | null }> = [
{ name: 'handles non array tuples', stringTuples: [['ip4', '0.0.0.0'], 'utp'], tuples: [[4, Uint8Array.from([0, 0, 0, 0])], [302]], path: null },
{ name: 'handle not null path', stringTuples: [['unix', '/tmp/p2p.sock']], tuples: [[400, convertToBytes(400, '/tmp/p2p.sock')]], path: '/tmp/p2p.sock' }
]

for (const { name, stringTuples, tuples, path } of testCases) {
it(name, () => {
expect(
codec.stringTuplesToTuples(stringTuples)
).to.eql(
{ tuples, path }
)
})
}
})

describe('.tuplesToStringTuples', () => {
Expand Down

0 comments on commit 50ff583

Please sign in to comment.