Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: get path and tuples on constructor #329

Closed

Conversation

twoeths
Copy link
Contributor

@twoeths twoeths commented Jul 11, 2023

Motivation

Description

  • during the creation of DefaultMultiaddr when MultiaddrInput is a string, we already have tuples in stringTuplesToTuples, just need to return it
  • in that function, we already loop through each item, just need to check each proto to return path

@twoeths twoeths marked this pull request as ready for review July 11, 2023 07:19
src/codec.ts Outdated Show resolved Hide resolved
src/codec.ts Outdated Show resolved Hide resolved
src/codec.ts Outdated
Comment on lines 85 to 87
if (path == null) {
path = null
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't get it. If value for path is not set, we are resetting it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check this implementation

if (this.#path == null) {

when there is no path in any protocols, it means path = null and we don't need to search for it again the next time getPath() is called

I'll add more comments to this function referring to DefaultMultiaddr.getPath()

Copy link

@nazarhussain nazarhussain Jul 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, with == you are checking for both null and `undefined.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nazarhussain yeah it's a bit confused, I changed the condition to if (path === undefined) instead

src/codec.ts Outdated Show resolved Hide resolved
test/codec.spec.ts Outdated Show resolved Hide resolved
@twoeths
Copy link
Contributor Author

twoeths commented Jul 17, 2023

closing this PR as there are further improvement in #330

@twoeths twoeths closed this Jul 17, 2023
achingbrain pushed a commit that referenced this pull request Jul 28, 2023
Taking #329 to its logical extreme, we're able to simplify the codebase (mostly `codec.ts` and its interaction with `DefaultMultiaddr`).

Precalculate a `MultiaddrParts` when constructing a `DefaultMultiaddr`.
`MultiaddrParts` is essentially an object containing all data which is stored in a `DefaultMultiaddr`.

This makes all `DefaultMultiaddr` methods cheap, at the expense of always storing the string, bytes, tuples, stringTuples, and path of the multiaddr.

The two important functions to review are `stringToMultiaddrParts` and `bytesToMultiaddrParts`, which convert untrusted string and bytes into `MultiaddrParts`.

---------

Co-authored-by: Tuyen Nguyen <[email protected]>
github-actions bot pushed a commit that referenced this pull request Jul 28, 2023
## [12.1.4](v12.1.3...v12.1.4) (2023-07-28)

### Bug Fixes

* precalculate multiaddr parts ([#330](#330)) ([cf7e9c6](cf7e9c6)), closes [#329](#329)

### Trivial Changes

* update project config ([#326](#326)) ([76fa6f5](76fa6f5))

### Documentation

* import from @multiformats/multiaddr ([#327](#327)) ([4dedd4b](4dedd4b))

### Dependencies

* bump multiformats from 11.0.2 to 12.0.1 ([#328](#328)) ([07d4f8a](07d4f8a))
* **dev:** bump aegir from 39.0.13 to 40.0.2 ([#333](#333)) ([3480741](3480741))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants