Skip to content

Commit

Permalink
feat!: refactor for multiformats (#2)
Browse files Browse the repository at this point in the history
BREAKING CHANGE!

* feat: migrate code

* wip: needs more coverage

* fix: make clean Uint8Array

* fix: 100% coverage, backing out error swallowing

* build: automated releases

* fix: linter fixes

* chore: pull in Uint8Array|Buffer test

Not strictly necessary but this test was added to ipld-dag-cbor and it's
nice for completeness and holds us strictly to the promise of Uint8Arrays
being the primary byte holder

Ref: ipld/js-ipld-dag-cbor#129

* fix: move constants to top

* fix: upgrade to latest ESM version

Co-authored-by: Rod Vagg <rod@vagg.org>
  • Loading branch information
mikeal and rvagg authored Jun 13, 2020
1 parent 4fe113d commit 211302a
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 306 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/main.yml
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 }}
1 change: 1 addition & 0 deletions .gitignore
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
Expand Down
40 changes: 0 additions & 40 deletions .travis.yml

This file was deleted.

46 changes: 20 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,27 @@
const cbor = require('borc')
const isCircular = require('is-circular')
import cbor from 'borc'
import isCircular from 'is-circular'

// https://github.com/ipfs/go-ipfs/issues/3570#issuecomment-273931692
const CID_CBOR_TAG = 42

module.exports = multiformats => {
const { CID, bytes } = multiformats
function tagCID (cid) {
if (typeof cid === 'string') {
cid = new CID(cid).buffer
} else if (CID.isCID(cid)) {
cid = cid.buffer
}
const code = 0x71
const name = 'dag-cbor'

const buffer = Uint8Array.from([...bytes.fromHex('00'), ...cid])
const create = 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) {
let circular
try {
circular = isCircular(dagNode)
} catch (e) {
circular = false
}
if (circular) {
if (isCircular(dagNode)) {
throw new Error('The object passed has circular references')
}

function transform (obj) {
if (!obj || bytes.isBinary(obj) || typeof obj === 'string') {
if (bytes.isBinary(obj)) return bytes.coerce(obj)
if (!obj || typeof obj === 'string') {
return obj
}

Expand Down Expand Up @@ -65,7 +57,12 @@ module.exports = multiformats => {
const defaultTags = {
[CID_CBOR_TAG]: (val) => {
// remove that 0
val = val.slice(1)
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)
}
}
Expand All @@ -84,7 +81,7 @@ module.exports = multiformats => {
* @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) => {
let tags = defaultTags
const tags = defaultTags

if (options) {
if (typeof options.size === 'number') {
Expand All @@ -93,9 +90,6 @@ module.exports = multiformats => {
if (typeof options.maxSize === 'number') {
maxSize = options.maxSize
}
if (options.tags) {
tags = Object.assign({}, defaultTags, options && options.tags)
}
} else {
// no options, reset to defaults
currentSize = defaultSize
Expand All @@ -112,6 +106,7 @@ module.exports = multiformats => {
currentSize = decoderOptions.size
}
configureDecoder()
create.configureDecoder = configureDecoder // for testing

const encode = (node) => {
const nodeTagged = replaceCIDbyTAG(node)
Expand All @@ -132,7 +127,6 @@ module.exports = multiformats => {
return deserialized
}

const code = 0x71
const name = 'dag-cbor'
return { encode, decode, code, name }
}
export default create
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.0.0",
"description": "JS implementation of dag-cbor",
"main": "src/index.js",
"type": "module",
"scripts": {
"test": "hundreds mocha test/test-*.js && npm run test:browser",
"test:browser": "polendina --cleanup test/test-*.js",
Expand All @@ -26,8 +27,10 @@
"is-circular": "^1.0.2"
},
"devDependencies": {
"hundreds": "0.0.2",
"mocha": "^7.1.2",
"garbage": "0.0.0",
"hundreds": "0.0.4",
"mocha": "^8.0.1",
"multiformats": "1.0.7",
"polendina": "^1.0.0",
"standard": "^14.3.4"
},
Expand Down
20 changes: 0 additions & 20 deletions test/mod.spec.js

This file was deleted.

106 changes: 0 additions & 106 deletions test/resolver.spec.js

This file was deleted.

Loading

0 comments on commit 211302a

Please sign in to comment.