@phala/cloud 0.6.0 → 0.6.2
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/actions/cvms/logs.d.ts +3 -0
- package/dist/index.js +199 -29
- package/dist/index.mjs +199 -29
- package/dist/types/cvm_info_v20251028.d.ts +1 -0
- package/package.json +1 -1
|
@@ -33,11 +33,14 @@ export declare class LogAccessError extends ResourceError {
|
|
|
33
33
|
readonly code?: string;
|
|
34
34
|
readonly httpStatus?: number;
|
|
35
35
|
readonly suggestions: string[];
|
|
36
|
+
/** True when the error was parsed from a 200 stream body, not an HTTP status. */
|
|
37
|
+
readonly fromStreamBody: boolean;
|
|
36
38
|
constructor(options: {
|
|
37
39
|
message: string;
|
|
38
40
|
code?: string;
|
|
39
41
|
httpStatus?: number;
|
|
40
42
|
suggestions?: readonly string[];
|
|
43
|
+
fromStreamBody?: boolean;
|
|
41
44
|
});
|
|
42
45
|
}
|
|
43
46
|
/** Get a presigned CVM-level log URL for a given node channel. */
|
package/dist/index.js
CHANGED
|
@@ -969,7 +969,7 @@ function defaultUserAgent(runtime = globalThis) {
|
|
|
969
969
|
if (runtime.window !== void 0 && runtime.document !== void 0) {
|
|
970
970
|
return void 0;
|
|
971
971
|
}
|
|
972
|
-
const product = "phala-cloud-sdk-js/0.6.
|
|
972
|
+
const product = "phala-cloud-sdk-js/0.6.2";
|
|
973
973
|
const token = runtimeToken(runtime);
|
|
974
974
|
return token ? `${product} ${token}` : product;
|
|
975
975
|
}
|
|
@@ -1624,6 +1624,7 @@ var CvmDetailV20251028Schema = import_zod5.z.object({
|
|
|
1624
1624
|
instance_type: import_zod5.z.string().optional().nullable(),
|
|
1625
1625
|
public_sysinfo: import_zod5.z.boolean().optional().default(false),
|
|
1626
1626
|
public_logs: import_zod5.z.boolean().optional().default(false),
|
|
1627
|
+
public_tcbinfo: import_zod5.z.boolean().optional(),
|
|
1627
1628
|
managed_env: import_zod5.z.boolean().optional().default(false),
|
|
1628
1629
|
dapp_dashboard_url: import_zod5.z.string().optional().nullable(),
|
|
1629
1630
|
/** Presigned serial log URL; expires shortly after it is issued. */
|
|
@@ -8088,7 +8089,7 @@ var LogAccessError = class extends ResourceError {
|
|
|
8088
8089
|
const suggestionText = options.suggestions?.length ? `
|
|
8089
8090
|
Suggestions:
|
|
8090
8091
|
${options.suggestions.map((suggestion) => `- ${suggestion}`).join("\n")}` : "";
|
|
8091
|
-
const status = options.httpStatus ?? (options.code ? logErrorHttpStatus(options.code) : void 0) ?? 0;
|
|
8092
|
+
const status = options.fromStreamBody ? 0 : options.httpStatus ?? (options.code ? logErrorHttpStatus(options.code) : void 0) ?? 0;
|
|
8092
8093
|
super(`${details}${suggestionText}`, {
|
|
8093
8094
|
status,
|
|
8094
8095
|
statusText: "",
|
|
@@ -8099,6 +8100,7 @@ ${options.suggestions.map((suggestion) => `- ${suggestion}`).join("\n")}` : "";
|
|
|
8099
8100
|
this.code = options.code;
|
|
8100
8101
|
this.httpStatus = status || void 0;
|
|
8101
8102
|
this.suggestions = options.suggestions ? [...options.suggestions] : [];
|
|
8103
|
+
this.fromStreamBody = options.fromStreamBody === true;
|
|
8102
8104
|
}
|
|
8103
8105
|
};
|
|
8104
8106
|
function resolveCvmId(id) {
|
|
@@ -8195,6 +8197,29 @@ function formatFollowContainerEntry(entry, includeTimestamps) {
|
|
|
8195
8197
|
message: timestamp.timestamp ? timestamp.message ?? "" : entry.message
|
|
8196
8198
|
};
|
|
8197
8199
|
}
|
|
8200
|
+
function guestAgentErrorMessage(text) {
|
|
8201
|
+
const trimmed = text.trim();
|
|
8202
|
+
if (!trimmed) return void 0;
|
|
8203
|
+
try {
|
|
8204
|
+
const parsed = JSON.parse(trimmed);
|
|
8205
|
+
if (typeof parsed.error === "string" && parsed.channel === void 0 && parsed.message === void 0) {
|
|
8206
|
+
return parsed.error;
|
|
8207
|
+
}
|
|
8208
|
+
} catch {
|
|
8209
|
+
return void 0;
|
|
8210
|
+
}
|
|
8211
|
+
return void 0;
|
|
8212
|
+
}
|
|
8213
|
+
function throwIfGuestAgentError(text) {
|
|
8214
|
+
const message = guestAgentErrorMessage(text);
|
|
8215
|
+
if (message !== void 0) {
|
|
8216
|
+
throw new LogAccessError({
|
|
8217
|
+
code: "ERR-17-009",
|
|
8218
|
+
message,
|
|
8219
|
+
fromStreamBody: true
|
|
8220
|
+
});
|
|
8221
|
+
}
|
|
8222
|
+
}
|
|
8198
8223
|
function parseStructuredLogLineToEntry(line) {
|
|
8199
8224
|
const trimmed = line.trim();
|
|
8200
8225
|
if (!trimmed) return null;
|
|
@@ -8204,6 +8229,13 @@ function parseStructuredLogLineToEntry(line) {
|
|
|
8204
8229
|
} catch {
|
|
8205
8230
|
throw new LogAccessError({ message: "Invalid NDJSON log record." });
|
|
8206
8231
|
}
|
|
8232
|
+
if (typeof parsed.error === "string" && parsed.channel === void 0 && parsed.message === void 0) {
|
|
8233
|
+
throw new LogAccessError({
|
|
8234
|
+
code: "ERR-17-009",
|
|
8235
|
+
message: parsed.error,
|
|
8236
|
+
fromStreamBody: true
|
|
8237
|
+
});
|
|
8238
|
+
}
|
|
8207
8239
|
if (typeof parsed.error_code === "string") {
|
|
8208
8240
|
throw new LogAccessError({
|
|
8209
8241
|
code: parsed.error_code,
|
|
@@ -8293,8 +8325,19 @@ async function getCvmLogEndpoint(client, id, channel) {
|
|
|
8293
8325
|
}
|
|
8294
8326
|
return endpoint;
|
|
8295
8327
|
}
|
|
8328
|
+
function containerLogsDisabledError() {
|
|
8329
|
+
return new LogAccessError({
|
|
8330
|
+
code: "ERR-17-006",
|
|
8331
|
+
message: "Container logs are disabled",
|
|
8332
|
+
httpStatus: 409
|
|
8333
|
+
});
|
|
8334
|
+
}
|
|
8296
8335
|
async function getContainerLogEndpoint(client, id, containerName, matchContainerIdPrefix = true) {
|
|
8297
8336
|
const cvmId = resolveCvmId(id);
|
|
8337
|
+
const info = await getCvmInfoLogs(client, cvmId);
|
|
8338
|
+
if (info.public_logs === false) {
|
|
8339
|
+
throw containerLogsDisabledError();
|
|
8340
|
+
}
|
|
8298
8341
|
const composition = await client.get(`/cvms/${cvmId}/composition`);
|
|
8299
8342
|
if (!composition.containers?.length) {
|
|
8300
8343
|
throw new Error(`No containers found for CVM '${cvmId}'`);
|
|
@@ -8342,9 +8385,11 @@ function getErrorHttpStatus(error) {
|
|
|
8342
8385
|
}
|
|
8343
8386
|
return void 0;
|
|
8344
8387
|
}
|
|
8345
|
-
|
|
8388
|
+
var EMPTY_STREAM_END_MESSAGE = "log stream keeps ending without new output; the container may have stopped";
|
|
8389
|
+
async function followLogRequests(resolveUrl, consume, signal, refreshCvm, trackEmptyEnds = false) {
|
|
8346
8390
|
let failureCount = 0;
|
|
8347
8391
|
let expiredRetries = 0;
|
|
8392
|
+
let emptyEnds = 0;
|
|
8348
8393
|
while (!signal?.aborted) {
|
|
8349
8394
|
try {
|
|
8350
8395
|
const url = await resolveUrl();
|
|
@@ -8352,14 +8397,31 @@ async function followLogRequests(resolveUrl, consume, signal, refreshCvm) {
|
|
|
8352
8397
|
const response = await fetch(url, { signal });
|
|
8353
8398
|
if (!response.ok) await throwResponseError(response);
|
|
8354
8399
|
failureCount = 0;
|
|
8355
|
-
await consume(response);
|
|
8400
|
+
const delivered = await consume(response) ?? true;
|
|
8356
8401
|
expiredRetries = 0;
|
|
8357
8402
|
if (signal?.aborted) return;
|
|
8403
|
+
if (trackEmptyEnds) {
|
|
8404
|
+
if (delivered) {
|
|
8405
|
+
emptyEnds = 0;
|
|
8406
|
+
} else {
|
|
8407
|
+
emptyEnds += 1;
|
|
8408
|
+
if (emptyEnds >= 5) {
|
|
8409
|
+
throw new LogAccessError({
|
|
8410
|
+
message: EMPTY_STREAM_END_MESSAGE,
|
|
8411
|
+
fromStreamBody: true
|
|
8412
|
+
});
|
|
8413
|
+
}
|
|
8414
|
+
await waitForReconnect(signal, Math.min(250 * 2 ** (emptyEnds - 1), 5e3));
|
|
8415
|
+
}
|
|
8416
|
+
}
|
|
8358
8417
|
} catch (error) {
|
|
8359
8418
|
if (signal?.aborted || error instanceof Error && error.name === "AbortError") {
|
|
8360
8419
|
return;
|
|
8361
8420
|
}
|
|
8362
8421
|
let retryError = error;
|
|
8422
|
+
if (error instanceof LogAccessError && error.fromStreamBody) {
|
|
8423
|
+
throw error;
|
|
8424
|
+
}
|
|
8363
8425
|
if (error instanceof LogAccessError && error.code === "ERR-17-003") {
|
|
8364
8426
|
expiredRetries += 1;
|
|
8365
8427
|
if (expiredRetries > 3) throw error;
|
|
@@ -8441,6 +8503,27 @@ async function fetchContainerLogsEntries(client, id, options = {}) {
|
|
|
8441
8503
|
return parseStructuredLogsToEntries(await response.text());
|
|
8442
8504
|
}
|
|
8443
8505
|
async function fetchContainerLogs(client, id, options = {}) {
|
|
8506
|
+
if (options.format === "text") {
|
|
8507
|
+
const times = resolveLogTimeWindow(options.since, options.until);
|
|
8508
|
+
const baseUrl = await getContainerLogEndpoint(
|
|
8509
|
+
client,
|
|
8510
|
+
id,
|
|
8511
|
+
options.container,
|
|
8512
|
+
options.matchContainerIdPrefix ?? true
|
|
8513
|
+
);
|
|
8514
|
+
const url = buildLogUrl(baseUrl, {
|
|
8515
|
+
...times,
|
|
8516
|
+
tail: options.tail,
|
|
8517
|
+
timestamps: options.timestamps,
|
|
8518
|
+
ansi: options.ansi,
|
|
8519
|
+
format: "text"
|
|
8520
|
+
});
|
|
8521
|
+
const response = await fetch(url, { signal: options.signal });
|
|
8522
|
+
if (!response.ok) await throwResponseError(response);
|
|
8523
|
+
const text = await response.text();
|
|
8524
|
+
throwIfGuestAgentError(text);
|
|
8525
|
+
return text;
|
|
8526
|
+
}
|
|
8444
8527
|
const entries = await fetchContainerLogsEntries(client, id, {
|
|
8445
8528
|
...options,
|
|
8446
8529
|
format: "ndjson"
|
|
@@ -8474,38 +8557,115 @@ async function streamContainerLogsEntries(client, id, onEntry, options = {}, sig
|
|
|
8474
8557
|
};
|
|
8475
8558
|
await followLogRequests(
|
|
8476
8559
|
buildRequestUrl,
|
|
8477
|
-
|
|
8478
|
-
|
|
8479
|
-
|
|
8480
|
-
|
|
8481
|
-
|
|
8482
|
-
|
|
8483
|
-
|
|
8484
|
-
if (cursor.boundarySecond !== void 0 && timestamp.second < cursor.boundarySecond) {
|
|
8485
|
-
onEntry(formatFollowContainerEntry(entry, options.timestamps ?? false));
|
|
8486
|
-
return;
|
|
8487
|
-
}
|
|
8488
|
-
if (timestamp.second === cursor.boundarySecond) {
|
|
8489
|
-
const replayCount = replayCounts.get(key) ?? 0;
|
|
8490
|
-
if (replayCount > 0) {
|
|
8491
|
-
replayCounts.set(key, replayCount - 1);
|
|
8560
|
+
async (response) => {
|
|
8561
|
+
let delivered = false;
|
|
8562
|
+
await streamNdjsonEntriesResponse(response, (entry) => {
|
|
8563
|
+
const timestamp = parseTimestampedMessage(entry.message);
|
|
8564
|
+
if (timestamp.second === void 0 || !timestamp.timestamp) {
|
|
8565
|
+
delivered = true;
|
|
8566
|
+
onEntry(formatFollowContainerEntry(entry, options.timestamps ?? false));
|
|
8492
8567
|
return;
|
|
8493
8568
|
}
|
|
8494
|
-
|
|
8495
|
-
cursor.boundarySecond
|
|
8496
|
-
|
|
8497
|
-
|
|
8498
|
-
|
|
8499
|
-
|
|
8500
|
-
|
|
8501
|
-
|
|
8569
|
+
const key = JSON.stringify([entry.channel, entry.message]);
|
|
8570
|
+
if (cursor.boundarySecond !== void 0 && timestamp.second < cursor.boundarySecond) {
|
|
8571
|
+
delivered = true;
|
|
8572
|
+
onEntry(formatFollowContainerEntry(entry, options.timestamps ?? false));
|
|
8573
|
+
return;
|
|
8574
|
+
}
|
|
8575
|
+
if (timestamp.second === cursor.boundarySecond) {
|
|
8576
|
+
const replayCount = replayCounts.get(key) ?? 0;
|
|
8577
|
+
if (replayCount > 0) {
|
|
8578
|
+
replayCounts.set(key, replayCount - 1);
|
|
8579
|
+
return;
|
|
8580
|
+
}
|
|
8581
|
+
} else if (cursor.boundarySecond === void 0 || timestamp.second > cursor.boundarySecond) {
|
|
8582
|
+
cursor.boundarySecond = timestamp.second;
|
|
8583
|
+
cursor.boundaryCounts.clear();
|
|
8584
|
+
}
|
|
8585
|
+
cursor.since = Math.max(cursor.since ?? timestamp.second, timestamp.second);
|
|
8586
|
+
cursor.boundaryCounts.set(key, (cursor.boundaryCounts.get(key) ?? 0) + 1);
|
|
8587
|
+
delivered = true;
|
|
8588
|
+
onEntry(formatFollowContainerEntry(entry, options.timestamps ?? false));
|
|
8589
|
+
});
|
|
8590
|
+
return delivered;
|
|
8591
|
+
},
|
|
8502
8592
|
abort,
|
|
8503
8593
|
async () => {
|
|
8504
8594
|
await getCvmInfoLogs(client, resolveCvmId(id));
|
|
8505
|
-
}
|
|
8595
|
+
},
|
|
8596
|
+
true
|
|
8506
8597
|
);
|
|
8507
8598
|
}
|
|
8599
|
+
async function streamContainerText(response, onData) {
|
|
8600
|
+
const reader = response.body?.getReader();
|
|
8601
|
+
if (!reader) throw new Error("No response body available for streaming");
|
|
8602
|
+
const decoder = new TextDecoder();
|
|
8603
|
+
let buffer = "";
|
|
8604
|
+
let finished = false;
|
|
8605
|
+
let delivered = false;
|
|
8606
|
+
try {
|
|
8607
|
+
while (true) {
|
|
8608
|
+
const { done, value } = await reader.read();
|
|
8609
|
+
buffer += decoder.decode(value, { stream: !done });
|
|
8610
|
+
let newline = buffer.indexOf("\n");
|
|
8611
|
+
while (newline >= 0) {
|
|
8612
|
+
const line = buffer.slice(0, newline);
|
|
8613
|
+
buffer = buffer.slice(newline + 1);
|
|
8614
|
+
throwIfGuestAgentError(line);
|
|
8615
|
+
onData(line + "\n");
|
|
8616
|
+
delivered = true;
|
|
8617
|
+
newline = buffer.indexOf("\n");
|
|
8618
|
+
}
|
|
8619
|
+
if (done) {
|
|
8620
|
+
finished = true;
|
|
8621
|
+
break;
|
|
8622
|
+
}
|
|
8623
|
+
}
|
|
8624
|
+
if (buffer) {
|
|
8625
|
+
throwIfGuestAgentError(buffer);
|
|
8626
|
+
onData(buffer);
|
|
8627
|
+
delivered = true;
|
|
8628
|
+
}
|
|
8629
|
+
} finally {
|
|
8630
|
+
if (!finished) await reader.cancel().catch(() => {
|
|
8631
|
+
});
|
|
8632
|
+
reader.releaseLock();
|
|
8633
|
+
}
|
|
8634
|
+
return delivered;
|
|
8635
|
+
}
|
|
8508
8636
|
async function streamContainerLogs(client, id, onData, options = {}, signal) {
|
|
8637
|
+
if (options.format === "text") {
|
|
8638
|
+
const abort = signal ?? options.signal;
|
|
8639
|
+
const times = resolveLogTimeWindow(options.since, options.until);
|
|
8640
|
+
let firstRequest = true;
|
|
8641
|
+
await followLogRequests(
|
|
8642
|
+
async () => {
|
|
8643
|
+
const baseUrl = await getContainerLogEndpoint(
|
|
8644
|
+
client,
|
|
8645
|
+
id,
|
|
8646
|
+
options.container,
|
|
8647
|
+
options.matchContainerIdPrefix ?? true
|
|
8648
|
+
);
|
|
8649
|
+
const tail = firstRequest ? options.tail : void 0;
|
|
8650
|
+
firstRequest = false;
|
|
8651
|
+
return buildLogUrl(baseUrl, {
|
|
8652
|
+
...times,
|
|
8653
|
+
tail,
|
|
8654
|
+
follow: true,
|
|
8655
|
+
timestamps: options.timestamps,
|
|
8656
|
+
ansi: options.ansi,
|
|
8657
|
+
format: "text"
|
|
8658
|
+
});
|
|
8659
|
+
},
|
|
8660
|
+
(response) => streamContainerText(response, onData),
|
|
8661
|
+
abort,
|
|
8662
|
+
async () => {
|
|
8663
|
+
await getCvmInfoLogs(client, resolveCvmId(id));
|
|
8664
|
+
},
|
|
8665
|
+
true
|
|
8666
|
+
);
|
|
8667
|
+
return;
|
|
8668
|
+
}
|
|
8509
8669
|
return streamContainerLogsEntries(
|
|
8510
8670
|
client,
|
|
8511
8671
|
id,
|
|
@@ -8540,7 +8700,17 @@ async function* followCvmLogs(client, id, source, options = {}) {
|
|
|
8540
8700
|
pending.push(item);
|
|
8541
8701
|
wake?.();
|
|
8542
8702
|
};
|
|
8543
|
-
const run = source.type === "node" ? streamCvmChannelLogs(client, id, source.channel, push, options, options.signal) :
|
|
8703
|
+
const run = source.type === "node" ? streamCvmChannelLogs(client, id, source.channel, push, options, options.signal) : options.format === "text" ? streamContainerLogs(
|
|
8704
|
+
client,
|
|
8705
|
+
id,
|
|
8706
|
+
(data) => push(data),
|
|
8707
|
+
{
|
|
8708
|
+
...options,
|
|
8709
|
+
container: source.name,
|
|
8710
|
+
matchContainerIdPrefix: source.matchContainerIdPrefix
|
|
8711
|
+
},
|
|
8712
|
+
options.signal
|
|
8713
|
+
) : streamContainerLogsEntries(
|
|
8544
8714
|
client,
|
|
8545
8715
|
id,
|
|
8546
8716
|
push,
|
package/dist/index.mjs
CHANGED
|
@@ -440,7 +440,7 @@ function defaultUserAgent(runtime = globalThis) {
|
|
|
440
440
|
if (runtime.window !== void 0 && runtime.document !== void 0) {
|
|
441
441
|
return void 0;
|
|
442
442
|
}
|
|
443
|
-
const product = "phala-cloud-sdk-js/0.6.
|
|
443
|
+
const product = "phala-cloud-sdk-js/0.6.2";
|
|
444
444
|
const token = runtimeToken(runtime);
|
|
445
445
|
return token ? `${product} ${token}` : product;
|
|
446
446
|
}
|
|
@@ -1095,6 +1095,7 @@ var CvmDetailV20251028Schema = z5.object({
|
|
|
1095
1095
|
instance_type: z5.string().optional().nullable(),
|
|
1096
1096
|
public_sysinfo: z5.boolean().optional().default(false),
|
|
1097
1097
|
public_logs: z5.boolean().optional().default(false),
|
|
1098
|
+
public_tcbinfo: z5.boolean().optional(),
|
|
1098
1099
|
managed_env: z5.boolean().optional().default(false),
|
|
1099
1100
|
dapp_dashboard_url: z5.string().optional().nullable(),
|
|
1100
1101
|
/** Presigned serial log URL; expires shortly after it is issued. */
|
|
@@ -7591,7 +7592,7 @@ var LogAccessError = class extends ResourceError {
|
|
|
7591
7592
|
const suggestionText = options.suggestions?.length ? `
|
|
7592
7593
|
Suggestions:
|
|
7593
7594
|
${options.suggestions.map((suggestion) => `- ${suggestion}`).join("\n")}` : "";
|
|
7594
|
-
const status = options.httpStatus ?? (options.code ? logErrorHttpStatus(options.code) : void 0) ?? 0;
|
|
7595
|
+
const status = options.fromStreamBody ? 0 : options.httpStatus ?? (options.code ? logErrorHttpStatus(options.code) : void 0) ?? 0;
|
|
7595
7596
|
super(`${details}${suggestionText}`, {
|
|
7596
7597
|
status,
|
|
7597
7598
|
statusText: "",
|
|
@@ -7602,6 +7603,7 @@ ${options.suggestions.map((suggestion) => `- ${suggestion}`).join("\n")}` : "";
|
|
|
7602
7603
|
this.code = options.code;
|
|
7603
7604
|
this.httpStatus = status || void 0;
|
|
7604
7605
|
this.suggestions = options.suggestions ? [...options.suggestions] : [];
|
|
7606
|
+
this.fromStreamBody = options.fromStreamBody === true;
|
|
7605
7607
|
}
|
|
7606
7608
|
};
|
|
7607
7609
|
function resolveCvmId(id) {
|
|
@@ -7698,6 +7700,29 @@ function formatFollowContainerEntry(entry, includeTimestamps) {
|
|
|
7698
7700
|
message: timestamp.timestamp ? timestamp.message ?? "" : entry.message
|
|
7699
7701
|
};
|
|
7700
7702
|
}
|
|
7703
|
+
function guestAgentErrorMessage(text) {
|
|
7704
|
+
const trimmed = text.trim();
|
|
7705
|
+
if (!trimmed) return void 0;
|
|
7706
|
+
try {
|
|
7707
|
+
const parsed = JSON.parse(trimmed);
|
|
7708
|
+
if (typeof parsed.error === "string" && parsed.channel === void 0 && parsed.message === void 0) {
|
|
7709
|
+
return parsed.error;
|
|
7710
|
+
}
|
|
7711
|
+
} catch {
|
|
7712
|
+
return void 0;
|
|
7713
|
+
}
|
|
7714
|
+
return void 0;
|
|
7715
|
+
}
|
|
7716
|
+
function throwIfGuestAgentError(text) {
|
|
7717
|
+
const message = guestAgentErrorMessage(text);
|
|
7718
|
+
if (message !== void 0) {
|
|
7719
|
+
throw new LogAccessError({
|
|
7720
|
+
code: "ERR-17-009",
|
|
7721
|
+
message,
|
|
7722
|
+
fromStreamBody: true
|
|
7723
|
+
});
|
|
7724
|
+
}
|
|
7725
|
+
}
|
|
7701
7726
|
function parseStructuredLogLineToEntry(line) {
|
|
7702
7727
|
const trimmed = line.trim();
|
|
7703
7728
|
if (!trimmed) return null;
|
|
@@ -7707,6 +7732,13 @@ function parseStructuredLogLineToEntry(line) {
|
|
|
7707
7732
|
} catch {
|
|
7708
7733
|
throw new LogAccessError({ message: "Invalid NDJSON log record." });
|
|
7709
7734
|
}
|
|
7735
|
+
if (typeof parsed.error === "string" && parsed.channel === void 0 && parsed.message === void 0) {
|
|
7736
|
+
throw new LogAccessError({
|
|
7737
|
+
code: "ERR-17-009",
|
|
7738
|
+
message: parsed.error,
|
|
7739
|
+
fromStreamBody: true
|
|
7740
|
+
});
|
|
7741
|
+
}
|
|
7710
7742
|
if (typeof parsed.error_code === "string") {
|
|
7711
7743
|
throw new LogAccessError({
|
|
7712
7744
|
code: parsed.error_code,
|
|
@@ -7796,8 +7828,19 @@ async function getCvmLogEndpoint(client, id, channel) {
|
|
|
7796
7828
|
}
|
|
7797
7829
|
return endpoint;
|
|
7798
7830
|
}
|
|
7831
|
+
function containerLogsDisabledError() {
|
|
7832
|
+
return new LogAccessError({
|
|
7833
|
+
code: "ERR-17-006",
|
|
7834
|
+
message: "Container logs are disabled",
|
|
7835
|
+
httpStatus: 409
|
|
7836
|
+
});
|
|
7837
|
+
}
|
|
7799
7838
|
async function getContainerLogEndpoint(client, id, containerName, matchContainerIdPrefix = true) {
|
|
7800
7839
|
const cvmId = resolveCvmId(id);
|
|
7840
|
+
const info = await getCvmInfoLogs(client, cvmId);
|
|
7841
|
+
if (info.public_logs === false) {
|
|
7842
|
+
throw containerLogsDisabledError();
|
|
7843
|
+
}
|
|
7801
7844
|
const composition = await client.get(`/cvms/${cvmId}/composition`);
|
|
7802
7845
|
if (!composition.containers?.length) {
|
|
7803
7846
|
throw new Error(`No containers found for CVM '${cvmId}'`);
|
|
@@ -7845,9 +7888,11 @@ function getErrorHttpStatus(error) {
|
|
|
7845
7888
|
}
|
|
7846
7889
|
return void 0;
|
|
7847
7890
|
}
|
|
7848
|
-
|
|
7891
|
+
var EMPTY_STREAM_END_MESSAGE = "log stream keeps ending without new output; the container may have stopped";
|
|
7892
|
+
async function followLogRequests(resolveUrl, consume, signal, refreshCvm, trackEmptyEnds = false) {
|
|
7849
7893
|
let failureCount = 0;
|
|
7850
7894
|
let expiredRetries = 0;
|
|
7895
|
+
let emptyEnds = 0;
|
|
7851
7896
|
while (!signal?.aborted) {
|
|
7852
7897
|
try {
|
|
7853
7898
|
const url = await resolveUrl();
|
|
@@ -7855,14 +7900,31 @@ async function followLogRequests(resolveUrl, consume, signal, refreshCvm) {
|
|
|
7855
7900
|
const response = await fetch(url, { signal });
|
|
7856
7901
|
if (!response.ok) await throwResponseError(response);
|
|
7857
7902
|
failureCount = 0;
|
|
7858
|
-
await consume(response);
|
|
7903
|
+
const delivered = await consume(response) ?? true;
|
|
7859
7904
|
expiredRetries = 0;
|
|
7860
7905
|
if (signal?.aborted) return;
|
|
7906
|
+
if (trackEmptyEnds) {
|
|
7907
|
+
if (delivered) {
|
|
7908
|
+
emptyEnds = 0;
|
|
7909
|
+
} else {
|
|
7910
|
+
emptyEnds += 1;
|
|
7911
|
+
if (emptyEnds >= 5) {
|
|
7912
|
+
throw new LogAccessError({
|
|
7913
|
+
message: EMPTY_STREAM_END_MESSAGE,
|
|
7914
|
+
fromStreamBody: true
|
|
7915
|
+
});
|
|
7916
|
+
}
|
|
7917
|
+
await waitForReconnect(signal, Math.min(250 * 2 ** (emptyEnds - 1), 5e3));
|
|
7918
|
+
}
|
|
7919
|
+
}
|
|
7861
7920
|
} catch (error) {
|
|
7862
7921
|
if (signal?.aborted || error instanceof Error && error.name === "AbortError") {
|
|
7863
7922
|
return;
|
|
7864
7923
|
}
|
|
7865
7924
|
let retryError = error;
|
|
7925
|
+
if (error instanceof LogAccessError && error.fromStreamBody) {
|
|
7926
|
+
throw error;
|
|
7927
|
+
}
|
|
7866
7928
|
if (error instanceof LogAccessError && error.code === "ERR-17-003") {
|
|
7867
7929
|
expiredRetries += 1;
|
|
7868
7930
|
if (expiredRetries > 3) throw error;
|
|
@@ -7944,6 +8006,27 @@ async function fetchContainerLogsEntries(client, id, options = {}) {
|
|
|
7944
8006
|
return parseStructuredLogsToEntries(await response.text());
|
|
7945
8007
|
}
|
|
7946
8008
|
async function fetchContainerLogs(client, id, options = {}) {
|
|
8009
|
+
if (options.format === "text") {
|
|
8010
|
+
const times = resolveLogTimeWindow(options.since, options.until);
|
|
8011
|
+
const baseUrl = await getContainerLogEndpoint(
|
|
8012
|
+
client,
|
|
8013
|
+
id,
|
|
8014
|
+
options.container,
|
|
8015
|
+
options.matchContainerIdPrefix ?? true
|
|
8016
|
+
);
|
|
8017
|
+
const url = buildLogUrl(baseUrl, {
|
|
8018
|
+
...times,
|
|
8019
|
+
tail: options.tail,
|
|
8020
|
+
timestamps: options.timestamps,
|
|
8021
|
+
ansi: options.ansi,
|
|
8022
|
+
format: "text"
|
|
8023
|
+
});
|
|
8024
|
+
const response = await fetch(url, { signal: options.signal });
|
|
8025
|
+
if (!response.ok) await throwResponseError(response);
|
|
8026
|
+
const text = await response.text();
|
|
8027
|
+
throwIfGuestAgentError(text);
|
|
8028
|
+
return text;
|
|
8029
|
+
}
|
|
7947
8030
|
const entries = await fetchContainerLogsEntries(client, id, {
|
|
7948
8031
|
...options,
|
|
7949
8032
|
format: "ndjson"
|
|
@@ -7977,38 +8060,115 @@ async function streamContainerLogsEntries(client, id, onEntry, options = {}, sig
|
|
|
7977
8060
|
};
|
|
7978
8061
|
await followLogRequests(
|
|
7979
8062
|
buildRequestUrl,
|
|
7980
|
-
|
|
7981
|
-
|
|
7982
|
-
|
|
7983
|
-
|
|
7984
|
-
|
|
7985
|
-
|
|
7986
|
-
|
|
7987
|
-
if (cursor.boundarySecond !== void 0 && timestamp.second < cursor.boundarySecond) {
|
|
7988
|
-
onEntry(formatFollowContainerEntry(entry, options.timestamps ?? false));
|
|
7989
|
-
return;
|
|
7990
|
-
}
|
|
7991
|
-
if (timestamp.second === cursor.boundarySecond) {
|
|
7992
|
-
const replayCount = replayCounts.get(key) ?? 0;
|
|
7993
|
-
if (replayCount > 0) {
|
|
7994
|
-
replayCounts.set(key, replayCount - 1);
|
|
8063
|
+
async (response) => {
|
|
8064
|
+
let delivered = false;
|
|
8065
|
+
await streamNdjsonEntriesResponse(response, (entry) => {
|
|
8066
|
+
const timestamp = parseTimestampedMessage(entry.message);
|
|
8067
|
+
if (timestamp.second === void 0 || !timestamp.timestamp) {
|
|
8068
|
+
delivered = true;
|
|
8069
|
+
onEntry(formatFollowContainerEntry(entry, options.timestamps ?? false));
|
|
7995
8070
|
return;
|
|
7996
8071
|
}
|
|
7997
|
-
|
|
7998
|
-
cursor.boundarySecond
|
|
7999
|
-
|
|
8000
|
-
|
|
8001
|
-
|
|
8002
|
-
|
|
8003
|
-
|
|
8004
|
-
|
|
8072
|
+
const key = JSON.stringify([entry.channel, entry.message]);
|
|
8073
|
+
if (cursor.boundarySecond !== void 0 && timestamp.second < cursor.boundarySecond) {
|
|
8074
|
+
delivered = true;
|
|
8075
|
+
onEntry(formatFollowContainerEntry(entry, options.timestamps ?? false));
|
|
8076
|
+
return;
|
|
8077
|
+
}
|
|
8078
|
+
if (timestamp.second === cursor.boundarySecond) {
|
|
8079
|
+
const replayCount = replayCounts.get(key) ?? 0;
|
|
8080
|
+
if (replayCount > 0) {
|
|
8081
|
+
replayCounts.set(key, replayCount - 1);
|
|
8082
|
+
return;
|
|
8083
|
+
}
|
|
8084
|
+
} else if (cursor.boundarySecond === void 0 || timestamp.second > cursor.boundarySecond) {
|
|
8085
|
+
cursor.boundarySecond = timestamp.second;
|
|
8086
|
+
cursor.boundaryCounts.clear();
|
|
8087
|
+
}
|
|
8088
|
+
cursor.since = Math.max(cursor.since ?? timestamp.second, timestamp.second);
|
|
8089
|
+
cursor.boundaryCounts.set(key, (cursor.boundaryCounts.get(key) ?? 0) + 1);
|
|
8090
|
+
delivered = true;
|
|
8091
|
+
onEntry(formatFollowContainerEntry(entry, options.timestamps ?? false));
|
|
8092
|
+
});
|
|
8093
|
+
return delivered;
|
|
8094
|
+
},
|
|
8005
8095
|
abort,
|
|
8006
8096
|
async () => {
|
|
8007
8097
|
await getCvmInfoLogs(client, resolveCvmId(id));
|
|
8008
|
-
}
|
|
8098
|
+
},
|
|
8099
|
+
true
|
|
8009
8100
|
);
|
|
8010
8101
|
}
|
|
8102
|
+
async function streamContainerText(response, onData) {
|
|
8103
|
+
const reader = response.body?.getReader();
|
|
8104
|
+
if (!reader) throw new Error("No response body available for streaming");
|
|
8105
|
+
const decoder = new TextDecoder();
|
|
8106
|
+
let buffer = "";
|
|
8107
|
+
let finished = false;
|
|
8108
|
+
let delivered = false;
|
|
8109
|
+
try {
|
|
8110
|
+
while (true) {
|
|
8111
|
+
const { done, value } = await reader.read();
|
|
8112
|
+
buffer += decoder.decode(value, { stream: !done });
|
|
8113
|
+
let newline = buffer.indexOf("\n");
|
|
8114
|
+
while (newline >= 0) {
|
|
8115
|
+
const line = buffer.slice(0, newline);
|
|
8116
|
+
buffer = buffer.slice(newline + 1);
|
|
8117
|
+
throwIfGuestAgentError(line);
|
|
8118
|
+
onData(line + "\n");
|
|
8119
|
+
delivered = true;
|
|
8120
|
+
newline = buffer.indexOf("\n");
|
|
8121
|
+
}
|
|
8122
|
+
if (done) {
|
|
8123
|
+
finished = true;
|
|
8124
|
+
break;
|
|
8125
|
+
}
|
|
8126
|
+
}
|
|
8127
|
+
if (buffer) {
|
|
8128
|
+
throwIfGuestAgentError(buffer);
|
|
8129
|
+
onData(buffer);
|
|
8130
|
+
delivered = true;
|
|
8131
|
+
}
|
|
8132
|
+
} finally {
|
|
8133
|
+
if (!finished) await reader.cancel().catch(() => {
|
|
8134
|
+
});
|
|
8135
|
+
reader.releaseLock();
|
|
8136
|
+
}
|
|
8137
|
+
return delivered;
|
|
8138
|
+
}
|
|
8011
8139
|
async function streamContainerLogs(client, id, onData, options = {}, signal) {
|
|
8140
|
+
if (options.format === "text") {
|
|
8141
|
+
const abort = signal ?? options.signal;
|
|
8142
|
+
const times = resolveLogTimeWindow(options.since, options.until);
|
|
8143
|
+
let firstRequest = true;
|
|
8144
|
+
await followLogRequests(
|
|
8145
|
+
async () => {
|
|
8146
|
+
const baseUrl = await getContainerLogEndpoint(
|
|
8147
|
+
client,
|
|
8148
|
+
id,
|
|
8149
|
+
options.container,
|
|
8150
|
+
options.matchContainerIdPrefix ?? true
|
|
8151
|
+
);
|
|
8152
|
+
const tail = firstRequest ? options.tail : void 0;
|
|
8153
|
+
firstRequest = false;
|
|
8154
|
+
return buildLogUrl(baseUrl, {
|
|
8155
|
+
...times,
|
|
8156
|
+
tail,
|
|
8157
|
+
follow: true,
|
|
8158
|
+
timestamps: options.timestamps,
|
|
8159
|
+
ansi: options.ansi,
|
|
8160
|
+
format: "text"
|
|
8161
|
+
});
|
|
8162
|
+
},
|
|
8163
|
+
(response) => streamContainerText(response, onData),
|
|
8164
|
+
abort,
|
|
8165
|
+
async () => {
|
|
8166
|
+
await getCvmInfoLogs(client, resolveCvmId(id));
|
|
8167
|
+
},
|
|
8168
|
+
true
|
|
8169
|
+
);
|
|
8170
|
+
return;
|
|
8171
|
+
}
|
|
8012
8172
|
return streamContainerLogsEntries(
|
|
8013
8173
|
client,
|
|
8014
8174
|
id,
|
|
@@ -8043,7 +8203,17 @@ async function* followCvmLogs(client, id, source, options = {}) {
|
|
|
8043
8203
|
pending.push(item);
|
|
8044
8204
|
wake?.();
|
|
8045
8205
|
};
|
|
8046
|
-
const run = source.type === "node" ? streamCvmChannelLogs(client, id, source.channel, push, options, options.signal) :
|
|
8206
|
+
const run = source.type === "node" ? streamCvmChannelLogs(client, id, source.channel, push, options, options.signal) : options.format === "text" ? streamContainerLogs(
|
|
8207
|
+
client,
|
|
8208
|
+
id,
|
|
8209
|
+
(data) => push(data),
|
|
8210
|
+
{
|
|
8211
|
+
...options,
|
|
8212
|
+
container: source.name,
|
|
8213
|
+
matchContainerIdPrefix: source.matchContainerIdPrefix
|
|
8214
|
+
},
|
|
8215
|
+
options.signal
|
|
8216
|
+
) : streamContainerLogsEntries(
|
|
8047
8217
|
client,
|
|
8048
8218
|
id,
|
|
8049
8219
|
push,
|
|
@@ -150,6 +150,7 @@ export declare const CvmDetailV20251028Schema: z.ZodObject<{
|
|
|
150
150
|
instance_type: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
151
151
|
public_sysinfo: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
152
152
|
public_logs: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
153
|
+
public_tcbinfo: z.ZodOptional<z.ZodBoolean>;
|
|
153
154
|
managed_env: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
154
155
|
dapp_dashboard_url: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
155
156
|
syslog_endpoint: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|