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: deprecate Deno.write() and Deno.writeSync() #22064

Merged
merged 4 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion cli/tests/unit/sync_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Deno.test(
create: true,
});
const data = new Uint8Array(64);
await Deno.write(file.rid, data);
await file.write(data);
await Deno.fdatasync(file.rid);
assertEquals(await Deno.readFile(filename), data);
Deno.close(file.rid);
Expand Down
14 changes: 7 additions & 7 deletions cli/tests/unit_node/_fs/_fs_fdatasync_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@ Deno.test({
name:
"ASYNC: flush any pending data operations of the given file stream to disk",
async fn() {
const file: string = await Deno.makeTempFile();
const { rid } = await Deno.open(file, {
const filePath = await Deno.makeTempFile();
const file = await Deno.open(filePath, {
read: true,
write: true,
create: true,
});
const data = new Uint8Array(64);
await Deno.write(rid, data);
await file.write(data);

await new Promise<void>((resolve, reject) => {
fdatasync(rid, (err: Error | null) => {
fdatasync(file.rid, (err: Error | null) => {
if (err !== null) reject();
else resolve();
});
})
.then(
async () => {
assertEquals(await Deno.readFile(file), data);
assertEquals(await Deno.readFile(filePath), data);
},
() => {
fail("No error expected");
},
)
.finally(async () => {
Deno.close(rid);
await Deno.remove(file);
Deno.close(file.rid);
await Deno.remove(filePath);
});
},
});
Expand Down
4 changes: 2 additions & 2 deletions cli/tests/unit_node/tls_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ Deno.test("tls.createServer creates a TLS server", async () => {
assertEquals(text.replaceAll("\0", ""), "welcome!\n");
buf.fill(0);

Deno.write(conn.rid, new TextEncoder().encode("hey\n"));
await conn.write(new TextEncoder().encode("hey\n"));
await Deno.read(conn.rid, buf);
text = new TextDecoder().decode(buf);
assertEquals(text.replaceAll("\0", ""), "hey\n");
buf.fill(0);

Deno.write(conn.rid, new TextEncoder().encode("goodbye\n"));
await conn.write(new TextEncoder().encode("goodbye\n"));
await Deno.read(conn.rid, buf);
text = new TextDecoder().decode(buf);
assertEquals(text.replaceAll("\0", ""), "goodbye\n");
Expand Down
16 changes: 11 additions & 5 deletions cli/tsc/dts/lib.deno.ns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2036,6 +2036,9 @@ declare namespace Deno {
* Deno.close(file.rid);
* ```
*
* @deprecated Use `writer.write()` instead. {@linkcode Deno.write} will be
* removed in Deno 2.0.
*
* @category I/O
*/
export function write(rid: number, data: Uint8Array): Promise<number>;
Expand All @@ -2059,6 +2062,9 @@ declare namespace Deno {
* Deno.close(file.rid);
* ```
*
* @deprecated Use `writer.writeSync()` instead. {@linkcode Deno.writeSync}
* will be removed in Deno 2.0.
*
* @category I/O
*/
export function writeSync(rid: number, data: Uint8Array): number;
Expand All @@ -2072,7 +2078,7 @@ declare namespace Deno {
* "hello.txt",
* { read: true, write: true, truncate: true, create: true },
* );
* await Deno.write(file.rid, new TextEncoder().encode("Hello world"));
* await file.write(new TextEncoder().encode("Hello world"));
*
* // advance cursor 6 bytes
* const cursorPosition = await Deno.seek(file.rid, 6, Deno.SeekMode.Start);
Expand All @@ -2090,7 +2096,7 @@ declare namespace Deno {
* "hello.txt",
* { read: true, write: true, truncate: true, create: true },
* );
* await Deno.write(file.rid, new TextEncoder().encode("Hello world"));
* await file.write(new TextEncoder().encode("Hello world"));
*
* // Seek 6 bytes from the start of the file
* console.log(await Deno.seek(file.rid, 6, Deno.SeekMode.Start)); // "6"
Expand Down Expand Up @@ -2162,7 +2168,7 @@ declare namespace Deno {
* "my_file.txt",
* { read: true, write: true, create: true },
* );
* await Deno.write(file.rid, new TextEncoder().encode("Hello World"));
* await file.write(new TextEncoder().encode("Hello World"));
* await Deno.ftruncate(file.rid, 1);
* await Deno.fsync(file.rid);
* console.log(await Deno.readTextFile("my_file.txt")); // H
Expand Down Expand Up @@ -2204,7 +2210,7 @@ declare namespace Deno {
* "my_file.txt",
* { read: true, write: true, create: true },
* );
* await Deno.write(file.rid, new TextEncoder().encode("Hello World"));
* await file.write(new TextEncoder().encode("Hello World"));
* await Deno.fdatasync(file.rid);
* console.log(await Deno.readTextFile("my_file.txt")); // Hello World
* ```
Expand Down Expand Up @@ -5284,7 +5290,7 @@ declare namespace Deno {
* "my_file.txt",
* { read: true, write: true, create: true }
* );
* await Deno.write(file.rid, new TextEncoder().encode("Hello World"));
* await file.write(new TextEncoder().encode("Hello World"));
* await Deno.ftruncate(file.rid, 7);
* const data = new Uint8Array(32);
* await Deno.read(file.rid, data);
Expand Down
20 changes: 17 additions & 3 deletions runtime/js/90_deno_ns.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { core } from "ext:core/mod.js";
import { core, internals } from "ext:core/mod.js";
const {
op_net_listen_udp,
op_net_listen_unixpacket,
Expand Down Expand Up @@ -98,8 +98,22 @@ const denoNs = {
SeekMode: io.SeekMode,
read: io.read,
readSync: io.readSync,
write: io.write,
writeSync: io.writeSync,
write(rid, data) {
internals.warnOnDeprecatedApi(
"Deno.write()",
new Error().stack,
"Use `writer.write()` instead.",
);
return io.write(rid, data);
},
writeSync(rid, data) {
internals.warnOnDeprecatedApi(
"Deno.writeSync()",
new Error().stack,
"Use `writer.writeSync()` instead.",
);
return io.writeSync(rid, data);
},
File: fs.File,
FsFile: fs.FsFile,
open: fs.open,
Expand Down