-
Notifications
You must be signed in to change notification settings - Fork 205
/
Copy paths3.e2e.ts
341 lines (282 loc) · 9.39 KB
/
s3.e2e.ts
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import {S3Store} from '@tus/s3-store'
import {Server, TUS_RESUMABLE} from '@tus/server'
import type {SuperAgentTest} from 'supertest'
import request from 'supertest'
import type http from 'node:http'
import {describe} from 'node:test'
import {strict as assert} from 'node:assert'
import {S3, S3ServiceException} from '@aws-sdk/client-s3'
const STORE_PATH = '/upload'
interface S3Options {
partSize?: number
useTags?: boolean
expirationPeriodInMilliseconds?: number
}
const expireTime = 5000
const s3Credentials = {
bucket: process.env.AWS_BUCKET as string,
region: process.env.AWS_REGION,
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID as string,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY as string,
},
}
const s3Client = new S3(s3Credentials)
const createStore = (options: S3Options = {}) =>
new S3Store({
...options,
s3ClientConfig: s3Credentials,
})
const createUpload = async (agent: SuperAgentTest, uploadLength?: number) => {
const req = agent.post(STORE_PATH).set('Tus-Resumable', TUS_RESUMABLE)
if (uploadLength) {
req.set('Upload-Length', uploadLength.toString())
} else {
req.set('Upload-Defer-Length', '1')
}
const response = await req.expect(201)
assert(Boolean(response.headers.location), 'location not returned')
const uploadId = response.headers.location.split('/').pop()
return {uploadId: uploadId as string, expires: response.headers['upload-expires']}
}
const allocMB = (mb: number) => Buffer.alloc(1024 * 1024 * mb)
const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
const patchUpload = async (
agent: SuperAgentTest,
uploadId: string,
data: Buffer,
offset = 0,
uploadLength?: number
) => {
const req = agent
.patch(`${STORE_PATH}/${uploadId}`)
.set('Tus-Resumable', TUS_RESUMABLE)
.set('Upload-Offset', offset.toString())
.set('Content-Type', 'application/offset+octet-stream')
if (uploadLength) {
req.set('Upload-Length', uploadLength.toString())
}
const res = await req.send(data).expect(204)
return {offset: Number.parseInt(res.headers['upload-offset'], 10)}
}
describe('S3 Store E2E', () => {
describe('Expiration extension', () => {
let server: Server
let listener: http.Server
let agent: SuperAgentTest
let store: S3Store
before((done) => {
store = createStore({
expirationPeriodInMilliseconds: expireTime,
partSize: 5_242_880,
})
server = new Server({
path: STORE_PATH,
datastore: store,
})
listener = server.listen()
agent = request.agent(listener)
done()
})
let startTime: number
beforeEach(() => {
startTime = Date.now()
})
afterEach(async () => {
const endTime = Date.now()
const elapsedMs = (endTime - startTime) / 10
if (elapsedMs < expireTime) {
await wait(expireTime - elapsedMs + 100)
}
await store.deleteExpired()
})
after((done) => {
listener.close(done)
})
it('should set Tus-Completed=false when the upload is not completed', async () => {
const data = allocMB(11)
const {uploadId} = await createUpload(agent, data.length)
await patchUpload(agent, uploadId, data.subarray(0, 1024 * 1024 * 6))
const {TagSet} = await s3Client.getObjectTagging({
Bucket: s3Credentials.bucket,
Key: `${uploadId}.info`,
})
assert(
TagSet?.find((tag) => tag.Key === 'Tus-Completed')?.Value === 'false',
'object tag Tus-Completed not set to "false"'
)
})
it('should set Tus-Completed=true when the upload is completed', async () => {
const data = allocMB(11)
const {uploadId} = await createUpload(agent, data.length)
const {offset} = await patchUpload(
agent,
uploadId,
data.subarray(0, 1024 * 1024 * 6)
)
await patchUpload(agent, uploadId, data.subarray(offset), offset)
const {TagSet} = await s3Client.getObjectTagging({
Bucket: s3Credentials.bucket,
Key: `${uploadId}.info`,
})
assert(
TagSet?.find((tag) => tag.Key === 'Tus-Completed')?.Value === 'true',
'object tag Tus-Completed not set to "true"'
)
})
it('should not set tags when using useTags and the upload is not completed', async () => {
const store = createStore({
useTags: false,
expirationPeriodInMilliseconds: expireTime,
partSize: 5_242_880,
})
const server = new Server({
path: STORE_PATH,
datastore: store,
})
const listener = server.listen()
agent = request.agent(listener)
const data = allocMB(11)
const {uploadId} = await createUpload(agent, data.length)
await patchUpload(agent, uploadId, data.subarray(0, 1024 * 1024 * 6))
const {TagSet} = await s3Client.getObjectTagging({
Bucket: s3Credentials.bucket,
Key: `${uploadId}.info`,
})
assert.equal(TagSet?.length, 0)
await new Promise((resolve) => listener.close(resolve))
})
it('should not set tags when using useTags and the upload is completed', async () => {
const store = createStore({
useTags: false,
expirationPeriodInMilliseconds: expireTime,
partSize: 5_242_880,
})
const server = new Server({
path: STORE_PATH,
datastore: store,
})
const listener = server.listen()
agent = request.agent(listener)
const data = allocMB(11)
const {uploadId} = await createUpload(agent, data.length)
const {offset} = await patchUpload(
agent,
uploadId,
data.subarray(0, 1024 * 1024 * 6)
)
await patchUpload(agent, uploadId, data.subarray(offset), offset)
const {TagSet} = await s3Client.getObjectTagging({
Bucket: s3Credentials.bucket,
Key: `${uploadId}.info`,
})
assert.equal(TagSet?.length, 0)
await new Promise((resolve) => listener.close(resolve))
})
// TODO: refactor to mocked integration test instead of e2e
it.skip('calling deleteExpired will delete all expired objects', async () => {
const data = allocMB(11)
const {uploadId} = await createUpload(agent, data.length)
await patchUpload(agent, uploadId, data.subarray(0, 1024 * 1024 * 6))
const [infoFile, partFile] = await Promise.all([
s3Client.getObject({
Bucket: s3Credentials.bucket,
Key: `${uploadId}.info`,
}),
s3Client.getObject({
Bucket: s3Credentials.bucket,
Key: `${uploadId}.part`,
}),
])
await store.deleteExpired()
// make sure the files are not deleted
assert(infoFile.$metadata.httpStatusCode === 200)
assert(partFile.$metadata.httpStatusCode === 200)
await wait(expireTime + 100)
// .info file and .part should be deleted since now they should be expired
const deletedFiles = await store.deleteExpired()
assert(deletedFiles === 2, `not all parts were deleted, deleted ${deletedFiles}`)
const files = await Promise.allSettled([
s3Client.getObject({
Bucket: s3Credentials.bucket,
Key: `${uploadId}.info`,
}),
s3Client.getObject({
Bucket: s3Credentials.bucket,
Key: `${uploadId}.part`,
}),
])
assert(
files.every((p) => p.status === 'rejected') === true,
'fetching deleted object succeeded'
)
assert(
files.every((p) => {
assert(p.status === 'rejected')
assert(
p.reason instanceof S3ServiceException,
'error is not of type S3ServiceException'
)
return p.reason.$metadata.httpStatusCode === 404
}) === true,
'not all rejections were 404'
)
})
it('will not allow to upload to an expired url', async () => {
const data = allocMB(11)
const {uploadId} = await createUpload(agent, data.length)
const {offset} = await patchUpload(
agent,
uploadId,
data.subarray(0, 1024 * 1024 * 6)
)
await wait(expireTime + 100)
await agent
.patch(`${STORE_PATH}/${uploadId}`)
.set('Tus-Resumable', TUS_RESUMABLE)
.set('Upload-Offset', offset.toString())
.set('Content-Type', 'application/offset+octet-stream')
.send(data.subarray(offset))
.expect(410)
})
})
describe('Upload', () => {
let server: Server
let listener: http.Server
let agent: SuperAgentTest
let store: S3Store
before((done) => {
store = createStore({
partSize: 5_242_880,
})
server = new Server({
path: STORE_PATH,
datastore: store,
})
listener = server.listen()
agent = request.agent(listener)
done()
})
after((done) => {
listener.close(done)
})
it('can a upload a smaller file than the minPreferred size using a deferred length', async () => {
const data = allocMB(1)
const {uploadId} = await createUpload(agent)
const {offset} = await patchUpload(agent, uploadId, data)
const {offset: offset2} = await patchUpload(
agent,
uploadId,
new Buffer(0),
offset,
data.length
)
assert.equal(offset2, data.length)
const head = await s3Client.headObject({
Bucket: s3Credentials.bucket,
Key: uploadId,
})
assert.equal(head.$metadata.httpStatusCode, 200)
})
})
})