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
168 lines (148 loc) · 4.1 KB
/
Copy pathmain.js
File metadata and controls
168 lines (148 loc) · 4.1 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
'use strict';
/* eslint-disable class-methods-use-this, no-await-in-loop, max-len */
const { basename } = require('path');
const MultipartLite = require('multi-part-lite');
const {
isBuffer, isStream, isHTTPStream, isVinyl
} = require('multi-part-lite/lib/helpers');
const mime = require('mime-kind');
const {
init, started, ended, stack, generate, next, length
} = MultipartLite.symbols;
const CRLF = '\r\n';
class Multipart extends MultipartLite {
/**
* Returns file name of val
* @param {Object} val
* @param {String} [val.filename]
* @param {String} [val.path]
* @param {Object} defaults
* @param {String} defaults.name
* @param {String} defaults.ext
* @returns {String}
*/
getFileName(val, { name, ext, type }) {
if (isBuffer(val)) {
return `${name}.${mime.sync(val, type).ext}`;
}
const filename = val.filename || val.path;
if (filename) {
return basename(filename);
}
return `${name}.${ext}`;
}
/**
* Returns content-type of val
* @param {Object} val
* @param {String} [val.contentType]
* @param {Object} defaults
* @param {String} defaults.type
* @returns {String}
*/
getContentType(val, { type }) {
return val.contentType || mime.sync(val.filename, type).mime;
}
}
class MultipartAsync extends MultipartLite {
/**
* Returns file name of val
* @param {Object} val
* @param {String} [val.filename]
* @param {String} [val.path]
* @param {Object} defaults
* @param {String} defaults.name
* @param {String} defaults.ext
* @returns {Promise<String>}
* @async
*/
async getFileName(val, { name, ext, type }) {
if (isBuffer(val)) {
const m = await mime.async(val, type);
return `${name}.${m.ext}`;
}
const filename = val.filename || val.path;
if (filename) {
return basename(filename);
}
return `${name}.${ext}`;
}
/**
* Returns content-type of val
* @param {Object} val
* @param {String} [val.contentType]
* @param {Object} defaults
* @param {String} defaults.type
* @returns {Promise<String>}
* @async
*/
async getContentType(val, { type }) {
if (val.contentType) {
return val.contentType;
}
const m = await mime.async(val.filename, type);
return m.mime;
}
async [init]() {
if (this[ended] || this[started]) {
return;
}
this[started] = true;
let value = this[stack].shift();
while (value) {
await this[generate](...value);
value = this[stack].shift();
}
this._append(`--${this.getBoundary()}--`, CRLF);
this[ended] = true;
this[next]();
}
async [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 = await this.getFileName(filename ? { filename } : value, this.opts.defaults);
this._append(`; filename="${file}"${CRLF}`);
const type = await 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);
}
/**
* Returns stream
* @returns {Promise<this>}
* @async
*/
async stream() {
await 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]().catch(reject);
});
}
}
module.exports = Multipart;
module.exports.MultipartSync = Multipart;
module.exports.MultipartAsync = MultipartAsync;