From 18c3117e5014929de9024d10d34a9b70cecb62cb Mon Sep 17 00:00:00 2001 From: Manas Jayanth <3097018+ManasJayanth@users.noreply.github.com> Date: Thu, 6 Feb 2025 14:41:23 +0530 Subject: [PATCH] Fixes compiler version in postinstall command --- dist/index.js | 55 ++++++++++++++++++++++++++++++++++++++++++++------- index.ts | 29 +++++++++++++++++++++++++-- 2 files changed, 75 insertions(+), 9 deletions(-) diff --git a/dist/index.js b/dist/index.js index 9809e3b..2698b3d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -95238,6 +95238,14 @@ const { isUint8Array, isArrayBuffer } = __nccwpck_require__(84978) const { File: UndiciFile } = __nccwpck_require__(78511) const { parseMIMEType, serializeAMimeType } = __nccwpck_require__(685) +let random +try { + const crypto = __nccwpck_require__(6005) + random = (max) => crypto.randomInt(0, max) +} catch { + random = (max) => Math.floor(Math.random(max)) +} + let ReadableStream = globalThis.ReadableStream /** @type {globalThis['File']} */ @@ -95323,7 +95331,7 @@ function extractBody (object, keepalive = false) { // Set source to a copy of the bytes held by object. source = new Uint8Array(object.buffer.slice(object.byteOffset, object.byteOffset + object.byteLength)) } else if (util.isFormDataLike(object)) { - const boundary = `----formdata-undici-0${`${Math.floor(Math.random() * 1e11)}`.padStart(11, '0')}` + const boundary = `----formdata-undici-0${`${random(1e11)}`.padStart(11, '0')}` const prefix = `--${boundary}\r\nContent-Disposition: form-data` /*! formdata-polyfill. MIT License. Jimmy Wärting */ @@ -113058,6 +113066,14 @@ ZipStream.prototype.finalize = function() { */ +/***/ }), + +/***/ 41901: +/***/ ((module) => { + +module.exports = eval("require")("./esy.lock/index.json"); + + /***/ }), /***/ 22877: @@ -113212,6 +113228,14 @@ module.exports = require("net"); /***/ }), +/***/ 6005: +/***/ ((module) => { + +"use strict"; +module.exports = require("node:crypto"); + +/***/ }), + /***/ 15673: /***/ ((module) => { @@ -137462,8 +137486,8 @@ const create = makeCommand(createFileSync, createFile, createSync, createAsync, //# sourceMappingURL=create.js.map ;// CONCATENATED MODULE: external "node:assert" const external_node_assert_namespaceObject = require("node:assert"); -;// CONCATENATED MODULE: external "node:crypto" -const external_node_crypto_namespaceObject = require("node:crypto"); +// EXTERNAL MODULE: external "node:crypto" +var external_node_crypto_ = __nccwpck_require__(6005); ;// CONCATENATED MODULE: ./node_modules/tar/dist/esm/get-write-flag.js // Get the appropriate flag to use for creating files // We use fmap on Windows platforms for files less than @@ -138329,7 +138353,7 @@ const unlinkFile = (path, cb) => { if (!unpack_isWindows) { return external_node_fs_.unlink(path, cb); } - const name = path + '.DELETE.' + (0,external_node_crypto_namespaceObject.randomBytes)(16).toString('hex'); + const name = path + '.DELETE.' + (0,external_node_crypto_.randomBytes)(16).toString('hex'); external_node_fs_.rename(path, name, er => { if (er) { return cb(er); @@ -138343,7 +138367,7 @@ const unlinkFileSync = (path) => { if (!unpack_isWindows) { return external_node_fs_.unlinkSync(path); } - const name = path + '.DELETE.' + (0,external_node_crypto_namespaceObject.randomBytes)(16).toString('hex'); + const name = path + '.DELETE.' + (0,external_node_crypto_.randomBytes)(16).toString('hex'); external_node_fs_.renameSync(path, name); external_node_fs_.unlinkSync(name); }; @@ -139820,6 +139844,23 @@ function bundleNPMArtifacts() { return external_child_process_.execSync(cmd).toString().trim(); } const version = exec("git describe --tags --always"); + function getCompilerVersion(sandbox = "esy") { + console.log(`Looking up ${sandbox}.lock/index.json for compiler version`); + const ocamlPackages = Object.keys((__nccwpck_require__(41901).node)).filter(k => k.startsWith("ocaml@")); + if (ocamlPackages.length === 0) { + throw new Error("Couldn't figure ocaml compiler version from lock file because no ocaml-like packages were found"); + } + const ocamlPackageID = ocamlPackages[0]; + const ocamlPackageIDParts = ocamlPackageID.split("@"); + if (ocamlPackageIDParts.length !== 3) { + throw new Error(`Couldn't figure ocaml compiler version from lock file because PackageId wasn't in expected format: ${ocamlPackageID}`); + } + return ocamlPackageIDParts[1]; + } + const compilerVersion = getCompilerVersion(); + console.log("Found compiler version", compilerVersion); + const staticCompilerVersion = getCompilerVersion("static.esy"); + console.log("Found static compiler version", staticCompilerVersion); const packageJson = JSON.stringify({ name: mainPackageJson.name, version, @@ -139828,8 +139869,8 @@ function bundleNPMArtifacts() { repository: mainPackageJson.repository, scripts: { postinstall: rewritePrefix - ? "node -e \"process.env['OCAML_VERSION'] = process.platform == 'linux' ? '4.12.0-musl.static.flambda': '4.12.0'; process.env['OCAML_PKG_NAME'] = 'ocaml'; process.env['ESY_RELEASE_REWRITE_PREFIX']=true; require('./postinstall.js')\"" - : "node -e \"process.env['OCAML_VERSION'] = process.platform == 'linux' ? '4.12.0-musl.static.flambda': '4.12.0'; process.env['OCAML_PKG_NAME'] = 'ocaml'; require('./postinstall.js')\"", + ? `node -e \"process.env['OCAML_VERSION'] = process.platform == 'linux' ? '${staticCompilerVersion}-musl.static.flambda': '${compilerVersion}'; process.env['OCAML_PKG_NAME'] = 'ocaml'; process.env['ESY_RELEASE_REWRITE_PREFIX']=true; require('./postinstall.js')\"` + : "require('./postinstall.js')\"", }, bin: bins, files: [ diff --git a/index.ts b/index.ts index d340f89..43904fc 100644 --- a/index.ts +++ b/index.ts @@ -410,6 +410,31 @@ async function bundleNPMArtifacts() { return cp.execSync(cmd).toString().trim(); } const version = exec("git describe --tags --always"); + + + function getCompilerVersion(sandbox = "esy") { + console.log(`Looking up ${sandbox}.lock/index.json for compiler version`); + const ocamlPackages = Object.keys(require("./esy.lock/index.json").node).filter(k => k.startsWith("ocaml@")); + + if (ocamlPackages.length === 0) { + throw new Error("Couldn't figure ocaml compiler version from lock file because no ocaml-like packages were found"); + } + + const ocamlPackageID = ocamlPackages[0]; + const ocamlPackageIDParts = ocamlPackageID.split("@"); + + if (ocamlPackageIDParts.length !== 3) { + throw new Error(`Couldn't figure ocaml compiler version from lock file because PackageId wasn't in expected format: ${ocamlPackageID}`); + } + + return ocamlPackageIDParts[1]; + } + + const compilerVersion = getCompilerVersion(); + console.log("Found compiler version", compilerVersion); + const staticCompilerVersion = getCompilerVersion("static.esy"); + console.log("Found static compiler version", staticCompilerVersion); + const packageJson = JSON.stringify( { name: mainPackageJson.name, @@ -419,8 +444,8 @@ async function bundleNPMArtifacts() { repository: mainPackageJson.repository, scripts: { postinstall: rewritePrefix - ? "node -e \"process.env['OCAML_VERSION'] = process.platform == 'linux' ? '4.12.0-musl.static.flambda': '4.12.0'; process.env['OCAML_PKG_NAME'] = 'ocaml'; process.env['ESY_RELEASE_REWRITE_PREFIX']=true; require('./postinstall.js')\"" - : "node -e \"process.env['OCAML_VERSION'] = process.platform == 'linux' ? '4.12.0-musl.static.flambda': '4.12.0'; process.env['OCAML_PKG_NAME'] = 'ocaml'; require('./postinstall.js')\"", + ? `node -e \"process.env['OCAML_VERSION'] = process.platform == 'linux' ? '${staticCompilerVersion}-musl.static.flambda': '${compilerVersion}'; process.env['OCAML_PKG_NAME'] = 'ocaml'; process.env['ESY_RELEASE_REWRITE_PREFIX']=true; require('./postinstall.js')\"` + : "require('./postinstall.js')\"", }, bin: bins, files: [