@bytescale/sdk 1.0.0-alpha.1
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/LICENSE +21 -0
- package/README.md +178 -0
- package/dist/browser/cjs/_96d8.main.js +10 -0
- package/dist/browser/cjs/_9d47.main.js +10 -0
- package/dist/browser/cjs/main.js +3946 -0
- package/dist/node/cjs/main.js +3793 -0
- package/dist/node/esm/main.mjs +3816 -0
- package/dist/types/gen/apis/FileApi.d.ts +135 -0
- package/dist/types/gen/apis/FolderApi.d.ts +110 -0
- package/dist/types/gen/apis/JobApi.d.ts +56 -0
- package/dist/types/gen/apis/UploadApi.d.ts +81 -0
- package/dist/types/gen/apis/index.d.ts +4 -0
- package/dist/types/gen/index.d.ts +3 -0
- package/dist/types/gen/models/index.d.ts +2856 -0
- package/dist/types/gen/runtime.d.ts +178 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/utils/ChunkedStream.d.ts +29 -0
- package/dist/types/utils/Model.d.ts +35 -0
- package/dist/types/utils/NodeUtils.d.ts +13 -0
- package/dist/types/utils/UploadManager.d.ts +39 -0
- package/dist/types/utils/index.d.ts +3 -0
- package/package.json +97 -0
- package/tests/ChunkedStream.test.ts +49 -0
- package/tests/UploadManager.test.ts +164 -0
- package/tests/utils/RandomStream.ts +76 -0
- package/tests/utils/StreamToBuffer.ts +8 -0
- package/tests/utils/TempUtils.ts +9 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// import randomGen from "random-seed";
|
|
2
|
+
import { Readable } from "stream";
|
|
3
|
+
import fs, { promises as fsAsync } from "fs";
|
|
4
|
+
import { prepareTempDirectory } from "./TempUtils";
|
|
5
|
+
import * as Path from "path";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* IMPORTANT:
|
|
9
|
+
* It's difficult to efficiently create a pseudorandom stream, since 'read' may be called a different number of times
|
|
10
|
+
* (i.e. with different read sizes) across two streams, so if the implementation updates the entrophy based on these
|
|
11
|
+
* read calls, it will produce two different results for the streams.
|
|
12
|
+
*
|
|
13
|
+
* As such, we create a random stream, save to file, then stream from the file.
|
|
14
|
+
*/
|
|
15
|
+
function createRandomStream(sizeInBytes: number): NodeJS.ReadableStream {
|
|
16
|
+
const dictionary = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".split("");
|
|
17
|
+
|
|
18
|
+
let producedSize = 0;
|
|
19
|
+
let iteration = 0;
|
|
20
|
+
return new Readable({
|
|
21
|
+
read(readSize) {
|
|
22
|
+
let shouldEnd = false;
|
|
23
|
+
|
|
24
|
+
if (producedSize + readSize >= sizeInBytes) {
|
|
25
|
+
readSize = sizeInBytes - producedSize;
|
|
26
|
+
shouldEnd = true;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const character = dictionary[iteration % dictionary.length];
|
|
30
|
+
const prefix = `${iteration}`.padEnd(10);
|
|
31
|
+
|
|
32
|
+
iteration++;
|
|
33
|
+
producedSize += readSize;
|
|
34
|
+
|
|
35
|
+
if (readSize > prefix.length + 2) {
|
|
36
|
+
// Makes debugging a little easier, if we need to read the files.
|
|
37
|
+
this.push(Buffer.from(prefix));
|
|
38
|
+
this.push(Buffer.alloc(readSize - (prefix.length + 1), character));
|
|
39
|
+
this.push(Buffer.alloc(1, "\n"));
|
|
40
|
+
} else {
|
|
41
|
+
this.push(Buffer.alloc(readSize, character));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (shouldEnd) {
|
|
45
|
+
this.push(null);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function writeToDisk(reader: NodeJS.ReadableStream, path: string): Promise<void> {
|
|
52
|
+
await new Promise((resolve, reject) => {
|
|
53
|
+
const writer = fs.createWriteStream(path);
|
|
54
|
+
writer.on("close", resolve);
|
|
55
|
+
writer.on("error", reject);
|
|
56
|
+
reader.pipe(writer);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Creates a method that will return a stream that holds the exact same (random) data each time.
|
|
62
|
+
* @param sizeInBytes
|
|
63
|
+
*/
|
|
64
|
+
export async function createRandomStreamFactory(sizeInBytes: number): Promise<() => NodeJS.ReadableStream> {
|
|
65
|
+
const dir = await prepareTempDirectory();
|
|
66
|
+
const file = Path.join(dir, "random.txt");
|
|
67
|
+
|
|
68
|
+
await writeToDisk(createRandomStream(sizeInBytes), file);
|
|
69
|
+
|
|
70
|
+
const fileInfo = await fsAsync.stat(file);
|
|
71
|
+
if (fileInfo.size !== sizeInBytes) {
|
|
72
|
+
throw new Error("Created a random source file with incorrect size!");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return () => fs.createReadStream(file);
|
|
76
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export async function streamToBuffer(stream: NodeJS.ReadableStream): Promise<Buffer> {
|
|
2
|
+
return await new Promise<Buffer>((resolve, reject) => {
|
|
3
|
+
const buffers = Array<Buffer>();
|
|
4
|
+
stream.on("data", chunk => buffers.push(chunk));
|
|
5
|
+
stream.on("end", () => resolve(Buffer.concat(buffers)));
|
|
6
|
+
stream.on("error", err => reject(err));
|
|
7
|
+
});
|
|
8
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { promises as fsAsync } from "fs";
|
|
2
|
+
import * as Path from "path";
|
|
3
|
+
|
|
4
|
+
export async function prepareTempDirectory(): Promise<string> {
|
|
5
|
+
const folder = Path.resolve(`tmp/${Date.now()}`);
|
|
6
|
+
await fsAsync.rm(folder, { recursive: true, force: true });
|
|
7
|
+
await fsAsync.mkdir(folder, { recursive: true });
|
|
8
|
+
return folder;
|
|
9
|
+
}
|