forked from LemLib/LemLink
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 🚚 move github package resolution into its own directory
- Loading branch information
Showing
5 changed files
with
123 additions
and
111 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { GithubPackage, type GithubPackageReleaseData } from "./package"; | ||
export { GithubPackageResolver } from "./resolver"; | ||
export { GithubPackageVersion } from "./version"; |
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,43 @@ | ||
import { type Octokit } from "octokit"; | ||
import { Package, type PackageIdentifier } from "../package"; | ||
import { GithubPackageVersion } from "./version"; | ||
|
||
export type GithubPackageReleaseData = Awaited< | ||
ReturnType<Octokit["rest"]["repos"]["listReleases"]> | ||
>["data"][number]; | ||
|
||
export class GithubPackage extends Package< | ||
GithubPackageVersion, | ||
PackageIdentifier | ||
> { | ||
constructor( | ||
protected readonly client: Octokit, | ||
protected readonly id: PackageIdentifier, | ||
) { | ||
super(id); | ||
} | ||
|
||
public override async getVersions(): Promise<GithubPackageVersion[]> { | ||
return ( | ||
await this.client.rest.repos.listReleases({ | ||
...this.id, | ||
}) | ||
).data | ||
.map((release) => | ||
GithubPackageVersion.create(this.client, this.id, release), | ||
) | ||
.filter((release): release is GithubPackageVersion => release != null); | ||
} | ||
|
||
public async getLatest(): Promise<GithubPackageVersion | null> { | ||
return GithubPackageVersion.create( | ||
this.client, | ||
this.id, | ||
( | ||
await this.client.rest.repos.getLatestRelease({ | ||
...this.id, | ||
}) | ||
).data, | ||
); | ||
} | ||
} |
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 { Octokit } from "octokit"; | ||
import { type PackageIdentifier, PackageResolver } from "../package"; | ||
import { GithubPackage } from "./package"; | ||
|
||
export class GithubPackageResolver extends PackageResolver<GithubPackage> { | ||
private static readonly client: Octokit = new Octokit(); | ||
private static readonly singleton: GithubPackageResolver = | ||
new GithubPackageResolver(GithubPackageResolver.client); | ||
|
||
protected constructor(protected readonly client: Octokit) { | ||
super(); | ||
} | ||
|
||
public static get(): GithubPackageResolver { | ||
return GithubPackageResolver.singleton; | ||
} | ||
|
||
public override async resolvePackage( | ||
id: PackageIdentifier, | ||
): Promise<GithubPackage> { | ||
return new GithubPackage(this.client, id); | ||
} | ||
} |
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,53 @@ | ||
import { type Octokit } from "octokit"; | ||
import { PackageVersion, type PackageIdentifier } from "../package"; | ||
import { type SemVer, parse } from "semver"; | ||
import { type GithubPackageReleaseData } from "./package"; | ||
import { type IncomingMessage } from "http"; | ||
import { get as httpsGet, type RequestOptions } from "https"; | ||
|
||
export class GithubPackageVersion extends PackageVersion { | ||
protected constructor( | ||
protected readonly client: Octokit, | ||
public readonly packId: PackageIdentifier, | ||
public readonly data: GithubPackageReleaseData, | ||
version: SemVer, | ||
) { | ||
super(packId, version); | ||
} | ||
|
||
public static create( | ||
client: Octokit, | ||
packId: PackageIdentifier, | ||
data: GithubPackageReleaseData, | ||
): GithubPackageVersion | null { | ||
const version = parse(data.tag_name); | ||
if (version == null) return null; | ||
return new GithubPackageVersion(client, packId, data, version); | ||
} | ||
|
||
protected getAssetIndex(): number { | ||
return 0; | ||
} | ||
|
||
public override async download(): Promise<NodeJS.ReadableStream | undefined> { | ||
const index = this.getAssetIndex(); | ||
const auth = await this.client.auth(); | ||
const opts: RequestOptions = {}; | ||
|
||
if ( | ||
typeof auth === "object" && | ||
auth != null && | ||
"token" in auth && | ||
typeof auth.token === "string" | ||
) | ||
opts.headers = { | ||
Authorization: `Bearer ${auth.token}`, | ||
}; | ||
|
||
const response = await new Promise<IncomingMessage>((resolve) => | ||
httpsGet(this.data.assets[index].url, opts, resolve), | ||
); | ||
|
||
return response; | ||
} | ||
} |
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