@blaxel/core 0.3.26-preview.310 → 0.3.26-preview.311

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.
@@ -30,8 +30,8 @@ function missingCredentialsMessage() {
30
30
  return "No Blaxel credentials found. Set the BL_API_KEY and BL_WORKSPACE environment variables, or run `bl login`.";
31
31
  }
32
32
  // Build info - these placeholders are replaced at build time by build:replace-imports
33
- const BUILD_VERSION = "0.3.26-preview.310";
34
- const BUILD_COMMIT = "13bd8f75d15c2c81add546bcabeee2f20a224417";
33
+ const BUILD_VERSION = "0.3.26-preview.311";
34
+ const BUILD_COMMIT = "a62496fa4537136926770cdd0d6e48ceb8506491";
35
35
  const BUILD_SENTRY_DSN = "https://fd5e60e1c9820e1eef5ccebb84a07127@o4508714045276160.ingest.us.sentry.io/4510465864564736";
36
36
  const BLAXEL_API_VERSION = "2026-09-22";
37
37
  // Bun < 1.3.11 never sends connection-level WINDOW_UPDATE: the pooled h2
@@ -2,11 +2,48 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SandboxPreviews = exports.SandboxPreview = exports.SandboxPreviewTokens = exports.SandboxPreviewToken = void 0;
4
4
  const index_js_1 = require("../client/index.js");
5
+ function isShorthand(preview) {
6
+ return !("metadata" in preview || "spec" in preview);
7
+ }
8
+ function normalizePreview(preview) {
9
+ if (!isShorthand(preview))
10
+ return preview;
11
+ if (!Number.isInteger(preview.port) || preview.port < 1 || preview.port > 65535) {
12
+ throw new RangeError("Preview port must be an integer between 1 and 65535");
13
+ }
14
+ if (preview.name !== undefined && (typeof preview.name !== "string" || preview.name.length === 0)) {
15
+ throw new RangeError("Preview name must be a nonempty string");
16
+ }
17
+ if (preview.public !== undefined && typeof preview.public !== "boolean") {
18
+ throw new RangeError("Preview public must be a boolean");
19
+ }
20
+ return {
21
+ metadata: { name: preview.name ?? `preview-${preview.port}` },
22
+ spec: { port: preview.port, public: preview.public ?? false },
23
+ };
24
+ }
25
+ // Explicit HTTP status takes precedence over a legacy numeric service code.
26
+ function httpStatus(error) {
27
+ if (typeof error !== "object" || error === null)
28
+ return undefined;
29
+ const fields = error;
30
+ const response = fields.response;
31
+ const responseStatus = typeof response === "object" && response !== null && "status" in response
32
+ ? response.status : undefined;
33
+ for (const status of [fields.status, fields.statusCode, responseStatus, fields.code]) {
34
+ if (typeof status === "number")
35
+ return status;
36
+ }
37
+ return undefined;
38
+ }
5
39
  class SandboxPreviewToken {
6
40
  previewToken;
7
41
  constructor(previewToken) {
8
42
  this.previewToken = previewToken;
9
43
  }
44
+ get name() {
45
+ return this.previewToken.metadata?.name ?? "";
46
+ }
10
47
  get value() {
11
48
  return this.previewToken.spec.token ?? "";
12
49
  }
@@ -47,6 +84,49 @@ class SandboxPreviewTokens {
47
84
  });
48
85
  return new SandboxPreviewToken(data);
49
86
  }
87
+ /**
88
+ * Reuse the latest token within the requested expiry ceiling, or mint one.
89
+ * minValidity is in milliseconds. Concurrent callers may create separate tokens.
90
+ */
91
+ async createIfExpired(expiresAt, minValidity = 3_600_000) {
92
+ const now = Date.now();
93
+ if (!Number.isFinite(minValidity) || minValidity < 0) {
94
+ throw new RangeError("minValidity must be finite and non-negative");
95
+ }
96
+ const requestedExpiry = expiresAt ?? new Date(now + 86_400_000);
97
+ const ceiling = requestedExpiry instanceof Date ? requestedExpiry.getTime() : NaN;
98
+ if (!Number.isFinite(ceiling) || ceiling <= now || ceiling < now + minValidity) {
99
+ throw new RangeError("expiresAt must be a valid future date at least minValidity from now");
100
+ }
101
+ if (this.preview.spec?.public === true) {
102
+ throw new Error("Cannot create or reuse a token for a public preview");
103
+ }
104
+ const { data } = await (0, index_js_1.listSandboxPreviewTokens)({
105
+ path: {
106
+ sandboxName: this.resourceName,
107
+ previewName: this.previewName,
108
+ },
109
+ throwOnError: true,
110
+ });
111
+ if (!Array.isArray(data))
112
+ throw new Error("Failed to list preview tokens");
113
+ let selected;
114
+ let latestExpiry = -Infinity;
115
+ for (const token of data) {
116
+ const spec = token?.spec;
117
+ if (!spec || typeof spec.token !== "string" || spec.token.length === 0 || spec.expired === true ||
118
+ typeof spec.expiresAt !== "string")
119
+ continue;
120
+ const expiry = Date.parse(spec.expiresAt);
121
+ if (!Number.isFinite(expiry) || expiry <= now || expiry < now + minValidity || expiry > ceiling)
122
+ continue;
123
+ if (expiry > latestExpiry) {
124
+ selected = token;
125
+ latestExpiry = expiry;
126
+ }
127
+ }
128
+ return selected ? new SandboxPreviewToken(selected) : this.create(requestedExpiry);
129
+ }
50
130
  async list() {
51
131
  const { data } = await (0, index_js_1.listSandboxPreviewTokens)({
52
132
  path: {
@@ -80,6 +160,9 @@ class SandboxPreview {
80
160
  get name() {
81
161
  return this.preview.metadata.name;
82
162
  }
163
+ get url() {
164
+ return this.preview.spec?.url ?? "";
165
+ }
83
166
  get metadata() {
84
167
  return this.preview.metadata;
85
168
  }
@@ -106,6 +189,7 @@ class SandboxPreviews {
106
189
  return data.map((preview) => new SandboxPreview(preview));
107
190
  }
108
191
  async create(preview, force) {
192
+ const normalized = normalizePreview(preview);
109
193
  const query = {};
110
194
  if (force) {
111
195
  query['force'] = 'true';
@@ -115,12 +199,31 @@ class SandboxPreviews {
115
199
  sandboxName: this.sandboxName,
116
200
  },
117
201
  query,
118
- body: preview,
202
+ body: normalized,
119
203
  throwOnError: true,
120
204
  });
121
205
  return new SandboxPreview(data);
122
206
  }
207
+ /** Existing previews are returned as-is; shorthand's private default applies only on creation. */
123
208
  async createIfNotExists(preview, force) {
209
+ if (isShorthand(preview)) {
210
+ const normalized = normalizePreview(preview);
211
+ try {
212
+ return await this.get(normalized.metadata.name);
213
+ }
214
+ catch (error) {
215
+ if (httpStatus(error) !== 404)
216
+ throw error;
217
+ }
218
+ try {
219
+ return await this.create(normalized, force);
220
+ }
221
+ catch (error) {
222
+ if (httpStatus(error) !== 409)
223
+ throw error;
224
+ return this.get(normalized.metadata.name);
225
+ }
226
+ }
124
227
  try {
125
228
  const previewInstance = await this.get(preview.metadata.name);
126
229
  return previewInstance;
@@ -1,7 +1,9 @@
1
- import { Preview, PreviewToken, Sandbox } from "../client/index.js";
1
+ import type { Preview, PreviewToken, Sandbox } from "../client/index.js";
2
+ import type { SandboxPreviewCreateConfiguration } from "./types.js";
2
3
  export declare class SandboxPreviewToken {
3
4
  private previewToken;
4
5
  constructor(previewToken: PreviewToken);
6
+ get name(): string;
5
7
  get value(): string;
6
8
  get expiresAt(): string | Date;
7
9
  get expired(): boolean;
@@ -12,6 +14,11 @@ export declare class SandboxPreviewTokens {
12
14
  get previewName(): string;
13
15
  get resourceName(): string;
14
16
  create(expiresAt: Date): Promise<SandboxPreviewToken>;
17
+ /**
18
+ * Reuse the latest token within the requested expiry ceiling, or mint one.
19
+ * minValidity is in milliseconds. Concurrent callers may create separate tokens.
20
+ */
21
+ createIfExpired(expiresAt?: Date, minValidity?: number): Promise<SandboxPreviewToken>;
15
22
  list(): Promise<SandboxPreviewToken[]>;
16
23
  delete(tokenName: string): Promise<{
17
24
  message?: string;
@@ -22,6 +29,7 @@ export declare class SandboxPreview {
22
29
  tokens: SandboxPreviewTokens;
23
30
  constructor(preview: Preview);
24
31
  get name(): string;
32
+ get url(): string;
25
33
  get metadata(): import("../client/types.gen.js").PreviewMetadata;
26
34
  get spec(): import("../client/types.gen.js").PreviewSpec;
27
35
  }
@@ -30,8 +38,9 @@ export declare class SandboxPreviews {
30
38
  constructor(sandbox: Sandbox);
31
39
  get sandboxName(): string;
32
40
  list(): Promise<SandboxPreview[]>;
33
- create(preview: Preview, force?: boolean): Promise<SandboxPreview>;
34
- createIfNotExists(preview: Preview, force?: boolean): Promise<SandboxPreview>;
41
+ create(preview: Preview | SandboxPreviewCreateConfiguration, force?: boolean): Promise<SandboxPreview>;
42
+ /** Existing previews are returned as-is; shorthand's private default applies only on creation. */
43
+ createIfNotExists(preview: Preview | SandboxPreviewCreateConfiguration, force?: boolean): Promise<SandboxPreview>;
35
44
  get(previewName: string): Promise<SandboxPreview>;
36
45
  delete(previewName: string): Promise<Preview>;
37
46
  private waitForDeletion;
@@ -38,6 +38,13 @@ export type SandboxUpdateMetadata = {
38
38
  export type SandboxUpdateNetwork = {
39
39
  network?: SandboxNetwork;
40
40
  };
41
+ export type SandboxPreviewCreateConfiguration = {
42
+ port: number;
43
+ /** Defaults to preview-<port> within this sandbox. */
44
+ name?: string;
45
+ /** Defaults to false on creation; existing previews are not changed. */
46
+ public?: boolean;
47
+ };
41
48
  export type SandboxCreateConfiguration = {
42
49
  name?: string;
43
50
  image?: string;