Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

feat: upgrade to latest spec #17

Merged
merged 1 commit into from
May 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ language: node_js
node_js:
- 4
- 5
- stable

# Make sure we have new NPM.
before_install:
Expand All @@ -13,12 +14,12 @@ script:
- npm test
- npm run coverage

addons:
firefox: 'latest'

before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start

after_success:
- npm run coverage-publish

addons:
firefox: 'latest'
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
"jsnext:main": "src/index.js",
"scripts": {
"test": "aegir-test",
"test:browser": "aegir-test browser",
"test:node": "aegir-test node",
"test:browser": "aegir-test --env browser",
"test:node": "aegir-test --env node",
"lint": "aegir-lint",
"release": "aegir-release",
"release-minor": "aegir-release minor",
"release-major": "aegir-release major",
"release-minor": "aegir-release --type minor",
"release-major": "aegir-release --type major",
"build": "aegir-build",
"coverage": "aegir-coverage",
"coverage-publish": "aegir-coverage publish"
Expand Down Expand Up @@ -39,18 +39,18 @@
"dependencies": {
"babel-runtime": "^6.6.1",
"bs58": "^3.0.0",
"cbor": "^1.0.3",
"cbor": "^1.0.4",
"lodash.clonedeep": "^4.3.2",
"lodash.defaults": "^4.0.1",
"lodash.includes": "^4.1.2",
"multiaddr": "^1.4.1",
"multihashes": "^0.2.1",
"lodash.includes": "^4.1.3",
"multiaddr": "^2.0.0",
"multihashes": "^0.2.2",
"multihashing": "^0.2.1",
"nofilter": "0.0.2"
},
"devDependencies": {
"aegir": "^3.0.0",
"aegir": "^3.0.4",
"chai": "^3.5.0",
"pre-commit": "^1.1.2"
"pre-commit": "^1.1.3"
}
}
22 changes: 13 additions & 9 deletions src/cbor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const cloneDeep = require('lodash.clonedeep')
exports = module.exports

exports.LINK_TAG = 258
const LINK_SYMBOL = exports.LINK_SYMBOL = '/'

exports.marshal = (original) => {
const input = cloneDeep(original)
Expand All @@ -25,23 +26,20 @@ exports.marshal = (original) => {
}
})

if (includes(keys, '@link')) {
let link = obj['@link']
if (includes(keys, LINK_SYMBOL)) {
let link = obj[LINK_SYMBOL]

// Multiaddr encoding
if (typeof link === 'string' && isMultiaddr(link)) {
link = new Multiaddr(link).buffer
}

// Remove the @link
delete obj['@link']
delete obj[LINK_SYMBOL]

// Non empty
if (keys.length > 1) {
return new cbor.Tagged(exports.LINK_TAG, [
link,
obj
])
throw new Error('Links must not have siblings')
}

return new cbor.Tagged(exports.LINK_TAG, link)
Expand Down Expand Up @@ -69,14 +67,20 @@ exports.unmarshal = (input, opts) => {
function transform (obj) {
Object.keys(obj).forEach((key) => {
const val = obj[key]
// This is safe as we reference the same cbor instance
// as we used to decode with
if (val instanceof cbor.Tagged) {
Copy link
Member

@daviddias daviddias May 19, 2016

Choose a reason for hiding this comment

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

Is it possible to use Duck Typing instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think that will work very well, we want this exact thing, not something that looks like it

Copy link
Member

Choose a reason for hiding this comment

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

It is just because a tiny change in the version, a module that gets 'reused' and suddenly this verification doesn't work

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually this can not happen here. We require cbor at the top and call cbor.decode and then check the decoded output against cbor.Tagged so this will always reference the same version of cbor.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, missed that, it is actually a very good point/guard. Can we add a comment so that the same question doesn't raise after?

if (typeof val.value === 'string') {
obj[key] = {
'@link': val.value
[LINK_SYMBOL]: val.value
}
} else if (Buffer.isBuffer(val.value)) {
obj[key] = {
[LINK_SYMBOL]: (new Multiaddr(val.value)).toString()
}
} else {
obj[key] = defaults({
'@link': val.value[0]
[LINK_SYMBOL]: val.value[0]
}, transform(val.value[1]))
}
} else if (typeof val === 'object') {
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const multihash = require('./multihash')
exports = module.exports

exports.LINK_TAG = cbor.LINK_TAG
exports.LINK_SYMBOL = cbor.LINK_SYMBOL
exports.marshal = cbor.marshal
exports.unmarshal = cbor.unmarshal
exports.multihash = multihash
152 changes: 38 additions & 114 deletions test/cbor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const Multiaddr = require('multiaddr')
const ipld = require('../src')

describe('IPLD -> CBOR', () => {
it('no @link', () => {
it('no /', () => {
const src = {
data: 'hello world',
size: 11
Expand All @@ -26,20 +26,18 @@ describe('IPLD -> CBOR', () => {
)
})

it('@link, is a string', () => {
it('/, is a string', () => {
const src = {
data: 'hello world',
size: 11,
'@link': 'hello-world'
l1: {'/': 'hello-world'}
}

const expected = new cbor.Tagged(ipld.LINK_TAG, [
'hello-world',
{
data: 'hello world',
size: 11
}
])
const expected = {
data: 'hello world',
size: 11,
l1: new cbor.Tagged(ipld.LINK_TAG, 'hello-world')
}

expect(
ipld.marshal(src)
Expand All @@ -48,21 +46,22 @@ describe('IPLD -> CBOR', () => {
)
})

it('@link, is a valid multiaddress', () => {
const addr = new Multiaddr('/ip4/127.0.0.1/udp/1234')
it('/, is a valid multiaddress', () => {
const addr1 = new Multiaddr('/ip4/127.0.0.1/udp/1234')
const addr2 = new Multiaddr('/ipfs/Qmafmh1Cw3H1bwdYpaaj5AbCW4LkYyUWaM7Nykpn5NZoYL')
const src = {
data: 'hello world',
size: 11,
'@link': addr.toString()
l1: {'/': addr1.toString()},
l2: {'/': addr2.toString()}
}

const expected = new cbor.Tagged(ipld.LINK_TAG, [
addr.buffer,
{
data: 'hello world',
size: 11
}
])
const expected = {
data: 'hello world',
size: 11,
l1: new cbor.Tagged(ipld.LINK_TAG, addr1.buffer),
l2: new cbor.Tagged(ipld.LINK_TAG, addr2.buffer)
}

expect(
ipld.marshal(src)
Expand All @@ -71,52 +70,41 @@ describe('IPLD -> CBOR', () => {
)
})

it('@link, with properties', () => {
it('/, with properties', () => {
const src = {
data: 'hello world',
size: 11,
secret: {
'@link': 'hello-world',
secret: 3
'/': 'hello-world',
i: 'should not be here'
}
}

const expected = {
data: 'hello world',
size: 11,
secret: new cbor.Tagged(ipld.LINK_TAG, [
'hello-world',
{
secret: 3
}
])
}

expect(
ipld.marshal(src)
).to.be.eql(
cbor.encode(expected)
() => ipld.marshal(src)
).to.throw(
'Links must not have siblings'
)
})

it('nested @link', () => {
it('nested /', () => {
const src = {
data: 'hello world',
size: 11,
'@link': 'hello-world',
l1: {'/': 'hello-world'},
secret: {
'@link': 'secret-link'
l1: {'/': 'secret-link'}
}
}

const expected = new cbor.Tagged(ipld.LINK_TAG, [
'hello-world',
{
data: 'hello world',
size: 11,
secret: new cbor.Tagged(ipld.LINK_TAG, 'secret-link')
const expected = {
data: 'hello world',
size: 11,
l1: new cbor.Tagged(ipld.LINK_TAG, 'hello-world'),
secret: {
l1: new cbor.Tagged(ipld.LINK_TAG, 'secret-link')
}
])
}

expect(
ipld.marshal(src)
Expand All @@ -127,13 +115,13 @@ describe('IPLD -> CBOR', () => {

it('does not modify the input', () => {
let src = {
'@link': 'hello'
l1: {'/': 'hello'}
}

ipld.marshal(src)

expect(src).to.be.eql({
'@link': 'hello'
l1: {'/': 'hello'}
})
})
})
Expand Down Expand Up @@ -168,71 +156,7 @@ describe('CBOR -> IPLD', () => {
data: 'hello world',
size: 11,
nested: {
'@link': 'hello-world'
}
}

expect(
ipld.unmarshal(src)
).to.be.eql(
target
)
})

it('one link, with properties', () => {
const src = cbor.encode({
data: 'hello world',
size: 11,
nested: new cbor.Tagged(ipld.LINK_TAG, [
'hello-world',
{cool: 'property'}
])
})

const target = {
data: 'hello world',
size: 11,
nested: {
'@link': 'hello-world',
cool: 'property'
}
}

expect(
ipld.unmarshal(src)
).to.be.eql(
target
)
})

it('nested links, with properties', () => {
const src = cbor.encode({
data: 'hello world',
size: 11,
secret: {
links: [
new cbor.Tagged(ipld.LINK_TAG, 'secret-link')
]
},
nested: new cbor.Tagged(ipld.LINK_TAG, [
'hello-world',
{
cool: 'property',
ref: new cbor.Tagged(ipld.LINK_TAG, 'world')
}
])
})

const target = {
data: 'hello world',
size: 11,
secret: {
links: [{'@link': 'secret-link'}]
},
nested: {
'@link': 'hello-world',
cool: 'property',
ref: {'@link': 'world'}
'/': 'hello-world'
}
}

Expand Down