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

Commit

Permalink
refactor: promise first addFromURL
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Marcin Rataj <lidel@lidel.org>
  • Loading branch information
lidel committed Jul 31, 2019
1 parent 8a9eda7 commit 85ca250
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
"peer-id": "~0.12.3",
"peer-info": "~0.15.0",
"progress": "^2.0.1",
"promise-nodeify": "3.0.1",
"promisify-es6": "^1.0.3",
"protons": "^1.0.1",
"pull-abortable": "^4.1.1",
Expand Down
36 changes: 13 additions & 23 deletions src/core/components/files-regular/add-from-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,21 @@

const { URL } = require('iso-url')
const { default: ky } = require('ky-universal')
const nodeify = require('promise-nodeify')

module.exports = (self) => {
return async (url, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}

let files
try {
const res = await ky.get(url)
const path = decodeURIComponent(new URL(res.url).pathname.split('/').pop())
const content = Buffer.from(await res.arrayBuffer())
files = await self.add({ content, path }, options)
} catch (err) {
if (callback) {
return callback(err)
}
throw err
}
module.exports = (ipfs) => {
const addFromURL = async (url, opts = {}) => {
const res = await ky.get(url)
const path = decodeURIComponent(new URL(res.url).pathname.split('/').pop())
const content = Buffer.from(await res.arrayBuffer())
return ipfs.add({ content, path }, opts)
}

if (callback) {
callback(null, files)
return (name, opts = {}, cb) => {
if (typeof opts === 'function') {
cb = opts
opts = {}
}

return files
return nodeify(addFromURL(name, opts), cb)
}
}

0 comments on commit 85ca250

Please sign in to comment.