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 2.0.10 to 3.0.0

test.js

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

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