UNPKG

yalive-server

Version:
187 lines (181 loc) 6.79 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/main.ts var main_exports = {}; __export(main_exports, { dev: () => dev }); module.exports = __toCommonJS(main_exports); // src/npm/node-platform.ts var fs = require("fs"); var os = require("os"); var path = require("path"); var YALIVE_BINARY_PATH = process.env.YALIVE_BINARY_PATH || YALIVE_BINARY_PATH; var knownWindowsPackages = { "win32 arm64 LE": "yalive-server-windows-arm64", // 'win32 ia32 LE': 'yalive-server-windows-32', "win32 x64 LE": "yalive-server-windows-64" }; var knownUnixlikePackages = { // 'android arm64 LE': 'yalive-server-android-arm64', "darwin arm64 LE": "yalive-server-darwin-arm64", "darwin x64 LE": "yalive-server-darwin-64", // 'freebsd arm64 LE': 'yalive-server-freebsd-arm64', // 'freebsd x64 LE': 'yalive-server-freebsd-64', // 'linux arm LE': 'yalive-server-linux-arm', "linux arm64 LE": "yalive-server-linux-arm64", // 'linux ia32 LE': 'yalive-server-linux-32', // 'linux mips64el LE': 'yalive-server-linux-mips64le', // 'linux ppc64 LE': 'yalive-server-linux-ppc64le', // 'linux s390x BE': 'yalive-server-linux-s390x', "linux x64 LE": "yalive-server-linux-64" // 'netbsd x64 LE': 'yalive-server-netbsd-64', // 'openbsd x64 LE': 'yalive-server-openbsd-64', // 'sunos x64 LE': 'yalive-server-sunos-64', }; function pkgAndSubpathForCurrentPlatform() { let pkg; let subpath; const platformKey = `${process.platform} ${os.arch()} ${os.endianness()}`; if (platformKey in knownWindowsPackages) { pkg = knownWindowsPackages[platformKey]; subpath = "bin/yalive-server.exe"; } else if (platformKey in knownUnixlikePackages) { pkg = knownUnixlikePackages[platformKey]; subpath = "bin/yalive-server"; } else { throw new Error(`Unsupported platform: ${platformKey}`); } return { pkg, subpath }; } function downloadedBinPath(pkg, subpath) { const libDir = path.dirname(require.resolve("yalive-server")); return path.join(libDir, `downloaded-${pkg}-${path.basename(subpath)}`); } function generateBinPath() { if (YALIVE_BINARY_PATH) { return YALIVE_BINARY_PATH; } const { pkg, subpath } = pkgAndSubpathForCurrentPlatform(); let binPath; try { binPath = require.resolve(`${pkg}/${subpath}`); } catch (e) { binPath = downloadedBinPath(pkg, subpath); if (!fs.existsSync(binPath)) { try { require.resolve(pkg); } catch { throw new Error(`The package "${pkg}" could not be found, and is needed by yalive-server. If you are installing yalive-server with npm, make sure that you don't specify the "--no-optional" flag. The "optionalDependencies" package.json feature is used by yalive-server to install the correct binary executable for your current platform.`); } throw e; } } let isYarnPnP = false; try { require("pnpapi"); isYarnPnP = true; } catch (_e) { } if (isYarnPnP) { const libDir = path.dirname(require.resolve("yalive-server")); const binTargetPath = path.join(libDir, `pnpapi-${pkg}-${path.basename(subpath)}`); if (!fs.existsSync(binTargetPath)) { fs.copyFileSync(binPath, binTargetPath); fs.chmodSync(binTargetPath, 493); } return binTargetPath; } return binPath; } // src/main.ts var child_process = require("child_process"); var path2 = require("path"); var defaultWD = process.cwd(); var isRunning = false; var yaliveCommandAndArgs = () => { if ((!YALIVE_BINARY_PATH || false) && (path2.basename(__filename) !== "main.js" || path2.basename(__dirname) !== "lib")) { throw new Error( /* eslint-disable quotes */ `The yalive-server JavaScript API cannot be bundled. Please mark the "yalive-server" package as external so it's not included in the bundle. More information: The file containing the code for yalive-server's JavaScript API (${__filename}) does not appear to be inside the yalive-server package on the file system, which usually means that the yalive-server package was bundled into another file. This is problematic because the API needs to run a binary executable inside the yalive-server package which is located using a relative path from the API code to the executable. If the yalive-server package is bundled, the relative path will be incorrect and the executable won't be found.` /* eslint-enable quotes */ ); } if (false) { return ["node", [path2.join(__dirname, "..", "bin", "yalive-server")]]; } return [generateBinPath(), []]; }; var dev = (options) => { if (isRunning) { return Promise.resolve(); } const [command, args] = yaliveCommandAndArgs(); const configStr = JSON.stringify(options); const child = child_process.spawn(command, args.concat("dev", "-c", configStr), { windowsHide: true, stdio: ["pipe", "pipe", "inherit"], cwd: defaultWD }); const stdin = child.stdin; const stdout = child.stdout; stdout.on("data", (chunk) => { console.debug(chunk); }); stdout.on("end", () => { console.log("server closed"); }); let refCount = 0; child.unref(); if (stdin.unref) { stdin.unref(); } if (stdout.unref) { stdout.unref(); } const _refs = { ref() { if (++refCount === 1) child.ref(); }, unref() { if (--refCount === 0) child.unref(); } }; return Promise.resolve(); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { dev });