|
| 1 | +import JSZip from "jszip"; |
| 2 | +import registryJson from "./extensions.json"; |
| 3 | +import crypto from "node:crypto"; |
| 4 | +import path from "node:path"; |
| 5 | +import * as core from "@actions/core"; |
| 6 | + |
| 7 | +function warning(message: string) { |
| 8 | + core.warning(message, { file: "extensions.json" }); |
| 9 | +} |
| 10 | + |
| 11 | +function error(message: string) { |
| 12 | + core.error(message, { file: "extensions.json" }); |
| 13 | + process.exitCode = core.ExitCode.Failure; |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * SHA sums of extensions that are known to be uncompressed. For these we will only log a warning. |
| 18 | + * For other (newer) ones it will be an error if compression is not used. |
| 19 | + */ |
| 20 | +const knownUncompressedExtensionsSHAs = [ |
| 21 | + "fa2b11af8ed7c420ca6e541196bca608661c0c1a81cd1f768c565c72a55a63c8", |
| 22 | + "ac07f5f84b96ad1139b4d66b685b864cf5713081e198e63fccef7a0546dd1ab2", |
| 23 | + "1193589eb2779a1224328defca4e2ca378ef786474be1842ac43b674b9535d82", |
| 24 | + "f03d7a517ba6f7d57564eec965358055c87285a8a504d5488af969767a76c4ab", |
| 25 | + "71ae6c6ffa4b86aa427d14fcc86af437fdca4b5e10cd714e70f95e1af324ceaf", |
| 26 | + "87e0a4fe397974fb2e3771f370009ccdbe195498d4792c04bc08b2e2482bda71", |
| 27 | + "6afa4437c45cb5b60a99405e40b0c51b4edfcc0f9a640532cc218962da1ee2b8", |
| 28 | + "b5c02cb834005affcf1873a6a481281a73428d04062bbf763863c8fb7e64fc95", |
| 29 | + "12863df314ec682e23ebb74598e3e3e89e610fa960f3461b5e91a6896ed8228f", |
| 30 | + "bfbb33b91f337a25de79449f9b529bc94bfaf908187f39cef24d70d4e1083434", |
| 31 | + "5b2dbe3280a7833c8d827e4f3b8acfa4df9cf4cd16a5db037885a6cfcdd42041", |
| 32 | + "d7b831c665111aeab5779bea6f5b8f7d5c3afa6d2200d1f79ba2bafbb799497c", |
| 33 | + "90af3124c1a68ab5b9b5d3faa480d1c77c0dd902ac1c7a71dea3f445c2df7939", |
| 34 | + "249be37fe7c42a2bf647b344f639b755956b5f656e232770b13353dea75c7696", |
| 35 | + "3c1ba8195e31809939c89ce73320cdc47147f012f41b050029f6aae4dabc7c93", |
| 36 | +]; |
| 37 | + |
| 38 | +/** |
| 39 | + * Check that a URL points at a plain-text file (Markdown). This helps avoid accidentally pointing |
| 40 | + * at an HTML page (like a non-raw github.com URL) |
| 41 | + */ |
| 42 | +async function validateMarkdownUrl(url: string, id: string, name: string) { |
| 43 | + if (url.startsWith("https://github.com/")) { |
| 44 | + error( |
| 45 | + `${id}: Invalid ${name} URL: use raw.githubusercontent.com instead of github.com` |
| 46 | + ); |
| 47 | + return; |
| 48 | + } |
| 49 | + const response = await fetch(url); |
| 50 | + const contentType = response.headers.get("content-type"); |
| 51 | + if (!response.ok) { |
| 52 | + error( |
| 53 | + `${id}: Invalid ${name} URL: expected status 200, got ${response.status} ${response.statusText}` |
| 54 | + ); |
| 55 | + return; |
| 56 | + } |
| 57 | + if (contentType == undefined || !contentType.startsWith("text/plain")) { |
| 58 | + error( |
| 59 | + `${id}: Invalid ${name} URL: expected content-type text/plain, got: ${contentType}` |
| 60 | + ); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +async function validateExtension(extension: (typeof registryJson)[number]) { |
| 65 | + console.log(`\nValidating ${extension.id}...`); |
| 66 | + const foxeResponse = await fetch(extension.foxe); |
| 67 | + if (foxeResponse.status !== 200) { |
| 68 | + error( |
| 69 | + `Extension ${extension.id} has invalid foxe URL. Expected 200 response, got ${foxeResponse.status}` |
| 70 | + ); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + const foxeContent = await foxeResponse.arrayBuffer(); |
| 75 | + const sha256sum = crypto |
| 76 | + .createHash("sha256") |
| 77 | + .update(new DataView(foxeContent)) |
| 78 | + .digest("hex"); |
| 79 | + if (sha256sum !== extension.sha256sum) { |
| 80 | + error( |
| 81 | + `${extension.id}:Digest mismatch for ${extension.foxe}, expected: ${extension.sha256sum}, actual: ${sha256sum}` |
| 82 | + ); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + const zip = await JSZip.loadAsync(foxeContent, { checkCRC32: true }); |
| 87 | + let uncompressedFiles = []; |
| 88 | + let anyFilesAreCompressed = false; |
| 89 | + for (const [path, zipObj] of Object.entries(zip.files)) { |
| 90 | + if (zipObj.dir) continue; |
| 91 | + if (zipObj.options.compression === "DEFLATE") { |
| 92 | + anyFilesAreCompressed = true; |
| 93 | + } else { |
| 94 | + uncompressedFiles.push(path); |
| 95 | + } |
| 96 | + } |
| 97 | + if (uncompressedFiles.length > 0) { |
| 98 | + (knownUncompressedExtensionsSHAs.includes(extension.sha256sum) |
| 99 | + ? warning |
| 100 | + : error)( |
| 101 | + `${extension.id}: the following files are stored without compression: ${ |
| 102 | + anyFilesAreCompressed ? uncompressedFiles.join(", ") : "(all files)" |
| 103 | + }` |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + const packageJsonContent = await zip.file("package.json")?.async("string"); |
| 108 | + if (!packageJsonContent) { |
| 109 | + error(`${extension.id}: Missing package.json`); |
| 110 | + return; |
| 111 | + } |
| 112 | + const packageJson = JSON.parse(packageJsonContent); |
| 113 | + if (packageJson.name == undefined || packageJson.name.length === 0) { |
| 114 | + error(`${extension.id}: Invalid package.json: missing name`); |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + const mainPath = packageJson.main; |
| 119 | + if (typeof mainPath !== "string") { |
| 120 | + error(`${extension.id}: Invalid package.json: missing "main" field`); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + // Normalize the package.json:main field so we can lookup the file in the zip archive. |
| 125 | + // This turns ./dist/extension.js into dist/extension.js because having a leading `./` is not |
| 126 | + // supported by jszip. |
| 127 | + const normalized = path.normalize(mainPath); |
| 128 | + const srcText = await zip.file(normalized)?.async("string"); |
| 129 | + if (srcText == undefined) { |
| 130 | + error( |
| 131 | + `${extension.id}: Extension ${extension.foxe} is corrupted: unable to extract main JS file` |
| 132 | + ); |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + await validateMarkdownUrl(extension.readme, extension.id, "readme"); |
| 137 | + await validateMarkdownUrl(extension.changelog, extension.id, "changelog"); |
| 138 | +} |
| 139 | + |
| 140 | +async function main() { |
| 141 | + const unusedSHAs = knownUncompressedExtensionsSHAs.filter( |
| 142 | + (sha) => !registryJson.find((ext) => ext.sha256sum === sha) |
| 143 | + ); |
| 144 | + for (const sha of unusedSHAs) { |
| 145 | + error( |
| 146 | + `The following SHA should be removed from knownUncompressedExtensionsSHAs: ${sha}` |
| 147 | + ); |
| 148 | + } |
| 149 | + |
| 150 | + for (const extension of registryJson) { |
| 151 | + await validateExtension(extension); |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +void main().catch((err) => { |
| 156 | + console.error(err); |
| 157 | + process.exit(1); |
| 158 | +}); |
0 commit comments