@miosa/sdk 0.3.0 → 1.0.0
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/index.d.ts +26 -0
- package/dist/index.js +90 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -4150,6 +4150,32 @@ declare class Sandbox {
|
|
|
4150
4150
|
deploy(params?: SandboxDeployParams): Promise<Record<string, unknown>>;
|
|
4151
4151
|
/** Check readiness of the sandbox (GET /sandboxes/:id/readiness). */
|
|
4152
4152
|
readiness(): Promise<Record<string, unknown>>;
|
|
4153
|
+
/**
|
|
4154
|
+
* Block until the sandbox reports ready, or *timeout* seconds elapse.
|
|
4155
|
+
*
|
|
4156
|
+
* When `stream` is `true` (the default) this opens an SSE connection
|
|
4157
|
+
* to `GET /sandboxes/:id/readiness/stream` and waits for an
|
|
4158
|
+
* `event: ready` frame. The server emits `ready` immediately if the
|
|
4159
|
+
* sandbox is already ready, otherwise as soon as the readiness PubSub
|
|
4160
|
+
* message fires.
|
|
4161
|
+
*
|
|
4162
|
+
* Returns `true` once the sandbox is ready, `false` on `event: timeout`
|
|
4163
|
+
* or when the local timeout elapses before ready.
|
|
4164
|
+
*
|
|
4165
|
+
* If the SSE endpoint returns 404 (server pre-dates the streaming
|
|
4166
|
+
* endpoint) this transparently falls back to polling
|
|
4167
|
+
* {@link readiness} every 10 ms until ready or timeout.
|
|
4168
|
+
*/
|
|
4169
|
+
waitUntilReady(options?: {
|
|
4170
|
+
timeout?: number;
|
|
4171
|
+
stream?: boolean;
|
|
4172
|
+
}): Promise<boolean>;
|
|
4173
|
+
/**
|
|
4174
|
+
* Returns `true` / `false` for terminal SSE events, or `null` if the
|
|
4175
|
+
* stream endpoint is unavailable (404 or transport error) so callers
|
|
4176
|
+
* can fall back to polling.
|
|
4177
|
+
*/
|
|
4178
|
+
private tryReadinessStream;
|
|
4153
4179
|
destroy(): Promise<void>;
|
|
4154
4180
|
delete(): Promise<void>;
|
|
4155
4181
|
private assertRunning;
|
package/dist/index.js
CHANGED
|
@@ -5224,6 +5224,96 @@ var Sandbox = class _Sandbox {
|
|
|
5224
5224
|
)
|
|
5225
5225
|
);
|
|
5226
5226
|
}
|
|
5227
|
+
/**
|
|
5228
|
+
* Block until the sandbox reports ready, or *timeout* seconds elapse.
|
|
5229
|
+
*
|
|
5230
|
+
* When `stream` is `true` (the default) this opens an SSE connection
|
|
5231
|
+
* to `GET /sandboxes/:id/readiness/stream` and waits for an
|
|
5232
|
+
* `event: ready` frame. The server emits `ready` immediately if the
|
|
5233
|
+
* sandbox is already ready, otherwise as soon as the readiness PubSub
|
|
5234
|
+
* message fires.
|
|
5235
|
+
*
|
|
5236
|
+
* Returns `true` once the sandbox is ready, `false` on `event: timeout`
|
|
5237
|
+
* or when the local timeout elapses before ready.
|
|
5238
|
+
*
|
|
5239
|
+
* If the SSE endpoint returns 404 (server pre-dates the streaming
|
|
5240
|
+
* endpoint) this transparently falls back to polling
|
|
5241
|
+
* {@link readiness} every 10 ms until ready or timeout.
|
|
5242
|
+
*/
|
|
5243
|
+
async waitUntilReady(options = {}) {
|
|
5244
|
+
const timeout = options.timeout ?? 30;
|
|
5245
|
+
const stream = options.stream ?? true;
|
|
5246
|
+
if (stream) {
|
|
5247
|
+
const sseResult = await this.tryReadinessStream(timeout);
|
|
5248
|
+
if (sseResult !== null) return sseResult;
|
|
5249
|
+
}
|
|
5250
|
+
const deadlineMs = Date.now() + timeout * 1e3;
|
|
5251
|
+
while (Date.now() < deadlineMs) {
|
|
5252
|
+
try {
|
|
5253
|
+
const data = await this.readiness();
|
|
5254
|
+
if (data.ready === true || data.status === "ready") return true;
|
|
5255
|
+
} catch {
|
|
5256
|
+
}
|
|
5257
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
5258
|
+
}
|
|
5259
|
+
return false;
|
|
5260
|
+
}
|
|
5261
|
+
/**
|
|
5262
|
+
* Returns `true` / `false` for terminal SSE events, or `null` if the
|
|
5263
|
+
* stream endpoint is unavailable (404 or transport error) so callers
|
|
5264
|
+
* can fall back to polling.
|
|
5265
|
+
*/
|
|
5266
|
+
async tryReadinessStream(timeoutSec) {
|
|
5267
|
+
const abort = new AbortController();
|
|
5268
|
+
const timer = setTimeout(() => abort.abort(), (timeoutSec + 5) * 1e3);
|
|
5269
|
+
try {
|
|
5270
|
+
const headers = {
|
|
5271
|
+
Authorization: `Bearer ${this.http.apiKey}`,
|
|
5272
|
+
Accept: "text/event-stream",
|
|
5273
|
+
"User-Agent": "@miosa/sdk/1.0.0"
|
|
5274
|
+
};
|
|
5275
|
+
const response = await fetch(
|
|
5276
|
+
`${this.http.baseUrl}/sandboxes/${this.id}/readiness/stream`,
|
|
5277
|
+
{ method: "GET", headers, signal: abort.signal }
|
|
5278
|
+
);
|
|
5279
|
+
if (response.status === 404) return null;
|
|
5280
|
+
if (!response.ok || !response.body) return null;
|
|
5281
|
+
const reader = response.body.getReader();
|
|
5282
|
+
const decoder = new TextDecoder();
|
|
5283
|
+
let buffer = "";
|
|
5284
|
+
try {
|
|
5285
|
+
while (true) {
|
|
5286
|
+
const { done, value } = await reader.read();
|
|
5287
|
+
if (done) break;
|
|
5288
|
+
buffer += decoder.decode(value, { stream: true });
|
|
5289
|
+
let newlineIdx;
|
|
5290
|
+
while ((newlineIdx = buffer.indexOf("\n")) !== -1) {
|
|
5291
|
+
const line = buffer.slice(0, newlineIdx).replace(/\r$/, "");
|
|
5292
|
+
buffer = buffer.slice(newlineIdx + 1);
|
|
5293
|
+
if (line.startsWith("event:")) {
|
|
5294
|
+
const evt = line.slice(6).trim();
|
|
5295
|
+
if (evt === "ready") return true;
|
|
5296
|
+
if (evt === "timeout") return false;
|
|
5297
|
+
}
|
|
5298
|
+
}
|
|
5299
|
+
}
|
|
5300
|
+
} finally {
|
|
5301
|
+
try {
|
|
5302
|
+
reader.releaseLock();
|
|
5303
|
+
} catch {
|
|
5304
|
+
}
|
|
5305
|
+
}
|
|
5306
|
+
return null;
|
|
5307
|
+
} catch {
|
|
5308
|
+
return null;
|
|
5309
|
+
} finally {
|
|
5310
|
+
clearTimeout(timer);
|
|
5311
|
+
try {
|
|
5312
|
+
abort.abort();
|
|
5313
|
+
} catch {
|
|
5314
|
+
}
|
|
5315
|
+
}
|
|
5316
|
+
}
|
|
5227
5317
|
async destroy() {
|
|
5228
5318
|
if (this.data.state === "destroyed") return;
|
|
5229
5319
|
await this.http.delete(`/sandboxes/${this.id}`);
|