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

chore: forward v1.29.4 release commit to main #17453

Merged
merged 2 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
revert unrelated changes
  • Loading branch information
bartlomieju committed Jan 16, 2023
commit 0feeed51c0a6b1d199b51a4fe53c60344482341f
7 changes: 7 additions & 0 deletions cli/tests/unit/os_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ Deno.test({ permissions: { env: true } }, function deleteEnv() {
assertEquals(Deno.env.get("TEST_VAR"), undefined);
});

Deno.test({ permissions: { env: true } }, function hasEnv() {
Deno.env.set("TEST_VAR", "A");
assert(Deno.env.has("TEST_VAR"));
Deno.env.delete("TEST_VAR");
assert(!Deno.env.has("TEST_VAR"));
});

Deno.test({ permissions: { env: true } }, function avoidEmptyNamedEnv() {
assertThrows(() => Deno.env.set("", "v"), TypeError);
assertThrows(() => Deno.env.set("a=a", "v"), TypeError);
Expand Down
16 changes: 16 additions & 0 deletions cli/tests/unit/write_file_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,19 @@ function pathExists(path: string | URL) {
return false;
}
}

Deno.test(
{ permissions: { read: true, write: true } },
async function writeFileStream() {
const stream = new ReadableStream({
pull(controller) {
controller.enqueue(new Uint8Array([1]));
controller.enqueue(new Uint8Array([2]));
controller.close();
},
});
const filename = Deno.makeTempDirSync() + "/test.txt";
await Deno.writeFile(filename, stream);
assertEquals(Deno.readFileSync(filename), new Uint8Array([1, 2]));
},
);
16 changes: 16 additions & 0 deletions cli/tests/unit/write_text_file_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,19 @@ Deno.test(
assertEquals(Deno.readTextFileSync(filename), "Hello");
},
);

Deno.test(
{ permissions: { read: true, write: true } },
async function writeTextFileStream() {
const stream = new ReadableStream({
pull(controller) {
controller.enqueue("Hello");
controller.enqueue("World");
controller.close();
},
});
const filename = Deno.makeTempDirSync() + "/test.txt";
await Deno.writeTextFile(filename, stream);
assertEquals(Deno.readTextFileSync(filename), "HelloWorld");
},
);
17 changes: 15 additions & 2 deletions cli/tsc/dts/lib.deno.ns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,19 @@ declare namespace Deno {
*/
delete(key: string): void;

/** Check whether an environment variable is present or not.
*
* ```ts
* Deno.env.set("SOME_VAR", "Value");
* Deno.env.has("SOME_VAR"); // outputs true
* ```
*
* Requires `allow-env` permission.
*
* @tags allow-env
*/
has(key: string): boolean;

/** Returns a snapshot of the environment variables at invocation as a
* simple object of keys and values.
*
Expand Down Expand Up @@ -3351,7 +3364,7 @@ declare namespace Deno {
*/
export function writeFile(
path: string | URL,
data: Uint8Array,
data: Uint8Array | ReadableStream<Uint8Array>,
options?: WriteFileOptions,
): Promise<void>;

Expand Down Expand Up @@ -3394,7 +3407,7 @@ declare namespace Deno {
*/
export function writeTextFile(
path: string | URL,
data: string,
data: string | ReadableStream<string>,
options?: WriteFileOptions,
): Promise<void>;

Expand Down
3 changes: 3 additions & 0 deletions runtime/js/30_os.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@
return ops.op_env();
},
set: setEnv,
has(key) {
return getEnv(key) !== undefined;
},
delete: deleteEnv,
};

Expand Down
48 changes: 36 additions & 12 deletions runtime/js/40_write_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
const ops = core.ops;
const { abortSignal } = window.__bootstrap;
const { pathFromURL } = window.__bootstrap.util;
const { open } = window.__bootstrap.files;
const { ReadableStreamPrototype } = window.__bootstrap.streams;
const { ObjectPrototypeIsPrototypeOf } = window.__bootstrap.primordials;

function writeFileSync(
path,
Expand Down Expand Up @@ -36,16 +39,29 @@
options.signal[abortSignal.add](abortHandler);
}
try {
await core.opAsync(
"op_write_file_async",
pathFromURL(path),
options.mode,
options.append ?? false,
options.create ?? true,
options.createNew ?? false,
data,
cancelRid,
);
if (ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, data)) {
const file = await open(path, {
mode: options.mode,
append: options.append ?? false,
create: options.create ?? true,
createNew: options.createNew ?? false,
write: true,
});
await data.pipeTo(file.writable, {
signal: options.signal,
});
} else {
await core.opAsync(
"op_write_file_async",
pathFromURL(path),
options.mode,
options.append ?? false,
options.create ?? true,
options.createNew ?? false,
data,
cancelRid,
);
}
} finally {
if (options.signal) {
options.signal[abortSignal.remove](abortHandler);
Expand All @@ -70,8 +86,16 @@
data,
options = {},
) {
const encoder = new TextEncoder();
return writeFile(path, encoder.encode(data), options);
if (ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, data)) {
return writeFile(
path,
data.pipeThrough(new TextEncoderStream()),
options,
);
} else {
const encoder = new TextEncoder();
return writeFile(path, encoder.encode(data), options);
}
}

window.__bootstrap.writeFile = {
Expand Down