@insforge/instabox-sdk 0.6.0 → 0.7.1

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/index.d.cts CHANGED
@@ -248,6 +248,7 @@ interface MachineUsage {
248
248
  vcpuSeconds: number;
249
249
  memGiBSeconds: number;
250
250
  diskGiBSeconds: number;
251
+ egressBytes?: number;
251
252
  }
252
253
  /** One sandbox's share of `Usage.storedBytes`. */
253
254
  interface UsageMachineStorage {
@@ -298,7 +299,12 @@ interface UsageCost {
298
299
  storageGiBHours: number;
299
300
  /** Egress beyond the free amount. */
300
301
  egressGiB: number;
302
+ /** Compute: `vcpuUSD` plus `memUSD`. */
301
303
  runtimeUSD: number;
304
+ /** The vCPU part of compute. Absent from older servers. */
305
+ vcpuUSD?: number;
306
+ /** The memory part of compute. Absent from older servers. */
307
+ memUSD?: number;
302
308
  storageUSD: number;
303
309
  egressUSD: number;
304
310
  /** The month's usage so far. */
@@ -381,6 +387,11 @@ interface RequestOptions {
381
387
  body?: unknown;
382
388
  /** A raw request body, sent as `application/octet-stream` instead of JSON. */
383
389
  rawBody?: Uint8Array;
390
+ /**
391
+ * A streamed request body, sent as `application/octet-stream` with `duplex: 'half'` (chunked when no
392
+ * length is known). A stream can be read once, so such a request is never retried.
393
+ */
394
+ streamBody?: ReadableStream<Uint8Array>;
384
395
  query?: Record<string, string | number | string[] | undefined>;
385
396
  /** Retry on 502/503/504 and network errors. Default: true for GET and for requests carrying an Idempotency-Key. */
386
397
  retry?: boolean;
@@ -445,8 +456,13 @@ interface CommandOptions {
445
456
  * For a longer command use `background: true` and `wait()` on the handle.
446
457
  */
447
458
  timeoutMs?: number;
448
- /** Text sent to the command's standard input, at most 1 MiB (UTF-8). */
449
- stdin?: string;
459
+ /**
460
+ * The command's standard input: text, or bytes that reach the command exactly as given. Up to 1 MiB
461
+ * of UTF-8 text goes in the exec request itself; anything else (bytes that are not UTF-8, or more than
462
+ * 1 MiB) is first uploaded to a temporary file in the sandbox, as `instabox exec --stdin` does, and the
463
+ * command reads it from there (up to the server's 5 GiB file limit). Not with `background: true`.
464
+ */
465
+ stdin?: string | Uint8Array;
450
466
  /**
451
467
  * Called with each chunk of stdout as the command writes it. With `onStdout` or `onStderr` set, the
452
468
  * output is streamed and not capped: the result still carries the whole stdout and stderr.
@@ -500,7 +516,9 @@ interface CommandHandle {
500
516
  */
501
517
  wait(opts?: CommandWaitOptions): Promise<CommandResult>;
502
518
  }
503
- type ExecOptions = Pick<CommandOptions, 'cwd' | 'envs' | 'timeoutMs' | 'stdin' | 'signal'> & {
519
+ type ExecOptions = Pick<CommandOptions, 'cwd' | 'envs' | 'timeoutMs' | 'signal'> & {
520
+ /** Text for the request's `stdin` (at most 1 MiB). */
521
+ stdin?: string;
504
522
  /** Leave the command out of the sandbox's history (job polls). */
505
523
  noHistory?: boolean;
506
524
  /** How the command appears in the sandbox's history. */
@@ -510,6 +528,11 @@ declare class Commands {
510
528
  private readonly http;
511
529
  private readonly sandboxId;
512
530
  private readonly defaultEnvs;
531
+ /**
532
+ * @internal Uploads stdin the exec request cannot carry to a temporary file and returns its path
533
+ * (set by `Sandbox`, which owns the file routes).
534
+ */
535
+ stageStdin?: (bytes: Uint8Array, signal?: AbortSignal) => Promise<string>;
513
536
  /** @internal */
514
537
  constructor(http: HttpClient, sandboxId: string, defaultEnvs: Record<string, string>);
515
538
  /**
@@ -550,8 +573,14 @@ declare class Commands {
550
573
  private waitJob;
551
574
  }
552
575
 
553
- /** Largest file `files.read` / `files.write` move. */
576
+ /**
577
+ * Default cap of a buffered `files.read` (raise it with `maxBytes`, or use `readStream`), and the most
578
+ * `files.write` moves on a server without the file routes (over exec). Over the file routes `write`
579
+ * and `readStream` have no such cap: the server takes a file of up to 5 GiB (`MAX_UPLOAD_BYTES`).
580
+ */
554
581
  declare const MAX_FILE_BYTES: number;
582
+ /** The largest file the server's `PUT /v1/machines/{id}/files` takes: 5 GiB. */
583
+ declare const MAX_UPLOAD_BYTES: number;
555
584
  interface EntryInfo {
556
585
  name: string;
557
586
  path: string;
@@ -561,10 +590,60 @@ interface WriteInfo {
561
590
  path: string;
562
591
  }
563
592
  /**
564
- * File access inside the sandbox, capped at 10 MiB per file. `read` and `write` stream the bytes
565
- * over the file routes (`GET /v1/machines/{id}/files/content`, `PUT /v1/machines/{id}/files`), as
566
- * the CLI's `cp` does; a server without them gets base64 chunks over exec. `list`, `exists` and
567
- * `remove` run over exec.
593
+ * What `files.write` takes: text, bytes, or a stream of bytes. A stream is sent as it is read, never
594
+ * held in memory whole: a Node `Readable` (`fs.createReadStream(...)`), a web `ReadableStream`, or any
595
+ * `AsyncIterable` of `Uint8Array` (or string) chunks, such as an async generator.
596
+ */
597
+ type WriteData = string | Uint8Array | ArrayBuffer | ReadableStream<Uint8Array> | AsyncIterable<Uint8Array | string>;
598
+ interface WriteOptions {
599
+ /** Stops the transfer: `write` throws `InstaboxError` with code `aborted`, and the file is left as it was. */
600
+ signal?: AbortSignal;
601
+ }
602
+ interface ReadStreamOptions {
603
+ /** Stops the transfer: the promise rejects, or the stream errors, with `InstaboxError` code `aborted`. */
604
+ signal?: AbortSignal;
605
+ }
606
+ /** A file's bytes as they arrive: a web `ReadableStream` that is also an `AsyncIterable` (`for await`). */
607
+ type FileStream = ReadableStream<Uint8Array> & AsyncIterable<Uint8Array>;
608
+ interface UploadOptions {
609
+ /**
610
+ * In a git work tree, upload only the files git tracks, as `instabox push` does. Default true;
611
+ * `false` uploads everything under the directory.
612
+ */
613
+ git?: boolean;
614
+ /** With `git`, also upload untracked files that git does not ignore (`instabox push --all`). */
615
+ all?: boolean;
616
+ /** Stops the transfer: throws `InstaboxError` with code `aborted`. */
617
+ signal?: AbortSignal;
618
+ }
619
+ interface UploadInfo {
620
+ /** The sandbox directory the files went into (absolute, symlinks resolved). */
621
+ path: string;
622
+ /** Regular files uploaded (directories and symlinks are not counted). */
623
+ files: number;
624
+ /** Size of the gzipped tar that was sent. */
625
+ bytes: number;
626
+ }
627
+ interface DownloadOptions {
628
+ /** Stops the transfer: throws `InstaboxError` with code `aborted`. */
629
+ signal?: AbortSignal;
630
+ }
631
+ interface DownloadInfo {
632
+ /** The local directory the files went into. */
633
+ path: string;
634
+ /** The sandbox directory (absolute, symlinks resolved). */
635
+ remotePath: string;
636
+ /** Regular files written. */
637
+ files: number;
638
+ /** Size of the gzipped tar that was received. */
639
+ bytes: number;
640
+ }
641
+ /**
642
+ * File access inside the sandbox. `read`, `readStream` and `write` move the bytes over the file routes
643
+ * (`GET /v1/machines/{id}/files/content`, `PUT /v1/machines/{id}/files`), as the CLI's `cp` does; a
644
+ * server without them gets base64 chunks over exec (up to 10 MiB). `upload` and `download` move a
645
+ * directory as one gzipped tar over the same routes, as `instabox push` and `instabox pull` do.
646
+ * `list`, `exists` and `remove` run over exec.
568
647
  */
569
648
  declare class Files {
570
649
  private readonly commands;
@@ -573,34 +652,109 @@ declare class Files {
573
652
  private home?;
574
653
  /** False once a server answered that it has no file routes. */
575
654
  private routes;
655
+ /** True once a server answered on a file route, so a stream need not probe for it. */
656
+ private routesSeen;
576
657
  /** @internal */
577
658
  constructor(commands: Commands, http?: HttpClient | undefined, sandboxId?: string | undefined);
659
+ private get filesPath();
578
660
  /**
579
- * Reads a file. Returns text (UTF-8) by default, or bytes with `{ format: 'bytes' }`. A text read
580
- * of a file that is not valid UTF-8 throws `InstaboxNotUtf8Error` (code `not_utf8`) rather than
581
- * returning U+FFFD in place of the bad bytes.
661
+ * Reads a file into memory. Returns text (UTF-8) by default, or bytes with `{ format: 'bytes' }`. A
662
+ * text read of a file that is not valid UTF-8 throws `InstaboxNotUtf8Error` (code `not_utf8`) rather
663
+ * than returning U+FFFD in place of the bad bytes. A file over `maxBytes` (default 10 MiB) throws
664
+ * `file_too_large`: pass a larger `maxBytes`, or use `readStream`, for bigger files.
582
665
  */
583
666
  read(path: string, opts?: {
584
667
  format?: 'text';
668
+ maxBytes?: number;
585
669
  }): Promise<string>;
586
670
  read(path: string, opts: {
587
- format: 'bytes'; /** @internal */
671
+ format: 'bytes';
588
672
  maxBytes?: number;
589
673
  }): Promise<Uint8Array>;
590
- /** Reads over `GET .../files/content`; undefined when the server has no such route. */
591
- private readStream;
674
+ /**
675
+ * Opens a file for reading as a stream, with no size cap: the bytes arrive as the server sends them,
676
+ * so a file of any size reads in bounded memory. Errors before the first byte (`not_found`, a
677
+ * directory) reject the promise; a stream cut short errors with `file_changed`. Read it with
678
+ * `for await`, `getReader()`, or in Node `Readable.fromWeb(stream)` and `pipeline`. On a server
679
+ * without the file routes the file is read over exec first (at most 10 MiB).
680
+ */
681
+ readStream(path: string, opts?: ReadStreamOptions): Promise<FileStream>;
682
+ /**
683
+ * `GET .../files/content` of `path` (or of `abs`, already absolute); undefined when the server has
684
+ * no such route. `not_found` and `not_a_file` become `not_found`: a directory is not a file either.
685
+ */
686
+ private openRoute;
687
+ /** Reads a whole response body of `path`, at most `limit` bytes. */
688
+ private readRoute;
689
+ /** A response body as a stream that errors with `file_changed` when it ends short of Content-Length. */
690
+ private countedBody;
592
691
  private readExec;
593
692
  /**
594
- * Writes a file, creating parent directories. Replaces the file atomically (temp file + rename).
595
- * Throws `is_a_directory` when `path` is a directory.
693
+ * Writes a file, creating parent directories. Replaces the file atomically (temp file + rename), so
694
+ * a failed or aborted write leaves the old file. Throws `is_a_directory` when `path` is a directory.
695
+ *
696
+ * `data` is a string, bytes, or a stream (a Node `Readable`, a web `ReadableStream`, an
697
+ * `AsyncIterable` of chunks): a stream is sent as it is read, in bounded memory, up to the server's
698
+ * 5 GiB. The size and SHA-256 the server reports are checked against what was sent
699
+ * (`transfer_mismatch` when they differ). On a server without the file routes, writes go over exec
700
+ * and are capped at 10 MiB (`file_too_large`).
596
701
  */
597
- write(path: string, data: string | Uint8Array): Promise<WriteInfo>;
702
+ write(path: string, data: WriteData, opts?: WriteOptions): Promise<WriteInfo>;
598
703
  /**
599
704
  * Writes over `PUT .../files?parents=true`, which writes a temp file beside the target and renames
600
705
  * it into place once every byte arrived. False when the server has no such route.
601
706
  */
602
- private writeStream;
707
+ private writeRoute;
708
+ /**
709
+ * True when the server has the file routes. A stream can be sent only once, so before the first one
710
+ * the route is probed with a relative path, which the server refuses (400) before touching the
711
+ * sandbox; a server without the route answers 404 or 405.
712
+ */
713
+ private routesAvailable;
714
+ /**
715
+ * `PUT .../files` of `body` to the absolute path `abs`, checking the size and SHA-256 the server
716
+ * reports. Bytes are retried on 502-504 (the server replaces the file atomically, so a repeat writes
717
+ * the same bytes); a stream is sent once, as it is read, and given up when it moves nothing for
718
+ * 2 minutes. Undefined when the server has no such route.
719
+ */
720
+ private put;
721
+ private putStream;
603
722
  private writeExec;
723
+ /**
724
+ * Puts `src` at the absolute temporary path `abs` (mode 0600): over the file route, or in base64
725
+ * chunks over exec (`instabox push`'s fallback) on a server without it.
726
+ */
727
+ private putTemp;
728
+ /**
729
+ * Runs a transfer script and returns its stdout after `mark`. A non-zero exit throws `transfer_failed`
730
+ * (`permission_denied`, status 403, when the sandbox refused access), as the CLI reports it.
731
+ */
732
+ private transferExec;
733
+ /**
734
+ * Uploads a local directory's contents into `remoteDir` in one call, as one gzipped tar, the way
735
+ * `instabox push` does: streamed over `PUT .../files` to a temporary file, then unpacked in the
736
+ * sandbox with `tar`. `remoteDir` and its parents are created; existing files there are replaced,
737
+ * others kept. In a git work tree only what git tracks is uploaded (pass `all: true` to add
738
+ * untracked files git does not ignore, `git: false` to upload everything). Symlinks are uploaded as
739
+ * symlinks, never followed; sockets, FIFOs and devices are skipped. Node.js (or Bun, Deno) only.
740
+ */
741
+ upload(localDir: string, remoteDir: string, opts?: UploadOptions): Promise<UploadInfo>;
742
+ /**
743
+ * Downloads a sandbox directory into `localDir` in one call, as one gzipped tar, the way
744
+ * `instabox pull` packs it: `tar` in the sandbox (as root when sudo allows, as the file routes read),
745
+ * streamed back over `GET .../files/content` and unpacked as it arrives. `localDir` and its parents
746
+ * are created; existing files are replaced, others kept. Everything under `remoteDir` comes, dot
747
+ * files and empty directories included; symlinks arrive as symlinks. No entry can write outside
748
+ * `localDir`. Node.js (or Bun, Deno) only.
749
+ */
750
+ download(remoteDir: string, localDir: string, opts?: DownloadOptions): Promise<DownloadInfo>;
751
+ /** The temporary archive's bytes: over the file route, or in base64 chunks over exec without it. */
752
+ private fetchTemp;
753
+ /**
754
+ * @internal Stages bytes as a temporary file for a command's stdin (`instabox exec --stdin`'s way) and
755
+ * returns its path. The command removes it as it starts.
756
+ */
757
+ stageStdin(bytes: Uint8Array, signal?: AbortSignal): Promise<string>;
604
758
  /** The file routes take absolute paths; a relative one is relative to exec's working directory (HOME). */
605
759
  private absolute;
606
760
  /** Lists the entries of a directory (not recursive). */
@@ -804,6 +958,11 @@ interface SandboxUsage {
804
958
  memGiBSeconds: number;
805
959
  /** Disk size (system disk plus /data) times the seconds the sandbox existed in the window. */
806
960
  diskGiBSeconds: number;
961
+ /**
962
+ * Bytes sent to the public internet, by whole hour: every hour from the one `from` falls in up to `to`.
963
+ * Billed or not (the plan's free egress and free sandboxes included). Absent from older servers.
964
+ */
965
+ egressBytes?: number;
807
966
  }
808
967
  interface SandboxUsageOpts {
809
968
  /** Default: the start of the current calendar month (UTC). */
@@ -942,4 +1101,4 @@ declare class Sandbox {
942
1101
  private mutate;
943
1102
  }
944
1103
 
945
- export { type ApiError, CommandExitError, type CommandHandle, type CommandOptions, type CommandResult, type CommandWaitOptions, Commands, type Connection, type ConnectionOptions, type CreateSnapshotOpts, DEFAULT_API_URL, DEFAULT_TEMPLATE, DESKTOP_DISPLAY, Desktop, type DesktopStatus, type DesktopStream, type EntryInfo, type ExecResponse, FALLBACK_TEMPLATE, Files, type HostTrust, InstaboxAuthError, InstaboxError, type InstaboxErrorInit, InstaboxNotUtf8Error, InstaboxQuotaError, InstaboxTimeoutError, LIMIT_CODES, MAX_FILE_BYTES, MAX_STDIN_BYTES, type Machine, type MachineList, type MachineMetric, type MachineMetrics, type MachineSpec, type MachineState, type MachineUsage, type MachineVault, type MachineVaultItem, type MouseButton, type Operation, Sandbox, type SandboxConnectOpts, type SandboxInfo, type SandboxListOpts, type SandboxMetrics, type SandboxMetricsOpts, type SandboxOpts, type SandboxUsage, type SandboxUsageOpts, type ScrollDirection, type Snapshot, type SnapshotList, type SshCredentials, type Template, type TemplateRef, type Usage, type UsageCost, type UsageLimits, type UsageMachineStorage, type UsageStorage, type VaultApplied, type VaultFile, type VaultKept, type WriteInfo, isLimitCode };
1104
+ export { type ApiError, CommandExitError, type CommandHandle, type CommandOptions, type CommandResult, type CommandWaitOptions, Commands, type Connection, type ConnectionOptions, type CreateSnapshotOpts, DEFAULT_API_URL, DEFAULT_TEMPLATE, DESKTOP_DISPLAY, Desktop, type DesktopStatus, type DesktopStream, type DownloadInfo, type DownloadOptions, type EntryInfo, type ExecResponse, FALLBACK_TEMPLATE, type FileStream, Files, type HostTrust, InstaboxAuthError, InstaboxError, type InstaboxErrorInit, InstaboxNotUtf8Error, InstaboxQuotaError, InstaboxTimeoutError, LIMIT_CODES, MAX_FILE_BYTES, MAX_STDIN_BYTES, MAX_UPLOAD_BYTES, type Machine, type MachineList, type MachineMetric, type MachineMetrics, type MachineSpec, type MachineState, type MachineUsage, type MachineVault, type MachineVaultItem, type MouseButton, type Operation, type ReadStreamOptions, Sandbox, type SandboxConnectOpts, type SandboxInfo, type SandboxListOpts, type SandboxMetrics, type SandboxMetricsOpts, type SandboxOpts, type SandboxUsage, type SandboxUsageOpts, type ScrollDirection, type Snapshot, type SnapshotList, type SshCredentials, type Template, type TemplateRef, type UploadInfo, type UploadOptions, type Usage, type UsageCost, type UsageLimits, type UsageMachineStorage, type UsageStorage, type VaultApplied, type VaultFile, type VaultKept, type WriteData, type WriteInfo, type WriteOptions, isLimitCode };
package/dist/index.d.ts CHANGED
@@ -248,6 +248,7 @@ interface MachineUsage {
248
248
  vcpuSeconds: number;
249
249
  memGiBSeconds: number;
250
250
  diskGiBSeconds: number;
251
+ egressBytes?: number;
251
252
  }
252
253
  /** One sandbox's share of `Usage.storedBytes`. */
253
254
  interface UsageMachineStorage {
@@ -298,7 +299,12 @@ interface UsageCost {
298
299
  storageGiBHours: number;
299
300
  /** Egress beyond the free amount. */
300
301
  egressGiB: number;
302
+ /** Compute: `vcpuUSD` plus `memUSD`. */
301
303
  runtimeUSD: number;
304
+ /** The vCPU part of compute. Absent from older servers. */
305
+ vcpuUSD?: number;
306
+ /** The memory part of compute. Absent from older servers. */
307
+ memUSD?: number;
302
308
  storageUSD: number;
303
309
  egressUSD: number;
304
310
  /** The month's usage so far. */
@@ -381,6 +387,11 @@ interface RequestOptions {
381
387
  body?: unknown;
382
388
  /** A raw request body, sent as `application/octet-stream` instead of JSON. */
383
389
  rawBody?: Uint8Array;
390
+ /**
391
+ * A streamed request body, sent as `application/octet-stream` with `duplex: 'half'` (chunked when no
392
+ * length is known). A stream can be read once, so such a request is never retried.
393
+ */
394
+ streamBody?: ReadableStream<Uint8Array>;
384
395
  query?: Record<string, string | number | string[] | undefined>;
385
396
  /** Retry on 502/503/504 and network errors. Default: true for GET and for requests carrying an Idempotency-Key. */
386
397
  retry?: boolean;
@@ -445,8 +456,13 @@ interface CommandOptions {
445
456
  * For a longer command use `background: true` and `wait()` on the handle.
446
457
  */
447
458
  timeoutMs?: number;
448
- /** Text sent to the command's standard input, at most 1 MiB (UTF-8). */
449
- stdin?: string;
459
+ /**
460
+ * The command's standard input: text, or bytes that reach the command exactly as given. Up to 1 MiB
461
+ * of UTF-8 text goes in the exec request itself; anything else (bytes that are not UTF-8, or more than
462
+ * 1 MiB) is first uploaded to a temporary file in the sandbox, as `instabox exec --stdin` does, and the
463
+ * command reads it from there (up to the server's 5 GiB file limit). Not with `background: true`.
464
+ */
465
+ stdin?: string | Uint8Array;
450
466
  /**
451
467
  * Called with each chunk of stdout as the command writes it. With `onStdout` or `onStderr` set, the
452
468
  * output is streamed and not capped: the result still carries the whole stdout and stderr.
@@ -500,7 +516,9 @@ interface CommandHandle {
500
516
  */
501
517
  wait(opts?: CommandWaitOptions): Promise<CommandResult>;
502
518
  }
503
- type ExecOptions = Pick<CommandOptions, 'cwd' | 'envs' | 'timeoutMs' | 'stdin' | 'signal'> & {
519
+ type ExecOptions = Pick<CommandOptions, 'cwd' | 'envs' | 'timeoutMs' | 'signal'> & {
520
+ /** Text for the request's `stdin` (at most 1 MiB). */
521
+ stdin?: string;
504
522
  /** Leave the command out of the sandbox's history (job polls). */
505
523
  noHistory?: boolean;
506
524
  /** How the command appears in the sandbox's history. */
@@ -510,6 +528,11 @@ declare class Commands {
510
528
  private readonly http;
511
529
  private readonly sandboxId;
512
530
  private readonly defaultEnvs;
531
+ /**
532
+ * @internal Uploads stdin the exec request cannot carry to a temporary file and returns its path
533
+ * (set by `Sandbox`, which owns the file routes).
534
+ */
535
+ stageStdin?: (bytes: Uint8Array, signal?: AbortSignal) => Promise<string>;
513
536
  /** @internal */
514
537
  constructor(http: HttpClient, sandboxId: string, defaultEnvs: Record<string, string>);
515
538
  /**
@@ -550,8 +573,14 @@ declare class Commands {
550
573
  private waitJob;
551
574
  }
552
575
 
553
- /** Largest file `files.read` / `files.write` move. */
576
+ /**
577
+ * Default cap of a buffered `files.read` (raise it with `maxBytes`, or use `readStream`), and the most
578
+ * `files.write` moves on a server without the file routes (over exec). Over the file routes `write`
579
+ * and `readStream` have no such cap: the server takes a file of up to 5 GiB (`MAX_UPLOAD_BYTES`).
580
+ */
554
581
  declare const MAX_FILE_BYTES: number;
582
+ /** The largest file the server's `PUT /v1/machines/{id}/files` takes: 5 GiB. */
583
+ declare const MAX_UPLOAD_BYTES: number;
555
584
  interface EntryInfo {
556
585
  name: string;
557
586
  path: string;
@@ -561,10 +590,60 @@ interface WriteInfo {
561
590
  path: string;
562
591
  }
563
592
  /**
564
- * File access inside the sandbox, capped at 10 MiB per file. `read` and `write` stream the bytes
565
- * over the file routes (`GET /v1/machines/{id}/files/content`, `PUT /v1/machines/{id}/files`), as
566
- * the CLI's `cp` does; a server without them gets base64 chunks over exec. `list`, `exists` and
567
- * `remove` run over exec.
593
+ * What `files.write` takes: text, bytes, or a stream of bytes. A stream is sent as it is read, never
594
+ * held in memory whole: a Node `Readable` (`fs.createReadStream(...)`), a web `ReadableStream`, or any
595
+ * `AsyncIterable` of `Uint8Array` (or string) chunks, such as an async generator.
596
+ */
597
+ type WriteData = string | Uint8Array | ArrayBuffer | ReadableStream<Uint8Array> | AsyncIterable<Uint8Array | string>;
598
+ interface WriteOptions {
599
+ /** Stops the transfer: `write` throws `InstaboxError` with code `aborted`, and the file is left as it was. */
600
+ signal?: AbortSignal;
601
+ }
602
+ interface ReadStreamOptions {
603
+ /** Stops the transfer: the promise rejects, or the stream errors, with `InstaboxError` code `aborted`. */
604
+ signal?: AbortSignal;
605
+ }
606
+ /** A file's bytes as they arrive: a web `ReadableStream` that is also an `AsyncIterable` (`for await`). */
607
+ type FileStream = ReadableStream<Uint8Array> & AsyncIterable<Uint8Array>;
608
+ interface UploadOptions {
609
+ /**
610
+ * In a git work tree, upload only the files git tracks, as `instabox push` does. Default true;
611
+ * `false` uploads everything under the directory.
612
+ */
613
+ git?: boolean;
614
+ /** With `git`, also upload untracked files that git does not ignore (`instabox push --all`). */
615
+ all?: boolean;
616
+ /** Stops the transfer: throws `InstaboxError` with code `aborted`. */
617
+ signal?: AbortSignal;
618
+ }
619
+ interface UploadInfo {
620
+ /** The sandbox directory the files went into (absolute, symlinks resolved). */
621
+ path: string;
622
+ /** Regular files uploaded (directories and symlinks are not counted). */
623
+ files: number;
624
+ /** Size of the gzipped tar that was sent. */
625
+ bytes: number;
626
+ }
627
+ interface DownloadOptions {
628
+ /** Stops the transfer: throws `InstaboxError` with code `aborted`. */
629
+ signal?: AbortSignal;
630
+ }
631
+ interface DownloadInfo {
632
+ /** The local directory the files went into. */
633
+ path: string;
634
+ /** The sandbox directory (absolute, symlinks resolved). */
635
+ remotePath: string;
636
+ /** Regular files written. */
637
+ files: number;
638
+ /** Size of the gzipped tar that was received. */
639
+ bytes: number;
640
+ }
641
+ /**
642
+ * File access inside the sandbox. `read`, `readStream` and `write` move the bytes over the file routes
643
+ * (`GET /v1/machines/{id}/files/content`, `PUT /v1/machines/{id}/files`), as the CLI's `cp` does; a
644
+ * server without them gets base64 chunks over exec (up to 10 MiB). `upload` and `download` move a
645
+ * directory as one gzipped tar over the same routes, as `instabox push` and `instabox pull` do.
646
+ * `list`, `exists` and `remove` run over exec.
568
647
  */
569
648
  declare class Files {
570
649
  private readonly commands;
@@ -573,34 +652,109 @@ declare class Files {
573
652
  private home?;
574
653
  /** False once a server answered that it has no file routes. */
575
654
  private routes;
655
+ /** True once a server answered on a file route, so a stream need not probe for it. */
656
+ private routesSeen;
576
657
  /** @internal */
577
658
  constructor(commands: Commands, http?: HttpClient | undefined, sandboxId?: string | undefined);
659
+ private get filesPath();
578
660
  /**
579
- * Reads a file. Returns text (UTF-8) by default, or bytes with `{ format: 'bytes' }`. A text read
580
- * of a file that is not valid UTF-8 throws `InstaboxNotUtf8Error` (code `not_utf8`) rather than
581
- * returning U+FFFD in place of the bad bytes.
661
+ * Reads a file into memory. Returns text (UTF-8) by default, or bytes with `{ format: 'bytes' }`. A
662
+ * text read of a file that is not valid UTF-8 throws `InstaboxNotUtf8Error` (code `not_utf8`) rather
663
+ * than returning U+FFFD in place of the bad bytes. A file over `maxBytes` (default 10 MiB) throws
664
+ * `file_too_large`: pass a larger `maxBytes`, or use `readStream`, for bigger files.
582
665
  */
583
666
  read(path: string, opts?: {
584
667
  format?: 'text';
668
+ maxBytes?: number;
585
669
  }): Promise<string>;
586
670
  read(path: string, opts: {
587
- format: 'bytes'; /** @internal */
671
+ format: 'bytes';
588
672
  maxBytes?: number;
589
673
  }): Promise<Uint8Array>;
590
- /** Reads over `GET .../files/content`; undefined when the server has no such route. */
591
- private readStream;
674
+ /**
675
+ * Opens a file for reading as a stream, with no size cap: the bytes arrive as the server sends them,
676
+ * so a file of any size reads in bounded memory. Errors before the first byte (`not_found`, a
677
+ * directory) reject the promise; a stream cut short errors with `file_changed`. Read it with
678
+ * `for await`, `getReader()`, or in Node `Readable.fromWeb(stream)` and `pipeline`. On a server
679
+ * without the file routes the file is read over exec first (at most 10 MiB).
680
+ */
681
+ readStream(path: string, opts?: ReadStreamOptions): Promise<FileStream>;
682
+ /**
683
+ * `GET .../files/content` of `path` (or of `abs`, already absolute); undefined when the server has
684
+ * no such route. `not_found` and `not_a_file` become `not_found`: a directory is not a file either.
685
+ */
686
+ private openRoute;
687
+ /** Reads a whole response body of `path`, at most `limit` bytes. */
688
+ private readRoute;
689
+ /** A response body as a stream that errors with `file_changed` when it ends short of Content-Length. */
690
+ private countedBody;
592
691
  private readExec;
593
692
  /**
594
- * Writes a file, creating parent directories. Replaces the file atomically (temp file + rename).
595
- * Throws `is_a_directory` when `path` is a directory.
693
+ * Writes a file, creating parent directories. Replaces the file atomically (temp file + rename), so
694
+ * a failed or aborted write leaves the old file. Throws `is_a_directory` when `path` is a directory.
695
+ *
696
+ * `data` is a string, bytes, or a stream (a Node `Readable`, a web `ReadableStream`, an
697
+ * `AsyncIterable` of chunks): a stream is sent as it is read, in bounded memory, up to the server's
698
+ * 5 GiB. The size and SHA-256 the server reports are checked against what was sent
699
+ * (`transfer_mismatch` when they differ). On a server without the file routes, writes go over exec
700
+ * and are capped at 10 MiB (`file_too_large`).
596
701
  */
597
- write(path: string, data: string | Uint8Array): Promise<WriteInfo>;
702
+ write(path: string, data: WriteData, opts?: WriteOptions): Promise<WriteInfo>;
598
703
  /**
599
704
  * Writes over `PUT .../files?parents=true`, which writes a temp file beside the target and renames
600
705
  * it into place once every byte arrived. False when the server has no such route.
601
706
  */
602
- private writeStream;
707
+ private writeRoute;
708
+ /**
709
+ * True when the server has the file routes. A stream can be sent only once, so before the first one
710
+ * the route is probed with a relative path, which the server refuses (400) before touching the
711
+ * sandbox; a server without the route answers 404 or 405.
712
+ */
713
+ private routesAvailable;
714
+ /**
715
+ * `PUT .../files` of `body` to the absolute path `abs`, checking the size and SHA-256 the server
716
+ * reports. Bytes are retried on 502-504 (the server replaces the file atomically, so a repeat writes
717
+ * the same bytes); a stream is sent once, as it is read, and given up when it moves nothing for
718
+ * 2 minutes. Undefined when the server has no such route.
719
+ */
720
+ private put;
721
+ private putStream;
603
722
  private writeExec;
723
+ /**
724
+ * Puts `src` at the absolute temporary path `abs` (mode 0600): over the file route, or in base64
725
+ * chunks over exec (`instabox push`'s fallback) on a server without it.
726
+ */
727
+ private putTemp;
728
+ /**
729
+ * Runs a transfer script and returns its stdout after `mark`. A non-zero exit throws `transfer_failed`
730
+ * (`permission_denied`, status 403, when the sandbox refused access), as the CLI reports it.
731
+ */
732
+ private transferExec;
733
+ /**
734
+ * Uploads a local directory's contents into `remoteDir` in one call, as one gzipped tar, the way
735
+ * `instabox push` does: streamed over `PUT .../files` to a temporary file, then unpacked in the
736
+ * sandbox with `tar`. `remoteDir` and its parents are created; existing files there are replaced,
737
+ * others kept. In a git work tree only what git tracks is uploaded (pass `all: true` to add
738
+ * untracked files git does not ignore, `git: false` to upload everything). Symlinks are uploaded as
739
+ * symlinks, never followed; sockets, FIFOs and devices are skipped. Node.js (or Bun, Deno) only.
740
+ */
741
+ upload(localDir: string, remoteDir: string, opts?: UploadOptions): Promise<UploadInfo>;
742
+ /**
743
+ * Downloads a sandbox directory into `localDir` in one call, as one gzipped tar, the way
744
+ * `instabox pull` packs it: `tar` in the sandbox (as root when sudo allows, as the file routes read),
745
+ * streamed back over `GET .../files/content` and unpacked as it arrives. `localDir` and its parents
746
+ * are created; existing files are replaced, others kept. Everything under `remoteDir` comes, dot
747
+ * files and empty directories included; symlinks arrive as symlinks. No entry can write outside
748
+ * `localDir`. Node.js (or Bun, Deno) only.
749
+ */
750
+ download(remoteDir: string, localDir: string, opts?: DownloadOptions): Promise<DownloadInfo>;
751
+ /** The temporary archive's bytes: over the file route, or in base64 chunks over exec without it. */
752
+ private fetchTemp;
753
+ /**
754
+ * @internal Stages bytes as a temporary file for a command's stdin (`instabox exec --stdin`'s way) and
755
+ * returns its path. The command removes it as it starts.
756
+ */
757
+ stageStdin(bytes: Uint8Array, signal?: AbortSignal): Promise<string>;
604
758
  /** The file routes take absolute paths; a relative one is relative to exec's working directory (HOME). */
605
759
  private absolute;
606
760
  /** Lists the entries of a directory (not recursive). */
@@ -804,6 +958,11 @@ interface SandboxUsage {
804
958
  memGiBSeconds: number;
805
959
  /** Disk size (system disk plus /data) times the seconds the sandbox existed in the window. */
806
960
  diskGiBSeconds: number;
961
+ /**
962
+ * Bytes sent to the public internet, by whole hour: every hour from the one `from` falls in up to `to`.
963
+ * Billed or not (the plan's free egress and free sandboxes included). Absent from older servers.
964
+ */
965
+ egressBytes?: number;
807
966
  }
808
967
  interface SandboxUsageOpts {
809
968
  /** Default: the start of the current calendar month (UTC). */
@@ -942,4 +1101,4 @@ declare class Sandbox {
942
1101
  private mutate;
943
1102
  }
944
1103
 
945
- export { type ApiError, CommandExitError, type CommandHandle, type CommandOptions, type CommandResult, type CommandWaitOptions, Commands, type Connection, type ConnectionOptions, type CreateSnapshotOpts, DEFAULT_API_URL, DEFAULT_TEMPLATE, DESKTOP_DISPLAY, Desktop, type DesktopStatus, type DesktopStream, type EntryInfo, type ExecResponse, FALLBACK_TEMPLATE, Files, type HostTrust, InstaboxAuthError, InstaboxError, type InstaboxErrorInit, InstaboxNotUtf8Error, InstaboxQuotaError, InstaboxTimeoutError, LIMIT_CODES, MAX_FILE_BYTES, MAX_STDIN_BYTES, type Machine, type MachineList, type MachineMetric, type MachineMetrics, type MachineSpec, type MachineState, type MachineUsage, type MachineVault, type MachineVaultItem, type MouseButton, type Operation, Sandbox, type SandboxConnectOpts, type SandboxInfo, type SandboxListOpts, type SandboxMetrics, type SandboxMetricsOpts, type SandboxOpts, type SandboxUsage, type SandboxUsageOpts, type ScrollDirection, type Snapshot, type SnapshotList, type SshCredentials, type Template, type TemplateRef, type Usage, type UsageCost, type UsageLimits, type UsageMachineStorage, type UsageStorage, type VaultApplied, type VaultFile, type VaultKept, type WriteInfo, isLimitCode };
1104
+ export { type ApiError, CommandExitError, type CommandHandle, type CommandOptions, type CommandResult, type CommandWaitOptions, Commands, type Connection, type ConnectionOptions, type CreateSnapshotOpts, DEFAULT_API_URL, DEFAULT_TEMPLATE, DESKTOP_DISPLAY, Desktop, type DesktopStatus, type DesktopStream, type DownloadInfo, type DownloadOptions, type EntryInfo, type ExecResponse, FALLBACK_TEMPLATE, type FileStream, Files, type HostTrust, InstaboxAuthError, InstaboxError, type InstaboxErrorInit, InstaboxNotUtf8Error, InstaboxQuotaError, InstaboxTimeoutError, LIMIT_CODES, MAX_FILE_BYTES, MAX_STDIN_BYTES, MAX_UPLOAD_BYTES, type Machine, type MachineList, type MachineMetric, type MachineMetrics, type MachineSpec, type MachineState, type MachineUsage, type MachineVault, type MachineVaultItem, type MouseButton, type Operation, type ReadStreamOptions, Sandbox, type SandboxConnectOpts, type SandboxInfo, type SandboxListOpts, type SandboxMetrics, type SandboxMetricsOpts, type SandboxOpts, type SandboxUsage, type SandboxUsageOpts, type ScrollDirection, type Snapshot, type SnapshotList, type SshCredentials, type Template, type TemplateRef, type UploadInfo, type UploadOptions, type Usage, type UsageCost, type UsageLimits, type UsageMachineStorage, type UsageStorage, type VaultApplied, type VaultFile, type VaultKept, type WriteData, type WriteInfo, type WriteOptions, isLimitCode };