@depup/multer-s3 3.0.1-depup.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitattributes +1 -0
- package/.travis.yml +6 -0
- package/LICENSE +22 -0
- package/README.md +33 -0
- package/changes.json +18 -0
- package/index.js +259 -0
- package/package.json +71 -0
- package/test/basic.js +293 -0
- package/test/files/a.txt +1 -0
- package/test/files/ffffff.png +0 -0
- package/test/files/test.svg +3 -0
- package/test/files/test2.svg +9 -0
- package/test/util/mock-s3.js +17 -0
package/.gitattributes
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
*.svg text eol=lf
|
package/.travis.yml
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015 Duncan Wong
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# @depup/multer-s3
|
|
2
|
+
|
|
3
|
+
> Dependency-bumped version of [multer-s3](https://www.npmjs.com/package/multer-s3)
|
|
4
|
+
|
|
5
|
+
Generated by [DepUp](https://github.com/depup/npm) -- all production
|
|
6
|
+
dependencies bumped to latest versions.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @depup/multer-s3
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
| Field | Value |
|
|
15
|
+
|-------|-------|
|
|
16
|
+
| Original | [multer-s3](https://www.npmjs.com/package/multer-s3) @ 3.0.1 |
|
|
17
|
+
| Processed | 2026-03-17 |
|
|
18
|
+
| Smoke test | passed |
|
|
19
|
+
| Deps updated | 3 |
|
|
20
|
+
|
|
21
|
+
## Dependency Changes
|
|
22
|
+
|
|
23
|
+
| Dependency | From | To |
|
|
24
|
+
|------------|------|-----|
|
|
25
|
+
| @aws-sdk/lib-storage | ^3.46.0 | ^3.1011.0 |
|
|
26
|
+
| file-type | ^3.3.0 | ^21.3.3 |
|
|
27
|
+
| run-parallel | ^1.1.6 | ^1.2.0 |
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
Source: https://github.com/depup/npm | Original: https://www.npmjs.com/package/multer-s3
|
|
32
|
+
|
|
33
|
+
License inherited from the original package.
|
package/changes.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"bumped": {
|
|
3
|
+
"@aws-sdk/lib-storage": {
|
|
4
|
+
"from": "^3.46.0",
|
|
5
|
+
"to": "^3.1011.0"
|
|
6
|
+
},
|
|
7
|
+
"file-type": {
|
|
8
|
+
"from": "^3.3.0",
|
|
9
|
+
"to": "^21.3.3"
|
|
10
|
+
},
|
|
11
|
+
"run-parallel": {
|
|
12
|
+
"from": "^1.1.6",
|
|
13
|
+
"to": "^1.2.0"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"timestamp": "2026-03-17T22:56:56.604Z",
|
|
17
|
+
"totalUpdated": 3
|
|
18
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
var crypto = require('crypto')
|
|
2
|
+
var stream = require('stream')
|
|
3
|
+
var fileType = require('file-type')
|
|
4
|
+
var htmlCommentRegex = require('html-comment-regex')
|
|
5
|
+
var parallel = require('run-parallel')
|
|
6
|
+
var Upload = require('@aws-sdk/lib-storage').Upload
|
|
7
|
+
var DeleteObjectCommand = require('@aws-sdk/client-s3').DeleteObjectCommand
|
|
8
|
+
var util = require('util')
|
|
9
|
+
|
|
10
|
+
function staticValue (value) {
|
|
11
|
+
return function (req, file, cb) {
|
|
12
|
+
cb(null, value)
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
var defaultAcl = staticValue('private')
|
|
17
|
+
var defaultContentType = staticValue('application/octet-stream')
|
|
18
|
+
|
|
19
|
+
var defaultMetadata = staticValue(undefined)
|
|
20
|
+
var defaultCacheControl = staticValue(null)
|
|
21
|
+
var defaultContentDisposition = staticValue(null)
|
|
22
|
+
var defaultContentEncoding = staticValue(null)
|
|
23
|
+
var defaultStorageClass = staticValue('STANDARD')
|
|
24
|
+
var defaultSSE = staticValue(null)
|
|
25
|
+
var defaultSSEKMS = staticValue(null)
|
|
26
|
+
|
|
27
|
+
// Regular expression to detect svg file content, inspired by: https://github.com/sindresorhus/is-svg/blob/master/index.js
|
|
28
|
+
// It is not always possible to check for an end tag if a file is very big. The firstChunk, see below, might not be the entire file.
|
|
29
|
+
var svgRegex = /^\s*(?:<\?xml[^>]*>\s*)?(?:<!doctype svg[^>]*>\s*)?<svg[^>]*>/i
|
|
30
|
+
|
|
31
|
+
function isSvg (svg) {
|
|
32
|
+
// Remove DTD entities
|
|
33
|
+
svg = svg.replace(/\s*<!Entity\s+\S*\s*(?:"|')[^"]+(?:"|')\s*>/img, '')
|
|
34
|
+
// Remove DTD markup declarations
|
|
35
|
+
svg = svg.replace(/\[?(?:\s*<![A-Z]+[^>]*>\s*)*\]?/g, '')
|
|
36
|
+
// Remove HTML comments
|
|
37
|
+
svg = svg.replace(htmlCommentRegex, '')
|
|
38
|
+
|
|
39
|
+
return svgRegex.test(svg)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function defaultKey (req, file, cb) {
|
|
43
|
+
crypto.randomBytes(16, function (err, raw) {
|
|
44
|
+
cb(err, err ? undefined : raw.toString('hex'))
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function autoContentType (req, file, cb) {
|
|
49
|
+
file.stream.once('data', function (firstChunk) {
|
|
50
|
+
var type = fileType(firstChunk)
|
|
51
|
+
var mime = 'application/octet-stream' // default type
|
|
52
|
+
|
|
53
|
+
// Make sure to check xml-extension for svg files.
|
|
54
|
+
if ((!type || type.ext === 'xml') && isSvg(firstChunk.toString())) {
|
|
55
|
+
mime = 'image/svg+xml'
|
|
56
|
+
} else if (type) {
|
|
57
|
+
mime = type.mime
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
var outStream = new stream.PassThrough()
|
|
61
|
+
|
|
62
|
+
outStream.write(firstChunk)
|
|
63
|
+
file.stream.pipe(outStream)
|
|
64
|
+
|
|
65
|
+
cb(null, mime, outStream)
|
|
66
|
+
})
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function collect (storage, req, file, cb) {
|
|
70
|
+
parallel([
|
|
71
|
+
storage.getBucket.bind(storage, req, file),
|
|
72
|
+
storage.getKey.bind(storage, req, file),
|
|
73
|
+
storage.getAcl.bind(storage, req, file),
|
|
74
|
+
storage.getMetadata.bind(storage, req, file),
|
|
75
|
+
storage.getCacheControl.bind(storage, req, file),
|
|
76
|
+
storage.getContentDisposition.bind(storage, req, file),
|
|
77
|
+
storage.getStorageClass.bind(storage, req, file),
|
|
78
|
+
storage.getSSE.bind(storage, req, file),
|
|
79
|
+
storage.getSSEKMS.bind(storage, req, file),
|
|
80
|
+
storage.getContentEncoding.bind(storage, req, file)
|
|
81
|
+
], function (err, values) {
|
|
82
|
+
if (err) return cb(err)
|
|
83
|
+
|
|
84
|
+
storage.getContentType(req, file, function (err, contentType, replacementStream) {
|
|
85
|
+
if (err) return cb(err)
|
|
86
|
+
|
|
87
|
+
cb.call(storage, null, {
|
|
88
|
+
bucket: values[0],
|
|
89
|
+
key: values[1],
|
|
90
|
+
acl: values[2],
|
|
91
|
+
metadata: values[3],
|
|
92
|
+
cacheControl: values[4],
|
|
93
|
+
contentDisposition: values[5],
|
|
94
|
+
storageClass: values[6],
|
|
95
|
+
contentType: contentType,
|
|
96
|
+
replacementStream: replacementStream,
|
|
97
|
+
serverSideEncryption: values[7],
|
|
98
|
+
sseKmsKeyId: values[8],
|
|
99
|
+
contentEncoding: values[9]
|
|
100
|
+
})
|
|
101
|
+
})
|
|
102
|
+
})
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function S3Storage (opts) {
|
|
106
|
+
switch (typeof opts.s3) {
|
|
107
|
+
case 'object': this.s3 = opts.s3; break
|
|
108
|
+
default: throw new TypeError('Expected opts.s3 to be object')
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
switch (typeof opts.bucket) {
|
|
112
|
+
case 'function': this.getBucket = opts.bucket; break
|
|
113
|
+
case 'string': this.getBucket = staticValue(opts.bucket); break
|
|
114
|
+
case 'undefined': throw new Error('bucket is required')
|
|
115
|
+
default: throw new TypeError('Expected opts.bucket to be undefined, string or function')
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
switch (typeof opts.key) {
|
|
119
|
+
case 'function': this.getKey = opts.key; break
|
|
120
|
+
case 'undefined': this.getKey = defaultKey; break
|
|
121
|
+
default: throw new TypeError('Expected opts.key to be undefined or function')
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
switch (typeof opts.acl) {
|
|
125
|
+
case 'function': this.getAcl = opts.acl; break
|
|
126
|
+
case 'string': this.getAcl = staticValue(opts.acl); break
|
|
127
|
+
case 'undefined': this.getAcl = defaultAcl; break
|
|
128
|
+
default: throw new TypeError('Expected opts.acl to be undefined, string or function')
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
switch (typeof opts.contentType) {
|
|
132
|
+
case 'function': this.getContentType = opts.contentType; break
|
|
133
|
+
case 'undefined': this.getContentType = defaultContentType; break
|
|
134
|
+
default: throw new TypeError('Expected opts.contentType to be undefined or function')
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
switch (typeof opts.metadata) {
|
|
138
|
+
case 'function': this.getMetadata = opts.metadata; break
|
|
139
|
+
case 'undefined': this.getMetadata = defaultMetadata; break
|
|
140
|
+
default: throw new TypeError('Expected opts.metadata to be undefined or function')
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
switch (typeof opts.cacheControl) {
|
|
144
|
+
case 'function': this.getCacheControl = opts.cacheControl; break
|
|
145
|
+
case 'string': this.getCacheControl = staticValue(opts.cacheControl); break
|
|
146
|
+
case 'undefined': this.getCacheControl = defaultCacheControl; break
|
|
147
|
+
default: throw new TypeError('Expected opts.cacheControl to be undefined, string or function')
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
switch (typeof opts.contentDisposition) {
|
|
151
|
+
case 'function': this.getContentDisposition = opts.contentDisposition; break
|
|
152
|
+
case 'string': this.getContentDisposition = staticValue(opts.contentDisposition); break
|
|
153
|
+
case 'undefined': this.getContentDisposition = defaultContentDisposition; break
|
|
154
|
+
default: throw new TypeError('Expected opts.contentDisposition to be undefined, string or function')
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
switch (typeof opts.contentEncoding) {
|
|
158
|
+
case 'function': this.getContentEncoding = opts.contentEncoding; break
|
|
159
|
+
case 'string': this.getContentEncoding = staticValue(opts.contentEncoding); break
|
|
160
|
+
case 'undefined': this.getContentEncoding = defaultContentEncoding; break
|
|
161
|
+
default: throw new TypeError('Expected opts.contentEncoding to be undefined, string or function')
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
switch (typeof opts.storageClass) {
|
|
165
|
+
case 'function': this.getStorageClass = opts.storageClass; break
|
|
166
|
+
case 'string': this.getStorageClass = staticValue(opts.storageClass); break
|
|
167
|
+
case 'undefined': this.getStorageClass = defaultStorageClass; break
|
|
168
|
+
default: throw new TypeError('Expected opts.storageClass to be undefined, string or function')
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
switch (typeof opts.serverSideEncryption) {
|
|
172
|
+
case 'function': this.getSSE = opts.serverSideEncryption; break
|
|
173
|
+
case 'string': this.getSSE = staticValue(opts.serverSideEncryption); break
|
|
174
|
+
case 'undefined': this.getSSE = defaultSSE; break
|
|
175
|
+
default: throw new TypeError('Expected opts.serverSideEncryption to be undefined, string or function')
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
switch (typeof opts.sseKmsKeyId) {
|
|
179
|
+
case 'function': this.getSSEKMS = opts.sseKmsKeyId; break
|
|
180
|
+
case 'string': this.getSSEKMS = staticValue(opts.sseKmsKeyId); break
|
|
181
|
+
case 'undefined': this.getSSEKMS = defaultSSEKMS; break
|
|
182
|
+
default: throw new TypeError('Expected opts.sseKmsKeyId to be undefined, string, or function')
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
S3Storage.prototype._handleFile = function (req, file, cb) {
|
|
187
|
+
collect(this, req, file, function (err, opts) {
|
|
188
|
+
if (err) return cb(err)
|
|
189
|
+
|
|
190
|
+
var currentSize = 0
|
|
191
|
+
|
|
192
|
+
var params = {
|
|
193
|
+
Bucket: opts.bucket,
|
|
194
|
+
Key: opts.key,
|
|
195
|
+
ACL: opts.acl,
|
|
196
|
+
CacheControl: opts.cacheControl,
|
|
197
|
+
ContentType: opts.contentType,
|
|
198
|
+
Metadata: opts.metadata,
|
|
199
|
+
StorageClass: opts.storageClass,
|
|
200
|
+
ServerSideEncryption: opts.serverSideEncryption,
|
|
201
|
+
SSEKMSKeyId: opts.sseKmsKeyId,
|
|
202
|
+
Body: (opts.replacementStream || file.stream)
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (opts.contentDisposition) {
|
|
206
|
+
params.ContentDisposition = opts.contentDisposition
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (opts.contentEncoding) {
|
|
210
|
+
params.ContentEncoding = opts.contentEncoding
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
var upload = new Upload({
|
|
214
|
+
client: this.s3,
|
|
215
|
+
params: params
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
upload.on('httpUploadProgress', function (ev) {
|
|
219
|
+
if (ev.total) currentSize = ev.total
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
util.callbackify(upload.done.bind(upload))(function (err, result) {
|
|
223
|
+
if (err) return cb(err)
|
|
224
|
+
|
|
225
|
+
cb(null, {
|
|
226
|
+
size: currentSize,
|
|
227
|
+
bucket: opts.bucket,
|
|
228
|
+
key: opts.key,
|
|
229
|
+
acl: opts.acl,
|
|
230
|
+
contentType: opts.contentType,
|
|
231
|
+
contentDisposition: opts.contentDisposition,
|
|
232
|
+
contentEncoding: opts.contentEncoding,
|
|
233
|
+
storageClass: opts.storageClass,
|
|
234
|
+
serverSideEncryption: opts.serverSideEncryption,
|
|
235
|
+
metadata: opts.metadata,
|
|
236
|
+
location: result.Location,
|
|
237
|
+
etag: result.ETag,
|
|
238
|
+
versionId: result.VersionId
|
|
239
|
+
})
|
|
240
|
+
})
|
|
241
|
+
})
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
S3Storage.prototype._removeFile = function (req, file, cb) {
|
|
245
|
+
this.s3.send(
|
|
246
|
+
new DeleteObjectCommand({
|
|
247
|
+
Bucket: file.bucket,
|
|
248
|
+
Key: file.key
|
|
249
|
+
}),
|
|
250
|
+
cb
|
|
251
|
+
)
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
module.exports = function (opts) {
|
|
255
|
+
return new S3Storage(opts)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
module.exports.AUTO_CONTENT_TYPE = autoContentType
|
|
259
|
+
module.exports.DEFAULT_CONTENT_TYPE = defaultContentType
|
package/package.json
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@depup/multer-s3",
|
|
3
|
+
"version": "3.0.1-depup.0",
|
|
4
|
+
"description": "[DepUp] Streaming multer storage engine for AWS S3",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "standard && mocha test/basic.js"
|
|
8
|
+
},
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": ">= 12.0.0"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/badunk/multer-s3.git"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"depup",
|
|
18
|
+
"dependency-bumped",
|
|
19
|
+
"updated-deps",
|
|
20
|
+
"multer-s3",
|
|
21
|
+
"multer",
|
|
22
|
+
"s3",
|
|
23
|
+
"amazon",
|
|
24
|
+
"aws"
|
|
25
|
+
],
|
|
26
|
+
"author": "badunk",
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"bugs": {
|
|
29
|
+
"url": "https://github.com/badunk/multer-s3/issues"
|
|
30
|
+
},
|
|
31
|
+
"homepage": "https://github.com/badunk/multer-s3#readme",
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@aws-sdk/lib-storage": "^3.1011.0",
|
|
34
|
+
"file-type": "^21.3.3",
|
|
35
|
+
"html-comment-regex": "^1.1.2",
|
|
36
|
+
"run-parallel": "^1.2.0"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"@aws-sdk/client-s3": "^3.0.0"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"express": "^4.13.1",
|
|
43
|
+
"form-data": "^1.0.0-rc3",
|
|
44
|
+
"mocha": "^2.2.5",
|
|
45
|
+
"multer": "^1.1.0",
|
|
46
|
+
"on-finished": "^2.3.0",
|
|
47
|
+
"standard": "^5.4.1",
|
|
48
|
+
"xtend": "^4.0.1"
|
|
49
|
+
},
|
|
50
|
+
"depup": {
|
|
51
|
+
"changes": {
|
|
52
|
+
"@aws-sdk/lib-storage": {
|
|
53
|
+
"from": "^3.46.0",
|
|
54
|
+
"to": "^3.1011.0"
|
|
55
|
+
},
|
|
56
|
+
"file-type": {
|
|
57
|
+
"from": "^3.3.0",
|
|
58
|
+
"to": "^21.3.3"
|
|
59
|
+
},
|
|
60
|
+
"run-parallel": {
|
|
61
|
+
"from": "^1.1.6",
|
|
62
|
+
"to": "^1.2.0"
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
"depsUpdated": 3,
|
|
66
|
+
"originalPackage": "multer-s3",
|
|
67
|
+
"originalVersion": "3.0.1",
|
|
68
|
+
"processedAt": "2026-03-17T22:57:14.722Z",
|
|
69
|
+
"smokeTest": "passed"
|
|
70
|
+
}
|
|
71
|
+
}
|
package/test/basic.js
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
/* eslint-env mocha */
|
|
2
|
+
|
|
3
|
+
var multerS3 = require('../')
|
|
4
|
+
|
|
5
|
+
var fs = require('fs')
|
|
6
|
+
var path = require('path')
|
|
7
|
+
var extend = require('xtend')
|
|
8
|
+
var assert = require('assert')
|
|
9
|
+
var multer = require('multer')
|
|
10
|
+
var stream = require('stream')
|
|
11
|
+
var FormData = require('form-data')
|
|
12
|
+
var onFinished = require('on-finished')
|
|
13
|
+
var mockS3 = require('./util/mock-s3')
|
|
14
|
+
|
|
15
|
+
var VALID_OPTIONS = {
|
|
16
|
+
bucket: 'string'
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
var INVALID_OPTIONS = [
|
|
20
|
+
['numeric key', { key: 1337 }],
|
|
21
|
+
['string key', { key: 'string' }],
|
|
22
|
+
['numeric bucket', { bucket: 1337 }],
|
|
23
|
+
['numeric contentType', { contentType: 1337 }]
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
function submitForm (multer, form, cb) {
|
|
27
|
+
form.getLength(function (err, length) {
|
|
28
|
+
if (err) return cb(err)
|
|
29
|
+
|
|
30
|
+
var req = new stream.PassThrough()
|
|
31
|
+
|
|
32
|
+
req.complete = false
|
|
33
|
+
form.once('end', function () {
|
|
34
|
+
req.complete = true
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
form.pipe(req)
|
|
38
|
+
req.headers = {
|
|
39
|
+
'content-type': 'multipart/form-data; boundary=' + form.getBoundary(),
|
|
40
|
+
'content-length': length
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
multer(req, null, function (err) {
|
|
44
|
+
onFinished(req, function () { cb(err, req) })
|
|
45
|
+
})
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
describe('Multer S3', function () {
|
|
50
|
+
it('is exposed as a function', function () {
|
|
51
|
+
assert.equal(typeof multerS3, 'function')
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
INVALID_OPTIONS.forEach(function (testCase) {
|
|
55
|
+
it('throws when given ' + testCase[0], function () {
|
|
56
|
+
function testBody () {
|
|
57
|
+
multerS3(extend(VALID_OPTIONS, testCase[1]))
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
assert.throws(testBody, TypeError)
|
|
61
|
+
})
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it('upload files', function (done) {
|
|
65
|
+
var s3 = mockS3()
|
|
66
|
+
var form = new FormData()
|
|
67
|
+
var storage = multerS3({ s3: s3, bucket: 'test' })
|
|
68
|
+
var upload = multer({ storage: storage })
|
|
69
|
+
var parser = upload.single('image')
|
|
70
|
+
var image = fs.createReadStream(path.join(__dirname, 'files', 'ffffff.png'))
|
|
71
|
+
|
|
72
|
+
form.append('name', 'Multer')
|
|
73
|
+
form.append('image', image)
|
|
74
|
+
|
|
75
|
+
submitForm(parser, form, function (err, req) {
|
|
76
|
+
assert.ifError(err)
|
|
77
|
+
|
|
78
|
+
assert.equal(req.body.name, 'Multer')
|
|
79
|
+
|
|
80
|
+
assert.equal(req.file.fieldname, 'image')
|
|
81
|
+
assert.equal(req.file.originalname, 'ffffff.png')
|
|
82
|
+
assert.equal(req.file.size, 68)
|
|
83
|
+
assert.equal(req.file.bucket, 'test')
|
|
84
|
+
assert.equal(req.file.etag, 'mock-etag')
|
|
85
|
+
assert.equal(req.file.location, 'mock-location')
|
|
86
|
+
|
|
87
|
+
done()
|
|
88
|
+
})
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
it('uploads file with AES256 server-side encryption', function (done) {
|
|
92
|
+
var s3 = mockS3()
|
|
93
|
+
var form = new FormData()
|
|
94
|
+
var storage = multerS3({ s3: s3, bucket: 'test', serverSideEncryption: 'AES256' })
|
|
95
|
+
var upload = multer({ storage: storage })
|
|
96
|
+
var parser = upload.single('image')
|
|
97
|
+
var image = fs.createReadStream(path.join(__dirname, 'files', 'ffffff.png'))
|
|
98
|
+
|
|
99
|
+
form.append('name', 'Multer')
|
|
100
|
+
form.append('image', image)
|
|
101
|
+
|
|
102
|
+
submitForm(parser, form, function (err, req) {
|
|
103
|
+
assert.ifError(err)
|
|
104
|
+
|
|
105
|
+
assert.equal(req.body.name, 'Multer')
|
|
106
|
+
|
|
107
|
+
assert.equal(req.file.fieldname, 'image')
|
|
108
|
+
assert.equal(req.file.originalname, 'ffffff.png')
|
|
109
|
+
assert.equal(req.file.size, 68)
|
|
110
|
+
assert.equal(req.file.bucket, 'test')
|
|
111
|
+
assert.equal(req.file.etag, 'mock-etag')
|
|
112
|
+
assert.equal(req.file.location, 'mock-location')
|
|
113
|
+
assert.equal(req.file.serverSideEncryption, 'AES256')
|
|
114
|
+
|
|
115
|
+
done()
|
|
116
|
+
})
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
it('uploads file with AWS KMS-managed server-side encryption', function (done) {
|
|
120
|
+
var s3 = mockS3()
|
|
121
|
+
var form = new FormData()
|
|
122
|
+
var storage = multerS3({ s3: s3, bucket: 'test', serverSideEncryption: 'aws:kms' })
|
|
123
|
+
var upload = multer({ storage: storage })
|
|
124
|
+
var parser = upload.single('image')
|
|
125
|
+
var image = fs.createReadStream(path.join(__dirname, 'files', 'ffffff.png'))
|
|
126
|
+
|
|
127
|
+
form.append('name', 'Multer')
|
|
128
|
+
form.append('image', image)
|
|
129
|
+
|
|
130
|
+
submitForm(parser, form, function (err, req) {
|
|
131
|
+
assert.ifError(err)
|
|
132
|
+
|
|
133
|
+
assert.equal(req.body.name, 'Multer')
|
|
134
|
+
|
|
135
|
+
assert.equal(req.file.fieldname, 'image')
|
|
136
|
+
assert.equal(req.file.originalname, 'ffffff.png')
|
|
137
|
+
assert.equal(req.file.size, 68)
|
|
138
|
+
assert.equal(req.file.bucket, 'test')
|
|
139
|
+
assert.equal(req.file.etag, 'mock-etag')
|
|
140
|
+
assert.equal(req.file.location, 'mock-location')
|
|
141
|
+
assert.equal(req.file.serverSideEncryption, 'aws:kms')
|
|
142
|
+
|
|
143
|
+
done()
|
|
144
|
+
})
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
it('uploads PNG file with correct content-type', function (done) {
|
|
148
|
+
var s3 = mockS3()
|
|
149
|
+
var form = new FormData()
|
|
150
|
+
var storage = multerS3({ s3: s3, bucket: 'test', serverSideEncryption: 'aws:kms', contentType: multerS3.AUTO_CONTENT_TYPE })
|
|
151
|
+
var upload = multer({ storage: storage })
|
|
152
|
+
var parser = upload.single('image')
|
|
153
|
+
var image = fs.createReadStream(path.join(__dirname, 'files', 'ffffff.png'))
|
|
154
|
+
|
|
155
|
+
form.append('name', 'Multer')
|
|
156
|
+
form.append('image', image)
|
|
157
|
+
|
|
158
|
+
submitForm(parser, form, function (err, req) {
|
|
159
|
+
assert.ifError(err)
|
|
160
|
+
|
|
161
|
+
assert.equal(req.body.name, 'Multer')
|
|
162
|
+
|
|
163
|
+
assert.equal(req.file.fieldname, 'image')
|
|
164
|
+
assert.equal(req.file.contentType, 'image/png')
|
|
165
|
+
assert.equal(req.file.originalname, 'ffffff.png')
|
|
166
|
+
assert.equal(req.file.size, 68)
|
|
167
|
+
assert.equal(req.file.bucket, 'test')
|
|
168
|
+
assert.equal(req.file.etag, 'mock-etag')
|
|
169
|
+
assert.equal(req.file.location, 'mock-location')
|
|
170
|
+
assert.equal(req.file.serverSideEncryption, 'aws:kms')
|
|
171
|
+
|
|
172
|
+
done()
|
|
173
|
+
})
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
it('uploads pure SVG file with correct content-type', function (done) {
|
|
177
|
+
var s3 = mockS3()
|
|
178
|
+
var form = new FormData()
|
|
179
|
+
var storage = multerS3({ s3: s3, bucket: 'test', serverSideEncryption: 'aws:kms', contentType: multerS3.AUTO_CONTENT_TYPE })
|
|
180
|
+
var upload = multer({ storage: storage })
|
|
181
|
+
var parser = upload.single('image')
|
|
182
|
+
var image = fs.createReadStream(path.join(__dirname, 'files', 'test.svg'))
|
|
183
|
+
|
|
184
|
+
form.append('name', 'Multer')
|
|
185
|
+
form.append('image', image)
|
|
186
|
+
|
|
187
|
+
submitForm(parser, form, function (err, req) {
|
|
188
|
+
assert.ifError(err)
|
|
189
|
+
|
|
190
|
+
assert.equal(req.body.name, 'Multer')
|
|
191
|
+
|
|
192
|
+
assert.equal(req.file.fieldname, 'image')
|
|
193
|
+
assert.equal(req.file.contentType, 'image/svg+xml')
|
|
194
|
+
assert.equal(req.file.originalname, 'test.svg')
|
|
195
|
+
assert.equal(req.file.size, 100)
|
|
196
|
+
assert.equal(req.file.bucket, 'test')
|
|
197
|
+
assert.equal(req.file.etag, 'mock-etag')
|
|
198
|
+
assert.equal(req.file.location, 'mock-location')
|
|
199
|
+
assert.equal(req.file.serverSideEncryption, 'aws:kms')
|
|
200
|
+
|
|
201
|
+
done()
|
|
202
|
+
})
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
it('uploads common SVG file with correct content-type', function (done) {
|
|
206
|
+
var s3 = mockS3()
|
|
207
|
+
var form = new FormData()
|
|
208
|
+
var storage = multerS3({ s3: s3, bucket: 'test', serverSideEncryption: 'aws:kms', contentType: multerS3.AUTO_CONTENT_TYPE })
|
|
209
|
+
var upload = multer({ storage: storage })
|
|
210
|
+
var parser = upload.single('image')
|
|
211
|
+
var image = fs.createReadStream(path.join(__dirname, 'files', 'test2.svg'))
|
|
212
|
+
|
|
213
|
+
form.append('name', 'Multer')
|
|
214
|
+
form.append('image', image)
|
|
215
|
+
|
|
216
|
+
submitForm(parser, form, function (err, req) {
|
|
217
|
+
assert.ifError(err)
|
|
218
|
+
|
|
219
|
+
assert.equal(req.body.name, 'Multer')
|
|
220
|
+
|
|
221
|
+
assert.equal(req.file.fieldname, 'image')
|
|
222
|
+
assert.equal(req.file.contentType, 'image/svg+xml')
|
|
223
|
+
assert.equal(req.file.originalname, 'test2.svg')
|
|
224
|
+
assert.equal(req.file.size, 285)
|
|
225
|
+
assert.equal(req.file.bucket, 'test')
|
|
226
|
+
assert.equal(req.file.etag, 'mock-etag')
|
|
227
|
+
assert.equal(req.file.location, 'mock-location')
|
|
228
|
+
assert.equal(req.file.serverSideEncryption, 'aws:kms')
|
|
229
|
+
|
|
230
|
+
done()
|
|
231
|
+
})
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
it('uploads SVG file without quadratic regex', function (done) {
|
|
235
|
+
this.timeout('10s')
|
|
236
|
+
|
|
237
|
+
var s3 = mockS3()
|
|
238
|
+
var form = new FormData()
|
|
239
|
+
var storage = multerS3({ s3: s3, bucket: 'test', serverSideEncryption: 'aws:kms', contentType: multerS3.AUTO_CONTENT_TYPE })
|
|
240
|
+
var upload = multer({ storage: storage })
|
|
241
|
+
var parser = upload.single('image')
|
|
242
|
+
fs.writeFileSync(path.join(__dirname, 'files', 'test_generated.svg'), '<!doctype svg ' + ' '.repeat(34560))
|
|
243
|
+
var image = fs.createReadStream(path.join(__dirname, 'files', 'test_generated.svg'))
|
|
244
|
+
|
|
245
|
+
form.append('name', 'Multer')
|
|
246
|
+
form.append('image', image)
|
|
247
|
+
|
|
248
|
+
submitForm(parser, form, function (err, req) {
|
|
249
|
+
assert.ifError(err)
|
|
250
|
+
|
|
251
|
+
assert.equal(req.body.name, 'Multer')
|
|
252
|
+
|
|
253
|
+
assert.equal(req.file.fieldname, 'image')
|
|
254
|
+
assert.equal(req.file.contentType, 'application/octet-stream')
|
|
255
|
+
assert.equal(req.file.originalname, 'test_generated.svg')
|
|
256
|
+
assert.equal(req.file.size, 34574)
|
|
257
|
+
assert.equal(req.file.bucket, 'test')
|
|
258
|
+
assert.equal(req.file.etag, 'mock-etag')
|
|
259
|
+
assert.equal(req.file.location, 'mock-location')
|
|
260
|
+
assert.equal(req.file.serverSideEncryption, 'aws:kms')
|
|
261
|
+
|
|
262
|
+
done()
|
|
263
|
+
})
|
|
264
|
+
})
|
|
265
|
+
|
|
266
|
+
it('uploads common file as gzip content encoded', function (done) {
|
|
267
|
+
var s3 = mockS3()
|
|
268
|
+
var form = new FormData()
|
|
269
|
+
var storage = multerS3({ s3: s3, bucket: 'test', serverSideEncryption: 'aws:kms', contentType: multerS3.AUTO_CONTENT_TYPE, contentEncoding: 'gzip' })
|
|
270
|
+
var upload = multer({ storage: storage })
|
|
271
|
+
var parser = upload.single('file')
|
|
272
|
+
var image = fs.createReadStream(path.join(__dirname, 'files', 'a.txt'))
|
|
273
|
+
|
|
274
|
+
form.append('name', 'Multer')
|
|
275
|
+
form.append('file', image)
|
|
276
|
+
|
|
277
|
+
submitForm(parser, form, function (err, req) {
|
|
278
|
+
assert.ifError(err)
|
|
279
|
+
|
|
280
|
+
assert.equal(req.body.name, 'Multer')
|
|
281
|
+
assert.equal(req.file.fieldname, 'file')
|
|
282
|
+
assert.equal(req.file.contentType, 'application/octet-stream')
|
|
283
|
+
assert.equal(req.file.originalname, 'a.txt')
|
|
284
|
+
assert.equal(req.file.size, 7)
|
|
285
|
+
assert.equal(req.file.bucket, 'test')
|
|
286
|
+
assert.equal(req.file.etag, 'mock-etag')
|
|
287
|
+
assert.equal(req.file.location, 'mock-location')
|
|
288
|
+
assert.equal(req.file.serverSideEncryption, 'aws:kms')
|
|
289
|
+
assert.equal(req.file.contentEncoding, 'gzip')
|
|
290
|
+
done()
|
|
291
|
+
})
|
|
292
|
+
})
|
|
293
|
+
})
|
package/test/files/a.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
content
|
|
Binary file
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
<?xml version="1.0"?>
|
|
2
|
+
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
|
|
3
|
+
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
|
|
4
|
+
|
|
5
|
+
<svg xmlns="http://www.w3.org/2000/svg"
|
|
6
|
+
width="226" height="226">
|
|
7
|
+
<circle cx="110" cy="107" r="80" stroke="black"
|
|
8
|
+
stroke-width="5" fill="red" />
|
|
9
|
+
</svg>
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
var events = require('events')
|
|
2
|
+
|
|
3
|
+
function createMockS3 () {
|
|
4
|
+
function send (opts, cb) {
|
|
5
|
+
var ee = new events.EventEmitter()
|
|
6
|
+
var buffer = opts['input']['Body']
|
|
7
|
+
ee.emit('httpUploadProgress', { total: buffer.length })
|
|
8
|
+
return Promise.resolve({
|
|
9
|
+
Location: 'mock-location',
|
|
10
|
+
ETag: 'mock-etag'
|
|
11
|
+
})
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return { send: send }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
module.exports = createMockS3
|