@micropue/drop-cli 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +307 -2
- package/package.json +52 -4
- package/src/cli.js +95 -0
- package/src/commands/auth.js +71 -0
- package/src/commands/deploy.js +81 -0
- package/src/commands/init.js +50 -0
- package/src/commands/status.js +38 -0
- package/src/commands/watch.js +380 -0
- package/src/index.js +9 -0
- package/src/lib/api.js +206 -0
- package/src/lib/auth.js +88 -0
- package/src/lib/config.js +159 -0
- package/src/lib/constants.js +22 -0
- package/src/lib/deployer.js +248 -0
- package/src/lib/discovery.js +329 -0
- package/src/lib/errors.js +59 -0
- package/src/lib/ignore.js +48 -0
- package/src/lib/prompt.js +58 -0
- package/src/lib/runtime.js +130 -0
- package/src/lib/snapshot.js +38 -0
- package/src/lib/terminal.js +45 -0
- package/src/lib/utils.js +189 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { readConfig } from "../lib/config.js";
|
|
2
|
+
import { DropConfigError } from "../lib/errors.js";
|
|
3
|
+
import {
|
|
4
|
+
color,
|
|
5
|
+
printDropBanner,
|
|
6
|
+
printInfo,
|
|
7
|
+
printUrl,
|
|
8
|
+
} from "../lib/terminal.js";
|
|
9
|
+
import { formatBytes, humanTimestamp } from "../lib/utils.js";
|
|
10
|
+
import { resolveDirectory } from "../lib/runtime.js";
|
|
11
|
+
|
|
12
|
+
export async function statusCommand(directory) {
|
|
13
|
+
const root = await resolveDirectory(directory);
|
|
14
|
+
const config = await readConfig(root);
|
|
15
|
+
if (!config) {
|
|
16
|
+
throw new DropConfigError("No .drop/drop.json found.", {
|
|
17
|
+
hint: "Run `drop init` or `drop watch` in the site directory.",
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
printDropBanner();
|
|
22
|
+
printInfo(`site ${root}`);
|
|
23
|
+
printInfo(`slug ${color.bold(config.slug)}`);
|
|
24
|
+
printInfo(`name ${config.name}`);
|
|
25
|
+
printInfo(
|
|
26
|
+
`access ${config.visibility === "password" ? "private (password)" : "public"}`,
|
|
27
|
+
);
|
|
28
|
+
printInfo(`version v${config.version ?? 0}`);
|
|
29
|
+
printInfo(`revision ${config.revision ?? "not published"}`);
|
|
30
|
+
printInfo(
|
|
31
|
+
`files ${config.files?.length ?? 0} · ${formatBytes(config.totalBytes ?? 0)}`,
|
|
32
|
+
);
|
|
33
|
+
printInfo(`folders ${config.folders?.length ?? 0}`);
|
|
34
|
+
printInfo(`deployed ${humanTimestamp(config.deployedAt)}`);
|
|
35
|
+
if (config.url) {
|
|
36
|
+
printUrl(config.url);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
|
|
3
|
+
import chokidar from "chokidar";
|
|
4
|
+
|
|
5
|
+
import { DropClient } from "../lib/api.js";
|
|
6
|
+
import { resolveApiKey } from "../lib/auth.js";
|
|
7
|
+
import { readConfig, syncDropBundle } from "../lib/config.js";
|
|
8
|
+
import { publishSnapshot } from "../lib/deployer.js";
|
|
9
|
+
import {
|
|
10
|
+
DropConfigError,
|
|
11
|
+
formatError,
|
|
12
|
+
isAbortError,
|
|
13
|
+
} from "../lib/errors.js";
|
|
14
|
+
import { loadIgnoreMatcher } from "../lib/ignore.js";
|
|
15
|
+
import {
|
|
16
|
+
loadWatchSnapshot,
|
|
17
|
+
} from "../lib/snapshot.js";
|
|
18
|
+
import {
|
|
19
|
+
color,
|
|
20
|
+
printDropBanner,
|
|
21
|
+
printError,
|
|
22
|
+
printInfo,
|
|
23
|
+
printProgress,
|
|
24
|
+
printSuccess,
|
|
25
|
+
printUrl,
|
|
26
|
+
printWarning,
|
|
27
|
+
} from "../lib/terminal.js";
|
|
28
|
+
import {
|
|
29
|
+
formatBytes,
|
|
30
|
+
hasWrongCaseSupportedExtension,
|
|
31
|
+
toPosixPath,
|
|
32
|
+
} from "../lib/utils.js";
|
|
33
|
+
import {
|
|
34
|
+
metadataChanged,
|
|
35
|
+
resolveDirectory,
|
|
36
|
+
resolveSettings,
|
|
37
|
+
} from "../lib/runtime.js";
|
|
38
|
+
|
|
39
|
+
function isRelevantWatchEvent(event, relativePath) {
|
|
40
|
+
if (!relativePath) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
const segments = relativePath.split("/");
|
|
44
|
+
if (segments.some((segment) => segment.startsWith("."))) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
if (segments.some((segment) => segment === "node_modules" || segment === ".git")) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
if (event === "addDir" || event === "unlinkDir") {
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
return (
|
|
54
|
+
[".html", ".css", ".js"].includes(path.extname(relativePath)) ||
|
|
55
|
+
hasWrongCaseSupportedExtension(relativePath)
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function retryDelay(error, intervalMs) {
|
|
60
|
+
if (error?.retryAfter && Number.isFinite(error.retryAfter)) {
|
|
61
|
+
return Math.max(intervalMs, error.retryAfter * 1000);
|
|
62
|
+
}
|
|
63
|
+
return Math.max(intervalMs * 3, 5_000);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export async function watchCommand(directory, options = {}) {
|
|
67
|
+
const root = await resolveDirectory(directory);
|
|
68
|
+
let config = await readConfig(root);
|
|
69
|
+
const settings = resolveSettings(root, options, config);
|
|
70
|
+
const intervalMs = settings.intervalMs;
|
|
71
|
+
const controller = new AbortController();
|
|
72
|
+
let stopping = false;
|
|
73
|
+
let watcher;
|
|
74
|
+
let timer;
|
|
75
|
+
let safetyTimer;
|
|
76
|
+
let running = null;
|
|
77
|
+
let pending = false;
|
|
78
|
+
let pendingDelay = intervalMs;
|
|
79
|
+
let ignoreMatcher;
|
|
80
|
+
let apiKey;
|
|
81
|
+
let client;
|
|
82
|
+
let lastErrorSignature = null;
|
|
83
|
+
let failedHash = null;
|
|
84
|
+
let retryAfterAt = 0;
|
|
85
|
+
let printedWarnings = false;
|
|
86
|
+
|
|
87
|
+
let resolveStopped;
|
|
88
|
+
const stopped = new Promise((resolve) => {
|
|
89
|
+
resolveStopped = resolve;
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
const stop = () => {
|
|
93
|
+
if (stopping) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
stopping = true;
|
|
97
|
+
pending = false;
|
|
98
|
+
if (timer) {
|
|
99
|
+
clearTimeout(timer);
|
|
100
|
+
timer = null;
|
|
101
|
+
}
|
|
102
|
+
if (safetyTimer) {
|
|
103
|
+
clearInterval(safetyTimer);
|
|
104
|
+
safetyTimer = null;
|
|
105
|
+
}
|
|
106
|
+
controller.abort();
|
|
107
|
+
void watcher?.close();
|
|
108
|
+
resolveStopped();
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const onSignal = () => stop();
|
|
112
|
+
process.once("SIGINT", onSignal);
|
|
113
|
+
process.once("SIGTERM", onSignal);
|
|
114
|
+
|
|
115
|
+
const ensureClient = async () => {
|
|
116
|
+
if (!client) {
|
|
117
|
+
apiKey ??= await resolveApiKey();
|
|
118
|
+
client = new DropClient({
|
|
119
|
+
apiKey,
|
|
120
|
+
baseUrl: settings.apiBase,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
return client;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const getPasswordIfNeeded = async () => {
|
|
127
|
+
if (settings.visibility !== "password") {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
const needsPassword =
|
|
131
|
+
!config?.projectId ||
|
|
132
|
+
config.visibility !== "password" ||
|
|
133
|
+
settings.explicitPassword;
|
|
134
|
+
if (!needsPassword || settings.password) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if (process.env.DROP_PASSWORD?.trim()) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
if (!process.stdin.isTTY) {
|
|
141
|
+
throw new DropConfigError(
|
|
142
|
+
"This site is password-protected but no password is available.",
|
|
143
|
+
{ hint: "Set DROP_PASSWORD or pass --password." },
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
const { promptHidden } = await import("../lib/prompt.js");
|
|
147
|
+
settings.password = (await promptHidden("Site password")).trim();
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
const armTimer = (delay) => {
|
|
151
|
+
if (stopping) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
if (timer) {
|
|
155
|
+
clearTimeout(timer);
|
|
156
|
+
}
|
|
157
|
+
timer = setTimeout(() => {
|
|
158
|
+
timer = null;
|
|
159
|
+
void runCycle(false);
|
|
160
|
+
}, delay);
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
const schedule = (delay = intervalMs) => {
|
|
164
|
+
if (stopping) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
pending = true;
|
|
168
|
+
if (running) {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
pendingDelay = delay;
|
|
172
|
+
armTimer(delay);
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
const runCycle = async (initial) => {
|
|
176
|
+
if (stopping) {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
if (running) {
|
|
180
|
+
pending = true;
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
pending = false;
|
|
185
|
+
running = (async () => {
|
|
186
|
+
try {
|
|
187
|
+
if (!ignoreMatcher) {
|
|
188
|
+
ignoreMatcher = await loadIgnoreMatcher(root);
|
|
189
|
+
}
|
|
190
|
+
const snapshot = await loadWatchSnapshot(root, { initial });
|
|
191
|
+
const wantsMetadata = metadataChanged(config, settings);
|
|
192
|
+
const forceThisCycle = initial && settings.force;
|
|
193
|
+
const needsWork =
|
|
194
|
+
forceThisCycle ||
|
|
195
|
+
!config?.projectId ||
|
|
196
|
+
config.contentHash !== snapshot.hash ||
|
|
197
|
+
wantsMetadata;
|
|
198
|
+
|
|
199
|
+
if (!needsWork) {
|
|
200
|
+
await syncDropBundle(root, snapshot, config);
|
|
201
|
+
if (initial) {
|
|
202
|
+
printSuccess(`Already up to date · v${config.version}`);
|
|
203
|
+
if (config.url) {
|
|
204
|
+
printUrl(config.url);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (snapshot.hash === failedHash && Date.now() < retryAfterAt) {
|
|
211
|
+
pending = true;
|
|
212
|
+
pendingDelay = retryAfterAt - Date.now();
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
await getPasswordIfNeeded();
|
|
217
|
+
const dropClient = await ensureClient();
|
|
218
|
+
const nextVersion = config?.projectId
|
|
219
|
+
? Math.max((config.version ?? 0) + 1, 1)
|
|
220
|
+
: 1;
|
|
221
|
+
printProgress(
|
|
222
|
+
`Publishing v${nextVersion} · ${snapshot.files.length} files · ${formatBytes(snapshot.totalBytes)}`,
|
|
223
|
+
);
|
|
224
|
+
const result = await publishSnapshot({
|
|
225
|
+
root,
|
|
226
|
+
config,
|
|
227
|
+
settings: { ...settings, force: forceThisCycle },
|
|
228
|
+
snapshot,
|
|
229
|
+
client: dropClient,
|
|
230
|
+
signal: controller.signal,
|
|
231
|
+
onProgress: printProgress,
|
|
232
|
+
});
|
|
233
|
+
config = result.config;
|
|
234
|
+
if (settings.explicitPassword) {
|
|
235
|
+
settings.explicitPassword = false;
|
|
236
|
+
}
|
|
237
|
+
settings.password = undefined;
|
|
238
|
+
failedHash = null;
|
|
239
|
+
lastErrorSignature = null;
|
|
240
|
+
if (result.changed || result.metadataChanged) {
|
|
241
|
+
printUrl(config.url);
|
|
242
|
+
printInfo(
|
|
243
|
+
`v${config.version} · revision ${config.revision} · ${config.files.length} files`,
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
if (initial && snapshot.warnings.length > 0 && !printedWarnings) {
|
|
247
|
+
for (const warning of snapshot.warnings) {
|
|
248
|
+
printWarning(warning);
|
|
249
|
+
}
|
|
250
|
+
printedWarnings = true;
|
|
251
|
+
}
|
|
252
|
+
} catch (error) {
|
|
253
|
+
if (stopping || isAbortError(error)) {
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
const message = formatError(error);
|
|
257
|
+
if (message !== lastErrorSignature) {
|
|
258
|
+
printError(message);
|
|
259
|
+
lastErrorSignature = message;
|
|
260
|
+
}
|
|
261
|
+
if (initial) {
|
|
262
|
+
throw error;
|
|
263
|
+
}
|
|
264
|
+
try {
|
|
265
|
+
const snapshot = await loadWatchSnapshot(root, { initial: false });
|
|
266
|
+
failedHash = snapshot.hash;
|
|
267
|
+
} catch {
|
|
268
|
+
// Leave the failed hash empty; a later scan may recover.
|
|
269
|
+
}
|
|
270
|
+
retryAfterAt = Date.now() + retryDelay(error, intervalMs);
|
|
271
|
+
pending = true;
|
|
272
|
+
pendingDelay = retryDelay(error, intervalMs);
|
|
273
|
+
}
|
|
274
|
+
})();
|
|
275
|
+
|
|
276
|
+
try {
|
|
277
|
+
await running;
|
|
278
|
+
} finally {
|
|
279
|
+
running = null;
|
|
280
|
+
if (pending && !stopping) {
|
|
281
|
+
pending = false;
|
|
282
|
+
const delay = pendingDelay;
|
|
283
|
+
pendingDelay = intervalMs;
|
|
284
|
+
armTimer(delay);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
printDropBanner();
|
|
290
|
+
printInfo(`watch ${root}`);
|
|
291
|
+
printInfo(`project ${color.bold(settings.slug)} · ${settings.visibility}`);
|
|
292
|
+
printInfo(`interval ${(intervalMs / 1000).toFixed(1)}s`);
|
|
293
|
+
|
|
294
|
+
try {
|
|
295
|
+
await runCycle(true);
|
|
296
|
+
if (stopping) {
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
watcher = chokidar.watch(root, {
|
|
301
|
+
ignoreInitial: true,
|
|
302
|
+
persistent: true,
|
|
303
|
+
usePolling: settings.poll,
|
|
304
|
+
ignored: (watchedPath) => {
|
|
305
|
+
const relativePath = toPosixPath(path.relative(root, watchedPath));
|
|
306
|
+
if (!relativePath) {
|
|
307
|
+
return false;
|
|
308
|
+
}
|
|
309
|
+
if (relativePath === ".drop-ignore") {
|
|
310
|
+
return false;
|
|
311
|
+
}
|
|
312
|
+
const segments = relativePath.split("/");
|
|
313
|
+
return segments.some(
|
|
314
|
+
(segment) =>
|
|
315
|
+
segment === "node_modules" || segment === ".git" || segment.startsWith("."),
|
|
316
|
+
);
|
|
317
|
+
},
|
|
318
|
+
awaitWriteFinish: settings.poll
|
|
319
|
+
? false
|
|
320
|
+
: {
|
|
321
|
+
stabilityThreshold: 200,
|
|
322
|
+
pollInterval: 50,
|
|
323
|
+
},
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
watcher.on("error", (error) => {
|
|
327
|
+
printWarning(`File watcher error: ${error.message}`);
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
const ready = new Promise((resolve) => {
|
|
331
|
+
watcher.once("ready", resolve);
|
|
332
|
+
watcher.once("error", resolve);
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
watcher.on("all", (event, watchedPath) => {
|
|
336
|
+
const relativePath = toPosixPath(path.relative(root, watchedPath));
|
|
337
|
+
if (relativePath === ".drop-ignore") {
|
|
338
|
+
ignoreMatcher = null;
|
|
339
|
+
schedule();
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
if (isRelevantWatchEvent(event, relativePath)) {
|
|
343
|
+
schedule();
|
|
344
|
+
}
|
|
345
|
+
});
|
|
346
|
+
safetyTimer = setInterval(() => {
|
|
347
|
+
schedule(0);
|
|
348
|
+
}, Math.max(intervalMs * 5, 15_000));
|
|
349
|
+
safetyTimer.unref?.();
|
|
350
|
+
|
|
351
|
+
await Promise.race([ready, stopped]);
|
|
352
|
+
if (stopping) {
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
printInfo("Press Ctrl+C to stop watching. The site stays online.");
|
|
356
|
+
await stopped;
|
|
357
|
+
} finally {
|
|
358
|
+
stopping = true;
|
|
359
|
+
controller.abort();
|
|
360
|
+
if (timer) {
|
|
361
|
+
clearTimeout(timer);
|
|
362
|
+
}
|
|
363
|
+
if (safetyTimer) {
|
|
364
|
+
clearInterval(safetyTimer);
|
|
365
|
+
}
|
|
366
|
+
await watcher?.close();
|
|
367
|
+
if (running) {
|
|
368
|
+
await running.catch(() => {});
|
|
369
|
+
}
|
|
370
|
+
process.off("SIGINT", onSignal);
|
|
371
|
+
process.off("SIGTERM", onSignal);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
process.stdout.write("\n");
|
|
375
|
+
if (config?.url) {
|
|
376
|
+
printSuccess(`Stopped watching. Site remains online at ${color.cyan(config.url)}`);
|
|
377
|
+
} else {
|
|
378
|
+
printInfo("Stopped watching.");
|
|
379
|
+
}
|
|
380
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { DropClient, DropClient as Client } from "./lib/api.js";
|
|
2
|
+
export {
|
|
3
|
+
DropApiError,
|
|
4
|
+
DropConfigError,
|
|
5
|
+
DropError,
|
|
6
|
+
} from "./lib/errors.js";
|
|
7
|
+
export { readConfig, syncDropBundle } from "./lib/config.js";
|
|
8
|
+
export { scanDropBundle, scanSource } from "./lib/discovery.js";
|
|
9
|
+
export { publishSnapshot } from "./lib/deployer.js";
|
package/src/lib/api.js
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_API_BASE,
|
|
3
|
+
DEFAULT_REQUEST_TIMEOUT_MS,
|
|
4
|
+
MAX_REQUEST_TIMEOUT_MS,
|
|
5
|
+
PACKAGE_NAME,
|
|
6
|
+
} from "./constants.js";
|
|
7
|
+
import { DropApiError } from "./errors.js";
|
|
8
|
+
|
|
9
|
+
function normalizeBaseUrl(value) {
|
|
10
|
+
const url = new URL(value);
|
|
11
|
+
if (url.protocol !== "https:" && url.protocol !== "http:") {
|
|
12
|
+
throw new Error("DROP API URL must use http or https.");
|
|
13
|
+
}
|
|
14
|
+
return value.replace(/\/+$/g, "");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function timeoutFromEnvironment() {
|
|
18
|
+
const raw = process.env.DROP_REQUEST_TIMEOUT_MS;
|
|
19
|
+
if (!raw) {
|
|
20
|
+
return DEFAULT_REQUEST_TIMEOUT_MS;
|
|
21
|
+
}
|
|
22
|
+
const number = Number(raw);
|
|
23
|
+
if (
|
|
24
|
+
!Number.isSafeInteger(number) ||
|
|
25
|
+
number < 1_000 ||
|
|
26
|
+
number > MAX_REQUEST_TIMEOUT_MS
|
|
27
|
+
) {
|
|
28
|
+
throw new Error(
|
|
29
|
+
`DROP_REQUEST_TIMEOUT_MS must be between 1000 and ${MAX_REQUEST_TIMEOUT_MS}.`,
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
return number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export class DropClient {
|
|
36
|
+
constructor({
|
|
37
|
+
apiKey,
|
|
38
|
+
baseUrl = process.env.DROP_API_BASE || DEFAULT_API_BASE,
|
|
39
|
+
timeoutMs = timeoutFromEnvironment(),
|
|
40
|
+
fetchImpl = globalThis.fetch,
|
|
41
|
+
userAgent = `${PACKAGE_NAME}`,
|
|
42
|
+
} = {}) {
|
|
43
|
+
if (!apiKey) {
|
|
44
|
+
throw new Error("DropClient requires an API key.");
|
|
45
|
+
}
|
|
46
|
+
this.apiKey = apiKey;
|
|
47
|
+
this.baseUrl = normalizeBaseUrl(baseUrl);
|
|
48
|
+
this.timeoutMs = timeoutMs;
|
|
49
|
+
this.fetchImpl = fetchImpl;
|
|
50
|
+
this.userAgent = userAgent;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async request(method, relativePath, { body, headers = {}, signal } = {}) {
|
|
54
|
+
const controller = new AbortController();
|
|
55
|
+
let timedOut = false;
|
|
56
|
+
const timer = setTimeout(() => {
|
|
57
|
+
timedOut = true;
|
|
58
|
+
controller.abort();
|
|
59
|
+
}, this.timeoutMs);
|
|
60
|
+
const onAbort = () => controller.abort(signal.reason);
|
|
61
|
+
if (signal?.aborted) {
|
|
62
|
+
clearTimeout(timer);
|
|
63
|
+
throw signal.reason ?? new DOMException("Aborted", "AbortError");
|
|
64
|
+
}
|
|
65
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
66
|
+
|
|
67
|
+
const url = `${this.baseUrl}${relativePath}`;
|
|
68
|
+
const requestHeaders = {
|
|
69
|
+
accept: "application/json, application/problem+json",
|
|
70
|
+
authorization: `Bearer ${this.apiKey}`,
|
|
71
|
+
"user-agent": this.userAgent,
|
|
72
|
+
...headers,
|
|
73
|
+
};
|
|
74
|
+
if (body !== undefined) {
|
|
75
|
+
requestHeaders["content-type"] = "application/json";
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
try {
|
|
79
|
+
const response = await this.fetchImpl(url, {
|
|
80
|
+
method,
|
|
81
|
+
headers: requestHeaders,
|
|
82
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
83
|
+
signal: controller.signal,
|
|
84
|
+
});
|
|
85
|
+
const responseText = await response.text();
|
|
86
|
+
let payload = null;
|
|
87
|
+
if (responseText) {
|
|
88
|
+
try {
|
|
89
|
+
payload = JSON.parse(responseText);
|
|
90
|
+
} catch {
|
|
91
|
+
payload = null;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (!response.ok) {
|
|
96
|
+
throw new DropApiError(
|
|
97
|
+
payload?.detail ||
|
|
98
|
+
payload?.title ||
|
|
99
|
+
`DROP returned HTTP ${response.status}.`,
|
|
100
|
+
{
|
|
101
|
+
status: response.status,
|
|
102
|
+
code: payload?.code,
|
|
103
|
+
requestId: payload?.requestId,
|
|
104
|
+
retryAfter: parseRetryAfter(response.headers.get("retry-after")),
|
|
105
|
+
issues: Array.isArray(payload?.errors) ? payload.errors : [],
|
|
106
|
+
},
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return payload;
|
|
111
|
+
} catch (error) {
|
|
112
|
+
if (signal?.aborted) {
|
|
113
|
+
throw signal.reason ?? error;
|
|
114
|
+
}
|
|
115
|
+
if (timedOut) {
|
|
116
|
+
throw new DropApiError(
|
|
117
|
+
`DROP did not respond within ${this.timeoutMs} ms.`,
|
|
118
|
+
{ code: "request_timeout" },
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
if (error instanceof DropApiError) {
|
|
122
|
+
throw error;
|
|
123
|
+
}
|
|
124
|
+
throw new DropApiError(`Could not reach DROP: ${error.message}`, {
|
|
125
|
+
code: "network_error",
|
|
126
|
+
cause: error,
|
|
127
|
+
hint: `Check your network and DROP_API_BASE (${this.baseUrl}).`,
|
|
128
|
+
});
|
|
129
|
+
} finally {
|
|
130
|
+
clearTimeout(timer);
|
|
131
|
+
signal?.removeEventListener("abort", onAbort);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
getMe(options) {
|
|
136
|
+
return this.request("GET", "/me", options);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
getProject(projectId, options) {
|
|
140
|
+
return this.request(
|
|
141
|
+
"GET",
|
|
142
|
+
`/projects/${encodeURIComponent(projectId)}`,
|
|
143
|
+
options,
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
createProject(payload, { idempotencyKey, signal } = {}) {
|
|
148
|
+
const headers = {};
|
|
149
|
+
if (idempotencyKey) {
|
|
150
|
+
headers["idempotency-key"] = idempotencyKey;
|
|
151
|
+
}
|
|
152
|
+
return this.request("POST", "/projects", {
|
|
153
|
+
body: payload,
|
|
154
|
+
headers,
|
|
155
|
+
signal,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
updateProject(projectId, payload, { revision, idempotencyKey, signal } = {}) {
|
|
160
|
+
const headers = {};
|
|
161
|
+
if (revision) {
|
|
162
|
+
headers["if-match"] = `"rev-${revision}"`;
|
|
163
|
+
}
|
|
164
|
+
if (idempotencyKey) {
|
|
165
|
+
headers["idempotency-key"] = idempotencyKey;
|
|
166
|
+
}
|
|
167
|
+
return this.request(
|
|
168
|
+
"PATCH",
|
|
169
|
+
`/projects/${encodeURIComponent(projectId)}`,
|
|
170
|
+
{ body: payload, headers, signal },
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
deployProject(projectId, payload, { revision, idempotencyKey, signal } = {}) {
|
|
175
|
+
const headers = {};
|
|
176
|
+
if (revision) {
|
|
177
|
+
headers["if-match"] = `"rev-${revision}"`;
|
|
178
|
+
}
|
|
179
|
+
if (idempotencyKey) {
|
|
180
|
+
headers["idempotency-key"] = idempotencyKey;
|
|
181
|
+
}
|
|
182
|
+
return this.request(
|
|
183
|
+
"POST",
|
|
184
|
+
`/projects/${encodeURIComponent(projectId)}/deploy`,
|
|
185
|
+
{ body: payload, headers, signal },
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function parseRetryAfter(value, now = Date.now()) {
|
|
191
|
+
if (value === null || value === undefined) {
|
|
192
|
+
return undefined;
|
|
193
|
+
}
|
|
194
|
+
const normalized = String(value).trim();
|
|
195
|
+
if (!normalized) {
|
|
196
|
+
return undefined;
|
|
197
|
+
}
|
|
198
|
+
if (/^\d+$/.test(normalized)) {
|
|
199
|
+
return Number(normalized);
|
|
200
|
+
}
|
|
201
|
+
const date = Date.parse(normalized);
|
|
202
|
+
if (Number.isNaN(date)) {
|
|
203
|
+
return undefined;
|
|
204
|
+
}
|
|
205
|
+
return Math.max(0, Math.ceil((date - now) / 1000));
|
|
206
|
+
}
|