@bytescale/sdk 3.0.0-alpha.9 → 3.2.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.
Files changed (31) hide show
  1. package/README.md +388 -48
  2. package/dist/browser/cjs/main.js +704 -568
  3. package/dist/browser/esm/main.mjs +2803 -0
  4. package/dist/node/cjs/main.js +622 -240
  5. package/dist/node/esm/main.mjs +622 -240
  6. package/dist/types/index.d.ts +1 -1
  7. package/dist/types/private/AuthSessionState.d.ts +25 -8
  8. package/dist/types/private/EnvChecker.d.ts +1 -1
  9. package/dist/types/private/Mutex.d.ts +1 -1
  10. package/dist/types/private/StreamUtils.d.ts +1 -0
  11. package/dist/types/private/UploadManagerBase.d.ts +6 -1
  12. package/dist/types/private/UploadManagerBrowserWorkerBase.d.ts +19 -0
  13. package/dist/types/private/UploadManagerFetchUtils.d.ts +8 -0
  14. package/dist/types/private/model/AuthManagerInterface.d.ts +22 -5
  15. package/dist/types/private/model/AuthSession.d.ts +2 -2
  16. package/dist/types/private/model/OnPartProgress.d.ts +1 -1
  17. package/dist/types/private/model/UploadSourceProcessed.d.ts +6 -1
  18. package/dist/types/public/browser/AuthManagerBrowser.d.ts +10 -6
  19. package/dist/types/public/browser/UploadManagerBrowser.d.ts +3 -12
  20. package/dist/types/public/node/AuthManagerNode.d.ts +8 -3
  21. package/dist/types/public/shared/CommonTypes.d.ts +0 -1
  22. package/dist/types/public/shared/UrlBuilder.d.ts +10 -12
  23. package/dist/types/public/shared/UrlBuilderTypes.d.ts +0 -6
  24. package/dist/types/public/shared/generated/runtime.d.ts +7 -5
  25. package/dist/types/public/worker/UploadManagerWorker.d.ts +8 -0
  26. package/dist/types/public/worker/index.d.ts +2 -0
  27. package/dist/worker/cjs/main.js +2578 -0
  28. package/dist/worker/esm/main.mjs +2578 -0
  29. package/package.json +11 -4
  30. package/tests/UploadManager.test.ts +0 -7
  31. package/tests/UrlBuilder.test.ts +33 -27
@@ -1,2 +1,2 @@
1
1
  export * from "./public/shared";
2
- export * from "./public/node";
2
+ export * from "./public/browser";
@@ -1,13 +1,30 @@
1
1
  import { AuthSession } from "./model/AuthSession";
2
+ import { Mutex } from "./Mutex";
3
+ /**
4
+ * Maintains a global session state, even across package versions.
5
+ *
6
+ * This is to allow users to start auth sessions via the Bytescale JavaScript SDK, where due to versioning or other
7
+ * bundling issues, the Bytescale Upload Widget has been bundled with a different Bytescale JavaScript SDK. In this
8
+ * scenario, the user wouldn't be able to start an auth session with the Bytescale Upload Widget. Therefore, we use
9
+ * global state (i.e. on the window) to ensure the session state can be shared between the user's instance of the
10
+ * Bytescale JavaScript SDK and the Upload Widget's version of the Bytescale JavaScript SDK.
11
+ *
12
+ * Users also frequently have problems caused by them not keeping track of *Api and *Manager instances correctly, so
13
+ * making this global prevents a lot of common mistakes.
14
+ */
2
15
  export declare class AuthSessionState {
16
+ private static readonly stateKey;
17
+ private static readonly mutexKey;
3
18
  /**
4
- * Intentionally global:
5
- *
6
- * Users frequently have problems caused by them not keeping track of *Api and *Manager instances correctly, so we
7
- * make this global as in 99.9% of cases, this is what users want, and it prevents lots of common mistakes.
8
- *
9
- * We only set this in the browser.
10
- * Never in Node.js.
19
+ * Called in the browser only.
11
20
  */
12
- static session: AuthSession | undefined;
21
+ static getMutex(): Mutex;
22
+ /**
23
+ * Called in the browser only.
24
+ */
25
+ static setSession(session: AuthSession | undefined): void;
26
+ /**
27
+ * Called in the browser and in Node.js (so we check the env before calling env-specific code).
28
+ */
29
+ static getSession(): AuthSession | undefined;
13
30
  }
@@ -1,4 +1,4 @@
1
1
  export declare class EnvChecker {
2
- static isNodeJs(): boolean;
2
+ static isBrowser(): boolean;
3
3
  static methodRequiresBrowser(methodName: string): Error;
4
4
  }
@@ -1,5 +1,5 @@
1
1
  /**
2
- * A lightweight mutex. (Other libraries contain too many features and we want to keep the size upload-js down).
2
+ * A lightweight mutex. (Other libraries contain too many features and we want to keep size down).
3
3
  *
4
4
  * Characteristics:
5
5
  * - Non-reentrant.
@@ -3,6 +3,7 @@ import type * as stream from "stream";
3
3
  export declare class StreamUtils {
4
4
  static create(): stream.Readable;
5
5
  static empty(): NodeJS.ReadableStream;
6
+ static fromArrayBuffer(buffer: ArrayBuffer): NodeJS.ReadableStream;
6
7
  static fromBuffer(buffer: Buffer): NodeJS.ReadableStream;
7
8
  static endStream(readable: stream.Readable): void;
8
9
  }
@@ -11,6 +11,7 @@ import { UploadManagerParams, UploadSource } from "../public/shared/CommonTypes"
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;
14
15
  private readonly defaultMaxConcurrentUploadParts;
15
16
  private readonly intervalMs;
16
17
  private readonly uploadApi;
@@ -28,7 +29,11 @@ export declare abstract class UploadManagerBase<TSource, TInit> implements Uploa
28
29
  protected abstract doPutUploadPart(part: UploadPart, contentLength: number, source: TSource, onProgress: (bytesSentDelta: number) => void, addCancellationHandler: AddCancellationHandler): Promise<PutUploadPartResult>;
29
30
  private onIntervalTick;
30
31
  private makeCancellationMethods;
31
- private makeProgressCallback;
32
+ /**
33
+ * Returns a callback, which when called, returns a callback that can be used by ONE specific part to report its progress.
34
+ */
35
+ private makeOnProgressForPartFactory;
36
+ private makeProgressEvent;
32
37
  private assertNotCancelled;
33
38
  private isCancelled;
34
39
  private beginUpload;
@@ -0,0 +1,19 @@
1
+ import { UploadManagerBase } from "./UploadManagerBase";
2
+ import { UploadSourceProcessedBrowser, UploadSourceProcessedWorker } from "./model/UploadSourceProcessed";
3
+ import { PreUploadInfo } from "./model/PreUploadInfo";
4
+ import { UploadPart } from "../public/shared/generated";
5
+ import { UploadManagerParams, UploadSource } from "../public/shared/CommonTypes";
6
+ declare type BrowserOrWorkerUploadSource = UploadSourceProcessedBrowser | UploadSourceProcessedWorker;
7
+ /**
8
+ * The "browser" and "worker" runtimes support the same input types, but the former uses XHR and the latter uses Fetch.
9
+ */
10
+ export declare abstract class UploadManagerBrowserWorkerBase extends UploadManagerBase<BrowserOrWorkerUploadSource, undefined> {
11
+ protected processUploadSource(data: UploadSource): BrowserOrWorkerUploadSource;
12
+ protected getPreUploadInfoPartial(_request: UploadManagerParams, data: BrowserOrWorkerUploadSource): Partial<PreUploadInfo> & {
13
+ size: number;
14
+ };
15
+ protected preUpload(_source: BrowserOrWorkerUploadSource): undefined;
16
+ protected postUpload(_init: undefined): Promise<void>;
17
+ protected getRequestBody(part: UploadPart, blob: BrowserOrWorkerUploadSource): Blob | ArrayBuffer;
18
+ }
19
+ export {};
@@ -0,0 +1,8 @@
1
+ import { BytescaleApiClientConfig, UploadPart } from "../public/shared";
2
+ import { AddCancellationHandler } from "./model/AddCancellationHandler";
3
+ export declare class UploadManagerFetchUtils {
4
+ static doPutUploadPart(config: BytescaleApiClientConfig, part: UploadPart, content: BodyInit, contentLength: number, addCancellationHandler: AddCancellationHandler): Promise<{
5
+ etag: string | undefined;
6
+ status: number;
7
+ }>;
8
+ }
@@ -1,3 +1,24 @@
1
+ import { BytescaleApiClientConfig } from "../../public/shared";
2
+ export interface BeginAuthSessionParams {
3
+ /**
4
+ * The account ID to authorize requests for.
5
+ */
6
+ accountId: string;
7
+ /**
8
+ * Headers to send to your backend API.
9
+ *
10
+ * IMPORTANT: do not call 'AuthManager.beginAuthSession' or 'AuthManager.endAuthSession' inside this callback, as this will cause a deadlock.
11
+ */
12
+ authHeaders: () => Promise<Record<string, string>>;
13
+ /**
14
+ * The fully-qualified URL for your backend API's auth endpoint.
15
+ */
16
+ authUrl: string;
17
+ /**
18
+ * Optional configuration.
19
+ */
20
+ options?: Pick<BytescaleApiClientConfig, "fetchApi" | "cdnUrl">;
21
+ }
1
22
  export interface AuthManagerInterface {
2
23
  /**
3
24
  * Begins an authenticated Bytescale API and Bytescale CDN session.
@@ -21,12 +42,8 @@ export interface AuthManagerInterface {
21
42
  * 2) The JWT will be saved to a cookie scoped to the Bytescale CDN. This allows the user to view private files via the URL in the browser, including <img> elements on the page that reference private images, etc.
22
43
  *
23
44
  * 3) The JWT will also be added as a request header via 'authorization-token' to all Bytescale API requests made via this SDK. This allows the user to upload private files and perform administrative operations permitted by the JWT, such as deleting files, etc.
24
- *
25
- * @param authUrl The fully-qualified URL for your backend API's auth endpoint.
26
- * @param authHeaders Headers to send to your backend API.
27
- * IMPORTANT: do not call '*AuthSession' inside this callback, as this will cause a deadlock.
28
45
  */
29
- beginAuthSession: (authUrl: string, authHeaders: () => Promise<Record<string, string>>) => Promise<void>;
46
+ beginAuthSession: (params: BeginAuthSessionParams) => Promise<void>;
30
47
  /**
31
48
  * Ends an authenticated Bytescale API and Bytescale CDN session.
32
49
  *
@@ -1,7 +1,7 @@
1
+ import { BeginAuthSessionParams } from "./AuthManagerInterface";
1
2
  export interface AuthSession {
2
3
  accessToken: string | undefined;
3
4
  accessTokenRefreshHandle: number | undefined;
4
- authHeaders: () => Promise<Record<string, string>>;
5
- authUrl: string;
6
5
  isActive: boolean;
6
+ params: BeginAuthSessionParams;
7
7
  }
@@ -1 +1 @@
1
- export declare type OnPartProgress = (bytesSentDelta: number) => void;
1
+ export declare type OnPartProgress = (bytesSentTotalForPart: number) => void;
@@ -9,10 +9,15 @@ export interface UploadSourceBuffer {
9
9
  type: "Buffer";
10
10
  value: Buffer;
11
11
  }
12
+ export interface UploadSourceArrayBuffer {
13
+ type: "ArrayBuffer";
14
+ value: ArrayBuffer;
15
+ }
12
16
  export interface UploadSourceStream {
13
17
  type: "Stream";
14
18
  value: NodeChunkedStream;
15
19
  }
16
- export declare type UploadSourceProcessedIsomorphic = UploadSourceBlob;
20
+ export declare type UploadSourceProcessedIsomorphic = UploadSourceBlob | UploadSourceArrayBuffer;
17
21
  export declare type UploadSourceProcessedBrowser = UploadSourceProcessedIsomorphic;
22
+ export declare type UploadSourceProcessedWorker = UploadSourceProcessedIsomorphic;
18
23
  export declare type UploadSourceProcessedNode = UploadSourceStream | UploadSourceBuffer | UploadSourceProcessedIsomorphic;
@@ -1,7 +1,5 @@
1
- import { AuthManagerInterface } from "../../private/model/AuthManagerInterface";
2
- import { BaseAPI, BytescaleApiClientConfig } from "../shared/generated";
3
- export declare class AuthManager extends BaseAPI implements AuthManagerInterface {
4
- private readonly accessTokenUrl;
1
+ import { AuthManagerInterface, BeginAuthSessionParams } from "../../private/model/AuthManagerInterface";
2
+ declare class AuthManagerImpl implements AuthManagerInterface {
5
3
  private readonly authSessionMutex;
6
4
  private readonly contentType;
7
5
  private readonly contentTypeJson;
@@ -10,12 +8,18 @@ export declare class AuthManager extends BaseAPI implements AuthManagerInterface
10
8
  private readonly maxJwtTtlSeconds;
11
9
  private readonly retryAuthAfterErrorSeconds;
12
10
  private readonly refreshBeforeExpirySeconds;
13
- constructor(config: BytescaleApiClientConfig);
11
+ constructor();
14
12
  isAuthSessionActive(): boolean;
15
- beginAuthSession(authUrl: string, authHeaders: () => Promise<Record<string, string>>): Promise<void>;
13
+ beginAuthSession(params: BeginAuthSessionParams): Promise<void>;
16
14
  endAuthSession(): Promise<void>;
17
15
  private refreshAccessToken;
16
+ private getAccessTokenUrl;
18
17
  private deleteAccessToken;
19
18
  private setAccessToken;
20
19
  private getAccessToken;
21
20
  }
21
+ /**
22
+ * 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).
23
+ */
24
+ export declare const AuthManager: AuthManagerImpl;
25
+ export {};
@@ -1,17 +1,8 @@
1
- import { UploadManagerBase } from "../../private/UploadManagerBase";
2
1
  import { UploadSourceProcessedBrowser } from "../../private/model/UploadSourceProcessed";
3
- import { PreUploadInfo } from "../../private/model/PreUploadInfo";
4
2
  import { UploadPart } from "../shared/generated";
5
3
  import { PutUploadPartResult } from "../../private/model/PutUploadPartResult";
6
4
  import { AddCancellationHandler } from "../../private/model/AddCancellationHandler";
7
- import { UploadManagerParams, UploadSource } from "../shared/CommonTypes";
8
- export declare class UploadManager extends UploadManagerBase<UploadSourceProcessedBrowser, undefined> {
9
- protected processUploadSource(data: UploadSource): UploadSourceProcessedBrowser;
10
- protected getPreUploadInfoPartial(_request: UploadManagerParams, data: UploadSourceProcessedBrowser): Partial<PreUploadInfo> & {
11
- size: number;
12
- };
13
- protected preUpload(_source: UploadSourceProcessedBrowser): undefined;
14
- protected postUpload(_init: undefined): Promise<void>;
15
- protected doPutUploadPart(part: UploadPart, contentLength: number, source: UploadSourceProcessedBrowser, onProgress: (bytesSentDelta: number) => void, addCancellationHandler: AddCancellationHandler): Promise<PutUploadPartResult>;
16
- private getRequestBody;
5
+ import { UploadManagerBrowserWorkerBase } from "../../private/UploadManagerBrowserWorkerBase";
6
+ export declare class UploadManager extends UploadManagerBrowserWorkerBase {
7
+ protected doPutUploadPart(part: UploadPart, _contentLength: number, source: UploadSourceProcessedBrowser, onProgress: (bytesSentDelta: number) => void, addCancellationHandler: AddCancellationHandler): Promise<PutUploadPartResult>;
17
8
  }
@@ -1,6 +1,11 @@
1
- import { AuthManagerInterface } from "../../private/model/AuthManagerInterface";
2
- export declare class AuthManager implements AuthManagerInterface {
3
- beginAuthSession(_authUrl: string, _authHeaders: () => Promise<Record<string, string>>): Promise<void>;
1
+ import { AuthManagerInterface, BeginAuthSessionParams } from "../../private/model/AuthManagerInterface";
2
+ declare class AuthManagerImpl implements AuthManagerInterface {
3
+ beginAuthSession(_params: BeginAuthSessionParams): Promise<void>;
4
4
  endAuthSession(): Promise<void>;
5
5
  isAuthSessionActive(): boolean;
6
6
  }
7
+ /**
8
+ * 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).
9
+ */
10
+ export declare const AuthManager: AuthManagerImpl;
11
+ export {};
@@ -20,7 +20,6 @@ export interface UploadProgress {
20
20
  }
21
21
  export declare type UploadSource = NodeJS.ReadableStream | BlobLike | Buffer | string;
22
22
  export interface UploadManagerParams extends Omit<BeginMultipartUploadRequest, "size" | "protocol"> {
23
- accountId: string;
24
23
  cancellationToken?: CancellationToken;
25
24
  data: UploadSource;
26
25
  maxConcurrentUploadParts?: number;
@@ -1,7 +1,5 @@
1
- import { UrlBuilderConfig, UrlBuilderParams } from "./UrlBuilderTypes";
1
+ import { UrlBuilderParams } from "./UrlBuilderTypes";
2
2
  export declare class UrlBuilder {
3
- private readonly cdnUrl;
4
- constructor(config?: UrlBuilderConfig);
5
3
  /**
6
4
  * Builds a URL to either a raw file or a transformed file.
7
5
  *
@@ -21,13 +19,13 @@ export declare class UrlBuilder {
21
19
  *
22
20
  * new UrlBuilder().url({ accountId: "1234abc", filePath: "/example.jpg", options: { transformation: { type: "preset", preset: "thumbnail" } } })
23
21
  */
24
- url(params: UrlBuilderParams): string;
25
- private raw;
26
- private transformation;
27
- private getBaseUrl;
28
- private getCommonTransformationQueryParams;
29
- private getCommonQueryParams;
30
- private makeQueryParams;
31
- private getTransformationParams;
32
- private addQueryParams;
22
+ static url(params: UrlBuilderParams): string;
23
+ private static raw;
24
+ private static transformation;
25
+ private static getBaseUrl;
26
+ private static getCommonTransformationQueryParams;
27
+ private static getCommonQueryParams;
28
+ private static makeQueryParams;
29
+ private static getTransformationParams;
30
+ private static addQueryParams;
33
31
  }
@@ -4,12 +4,6 @@
4
4
  export declare const UrlBuilderTypesNoOp = false;
5
5
  export declare type ParameterGroup = Record<string, string | number | boolean | undefined | null>;
6
6
  export declare type KeyValuePair = [string, string];
7
- export interface UrlBuilderConfig {
8
- /**
9
- * The base URL of the Bytescale CDN.
10
- */
11
- cdnUrl?: string;
12
- }
13
7
  export interface UrlBuilderParams {
14
8
  /**
15
9
  * The account that manages the file.
@@ -45,9 +45,9 @@ export declare class BytescaleApiClientConfigUtils {
45
45
  private static readonly specialApiKeyAccountId;
46
46
  private static readonly accountIdLength;
47
47
  static getApiUrl(config: BytescaleApiClientConfig): string;
48
- static getCdnUrl(config: BytescaleApiClientConfig): string;
49
- static getFetchApi(config: BytescaleApiClientConfig): FetchAPI;
50
- static getAccountId(config: BytescaleApiClientConfig): string;
48
+ static getCdnUrl(config: Pick<BytescaleApiClientConfig, "cdnUrl">): string;
49
+ static getFetchApi(config: Pick<BytescaleApiClientConfig, "fetchApi">): FetchAPI;
50
+ static getAccountId(config: Pick<BytescaleApiClientConfig, "apiKey">): string;
51
51
  static validate(config: BytescaleApiClientConfig): void;
52
52
  }
53
53
  /**
@@ -56,11 +56,13 @@ export declare class BytescaleApiClientConfigUtils {
56
56
  export declare class BaseAPI {
57
57
  protected readonly config: BytescaleApiClientConfig;
58
58
  constructor(config: BytescaleApiClientConfig);
59
- protected request(context: RequestOpts, initOverrides: RequestInit | InitOverrideFunction | undefined, baseUrlOverride: string | undefined): Promise<Response>;
60
59
  /**
61
60
  * Returns a successful response (2**) else throws an error.
62
61
  */
63
- protected doFetch(url: string, init: RequestInit, isBytescaleApi: boolean): Promise<Response>;
62
+ static fetch(url: string, init: RequestInit, config: Pick<BytescaleApiClientConfig, "fetchApi"> & {
63
+ isBytescaleApi: boolean;
64
+ }): Promise<Response>;
65
+ protected request(context: RequestOpts, initOverrides: RequestInit | InitOverrideFunction | undefined, baseUrlOverride: string | undefined): Promise<Response>;
64
66
  protected encodeParam(paramName: string, paramValue: string): string;
65
67
  private createFetchParams;
66
68
  }
@@ -0,0 +1,8 @@
1
+ import { UploadSourceProcessedWorker } from "../../private/model/UploadSourceProcessed";
2
+ import { UploadPart } from "../shared/generated";
3
+ import { PutUploadPartResult } from "../../private/model/PutUploadPartResult";
4
+ import { AddCancellationHandler } from "../../private/model/AddCancellationHandler";
5
+ import { UploadManagerBrowserWorkerBase } from "../../private/UploadManagerBrowserWorkerBase";
6
+ export declare class UploadManager extends UploadManagerBrowserWorkerBase {
7
+ protected doPutUploadPart(part: UploadPart, contentLength: number, source: UploadSourceProcessedWorker, _onProgress: (bytesSentDelta: number) => void, addCancellationHandler: AddCancellationHandler): Promise<PutUploadPartResult>;
8
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./UploadManagerWorker";
2
+ export * from "../node/AuthManagerNode";