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

Commit 0a08192

Browse files
hugomrdiasAlan Shaw
authored and
Alan Shaw
committed
feat: add support for File DOM API to files-regular (#2013)
* feat: add support for File DOM API to files-regular * chore: use correct interface tests * chore: use correct interface tests * chore: fix package declaration cause npm is dumb * chore: fix deps * chore: fix deps * chore: fix deps * fix: use correct input * chore: add correct http-client * fix: use ipfs-utils * chore: fix semver lint
1 parent 6dc9075 commit 0a08192

File tree

4 files changed

+43
-36
lines changed

4 files changed

+43
-36
lines changed

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@
7272
"execa": "^1.0.0",
7373
"form-data": "^2.3.3",
7474
"hat": "0.0.3",
75-
"interface-ipfs-core": "~0.101.1",
7675
"ipfsd-ctl": "~0.42.0",
7776
"libp2p-websocket-star": "~0.10.2",
7877
"ncp": "^2.0.0",
7978
"qs": "^6.5.2",
8079
"rimraf": "^2.6.2",
80+
"interface-ipfs-core": "~0.102.0",
8181
"sinon": "^7.3.1",
8282
"stream-to-promise": "^2.2.0"
8383
},
@@ -109,11 +109,11 @@
109109
"glob": "^7.1.3",
110110
"hapi-pino": "^6.0.0",
111111
"human-to-milliseconds": "^1.0.0",
112-
"interface-datastore": "~0.6.0",
112+
"interface-datastore": "~0.7.0",
113113
"ipfs-bitswap": "~0.23.0",
114114
"ipfs-block": "~0.8.0",
115115
"ipfs-block-service": "~0.15.1",
116-
"ipfs-http-client": "^31.0.2",
116+
"ipfs-http-client": "^31.1.0",
117117
"ipfs-http-response": "~0.2.1",
118118
"ipfs-mfs": "~0.10.2",
119119
"ipfs-multipart": "~0.1.0",
@@ -134,8 +134,10 @@
134134
"is-pull-stream": "~0.0.0",
135135
"is-stream": "^2.0.0",
136136
"iso-url": "~0.4.6",
137+
"ipfs-utils": "~0.0.3",
137138
"just-flatten-it": "^2.1.0",
138139
"just-safe-set": "^2.1.0",
140+
"kind-of": "^6.0.2",
139141
"libp2p": "~0.25.0",
140142
"libp2p-bootstrap": "~0.9.3",
141143
"libp2p-crypto": "~0.16.0",

src/core/components/files-regular/add-pull-stream.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
'use strict'
22

33
const importer = require('ipfs-unixfs-importer')
4-
const pull = require('pull-stream')
4+
const kindOf = require('kind-of')
5+
const CID = require('cids')
6+
const pullValues = require('pull-stream/sources/values')
7+
const pullMap = require('pull-stream/throughs/map')
8+
const pullAsyncMap = require('pull-stream/throughs/async-map')
9+
const pullFlatten = require('pull-stream/throughs/flatten')
10+
const pull = require('pull-stream/pull')
511
const toPull = require('stream-to-pull-stream')
612
const waterfall = require('async/waterfall')
713
const isStream = require('is-stream')
8-
const isSource = require('is-pull-stream').isSource
9-
const CID = require('cids')
14+
const { isSource } = require('is-pull-stream')
1015
const { parseChunkerString } = require('./utils')
16+
const streamFromFileReader = require('ipfs-utils/src/streams/stream-from-filereader')
17+
const { supportsFileReader } = require('ipfs-utils/src/supports')
1118

1219
const WRAPPER = 'wrapper/'
1320

@@ -52,9 +59,12 @@ function normalizeContent (content, opts) {
5259
}
5360

5461
return content.map((data) => {
62+
if (supportsFileReader && kindOf(data) === 'file') {
63+
data = { path: '', content: toPull.source(streamFromFileReader(data)) }
64+
}
5565
// Buffer input
5666
if (Buffer.isBuffer(data)) {
57-
data = { path: '', content: pull.values([data]) }
67+
data = { path: '', content: pullValues([data]) }
5868
}
5969

6070
// Readable stream input
@@ -68,7 +78,7 @@ function normalizeContent (content, opts) {
6878

6979
if (data && data.content && typeof data.content !== 'function') {
7080
if (Buffer.isBuffer(data.content)) {
71-
data.content = pull.values([data.content])
81+
data.content = pullValues([data.content])
7282
}
7383

7484
if (isStream.readable(data.content)) {
@@ -124,7 +134,7 @@ module.exports = function (self) {
124134
try {
125135
chunkerOptions = parseChunkerString(options.chunker)
126136
} catch (err) {
127-
return pull.map(() => { throw err })
137+
return pullMap(() => { throw err })
128138
}
129139
const opts = Object.assign({}, {
130140
shardSplitThreshold: self._options.EXPERIMENTAL.sharding
@@ -147,12 +157,12 @@ module.exports = function (self) {
147157

148158
opts.progress = progress
149159
return pull(
150-
pull.map(content => normalizeContent(content, opts)),
151-
pull.flatten(),
160+
pullMap(content => normalizeContent(content, opts)),
161+
pullFlatten(),
152162
importer(self._ipld, opts),
153-
pull.asyncMap((file, cb) => prepareFile(file, self, opts, cb)),
154-
pull.map(file => preloadFile(file, self, opts)),
155-
pull.asyncMap((file, cb) => pinFile(file, self, opts, cb))
163+
pullAsyncMap((file, cb) => prepareFile(file, self, opts, cb)),
164+
pullMap(file => preloadFile(file, self, opts)),
165+
pullAsyncMap((file, cb) => pinFile(file, self, opts, cb))
156166
)
157167
}
158168
}

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

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
const promisify = require('promisify-es6')
44
const pull = require('pull-stream')
55
const sort = require('pull-sort')
6-
const isStream = require('is-stream')
76
const isSource = require('is-pull-stream').isSource
7+
const validateAddInput = require('ipfs-utils/src/files/add-input-validation')
88

99
module.exports = function (self) {
1010
const add = promisify((data, options, callback) => {
@@ -15,23 +15,10 @@ module.exports = function (self) {
1515

1616
options = options || {}
1717

18-
// Buffer, pull stream or Node.js stream
19-
const isBufferOrStream = obj => Buffer.isBuffer(obj) || isStream.readable(obj) || isSource(obj)
20-
// An object like { content?, path? }, where content isBufferOrStream and path isString
21-
const isContentObject = obj => {
22-
if (typeof obj !== 'object') return false
23-
// path is optional if content is present
24-
if (obj.content) return isBufferOrStream(obj.content)
25-
// path must be a non-empty string if no content
26-
return Boolean(obj.path) && typeof obj.path === 'string'
27-
}
28-
// An input atom: a buffer, stream or content object
29-
const isInput = obj => isBufferOrStream(obj) || isContentObject(obj)
30-
// All is ok if data isInput or data is an array of isInput
31-
const ok = isInput(data) || (Array.isArray(data) && data.every(isInput))
32-
33-
if (!ok) {
34-
return callback(new Error('invalid input: expected buffer, readable stream, pull stream, object or array of objects'))
18+
try {
19+
validateAddInput(data)
20+
} catch (err) {
21+
return callback(err)
3522
}
3623

3724
pull(

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict'
22

33
const CID = require('cids')
4+
const { Buffer } = require('buffer')
45

5-
exports.normalizePath = (path) => {
6+
const normalizePath = (path) => {
67
if (Buffer.isBuffer(path)) {
78
return new CID(path).toString()
89
}
@@ -30,7 +31,7 @@ exports.normalizePath = (path) => {
3031
*
3132
* @return {Object} Chunker options for DAGBuilder
3233
*/
33-
exports.parseChunkerString = (chunker) => {
34+
const parseChunkerString = (chunker) => {
3435
if (!chunker) {
3536
return {
3637
chunker: 'fixed'
@@ -67,7 +68,7 @@ exports.parseChunkerString = (chunker) => {
6768
*
6869
* @return {Object} rabin chunker options
6970
*/
70-
function parseRabinString (chunker) {
71+
const parseRabinString = (chunker) => {
7172
const options = {}
7273
const parts = chunker.split('-')
7374
switch (parts.length) {
@@ -89,11 +90,18 @@ function parseRabinString (chunker) {
8990
return options
9091
}
9192

92-
function parseChunkSize (str, name) {
93+
const parseChunkSize = (str, name) => {
9394
let size = parseInt(str)
9495
if (isNaN(size)) {
9596
throw new Error(`Chunker parameter ${name} must be an integer`)
9697
}
9798

9899
return size
99100
}
101+
102+
module.exports = {
103+
normalizePath,
104+
parseChunkSize,
105+
parseRabinString,
106+
parseChunkerString
107+
}

0 commit comments

Comments
 (0)