source: trip-planner-front/node_modules/sockjs-client/lib/transport/receiver/htmlfile.js@ ceaed42

Last change on this file since ceaed42 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.2 KB
Line 
1'use strict';
2
3var inherits = require('inherits')
4 , iframeUtils = require('../../utils/iframe')
5 , urlUtils = require('../../utils/url')
6 , EventEmitter = require('events').EventEmitter
7 , random = require('../../utils/random')
8 ;
9
10var debug = function() {};
11if (process.env.NODE_ENV !== 'production') {
12 debug = require('debug')('sockjs-client:receiver:htmlfile');
13}
14
15function HtmlfileReceiver(url) {
16 debug(url);
17 EventEmitter.call(this);
18 var self = this;
19 iframeUtils.polluteGlobalNamespace();
20
21 this.id = 'a' + random.string(6);
22 url = urlUtils.addQuery(url, 'c=' + decodeURIComponent(iframeUtils.WPrefix + '.' + this.id));
23
24 debug('using htmlfile', HtmlfileReceiver.htmlfileEnabled);
25 var constructFunc = HtmlfileReceiver.htmlfileEnabled ?
26 iframeUtils.createHtmlfile : iframeUtils.createIframe;
27
28 global[iframeUtils.WPrefix][this.id] = {
29 start: function() {
30 debug('start');
31 self.iframeObj.loaded();
32 }
33 , message: function(data) {
34 debug('message', data);
35 self.emit('message', data);
36 }
37 , stop: function() {
38 debug('stop');
39 self._cleanup();
40 self._close('network');
41 }
42 };
43 this.iframeObj = constructFunc(url, function() {
44 debug('callback');
45 self._cleanup();
46 self._close('permanent');
47 });
48}
49
50inherits(HtmlfileReceiver, EventEmitter);
51
52HtmlfileReceiver.prototype.abort = function() {
53 debug('abort');
54 this._cleanup();
55 this._close('user');
56};
57
58HtmlfileReceiver.prototype._cleanup = function() {
59 debug('_cleanup');
60 if (this.iframeObj) {
61 this.iframeObj.cleanup();
62 this.iframeObj = null;
63 }
64 delete global[iframeUtils.WPrefix][this.id];
65};
66
67HtmlfileReceiver.prototype._close = function(reason) {
68 debug('_close', reason);
69 this.emit('close', null, reason);
70 this.removeAllListeners();
71};
72
73HtmlfileReceiver.htmlfileEnabled = false;
74
75// obfuscate to avoid firewalls
76var axo = ['Active'].concat('Object').join('X');
77if (axo in global) {
78 try {
79 HtmlfileReceiver.htmlfileEnabled = !!new global[axo]('htmlfile');
80 } catch (x) {
81 // intentionally empty
82 }
83}
84
85HtmlfileReceiver.enabled = HtmlfileReceiver.htmlfileEnabled || iframeUtils.iframeEnabled;
86
87module.exports = HtmlfileReceiver;
Note: See TracBrowser for help on using the repository browser.