Skip to content

Commit a9643b0

Browse files
refactor: update to yarn@v4, migrate to vitest, switch to typescript, add tests (#192)
* chore: update to yarn@v4 * chore: migrate to vitest * chore: rewrite in typescript * chore: add more tests * chore: remove unused dep and fix thingy * clean deps
1 parent 835ee62 commit a9643b0

24 files changed

+5226
-2638
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@ node_modules
55
example/dist
66
example/node_modules
77
dump.rdb
8+
.yarn/install-state.gz
9+
dist
10+
coverage
11+
*.log
12+
.DS_Store
13+
.vscode
14+
*.tsbuildinfo
15+
.claude

.yarn/releases/yarn-4.10.3.cjs

Lines changed: 942 additions & 0 deletions
Large diffs are not rendered by default.

.yarnrc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
nodeLinker: node-modules
2+
3+
yarnPath: .yarn/releases/yarn-4.10.3.cjs

bin/test-update-server.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

bin/test-update-server.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env node
2+
3+
import { createServer } from "../test/helpers/create-server.js";
4+
5+
async function main() {
6+
const { address } = await createServer();
7+
console.log(`Server running at ${address}`);
8+
}
9+
10+
main();
Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
#!/usr/bin/env node
22

3-
"use strict";
3+
import { config } from "dotenv-safe";
4+
import assert from "node:assert";
5+
import redis from "redis";
6+
import ms, { StringValue } from "ms";
7+
import Redlock, { CompatibleRedisClient } from "redlock";
8+
import Updates from "../src/updates.js";
49

5-
require("dotenv-safe").config();
10+
config();
611

712
process.title = "update-server";
813

9-
const Updates = require("../src/updates");
10-
const redis = require("redis");
11-
const ms = require("ms");
12-
const assert = require("assert");
13-
const Redlock = require("redlock");
14-
1514
//
1615
// Args
1716
//
1817

1918
const {
2019
GH_TOKEN: token,
2120
REDIS_URL: redisUrl = "redis://localhost:6379",
22-
PORT: port = 3000,
21+
PORT: port = "3000",
2322
CACHE_TTL: cacheTTL = "15m",
2423
} = process.env;
2524
assert(token, "GH_TOKEN required");
@@ -31,10 +30,6 @@ async function getCache() {
3130
const fixedRedisUrl = redisUrl.replace("redis://h:", "redis://:");
3231
const client = redis.createClient({
3332
url: fixedRedisUrl,
34-
// Needed for compatibility with Redlock. However, it also requires all "modern" commands
35-
// to be prefixed with `client.v4`.
36-
// See also: https://github.com/redis/node-redis/blob/master/docs/v3-to-v4.md#legacy-mode
37-
legacyMode: true,
3833
socket: {
3934
tls: true,
4035
rejectUnauthorized: false,
@@ -46,24 +41,29 @@ async function getCache() {
4641

4742
client.on("error", (err) => console.log("Redis Client Error", err));
4843

49-
const redlock = new Redlock([client], {
44+
const redlock = new Redlock([client.legacy() as CompatibleRedisClient], {
5045
retryDelay: ms("10s"),
5146
});
5247

5348
const cache = {
54-
async get(key) {
55-
const json = await client.v4.get(key);
56-
return json && JSON.parse(json);
49+
async get(key: string) {
50+
const json = await client.get(key);
51+
return json && typeof json === "string" && JSON.parse(json);
5752
},
58-
async set(key, value) {
53+
async set(key: string, value: any) {
5954
const json = JSON.stringify(value);
6055

61-
await client.v4.set(key, json, {
62-
EX: ms(cacheTTL) / 1000,
56+
await client.set(key, json, {
57+
EX: Math.floor(ms(cacheTTL as StringValue) / 1000),
6358
});
6459
},
65-
async lock(resource) {
66-
return redlock.lock(`locks:${resource}`, ms("1m"));
60+
async lock(resource: string) {
61+
const result = await redlock.lock([`locks:${resource}`], ms("1m"));
62+
return {
63+
async unlock() {
64+
await result.unlock();
65+
},
66+
};
6767
},
6868
};
6969

@@ -76,7 +76,7 @@ async function getCache() {
7676
async function main() {
7777
const cache = await getCache();
7878
const updates = new Updates({ token, cache });
79-
updates.listen(port, () => {
79+
updates.listen(Number(port), () => {
8080
console.log(`http://localhost:${port}`);
8181
});
8282
}

package.json

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,46 @@
11
{
22
"name": "update.electronjs.org",
33
"private": true,
4+
"type": "module",
45
"scripts": {
5-
"start": "nodemon bin/update-server.js",
6-
"start-test-server": "cross-env NODE_ENV=test nodemon bin/test-update-server.js",
7-
"lint": "prettier --check '**/*.js'",
8-
"prettier": "prettier --write '**/*.js'",
9-
"test": "npm run lint && cross-env NODE_ENV=test nyc tap test/*.js"
6+
"build": "tsc",
7+
"start": "node dist/bin/update-server.js",
8+
"start-test-server": "cross-env NODE_ENV=test node dist/bin/test-update-server.js",
9+
"lint": "prettier --check '**/*.{js,ts}' --ignore-path .gitignore",
10+
"prettier": "prettier --write '**/*.{js,ts}' --ignore-path .gitignore",
11+
"typecheck": "tsc --noEmit",
12+
"test": "npm run lint && npm run typecheck && cross-env NODE_ENV=test vitest run",
13+
"test:watch": "cross-env NODE_ENV=test vitest watch",
14+
"test:ui": "cross-env NODE_ENV=test vitest --ui",
15+
"test:coverage": "cross-env NODE_ENV=test vitest run --coverage"
1016
},
1117
"dependencies": {
12-
"dotenv-safe": "^8.2.0",
18+
"dotenv": "^17.2.3",
19+
"dotenv-safe": "^9.1.0",
1320
"ms": "^2.1.3",
1421
"pino": "^8.12.1",
15-
"redis": "^4.6.6",
16-
"redlock": "^3.1.2",
22+
"redis": "^5.8.3",
23+
"redlock": "^4.2.0",
1724
"request-ip": "^3.3.0",
1825
"semver": "^7.5.2"
1926
},
2027
"devDependencies": {
28+
"@tsconfig/node22": "^22.0.2",
29+
"@types/dotenv-safe": "^9.1.0",
30+
"@types/ms": "^2.1.0",
31+
"@types/node": "^24.6.2",
32+
"@types/node-fetch": "^2.6.13",
33+
"@types/redlock": "^4.0.7",
34+
"@types/request-ip": "^0.0.41",
35+
"@types/semver": "^7.7.1",
36+
"@vitest/coverage-v8": "^3.2.4",
37+
"@vitest/ui": "^3.2.4",
2138
"cross-env": "^7.0.3",
2239
"nock": "^13.3.1",
2340
"node-fetch": "2.6.9",
24-
"nodemon": "^3.0.1",
25-
"nyc": "^15.1.0",
2641
"prettier": "^2.8.8",
27-
"tap": "^16.3.4"
28-
}
42+
"typescript": "^5.9.3",
43+
"vitest": "^3.2.4"
44+
},
45+
"packageManager": "yarn@4.10.3+sha512.c38cafb5c7bb273f3926d04e55e1d8c9dfa7d9c3ea1f36a4868fa028b9e5f72298f0b7f401ad5eb921749eb012eb1c3bb74bf7503df3ee43fd600d14a018266f"
2946
}
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const { PLATFORM_ARCH } = require("./constants");
1+
import { PLATFORM_ARCH, type PlatformArch } from "./constants.js";
22

3-
const assetPlatform = (fileName) => {
3+
export const assetPlatform = (fileName: string): PlatformArch | false => {
44
if (/.*-(mac|darwin|osx).*\.zip$/i.test(fileName)) {
55
if (/-arm64/.test(fileName)) return PLATFORM_ARCH.DARWIN_ARM64;
66
if (/-universal/.test(fileName)) return PLATFORM_ARCH.DARWIN_UNIVERSAL;
@@ -28,7 +28,3 @@ const assetPlatform = (fileName) => {
2828

2929
return false;
3030
};
31-
32-
module.exports = {
33-
assetPlatform,
34-
};

src/constants.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/constants.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export const PLATFORM = {
2+
WIN32: "win32",
3+
DARWIN: "darwin",
4+
} as const;
5+
6+
export const PLATFORM_ARCH = {
7+
DARWIN_X64: "darwin-x64",
8+
DARWIN_ARM64: "darwin-arm64",
9+
DARWIN_UNIVERSAL: "darwin-universal",
10+
WIN_X64: "win32-x64",
11+
WIN_IA32: "win32-ia32",
12+
WIN_ARM64: "win32-arm64",
13+
} as const;
14+
15+
export type Platform = (typeof PLATFORM)[keyof typeof PLATFORM];
16+
export type PlatformArch = (typeof PLATFORM_ARCH)[keyof typeof PLATFORM_ARCH];
17+
18+
export const ENV = process.env.NODE_ENV || "development";
19+
20+
export const PLATFORM_ARCHS: readonly PlatformArch[] =
21+
Object.values(PLATFORM_ARCH);

0 commit comments

Comments
 (0)