feat: initial commit

This commit is contained in:
2024-07-06 21:21:10 +02:00
commit 96f7c2cfe7
21 changed files with 1511 additions and 0 deletions

40
http/libraries/client.ts Normal file
View File

@@ -0,0 +1,40 @@
import { Request, type RequestMethod } from "./request.ts";
import { type Response } from "./response.ts";
export class Client {
constructor(readonly options: Deno.ConnectOptions | Deno.UnixConnectOptions) {}
/**
* Connection instance to use for a new fetch operation.
*
* Note! A new connection is spawned for every fetch request and is only automatically
* closed when accessing the .stream on the response. Otherwise a manual .close must
* be executed on the response to ensure that the connection is cleaned up.
*/
get connection() {
if ("path" in this.options) {
return Deno.connect(this.options);
}
return Deno.connect(this.options);
}
async fetch(path: string, { method, headers = {}, body }: RequestOptions): Promise<Response> {
const url = new URL(path);
return new Request(await this.connection, {
method,
path: url.pathname + url.search,
headers: {
Host: url.host,
"Content-Type": "application/json",
...headers,
},
body: body ? JSON.stringify(body) : undefined,
}).send();
}
}
type RequestOptions = {
method: RequestMethod;
headers?: RequestInit["headers"];
body?: Record<string, unknown>;
};

3
http/libraries/common.ts Normal file
View File

@@ -0,0 +1,3 @@
export const NEW_LINE = "\r\n";
export const PROTOCOL = "HTTP/1.1";

43
http/libraries/request.ts Normal file
View File

@@ -0,0 +1,43 @@
import { NEW_LINE, PROTOCOL } from "./common.ts";
import { Response } from "./response.ts";
export class Request {
constructor(readonly connection: Deno.Conn, readonly options: RequestOptions) {}
async send(): Promise<Response> {
const http = await this.encode(this.toHttp());
await this.connection.write(http);
return new Response(this.connection).resolve();
}
toHttp() {
const { method, path, headers = {}, body } = this.options;
const parts: string[] = [
`${method} ${path} ${PROTOCOL}`,
];
for (const key in headers) {
parts.push(`${key}: ${(headers as any)[key]}`);
}
if (body !== undefined) {
parts.push(`Content-Length: ${body.length}`);
}
return `${parts.join(NEW_LINE)}${NEW_LINE}${NEW_LINE}${body ?? ""}`;
}
async encode(value: string): Promise<Uint8Array> {
return new TextEncoder().encode(value);
}
async decode(buffer: Uint8Array): Promise<string> {
return new TextDecoder().decode(buffer);
}
}
export type RequestOptions = {
method: RequestMethod;
path: string;
headers?: RequestInit["headers"];
body?: string;
};
export type RequestMethod = "HEAD" | "OPTIONS" | "POST" | "GET" | "PUT" | "DELETE";

151
http/libraries/response.ts Normal file
View File

@@ -0,0 +1,151 @@
import { PROTOCOL } from "./common.ts";
export class Response {
status: number = 500;
headers = new Map<string, string>();
body = "";
constructor(readonly connection: Deno.Conn) {}
/*
|--------------------------------------------------------------------------------
| Accessors
|--------------------------------------------------------------------------------
*/
/**
* ReadableStream of the HTTP response.
*/
get stream(): ReadableStream<string> {
let isCancelled = false;
return new ReadableStream({
start: (controller) => {
const push = () => {
this.#readLine().then((line) => {
const size = parseInt(line, 16);
if (size === 0 || isCancelled === true) {
if (size === 0 && isCancelled === false) {
controller.close();
}
return this.connection.close();
}
controller.enqueue(line);
push();
});
};
push();
},
cancel: () => {
isCancelled = true;
},
});
}
/**
* Parsed JSON instance of the response body.
*/
get json() {
if (this.body === "") {
return {};
}
return JSON.parse(this.body);
}
/*
|--------------------------------------------------------------------------------
| Resolver
|--------------------------------------------------------------------------------
*/
/**
* Resolve the current response by reading the connection buffer and extracting
* the head, headers and body.
*/
async resolve(): Promise<this> {
await this.#readHead();
await this.#readHeader();
if (this.headers.get("Content-Type") === "application/json") {
await this.#readBody();
}
return this;
}
async #readHead() {
const [protocol, statusCode] = await this.#readLine().then((head) => head.split(" "));
if (protocol !== PROTOCOL) {
throw new Error(`HttpResponse > Unknown protocol ${protocol} received.`);
}
this.status = parseInt(statusCode, 10);
}
async #readHeader() {
const header = await this.#readLine();
if (header === "") {
return;
}
const [key, value] = header.split(":");
this.headers.set(key.trim(), value.trim());
await this.#readHeader();
}
async #readBody() {
if (this.headers.get("Transfer-Encoding") === "chunked") {
while (true) {
const line = await this.#readLine();
const size = parseInt(line, 16);
if (size === 0) {
return;
}
const buf = new ArrayBuffer(size);
const arr = new Uint8Array(buf);
await this.connection.read(arr);
this.body += await this.#decode(arr);
}
} else if (this.headers.has("Content-Length") === true) {
const size = parseInt(this.headers.get("Content-Length")!, 10);
const buf = new ArrayBuffer(size);
const arr = new Uint8Array(buf);
await this.connection.read(arr);
this.body += await this.#decode(arr);
}
}
async #readLine(): Promise<string> {
let result = "";
while (true) {
const buffer = new Uint8Array(1);
if (result.indexOf("\n") !== -1) {
return result.slice(0, result.length - 2); // return the full line without the \n flag
}
await this.connection.read(buffer);
result += await this.#decode(buffer);
}
}
/*
|--------------------------------------------------------------------------------
| Lifecycle
|--------------------------------------------------------------------------------
*/
/**
* Close the connection that was used to produce the current response instance.
*
* Note! If the response is not closed an active connection may remain open
* causing unclean shutdown of processes.
*/
close(): this {
this.connection.close();
return this;
}
/*
|--------------------------------------------------------------------------------
| Utilities
|--------------------------------------------------------------------------------
*/
async #decode(value: Uint8Array): Promise<string> {
return new TextDecoder().decode(value);
}
}

2
http/mod.ts Normal file
View File

@@ -0,0 +1,2 @@
export * from "./libraries/client.ts";
export * from "./libraries/response.ts";