Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stdio jsonrpc server npm package #5332

Merged
merged 48 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
099c3af
start building the package
Simon-Laux Mar 10, 2024
102814e
update readme
Simon-Laux Mar 10, 2024
272cac4
rename npm packages and start with js code
Simon-Laux Mar 17, 2024
282cbe6
fix filename typo
Simon-Laux Mar 17, 2024
b5fbdda
fix typo
Simon-Laux Mar 17, 2024
0cc2bc1
git: ignore vscode folder
Simon-Laux May 6, 2024
f6aa4de
more progress
Simon-Laux May 6, 2024
8582c63
add script for local instalation
Simon-Laux May 6, 2024
50ad41b
add types and fix local installation
Simon-Laux May 6, 2024
288a1f0
fix ts
Simon-Laux May 6, 2024
972ab65
fix resolving npm prebuild path
Simon-Laux May 6, 2024
fe0e706
fix options
Simon-Laux May 6, 2024
09f8d7a
add todo comment
Simon-Laux May 6, 2024
a42cf32
add missing export in jsonrpc typescript package.json
Simon-Laux May 6, 2024
406d33b
add highlevel interface "startDeltaChat"
Simon-Laux May 7, 2024
11c2e76
add usage example to readme
Simon-Laux May 7, 2024
335f6e1
updates to imports
Simon-Laux May 7, 2024
bb40900
move code in attempt to make it cross compatible with deno
Simon-Laux May 7, 2024
ddd17b7
expose pathToServerBinary in DeltaChatOverJsonRpcServer
Simon-Laux May 7, 2024
816005a
add temporary logging
Simon-Laux May 7, 2024
078e5f3
rm temp log line again
Simon-Laux May 7, 2024
f8478a5
ignore ts error
Simon-Laux May 7, 2024
2552ae7
hint about min nodejs version
Simon-Laux May 8, 2024
3232848
Split up python module and add a script to pack already built binary.
Simon-Laux May 8, 2024
31bf2a1
add ci to make npm packages
Simon-Laux May 9, 2024
6d82bc4
change trigger condition of release ci to test it
Simon-Laux May 9, 2024
bcae52d
try fix yaml syntax
Simon-Laux May 9, 2024
18f3143
fix ci yaml
Simon-Laux May 9, 2024
f4954b3
try to fix ci: change python comand to use new version
Simon-Laux May 9, 2024
22cc110
ci: fix python version
Simon-Laux May 10, 2024
40b33eb
try fix ci
Simon-Laux May 10, 2024
66ebd25
fix make_package script
Simon-Laux May 10, 2024
a4752f3
revert run conditions of workflow
Simon-Laux May 10, 2024
f98c0af
fix tripplet parsing
Simon-Laux May 10, 2024
5eff575
fix platform conversion
Simon-Laux May 10, 2024
335ba15
make deltachat-rpc-server executable
Simon-Laux May 10, 2024
bea421b
Remove unused `json` import from build_platform_package.py
link2xt May 11, 2024
59ad9fb
Make build_platform_package.py executable
link2xt May 11, 2024
fbe82b0
Format build_platform_package.py
link2xt May 11, 2024
377e891
Do not depend on rustup in build_platform_package.py
link2xt May 11, 2024
7b6f147
Use `rustc --print target-list` instead of `rustup` in pack_binary_fo…
link2xt May 11, 2024
f31a525
Readme typo fixing
link2xt May 11, 2024
c551522
Check subprocess exit code
link2xt May 11, 2024
fda5728
Add newline at the end of python file
link2xt May 11, 2024
5ef46ae
Rename Readme.md into README.md for consistency
link2xt May 11, 2024
f811554
do not prioritize cargo bin
Simon-Laux May 11, 2024
701b48a
rm comment and fmt
Simon-Laux May 11, 2024
c203c6f
use `where` and `command -v` to find deltachat-rpc-server from path
Simon-Laux May 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
125 changes: 125 additions & 0 deletions .github/workflows/deltachat-rpc-server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,128 @@ jobs:
- name: Publish deltachat-rpc-client to PyPI
if: github.event_name == 'release'
uses: pypa/gh-action-pypi-publish@release/v1

publish_npm_package:
name: Build & Publish npm prebuilds and deltachat-rpc-server
needs: ["build_linux", "build_windows", "build_macos"]
runs-on: "ubuntu-latest"
steps:
- uses: actions/checkout@v4
with:
show-progress: false
- uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: Download Linux aarch64 binary
uses: actions/download-artifact@v4
with:
name: deltachat-rpc-server-aarch64-linux
path: deltachat-rpc-server-aarch64-linux.d

- name: Download Linux armv7l binary
uses: actions/download-artifact@v4
with:
name: deltachat-rpc-server-armv7l-linux
path: deltachat-rpc-server-armv7l-linux.d

- name: Download Linux armv6l binary
uses: actions/download-artifact@v4
with:
name: deltachat-rpc-server-armv6l-linux
path: deltachat-rpc-server-armv6l-linux.d

- name: Download Linux i686 binary
uses: actions/download-artifact@v4
with:
name: deltachat-rpc-server-i686-linux
path: deltachat-rpc-server-i686-linux.d

- name: Download Linux x86_64 binary
uses: actions/download-artifact@v4
with:
name: deltachat-rpc-server-x86_64-linux
path: deltachat-rpc-server-x86_64-linux.d

- name: Download Win32 binary
uses: actions/download-artifact@v4
with:
name: deltachat-rpc-server-win32
path: deltachat-rpc-server-win32.d

- name: Download Win64 binary
uses: actions/download-artifact@v4
with:
name: deltachat-rpc-server-win64
path: deltachat-rpc-server-win64.d

- name: Download macOS binary for x86_64
uses: actions/download-artifact@v4
with:
name: deltachat-rpc-server-x86_64-macos
path: deltachat-rpc-server-x86_64-macos.d

- name: Download macOS binary for aarch64
uses: actions/download-artifact@v4
with:
name: deltachat-rpc-server-aarch64-macos
path: deltachat-rpc-server-aarch64-macos.d

- name: Download Android binary for arm64-v8a
uses: actions/download-artifact@v4
with:
name: deltachat-rpc-server-arm64-v8a-android
path: deltachat-rpc-server-arm64-v8a-android.d

- name: Download Android binary for armeabi-v7a
uses: actions/download-artifact@v4
with:
name: deltachat-rpc-server-armeabi-v7a-android
path: deltachat-rpc-server-armeabi-v7a-android.d

- name: make npm packets for prebuilds and `@deltachat/stdio-rpc-server`
run: |
cd deltachat-rpc-server/npm-package

python --version

python scripts/pack_binary_for_platform.py aarch64-unknown-linux-musl ../../deltachat-rpc-server-aarch64-linux.d/deltachat-rpc-server
python scripts/pack_binary_for_platform.py armv7-unknown-linux-musleabihf ../../deltachat-rpc-server-armv7l-linux.d/deltachat-rpc-server
python scripts/pack_binary_for_platform.py arm-unknown-linux-musleabihf ../../deltachat-rpc-server-armv6l-linux.d/deltachat-rpc-server
python scripts/pack_binary_for_platform.py i686-unknown-linux-musl ../../deltachat-rpc-server-i686-linux.d/deltachat-rpc-server
python scripts/pack_binary_for_platform.py x86_64-unknown-linux-musl ../../deltachat-rpc-server-x86_64-linux.d/deltachat-rpc-server
python scripts/pack_binary_for_platform.py i686-pc-windows-gnu ../../deltachat-rpc-server-win32.d/deltachat-rpc-server.exe
python scripts/pack_binary_for_platform.py x86_64-pc-windows-gnu ../../deltachat-rpc-server-win64.d/deltachat-rpc-server.exe
python scripts/pack_binary_for_platform.py x86_64-apple-darwin ../../deltachat-rpc-server-x86_64-macos.d/deltachat-rpc-server
python scripts/pack_binary_for_platform.py aarch64-apple-darwin ../../deltachat-rpc-server-aarch64-macos.d/deltachat-rpc-server
python scripts/pack_binary_for_platform.py aarch64-linux-android ../../deltachat-rpc-server-arm64-v8a-android.d/deltachat-rpc-server
python scripts/pack_binary_for_platform.py armv7-linux-androideabi ../../deltachat-rpc-server-armeabi-v7a-android.d/deltachat-rpc-server

ls -lah platform_package

for platform in ./platform_package/*; do npm pack "$platform"; done
npm pack
ls -lah

- name: Upload to artifacts
uses: actions/upload-artifact@v4
with:
name: deltachat-rpc-server-npm-package
path: deltachat-rpc-server/npm-package/*.tgz
if-no-files-found: error

- name: Upload npm packets to the GitHub release
if: github.event_name == 'release'
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
run: |
gh release upload ${{ github.ref_name }} \
--repo ${{ github.repository }} \
*.tgz

- name: Publish npm packets for prebuilds and `@deltachat/stdio-rpc-server`
if: github.event_name == 'release'
run: |
ls -lah platform_package

for platform in *.tgz; do npm publish "$platform"; done
Simon-Laux marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ deltachat-ffi/xml

coverage/
.DS_Store
.vscode
.vscode/launch.json
python/accounts.txt
python/all-testaccounts.txt
Expand Down
3 changes: 2 additions & 1 deletion deltachat-jsonrpc/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"exports": {
".": {
"import": "./dist/deltachat.js",
"require": "./dist/deltachat.cjs"
"require": "./dist/deltachat.cjs",
"types": "./dist/deltachat.d.ts"
}
},
"license": "MPL-2.0",
Expand Down
2 changes: 2 additions & 0 deletions deltachat-rpc-server/npm-package/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
platform_package
*.tgz
3 changes: 3 additions & 0 deletions deltachat-rpc-server/npm-package/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
platform_package/*
scripts/
*.tgz
77 changes: 77 additions & 0 deletions deltachat-rpc-server/npm-package/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
## npm package for deltachat-rpc-server

This is the successor of `deltachat-node`,
it does not use NAPI bindings but instead uses stdio executables
to let you talk to core over jsonrpc over stdio.
This simplifies cross-compilation and even reduces binary size (no CFFI layer and no NAPI layer).

## Usage

> The **minimum** nodejs version for this package is `20.11`

```
npm i @deltachat/stdio-rpc-server @deltachat/jsonrpc-client
```

```js
import { startDeltaChat } from "@deltachat/stdio-rpc-server";
import { C } from "@deltachat/jsonrpc-client";

async function main() {
const dc = await startDeltaChat("deltachat-data");
console.log(await dc.rpc.getSystemInfo());
}
```

For a more complete example refer to https://github.com/deltachat-bot/echo/pull/69/files (TODO change link when pr is merged).

## How to use on an unsupported platform

<!-- todo instructions, will uses an env var for pointing to `deltachat-rpc-server` binary -->

<!-- todo copy parts from https://github.com/deltachat/deltachat-desktop/blob/7045c6f549e4b9d5caa0709d5bd314bbd9fd53db/docs/UPDATE_CORE.md -->

## How does it work when you install it

NPM automatically installs platform dependent optional dependencies when `os` and `cpu` fields are set correctly.

references:

- https://napi.rs/docs/deep-dive/release#3-the-native-addon-for-different-platforms-is-distributed-through-different-npm-packages, [webarchive version](https://web.archive.org/web/20240309234250/https://napi.rs/docs/deep-dive/release#3-the-native-addon-for-different-platforms-is-distributed-through-different-npm-packages)
- https://docs.npmjs.com/cli/v6/configuring-npm/package-json#cpu
- https://docs.npmjs.com/cli/v6/configuring-npm/package-json#os

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
3. prebuilds in npm packages

## How do you built this package in CI

- To build platform packages, run the `build_platform_package.py` script:
```
python3 build_platform_package.py <cargo-target>
# example
python3 build_platform_package.py x86_64-apple-darwin
```
- Then pass it as an artifact to the last CI action that publishes the main package.
- upload all packages from `deltachat-rpc-server/npm-package/platform_package`.
- then publish `deltachat-rpc-server/npm-package`,
- this will run `update_optional_dependencies_and_version.js` (in the `prepack` script),
which puts all platform packages into `optionalDependencies` and updates the `version` in `package.json`

## How to build a version you can use localy on your host machine for development

You can not install the npm packet from the previous section locally, unless you have a local npm registry set up where you upload it too. This is why we have seperate scripts for making it work for local installation.

- If you just need your host platform run `python scripts/make_local_dev_version.py`
- note: this clears the `platform_package` folder
- (advanced) If you need more than one platform for local install you can just run `node scripts/update_optional_dependencies_and_version.js` after building multiple plaftorms with `build_platform_package.py`

## Thanks to nlnet

The initial work on this package was funded by nlnet as part of the [Delta Tauri](https://nlnet.nl/project/DeltaTauri/) Project.
39 changes: 39 additions & 0 deletions deltachat-rpc-server/npm-package/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { StdioDeltaChat } from "@deltachat/jsonrpc-client";

export interface SearchOptions {
/** whether to disable looking for deltachat-rpc-server inside of $PATH */
skipSearchInPath: boolean;

/** whether to disable the DELTA_CHAT_RPC_SERVER environment variable */
disableEnvPath: boolean;
}

/**
*
* @returns absolute path to deltachat-rpc-server binary
* @throws when it is not found
*/
export function getRPCServerPath(
options?: Partial<SearchOptions>
): Promise<string>;



export type DeltaChatOverJsonRpcServer = StdioDeltaChat & {
shutdown: () => Promise<void>;
readonly pathToServerBinary: string;
};


/**
*
* @param directory directory for accounts folder
* @param options
*/
export function startDeltaChat(directory: string, options?: Partial<SearchOptions> ): Promise<DeltaChatOverJsonRpcServer>


export namespace FnTypes {
export type getRPCServerPath = typeof getRPCServerPath
export type startDeltaChat = typeof startDeltaChat
}