-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b096e27
commit 9cd9989
Showing
4 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
*.sw? | ||
/test/cache/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
var debug = require('debug')('httpism:cache') | ||
var fileStore = require('./fileStore') | ||
var urlUtils = require('url') | ||
|
||
function createStore (options) { | ||
var url = typeof options === 'object' && options.hasOwnProperty('url') ? options.url : undefined | ||
var parsedUrl = urlUtils.parse(url) | ||
var protocol = parsedUrl.protocol || 'file' | ||
|
||
var storeConstructor = storeTypes[protocol] | ||
|
||
if (!storeConstructor) { | ||
throw new Error('no such store for url: ' + url) | ||
} | ||
|
||
return storeConstructor(options) | ||
} | ||
|
||
module.exports = function (options) { | ||
var store = createStore(options) | ||
var isResponseCachable = typeof options === 'object' && | ||
options.hasOwnProperty('isResponseCachable') | ||
? options.isResponseCachable | ||
: function (response) { | ||
return response.statusCode >= 200 && response.statusCode < 400 | ||
} | ||
|
||
var httpismCache = function (req, next) { | ||
var url = req.url | ||
|
||
return store.responseExists(url).then(function (exists) { | ||
if (exists) { | ||
debug('hit', url) | ||
return store.readResponse(url) | ||
} else { | ||
debug('miss', url) | ||
return next().then(function (response) { | ||
if (isResponseCachable(response)) { | ||
return store.writeResponse(url, response) | ||
} else { | ||
return response | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
httpismCache.httpismMiddleware = { | ||
name: 'cache', | ||
before: ['debugLog', 'http'] | ||
} | ||
|
||
httpismCache.middleware = 'cache' | ||
httpismCache.before = ['debugLog', 'http'] | ||
|
||
return httpismCache | ||
} | ||
|
||
var storeTypes = { | ||
file: fileStore | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
var fs = require('fs-promise') | ||
var PassThrough = require('stream').PassThrough | ||
var urlUtils = require('url') | ||
|
||
function writeStreamToFile (filename, stream) { | ||
return new Promise(function (resolve, reject) { | ||
var file = fs.createWriteStream(filename) | ||
stream.on('error', reject) | ||
stream.on('end', resolve) | ||
stream.pipe(file) | ||
}) | ||
} | ||
|
||
module.exports = function (options) { | ||
var url = typeof options === 'object' && options.hasOwnProperty('url') ? options.url : undefined | ||
var parsedUrl = urlUtils.parse(url) | ||
var path = parsedUrl.path | ||
|
||
return { | ||
filename: function (url) { | ||
return path + '/' + encodeURIComponent(url) | ||
}, | ||
|
||
responseExists: function (url) { | ||
return fs.exists(this.filename(url)) | ||
}, | ||
|
||
writeResponse: function (url, response) { | ||
var filename = this.filename(url) | ||
var body = response.body | ||
delete response.body | ||
|
||
var fileStream = body.pipe(new PassThrough()) | ||
var responseStream = body.pipe(new PassThrough()) | ||
|
||
var responseJson = JSON.stringify(response, null, 2) | ||
|
||
return fs | ||
.mkdirs(path) | ||
.then(function () { | ||
return fs.writeFile(filename + '.json', responseJson) | ||
}) | ||
.then(function () { | ||
writeStreamToFile(filename, fileStream).catch(function (e) { | ||
console.error((e && e.stack) || e) | ||
}) | ||
|
||
response.body = responseStream | ||
return response | ||
}) | ||
}, | ||
|
||
readResponse: function (url) { | ||
var filename = this.filename(url) | ||
|
||
return fs.readFile(filename + '.json', 'utf-8').then(function (contents) { | ||
var response = JSON.parse(contents) | ||
response.body = fs.createReadStream(filename) | ||
return response | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters