Skip to content

Commit

Permalink
feat: support cjs and esm both by tshy (#17)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: drop Node.js < 18.19.0 support

part of eggjs/egg#3644

eggjs/egg#5257

closes #16

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
	- Introduced a new ESLint configuration for TypeScript and Node.js.
	- Added a new GitHub Actions workflow for package publishing.
	- Implemented a new TypeScript configuration for enhanced type safety.
	- Created a new test suite for validating worker process behavior.
	- Added a new test suite for verifying child process behavior.

- **Bug Fixes**
- Updated Node.js CI workflow to include newer versions and improved
configurations.

- **Documentation**
	- Enhanced README with updated badges and installation instructions.
	- Updated example documentation to reflect file extension changes.

- **Chores**
	- Removed outdated files and workflows to streamline the repository.
	- Updated `.gitignore` to exclude additional files and directories.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
fengmk2 authored Dec 15, 2024
1 parent fcc80da commit 7192a67
Show file tree
Hide file tree
Showing 30 changed files with 519 additions and 534 deletions.
6 changes: 6 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": [
"eslint-config-egg/typescript",
"eslint-config-egg/lib/rules/enforce-node-prefix"
]
}
74 changes: 0 additions & 74 deletions .github/workflows/codeql-analysis.yml

This file was deleted.

9 changes: 4 additions & 5 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ name: CI
on:
push:
branches: [ master ]

pull_request:
branches: [ master ]

workflow_dispatch: {}

jobs:
Job:
name: Node.js
uses: artusjs/github-actions/.github/workflows/node-test.yml@v1
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
os: 'ubuntu-latest, macos-latest'
version: '14, 16, 18'
version: '18.19.0, 18, 20, 22'
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
23 changes: 23 additions & 0 deletions .github/workflows/pkg.pr.new.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Publish Any Commit
on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- run: corepack enable
- uses: actions/setup-node@v4
with:
node-version: 20

- name: Install dependencies
run: npm install

- name: Build
run: npm run prepublishOnly --if-present

- run: npx pkg-pr-new publish
6 changes: 1 addition & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ on:
push:
branches: [ master ]

workflow_dispatch: {}

jobs:
release:
name: Node.js
uses: artusjs/github-actions/.github/workflows/node-release.yml@v1
uses: node-modules/github-actions/.github/workflows/node-release.yml@master
secrets:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}
with:
checkTest: false
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ results

node_modules
npm-debug.log
.tshy*
.eslintcache
dist
File renamed without changes.
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2013 - 2014 fengmk2
Copyright (c) 2015 - present node-modules and other contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
57 changes: 16 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
[![NPM version][npm-image]][npm-url]
[![Test coverage][cov-image]][cov-url]
[![npm download][download-image]][download-url]
[![Node.js Version](https://img.shields.io/node/v/graceful.svg?style=flat)](https://nodejs.org/en/download/)

[npm-image]: https://img.shields.io/npm/v/graceful.svg?style=flat-square
[npm-url]: https://npmjs.org/package/graceful
[cov-image]: https://codecov.io/github/node-modules/cfork/coverage.svg?branch=master
[cov-url]: https://codecov.io/github/node-modules/cfork?branch=master
[cov-image]: https://codecov.io/github/node-modules/graceful/coverage.svg?branch=master
[cov-url]: https://codecov.io/github/node-modules/graceful?branch=master
[download-image]: https://img.shields.io/npm/dm/graceful.svg?style=flat-square
[download-url]: https://npmjs.org/package/graceful

Expand All @@ -17,26 +18,25 @@ Graceful exit when `uncaughtException` emit, base on `process.on('uncaughtExcept

It's the best way to handle `uncaughtException` on current situations.

* [domain failure](https://github.com/fengmk2/domain-middleware/blob/master/example/failure.js).
* [Node.js 异步异常的处理与domain模块解析](http://deadhorse.me/nodejs/2013/04/13/exception_and_domain.html)

## Install

```bash
$ npm install graceful
npm install graceful
```

## Usage

Please see [connect_with_cluster](https://github.com/fengmk2/graceful/tree/master/example/connect_with_cluster) example.
Please see [express_with_cluster](https://github.com/node-modules/graceful/tree/master/example/express_with_cluster) example.

This below code just for dev demo, don't use it on production env:

```js
var express = require('express');
var graceful = require('graceful');
const express = require('express');
const { graceful } = require('graceful');

var app = express()
const app = express()
.use()
.use(function(req, res){
if (Math.random() > 0.5) {
Expand All @@ -59,7 +59,7 @@ var app = express()
res.end(err.message);
});

var server = app.listen(1984);
const server = app.listen(1984);

graceful({
servers: [server],
Expand All @@ -76,43 +76,18 @@ graceful({
});
```

If you are using [pm](https://github.com/aleafs/pm),
you can follow the [graceful_exit with pm demo](https://github.com/aleafs/pm/tree/master/demo/graceful_exit).
### ESM and TypeScript

<!-- GITCONTRIBUTOR_START -->
```ts
import { graceful } from 'graceful';
```

## Contributors

|[<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/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/19908330?v=4" width="100px;"/><br/><sub><b>hyj1991</b></sub>](https://github.com/hyj1991)<br/>|[<img src="https://avatars.githubusercontent.com/u/2399123?v=4" width="100px;"/><br/><sub><b>imyelo</b></sub>](https://github.com/imyelo)<br/>|
| :---: | :---: | :---: | :---: |


This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Thu Sep 22 2022 19:41:57 GMT+0800`.
[![Contributors](https://contrib.rocks/image?repo=node-modules/graceful)](https://github.com/node-modules/graceful/graphs/contributors)

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

## License

(The MIT License)

Copyright (c) 2013 - 2014 fengmk2 &lt;fengmk2@gmail.com&gt;
Copyright (c) 2015 - 2016 node-modules and other contributors

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
[MIT](LICENSE)
12 changes: 6 additions & 6 deletions example/express_with_cluster/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# express with cluster example

* Master: dispatch.js
* Worker: worker.js
* Your application logic: app.js
* Master: dispatch.cjs
* Worker: worker.cjs
* Your application logic: app.cjs

## Run

```bash
$ node example/express_with_cluster/dispatch.js
$ node example/express_with_cluster/dispatch.cjs
```

## Test
Expand All @@ -21,10 +21,10 @@ $ curl localhost:1337/asyncerror

```

[dispatch.js](https://github.com/node-modules/graceful/blob/master/example/express_with_cluster/dispatch.js) stdout:
[dispatch.cjs](https://github.com/node-modules/graceful/blob/master/example/express_with_cluster/dispatch.cjs) stdout:

```bash
$ node example/express_with_cluster/dispatch.js
$ node example/express_with_cluster/dispatch.cjs
[Thu Apr 11 2013 18:45:36 GMT+0800 (CST)] [worker:21711] start listen on 1337
[Thu Apr 11 2013 18:45:36 GMT+0800 (CST)] [worker:21712] start listen on 1337
[uncaughtException] throw error 1 times
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
"use strict";

// http://nodejs.org/docs/latest/api/domain.html#domain_warning_don_t_ignore_errors
var cluster = require('cluster');
var path = require('path');

cluster.setupMaster({
exec: path.join(__dirname, 'worker.js')
exec: path.join(__dirname, 'worker.cjs')
});

// In real life, you'd probably use more than just 2 workers,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

var PORT = +process.env.PORT || 1337;
var graceful = require('../../');
var { graceful } = require('../../');
var server = require('./app');
server.listen(PORT);
console.log('[%s] [worker:%s] web server start listen on %s', new Date(), process.pid, PORT);
Expand Down
2 changes: 1 addition & 1 deletion example/failure.js → example/failure.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var http = require('http');
var express = require('express');
var graceful = require('../');
var { graceful } = require('../');

var keepAliveClient = http.request({
host: 'www.google.com',
Expand Down
Loading

0 comments on commit 7192a67

Please sign in to comment.