@blaxel/core 0.2.2 → 0.2.3-dev.62

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.
@@ -10,6 +10,11 @@ exports.fs = fs;
10
10
  let os = null;
11
11
  exports.os = os;
12
12
  if (isNode) {
13
- exports.fs = fs = eval("require")("fs");
14
- exports.os = os = eval("require")("os");
13
+ try {
14
+ exports.fs = fs = eval("require")("fs");
15
+ exports.os = os = eval("require")("os");
16
+ }
17
+ catch (e) {
18
+ console.warn("fs and os are not available in this environment");
19
+ }
15
20
  }
@@ -75,7 +75,7 @@ class Settings {
75
75
  if (!this.generation) {
76
76
  return "";
77
77
  }
78
- return env_js_1.env.BL_RUN_INTERNAL_HOSTNAME || "";
78
+ return env_js_1.env.BL_RUN_INTERNAL_HOST || "";
79
79
  }
80
80
  get runInternalProtocol() {
81
81
  return env_js_1.env.BL_RUN_INTERNAL_PROTOCOL || "https";
@@ -2,6 +2,7 @@ declare class BlJob {
2
2
  getArguments(): Promise<{
3
3
  [key: number]: any;
4
4
  }>;
5
+ private parseCommandLineArgs;
5
6
  get indexKey(): string;
6
7
  get index(): number;
7
8
  start(func: (args: any) => Promise<void>): Promise<void>;
package/dist/jobs/jobs.js CHANGED
@@ -1,25 +1,38 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.blJob = void 0;
7
- const yargs_1 = __importDefault(require("yargs"));
8
- const helpers_1 = require("yargs/helpers");
9
4
  const env_js_1 = require("../common/env.js");
10
5
  class BlJob {
11
6
  async getArguments() {
12
7
  if (!env_js_1.env.BL_EXECUTION_DATA_URL) {
13
- const argv = await (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
14
- .parseAsync();
15
- return argv;
8
+ const args = this.parseCommandLineArgs();
9
+ return args;
16
10
  }
17
11
  const response = await fetch(env_js_1.env.BL_EXECUTION_DATA_URL);
18
12
  const data = await response.json();
19
13
  return data.tasks[this.index] ?? {};
20
14
  }
15
+ parseCommandLineArgs() {
16
+ const args = process.argv.slice(2);
17
+ const result = {};
18
+ for (let i = 0; i < args.length; i++) {
19
+ const arg = args[i];
20
+ if (arg.startsWith('--')) {
21
+ const key = arg.slice(2);
22
+ const value = args[i + 1];
23
+ if (value && !value.startsWith('--')) {
24
+ result[key] = value;
25
+ i++;
26
+ }
27
+ else {
28
+ result[key] = 'true';
29
+ }
30
+ }
31
+ }
32
+ return result;
33
+ }
21
34
  get indexKey() {
22
- return env_js_1.env.BL_EXECUTION_INDEX_KEY ?? "TASK_INDEX";
35
+ return env_js_1.env.BL_TASK_KEY ?? "TASK_INDEX";
23
36
  }
24
37
  get index() {
25
38
  return env_js_1.env[this.indexKey] ? Number(env_js_1.env[this.indexKey]) ?? 0 : 0;
@@ -16,5 +16,4 @@ export declare class SandboxAction {
16
16
  get forcedUrl(): string | null;
17
17
  get url(): string;
18
18
  handleResponseError(response: Response, data: unknown, error: unknown): void;
19
- websocket(path: string): WebSocket | null;
20
19
  }
@@ -84,45 +84,5 @@ class SandboxAction {
84
84
  throw new ResponseError(response, data, error);
85
85
  }
86
86
  }
87
- websocket(path) {
88
- let ws = null;
89
- // Build ws:// or wss:// URL from baseUrl
90
- let baseUrl = this.url.replace(/^http/, 'ws');
91
- if (baseUrl.endsWith('/'))
92
- baseUrl = baseUrl.slice(0, -1);
93
- let params = `token=${settings_js_1.settings.token}`;
94
- if (this.sandbox.params) {
95
- params = "";
96
- for (const [key, value] of Object.entries(this.sandbox.params)) {
97
- params += `${key}=${value}&`;
98
- }
99
- params = params.slice(0, -1);
100
- }
101
- const wsUrl = `${baseUrl}/ws/${path}?${params}`;
102
- // Use isomorphic WebSocket: browser or Node.js
103
- let WS = undefined;
104
- if (typeof globalThis.WebSocket !== 'undefined') {
105
- WS = globalThis.WebSocket;
106
- }
107
- else {
108
- try {
109
- // eslint-disable-next-line @typescript-eslint/no-var-requires
110
- WS = require('ws');
111
- }
112
- catch {
113
- WS = undefined;
114
- }
115
- }
116
- if (!WS)
117
- throw new Error('WebSocket is not available in this environment');
118
- try {
119
- ws = typeof WS === 'function' ? new WS(wsUrl) : new WS(wsUrl);
120
- }
121
- catch (err) {
122
- console.error('WebSocket connection error:', err);
123
- throw err;
124
- }
125
- return ws;
126
- }
127
87
  }
128
88
  exports.SandboxAction = SandboxAction;
@@ -14,27 +14,7 @@ export declare class SandboxFileSystem extends SandboxAction {
14
14
  rm(path: string, recursive?: boolean): Promise<SuccessResponse>;
15
15
  ls(path: string): Promise<Directory>;
16
16
  cp(source: string, destination: string): Promise<CopyResponse>;
17
- /**
18
- * Watch for changes in a directory. Calls the callback with the changed file path (and optionally its content).
19
- * Returns a handle with a close() method to stop watching.
20
- * @param path Directory to watch
21
- * @param callback Function called on each change: (filePath, content?)
22
- * @param withContent If true, also fetches and passes the file content (default: false)
23
- */
24
17
  watch(path: string, callback: (filePath: string, content?: string) => void | Promise<void>, options?: {
25
- ws?: boolean;
26
- onError?: (error: Error) => void;
27
- withContent: boolean;
28
- }): {
29
- close: () => void;
30
- };
31
- wsWatch(path: string, callback: (filePath: string, content?: string) => void | Promise<void>, options?: {
32
- onError?: (error: Error) => void;
33
- withContent: boolean;
34
- }): {
35
- close: () => void;
36
- };
37
- sseWatch(path: string, callback: (filePath: string, content?: string) => void | Promise<void>, options?: {
38
18
  onError?: (error: Error) => void;
39
19
  withContent: boolean;
40
20
  }): {
@@ -117,131 +117,7 @@ class SandboxFileSystem extends action_js_1.SandboxAction {
117
117
  }
118
118
  throw new Error("Unsupported file type");
119
119
  }
120
- /**
121
- * Watch for changes in a directory. Calls the callback with the changed file path (and optionally its content).
122
- * Returns a handle with a close() method to stop watching.
123
- * @param path Directory to watch
124
- * @param callback Function called on each change: (filePath, content?)
125
- * @param withContent If true, also fetches and passes the file content (default: false)
126
- */
127
120
  watch(path, callback, options) {
128
- if (options?.ws) {
129
- return this.wsWatch(path, callback, options);
130
- }
131
- return this.sseWatch(path, callback, options);
132
- }
133
- wsWatch(path, callback, options) {
134
- path = this.formatPath(path);
135
- let closed = false;
136
- let ws = this.websocket(`watch/filesystem${path.startsWith('/') ? path : '/' + path}`);
137
- let pingInterval = null;
138
- let pongTimeout = null;
139
- const PING_INTERVAL_MS = 30000;
140
- const PONG_TIMEOUT_MS = 10000;
141
- function sendPing() {
142
- if (ws && ws.readyState === ws.OPEN) {
143
- try {
144
- ws.send(JSON.stringify({ type: 'ping' }));
145
- }
146
- catch { }
147
- // Set pong timeout
148
- if (pongTimeout)
149
- clearTimeout(pongTimeout);
150
- pongTimeout = setTimeout(() => {
151
- // No pong received in time, close connection
152
- if (ws && typeof ws.close === 'function')
153
- ws.close();
154
- }, PONG_TIMEOUT_MS);
155
- }
156
- }
157
- if (ws) {
158
- ws.onmessage = async (event) => {
159
- if (closed)
160
- return;
161
- let data;
162
- try {
163
- data = typeof event.data === 'string' ? event.data : event.data;
164
- if (!data)
165
- return;
166
- // Accept both JSON and plain string (file path)
167
- let payload;
168
- try {
169
- payload = JSON.parse(data);
170
- }
171
- catch {
172
- payload = { name: data, event: undefined };
173
- }
174
- // Handle ping/pong
175
- if (payload.type === 'ping') {
176
- // Respond to ping with pong
177
- if (ws && ws.readyState === ws.OPEN) {
178
- try {
179
- ws.send(JSON.stringify({ type: 'pong' }));
180
- }
181
- catch { }
182
- }
183
- return;
184
- }
185
- if (payload.type === 'pong') {
186
- // Pong received, clear pong timeout
187
- if (pongTimeout)
188
- clearTimeout(pongTimeout);
189
- pongTimeout = null;
190
- return;
191
- }
192
- const filePath = payload.name || payload.path || data;
193
- if (!filePath)
194
- return;
195
- if (options?.withContent) {
196
- try {
197
- const content = await this.read(filePath);
198
- await callback(filePath, content);
199
- }
200
- catch (e) {
201
- await callback(filePath, undefined);
202
- }
203
- }
204
- else {
205
- await callback(filePath);
206
- }
207
- }
208
- catch (err) {
209
- if (options?.onError)
210
- options.onError(err);
211
- }
212
- };
213
- ws.onerror = (err) => {
214
- if (options?.onError)
215
- options.onError(err instanceof Error ? err : new Error(String(err)));
216
- closed = true;
217
- if (ws && typeof ws.close === 'function')
218
- ws.close();
219
- };
220
- ws.onclose = () => {
221
- closed = true;
222
- ws = null;
223
- if (pingInterval)
224
- clearInterval(pingInterval);
225
- if (pongTimeout)
226
- clearTimeout(pongTimeout);
227
- };
228
- // Start ping interval
229
- pingInterval = setInterval(sendPing, PING_INTERVAL_MS);
230
- }
231
- return {
232
- close: () => {
233
- closed = true;
234
- if (ws && typeof ws.close === 'function')
235
- ws.close();
236
- ws = null;
237
- if (pingInterval)
238
- clearInterval(pingInterval);
239
- if (pongTimeout)
240
- clearTimeout(pongTimeout);
241
- },
242
- };
243
- }
244
- sseWatch(path, callback, options) {
245
121
  path = this.formatPath(path);
246
122
  let closed = false;
247
123
  let controller = new AbortController();
@@ -4,21 +4,6 @@ import { DeleteProcessByIdentifierKillResponse, DeleteProcessByIdentifierRespons
4
4
  export declare class SandboxProcess extends SandboxAction {
5
5
  constructor(sandbox: Sandbox);
6
6
  streamLogs(identifier: string, options: {
7
- ws?: boolean;
8
- onLog?: (log: string) => void;
9
- onStdout?: (stdout: string) => void;
10
- onStderr?: (stderr: string) => void;
11
- }): {
12
- close: () => void;
13
- };
14
- wsStreamLogs(identifier: string, options: {
15
- onLog?: (log: string) => void;
16
- onStdout?: (stdout: string) => void;
17
- onStderr?: (stderr: string) => void;
18
- }): {
19
- close: () => void;
20
- };
21
- sseStreamLogs(identifier: string, options: {
22
7
  onLog?: (log: string) => void;
23
8
  onStdout?: (stdout: string) => void;
24
9
  onStderr?: (stderr: string) => void;
@@ -9,119 +9,6 @@ class SandboxProcess extends action_js_1.SandboxAction {
9
9
  super(sandbox);
10
10
  }
11
11
  streamLogs(identifier, options) {
12
- if (options.ws) {
13
- return this.wsStreamLogs(identifier, options);
14
- }
15
- return this.sseStreamLogs(identifier, options);
16
- }
17
- wsStreamLogs(identifier, options) {
18
- let closed = false;
19
- let ws = this.websocket(`process/${identifier}/logs/stream`);
20
- let pingInterval = null;
21
- let pongTimeout = null;
22
- const PING_INTERVAL_MS = 30000;
23
- const PONG_TIMEOUT_MS = 10000;
24
- function sendPing() {
25
- if (ws && ws.readyState === ws.OPEN) {
26
- try {
27
- ws.send(JSON.stringify({ type: 'ping' }));
28
- }
29
- catch { }
30
- // Set pong timeout
31
- if (pongTimeout)
32
- clearTimeout(pongTimeout);
33
- pongTimeout = setTimeout(() => {
34
- // No pong received in time, close connection
35
- if (ws && typeof ws.close === 'function')
36
- ws.close();
37
- }, PONG_TIMEOUT_MS);
38
- }
39
- }
40
- if (ws) {
41
- ws.onmessage = (event) => {
42
- if (closed)
43
- return;
44
- let data;
45
- try {
46
- data = typeof event.data === 'string' ? event.data : event.data;
47
- if (!data)
48
- return;
49
- let payload;
50
- try {
51
- payload = JSON.parse(data);
52
- }
53
- catch {
54
- payload = { log: data };
55
- }
56
- // Handle ping/pong
57
- if (payload.type === 'ping') {
58
- // Respond to ping with pong
59
- if (ws && ws.readyState === ws.OPEN) {
60
- try {
61
- ws.send(JSON.stringify({ type: 'pong' }));
62
- }
63
- catch { }
64
- }
65
- return;
66
- }
67
- if (payload.type === 'pong') {
68
- // Pong received, clear pong timeout
69
- if (pongTimeout)
70
- clearTimeout(pongTimeout);
71
- pongTimeout = null;
72
- return;
73
- }
74
- if (payload.type === 'log') {
75
- const logLine = payload.log || "";
76
- if (typeof logLine === 'string') {
77
- if (logLine.startsWith('stdout:')) {
78
- options.onStdout?.(logLine.slice(7));
79
- options.onLog?.(logLine.slice(7));
80
- }
81
- else if (logLine.startsWith('stderr:')) {
82
- options.onStderr?.(logLine.slice(7));
83
- options.onLog?.(logLine.slice(7));
84
- }
85
- else {
86
- options.onLog?.(logLine);
87
- }
88
- }
89
- }
90
- }
91
- catch (err) {
92
- console.error('WebSocket log stream error:', err);
93
- }
94
- };
95
- ws.onerror = (err) => {
96
- closed = true;
97
- if (ws && typeof ws.close === 'function')
98
- ws.close();
99
- };
100
- ws.onclose = () => {
101
- closed = true;
102
- ws = null;
103
- if (pingInterval)
104
- clearInterval(pingInterval);
105
- if (pongTimeout)
106
- clearTimeout(pongTimeout);
107
- };
108
- // Start ping interval
109
- pingInterval = setInterval(sendPing, PING_INTERVAL_MS);
110
- }
111
- return {
112
- close: () => {
113
- closed = true;
114
- if (ws && typeof ws.close === 'function')
115
- ws.close();
116
- ws = null;
117
- if (pingInterval)
118
- clearInterval(pingInterval);
119
- if (pongTimeout)
120
- clearTimeout(pongTimeout);
121
- },
122
- };
123
- }
124
- sseStreamLogs(identifier, options) {
125
12
  const controller = new AbortController();
126
13
  (async () => {
127
14
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blaxel/core",
3
- "version": "0.2.2",
3
+ "version": "0.2.3-dev.62",
4
4
  "description": "Blaxel Core SDK for TypeScript",
5
5
  "license": "MIT",
6
6
  "author": "Blaxel, INC (https://blaxel.ai)",
@@ -62,13 +62,11 @@
62
62
  "uuid": "^11.1.0",
63
63
  "ws": "^8.18.2",
64
64
  "yaml": "^2.7.1",
65
- "yargs": "^17.7.2",
66
65
  "zod": "^3.24.3"
67
66
  },
68
67
  "devDependencies": {
69
68
  "@eslint/js": "^9.26.0",
70
69
  "@types/ws": "^8.18.1",
71
- "@types/yargs": "^17.0.33",
72
70
  "typescript": "^5.0.0",
73
71
  "typescript-eslint": "^8.31.1"
74
72
  },