kolodziejczak-sz / uwebsocket-serve Goto Github PK
View Code? Open in Web Editor NEWStatic file serving for uWebSockets.js
Static file serving for uWebSockets.js
This works:
uWS_app.get('/*', serveDir('public'))
can access: mysite.com/somefile.txt
These do not work:
uWS_app.get('/otherRoute', serveDir('public'))
or uWS_app.get('/otherRoute/*', serveDir('public'))
can not access anything at mysite.com/otherRoute/somefile.txt
Can you help me do this with your tool? I don't know if I am doing it wrong, or if I would need to modify your tool at all.
Thanks
This can be an enhancement, you can get the 'accept-encoding' and encode the readStream using zlib based on contentType, I've done something like this in my project:
let acceptEncoding = req.getHeader("accept-encoding");
res.cork(() => {
// add some security headers like Content-Security-Policy ...
if (
!acceptEncoding ||
contentType === "image/jpg" ||
contentType === "image/png" ||
contentType === "image/gif" ||
contentType === "application/zip"
) {
acceptEncoding = ""; // no encoding needed
// add some headers res.writeHeader like Cache-Control: 'public, max-age=604800, immutable' for this kind of resources
} else {
// add some no-store, no-cache headers maybe and encode for other files
if (/\bgzip\b/.test(acceptEncoding)) {
res.writeHeader("Content-Encoding", "gzip");
readStream = zlib.gzipSync(readStream);
} else if (/\bdeflate\b/.test(acceptEncoding)) {
res.writeHeader("Content-Encoding", "deflate");
readStream = zlib.deflateSync(readStream);
}
}
res.end(readStream);
});
Good job with the library, I've searched for something like this and had to do it myself for a past project...
Im using uwebsocket-serve to serve some static files (JSON files)
Everything works fine, however sometimes randomly none of my static files show anymore.
I dont understand why, and to resolve this I have no choice but to restart my application
const uws = require('uWebSockets.js');
const {serveDir} = require('uwebsocket-serve');
const fs = require('fs');
const path = require("path");
let server;
const sslPath = path.resolve(__dirname, '../../ssl');
if (fs.existsSync(sslPath)) {
server = uws.SSLApp({
key_file_name: path.resolve(__dirname, '../../ssl/key.pem'),
cert_file_name: path.resolve(__dirname, '../../ssl/cert.pem'),
});
} else {
server = uws.App();
}
if (c.servesStatic) {
const publicPath = path.resolve(__dirname, '../../../public');
server.get('/*', serveDir(publicPath));
}
server.listen('0.0.0.0', c.port, (ws) => {
if (ws) {
start();
console.log('uWebSockets server listeting on port', c.port);
}
});
function setCorsHeaders(response) {
response.writeHeader('Access-Control-Allow-Origin', '*');
response.writeHeader('Content-Type', 'application/json');
}
server.get("/lib/json/mockups.json", function(response) {
setCorsHeaders(response);
response.end(mockupJsonData);
});
server.get("/lib/json/gamemodeData.json", function(response) {
setCorsHeaders(response);
response.end(JSON.stringify({
gameMode: c.gameModeName,
players: views.length,
code: [c.MODE, c.MODE === "ffa" ? "f" : c.TEAMS, c.secondaryGameMode].join("-"),
ip: c.host + ":" + c.port
}));
});
server.get("/serverData.json", function(response) {
setCorsHeaders(response);
response.end(JSON.stringify({
ok: true,
ip: c.host + ":" + c.port
}));
});
function start() {
server.ws('/*', {
compression: uws.SHARED_COMPRESSOR,
idleTimeout: 32,
maxPayloadLength: 1024 * 1024,
open: (socket) => {
sockets.connect(socket);
},
message: (socket, payload) => {
sockets.incoming(payload, socket);
},
close: (socket) => {
sockets.close(socket);
}
});
}
module.exports = {
server
};
Not sure whats causing the static files to stop showing, but there is 0 error in my application when it happened, and nothing in the logs. Any help/advice would be appreciated.
I think you could use some customized implementation of path.resolve, which does not escape the parent, I've used something like this
function saferesolve(base, target) {
var targetPath = '.' + path.posix.normalize('/' + target);
return path.posix.resolve(base, targetPath);
}
and even
saferesolve(base, target) {
var DANGEROUS_PATH_PATTERN = /(?:^|[\\/])\.\.(?:[\\/]|$)/;
if (target.indexOf('\0') !== -1 || DANGEROUS_PATH_PATTERN.test(target)) return false;
return base + path.posix.normalize('/' + target);
},
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.