Skip to content

Commit 30acc65

Browse files
authored
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
1 parent d54af2c commit 30acc65

File tree

11 files changed

+185
-135
lines changed

11 files changed

+185
-135
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"extends": [
3-
"eslint-config-egg"
3+
"eslint-config-egg/typescript",
4+
"eslint-config-egg/lib/rules/enforce-node-prefix"
45
]
56
}

.github/workflows/nodejs.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ name: CI
33
on:
44
push:
55
branches: [ master ]
6-
76
pull_request:
87
branches: [ master ]
98

@@ -13,4 +12,6 @@ jobs:
1312
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
1413
with:
1514
os: 'ubuntu-latest, macos-latest, windows-latest'
16-
version: '14, 16, 18, 20'
15+
version: '18.19.0, 18, 20, 22'
16+
secrets:
17+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

.github/workflows/release.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,3 @@ jobs:
1010
secrets:
1111
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
1212
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}
13-
with:
14-
checkTest: false

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ results
1414
node_modules
1515
npm-debug.log
1616
coverage/
17+
.tshy*
18+
.eslintcache
19+
dist

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MIT License
2+
3+
Copyright (c) 2015-present node-modules and other contributors.
4+
Copyright (c) 2014 - 2015 fengmk2 <[email protected]> and other contributors
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.

README.md

Lines changed: 15 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -25,84 +25,45 @@ npm install sendmessage --save
2525

2626
### master.js
2727

28-
```js
29-
var childprocess = require('child_process');
30-
var sendmessage = require('sendmessage');
28+
```ts
29+
import { fork } from 'node:child_process';
30+
import sendmessage from 'sendmessage';
31+
32+
const worker = fork('./worker.js');
3133

32-
var worker = childprocess.fork('./worker.js');
33-
sendmessage(worker, {hi: 'this is a message to worker'});
34+
sendmessage(worker, { hi: 'this is a message to worker' });
3435
```
3536

3637
### worker.js
3738

38-
```js
39-
var sendmessage = require('sendmessage');
39+
```ts
40+
import sendmessage from 'sendmessage';
4041

41-
sendmessage(process, {hello: 'this is a message to master'});
42+
sendmessage(process, { hello: 'this is a message to master' });
4243
```
4344

4445
## API
4546

46-
### #sendmessage(childprocess, message)
47+
### #sendmessage(childProcess, message)
4748

4849
Send a cross process message.
4950
If a process is not child process, this will just call `process.emit('message', message)` instead.
5051

51-
- childprocess: child process instance
52+
- childProcess: child process instance
5253
- message: the message need to send
5354

5455
```js
55-
sendmessage(process, {hello: 'this is a message to master'});
56+
sendmessage(process, { hello: 'this is a message to master' });
5657
```
5758

5859
You can switch to `process.emit('message', message)` using `process.env.SENDMESSAGE_ONE_PROCESS`
5960

60-
## Test
61-
62-
```bash
63-
npm install
64-
npm test
65-
```
66-
67-
### Coverage
68-
69-
```bash
70-
npm run ci
71-
```
72-
7361
## License
7462

75-
(The MIT License)
76-
77-
Copyright (c) 2014 - 2015 fengmk2 <[email protected]> and other contributors
78-
79-
Permission is hereby granted, free of charge, to any person obtaining
80-
a copy of this software and associated documentation files (the
81-
'Software'), to deal in the Software without restriction, including
82-
without limitation the rights to use, copy, modify, merge, publish,
83-
distribute, sublicense, and/or sell copies of the Software, and to
84-
permit persons to whom the Software is furnished to do so, subject to
85-
the following conditions:
86-
87-
The above copyright notice and this permission notice shall be
88-
included in all copies or substantial portions of the Software.
89-
90-
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
91-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
92-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
93-
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
94-
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
95-
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
96-
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
97-
98-
<!-- GITCONTRIBUTOR_START -->
63+
[MIT](LICENSE)
9964

10065
## Contributors
10166

102-
|[<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/>|
103-
| :---: | :---: | :---: | :---: |
104-
105-
106-
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`.
67+
[![Contributors](https://contrib.rocks/image?repo=node-modules/sendmessage)](https://github.com/node-modules/sendmessage/graphs/contributors)
10768

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

package.json

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,36 @@
11
{
22
"name": "sendmessage",
33
"version": "2.0.0",
4+
"engines": {
5+
"node": ">= 18.19.0"
6+
},
47
"description": "Send a cross process message if message channel is connected.",
5-
"main": "index.js",
6-
"files": [
7-
"index.js"
8-
],
98
"scripts": {
10-
"test": "mocha --exit -t 5000 test/*.test.js",
9+
"lint": "eslint --cache src --ext .ts",
10+
"pretest": "npm run prepublishOnly && attw --pack",
11+
"test": "npm run lint && mocha --exit -t 5000 test/*.test.js",
1112
"ci": "c8 -r lcov -r text -r text-summary npm test",
12-
"lint": "eslint .",
13-
"contributors": "git-contributor"
13+
"prepublishOnly": "tshy && tshy-after"
1414
},
1515
"dependencies": {},
1616
"devDependencies": {
17-
"c8": "^7.14.0",
18-
"eslint": "^8.42.0",
19-
"eslint-config-egg": "^12.2.1",
20-
"git-contributor": "^2.1.5",
21-
"mm": "^3.3.0",
22-
"mocha": "^10.2.0",
23-
"should": "*"
17+
"@arethetypeswrong/cli": "^0.15.3",
18+
"@eggjs/tsconfig": "1",
19+
"@types/mocha": "10",
20+
"@types/node": "20",
21+
"c8": "^10.1.2",
22+
"eslint": "8",
23+
"eslint-config-egg": "13",
24+
"mm": "3",
25+
"mocha": "^10.4.0",
26+
"tshy": "1",
27+
"tshy-after": "1",
28+
"typescript": "5"
2429
},
2530
"homepage": "https://github.com/node-modules/sendmessage",
2631
"repository": {
2732
"type": "git",
28-
"url": "git://github.com/node-modules/sendmessage.git",
29-
"web": "https://github.com/node-modules/sendmessage"
33+
"url": "git://github.com/node-modules/sendmessage.git"
3034
},
3135
"bugs": {
3236
"url": "https://github.com/node-modules/sendmessage/issues"
@@ -38,9 +42,34 @@
3842
"message",
3943
"channel closed"
4044
],
41-
"engines": {
42-
"node": ">= 14.17.0"
43-
},
4445
"author": "fengmk2 <[email protected]> (https://github.com/fengmk2)",
45-
"license": "MIT"
46+
"license": "MIT",
47+
"type": "module",
48+
"tshy": {
49+
"exports": {
50+
".": "./src/index.ts",
51+
"./package.json": "./package.json"
52+
}
53+
},
54+
"exports": {
55+
".": {
56+
"import": {
57+
"source": "./src/index.ts",
58+
"types": "./dist/esm/index.d.ts",
59+
"default": "./dist/esm/index.js"
60+
},
61+
"require": {
62+
"source": "./src/index.ts",
63+
"types": "./dist/commonjs/index.d.ts",
64+
"default": "./dist/commonjs/index.js"
65+
}
66+
},
67+
"./package.json": "./package.json"
68+
},
69+
"files": [
70+
"dist",
71+
"src"
72+
],
73+
"types": "./dist/commonjs/index.d.ts",
74+
"main": "./dist/commonjs/index.js"
4675
}
Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,70 @@
1-
const { isMainThread, parentPort } = require('worker_threads');
1+
import { debuglog } from 'node:util';
2+
import { isMainThread, parentPort } from 'node:worker_threads';
3+
import { EventEmitter } from 'node:events';
4+
5+
const debug = debuglog('sendmessage');
26

37
let IS_NODE_DEV_RUNNER = /node\-dev$/.test(process.env._ || '');
48
if (!IS_NODE_DEV_RUNNER && process.env.IS_NODE_DEV_RUNNER) {
59
IS_NODE_DEV_RUNNER = true;
610
}
11+
debug('IS_NODE_DEV_RUNNER: %s', IS_NODE_DEV_RUNNER);
12+
13+
export interface ChildProcessOrWorker extends EventEmitter {
14+
// Worker
15+
postMessage?(message: unknown): void;
16+
// ChildProcess
17+
send?(message: unknown): boolean;
18+
connected?: boolean;
19+
pid?: number;
20+
process?: {
21+
connected?: boolean;
22+
pid?: number;
23+
};
24+
}
725

8-
module.exports = function send(child, message) {
26+
export default function sendmessage(child: ChildProcessOrWorker, message: unknown) {
927
if (
1028
isMainThread // not in worker thread
1129
&& typeof child.postMessage !== 'function' // child is not worker
1230
&& typeof child.send !== 'function'
1331
) {
32+
debug('child is master process, emit message: %j', message);
1433
// not a child process
1534
return setImmediate(child.emit.bind(child, 'message', message));
1635
}
1736

1837
if (IS_NODE_DEV_RUNNER || process.env.SENDMESSAGE_ONE_PROCESS) {
1938
// run with node-dev, only one process
2039
// https://github.com/node-modules/sendmessage/issues/1
40+
debug('node-dev: %s or SENDMESSAGE_ONE_PROCESS: %s, emit message: %j',
41+
IS_NODE_DEV_RUNNER, process.env.SENDMESSAGE_ONE_PROCESS, message);
2142
return setImmediate(child.emit.bind(child, 'message', message));
2243
}
2344

2445
// child is worker
2546
if (typeof child.postMessage === 'function') {
47+
debug('child is worker, postMessage: %j', message);
2648
return child.postMessage(message);
2749
}
2850
// in worker thread
2951
if (!isMainThread) {
30-
return parentPort.postMessage(message);
52+
debug('in worker thread, parentPort.postMessage: %j', message);
53+
return parentPort!.postMessage(message);
3154
}
3255

3356
// cluster.fork(): child.process is process
3457
// childprocess.fork(): child is process
3558
const connected = child.process ? child.process.connected : child.connected;
3659

3760
if (connected) {
38-
return child.send(message);
61+
debug('child is process, send: %j', message);
62+
return child.send!(message);
3963
}
4064

41-
// just log warnning message
65+
// just log warning message
4266
const pid = child.process ? child.process.pid : child.pid;
4367
const err = new Error('channel closed');
4468
console.warn('[%s][sendmessage] WARN pid#%s channel closed, nothing send\nstack: %s',
4569
Date(), pid, err.stack);
46-
};
70+
}

test/child.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const { isMainThread, parentPort } = require('worker_threads');
2-
const sendmessage = require('..');
1+
import { isMainThread, parentPort } from 'node:worker_threads';
2+
import sendmessage from '../dist/esm/index.js';
33

44
const listener = function(message) {
55
if (message.disconnect) {
@@ -11,7 +11,9 @@ const listener = function(message) {
1111
got: message,
1212
});
1313
};
14+
1415
process.on('message', listener);
16+
1517
if (!isMainThread) {
1618
// worker thread
1719
parentPort.on('message', listener);

0 commit comments

Comments
 (0)