diff --git a/deno.jsonc b/deno.jsonc index 3c5130f..01fa80c 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -1,5 +1,12 @@ { "tasks": { - "dev": "deno run --watch main.ts" + "dev": "deno run --allow-net --watch main.ts" + }, + "imports": { + "oak": "https://deno.land/x/oak@v12.1.0/mod.ts", + "socket.io": "https://deno.land/x/socket_io@0.2.0/mod.ts", + "std/crypto/mod.ts": "https://deno.land/std@0.183.0/crypto/mod.ts", + "std/encoding/hex.ts": "https://deno.land/std@0.183.0/encoding/hex.ts", + "std/http/server.ts": "https://deno.land/std@0.182.0/http/server.ts" } } diff --git a/deno.lock b/deno.lock index c58b92a..2fafdfc 100644 --- a/deno.lock +++ b/deno.lock @@ -1,5 +1,5 @@ { - "version": "2", + "version": "5", "remote": { "https://deno.land/std@0.150.0/_util/assert.ts": "e94f2eb37cebd7f199952e242c77654e43333c1ac4c5c700e929ea3aa5489f74", "https://deno.land/std@0.150.0/async/abortable.ts": "87aa7230be8360c24ad437212311c9e8d4328854baec27b4c7abb26e85515c06", diff --git a/main.ts b/main.ts index ac48222..4971c07 100644 --- a/main.ts +++ b/main.ts @@ -2,12 +2,11 @@ import { Application, Router, send, -} from "https://deno.land/x/oak@v12.1.0/mod.ts"; -import { Server } from "https://deno.land/x/socket_io@0.2.0/mod.ts"; -import { serve } from "https://deno.land/std@0.182.0/http/server.ts"; -import { crypto } from "https://deno.land/std@0.183.0/crypto/mod.ts"; -import { toHashString } from "https://deno.land/std@0.183.0/crypto/to_hash_string.ts"; -import {oakCors} from "https://deno.land/x/cors@v1.2.2/mod.ts" +} from "oak"; +import { Server } from "socket.io"; +import { serve } from "std/http/server.ts"; +import { crypto } from "std/crypto/mod.ts"; +import { encode } from "std/encoding/hex.ts"; const app = new Application(); const io = new Server(); @@ -30,27 +29,36 @@ router const createHash = async (secret: string) => { const data = new TextEncoder().encode(secret); const hash = await crypto.subtle.digest("SHAKE128", data); - return toHashString(hash); + return new TextDecoder().decode(encode(new Uint8Array(hash))); }; io.on("connection", (socket) => { - socket.on("multiplex-statechanged", (data) => { + socket.on("multiplex-statechanged", async (data) => { if ( typeof data.secret == "undefined" || data.secret == null || data.secret === "" ) return; - if (createHash(data.secret) === data.socketId) { + if (await createHash(data.secret) === data.socketId) { data.secret = null; socket.broadcast.emit(data.socketId, data); } }); }); -app.use(oakCors({ - origin: "*", - credentials: true, - preflightContinue: true -})) +// Set CORS headers directly +app.use(async (ctx, next) => { + ctx.response.headers.set("Access-Control-Allow-Origin", "*"); + ctx.response.headers.set("Access-Control-Allow-Credentials", "true"); + ctx.response.headers.set("Access-Control-Allow-Methods", "GET, HEAD, PUT, PATCH, POST, DELETE"); + ctx.response.headers.set("Access-Control-Allow-Headers", "Content-Type, Authorization"); + + if (ctx.request.method === "OPTIONS") { + ctx.response.status = 204; + return; + } + + await next(); +}) app.use(router.routes()); app.use(router.allowedMethods());