From d710daa4e8b2f5527db10e2231dd9574c0a2c6ce Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Thu, 6 Oct 2022 10:29:36 -0700 Subject: [PATCH] feat: add kubo-rpc-client (#499) fixes https://github.com/ipfs/js-kubo-rpc-client/issues/4 --- .aegir.js | 12 ++++-- README.md | 7 ++- package-lock.json | 73 ++++++++++++++++++++++++++++---- package.json | 5 ++- scripts/custom-runtime.sh | 1 + scripts/download-relay-daemon.js | 1 + src/types.d.ts | 1 + test/exchange-files.js | 2 +- test/utils/circuit.js | 3 +- test/utils/daemon-factory.js | 17 ++++++-- test/utils/relayd.js | 4 +- tsconfig.json | 2 +- 12 files changed, 105 insertions(+), 23 deletions(-) create mode 100644 src/types.d.ts diff --git a/.aegir.js b/.aegir.js index 31c61595..059bb23a 100644 --- a/.aegir.js +++ b/.aegir.js @@ -7,6 +7,7 @@ import { resolve } from 'import-meta-resolve' const __dirname = path.dirname(fileURLToPath(import.meta.url)) const ipfsModule = await resolve(process.env.IPFS_JS_HTTP_MODULE || 'ipfs', import.meta.url) const ipfsHttpModule = await resolve(process.env.IPFS_JS_HTTP_MODULE || 'ipfs-http-client', import.meta.url) +const kuboRpcModule = await resolve(process.env.KUBO_RPC_MODULE || 'kubo-rpc-client', import.meta.url) async function findGoIpfsBin () { if (process.env.IPFS_GO_EXEC != null) { @@ -32,6 +33,9 @@ const esbuild = { build.onResolve({ filter: /^ipfs-http-client$/ }, () => { return { path: ipfsHttpModule.replace('file://', '') } }) + build.onResolve({ filter: /^kubo-rpc-client$/ }, () => { + return { path: kuboRpcModule.replace('file://', '') } + }) } } ] @@ -47,6 +51,7 @@ export default { }, async before (options) { const ipfsHttpModule = await import(process.env.IPFS_JS_HTTP_MODULE || 'ipfs-http-client') + const kuboRpcModule = await import(process.env.KUBO_RPC_MODULE || 'kubo-rpc-client') const ipfsModule = await import(process.env.IPFS_JS_MODULE || 'ipfs') if (options.runner !== 'node') { @@ -56,10 +61,10 @@ export default { }, { type: 'go', test: true, - ipfsHttpModule }, { go: { - ipfsBin: await findGoIpfsBin() + ipfsBin: await findGoIpfsBin(), + kuboRpcModule: kuboRpcModule }, js: { ipfsOptions: { @@ -72,7 +77,8 @@ export default { } }, ipfsModule, - ipfsBin: process.env.IPFS_JS_EXEC || ipfsModule.path() + ipfsBin: process.env.IPFS_JS_EXEC || ipfsModule.path(), + ipfsHttpModule, } }).start() diff --git a/README.md b/README.md index 2673e9ee..5927a413 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ $ node bin/ipfs-interop.js -- -t node --grep {substring-test-name} ### As a project -This module declares peer dependencies of `ipfs`, `ipfs-http-client` and `go-ipfs` so if you have `ipfs-interop` as a dependecy of your project, simply add the extra IPFS dependencies: +This module declares peer dependencies of `ipfs`, `ipfs-http-client`, `kubo-rpc-client` and `go-ipfs` so if you have `ipfs-interop` as a dependecy of your project, simply add the extra IPFS dependencies: ```json { @@ -61,7 +61,8 @@ This module declares peer dependencies of `ipfs`, `ipfs-http-client` and `go-ipf "go-ipfs": "...", "ipfs": "...", "ipfs-http-client": "...", - "ipfs-interop": "..." + "ipfs-interop": "...", + "kubo-rpc-client": "..." } } ``` @@ -80,6 +81,7 @@ Specify the following environmental variables to control which versions of IPFS - `IPFS_JS_EXEC` A path to `/src/cli.js` inside a js-IPFS install - `IPFS_JS_MODULE` A path to a js-IPFS install - `IPFS_JS_HTTP_MODULE` A path to a ipfs-http-client install (optional) +- `KUBO_RPC_MODULE` A path to a kubo-rpc-client install (optional) Then run: @@ -104,6 +106,7 @@ in `./scripts/custom-runtime.sh`, one can: export IPFS_JS_EXEC=/tmp/js-ipfs/packages/ipfs/src/cli.js export IPFS_JS_MODULE=/tmp/js-ipfs/packages/ipfs/src/index.js export IPFS_JS_HTTP_MODULE=/tmp/js-ipfs/packages/ipfs-http-client/src/index.js + export KUBO_RPC_MODULE=/tmp/kubo-rpc-client/src/index.js ./scripts/custom-runtime.sh node bin/ipfs-interop.js -- -t node --grep "pubsub" diff --git a/package-lock.json b/package-lock.json index d0e23e01..4e647645 100644 --- a/package-lock.json +++ b/package-lock.json @@ -29,7 +29,7 @@ "import-meta-resolve": "^2.0.3", "ipfs-unixfs": "^7.0.0", "ipfs-utils": "^9.0.1", - "ipfsd-ctl": "^12.0.0", + "ipfsd-ctl": "^12.2.0", "ipns": "^3.0.0", "is-ci": "^3.0.0", "iso-random-stream": "^2.0.0", @@ -57,7 +57,8 @@ "devDependencies": { "go-ipfs": "^0.16.0", "ipfs": "^0.64.2", - "ipfs-http-client": "^58.0.1" + "ipfs-http-client": "^58.0.1", + "kubo-rpc-client": "^1.0.1" }, "engines": { "node": ">=16.0.0", @@ -13212,9 +13213,9 @@ } }, "node_modules/ipfsd-ctl": { - "version": "12.0.3", - "resolved": "https://registry.npmjs.org/ipfsd-ctl/-/ipfsd-ctl-12.0.3.tgz", - "integrity": "sha512-FsPVVgUodt0YKlYvT8ZQxfqB0lnSLDnbAzgQhQYlZvk4Z2xPcCPusyRlA3ODUd1J/lJcN6LfPeqAlgMN1MZQRw==", + "version": "12.2.0", + "resolved": "https://registry.npmjs.org/ipfsd-ctl/-/ipfsd-ctl-12.2.0.tgz", + "integrity": "sha512-UIbSj0wxANo24FASUu6m8tXsc5TrMJNbEFUeWCCQpTCNttrjVe2Dp8ZNCyaCdh4XCzQKEhKd5SOCRzgCi/zPmA==", "dependencies": { "@hapi/boom": "^10.0.0", "@hapi/hapi": "^20.0.0", @@ -14805,6 +14806,36 @@ "node": ">=6" } }, + "node_modules/kubo-rpc-client": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/kubo-rpc-client/-/kubo-rpc-client-1.0.1.tgz", + "integrity": "sha512-px12Y47OlOhbL/j7BO0OkGF3F/8gZ4CdaNLnBnORH9/LbASGDg9sOrOSaMf9BxhB68e6q8rOfKes+rGNpnVAzQ==", + "dev": true, + "dependencies": { + "@ipld/dag-cbor": "^7.0.2", + "@ipld/dag-json": "^8.0.10", + "@ipld/dag-pb": "^2.1.17", + "@libp2p/logger": "^2.0.1", + "@libp2p/peer-id": "^1.1.15", + "@multiformats/multiaddr": "^11.0.0", + "any-signal": "^3.0.1", + "dag-jose": "^2.0.1", + "err-code": "^3.0.1", + "ipfs-core-utils": "^0.16.1", + "ipfs-utils": "^9.0.7", + "it-first": "^1.0.6", + "it-last": "^1.0.6", + "merge-options": "^3.0.4", + "multiformats": "^9.7.1", + "parse-duration": "^1.0.2", + "stream-to-it": "^0.2.4", + "uint8arrays": "^3.1.0" + }, + "engines": { + "node": ">=16.0.0", + "npm": ">=7.0.0" + } + }, "node_modules/latest-version": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/latest-version/-/latest-version-7.0.0.tgz", @@ -36852,9 +36883,9 @@ } }, "ipfsd-ctl": { - "version": "12.0.3", - "resolved": "https://registry.npmjs.org/ipfsd-ctl/-/ipfsd-ctl-12.0.3.tgz", - "integrity": "sha512-FsPVVgUodt0YKlYvT8ZQxfqB0lnSLDnbAzgQhQYlZvk4Z2xPcCPusyRlA3ODUd1J/lJcN6LfPeqAlgMN1MZQRw==", + "version": "12.2.0", + "resolved": "https://registry.npmjs.org/ipfsd-ctl/-/ipfsd-ctl-12.2.0.tgz", + "integrity": "sha512-UIbSj0wxANo24FASUu6m8tXsc5TrMJNbEFUeWCCQpTCNttrjVe2Dp8ZNCyaCdh4XCzQKEhKd5SOCRzgCi/zPmA==", "requires": { "@hapi/boom": "^10.0.0", "@hapi/hapi": "^20.0.0", @@ -38064,6 +38095,32 @@ "resolved": "https://registry.npmjs.org/kleur/-/kleur-4.1.5.tgz", "integrity": "sha512-o+NO+8WrRiQEE4/7nwRJhN1HWpVmJm511pBHUxPLtp0BUISzlBplORYSmTclCnJvQq2tKu/sgl3xVpkc7ZWuQQ==" }, + "kubo-rpc-client": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/kubo-rpc-client/-/kubo-rpc-client-1.0.1.tgz", + "integrity": "sha512-px12Y47OlOhbL/j7BO0OkGF3F/8gZ4CdaNLnBnORH9/LbASGDg9sOrOSaMf9BxhB68e6q8rOfKes+rGNpnVAzQ==", + "dev": true, + "requires": { + "@ipld/dag-cbor": "^7.0.2", + "@ipld/dag-json": "^8.0.10", + "@ipld/dag-pb": "^2.1.17", + "@libp2p/logger": "^2.0.1", + "@libp2p/peer-id": "^1.1.15", + "@multiformats/multiaddr": "^11.0.0", + "any-signal": "^3.0.1", + "dag-jose": "^2.0.1", + "err-code": "^3.0.1", + "ipfs-core-utils": "^0.16.1", + "ipfs-utils": "^9.0.7", + "it-first": "^1.0.6", + "it-last": "^1.0.6", + "merge-options": "^3.0.4", + "multiformats": "^9.7.1", + "parse-duration": "^1.0.2", + "stream-to-it": "^0.2.4", + "uint8arrays": "^3.1.0" + } + }, "latest-version": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/latest-version/-/latest-version-7.0.0.tgz", diff --git a/package.json b/package.json index 20486953..d97ff86b 100644 --- a/package.json +++ b/package.json @@ -181,7 +181,7 @@ "import-meta-resolve": "^2.0.3", "ipfs-unixfs": "^7.0.0", "ipfs-utils": "^9.0.1", - "ipfsd-ctl": "^12.0.0", + "ipfsd-ctl": "^12.2.0", "ipns": "^3.0.0", "is-ci": "^3.0.0", "iso-random-stream": "^2.0.0", @@ -206,7 +206,8 @@ "devDependencies": { "go-ipfs": "^0.16.0", "ipfs": "^0.64.2", - "ipfs-http-client": "^58.0.1" + "ipfs-http-client": "^58.0.1", + "kubo-rpc-client": "^1.0.1" }, "browser": { "go-ipfs": false, diff --git a/scripts/custom-runtime.sh b/scripts/custom-runtime.sh index dac42524..b15ab30d 100755 --- a/scripts/custom-runtime.sh +++ b/scripts/custom-runtime.sh @@ -12,6 +12,7 @@ # export IPFS_JS_EXEC=/tmp/js-ipfs/packages/ipfs/src/cli.js # export IPFS_JS_MODULE=/tmp/js-ipfs/packages/ipfs/src/index.js # export IPFS_JS_HTTP_MODULE=/tmp/js-ipfs/packages/ipfs-http-client/src/index.js +# export KUBO_RPC_MODULE=/tmp/kubo-rpc-client/src/index.js # ./scripts/custom-runtime.sh # node bin/ipfs-interop.js -- -t node --grep "pubsub" diff --git a/scripts/download-relay-daemon.js b/scripts/download-relay-daemon.js index a3956198..42d4a8fc 100644 --- a/scripts/download-relay-daemon.js +++ b/scripts/download-relay-daemon.js @@ -22,6 +22,7 @@ export const LIBP2P_RELAY_DAEMON_VERSION = 'v0.1.0' /** * avoid expensive fetch if file is already in cache + * * @param {string} url */ async function cachingFetchAndVerify (url) { diff --git a/src/types.d.ts b/src/types.d.ts new file mode 100644 index 00000000..5a29fbbc --- /dev/null +++ b/src/types.d.ts @@ -0,0 +1 @@ +export type CID = import('ipfsd-ctl/node_modules/multiformats/types/src/cid').CID diff --git a/test/exchange-files.js b/test/exchange-files.js index d6062453..f327cd56 100644 --- a/test/exchange-files.js +++ b/test/exchange-files.js @@ -159,7 +159,7 @@ describe('exchange files', function () { }) /** - * @param {import('multiformats/cid').CID} cid + * @param {import('../src/types.js').CID} cid * @param {Controller["api"]} ipfs */ async function countFiles (cid, ipfs) { diff --git a/test/utils/circuit.js b/test/utils/circuit.js index 52a8c2d9..6fa72846 100644 --- a/test/utils/circuit.js +++ b/test/utils/circuit.js @@ -11,6 +11,7 @@ import { multiaddr } from '@multiformats/multiaddr' /** * @typedef {import('ipfsd-ctl').Controller} Controller * @typedef {import('ipfsd-ctl').Factory} Factory + * @typedef {import('@libp2p/interface-transport').MultiaddrFilter} MultiaddrFilter */ export const randomWsAddr = '/ip4/127.0.0.1/tcp/0/ws' @@ -44,7 +45,7 @@ export function createProc (addrs, factory, relay) { libp2p: { transports: [ new WebSockets({ - filter: filters.all + filter: /** @type {MultiaddrFilter} */(/** @type {unknown} */(filters.all)) }) ] } diff --git a/test/utils/daemon-factory.js b/test/utils/daemon-factory.js index 4d02bfb9..2f6d8ba9 100644 --- a/test/utils/daemon-factory.js +++ b/test/utils/daemon-factory.js @@ -4,6 +4,7 @@ import { isNode, isElectronMain } from 'wherearewe' export async function daemonFactory () { let ipfsHttpModule let ipfsModule + let kuboRpcModule try { // @ts-expect-error env var could be undefined @@ -12,6 +13,13 @@ export async function daemonFactory () { ipfsHttpModule = await import('ipfs-http-client') } + try { + // @ts-expect-error env var could be undefined + kuboRpcModule = await import(process.env.KUBO_RPC_MODULE) + } catch { + kuboRpcModule = await import('kubo-rpc-client') + } + try { // @ts-expect-error env var could be undefined ipfsModule = await import(process.env.IPFS_JS_MODULE) @@ -21,17 +29,18 @@ export async function daemonFactory () { return createFactory({ type: 'go', - test: true, - ipfsHttpModule + test: true }, { proc: { ipfsModule }, js: { - ipfsBin: await findBin('IPFS_JS_EXEC', 'ipfs', ipfsModule) + ipfsBin: await findBin('IPFS_JS_EXEC', 'ipfs', ipfsModule), + ipfsHttpModule }, go: { - ipfsBin: await findBin('IPFS_GO_EXEC', 'go-ipfs') + ipfsBin: await findBin('IPFS_GO_EXEC', 'go-ipfs'), + kuboRpcModule } }) } diff --git a/test/utils/relayd.js b/test/utils/relayd.js index 3f47dfb6..806fae5d 100644 --- a/test/utils/relayd.js +++ b/test/utils/relayd.js @@ -85,8 +85,10 @@ export async function getRelayV (version) { } } + /** + * @type {Promise & {cancel?: () => void}} + */ const promise = waitForStartup() - // @ts-ignore promise.cancel = () => { console.error(`Timed out waiting for ${binaryPath} to start after ${RELAY_STARTUP_TIMEOUT}ms, killing process`) // eslint-disable-line no-console relayd.kill() diff --git a/tsconfig.json b/tsconfig.json index 0477d6b3..3e3ed80d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,4 +9,4 @@ "scripts", "test" ] -} \ No newline at end of file +}