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