Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade dependencies #133

Merged
merged 8 commits into from
May 18, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ version: 2.1
orbs:
win: circleci/windows@1.0.0
jobs:
test-linux-14:
test-linux-18:
docker:
- image: cimg/node:14.21
- image: cimg/node:18.16.0
<<: *steps-test

workflows:
version: 2
test_and_release:
jobs:
- test-linux-14


- test-linux-18
8 changes: 8 additions & 0 deletions bin/test-update-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { createServer } = require("../test/helpers/create-server");

async function main() {
const { server, address } = await createServer();
console.log(`Server running at ${address}`);
}

main();
70 changes: 39 additions & 31 deletions bin/update-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,49 @@ assert(token, "GH_TOKEN required");
//
// Cache
//
async function getCache() {
const fixedRedisUrl = redisUrl.replace("redis://h:", "redis://:");
const client = redis.createClient({
url: fixedRedisUrl,
tls: {
rejectUnauthorized: false,
},
});

const fixedRedisUrl = redisUrl.replace("redis://h:", "redis://:");
const client = redis.createClient({
url: fixedRedisUrl,
tls: {
rejectUnauthorized: false,
},
});
const get = promisify(client.get).bind(client);
const redlock = new Redlock([client], {
retryDelay: ms("10s"),
});
await client.connect();
await client.ping();

const cache = {
async get(key) {
const json = await get(key);
return json && JSON.parse(json);
},
async set(key, value) {
const multi = client.multi();
multi.set(key, JSON.stringify(value));
multi.expire(key, ms(cacheTTL) / 1000);
const exec = promisify(multi.exec).bind(multi);
await exec();
},
async lock(resource) {
return redlock.lock(`locks:${resource}`, ms("1m"));
},
};
const redlock = new Redlock([client], {
retryDelay: ms("10s"),
});

const cache = {
async get(key) {
const json = await client.get(key);
return json && JSON.parse(json);
},
async set(key, value) {
await client.set(key, JSON.stringify(value), {
EX: ms(cacheTTL) / 1000,
});
},
async lock(resource) {
return redlock.lock(`locks:${resource}`, ms("1m"));
},
};

return cache;
}

//
// Go!
//
async function main() {
const cache = getCache();
const updates = new Updates({ token, cache });
updates.listen(port, () => {
console.log(`http://localhost:${port}`);
});
}

const updates = new Updates({ token, cache });
updates.listen(port, () => {
console.log(`http://localhost:${port}`);
});
main();
31 changes: 16 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,28 @@
"name": "update.electronjs.org",
"scripts": {
"start": "nodemon bin/update-server.js",
"prettier": "prettier --write '**/*.js'",
"start-test-server": "cross-env NODE_ENV=test nodemon bin/test-update-server.js",
"lint": "prettier --check '**/*.js'",
"prettier": "prettier --write '**/*.js'",
"test": "npm run lint && cross-env NODE_ENV=test nyc tap test/*.js"
},
"dependencies": {
"dotenv-safe": "^8.2.0",
"ms": "^2.1.2",
"node-fetch": "^2.6.1",
"pino": "^6.7.0",
"redis": "^3.1.1",
"redlock": "^3.1.2",
"request-ip": "^2.1.3",
"semver": "^7.3.2"
"ms": "^2.1.3",
"pino": "^8.12.1",
"redis": "^4.6.6",
"redlock": "^4.2.0",
"request-ip": "^3.3.0",
"semver": "^7.5.0"
},
"devDependencies": {
"cross-env": "^7.0.2",
"nock": "^13.0.5",
"nodemon": "^2.0.6",
"cross-env": "^7.0.3",
"nock": "^13.3.1",
"node-fetch": "2.6.9",
"nodemon": "^2.0.22",
"nyc": "^15.1.0",
"prettier": "^2.8.0",
"standard": "^16.0.3",
"tap": "^16.3.2"
"prettier": "^2.8.8",
"standard": "^17.0.0",
"tap": "^16.3.4"
}
}
}
12 changes: 9 additions & 3 deletions src/updates.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"use strict";

const http = require("http");
const { default: fetch } = require("node-fetch");
const semver = require("semver");
const assert = require("assert");
const log = require("pino")();
Expand All @@ -12,8 +11,15 @@ const requestIp = require("request-ip");
const { assetPlatform } = require("./asset-platform");
const { PLATFORM, PLATFORM_ARCH, PLATFORM_ARCHS } = require("./constants");

const { NODE_ENV: env } = process.env;
if (env === "test") log.level = "error";
// TODO: Nock does not support native fetch, use node-fetch instead
// This dance will hopefully not be necessary once nock figures
// out a way to mock Node's native fetch() implementation
let fetch = global.fetch;

if (process.env.NODE_ENV === "test") {
fetch = require("node-fetch").default;
log.level = "error";
}

class Updates {
constructor({ token, cache }) {
Expand Down
31 changes: 31 additions & 0 deletions test/helpers/create-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const Updates = require("../../src/updates");

class MemoryCache {
constructor() {
this.data = new Map();
}

async get(key) {
return this.data.get(key);
}

async set(key, value) {
this.data.set(key, value);
}
}

function createServer() {
return new Promise((resolve) => {
const updates = new Updates({ cache: new MemoryCache() });
const server = updates.listen(() => {
resolve({
server,
address: `http://localhost:${server.address().port}`,
});
});
});
}

module.exports = {
createServer,
};
39 changes: 9 additions & 30 deletions test/update.test.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,9 @@
"use strict";

const { test } = require("tap");
const fetch = require("node-fetch");
const Updates = require("../src/updates");
const nock = require("nock");

class MemoryCache {
constructor() {
this.data = new Map();
}

async get(key) {
return this.data.get(key);
}

async set(key, value) {
this.data.set(key, value);
}
}

const createServer = () =>
new Promise((resolve) => {
const updates = new Updates({ cache: new MemoryCache() });
const server = updates.listen(() => {
resolve({
server,
address: `http://localhost:${server.address().port}`,
});
});
});
const { test, teardown } = require("tap");
const Updates = require("../src/updates");
const { createServer } = require("./helpers/create-server");

nock.disableNetConnect();
nock.enableNetConnect("localhost");
Expand Down Expand Up @@ -191,14 +166,16 @@ test("Updates", async (t) => {

await t.test("Routes", async (t) => {
const res = await fetch(`${address}/`);
void res.text();
t.equal(res.status, 200);

await t.test("exists and has update", async (t) => {
for (let i = 0; i < 2; i++) {
const res = await fetch(`${address}/owner/repo/darwin/0.0.0`);
t.equal(res.status, 200);

const body = await res.json();
t.deepEqual(body, {
t.same(body, {
name: "name",
url: "mac.zip",
notes: "notes",
Expand Down Expand Up @@ -466,5 +443,7 @@ test("Updates", async (t) => {
});
});

server.close();
teardown(() => {
server.close();
});
});