Code Monkey home page Code Monkey logo

uwebsocket-serve's People

Contributors

charlypoirier avatar kolodziejczak-sz avatar rhrn avatar sup3rfire avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

uwebsocket-serve's Issues

Having trouble serving on a specific route.

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

How about Content-Encoding

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...

Random Issue (not sure why)

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.

path.resolve could be a problem

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);
 },

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.