Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Tests

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: pnpm/action-setup@v5
- uses: actions/setup-node@v6
with:
node-version-file: .nvmrc
cache: pnpm
- run: pnpm install
- run: pnpm build
- run: pnpm test
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20
24
3 changes: 3 additions & 0 deletions @types/import-meta.d.ts

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does some lib in tsconfig not support it? Or updating @types/node?
Fine this way as well.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface ImportMeta {
main: boolean;
}
3 changes: 1 addition & 2 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import helmet from "helmet";

import * as logging from "./utils/logging.js";
import { register as registerViewEngine } from "./utils/view-engine.js";
import { legacyDirname } from "./utils/misc.js";
import { PROJECT_ROOT } from "./utils/constants.js";

import xrefRouter from "./routes/xref/index.js";
Expand All @@ -28,7 +27,7 @@ app.use(logging.stderr());

app.use(express.static(path.join(PROJECT_ROOT, "/static")));

app.set("views", path.join(legacyDirname(import.meta), "views"));
app.set("views", path.join(import.meta.dirname, "views"));
registerViewEngine(app);

// Security
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"author": "Marcos Caceres <marcos@marcosc.com>",
"type": "module",
"license": "MIT",
"engines": {
"node": ">=24"
},
"dependencies": {
"chalk": "^5.6.2",
"compression": "^1.8.1",
Expand All @@ -16,7 +19,6 @@
"helmet": "^8.0.0",
"morgan": "^1.10.1",
"nanoid": "^5.0.8",
"node-fetch": "^3.3.2",
"serialize-error": "^11.0.3",
"split2": "^4.2.0",
"ucontent": "^2.0.0"
Expand Down
49 changes: 0 additions & 49 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions routes/caniuse/update.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import path from "path";

import { legacyDirname } from "../../utils/misc.js";
import { BackgroundTaskQueue } from "../../utils/background-task-queue.js";
import { cache } from "./lib/index.js";
import { Request, Response } from "express";

const workerFile = path.join(legacyDirname(import.meta), "update.worker.js");
const workerFile = path.join(import.meta.dirname, "update.worker.js");
const taskQueue = new BackgroundTaskQueue<typeof import("./update.worker")>(
workerFile,
"caniuse_update",
Expand Down
1 change: 0 additions & 1 deletion routes/docs/update.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import path from "path";
import { writeFile } from "fs/promises";

import fetch from "node-fetch";
import { Request, Response } from "express";

import { HTTPError } from "../../utils/misc.js";
Expand Down
1 change: 0 additions & 1 deletion routes/github/lib/utils/graphql.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fetch from "node-fetch";
import { getToken, updateRateLimit, RateLimit } from "./tokens.js";

const ENDPOINT = "https://api.github.com/graphql";
Expand Down
14 changes: 11 additions & 3 deletions routes/github/lib/utils/rest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import fetch, { Response } from "node-fetch";
import { getToken, updateRateLimit, RateLimit } from "./tokens.js";

const GITHUB_API_PREFIX = "https://api.github.com/";

export async function* requestData(endpoint: string, pages = 30) {
if (!endpoint.startsWith(GITHUB_API_PREFIX)) {
throw new Error(`requestData: endpoint must start with ${GITHUB_API_PREFIX}`);
}
let url: string | null = endpoint;
do {
const token = getToken();
Expand All @@ -20,7 +24,11 @@ export async function* requestData(endpoint: string, pages = 30) {
const result = await response.json();
yield { url, result };

url = nextPage(response.headers.get("link") || "");
const next = nextPage(response.headers.get("link") || "");
if (next !== null && !next.startsWith(GITHUB_API_PREFIX)) {
throw new Error(`requestData: pagination URL must start with ${GITHUB_API_PREFIX}`);
}
url = next;
updateRateLimit(token, getRateLimit(response.headers));
} while (url !== null && --pages > 0);

Expand All @@ -38,7 +46,7 @@ function nextPage(link: string) {
return m ? m[1] : null;
}

function getRateLimit(headers: Response["headers"]): RateLimit {
function getRateLimit(headers: Headers): RateLimit {
return {
remaining: parseInt(headers.get("x-ratelimit-remaining") as string, 10),
resetAt: new Date(parseInt(headers.get("x-ratelimit-reset") as string, 10)),
Expand Down
2 changes: 1 addition & 1 deletion routes/w3c/group.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import path from "path";
import { readFileSync } from "fs";

import fetch from "node-fetch";
import { Request, Response } from "express";

import { MemCache } from "../../utils/mem-cache.js";
Expand Down Expand Up @@ -56,6 +55,7 @@ export default async function route(req: IRequest, res: Response) {
}

if (type && !groups.hasOwnProperty(type)) {
res.set("Content-Type", "text/plain");
return res.status(404).send(`Invalid group type: "${type}".`);
}

Expand Down
1 change: 0 additions & 1 deletion routes/well-known/pay.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Request, Response } from "express";
import fetch from "node-fetch";

const PAYMENT_POINTERS = [
// "$ilp.uphold.com/DwJmxPHHi8K3", // Marcos
Expand Down
3 changes: 1 addition & 2 deletions routes/xref/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import path from "path";

import { Request, Response } from "express";

import { legacyDirname } from "../../utils/misc.js";
import { BackgroundTaskQueue } from "../../utils/background-task-queue.js";
import { ms } from "../../utils/misc.js";

import { cache as searchCache } from "./lib/search.js";
import { store } from "./lib/store-init.js";

const workerFile = path.join(legacyDirname(import.meta), "update.worker.js");
const workerFile = path.join(import.meta.dirname, "update.worker.js");
const taskQueue = new BackgroundTaskQueue<typeof import("./update.worker")>(
workerFile,
"xref_update",
Expand Down
10 changes: 1 addition & 9 deletions scripts/update-w3c-groups-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
*/

import path from "path";
import { fileURLToPath } from "url";
import { writeFile, mkdir } from "fs/promises";

import "dotenv/config";
import fetch from "node-fetch";

import { env } from "../utils/misc.js";

Expand Down Expand Up @@ -122,12 +120,6 @@ export default async function update() {
await writeFile(OUTPUT_FILE, JSON.stringify(data, null, 2), "utf-8");
}

const runAsScript = (() => {
const modulePath = fileURLToPath(import.meta.url);
const scriptPath = process.argv[1];
return modulePath === scriptPath;
})();

if (runAsScript) {
if (import.meta.main) {
await update();
}
13 changes: 13 additions & 0 deletions tests/node24-globals.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
describe("Node 24 globals", () => {
it("fetch is available as a global", () => {
expect(typeof fetch).toBe("function");
});

it("import.meta.dirname is a string", () => {
expect(typeof import.meta.dirname).toBe("string");
});

it("import.meta.filename is a string", () => {
expect(typeof import.meta.filename).toBe("string");
});
});
9 changes: 9 additions & 0 deletions tests/utils/constants.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { PROJECT_ROOT } from "../../build/utils/constants.js";
import { existsSync } from "fs";
import { join } from "path";

describe("utils/constants", () => {
it("PROJECT_ROOT resolves to the repo root", () => {
expect(existsSync(join(PROJECT_ROOT, "package.json"))).toBeTrue();
});
});
4 changes: 2 additions & 2 deletions utils/background-task-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { nanoid } from "nanoid";
import split2 from "split2";
import { serializeError, deserializeError } from "serialize-error";

import { legacyFilename, env } from "./misc.js";
import { env } from "./misc.js";

type Message = { id: string };

Expand Down Expand Up @@ -151,7 +151,7 @@ export class BackgroundTaskQueue<M extends TaskModule> {
constructor(jobDescriptionModulePath: string, queueName: string) {
this.modulePath = jobDescriptionModulePath;
this.name = queueName;
const __filename = legacyFilename(import.meta);
const __filename = import.meta.filename;
this.worker = new Worker(__filename, { stderr: true, stdout: true });
}

Expand Down
3 changes: 1 addition & 2 deletions utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { join } from "path";
import { readdirSync } from "fs";
import { legacyDirname } from "./misc.js";

export const isDevEnv = process.env.NODE_ENV !== "production";

export const PROJECT_ROOT = (() => {
let currentDir = legacyDirname(import.meta);
let currentDir = import.meta.dirname;
while (!readdirSync(currentDir).includes("package.json")) {
const newCurrentDir = join(currentDir, "..");
if (currentDir === newCurrentDir) {
Expand Down
13 changes: 0 additions & 13 deletions utils/misc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import path from "path";
import { fileURLToPath } from "url";

/**
* Ensure env variable exists and get its value.
* @param name name of env variable
Expand Down Expand Up @@ -58,13 +55,3 @@ export class HTTPError extends Error {
super(message);
}
}

// __dirname
export function legacyDirname(meta: ImportMeta) {
return path.dirname(fileURLToPath(meta.url));
}

// __filename
export function legacyFilename(meta: ImportMeta) {
return fileURLToPath(meta.url);
}
Loading