Skip to content

Commit a2981f8

Browse files
authored
feat: support cjs and esm both by tshy (#8)
BREAKING CHANGE: drop Node.js < 18.19.0 support part of eggjs/egg#3644 eggjs/egg#5257
1 parent b86655c commit a2981f8

15 files changed

+261
-133
lines changed

.eslintrc

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": [
3+
"eslint-config-egg/typescript",
4+
"eslint-config-egg/lib/rules/enforce-node-prefix"
5+
]
6+
}

.github/workflows/nodejs.yml

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ name: CI
33
on:
44
push:
55
branches: [ master ]
6-
76
pull_request:
87
branches: [ master ]
98

10-
workflow_dispatch: {}
11-
129
jobs:
1310
Job:
1411
name: Node.js
15-
uses: artusjs/github-actions/.github/workflows/node-test.yml@v1
12+
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
1613
with:
17-
os: 'ubuntu-latest'
18-
version: '8, 10, 12, 14, 16, 18, 19'
14+
os: 'ubuntu-latest, macos-latest, windows-latest'
15+
version: '18.19.0, 18, 20, 22'
16+
secrets:
17+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

.github/workflows/pkg.pr.new.yml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Publish Any Commit
2+
on: [push, pull_request]
3+
4+
jobs:
5+
build:
6+
runs-on: ubuntu-latest
7+
8+
steps:
9+
- name: Checkout code
10+
uses: actions/checkout@v4
11+
12+
- run: corepack enable
13+
- uses: actions/setup-node@v4
14+
with:
15+
node-version: 20
16+
17+
- name: Install dependencies
18+
run: npm install
19+
20+
- name: Build
21+
run: npm run prepublishOnly --if-present
22+
23+
- run: npx pkg-pr-new publish

.github/workflows/release.yml

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
name: Release
22

33
on:
4-
# 合并后自动发布
54
push:
65
branches: [ master ]
76

8-
# 手动发布
9-
workflow_dispatch: {}
10-
117
jobs:
128
release:
139
name: Node.js
14-
uses: artusjs/github-actions/.github/workflows/node-release.yml@v1
10+
uses: node-modules/github-actions/.github/workflows/node-release.yml@master
1511
secrets:
1612
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
1713
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}
18-
with:
19-
checkTest: false

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@ results
1212

1313
node_modules
1414
npm-debug.log
15-
coverage/
1615
.vscode
16+
.tshy*
17+
.eslintcache
18+
dist
19+
coverage

History.md CHANGELOG.md

File renamed without changes.

README.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
cluster-reload
2-
=======
1+
# cluster-reload
32

43
[![NPM version][npm-image]][npm-url]
54
[![npm download][download-image]][download-url]
65
[![Node.js CI](https://github.com/node-modules/cluster-reload/actions/workflows/nodejs.yml/badge.svg)](https://github.com/node-modules/cluster-reload/actions/workflows/nodejs.yml)
76
[![Test coverage][codecov-image]][codecov-url]
7+
[![Node.js Version](https://img.shields.io/node/v/cluster-reload.svg?style=flat)](https://nodejs.org/en/download/)
88

99
[npm-image]: https://img.shields.io/npm/v/cluster-reload.svg?style=flat-square
1010
[npm-url]: https://npmjs.org/package/cluster-reload
@@ -18,9 +18,15 @@ Easy and safe reload your workers.
1818
## Installation
1919

2020
```bash
21-
npm install cluster-reload --save
21+
npm install cluster-reload
2222
```
2323

2424
## License
2525

2626
[MIT](LICENSE)
27+
28+
## Contributors
29+
30+
[![Contributors](https://contrib.rocks/image?repo=node-modules/cluster-reload)](https://github.com/node-modules/cluster-reload/graphs/contributors)
31+
32+
Made with [contributors-img](https://contrib.rocks).

package.json

+52-17
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,6 @@
22
"name": "cluster-reload",
33
"version": "1.1.0",
44
"description": "cluster workers reload",
5-
"main": "index.js",
6-
"files": [
7-
"index.js"
8-
],
9-
"scripts": {
10-
"lint": "echo 'ignore'",
11-
"test": "egg-bin test",
12-
"ci": "egg-bin cov"
13-
},
14-
"dependencies": {},
15-
"devDependencies": {
16-
"egg-bin": "^4.20.0",
17-
"urllib": "^2.40.0"
18-
},
195
"homepage": "https://github.com/node-modules/cluster-reload",
206
"repository": {
217
"type": "git",
@@ -27,9 +13,58 @@
2713
"keywords": [
2814
"cluster-reload"
2915
],
16+
"author": "fengmk2 <[email protected]> (https://github.com/fengmk2)",
17+
"license": "MIT",
3018
"engines": {
31-
"node": ">=8.0.0"
19+
"node": ">= 18.19.0"
3220
},
33-
"author": "fengmk2 <[email protected]> (https://github.com/fengmk2)",
34-
"license": "MIT"
21+
"dependencies": {},
22+
"devDependencies": {
23+
"@arethetypeswrong/cli": "^0.17.1",
24+
"@eggjs/tsconfig": "1",
25+
"@types/mocha": "10",
26+
"@types/node": "22",
27+
"egg-bin": "6",
28+
"eslint": "8",
29+
"eslint-config-egg": "14",
30+
"tshy": "3",
31+
"tshy-after": "1",
32+
"typescript": "5",
33+
"urllib": "^4.6.8"
34+
},
35+
"scripts": {
36+
"lint": "eslint --cache src test --ext .ts",
37+
"pretest": "npm run lint -- --fix && npm run prepublishOnly",
38+
"test": "egg-bin test",
39+
"preci": "npm run lint && npm run prepublishOnly",
40+
"ci": "egg-bin cov && attw --pack",
41+
"prepublishOnly": "tshy && tshy-after"
42+
},
43+
"type": "module",
44+
"tshy": {
45+
"exports": {
46+
".": "./src/index.ts",
47+
"./package.json": "./package.json"
48+
}
49+
},
50+
"exports": {
51+
".": {
52+
"import": {
53+
"types": "./dist/esm/index.d.ts",
54+
"default": "./dist/esm/index.js"
55+
},
56+
"require": {
57+
"types": "./dist/commonjs/index.d.ts",
58+
"default": "./dist/commonjs/index.js"
59+
}
60+
},
61+
"./package.json": "./package.json"
62+
},
63+
"files": [
64+
"dist",
65+
"src"
66+
],
67+
"types": "./dist/commonjs/index.d.ts",
68+
"main": "./dist/commonjs/index.js",
69+
"module": "./dist/esm/index.js"
3570
}

index.js src/index.ts

+16-17
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
1-
const cluster = require('cluster');
2-
3-
module.exports = reload;
1+
import cluster, { type Worker } from 'node:cluster';
2+
import { cpus } from 'node:os';
43

54
// Windows not support SIGQUIT https://nodejs.org/api/process.html#process_signal_events
65
const KILL_SIGNAL = 'SIGTERM';
76
let reloading = false;
8-
let reloadPedding = false;
9-
function reload(count) {
7+
let reloadPadding = false;
8+
9+
export function reload(count?: number) {
1010
if (reloading) {
11-
reloadPedding = true;
11+
reloadPadding = true;
1212
return;
1313
}
1414
if (!count) {
15-
count = require('os').cpus().length;
15+
count = cpus().length;
1616
}
1717
reloading = true;
1818
// find out all alive workers
1919
const aliveWorkers = [];
20-
let worker;
2120
for (const id in cluster.workers) {
22-
worker = cluster.workers[id];
23-
if (worker.state === 'disconnected') {
21+
const worker = cluster.workers[id]!;
22+
const state = Reflect.get(worker, 'state');
23+
if (state === 'disconnected') {
2424
continue;
2525
}
2626
aliveWorkers.push(worker);
2727
}
2828

29-
let firstWorker;
30-
let newWorker;
29+
let firstWorker: Worker;
30+
let newWorker: Worker;
3131

3232
function reset() {
3333
// don't leak
3434
newWorker.removeListener('listening', reset);
35-
newWorker.removeListener('error', reset);
35+
newWorker.removeListener('exit', reset);
3636

3737
if (firstWorker) {
3838
// console.log('firstWorker %s %s', firstWorker.id, firstWorker.state);
@@ -42,9 +42,9 @@ function reload(count) {
4242
}, 100);
4343
}
4444
reloading = false;
45-
if (reloadPedding) {
45+
if (reloadPadding) {
4646
// has reload jobs, reload again
47-
reloadPedding = false;
47+
reloadPadding = false;
4848
reload(count);
4949
}
5050
}
@@ -54,8 +54,7 @@ function reload(count) {
5454
newWorker.on('listening', reset).on('exit', reset);
5555

5656
// kill other workers
57-
for (let i = 1; i < aliveWorkers.length; i++) {
58-
worker = aliveWorkers[i];
57+
for (const worker of aliveWorkers) {
5958
// console.log('worker %s %s', worker.id, worker.state);
6059
worker.kill(KILL_SIGNAL);
6160
}

test/cluster-reload.test.js

-69
This file was deleted.

0 commit comments

Comments
 (0)