@alibaba-group/opensandbox 0.1.0-dev1

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 (92) hide show
  1. package/README.md +212 -0
  2. package/dist/adapters/commandsAdapter.d.ts +22 -0
  3. package/dist/adapters/commandsAdapter.d.ts.map +1 -0
  4. package/dist/adapters/commandsAdapter.js +76 -0
  5. package/dist/adapters/filesystemAdapter.d.ts +52 -0
  6. package/dist/adapters/filesystemAdapter.d.ts.map +1 -0
  7. package/dist/adapters/filesystemAdapter.js +398 -0
  8. package/dist/adapters/healthAdapter.d.ts +8 -0
  9. package/dist/adapters/healthAdapter.d.ts.map +1 -0
  10. package/dist/adapters/healthAdapter.js +25 -0
  11. package/dist/adapters/metricsAdapter.d.ts +9 -0
  12. package/dist/adapters/metricsAdapter.d.ts.map +1 -0
  13. package/dist/adapters/metricsAdapter.js +43 -0
  14. package/dist/adapters/openapiError.d.ts +5 -0
  15. package/dist/adapters/openapiError.d.ts.map +1 -0
  16. package/dist/adapters/openapiError.js +33 -0
  17. package/dist/adapters/sandboxesAdapter.d.ts +18 -0
  18. package/dist/adapters/sandboxesAdapter.d.ts.map +1 -0
  19. package/dist/adapters/sandboxesAdapter.js +146 -0
  20. package/dist/adapters/sse.d.ts +9 -0
  21. package/dist/adapters/sse.d.ts.map +1 -0
  22. package/dist/adapters/sse.js +84 -0
  23. package/dist/api/execd.d.ts +1555 -0
  24. package/dist/api/execd.d.ts.map +1 -0
  25. package/dist/api/execd.js +14 -0
  26. package/dist/api/lifecycle.d.ts +787 -0
  27. package/dist/api/lifecycle.d.ts.map +1 -0
  28. package/dist/api/lifecycle.js +14 -0
  29. package/dist/config/connection.d.ts +58 -0
  30. package/dist/config/connection.d.ts.map +1 -0
  31. package/dist/config/connection.js +171 -0
  32. package/dist/core/constants.d.ts +9 -0
  33. package/dist/core/constants.d.ts.map +1 -0
  34. package/dist/core/constants.js +24 -0
  35. package/dist/core/exceptions.d.ts +74 -0
  36. package/dist/core/exceptions.d.ts.map +1 -0
  37. package/dist/core/exceptions.js +103 -0
  38. package/dist/factory/adapterFactory.d.ts +33 -0
  39. package/dist/factory/adapterFactory.d.ts.map +1 -0
  40. package/dist/factory/adapterFactory.js +14 -0
  41. package/dist/factory/defaultAdapterFactory.d.ts +7 -0
  42. package/dist/factory/defaultAdapterFactory.d.ts.map +1 -0
  43. package/dist/factory/defaultAdapterFactory.js +61 -0
  44. package/dist/index.d.ts +22 -0
  45. package/dist/index.d.ts.map +1 -0
  46. package/dist/index.js +20 -0
  47. package/dist/internal.d.ts +22 -0
  48. package/dist/internal.d.ts.map +1 -0
  49. package/dist/internal.js +30 -0
  50. package/dist/manager.d.ts +40 -0
  51. package/dist/manager.d.ts.map +1 -0
  52. package/dist/manager.js +71 -0
  53. package/dist/models/execd.d.ts +54 -0
  54. package/dist/models/execd.d.ts.map +1 -0
  55. package/dist/models/execd.js +14 -0
  56. package/dist/models/execution.d.ts +52 -0
  57. package/dist/models/execution.d.ts.map +1 -0
  58. package/dist/models/execution.js +14 -0
  59. package/dist/models/executionEventDispatcher.d.ts +15 -0
  60. package/dist/models/executionEventDispatcher.d.ts.map +1 -0
  61. package/dist/models/executionEventDispatcher.js +95 -0
  62. package/dist/models/filesystem.d.ts +77 -0
  63. package/dist/models/filesystem.d.ts.map +1 -0
  64. package/dist/models/filesystem.js +14 -0
  65. package/dist/models/sandboxes.d.ts +105 -0
  66. package/dist/models/sandboxes.d.ts.map +1 -0
  67. package/dist/models/sandboxes.js +15 -0
  68. package/dist/openapi/execdClient.d.ts +25 -0
  69. package/dist/openapi/execdClient.d.ts.map +1 -0
  70. package/dist/openapi/execdClient.js +21 -0
  71. package/dist/openapi/lifecycleClient.d.ts +28 -0
  72. package/dist/openapi/lifecycleClient.d.ts.map +1 -0
  73. package/dist/openapi/lifecycleClient.js +35 -0
  74. package/dist/sandbox.d.ts +132 -0
  75. package/dist/sandbox.d.ts.map +1 -0
  76. package/dist/sandbox.js +250 -0
  77. package/dist/services/execdCommands.d.ts +19 -0
  78. package/dist/services/execdCommands.d.ts.map +1 -0
  79. package/dist/services/execdCommands.js +14 -0
  80. package/dist/services/execdHealth.d.ts +4 -0
  81. package/dist/services/execdHealth.d.ts.map +1 -0
  82. package/dist/services/execdHealth.js +14 -0
  83. package/dist/services/execdMetrics.d.ts +5 -0
  84. package/dist/services/execdMetrics.d.ts.map +1 -0
  85. package/dist/services/execdMetrics.js +14 -0
  86. package/dist/services/filesystem.d.ts +30 -0
  87. package/dist/services/filesystem.d.ts.map +1 -0
  88. package/dist/services/filesystem.js +14 -0
  89. package/dist/services/sandboxes.d.ts +12 -0
  90. package/dist/services/sandboxes.d.ts.map +1 -0
  91. package/dist/services/sandboxes.js +14 -0
  92. package/package.json +52 -0
@@ -0,0 +1,146 @@
1
+ // Copyright 2026 Alibaba Group Holding Ltd.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ import { throwOnOpenApiFetchError } from "./openapiError.js";
15
+ function encodeMetadataFilter(metadata) {
16
+ // The Lifecycle API expects a single `metadata` query parameter whose value is `k=v&k2=v2`.
17
+ // The query serializer will URL-encode the value (e.g. `=` -> %3D and `&` -> %26).
18
+ const parts = [];
19
+ for (const [k, v] of Object.entries(metadata)) {
20
+ parts.push(`${k}=${v}`);
21
+ }
22
+ return parts.join("&");
23
+ }
24
+ export class SandboxesAdapter {
25
+ client;
26
+ constructor(client) {
27
+ this.client = client;
28
+ }
29
+ parseIsoDate(field, v) {
30
+ if (typeof v !== "string" || !v) {
31
+ throw new Error(`Invalid ${field}: expected ISO string, got ${typeof v}`);
32
+ }
33
+ const d = new Date(v);
34
+ if (Number.isNaN(d.getTime())) {
35
+ throw new Error(`Invalid ${field}: ${v}`);
36
+ }
37
+ return d;
38
+ }
39
+ mapSandboxInfo(raw) {
40
+ return {
41
+ ...(raw ?? {}),
42
+ createdAt: this.parseIsoDate("createdAt", raw?.createdAt),
43
+ expiresAt: this.parseIsoDate("expiresAt", raw?.expiresAt),
44
+ };
45
+ }
46
+ async createSandbox(req) {
47
+ // Make the OpenAPI contract explicit so backend schema changes surface quickly.
48
+ const body = req;
49
+ const { data, error, response } = await this.client.POST("/sandboxes", {
50
+ body,
51
+ });
52
+ throwOnOpenApiFetchError({ error, response }, "Create sandbox failed");
53
+ const raw = data;
54
+ if (!raw || typeof raw !== "object") {
55
+ throw new Error("Create sandbox failed: unexpected response shape");
56
+ }
57
+ return {
58
+ ...(raw ?? {}),
59
+ createdAt: this.parseIsoDate("createdAt", raw?.createdAt),
60
+ expiresAt: this.parseIsoDate("expiresAt", raw?.expiresAt),
61
+ };
62
+ }
63
+ async getSandbox(sandboxId) {
64
+ const { data, error, response } = await this.client.GET("/sandboxes/{sandboxId}", {
65
+ params: { path: { sandboxId } },
66
+ });
67
+ throwOnOpenApiFetchError({ error, response }, "Get sandbox failed");
68
+ const ok = data;
69
+ if (!ok || typeof ok !== "object") {
70
+ throw new Error("Get sandbox failed: unexpected response shape");
71
+ }
72
+ return this.mapSandboxInfo(ok);
73
+ }
74
+ async listSandboxes(params = {}) {
75
+ const query = {};
76
+ if (params.states?.length)
77
+ query.state = params.states;
78
+ if (params.metadata && Object.keys(params.metadata).length) {
79
+ query.metadata = encodeMetadataFilter(params.metadata);
80
+ }
81
+ if (params.page != null)
82
+ query.page = params.page;
83
+ if (params.pageSize != null)
84
+ query.pageSize = params.pageSize;
85
+ const { data, error, response } = await this.client.GET("/sandboxes", {
86
+ params: { query },
87
+ });
88
+ throwOnOpenApiFetchError({ error, response }, "List sandboxes failed");
89
+ const raw = data;
90
+ if (!raw || typeof raw !== "object") {
91
+ throw new Error("List sandboxes failed: unexpected response shape");
92
+ }
93
+ const itemsRaw = raw.items;
94
+ if (!Array.isArray(itemsRaw))
95
+ throw new Error("List sandboxes failed: unexpected items shape");
96
+ return {
97
+ ...(raw ?? {}),
98
+ items: itemsRaw.map((x) => this.mapSandboxInfo(x)),
99
+ };
100
+ }
101
+ async deleteSandbox(sandboxId) {
102
+ const { error, response } = await this.client.DELETE("/sandboxes/{sandboxId}", {
103
+ params: { path: { sandboxId } },
104
+ });
105
+ throwOnOpenApiFetchError({ error, response }, "Delete sandbox failed");
106
+ }
107
+ async pauseSandbox(sandboxId) {
108
+ const { error, response } = await this.client.POST("/sandboxes/{sandboxId}/pause", {
109
+ params: { path: { sandboxId } },
110
+ });
111
+ throwOnOpenApiFetchError({ error, response }, "Pause sandbox failed");
112
+ }
113
+ async resumeSandbox(sandboxId) {
114
+ const { error, response } = await this.client.POST("/sandboxes/{sandboxId}/resume", {
115
+ params: { path: { sandboxId } },
116
+ });
117
+ throwOnOpenApiFetchError({ error, response }, "Resume sandbox failed");
118
+ }
119
+ async renewSandboxExpiration(sandboxId, req) {
120
+ const body = req;
121
+ const { data, error, response } = await this.client.POST("/sandboxes/{sandboxId}/renew-expiration", {
122
+ params: { path: { sandboxId } },
123
+ body,
124
+ });
125
+ throwOnOpenApiFetchError({ error, response }, "Renew sandbox expiration failed");
126
+ const raw = data;
127
+ if (!raw || typeof raw !== "object") {
128
+ throw new Error("Renew sandbox expiration failed: unexpected response shape");
129
+ }
130
+ return {
131
+ ...(raw ?? {}),
132
+ expiresAt: raw?.expiresAt ? this.parseIsoDate("expiresAt", raw.expiresAt) : undefined,
133
+ };
134
+ }
135
+ async getSandboxEndpoint(sandboxId, port) {
136
+ const { data, error, response } = await this.client.GET("/sandboxes/{sandboxId}/endpoints/{port}", {
137
+ params: { path: { sandboxId, port } },
138
+ });
139
+ throwOnOpenApiFetchError({ error, response }, "Get sandbox endpoint failed");
140
+ const ok = data;
141
+ if (!ok || typeof ok !== "object") {
142
+ throw new Error("Get sandbox endpoint failed: unexpected response shape");
143
+ }
144
+ return ok;
145
+ }
146
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Parses an SSE-like stream that may be either:
3
+ * - standard SSE frames (`data: {...}\n\n`)
4
+ * - newline-delimited JSON (one JSON object per line)
5
+ */
6
+ export declare function parseJsonEventStream<T>(res: Response, opts?: {
7
+ fallbackErrorMessage?: string;
8
+ }): AsyncIterable<T>;
9
+ //# sourceMappingURL=sse.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sse.d.ts","sourceRoot":"","sources":["../../src/adapters/sse.ts"],"names":[],"mappings":"AAwBA;;;;GAIG;AACH,wBAAuB,oBAAoB,CAAC,CAAC,EAC3C,GAAG,EAAE,QAAQ,EACb,IAAI,CAAC,EAAE;IAAE,oBAAoB,CAAC,EAAE,MAAM,CAAA;CAAE,GACvC,aAAa,CAAC,CAAC,CAAC,CA2DlB"}
@@ -0,0 +1,84 @@
1
+ // Copyright 2026 Alibaba Group Holding Ltd.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ import { SandboxApiException, SandboxError } from "../core/exceptions.js";
15
+ function tryParseJson(line) {
16
+ try {
17
+ return JSON.parse(line);
18
+ }
19
+ catch {
20
+ return undefined;
21
+ }
22
+ }
23
+ /**
24
+ * Parses an SSE-like stream that may be either:
25
+ * - standard SSE frames (`data: {...}\n\n`)
26
+ * - newline-delimited JSON (one JSON object per line)
27
+ */
28
+ export async function* parseJsonEventStream(res, opts) {
29
+ if (!res.ok) {
30
+ const text = await res.text().catch(() => "");
31
+ const parsed = tryParseJson(text);
32
+ const err = parsed && typeof parsed === "object" ? parsed : undefined;
33
+ const requestId = res.headers.get("x-request-id") ?? undefined;
34
+ const message = err?.message ?? opts?.fallbackErrorMessage ?? `Stream request failed (status=${res.status})`;
35
+ const code = err?.code ? String(err.code) : SandboxError.UNEXPECTED_RESPONSE;
36
+ throw new SandboxApiException({
37
+ message,
38
+ statusCode: res.status,
39
+ requestId,
40
+ error: new SandboxError(code, err?.message ? String(err.message) : message),
41
+ rawBody: parsed ?? text,
42
+ });
43
+ }
44
+ if (!res.body) {
45
+ return;
46
+ }
47
+ const reader = res.body.getReader();
48
+ const decoder = new TextDecoder("utf-8");
49
+ let buf = "";
50
+ while (true) {
51
+ const { value, done } = await reader.read();
52
+ if (done)
53
+ break;
54
+ buf += decoder.decode(value, { stream: true });
55
+ let idx;
56
+ while ((idx = buf.indexOf("\n")) >= 0) {
57
+ const rawLine = buf.slice(0, idx);
58
+ buf = buf.slice(idx + 1);
59
+ const line = rawLine.trim();
60
+ if (!line)
61
+ continue;
62
+ // Support standard SSE "data:" prefix
63
+ if (line.startsWith(":"))
64
+ continue;
65
+ if (line.startsWith("event:") || line.startsWith("id:") || line.startsWith("retry:"))
66
+ continue;
67
+ const jsonLine = line.startsWith("data:") ? line.slice("data:".length).trim() : line;
68
+ if (!jsonLine)
69
+ continue;
70
+ const parsed = tryParseJson(jsonLine);
71
+ if (!parsed)
72
+ continue;
73
+ yield parsed;
74
+ }
75
+ }
76
+ // flush last line if exists
77
+ const last = buf.trim();
78
+ if (last) {
79
+ const jsonLine = last.startsWith("data:") ? last.slice("data:".length).trim() : last;
80
+ const parsed = tryParseJson(jsonLine);
81
+ if (parsed)
82
+ yield parsed;
83
+ }
84
+ }