@brimble/sandbox 0.1.0 → 0.1.2

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.
Files changed (61) hide show
  1. package/README.md +9 -5
  2. package/dist/package.json +10 -3
  3. package/dist/src/client.d.ts +3 -3
  4. package/dist/src/client.js +3 -3
  5. package/dist/src/index.d.ts +3 -3
  6. package/dist/src/index.js +2 -2
  7. package/dist/src/resources/files.d.ts +6 -1
  8. package/dist/src/resources/files.js +36 -0
  9. package/dist/src/resources/sandbox-handle.d.ts +6 -1
  10. package/dist/src/resources/sandbox-handle.js +8 -0
  11. package/dist/src/resources/scoped-sandbox.d.ts +3 -1
  12. package/dist/src/resources/scoped-sandbox.js +4 -0
  13. package/dist/src/types/files.d.ts +16 -0
  14. package/dist/src/types/index.d.ts +1 -1
  15. package/package.json +7 -3
  16. package/CODEX.md +0 -188
  17. package/PLAN.md +0 -364
  18. package/src/client.ts +0 -61
  19. package/src/constants.ts +0 -17
  20. package/src/enums/code-language.ts +0 -4
  21. package/src/enums/destroy-reason.ts +0 -8
  22. package/src/enums/destroy-timeout.ts +0 -8
  23. package/src/enums/index.ts +0 -7
  24. package/src/enums/sandbox-status.ts +0 -9
  25. package/src/enums/snapshot-mode.ts +0 -4
  26. package/src/enums/snapshot-status.ts +0 -5
  27. package/src/enums/volume-type.ts +0 -3
  28. package/src/errors/index.ts +0 -2
  29. package/src/errors/sandbox-api-error.ts +0 -54
  30. package/src/index.ts +0 -71
  31. package/src/resources/exec.ts +0 -56
  32. package/src/resources/files.ts +0 -46
  33. package/src/resources/index.ts +0 -8
  34. package/src/resources/path.ts +0 -16
  35. package/src/resources/sandbox-handle.ts +0 -215
  36. package/src/resources/sandboxes.ts +0 -297
  37. package/src/resources/scoped-sandbox.ts +0 -65
  38. package/src/resources/snapshots.ts +0 -104
  39. package/src/resources/stats.ts +0 -30
  40. package/src/resources/volumes.ts +0 -95
  41. package/src/transport/auth.ts +0 -4
  42. package/src/transport/http.ts +0 -501
  43. package/src/transport/pagination.ts +0 -10
  44. package/src/types/exec.ts +0 -42
  45. package/src/types/files.ts +0 -1
  46. package/src/types/index.ts +0 -23
  47. package/src/types/pagination.ts +0 -16
  48. package/src/types/region.ts +0 -19
  49. package/src/types/sandbox.ts +0 -103
  50. package/src/types/snapshot.ts +0 -17
  51. package/src/types/stats.ts +0 -35
  52. package/src/types/template.ts +0 -5
  53. package/src/types/volume.ts +0 -26
  54. package/test/integration/sandbox.integration.test.ts +0 -269
  55. package/test/unit/client.test.ts +0 -87
  56. package/test/unit/sandboxes.test.ts +0 -69
  57. package/test/unit/transport.test.ts +0 -126
  58. package/test/unit/volumes.test.ts +0 -122
  59. package/tsconfig.json +0 -16
  60. package/vitest.config.ts +0 -12
  61. package/vitest.integration.config.ts +0 -15
@@ -1,65 +0,0 @@
1
- import { ExecResource } from './exec';
2
- import { FilesResource } from './files';
3
- import { SnapshotScopeResource } from './snapshots';
4
- import { StatsResource } from './stats';
5
- import { HttpTransport } from '../transport/http';
6
- import type { CodeInput, CreateSnapshotInput, ExecInput, ExecResult, FileUploadBody, Paginated, Pagination, Snapshot, Stats, StatsQuery } from '../types';
7
- import type { RequestOptions } from '../transport/http';
8
-
9
- export class ScopedSandboxResource {
10
- /** Lower-level exec/code runner resource. */
11
- public readonly execResource: ExecResource;
12
- /** File upload/download resource. */
13
- public readonly files: FilesResource;
14
- /** Snapshot resource scoped to this sandbox. */
15
- public readonly snapshots: SnapshotScopeResource;
16
- /** Stats resource scoped to this sandbox. */
17
- public readonly statsResource: StatsResource;
18
-
19
- /** @internal Create a sandbox-scoped resource wrapper. */
20
- public constructor(transport: HttpTransport, sandboxId: string) {
21
- this.execResource = new ExecResource(transport, sandboxId);
22
- this.files = new FilesResource(transport, sandboxId);
23
- this.snapshots = new SnapshotScopeResource(transport, sandboxId);
24
- this.statsResource = new StatsResource(transport, sandboxId);
25
- }
26
-
27
- /** Run a shell command in this sandbox. */
28
- public exec(input: ExecInput & { stream: true }, options?: RequestOptions): Promise<ReadableStream<Uint8Array>>;
29
- public exec(input: ExecInput, options?: RequestOptions): Promise<ExecResult>;
30
- public exec(input: ExecInput, options?: RequestOptions): Promise<ExecResult | ReadableStream<Uint8Array>> {
31
- return this.execResource.exec(input, options);
32
- }
33
-
34
- /** Run a code snippet in this sandbox. */
35
- public runCode(input: CodeInput & { stream: true }, options?: RequestOptions): Promise<ReadableStream<Uint8Array>>;
36
- public runCode(input: CodeInput, options?: RequestOptions): Promise<ExecResult>;
37
- public runCode(input: CodeInput, options?: RequestOptions): Promise<ExecResult | ReadableStream<Uint8Array>> {
38
- return this.execResource.runCode(input, options);
39
- }
40
-
41
- /** Upload bytes to a file path inside this sandbox. */
42
- public putFile(path: string, body: FileUploadBody, options?: RequestOptions): Promise<void> {
43
- return this.files.put(path, body, options);
44
- }
45
-
46
- /** Download file bytes from this sandbox as a stream. */
47
- public getFile(path: string, options?: RequestOptions): Promise<ReadableStream<Uint8Array>> {
48
- return this.files.get(path, options);
49
- }
50
-
51
- /** Fetch CPU, memory, and network stats for this sandbox. */
52
- public stats(query: StatsQuery = {}, options?: RequestOptions): Promise<Stats> {
53
- return this.statsResource.stats(query, options);
54
- }
55
-
56
- /** Create a snapshot for this sandbox. */
57
- public createSnapshot(input: CreateSnapshotInput, options?: RequestOptions): Promise<Snapshot> {
58
- return this.snapshots.create(input, options);
59
- }
60
-
61
- /** List snapshots for this sandbox. */
62
- public listSnapshots(query: Pagination = {}, options?: RequestOptions): Promise<Paginated<Snapshot>> {
63
- return this.snapshots.list(query, options);
64
- }
65
- }
@@ -1,104 +0,0 @@
1
- import type { RequestOptions } from '../transport/http';
2
- import { HttpTransport } from '../transport/http';
3
- import { toPaginationQuery } from '../transport/pagination';
4
- import type { CreateSnapshotInput, Paginated, Pagination, Snapshot } from '../types';
5
- import { DEFAULT_PAGE, DEFAULT_PAGE_LIMIT } from '../constants';
6
-
7
- export class SnapshotScopeResource {
8
- private readonly transport: HttpTransport;
9
- private readonly sandboxId: string;
10
-
11
- /** @internal Create a sandbox-scoped snapshots wrapper. */
12
- public constructor(transport: HttpTransport, sandboxId: string) {
13
- this.transport = transport;
14
- this.sandboxId = sandboxId;
15
- }
16
-
17
- /** Create a snapshot for this specific sandbox. */
18
- public create(input: CreateSnapshotInput, options?: RequestOptions): Promise<Snapshot> {
19
- return this.transport.requestJson<Snapshot>({
20
- endpoint: `/sandboxes/${this.sandboxId}/snapshots`,
21
- method: 'POST',
22
- body: input,
23
- ...options,
24
- }) as Promise<Snapshot>;
25
- }
26
-
27
- /** List snapshots for this specific sandbox. */
28
- public list(query: Pagination = {}, options?: RequestOptions): Promise<Paginated<Snapshot>> {
29
- return this.transport.requestJson<Paginated<Snapshot>>({
30
- endpoint: `/sandboxes/${this.sandboxId}/snapshots`,
31
- method: 'GET',
32
- query: toPaginationQuery(query),
33
- ...options,
34
- }) as Promise<Paginated<Snapshot>>;
35
- }
36
-
37
- /** Iterate over all snapshots for this sandbox across pages. */
38
- public async *iterate(query: Pagination = {}, options?: RequestOptions): AsyncGenerator<Snapshot> {
39
- const limit = query.limit ?? DEFAULT_PAGE_LIMIT;
40
- let page = query.page ?? DEFAULT_PAGE;
41
-
42
- while (true) {
43
- const paginated = await this.list({ ...query, page, limit }, options);
44
-
45
- for (const snapshot of paginated.data) {
46
- yield snapshot;
47
- }
48
-
49
- if (page >= paginated.totalPages || paginated.data.length === 0) {
50
- return;
51
- }
52
-
53
- page += 1;
54
- }
55
- }
56
- }
57
-
58
- export class SnapshotsResource {
59
- private readonly transport: HttpTransport;
60
-
61
- /** @internal Create the global snapshots resource wrapper. */
62
- public constructor(transport: HttpTransport) {
63
- this.transport = transport;
64
- }
65
-
66
- /** List all snapshots owned by the current caller. */
67
- public listAll(query: Pagination = {}, options?: RequestOptions): Promise<Paginated<Snapshot>> {
68
- return this.transport.requestJson<Paginated<Snapshot>>({
69
- endpoint: '/sandboxes/snapshots',
70
- method: 'GET',
71
- query: toPaginationQuery(query),
72
- ...options,
73
- }) as Promise<Paginated<Snapshot>>;
74
- }
75
-
76
- /** Iterate over all snapshots owned by the caller across pages. */
77
- public async *iterateAll(query: Pagination = {}, options?: RequestOptions): AsyncGenerator<Snapshot> {
78
- const limit = query.limit ?? DEFAULT_PAGE_LIMIT;
79
- let page = query.page ?? DEFAULT_PAGE;
80
-
81
- while (true) {
82
- const paginated = await this.listAll({ ...query, page, limit }, options);
83
-
84
- for (const snapshot of paginated.data) {
85
- yield snapshot;
86
- }
87
-
88
- if (page >= paginated.totalPages || paginated.data.length === 0) {
89
- return;
90
- }
91
-
92
- page += 1;
93
- }
94
- }
95
-
96
- /** Delete a snapshot by id. */
97
- public async delete(snapshotId: string, options?: RequestOptions): Promise<void> {
98
- await this.transport.requestJson({
99
- endpoint: `/sandboxes/snapshots/${snapshotId}`,
100
- method: 'DELETE',
101
- ...options,
102
- });
103
- }
104
- }
@@ -1,30 +0,0 @@
1
- import type { Stats, StatsQuery } from '../types';
2
- import type { RequestOptions } from '../transport/http';
3
- import { HttpTransport } from '../transport/http';
4
-
5
- export class StatsResource {
6
- private readonly transport: HttpTransport;
7
- private readonly sandboxId: string;
8
-
9
- /** @internal Create the stats wrapper for one sandbox. */
10
- public constructor(transport: HttpTransport, sandboxId: string) {
11
- this.transport = transport;
12
- this.sandboxId = sandboxId;
13
- }
14
-
15
- /** Fetch sandbox usage stats for a lookback window. */
16
- public stats(query: StatsQuery = {}, options?: RequestOptions): Promise<Stats> {
17
- const params = new URLSearchParams();
18
-
19
- if (query.hoursAgo !== undefined) {
20
- params.set('hoursAgo', String(query.hoursAgo));
21
- }
22
-
23
- return this.transport.requestJson<Stats>({
24
- endpoint: `/sandboxes/${this.sandboxId}/stats`,
25
- method: 'GET',
26
- query: params,
27
- ...options,
28
- }) as Promise<Stats>;
29
- }
30
- }
@@ -1,95 +0,0 @@
1
- import type { RequestOptions } from '../transport/http';
2
- import { HttpTransport } from '../transport/http';
3
- import { toPaginationQuery } from '../transport/pagination';
4
- import { VolumeType } from '../enums';
5
- import { DEFAULT_PAGE, DEFAULT_PAGE_LIMIT, MIN_VOLUME_SIZE_GB } from '../constants';
6
- import type { CreateVolumeInput, Paginated, TeamScopedPagination, Volume } from '../types';
7
-
8
- export class VolumesResource {
9
- private readonly transport: HttpTransport;
10
-
11
- /** @internal Create the volumes resource wrapper. */
12
- public constructor(transport: HttpTransport) {
13
- this.transport = transport;
14
- }
15
-
16
- /** List your volumes with pagination. */
17
- public list(query: TeamScopedPagination = {}, options?: RequestOptions): Promise<Paginated<Volume>> {
18
- const params = toPaginationQuery(query);
19
-
20
- if (query.teamId) {
21
- params.set('teamId', query.teamId);
22
- }
23
-
24
- return this.transport.requestJson<Paginated<Volume>>({
25
- endpoint: '/volumes',
26
- method: 'GET',
27
- query: params,
28
- ...options,
29
- }) as Promise<Paginated<Volume>>;
30
- }
31
-
32
- /** Iterate over all volumes across paginated results. */
33
- public async *iterate(query: TeamScopedPagination = {}, options?: RequestOptions): AsyncGenerator<Volume> {
34
- const limit = query.limit ?? DEFAULT_PAGE_LIMIT;
35
- let page = query.page ?? DEFAULT_PAGE;
36
-
37
- while (true) {
38
- const paginated = await this.list({ ...query, page, limit }, options);
39
-
40
- for (const volume of paginated.data) {
41
- yield volume;
42
- }
43
-
44
- if (page >= paginated.totalPages || paginated.data.length === 0) {
45
- return;
46
- }
47
-
48
- page += 1;
49
- }
50
- }
51
-
52
- /**
53
- * Create a new volume.
54
- * This SDK accepts only `type: "sandbox"` and defaults to it when omitted.
55
- */
56
- public create(input: CreateVolumeInput, options?: RequestOptions): Promise<Volume> {
57
- if (input.type && input.type !== VolumeType.Sandbox) {
58
- throw new Error('Only volume type "sandbox" is supported by this package.');
59
- }
60
-
61
- if (input.sizeGB < MIN_VOLUME_SIZE_GB) {
62
- throw new Error(`Volume size must be at least ${MIN_VOLUME_SIZE_GB}GB.`);
63
- }
64
-
65
- const body: CreateVolumeInput = {
66
- ...input,
67
- type: VolumeType.Sandbox,
68
- };
69
-
70
- return this.transport.requestJson<Volume>({
71
- endpoint: '/volumes',
72
- method: 'POST',
73
- body,
74
- ...options,
75
- }) as Promise<Volume>;
76
- }
77
-
78
- /** Fetch one volume by id. */
79
- public get(volumeId: string, options?: RequestOptions): Promise<Volume> {
80
- return this.transport.requestJson<Volume>({
81
- endpoint: `/volumes/${volumeId}`,
82
- method: 'GET',
83
- ...options,
84
- }) as Promise<Volume>;
85
- }
86
-
87
- /** Delete a volume by id. */
88
- public async delete(volumeId: string, options?: RequestOptions): Promise<void> {
89
- await this.transport.requestJson({
90
- endpoint: `/volumes/${volumeId}`,
91
- method: 'DELETE',
92
- ...options,
93
- });
94
- }
95
- }
@@ -1,4 +0,0 @@
1
- /** Build the header value used by `x-brimble-key`. */
2
- export function buildApiKeyHeader(apiKey: string): string {
3
- return apiKey;
4
- }