Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / ws / lib / Sender.hixie.js
1 /*!
2  * ws: a node.js websocket client
3  * Copyright(c) 2011 Einar Otto Stangvik <einaros@gmail.com>
4  * MIT Licensed
5  */
6
7 var events = require('events')
8   , util = require('util')
9   , EventEmitter = events.EventEmitter;
10
11 /**
12  * Hixie Sender implementation
13  */
14
15 function Sender(socket) {
16   this.socket = socket;
17   this.continuationFrame = false;
18   this.isClosed = false;
19 }
20
21 module.exports = Sender;
22
23 /**
24  * Inherits from EventEmitter.
25  */
26
27 util.inherits(Sender, events.EventEmitter);
28
29 /**
30  * Frames and writes data.
31  *
32  * @api public
33  */
34
35 Sender.prototype.send = function(data, options, cb) {
36   if (this.isClosed) return;
37
38   var isString = typeof data == 'string'
39     , length = isString ? Buffer.byteLength(data) : data.length
40     , lengthbytes = (length > 127) ? 2 : 1 // assume less than 2**14 bytes
41     , writeStartMarker = this.continuationFrame == false
42     , writeEndMarker = !options || !(typeof options.fin != 'undefined' && !options.fin)
43     , buffer = new Buffer((writeStartMarker ? ((options && options.binary) ? (1 + lengthbytes) : 1) : 0) + length + ((writeEndMarker && !(options && options.binary)) ? 1 : 0))
44     , offset = writeStartMarker ? 1 : 0;
45
46   if (writeStartMarker) {
47     if (options && options.binary) {
48       buffer.write('\x80', 'binary');
49       // assume length less than 2**14 bytes
50       if (lengthbytes > 1)
51         buffer.write(String.fromCharCode(128+length/128), offset++, 'binary');
52       buffer.write(String.fromCharCode(length&0x7f), offset++, 'binary');
53     } else
54       buffer.write('\x00', 'binary');
55   }
56
57   if (isString) buffer.write(data, offset, 'utf8');
58   else data.copy(buffer, offset, 0);
59
60   if (writeEndMarker) {
61     if (options && options.binary) {
62       // sending binary, not writing end marker
63     } else
64       buffer.write('\xff', offset + length, 'binary');
65     this.continuationFrame = false;
66   }
67   else this.continuationFrame = true;
68
69   try {
70     this.socket.write(buffer, 'binary', cb);
71   } catch (e) {
72     this.error(e.toString());
73   }
74 };
75
76 /**
77  * Sends a close instruction to the remote party.
78  *
79  * @api public
80  */
81
82 Sender.prototype.close = function(code, data, mask, cb) {
83   if (this.isClosed) return;
84   this.isClosed = true;
85   try {
86     if (this.continuationFrame) this.socket.write(new Buffer([0xff], 'binary'));
87     this.socket.write(new Buffer([0xff, 0x00]), 'binary', cb);
88   } catch (e) {
89     this.error(e.toString());
90   }
91 };
92
93 /**
94  * Sends a ping message to the remote party. Not available for hixie.
95  *
96  * @api public
97  */
98
99 Sender.prototype.ping = function(data, options) {};
100
101 /**
102  * Sends a pong message to the remote party. Not available for hixie.
103  *
104  * @api public
105  */
106
107 Sender.prototype.pong = function(data, options) {};
108
109 /**
110  * Handles an error
111  *
112  * @api private
113  */
114
115 Sender.prototype.error = function (reason) {
116   this.emit('error', reason);
117   return this;
118 };