pryv 2.2.0 → 2.3.2

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 (46) hide show
  1. package/README.md +291 -299
  2. package/package.json +11 -45
  3. package/src/Auth/AuthController.js +43 -50
  4. package/src/Auth/AuthStates.js +12 -12
  5. package/src/Auth/LoginMessages.js +17 -14
  6. package/src/Auth/index.js +18 -15
  7. package/src/Browser/CookieUtils.js +37 -27
  8. package/src/Browser/LoginButton.js +42 -37
  9. package/src/Browser/index.js +16 -42
  10. package/src/Connection.js +102 -95
  11. package/src/Service.js +47 -45
  12. package/src/ServiceAssets.js +42 -34
  13. package/src/browser-index-bundle.js +8 -0
  14. package/src/browser-index.js +7 -0
  15. package/src/index.d.ts +37 -48
  16. package/src/index.js +20 -3
  17. package/src/lib/browser-getEventStreamed.js +21 -19
  18. package/src/lib/json-parser.js +23 -24
  19. package/src/utils.js +55 -43
  20. package/test/Browser.AuthController.test.js +19 -21
  21. package/test/Browser.test.js +23 -26
  22. package/test/Connection.test.js +135 -151
  23. package/test/Service.test.js +30 -44
  24. package/test/ServiceAssets.test.js +16 -22
  25. package/test/browser-index.html +26 -0
  26. package/test/utils.test.js +30 -35
  27. package/.jsdoc-conf.json +0 -29
  28. package/.mocharc.js +0 -13
  29. package/LICENSE.md +0 -27
  30. package/scripts/setup-environment-dev.sh +0 -28
  31. package/scripts/upload.sh +0 -15
  32. package/src/Pryv.js +0 -19
  33. package/src/index-socket.io-monitor.js +0 -4
  34. package/src/index.html +0 -17
  35. package/test/browser-index.js +0 -11
  36. package/test/browser-tests.html +0 -31
  37. package/test/helpers.js +0 -8
  38. package/test/load-test-account.js +0 -108
  39. package/test/test-data.js +0 -95
  40. package/web-demos/auth-with-redirection.html +0 -72
  41. package/web-demos/auth.html +0 -77
  42. package/web-demos/custom-login-button.html +0 -158
  43. package/web-demos/index.html +0 -186
  44. package/web-demos/service-info.json +0 -13
  45. package/web-demos/stream-examples.html +0 -80
  46. package/webpack.config.js +0 -83
package/package.json CHANGED
@@ -1,58 +1,24 @@
1
1
  {
2
2
  "name": "pryv",
3
- "version": "2.2.0",
4
- "homepage": "https://github.com/pryv/lib-js",
5
- "description": "Pryv Javascript Library",
3
+ "version": "2.3.2",
4
+ "description": "Pryv JavaScript library",
6
5
  "keywords": [
7
- "Pryv"
6
+ "Pryv",
7
+ "Pryv.io"
8
8
  ],
9
- "repository": {
10
- "type": "git",
11
- "url": "git://github.com/pryv/lib-js"
12
- },
9
+ "homepage": "https://github.com/pryv/lib-js",
13
10
  "bugs": {
14
11
  "url": "https://github.com/pryv/lib-js/issues"
15
12
  },
16
- "author": {
17
- "name": "Pryv S.A."
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git://github.com/pryv/lib-js"
18
16
  },
17
+ "license": "BSD-3-Clause",
18
+ "author": "Pryv S.A. <info@pryv.com> (https://pryv.com)",
19
19
  "main": "src/index.js",
20
20
  "types": "src/index.d.ts",
21
- "license": "BSD-3-Clause",
22
- "scripts": {
23
- "setup": "./scripts/setup-environment-dev.sh",
24
- "build": "webpack --config webpack.config.js",
25
- "doc": "npx jsdoc -c .jsdoc-conf.json",
26
- "test": "mocha --reporter spec test/**/*.test.js --timeout 3000",
27
- "test-debug": "mocha --inpect-brk=40000 --reporter spec test/**/*.test.js ",
28
- "test:browser": "npm run build; ",
29
- "cover": "nyc npm run test && npm run cover:report",
30
- "cover:report": "nyc report --reporter=lcov --reporter=html",
31
- "webserver": "./node_modules/.bin/rec-la ./dist 9443",
32
- "gh-pages": "./scripts/upload.sh",
33
- "clear": "rm -r dist/ && npm run setup"
34
- },
35
- "contributors": [
36
- {
37
- "name": "Pierre-Mikael Legris",
38
- "email": "perki@pryv.com"
39
- }
40
- ],
41
21
  "dependencies": {
42
- "superagent": "^7.0.2"
43
- },
44
- "devDependencies": {
45
- "@pryv/lib-js-common": "github:pryv/lib-js-common#1.0.6",
46
- "@pryv/monitor": "^1.0.6",
47
- "@pryv/socket.io": "^1.0.6",
48
- "chai": "^4.3.4",
49
- "chai-as-promised": "^7.1.1",
50
- "cuid": "^2.1.8",
51
- "mocha": "^9.1.3",
52
- "node-fetch": "^2.6.1",
53
- "nyc": "^15.1.0",
54
- "rec.la": "latest",
55
- "universal-url": "^2.0.0",
56
- "zombie": "^6.1.4"
22
+ "superagent": "^7.1.6"
57
23
  }
58
24
  }
@@ -1,5 +1,8 @@
1
+ /**
2
+ * @license
3
+ * [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
4
+ */
1
5
  const utils = require('../utils');
2
- const Service = require('../Service');
3
6
  const AuthStates = require('./AuthStates');
4
7
  const Messages = require('./LoginMessages');
5
8
 
@@ -7,11 +10,10 @@ const Messages = require('./LoginMessages');
7
10
  * @private
8
11
  */
9
12
  class AuthController {
10
-
11
13
  /**
12
- *
13
- * @param {*} settings
14
- * @param {*} service
14
+ *
15
+ * @param {*} settings
16
+ * @param {*} service
15
17
  */
16
18
  constructor (settings, service, loginButton) {
17
19
  this.settings = settings;
@@ -24,20 +26,20 @@ class AuthController {
24
26
  this.service = service;
25
27
 
26
28
  // probably remove
27
- this.languageCode = this.settings.authRequest.languageCode || 'en';
29
+ this.languageCode = this.settings.authRequest.languageCode || 'en';
28
30
  this.messages = Messages(this.languageCode);
29
31
 
30
32
  this.loginButton = loginButton;
31
33
 
32
34
  function validateSettings (settings) {
33
35
  if (!settings) { throw new Error('settings cannot be null'); }
34
- // -- settings
36
+ // -- settings
35
37
  if (!settings.authRequest) { throw new Error('Missing settings.authRequest'); }
36
-
37
- // -- Extract returnURL
38
+
39
+ // -- Extract returnURL
38
40
  settings.authRequest.returnURL =
39
41
  this.getReturnURL(settings.authRequest.returnURL);
40
-
42
+
41
43
  if (!settings.authRequest.requestingAppId) {
42
44
  throw new Error('Missing settings.authRequest.requestingAppId');
43
45
  }
@@ -49,14 +51,14 @@ class AuthController {
49
51
 
50
52
  /**
51
53
  * async function to call right after instanciating object
52
- *
53
- * @returns {PryvService}
54
+ *
55
+ * @returns {Service}
54
56
  */
55
57
  async init () {
56
58
  this.serviceInfo = this.service.infoSync();
57
59
  this.state = { status: AuthStates.LOADING };
58
60
  this.assets = await loadAssets(this);
59
-
61
+
60
62
  const loginButton = this.loginButton;
61
63
  // initialize human interaction interface
62
64
  if (loginButton != null) {
@@ -66,14 +68,14 @@ class AuthController {
66
68
  }
67
69
 
68
70
  // if auto login is not prompted
69
- if (this.state.status != AuthStates.AUTHORIZED) {
70
- this.state = { status: AuthStates.INITIALIZED, serviceInfo: this.serviceInfo};
71
+ if (this.state.status !== AuthStates.AUTHORIZED) {
72
+ this.state = { status: AuthStates.INITIALIZED, serviceInfo: this.serviceInfo };
71
73
  }
72
74
 
73
75
  if (loginButton != null && loginButton.finishAuthProcessAfterRedirection != null) {
74
76
  await loginButton.finishAuthProcessAfterRedirection(this);
75
77
  }
76
-
78
+
77
79
  return this.service;
78
80
  }
79
81
 
@@ -93,14 +95,14 @@ class AuthController {
93
95
  } else if (isInitialized.call(this)) {
94
96
  this.startAuthRequest();
95
97
  } else if (isNeedSignIn.call(this)) {
96
- // reopen popup
97
- this.state = this.state;
98
+ // reopen popup (HACK for now: set to private property to avoid self-assignment)
99
+ this.state = this._state;
98
100
  } else {
99
101
  console.log('Unhandled action in "handleClick()" for status:', this.state.status);
100
102
  }
101
103
 
102
104
  function isAuthorized () {
103
- return this.state.status == AuthStates.AUTHORIZED;
105
+ return this.state.status === AuthStates.AUTHORIZED;
104
106
  }
105
107
  function isInitialized () {
106
108
  return this.state.status === AuthStates.INITIALIZED;
@@ -112,10 +114,10 @@ class AuthController {
112
114
 
113
115
  /**
114
116
  * Used only to retrieve returnUrl in browser environments
115
- *
116
- * @param {*} returnURL
117
- * @param {*} windowLocationForTest
118
- * @param {*} navigatorForTests
117
+ *
118
+ * @param {*} returnURL
119
+ * @param {*} windowLocationForTest
120
+ * @param {*} navigatorForTests
119
121
  */
120
122
  getReturnURL (
121
123
  returnURL,
@@ -127,24 +129,19 @@ class AuthController {
127
129
  returnURL = returnURL || RETURN_URL_AUTO + '#';
128
130
 
129
131
  // check the trailer
130
- let trailer = returnURL.slice(-1);
132
+ const trailer = returnURL.slice(-1);
131
133
  if ('#&?'.indexOf(trailer) < 0) {
132
134
  throw new Error('Pryv access: Last character of --returnURL setting-- is not ' +
133
135
  '"?", "&" or "#": ' + returnURL);
134
136
  }
135
137
  // auto mode for desktop
136
- if (
137
- returnUrlIsAuto(returnURL) &&
138
- !utils.browserIsMobileOrTablet(navigatorForTests)
139
- ) {
138
+ if (returnUrlIsAuto(returnURL) &&
139
+ !utils.browserIsMobileOrTablet(navigatorForTests)) {
140
140
  return false;
141
- } else if (
142
- // auto mode for mobile or self
143
- (returnUrlIsAuto(returnURL) &&
144
- utils.browserIsMobileOrTablet(navigatorForTests)
145
- )
146
- || returnURL.indexOf('self') === 0
147
- ) {
141
+ // auto mode for mobile or self
142
+ } else if ((returnUrlIsAuto(returnURL) &&
143
+ utils.browserIsMobileOrTablet(navigatorForTests)) ||
144
+ returnURL.indexOf('self') === 0) {
148
145
  // set self as return url?
149
146
  // eventually clean-up current url from previous pryv returnURL
150
147
  const locationHref = windowLocationForTest || window.location.href;
@@ -159,9 +156,9 @@ class AuthController {
159
156
 
160
157
  async startAuthRequest () {
161
158
  this.state = await postAccess.call(this);
162
-
159
+
163
160
  await doPolling.call(this);
164
-
161
+
165
162
  async function postAccess () {
166
163
  try {
167
164
  const res = await utils.superagent
@@ -178,22 +175,21 @@ class AuthController {
178
175
  }
179
176
  }
180
177
 
181
- async function doPolling() {
178
+ async function doPolling () {
182
179
  if (this.state.status !== AuthStates.NEED_SIGNIN) {
183
180
  return;
184
181
  }
185
182
  const pollResponse = await pollAccess(this.state.poll);
186
-
183
+
187
184
  if (pollResponse.status === AuthStates.NEED_SIGNIN) {
188
185
  setTimeout(await doPolling.bind(this), this.state.poll_rate_ms);
189
186
  } else {
190
187
  this.state = pollResponse;
191
188
  }
192
189
 
193
- async function pollAccess(pollUrl) {
190
+ async function pollAccess (pollUrl) {
194
191
  try {
195
- const res = await utils.superagent
196
- .get(pollUrl)
192
+ const res = await utils.superagent.get(pollUrl);
197
193
  return res.body;
198
194
  } catch (e) {
199
195
  if (e.response &&
@@ -207,21 +203,18 @@ class AuthController {
207
203
  }
208
204
  }
209
205
  }
210
-
211
-
212
206
  }
213
207
 
214
208
  // -------------- state listeners ---------------------
215
209
  set state (newState) {
216
-
217
210
  // retro-compatibility for lib-js < 2.0.9
218
211
  newState.id = newState.status;
219
212
 
220
213
  this._state = newState;
221
214
 
222
- this.stateChangeListeners.map((listener) => {
215
+ this.stateChangeListeners.forEach((listener) => {
223
216
  try {
224
- listener(this.state)
217
+ listener(this.state);
225
218
  } catch (e) {
226
219
  console.log('Error during set state ()', e);
227
220
  }
@@ -243,13 +236,13 @@ async function checkAutoLogin (authController) {
243
236
 
244
237
  const storedCredentials = await loginButton.getAuthorizationData();
245
238
  if (storedCredentials != null) {
246
- authController.state = Object.assign({}, {status: AuthStates.AUTHORIZED}, storedCredentials);
239
+ authController.state = Object.assign({}, { status: AuthStates.AUTHORIZED }, storedCredentials);
247
240
  }
248
241
  }
249
242
 
250
243
  // ------------------ ACTIONS ----------- //
251
244
 
252
- async function loadAssets(authController) {
245
+ async function loadAssets (authController) {
253
246
  let loadedAssets = {};
254
247
  try {
255
248
  loadedAssets = await authController.service.assets();
@@ -259,7 +252,7 @@ async function loadAssets(authController) {
259
252
  if (thisMessages.LOADING) {
260
253
  authController.messages = Messages(authController.languageCode, thisMessages);
261
254
  } else {
262
- console.log("WARNING Messages cannot be loaded using defaults: ", thisMessages)
255
+ console.log('WARNING Messages cannot be loaded using defaults: ', thisMessages);
263
256
  }
264
257
  }
265
258
  } catch (e) {
@@ -1,20 +1,20 @@
1
-
2
-
3
1
  /**
4
- * Enum Possible states: ERROR, LOADING, INITIALIZED, AUTHORIZED, SIGNOUT
2
+ * @license
3
+ * [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
4
+ */
5
+ /**
6
+ * The possible auth states:
7
+ * ERROR, LOADING, INITIALIZED, NEED_SIGNIN, AUTHORIZED, SIGNOUT, REFUSED
5
8
  * @readonly
6
9
  * @enum {string}
7
- * @memberof Pryv.Browser
10
+ * @memberof pryv.Browser
8
11
  */
9
- const AuthState = {
10
- ERROR : 'ERROR',
11
- LOADING : 'LOADING',
12
+ module.exports = {
13
+ ERROR: 'ERROR',
14
+ LOADING: 'LOADING',
12
15
  INITIALIZED: 'INITIALIZED',
13
16
  NEED_SIGNIN: 'NEED_SIGNIN',
14
17
  AUTHORIZED: 'ACCEPTED',
15
18
  SIGNOUT: 'SIGNOUT',
16
- REFUSED: 'REFUSED',
17
- }
18
-
19
-
20
- module.exports = AuthState
19
+ REFUSED: 'REFUSED'
20
+ };
@@ -1,29 +1,32 @@
1
+ /**
2
+ * @license
3
+ * [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
4
+ */
5
+ module.exports = get;
6
+
1
7
  const Messages = {
2
8
  LOADING: {
3
- 'en': '...'
9
+ en: '...'
4
10
  },
5
11
  ERROR: {
6
- 'en': 'Error',
7
- 'fr': 'Erreur',
12
+ en: 'Error',
13
+ fr: 'Erreur'
8
14
  },
9
15
  LOGIN: {
10
- 'en': 'Signin',
11
- 'fr': 'Login'
16
+ en: 'Signin',
17
+ fr: 'Login'
12
18
  },
13
19
  SIGNOUT_CONFIRM: {
14
- 'en': 'Logout ?',
15
- 'fr': 'Se deconnecter ?'
20
+ en: 'Logout?',
21
+ fr: 'Se déconnecter ?'
16
22
  }
17
- }
23
+ };
18
24
 
19
- function get(languageCode, definitions) {
25
+ function get (languageCode, definitions) {
20
26
  const myMessages = definitions || Messages;
21
27
  const res = {};
22
- Object.keys(myMessages).map((key) => {
23
- res[key] = myMessages[key][languageCode] || myMessages[key]['en'];
28
+ Object.keys(myMessages).forEach((key) => {
29
+ res[key] = myMessages[key][languageCode] || myMessages[key].en;
24
30
  });
25
31
  return res;
26
32
  }
27
-
28
-
29
- module.exports = get;
package/src/Auth/index.js CHANGED
@@ -1,43 +1,46 @@
1
+ /**
2
+ * @license
3
+ * [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
4
+ */
1
5
  const AuthController = require('./AuthController');
2
6
  const AuthStates = require('./AuthStates');
3
7
  const LoginButton = require('../Browser/LoginButton');
4
8
  const Service = require('../Service');
5
9
 
6
10
  /**
7
- * @memberof Pryv
8
- * @namespace Pryv.Auth
11
+ * @memberof pryv
12
+ * @namespace pryv.Auth
9
13
  */
14
+ module.exports = {
15
+ setupAuth,
16
+ AuthStates,
17
+ AuthController
18
+ };
10
19
 
11
20
  /**
12
21
  * Start an authentication process
13
22
  *
14
- * @memberof Pryv.Auth
23
+ * @memberof pryv.Auth
15
24
  * @param {Object} settings
16
25
  * @param {Object} settings.authRequest See https://api.pryv.com/reference/#data-structure-access
17
26
  * @param {string} [settings.authRequest.languageCode] Language code, as per LoginButton Messages: 'en', 'fr
18
27
  * @param {string} settings.authRequest.requestingAppId Application id, ex: 'my-app'
19
28
  * @param {Object} settings.authRequest.requestedPermissions
20
- * @param {string | boolean} settings.authRequest.returnURL : false, // set this if you don't want a popup
21
- * @param {string} [settings.authRequest.referer] To track registration source
22
- * @param {string} settings.spanButtonID set and <span> id in DOM to insert default login button or null for custom
29
+ * @param {string | boolean} settings.authRequest.returnURL : false, // set this if you don't want a popup
30
+ * @param {string} [settings.authRequest.referer] To track registration source
31
+ * @param {string} settings.spanButtonID set and <span> id in DOM to insert default login button or null for custom
23
32
  * @param {Browser.AuthStateChangeHandler} settings.onStateChange
24
33
  * @param {string} [settings.returnURL=auto#] Set to "self#" to disable popup and force using the same page. Set a custom url when process is finished (specific use cases). Should always end by # ? or &
25
34
  * @param {string} serviceInfoUrl
26
- * @param {Object} [serviceCustomizations] override properties of serviceInfoUrl
27
- * @returns {Pryv.Service}
35
+ * @param {Object} [serviceCustomizations] override properties of serviceInfoUrl
36
+ * @returns {pryv.Service}
28
37
  */
29
38
  async function setupAuth (settings, serviceInfoUrl, serviceCustomizations, HumanInteraction = LoginButton) {
30
39
  const service = new Service(serviceInfoUrl, serviceCustomizations);
31
- await service.info()
40
+ await service.info();
32
41
 
33
42
  const humanInteraction = new HumanInteraction(settings, service);
34
43
  await humanInteraction.init();
35
44
 
36
45
  return service;
37
46
  }
38
-
39
- module.exports = {
40
- setupAuth: setupAuth,
41
- AuthStates: AuthStates,
42
- AuthController: AuthController,
43
- }
@@ -1,51 +1,61 @@
1
+ /**
2
+ * @license
3
+ * [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
4
+ */
1
5
 
2
6
  const utils = require('../utils');
7
+
3
8
  /**
4
- * @memberof Pryv.Browser
5
- * @namespace Pryv.Browser.CookieUtils
9
+ * @memberof pryv.Browser
10
+ * @namespace pryv.Browser.CookieUtils
6
11
  */
12
+ module.exports = {
13
+ get,
14
+ set,
15
+ del
16
+ };
7
17
 
8
18
  /**
9
- * Set a Local cookier
10
- * @memberof Pryv.Browser.CookieUtils
19
+ * Set a local cookie
20
+ * @memberof pryv.Browser.CookieUtils
11
21
  * @param {string} cookieKey - The key for the cookie
12
- * @param {mixed} value - The Value
22
+ * @param {mixed} value - The Value
13
23
  * @param {number} expireInDays - Expiration date in days from now
14
24
  */
15
- function set(cookieKey, value, expireInDays) {
16
- if (! utils.isBrowser()) return;
25
+ function set (cookieKey, value, expireInDays) {
26
+ if (!utils.isBrowser()) return;
17
27
  expireInDays = expireInDays || 365;
18
- var myDate = new Date();
19
- var hostName = window.location.hostname;
20
- var path = window.location.pathname;
28
+ const myDate = new Date();
29
+ const hostName = window.location.hostname;
30
+ const path = window.location.pathname;
21
31
  myDate.setDate(myDate.getDate() + expireInDays);
22
- var cookieStr = encodeURIComponent(cookieKey) + '=' + encodeURIComponent(JSON.stringify(value))
23
- + ';expires=' + myDate.toGMTString()
24
- + ';domain=.' + hostName + ';path=' + path;
25
- // do not add SameSite when removing a cookie
32
+ let cookieStr = encodeURIComponent(cookieKey) + '=' +
33
+ encodeURIComponent(JSON.stringify(value)) +
34
+ ';expires=' + myDate.toGMTString() +
35
+ ';domain=.' + hostName + ';path=' + path;
36
+ // do not add SameSite when removing a cookie
26
37
  if (expireInDays >= 0) cookieStr += ';SameSite=Strict';
27
38
  document.cookie = cookieStr;
28
39
  }
29
- exports.set = set;
30
40
 
31
41
  /**
32
- * returns the value of a local cookie
33
- * @memberof Pryv.Browser.CookieUtils
42
+ * Return the value of a local cookie
43
+ * @memberof pryv.Browser.CookieUtils
34
44
  * @param cookieKey - The key
35
45
  */
36
- exports.get = function get(cookieKey) {
46
+ function get (cookieKey) {
37
47
  const name = encodeURIComponent(cookieKey);
38
- if (! utils.isBrowser()) return;
39
- var value = "; " + document.cookie;
40
- var parts = value.split("; " + name + "=");
41
- if (parts.length == 2) return JSON.parse(decodeURIComponent(parts.pop().split(";").shift()));
48
+ if (!utils.isBrowser()) return;
49
+ const value = '; ' + document.cookie;
50
+ const parts = value.split('; ' + name + '=');
51
+ if (parts.length === 2) return JSON.parse(decodeURIComponent(parts.pop().split(';').shift()));
42
52
  }
43
53
 
44
54
  /**
45
- * delete a local cookie
46
- * @memberof Pryv.Browser.CookieUtils
55
+ * Delete a local cookie
56
+ * @memberof pryv.Browser.CookieUtils
47
57
  * @param cookieKey - The key
48
58
  */
49
- exports.del = function del(cookieKey) {
50
- set(cookieKey, {deleted: true}, -1);
51
- }
59
+ function del (cookieKey) {
60
+ set(cookieKey, { deleted: true }, -1);
61
+ }