-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support cjs and esm both by tshy (#6)
BREAKING CHANGE: drop Node.js < 18.19.0 support part of eggjs/egg#3644 eggjs/egg#5257
- Loading branch information
Showing
11 changed files
with
185 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"extends": [ | ||
"eslint-config-egg" | ||
"eslint-config-egg/typescript", | ||
"eslint-config-egg/lib/rules/enforce-node-prefix" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,5 +10,3 @@ jobs: | |
secrets: | ||
NPM_TOKEN: ${{ secrets.NPM_TOKEN }} | ||
GIT_TOKEN: ${{ secrets.GIT_TOKEN }} | ||
with: | ||
checkTest: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,6 @@ results | |
node_modules | ||
npm-debug.log | ||
coverage/ | ||
.tshy* | ||
.eslintcache | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
MIT License | ||
|
||
Copyright (c) 2015-present node-modules and other contributors. | ||
Copyright (c) 2014 - 2015 fengmk2 <[email protected]> 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,84 +25,45 @@ npm install sendmessage --save | |
|
||
### master.js | ||
|
||
```js | ||
var childprocess = require('child_process'); | ||
var sendmessage = require('sendmessage'); | ||
```ts | ||
import { fork } from 'node:child_process'; | ||
import sendmessage from 'sendmessage'; | ||
|
||
const worker = fork('./worker.js'); | ||
|
||
var worker = childprocess.fork('./worker.js'); | ||
sendmessage(worker, {hi: 'this is a message to worker'}); | ||
sendmessage(worker, { hi: 'this is a message to worker' }); | ||
``` | ||
|
||
### worker.js | ||
|
||
```js | ||
var sendmessage = require('sendmessage'); | ||
```ts | ||
import sendmessage from 'sendmessage'; | ||
|
||
sendmessage(process, {hello: 'this is a message to master'}); | ||
sendmessage(process, { hello: 'this is a message to master' }); | ||
``` | ||
|
||
## API | ||
|
||
### #sendmessage(childprocess, message) | ||
### #sendmessage(childProcess, message) | ||
|
||
Send a cross process message. | ||
If a process is not child process, this will just call `process.emit('message', message)` instead. | ||
|
||
- childprocess: child process instance | ||
- childProcess: child process instance | ||
- message: the message need to send | ||
|
||
```js | ||
sendmessage(process, {hello: 'this is a message to master'}); | ||
sendmessage(process, { hello: 'this is a message to master' }); | ||
``` | ||
|
||
You can switch to `process.emit('message', message)` using `process.env.SENDMESSAGE_ONE_PROCESS` | ||
|
||
## Test | ||
|
||
```bash | ||
npm install | ||
npm test | ||
``` | ||
|
||
### Coverage | ||
|
||
```bash | ||
npm run ci | ||
``` | ||
|
||
## License | ||
|
||
(The MIT License) | ||
|
||
Copyright (c) 2014 - 2015 fengmk2 <[email protected]> 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. | ||
|
||
<!-- GITCONTRIBUTOR_START --> | ||
[MIT](LICENSE) | ||
|
||
## 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/360661?v=4" width="100px;"/><br/><sub><b>popomore</b></sub>](https://github.com/popomore)<br/>|[<img src="https://avatars.githubusercontent.com/u/32174276?v=4" width="100px;"/><br/><sub><b>semantic-release-bot</b></sub>](https://github.com/semantic-release-bot)<br/>|[<img src="https://avatars.githubusercontent.com/u/7581901?v=4" width="100px;"/><br/><sub><b>sjfkai</b></sub>](https://github.com/sjfkai)<br/>| | ||
| :---: | :---: | :---: | :---: | | ||
|
||
|
||
This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Tue Jun 13 2023 20:42:15 GMT+0800`. | ||
[![Contributors](https://contrib.rocks/image?repo=node-modules/sendmessage)](https://github.com/node-modules/sendmessage/graphs/contributors) | ||
|
||
<!-- GITCONTRIBUTOR_END --> | ||
Made with [contributors-img](https://contrib.rocks). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,36 @@ | ||
{ | ||
"name": "sendmessage", | ||
"version": "2.0.0", | ||
"engines": { | ||
"node": ">= 18.19.0" | ||
}, | ||
"description": "Send a cross process message if message channel is connected.", | ||
"main": "index.js", | ||
"files": [ | ||
"index.js" | ||
], | ||
"scripts": { | ||
"test": "mocha --exit -t 5000 test/*.test.js", | ||
"lint": "eslint --cache src --ext .ts", | ||
"pretest": "npm run prepublishOnly && attw --pack", | ||
"test": "npm run lint && mocha --exit -t 5000 test/*.test.js", | ||
"ci": "c8 -r lcov -r text -r text-summary npm test", | ||
"lint": "eslint .", | ||
"contributors": "git-contributor" | ||
"prepublishOnly": "tshy && tshy-after" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"c8": "^7.14.0", | ||
"eslint": "^8.42.0", | ||
"eslint-config-egg": "^12.2.1", | ||
"git-contributor": "^2.1.5", | ||
"mm": "^3.3.0", | ||
"mocha": "^10.2.0", | ||
"should": "*" | ||
"@arethetypeswrong/cli": "^0.15.3", | ||
"@eggjs/tsconfig": "1", | ||
"@types/mocha": "10", | ||
"@types/node": "20", | ||
"c8": "^10.1.2", | ||
"eslint": "8", | ||
"eslint-config-egg": "13", | ||
"mm": "3", | ||
"mocha": "^10.4.0", | ||
"tshy": "1", | ||
"tshy-after": "1", | ||
"typescript": "5" | ||
}, | ||
"homepage": "https://github.com/node-modules/sendmessage", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/node-modules/sendmessage.git", | ||
"web": "https://github.com/node-modules/sendmessage" | ||
"url": "git://github.com/node-modules/sendmessage.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/node-modules/sendmessage/issues" | ||
|
@@ -38,9 +42,34 @@ | |
"message", | ||
"channel closed" | ||
], | ||
"engines": { | ||
"node": ">= 14.17.0" | ||
}, | ||
"author": "fengmk2 <[email protected]> (https://github.com/fengmk2)", | ||
"license": "MIT" | ||
"license": "MIT", | ||
"type": "module", | ||
"tshy": { | ||
"exports": { | ||
".": "./src/index.ts", | ||
"./package.json": "./package.json" | ||
} | ||
}, | ||
"exports": { | ||
".": { | ||
"import": { | ||
"source": "./src/index.ts", | ||
"types": "./dist/esm/index.d.ts", | ||
"default": "./dist/esm/index.js" | ||
}, | ||
"require": { | ||
"source": "./src/index.ts", | ||
"types": "./dist/commonjs/index.d.ts", | ||
"default": "./dist/commonjs/index.js" | ||
} | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"files": [ | ||
"dist", | ||
"src" | ||
], | ||
"types": "./dist/commonjs/index.d.ts", | ||
"main": "./dist/commonjs/index.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,70 @@ | ||
const { isMainThread, parentPort } = require('worker_threads'); | ||
import { debuglog } from 'node:util'; | ||
import { isMainThread, parentPort } from 'node:worker_threads'; | ||
import { EventEmitter } from 'node:events'; | ||
|
||
const debug = debuglog('sendmessage'); | ||
|
||
let IS_NODE_DEV_RUNNER = /node\-dev$/.test(process.env._ || ''); | ||
if (!IS_NODE_DEV_RUNNER && process.env.IS_NODE_DEV_RUNNER) { | ||
IS_NODE_DEV_RUNNER = true; | ||
} | ||
debug('IS_NODE_DEV_RUNNER: %s', IS_NODE_DEV_RUNNER); | ||
|
||
export interface ChildProcessOrWorker extends EventEmitter { | ||
// Worker | ||
postMessage?(message: unknown): void; | ||
// ChildProcess | ||
send?(message: unknown): boolean; | ||
connected?: boolean; | ||
pid?: number; | ||
process?: { | ||
connected?: boolean; | ||
pid?: number; | ||
}; | ||
} | ||
|
||
module.exports = function send(child, message) { | ||
export default function sendmessage(child: ChildProcessOrWorker, message: unknown) { | ||
if ( | ||
isMainThread // not in worker thread | ||
&& typeof child.postMessage !== 'function' // child is not worker | ||
&& typeof child.send !== 'function' | ||
) { | ||
debug('child is master process, emit message: %j', message); | ||
// not a child process | ||
return setImmediate(child.emit.bind(child, 'message', message)); | ||
} | ||
|
||
if (IS_NODE_DEV_RUNNER || process.env.SENDMESSAGE_ONE_PROCESS) { | ||
// run with node-dev, only one process | ||
// https://github.com/node-modules/sendmessage/issues/1 | ||
debug('node-dev: %s or SENDMESSAGE_ONE_PROCESS: %s, emit message: %j', | ||
IS_NODE_DEV_RUNNER, process.env.SENDMESSAGE_ONE_PROCESS, message); | ||
return setImmediate(child.emit.bind(child, 'message', message)); | ||
} | ||
|
||
// child is worker | ||
if (typeof child.postMessage === 'function') { | ||
debug('child is worker, postMessage: %j', message); | ||
return child.postMessage(message); | ||
} | ||
// in worker thread | ||
if (!isMainThread) { | ||
return parentPort.postMessage(message); | ||
debug('in worker thread, parentPort.postMessage: %j', message); | ||
return parentPort!.postMessage(message); | ||
} | ||
|
||
// cluster.fork(): child.process is process | ||
// childprocess.fork(): child is process | ||
const connected = child.process ? child.process.connected : child.connected; | ||
|
||
if (connected) { | ||
return child.send(message); | ||
debug('child is process, send: %j', message); | ||
return child.send!(message); | ||
} | ||
|
||
// just log warnning message | ||
// just log warning message | ||
const pid = child.process ? child.process.pid : child.pid; | ||
const err = new Error('channel closed'); | ||
console.warn('[%s][sendmessage] WARN pid#%s channel closed, nothing send\nstack: %s', | ||
Date(), pid, err.stack); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.