@bytescale/sdk 3.57.0 → 3.59.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 +724 -313
- package/dist/browser/esm/main.mjs +724 -313
- 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 +2 -0
- package/dist/types/private/model/AuthManagerInterface.d.ts +1 -93
- 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 +12 -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 +27 -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 +312 -192
- package/tests/AuthServiceWorkerRewrite.test.ts +337 -0
- package/tests/UploadManagerAuth.test.ts +156 -0
|
@@ -1,96 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { AuthSwConfigEntryDto } from "../dtos/AuthSwConfigEntryDto";
|
|
3
|
-
export interface AuthManagerServiceWorkerConfig {
|
|
4
|
-
/**
|
|
5
|
-
* Additional download-only service-worker authorization rules.
|
|
6
|
-
* An empty array clears all additional rules.
|
|
7
|
-
*/
|
|
8
|
-
additionalConfig: AuthSwConfigEntryDto[];
|
|
9
|
-
/**
|
|
10
|
-
* Restricts the primary JWT to pages and iframes whose URL starts with one of these prefixes.
|
|
11
|
-
* An empty array matches no clients. Omit this field to preserve the existing service-worker behavior.
|
|
12
|
-
* History API changes do not update the matched client URL.
|
|
13
|
-
*/
|
|
14
|
-
sourceUrlPrefixes?: string[];
|
|
15
|
-
}
|
|
16
|
-
export interface BeginAuthSessionParams {
|
|
17
|
-
/**
|
|
18
|
-
* The account ID to authorize requests for.
|
|
19
|
-
*/
|
|
20
|
-
accountId: string;
|
|
21
|
-
/**
|
|
22
|
-
* Headers to send to your backend API.
|
|
23
|
-
*
|
|
24
|
-
* IMPORTANT: do not call 'AuthManager.beginAuthSession' or 'AuthManager.endAuthSession' inside this callback, as this will cause a deadlock.
|
|
25
|
-
*/
|
|
26
|
-
authHeaders: () => Promise<Record<string, string>>;
|
|
27
|
-
/**
|
|
28
|
-
* The fully-qualified URL for your backend API's auth endpoint (the endpoint that returns a JWT as plain text).
|
|
29
|
-
*/
|
|
30
|
-
authUrl: string;
|
|
31
|
-
/**
|
|
32
|
-
* Optional configuration.
|
|
33
|
-
*/
|
|
34
|
-
options?: Pick<BytescaleApiClientConfig, "fetchApi" | "cdnUrl">;
|
|
35
|
-
/**
|
|
36
|
-
* Returns configuration for service-worker authorization. The primary JWT returned by `authUrl` is restricted using
|
|
37
|
-
* `sourceUrlPrefixes`; `additionalConfig` contains download-only rules and never authenticates SDK API operations.
|
|
38
|
-
* Called when the session begins and again 20 seconds before the earliest additional entry expires. Entries without
|
|
39
|
-
* an expiry do not trigger a refresh.
|
|
40
|
-
*
|
|
41
|
-
* Requires `serviceWorkerScript` and browser service-worker support. Additional rules cannot use the CDN cookie
|
|
42
|
-
* fallback.
|
|
43
|
-
*
|
|
44
|
-
* IMPORTANT: do not call 'AuthManager.beginAuthSession' or 'AuthManager.endAuthSession' inside this callback, as this will cause a deadlock.
|
|
45
|
-
*/
|
|
46
|
-
serviceWorkerConfig?: () => Promise<AuthManagerServiceWorkerConfig>;
|
|
47
|
-
/**
|
|
48
|
-
* Enables support for modern browsers that block third-party cookies (like Safari).
|
|
49
|
-
*
|
|
50
|
-
* The value of this field must be the path to the service worker JavaScript file hosted at the root of your website.
|
|
51
|
-
*
|
|
52
|
-
* OVERVIEW:
|
|
53
|
-
*
|
|
54
|
-
* This feature works by running a "service worker" in the background that adds "Authorization" and "Authorization-Token"
|
|
55
|
-
* request headers to HTTP requests made to the Bytescale CDN. This allows the Bytescale CDN to authorize requests
|
|
56
|
-
* to private files using JWTs issued by your application. Historically, these requests have been authorized using
|
|
57
|
-
* JWT cookies (i.e. JWTs sent to the Bytescale CDN via the "Cookies" header). However, modern browsers are starting
|
|
58
|
-
* to block these cookies, meaning "Authorization" request headers must be used instead. Authorization headers can
|
|
59
|
-
* only be added to requests originating from page elements like "<img>" elements through the use of service workers.
|
|
60
|
-
*
|
|
61
|
-
* INSTRUCTIONS:
|
|
62
|
-
*
|
|
63
|
-
* 1. Your JWT must include the 'accountId' field at the root of the 'payload' section of the JWT (i.e. next to the 'exp' field).
|
|
64
|
-
*
|
|
65
|
-
* 2. Create a JavaScript file that contains the following line:
|
|
66
|
-
*
|
|
67
|
-
* importScripts("https://js.bytescale.com/auth-sw/v1");
|
|
68
|
-
*
|
|
69
|
-
* 3. Host this JavaScript file from your website:
|
|
70
|
-
*
|
|
71
|
-
* 3a. It MUST be under the ROOT directory of your website.
|
|
72
|
-
* (e.g. "/bytescale-auth-sw.js")
|
|
73
|
-
*
|
|
74
|
-
* 3b. It MUST be on the SAME DOMAIN as your website.
|
|
75
|
-
* (e.g. "www.example.com" and not "assets.example.com")
|
|
76
|
-
*
|
|
77
|
-
* 4. Specify the absolute path to your JavaScript file in the 'beginAuthSession' call.
|
|
78
|
-
* (e.g. { ..., serviceWorkerScript: "/bytescale-auth-sw.js" })
|
|
79
|
-
*
|
|
80
|
-
* Examples:
|
|
81
|
-
*
|
|
82
|
-
* - CORRECT: "/bytescale-auth-sw.js"
|
|
83
|
-
* - INCORRECT: "bytescale-auth-sw.js"
|
|
84
|
-
* - INCORRECT: "/scripts/bytescale-auth-sw.js"
|
|
85
|
-
* - INCORRECT: "https://example.com/bytescale-auth-sw.js"
|
|
86
|
-
*
|
|
87
|
-
* Why does the script need to be hosted on the website's domain, under the root directory?:
|
|
88
|
-
*
|
|
89
|
-
* Service workers can only interact with events raised by pages at the same level or below them, hence why your
|
|
90
|
-
* script must be hosted on your website's domain in the root directory.
|
|
91
|
-
*/
|
|
92
|
-
serviceWorkerScript?: string;
|
|
93
|
-
}
|
|
1
|
+
import { BeginAuthSessionParams } from "./BeginAuthSessionParams";
|
|
94
2
|
export interface AuthManagerInterface {
|
|
95
3
|
/**
|
|
96
4
|
* Begins a JWT auth session with the Bytescale API and Bytescale CDN.
|
|
@@ -1,14 +1,21 @@
|
|
|
1
|
-
import { AuthManagerServiceWorkerConfig, BeginAuthSessionParams } from "./AuthManagerInterface";
|
|
2
|
-
import { AuthSwConfigEntryDto } from "../dtos/AuthSwConfigEntryDto";
|
|
3
1
|
import { ServiceWorkerConfig } from "./ServiceWorkerConfig";
|
|
2
|
+
import { BeginAuthSessionParams } from "./BeginAuthSessionParams";
|
|
3
|
+
import { AuthSessionConfig } from "./AuthSessionConfig";
|
|
4
|
+
export interface AuthSessionConfigState {
|
|
5
|
+
accessToken: string | undefined;
|
|
6
|
+
config: AuthSessionConfig;
|
|
7
|
+
expiresAt: number | undefined;
|
|
8
|
+
jwt: string | undefined;
|
|
9
|
+
refreshHandle: number | undefined;
|
|
10
|
+
}
|
|
4
11
|
export interface AuthSession {
|
|
12
|
+
/** Legacy default-token fields retained for SDK 3.54.0 bundles sharing this global state. */
|
|
5
13
|
accessToken: string | undefined;
|
|
6
14
|
accessTokenRefreshHandle: number | undefined;
|
|
15
|
+
authConfigs?: AuthSessionConfigState[];
|
|
7
16
|
authServiceWorker: ServiceWorkerConfig | undefined;
|
|
8
17
|
isActive: boolean;
|
|
9
18
|
isReady?: boolean;
|
|
10
19
|
params: BeginAuthSessionParams;
|
|
11
|
-
|
|
12
|
-
serviceWorkerConfig: AuthManagerServiceWorkerConfig | undefined;
|
|
13
|
-
serviceWorkerConfigRefreshHandle: number | undefined;
|
|
20
|
+
serviceWorkerConfigured?: boolean;
|
|
14
21
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface AuthSessionConfigBase {
|
|
2
|
+
/** The Bytescale account authorized by this configuration. */
|
|
3
|
+
accountId: string;
|
|
4
|
+
/** A string names this configuration. `undefined` deliberately marks it as the session default. */
|
|
5
|
+
authConfigId: string | undefined;
|
|
6
|
+
/** Enables CDN cookie authentication. Defaults to false. */
|
|
7
|
+
enableCookieAuth?: boolean;
|
|
8
|
+
/** Enables CDN service-worker authentication. Defaults to true. */
|
|
9
|
+
enableServiceWorkerAuth?: boolean;
|
|
10
|
+
/** Restricts service-worker authentication to matching page or iframe URLs. */
|
|
11
|
+
sourceUrlPrefixes?: string[];
|
|
12
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { AuthSessionConfigBase } from "./AuthSessionConfigBase";
|
|
2
|
+
export interface AuthSessionConfigManual extends AuthSessionConfigBase {
|
|
3
|
+
authHeaders?: never;
|
|
4
|
+
authUrl?: never;
|
|
5
|
+
/** Returns the JWT for this configuration. AuthManager refreshes it based on its validated TTL. */
|
|
6
|
+
getAuthorizationToken: () => Promise<string>;
|
|
7
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { BeginAuthSessionParamsOptions } from "./BeginAuthSessionParamsOptions";
|
|
2
|
+
/** The complete AuthManager input supported by SDK 3.54.0. */
|
|
3
|
+
export interface BeginAuthSessionParamsV1 {
|
|
4
|
+
accountId: string;
|
|
5
|
+
authConfigs?: never;
|
|
6
|
+
authHeaders: () => Promise<Record<string, string>>;
|
|
7
|
+
authUrl: string;
|
|
8
|
+
options?: BeginAuthSessionParamsOptions;
|
|
9
|
+
serviceWorkerScript?: string;
|
|
10
|
+
urlRewriteRules?: never;
|
|
11
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { AuthSessionConfig } from "./AuthSessionConfig";
|
|
2
|
+
import { UrlRewriteRule } from "./UrlRewriteRule";
|
|
3
|
+
import { NonEmptyArray } from "./NonEmptyArray";
|
|
4
|
+
import { BeginAuthSessionParamsOptions } from "./BeginAuthSessionParamsOptions";
|
|
5
|
+
export interface BeginAuthSessionParamsV2 {
|
|
6
|
+
/** Present only to make the structural distinction from V1 explicit. */
|
|
7
|
+
accountId?: undefined;
|
|
8
|
+
authConfigs: () => Promise<NonEmptyArray<AuthSessionConfig>>;
|
|
9
|
+
authHeaders?: never;
|
|
10
|
+
authUrl?: never;
|
|
11
|
+
options?: BeginAuthSessionParamsOptions;
|
|
12
|
+
/** Required when a configuration enables service-worker authentication or a rewrite rule is present. */
|
|
13
|
+
serviceWorkerScript?: string;
|
|
14
|
+
/** Rewrites in-scope requests before service-worker authentication matching. */
|
|
15
|
+
urlRewriteRules?: UrlRewriteRule[];
|
|
16
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type NonEmptyArray<T> = readonly [T, ...T[]];
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export interface UrlRewriteRule {
|
|
2
|
+
/** URL prefix to replace. This URL must be covered by the service worker's origin and scope. */
|
|
3
|
+
fromUrlPrefix: string;
|
|
4
|
+
/** Replacement URL prefix. The remainder of the original URL is appended unchanged. */
|
|
5
|
+
toUrlPrefix: string;
|
|
6
|
+
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { AuthManagerInterface
|
|
1
|
+
import { AuthManagerInterface } from "../../private/model/AuthManagerInterface";
|
|
2
2
|
import { AuthSwSetConfigDto } from "../../private/dtos/AuthSwSetConfigDto";
|
|
3
3
|
import { ServiceWorkerUtils } from "../../private/ServiceWorkerUtils";
|
|
4
|
+
import { BeginAuthSessionParams } from "../../private/model/BeginAuthSessionParams";
|
|
4
5
|
export type { AuthSwConfigEntryDto } from "../../private/dtos/AuthSwConfigEntryDto";
|
|
5
6
|
export type { AuthSwHeaderDto } from "../../private/dtos/AuthSwHeaderDto";
|
|
6
|
-
export type { AuthManagerServiceWorkerConfig, BeginAuthSessionParams } from "../../private/model/AuthManagerInterface";
|
|
7
7
|
declare class AuthManagerImpl implements AuthManagerInterface {
|
|
8
8
|
private readonly serviceWorkerUtils;
|
|
9
9
|
private readonly authSessionMutex;
|
|
@@ -21,20 +21,36 @@ declare class AuthManagerImpl implements AuthManagerInterface {
|
|
|
21
21
|
isAuthSessionReady(): boolean;
|
|
22
22
|
beginAuthSession(params: BeginAuthSessionParams): Promise<void>;
|
|
23
23
|
endAuthSession(): Promise<void>;
|
|
24
|
-
private
|
|
25
|
-
private
|
|
26
|
-
private sendMergedServiceWorkerConfig;
|
|
27
|
-
private getServiceWorkerConfigRefreshEpoch;
|
|
24
|
+
private refreshAuthConfig;
|
|
25
|
+
private sendCurrentServiceWorkerConfig;
|
|
28
26
|
private sendServiceWorkerConfig;
|
|
29
|
-
private
|
|
27
|
+
private getV2Configs;
|
|
28
|
+
private normalizeV1Config;
|
|
29
|
+
private validateSessionConfig;
|
|
30
|
+
private validateUrlRewriteRules;
|
|
31
|
+
private updateSessionState;
|
|
32
|
+
private isConfigUsable;
|
|
33
|
+
private isServiceWorkerEnabled;
|
|
34
|
+
private isV2Params;
|
|
35
|
+
private isValidAccountId;
|
|
36
|
+
private shouldSetCookie;
|
|
30
37
|
private waitForServiceWorker;
|
|
31
38
|
private getAccessTokenUrl;
|
|
32
39
|
private getCdnUrl;
|
|
33
40
|
private deleteAccessToken;
|
|
34
41
|
private setAccessToken;
|
|
35
|
-
private
|
|
42
|
+
private getAuthorizationToken;
|
|
43
|
+
private validateJwt;
|
|
36
44
|
}
|
|
37
|
-
/**
|
|
38
|
-
* Alternative way of implementing a static class (i.e. all methods static). We do this so we can use a interface on the class (interfaces can't define static methods).
|
|
39
|
-
*/
|
|
45
|
+
/** Alternative to a static class that allows the implementation to satisfy an interface. */
|
|
40
46
|
export declare const AuthManager: AuthManagerImpl;
|
|
47
|
+
export { BeginAuthSessionParamsV1 } from "../../private/model/BeginAuthSessionParamsV1";
|
|
48
|
+
export { BeginAuthSessionParamsV2 } from "../../private/model/BeginAuthSessionParamsV2";
|
|
49
|
+
export { BeginAuthSessionParams } from "../../private/model/BeginAuthSessionParams";
|
|
50
|
+
export { AuthSessionConfigAuto } from "../../private/model/AuthSessionConfigAuto";
|
|
51
|
+
export { AuthSessionConfigManual } from "../../private/model/AuthSessionConfigManual";
|
|
52
|
+
export { AuthSessionConfig } from "../../private/model/AuthSessionConfig";
|
|
53
|
+
export { AuthSessionConfigBase } from "../../private/model/AuthSessionConfigBase";
|
|
54
|
+
export { UrlRewriteRule } from "../../private/model/UrlRewriteRule";
|
|
55
|
+
export { NonEmptyArray } from "../../private/model/NonEmptyArray";
|
|
56
|
+
export { BeginAuthSessionParamsOptions } from "../../private/model/BeginAuthSessionParamsOptions";
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { AuthManagerInterface
|
|
1
|
+
import { AuthManagerInterface } from "../../private/model/AuthManagerInterface";
|
|
2
|
+
import { BeginAuthSessionParams } from "../../private/model/BeginAuthSessionParams";
|
|
2
3
|
export type { AuthSwConfigEntryDto } from "../../private/dtos/AuthSwConfigEntryDto";
|
|
3
4
|
export type { AuthSwHeaderDto } from "../../private/dtos/AuthSwHeaderDto";
|
|
4
|
-
export type { AuthManagerServiceWorkerConfig, BeginAuthSessionParams } from "../../private/model/AuthManagerInterface";
|
|
5
5
|
declare class AuthManagerImpl implements AuthManagerInterface {
|
|
6
6
|
beginAuthSession(_params: BeginAuthSessionParams): Promise<void>;
|
|
7
7
|
endAuthSession(): Promise<void>;
|
|
@@ -12,3 +12,13 @@ declare class AuthManagerImpl implements AuthManagerInterface {
|
|
|
12
12
|
* Alternative way of implementing a static class (i.e. all methods static). We do this so we can use a interface on the class (interfaces can't define static methods).
|
|
13
13
|
*/
|
|
14
14
|
export declare const AuthManager: AuthManagerImpl;
|
|
15
|
+
export { BeginAuthSessionParamsV1 } from "../../private/model/BeginAuthSessionParamsV1";
|
|
16
|
+
export { BeginAuthSessionParamsV2 } from "../../private/model/BeginAuthSessionParamsV2";
|
|
17
|
+
export { BeginAuthSessionParams } from "../../private/model/BeginAuthSessionParams";
|
|
18
|
+
export { AuthSessionConfigAuto } from "../../private/model/AuthSessionConfigAuto";
|
|
19
|
+
export { AuthSessionConfigManual } from "../../private/model/AuthSessionConfigManual";
|
|
20
|
+
export { AuthSessionConfig } from "../../private/model/AuthSessionConfig";
|
|
21
|
+
export { AuthSessionConfigBase } from "../../private/model/AuthSessionConfigBase";
|
|
22
|
+
export { UrlRewriteRule } from "../../private/model/UrlRewriteRule";
|
|
23
|
+
export { NonEmptyArray } from "../../private/model/NonEmptyArray";
|
|
24
|
+
export { BeginAuthSessionParamsOptions } from "../../private/model/BeginAuthSessionParamsOptions";
|
|
@@ -17,12 +17,13 @@ export interface BytescaleApiClientConfig {
|
|
|
17
17
|
* Not required for the browser.
|
|
18
18
|
*/
|
|
19
19
|
fetchApi?: FetchAPI;
|
|
20
|
+
/** Must begin with "public_" or "secret_". Optional when an AuthManager configuration authenticates the request. */
|
|
21
|
+
apiKey?: string;
|
|
20
22
|
/**
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
* Please note: if you require JWT-based auth, you must provide an API key to this field, and then call 'AuthManager.beginAuthSession' to start a JWT-based auth session. The JWT's permissions will be merged with the API key's permissions, with precedence given to the JWT.
|
|
23
|
+
* Selects a named AuthManager configuration. Omit to use the default configuration, or use `false` to disable
|
|
24
|
+
* AuthManager authentication for this client.
|
|
24
25
|
*/
|
|
25
|
-
|
|
26
|
+
authConfigId?: string | false;
|
|
26
27
|
/**
|
|
27
28
|
* The base URL of the Bytescale API. (Excludes trailing "/".)
|
|
28
29
|
*/
|
|
@@ -38,7 +39,8 @@ export interface BytescaleApiClientConfig {
|
|
|
38
39
|
/**
|
|
39
40
|
* Headers to include in all API requests.
|
|
40
41
|
*
|
|
41
|
-
* These headers take precedence over
|
|
42
|
+
* These headers intentionally take precedence over headers automatically added by the SDK, including
|
|
43
|
+
* "Authorization" and "Authorization-Token". This preserves the SDK's existing custom-header precedence.
|
|
42
44
|
*/
|
|
43
45
|
headers?: HTTPHeaders | (() => Promise<HTTPHeaders> | HTTPHeaders);
|
|
44
46
|
}
|
|
@@ -51,7 +53,12 @@ export declare class BytescaleApiClientConfigUtils {
|
|
|
51
53
|
static getApiUrl(config: BytescaleApiClientConfig): string;
|
|
52
54
|
static getCdnUrl(config: Pick<BytescaleApiClientConfig, "cdnUrl">): string;
|
|
53
55
|
static getFetchApi(config: Pick<BytescaleApiClientConfig, "fetchApi">): FetchAPI;
|
|
54
|
-
static getAccountId(config: Pick<BytescaleApiClientConfig, "apiKey">): string;
|
|
56
|
+
static getAccountId(config: Pick<BytescaleApiClientConfig, "apiKey" | "authConfigId">): string;
|
|
57
|
+
static resolveAuthentication(config: Pick<BytescaleApiClientConfig, "apiKey" | "authConfigId">): {
|
|
58
|
+
accountId: string;
|
|
59
|
+
headers: HTTPHeaders;
|
|
60
|
+
};
|
|
61
|
+
private static getApiKeyAccountId;
|
|
55
62
|
static validate(config: BytescaleApiClientConfig): void;
|
|
56
63
|
}
|
|
57
64
|
/**
|
package/dist/worker/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) {
|