Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ability to provide JavaScript objects and streams as file descriptors for redirects #230

Merged
merged 18 commits into from
Jan 28, 2024

Conversation

dsherret
Copy link
Owner

@dsherret dsherret commented Jan 28, 2024

This adds to Dax what's available in Bun's shell where instead of providing objects to the entire command like so:

const request = $.request("https://plugins.dprint.dev/info.json")
  .showProgress();
const bytes = await `gzip`.stdin(request).bytes(); // obviously, gzip doesn't work on windows

You can now do:

const request = $.request("https://plugins.dprint.dev/info.json")
  .showProgress();
const bytes = await `gzip < ${request}`.bytes();

// makes the request after 5 seconds instead of immediately
// (not possible without two separate statements before)
const bytes2 = await `sleep 5 && gzip < ${request}`.bytes();

You can also now redirect into any WritableStream:

const buffer = new Buffer();
await $`cat data.txt > ${toWritableStream(buffer)}`; // toWritableStream from deno_std

Or do an input redirect from any ReadableStream:

const text = "testing".repeat(1000);
const bytes = new TextEncoder().encode(text);
const stream = new ReadableStream({
  start(controller) {
    controller.enqueue(bytes);
    controller.close();
  },
});
const bytes = await $`gzip < ${stream}`.bytes();

// don't do that example though, just do:
const bytes = await $`gzip`.stdinText(text).bytes();

Or provide bytes:

const text = "testing".repeat(1000);
const bytes = new TextEncoder().encode(text);
const output = await $`gzip < ${bytes}`.bytes();

Or Response objects:

const response = new Response("testing".repeat(1000));
const output = await $`gzip < ${response}`.bytes();

Other supported objects:

  • FsFileWrapper (dax's wrapper over Deno's FsFile in the $.path API)
  • Function - Must return a ReadableStream<Uint8Array> or WritableStream

Additionally, the $.symbols.writable (Symbol.for("dax.writableStream")) and $.symbols.readable (Symbol.for("dax.readableStream")) symbols can be attached to any object to make them work in these positions (still debating this).

Unsupported objects in this position will cause an error:

assertThrows(
  () => $`echo 1 > ${new TextEncoder()}`,
  Error,
  "Failed resolving expression in command. Unsupported object provided to output redirect.",
);

@dsherret dsherret merged commit 4d39bf3 into main Jan 28, 2024
4 checks passed
@dsherret dsherret deleted the provided_redirects branch January 28, 2024 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant