forked from microsoft/code-push-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsign.ts
80 lines (69 loc) · 2.43 KB
/
sign.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import * as fs from "fs/promises";
import * as hashUtils from "./hash-utils";
import * as path from "path";
import * as jwt from "jsonwebtoken";
import { copyFileToTmpDir, isDirectory } from "./utils/file-utils";
const CURRENT_CLAIM_VERSION: string = "1.0.0";
const METADATA_FILE_NAME: string = ".codepushrelease";
interface CodeSigningClaims {
claimVersion: string;
contentHash: string;
}
export default async function sign(privateKeyPath: string, updateContentsPath: string): Promise<void> {
if (!privateKeyPath) {
return Promise.resolve<void>(null);
}
let privateKey: Buffer;
try {
privateKey = await fs.readFile(privateKeyPath);
} catch (err) {
return Promise.reject(new Error(`The path specified for the signing key ("${privateKeyPath}") was not valid.`));
}
// If releasing a single file, copy the file to a temporary 'CodePush' directory in which to publish the release
try {
if (!isDirectory(updateContentsPath)) {
updateContentsPath = copyFileToTmpDir(updateContentsPath);
}
} catch (error) {
Promise.reject(error);
}
const signatureFilePath: string = path.join(updateContentsPath, METADATA_FILE_NAME);
let prevSignatureExists = true;
try {
await fs.access(signatureFilePath, fs.constants.F_OK);
} catch (err) {
if (err.code === "ENOENT") {
prevSignatureExists = false;
} else {
return Promise.reject<void>(
new Error(
`Could not delete previous release signature at ${signatureFilePath}.
Please, check your access rights.`
)
);
}
}
if (prevSignatureExists) {
console.log(`Deleting previous release signature at ${signatureFilePath}`);
await fs.rmdir(signatureFilePath);
}
const hash: string = await hashUtils.generatePackageHashFromDirectory(updateContentsPath, path.join(updateContentsPath, ".."));
const claims: CodeSigningClaims = {
claimVersion: CURRENT_CLAIM_VERSION,
contentHash: hash,
};
return new Promise<void>((resolve, reject) => {
jwt.sign(claims, privateKey, { algorithm: "RS256" }, async (err: Error, signedJwt: string) => {
if (err) {
reject(new Error("The specified signing key file was not valid"));
}
try {
await fs.writeFile(signatureFilePath, signedJwt);
console.log(`Generated a release signature and wrote it to ${signatureFilePath}`);
resolve(null);
} catch (error) {
reject(error);
}
});
});
}