@mountra/mountra-sdk 0.1.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/README.md +126 -0
- package/dist/index.cjs +751 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +309 -0
- package/dist/index.d.ts +309 -0
- package/dist/index.js +711 -0
- package/dist/index.js.map +1 -0
- package/package.json +39 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
type ChunkMethod = "none" | "fix";
|
|
2
|
+
type WorkspaceId = number | string;
|
|
3
|
+
declare const JUMBO_CHUNK_THRESHOLD_BYTES: number;
|
|
4
|
+
declare const MIN_S3_MULTIPART_PART_SIZE_BYTES: number;
|
|
5
|
+
declare const DEFAULT_S3_MULTIPART_PART_SIZE_BYTES: number;
|
|
6
|
+
declare const MAX_S3_MULTIPART_PARTS = 10000;
|
|
7
|
+
interface WorkspaceSelector {
|
|
8
|
+
workspaceCuid?: string;
|
|
9
|
+
workspaceId?: WorkspaceId;
|
|
10
|
+
}
|
|
11
|
+
type AccessToken = string | null | undefined | (() => string | null | undefined | Promise<string | null | undefined>);
|
|
12
|
+
interface MountraClientOptions extends WorkspaceSelector {
|
|
13
|
+
/** Mountra server origin, for example `https://mountra.example.com`. */
|
|
14
|
+
baseUrl: string;
|
|
15
|
+
/** API prefix. Set to an empty string when `baseUrl` already includes `/api`. */
|
|
16
|
+
apiPrefix?: string;
|
|
17
|
+
token?: AccessToken;
|
|
18
|
+
/** Additional headers for Mountra API requests. They are never sent to signed URLs. */
|
|
19
|
+
headers?: HeadersInit;
|
|
20
|
+
fetch?: typeof globalThis.fetch;
|
|
21
|
+
/** Adapter for S3 Multipart uploads used by chunks larger than 128 MiB. */
|
|
22
|
+
jumboChunkUploader?: JumboChunkUploader;
|
|
23
|
+
/** Durable local checkpoint store for jumbo Multipart uploads. */
|
|
24
|
+
jumboChunkCheckpointStore?: JumboChunkCheckpointStore;
|
|
25
|
+
/** S3 Multipart part size. Defaults to 16 MiB. */
|
|
26
|
+
jumboPartSize?: number;
|
|
27
|
+
}
|
|
28
|
+
interface UploadFileOptions extends WorkspaceSelector {
|
|
29
|
+
path: string;
|
|
30
|
+
/** Number of logical chunks. Defaults to 1. */
|
|
31
|
+
chunkCount?: number;
|
|
32
|
+
contentType?: string;
|
|
33
|
+
nameConflictStrategy?: "overwrite" | "keep_both";
|
|
34
|
+
signal?: AbortSignal;
|
|
35
|
+
onProgress?: (progress: MountraProgress) => void;
|
|
36
|
+
/** Persist this serializable context before the first chunk is uploaded. */
|
|
37
|
+
onSessionPrepared?: (context: UploadSessionContext) => void | Promise<void>;
|
|
38
|
+
}
|
|
39
|
+
interface UploadResumeOptions extends WorkspaceSelector {
|
|
40
|
+
signal?: AbortSignal;
|
|
41
|
+
onProgress?: (progress: MountraProgress) => void;
|
|
42
|
+
}
|
|
43
|
+
interface MountraProgress {
|
|
44
|
+
phase: "uploading" | "finishing";
|
|
45
|
+
uploadedBytes: number;
|
|
46
|
+
totalBytes: number;
|
|
47
|
+
uploadedChunks: number;
|
|
48
|
+
totalChunks: number;
|
|
49
|
+
uploadedParts?: number;
|
|
50
|
+
totalParts?: number;
|
|
51
|
+
}
|
|
52
|
+
interface FileChunk {
|
|
53
|
+
index: number;
|
|
54
|
+
offset: number;
|
|
55
|
+
size: number;
|
|
56
|
+
blob: Blob;
|
|
57
|
+
}
|
|
58
|
+
interface UploadTarget {
|
|
59
|
+
chunk_index: number;
|
|
60
|
+
storage_key: string;
|
|
61
|
+
put_method?: string;
|
|
62
|
+
put_url?: string;
|
|
63
|
+
put_headers: Record<string, string>;
|
|
64
|
+
size: number;
|
|
65
|
+
upload_required: boolean;
|
|
66
|
+
}
|
|
67
|
+
interface UploadSessionChunkContext {
|
|
68
|
+
index: number;
|
|
69
|
+
offset: number;
|
|
70
|
+
size: number;
|
|
71
|
+
sha256: string;
|
|
72
|
+
}
|
|
73
|
+
interface S3MultipartPart {
|
|
74
|
+
partNumber: number;
|
|
75
|
+
etag: string;
|
|
76
|
+
size: number;
|
|
77
|
+
sha256: string;
|
|
78
|
+
}
|
|
79
|
+
interface JumboChunkCreateInput {
|
|
80
|
+
storageKey: string;
|
|
81
|
+
contentType?: string;
|
|
82
|
+
signal?: AbortSignal;
|
|
83
|
+
}
|
|
84
|
+
interface JumboChunkUploadPartInput {
|
|
85
|
+
storageKey: string;
|
|
86
|
+
uploadId: string;
|
|
87
|
+
partNumber: number;
|
|
88
|
+
body: Blob;
|
|
89
|
+
signal?: AbortSignal;
|
|
90
|
+
}
|
|
91
|
+
interface JumboChunkCompleteInput {
|
|
92
|
+
storageKey: string;
|
|
93
|
+
uploadId: string;
|
|
94
|
+
parts: Array<Pick<S3MultipartPart, "partNumber" | "etag">>;
|
|
95
|
+
signal?: AbortSignal;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Small S3 Multipart adapter. Implement these methods with the S3 SDK used by
|
|
99
|
+
* the host application. Reusing a part number is intentional: S3 replaces
|
|
100
|
+
* the previous part, which makes retries idempotent.
|
|
101
|
+
*/
|
|
102
|
+
interface JumboChunkUploader {
|
|
103
|
+
createMultipartUpload(input: JumboChunkCreateInput): Promise<{
|
|
104
|
+
uploadId: string;
|
|
105
|
+
}>;
|
|
106
|
+
uploadPart(input: JumboChunkUploadPartInput): Promise<{
|
|
107
|
+
etag: string;
|
|
108
|
+
}>;
|
|
109
|
+
completeMultipartUpload(input: JumboChunkCompleteInput): Promise<void>;
|
|
110
|
+
abortMultipartUpload?(input: {
|
|
111
|
+
storageKey: string;
|
|
112
|
+
uploadId: string;
|
|
113
|
+
signal?: AbortSignal;
|
|
114
|
+
}): Promise<void>;
|
|
115
|
+
}
|
|
116
|
+
interface JumboChunkCheckpoint {
|
|
117
|
+
version: 1;
|
|
118
|
+
storageKey: string;
|
|
119
|
+
chunkIndex: number;
|
|
120
|
+
chunkSize: number;
|
|
121
|
+
chunkSha256: string;
|
|
122
|
+
partSize: number;
|
|
123
|
+
uploadId: string;
|
|
124
|
+
completed: boolean;
|
|
125
|
+
completing: boolean;
|
|
126
|
+
parts: S3MultipartPart[];
|
|
127
|
+
}
|
|
128
|
+
interface JumboChunkCheckpointStore {
|
|
129
|
+
load(key: string): Promise<JumboChunkCheckpoint | null>;
|
|
130
|
+
save(key: string, checkpoint: JumboChunkCheckpoint): Promise<void>;
|
|
131
|
+
remove(key: string): Promise<void>;
|
|
132
|
+
}
|
|
133
|
+
interface S3MultipartClient {
|
|
134
|
+
createMultipartUpload(input: {
|
|
135
|
+
bucket: string;
|
|
136
|
+
key: string;
|
|
137
|
+
contentType?: string;
|
|
138
|
+
signal?: AbortSignal;
|
|
139
|
+
}): Promise<{
|
|
140
|
+
uploadId: string;
|
|
141
|
+
}>;
|
|
142
|
+
uploadPart(input: {
|
|
143
|
+
bucket: string;
|
|
144
|
+
key: string;
|
|
145
|
+
uploadId: string;
|
|
146
|
+
partNumber: number;
|
|
147
|
+
body: Blob;
|
|
148
|
+
signal?: AbortSignal;
|
|
149
|
+
}): Promise<{
|
|
150
|
+
etag: string;
|
|
151
|
+
}>;
|
|
152
|
+
completeMultipartUpload(input: {
|
|
153
|
+
bucket: string;
|
|
154
|
+
key: string;
|
|
155
|
+
uploadId: string;
|
|
156
|
+
parts: Array<Pick<S3MultipartPart, "partNumber" | "etag">>;
|
|
157
|
+
signal?: AbortSignal;
|
|
158
|
+
}): Promise<void>;
|
|
159
|
+
abortMultipartUpload?(input: {
|
|
160
|
+
bucket: string;
|
|
161
|
+
key: string;
|
|
162
|
+
uploadId: string;
|
|
163
|
+
signal?: AbortSignal;
|
|
164
|
+
}): Promise<void>;
|
|
165
|
+
}
|
|
166
|
+
/** Serializable metadata a client must persist alongside its file/blob handle. */
|
|
167
|
+
interface UploadSessionContext {
|
|
168
|
+
uploadId: string;
|
|
169
|
+
path: string;
|
|
170
|
+
fileSize: number;
|
|
171
|
+
contentHash: string;
|
|
172
|
+
chunkMethod: "none" | "fix" | "cdc";
|
|
173
|
+
chunks: UploadSessionChunkContext[];
|
|
174
|
+
contentType?: string;
|
|
175
|
+
}
|
|
176
|
+
interface PrepareUploadResponse {
|
|
177
|
+
upload_id: string;
|
|
178
|
+
path: string;
|
|
179
|
+
expires_in_seconds: number;
|
|
180
|
+
chunk_method: "none" | "fix" | "cdc";
|
|
181
|
+
session_status: "prepared" | "uploaded" | "finished" | "aborted" | "expired";
|
|
182
|
+
upload_targets: UploadTarget[];
|
|
183
|
+
}
|
|
184
|
+
interface UploadStatusChunk {
|
|
185
|
+
chunk_index: number;
|
|
186
|
+
offset: number;
|
|
187
|
+
size: number;
|
|
188
|
+
sha256: string;
|
|
189
|
+
storage_key: string;
|
|
190
|
+
status: "pending" | "uploaded";
|
|
191
|
+
upload_required: boolean;
|
|
192
|
+
put_method?: string;
|
|
193
|
+
put_url?: string;
|
|
194
|
+
put_headers: Record<string, string>;
|
|
195
|
+
}
|
|
196
|
+
interface UploadStatusResponse {
|
|
197
|
+
upload_id: string;
|
|
198
|
+
path: string;
|
|
199
|
+
storage_key: string;
|
|
200
|
+
size: number;
|
|
201
|
+
chunk_method: "none" | "fix" | "cdc";
|
|
202
|
+
session_status: "prepared" | "uploaded" | "finished" | "aborted" | "expired";
|
|
203
|
+
finish_status: "awaiting_upload" | "awaiting_finish" | "awaiting_chunks" | "completed" | "aborted" | "expired";
|
|
204
|
+
total_chunks: number;
|
|
205
|
+
uploaded_chunks: number;
|
|
206
|
+
uploaded_bytes: number;
|
|
207
|
+
chunks: UploadStatusChunk[];
|
|
208
|
+
ino?: number;
|
|
209
|
+
}
|
|
210
|
+
interface FinishUploadResponse {
|
|
211
|
+
upload_id: string;
|
|
212
|
+
path: string;
|
|
213
|
+
storage_key: string;
|
|
214
|
+
size: number;
|
|
215
|
+
chunk_method: "none" | "fix" | "cdc";
|
|
216
|
+
session_status: "prepared" | "uploaded" | "finished" | "aborted" | "expired";
|
|
217
|
+
finish_status: "awaiting_upload" | "awaiting_finish" | "awaiting_chunks" | "completed" | "aborted" | "expired";
|
|
218
|
+
ino?: number;
|
|
219
|
+
}
|
|
220
|
+
type UploadResult = FinishUploadResponse;
|
|
221
|
+
interface PreparedUpload {
|
|
222
|
+
response: PrepareUploadResponse;
|
|
223
|
+
chunks: FileChunk[];
|
|
224
|
+
chunkHashes: string[];
|
|
225
|
+
contentHash: string;
|
|
226
|
+
context: UploadSessionContext;
|
|
227
|
+
}
|
|
228
|
+
declare class MountraApiError extends Error {
|
|
229
|
+
readonly status: number;
|
|
230
|
+
readonly code?: string;
|
|
231
|
+
readonly body?: unknown;
|
|
232
|
+
constructor(message: string, input: {
|
|
233
|
+
status: number;
|
|
234
|
+
code?: string;
|
|
235
|
+
body?: unknown;
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
declare class InMemoryJumboChunkCheckpointStore implements JumboChunkCheckpointStore {
|
|
239
|
+
private readonly records;
|
|
240
|
+
load(key: string): Promise<JumboChunkCheckpoint | null>;
|
|
241
|
+
save(key: string, checkpoint: JumboChunkCheckpoint): Promise<void>;
|
|
242
|
+
remove(key: string): Promise<void>;
|
|
243
|
+
}
|
|
244
|
+
declare class LocalStorageJumboChunkCheckpointStore implements JumboChunkCheckpointStore {
|
|
245
|
+
private readonly prefix;
|
|
246
|
+
constructor(prefix?: string);
|
|
247
|
+
load(key: string): Promise<JumboChunkCheckpoint | null>;
|
|
248
|
+
save(key: string, checkpoint: JumboChunkCheckpoint): Promise<void>;
|
|
249
|
+
remove(key: string): Promise<void>;
|
|
250
|
+
private storageKey;
|
|
251
|
+
private getStorage;
|
|
252
|
+
}
|
|
253
|
+
declare class S3MultipartUploader implements JumboChunkUploader {
|
|
254
|
+
private readonly bucket;
|
|
255
|
+
private readonly client;
|
|
256
|
+
constructor(input: {
|
|
257
|
+
bucket: string;
|
|
258
|
+
client: S3MultipartClient;
|
|
259
|
+
});
|
|
260
|
+
createMultipartUpload(input: JumboChunkCreateInput): Promise<{
|
|
261
|
+
uploadId: string;
|
|
262
|
+
}>;
|
|
263
|
+
uploadPart(input: JumboChunkUploadPartInput): Promise<{
|
|
264
|
+
etag: string;
|
|
265
|
+
}>;
|
|
266
|
+
completeMultipartUpload(input: JumboChunkCompleteInput): Promise<void>;
|
|
267
|
+
abortMultipartUpload(input: {
|
|
268
|
+
storageKey: string;
|
|
269
|
+
uploadId: string;
|
|
270
|
+
signal?: AbortSignal;
|
|
271
|
+
}): Promise<void>;
|
|
272
|
+
}
|
|
273
|
+
declare function splitFileIntoChunks(file: Blob, chunkCount?: number): FileChunk[];
|
|
274
|
+
declare function isJumboChunk(size: number): boolean;
|
|
275
|
+
declare function sha256Hex(blob: Blob): Promise<string>;
|
|
276
|
+
declare class MountraClient {
|
|
277
|
+
private readonly baseUrl;
|
|
278
|
+
private readonly apiPrefix;
|
|
279
|
+
private readonly clientSelector;
|
|
280
|
+
private readonly token?;
|
|
281
|
+
private readonly headers?;
|
|
282
|
+
private readonly fetchImpl;
|
|
283
|
+
private readonly jumboChunkUploader?;
|
|
284
|
+
private readonly jumboChunkCheckpointStore;
|
|
285
|
+
private readonly jumboPartSize;
|
|
286
|
+
constructor(options: MountraClientOptions);
|
|
287
|
+
prepareUpload(file: Blob, options: UploadFileOptions): Promise<PreparedUpload>;
|
|
288
|
+
uploadFile(file: Blob, options: UploadFileOptions): Promise<UploadResult>;
|
|
289
|
+
/** Generic upload entry point. It selects PUT or S3 Multipart per chunk. */
|
|
290
|
+
upload(file: Blob, options: UploadFileOptions): Promise<UploadResult>;
|
|
291
|
+
getUploadStatus(uploadId: string, selector?: WorkspaceSelector, signal?: AbortSignal): Promise<UploadStatusResponse>;
|
|
292
|
+
/**
|
|
293
|
+
* Resume a previously prepared session. The caller must restore the same
|
|
294
|
+
* file/blob separately; this context only describes how that file was cut.
|
|
295
|
+
*/
|
|
296
|
+
resumeUpload(file: Blob, context: UploadSessionContext, options?: UploadResumeOptions): Promise<UploadResult>;
|
|
297
|
+
private uploadPreparedChunks;
|
|
298
|
+
private buildJumboCheckpointKey;
|
|
299
|
+
private uploadJumboChunk;
|
|
300
|
+
private uploadJumboPart;
|
|
301
|
+
private isCompatibleJumboCheckpoint;
|
|
302
|
+
private assertUploadContext;
|
|
303
|
+
finishUpload(uploadId: string, selector?: WorkspaceSelector): Promise<UploadResult>;
|
|
304
|
+
private endpoint;
|
|
305
|
+
private requestJson;
|
|
306
|
+
}
|
|
307
|
+
declare function createMountraClient(options: MountraClientOptions): MountraClient;
|
|
308
|
+
|
|
309
|
+
export { type AccessToken, type ChunkMethod, DEFAULT_S3_MULTIPART_PART_SIZE_BYTES, type FileChunk, type FinishUploadResponse, InMemoryJumboChunkCheckpointStore, JUMBO_CHUNK_THRESHOLD_BYTES, type JumboChunkCheckpoint, type JumboChunkCheckpointStore, type JumboChunkCompleteInput, type JumboChunkCreateInput, type JumboChunkUploadPartInput, type JumboChunkUploader, LocalStorageJumboChunkCheckpointStore, MAX_S3_MULTIPART_PARTS, MIN_S3_MULTIPART_PART_SIZE_BYTES, MountraApiError, MountraClient, type MountraClientOptions, type MountraProgress, MountraClient as MountraSDK, type PrepareUploadResponse, type PreparedUpload, type S3MultipartClient, type S3MultipartPart, S3MultipartUploader, type UploadFileOptions, type UploadResult, type UploadResumeOptions, type UploadSessionChunkContext, type UploadSessionContext, type UploadStatusChunk, type UploadStatusResponse, type UploadTarget, type WorkspaceId, type WorkspaceSelector, createMountraClient, isJumboChunk, sha256Hex, splitFileIntoChunks };
|