-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathStreamUpload.ts
More file actions
98 lines (77 loc) · 4.07 KB
/
StreamUpload.ts
File metadata and controls
98 lines (77 loc) · 4.07 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
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
import "isomorphic-fetch";
import { assert } from "chai";
import * as fs from "fs";
import { StreamUpload } from "../../../src/tasks/FileUploadTask/FileObjectClasses/StreamUpload";
const fileName = "sample_image.jpg";
const filePath = `./test/sample_files/${fileName}`;
const stats = fs.statSync(`./test/sample_files/${fileName}`);
const totalsize = stats.size;
describe("StreamUpload", () => {
it("Stream size smaller than upload range size", async () => {
const readStream = fs.createReadStream(`./test/sample_files/${fileName}`, { highWaterMark: 8 });
const sliceSize = 200;
const upload = new StreamUpload(readStream, fileName, totalsize);
const slice = await upload.sliceFile({ minValue: 0, maxValue: sliceSize - 1 });
assert.isDefined(slice);
assert.equal(sliceSize, (slice as Buffer).length);
});
it("Stream size greater than upload range size", async () => {
const readStream = fs.createReadStream(filePath, { highWaterMark: 200 });
const sliceSize = 100;
const upload = new StreamUpload(readStream, fileName, totalsize);
const slice = await upload.sliceFile({ minValue: 0, maxValue: sliceSize - 1 });
assert.isDefined(slice);
assert.equal(sliceSize, (slice as Buffer).length);
});
it("Stream size with complete file and greater than upload range size", async () => {
const readStream = fs.createReadStream(filePath, { highWaterMark: totalsize });
const sliceSize = 100;
const upload = new StreamUpload(readStream, fileName, totalsize);
const slice = await upload.sliceFile({ minValue: 0, maxValue: sliceSize - 1 });
assert.isDefined(slice);
assert.equal(sliceSize, (slice as Buffer).length);
});
});
describe("Stream upload resume", () => {
it("New range is equal to previous upload range", async () => {
const readStream = fs.createReadStream(filePath, { highWaterMark: totalsize });
const sliceSize = 20;
const upload = new StreamUpload(readStream, fileName, totalsize);
const slice = await upload.sliceFile({ minValue: 0, maxValue: sliceSize - 1 });
const retrySlice = await upload.sliceFile({ minValue: 0, maxValue: sliceSize - 1 });
assert.isDefined(slice);
assert.isDefined(retrySlice);
assert.equal(Buffer.compare(slice as Buffer, retrySlice as Buffer), 0);
});
it("New Range.Minimum greater than previous Range.Minimum and new Range.Maximum is equal previous Range.Maximum", async () => {
const readStream = fs.createReadStream(filePath, { highWaterMark: totalsize });
const sliceSize = 20;
const upload = new StreamUpload(readStream, fileName, totalsize);
const retryRangeMin = 15;
const slice = await upload.sliceFile({ minValue: 0, maxValue: sliceSize - 1 });
const retrySlice = await upload.sliceFile({ minValue: 15, maxValue: sliceSize - 1 });
assert.isDefined(slice);
assert.isDefined(retrySlice);
assert.equal(sliceSize, (slice as Buffer).length);
assert.equal(Buffer.compare(slice.slice(retryRangeMin, sliceSize) as Buffer, retrySlice as Buffer), 0);
});
it("New Range.Minimum greater than previous Range.Minimum and new Range.Maximum is greater than previous Range.Maximum", async () => {
const readStream = fs.createReadStream(filePath, { highWaterMark: totalsize });
const sliceSize = 20;
const retryRangeMin = 15;
const retryRangeMax = 21;
const upload = new StreamUpload(readStream, fileName, totalsize);
const slice = await upload.sliceFile({ minValue: 0, maxValue: sliceSize - 1 });
const retrySlice = (await upload.sliceFile({ minValue: retryRangeMin, maxValue: retryRangeMax })) as Buffer;
assert.isDefined(slice);
assert.isDefined(retrySlice);
assert.equal(retrySlice.length, retryRangeMax - retryRangeMin + 1);
assert.equal(Buffer.compare(slice.slice(retryRangeMin, sliceSize) as Buffer, retrySlice.slice(0, sliceSize - retryRangeMin)), 0);
});
});