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

Commit b20f90b

Browse files
fix: Ensure inputs are not modified
1 parent cb8267d commit b20f90b

4 files changed

Lines changed: 17 additions & 2 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"dependencies": {
3636
"bs58": "^3.0.0",
3737
"cbor": "^1.0.1",
38+
"lodash.clonedeep": "^4.3.2",
3839
"lodash.defaults": "^4.0.1",
3940
"lodash.includes": "^4.1.2",
4041
"multiaddr": "^1.3.0",

src/cbor.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ const Multiaddr = require('multiaddr')
66
const NoFilter = require('nofilter')
77
const defaults = require('lodash.defaults')
88
const includes = require('lodash.includes')
9+
const cloneDeep = require('lodash.cloneDeep')
910

1011
exports = module.exports
1112

1213
exports.LINK_TAG = 258
1314

14-
exports.marshal = (input) => {
15+
exports.marshal = (original) => {
16+
const input = cloneDeep(original)
17+
1518
function transform (obj) {
1619
const keys = Object.keys(obj)
1720

src/multihash.js

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

3-
const crypto = require('crypto')
43
const multihashing = require('multihashing')
54
const bs58 = require('bs58')
65

test/cbor.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,18 @@ describe('IPLD -> CBOR', () => {
124124
cbor.encode(expected)
125125
)
126126
})
127+
128+
it('does not modify the input', () => {
129+
let src = {
130+
'@link': 'hello'
131+
}
132+
133+
ipld.marshal(src)
134+
135+
expect(src).to.be.eql({
136+
'@link': 'hello'
137+
})
138+
})
127139
})
128140

129141
describe('CBOR -> IPLD', () => {

0 commit comments

Comments
 (0)