Skip to main content

Streaming access string and binary output

Deno Sandbox provides a sandboxed Linux microVM. This is useful for evaluating code that is not trusted or for testing code that is not safe to run in your main runtime.

You can access string and binary output from commands in a sandbox. This example shows how to capture command output in whichever form your workflow needs:

import { Sandbox } from "@deno/sandbox";

await using sandbox = await Sandbox.create();

// Get both string and binary data
const result = await sandbox.sh`cat binary-file.png`
  .stdout("piped");
console.log("Binary length:", result.stdout!.length);
console.log("Text length:", result.stdoutText!.length);

// Use the binary data
import fs from "node:fs";
fs.writeFileSync("output.png", result.stdout!);

Piping stdout lets you retrieve both the raw binary buffer and a decoded text view from the same command, so you can handle files that mix binary and textual data without re-running the command.

Once you have the binary result, you can pass it directly to APIs such as fs.writeFileSync to persist artifacts generated inside the sandbox, making it easy to move data between the sandbox and your host environment

This is useful when sandbox commands produce files (images, archives, etc.) that you need to consume programmatically rather than just printing to the console.

For more information, see the Deno Sandbox documentation.

Did you find what you needed?

Privacy policy