@roarkanalytics/sdk 2.21.1 → 2.22.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/README.md +78 -46
- package/api-promise.d.mts +1 -46
- package/api-promise.d.mts.map +1 -1
- package/api-promise.d.ts +1 -46
- package/api-promise.d.ts.map +1 -1
- package/api-promise.js +3 -81
- package/api-promise.js.map +1 -1
- package/api-promise.mjs +1 -79
- package/api-promise.mjs.map +1 -1
- package/client.d.mts +25 -30
- package/client.d.mts.map +1 -1
- package/client.d.ts +25 -30
- package/client.d.ts.map +1 -1
- package/client.js +150 -132
- package/client.js.map +1 -1
- package/client.mjs +145 -104
- package/client.mjs.map +1 -1
- package/core/api-promise.d.mts +46 -0
- package/core/api-promise.d.mts.map +1 -0
- package/core/api-promise.d.ts +46 -0
- package/core/api-promise.d.ts.map +1 -0
- package/core/api-promise.js +74 -0
- package/core/api-promise.js.map +1 -0
- package/core/api-promise.mjs +70 -0
- package/core/api-promise.mjs.map +1 -0
- package/core/error.d.mts +46 -0
- package/core/error.d.mts.map +1 -0
- package/core/error.d.ts +46 -0
- package/core/error.d.ts.map +1 -0
- package/core/error.js +113 -0
- package/core/error.js.map +1 -0
- package/core/error.mjs +97 -0
- package/core/error.mjs.map +1 -0
- package/core/resource.d.mts +6 -0
- package/core/resource.d.mts.map +1 -0
- package/core/resource.d.ts +6 -0
- package/core/resource.d.ts.map +1 -0
- package/core/resource.js +11 -0
- package/core/resource.js.map +1 -0
- package/core/resource.mjs +7 -0
- package/core/resource.mjs.map +1 -0
- package/core/uploads.d.mts +3 -0
- package/core/uploads.d.mts.map +1 -0
- package/core/uploads.d.ts +3 -0
- package/core/uploads.d.ts.map +1 -0
- package/core/uploads.js +6 -0
- package/core/uploads.js.map +1 -0
- package/core/uploads.mjs +2 -0
- package/core/uploads.mjs.map +1 -0
- package/error.d.mts +1 -46
- package/error.d.mts.map +1 -1
- package/error.d.ts +1 -46
- package/error.d.ts.map +1 -1
- package/error.js +3 -110
- package/error.js.map +1 -1
- package/error.mjs +1 -96
- package/error.mjs.map +1 -1
- package/index.d.mts +6 -6
- package/index.d.ts +3 -3
- package/index.js +3 -3
- package/index.js.map +1 -1
- package/index.mjs +3 -3
- package/internal/builtin-types.d.mts +11 -3
- package/internal/builtin-types.d.mts.map +1 -1
- package/internal/builtin-types.d.ts +11 -3
- package/internal/builtin-types.d.ts.map +1 -1
- package/internal/detect-platform.js +3 -3
- package/internal/detect-platform.js.map +1 -1
- package/internal/detect-platform.mjs +3 -3
- package/internal/detect-platform.mjs.map +1 -1
- package/internal/errors.d.mts.map +1 -1
- package/internal/errors.d.ts.map +1 -1
- package/internal/errors.js +17 -2
- package/internal/errors.js.map +1 -1
- package/internal/errors.mjs +15 -0
- package/internal/errors.mjs.map +1 -1
- package/internal/headers.d.mts +1 -2
- package/internal/headers.d.mts.map +1 -1
- package/internal/headers.d.ts +1 -2
- package/internal/headers.d.ts.map +1 -1
- package/internal/headers.js +7 -5
- package/internal/headers.js.map +1 -1
- package/internal/headers.mjs +7 -5
- package/internal/headers.mjs.map +1 -1
- package/internal/parse.d.mts +3 -1
- package/internal/parse.d.mts.map +1 -1
- package/internal/parse.d.ts +3 -1
- package/internal/parse.d.ts.map +1 -1
- package/internal/parse.js +33 -21
- package/internal/parse.js.map +1 -1
- package/internal/parse.mjs +33 -20
- package/internal/parse.mjs.map +1 -1
- package/internal/qs/formats.d.mts +1 -0
- package/internal/qs/formats.d.mts.map +1 -1
- package/internal/qs/formats.d.ts +1 -0
- package/internal/qs/formats.d.ts.map +1 -1
- package/internal/qs/formats.js +4 -2
- package/internal/qs/formats.js.map +1 -1
- package/internal/qs/formats.mjs +2 -1
- package/internal/qs/formats.mjs.map +1 -1
- package/internal/qs/stringify.d.mts.map +1 -1
- package/internal/qs/stringify.d.ts.map +1 -1
- package/internal/qs/stringify.js +16 -19
- package/internal/qs/stringify.js.map +1 -1
- package/internal/qs/stringify.mjs +17 -19
- package/internal/qs/stringify.mjs.map +1 -1
- package/internal/qs/utils.d.mts +1 -0
- package/internal/qs/utils.d.mts.map +1 -1
- package/internal/qs/utils.d.ts +1 -0
- package/internal/qs/utils.d.ts.map +1 -1
- package/internal/qs/utils.js +22 -21
- package/internal/qs/utils.js.map +1 -1
- package/internal/qs/utils.mjs +12 -12
- package/internal/qs/utils.mjs.map +1 -1
- package/internal/request-options.d.mts +43 -2
- package/internal/request-options.d.mts.map +1 -1
- package/internal/request-options.d.ts +43 -2
- package/internal/request-options.d.ts.map +1 -1
- package/internal/request-options.js +1 -26
- package/internal/request-options.js.map +1 -1
- package/internal/request-options.mjs +0 -24
- package/internal/request-options.mjs.map +1 -1
- package/internal/shim-types.d.mts +11 -22
- package/internal/shim-types.d.mts.map +1 -0
- package/internal/shim-types.d.ts +11 -22
- package/internal/shim-types.d.ts.map +1 -0
- package/internal/shim-types.js +4 -0
- package/internal/shim-types.js.map +1 -0
- package/internal/shim-types.mjs +3 -0
- package/internal/shim-types.mjs.map +1 -0
- package/internal/shims.d.mts +7 -48
- package/internal/shims.d.mts.map +1 -1
- package/internal/shims.d.ts +7 -48
- package/internal/shims.d.ts.map +1 -1
- package/internal/shims.js +21 -30
- package/internal/shims.js.map +1 -1
- package/internal/shims.mjs +16 -23
- package/internal/shims.mjs.map +1 -1
- package/internal/to-file.d.mts +45 -0
- package/internal/to-file.d.mts.map +1 -0
- package/internal/to-file.d.ts +45 -0
- package/internal/to-file.d.ts.map +1 -0
- package/internal/to-file.js +91 -0
- package/internal/to-file.js.map +1 -0
- package/internal/to-file.mjs +88 -0
- package/internal/to-file.mjs.map +1 -0
- package/internal/tslib.js +81 -0
- package/internal/tslib.mjs +17 -0
- package/internal/types.d.mts +14 -13
- package/internal/types.d.mts.map +1 -1
- package/internal/types.d.ts +14 -13
- package/internal/types.d.ts.map +1 -1
- package/internal/uploads.d.mts +16 -47
- package/internal/uploads.d.mts.map +1 -1
- package/internal/uploads.d.ts +16 -47
- package/internal/uploads.d.ts.map +1 -1
- package/internal/uploads.js +41 -108
- package/internal/uploads.js.map +1 -1
- package/internal/uploads.mjs +39 -108
- package/internal/uploads.mjs.map +1 -1
- package/internal/utils/base64.d.mts.map +1 -1
- package/internal/utils/base64.d.ts.map +1 -1
- package/internal/utils/base64.js +13 -8
- package/internal/utils/base64.js.map +1 -1
- package/internal/utils/base64.mjs +13 -8
- package/internal/utils/base64.mjs.map +1 -1
- package/internal/utils/bytes.d.mts +4 -0
- package/internal/utils/bytes.d.mts.map +1 -0
- package/internal/utils/bytes.d.ts +4 -0
- package/internal/utils/bytes.d.ts.map +1 -0
- package/internal/utils/bytes.js +31 -0
- package/internal/utils/bytes.js.map +1 -0
- package/internal/utils/bytes.mjs +26 -0
- package/internal/utils/bytes.mjs.map +1 -0
- package/internal/utils/env.d.mts.map +1 -1
- package/internal/utils/env.d.ts.map +1 -1
- package/internal/utils/env.js.map +1 -1
- package/internal/utils/env.mjs.map +1 -1
- package/internal/utils/log.d.mts +35 -2
- package/internal/utils/log.d.mts.map +1 -1
- package/internal/utils/log.d.ts +35 -2
- package/internal/utils/log.d.ts.map +1 -1
- package/internal/utils/log.js +62 -24
- package/internal/utils/log.js.map +1 -1
- package/internal/utils/log.mjs +58 -22
- package/internal/utils/log.mjs.map +1 -1
- package/internal/utils/path.d.mts +15 -0
- package/internal/utils/path.d.mts.map +1 -0
- package/internal/utils/path.d.ts +15 -0
- package/internal/utils/path.d.ts.map +1 -0
- package/internal/utils/path.js +79 -0
- package/internal/utils/path.js.map +1 -0
- package/internal/utils/path.mjs +74 -0
- package/internal/utils/path.mjs.map +1 -0
- package/internal/utils/sleep.d.mts.map +1 -1
- package/internal/utils/sleep.d.ts.map +1 -1
- package/internal/utils/uuid.d.mts +1 -1
- package/internal/utils/uuid.d.mts.map +1 -1
- package/internal/utils/uuid.d.ts +1 -1
- package/internal/utils/uuid.d.ts.map +1 -1
- package/internal/utils/uuid.js +10 -6
- package/internal/utils/uuid.js.map +1 -1
- package/internal/utils/uuid.mjs +9 -5
- package/internal/utils/uuid.mjs.map +1 -1
- package/internal/utils/values.d.mts +4 -1
- package/internal/utils/values.d.mts.map +1 -1
- package/internal/utils/values.d.ts +4 -1
- package/internal/utils/values.d.ts.map +1 -1
- package/internal/utils/values.js +21 -9
- package/internal/utils/values.js.map +1 -1
- package/internal/utils/values.mjs +14 -4
- package/internal/utils/values.mjs.map +1 -1
- package/internal/utils.js +7 -20
- package/internal/utils.js.map +1 -1
- package/package.json +31 -13
- package/resource.d.mts +1 -5
- package/resource.d.mts.map +1 -1
- package/resource.d.ts +1 -5
- package/resource.d.ts.map +1 -1
- package/resource.js +3 -8
- package/resource.js.map +1 -1
- package/resource.mjs +1 -6
- package/resource.mjs.map +1 -1
- package/resources/call.d.mts +17 -7
- package/resources/call.d.mts.map +1 -1
- package/resources/call.d.ts +17 -7
- package/resources/call.d.ts.map +1 -1
- package/resources/call.js +6 -5
- package/resources/call.js.map +1 -1
- package/resources/call.mjs +6 -5
- package/resources/call.mjs.map +1 -1
- package/resources/evaluation.d.mts +20 -8
- package/resources/evaluation.d.mts.map +1 -1
- package/resources/evaluation.d.ts +20 -8
- package/resources/evaluation.d.ts.map +1 -1
- package/resources/evaluation.js +5 -4
- package/resources/evaluation.js.map +1 -1
- package/resources/evaluation.mjs +5 -4
- package/resources/evaluation.mjs.map +1 -1
- package/resources/health.d.mts +2 -2
- package/resources/health.d.ts +2 -2
- package/resources/health.js +1 -1
- package/resources/health.js.map +1 -1
- package/resources/health.mjs +1 -1
- package/resources/integrations.d.mts +39 -6
- package/resources/integrations.d.mts.map +1 -1
- package/resources/integrations.d.ts +39 -6
- package/resources/integrations.d.ts.map +1 -1
- package/resources/integrations.js +26 -1
- package/resources/integrations.js.map +1 -1
- package/resources/integrations.mjs +26 -1
- package/resources/integrations.mjs.map +1 -1
- package/resources/metric.d.mts +2 -2
- package/resources/metric.d.ts +2 -2
- package/resources/metric.js +1 -1
- package/resources/metric.js.map +1 -1
- package/resources/metric.mjs +1 -1
- package/resources/persona.d.mts +45 -8
- package/resources/persona.d.mts.map +1 -1
- package/resources/persona.d.ts +45 -8
- package/resources/persona.d.ts.map +1 -1
- package/resources/persona.js +29 -3
- package/resources/persona.js.map +1 -1
- package/resources/persona.mjs +29 -3
- package/resources/persona.mjs.map +1 -1
- package/resources/simulation.d.mts +51 -5
- package/resources/simulation.d.mts.map +1 -1
- package/resources/simulation.d.ts +51 -5
- package/resources/simulation.d.ts.map +1 -1
- package/resources/simulation.js +45 -4
- package/resources/simulation.js.map +1 -1
- package/resources/simulation.mjs +45 -4
- package/resources/simulation.mjs.map +1 -1
- package/resources.d.mts +2 -0
- package/resources.d.mts.map +1 -0
- package/resources.d.ts +2 -0
- package/resources.d.ts.map +1 -0
- package/resources.js +5 -0
- package/resources.js.map +1 -0
- package/resources.mjs +2 -0
- package/resources.mjs.map +1 -0
- package/src/api-promise.ts +2 -92
- package/src/client.ts +207 -116
- package/src/core/README.md +3 -0
- package/src/core/api-promise.ts +92 -0
- package/src/core/error.ts +130 -0
- package/src/core/resource.ts +11 -0
- package/src/core/uploads.ts +2 -0
- package/src/error.ts +2 -130
- package/src/index.ts +3 -3
- package/src/internal/README.md +3 -0
- package/src/internal/builtin-types.ts +14 -0
- package/src/internal/detect-platform.ts +3 -3
- package/src/internal/errors.ts +11 -0
- package/src/internal/headers.ts +9 -8
- package/src/internal/parse.ts +43 -28
- package/src/internal/qs/formats.ts +2 -1
- package/src/internal/qs/stringify.ts +17 -20
- package/src/internal/qs/utils.ts +14 -14
- package/src/internal/request-options.ts +55 -31
- package/src/internal/shim-types.ts +26 -0
- package/src/internal/shims.ts +20 -58
- package/src/internal/to-file.ts +154 -0
- package/src/internal/types.ts +14 -17
- package/src/internal/uploads.ts +60 -180
- package/src/internal/utils/base64.ts +14 -11
- package/src/internal/utils/bytes.ts +32 -0
- package/src/internal/utils/log.ts +100 -23
- package/src/internal/utils/path.ts +88 -0
- package/src/internal/utils/uuid.ts +10 -6
- package/src/internal/utils/values.ts +16 -5
- package/src/resource.ts +2 -11
- package/src/resources/call.ts +12 -11
- package/src/resources/evaluation.ts +12 -11
- package/src/resources/health.ts +2 -2
- package/src/resources/integrations.ts +31 -6
- package/src/resources/metric.ts +2 -2
- package/src/resources/persona.ts +36 -10
- package/src/resources/simulation.ts +49 -8
- package/src/resources.ts +1 -0
- package/src/uploads.ts +2 -1
- package/src/version.ts +1 -1
- package/uploads.d.mts +1 -1
- package/uploads.d.mts.map +1 -1
- package/uploads.d.ts +1 -1
- package/uploads.d.ts.map +1 -1
- package/uploads.js +3 -3
- package/uploads.js.map +1 -1
- package/uploads.mjs +1 -1
- package/uploads.mjs.map +1 -1
- package/version.d.mts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
- package/internal/polyfill/crypto.node.d.ts +0 -10
- package/internal/polyfill/crypto.node.js +0 -11
- package/internal/polyfill/crypto.node.mjs +0 -2
- package/internal/polyfill/file.node.d.ts +0 -9
- package/internal/polyfill/file.node.js +0 -17
- package/internal/polyfill/file.node.mjs +0 -9
- package/src/internal/polyfill/crypto.node.d.ts +0 -10
- package/src/internal/polyfill/crypto.node.js +0 -11
- package/src/internal/polyfill/crypto.node.mjs +0 -2
- package/src/internal/polyfill/file.node.d.ts +0 -9
- package/src/internal/polyfill/file.node.js +0 -17
- package/src/internal/polyfill/file.node.mjs +0 -9
- package/src/internal/shim-types.d.ts +0 -28
package/internal/uploads.d.ts
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
1
|
import { type RequestOptions } from "./request-options.js";
|
|
4
2
|
import type { FilePropertyBag, Fetch } from "./builtin-types.js";
|
|
5
|
-
import { type FsReadStreamLike } from "./shims.js";
|
|
6
3
|
import type { Roark } from "../client.js";
|
|
7
|
-
|
|
8
|
-
type
|
|
9
|
-
|
|
4
|
+
export type BlobPart = string | ArrayBuffer | ArrayBufferView | Blob | DataView;
|
|
5
|
+
type FsReadStream = AsyncIterable<Uint8Array> & {
|
|
6
|
+
path: string | {
|
|
7
|
+
toString(): string;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
interface BunFile extends Blob {
|
|
11
|
+
readonly name?: string | undefined;
|
|
12
|
+
}
|
|
13
|
+
export declare const checkFileSupport: () => void;
|
|
10
14
|
/**
|
|
11
15
|
* Typically, this is a native "File" class.
|
|
12
16
|
*
|
|
@@ -16,49 +20,14 @@ type BlobPart = string | ArrayBuffer | ArrayBufferView | Blob | DataView;
|
|
|
16
20
|
* For convenience, you can also pass a fetch Response, or in Node,
|
|
17
21
|
* the result of fs.createReadStream().
|
|
18
22
|
*/
|
|
19
|
-
export type Uploadable =
|
|
20
|
-
/**
|
|
21
|
-
* Intended to match DOM Blob, node-fetch Blob, node:buffer Blob, etc.
|
|
22
|
-
* Don't add arrayBuffer here, node-fetch doesn't have it
|
|
23
|
-
*/
|
|
24
|
-
interface BlobLike {
|
|
25
|
-
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/size) */
|
|
26
|
-
readonly size: number;
|
|
27
|
-
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/type) */
|
|
28
|
-
readonly type: string;
|
|
29
|
-
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/text) */
|
|
30
|
-
text(): Promise<string>;
|
|
31
|
-
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/slice) */
|
|
32
|
-
slice(start?: number, end?: number): BlobLike;
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Intended to match DOM File, node:buffer File, undici File, etc.
|
|
36
|
-
*/
|
|
37
|
-
interface FileLike extends BlobLike {
|
|
38
|
-
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/lastModified) */
|
|
39
|
-
readonly lastModified: number;
|
|
40
|
-
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/name) */
|
|
41
|
-
readonly name: string;
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Intended to match DOM Response, node-fetch Response, undici Response, etc.
|
|
45
|
-
*/
|
|
46
|
-
export interface ResponseLike {
|
|
47
|
-
url: string;
|
|
48
|
-
blob(): Promise<BlobLike>;
|
|
49
|
-
}
|
|
50
|
-
type ToFileInput = Uploadable | Exclude<BlobLikePart, string> | AsyncIterable<BlobLikePart>;
|
|
23
|
+
export type Uploadable = File | Response | FsReadStream | BunFile;
|
|
51
24
|
/**
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
* @param {string=} name the name of the file. If omitted, toFile will try to determine a file name from bits if possible
|
|
55
|
-
* @param {Object=} options additional properties
|
|
56
|
-
* @param {string=} options.type the MIME type of the content
|
|
57
|
-
* @param {number=} options.lastModified the last modified timestamp
|
|
58
|
-
* @returns a {@link File} with the given properties
|
|
25
|
+
* Construct a `File` instance. This is used to ensure a helpful error is thrown
|
|
26
|
+
* for environments that don't define a global `File` yet.
|
|
59
27
|
*/
|
|
60
|
-
export declare function
|
|
61
|
-
export declare function
|
|
28
|
+
export declare function makeFile(fileBits: BlobPart[], fileName: string | undefined, options?: FilePropertyBag): File;
|
|
29
|
+
export declare function getName(value: any): string | undefined;
|
|
30
|
+
export declare const isAsyncIterable: (value: any) => value is AsyncIterable<any>;
|
|
62
31
|
/**
|
|
63
32
|
* Returns a multipart/form-data request if any part of the given request body contains a File / Blob value.
|
|
64
33
|
* Otherwise returns the request as is.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"uploads.d.ts","sourceRoot":"","sources":["../src/internal/uploads.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"uploads.d.ts","sourceRoot":"","sources":["../src/internal/uploads.ts"],"names":[],"mappings":"OAAO,EAAE,KAAK,cAAc,EAAE;OACvB,KAAK,EAAE,eAAe,EAAE,KAAK,EAAE;OAC/B,KAAK,EAAE,KAAK,EAAE;AAGrB,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG,WAAW,GAAG,eAAe,GAAG,IAAI,G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ASlB,CAAC"}
|
package/internal/uploads.js
CHANGED
|
@@ -1,119 +1,41 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createForm = exports.multipartFormRequestOptions = exports.maybeMultipartFormRequestOptions = exports.
|
|
3
|
+
exports.createForm = exports.multipartFormRequestOptions = exports.maybeMultipartFormRequestOptions = exports.isAsyncIterable = exports.checkFileSupport = void 0;
|
|
4
|
+
exports.makeFile = makeFile;
|
|
5
|
+
exports.getName = getName;
|
|
4
6
|
const shims_1 = require("./shims.js");
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
typeof value.slice === 'function' &&
|
|
15
|
-
typeof value.arrayBuffer === 'function';
|
|
16
|
-
/**
|
|
17
|
-
* This check adds the arrayBuffer() method type because it is available and used at runtime
|
|
18
|
-
*/
|
|
19
|
-
const isFileLike = (value) => value != null &&
|
|
20
|
-
typeof value === 'object' &&
|
|
21
|
-
typeof value.name === 'string' &&
|
|
22
|
-
typeof value.lastModified === 'number' &&
|
|
23
|
-
isBlobLike(value);
|
|
24
|
-
const isResponseLike = (value) => value != null &&
|
|
25
|
-
typeof value === 'object' &&
|
|
26
|
-
typeof value.url === 'string' &&
|
|
27
|
-
typeof value.blob === 'function';
|
|
28
|
-
const isUploadable = (value) => {
|
|
29
|
-
return isFileLike(value) || isResponseLike(value) || (0, shims_1.isFsReadStreamLike)(value);
|
|
7
|
+
const checkFileSupport = () => {
|
|
8
|
+
if (typeof File === 'undefined') {
|
|
9
|
+
const { process } = globalThis;
|
|
10
|
+
const isOldNode = typeof process?.versions?.node === 'string' && parseInt(process.versions.node.split('.')) < 20;
|
|
11
|
+
throw new Error('`File` is not defined as a global, which is required for file uploads.' +
|
|
12
|
+
(isOldNode ?
|
|
13
|
+
" Update to Node 20 LTS or newer, or set `globalThis.File` to `import('node:buffer').File`."
|
|
14
|
+
: ''));
|
|
15
|
+
}
|
|
30
16
|
};
|
|
17
|
+
exports.checkFileSupport = checkFileSupport;
|
|
31
18
|
/**
|
|
32
19
|
* Construct a `File` instance. This is used to ensure a helpful error is thrown
|
|
33
|
-
* for environments that don't define a global `File` yet
|
|
34
|
-
* accidentally rely on a global `File` type in our annotations.
|
|
20
|
+
* for environments that don't define a global `File` yet.
|
|
35
21
|
*/
|
|
36
22
|
function makeFile(fileBits, fileName, options) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
throw new Error('`File` is not defined as a global which is required for file uploads');
|
|
40
|
-
}
|
|
41
|
-
return new File(fileBits, fileName, options);
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Helper for creating a {@link File} to pass to an SDK upload method from a variety of different data formats
|
|
45
|
-
* @param value the raw content of the file. Can be an {@link Uploadable}, {@link BlobLikePart}, or {@link AsyncIterable} of {@link BlobLikePart}s
|
|
46
|
-
* @param {string=} name the name of the file. If omitted, toFile will try to determine a file name from bits if possible
|
|
47
|
-
* @param {Object=} options additional properties
|
|
48
|
-
* @param {string=} options.type the MIME type of the content
|
|
49
|
-
* @param {number=} options.lastModified the last modified timestamp
|
|
50
|
-
* @returns a {@link File} with the given properties
|
|
51
|
-
*/
|
|
52
|
-
async function toFile(value, name, options) {
|
|
53
|
-
// If it's a promise, resolve it.
|
|
54
|
-
value = await value;
|
|
55
|
-
// If we've been given a `File` we don't need to do anything
|
|
56
|
-
if (isFileLike(value)) {
|
|
57
|
-
const File = globalThis.File;
|
|
58
|
-
if (File && value instanceof File) {
|
|
59
|
-
return value;
|
|
60
|
-
}
|
|
61
|
-
return makeFile([await value.arrayBuffer()], value.name);
|
|
62
|
-
}
|
|
63
|
-
if (isResponseLike(value)) {
|
|
64
|
-
const blob = await value.blob();
|
|
65
|
-
name || (name = new URL(value.url).pathname.split(/[\\/]/).pop() ?? 'unknown_file');
|
|
66
|
-
return makeFile(await getBytes(blob), name, options);
|
|
67
|
-
}
|
|
68
|
-
const parts = await getBytes(value);
|
|
69
|
-
name || (name = getName(value) ?? 'unknown_file');
|
|
70
|
-
if (!options?.type) {
|
|
71
|
-
const type = parts.find((part) => typeof part === 'object' && 'type' in part && part.type);
|
|
72
|
-
if (typeof type === 'string') {
|
|
73
|
-
options = { ...options, type };
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
return makeFile(parts, name, options);
|
|
77
|
-
}
|
|
78
|
-
exports.toFile = toFile;
|
|
79
|
-
async function getBytes(value) {
|
|
80
|
-
let parts = [];
|
|
81
|
-
if (typeof value === 'string' ||
|
|
82
|
-
ArrayBuffer.isView(value) || // includes Uint8Array, Buffer, etc.
|
|
83
|
-
value instanceof ArrayBuffer) {
|
|
84
|
-
parts.push(value);
|
|
85
|
-
}
|
|
86
|
-
else if (isBlobLike(value)) {
|
|
87
|
-
parts.push(value instanceof Blob ? value : await value.arrayBuffer());
|
|
88
|
-
}
|
|
89
|
-
else if (isAsyncIterableIterator(value) // includes Readable, ReadableStream, etc.
|
|
90
|
-
) {
|
|
91
|
-
for await (const chunk of value) {
|
|
92
|
-
parts.push(...(await getBytes(chunk))); // TODO, consider validating?
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
else {
|
|
96
|
-
const constructor = value?.constructor?.name;
|
|
97
|
-
throw new Error(`Unexpected data type: ${typeof value}${constructor ? `; constructor: ${constructor}` : ''}${propsForError(value)}`);
|
|
98
|
-
}
|
|
99
|
-
return parts;
|
|
100
|
-
}
|
|
101
|
-
exports.getBytes = getBytes;
|
|
102
|
-
function propsForError(value) {
|
|
103
|
-
if (typeof value !== 'object' || value === null)
|
|
104
|
-
return '';
|
|
105
|
-
const props = Object.getOwnPropertyNames(value);
|
|
106
|
-
return `; props: [${props.map((p) => `"${p}"`).join(', ')}]`;
|
|
23
|
+
(0, exports.checkFileSupport)();
|
|
24
|
+
return new File(fileBits, fileName ?? 'unknown_file', options);
|
|
107
25
|
}
|
|
108
26
|
function getName(value) {
|
|
109
|
-
return ((typeof value === 'object' &&
|
|
27
|
+
return (((typeof value === 'object' &&
|
|
110
28
|
value !== null &&
|
|
111
|
-
(('name' in value && String(value.name)) ||
|
|
112
|
-
('
|
|
113
|
-
('
|
|
114
|
-
|
|
29
|
+
(('name' in value && value.name && String(value.name)) ||
|
|
30
|
+
('url' in value && value.url && String(value.url)) ||
|
|
31
|
+
('filename' in value && value.filename && String(value.filename)) ||
|
|
32
|
+
('path' in value && value.path && String(value.path)))) ||
|
|
33
|
+
'')
|
|
34
|
+
.split(/[\\/]/)
|
|
35
|
+
.pop() || undefined);
|
|
115
36
|
}
|
|
116
|
-
const
|
|
37
|
+
const isAsyncIterable = (value) => value != null && typeof value === 'object' && typeof value[Symbol.asyncIterator] === 'function';
|
|
38
|
+
exports.isAsyncIterable = isAsyncIterable;
|
|
117
39
|
/**
|
|
118
40
|
* Returns a multipart/form-data request if any part of the given request body contains a File / Blob value.
|
|
119
41
|
* Otherwise returns the request as is.
|
|
@@ -128,7 +50,7 @@ const multipartFormRequestOptions = async (opts, fetch) => {
|
|
|
128
50
|
return { ...opts, body: await (0, exports.createForm)(opts.body, fetch) };
|
|
129
51
|
};
|
|
130
52
|
exports.multipartFormRequestOptions = multipartFormRequestOptions;
|
|
131
|
-
const supportsFormDataMap = new WeakMap();
|
|
53
|
+
const supportsFormDataMap = /* @__PURE__ */ new WeakMap();
|
|
132
54
|
/**
|
|
133
55
|
* node-fetch doesn't support the global FormData object in recent node versions. Instead of sending
|
|
134
56
|
* properly-encoded form data, it just stringifies the object, resulting in a request body of "[object FormData]".
|
|
@@ -168,6 +90,12 @@ const createForm = async (body, fetch) => {
|
|
|
168
90
|
return form;
|
|
169
91
|
};
|
|
170
92
|
exports.createForm = createForm;
|
|
93
|
+
// We check for Blob not File because Bun.File doesn't inherit from File,
|
|
94
|
+
// but they both inherit from Blob and have a `name` property at runtime.
|
|
95
|
+
const isNamedBlob = (value) => value instanceof Blob && 'name' in value;
|
|
96
|
+
const isUploadable = (value) => typeof value === 'object' &&
|
|
97
|
+
value !== null &&
|
|
98
|
+
(value instanceof Response || (0, exports.isAsyncIterable)(value) || isNamedBlob(value));
|
|
171
99
|
const hasUploadableValue = (value) => {
|
|
172
100
|
if (isUploadable(value))
|
|
173
101
|
return true;
|
|
@@ -191,9 +119,14 @@ const addFormValue = async (form, key, value) => {
|
|
|
191
119
|
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
|
192
120
|
form.append(key, String(value));
|
|
193
121
|
}
|
|
194
|
-
else if (
|
|
195
|
-
|
|
196
|
-
|
|
122
|
+
else if (value instanceof Response) {
|
|
123
|
+
form.append(key, makeFile([await value.blob()], getName(value)));
|
|
124
|
+
}
|
|
125
|
+
else if ((0, exports.isAsyncIterable)(value)) {
|
|
126
|
+
form.append(key, makeFile([await new Response((0, shims_1.ReadableStreamFrom)(value)).blob()], getName(value)));
|
|
127
|
+
}
|
|
128
|
+
else if (isNamedBlob(value)) {
|
|
129
|
+
form.append(key, value, getName(value));
|
|
197
130
|
}
|
|
198
131
|
else if (Array.isArray(value)) {
|
|
199
132
|
await Promise.all(value.map((entry) => addFormValue(form, key + '[]', entry)));
|
package/internal/uploads.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"uploads.js","sourceRoot":"","sources":["../src/internal/uploads.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"uploads.js","sourceRoot":"","sources":["../src/internal/uploads.ts"],"names":[],"mappings":";;;AA0CA,4BAOC;AAED,0BAcC;AA9DD,sCAA6C;AAUtC,MAAM,gBAAgB,GAAG,GAAG,EAAE;IACnC,IAAI,OAAO,IAAI,KAAK,WAAW,EAAE,CAAC;QAChC,MAAM,EAAE,OAAO,EAAE,GAAG,UAAiB,CAAC;QACtC,MAAM,SAAS,GACb,OAAO,OAAO,EAAE,QAAQ,EAAE,IAAI,KAAK,QAAQ,IAAI,QAAQ,CAAC,OAAO,CAAC,QAAQ,CAA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AK,CAAC,EAAE,CAAC;QAClC,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC,MAAM,IAAI,QAAQ,CAAC,IAAA,0BAAkB,EAAC,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IACrG,CAAC;SAAM,IAAI,WAAW,CAAC,KAAK,CAAC,EAAE,CAAC;QAC9B,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;IAC1C,CAAC;SAAM,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QAChC,MAAM,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,GAAG,GAAG,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;IACjF,CAAC;SAAM,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QACrC,MAAM,OAAO,CAAC,GAAG,CACf,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,GAAG,GAAG,IAAI,IAAI,GAAG,EAAE,IAAI,CAAC,CAAC,CACzF,CAAC;IACJ,CAAC;SAAM,CAAC;QACN,MAAM,IAAI,SAAS,CACjB,wGAAwG,KAAK,UAAU,CACxH,CAAC;IACJ,CAAC;AACH,CAAC,CAAC"}
|
package/internal/uploads.mjs
CHANGED
|
@@ -1,114 +1,34 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* This check adds the arrayBuffer() method type because it is available and used at runtime
|
|
5
|
-
*/
|
|
6
|
-
const isBlobLike = (value) => value != null &&
|
|
7
|
-
typeof value === 'object' &&
|
|
8
|
-
typeof value.size === 'number' &&
|
|
9
|
-
typeof value.type === 'string' &&
|
|
10
|
-
typeof value.text === 'function' &&
|
|
11
|
-
typeof value.slice === 'function' &&
|
|
12
|
-
typeof value.arrayBuffer === 'function';
|
|
13
|
-
/**
|
|
14
|
-
* This check adds the arrayBuffer() method type because it is available and used at runtime
|
|
15
|
-
*/
|
|
16
|
-
const isFileLike = (value) => value != null &&
|
|
17
|
-
typeof value === 'object' &&
|
|
18
|
-
typeof value.name === 'string' &&
|
|
19
|
-
typeof value.lastModified === 'number' &&
|
|
20
|
-
isBlobLike(value);
|
|
21
|
-
const isResponseLike = (value) => value != null &&
|
|
22
|
-
typeof value === 'object' &&
|
|
23
|
-
typeof value.url === 'string' &&
|
|
24
|
-
typeof value.blob === 'function';
|
|
25
|
-
const isUploadable = (value) => {
|
|
26
|
-
return isFileLike(value) || isResponseLike(value) || isFsReadStreamLike(value);
|
|
27
|
-
};
|
|
28
|
-
/**
|
|
29
|
-
* Construct a `File` instance. This is used to ensure a helpful error is thrown
|
|
30
|
-
* for environments that don't define a global `File` yet and so that we don't
|
|
31
|
-
* accidentally rely on a global `File` type in our annotations.
|
|
32
|
-
*/
|
|
33
|
-
function makeFile(fileBits, fileName, options) {
|
|
34
|
-
const File = globalThis.File;
|
|
1
|
+
import { ReadableStreamFrom } from "./shims.mjs";
|
|
2
|
+
export const checkFileSupport = () => {
|
|
35
3
|
if (typeof File === 'undefined') {
|
|
36
|
-
|
|
4
|
+
const { process } = globalThis;
|
|
5
|
+
const isOldNode = typeof process?.versions?.node === 'string' && parseInt(process.versions.node.split('.')) < 20;
|
|
6
|
+
throw new Error('`File` is not defined as a global, which is required for file uploads.' +
|
|
7
|
+
(isOldNode ?
|
|
8
|
+
" Update to Node 20 LTS or newer, or set `globalThis.File` to `import('node:buffer').File`."
|
|
9
|
+
: ''));
|
|
37
10
|
}
|
|
38
|
-
|
|
39
|
-
}
|
|
11
|
+
};
|
|
40
12
|
/**
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
* @param {string=} name the name of the file. If omitted, toFile will try to determine a file name from bits if possible
|
|
44
|
-
* @param {Object=} options additional properties
|
|
45
|
-
* @param {string=} options.type the MIME type of the content
|
|
46
|
-
* @param {number=} options.lastModified the last modified timestamp
|
|
47
|
-
* @returns a {@link File} with the given properties
|
|
13
|
+
* Construct a `File` instance. This is used to ensure a helpful error is thrown
|
|
14
|
+
* for environments that don't define a global `File` yet.
|
|
48
15
|
*/
|
|
49
|
-
export
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
// If we've been given a `File` we don't need to do anything
|
|
53
|
-
if (isFileLike(value)) {
|
|
54
|
-
const File = globalThis.File;
|
|
55
|
-
if (File && value instanceof File) {
|
|
56
|
-
return value;
|
|
57
|
-
}
|
|
58
|
-
return makeFile([await value.arrayBuffer()], value.name);
|
|
59
|
-
}
|
|
60
|
-
if (isResponseLike(value)) {
|
|
61
|
-
const blob = await value.blob();
|
|
62
|
-
name || (name = new URL(value.url).pathname.split(/[\\/]/).pop() ?? 'unknown_file');
|
|
63
|
-
return makeFile(await getBytes(blob), name, options);
|
|
64
|
-
}
|
|
65
|
-
const parts = await getBytes(value);
|
|
66
|
-
name || (name = getName(value) ?? 'unknown_file');
|
|
67
|
-
if (!options?.type) {
|
|
68
|
-
const type = parts.find((part) => typeof part === 'object' && 'type' in part && part.type);
|
|
69
|
-
if (typeof type === 'string') {
|
|
70
|
-
options = { ...options, type };
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
return makeFile(parts, name, options);
|
|
16
|
+
export function makeFile(fileBits, fileName, options) {
|
|
17
|
+
checkFileSupport();
|
|
18
|
+
return new File(fileBits, fileName ?? 'unknown_file', options);
|
|
74
19
|
}
|
|
75
|
-
export
|
|
76
|
-
|
|
77
|
-
if (typeof value === 'string' ||
|
|
78
|
-
ArrayBuffer.isView(value) || // includes Uint8Array, Buffer, etc.
|
|
79
|
-
value instanceof ArrayBuffer) {
|
|
80
|
-
parts.push(value);
|
|
81
|
-
}
|
|
82
|
-
else if (isBlobLike(value)) {
|
|
83
|
-
parts.push(value instanceof Blob ? value : await value.arrayBuffer());
|
|
84
|
-
}
|
|
85
|
-
else if (isAsyncIterableIterator(value) // includes Readable, ReadableStream, etc.
|
|
86
|
-
) {
|
|
87
|
-
for await (const chunk of value) {
|
|
88
|
-
parts.push(...(await getBytes(chunk))); // TODO, consider validating?
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
else {
|
|
92
|
-
const constructor = value?.constructor?.name;
|
|
93
|
-
throw new Error(`Unexpected data type: ${typeof value}${constructor ? `; constructor: ${constructor}` : ''}${propsForError(value)}`);
|
|
94
|
-
}
|
|
95
|
-
return parts;
|
|
96
|
-
}
|
|
97
|
-
function propsForError(value) {
|
|
98
|
-
if (typeof value !== 'object' || value === null)
|
|
99
|
-
return '';
|
|
100
|
-
const props = Object.getOwnPropertyNames(value);
|
|
101
|
-
return `; props: [${props.map((p) => `"${p}"`).join(', ')}]`;
|
|
102
|
-
}
|
|
103
|
-
function getName(value) {
|
|
104
|
-
return ((typeof value === 'object' &&
|
|
20
|
+
export function getName(value) {
|
|
21
|
+
return (((typeof value === 'object' &&
|
|
105
22
|
value !== null &&
|
|
106
|
-
(('name' in value && String(value.name)) ||
|
|
107
|
-
('
|
|
108
|
-
('
|
|
109
|
-
|
|
23
|
+
(('name' in value && value.name && String(value.name)) ||
|
|
24
|
+
('url' in value && value.url && String(value.url)) ||
|
|
25
|
+
('filename' in value && value.filename && String(value.filename)) ||
|
|
26
|
+
('path' in value && value.path && String(value.path)))) ||
|
|
27
|
+
'')
|
|
28
|
+
.split(/[\\/]/)
|
|
29
|
+
.pop() || undefined);
|
|
110
30
|
}
|
|
111
|
-
const
|
|
31
|
+
export const isAsyncIterable = (value) => value != null && typeof value === 'object' && typeof value[Symbol.asyncIterator] === 'function';
|
|
112
32
|
/**
|
|
113
33
|
* Returns a multipart/form-data request if any part of the given request body contains a File / Blob value.
|
|
114
34
|
* Otherwise returns the request as is.
|
|
@@ -121,7 +41,7 @@ export const maybeMultipartFormRequestOptions = async (opts, fetch) => {
|
|
|
121
41
|
export const multipartFormRequestOptions = async (opts, fetch) => {
|
|
122
42
|
return { ...opts, body: await createForm(opts.body, fetch) };
|
|
123
43
|
};
|
|
124
|
-
const supportsFormDataMap = new WeakMap();
|
|
44
|
+
const supportsFormDataMap = /* @__PURE__ */ new WeakMap();
|
|
125
45
|
/**
|
|
126
46
|
* node-fetch doesn't support the global FormData object in recent node versions. Instead of sending
|
|
127
47
|
* properly-encoded form data, it just stringifies the object, resulting in a request body of "[object FormData]".
|
|
@@ -160,6 +80,12 @@ export const createForm = async (body, fetch) => {
|
|
|
160
80
|
await Promise.all(Object.entries(body || {}).map(([key, value]) => addFormValue(form, key, value)));
|
|
161
81
|
return form;
|
|
162
82
|
};
|
|
83
|
+
// We check for Blob not File because Bun.File doesn't inherit from File,
|
|
84
|
+
// but they both inherit from Blob and have a `name` property at runtime.
|
|
85
|
+
const isNamedBlob = (value) => value instanceof Blob && 'name' in value;
|
|
86
|
+
const isUploadable = (value) => typeof value === 'object' &&
|
|
87
|
+
value !== null &&
|
|
88
|
+
(value instanceof Response || isAsyncIterable(value) || isNamedBlob(value));
|
|
163
89
|
const hasUploadableValue = (value) => {
|
|
164
90
|
if (isUploadable(value))
|
|
165
91
|
return true;
|
|
@@ -183,9 +109,14 @@ const addFormValue = async (form, key, value) => {
|
|
|
183
109
|
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
|
184
110
|
form.append(key, String(value));
|
|
185
111
|
}
|
|
186
|
-
else if (
|
|
187
|
-
|
|
188
|
-
|
|
112
|
+
else if (value instanceof Response) {
|
|
113
|
+
form.append(key, makeFile([await value.blob()], getName(value)));
|
|
114
|
+
}
|
|
115
|
+
else if (isAsyncIterable(value)) {
|
|
116
|
+
form.append(key, makeFile([await new Response(ReadableStreamFrom(value)).blob()], getName(value)));
|
|
117
|
+
}
|
|
118
|
+
else if (isNamedBlob(value)) {
|
|
119
|
+
form.append(key, value, getName(value));
|
|
189
120
|
}
|
|
190
121
|
else if (Array.isArray(value)) {
|
|
191
122
|
await Promise.all(value.map((entry) => addFormValue(form, key + '[]', entry)));
|
package/internal/uploads.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"uploads.mjs","sourceRoot":"","sources":["../src/internal/uploads.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"uploads.mjs","sourceRoot":"","sources":["../src/internal/uploads.ts"],"names":[],"mappings":"OAGO,EAAE,kBAAkB,EAAE;AAU7B,MAAM,CAAC,MAAM,gBAAgB,GAAG,GAAG,EAAE;IACnC,IAAI,OAAO,IAAI,KAAK,WAAW,EAAE,CAAC;QAChC,MAAM,EAAE,OAAO,EAAE,GAAG,UAAiB,CAAC;QACtC,MAAM,SAAS,GACb,OAAO,OAAO,EAAE,QAAQ,EAAE,IAAI,KAAK,QAAQ,IAAI,QAAQ,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC;QACjG,MAAM,IAAI,KAAK,CACb,wEAAwE;YACtE,CAAC,SAAS,CAAC,CAAC;gBACV,4FAA4F;gBAC9F,CAAC,CAAC,EAAE,CAAC,CACR,CAAC;IACJ,CAAC;AACH,CAAC,CAAC;AAaF;;;GAGG;AACH,MAAM,UAAU,QAAQ,CACtB,QAAoB,EACpB,QAA4B,EAC5B,OAAyB;IAEzB,gBAAgB,EAAE,CAAC;IACnB,OAAO,IAAI,IAAI,CAAC,QAAe,EAAE,QAAQ,IAAI,cAAc,EAAE,OAAO,CAAC,CAAC;AACxE,CAAC;AAED,MAAM,UAAU,OAAO,CAAC,KAAU;IAChC,OAAO,CACL,CACE,CAAC,OAAO,KAAK,KAAK,QAAQ;QACxB,KAAK,KAAK,IAAI;QACd,CAAC,CAAC,MAAM,IAAI,KAAK,IAAI,KAAK,CAAC,IAAI,I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CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,GAAG,GAAG,IAAI,IAAI,GAAG,EAAE,IAAI,CAAC,CAAC,CACzF,CAAC;IACJ,CAAC;SAAM,CAAC;QACN,MAAM,IAAI,SAAS,CACjB,wGAAwG,KAAK,UAAU,CACxH,CAAC;IACJ,CAAC;AACH,CAAC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base64.d.mts","sourceRoot":"","sources":["../../src/internal/utils/base64.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"base64.d.mts","sourceRoot":"","sources":["../../src/internal/utils/base64.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,QAAQ,GAAI,MAAM,MAAM,GAAG,UAAU,GAAG,IAAI,GAAG,SAAS,KAAG,MAgBvE,CAAC;AAEF,eAAO,MAAM,UAAU,GAAI,KAAK,MAAM,KAAG,UAgBxC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base64.d.ts","sourceRoot":"","sources":["../../src/internal/utils/base64.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"base64.d.ts","sourceRoot":"","sources":["../../src/internal/utils/base64.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,QAAQ,GAAI,MAAM,MAAM,GAAG,UAAU,GAAG,IAAI,GAAG,SAAS,KAAG,MAgBvE,CAAC;AAEF,eAAO,MAAM,UAAU,GAAI,KAAK,MAAM,KAAG,UAgBxC,CAAC"}
|
package/internal/utils/base64.js
CHANGED
|
@@ -2,16 +2,17 @@
|
|
|
2
2
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.fromBase64 = exports.toBase64 = void 0;
|
|
5
|
-
const error_1 = require("../../error.js");
|
|
5
|
+
const error_1 = require("../../core/error.js");
|
|
6
|
+
const bytes_1 = require("./bytes.js");
|
|
6
7
|
const toBase64 = (data) => {
|
|
7
8
|
if (!data)
|
|
8
9
|
return '';
|
|
9
|
-
if (typeof data === 'string') {
|
|
10
|
-
data = new globalThis.TextEncoder().encode(data);
|
|
11
|
-
}
|
|
12
10
|
if (typeof globalThis.Buffer !== 'undefined') {
|
|
13
11
|
return globalThis.Buffer.from(data).toString('base64');
|
|
14
12
|
}
|
|
13
|
+
if (typeof data === 'string') {
|
|
14
|
+
data = (0, bytes_1.encodeUTF8)(data);
|
|
15
|
+
}
|
|
15
16
|
if (typeof btoa !== 'undefined') {
|
|
16
17
|
return btoa(String.fromCharCode.apply(null, data));
|
|
17
18
|
}
|
|
@@ -20,12 +21,16 @@ const toBase64 = (data) => {
|
|
|
20
21
|
exports.toBase64 = toBase64;
|
|
21
22
|
const fromBase64 = (str) => {
|
|
22
23
|
if (typeof globalThis.Buffer !== 'undefined') {
|
|
23
|
-
|
|
24
|
+
const buf = globalThis.Buffer.from(str, 'base64');
|
|
25
|
+
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
24
26
|
}
|
|
25
27
|
if (typeof atob !== 'undefined') {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
const bstr = atob(str);
|
|
29
|
+
const buf = new Uint8Array(bstr.length);
|
|
30
|
+
for (let i = 0; i < bstr.length; i++) {
|
|
31
|
+
buf[i] = bstr.charCodeAt(i);
|
|
32
|
+
}
|
|
33
|
+
return buf;
|
|
29
34
|
}
|
|
30
35
|
throw new error_1.RoarkError('Cannot decode base64 string; Expected `Buffer` or `atob` to be defined');
|
|
31
36
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base64.js","sourceRoot":"","sources":["../../src/internal/utils/base64.ts"],"names":[],"mappings":";AAAA,sFAAsF;;;AAEtF,
|
|
1
|
+
{"version":3,"file":"base64.js","sourceRoot":"","s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