From b876589b153cda67acbd2c38f2c50cc672d8798d Mon Sep 17 00:00:00 2001 From: Keita Kobayashi Date: Fri, 10 Jan 2025 12:47:17 +0900 Subject: [PATCH] Multiplatform --- .github/.env | 5 + .github/actions/setup/action.yml | 76 ++++++++++ .github/workflows/build.yml | 137 ++++++++++++++++++ .github/workflows/cicd.yml | 33 ----- .github/workflows/release.yml | 135 +++++++++++++++++ .github/workflows/test.yml | 73 ++++++++++ Cargo.toml | 21 +-- README.md | 2 +- crates/audio-snippet-detector/Cargo.toml | 18 +++ .../audio-snippet-detector/src}/asd.rs | 0 .../audio-snippet-detector/src}/db.rs | 0 .../audio-snippet-detector/src}/lib.rs | 0 .../audio-snippet-detector/src}/mfcc.rs | 0 .../audio-snippet-detector/src}/util.rs | 0 index.node.d.ts | 9 -- package-lock.json | 13 +- package.json | 29 +++- platforms/darwin-arm64/package.json | 25 ++++ platforms/linux-arm64-gnu/package.json | 25 ++++ platforms/linux-x64-gnu/package.json | 25 ++++ ts/index.ts => src/index.cts | 14 +- src/index.mts | 1 + {ts => src}/index.test.ts | 2 +- src/load.cts | 16 ++ tsconfig.dist.json | 4 +- tsconfig.json | 2 +- 26 files changed, 589 insertions(+), 76 deletions(-) create mode 100644 .github/.env create mode 100644 .github/actions/setup/action.yml create mode 100644 .github/workflows/build.yml delete mode 100644 .github/workflows/cicd.yml create mode 100644 .github/workflows/release.yml create mode 100644 .github/workflows/test.yml create mode 100644 crates/audio-snippet-detector/Cargo.toml rename {src => crates/audio-snippet-detector/src}/asd.rs (100%) rename {src => crates/audio-snippet-detector/src}/db.rs (100%) rename {src => crates/audio-snippet-detector/src}/lib.rs (100%) rename {src => crates/audio-snippet-detector/src}/mfcc.rs (100%) rename {src => crates/audio-snippet-detector/src}/util.rs (100%) delete mode 100644 index.node.d.ts create mode 100644 platforms/darwin-arm64/package.json create mode 100644 platforms/linux-arm64-gnu/package.json create mode 100644 platforms/linux-x64-gnu/package.json rename ts/index.ts => src/index.cts (56%) create mode 100644 src/index.mts rename {ts => src}/index.test.ts (96%) create mode 100644 src/load.cts diff --git a/.github/.env b/.github/.env new file mode 100644 index 0000000..0911b1b --- /dev/null +++ b/.github/.env @@ -0,0 +1,5 @@ +NODE_VERSION=22.x +NPM_REGISTRY=https://registry.npmjs.org +RUST_VERSION=stable +ACTIONS_USER=github-actions +ACTIONS_EMAIL=github-actions@github.com diff --git a/.github/actions/setup/action.yml b/.github/actions/setup/action.yml new file mode 100644 index 0000000..3364b02 --- /dev/null +++ b/.github/actions/setup/action.yml @@ -0,0 +1,76 @@ +name: 'Setup Neon' +description: 'Setup the Neon toolchain.' +inputs: + platform: + description: 'Platform being built for.' + required: false + default: '' + use-rust: + description: 'Install Rust?' + required: false + default: 'true' + use-cross: + description: 'Install cross-rs?' + required: false + default: 'false' + workspace: + description: 'Path to workspace being setup.' + required: false + default: '.' +outputs: + rust: + description: 'Rust version installed.' + value: ${{ steps.rust.outputs.version }} + node: + description: 'Node version installed.' + value: ${{ steps.node.outputs.version }} + target: + description: 'Rust target architecture installed.' + value: ${{ steps.target.outputs.target }} +runs: + using: "composite" + steps: + - name: Set Environment Variables + uses: falti/dotenv-action@d1cd55661714e830a6e26f608f81d36e23424fed # v1.1.2 + with: + path: ./.github/.env + export-variables: true + keys-case: bypass + - name: Install Node + uses: actions/setup-node@v4 + with: + node-version: ${{ env.NODE_VERSION }} + registry-url: ${{ env.NPM_REGISTRY }} + cache: npm + - name: Install Dependencies + shell: bash + run: npm ci + - name: Compute Rust Target + if: ${{ inputs['use-rust'] == 'true' }} + id: target + shell: bash + run: echo target=$(npx neon list-platforms | jq -r '.["${{ inputs.platform }}"]') | tee -a $GITHUB_OUTPUT + working-directory: ${{ inputs.workspace }} + - name: Install Rust + if: ${{ inputs['use-rust'] == 'true' }} + uses: actions-rust-lang/setup-rust-toolchain@v1 + with: + toolchain: ${{ env.RUST_VERSION }} + target: ${{ steps.target.outputs.target }} + override: true + - name: Install cross-rs + if: ${{ inputs['use-cross'] == 'true' }} + uses: baptiste0928/cargo-install@v3 + with: + crate: cross + - name: Node Version + id: node + shell: bash + run: | + echo version=$(node -e 'console.log(process.versions.node)') | tee -a $GITHUB_OUTPUT + - name: Rust Version + if: ${{ inputs['use-rust'] == 'true' }} + id: rust + shell: bash + run: | + echo version=$(cargo -Vv | fgrep release: | cut -d' ' -f2) | tee -a $GITHUB_OUTPUT diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..2939a48 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,137 @@ +name: Build + +on: + workflow_call: + inputs: + ref: + description: 'The branch, tag, or SHA to check out' + required: true + type: string + update-version: + description: 'Update version before building?' + required: false + type: boolean + default: false + version: + description: 'Version update (ignored if update-version is false)' + required: false + type: string + default: 'patch' + github-release: + description: 'Publish GitHub release?' + required: false + type: boolean + default: false + tag: + description: 'The release tag (ignored if github-release is false)' + required: false + type: string + default: '' + +jobs: + matrix: + name: Matrix + runs-on: ubuntu-latest + outputs: + matrix: ${{ steps.matrix.outputs.result }} + steps: + - name: Checkout Code + uses: actions/checkout@v4 + with: + ref: ${{ inputs.ref }} + - name: Setup Neon Environment + uses: ./.github/actions/setup + with: + use-rust: false + - name: Look Up Matrix Data + id: matrixData + shell: bash + run: echo "json=$(npx neon show ci github | jq -rc)" | tee -a $GITHUB_OUTPUT + - name: Compute Matrix + id: matrix + uses: actions/github-script@v7 + with: + script: | + const platforms = ${{ steps.matrixData.outputs.json }}; + const macOS = platforms.macOS.map(platform => { + return { os: "macos-latest", platform, script: "build" }; + }); + const windows = platforms.Windows.map(platform => { + return { os: "windows-latest", platform, script: "build" }; + }); + const linux = platforms.Linux.map(platform => { + return { os: "ubuntu-latest", platform, script: "cross" }; + }); + return [...macOS, ...windows, ...linux]; + + binaries: + name: Binaries + needs: [matrix] + strategy: + matrix: + cfg: ${{ fromJSON(needs.matrix.outputs.matrix) }} + runs-on: ${{ matrix.cfg.os }} + permissions: + contents: write + steps: + - name: Checkout Code + uses: actions/checkout@v4 + with: + ref: ${{ inputs.ref }} + - name: Setup Neon Environment + id: neon + uses: ./.github/actions/setup + with: + use-cross: ${{ matrix.cfg.script == 'cross' }} + platform: ${{ matrix.cfg.platform }} + - name: Update Version + if: ${{ inputs.update-version }} + shell: bash + run: | + git config --global user.name $ACTIONS_USER + git config --global user.email $ACTIONS_EMAIL + npm version ${{ inputs.version }} -m "v%s" + - name: Build + shell: bash + env: + CARGO_BUILD_TARGET: ${{ steps.neon.outputs.target }} + NEON_BUILD_PLATFORM: ${{ matrix.cfg.platform }} + run: npm run ${{ matrix.cfg.script }} + - name: Pack + id: pack + shell: bash + run: | + mkdir -p dist + echo filename=$(basename $(npm pack ./platforms/${{ matrix.cfg.platform }} --silent --pack-destination=./dist --json | jq -r '.[0].filename')) | tee -a $GITHUB_OUTPUT + - name: Release + if: ${{ inputs.github-release }} + uses: softprops/action-gh-release@9d7c94cfd0a1f3ed45544c887983e9fa900f0564 # v2.0.4 + with: + files: ./dist/${{ steps.pack.outputs.filename }} + tag_name: ${{ inputs.tag }} + + main: + name: Main + needs: [matrix] + runs-on: ubuntu-latest + steps: + - name: Checkout Code + uses: actions/checkout@v4 + with: + ref: ${{ inputs.ref }} + - name: Setup Neon Environment + uses: ./.github/actions/setup + with: + use-rust: false + - name: Pack + id: pack + shell: bash + run: | + mkdir -p dist + echo "filename=$(npm pack --silent --pack-destination=./dist)" | tee -a $GITHUB_OUTPUT + - name: Release + if: ${{ inputs.github-release }} + uses: softprops/action-gh-release@9d7c94cfd0a1f3ed45544c887983e9fa900f0564 # v2.0.4 + with: + files: ./dist/${{ steps.pack.outputs.filename }} + tag_name: ${{ inputs.tag }} diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml deleted file mode 100644 index 6eacb9d..0000000 --- a/.github/workflows/cicd.yml +++ /dev/null @@ -1,33 +0,0 @@ -name: CICD - -on: - push: - branches: - - main - pull_request: - branches: - - main - -jobs: - build: - runs-on: ubuntu-latest - - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - - name: Set up Node.js - uses: actions/setup-node@v2 - with: - node-version: '22' - - - uses: actions-rust-lang/setup-rust-toolchain@v1 - - - name: Install dependencies - run: npm ci - - - name: Build - run: npm run build - - - name: Run tests - run: npm test diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..f755a8e --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,135 @@ +name: Release + +run-name: | + ${{ (inputs.dryrun && 'Dry run') + || format('Release: {0}', (inputs.version == 'custom' && inputs.custom) || inputs.version) }} + +on: + workflow_dispatch: + inputs: + dryrun: + description: 'Dry run (no npm publish)' + required: false + type: boolean + default: true + version: + description: 'Version component to update (or "custom" to provide exact version)' + required: true + type: choice + options: + - patch + - minor + - major + - prepatch + - preminor + - premajor + - prerelease + - custom + custom: + description: 'Custom version' + required: false + default: '' + +jobs: + setup: + name: Setup + runs-on: ubuntu-latest + permissions: + contents: write + outputs: + dryrun: ${{ steps.dryrun.outputs.dryrun }} + publish: ${{ steps.publish.outputs.publish }} + ref: ${{ steps.tag.outputs.tag || github.event.repository.default_branch }} + tag: ${{ steps.tag.outputs.tag || '' }} + steps: + - name: Validate Workflow Inputs + if: ${{ inputs.version == 'custom' && inputs.custom == '' }} + shell: bash + run: | + echo '::error::No custom version number provided' + exit 1 + - id: dryrun + name: Validate Dry Run Event + if: ${{ inputs.dryrun }} + shell: bash + run: echo dryrun=true | tee -a $GITHUB_OUTPUT + - id: publish + name: Validate Publish Event + if: ${{ !inputs.dryrun }} + shell: bash + env: + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + run: | + if [[ -z $NPM_TOKEN ]]; then + echo "::error::Secret NPM_TOKEN is not defined for this GitHub repo." + echo "::error::To publish to npm, this action requires:" + echo "::error:: • an npm access token;" + echo "::error:: • with Read-Write access to this project's npm packages;" + echo "::error:: • stored as a repo secret named NPM_TOKEN." + echo "::error::See https://docs.npmjs.com/about-access-tokens for info about creating npm tokens." + echo "::error:: 💡 The simplest method is to create a Classic npm token of type Automation." + echo "::error:: 💡 For greater security, consider using a Granual access token." + echo "::error::See https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions for info about how to store GitHub repo secrets." + exit 1 + fi + echo publish=true | tee -a $GITHUB_OUTPUT + - name: Checkout Code + uses: actions/checkout@v4 + - name: Setup Neon Environment + uses: ./.github/actions/setup + with: + use-rust: false + - name: Tag Release + if: ${{ !inputs.dryrun }} + id: tag + shell: bash + run: | + git config --global user.name $ACTIONS_USER + git config --global user.email $ACTIONS_EMAIL + npm version -m 'v%s' '${{ (inputs.version == 'custom' && inputs.custom) || inputs.version }}' + git push --follow-tags + echo tag=$(git describe --abbrev=0) | tee -a $GITHUB_OUTPUT + + build: + name: Build + needs: [setup] + permissions: + contents: write + uses: ./.github/workflows/build.yml + with: + ref: ${{ needs.setup.outputs.ref }} + tag: ${{ needs.setup.outputs.tag }} + update-version: ${{ !!needs.setup.outputs.dryrun }} + version: ${{ (inputs.version == 'custom' && inputs.custom) || inputs.version }} + github-release: ${{ !!needs.setup.outputs.publish }} + + publish: + name: Publish + if: ${{ needs.setup.outputs.publish }} + needs: [setup, build] + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - name: Checkout Code + uses: actions/checkout@v4 + with: + ref: ${{ needs.setup.outputs.ref }} + - name: Setup Neon Environment + uses: ./.github/actions/setup + with: + use-rust: false + - name: Fetch + uses: robinraju/release-downloader@c39a3b234af58f0cf85888573d361fb6fa281534 # v1.10 + with: + tag: ${{ needs.setup.outputs.tag }} + fileName: "*.tgz" + out-file-path: ./dist + - name: Publish + shell: bash + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} + run: | + for p in ./dist/*.tgz ; do + npm publish --access public $p + done diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..2b308e5 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,73 @@ +name: Test + +run-name: | + ${{ (github.event_name == 'pull_request' && format('Test (PR #{0}): {1}', github.event.number, github.event.pull_request.title)) + || format('Test: {0}', github.event.head_commit.message) }} + +on: + # Event: A maintainer has pushed commits or merged a PR to main. + push: + # Limiting push events to 'main' prevents duplicate runs of this workflow + # when maintainers push to internal PRs. + branches: + - main + + # Event: A contributor has created or updated a PR. + pull_request: + types: [opened, synchronize, reopened, labeled] + branches: + - main + +jobs: + pr: + name: Pull Request Details + runs-on: ubuntu-latest + if: ${{ github.event_name == 'pull_request' }} + outputs: + branch: ${{ steps.pr-ref.outputs.branch || github.event.repository.default_branch }} + steps: + - name: PR Branch + id: pr-ref + shell: bash + run: echo "branch=$(gh pr view $PR_NO --repo $REPO --json headRefName --jq '.headRefName')" | tee -a "$GITHUB_OUTPUT" + env: + REPO: ${{ github.repository }} + PR_NO: ${{ github.event.number }} + GH_TOKEN: ${{ github.token }} + + # Labeling a PR with a `ci:full-matrix` label does a full matrix build on + # every run of this workflow for that PR, in addition to the other tests. + full-matrix: + name: Build + if: ${{ github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 'ci:full-matrix') }} + needs: [pr] + permissions: + contents: write + uses: ./.github/workflows/build.yml + with: + ref: ${{ needs.pr.outputs.branch }} + update-version: true + github-release: false + + tests: + name: Tests + runs-on: ubuntu-latest + steps: + - name: Checkout Code + uses: actions/checkout@v4 + - name: Setup Neon Environment + id: neon + uses: ./.github/actions/setup + with: + platform: linux-x64-gnu + + - name: Build + shell: bash + env: + CARGO_BUILD_TARGET: ${{ steps.neon.outputs.target }} + NEON_BUILD_PLATFORM: linux-x64-gnu + run: npm run debug + + - name: Test + shell: bash + run: npm test diff --git a/Cargo.toml b/Cargo.toml index 7067918..291fa96 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,18 +1,3 @@ -[package] -name = "audio-snippet-detector" -version = "0.0.1" -authors = ["Keitaroh Kobayashi "] -license = "MIT" -edition = "2021" -exclude = ["index.node"] - -[lib] -crate-type = ["cdylib"] - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -neon = "1" - -rustfft = "6.2" -ndarray = "0.16.1" +[workspace] +members = ["crates/audio-snippet-detector"] +resolver = "2" diff --git a/README.md b/README.md index 01edc10..6257fda 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ At this time, the detector code assumes 16-bit 16kHz signed-integer raw audio da ## How to use ```typescript -import { AudioSnippetDetector } from "audio-snippet-detector"; +import { AudioSnippetDetector } from "@kotobamedia/audio-snippet-detector"; const detector = new AudioSnippetDetector(); // add the sounds you want to detect diff --git a/crates/audio-snippet-detector/Cargo.toml b/crates/audio-snippet-detector/Cargo.toml new file mode 100644 index 0000000..7067918 --- /dev/null +++ b/crates/audio-snippet-detector/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "audio-snippet-detector" +version = "0.0.1" +authors = ["Keitaroh Kobayashi "] +license = "MIT" +edition = "2021" +exclude = ["index.node"] + +[lib] +crate-type = ["cdylib"] + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +neon = "1" + +rustfft = "6.2" +ndarray = "0.16.1" diff --git a/src/asd.rs b/crates/audio-snippet-detector/src/asd.rs similarity index 100% rename from src/asd.rs rename to crates/audio-snippet-detector/src/asd.rs diff --git a/src/db.rs b/crates/audio-snippet-detector/src/db.rs similarity index 100% rename from src/db.rs rename to crates/audio-snippet-detector/src/db.rs diff --git a/src/lib.rs b/crates/audio-snippet-detector/src/lib.rs similarity index 100% rename from src/lib.rs rename to crates/audio-snippet-detector/src/lib.rs diff --git a/src/mfcc.rs b/crates/audio-snippet-detector/src/mfcc.rs similarity index 100% rename from src/mfcc.rs rename to crates/audio-snippet-detector/src/mfcc.rs diff --git a/src/util.rs b/crates/audio-snippet-detector/src/util.rs similarity index 100% rename from src/util.rs rename to crates/audio-snippet-detector/src/util.rs diff --git a/index.node.d.ts b/index.node.d.ts deleted file mode 100644 index db5cfc3..0000000 --- a/index.node.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -/// An opaque type representing a native context. -export type Context = unknown; - -export function new_ctx(): Context; -export function db_add(ctx: Context, label: string, data: Uint8Array): undefined; -export type StreamValue = { label: string, score: number }; -export function stream_next(ctx: Context): Promise<{value: StreamValue, done: boolean}>; -export function stream_write(ctx: Context, data: Uint8Array): undefined; -export function stream_close(ctx: Context): undefined; diff --git a/package-lock.json b/package-lock.json index 53fc6bf..b9c63bf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,13 +1,16 @@ { - "name": "audio-snippet-detector", + "name": "@kotobamedia/audio-snippet-detector", "version": "0.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "audio-snippet-detector", + "name": "@kotobamedia/audio-snippet-detector", "version": "0.0.1", "license": "MIT", + "dependencies": { + "@neon-rs/load": "^0.1.82" + }, "devDependencies": { "@neon-rs/cli": "0.1.82", "@tsconfig/node22": "^22.0.0", @@ -605,6 +608,12 @@ "@cargo-messages/win32-x64-msvc": "0.1.81" } }, + "node_modules/@neon-rs/load": { + "version": "0.1.82", + "resolved": "https://registry.npmjs.org/@neon-rs/load/-/load-0.1.82.tgz", + "integrity": "sha512-H4Gu2o5kPp+JOEhRrOQCnJnf7X6sv9FBLttM/wSbb4efsgFWeHzfU/ItZ01E5qqEk+U6QGdeVO7lxXIAtYHr5A==", + "license": "MIT" + }, "node_modules/@tsconfig/node22": { "version": "22.0.0", "resolved": "https://registry.npmjs.org/@tsconfig/node22/-/node22-22.0.0.tgz", diff --git a/package.json b/package.json index b40bd29..a88c5c2 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,8 @@ { - "name": "audio-snippet-detector", + "name": "@kotobamedia/audio-snippet-detector", "version": "0.0.1", "description": "", + "type": "module", "main": "dist/index.js", "files": [ "README.md", @@ -13,20 +14,29 @@ "scripts": { "test": "npm run test:node && npm run test:cargo", "test:cargo": "cargo test", - "test:node": "tsc --noEmit && glob -c 'node --import tsx' './ts/**/*.test.ts'", - "cargo-build": "cargo build --message-format=json-render-diagnostics > cargo.log", - "cross-build": "cross build --message-format=json-render-diagnostics > cross.log", + "test:node": "tsc --noEmit && glob -c 'node --import tsx' './src/**/*.test.ts'", + "tsc-build": "tsc --project tsconfig.dist.json", + "cargo-build": "npm run tsc-build && cargo build --message-format=json-render-diagnostics > cargo.log", + "cross-build": "npm run tsc-build && cross build --message-format=json-render-diagnostics > cross.log", "postcargo-build": "neon dist < cargo.log", "postcross-build": "neon dist -m /target < cross.log", "debug": "npm run cargo-build --", - "build": "npm run build:node && npm run build:cargo", - "build:node": "tsc --project tsconfig.dist.json", - "build:cargo": "npm run cargo-build -- --release", + "build": "npm run cargo-build -- --release", "cross": "npm run cross-build -- --release", - "prepack": "npm run build" + "prepack": "tsc &&neon update", + "version": "neon bump --binaries platforms && git add .", + "release": "gh workflow run release.yml -f dryrun=false -f version=patch", + "dryrun": "gh workflow run publish.yml -f dryrun=true" }, "author": "", "license": "MIT", + "neon": { + "type": "library", + "org": "@kotobamedia", + "prefix": "asd-", + "platforms": ["darwin-arm64", "linux-x64-gnu", "linux-arm64-gnu"], + "load": "./src/load.cts" + }, "devDependencies": { "@neon-rs/cli": "0.1.82", "@tsconfig/node22": "^22.0.0", @@ -34,5 +44,8 @@ "glob": "^11.0.0", "tsx": "^4.19.2", "typescript": "^5.7.2" + }, + "dependencies": { + "@neon-rs/load": "^0.1.82" } } diff --git a/platforms/darwin-arm64/package.json b/platforms/darwin-arm64/package.json new file mode 100644 index 0000000..01ba174 --- /dev/null +++ b/platforms/darwin-arm64/package.json @@ -0,0 +1,25 @@ +{ + "name": "@kotobamedia/asd-darwin-arm64", + "description": "Prebuilt binary package for `audio-snippet-detector` on `darwin-arm64`.", + "version": "0.0.1", + "os": [ + "darwin" + ], + "cpu": [ + "arm64" + ], + "main": "index.node", + "files": [ + "index.node" + ], + "neon": { + "type": "binary", + "rust": "aarch64-apple-darwin", + "node": "darwin-arm64", + "os": "darwin", + "arch": "arm64", + "abi": null + }, + "author": "", + "license": "MIT" +} diff --git a/platforms/linux-arm64-gnu/package.json b/platforms/linux-arm64-gnu/package.json new file mode 100644 index 0000000..ca31ecf --- /dev/null +++ b/platforms/linux-arm64-gnu/package.json @@ -0,0 +1,25 @@ +{ + "name": "@kotobamedia/asd-linux-arm64-gnu", + "description": "Prebuilt binary package for `audio-snippet-detector` on `linux-arm64-gnu`.", + "version": "0.1.0", + "os": [ + "linux" + ], + "cpu": [ + "arm64" + ], + "main": "index.node", + "files": [ + "index.node" + ], + "neon": { + "type": "binary", + "rust": "aarch64-unknown-linux-gnu", + "node": "linux-arm64-gnu", + "os": "linux", + "arch": "arm64", + "abi": "gnu" + }, + "author": "", + "license": "MIT" +} diff --git a/platforms/linux-x64-gnu/package.json b/platforms/linux-x64-gnu/package.json new file mode 100644 index 0000000..4a78a79 --- /dev/null +++ b/platforms/linux-x64-gnu/package.json @@ -0,0 +1,25 @@ +{ + "name": "@kotobamedia/asd-linux-x64-gnu", + "description": "Prebuilt binary package for `audio-snippet-detector` on `linux-x64-gnu`.", + "version": "0.1.0", + "os": [ + "linux" + ], + "cpu": [ + "x64" + ], + "main": "index.node", + "files": [ + "index.node" + ], + "neon": { + "type": "binary", + "rust": "x86_64-unknown-linux-gnu", + "node": "linux-x64-gnu", + "os": "linux", + "arch": "x64", + "abi": "gnu" + }, + "author": "", + "license": "MIT" +} diff --git a/ts/index.ts b/src/index.cts similarity index 56% rename from ts/index.ts rename to src/index.cts index a15546a..6138a88 100644 --- a/ts/index.ts +++ b/src/index.cts @@ -1,5 +1,17 @@ import { Writable } from "node:stream"; -import * as native from "../index.node"; +import * as native from "./load.cjs"; + +declare module "./load.cjs" { + /// An opaque type representing a native context. + type Context = unknown; + + function new_ctx(): Context; + function db_add(ctx: Context, label: string, data: Uint8Array): undefined; + type StreamValue = { label: string, score: number }; + function stream_next(ctx: Context): Promise<{value: StreamValue, done: boolean}>; + function stream_write(ctx: Context, data: Uint8Array): undefined; + function stream_close(ctx: Context): undefined; +} export class AudioSnippetDetector extends Writable { private _ctx: native.Context; diff --git a/src/index.mts b/src/index.mts new file mode 100644 index 0000000..147a587 --- /dev/null +++ b/src/index.mts @@ -0,0 +1 @@ +export * from './index.cjs'; diff --git a/ts/index.test.ts b/src/index.test.ts similarity index 96% rename from ts/index.test.ts rename to src/index.test.ts index 7a64b50..4a36428 100644 --- a/ts/index.test.ts +++ b/src/index.test.ts @@ -3,7 +3,7 @@ import assert from 'node:assert'; import fs from 'node:fs'; import {pipeline} from 'node:stream/promises'; -import {AudioSnippetDetector} from './index'; +import {AudioSnippetDetector} from './index.mjs'; interface AsyncIterableItem { [Symbol.asyncIterator](): AsyncIterator; diff --git a/src/load.cts b/src/load.cts new file mode 100644 index 0000000..707040a --- /dev/null +++ b/src/load.cts @@ -0,0 +1,16 @@ +// This module loads the platform-specific build of the addon on +// the current system. The supported platforms are registered in +// the `platforms` object below, whose entries can be managed by +// by the Neon CLI: +// +// https://www.npmjs.com/package/@neon-rs/cli + +module.exports = require('@neon-rs/load').proxy({ + platforms: { + 'darwin-x64': () => require('@nrc/darwin-x64'), + 'darwin-arm64': () => require('@nrc/darwin-arm64'), + 'linux-x64-gnu': () => require('@nrc/linux-x64-gnu'), + 'linux-arm64-gnu': () => require('@nrc/linux-arm64-gnu') + }, + debug: () => require('../index.node') +}); diff --git a/tsconfig.dist.json b/tsconfig.dist.json index d137818..5b093c3 100644 --- a/tsconfig.dist.json +++ b/tsconfig.dist.json @@ -5,6 +5,6 @@ "declarationMap": true, "sourceMap": true }, - "include": ["ts/**/*.ts"], - "exclude": ["node_modules", "dist", "ts/**/*.test.ts"] + "include": ["src/**/*.ts", "src/**/*.mts", "src/**/*.cts"], + "exclude": ["node_modules", "dist", "src/**/*.test.ts"] } diff --git a/tsconfig.json b/tsconfig.json index 919760f..b58997e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,6 +3,6 @@ "compilerOptions": { "lib": ["ESNext"], "outDir": "./dist", - "rootDir": "./ts" + "rootDir": "./src" } }