UNPKG

@hot-updater/react-native

Version:

React Native OTA solution for self-hosted

288 lines (287 loc) 15.3 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var promises_1 = require("node:fs/promises"); var cli_tools_1 = require("@hot-updater/cli-tools"); var config_plugins_1 = require("expo/config-plugins"); var hot_updater_1 = require("hot-updater"); var path_1 = __importDefault(require("path")); var package_json_1 = __importDefault(require("../../package.json")); var transformers_1 = require("./transformers"); var fingerprintCache = null; var getFingerprint = function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: if (fingerprintCache) { return [2 /*return*/, fingerprintCache]; } return [4 /*yield*/, (0, hot_updater_1.generateFingerprints)()]; case 1: fingerprintCache = _a.sent(); return [4 /*yield*/, (0, hot_updater_1.createFingerprintJSON)(fingerprintCache)]; case 2: _a.sent(); return [2 /*return*/, fingerprintCache]; } }); }); }; /** * Extract public key for embedding in native configs. * Supports multiple sources with priority order: * 1. HOT_UPDATER_PRIVATE_KEY environment variable * 2. Private key file (extract public key) * 3. Public key file (derived from privateKeyPath) * 4. Skip with warning (graceful fallback) */ var getPublicKeyFromConfig = function (signingConfig) { return __awaiter(void 0, void 0, void 0, function () { var envPrivateKey, publicKeyPEM, privateKeyPath, publicKeyPath, privateKeyPEM, publicKeyPEM, _privateKeyError_1, publicKeyPEM, _publicKeyError_1; return __generator(this, function (_a) { switch (_a.label) { case 0: // If signing not enabled, no public key needed if (!(signingConfig === null || signingConfig === void 0 ? void 0 : signingConfig.enabled)) { return [2 /*return*/, null]; } envPrivateKey = process.env.HOT_UPDATER_PRIVATE_KEY; if (envPrivateKey) { try { publicKeyPEM = (0, hot_updater_1.getPublicKeyFromPrivate)(envPrivateKey); console.log("[hot-updater] Using public key extracted from HOT_UPDATER_PRIVATE_KEY environment variable"); return [2 /*return*/, publicKeyPEM.trim()]; } catch (error) { console.warn("[hot-updater] WARNING: Failed to extract public key from HOT_UPDATER_PRIVATE_KEY:\n" + "".concat(error instanceof Error ? error.message : String(error), "\n")); // Continue to try other methods } } // If no privateKeyPath configured, can't proceed with file-based methods if (!signingConfig.privateKeyPath) { console.warn("[hot-updater] WARNING: signing.enabled is true but no privateKeyPath configured.\n" + "Public key will not be embedded. Set HOT_UPDATER_PRIVATE_KEY environment variable or configure privateKeyPath."); return [2 /*return*/, null]; } privateKeyPath = path_1.default.isAbsolute(signingConfig.privateKeyPath) ? signingConfig.privateKeyPath : path_1.default.resolve(process.cwd(), signingConfig.privateKeyPath); publicKeyPath = privateKeyPath.replace(/private-key\.pem$/, "public-key.pem"); _a.label = 1; case 1: _a.trys.push([1, 3, , 8]); return [4 /*yield*/, (0, hot_updater_1.loadPrivateKey)(privateKeyPath)]; case 2: privateKeyPEM = _a.sent(); publicKeyPEM = (0, hot_updater_1.getPublicKeyFromPrivate)(privateKeyPEM); console.log("[hot-updater] Extracted public key from ".concat(privateKeyPath)); return [2 /*return*/, publicKeyPEM.trim()]; case 3: _privateKeyError_1 = _a.sent(); _a.label = 4; case 4: _a.trys.push([4, 6, , 7]); return [4 /*yield*/, (0, promises_1.readFile)(publicKeyPath, "utf-8")]; case 5: publicKeyPEM = _a.sent(); console.log("[hot-updater] Using public key from ".concat(publicKeyPath)); return [2 /*return*/, publicKeyPEM.trim()]; case 6: _publicKeyError_1 = _a.sent(); // Priority 4: All sources failed - throw error throw new Error("[hot-updater] Failed to load public key for bundle signing.\n\n" + "Signing is enabled (signing.enabled: true) but no public key sources found.\n\n" + "For EAS builds, use EAS Secrets:\n" + ' eas env:create --name HOT_UPDATER_PRIVATE_KEY --value "$(cat keys/private-key.pem)"\n\n' + "Or add to eas.json:\n" + ' "env": { "HOT_UPDATER_PRIVATE_KEY": "-----BEGIN PRIVATE KEY-----\\n..." }\n\n' + "For local development:\n" + " npx hot-updater keys generate\n\n" + "Searched locations:\n" + " - HOT_UPDATER_PRIVATE_KEY environment variable\n" + " - Private key file: ".concat(privateKeyPath, "\n") + " - Public key file: ".concat(publicKeyPath, "\n")); case 7: return [3 /*break*/, 8]; case 8: return [2 /*return*/]; } }); }); }; /** * Native code modifications - should only run once */ var withHotUpdaterNativeCode = function (config) { var modifiedConfig = config; // === iOS: Objective-C & Swift in AppDelegate === modifiedConfig = (0, config_plugins_1.withAppDelegate)(modifiedConfig, function (cfg) { var contents = cfg.modResults.contents; contents = (0, transformers_1.transformIOS)(contents); cfg.modResults.contents = contents; return cfg; }); // === Android: Kotlin in MainApplication === modifiedConfig = (0, config_plugins_1.withMainApplication)(modifiedConfig, function (cfg) { var contents = cfg.modResults.contents; contents = (0, transformers_1.transformAndroid)(contents); cfg.modResults.contents = contents; return cfg; }); return modifiedConfig; }; /** * Configuration updates - should run every time */ var withHotUpdaterConfigAsync = function (props) { return function (config) { var channel = props.channel || "production"; var modifiedConfig = config; // === iOS: Add channel and fingerprint to Info.plist === modifiedConfig = (0, config_plugins_1.withInfoPlist)(modifiedConfig, function (cfg) { return __awaiter(void 0, void 0, void 0, function () { var fingerprintHash, config, fingerprint, publicKey; return __generator(this, function (_a) { switch (_a.label) { case 0: fingerprintHash = null; return [4 /*yield*/, (0, cli_tools_1.loadConfig)(null)]; case 1: config = _a.sent(); if (!(config.updateStrategy !== "appVersion")) return [3 /*break*/, 3]; return [4 /*yield*/, getFingerprint()]; case 2: fingerprint = _a.sent(); fingerprintHash = fingerprint.ios.hash; _a.label = 3; case 3: return [4 /*yield*/, getPublicKeyFromConfig(config.signing)]; case 4: publicKey = _a.sent(); cfg.modResults.HOT_UPDATER_CHANNEL = channel; if (fingerprintHash) { cfg.modResults.HOT_UPDATER_FINGERPRINT_HASH = fingerprintHash; } if (publicKey) { cfg.modResults.HOT_UPDATER_PUBLIC_KEY = publicKey; } return [2 /*return*/, cfg]; } }); }); }); // === Android: Add channel and fingerprint to strings.xml === modifiedConfig = (0, config_plugins_1.withStringsXml)(modifiedConfig, function (cfg) { return __awaiter(void 0, void 0, void 0, function () { var fingerprintHash, config, fingerprint, publicKey; return __generator(this, function (_a) { switch (_a.label) { case 0: fingerprintHash = null; return [4 /*yield*/, (0, cli_tools_1.loadConfig)(null)]; case 1: config = _a.sent(); if (!(config.updateStrategy !== "appVersion")) return [3 /*break*/, 3]; return [4 /*yield*/, getFingerprint()]; case 2: fingerprint = _a.sent(); fingerprintHash = fingerprint.android.hash; _a.label = 3; case 3: return [4 /*yield*/, getPublicKeyFromConfig(config.signing)]; case 4: publicKey = _a.sent(); // Ensure resources object exists if (!cfg.modResults.resources) { cfg.modResults.resources = {}; } if (!cfg.modResults.resources.string) { cfg.modResults.resources.string = []; } // Remove existing hot_updater_channel entry if it exists cfg.modResults.resources.string = cfg.modResults.resources.string.filter(function (item) { return !(item.$ && item.$.name === "hot_updater_channel"); }); // Add the new hot_updater_channel entry cfg.modResults.resources.string.push({ $: { name: "hot_updater_channel", moduleConfig: "true", }, _: channel, }); if (fingerprintHash) { // Remove existing hot_updater_fingerprint_hash entry if it exists cfg.modResults.resources.string = cfg.modResults.resources.string.filter(function (item) { return !(item.$ && item.$.name === "hot_updater_fingerprint_hash"); }); // Add the new hot_updater_fingerprint_hash entry cfg.modResults.resources.string.push({ $: { name: "hot_updater_fingerprint_hash", moduleConfig: "true", }, _: fingerprintHash, }); } if (publicKey) { // Remove existing hot_updater_public_key entry if it exists cfg.modResults.resources.string = cfg.modResults.resources.string.filter(function (item) { return !(item.$ && item.$.name === "hot_updater_public_key"); }); // Add the new hot_updater_public_key entry cfg.modResults.resources.string.push({ $: { name: "hot_updater_public_key", moduleConfig: "true", }, _: publicKey, }); } return [2 /*return*/, cfg]; } }); }); }); return modifiedConfig; }; }; /** * Main plugin that combines both native code (run once) and config (run always) */ var withHotUpdater = function (config, props) { if (props === void 0) { props = {}; } // Apply plugins in order return (0, config_plugins_1.withPlugins)(config, [ // Native code modifications - wrapped with createRunOncePlugin (0, config_plugins_1.createRunOncePlugin)(withHotUpdaterNativeCode, "".concat(package_json_1.default.name, "-native"), package_json_1.default.version), // Configuration updates - runs every time withHotUpdaterConfigAsync(props), ]); }; // Export the main plugin exports.default = withHotUpdater;