From 498d9b0f3c5325af2d2f93ce1538c65f7c1eb2dd Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 23 Sep 2021 11:41:08 +0100 Subject: [PATCH] chore: fix examples --- docs/MODULE.md | 21 +++++++++++++ packages/ipfs-core-config/src/dns.browser.js | 5 ++- .../ipfs-core-config/src/preload.browser.js | 5 ++- packages/ipfs/package.json | 16 ++++++++++ packages/ipfs/src/index.js | 4 +++ packages/ipfs/src/path.js | 31 +++++++++++++++++++ 6 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 packages/ipfs/src/path.js diff --git a/docs/MODULE.md b/docs/MODULE.md index 407345ad67..132ec478a0 100644 --- a/docs/MODULE.md +++ b/docs/MODULE.md @@ -28,6 +28,9 @@ Use the IPFS module as a dependency of your project to spawn in process instance - [URL source](#url-source) - [`urlSource(url)`](#urlsourceurl) - [Example](#example-1) + - [Path](#path) + - [`path()`](#path-1) + - [Example](#example-2) ## Getting started @@ -458,3 +461,21 @@ console.log(file) } */ ``` + +##### Path + +A function that returns the path to the js-ipfs CLI. + +This is analogous to the `.path()` function exported by the [go-ipfs](https://www.npmjs.com/package/go-ipfs) module. + +###### `path()` + +Returns the path to the js-ipfs CLI + +###### Example + +```js +import { path } from 'ipfs' + +console.info(path()) // /foo/bar/node_modules/ipfs/src/cli.js +``` diff --git a/packages/ipfs-core-config/src/dns.browser.js b/packages/ipfs-core-config/src/dns.browser.js index 203ca9db10..aedb80b31e 100644 --- a/packages/ipfs-core-config/src/dns.browser.js +++ b/packages/ipfs-core-config/src/dns.browser.js @@ -11,9 +11,12 @@ const cache = new TLRU(1000) // which acts a provisional default ttl: https://stackoverflow.com/a/36917902/11518426 const ttl = 60 * 1000 +// @ts-expect-error PQueue@6 is broken +const Queue = PQueue.default ? PQueue.default : PQueue + // browsers limit concurrent connections per host, // we don't want preload calls to exhaust the limit (~6) -const httpQueue = new PQueue({ concurrency: 4 }) +const httpQueue = new Queue({ concurrency: 4 }) /** * @param {{ Path: string, Message: string }} response diff --git a/packages/ipfs-core-config/src/preload.browser.js b/packages/ipfs-core-config/src/preload.browser.js index 3a9140a54c..0079268e83 100644 --- a/packages/ipfs-core-config/src/preload.browser.js +++ b/packages/ipfs-core-config/src/preload.browser.js @@ -8,9 +8,12 @@ const log = Object.assign(debug('ipfs:preload'), { error: debug('ipfs:preload:error') }) +// @ts-expect-error PQueue@6 is broken +const Queue = PQueue.default ? PQueue.default : PQueue + // browsers limit concurrent connections per host, // we don't want preload calls to exhaust the limit (~6) -const httpQueue = new PQueue({ concurrency: 4 }) +const httpQueue = new Queue({ concurrency: 4 }) /** * @param {string} url diff --git a/packages/ipfs/package.json b/packages/ipfs/package.json index 26f78b4d5b..817efc5c99 100644 --- a/packages/ipfs/package.json +++ b/packages/ipfs/package.json @@ -12,6 +12,18 @@ "type": "module", "main": "src/index.js", "types": "types/src/index.d.ts", + "typesVersions": { + "*": { + "*": [ + "types/*", + "types/src/*" + ], + "types/*": [ + "types/*", + "types/src/*" + ] + } + }, "files": [ "*", "!**/*.tsbuildinfo" @@ -28,6 +40,9 @@ "exports": { ".": { "import": "./src/index.js" + }, + "./path": { + "import": "./src/path.js" } }, "bin": { @@ -42,6 +57,7 @@ "build:update-version": "node scripts/update-version.js", "build:aegir": "aegir build", "build:copy-cli": "copyfiles ./src/cli.js ./src/package.js ./dist", + "build:copy-package": "copyfiles ./dist/esm/package.json ./dist/src -u 2", "lint": "aegir ts -p check && aegir lint", "test:interface:core": "aegir test -f test/interface-core.js", "test:interface:client": "aegir test -f test/interface-client.js", diff --git a/packages/ipfs/src/index.js b/packages/ipfs/src/index.js index e6193e0900..8db9fd1321 100644 --- a/packages/ipfs/src/index.js +++ b/packages/ipfs/src/index.js @@ -8,6 +8,9 @@ import { globSource as globSourceImport, urlSource as urlSourceImport } from 'ipfs-core' +import { + path as pathImport +} from './path.js' /** * @typedef {import('ipfs-core-types').IPFS} IPFS @@ -21,3 +24,4 @@ export const multiaddr = multiaddrImport export const PeerId = PeerIdImport export const globSource = globSourceImport export const urlSource = urlSourceImport +export const path = pathImport diff --git a/packages/ipfs/src/path.js b/packages/ipfs/src/path.js new file mode 100644 index 0000000000..cbb4d6de7a --- /dev/null +++ b/packages/ipfs/src/path.js @@ -0,0 +1,31 @@ +import fs from 'fs' +import Path from 'path' + +export function path () { + const paths = [] + + // simulate node's node_modules lookup + for (let i = 0; i < process.cwd().split(Path.sep).length; i++) { + const dots = new Array(i).fill('..') + + paths.push( + Path.resolve( + Path.join(process.cwd(), ...dots, 'node_modules', 'ipfs') + ) + ) + } + + const resourcePath = paths.find(path => fs.existsSync(path)) + + if (!resourcePath) { + throw new Error(`Could not find ipfs module in paths: \n${paths.join('\n')}`) + } + + const pkg = JSON.parse(fs.readFileSync(resourcePath + Path.sep + 'package.json', { + encoding: 'utf-8' + })) + + const bin = pkg.bin.jsipfs + + return Path.resolve(Path.join(resourcePath, bin)) +}