-
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 <fengmk2@gmail.com> 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
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 |
---|---|---|
@@ -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.