Browse Source

Add Deno runtime (TypeScript) (#5687)

* add deno runtime using typescript

* bump to deno 1.0.0

* use Headers constructor

* add README.md

* rename IRequestHandler -> Handler

* delete empty line
Marcelo Barbosa 5 years ago
parent
commit
718d791b91

+ 18 - 0
frameworks/TypeScript/deno/README.md

@@ -0,0 +1,18 @@
+# [Deno](https://deno.land/) - A secure runtime for JavaScript and TypeScript
+
+## Description
+
+Deno is a simple, modern and secure runtime for JavaScript and TypeScript that uses V8 and is built in Rust.
+
+* [Deno Manual](https://deno.land/manual)
+* [Deno Standard Library](https://deno.land/std)
+
+## Test URLs
+
+### Test 1: JSON Encoding
+
+    http://localhost:8080/json
+
+### Test 2: Plaintext
+
+    http://localhost:8080/plaintext

+ 21 - 0
frameworks/TypeScript/deno/benchmark_config.json

@@ -0,0 +1,21 @@
+{
+  "framework": "deno",
+  "tests": [{
+    "default": {
+      "json_url": "/json",
+      "plaintext_url": "/plaintext",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Platform",
+      "framework": "None",
+      "language": "TypeScript",
+      "flavor": "deno",
+      "platform": "deno",
+      "webserver": "None",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "deno",
+      "versus": "nodejs"
+    }
+  }]
+}

+ 13 - 0
frameworks/TypeScript/deno/deno.dockerfile

@@ -0,0 +1,13 @@
+FROM hayd/alpine-deno:1.0.0
+
+EXPOSE 8080
+
+WORKDIR /app
+
+USER deno
+
+COPY ./src .
+
+RUN deno cache main.ts
+
+CMD ["run", "--allow-net", "main.ts"]

+ 46 - 0
frameworks/TypeScript/deno/src/handlers.ts

@@ -0,0 +1,46 @@
+import {
+  ServerRequest,
+  Response
+} from "https://deno.land/[email protected]/http/server.ts";
+
+interface Handler {
+  (request: ServerRequest): Promise<void>
+}
+
+const SERVER: string = "Deno";
+const HELLO_WORLD: Uint8Array = new TextEncoder().encode("Hello, World!");
+
+let date: string = new Date().toUTCString();
+setInterval(() => { date = new Date().toUTCString(); }, 1000);
+
+const json: Handler = async (req: ServerRequest): Promise<void> => {
+  const headers = new Headers([
+    ["server", SERVER],
+    ["content-type", "application/json"],
+    ["date", date]
+  ]);
+
+  req.respond({
+    headers,
+    body: JSON.stringify({ message: "Hello, World!" })
+  } as Response);
+};
+
+const plaintext: Handler = async (req: ServerRequest): Promise<void> => {
+  const headers = new Headers([
+    ["server", SERVER],
+    ["content-type", "text/plain; charset=UTF-8"],
+    ["date", date]
+  ]);
+
+  req.respond({ headers, body: HELLO_WORLD } as Response);
+};
+
+const handlers: { [Key: string]: Handler } = {};
+
+handlers["/json"] = json;
+handlers["/plaintext"] = plaintext;
+
+export {
+  handlers
+};

+ 7 - 0
frameworks/TypeScript/deno/src/main.ts

@@ -0,0 +1,7 @@
+import { serve } from "https://deno.land/[email protected]/http/server.ts";
+
+import { handlers } from "./handlers.ts";
+
+for await (const req of serve("0.0.0.0:8080")) {
+  handlers[req.url](req);
+}