Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 498d9b0

Browse files
committed
chore: fix examples
1 parent 1d56843 commit 498d9b0

File tree

6 files changed

+80
-2
lines changed

6 files changed

+80
-2
lines changed

docs/MODULE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ Use the IPFS module as a dependency of your project to spawn in process instance
2828
- [URL source](#url-source)
2929
- [`urlSource(url)`](#urlsourceurl)
3030
- [Example](#example-1)
31+
- [Path](#path)
32+
- [`path()`](#path-1)
33+
- [Example](#example-2)
3134

3235
## Getting started
3336

@@ -458,3 +461,21 @@ console.log(file)
458461
}
459462
*/
460463
```
464+
465+
##### Path
466+
467+
A function that returns the path to the js-ipfs CLI.
468+
469+
This is analogous to the `.path()` function exported by the [go-ipfs](https://www.npmjs.com/package/go-ipfs) module.
470+
471+
###### `path()`
472+
473+
Returns the path to the js-ipfs CLI
474+
475+
###### Example
476+
477+
```js
478+
import { path } from 'ipfs'
479+
480+
console.info(path()) // /foo/bar/node_modules/ipfs/src/cli.js
481+
```

packages/ipfs-core-config/src/dns.browser.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ const cache = new TLRU(1000)
1111
// which acts a provisional default ttl: https://stackoverflow.com/a/36917902/11518426
1212
const ttl = 60 * 1000
1313

14+
// @ts-expect-error PQueue@6 is broken
15+
const Queue = PQueue.default ? PQueue.default : PQueue
16+
1417
// browsers limit concurrent connections per host,
1518
// we don't want preload calls to exhaust the limit (~6)
16-
const httpQueue = new PQueue({ concurrency: 4 })
19+
const httpQueue = new Queue({ concurrency: 4 })
1720

1821
/**
1922
* @param {{ Path: string, Message: string }} response

packages/ipfs-core-config/src/preload.browser.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ const log = Object.assign(debug('ipfs:preload'), {
88
error: debug('ipfs:preload:error')
99
})
1010

11+
// @ts-expect-error PQueue@6 is broken
12+
const Queue = PQueue.default ? PQueue.default : PQueue
13+
1114
// browsers limit concurrent connections per host,
1215
// we don't want preload calls to exhaust the limit (~6)
13-
const httpQueue = new PQueue({ concurrency: 4 })
16+
const httpQueue = new Queue({ concurrency: 4 })
1417

1518
/**
1619
* @param {string} url

packages/ipfs/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@
1212
"type": "module",
1313
"main": "src/index.js",
1414
"types": "types/src/index.d.ts",
15+
"typesVersions": {
16+
"*": {
17+
"*": [
18+
"types/*",
19+
"types/src/*"
20+
],
21+
"types/*": [
22+
"types/*",
23+
"types/src/*"
24+
]
25+
}
26+
},
1527
"files": [
1628
"*",
1729
"!**/*.tsbuildinfo"
@@ -28,6 +40,9 @@
2840
"exports": {
2941
".": {
3042
"import": "./src/index.js"
43+
},
44+
"./path": {
45+
"import": "./src/path.js"
3146
}
3247
},
3348
"bin": {
@@ -42,6 +57,7 @@
4257
"build:update-version": "node scripts/update-version.js",
4358
"build:aegir": "aegir build",
4459
"build:copy-cli": "copyfiles ./src/cli.js ./src/package.js ./dist",
60+
"build:copy-package": "copyfiles ./dist/esm/package.json ./dist/src -u 2",
4561
"lint": "aegir ts -p check && aegir lint",
4662
"test:interface:core": "aegir test -f test/interface-core.js",
4763
"test:interface:client": "aegir test -f test/interface-client.js",

packages/ipfs/src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import {
88
globSource as globSourceImport,
99
urlSource as urlSourceImport
1010
} from 'ipfs-core'
11+
import {
12+
path as pathImport
13+
} from './path.js'
1114

1215
/**
1316
* @typedef {import('ipfs-core-types').IPFS} IPFS
@@ -21,3 +24,4 @@ export const multiaddr = multiaddrImport
2124
export const PeerId = PeerIdImport
2225
export const globSource = globSourceImport
2326
export const urlSource = urlSourceImport
27+
export const path = pathImport

packages/ipfs/src/path.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import fs from 'fs'
2+
import Path from 'path'
3+
4+
export function path () {
5+
const paths = []
6+
7+
// simulate node's node_modules lookup
8+
for (let i = 0; i < process.cwd().split(Path.sep).length; i++) {
9+
const dots = new Array(i).fill('..')
10+
11+
paths.push(
12+
Path.resolve(
13+
Path.join(process.cwd(), ...dots, 'node_modules', 'ipfs')
14+
)
15+
)
16+
}
17+
18+
const resourcePath = paths.find(path => fs.existsSync(path))
19+
20+
if (!resourcePath) {
21+
throw new Error(`Could not find ipfs module in paths: \n${paths.join('\n')}`)
22+
}
23+
24+
const pkg = JSON.parse(fs.readFileSync(resourcePath + Path.sep + 'package.json', {
25+
encoding: 'utf-8'
26+
}))
27+
28+
const bin = pkg.bin.jsipfs
29+
30+
return Path.resolve(Path.join(resourcePath, bin))
31+
}

0 commit comments

Comments
 (0)