@raytrace-cli/sdk 0.2.10 → 0.2.11
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 +1 -1
- package/src/client.mjs +25 -0
- package/src/index.d.ts +46 -0
- package/src/index.mjs +84 -8
package/package.json
CHANGED
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,37 @@ 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
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface EnvProfileSummary {
|
|
35
|
+
id: string;
|
|
36
|
+
org_id?: string;
|
|
37
|
+
name: string;
|
|
38
|
+
description: string;
|
|
39
|
+
member_usable: boolean;
|
|
40
|
+
keys: string[];
|
|
41
|
+
key_count: number;
|
|
42
|
+
created_by?: string | null;
|
|
43
|
+
created: number;
|
|
44
|
+
updated: number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface CreateEnvProfileOptions {
|
|
48
|
+
name: string;
|
|
49
|
+
description?: string;
|
|
50
|
+
memberUsable?: boolean;
|
|
51
|
+
variables: Record<string, string>;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface UpdateEnvProfileOptions {
|
|
55
|
+
name?: string;
|
|
56
|
+
description?: string;
|
|
57
|
+
memberUsable?: boolean;
|
|
58
|
+
variables?: Record<string, string>;
|
|
59
|
+
set?: Record<string, string>;
|
|
60
|
+
unset?: string[];
|
|
30
61
|
}
|
|
31
62
|
|
|
32
63
|
export interface CreateSandboxOptions {
|
|
@@ -47,6 +78,8 @@ export interface CreateSandboxOptions {
|
|
|
47
78
|
* what is uploaded and `repo` is only what it is connected to.
|
|
48
79
|
*/
|
|
49
80
|
connect?: boolean;
|
|
81
|
+
/** Attach an encrypted organization env profile (by ID or name). */
|
|
82
|
+
envProfile?: string;
|
|
50
83
|
idempotencyKey?: string;
|
|
51
84
|
}
|
|
52
85
|
|
|
@@ -122,9 +155,15 @@ export interface SandboxRepos {
|
|
|
122
155
|
set(repos: string[]): Promise<string[]>;
|
|
123
156
|
}
|
|
124
157
|
|
|
158
|
+
export interface SandboxEnvProfile {
|
|
159
|
+
get(): Promise<EnvProfileSummary | null>;
|
|
160
|
+
set(envProfile: string | null): Promise<EnvProfileSummary | null>;
|
|
161
|
+
}
|
|
162
|
+
|
|
125
163
|
export class Sandbox {
|
|
126
164
|
readonly id: string;
|
|
127
165
|
readonly repos: SandboxRepos;
|
|
166
|
+
readonly envProfile: SandboxEnvProfile;
|
|
128
167
|
/** Set by create({ connect: true }). */
|
|
129
168
|
git?: ConnectedRepo;
|
|
130
169
|
info: SandboxSummary;
|
|
@@ -160,6 +199,13 @@ export class RayTrace {
|
|
|
160
199
|
list(): Promise<ApiKeySummary[]>;
|
|
161
200
|
revoke(id: string): Promise<{ id: string; revoked: boolean }>;
|
|
162
201
|
};
|
|
202
|
+
readonly envProfiles: {
|
|
203
|
+
list(): Promise<EnvProfileSummary[]>;
|
|
204
|
+
get(id: string): Promise<EnvProfileSummary>;
|
|
205
|
+
create(options: CreateEnvProfileOptions): Promise<EnvProfileSummary>;
|
|
206
|
+
update(id: string, options?: UpdateEnvProfileOptions): Promise<EnvProfileSummary>;
|
|
207
|
+
delete(id: string): Promise<{ id: string; deleted: boolean; detached_sandboxes: number }>;
|
|
208
|
+
};
|
|
163
209
|
constructor(options?: RayTraceOptions);
|
|
164
210
|
}
|
|
165
211
|
|
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, 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');
|
|
@@ -183,13 +209,22 @@ class SandboxesResource {
|
|
|
183
209
|
}
|
|
184
210
|
const derivedName = name || (repo ? repoName(repo) : 'sandbox');
|
|
185
211
|
const cleanName = String(derivedName).replace(/[^A-Za-z0-9._ -]/g, '-').slice(0, 100) || 'sandbox';
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
212
|
+
let headers;
|
|
213
|
+
let body;
|
|
214
|
+
if (envProfile) {
|
|
215
|
+
const mp = buildMultipartSandboxBody({ name: cleanName, size, env_profile: envProfile }, payload);
|
|
216
|
+
headers = { 'Content-Type': mp.contentType };
|
|
217
|
+
body = mp.body;
|
|
218
|
+
} else {
|
|
219
|
+
headers = {
|
|
220
|
+
'Content-Type': 'application/gzip',
|
|
221
|
+
'X-RayTrace-Name': cleanName,
|
|
222
|
+
'X-RayTrace-Size': size,
|
|
223
|
+
};
|
|
224
|
+
body = payload;
|
|
225
|
+
}
|
|
191
226
|
if (idempotencyKey) headers['Idempotency-Key'] = idempotencyKey;
|
|
192
|
-
const { data } = await this._client.request('POST', '/v1/sandboxes', { headers, body
|
|
227
|
+
const { data } = await this._client.request('POST', '/v1/sandboxes', { headers, body });
|
|
193
228
|
const sandbox = new Sandbox(this._client, data);
|
|
194
229
|
if (connect) {
|
|
195
230
|
try {
|
|
@@ -237,12 +272,53 @@ class ApiKeysResource {
|
|
|
237
272
|
}
|
|
238
273
|
}
|
|
239
274
|
|
|
275
|
+
class EnvProfilesResource {
|
|
276
|
+
constructor(client) {
|
|
277
|
+
this._client = client;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
async list() {
|
|
281
|
+
const { data } = await this._client.request('GET', '/v1/env-profiles');
|
|
282
|
+
return data?.profiles || [];
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
async get(id) {
|
|
286
|
+
const { data } = await this._client.request('GET', `/v1/env-profiles/${encodeURIComponent(id)}`);
|
|
287
|
+
return data;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
async create({ name, description = '', memberUsable = true, variables = {} }) {
|
|
291
|
+
const { data } = await this._client.request('POST', '/v1/env-profiles', {
|
|
292
|
+
body: { name, description, member_usable: memberUsable, variables },
|
|
293
|
+
});
|
|
294
|
+
return data;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
async update(id, { name = undefined, description = undefined, memberUsable = undefined, variables = undefined, set = undefined, unset = undefined } = {}) {
|
|
298
|
+
const body = {};
|
|
299
|
+
if (name !== undefined) body.name = name;
|
|
300
|
+
if (description !== undefined) body.description = description;
|
|
301
|
+
if (memberUsable !== undefined) body.member_usable = memberUsable;
|
|
302
|
+
if (variables !== undefined) body.variables = variables;
|
|
303
|
+
if (set !== undefined) body.set = set;
|
|
304
|
+
if (unset !== undefined) body.unset = unset;
|
|
305
|
+
const { data } = await this._client.request('PUT', `/v1/env-profiles/${encodeURIComponent(id)}`, { body });
|
|
306
|
+
return data;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
async delete(id) {
|
|
310
|
+
const { data } = await this._client.request('DELETE', `/v1/env-profiles/${encodeURIComponent(id)}`);
|
|
311
|
+
return data;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
240
315
|
export class RayTrace {
|
|
241
316
|
constructor(options = {}) {
|
|
242
317
|
this.client = new RayTraceClient(options);
|
|
243
318
|
this.sandboxes = new SandboxesResource(this.client);
|
|
244
319
|
this.usage = new UsageResource(this.client);
|
|
245
320
|
this.apiKeys = new ApiKeysResource(this.client);
|
|
321
|
+
this.envProfiles = new EnvProfilesResource(this.client);
|
|
246
322
|
}
|
|
247
323
|
}
|
|
248
324
|
|