mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
BREAKING(encoding): remove module utf8.ts (#728)
This commit is contained in:
parent
06c2c8f37b
commit
5bc18f5d86
@ -12,7 +12,6 @@ import {
|
||||
startsWith,
|
||||
} from "./mod.ts";
|
||||
import { assert, assertEquals, assertThrows } from "../testing/asserts.ts";
|
||||
import { decode, encode } from "../encoding/utf8.ts";
|
||||
|
||||
Deno.test("[bytes] indexOf1", () => {
|
||||
const i = indexOf(
|
||||
@ -28,7 +27,8 @@ Deno.test("[bytes] indexOf2", () => {
|
||||
});
|
||||
|
||||
Deno.test("[bytes] indexOf3", () => {
|
||||
const i = indexOf(encode("Deno"), encode("D"));
|
||||
const encoder = new TextEncoder();
|
||||
const i = indexOf(encoder.encode("Deno"), encoder.encode("D"));
|
||||
assertEquals(i, 0);
|
||||
});
|
||||
|
||||
@ -140,10 +140,11 @@ Deno.test("[bytes] repeat", () => {
|
||||
});
|
||||
|
||||
Deno.test("[bytes] concat", () => {
|
||||
const u1 = encode("Hello ");
|
||||
const u2 = encode("World");
|
||||
const encoder = new TextEncoder();
|
||||
const u1 = encoder.encode("Hello ");
|
||||
const u2 = encoder.encode("World");
|
||||
const joined = concat(u1, u2);
|
||||
assertEquals(decode(joined), "Hello World");
|
||||
assertEquals(new TextDecoder().decode(joined), "Hello World");
|
||||
assert(u1 !== joined);
|
||||
assert(u2 !== joined);
|
||||
});
|
||||
@ -158,21 +159,23 @@ Deno.test("[bytes] concat empty arrays", () => {
|
||||
});
|
||||
|
||||
Deno.test("[bytes] concat multiple arrays", () => {
|
||||
const u1 = encode("Hello ");
|
||||
const u2 = encode("W");
|
||||
const u3 = encode("o");
|
||||
const u4 = encode("r");
|
||||
const u5 = encode("l");
|
||||
const u6 = encode("d");
|
||||
const encoder = new TextEncoder();
|
||||
const u1 = encoder.encode("Hello ");
|
||||
const u2 = encoder.encode("W");
|
||||
const u3 = encoder.encode("o");
|
||||
const u4 = encoder.encode("r");
|
||||
const u5 = encoder.encode("l");
|
||||
const u6 = encoder.encode("d");
|
||||
const joined = concat(u1, u2, u3, u4, u5, u6);
|
||||
assertEquals(decode(joined), "Hello World");
|
||||
assertEquals(new TextDecoder().decode(joined), "Hello World");
|
||||
assert(u1 !== joined);
|
||||
assert(u2 !== joined);
|
||||
});
|
||||
|
||||
Deno.test("[bytes] contains", () => {
|
||||
const source = encode("deno.land");
|
||||
const pattern = encode("deno");
|
||||
const encoder = new TextEncoder();
|
||||
const source = encoder.encode("deno.land");
|
||||
const pattern = encoder.encode("deno");
|
||||
assert(contains(source, pattern));
|
||||
|
||||
assert(contains(new Uint8Array([0, 1, 2, 3]), new Uint8Array([2, 3])));
|
||||
|
@ -1,17 +0,0 @@
|
||||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
/** A default TextEncoder instance */
|
||||
export const encoder = new TextEncoder();
|
||||
|
||||
/** Shorthand for new TextEncoder().encode() */
|
||||
export function encode(input?: string): Uint8Array {
|
||||
return encoder.encode(input);
|
||||
}
|
||||
|
||||
/** A default TextDecoder instance */
|
||||
export const decoder = new TextDecoder();
|
||||
|
||||
/** Shorthand for new TextDecoder().decode() */
|
||||
export function decode(input?: Uint8Array): string {
|
||||
return decoder.decode(input);
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||
import { xeval } from "./xeval.ts";
|
||||
import { StringReader } from "../io/readers.ts";
|
||||
import { decode, encode } from "../encoding/utf8.ts";
|
||||
import {
|
||||
assert,
|
||||
assertEquals,
|
||||
@ -49,10 +48,10 @@ Deno.test({
|
||||
stderr: "null",
|
||||
});
|
||||
assert(p.stdin != null);
|
||||
await p.stdin.write(encode("hello"));
|
||||
await p.stdin.write(new TextEncoder().encode("hello"));
|
||||
p.stdin.close();
|
||||
assertEquals(await p.status(), { code: 0, success: true });
|
||||
assertEquals(decode(await p.output()).trimEnd(), "hello");
|
||||
assertEquals(new TextDecoder().decode(await p.output()).trimEnd(), "hello");
|
||||
p.close();
|
||||
},
|
||||
});
|
||||
@ -65,8 +64,9 @@ Deno.test("xevalCliSyntaxError", async function (): Promise<void> {
|
||||
stdout: "piped",
|
||||
stderr: "piped",
|
||||
});
|
||||
const decoder = new TextDecoder();
|
||||
assertEquals(await p.status(), { code: 1, success: false });
|
||||
assertEquals(decode(await p.output()), "");
|
||||
assertStringIncludes(decode(await p.stderrOutput()), "SyntaxError");
|
||||
assertEquals(decoder.decode(await p.output()), "");
|
||||
assertStringIncludes(decoder.decode(await p.stderrOutput()), "SyntaxError");
|
||||
p.close();
|
||||
});
|
||||
|
@ -1,5 +1,4 @@
|
||||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||
import { decode } from "../encoding/utf8.ts";
|
||||
import {
|
||||
assert,
|
||||
assertEquals,
|
||||
@ -130,10 +129,11 @@ Deno.test("expandGlobPermError", async function (): Promise<void> {
|
||||
stdout: "piped",
|
||||
stderr: "piped",
|
||||
});
|
||||
const decoder = new TextDecoder();
|
||||
assertEquals(await p.status(), { code: 1, success: false });
|
||||
assertEquals(decode(await p.output()), "");
|
||||
assertEquals(decoder.decode(await p.output()), "");
|
||||
assertStringIncludes(
|
||||
decode(await p.stderrOutput()),
|
||||
decoder.decode(await p.stderrOutput()),
|
||||
"Uncaught PermissionDenied",
|
||||
);
|
||||
p.close();
|
||||
|
@ -2,7 +2,6 @@
|
||||
import { BufReader, BufWriter } from "../io/bufio.ts";
|
||||
import { TextProtoReader } from "../textproto/mod.ts";
|
||||
import { assert } from "../_util/assert.ts";
|
||||
import { encoder } from "../encoding/utf8.ts";
|
||||
import { Response, ServerRequest } from "./server.ts";
|
||||
import { STATUS_TEXT } from "./http_status.ts";
|
||||
|
||||
@ -174,6 +173,7 @@ export async function writeChunkedBody(
|
||||
w: BufWriter,
|
||||
r: Deno.Reader,
|
||||
): Promise<void> {
|
||||
const encoder = new TextEncoder();
|
||||
for await (const chunk of Deno.iter(r)) {
|
||||
if (chunk.byteLength <= 0) continue;
|
||||
const start = encoder.encode(`${chunk.byteLength.toString(16)}\r\n`);
|
||||
@ -221,6 +221,7 @@ export async function writeTrailers(
|
||||
if (undeclared.length > 0) {
|
||||
throw new TypeError(`Undeclared trailers: ${Deno.inspect(undeclared)}.`);
|
||||
}
|
||||
const encoder = new TextEncoder();
|
||||
for (const [key, value] of trailers) {
|
||||
await writer.write(encoder.encode(`${key}: ${value}\r\n`));
|
||||
}
|
||||
@ -237,6 +238,7 @@ export async function writeResponse(
|
||||
const statusCode = r.status || 200;
|
||||
const statusText = STATUS_TEXT.get(statusCode);
|
||||
const writer = BufWriter.create(w);
|
||||
const encoder = new TextEncoder();
|
||||
if (!statusText) {
|
||||
throw new Deno.errors.InvalidData("Bad status code");
|
||||
}
|
||||
|
@ -14,7 +14,6 @@ import {
|
||||
writeResponse,
|
||||
writeTrailers,
|
||||
} from "./_io.ts";
|
||||
import { decode, encode } from "../encoding/utf8.ts";
|
||||
import { BufReader, ReadLineResult } from "../io/bufio.ts";
|
||||
import { Response, ServerRequest } from "./server.ts";
|
||||
import { StringReader } from "../io/readers.ts";
|
||||
@ -24,16 +23,18 @@ Deno.test("bodyReader", async () => {
|
||||
const text = "Hello, Deno";
|
||||
const r = bodyReader(
|
||||
text.length,
|
||||
new BufReader(new Deno.Buffer(encode(text))),
|
||||
new BufReader(new Deno.Buffer(new TextEncoder().encode(text))),
|
||||
);
|
||||
assertEquals(decode(await Deno.readAll(r)), text);
|
||||
assertEquals(new TextDecoder().decode(await Deno.readAll(r)), text);
|
||||
});
|
||||
|
||||
function chunkify(n: number, char: string): string {
|
||||
const v = Array.from({ length: n })
|
||||
.map(() => `${char}`)
|
||||
.join("");
|
||||
return `${n.toString(16)}\r\n${v}\r\n`;
|
||||
}
|
||||
|
||||
Deno.test("chunkedBodyReader", async () => {
|
||||
const body = [
|
||||
chunkify(3, "a"),
|
||||
@ -43,7 +44,10 @@ Deno.test("chunkedBodyReader", async () => {
|
||||
chunkify(0, ""),
|
||||
].join("");
|
||||
const h = new Headers();
|
||||
const r = chunkedBodyReader(h, new BufReader(new Deno.Buffer(encode(body))));
|
||||
const r = chunkedBodyReader(
|
||||
h,
|
||||
new BufReader(new Deno.Buffer(new TextEncoder().encode(body))),
|
||||
);
|
||||
let result: number | null;
|
||||
// Use small buffer as some chunks exceed buffer size
|
||||
const buf = new Uint8Array(5);
|
||||
@ -70,11 +74,14 @@ Deno.test("chunkedBodyReader with trailers", async () => {
|
||||
const h = new Headers({
|
||||
trailer: "deno,node",
|
||||
});
|
||||
const r = chunkedBodyReader(h, new BufReader(new Deno.Buffer(encode(body))));
|
||||
const r = chunkedBodyReader(
|
||||
h,
|
||||
new BufReader(new Deno.Buffer(new TextEncoder().encode(body))),
|
||||
);
|
||||
assertEquals(h.has("trailer"), true);
|
||||
assertEquals(h.has("deno"), false);
|
||||
assertEquals(h.has("node"), false);
|
||||
const act = decode(await Deno.readAll(r));
|
||||
const act = new TextDecoder().decode(await Deno.readAll(r));
|
||||
const exp = "aaabbbbbcccccccccccdddddddddddddddddddddd";
|
||||
assertEquals(act, exp);
|
||||
assertEquals(h.has("trailer"), false);
|
||||
@ -87,7 +94,10 @@ Deno.test("readTrailers", async () => {
|
||||
trailer: "Deno, Node",
|
||||
});
|
||||
const trailer = ["deno: land", "node: js", "", ""].join("\r\n");
|
||||
await readTrailers(h, new BufReader(new Deno.Buffer(encode(trailer))));
|
||||
await readTrailers(
|
||||
h,
|
||||
new BufReader(new Deno.Buffer(new TextEncoder().encode(trailer))),
|
||||
);
|
||||
assertEquals(h.has("trailer"), false);
|
||||
assertEquals(h.get("deno"), "land");
|
||||
assertEquals(h.get("node"), "js");
|
||||
@ -109,7 +119,7 @@ Deno.test(
|
||||
async () => {
|
||||
await readTrailers(
|
||||
h,
|
||||
new BufReader(new Deno.Buffer(encode(trailer))),
|
||||
new BufReader(new Deno.Buffer(new TextEncoder().encode(trailer))),
|
||||
);
|
||||
},
|
||||
Deno.errors.InvalidData,
|
||||
@ -371,12 +381,13 @@ Deno.test("writeResponse with trailer", async () => {
|
||||
Deno.test("writeResponseShouldNotModifyOriginHeaders", async () => {
|
||||
const headers = new Headers();
|
||||
const buf = new Deno.Buffer();
|
||||
const decoder = new TextDecoder();
|
||||
|
||||
await writeResponse(buf, { body: "foo", headers });
|
||||
assert(decode(await Deno.readAll(buf)).includes("content-length: 3"));
|
||||
assert(decoder.decode(await Deno.readAll(buf)).includes("content-length: 3"));
|
||||
|
||||
await writeResponse(buf, { body: "hello", headers });
|
||||
assert(decode(await Deno.readAll(buf)).includes("content-length: 5"));
|
||||
assert(decoder.decode(await Deno.readAll(buf)).includes("content-length: 5"));
|
||||
});
|
||||
|
||||
Deno.test("readRequestError", async function (): Promise<void> {
|
||||
|
@ -1,5 +1,4 @@
|
||||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||
import { encode } from "../encoding/utf8.ts";
|
||||
import { BufReader, BufWriter } from "../io/bufio.ts";
|
||||
import { assert } from "../_util/assert.ts";
|
||||
import { Deferred, deferred, MuxAsyncIterator } from "../async/mod.ts";
|
||||
@ -159,7 +158,7 @@ export class Server implements AsyncIterable<ServerRequest> {
|
||||
try {
|
||||
await writeResponse(writer, {
|
||||
status: 400,
|
||||
body: encode(`${error.message}\r\n\r\n`),
|
||||
body: new TextEncoder().encode(`${error.message}\r\n\r\n`),
|
||||
});
|
||||
} catch (error) {
|
||||
// The connection is broken.
|
||||
|
@ -23,7 +23,6 @@ import {
|
||||
} from "./server.ts";
|
||||
import { BufReader, BufWriter } from "../io/bufio.ts";
|
||||
import { delay } from "../async/delay.ts";
|
||||
import { decode, encode } from "../encoding/utf8.ts";
|
||||
import { mockConn } from "./_mock_conn.ts";
|
||||
import { dirname, fromFileUrl, join, resolve } from "../path/mod.ts";
|
||||
|
||||
@ -82,7 +81,7 @@ Deno.test("requestContentLength", function (): void {
|
||||
const req = new ServerRequest();
|
||||
req.headers = new Headers();
|
||||
req.headers.set("content-length", "5");
|
||||
const buf = new Deno.Buffer(encode("Hello"));
|
||||
const buf = new Deno.Buffer(new TextEncoder().encode("Hello"));
|
||||
req.r = new BufReader(buf);
|
||||
assertEquals(req.contentLength, 5);
|
||||
}
|
||||
@ -103,7 +102,7 @@ Deno.test("requestContentLength", function (): void {
|
||||
chunkOffset += chunkSize;
|
||||
}
|
||||
chunksData += "0\r\n\r\n";
|
||||
const buf = new Deno.Buffer(encode(chunksData));
|
||||
const buf = new Deno.Buffer(new TextEncoder().encode(chunksData));
|
||||
req.r = new BufReader(buf);
|
||||
assertEquals(req.contentLength, null);
|
||||
}
|
||||
@ -133,9 +132,9 @@ Deno.test("requestBodyWithContentLength", async function (): Promise<void> {
|
||||
const req = new ServerRequest();
|
||||
req.headers = new Headers();
|
||||
req.headers.set("content-length", "5");
|
||||
const buf = new Deno.Buffer(encode("Hello"));
|
||||
const buf = new Deno.Buffer(new TextEncoder().encode("Hello"));
|
||||
req.r = new BufReader(buf);
|
||||
const body = decode(await Deno.readAll(req.body));
|
||||
const body = new TextDecoder().decode(await Deno.readAll(req.body));
|
||||
assertEquals(body, "Hello");
|
||||
}
|
||||
|
||||
@ -145,9 +144,9 @@ Deno.test("requestBodyWithContentLength", async function (): Promise<void> {
|
||||
const req = new ServerRequest();
|
||||
req.headers = new Headers();
|
||||
req.headers.set("Content-Length", "5000");
|
||||
const buf = new Deno.Buffer(encode(longText));
|
||||
const buf = new Deno.Buffer(new TextEncoder().encode(longText));
|
||||
req.r = new BufReader(buf);
|
||||
const body = decode(await Deno.readAll(req.body));
|
||||
const body = new TextDecoder().decode(await Deno.readAll(req.body));
|
||||
assertEquals(body, longText);
|
||||
}
|
||||
// Handler ignored to consume body
|
||||
@ -159,7 +158,7 @@ Deno.test(
|
||||
const req = new ServerRequest();
|
||||
req.headers = new Headers();
|
||||
req.headers.set("content-length", "" + text.length);
|
||||
const tr = totalReader(new Deno.Buffer(encode(text)));
|
||||
const tr = totalReader(new Deno.Buffer(new TextEncoder().encode(text)));
|
||||
req.r = new BufReader(tr);
|
||||
req.w = new BufWriter(new Deno.Buffer());
|
||||
await req.respond({ status: 200, body: "ok" });
|
||||
@ -187,7 +186,7 @@ Deno.test(
|
||||
req.headers = new Headers();
|
||||
req.headers.set("transfer-encoding", "chunked");
|
||||
req.headers.set("trailer", "deno,node");
|
||||
const body = encode(text);
|
||||
const body = new TextEncoder().encode(text);
|
||||
const tr = totalReader(new Deno.Buffer(body));
|
||||
req.r = new BufReader(tr);
|
||||
req.w = new BufWriter(new Deno.Buffer());
|
||||
@ -220,9 +219,9 @@ Deno.test("requestBodyWithTransferEncoding", async function (): Promise<void> {
|
||||
chunkOffset += chunkSize;
|
||||
}
|
||||
chunksData += "0\r\n\r\n";
|
||||
const buf = new Deno.Buffer(encode(chunksData));
|
||||
const buf = new Deno.Buffer(new TextEncoder().encode(chunksData));
|
||||
req.r = new BufReader(buf);
|
||||
const body = decode(await Deno.readAll(req.body));
|
||||
const body = new TextDecoder().decode(await Deno.readAll(req.body));
|
||||
assertEquals(body, shortText);
|
||||
}
|
||||
|
||||
@ -243,9 +242,9 @@ Deno.test("requestBodyWithTransferEncoding", async function (): Promise<void> {
|
||||
chunkOffset += chunkSize;
|
||||
}
|
||||
chunksData += "0\r\n\r\n";
|
||||
const buf = new Deno.Buffer(encode(chunksData));
|
||||
const buf = new Deno.Buffer(new TextEncoder().encode(chunksData));
|
||||
req.r = new BufReader(buf);
|
||||
const body = decode(await Deno.readAll(req.body));
|
||||
const body = new TextDecoder().decode(await Deno.readAll(req.body));
|
||||
assertEquals(body, longText);
|
||||
}
|
||||
});
|
||||
@ -258,14 +257,14 @@ Deno.test("requestBodyReaderWithContentLength", async function (): Promise<
|
||||
const req = new ServerRequest();
|
||||
req.headers = new Headers();
|
||||
req.headers.set("content-length", "" + shortText.length);
|
||||
const buf = new Deno.Buffer(encode(shortText));
|
||||
const buf = new Deno.Buffer(new TextEncoder().encode(shortText));
|
||||
req.r = new BufReader(buf);
|
||||
const readBuf = new Uint8Array(6);
|
||||
let offset = 0;
|
||||
while (offset < shortText.length) {
|
||||
const nread = await req.body.read(readBuf);
|
||||
assert(nread !== null);
|
||||
const s = decode(readBuf.subarray(0, nread as number));
|
||||
const s = new TextDecoder().decode(readBuf.subarray(0, nread as number));
|
||||
assertEquals(shortText.substr(offset, nread as number), s);
|
||||
offset += nread as number;
|
||||
}
|
||||
@ -279,14 +278,14 @@ Deno.test("requestBodyReaderWithContentLength", async function (): Promise<
|
||||
const req = new ServerRequest();
|
||||
req.headers = new Headers();
|
||||
req.headers.set("Content-Length", "5000");
|
||||
const buf = new Deno.Buffer(encode(longText));
|
||||
const buf = new Deno.Buffer(new TextEncoder().encode(longText));
|
||||
req.r = new BufReader(buf);
|
||||
const readBuf = new Uint8Array(1000);
|
||||
let offset = 0;
|
||||
while (offset < longText.length) {
|
||||
const nread = await req.body.read(readBuf);
|
||||
assert(nread !== null);
|
||||
const s = decode(readBuf.subarray(0, nread as number));
|
||||
const s = new TextDecoder().decode(readBuf.subarray(0, nread as number));
|
||||
assertEquals(longText.substr(offset, nread as number), s);
|
||||
offset += nread as number;
|
||||
}
|
||||
@ -314,14 +313,14 @@ Deno.test("requestBodyReaderWithTransferEncoding", async function (): Promise<
|
||||
chunkOffset += chunkSize;
|
||||
}
|
||||
chunksData += "0\r\n\r\n";
|
||||
const buf = new Deno.Buffer(encode(chunksData));
|
||||
const buf = new Deno.Buffer(new TextEncoder().encode(chunksData));
|
||||
req.r = new BufReader(buf);
|
||||
const readBuf = new Uint8Array(6);
|
||||
let offset = 0;
|
||||
while (offset < shortText.length) {
|
||||
const nread = await req.body.read(readBuf);
|
||||
assert(nread !== null);
|
||||
const s = decode(readBuf.subarray(0, nread as number));
|
||||
const s = new TextDecoder().decode(readBuf.subarray(0, nread as number));
|
||||
assertEquals(shortText.substr(offset, nread as number), s);
|
||||
offset += nread as number;
|
||||
}
|
||||
@ -346,14 +345,14 @@ Deno.test("requestBodyReaderWithTransferEncoding", async function (): Promise<
|
||||
chunkOffset += chunkSize;
|
||||
}
|
||||
chunksData += "0\r\n\r\n";
|
||||
const buf = new Deno.Buffer(encode(chunksData));
|
||||
const buf = new Deno.Buffer(new TextEncoder().encode(chunksData));
|
||||
req.r = new BufReader(buf);
|
||||
const readBuf = new Uint8Array(1000);
|
||||
let offset = 0;
|
||||
while (offset < longText.length) {
|
||||
const nread = await req.body.read(readBuf);
|
||||
assert(nread !== null);
|
||||
const s = decode(readBuf.subarray(0, nread as number));
|
||||
const s = new TextDecoder().decode(readBuf.subarray(0, nread as number));
|
||||
assertEquals(longText.substr(offset, nread as number), s);
|
||||
offset += nread as number;
|
||||
}
|
||||
@ -553,9 +552,11 @@ Deno.test({
|
||||
});
|
||||
await Deno.writeAll(
|
||||
conn,
|
||||
encode("GET / HTTP/1.1\r\nmalformedHeader\r\n\r\n\r\n\r\n"),
|
||||
new TextEncoder().encode(
|
||||
"GET / HTTP/1.1\r\nmalformedHeader\r\n\r\n\r\n\r\n",
|
||||
),
|
||||
);
|
||||
const responseString = decode(await Deno.readAll(conn));
|
||||
const responseString = new TextDecoder().decode(await Deno.readAll(conn));
|
||||
assertMatch(
|
||||
responseString,
|
||||
/^HTTP\/1\.1 400 Bad Request\r\ncontent-length: \d+\r\n\r\n.*\r\n\r\n$/ms,
|
||||
@ -584,12 +585,12 @@ Deno.test({
|
||||
});
|
||||
await Deno.writeAll(
|
||||
conn,
|
||||
encode(
|
||||
new TextEncoder().encode(
|
||||
"PUT / HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\nzzzzzzz\r\nhello",
|
||||
),
|
||||
);
|
||||
await conn.closeWrite();
|
||||
const responseString = decode(await Deno.readAll(conn));
|
||||
const responseString = new TextDecoder().decode(await Deno.readAll(conn));
|
||||
assertEquals(
|
||||
responseString,
|
||||
"HTTP/1.1 200 OK\r\ncontent-length: 13\r\n\r\nHello, world!",
|
||||
@ -617,10 +618,12 @@ Deno.test({
|
||||
});
|
||||
await Deno.writeAll(
|
||||
conn,
|
||||
encode("PUT / HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nHello"),
|
||||
new TextEncoder().encode(
|
||||
"PUT / HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nHello",
|
||||
),
|
||||
);
|
||||
conn.closeWrite();
|
||||
const responseString = decode(await Deno.readAll(conn));
|
||||
const responseString = new TextDecoder().decode(await Deno.readAll(conn));
|
||||
assertEquals(
|
||||
responseString,
|
||||
"HTTP/1.1 200 OK\r\ncontent-length: 13\r\n\r\nHello, world!",
|
||||
@ -647,7 +650,7 @@ Deno.test({
|
||||
});
|
||||
await Deno.writeAll(
|
||||
conn,
|
||||
encode([
|
||||
new TextEncoder().encode([
|
||||
// A normal request is required:
|
||||
"GET / HTTP/1.1",
|
||||
"Host: localhost",
|
||||
|
@ -3,12 +3,11 @@
|
||||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
import { encode } from "../encoding/utf8.ts";
|
||||
|
||||
/** Reader utility for strings */
|
||||
export class StringReader extends Deno.Buffer {
|
||||
constructor(s: string) {
|
||||
super(encode(s).buffer);
|
||||
super(new TextEncoder().encode(s).buffer);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,6 @@ import { assertEquals } from "../testing/asserts.ts";
|
||||
import { LimitedReader, MultiReader, StringReader } from "./readers.ts";
|
||||
import { StringWriter } from "./writers.ts";
|
||||
import { copyN } from "./ioutil.ts";
|
||||
import { decode } from "../encoding/utf8.ts";
|
||||
|
||||
Deno.test("ioStringReader", async function (): Promise<void> {
|
||||
const r = new StringReader("abcdef");
|
||||
@ -14,17 +13,18 @@ Deno.test("ioStringReader", async function (): Promise<void> {
|
||||
});
|
||||
|
||||
Deno.test("ioStringReader", async function (): Promise<void> {
|
||||
const decoder = new TextDecoder();
|
||||
const r = new StringReader("abcdef");
|
||||
const buf = new Uint8Array(3);
|
||||
const res1 = await r.read(buf);
|
||||
assertEquals(res1, 3);
|
||||
assertEquals(decode(buf), "abc");
|
||||
assertEquals(decoder.decode(buf), "abc");
|
||||
const res2 = await r.read(buf);
|
||||
assertEquals(res2, 3);
|
||||
assertEquals(decode(buf), "def");
|
||||
assertEquals(decoder.decode(buf), "def");
|
||||
const res3 = await r.read(buf);
|
||||
assertEquals(res3, null);
|
||||
assertEquals(decode(buf), "def");
|
||||
assertEquals(decoder.decode(buf), "def");
|
||||
});
|
||||
|
||||
Deno.test("ioMultiReader", async function (): Promise<void> {
|
||||
@ -38,20 +38,21 @@ Deno.test("ioMultiReader", async function (): Promise<void> {
|
||||
});
|
||||
|
||||
Deno.test("ioLimitedReader", async function (): Promise<void> {
|
||||
const decoder = new TextDecoder();
|
||||
let sr = new StringReader("abc");
|
||||
let r = new LimitedReader(sr, 2);
|
||||
let buffer = await Deno.readAll(r);
|
||||
assertEquals(decode(buffer), "ab");
|
||||
assertEquals(decode(await Deno.readAll(sr)), "c");
|
||||
assertEquals(decoder.decode(buffer), "ab");
|
||||
assertEquals(decoder.decode(await Deno.readAll(sr)), "c");
|
||||
sr = new StringReader("abc");
|
||||
r = new LimitedReader(sr, 3);
|
||||
buffer = await Deno.readAll(r);
|
||||
assertEquals(decode(buffer), "abc");
|
||||
assertEquals(decoder.decode(buffer), "abc");
|
||||
assertEquals((await Deno.readAll(r)).length, 0);
|
||||
sr = new StringReader("abc");
|
||||
r = new LimitedReader(sr, 4);
|
||||
buffer = await Deno.readAll(r);
|
||||
assertEquals(decode(buffer), "abc");
|
||||
assertEquals(decoder.decode(buffer), "abc");
|
||||
assertEquals((await Deno.readAll(r)).length, 0);
|
||||
});
|
||||
|
||||
|
@ -1,7 +1,6 @@
|
||||
// Copyright 2018-2021 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, WriterSync {
|
||||
@ -10,7 +9,7 @@ export class StringWriter implements Writer, WriterSync {
|
||||
private cache: string | undefined;
|
||||
|
||||
constructor(private base: string = "") {
|
||||
const c = encode(base);
|
||||
const c = new TextEncoder().encode(base);
|
||||
this.chunks.push(c);
|
||||
this.byteLength += c.byteLength;
|
||||
}
|
||||
@ -36,7 +35,7 @@ export class StringWriter implements Writer, WriterSync {
|
||||
buf.set(chunk, offs);
|
||||
offs += chunk.byteLength;
|
||||
}
|
||||
this.cache = decode(buf);
|
||||
this.cache = new TextDecoder().decode(buf);
|
||||
return this.cache;
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ import { copyN } from "../io/ioutil.ts";
|
||||
import { MultiReader } from "../io/readers.ts";
|
||||
import { extname } from "../path/mod.ts";
|
||||
import { BufReader, BufWriter } from "../io/bufio.ts";
|
||||
import { encoder } from "../encoding/utf8.ts";
|
||||
import { assert } from "../_util/assert.ts";
|
||||
import { TextProtoReader } from "../textproto/mod.ts";
|
||||
import { hasOwnProperty } from "../_util/has_own_property.ts";
|
||||
@ -39,6 +38,8 @@ function randomBoundary(): string {
|
||||
return boundary;
|
||||
}
|
||||
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
/**
|
||||
* Checks whether `buf` should be considered to match the boundary.
|
||||
*
|
||||
|
@ -6,7 +6,6 @@
|
||||
|
||||
import type { BufReader } from "../io/bufio.ts";
|
||||
import { concat } from "../bytes/mod.ts";
|
||||
import { decode } from "../encoding/utf8.ts";
|
||||
|
||||
// FROM https://github.com/denoland/deno/blob/b34628a26ab0187a827aa4ebe256e23178e25d39/cli/js/web/headers.ts#L9
|
||||
const invalidHeaderCharRegex = /[^\t\x20-\x7e\x80-\xff]/g;
|
||||
@ -15,7 +14,7 @@ function str(buf: Uint8Array | null | undefined): string {
|
||||
if (buf == null) {
|
||||
return "";
|
||||
} else {
|
||||
return decode(buf);
|
||||
return new TextDecoder().decode(buf);
|
||||
}
|
||||
}
|
||||
|
||||
|
18
ws/mod.ts
18
ws/mod.ts
@ -1,5 +1,4 @@
|
||||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||
import { decode, encode } from "../encoding/utf8.ts";
|
||||
import { hasOwnProperty } from "../_util/has_own_property.ts";
|
||||
import { BufReader, BufWriter } from "../io/bufio.ts";
|
||||
import { readLong, readShort, sliceLongToBytes } from "../io/ioutil.ts";
|
||||
@ -222,6 +221,7 @@ class WebSocketImpl implements WebSocket {
|
||||
}
|
||||
|
||||
async *[Symbol.asyncIterator](): AsyncIterableIterator<WebSocketEvent> {
|
||||
const decoder = new TextDecoder();
|
||||
let frames: WebSocketFrame[] = [];
|
||||
let payloadsLength = 0;
|
||||
while (!this._isClosed) {
|
||||
@ -248,7 +248,7 @@ class WebSocketImpl implements WebSocket {
|
||||
}
|
||||
if (frames[0].opcode === OpCode.TextFrame) {
|
||||
// text
|
||||
yield decode(concat);
|
||||
yield decoder.decode(concat);
|
||||
} else {
|
||||
// binary
|
||||
yield concat;
|
||||
@ -260,7 +260,7 @@ class WebSocketImpl implements WebSocket {
|
||||
case OpCode.Close: {
|
||||
// [0x12, 0x34] -> 0x1234
|
||||
const code = (frame.payload[0] << 8) | frame.payload[1];
|
||||
const reason = decode(
|
||||
const reason = decoder.decode(
|
||||
frame.payload.subarray(2, frame.payload.length),
|
||||
);
|
||||
await this.close(code, reason);
|
||||
@ -313,7 +313,9 @@ class WebSocketImpl implements WebSocket {
|
||||
const opcode = typeof data === "string"
|
||||
? OpCode.TextFrame
|
||||
: OpCode.BinaryFrame;
|
||||
const payload = typeof data === "string" ? encode(data) : data;
|
||||
const payload = typeof data === "string"
|
||||
? new TextEncoder().encode(data)
|
||||
: data;
|
||||
const isLastFrame = true;
|
||||
const frame = {
|
||||
isLastFrame,
|
||||
@ -325,7 +327,9 @@ class WebSocketImpl implements WebSocket {
|
||||
}
|
||||
|
||||
ping(data: WebSocketMessage = ""): Promise<void> {
|
||||
const payload = typeof data === "string" ? encode(data) : data;
|
||||
const payload = typeof data === "string"
|
||||
? new TextEncoder().encode(data)
|
||||
: data;
|
||||
const frame = {
|
||||
isLastFrame: true,
|
||||
opcode: OpCode.Ping,
|
||||
@ -345,7 +349,7 @@ class WebSocketImpl implements WebSocket {
|
||||
const header = [code >>> 8, code & 0x00ff];
|
||||
let payload: Uint8Array;
|
||||
if (reason) {
|
||||
const reasonBytes = encode(reason);
|
||||
const reasonBytes = new TextEncoder().encode(reason);
|
||||
payload = new Uint8Array(2 + reasonBytes.byteLength);
|
||||
payload.set(header);
|
||||
payload.set(reasonBytes, 2);
|
||||
@ -484,7 +488,7 @@ export async function handshake(
|
||||
}
|
||||
headerStr += "\r\n";
|
||||
|
||||
await bufWriter.write(encode(headerStr));
|
||||
await bufWriter.write(new TextEncoder().encode(headerStr));
|
||||
await bufWriter.flush();
|
||||
|
||||
const tpReader = new TextProtoReader(bufReader);
|
||||
|
17
ws/test.ts
17
ws/test.ts
@ -20,7 +20,6 @@ import {
|
||||
unmask,
|
||||
writeFrame,
|
||||
} from "./mod.ts";
|
||||
import { decode, encode } from "../encoding/utf8.ts";
|
||||
import { delay } from "../async/delay.ts";
|
||||
import { serve } from "../http/server.ts";
|
||||
import { deferred } from "../async/deferred.ts";
|
||||
@ -211,7 +210,7 @@ Deno.test("[ws] write and read masked frame", async () => {
|
||||
isLastFrame: true,
|
||||
mask,
|
||||
opcode: OpCode.TextFrame,
|
||||
payload: encode(msg),
|
||||
payload: new TextEncoder().encode(msg),
|
||||
},
|
||||
buf,
|
||||
);
|
||||
@ -220,12 +219,12 @@ Deno.test("[ws] write and read masked frame", async () => {
|
||||
assertEquals(frame.isLastFrame, true);
|
||||
assertEquals(frame.mask, mask);
|
||||
unmask(frame.payload, frame.mask);
|
||||
assertEquals(frame.payload, encode(msg));
|
||||
assertEquals(frame.payload, new TextEncoder().encode(msg));
|
||||
});
|
||||
|
||||
Deno.test("[ws] handshake should not send search when it's empty", async () => {
|
||||
const writer = new Deno.Buffer();
|
||||
const reader = new Deno.Buffer(encode("HTTP/1.1 400\r\n"));
|
||||
const reader = new Deno.Buffer(new TextEncoder().encode("HTTP/1.1 400\r\n"));
|
||||
|
||||
await assertThrowsAsync(
|
||||
async (): Promise<void> => {
|
||||
@ -248,7 +247,9 @@ Deno.test(
|
||||
"[ws] handshake should send search correctly",
|
||||
async function wsHandshakeWithSearch(): Promise<void> {
|
||||
const writer = new Deno.Buffer();
|
||||
const reader = new Deno.Buffer(encode("HTTP/1.1 400\r\n"));
|
||||
const reader = new Deno.Buffer(
|
||||
new TextEncoder().encode("HTTP/1.1 400\r\n"),
|
||||
);
|
||||
|
||||
await assertThrowsAsync(
|
||||
async (): Promise<void> => {
|
||||
@ -322,9 +323,9 @@ Deno.test({
|
||||
const ping = await readFrame(bufr);
|
||||
const third = await readFrame(bufr);
|
||||
assertEquals(first.opcode, OpCode.TextFrame);
|
||||
assertEquals(decode(first.payload), "first");
|
||||
assertEquals(new TextDecoder().decode(first.payload), "first");
|
||||
assertEquals(first.opcode, OpCode.TextFrame);
|
||||
assertEquals(decode(second.payload), "second");
|
||||
assertEquals(new TextDecoder().decode(second.payload), "second");
|
||||
assertEquals(ping.opcode, OpCode.Ping);
|
||||
assertEquals(third.opcode, OpCode.BinaryFrame);
|
||||
assertEquals(bytes.equals(third.payload, new Uint8Array([3])), true);
|
||||
@ -458,7 +459,7 @@ Deno.test("[ws] WebSocket should act as asyncIterator", async () => {
|
||||
}
|
||||
|
||||
assertEquals(events.length, 3);
|
||||
assertEquals(events[0], ["ping", encode("Hello")]);
|
||||
assertEquals(events[0], ["ping", new TextEncoder().encode("Hello")]);
|
||||
assertEquals(events[1], "Hello");
|
||||
assertEquals(events[2], { code: 1011, reason: "42" });
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user