@ipld/dag-json
Advanced tools
Comparing version 2.0.10 to 3.0.0
85
index.js
@@ -1,48 +0,53 @@ | ||
'use strict' | ||
const json = require('fast-json-stable-stringify') | ||
const CID = require('cids') | ||
const isCircular = require('is-circular') | ||
const transform = require('lodash.transform') | ||
const codecInterface = require('@ipld/codec-interface') | ||
import json from 'fast-json-stable-stringify' | ||
import isCircular from 'is-circular' | ||
import transform from 'lodash.transform' | ||
const _encode = obj => transform(obj, (result, value, key) => { | ||
if (CID.isCID(value)) { | ||
result[key] = { '/': value.toBaseEncodedString() } | ||
} else if (Buffer.isBuffer(value)) { | ||
result[key] = { '/': { base64: value.toString('base64') } } | ||
} else if (typeof value === 'object' && value !== null) { | ||
result[key] = _encode(value) | ||
} else { | ||
result[key] = value | ||
} | ||
}) | ||
export default multiformats => { | ||
const { CID, bytes, multibase } = multiformats | ||
const _encode = obj => transform(obj, (result, value, key) => { | ||
if (CID.isCID(value)) { | ||
result[key] = { '/': value.toString() } | ||
} else if (bytes.isBinary(value)) { | ||
value = bytes.coerce(value) | ||
result[key] = { '/': { bytes: multibase.encode(value, 'base64') } } | ||
} else if (typeof value === 'object' && value !== null) { | ||
result[key] = _encode(value) | ||
} else { | ||
result[key] = value | ||
} | ||
}) | ||
const encode = obj => { | ||
if (isCircular(obj)) { | ||
throw new Error('Object contains circular references.') | ||
const encode = obj => { | ||
if (isCircular(obj)) { | ||
throw new Error('Object contains circular references.') | ||
} | ||
const data = _encode(obj) | ||
return bytes.fromString(json(data)) | ||
} | ||
const data = _encode(obj) | ||
return Buffer.from(json(data)) | ||
} | ||
const _decode = obj => transform(obj, (result, value, key) => { | ||
if (typeof value === 'object' && value !== null) { | ||
if (value['/']) { | ||
if (typeof value['/'] === 'string') result[key] = new CID(value['/']) | ||
else if (typeof value['/'] === 'object' && value['/'].base64) { | ||
result[key] = Buffer.from(value['/'].base64, 'base64') | ||
} else result[key] = _decode(value) | ||
const _decode = obj => transform(obj, (result, value, key) => { | ||
if (typeof value === 'object' && value !== null) { | ||
if (value['/']) { | ||
if (typeof value['/'] === 'string') { | ||
result[key] = new CID(value['/']) | ||
} else if (typeof value['/'] === 'object' && value['/'].bytes) { | ||
result[key] = multibase.decode(value['/'].bytes, 'base64') | ||
} else { | ||
result[key] = _decode(value) | ||
} | ||
} else { | ||
result[key] = _decode(value) | ||
} | ||
} else { | ||
result[key] = _decode(value) | ||
result[key] = value | ||
} | ||
} else { | ||
result[key] = value | ||
}) | ||
const decode = buffer => { | ||
const obj = JSON.parse(bytes.toString(buffer)) | ||
return _decode({ value: obj }).value | ||
} | ||
}) | ||
const decode = buffer => { | ||
const obj = JSON.parse(buffer.toString()) | ||
return _decode({ value: obj }).value | ||
const name = 'dag-json' | ||
const code = 0x0129 | ||
return { encode, decode, name, code } | ||
} | ||
module.exports = codecInterface.create(encode, decode, 'dag-json') |
{ | ||
"name": "@ipld/dag-json", | ||
"version": "2.0.10", | ||
"version": "3.0.0", | ||
"description": "JSON Directed Acrylic Graph for IPLD", | ||
"main": "index.js", | ||
"type": "module", | ||
"directories": { | ||
@@ -10,4 +11,6 @@ "test": "test" | ||
"scripts": { | ||
"test": "hundreds aegir test", | ||
"pretest": "aegir lint" | ||
"test": "hundreds mocha test.js && npm run test:browser", | ||
"test:browser": "polendina --cleanup test.js", | ||
"coverage": "c8 --reporter=html mocha test.js && npx st -d coverage -p 8080", | ||
"pretest": "standard" | ||
}, | ||
@@ -26,9 +29,9 @@ "repository": { | ||
"devDependencies": { | ||
"aegir": "^21.3.0", | ||
"hundreds": "0.0.2", | ||
"tsame": "^2.0.1" | ||
"hundreds": "0.0.4", | ||
"mocha": "^8.0.1", | ||
"multiformats": "1.0.3", | ||
"polendina": "^1.0.0", | ||
"standard": "^14.3.4" | ||
}, | ||
"dependencies": { | ||
"@ipld/codec-interface": "^1.0.8", | ||
"cids": "~0.7.3", | ||
"fast-json-stable-stringify": "^2.1.0", | ||
@@ -35,0 +38,0 @@ "is-circular": "^1.0.2", |
@@ -6,7 +6,11 @@ ## JSON Directed Acrylic Graph for IPLD | ||
Usage: | ||
Usage (w/ Block Interface): | ||
```javascript | ||
const Block = require('@ipld/block') | ||
const CID = require('cids') | ||
import multiformats from 'multiformats/basics' | ||
import { create } from '@ipld/block' | ||
multiformats.add(require('@ipld/dag-json')) | ||
const Block = create(multiformats) | ||
const { CID } = Block | ||
const obj = { | ||
@@ -45,4 +49,1 @@ x: 1, | ||
# API | ||
This library exposes an interface conforming to [`@ipld/codec-interface`](https://github.com/ipld/js-ipld-codec-interface). |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6059
3
89
48
Yes
5
- Removed@ipld/codec-interface@^1.0.8
- Removedcids@~0.7.3
- Removed@ipld/codec-interface@1.0.14(transitive)
- Removed@multiformats/base-x@4.0.1(transitive)
- Removedbase-x@3.0.10(transitive)
- Removedbase64-js@1.5.1(transitive)
- Removedbuffer@5.7.1(transitive)
- Removedcids@0.7.51.1.9(transitive)
- Removedclass-is@1.1.0(transitive)
- Removedieee754@1.2.1(transitive)
- Removedmultibase@0.6.10.7.04.0.6(transitive)
- Removedmulticodec@1.0.43.2.1(transitive)
- Removedmultiformats@9.9.0(transitive)
- Removedmultihashes@0.4.214.0.3(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removeduint8arrays@3.1.1(transitive)
- Removedvarint@5.0.26.0.0(transitive)