Skip to content

Commit

Permalink
use request parser from std library
Browse files Browse the repository at this point in the history
  • Loading branch information
afinch7 committed Jul 23, 2020
1 parent 24ade9d commit 27b3425
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 88 deletions.
28 changes: 19 additions & 9 deletions core/app.ts
@@ -1,7 +1,9 @@
import { RequestParser } from '../http/parser.ts';
import { readRequest, BufReader } from '../deps.ts';
import { Type } from '../decorators/module.ts';
import { MODULE_METADATA, PATH_METADATA } from '../common/constants.ts';

const decoder = new TextDecoder();

export class ReverbApplication {
constructor(appModule: Type<any>) {
const controllers = Reflect.getMetadata(MODULE_METADATA.CONTROLLERS, appModule)
Expand All @@ -22,16 +24,24 @@ export class ReverbApplication {
}

async handle(conn: Deno.Conn): Promise<void> {
const buffer = new Uint8Array(1024);
try {
while (true) {
const r = await conn.read(buffer);
if (r === null) {
break;
}
const parsedRequest = RequestParser.parseRequest(buffer);
await conn.write(this.response);
const reader = new BufReader(conn);
const parsedRequest = await readRequest(conn, reader);
if (parsedRequest == null) {
throw "request is null?";
}
let bodyText = "";
const bodyReader = new BufReader(parsedRequest.body);
let lineRes = await bodyReader.readLine();
while (lineRes != null) {
const lineText = decoder.decode(lineRes?.line);
bodyText += lineText + "\n";
lineRes = await bodyReader.readLine();
}
console.log({
body: bodyText
});
await conn.write(this.response);
} finally {
conn.close();
}
Expand Down
3 changes: 3 additions & 0 deletions deps.ts
@@ -0,0 +1,3 @@
export { readRequest } from "https://deno.land/std/http/_io.ts"
export { ServerRequest } from "https://deno.land/std/http/mod.ts";
export { BufReader } from "https://deno.land/std/io/mod.ts";
79 changes: 0 additions & 79 deletions http/parser.ts

This file was deleted.

0 comments on commit 27b3425

Please sign in to comment.