This repository was archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy pathsend-files-stream.js
162 lines (134 loc) · 4.13 KB
/
send-files-stream.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
'use strict'
const { Duplex } = require('readable-stream')
const eachSeries = require('async/eachSeries')
const isStream = require('is-stream')
const once = require('once')
const prepareFile = require('./prepare-file')
const Multipart = require('./multipart')
function headers (file, i) {
const filename = file.path
? encodeURIComponent(file.path)
: ''
const header = { 'Content-Disposition': `form-data; name="data${i}"; filename="${filename}"` }
if (!file.content) {
header['Content-Type'] = 'application/x-directory'
} else if (file.symlink) {
header['Content-Type'] = 'application/symlink'
} else {
header['Content-Type'] = 'application/octet-stream'
}
return header
}
module.exports = (send, path) => {
return (options) => {
let request
let ended = false
let writing = false
options = options ? Object.assign({}, options, options.qs) : {}
const multipart = new Multipart()
const retStream = new Duplex({ objectMode: true })
retStream._read = (n) => {}
retStream._write = (file, enc, _next) => {
const next = once(_next)
try {
const files = prepareFile(file, options)
.map((file, i) => Object.assign({ headers: headers(file, i) }, file))
writing = true
eachSeries(
files,
(file, cb) => multipart.write(file, enc, cb),
(err) => {
writing = false
if (err) {
return next(err)
}
if (ended) {
multipart.end()
}
next()
})
} catch (err) {
next(err)
}
}
retStream.once('finish', () => {
if (!ended) {
ended = true
if (!writing) {
multipart.end()
}
}
})
const qs = options.qs || {}
qs['cid-version'] = propOrProp(options, 'cid-version', 'cidVersion')
qs['raw-leaves'] = propOrProp(options, 'raw-leaves', 'rawLeaves')
qs['only-hash'] = propOrProp(options, 'only-hash', 'onlyHash')
qs['wrap-with-directory'] = propOrProp(options, 'wrap-with-directory', 'wrapWithDirectory')
qs['pin'] = propOrProp(options, 'pin')
qs['preload'] = propOrProp(options, 'preload')
qs.hash = propOrProp(options, 'hash', 'hashAlg')
if (options.strategy === 'trickle' || options.trickle) {
qs['trickle'] = 'true'
}
const args = {
path: path,
qs: qs,
args: options.args,
multipart: true,
multipartBoundary: multipart._boundary,
stream: true,
recursive: true,
progress: options.progress
}
multipart.on('error', (err) => {
retStream.emit('error', err)
})
request = send(args, (err, response) => {
if (err) {
return retStream.emit('error', err)
}
if (!response) {
// no response, which means everything is ok, so we end the retStream
return retStream.push(null) // early
}
if (!isStream(response)) {
retStream.push(response)
retStream.push(null)
return
}
response.on('error', (err) => retStream.emit('error', err))
if (options.converter) {
response.on('data', (d) => {
if (d.Bytes && options.progress) {
options.progress(d.Bytes)
}
})
const Converter = options.converter
const convertedResponse = new Converter()
convertedResponse.once('end', () => retStream.push(null))
convertedResponse.on('data', (d) => retStream.push(d))
response.pipe(convertedResponse)
} else {
response.on('data', (d) => {
if (d.Bytes && options.progress) {
options.progress(d.Bytes)
}
retStream.push(d)
})
response.once('end', () => retStream.push(null))
}
})
// signal the multipart that the underlying stream has drained and that
// it can continue producing data..
request.on('drain', () => multipart.emit('drain'))
multipart.pipe(request)
return retStream
}
}
function propOrProp (source, prop1, prop2) {
if (prop1 in source) {
return source[prop1]
} else if (prop2 in source) {
return source[prop2]
}
}