Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

koa-static-cache

Package Overview
Dependencies
Maintainers
4
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-static-cache - npm Package Compare versions

Comparing version 1.0.6 to 1.0.7

9

HISTORY.md

@@ -0,1 +1,8 @@

1.0.7 / 2014-03-26
==================
* add options.gzip to control gzip, support stream's gzip
* add gzip support for buffers
1.0.3 / 2014-01-14

@@ -9,2 +16,2 @@ ==================

* use `yield* next`
* use `yield* next`
var crypto = require('crypto')
var fs = require('fs')
var zlib = require('zlib')
var path = require('path')

@@ -9,2 +10,14 @@ var mime = require('mime')

var stat = function (file) {
return function (done) {
fs.stat(file, done)
}
}
function gzip(buf) {
return function (done) {
zlib.gzip(buf, done)
}
}
module.exports = function staticCache(dir, options, files) {

@@ -16,2 +29,3 @@ if (typeof dir !== 'string')

files = files || options.files || Object.create(null)
var enableGzip = !!options.gzip

@@ -34,3 +48,2 @@ readDir(dir).forEach(function (name) {

debug('file: ' + JSON.stringify(obj, null, 2))
if (options.buffer)

@@ -63,4 +76,15 @@ obj.buffer = buffer

this.status = 200
if (!file.buffer) {
var stats = yield stat(file.path)
if (stats.mtime > new Date(file.mtime)) {
file.mtime = stats.mtime.toUTCString()
file.md5 = null
file.length = stats.size
}
}
this.response.lastModified = file.mtime
this.response.etag = file.md5
if (file.md5) this.response.etag = file.md5
if (this.fresh)

@@ -70,5 +94,5 @@ return this.status = 304

this.type = file.type
this.length = file.length
this.length = file.zipBuffer ? file.zipBuffer.length : file.length
this.set('Cache-Control', file.cacheControl || 'public, max-age=' + file.maxAge)
this.set('Content-MD5', file.md5)
if (file.md5) this.set('Content-MD5', file.md5)

@@ -78,10 +102,40 @@ if (this.method === 'HEAD')

if (file.zipBuffer) {
this.set('Content-Encoding', 'gzip')
this.body = file.zipBuffer
return
}
if (file.buffer) {
this.body = file.buffer
if (enableGzip && this.acceptsEncodings('gzip') === 'gzip') {
file.zipBuffer = yield gzip(file.buffer)
this.set('Content-Encoding', 'gzip')
this.body = file.zipBuffer
} else {
this.body = file.buffer
}
return
}
var stream = fs.createReadStream(file.path)
stream.on('error', this.onerror)
// update file hash
if (!file.md5) {
var hash = crypto.createHash('md5')
stream.on('data', hash.update.bind(hash))
stream.on('end', function () {
file.md5 = hash.digest('base64')
})
}
// enable gzip will remove content length
if (enableGzip && this.acceptsEncodings('gzip') === 'gzip') {
this.remove('Content-Length')
this.set('Content-Encoding', 'gzip')
this.body = stream.pipe(zlib.createGzip())
} else {
var stream = this.body = fs.createReadStream(file.path)
stream.on('error', this.onerror)
onFinished(this, stream.destroy.bind(stream))
this.body = stream
}
onFinished(this, stream.destroy.bind(stream))
return

@@ -88,0 +142,0 @@ case 'OPTIONS':

2

package.json
{
"name": "koa-static-cache",
"description": "Static cache for koa",
"version": "1.0.6",
"version": "1.0.7",
"author": {

@@ -6,0 +6,0 @@ "name": "Jonathan Ong",

@@ -35,2 +35,3 @@ # Koa Static Cache [![Build Status](https://travis-ci.org/koajs/static-cache.png)](https://travis-ci.org/koajs/static-cache)

- `options.buffer` (bool) - store the files in memory instead of streaming from the filesystem on each request.
- `options.gzip` (bool) - when request's accept-encoding include gzip, files will compressed by gzip.
- `options.alias` (obj) - object map of aliases. See below.

@@ -37,0 +38,0 @@ - `files` (obj) - optional files object. See below.

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc