disk 0.8.17 → 0.8.19
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 +57 -10
- package/dist/cli.mjs +280 -0
- package/dist/disk-BLF7FFqG.d.cts +641 -0
- package/dist/disk-BLF7FFqG.d.mts +641 -0
- package/dist/index.cjs +1339 -1155
- package/dist/index.d.cts +207 -2131
- package/dist/index.d.mts +266 -0
- package/dist/index.mjs +2 -0
- package/dist/internal/tools.cjs +328 -0
- package/dist/internal/tools.d.cts +126 -0
- package/dist/internal/tools.d.mts +126 -0
- package/dist/internal/tools.mjs +326 -0
- package/dist/paths-1c4QqbOB.cjs +33 -0
- package/dist/paths-Br9snUhy.mjs +28 -0
- package/dist/src--b7LHPh_.mjs +1398 -0
- package/package.json +27 -22
- package/dist/cli.js +0 -1450
- package/dist/index.d.ts +0 -2190
- package/dist/index.js +0 -1202
package/dist/index.js
DELETED
|
@@ -1,1202 +0,0 @@
|
|
|
1
|
-
// src/client.ts
|
|
2
|
-
import createClientDefault from "openapi-fetch";
|
|
3
|
-
|
|
4
|
-
// src/s3xml.ts
|
|
5
|
-
import { XMLParser } from "fast-xml-parser";
|
|
6
|
-
var parser = new XMLParser({
|
|
7
|
-
isArray: (name) => name === "Contents" || name === "CommonPrefixes",
|
|
8
|
-
parseTagValue: false,
|
|
9
|
-
trimValues: true,
|
|
10
|
-
ignoreAttributes: true
|
|
11
|
-
});
|
|
12
|
-
function parseXml(xml) {
|
|
13
|
-
if (!xml.trim()) return {};
|
|
14
|
-
return parser.parse(xml);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// src/errors.ts
|
|
18
|
-
var ArchilError = class extends Error {
|
|
19
|
-
/** HTTP status code associated with the failure. */
|
|
20
|
-
status;
|
|
21
|
-
/** Machine-readable error code (e.g. an S3 code like "NoSuchKey"), if known. */
|
|
22
|
-
code;
|
|
23
|
-
constructor(message, status, code) {
|
|
24
|
-
super(message);
|
|
25
|
-
this.name = "ArchilError";
|
|
26
|
-
this.status = status;
|
|
27
|
-
this.code = code;
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
var ArchilApiError = class extends ArchilError {
|
|
31
|
-
constructor(message, status, code) {
|
|
32
|
-
super(message, status, code);
|
|
33
|
-
this.name = "ArchilApiError";
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
var ArchilS3Error = class extends ArchilError {
|
|
37
|
-
/** S3 request id, if the gateway returned one. */
|
|
38
|
-
requestId;
|
|
39
|
-
/** Raw response body (the XML document), for debugging. */
|
|
40
|
-
raw;
|
|
41
|
-
constructor(opts) {
|
|
42
|
-
const detail = opts.message ?? opts.statusText ?? "";
|
|
43
|
-
const codePart = opts.code ? ` ${opts.code}` : "";
|
|
44
|
-
super(
|
|
45
|
-
`S3 ${opts.operation} failed: ${opts.statusCode}${codePart}${detail ? ` \u2014 ${detail}` : ""}`,
|
|
46
|
-
opts.statusCode,
|
|
47
|
-
opts.code
|
|
48
|
-
);
|
|
49
|
-
this.name = "ArchilS3Error";
|
|
50
|
-
this.requestId = opts.requestId;
|
|
51
|
-
this.raw = opts.raw;
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
function tagString(obj, tag) {
|
|
55
|
-
const value = obj[tag];
|
|
56
|
-
return value === void 0 || value === null ? void 0 : String(value);
|
|
57
|
-
}
|
|
58
|
-
function parseS3Error(operation, statusCode, statusText, body) {
|
|
59
|
-
let err = {};
|
|
60
|
-
try {
|
|
61
|
-
err = parseXml(body).Error ?? {};
|
|
62
|
-
} catch {
|
|
63
|
-
err = {};
|
|
64
|
-
}
|
|
65
|
-
return new ArchilS3Error({
|
|
66
|
-
operation,
|
|
67
|
-
statusCode,
|
|
68
|
-
statusText,
|
|
69
|
-
code: tagString(err, "Code"),
|
|
70
|
-
message: tagString(err, "Message"),
|
|
71
|
-
requestId: tagString(err, "RequestId"),
|
|
72
|
-
raw: body
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// src/regions.ts
|
|
77
|
-
var REGION_URLS = {
|
|
78
|
-
"aws-us-east-1": "https://control.green.us-east-1.aws.prod.archil.com",
|
|
79
|
-
"aws-us-west-2": "https://control.green.us-west-2.aws.prod.archil.com",
|
|
80
|
-
"aws-eu-west-1": "https://control.green.eu-west-1.aws.prod.archil.com",
|
|
81
|
-
"gcp-us-central1": "https://control.blue.us-central1.gcp.prod.archil.com"
|
|
82
|
-
};
|
|
83
|
-
function resolveBaseUrl(region) {
|
|
84
|
-
const url = REGION_URLS[region];
|
|
85
|
-
if (!url) {
|
|
86
|
-
throw new Error(
|
|
87
|
-
`Unknown region "${region}". Valid regions: ${Object.keys(REGION_URLS).join(", ")}`
|
|
88
|
-
);
|
|
89
|
-
}
|
|
90
|
-
return url;
|
|
91
|
-
}
|
|
92
|
-
function deriveS3BaseUrl(controlBaseUrl) {
|
|
93
|
-
try {
|
|
94
|
-
const u = new URL(controlBaseUrl);
|
|
95
|
-
u.hostname = u.hostname.replace(/^control\./, "s3.");
|
|
96
|
-
return u.toString().replace(/\/$/, "");
|
|
97
|
-
} catch {
|
|
98
|
-
return void 0;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// src/version.ts
|
|
103
|
-
var VERSION = true ? "0.8.17" : "0.0.0-dev";
|
|
104
|
-
var USER_AGENT = `archil-js/${VERSION}`;
|
|
105
|
-
|
|
106
|
-
// src/client.ts
|
|
107
|
-
var createClient = typeof createClientDefault === "function" ? createClientDefault : createClientDefault.default;
|
|
108
|
-
function createApiClient(opts) {
|
|
109
|
-
const baseUrl = opts.baseUrl ?? resolveBaseUrl(opts.region);
|
|
110
|
-
return createClient({
|
|
111
|
-
baseUrl,
|
|
112
|
-
headers: {
|
|
113
|
-
Authorization: `key-${opts.apiKey.replace(/^key-/, "")}`,
|
|
114
|
-
// Identifies the JS SDK (and its version) to the control plane. Honored
|
|
115
|
-
// by Node's fetch; browsers treat User-Agent as a forbidden header and
|
|
116
|
-
// drop it, which is fine — the SDK's primary use is server-side.
|
|
117
|
-
"User-Agent": USER_AGENT
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
async function unwrap(promise) {
|
|
122
|
-
const { data: body, error, response } = await promise;
|
|
123
|
-
if (error || !body) {
|
|
124
|
-
const errBody = error;
|
|
125
|
-
throw new ArchilApiError(
|
|
126
|
-
errBody?.error ?? `API request failed with status ${response.status}`,
|
|
127
|
-
response.status
|
|
128
|
-
);
|
|
129
|
-
}
|
|
130
|
-
if (!body.success) {
|
|
131
|
-
throw new ArchilApiError(
|
|
132
|
-
body.error ?? "Unknown API error",
|
|
133
|
-
response.status
|
|
134
|
-
);
|
|
135
|
-
}
|
|
136
|
-
return body.data;
|
|
137
|
-
}
|
|
138
|
-
async function unwrapEmpty(promise) {
|
|
139
|
-
const { data: body, error, response } = await promise;
|
|
140
|
-
if (error || !body) {
|
|
141
|
-
const errBody = error;
|
|
142
|
-
throw new ArchilApiError(
|
|
143
|
-
errBody?.error ?? `API request failed with status ${response.status}`,
|
|
144
|
-
response.status
|
|
145
|
-
);
|
|
146
|
-
}
|
|
147
|
-
if (!body.success) {
|
|
148
|
-
throw new ArchilApiError(
|
|
149
|
-
body.error ?? "Unknown API error",
|
|
150
|
-
response.status
|
|
151
|
-
);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// src/disk.ts
|
|
156
|
-
var Disk = class {
|
|
157
|
-
id;
|
|
158
|
-
name;
|
|
159
|
-
organization;
|
|
160
|
-
status;
|
|
161
|
-
provider;
|
|
162
|
-
region;
|
|
163
|
-
createdAt;
|
|
164
|
-
fsHandlerStatus;
|
|
165
|
-
lastAccessed;
|
|
166
|
-
dataSize;
|
|
167
|
-
monthlyUsage;
|
|
168
|
-
mounts;
|
|
169
|
-
metrics;
|
|
170
|
-
connectedClients;
|
|
171
|
-
authorizedUsers;
|
|
172
|
-
allowedIps;
|
|
173
|
-
/** @internal */
|
|
174
|
-
_client;
|
|
175
|
-
/** @internal */
|
|
176
|
-
_archilRegion;
|
|
177
|
-
/** Base URL for the S3-compatible API (port 9000 ingress). Empty if unset. */
|
|
178
|
-
_s3BaseUrl;
|
|
179
|
-
/** Lazily-constructed multipart namespace (see {@link multipart}). @internal */
|
|
180
|
-
_multipart;
|
|
181
|
-
/** @internal */
|
|
182
|
-
constructor(data, client, archilRegion, s3BaseUrl) {
|
|
183
|
-
this.id = data.id;
|
|
184
|
-
this.name = data.name;
|
|
185
|
-
this.organization = data.organization;
|
|
186
|
-
this.status = data.status;
|
|
187
|
-
this.provider = data.provider;
|
|
188
|
-
this.region = data.region;
|
|
189
|
-
this.createdAt = data.createdAt;
|
|
190
|
-
this.fsHandlerStatus = data.fsHandlerStatus;
|
|
191
|
-
this.lastAccessed = data.lastAccessed;
|
|
192
|
-
this.dataSize = data.dataSize;
|
|
193
|
-
this.monthlyUsage = data.monthlyUsage;
|
|
194
|
-
this.mounts = data.mounts;
|
|
195
|
-
this.metrics = data.metrics;
|
|
196
|
-
this.connectedClients = data.connectedClients;
|
|
197
|
-
this.authorizedUsers = data.authorizedUsers;
|
|
198
|
-
this.allowedIps = data.allowedIps;
|
|
199
|
-
this._client = client;
|
|
200
|
-
this._archilRegion = archilRegion;
|
|
201
|
-
this._s3BaseUrl = s3BaseUrl ?? "";
|
|
202
|
-
}
|
|
203
|
-
toJSON() {
|
|
204
|
-
return {
|
|
205
|
-
id: this.id,
|
|
206
|
-
name: this.name,
|
|
207
|
-
organization: this.organization,
|
|
208
|
-
status: this.status,
|
|
209
|
-
provider: this.provider,
|
|
210
|
-
region: this.region,
|
|
211
|
-
createdAt: this.createdAt,
|
|
212
|
-
fsHandlerStatus: this.fsHandlerStatus,
|
|
213
|
-
lastAccessed: this.lastAccessed,
|
|
214
|
-
dataSize: this.dataSize,
|
|
215
|
-
monthlyUsage: this.monthlyUsage,
|
|
216
|
-
mounts: this.mounts,
|
|
217
|
-
metrics: this.metrics,
|
|
218
|
-
connectedClients: this.connectedClients,
|
|
219
|
-
authorizedUsers: this.authorizedUsers,
|
|
220
|
-
allowedIps: this.allowedIps
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
async addUser(user) {
|
|
224
|
-
return unwrap(
|
|
225
|
-
this._client.POST("/api/disks/{id}/users", {
|
|
226
|
-
params: { path: { id: this.id } },
|
|
227
|
-
body: user
|
|
228
|
-
})
|
|
229
|
-
);
|
|
230
|
-
}
|
|
231
|
-
async removeUser(userType, identifier) {
|
|
232
|
-
await unwrapEmpty(
|
|
233
|
-
this._client.DELETE("/api/disks/{id}/users/{userType}", {
|
|
234
|
-
params: {
|
|
235
|
-
path: { id: this.id, userType },
|
|
236
|
-
query: { identifier }
|
|
237
|
-
}
|
|
238
|
-
})
|
|
239
|
-
);
|
|
240
|
-
}
|
|
241
|
-
async createToken(nickname) {
|
|
242
|
-
const user = await unwrap(
|
|
243
|
-
this._client.POST("/api/disks/{id}/users", {
|
|
244
|
-
params: { path: { id: this.id } },
|
|
245
|
-
body: { type: "token", nickname }
|
|
246
|
-
})
|
|
247
|
-
);
|
|
248
|
-
if (!user.token || !user.identifier) {
|
|
249
|
-
throw new Error("Server did not return a generated token");
|
|
250
|
-
}
|
|
251
|
-
return user;
|
|
252
|
-
}
|
|
253
|
-
async removeTokenUser(identifier) {
|
|
254
|
-
await this.removeUser("token", identifier);
|
|
255
|
-
}
|
|
256
|
-
async getAllowedIPs() {
|
|
257
|
-
const data = await unwrap(
|
|
258
|
-
this._client.GET("/api/disks/{id}/allowed-ips", {
|
|
259
|
-
params: { path: { id: this.id } }
|
|
260
|
-
})
|
|
261
|
-
);
|
|
262
|
-
return data.allowedIps;
|
|
263
|
-
}
|
|
264
|
-
async setAllowedIPs(allowedIps) {
|
|
265
|
-
const data = await unwrap(
|
|
266
|
-
this._client.PUT("/api/disks/{id}/allowed-ips", {
|
|
267
|
-
params: { path: { id: this.id } },
|
|
268
|
-
body: { allowedIps }
|
|
269
|
-
})
|
|
270
|
-
);
|
|
271
|
-
return data.allowedIps;
|
|
272
|
-
}
|
|
273
|
-
async addAllowedIP(ip) {
|
|
274
|
-
const current = await this.getAllowedIPs();
|
|
275
|
-
if (current.includes(ip)) return current;
|
|
276
|
-
return this.setAllowedIPs([...current, ip]);
|
|
277
|
-
}
|
|
278
|
-
async removeAllowedIP(ip) {
|
|
279
|
-
const current = await this.getAllowedIPs();
|
|
280
|
-
return this.setAllowedIPs(current.filter((i) => i !== ip));
|
|
281
|
-
}
|
|
282
|
-
async delete() {
|
|
283
|
-
await unwrapEmpty(
|
|
284
|
-
this._client.DELETE("/api/disks/{id}", {
|
|
285
|
-
params: { path: { id: this.id } }
|
|
286
|
-
})
|
|
287
|
-
);
|
|
288
|
-
}
|
|
289
|
-
/**
|
|
290
|
-
* Execute a command in a container with this disk mounted.
|
|
291
|
-
* Blocks until the command completes and returns stdout, stderr, and exit code.
|
|
292
|
-
*/
|
|
293
|
-
async exec(command) {
|
|
294
|
-
return unwrap(
|
|
295
|
-
this._client.POST("/api/disks/{id}/exec", {
|
|
296
|
-
params: { path: { id: this.id } },
|
|
297
|
-
body: { command }
|
|
298
|
-
})
|
|
299
|
-
);
|
|
300
|
-
}
|
|
301
|
-
/**
|
|
302
|
-
* Constant-time parallel grep across files on this disk. Listing and
|
|
303
|
-
* matching are fanned out across ephemeral exec containers; the request
|
|
304
|
-
* finishes within the supplied time budget regardless of dataset size.
|
|
305
|
-
*
|
|
306
|
-
* The returned `stoppedReason` says whether the search ran to completion
|
|
307
|
-
* or short-circuited on `maxResults` / `maxDurationSeconds`. When
|
|
308
|
-
* stopping early, the matches returned are a sample (whichever workers
|
|
309
|
-
* reported first), not the lexicographically first N.
|
|
310
|
-
*/
|
|
311
|
-
async grep(opts) {
|
|
312
|
-
return unwrap(
|
|
313
|
-
this._client.POST("/api/disks/{id}/grep", {
|
|
314
|
-
params: { path: { id: this.id } },
|
|
315
|
-
body: {
|
|
316
|
-
directory: opts.directory,
|
|
317
|
-
pattern: opts.pattern,
|
|
318
|
-
recursive: opts.recursive ?? false,
|
|
319
|
-
maxDurationSeconds: opts.maxDurationSeconds ?? 30,
|
|
320
|
-
concurrency: opts.concurrency ?? 50,
|
|
321
|
-
maxResults: opts.maxResults ?? 1e3
|
|
322
|
-
}
|
|
323
|
-
})
|
|
324
|
-
);
|
|
325
|
-
}
|
|
326
|
-
/**
|
|
327
|
-
* Create a signed, time-limited URL that lets anyone download a single file
|
|
328
|
-
* from this disk without authentication. The returned URL embeds a
|
|
329
|
-
* cryptographically signed token carrying the disk, the file's key, and an
|
|
330
|
-
* expiry — share it directly; no API key is needed to redeem it.
|
|
331
|
-
*
|
|
332
|
-
* @param key Path to the file on the disk (e.g. "reports/2026-01/data.pdf").
|
|
333
|
-
* @param opts `expiresIn` sets the URL lifetime in seconds (any positive
|
|
334
|
-
* integer, max 604800 = 7 days). Defaults to 24h.
|
|
335
|
-
*/
|
|
336
|
-
async share(key, opts = {}) {
|
|
337
|
-
const body = { key };
|
|
338
|
-
if (opts.expiresIn !== void 0) body.expiresIn = opts.expiresIn;
|
|
339
|
-
const call = this._client.POST;
|
|
340
|
-
return unwrap(call(`/api/disks/${this.id}/share`, { body }));
|
|
341
|
-
}
|
|
342
|
-
/**
|
|
343
|
-
* Read an object from the disk via the S3-compatible GetObject API and return
|
|
344
|
-
* its full contents as bytes. Throws `ArchilS3Error` if the object does not
|
|
345
|
-
* exist (status 404, code "NoSuchKey") or the request is rejected; use
|
|
346
|
-
* `headObject`/`objectExists` to check existence without throwing.
|
|
347
|
-
*
|
|
348
|
-
* @param key Path on the disk (e.g. "reports/2026-01/data.json")
|
|
349
|
-
*/
|
|
350
|
-
async getObject(key) {
|
|
351
|
-
const resp = await this._s3Request("GET", key);
|
|
352
|
-
if (!resp.ok) {
|
|
353
|
-
throw parseS3Error("GetObject", resp.status, resp.statusText, decodeText(resp.body));
|
|
354
|
-
}
|
|
355
|
-
return resp.body;
|
|
356
|
-
}
|
|
357
|
-
/**
|
|
358
|
-
* Fetch an object's metadata (size, etag, content type, last-modified) without
|
|
359
|
-
* downloading its contents, via the S3-compatible HeadObject API. Returns
|
|
360
|
-
* `null` if the object does not exist.
|
|
361
|
-
*
|
|
362
|
-
* @param key Path on the disk (e.g. "reports/2026-01/data.json")
|
|
363
|
-
*/
|
|
364
|
-
async headObject(key) {
|
|
365
|
-
const resp = await this._s3Request("HEAD", key);
|
|
366
|
-
if (resp.status === 404) return null;
|
|
367
|
-
if (!resp.ok) {
|
|
368
|
-
throw parseS3Error("HeadObject", resp.status, resp.statusText, decodeText(resp.body));
|
|
369
|
-
}
|
|
370
|
-
const lastModified = resp.headers.get("last-modified");
|
|
371
|
-
return {
|
|
372
|
-
size: Number(resp.headers.get("content-length") ?? 0),
|
|
373
|
-
etag: resp.headers.get("etag") ?? void 0,
|
|
374
|
-
contentType: resp.headers.get("content-type") ?? void 0,
|
|
375
|
-
lastModified: lastModified ? new Date(lastModified) : void 0
|
|
376
|
-
};
|
|
377
|
-
}
|
|
378
|
-
/** Whether an object exists on the disk (a HeadObject that maps 404 → false). */
|
|
379
|
-
async objectExists(key) {
|
|
380
|
-
return await this.headObject(key) !== null;
|
|
381
|
-
}
|
|
382
|
-
/**
|
|
383
|
-
* Write an object to the disk via the S3-compatible API. Handles any size:
|
|
384
|
-
* bodies at or below `multipartThreshold` (defaults to `partSize`, i.e.
|
|
385
|
-
* 16 MiB) go through a single PutObject request; larger bodies are uploaded as
|
|
386
|
-
* a multipart upload — split into `partSize` parts, uploaded with bounded
|
|
387
|
-
* `concurrency` (default 4), and assembled. A failed part aborts the upload so
|
|
388
|
-
* nothing is left half-staged. For manual control over the multipart
|
|
389
|
-
* lifecycle, use the {@link multipart} namespace.
|
|
390
|
-
*
|
|
391
|
-
* Faster than exec for large files — no container overhead, no command-length
|
|
392
|
-
* limits. Returns the entity tag the server assigned (a multipart upload's tag
|
|
393
|
-
* is S3's `md5(concat(partMd5s))-N` form rather than a plain MD5).
|
|
394
|
-
*
|
|
395
|
-
* @param key Path on the disk (e.g. "reports/2026-01/data.json")
|
|
396
|
-
* @param body Contents as a string, Uint8Array/Buffer, or ArrayBuffer
|
|
397
|
-
* @param options Either a content-type string, or {@link PutObjectOptions}
|
|
398
|
-
* (`contentType`, `multipartThreshold`, `partSize`,
|
|
399
|
-
* `concurrency`). Content type defaults to
|
|
400
|
-
* "application/octet-stream".
|
|
401
|
-
*/
|
|
402
|
-
async putObject(key, body, options) {
|
|
403
|
-
const opts = typeof options === "string" ? { contentType: options } : options ?? {};
|
|
404
|
-
const contentType = opts.contentType ?? "application/octet-stream";
|
|
405
|
-
const partSize = Math.max(opts.partSize ?? DEFAULT_PART_SIZE, MIN_PART_SIZE);
|
|
406
|
-
const threshold = opts.multipartThreshold ?? partSize;
|
|
407
|
-
const bytes = toBytes(body);
|
|
408
|
-
if (bytes.length <= threshold) {
|
|
409
|
-
const resp = await this._s3Request("PUT", key, { body, contentType });
|
|
410
|
-
if (!resp.ok) {
|
|
411
|
-
throw parseS3Error("PutObject", resp.status, resp.statusText, decodeText(resp.body));
|
|
412
|
-
}
|
|
413
|
-
return { etag: resp.headers.get("etag") ?? void 0 };
|
|
414
|
-
}
|
|
415
|
-
return this._putMultipart(
|
|
416
|
-
key,
|
|
417
|
-
bytes,
|
|
418
|
-
contentType,
|
|
419
|
-
partSize,
|
|
420
|
-
Math.max(1, opts.concurrency ?? DEFAULT_UPLOAD_CONCURRENCY)
|
|
421
|
-
);
|
|
422
|
-
}
|
|
423
|
-
/**
|
|
424
|
-
* Upload a large body through the multipart lifecycle: split into `partSize`
|
|
425
|
-
* parts, upload them with bounded concurrency, then complete — aborting the
|
|
426
|
-
* upload if any part fails so nothing is left half-staged. @internal
|
|
427
|
-
*/
|
|
428
|
-
async _putMultipart(key, bytes, contentType, partSize, concurrency) {
|
|
429
|
-
const effectivePartSize = effectiveUploadPartSize(bytes.length, partSize);
|
|
430
|
-
const mp = this.multipart;
|
|
431
|
-
const upload = await mp.create(key, contentType);
|
|
432
|
-
try {
|
|
433
|
-
const partCount = Math.ceil(bytes.length / effectivePartSize);
|
|
434
|
-
const parts = new Array(partCount);
|
|
435
|
-
let next = 0;
|
|
436
|
-
const worker = async () => {
|
|
437
|
-
for (; ; ) {
|
|
438
|
-
const index = next++;
|
|
439
|
-
if (index >= partCount) return;
|
|
440
|
-
const start = index * effectivePartSize;
|
|
441
|
-
const slice = bytes.subarray(start, Math.min(start + effectivePartSize, bytes.length));
|
|
442
|
-
parts[index] = await mp.uploadPart(key, upload.uploadId, index + 1, slice);
|
|
443
|
-
}
|
|
444
|
-
};
|
|
445
|
-
await Promise.all(Array.from({ length: Math.min(concurrency, partCount) }, worker));
|
|
446
|
-
const done = await mp.complete(key, upload.uploadId, parts);
|
|
447
|
-
return { etag: done.etag };
|
|
448
|
-
} catch (err) {
|
|
449
|
-
await mp.abort(key, upload.uploadId).catch(() => {
|
|
450
|
-
});
|
|
451
|
-
throw err;
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
/**
|
|
455
|
-
* Append bytes to an object via the S3-compatible PutObject append extension
|
|
456
|
-
* (`?append=true`). If the object already exists the bytes are appended to it;
|
|
457
|
-
* if it doesn't, it is created. Returns the entity tag of the full object
|
|
458
|
-
* after the append.
|
|
459
|
-
*
|
|
460
|
-
* Each call may append at most 1 MiB — the server rejects a larger body with
|
|
461
|
-
* `EntityTooLarge`. To grow an object past that, append in chunks (or use
|
|
462
|
-
* {@link putObject} for a one-shot large write).
|
|
463
|
-
*
|
|
464
|
-
* Unlike most operations this is NOT auto-retried on a transient error:
|
|
465
|
-
* append isn't idempotent, so retrying a succeeded-but-unacknowledged append
|
|
466
|
-
* would duplicate the bytes. On a transient failure, re-append yourself only
|
|
467
|
-
* after confirming the object's size.
|
|
468
|
-
*
|
|
469
|
-
* @param key Path on the disk (e.g. "logs/app.log")
|
|
470
|
-
* @param body Bytes to append (string, Uint8Array/Buffer, or ArrayBuffer)
|
|
471
|
-
* @param contentType MIME type, applied only when the object is newly created.
|
|
472
|
-
*/
|
|
473
|
-
async appendObject(key, body, contentType = "application/octet-stream") {
|
|
474
|
-
const resp = await this._s3Request("PUT", key, {
|
|
475
|
-
body,
|
|
476
|
-
contentType,
|
|
477
|
-
query: { append: "true" },
|
|
478
|
-
retry: false
|
|
479
|
-
});
|
|
480
|
-
if (!resp.ok) {
|
|
481
|
-
throw parseS3Error("AppendObject", resp.status, resp.statusText, decodeText(resp.body));
|
|
482
|
-
}
|
|
483
|
-
return { etag: resp.headers.get("etag") ?? void 0 };
|
|
484
|
-
}
|
|
485
|
-
/**
|
|
486
|
-
* Delete an object from the disk via the S3-compatible DeleteObject API.
|
|
487
|
-
* Idempotent: deleting a key that doesn't exist resolves successfully, per
|
|
488
|
-
* S3 semantics.
|
|
489
|
-
*
|
|
490
|
-
* @param key Path on the disk (e.g. "project/dist/server.cjs")
|
|
491
|
-
*/
|
|
492
|
-
async deleteObject(key) {
|
|
493
|
-
const resp = await this._s3Request("DELETE", key);
|
|
494
|
-
if (!resp.ok && resp.status !== 404) {
|
|
495
|
-
throw parseS3Error("DeleteObject", resp.status, resp.statusText, decodeText(resp.body));
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
/**
|
|
499
|
-
* List objects on the disk via the S3-compatible ListObjectsV2 API. By
|
|
500
|
-
* default this follows continuation tokens until the listing is exhausted and
|
|
501
|
-
* returns every matching key. Use `limit` to cap the total, `singlePage` for a
|
|
502
|
-
* single request, or {@link listObjectsPages} to stream pages without loading
|
|
503
|
-
* everything into memory.
|
|
504
|
-
*
|
|
505
|
-
* @param prefix Only return keys beginning with this prefix (omit for all).
|
|
506
|
-
* @param opts Listing and pagination options.
|
|
507
|
-
*/
|
|
508
|
-
async listObjects(prefix, opts = {}) {
|
|
509
|
-
if (opts.singlePage) {
|
|
510
|
-
return this._listObjectsPage(prefix, opts);
|
|
511
|
-
}
|
|
512
|
-
const objects = [];
|
|
513
|
-
const commonPrefixes = [];
|
|
514
|
-
const seenPrefixes = /* @__PURE__ */ new Set();
|
|
515
|
-
let echoedPrefix;
|
|
516
|
-
let truncated = false;
|
|
517
|
-
outer: for await (const page of this.listObjectsPages(prefix, opts)) {
|
|
518
|
-
echoedPrefix = page.prefix;
|
|
519
|
-
for (const cp of page.commonPrefixes) {
|
|
520
|
-
if (!seenPrefixes.has(cp)) {
|
|
521
|
-
seenPrefixes.add(cp);
|
|
522
|
-
commonPrefixes.push(cp);
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
for (const obj of page.objects) {
|
|
526
|
-
if (opts.limit !== void 0 && objects.length >= opts.limit) {
|
|
527
|
-
truncated = true;
|
|
528
|
-
break outer;
|
|
529
|
-
}
|
|
530
|
-
objects.push(obj);
|
|
531
|
-
}
|
|
532
|
-
}
|
|
533
|
-
return { objects, commonPrefixes, isTruncated: truncated, keyCount: objects.length, prefix: echoedPrefix };
|
|
534
|
-
}
|
|
535
|
-
/**
|
|
536
|
-
* Yield ListObjectsV2 pages lazily, following continuation tokens. A
|
|
537
|
-
* memory-friendly way to process a large listing without materializing it:
|
|
538
|
-
*
|
|
539
|
-
* ```ts
|
|
540
|
-
* for await (const page of disk.listObjectsPages("logs/")) {
|
|
541
|
-
* for (const obj of page.objects) process(obj);
|
|
542
|
-
* }
|
|
543
|
-
* ```
|
|
544
|
-
*
|
|
545
|
-
* @param prefix Only return keys beginning with this prefix (omit for all).
|
|
546
|
-
* @param opts Listing options (`limit` / `singlePage` are ignored here —
|
|
547
|
-
* control your own loop).
|
|
548
|
-
*/
|
|
549
|
-
async *listObjectsPages(prefix, opts = {}) {
|
|
550
|
-
const seenTokens = /* @__PURE__ */ new Set();
|
|
551
|
-
let continuationToken = opts.continuationToken;
|
|
552
|
-
for (; ; ) {
|
|
553
|
-
const page = await this._listObjectsPage(prefix, { ...opts, continuationToken });
|
|
554
|
-
yield page;
|
|
555
|
-
const next = page.isTruncated ? page.nextContinuationToken : void 0;
|
|
556
|
-
if (!next || seenTokens.has(next)) break;
|
|
557
|
-
seenTokens.add(next);
|
|
558
|
-
continuationToken = next;
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
|
-
/** Fetch a single ListObjectsV2 page. @internal */
|
|
562
|
-
async _listObjectsPage(prefix, opts) {
|
|
563
|
-
const query = { "list-type": 2 };
|
|
564
|
-
if (prefix !== void 0) query.prefix = prefix;
|
|
565
|
-
if (!opts.recursive) query.delimiter = "/";
|
|
566
|
-
if (opts.continuationToken !== void 0) query["continuation-token"] = opts.continuationToken;
|
|
567
|
-
if (opts.startAfter !== void 0) query["start-after"] = opts.startAfter;
|
|
568
|
-
const resp = await this._s3Request("GET", "", { query });
|
|
569
|
-
if (!resp.ok) {
|
|
570
|
-
throw parseS3Error("ListObjectsV2", resp.status, resp.statusText, decodeText(resp.body));
|
|
571
|
-
}
|
|
572
|
-
return parseListObjectsResult(decodeText(resp.body));
|
|
573
|
-
}
|
|
574
|
-
/**
|
|
575
|
-
* Delete up to many objects in a single S3-compatible DeleteObjects request.
|
|
576
|
-
* Unlike {@link deleteObject}, failures are reported per key rather than
|
|
577
|
-
* thrown: the result's `deleted` lists the keys that were removed and
|
|
578
|
-
* `errors` lists the ones that weren't (with the server's code/message). A
|
|
579
|
-
* key that didn't exist still counts as deleted, per S3 semantics.
|
|
580
|
-
*
|
|
581
|
-
* The server caps a single request at 1000 keys; this method transparently
|
|
582
|
-
* splits larger inputs into 1000-key batches and merges the results.
|
|
583
|
-
*
|
|
584
|
-
* @param keys Object keys to delete.
|
|
585
|
-
* @param opts `quiet` suppresses the per-key success list server-side.
|
|
586
|
-
*/
|
|
587
|
-
async deleteObjects(keys, opts = {}) {
|
|
588
|
-
const deleted = [];
|
|
589
|
-
const errors = [];
|
|
590
|
-
for (let i = 0; i < keys.length; i += MAX_DELETE_OBJECTS_PER_REQUEST) {
|
|
591
|
-
const batch = keys.slice(i, i + MAX_DELETE_OBJECTS_PER_REQUEST);
|
|
592
|
-
const xml = buildDeleteObjectsXml(batch, opts.quiet ?? false);
|
|
593
|
-
const resp = await this._s3Request("POST", "", {
|
|
594
|
-
query: { delete: "" },
|
|
595
|
-
body: xml,
|
|
596
|
-
contentType: "application/xml"
|
|
597
|
-
});
|
|
598
|
-
if (!resp.ok) {
|
|
599
|
-
throw parseS3Error("DeleteObjects", resp.status, resp.statusText, decodeText(resp.body));
|
|
600
|
-
}
|
|
601
|
-
const parsed = parseDeleteObjectsResult(decodeText(resp.body));
|
|
602
|
-
deleted.push(...parsed.deleted);
|
|
603
|
-
errors.push(...parsed.errors);
|
|
604
|
-
}
|
|
605
|
-
return { deleted, errors };
|
|
606
|
-
}
|
|
607
|
-
/**
|
|
608
|
-
* The advanced, opt-in multipart-upload API. Drive the raw lifecycle
|
|
609
|
-
* yourself — `create` → `uploadPart` → `complete` (or `abort`), plus
|
|
610
|
-
* `listParts` / `listUploads`. Most callers don't need this: {@link putObject}
|
|
611
|
-
* runs the whole lifecycle automatically for large bodies. Reach for it only
|
|
612
|
-
* when you need manual control (e.g. uploading parts from separate processes),
|
|
613
|
-
* and note you then own part-size, memory, and concurrency management.
|
|
614
|
-
*/
|
|
615
|
-
get multipart() {
|
|
616
|
-
return this._multipart ??= new DiskMultipart(this.id, this._s3Request.bind(this));
|
|
617
|
-
}
|
|
618
|
-
/**
|
|
619
|
-
* Send a single request to the disk's S3-compatible endpoint. This reuses the
|
|
620
|
-
* control-plane client purely for its credential and transport — the same
|
|
621
|
-
* `Authorization` header is sent and verified by the same code server-side —
|
|
622
|
-
* pointed at the S3 host. Returns the response status and fully-buffered body
|
|
623
|
-
* so callers can inspect both regardless of the verb used.
|
|
624
|
-
*
|
|
625
|
-
* The S3 routes (`/{diskId}/{key}` for objects, `/{diskId}` for the bucket)
|
|
626
|
-
* are not part of the typed control-plane API, so the path and per-request
|
|
627
|
-
* options are passed untyped. An empty `key` targets the bucket itself (used
|
|
628
|
-
* by listObjects).
|
|
629
|
-
*
|
|
630
|
-
* @internal
|
|
631
|
-
*/
|
|
632
|
-
async _s3Request(method, key, opts = {}) {
|
|
633
|
-
if (!this._s3BaseUrl) {
|
|
634
|
-
throw new Error(
|
|
635
|
-
"S3 base URL not configured. Pass s3BaseUrl to new Archil({...}) or set ARCHIL_S3_BASE_URL."
|
|
636
|
-
);
|
|
637
|
-
}
|
|
638
|
-
const call = this._client[method];
|
|
639
|
-
const trimmedKey = key.replace(/^\//, "");
|
|
640
|
-
const encodedKey = trimmedKey.split("/").map(encodeURIComponent).join("/");
|
|
641
|
-
const path = encodedKey ? `/${this.id}/${encodedKey}` : `/${this.id}`;
|
|
642
|
-
const init = {
|
|
643
|
-
baseUrl: this._s3BaseUrl,
|
|
644
|
-
parseAs: "stream",
|
|
645
|
-
...opts.query ? { params: { query: opts.query } } : {},
|
|
646
|
-
// fetch accepts string / Uint8Array / ArrayBuffer bodies directly; pass it
|
|
647
|
-
// through unchanged (no Node Buffer in the data path). Bodies are fully
|
|
648
|
-
// buffered (never streams), so re-sending one on a retry is safe.
|
|
649
|
-
...opts.body !== void 0 ? { body: opts.body, bodySerializer: (b) => b } : {},
|
|
650
|
-
...opts.contentType ? { headers: { "Content-Type": opts.contentType } } : {}
|
|
651
|
-
};
|
|
652
|
-
const hasBody = method === "GET" || method === "POST";
|
|
653
|
-
const maxRetries = opts.retry === false ? 0 : MAX_S3_RETRIES;
|
|
654
|
-
for (let attempt = 0; ; attempt++) {
|
|
655
|
-
let error;
|
|
656
|
-
let response;
|
|
657
|
-
try {
|
|
658
|
-
({ error, response } = await call(path, init));
|
|
659
|
-
} catch (transportError) {
|
|
660
|
-
if (attempt < maxRetries) {
|
|
661
|
-
await sleep(s3RetryDelayMs(attempt));
|
|
662
|
-
continue;
|
|
663
|
-
}
|
|
664
|
-
throw transportError;
|
|
665
|
-
}
|
|
666
|
-
if (!response.ok) {
|
|
667
|
-
if (isTransientS3Status(response.status) && attempt < maxRetries) {
|
|
668
|
-
await response.body?.cancel().catch(() => {
|
|
669
|
-
});
|
|
670
|
-
await sleep(s3RetryDelayMs(attempt));
|
|
671
|
-
continue;
|
|
672
|
-
}
|
|
673
|
-
const message = typeof error === "string" ? error : error ? JSON.stringify(error) : "";
|
|
674
|
-
return {
|
|
675
|
-
ok: false,
|
|
676
|
-
status: response.status,
|
|
677
|
-
statusText: response.statusText,
|
|
678
|
-
headers: response.headers,
|
|
679
|
-
body: new TextEncoder().encode(message)
|
|
680
|
-
};
|
|
681
|
-
}
|
|
682
|
-
const bytes = hasBody ? new Uint8Array(await response.arrayBuffer()) : new Uint8Array(0);
|
|
683
|
-
return {
|
|
684
|
-
ok: true,
|
|
685
|
-
status: response.status,
|
|
686
|
-
statusText: response.statusText,
|
|
687
|
-
headers: response.headers,
|
|
688
|
-
body: bytes
|
|
689
|
-
};
|
|
690
|
-
}
|
|
691
|
-
}
|
|
692
|
-
/**
|
|
693
|
-
* Connect to this disk's data plane via the native ArchilClient.
|
|
694
|
-
*
|
|
695
|
-
* Requires the native module to be available (platform-specific .node binary).
|
|
696
|
-
*/
|
|
697
|
-
async mount(opts) {
|
|
698
|
-
let ArchilClient;
|
|
699
|
-
try {
|
|
700
|
-
const nativeSpecifier = "@archildata/native";
|
|
701
|
-
const native = await import(nativeSpecifier);
|
|
702
|
-
ArchilClient = native.ArchilClient ?? native.default?.ArchilClient;
|
|
703
|
-
if (!ArchilClient) {
|
|
704
|
-
throw new Error("@archildata/native did not export ArchilClient");
|
|
705
|
-
}
|
|
706
|
-
} catch {
|
|
707
|
-
throw new Error(
|
|
708
|
-
"Native client not available. Install @archildata/native (platform-specific binary) or use ArchilClient.connect() directly. mount() is not supported in browsers."
|
|
709
|
-
);
|
|
710
|
-
}
|
|
711
|
-
return ArchilClient.connect({
|
|
712
|
-
region: this._archilRegion,
|
|
713
|
-
diskName: `${this.organization}/${this.name}`,
|
|
714
|
-
authToken: opts?.authToken,
|
|
715
|
-
logLevel: opts?.logLevel,
|
|
716
|
-
serverAddress: opts?.serverAddress,
|
|
717
|
-
insecure: opts?.insecure
|
|
718
|
-
});
|
|
719
|
-
}
|
|
720
|
-
};
|
|
721
|
-
var DiskMultipart = class {
|
|
722
|
-
/** @internal */
|
|
723
|
-
constructor(diskId, s3Request) {
|
|
724
|
-
this.diskId = diskId;
|
|
725
|
-
this.s3Request = s3Request;
|
|
726
|
-
}
|
|
727
|
-
diskId;
|
|
728
|
-
s3Request;
|
|
729
|
-
/**
|
|
730
|
-
* Start a multipart upload (CreateMultipartUpload) and return its `uploadId`.
|
|
731
|
-
*
|
|
732
|
-
* @param key Path on the disk the finished object will live at.
|
|
733
|
-
* @param contentType MIME type to store the object with.
|
|
734
|
-
*/
|
|
735
|
-
async create(key, contentType) {
|
|
736
|
-
const resp = await this.s3Request("POST", key, { query: { uploads: "" }, contentType });
|
|
737
|
-
if (!resp.ok) {
|
|
738
|
-
throw parseS3Error("CreateMultipartUpload", resp.status, resp.statusText, decodeText(resp.body));
|
|
739
|
-
}
|
|
740
|
-
const root = parseXml(decodeText(resp.body)).InitiateMultipartUploadResult ?? {};
|
|
741
|
-
const uploadId = optionalString(root.UploadId);
|
|
742
|
-
if (!uploadId) {
|
|
743
|
-
throw new ArchilS3Error({
|
|
744
|
-
operation: "CreateMultipartUpload",
|
|
745
|
-
statusCode: resp.status,
|
|
746
|
-
message: "response did not contain an UploadId",
|
|
747
|
-
raw: decodeText(resp.body)
|
|
748
|
-
});
|
|
749
|
-
}
|
|
750
|
-
return {
|
|
751
|
-
uploadId,
|
|
752
|
-
key: optionalString(root.Key) ?? key,
|
|
753
|
-
bucket: optionalString(root.Bucket) ?? this.diskId
|
|
754
|
-
};
|
|
755
|
-
}
|
|
756
|
-
/**
|
|
757
|
-
* Upload one part (UploadPart) and return its entity tag, which you must
|
|
758
|
-
* collect (with its part number) and pass to {@link complete}. Every part
|
|
759
|
-
* except the last must be at least 5 MiB.
|
|
760
|
-
*
|
|
761
|
-
* @param key The upload's object key.
|
|
762
|
-
* @param uploadId The id from {@link create}.
|
|
763
|
-
* @param partNumber 1-based part number (1..=10000).
|
|
764
|
-
* @param body Part contents as a string, Uint8Array/Buffer, or ArrayBuffer.
|
|
765
|
-
*/
|
|
766
|
-
async uploadPart(key, uploadId, partNumber, body) {
|
|
767
|
-
const resp = await this.s3Request("PUT", key, { query: { uploadId, partNumber }, body });
|
|
768
|
-
if (!resp.ok) {
|
|
769
|
-
throw parseS3Error("UploadPart", resp.status, resp.statusText, decodeText(resp.body));
|
|
770
|
-
}
|
|
771
|
-
return { partNumber, etag: resp.headers.get("etag") ?? "" };
|
|
772
|
-
}
|
|
773
|
-
/**
|
|
774
|
-
* Finish a multipart upload (CompleteMultipartUpload), assembling the listed
|
|
775
|
-
* parts into one object. Parts are sorted by part number before submission
|
|
776
|
-
* (the server requires strictly-increasing order).
|
|
777
|
-
*
|
|
778
|
-
* Unlike the other operations this is NOT auto-retried on a transient error:
|
|
779
|
-
* the gateway isn't idempotent for completion, so a retry after a
|
|
780
|
-
* successful-but-unacknowledged complete would return a spurious NoSuchUpload.
|
|
781
|
-
* On a transient failure, re-drive completion yourself only after confirming
|
|
782
|
-
* the object isn't already present.
|
|
783
|
-
*
|
|
784
|
-
* @param key The upload's object key.
|
|
785
|
-
* @param uploadId The id from {@link create}.
|
|
786
|
-
* @param parts The `{ partNumber, etag }` pairs from {@link uploadPart}.
|
|
787
|
-
*/
|
|
788
|
-
async complete(key, uploadId, parts) {
|
|
789
|
-
const sorted = [...parts].sort((a, b) => a.partNumber - b.partNumber);
|
|
790
|
-
const xml = buildCompleteMultipartUploadXml(sorted);
|
|
791
|
-
const resp = await this.s3Request("POST", key, {
|
|
792
|
-
query: { uploadId },
|
|
793
|
-
body: xml,
|
|
794
|
-
contentType: "application/xml",
|
|
795
|
-
retry: false
|
|
796
|
-
});
|
|
797
|
-
if (!resp.ok) {
|
|
798
|
-
throw parseS3Error("CompleteMultipartUpload", resp.status, resp.statusText, decodeText(resp.body));
|
|
799
|
-
}
|
|
800
|
-
const root = parseXml(decodeText(resp.body)).CompleteMultipartUploadResult ?? {};
|
|
801
|
-
return {
|
|
802
|
-
etag: optionalString(root.ETag),
|
|
803
|
-
location: optionalString(root.Location),
|
|
804
|
-
bucket: optionalString(root.Bucket),
|
|
805
|
-
key: optionalString(root.Key)
|
|
806
|
-
};
|
|
807
|
-
}
|
|
808
|
-
/**
|
|
809
|
-
* Abort a multipart upload (AbortMultipartUpload), discarding every staged
|
|
810
|
-
* part. Idempotent against an upload that's already gone (404 / NoSuchUpload
|
|
811
|
-
* resolves successfully).
|
|
812
|
-
*
|
|
813
|
-
* @param key The upload's object key.
|
|
814
|
-
* @param uploadId The id from {@link create}.
|
|
815
|
-
*/
|
|
816
|
-
async abort(key, uploadId) {
|
|
817
|
-
const resp = await this.s3Request("DELETE", key, { query: { uploadId } });
|
|
818
|
-
if (!resp.ok && resp.status !== 404) {
|
|
819
|
-
throw parseS3Error("AbortMultipartUpload", resp.status, resp.statusText, decodeText(resp.body));
|
|
820
|
-
}
|
|
821
|
-
}
|
|
822
|
-
/**
|
|
823
|
-
* List the parts already uploaded for an in-progress upload (ListParts).
|
|
824
|
-
* Returns a single page; follow `nextPartNumberMarker` (when `isTruncated`)
|
|
825
|
-
* to page through the rest.
|
|
826
|
-
*
|
|
827
|
-
* @param key The upload's object key.
|
|
828
|
-
* @param uploadId The id from {@link create}.
|
|
829
|
-
* @param opts `maxParts` / `partNumberMarker` pagination controls.
|
|
830
|
-
*/
|
|
831
|
-
async listParts(key, uploadId, opts = {}) {
|
|
832
|
-
const query = { uploadId };
|
|
833
|
-
if (opts.maxParts !== void 0) query["max-parts"] = opts.maxParts;
|
|
834
|
-
if (opts.partNumberMarker !== void 0) query["part-number-marker"] = opts.partNumberMarker;
|
|
835
|
-
const resp = await this.s3Request("GET", key, { query });
|
|
836
|
-
if (!resp.ok) {
|
|
837
|
-
throw parseS3Error("ListParts", resp.status, resp.statusText, decodeText(resp.body));
|
|
838
|
-
}
|
|
839
|
-
return parseListPartsResult(decodeText(resp.body));
|
|
840
|
-
}
|
|
841
|
-
/**
|
|
842
|
-
* List in-progress multipart uploads on the disk (ListMultipartUploads).
|
|
843
|
-
* Returns a single page; follow `nextKeyMarker` / `nextUploadIdMarker` (when
|
|
844
|
-
* `isTruncated`) for the rest.
|
|
845
|
-
*
|
|
846
|
-
* @param opts Prefix/delimiter filter and pagination markers.
|
|
847
|
-
*/
|
|
848
|
-
async listUploads(opts = {}) {
|
|
849
|
-
const query = { uploads: "" };
|
|
850
|
-
if (opts.prefix !== void 0) query.prefix = opts.prefix;
|
|
851
|
-
if (opts.delimiter !== void 0) query.delimiter = opts.delimiter;
|
|
852
|
-
if (opts.keyMarker !== void 0) query["key-marker"] = opts.keyMarker;
|
|
853
|
-
if (opts.uploadIdMarker !== void 0) query["upload-id-marker"] = opts.uploadIdMarker;
|
|
854
|
-
if (opts.maxUploads !== void 0) query["max-uploads"] = opts.maxUploads;
|
|
855
|
-
const resp = await this.s3Request("GET", "", { query });
|
|
856
|
-
if (!resp.ok) {
|
|
857
|
-
throw parseS3Error("ListMultipartUploads", resp.status, resp.statusText, decodeText(resp.body));
|
|
858
|
-
}
|
|
859
|
-
return parseListMultipartUploadsResult(decodeText(resp.body));
|
|
860
|
-
}
|
|
861
|
-
};
|
|
862
|
-
var MAX_DELETE_OBJECTS_PER_REQUEST = 1e3;
|
|
863
|
-
var MAX_S3_RETRIES = 3;
|
|
864
|
-
var S3_RETRY_BASE_MS = 100;
|
|
865
|
-
var S3_RETRY_CAP_MS = 2e3;
|
|
866
|
-
function isTransientS3Status(status) {
|
|
867
|
-
return status === 429 || status === 500 || status === 502 || status === 503 || status === 504;
|
|
868
|
-
}
|
|
869
|
-
function s3RetryDelayMs(attempt) {
|
|
870
|
-
const ceiling = Math.min(S3_RETRY_CAP_MS, S3_RETRY_BASE_MS * 2 ** attempt);
|
|
871
|
-
return Math.random() * ceiling;
|
|
872
|
-
}
|
|
873
|
-
function sleep(ms) {
|
|
874
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
875
|
-
}
|
|
876
|
-
var MIN_PART_SIZE = 5 * 1024 * 1024;
|
|
877
|
-
var DEFAULT_PART_SIZE = 16 * 1024 * 1024;
|
|
878
|
-
var DEFAULT_UPLOAD_CONCURRENCY = 4;
|
|
879
|
-
var MAX_PARTS_PER_UPLOAD = 1e4;
|
|
880
|
-
function effectiveUploadPartSize(totalBytes, requestedPartSize) {
|
|
881
|
-
if (Math.ceil(totalBytes / requestedPartSize) <= MAX_PARTS_PER_UPLOAD) {
|
|
882
|
-
return requestedPartSize;
|
|
883
|
-
}
|
|
884
|
-
const MiB = 1024 * 1024;
|
|
885
|
-
const needed = Math.ceil(totalBytes / MAX_PARTS_PER_UPLOAD);
|
|
886
|
-
return Math.ceil(needed / MiB) * MiB;
|
|
887
|
-
}
|
|
888
|
-
function toBytes(body) {
|
|
889
|
-
if (typeof body === "string") return new TextEncoder().encode(body);
|
|
890
|
-
if (body instanceof Uint8Array) return body;
|
|
891
|
-
return new Uint8Array(body);
|
|
892
|
-
}
|
|
893
|
-
function escapeXml(value) {
|
|
894
|
-
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
895
|
-
}
|
|
896
|
-
function buildDeleteObjectsXml(keys, quiet) {
|
|
897
|
-
const objects = keys.map((k) => `<Object><Key>${escapeXml(k)}</Key></Object>`).join("");
|
|
898
|
-
const quietTag = quiet ? "<Quiet>true</Quiet>" : "";
|
|
899
|
-
return `<?xml version="1.0" encoding="UTF-8"?><Delete>${objects}${quietTag}</Delete>`;
|
|
900
|
-
}
|
|
901
|
-
function buildCompleteMultipartUploadXml(parts) {
|
|
902
|
-
const body = parts.map(
|
|
903
|
-
(p) => `<Part><PartNumber>${p.partNumber}</PartNumber><ETag>${escapeXml(p.etag)}</ETag></Part>`
|
|
904
|
-
).join("");
|
|
905
|
-
return `<?xml version="1.0" encoding="UTF-8"?><CompleteMultipartUpload>${body}</CompleteMultipartUpload>`;
|
|
906
|
-
}
|
|
907
|
-
function asArray(value) {
|
|
908
|
-
if (value === void 0 || value === null) return [];
|
|
909
|
-
return Array.isArray(value) ? value : [value];
|
|
910
|
-
}
|
|
911
|
-
function optionalNumber(value) {
|
|
912
|
-
return value === void 0 || value === null ? void 0 : Number(value);
|
|
913
|
-
}
|
|
914
|
-
function optionalString(value) {
|
|
915
|
-
return value === void 0 || value === null ? void 0 : String(value);
|
|
916
|
-
}
|
|
917
|
-
function optionalDate(value) {
|
|
918
|
-
return value === void 0 || value === null ? void 0 : new Date(String(value));
|
|
919
|
-
}
|
|
920
|
-
function decodeText(bytes) {
|
|
921
|
-
return new TextDecoder().decode(bytes);
|
|
922
|
-
}
|
|
923
|
-
function parseListObjectsResult(xml) {
|
|
924
|
-
const root = parseXml(xml).ListBucketResult ?? {};
|
|
925
|
-
const contents = root.Contents ?? [];
|
|
926
|
-
const objects = contents.map((c) => ({
|
|
927
|
-
key: String(c.Key ?? ""),
|
|
928
|
-
size: Number(c.Size ?? 0),
|
|
929
|
-
etag: optionalString(c.ETag),
|
|
930
|
-
lastModified: optionalDate(c.LastModified)
|
|
931
|
-
}));
|
|
932
|
-
const commonPrefixes = (root.CommonPrefixes ?? []).map((cp) => optionalString(cp.Prefix)).filter((p) => p !== void 0);
|
|
933
|
-
return {
|
|
934
|
-
objects,
|
|
935
|
-
commonPrefixes,
|
|
936
|
-
isTruncated: root.IsTruncated === "true" || root.IsTruncated === true,
|
|
937
|
-
nextContinuationToken: optionalString(root.NextContinuationToken),
|
|
938
|
-
keyCount: root.KeyCount !== void 0 ? Number(root.KeyCount) : objects.length,
|
|
939
|
-
prefix: optionalString(root.Prefix)
|
|
940
|
-
};
|
|
941
|
-
}
|
|
942
|
-
function xmlIsTruncated(value) {
|
|
943
|
-
return value === "true" || value === true;
|
|
944
|
-
}
|
|
945
|
-
function parseDeleteObjectsResult(xml) {
|
|
946
|
-
const root = parseXml(xml).DeleteResult ?? {};
|
|
947
|
-
const deleted = asArray(root.Deleted).map((d) => optionalString(d.Key)).filter((k) => k !== void 0);
|
|
948
|
-
const errors = asArray(root.Error).map((e) => ({
|
|
949
|
-
key: String(e.Key ?? ""),
|
|
950
|
-
code: optionalString(e.Code),
|
|
951
|
-
message: optionalString(e.Message)
|
|
952
|
-
}));
|
|
953
|
-
return { deleted, errors };
|
|
954
|
-
}
|
|
955
|
-
function parseListPartsResult(xml) {
|
|
956
|
-
const root = parseXml(xml).ListPartsResult ?? {};
|
|
957
|
-
const parts = asArray(root.Part).map((p) => ({
|
|
958
|
-
partNumber: Number(p.PartNumber ?? 0),
|
|
959
|
-
etag: optionalString(p.ETag),
|
|
960
|
-
size: Number(p.Size ?? 0),
|
|
961
|
-
lastModified: optionalDate(p.LastModified)
|
|
962
|
-
}));
|
|
963
|
-
return {
|
|
964
|
-
bucket: optionalString(root.Bucket),
|
|
965
|
-
key: optionalString(root.Key),
|
|
966
|
-
uploadId: optionalString(root.UploadId),
|
|
967
|
-
parts,
|
|
968
|
-
isTruncated: xmlIsTruncated(root.IsTruncated),
|
|
969
|
-
partNumberMarker: Number(root.PartNumberMarker ?? 0),
|
|
970
|
-
nextPartNumberMarker: optionalNumber(root.NextPartNumberMarker),
|
|
971
|
-
maxParts: Number(root.MaxParts ?? parts.length)
|
|
972
|
-
};
|
|
973
|
-
}
|
|
974
|
-
function parseListMultipartUploadsResult(xml) {
|
|
975
|
-
const root = parseXml(xml).ListMultipartUploadsResult ?? {};
|
|
976
|
-
const uploads = asArray(root.Upload).map((u) => ({
|
|
977
|
-
key: String(u.Key ?? ""),
|
|
978
|
-
uploadId: String(u.UploadId ?? ""),
|
|
979
|
-
initiated: optionalDate(u.Initiated)
|
|
980
|
-
}));
|
|
981
|
-
const commonPrefixes = asArray(root.CommonPrefixes).map((cp) => optionalString(cp.Prefix)).filter((p) => p !== void 0);
|
|
982
|
-
return {
|
|
983
|
-
bucket: optionalString(root.Bucket),
|
|
984
|
-
uploads,
|
|
985
|
-
commonPrefixes,
|
|
986
|
-
isTruncated: xmlIsTruncated(root.IsTruncated),
|
|
987
|
-
keyMarker: optionalString(root.KeyMarker),
|
|
988
|
-
uploadIdMarker: optionalString(root.UploadIdMarker),
|
|
989
|
-
nextKeyMarker: optionalString(root.NextKeyMarker),
|
|
990
|
-
nextUploadIdMarker: optionalString(root.NextUploadIdMarker),
|
|
991
|
-
prefix: optionalString(root.Prefix),
|
|
992
|
-
delimiter: optionalString(root.Delimiter),
|
|
993
|
-
maxUploads: optionalNumber(root.MaxUploads)
|
|
994
|
-
};
|
|
995
|
-
}
|
|
996
|
-
|
|
997
|
-
// src/disks.ts
|
|
998
|
-
var Disks = class {
|
|
999
|
-
/** @internal */
|
|
1000
|
-
_client;
|
|
1001
|
-
/** @internal */
|
|
1002
|
-
_region;
|
|
1003
|
-
/** @internal */
|
|
1004
|
-
_s3BaseUrl;
|
|
1005
|
-
/** @internal */
|
|
1006
|
-
constructor(client, region, s3BaseUrl) {
|
|
1007
|
-
this._client = client;
|
|
1008
|
-
this._region = region;
|
|
1009
|
-
this._s3BaseUrl = s3BaseUrl;
|
|
1010
|
-
}
|
|
1011
|
-
async list(opts) {
|
|
1012
|
-
const data = await unwrap(
|
|
1013
|
-
this._client.GET("/api/disks", {
|
|
1014
|
-
params: { query: { limit: opts?.limit, cursor: opts?.cursor, name: opts?.name } }
|
|
1015
|
-
})
|
|
1016
|
-
);
|
|
1017
|
-
return data.map(
|
|
1018
|
-
(d) => new Disk(d, this._client, this._region, this._s3BaseUrl)
|
|
1019
|
-
);
|
|
1020
|
-
}
|
|
1021
|
-
async get(id) {
|
|
1022
|
-
const data = await unwrap(
|
|
1023
|
-
this._client.GET("/api/disks/{id}", {
|
|
1024
|
-
params: { path: { id } }
|
|
1025
|
-
})
|
|
1026
|
-
);
|
|
1027
|
-
return new Disk(data, this._client, this._region, this._s3BaseUrl);
|
|
1028
|
-
}
|
|
1029
|
-
/**
|
|
1030
|
-
* Create a new disk with an auto-generated mount token.
|
|
1031
|
-
*
|
|
1032
|
-
* Returns the Disk, the one-time token (save it — it cannot be retrieved
|
|
1033
|
-
* again), and the token identifier for later management.
|
|
1034
|
-
*/
|
|
1035
|
-
async create(req) {
|
|
1036
|
-
const created = await unwrap(
|
|
1037
|
-
this._client.POST("/api/disks", { body: req })
|
|
1038
|
-
);
|
|
1039
|
-
const resp = created;
|
|
1040
|
-
if (!resp.diskId) {
|
|
1041
|
-
throw new Error("API returned success but no diskId");
|
|
1042
|
-
}
|
|
1043
|
-
const authorizedUsers = resp.authorizedUsers ?? [];
|
|
1044
|
-
const tokenUser = authorizedUsers.find((u) => u.token);
|
|
1045
|
-
const disk = await this.get(resp.diskId);
|
|
1046
|
-
return {
|
|
1047
|
-
disk,
|
|
1048
|
-
token: tokenUser?.token ?? null,
|
|
1049
|
-
tokenIdentifier: tokenUser?.identifier ?? null,
|
|
1050
|
-
authorizedUsers
|
|
1051
|
-
};
|
|
1052
|
-
}
|
|
1053
|
-
};
|
|
1054
|
-
|
|
1055
|
-
// src/tokens.ts
|
|
1056
|
-
var Tokens = class {
|
|
1057
|
-
/** @internal */
|
|
1058
|
-
_client;
|
|
1059
|
-
/** @internal */
|
|
1060
|
-
constructor(client) {
|
|
1061
|
-
this._client = client;
|
|
1062
|
-
}
|
|
1063
|
-
async list(opts) {
|
|
1064
|
-
const data = await unwrap(
|
|
1065
|
-
this._client.GET("/api/tokens", {
|
|
1066
|
-
params: { query: { limit: opts?.limit, cursor: opts?.cursor } }
|
|
1067
|
-
})
|
|
1068
|
-
);
|
|
1069
|
-
return data.tokens ?? [];
|
|
1070
|
-
}
|
|
1071
|
-
async create(req) {
|
|
1072
|
-
const data = await unwrap(
|
|
1073
|
-
this._client.POST("/api/tokens", { body: req })
|
|
1074
|
-
);
|
|
1075
|
-
return data;
|
|
1076
|
-
}
|
|
1077
|
-
async delete(id) {
|
|
1078
|
-
await unwrapEmpty(
|
|
1079
|
-
this._client.DELETE("/api/tokens/{id}", {
|
|
1080
|
-
params: { path: { id } }
|
|
1081
|
-
})
|
|
1082
|
-
);
|
|
1083
|
-
}
|
|
1084
|
-
};
|
|
1085
|
-
|
|
1086
|
-
// src/archil.ts
|
|
1087
|
-
function envVar(name) {
|
|
1088
|
-
return typeof process !== "undefined" && process.env ? process.env[name] : void 0;
|
|
1089
|
-
}
|
|
1090
|
-
function isExecMountSpec(m) {
|
|
1091
|
-
return typeof m === "object" && m !== null && "disk" in m;
|
|
1092
|
-
}
|
|
1093
|
-
function diskIdFromMount(m) {
|
|
1094
|
-
return typeof m === "string" ? m : m.id;
|
|
1095
|
-
}
|
|
1096
|
-
var Archil = class {
|
|
1097
|
-
disks;
|
|
1098
|
-
tokens;
|
|
1099
|
-
/** @internal */
|
|
1100
|
-
_client;
|
|
1101
|
-
constructor(opts = {}) {
|
|
1102
|
-
const apiKey = opts.apiKey ?? envVar("ARCHIL_API_KEY");
|
|
1103
|
-
const region = opts.region ?? envVar("ARCHIL_REGION");
|
|
1104
|
-
if (!apiKey) {
|
|
1105
|
-
throw new Error("Missing API key: pass apiKey in options or set ARCHIL_API_KEY environment variable");
|
|
1106
|
-
}
|
|
1107
|
-
if (!region) {
|
|
1108
|
-
throw new Error("Missing region: pass region in options or set ARCHIL_REGION environment variable");
|
|
1109
|
-
}
|
|
1110
|
-
const controlBaseUrl = opts.baseUrl ?? resolveBaseUrl(region);
|
|
1111
|
-
const client = createApiClient({
|
|
1112
|
-
apiKey,
|
|
1113
|
-
region,
|
|
1114
|
-
baseUrl: controlBaseUrl
|
|
1115
|
-
});
|
|
1116
|
-
const s3BaseUrl = opts.s3BaseUrl ?? envVar("ARCHIL_S3_BASE_URL") ?? deriveS3BaseUrl(controlBaseUrl);
|
|
1117
|
-
this._client = client;
|
|
1118
|
-
this.disks = new Disks(client, region, s3BaseUrl);
|
|
1119
|
-
this.tokens = new Tokens(client);
|
|
1120
|
-
}
|
|
1121
|
-
/**
|
|
1122
|
-
* Run a command in a container with multiple disks mounted simultaneously,
|
|
1123
|
-
* each at its own relative path under `/mnt/archil`. Blocks until the
|
|
1124
|
-
* command completes and returns its stdout, stderr, exit code, and timing.
|
|
1125
|
-
*/
|
|
1126
|
-
async exec(opts) {
|
|
1127
|
-
const disks = {};
|
|
1128
|
-
for (const [relPath, mount] of Object.entries(opts.disks)) {
|
|
1129
|
-
if (isExecMountSpec(mount)) {
|
|
1130
|
-
const entry = {
|
|
1131
|
-
disk: diskIdFromMount(mount.disk),
|
|
1132
|
-
readOnly: mount.readOnly ?? false
|
|
1133
|
-
};
|
|
1134
|
-
if (mount.subdirectory !== void 0) entry.subdirectory = mount.subdirectory;
|
|
1135
|
-
disks[relPath] = entry;
|
|
1136
|
-
} else {
|
|
1137
|
-
disks[relPath] = diskIdFromMount(mount);
|
|
1138
|
-
}
|
|
1139
|
-
}
|
|
1140
|
-
return unwrap(
|
|
1141
|
-
this._client.POST("/api/exec", {
|
|
1142
|
-
body: { disks, command: opts.command }
|
|
1143
|
-
})
|
|
1144
|
-
);
|
|
1145
|
-
}
|
|
1146
|
-
};
|
|
1147
|
-
|
|
1148
|
-
// src/index.ts
|
|
1149
|
-
var _options;
|
|
1150
|
-
var _instance;
|
|
1151
|
-
function configure(options) {
|
|
1152
|
-
_options = options;
|
|
1153
|
-
_instance = void 0;
|
|
1154
|
-
}
|
|
1155
|
-
function archil() {
|
|
1156
|
-
if (!_instance) {
|
|
1157
|
-
_instance = new Archil(_options);
|
|
1158
|
-
}
|
|
1159
|
-
return _instance;
|
|
1160
|
-
}
|
|
1161
|
-
function createDisk(req) {
|
|
1162
|
-
return archil().disks.create(req);
|
|
1163
|
-
}
|
|
1164
|
-
function listDisks(opts) {
|
|
1165
|
-
return archil().disks.list(opts);
|
|
1166
|
-
}
|
|
1167
|
-
function getDisk(id) {
|
|
1168
|
-
return archil().disks.get(id);
|
|
1169
|
-
}
|
|
1170
|
-
function listApiKeys(opts) {
|
|
1171
|
-
return archil().tokens.list(opts);
|
|
1172
|
-
}
|
|
1173
|
-
function createApiKey(req) {
|
|
1174
|
-
return archil().tokens.create(req);
|
|
1175
|
-
}
|
|
1176
|
-
function deleteApiKey(id) {
|
|
1177
|
-
return archil().tokens.delete(id);
|
|
1178
|
-
}
|
|
1179
|
-
function exec(opts) {
|
|
1180
|
-
return archil().exec(opts);
|
|
1181
|
-
}
|
|
1182
|
-
export {
|
|
1183
|
-
Archil,
|
|
1184
|
-
ArchilApiError,
|
|
1185
|
-
ArchilError,
|
|
1186
|
-
ArchilS3Error,
|
|
1187
|
-
Disk,
|
|
1188
|
-
DiskMultipart,
|
|
1189
|
-
Disks,
|
|
1190
|
-
Tokens,
|
|
1191
|
-
USER_AGENT,
|
|
1192
|
-
VERSION,
|
|
1193
|
-
configure,
|
|
1194
|
-
createApiKey,
|
|
1195
|
-
createDisk,
|
|
1196
|
-
deleteApiKey,
|
|
1197
|
-
effectiveUploadPartSize,
|
|
1198
|
-
exec,
|
|
1199
|
-
getDisk,
|
|
1200
|
-
listApiKeys,
|
|
1201
|
-
listDisks
|
|
1202
|
-
};
|