UNPKG

7.18 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.KeygenPublisher = void 0;
4const builder_util_1 = require("builder-util");
5const nodeHttpExecutor_1 = require("builder-util/out/nodeHttpExecutor");
6const electron_publish_1 = require("electron-publish");
7const builder_util_runtime_1 = require("builder-util-runtime");
8const filename_1 = require("../util/filename");
9class KeygenPublisher extends electron_publish_1.HttpPublisher {
10 constructor(context, info, version) {
11 super(context);
12 this.providerName = "keygen";
13 this.hostname = "api.keygen.sh";
14 const token = process.env.KEYGEN_TOKEN;
15 if ((0, builder_util_1.isEmptyOrSpaces)(token)) {
16 throw new builder_util_1.InvalidConfigurationError(`Keygen token is not set using env "KEYGEN_TOKEN" (see https://www.electron.build/configuration/publish#KeygenOptions)`);
17 }
18 this.info = info;
19 this.auth = `Bearer ${token.trim()}`;
20 this.version = version;
21 this.basePath = `/v1/accounts/${this.info.account}`;
22 }
23 doUpload(fileName, _arch, dataLength, requestProcessor,
24 // eslint-disable-next-line @typescript-eslint/no-unused-vars
25 _file) {
26 return builder_util_runtime_1.HttpExecutor.retryOnServerError(async () => {
27 const { data, errors } = await this.getOrCreateRelease();
28 if (errors) {
29 throw new Error(`Keygen - Creating release returned errors: ${JSON.stringify(errors)}`);
30 }
31 await this.uploadArtifact(data.id, fileName, dataLength, requestProcessor);
32 return data.id;
33 });
34 }
35 async uploadArtifact(releaseId, fileName, dataLength, requestProcessor) {
36 const { data, errors } = await this.createArtifact(releaseId, fileName, dataLength);
37 if (errors) {
38 throw new Error(`Keygen - Creating artifact returned errors: ${JSON.stringify(errors)}`);
39 }
40 // Follow the redirect and upload directly to S3-equivalent storage provider
41 const url = new URL(data.links.redirect);
42 const upload = {
43 hostname: url.hostname,
44 path: url.pathname + url.search,
45 headers: {
46 "Content-Length": dataLength,
47 },
48 timeout: this.info.timeout || undefined,
49 };
50 await nodeHttpExecutor_1.httpExecutor.doApiRequest((0, builder_util_runtime_1.configureRequestOptions)(upload, null, "PUT"), this.context.cancellationToken, requestProcessor);
51 }
52 async createArtifact(releaseId, fileName, dataLength) {
53 const upload = {
54 hostname: this.hostname,
55 path: `${this.basePath}/artifacts`,
56 headers: {
57 "Content-Type": "application/vnd.api+json",
58 Accept: "application/vnd.api+json",
59 "Keygen-Version": "1.1",
60 Prefer: "no-redirect",
61 },
62 timeout: this.info.timeout || undefined,
63 };
64 const data = {
65 type: "artifacts",
66 attributes: {
67 filename: fileName,
68 filetype: (0, filename_1.getCompleteExtname)(fileName),
69 filesize: dataLength,
70 platform: this.info.platform,
71 },
72 relationships: {
73 release: {
74 data: {
75 type: "releases",
76 id: releaseId,
77 },
78 },
79 },
80 };
81 builder_util_1.log.debug({ data: JSON.stringify(data) }, "Keygen create artifact");
82 return (0, builder_util_runtime_1.parseJson)(nodeHttpExecutor_1.httpExecutor.request((0, builder_util_runtime_1.configureRequestOptions)(upload, this.auth, "POST"), this.context.cancellationToken, { data }));
83 }
84 async getOrCreateRelease() {
85 try {
86 // First, we'll attempt to fetch the release.
87 return await this.getRelease();
88 }
89 catch (e) {
90 if (e.statusCode !== 404) {
91 throw e;
92 }
93 try {
94 // Next, if the release doesn't exist, we'll attempt to create it.
95 return await this.createRelease();
96 }
97 catch (e) {
98 if (e.statusCode !== 409 && e.statusCode !== 422) {
99 throw e;
100 }
101 // Lastly, when a conflict occurs (in the case of parallel uploads),
102 // we'll try to fetch it one last time.
103 return this.getRelease();
104 }
105 }
106 }
107 async getRelease() {
108 const req = {
109 hostname: this.hostname,
110 path: `${this.basePath}/releases/${this.version}?product=${this.info.product}`,
111 headers: {
112 Accept: "application/vnd.api+json",
113 "Keygen-Version": "1.1",
114 },
115 timeout: this.info.timeout || undefined,
116 };
117 return (0, builder_util_runtime_1.parseJson)(nodeHttpExecutor_1.httpExecutor.request((0, builder_util_runtime_1.configureRequestOptions)(req, this.auth, "GET"), this.context.cancellationToken, null));
118 }
119 async createRelease() {
120 const req = {
121 hostname: this.hostname,
122 path: `${this.basePath}/releases`,
123 headers: {
124 "Content-Type": "application/vnd.api+json",
125 Accept: "application/vnd.api+json",
126 "Keygen-Version": "1.1",
127 },
128 timeout: this.info.timeout || undefined,
129 };
130 const data = {
131 type: "releases",
132 attributes: {
133 version: this.version,
134 channel: this.info.channel || "stable",
135 status: "PUBLISHED",
136 },
137 relationships: {
138 product: {
139 data: {
140 type: "products",
141 id: this.info.product,
142 },
143 },
144 },
145 };
146 builder_util_1.log.debug({ data: JSON.stringify(data) }, "Keygen create release");
147 return (0, builder_util_runtime_1.parseJson)(nodeHttpExecutor_1.httpExecutor.request((0, builder_util_runtime_1.configureRequestOptions)(req, this.auth, "POST"), this.context.cancellationToken, { data }));
148 }
149 async deleteRelease(releaseId) {
150 const req = {
151 hostname: this.hostname,
152 path: `${this.basePath}/releases/${releaseId}`,
153 headers: {
154 Accept: "application/vnd.api+json",
155 "Keygen-Version": "1.1",
156 },
157 timeout: this.info.timeout || undefined,
158 };
159 await nodeHttpExecutor_1.httpExecutor.request((0, builder_util_runtime_1.configureRequestOptions)(req, this.auth, "DELETE"), this.context.cancellationToken);
160 }
161 toString() {
162 const { account, product, platform } = this.info;
163 return `Keygen (account: ${account}, product: ${product}, platform: ${platform}, version: ${this.version})`;
164 }
165}
166exports.KeygenPublisher = KeygenPublisher;
167//# sourceMappingURL=KeygenPublisher.js.map
\No newline at end of file