Node.js: a shim for overriding "http" and "websocket" modules.

Also added stubs for Server.address()
This was done to prevent crashes in some popular frameworks like express

Supports both CommonJS and the new ES Modules system syntax e.g:

app.js:
const http = require('http')

app.mjs:
import http from "http"

Usage on Node 14.16.x and higher:
{
    "type": "external",
    "processes": {"spare": 0},
    "working_directory": '/project',
    "executable": "/usr/bin/env",
    "arguments": [
        "node",
        "--loader",
        "unit-http/require_shim.mjs"
        "--require",
        "unit-http/require_shim",
        "app.js"
    ]
}

Usage on Node 14.15.x and lower:
{
    "type": "external",
    "processes": {"spare": 0},
    "working_directory": '/project',
    "executable": "/usr/bin/env",
    "arguments": [
        "node",
        "--require",
        "unit-http/require_shim",
        "app.js"
    ]
}
This commit is contained in:
Oisin Canty
2021-05-12 09:26:55 +00:00
parent 07c6bf165d
commit a0c083af20
44 changed files with 288 additions and 78 deletions

3
test/node/404/app.js Executable file → Normal file
View File

@@ -1,7 +1,6 @@
#!/usr/bin/env node
var fs = require('fs');
require('unit-http').createServer(function (req, res) {
require('http').createServer(function (req, res) {
res.writeHead(404, {}).end(fs.readFileSync('404.html'));
}).listen(7080);

3
test/node/basic/app.js Executable file → Normal file
View File

@@ -1,6 +1,5 @@
#!/usr/bin/env node
require('unit-http').createServer(function (req, res) {
require('http').createServer(function (req, res) {
res.writeHead(200, {'Content-Length': 12, 'Content-Type': 'text/plain'})
.end('Hello World\n');
}).listen(7080);

3
test/node/double_end/app.js Executable file → Normal file
View File

@@ -1,5 +1,4 @@
#!/usr/bin/env node
require('unit-http').createServer(function (req, res) {
require('http').createServer(function (req, res) {
res.end().end();
}).listen(7080);

3
test/node/get_header_names/app.js Executable file → Normal file
View File

@@ -1,6 +1,5 @@
#!/usr/bin/env node
require('unit-http').createServer(function (req, res) {
require('http').createServer(function (req, res) {
res.setHeader('DATE', ['date1', 'date2']);
res.setHeader('X-Header', 'blah');
res.setHeader('X-Names', res.getHeaderNames());

3
test/node/get_header_type/app.js Executable file → Normal file
View File

@@ -1,6 +1,5 @@
#!/usr/bin/env node
require('unit-http').createServer(function (req, res) {
require('http').createServer(function (req, res) {
res.setHeader('X-Number', 100);
res.setHeader('X-Type', typeof(res.getHeader('X-Number')));
res.end();

3
test/node/get_variables/app.js Executable file → Normal file
View File

@@ -1,6 +1,5 @@
#!/usr/bin/env node
require('unit-http').createServer(function (req, res) {
require('http').createServer(function (req, res) {
let query = require('url').parse(req.url, true).query;
res.setHeader('X-Var-1', query.var1);
res.setHeader('X-Var-2', query.var2);

3
test/node/has_header/app.js Executable file → Normal file
View File

@@ -1,6 +1,5 @@
#!/usr/bin/env node
require('unit-http').createServer(function (req, res) {
require('http').createServer(function (req, res) {
res.setHeader('X-Has-Header', res.hasHeader(req.headers['x-header']) + '');
res.end();
}).listen(7080);

3
test/node/header_name_case/app.js Executable file → Normal file
View File

@@ -1,6 +1,5 @@
#!/usr/bin/env node
require('unit-http').createServer(function (req, res) {
require('http').createServer(function (req, res) {
res.setHeader('X-Header', '1');
res.setHeader('X-header', '2');
res.setHeader('X-HEADER', '3');

3
test/node/header_name_valid/app.js Executable file → Normal file
View File

@@ -1,6 +1,5 @@
#!/usr/bin/env node
require('unit-http').createServer(function (req, res) {
require('http').createServer(function (req, res) {
res.writeHead(200, {});
res.setHeader('@$', 'test');
res.end();

3
test/node/header_value_object/app.js Executable file → Normal file
View File

@@ -1,6 +1,5 @@
#!/usr/bin/env node
require('unit-http').createServer(function (req, res) {
require('http').createServer(function (req, res) {
res.setHeader('X-Header', {});
res.end();
}).listen(7080);

3
test/node/mirror/app.js Executable file → Normal file
View File

@@ -1,6 +1,5 @@
#!/usr/bin/env node
require('unit-http').createServer(function (req, res) {
require('http').createServer(function (req, res) {
let body = '';
req.on('data', chunk => {
body += chunk.toString();

3
test/node/post_variables/app.js Executable file → Normal file
View File

@@ -1,6 +1,5 @@
#!/usr/bin/env node
require('unit-http').createServer(function (req, res) {
require('http').createServer(function (req, res) {
let body = '';
req.on('data', chunk => {
body += chunk.toString();

3
test/node/promise_end/app.js Executable file → Normal file
View File

@@ -1,8 +1,7 @@
#!/usr/bin/env node
var fs = require('fs');
require('unit-http').createServer(function (req, res) {
require('http').createServer(function (req, res) {
res.write('blah');
Promise.resolve().then(() => {

3
test/node/promise_handler/app.js Executable file → Normal file
View File

@@ -1,8 +1,7 @@
#!/usr/bin/env node
var fs = require('fs');
require('unit-http').createServer(function (req, res) {
require('http').createServer(function (req, res) {
res.end();
if (req.headers['x-write-call']) {

3
test/node/remove_header/app.js Executable file → Normal file
View File

@@ -1,6 +1,5 @@
#!/usr/bin/env node
require('unit-http').createServer(function (req, res) {
require('http').createServer(function (req, res) {
res.setHeader('X-Header', 'test');
res.setHeader('Was-Header', res.hasHeader('X-Header').toString());

View File

@@ -0,0 +1,6 @@
import http from "http"
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Length': 12, 'Content-Type': 'text/plain'})
.end('Hello World\n');
}).listen(7080);

View File

@@ -0,0 +1 @@
import("./module.mjs")

View File

@@ -0,0 +1,6 @@
import http from "http"
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Length': 12, 'Content-Type': 'text/plain'})
.end('Hello World\n');
}).listen(7080);

View File

@@ -0,0 +1,30 @@
import http from "http"
import websocket from "websocket"
let server = http.createServer(function() {});
let webSocketServer = websocket.server;
server.listen(7080, function() {});
var wsServer = new webSocketServer({
maxReceivedMessageSize: 0x1000000000,
maxReceivedFrameSize: 0x1000000000,
fragmentOutgoingMessages: false,
fragmentationThreshold: 0x1000000000,
httpServer: server,
});
wsServer.on('request', function(request) {
var connection = request.accept(null);
connection.on('message', function(message) {
if (message.type === 'utf8') {
connection.send(message.utf8Data);
} else if (message.type === 'binary') {
connection.send(message.binaryData);
}
});
connection.on('close', function(r) {});
});

View File

@@ -0,0 +1 @@
import("./module.mjs")

View File

@@ -0,0 +1,30 @@
import http from "http"
import websocket from "websocket"
let server = http.createServer(function() {});
let webSocketServer = websocket.server;
server.listen(7080, function() {});
var wsServer = new webSocketServer({
maxReceivedMessageSize: 0x1000000000,
maxReceivedFrameSize: 0x1000000000,
fragmentOutgoingMessages: false,
fragmentationThreshold: 0x1000000000,
httpServer: server,
});
wsServer.on('request', function(request) {
var connection = request.accept(null);
connection.on('message', function(message) {
if (message.type === 'utf8') {
connection.send(message.utf8Data);
} else if (message.type === 'binary') {
connection.send(message.binaryData);
}
});
connection.on('close', function(r) {});
});

View File

@@ -0,0 +1 @@
require("./transitive_http")

View File

@@ -0,0 +1,8 @@
const http = require("http");
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Length': 12, 'Content-Type': 'text/plain'})
.end('Hello World\n');
}).listen(7080);
module.exports = http;

View File

@@ -0,0 +1,4 @@
require("unit-http").createServer(function (req, res) {
res.writeHead(200, {'Content-Length': 12, 'Content-Type': 'text/plain'})
.end('Hello World\n');
}).listen(7080);

3
test/node/set_header_array/app.js Executable file → Normal file
View File

@@ -1,6 +1,5 @@
#!/usr/bin/env node
require('unit-http').createServer(function (req, res) {
require('http').createServer(function (req, res) {
res.setHeader('Set-Cookie', ['tc=one,two,three', 'tc=four,five,six']);
res.end();
}).listen(7080);

3
test/node/status_message/app.js Executable file → Normal file
View File

@@ -1,5 +1,4 @@
#!/usr/bin/env node
require('unit-http').createServer(function (req, res) {
require('http').createServer(function (req, res) {
res.writeHead(200, 'blah', {'Content-Type': 'text/plain'}).end();
}).listen(7080);

3
test/node/update_header/app.js Executable file → Normal file
View File

@@ -1,6 +1,5 @@
#!/usr/bin/env node
require('unit-http').createServer(function (req, res) {
require('http').createServer(function (req, res) {
res.setHeader('X-Header', 'test');
res.setHeader('X-Header', 'new');
res.end();

3
test/node/variables/app.js Executable file → Normal file
View File

@@ -1,6 +1,5 @@
#!/usr/bin/env node
require('unit-http').createServer(function (req, res) {
require('http').createServer(function (req, res) {
let body = '';
req.on('data', chunk => {
body += chunk.toString();

7
test/node/websockets/mirror/app.js Executable file → Normal file
View File

@@ -1,9 +1,6 @@
#!/usr/bin/env node
server = require('unit-http').createServer(function() {});
webSocketServer = require('unit-http/websocket').server;
//server = require('http').createServer(function() {});
//webSocketServer = require('websocket').server;
server = require('http').createServer(function() {});
webSocketServer = require('websocket').server;
server.listen(7080, function() {});

7
test/node/websockets/mirror_fragmentation/app.js Executable file → Normal file
View File

@@ -1,9 +1,6 @@
#!/usr/bin/env node
server = require('unit-http').createServer(function() {});
webSocketServer = require('unit-http/websocket').server;
//server = require('http').createServer(function() {});
//webSocketServer = require('websocket').server;
server = require('http').createServer(function() {});
webSocketServer = require('websocket').server;
server.listen(7080, function() {});

3
test/node/write_before_write_head/app.js Executable file → Normal file
View File

@@ -1,6 +1,5 @@
#!/usr/bin/env node
require('unit-http').createServer(function (req, res) {
require('http').createServer(function (req, res) {
res.write('blah');
res.writeHead(200, {'Content-Type': 'text/plain'}).end();
}).listen(7080);

3
test/node/write_buffer/app.js Executable file → Normal file
View File

@@ -1,6 +1,5 @@
#!/usr/bin/env node
require('unit-http').createServer(function (req, res) {
require('http').createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'})
.end(new Buffer([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]));
}).listen(7080);

3
test/node/write_callback/app.js Executable file → Normal file
View File

@@ -1,8 +1,7 @@
#!/usr/bin/env node
var fs = require('fs');
require('unit-http').createServer(function (req, res) {
require('http').createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
var a = 'world';
res.write('hello', 'utf8', function() {

3
test/node/write_multiple/app.js Executable file → Normal file
View File

@@ -1,6 +1,5 @@
#!/usr/bin/env node
require('unit-http').createServer(function (req, res) {
require('http').createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain', 'Content-Length': 14});
res.write('write');
res.write('write2');

3
test/node/write_return/app.js Executable file → Normal file
View File

@@ -1,6 +1,5 @@
#!/usr/bin/env node
require('unit-http').createServer(function (req, res) {
require('http').createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'})
.end(res.write('body').toString());
}).listen(7080);