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