@blaxel/core 0.2.80-preview.138 → 0.2.80-preview.139

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.
@@ -1,7 +1,36 @@
1
+ import { ensureAutoloaded } from "../common/lazyInit.js";
1
2
  import { settings } from "../common/settings.js";
3
+ // Default baseUrl baked into the generated control-plane client. Requests
4
+ // carrying this prefix came from the module-load client config, before
5
+ // `ensureAutoloaded()` had a chance to resolve the real env from
6
+ // `~/.blaxel/config.yaml`.
7
+ const DEFAULT_CONTROLPLANE_BASE_URL = "https://api.blaxel.ai/v0";
2
8
  export const interceptors = [
3
9
  // Authentication interceptor
4
10
  async (request, options) => {
11
+ // Trigger deferred autoload side-effects (lazy credential resolution,
12
+ // client baseUrl sync, Sentry init, H2 warming) on the first actual SDK
13
+ // use, rather than at module-import time.
14
+ ensureAutoloaded();
15
+ // If lazy env resolution just moved the effective baseUrl off the
16
+ // module-load default (e.g. a user with `env: dev` in config.yaml but
17
+ // no BL_ENV env var), the very first request was built against the
18
+ // stale prod URL. Rebase it to the correct environment. Subsequent
19
+ // requests use the updated `client.setConfig()` applied in
20
+ // `ensureAutoloaded()`, so this branch only fires once.
21
+ //
22
+ // This must happen before the `authenticated === false` early return:
23
+ // the OAuth token request issued by `ClientCredentials.authenticate()`
24
+ // itself is unauthenticated, and if the user calls `authenticate()`
25
+ // before any other SDK call it is the very first request and also
26
+ // needs to be rebased to the correct environment.
27
+ const correctBase = settings.baseUrl;
28
+ if (correctBase !== DEFAULT_CONTROLPLANE_BASE_URL &&
29
+ request.url.startsWith(DEFAULT_CONTROLPLANE_BASE_URL)) {
30
+ const newUrl = correctBase.replace(/\/$/, "") +
31
+ request.url.slice(DEFAULT_CONTROLPLANE_BASE_URL.length);
32
+ request = new Request(newUrl, request);
33
+ }
5
34
  if (options.authenticated === false) {
6
35
  return request;
7
36
  }
@@ -2,8 +2,8 @@ import { client } from "../client/client.gen.js";
2
2
  import { interceptors } from "../client/interceptors.js";
3
3
  import { responseInterceptors } from "../client/responseInterceptor.js";
4
4
  import { client as clientSandbox } from "../sandbox/client/client.gen.js";
5
- import { initSentry } from "./sentry.js";
6
5
  import { settings } from "./settings.js";
6
+ export { ensureAutoloaded } from "./lazyInit.js";
7
7
  client.setConfig({
8
8
  baseUrl: settings.baseUrl,
9
9
  });
@@ -19,34 +19,15 @@ for (const interceptor of responseInterceptors) {
19
19
  client.interceptors.response.use(interceptor);
20
20
  clientSandbox.interceptors.response.use(interceptor);
21
21
  }
22
- // Initialize Sentry for SDK error tracking immediately when module loads
23
- initSentry();
24
- // Background H2 connection warming (Node.js only)
25
- const isNode = typeof process !== "undefined" && process.versions != null && process.versions.node != null;
26
- /* eslint-disable */
27
- const isBrowser = typeof globalThis !== "undefined" && globalThis?.window !== undefined;
28
- if (isNode && !isBrowser) {
29
- try {
30
- // Pre-warm edge H2 for the configured region so the first
31
- // SandboxInstance.create() gets an instant session via the pool.
32
- // The control-plane client (api.blaxel.ai) stays on regular fetch
33
- // which already benefits from undici's built-in connection pooling.
34
- const region = settings.region;
35
- if (region) {
36
- import("./h2pool.js").then(({ h2Pool }) => {
37
- const edgeSuffix = settings.env === "prod" ? "bl.run" : "runv2.blaxel.dev";
38
- h2Pool.warm(`any.${region}.${edgeSuffix}`);
39
- }).catch(() => { });
40
- }
41
- }
42
- catch {
43
- // Silently ignore warming failures
44
- }
45
- }
46
22
  /**
47
23
  * Configure the SDK programmatically at runtime, instead of relying on
48
24
  * environment variables or config files.
49
25
  *
26
+ * You do not need to call {@link authenticate} yourself: the SDK
27
+ * transparently authenticates (and refreshes tokens when needed) on every
28
+ * request. Only call `authenticate()` directly if you want to fail fast on
29
+ * invalid credentials before making any API call.
30
+ *
50
31
  * @example
51
32
  * // With an API key
52
33
  * initialize({ workspace: 'my-workspace', apiKey: 'bl_...' });
@@ -57,7 +38,6 @@ if (isNode && !isBrowser) {
57
38
  * workspace: 'my-workspace',
58
39
  * clientCredentials: { clientId: '...', clientSecret: '...' },
59
40
  * });
60
- * await authenticate();
61
41
  *
62
42
  * @example
63
43
  * // With client credentials (pre-encoded Base64 string)
@@ -65,7 +45,6 @@ if (isNode && !isBrowser) {
65
45
  * workspace: 'my-workspace',
66
46
  * clientCredentials: 'base64-encoded-string',
67
47
  * });
68
- * await authenticate();
69
48
  */
70
49
  export function initialize(config) {
71
50
  settings.setConfig(config);
@@ -0,0 +1,62 @@
1
+ import { client } from "../client/client.gen.js";
2
+ import { client as clientSandbox } from "../sandbox/client/client.gen.js";
3
+ import { initSentry } from "./sentry.js";
4
+ import { settings } from "./settings.js";
5
+ let autoloaded = false;
6
+ /**
7
+ * Perform the observable side-effects that the SDK needs for error
8
+ * reporting, low-latency sandbox sessions, and correct environment
9
+ * routing:
10
+ *
11
+ * - Resolve credentials (which reads `~/.blaxel/config.yaml` once and
12
+ * populates `BL_ENV` if the user has a dev workspace configured).
13
+ * - Re-apply the control-plane and sandbox clients' `baseUrl` so they
14
+ * target the now-env-aware endpoint.
15
+ * - Initialize the lightweight Sentry client (registers
16
+ * `uncaughtExceptionMonitor` and patches `console.error` in Node).
17
+ * - Pre-warm the edge H2 connection pool for `settings.region`.
18
+ *
19
+ * These used to run at module load, but that meant `import "@blaxel/core"`
20
+ * alone had observable side-effects. They are now deferred until the SDK
21
+ * is first actually used — the request interceptor calls this on the
22
+ * first HTTP request.
23
+ */
24
+ export function ensureAutoloaded() {
25
+ if (autoloaded)
26
+ return;
27
+ autoloaded = true;
28
+ // Trigger lazy credential resolution. This may read `~/.blaxel/config.yaml`
29
+ // and set `process.env.BL_ENV` if the user has a dev workspace configured,
30
+ // which in turn affects `settings.baseUrl`.
31
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
32
+ settings.credentials;
33
+ // Keep the clients' baseUrl in sync with the now-resolved env. Without
34
+ // this, the module-load `client.setConfig({ baseUrl })` would be stuck on
35
+ // the prod default for users who rely on `config.yaml` (no env vars).
36
+ client.setConfig({ baseUrl: settings.baseUrl });
37
+ clientSandbox.setConfig({ baseUrl: settings.baseUrl });
38
+ // Initialize Sentry for SDK error tracking.
39
+ initSentry();
40
+ // Background H2 connection warming (Node.js only)
41
+ const isNode = typeof process !== "undefined" && process.versions != null && process.versions.node != null;
42
+ /* eslint-disable */
43
+ const isBrowser = typeof globalThis !== "undefined" && globalThis?.window !== undefined;
44
+ if (isNode && !isBrowser) {
45
+ try {
46
+ // Pre-warm edge H2 for the configured region so the first
47
+ // SandboxInstance.create() gets an instant session via the pool.
48
+ // The control-plane client (api.blaxel.ai) stays on regular fetch
49
+ // which already benefits from undici's built-in connection pooling.
50
+ const region = settings.region;
51
+ if (region) {
52
+ import("./h2pool.js").then(({ h2Pool }) => {
53
+ const edgeSuffix = settings.env === "prod" ? "bl.run" : "runv2.blaxel.dev";
54
+ h2Pool.warm(`any.${region}.${edgeSuffix}`);
55
+ }).catch(() => { });
56
+ }
57
+ }
58
+ catch {
59
+ // Silently ignore warming failures
60
+ }
61
+ }
62
+ }
@@ -5,8 +5,8 @@ import { authentication } from "../authentication/index.js";
5
5
  import { env } from "../common/env.js";
6
6
  import { fs, os, path } from "../common/node.js";
7
7
  // Build info - these placeholders are replaced at build time by build:replace-imports
8
- const BUILD_VERSION = "0.2.80-preview.138";
9
- const BUILD_COMMIT = "a58f8cc475d6ca3147ed31106d71a68e504c55a0";
8
+ const BUILD_VERSION = "0.2.80-preview.139";
9
+ const BUILD_COMMIT = "90ee846099b1f428daba9643df172bf43a5fd53e";
10
10
  const BUILD_SENTRY_DSN = "https://fd5e60e1c9820e1eef5ccebb84a07127@o4508714045276160.ingest.us.sentry.io/4510465864564736";
11
11
  // Cache for config.yaml tracking value
12
12
  let configTrackingValue = null;
@@ -66,20 +66,32 @@ function getOsArch() {
66
66
  return "browser/unknown";
67
67
  }
68
68
  class Settings {
69
- credentials;
69
+ _credentials;
70
70
  config;
71
71
  constructor() {
72
- this.credentials = authentication();
72
+ // `credentials` are resolved lazily on first access so that simply
73
+ // importing `@blaxel/core` does not read `~/.blaxel/config.yaml`
74
+ // or mutate `process.env.BL_ENV`. See the `credentials` getter.
75
+ this._credentials = null;
73
76
  this.config = {
74
77
  proxy: "",
75
78
  apikey: "",
76
79
  workspace: "",
77
80
  };
78
81
  }
82
+ get credentials() {
83
+ if (this._credentials === null) {
84
+ this._credentials = authentication();
85
+ }
86
+ return this._credentials;
87
+ }
88
+ set credentials(value) {
89
+ this._credentials = value;
90
+ }
79
91
  setConfig(config) {
80
92
  this.config = config;
81
93
  if (config.apiKey) {
82
- this.credentials = new ApiKey({
94
+ this._credentials = new ApiKey({
83
95
  apiKey: config.apiKey,
84
96
  workspace: config.workspace,
85
97
  });
@@ -88,7 +100,7 @@ class Settings {
88
100
  const encoded = typeof config.clientCredentials === 'string'
89
101
  ? config.clientCredentials
90
102
  : btoa(`${config.clientCredentials.clientId}:${config.clientCredentials.clientSecret}`);
91
- this.credentials = new ClientCredentials({
103
+ this._credentials = new ClientCredentials({
92
104
  clientCredentials: encoded,
93
105
  workspace: config.workspace,
94
106
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blaxel/core",
3
- "version": "0.2.80-preview.138",
3
+ "version": "0.2.80-preview.139",
4
4
  "description": "Blaxel Core SDK for TypeScript",
5
5
  "license": "MIT",
6
6
  "author": "Blaxel, INC (https://blaxel.ai)",