synthax-fix

This commit is contained in:
Untone 2024-04-09 20:09:35 +03:00
parent 41895fc7f7
commit 583d8e5d8e
3 changed files with 94 additions and 92 deletions

BIN
bun.lockb

Binary file not shown.

View File

@ -1,16 +1,16 @@
import { Authorizer } from '@authorizerdev/authorizer-js'; import { Authorizer } from "@authorizerdev/authorizer-js";
import { Server } from '@hocuspocus/server'; import { Server } from "@hocuspocus/server";
import Sentry from "@sentry/node";
Sentry.init({ dsn: process.env.GLITCHTIP_DSN });
const port = process.env.PORT || 4000; const port = process.env.PORT || 4000;
const authorizer = new Authorizer({ const authorizer = new Authorizer({
clientID: process.env.AUTHORIZER_CLIENT_ID || '', clientID: process.env.AUTHORIZER_CLIENT_ID || "",
authorizerURL: process.env.AUTHORIZER_URL || 'https://auth.discours.io', authorizerURL: process.env.AUTHORIZER_URL || "https://auth.discours.io",
redirectURL: process.env.REDIRECT_URL || 'https://testing.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 startServer = async () => {
const server = await Server.configure({ const server = await Server.configure({
port: process.env.PORT || 4000, port: process.env.PORT || 4000,
@ -21,39 +21,40 @@ const startServer = async () => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const headers = data.requestHeaders; const headers = data.requestHeaders;
if (!headers) { if (!headers) {
console.error('Request headers not found'); console.error("Request headers not found");
return reject(new Error('Required header is not present')); return reject(new Error("Required header is not present"));
} }
const shout_id = parseInt(data.documentName.replace('shout-', ''), 10); const shout_id = parseInt(data.documentName.replace("shout-", ""), 10);
console.debug(`shout_id extracted: ${shout_id}`); console.debug(`shout_id extracted: ${shout_id}`);
const token = data.token || headers['authorization'] || ''; const token = data.token || headers["authorization"] || "";
if (!token) { if (!token) {
console.error('Authorization token not found'); console.error("Authorization token not found");
return reject(new Error('Token is not found')); return reject(new Error("Token is not found"));
} }
authorizer.validateJWTToken({ token_type: 'access_token', token }) authorizer
.then(response => { .validateJWTToken({ token_type: "access_token", token })
.then((response) => {
if (!response?.data?.is_valid) { if (!response?.data?.is_valid) {
console.error('Invalid authorization token'); console.error("Invalid authorization token");
return reject(new Error('Token is invalid')); return reject(new Error("Token is invalid"));
} }
const { sub: user, allowed_roles: roles } = response.data.claims; const { sub: user, allowed_roles: roles } = response.data.claims;
console.debug(`user: ${user} roles: ${roles}`); console.debug(`user: ${user} roles: ${roles}`);
if (roles.includes('editor')) { if (roles.includes("editor")) {
return resolve({ return resolve({
id: user, id: user,
roles: Array.isArray(roles) ? roles : roles.split(','), roles: Array.isArray(roles) ? roles : roles.split(","),
}); });
} }
fetch('https://core.discours.io/', { fetch("https://core.discours.io/", {
method: 'POST', method: "POST",
headers: { 'Content-Type': 'application/json' }, headers: { "Content-Type": "application/json" },
body: JSON.stringify({ body: JSON.stringify({
query: ` query: `
query { query {
@ -66,14 +67,14 @@ const startServer = async () => {
`, `,
}), }),
}) })
.then(res => res.json()) .then((res) => res.json())
.then(({ data }) => { .then(({ data }) => {
// console.debug(data) // console.debug(data)
const { id: author_id } = data.get_author_id const { id: author_id } = data.get_author_id;
if (author_id) { if (author_id) {
fetch('https://core.discours.io/', { fetch("https://core.discours.io/", {
method: 'POST', method: "POST",
headers: { 'Content-Type': 'application/json' }, headers: { "Content-Type": "application/json" },
body: JSON.stringify({ body: JSON.stringify({
query: `query { query: `query {
get_shout(shout_id: ${shout_id}) { get_shout(shout_id: ${shout_id}) {
@ -82,49 +83,51 @@ const startServer = async () => {
authors { id } authors { id }
} }
} }
` `,
}), }),
}) })
.then(res => res.json()) .then((res) => res.json())
.then(({data}) => { .then(({ data }) => {
// console.debug('shout data:', data) // console.debug('shout data:', data)
const { authors } = data.get_shout; const { authors } = data.get_shout;
if (authors.some(author => author.id === author_id)) { if (authors.some((author) => author.id === author_id)) {
return resolve({ return resolve({
id: user, id: user,
roles: Array.isArray(roles) ? roles : roles.split(','), roles: Array.isArray(roles)
? roles
: roles.split(","),
}); });
} }
return reject(new Error('User is not in authors list')); return reject(new Error("User is not in authors list"));
}) })
.catch(e => { .catch((e) => {
console.error('Error fetching shout data:', e.message); console.error("Error fetching shout data:", e.message);
console.error(e.stack); console.error(e.stack);
return reject(new Error('Error fetching shout data')); return reject(new Error("Error fetching shout data"));
}); });
} }
}) })
.catch(e => { .catch((e) => {
console.error('Error fetching author data:', e.message); console.error("Error fetching author data:", e.message);
console.error(e.stack); console.error(e.stack);
return reject(new Error('Error fetching author data')); return reject(new Error("Error fetching author data"));
}); });
}) })
.catch(e => { .catch((e) => {
console.error('Error validating authorization token:', e.message); console.error("Error validating authorization token:", e.message);
console.error(e.stack); console.error(e.stack);
return reject(new Error('Error validating authorization token')); return reject(new Error("Error validating authorization token"));
}); });
}); });
}, },
}); });
};
process.on('unhandledRejection', (reason, promise) => { process.on("unhandledRejection", (reason, promise) => {
Sentry.captureException(reason); Sentry.captureException(reason);
}); });
process.on('uncaughtException', (error) => { process.on("uncaughtException", (error) => {
Sentry.captureException(error); Sentry.captureException(error);
}); });
startServer(); startServer();

View File

@ -3,7 +3,6 @@
"version": "1.0.4", "version": "1.0.4",
"description": "discours.io webrtc p2p network helping crdt-server", "description": "discours.io webrtc p2p network helping crdt-server",
"main": "index.mjs", "main": "index.mjs",
"type": "module",
"scripts": { "scripts": {
"start": "node ./index.mjs" "start": "node ./index.mjs"
}, },
@ -16,6 +15,6 @@
"dependencies": { "dependencies": {
"@authorizerdev/authorizer-js": "^2.0.0", "@authorizerdev/authorizer-js": "^2.0.0",
"@hocuspocus/server": "^2.11.2", "@hocuspocus/server": "^2.11.2",
"@sentry/node": "^6.14.0" "@sentry/node": "^7.109.0"
} }
} }