-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsns-q.js
404 lines (339 loc) · 12.1 KB
/
sns-q.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/**
* sns-q, a promise interface for the AWS Sns SDK
*
* @author Mike MacMillan ([email protected])
*/
//**** CURRENTLY only APNS/APNS_SANDBOX is supported
var Q = require('q'),
_ = require('lodash'),
aws = require('aws-sdk');
var platforms = {
ADM: 'ADM',
GCM: 'GCM',
APNS: 'APNS',
APNSSandbox: 'APNS_SANDBOX'
};
var application = {
/**
* @class application
*/
/**
* creates a platform application object for the specified service
*
* @param {String} name name of the platformApplication object
* @param {String} platform the platform this application object targets (APNS, GCM, ADM)
* @param {Object} attributes additional attributes provided
*
* for more information regarding the additional attributes, see:
* http://docs.aws.amazon.com/sns/latest/api/API_SetPlatformApplicationAttributes.html
*/
create: function(name, platform, attributes) {
var params = {
Name: name,
Platform: platform,
Attributes: attributes
};
return this.svc.createPlatformApplication(params);
},
/**
* gets the platform application object's attributes
*
* @param {String} arn the PlatformApplicationArn of the target platform application
*/
get: function(arn) {
var params = { PlatformApplicationArn: arn };
return this.svc.getPlatformApplicationAttributes(params);
},
/**
* updates the attributes for the specified platformApplicationArn
*
* @param {String} arn the PlatformApplicationArn of the target platform application
* @param {Object} attributes the Attributes map; a collection of fields to update
*/
update: function(arn, attributes) {
var params = {
PlatformApplicationArn: arn,
Attributes: attributes
};
return this.svc.setPlatformApplicationAttributes(params);
},
/**
* returns a list of all platform applications for this account; supports "pagination" if more than 100 results returned
*
* @param {String} token the token returned from the previous call, to access the next set of objects
*/
list: function(token) {
return this.svc.listPlatformApplications(token);
},
/**
* returns a list of all endpoints for the specified platform application
*
* @param {String} arn the PlatformApplicationArn of the application to list endpoints for
* @param {String} token the token returned from the previous call, to access the next set of objects
*/
endpoints: function(arn, token) {
var params = {
PlatformApplicationArn: arn,
NextToken: token
};
return this.svc.listEndpointsByPlatformApplication(params);
},
/**
* deletes the target platform application object
*
* @param {String} arn the PlatformApplicationArn of the target platform application
*/
delete: function(arn) {
var params = { PlatformApplicationArn: arn };
return this.svc.deletePlatformApplication(params);
}
};
var endpoint = {
/**
* creates an endpoint for a device, within the specified application
*
* @param {String} name name of the platformApplication object
* @param {String} platform the platform this application object targets (APNS, GCM, ADM)
* @param {Object} attributes additional attributes provided
*
* for more information regarding the additional attributes, see:
* http://docs.aws.amazon.com/sns/latest/api/API_SetEndpointAttributes.html
*/
create: function(arn, token, data, attributes) {
var params = {
PlatformApplicationArn: arn,
Token: token,
CustomUserData: data,
Attributes: attributes
};
return this.svc.createPlatformEndpoint(params);
},
/**
* gets the endpoint object's attributes
*
* @param {String} arn the EndpointArn of the target endpoint
*/
get: function(arn) {
var params = { EndpointArn: arn };
return this.svc.getEndpointAttributes(params);
},
/**
* updates the attributes for the specified endpoint
*
* @param {String} arn the EndpointArn of the target platform endpoint
* @param {Object} attributes the Attributes map; a collection of fields to update
*/
update: function(arn, attributes) {
var params = {
EndpointArn: arn,
Attributes: attributes
};
return this.svc.setEndpointAttributes(params);
},
/**
* deletes the target platform application object
*
* @param {String} arn the PlatformApplicationArn of the target platform application
*/
delete: function(arn) {
var params = { EndpointArn: arn };
return this.svc.deleteEndpoint(params);
},
/**
* sends a message to the target endpoint. accepts either a string message, or a message object. if
* an object is provided, it is assumed to include the message configuration for each platform intended.
* use an object if you intend to update the badge/count, play a sound, etc. Make sure each
* platforms message object is in string format (JSON.stringify). see here for more details on targetting
* multiple platforms with a single message:
*
* http://docs.aws.amazon.com/sns/latest/dg/mobile-push-send-custommessage.html
*
* also, and object of arguments may be specified, that based on the platform, will be attached to the
* paylod; this can be used for routing, actions, etc
*
* @param {String} arn the EndpointArn of the endpoint that should receive the message
* @param {String} msg the message string to send, or a message object
* @param {Object} args additional arguments to be mixed in with the message object
*/
message: function(arn, msg, args) {
var params = {
Message: typeof(msg) !== 'string' ? JSON.stringify(msg) : {},
MessageStructure: 'json',
TargetArn: arn
};
//** currently in testing, limited to APNS/APNS_SANDBOX
if(typeof(msg) === 'string')
params.Message = messageBuilder(msg, args)
.platforms(this.platforms)
.stringify()
return this.svc.publish(params);
}
};
var topic = {
/**
* creates a topic with the given name, or returns the existing topic
*
* @param {String} name name of the topic
*/
create: function(name) {
var params = { Name: name };
return this.svc.createTopic(params);
},
/**
* gets the topic object's attributes
*
* @param {String} arn the TopicArn of the target topic
*/
get: function(arn) {
var params = { TopicArn: arn };
return this.svc.getTopicAttributes(params);
},
/**
* updates the attributes for the specified topic. only a few of the attributes can be updated, those
* being: Policy, DisplayName, and DeliveryPolicy
*
* @param {String} arn the TopicArn of the target topic
* @param {String} name the name of the attribute to update
* @param {String} value the value of the attribute to update
*/
update: function(arn, name, value) {
var params = {
AttributeName: name,
AttributeValue: value,
TopicArn: arn
};
return this.svc.setTopicAttributes(params);
},
/**
* returns a list of all topics for this account
*
* @param {String} token the token returned from the previous call, to access the next set of objects
*/
list: function(token) {
return this.svc.listTopics(token);
},
/**
* deletes the target topic object
*
* @param {String} arn the TopicArn of the topic to delete
*/
delete: function(arn) {
var params = { TopicArn: arn };
return this.svc.deleteTopic(params);
},
subscribe: function(topicArn, endpoint, protocol) {
var params = {
TopicArn: topicArn,
Protocol: protocol,
Endpoint: endpoint
};
return this.svc.subscribe(params);
},
/**
* shorthand for device specific registrations, assumes the endpoint is an arn, and presets the protocol
* to 'application', which is used for mobile devices
*/
subscribeMobile: function(topicArn, endpointArn) {
var params = {
TopicArn: topicArn,
Protocol: 'application',
Endpoint: endpointArn
};
return this.svc.subscribe(params);
},
message: function(arn, msg, args) {
var params = {
Message: typeof(msg) !== 'string' ? JSON.stringify(msg) : {},
MessageStructure: 'json',
TopicArn: arn
};
//** currently in testing, limited to APNS/APNS_SANDBOX
if(typeof(msg) === 'string')
params.Message = messageBuilder(msg, args)
.platforms(this.platforms)
.stringify()
return this.svc.publish(params);
}
};
/**
* a message builder to abstract the individual format for each platform; supports a message, badges, and custom payloads
*
* @param {String} msg the message we are building platform specific representations of
* @param {Object} args individual platform specific payload arguments (optional)
*/
function messageBuilder(msg, args) {
var message = msg,
supported = [],
badge = 0,
builders = {};
//** currently only APNS is supported
builders[platforms.APNS] = builders[platforms.APNSSandbox] = function() {
return {
aps: {
//** adds the custom arguments onto the "alert" object
alert: _.extend({}, { body: msg }, args||{}),
badge: badge
}
};
}
//** sets the supported platforms
this.platforms = function(list) {
if(!Array.isArray(list)) return;
supported = list;
return this;
}
//** shows/clears badges for the app icon
this.showBadge = function(count) {
badge = count;
return this;
}
this.clearBadge = function() {
badge = 0;
return this;
}
this.toJSON = function() {
//** create the composite message object based on the supported platforms
return _.reduce(supported, function(m, platform) {
if(!!builders[platform])
m[platform] = JSON.stringify(builders[platform]());
return m;
},
//** support the default message inherently; this is required for publishing to topics
{ default: msg });
}
this.stringify = function() { return JSON.stringify(this.toJSON()); }
return this;
}
//** simple wrapper function for introducing a promise interface
function wrap(ctx, fn, key) {
return function() {
var def = Q.defer(),
args = _.toArray(arguments);
args.push(def.makeNodeResolver());
fn.apply(ctx, args);
return def.promise;
}
}
function snsQ(opt) {
//** proxy the options unchanged to the aws-sns sdk for creation
this.sns = new aws.SNS((opt=opt||{}))
this.svc = {};
//** default the sandbox status and platform support, if not specified
this.sandbox = opt.sandbox === true;
this.platforms = opt.platforms||[platforms.APNS, platforms.GCM, platforms.ADM];
//** create a curried version of each sns sdk api function, introducing a promise, hoisting it to a service object
_.each(this.sns.constructor.prototype, function(obj, key) {
if(typeof(obj) !== 'function' || key == 'constructor') return;
this.svc[key] = wrap(this.sns, obj, key);
}.bind(this));
var scope = function(ctx, fn, key) {
ctx[key] = fn.bind(this);
return ctx;
}.bind(this);
//** scope each of the individual service libs
this.application = _.reduce(application, scope, {});
this.endpoint = _.reduce(endpoint, scope, {});
this.topic = _.reduce(topic, scope, {});
this.messageBuilder = messageBuilder;
}
module.exports = snsQ;