Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adding Keygen as an official publisher/updater for electron-bui…
…lder #6167
- Loading branch information
Showing
28 changed files
with
23,199 additions
and
4,409 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
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 |
---|---|---|
|
@@ -6,7 +6,6 @@ | |
"files": [ | ||
"out", | ||
"templates", | ||
"scheme.json", | ||
"electron-osx-sign", | ||
"certs/root_certs.keychain" | ||
], | ||
|
119 changes: 119 additions & 0 deletions
119
packages/app-builder-lib/src/publish/KeygenPublisher.ts
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,119 @@ | ||
import { Arch, InvalidConfigurationError, log, isEmptyOrSpaces } from "builder-util" | ||
import { httpExecutor } from "builder-util/out/nodeHttpExecutor" | ||
import { ClientRequest, RequestOptions } from "http" | ||
import { HttpPublisher, PublishContext } from "electron-publish" | ||
import { KeygenOptions } from "builder-util-runtime/out/publishOptions" | ||
import { configureRequestOptions, HttpError, parseJson } from "builder-util-runtime" | ||
import * as path from "path" | ||
export class KeygenPublisher extends HttpPublisher { | ||
readonly providerName = "keygen" | ||
readonly hostname = "api.keygen.sh" | ||
|
||
private readonly info: KeygenOptions | ||
private readonly auth: string | ||
private readonly version: string | ||
private readonly basePath: string | ||
|
||
constructor(context: PublishContext, info: KeygenOptions, version: string) { | ||
super(context) | ||
|
||
const token = process.env.KEYGEN_TOKEN | ||
if (isEmptyOrSpaces(token)) { | ||
throw new InvalidConfigurationError(`Keygen token is not set using env "KEYGEN_TOKEN" (see https://www.electron.build/configuration/publish#KeygenOptions)`) | ||
} | ||
|
||
this.info = info | ||
this.auth = `Bearer ${token.trim()}` | ||
this.version = version | ||
this.basePath = `/v1/accounts/${this.info.account}/releases` | ||
} | ||
|
||
protected async doUpload( | ||
fileName: string, | ||
arch: Arch, | ||
dataLength: number, | ||
requestProcessor: (request: ClientRequest, reject: (error: Error) => void) => void, | ||
file?: string | ||
): Promise<any> { | ||
for (let attemptNumber = 0; ; attemptNumber++) { | ||
try { | ||
const { data, errors } = await this.upsertVersion(fileName, dataLength) | ||
const releaseId = data.id | ||
This comment has been minimized.
Sorry, something went wrong. |
||
if (releaseId == null) { | ||
log.warn({ file: fileName, reason: "UUID doesn't exist and was not created" }, "upserting release failed") | ||
return | ||
} | ||
if (errors) { | ||
log.error({ ...errors }, "upserting release returned errors. Continuing to attempt upload since UUID is present") | ||
This comment has been minimized.
Sorry, something went wrong.
ezekg
Contributor
|
||
} | ||
const upload: RequestOptions = { | ||
hostname: this.hostname, | ||
path: `${this.basePath}/${releaseId}/artifact`, | ||
headers: { | ||
Accept: "application/vnd.api+json", | ||
"Content-Length": dataLength, | ||
}, | ||
} | ||
await httpExecutor.doApiRequest(configureRequestOptions(upload, this.auth, "PUT"), this.context.cancellationToken, requestProcessor) | ||
return releaseId | ||
} catch (e) { | ||
if (attemptNumber < 3 && ((e instanceof HttpError && e.statusCode === 502) || e.code === "EPIPE")) { | ||
This comment has been minimized.
Sorry, something went wrong.
ezekg
Contributor
|
||
continue | ||
} | ||
|
||
throw e | ||
} | ||
} | ||
} | ||
|
||
private async upsertVersion(fileName: string, dataLength: number): Promise<{ data: any; errors: any }> { | ||
const req: RequestOptions = { | ||
hostname: this.hostname, | ||
method: "PUT", | ||
path: this.basePath, | ||
headers: { | ||
"Content-Type": "application/vnd.api+json", | ||
Accept: "application/vnd.api+json", | ||
}, | ||
} | ||
const data = { | ||
data: { | ||
type: "release", | ||
attributes: { | ||
filename: fileName, | ||
filetype: path.extname(fileName), | ||
filesize: dataLength, | ||
version: this.version, | ||
platform: this.info.platform, | ||
channel: this.info.channel || "stable", | ||
}, | ||
relationships: { | ||
product: { | ||
data: { | ||
type: "product", | ||
id: this.info.product, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
log.debug({ data: JSON.stringify(data) }, "Keygen upsert release") | ||
return parseJson(httpExecutor.request(configureRequestOptions(req, this.auth, "PUT"), this.context.cancellationToken, data)) | ||
} | ||
|
||
async deleteRelease(releaseId: string): Promise<void> { | ||
const req: RequestOptions = { | ||
hostname: this.hostname, | ||
path: `${this.basePath}/${releaseId}`, | ||
headers: { | ||
Accept: "application/vnd.api+json", | ||
}, | ||
} | ||
await httpExecutor.request(configureRequestOptions(req, this.auth, "DELETE"), this.context.cancellationToken) | ||
} | ||
|
||
toString() { | ||
const { account, product, platform } = this.info | ||
return `Keygen (account: ${account}, product: ${product}, platform: ${platform}, version: ${this.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
Oops, something went wrong.
The
data
anderrors
properties will never be returned at the same time. So on API error, this will be accessingundefined.id
, which will throw.I suggest moving the
if (errors) { ... }
block before accessingdata.id
.