-
Notifications
You must be signed in to change notification settings - Fork 4
/
bucket.js
129 lines (110 loc) · 2.84 KB
/
bucket.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
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
'use strict';
var EventEmitter = require('events').EventEmitter;
var request = require('./https-request');
var Clock = require('./epoch-clock');
var API_HOST = 'groker.initialstate.com';
function Bucket(name, id, accessKey) {
if (!(this instanceof Bucket)) {
return new Bucket(name, id, accessKey);
}
EventEmitter.call(this);
var bucket = this,
buffer = [],
verified = false,
sending = false,
flushSymbol = null,
bucketData = {
bucketKey: id || name,
bucketName: name || id
},
eventHttp = {
hostname: process.env.IS_API_HOST || API_HOST,
path: '/api/buckets',
method: 'POST',
keepAlive: true,
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Accept-Version': '0.0.1',
'X-IS-AccessKey': accessKey
}
};
bucket.name = bucketData.bucketName;
bucket.id = bucketData.bucketKey;
function pushEvent(event) {
if (event) {
buffer.push(event);
flushSoon();
}
}
this.push = function (key, value, date) {
pushEvent({
key: (typeof key === 'string') ? key : String(key),
value: (typeof value === 'string') ? value : String(value),
epoch: Clock.toTimestamp(date)
});
};
function sent(err) {
sending = false;
// events sent, ready to send more...
if (err) {
bucket.emit('error', err);
return;
}
if (buffer.length > 0) {
flushSoon();
} else {
bucket.emit('drain');
}
}
function flush() {
// send all events
clearImmediate(flushSymbol);
flushSymbol = null;
var len = buffer.length;
if (verified && len > 0) {
sending = true;
var events = buffer.splice(0, len);
request(eventHttp, events, sent);
bucket.emit('data', events);
}
}
function flushSoon() {
// queue new flush, only after flush has completed
if (verified && !sending && flushSymbol == null) {
flushSymbol = setImmediate(flush);
}
}
// Create/verify bucket before sending data
request(eventHttp, bucketData, function (err, status) {
if (err) {
bucket.emit('error', err);
return;
}
// Switch to events endpoint
eventHttp.path = '/api/events';
eventHttp.headers['X-IS-BucketKey'] = bucketData.bucketKey;
// bucket exists, start sending events
verified = true;
if (buffer.length > 0) {
flushSoon();
}
bucket.emit('ready', status === 201);
});
}
Bucket.prototype = Object.create(EventEmitter.prototype, {
constructor: { value: Bucket }
});
module.exports = function createBucket(id, accessKey) {
// (id/name, accessKey)
var name = id;
if (id != null && typeof id !== 'string' && typeof id.id === 'string') {
// ({ name, id }, accessKey) || ({ id, name, accessKey })
name = (typeof id.name === 'string') ? id.name : id.id;
if (typeof id.accessKey === 'string' && !accessKey) {
accessKey = id.accessKey;
}
id = id.id;
}
return new Bucket(name, id, accessKey || process.env.IS_API_ACCESS_KEY || '');
};
module.exports.Bucket = Bucket;