| 
									
										
										
										
											2018-04-25 17:41:20 +00:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var Devices = module.exports; | 
					
						
							|  |  |  | Devices.add = function (store, servername, newDevice) { | 
					
						
							| 
									
										
										
										
											2018-06-30 23:09:38 +00:00
										 |  |  |   if (!store[servername]) { | 
					
						
							|  |  |  |     store[servername] = []; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   var devices = store[servername]; | 
					
						
							| 
									
										
										
										
											2018-04-25 17:41:20 +00:00
										 |  |  |   devices.push(newDevice); | 
					
						
							| 
									
										
										
										
											2018-06-30 23:09:38 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | Devices.alias = function (store, servername, alias) { | 
					
						
							|  |  |  |   if (!store[servername]) { | 
					
						
							|  |  |  |     store[servername] = []; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (!store[servername]._primary) { | 
					
						
							|  |  |  |     store[servername]._primary = servername; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (!store[servername].aliases) { | 
					
						
							|  |  |  |     store[servername].aliases = {}; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   store[alias] = store[servername]; | 
					
						
							|  |  |  |   store[servername].aliases[alias] = true; | 
					
						
							| 
									
										
										
										
											2018-04-25 17:41:20 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | Devices.remove = function (store, servername, device) { | 
					
						
							|  |  |  |   var devices = store[servername] || []; | 
					
						
							|  |  |  |   var index = devices.indexOf(device); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (index < 0) { | 
					
						
							|  |  |  |     console.warn('attempted to remove non-present device', device.deviceId, 'from', servername); | 
					
						
							|  |  |  |     return null; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return devices.splice(index, 1)[0]; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | Devices.list = function (store, servername) { | 
					
						
							| 
									
										
										
										
											2018-06-30 23:09:38 +00:00
										 |  |  |   // efficient lookup first
 | 
					
						
							| 
									
										
										
										
											2018-04-25 17:41:20 +00:00
										 |  |  |   if (store[servername] && store[servername].length) { | 
					
						
							| 
									
										
										
										
											2018-06-30 23:09:38 +00:00
										 |  |  |     return store[servername]._primary && store[store[servername]._primary] || store[servername]; | 
					
						
							| 
									
										
										
										
											2018-04-25 17:41:20 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-06-30 23:09:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-25 17:41:20 +00:00
										 |  |  |   // There wasn't an exact match so check any of the wildcard domains, sorted longest
 | 
					
						
							|  |  |  |   // first so the one with the biggest natural match with be found first.
 | 
					
						
							|  |  |  |   var deviceList = []; | 
					
						
							|  |  |  |   Object.keys(store).filter(function (pattern) { | 
					
						
							|  |  |  |     return pattern[0] === '*' && store[pattern].length; | 
					
						
							|  |  |  |   }).sort(function (a, b) { | 
					
						
							|  |  |  |     return b.length - a.length; | 
					
						
							|  |  |  |   }).some(function (pattern) { | 
					
						
							| 
									
										
										
										
											2018-06-30 23:09:38 +00:00
										 |  |  |     // '.example.com' = '*.example.com'.split(1)
 | 
					
						
							| 
									
										
										
										
											2018-04-25 17:41:20 +00:00
										 |  |  |     var subPiece = pattern.slice(1); | 
					
						
							| 
									
										
										
										
											2018-06-30 23:09:38 +00:00
										 |  |  |     // '.com' = 'sub.example.com'.slice(-4)
 | 
					
						
							|  |  |  |     // '.example.com' = 'sub.example.com'.slice(-12)
 | 
					
						
							| 
									
										
										
										
											2018-04-25 17:41:20 +00:00
										 |  |  |     if (subPiece === servername.slice(-subPiece.length)) { | 
					
						
							| 
									
										
										
										
											2018-06-30 23:09:38 +00:00
										 |  |  |       console.log('[Devices.list] "'+servername+'" matches "'+pattern+'"'); | 
					
						
							| 
									
										
										
										
											2018-04-25 17:41:20 +00:00
										 |  |  |       deviceList = store[pattern]; | 
					
						
							| 
									
										
										
										
											2018-06-30 23:09:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // Devices.alias(store, '*.example.com', 'sub.example.com'
 | 
					
						
							|  |  |  |       // '*.example.com' retrieves a reference to 'example.com'
 | 
					
						
							|  |  |  |       // and this reference then also referenced by 'sub.example.com'
 | 
					
						
							|  |  |  |       // Hence this O(n) check is replaced with the O(1) check above
 | 
					
						
							|  |  |  |       Devices.alias(store, pattern, servername); | 
					
						
							| 
									
										
										
										
											2018-04-25 17:41:20 +00:00
										 |  |  |       return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return deviceList; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | Devices.exist = function (store, servername) { | 
					
						
							|  |  |  |   return !!(Devices.list(store, servername).length); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | Devices.next = function (store, servername) { | 
					
						
							|  |  |  |   var devices = Devices.list(store, servername); | 
					
						
							|  |  |  |   var device; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (devices._index >= devices.length) { | 
					
						
							|  |  |  |     devices._index = 0; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   device = devices[devices._index || 0]; | 
					
						
							|  |  |  |   devices._index = (devices._index || 0) + 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return device; | 
					
						
							|  |  |  | }; |