Skip to content

Commit 036774d

Browse files
authored
Merge pull request #19 from CDPTechnologies/add-node-browser-detection
Add automatic detection to index.js between Node.js and browser
2 parents 25d88b4 + c5efd9a commit 036774d

4 files changed

Lines changed: 156 additions & 57 deletions

File tree

README.rst

Lines changed: 74 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,24 @@ may also have other INode objects as children.
2121

2222
Before all examples, you need:
2323

24+
**Browser (Script Tag):**
25+
26+
.. code:: html
27+
28+
<script src="./protobuf.min.js"></script>
29+
<script src="./studioapi.proto.js"></script>
30+
<script src="./index.js"></script>
31+
<script>
32+
// studio is now available as a global
33+
var client = new studio.api.Client(...);
34+
</script>
35+
36+
**Node.js (CommonJS):**
37+
2438
.. code:: javascript
2539
26-
import studio from "cdp-client"
27-
40+
const studio = require("cdp-client")
41+
2842
Global API
2943
~~~~~~~~~~
3044

@@ -36,15 +50,15 @@ studio.api.Request(systemName, applicationName, cdpVersion, systemUseNotificatio
3650
systemName - System name the application belongs to.
3751

3852
applicationName - Application name.
39-
53+
4054
cdpVersion - CDP version the application is built with.
41-
55+
4256
systemUseNotification - System use notification message to ask for confirmation to continue.
43-
57+
4458
- Returns
4559

4660
The created Request object.
47-
61+
4862
Instance Methods / Request
4963
~~~~~~~~~~~~~~~~~~~~~~~~~~
5064

@@ -92,9 +106,9 @@ studio.api.UserAuthResult(code, text, additionalCredentials)
92106
code - response code.
93107

94108
text - response info/error.
95-
109+
96110
additionalCredentials - Returns additional credentials if required by received response code.
97-
111+
98112
- Returns
99113

100114
The created UserAuthResult object.
@@ -108,7 +122,7 @@ result.code()
108122
- Returns
109123

110124
Authentication response code.
111-
125+
112126
+---------------------------------+----------------------------------------------+---------------------------------------------------------------------------------------+
113127
| Type | Value | Description |
114128
+=================================+==============================================+=======================================================================================+
@@ -121,7 +135,7 @@ result.code()
121135
| | - eGrantedPasswordWillExpireSoon | expiry timestamp is provided by text() |
122136
+ +----------------------------------------------+---------------------------------------------------------------------------------------+
123137
| | - eNewPasswordRequired | AuthRequest with additional response with new username + password hash is required |
124-
+ +----------------------------------------------+---------------------------------------------------------------------------------------+
138+
+ +----------------------------------------------+---------------------------------------------------------------------------------------+
125139
| | - eInvalidChallengeResponse | challenge response sent was invalid |
126140
+ +----------------------------------------------+---------------------------------------------------------------------------------------+
127141
| | - eAdditionalResponseRequired | additional challenge responses based on additional credential types are required. |
@@ -146,7 +160,7 @@ result.additionalCredentials()
146160
Additional credentials in following structure:
147161

148162
.. code:: none
149-
163+
150164
returns {
151165
string type;
152166
string prompt;
@@ -164,11 +178,11 @@ studio.api.Client(uri, notificationListener, autoConnect)
164178

165179
uri - String containing the address and port of StudioAPI server separated by colon character
166180

167-
notificationListener - Object returning two functions: applicationAcceptanceRequested(studio.api.Request) and credentialsRequested(studio.api.Request).
181+
notificationListener - Object returning two functions: applicationAcceptanceRequested(studio.api.Request) and credentialsRequested(studio.api.Request).
168182
Function applicationAcceptanceRequested must return a Promise of void. Can be used to popup system use notification message and ask for confirmation to continue.
169183
Function credentialsRequested must return a Promise of dictionary containing 'Username' and 'Password' as keys for authentication.
170-
171-
autoConnect - Tries to reconnect once disconnected. By default is enabled.
184+
185+
autoConnect - Tries to reconnect once disconnected. By default is enabled.
172186

173187
- Returns
174188

@@ -179,26 +193,26 @@ studio.api.Client(uri, notificationListener, autoConnect)
179193
Create client object to interrogate a CDP Application. The client constructor expects a full
180194
uri with port number separated by colon pointing to StudioAPI service. For exact IP and Port see
181195
CDP Application startup output.
182-
196+
183197
- Example
184198

185199
.. code:: javascript
186200
187201
// Create client connected to uri provided in browser address bar.
188202
var client = new studio.api.Client(window.location.host);
189-
203+
190204
.. code:: javascript
191205
192206
// Create client with NotificationListener connected to uri provided in browser address bar.
193207
// The NotificationListener is only called when page requires a login.
194-
208+
195209
class NotificationListener {
196210
applicationAcceptanceRequested(request) {
197211
return new Promise(function(resolve, reject) {
198212
if (request.systemUseNotification()) {
199213
// Pop up a System Use Notification message and ask for confirmation to continue,
200214
// then based on the user answer call either resolve() or reject()
201-
}
215+
}
202216
else
203217
resolve();
204218
});
@@ -220,6 +234,24 @@ studio.api.Client(uri, notificationListener, autoConnect)
220234
221235
var client = new studio.api.Client(window.location.host, new NotificationListener());
222236
237+
.. code:: javascript
238+
239+
// Node.js example
240+
const studio = require("cdp-client");
241+
242+
const client = new studio.api.Client("127.0.0.1:7689", {
243+
applicationAcceptanceRequested: async (request) => {
244+
console.log('Application access requested');
245+
},
246+
credentialsRequested: async (request) => {
247+
return { Username: "cdpuser", Password: "cdpuser" };
248+
}
249+
});
250+
251+
client.root().then(root => {
252+
console.log("Connected to:", root.name());
253+
});
254+
223255
224256
Instance Methods / Client
225257
~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -235,7 +267,7 @@ client.root()
235267

236268
Wait for root INode object to be available from connected application. The root node is
237269
the top-level "system" node that contains the connected applications.
238-
270+
239271
- Example
240272

241273
.. code:: javascript
@@ -254,7 +286,7 @@ client.find(path)
254286
- Returns
255287

256288
Promise containing requested INode object when fulfilled.
257-
289+
258290
- Restriction
259291

260292
The requested node must reside in the application client was connected to.
@@ -270,21 +302,21 @@ client.find(path)
270302
client.find("MyApp.CPULoad").then(function (load) {
271303
// use the load object referring to CPULoad in MyApp
272304
});
273-
305+
274306
Instance Methods / INode
275307
~~~~~~~~~~~~~~~~~~~~~~~~
276-
308+
277309
node.name()
278310
^^^^^^^^^^^
279311

280312
- Returns
281313

282314
Node name.
283-
315+
284316
- Usage
285317

286318
Get the short node name of INode object. Names in a parent node are all unique.
287-
319+
288320
node.info()
289321
^^^^^^^^^^^
290322

@@ -296,7 +328,7 @@ node.info()
296328

297329
Internal Info object should be used sparingly in client code as it is a protocol object any may change more often.
298330
Optional object members may not be present on all instances.
299-
331+
300332
- Details
301333

302334
+------------------+------------------------------+---------------------------------------------------------------+
@@ -369,14 +401,14 @@ node.lastValue()
369401
- Usage
370402

371403
Access the last known value of existing INode object.
372-
404+
373405
node.setValue(value, timestamp)
374406
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
375407

376408
- Arguments
377409

378410
value
379-
411+
380412
timestamp - timestamp in nanoseconds since EPOCH presented as long int
381413

382414
- Returns
@@ -386,7 +418,7 @@ node.setValue(value, timestamp)
386418
- Usage
387419

388420
**Setting value and timestamp (timestamp will be ignored in current implementation).**
389-
421+
390422
node.forEachChild(iteratorCallback)
391423
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
392424

@@ -397,7 +429,7 @@ node.forEachChild(iteratorCallback)
397429
- Usage
398430

399431
Iterate over children of current node. Iteration starts latest when children for the node are resolved.
400-
432+
401433
- Example
402434

403435
.. code:: javascript
@@ -430,32 +462,32 @@ node.child(name)
430462
node.child("CPULoad").then(function (load) {
431463
// use the load object referring to CPULoad child in current node
432464
});
433-
465+
434466
node.subscribeToValues(valueConsumer, fs, sampleRate)
435467
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
436468

437469
- Arguments
438470

439471
Function(value, timestamp) valueConsumer - timestamp in nanoseconds since EPOCH presented as long int
440-
472+
441473
fs - maximum frequency that value updates are expected (controls how many changes are sent in a single packet). Defaults to 5 hz.
442-
474+
443475
sampleRate - maximum amount of value updates sent per second (controls the amount of data transferred). Zero means all samples must be provided. Defaults to 0.
444476

445477
- Usage
446478

447479
Subscribe to value changes on this node. On each value change valueConsumer function is called
448480
with value of the nodes value_type and UTC Unix timestamp in nanoseconds (nanoseconds from 01.01.1970).
449481
Timestamp refers to the time of value change in connected application on target controller.
450-
482+
451483
- Example
452484

453485
.. code:: javascript
454486
455487
cpuLoad.subscribeToValues(function (value, timestamp) {
456488
console.log("CPULoad:" + value + " at " + timestamp);
457489
});
458-
490+
459491
node.unsubscribeFromValues(valueConsumer)
460492
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
461493

@@ -474,11 +506,11 @@ node.subscribeToChildValues(name, valueConsumer, fs, sampleRate)
474506
- Arguments
475507

476508
name
477-
509+
478510
Function(value, timestamp) valueConsumer - timestamp in nanoseconds since EPOCH presented as long int
479-
511+
480512
fs - maximum frequency that value updates are expected (controls how many changes are sent in a single packet). Defaults to 5 hz.
481-
513+
482514
sampleRate - maximum amount of value updates sent per second (controls the amount of data transferred). Zero means all samples must be provided. Defaults to 0.
483515

484516
- Usage
@@ -492,7 +524,7 @@ node.unsubscribeFromChildValues(name, valueConsumer)
492524
- Arguments
493525

494526
name
495-
527+
496528
Function(value, timestamp) valueConsumer - timestamp in nanoseconds since EPOCH presented as long int
497529

498530
- Usage
@@ -529,7 +561,7 @@ node.subscribeToEvents(eventConsumer, timestampFrom)
529561
- Arguments
530562

531563
Function(event) eventConsumer
532-
564+
533565
timestampFrom - If 0, then all events are passed from the start of the CDP application.
534566
If > 0, events starting from this timestamp (in UTC nanotime) are passed.
535567
If not defined, then events from the moment of subscription is passed.
@@ -613,7 +645,7 @@ node.subscribeToEvents(eventConsumer, timestampFrom)
613645
node.subscribeToEvents(function (event) {
614646
console.log("Event triggered by:" + event.sender);
615647
});
616-
648+
617649
node.unsubscribeFromEvent(eventConsumer)
618650
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
619651

@@ -631,7 +663,7 @@ node.addChild(name, typeName)
631663
- Arguments
632664

633665
name - Name for the new node
634-
666+
635667
typeName - Model name to be used for adding the new node
636668

637669
- Usage
@@ -644,7 +676,7 @@ node.removeChild(name)
644676
- Arguments
645677

646678
name - Name of the node to be removed
647-
679+
648680
- Usage
649681

650682
Remove child Node from this Node.

0 commit comments

Comments
 (0)