@koeroesi86/node-worker-express 20.10.9-16a7d5b → 22.3.12-fd2e588
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/constants/index.js +1 -0
- package/examples/staticWorker.js +46 -7
- package/index.d.ts +1 -0
- package/middleware/index.js +19 -0
- package/middleware/workerInvoke.js +4 -2
- package/package.json +6 -6
- package/utils/workerPool.js +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Node Worker Express [](https://app.travis-ci.com/Koeroesi86/node-worker-express)
|
|
2
2
|
|
|
3
3
|
Express JS library for [@koeroesi86/node-worker](https://www.npmjs.com/package/@koeroesi86/node-worker)
|
|
4
4
|
|
package/constants/index.js
CHANGED
|
@@ -2,6 +2,7 @@ const WORKER_EVENT = {
|
|
|
2
2
|
REQUEST: 'WORKER_REQUEST',
|
|
3
3
|
REQUEST_ACKNOWLEDGE: 'WORKER_REQUEST_ACK',
|
|
4
4
|
RESPONSE: 'WORKER_RESPONSE',
|
|
5
|
+
RESPONSE_EMIT: 'WORKER_RESPONSE_EMIT',
|
|
5
6
|
RESPONSE_ACKNOWLEDGE: 'WORKER_RESPONSE_ACK',
|
|
6
7
|
WS_MESSAGE_RECEIVE: 'WS_MESSAGE_RECEIVE',
|
|
7
8
|
WS_MESSAGE_SEND: 'WS_MESSAGE_SEND',
|
package/examples/staticWorker.js
CHANGED
|
@@ -2,9 +2,14 @@ const path = require('path');
|
|
|
2
2
|
const fs = require('fs');
|
|
3
3
|
const crypto = require('crypto');
|
|
4
4
|
const { execSync } = require('child_process');
|
|
5
|
+
const { Writable } = require('stream');
|
|
5
6
|
|
|
6
7
|
let timer;
|
|
7
8
|
|
|
9
|
+
const features = {
|
|
10
|
+
enableEmit: false,
|
|
11
|
+
};
|
|
12
|
+
|
|
8
13
|
function debounce(fn = () => {}, timeout = 0) {
|
|
9
14
|
if (timer) clearTimeout(timer);
|
|
10
15
|
timer = setTimeout(() => {
|
|
@@ -133,8 +138,7 @@ function getCharset(body, fileName) {
|
|
|
133
138
|
return 'iso-8859-1';
|
|
134
139
|
|
|
135
140
|
try {
|
|
136
|
-
|
|
137
|
-
return out;
|
|
141
|
+
return execSync(`file -b --mime-encoding ${fileName}`).toString('utf8').trim();
|
|
138
142
|
} catch (e) {
|
|
139
143
|
//
|
|
140
144
|
}
|
|
@@ -169,19 +173,54 @@ const staticWorker = (event, callback = () => {}) => {
|
|
|
169
173
|
isModified = false;
|
|
170
174
|
}
|
|
171
175
|
}
|
|
172
|
-
|
|
173
|
-
callback({
|
|
176
|
+
const response = {
|
|
174
177
|
statusCode: isModified ? 200 : 304,
|
|
175
178
|
headers: {
|
|
176
179
|
'Content-Type': `${contentType}${charset ? `; charset=${charset}` : ''}`,
|
|
177
180
|
'Cache-Control': 'public, max-age=0',
|
|
178
|
-
'Content-Length': bodyBuffer.
|
|
181
|
+
'Content-Length': bodyBuffer.byteLength,
|
|
179
182
|
'ETag': currentEtag,
|
|
180
183
|
...(stats.mtime && { 'Last-Modified': lastModified.toUTCString() }),
|
|
181
184
|
},
|
|
182
|
-
body: bodyBuffer.toString('base64'),
|
|
185
|
+
body: isModified ? bodyBuffer.toString('base64') : '',
|
|
183
186
|
isBase64Encoded: true,
|
|
184
|
-
}
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
if (features.enableEmit && isModified) {
|
|
190
|
+
const writer = new Writable({
|
|
191
|
+
/**
|
|
192
|
+
* @param {Buffer} chunk
|
|
193
|
+
* @param {string} encoding
|
|
194
|
+
* @param {Function} next
|
|
195
|
+
*/
|
|
196
|
+
write(chunk, encoding, next) {
|
|
197
|
+
response.emit = true;
|
|
198
|
+
response.isBase64Encoded = true;
|
|
199
|
+
response.body = chunk.toString('base64');
|
|
200
|
+
// console.log('sending', chunk.length, 'bytes')
|
|
201
|
+
callback(response);
|
|
202
|
+
next();
|
|
203
|
+
},
|
|
204
|
+
destroy() {
|
|
205
|
+
response.isBase64Encoded = false;
|
|
206
|
+
response.emit = true;
|
|
207
|
+
response.body = null;
|
|
208
|
+
callback(response);
|
|
209
|
+
},
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
fs.createReadStream(fileName, {
|
|
213
|
+
// encoding: 'binary',
|
|
214
|
+
// encoding: 'utf-8',
|
|
215
|
+
emitClose: false,
|
|
216
|
+
autoClose: false,
|
|
217
|
+
// highWaterMark: 16,
|
|
218
|
+
start: 0,
|
|
219
|
+
end: Infinity,
|
|
220
|
+
}).pipe(writer);
|
|
221
|
+
} else {
|
|
222
|
+
callback(response);
|
|
223
|
+
}
|
|
185
224
|
} else {
|
|
186
225
|
callback({
|
|
187
226
|
statusCode: 404,
|
package/index.d.ts
CHANGED
package/middleware/index.js
CHANGED
|
@@ -215,6 +215,25 @@ const workerMiddleware = (options) => {
|
|
|
215
215
|
response.end();
|
|
216
216
|
}
|
|
217
217
|
|
|
218
|
+
if (responseEvent.type === WORKER_EVENT.RESPONSE_EMIT) {
|
|
219
|
+
worker.postMessage({
|
|
220
|
+
type: WORKER_EVENT.RESPONSE_ACKNOWLEDGE,
|
|
221
|
+
requestId,
|
|
222
|
+
});
|
|
223
|
+
/** @type {ResponseEvent|WSFrameEvent} event */
|
|
224
|
+
const { event } = responseEvent;
|
|
225
|
+
const bufferEncoding = event.isBase64Encoded ? 'base64' : 'utf8';
|
|
226
|
+
|
|
227
|
+
if (!response.headersSent) {
|
|
228
|
+
response.writeHead(event.statusCode, event.headers);
|
|
229
|
+
}
|
|
230
|
+
if (event.body !== null) {
|
|
231
|
+
response.write(Buffer.from(event.body, bufferEncoding).toString());
|
|
232
|
+
} else {
|
|
233
|
+
response.end();
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
218
237
|
if (responseEvent.type === WORKER_EVENT.WS_MESSAGE_SEND) {
|
|
219
238
|
request.socket.write(constructWsMessage(responseEvent.event.frame));
|
|
220
239
|
}
|
|
@@ -15,10 +15,12 @@ function messageListener(message) {
|
|
|
15
15
|
const callback = responseEvent => {
|
|
16
16
|
/** @type {WorkerOutputEvent} e */
|
|
17
17
|
const e = {
|
|
18
|
-
type:
|
|
18
|
+
type: WORKER_EVENT.RESPONSE,
|
|
19
19
|
requestId: message.requestId,
|
|
20
20
|
event: responseEvent,
|
|
21
|
-
}
|
|
21
|
+
};
|
|
22
|
+
if (responseEvent.sendWsMessage) e.type = WORKER_EVENT.WS_MESSAGE_SEND;
|
|
23
|
+
if (responseEvent.emit) e.type = WORKER_EVENT.RESPONSE_EMIT;
|
|
22
24
|
process.send(e);
|
|
23
25
|
};
|
|
24
26
|
worker(message.event, callback);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@koeroesi86/node-worker-express",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "v22.03.12-fd2e588",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"author": {
|
|
@@ -19,12 +19,12 @@
|
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"@koeroesi86/node-worker": "^1.0.3-6bc3bc7",
|
|
22
|
-
"uuid": "^
|
|
22
|
+
"uuid": "^8.3.2"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@types/express": "^4.17.
|
|
26
|
-
"@types/node": "^
|
|
27
|
-
"express": "^4.17.
|
|
28
|
-
"nodemon": "^2.0.
|
|
25
|
+
"@types/express": "^4.17.13",
|
|
26
|
+
"@types/node": "^17.0.21",
|
|
27
|
+
"express": "^4.17.3",
|
|
28
|
+
"nodemon": "^2.0.15"
|
|
29
29
|
}
|
|
30
30
|
}
|
package/utils/workerPool.js
CHANGED
|
@@ -14,7 +14,7 @@ const createWorkerCommand = workerPath => {
|
|
|
14
14
|
switch (ext) {
|
|
15
15
|
case '.js':
|
|
16
16
|
default:
|
|
17
|
-
return `node ${path.resolve(__dirname, '../middleware/workerInvoke.js')} ${workerPath}`;
|
|
17
|
+
return `node --expose-gc ${path.resolve(__dirname, '../middleware/workerInvoke.js')} ${workerPath}`;
|
|
18
18
|
}
|
|
19
19
|
};
|
|
20
20
|
|