@phala/cloud 0.6.1 → 0.6.3

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.
@@ -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.1";
972
+ const product = "phala-cloud-sdk-js/0.6.3";
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. */
@@ -8084,11 +8085,11 @@ function logErrorHttpStatus(code) {
8084
8085
  }
8085
8086
  var LogAccessError = class extends ResourceError {
8086
8087
  constructor(options) {
8087
- const details = options.code ? `${options.code}: ${options.message}` : options.message;
8088
+ const details = options.message;
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,
@@ -8300,12 +8332,14 @@ function containerLogsDisabledError() {
8300
8332
  httpStatus: 409
8301
8333
  });
8302
8334
  }
8303
- async function getContainerLogEndpoint(client, id, containerName, matchContainerIdPrefix = true) {
8304
- const cvmId = resolveCvmId(id);
8305
- const info = await getCvmInfoLogs(client, cvmId);
8335
+ async function assertContainerLogsEnabled(client, id) {
8336
+ const info = await getCvmInfoLogs(client, resolveCvmId(id));
8306
8337
  if (info.public_logs === false) {
8307
8338
  throw containerLogsDisabledError();
8308
8339
  }
8340
+ }
8341
+ async function getContainerLogEndpoint(client, id, containerName, matchContainerIdPrefix = true) {
8342
+ const cvmId = resolveCvmId(id);
8309
8343
  const composition = await client.get(`/cvms/${cvmId}/composition`);
8310
8344
  if (!composition.containers?.length) {
8311
8345
  throw new Error(`No containers found for CVM '${cvmId}'`);
@@ -8353,9 +8387,11 @@ function getErrorHttpStatus(error) {
8353
8387
  }
8354
8388
  return void 0;
8355
8389
  }
8356
- async function followLogRequests(resolveUrl, consume, signal, refreshCvm) {
8390
+ var EMPTY_STREAM_END_MESSAGE = "log stream keeps ending without new output; the container may have stopped";
8391
+ async function followLogRequests(resolveUrl, consume, signal, refreshCvm, trackEmptyEnds = false) {
8357
8392
  let failureCount = 0;
8358
8393
  let expiredRetries = 0;
8394
+ let emptyEnds = 0;
8359
8395
  while (!signal?.aborted) {
8360
8396
  try {
8361
8397
  const url = await resolveUrl();
@@ -8363,14 +8399,31 @@ async function followLogRequests(resolveUrl, consume, signal, refreshCvm) {
8363
8399
  const response = await fetch(url, { signal });
8364
8400
  if (!response.ok) await throwResponseError(response);
8365
8401
  failureCount = 0;
8366
- await consume(response);
8402
+ const delivered = await consume(response) ?? true;
8367
8403
  expiredRetries = 0;
8368
8404
  if (signal?.aborted) return;
8405
+ if (trackEmptyEnds) {
8406
+ if (delivered) {
8407
+ emptyEnds = 0;
8408
+ } else {
8409
+ emptyEnds += 1;
8410
+ if (emptyEnds >= 5) {
8411
+ throw new LogAccessError({
8412
+ message: EMPTY_STREAM_END_MESSAGE,
8413
+ fromStreamBody: true
8414
+ });
8415
+ }
8416
+ await waitForReconnect(signal, Math.min(250 * 2 ** (emptyEnds - 1), 5e3));
8417
+ }
8418
+ }
8369
8419
  } catch (error) {
8370
8420
  if (signal?.aborted || error instanceof Error && error.name === "AbortError") {
8371
8421
  return;
8372
8422
  }
8373
8423
  let retryError = error;
8424
+ if (error instanceof LogAccessError && error.fromStreamBody) {
8425
+ throw error;
8426
+ }
8374
8427
  if (error instanceof LogAccessError && error.code === "ERR-17-003") {
8375
8428
  expiredRetries += 1;
8376
8429
  if (expiredRetries > 3) throw error;
@@ -8386,6 +8439,9 @@ async function followLogRequests(resolveUrl, consume, signal, refreshCvm) {
8386
8439
  if (retryError instanceof LogAccessError && status === void 0) {
8387
8440
  throw retryError;
8388
8441
  }
8442
+ if (status === void 0 && !(retryError instanceof TypeError)) {
8443
+ throw retryError;
8444
+ }
8389
8445
  failureCount += 1;
8390
8446
  if (failureCount >= 5) throw retryError;
8391
8447
  const delayMs = Math.min(250 * 2 ** (failureCount - 1), 5e3);
@@ -8434,6 +8490,7 @@ async function streamCvmChannelLogs(client, id, channel, onData, options = {}, s
8434
8490
  }
8435
8491
  async function fetchContainerLogsEntries(client, id, options = {}) {
8436
8492
  const times = resolveLogTimeWindow(options.since, options.until);
8493
+ await assertContainerLogsEnabled(client, id);
8437
8494
  const baseUrl = await getContainerLogEndpoint(
8438
8495
  client,
8439
8496
  id,
@@ -8452,6 +8509,28 @@ async function fetchContainerLogsEntries(client, id, options = {}) {
8452
8509
  return parseStructuredLogsToEntries(await response.text());
8453
8510
  }
8454
8511
  async function fetchContainerLogs(client, id, options = {}) {
8512
+ if (options.format === "text") {
8513
+ const times = resolveLogTimeWindow(options.since, options.until);
8514
+ await assertContainerLogsEnabled(client, id);
8515
+ const baseUrl = await getContainerLogEndpoint(
8516
+ client,
8517
+ id,
8518
+ options.container,
8519
+ options.matchContainerIdPrefix ?? true
8520
+ );
8521
+ const url = buildLogUrl(baseUrl, {
8522
+ ...times,
8523
+ tail: options.tail,
8524
+ timestamps: options.timestamps,
8525
+ ansi: options.ansi,
8526
+ format: "text"
8527
+ });
8528
+ const response = await fetch(url, { signal: options.signal });
8529
+ if (!response.ok) await throwResponseError(response);
8530
+ const text = await response.text();
8531
+ throwIfGuestAgentError(text);
8532
+ return text;
8533
+ }
8455
8534
  const entries = await fetchContainerLogsEntries(client, id, {
8456
8535
  ...options,
8457
8536
  format: "ndjson"
@@ -8461,6 +8540,7 @@ async function fetchContainerLogs(client, id, options = {}) {
8461
8540
  async function streamContainerLogsEntries(client, id, onEntry, options = {}, signal) {
8462
8541
  const abort = signal ?? options.signal;
8463
8542
  const times = resolveLogTimeWindow(options.since, options.until);
8543
+ await assertContainerLogsEnabled(client, id);
8464
8544
  const cursor = { since: times.since, boundaryCounts: /* @__PURE__ */ new Map() };
8465
8545
  let replayCounts = /* @__PURE__ */ new Map();
8466
8546
  let firstRequest = true;
@@ -8485,38 +8565,116 @@ async function streamContainerLogsEntries(client, id, onEntry, options = {}, sig
8485
8565
  };
8486
8566
  await followLogRequests(
8487
8567
  buildRequestUrl,
8488
- (response) => streamNdjsonEntriesResponse(response, (entry) => {
8489
- const timestamp = parseTimestampedMessage(entry.message);
8490
- if (timestamp.second === void 0 || !timestamp.timestamp) {
8491
- onEntry(formatFollowContainerEntry(entry, options.timestamps ?? false));
8492
- return;
8493
- }
8494
- const key = JSON.stringify([entry.channel, entry.message]);
8495
- if (cursor.boundarySecond !== void 0 && timestamp.second < cursor.boundarySecond) {
8496
- onEntry(formatFollowContainerEntry(entry, options.timestamps ?? false));
8497
- return;
8498
- }
8499
- if (timestamp.second === cursor.boundarySecond) {
8500
- const replayCount = replayCounts.get(key) ?? 0;
8501
- if (replayCount > 0) {
8502
- replayCounts.set(key, replayCount - 1);
8568
+ async (response) => {
8569
+ let delivered = false;
8570
+ await streamNdjsonEntriesResponse(response, (entry) => {
8571
+ const timestamp = parseTimestampedMessage(entry.message);
8572
+ if (timestamp.second === void 0 || !timestamp.timestamp) {
8573
+ delivered = true;
8574
+ onEntry(formatFollowContainerEntry(entry, options.timestamps ?? false));
8503
8575
  return;
8504
8576
  }
8505
- } else if (cursor.boundarySecond === void 0 || timestamp.second > cursor.boundarySecond) {
8506
- cursor.boundarySecond = timestamp.second;
8507
- cursor.boundaryCounts.clear();
8508
- }
8509
- cursor.since = Math.max(cursor.since ?? timestamp.second, timestamp.second);
8510
- cursor.boundaryCounts.set(key, (cursor.boundaryCounts.get(key) ?? 0) + 1);
8511
- onEntry(formatFollowContainerEntry(entry, options.timestamps ?? false));
8512
- }),
8577
+ const key = JSON.stringify([entry.channel, entry.message]);
8578
+ if (cursor.boundarySecond !== void 0 && timestamp.second < cursor.boundarySecond) {
8579
+ delivered = true;
8580
+ onEntry(formatFollowContainerEntry(entry, options.timestamps ?? false));
8581
+ return;
8582
+ }
8583
+ if (timestamp.second === cursor.boundarySecond) {
8584
+ const replayCount = replayCounts.get(key) ?? 0;
8585
+ if (replayCount > 0) {
8586
+ replayCounts.set(key, replayCount - 1);
8587
+ return;
8588
+ }
8589
+ } else if (cursor.boundarySecond === void 0 || timestamp.second > cursor.boundarySecond) {
8590
+ cursor.boundarySecond = timestamp.second;
8591
+ cursor.boundaryCounts.clear();
8592
+ }
8593
+ cursor.since = Math.max(cursor.since ?? timestamp.second, timestamp.second);
8594
+ cursor.boundaryCounts.set(key, (cursor.boundaryCounts.get(key) ?? 0) + 1);
8595
+ delivered = true;
8596
+ onEntry(formatFollowContainerEntry(entry, options.timestamps ?? false));
8597
+ });
8598
+ return delivered;
8599
+ },
8513
8600
  abort,
8514
8601
  async () => {
8515
- await getCvmInfoLogs(client, resolveCvmId(id));
8516
- }
8602
+ await assertContainerLogsEnabled(client, id);
8603
+ },
8604
+ true
8517
8605
  );
8518
8606
  }
8607
+ async function streamContainerText(response, onData) {
8608
+ const reader = response.body?.getReader();
8609
+ if (!reader) throw new Error("No response body available for streaming");
8610
+ const decoder = new TextDecoder();
8611
+ let buffer = "";
8612
+ let finished = false;
8613
+ let delivered = false;
8614
+ try {
8615
+ while (true) {
8616
+ const { done, value } = await reader.read();
8617
+ buffer += decoder.decode(value, { stream: !done });
8618
+ let newline = buffer.indexOf("\n");
8619
+ while (newline >= 0) {
8620
+ const line = buffer.slice(0, newline);
8621
+ buffer = buffer.slice(newline + 1);
8622
+ throwIfGuestAgentError(line);
8623
+ onData(line + "\n");
8624
+ delivered = true;
8625
+ newline = buffer.indexOf("\n");
8626
+ }
8627
+ if (done) {
8628
+ finished = true;
8629
+ break;
8630
+ }
8631
+ }
8632
+ if (buffer) {
8633
+ throwIfGuestAgentError(buffer);
8634
+ onData(buffer);
8635
+ delivered = true;
8636
+ }
8637
+ } finally {
8638
+ if (!finished) await reader.cancel().catch(() => {
8639
+ });
8640
+ reader.releaseLock();
8641
+ }
8642
+ return delivered;
8643
+ }
8519
8644
  async function streamContainerLogs(client, id, onData, options = {}, signal) {
8645
+ if (options.format === "text") {
8646
+ const abort = signal ?? options.signal;
8647
+ const times = resolveLogTimeWindow(options.since, options.until);
8648
+ await assertContainerLogsEnabled(client, id);
8649
+ let firstRequest = true;
8650
+ await followLogRequests(
8651
+ async () => {
8652
+ const baseUrl = await getContainerLogEndpoint(
8653
+ client,
8654
+ id,
8655
+ options.container,
8656
+ options.matchContainerIdPrefix ?? true
8657
+ );
8658
+ const tail = firstRequest ? options.tail : void 0;
8659
+ firstRequest = false;
8660
+ return buildLogUrl(baseUrl, {
8661
+ ...times,
8662
+ tail,
8663
+ follow: true,
8664
+ timestamps: options.timestamps,
8665
+ ansi: options.ansi,
8666
+ format: "text"
8667
+ });
8668
+ },
8669
+ (response) => streamContainerText(response, onData),
8670
+ abort,
8671
+ async () => {
8672
+ await assertContainerLogsEnabled(client, id);
8673
+ },
8674
+ true
8675
+ );
8676
+ return;
8677
+ }
8520
8678
  return streamContainerLogsEntries(
8521
8679
  client,
8522
8680
  id,
@@ -8551,7 +8709,17 @@ async function* followCvmLogs(client, id, source, options = {}) {
8551
8709
  pending.push(item);
8552
8710
  wake?.();
8553
8711
  };
8554
- const run = source.type === "node" ? streamCvmChannelLogs(client, id, source.channel, push, options, options.signal) : streamContainerLogsEntries(
8712
+ const run = source.type === "node" ? streamCvmChannelLogs(client, id, source.channel, push, options, options.signal) : options.format === "text" ? streamContainerLogs(
8713
+ client,
8714
+ id,
8715
+ (data) => push(data),
8716
+ {
8717
+ ...options,
8718
+ container: source.name,
8719
+ matchContainerIdPrefix: source.matchContainerIdPrefix
8720
+ },
8721
+ options.signal
8722
+ ) : streamContainerLogsEntries(
8555
8723
  client,
8556
8724
  id,
8557
8725
  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.1";
443
+ const product = "phala-cloud-sdk-js/0.6.3";
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. */
@@ -7587,11 +7588,11 @@ function logErrorHttpStatus(code) {
7587
7588
  }
7588
7589
  var LogAccessError = class extends ResourceError {
7589
7590
  constructor(options) {
7590
- const details = options.code ? `${options.code}: ${options.message}` : options.message;
7591
+ const details = options.message;
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,
@@ -7803,12 +7835,14 @@ function containerLogsDisabledError() {
7803
7835
  httpStatus: 409
7804
7836
  });
7805
7837
  }
7806
- async function getContainerLogEndpoint(client, id, containerName, matchContainerIdPrefix = true) {
7807
- const cvmId = resolveCvmId(id);
7808
- const info = await getCvmInfoLogs(client, cvmId);
7838
+ async function assertContainerLogsEnabled(client, id) {
7839
+ const info = await getCvmInfoLogs(client, resolveCvmId(id));
7809
7840
  if (info.public_logs === false) {
7810
7841
  throw containerLogsDisabledError();
7811
7842
  }
7843
+ }
7844
+ async function getContainerLogEndpoint(client, id, containerName, matchContainerIdPrefix = true) {
7845
+ const cvmId = resolveCvmId(id);
7812
7846
  const composition = await client.get(`/cvms/${cvmId}/composition`);
7813
7847
  if (!composition.containers?.length) {
7814
7848
  throw new Error(`No containers found for CVM '${cvmId}'`);
@@ -7856,9 +7890,11 @@ function getErrorHttpStatus(error) {
7856
7890
  }
7857
7891
  return void 0;
7858
7892
  }
7859
- async function followLogRequests(resolveUrl, consume, signal, refreshCvm) {
7893
+ var EMPTY_STREAM_END_MESSAGE = "log stream keeps ending without new output; the container may have stopped";
7894
+ async function followLogRequests(resolveUrl, consume, signal, refreshCvm, trackEmptyEnds = false) {
7860
7895
  let failureCount = 0;
7861
7896
  let expiredRetries = 0;
7897
+ let emptyEnds = 0;
7862
7898
  while (!signal?.aborted) {
7863
7899
  try {
7864
7900
  const url = await resolveUrl();
@@ -7866,14 +7902,31 @@ async function followLogRequests(resolveUrl, consume, signal, refreshCvm) {
7866
7902
  const response = await fetch(url, { signal });
7867
7903
  if (!response.ok) await throwResponseError(response);
7868
7904
  failureCount = 0;
7869
- await consume(response);
7905
+ const delivered = await consume(response) ?? true;
7870
7906
  expiredRetries = 0;
7871
7907
  if (signal?.aborted) return;
7908
+ if (trackEmptyEnds) {
7909
+ if (delivered) {
7910
+ emptyEnds = 0;
7911
+ } else {
7912
+ emptyEnds += 1;
7913
+ if (emptyEnds >= 5) {
7914
+ throw new LogAccessError({
7915
+ message: EMPTY_STREAM_END_MESSAGE,
7916
+ fromStreamBody: true
7917
+ });
7918
+ }
7919
+ await waitForReconnect(signal, Math.min(250 * 2 ** (emptyEnds - 1), 5e3));
7920
+ }
7921
+ }
7872
7922
  } catch (error) {
7873
7923
  if (signal?.aborted || error instanceof Error && error.name === "AbortError") {
7874
7924
  return;
7875
7925
  }
7876
7926
  let retryError = error;
7927
+ if (error instanceof LogAccessError && error.fromStreamBody) {
7928
+ throw error;
7929
+ }
7877
7930
  if (error instanceof LogAccessError && error.code === "ERR-17-003") {
7878
7931
  expiredRetries += 1;
7879
7932
  if (expiredRetries > 3) throw error;
@@ -7889,6 +7942,9 @@ async function followLogRequests(resolveUrl, consume, signal, refreshCvm) {
7889
7942
  if (retryError instanceof LogAccessError && status === void 0) {
7890
7943
  throw retryError;
7891
7944
  }
7945
+ if (status === void 0 && !(retryError instanceof TypeError)) {
7946
+ throw retryError;
7947
+ }
7892
7948
  failureCount += 1;
7893
7949
  if (failureCount >= 5) throw retryError;
7894
7950
  const delayMs = Math.min(250 * 2 ** (failureCount - 1), 5e3);
@@ -7937,6 +7993,7 @@ async function streamCvmChannelLogs(client, id, channel, onData, options = {}, s
7937
7993
  }
7938
7994
  async function fetchContainerLogsEntries(client, id, options = {}) {
7939
7995
  const times = resolveLogTimeWindow(options.since, options.until);
7996
+ await assertContainerLogsEnabled(client, id);
7940
7997
  const baseUrl = await getContainerLogEndpoint(
7941
7998
  client,
7942
7999
  id,
@@ -7955,6 +8012,28 @@ async function fetchContainerLogsEntries(client, id, options = {}) {
7955
8012
  return parseStructuredLogsToEntries(await response.text());
7956
8013
  }
7957
8014
  async function fetchContainerLogs(client, id, options = {}) {
8015
+ if (options.format === "text") {
8016
+ const times = resolveLogTimeWindow(options.since, options.until);
8017
+ await assertContainerLogsEnabled(client, id);
8018
+ const baseUrl = await getContainerLogEndpoint(
8019
+ client,
8020
+ id,
8021
+ options.container,
8022
+ options.matchContainerIdPrefix ?? true
8023
+ );
8024
+ const url = buildLogUrl(baseUrl, {
8025
+ ...times,
8026
+ tail: options.tail,
8027
+ timestamps: options.timestamps,
8028
+ ansi: options.ansi,
8029
+ format: "text"
8030
+ });
8031
+ const response = await fetch(url, { signal: options.signal });
8032
+ if (!response.ok) await throwResponseError(response);
8033
+ const text = await response.text();
8034
+ throwIfGuestAgentError(text);
8035
+ return text;
8036
+ }
7958
8037
  const entries = await fetchContainerLogsEntries(client, id, {
7959
8038
  ...options,
7960
8039
  format: "ndjson"
@@ -7964,6 +8043,7 @@ async function fetchContainerLogs(client, id, options = {}) {
7964
8043
  async function streamContainerLogsEntries(client, id, onEntry, options = {}, signal) {
7965
8044
  const abort = signal ?? options.signal;
7966
8045
  const times = resolveLogTimeWindow(options.since, options.until);
8046
+ await assertContainerLogsEnabled(client, id);
7967
8047
  const cursor = { since: times.since, boundaryCounts: /* @__PURE__ */ new Map() };
7968
8048
  let replayCounts = /* @__PURE__ */ new Map();
7969
8049
  let firstRequest = true;
@@ -7988,38 +8068,116 @@ async function streamContainerLogsEntries(client, id, onEntry, options = {}, sig
7988
8068
  };
7989
8069
  await followLogRequests(
7990
8070
  buildRequestUrl,
7991
- (response) => streamNdjsonEntriesResponse(response, (entry) => {
7992
- const timestamp = parseTimestampedMessage(entry.message);
7993
- if (timestamp.second === void 0 || !timestamp.timestamp) {
7994
- onEntry(formatFollowContainerEntry(entry, options.timestamps ?? false));
7995
- return;
7996
- }
7997
- const key = JSON.stringify([entry.channel, entry.message]);
7998
- if (cursor.boundarySecond !== void 0 && timestamp.second < cursor.boundarySecond) {
7999
- onEntry(formatFollowContainerEntry(entry, options.timestamps ?? false));
8000
- return;
8001
- }
8002
- if (timestamp.second === cursor.boundarySecond) {
8003
- const replayCount = replayCounts.get(key) ?? 0;
8004
- if (replayCount > 0) {
8005
- replayCounts.set(key, replayCount - 1);
8071
+ async (response) => {
8072
+ let delivered = false;
8073
+ await streamNdjsonEntriesResponse(response, (entry) => {
8074
+ const timestamp = parseTimestampedMessage(entry.message);
8075
+ if (timestamp.second === void 0 || !timestamp.timestamp) {
8076
+ delivered = true;
8077
+ onEntry(formatFollowContainerEntry(entry, options.timestamps ?? false));
8006
8078
  return;
8007
8079
  }
8008
- } else if (cursor.boundarySecond === void 0 || timestamp.second > cursor.boundarySecond) {
8009
- cursor.boundarySecond = timestamp.second;
8010
- cursor.boundaryCounts.clear();
8011
- }
8012
- cursor.since = Math.max(cursor.since ?? timestamp.second, timestamp.second);
8013
- cursor.boundaryCounts.set(key, (cursor.boundaryCounts.get(key) ?? 0) + 1);
8014
- onEntry(formatFollowContainerEntry(entry, options.timestamps ?? false));
8015
- }),
8080
+ const key = JSON.stringify([entry.channel, entry.message]);
8081
+ if (cursor.boundarySecond !== void 0 && timestamp.second < cursor.boundarySecond) {
8082
+ delivered = true;
8083
+ onEntry(formatFollowContainerEntry(entry, options.timestamps ?? false));
8084
+ return;
8085
+ }
8086
+ if (timestamp.second === cursor.boundarySecond) {
8087
+ const replayCount = replayCounts.get(key) ?? 0;
8088
+ if (replayCount > 0) {
8089
+ replayCounts.set(key, replayCount - 1);
8090
+ return;
8091
+ }
8092
+ } else if (cursor.boundarySecond === void 0 || timestamp.second > cursor.boundarySecond) {
8093
+ cursor.boundarySecond = timestamp.second;
8094
+ cursor.boundaryCounts.clear();
8095
+ }
8096
+ cursor.since = Math.max(cursor.since ?? timestamp.second, timestamp.second);
8097
+ cursor.boundaryCounts.set(key, (cursor.boundaryCounts.get(key) ?? 0) + 1);
8098
+ delivered = true;
8099
+ onEntry(formatFollowContainerEntry(entry, options.timestamps ?? false));
8100
+ });
8101
+ return delivered;
8102
+ },
8016
8103
  abort,
8017
8104
  async () => {
8018
- await getCvmInfoLogs(client, resolveCvmId(id));
8019
- }
8105
+ await assertContainerLogsEnabled(client, id);
8106
+ },
8107
+ true
8020
8108
  );
8021
8109
  }
8110
+ async function streamContainerText(response, onData) {
8111
+ const reader = response.body?.getReader();
8112
+ if (!reader) throw new Error("No response body available for streaming");
8113
+ const decoder = new TextDecoder();
8114
+ let buffer = "";
8115
+ let finished = false;
8116
+ let delivered = false;
8117
+ try {
8118
+ while (true) {
8119
+ const { done, value } = await reader.read();
8120
+ buffer += decoder.decode(value, { stream: !done });
8121
+ let newline = buffer.indexOf("\n");
8122
+ while (newline >= 0) {
8123
+ const line = buffer.slice(0, newline);
8124
+ buffer = buffer.slice(newline + 1);
8125
+ throwIfGuestAgentError(line);
8126
+ onData(line + "\n");
8127
+ delivered = true;
8128
+ newline = buffer.indexOf("\n");
8129
+ }
8130
+ if (done) {
8131
+ finished = true;
8132
+ break;
8133
+ }
8134
+ }
8135
+ if (buffer) {
8136
+ throwIfGuestAgentError(buffer);
8137
+ onData(buffer);
8138
+ delivered = true;
8139
+ }
8140
+ } finally {
8141
+ if (!finished) await reader.cancel().catch(() => {
8142
+ });
8143
+ reader.releaseLock();
8144
+ }
8145
+ return delivered;
8146
+ }
8022
8147
  async function streamContainerLogs(client, id, onData, options = {}, signal) {
8148
+ if (options.format === "text") {
8149
+ const abort = signal ?? options.signal;
8150
+ const times = resolveLogTimeWindow(options.since, options.until);
8151
+ await assertContainerLogsEnabled(client, id);
8152
+ let firstRequest = true;
8153
+ await followLogRequests(
8154
+ async () => {
8155
+ const baseUrl = await getContainerLogEndpoint(
8156
+ client,
8157
+ id,
8158
+ options.container,
8159
+ options.matchContainerIdPrefix ?? true
8160
+ );
8161
+ const tail = firstRequest ? options.tail : void 0;
8162
+ firstRequest = false;
8163
+ return buildLogUrl(baseUrl, {
8164
+ ...times,
8165
+ tail,
8166
+ follow: true,
8167
+ timestamps: options.timestamps,
8168
+ ansi: options.ansi,
8169
+ format: "text"
8170
+ });
8171
+ },
8172
+ (response) => streamContainerText(response, onData),
8173
+ abort,
8174
+ async () => {
8175
+ await assertContainerLogsEnabled(client, id);
8176
+ },
8177
+ true
8178
+ );
8179
+ return;
8180
+ }
8023
8181
  return streamContainerLogsEntries(
8024
8182
  client,
8025
8183
  id,
@@ -8054,7 +8212,17 @@ async function* followCvmLogs(client, id, source, options = {}) {
8054
8212
  pending.push(item);
8055
8213
  wake?.();
8056
8214
  };
8057
- const run = source.type === "node" ? streamCvmChannelLogs(client, id, source.channel, push, options, options.signal) : streamContainerLogsEntries(
8215
+ const run = source.type === "node" ? streamCvmChannelLogs(client, id, source.channel, push, options, options.signal) : options.format === "text" ? streamContainerLogs(
8216
+ client,
8217
+ id,
8218
+ (data) => push(data),
8219
+ {
8220
+ ...options,
8221
+ container: source.name,
8222
+ matchContainerIdPrefix: source.matchContainerIdPrefix
8223
+ },
8224
+ options.signal
8225
+ ) : streamContainerLogsEntries(
8058
8226
  client,
8059
8227
  id,
8060
8228
  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>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@phala/cloud",
3
- "version": "0.6.1",
3
+ "version": "0.6.3",
4
4
  "description": "TypeScript SDK for Phala Cloud API",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",