-
Notifications
You must be signed in to change notification settings - Fork 17
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
refactor for multiformats #2
Merged
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4fe113d
feat: migrate code
mikeal 375c7f8
wip: needs more coverage
mikeal a07a83f
fix: make clean Uint8Array
rvagg 04b9710
Merge pull request #1 from ipld/rvagg/clean-cid-backing-buffer
mikeal b3f192e
fix: 100% coverage, backing out error swallowing
mikeal a2f9881
build: automated releases
mikeal c73108e
fix: linter fixes
mikeal 0bf1138
chore: pull in Uint8Array|Buffer test
rvagg ab546d3
Merge pull request #4 from ipld/rvagg/verify-uint8array
mikeal 1149b0f
fix: move constants to top
mikeal a42966a
Merge branch 'multiformats' of https://github.com/ipld/js-dag-cbor in…
mikeal 025e26e
fix: upgrade to latest ESM version
mikeal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,25 @@ | ||
on: [push, pull_request] | ||
name: Build, test and maybe publish | ||
jobs: | ||
build: | ||
name: Build | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@master | ||
- name: Cache node_modules | ||
id: cache-modules | ||
uses: actions/cache@v1 | ||
with: | ||
path: node_modules | ||
key: ${{ runner.OS }}-build-${{ hashFiles('package.json') }} | ||
- name: Build | ||
if: steps.cache-modules.outputs.cache-hit != 'true' | ||
run: npm install | ||
- name: Test | ||
run: npm test | ||
- name: Publish | ||
if: github.ref == 'refs/heads/master' && github.repository == 'ipld/js-dag-cbor' | ||
uses: mikeal/merge-release@master | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }} |
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
docs | ||
package-lock.json | ||
yarn.lock | ||
.nyc_output | ||
# Logs | ||
logs | ||
*.log | ||
|
This file was deleted.
Oops, something went wrong.
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,130 @@ | ||
const cbor = require('borc') | ||
const isCircular = require('is-circular') | ||
|
||
// https://github.com/ipfs/go-ipfs/issues/3570#issuecomment-273931692 | ||
const CID_CBOR_TAG = 42 | ||
|
||
module.exports = multiformats => { | ||
const { CID, bytes, varint } = multiformats | ||
function tagCID (cid) { | ||
const buffer = Uint8Array.from([...bytes.fromHex('00'), ...cid.buffer]) | ||
return new cbor.Tagged(CID_CBOR_TAG, buffer) | ||
} | ||
|
||
function replaceCIDbyTAG (dagNode) { | ||
if (isCircular(dagNode)) { | ||
throw new Error('The object passed has circular references') | ||
} | ||
|
||
function transform (obj) { | ||
if (bytes.isBinary(obj)) return bytes.coerce(obj) | ||
if (!obj || typeof obj === 'string') { | ||
return obj | ||
} | ||
|
||
if (Array.isArray(obj)) { | ||
return obj.map(transform) | ||
} | ||
|
||
if (CID.isCID(obj)) { | ||
return tagCID(obj) | ||
} | ||
|
||
const keys = Object.keys(obj) | ||
|
||
if (keys.length > 0) { | ||
// Recursive transform | ||
const out = {} | ||
keys.forEach((key) => { | ||
if (typeof obj[key] === 'object') { | ||
out[key] = transform(obj[key]) | ||
} else { | ||
out[key] = obj[key] | ||
} | ||
}) | ||
return out | ||
} else { | ||
return obj | ||
} | ||
} | ||
|
||
return transform(dagNode) | ||
} | ||
|
||
const defaultTags = { | ||
[CID_CBOR_TAG]: (val) => { | ||
// remove that 0 | ||
val = Uint8Array.from(val.slice(1)) | ||
const [version] = varint.decode(val) | ||
if (version > 1) { | ||
// CIDv0 | ||
return new CID(0, 0x70, val) | ||
} | ||
return new CID(val) | ||
} | ||
} | ||
const defaultSize = 64 * 1024 // current decoder heap size, 64 Kb | ||
let currentSize = defaultSize | ||
const defaultMaxSize = 64 * 1024 * 1024 // max heap size when auto-growing, 64 Mb | ||
let maxSize = defaultMaxSize | ||
let decoder = null | ||
|
||
/** | ||
* Configure the underlying CBOR decoder. | ||
* | ||
* @param {Object} [options] - The options the decoder takes. The decoder will reset to the defaul values if no options are given. | ||
* @param {number} [options.size=65536] - The current heap size used in CBOR parsing, this may grow automatically as larger blocks are encountered up to `maxSize` | ||
* @param {number} [options.maxSize=67108864] - The maximum size the CBOR parsing heap is allowed to grow to before `dagCBOR.util.deserialize()` returns an error | ||
* @param {Object} [options.tags] - An object whose keys are CBOR tag numbers and values are transform functions that accept a `value` and return a decoded representation of that `value` | ||
*/ | ||
const configureDecoder = (options) => { | ||
const tags = defaultTags | ||
|
||
if (options) { | ||
if (typeof options.size === 'number') { | ||
currentSize = options.size | ||
} | ||
if (typeof options.maxSize === 'number') { | ||
maxSize = options.maxSize | ||
} | ||
} else { | ||
// no options, reset to defaults | ||
currentSize = defaultSize | ||
maxSize = defaultMaxSize | ||
} | ||
|
||
const decoderOptions = { | ||
tags, | ||
size: currentSize | ||
} | ||
|
||
decoder = new cbor.Decoder(decoderOptions) | ||
// borc edits opts.size in-place so we can capture _actual_ size | ||
currentSize = decoderOptions.size | ||
} | ||
configureDecoder() | ||
module.exports.configureDecoder = configureDecoder // for testing | ||
|
||
const encode = (node) => { | ||
const nodeTagged = replaceCIDbyTAG(node) | ||
const serialized = cbor.encode(nodeTagged) | ||
return serialized | ||
} | ||
|
||
const decode = (data) => { | ||
if (data.length > currentSize && data.length <= maxSize) { | ||
configureDecoder({ size: data.length }) | ||
} | ||
|
||
if (data.length > currentSize) { | ||
throw new Error('Data is too large to deserialize with current decoder') | ||
} | ||
|
||
const deserialized = decoder.decodeFirst(data) | ||
return deserialized | ||
} | ||
|
||
const code = 0x71 | ||
const name = 'dag-cbor' | ||
return { encode, decode, code, name } | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tiny nit: could we pull these two to the top level and at the top of the file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch, i agree.