12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- 'use strict';
- var url = require('url');
- var getCurrentScriptSource = require('./getCurrentScriptSource');
- function createSocketUrl(resourceQuery, currentLocation) {
- var urlParts;
- if (typeof resourceQuery === 'string' && resourceQuery !== '') {
-
-
- urlParts = url.parse(resourceQuery
- .substr(1)
- .replace('&', '?'), true);
- } else {
-
- var scriptHost = getCurrentScriptSource();
- urlParts = url.parse(scriptHost || '/', true, true);
- }
- if (typeof currentLocation === 'string' && currentLocation !== '') {
- currentLocation = url.parse(currentLocation);
- } else {
- currentLocation = self.location;
- }
- return getSocketUrl(urlParts, currentLocation);
- }
- function getSocketUrl(urlParts, loc) {
- var auth = urlParts.auth,
- query = urlParts.query;
- var hostname = urlParts.hostname,
- protocol = urlParts.protocol,
- port = urlParts.port;
- if (!port || port === '0') {
- port = loc.port;
- }
-
-
-
- if ((hostname === '0.0.0.0' || hostname === '::') && loc.hostname && loc.protocol.indexOf('http') === 0) {
- hostname = loc.hostname;
- }
-
-
-
- if (hostname && hostname !== '127.0.0.1' && (loc.protocol === 'https:' || urlParts.hostname === '0.0.0.0')) {
- protocol = loc.protocol;
- }
-
-
- var sockHost = query.sockHost || hostname;
- var sockPath = query.sockPath || '/sockjs-node';
- var sockPort = query.sockPort || port;
- if (sockPort === 'location') {
- sockPort = loc.port;
- }
- return url.format({
- protocol: protocol,
- auth: auth,
- hostname: sockHost,
- port: sockPort,
-
-
-
- pathname: sockPath
- });
- }
- module.exports = createSocketUrl;
|