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:
Executable → Regular
+1
-2
@@ -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);
|
||||
|
||||
Executable → Regular
+1
-2
@@ -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);
|
||||
|
||||
Executable → Regular
+1
-2
@@ -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);
|
||||
|
||||
Executable → Regular
+1
-2
@@ -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());
|
||||
|
||||
Executable → Regular
+1
-2
@@ -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();
|
||||
|
||||
Executable → Regular
+1
-2
@@ -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);
|
||||
|
||||
Executable → Regular
+1
-2
@@ -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);
|
||||
|
||||
Executable → Regular
+1
-2
@@ -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');
|
||||
|
||||
Executable → Regular
+1
-2
@@ -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();
|
||||
|
||||
Executable → Regular
+1
-2
@@ -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);
|
||||
|
||||
Executable → Regular
+1
-2
@@ -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();
|
||||
|
||||
Executable → Regular
+1
-2
@@ -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();
|
||||
|
||||
Executable → Regular
+1
-2
@@ -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(() => {
|
||||
|
||||
Executable → Regular
+1
-2
@@ -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']) {
|
||||
|
||||
Executable → Regular
+1
-2
@@ -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());
|
||||
|
||||
|
||||
@@ -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);
|
||||
@@ -0,0 +1 @@
|
||||
import("./module.mjs")
|
||||
@@ -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);
|
||||
@@ -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) {});
|
||||
});
|
||||
@@ -0,0 +1 @@
|
||||
import("./module.mjs")
|
||||
@@ -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) {});
|
||||
});
|
||||
@@ -0,0 +1 @@
|
||||
require("./transitive_http")
|
||||
@@ -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;
|
||||
@@ -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);
|
||||
Executable → Regular
+1
-2
@@ -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);
|
||||
|
||||
Executable → Regular
+1
-2
@@ -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);
|
||||
|
||||
Executable → Regular
+1
-2
@@ -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();
|
||||
|
||||
Executable → Regular
+1
-2
@@ -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();
|
||||
|
||||
Executable → Regular
+2
-5
@@ -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() {});
|
||||
|
||||
|
||||
Executable → Regular
+2
-5
@@ -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() {});
|
||||
|
||||
|
||||
Executable → Regular
+1
-2
@@ -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);
|
||||
|
||||
Executable → Regular
+1
-2
@@ -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);
|
||||
|
||||
Executable → Regular
+1
-2
@@ -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() {
|
||||
|
||||
Executable → Regular
+1
-2
@@ -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');
|
||||
|
||||
Executable → Regular
+1
-2
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user