forked from openstf/stf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.js
66 lines (53 loc) · 1.26 KB
/
storage.js
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
var events = require('events')
var util = require('util')
var fs = require('fs')
var uuid = require('uuid')
function Storage() {
events.EventEmitter.call(this)
this.files = Object.create(null)
this.timer = setInterval(this.check.bind(this), 60000)
}
util.inherits(Storage, events.EventEmitter)
Storage.prototype.store = function(file) {
var id = uuid.v4()
this.set(id, file)
return id
}
Storage.prototype.set = function(id, file) {
this.files[id] = {
timeout: 600000
, lastActivity: Date.now()
, data: file
}
return file
}
Storage.prototype.remove = function(id) {
var file = this.files[id]
if (file) {
delete this.files[id]
fs.unlink(file.data.path, function() {})
}
}
Storage.prototype.retrieve = function(id) {
var file = this.files[id]
if (file) {
file.lastActivity = Date.now()
return file.data
}
return null
}
Storage.prototype.check = function() {
var now = Date.now()
Object.keys(this.files).forEach(function(id) {
var file = this.files[id]
var inactivePeriod = now - file.lastActivity
if (inactivePeriod >= file.timeout) {
this.remove(id)
this.emit('timeout', id, file.data)
}
}, this)
}
Storage.prototype.stop = function() {
clearInterval(this.timer)
}
module.exports = Storage