@bytescale/sdk 3.58.0 → 3.60.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/dist/browser/cjs/main.js +730 -316
- package/dist/browser/esm/main.mjs +730 -316
- package/dist/node/cjs/main.js +123 -29
- package/dist/node/esm/main.mjs +123 -29
- package/dist/types/private/AuthSessionState.d.ts +7 -0
- package/dist/types/private/UploadManagerBase.d.ts +0 -1
- package/dist/types/private/dtos/AuthSwSetConfigDto.d.ts +1 -1
- package/dist/types/private/model/AuthManagerInterface.d.ts +1 -105
- package/dist/types/private/model/AuthSession.d.ts +12 -5
- package/dist/types/private/model/AuthSessionConfig.d.ts +3 -0
- package/dist/types/private/model/AuthSessionConfigAuto.d.ts +6 -0
- package/dist/types/private/model/AuthSessionConfigBase.d.ts +14 -0
- package/dist/types/private/model/AuthSessionConfigManual.d.ts +7 -0
- package/dist/types/private/model/BeginAuthSessionParams.d.ts +3 -0
- package/dist/types/private/model/BeginAuthSessionParamsOptions.d.ts +2 -0
- package/dist/types/private/model/BeginAuthSessionParamsV1.d.ts +11 -0
- package/dist/types/private/model/BeginAuthSessionParamsV2.d.ts +16 -0
- package/dist/types/private/model/NonEmptyArray.d.ts +1 -0
- package/dist/types/private/model/UrlRewriteRule.d.ts +6 -0
- package/dist/types/public/browser/AuthManagerBrowser.d.ts +28 -11
- package/dist/types/public/node/AuthManagerNode.d.ts +12 -2
- package/dist/types/public/shared/generated/runtime.d.ts +13 -6
- package/dist/worker/cjs/main.js +123 -29
- package/dist/worker/esm/main.mjs +123 -29
- package/package.json +1 -1
- package/tests/ApiClientAuth.test.ts +222 -0
- package/tests/AuthManagerBrowser.test.ts +360 -234
- package/tests/AuthServiceWorkerRewrite.test.ts +86 -5
- package/tests/UploadManagerAuth.test.ts +156 -0
package/dist/node/cjs/main.js
CHANGED
|
@@ -250,6 +250,55 @@ var AuthSessionState = /*#__PURE__*/function () {
|
|
|
250
250
|
}
|
|
251
251
|
return window[AuthSessionState.stateKey];
|
|
252
252
|
}
|
|
253
|
+
/** Resolves request-time auth while remaining compatible with the single-token session used by SDK 3.54.0. */
|
|
254
|
+
}, {
|
|
255
|
+
key: "resolveAuthConfig",
|
|
256
|
+
value: function resolveAuthConfig(authConfigId, requireReadyDefault) {
|
|
257
|
+
if (authConfigId === false) {
|
|
258
|
+
return undefined;
|
|
259
|
+
}
|
|
260
|
+
var session = AuthSessionState.getSession();
|
|
261
|
+
if (session === undefined || !session.isActive) {
|
|
262
|
+
if (typeof authConfigId === "string") {
|
|
263
|
+
throw new Error("No active AuthManager configuration has ID '".concat(authConfigId, "'."));
|
|
264
|
+
}
|
|
265
|
+
return undefined;
|
|
266
|
+
}
|
|
267
|
+
if (Array.isArray(session.authConfigs)) {
|
|
268
|
+
var state = session.authConfigs.find(function (config) {
|
|
269
|
+
return config.config.authConfigId === authConfigId;
|
|
270
|
+
});
|
|
271
|
+
if (state === undefined) {
|
|
272
|
+
if (typeof authConfigId === "string") {
|
|
273
|
+
throw new Error("No active AuthManager configuration has ID '".concat(authConfigId, "'."));
|
|
274
|
+
}
|
|
275
|
+
return undefined;
|
|
276
|
+
}
|
|
277
|
+
if (state.accessToken === undefined || state.expiresAt === undefined || state.expiresAt <= Date.now() || state.jwt === undefined) {
|
|
278
|
+
var isV2Session = typeof session.params.authConfigs === "function";
|
|
279
|
+
if (typeof authConfigId === "string" || requireReadyDefault || isV2Session) {
|
|
280
|
+
throw new Error("AuthManager configuration '".concat(authConfigId !== null && authConfigId !== void 0 ? authConfigId : "default", "' is not ready."));
|
|
281
|
+
}
|
|
282
|
+
return undefined;
|
|
283
|
+
}
|
|
284
|
+
return {
|
|
285
|
+
accessToken: state.accessToken,
|
|
286
|
+
accountId: state.config.accountId,
|
|
287
|
+
jwt: state.jwt
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
if (typeof authConfigId === "string") {
|
|
291
|
+
throw new Error("No active AuthManager configuration has ID '".concat(authConfigId, "'."));
|
|
292
|
+
}
|
|
293
|
+
if (session.accessToken === undefined || typeof session.params.accountId !== "string") {
|
|
294
|
+
return undefined;
|
|
295
|
+
}
|
|
296
|
+
return {
|
|
297
|
+
accessToken: session.accessToken,
|
|
298
|
+
accountId: session.params.accountId,
|
|
299
|
+
jwt: undefined
|
|
300
|
+
};
|
|
301
|
+
}
|
|
253
302
|
}]);
|
|
254
303
|
}();
|
|
255
304
|
AuthSessionState.stateKey = "BytescaleSessionState";
|
|
@@ -393,12 +442,47 @@ var BytescaleApiClientConfigUtils = /*#__PURE__*/function () {
|
|
|
393
442
|
}, {
|
|
394
443
|
key: "getAccountId",
|
|
395
444
|
value: function getAccountId(config) {
|
|
445
|
+
return BytescaleApiClientConfigUtils.resolveAuthentication(config).accountId;
|
|
446
|
+
}
|
|
447
|
+
}, {
|
|
448
|
+
key: "resolveAuthentication",
|
|
449
|
+
value: function resolveAuthentication(config) {
|
|
450
|
+
var _a, _b;
|
|
451
|
+
var apiKey = (_a = config.apiKey) !== null && _a !== void 0 ? _a : undefined;
|
|
452
|
+
var authConfig = AuthSessionState.resolveAuthConfig(config.authConfigId, apiKey === undefined);
|
|
453
|
+
var apiKeyAccountId = apiKey === undefined ? undefined : BytescaleApiClientConfigUtils.getApiKeyAccountId(apiKey);
|
|
454
|
+
if (apiKeyAccountId !== undefined && authConfig !== undefined && apiKeyAccountId !== authConfig.accountId) {
|
|
455
|
+
throw new Error("The API key belongs to account '".concat(apiKeyAccountId, "', but AuthManager configuration '").concat((_b = config.authConfigId) !== null && _b !== void 0 ? _b : "default", "' belongs to account '").concat(authConfig.accountId, "'."));
|
|
456
|
+
}
|
|
457
|
+
if (apiKey !== undefined) {
|
|
458
|
+
return {
|
|
459
|
+
accountId: apiKeyAccountId,
|
|
460
|
+
headers: Object.assign({
|
|
461
|
+
Authorization: "Bearer ".concat(apiKey)
|
|
462
|
+
}, authConfig === undefined ? {} : {
|
|
463
|
+
"Authorization-Token": authConfig.accessToken
|
|
464
|
+
})
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
if ((authConfig === null || authConfig === void 0 ? void 0 : authConfig.jwt) !== undefined) {
|
|
468
|
+
return {
|
|
469
|
+
accountId: authConfig.accountId,
|
|
470
|
+
headers: {
|
|
471
|
+
Authorization: "Bearer ".concat(authConfig.jwt)
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
throw new Error("Please provide an API key via the 'apiKey' config parameter.");
|
|
476
|
+
}
|
|
477
|
+
}, {
|
|
478
|
+
key: "getApiKeyAccountId",
|
|
479
|
+
value: function getApiKeyAccountId(apiKey) {
|
|
396
480
|
var _a, _b;
|
|
397
481
|
var accountId;
|
|
398
|
-
if (BytescaleApiClientConfigUtils.specialApiKeys.includes(
|
|
482
|
+
if (BytescaleApiClientConfigUtils.specialApiKeys.includes(apiKey)) {
|
|
399
483
|
accountId = BytescaleApiClientConfigUtils.specialApiKeyAccountId;
|
|
400
484
|
} else {
|
|
401
|
-
accountId = (_b = (_a =
|
|
485
|
+
accountId = (_b = (_a = apiKey.split("_")[1]) === null || _a === void 0 ? void 0 : _a.substr(0, BytescaleApiClientConfigUtils.accountIdLength)) !== null && _b !== void 0 ? _b : "";
|
|
402
486
|
if (accountId.length !== BytescaleApiClientConfigUtils.accountIdLength) {
|
|
403
487
|
throw new Error("Invalid Bytescale API key.");
|
|
404
488
|
}
|
|
@@ -408,21 +492,24 @@ var BytescaleApiClientConfigUtils = /*#__PURE__*/function () {
|
|
|
408
492
|
}, {
|
|
409
493
|
key: "validate",
|
|
410
494
|
value: function validate(config) {
|
|
411
|
-
var _a;
|
|
412
495
|
// Defensive programming, for users not using TypeScript. Mainly because this is used by UploadWidget users.
|
|
413
496
|
if ((config !== null && config !== void 0 ? config : undefined) === undefined) {
|
|
414
497
|
throw new Error("Config parameter required.");
|
|
415
498
|
}
|
|
416
|
-
if (
|
|
417
|
-
throw new Error("
|
|
499
|
+
if (config.authConfigId !== undefined && config.authConfigId !== false && typeof config.authConfigId !== "string") {
|
|
500
|
+
throw new Error("The 'authConfigId' config parameter must be a string, false, or undefined.");
|
|
418
501
|
}
|
|
419
|
-
if (config.apiKey
|
|
502
|
+
if (config.apiKey !== undefined && typeof config.apiKey !== "string") {
|
|
503
|
+
throw new Error("The 'apiKey' config parameter must be a string when provided.");
|
|
504
|
+
}
|
|
505
|
+
if (config.apiKey !== undefined && config.apiKey.trim() !== config.apiKey) {
|
|
420
506
|
// We do not support API keys with whitespace (by trimming ourselves) because otherwise we'd need to support this
|
|
421
507
|
// everywhere in perpetuity (since removing the trimming would be a breaking change).
|
|
422
508
|
throw new Error("API key needs trimming (whitespace detected).");
|
|
423
509
|
}
|
|
424
|
-
|
|
425
|
-
|
|
510
|
+
if (config.apiKey !== undefined) {
|
|
511
|
+
BytescaleApiClientConfigUtils.getApiKeyAccountId(config.apiKey);
|
|
512
|
+
}
|
|
426
513
|
}
|
|
427
514
|
}]);
|
|
428
515
|
}();
|
|
@@ -449,12 +536,7 @@ var BaseAPI = /*#__PURE__*/function () {
|
|
|
449
536
|
try {
|
|
450
537
|
var _this = this;
|
|
451
538
|
var _a;
|
|
452
|
-
|
|
453
|
-
context.headers["Authorization"] = "Bearer ".concat(apiKey); // authorization-header authentication
|
|
454
|
-
var session = AuthSessionState.getSession();
|
|
455
|
-
if ((session === null || session === void 0 ? void 0 : session.accessToken) !== undefined) {
|
|
456
|
-
context.headers["Authorization-Token"] = session.accessToken;
|
|
457
|
-
}
|
|
539
|
+
Object.assign(context.headers, BytescaleApiClientConfigUtils.resolveAuthentication(_this.config).headers);
|
|
458
540
|
// Key: any possible value for 'baseUrlOverride'
|
|
459
541
|
// Value: user-overridden value for that base URL from the config.
|
|
460
542
|
var nonDefaultBasePaths = _defineProperty({}, BytescaleApiClientConfigUtils.defaultCdnUrl, BytescaleApiClientConfigUtils.getCdnUrl(_this.config));
|
|
@@ -494,9 +576,21 @@ var BaseAPI = /*#__PURE__*/function () {
|
|
|
494
576
|
url += "?" + querystring(context.query);
|
|
495
577
|
}
|
|
496
578
|
var configHeaders = _this2.config.headers;
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
579
|
+
return runtime_await(runtime_await(configHeaders === undefined ? {} : typeof configHeaders === "function" ? configHeaders() : configHeaders, function (resolvedConfigHeaders) {
|
|
580
|
+
for (var _i = 0, _Object$keys = Object.keys(resolvedConfigHeaders); _i < _Object$keys.length; _i++) {
|
|
581
|
+
var key = _Object$keys[_i];
|
|
582
|
+
if (key.toLowerCase() === "authorization" || key.toLowerCase() === "authorization-token") {
|
|
583
|
+
for (var _i2 = 0, _Object$keys2 = Object.keys(context.headers); _i2 < _Object$keys2.length; _i2++) {
|
|
584
|
+
var generatedKey = _Object$keys2[_i2];
|
|
585
|
+
if (generatedKey.toLowerCase() === key.toLowerCase()) {
|
|
586
|
+
// Custom auth headers have documented precedence; remove the generated spelling to make that deterministic.
|
|
587
|
+
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
|
588
|
+
delete context.headers[generatedKey];
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
var headers = Object.assign(Object.assign({}, context.headers), resolvedConfigHeaders);
|
|
500
594
|
Object.keys(headers).forEach(function (key) {
|
|
501
595
|
return headers[key] === undefined ? delete headers[key] : {};
|
|
502
596
|
});
|
|
@@ -508,12 +602,12 @@ var BaseAPI = /*#__PURE__*/function () {
|
|
|
508
602
|
headers: headers,
|
|
509
603
|
body: context.body
|
|
510
604
|
};
|
|
511
|
-
var _Object$
|
|
605
|
+
var _Object$assign = Object.assign({}, initParams);
|
|
512
606
|
return runtime_await(initOverrideFn({
|
|
513
607
|
init: initParams,
|
|
514
608
|
context: context
|
|
515
609
|
}), function (_initOverrideFn) {
|
|
516
|
-
var overriddenInit = Object.assign(_Object$
|
|
610
|
+
var overriddenInit = Object.assign(_Object$assign, _initOverrideFn);
|
|
517
611
|
var init = Object.assign(Object.assign({}, overriddenInit), {
|
|
518
612
|
body: JSON.stringify(overriddenInit.body)
|
|
519
613
|
});
|
|
@@ -2138,7 +2232,6 @@ var UploadManagerBase = /*#__PURE__*/function () {
|
|
|
2138
2232
|
this.defaultMaxConcurrentUploadParts = 4;
|
|
2139
2233
|
this.intervalMs = 500;
|
|
2140
2234
|
this.uploadApi = new UploadApi(config);
|
|
2141
|
-
this.accountId = BytescaleApiClientConfigUtils.getAccountId(config);
|
|
2142
2235
|
}
|
|
2143
2236
|
return UploadManagerBase_createClass(UploadManagerBase, [{
|
|
2144
2237
|
key: "upload",
|
|
@@ -2146,6 +2239,7 @@ var UploadManagerBase = /*#__PURE__*/function () {
|
|
|
2146
2239
|
try {
|
|
2147
2240
|
var _this = this;
|
|
2148
2241
|
_this.assertNotCancelled(request);
|
|
2242
|
+
var accountId = BytescaleApiClientConfigUtils.getAccountId(_this.config);
|
|
2149
2243
|
var source = _this.processUploadSource(request.data);
|
|
2150
2244
|
var preUploadInfo = _this.getPreUploadInfo(request, source);
|
|
2151
2245
|
var bytesTotal = preUploadInfo.size;
|
|
@@ -2155,7 +2249,7 @@ var UploadManagerBase = /*#__PURE__*/function () {
|
|
|
2155
2249
|
if (request.onProgress !== undefined) {
|
|
2156
2250
|
request.onProgress(_this.makeProgressEvent(0, bytesTotal));
|
|
2157
2251
|
}
|
|
2158
|
-
return UploadManagerBase_await(_this.beginUpload(request, preUploadInfo), function (uploadInfo) {
|
|
2252
|
+
return UploadManagerBase_await(_this.beginUpload(request, preUploadInfo, accountId), function (uploadInfo) {
|
|
2159
2253
|
var partCount = uploadInfo.uploadParts.count;
|
|
2160
2254
|
var parts = UploadManagerBase_toConsumableArray(Array(partCount).keys());
|
|
2161
2255
|
var _this$makeCancellatio = _this.makeCancellationMethods(),
|
|
@@ -2165,7 +2259,7 @@ var UploadManagerBase = /*#__PURE__*/function () {
|
|
|
2165
2259
|
var uploadedParts;
|
|
2166
2260
|
return UploadManagerBase_continue(UploadManagerBase_finallyRethrows(function () {
|
|
2167
2261
|
return UploadManagerBase_await(_this.mapAsync(parts, preUploadInfo.maxConcurrentUploadParts, _async(function (part) {
|
|
2168
|
-
return _this.uploadPart(request, source, part, uploadInfo, makeOnProgressForPart(), addCancellationHandler);
|
|
2262
|
+
return _this.uploadPart(request, source, part, uploadInfo, makeOnProgressForPart(), addCancellationHandler, accountId);
|
|
2169
2263
|
})), function (_this$mapAsync) {
|
|
2170
2264
|
uploadedParts = _this$mapAsync;
|
|
2171
2265
|
return UploadManagerBase_awaitIgnored(_this.postUpload(init));
|
|
@@ -2277,14 +2371,14 @@ var UploadManagerBase = /*#__PURE__*/function () {
|
|
|
2277
2371
|
}
|
|
2278
2372
|
}, {
|
|
2279
2373
|
key: "beginUpload",
|
|
2280
|
-
value: function beginUpload(request, _ref2) {
|
|
2374
|
+
value: function beginUpload(request, _ref2, accountId) {
|
|
2281
2375
|
var size = _ref2.size,
|
|
2282
2376
|
mime = _ref2.mime,
|
|
2283
2377
|
originalFileName = _ref2.originalFileName;
|
|
2284
2378
|
try {
|
|
2285
2379
|
var _this4 = this;
|
|
2286
2380
|
return UploadManagerBase_await(_this4.uploadApi.beginMultipartUpload({
|
|
2287
|
-
accountId:
|
|
2381
|
+
accountId: accountId,
|
|
2288
2382
|
beginMultipartUploadRequest: {
|
|
2289
2383
|
metadata: request.metadata,
|
|
2290
2384
|
mime: mime,
|
|
@@ -2301,16 +2395,16 @@ var UploadManagerBase = /*#__PURE__*/function () {
|
|
|
2301
2395
|
}
|
|
2302
2396
|
}, {
|
|
2303
2397
|
key: "uploadPart",
|
|
2304
|
-
value: function uploadPart(request, source, partIndex, uploadInfo, onProgress, addCancellationHandler) {
|
|
2398
|
+
value: function uploadPart(request, source, partIndex, uploadInfo, onProgress, addCancellationHandler, accountId) {
|
|
2305
2399
|
try {
|
|
2306
2400
|
var _this5 = this;
|
|
2307
2401
|
_this5.assertNotCancelled(request);
|
|
2308
|
-
return UploadManagerBase_await(_this5.getUploadPart(partIndex, uploadInfo), function (part) {
|
|
2402
|
+
return UploadManagerBase_await(_this5.getUploadPart(partIndex, uploadInfo, accountId), function (part) {
|
|
2309
2403
|
_this5.assertNotCancelled(request);
|
|
2310
2404
|
return UploadManagerBase_await(_this5.putUploadPart(part, source, onProgress, addCancellationHandler), function (etag) {
|
|
2311
2405
|
_this5.assertNotCancelled(request);
|
|
2312
2406
|
return UploadManagerBase_await(_this5.uploadApi.completeUploadPart({
|
|
2313
|
-
accountId:
|
|
2407
|
+
accountId: accountId,
|
|
2314
2408
|
uploadId: uploadInfo.uploadId,
|
|
2315
2409
|
uploadPartIndex: partIndex,
|
|
2316
2410
|
completeUploadPartRequest: {
|
|
@@ -2351,7 +2445,7 @@ var UploadManagerBase = /*#__PURE__*/function () {
|
|
|
2351
2445
|
}
|
|
2352
2446
|
}, {
|
|
2353
2447
|
key: "getUploadPart",
|
|
2354
|
-
value: function getUploadPart(partIndex, uploadInfo) {
|
|
2448
|
+
value: function getUploadPart(partIndex, uploadInfo, accountId) {
|
|
2355
2449
|
try {
|
|
2356
2450
|
var _this7 = this;
|
|
2357
2451
|
if (partIndex === 0) {
|
|
@@ -2359,7 +2453,7 @@ var UploadManagerBase = /*#__PURE__*/function () {
|
|
|
2359
2453
|
}
|
|
2360
2454
|
return UploadManagerBase_await(_this7.uploadApi.getUploadPart({
|
|
2361
2455
|
uploadId: uploadInfo.uploadId,
|
|
2362
|
-
accountId:
|
|
2456
|
+
accountId: accountId,
|
|
2363
2457
|
uploadPartIndex: partIndex
|
|
2364
2458
|
}));
|
|
2365
2459
|
} catch (e) {
|
package/dist/node/esm/main.mjs
CHANGED
|
@@ -212,6 +212,55 @@ var AuthSessionState = /*#__PURE__*/function () {
|
|
|
212
212
|
}
|
|
213
213
|
return window[AuthSessionState.stateKey];
|
|
214
214
|
}
|
|
215
|
+
/** Resolves request-time auth while remaining compatible with the single-token session used by SDK 3.54.0. */
|
|
216
|
+
}, {
|
|
217
|
+
key: "resolveAuthConfig",
|
|
218
|
+
value: function resolveAuthConfig(authConfigId, requireReadyDefault) {
|
|
219
|
+
if (authConfigId === false) {
|
|
220
|
+
return undefined;
|
|
221
|
+
}
|
|
222
|
+
var session = AuthSessionState.getSession();
|
|
223
|
+
if (session === undefined || !session.isActive) {
|
|
224
|
+
if (typeof authConfigId === "string") {
|
|
225
|
+
throw new Error("No active AuthManager configuration has ID '".concat(authConfigId, "'."));
|
|
226
|
+
}
|
|
227
|
+
return undefined;
|
|
228
|
+
}
|
|
229
|
+
if (Array.isArray(session.authConfigs)) {
|
|
230
|
+
var state = session.authConfigs.find(function (config) {
|
|
231
|
+
return config.config.authConfigId === authConfigId;
|
|
232
|
+
});
|
|
233
|
+
if (state === undefined) {
|
|
234
|
+
if (typeof authConfigId === "string") {
|
|
235
|
+
throw new Error("No active AuthManager configuration has ID '".concat(authConfigId, "'."));
|
|
236
|
+
}
|
|
237
|
+
return undefined;
|
|
238
|
+
}
|
|
239
|
+
if (state.accessToken === undefined || state.expiresAt === undefined || state.expiresAt <= Date.now() || state.jwt === undefined) {
|
|
240
|
+
var isV2Session = typeof session.params.authConfigs === "function";
|
|
241
|
+
if (typeof authConfigId === "string" || requireReadyDefault || isV2Session) {
|
|
242
|
+
throw new Error("AuthManager configuration '".concat(authConfigId !== null && authConfigId !== void 0 ? authConfigId : "default", "' is not ready."));
|
|
243
|
+
}
|
|
244
|
+
return undefined;
|
|
245
|
+
}
|
|
246
|
+
return {
|
|
247
|
+
accessToken: state.accessToken,
|
|
248
|
+
accountId: state.config.accountId,
|
|
249
|
+
jwt: state.jwt
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
if (typeof authConfigId === "string") {
|
|
253
|
+
throw new Error("No active AuthManager configuration has ID '".concat(authConfigId, "'."));
|
|
254
|
+
}
|
|
255
|
+
if (session.accessToken === undefined || typeof session.params.accountId !== "string") {
|
|
256
|
+
return undefined;
|
|
257
|
+
}
|
|
258
|
+
return {
|
|
259
|
+
accessToken: session.accessToken,
|
|
260
|
+
accountId: session.params.accountId,
|
|
261
|
+
jwt: undefined
|
|
262
|
+
};
|
|
263
|
+
}
|
|
215
264
|
}]);
|
|
216
265
|
}();
|
|
217
266
|
AuthSessionState.stateKey = "BytescaleSessionState";
|
|
@@ -355,12 +404,47 @@ var BytescaleApiClientConfigUtils = /*#__PURE__*/function () {
|
|
|
355
404
|
}, {
|
|
356
405
|
key: "getAccountId",
|
|
357
406
|
value: function getAccountId(config) {
|
|
407
|
+
return BytescaleApiClientConfigUtils.resolveAuthentication(config).accountId;
|
|
408
|
+
}
|
|
409
|
+
}, {
|
|
410
|
+
key: "resolveAuthentication",
|
|
411
|
+
value: function resolveAuthentication(config) {
|
|
412
|
+
var _a, _b;
|
|
413
|
+
var apiKey = (_a = config.apiKey) !== null && _a !== void 0 ? _a : undefined;
|
|
414
|
+
var authConfig = AuthSessionState.resolveAuthConfig(config.authConfigId, apiKey === undefined);
|
|
415
|
+
var apiKeyAccountId = apiKey === undefined ? undefined : BytescaleApiClientConfigUtils.getApiKeyAccountId(apiKey);
|
|
416
|
+
if (apiKeyAccountId !== undefined && authConfig !== undefined && apiKeyAccountId !== authConfig.accountId) {
|
|
417
|
+
throw new Error("The API key belongs to account '".concat(apiKeyAccountId, "', but AuthManager configuration '").concat((_b = config.authConfigId) !== null && _b !== void 0 ? _b : "default", "' belongs to account '").concat(authConfig.accountId, "'."));
|
|
418
|
+
}
|
|
419
|
+
if (apiKey !== undefined) {
|
|
420
|
+
return {
|
|
421
|
+
accountId: apiKeyAccountId,
|
|
422
|
+
headers: Object.assign({
|
|
423
|
+
Authorization: "Bearer ".concat(apiKey)
|
|
424
|
+
}, authConfig === undefined ? {} : {
|
|
425
|
+
"Authorization-Token": authConfig.accessToken
|
|
426
|
+
})
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
if ((authConfig === null || authConfig === void 0 ? void 0 : authConfig.jwt) !== undefined) {
|
|
430
|
+
return {
|
|
431
|
+
accountId: authConfig.accountId,
|
|
432
|
+
headers: {
|
|
433
|
+
Authorization: "Bearer ".concat(authConfig.jwt)
|
|
434
|
+
}
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
throw new Error("Please provide an API key via the 'apiKey' config parameter.");
|
|
438
|
+
}
|
|
439
|
+
}, {
|
|
440
|
+
key: "getApiKeyAccountId",
|
|
441
|
+
value: function getApiKeyAccountId(apiKey) {
|
|
358
442
|
var _a, _b;
|
|
359
443
|
var accountId;
|
|
360
|
-
if (BytescaleApiClientConfigUtils.specialApiKeys.includes(
|
|
444
|
+
if (BytescaleApiClientConfigUtils.specialApiKeys.includes(apiKey)) {
|
|
361
445
|
accountId = BytescaleApiClientConfigUtils.specialApiKeyAccountId;
|
|
362
446
|
} else {
|
|
363
|
-
accountId = (_b = (_a =
|
|
447
|
+
accountId = (_b = (_a = apiKey.split("_")[1]) === null || _a === void 0 ? void 0 : _a.substr(0, BytescaleApiClientConfigUtils.accountIdLength)) !== null && _b !== void 0 ? _b : "";
|
|
364
448
|
if (accountId.length !== BytescaleApiClientConfigUtils.accountIdLength) {
|
|
365
449
|
throw new Error("Invalid Bytescale API key.");
|
|
366
450
|
}
|
|
@@ -370,21 +454,24 @@ var BytescaleApiClientConfigUtils = /*#__PURE__*/function () {
|
|
|
370
454
|
}, {
|
|
371
455
|
key: "validate",
|
|
372
456
|
value: function validate(config) {
|
|
373
|
-
var _a;
|
|
374
457
|
// Defensive programming, for users not using TypeScript. Mainly because this is used by UploadWidget users.
|
|
375
458
|
if ((config !== null && config !== void 0 ? config : undefined) === undefined) {
|
|
376
459
|
throw new Error("Config parameter required.");
|
|
377
460
|
}
|
|
378
|
-
if (
|
|
379
|
-
throw new Error("
|
|
461
|
+
if (config.authConfigId !== undefined && config.authConfigId !== false && typeof config.authConfigId !== "string") {
|
|
462
|
+
throw new Error("The 'authConfigId' config parameter must be a string, false, or undefined.");
|
|
380
463
|
}
|
|
381
|
-
if (config.apiKey
|
|
464
|
+
if (config.apiKey !== undefined && typeof config.apiKey !== "string") {
|
|
465
|
+
throw new Error("The 'apiKey' config parameter must be a string when provided.");
|
|
466
|
+
}
|
|
467
|
+
if (config.apiKey !== undefined && config.apiKey.trim() !== config.apiKey) {
|
|
382
468
|
// We do not support API keys with whitespace (by trimming ourselves) because otherwise we'd need to support this
|
|
383
469
|
// everywhere in perpetuity (since removing the trimming would be a breaking change).
|
|
384
470
|
throw new Error("API key needs trimming (whitespace detected).");
|
|
385
471
|
}
|
|
386
|
-
|
|
387
|
-
|
|
472
|
+
if (config.apiKey !== undefined) {
|
|
473
|
+
BytescaleApiClientConfigUtils.getApiKeyAccountId(config.apiKey);
|
|
474
|
+
}
|
|
388
475
|
}
|
|
389
476
|
}]);
|
|
390
477
|
}();
|
|
@@ -411,12 +498,7 @@ var BaseAPI = /*#__PURE__*/function () {
|
|
|
411
498
|
try {
|
|
412
499
|
var _this = this;
|
|
413
500
|
var _a;
|
|
414
|
-
|
|
415
|
-
context.headers["Authorization"] = "Bearer ".concat(apiKey); // authorization-header authentication
|
|
416
|
-
var session = AuthSessionState.getSession();
|
|
417
|
-
if ((session === null || session === void 0 ? void 0 : session.accessToken) !== undefined) {
|
|
418
|
-
context.headers["Authorization-Token"] = session.accessToken;
|
|
419
|
-
}
|
|
501
|
+
Object.assign(context.headers, BytescaleApiClientConfigUtils.resolveAuthentication(_this.config).headers);
|
|
420
502
|
// Key: any possible value for 'baseUrlOverride'
|
|
421
503
|
// Value: user-overridden value for that base URL from the config.
|
|
422
504
|
var nonDefaultBasePaths = _defineProperty({}, BytescaleApiClientConfigUtils.defaultCdnUrl, BytescaleApiClientConfigUtils.getCdnUrl(_this.config));
|
|
@@ -456,9 +538,21 @@ var BaseAPI = /*#__PURE__*/function () {
|
|
|
456
538
|
url += "?" + querystring(context.query);
|
|
457
539
|
}
|
|
458
540
|
var configHeaders = _this2.config.headers;
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
541
|
+
return runtime_await(runtime_await(configHeaders === undefined ? {} : typeof configHeaders === "function" ? configHeaders() : configHeaders, function (resolvedConfigHeaders) {
|
|
542
|
+
for (var _i = 0, _Object$keys = Object.keys(resolvedConfigHeaders); _i < _Object$keys.length; _i++) {
|
|
543
|
+
var key = _Object$keys[_i];
|
|
544
|
+
if (key.toLowerCase() === "authorization" || key.toLowerCase() === "authorization-token") {
|
|
545
|
+
for (var _i2 = 0, _Object$keys2 = Object.keys(context.headers); _i2 < _Object$keys2.length; _i2++) {
|
|
546
|
+
var generatedKey = _Object$keys2[_i2];
|
|
547
|
+
if (generatedKey.toLowerCase() === key.toLowerCase()) {
|
|
548
|
+
// Custom auth headers have documented precedence; remove the generated spelling to make that deterministic.
|
|
549
|
+
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
|
550
|
+
delete context.headers[generatedKey];
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
var headers = Object.assign(Object.assign({}, context.headers), resolvedConfigHeaders);
|
|
462
556
|
Object.keys(headers).forEach(function (key) {
|
|
463
557
|
return headers[key] === undefined ? delete headers[key] : {};
|
|
464
558
|
});
|
|
@@ -470,12 +564,12 @@ var BaseAPI = /*#__PURE__*/function () {
|
|
|
470
564
|
headers: headers,
|
|
471
565
|
body: context.body
|
|
472
566
|
};
|
|
473
|
-
var _Object$
|
|
567
|
+
var _Object$assign = Object.assign({}, initParams);
|
|
474
568
|
return runtime_await(initOverrideFn({
|
|
475
569
|
init: initParams,
|
|
476
570
|
context: context
|
|
477
571
|
}), function (_initOverrideFn) {
|
|
478
|
-
var overriddenInit = Object.assign(_Object$
|
|
572
|
+
var overriddenInit = Object.assign(_Object$assign, _initOverrideFn);
|
|
479
573
|
var init = Object.assign(Object.assign({}, overriddenInit), {
|
|
480
574
|
body: JSON.stringify(overriddenInit.body)
|
|
481
575
|
});
|
|
@@ -2100,7 +2194,6 @@ var UploadManagerBase = /*#__PURE__*/function () {
|
|
|
2100
2194
|
this.defaultMaxConcurrentUploadParts = 4;
|
|
2101
2195
|
this.intervalMs = 500;
|
|
2102
2196
|
this.uploadApi = new UploadApi(config);
|
|
2103
|
-
this.accountId = BytescaleApiClientConfigUtils.getAccountId(config);
|
|
2104
2197
|
}
|
|
2105
2198
|
return UploadManagerBase_createClass(UploadManagerBase, [{
|
|
2106
2199
|
key: "upload",
|
|
@@ -2108,6 +2201,7 @@ var UploadManagerBase = /*#__PURE__*/function () {
|
|
|
2108
2201
|
try {
|
|
2109
2202
|
var _this = this;
|
|
2110
2203
|
_this.assertNotCancelled(request);
|
|
2204
|
+
var accountId = BytescaleApiClientConfigUtils.getAccountId(_this.config);
|
|
2111
2205
|
var source = _this.processUploadSource(request.data);
|
|
2112
2206
|
var preUploadInfo = _this.getPreUploadInfo(request, source);
|
|
2113
2207
|
var bytesTotal = preUploadInfo.size;
|
|
@@ -2117,7 +2211,7 @@ var UploadManagerBase = /*#__PURE__*/function () {
|
|
|
2117
2211
|
if (request.onProgress !== undefined) {
|
|
2118
2212
|
request.onProgress(_this.makeProgressEvent(0, bytesTotal));
|
|
2119
2213
|
}
|
|
2120
|
-
return UploadManagerBase_await(_this.beginUpload(request, preUploadInfo), function (uploadInfo) {
|
|
2214
|
+
return UploadManagerBase_await(_this.beginUpload(request, preUploadInfo, accountId), function (uploadInfo) {
|
|
2121
2215
|
var partCount = uploadInfo.uploadParts.count;
|
|
2122
2216
|
var parts = UploadManagerBase_toConsumableArray(Array(partCount).keys());
|
|
2123
2217
|
var _this$makeCancellatio = _this.makeCancellationMethods(),
|
|
@@ -2127,7 +2221,7 @@ var UploadManagerBase = /*#__PURE__*/function () {
|
|
|
2127
2221
|
var uploadedParts;
|
|
2128
2222
|
return UploadManagerBase_continue(UploadManagerBase_finallyRethrows(function () {
|
|
2129
2223
|
return UploadManagerBase_await(_this.mapAsync(parts, preUploadInfo.maxConcurrentUploadParts, _async(function (part) {
|
|
2130
|
-
return _this.uploadPart(request, source, part, uploadInfo, makeOnProgressForPart(), addCancellationHandler);
|
|
2224
|
+
return _this.uploadPart(request, source, part, uploadInfo, makeOnProgressForPart(), addCancellationHandler, accountId);
|
|
2131
2225
|
})), function (_this$mapAsync) {
|
|
2132
2226
|
uploadedParts = _this$mapAsync;
|
|
2133
2227
|
return UploadManagerBase_awaitIgnored(_this.postUpload(init));
|
|
@@ -2239,14 +2333,14 @@ var UploadManagerBase = /*#__PURE__*/function () {
|
|
|
2239
2333
|
}
|
|
2240
2334
|
}, {
|
|
2241
2335
|
key: "beginUpload",
|
|
2242
|
-
value: function beginUpload(request, _ref2) {
|
|
2336
|
+
value: function beginUpload(request, _ref2, accountId) {
|
|
2243
2337
|
var size = _ref2.size,
|
|
2244
2338
|
mime = _ref2.mime,
|
|
2245
2339
|
originalFileName = _ref2.originalFileName;
|
|
2246
2340
|
try {
|
|
2247
2341
|
var _this4 = this;
|
|
2248
2342
|
return UploadManagerBase_await(_this4.uploadApi.beginMultipartUpload({
|
|
2249
|
-
accountId:
|
|
2343
|
+
accountId: accountId,
|
|
2250
2344
|
beginMultipartUploadRequest: {
|
|
2251
2345
|
metadata: request.metadata,
|
|
2252
2346
|
mime: mime,
|
|
@@ -2263,16 +2357,16 @@ var UploadManagerBase = /*#__PURE__*/function () {
|
|
|
2263
2357
|
}
|
|
2264
2358
|
}, {
|
|
2265
2359
|
key: "uploadPart",
|
|
2266
|
-
value: function uploadPart(request, source, partIndex, uploadInfo, onProgress, addCancellationHandler) {
|
|
2360
|
+
value: function uploadPart(request, source, partIndex, uploadInfo, onProgress, addCancellationHandler, accountId) {
|
|
2267
2361
|
try {
|
|
2268
2362
|
var _this5 = this;
|
|
2269
2363
|
_this5.assertNotCancelled(request);
|
|
2270
|
-
return UploadManagerBase_await(_this5.getUploadPart(partIndex, uploadInfo), function (part) {
|
|
2364
|
+
return UploadManagerBase_await(_this5.getUploadPart(partIndex, uploadInfo, accountId), function (part) {
|
|
2271
2365
|
_this5.assertNotCancelled(request);
|
|
2272
2366
|
return UploadManagerBase_await(_this5.putUploadPart(part, source, onProgress, addCancellationHandler), function (etag) {
|
|
2273
2367
|
_this5.assertNotCancelled(request);
|
|
2274
2368
|
return UploadManagerBase_await(_this5.uploadApi.completeUploadPart({
|
|
2275
|
-
accountId:
|
|
2369
|
+
accountId: accountId,
|
|
2276
2370
|
uploadId: uploadInfo.uploadId,
|
|
2277
2371
|
uploadPartIndex: partIndex,
|
|
2278
2372
|
completeUploadPartRequest: {
|
|
@@ -2313,7 +2407,7 @@ var UploadManagerBase = /*#__PURE__*/function () {
|
|
|
2313
2407
|
}
|
|
2314
2408
|
}, {
|
|
2315
2409
|
key: "getUploadPart",
|
|
2316
|
-
value: function getUploadPart(partIndex, uploadInfo) {
|
|
2410
|
+
value: function getUploadPart(partIndex, uploadInfo, accountId) {
|
|
2317
2411
|
try {
|
|
2318
2412
|
var _this7 = this;
|
|
2319
2413
|
if (partIndex === 0) {
|
|
@@ -2321,7 +2415,7 @@ var UploadManagerBase = /*#__PURE__*/function () {
|
|
|
2321
2415
|
}
|
|
2322
2416
|
return UploadManagerBase_await(_this7.uploadApi.getUploadPart({
|
|
2323
2417
|
uploadId: uploadInfo.uploadId,
|
|
2324
|
-
accountId:
|
|
2418
|
+
accountId: accountId,
|
|
2325
2419
|
uploadPartIndex: partIndex
|
|
2326
2420
|
}));
|
|
2327
2421
|
} catch (e) {
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { AuthSession } from "./model/AuthSession";
|
|
2
2
|
import { FairMutex } from "./FairMutex";
|
|
3
|
+
export interface ResolvedAuthSessionConfig {
|
|
4
|
+
accessToken: string;
|
|
5
|
+
accountId: string;
|
|
6
|
+
jwt: string | undefined;
|
|
7
|
+
}
|
|
3
8
|
/**
|
|
4
9
|
* Maintains a global session state, even across package versions.
|
|
5
10
|
*
|
|
@@ -27,4 +32,6 @@ export declare class AuthSessionState {
|
|
|
27
32
|
* Called in the browser and in Node.js (so we check the env before calling env-specific code).
|
|
28
33
|
*/
|
|
29
34
|
static getSession(): AuthSession | undefined;
|
|
35
|
+
/** Resolves request-time auth while remaining compatible with the single-token session used by SDK 3.54.0. */
|
|
36
|
+
static resolveAuthConfig(authConfigId: string | false | undefined, requireReadyDefault: boolean): ResolvedAuthSessionConfig | undefined;
|
|
30
37
|
}
|
|
@@ -11,7 +11,6 @@ import { UploadManagerParams, UploadSource, UploadResult } from "../public/share
|
|
|
11
11
|
export declare abstract class UploadManagerBase<TSource, TInit> implements UploadManagerInterface {
|
|
12
12
|
protected readonly config: BytescaleApiClientConfig;
|
|
13
13
|
protected readonly stringMimeType = "text/plain";
|
|
14
|
-
private readonly accountId;
|
|
15
14
|
private readonly defaultMaxConcurrentUploadParts;
|
|
16
15
|
private readonly intervalMs;
|
|
17
16
|
private readonly uploadApi;
|