@phala/cloud 0.6.1 → 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.
@@ -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.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,
@@ -8353,9 +8385,11 @@ function getErrorHttpStatus(error) {
8353
8385
  }
8354
8386
  return void 0;
8355
8387
  }
8356
- async function followLogRequests(resolveUrl, consume, signal, refreshCvm) {
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) {
8357
8390
  let failureCount = 0;
8358
8391
  let expiredRetries = 0;
8392
+ let emptyEnds = 0;
8359
8393
  while (!signal?.aborted) {
8360
8394
  try {
8361
8395
  const url = await resolveUrl();
@@ -8363,14 +8397,31 @@ async function followLogRequests(resolveUrl, consume, signal, refreshCvm) {
8363
8397
  const response = await fetch(url, { signal });
8364
8398
  if (!response.ok) await throwResponseError(response);
8365
8399
  failureCount = 0;
8366
- await consume(response);
8400
+ const delivered = await consume(response) ?? true;
8367
8401
  expiredRetries = 0;
8368
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
+ }
8369
8417
  } catch (error) {
8370
8418
  if (signal?.aborted || error instanceof Error && error.name === "AbortError") {
8371
8419
  return;
8372
8420
  }
8373
8421
  let retryError = error;
8422
+ if (error instanceof LogAccessError && error.fromStreamBody) {
8423
+ throw error;
8424
+ }
8374
8425
  if (error instanceof LogAccessError && error.code === "ERR-17-003") {
8375
8426
  expiredRetries += 1;
8376
8427
  if (expiredRetries > 3) throw error;
@@ -8452,6 +8503,27 @@ async function fetchContainerLogsEntries(client, id, options = {}) {
8452
8503
  return parseStructuredLogsToEntries(await response.text());
8453
8504
  }
8454
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
+ }
8455
8527
  const entries = await fetchContainerLogsEntries(client, id, {
8456
8528
  ...options,
8457
8529
  format: "ndjson"
@@ -8485,38 +8557,115 @@ async function streamContainerLogsEntries(client, id, onEntry, options = {}, sig
8485
8557
  };
8486
8558
  await followLogRequests(
8487
8559
  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);
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));
8503
8567
  return;
8504
8568
  }
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
- }),
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
+ },
8513
8592
  abort,
8514
8593
  async () => {
8515
8594
  await getCvmInfoLogs(client, resolveCvmId(id));
8516
- }
8595
+ },
8596
+ true
8517
8597
  );
8518
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
+ }
8519
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
+ }
8520
8669
  return streamContainerLogsEntries(
8521
8670
  client,
8522
8671
  id,
@@ -8551,7 +8700,17 @@ async function* followCvmLogs(client, id, source, options = {}) {
8551
8700
  pending.push(item);
8552
8701
  wake?.();
8553
8702
  };
8554
- const run = source.type === "node" ? streamCvmChannelLogs(client, id, source.channel, push, options, options.signal) : streamContainerLogsEntries(
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(
8555
8714
  client,
8556
8715
  id,
8557
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.1";
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,
@@ -7856,9 +7888,11 @@ function getErrorHttpStatus(error) {
7856
7888
  }
7857
7889
  return void 0;
7858
7890
  }
7859
- async function followLogRequests(resolveUrl, consume, signal, refreshCvm) {
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) {
7860
7893
  let failureCount = 0;
7861
7894
  let expiredRetries = 0;
7895
+ let emptyEnds = 0;
7862
7896
  while (!signal?.aborted) {
7863
7897
  try {
7864
7898
  const url = await resolveUrl();
@@ -7866,14 +7900,31 @@ async function followLogRequests(resolveUrl, consume, signal, refreshCvm) {
7866
7900
  const response = await fetch(url, { signal });
7867
7901
  if (!response.ok) await throwResponseError(response);
7868
7902
  failureCount = 0;
7869
- await consume(response);
7903
+ const delivered = await consume(response) ?? true;
7870
7904
  expiredRetries = 0;
7871
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
+ }
7872
7920
  } catch (error) {
7873
7921
  if (signal?.aborted || error instanceof Error && error.name === "AbortError") {
7874
7922
  return;
7875
7923
  }
7876
7924
  let retryError = error;
7925
+ if (error instanceof LogAccessError && error.fromStreamBody) {
7926
+ throw error;
7927
+ }
7877
7928
  if (error instanceof LogAccessError && error.code === "ERR-17-003") {
7878
7929
  expiredRetries += 1;
7879
7930
  if (expiredRetries > 3) throw error;
@@ -7955,6 +8006,27 @@ async function fetchContainerLogsEntries(client, id, options = {}) {
7955
8006
  return parseStructuredLogsToEntries(await response.text());
7956
8007
  }
7957
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
+ }
7958
8030
  const entries = await fetchContainerLogsEntries(client, id, {
7959
8031
  ...options,
7960
8032
  format: "ndjson"
@@ -7988,38 +8060,115 @@ async function streamContainerLogsEntries(client, id, onEntry, options = {}, sig
7988
8060
  };
7989
8061
  await followLogRequests(
7990
8062
  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);
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));
8006
8070
  return;
8007
8071
  }
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
- }),
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
+ },
8016
8095
  abort,
8017
8096
  async () => {
8018
8097
  await getCvmInfoLogs(client, resolveCvmId(id));
8019
- }
8098
+ },
8099
+ true
8020
8100
  );
8021
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
+ }
8022
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
+ }
8023
8172
  return streamContainerLogsEntries(
8024
8173
  client,
8025
8174
  id,
@@ -8054,7 +8203,17 @@ async function* followCvmLogs(client, id, source, options = {}) {
8054
8203
  pending.push(item);
8055
8204
  wake?.();
8056
8205
  };
8057
- const run = source.type === "node" ? streamCvmChannelLogs(client, id, source.channel, push, options, options.signal) : streamContainerLogsEntries(
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(
8058
8217
  client,
8059
8218
  id,
8060
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>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@phala/cloud",
3
- "version": "0.6.1",
3
+ "version": "0.6.2",
4
4
  "description": "TypeScript SDK for Phala Cloud API",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",