pryv 2.1.9 → 2.3.1
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.
- package/README.md +300 -304
- package/package.json +12 -45
- package/src/Auth/AuthController.js +43 -50
- package/src/Auth/AuthStates.js +12 -12
- package/src/Auth/LoginMessages.js +17 -14
- package/src/Auth/index.js +18 -15
- package/src/Browser/CookieUtils.js +37 -27
- package/src/Browser/LoginButton.js +42 -37
- package/src/Browser/index.js +16 -42
- package/src/Connection.js +102 -95
- package/src/Service.js +47 -45
- package/src/ServiceAssets.js +42 -34
- package/src/browser-index-bundle.js +8 -0
- package/src/browser-index.js +7 -0
- package/src/index.d.ts +833 -0
- package/src/index.js +20 -3
- package/src/lib/browser-getEventStreamed.js +21 -19
- package/src/lib/json-parser.js +23 -24
- package/src/utils.js +55 -43
- package/test/Browser.AuthController.test.js +19 -21
- package/test/Browser.test.js +23 -26
- package/test/Connection.test.js +135 -151
- package/test/Service.test.js +30 -44
- package/test/ServiceAssets.test.js +16 -22
- package/test/browser-index.html +26 -0
- package/test/utils.test.js +30 -35
- package/.jsdoc-conf.json +0 -29
- package/.mocharc.js +0 -13
- package/LICENSE.md +0 -27
- package/scripts/setup-environment-dev.sh +0 -28
- package/scripts/upload.sh +0 -15
- package/src/Pryv.js +0 -19
- package/src/index-socket.io-monitor.js +0 -4
- package/src/index.html +0 -17
- package/test/browser-index.js +0 -11
- package/test/browser-tests.html +0 -31
- package/test/helpers.js +0 -8
- package/test/load-test-account.js +0 -108
- package/test/test-data.js +0 -95
- package/web-demos/auth-with-redirection.html +0 -72
- package/web-demos/auth.html +0 -77
- package/web-demos/custom-login-button.html +0 -158
- package/web-demos/index.html +0 -186
- package/web-demos/service-info.json +0 -13
- package/web-demos/stream-examples.html +0 -80
- package/webpack.config.js +0 -83
package/src/index.js
CHANGED
|
@@ -1,3 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* `pryv` library
|
|
7
|
+
* @exports pryv
|
|
8
|
+
* @property {pryv.Service} Service - To interact with Pryv.io at a "Platform level"
|
|
9
|
+
* @property {pryv.Connection} Connection - To interact with an individual's (user) data set
|
|
10
|
+
* @property {pryv.Browser} Browser - Browser Tools - Access request helpers and visuals (button)
|
|
11
|
+
* @property {pryv.utils} utils - Exposes **superagent** for HTTP calls and tools to manipulate Pryv's API Endpoints
|
|
12
|
+
*/
|
|
13
|
+
module.exports = {
|
|
14
|
+
Service: require('./Service'),
|
|
15
|
+
Connection: require('./Connection'),
|
|
16
|
+
Auth: require('./Auth'),
|
|
17
|
+
Browser: require('./Browser'),
|
|
18
|
+
utils: require('./utils'),
|
|
19
|
+
version: require('../package.json').version
|
|
20
|
+
};
|
|
@@ -1,12 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
|
|
4
|
+
*/
|
|
5
|
+
/* global fetch */
|
|
6
|
+
|
|
7
|
+
module.exports = getEventStreamed;
|
|
1
8
|
|
|
2
9
|
/**
|
|
3
10
|
* @private
|
|
4
11
|
* Replacement for getEventStreamed for Browser
|
|
5
12
|
* To be used as long as superagent does not propose it.
|
|
6
|
-
*
|
|
13
|
+
*
|
|
7
14
|
*/
|
|
8
|
-
async function getEventStreamed(conn, queryParam, parser) {
|
|
9
|
-
|
|
15
|
+
async function getEventStreamed (conn, queryParam, parser) {
|
|
10
16
|
/**
|
|
11
17
|
* Holds Parser's settings
|
|
12
18
|
*/
|
|
@@ -14,19 +20,19 @@ async function getEventStreamed(conn, queryParam, parser) {
|
|
|
14
20
|
ondata: null,
|
|
15
21
|
onend: null,
|
|
16
22
|
encoding: 'utf8'
|
|
17
|
-
}
|
|
23
|
+
};
|
|
18
24
|
|
|
19
25
|
/**
|
|
20
26
|
* Mock Response
|
|
21
27
|
*/
|
|
22
28
|
const fakeRes = {
|
|
23
|
-
setEncoding
|
|
29
|
+
setEncoding: function (encoding) {
|
|
24
30
|
parserSettings.encoding = encoding;
|
|
25
31
|
}, // will receive 'data' and 'end' callbacks
|
|
26
|
-
on: function(key, f) {
|
|
32
|
+
on: function (key, f) {
|
|
27
33
|
parserSettings['on' + key] = f;
|
|
28
34
|
}
|
|
29
|
-
}
|
|
35
|
+
};
|
|
30
36
|
|
|
31
37
|
/**
|
|
32
38
|
* Holds results from the parser
|
|
@@ -34,23 +40,22 @@ async function getEventStreamed(conn, queryParam, parser) {
|
|
|
34
40
|
let errResult;
|
|
35
41
|
let bodyObjectResult;
|
|
36
42
|
/**
|
|
37
|
-
*
|
|
43
|
+
*
|
|
38
44
|
*/
|
|
39
|
-
parser(fakeRes, function (err, bodyObject) {
|
|
45
|
+
parser(fakeRes, function (err, bodyObject) {
|
|
40
46
|
errResult = err;
|
|
41
47
|
bodyObjectResult = bodyObject;
|
|
42
48
|
});
|
|
43
49
|
|
|
44
|
-
|
|
45
50
|
// ------------ fetch ------------------- //
|
|
46
|
-
|
|
51
|
+
const url = new URL(conn.endpoint + 'events');
|
|
47
52
|
url.search = new URLSearchParams(queryParam);
|
|
48
|
-
|
|
53
|
+
const fetchParams = { method: 'GET', headers: { Accept: 'application/json' } };
|
|
49
54
|
if (conn.token) fetchParams.headers.Authorization = conn.token;
|
|
50
55
|
|
|
51
|
-
|
|
56
|
+
const response = await fetch(url, fetchParams);
|
|
52
57
|
const reader = response.body.getReader();
|
|
53
|
-
|
|
58
|
+
|
|
54
59
|
while (true) {
|
|
55
60
|
const { done, value } = await reader.read();
|
|
56
61
|
parserSettings.ondata(new TextDecoder(parserSettings.encoding).decode(value));
|
|
@@ -67,14 +72,11 @@ async function getEventStreamed(conn, queryParam, parser) {
|
|
|
67
72
|
body: bodyObjectResult, // from the parser
|
|
68
73
|
statusCode: response.status,
|
|
69
74
|
headers: {}
|
|
70
|
-
}
|
|
75
|
+
};
|
|
71
76
|
// add headers to result
|
|
72
|
-
for (
|
|
77
|
+
for (const pair of response.headers.entries()) {
|
|
73
78
|
result.headers[pair[0]] = pair[1];
|
|
74
79
|
}
|
|
75
80
|
|
|
76
81
|
return result;
|
|
77
82
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
module.exports = getEventStreamed;
|
package/src/lib/json-parser.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
|
|
4
|
+
*/
|
|
1
5
|
// there two steps 1 find events, then eventDeletions
|
|
2
6
|
const EVENTMARKERS = ['"events":[', '"eventDeletions":['];
|
|
3
7
|
|
|
@@ -10,9 +14,9 @@ module.exports = function (foreachEvent, includeDeletions) {
|
|
|
10
14
|
let buffer = ''; // temp data
|
|
11
15
|
let body = null; // to be returned
|
|
12
16
|
|
|
13
|
-
//IN EVENTS VARS
|
|
14
|
-
let depth = 0; // level of depth in brackets
|
|
15
|
-
let inString = false; // cursor is in a String
|
|
17
|
+
// IN EVENTS VARS
|
|
18
|
+
let depth = 0; // level of depth in brackets
|
|
19
|
+
let inString = false; // cursor is in a String
|
|
16
20
|
let skipNextOne = false; // when a backslash is found
|
|
17
21
|
let cursorPos = 0; // position of Character Cursor
|
|
18
22
|
|
|
@@ -24,11 +28,11 @@ module.exports = function (foreachEvent, includeDeletions) {
|
|
|
24
28
|
A_BEFORE_EVENTS: 0,
|
|
25
29
|
B_IN_EVENTS: 1,
|
|
26
30
|
D_AFTER_EVENTS: 2
|
|
27
|
-
}
|
|
31
|
+
};
|
|
28
32
|
|
|
29
33
|
let state = states.A_BEFORE_EVENTS;
|
|
30
34
|
|
|
31
|
-
function processBuffer() {
|
|
35
|
+
function processBuffer () {
|
|
32
36
|
switch (state) {
|
|
33
37
|
case states.A_BEFORE_EVENTS:
|
|
34
38
|
searchStartEvents();
|
|
@@ -42,10 +46,9 @@ module.exports = function (foreachEvent, includeDeletions) {
|
|
|
42
46
|
}
|
|
43
47
|
}
|
|
44
48
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
var n = buffer.indexOf(EVENTMARKERS[eventOrEventDeletions]);
|
|
49
|
+
function searchStartEvents () {
|
|
50
|
+
// search for "events": and happend any info before to the body
|
|
51
|
+
const n = buffer.indexOf(EVENTMARKERS[eventOrEventDeletions]);
|
|
49
52
|
if (n > 0) {
|
|
50
53
|
if (eventOrEventDeletions === 0) { // do only once
|
|
51
54
|
body = buffer.substring(0, n);
|
|
@@ -56,8 +59,7 @@ module.exports = function (foreachEvent, includeDeletions) {
|
|
|
56
59
|
}
|
|
57
60
|
}
|
|
58
61
|
|
|
59
|
-
|
|
60
|
-
function processEvents() {
|
|
62
|
+
function processEvents () {
|
|
61
63
|
/// ---- in Event
|
|
62
64
|
while (cursorPos < buffer.length && (state === states.B_IN_EVENTS)) {
|
|
63
65
|
if (skipNextOne) { // ignore next character
|
|
@@ -66,7 +68,7 @@ module.exports = function (foreachEvent, includeDeletions) {
|
|
|
66
68
|
continue;
|
|
67
69
|
}
|
|
68
70
|
switch (buffer.charCodeAt(cursorPos)) {
|
|
69
|
-
case 93:
|
|
71
|
+
case 93: // ]
|
|
70
72
|
if (depth === 0) { // end of events
|
|
71
73
|
if (cursorPos !== 0) {
|
|
72
74
|
throw new Error('Found trailling ] in mid-course');
|
|
@@ -75,20 +77,20 @@ module.exports = function (foreachEvent, includeDeletions) {
|
|
|
75
77
|
state = states.A_BEFORE_EVENTS;
|
|
76
78
|
eventOrEventDeletions = 1; // now look for eventDeletions
|
|
77
79
|
return;
|
|
78
|
-
} else { // done
|
|
80
|
+
} else { // done
|
|
79
81
|
state = states.D_AFTER_EVENTS;
|
|
80
82
|
let eventsOrDeletionMsg = '';
|
|
81
83
|
if (eventOrEventDeletions === 1) {
|
|
82
|
-
eventsOrDeletionMsg = '"eventDeletionsCount":' + eventDeletionsCount + ','
|
|
84
|
+
eventsOrDeletionMsg = '"eventDeletionsCount":' + eventDeletionsCount + ',';
|
|
83
85
|
}
|
|
84
86
|
buffer = eventsOrDeletionMsg + '"eventsCount":' + eventsCount + '' + buffer.substr(1);
|
|
85
87
|
}
|
|
86
88
|
}
|
|
87
89
|
break;
|
|
88
|
-
case 92:
|
|
90
|
+
case 92: // \
|
|
89
91
|
skipNextOne = true;
|
|
90
92
|
break;
|
|
91
|
-
case 123:
|
|
93
|
+
case 123: // {
|
|
92
94
|
if (!inString) depth++;
|
|
93
95
|
break;
|
|
94
96
|
case 34: // "
|
|
@@ -100,13 +102,13 @@ module.exports = function (foreachEvent, includeDeletions) {
|
|
|
100
102
|
// ignore possible coma ',' if first char
|
|
101
103
|
const ignoreComa = (buffer.charCodeAt(0) === 44) ? 1 : 0;
|
|
102
104
|
const eventStr = buffer.substring(ignoreComa, cursorPos + 1);
|
|
103
|
-
|
|
105
|
+
|
|
104
106
|
if (eventOrEventDeletions === 0) {
|
|
105
107
|
eventsCount++;
|
|
106
108
|
} else {
|
|
107
109
|
eventDeletionsCount++;
|
|
108
110
|
}
|
|
109
|
-
buffer = buffer.substr(cursorPos + 1
|
|
111
|
+
buffer = buffer.substr(cursorPos + 1);
|
|
110
112
|
addEvent(eventStr);
|
|
111
113
|
cursorPos = -1;
|
|
112
114
|
}
|
|
@@ -116,11 +118,10 @@ module.exports = function (foreachEvent, includeDeletions) {
|
|
|
116
118
|
}
|
|
117
119
|
}
|
|
118
120
|
|
|
119
|
-
function afterEvents() {
|
|
121
|
+
function afterEvents () {
|
|
120
122
|
// just happend the end of message;
|
|
121
123
|
body += buffer;
|
|
122
124
|
buffer = '';
|
|
123
|
-
return;
|
|
124
125
|
}
|
|
125
126
|
|
|
126
127
|
return function (res, fn) {
|
|
@@ -147,10 +148,8 @@ module.exports = function (foreachEvent, includeDeletions) {
|
|
|
147
148
|
});
|
|
148
149
|
};
|
|
149
150
|
|
|
150
|
-
|
|
151
151
|
/// --- Direct Push
|
|
152
|
-
function addEvent(strEvent) {
|
|
152
|
+
function addEvent (strEvent) {
|
|
153
153
|
foreachEvent(JSON.parse(strEvent));
|
|
154
154
|
}
|
|
155
|
-
|
|
156
|
-
};
|
|
155
|
+
};
|
package/src/utils.js
CHANGED
|
@@ -1,52 +1,53 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
|
|
4
|
+
*/
|
|
2
5
|
const regexAPIandToken = /(.+):\/\/(.+)@(.+)/gm;
|
|
3
6
|
const regexSchemaAndPath = /(.+):\/\/(.+)/gm;
|
|
4
7
|
|
|
5
8
|
/**
|
|
6
9
|
* Utilities to access Pryv API.
|
|
7
|
-
* Exposes superagent and methods to manipulate
|
|
8
|
-
* @memberof
|
|
9
|
-
* @namespace
|
|
10
|
+
* Exposes superagent and methods to manipulate API endpoints
|
|
11
|
+
* @memberof pryv
|
|
12
|
+
* @namespace pryv.utils
|
|
10
13
|
*/
|
|
11
|
-
const utils = {
|
|
12
|
-
|
|
14
|
+
const utils = module.exports = {
|
|
13
15
|
/**
|
|
14
16
|
* Exposes superagent https://visionmedia.github.io/superagent/
|
|
15
|
-
* @memberof
|
|
16
|
-
* @property {Superagent} superagent
|
|
17
|
+
* @memberof pryv.utils
|
|
18
|
+
* @property {Superagent} superagent
|
|
17
19
|
*/
|
|
18
20
|
superagent: require('superagent'),
|
|
19
21
|
|
|
20
22
|
/**
|
|
21
23
|
* Returns true is run in a browser
|
|
22
|
-
* @memberof
|
|
24
|
+
* @memberof pryv.utils
|
|
23
25
|
* @returns {boolean}
|
|
24
26
|
*/
|
|
25
|
-
isBrowser: function() {
|
|
26
|
-
|
|
27
|
+
isBrowser: function () {
|
|
28
|
+
return typeof window !== 'undefined' && typeof document !== 'undefined';
|
|
27
29
|
},
|
|
28
30
|
|
|
29
|
-
|
|
30
31
|
/**
|
|
31
|
-
* From a
|
|
32
|
-
* @memberof
|
|
33
|
-
* @param {
|
|
32
|
+
* From a APIEndpoint URL, return an object (TokenAndAPI) with two properties
|
|
33
|
+
* @memberof pryv.utils
|
|
34
|
+
* @param {APIEndpoint} apiEndpoint
|
|
34
35
|
* @returns {TokenAndEndpoint}
|
|
35
36
|
*/
|
|
36
|
-
|
|
37
|
+
extractTokenAndAPIEndpoint: function (apiEndpoint) {
|
|
37
38
|
regexAPIandToken.lastIndex = 0;
|
|
38
|
-
const res = regexAPIandToken.exec(
|
|
39
|
+
const res = regexAPIandToken.exec(apiEndpoint);
|
|
39
40
|
|
|
40
|
-
if (res !== null) {
|
|
41
|
+
if (res !== null) { // has token
|
|
41
42
|
// add a trailing '/' to end point if missing
|
|
42
43
|
if (!res[3].endsWith('/')) {
|
|
43
44
|
res[3] += '/';
|
|
44
45
|
}
|
|
45
|
-
return { endpoint: res[1] + '://' + res[3], token: res[2] }
|
|
46
|
+
return { endpoint: res[1] + '://' + res[3], token: res[2] };
|
|
46
47
|
}
|
|
47
48
|
// else check if valid url
|
|
48
49
|
regexSchemaAndPath.lastIndex = 0;
|
|
49
|
-
const res2 = regexSchemaAndPath.exec(
|
|
50
|
+
const res2 = regexSchemaAndPath.exec(apiEndpoint);
|
|
50
51
|
if (res2 === null) {
|
|
51
52
|
throw new Error('Cannot find endpoint, invalid URL format');
|
|
52
53
|
}
|
|
@@ -55,41 +56,42 @@ const utils = {
|
|
|
55
56
|
res2[2] += '/';
|
|
56
57
|
}
|
|
57
58
|
|
|
58
|
-
return { endpoint: res2[1] + '://' + res2[2]
|
|
59
|
+
return { endpoint: res2[1] + '://' + res2[2], token: null };
|
|
59
60
|
},
|
|
60
61
|
|
|
61
62
|
/**
|
|
62
|
-
* Get a
|
|
63
|
-
* @memberof
|
|
64
|
-
* @param {TokenAndEndpoint}
|
|
65
|
-
* @returns {
|
|
63
|
+
* Get a APIEndpoint URL from a TokenAndAPI object
|
|
64
|
+
* @memberof pryv.utils
|
|
65
|
+
* @param {TokenAndEndpoint} tokenAndAPI
|
|
66
|
+
* @returns {APIEndpoint}
|
|
66
67
|
*/
|
|
67
|
-
|
|
68
|
-
if (!
|
|
69
|
-
let res =
|
|
70
|
-
if (!
|
|
68
|
+
buildAPIEndpoint: function (tokenAndAPI) {
|
|
69
|
+
if (!tokenAndAPI.token) {
|
|
70
|
+
let res = tokenAndAPI.endpoint + '';
|
|
71
|
+
if (!tokenAndAPI.endpoint.endsWith('/')) {
|
|
71
72
|
res += '/';
|
|
72
73
|
}
|
|
73
|
-
return res;
|
|
74
|
+
return res;
|
|
74
75
|
}
|
|
75
76
|
regexSchemaAndPath.lastIndex = 0;
|
|
76
|
-
|
|
77
|
+
const res = regexSchemaAndPath.exec(tokenAndAPI.endpoint);
|
|
77
78
|
// add a trailing '/' to end point if missing
|
|
78
79
|
if (!res[2].endsWith('/')) {
|
|
79
80
|
res[2] += '/';
|
|
80
81
|
}
|
|
81
|
-
return res[1] + '://' +
|
|
82
|
+
return res[1] + '://' + tokenAndAPI.token + '@' + res[2];
|
|
82
83
|
},
|
|
83
84
|
|
|
84
85
|
/**
|
|
85
|
-
*
|
|
86
|
-
* @param {Object} [navigatorForTests] mock navigator var only for testing purposes
|
|
86
|
+
*
|
|
87
|
+
* @param {Object} [navigatorForTests] mock navigator var only for testing purposes
|
|
87
88
|
*/
|
|
88
89
|
browserIsMobileOrTablet: function (navigator) {
|
|
89
90
|
if (navigator == null) {
|
|
90
91
|
return false;
|
|
91
92
|
}
|
|
92
93
|
let check = false;
|
|
94
|
+
// eslint-disable-next-line no-useless-escape
|
|
93
95
|
(function (a) { if (/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(a) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0, 4))) check = true; })(navigator.userAgent || navigator.vendor || navigator.opera);
|
|
94
96
|
return check;
|
|
95
97
|
},
|
|
@@ -100,37 +102,47 @@ const utils = {
|
|
|
100
102
|
},
|
|
101
103
|
|
|
102
104
|
getQueryParamsFromURL: function (url) {
|
|
103
|
-
|
|
105
|
+
const vars = {};
|
|
104
106
|
const QUERY_REGEXP = /[?#&]+([^=&]+)=([^&]*)/g;
|
|
105
107
|
url.replace(QUERY_REGEXP,
|
|
106
108
|
function (m, key, value) {
|
|
107
109
|
vars[key] = decodeURIComponent(value);
|
|
108
110
|
});
|
|
109
111
|
return vars;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
+
}
|
|
113
|
+
};
|
|
112
114
|
|
|
113
|
-
|
|
115
|
+
// TODO: remove following deprecated aliases with next major version
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* @deprecated Renamed to `extractTokenAndAPIEndpoint()`
|
|
119
|
+
*/
|
|
120
|
+
utils.extractTokenAndApiEndpoint = utils.extractTokenAndAPIEndpoint;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* @deprecated Renamed to `buildAPIEndpoint()`
|
|
124
|
+
*/
|
|
125
|
+
// TODO: remove deprecated alias with next major version
|
|
126
|
+
utils.buildPryvApiEndpoint = utils.buildAPIEndpoint;
|
|
114
127
|
|
|
115
128
|
// --------------- typedfs ------------------------------- //
|
|
116
129
|
|
|
117
130
|
/**
|
|
118
131
|
* An object with two properties: token & apiEndpoint
|
|
119
132
|
* @typedef {Object} TokenAndEndpoint
|
|
120
|
-
* @property {string}
|
|
121
|
-
* @property {string}
|
|
133
|
+
* @property {string} [token] Authorization token
|
|
134
|
+
* @property {string} endpoint url of API endpoint
|
|
122
135
|
*/
|
|
123
136
|
|
|
124
137
|
/**
|
|
125
138
|
* A String url of the form http(s)://{token}@{apiEndpoint}
|
|
126
|
-
* @typedef {string}
|
|
139
|
+
* @typedef {string} APIEndpoint
|
|
127
140
|
*/
|
|
128
141
|
|
|
129
|
-
|
|
130
142
|
/**
|
|
131
143
|
* Common Meta are returned by each standard call on the API https://api.pryv.com/reference/#in-method-results
|
|
132
144
|
* @typedef {Object} CommonMeta
|
|
133
145
|
* @property {string} apiVersion The version of the API in the form {major}.{minor}.{revision}. Mirrored in HTTP header API-Version.
|
|
134
146
|
* @property {number} serverTime The current server time as a timestamp in second. Keeping track of server time is necessary to properly handle time in API calls.
|
|
135
147
|
* @property {string} serial The serial will change every time the core or register is updated. If you compare it with the serial of a previous response and notice a difference, you should reload the service information.
|
|
136
|
-
*/
|
|
148
|
+
*/
|
|
@@ -1,34 +1,38 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
|
|
4
|
+
*/
|
|
5
|
+
/* global describe, it, before, after, expect, testData, Browser */
|
|
6
|
+
/* eslint-disable no-unused-expressions */
|
|
2
7
|
|
|
3
8
|
const utils = require('../src/utils.js');
|
|
4
9
|
const Service = require('../src/Service');
|
|
5
10
|
const AuthController = require('../src/Auth/AuthController.js');
|
|
6
|
-
const testData = require('./test-data.js');
|
|
7
11
|
|
|
8
|
-
describe('Browser.LoginButton', function() {
|
|
9
|
-
this.timeout(5000);
|
|
12
|
+
describe('Browser.LoginButton', function () {
|
|
13
|
+
this.timeout(5000);
|
|
10
14
|
|
|
11
15
|
let auth;
|
|
12
16
|
let removeZombie = false;
|
|
13
|
-
before(async function() {
|
|
17
|
+
before(async function () {
|
|
14
18
|
if (typeof document !== 'undefined') return; // in browser
|
|
15
19
|
removeZombie = true;
|
|
16
20
|
const browser = new Browser();
|
|
17
|
-
browser.visit('./?pryvServiceInfoUrl=https://
|
|
21
|
+
browser.visit('./?pryvServiceInfoUrl=https://zou.zou/service/info');
|
|
18
22
|
global.document = browser.document;
|
|
19
23
|
global.window = browser.window;
|
|
20
24
|
global.location = browser.location;
|
|
21
25
|
global.navigator = { userAgent: 'Safari' };
|
|
22
26
|
});
|
|
23
27
|
|
|
24
|
-
after(async function() {
|
|
28
|
+
after(async function () {
|
|
25
29
|
if (!removeZombie) return; // in browser
|
|
26
30
|
delete global.document;
|
|
27
31
|
delete global.window;
|
|
28
32
|
delete global.location;
|
|
29
33
|
});
|
|
30
|
-
before(async function() {
|
|
31
|
-
|
|
34
|
+
before(async function () {
|
|
35
|
+
const service = new Service(testData.serviceInfoUrl);
|
|
32
36
|
await service.info();
|
|
33
37
|
auth = new AuthController({
|
|
34
38
|
authRequest: {
|
|
@@ -38,8 +42,8 @@ describe('Browser.LoginButton', function() {
|
|
|
38
42
|
}, service);
|
|
39
43
|
await auth.init();
|
|
40
44
|
});
|
|
41
|
-
|
|
42
|
-
it('getReturnURL()', async function() {
|
|
45
|
+
|
|
46
|
+
it('getReturnURL()', async function () {
|
|
43
47
|
const myUrl = 'https://mysite.com/bobby';
|
|
44
48
|
let error = null;
|
|
45
49
|
try {
|
|
@@ -57,24 +61,22 @@ describe('Browser.LoginButton', function() {
|
|
|
57
61
|
|
|
58
62
|
expect(auth.getReturnURL('http://zou.zou/toto#', myUrl, fakeNavigator)).to.equal('http://zou.zou/toto#');
|
|
59
63
|
|
|
60
|
-
fakeNavigator =
|
|
64
|
+
fakeNavigator = { userAgent: 'Safari' };
|
|
61
65
|
expect(auth.getReturnURL('auto#', myUrl, fakeNavigator)).to.equal(false);
|
|
62
66
|
expect(auth.getReturnURL('auto?', myUrl, fakeNavigator)).to.equal(false);
|
|
63
67
|
expect(auth.getReturnURL(false, myUrl, fakeNavigator)).to.equal(false);
|
|
64
68
|
expect(auth.getReturnURL('self?', myUrl, fakeNavigator)).to.equal(myUrl + '?');
|
|
65
69
|
expect(auth.getReturnURL('http://zou.zou/toto#', myUrl, fakeNavigator)).to.equal('http://zou.zou/toto#');
|
|
66
|
-
global.window = { location: { href: myUrl + '?prYvstatus=zouzou'} }
|
|
70
|
+
global.window = { location: { href: myUrl + '?prYvstatus=zouzou' } };
|
|
67
71
|
expect(auth.getReturnURL('self?', myUrl, fakeNavigator)).to.equal(myUrl + '?');
|
|
68
72
|
});
|
|
69
73
|
|
|
70
|
-
it('browserIsMobileOrTablet()', async function() {
|
|
74
|
+
it('browserIsMobileOrTablet()', async function () {
|
|
71
75
|
expect(utils.browserIsMobileOrTablet({ userAgent: 'android' })).to.be.true;
|
|
72
76
|
expect(utils.browserIsMobileOrTablet({ userAgent: 'Safari' })).to.be.false;
|
|
73
77
|
});
|
|
74
78
|
|
|
75
|
-
|
|
76
|
-
it('cleanURLFromPrYvParams()', async function() {
|
|
77
|
-
|
|
79
|
+
it('cleanURLFromPrYvParams()', async function () {
|
|
78
80
|
expect('https://my.Url.com/?bobby=2').to.equal(utils.cleanURLFromPrYvParams(
|
|
79
81
|
'https://my.Url.com/?bobby=2&prYvZoutOu=1&prYvstatus=2jsadh'));
|
|
80
82
|
|
|
@@ -89,9 +91,5 @@ describe('Browser.LoginButton', function() {
|
|
|
89
91
|
|
|
90
92
|
expect('https://my.Url.com/#bobby=2').to.equal(utils.cleanURLFromPrYvParams(
|
|
91
93
|
'https://my.Url.com/#bobby=2&prYvZoutOu=1&prYvstatus=2jsadh'));
|
|
92
|
-
|
|
93
94
|
});
|
|
94
|
-
|
|
95
95
|
});
|
|
96
|
-
|
|
97
|
-
|
package/test/Browser.test.js
CHANGED
|
@@ -1,24 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
|
|
4
|
+
*/
|
|
5
|
+
/* global describe, it, before, after, expect, Browser, pryv, testData */
|
|
6
|
+
/* eslint-disable no-unused-expressions */
|
|
1
7
|
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const testData = require('./test-data.js');
|
|
6
|
-
|
|
7
|
-
function genSettings() {
|
|
8
|
-
function defaultStateChange(state) {
|
|
9
|
-
console.log('Test unimplemented on state change', state);
|
|
8
|
+
function genSettings () {
|
|
9
|
+
function defaultStateChange (state) {
|
|
10
|
+
console.log('Test unimplemented on state change', state);
|
|
10
11
|
}
|
|
11
12
|
return {
|
|
12
13
|
authRequest: {
|
|
13
14
|
requestingAppId: 'lib-js-test',
|
|
14
|
-
requestedPermissions: [{ streamId: '*', level: 'read' }]
|
|
15
|
+
requestedPermissions: [{ streamId: '*', level: 'read' }]
|
|
15
16
|
},
|
|
16
17
|
onStateChange: defaultStateChange
|
|
17
18
|
};
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
describe('Browser', function () {
|
|
21
|
-
this.timeout(5000);
|
|
22
|
+
this.timeout(5000);
|
|
22
23
|
|
|
23
24
|
before(async function () {
|
|
24
25
|
this.timeout(5000);
|
|
@@ -31,11 +32,11 @@ describe('Browser', function () {
|
|
|
31
32
|
if (typeof document !== 'undefined') return; // in browser
|
|
32
33
|
removeZombie = true;
|
|
33
34
|
const browser = new Browser();
|
|
34
|
-
browser.visit('./?pryvServiceInfoUrl=https://
|
|
35
|
+
browser.visit('./?pryvServiceInfoUrl=https://zou.zou/service/info');
|
|
35
36
|
global.document = browser.document;
|
|
36
37
|
global.window = browser.window;
|
|
37
38
|
global.location = browser.location;
|
|
38
|
-
global.navigator = {userAgent: 'Safari'};
|
|
39
|
+
global.navigator = { userAgent: 'Safari' };
|
|
39
40
|
});
|
|
40
41
|
|
|
41
42
|
after(async () => {
|
|
@@ -49,31 +50,27 @@ describe('Browser', function () {
|
|
|
49
50
|
const settings = genSettings();
|
|
50
51
|
let AuthLoaded = false;
|
|
51
52
|
settings.onStateChange = function (state) {
|
|
52
|
-
|
|
53
|
-
if (state.id
|
|
53
|
+
expect(state.id).to.exist;
|
|
54
|
+
if (state.id === pryv.Auth.AuthStates.LOADING) {
|
|
54
55
|
AuthLoaded = true;
|
|
55
56
|
}
|
|
56
|
-
if (state.id
|
|
57
|
+
if (state.id === pryv.Auth.AuthStates.INITIALIZED) {
|
|
57
58
|
expect(AuthLoaded).to.true;
|
|
58
59
|
}
|
|
59
|
-
}
|
|
60
|
+
};
|
|
60
61
|
|
|
61
62
|
try {
|
|
62
|
-
const service = await
|
|
63
|
+
const service = await pryv.Auth.setupAuth(settings, testData.serviceInfoUrl);
|
|
63
64
|
const serviceInfo = service.infoSync();
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
} catch(error) {
|
|
65
|
+
expect(serviceInfo.access).to.exist;
|
|
66
|
+
expect(serviceInfo.serial).to.exist;
|
|
67
|
+
} catch (error) {
|
|
67
68
|
console.log(error);
|
|
68
|
-
|
|
69
|
+
expect(error).to.not.exist;
|
|
69
70
|
}
|
|
70
71
|
});
|
|
71
72
|
|
|
72
|
-
|
|
73
73
|
it('serviceInfoFromUrl()', async () => {
|
|
74
|
-
expect('https://
|
|
74
|
+
expect('https://zou.zou/service/info').to.equal(pryv.Browser.serviceInfoFromUrl());
|
|
75
75
|
});
|
|
76
|
-
|
|
77
76
|
});
|
|
78
|
-
|
|
79
|
-
|