Skip to content

Commit b2e1b53

Browse files
authored
feat: support cjs and esm both by tshy (#120)
BREAKING CHANGE: drop Node.js < 18.19.0 support part of eggjs/egg#3644 eggjs/egg#5257 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes - **New Features** - Introduced a new GitHub Actions workflow for automated package publishing. - Added TypeScript configuration to enhance type safety and modern JavaScript compatibility. - New tests for child process management functionality. - **Updates** - Updated ESLint configuration to utilize external presets. - Modified GitHub Actions CI to specify a more precise Node.js version. - Enhanced README with installation and usage examples for ESM and TypeScript. - Updated package.json to reflect new module structure and dependencies. - Improved .gitignore to exclude additional temporary files and directories. - **Bug Fixes** - Improved .gitignore to exclude additional temporary files and directories. - **Removals** - Removed outdated JavaScript files and configurations in favor of CommonJS and TypeScript standards. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 79f495b commit b2e1b53

23 files changed

Lines changed: 1004 additions & 1047 deletions

.eslintrc

Lines changed: 4 additions & 196 deletions
Original file line numberDiff line numberDiff line change
@@ -1,198 +1,6 @@
11
{
2-
"ecmaFeatures": {
3-
"jsx": true
4-
},
5-
"parser": "espree",
6-
"env": {
7-
"amd": false,
8-
"jasmine": false,
9-
"node": true,
10-
"mocha": true,
11-
"browser": true,
12-
"builtin": true,
13-
"es6": true
14-
},
15-
"rules": {
16-
"no-alert": 2,
17-
"no-array-constructor": 2,
18-
"no-bitwise": 2,
19-
"no-caller": 2,
20-
"no-catch-shadow": 2,
21-
"no-comma-dangle": 0,
22-
"no-cond-assign": [2, "except-parens"],
23-
"no-constant-condition": 2,
24-
"no-continue": 0,
25-
"no-control-regex": 2,
26-
"no-debugger": 2,
27-
"no-delete-var": 2,
28-
"no-div-regex": 0,
29-
"no-dupe-keys": 2,
30-
"no-dupe-args": 2,
31-
"no-duplicate-case": 2,
32-
"no-else-return": 0,
33-
"no-empty": 2,
34-
"no-empty-class": 0,
35-
"no-empty-character-class": 2,
36-
"no-eq-null": 0,
37-
"no-eval": 2,
38-
"no-ex-assign": 2,
39-
"no-extend-native": 2,
40-
"no-extra-bind": 2,
41-
"no-extra-boolean-cast": 2,
42-
"no-extra-parens": 0,
43-
"no-extra-semi": 2,
44-
"no-fallthrough": 2,
45-
"no-floating-decimal": 0,
46-
"no-func-assign": 2,
47-
"no-implied-eval": 2,
48-
"no-inline-comments": 0,
49-
"no-inner-declarations": [2, "functions"],
50-
"no-invalid-regexp": 2,
51-
"no-irregular-whitespace": 2,
52-
"no-iterator": 0,
53-
"no-label-var": 2,
54-
"no-labels": 2,
55-
"no-lone-blocks": 2,
56-
"no-lonely-if": 0,
57-
"no-loop-func": 2,
58-
"no-mixed-requires": [0, false],
59-
"no-mixed-spaces-and-tabs": [2, false],
60-
"linebreak-style": [0, "unix"],
61-
"no-multi-spaces": 2,
62-
"no-multi-str": 2,
63-
"no-multiple-empty-lines": [0, {
64-
"max": 2
65-
}],
66-
"no-native-reassign": 2,
67-
"no-negated-in-lhs": 2,
68-
"no-nested-ternary": 0,
69-
"no-new": 0,
70-
"no-new-func": 2,
71-
"no-new-object": 2,
72-
"no-new-require": 0,
73-
"no-new-wrappers": 2,
74-
"no-obj-calls": 2,
75-
"no-octal": 2,
76-
"no-octal-escape": 2,
77-
"no-param-reassign": 0,
78-
"no-path-concat": 0,
79-
"no-plusplus": 0,
80-
"no-process-env": 0,
81-
"no-process-exit": 0,
82-
"no-proto": 2,
83-
"no-redeclare": 0,
84-
"no-regex-spaces": 2,
85-
"no-reserved-keys": 0,
86-
"no-restricted-modules": 0,
87-
"no-return-assign": 0,
88-
"no-script-url": 2,
89-
"no-self-compare": 0,
90-
"no-sequences": 2,
91-
"no-shadow": 0,
92-
"no-shadow-restricted-names": 2,
93-
"no-space-before-semi": 0,
94-
"no-spaced-func": 2,
95-
"no-sparse-arrays": 2,
96-
"no-sync": 0,
97-
"no-ternary": 0,
98-
"no-trailing-spaces": 2,
99-
"no-this-before-super": 0,
100-
"no-throw-literal": 0,
101-
"no-undef": 2,
102-
"no-undef-init": 2,
103-
"no-undefined": 0,
104-
"no-unexpected-multiline": 0,
105-
"no-underscore-dangle": 0,
106-
"no-unneeded-ternary": 0,
107-
"no-unreachable": 2,
108-
"no-unused-expressions": 0,
109-
"no-unused-vars": 2,
110-
"no-use-before-define": [2, "nofunc"],
111-
"no-void": 0,
112-
"no-var": 0,
113-
"prefer-const": 0,
114-
"no-warning-comments": [0, {
115-
"terms": ["todo", "fixme", "xxx"],
116-
"location": "start"
117-
}],
118-
"no-with": 2,
119-
"array-bracket-spacing": [0, "never"],
120-
"accessor-pairs": 0,
121-
"block-scoped-var": 0,
122-
"brace-style": [0, "1tbs"],
123-
"camelcase": 0,
124-
"comma-dangle": 0,
125-
"comma-spacing": 2,
126-
"comma-style": 0,
127-
"complexity": [0, 11],
128-
"computed-property-spacing": [0, "never"],
129-
"consistent-return": 0,
130-
"consistent-this": [0, "that"],
131-
"constructor-super": 0,
132-
"curly": [2, "all"],
133-
"default-case": 0,
134-
"dot-location": 0,
135-
"dot-notation": 0,
136-
"eol-last": 2,
137-
"eqeqeq": 2,
138-
"func-names": 0,
139-
"func-style": [0, "declaration"],
140-
"generator-star": 0,
141-
"generator-star-spacing": 0,
142-
"global-strict": [0, "never"],
143-
"guard-for-in": 0,
144-
"handle-callback-err": 0,
145-
"indent": [2, 2],
146-
"key-spacing": [2, {
147-
"beforeColon": false,
148-
"afterColon": true
149-
}],
150-
"keyword-spacing": 2,
151-
"lines-around-comment": 0,
152-
"max-depth": [0, 4],
153-
"max-len": [0, 80, 4],
154-
"max-nested-callbacks": [0, 2],
155-
"max-params": [0, 3],
156-
"max-statements": [0, 10],
157-
"new-cap": 0,
158-
"new-parens": 2,
159-
"newline-after-var": 0,
160-
"object-curly-spacing": [0, "never"],
161-
"object-shorthand": 0,
162-
"one-var": 0,
163-
"operator-assignment": [0, "always"],
164-
"operator-linebreak": 0,
165-
"padded-blocks": 0,
166-
"quote-props": 0,
167-
"quotes": [2, "single"],
168-
"radix": 0,
169-
"semi": 2,
170-
"semi-spacing": [2, {
171-
"before": false,
172-
"after": true
173-
}],
174-
"sort-vars": 0,
175-
"space-after-function-name": [0, "never"],
176-
"space-after-keywords": [0, "always"],
177-
"space-before-blocks": [0, "always"],
178-
"space-before-function-paren": [0, "always"],
179-
"space-before-function-parentheses": [0, "always"],
180-
"space-in-brackets": [0, "never"],
181-
"space-in-parens": [0, "never"],
182-
"space-infix-ops": 2,
183-
"space-unary-ops": [2, {
184-
"words": true,
185-
"nonwords": false
186-
}],
187-
"spaced-comment": 0,
188-
"spaced-line-comment": [0, "always"],
189-
"strict": [2, "global"],
190-
"use-isnan": 2,
191-
"valid-jsdoc": 0,
192-
"valid-typeof": 0,
193-
"vars-on-top": 0,
194-
"wrap-iife": 0,
195-
"wrap-regex": 0,
196-
"yoda": [2, "never"]
197-
}
2+
"extends": [
3+
"eslint-config-egg/typescript",
4+
"eslint-config-egg/lib/rules/enforce-node-prefix"
5+
]
1986
}

.github/workflows/nodejs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ jobs:
1212
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
1313
with:
1414
os: 'ubuntu-latest, macos-latest, windows-latest'
15-
version: '14, 16, 18, 20, 22'
15+
version: '18.19.0, 18, 20, 22'
1616
secrets:
1717
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

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

Lines changed: 23 additions & 0 deletions
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

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@ results
1313

1414
node_modules
1515
npm-debug.log
16-
coverage/
1716
package-lock.json
17+
.tshy*
18+
.eslintcache
19+
dist
20+
coverage

README.md

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
[![Node.js CI](https://github.com/node-modules/cfork/actions/workflows/nodejs.yml/badge.svg)](https://github.com/node-modules/cfork/actions/workflows/nodejs.yml)
55
[![Test coverage][codecov-image]][codecov-url]
66
[![npm download][download-image]][download-url]
7+
[![Node.js Version](https://img.shields.io/node/v/cfork.svg?style=flat)](https://nodejs.org/en/download/)
78

89
[npm-image]: https://img.shields.io/npm/v/cfork.svg?style=flat-square
910
[npm-url]: https://npmjs.org/package/cfork
@@ -21,16 +22,16 @@ cluster fork and restart easy way.
2122
## Install
2223

2324
```bash
24-
$ npm install cfork --save
25+
npm install cfork
2526
```
2627

2728
## Usage
2829

29-
### Example
30+
### ESM and TypeScript
3031

31-
```js
32-
const cfork = require('cfork');
33-
const util = require('util');
32+
```ts
33+
import util from 'node:util';
34+
import { cfork } from 'cfork';
3435

3536
const clusterWorker = cfork({
3637
exec: '/your/app/worker.js',
@@ -68,37 +69,34 @@ process.on('uncaughtException', err => {
6869

6970
// if you want to dynamically disable refork, you can call the setDisableRefork, priority over the refork parameter
7071
cfork.setDisableRefork(clusterWorker, true);
72+
```
7173

74+
### CommonJS
7275

76+
```js
77+
const { cfork } = require('cfork');
7378
```
7479

7580
### Options
7681

77-
- **exec** : exec file path
78-
- **slaves** : slave process config
79-
- **args** : exec arguments
80-
- **count** : fork worker nums, default is `os.cpus().length`
81-
- **refork** : refork when worker disconnect or unexpected exit, default is `true`
82-
- **limit**: limit refork times within the `duration`, default is `60`
83-
- **duration**: default is `60000`, one minute (so, the `refork times` < `limit / duration`)
84-
- **autoCoverage**: auto fork with istanbul when `running_under_istanbul` env set, default is `false`
85-
- **env**: attach some environment variable key-value pairs to the worker / slave process, default to an empty object.
86-
- **windowsHide**: Hide the forked processes console window that would normally be created on Windows systems, default to false.
87-
- **serialization**: Specify the kind of serialization used for sending messages between processes. Possible values are 'json' and 'advanced'. See Advanced serialization for child_process for more details. Default: false.
82+
* **exec** : exec file path
83+
* **slaves** : slave process config
84+
* **args** : exec arguments
85+
* **count** : fork worker nums, default is `os.cpus().length`
86+
* **refork** : refork when worker disconnect or unexpected exit, default is `true`
87+
* **limit**: limit refork times within the `duration`, default is `60`
88+
* **duration**: default is `60000`, one minute (so, the `refork times` < `limit / duration`)
89+
* **autoCoverage**: auto fork with istanbul when `running_under_istanbul` env set, default is `false`
90+
* **env**: attach some environment variable key-value pairs to the worker / slave process, default to an empty object.
91+
* **windowsHide**: Hide the forked processes console window that would normally be created on Windows systems, default to false.
92+
* **serialization**: Specify the kind of serialization used for sending messages between processes. Possible values are 'json' and 'advanced'. See Advanced serialization for child_process for more details. Default: false.
8893

8994
## License
9095

9196
[MIT](LICENSE)
9297

93-
<!-- GITCONTRIBUTOR_START -->
94-
9598
## Contributors
9699

97-
|[<img src="https://avatars.githubusercontent.com/u/156269?v=4" width="100px;"/><br/><sub><b>fengmk2</b></sub>](https://github.com/fengmk2)<br/>|[<img src="https://avatars.githubusercontent.com/u/14790466?v=4" width="100px;"/><br/><sub><b>greenkeeperio-bot</b></sub>](https://github.com/greenkeeperio-bot)<br/>|[<img src="https://avatars.githubusercontent.com/u/985607?v=4" width="100px;"/><br/><sub><b>dead-horse</b></sub>](https://github.com/dead-horse)<br/>|[<img src="https://avatars.githubusercontent.com/u/327019?v=4" width="100px;"/><br/><sub><b>JacksonTian</b></sub>](https://github.com/JacksonTian)<br/>|[<img src="https://avatars.githubusercontent.com/u/1474688?v=4" width="100px;"/><br/><sub><b>luckydrq</b></sub>](https://github.com/luckydrq)<br/>|[<img src="https://avatars.githubusercontent.com/u/2842176?v=4" width="100px;"/><br/><sub><b>XadillaX</b></sub>](https://github.com/XadillaX)<br/>|
98-
| :---: | :---: | :---: | :---: | :---: | :---: |
99-
|[<img src="https://avatars.githubusercontent.com/u/360661?v=4" width="100px;"/><br/><sub><b>popomore</b></sub>](https://github.com/popomore)<br/>|[<img src="https://avatars.githubusercontent.com/u/18315?v=4" width="100px;"/><br/><sub><b>hemanth</b></sub>](https://github.com/hemanth)<br/>|[<img src="https://avatars.githubusercontent.com/u/3230673?v=4" width="100px;"/><br/><sub><b>qingdengyue</b></sub>](https://github.com/qingdengyue)<br/>|[<img src="https://avatars.githubusercontent.com/u/1422472?v=4" width="100px;"/><br/><sub><b>jpuncle</b></sub>](https://github.com/jpuncle)<br/>|[<img src="https://avatars.githubusercontent.com/u/1102038?v=4" width="100px;"/><br/><sub><b>hustxiaoc</b></sub>](https://github.com/hustxiaoc)<br/>|[<img src="https://avatars.githubusercontent.com/u/2972143?v=4" width="100px;"/><br/><sub><b>nightink</b></sub>](https://github.com/nightink)<br/>|
100-
[<img src="https://avatars.githubusercontent.com/u/7971415?v=4" width="100px;"/><br/><sub><b>paranoidjk</b></sub>](https://github.com/paranoidjk)<br/>|[<img src="https://avatars.githubusercontent.com/u/19849579?v=4" width="100px;"/><br/><sub><b>sinkhaha</b></sub>](https://github.com/sinkhaha)<br/>
101-
102-
This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Sat May 06 2023 00:30:47 GMT+0800`.
100+
[![Contributors](https://contrib.rocks/image?repo=node-modules/cfork)](https://github.com/node-modules/cfork/graphs/contributors)
103101

104-
<!-- GITCONTRIBUTOR_END -->
102+
Made with [contributors-img](https://contrib.rocks).

fixtures/kill_worker/master.js

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

0 commit comments

Comments
 (0)