initial neon

This commit is contained in:
Caelan Sayler
2024-08-08 19:09:39 +01:00
committed by Caelan
parent 48946fd147
commit e66560c676
23 changed files with 776 additions and 0 deletions

5
src/lib-node/.github/.env vendored Normal file
View File

@@ -0,0 +1,5 @@
NODE_VERSION=20.x
NPM_REGISTRY=https://registry.npmjs.org
RUST_VERSION=stable
ACTIONS_USER=github-actions
ACTIONS_EMAIL=github-actions@github.com

View File

@@ -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@v3
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-rs/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@v2
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

137
src/lib-node/.github/workflows/build.yml vendored Normal file
View File

@@ -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@v3
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@v3
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@v3
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 }}

View File

@@ -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@v3
- 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@v3
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

71
src/lib-node/.github/workflows/test.yml vendored Normal file
View File

@@ -0,0 +1,71 @@
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
unit-tests:
name: Unit Tests
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
- 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

8
src/lib-node/.gitignore vendored Normal file
View File

@@ -0,0 +1,8 @@
target
index.node
**/node_modules
**/.DS_Store
npm-debug.log*
lib
cargo.log
cross.log

3
src/lib-node/Cargo.toml Normal file
View File

@@ -0,0 +1,3 @@
[workspace]
members = ["crates/veloz"]
resolver = "2"

104
src/lib-node/README.md Normal file
View File

@@ -0,0 +1,104 @@
# veloz
This project was bootstrapped by [create-neon](https://www.npmjs.com/package/create-neon).
## Building veloz
Building veloz requires a [supported version of Node and Rust](https://github.com/neon-bindings/neon#platform-support).
To run the build, run:
```sh
$ npm run build
```
This command uses the [@neon-rs/cli](https://www.npmjs.com/package/@neon-rs/cli) utility to assemble the binary Node addon from the output of `cargo`.
## Exploring veloz
After building veloz, you can explore its exports at the Node console:
```sh
$ npm i
$ npm run build
$ node
> require('.').greeting()
{ message: 'hello node' }
```
## Available Scripts
In the project directory, you can run:
#### `npm run build`
Builds the Node addon (`index.node`) from source, generating a release build with `cargo --release`.
Additional [`cargo build`](https://doc.rust-lang.org/cargo/commands/cargo-build.html) arguments may be passed to `npm run build` and similar commands. For example, to enable a [cargo feature](https://doc.rust-lang.org/cargo/reference/features.html):
```
npm run build -- --feature=beetle
```
#### `npm run debug`
Similar to `npm run build` but generates a debug build with `cargo`.
#### `npm run cross`
Similar to `npm run build` but uses [cross-rs](https://github.com/cross-rs/cross) to cross-compile for another platform. Use the [`CARGO_BUILD_TARGET`](https://doc.rust-lang.org/cargo/reference/config.html#buildtarget) environment variable to select the build target.
#### `npm run release`
Initiate a full build and publication of a new patch release of this library via GitHub Actions.
#### `npm run dryrun`
Initiate a dry run of a patch release of this library via GitHub Actions. This performs a full build but does not publish the final result.
#### `npm test`
Runs the unit tests by calling `cargo test`. You can learn more about [adding tests to your Rust code](https://doc.rust-lang.org/book/ch11-01-writing-tests.html) from the [Rust book](https://doc.rust-lang.org/book/).
## Project Layout
The directory structure of this project is:
```
veloz/
├── Cargo.toml
├── README.md
├── lib/
├── src/
| ├── index.mts
| └── index.cts
├── crates/
| └── veloz/
| └── src/
| └── lib.rs
├── platforms/
├── package.json
└── target/
```
| Entry | Purpose |
|----------------|------------------------------------------------------------------------------------------------------------------------------------------|
| `Cargo.toml` | The Cargo [manifest file](https://doc.rust-lang.org/cargo/reference/manifest.html), which informs the `cargo` command. |
| `README.md` | This file. |
| `lib/` | The directory containing the generated output from [tsc](https://typescriptlang.org). |
| `src/` | The directory containing the TypeScript source files. |
| `index.mts` | Entry point for when this library is loaded via [ESM `import`](https://nodejs.org/api/esm.html#modules-ecmascript-modules) syntax. |
| `index.cts` | Entry point for when this library is loaded via [CJS `require`](https://nodejs.org/api/modules.html#requireid). |
| `crates/` | The directory tree containing the Rust source code for the project. |
| `lib.rs` | Entry point for the Rust source code. |
| `platforms/` | The directory containing distributions of the binary addon backend for each platform supported by this library. |
| `package.json` | The npm [manifest file](https://docs.npmjs.com/cli/v7/configuring-npm/package-json), which informs the `npm` command. |
| `target/` | Binary artifacts generated by the Rust build. |
## Learn More
Learn more about:
- [Neon](https://neon-bindings.com).
- [Rust](https://www.rust-lang.org).
- [Node](https://nodejs.org).

View File

@@ -0,0 +1,14 @@
[package]
name = "veloz"
version = "0.1.0"
license = "ISC"
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"

View File

@@ -0,0 +1,11 @@
use neon::prelude::*;
fn hello(mut cx: FunctionContext) -> JsResult<JsString> {
Ok(cx.string("hello node"))
}
#[neon::main]
fn main(mut cx: ModuleContext) -> NeonResult<()> {
cx.export_function("hello", hello)?;
Ok(())
}

53
src/lib-node/package.json Normal file
View File

@@ -0,0 +1,53 @@
{
"name": "veloz",
"version": "0.1.0",
"description": "",
"main": "./lib/index.cjs",
"scripts": {
"test": "tsc &&cargo test",
"cargo-build": "tsc &&cargo build --message-format=json > cargo.log",
"cross-build": "tsc &&cross build --message-format=json > cross.log",
"postcargo-build": "neon dist < cargo.log",
"postcross-build": "neon dist -m /target < cross.log",
"debug": "npm run cargo-build --",
"build": "npm run cargo-build -- --release",
"cross": "npm run cross-build -- --release",
"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": "ISC",
"exports": {
".": {
"import": {
"types": "./lib/index.d.mts",
"default": "./lib/index.mjs"
},
"require": {
"types": "./lib/index.d.cts",
"default": "./lib/index.cjs"
}
}
},
"types": "./lib/index.d.cts",
"files": [
"lib/**/*.?({c,m}){t,j}s"
],
"neon": {
"type": "library",
"org": "@velopack",
"platforms": "common",
"load": "./src/load.cts"
},
"devDependencies": {
"@neon-rs/cli": "^0.1.73",
"@tsconfig/node20": "^20.1.4",
"@types/node": "^20.11.16",
"typescript": "^5.3.3"
},
"dependencies": {
"@neon-rs/load": "^0.1.73"
}
}

View File

@@ -0,0 +1,3 @@
# `@velopack/darwin-arm64`
Prebuilt binary package for `veloz` on `darwin-arm64`.

View File

@@ -0,0 +1,25 @@
{
"name": "@velopack/darwin-arm64",
"description": "Prebuilt binary package for `veloz` on `darwin-arm64`.",
"version": "0.1.0",
"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": "ISC"
}

View File

@@ -0,0 +1,3 @@
# `@velopack/darwin-x64`
Prebuilt binary package for `veloz` on `darwin-x64`.

View File

@@ -0,0 +1,25 @@
{
"name": "@velopack/darwin-x64",
"description": "Prebuilt binary package for `veloz` on `darwin-x64`.",
"version": "0.1.0",
"os": [
"darwin"
],
"cpu": [
"x64"
],
"main": "index.node",
"files": [
"index.node"
],
"neon": {
"type": "binary",
"rust": "x86_64-apple-darwin",
"node": "darwin-x64",
"os": "darwin",
"arch": "x64",
"abi": null
},
"author": "",
"license": "ISC"
}

View File

@@ -0,0 +1,3 @@
# `@velopack/linux-x64-gnu`
Prebuilt binary package for `veloz` on `linux-x64-gnu`.

View File

@@ -0,0 +1,25 @@
{
"name": "@velopack/linux-x64-gnu",
"description": "Prebuilt binary package for `veloz` 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": "ISC"
}

View File

@@ -0,0 +1,3 @@
# `@velopack/win32-x64-msvc`
Prebuilt binary package for `veloz` on `win32-x64-msvc`.

View File

@@ -0,0 +1,25 @@
{
"name": "@velopack/win32-x64-msvc",
"description": "Prebuilt binary package for `veloz` on `win32-x64-msvc`.",
"version": "0.1.0",
"os": [
"win32"
],
"cpu": [
"x64"
],
"main": "index.node",
"files": [
"index.node"
],
"neon": {
"type": "binary",
"rust": "x86_64-pc-windows-msvc",
"node": "win32-x64-msvc",
"os": "win32",
"arch": "x64",
"abi": "msvc"
},
"author": "",
"license": "ISC"
}

View File

@@ -0,0 +1,19 @@
// This module is the CJS entry point for the library.
// The Rust addon.
import * as addon from './load.cjs';
// Use this declaration to assign types to the addon's exports,
// which otherwise by default are `any`.
declare module "./load.cjs" {
function hello(): string;
}
export type Greeting = {
message: string
};
export function greeting(): Greeting {
const message = addon.hello();
return { message };
}

View File

@@ -0,0 +1,3 @@
// This module is the ESM entry point for the library.
export * from './index.cjs';

16
src/lib-node/src/load.cts Normal file
View File

@@ -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: {
'win32-x64-msvc': () => require('@velopack/win32-x64-msvc'),
'darwin-x64': () => require('@velopack/darwin-x64'),
'darwin-arm64': () => require('@velopack/darwin-arm64'),
'linux-x64-gnu': () => require('@velopack/linux-x64-gnu')
},
debug: () => require('../index.node')
});

View File

@@ -0,0 +1,9 @@
{
"extends": "@tsconfig/node20/tsconfig.json",
"compilerOptions": {
"module": "node16",
"declaration": true,
"outDir": "lib",
},
"exclude": ["lib"]
}