refactor router methods to use rest parameters for handler functions
This commit is contained in:
		
							
								
								
									
										82
									
								
								dist/router.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										82
									
								
								dist/router.js
									
									
									
									
										vendored
									
									
								
							@@ -77,28 +77,28 @@ export default class Router {
 | 
			
		||||
            this.tpl = obj;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    get(path, cb) {
 | 
			
		||||
        this.registerRoute(path, "get", cb);
 | 
			
		||||
    get(path, ...callback) {
 | 
			
		||||
        this.registerRoute(path, "get", callback);
 | 
			
		||||
        return this;
 | 
			
		||||
    }
 | 
			
		||||
    post(path, cb) {
 | 
			
		||||
        this.registerRoute(path, "post", cb);
 | 
			
		||||
    post(path, ...callback) {
 | 
			
		||||
        this.registerRoute(path, "post", callback);
 | 
			
		||||
        return this;
 | 
			
		||||
    }
 | 
			
		||||
    head(path, cb) {
 | 
			
		||||
        this.registerRoute(path, "head", cb);
 | 
			
		||||
    head(path, ...callback) {
 | 
			
		||||
        this.registerRoute(path, "head", callback);
 | 
			
		||||
        return this;
 | 
			
		||||
    }
 | 
			
		||||
    put(path, cb) {
 | 
			
		||||
        this.registerRoute(path, "put", cb);
 | 
			
		||||
    put(path, ...callback) {
 | 
			
		||||
        this.registerRoute(path, "put", callback);
 | 
			
		||||
        return this;
 | 
			
		||||
    }
 | 
			
		||||
    delete(path, cb) {
 | 
			
		||||
        this.registerRoute(path, "delete", cb);
 | 
			
		||||
    delete(path, ...callback) {
 | 
			
		||||
        this.registerRoute(path, "delete", callback);
 | 
			
		||||
        return this;
 | 
			
		||||
    }
 | 
			
		||||
    patch(path, cb) {
 | 
			
		||||
        this.registerRoute(path, "patch", cb);
 | 
			
		||||
    patch(path, ...callback) {
 | 
			
		||||
        this.registerRoute(path, "patch", callback);
 | 
			
		||||
        return this;
 | 
			
		||||
    }
 | 
			
		||||
    registerRoute(path, method, handler) {
 | 
			
		||||
@@ -157,37 +157,37 @@ export default class Router {
 | 
			
		||||
    static({ dir = path.resolve() + "/public", route = /^\/public/ }) {
 | 
			
		||||
        if (!this.mimes.size)
 | 
			
		||||
            this.readMimes();
 | 
			
		||||
        this.get(route, [(req, res) => {
 | 
			
		||||
                try {
 | 
			
		||||
                    const filename = req.url.pathname.replace(route, "") || "index.html";
 | 
			
		||||
                    const mime = this.mimes.get(filename.split(".").pop() || "");
 | 
			
		||||
                    const file = path.join(dir, filename);
 | 
			
		||||
                    const stat = fs.statSync(file);
 | 
			
		||||
                    if (req.headers.range) {
 | 
			
		||||
                        const [startStr, endStr] = req.headers.range.replace(/bytes=/, "").split("-");
 | 
			
		||||
                        const start = parseInt(startStr, 10);
 | 
			
		||||
                        const end = endStr ? parseInt(endStr, 10) : stat.size - 1;
 | 
			
		||||
                        res.writeHead(206, {
 | 
			
		||||
                            "Content-Range": `bytes ${start}-${end}/${stat.size}`,
 | 
			
		||||
                            "Accept-Ranges": "bytes",
 | 
			
		||||
                            "Content-Length": end - start + 1,
 | 
			
		||||
                            "Content-Type": mime,
 | 
			
		||||
                        });
 | 
			
		||||
                        fs.createReadStream(file, { start, end }).pipe(res);
 | 
			
		||||
                    }
 | 
			
		||||
                    else {
 | 
			
		||||
                        res.writeHead(200, {
 | 
			
		||||
                            "Content-Length": stat.size,
 | 
			
		||||
                            "Content-Type": mime,
 | 
			
		||||
                        });
 | 
			
		||||
                        fs.createReadStream(file).pipe(res);
 | 
			
		||||
                    }
 | 
			
		||||
        this.get(route, (req, res, next) => {
 | 
			
		||||
            try {
 | 
			
		||||
                const filename = req.url.pathname.replace(route, "") || "index.html";
 | 
			
		||||
                const mime = this.mimes.get(filename.split(".").pop() || "");
 | 
			
		||||
                const file = path.join(dir, filename);
 | 
			
		||||
                const stat = fs.statSync(file);
 | 
			
		||||
                if (req.headers.range) {
 | 
			
		||||
                    const [startStr, endStr] = req.headers.range.replace(/bytes=/, "").split("-");
 | 
			
		||||
                    const start = parseInt(startStr, 10);
 | 
			
		||||
                    const end = endStr ? parseInt(endStr, 10) : stat.size - 1;
 | 
			
		||||
                    res.writeHead(206, {
 | 
			
		||||
                        "Content-Range": `bytes ${start}-${end}/${stat.size}`,
 | 
			
		||||
                        "Accept-Ranges": "bytes",
 | 
			
		||||
                        "Content-Length": end - start + 1,
 | 
			
		||||
                        "Content-Type": mime,
 | 
			
		||||
                    });
 | 
			
		||||
                    fs.createReadStream(file, { start, end }).pipe(res);
 | 
			
		||||
                }
 | 
			
		||||
                catch (err) {
 | 
			
		||||
                    console.error(err);
 | 
			
		||||
                    res.reply({ code: 404, body: "404 - File not found" });
 | 
			
		||||
                else {
 | 
			
		||||
                    res.writeHead(200, {
 | 
			
		||||
                        "Content-Length": stat.size,
 | 
			
		||||
                        "Content-Type": mime,
 | 
			
		||||
                    });
 | 
			
		||||
                    fs.createReadStream(file).pipe(res);
 | 
			
		||||
                }
 | 
			
		||||
            }]);
 | 
			
		||||
            }
 | 
			
		||||
            catch (err) {
 | 
			
		||||
                console.error(err);
 | 
			
		||||
                res.reply({ code: 404, body: "404 - File not found" });
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
        return this;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user