Permalink
Browse files

cache: support for caching

  • Loading branch information...
1 parent e4afd40 commit 59fbe80153b1d76878ea2ef520529e77335962d6 @LinusU committed Mar 28, 2013
Showing with 33 additions and 1 deletion.
  1. +6 −1 lib/assman.coffee
  2. +27 −0 lib/cache.coffee
View
7 lib/assman.coffee
@@ -1,8 +1,10 @@
path = require 'path'
+cache = require './cache'
compiler = require './compiler'
state =
+ mode: (process.env.NODE_ENV || 'development')
top: ''
map: {}
@@ -12,5 +14,8 @@ module.exports = exports =
state.map['/' + name + '.' + type] = { type: type, name: name, assets: assets.map (e) -> path.join state.top, e }
middleware: (req, res, next) ->
if state.map[req.url]
- compiler res, state.map[req.url].type, state.map[req.url].assets
+ if state.mode is 'production'
+ cache res, state.map[req.url]
+ else
+ compiler res, state.map[req.url].type, state.map[req.url].assets
else do next
View
27 lib/cache.coffee
@@ -0,0 +1,27 @@
+
+compiler = require './compiler'
+
+module.exports = exports = (res, obj) ->
+ if obj.cache
+ obj.cache.push res
+ else
+ data = ''
+ headers = []
+ obj.cache = []
+ fakeRes =
+ setHeader: (key, val) ->
+ headers.push([key, val])
+ res.setHeader key, val
+ write: (d) ->
+ data += d.toString()
+ res.write d
+ end: ->
+ res.end()
+ arr = obj.cache
+ obj.cache =
+ push: (res) ->
+ headers.forEach ([k, v]) -> res.setHeader k, v
+ res.send 200, data
+ arr.forEach obj.cache.push
+
+ compiler fakeRes, obj.type, obj.assets

0 comments on commit 59fbe80

Please sign in to comment.