Skip to content

Commit

Permalink
fix: Use Buffer from deno.land/std/io/buffer.ts instead (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
gamoutatsumi authored Apr 15, 2021
1 parent 56764bb commit 834074b
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 23 deletions.
3 changes: 2 additions & 1 deletion _readers_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { streamReader } from "./_readers.ts";
import { assertEquals } from "./vendor/https/deno.land/std/testing/asserts.ts";
import { group } from "./_test_util.ts";
import { decode, encode } from "./_util.ts";
import { Buffer } from "./vendor/https/deno.land/std/io/buffer.ts";

group("streamReader", ({ test }) => {
test("basic", async () => {
Expand All @@ -16,7 +17,7 @@ group("streamReader", ({ test }) => {
});
const sr = streamReader(stream);
const buf = new Uint8Array(3);
const dest = new Deno.Buffer();
const dest = new Buffer();
let result: null | number = 0;
while ((result = await sr.read(buf)) !== null) {
await dest.write(buf.subarray(0, result));
Expand Down
11 changes: 6 additions & 5 deletions body_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
MultipartReader,
} from "./vendor/https/deno.land/std/mime/multipart.ts";
import { decode } from "./_util.ts";
import { Buffer } from "./vendor/https/deno.land/std/io/buffer.ts";

import Reader = Deno.Reader;
export interface BodyParser {
Expand All @@ -18,7 +19,7 @@ export function createBodyParser(holder: {
readonly contentType: string;
readonly maxMemory?: number;
}): BodyParser {
let bodyBuf: Deno.Buffer | undefined;
let bodyBuf: Buffer | undefined;
let formBody: MultipartFormData | undefined;
let textBody: string | undefined;
let jsonBody: any | undefined;
Expand Down Expand Up @@ -67,7 +68,7 @@ export function createBodyParser(holder: {
} else if (bodyBuf) {
return (jsonBody = JSON.parse(decode(bodyBuf.bytes())));
}
bodyBuf = new Deno.Buffer();
bodyBuf = new Buffer();
await Deno.copy(holder.reader, bodyBuf);
return JSON.parse(decode(bodyBuf.bytes()));
}
Expand All @@ -78,7 +79,7 @@ export function createBodyParser(holder: {
} else if (bodyBuf) {
return (textBody = decode(bodyBuf.bytes()));
}
bodyBuf = new Deno.Buffer();
bodyBuf = new Buffer();
await Deno.copy(holder.reader, bodyBuf);
return (textBody = decode(bodyBuf.bytes()));
}
Expand All @@ -87,7 +88,7 @@ export function createBodyParser(holder: {
if (bodyBuf) {
return bodyBuf.bytes();
}
bodyBuf = new Deno.Buffer();
bodyBuf = new Buffer();
await Deno.copy(holder.reader, bodyBuf);
return bodyBuf.bytes();
}
Expand Down Expand Up @@ -138,7 +139,7 @@ export async function parseUrlEncodedForm(req: {
) {
throw new Error("is not form urlencoded request");
}
const buf = new Deno.Buffer();
const buf = new Buffer();
await Deno.copy(req.body, buf);
const params = new URLSearchParams(decodeURIComponent(decode(buf.bytes())));
function* entries() {
Expand Down
4 changes: 2 additions & 2 deletions body_parser_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "./vendor/https/deno.land/std/testing/asserts.ts";
import { createBodyParser, parserMultipartRequest } from "./body_parser.ts";
import { exists as fsExists } from "./vendor/https/deno.land/std/fs/exists.ts";
import Buffer = Deno.Buffer;
import { Buffer } from "./vendor/https/deno.land/std/io/buffer.ts";
import { group } from "./_test_util.ts";
import { StringReader } from "./vendor/https/deno.land/std/io/readers.ts";

Expand Down Expand Up @@ -117,7 +117,7 @@ group("bodyParser", ({ test }) => {
);
});
test("arrayBuffer()", async () => {
const bin = new Deno.Buffer(new Uint8Array([0, 1, 2, 3]));
const bin = new Buffer(new Uint8Array([0, 1, 2, 3]));
const br = createBodyParser(
{ reader: bin, contentType: "application/octet-stream" },
);
Expand Down
1 change: 1 addition & 0 deletions modules-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"/io/bufio.ts",
"/io/readers.ts",
"/io/writers.ts",
"/io/buffer.ts",
"/http/_io.ts",
"/http/http_status.ts",
"/flags/mod.ts",
Expand Down
1 change: 1 addition & 0 deletions modules.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"/io/bufio.ts",
"/io/readers.ts",
"/io/writers.ts",
"/io/buffer.ts",
"/http/_io.ts",
"/http/http_status.ts",
"/flags/mod.ts",
Expand Down
21 changes: 11 additions & 10 deletions responder_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import {
import { StringReader } from "./vendor/https/deno.land/std/io/readers.ts";
import { readResponse, writeResponse } from "./serveio.ts";
import { group } from "./_test_util.ts";
import { Buffer } from "./vendor/https/deno.land/std/io/buffer.ts";

group("responder", (t) => {
function _createResponder(w: Deno.Writer) {
return createResponder((resp) => writeResponse(w, resp));
}
t.test("basic", async function () {
const w = new Deno.Buffer();
const w = new Buffer();
const res = _createResponder(w);
assert(!res.isResponded());
await res.respond({
Expand All @@ -32,7 +33,7 @@ group("responder", (t) => {
});

t.test("respond() should throw if already responded", async function () {
const w = new Deno.Buffer();
const w = new Buffer();
const res = _createResponder(w);
await res.respond({
status: 200,
Expand All @@ -50,7 +51,7 @@ group("responder", (t) => {
});

t.test("sendFile() basic", async function () {
const w = new Deno.Buffer();
const w = new Buffer();
const res = _createResponder(w);
await res.sendFile("./fixtures/sample.txt");
const resp = await readResponse(w);
Expand All @@ -60,7 +61,7 @@ group("responder", (t) => {
});

t.test("sendFile() should throw if file not found", async () => {
const w = new Deno.Buffer();
const w = new Buffer();
const res = _createResponder(w);
await assertThrowsAsync(
() => res.sendFile("./fixtures/not-found"),
Expand All @@ -69,7 +70,7 @@ group("responder", (t) => {
});

t.test("sendFile() with attachment", async () => {
const w = new Deno.Buffer();
const w = new Buffer();
const res = _createResponder(w);
await res.sendFile("./fixtures/sample.txt", {
contentDisposition: "inline",
Expand All @@ -81,7 +82,7 @@ group("responder", (t) => {
});

t.test("sendFile() with attachment", async () => {
const w = new Deno.Buffer();
const w = new Buffer();
const res = _createResponder(w);
await res.sendFile("./fixtures/sample.txt", {
contentDisposition: "attachment",
Expand All @@ -96,7 +97,7 @@ group("responder", (t) => {
});

t.test("redirect() should set Location header", async () => {
const w = new Deno.Buffer();
const w = new Buffer();
const res = _createResponder(w);
await res.redirect("/index.html");
const { status, headers } = await readResponse(w);
Expand All @@ -105,7 +106,7 @@ group("responder", (t) => {
});

t.test("redirect() should use partial body for response", async () => {
const w = new Deno.Buffer();
const w = new Buffer();
const res = _createResponder(w);
await res.redirect("/", {
status: 303,
Expand All @@ -119,7 +120,7 @@ group("responder", (t) => {
});

t.test("resirect() should throw error if status code is not in 300~399", async () => {
const w = new Deno.Buffer();
const w = new Buffer();
const res = _createResponder(w);
await assertThrowsAsync(
async () => {
Expand All @@ -131,7 +132,7 @@ group("responder", (t) => {
});

t.test("markResponded()", async () => {
const w = new Deno.Buffer();
const w = new Buffer();
const res = _createResponder(w);
res.markAsResponded(200);
assertEquals(res.isResponded(), true);
Expand Down
2 changes: 1 addition & 1 deletion serveio.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2019-2020 Yusuke Sakurai. All rights reserved. MIT license.
import { BufReader, BufWriter } from "./vendor/https/deno.land/std/io/bufio.ts";
import { Buffer } from "./vendor/https/deno.land/std/io/buffer.ts";
import { TextProtoReader } from "./vendor/https/deno.land/std/textproto/mod.ts";
import { closableBodyReader, streamReader, timeoutReader } from "./_readers.ts";
import { encode, promiseInterrupter } from "./_util.ts";
Expand All @@ -19,7 +20,6 @@ import {
} from "./server.ts";
import Reader = Deno.Reader;
import Writer = Deno.Writer;
import Buffer = Deno.Buffer;
import { toIMF } from "./vendor/https/deno.land/std/datetime/mod.ts";
import { parseCookie } from "./cookie.ts";
import {
Expand Down
2 changes: 1 addition & 1 deletion serveio_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { assertEquals } from "./vendor/https/deno.land/std/testing/asserts.ts";
import { StringReader } from "./vendor/https/deno.land/std/io/readers.ts";
import { encode } from "./_util.ts";
import Buffer = Deno.Buffer;
import { Buffer } from "./vendor/https/deno.land/std/io/buffer.ts";
import { ServerResponse } from "./server.ts";
import { group } from "./_test_util.ts";
import { noopReader } from "./_readers.ts";
Expand Down
2 changes: 1 addition & 1 deletion server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { createAgent } from "./agent.ts";
import { readResponse, writeRequest } from "./serveio.ts";
import { BufReader } from "./vendor/https/deno.land/std/io/bufio.ts";
import { deferred, delay } from "./vendor/https/deno.land/std/async/mod.ts";
import Buffer = Deno.Buffer;
import { Buffer } from "./vendor/https/deno.land/std/io/buffer.ts";
import { group } from "./_test_util.ts";

let port = 8880;
Expand Down
3 changes: 2 additions & 1 deletion site/public/example/comparison/body/std_http.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { serve } from "https://deno.land/std/http/mod.ts";
import { Buffer } from "https://deno.land/std/io/buffer.ts";

const server = serve({ port: 8888 });
for await (const req of server) {
if (
req.method === "POST" &&
req.url === "/post"
) {
const buf = new Deno.Buffer();
const buf = new Buffer();
await Deno.copy(req.body, buf);
const decoder = new TextDecoder();
const str = decoder.decode(buf.bytes());
Expand Down
3 changes: 2 additions & 1 deletion testing.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2019-2020 Yusuke Sakurai. All rights reserved. MIT license.
import { BufReader, BufWriter } from "./vendor/https/deno.land/std/io/bufio.ts";
import { Buffer } from "./vendor/https/deno.land/std/io/buffer.ts";
import {
BodyReader,
HttpBody,
Expand Down Expand Up @@ -50,7 +51,7 @@ export function createRecorder(opts?: {
return 0;
},
};
const buf = new Deno.Buffer();
const buf = new Buffer();
const bufReader = new BufReader(buf);
const bufWriter = new BufWriter(buf);
let br: BodyReader;
Expand Down
1 change: 1 addition & 0 deletions vendor/https/deno.land/std/io/buffer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "https://deno.land/[email protected]/io/buffer.ts";

0 comments on commit 834074b

Please sign in to comment.