This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: enable custom formats for dag put and get #3347
Merged
achingbrain
merged 15 commits into
master
from
feat/enable-custom-formats-for-dag-put-and-get
Oct 27, 2020
Merged
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e3a5daf
feat: enable custom formats for server/client DAG (put|get) operations
simonovic86 97f7fc5
fix: fix possible undefined
simonovic86 d246a4d
chore: refactor format fetch
simonovic86 a8f0825
fix: pass opts to the server
simonovic86 1258569
feat: pass opts
simonovic86 64d37b9
chore: add documentation
simonovic86 f8b9389
fix: fix lint
simonovic86 72129bf
Merge remote-tracking branch 'origin/master' into feat/enable-custom-…
achingbrain 9c17509
feat: enable custom formats for server/client DAG get and put
achingbrain 0aea176
chore: remove extra options
achingbrain 03a9e65
chore: add example showing how to do custom ipld formats
achingbrain 2eba17a
chore: remove redundant options arg
achingbrain a856766
docs: update examples/custom-ipld-formats/README.md
achingbrain 26fdb20
chore: destructure ipld-formats args
achingbrain 00c06d5
chore: upgrade ipld
achingbrain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Custom IPLD formats | ||
|
||
This example shows you how to configure an IPFS daemon with the ability to load extra IPLD formats so you can use them in your applications. | ||
|
||
## Before you start | ||
|
||
First clone this repo, install dependencies in the project root and build the project. | ||
|
||
```console | ||
$ git clone https://github.com/ipfs/js-ipfs.git | ||
$ cd js-ipfs | ||
$ npm install | ||
$ npm run build | ||
``` | ||
|
||
## Running the example | ||
|
||
Running this example should result in metrics being logged out to the console every few seconds. | ||
|
||
``` | ||
> npm start | ||
``` | ||
|
||
## Play with the configuration! | ||
|
||
By default, IPFS is only configured to support a few common IPLD formats. Your application may require extra or more esoteric formats, in which case you can configure your node to support them using `options.ipld.formats` passed to the client or an in-process node or even a daemon if you start it with a wrapper. | ||
|
||
See the following files for different configuration: | ||
|
||
* [./in-process-node.js](./in-process-node.js) for running an in-process node as part of your confiugration | ||
* [./daemon-node.js](./daemon-node.js) for running a node as a separate daemon process |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// ordinarily we'd open a PR against the multicodec module to get our | ||
// codec number added but since we're just testing we shim our new | ||
// codec into the base-table.json file - this has to be done | ||
// before requiring other modules as the int table will become read-only | ||
const codecName = 'dag-test' | ||
const codecNumber = 392091 | ||
|
||
const baseTable = require('multicodec/src/base-table.json') | ||
baseTable[codecName] = codecNumber | ||
|
||
// now require modules as usual | ||
const IPFSDaemon = require('ipfs-cli/src/daemon') | ||
const multihashing = require('multihashing-async') | ||
const multihash = multihashing.multihash | ||
const multicodec = require('multicodec') | ||
const CID = require('cids') | ||
const ipfsHttpClient = require('ipfs-http-client') | ||
const uint8ArrayToString = require('uint8arrays/to-string') | ||
|
||
async function main () { | ||
// see https://github.com/ipld/interface-ipld-format for the interface definition | ||
const format = { | ||
codec: codecNumber, | ||
defaultHashAlg: multicodec.SHA2_256, | ||
util: { | ||
serialize (data) { | ||
return Buffer.from(JSON.stringify(data)) | ||
}, | ||
deserialize (buf) { | ||
return JSON.parse(uint8ArrayToString(buf)) | ||
}, | ||
async cid (buf) { | ||
const multihash = await multihashing(buf, format.defaultHashAlg) | ||
|
||
return new CID(1, format.codec, multihash) | ||
} | ||
}, | ||
resolver: { | ||
resolve: (buf, path) => { | ||
return { | ||
value: format.util.deserialize(buf), | ||
remainderPath: path | ||
} | ||
} | ||
} | ||
} | ||
|
||
// start an IPFS Daemon | ||
const daemon = new IPFSDaemon({ | ||
ipld: { | ||
formats: [ | ||
format | ||
] | ||
} | ||
}) | ||
await daemon.start() | ||
|
||
// in another process: | ||
const client = ipfsHttpClient({ | ||
url: `http://localhost:${daemon._httpApi._apiServers[0].info.port}`, | ||
ipld: { | ||
formats: [ | ||
format | ||
] | ||
} | ||
}) | ||
|
||
const data = { | ||
hello: 'world' | ||
} | ||
|
||
const cid = await client.dag.put(data, { | ||
format: codecName, | ||
hashAlg: multihash.codes[format.defaultHashAlg] | ||
}) | ||
|
||
console.info(`Put ${JSON.stringify(data)} = CID(${cid})`) | ||
|
||
const { | ||
value | ||
} = await client.dag.get(cid) | ||
|
||
console.info(`Get CID(${cid}) = ${JSON.stringify(value)}`) | ||
|
||
await daemon.stop() | ||
} | ||
|
||
main() | ||
.catch(err => { | ||
console.error(err) | ||
process.exit(1) | ||
}) | ||
.then(() => { | ||
// https://github.com/libp2p/js-libp2p/issues/779 | ||
process.exit(0) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// ordinarily we'd open a PR against the multicodec module to get our | ||
// codec number added but since we're just testing we shim our new | ||
// codec into the base-table.json file - this has to be done | ||
// before requiring other modules as the int table will become read-only | ||
const codecName = 'dag-test' | ||
const codecNumber = 392091 | ||
|
||
const baseTable = require('multicodec/src/base-table.json') | ||
baseTable[codecName] = codecNumber | ||
|
||
// now require modules as usual | ||
const IPFS = require('ipfs-core') | ||
const multihashing = require('multihashing-async') | ||
const multicodec = require('multicodec') | ||
const CID = require('cids') | ||
|
||
async function main () { | ||
// see https://github.com/ipld/interface-ipld-format for the interface definition | ||
const format = { | ||
codec: codecNumber, | ||
defaultHashAlg: multicodec.SHA2_256, | ||
util: { | ||
serialize (data) { | ||
return Buffer.from(JSON.stringify(data)) | ||
}, | ||
deserialize (buf) { | ||
return JSON.parse(buf.toString('utf8')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use uint8arrays above? |
||
}, | ||
async cid (buf) { | ||
const multihash = await multihashing(buf, format.defaultHashAlg) | ||
|
||
return new CID(1, format.codec, multihash) | ||
} | ||
} | ||
} | ||
|
||
const node = await IPFS.create({ | ||
ipld: { | ||
formats: [ | ||
format | ||
] | ||
} | ||
}) | ||
|
||
const data = { | ||
hello: 'world' | ||
} | ||
|
||
const cid = await node.dag.put(data, { | ||
format: codecName, | ||
hashAlg: format.defaultHashAlg | ||
}) | ||
|
||
console.info(`Put ${JSON.stringify(data)} = CID(${cid})`) | ||
|
||
const { | ||
value | ||
} = await node.dag.get(cid) | ||
|
||
console.info(`Get CID(${cid}) = ${JSON.stringify(value)}`) | ||
|
||
await node.stop() | ||
} | ||
|
||
main() | ||
.catch(err => { | ||
console.error(err) | ||
process.exit(1) | ||
}) | ||
.then(() => { | ||
// https://github.com/libp2p/js-libp2p/issues/779 | ||
process.exit(0) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "example-custom-ipld-formats", | ||
"version": "1.0.0", | ||
"private": true, | ||
"scripts": { | ||
"test": "test-ipfs-example" | ||
}, | ||
"license": "MIT", | ||
"devDependencies": { | ||
"execa": "^4.0.3", | ||
"test-ipfs-example": "^2.0.3" | ||
}, | ||
"dependencies": { | ||
"cids": "^1.0.0", | ||
"ipfs-cli": "0.0.1", | ||
"ipfs-core": "0.0.1", | ||
"ipfs-http-client": "^47.0.0", | ||
"multicodec": "^2.0.1", | ||
"multihashing-async": "^2.0.1", | ||
"uint8arrays": "^1.1.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict' | ||
|
||
const path = require('path') | ||
const { | ||
waitForOutput | ||
} = require('test-ipfs-example/utils') | ||
|
||
const testInProcessNode = async () => { | ||
await waitForOutput( | ||
'Put {"hello":"world"} = CID(bagn7ofysecj2eolrvekol2wl6cuneukuzwrqtq6by4x3xgiu2r6gb46lnakyq)\n' + | ||
'Get CID(bagn7ofysecj2eolrvekol2wl6cuneukuzwrqtq6by4x3xgiu2r6gb46lnakyq) = {"hello":"world"}', path.resolve(__dirname, 'in-process-node.js')) | ||
} | ||
|
||
const testDaemonNode = async () => { | ||
await waitForOutput( | ||
'Put {"hello":"world"} = CID(bagn7ofysecj2eolrvekol2wl6cuneukuzwrqtq6by4x3xgiu2r6gb46lnakyq)\n' + | ||
'Get CID(bagn7ofysecj2eolrvekol2wl6cuneukuzwrqtq6by4x3xgiu2r6gb46lnakyq) = {"hello":"world"}', path.resolve(__dirname, 'daemon-node.js')) | ||
} | ||
|
||
async function test () { | ||
console.info('Testing in-process node') | ||
await testInProcessNode() | ||
|
||
console.info('Testing daemon node') | ||
await testDaemonNode() | ||
} | ||
|
||
module.exports = test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use
uint8ArrayFromString
here per ipld/interface-ipld-format#utilserializeipldnode. It also needs to be requiredThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not necessary because the daemon only runs under node and node Buffers are Uint8Arrays so the interface contract is fulfilled.