From 11546a1ce97ee14d7f5ed8068e3b4a5fb1e67c1d Mon Sep 17 00:00:00 2001 From: Simon Laux Date: Mon, 20 May 2024 18:36:49 +0200 Subject: [PATCH] api!(npm rpc server): change api: don't search in path unless `options.takeVersionFromPATH` is set to `true` -remove `DELTA_CHAT_SKIP_PATH` environment variable - remove version check / search for dc rpc server in $PATH - remove `options.skipSearchInPath` - add `options.takeVersionFromPATH` --- deltachat-rpc-server/npm-package/README.md | 7 +- deltachat-rpc-server/npm-package/index.d.ts | 4 +- deltachat-rpc-server/npm-package/index.js | 90 +++---------------- deltachat-rpc-server/npm-package/src/const.js | 3 +- 4 files changed, 19 insertions(+), 85 deletions(-) diff --git a/deltachat-rpc-server/npm-package/README.md b/deltachat-rpc-server/npm-package/README.md index 66804b5b0..1fe05a355 100644 --- a/deltachat-rpc-server/npm-package/README.md +++ b/deltachat-rpc-server/npm-package/README.md @@ -46,12 +46,11 @@ references: When you import this package it searches for the rpc server in the following locations and order: 1. `DELTA_CHAT_RPC_SERVER` environment variable -2. in PATH - - unless `DELTA_CHAT_SKIP_PATH=1` is specified - - searches in .cargo/bin directory first - - but there an additional version check is performed +2. use the PATH when `{takeVersionFromPATH: true}` is supplied in the options. 3. prebuilds in npm packages +so by default it uses the prebuilds. + ## How do you built this package in CI - To build platform packages, run the `build_platform_package.py` script: diff --git a/deltachat-rpc-server/npm-package/index.d.ts b/deltachat-rpc-server/npm-package/index.d.ts index 640a65535..af9afded3 100644 --- a/deltachat-rpc-server/npm-package/index.d.ts +++ b/deltachat-rpc-server/npm-package/index.d.ts @@ -1,8 +1,8 @@ import { StdioDeltaChat } from "@deltachat/jsonrpc-client"; export interface SearchOptions { - /** whether to disable looking for deltachat-rpc-server inside of $PATH */ - skipSearchInPath: boolean; + /** whether take deltachat-rpc-server inside of $PATH*/ + takeVersionFromPATH: boolean; /** whether to disable the DELTA_CHAT_RPC_SERVER environment variable */ disableEnvPath: boolean; diff --git a/deltachat-rpc-server/npm-package/index.js b/deltachat-rpc-server/npm-package/index.js index 6c0d4c31f..a6e4dfdde 100644 --- a/deltachat-rpc-server/npm-package/index.js +++ b/deltachat-rpc-server/npm-package/index.js @@ -1,15 +1,9 @@ //@ts-check -import { execFile, spawn } from "node:child_process"; -import { stat, readdir } from "node:fs/promises"; +import { spawn } from "node:child_process"; +import { stat } from "node:fs/promises"; import os from "node:os"; -import { join, basename } from "node:path"; import process from "node:process"; -import { promisify } from "node:util"; -import { - ENV_VAR_NAME, - PATH_EXECUTABLE_NAME, - SKIP_SEARCH_IN_PATH, -} from "./src/const.js"; +import { ENV_VAR_NAME, PATH_EXECUTABLE_NAME } from "./src/const.js"; import { ENV_VAR_LOCATION_NOT_FOUND, FAILED_TO_START_SERVER_EXECUTABLE, @@ -39,38 +33,13 @@ function findRPCServerInNodeModules() { } } -/** - * @returns {Promise} - */ -async function getLocationInPath() { - const exec = promisify(execFile); - - if (os.platform() === "win32") { - const { stdout: executable } = await exec("where", [PATH_EXECUTABLE_NAME], { - shell: true, - }); - return executable; - } - - try { - const { stdout: executable } = await exec( - "command", - ["-v", PATH_EXECUTABLE_NAME], - { shell: true } - ); - return executable; - } catch (error) { - if (error.code > 0) return ""; - else throw error; - } -} - /** @type {import("./index").FnTypes.getRPCServerPath} */ -export async function getRPCServerPath( - options = { skipSearchInPath: false, disableEnvPath: false } -) { - // @TODO: improve confusing naming of these options - const { skipSearchInPath, disableEnvPath } = options; +export async function getRPCServerPath(options = {}) { + const { takeVersionFromPATH, disableEnvPath } = { + takeVersionFromPATH: false, + disableEnvPath: false, + ...options, + }; // 1. check if it is set as env var if (process.env[ENV_VAR_NAME] && !disableEnvPath) { try { @@ -85,35 +54,9 @@ export async function getRPCServerPath( return process.env[ENV_VAR_NAME]; } - // 2. check if it can be found in PATH - if (!process.env[SKIP_SEARCH_IN_PATH] && !skipSearchInPath) { - const executable = await getLocationInPath(); - - // by just trying to execute it and then use "command -v deltachat-rpc-server" (unix) or "where deltachat-rpc-server" (windows) to get the path to the executable - if (executable.length > 1) { - // test if it is the right version - try { - // for some unknown reason it is in stderr and not in stdout - const { stderr } = await promisify(execFile)( - executable, - ["--version"], - { shell: true } - ); - const version = stderr.slice(0, stderr.indexOf("\n")); - if (package_json.version !== version) { - throw new Error( - `version mismatch: (npm package: ${package_json.version}) (installed ${PATH_EXECUTABLE_NAME} version: ${version})` - ); - } else { - return executable; - } - } catch (error) { - console.error( - "Found executable in PATH, but there was an error: " + error - ); - console.error("So falling back to using prebuild..."); - } - } + // 2. check if PATH should be used + if (takeVersionFromPATH) { + return PATH_EXECUTABLE_NAME; } // 3. check for prebuilds @@ -123,14 +66,7 @@ export async function getRPCServerPath( import { StdioDeltaChat } from "@deltachat/jsonrpc-client"; /** @type {import("./index").FnTypes.startDeltaChat} */ -export async function startDeltaChat( - directory, - options = { - skipSearchInPath: false, - disableEnvPath: false, - muteStdErr: false, - } -) { +export async function startDeltaChat(directory, options = {}) { const pathToServerBinary = await getRPCServerPath(options); const server = spawn(pathToServerBinary, { env: { diff --git a/deltachat-rpc-server/npm-package/src/const.js b/deltachat-rpc-server/npm-package/src/const.js index fafed99a5..fc0c34aeb 100644 --- a/deltachat-rpc-server/npm-package/src/const.js +++ b/deltachat-rpc-server/npm-package/src/const.js @@ -2,5 +2,4 @@ export const PATH_EXECUTABLE_NAME = 'deltachat-rpc-server' -export const ENV_VAR_NAME = "DELTA_CHAT_RPC_SERVER" -export const SKIP_SEARCH_IN_PATH = "DELTA_CHAT_SKIP_PATH" \ No newline at end of file +export const ENV_VAR_NAME = "DELTA_CHAT_RPC_SERVER" \ No newline at end of file