-
Notifications
You must be signed in to change notification settings - Fork 74
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
chore: add JSDocs to improve maintainability and tooling support #129
Open
adam-coster
wants to merge
1
commit into
electron:main
Choose a base branch
from
adam-coster:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+83
−9
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { PLATFORM_ARCH } from "./constants.js"; | ||
|
||
export type Platform = typeof PLATFORM_ARCH[keyof typeof PLATFORM_ARCH]; | ||
|
||
export interface Lock { | ||
unlock(): Promise<void>; | ||
} | ||
|
||
export interface ServerCache { | ||
get<T = unknown>(key: string): Promise<T | undefined>; | ||
set(key: string, value: any): Promise<void>; | ||
lock(resource: unknown): Promise<Lock>; | ||
} | ||
|
||
export type Latest = { | ||
[P in Platform | "darwin" | "win32"]?: { | ||
name: string; | ||
version: string; | ||
url: string; | ||
notes: string; | ||
RELEASES?: string; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,12 +16,20 @@ const { NODE_ENV: env } = process.env; | |
if (env === "test") log.level = "error"; | ||
|
||
class Updates { | ||
/** @type {string} */ token; | ||
/** @type {import("./types.js").ServerCache} */ cache; | ||
|
||
/** @param {{token: string, cache: import("./types.js").ServerCache}} options */ | ||
constructor({ token, cache }) { | ||
assert(cache, ".cache required"); | ||
this.token = token; | ||
this.cache = cache; | ||
} | ||
|
||
/** | ||
* @param {string|number|undefined} port | ||
* @param {()=>void} [cb] | ||
*/ | ||
listen(port, cb) { | ||
if (typeof port === "function") { | ||
[port, cb] = [undefined, port]; | ||
|
@@ -113,8 +121,14 @@ class Updates { | |
} | ||
} | ||
|
||
/** | ||
* @param {string} account | ||
* @param {string} repository | ||
* @param {import("./types.js").Platform} platform | ||
*/ | ||
async cachedGetLatest(account, repository, platform) { | ||
const key = `${account}/${repository}`; | ||
/** @type {import("./types.js").Latest|undefined|null} */ | ||
let latest = await this.cache.get(key); | ||
|
||
if (latest) { | ||
|
@@ -126,6 +140,7 @@ class Updates { | |
return latest[platform] || null; | ||
} | ||
|
||
/** @type {import("./types.js").Lock|undefined} */ | ||
let lock; | ||
if (this.cache.lock) { | ||
log.debug({ key }, "lock acquiring"); | ||
|
@@ -155,6 +170,11 @@ class Updates { | |
return latest && latest[platform]; | ||
} | ||
|
||
/** | ||
* @param {string} account | ||
* @param {string} repository | ||
* @returns {Promise<import("./types.js").Latest|undefined|null>} | ||
*/ | ||
async getLatest(account, repository) { | ||
account = encodeURIComponent(account); | ||
repository = encodeURIComponent(repository); | ||
|
@@ -176,6 +196,7 @@ class Updates { | |
return; | ||
} | ||
|
||
/** @type {import("./types.js").Latest} */ | ||
const latest = {}; | ||
|
||
const releases = await res.json(); | ||
|
@@ -213,28 +234,31 @@ class Updates { | |
PLATFORM_ARCH.WIN_IA32, | ||
PLATFORM_ARCH.WIN_ARM64, | ||
]) { | ||
if (latest[key]) { | ||
const rurl = `https://github.com/${account}/${repository}/releases/download/${latest[key].version}/RELEASES`; | ||
const the_latest = latest[key]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block contains the only functional changes I made. Their only purpose is to get better type support, since |
||
if (the_latest) { | ||
const rurl = `https://github.com/${account}/${repository}/releases/download/${the_latest.version}/RELEASES`; | ||
const rres = await fetch(rurl); | ||
if (rres.status < 400) { | ||
const body = await rres.text(); | ||
const matches = body.match(/[^ ]*\.nupkg/gim); | ||
assert(matches); | ||
const nuPKG = rurl.replace("RELEASES", matches[0]); | ||
latest[key].RELEASES = body.replace(matches[0], nuPKG); | ||
the_latest.RELEASES = body.replace(matches[0], nuPKG); | ||
} | ||
} | ||
} | ||
|
||
return hasAnyAsset(latest) ? latest : null; | ||
} | ||
|
||
/** @param {string|null} ip */ | ||
hashIp(ip) { | ||
if (!ip) return; | ||
return crypto.createHash("sha256").update(ip).digest("hex"); | ||
} | ||
} | ||
|
||
/** @param {import("./types.js").Latest} latest */ | ||
const hasAllAssets = (latest) => { | ||
return !!( | ||
latest[PLATFORM_ARCH.DARWIN_X64] && | ||
|
@@ -245,6 +269,7 @@ const hasAllAssets = (latest) => { | |
); | ||
}; | ||
|
||
/** @param {import("./types.js").Latest} latest */ | ||
const hasAnyAsset = (latest) => { | ||
return !!( | ||
latest[PLATFORM_ARCH.DARWIN_X64] || | ||
|
@@ -255,32 +280,54 @@ const hasAnyAsset = (latest) => { | |
); | ||
}; | ||
|
||
/** | ||
* @param {Res} res | ||
* @param {string} message | ||
*/ | ||
const notFound = (res, message = "Not found") => { | ||
res.statusCode = 404; | ||
res.end(message); | ||
}; | ||
|
||
/** | ||
* @param {Res} res | ||
* @param {string} message | ||
*/ | ||
const badRequest = (res, message) => { | ||
res.statusCode = 400; | ||
res.end(message); | ||
}; | ||
|
||
/** @param {Res} res */ | ||
const noContent = (res) => { | ||
res.statusCode = 204; | ||
res.end(); | ||
}; | ||
|
||
/** | ||
* @param {Res} res | ||
* @param {any} obj | ||
*/ | ||
const json = (res, obj) => { | ||
res.setHeader("Content-Type", "application/json"); | ||
res.end(JSON.stringify(obj)); | ||
}; | ||
|
||
// DO NOT PASS USER-SUPPLIED CONTENT TO THIS FUNCTION | ||
// AS IT WILL REDIRECT A USER ANYWHERE | ||
/** | ||
* DO NOT PASS USER-SUPPLIED CONTENT TO THIS FUNCTION | ||
* AS IT WILL REDIRECT A USER ANYWHERE | ||
* @param {Res} res | ||
* @param {string} url | ||
*/ | ||
const redirect = (res, url) => { | ||
res.statusCode = 302; | ||
res.setHeader("Location", url); | ||
res.end(url); | ||
}; | ||
|
||
module.exports = Updates; | ||
|
||
/** | ||
* @typedef {http.IncomingMessage} Req | ||
* @typedef {http.ServerResponse} Res | ||
*/ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Provides editor support for Typescript-aware IDEs.