diff --git a/.oxlint/node.mjs b/.oxlint/node.mjs new file mode 100644 index 0000000000..dc046bf645 --- /dev/null +++ b/.oxlint/node.mjs @@ -0,0 +1,78 @@ +/** + * Local oxlint plugin with rules for Node.js built-in modules. + */ + +const urlModules = new Set(["url", "node:url"]); + +/** Returns true if the member expression accesses the given property name. */ +function isProperty(node, name) { + if (node.type !== "MemberExpression") { + return false; + } + + if (!node.computed) { + return node.property.type === "Identifier" && node.property.name === name; + } + + return node.property.type === "Literal" && node.property.value === name; +} + +/** Returns the name of an import specifier's imported binding. */ +function importedName(specifier) { + return specifier.imported.type === "Identifier" + ? specifier.imported.name + : specifier.imported.value; +} + +const plugin = { + meta: { + name: "node-local", + }, + rules: { + "no-url-parse": { + meta: { + type: "problem", + docs: { + description: + "Disallow the legacy `url.parse()`, which is deprecated (DEP0169).", + }, + messages: { + forbidden: + "`url.parse()` is deprecated and not standardized. Use the WHATWG `URL` API instead.", + }, + }, + create(context) { + const moduleNames = new Set(); + + return { + ImportDeclaration(node) { + if (!urlModules.has(node.source.value)) { + return; + } + + for (const specifier of node.specifiers) { + if (specifier.type === "ImportSpecifier") { + if (importedName(specifier) === "parse") { + context.report({ node: specifier, messageId: "forbidden" }); + } + continue; + } + moduleNames.add(specifier.local.name); + } + }, + MemberExpression(node) { + if ( + node.object.type === "Identifier" && + moduleNames.has(node.object.name) && + isProperty(node, "parse") + ) { + context.report({ node, messageId: "forbidden" }); + } + }, + }; + }, + }, + }, +}; + +export default plugin; diff --git a/.oxlintrc.json b/.oxlintrc.json index 75b70179c3..207b36d758 100644 --- a/.oxlintrc.json +++ b/.oxlintrc.json @@ -17,7 +17,8 @@ "./.oxlint/editor.mjs", "./.oxlint/koa.mjs", "./.oxlint/react-router.mjs", - "./.oxlint/classes.mjs" + "./.oxlint/classes.mjs", + "./.oxlint/node.mjs" ], "plugins": ["eslint", "typescript", "unicorn", "oxc", "vitest"], "rules": { @@ -41,6 +42,7 @@ "koa/no-request-origin": "error", "react-router/no-deferred-use-location": "error", "classes/no-private-identifier": "error", + "node-local/no-url-parse": "error", "for-direction": "error", "no-async-promise-executor": "error", "no-case-declarations": "error", diff --git a/server/services/collaboration.ts b/server/services/collaboration.ts index fb4200a987..c76662fe97 100644 --- a/server/services/collaboration.ts +++ b/server/services/collaboration.ts @@ -1,7 +1,6 @@ import type { IncomingMessage } from "node:http"; import type http from "node:http"; import type { Duplex } from "node:stream"; -import url from "node:url"; import { Redis } from "@hocuspocus/extension-redis"; import { Throttle } from "@hocuspocus/extension-throttle"; import { Server } from "@hocuspocus/server"; @@ -78,9 +77,8 @@ export default function init( function (req: IncomingMessage, socket: Duplex, head: Buffer) { if (req.url?.startsWith(path)) { // parse document id and close connection if not present in request - const documentId = url - .parse(req.url) - .pathname?.replace(path, "") + const documentId = new URL(req.url, "http://localhost").pathname + .replace(path, "") .split("/") .pop();