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

Commit

Permalink
chore: fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Sep 23, 2021
1 parent 1d56843 commit 498d9b0
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 2 deletions.
21 changes: 21 additions & 0 deletions docs/MODULE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
```
5 changes: 4 additions & 1 deletion packages/ipfs-core-config/src/dns.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion packages/ipfs-core-config/src/preload.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions packages/ipfs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -28,6 +40,9 @@
"exports": {
".": {
"import": "./src/index.js"
},
"./path": {
"import": "./src/path.js"
}
},
"bin": {
Expand All @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions packages/ipfs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,3 +24,4 @@ export const multiaddr = multiaddrImport
export const PeerId = PeerIdImport
export const globSource = globSourceImport
export const urlSource = urlSourceImport
export const path = pathImport
31 changes: 31 additions & 0 deletions packages/ipfs/src/path.js
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit 498d9b0

Please sign in to comment.