appmesh 3.1.15 → 3.1.19
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/README.md +3 -5
- package/dist/appmesh.esm.js +1 -1
- package/package.json +1 -1
- package/src/appmesh.js +392 -16
- package/src/appmesh_tcp.js +146 -35
- package/src/request_signature.js +33 -18
- package/src/worker.js +47 -8
package/package.json
CHANGED
package/src/appmesh.js
CHANGED
|
@@ -20,6 +20,15 @@ const CONSTANTS = Object.freeze({
|
|
|
20
20
|
HTTP_HEADER_KEY_AUTH: "Authorization",
|
|
21
21
|
HTTP_HEADER_KEY_X_TARGET_HOST: "X-Target-Host",
|
|
22
22
|
HTTP_HEADER_KEY_X_FILE_PATH: "X-File-Path",
|
|
23
|
+
HTTP_HEADER_KEY_X_FILE_OVERWRITE: "X-File-Overwrite",
|
|
24
|
+
HTTP_HEADER_KEY_X_FILE_MODE: "X-File-Mode",
|
|
25
|
+
HTTP_HEADER_KEY_X_FILE_USER: "X-File-User",
|
|
26
|
+
HTTP_HEADER_KEY_X_FILE_GROUP: "X-File-Group",
|
|
27
|
+
HTTP_HEADER_KEY_X_OUTPUT_POSITION: "X-Output-Position",
|
|
28
|
+
HTTP_HEADER_KEY_X_EXIT_CODE: "X-Exit-Code",
|
|
29
|
+
HTTP_HEADER_KEY_X_INPUT_CLOSE: "X-Input-Close",
|
|
30
|
+
HTTP_HEADER_KEY_X_SUBSCRIPTION_ID: "X-Subscription-Id",
|
|
31
|
+
HTTP_HEADER_KEY_X_APPMESH_PROCESS_KEY: "X-AppMesh-Process-Key",
|
|
23
32
|
});
|
|
24
33
|
|
|
25
34
|
// Environment detection
|
|
@@ -109,6 +118,27 @@ class TransportDisconnectedError extends AppMeshError {
|
|
|
109
118
|
}
|
|
110
119
|
}
|
|
111
120
|
|
|
121
|
+
/** The daemon rejected a request, or a stream ended with reason 'error' (Python AppMeshRequestError). */
|
|
122
|
+
class AppMeshRequestError extends AppMeshError {
|
|
123
|
+
/**
|
|
124
|
+
* @param {string} message - Error message
|
|
125
|
+
* @param {number|null} [statusCode=null] - HTTP status code
|
|
126
|
+
*/
|
|
127
|
+
constructor(message, statusCode = null) {
|
|
128
|
+
super(message, statusCode, null, 'REQUEST');
|
|
129
|
+
this.name = 'AppMeshRequestError';
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** A call or stream exceeded its overall timeout (Python AppMeshTimeoutError). */
|
|
134
|
+
class AppMeshTimeoutError extends AppMeshError {
|
|
135
|
+
/** @param {string} message - Error message */
|
|
136
|
+
constructor(message) {
|
|
137
|
+
super(message, null, null, 'TIMEOUT');
|
|
138
|
+
this.name = 'AppMeshTimeoutError';
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
112
142
|
// Default output handler
|
|
113
143
|
const defaultOutputHandler = (output, position) => {
|
|
114
144
|
if (ENV.isNode) {
|
|
@@ -376,6 +406,113 @@ function _resolveSslOptions(sslConfig) {
|
|
|
376
406
|
return opts;
|
|
377
407
|
}
|
|
378
408
|
|
|
409
|
+
/** Terminal marker of an SSE stream: the async generator's return value (Python SseEnd counterpart). */
|
|
410
|
+
class SseEnd {
|
|
411
|
+
/**
|
|
412
|
+
* @param {string} reason - exit | removed | timeout | eof | final | disconnected | error | closed
|
|
413
|
+
* @param {Object} [fields] - Fields carried by the terminal `end` frame
|
|
414
|
+
* @param {number|null} [fields.exitCode] - Process exit code when reason is 'exit'
|
|
415
|
+
* @param {number|null} [fields.position] - Output streams: final stdout byte cursor
|
|
416
|
+
* @param {number|null} [fields.status] - Task streams: HTTP status carried by an error end frame
|
|
417
|
+
* @param {string|null} [fields.message] - Task streams: error message
|
|
418
|
+
* @param {string|null} [fields.result] - Task streams: the final result payload
|
|
419
|
+
*/
|
|
420
|
+
constructor(reason, fields = {}) {
|
|
421
|
+
/** @type {string} One of the constructor's reason values; 'closed' = peer cut the stream */
|
|
422
|
+
this.reason = reason;
|
|
423
|
+
/** @type {number|null} Process exit code when reason is 'exit' */
|
|
424
|
+
this.exitCode = fields.exitCode ?? null;
|
|
425
|
+
/** @type {number|null} Output streams: final stdout byte cursor */
|
|
426
|
+
this.position = fields.position ?? null;
|
|
427
|
+
/** @type {number|null} Task streams: HTTP status carried by an error end frame */
|
|
428
|
+
this.status = fields.status ?? null;
|
|
429
|
+
/** @type {string|null} Task streams: error message */
|
|
430
|
+
this.message = fields.message ?? null;
|
|
431
|
+
/** @type {string|null} Task streams: the final result payload (the same string runTask would return) */
|
|
432
|
+
this.result = fields.result ?? null;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Yield `[event, payload]` pairs from an SSE body: a blank line dispatches the frame,
|
|
438
|
+
* `event:`/`data:` set its fields, `:` lines are heartbeats (Python iter_sse_events rules).
|
|
439
|
+
* @private
|
|
440
|
+
* @param {Object} body - Streamed response body (axios responseType 'stream')
|
|
441
|
+
*/
|
|
442
|
+
async function* _iterSseEvents(body) {
|
|
443
|
+
const decoder = new TextDecoder('utf-8');
|
|
444
|
+
let pending = '';
|
|
445
|
+
let eventName = 'message';
|
|
446
|
+
let dataLines = [];
|
|
447
|
+
for await (const chunk of body) {
|
|
448
|
+
pending += typeof chunk === 'string' ? chunk : decoder.decode(chunk, { stream: true });
|
|
449
|
+
let newline;
|
|
450
|
+
while ((newline = pending.indexOf('\n')) >= 0) {
|
|
451
|
+
const line = pending.slice(0, newline).replace(/\r$/, '');
|
|
452
|
+
pending = pending.slice(newline + 1);
|
|
453
|
+
if (line === '') {
|
|
454
|
+
if (eventName !== 'message' || dataLines.length > 0) {
|
|
455
|
+
const data = dataLines.join('\n');
|
|
456
|
+
let payload = {};
|
|
457
|
+
if (data) {
|
|
458
|
+
try {
|
|
459
|
+
payload = JSON.parse(data);
|
|
460
|
+
} catch (error) {
|
|
461
|
+
throw new AppMeshError(`malformed SSE frame (${eventName}): ${error.message}`);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
yield [eventName, payload];
|
|
465
|
+
}
|
|
466
|
+
eventName = 'message';
|
|
467
|
+
dataLines = [];
|
|
468
|
+
} else if (line.startsWith(':')) {
|
|
469
|
+
// heartbeat comment
|
|
470
|
+
} else if (line.startsWith('event:')) {
|
|
471
|
+
eventName = line.slice('event:'.length).trim();
|
|
472
|
+
} else if (line.startsWith('data:')) {
|
|
473
|
+
dataLines.push(line.slice('data:'.length).trimStart());
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* Build the terminal marker from an `end` frame payload.
|
|
481
|
+
* @private
|
|
482
|
+
* @param {Object} payload - Parsed `end` frame data
|
|
483
|
+
* @param {Object} [extra] - Additional fields (e.g. the collected final result)
|
|
484
|
+
* @returns {SseEnd} Terminal marker
|
|
485
|
+
*/
|
|
486
|
+
function _endFromPayload(payload, extra = {}) {
|
|
487
|
+
return new SseEnd(String(payload.reason ?? 'closed'), {
|
|
488
|
+
exitCode: payload.exit_code,
|
|
489
|
+
position: payload.position,
|
|
490
|
+
status: payload.status,
|
|
491
|
+
message: payload.message,
|
|
492
|
+
...extra
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* Turn an error/timeout terminal frame into the matching exception.
|
|
498
|
+
* @private
|
|
499
|
+
* @param {SseEnd} end - Terminal marker
|
|
500
|
+
* @throws {AppMeshRequestError} reason is 'error'
|
|
501
|
+
* @throws {AppMeshTimeoutError} reason is 'timeout'
|
|
502
|
+
* @throws {AppMeshError} reason is unknown
|
|
503
|
+
*/
|
|
504
|
+
function _raiseOnErrorEnd(end) {
|
|
505
|
+
if (end.reason === 'error') {
|
|
506
|
+
throw new AppMeshRequestError(end.message || 'stream failed with an error', end.status ?? null);
|
|
507
|
+
}
|
|
508
|
+
if (end.reason === 'timeout') {
|
|
509
|
+
throw new AppMeshTimeoutError(end.message || 'stream timed out');
|
|
510
|
+
}
|
|
511
|
+
if (!['exit', 'removed', 'eof', 'final', 'disconnected', 'closed'].includes(end.reason)) {
|
|
512
|
+
throw new AppMeshError(`stream ended with unknown reason: ${end.reason}`);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
379
516
|
/**
|
|
380
517
|
* AppMesh REST Service client
|
|
381
518
|
*/
|
|
@@ -644,6 +781,19 @@ class AppMeshClient {
|
|
|
644
781
|
if (!canRefresh || typeof provider.refreshAccessToken !== 'function' || !_isReplayableBody(config.data)) {
|
|
645
782
|
return response;
|
|
646
783
|
}
|
|
784
|
+
// A signed request's 401 is terminal unless the server pins the blame on
|
|
785
|
+
// the bearer (WWW-Authenticate ... invalid_token): refresh tokens are
|
|
786
|
+
// single-use, so force-refreshing on a signature rejection would burn the
|
|
787
|
+
// session without fixing the request.
|
|
788
|
+
if (this._requestSigner) {
|
|
789
|
+
const headers = response.headers || {};
|
|
790
|
+
const challenge = typeof headers.get === 'function'
|
|
791
|
+
? headers.get('WWW-Authenticate')
|
|
792
|
+
: (headers['WWW-Authenticate'] || headers['www-authenticate']);
|
|
793
|
+
if (!challenge || !challenge.includes('invalid_token')) {
|
|
794
|
+
return response;
|
|
795
|
+
}
|
|
796
|
+
}
|
|
647
797
|
|
|
648
798
|
const rejectedToken = _extractBearerToken(config.headers) ?? (await this._getAccessToken());
|
|
649
799
|
const newToken = await provider.refreshAccessToken(rejectedToken);
|
|
@@ -719,13 +869,13 @@ class AppMeshClient {
|
|
|
719
869
|
* session_login: false,
|
|
720
870
|
* description: "",
|
|
721
871
|
* metadata: "",
|
|
872
|
+
* stdin: "none", // "pipe" keeps the daemon-side write end open so sendInput() can drive the process
|
|
722
873
|
* working_dir: "",
|
|
723
874
|
* enabled: 1,
|
|
724
875
|
* docker_image: "",
|
|
725
876
|
* docker_ports: [], // docker run -p spelling: "[hostIp:]hostPort:containerPort[/protocol]"
|
|
726
877
|
* docker_volumes: [], // docker run -v spelling
|
|
727
878
|
* docker_network: null, // host, bridge, none or a network name
|
|
728
|
-
* docker_backend: null, // "api" (default) or "cli"
|
|
729
879
|
* stdout_backup_count: 3,
|
|
730
880
|
* start_time: "",
|
|
731
881
|
* end_time: "",
|
|
@@ -807,8 +957,8 @@ class AppMeshClient {
|
|
|
807
957
|
|
|
808
958
|
const response = await this._request("get", `/appmesh/app/${app_name}/output`, null, { params });
|
|
809
959
|
// axios lowercases header names; the TCP transport keeps the daemon's exact case
|
|
810
|
-
const outPositionHeader = _getHeader(response.headers,
|
|
811
|
-
const exitCodeHeader = _getHeader(response.headers,
|
|
960
|
+
const outPositionHeader = _getHeader(response.headers, CONSTANTS.HTTP_HEADER_KEY_X_OUTPUT_POSITION);
|
|
961
|
+
const exitCodeHeader = _getHeader(response.headers, CONSTANTS.HTTP_HEADER_KEY_X_EXIT_CODE);
|
|
812
962
|
const outPosition = outPositionHeader ? parseInt(outPositionHeader, 10) : null;
|
|
813
963
|
const exitCode = exitCodeHeader ? parseInt(exitCodeHeader, 10) : null;
|
|
814
964
|
return new AppOutput(response.status, response.data, outPosition, exitCode);
|
|
@@ -837,7 +987,7 @@ class AppMeshClient {
|
|
|
837
987
|
}
|
|
838
988
|
if (response.status === 200) {
|
|
839
989
|
// axios lowercases header names; the TCP transport keeps the daemon's exact case
|
|
840
|
-
const exitCodeHeader = _getHeader(response.headers,
|
|
990
|
+
const exitCodeHeader = _getHeader(response.headers, CONSTANTS.HTTP_HEADER_KEY_X_EXIT_CODE);
|
|
841
991
|
if (exitCodeHeader) {
|
|
842
992
|
exitCode = parseInt(exitCodeHeader, 10);
|
|
843
993
|
}
|
|
@@ -933,6 +1083,164 @@ class AppMeshClient {
|
|
|
933
1083
|
return true;
|
|
934
1084
|
}
|
|
935
1085
|
|
|
1086
|
+
/**
|
|
1087
|
+
* Stream an application's stdout incrementally over server-sent events.
|
|
1088
|
+
*
|
|
1089
|
+
* Replays from `stdoutPosition`, then (with `follow`) yields new output until exit,
|
|
1090
|
+
* removal or `timeout`. The server de-duplicates the replay/live boundary.
|
|
1091
|
+
*
|
|
1092
|
+
* @param {string} appName - Name of the target application
|
|
1093
|
+
* @param {Object} [opts={}] - Stream options
|
|
1094
|
+
* @param {number} [opts.stdoutPosition=0] - Byte cursor to start from (0 replays from the beginning)
|
|
1095
|
+
* @param {string} [opts.processUuid=''] - Pin the stream to one run; empty follows the current run
|
|
1096
|
+
* @param {boolean} [opts.follow=true] - `false` replays to the current end of output and stops
|
|
1097
|
+
* @param {number} [opts.timeout=3600] - Overall stream lifetime cap in seconds (server-side)
|
|
1098
|
+
* @returns {AsyncGenerator<string>} Yields each stdout chunk; the return value (final
|
|
1099
|
+
* `next()` result, `{ done: true, value }`) is the terminal {@link SseEnd} with
|
|
1100
|
+
* `exitCode`/`position` filled on a natural exit
|
|
1101
|
+
* @throws {AppMeshRequestError} The stream ended with an error
|
|
1102
|
+
*/
|
|
1103
|
+
async *followOutput(appName, opts = {}) {
|
|
1104
|
+
const { stdoutPosition = 0, processUuid = '', follow = true, timeout = 3600 } = opts;
|
|
1105
|
+
const params = {
|
|
1106
|
+
stdout_position: String(stdoutPosition),
|
|
1107
|
+
follow: follow ? "true" : "false",
|
|
1108
|
+
timeout: String(timeout)
|
|
1109
|
+
};
|
|
1110
|
+
if (processUuid) {
|
|
1111
|
+
params.process_uuid = processUuid;
|
|
1112
|
+
}
|
|
1113
|
+
const response = await this._sseRequest("get", `/appmesh/app/${appName}/output/stream`, { params });
|
|
1114
|
+
let end = null;
|
|
1115
|
+
try {
|
|
1116
|
+
for await (const [event, payload] of _iterSseEvents(response.data)) {
|
|
1117
|
+
if (event === "stdout") {
|
|
1118
|
+
yield String(payload.output ?? "");
|
|
1119
|
+
} else if (event === "end") {
|
|
1120
|
+
end = _endFromPayload(payload);
|
|
1121
|
+
break;
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
} catch (error) {
|
|
1125
|
+
// Transport-level failure (connection reset) normalizes like _request
|
|
1126
|
+
throw this.onError(error);
|
|
1127
|
+
} finally {
|
|
1128
|
+
response.data?.destroy?.();
|
|
1129
|
+
}
|
|
1130
|
+
if (end === null) {
|
|
1131
|
+
end = new SseEnd("closed");
|
|
1132
|
+
}
|
|
1133
|
+
_raiseOnErrorEnd(end);
|
|
1134
|
+
return end;
|
|
1135
|
+
}
|
|
1136
|
+
|
|
1137
|
+
/**
|
|
1138
|
+
* Streaming task RPC: send an invocation and yield incremental results as they arrive.
|
|
1139
|
+
*
|
|
1140
|
+
* The runTask() counterpart for continuous-output services (LLM agents): the worker
|
|
1141
|
+
* pushes partial results (`worker.sendTaskPartial`) before its final reply, in order.
|
|
1142
|
+
*
|
|
1143
|
+
* @param {string} appName - Name of the target application
|
|
1144
|
+
* @param {string} data - Payload to deliver to the application (same contract as runTask)
|
|
1145
|
+
* @param {Object} [opts={}] - Stream options
|
|
1146
|
+
* @param {number} [opts.timeout=300] - Maximum seconds for the whole call; the stream ends
|
|
1147
|
+
* with `SseEnd(reason='timeout')` (raised as AppMeshTimeoutError) when exceeded
|
|
1148
|
+
* @returns {AsyncGenerator<string>} Yields each partial; the return value is an {@link SseEnd}
|
|
1149
|
+
* whose `result` carries the final payload (what runTask would return)
|
|
1150
|
+
* @throws {AppMeshTimeoutError} The task did not finish within `timeout`
|
|
1151
|
+
* @throws {AppMeshRequestError} The stream ended with an error
|
|
1152
|
+
*/
|
|
1153
|
+
async *runTaskStream(appName, data, opts = {}) {
|
|
1154
|
+
const { timeout = 300 } = opts;
|
|
1155
|
+
const params = timeout && timeout > 0 ? { timeout: String(timeout) } : {};
|
|
1156
|
+
const response = await this._sseRequest("post", `/appmesh/app/${appName}/task/stream`, { params, body: data });
|
|
1157
|
+
let end = null;
|
|
1158
|
+
let finalResult = null;
|
|
1159
|
+
try {
|
|
1160
|
+
for await (const [event, payload] of _iterSseEvents(response.data)) {
|
|
1161
|
+
if (event === "partial") {
|
|
1162
|
+
yield String(payload.data ?? "");
|
|
1163
|
+
} else if (event === "final") {
|
|
1164
|
+
finalResult = payload.result ?? null;
|
|
1165
|
+
} else if (event === "end") {
|
|
1166
|
+
end = _endFromPayload(payload, { result: finalResult });
|
|
1167
|
+
break;
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
} catch (error) {
|
|
1171
|
+
// Transport-level failure (connection reset) normalizes like _request
|
|
1172
|
+
throw this.onError(error);
|
|
1173
|
+
} finally {
|
|
1174
|
+
response.data?.destroy?.();
|
|
1175
|
+
}
|
|
1176
|
+
if (end === null) {
|
|
1177
|
+
end = new SseEnd("closed", { result: finalResult });
|
|
1178
|
+
}
|
|
1179
|
+
_raiseOnErrorEnd(end);
|
|
1180
|
+
return end;
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
/**
|
|
1184
|
+
* Run a one-shot application remotely and stream its stdout incrementally.
|
|
1185
|
+
*
|
|
1186
|
+
* runAppAsync() + followOutput() + deleteApp(): stdout chunks are yielded as produced,
|
|
1187
|
+
* unlike the fully-buffered runAppSync() that answers only after completion.
|
|
1188
|
+
*
|
|
1189
|
+
* @param {Object|string} app - App config, or a shell command string (runs with `shell: true`, daemon-generated name)
|
|
1190
|
+
* @param {string|number} [maxTime] - Maximum runtime for the remote process (seconds or ISO 8601 duration)
|
|
1191
|
+
* @param {string|number} [lifecycle] - Retention window for the temporary run app (seconds or ISO 8601 duration)
|
|
1192
|
+
* @returns {AsyncGenerator<string>} Yields each stdout chunk; the return value is the run's
|
|
1193
|
+
* {@link SseEnd} (`exitCode` on a natural exit). The temporary app is always deleted on exit.
|
|
1194
|
+
* @throws {AppMeshRequestError} The stream ended with an error
|
|
1195
|
+
*/
|
|
1196
|
+
async *streamRun(app, maxTime = CONSTANTS.DEFAULT_RUN_APP_TIMEOUT_SECONDS,
|
|
1197
|
+
lifecycle = CONSTANTS.DEFAULT_RUN_APP_LIFECYCLE_SECONDS) {
|
|
1198
|
+
const appConfig = typeof app === "string" ? { command: app, shell: true } : app;
|
|
1199
|
+
const run = await this.runAppAsync(appConfig, maxTime, lifecycle);
|
|
1200
|
+
let end;
|
|
1201
|
+
try {
|
|
1202
|
+
// Delegate to followOutput and re-yield: JS has no `yield from`.
|
|
1203
|
+
const stream = this.followOutput(run.appName, {
|
|
1204
|
+
processUuid: run.procUid,
|
|
1205
|
+
follow: true,
|
|
1206
|
+
timeout: Math.max(parseDuration(maxTime), parseDuration(lifecycle))
|
|
1207
|
+
});
|
|
1208
|
+
let next = await stream.next();
|
|
1209
|
+
while (!next.done) {
|
|
1210
|
+
yield next.value;
|
|
1211
|
+
next = await stream.next();
|
|
1212
|
+
}
|
|
1213
|
+
end = next.value;
|
|
1214
|
+
} finally {
|
|
1215
|
+
try {
|
|
1216
|
+
await this.deleteApp(run.appName);
|
|
1217
|
+
} catch (_) { /* best-effort cleanup of the temporary run app */ }
|
|
1218
|
+
}
|
|
1219
|
+
return end;
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1222
|
+
/**
|
|
1223
|
+
* Inject bytes into the stdin of a running application.
|
|
1224
|
+
*
|
|
1225
|
+
* The app must be registered with `stdin: "pipe"`: the daemon holds the write end
|
|
1226
|
+
* open, making the process conversational (REPL) while followOutput() reads its output.
|
|
1227
|
+
*
|
|
1228
|
+
* @param {string} appName - Name of the target application
|
|
1229
|
+
* @param {string|Buffer} data - Bytes (or text, encoded as UTF-8) to write to the process stdin
|
|
1230
|
+
* @param {Object} [opts={}] - Write options
|
|
1231
|
+
* @param {boolean} [opts.close=false] - Also close the pipe write end so the process reads EOF
|
|
1232
|
+
* @param {string} [opts.processUuid=''] - Restrict the write to this run; empty targets the current run
|
|
1233
|
+
* @returns {Promise<number>} Number of bytes written
|
|
1234
|
+
* @throws {AppMeshError} The stdin pipe buffer is full (retry with the remainder) or the write failed
|
|
1235
|
+
*/
|
|
1236
|
+
async sendInput(appName, data, opts = {}) {
|
|
1237
|
+
const { close = false, processUuid = '' } = opts;
|
|
1238
|
+
const headers = close ? { [CONSTANTS.HTTP_HEADER_KEY_X_INPUT_CLOSE]: "true" } : {};
|
|
1239
|
+
const params = processUuid ? { process_uuid: processUuid } : {};
|
|
1240
|
+
const response = await this._request("post", `/appmesh/app/${appName}/input`, data, { headers, params });
|
|
1241
|
+
return parseInt(response.data?.bytes_written ?? 0, 10);
|
|
1242
|
+
}
|
|
1243
|
+
|
|
936
1244
|
/**
|
|
937
1245
|
* Download a remote file.
|
|
938
1246
|
*
|
|
@@ -1041,8 +1349,10 @@ class AppMeshClient {
|
|
|
1041
1349
|
* @param {string} [filePath=null] - Remote target path; defaults to the basename of localFile
|
|
1042
1350
|
* @param {boolean} [applyAttrs=false] - In Node.js, send local permission bits plus
|
|
1043
1351
|
* user/group (names when resolvable locally, numeric ids otherwise)
|
|
1352
|
+
* @param {boolean} [overwrite=false] - Replace an existing remote file; the
|
|
1353
|
+
* default keeps the server's 409 refusal
|
|
1044
1354
|
*/
|
|
1045
|
-
async uploadFile(localFile, filePath = null, applyAttrs = false) {
|
|
1355
|
+
async uploadFile(localFile, filePath = null, applyAttrs = false, overwrite = false) {
|
|
1046
1356
|
if (!filePath) {
|
|
1047
1357
|
// Node.js passes a path string; browser passes a File (use its name)
|
|
1048
1358
|
const sourceName = typeof localFile === 'string' ? localFile : localFile && localFile.name;
|
|
@@ -1052,6 +1362,9 @@ class AppMeshClient {
|
|
|
1052
1362
|
filePath = sourceName.split(/[\\/]/).pop();
|
|
1053
1363
|
}
|
|
1054
1364
|
const headers = { [CONSTANTS.HTTP_HEADER_KEY_X_FILE_PATH]: encodeURIComponent(filePath) };
|
|
1365
|
+
if (overwrite) {
|
|
1366
|
+
headers[CONSTANTS.HTTP_HEADER_KEY_X_FILE_OVERWRITE] = 'true';
|
|
1367
|
+
}
|
|
1055
1368
|
let body;
|
|
1056
1369
|
|
|
1057
1370
|
if (ENV.isNode) {
|
|
@@ -1320,16 +1633,7 @@ class AppMeshClient {
|
|
|
1320
1633
|
*/
|
|
1321
1634
|
async _request(method, path, body = null, options = {}) {
|
|
1322
1635
|
// Ensure HTTPS agent is ready (first-call lazy setup for Node.js ESM)
|
|
1323
|
-
|
|
1324
|
-
await _httpsReady;
|
|
1325
|
-
if (_https) {
|
|
1326
|
-
this._client.defaults.httpsAgent = new _https.Agent({
|
|
1327
|
-
..._resolveSslOptions(this._sslConfig),
|
|
1328
|
-
keepAlive: true,
|
|
1329
|
-
keepAliveMsecs: 3000
|
|
1330
|
-
});
|
|
1331
|
-
}
|
|
1332
|
-
}
|
|
1636
|
+
await this._ensureHttpsAgent();
|
|
1333
1637
|
|
|
1334
1638
|
const { headers = {}, params = {}, config = {} } = options;
|
|
1335
1639
|
|
|
@@ -1364,6 +1668,77 @@ class AppMeshClient {
|
|
|
1364
1668
|
}
|
|
1365
1669
|
}
|
|
1366
1670
|
|
|
1671
|
+
/**
|
|
1672
|
+
* Lazily install the shared https.Agent on the axios instance (Node.js ESM first-use setup).
|
|
1673
|
+
* @private
|
|
1674
|
+
*/
|
|
1675
|
+
async _ensureHttpsAgent() {
|
|
1676
|
+
if (ENV.isNode && _httpsReady && !this._client.defaults.httpsAgent) {
|
|
1677
|
+
await _httpsReady;
|
|
1678
|
+
if (_https) {
|
|
1679
|
+
this._client.defaults.httpsAgent = new _https.Agent({
|
|
1680
|
+
..._resolveSslOptions(this._sslConfig),
|
|
1681
|
+
keepAlive: true,
|
|
1682
|
+
keepAliveMsecs: 3000
|
|
1683
|
+
});
|
|
1684
|
+
}
|
|
1685
|
+
}
|
|
1686
|
+
}
|
|
1687
|
+
|
|
1688
|
+
/**
|
|
1689
|
+
* Open a server-sent-events stream as a streamed response. Plain HTTP by design: SSE
|
|
1690
|
+
* frames cannot ride a framed (TCP/WSS) transport, so framed subclasses redirect this
|
|
1691
|
+
* hook to direct HTTPS (the WSS file data side-channel shape).
|
|
1692
|
+
*
|
|
1693
|
+
* @private
|
|
1694
|
+
* @param {string} method - HTTP method ('get' or 'post')
|
|
1695
|
+
* @param {string} path - Endpoint path
|
|
1696
|
+
* @param {Object} [options={}] - `{ params, headers, body }`
|
|
1697
|
+
* @returns {Promise<Object>} Axios response with a streamed `data` body
|
|
1698
|
+
* @throws {AppMeshError} Non-200 statuses, like `_request`
|
|
1699
|
+
*/
|
|
1700
|
+
async _sseRequest(method, path, options = {}) {
|
|
1701
|
+
await this._ensureHttpsAgent();
|
|
1702
|
+
const { params = {}, headers = {}, body } = options;
|
|
1703
|
+
const requestConfig = {
|
|
1704
|
+
method,
|
|
1705
|
+
url: path,
|
|
1706
|
+
withCredentials: false,
|
|
1707
|
+
responseType: "stream",
|
|
1708
|
+
// Own server-side lifetime and 15s heartbeats: axios's 5-minute default would cut long follows short.
|
|
1709
|
+
timeout: 0,
|
|
1710
|
+
headers: { ...headers },
|
|
1711
|
+
params: { ...params }
|
|
1712
|
+
};
|
|
1713
|
+
if (body !== undefined && body !== null) {
|
|
1714
|
+
requestConfig.data = body;
|
|
1715
|
+
}
|
|
1716
|
+
if (this._requestSigner) {
|
|
1717
|
+
await this._applyRequestSignature(requestConfig);
|
|
1718
|
+
}
|
|
1719
|
+
|
|
1720
|
+
let response;
|
|
1721
|
+
try {
|
|
1722
|
+
response = await this._client(requestConfig);
|
|
1723
|
+
} catch (error) {
|
|
1724
|
+
throw this.onError(error);
|
|
1725
|
+
}
|
|
1726
|
+
if (response.status !== 200) {
|
|
1727
|
+
// The error reply is short; collect it for the message.
|
|
1728
|
+
let detail = "";
|
|
1729
|
+
try {
|
|
1730
|
+
const chunks = [];
|
|
1731
|
+
for await (const chunk of response.data) chunks.push(chunk);
|
|
1732
|
+
detail = Buffer.concat(chunks).toString("utf8");
|
|
1733
|
+
} catch (_) { /* socket already gone — raise a status-only error */ }
|
|
1734
|
+
const message = detail
|
|
1735
|
+
? this._extractErrorMessage(detail)
|
|
1736
|
+
: `SSE request failed with status ${response.status}`;
|
|
1737
|
+
throw new AppMeshError(message, response.status, detail || null);
|
|
1738
|
+
}
|
|
1739
|
+
return response;
|
|
1740
|
+
}
|
|
1741
|
+
|
|
1367
1742
|
/**
|
|
1368
1743
|
* Extract user-friendly error message from response data
|
|
1369
1744
|
* @private
|
|
@@ -1497,7 +1872,8 @@ class AppRun {
|
|
|
1497
1872
|
// Export the main classes
|
|
1498
1873
|
export {
|
|
1499
1874
|
AppMeshClient, AppOutput, AppRun, AppMeshError, AppRemovedError, TransportDisconnectedError,
|
|
1500
|
-
|
|
1875
|
+
AppMeshRequestError, AppMeshTimeoutError, SseEnd,
|
|
1876
|
+
DEFAULT_CA_FILE, StaticAccessTokenProvider, RefreshTokenProvider, RequestSigner, CONSTANTS
|
|
1501
1877
|
};
|
|
1502
1878
|
// Exported for the TCP client (Node.js only); not part of the browser-facing API
|
|
1503
1879
|
export { _resolveUid, _resolveGid, _resolveUserName, _resolveGroupName };
|