Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ipld/dag-json

Package Overview
Dependencies
Maintainers
3
Versions
75
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ipld/dag-json - npm Package Compare versions

Comparing version 3.0.17 to 4.0.0

100

cjs/index.js
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var json = require('fast-json-stable-stringify');
var isCircular = require('@ipld/is-circular');
var transform = require('lodash.transform');
var multiformats = require('multiformats');
var base64 = require('multiformats/bases/base64');

@@ -13,40 +17,31 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var index = multiformats => {
const {CID, bytes, multibase} = multiformats;
const _encode = obj => transform__default['default'](obj, (result, value, key) => {
const cid = CID.asCID(value);
if (cid) {
result[key] = {
'/': cid.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 => transform__default['default'](obj, (result, value, key) => {
const cid = multiformats.CID.asCID(value);
if (cid) {
result[key] = { '/': cid.toString() };
} else if (multiformats.bytes.isBinary(value)) {
value = multiformats.bytes.coerce(value);
result[key] = { '/': { bytes: base64.base64.encode(value) } };
} else if (typeof value === 'object' && value !== null) {
result[key] = _encode(value);
} else {
result[key] = value;
}
});
const encode = obj => {
if (typeof obj === 'object' && !multiformats.bytes.isBinary(obj) && !multiformats.CID.asCID(obj) && obj) {
if (isCircular__default['default'](obj, { asCID: true })) {
throw new Error('Object contains circular references');
}
});
const encode = obj => {
if (typeof obj === 'object' && !bytes.isBinary(obj) && !CID.asCID(obj) && obj) {
if (isCircular__default['default'](CID, obj)) throw new Error('Object contains circular references.');
obj = _encode(obj);
}
return bytes.fromString(json__default['default'](obj));
};
const _decode = obj => transform__default['default'](obj, (result, value, key) => {
if (typeof value === 'object' && value !== null) {
if (value['/']) {
if (typeof value['/'] === 'string') {
result[key] = CID.from(value['/']);
} else if (typeof value['/'] === 'object' && value['/'].bytes) {
result[key] = multibase.decode(value['/'].bytes, 'base64');
} else {
result[key] = _decode(value);
}
obj = _encode(obj);
}
return multiformats.bytes.fromString(json__default['default'](obj));
};
const _decode = obj => transform__default['default'](obj, (result, value, key) => {
if (typeof value === 'object' && value !== null) {
if (value['/']) {
if (typeof value['/'] === 'string') {
result[key] = multiformats.CID.parse(value['/']);
} else if (typeof value['/'] === 'object' && value['/'].bytes) {
result[key] = base64.base64.decode(value['/'].bytes);
} else {

@@ -56,21 +51,18 @@ result[key] = _decode(value);

} else {
result[key] = value;
result[key] = _decode(value);
}
});
const decode = buffer => {
const obj = JSON.parse(bytes.toString(buffer));
return _decode({
value: obj
}).value;
};
const name = 'dag-json';
const code = 0x0129;
return {
encode,
decode,
name,
code
};
} else {
result[key] = value;
}
});
const decode = buffer => {
const obj = JSON.parse(multiformats.bytes.toString(buffer));
return _decode({ value: obj }).value;
};
const name = 'dag-json';
const code = 297;
module.exports = index;
exports.code = code;
exports.decode = decode;
exports.encode = encode;
exports.name = name;
import json from 'fast-json-stable-stringify';
import isCircular from '@ipld/is-circular';
import transform from 'lodash.transform';
export default multiformats => {
const {CID, bytes, multibase} = multiformats;
const _encode = obj => transform(obj, (result, value, key) => {
const cid = CID.asCID(value);
if (cid) {
result[key] = {
'/': cid.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;
import {
bytes,
CID
} from 'multiformats';
import { base64 } from 'multiformats/bases/base64';
const _encode = obj => transform(obj, (result, value, key) => {
const cid = CID.asCID(value);
if (cid) {
result[key] = { '/': cid.toString() };
} else if (bytes.isBinary(value)) {
value = bytes.coerce(value);
result[key] = { '/': { bytes: base64.encode(value) } };
} else if (typeof value === 'object' && value !== null) {
result[key] = _encode(value);
} else {
result[key] = value;
}
});
const encode = obj => {
if (typeof obj === 'object' && !bytes.isBinary(obj) && !CID.asCID(obj) && obj) {
if (isCircular(obj, { asCID: true })) {
throw new Error('Object contains circular references');
}
});
const encode = obj => {
if (typeof obj === 'object' && !bytes.isBinary(obj) && !CID.asCID(obj) && obj) {
if (isCircular(CID, obj)) throw new Error('Object contains circular references.');
obj = _encode(obj);
}
return bytes.fromString(json(obj));
};
const _decode = obj => transform(obj, (result, value, key) => {
if (typeof value === 'object' && value !== null) {
if (value['/']) {
if (typeof value['/'] === 'string') {
result[key] = CID.from(value['/']);
} else if (typeof value['/'] === 'object' && value['/'].bytes) {
result[key] = multibase.decode(value['/'].bytes, 'base64');
} else {
result[key] = _decode(value);
}
obj = _encode(obj);
}
return bytes.fromString(json(obj));
};
const _decode = obj => transform(obj, (result, value, key) => {
if (typeof value === 'object' && value !== null) {
if (value['/']) {
if (typeof value['/'] === 'string') {
result[key] = CID.parse(value['/']);
} else if (typeof value['/'] === 'object' && value['/'].bytes) {
result[key] = base64.decode(value['/'].bytes);
} else {

@@ -46,19 +42,19 @@ result[key] = _decode(value);

} else {
result[key] = value;
result[key] = _decode(value);
}
});
const decode = buffer => {
const obj = JSON.parse(bytes.toString(buffer));
return _decode({
value: obj
}).value;
};
const name = 'dag-json';
const code = 0x0129;
return {
encode,
decode,
name,
code
};
} else {
result[key] = value;
}
});
const decode = buffer => {
const obj = JSON.parse(bytes.toString(buffer));
return _decode({ value: obj }).value;
};
const name = 'dag-json';
const code = 297;
export {
encode,
decode,
name,
code
};
{
"name": "@ipld/dag-json",
"version": "3.0.17",
"version": "4.0.0",
"description": "JSON Directed Acrylic Graph for IPLD",

@@ -37,13 +37,12 @@ "directories": {

"mocha": "^8.1.1",
"multiformats": "3.0.3",
"polendina": "^1.1.0",
"rollup": "^2.26.4",
"standard": "^14.3.4"
},
"dependencies": {
"@ipld/is-circular": "^1.0.3",
"@ipld/is-circular": "^2.0.0",
"fast-json-stable-stringify": "^2.1.0",
"lodash.transform": "^4.6.0"
"lodash.transform": "^4.6.0",
"multiformats": "^4.0.0"
},
"browser": "./cjs/index.js"
}

@@ -1,14 +0,10 @@

## JSON Directed Acrylic Graph for IPLD
# @ipld/dag-json
You probably don't want to use this library directly and instead
access it through the Block interface.
An implementation of the [DAG-JSON spec](https://github.com/ipld/specs/blob/master/block-layer/codecs/dag-json.md) ("Directed Acyclic Graph for IPLD") for JavaScript designed for use with [multiformats](https://github.com/multiformats/js-multiformats) or via the higher-level `Block` abstraction in [@ipld/block](https://github.com/ipld/js-block).
Usage (w/ Block Interface):
## Example
```javascript
import multiformats from 'multiformats/basics'
import { create } from '@ipld/block'
multiformats.add(require('@ipld/dag-json'))
const Block = create(multiformats)
const { CID } = Block
import { encode, decode } from '@ipld/dag-json'
import { CID } from 'multiformats'

@@ -18,5 +14,5 @@ const obj = {

/* CID instances are encoded as links */
y: [2, 3, CID.from('QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4')],
y: [2, 3, CID.parse('QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4')],
z: {
a: CID.from('QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4'),
a: CID.parse('QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4'),
b: null,

@@ -27,5 +23,4 @@ c: 'string'

let encoder = Block.encoder(obj, 'dag-json')
let encoded = await Block.encode() // binary encoded block
let decoded = await Block.decoder(encoded, 'dag-json').decode()
let data = encode(obj)
let decoded = decode(data)
decoded.y[0] // 2

@@ -35,5 +30,15 @@ CID.asCID(decoded.z.a) // cid instance

# Spec
## Usage
The [`dag-json` spec is in the IPLD specs repo](https://github.com/ipld/specs/blob/master/block-layer/codecs/dag-json.md).
`@ipld/dag-pb` is designed to be used within multiformats but can be used separately. `encode()`, `decode()` are available as exports, as are `name` and `code` to match with the corresponding DAG-JSON [multicodec](https://github.com/multiformats/multicodec/).
## License
Licensed under either of
* Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / http://www.apache.org/licenses/LICENSE-2.0)
* MIT ([LICENSE-MIT](LICENSE-MIT) / http://opensource.org/licenses/MIT)
### Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc