snack-sdk 3.6.0-rc.1 → 3.8.0
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/CHANGELOG.md +16 -0
- package/build/DependencyResolver.d.ts +26 -25
- package/build/DependencyResolver.js +258 -257
- package/build/DependencyResolver.js.map +1 -1
- package/build/DevSession.d.ts +20 -20
- package/build/DevSession.js +203 -203
- package/build/FileUploader.d.ts +23 -22
- package/build/FileUploader.js +174 -174
- package/build/FileUploader.js.map +1 -1
- package/build/Logger.d.ts +11 -11
- package/build/Logger.js +33 -33
- package/build/Session.d.ts +301 -296
- package/build/Session.js +1171 -1163
- package/build/Session.js.map +1 -1
- package/build/State.d.ts +16 -16
- package/build/State.js +90 -90
- package/build/State.js.map +1 -1
- package/build/WantedVersions.d.ts +17 -16
- package/build/WantedVersions.js +93 -96
- package/build/WantedVersions.js.map +1 -1
- package/build/defaultConfig.d.ts +12 -13
- package/build/defaultConfig.js +29 -29
- package/build/defaultConfig.js.map +1 -1
- package/build/index.d.ts +7 -7
- package/build/index.js +30 -30
- package/build/index.js.map +1 -1
- package/build/transports/CodeMessageBuilder.d.ts +31 -31
- package/build/transports/CodeMessageBuilder.js +196 -196
- package/build/transports/CodeMessageBuilder.js.map +1 -1
- package/build/transports/Protocol.d.ts +64 -64
- package/build/transports/Protocol.js +2 -2
- package/build/transports/Protocol.js.map +1 -1
- package/build/transports/Transport.d.ts +27 -27
- package/build/transports/Transport.js +298 -298
- package/build/transports/index.d.ts +3 -3
- package/build/transports/index.js +22 -22
- package/build/transports/types.d.ts +63 -63
- package/build/transports/types.js +2 -2
- package/build/transports/types.js.map +1 -1
- package/build/transports/webPlayer.d.ts +12 -11
- package/build/transports/webPlayer.js +70 -70
- package/build/transports/webPlayer.js.map +1 -1
- package/build/types.d.ts +185 -250
- package/build/types.js +2 -2
- package/build/types.js.map +1 -1
- package/build/utils.d.ts +17 -16
- package/build/utils.js +68 -68
- package/build/utils.js.map +1 -1
- package/package.json +3 -2
- package/src/DependencyResolver.ts +11 -7
- package/src/FileUploader.ts +3 -1
- package/src/Session.ts +19 -6
- package/src/State.ts +1 -1
- package/src/WantedVersions.ts +3 -2
- package/src/defaultConfig.ts +5 -5
- package/src/index.ts +37 -9
- package/src/transports/CodeMessageBuilder.ts +7 -2
- package/src/transports/Protocol.ts +1 -1
- package/src/transports/types.ts +2 -1
- package/src/transports/webPlayer.ts +3 -1
- package/src/types.ts +19 -83
- package/src/utils.ts +2 -1
- package/build/sdk.d.ts +0 -34
- package/build/sdk.js +0 -139
- package/build/sdk.js.map +0 -1
- package/build/sdks/features.d.ts +0 -9
- package/build/sdks/features.js +0 -13
- package/build/sdks/features.js.map +0 -1
- package/build/sdks/index.d.ts +0 -5
- package/build/sdks/index.js +0 -124
- package/build/sdks/index.js.map +0 -1
- package/build/sdks/types.d.ts +0 -21
- package/build/sdks/types.js +0 -3
- package/build/sdks/types.js.map +0 -1
- package/src/sdk.ts +0 -135
- package/src/sdks/features.ts +0 -11
- package/src/sdks/index.ts +0 -159
- package/src/sdks/types.ts +0 -49
package/build/FileUploader.js
CHANGED
|
@@ -1,175 +1,175 @@
|
|
|
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 (_) 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
|
-
var utils_1 = require("./utils");
|
|
40
|
-
var FileUploader = /** @class */ (function () {
|
|
41
|
-
function FileUploader(options) {
|
|
42
|
-
this.status = {};
|
|
43
|
-
this.apiURL = options.apiURL;
|
|
44
|
-
this.callback = options.callback;
|
|
45
|
-
this.logger = options.logger;
|
|
46
|
-
}
|
|
47
|
-
FileUploader.prototype.add = function (path, file) {
|
|
48
|
-
var status = this.status[path];
|
|
49
|
-
if (status && status.file === file) {
|
|
50
|
-
return status.promise;
|
|
51
|
-
}
|
|
52
|
-
this.status[path] = {
|
|
53
|
-
file: file,
|
|
54
|
-
promise: this.upload(path, file),
|
|
55
|
-
};
|
|
56
|
-
return this.status[path].promise;
|
|
57
|
-
};
|
|
58
|
-
FileUploader.prototype.remove = function (path, file) {
|
|
59
|
-
var _a;
|
|
60
|
-
if (!file || ((_a = this.status[path]) === null || _a === void 0 ? void 0 : _a.file) === file) {
|
|
61
|
-
delete this.status[path];
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
FileUploader.prototype.waitForCompletion = function () {
|
|
65
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
66
|
-
var promises;
|
|
67
|
-
return __generator(this, function (_a) {
|
|
68
|
-
switch (_a.label) {
|
|
69
|
-
case 0:
|
|
70
|
-
promises = Object.values(this.status).map(function (status) { return status.promise; });
|
|
71
|
-
_a.label = 1;
|
|
72
|
-
case 1:
|
|
73
|
-
if (!promises.length) return [3 /*break*/, 3];
|
|
74
|
-
return [4 /*yield*/, Promise.all(promises)];
|
|
75
|
-
case 2:
|
|
76
|
-
_a.sent();
|
|
77
|
-
promises = Object.values(this.status).map(function (status) { return status.promise; });
|
|
78
|
-
return [3 /*break*/, 1];
|
|
79
|
-
case 3: return [2 /*return*/];
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
});
|
|
83
|
-
};
|
|
84
|
-
FileUploader.prototype.upload = function (path, file) {
|
|
85
|
-
var _a, _b, _c, _d, _e, _f, _g;
|
|
86
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
87
|
-
var url, input, formData, response, text, result, resultURL, e_1, error;
|
|
88
|
-
return __generator(this, function (_h) {
|
|
89
|
-
switch (_h.label) {
|
|
90
|
-
case 0:
|
|
91
|
-
_h.trys.push([0, 5, , 6]);
|
|
92
|
-
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.module('Uploading file', path, '...');
|
|
93
|
-
url = void 0;
|
|
94
|
-
input = void 0;
|
|
95
|
-
switch (file.type) {
|
|
96
|
-
case 'CODE':
|
|
97
|
-
url = this.apiURL + "/--/api/v2/snack/uploadCode";
|
|
98
|
-
input = {
|
|
99
|
-
method: 'POST',
|
|
100
|
-
body: JSON.stringify({ code: file.contents }),
|
|
101
|
-
headers: { 'Content-Type': 'application/json' },
|
|
102
|
-
};
|
|
103
|
-
break;
|
|
104
|
-
case 'ASSET':
|
|
105
|
-
url = this.apiURL + "/--/api/v2/snack/uploadAsset";
|
|
106
|
-
if ((typeof FormData !== 'undefined' && file.contents instanceof FormData) ||
|
|
107
|
-
(typeof file.contents === 'object' && ((_b = file.contents.constructor) === null || _b === void 0 ? void 0 : _b.name) === 'FormData')) {
|
|
108
|
-
input = {
|
|
109
|
-
method: 'POST',
|
|
110
|
-
body: file.contents,
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
else if (typeof FormData !== 'undefined') {
|
|
114
|
-
formData = new FormData();
|
|
115
|
-
// @ts-expect-error: file.contents can be both File or Blob
|
|
116
|
-
formData.append('asset', file.contents, ((_c = file.contents) === null || _c === void 0 ? void 0 : _c.name) || path);
|
|
117
|
-
input = {
|
|
118
|
-
method: 'POST',
|
|
119
|
-
body: formData,
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
else {
|
|
123
|
-
// TODO: Add support for uploading files using ArrayBuffer in body to the /snack/uploadAsset end-point
|
|
124
|
-
// That way it's no longer neccessary to polyfill FormData is environments such as node.
|
|
125
|
-
throw new Error('Uploading assets is not supported in this environment. Make sure FormData is polyfilled or provide the data as a FormData object');
|
|
126
|
-
}
|
|
127
|
-
break;
|
|
128
|
-
default:
|
|
129
|
-
// @ts-ignore: Property 'type' does not exist on type 'never'
|
|
130
|
-
throw new Error("Invalid file type " + file.type);
|
|
131
|
-
}
|
|
132
|
-
return [4 /*yield*/, utils_1.fetch(url, input)];
|
|
133
|
-
case 1:
|
|
134
|
-
response = _h.sent();
|
|
135
|
-
if (!!response.ok) return [3 /*break*/, 3];
|
|
136
|
-
return [4 /*yield*/, response.text()];
|
|
137
|
-
case 2:
|
|
138
|
-
text = _h.sent();
|
|
139
|
-
throw new Error(text);
|
|
140
|
-
case 3: return [4 /*yield*/, response.json()];
|
|
141
|
-
case 4:
|
|
142
|
-
result = _h.sent();
|
|
143
|
-
resultURL = result.url;
|
|
144
|
-
if (((_d = this.status[path]) === null || _d === void 0 ? void 0 : _d.file) === file) {
|
|
145
|
-
(_e = this.logger) === null || _e === void 0 ? void 0 : _e.module('Uploaded file', path, resultURL);
|
|
146
|
-
delete this.status[path];
|
|
147
|
-
try {
|
|
148
|
-
this.callback({ path: path, file: file }, resultURL);
|
|
149
|
-
}
|
|
150
|
-
catch (e) {
|
|
151
|
-
return [2 /*return*/, Promise.reject(e)];
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
return [3 /*break*/, 6];
|
|
155
|
-
case 5:
|
|
156
|
-
e_1 = _h.sent();
|
|
157
|
-
error = utils_1.createError({
|
|
158
|
-
message: "Failed to upload file " + path + " (" + e_1.message + ")",
|
|
159
|
-
fileName: path,
|
|
160
|
-
});
|
|
161
|
-
(_f = this.logger) === null || _f === void 0 ? void 0 : _f.error(error);
|
|
162
|
-
if (((_g = this.status[path]) === null || _g === void 0 ? void 0 : _g.file) === file) {
|
|
163
|
-
delete this.status[path];
|
|
164
|
-
this.callback({ path: path, file: file }, undefined, error);
|
|
165
|
-
}
|
|
166
|
-
return [3 /*break*/, 6];
|
|
167
|
-
case 6: return [2 /*return*/];
|
|
168
|
-
}
|
|
169
|
-
});
|
|
170
|
-
});
|
|
171
|
-
};
|
|
172
|
-
return FileUploader;
|
|
173
|
-
}());
|
|
174
|
-
exports.default = FileUploader;
|
|
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 (_) 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
|
+
var utils_1 = require("./utils");
|
|
40
|
+
var FileUploader = /** @class */ (function () {
|
|
41
|
+
function FileUploader(options) {
|
|
42
|
+
this.status = {};
|
|
43
|
+
this.apiURL = options.apiURL;
|
|
44
|
+
this.callback = options.callback;
|
|
45
|
+
this.logger = options.logger;
|
|
46
|
+
}
|
|
47
|
+
FileUploader.prototype.add = function (path, file) {
|
|
48
|
+
var status = this.status[path];
|
|
49
|
+
if (status && status.file === file) {
|
|
50
|
+
return status.promise;
|
|
51
|
+
}
|
|
52
|
+
this.status[path] = {
|
|
53
|
+
file: file,
|
|
54
|
+
promise: this.upload(path, file),
|
|
55
|
+
};
|
|
56
|
+
return this.status[path].promise;
|
|
57
|
+
};
|
|
58
|
+
FileUploader.prototype.remove = function (path, file) {
|
|
59
|
+
var _a;
|
|
60
|
+
if (!file || ((_a = this.status[path]) === null || _a === void 0 ? void 0 : _a.file) === file) {
|
|
61
|
+
delete this.status[path];
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
FileUploader.prototype.waitForCompletion = function () {
|
|
65
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
66
|
+
var promises;
|
|
67
|
+
return __generator(this, function (_a) {
|
|
68
|
+
switch (_a.label) {
|
|
69
|
+
case 0:
|
|
70
|
+
promises = Object.values(this.status).map(function (status) { return status.promise; });
|
|
71
|
+
_a.label = 1;
|
|
72
|
+
case 1:
|
|
73
|
+
if (!promises.length) return [3 /*break*/, 3];
|
|
74
|
+
return [4 /*yield*/, Promise.all(promises)];
|
|
75
|
+
case 2:
|
|
76
|
+
_a.sent();
|
|
77
|
+
promises = Object.values(this.status).map(function (status) { return status.promise; });
|
|
78
|
+
return [3 /*break*/, 1];
|
|
79
|
+
case 3: return [2 /*return*/];
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
FileUploader.prototype.upload = function (path, file) {
|
|
85
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
86
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
87
|
+
var url, input, formData, response, text, result, resultURL, e_1, error;
|
|
88
|
+
return __generator(this, function (_h) {
|
|
89
|
+
switch (_h.label) {
|
|
90
|
+
case 0:
|
|
91
|
+
_h.trys.push([0, 5, , 6]);
|
|
92
|
+
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.module('Uploading file', path, '...');
|
|
93
|
+
url = void 0;
|
|
94
|
+
input = void 0;
|
|
95
|
+
switch (file.type) {
|
|
96
|
+
case 'CODE':
|
|
97
|
+
url = this.apiURL + "/--/api/v2/snack/uploadCode";
|
|
98
|
+
input = {
|
|
99
|
+
method: 'POST',
|
|
100
|
+
body: JSON.stringify({ code: file.contents }),
|
|
101
|
+
headers: { 'Content-Type': 'application/json' },
|
|
102
|
+
};
|
|
103
|
+
break;
|
|
104
|
+
case 'ASSET':
|
|
105
|
+
url = this.apiURL + "/--/api/v2/snack/uploadAsset";
|
|
106
|
+
if ((typeof FormData !== 'undefined' && file.contents instanceof FormData) ||
|
|
107
|
+
(typeof file.contents === 'object' && ((_b = file.contents.constructor) === null || _b === void 0 ? void 0 : _b.name) === 'FormData')) {
|
|
108
|
+
input = {
|
|
109
|
+
method: 'POST',
|
|
110
|
+
body: file.contents,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
else if (typeof FormData !== 'undefined') {
|
|
114
|
+
formData = new FormData();
|
|
115
|
+
// @ts-expect-error: file.contents can be both File or Blob
|
|
116
|
+
formData.append('asset', file.contents, ((_c = file.contents) === null || _c === void 0 ? void 0 : _c.name) || path);
|
|
117
|
+
input = {
|
|
118
|
+
method: 'POST',
|
|
119
|
+
body: formData,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
// TODO: Add support for uploading files using ArrayBuffer in body to the /snack/uploadAsset end-point
|
|
124
|
+
// That way it's no longer neccessary to polyfill FormData is environments such as node.
|
|
125
|
+
throw new Error('Uploading assets is not supported in this environment. Make sure FormData is polyfilled or provide the data as a FormData object');
|
|
126
|
+
}
|
|
127
|
+
break;
|
|
128
|
+
default:
|
|
129
|
+
// @ts-ignore: Property 'type' does not exist on type 'never'
|
|
130
|
+
throw new Error("Invalid file type " + file.type);
|
|
131
|
+
}
|
|
132
|
+
return [4 /*yield*/, utils_1.fetch(url, input)];
|
|
133
|
+
case 1:
|
|
134
|
+
response = _h.sent();
|
|
135
|
+
if (!!response.ok) return [3 /*break*/, 3];
|
|
136
|
+
return [4 /*yield*/, response.text()];
|
|
137
|
+
case 2:
|
|
138
|
+
text = _h.sent();
|
|
139
|
+
throw new Error(text);
|
|
140
|
+
case 3: return [4 /*yield*/, response.json()];
|
|
141
|
+
case 4:
|
|
142
|
+
result = _h.sent();
|
|
143
|
+
resultURL = result.url;
|
|
144
|
+
if (((_d = this.status[path]) === null || _d === void 0 ? void 0 : _d.file) === file) {
|
|
145
|
+
(_e = this.logger) === null || _e === void 0 ? void 0 : _e.module('Uploaded file', path, resultURL);
|
|
146
|
+
delete this.status[path];
|
|
147
|
+
try {
|
|
148
|
+
this.callback({ path: path, file: file }, resultURL);
|
|
149
|
+
}
|
|
150
|
+
catch (e) {
|
|
151
|
+
return [2 /*return*/, Promise.reject(e)];
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return [3 /*break*/, 6];
|
|
155
|
+
case 5:
|
|
156
|
+
e_1 = _h.sent();
|
|
157
|
+
error = utils_1.createError({
|
|
158
|
+
message: "Failed to upload file " + path + " (" + e_1.message + ")",
|
|
159
|
+
fileName: path,
|
|
160
|
+
});
|
|
161
|
+
(_f = this.logger) === null || _f === void 0 ? void 0 : _f.error(error);
|
|
162
|
+
if (((_g = this.status[path]) === null || _g === void 0 ? void 0 : _g.file) === file) {
|
|
163
|
+
delete this.status[path];
|
|
164
|
+
this.callback({ path: path, file: file }, undefined, error);
|
|
165
|
+
}
|
|
166
|
+
return [3 /*break*/, 6];
|
|
167
|
+
case 6: return [2 /*return*/];
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
};
|
|
172
|
+
return FileUploader;
|
|
173
|
+
}());
|
|
174
|
+
exports.default = FileUploader;
|
|
175
175
|
//# sourceMappingURL=FileUploader.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FileUploader.js","sourceRoot":"","sources":["../src/FileUploader.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"FileUploader.js","sourceRoot":"","sources":["../src/FileUploader.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,iCAA6C;AAa7C;IAWE,sBAAY,OAA4E;QARhF,WAAM,GAKV,EAAE,CAAC;QAIL,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;QAC7B,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;QACjC,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAC/B,CAAC;IAED,0BAAG,GAAH,UAAI,IAAY,EAAE,IAAe;QAC/B,IAAM,MAAM,GAAG,IA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E,IAAI,MAAA,EAAE,EAAE,SAAS,EAAE,KAAK,CAAC,CAAC;yBACjD;;;;;;KAEJ;IACH,mBAAC;AAAD,CAAC,AArHD,IAqHC","sourcesContent":["import { SnackFile } from 'snack-content';\n\nimport { Logger } from './Logger';\nimport { SnackError } from './types';\nimport { fetch, createError } from './utils';\n\nexport type FileUploaderRequest = {\n path: string;\n file: SnackFile;\n};\n\nexport type FileUploaderCallback = (\n request: FileUploaderRequest,\n resultURL?: string,\n error?: SnackError\n) => any;\n\nexport default class FileUploader {\n private apiURL: string;\n private logger?: Logger;\n private status: {\n [path: string]: {\n file: SnackFile;\n promise: Promise<any>;\n };\n } = {};\n private callback: FileUploaderCallback;\n\n constructor(options: { apiURL: string; callback: FileUploaderCallback; logger?: Logger }) {\n this.apiURL = options.apiURL;\n this.callback = options.callback;\n this.logger = options.logger;\n }\n\n add(path: string, file: SnackFile) {\n const status = this.status[path];\n if (status && status.file === file) {\n return status.promise;\n }\n this.status[path] = {\n file,\n promise: this.upload(path, file),\n };\n return this.status[path].promise;\n }\n\n remove(path: string, file?: SnackFile) {\n if (!file || this.status[path]?.file === file) {\n delete this.status[path];\n }\n }\n\n async waitForCompletion() {\n let promises = Object.values(this.status).map((status) => status.promise);\n while (promises.length) {\n await Promise.all(promises);\n promises = Object.values(this.status).map((status) => status.promise);\n }\n }\n\n private async upload(path: string, file: SnackFile): Promise<any> {\n try {\n this.logger?.module('Uploading file', path, '...');\n let url: string;\n let input;\n switch (file.type) {\n case 'CODE':\n url = `${this.apiURL}/--/api/v2/snack/uploadCode`;\n input = {\n method: 'POST',\n body: JSON.stringify({ code: file.contents }),\n headers: { 'Content-Type': 'application/json' },\n };\n break;\n case 'ASSET':\n url = `${this.apiURL}/--/api/v2/snack/uploadAsset`;\n if (\n (typeof FormData !== 'undefined' && file.contents instanceof FormData) ||\n (typeof file.contents === 'object' && file.contents.constructor?.name === 'FormData')\n ) {\n input = {\n method: 'POST',\n body: file.contents,\n };\n } else if (typeof FormData !== 'undefined') {\n const formData = new FormData();\n // @ts-expect-error: file.contents can be both File or Blob\n formData.append('asset', file.contents, file.contents?.name || path);\n input = {\n method: 'POST',\n body: formData,\n };\n } else {\n // TODO: Add support for uploading files using ArrayBuffer in body to the /snack/uploadAsset end-point\n // That way it's no longer neccessary to polyfill FormData is environments such as node.\n throw new Error(\n 'Uploading assets is not supported in this environment. Make sure FormData is polyfilled or provide the data as a FormData object'\n );\n }\n break;\n default:\n // @ts-ignore: Property 'type' does not exist on type 'never'\n throw new Error(`Invalid file type ${file.type}`);\n }\n\n const response = await fetch(url, input);\n if (!response.ok) {\n const text = await response.text();\n throw new Error(text);\n }\n const result = await response.json();\n const resultURL = result.url;\n\n if (this.status[path]?.file === file) {\n this.logger?.module('Uploaded file', path, resultURL);\n delete this.status[path];\n try {\n this.callback({ path, file }, resultURL);\n } catch (e) {\n return Promise.reject(e);\n }\n }\n } catch (e) {\n const error = createError({\n message: `Failed to upload file ${path} (${e.message})`,\n fileName: path,\n });\n this.logger?.error(error);\n if (this.status[path]?.file === file) {\n delete this.status[path];\n this.callback({ path, file }, undefined, error);\n }\n }\n }\n}\n"]}
|
package/build/Logger.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
declare type LogFn = (...messages: unknown[]) => void;
|
|
2
|
-
export interface Logger {
|
|
3
|
-
info: LogFn;
|
|
4
|
-
warn: LogFn;
|
|
5
|
-
error: LogFn;
|
|
6
|
-
comm: LogFn;
|
|
7
|
-
comm_recv: LogFn;
|
|
8
|
-
module: LogFn;
|
|
9
|
-
}
|
|
10
|
-
export declare function createLogger(isEnabled: boolean): Logger;
|
|
11
|
-
export {};
|
|
1
|
+
declare type LogFn = (...messages: unknown[]) => void;
|
|
2
|
+
export interface Logger {
|
|
3
|
+
info: LogFn;
|
|
4
|
+
warn: LogFn;
|
|
5
|
+
error: LogFn;
|
|
6
|
+
comm: LogFn;
|
|
7
|
+
comm_recv: LogFn;
|
|
8
|
+
module: LogFn;
|
|
9
|
+
}
|
|
10
|
+
export declare function createLogger(isEnabled: boolean): Logger;
|
|
11
|
+
export {};
|
package/build/Logger.js
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __spreadArrays = (this && this.__spreadArrays) || function () {
|
|
3
|
-
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
|
|
4
|
-
for (var r = Array(s), k = 0, i = 0; i < il; i++)
|
|
5
|
-
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
|
|
6
|
-
r[k] = a[j];
|
|
7
|
-
return r;
|
|
8
|
-
};
|
|
9
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
exports.createLogger = void 0;
|
|
11
|
-
function logFn(isEnabled, type, color, textColor) {
|
|
12
|
-
return function () {
|
|
13
|
-
var messages = [];
|
|
14
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
15
|
-
messages[_i] = arguments[_i];
|
|
16
|
-
}
|
|
17
|
-
if (isEnabled) {
|
|
18
|
-
console.log.apply(console, __spreadArrays(["%c " + type.toUpperCase() + " ",
|
|
19
|
-
"background: " + color + "; color: " + (textColor !== null && textColor !== void 0 ? textColor : '#fff')], messages));
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
function createLogger(isEnabled) {
|
|
24
|
-
return {
|
|
25
|
-
info: logFn(isEnabled, 'info', '#2196f3'),
|
|
26
|
-
warn: logFn(isEnabled, 'warn', '#FF9800'),
|
|
27
|
-
error: logFn(isEnabled, 'error', '#f44336'),
|
|
28
|
-
comm: logFn(isEnabled, 'comm', '#DBD64E'),
|
|
29
|
-
comm_recv: logFn(isEnabled, 'comm', '#DBD64E', '#000'),
|
|
30
|
-
module: logFn(isEnabled, 'module', '#DB14C7'),
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
exports.createLogger = createLogger;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __spreadArrays = (this && this.__spreadArrays) || function () {
|
|
3
|
+
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
|
|
4
|
+
for (var r = Array(s), k = 0, i = 0; i < il; i++)
|
|
5
|
+
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
|
|
6
|
+
r[k] = a[j];
|
|
7
|
+
return r;
|
|
8
|
+
};
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.createLogger = void 0;
|
|
11
|
+
function logFn(isEnabled, type, color, textColor) {
|
|
12
|
+
return function () {
|
|
13
|
+
var messages = [];
|
|
14
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
15
|
+
messages[_i] = arguments[_i];
|
|
16
|
+
}
|
|
17
|
+
if (isEnabled) {
|
|
18
|
+
console.log.apply(console, __spreadArrays(["%c " + type.toUpperCase() + " ",
|
|
19
|
+
"background: " + color + "; color: " + (textColor !== null && textColor !== void 0 ? textColor : '#fff')], messages));
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function createLogger(isEnabled) {
|
|
24
|
+
return {
|
|
25
|
+
info: logFn(isEnabled, 'info', '#2196f3'),
|
|
26
|
+
warn: logFn(isEnabled, 'warn', '#FF9800'),
|
|
27
|
+
error: logFn(isEnabled, 'error', '#f44336'),
|
|
28
|
+
comm: logFn(isEnabled, 'comm', '#DBD64E'),
|
|
29
|
+
comm_recv: logFn(isEnabled, 'comm', '#DBD64E', '#000'),
|
|
30
|
+
module: logFn(isEnabled, 'module', '#DB14C7'),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
exports.createLogger = createLogger;
|
|
34
34
|
//# sourceMappingURL=Logger.js.map
|