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.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
47 changed files
with
1,213 additions
and
873 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict' | ||
|
||
const legacyCID = require('cids') | ||
// @ts-ignore | ||
const CID = require('multiformats/cid') | ||
|
||
/** | ||
* Makes sure that an object only contains js-cid style CIDs. | ||
* | ||
* It traverses the object recursively and changes all instances of a CID from | ||
* a js-multiforamts style CID to a js-cid style CID (js-cid style CIDs stay | ||
* as they are). You can also pass in a CID directly. | ||
* | ||
* Once js-cid is no longer used in the code base, this utility function will | ||
* no longer be needed. | ||
* | ||
* @param {any} obj - The object to do the transformation on | ||
*/ | ||
const asLegacyCid = (obj) => { | ||
if (legacyCID.isCID(obj)) { | ||
return obj | ||
} | ||
|
||
// NOTE vmx 2021-02-22: I have no idea why TypeScript doesn't pick this up | ||
// correctly => ignore it for now, deal with it later. | ||
// @ts-ignore | ||
const newCID = CID.asCID(obj) | ||
if (newCID) { | ||
const { version, code, multihash: { bytes } } = newCID | ||
const { buffer, byteOffset, byteLength } = bytes | ||
const multihash = Buffer.from(buffer, byteOffset, byteLength) | ||
return new legacyCID(version, code, multihash) | ||
} | ||
|
||
if (obj && typeof obj === 'object') { | ||
for (const [key, value] of Object.entries(obj)) { | ||
obj[key] = asLegacyCid(value) | ||
} | ||
} | ||
|
||
return obj | ||
} | ||
|
||
module.exports = asLegacyCid |
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
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
Oops, something went wrong.