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

Commit 85ca250

Browse files
committed
refactor: promise first addFromURL
License: MIT Signed-off-by: Marcin Rataj <[email protected]>
1 parent 8a9eda7 commit 85ca250

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
"peer-id": "~0.12.3",
153153
"peer-info": "~0.15.0",
154154
"progress": "^2.0.1",
155+
"promise-nodeify": "3.0.1",
155156
"promisify-es6": "^1.0.3",
156157
"protons": "^1.0.1",
157158
"pull-abortable": "^4.1.1",

src/core/components/files-regular/add-from-url.js

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,21 @@
22

33
const { URL } = require('iso-url')
44
const { default: ky } = require('ky-universal')
5+
const nodeify = require('promise-nodeify')
56

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-
try {
15-
const res = await ky.get(url)
16-
const path = decodeURIComponent(new URL(res.url).pathname.split('/').pop())
17-
const content = Buffer.from(await res.arrayBuffer())
18-
files = await self.add({ content, path }, options)
19-
} catch (err) {
20-
if (callback) {
21-
return callback(err)
22-
}
23-
throw err
24-
}
7+
module.exports = (ipfs) => {
8+
const addFromURL = async (url, opts = {}) => {
9+
const res = await ky.get(url)
10+
const path = decodeURIComponent(new URL(res.url).pathname.split('/').pop())
11+
const content = Buffer.from(await res.arrayBuffer())
12+
return ipfs.add({ content, path }, opts)
13+
}
2514

26-
if (callback) {
27-
callback(null, files)
15+
return (name, opts = {}, cb) => {
16+
if (typeof opts === 'function') {
17+
cb = opts
18+
opts = {}
2819
}
29-
30-
return files
20+
return nodeify(addFromURL(name, opts), cb)
3121
}
3222
}

0 commit comments

Comments
 (0)