🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

memfile

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

memfile

A simplistic in-memory file cache. Not suitable of a huge number of files. It doesn't feature logging which a production implementation requirement.

latest
Source
npmnpm
Version
0.0.3
Version published
Weekly downloads
1
-50%
Maintainers
1
Weekly downloads
 
Created
Source

memfile.js

revision 0.0.3

Overview

An in-memory file cache written for instructional purposes. If you were to extend this for production purposes then you would need to cap memory consumption at some point and attrition out items which were accessed rarely. You would also want logging integration. It might make more sense at that point to integrate with a database backend allowing for a secondary level of caching.

Problems

The onChange() should really be using fs.watch() to detect a change in the file but currently that is listed as unstable in NodeJS version 0.6.15.

I really should inherit form events rather than attach it to the module.

Examples

Below is an example of using memfile with a simplistic web server.

var http = require("http"),
	path = require("path"),
	memfile = require("memfile"),
	mimetype = require("mimetype");

var file_list = [ 
	"htdocs/favicon.png", 
	"htdocs/clock.html", 
	"htdocs/js/clock.js", 
	"htdocs/clock.css"
];

// We're reading in a blocking manner here using setSync().
file_list.forEach(function (filename) {
	// Remember the files, mime type, and update if it changes
	memfile.setSync(filename, {
		mime_type: mimetype.lookup(filename), 
		on_change: true});
});

// Create the web server serving the clock web site
console.log("Starting web server)
http.createServer(function (req, res) {
	var file;

	// Handle special case urls	
	if (req.url === "/" || req.url === "/index.html") {
		req.url = "/clock.html";
	} else if (req.url === "/favicon.ico") {
		req.url = "/favicon.png";
	}

	file = memfile.get(path.join("htdocs", req.url));
	if (file === true) {
		res.writeHead(200, {"Content-Type": file.mime_type)});
		res.end(file.buf);
	} else {
		res.writeHead(404, {"Content-Type": "text/plain"});
		res.end(req.url + " not found.");
	}
}).listen(8080, "localhost");

FAQs

Package last updated on 20 Apr 2012

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts