This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(pubsub): new wire format in http rpc
- Loading branch information
Showing
6 changed files
with
107 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' | ||
import { toString as uint8ArrayToString } from 'uint8arrays/to-string' | ||
import { base64url } from 'multiformats/bases/base64' | ||
import PeerId from 'peer-id' | ||
|
||
/* HTTP RPC: | ||
* - sends PeerIds as Base58btc (legacy) or CIDv1 | ||
* - wraps binary data in multibase. base64url is used to avoid issues | ||
* when a binary data is passed as search param in URL. | ||
* Historical context: https://github.com/ipfs/go-ipfs/issues/7939 | ||
* Multibase wrapping introduced in: https://github.com/ipfs/go-ipfs/pull/8183 | ||
*/ | ||
|
||
/** | ||
* @param {Array<string>} strings | ||
* @returns {Array<string>} strings | ||
*/ | ||
const mbToTextArray = strings => { | ||
if (Array.isArray(strings)) { | ||
return strings.map(mbToText) | ||
} | ||
return strings | ||
} | ||
|
||
/** | ||
* @param {string} mb | ||
* @returns {string} | ||
*/ | ||
const mbToText = mb => uint8ArrayToString(mbToBytes(mb)) | ||
|
||
/** | ||
* @param {string} mb | ||
* @returns {Uint8Array} | ||
*/ | ||
const mbToBytes = mb => base64url.decode(mb) | ||
|
||
/** | ||
* @param {string} text | ||
* @returns {string} | ||
*/ | ||
const toUrlSafeBase = text => base64url.encode(uint8ArrayFromString(text)) | ||
|
||
/** | ||
* Ensure uniform Peer ID representation in text | ||
* | ||
* @param {Array<string>} peerids | ||
* @returns {Array<string>} peerids | ||
*/ | ||
const normalizePeerIds = peerids => { | ||
if (Array.isArray(peerids)) { | ||
return peerids.map(normalizePeerId) | ||
} | ||
return peerids | ||
} | ||
|
||
/** | ||
* Ensure uniform Peer ID representation in text | ||
* | ||
* @param {string} peerid | ||
* @returns {string} | ||
*/ | ||
const normalizePeerId = peerid => PeerId.parse(peerid).toB58String() // TODO: toString() when go-ipfs switch to CIDv1 | ||
|
||
export { mbToTextArray, mbToText, mbToBytes, toUrlSafeBase, normalizePeerIds, normalizePeerId } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters