Skip to content

Commit

Permalink
popm/wasm: add new @hemilabs/pop-miner NPM package (hemilabs#162)
Browse files Browse the repository at this point in the history
Add the initial implementation of the `@hemilabs/pop-miner` NPM package,
a TypeScript API layer for the WebAssembly PoP Miner.

Also adds supporting files, GitHub Actions workflow changes to build and
publish the NPM package.

This is the initial version of this package, more functionality will be
added in future pull requests.

Reviewed-on: hemilabs#162
  • Loading branch information
joshuasing authored Jul 2, 2024
1 parent ecef76a commit b9b673a
Show file tree
Hide file tree
Showing 22 changed files with 9,292 additions and 11 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,3 @@ jobs:
run: |
make
git diff --exit-code
- name: "make web popm"
if: (success() || failure()) && steps.deps.outcome == 'success'
run: |
cd web && make
72 changes: 72 additions & 0 deletions .github/workflows/node.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Copyright (c) 2024 Hemi Labs, Inc.
# Use of this source code is governed by the MIT License,
# which can be found in the LICENSE file.

# GitHub Actions workflow to lint, build and test NPM packages.
name: "Node"
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
workflow_dispatch:
workflow_call:

concurrency:
group: "node-${{ github.workflow }}-${{ github.event.number || github.ref }}"
cancel-in-progress: "${{ github.event_name == 'pull_request' }}"

env:
GO_VERSION: "1.22.x"
PNPM_VERSION: "9.4.x"

jobs:
build:
name: "Build"
runs-on: "ubuntu-latest"
permissions:
contents: read
steps:
- name: "Checkout repository"
uses: actions/checkout@v4

- name: "Setup Go ${{ env.GO_VERSION }}"
uses: actions/setup-go@v5
with:
go-version: "${{ env.GO_VERSION }}"
cache: true
check-latest: true

- name: "Setup pnpm ${{ env.PNPM_VERSION }}"
uses: pnpm/action-setup@v4
with:
version: "${{ env.PNPM_VERSION }}"

- name: "Setup Node"
uses: actions/setup-node@v4
with:
node-version-file: "web/.nvmrc"
check-latest: true
cache: "pnpm"
cache-dependency-path: "web/**/pnpm-lock.yaml"

- name: "Install dependencies"
working-directory: "web/"
run: pnpm install --frozen-lockfile

- name: "Install Go dependencies"
working-directory: "web/"
run: make deps

# TODO(joshuasing): Install and use binaryen
- name: "Build @hemilabs/pop-miner WebAssembly binary"
working-directory: "web/"
run: make wasm

- name: "Build @hemilabs/pop-miner package"
working-directory: "web/"
run: pnpm build:pop-miner

- name: "Lint"
working-directory: "web/"
run: pnpm lint
78 changes: 77 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ on:
type: boolean
required: true
default: false
npm:
description: "Publish NPM packages"
type: boolean
required: true
default: false
release:
description: "Create GitHub release and publish Docker images"
type: boolean
Expand All @@ -31,9 +36,11 @@ concurrency:

env:
GO_VERSION: "1.22.x"
PNPM_VERSION: "9.4.x"

jobs:
# Run tests
# TODO(joshuasing): run PoP Miner / NPM package tests once added
test:
name: "Test"
uses: ./.github/workflows/go.yml
Expand Down Expand Up @@ -181,11 +188,80 @@ jobs:
ghcr.io/hemilabs/${{ matrix.service }}:latest
ghcr.io/hemilabs/${{ matrix.service }}:${{ needs.prepare.outputs.tag }}
# Publish NPM packages
npm:
name: "npm"
runs-on: "ubuntu-latest"
if: github.event_name == 'push' || inputs.npm
needs: [ "prepare" ]
permissions:
contents: read
steps:
- name: "Checkout repository"
uses: actions/checkout@v4

- name: "Setup Go ${{ env.GO_VERSION }}"
uses: actions/setup-go@v5
with:
go-version: "${{ env.GO_VERSION }}"
cache: true
check-latest: true

- name: "Setup pnpm ${{ env.PNPM_VERSION }}"
uses: pnpm/action-setup@v4
with:
version: "${{ env.PNPM_VERSION }}"

- name: "Setup Node"
uses: actions/setup-node@v4
with:
node-version-file: "web/.nvmrc"
check-latest: true
cache: "pnpm"
cache-dependency-path: "web/**/pnpm-lock.yaml"

- name: "Install dependencies"
working-directory: "web/"
run: pnpm install --frozen-lockfile

- name: "Install Go dependencies"
working-directory: "web/"
run: make deps

- name: "Set package.json versions"
working-directory: "web/"
env:
VERSION: "${{ needs.prepare.outputs.version }}"
run: |
# Prints all package.json files
PACKAGE_FILES=$(find . -path '**/node_modules' -prune -o -name 'package.json' -print)
for file in $PACKAGE_FILES; do
# Set "version" in package.json file to $VERSION.
TMP_FILE="$(mktemp)"
jq --arg v "$VERSION" '.version = $v' "$file" > "$TMP_FILE"
mv "$TMP_FILE" "$file"
done
# TODO(joshuasing): Install and use binaryen
- name: "Build @hemilabs/pop-miner WebAssembly binary"
working-directory: "web/"
run: make wasm

- name: "Build @hemilabs/pop-miner package"
working-directory: "web/"
run: pnpm build:pop-miner

- name: "Publish @hemilabs/pop-miner package"
working-directory: "web/"
run: pnpm publish packages/pop-miner --access public --no-git-checks
env:
NODE_AUTH_TOKEN: "${{ secrets.NPM_TOKEN }}"

# Create GitHub Release
release:
name: "Release"
runs-on: "ubuntu-latest"
needs: [ "prepare", "build", "docker" ]
needs: [ "prepare", "build", "docker", "npm" ]
permissions:
# Permission to write contents is required to create GitHub releases.
# Builds are performed in a separate job with more restrictive permissions
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ lint:
$(shell go env GOPATH)/bin/goimports -local github.com/hemilabs/heminetwork -w -l .
$(shell go env GOPATH)/bin/gofumpt -w -l .
$(shell go env GOPATH)/bin/addlicense -c "Hemi Labs, Inc." -f $(PROJECTPATH)/license_header.txt \
-ignore "{.idea,.vscode}/**" -ignore ".github/release.yml" -ignore ".github/ISSUE_TEMPLATE/**" .
-ignore "{.idea,.vscode}/**" -ignore ".github/release.yml" -ignore ".github/ISSUE_TEMPLATE/**" \
-ignore "**/pnpm-{lock,workspace}.yaml" -ignore "**/node_modules/**" .
go vet ./...

lint-deps:
Expand Down
5 changes: 4 additions & 1 deletion web/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
webapp
webapp/
node_modules/
dist/
packages/pop-miner/wasm/
1 change: 1 addition & 0 deletions web/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20.15.0
5 changes: 5 additions & 0 deletions web/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
**/dist
pnpm-lock.yaml

www/
webapp/
7 changes: 7 additions & 0 deletions web/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"semi": true,
"singleQuote": true,
"trailingComma": "all",
"tabWidth": 2,
"printWidth": 80
}
10 changes: 8 additions & 2 deletions web/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,24 @@ PROJECTPATH = $(abspath $(dir $(realpath $(firstword $(MAKEFILE_LIST)))))
GOROOT=$(shell go env GOROOT)
WEBAPP=$(PROJECTPATH)/webapp
WWW_DIR=$(PROJECTPATH)/www
WASM_BINARY=$(WEBAPP)/popminer.wasm
POP_MINER_PACKAGE_DIR=$(PROJECTPATH)/packages/pop-miner
WASM_BINARY=$(POP_MINER_PACKAGE_DIR)/wasm/popminer.wasm

version = $(patsubst v%,%,$(shell git describe --tags 2>/dev/null || echo "v0.0.0"))
commit = $(shell git rev-parse --short HEAD)

.PHONY: all clean wasm wasm-opt www
.PHONY: all clean deps wasm wasm-opt www

all: wasm www

clean:
rm -rf ${WEBAPP}
rm -rf ${WASM_BINARY}

deps:
go mod download
go mod verify

wasm:
CGO_ENABLED=0 GOOS=js GOARCH=wasm go build -trimpath -tags "$(BUILD_TAGS)" \
-ldflags "-s -w -X main.version=${version} -X main.gitCommit=${commit}" \
Expand All @@ -35,6 +40,7 @@ wasm-opt:

www: wasm
mkdir -p ${WEBAPP}
cp ${WASM_BINARY} ${WEBAPP}
cp ${WWW_DIR}/index.html ${WEBAPP}
cp ${WWW_DIR}/index.js ${WEBAPP}
cp ${WWW_DIR}/popminer.js ${WEBAPP}
Expand Down
28 changes: 28 additions & 0 deletions web/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (c) 2024 Hemi Labs, Inc.
* Use of this source code is governed by the MIT License,
* which can be found in the LICENSE file.
*/

import globals from 'globals';
import pluginJs from '@eslint/js';
import tseslint from 'typescript-eslint';
import pluginTSDoc from 'eslint-plugin-tsdoc';

export default [
{
languageOptions: {
globals: {
...globals.browser,
...globals.node,
},
},
},
pluginJs.configs.recommended,
...tseslint.configs.recommended,
{
plugins: {
pluginTSDoc,
},
},
];
40 changes: 40 additions & 0 deletions web/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "@hemilabs/heminetwork",
"version": "0.0.0",
"private": true,
"license": "MIT",
"bugs": "https://github.com/hemilabs/heminetwork/issues",
"homepage": "https://github.com/hemilabs/heminetwork",
"repository": {
"type": "git",
"url": "https://github.com/hemilabs/heminetwork.git",
"directory": "web"
},
"scripts": {
"preinstall": "npx only-allow pnpm",
"build": "make wasm",
"build:opt": "make wasm-opt",
"build:pop-miner": "pnpm --filter @hemilabs/pop-miner run build",
"lint": "eslint \"packages/**/src/**/*.ts\" && prettier . --check",
"format": "prettier -w ."
},
"packageManager": "pnpm@9.4.0",
"workspaces": [
"packages/*"
],
"engines": {
"node": "^18.0.0 || >=20.0.0"
},
"devDependencies": {
"@eslint/js": "9.5.0",
"eslint": "9.5.0",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-prettier": "5.1.3",
"eslint-plugin-tsdoc": "0.3.0",
"globals": "15.6.0",
"prettier": "3.3.2",
"typescript": "5.5.2",
"typescript-eslint": "7.14.1",
"vitest": "1.6.0"
}
}
43 changes: 43 additions & 0 deletions web/packages/pop-miner/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "@hemilabs/pop-miner",
"version": "0.1.0",
"type": "module",
"license": "MIT",
"author": "Hemi Labs",
"description": "Hemi Labs WebAssembly Proof-of-Proof Miner",
"repository": {
"type": "git",
"url": "https://github.com/hemilabs/heminetwork.git",
"directory": "web/packages/pop-miner"
},
"homepage": "https://hemi.xyz",
"bugs": {
"url": "https://github.com/hemilabs/heminetwork/issues"
},
"engines": {
"node": "^18.0.0 || >=20.0.0"
},
"scripts": {
"build": "tsup"
},
"main": "./dist/browser/index.js",
"types": "./dist/types.d.ts",
"files": [
"dist"
],
"exports": {
".": {
"types": "./dist/browser/index.d.ts",
"browser": "./dist/browser/index.js",
"import": "./dist/browser/index.js",
"default": "./dist/browser/index.js"
},
"./popminer.wasm": "./dist/popminer.wasm",
"./types": "./dist/types.d.ts",
"./package.json": "./package.json"
},
"devDependencies": {
"tsup": "8.0.2",
"typescript": "5.4.5"
}
}
Loading

0 comments on commit b9b673a

Please sign in to comment.