partial-uploader 0.0.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 ADDED
@@ -0,0 +1,6 @@
1
+ (C) 2022 Wilson Foo Yu Kang. All rights reserved except as expressly set out hereinafter.
2
+ Unrestricted sole licence granted to Custom Automated Systems (R) Pte Ltd, revocable with express written notice by Wilson Foo Yu Kang.
3
+
4
+ Dual licensed under:
5
+ NPOSL-3.0 https://opensource.org/licenses/NPOSL-3.0
6
+ Proprietary sub-licence with Custom Automated Systems Pte Ltd (contact sales@customautosys.com to negotiate licence terms)
package/README.md ADDED
@@ -0,0 +1,27 @@
1
+ ## Installation
2
+
3
+ ```sh
4
+ npm install partial-uploader
5
+ ```
6
+
7
+ ## Usage
8
+
9
+ ```js
10
+ import { uploadWithPartialFile } from 'partial-uploader';
11
+
12
+ let file = event.target.files[0];
13
+
14
+ uploadWithPartialFile('/url', this.file, {
15
+ // headers
16
+ }).then((x: any) => {
17
+ if (x.isSuccess)
18
+ return x.id; // folder name
19
+ else
20
+ console.log(x.message);
21
+ }).catch((err: any) => {
22
+ if (err.isSuccess)
23
+ return err.id; // folder name
24
+ else
25
+ console.log(err.message);
26
+ });
27
+ ```
@@ -0,0 +1,6 @@
1
+ declare function uploadWithPartialFile(url: string, file: any, chunkSize?: number): Promise<{
2
+ isSuccess: boolean;
3
+ id: string;
4
+ message: string;
5
+ }>;
6
+ export { uploadWithPartialFile };
package/dist/index.js ADDED
@@ -0,0 +1,156 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.uploadWithPartialFile = void 0;
40
+ function uploadWithPartialFile(url, file, chunkSize) {
41
+ if (chunkSize === void 0) { chunkSize = 26214400; }
42
+ return __awaiter(this, void 0, void 0, function () {
43
+ var isSuccess, id, chunks, i, chunk;
44
+ return __generator(this, function (_a) {
45
+ switch (_a.label) {
46
+ case 0: return [4 /*yield*/, delay(50)];
47
+ case 1:
48
+ _a.sent();
49
+ isSuccess = true;
50
+ id = generateGuid();
51
+ if (!(file.size <= chunkSize)) return [3 /*break*/, 3];
52
+ return [4 /*yield*/, upload(url, file, 1, file.size, file.name, id, 1)];
53
+ case 2:
54
+ isSuccess = _a.sent();
55
+ return [3 /*break*/, 9];
56
+ case 3:
57
+ chunks = splitFileIntoChunks(file, chunkSize);
58
+ i = 1;
59
+ _a.label = 4;
60
+ case 4:
61
+ if (!(i < chunks.length + 1)) return [3 /*break*/, 8];
62
+ chunk = chunks[i - 1];
63
+ if (!chunk)
64
+ return [2 /*return*/, { isSuccess: false, id: id, message: "chunk is undefined" }];
65
+ return [4 /*yield*/, upload(url, chunk, chunks.length, file.size, file.name, id, i)];
66
+ case 5:
67
+ isSuccess = _a.sent();
68
+ if (!isSuccess)
69
+ return [3 /*break*/, 8];
70
+ return [4 /*yield*/, delay(550)];
71
+ case 6:
72
+ _a.sent();
73
+ _a.label = 7;
74
+ case 7:
75
+ i++;
76
+ return [3 /*break*/, 4];
77
+ case 8:
78
+ ;
79
+ _a.label = 9;
80
+ case 9:
81
+ ;
82
+ return [2 /*return*/, { isSuccess: isSuccess, id: id, message: "file uploaded successfully" }];
83
+ }
84
+ });
85
+ });
86
+ }
87
+ exports.uploadWithPartialFile = uploadWithPartialFile;
88
+ var upload = function (url, chunk, chunksLength, fileSize, filename, fileGuid, index) { return __awaiter(void 0, void 0, void 0, function () {
89
+ var formData, isDone, isSuccess, res;
90
+ return __generator(this, function (_a) {
91
+ switch (_a.label) {
92
+ case 0:
93
+ formData = new FormData();
94
+ formData.append('file', chunk, "".concat(filename, "_chunk_").concat(index));
95
+ formData.append('fileGuid', fileGuid);
96
+ isDone = chunksLength === index;
97
+ formData.append('isDone', isDone.toString());
98
+ formData.append('totalSize', fileSize.toString());
99
+ formData.append('totalChunks', chunksLength.toString());
100
+ formData.append('filename', filename);
101
+ isSuccess = true;
102
+ return [4 /*yield*/, uploadSubscribe(url, formData)];
103
+ case 1:
104
+ res = _a.sent();
105
+ if (!(res.status === 406 || res.status === 401)) return [3 /*break*/, 2];
106
+ isSuccess = false;
107
+ return [3 /*break*/, 5];
108
+ case 2: return [4 /*yield*/, delay(500)];
109
+ case 3:
110
+ _a.sent();
111
+ return [4 /*yield*/, uploadSubscribe(url, formData)];
112
+ case 4:
113
+ res = _a.sent();
114
+ if (!res.ok)
115
+ isSuccess = false;
116
+ _a.label = 5;
117
+ case 5:
118
+ ;
119
+ return [2 /*return*/, isSuccess];
120
+ }
121
+ });
122
+ }); };
123
+ var delay = function (ms) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
124
+ switch (_a.label) {
125
+ case 0: return [4 /*yield*/, new Promise(function (f) { return setTimeout(f, ms); })];
126
+ case 1: return [2 /*return*/, _a.sent()];
127
+ }
128
+ }); }); };
129
+ var uploadSubscribe = function (url, formData) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
130
+ switch (_a.label) {
131
+ case 0: return [4 /*yield*/, fetch(url, { method: 'POST', body: formData })];
132
+ case 1: return [2 /*return*/, (_a.sent())];
133
+ }
134
+ }); }); };
135
+ var splitFileIntoChunks = function (file, chunkSize) {
136
+ var chunks = [];
137
+ var start = 0;
138
+ while (start < file.size) {
139
+ var end = Math.min(start + chunkSize, file.size);
140
+ var chunk = file.slice(start, end);
141
+ chunks.push(chunk);
142
+ start = end;
143
+ }
144
+ ;
145
+ return chunks;
146
+ };
147
+ var generateGuid = function () {
148
+ var chars = '0123456789abcdef';
149
+ var guid = '';
150
+ for (var i = 0; i < 40; i++) {
151
+ var randomIndex = Math.floor(Math.random() * chars.length);
152
+ guid += chars.charAt(randomIndex);
153
+ }
154
+ ;
155
+ return guid;
156
+ };
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "partial-uploader",
3
+ "version": "0.0.1",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "/dist"
9
+ ],
10
+ "scripts": {
11
+ "build": "tsc src/index.ts"
12
+ },
13
+ "author": "Yusuf Unal <theunal9@gmail.com> (https://github.com/theunal)",
14
+ "license": "MIT",
15
+ "repository": "https://github.com/theunal/partial-uploader",
16
+ "bugs": {
17
+ "url": "https://github.com/theunal/partial-uploader/issues"
18
+ },
19
+ "homepage": "https://github.com/theunal/partial-uploader#readme",
20
+ "keywords": [
21
+ "chunk",
22
+ "file",
23
+ "upload"
24
+ ],
25
+ "devDependencies": {
26
+ "typescript": "^5.1.3"
27
+ }
28
+ }