From e4e56b8a88dccc0051d248b145cc4dcb5c9d5f60 Mon Sep 17 00:00:00 2001 From: Davide Date: Mon, 20 Jun 2022 21:26:54 +0200 Subject: [PATCH] feat: new napi implementation --- .github/workflows/nodejs.yml | 24 ++- .gitignore | 10 + Cargo.lock | 369 +++++++++++++++++++++++++++++++++++ cargo.toml | 9 + napi/Cargo.toml | 22 +++ napi/build.rs | 5 + napi/package.json | 12 ++ napi/src/lib.rs | 24 +++ package-lock.json | 27 ++- package.json | 5 +- public/electron.js | 14 +- scripts/moveNapi.js | 5 + 12 files changed, 512 insertions(+), 14 deletions(-) create mode 100644 Cargo.lock create mode 100644 cargo.toml create mode 100644 napi/Cargo.toml create mode 100644 napi/build.rs create mode 100644 napi/package.json create mode 100644 napi/src/lib.rs create mode 100644 scripts/moveNapi.js diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index c82c99ba4..dd23d344f 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -1,4 +1,4 @@ -name: GDLauncher Next CI +name: GDLauncher CI on: push: @@ -26,10 +26,28 @@ jobs: - uses: actions/checkout@v2 with: fetch-depth: 1 - - name: Use Node.js 14.16.0 + - name: Use Node.js 16.13.1 uses: actions/setup-node@v1 with: - node-version: '14.16.0' + node-version: '16.13.1' + + + - name: Install rust (Windows) + if: matrix.os == 'windows-latest' + run: | + rustup target add x86_64-pc-windows-msvc --toolchain nightly + - name: Install rust (MacOS) + if: matrix.os == 'macos-latest' + run: | + rustup target add aarch64-apple-darwin --toolchain nightly + - name: Install rust (Linux) + if: matrix.os == 'ubuntu-latest' + run: | + rustup target add x86_64-unknown-linux-gnu --toolchain nightly + + - name: Switch rust to nightly + run: | + rustup default nightly - name: npm run install run: | diff --git a/.gitignore b/.gitignore index 4a679ef2b..6ab32cd40 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,16 @@ /build /release /deploy +/public/native/*/napi.node +/public/native/*/index.d.ts + +# rust target +/target +/napi/target +/public/napi.node +/napi/napi.node +/napi/node_modules +/napi/index.d.ts # IDEs .idea diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 000000000..0e91c0beb --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,369 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "0.7.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" +dependencies = [ + "memchr", +] + +[[package]] +name = "byteorder" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "convert_case" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb4a24b1aaf0fd0ce8b45161144d6f42cd91677fd5940fd431183eb023b3a2b8" + +[[package]] +name = "ctor" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f877be4f7c9f246b183111634f75baa039715e3f46ce860677d3b19a69fb229c" +dependencies = [ + "quote", + "syn", +] + +[[package]] +name = "futures" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f73fe65f54d1e12b726f517d3e2135ca3125a437b6d998caf1962961f7172d9e" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3083ce4b914124575708913bca19bfe887522d6e2e6d0952943f5eac4a74010" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c09fd04b7e4073ac7156a9539b57a484a8ea920f79c7c675d05d289ab6110d3" + +[[package]] +name = "futures-executor" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9420b90cfa29e327d0429f19be13e7ddb68fa1cccb09d65e5706b8c7a749b8a6" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc4045962a5a5e935ee2fdedaa4e08284547402885ab326734432bed5d12966b" + +[[package]] +name = "futures-macro" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33c1e13800337f4d4d7a316bf45a567dbcb6ffe087f16424852d97e97a91f512" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21163e139fa306126e6eedaf49ecdb4588f939600f0b1e770f4205ee4b7fa868" + +[[package]] +name = "futures-task" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c66a976bf5909d801bbef33416c41372779507e7a6b3a5e25e4749c58f776a" + +[[package]] +name = "futures-util" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8b7abd5d659d9b90c8cba917f6ec750a74e2dc23902ef9cd4cc8c8b22e6036a" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "slab", +] + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.126" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" + +[[package]] +name = "libloading" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "efbc0f03f9a775e9f6aed295c6a1ba2253c5757a9e03d55c6caa46a681abcddd" +dependencies = [ + "cfg-if", + "winapi", +] + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "murmurhash32" +version = "0.2.0" +source = "git+https://github.com/gorilla-devs/murmurhash32.git#401640895f352c88ccc61670993eec49c10e730b" +dependencies = [ + "byteorder", +] + +[[package]] +name = "napi" +version = "0.1.0" +dependencies = [ + "futures", + "murmurhash32", + "napi 2.5.0", + "napi-build", + "napi-derive", + "tokio", +] + +[[package]] +name = "napi" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec369bd2ae196a21bb08fb53761e637862c3c0699da9cd21b6d954e32dda04b3" +dependencies = [ + "ctor", + "lazy_static", + "napi-sys", + "thread_local", + "tokio", +] + +[[package]] +name = "napi-build" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebd4419172727423cf30351406c54f6cc1b354a2cfb4f1dba3e6cd07f6d5522b" + +[[package]] +name = "napi-derive" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c0308f2313ee504b032bad9c75343d967e6ba27be2e3f19e89bb0ee8786b5b0" +dependencies = [ + "convert_case", + "napi-derive-backend", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "napi-derive-backend" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c97606e865ebfca1c5d0c952e34969ca26b3c8ba0c246c569a2fc945719a78ea" +dependencies = [ + "convert_case", + "once_cell", + "proc-macro2", + "quote", + "regex", + "syn", +] + +[[package]] +name = "napi-sys" +version = "2.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "529671ebfae679f2ce9630b62dd53c72c56b3eb8b2c852e7e2fa91704ff93d67" +dependencies = [ + "libloading", +] + +[[package]] +name = "num_cpus" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1" +dependencies = [ + "hermit-abi", + "libc", +] + +[[package]] +name = "once_cell" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7709cef83f0c1f58f666e746a08b21e0085f7440fa6a29cc194d68aac97a4225" + +[[package]] +name = "pin-project-lite" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "proc-macro2" +version = "1.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c54b25569025b7fc9651de43004ae593a75ad88543b17178aa5e1b9c4f15f56f" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f53dc8cf16a769a6f677e09e7ff2cd4be1ea0f48754aac39520536962011de0d" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex" +version = "1.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d83f127d94bdbcda4c8cc2e50f6f84f4b611f69c902699ca385a39c3a75f9ff1" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49b3de9ec5dc0a3417da371aab17d729997c15010e7fd24ff707773a33bddb64" + +[[package]] +name = "slab" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb703cfe953bccee95685111adeedb76fabe4e97549a58d16f03ea7b9367bb32" + +[[package]] +name = "syn" +version = "1.0.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c50aef8a904de4c23c788f104b7dddc7d6f79c647c7c8ce4cc8f73eb0ca773dd" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thread_local" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5516c27b78311c50bf42c071425c560ac799b11c30b31f87e3081965fe5e0180" +dependencies = [ + "once_cell", +] + +[[package]] +name = "tokio" +version = "1.19.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c51a52ed6686dd62c320f9b89299e9dfb46f730c7a48e635c19f21d116cb1439" +dependencies = [ + "num_cpus", + "once_cell", + "pin-project-lite", +] + +[[package]] +name = "unicode-ident" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bd2fe26506023ed7b5e1e315add59d6f584c621d037f9368fea9cfb988f368c" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/cargo.toml b/cargo.toml new file mode 100644 index 000000000..441561b6a --- /dev/null +++ b/cargo.toml @@ -0,0 +1,9 @@ +[workspace] +members = [ + "napi", +] + +[profile.release] +opt-level = 3 +strip = "symbols" +lto = true \ No newline at end of file diff --git a/napi/Cargo.toml b/napi/Cargo.toml new file mode 100644 index 000000000..5b696235c --- /dev/null +++ b/napi/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "napi" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +napi = { version = "2", features = [ + "tokio_fs", + "napi8", + "tokio_rt", + "async", +]} +futures = "0.3.21" +napi-derive = "2" +tokio = "1.19.2" +murmurhash32 = {git = "https://github.com/gorilla-devs/murmurhash32.git"} + +[build-dependencies] +napi-build = "1" \ No newline at end of file diff --git a/napi/build.rs b/napi/build.rs new file mode 100644 index 000000000..900ef8ba2 --- /dev/null +++ b/napi/build.rs @@ -0,0 +1,5 @@ +fn main() { + use napi_build::setup; + + setup(); +} diff --git a/napi/package.json b/napi/package.json new file mode 100644 index 000000000..ddeef6e33 --- /dev/null +++ b/napi/package.json @@ -0,0 +1,12 @@ +{ + "name": "napi", + "version": "0.0.1", + "main": "./index.node", + "types": "./index.d.ts", + "scripts": { + "build": "node ../node_modules/cross-env/src/bin/cross-env-shell.js RUSTFLAGS=-Zsymbol-mangling-version=v0 node ../node_modules/@napi-rs/cli/scripts/index.js build --js false --release" + }, + "napi": { + "name": "napi" + } +} diff --git a/napi/src/lib.rs b/napi/src/lib.rs new file mode 100644 index 000000000..f43802866 --- /dev/null +++ b/napi/src/lib.rs @@ -0,0 +1,24 @@ +use murmurhash32::murmurhash2; +use napi::bindgen_prelude::*; +use napi_derive::napi; +use std::io::Read; + +#[allow(dead_code)] +#[napi] +fn fibonacci(n: u32) -> u32 { + match n { + 1 | 2 => 1, + _ => fibonacci(n - 1) + fibonacci(n - 2), + } +} + +#[allow(dead_code)] +#[napi] +async fn compute_path_murmur(path: String) -> Result { + let mut file = std::fs::File::open(path)?; + let mut buffer = Vec::new(); + file.read_to_end(&mut buffer)?; + buffer.retain(|&x| (x != 9 && x != 10 && x != 13 && x != 32)); + let hash = murmurhash2(&buffer); + Ok(hash) +} diff --git a/package-lock.json b/package-lock.json index c11c8bda7..f2bd3b2fb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "gdlauncher", - "version": "1.1.24", + "version": "1.1.25", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "gdlauncher", - "version": "1.1.24", + "version": "1.1.25", "cpu": [ "x64", "arm64" @@ -100,6 +100,7 @@ "@fortawesome/free-regular-svg-icons": "^5.15.4", "@fortawesome/free-solid-svg-icons": "^5.15.4", "@fortawesome/react-fontawesome": "^0.1.15", + "@napi-rs/cli": "^2.10.0", "@sentry/webpack-plugin": "^1.18.3", "babel-core": "^6.26.3", "babel-loader": "^8.2.2", @@ -4023,6 +4024,22 @@ "integrity": "sha512-n2RC9d6XatVbWFdHLimzzUJxJ1KY8LdjqrW6YvGPiRmsHkhOUx74/Ct10x5Yo7bC/Jvqx7cDEW8IMPv/+vwEzA==", "dev": true }, + "node_modules/@napi-rs/cli": { + "version": "2.10.0", + "resolved": "https://registry.npmjs.org/@napi-rs/cli/-/cli-2.10.0.tgz", + "integrity": "sha512-pK/VLOdo690FEItPJ9T5PLEfzmDTHgXjzuVChnsKvqI9vPg+rH12iyoxOrux6PAqg/pImJTd4CCabRqEfLt/sA==", + "dev": true, + "bin": { + "napi": "scripts/index.js" + }, + "engines": { + "node": ">= 10" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/Brooooooklyn" + } + }, "node_modules/@nicolo-ribaudo/chokidar-2": { "version": "2.1.8-no-fsevents.2", "resolved": "https://registry.npmjs.org/@nicolo-ribaudo/chokidar-2/-/chokidar-2-2.1.8-no-fsevents.2.tgz", @@ -34144,6 +34161,12 @@ "integrity": "sha512-n2RC9d6XatVbWFdHLimzzUJxJ1KY8LdjqrW6YvGPiRmsHkhOUx74/Ct10x5Yo7bC/Jvqx7cDEW8IMPv/+vwEzA==", "dev": true }, + "@napi-rs/cli": { + "version": "2.10.0", + "resolved": "https://registry.npmjs.org/@napi-rs/cli/-/cli-2.10.0.tgz", + "integrity": "sha512-pK/VLOdo690FEItPJ9T5PLEfzmDTHgXjzuVChnsKvqI9vPg+rH12iyoxOrux6PAqg/pImJTd4CCabRqEfLt/sA==", + "dev": true + }, "@nicolo-ribaudo/chokidar-2": { "version": "2.1.8-no-fsevents.2", "resolved": "https://registry.npmjs.org/@nicolo-ribaudo/chokidar-2/-/chokidar-2-2.1.8-no-fsevents.2.tgz", diff --git a/package.json b/package.json index 8717e25f0..864ad6c10 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "homepage": "./", "main": "build/electron.js", "scripts": { + "install": "npm run napi-build", "start": "cross-env-shell NODE_ENV=development craco start", "start-electron": "concurrently \"npm run start\" \"wait-on http://localhost:3000 && electron .\"", "start-prod": "electron .", @@ -48,7 +49,8 @@ "release:portable": "cross-env-shell NODE_ENV=production APP_TYPE=electron REACT_APP_RELEASE_TYPE=portable \"npm run build\" && npm run build-electron:portable && npm run deploy portable", "upload": "node ./scripts/uploadRelease.js", "release": "rimraf ./deploy && npm run release:setup && npm run release:portable", - "prepare": "husky install" + "prepare": "husky install", + "napi-build": "cd napi && npm run build && node ../scripts/moveNapi.js" }, "lint-staged": { "*.{js,jsx}": [ @@ -106,6 +108,7 @@ "@fortawesome/free-regular-svg-icons": "^5.15.4", "@fortawesome/free-solid-svg-icons": "^5.15.4", "@fortawesome/react-fontawesome": "^0.1.15", + "@napi-rs/cli": "^2.10.0", "@sentry/webpack-plugin": "^1.18.3", "babel-core": "^6.26.3", "babel-loader": "^8.2.2", diff --git a/public/electron.js b/public/electron.js index f2224822e..c838ce8c3 100644 --- a/public/electron.js +++ b/public/electron.js @@ -23,8 +23,10 @@ const { } = require('base64url'); const { URL } = require('url'); const UserAgent = require('user-agents'); -const murmur = require('./native/murmur2'); const nsfw = require('./native/nsfw'); +const napi = require('./napi.node'); + +// console.log(napi.fibonacci(10)); const fs = fss.promises; @@ -866,13 +868,9 @@ ipcMain.handle('stop-listener', async () => { } }); -ipcMain.handle('calculateMurmur2FromPath', (e, filePath) => { - return new Promise((resolve, reject) => { - return murmur(filePath).then(v => { - if (v.toString().length === 0) reject(); - return resolve(v); - }); - }); +ipcMain.handle('calculateMurmur2FromPath', async (e, filePath) => { + const res = await napi.computePathMurmur(filePath); + return res.toString(); }); // AutoUpdater diff --git a/scripts/moveNapi.js b/scripts/moveNapi.js new file mode 100644 index 000000000..9dfcc4263 --- /dev/null +++ b/scripts/moveNapi.js @@ -0,0 +1,5 @@ +const fs = require('fs'); +const os = require('os'); + +fs.renameSync('./napi.node', `../public/native/${os.platform()}/napi.node`); +fs.renameSync('./index.d.ts', `../public/native/${os.platform()}/index.d.ts`);