Selaa lähdekoodia

Create config util

master
Ilya Daynatovich 8 vuotta sitten
vanhempi
commit
96b1f0ca74

+ 15
- 12
connection_optimization/do_external_connect.js Näytä tiedosto

@@ -1,7 +1,9 @@
1
-/* global config,
2
-          createConnectionExternally,
3
-          getConfigParamsFromUrl,
4
-          getRoomName */
1
+/* global config, createConnectionExternally */
2
+
3
+import {
4
+    getRoomName,
5
+    parseURLParams
6
+} from '../react/features/base/config/functions';
5 7
 
6 8
 /**
7 9
  * Implements external connect using createConnectionExternally function defined
@@ -15,14 +17,10 @@
15 17
  * external_connect.js.
16 18
  */
17 19
 
18
-const hashParams = getConfigParamsFromUrl('hash', true);
19
-const searchParams = getConfigParamsFromUrl('search', true);
20
+const hashParams = parseURLParams(window.location, true);
20 21
 
21 22
 // URL params have higher proirity than config params.
22
-let url
23
-    = hashParams.hasOwnProperty('config.externalConnectUrl')
24
-        ? hashParams['config.externalConnectUrl']
25
-        : config.externalConnectUrl;
23
+let url = hashParams['config.externalConnectUrl'] || config.externalConnectUrl;
26 24
 
27 25
 if (url && window.createConnectionExternally) {
28 26
     const roomName = getRoomName();
@@ -30,9 +28,14 @@ if (url && window.createConnectionExternally) {
30 28
     if (roomName) {
31 29
         url += `?room=${roomName}`;
32 30
 
33
-        const token
34
-            = hashParams['config.token'] || config.token || searchParams.jwt;
31
+        let token = hashParams['config.token'] || config.token;
32
+
33
+        if (!token) {
34
+            const searchParams
35
+                = parseURLParams(window.location, true, 'search');
35 36
 
37
+            token = searchParams.jwt;
38
+        }
36 39
         if (token) {
37 40
             url += `&token=${token}`;
38 41
         }

+ 0
- 1
index.html Näytä tiedosto

@@ -127,7 +127,6 @@
127 127
             'error', loadErrHandler, true /* capture phase type of listener */);
128 128
     </script>
129 129
     <script><!--#include virtual="/config.js" --></script><!-- adapt to your needs, i.e. set hosts and bosh path -->
130
-    <script src="static/utils.js?v=1"></script>
131 130
     <!--#include virtual="connection_optimization/connection_optimization.html" -->
132 131
     <script src="libs/do_external_connect.min.js?v=1"></script>
133 132
     <script><!--#include virtual="/interface_config.js" --></script>

+ 8
- 8
modules/config/URLProcessor.js Näytä tiedosto

@@ -1,13 +1,13 @@
1
-/* global config, getConfigParamsFromUrl, interfaceConfig, loggingConfig */
1
+/* global config, interfaceConfig, loggingConfig */
2 2
 
3 3
 const logger = require("jitsi-meet-logger").getLogger(__filename);
4 4
 
5
-var configUtils = require('./Util');
6
-var params = {};
5
+import { getConfigParamsFromUrl } from '../../react/features/base/config';
7 6
 
8
-if (typeof getConfigParamsFromUrl === 'function') {
9
-    params = getConfigParamsFromUrl();
10
-}
7
+import configUtils from './Util';
8
+
9
+// Parsing config params from URL hash.
10
+const URL_PARAMS = getConfigParamsFromUrl(window.location);
11 11
 
12 12
 var URLProcessor = {
13 13
     setConfigParametersFromUrl: function () {
@@ -33,7 +33,7 @@ var URLProcessor = {
33 33
             interfaceConfig: {},
34 34
             loggingConfig: {}
35 35
         };
36
-        for (var key in params) {
36
+        for (var key in URL_PARAMS) {
37 37
             if (typeof key !== "string") {
38 38
                 logger.warn("Invalid config key: ", key);
39 39
                 continue;
@@ -59,7 +59,7 @@ var URLProcessor = {
59 59
             if (!confObj)
60 60
                 continue;
61 61
 
62
-            confObj[confKey] = params[key];
62
+            confObj[confKey] = URL_PARAMS[key];
63 63
         }
64 64
         configUtils.overrideConfigJSON(
65 65
             config, interfaceConfig, loggingConfig, configJSON);

+ 4
- 2
modules/tokendata/TokenData.js Näytä tiedosto

@@ -1,4 +1,4 @@
1
-/* global  getConfigParamsFromUrl, config */
1
+/* global config */
2 2
 
3 3
 /**
4 4
  * Parses and handles JWT tokens. Sets config.token.
@@ -6,10 +6,12 @@
6 6
 
7 7
 import * as jws from "jws";
8 8
 
9
+import { getConfigParamsFromUrl } from '../../react/features/base/config';
10
+
9 11
 /**
10 12
  * Get the JWT token from the URL.
11 13
  */
12
-let params = getConfigParamsFromUrl("search", true);
14
+let params = getConfigParamsFromUrl(window.location, true, 'search');
13 15
 let jwt = params.jwt;
14 16
 
15 17
 /**

+ 72
- 0
react/features/base/config/functions.js Näytä tiedosto

@@ -0,0 +1,72 @@
1
+/* @flow */
2
+
3
+declare var config: Object;
4
+
5
+/**
6
+ * Builds and returns the room name.
7
+ *
8
+ * @returns {string}
9
+ */
10
+export function getRoomName(): ?string {
11
+    const { getroomnode } = config;
12
+    const path = window.location.pathname;
13
+    let roomName;
14
+
15
+    // Determine the room node from the URL.
16
+    if (getroomnode && typeof getroomnode === 'function') {
17
+        roomName = getroomnode.call(config, path);
18
+    } else {
19
+        // Fall back to the default strategy of making assumptions about how the
20
+        // URL maps to the room (name). It currently assumes a deployment in
21
+        // which the last non-directory component of the path (name) is the
22
+        // room.
23
+        roomName
24
+            = path.substring(path.lastIndexOf('/') + 1).toLowerCase()
25
+                || undefined;
26
+    }
27
+
28
+    return roomName;
29
+}
30
+
31
+/**
32
+ * Parses the parameters from the URL and returns them as a JS object.
33
+ *
34
+ * @param {string} url - URL to parse.
35
+ * @param {boolean} dontParse - If false or undefined some transformations
36
+ * (for parsing the value as JSON) are going to be executed.
37
+ * @param {string} source - Values - "hash"/"search" if "search" the parameters
38
+ * will parsed from location.search otherwise from location.hash.
39
+ * @returns {Object}
40
+ */
41
+export function parseURLParams(
42
+        url: URL,
43
+        dontParse: boolean = false,
44
+        source: string = 'hash'): Object {
45
+    const paramStr = source === 'search' ? url.search : url.hash;
46
+    const params = {};
47
+
48
+    // eslint-disable-next-line newline-per-chained-call
49
+    paramStr && paramStr.substr(1).split('&').forEach(part => {
50
+        const param = part.split('=');
51
+        let value;
52
+
53
+        try {
54
+            value = param[1];
55
+            if (!dontParse) {
56
+                value
57
+                    = JSON.parse(
58
+                        decodeURIComponent(param[1]).replace(/\\&/, '&'));
59
+            }
60
+        } catch (e) {
61
+            const msg = `Failed to parse URL parameter value: ${String(value)}`;
62
+
63
+            console.warn(msg, e);
64
+            window.onerror && window.onerror(msg, null, null, null, e);
65
+
66
+            return;
67
+        }
68
+        params[param[0]] = value;
69
+    });
70
+
71
+    return params;
72
+}

+ 1
- 0
react/features/base/config/index.js Näytä tiedosto

@@ -1,4 +1,5 @@
1 1
 export * from './actions';
2 2
 export * from './actionTypes';
3
+export * from './functions';
3 4
 
4 5
 import './reducer';

+ 0
- 64
static/utils.js Näytä tiedosto

@@ -1,64 +0,0 @@
1
-/* global config */
2
-
3
-/**
4
- * Defines some utility methods that are used before the other JS files are
5
- * loaded.
6
- */
7
-
8
-
9
-/**
10
- * Builds and returns the room name.
11
- */
12
-function getRoomName () { // eslint-disable-line no-unused-vars
13
-    var getroomnode = config.getroomnode;
14
-    var path = window.location.pathname;
15
-    var roomName;
16
-
17
-    // Determine the room node from the URL.
18
-    if (getroomnode && typeof getroomnode === 'function') {
19
-        // custom function might be responsible for doing the pushstate
20
-        roomName = getroomnode.call(config, path);
21
-    } else {
22
-        // Fall back to the default strategy of making assumptions about how the
23
-        // URL maps to the room (name). It currently assumes a deployment in
24
-        // which the last non-directory component of the path (name) is the
25
-        // room.
26
-        roomName
27
-            = path.substring(path.lastIndexOf('/') + 1).toLowerCase()
28
-                || undefined;
29
-    }
30
-
31
-    return roomName;
32
-}
33
-
34
-/**
35
- * Parses the parameters from the URL and returns them as a JS object.
36
- * @param source {string} values - "hash"/"search" if "search" the parameters
37
- * will parsed from location.search otherwise from location.hash
38
- * @param dontParse if false or undefined some transformations
39
- * (for parsing the value as JSON) are going to be executed
40
- */
41
-// eslint-disable-next-line no-unused-vars
42
-function getConfigParamsFromUrl(source, dontParse) {
43
-    var paramStr = (source === "search")? location.search : location.hash;
44
-    if (!paramStr)
45
-        return {};
46
-    paramStr = paramStr.substr(1);
47
-    var result = {};
48
-    paramStr.split("&").forEach(function (part) {
49
-        var item = part.split("=");
50
-        var value;
51
-        try {
52
-            value = (dontParse)? item[1] : JSON.parse(
53
-                decodeURIComponent(item[1]).replace(/\\&/, "&"));
54
-        } catch (e) {
55
-            console.warn("Failed to parse URL argument", e);
56
-            if(window.onerror)
57
-                window.onerror("Failed to parse URL argument", null, null,
58
-                    null, e);
59
-            return;
60
-        }
61
-        result[item[0]] = value;
62
-    });
63
-    return result;
64
-}

Loading…
Peruuta
Tallenna