chore: Move to Typescript (#2783)

This PR moves the entire project to Typescript. Due to the ~1000 ignores this will lead to a messy codebase for a while, but the churn is worth it – all of those ignore comments are places that were never type-safe previously.

closes #1282
This commit is contained in:
Tom Moor
2021-11-29 06:40:55 -08:00
committed by GitHub
parent 25ccfb5d04
commit 15b1069bcc
1017 changed files with 17410 additions and 54942 deletions

View File

@@ -1,5 +1,3 @@
// @flow
import http from "http";
import { createBullBoard } from "@bull-board/api";
import { BullAdapter } from "@bull-board/api/bullAdapter";
import { KoaAdapter } from "@bull-board/koa";
@@ -11,7 +9,7 @@ import {
websocketsQueue,
} from "../queues";
export default function init(app: Koa, server?: http.Server) {
export default function init(app: Koa) {
const serverAdapter = new KoaAdapter();
createBullBoard({
queues: [
@@ -22,7 +20,6 @@ export default function init(app: Koa, server?: http.Server) {
],
serverAdapter,
});
serverAdapter.setBasePath("/admin");
app.use(serverAdapter.registerPlugin());
}

View File

@@ -1,7 +1,7 @@
// @flow
import http from "http";
import url from "url";
import { Server } from "@hocuspocus/server";
import invariant from "invariant";
import Koa from "koa";
import WebSocket from "ws";
import AuthenticationExtension from "../collaboration/authentication";
@@ -11,27 +11,30 @@ import TracingExtension from "../collaboration/tracing";
export default function init(app: Koa, server: http.Server) {
const path = "/collaboration";
const wss = new WebSocket.Server({ noServer: true });
const wss = new WebSocket.Server({
noServer: true,
});
const hocuspocus = Server.configure({
extensions: [
new AuthenticationExtension(),
// @ts-expect-error ts-migrate(2322) FIXME: Type 'Persistence' is not assignable to type 'Exte... Remove this comment to see the full error message
new PersistenceExtension(),
new LoggerExtension(),
// @ts-expect-error ts-migrate(2322) FIXME: Type 'Persistence' is not assignable to type 'Exte... Remove this comment to see the full error message
new TracingExtension(),
],
});
server.on("upgrade", function (req, socket, head) {
if (req.url.indexOf(path) > -1) {
if (req.url && req.url.indexOf(path) > -1) {
const documentName = url.parse(req.url).pathname?.split("/").pop();
invariant(documentName, "Document name must be provided");
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type 'Duplex' is not assignable to par... Remove this comment to see the full error message
wss.handleUpgrade(req, socket, head, (client) => {
hocuspocus.handleConnection(client, req, documentName);
});
}
});
server.on("shutdown", () => {
hocuspocus.destroy();
});

View File

@@ -1,8 +1,13 @@
// @flow
import admin from "./admin";
import collaboration from "./collaboration";
import web from "./web";
import websockets from "./websockets";
import worker from "./worker";
export default { websockets, collaboration, admin, web, worker };
export default {
websockets,
collaboration,
admin,
web,
worker,
};

View File

@@ -1,5 +1,4 @@
// @flow
import http from "http";
/* eslint-disable @typescript-eslint/no-var-requires */
import Koa from "koa";
import {
contentSecurityPolicy,
@@ -8,16 +7,15 @@ import {
} from "koa-helmet";
import mount from "koa-mount";
import enforceHttps from "koa-sslify";
import env from "@server/env";
import Logger from "@server/logging/logger";
import emails from "../emails";
import env from "../env";
import Logger from "../logging/logger";
import routes from "../routes";
import api from "../routes/api";
import auth from "../routes/auth";
const isProduction = env.NODE_ENV === "production";
const isTest = env.NODE_ENV === "test";
// Construct scripts CSP based on services in use by this installation
const defaultSrc = ["'self'"];
const scriptSrc = [
@@ -30,17 +28,19 @@ const scriptSrc = [
if (env.GOOGLE_ANALYTICS_ID) {
scriptSrc.push("www.google-analytics.com");
}
if (env.CDN_URL) {
scriptSrc.push(env.CDN_URL);
defaultSrc.push(env.CDN_URL);
}
export default function init(app: Koa = new Koa(), server?: http.Server): Koa {
export default function init(app: Koa = new Koa()): Koa {
if (isProduction) {
// Force redirect to HTTPS protocol unless explicitly disabled
if (process.env.FORCE_HTTPS !== "false") {
app.use(
enforceHttps({
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type '{ trustProtoHeader: boolean; }' ... Remove this comment to see the full error message
trustProtoHeader: true,
})
);
@@ -51,37 +51,31 @@ export default function init(app: Koa = new Koa(), server?: http.Server): Koa {
// trust header fields set by our proxy. eg X-Forwarded-For
app.proxy = true;
} else if (!isTest) {
/* eslint-disable global-require */
const convert = require("koa-convert");
const webpack = require("webpack");
const devMiddleware = require("koa-webpack-dev-middleware");
const hotMiddleware = require("koa-webpack-hot-middleware");
const config = require("../../webpack.config.dev");
const compile = webpack(config);
/* eslint-enable global-require */
/* eslint-enable global-require */
const middleware = devMiddleware(compile, {
// display no info to console (only warnings and errors)
noInfo: true,
// display nothing to the console
quiet: false,
watchOptions: {
poll: 1000,
ignored: ["node_modules", "flow-typed", "server", "build", "__mocks__"],
},
// public path to bind the middleware to
// use the same as in webpack
publicPath: config.output.publicPath,
// options for formatting the statistics
stats: {
colors: true,
},
});
app.use(async (ctx, next) => {
ctx.webpackConfig = config;
ctx.devMiddleware = middleware;
@@ -91,6 +85,7 @@ export default function init(app: Koa = new Koa(), server?: http.Server): Koa {
app.use(
convert(
hotMiddleware(compile, {
// @ts-expect-error ts-migrate(7019) FIXME: Rest parameter 'args' implicitly has an 'any[]' ty... Remove this comment to see the full error message
log: (...args) => Logger.info("lifecycle", ...args),
path: "/__webpack_hmr",
heartbeat: 10 * 1000,
@@ -102,7 +97,6 @@ export default function init(app: Koa = new Koa(), server?: http.Server): Koa {
app.use(mount("/auth", auth));
app.use(mount("/api", api));
// Sets common security headers by default, such as no-sniff, hsts, hide powered
// by etc, these are applied after auth and api so they are only returned on
// standard non-XHR accessed routes
@@ -118,18 +112,23 @@ export default function init(app: Koa = new Koa(), server?: http.Server): Koa {
styleSrc: ["'self'", "'unsafe-inline'", "github.githubassets.com"],
imgSrc: ["*", "data:", "blob:"],
frameSrc: ["*"],
connectSrc: ["*"],
// Do not use connect-src: because self + websockets does not work in
connectSrc: ["*"], // Do not use connect-src: because self + websockets does not work in
// Safari, ref: https://bugs.webkit.org/show_bug.cgi?id=201591
},
})
);
// Allow DNS prefetching for performance, we do not care about leaking requests
// to our own CDN's
app.use(dnsPrefetchControl({ allow: true }));
app.use(referrerPolicy({ policy: "no-referrer" }));
app.use(
dnsPrefetchControl({
allow: true,
})
);
app.use(
referrerPolicy({
policy: "no-referrer",
})
);
app.use(mount(routes));
return app;
}

View File

@@ -1,17 +1,17 @@
// @flow
import http from "http";
import invariant from "invariant";
import Koa from "koa";
import IO from "socket.io";
import socketRedisAdapter from "socket.io-redis";
import SocketAuth from "socketio-auth";
import Logger from "../logging/logger";
import Metrics from "../logging/metrics";
import { Document, Collection, View } from "../models";
import Logger from "@server/logging/logger";
import Metrics from "@server/logging/metrics";
import { Document, Collection, View } from "@server/models";
import { getUserForJWT } from "@server/utils/jwt";
import policy from "../policies";
import { websocketsQueue } from "../queues";
import WebsocketsProcessor from "../queues/processors/websockets";
import { client, subscriber } from "../redis";
import { getUserForJWT } from "../utils/jwt";
const { can } = policy;
@@ -30,10 +30,13 @@ export default function init(app: Koa, server: http.Server) {
// collaboration websockets to exist in the same process as engine.io.
const listeners = server.listeners("upgrade");
const ioHandleUpgrade = listeners.pop();
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type 'Function | undefined' is not ass... Remove this comment to see the full error message
server.removeListener("upgrade", ioHandleUpgrade);
server.on("upgrade", function (req, socket, head) {
if (req.url.indexOf(path) > -1) {
if (req.url && req.url.indexOf(path) > -1) {
invariant(ioHandleUpgrade, "Existing upgrade handler must exist");
ioHandleUpgrade(req, socket, head);
}
});
@@ -49,11 +52,11 @@ export default function init(app: Koa, server: http.Server) {
})
);
io.origins((_, callback) => {
io.origins((_req, callback) => {
callback(null, true);
});
io.of("/").adapter.on("error", (err) => {
io.of("/").adapter.on("error", (err: Error) => {
if (err.name === "MaxRetriesPerRequestError") {
Logger.error("Redis maximum retries exceeded in socketio adapter", err);
throw err;
@@ -68,7 +71,6 @@ export default function init(app: Koa, server: http.Server) {
"websockets.count",
socket.client.conn.server.clientsCount
);
socket.on("disconnect", () => {
Metrics.increment("websockets.disconnected");
Metrics.gaugePerInstance(
@@ -89,23 +91,24 @@ export default function init(app: Koa, server: http.Server) {
// store the mapping between socket id and user id in redis
// so that it is accessible across multiple server nodes
await client.hset(socket.id, "userId", user.id);
return callback(null, true);
} catch (err) {
return callback(err);
return callback(err, false);
}
},
postAuthenticate: async (socket, data) => {
postAuthenticate: async (socket) => {
const { user } = socket.client;
// the rooms associated with the current team
// and user so we can send authenticated events
let rooms = [`team-${user.teamId}`, `user-${user.id}`];
const rooms = [`team-${user.teamId}`, `user-${user.id}`];
// the rooms associated with collections this user
// has access to on connection. New collection subscriptions
// are managed from the client as needed through the 'join' event
const collectionIds = await user.collectionIds();
const collectionIds: string[] = await user.collectionIds();
collectionIds.forEach((collectionId) =>
rooms.push(`collection-${collectionId}`)
);
@@ -138,7 +141,6 @@ export default function init(app: Koa, server: http.Server) {
if (can(user, "read", document)) {
const room = `document-${event.documentId}`;
await View.touch(event.documentId, user.id, event.isEditing);
const editing = await View.findRecentlyEditingByDocument(
event.documentId
@@ -155,7 +157,7 @@ export default function init(app: Koa, server: http.Server) {
});
// let this user know who else is already present in the room
io.in(room).clients(async (err, sockets) => {
io.in(room).clients(async (err: Error, sockets: string[]) => {
if (err) {
Logger.error("Error getting clients for room", err, {
sockets,
@@ -166,14 +168,17 @@ export default function init(app: Koa, server: http.Server) {
// because a single user can have multiple socket connections we
// need to make sure that only unique userIds are returned. A Map
// makes this easy.
let userIds = new Map();
const userIds = new Map();
for (const socketId of sockets) {
const userId = await client.hget(socketId, "userId");
userIds.set(userId, userId);
}
socket.emit("document.presence", {
documentId: event.documentId,
userIds: Array.from(userIds.keys()),
// @ts-expect-error ts-migrate(7006) FIXME: Parameter 'view' implicitly has an 'any' type.
editingIds: editing.map((view) => view.userId),
});
});
@@ -189,11 +194,12 @@ export default function init(app: Koa, server: http.Server) {
Metrics.increment("websockets.collections.leave");
});
}
if (event.documentId) {
const room = `document-${event.documentId}`;
socket.leave(room, () => {
Metrics.increment("websockets.documents.leave");
io.to(room).emit("user.leave", {
userId: user.id,
documentId: event.documentId,
@@ -218,7 +224,6 @@ export default function init(app: Koa, server: http.Server) {
socket.on("presence", async (event) => {
Metrics.increment("websockets.presence");
const room = `document-${event.documentId}`;
if (event.documentId && socket.rooms[room]) {
@@ -227,8 +232,8 @@ export default function init(app: Koa, server: http.Server) {
user.id,
event.isEditing
);
view.user = user;
view.user = user;
io.to(room).emit("user.presence", {
userId: user.id,
documentId: event.documentId,
@@ -241,11 +246,12 @@ export default function init(app: Koa, server: http.Server) {
// Handle events from event queue that should be sent to the clients down ws
const websockets = new WebsocketsProcessor();
websocketsQueue.process(async function websocketEventsProcessor(job) {
const event = job.data;
websockets.on(event, io).catch((error) => {
Logger.error("Error processing websocket event", error, { event });
Logger.error("Error processing websocket event", error, {
event,
});
});
});
}

View File

@@ -1,7 +1,4 @@
// @flow
import http from "http";
import Koa from "koa";
import Logger from "../logging/logger";
import Logger from "@server/logging/logger";
import {
globalEventQueue,
processorEventQueue,
@@ -18,7 +15,6 @@ import Revisions from "../queues/processors/revisions";
import Slack from "../queues/processors/slack";
const EmailsProcessor = new Emails();
const eventProcessors = {
backlinks: new Backlinks(),
debouncer: new Debouncer(),
@@ -29,19 +25,18 @@ const eventProcessors = {
slack: new Slack(),
};
export default function init(app: Koa, server?: http.Server) {
export default function init() {
// this queue processes global events and hands them off to services
globalEventQueue.process(function (job) {
Object.keys(eventProcessors).forEach((name) => {
processorEventQueue.add({ ...job.data, service: name });
});
websocketsQueue.add(job.data);
});
processorEventQueue.process(function (job) {
const event = job.data;
const processor = eventProcessors[event.service];
if (!processor) {
Logger.warn(`Received event for processor that isn't registered`, event);
return;
@@ -52,7 +47,7 @@ export default function init(app: Koa, server?: http.Server) {
name: event.name,
modelId: event.modelId,
});
// @ts-expect-error ts-migrate(7006) FIXME: Parameter 'error' implicitly has an 'any' type.
processor.on(event).catch((error) => {
Logger.error(
`Error processing ${event.name} in ${event.service}`,
@@ -62,10 +57,8 @@ export default function init(app: Koa, server?: http.Server) {
});
}
});
emailsQueue.process(function (job) {
const event = job.data;
EmailsProcessor.on(event).catch((error) => {
Logger.error(
`Error processing ${event.name} in emails processor`,