1 line
12 KiB
JSON
1 line
12 KiB
JSON
|
{"ast":null,"code":"'use strict';\n\nrequire('./shims');\n\nvar URL = require('url-parse'),\n inherits = require('inherits'),\n JSON3 = require('json3'),\n random = require('./utils/random'),\n escape = require('./utils/escape'),\n urlUtils = require('./utils/url'),\n eventUtils = require('./utils/event'),\n transport = require('./utils/transport'),\n objectUtils = require('./utils/object'),\n browser = require('./utils/browser'),\n log = require('./utils/log'),\n Event = require('./event/event'),\n EventTarget = require('./event/eventtarget'),\n loc = require('./location'),\n CloseEvent = require('./event/close'),\n TransportMessageEvent = require('./event/trans-message'),\n InfoReceiver = require('./info-receiver');\n\nvar debug = function debug() {};\n\nif (process.env.NODE_ENV !== 'production') {\n debug = require('debug')('sockjs-client:main');\n}\n\nvar transports; // follow constructor steps defined at http://dev.w3.org/html5/websockets/#the-websocket-interface\n\nfunction SockJS(url, protocols, options) {\n if (!(this instanceof SockJS)) {\n return new SockJS(url, protocols, options);\n }\n\n if (arguments.length < 1) {\n throw new TypeError(\"Failed to construct 'SockJS: 1 argument required, but only 0 present\");\n }\n\n EventTarget.call(this);\n this.readyState = SockJS.CONNECTING;\n this.extensions = '';\n this.protocol = ''; // non-standard extension\n\n options = options || {};\n\n if (options.protocols_whitelist) {\n log.warn(\"'protocols_whitelist' is DEPRECATED. Use 'transports' instead.\");\n }\n\n this._transportsWhitelist = options.transports;\n this._transportOptions = options.transportOptions || {};\n var sessionId = options.sessionId || 8;\n\n if (typeof sessionId === 'function') {\n this._generateSessionId = sessionId;\n } else if (typeof sessionId === 'number') {\n this._generateSessionId = function () {\n return random.string(sessionId);\n };\n } else {\n throw new TypeError('If sessionId is used in the options, it needs to be a number or a function.');\n }\n\n this._server = options.server || random.numberString(1000); // Step 1 of WS spec - parse and validate the url. Issue #8\n\n var parsedUrl = new URL(url);\n\n if (!parsedUrl.host || !parsedUrl.protocol) {\n throw new SyntaxError(\"The URL '\" + url + \"' is invalid\");\n } else if (parsedUrl.hash) {\n throw new SyntaxError('The URL must not contain a fragment');\n } else if (parsedUrl.protocol !== 'http:' && parsedUrl.protocol !== 'https:') {\n throw new SyntaxError(\"The URL's scheme must be either 'http:' or 'https:'. '\" + parsedUrl.protocol + \"' is not allowed.\");\n }\n\n var secure = parsedUrl.protocol === 'https:'; // Step 2 - don't allow secure origin with an insecure protocol\n\n if (loc.protocol === 'https:' && !secure) {\n throw new Error('SecurityError: An insecure SockJS connection may not be initiated from a page loaded over HTTPS');\n } // Step 3 - check port access - no need here\n // Step 4 - parse protocols argument\n\n\n if (!protocols) {\n protocols = [];\n } else if (!Array.isArray(protocols)) {\n protocols = [protocols];\n } // Step 5 - check protocols argument\n\n\n var sortedProtocols = protocols.sort();\n sortedProtocols.forEach(function (proto, i) {\n if (!proto) {\n throw new SyntaxError(\"The protocols entry '\" + proto + \"' is invalid.\");\n }\n\n if (i < sortedProtocols.length - 1 && proto === sortedProtocols[i + 1]) {\n throw new SyntaxError(\"The protocols entry '\" + proto + \"' is duplicated.\");\n }\n }); // Step 6 - convert origin\n\n var o = urlUtils.getOrigin(loc.href);\n this._origin = o ? o.toLowerCase() : null; // remove the trailing slash\n\n parsedUrl.set('pathname', parsedUrl.pathname.replace(/\\/+$/, '')); // store the sanitized url\n\n this.url = parsedUrl.href;\n debug('using url', this.url); // Step 7 - start connection in background\n // obtain server info\n // http://sockjs.github.io/sockjs-protocol/sockjs-protocol-0.3.3.html#section-
|