node-http-server

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 37  →  ?path2? @ 38
/src/cache.js
@@ -4,71 +4,71 @@
/* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
/*************************************************************************/
 
const fs = require('fs');
const stream = require('stream');
const util = require('util');
const tz = require('moment-timezone');
const forge = require('node-forge');
const EventEmitter = require('events').EventEmitter;
const fs = require('fs')
const stream = require('stream')
const util = require('util')
const tz = require('moment-timezone')
const forge = require('node-forge')
const EventEmitter = require('events').EventEmitter
 
// Cache constructor.
function Cache(config, client, request, response) {
// Create events emitters for logging and data.
EventEmitter.call(this);
EventEmitter.call(this)
 
// Pass through objects needed for caching.
this.config = config;
this.client = client;
this.request = request;
this.response = response;
};
this.config = config
this.client = client
this.request = request
this.response = response
}
 
// Cache handling.
Cache.prototype.process = function(resource, input, type) {
EventEmitter.call(this);
const self = this;
EventEmitter.call(this)
const self = this
 
// Read the resource and cache or not depending on configuration settings.
fs.stat(resource, (error, stats) => {
var expires = 0;
var expires = 0
Object.keys(self.config.site.cache).forEach((key) => {
self.config.site.cache[key].forEach((expire) => {
if (expire.test(resource)) {
expires = key;
expires = key
}
});
});
})
})
 
switch (self.request.httpVersion) {
case '1.1': // HTTP 1.1
self.response.setHeader('Cache-Control',
"max-age=" + expires + ", public"
);
const sha1 = forge.md.sha1.create();
)
const sha1 = forge.md.sha1.create()
const data = new stream.Readable({
objectMode: true,
read(size) {}
});
})
input
.on('data', (chunk) => {
sha1.update(chunk);
data.push(chunk);
sha1.update(chunk)
data.push(chunk)
})
.on('end', () => {
const etag = sha1.digest().toHex();
const etag = sha1.digest().toHex()
 
// Set the ETag for the resource.
self.response.setHeader('ETag', etag);
self.response.setHeader('ETag', etag)
 
const ifNoneMatch = Object
.getOwnPropertyNames(self.request.headers)
.filter((header) => header.toUpperCase() ===
'If-None-Match'.toUpperCase());
'If-None-Match'.toUpperCase())
 
const ifModifiedSince = Object
.getOwnPropertyNames(self.request.headers)
.filter((header) => header.toUpperCase() ===
'If-Modified-Since'.toUpperCase());
'If-Modified-Since'.toUpperCase())
 
if ((ifNoneMatch.length !== 0 &&
self.request.headers[ifNoneMatch].toUpperCase() === etag.toUpperCase()) ||
@@ -82,17 +82,17 @@
' cached resource: ' +
resource,
severity: 'info'
});
})
self.emit('data', {
status: 304,
data: new stream.Readable({
read(size) {
this.push(null);
this.push(null)
}
}),
type: type
});
return;
})
return
}
 
// Send the resource.
@@ -103,28 +103,28 @@
' sent resource: ' +
resource,
severity: 'info'
});
data.push(null);
})
data.push(null)
self.emit('data', {
status: 200,
data: data,
type: type
});
});
})
})
 
return;
return
default:
self.response.setHeader('Last-Modified',
tz(stats.mtime)
.tz('UTC')
.format("ddd, DD MMM YYYY HH:mm:ss z")
);
)
self.response.setHeader('Expires',
tz()
.tz('UTC')
.add(expires, 'seconds')
.format("ddd, DD MMM YYYY HH:mm:ss z")
);
)
// Send the resource.
self.emit('log', {
message: 'Client: ' +
@@ -133,18 +133,18 @@
' sent resource: ' +
resource,
severity: 'info'
});
})
self.emit('data', {
status: 200,
data: input,
type: type
});
break;
})
break
}
});
})
 
return this;
};
return this
}
 
util.inherits(Cache, EventEmitter);
module.exports = Cache;
util.inherits(Cache, EventEmitter)
module.exports = Cache