Skip to content

Instantly share code, notes, and snippets.

@mayankchoubey
mayankchoubey / deno_beginner_http_server.ts
Last active January 3, 2022 07:11
A beginner HTTP server in Deno
import { serve } from "https://deno.land/std/http/mod.ts";
async function reqHandler(req: Request) {
if (
!req.headers.has("Authorization") ||
req.headers.get("Authorization")?.split(" ")[1] !==
Deno.env.get("AUTH_TOKEN")
) {
return new Response(null, { status: 401 });
}
@mayankchoubey
mayankchoubey / deno_aes_encryption_decryption.ts
Created November 9, 2021 21:59
AES encryption & decryption in Deno
import {
decode as hd,
encode as he,
} from "https://deno.land/std/encoding/hex.ts";
const te = (s: string) => new TextEncoder().encode(s),
td = (d: Uint8Array) => new TextDecoder().decode(d);
const rawKey = new Uint8Array([
238,
17,
@mayankchoubey
mayankchoubey / deno_content_server_direct.ts
Last active January 3, 2022 07:15
Deno content server by reading files directly
import { readableStreamFromReader as toStream } from "https://deno.land/std/streams/mod.ts";
import { serve } from "https://deno.land/std/http/mod.ts";
const BASE_PATH = "./testdata";
const reqHandler = async (req: Request) => {
const p = new URL(req.url).pathname;
const filePath = BASE_PATH + p;
let len = 0;
try {
@mayankchoubey
mayankchoubey / deno_content_server_fetch.ts
Last active January 3, 2022 07:16
Deno content server by reading local files using fetch
import { serve } from "https://deno.land/std/http/mod.ts";
const BASE_PATH = "file:///var/tmp/testdata";
const reqHandler = async (req: Request) => {
const p = new URL(req.url).pathname;
const filePath = BASE_PATH + p;
try {
const res = await fetch(filePath);
return new Response(res.body);
@mayankchoubey
mayankchoubey / deno_template_hello_name.ts
Last active January 3, 2022 07:19
Deno hello name using ETA templating
import * as Eta from "https://deno.land/x/eta/mod.ts";
import { serve } from "https://deno.land/std/http/mod.ts";
Eta.configure({ views: "./" });
const getName = (u: URL) => u.searchParams.get("name") || "James Bond 007";
const reqHandler = async (req: Request) => {
try {
const u = new URL(req.url);
@mayankchoubey
mayankchoubey / authService.ts
Last active January 3, 2022 07:21
Deno content server - Medium course - section 1
import { API_KEYS_PATH } from "./consts.ts";
let apiKeys: Array<string>=[];
try {
apiKeys=JSON.parse(Deno.readTextFileSync(API_KEYS_PATH));
} catch (e) {}
export function authorize(headers: Headers) {
const authorized=true, notAuthorized=false;
if (!apiKeys.length)
@mayankchoubey
mayankchoubey / authService.ts
Last active January 3, 2022 07:23
Deno content server - Medium course - section 1
import { API_KEYS_PATH } from "./consts.ts";
let apiKeys: Array<string> = [];
try {
apiKeys = JSON.parse(Deno.readTextFileSync(API_KEYS_PATH));
} catch (_e) {
console.info("No API keys found, authentication is disabled");
}
export function authorize(headers: Headers) {
@mayankchoubey
mayankchoubey / authService_tests.ts
Created November 26, 2021 20:09
Deno content server - medium course - section 3 authorize UT
import { authorize } from "../authService.ts";
import { assert, assertExists } from "https://deno.land/std/testing/asserts.ts";
Deno.test("No headers", async () => {
const headers = new Headers();
const ret = authorize(headers);
assertExists(ret);
assert(ret === false);
});
@mayankchoubey
mayankchoubey / fileService_test.ts
Last active November 27, 2021 02:41
Deno content server - medium course - section 3 getContent UT
import { getContent } from "../../fileService.ts";
import {
assert,
assertExists,
assertThrowsAsync,
} from "https://deno.land/std/testing/asserts.ts";
async function readStream(r: ReadableStream) {
const rd = r.getReader();
assertExists(rd);
@mayankchoubey
mayankchoubey / controller_test.ts
Created November 27, 2021 02:45
Deno content server - medium course - section 3 handleRequest UT
import { handleRequest } from "../../controller.ts";
import { assert, assertExists } from "https://deno.land/std/testing/asserts.ts";
const baseUrl = "http://localhost:8000",
token = "cba633d4-59f3-42a5-af00-b7430c3a65d8";
Deno.test("No Authorization header", async () => {
const req = new Request(baseUrl + "/");
const resp = await handleRequest(req);
assertExists(resp);