/
uploader.js
294 lines (245 loc) · 10.3 KB
/
uploader.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
'use strict'
jest.mock('tus-js-client')
const intoStream = require('into-stream')
const fs = require('fs')
const nock = require('nock')
const Uploader = require('../../src/server/Uploader')
const socketClient = require('../mocksocket')
const standalone = require('../../src/standalone')
const Emitter = require('../../src/server/emitter')
afterAll(() => {
nock.cleanAll()
nock.restore()
})
process.env.COMPANION_DATADIR = './test/output'
process.env.COMPANION_DOMAIN = 'localhost:3020'
const { companionOptions } = standalone()
const protocol = 'tus'
describe('uploader with tus protocol', () => {
test('uploader respects uploadUrls', async () => {
const opts = {
endpoint: 'http://localhost/files',
protocol,
companionOptions: { ...companionOptions, uploadUrls: [/^http:\/\/url.myendpoint.com\//] },
}
expect(() => new Uploader(opts)).toThrow(new Uploader.ValidationError('upload destination does not match any allowed destinations'))
})
test('uploader respects uploadUrls, valid', async () => {
const opts = {
endpoint: 'http://url.myendpoint.com/files',
protocol,
companionOptions: { ...companionOptions, uploadUrls: [/^http:\/\/url.myendpoint.com\//] },
}
// eslint-disable-next-line no-new
new Uploader(opts) // no validation error
})
test('uploader respects uploadUrls, localhost', async () => {
const opts = {
endpoint: 'http://localhost:1337/',
protocol,
companionOptions: { ...companionOptions, uploadUrls: [/^http:\/\/localhost:1337\//] },
}
// eslint-disable-next-line no-new
new Uploader(opts) // no validation error
})
test('upload functions with tus protocol', async () => {
const fileContent = Buffer.from('Some file content')
const stream = intoStream(fileContent)
const opts = {
companionOptions,
endpoint: 'http://url.myendpoint.com/files',
protocol: 'tus',
size: fileContent.length,
pathPrefix: companionOptions.filePath,
}
const uploader = new Uploader(opts)
const uploadToken = uploader.token
expect(uploadToken).toBeTruthy()
let progressReceived = 0
const onProgress = jest.fn()
const onUploadSuccess = jest.fn()
const onBeginUploadEvent = jest.fn()
const onUploadEvent = jest.fn()
const emitter = Emitter()
emitter.on('upload-start', onBeginUploadEvent)
emitter.on(uploadToken, onUploadEvent)
const promise = uploader.awaitReady(60000)
// emulate socket connection
socketClient.connect(uploadToken)
socketClient.onProgress(uploadToken, (message) => {
progressReceived = message.payload.bytesUploaded
onProgress(message)
})
socketClient.onUploadSuccess(uploadToken, onUploadSuccess)
await promise
await uploader.tryUploadStream(stream)
expect(progressReceived).toBe(fileContent.length)
expect(onProgress).toHaveBeenLastCalledWith(expect.objectContaining({
payload: expect.objectContaining({
bytesTotal: fileContent.length,
}),
}))
const expectedPayload = expect.objectContaining({
// see __mocks__/tus-js-client.js
url: 'https://tus.endpoint/files/foo-bar',
})
expect(onUploadSuccess).toHaveBeenCalledWith(expect.objectContaining({
payload: expectedPayload,
}))
expect(onBeginUploadEvent).toHaveBeenCalledWith({ token: uploadToken })
expect(onUploadEvent).toHaveBeenLastCalledWith({ action: 'success', payload: expectedPayload })
})
test('upload functions with tus protocol without size', async () => {
const fileContent = Buffer.alloc(1e6)
const stream = intoStream(fileContent)
const opts = {
companionOptions,
endpoint: 'http://url.myendpoint.com/files',
protocol: 'tus',
size: null,
pathPrefix: companionOptions.filePath,
}
const uploader = new Uploader(opts)
const uploadToken = uploader.token
expect(uploadToken).toBeTruthy()
return new Promise((resolve, reject) => {
// validate that the test is resolved on socket connection
uploader.awaitReady(60000).then(() => {
uploader.tryUploadStream(stream).then(() => {
try {
expect(fs.existsSync(uploader.path)).toBe(false)
resolve()
} catch (err) {
reject(err)
}
})
})
let progressReceived = 0
// emulate socket connection
socketClient.connect(uploadToken)
socketClient.onProgress(uploadToken, (message) => {
// validate that the file has been downloaded and saved into the file path
try {
progressReceived = message.payload.bytesUploaded
if (progressReceived === fileContent.length) {
const fileInfo = fs.statSync(uploader.tmpPath)
expect(fileInfo.isFile()).toBe(true)
expect(fileInfo.size).toBe(fileContent.length)
expect(message.payload.bytesTotal).toBe(fileContent.length)
}
} catch (err) {
reject(err)
}
})
socketClient.onUploadSuccess(uploadToken, (message) => {
try {
expect(progressReceived).toBe(fileContent.length)
// see __mocks__/tus-js-client.js
expect(message.payload.url).toBe('https://tus.endpoint/files/foo-bar')
} catch (err) {
reject(err)
}
})
})
})
async function runMultipartTest ({ metadata, useFormData, includeSize = true } = {}) {
const fileContent = Buffer.from('Some file content')
const stream = intoStream(fileContent)
const opts = {
companionOptions,
endpoint: 'http://localhost',
protocol: 'multipart',
size: includeSize ? fileContent.length : undefined,
metadata,
pathPrefix: companionOptions.filePath,
useFormData,
}
const uploader = new Uploader(opts)
return uploader.uploadStream(stream)
}
test('upload functions with xhr protocol', async () => {
nock('http://localhost').post('/').reply(200)
const ret = await runMultipartTest()
expect(ret).toMatchObject({ url: null, extraData: { response: expect.anything(), bytesUploaded: 17 } })
})
// eslint-disable-next-line max-len
const formDataNoMetaMatch = /^----------------------------\d+\r\nContent-Disposition: form-data; name="files\[\]"; filename="uppy-file-[^"]+"\r\nContent-Type: application\/octet-stream\r\n\r\nSome file content\r\n----------------------------\d+--\r\n$/
test('upload functions with xhr formdata', async () => {
nock('http://localhost').post('/', formDataNoMetaMatch)
.reply(200)
const ret = await runMultipartTest({ useFormData: true })
expect(ret).toMatchObject({ url: null, extraData: { response: expect.anything(), bytesUploaded: 17 } })
})
test('upload functions with unknown file size', async () => {
// eslint-disable-next-line max-len
nock('http://localhost').post('/', formDataNoMetaMatch)
.reply(200)
const ret = await runMultipartTest({ useFormData: true, includeSize: false })
expect(ret).toMatchObject({ url: null, extraData: { response: expect.anything(), bytesUploaded: 17 } })
})
// https://github.com/transloadit/uppy/issues/3477
test('upload functions with xhr formdata and metadata', async () => {
// eslint-disable-next-line max-len
nock('http://localhost').post('/', /^----------------------------\d+\r\nContent-Disposition: form-data; name="key1"\r\n\r\nnull\r\n----------------------------\d+\r\nContent-Disposition: form-data; name="key2"\r\n\r\ntrue\r\n----------------------------\d+\r\nContent-Disposition: form-data; name="key3"\r\n\r\n\d+\r\n----------------------------\d+\r\nContent-Disposition: form-data; name="key4"\r\n\r\n\[object Object\]\r\n----------------------------\d+\r\nContent-Disposition: form-data; name="key5"\r\n\r\n\(\) => {}\r\n----------------------------\d+\r\nContent-Disposition: form-data; name="key6"\r\n\r\nSymbol\(\)\r\n----------------------------\d+\r\nContent-Disposition: form-data; name="files\[\]"; filename="uppy-file-[^"]+"\r\nContent-Type: application\/octet-stream\r\n\r\nSome file content\r\n----------------------------\d+--\r\n$/)
.reply(200)
const metadata = {
key1: null, key2: true, key3: 1234, key4: {}, key5: () => {}, key6: Symbol(''),
}
const ret = await runMultipartTest({ useFormData: true, metadata })
expect(ret).toMatchObject({ url: null, extraData: { response: expect.anything(), bytesUploaded: 17 } })
})
test('uploader checks metadata', () => {
const opts = {
companionOptions,
endpoint: 'http://localhost',
protocol,
}
// eslint-disable-next-line no-new
new Uploader({ ...opts, metadata: { key: 'string value' } })
expect(() => new Uploader({ ...opts, metadata: '' })).toThrow(new Uploader.ValidationError('metadata must be an object'))
})
test('uploader respects maxFileSize', async () => {
const opts = {
endpoint: 'http://url.myendpoint.com/files',
companionOptions: { ...companionOptions, maxFileSize: 100 },
size: 101,
}
expect(() => new Uploader(opts)).toThrow(new Uploader.ValidationError('maxFileSize exceeded'))
})
test('uploader respects maxFileSize correctly', async () => {
const opts = {
endpoint: 'http://url.myendpoint.com/files',
protocol,
companionOptions: { ...companionOptions, maxFileSize: 100 },
size: 99,
}
// eslint-disable-next-line no-new
new Uploader(opts) // no validation error
})
test('uploader respects maxFileSize with unknown size', async () => {
const fileContent = Buffer.alloc(10000)
const stream = intoStream(fileContent)
const opts = {
companionOptions: { ...companionOptions, maxFileSize: 1000 },
endpoint: 'http://url.myendpoint.com/files',
protocol: 'tus',
size: null,
pathPrefix: companionOptions.filePath,
}
const uploader = new Uploader(opts)
const uploadToken = uploader.token
// validate that the test is resolved on socket connection
uploader.awaitReady(60000).then(() => uploader.tryUploadStream(stream))
socketClient.connect(uploadToken)
return new Promise((resolve, reject) => {
socketClient.onUploadError(uploadToken, (message) => {
try {
expect(message).toMatchObject({ payload: { error: { message: 'maxFileSize exceeded' } } })
resolve()
} catch (err) {
reject(err)
}
})
})
})
})