@raytrace-cli/sdk 0.2.10 → 0.2.12

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@raytrace-cli/sdk",
3
- "version": "0.2.10",
3
+ "version": "0.2.12",
4
4
  "description": "TypeScript/JavaScript SDK for RayTrace cloud sandboxes: create, run commands, stop, start and destroy over the /v1 API",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/client.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  // Shared low-level /v1 HTTP client for @raytrace-cli/sdk.
2
2
  // Zero runtime dependencies (Node 20+ native fetch + zlib + crypto).
3
3
  import { gzipSync } from 'node:zlib';
4
+ import { randomBytes } from 'node:crypto';
4
5
  import { mkdtempSync, readdirSync, readFileSync, rmSync, statSync } from 'node:fs';
5
6
  import { join, posix } from 'node:path';
6
7
  import { execFile } from 'node:child_process';
@@ -54,6 +55,30 @@ export function buildTarGz(files) {
54
55
  return buildTarGzWithModes(files);
55
56
  }
56
57
 
58
+ /** Builds a multipart/form-data body with JSON `metadata` and `.tar.gz` `archive`. */
59
+ export function buildMultipartSandboxBody(metadata, archive) {
60
+ const boundary = `----RayTraceSdkBoundary${randomBytes(12).toString('hex')}`;
61
+ const head1 = Buffer.from(
62
+ `--${boundary}\r\n`
63
+ + 'Content-Disposition: form-data; name="metadata"; filename="metadata.json"\r\n'
64
+ + 'Content-Type: application/json\r\n\r\n'
65
+ + JSON.stringify(metadata)
66
+ + '\r\n',
67
+ 'utf8',
68
+ );
69
+ const head2 = Buffer.from(
70
+ `--${boundary}\r\n`
71
+ + 'Content-Disposition: form-data; name="archive"; filename="project.tar.gz"\r\n'
72
+ + 'Content-Type: application/gzip\r\n\r\n',
73
+ 'utf8',
74
+ );
75
+ const tail = Buffer.from(`\r\n--${boundary}--\r\n`, 'utf8');
76
+ return {
77
+ contentType: `multipart/form-data; boundary=${boundary}`,
78
+ body: Buffer.concat([head1, head2, Buffer.from(archive), tail]),
79
+ };
80
+ }
81
+
57
82
  function buildTarGzWithModes(files, modes = new Map()) {
58
83
  const chunks = [];
59
84
  const entries = Object.entries(files || {});
package/src/index.d.ts CHANGED
@@ -27,6 +27,71 @@ export interface SandboxSummary {
27
27
  restored_from_archive?: boolean;
28
28
  restore_warning?: string;
29
29
  restore_error?: string;
30
+ env_profile_id?: string | null;
31
+ env_profile_name?: string | null;
32
+ master_id?: string | null;
33
+ master_version?: number | null;
34
+ master_name?: string | null;
35
+ }
36
+
37
+ export interface MasterSnapshotSummary {
38
+ master_id: string;
39
+ version: number;
40
+ status: 'queued' | 'publishing' | 'ready' | 'failed' | 'pruned';
41
+ base_release: string;
42
+ runtime_abi?: string | null;
43
+ size_bytes?: number | null;
44
+ dependencies?: {
45
+ npm?: Array<{ name: string; version: string }>;
46
+ pypi?: Array<{ name: string; version: string }>;
47
+ binaries?: string[];
48
+ dirs?: Record<string, number>;
49
+ } | null;
50
+ error?: string | null;
51
+ created: number;
52
+ ready_at?: number | null;
53
+ }
54
+
55
+ export interface MasterSandboxSummary {
56
+ id: string;
57
+ org_id?: string;
58
+ name: string;
59
+ description: string;
60
+ setup_sandbox_id?: string | null;
61
+ active_version?: number | null;
62
+ default_env_profile_id?: string | null;
63
+ latest_snapshot?: MasterSnapshotSummary | null;
64
+ created: number;
65
+ updated: number;
66
+ }
67
+
68
+ export interface EnvProfileSummary {
69
+ id: string;
70
+ org_id?: string;
71
+ name: string;
72
+ description: string;
73
+ member_usable: boolean;
74
+ keys: string[];
75
+ key_count: number;
76
+ created_by?: string | null;
77
+ created: number;
78
+ updated: number;
79
+ }
80
+
81
+ export interface CreateEnvProfileOptions {
82
+ name: string;
83
+ description?: string;
84
+ memberUsable?: boolean;
85
+ variables: Record<string, string>;
86
+ }
87
+
88
+ export interface UpdateEnvProfileOptions {
89
+ name?: string;
90
+ description?: string;
91
+ memberUsable?: boolean;
92
+ variables?: Record<string, string>;
93
+ set?: Record<string, string>;
94
+ unset?: string[];
30
95
  }
31
96
 
32
97
  export interface CreateSandboxOptions {
@@ -47,6 +112,10 @@ export interface CreateSandboxOptions {
47
112
  * what is uploaded and `repo` is only what it is connected to.
48
113
  */
49
114
  connect?: boolean;
115
+ /** Initialize `/opt/env` dependencies from a published organization master sandbox (by ID or name). */
116
+ master?: string;
117
+ /** Attach an encrypted organization env profile (by ID or name). */
118
+ envProfile?: string;
50
119
  idempotencyKey?: string;
51
120
  }
52
121
 
@@ -122,9 +191,15 @@ export interface SandboxRepos {
122
191
  set(repos: string[]): Promise<string[]>;
123
192
  }
124
193
 
194
+ export interface SandboxEnvProfile {
195
+ get(): Promise<EnvProfileSummary | null>;
196
+ set(envProfile: string | null): Promise<EnvProfileSummary | null>;
197
+ }
198
+
125
199
  export class Sandbox {
126
200
  readonly id: string;
127
201
  readonly repos: SandboxRepos;
202
+ readonly envProfile: SandboxEnvProfile;
128
203
  /** Set by create({ connect: true }). */
129
204
  git?: ConnectedRepo;
130
205
  info: SandboxSummary;
@@ -160,6 +235,21 @@ export class RayTrace {
160
235
  list(): Promise<ApiKeySummary[]>;
161
236
  revoke(id: string): Promise<{ id: string; revoked: boolean }>;
162
237
  };
238
+ readonly envProfiles: {
239
+ list(): Promise<EnvProfileSummary[]>;
240
+ get(id: string): Promise<EnvProfileSummary>;
241
+ create(options: CreateEnvProfileOptions): Promise<EnvProfileSummary>;
242
+ update(id: string, options?: UpdateEnvProfileOptions): Promise<EnvProfileSummary>;
243
+ delete(id: string): Promise<{ id: string; deleted: boolean; detached_sandboxes: number }>;
244
+ };
245
+ readonly masters: {
246
+ list(): Promise<MasterSandboxSummary[]>;
247
+ get(id: string): Promise<MasterSandboxSummary & { versions?: MasterSnapshotSummary[] }>;
248
+ create(options: { name: string; description?: string; size?: SandboxSize; defaultEnvProfile?: string; setupScript?: string }): Promise<MasterSandboxSummary>;
249
+ publish(id: string, options?: { wait?: boolean; pollIntervalMs?: number }): Promise<MasterSnapshotSummary>;
250
+ getVersion(id: string, version: number): Promise<MasterSnapshotSummary>;
251
+ delete(id: string): Promise<{ id: string; deleted: boolean }>;
252
+ };
163
253
  constructor(options?: RayTraceOptions);
164
254
  }
165
255
 
package/src/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  // @raytrace-cli/sdk — Typed high-level SDK over RayTrace's frozen /v1 API.
2
2
  import { randomBytes } from 'node:crypto';
3
- import { RayTraceClient, RayTraceError, buildTarGz, archiveFromDirectory, archiveFromRepo, repoName, githubRepo, workBranch, connectScript } from './client.mjs';
3
+ import { RayTraceClient, RayTraceError, buildTarGz, buildMultipartSandboxBody, archiveFromDirectory, archiveFromRepo, repoName, githubRepo, workBranch, connectScript } from './client.mjs';
4
4
 
5
5
  export { RayTraceClient, RayTraceError, buildTarGz, archiveFromDirectory, archiveFromRepo, githubRepo, workBranch, connectScript };
6
6
 
@@ -39,12 +39,38 @@ class SandboxRepos {
39
39
  }
40
40
  }
41
41
 
42
+ class SandboxEnvProfile {
43
+ constructor(client, sandbox) {
44
+ this._client = client;
45
+ this._sandbox = sandbox;
46
+ this._path = `/v1/sandboxes/${encodeURIComponent(sandbox.id)}/env-profile`;
47
+ }
48
+
49
+ async get() {
50
+ const { data } = await this._client.request('GET', this._path);
51
+ return data?.env_profile || null;
52
+ }
53
+
54
+ /** Attaches an env profile by ID or name, or detaches when passed null. */
55
+ async set(envProfile) {
56
+ const { data } = await this._client.request('PUT', this._path, { body: { env_profile: envProfile ?? null } });
57
+ const prof = data?.env_profile || null;
58
+ this._sandbox.info = {
59
+ ...this._sandbox.info,
60
+ env_profile_id: prof?.id ?? null,
61
+ env_profile_name: prof?.name ?? null,
62
+ };
63
+ return prof;
64
+ }
65
+ }
66
+
42
67
  export class Sandbox {
43
68
  constructor(client, info) {
44
69
  this._client = client;
45
70
  this.id = info.id;
46
71
  this.info = info;
47
72
  this.repos = new SandboxRepos(client, info.id);
73
+ this.envProfile = new SandboxEnvProfile(client, this);
48
74
  }
49
75
 
50
76
  get status() { return this.info.status; }
@@ -163,7 +189,7 @@ class SandboxesResource {
163
189
  return found;
164
190
  }
165
191
 
166
- async create({ name = undefined, size = 'small', archive = undefined, files = undefined, dir = undefined, repo = undefined, ref = undefined, connect = false, idempotencyKey = undefined } = {}) {
192
+ async create({ name = undefined, size = 'small', archive = undefined, files = undefined, dir = undefined, repo = undefined, ref = undefined, connect = false, master = undefined, envProfile = undefined, idempotencyKey = undefined } = {}) {
167
193
  if (connect) {
168
194
  // Checked before anything is uploaded: connectScript throws on a ref it would not run.
169
195
  if (!githubRepo(repo)) throw new Error('connect needs repo: a GitHub repository, as owner/name');
@@ -177,19 +203,30 @@ class SandboxesResource {
177
203
  payload = archiveFromDirectory(dir);
178
204
  } else if (repo) {
179
205
  payload = await archiveFromRepo(repo, { ref });
180
- } else {
181
- throw new Error('Provide one of { archive, files, dir, repo } to create a sandbox');
206
+ } else if (!master) {
207
+ throw new Error('Provide one of { archive, files, dir, repo, master } to create a sandbox');
182
208
  }
183
209
  }
184
- const derivedName = name || (repo ? repoName(repo) : 'sandbox');
210
+ const derivedName = name || (repo ? repoName(repo) : master ? `${master}-sandbox` : 'sandbox');
185
211
  const cleanName = String(derivedName).replace(/[^A-Za-z0-9._ -]/g, '-').slice(0, 100) || 'sandbox';
186
- const headers = {
187
- 'Content-Type': 'application/gzip',
188
- 'X-RayTrace-Name': cleanName,
189
- 'X-RayTrace-Size': size,
190
- };
212
+ let headers = {};
213
+ let body;
214
+ if (!payload) {
215
+ body = { name: cleanName, size, ...(master ? { master } : {}), ...(envProfile ? { env_profile: envProfile } : {}) };
216
+ } else if (envProfile || master) {
217
+ const mp = buildMultipartSandboxBody({ name: cleanName, size, ...(master ? { master } : {}), ...(envProfile ? { env_profile: envProfile } : {}) }, payload);
218
+ headers = { 'Content-Type': mp.contentType };
219
+ body = mp.body;
220
+ } else {
221
+ headers = {
222
+ 'Content-Type': 'application/gzip',
223
+ 'X-RayTrace-Name': cleanName,
224
+ 'X-RayTrace-Size': size,
225
+ };
226
+ body = payload;
227
+ }
191
228
  if (idempotencyKey) headers['Idempotency-Key'] = idempotencyKey;
192
- const { data } = await this._client.request('POST', '/v1/sandboxes', { headers, body: payload });
229
+ const { data } = await this._client.request('POST', '/v1/sandboxes', { headers, body });
193
230
  const sandbox = new Sandbox(this._client, data);
194
231
  if (connect) {
195
232
  try {
@@ -237,12 +274,103 @@ class ApiKeysResource {
237
274
  }
238
275
  }
239
276
 
277
+ class EnvProfilesResource {
278
+ constructor(client) {
279
+ this._client = client;
280
+ }
281
+
282
+ async list() {
283
+ const { data } = await this._client.request('GET', '/v1/env-profiles');
284
+ return data?.profiles || [];
285
+ }
286
+
287
+ async get(id) {
288
+ const { data } = await this._client.request('GET', `/v1/env-profiles/${encodeURIComponent(id)}`);
289
+ return data;
290
+ }
291
+
292
+ async create({ name, description = '', memberUsable = true, variables = {} }) {
293
+ const { data } = await this._client.request('POST', '/v1/env-profiles', {
294
+ body: { name, description, member_usable: memberUsable, variables },
295
+ });
296
+ return data;
297
+ }
298
+
299
+ async update(id, { name = undefined, description = undefined, memberUsable = undefined, variables = undefined, set = undefined, unset = undefined } = {}) {
300
+ const body = {};
301
+ if (name !== undefined) body.name = name;
302
+ if (description !== undefined) body.description = description;
303
+ if (memberUsable !== undefined) body.member_usable = memberUsable;
304
+ if (variables !== undefined) body.variables = variables;
305
+ if (set !== undefined) body.set = set;
306
+ if (unset !== undefined) body.unset = unset;
307
+ const { data } = await this._client.request('PUT', `/v1/env-profiles/${encodeURIComponent(id)}`, { body });
308
+ return data;
309
+ }
310
+
311
+ async delete(id) {
312
+ const { data } = await this._client.request('DELETE', `/v1/env-profiles/${encodeURIComponent(id)}`);
313
+ return data;
314
+ }
315
+ }
316
+
317
+ class MastersResource {
318
+ constructor(client) {
319
+ this._client = client;
320
+ }
321
+
322
+ async list() {
323
+ const { data } = await this._client.request('GET', '/v1/masters');
324
+ return data?.masters || [];
325
+ }
326
+
327
+ async get(id) {
328
+ const { data } = await this._client.request('GET', `/v1/masters/${encodeURIComponent(id)}`);
329
+ return data;
330
+ }
331
+
332
+ async create({ name, description = '', size = 'small', defaultEnvProfile = undefined, setupScript = undefined }) {
333
+ const body = { name, description, size };
334
+ if (defaultEnvProfile !== undefined) body.default_env_profile = defaultEnvProfile;
335
+ if (setupScript !== undefined) body.setup_script = setupScript;
336
+ const { data } = await this._client.request('POST', '/v1/masters', { body });
337
+ return data;
338
+ }
339
+
340
+ async publish(id, { wait = true, pollIntervalMs = 1000 } = {}) {
341
+ const { data: started } = await this._client.request('POST', `/v1/masters/${encodeURIComponent(id)}/publish`, { body: {} });
342
+ if (!wait) return started;
343
+ let current = started;
344
+ while (current.status === 'queued' || current.status === 'publishing') {
345
+ await sleep(pollIntervalMs);
346
+ const { data: latest } = await this._client.request('GET', `/v1/masters/${encodeURIComponent(id)}/versions/${encodeURIComponent(started.version)}`);
347
+ current = latest;
348
+ }
349
+ if (current.status === 'failed') {
350
+ throw new RayTraceError(current.error || 'Master publish failed', { status: 500, body: current });
351
+ }
352
+ return current;
353
+ }
354
+
355
+ async getVersion(id, version) {
356
+ const { data } = await this._client.request('GET', `/v1/masters/${encodeURIComponent(id)}/versions/${encodeURIComponent(version)}`);
357
+ return data;
358
+ }
359
+
360
+ async delete(id) {
361
+ const { data } = await this._client.request('DELETE', `/v1/masters/${encodeURIComponent(id)}`);
362
+ return data;
363
+ }
364
+ }
365
+
240
366
  export class RayTrace {
241
367
  constructor(options = {}) {
242
368
  this.client = new RayTraceClient(options);
243
369
  this.sandboxes = new SandboxesResource(this.client);
244
370
  this.usage = new UsageResource(this.client);
245
371
  this.apiKeys = new ApiKeysResource(this.client);
372
+ this.envProfiles = new EnvProfilesResource(this.client);
373
+ this.masters = new MastersResource(this.client);
246
374
  }
247
375
  }
248
376