Skip to content

Commit

Permalink
Support Response
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Jan 28, 2024
1 parent 076af49 commit c0f8362
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
8 changes: 8 additions & 0 deletions mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1190,12 +1190,20 @@ Deno.test("input redirects with readable", async () => {
});

Deno.test("input redirects with bytes", async () => {
// bytes
{
const text = "testing".repeat(1000);
const bytes = new TextEncoder().encode(text);
const output = await $`cat - < ${bytes}`.text();
assertEquals(output, text);
}
// response
{
const text = "testing".repeat(1000);
const response = new Response(text);
const output = await $`cat - < ${response}`.text();
assertEquals(output, text);
}
});

Deno.test("output redirect with writable", async () => {
Expand Down
20 changes: 14 additions & 6 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,9 @@ function templateInner(
const expr = exprs[i];
const inputOrOutputRedirect = detectInputOrOutputRedirect(text);
if (inputOrOutputRedirect === "<") {
if (expr instanceof ReadableStream) {
if (typeof expr === "string" || expr instanceof PathRef) {
text += templateLiteralExprToString(expr, escape);
} else if (expr instanceof ReadableStream) {
handleReadableStream(() => expr);
} else if (expr?.[symbols.readable]) {
handleReadableStream(() => {
Expand All @@ -1273,13 +1275,21 @@ function templateInner(
},
});
});
} else if (typeof expr === "string" || expr instanceof PathRef) {
text += templateLiteralExprToString(expr, escape);
} else if (expr instanceof Response) {
handleReadableStream(() => {
return expr.body ?? new ReadableStream({
start(controller) {
controller.close();
},
});
});
} else {
throw new Error("Unsupported object provided to input redirect.");
}
} else if (inputOrOutputRedirect === ">") {
if (expr instanceof WritableStream) {
if (typeof expr === "string" || expr instanceof PathRef) {
text += templateLiteralExprToString(expr, escape);
} else if (expr instanceof WritableStream) {
handleWritableStream(() => expr);
} else if (expr?.[symbols.writable]) {
handleWritableStream(() => {
Expand All @@ -1292,8 +1302,6 @@ function templateInner(
}
return stream;
});
} else if (typeof expr === "string" || expr instanceof PathRef) {
text += templateLiteralExprToString(expr, escape);
} else {
throw new Error("Unsupported object provided to output redirect.");
}
Expand Down

0 comments on commit c0f8362

Please sign in to comment.