/
pantry.coffee
207 lines (156 loc) · 6.18 KB
/
pantry.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
EventEmitter = require('events').EventEmitter
url = require 'url'
querystring = require 'querystring'
request = require 'request'
xml2js = require 'xml2js'
Log = require 'coloured-log'
log = new Log()
currentStock = {} # key/value list of all current items in stock
stockCount = 0 # number of items currently in stock
config = { shelfLife: 60, maxLife: 300, capacity: 1000, ideal: 900, caseSensitive: true, verbosity: "INFO"}
log.notice "Initializing the Pantry"
log.info "Configuration: shelfLife=#{config.shelfLife}, maxLife=#{config.maxLife}, capacity=#{config.capacity}, ideal=#{config.ideal}"
# update configuration and defaults
@configure = (options) ->
config[k] = v for k, v of options
# recalculate new ideal capacity (unless alternate and valid ideal has been specified)
config.ideal = (config.capacity * 0.9) unless options.ideal and config.ideal <= (config.capacity * 0.9)
log.notice "Updated configuration"
log.info "Configuration: shelfLife=#{config.shelfLife}, maxLife=#{config.maxLife}, capacity=#{config.capacity}, ideal=#{config.ideal}"
return config
# retrieve a specific resource
@fetch = (options, callback) ->
# apply default options
options[k] ?= v for k, v of config
# if no key is specified then use the uri as the unique cache key
options.key ?= normalizeURL(options.uri, options.caseSensitive)
# get/create new stocked item
stockedItem = getItem(options)
if stockedItem.hasSpoiled()
# if the item has expired (or currently unavailable) then
# the callback must wait until we have updated data
stockedItem.once 'stocked', (error, results) =>
callback(error, results)
else
callback(null, stockedItem.results)
# request an update if expired (or spoiled)
stockedItem.fetch(options) if stockedItem.hasExpired()
@getStock = (callback) ->
stock = {}
stock.currentStock = currentStock
stock.stockCount = stockCount
callback null, stock
normalizeURL = (value, caseSensitive = false) ->
uri = url.parse value, true
keys = []
for k of uri.query
keys.push k
keys.sort()
query = {}
for k in keys
if uri.query.hasOwnProperty(k)
query[if caseSensitive then k else k.toLowerCase()] = uri.query[k]
uri.search = querystring.stringify(query)
uri.pathname = uri.pathname.toLowerCase() unless caseSensitive
url.format uri
getItem = (options) ->
if not currentStock[options.key]?
currentStock[options.key] = new StockedItem(options)
stockCount++
cleanUp()
return currentStock[options.key]
removeItem = (key) ->
delete currentStock[key]
stockCount--
cleanUp = () ->
if stockCount > config.capacity
log.warning "We're over capacity #{stockCount} / #{config.ideal}. Time to clean up the pantry"
expired = [] # used for efficient to prevent possibly looping through a second time
# remove spoiled items
for key, item of currentStock when item.results?
if item.hasSpoiled()
log.info "Spoiled #{key}"
removeItem key
else if item.hasExpired()
expired.push key
if stockCount > config.capacity
# still over capacity. let's toss out some expired times to make room
for key in expired
log.warning "Expired #{key}"
removeItem key
break if stockCount <= config.ideal
if stockCount > config.capacity
# we have more stuff than we can handle. time to toss some good stuff out
# TODO: likely want to be smarter about which good items we toss
# but without significant overhead
for key, item of currentStock when item.results
log.alert "Tossed #{key}"
removeItem key
break if stockCount <= config.ideal
log.notice "Cleanup complete. Currently have #{stockCount} items in stock"
class StockedItem extends EventEmitter
constructor: (@options) ->
@loading = false
hasExpired: ->
@hasSpoiled() or (new Date()) > @bestBefore
hasSpoiled: ->
not @results? or (new Date()) > @spoilesOn
fetch: (@options)->
# the loading flag is used to ensure only one request for a resource is executed at a time
return if @loading
@loading = true
# set headers for conditional GETs
@options.headers ?= {}
@options.headers['if-none-match'] = @eTag if @eTag
@options.headers['if-modified-since'] = @lastModified if @lastModified
request @options, (error, response, body) =>
# cache-control header max-age value will by default overide configured shelfLife
match = /max-age=(\d+)/.exec(response.headers['cache-control'])
@options.shelfLife = parseInt(match[1]) if match and not @options.ignoreCacheControl
unless error?
switch response.statusCode
when 304 # cached data is still good. keep using it
log.info "Still Good #{options.key}"
@stock(response, null)
when 200 # new data available
log.notice "Re-stocked #{options.key}"
contentType = response.headers["content-type"]
# parse JSON
if @options.parser is 'json' or contentType.indexOf('application/json') is 0
@stock(response, JSON.parse body)
# parse XML
else if @options.parser is 'xml' or contentType.search(/[\/\+]xml/) > 0
# some xml is 'bad' but can be fixed, so let's try
start = body.indexOf('<')
body = body[start...body.length] if start
# now we can parse
parser = new xml2js.Parser()
parser.on 'end', (results) =>
@stock(response, results)
parser.parseString body
# that was unexpected
else
@oops("Invalid Response Type (#{contentType})")
else
# something wrong with the server or the request
@oops("Invalid Response Code (#{response.statusCode})")
stock: (response, results) ->
@loading = false
if @firstPurchased?
@lastPurchased = new Date()
else
@firstPurchased = new Date()
@lastPurchased = new Date(@firstPurchased)
@lastUsed = new Date(@lastPurchased)
@bestBefore = new Date(@lastPurchased)
@bestBefore.setSeconds @bestBefore.getSeconds() + @options.shelfLife
@spoilesOn = new Date(@lastPurchased)
@spoilesOn.setSeconds @spoilesOn.getSeconds() + @options.maxLife
@eTag = response.headers['etag']
@lastModified = response.headers['last-modified']
@results = results if results?
@emit 'stocked', null, @results
oops: (error) ->
@loading = false
log.error "#{error}"
@emit 'stocked', error