From 583d8e5d8eec9e73ff59973d866a06623aa4b3f2 Mon Sep 17 00:00:00 2001 From: Untone Date: Tue, 9 Apr 2024 20:09:35 +0300 Subject: [PATCH] synthax-fix --- bun.lockb | Bin 7396 -> 9387 bytes index.mjs | 183 ++++++++++++++++++++++++++------------------------- package.json | 3 +- 3 files changed, 94 insertions(+), 92 deletions(-) diff --git a/bun.lockb b/bun.lockb index 45a7001c5d9a698d54f6e186b437cafb2a25594a..352c8af1483a19d2826a3866d0a8861fe08ad600 100755 GIT binary patch delta 2638 zcmb_ddr(wW7{B-KuKR!mfrVY< z%2*;2l87lN)qsHbNKLcBa$`T(tE!*=_He-<7a$Li6d0qs6i#hdb_1nVS2+$%`^KWr@};H9|Qk za4L#2#=+1Muo+MeSj%B0r!N9jfj$_}7BGKB9*ig|D>F@v*uV_V^I%G9#u}6$$-qkl zh~BuIGhm;PqAUQTMT7Q24!;LPKfP{zD6-X^6li=s*{+2-_teK~ZY}cL6+EMOk+M$G zF1kH_>Zav!8;E9qXlCmfqgCa_>KmH`o*%bl3z|OvGrhLG%GI8D)2HOqfM+6biUC)V z3fhl!(h~UvkRo5wis*mRMuYZchIOD#7|y_aLz8Y=!}v4Am(ehV3|RqimLW9&Vgcy} zh!YTBfrcp+kQD&u1*8U`4{QLwLgFjbFiVAImIFdk1Cl^Qx&e|z#8;$|mcSWd*((L{p)HisQfnZj*Dq?80YHbl4$ycV+$T(Vhb4~Tva9P)n# zBKkXW=mdxj5&hvh&20!8%mx=wqk}7lZh%PS!QpFw*m%V8{}cE!q@t*Q5&5eHPQu0d zsIM#KRBXe-zKx~Y-LXD)f~i}sJ-TX#KtZ^{R<1+57c+Z43AX^ww+heyAv zvo^)Qluk)1wE21G+DYWTSY_x?Jy74hBp#JN>y5&{imOd|bFNDJmTH%@MLVsJmB$t9 zHuR=R9!`&O-{L-5Sm_g^=rTk~-rl-s@xi9q-u6#1DQRlJdH~KFkC1P|0&3%)h{tI= zc~yvCS*bPCq@RyQ{@U<~>h+t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=X2XG* zGu$riM5hZ$MkJpCgB~Oqk^BoupzSBUr)m<53>G6P&S8UoB-$u59&|+x|2H8_??6V( z4<+^?Xm!S6%`U?OXPis0=}f}7LxxUQoSWgkD+!HG8Fo=wpiQv zJFa-4)pzO)`?Hrbs#Cu9bZ%sMO{KHzZ*u2B_1`w)v=l*?KXjgE^T$K(*s}~e@h+nA%yqBQh85CfD8nxpwrq`q&sQLBi6ujCMfOX`&T-F&8I$}TFOjthP{ zqSn-?wQQQfOdu_08B_!lqKU&+Fbp$6Mf~9KD(F{sF?Hr?uT5-P&tR{M#*X&~1Ru2rZ$& Tozi|7D^nmAaxOe7x0(L}V?o{q diff --git a/index.mjs b/index.mjs index 6e84e65..1368513 100644 --- a/index.mjs +++ b/index.mjs @@ -1,16 +1,16 @@ -import { Authorizer } from '@authorizerdev/authorizer-js'; -import { Server } from '@hocuspocus/server'; +import { Authorizer } from "@authorizerdev/authorizer-js"; +import { Server } from "@hocuspocus/server"; +import Sentry from "@sentry/node"; + +Sentry.init({ dsn: process.env.GLITCHTIP_DSN }); const port = process.env.PORT || 4000; const authorizer = new Authorizer({ - clientID: process.env.AUTHORIZER_CLIENT_ID || '', - authorizerURL: process.env.AUTHORIZER_URL || 'https://auth.discours.io', - redirectURL: process.env.REDIRECT_URL || 'https://testing.discours.io', + clientID: process.env.AUTHORIZER_CLIENT_ID || "", + authorizerURL: process.env.AUTHORIZER_URL || "https://auth.discours.io", + redirectURL: process.env.REDIRECT_URL || "https://testing.discours.io", }); -const Sentry = require("@sentry/node"); -Sentry.init({ dsn: process.env.GLITCHTIP_DSN }); - const startServer = async () => { const server = await Server.configure({ port: process.env.PORT || 4000, @@ -18,44 +18,45 @@ const startServer = async () => { connection.requiresAuthentication = true; }, onAuthenticate(data) { - return new Promise((resolve, reject) => { - const headers = data.requestHeaders; - if (!headers) { - console.error('Request headers not found'); - return reject(new Error('Required header is not present')); + return new Promise((resolve, reject) => { + const headers = data.requestHeaders; + if (!headers) { + console.error("Request headers not found"); + return reject(new Error("Required header is not present")); + } + + const shout_id = parseInt(data.documentName.replace("shout-", ""), 10); + console.debug(`shout_id extracted: ${shout_id}`); + + const token = data.token || headers["authorization"] || ""; + if (!token) { + console.error("Authorization token not found"); + return reject(new Error("Token is not found")); + } + + authorizer + .validateJWTToken({ token_type: "access_token", token }) + .then((response) => { + if (!response?.data?.is_valid) { + console.error("Invalid authorization token"); + return reject(new Error("Token is invalid")); } - const shout_id = parseInt(data.documentName.replace('shout-', ''), 10); - console.debug(`shout_id extracted: ${shout_id}`); + const { sub: user, allowed_roles: roles } = response.data.claims; + console.debug(`user: ${user} roles: ${roles}`); - const token = data.token || headers['authorization'] || ''; - if (!token) { - console.error('Authorization token not found'); - return reject(new Error('Token is not found')); + if (roles.includes("editor")) { + return resolve({ + id: user, + roles: Array.isArray(roles) ? roles : roles.split(","), + }); } - authorizer.validateJWTToken({ token_type: 'access_token', token }) - .then(response => { - if (!response?.data?.is_valid) { - console.error('Invalid authorization token'); - return reject(new Error('Token is invalid')); - } - - const { sub: user, allowed_roles: roles } = response.data.claims; - console.debug(`user: ${user} roles: ${roles}`); - - if (roles.includes('editor')) { - return resolve({ - id: user, - roles: Array.isArray(roles) ? roles : roles.split(','), - }); - } - - fetch('https://core.discours.io/', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ - query: ` + fetch("https://core.discours.io/", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + query: ` query { get_author_id(user: "${user}") { id @@ -64,67 +65,69 @@ const startServer = async () => { } } `, - }), - }) - .then(res => res.json()) - .then(({ data }) => { - // console.debug(data) - const { id: author_id } = data.get_author_id - if (author_id) { - fetch('https://core.discours.io/', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ - query: `query { + }), + }) + .then((res) => res.json()) + .then(({ data }) => { + // console.debug(data) + const { id: author_id } = data.get_author_id; + if (author_id) { + fetch("https://core.discours.io/", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + query: `query { get_shout(shout_id: ${shout_id}) { id slug authors { id } } } - ` - }), - }) - .then(res => res.json()) - .then(({data}) => { - // console.debug('shout data:', data) - const { authors } = data.get_shout; - if (authors.some(author => author.id === author_id)) { - return resolve({ - id: user, - roles: Array.isArray(roles) ? roles : roles.split(','), - }); - } - return reject(new Error('User is not in authors list')); - }) - .catch(e => { - console.error('Error fetching shout data:', e.message); - console.error(e.stack); - return reject(new Error('Error fetching shout data')); - }); - } - }) - .catch(e => { - console.error('Error fetching author data:', e.message); - console.error(e.stack); - return reject(new Error('Error fetching author data')); + `, + }), + }) + .then((res) => res.json()) + .then(({ data }) => { + // console.debug('shout data:', data) + const { authors } = data.get_shout; + if (authors.some((author) => author.id === author_id)) { + return resolve({ + id: user, + roles: Array.isArray(roles) + ? roles + : roles.split(","), }); - }) - .catch(e => { - console.error('Error validating authorization token:', e.message); - console.error(e.stack); - return reject(new Error('Error validating authorization token')); - }); - }); + } + return reject(new Error("User is not in authors list")); + }) + .catch((e) => { + console.error("Error fetching shout data:", e.message); + console.error(e.stack); + return reject(new Error("Error fetching shout data")); + }); + } + }) + .catch((e) => { + console.error("Error fetching author data:", e.message); + console.error(e.stack); + return reject(new Error("Error fetching author data")); + }); + }) + .catch((e) => { + console.error("Error validating authorization token:", e.message); + console.error(e.stack); + return reject(new Error("Error validating authorization token")); + }); + }); }, -}); + }); +}; -process.on('unhandledRejection', (reason, promise) => { +process.on("unhandledRejection", (reason, promise) => { Sentry.captureException(reason); }); -process.on('uncaughtException', (error) => { +process.on("uncaughtException", (error) => { Sentry.captureException(error); }); - startServer(); diff --git a/package.json b/package.json index 653f0a8..8088ac7 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,6 @@ "version": "1.0.4", "description": "discours.io webrtc p2p network helping crdt-server", "main": "index.mjs", - "type": "module", "scripts": { "start": "node ./index.mjs" }, @@ -16,6 +15,6 @@ "dependencies": { "@authorizerdev/authorizer-js": "^2.0.0", "@hocuspocus/server": "^2.11.2", - "@sentry/node": "^6.14.0" + "@sentry/node": "^7.109.0" } }