Skip to content

Commit d084248

Browse files
committed
Migrate to ESM
1 parent 1d24f37 commit d084248

37 files changed

+5075
-2585
lines changed

.github/workflows/test.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ name: Node.js
22

33
on:
44
push:
5+
workflow_dispatch:
6+
7+
permissions:
8+
contents: write
9+
packages: write
10+
id-token: write
511

612
jobs:
713
test-node:
@@ -16,6 +22,9 @@ jobs:
1622
uses: actions/setup-node@v4
1723
with:
1824
node-version: 'lts/*'
25+
cache: yarn
26+
cache-dependency-path: "node"
27+
registry-url: 'https://registry.npmjs.org/'
1928

2029
- name: Install dependencies
2130
run: yarn install --frozen-lockfile
@@ -25,6 +34,13 @@ jobs:
2534
run: yarn test
2635
working-directory: ./node
2736

37+
- name: npm publish
38+
if: startsWith(github.ref, 'refs/tags/')
39+
env:
40+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
41+
run: npm publish --provenance --access public
42+
working-directory: ./node
43+
2844
test-wasm:
2945
name: Run Yespower Tests (WASM)
3046
runs-on: ubuntu-latest
@@ -37,6 +53,9 @@ jobs:
3753
uses: actions/setup-node@v4
3854
with:
3955
node-version: 'lts/*'
56+
cache: yarn
57+
cache-dependency-path: "wasm"
58+
registry-url: 'https://registry.npmjs.org/'
4059

4160
- name: Install dependencies
4261
run: yarn install --frozen-lockfile
@@ -45,3 +64,10 @@ jobs:
4564
- name: Run tests
4665
run: yarn test
4766
working-directory: ./wasm
67+
68+
- name: npm publish
69+
if: startsWith(github.ref, 'refs/tags/')
70+
env:
71+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
72+
run: npm publish --provenance --access public
73+
working-directory: ./wasm

node/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
node_modules/
22
build/
3+
4+
# coverage
5+
coverage

node/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ $ yarn
2222
## Example Code
2323

2424
```js
25-
const { yespower } = require('yespower');
25+
import { yespower } from 'yespower';
2626

2727
const data = new Buffer("7000000001e980924e4e1109230383e66d62945ff8e749903bea4336755c00000000000051928aff1b4d72416173a8c3948159a09a73ac3bb556aa6bfbcad1a85da7f4c1d13350531e24031b939b9e2b", "hex");
2828
console.log(yespower(data).toString('hex'));

node/eslint.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import tseslint from 'typescript-eslint';
2+
import { getConfig } from '@cpuchain/eslint';
3+
4+
export default tseslint.config(getConfig());

node/index.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./build/Release/yespower.node');

node/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
module.exports = require('bindings')('yespower');
1+
import { createRequire } from 'node:module';
2+
const require = createRequire(import.meta.url);
3+
const addon = require('./build/Release/yespower.node');
4+
5+
export const yespower = addon.yespower;

node/package.json

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,46 @@
11
{
22
"name": "yespower",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "Node.js bindings of the Yespower hashing algorithm",
55
"license": "BSD 2-Clause",
66
"author": "Alexander Peslyak",
7-
"main": "index.js",
8-
"types": "index.d.ts",
7+
"type": "module",
8+
"main": "./index.cjs",
9+
"module": "./index.js",
10+
"types": "./index.d.ts",
11+
"exports": {
12+
".": {
13+
"import": "./index.js",
14+
"require": "./index.cjs",
15+
"default": "./index.js"
16+
}
17+
},
918
"gypfile": true,
19+
"keywords": [
20+
"yespower",
21+
"proof-of-work",
22+
"hashing",
23+
"napi",
24+
"node-addon-api"
25+
],
1026
"repository": {
1127
"type": "git",
1228
"url": "git+https://github.com/cpuchain/yespower-js.git"
1329
},
1430
"scripts": {
31+
"lint": "eslint scripts/**/*.ts test/**/*.ts",
1532
"bench": "node scripts/bench.js",
16-
"test": "node --test test/index.js",
33+
"test": "tsx --test",
1734
"install": "node-gyp rebuild"
1835
},
1936
"dependencies": {
20-
"bindings": "*",
2137
"node-addon-api": "^8.3.1"
38+
},
39+
"devDependencies": {
40+
"@cpuchain/eslint": "^1.0.9",
41+
"@types/node": "^22.16.0",
42+
"tsc": "^2.0.4",
43+
"tsx": "^4.20.3",
44+
"typescript": "^5.8.3"
2245
}
2346
}

node/scripts/bench.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { yespower } = require('../index');
1+
import { yespower } from '../index.js';
22

33
const tests = 30;
44

@@ -9,11 +9,11 @@ const timeStart = Date.now();
99

1010
for (let i = 0; i < tests; ++i) {
1111
const buf = Buffer.allocUnsafe(4);
12-
buf.writeUint32BE(i)
12+
buf.writeUint32BE(i);
1313
console.log(yespower(buf, N, r).toString('hex'));
1414
}
1515

1616
const timeTook = Date.now() - timeStart;
17-
const hps = Math.floor(1000 * tests / timeTook);
17+
const hps = Math.floor((1000 * tests) / timeTook);
1818

1919
console.log(`${tests} Tests: ${Date.now() - timeStart}ms (${hps}H/s)`);

node/scripts/case.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

node/scripts/case.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { webcrypto as crypto } from 'crypto';
2+
import { yespower } from '../index.js';
3+
4+
interface TestCases {
5+
input: string;
6+
output: string;
7+
N?: number;
8+
r?: number;
9+
pers?: string;
10+
}
11+
12+
function getRandomBuffer() {
13+
return Buffer.from(crypto.getRandomValues(new Uint8Array(32)));
14+
}
15+
16+
const pers = 'pers';
17+
18+
const num = 5;
19+
20+
function createCases() {
21+
const cases: TestCases[] = [];
22+
23+
for (let i = 0; i < num; ++i) {
24+
const input = getRandomBuffer();
25+
const output = yespower(input, undefined, undefined, pers);
26+
27+
cases.push({
28+
input: input.toString('hex'),
29+
output: output.toString('hex'),
30+
pers,
31+
});
32+
}
33+
34+
console.log(cases);
35+
}
36+
37+
createCases();

0 commit comments

Comments
 (0)