Skip to content

Commit 18a4974

Browse files
hugomrdiasdignifiedquire
authored andcommitted
feat: remove url dependency in the browser
also updates some deps and fixes linting needed because of aegir update
1 parent 91149e2 commit 18a4974

File tree

9 files changed

+3741
-1506
lines changed

9 files changed

+3741
-1506
lines changed

benchmarks/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ suite.add(`encode - borc - ${vecLength}`, () => {
4040
})
4141

4242
suite.add(`encode - stream - borc - ${vecLength}`, () => {
43-
const enc = new fastCbor.Encoder({stream (chunk) {
43+
const enc = new fastCbor.Encoder({ stream (chunk) {
4444
res.push(chunk)
45-
}})
45+
} })
4646
for (let i = 0; i < vecLength; i++) {
4747
enc.write(vectors[i].decoded)
4848
}

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"email": "dignifiedquire@gmail.com"
3939
},
4040
"devDependencies": {
41-
"aegir": "^13.0.6",
41+
"aegir": "^18.0.1",
4242
"benchmark": "^2.1.0",
4343
"budo": "^11.2.0",
4444
"cbor": "^4.0.0",
@@ -49,10 +49,11 @@
4949
"license": "MIT",
5050
"readmeFilename": "README.md",
5151
"dependencies": {
52-
"bignumber.js": "^7.2.1",
52+
"bignumber.js": "^8.0.1",
5353
"commander": "^2.15.0",
5454
"ieee754": "^1.1.8",
55-
"json-text-sequence": "^0.1"
55+
"iso-url": "~0.4.4",
56+
"json-text-sequence": "~0.1.0"
5657
},
5758
"engines": {
5859
"node": ">=4"

src/decoder.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const utils = require('./utils')
88
const c = require('./constants')
99
const Simple = require('./simple')
1010
const Tagged = require('./tagged')
11-
const url = require('url')
11+
const { URL } = require('iso-url')
1212

1313
/**
1414
* Transform binary cbor data into JavaScript objects.
@@ -49,7 +49,7 @@ class Decoder {
4949
// const v = new Uint8Array(val)
5050
return c.TWO.pow(v[0]).times(v[1])
5151
},
52-
32: (val) => url.parse(val),
52+
32: (val) => new URL(val),
5353
35: (val) => new RegExp(val)
5454
}, opts.tags)
5555

@@ -595,7 +595,7 @@ class Decoder {
595595
input = Buffer.from(input, enc || 'hex')
596596
}
597597

598-
const dec = new Decoder({size: input.length})
598+
const dec = new Decoder({ size: input.length })
599599
return dec.decodeFirst(input)
600600
}
601601

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

614-
const dec = new Decoder({size: input.length})
614+
const dec = new Decoder({ size: input.length })
615615
return dec.decodeAll(input)
616616
}
617617
}

src/encoder.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

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

66
const utils = require('./utils')
@@ -45,7 +45,7 @@ class Encoder {
4545
this.onData = options.stream
4646

4747
this.semanticTypes = [
48-
[url.Url, this._pushUrl],
48+
[URL, this._pushUrl],
4949
[Bignumber, this._pushBigNumber]
5050
]
5151

@@ -412,6 +412,10 @@ class Encoder {
412412
return this._pushMap(this, obj)
413413
case 'Set':
414414
return this._pushSet(this, obj)
415+
case 'URL':
416+
return this._pushUrl(this, obj)
417+
case 'BigNumber':
418+
return this._pushBigNumber(this, obj)
415419
case 'Date':
416420
return this._pushDate(this, obj)
417421
case 'RegExp':

src/utils.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ exports.writeHalf = function writeHalf (buf, half) {
118118
}
119119
s16 += ((mant + 0x800000) >> (126 - exp))
120120

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

124-
// hildjj: Infinity already handled
124+
// hildjj: Infinity already handled
125125

126126
// } else
127127
// goto float32; /* loss of range */

test/decoder.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,23 @@ describe('Decoder', function () {
3434
describe('misc', () => {
3535
it('custom tags', () => {
3636
function replaceTag (val) {
37-
return {foo: val}
37+
return { foo: val }
3838
}
3939

4040
function newTag (val) {
4141
return 'cool'
4242
}
4343

4444
const d = new cbor.Decoder({
45-
tags: {0: replaceTag, 127: newTag}
45+
tags: { 0: replaceTag, 127: newTag }
4646
})
4747

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

5050
expect(
5151
d.decodeAll(input)
5252
).to.be.eql([
53-
'cool', {foo: 1}
53+
'cool', { foo: 1 }
5454
])
5555
})
5656

@@ -142,7 +142,7 @@ describe('Decoder', function () {
142142
// TODO: implement depth limit
143143
it.skip('depth', () => {
144144
expect(
145-
() => cbor.decodeFirst('818180', {max_depth: 1})
145+
() => cbor.decodeFirst('818180', { max_depth: 1 })
146146
).to.throw()
147147
})
148148
})

test/encoder.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ describe('encoder', () => {
119119

120120
enc.write('hello')
121121
enc.write('world')
122-
enc.write({a: 1})
122+
enc.write({ a: 1 })
123123
enc.write(123456)
124124

125125
expect(chunks).to.be.eql([
@@ -141,7 +141,7 @@ describe('encoder', () => {
141141
cases.EncodeFailer.tryAll(new Bignum(0))
142142
cases.EncodeFailer.tryAll(new Bignum(1.1))
143143
cases.EncodeFailer.tryAll(new Map([[1, 2], ['a', null]]))
144-
cases.EncodeFailer.tryAll({a: 1, b: null})
144+
cases.EncodeFailer.tryAll({ a: 1, b: null })
145145
cases.EncodeFailer.tryAll(undefined)
146146
})
147147

@@ -154,7 +154,7 @@ describe('encoder', () => {
154154
)
155155

156156
expect(
157-
cbor.Encoder.encode({aa: 2, b: 1}).toString('hex')
157+
cbor.Encoder.encode({ aa: 2, b: 1 }).toString('hex')
158158
).to.be.eql(
159159
'a261620162616102'
160160
)

test/fixtures/cases.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const Bignum = require('bignumber.js')
4-
const url = require('url')
4+
const { URL } = require('iso-url')
55
const expect = require('chai').expect
66

77
const cbor = require('../../')
@@ -171,7 +171,7 @@ exports.good = [
171171
514b67b0 -- 1363896240
172172
0xc11a514b67b0`],
173173

174-
[url.parse('http://www.example.com'), '32("http://www.example.com/")', `
174+
[new URL('http://www.example.com'), '32("http://www.example.com/")', `
175175
d8 -- next 1 byte
176176
20 -- Tag #32
177177
77 -- String, length: 23
@@ -270,7 +270,7 @@ exports.good = [
270270
[{}, '{}', `
271271
a0 -- {}
272272
0xa0`],
273-
[{1: 2, 3: 4}, '{"1": 2, "3": 4}', `
273+
[{ 1: 2, 3: 4 }, '{"1": 2, "3": 4}', `
274274
a2 -- Map, 2 pairs
275275
61 -- String, length: 1
276276
31 -- {Key:0}, "1"
@@ -279,7 +279,7 @@ exports.good = [
279279
33 -- {Key:1}, "3"
280280
04 -- {Val:1}, 4
281281
0xa2613102613304`],
282-
[{a: 1, b: [2, 3]}, '{"a": 1, "b": [2, 3]}', `
282+
[{ a: 1, b: [2, 3] }, '{"a": 1, "b": [2, 3]}', `
283283
a2 -- Map, 2 pairs
284284
61 -- String, length: 1
285285
61 -- {Key:0}, "a"
@@ -290,7 +290,7 @@ exports.good = [
290290
02 -- [0], 2
291291
03 -- [1], 3
292292
0xa26161016162820203`],
293-
[['a', {b: 'c'}], '["a", {"b": "c"}]', `
293+
[['a', { b: 'c' }], '["a", {"b": "c"}]', `
294294
82 -- Array, 2 items
295295
61 -- String, length: 1
296296
61 -- [0], "a"
@@ -300,7 +300,7 @@ exports.good = [
300300
61 -- String, length: 1
301301
63 -- {Val:0}, "c"
302302
0x826161a161626163`],
303-
[{a: 'A', b: 'B', c: 'C', d: 'D', e: 'E'}, '{"a": "A", "b": "B", "c": "C", "d": "D", "e": "E"}', `
303+
[{ a: 'A', b: 'B', c: 'C', d: 'D', e: 'E' }, '{"a": "A", "b": "B", "c": "C", "d": "D", "e": "E"}', `
304304
a5 -- Map, 5 pairs
305305
61 -- String, length: 1
306306
61 -- {Key:0}, "a"
@@ -323,8 +323,8 @@ exports.good = [
323323
61 -- String, length: 1
324324
45 -- {Val:4}, "E"
325325
0xa56161614161626142616361436164614461656145`],
326-
[{1: {2: {3: { 4: {5: {6: {
327-
7: {8: {9: {10: {11: {12: {
326+
[{ 1: { 2: { 3: { 4: { 5: { 6: {
327+
7: { 8: { 9: { 10: { 11: { 12: {
328328
13: 'hello', 14: 'world'
329329
} } } } } }
330330
} } } } } } }, `{"1": {"2": {"3": {"4": {"5": {"6": {"7": {"8": {"9": {"10": {"11": {"12": {"13": "hello", "14": "world"}}}}}}}}}}}}}`, `
@@ -423,7 +423,7 @@ a1 # map(1)
423423
18 -- Positive number, next 1 byte
424424
19 -- [24], 25
425425
0x98190102030405060708090a0b0c0d0e0f101112131415161718181819`],
426-
[{a: 1, b: [2, 3]}, '{"a": 1, "b": [2, 3]}', `
426+
[{ a: 1, b: [2, 3] }, '{"a": 1, "b": [2, 3]}', `
427427
a2 -- Map, 2 pairs
428428
61 -- String, length: 1
429429
61 -- {Key:0}, "a"
@@ -434,7 +434,7 @@ a1 # map(1)
434434
02 -- [0], 2
435435
03 -- [1], 3
436436
0xa26161016162820203`],
437-
[['a', {b: 'c'}], '["a", {"b": "c"}]', `
437+
[['a', { b: 'c' }], '["a", {"b": "c"}]', `
438438
82 -- Array, 2 items
439439
61 -- String, length: 1
440440
61 -- [0], "a"
@@ -559,7 +559,7 @@ a1 # map(1)
559559
01 -- {Key:0}, 1
560560
02 -- {Val:0}, 2
561561
0xa10102`],
562-
[new Map([[{b: 1}, {b: 1}]]), '{{"b": 1}: {"b": 1}}', `
562+
[new Map([[{ b: 1 }, { b: 1 }]]), '{{"b": 1}: {"b": 1}}', `
563563
a1 -- Map, 1 pair
564564
a1 -- {Key:0}, Map, 1 pair
565565
61 -- String, length: 1
@@ -897,7 +897,7 @@ exports.decodeGood = [
897897
19 -- [24], 25
898898
ff -- BREAK
899899
0x9f0102030405060708090a0b0c0d0e0f101112131415161718181819ff`],
900-
[{a: 1, b: [2, 3]}, '{_ "a": 1, "b": [_ 2, 3]}', `
900+
[{ a: 1, b: [2, 3] }, '{_ "a": 1, "b": [_ 2, 3]}', `
901901
bf -- Map (streaming)
902902
61 -- String, length: 1
903903
61 -- {Key:0}, "a"
@@ -910,7 +910,7 @@ exports.decodeGood = [
910910
ff -- BREAK
911911
ff -- BREAK
912912
0xbf61610161629f0203ffff`],
913-
[['a', {b: 'c'}], '["a", {_ "b": "c"}]', `
913+
[['a', { b: 'c' }], '["a", {_ "b": "c"}]', `
914914
82 -- Array, 2 items
915915
61 -- String, length: 1
916916
61 -- [0], "a"
@@ -939,7 +939,7 @@ exports.decodeGood = [
939939
eeff99 -- eeff99p
940940
ff -- BREAK
941941
0xd8405f44aabbccdd43eeff99ff`],
942-
[{'!5{': ['-Lhe2xMsDYB!_', [1.9054760350149715, {'{_aC(z~> ': -1.267609797117641}, [{'9\n': [undefined]}]]]}, '{"!5{": ["-Lhe2xMsDYB!_", [1.9054760350149715_3, {"{_aC(z~> ": -1.267609797117641_3}, [{"9\n": [undefined]}]]]}', `
942+
[{ '!5{': ['-Lhe2xMsDYB!_', [1.9054760350149715, { '{_aC(z~> ': -1.267609797117641 }, [{ '9\n': [undefined] }]]] }, '{"!5{": ["-Lhe2xMsDYB!_", [1.9054760350149715_3, {"{_aC(z~> ": -1.267609797117641_3}, [{"9\n": [undefined]}]]]}', `
943943
a1 # map(1)
944944
63 # text(3)
945945
21357b # "!5{"
@@ -1114,7 +1114,7 @@ exports.goodMap = new Map([
11141114
[[], 'empty array'],
11151115
[null, 'null'],
11161116
[[1], 'array'],
1117-
[{1: 2}, 'obj'],
1117+
[{ 1: 2 }, 'obj'],
11181118
['a', 1],
11191119
['aaa', 3],
11201120
['aa', 2],

0 commit comments

Comments
 (0)