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

Commit

Permalink
feat: add from url/stream (#1773)
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
  • Loading branch information
Alan Shaw authored Dec 13, 2018
1 parent c5e5c07 commit b6a7ab6
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 5 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
"browser": {
"./src/core/components/init-assets.js": false,
"./src/core/runtime/config-nodejs.js": "./src/core/runtime/config-browser.js",
"./src/core/runtime/dns-nodejs.js": "./src/core/runtime/dns-browser.js",
"./src/core/runtime/fetch-nodejs.js": "./src/core/runtime/fetch-browser.js",
"./src/core/runtime/libp2p-nodejs.js": "./src/core/runtime/libp2p-browser.js",
"./src/core/runtime/preload-nodejs.js": "./src/core/runtime/preload-browser.js",
"./src/core/runtime/repo-nodejs.js": "./src/core/runtime/repo-browser.js",
"./src/core/runtime/dns-nodejs.js": "./src/core/runtime/dns-browser.js",
"./test/utils/create-repo-nodejs.js": "./test/utils/create-repo-browser.js",
"stream": "readable-stream",
"joi": "joi-browser"
Expand Down Expand Up @@ -149,6 +150,7 @@
"multibase": "~0.6.0",
"multihashes": "~0.4.14",
"multihashing-async": "~0.5.1",
"node-fetch": "^2.3.0",
"once": "^1.4.0",
"peer-book": "~0.8.0",
"peer-id": "~0.12.0",
Expand Down
3 changes: 3 additions & 0 deletions src/core/components/files-regular/add-from-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict'

module.exports = self => require('./add')(self)
41 changes: 41 additions & 0 deletions src/core/components/files-regular/add-from-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict'

const { URL } = require('url')
const fetch = require('../../runtime/fetch-nodejs')

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

let files

try {
const parsedUrl = new URL(url)
const res = await fetch(url)

if (!res.ok) {
throw new Error('unexpected status code: ' + res.status)
}

// TODO: use res.body when supported
const content = Buffer.from(await res.arrayBuffer())
const path = decodeURIComponent(parsedUrl.pathname.split('/').pop())

files = await self.add({ content, path }, options)
} catch (err) {
if (callback) {
return callback(err)
}
throw err
}

if (callback) {
callback(null, files)
}

return files
}
}
2 changes: 2 additions & 0 deletions src/core/components/files-regular/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module.exports = self => ({
add: require('./add')(self),
addFromStream: require('./add-from-stream')(self),
addFromURL: require('./add-from-url')(self),
addPullStream: require('./add-pull-stream')(self),
addReadableStream: require('./add-readable-stream')(self),
cat: require('./cat')(self),
Expand Down
3 changes: 3 additions & 0 deletions src/core/runtime/fetch-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* eslint-env browser */
'use strict'
module.exports = fetch
2 changes: 2 additions & 0 deletions src/core/runtime/fetch-nodejs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'use strict'
module.exports = require('node-fetch')
11 changes: 7 additions & 4 deletions test/core/interface.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,18 @@ describe('interface-ipfs-core tests', () => {
})

tests.filesRegular(defaultCommonFactory, {
skip: [{
name: 'addFromStream',
skip: isNode ? [{
name: 'addFromFs',
reason: 'TODO: not implemented yet'
}] : [{
name: 'addFromStream',
reason: 'Not designed to run in the browser'
}, {
name: 'addFromFs',
reason: 'TODO: not implemented yet'
reason: 'Not designed to run in the browser'
}, {
name: 'addFromUrl',
reason: 'TODO: not implemented yet'
reason: 'Not designed to run in the browser'
}]
})

Expand Down

0 comments on commit b6a7ab6

Please sign in to comment.