Skip to content

Commit

Permalink
feat: remove url dependency in the browser
Browse files Browse the repository at this point in the history
also updates some deps and fixes linting needed because of aegir update
  • Loading branch information
hugomrdias authored and dignifiedquire committed Dec 24, 2018
1 parent 91149e2 commit 18a4974
Show file tree
Hide file tree
Showing 9 changed files with 3,741 additions and 1,506 deletions.
4 changes: 2 additions & 2 deletions benchmarks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ suite.add(`encode - borc - ${vecLength}`, () => {
})

suite.add(`encode - stream - borc - ${vecLength}`, () => {
const enc = new fastCbor.Encoder({stream (chunk) {
const enc = new fastCbor.Encoder({ stream (chunk) {
res.push(chunk)
}})
} })
for (let i = 0; i < vecLength; i++) {
enc.write(vectors[i].decoded)
}
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"email": "dignifiedquire@gmail.com"
},
"devDependencies": {
"aegir": "^13.0.6",
"aegir": "^18.0.1",
"benchmark": "^2.1.0",
"budo": "^11.2.0",
"cbor": "^4.0.0",
Expand All @@ -49,10 +49,11 @@
"license": "MIT",
"readmeFilename": "README.md",
"dependencies": {
"bignumber.js": "^7.2.1",
"bignumber.js": "^8.0.1",
"commander": "^2.15.0",
"ieee754": "^1.1.8",
"json-text-sequence": "^0.1"
"iso-url": "~0.4.4",
"json-text-sequence": "~0.1.0"
},
"engines": {
"node": ">=4"
Expand Down
8 changes: 4 additions & 4 deletions src/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const utils = require('./utils')
const c = require('./constants')
const Simple = require('./simple')
const Tagged = require('./tagged')
const url = require('url')
const { URL } = require('iso-url')

/**
* Transform binary cbor data into JavaScript objects.
Expand Down Expand Up @@ -49,7 +49,7 @@ class Decoder {
// const v = new Uint8Array(val)
return c.TWO.pow(v[0]).times(v[1])
},
32: (val) => url.parse(val),
32: (val) => new URL(val),
35: (val) => new RegExp(val)
}, opts.tags)

Expand Down Expand Up @@ -595,7 +595,7 @@ class Decoder {
input = Buffer.from(input, enc || 'hex')
}

const dec = new Decoder({size: input.length})
const dec = new Decoder({ size: input.length })
return dec.decodeFirst(input)
}

Expand All @@ -611,7 +611,7 @@ class Decoder {
input = Buffer.from(input, enc || 'hex')
}

const dec = new Decoder({size: input.length})
const dec = new Decoder({ size: input.length })
return dec.decodeAll(input)
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/encoder.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const url = require('url')
const { URL } = require('iso-url')
const Bignumber = require('bignumber.js')

const utils = require('./utils')
Expand Down Expand Up @@ -45,7 +45,7 @@ class Encoder {
this.onData = options.stream

this.semanticTypes = [
[url.Url, this._pushUrl],
[URL, this._pushUrl],
[Bignumber, this._pushBigNumber]
]

Expand Down Expand Up @@ -412,6 +412,10 @@ class Encoder {
return this._pushMap(this, obj)
case 'Set':
return this._pushSet(this, obj)
case 'URL':
return this._pushUrl(this, obj)
case 'BigNumber':
return this._pushBigNumber(this, obj)
case 'Date':
return this._pushDate(this, obj)
case 'RegExp':
Expand Down
6 changes: 3 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ exports.writeHalf = function writeHalf (buf, half) {
}
s16 += ((mant + 0x800000) >> (126 - exp))

// } else if (exp == 255 && mant == 0) { /* Inf */
// s16 += 0x7c00;
// } else if (exp == 255 && mant == 0) { /* Inf */
// s16 += 0x7c00;

// hildjj: Infinity already handled
// hildjj: Infinity already handled

// } else
// goto float32; /* loss of range */
Expand Down
8 changes: 4 additions & 4 deletions test/decoder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@ describe('Decoder', function () {
describe('misc', () => {
it('custom tags', () => {
function replaceTag (val) {
return {foo: val}
return { foo: val }
}

function newTag (val) {
return 'cool'
}

const d = new cbor.Decoder({
tags: {0: replaceTag, 127: newTag}
tags: { 0: replaceTag, 127: newTag }
})

const input = Buffer.from('d87f01c001', 'hex')

expect(
d.decodeAll(input)
).to.be.eql([
'cool', {foo: 1}
'cool', { foo: 1 }
])
})

Expand Down Expand Up @@ -142,7 +142,7 @@ describe('Decoder', function () {
// TODO: implement depth limit
it.skip('depth', () => {
expect(
() => cbor.decodeFirst('818180', {max_depth: 1})
() => cbor.decodeFirst('818180', { max_depth: 1 })
).to.throw()
})
})
Expand Down
6 changes: 3 additions & 3 deletions test/encoder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ describe('encoder', () => {

enc.write('hello')
enc.write('world')
enc.write({a: 1})
enc.write({ a: 1 })
enc.write(123456)

expect(chunks).to.be.eql([
Expand All @@ -141,7 +141,7 @@ describe('encoder', () => {
cases.EncodeFailer.tryAll(new Bignum(0))
cases.EncodeFailer.tryAll(new Bignum(1.1))
cases.EncodeFailer.tryAll(new Map([[1, 2], ['a', null]]))
cases.EncodeFailer.tryAll({a: 1, b: null})
cases.EncodeFailer.tryAll({ a: 1, b: null })
cases.EncodeFailer.tryAll(undefined)
})

Expand All @@ -154,7 +154,7 @@ describe('encoder', () => {
)

expect(
cbor.Encoder.encode({aa: 2, b: 1}).toString('hex')
cbor.Encoder.encode({ aa: 2, b: 1 }).toString('hex')
).to.be.eql(
'a261620162616102'
)
Expand Down
30 changes: 15 additions & 15 deletions test/fixtures/cases.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const Bignum = require('bignumber.js')
const url = require('url')
const { URL } = require('iso-url')
const expect = require('chai').expect

const cbor = require('../../')
Expand Down Expand Up @@ -171,7 +171,7 @@ exports.good = [
514b67b0 -- 1363896240
0xc11a514b67b0`],

[url.parse('http://www.example.com'), '32("http://www.example.com/")', `
[new URL('http://www.example.com'), '32("http://www.example.com/")', `
d8 -- next 1 byte
20 -- Tag #32
77 -- String, length: 23
Expand Down Expand Up @@ -270,7 +270,7 @@ exports.good = [
[{}, '{}', `
a0 -- {}
0xa0`],
[{1: 2, 3: 4}, '{"1": 2, "3": 4}', `
[{ 1: 2, 3: 4 }, '{"1": 2, "3": 4}', `
a2 -- Map, 2 pairs
61 -- String, length: 1
31 -- {Key:0}, "1"
Expand All @@ -279,7 +279,7 @@ exports.good = [
33 -- {Key:1}, "3"
04 -- {Val:1}, 4
0xa2613102613304`],
[{a: 1, b: [2, 3]}, '{"a": 1, "b": [2, 3]}', `
[{ a: 1, b: [2, 3] }, '{"a": 1, "b": [2, 3]}', `
a2 -- Map, 2 pairs
61 -- String, length: 1
61 -- {Key:0}, "a"
Expand All @@ -290,7 +290,7 @@ exports.good = [
02 -- [0], 2
03 -- [1], 3
0xa26161016162820203`],
[['a', {b: 'c'}], '["a", {"b": "c"}]', `
[['a', { b: 'c' }], '["a", {"b": "c"}]', `
82 -- Array, 2 items
61 -- String, length: 1
61 -- [0], "a"
Expand All @@ -300,7 +300,7 @@ exports.good = [
61 -- String, length: 1
63 -- {Val:0}, "c"
0x826161a161626163`],
[{a: 'A', b: 'B', c: 'C', d: 'D', e: 'E'}, '{"a": "A", "b": "B", "c": "C", "d": "D", "e": "E"}', `
[{ a: 'A', b: 'B', c: 'C', d: 'D', e: 'E' }, '{"a": "A", "b": "B", "c": "C", "d": "D", "e": "E"}', `
a5 -- Map, 5 pairs
61 -- String, length: 1
61 -- {Key:0}, "a"
Expand All @@ -323,8 +323,8 @@ exports.good = [
61 -- String, length: 1
45 -- {Val:4}, "E"
0xa56161614161626142616361436164614461656145`],
[{1: {2: {3: { 4: {5: {6: {
7: {8: {9: {10: {11: {12: {
[{ 1: { 2: { 3: { 4: { 5: { 6: {
7: { 8: { 9: { 10: { 11: { 12: {
13: 'hello', 14: 'world'
} } } } } }
} } } } } } }, `{"1": {"2": {"3": {"4": {"5": {"6": {"7": {"8": {"9": {"10": {"11": {"12": {"13": "hello", "14": "world"}}}}}}}}}}}}}`, `
Expand Down Expand Up @@ -423,7 +423,7 @@ a1 # map(1)
18 -- Positive number, next 1 byte
19 -- [24], 25
0x98190102030405060708090a0b0c0d0e0f101112131415161718181819`],
[{a: 1, b: [2, 3]}, '{"a": 1, "b": [2, 3]}', `
[{ a: 1, b: [2, 3] }, '{"a": 1, "b": [2, 3]}', `
a2 -- Map, 2 pairs
61 -- String, length: 1
61 -- {Key:0}, "a"
Expand All @@ -434,7 +434,7 @@ a1 # map(1)
02 -- [0], 2
03 -- [1], 3
0xa26161016162820203`],
[['a', {b: 'c'}], '["a", {"b": "c"}]', `
[['a', { b: 'c' }], '["a", {"b": "c"}]', `
82 -- Array, 2 items
61 -- String, length: 1
61 -- [0], "a"
Expand Down Expand Up @@ -559,7 +559,7 @@ a1 # map(1)
01 -- {Key:0}, 1
02 -- {Val:0}, 2
0xa10102`],
[new Map([[{b: 1}, {b: 1}]]), '{{"b": 1}: {"b": 1}}', `
[new Map([[{ b: 1 }, { b: 1 }]]), '{{"b": 1}: {"b": 1}}', `
a1 -- Map, 1 pair
a1 -- {Key:0}, Map, 1 pair
61 -- String, length: 1
Expand Down Expand Up @@ -897,7 +897,7 @@ exports.decodeGood = [
19 -- [24], 25
ff -- BREAK
0x9f0102030405060708090a0b0c0d0e0f101112131415161718181819ff`],
[{a: 1, b: [2, 3]}, '{_ "a": 1, "b": [_ 2, 3]}', `
[{ a: 1, b: [2, 3] }, '{_ "a": 1, "b": [_ 2, 3]}', `
bf -- Map (streaming)
61 -- String, length: 1
61 -- {Key:0}, "a"
Expand All @@ -910,7 +910,7 @@ exports.decodeGood = [
ff -- BREAK
ff -- BREAK
0xbf61610161629f0203ffff`],
[['a', {b: 'c'}], '["a", {_ "b": "c"}]', `
[['a', { b: 'c' }], '["a", {_ "b": "c"}]', `
82 -- Array, 2 items
61 -- String, length: 1
61 -- [0], "a"
Expand Down Expand Up @@ -939,7 +939,7 @@ exports.decodeGood = [
eeff99 -- eeff99p
ff -- BREAK
0xd8405f44aabbccdd43eeff99ff`],
[{'!5{': ['-Lhe2xMsDYB!_', [1.9054760350149715, {'{_aC(z~> ': -1.267609797117641}, [{'9\n': [undefined]}]]]}, '{"!5{": ["-Lhe2xMsDYB!_", [1.9054760350149715_3, {"{_aC(z~> ": -1.267609797117641_3}, [{"9\n": [undefined]}]]]}', `
[{ '!5{': ['-Lhe2xMsDYB!_', [1.9054760350149715, { '{_aC(z~> ': -1.267609797117641 }, [{ '9\n': [undefined] }]]] }, '{"!5{": ["-Lhe2xMsDYB!_", [1.9054760350149715_3, {"{_aC(z~> ": -1.267609797117641_3}, [{"9\n": [undefined]}]]]}', `
a1 # map(1)
63 # text(3)
21357b # "!5{"
Expand Down Expand Up @@ -1114,7 +1114,7 @@ exports.goodMap = new Map([
[[], 'empty array'],
[null, 'null'],
[[1], 'array'],
[{1: 2}, 'obj'],
[{ 1: 2 }, 'obj'],
['a', 1],
['aaa', 3],
['aa', 2],
Expand Down

0 comments on commit 18a4974

Please sign in to comment.