Skip to content

Commit

Permalink
feat: deprecate Deno.write() and Deno.writeSync() (#22064)
Browse files Browse the repository at this point in the history
For removal in Deno v2.
  • Loading branch information
iuioiua authored Jan 24, 2024
1 parent 50eaeab commit a5a973e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cli/tests/unit/sync_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,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);
await Deno.remove(filename);
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/unit_node/_fs/_fs_fdatasync_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Deno.test({
create: true,
});
const data = new Uint8Array(64);
await Deno.write(file.rid, data);
await file.write(data);

await new Promise<void>((resolve, reject) => {
fdatasync(file.rid, (err: Error | null) => {
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 conn.read(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 conn.read(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 @@ -2039,6 +2039,9 @@ declare namespace Deno {
* const bytesWritten = await Deno.write(file.rid, data); // 11
* ```
*
* @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 @@ -2061,6 +2064,9 @@ declare namespace Deno {
* const bytesWritten = Deno.writeSync(file.rid, data); // 11
* ```
*
* @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 @@ -2074,7 +2080,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 @@ -2092,7 +2098,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 @@ -2170,7 +2176,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 file.truncate(1);
* await Deno.fsync(file.rid);
* console.log(await Deno.readTextFile("my_file.txt")); // H
Expand Down Expand Up @@ -2212,7 +2218,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 @@ -5304,7 +5310,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
18 changes: 16 additions & 2 deletions runtime/js/90_deno_ns.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,22 @@ const denoNs = {
);
return io.readSync(rid, buffer);
},
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,
open: fs.open,
Expand Down

0 comments on commit a5a973e

Please sign in to comment.