Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit a02bf45

Browse files
author
Alan Shaw
committed
refactor: remove addFromURL addFromFs and export globSource and urlSource
1 parent bafc93f commit a02bf45

File tree

7 files changed

+92
-40
lines changed

7 files changed

+92
-40
lines changed

README.md

+72
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
- [Additional Options](#additional-options)
5454
- [Instance Utils](#instance-utils)
5555
- [Static Types and Utils](#static-types-and-utils)
56+
- [Glob source](#glob-source)
57+
- [URL source](#url-source)
5658
- [Development](#development)
5759
- [Testing](#testing)
5860
- [Contribute](#contribute)
@@ -362,6 +364,8 @@ Aside from the default export, `ipfs-http-client` exports various types and util
362364
- [`multicodec`](https://www.npmjs.com/package/multicodec)
363365
- [`multihash`](https://www.npmjs.com/package/multihashes)
364366
- [`CID`](https://www.npmjs.com/package/cids)
367+
- [`globSource`](https://github.com/ipfs/js-ipfs-utils/blob/master/src/files/glob-source.js) (not available in the browser)
368+
- [`urlSource`](https://github.com/ipfs/js-ipfs-utils/blob/master/src/files/url-source.js)
365369

366370
These can be accessed like this, for example:
367371

@@ -371,6 +375,74 @@ const { CID } = require('ipfs-http-client')
371375
import { CID } from 'ipfs-http-client'
372376
```
373377

378+
##### Glob source
379+
380+
A utility to allow files on the file system to be easily added to IPFS.
381+
382+
###### `globSource(path, [options])`
383+
384+
- `path`: A path to a single file or directory to glob from
385+
- `options`: Optional options
386+
- `options.recursive`: If `path` is a directory, use option `{ recursive: true }` to add the directory and all its sub-directories.
387+
- `options.ignore`: To exclude file globs from the directory, use option `{ ignore: ['ignore/this/folder/**', 'and/this/file'] }`.
388+
- `options.hidden`: Hidden/dot files (files or folders starting with a `.`, for example, `.git/`) are not included by default. To add them, use the option `{ hidden: true }`.
389+
390+
Returns an async iterable that yields `{ path, content }` objects suitable for passing to `ipfs.add`.
391+
392+
###### Example
393+
394+
```js
395+
const IpfsHttpClient = require('ipfs-http-client')
396+
const { globSource } = IpfsHttpClient
397+
const ipfs = IpfsHttpClient()
398+
399+
for await (const file of ipfs.add(globSource('./docs', { recursive: true }))) {
400+
console.log(file)
401+
}
402+
/*
403+
{
404+
path: 'docs/assets/anchor.js',
405+
hash: 'QmVHxRocoWgUChLEvfEyDuuD6qJ4PhdDL2dTLcpUy3dSC2',
406+
size: 15347
407+
}
408+
{
409+
path: 'docs/assets/bass-addons.css',
410+
hash: 'QmPiLWKd6yseMWDTgHegb8T7wVS7zWGYgyvfj7dGNt2viQ',
411+
size: 232
412+
}
413+
...
414+
*/
415+
```
416+
417+
##### URL source
418+
419+
A utility to allow content from the internet to be easily added to IPFS.
420+
421+
###### `urlSource(url)`
422+
423+
- `url`: A string URL or [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL) instance to send HTTP GET request to
424+
425+
Returns an async iterable that yields `{ path, content }` objects suitable for passing to `ipfs.add`.
426+
427+
###### Example
428+
429+
```js
430+
const IpfsHttpClient = require('ipfs-http-client')
431+
const { urlSource } = IpfsHttpClient
432+
const ipfs = IpfsHttpClient()
433+
434+
for await (const file of ipfs.add(urlSource('https://ipfs.io/images/ipfs-logo.svg'))) {
435+
console.log(file)
436+
}
437+
/*
438+
{
439+
path: 'ipfs-logo.svg',
440+
hash: 'QmTqZhR6f7jzdhLgPArDPnsbZpvvgxzCZycXK7ywkLxSyU',
441+
size: 3243
442+
}
443+
*/
444+
```
445+
374446
## Development
375447

376448
### Testing

package.json

+2-5
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,10 @@
1515
],
1616
"main": "src/index.js",
1717
"browser": {
18-
"glob": false,
19-
"fs": false,
20-
"stream": "readable-stream",
2118
"ky-universal": "ky/umd",
2219
"./src/add/form-data.js": "./src/add/form-data.browser.js",
23-
"./src/add-from-fs/index.js": "./src/add-from-fs/index.browser.js",
24-
"./src/lib/buffer-to-form-data.js": "./src/lib/buffer-to-form-data.browser.js"
20+
"./src/lib/buffer-to-form-data.js": "./src/lib/buffer-to-form-data.browser.js",
21+
"ipfs-utils/src/files/glob-source": false
2522
},
2623
"repository": "github:ipfs/js-ipfs-http-client",
2724
"scripts": {

src/add-from-fs/index.browser.js

-3
This file was deleted.

src/add-from-fs/index.js

-8
This file was deleted.

src/add-from-url.js

-21
This file was deleted.

src/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ const multiaddr = require('multiaddr')
66
const multibase = require('multibase')
77
const multicodec = require('multicodec')
88
const multihash = require('multihashes')
9+
const globSource = require('ipfs-utils/src/files/glob-source')
10+
const urlSource = require('./lib/url-source') // TODO: move to ipfs-utils
911

1012
function ipfsClient (config) {
1113
return {
1214
add: require('./add')(config),
13-
addFromFs: require('./add-from-fs')(config),
14-
addFromURL: require('./add-from-url')(config),
1515
bitswap: require('./bitswap')(config),
1616
block: require('./block')(config),
1717
bootstrap: require('./bootstrap')(config),
@@ -46,6 +46,6 @@ function ipfsClient (config) {
4646
}
4747
}
4848

49-
Object.assign(ipfsClient, { Buffer, CID, multiaddr, multibase, multicodec, multihash })
49+
Object.assign(ipfsClient, { Buffer, CID, multiaddr, multibase, multicodec, multihash, globSource, urlSource })
5050

5151
module.exports = ipfsClient

src/lib/url-source.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict'
2+
3+
const { default: ky } = require('ky-universal')
4+
const toIterable = require('./stream-to-iterable')
5+
6+
module.exports = async function * urlSource (url, options) {
7+
options = options || {}
8+
9+
const { body } = await ky.get(url)
10+
11+
yield {
12+
path: decodeURIComponent(new URL(url).pathname.split('/').pop() || ''),
13+
content: toIterable(body)
14+
}
15+
}

0 commit comments

Comments
 (0)