-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserDataProvider.js
More file actions
144 lines (144 loc) · 5.05 KB
/
UserDataProvider.js
File metadata and controls
144 lines (144 loc) · 5.05 KB
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
window.JSON = window.JSON || require('json3');
/**
* A User Data Provider API that partners wishing to participate in CSUD can use to send user data.
* In order to send back some user data, users should instantiate a new instance of this function
* and use the #listen method to listen for user data requests, replying with a JSON object that
* contains a mandatory "id":{string} property and optional "ext":{object} property either through
* a callback or by simply returning the object:
*
* <pre><code>
* new UserDataProvider().listen(function(request,callback){
* callback(null, {
* id: 'my-org-user-id',
* ext: {
* fcap: 0
* }
* });
* });
* </code></pre>
*
* or
*
* <pre><code>
* new UserDataProvider().listen(function(request,callback){
* return {
* id: 'my-org-user-id',
* ext: {
* fcap: 0
* }
* };
* });
* </code></pre>
*
* The 'request' parameter is currently unused, but might contain relevant information in the
* future.
*
* @constructor
*/
var UserDataProvider = function(){};
UserDataProvider.prototype = {
/**
* Check whether browser has window.postMessage support
* @returns {Boolean} true if browser has window.postMessage support
*/
hasPostMessage: function hasPostMessage(){
return window.postMessage ? true: false;
},
/**
* Parse and return an incoming request
* @returns {Object} incoming request; should always contain an "id" property
*/
parseRequest: function parseRequest(){
return JSON.parse(unescape(window.location.href.split("#")[1]));
},
/**
* Listen for incoming user data requests and invoke the supplied handler function to process them.
* The supplied handler will be invoked with two arguments:
* <li>A request - currently unused</li>
* <li>A callback function - a node-compliant callback function (function(error,result))
* The handler is expected to either
* <li>Immediately return user data</li>
* <li>Eventually return user data by invoking the callback function</li>
* The handler may also throw an error or report an error by calling the callback function with the
* first argument representing the error (callback('my-error')).
*
* User data must be a JSON object that
* <li>contains a mandatory "id":{string} property</li>
* <li>contains an optional "ext":{object} property</li>
*
* <pre><code>{id:'my-org-user-id'}</code></pre> and <pre><code>{id:'123',ext:{fcap:3}}</code></pre>
* are examples of valid user data.
*
* Example usage:
*
* <pre><code>
* new UserDataProvider().listen(function(request,callback){
* callback(null, {
* id: 'my-org-user-id',
* ext: {
* fcap: 0
* }
* });
* });
* </code></pre>
*
* <pre><code>
* new UserDataProvider().listen(function(request,callback){
* return {
* id: 'my-org-user-id',
* ext: {
* fcap: 0
* }
* };
* });
* </code></pre>
*
* @callback handler function
* @param {Object} request - currently unused
* @param {function} a node-compliant callback handler (function(error,result))
*/
listen: function listen(handler){
try{
/* parse request */
var me = this,
request = me.parseRequest(),
/* make a user data callback*/
userDataCallback = function (error, result){
try{
if(error && error instanceof Error){
/* rewrite error messages */
if(error.message){
error = error.message;
}else{
error = 'General Error';
}
}
var payload = JSON.stringify({error:error,result:result,id:request.id});
if(me.hasPostMessage()){
window.parent.postMessage(payload,'*');
}else{
window.name = payload;
var udlHost = document.referrer.split('/')[2];
window.location.href = '//' + udlHost + '/favicon.ico';
}
}catch(e){
/* swallow */
}
};
/* call handler */
var syncResult;
try{
syncResult = handler(request.payload,userDataCallback);
}catch(e){
userDataCallback(e);
return;
}
if(syncResult !== undefined && syncResult !== null){
userDataCallback(null,syncResult);
}
} catch (listenSetupError) {
/* swallow */
}
}
};
module.exports = UserDataProvider;