zmdms-utils 0.0.81 → 0.0.83

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (162) hide show
  1. package/dist/es/validate.js +168 -1
  2. package/dist/index.d.ts +0 -1
  3. package/dist/index.js +0 -1
  4. package/package.json +2 -6
  5. package/src/index.ts +0 -3
  6. package/src/validate.ts +198 -0
  7. package/dist/es/_virtual/FastBufferList.js +0 -7
  8. package/dist/es/_virtual/_commonjs-dynamic-modules.js +0 -5
  9. package/dist/es/_virtual/browser.js +0 -7
  10. package/dist/es/_virtual/browser2.js +0 -7
  11. package/dist/es/_virtual/debug.js +0 -7
  12. package/dist/es/_virtual/event.js +0 -7
  13. package/dist/es/_virtual/http-parser.js +0 -3
  14. package/dist/es/_virtual/iframe.js +0 -7
  15. package/dist/es/_virtual/index.js +0 -7
  16. package/dist/es/_virtual/index2.js +0 -7
  17. package/dist/es/_virtual/index3.js +0 -7
  18. package/dist/es/_virtual/index4.js +0 -7
  19. package/dist/es/_virtual/index5.js +0 -7
  20. package/dist/es/_virtual/index6.js +0 -7
  21. package/dist/es/_virtual/index7.js +0 -3
  22. package/dist/es/_virtual/index8.js +0 -7
  23. package/dist/es/_virtual/index9.js +0 -7
  24. package/dist/es/_virtual/inherits.js +0 -7
  25. package/dist/es/_virtual/inherits_browser.js +0 -7
  26. package/dist/es/_virtual/json3.js +0 -7
  27. package/dist/es/_virtual/node.js +0 -7
  28. package/dist/es/_virtual/node2.js +0 -7
  29. package/dist/es/_virtual/stomp-node.js +0 -3
  30. package/dist/es/_virtual/stomp.js +0 -3
  31. package/dist/es/_virtual/streams.js +0 -3
  32. package/dist/es/_virtual/utils.js +0 -3
  33. package/dist/es/node_modules/bufferutil/fallback.js +0 -43
  34. package/dist/es/node_modules/bufferutil/index.js +0 -21
  35. package/dist/es/node_modules/debug/src/browser.js +0 -192
  36. package/dist/es/node_modules/debug/src/common.js +0 -259
  37. package/dist/es/node_modules/debug/src/index.js +0 -25
  38. package/dist/es/node_modules/debug/src/node.js +0 -192
  39. package/dist/es/node_modules/eventsource/lib/eventsource.js +0 -493
  40. package/dist/es/node_modules/faye-websocket/lib/faye/eventsource.js +0 -150
  41. package/dist/es/node_modules/faye-websocket/lib/faye/websocket/api/event.js +0 -31
  42. package/dist/es/node_modules/faye-websocket/lib/faye/websocket/api/event_target.js +0 -41
  43. package/dist/es/node_modules/faye-websocket/lib/faye/websocket/api.js +0 -205
  44. package/dist/es/node_modules/faye-websocket/lib/faye/websocket/client.js +0 -107
  45. package/dist/es/node_modules/faye-websocket/lib/faye/websocket.js +0 -47
  46. package/dist/es/node_modules/has-flag/index.js +0 -17
  47. package/dist/es/node_modules/http-parser-js/http-parser.js +0 -471
  48. package/dist/es/node_modules/inherits/inherits.js +0 -14
  49. package/dist/es/node_modules/inherits/inherits_browser.js +0 -38
  50. package/dist/es/node_modules/is-typedarray/index.js +0 -51
  51. package/dist/es/node_modules/json3/lib/json3.js +0 -937
  52. package/dist/es/node_modules/ms/index.js +0 -172
  53. package/dist/es/node_modules/node-gyp-build/index.js +0 -21
  54. package/dist/es/node_modules/node-gyp-build/node-gyp-build.js +0 -222
  55. package/dist/es/node_modules/querystringify/index.js +0 -120
  56. package/dist/es/node_modules/requires-port/index.js +0 -38
  57. package/dist/es/node_modules/safe-buffer/index.js +0 -71
  58. package/dist/es/node_modules/supports-color/index.js +0 -143
  59. package/dist/es/node_modules/typedarray-to-buffer/index.js +0 -37
  60. package/dist/es/node_modules/url-parse/index.js +0 -594
  61. package/dist/es/node_modules/utf-8-validate/fallback.js +0 -71
  62. package/dist/es/node_modules/utf-8-validate/index.js +0 -21
  63. package/dist/es/node_modules/websocket/index.js +0 -15
  64. package/dist/es/node_modules/websocket/lib/Deprecation.js +0 -42
  65. package/dist/es/node_modules/websocket/lib/W3CWebSocket.js +0 -268
  66. package/dist/es/node_modules/websocket/lib/WebSocketClient.js +0 -380
  67. package/dist/es/node_modules/websocket/lib/WebSocketConnection.js +0 -914
  68. package/dist/es/node_modules/websocket/lib/WebSocketFrame.js +0 -293
  69. package/dist/es/node_modules/websocket/lib/WebSocketRequest.js +0 -543
  70. package/dist/es/node_modules/websocket/lib/WebSocketRouter.js +0 -172
  71. package/dist/es/node_modules/websocket/lib/WebSocketRouterRequest.js +0 -67
  72. package/dist/es/node_modules/websocket/lib/WebSocketServer.js +0 -272
  73. package/dist/es/node_modules/websocket/lib/utils.js +0 -80
  74. package/dist/es/node_modules/websocket/lib/version.js +0 -13
  75. package/dist/es/node_modules/websocket/lib/websocket.js +0 -31
  76. package/dist/es/node_modules/websocket/node_modules/debug/src/browser.js +0 -199
  77. package/dist/es/node_modules/websocket/node_modules/debug/src/debug.js +0 -215
  78. package/dist/es/node_modules/websocket/node_modules/debug/src/index.js +0 -25
  79. package/dist/es/node_modules/websocket/node_modules/debug/src/node.js +0 -266
  80. package/dist/es/node_modules/websocket/node_modules/ms/index.js +0 -162
  81. package/dist/es/node_modules/websocket/package.json.js +0 -77
  82. package/dist/es/node_modules/websocket/vendor/FastBufferList.js +0 -205
  83. package/dist/es/node_modules/websocket-driver/lib/websocket/driver/base.js +0 -202
  84. package/dist/es/node_modules/websocket-driver/lib/websocket/driver/client.js +0 -152
  85. package/dist/es/node_modules/websocket-driver/lib/websocket/driver/draft75.js +0 -128
  86. package/dist/es/node_modules/websocket-driver/lib/websocket/driver/draft76.js +0 -124
  87. package/dist/es/node_modules/websocket-driver/lib/websocket/driver/headers.js +0 -44
  88. package/dist/es/node_modules/websocket-driver/lib/websocket/driver/hybi/frame.js +0 -21
  89. package/dist/es/node_modules/websocket-driver/lib/websocket/driver/hybi/message.js +0 -37
  90. package/dist/es/node_modules/websocket-driver/lib/websocket/driver/hybi.js +0 -492
  91. package/dist/es/node_modules/websocket-driver/lib/websocket/driver/proxy.js +0 -108
  92. package/dist/es/node_modules/websocket-driver/lib/websocket/driver/server.js +0 -119
  93. package/dist/es/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js +0 -72
  94. package/dist/es/node_modules/websocket-driver/lib/websocket/driver.js +0 -47
  95. package/dist/es/node_modules/websocket-driver/lib/websocket/http_parser.js +0 -140
  96. package/dist/es/node_modules/websocket-driver/lib/websocket/streams.js +0 -150
  97. package/dist/es/node_modules/websocket-extensions/lib/parser.js +0 -103
  98. package/dist/es/node_modules/websocket-extensions/lib/pipeline/cell.js +0 -56
  99. package/dist/es/node_modules/websocket-extensions/lib/pipeline/functor.js +0 -74
  100. package/dist/es/node_modules/websocket-extensions/lib/pipeline/index.js +0 -50
  101. package/dist/es/node_modules/websocket-extensions/lib/pipeline/pledge.js +0 -39
  102. package/dist/es/node_modules/websocket-extensions/lib/pipeline/ring_buffer.js +0 -66
  103. package/dist/es/node_modules/websocket-extensions/lib/websocket_extensions.js +0 -165
  104. package/dist/es/node_modules/yaeti/index.js +0 -17
  105. package/dist/es/node_modules/yaeti/lib/Event.js +0 -24
  106. package/dist/es/node_modules/yaeti/lib/EventTarget.js +0 -130
  107. package/dist/es/node_modules/zt-sockjs-client/lib/entry.js +0 -14
  108. package/dist/es/node_modules/zt-sockjs-client/lib/event/close.js +0 -21
  109. package/dist/es/node_modules/zt-sockjs-client/lib/event/event.js +0 -22
  110. package/dist/es/node_modules/zt-sockjs-client/lib/event/eventtarget.js +0 -62
  111. package/dist/es/node_modules/zt-sockjs-client/lib/event/trans-message.js +0 -19
  112. package/dist/es/node_modules/zt-sockjs-client/lib/facade.js +0 -40
  113. package/dist/es/node_modules/zt-sockjs-client/lib/iframe-bootstrap.js +0 -122
  114. package/dist/es/node_modules/zt-sockjs-client/lib/info-ajax.js +0 -57
  115. package/dist/es/node_modules/zt-sockjs-client/lib/info-iframe-receiver.js +0 -50
  116. package/dist/es/node_modules/zt-sockjs-client/lib/info-iframe.js +0 -81
  117. package/dist/es/node_modules/zt-sockjs-client/lib/info-receiver.js +0 -101
  118. package/dist/es/node_modules/zt-sockjs-client/lib/location.js +0 -12
  119. package/dist/es/node_modules/zt-sockjs-client/lib/main.js +0 -413
  120. package/dist/es/node_modules/zt-sockjs-client/lib/shims.js +0 -449
  121. package/dist/es/node_modules/zt-sockjs-client/lib/transport/driver/websocket.js +0 -5
  122. package/dist/es/node_modules/zt-sockjs-client/lib/transport/driver/xhr.js +0 -80
  123. package/dist/es/node_modules/zt-sockjs-client/lib/transport/eventsource.js +0 -34
  124. package/dist/es/node_modules/zt-sockjs-client/lib/transport/htmlfile.js +0 -31
  125. package/dist/es/node_modules/zt-sockjs-client/lib/transport/iframe.js +0 -154
  126. package/dist/es/node_modules/zt-sockjs-client/lib/transport/jsonp-polling.js +0 -41
  127. package/dist/es/node_modules/zt-sockjs-client/lib/transport/lib/ajax-based.js +0 -55
  128. package/dist/es/node_modules/zt-sockjs-client/lib/transport/lib/buffered-sender.js +0 -92
  129. package/dist/es/node_modules/zt-sockjs-client/lib/transport/lib/iframe-wrap.js +0 -39
  130. package/dist/es/node_modules/zt-sockjs-client/lib/transport/lib/polling.js +0 -65
  131. package/dist/es/node_modules/zt-sockjs-client/lib/transport/lib/sender-receiver.js +0 -52
  132. package/dist/es/node_modules/zt-sockjs-client/lib/transport/receiver/eventsource.js +0 -69
  133. package/dist/es/node_modules/zt-sockjs-client/lib/transport/receiver/htmlfile.js +0 -96
  134. package/dist/es/node_modules/zt-sockjs-client/lib/transport/receiver/jsonp.js +0 -193
  135. package/dist/es/node_modules/zt-sockjs-client/lib/transport/receiver/xhr.js +0 -75
  136. package/dist/es/node_modules/zt-sockjs-client/lib/transport/sender/jsonp.js +0 -104
  137. package/dist/es/node_modules/zt-sockjs-client/lib/transport/sender/xdr.js +0 -113
  138. package/dist/es/node_modules/zt-sockjs-client/lib/transport/sender/xhr-cors.js +0 -19
  139. package/dist/es/node_modules/zt-sockjs-client/lib/transport/sender/xhr-fake.js +0 -28
  140. package/dist/es/node_modules/zt-sockjs-client/lib/transport/sender/xhr-local.js +0 -21
  141. package/dist/es/node_modules/zt-sockjs-client/lib/transport/websocket.js +0 -108
  142. package/dist/es/node_modules/zt-sockjs-client/lib/transport/xdr-polling.js +0 -30
  143. package/dist/es/node_modules/zt-sockjs-client/lib/transport/xdr-streaming.js +0 -38
  144. package/dist/es/node_modules/zt-sockjs-client/lib/transport/xhr-polling.js +0 -40
  145. package/dist/es/node_modules/zt-sockjs-client/lib/transport/xhr-streaming.js +0 -50
  146. package/dist/es/node_modules/zt-sockjs-client/lib/transport-list.js +0 -28
  147. package/dist/es/node_modules/zt-sockjs-client/lib/utils/browser.js +0 -29
  148. package/dist/es/node_modules/zt-sockjs-client/lib/utils/escape.js +0 -53
  149. package/dist/es/node_modules/zt-sockjs-client/lib/utils/event.js +0 -78
  150. package/dist/es/node_modules/zt-sockjs-client/lib/utils/iframe.js +0 -205
  151. package/dist/es/node_modules/zt-sockjs-client/lib/utils/log.js +0 -20
  152. package/dist/es/node_modules/zt-sockjs-client/lib/utils/object.js +0 -24
  153. package/dist/es/node_modules/zt-sockjs-client/lib/utils/random.js +0 -30
  154. package/dist/es/node_modules/zt-sockjs-client/lib/utils/transport.js +0 -52
  155. package/dist/es/node_modules/zt-sockjs-client/lib/utils/url.js +0 -54
  156. package/dist/es/node_modules/zt-sockjs-client/lib/version.js +0 -11
  157. package/dist/es/node_modules/zt-stompjs/index.js +0 -23
  158. package/dist/es/node_modules/zt-stompjs/lib/stomp-node.js +0 -117
  159. package/dist/es/node_modules/zt-stompjs/lib/stomp.js +0 -536
  160. package/dist/es/stompClient.d.ts +0 -55
  161. package/dist/es/stompClient.js +0 -379
  162. package/src/stompClient.ts +0 -449
@@ -1,914 +0,0 @@
1
- import { commonjsGlobal } from '../../../_virtual/_commonjsHelpers.js';
2
- import require$$1$1 from 'util';
3
- import { __require as requireUtils } from './utils.js';
4
- import require$$1 from 'events';
5
- import { __require as requireWebSocketFrame } from './WebSocketFrame.js';
6
- import { __require as requireFastBufferList } from '../vendor/FastBufferList.js';
7
- import { __require as requireUtf8Validate } from '../../utf-8-validate/index.js';
8
-
9
- /************************************************************************
10
- * Copyright 2010-2015 Brian McKelvey.
11
- *
12
- * Licensed under the Apache License, Version 2.0 (the "License");
13
- * you may not use this file except in compliance with the License.
14
- * You may obtain a copy of the License at
15
- *
16
- * http://www.apache.org/licenses/LICENSE-2.0
17
- *
18
- * Unless required by applicable law or agreed to in writing, software
19
- * distributed under the License is distributed on an "AS IS" BASIS,
20
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
- * See the License for the specific language governing permissions and
22
- * limitations under the License.
23
- ***********************************************************************/
24
-
25
- var WebSocketConnection_1;
26
- var hasRequiredWebSocketConnection;
27
-
28
- function requireWebSocketConnection () {
29
- if (hasRequiredWebSocketConnection) return WebSocketConnection_1;
30
- hasRequiredWebSocketConnection = 1;
31
- var util = require$$1$1;
32
- var utils = requireUtils();
33
- var EventEmitter = require$$1.EventEmitter;
34
- var WebSocketFrame = requireWebSocketFrame();
35
- var BufferList = requireFastBufferList();
36
- var isValidUTF8 = requireUtf8Validate();
37
- var bufferAllocUnsafe = utils.bufferAllocUnsafe;
38
- var bufferFromString = utils.bufferFromString;
39
-
40
- // Connected, fully-open, ready to send and receive frames
41
- const STATE_OPEN = 'open';
42
- // Received a close frame from the remote peer
43
- const STATE_PEER_REQUESTED_CLOSE = 'peer_requested_close';
44
- // Sent close frame to remote peer. No further data can be sent.
45
- const STATE_ENDING = 'ending';
46
- // Connection is fully closed. No further data can be sent or received.
47
- const STATE_CLOSED = 'closed';
48
-
49
- var setImmediateImpl = ('setImmediate' in commonjsGlobal) ?
50
- commonjsGlobal.setImmediate.bind(commonjsGlobal) :
51
- process.nextTick.bind(process);
52
-
53
- var idCounter = 0;
54
-
55
- function WebSocketConnection(socket, extensions, protocol, maskOutgoingPackets, config) {
56
- this._debug = utils.BufferingLogger('websocket:connection', ++idCounter);
57
- this._debug('constructor');
58
-
59
- if (this._debug.enabled) {
60
- instrumentSocketForDebugging(this, socket);
61
- }
62
-
63
- // Superclass Constructor
64
- EventEmitter.call(this);
65
-
66
- this._pingListenerCount = 0;
67
- this.on('newListener', function(ev) {
68
- if (ev === 'ping'){
69
- this._pingListenerCount++;
70
- }
71
- }).on('removeListener', function(ev) {
72
- if (ev === 'ping') {
73
- this._pingListenerCount--;
74
- }
75
- });
76
-
77
- this.config = config;
78
- this.socket = socket;
79
- this.protocol = protocol;
80
- this.extensions = extensions;
81
- this.remoteAddress = socket.remoteAddress;
82
- this.closeReasonCode = -1;
83
- this.closeDescription = null;
84
- this.closeEventEmitted = false;
85
-
86
- // We have to mask outgoing packets if we're acting as a WebSocket client.
87
- this.maskOutgoingPackets = maskOutgoingPackets;
88
-
89
- // We re-use the same buffers for the mask and frame header for all frames
90
- // received on each connection to avoid a small memory allocation for each
91
- // frame.
92
- this.maskBytes = bufferAllocUnsafe(4);
93
- this.frameHeader = bufferAllocUnsafe(10);
94
-
95
- // the BufferList will handle the data streaming in
96
- this.bufferList = new BufferList();
97
-
98
- // Prepare for receiving first frame
99
- this.currentFrame = new WebSocketFrame(this.maskBytes, this.frameHeader, this.config);
100
- this.fragmentationSize = 0; // data received so far...
101
- this.frameQueue = [];
102
-
103
- // Various bits of connection state
104
- this.connected = true;
105
- this.state = STATE_OPEN;
106
- this.waitingForCloseResponse = false;
107
- // Received TCP FIN, socket's readable stream is finished.
108
- this.receivedEnd = false;
109
-
110
- this.closeTimeout = this.config.closeTimeout;
111
- this.assembleFragments = this.config.assembleFragments;
112
- this.maxReceivedMessageSize = this.config.maxReceivedMessageSize;
113
-
114
- this.outputBufferFull = false;
115
- this.inputPaused = false;
116
- this.receivedDataHandler = this.processReceivedData.bind(this);
117
- this._closeTimerHandler = this.handleCloseTimer.bind(this);
118
-
119
- // Disable nagle algorithm?
120
- this.socket.setNoDelay(this.config.disableNagleAlgorithm);
121
-
122
- // Make sure there is no socket inactivity timeout
123
- this.socket.setTimeout(0);
124
-
125
- if (this.config.keepalive && !this.config.useNativeKeepalive) {
126
- if (typeof(this.config.keepaliveInterval) !== 'number') {
127
- throw new Error('keepaliveInterval must be specified and numeric ' +
128
- 'if keepalive is true.');
129
- }
130
- this._keepaliveTimerHandler = this.handleKeepaliveTimer.bind(this);
131
- this.setKeepaliveTimer();
132
-
133
- if (this.config.dropConnectionOnKeepaliveTimeout) {
134
- if (typeof(this.config.keepaliveGracePeriod) !== 'number') {
135
- throw new Error('keepaliveGracePeriod must be specified and ' +
136
- 'numeric if dropConnectionOnKeepaliveTimeout ' +
137
- 'is true.');
138
- }
139
- this._gracePeriodTimerHandler = this.handleGracePeriodTimer.bind(this);
140
- }
141
- }
142
- else if (this.config.keepalive && this.config.useNativeKeepalive) {
143
- if (!('setKeepAlive' in this.socket)) {
144
- throw new Error('Unable to use native keepalive: unsupported by ' +
145
- 'this version of Node.');
146
- }
147
- this.socket.setKeepAlive(true, this.config.keepaliveInterval);
148
- }
149
-
150
- // The HTTP Client seems to subscribe to socket error events
151
- // and re-dispatch them in such a way that doesn't make sense
152
- // for users of our client, so we want to make sure nobody
153
- // else is listening for error events on the socket besides us.
154
- this.socket.removeAllListeners('error');
155
- }
156
-
157
- WebSocketConnection.CLOSE_REASON_NORMAL = 1000;
158
- WebSocketConnection.CLOSE_REASON_GOING_AWAY = 1001;
159
- WebSocketConnection.CLOSE_REASON_PROTOCOL_ERROR = 1002;
160
- WebSocketConnection.CLOSE_REASON_UNPROCESSABLE_INPUT = 1003;
161
- WebSocketConnection.CLOSE_REASON_RESERVED = 1004; // Reserved value. Undefined meaning.
162
- WebSocketConnection.CLOSE_REASON_NOT_PROVIDED = 1005; // Not to be used on the wire
163
- WebSocketConnection.CLOSE_REASON_ABNORMAL = 1006; // Not to be used on the wire
164
- WebSocketConnection.CLOSE_REASON_INVALID_DATA = 1007;
165
- WebSocketConnection.CLOSE_REASON_POLICY_VIOLATION = 1008;
166
- WebSocketConnection.CLOSE_REASON_MESSAGE_TOO_BIG = 1009;
167
- WebSocketConnection.CLOSE_REASON_EXTENSION_REQUIRED = 1010;
168
- WebSocketConnection.CLOSE_REASON_INTERNAL_SERVER_ERROR = 1011;
169
- WebSocketConnection.CLOSE_REASON_TLS_HANDSHAKE_FAILED = 1015; // Not to be used on the wire
170
-
171
- WebSocketConnection.CLOSE_DESCRIPTIONS = {
172
- 1000: 'Normal connection closure',
173
- 1001: 'Remote peer is going away',
174
- 1002: 'Protocol error',
175
- 1003: 'Unprocessable input',
176
- 1004: 'Reserved',
177
- 1005: 'Reason not provided',
178
- 1006: 'Abnormal closure, no further detail available',
179
- 1007: 'Invalid data received',
180
- 1008: 'Policy violation',
181
- 1009: 'Message too big',
182
- 1010: 'Extension requested by client is required',
183
- 1011: 'Internal Server Error',
184
- 1015: 'TLS Handshake Failed'
185
- };
186
-
187
- function validateCloseReason(code) {
188
- if (code < 1000) {
189
- // Status codes in the range 0-999 are not used
190
- return false;
191
- }
192
- if (code >= 1000 && code <= 2999) {
193
- // Codes from 1000 - 2999 are reserved for use by the protocol. Only
194
- // a few codes are defined, all others are currently illegal.
195
- return [1000, 1001, 1002, 1003, 1007, 1008, 1009, 1010, 1011, 1012, 1013, 1014, 1015].indexOf(code) !== -1;
196
- }
197
- if (code >= 3000 && code <= 3999) {
198
- // Reserved for use by libraries, frameworks, and applications.
199
- // Should be registered with IANA. Interpretation of these codes is
200
- // undefined by the WebSocket protocol.
201
- return true;
202
- }
203
- if (code >= 4000 && code <= 4999) {
204
- // Reserved for private use. Interpretation of these codes is
205
- // undefined by the WebSocket protocol.
206
- return true;
207
- }
208
- if (code >= 5000) {
209
- return false;
210
- }
211
- }
212
-
213
- util.inherits(WebSocketConnection, EventEmitter);
214
-
215
- WebSocketConnection.prototype._addSocketEventListeners = function() {
216
- this.socket.on('error', this.handleSocketError.bind(this));
217
- this.socket.on('end', this.handleSocketEnd.bind(this));
218
- this.socket.on('close', this.handleSocketClose.bind(this));
219
- this.socket.on('drain', this.handleSocketDrain.bind(this));
220
- this.socket.on('pause', this.handleSocketPause.bind(this));
221
- this.socket.on('resume', this.handleSocketResume.bind(this));
222
- this.socket.on('data', this.handleSocketData.bind(this));
223
- };
224
-
225
- // set or reset the keepalive timer when data is received.
226
- WebSocketConnection.prototype.setKeepaliveTimer = function() {
227
- this._debug('setKeepaliveTimer');
228
- if (!this.config.keepalive || this.config.useNativeKeepalive) { return; }
229
- this.clearKeepaliveTimer();
230
- this.clearGracePeriodTimer();
231
- this._keepaliveTimeoutID = setTimeout(this._keepaliveTimerHandler, this.config.keepaliveInterval);
232
- };
233
-
234
- WebSocketConnection.prototype.clearKeepaliveTimer = function() {
235
- if (this._keepaliveTimeoutID) {
236
- clearTimeout(this._keepaliveTimeoutID);
237
- }
238
- };
239
-
240
- // No data has been received within config.keepaliveTimeout ms.
241
- WebSocketConnection.prototype.handleKeepaliveTimer = function() {
242
- this._debug('handleKeepaliveTimer');
243
- this._keepaliveTimeoutID = null;
244
- this.ping();
245
-
246
- // If we are configured to drop connections if the client doesn't respond
247
- // then set the grace period timer.
248
- if (this.config.dropConnectionOnKeepaliveTimeout) {
249
- this.setGracePeriodTimer();
250
- }
251
- else {
252
- // Otherwise reset the keepalive timer to send the next ping.
253
- this.setKeepaliveTimer();
254
- }
255
- };
256
-
257
- WebSocketConnection.prototype.setGracePeriodTimer = function() {
258
- this._debug('setGracePeriodTimer');
259
- this.clearGracePeriodTimer();
260
- this._gracePeriodTimeoutID = setTimeout(this._gracePeriodTimerHandler, this.config.keepaliveGracePeriod);
261
- };
262
-
263
- WebSocketConnection.prototype.clearGracePeriodTimer = function() {
264
- if (this._gracePeriodTimeoutID) {
265
- clearTimeout(this._gracePeriodTimeoutID);
266
- }
267
- };
268
-
269
- WebSocketConnection.prototype.handleGracePeriodTimer = function() {
270
- this._debug('handleGracePeriodTimer');
271
- // If this is called, the client has not responded and is assumed dead.
272
- this._gracePeriodTimeoutID = null;
273
- this.drop(WebSocketConnection.CLOSE_REASON_ABNORMAL, 'Peer not responding.', true);
274
- };
275
-
276
- WebSocketConnection.prototype.handleSocketData = function(data) {
277
- this._debug('handleSocketData');
278
- // Reset the keepalive timer when receiving data of any kind.
279
- this.setKeepaliveTimer();
280
-
281
- // Add received data to our bufferList, which efficiently holds received
282
- // data chunks in a linked list of Buffer objects.
283
- this.bufferList.write(data);
284
-
285
- this.processReceivedData();
286
- };
287
-
288
- WebSocketConnection.prototype.processReceivedData = function() {
289
- this._debug('processReceivedData');
290
- // If we're not connected, we should ignore any data remaining on the buffer.
291
- if (!this.connected) { return; }
292
-
293
- // Receiving/parsing is expected to be halted when paused.
294
- if (this.inputPaused) { return; }
295
-
296
- var frame = this.currentFrame;
297
-
298
- // WebSocketFrame.prototype.addData returns true if all data necessary to
299
- // parse the frame was available. It returns false if we are waiting for
300
- // more data to come in on the wire.
301
- if (!frame.addData(this.bufferList)) { this._debug('-- insufficient data for frame'); return; }
302
-
303
- var self = this;
304
-
305
- // Handle possible parsing errors
306
- if (frame.protocolError) {
307
- // Something bad happened.. get rid of this client.
308
- this._debug('-- protocol error');
309
- process.nextTick(function() {
310
- self.drop(WebSocketConnection.CLOSE_REASON_PROTOCOL_ERROR, frame.dropReason);
311
- });
312
- return;
313
- }
314
- else if (frame.frameTooLarge) {
315
- this._debug('-- frame too large');
316
- process.nextTick(function() {
317
- self.drop(WebSocketConnection.CLOSE_REASON_MESSAGE_TOO_BIG, frame.dropReason);
318
- });
319
- return;
320
- }
321
-
322
- // For now since we don't support extensions, all RSV bits are illegal
323
- if (frame.rsv1 || frame.rsv2 || frame.rsv3) {
324
- this._debug('-- illegal rsv flag');
325
- process.nextTick(function() {
326
- self.drop(WebSocketConnection.CLOSE_REASON_PROTOCOL_ERROR,
327
- 'Unsupported usage of rsv bits without negotiated extension.');
328
- });
329
- return;
330
- }
331
-
332
- if (!this.assembleFragments) {
333
- this._debug('-- emitting frame');
334
- process.nextTick(function() { self.emit('frame', frame); });
335
- }
336
-
337
- process.nextTick(function() { self.processFrame(frame); });
338
-
339
- this.currentFrame = new WebSocketFrame(this.maskBytes, this.frameHeader, this.config);
340
-
341
- // If there's data remaining, schedule additional processing, but yield
342
- // for now so that other connections have a chance to have their data
343
- // processed. We use setImmediate here instead of process.nextTick to
344
- // explicitly indicate that we wish for other I/O to be handled first.
345
- if (this.bufferList.length > 0) {
346
- setImmediateImpl(this.receivedDataHandler);
347
- }
348
- };
349
-
350
- WebSocketConnection.prototype.handleSocketError = function(error) {
351
- this._debug('handleSocketError: %j', error);
352
- if (this.state === STATE_CLOSED) {
353
- // See https://github.com/theturtle32/WebSocket-Node/issues/288
354
- this._debug(' --- Socket \'error\' after \'close\'');
355
- return;
356
- }
357
- this.closeReasonCode = WebSocketConnection.CLOSE_REASON_ABNORMAL;
358
- this.closeDescription = 'Socket Error: ' + error.syscall + ' ' + error.code;
359
- this.connected = false;
360
- this.state = STATE_CLOSED;
361
- this.fragmentationSize = 0;
362
- if (utils.eventEmitterListenerCount(this, 'error') > 0) {
363
- this.emit('error', error);
364
- }
365
- this.socket.destroy();
366
- this._debug.printOutput();
367
- };
368
-
369
- WebSocketConnection.prototype.handleSocketEnd = function() {
370
- this._debug('handleSocketEnd: received socket end. state = %s', this.state);
371
- this.receivedEnd = true;
372
- if (this.state === STATE_CLOSED) {
373
- // When using the TLS module, sometimes the socket will emit 'end'
374
- // after it emits 'close'. I don't think that's correct behavior,
375
- // but we should deal with it gracefully by ignoring it.
376
- this._debug(' --- Socket \'end\' after \'close\'');
377
- return;
378
- }
379
- if (this.state !== STATE_PEER_REQUESTED_CLOSE &&
380
- this.state !== STATE_ENDING) {
381
- this._debug(' --- UNEXPECTED socket end.');
382
- this.socket.end();
383
- }
384
- };
385
-
386
- WebSocketConnection.prototype.handleSocketClose = function(hadError) {
387
- this._debug('handleSocketClose: received socket close');
388
- this.socketHadError = hadError;
389
- this.connected = false;
390
- this.state = STATE_CLOSED;
391
- // If closeReasonCode is still set to -1 at this point then we must
392
- // not have received a close frame!!
393
- if (this.closeReasonCode === -1) {
394
- this.closeReasonCode = WebSocketConnection.CLOSE_REASON_ABNORMAL;
395
- this.closeDescription = 'Connection dropped by remote peer.';
396
- }
397
- this.clearCloseTimer();
398
- this.clearKeepaliveTimer();
399
- this.clearGracePeriodTimer();
400
- if (!this.closeEventEmitted) {
401
- this.closeEventEmitted = true;
402
- this._debug('-- Emitting WebSocketConnection close event');
403
- this.emit('close', this.closeReasonCode, this.closeDescription);
404
- }
405
- };
406
-
407
- WebSocketConnection.prototype.handleSocketDrain = function() {
408
- this._debug('handleSocketDrain: socket drain event');
409
- this.outputBufferFull = false;
410
- this.emit('drain');
411
- };
412
-
413
- WebSocketConnection.prototype.handleSocketPause = function() {
414
- this._debug('handleSocketPause: socket pause event');
415
- this.inputPaused = true;
416
- this.emit('pause');
417
- };
418
-
419
- WebSocketConnection.prototype.handleSocketResume = function() {
420
- this._debug('handleSocketResume: socket resume event');
421
- this.inputPaused = false;
422
- this.emit('resume');
423
- this.processReceivedData();
424
- };
425
-
426
- WebSocketConnection.prototype.pause = function() {
427
- this._debug('pause: pause requested');
428
- this.socket.pause();
429
- };
430
-
431
- WebSocketConnection.prototype.resume = function() {
432
- this._debug('resume: resume requested');
433
- this.socket.resume();
434
- };
435
-
436
- WebSocketConnection.prototype.close = function(reasonCode, description) {
437
- if (this.connected) {
438
- this._debug('close: Initating clean WebSocket close sequence.');
439
- if ('number' !== typeof reasonCode) {
440
- reasonCode = WebSocketConnection.CLOSE_REASON_NORMAL;
441
- }
442
- if (!validateCloseReason(reasonCode)) {
443
- throw new Error('Close code ' + reasonCode + ' is not valid.');
444
- }
445
- if ('string' !== typeof description) {
446
- description = WebSocketConnection.CLOSE_DESCRIPTIONS[reasonCode];
447
- }
448
- this.closeReasonCode = reasonCode;
449
- this.closeDescription = description;
450
- this.setCloseTimer();
451
- this.sendCloseFrame(this.closeReasonCode, this.closeDescription);
452
- this.state = STATE_ENDING;
453
- this.connected = false;
454
- }
455
- };
456
-
457
- WebSocketConnection.prototype.drop = function(reasonCode, description, skipCloseFrame) {
458
- this._debug('drop');
459
- if (typeof(reasonCode) !== 'number') {
460
- reasonCode = WebSocketConnection.CLOSE_REASON_PROTOCOL_ERROR;
461
- }
462
-
463
- if (typeof(description) !== 'string') {
464
- // If no description is provided, try to look one up based on the
465
- // specified reasonCode.
466
- description = WebSocketConnection.CLOSE_DESCRIPTIONS[reasonCode];
467
- }
468
-
469
- this._debug('Forcefully dropping connection. skipCloseFrame: %s, code: %d, description: %s',
470
- skipCloseFrame, reasonCode, description
471
- );
472
-
473
- this.closeReasonCode = reasonCode;
474
- this.closeDescription = description;
475
- this.frameQueue = [];
476
- this.fragmentationSize = 0;
477
- if (!skipCloseFrame) {
478
- this.sendCloseFrame(reasonCode, description);
479
- }
480
- this.connected = false;
481
- this.state = STATE_CLOSED;
482
- this.clearCloseTimer();
483
- this.clearKeepaliveTimer();
484
- this.clearGracePeriodTimer();
485
-
486
- if (!this.closeEventEmitted) {
487
- this.closeEventEmitted = true;
488
- this._debug('Emitting WebSocketConnection close event');
489
- this.emit('close', this.closeReasonCode, this.closeDescription);
490
- }
491
-
492
- this._debug('Drop: destroying socket');
493
- this.socket.destroy();
494
- };
495
-
496
- WebSocketConnection.prototype.setCloseTimer = function() {
497
- this._debug('setCloseTimer');
498
- this.clearCloseTimer();
499
- this._debug('Setting close timer');
500
- this.waitingForCloseResponse = true;
501
- this.closeTimer = setTimeout(this._closeTimerHandler, this.closeTimeout);
502
- };
503
-
504
- WebSocketConnection.prototype.clearCloseTimer = function() {
505
- this._debug('clearCloseTimer');
506
- if (this.closeTimer) {
507
- this._debug('Clearing close timer');
508
- clearTimeout(this.closeTimer);
509
- this.waitingForCloseResponse = false;
510
- this.closeTimer = null;
511
- }
512
- };
513
-
514
- WebSocketConnection.prototype.handleCloseTimer = function() {
515
- this._debug('handleCloseTimer');
516
- this.closeTimer = null;
517
- if (this.waitingForCloseResponse) {
518
- this._debug('Close response not received from client. Forcing socket end.');
519
- this.waitingForCloseResponse = false;
520
- this.state = STATE_CLOSED;
521
- this.socket.end();
522
- }
523
- };
524
-
525
- WebSocketConnection.prototype.processFrame = function(frame) {
526
- this._debug('processFrame');
527
- this._debug(' -- frame: %s', frame);
528
-
529
- // Any non-control opcode besides 0x00 (continuation) received in the
530
- // middle of a fragmented message is illegal.
531
- if (this.frameQueue.length !== 0 && (frame.opcode > 0x00 && frame.opcode < 0x08)) {
532
- this.drop(WebSocketConnection.CLOSE_REASON_PROTOCOL_ERROR,
533
- 'Illegal frame opcode 0x' + frame.opcode.toString(16) + ' ' +
534
- 'received in middle of fragmented message.');
535
- return;
536
- }
537
-
538
- switch(frame.opcode) {
539
- case 0x02: // WebSocketFrame.BINARY_FRAME
540
- this._debug('-- Binary Frame');
541
- if (this.assembleFragments) {
542
- if (frame.fin) {
543
- // Complete single-frame message received
544
- this._debug('---- Emitting \'message\' event');
545
- this.emit('message', {
546
- type: 'binary',
547
- binaryData: frame.binaryPayload
548
- });
549
- }
550
- else {
551
- // beginning of a fragmented message
552
- this.frameQueue.push(frame);
553
- this.fragmentationSize = frame.length;
554
- }
555
- }
556
- break;
557
- case 0x01: // WebSocketFrame.TEXT_FRAME
558
- this._debug('-- Text Frame');
559
- if (this.assembleFragments) {
560
- if (frame.fin) {
561
- if (!isValidUTF8(frame.binaryPayload)) {
562
- this.drop(WebSocketConnection.CLOSE_REASON_INVALID_DATA,
563
- 'Invalid UTF-8 Data Received');
564
- return;
565
- }
566
- // Complete single-frame message received
567
- this._debug('---- Emitting \'message\' event');
568
- this.emit('message', {
569
- type: 'utf8',
570
- utf8Data: frame.binaryPayload.toString('utf8')
571
- });
572
- }
573
- else {
574
- // beginning of a fragmented message
575
- this.frameQueue.push(frame);
576
- this.fragmentationSize = frame.length;
577
- }
578
- }
579
- break;
580
- case 0x00: // WebSocketFrame.CONTINUATION
581
- this._debug('-- Continuation Frame');
582
- if (this.assembleFragments) {
583
- if (this.frameQueue.length === 0) {
584
- this.drop(WebSocketConnection.CLOSE_REASON_PROTOCOL_ERROR,
585
- 'Unexpected Continuation Frame');
586
- return;
587
- }
588
-
589
- this.fragmentationSize += frame.length;
590
-
591
- if (this.fragmentationSize > this.maxReceivedMessageSize) {
592
- this.drop(WebSocketConnection.CLOSE_REASON_MESSAGE_TOO_BIG,
593
- 'Maximum message size exceeded.');
594
- return;
595
- }
596
-
597
- this.frameQueue.push(frame);
598
-
599
- if (frame.fin) {
600
- // end of fragmented message, so we process the whole
601
- // message now. We also have to decode the utf-8 data
602
- // for text frames after combining all the fragments.
603
- var bytesCopied = 0;
604
- var binaryPayload = bufferAllocUnsafe(this.fragmentationSize);
605
- var opcode = this.frameQueue[0].opcode;
606
- this.frameQueue.forEach(function (currentFrame) {
607
- currentFrame.binaryPayload.copy(binaryPayload, bytesCopied);
608
- bytesCopied += currentFrame.binaryPayload.length;
609
- });
610
- this.frameQueue = [];
611
- this.fragmentationSize = 0;
612
-
613
- switch (opcode) {
614
- case 0x02: // WebSocketOpcode.BINARY_FRAME
615
- this.emit('message', {
616
- type: 'binary',
617
- binaryData: binaryPayload
618
- });
619
- break;
620
- case 0x01: // WebSocketOpcode.TEXT_FRAME
621
- if (!isValidUTF8(binaryPayload)) {
622
- this.drop(WebSocketConnection.CLOSE_REASON_INVALID_DATA,
623
- 'Invalid UTF-8 Data Received');
624
- return;
625
- }
626
- this.emit('message', {
627
- type: 'utf8',
628
- utf8Data: binaryPayload.toString('utf8')
629
- });
630
- break;
631
- default:
632
- this.drop(WebSocketConnection.CLOSE_REASON_PROTOCOL_ERROR,
633
- 'Unexpected first opcode in fragmentation sequence: 0x' + opcode.toString(16));
634
- return;
635
- }
636
- }
637
- }
638
- break;
639
- case 0x09: // WebSocketFrame.PING
640
- this._debug('-- Ping Frame');
641
-
642
- if (this._pingListenerCount > 0) {
643
- // logic to emit the ping frame: this is only done when a listener is known to exist
644
- // Expose a function allowing the user to override the default ping() behavior
645
- var cancelled = false;
646
- var cancel = function() {
647
- cancelled = true;
648
- };
649
- this.emit('ping', cancel, frame.binaryPayload);
650
-
651
- // Only send a pong if the client did not indicate that he would like to cancel
652
- if (!cancelled) {
653
- this.pong(frame.binaryPayload);
654
- }
655
- }
656
- else {
657
- this.pong(frame.binaryPayload);
658
- }
659
-
660
- break;
661
- case 0x0A: // WebSocketFrame.PONG
662
- this._debug('-- Pong Frame');
663
- this.emit('pong', frame.binaryPayload);
664
- break;
665
- case 0x08: // WebSocketFrame.CONNECTION_CLOSE
666
- this._debug('-- Close Frame');
667
- if (this.waitingForCloseResponse) {
668
- // Got response to our request to close the connection.
669
- // Close is complete, so we just hang up.
670
- this._debug('---- Got close response from peer. Completing closing handshake.');
671
- this.clearCloseTimer();
672
- this.waitingForCloseResponse = false;
673
- this.state = STATE_CLOSED;
674
- this.socket.end();
675
- return;
676
- }
677
-
678
- this._debug('---- Closing handshake initiated by peer.');
679
- // Got request from other party to close connection.
680
- // Send back acknowledgement and then hang up.
681
- this.state = STATE_PEER_REQUESTED_CLOSE;
682
- var respondCloseReasonCode;
683
-
684
- // Make sure the close reason provided is legal according to
685
- // the protocol spec. Providing no close status is legal.
686
- // WebSocketFrame sets closeStatus to -1 by default, so if it
687
- // is still -1, then no status was provided.
688
- if (frame.invalidCloseFrameLength) {
689
- this.closeReasonCode = 1005; // 1005 = No reason provided.
690
- respondCloseReasonCode = WebSocketConnection.CLOSE_REASON_PROTOCOL_ERROR;
691
- }
692
- else if (frame.closeStatus === -1 || validateCloseReason(frame.closeStatus)) {
693
- this.closeReasonCode = frame.closeStatus;
694
- respondCloseReasonCode = WebSocketConnection.CLOSE_REASON_NORMAL;
695
- }
696
- else {
697
- this.closeReasonCode = frame.closeStatus;
698
- respondCloseReasonCode = WebSocketConnection.CLOSE_REASON_PROTOCOL_ERROR;
699
- }
700
-
701
- // If there is a textual description in the close frame, extract it.
702
- if (frame.binaryPayload.length > 1) {
703
- if (!isValidUTF8(frame.binaryPayload)) {
704
- this.drop(WebSocketConnection.CLOSE_REASON_INVALID_DATA,
705
- 'Invalid UTF-8 Data Received');
706
- return;
707
- }
708
- this.closeDescription = frame.binaryPayload.toString('utf8');
709
- }
710
- else {
711
- this.closeDescription = WebSocketConnection.CLOSE_DESCRIPTIONS[this.closeReasonCode];
712
- }
713
- this._debug(
714
- '------ Remote peer %s - code: %d - %s - close frame payload length: %d',
715
- this.remoteAddress, this.closeReasonCode,
716
- this.closeDescription, frame.length
717
- );
718
- this._debug('------ responding to remote peer\'s close request.');
719
- this.sendCloseFrame(respondCloseReasonCode, null);
720
- this.connected = false;
721
- break;
722
- default:
723
- this._debug('-- Unrecognized Opcode %d', frame.opcode);
724
- this.drop(WebSocketConnection.CLOSE_REASON_PROTOCOL_ERROR,
725
- 'Unrecognized Opcode: 0x' + frame.opcode.toString(16));
726
- break;
727
- }
728
- };
729
-
730
- WebSocketConnection.prototype.send = function(data, cb) {
731
- this._debug('send');
732
- if (Buffer.isBuffer(data)) {
733
- this.sendBytes(data, cb);
734
- }
735
- else if (typeof(data['toString']) === 'function') {
736
- this.sendUTF(data, cb);
737
- }
738
- else {
739
- throw new Error('Data provided must either be a Node Buffer or implement toString()');
740
- }
741
- };
742
-
743
- WebSocketConnection.prototype.sendUTF = function(data, cb) {
744
- data = bufferFromString(data.toString(), 'utf8');
745
- this._debug('sendUTF: %d bytes', data.length);
746
- var frame = new WebSocketFrame(this.maskBytes, this.frameHeader, this.config);
747
- frame.opcode = 0x01; // WebSocketOpcode.TEXT_FRAME
748
- frame.binaryPayload = data;
749
- this.fragmentAndSend(frame, cb);
750
- };
751
-
752
- WebSocketConnection.prototype.sendBytes = function(data, cb) {
753
- this._debug('sendBytes');
754
- if (!Buffer.isBuffer(data)) {
755
- throw new Error('You must pass a Node Buffer object to WebSocketConnection.prototype.sendBytes()');
756
- }
757
- var frame = new WebSocketFrame(this.maskBytes, this.frameHeader, this.config);
758
- frame.opcode = 0x02; // WebSocketOpcode.BINARY_FRAME
759
- frame.binaryPayload = data;
760
- this.fragmentAndSend(frame, cb);
761
- };
762
-
763
- WebSocketConnection.prototype.ping = function(data) {
764
- this._debug('ping');
765
- var frame = new WebSocketFrame(this.maskBytes, this.frameHeader, this.config);
766
- frame.opcode = 0x09; // WebSocketOpcode.PING
767
- frame.fin = true;
768
- if (data) {
769
- if (!Buffer.isBuffer(data)) {
770
- data = bufferFromString(data.toString(), 'utf8');
771
- }
772
- if (data.length > 125) {
773
- this._debug('WebSocket: Data for ping is longer than 125 bytes. Truncating.');
774
- data = data.slice(0,124);
775
- }
776
- frame.binaryPayload = data;
777
- }
778
- this.sendFrame(frame);
779
- };
780
-
781
- // Pong frames have to echo back the contents of the data portion of the
782
- // ping frame exactly, byte for byte.
783
- WebSocketConnection.prototype.pong = function(binaryPayload) {
784
- this._debug('pong');
785
- var frame = new WebSocketFrame(this.maskBytes, this.frameHeader, this.config);
786
- frame.opcode = 0x0A; // WebSocketOpcode.PONG
787
- if (Buffer.isBuffer(binaryPayload) && binaryPayload.length > 125) {
788
- this._debug('WebSocket: Data for pong is longer than 125 bytes. Truncating.');
789
- binaryPayload = binaryPayload.slice(0,124);
790
- }
791
- frame.binaryPayload = binaryPayload;
792
- frame.fin = true;
793
- this.sendFrame(frame);
794
- };
795
-
796
- WebSocketConnection.prototype.fragmentAndSend = function(frame, cb) {
797
- this._debug('fragmentAndSend');
798
- if (frame.opcode > 0x07) {
799
- throw new Error('You cannot fragment control frames.');
800
- }
801
-
802
- var threshold = this.config.fragmentationThreshold;
803
- var length = frame.binaryPayload.length;
804
-
805
- // Send immediately if fragmentation is disabled or the message is not
806
- // larger than the fragmentation threshold.
807
- if (!this.config.fragmentOutgoingMessages || (frame.binaryPayload && length <= threshold)) {
808
- frame.fin = true;
809
- this.sendFrame(frame, cb);
810
- return;
811
- }
812
-
813
- var numFragments = Math.ceil(length / threshold);
814
- var sentFragments = 0;
815
- var sentCallback = function fragmentSentCallback(err) {
816
- if (err) {
817
- if (typeof cb === 'function') {
818
- // pass only the first error
819
- cb(err);
820
- cb = null;
821
- }
822
- return;
823
- }
824
- ++sentFragments;
825
- if ((sentFragments === numFragments) && (typeof cb === 'function')) {
826
- cb();
827
- }
828
- };
829
- for (var i=1; i <= numFragments; i++) {
830
- var currentFrame = new WebSocketFrame(this.maskBytes, this.frameHeader, this.config);
831
-
832
- // continuation opcode except for first frame.
833
- currentFrame.opcode = (i === 1) ? frame.opcode : 0x00;
834
-
835
- // fin set on last frame only
836
- currentFrame.fin = (i === numFragments);
837
-
838
- // length is likely to be shorter on the last fragment
839
- var currentLength = (i === numFragments) ? length - (threshold * (i-1)) : threshold;
840
- var sliceStart = threshold * (i-1);
841
-
842
- // Slice the right portion of the original payload
843
- currentFrame.binaryPayload = frame.binaryPayload.slice(sliceStart, sliceStart + currentLength);
844
-
845
- this.sendFrame(currentFrame, sentCallback);
846
- }
847
- };
848
-
849
- WebSocketConnection.prototype.sendCloseFrame = function(reasonCode, description, cb) {
850
- if (typeof(reasonCode) !== 'number') {
851
- reasonCode = WebSocketConnection.CLOSE_REASON_NORMAL;
852
- }
853
-
854
- this._debug('sendCloseFrame state: %s, reasonCode: %d, description: %s', this.state, reasonCode, description);
855
-
856
- if (this.state !== STATE_OPEN && this.state !== STATE_PEER_REQUESTED_CLOSE) { return; }
857
-
858
- var frame = new WebSocketFrame(this.maskBytes, this.frameHeader, this.config);
859
- frame.fin = true;
860
- frame.opcode = 0x08; // WebSocketOpcode.CONNECTION_CLOSE
861
- frame.closeStatus = reasonCode;
862
- if (typeof(description) === 'string') {
863
- frame.binaryPayload = bufferFromString(description, 'utf8');
864
- }
865
-
866
- this.sendFrame(frame, cb);
867
- this.socket.end();
868
- };
869
-
870
- WebSocketConnection.prototype.sendFrame = function(frame, cb) {
871
- this._debug('sendFrame');
872
- frame.mask = this.maskOutgoingPackets;
873
- var flushed = this.socket.write(frame.toBuffer(), cb);
874
- this.outputBufferFull = !flushed;
875
- return flushed;
876
- };
877
-
878
- WebSocketConnection_1 = WebSocketConnection;
879
-
880
-
881
-
882
- function instrumentSocketForDebugging(connection, socket) {
883
- /* jshint loopfunc: true */
884
- if (!connection._debug.enabled) { return; }
885
-
886
- var originalSocketEmit = socket.emit;
887
- socket.emit = function(event) {
888
- connection._debug('||| Socket Event \'%s\'', event);
889
- originalSocketEmit.apply(this, arguments);
890
- };
891
-
892
- for (var key in socket) {
893
- if ('function' !== typeof(socket[key])) { continue; }
894
- if (['emit'].indexOf(key) !== -1) { continue; }
895
- (function(key) {
896
- var original = socket[key];
897
- if (key === 'on') {
898
- socket[key] = function proxyMethod__EventEmitter__On() {
899
- connection._debug('||| Socket method called: %s (%s)', key, arguments[0]);
900
- return original.apply(this, arguments);
901
- };
902
- return;
903
- }
904
- socket[key] = function proxyMethod() {
905
- connection._debug('||| Socket method called: %s', key);
906
- return original.apply(this, arguments);
907
- };
908
- })(key);
909
- }
910
- }
911
- return WebSocketConnection_1;
912
- }
913
-
914
- export { requireWebSocketConnection as __require };