From b553dba06d401380d07f85cfb34177d56c59e79f Mon Sep 17 00:00:00 2001 From: Zura Sekhniashvili Date: Fri, 26 Jun 2020 08:31:55 +0400 Subject: [PATCH 1/7] Change specific version into 0.58.0 --- chat.js | 4 ++-- server.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/chat.js b/chat.js index 3d06dcc..3e26488 100644 --- a/chat.js +++ b/chat.js @@ -1,6 +1,6 @@ // @ts-nocheck -import { isWebSocketCloseEvent } from "https://deno.land/std/ws/mod.ts"; -import { v4 } from "https://deno.land/std/uuid/mod.ts"; +import { isWebSocketCloseEvent } from "https://deno.land/std@0.58.0/ws/mod.ts"; +import { v4 } from "https://deno.land/std@0.58.0/uuid/mod.ts"; /** * userId: { diff --git a/server.js b/server.js index 410f406..43ff182 100644 --- a/server.js +++ b/server.js @@ -1,5 +1,5 @@ -import { listenAndServe } from "https://deno.land/std/http/server.ts"; -import { acceptWebSocket, acceptable } from "https://deno.land/std/ws/mod.ts"; +import { listenAndServe } from "https://deno.land/std@0.58.0/http/server.ts"; +import { acceptWebSocket, acceptable } from "https://deno.land/std@0.58.0/ws/mod.ts"; import chat from "./chat.js"; listenAndServe({ port: 3000 }, async (req) => { From a930e08b7d71a3193c4b2e9a5f4e0c388bca1ce9 Mon Sep 17 00:00:00 2001 From: Zura Sekhniashvili Date: Mon, 29 Jun 2020 21:39:27 +0400 Subject: [PATCH 2/7] Add to serve static files from Deno --- server.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/server.js b/server.js index 43ff182..2e06eee 100644 --- a/server.js +++ b/server.js @@ -1,8 +1,36 @@ import { listenAndServe } from "https://deno.land/std@0.58.0/http/server.ts"; +import { serveFile } from "https://deno.land/std@0.58.0/http/file_server.ts"; import { acceptWebSocket, acceptable } from "https://deno.land/std@0.58.0/ws/mod.ts"; import chat from "./chat.js"; +async function fileExists(path) { + try { + const stats = await Deno.lstat(path); + return stats && stats.isFile; + } catch(e) { + if (e && e instanceof Deno.errors.NotFound) { + return false; + } else { + throw e; + } + } +} + listenAndServe({ port: 3000 }, async (req) => { + + const position = req.url.indexOf('?'); + + let url = req.url; + if (position > -1) { + url = req.url.substring(0, position); + } + const path = `${Deno.cwd()}/public${url}`; // /index.html + if (await fileExists(path)) { + const content = await serveFile(req, path); + req.respond(content); + return; + } + if (req.method === "GET" && req.url === "/ws") { if (acceptable(req)) { acceptWebSocket({ From d807cbd2ed3ac4f2308f4afca7c3a84b675b1724 Mon Sep 17 00:00:00 2001 From: Zura Sekhniashvili Date: Mon, 29 Jun 2020 21:51:04 +0400 Subject: [PATCH 3/7] Prepare client.js to work on any domain under ssl --- public/client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/client.js b/public/client.js index c984ff2..28d762d 100644 --- a/public/client.js +++ b/public/client.js @@ -9,7 +9,7 @@ let leaveGroupBtn = document.querySelector("#leaveGroupBtn"); let groupName = document.querySelector("#groupName"); window.addEventListener("DOMContentLoaded", () => { - ws = new WebSocket(`ws://localhost:3000/ws`); + ws = new WebSocket(`${window.location.protocol === 'http:' ? 'ws' : 'wss'}://${window.location.host}/ws`); ws.addEventListener("open", onConnectionOpen); ws.addEventListener("message", onMessageReceived); }); From 755b0908d8e47fbc863c1075eee9cf9bfea904c1 Mon Sep 17 00:00:00 2001 From: Zura Sekhniashvili Date: Mon, 29 Jun 2020 21:56:56 +0400 Subject: [PATCH 4/7] Take port from command line, if not exists use default --- server.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/server.js b/server.js index 2e06eee..5cfd17e 100644 --- a/server.js +++ b/server.js @@ -1,6 +1,7 @@ import { listenAndServe } from "https://deno.land/std@0.58.0/http/server.ts"; import { serveFile } from "https://deno.land/std@0.58.0/http/file_server.ts"; import { acceptWebSocket, acceptable } from "https://deno.land/std@0.58.0/ws/mod.ts"; +import { parse } from "https://deno.land/std@0.59.0/flags/mod.ts"; import chat from "./chat.js"; async function fileExists(path) { @@ -16,7 +17,11 @@ async function fileExists(path) { } } -listenAndServe({ port: 3000 }, async (req) => { +const DEFAULT_PORT = 3000; +const argPort = parse(Deno.args).port; +const port = argPort ? parseInt(argPort) : DEFAULT_PORT + +listenAndServe({ port: port }, async (req) => { const position = req.url.indexOf('?'); From 27adb3e56cf85a0262f9f564aaad9b247f63240d Mon Sep 17 00:00:00 2001 From: Zura Sekhniashvili Date: Mon, 29 Jun 2020 22:00:12 +0400 Subject: [PATCH 5/7] Add Procfile --- Procfile | 1 + 1 file changed, 1 insertion(+) create mode 100644 Procfile diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..e3eca6b --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +web: deno run --allow-net --allow-read --cached-only server.js --port=${PORT} \ No newline at end of file From 2fd85f45485710657b98f2830ee6c9f61439f610 Mon Sep 17 00:00:00 2001 From: Zura Sekhniashvili Date: Mon, 29 Jun 2020 22:28:08 +0400 Subject: [PATCH 6/7] Serve /index.html if path is / --- server.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/server.js b/server.js index 5cfd17e..6d52a56 100644 --- a/server.js +++ b/server.js @@ -22,12 +22,15 @@ const argPort = parse(Deno.args).port; const port = argPort ? parseInt(argPort) : DEFAULT_PORT listenAndServe({ port: port }, async (req) => { + let url = req.url; + if (req.method === 'GET' && url === '/') { + url = '/index.html'; + } const position = req.url.indexOf('?'); - - let url = req.url; + if (position > -1) { - url = req.url.substring(0, position); + url = url.substring(0, position); } const path = `${Deno.cwd()}/public${url}`; // /index.html if (await fileExists(path)) { @@ -47,4 +50,4 @@ listenAndServe({ port: port }, async (req) => { } } }); -console.log("Server started on port 3000"); +console.log(`Server started on port ${port}`); From 2e35e0354bb528fb3ddf0dbc52c87ded1be843f4 Mon Sep 17 00:00:00 2001 From: EshwarCVS Date: Sun, 30 Aug 2020 11:43:48 +0530 Subject: [PATCH 7/7] Replacing the standard libraries version --- chat.js | 4 ++-- server.js | 25 ++++++++++++++----------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/chat.js b/chat.js index 3e26488..b99994b 100644 --- a/chat.js +++ b/chat.js @@ -1,6 +1,6 @@ // @ts-nocheck -import { isWebSocketCloseEvent } from "https://deno.land/std@0.58.0/ws/mod.ts"; -import { v4 } from "https://deno.land/std@0.58.0/uuid/mod.ts"; +import { isWebSocketCloseEvent } from "https://deno.land/std@0.65.0/ws/mod.ts"; +import { v4 } from "https://deno.land/std@0.65.0/uuid/mod.ts"; /** * userId: { diff --git a/server.js b/server.js index 6d52a56..033be35 100644 --- a/server.js +++ b/server.js @@ -1,14 +1,17 @@ -import { listenAndServe } from "https://deno.land/std@0.58.0/http/server.ts"; -import { serveFile } from "https://deno.land/std@0.58.0/http/file_server.ts"; -import { acceptWebSocket, acceptable } from "https://deno.land/std@0.58.0/ws/mod.ts"; -import { parse } from "https://deno.land/std@0.59.0/flags/mod.ts"; +import { listenAndServe } from "https://deno.land/std@0.67.0/http/server.ts"; +import { serveFile } from "https://deno.land/std@0.67.0/http/file_server.ts"; +import { + acceptWebSocket, + acceptable, +} from "https://deno.land/std@0.67.0/ws/mod.ts"; +import { parse } from "https://deno.land/std@0.67.0/flags/mod.ts"; import chat from "./chat.js"; async function fileExists(path) { try { const stats = await Deno.lstat(path); return stats && stats.isFile; - } catch(e) { + } catch (e) { if (e && e instanceof Deno.errors.NotFound) { return false; } else { @@ -19,16 +22,16 @@ async function fileExists(path) { const DEFAULT_PORT = 3000; const argPort = parse(Deno.args).port; -const port = argPort ? parseInt(argPort) : DEFAULT_PORT +const port = argPort ? parseInt(argPort) : DEFAULT_PORT; listenAndServe({ port: port }, async (req) => { let url = req.url; - if (req.method === 'GET' && url === '/') { - url = '/index.html'; + if (req.method === "GET" && url === "/") { + url = "/index.html"; } - const position = req.url.indexOf('?'); - + const position = req.url.indexOf("?"); + if (position > -1) { url = url.substring(0, position); } @@ -36,7 +39,7 @@ listenAndServe({ port: port }, async (req) => { if (await fileExists(path)) { const content = await serveFile(req, path); req.respond(content); - return; + return; } if (req.method === "GET" && req.url === "/ws") {