Skip to content

Instantly share code, notes, and snippets.

@mayankchoubey
mayankchoubey / deno_progress_indicator.ts
Last active June 25, 2021 01:49
A progress indicator for CLI apps
export const enum ProgressIndicatorType {
BAR,
PERCENT,
DOTS
};
export class ProgressIndicator {
private numUnits:number;
private nextUpdateTS:number=Date.now();
private enc:TextEncoder=new TextEncoder();
@mayankchoubey
mayankchoubey / server.ts
Created June 30, 2021 06:24
URL shortener - server.ts
import {PORT as port} from "./constants.ts";
import {handleRequest} from "./router.ts";
if(!window.location) {
console.error("ERROR: Location must be specified with --location");
Deno.exit(1);
}
const listener = Deno.listen({port});
@mayankchoubey
mayankchoubey / router.ts
Created June 30, 2021 06:26
URL shortener - router.ts
import {Status} from "https://deno.land/std/http/http_status.ts";
import {HTTP_METHOD_GET, HTTP_METHOD_POST, ROUTE_SHORTEN, QUERY_PARAM_TARGET} from "./constants.ts";
import {getTarget, addTarget} from "./controller.ts";
import {sendResponseCode} from "./utils.ts";
export async function handleRequest(req: Request, resp: any) {
const u=new URL(req.url);
const path=u.pathname, target=u.searchParams.get(QUERY_PARAM_TARGET);
switch(req.method) {
case HTTP_METHOD_GET: {
@mayankchoubey
mayankchoubey / controller.ts
Created June 30, 2021 06:29
URL shortener - controller.ts
import {Status} from "https://deno.land/std/http/http_status.ts"
import {sendResponseCode, sendResponseRedirect, sendResponseShortenedUrl} from "./utils.ts";
import {get,add} from "./service.ts";
export function getTarget(resp: any, urlCode: string) {
const target=get(urlCode);
if(!target)
return sendResponseCode(resp, Status.NotFound);
sendResponseRedirect(resp, target);
}
@mayankchoubey
mayankchoubey / service.ts
Created June 30, 2021 06:32
URL shortener - service.ts
import {getId} from "./utils.ts";
export function add(target:string) {
const id=getId();
localStorage.setItem(id, target);
return id;
}
export function get(id:string) {
return localStorage.getItem(id.startsWith('/')?id.slice(1): id);
@mayankchoubey
mayankchoubey / constants.ts
Last active June 30, 2021 21:32
URL shortener - utils.ts & constants.ts
export const PORT=80;
export const ROUTE_SHORTEN='/shorten';
export const HTTP_METHOD_GET='GET';
export const HTTP_METHOD_POST='POST';
export const HEADER_LOCATION='location';
export const QUERY_PARAM_TARGET='target';
export const URL_CODE_LENGTH=8;
export const SERVICE_DOMAIN='http://sho.rt/';
@mayankchoubey
mayankchoubey / deno_native_hello_name.ts
Created July 11, 2021 05:32
Deno native HTTP v/s Go native HTTP
const listener = Deno.listen({ port: 3000 });
for await(const conn of listener)
handleNewConnection(conn);
async function handleNewConnection(conn: Deno.Conn) {
for await(const { request, respondWith } of Deno.serveHttp(conn)) {
const reqBody=await request.json();
if(reqBody) {
respondWith(new Response(JSON.stringify({ name: reqBody.name }), {
headers: { 'content-type': 'application/json'}
@mayankchoubey
mayankchoubey / DemoApplication.java
Created July 13, 2021 22:09
Deno native HTTP v/s Spring Boot HTTP
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RestController;
@mayankchoubey
mayankchoubey / deno_file_server.ts
Created October 12, 2021 03:30
Deno vs Node: File server
import {readableStreamFromReader as toStream} from "https://deno.land/std/io/mod.ts";
const baseServePath='/Users/mayankc/Work/source/deno-vs-nodejs/testdata';
const listener = Deno.listen({ port: 3000 });
for await(const conn of listener)
handleNewConnection(conn);
async function handleNewConnection(conn: Deno.Conn) {
for await(const {request:req, respondWith:res} of Deno.serveHttp(conn)) {
const filePath=baseServePath+(new URL(req.url)).pathname;
@mayankchoubey
mayankchoubey / deno_receive_complex_form_data.ts
Last active December 2, 2022 02:27
Deno vs Node: Multipart/form-data with fields and files
import {readerFromStreamReader, copy} from "https://deno.land/std/streams/mod.ts";
const basePath=Deno.env.get('TMPDIR');
const listener = Deno.listen({ port: 3000 });
for await(const conn of listener)
handleNewConnection(conn);
async function handleNewConnection(conn: Deno.Conn) {
for await(const { request, respondWith } of Deno.serveHttp(conn)) {
const reqBody=await request.formData();