-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
225 lines (194 loc) · 6.13 KB
/
index.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
var url = require( "url" );
var util = require( "util" );
var http = require( "http" );
var https = require( "https" );
var events = require( "events" );
var qs = require( "querystring" );
var pkg = require( "./package.json" );
var MAXSIZE = 1024 * 250; // 250kib
var FLUSH_TIMEOUT = 2 * 1000; // 2 seconds
module.exports.SDK = SDK;
util.inherits( SDK, events.EventEmitter );
function SDK ( apikey, apisecret ) {
events.EventEmitter.call( this );
// api-key: ACCOUNT/RAND1
// api-secret: BASE64( RAND2/UUID/AWSACCOUNT/REGION )
// queue: sdk-ACCOUNT-RAND2
var account = apikey.split( "/" )[ 0 ];
var decoded = new Buffer( apisecret, "base64" ).toString().split( "/" );
var rand = decoded[ 0 ];
var awsaccount = decoded[ 2 ];
var region = decoded[ 3 ];
this.apikey = apikey;
this.apisecret = apisecret;
this.logger = console.log;
this._buffer = "";
this._timeout = null;
this.qurl = [
"https://sqs." + region + ".amazonaws.com",
awsaccount,
"sdk-" + account + "-" + rand
].join( "/" );
this.flushcnt = 0;
this.flushid = 0;
this.eventscnt = 0;
this._log( "Created. Version: " + pkg.version );
this._log( "Queue URL: ", this.qurl );
this.on( "error", function ( err ) {
this._log( "ERROR #" + err.id + ":", err );
})
this.on( "warn", function ( err ) {
this._log( "WARN #" + err.id + ":", err );
})
this.on( "send", function ( data ) {
this._log( "Sending #" + data.id + ":", "tries: " + data.tries + " ,",
data.count, "events",
"(" + data.size + " bytes)",
this.flushcnt, "flushes remaining"
);
})
this.on( "flush", function ( data ) {
this._log( "Sent Successfuly #" + data.id + ":",
data.count, "events",
"(" + data.size + " bytes)",
"in", data.t + "s",
this.flushcnt, "flushes remaining"
);
})
this.on( "empty", function () {
this.flushcnt = 0;
this._log( "Empty" );
})
}
SDK.prototype.write = function ( table, data ) {
data = copy( data );
data.__table = table;
this._buffer += encodeURIComponent( JSON.stringify( data ) + "\n" );
this.eventscnt += 1;
if ( this._buffer.length > MAXSIZE ) {
this.flush(); // max size is exceeded, flush immediately
} else if ( !this._timeout ) {
// flush something after the flush timeout
this._timeout = setTimeout( this.flush.bind( this ), FLUSH_TIMEOUT );
}
return this;
}
SDK.prototype._log = function () {
if ( !this.logger ) {
return;
}
var args = [].slice.call( arguments );
args = [ new Date().toISOString(), "COLOR-SDK" ].concat( args );
this.logger.apply( null, args );
}
SDK.prototype.flush = function () {
clearTimeout( this._timeout );
this._timeout = null;
if ( !this._buffer.length ) {
if ( !this.flushcnt ) {
this.emit( "empty" )
}
return
}
var that = this;
var done = false;
var retries = 3;
var count = this.eventscnt;
var buffer = this._buffer;
var flushid = ++this.flushid;
var size = buffer.length;
this.eventscnt = 0;
this.flushcnt += 1;
this._buffer = "";
var time = new Date().toISOString();
var body = [
"Action=SendMessage",
"MessageBody=" + buffer,
"MessageAttribute.1.Name=key",
"MessageAttribute.1.Value.DataType=String",
"MessageAttribute.1.Value.StringValue=" + this.apikey,
"MessageAttribute.2.Name=secret",
"MessageAttribute.2.Value.DataType=String",
"MessageAttribute.2.Value.StringValue=" + this.apisecret,
"MessageAttribute.3.Name=sdk",
"MessageAttribute.3.Value.DataType=String",
"MessageAttribute.3.Value.StringValue=" + pkg.name + "-" + pkg.version,
].join( "&" );
var parsedurl = url.parse( this.qurl )
var options = {
port: 443,
method: "POST",
path: parsedurl.path,
hostname: parsedurl.hostname,
headers: {
"Content-Length": body.length,
"Content-Type": "application/x-www-form-urlencoded"
}
}
var t;
var tries = 0;
request();
function request() {
t = new Date().getTime();
that.emit( "send", {
id: flushid,
count: count,
size: size,
fullsize: body.length,
tries: ++tries
});
var req = https.request( options, function ( res ) {
var code = res.statusCode;
var body = "";
res
.on( "error", onerror )
.on( "data", function ( d ) { body += d.toString(); } )
.on( "end", function () {
if ( code < 200 || code > 300 ) {
var err = [
code, ":", http.STATUS_CODES[ code ], body
].join( " " );
onerror( new Error( err ) );
} else {
onend();
}
})
})
.on( "error", onerror )
req.setTimeout( 60 * 1000 );
req.end( body );
}
function onerror ( err ) {
err.id = flushid;
that.emit( "warn", err );
if ( done ) return;
if ( retries-- > 0 ) {
that._log( "Retrying after error, in 2s. Remaining: ", retries );
return setTimeout( request, 2000 );
}
that.emit( "error", err );
if ( --that.flushcnt <= 0 && !that._buffer ) {
that.emit( "empty" )
}
done = true;
}
function onend() {
t = ( new Date().getTime() - t ) / 1000;
that.emit( "flush", {
id: flushid,
count: count,
size: size,
fullsize: body.length,
t: t
});
if ( done ) return;
if ( --that.flushcnt <= 0 && !that._buffer ) {
that.emit( "empty" )
}
done = true;
}
return this;
}
function copy ( obj ) {
return JSON.parse( JSON.stringify( obj ) );
}