From a27edd668a31eb6c46588ef57b0966becdb0529c Mon Sep 17 00:00:00 2001 From: naskya Date: Sat, 3 Feb 2024 22:15:40 +0900 Subject: [PATCH] WIP (wasm-bindgen) --- .gitignore | 2 +- Cargo.lock | 67 +- Cargo.toml | 4 +- Makefile | 13 +- neko/index.js | 153 +++-- neko/update/native.sh | 1 + package.json | 5 +- packages/README.md | 1 + packages/backend-rs/Cargo.toml | 2 + packages/backend-rs/package.json | 23 +- packages/backend-rs/src/lib.rs | 2 + packages/backend-rs/src/util/mod.rs | 1 - packages/backend/.gitignore | 1 + .../queue/processors/db/import-blocking.ts | 2 +- .../queue/processors/db/import-following.ts | 2 +- .../src/queue/processors/db/import-muting.ts | 2 +- .../queue/processors/db/import-user-lists.ts | 2 +- packages/backend/src/server/index.ts | 3 +- packages/backend/src/server/web/index.ts | 3 +- packages/backend/src/server/well-known.ts | 3 +- packages/client/.gitignore | 1 + packages/client/package.json | 3 + packages/client/src/account.ts | 12 +- .../src/components/MkAbuseReportWindow.vue | 4 +- .../client/src/components/MkAutocomplete.vue | 4 +- .../client/src/components/MkChatPreview.vue | 13 +- .../client/src/components/MkCropperDialog.vue | 8 +- packages/client/src/components/MkCwButton.vue | 4 +- packages/client/src/components/MkDialog.vue | 4 +- .../client/src/components/MkDrive.file.vue | 6 +- .../client/src/components/MkDrive.folder.vue | 14 +- .../src/components/MkDrive.navFolder.vue | 19 +- packages/client/src/components/MkDrive.vue | 77 +-- .../src/components/MkDriveFileThumbnail.vue | 4 +- .../src/components/MkDriveSelectDialog.vue | 8 +- .../client/src/components/MkDriveWindow.vue | 5 +- .../client/src/components/MkEmojiPicker.vue | 8 +- .../src/components/MkFeaturedPhotos.vue | 4 +- .../src/components/MkFileListForAdmin.vue | 6 +- .../client/src/components/MkFollowButton.vue | 6 +- .../client/src/components/MkImageViewer.vue | 6 +- .../src/components/MkInstanceCardMini.vue | 9 +- .../src/components/MkInstanceSelectDialog.vue | 2 +- packages/client/src/components/MkMedia.vue | 4 +- .../client/src/components/MkMediaBanner.vue | 6 +- .../client/src/components/MkMediaList.vue | 9 +- .../client/src/components/MkModPlayer.vue | 4 +- packages/client/src/components/MkNote.vue | 8 +- .../client/src/components/MkNoteDetailed.vue | 13 +- .../client/src/components/MkNoteHeader.vue | 4 +- .../client/src/components/MkNoteSimple.vue | 4 +- packages/client/src/components/MkNoteSub.vue | 12 +- .../client/src/components/MkNotification.vue | 4 +- .../client/src/components/MkPagination.vue | 10 +- packages/client/src/components/MkPoll.vue | 4 +- packages/client/src/components/MkPostForm.vue | 30 +- .../src/components/MkPostFormDialog.vue | 22 +- .../client/src/components/MkQuoteButton.vue | 2 +- .../client/src/components/MkReactedUsers.vue | 6 +- .../components/MkReactionsViewer.reaction.vue | 4 +- .../src/components/MkReactionsViewer.vue | 4 +- .../client/src/components/MkRenoteButton.vue | 4 +- .../client/src/components/MkStarButton.vue | 2 +- .../src/components/MkStarButtonNoEmoji.vue | 2 +- .../src/components/MkSubNoteContent.vue | 4 +- .../client/src/components/MkUserCardMini.vue | 13 +- packages/client/src/components/MkUserInfo.vue | 4 +- .../src/components/MkUserOnlineIndicator.vue | 4 +- .../client/src/components/MkUserPreview.vue | 8 +- .../src/components/MkUserSelectDialog.vue | 10 +- .../components/MkUserSelectLocalDialog.vue | 10 +- .../src/components/MkVisibilityPicker.vue | 11 +- .../client/src/components/global/MkAcct.vue | 4 +- .../client/src/components/global/MkAvatar.vue | 13 +- .../client/src/components/global/MkEmoji.vue | 2 +- .../src/components/global/MkUserName.vue | 4 +- packages/client/src/filters/user.ts | 15 +- packages/client/src/init.ts | 2 + packages/client/src/instance.ts | 4 +- packages/client/src/os.ts | 10 +- packages/client/src/pages/_error_.vue | 4 +- .../client/src/pages/admin/overview.user.vue | 15 +- packages/client/src/pages/clip.vue | 4 +- packages/client/src/pages/follow-requests.vue | 5 +- packages/client/src/pages/follow.vue | 4 +- packages/client/src/pages/instance-info.vue | 6 +- packages/client/src/pages/messaging/index.vue | 4 +- .../pages/messaging/messaging-room.form.vue | 8 +- .../messaging/messaging-room.message.vue | 4 +- .../src/pages/messaging/messaging-room.vue | 16 +- .../client/src/pages/my-antennas/editor.vue | 4 +- packages/client/src/pages/note.vue | 8 +- packages/client/src/pages/share.vue | 15 +- packages/client/src/pages/user-info.vue | 11 +- packages/client/src/pages/user/clips.vue | 4 +- .../client/src/pages/user/follow-list.vue | 4 +- packages/client/src/pages/user/followers.vue | 7 +- packages/client/src/pages/user/following.vue | 7 +- packages/client/src/pages/user/gallery.vue | 4 +- packages/client/src/pages/user/home.vue | 4 +- .../client/src/pages/user/index.photos.vue | 10 +- .../client/src/pages/user/index.timeline.vue | 4 +- packages/client/src/pages/user/index.vue | 10 +- packages/client/src/pages/user/pages.vue | 4 +- packages/client/src/pages/user/reactions.vue | 4 +- .../client/src/scripts/check-word-mute.ts | 8 +- .../client/src/scripts/gen-search-query.ts | 4 +- .../client/src/scripts/get-note-summary.ts | 4 +- packages/client/src/scripts/get-user-menu.ts | 4 +- packages/client/src/scripts/page-metadata.ts | 6 +- packages/client/src/scripts/select-file.ts | 2 +- packages/client/src/scripts/upload.ts | 4 +- .../client/src/scripts/use-note-capture.ts | 4 +- packages/client/src/stream.ts | 6 +- packages/client/src/types/menu.ts | 8 +- .../src/ui/_common_/statusbar-federation.vue | 4 +- .../src/ui/_common_/statusbar-user-list.vue | 4 +- packages/client/src/ui/universal.vue | 4 +- packages/client/vite.config.ts | 14 +- packages/firefish-js/.gitignore | 2 + packages/firefish-js/src/acct.ts | 14 - packages/firefish-js/src/index.ts | 11 +- packages/sdk/.cargo/config.toml | 3 + packages/sdk/.editorconfig | 3 + packages/sdk/.npmignore | 13 + packages/sdk/Cargo.toml | 10 + packages/sdk/npm/android-arm-eabi/README.md | 3 + .../sdk/npm/android-arm-eabi/package.json | 18 + packages/sdk/npm/android-arm64/README.md | 3 + packages/sdk/npm/android-arm64/package.json | 18 + packages/sdk/npm/darwin-arm64/README.md | 3 + packages/sdk/npm/darwin-arm64/package.json | 18 + packages/sdk/npm/darwin-universal/README.md | 3 + .../sdk/npm/darwin-universal/package.json | 15 + packages/sdk/npm/darwin-x64/README.md | 3 + packages/sdk/npm/darwin-x64/package.json | 18 + packages/sdk/npm/freebsd-x64/README.md | 3 + packages/sdk/npm/freebsd-x64/package.json | 18 + .../sdk/npm/linux-arm-gnueabihf/README.md | 3 + .../sdk/npm/linux-arm-gnueabihf/package.json | 18 + packages/sdk/npm/linux-arm64-gnu/README.md | 3 + packages/sdk/npm/linux-arm64-gnu/package.json | 21 + packages/sdk/npm/linux-arm64-musl/README.md | 3 + .../sdk/npm/linux-arm64-musl/package.json | 21 + packages/sdk/npm/linux-x64-gnu/README.md | 3 + packages/sdk/npm/linux-x64-gnu/package.json | 21 + packages/sdk/npm/linux-x64-musl/README.md | 3 + packages/sdk/npm/linux-x64-musl/package.json | 21 + packages/sdk/npm/win32-arm64-msvc/README.md | 3 + .../sdk/npm/win32-arm64-msvc/package.json | 18 + packages/sdk/npm/win32-ia32-msvc/README.md | 3 + packages/sdk/npm/win32-ia32-msvc/package.json | 18 + packages/sdk/npm/win32-x64-msvc/README.md | 3 + packages/sdk/npm/win32-x64-msvc/package.json | 18 + packages/sdk/package.json | 16 + .../{backend-rs/src/util => sdk/src}/acct.rs | 8 +- packages/sdk/src/lib.rs | 1 + pnpm-lock.yaml | 648 +++++++++++++++++- pnpm-workspace.yaml | 1 + scripts/clean-built.mjs | 4 + scripts/clean-npm.mjs | 4 + 161 files changed, 1550 insertions(+), 532 deletions(-) create mode 100644 packages/backend/.gitignore create mode 100644 packages/client/.gitignore delete mode 100644 packages/firefish-js/src/acct.ts create mode 100644 packages/sdk/.cargo/config.toml create mode 100644 packages/sdk/.editorconfig create mode 100644 packages/sdk/.npmignore create mode 100644 packages/sdk/Cargo.toml create mode 100644 packages/sdk/npm/android-arm-eabi/README.md create mode 100644 packages/sdk/npm/android-arm-eabi/package.json create mode 100644 packages/sdk/npm/android-arm64/README.md create mode 100644 packages/sdk/npm/android-arm64/package.json create mode 100644 packages/sdk/npm/darwin-arm64/README.md create mode 100644 packages/sdk/npm/darwin-arm64/package.json create mode 100644 packages/sdk/npm/darwin-universal/README.md create mode 100644 packages/sdk/npm/darwin-universal/package.json create mode 100644 packages/sdk/npm/darwin-x64/README.md create mode 100644 packages/sdk/npm/darwin-x64/package.json create mode 100644 packages/sdk/npm/freebsd-x64/README.md create mode 100644 packages/sdk/npm/freebsd-x64/package.json create mode 100644 packages/sdk/npm/linux-arm-gnueabihf/README.md create mode 100644 packages/sdk/npm/linux-arm-gnueabihf/package.json create mode 100644 packages/sdk/npm/linux-arm64-gnu/README.md create mode 100644 packages/sdk/npm/linux-arm64-gnu/package.json create mode 100644 packages/sdk/npm/linux-arm64-musl/README.md create mode 100644 packages/sdk/npm/linux-arm64-musl/package.json create mode 100644 packages/sdk/npm/linux-x64-gnu/README.md create mode 100644 packages/sdk/npm/linux-x64-gnu/package.json create mode 100644 packages/sdk/npm/linux-x64-musl/README.md create mode 100644 packages/sdk/npm/linux-x64-musl/package.json create mode 100644 packages/sdk/npm/win32-arm64-msvc/README.md create mode 100644 packages/sdk/npm/win32-arm64-msvc/package.json create mode 100644 packages/sdk/npm/win32-ia32-msvc/README.md create mode 100644 packages/sdk/npm/win32-ia32-msvc/package.json create mode 100644 packages/sdk/npm/win32-x64-msvc/README.md create mode 100644 packages/sdk/npm/win32-x64-msvc/package.json create mode 100644 packages/sdk/package.json rename packages/{backend-rs/src/util => sdk/src}/acct.rs (79%) create mode 100644 packages/sdk/src/lib.rs diff --git a/.gitignore b/.gitignore index 222302122..b6d1a364b 100644 --- a/.gitignore +++ b/.gitignore @@ -34,7 +34,7 @@ coverage .eslintcache # misskey -built +/built db elasticsearch redis diff --git a/Cargo.lock b/Cargo.lock index a67a04e00..35c84a2f2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -230,6 +230,7 @@ dependencies = [ "rand", "regex", "schemars", + "sdk", "sea-orm", "serde", "serde_json", @@ -1118,9 +1119,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.2.1" +version = "2.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "433de089bd45971eecf4668ee0ee8f4cec17db4f8bd8f7bc3197a6ce37aa7d9b" +checksum = "824b2ae422412366ba479e8111fd301f7b5faece8149317bb81925979a53f520" dependencies = [ "equivalent", "hashbrown 0.14.3", @@ -1235,9 +1236,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.152" +version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13e3bf6590cbc649f4d1a3eefc9d5d6eb746f5200ffb04e5e142700b8faa56e7" +checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" [[package]] name = "libloading" @@ -1338,9 +1339,9 @@ dependencies = [ [[package]] name = "napi" -version = "2.15.0" +version = "2.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "efbf98e1bcb85cc441bbf7cdfb11070d2537a100e2697d75397b2584c32492d1" +checksum = "43792514b0c95c5beec42996da0c1b39265b02b75c97baa82d163d3ef55cbfa7" dependencies = [ "bitflags 2.4.2", "chrono", @@ -1475,6 +1476,12 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-conv" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" + [[package]] name = "num-integer" version = "0.1.45" @@ -1880,9 +1887,9 @@ dependencies = [ [[package]] name = "reqwest" -version = "0.11.23" +version = "0.11.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37b1ae8d9ac08420c66222fb9096fc5de435c3c48542bc5336c51892cffafb41" +checksum = "c6920094eb85afde5e4a138be3f2de8bbdf28000f0029e72c45025a56b042251" dependencies = [ "base64", "bytes", @@ -1903,6 +1910,7 @@ dependencies = [ "serde", "serde_json", "serde_urlencoded", + "sync_wrapper", "system-configuration", "tokio", "tower-service", @@ -1978,9 +1986,9 @@ dependencies = [ [[package]] name = "rust_decimal" -version = "1.33.1" +version = "1.34.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06676aec5ccb8fc1da723cc8c0f9a46549f21ebb8753d3915c6c41db1e7f1dc4" +checksum = "755392e1a2f77afd95580d3f0d0e94ac83eeeb7167552c9b5bca549e61a94d83" dependencies = [ "arrayvec", "borsh", @@ -2000,9 +2008,9 @@ checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" [[package]] name = "rustix" -version = "0.38.30" +version = "0.38.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "322394588aaf33c24007e8bb3238ee3e4c5c09c084ab32bc73890b99ff326bca" +checksum = "6ea3e1a662af26cd7a3ba09c0297a31af215563ecf42817c98df621387f4e949" dependencies = [ "bitflags 2.4.2", "errno", @@ -2088,6 +2096,13 @@ dependencies = [ "untrusted", ] +[[package]] +name = "sdk" +version = "0.0.0" +dependencies = [ + "wasm-bindgen", +] + [[package]] name = "sea-bae" version = "0.2.0" @@ -2684,6 +2699,12 @@ dependencies = [ "syn 2.0.48", ] +[[package]] +name = "sync_wrapper" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" + [[package]] name = "system-configuration" version = "0.5.1" @@ -2746,12 +2767,13 @@ dependencies = [ [[package]] name = "time" -version = "0.3.31" +version = "0.3.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f657ba42c3f86e7680e53c8cd3af8abbe56b5491790b46e22e19c0d57463583e" +checksum = "fe80ced77cbfb4cb91a94bf72b378b4b6791a0d9b7f09d0be747d1bdff4e68bd" dependencies = [ "deranged", "itoa", + "num-conv", "powerfmt", "serde", "time-core", @@ -2766,10 +2788,11 @@ checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" [[package]] name = "time-macros" -version = "0.2.16" +version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26197e33420244aeb70c3e8c78376ca46571bc4e701e4791c2cd9f57dcb3a43f" +checksum = "7ba3a3ef41e6672a2f0f001392bb5dcd3ff0a9992d618ca761a11c3121547774" dependencies = [ + "num-conv", "time-core", ] @@ -2790,9 +2813,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.35.1" +version = "1.36.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c89b4efa943be685f629b149f53829423f8f5531ea21249408e8e2f8671ec104" +checksum = "61285f6515fa018fb2d1e46eb21223fff441ee8db5d0f1435e8ab4f5cdb80931" dependencies = [ "backtrace", "bytes", @@ -2851,9 +2874,9 @@ checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1" [[package]] name = "toml_edit" -version = "0.21.0" +version = "0.21.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d34d383cd00a163b4a5b85053df514d45bc330f6de7737edfe0a93311d1eaa03" +checksum = "6a8534fd7f78b5405e860340ad6575217ce99f38d4d5c8f2442cb5ecb50090e1" dependencies = [ "indexmap", "toml_datetime", @@ -3245,9 +3268,9 @@ checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" [[package]] name = "winnow" -version = "0.5.35" +version = "0.5.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1931d78a9c73861da0134f453bb1f790ce49b2e30eba8410b4b79bac72b46a2d" +checksum = "818ce546a11a9986bc24f93d0cdf38a8a1a400f1473ea8c82e59f6e0ffab9249" dependencies = [ "memchr", ] diff --git a/Cargo.toml b/Cargo.toml index 53e135b56..59e9a9f49 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = ["packages/backend-rs"] +members = ["packages/backend-rs", "packages/sdk"] resolver = "2" [workspace.dependencies] @@ -7,6 +7,8 @@ napi = { version = "2.15.0", default-features = false } napi-build = "2.1.0" napi-derive = "2.15.0" +wasm-bindgen = "0.2.90" + argon2 = "0.5.3" async-trait = "0.1.77" basen = "0.1.0" diff --git a/Makefile b/Makefile index ea86c8f01..fe07b2173 100644 --- a/Makefile +++ b/Makefile @@ -9,15 +9,22 @@ PACKAGE_JSONS += packages/client/package.json PACKAGE_JSONS += packages/firefish-js/package.json PACKAGE_JSONS += packages/megalodon/package.json PACKAGE_JSONS += packages/sw/package.json +PACKAGE_JSONS += packages/sdk/package.json CARGO_TOMLS := Cargo.toml CARGO_TOMLS += packages/backend-rs/Cargo.toml +CARGO_TOMLS += packages/sdk/Cargo.toml RUST_BE_DIR := packages/backend-rs RUST_BE_SRC := Cargo.toml RUST_BE_SRC += $(RUST_BE_DIR)/Cargo.toml RUST_BE_SRC += $(call recursive_wildcard, $(RUST_BE_DIR)/src, *) +SDK_DIR := packages/sdk +SDK_SRC := Cargo.toml +SDK_SRC += $(SDK_DIR)/Cargo.toml +SDK_SRC += $(call recursive_wildcard, $(SDK_DIR)/src, *) + MIGRATIONS := $(foreach dir, packages/backend/migration, $(wildcard $(dir)/*)) MIGRATIONS += $(foreach dir, packages/backend/migration-neko, $(wildcard $(dir)/*)) @@ -85,12 +92,16 @@ format: .PHONY: rust-lint -rust-lint: $(T)/rust-be-lint +rust-lint: $(T)/rust-be-lint $(T)/sdk-lint $(T)/rust-be-lint: $(RUST_BE_SRC) pnpm --filter='backend-rs' run lint touch $@ +$(T)/sdk-lint: $(SDK_SRC) + pnpm --filter='sdk' run lint + touch $@ + .PHONY: regenerate-entities regenerate-entities: $(T)/regenerate-entities diff --git a/neko/index.js b/neko/index.js index 8a8c98a04..188fedbe9 100644 --- a/neko/index.js +++ b/neko/index.js @@ -1,7 +1,5 @@ -/* tslint:disable */ +// prettier-ignore /* eslint-disable */ -/* prettier-ignore */ - /* auto-generated by NAPI-RS */ const { existsSync, readFileSync } = require('fs') @@ -13,18 +11,52 @@ let nativeBinding = null let localFileExisted = false let loadError = null -function isMusl() { - // For Node 10 - if (!process.report || typeof process.report.getReport !== 'function') { - try { - const lddPath = require('child_process').execSync('which ldd').toString().trim() - return readFileSync(lddPath, 'utf8').includes('musl') - } catch (e) { +const isMusl = () => { + let musl = false + if (process.platform === 'linux') { + musl = isMuslFromFilesystem() + if (musl === null) { + musl = isMuslFromReport() + } + if (musl === null) { + musl = isMuslFromChildProcess() + } + } + return musl +} + +const isFileMusl = (f) => f.includes('libc.musl-') || f.includes('ld-musl-') + +const isMuslFromFilesystem = () => { + try { + return readFileSync('/usr/bin/ldd', 'utf-8').includes('musl') + } catch { + return null + } +} + +const isMuslFromReport = () => { + const report = typeof process.report.getReport === 'function' ? process.report.getReport() : null + if (!report) { + return null + } + if (report.header && report.header.glibcVersionRuntime) { + return false + } + if (Array.isArray(report.sharedObjects)) { + if (report.sharedObjects.some(isFileMusl)) { return true } - } else { - const { glibcVersionRuntime } = process.report.getReport().header - return !glibcVersionRuntime + } + return false +} + +const isMuslFromChildProcess = () => { + try { + return require('child_process').execSync('ldd --version', { encoding: 'utf8' }).includes('musl') + } catch (e) { + // If we reach this case, we don't know if the system is musl or not, so is better to just fallback to false + return false } } @@ -288,6 +320,21 @@ switch (platform) { throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`) } +if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) { + try { + nativeBinding = require('./backend-rs.wasi.cjs') + } catch { + // ignore + } + if (!nativeBinding) { + try { + nativeBinding = require('backend-rs-wasm32-wasi') + } catch (err) { + console.error(err) + } + } +} + if (!nativeBinding) { if (loadError) { throw loadError @@ -295,45 +342,41 @@ if (!nativeBinding) { throw new Error(`Failed to load native binding`) } -const { EnvConfig, readEnvironmentConfig, readServerConfig, JsDbConn, connectToDatabase, AntennaSrcEnum, MutedNoteReasonEnum, NoteVisibilityEnum, NotificationTypeEnum, PageVisibilityEnum, PollNotevisibilityEnum, RelayStatusEnum, UserEmojimodpermEnum, UserProfileFfvisibilityEnum, UserProfileMutingnotificationtypesEnum, stringToAcct, acctToString, checkWordMute, getFullApAccount, isSelfHost, extractHost, toPuny, toPunyOptional, convertToHiddenPost, sqlLikeEscape, safeForSql, formatMilliseconds, nativeInitIdGenerator, nativeCreateId, nativeGetTimestamp, fetchMeta, metaToPugArgs, hasOtherRenoteOfThisNote, nyaify, hashPassword, verifyPassword, isOldPasswordAlgorithm, genString, IdConvertType, convertId } = nativeBinding - -module.exports.EnvConfig = EnvConfig -module.exports.readEnvironmentConfig = readEnvironmentConfig -module.exports.readServerConfig = readServerConfig -module.exports.JsDbConn = JsDbConn -module.exports.connectToDatabase = connectToDatabase -module.exports.AntennaSrcEnum = AntennaSrcEnum -module.exports.MutedNoteReasonEnum = MutedNoteReasonEnum -module.exports.NoteVisibilityEnum = NoteVisibilityEnum -module.exports.NotificationTypeEnum = NotificationTypeEnum -module.exports.PageVisibilityEnum = PageVisibilityEnum -module.exports.PollNotevisibilityEnum = PollNotevisibilityEnum -module.exports.RelayStatusEnum = RelayStatusEnum -module.exports.UserEmojimodpermEnum = UserEmojimodpermEnum -module.exports.UserProfileFfvisibilityEnum = UserProfileFfvisibilityEnum -module.exports.UserProfileMutingnotificationtypesEnum = UserProfileMutingnotificationtypesEnum -module.exports.stringToAcct = stringToAcct -module.exports.acctToString = acctToString -module.exports.checkWordMute = checkWordMute -module.exports.getFullApAccount = getFullApAccount -module.exports.isSelfHost = isSelfHost -module.exports.extractHost = extractHost -module.exports.toPuny = toPuny -module.exports.toPunyOptional = toPunyOptional -module.exports.convertToHiddenPost = convertToHiddenPost -module.exports.sqlLikeEscape = sqlLikeEscape -module.exports.safeForSql = safeForSql -module.exports.formatMilliseconds = formatMilliseconds -module.exports.nativeInitIdGenerator = nativeInitIdGenerator -module.exports.nativeCreateId = nativeCreateId -module.exports.nativeGetTimestamp = nativeGetTimestamp -module.exports.fetchMeta = fetchMeta -module.exports.metaToPugArgs = metaToPugArgs -module.exports.hasOtherRenoteOfThisNote = hasOtherRenoteOfThisNote -module.exports.nyaify = nyaify -module.exports.hashPassword = hashPassword -module.exports.verifyPassword = verifyPassword -module.exports.isOldPasswordAlgorithm = isOldPasswordAlgorithm -module.exports.genString = genString -module.exports.IdConvertType = IdConvertType -module.exports.convertId = convertId +module.exports.EnvConfig = nativeBinding.EnvConfig +module.exports.JsDbConn = nativeBinding.JsDbConn +module.exports.AntennaSrcEnum = nativeBinding.AntennaSrcEnum +module.exports.checkWordMute = nativeBinding.checkWordMute +module.exports.connectToDatabase = nativeBinding.connectToDatabase +module.exports.convertId = nativeBinding.convertId +module.exports.convertToHiddenPost = nativeBinding.convertToHiddenPost +module.exports.extractHost = nativeBinding.extractHost +module.exports.fetchMeta = nativeBinding.fetchMeta +module.exports.formatMilliseconds = nativeBinding.formatMilliseconds +module.exports.genString = nativeBinding.genString +module.exports.getFullApAccount = nativeBinding.getFullApAccount +module.exports.hashPassword = nativeBinding.hashPassword +module.exports.hasOtherRenoteOfThisNote = nativeBinding.hasOtherRenoteOfThisNote +module.exports.IdConvertType = nativeBinding.IdConvertType +module.exports.isOldPasswordAlgorithm = nativeBinding.isOldPasswordAlgorithm +module.exports.isSelfHost = nativeBinding.isSelfHost +module.exports.metaToPugArgs = nativeBinding.metaToPugArgs +module.exports.MutedNoteReasonEnum = nativeBinding.MutedNoteReasonEnum +module.exports.nativeCreateId = nativeBinding.nativeCreateId +module.exports.nativeGetTimestamp = nativeBinding.nativeGetTimestamp +module.exports.nativeInitIdGenerator = nativeBinding.nativeInitIdGenerator +module.exports.NoteVisibilityEnum = nativeBinding.NoteVisibilityEnum +module.exports.NotificationTypeEnum = nativeBinding.NotificationTypeEnum +module.exports.nyaify = nativeBinding.nyaify +module.exports.PageVisibilityEnum = nativeBinding.PageVisibilityEnum +module.exports.PollNotevisibilityEnum = nativeBinding.PollNotevisibilityEnum +module.exports.readEnvironmentConfig = nativeBinding.readEnvironmentConfig +module.exports.readServerConfig = nativeBinding.readServerConfig +module.exports.RelayStatusEnum = nativeBinding.RelayStatusEnum +module.exports.safeForSql = nativeBinding.safeForSql +module.exports.sqlLikeEscape = nativeBinding.sqlLikeEscape +module.exports.toPuny = nativeBinding.toPuny +module.exports.toPunyOptional = nativeBinding.toPunyOptional +module.exports.UserEmojimodpermEnum = nativeBinding.UserEmojimodpermEnum +module.exports.UserProfileFfvisibilityEnum = nativeBinding.UserProfileFfvisibilityEnum +module.exports.UserProfileMutingnotificationtypesEnum = nativeBinding.UserProfileMutingnotificationtypesEnum +module.exports.verifyPassword = nativeBinding.verifyPassword diff --git a/neko/update/native.sh b/neko/update/native.sh index 5add1cc83..e5ec0f559 100755 --- a/neko/update/native.sh +++ b/neko/update/native.sh @@ -42,6 +42,7 @@ br say 'Start building Firefish.' say 'It takes some time! Why not brew a cup of cofe?' +run 'rustup target add wasm32-wasi-preview1-threads' run 'NODE_OPTIONS="--max_old_space_size=3072" NODE_ENV="production" pnpm run rebuild' say "Done! We're almost there." diff --git a/package.json b/package.json index 1109fae56..8c4def64e 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "private": true, "scripts": { "rebuild": "pnpm run clean && pnpm run build", - "build": "pnpm --filter backend-rs run build && pnpm --filter firefish-js run build && pnpm --filter !firefish-js --filter !backend-rs -r --parallel run build && pnpm run gulp", + "build": "pnpm --filter sdk run build && pnpm --filter backend-rs run build && pnpm --filter firefish-js run build && pnpm --filter !sdk --filter !firefish-js --filter !backend-rs -r --parallel run build && pnpm run gulp", "start": "pnpm --filter backend run start", "start:container": "pnpm run gulp && pnpm run migrate && pnpm run start", "init": "pnpm run migrate", @@ -25,7 +25,7 @@ "dev:staging": "NODE_OPTIONS=--max_old_space_size=3072 NODE_ENV=development pnpm run build && pnpm run start", "lint": "pnpm -r --parallel run lint", "debug": "pnpm run build:debug && pnpm run start", - "build:debug": "pnpm clean && pnpm --filter backend-rs run build:debug && pnpm --filter firefish-js run build:debug && pnpm --filter firefish-js run build:types && pnpm --filter !firefish-js --filter !backend-rs -r --parallel run build:debug && pnpm run gulp", + "build:debug": "pnpm clean && pnpm --filter sdk run build:debug && pnpm --filter backend-rs run build:debug && pnpm --filter firefish-js run build:debug && pnpm --filter firefish-js run build:types && pnpm --filter !sdk --filter !firefish-js --filter !backend-rs -r --parallel run build:debug && pnpm run gulp", "format": "pnpm -r --parallel run format", "clean": "pnpm node ./scripts/clean-built.mjs", "clean-cargo": "cargo clean", @@ -36,7 +36,6 @@ "dependencies": { "@bull-board/api": "5.14.0", "@bull-board/ui": "5.14.0", - "@napi-rs/cli": "2.18.0", "gulp": "4.0.2", "gulp-cssnano": "2.1.3", "gulp-replace": "1.1.4", diff --git a/packages/README.md b/packages/README.md index 724f79322..d98f4b1a8 100644 --- a/packages/README.md +++ b/packages/README.md @@ -8,3 +8,4 @@ This directory contains all of the packages Firefish uses. - `sw`: Web [Service Worker](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API) written in TypeScript - `firefish-js`: TypeScript SDK for both backend and client, also published on [NPM](https://www.npmjs.com/package/firefish-js) for public use - `megalodon`: TypeScript library used for partial Mastodon API compatibility +- `sdk`: SDK for both backend and client, written in Rust, bound to WebAssembly by [NAPI-RS](https://napi.rs/) diff --git a/packages/backend-rs/Cargo.toml b/packages/backend-rs/Cargo.toml index b52800e5c..fa2cd5233 100644 --- a/packages/backend-rs/Cargo.toml +++ b/packages/backend-rs/Cargo.toml @@ -10,6 +10,8 @@ crate-type = ["cdylib", "lib"] napi = { workspace = true, default-features = false, features = ["napi9", "tokio_rt", "chrono_date", "serde-json"] } napi-derive = { workspace = true } +sdk = { path = "../sdk" } + argon2 = { workspace = true } async-trait = { workspace = true } basen = { workspace = true } diff --git a/packages/backend-rs/package.json b/packages/backend-rs/package.json index 4c7f8a2b3..69e806205 100644 --- a/packages/backend-rs/package.json +++ b/packages/backend-rs/package.json @@ -4,26 +4,11 @@ "main": "built/index.js", "types": "built/index.d.ts", "napi": { - "name": "backend-rs", - "triples": { - "additional": [ - "aarch64-apple-darwin", - "aarch64-linux-android", - "aarch64-unknown-linux-gnu", - "aarch64-unknown-linux-musl", - "aarch64-pc-windows-msvc", - "armv7-unknown-linux-gnueabihf", - "x86_64-unknown-linux-musl", - "x86_64-unknown-freebsd", - "i686-pc-windows-msvc", - "armv7-linux-androideabi", - "universal-apple-darwin" - ] - } + "binaryName": "backend-rs" }, "license": "MIT", "devDependencies": { - "@napi-rs/cli": "2.18.0", + "@napi-rs/cli": "3.0.0-alpha.36", "ava": "6.1.1" }, "ava": { @@ -31,8 +16,8 @@ }, "scripts": { "artifacts": "napi artifacts", - "build": "napi build --platform --release ./built/", - "build:debug": "napi build --platform ./built/", + "build": "napi build --platform --release --output-dir ./built/", + "build:debug": "napi build --platform --output-dir ./built/", "prepublishOnly": "napi prepublish -t npm", "universal": "napi universal", "version": "napi version", diff --git a/packages/backend-rs/src/lib.rs b/packages/backend-rs/src/lib.rs index 6bfbb4643..17b47568f 100644 --- a/packages/backend-rs/src/lib.rs +++ b/packages/backend-rs/src/lib.rs @@ -1,3 +1,5 @@ +pub use sdk; + pub mod config; pub mod database; pub mod macros; diff --git a/packages/backend-rs/src/util/mod.rs b/packages/backend-rs/src/util/mod.rs index 0c6789409..31f69615d 100644 --- a/packages/backend-rs/src/util/mod.rs +++ b/packages/backend-rs/src/util/mod.rs @@ -1,4 +1,3 @@ -pub mod acct; pub mod check_word_mute; pub mod convert_host; pub mod convert_to_hidden_post; diff --git a/packages/backend/.gitignore b/packages/backend/.gitignore new file mode 100644 index 000000000..cc8bf1ede --- /dev/null +++ b/packages/backend/.gitignore @@ -0,0 +1 @@ +built \ No newline at end of file diff --git a/packages/backend/src/queue/processors/db/import-blocking.ts b/packages/backend/src/queue/processors/db/import-blocking.ts index b081ac04f..166f7c2d6 100644 --- a/packages/backend/src/queue/processors/db/import-blocking.ts +++ b/packages/backend/src/queue/processors/db/import-blocking.ts @@ -3,7 +3,7 @@ import type Bull from "bull"; import { queueLogger } from "../../logger.js"; import { resolveUser } from "@/remote/resolve-user.js"; import { downloadTextFile } from "@/misc/download-text-file.js"; -import { isSelfHost, stringToAcct, toPuny } from "backend-rs"; +import { isSelfHost, toPuny, stringToAcct } from "backend-rs"; import { Users, DriveFiles } from "@/models/index.js"; import type { DbUserImportJobData } from "@/queue/types.js"; import block from "@/services/blocking/create.js"; diff --git a/packages/backend/src/queue/processors/db/import-following.ts b/packages/backend/src/queue/processors/db/import-following.ts index d8a5ad1f3..a5f0331d8 100644 --- a/packages/backend/src/queue/processors/db/import-following.ts +++ b/packages/backend/src/queue/processors/db/import-following.ts @@ -3,7 +3,7 @@ import follow from "@/services/following/create.js"; import { resolveUser } from "@/remote/resolve-user.js"; import { downloadTextFile } from "@/misc/download-text-file.js"; -import { isSelfHost, stringToAcct, toPuny } from "backend-rs"; +import { isSelfHost, toPuny, stringToAcct } from "backend-rs"; import { Users, DriveFiles } from "@/models/index.js"; import type { DbUserImportJobData } from "@/queue/types.js"; import { queueLogger } from "../../logger.js"; diff --git a/packages/backend/src/queue/processors/db/import-muting.ts b/packages/backend/src/queue/processors/db/import-muting.ts index b27b577aa..aac12da8e 100644 --- a/packages/backend/src/queue/processors/db/import-muting.ts +++ b/packages/backend/src/queue/processors/db/import-muting.ts @@ -3,7 +3,7 @@ import type Bull from "bull"; import { queueLogger } from "../../logger.js"; import { resolveUser } from "@/remote/resolve-user.js"; import { downloadTextFile } from "@/misc/download-text-file.js"; -import { isSelfHost, stringToAcct, toPuny } from "backend-rs"; +import { isSelfHost, toPuny, stringToAcct } from "backend-rs"; import { Users, DriveFiles, Mutings } from "@/models/index.js"; import type { DbUserImportJobData } from "@/queue/types.js"; import type { User } from "@/models/entities/user.js"; diff --git a/packages/backend/src/queue/processors/db/import-user-lists.ts b/packages/backend/src/queue/processors/db/import-user-lists.ts index f372d8124..7c98ec8a6 100644 --- a/packages/backend/src/queue/processors/db/import-user-lists.ts +++ b/packages/backend/src/queue/processors/db/import-user-lists.ts @@ -4,7 +4,7 @@ import { queueLogger } from "../../logger.js"; import { resolveUser } from "@/remote/resolve-user.js"; import { pushUserToUserList } from "@/services/user-list/push.js"; import { downloadTextFile } from "@/misc/download-text-file.js"; -import { isSelfHost, stringToAcct, toPuny } from "backend-rs"; +import { isSelfHost, toPuny, stringToAcct } from "backend-rs"; import { DriveFiles, Users, diff --git a/packages/backend/src/server/index.ts b/packages/backend/src/server/index.ts index f213f7709..b03059a7f 100644 --- a/packages/backend/src/server/index.ts +++ b/packages/backend/src/server/index.ts @@ -16,11 +16,10 @@ import { IsNull } from "typeorm"; import config from "@/config/index.js"; import Logger from "@/services/logger.js"; import { Users } from "@/models/index.js"; -import { fetchMeta } from "@/misc/backend-rs.js"; import { genIdenticon } from "@/misc/gen-identicon.js"; import { createTemp } from "@/misc/create-temp.js"; import { stringToAcct } from "backend-rs"; -import { envConfig } from "@/misc/backend-rs.js"; +import { envConfig, fetchMeta } from "@/misc/backend-rs.js"; import megalodon, { MegalodonInterface } from "megalodon"; import activityPub from "./activitypub.js"; import nodeinfo from "./nodeinfo.js"; diff --git a/packages/backend/src/server/web/index.ts b/packages/backend/src/server/web/index.ts index b36b8485b..ed3983636 100644 --- a/packages/backend/src/server/web/index.ts +++ b/packages/backend/src/server/web/index.ts @@ -17,7 +17,7 @@ import { KoaAdapter } from "@bull-board/koa"; import { In, IsNull } from "typeorm"; import { fetchMeta } from "@/misc/backend-rs.js"; -import { metaToPugArgs } from "backend-rs"; +import { metaToPugArgs, stringToAcct } from "backend-rs"; import config from "@/config/index.js"; import { Users, @@ -29,7 +29,6 @@ import { Emojis, GalleryPosts, } from "@/models/index.js"; -import { stringToAcct } from "backend-rs"; import { getNoteSummary } from "@/misc/get-note-summary.js"; import { queues } from "@/queue/queues.js"; import { genOpenapiSpec } from "../api/openapi/gen-spec.js"; diff --git a/packages/backend/src/server/well-known.ts b/packages/backend/src/server/well-known.ts index 9421b9cc3..1ec41cf2d 100644 --- a/packages/backend/src/server/well-known.ts +++ b/packages/backend/src/server/well-known.ts @@ -1,8 +1,7 @@ import Router from "@koa/router"; import config from "@/config/index.js"; -import { stringToAcct } from "backend-rs"; -import type { Acct } from "backend-rs"; +import { stringToAcct, type Acct } from "backend-rs"; import { links } from "./nodeinfo.js"; import { escapeAttribute, escapeValue } from "@/prelude/xml.js"; import { Users } from "@/models/index.js"; diff --git a/packages/client/.gitignore b/packages/client/.gitignore new file mode 100644 index 000000000..cc8bf1ede --- /dev/null +++ b/packages/client/.gitignore @@ -0,0 +1 @@ +built \ No newline at end of file diff --git a/packages/client/package.json b/packages/client/package.json index 7aa7a995e..f45601dd6 100644 --- a/packages/client/package.json +++ b/packages/client/package.json @@ -71,6 +71,7 @@ "rollup": "4.9.6", "s-age": "1.1.2", "sass": "1.70.0", + "sdk": "workspace:*", "seedrandom": "3.0.5", "stringz": "2.1.0", "swiper": "11.0.5", @@ -85,6 +86,8 @@ "uuid": "9.0.1", "vite": "5.0.12", "vite-plugin-compression": "0.5.1", + "vite-plugin-top-level-await": "^1.4.1", + "vite-plugin-wasm": "^3.3.0", "vue": "3.4.15", "vue-draggable-plus": "^0.3.5", "vue-plyr": "^7.0.0", diff --git a/packages/client/src/account.ts b/packages/client/src/account.ts index 2957ab085..74637de5b 100644 --- a/packages/client/src/account.ts +++ b/packages/client/src/account.ts @@ -1,4 +1,4 @@ -import type * as firefish from "firefish-js"; +import type { entities } from "firefish-js"; import { defineAsyncComponent } from "vue"; import { i18n } from "./i18n"; import { apiUrl } from "@/config"; @@ -10,7 +10,7 @@ import { reloadChannel, unisonReload } from "@/scripts/unison-reload"; // TODO: 他のタブと永続化されたstateを同期 -export type Account = firefish.entities.MeDetailed; +export type Account = entities.MeDetailed; export async function signout() { waiting(); @@ -149,8 +149,8 @@ export async function openAccountMenu( opts: { includeCurrentAccount?: boolean; withExtraOperation: boolean; - active?: firefish.entities.UserDetailed["id"]; - onChoose?: (account: firefish.entities.UserDetailed) => void; + active?: entities.UserDetailed["id"]; + onChoose?: (account: entities.UserDetailed) => void; }, ev: MouseEvent, isMobile?: boolean, @@ -183,7 +183,7 @@ export async function openAccountMenu( ); } - async function switchAccount(account: firefish.entities.UserDetailed) { + async function switchAccount(account: entities.UserDetailed) { const storedAccounts = await getAccounts(); const token = storedAccounts.find((x) => x.id === account.id).token; switchAccountWithToken(token); @@ -200,7 +200,7 @@ export async function openAccountMenu( userIds: storedAccounts.map((x) => x.id), }); - function createItem(account: firefish.entities.UserDetailed) { + function createItem(account: entities.UserDetailed) { return { type: "user", user: account, diff --git a/packages/client/src/components/MkAbuseReportWindow.vue b/packages/client/src/components/MkAbuseReportWindow.vue index e93b834f8..df087ffa0 100644 --- a/packages/client/src/components/MkAbuseReportWindow.vue +++ b/packages/client/src/components/MkAbuseReportWindow.vue @@ -41,7 +41,7 @@ diff --git a/packages/client/src/components/MkDriveWindow.vue b/packages/client/src/components/MkDriveWindow.vue index f116b6e8f..bdddcd2e5 100644 --- a/packages/client/src/components/MkDriveWindow.vue +++ b/packages/client/src/components/MkDriveWindow.vue @@ -14,14 +14,13 @@ diff --git a/packages/client/src/components/MkNoteSub.vue b/packages/client/src/components/MkNoteSub.vue index 7d5090526..61800a9e8 100644 --- a/packages/client/src/components/MkNoteSub.vue +++ b/packages/client/src/components/MkNoteSub.vue @@ -191,7 +191,7 @@ diff --git a/packages/client/src/pages/clip.vue b/packages/client/src/pages/clip.vue index 63ff6315c..61748b736 100644 --- a/packages/client/src/pages/clip.vue +++ b/packages/client/src/pages/clip.vue @@ -29,7 +29,7 @@