summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcos Casagrande <marcoscvp90@gmail.com>2020-06-12 21:15:29 +0200
committerGitHub <noreply@github.com>2020-06-12 15:15:29 -0400
commit26bf56afdaf16634ffbaa23684faf3a44cc10f62 (patch)
tree25265007d7b09bd4580659ae34d833759df745d2
parent7d41bacfba6a3557066345ecd282f0d83e4a4381 (diff)
fix(std/io): BufWriter/StringWriter bug (#6247)
-rw-r--r--std/io/bufio.ts2
-rw-r--r--std/io/bufio_test.ts31
-rw-r--r--std/io/writers.ts9
-rw-r--r--std/io/writers_test.ts9
4 files changed, 49 insertions, 2 deletions
diff --git a/std/io/bufio.ts b/std/io/bufio.ts
index 4ebec13e1..e4759dcb7 100644
--- a/std/io/bufio.ts
+++ b/std/io/bufio.ts
@@ -486,6 +486,7 @@ export class BufWriter extends AbstractBufBase implements Writer {
this.checkBytesWritten(numBytesWritten);
+ this.buf = new Uint8Array(this.buf.length);
this.usedBufferBytes = 0;
}
@@ -580,6 +581,7 @@ export class BufWriterSync extends AbstractBufBase implements WriterSync {
this.checkBytesWritten(numBytesWritten);
+ this.buf = new Uint8Array(this.buf.length);
this.usedBufferBytes = 0;
}
diff --git a/std/io/bufio_test.ts b/std/io/bufio_test.ts
index b03640d52..92119e4db 100644
--- a/std/io/bufio_test.ts
+++ b/std/io/bufio_test.ts
@@ -18,6 +18,7 @@ import {
} from "./bufio.ts";
import * as iotest from "./_iotest.ts";
import { StringReader } from "./readers.ts";
+import { StringWriter } from "./writers.ts";
import { charCode, copyBytes } from "./util.ts";
const encoder = new TextEncoder();
@@ -469,3 +470,33 @@ Deno.test(
);
}
);
+
+Deno.test({
+ name: "Reset buffer after flush",
+ async fn(): Promise<void> {
+ const stringWriter = new StringWriter();
+ const bufWriter = new BufWriter(stringWriter);
+ const encoder = new TextEncoder();
+ await bufWriter.write(encoder.encode("hello\nworld\nhow\nare\nyou?\n\n"));
+ await bufWriter.flush();
+ await bufWriter.write(encoder.encode("foobar\n\n"));
+ await bufWriter.flush();
+ const actual = stringWriter.toString();
+ assertEquals(actual, "hello\nworld\nhow\nare\nyou?\n\nfoobar\n\n");
+ },
+});
+
+Deno.test({
+ name: "Reset buffer after flush sync",
+ fn(): void {
+ const stringWriter = new StringWriter();
+ const bufWriter = new BufWriterSync(stringWriter);
+ const encoder = new TextEncoder();
+ bufWriter.writeSync(encoder.encode("hello\nworld\nhow\nare\nyou?\n\n"));
+ bufWriter.flush();
+ bufWriter.writeSync(encoder.encode("foobar\n\n"));
+ bufWriter.flush();
+ const actual = stringWriter.toString();
+ assertEquals(actual, "hello\nworld\nhow\nare\nyou?\n\nfoobar\n\n");
+ },
+});
diff --git a/std/io/writers.ts b/std/io/writers.ts
index 8e085c84b..d42f552e8 100644
--- a/std/io/writers.ts
+++ b/std/io/writers.ts
@@ -1,9 +1,10 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
type Writer = Deno.Writer;
+type WriterSync = Deno.WriterSync;
import { decode, encode } from "../encoding/utf8.ts";
/** Writer utility for buffering string chunks */
-export class StringWriter implements Writer {
+export class StringWriter implements Writer, WriterSync {
private chunks: Uint8Array[] = [];
private byteLength = 0;
private cache: string | undefined;
@@ -15,10 +16,14 @@ export class StringWriter implements Writer {
}
write(p: Uint8Array): Promise<number> {
+ return Promise.resolve(this.writeSync(p));
+ }
+
+ writeSync(p: Uint8Array): number {
this.chunks.push(p);
this.byteLength += p.byteLength;
this.cache = undefined;
- return Promise.resolve(p.byteLength);
+ return p.byteLength;
}
toString(): string {
diff --git a/std/io/writers_test.ts b/std/io/writers_test.ts
index a50f8b3d1..f27885f81 100644
--- a/std/io/writers_test.ts
+++ b/std/io/writers_test.ts
@@ -12,3 +12,12 @@ test("ioStringWriter", async function (): Promise<void> {
await copy(r, w);
assertEquals(w.toString(), "base0123456789");
});
+
+test("ioStringWriterSync", function (): void {
+ const encoder = new TextEncoder();
+ const w = new StringWriter("");
+ w.writeSync(encoder.encode("deno"));
+ assertEquals(w.toString(), "deno");
+ w.writeSync(encoder.encode("\nland"));
+ assertEquals(w.toString(), "deno\nland");
+});