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