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(std) support AbortSignal in writeFile #11568

Merged
merged 5 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cli/dts/lib.deno.ns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1714,6 +1714,12 @@ declare namespace Deno {
create?: boolean;
/** Permissions always applied to file. */
mode?: number;
/**
* An abort signal to allow cancellation of the file write operation.
* If the signal becomes aborted the writeFile operation will be stopped
* and the promise returned will be rejected with an AbortError.
*/
signal?: AbortSignal;
}

/** Synchronously write `data` to the given `path`, by default creating a new
Expand Down
36 changes: 36 additions & 0 deletions cli/tests/unit/write_file_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,39 @@ unitTest(
assertEquals("Hello", actual);
},
);

unitTest(
{ perms: { read: true, write: true } },
async function writeFileAbortSignal(): Promise<void> {
const ac = new AbortController();
const enc = new TextEncoder();
const data = enc.encode("Hello");
const filename = Deno.makeTempDirSync() + "/test.txt";
queueMicrotask(() => ac.abort());
try {
await Deno.writeFile(filename, data, { signal: ac.signal });
} catch (e) {
assertEquals(e.name, "AbortError");
}
const stat = Deno.statSync(filename);
assertEquals(stat.size, 0);
},
);

unitTest(
{ perms: { read: true, write: true } },
async function writeFileAbortSignalPreAborted(): Promise<void> {
const ac = new AbortController();
ac.abort();
const enc = new TextEncoder();
const data = enc.encode("Hello");
const filename = Deno.makeTempDirSync() + "/test.txt";
try {
await Deno.writeFile(filename, data, { signal: ac.signal });
} catch (e) {
assertEquals(e.name, "AbortError");
}
const stat = Deno.statSync(filename);
assertEquals(stat.size, 0);
},
);
10 changes: 9 additions & 1 deletion runtime/js/40_write_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
data,
options = {},
) {
if (options?.signal?.aborted) {
throw new DOMException("The write operation was aborted.", "AbortError");
}
if (options.create !== undefined) {
const create = !!options.create;
if (!create) {
Expand Down Expand Up @@ -68,12 +71,17 @@
await chmod(path, options.mode);
}

const signal = options?.signal ?? null;
let nwritten = 0;
while (nwritten < data.length) {
while (!signal?.aborted && nwritten < data.length) {
nwritten += await file.write(TypedArrayPrototypeSubarray(data, nwritten));
}

file.close();

if (signal?.aborted) {
throw new DOMException("The write operation was aborted.", "AbortError");
}
}

function writeTextFileSync(
Expand Down