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

Commit 3bee947

Browse files
author
Alan Shaw
committed
feat: add addFromURL method
License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
1 parent 49d23f0 commit 3bee947

File tree

6 files changed

+59
-3
lines changed

6 files changed

+59
-3
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: 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
module.exports = self => ({
44
add: require('./add')(self),
5+
addFromURL: require('./add-from-url')(self),
56
addPullStream: require('./add-pull-stream')(self),
67
addReadableStream: require('./add-readable-stream')(self),
78
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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,22 @@ describe('interface-ipfs-core tests', () => {
7979
})
8080

8181
tests.filesRegular(defaultCommonFactory, {
82-
skip: [{
82+
only: ['addFromUrl'],
83+
skip: isNode ? [{
8384
name: 'addFromStream',
8485
reason: 'TODO: not implemented yet'
8586
}, {
8687
name: 'addFromFs',
8788
reason: 'TODO: not implemented yet'
89+
}] : [{
90+
name: 'addFromStream',
91+
reason: 'Not designed to run in the browser'
92+
}, {
93+
name: 'addFromFs',
94+
reason: 'Not designed to run in the browser'
8895
}, {
8996
name: 'addFromUrl',
90-
reason: 'TODO: not implemented yet'
97+
reason: 'Not designed to run in the browser'
9198
}]
9299
})
93100

0 commit comments

Comments
 (0)