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

Commit b6a7ab6

Browse files
author
Alan Shaw
authored
feat: add from url/stream (#1773)
License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent c5e5c07 commit b6a7ab6

File tree

7 files changed

+61
-5
lines changed

7 files changed

+61
-5
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
"browser": {
1111
"./src/core/components/init-assets.js": false,
1212
"./src/core/runtime/config-nodejs.js": "./src/core/runtime/config-browser.js",
13+
"./src/core/runtime/dns-nodejs.js": "./src/core/runtime/dns-browser.js",
14+
"./src/core/runtime/fetch-nodejs.js": "./src/core/runtime/fetch-browser.js",
1315
"./src/core/runtime/libp2p-nodejs.js": "./src/core/runtime/libp2p-browser.js",
1416
"./src/core/runtime/preload-nodejs.js": "./src/core/runtime/preload-browser.js",
1517
"./src/core/runtime/repo-nodejs.js": "./src/core/runtime/repo-browser.js",
16-
"./src/core/runtime/dns-nodejs.js": "./src/core/runtime/dns-browser.js",
1718
"./test/utils/create-repo-nodejs.js": "./test/utils/create-repo-browser.js",
1819
"stream": "readable-stream",
1920
"joi": "joi-browser"
@@ -149,6 +150,7 @@
149150
"multibase": "~0.6.0",
150151
"multihashes": "~0.4.14",
151152
"multihashing-async": "~0.5.1",
153+
"node-fetch": "^2.3.0",
152154
"once": "^1.4.0",
153155
"peer-book": "~0.8.0",
154156
"peer-id": "~0.12.0",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict'
2+
3+
module.exports = self => require('./add')(self)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict'
2+
3+
const { URL } = require('url')
4+
const fetch = require('../../runtime/fetch-nodejs')
5+
6+
module.exports = (self) => {
7+
return async (url, options, callback) => {
8+
if (typeof options === 'function') {
9+
callback = options
10+
options = {}
11+
}
12+
13+
let files
14+
15+
try {
16+
const parsedUrl = new URL(url)
17+
const res = await fetch(url)
18+
19+
if (!res.ok) {
20+
throw new Error('unexpected status code: ' + res.status)
21+
}
22+
23+
// TODO: use res.body when supported
24+
const content = Buffer.from(await res.arrayBuffer())
25+
const path = decodeURIComponent(parsedUrl.pathname.split('/').pop())
26+
27+
files = await self.add({ content, path }, options)
28+
} catch (err) {
29+
if (callback) {
30+
return callback(err)
31+
}
32+
throw err
33+
}
34+
35+
if (callback) {
36+
callback(null, files)
37+
}
38+
39+
return files
40+
}
41+
}

src/core/components/files-regular/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
module.exports = self => ({
44
add: require('./add')(self),
5+
addFromStream: require('./add-from-stream')(self),
6+
addFromURL: require('./add-from-url')(self),
57
addPullStream: require('./add-pull-stream')(self),
68
addReadableStream: require('./add-readable-stream')(self),
79
cat: require('./cat')(self),

src/core/runtime/fetch-browser.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* eslint-env browser */
2+
'use strict'
3+
module.exports = fetch

src/core/runtime/fetch-nodejs.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
'use strict'
2+
module.exports = require('node-fetch')

test/core/interface.spec.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,18 @@ describe('interface-ipfs-core tests', () => {
7979
})
8080

8181
tests.filesRegular(defaultCommonFactory, {
82-
skip: [{
83-
name: 'addFromStream',
82+
skip: isNode ? [{
83+
name: 'addFromFs',
8484
reason: 'TODO: not implemented yet'
85+
}] : [{
86+
name: 'addFromStream',
87+
reason: 'Not designed to run in the browser'
8588
}, {
8689
name: 'addFromFs',
87-
reason: 'TODO: not implemented yet'
90+
reason: 'Not designed to run in the browser'
8891
}, {
8992
name: 'addFromUrl',
90-
reason: 'TODO: not implemented yet'
93+
reason: 'Not designed to run in the browser'
9194
}]
9295
})
9396

0 commit comments

Comments
 (0)