forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
199 lines (174 loc) · 4.7 KB
/
Copy pathmain.js
File metadata and controls
199 lines (174 loc) · 4.7 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
'use strict';
/* eslint-disable no-await-in-loop, max-len */
const CombinedStream = require('./lib/combine');
const {
isString, isNumber, isStream, isHTTPStream, isBuffer,
isArray, isVinyl, getFileName, getContentType, genBoundary
} = require('./lib/helpers');
const { next } = CombinedStream.symbols;
const CRLF = '\r\n';
const length = Symbol('length');
const started = Symbol('started');
const ended = Symbol('ended');
const stack = Symbol('stack');
const init = Symbol('init');
const generate = Symbol('generate');
const defaults = {
name: 'file',
ext: 'bin',
type: 'application/octet-stream',
};
class MultipartLite extends CombinedStream {
constructor(opts = {}) {
super();
this.opts = { ...opts, defaults: { ...defaults, ...opts.defaults } };
this.boundary = this.opts.boundary || genBoundary(this.opts.boundaryPrefix);
this.headers = {
'content-type': `multipart/form-data; boundary="${this.getBoundary()}"`
};
this[length] = 0;
this[stack] = [];
this[started] = false;
this[ended] = false;
}
[init]() {
if (this[ended] || this[started]) {
return;
}
this[started] = true;
let value = this[stack].shift();
while (value) {
this[generate](...value);
value = this[stack].shift();
}
this._append(`--${this.getBoundary()}--`, CRLF);
this[ended] = true;
this[next]();
}
[generate](field, value, { filename, contentType }) {
this._append(`--${this.getBoundary()}${CRLF}`);
this._append(`Content-Disposition: form-data; name="${field}"`);
if (isBuffer(value) || isStream(value) || isHTTPStream(value) || isVinyl(value)) {
if (isVinyl(value)) {
filename = filename || value.basename;
value = value.contents;
}
const file = this.getFileName(filename ? { filename } : value, this.opts.defaults);
this._append(`; filename="${file}"${CRLF}`);
const type = this.getContentType({ filename: filename || file, contentType }, this.opts.defaults);
this._append(`Content-Type: ${type}${CRLF}`);
} else {
this._append(CRLF);
}
return this._append(CRLF, value, CRLF);
}
/**
* List of symbols
* @returns {Object}
* @static
*/
static get symbols() {
return {
...CombinedStream.symbols,
length,
started,
ended,
stack,
init,
generate,
};
}
/**
* Returns content length. Only used with .buffer().
* @param {Function} [cb]
* @returns {Number}
*/
getLength(cb) {
// HACK: for got >= 6.5.0
if (cb && typeof cb === 'function') {
return cb(null, this[length]);
}
return this[length];
}
/**
* Returns boundary.
* @returns {String}
*/
getBoundary() {
return this.boundary;
}
/**
* Returns headers
* @param {Boolean} [chunked = true]
* @returns {{'transfer-encoding': String, 'content-type': String}|{'transfer-encoding': String, 'content-length': String}}
*/
getHeaders(chunked = true) {
if (chunked) {
return { ...this.headers, 'transfer-encoding': 'chunked' };
}
return { ...this.headers, 'content-length': String(this.getLength()) };
}
/**
* Appends data to the stream
* @param {String|Number} field
* @param {Any} value
* @param {Object} [options]
* @param {String} [options.filename]
* @param {String} [options.contentType]
* @returns {this}
*/
append(field, value, options = {}) {
if (!field || (!isNumber(field) && !isString(field))) {
throw new TypeError('Field must be specified and must be a string or a number');
}
if (value === undefined) {
throw new Error('Value can\'t be undefined');
}
if (isArray(value)) {
if (!value.length) {
value = '';
} else {
for (let i = 0; i < value.length; i++) {
this.append(field, value[i], options);
}
return this;
}
}
if (value === true || value === false || value === null) {
value = Number(value);
}
this[stack].push([field, value, options]);
return this;
}
/**
* Returns stream
* @returns {this}
*/
stream() {
this[init]();
return this;
}
/**
* Returns buffer of the stream
* @returns {Promise<Buffer>}
* @async
*/
async buffer() {
return new Promise((resolve, reject) => {
this.once('error', reject);
const buffer = [];
this.on('data', (data) => {
buffer.push(data);
});
this.on('end', () => {
const body = Buffer.concat(buffer);
this[length] = Buffer.byteLength(body);
return resolve(body);
});
return this[init]();
});
}
}
MultipartLite.prototype.getFileName = getFileName;
MultipartLite.prototype.getContentType = getContentType;
module.exports = MultipartLite;