insta 0.0.32 → 0.0.33
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/dist/commands/services.js +15 -11
- package/dist/commands/storage.js +173 -0
- package/dist/index.js +21 -1
- package/package.json +1 -1
|
@@ -53,20 +53,24 @@ export function resolveServiceId(services, type, name) {
|
|
|
53
53
|
throw new Error(`service not found: ${type} ${name}`);
|
|
54
54
|
return svc.id;
|
|
55
55
|
}
|
|
56
|
-
// Resolve
|
|
57
|
-
export function
|
|
58
|
-
const
|
|
56
|
+
// Resolve one service of a type: by name, or the sole one of that type when name is omitted.
|
|
57
|
+
export function resolveSoleService(services, type, name) {
|
|
58
|
+
const of = services.filter((s) => s.type === type);
|
|
59
59
|
if (name) {
|
|
60
|
-
const svc =
|
|
60
|
+
const svc = of.find((s) => s.name === name);
|
|
61
61
|
if (!svc)
|
|
62
|
-
throw new Error(
|
|
63
|
-
return svc
|
|
62
|
+
throw new Error(`${type} service not found: ${name}`);
|
|
63
|
+
return svc;
|
|
64
64
|
}
|
|
65
|
-
if (
|
|
66
|
-
throw new Error(
|
|
67
|
-
if (
|
|
68
|
-
throw new Error(`multiple
|
|
69
|
-
return
|
|
65
|
+
if (of.length === 0)
|
|
66
|
+
throw new Error(`no ${type} service in this project (add one with \`insta services add ${type} <name>\`)`);
|
|
67
|
+
if (of.length > 1)
|
|
68
|
+
throw new Error(`multiple ${type} services — specify one: ${of.map((s) => s.name).join(', ')}`);
|
|
69
|
+
return of[0];
|
|
70
|
+
}
|
|
71
|
+
// Resolve a compute service id: by name, or the sole compute service when name is omitted.
|
|
72
|
+
export function resolveComputeServiceId(services, name) {
|
|
73
|
+
return resolveSoleService(services, 'compute', name).id;
|
|
70
74
|
}
|
|
71
75
|
// Map service-add options to the platform POST body. Pure, so it's unit-tested without a network
|
|
72
76
|
// mock (mirrors deployRequestBody in deploy.ts). Validation (which options are valid for which
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
// `insta storage` — browse, download, and delete the objects in a storage service's bucket.
|
|
2
|
+
import { chmod, rename, rm, stat } from 'node:fs/promises';
|
|
3
|
+
import { createWriteStream, rmSync } from 'node:fs';
|
|
4
|
+
import { randomBytes } from 'node:crypto';
|
|
5
|
+
import { Readable } from 'node:stream';
|
|
6
|
+
import { pipeline } from 'node:stream/promises';
|
|
7
|
+
import { ApiClient, requireProject } from '../api.js';
|
|
8
|
+
import { info, printJson, handleApproval } from '../util.js';
|
|
9
|
+
import { q, resolveSoleService } from './services.js';
|
|
10
|
+
import { fmtBytes } from './db.js'; // the repo's tested bytes formatter — don't grow a third copy
|
|
11
|
+
function qs(params) {
|
|
12
|
+
const u = new URLSearchParams();
|
|
13
|
+
for (const [k, v] of Object.entries(params))
|
|
14
|
+
if (v !== undefined && v !== '')
|
|
15
|
+
u.set(k, String(v));
|
|
16
|
+
const s = u.toString();
|
|
17
|
+
return s ? `?${s}` : '';
|
|
18
|
+
}
|
|
19
|
+
// Page size the listing route accepts. Junk must fail here, not travel as `limit=NaN`.
|
|
20
|
+
export function parseObjectLimit(raw) {
|
|
21
|
+
const n = Number(raw);
|
|
22
|
+
if (!Number.isInteger(n) || n < 1 || n > 1000)
|
|
23
|
+
throw new Error(`--limit must be an integer 1..1000, got: ${raw}`);
|
|
24
|
+
return n;
|
|
25
|
+
}
|
|
26
|
+
// pure: platform path for the objects collection — GET lists it, DELETE removes one `key`.
|
|
27
|
+
export function objectsPath(projectId, serviceId, params) {
|
|
28
|
+
const { limit, ...rest } = params;
|
|
29
|
+
return `/projects/${projectId}/services/${serviceId}/objects${qs({ ...rest, limit: limit === undefined ? undefined : String(limit) })}`;
|
|
30
|
+
}
|
|
31
|
+
// pure: the presign route — a static subpath, so keys containing `/` stay in the query.
|
|
32
|
+
export function objectDownloadPath(projectId, serviceId, params) {
|
|
33
|
+
return `/projects/${projectId}/services/${serviceId}/objects/download${qs(params)}`;
|
|
34
|
+
}
|
|
35
|
+
// pure: one `storage list` row, size-first so the columns line up over variable-length keys.
|
|
36
|
+
export function objectListLine(o) {
|
|
37
|
+
const size = typeof o.size === 'number' ? fmtBytes(o.size) : '—';
|
|
38
|
+
return `${size.padStart(10)} ${(o.lastModified ?? '—').padEnd(24)} ${o.key}`;
|
|
39
|
+
}
|
|
40
|
+
// Resolve the branch's storage service (named, or the sole one) — its bucket is what we browse.
|
|
41
|
+
async function storageTarget(api, projectId, branch, name) {
|
|
42
|
+
const { services } = await api.request('GET', `/projects/${projectId}/services${q(branch)}`);
|
|
43
|
+
return resolveSoleService(services, 'storage', name);
|
|
44
|
+
}
|
|
45
|
+
// S3 filters by prefix only — there is no substring search, so `--prefix` is the query surface.
|
|
46
|
+
export async function storageList(opts) {
|
|
47
|
+
const limit = opts.limit === undefined ? undefined : parseObjectLimit(opts.limit);
|
|
48
|
+
const api = await ApiClient.load();
|
|
49
|
+
const p = await requireProject();
|
|
50
|
+
const branch = opts.branch ?? p.branch;
|
|
51
|
+
const svc = await storageTarget(api, p.projectId, branch, opts.service);
|
|
52
|
+
const res = await api.rawRequest('GET', objectsPath(p.projectId, svc.id, { branch, prefix: opts.prefix, cursor: opts.cursor, limit }));
|
|
53
|
+
if (handleApproval(res))
|
|
54
|
+
return;
|
|
55
|
+
if (opts.json)
|
|
56
|
+
return printJson(res.body);
|
|
57
|
+
const objects = res.body?.objects ?? [];
|
|
58
|
+
if (!objects.length) {
|
|
59
|
+
return info(opts.prefix ? `(no objects under prefix ${opts.prefix} in storage/${svc.name})` : `(storage/${svc.name} is empty)`);
|
|
60
|
+
}
|
|
61
|
+
for (const o of objects)
|
|
62
|
+
info(objectListLine(o));
|
|
63
|
+
const next = res.body?.nextCursor;
|
|
64
|
+
if (next)
|
|
65
|
+
info(` (more — next page: ${nextPageCommand({ ...opts, limit }, next)})`);
|
|
66
|
+
}
|
|
67
|
+
// Safe unquoted in every shell we care about — no spaces, quotes, or expansion characters.
|
|
68
|
+
const SHELL_SAFE = /^[\w./:@=+-]+$/;
|
|
69
|
+
// The continuation must repeat the filters, or following it lists a different set. Quoting rules
|
|
70
|
+
// differ between sh and PowerShell, so rather than guess the shell, a value that would need quotes
|
|
71
|
+
// gets a sentence instead of syntax that is broken somewhere.
|
|
72
|
+
export function nextPageCommand(opts, cursor) {
|
|
73
|
+
const values = [opts.service, opts.branch, opts.prefix, cursor].filter((v) => !!v);
|
|
74
|
+
if (!values.every((v) => SHELL_SAFE.test(v))) {
|
|
75
|
+
return `re-run this command with --cursor set to ${cursor}`;
|
|
76
|
+
}
|
|
77
|
+
const flags = [
|
|
78
|
+
opts.service ? `--service ${opts.service}` : '',
|
|
79
|
+
opts.branch ? `--branch ${opts.branch}` : '',
|
|
80
|
+
opts.prefix ? `--prefix ${opts.prefix}` : '',
|
|
81
|
+
opts.limit === undefined ? '' : `--limit ${opts.limit}`,
|
|
82
|
+
`--cursor ${cursor}`,
|
|
83
|
+
].filter(Boolean);
|
|
84
|
+
return `insta storage list ${flags.join(' ')}`;
|
|
85
|
+
}
|
|
86
|
+
// pure: where the bytes land. Only the last segment is used, so no key can escape cwd.
|
|
87
|
+
export function outputPath(key, output) {
|
|
88
|
+
if (output)
|
|
89
|
+
return output;
|
|
90
|
+
// Split on `\` too: a key may contain one, and on Windows that is also a separator.
|
|
91
|
+
const base = key.split(/[\\/]/).pop() ?? '';
|
|
92
|
+
if (!base)
|
|
93
|
+
throw new Error(`cannot infer a filename from key "${key}" — pass -o <file>`);
|
|
94
|
+
return base;
|
|
95
|
+
}
|
|
96
|
+
// Stream from the provider (never through the platform, which only signs) straight to disk, so a
|
|
97
|
+
// multi-gigabyte object never has to fit in memory. Returns the byte count written.
|
|
98
|
+
export async function streamPresignedTo(url, out, fetchImpl = fetch) {
|
|
99
|
+
const res = await fetchImpl(url);
|
|
100
|
+
if (!res.ok || !res.body)
|
|
101
|
+
throw new Error(`download failed: HTTP ${res.status} (a presigned URL lives ~60s — re-run to mint a fresh one)`);
|
|
102
|
+
let written = 0;
|
|
103
|
+
const counting = new TransformStream({
|
|
104
|
+
transform(chunk, controller) { written += chunk.byteLength; controller.enqueue(chunk); },
|
|
105
|
+
});
|
|
106
|
+
// Write beside the target, then rename: opening `out` directly would truncate an existing file
|
|
107
|
+
// that a failed download then deletes. Same directory keeps the rename atomic.
|
|
108
|
+
const part = `${out}.insta-part-${randomBytes(4).toString('hex')}`;
|
|
109
|
+
// A signal kills the process without unwinding, so the part file needs a synchronous sweep.
|
|
110
|
+
// Exit 128+signo, so a supervisor still reads interrupted (130) apart from terminated (143).
|
|
111
|
+
const sweep = (signo) => () => { rmSync(part, { force: true }); process.exit(128 + signo); };
|
|
112
|
+
const onInt = sweep(2);
|
|
113
|
+
const onTerm = sweep(15);
|
|
114
|
+
process.once('SIGINT', onInt);
|
|
115
|
+
process.once('SIGTERM', onTerm);
|
|
116
|
+
try {
|
|
117
|
+
await pipeline(Readable.fromWeb(res.body.pipeThrough(counting)), createWriteStream(part));
|
|
118
|
+
// Replacing a 0600 file must not widen it to the umask default the part was created with.
|
|
119
|
+
const mode = await stat(out).then((s) => s.mode, () => undefined);
|
|
120
|
+
if (mode !== undefined)
|
|
121
|
+
await chmod(part, mode);
|
|
122
|
+
await rename(part, out);
|
|
123
|
+
}
|
|
124
|
+
catch (e) {
|
|
125
|
+
await rm(part, { force: true });
|
|
126
|
+
throw e;
|
|
127
|
+
}
|
|
128
|
+
finally {
|
|
129
|
+
process.off('SIGINT', onInt);
|
|
130
|
+
process.off('SIGTERM', onTerm);
|
|
131
|
+
}
|
|
132
|
+
return written;
|
|
133
|
+
}
|
|
134
|
+
// Core, dependency-injected for tests (mirrors runWithSecrets): stream → disk, return byte count.
|
|
135
|
+
export async function saveObject(url, out, deps = {}) {
|
|
136
|
+
return (deps.streamTo ?? streamPresignedTo)(url, out);
|
|
137
|
+
}
|
|
138
|
+
export async function storageGet(key, opts, deps = {}) {
|
|
139
|
+
if (!key)
|
|
140
|
+
throw new Error('key is required');
|
|
141
|
+
const api = await ApiClient.load();
|
|
142
|
+
const p = await requireProject();
|
|
143
|
+
const branch = opts.branch ?? p.branch;
|
|
144
|
+
const svc = await storageTarget(api, p.projectId, branch, opts.service);
|
|
145
|
+
const res = await api.rawRequest('GET', objectDownloadPath(p.projectId, svc.id, { branch, key }));
|
|
146
|
+
if (handleApproval(res))
|
|
147
|
+
return;
|
|
148
|
+
// --json hands over the presigned URL instead of downloading, as `insta secrets --json` does.
|
|
149
|
+
// Before outputPath, so a key with no filename still works when nothing is written to disk.
|
|
150
|
+
if (opts.json)
|
|
151
|
+
return printJson(res.body);
|
|
152
|
+
const out = outputPath(key, opts.output);
|
|
153
|
+
if (!res.body?.url)
|
|
154
|
+
throw new Error('the platform returned no download URL');
|
|
155
|
+
const bytes = await saveObject(res.body.url, out, deps);
|
|
156
|
+
info(`wrote ${fmtBytes(bytes)} to ${out} (${key} from storage/${svc.name}, branch ${branch})`);
|
|
157
|
+
}
|
|
158
|
+
// No prompt, matching every other destructive command here — the governance gate is the guard.
|
|
159
|
+
export async function storageDelete(key, opts) {
|
|
160
|
+
if (!key)
|
|
161
|
+
throw new Error('key is required');
|
|
162
|
+
const api = await ApiClient.load();
|
|
163
|
+
const p = await requireProject();
|
|
164
|
+
const branch = opts.branch ?? p.branch;
|
|
165
|
+
const svc = await storageTarget(api, p.projectId, branch, opts.service);
|
|
166
|
+
const res = await api.rawRequest('DELETE', objectsPath(p.projectId, svc.id, { branch, key }));
|
|
167
|
+
if (handleApproval(res))
|
|
168
|
+
return;
|
|
169
|
+
if (opts.json)
|
|
170
|
+
return printJson(res.body);
|
|
171
|
+
info(`deleted ${key} from storage/${svc.name} (branch ${branch})`);
|
|
172
|
+
}
|
|
173
|
+
//# sourceMappingURL=storage.js.map
|
package/dist/index.js
CHANGED
|
@@ -19,6 +19,7 @@ import * as secretsCmd from './commands/secrets.js';
|
|
|
19
19
|
import { deploy } from './commands/deploy.js';
|
|
20
20
|
import * as computeCmd from './commands/compute.js';
|
|
21
21
|
import * as dbCmd from './commands/db.js';
|
|
22
|
+
import * as storageCmd from './commands/storage.js';
|
|
22
23
|
import { manifest } from './commands/manifest.js';
|
|
23
24
|
import * as govern from './commands/govern.js';
|
|
24
25
|
import * as observe from './commands/observe.js';
|
|
@@ -199,6 +200,25 @@ db.command('volume').description("Show or grow a postgres service's provisioned
|
|
|
199
200
|
.option('--size <gi>', 'new size in whole Gi, e.g. 10 (must be ≥ the current size)')
|
|
200
201
|
.option('--json').option('--branch <branch>', 'branch (default: current)').option('--group <g>', 'postgres service name (default: the sole/default one)')
|
|
201
202
|
.action(guard((o) => dbCmd.dbVolume(o)));
|
|
203
|
+
// ---- storage (bucket objects) ----
|
|
204
|
+
const storage = program.command('storage').description("Browse, download, and delete a storage service's bucket objects");
|
|
205
|
+
storage.command('list').description("List the bucket's objects. S3 filters by prefix only — there is no substring search")
|
|
206
|
+
.option('--prefix <p>', 'only keys starting with this prefix (applied server-side)')
|
|
207
|
+
.option('--cursor <c>', 'continue from the nextCursor a previous page printed')
|
|
208
|
+
.option('--limit <n>', 'page size, 1..1000 (default 100)')
|
|
209
|
+
.option('--service <name>', 'storage service (default: the sole one on the branch)')
|
|
210
|
+
.option('--branch <b>', 'branch (default: current)').option('--json')
|
|
211
|
+
.action(guard((o) => storageCmd.storageList(o)));
|
|
212
|
+
storage.command('get <key>').description('Download one object to disk through a short-lived presigned URL (bytes come straight from the provider)')
|
|
213
|
+
.option('-o, --output <file>', "output file (default: the key's last segment)")
|
|
214
|
+
.option('--service <name>', 'storage service (default: the sole one on the branch)')
|
|
215
|
+
.option('--branch <b>', 'branch (default: current)')
|
|
216
|
+
.option('--json', 'print the presigned URL + expiry instead of downloading')
|
|
217
|
+
.action(guard((key, o) => storageCmd.storageGet(key, o)));
|
|
218
|
+
storage.command('delete <key>').description('DELETES one object from the bucket immediately — no undo, and an already-gone key still reports success (gated: storage.delete)')
|
|
219
|
+
.option('--service <name>', 'storage service (default: the sole one on the branch)')
|
|
220
|
+
.option('--branch <b>', 'branch (default: current)').option('--json')
|
|
221
|
+
.action(guard((key, o) => storageCmd.storageDelete(key, o)));
|
|
202
222
|
// ---- manifest ----
|
|
203
223
|
program.command('manifest').description('Print an agent-legible view of the project environments').option('--json').action(guard((o) => manifest(o)));
|
|
204
224
|
// ---- regions ----
|
|
@@ -238,7 +258,7 @@ ob.command('sync').description('Upload findings into the project timeline').acti
|
|
|
238
258
|
// ---- policy ----
|
|
239
259
|
const pol = program.command('policy').description('Governance policy');
|
|
240
260
|
pol.command('get').option('--json').action(guard((o) => govern.policyGet(o)));
|
|
241
|
-
pol.command('set <action> <decision>').description('action: secrets.read|secrets.write|deploy|project.delete|branch.delete|service.add|service.remove|service.scale|service.upgrade|service.setAccess; decision: allow|deny|approve').action(guard((a, d) => govern.policySet(a, d)));
|
|
261
|
+
pol.command('set <action> <decision>').description('action: secrets.read|secrets.write|deploy|project.delete|branch.delete|service.add|service.remove|service.scale|service.upgrade|service.setAccess|storage.read|storage.write|storage.delete; decision: allow|deny|approve').action(guard((a, d) => govern.policySet(a, d)));
|
|
242
262
|
// ---- self-update ----
|
|
243
263
|
program.command('upgrade').description('Update the insta CLI to the latest release (binary or npm install)')
|
|
244
264
|
.action(guard(() => selfUpdate.upgrade()));
|