nitrogen 0.2.24 → 0.29.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (241) hide show
  1. package/README.md +18 -108
  2. package/lib/Logger.js +56 -0
  3. package/lib/autolinking/Autolinking.js +1 -0
  4. package/lib/autolinking/android/createCMakeExtension.js +109 -0
  5. package/lib/autolinking/android/createGradleExtension.js +36 -0
  6. package/lib/autolinking/android/createHybridObjectInitializer.js +159 -0
  7. package/lib/autolinking/createAndroidAutolinking.js +13 -0
  8. package/lib/autolinking/createIOSAutolinking.js +19 -0
  9. package/lib/autolinking/ios/createHybridObjectInitializer.js +97 -0
  10. package/lib/autolinking/ios/createPodspecRubyExtension.js +69 -0
  11. package/lib/autolinking/ios/createSwiftCxxBridge.js +117 -0
  12. package/lib/autolinking/ios/createSwiftUmbrellaHeader.js +74 -0
  13. package/lib/config/NitroConfig.js +112 -0
  14. package/lib/config/NitroUserConfig.js +88 -0
  15. package/lib/config/getConfig.js +84 -0
  16. package/lib/createGitAttributes.js +11 -0
  17. package/lib/createPlatformSpec.js +127 -0
  18. package/lib/getFiles.js +28 -0
  19. package/lib/getPlatformSpecs.js +153 -0
  20. package/lib/index.js +113 -10
  21. package/lib/init.js +123 -0
  22. package/lib/nitrogen.js +165 -0
  23. package/lib/prettifyDirectory.js +27 -0
  24. package/lib/syntax/BridgedType.js +1 -0
  25. package/lib/syntax/CodeNode.js +1 -0
  26. package/lib/syntax/HybridObjectSpec.js +1 -0
  27. package/lib/syntax/Method.js +108 -0
  28. package/lib/syntax/Parameter.js +65 -0
  29. package/lib/syntax/Property.js +147 -0
  30. package/lib/syntax/SourceFile.js +7 -0
  31. package/lib/syntax/c++/CppEnum.js +110 -0
  32. package/lib/syntax/c++/CppHybridObject.js +146 -0
  33. package/lib/syntax/c++/CppHybridObjectRegistration.js +18 -0
  34. package/lib/syntax/c++/CppStruct.js +108 -0
  35. package/lib/syntax/c++/CppUnion.js +88 -0
  36. package/lib/syntax/c++/getForwardDeclaration.js +14 -0
  37. package/lib/syntax/c++/includeNitroHeader.js +34 -0
  38. package/lib/syntax/createType.js +303 -0
  39. package/lib/syntax/getAllTypes.js +11 -0
  40. package/lib/syntax/getCustomTypeConfig.js +53 -0
  41. package/lib/syntax/getHybridObjectName.d.ts +36 -0
  42. package/lib/syntax/getHybridObjectName.js +10 -0
  43. package/lib/syntax/getInterfaceProperties.js +9 -0
  44. package/lib/syntax/getReferencedTypes.js +47 -0
  45. package/lib/syntax/helpers.js +53 -0
  46. package/lib/syntax/isCoreType.js +47 -0
  47. package/lib/syntax/kotlin/FbjniHybridObject.js +261 -0
  48. package/lib/syntax/kotlin/JNINativeRegistrations.js +7 -0
  49. package/lib/syntax/kotlin/KotlinBoxedPrimitive.js +17 -0
  50. package/lib/syntax/kotlin/KotlinCxxBridgedType.js +893 -0
  51. package/lib/syntax/kotlin/KotlinEnum.js +113 -0
  52. package/lib/syntax/kotlin/KotlinFunction.js +256 -0
  53. package/lib/syntax/kotlin/KotlinHybridObject.js +177 -0
  54. package/lib/syntax/kotlin/KotlinHybridObjectRegistration.js +26 -0
  55. package/lib/syntax/kotlin/KotlinStruct.js +172 -0
  56. package/lib/syntax/kotlin/KotlinVariant.js +191 -0
  57. package/lib/syntax/swift/SwiftCxxBridgedType.js +819 -0
  58. package/lib/syntax/swift/SwiftCxxTypeHelper.js +613 -0
  59. package/lib/syntax/swift/SwiftEnum.js +52 -0
  60. package/lib/syntax/swift/SwiftFunction.js +83 -0
  61. package/lib/syntax/swift/SwiftHybridObject.js +103 -0
  62. package/lib/syntax/swift/SwiftHybridObjectBridge.js +451 -0
  63. package/lib/syntax/swift/SwiftHybridObjectRegistration.js +42 -0
  64. package/lib/syntax/swift/SwiftStruct.js +75 -0
  65. package/lib/syntax/swift/SwiftVariant.js +58 -0
  66. package/lib/syntax/types/ArrayBufferType.js +37 -0
  67. package/lib/syntax/types/ArrayType.d.ts +12 -0
  68. package/lib/syntax/types/ArrayType.js +52 -0
  69. package/lib/syntax/types/BigIntType.js +27 -0
  70. package/lib/syntax/types/BooleanType.js +27 -0
  71. package/lib/syntax/types/CustomType.d.ts +14 -0
  72. package/lib/syntax/types/CustomType.js +36 -0
  73. package/lib/syntax/types/DateType.js +35 -0
  74. package/lib/syntax/types/EnumType.js +101 -0
  75. package/lib/syntax/types/ErrorType.js +37 -0
  76. package/lib/syntax/types/FunctionType.js +147 -0
  77. package/lib/syntax/types/HybridObjectBaseType.js +38 -0
  78. package/lib/syntax/types/HybridObjectType.js +131 -0
  79. package/lib/syntax/types/MapType.js +37 -0
  80. package/lib/syntax/types/NamedWrappingType.js +27 -0
  81. package/lib/syntax/types/NullType.js +23 -0
  82. package/lib/syntax/types/NumberType.js +27 -0
  83. package/lib/syntax/types/OptionalType.js +59 -0
  84. package/lib/syntax/types/PromiseType.js +62 -0
  85. package/lib/syntax/types/RecordType.js +47 -0
  86. package/lib/syntax/types/ResultWrappingType.js +44 -0
  87. package/lib/syntax/types/StringType.js +35 -0
  88. package/lib/syntax/types/StructType.js +61 -0
  89. package/lib/syntax/types/TupleType.js +39 -0
  90. package/lib/syntax/types/Type.js +1 -0
  91. package/lib/syntax/types/VariantType.js +75 -0
  92. package/lib/syntax/types/VoidType.js +27 -0
  93. package/lib/syntax/types/getTypeAs.js +12 -0
  94. package/lib/utils.js +126 -0
  95. package/lib/views/CppHybridViewComponent.js +256 -0
  96. package/lib/views/createHostComponentJs.js +27 -0
  97. package/lib/views/kotlin/KotlinHybridViewManager.js +229 -0
  98. package/lib/views/swift/SwiftHybridViewManager.js +131 -0
  99. package/lib/writeFile.js +19 -0
  100. package/package.json +58 -29
  101. package/src/Logger.ts +63 -0
  102. package/src/autolinking/Autolinking.ts +9 -0
  103. package/src/autolinking/android/createCMakeExtension.ts +126 -0
  104. package/src/autolinking/android/createGradleExtension.ts +43 -0
  105. package/src/autolinking/android/createHybridObjectInitializer.ts +174 -0
  106. package/src/autolinking/createAndroidAutolinking.ts +28 -0
  107. package/src/autolinking/createIOSAutolinking.ts +24 -0
  108. package/src/autolinking/ios/createHybridObjectInitializer.ts +112 -0
  109. package/src/autolinking/ios/createPodspecRubyExtension.ts +76 -0
  110. package/src/autolinking/ios/createSwiftCxxBridge.ts +137 -0
  111. package/src/autolinking/ios/createSwiftUmbrellaHeader.ts +90 -0
  112. package/src/config/NitroConfig.ts +139 -0
  113. package/src/config/NitroUserConfig.ts +105 -0
  114. package/src/config/getConfig.ts +91 -0
  115. package/src/createGitAttributes.ts +15 -0
  116. package/src/createPlatformSpec.ts +176 -0
  117. package/src/getFiles.ts +31 -0
  118. package/src/getPlatformSpecs.ts +202 -0
  119. package/src/index.ts +146 -0
  120. package/src/init.ts +186 -0
  121. package/src/nitrogen.ts +246 -0
  122. package/src/prettifyDirectory.ts +32 -0
  123. package/src/syntax/BridgedType.ts +59 -0
  124. package/src/syntax/CodeNode.ts +24 -0
  125. package/src/syntax/HybridObjectSpec.ts +14 -0
  126. package/src/syntax/Method.ts +154 -0
  127. package/src/syntax/Parameter.ts +81 -0
  128. package/src/syntax/Property.ts +203 -0
  129. package/src/syntax/SourceFile.ts +80 -0
  130. package/src/syntax/c++/CppEnum.ts +128 -0
  131. package/src/syntax/c++/CppHybridObject.ts +165 -0
  132. package/src/syntax/c++/CppHybridObjectRegistration.ts +39 -0
  133. package/src/syntax/c++/CppStruct.ts +129 -0
  134. package/src/syntax/c++/CppUnion.ts +105 -0
  135. package/src/syntax/c++/getForwardDeclaration.ts +19 -0
  136. package/src/syntax/c++/includeNitroHeader.ts +40 -0
  137. package/src/syntax/createType.ts +365 -0
  138. package/src/syntax/getAllTypes.ts +18 -0
  139. package/src/syntax/getCustomTypeConfig.ts +71 -0
  140. package/src/syntax/getHybridObjectName.ts +48 -0
  141. package/src/syntax/getInterfaceProperties.ts +21 -0
  142. package/src/syntax/getReferencedTypes.ts +57 -0
  143. package/src/syntax/helpers.ts +79 -0
  144. package/src/syntax/isCoreType.ts +60 -0
  145. package/src/syntax/kotlin/FbjniHybridObject.ts +313 -0
  146. package/src/syntax/kotlin/JNINativeRegistrations.ts +19 -0
  147. package/src/syntax/kotlin/KotlinBoxedPrimitive.ts +19 -0
  148. package/src/syntax/kotlin/KotlinCxxBridgedType.ts +942 -0
  149. package/src/syntax/kotlin/KotlinEnum.ts +130 -0
  150. package/src/syntax/kotlin/KotlinFunction.ts +277 -0
  151. package/src/syntax/kotlin/KotlinHybridObject.ts +205 -0
  152. package/src/syntax/kotlin/KotlinHybridObjectRegistration.ts +51 -0
  153. package/src/syntax/kotlin/KotlinStruct.ts +198 -0
  154. package/src/syntax/kotlin/KotlinVariant.ts +212 -0
  155. package/src/syntax/swift/SwiftCxxBridgedType.ts +874 -0
  156. package/src/syntax/swift/SwiftCxxTypeHelper.ts +674 -0
  157. package/src/syntax/swift/SwiftEnum.ts +65 -0
  158. package/src/syntax/swift/SwiftFunction.ts +91 -0
  159. package/src/syntax/swift/SwiftHybridObject.ts +121 -0
  160. package/src/syntax/swift/SwiftHybridObjectBridge.ts +522 -0
  161. package/src/syntax/swift/SwiftHybridObjectRegistration.ts +75 -0
  162. package/src/syntax/swift/SwiftStruct.ts +85 -0
  163. package/src/syntax/swift/SwiftVariant.ts +67 -0
  164. package/src/syntax/types/ArrayBufferType.ts +49 -0
  165. package/src/syntax/types/ArrayType.ts +62 -0
  166. package/src/syntax/types/BigIntType.ts +35 -0
  167. package/src/syntax/types/BooleanType.ts +35 -0
  168. package/src/syntax/types/CustomType.ts +47 -0
  169. package/src/syntax/types/DateType.ts +43 -0
  170. package/src/syntax/types/EnumType.ts +130 -0
  171. package/src/syntax/types/ErrorType.ts +44 -0
  172. package/src/syntax/types/FunctionType.ts +167 -0
  173. package/src/syntax/types/HybridObjectBaseType.ts +54 -0
  174. package/src/syntax/types/HybridObjectType.ts +198 -0
  175. package/src/syntax/types/MapType.ts +49 -0
  176. package/src/syntax/types/NamedWrappingType.ts +33 -0
  177. package/src/syntax/types/NullType.ts +30 -0
  178. package/src/syntax/types/NumberType.ts +34 -0
  179. package/src/syntax/types/OptionalType.ts +66 -0
  180. package/src/syntax/types/PromiseType.ts +72 -0
  181. package/src/syntax/types/RecordType.ts +56 -0
  182. package/src/syntax/types/ResultWrappingType.ts +53 -0
  183. package/src/syntax/types/StringType.ts +44 -0
  184. package/src/syntax/types/StructType.ts +83 -0
  185. package/src/syntax/types/TupleType.ts +53 -0
  186. package/src/syntax/types/Type.ts +82 -0
  187. package/src/syntax/types/VariantType.ts +92 -0
  188. package/src/syntax/types/VoidType.ts +34 -0
  189. package/src/syntax/types/getTypeAs.ts +15 -0
  190. package/src/utils.ts +162 -0
  191. package/src/views/CppHybridViewComponent.ts +304 -0
  192. package/src/views/createHostComponentJs.ts +34 -0
  193. package/src/views/kotlin/KotlinHybridViewManager.ts +258 -0
  194. package/src/views/swift/SwiftHybridViewManager.ts +153 -0
  195. package/src/writeFile.ts +27 -0
  196. package/.jshintignore +0 -6
  197. package/.jshintrc +0 -3
  198. package/.npmignore +0 -3
  199. package/.travis.yml +0 -13
  200. package/LICENSE +0 -13
  201. package/browser/nitrogen-min.js +0 -3
  202. package/browser/nitrogen.js +0 -6369
  203. package/lib/apiKey.js +0 -67
  204. package/lib/blob.js +0 -57
  205. package/lib/commandManager.js +0 -350
  206. package/lib/device.js +0 -19
  207. package/lib/memoryStore.js +0 -24
  208. package/lib/message.js +0 -298
  209. package/lib/permission.js +0 -121
  210. package/lib/principal.js +0 -330
  211. package/lib/service.js +0 -347
  212. package/lib/session.js +0 -494
  213. package/lib/user.js +0 -20
  214. package/publish +0 -2
  215. package/scripts/build-documentation +0 -4
  216. package/scripts/build-module +0 -27
  217. package/scripts/module.js +0 -12
  218. package/scripts/postamble.js +0 -1
  219. package/scripts/preamble.js +0 -2
  220. package/scripts/run-test-server +0 -9
  221. package/test/config.js +0 -12
  222. package/test/fixtures/images/image.jpg +0 -0
  223. package/test/fixtures/images/motion0.jpg +0 -0
  224. package/test/fixtures/images/motion1.jpg +0 -0
  225. package/test/fixtures/images/motion2.jpg +0 -0
  226. package/test/fixtures/index.js +0 -76
  227. package/test/main.js +0 -5
  228. package/test/memoryStore.js +0 -22
  229. package/test/mocha.opts +0 -3
  230. package/test/units/apiKey.js +0 -46
  231. package/test/units/blob.js +0 -35
  232. package/test/units/commandManager.js +0 -67
  233. package/test/units/device.js +0 -26
  234. package/test/units/heartbeat.js +0 -28
  235. package/test/units/message.js +0 -79
  236. package/test/units/permissions.js +0 -43
  237. package/test/units/principal.js +0 -116
  238. package/test/units/service.js +0 -92
  239. package/test/units/session.js +0 -97
  240. package/test/units/user.js +0 -48
  241. package/yuidoc.json +0 -8
package/lib/session.js DELETED
@@ -1,494 +0,0 @@
1
- var io = require('socket.io-client')
2
- , Message = require('./message')
3
- , Principal = require('./principal')
4
- , request = require('request');
5
-
6
- /**
7
- * A Session represents an authenticated session and subscription connection between a principal and a service.
8
- *
9
- * @class Session
10
- * @namespace nitrogen
11
- * @param {Object} service The service this session is associated with.
12
- * @param {Object} principal The principal this session is associated with.
13
- * @param {Object} accessToken The accessToken to use for authenticating requests with this session.
14
- * @param {Object} socket The subscription socket to use for realtime updates.
15
- */
16
-
17
- function Session(service, principal, accessToken) {
18
- var self = this;
19
-
20
- this.service = service;
21
- this.principal = principal;
22
- this.accessToken = accessToken;
23
-
24
- this.subscriptionCount = 0;
25
- this.sessionId = Math.floor(Math.random()*100000000);
26
-
27
- this.heartbeatTimeout = false;
28
- this.heartbeatReceived = true;
29
- this.consecutiveHeartbeatFailures = 0;
30
-
31
- this.subscriptions = {};
32
- this.failureCallback = function() {};
33
-
34
- Session.prototype.log = {};
35
- Session.prototype.log.debug = function(message) { Session.log(self, "debug", message); };
36
- Session.prototype.log.info = function(message) { Session.log(self, "info", message); };
37
- Session.prototype.log.warn = function(message) { Session.log(self, "warn", message); };
38
- Session.prototype.log.error = function(message) { Session.log(self, "error", message); };
39
-
40
- this.log.debug('session: created.');
41
- }
42
-
43
- Session.HEARTBEAT_DEFAULT_INTERVAL = 5 * 60 * 1000; // ms
44
- Session.MAX_SUBSCRIPTION_RESTARTS = 3;
45
- Session.HEARTBEAT_TIMEOUT = 15 * 1000; // ms
46
-
47
- Session.queryStringFromObject = function(obj) {
48
- var str = [];
49
-
50
- for(var p in obj)
51
- str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
52
-
53
- return str.join("&");
54
- };
55
-
56
-
57
- Session.startSession = function(service, principal, accessToken) {
58
- if (!Session.sessions)
59
- Session.sessions = {};
60
-
61
- if (!Session.sessions[principal.id]) {
62
- Session.sessions[principal.id] = new Session(service, principal, accessToken);
63
- }
64
-
65
- return Session.sessions[principal.id];
66
- };
67
-
68
- Session.stopSession = function(session) {
69
- delete Session.sessions[session.principal.id];
70
- };
71
-
72
- /**
73
- * Stop the session with the service.
74
- *
75
- * @method stop
76
- * @private
77
- **/
78
-
79
- Session.prototype.stop = function() {
80
- Session.stopSession(this);
81
- this.failureCallback = null;
82
-
83
- var self = this;
84
- for (var key in this.subscriptions) {
85
- this.disconnectSubscription(this.subscriptions[key].id);
86
- }
87
-
88
- if (this.socket)
89
- this.socket.disconnect();
90
-
91
- this.socket = null;
92
-
93
- if (this.heartbeat)
94
- this.heartbeat.stop();
95
-
96
- this.heartbeat = null;
97
- };
98
-
99
- Session.prototype.beforeRequest = function(options) {
100
- if (!options.headers) options.headers = {};
101
- options.headers.Authorization = "Bearer " + this.accessToken.token;
102
-
103
- var prefix = "?";
104
- if (options.query) {
105
- var queryString = JSON.stringify(options.query);
106
- options.url += prefix + "q=" + encodeURIComponent(queryString);
107
- delete options.query;
108
-
109
- prefix = "&";
110
- }
111
-
112
- if (options.queryOptions) {
113
- var optionsString = JSON.stringify(options.queryOptions);
114
- options.url += prefix + "options=" + encodeURIComponent(optionsString);
115
- delete options.queryOptions;
116
- }
117
- };
118
-
119
- Session.prototype.afterRequest = function(err, resp, body, callback) {
120
- if (resp && resp.statusCode !== 200) {
121
- if (body && body.error)
122
- err = body.error;
123
- else
124
- err = resp.statusCode;
125
- }
126
-
127
- if (resp && resp.statusCode === 401) this.signalFailure();
128
-
129
- // If we get an access token update, replace the current access token and continue.
130
- if (resp && resp.headers && resp.headers['x-n2-set-access-token']) {
131
- this.accessToken = JSON.parse(resp.headers['x-n2-set-access-token']);
132
- }
133
-
134
- callback(err, resp, body);
135
- };
136
-
137
- Session.prototype.makeRequest = function(options, requestFunc, callback) {
138
- var self = this;
139
-
140
- this.beforeRequest(options);
141
-
142
- return requestFunc(options, function(err, resp, body) {
143
- self.afterRequest(err, resp, body, callback);
144
- });
145
- };
146
-
147
- Session.prototype.get = function(options, callback) {
148
- return this.makeRequest(options, request.get, callback);
149
- };
150
-
151
- Session.prototype.post = function(options, callback) {
152
- return this.makeRequest(options, request.post, callback);
153
- };
154
-
155
- Session.prototype.put = function(options, callback) {
156
- return this.makeRequest(options, request.put, callback);
157
- };
158
-
159
- Session.prototype.remove = function(options, callback) {
160
- return this.makeRequest(options, request.del, callback);
161
- };
162
-
163
- /**
164
- * Clear all of the credentials for a particular principal.
165
- *
166
- * @method clearCredentials
167
- * @private
168
- * @param {Object} principal The principal to clear credentials for.
169
- **/
170
-
171
- Session.prototype.clearCredentials = function() {
172
- this.service.clearCredentials(this.principal);
173
- };
174
-
175
- /**
176
- * Connect subscription socket for principal with this accessToken to the service.
177
- *
178
- * @method connectSocket
179
- * @private
180
- **/
181
-
182
- Session.prototype.connectSocket = function() {
183
- var self = this;
184
- if (!this.principal || !this.principal.id || !this.accessToken) throw new Error('need both principal and accessToken to connectSocket');
185
-
186
- // we can only share a socket on a per principal basis because otherwise principals can listen in
187
- // on other principals' events.
188
- if (!this.socket) {
189
- this.socket = io.connect(this.service.config.endpoints.subscriptions, {
190
- query: Session.queryStringFromObject({ auth: this.accessToken.token }),
191
- 'force new connection': true
192
- });
193
-
194
- this.setupSocketEvents();
195
- self.startHeartbeat();
196
- }
197
-
198
- return this.socket;
199
- };
200
-
201
- Session.prototype.setupSocketEvents = function() {
202
- var self = this;
203
-
204
- //this.socket.on('connecting', function() { self.log.debug('session: socket.io connecting'); });
205
-
206
- this.socket.on('connect', function() {
207
- self.log.debug('session: socket.io connected');
208
- });
209
-
210
- this.socket.on('disconnect', function() {
211
- self.log.debug('session: socket.io connection disconnected');
212
- });
213
-
214
- // this.socket.on('reconnecting', function() { self.log.info('session: socket.io connection reconnecting'); });
215
-
216
- this.socket.on('reconnect', function() {
217
- if (self.socket) {
218
- self.log.warn('session: socket.io connection reconnected. restarting subscriptions: ' + JSON.stringify(self.subscriptions));
219
- self.restartSubscriptions();
220
- }
221
- });
222
- };
223
-
224
- /**
225
- * Disconnect the current subscription connection with the service.
226
- *
227
- * @method disconnectSubscription
228
- * @private
229
- **/
230
-
231
- Session.prototype.disconnectSubscription = function(subscriptionId) {
232
- if (!this.subscriptions[subscriptionId]) return;
233
-
234
- this.socket.emit('stop', this.subscriptions[subscriptionId]);
235
- delete this.subscriptions[subscriptionId];
236
- };
237
-
238
- /**
239
- * Connect a subscription with the service.
240
- *
241
- * @method disconnectSubscription
242
- * @private
243
- **/
244
-
245
- Session.prototype.connectSubscription = function(options) {
246
- this.subscriptions[options.id] = options;
247
- this.socket.emit('start', options);
248
- };
249
-
250
- /**
251
- * Restart all subscriptions with the service. Used after a connection disruption.
252
- *
253
- * @method restartSubscriptions
254
- * @private
255
- **/
256
-
257
- Session.prototype.restartSubscriptions = function() {
258
- for (var key in this.subscriptions) {
259
- var subscription = this.subscriptions[key];
260
- this.log.info('restarting subscription: ' + key + ': ' + JSON.stringify(subscription));
261
-
262
- if (this.socket) this.socket.emit('stop', this.subscriptions[subscription.id]);
263
- this.connectSubscription(this.subscriptions[key]);
264
- }
265
- };
266
-
267
- /**
268
- * Impersonate the principal with the authorization context of this session. Used by the Nitrogen service to impersonate principals for agent setup.
269
- *
270
- * @method impersonate
271
- * @private
272
- * @param {Object} principal The principal to impersonate with this session.
273
- * @param {Object} callback Callback for the impersonation.
274
- * @param {Object} callback.err If the impersonation failed, this will contain the error.
275
- * @param {Object} callback.session The session for the impersonated principal with this service.
276
- * @param {Object} callback.principal The impersonated principal.
277
- **/
278
-
279
- Session.prototype.impersonate = function(principal, callback) {
280
- this.service.impersonate(this, principal, callback);
281
- };
282
-
283
- // DEPRECIATED
284
- Session.prototype.onAuthFailure = function(callback) {
285
- this.failureCallback = callback;
286
- };
287
-
288
- /**
289
- * Passed callback function will be called on session failure.
290
- *
291
- * @method onFailure
292
- * @param {Object} principal The principal to impersonate with this session.
293
- * @param {Function} callback Callback function with signature f(err).
294
- **/
295
-
296
- Session.prototype.onFailure = function(callback) {
297
- this.failureCallback = callback;
298
- };
299
-
300
- Session.prototype.signalFailure = function() {
301
- var failureCallback = this.failureCallback;
302
-
303
- this.stop();
304
-
305
- if (failureCallback) failureCallback();
306
- };
307
-
308
- /**
309
- * Core subscription event method. Primarily used to subscribe for changes to principals and new messages.
310
- *
311
- * @method on
312
- * @param {Object} options Options for the filter of the messages this subscription should receive: 'type': The type this subscription should receive. Only 'message' is currently supported. 'filter': The filter to apply to the objects returned from this subscription. For example { from: '51f2735fda5fcca439000001' } will restrict messages received to only those from this particular principal id.
313
- * @param {Function} callback Callback function with signature f(objectReceived).
314
- **/
315
-
316
- Session.prototype.on = function(options, callback) {
317
- if (!options) return callback("Options hash required for subscription");
318
- if (['message', 'principal'].indexOf(options.type) === -1) return callback("Unknown subscription 'type'");
319
-
320
- // if there is an existing socket connection already, this will be a NOP.
321
- this.connectSocket();
322
-
323
- this.subscriptionCount += 1;
324
- options.id = this.principal.id + "_" + this.sessionId + "_" + this.subscriptionCount;
325
-
326
- this.socket.on(options.id, function(obj) {
327
- if (options.type === 'message')
328
- return callback(new Message(obj));
329
-
330
- if (options.type === 'principal')
331
- return callback(new Principal(obj));
332
- });
333
-
334
- this.connectSubscription(options);
335
- return options.id;
336
- };
337
-
338
- /**
339
- * Syntax sugar to setup a message subscription.
340
- *
341
- * @method onMessage
342
- * @param {Object} filter The filter to apply to the objects returned from this subscription. For example { from: '51f2735fda5fcca439000001' } will restrict messages received to only those from this particular principal id.
343
- * @param {Function} callback Callback function with signature f(messageReceived).
344
- **/
345
-
346
- Session.prototype.onMessage = function(filter, callback) {
347
- if (typeof filter === 'function') {
348
- callback = filter;
349
- }
350
-
351
- var options = {};
352
- options.filter = filter || {};
353
- options.type = 'message';
354
- return this.on(options, callback);
355
- };
356
-
357
- /**
358
- * Syntax sugar to setup a principal subscription.
359
- *
360
- * @method onMessage
361
- * @param {Object} filter The filter to apply to the objects returned from this subscription. For example { from: '51f2735fda5fcca439000001' } will restrict messages received to only those from this particular principal id.
362
- * @param {Function} callback Callback function with signature f(messageReceived).
363
- **/
364
-
365
- Session.prototype.onPrincipal = function(filter, callback) {
366
- if (typeof filter === 'function') {
367
- callback = filter;
368
- }
369
-
370
- var options = {};
371
- options.filter = filter || {};
372
- options.type = 'principal';
373
- return this.on(options, callback);
374
- };
375
-
376
- Session.log = function(session, severity, message) {
377
- if (session.service.config.log_levels && session.service.config.log_levels.indexOf(severity) === -1) return;
378
-
379
- var logLifetime = session.service.config.log_lifetime || 24 * 60 * 60000;
380
-
381
- var logMessage = new Message({
382
- type: 'log',
383
- ts: new Date(),
384
- from: session.principal.id,
385
- body: {
386
- severity: severity,
387
- message: message
388
- },
389
- index_until: new Date(new Date().getTime() + logLifetime)
390
- });
391
-
392
- var principalNameOrId = (session.principal.name || session.principal.id);
393
-
394
- var dateFormat = [logMessage.ts.getMonth()+1, logMessage.ts.getDate(), logMessage.ts.getFullYear()];
395
- var date = dateFormat.join('/');
396
-
397
- console.log(date + " " + logMessage.ts.toLocaleTimeString() + ": " + principalNameOrId + ': ' + severity + ": " + message);
398
-
399
- logMessage.send(session);
400
- };
401
-
402
- Session.prototype.heartbeatReceivedCheck = function() {
403
- if (!this.heartbeatReceived) {
404
- this.consecutiveHeartbeatFailures += 1;
405
- this.log.error('heartbeat: not received within timeout: restarting subscriptions (' + this.consecutiveHeartbeatFailures + ' consecutive).');
406
- if (this.session && this.consecutiveHeartbeatFailures < Heartbeat.MAX_SUBSCRIPTION_RESTARTS) {
407
- this.stopHeartbeat();
408
- this.restartSubscriptions();
409
- this.startHeartbeatInterval();
410
- } else {
411
- // too many consecutive heartbeat failures
412
-
413
- this.log.error('heartbeat: too many consecutive failures. signalling session failure.');
414
- this.signalFailure();
415
- }
416
- }
417
- };
418
-
419
- Session.prototype.startHeartbeat = function() {
420
- if (this.heartbeatTimeout) return;
421
- var self = this;
422
-
423
- this.onMessage({ type: 'heartbeat' }, function(message) {
424
- // the goal of this is to test realtime connectivity so we don't care whose heartbeat this is.
425
-
426
- self.log.debug('received heartbeat');
427
- self.heartbeatReceived = true;
428
- self.consecutiveHeartbeatFailures = 0;
429
- });
430
-
431
- this.startHeartbeatInterval();
432
- };
433
-
434
- Session.prototype.startHeartbeatInterval = function() {
435
- var self = this;
436
-
437
- this.stopHeartbeat();
438
-
439
- this.log.debug('starting heartbeat interval');
440
-
441
- this.heartbeatTimeout = setInterval(function() {
442
- self.sendHeartbeat();
443
- }, Session.HEARTBEAT_DEFAULT_INTERVAL);
444
- };
445
-
446
- Session.prototype.sendHeartbeat = function(callback) {
447
- var self = this;
448
-
449
- this.principal.status(function(err, status) {
450
- if (err) {
451
- status = status || {};
452
- status.error = err;
453
- }
454
-
455
- var heartbeatIndexUntil = self.service.config.heartbeat_lifetime || 30 * 60 * 1000;
456
-
457
- var message = new Message({
458
- type: 'heartbeat',
459
- public: false,
460
- body: {
461
- error: !!err,
462
- status: status
463
- },
464
- index_until: new Date(new Date().getTime() + heartbeatIndexUntil)
465
- });
466
-
467
- setTimeout(function() { self.heartbeatReceivedCheck() }, Session.HEARTBEAT_TIMEOUT);
468
- self.heartbeatReceived = false;
469
-
470
- self.log.debug('sending heartbeat');
471
- message.send(self, function(err, message) {
472
- if (err) {
473
- self.log.error("failed to send heartbeat: " + err);
474
-
475
- // something is far more wrong than the subscription.
476
- self.heartbeatReceived = true;
477
- if (self) self.signalFailure();
478
- }
479
-
480
- if (callback) return callback(err);
481
- });
482
- });
483
- };
484
-
485
- Session.prototype.stopHeartbeat = function() {
486
- if (!this.heartbeatTimeout) return;
487
-
488
- this.log.debug('clearing heartbeat interval');
489
-
490
- clearInterval(this.heartbeatTimeout);
491
- this.heartbeatTimeout = false;
492
- };
493
-
494
- module.exports = Session;
package/lib/user.js DELETED
@@ -1,20 +0,0 @@
1
- var Principal = require('./principal')
2
- , request = require('request');
3
-
4
- /**
5
- * User is a subclass of principal that houses all user specific principal functionality.
6
- *
7
- * @class User
8
- * @namespace nitrogen
9
- */
10
-
11
- function User() {
12
- Principal.apply(this, arguments);
13
-
14
- this.type = 'user';
15
- }
16
-
17
- User.prototype = Object.create(Principal.prototype);
18
- User.prototype.constructor = User;
19
-
20
- module.exports = User;
package/publish DELETED
@@ -1,2 +0,0 @@
1
- scripts/build-module
2
- npm publish
@@ -1,4 +0,0 @@
1
- #!/bin/bash
2
- # build documentation (assumes Nitrogen website repo is in dir parallel to the client project repo)
3
-
4
- jsdox lib --output ../website/source/docs/nitrogen
@@ -1,27 +0,0 @@
1
- #!/bin/bash
2
-
3
- # This fantastic piece of engineering is because I can't figure out how to do the equivalent in Browserify
4
- # without ending up with a 500k pile of goop that doesn't function. Pull requests welcome.
5
-
6
- mkdir -p browser
7
-
8
- cat node_modules/nitrogen-browser-request/index.js > browser/nitrogen.js
9
- cat node_modules/socket.io-client/dist/socket.io.js >> browser/nitrogen.js
10
-
11
- cat scripts/preamble.js >> browser/nitrogen.js
12
- cat lib/commandManager.js | sed '/require(/d' | sed '/module.export/d' >> browser/nitrogen.js
13
- cat lib/apiKey.js | sed '/require(/d' | sed '/module.export/d' >> browser/nitrogen.js
14
- cat lib/permission.js | sed '/require(/d' | sed '/module.export/d' >> browser/nitrogen.js
15
- cat lib/principal.js | sed '/require(/d' | sed '/module.export/d' >> browser/nitrogen.js
16
- cat lib/message.js | sed '/require(/d' | sed '/module.export/d' >> browser/nitrogen.js
17
- cat lib/blob.js | sed '/require(/d' | sed '/module.export/d' >> browser/nitrogen.js
18
- cat lib/session.js | sed '/require(/d' | sed '/module.export/d' >> browser/nitrogen.js
19
- cat lib/service.js | sed '/require(/d' | sed '/module.export/d' >> browser/nitrogen.js
20
- cat lib/device.js | sed '/require(/d' | sed '/module.export/d' >> browser/nitrogen.js
21
- cat lib/user.js | sed '/require(/d' | sed '/module.export/d' >> browser/nitrogen.js
22
- cat lib/memoryStore.js | sed '/require(/d' | sed '/module.export/d' >> browser/nitrogen.js
23
-
24
- cat scripts/module.js >> browser/nitrogen.js
25
- cat scripts/postamble.js >> browser/nitrogen.js
26
-
27
- node_modules/.bin/uglifyjs browser/nitrogen.js > browser/nitrogen-min.js
package/scripts/module.js DELETED
@@ -1,12 +0,0 @@
1
- window.nitrogen = {
2
- ApiKey: ApiKey,
3
- Blob: Blob,
4
- CommandManager: CommandManager,
5
- Device: Device,
6
- Message: Message,
7
- Permission: Permission,
8
- Principal: Principal,
9
- Service: Service,
10
- Session: Session,
11
- User: User
12
- };
@@ -1 +0,0 @@
1
- }());
@@ -1,2 +0,0 @@
1
- (function () {
2
- "use strict";
@@ -1,9 +0,0 @@
1
- #!/bin/sh
2
-
3
- git clone https://github.com/nitrogenjs/service.git --depth 1
4
- cd service
5
- npm install
6
- export NODE_ENV=test
7
- node server.js > /dev/null &
8
- sleep 6
9
- cd ..
package/test/config.js DELETED
@@ -1,12 +0,0 @@
1
- var nitrogen = require('../lib'),
2
- MemoryStore = require('./memoryStore');
3
-
4
- var config = {
5
- host: "localhost",
6
- protocol: "http",
7
- http_port: 3050
8
- };
9
-
10
- config.store = new MemoryStore(config);
11
-
12
- module.exports = config;
Binary file
Binary file
Binary file
Binary file
@@ -1,76 +0,0 @@
1
- var assert = require('assert')
2
- , config = require('../config')
3
- , nitrogen = require('../../lib');
4
-
5
- var fixtures = {};
6
-
7
- var addToFixture = function(fixtureId) {
8
- return function(err, model) {
9
- if (err) throw err;
10
- fixtures[fixtureId] = model;
11
- };
12
- };
13
-
14
- exports.reset = function(callback) {
15
- var service = new nitrogen.Service(config);
16
- service.store.clear();
17
-
18
- var user = new nitrogen.User({
19
- nickname: 'userFixture',
20
- name: "John Doe",
21
- email: 'test' + Math.random() + '@domain.com',
22
- password: 'foobar123'
23
- });
24
-
25
- service.create(user, function(err, session, user) {
26
- assert(!err);
27
-
28
- fixtures.user = user;
29
-
30
- nitrogen.ApiKey.find(session, {}, {}, function(err, apiKeys) {
31
- assert(!err);
32
-
33
- fixtures.userApiKey = apiKeys[0];
34
-
35
- var camera = new nitrogen.Device({
36
- api_key: fixtures.userApiKey.key,
37
- nickname: "cameraFixture",
38
- tags: ["executes:cameraCommand"],
39
- sensors: [{
40
- id: 1,
41
- name: 'Camera',
42
- executes: 'cameraCommand'
43
- }]
44
- });
45
-
46
- service.connect(camera, function(err, session, camera) {
47
- if (err) throw err;
48
-
49
- assert.equal(camera.tags.length, 1);
50
- assert.equal(camera.sensors.length, 1);
51
-
52
- fixtures.camera = camera;
53
-
54
- var message = new nitrogen.Message({
55
- type: "image",
56
- body: {
57
- url: "http://localhost:3030/blobs/1"
58
- }
59
- });
60
-
61
- message.send(session, function(err, messages) {
62
- if (err) throw err;
63
-
64
- messages.forEach(function(message) {
65
- fixtures.message = message;
66
- });
67
-
68
- return callback();
69
- });
70
- });
71
-
72
- });
73
- });
74
- };
75
-
76
- exports.models = fixtures;
package/test/main.js DELETED
@@ -1,5 +0,0 @@
1
- var fixtures = require('./fixtures');
2
-
3
- before(function(done) {
4
- fixtures.reset(done);
5
- });