@webvpn/dgram 0.0.1 → 0.2.0
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.
- package/build/index.cjs +2235 -2327
- package/build/index.cjs.map +1 -1
- package/build/index.d.ts +5 -184
- package/build/index.js +10094 -2664
- package/build/index.js.map +1 -1
- package/package.json +4 -6
- package/src/index.ts +5 -698
- package/vite-plugin-node-stdlib-browser.cjs +61 -61
- package/vite.config.ts +29 -29
- package/build/index.mjs +0 -3252
- package/build/index.mjs.map +0 -1
- package/build/test.d.ts +0 -1
- package/build/utils.d.ts +0 -13
- package/index.html +0 -11
- package/src/chrome-dgram.js +0 -503
- package/src/flatbuffers/client-packet-type.ts +0 -40
- package/src/flatbuffers/client-packet.ts +0 -162
- package/src/flatbuffers/client-tcpserver-close.ts +0 -93
- package/src/flatbuffers/client-tcpserver-get-connections.ts +0 -93
- package/src/flatbuffers/client-tcpserver-listen.ts +0 -211
- package/src/flatbuffers/client-tcpserver.ts +0 -159
- package/src/flatbuffers/client-tcpsocket-connect.ts +0 -253
- package/src/flatbuffers/client-tcpsocket-destroy-soon.ts +0 -75
- package/src/flatbuffers/client-tcpsocket-destroy.ts +0 -93
- package/src/flatbuffers/client-tcpsocket-end.ts +0 -93
- package/src/flatbuffers/client-tcpsocket-pause.ts +0 -75
- package/src/flatbuffers/client-tcpsocket-reset-and-destroy.ts +0 -75
- package/src/flatbuffers/client-tcpsocket-resume.ts +0 -75
- package/src/flatbuffers/client-tcpsocket-set-encoding.ts +0 -93
- package/src/flatbuffers/client-tcpsocket-set-keep-alive.ts +0 -103
- package/src/flatbuffers/client-tcpsocket-set-no-delay.ts +0 -89
- package/src/flatbuffers/client-tcpsocket-set-timeout.ts +0 -107
- package/src/flatbuffers/client-tcpsocket-write.ts +0 -147
- package/src/flatbuffers/client-tcpsocket.ts +0 -89
- package/src/flatbuffers/client-udpsocket-add-membership.ts +0 -110
- package/src/flatbuffers/client-udpsocket-add-source-specific-membership.ts +0 -127
- package/src/flatbuffers/client-udpsocket-bind.ts +0 -138
- package/src/flatbuffers/client-udpsocket-close.ts +0 -93
- package/src/flatbuffers/client-udpsocket-disconnect.ts +0 -75
- package/src/flatbuffers/client-udpsocket-drop-membership.ts +0 -110
- package/src/flatbuffers/client-udpsocket-drop-source-specific-membership.ts +0 -127
- package/src/flatbuffers/client-udpsocket-send.ts +0 -189
- package/src/flatbuffers/client-udpsocket-set-broadcast.ts +0 -89
- package/src/flatbuffers/client-udpsocket-set-multicast-interface.ts +0 -93
- package/src/flatbuffers/client-udpsocket-set-multicast-loopback.ts +0 -89
- package/src/flatbuffers/client-udpsocket-set-multicast-ttl.ts +0 -89
- package/src/flatbuffers/client-udpsocket-set-recv-buffer-size.ts +0 -89
- package/src/flatbuffers/client-udpsocket-set-send-buffer-size.ts +0 -89
- package/src/flatbuffers/client-udpsocket-set-ttl.ts +0 -89
- package/src/flatbuffers/client-udpsocket.ts +0 -149
- package/src/flatbuffers/packet-handshake-type.ts +0 -9
- package/src/flatbuffers/packet.ts +0 -210
- package/src/flatbuffers/schema.ts +0 -75
- package/src/flatbuffers/server-packet-type.ts +0 -38
- package/src/flatbuffers/server-packet.ts +0 -158
- package/src/flatbuffers/server-tcpserver-callback-close.ts +0 -93
- package/src/flatbuffers/server-tcpserver-callback-get-connections.ts +0 -124
- package/src/flatbuffers/server-tcpserver-callback-listen.ts +0 -141
- package/src/flatbuffers/server-tcpserver-event-close.ts +0 -75
- package/src/flatbuffers/server-tcpserver-event-drop.ts +0 -75
- package/src/flatbuffers/server-tcpserver-event-error.ts +0 -75
- package/src/flatbuffers/server-tcpserver-event-listening.ts +0 -124
- package/src/flatbuffers/server-tcpsocket-callback-connect.ts +0 -189
- package/src/flatbuffers/server-tcpsocket-callback-end.ts +0 -93
- package/src/flatbuffers/server-tcpsocket-callback-set-timeout.ts +0 -93
- package/src/flatbuffers/server-tcpsocket-callback-write.ts +0 -93
- package/src/flatbuffers/server-tcpsocket-event-close.ts +0 -89
- package/src/flatbuffers/server-tcpsocket-event-connect.ts +0 -172
- package/src/flatbuffers/server-tcpsocket-event-connection-attempt-failed.ts +0 -138
- package/src/flatbuffers/server-tcpsocket-event-connection-attempt-timeout.ts +0 -121
- package/src/flatbuffers/server-tcpsocket-event-connection-attempt.ts +0 -121
- package/src/flatbuffers/server-tcpsocket-event-data.ts +0 -113
- package/src/flatbuffers/server-tcpsocket-event-drain.ts +0 -75
- package/src/flatbuffers/server-tcpsocket-event-end.ts +0 -75
- package/src/flatbuffers/server-tcpsocket-event-error.ts +0 -93
- package/src/flatbuffers/server-tcpsocket-event-lookup.ts +0 -141
- package/src/flatbuffers/server-tcpsocket-event-ready.ts +0 -75
- package/src/flatbuffers/server-tcpsocket-event-timeout.ts +0 -75
- package/src/flatbuffers/server-udpsocket-callback-bind.ts +0 -141
- package/src/flatbuffers/server-udpsocket-callback-close.ts +0 -93
- package/src/flatbuffers/server-udpsocket-callback-connect.ts +0 -141
- package/src/flatbuffers/server-udpsocket-callback-send.ts +0 -93
- package/src/flatbuffers/server-udpsocket-event-close.ts +0 -75
- package/src/flatbuffers/server-udpsocket-event-connect.ts +0 -75
- package/src/flatbuffers/server-udpsocket-event-error.ts +0 -93
- package/src/flatbuffers/server-udpsocket-event-listening.ts +0 -124
- package/src/flatbuffers/server-udpsocket-event-message.ts +0 -175
- package/src/net.js +0 -2442
- package/src/test.ts +0 -34
- package/src/types.ts +0 -16
- package/src/utils.ts +0 -45
package/build/utils.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { SocketClientPacket, WebVPNHandshakePacket } from '.';
|
|
2
|
-
|
|
3
|
-
export declare const packPacket: (packet: WebVPNHandshakePacket | SocketClientPacket) => any;
|
|
4
|
-
export declare const unpackPacket: (data: Uint8Array) => SocketServerPacket;
|
|
5
|
-
export declare const unpackMultiplePackets: (data: Uint8Array) => SocketServerPacket[];
|
|
6
|
-
export interface WebTransportOptions {
|
|
7
|
-
address: string;
|
|
8
|
-
serverCertificateHashes: {
|
|
9
|
-
algorithm: string;
|
|
10
|
-
value: number[];
|
|
11
|
-
}[];
|
|
12
|
-
}
|
|
13
|
-
export declare const makeWebVPNWebtransport: () => Promise<any>;
|
package/index.html
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
<!doctype html>
|
|
2
|
-
<html lang="en">
|
|
3
|
-
<head>
|
|
4
|
-
<meta charset="UTF-8" />
|
|
5
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
-
<title>WebVPN test page</title>
|
|
7
|
-
</head>
|
|
8
|
-
<body style="background-color:#262626;">
|
|
9
|
-
<script type="module" src="/src/test.ts"></script>
|
|
10
|
-
</body>
|
|
11
|
-
</html>
|
package/src/chrome-dgram.js
DELETED
|
@@ -1,503 +0,0 @@
|
|
|
1
|
-
/*! chrome-dgram. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
|
|
2
|
-
/* global chrome */
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* UDP / Datagram Sockets
|
|
6
|
-
* ======================
|
|
7
|
-
*
|
|
8
|
-
* Datagram sockets are available through require('chrome-dgram').
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
exports.Socket = Socket
|
|
12
|
-
|
|
13
|
-
const EventEmitter = require('events').EventEmitter
|
|
14
|
-
const inherits = require('inherits')
|
|
15
|
-
const series = require('run-series')
|
|
16
|
-
|
|
17
|
-
const BIND_STATE_UNBOUND = 0
|
|
18
|
-
const BIND_STATE_BINDING = 1
|
|
19
|
-
const BIND_STATE_BOUND = 2
|
|
20
|
-
|
|
21
|
-
// Track open sockets to route incoming data (via onReceive) to the right handlers.
|
|
22
|
-
const sockets = {}
|
|
23
|
-
|
|
24
|
-
// Thorough check for Chrome App since both Edge and Chrome implement dummy chrome object
|
|
25
|
-
if (
|
|
26
|
-
typeof chrome === 'object' &&
|
|
27
|
-
typeof chrome.runtime === 'object' &&
|
|
28
|
-
typeof chrome.runtime.id === 'string' &&
|
|
29
|
-
typeof chrome.sockets === 'object' &&
|
|
30
|
-
typeof chrome.sockets.udp === 'object'
|
|
31
|
-
) {
|
|
32
|
-
chrome.sockets.udp.onReceive.addListener(onReceive)
|
|
33
|
-
chrome.sockets.udp.onReceiveError.addListener(onReceiveError)
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function onReceive (info) {
|
|
37
|
-
if (info.socketId in sockets) {
|
|
38
|
-
sockets[info.socketId]._onReceive(info)
|
|
39
|
-
} else {
|
|
40
|
-
console.error('Unknown socket id: ' + info.socketId)
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function onReceiveError (info) {
|
|
45
|
-
if (info.socketId in sockets) {
|
|
46
|
-
sockets[info.socketId]._onReceiveError(info.resultCode)
|
|
47
|
-
} else {
|
|
48
|
-
console.error('Unknown socket id: ' + info.socketId)
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* dgram.createSocket(type, [callback])
|
|
54
|
-
*
|
|
55
|
-
* Creates a datagram Socket of the specified types. Valid types are `udp4`
|
|
56
|
-
* and `udp6`.
|
|
57
|
-
*
|
|
58
|
-
* Takes an optional callback which is added as a listener for message events.
|
|
59
|
-
*
|
|
60
|
-
* Call socket.bind if you want to receive datagrams. socket.bind() will bind
|
|
61
|
-
* to the "all interfaces" address on a random port (it does the right thing
|
|
62
|
-
* for both udp4 and udp6 sockets). You can then retrieve the address and port
|
|
63
|
-
* with socket.address().address and socket.address().port.
|
|
64
|
-
*
|
|
65
|
-
* @param {string} type Either 'udp4' or 'udp6'
|
|
66
|
-
* @param {function} listener Attached as a listener to message events.
|
|
67
|
-
* Optional
|
|
68
|
-
* @return {Socket} Socket object
|
|
69
|
-
*/
|
|
70
|
-
exports.createSocket = function (type, listener) {
|
|
71
|
-
return new Socket(type, listener)
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
inherits(Socket, EventEmitter)
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Class: dgram.Socket
|
|
78
|
-
*
|
|
79
|
-
* The dgram Socket class encapsulates the datagram functionality. It should
|
|
80
|
-
* be created via `dgram.createSocket(type, [callback])`.
|
|
81
|
-
*
|
|
82
|
-
* Event: 'message'
|
|
83
|
-
* - msg Buffer object. The message
|
|
84
|
-
* - rinfo Object. Remote address information
|
|
85
|
-
* Emitted when a new datagram is available on a socket. msg is a Buffer and
|
|
86
|
-
* rinfo is an object with the sender's address information and the number
|
|
87
|
-
* of bytes in the datagram.
|
|
88
|
-
*
|
|
89
|
-
* Event: 'listening'
|
|
90
|
-
* Emitted when a socket starts listening for datagrams. This happens as soon
|
|
91
|
-
* as UDP sockets are created.
|
|
92
|
-
*
|
|
93
|
-
* Event: 'close'
|
|
94
|
-
* Emitted when a socket is closed with close(). No new message events will
|
|
95
|
-
* be emitted on this socket.
|
|
96
|
-
*
|
|
97
|
-
* Event: 'error'
|
|
98
|
-
* - exception Error object
|
|
99
|
-
* Emitted when an error occurs.
|
|
100
|
-
*/
|
|
101
|
-
function Socket (options, listener) {
|
|
102
|
-
const self = this
|
|
103
|
-
EventEmitter.call(self)
|
|
104
|
-
if (typeof options === 'string') options = { type: options }
|
|
105
|
-
if (options.type !== 'udp4') throw new Error('Bad socket type specified. Valid types are: udp4')
|
|
106
|
-
|
|
107
|
-
if (typeof listener === 'function') self.on('message', listener)
|
|
108
|
-
|
|
109
|
-
self._destroyed = false
|
|
110
|
-
self._bindState = BIND_STATE_UNBOUND
|
|
111
|
-
self._bindTasks = []
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* socket.bind(port, [address], [callback])
|
|
116
|
-
*
|
|
117
|
-
* For UDP sockets, listen for datagrams on a named port and optional address.
|
|
118
|
-
* If address is not specified, the OS will try to listen on all addresses.
|
|
119
|
-
* After binding is done, a "listening" event is emitted and the callback(if
|
|
120
|
-
* specified) is called. Specifying both a "listening" event listener and
|
|
121
|
-
* callback is not harmful but not very useful.
|
|
122
|
-
*
|
|
123
|
-
* A bound datagram socket keeps the node process running to receive
|
|
124
|
-
* datagrams.
|
|
125
|
-
*
|
|
126
|
-
* If binding fails, an "error" event is generated. In rare case (e.g. binding
|
|
127
|
-
* a closed socket), an Error may be thrown by this method.
|
|
128
|
-
*
|
|
129
|
-
* @param {number} port
|
|
130
|
-
* @param {string} address Optional
|
|
131
|
-
* @param {function} callback Function with no parameters, Optional. Callback
|
|
132
|
-
* when binding is done.
|
|
133
|
-
*/
|
|
134
|
-
Socket.prototype.bind = function (port, address, callback) {
|
|
135
|
-
const self = this
|
|
136
|
-
if (typeof address === 'function') {
|
|
137
|
-
callback = address
|
|
138
|
-
address = undefined
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
if (!address) address = '0.0.0.0'
|
|
142
|
-
|
|
143
|
-
if (!port) port = 0
|
|
144
|
-
|
|
145
|
-
if (self._bindState !== BIND_STATE_UNBOUND) throw new Error('Socket is already bound')
|
|
146
|
-
|
|
147
|
-
self._bindState = BIND_STATE_BINDING
|
|
148
|
-
|
|
149
|
-
if (typeof callback === 'function') self.once('listening', callback)
|
|
150
|
-
|
|
151
|
-
chrome.sockets.udp.create(function (createInfo) {
|
|
152
|
-
self.id = createInfo.socketId
|
|
153
|
-
|
|
154
|
-
sockets[self.id] = self
|
|
155
|
-
|
|
156
|
-
const bindFns = self._bindTasks.map(function (t) { return t.fn })
|
|
157
|
-
|
|
158
|
-
series(bindFns, function (err) {
|
|
159
|
-
if (err) return self.emit('error', err)
|
|
160
|
-
chrome.sockets.udp.bind(self.id, address, port, function (result) {
|
|
161
|
-
if (result < 0) {
|
|
162
|
-
self.emit('error', new Error('Socket ' + self.id + ' failed to bind. ' +
|
|
163
|
-
chrome.runtime.lastError.message))
|
|
164
|
-
return
|
|
165
|
-
}
|
|
166
|
-
chrome.sockets.udp.getInfo(self.id, function (socketInfo) {
|
|
167
|
-
if (!socketInfo.localPort || !socketInfo.localAddress) {
|
|
168
|
-
self.emit('error', new Error('Cannot get local port/address for Socket ' + self.id))
|
|
169
|
-
return
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
self._port = socketInfo.localPort
|
|
173
|
-
self._address = socketInfo.localAddress
|
|
174
|
-
|
|
175
|
-
self._bindState = BIND_STATE_BOUND
|
|
176
|
-
self.emit('listening')
|
|
177
|
-
|
|
178
|
-
self._bindTasks.forEach(function (t) {
|
|
179
|
-
t.callback()
|
|
180
|
-
})
|
|
181
|
-
})
|
|
182
|
-
})
|
|
183
|
-
})
|
|
184
|
-
})
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Internal function to receive new messages and emit `message` events.
|
|
189
|
-
*/
|
|
190
|
-
Socket.prototype._onReceive = function (info) {
|
|
191
|
-
const self = this
|
|
192
|
-
|
|
193
|
-
const buf = Buffer.from(new Uint8Array(info.data))
|
|
194
|
-
const rinfo = {
|
|
195
|
-
address: info.remoteAddress,
|
|
196
|
-
family: 'IPv4',
|
|
197
|
-
port: info.remotePort,
|
|
198
|
-
size: buf.length
|
|
199
|
-
}
|
|
200
|
-
self.emit('message', buf, rinfo)
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
Socket.prototype._onReceiveError = function (resultCode) {
|
|
204
|
-
const self = this
|
|
205
|
-
self.emit('error', new Error('Socket ' + self.id + ' receive error ' + resultCode))
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* socket.send(buf, offset, length, port, address, [callback])
|
|
210
|
-
*
|
|
211
|
-
* For UDP sockets, the destination port and IP address must be
|
|
212
|
-
* specified. A string may be supplied for the address parameter, and it will
|
|
213
|
-
* be resolved with DNS. An optional callback may be specified to detect any
|
|
214
|
-
* DNS errors and when buf may be re-used. Note that DNS lookups will delay
|
|
215
|
-
* the time that a send takes place, at least until the next tick. The only
|
|
216
|
-
* way to know for sure that a send has taken place is to use the callback.
|
|
217
|
-
*
|
|
218
|
-
* If the socket has not been previously bound with a call to bind, it's
|
|
219
|
-
* assigned a random port number and bound to the "all interfaces" address
|
|
220
|
-
* (0.0.0.0 for udp4 sockets, ::0 for udp6 sockets).
|
|
221
|
-
*
|
|
222
|
-
* @param {Buffer|Arrayish|string} buf Message to be sent
|
|
223
|
-
* @param {number} offset Offset in the buffer where the message starts. Optional.
|
|
224
|
-
* @param {number} length Number of bytes in the message. Optional.
|
|
225
|
-
* @param {number} port destination port
|
|
226
|
-
* @param {string} address destination IP
|
|
227
|
-
* @param {function} callback Callback when message is done being delivered.
|
|
228
|
-
* Optional.
|
|
229
|
-
*
|
|
230
|
-
* Valid combinations:
|
|
231
|
-
* send(buffer, offset, length, port, address, callback)
|
|
232
|
-
* send(buffer, offset, length, port, address)
|
|
233
|
-
* send(buffer, offset, length, port)
|
|
234
|
-
* send(bufferOrList, port, address, callback)
|
|
235
|
-
* send(bufferOrList, port, address)
|
|
236
|
-
* send(bufferOrList, port)
|
|
237
|
-
*
|
|
238
|
-
*/
|
|
239
|
-
Socket.prototype.send = function (buffer, offset, length, port, address, callback) {
|
|
240
|
-
const self = this
|
|
241
|
-
|
|
242
|
-
let list
|
|
243
|
-
|
|
244
|
-
if (address || (port && typeof port !== 'function')) {
|
|
245
|
-
buffer = sliceBuffer(buffer, offset, length)
|
|
246
|
-
} else {
|
|
247
|
-
callback = port
|
|
248
|
-
port = offset
|
|
249
|
-
address = length
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
if (!Array.isArray(buffer)) {
|
|
253
|
-
if (typeof buffer === 'string') {
|
|
254
|
-
list = [Buffer.from(buffer)]
|
|
255
|
-
} else if (!(buffer instanceof Buffer)) {
|
|
256
|
-
throw new TypeError('First argument must be a buffer or a string')
|
|
257
|
-
} else {
|
|
258
|
-
list = [buffer]
|
|
259
|
-
}
|
|
260
|
-
} else if (!(list = fixBufferList(buffer))) {
|
|
261
|
-
throw new TypeError('Buffer list arguments must be buffers or strings')
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
port = port >>> 0
|
|
265
|
-
if (port === 0 || port > 65535) {
|
|
266
|
-
throw new RangeError('Port should be > 0 and < 65536')
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
// Normalize callback so it's always a function
|
|
270
|
-
if (typeof callback !== 'function') {
|
|
271
|
-
callback = function () {}
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
if (self._bindState === BIND_STATE_UNBOUND) self.bind(0)
|
|
275
|
-
|
|
276
|
-
// If the socket hasn't been bound yet, push the outbound packet onto the
|
|
277
|
-
// send queue and send after binding is complete.
|
|
278
|
-
if (self._bindState !== BIND_STATE_BOUND) {
|
|
279
|
-
// If the send queue hasn't been initialized yet, do it, and install an
|
|
280
|
-
// event handler that flishes the send queue after binding is done.
|
|
281
|
-
if (!self._sendQueue) {
|
|
282
|
-
self._sendQueue = []
|
|
283
|
-
self.once('listening', function () {
|
|
284
|
-
// Flush the send queue.
|
|
285
|
-
for (let i = 0; i < self._sendQueue.length; i++) {
|
|
286
|
-
self.send.apply(self, self._sendQueue[i])
|
|
287
|
-
}
|
|
288
|
-
self._sendQueue = undefined
|
|
289
|
-
})
|
|
290
|
-
}
|
|
291
|
-
self._sendQueue.push([buffer, offset, length, port, address, callback])
|
|
292
|
-
return
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
const ab = Buffer.concat(list).buffer
|
|
296
|
-
|
|
297
|
-
chrome.sockets.udp.send(self.id, ab, address, port, function (sendInfo) {
|
|
298
|
-
if (sendInfo.resultCode < 0) {
|
|
299
|
-
const err = new Error('Socket ' + self.id + ' send error ' + sendInfo.resultCode)
|
|
300
|
-
callback(err)
|
|
301
|
-
self.emit('error', err)
|
|
302
|
-
} else {
|
|
303
|
-
callback(null)
|
|
304
|
-
}
|
|
305
|
-
})
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
function sliceBuffer (buffer, offset, length) {
|
|
309
|
-
if (typeof buffer === 'string') {
|
|
310
|
-
buffer = Buffer.from(buffer)
|
|
311
|
-
} else if (!(buffer instanceof Buffer)) {
|
|
312
|
-
throw new TypeError('First argument must be a buffer or string')
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
offset = offset >>> 0
|
|
316
|
-
length = length >>> 0
|
|
317
|
-
|
|
318
|
-
// assuming buffer is browser implementation (`buffer` package on npm)
|
|
319
|
-
let buf = buffer.buffer
|
|
320
|
-
if (buffer.byteOffset || buffer.byteLength !== buf.byteLength) {
|
|
321
|
-
buf = buf.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength)
|
|
322
|
-
}
|
|
323
|
-
if (offset || length !== buffer.length) {
|
|
324
|
-
buf = buf.slice(offset, length)
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
return Buffer.from(buf)
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
function fixBufferList (list) {
|
|
331
|
-
const newlist = new Array(list.length)
|
|
332
|
-
|
|
333
|
-
for (let i = 0, l = list.length; i < l; i++) {
|
|
334
|
-
const buf = list[i]
|
|
335
|
-
if (typeof buf === 'string') {
|
|
336
|
-
newlist[i] = Buffer.from(buf)
|
|
337
|
-
} else if (!(buf instanceof Buffer)) {
|
|
338
|
-
return null
|
|
339
|
-
} else {
|
|
340
|
-
newlist[i] = buf
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
return newlist
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
/**
|
|
348
|
-
* Close the underlying socket and stop listening for data on it.
|
|
349
|
-
*/
|
|
350
|
-
Socket.prototype.close = function () {
|
|
351
|
-
const self = this
|
|
352
|
-
if (self._destroyed) return
|
|
353
|
-
|
|
354
|
-
delete sockets[self.id]
|
|
355
|
-
chrome.sockets.udp.close(self.id)
|
|
356
|
-
self._destroyed = true
|
|
357
|
-
|
|
358
|
-
self.emit('close')
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
/**
|
|
362
|
-
* Returns an object containing the address information for a socket. For UDP
|
|
363
|
-
* sockets, this object will contain address, family and port.
|
|
364
|
-
*
|
|
365
|
-
* @return {Object} information
|
|
366
|
-
*/
|
|
367
|
-
Socket.prototype.address = function () {
|
|
368
|
-
const self = this
|
|
369
|
-
return {
|
|
370
|
-
address: self._address,
|
|
371
|
-
port: self._port,
|
|
372
|
-
family: 'IPv4'
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
Socket.prototype.setBroadcast = function (flag) {
|
|
377
|
-
// No chrome.sockets equivalent
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
Socket.prototype.setTTL = function (ttl) {
|
|
381
|
-
// No chrome.sockets equivalent
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
// NOTE: Multicast code is untested. Pull requests accepted for bug fixes and to
|
|
385
|
-
// add tests!
|
|
386
|
-
|
|
387
|
-
/**
|
|
388
|
-
* Sets the IP_MULTICAST_TTL socket option. TTL stands for "Time to Live," but
|
|
389
|
-
* in this context it specifies the number of IP hops that a packet is allowed
|
|
390
|
-
* to go through, specifically for multicast traffic. Each router or gateway
|
|
391
|
-
* that forwards a packet decrements the TTL. If the TTL is decremented to 0
|
|
392
|
-
* by a router, it will not be forwarded.
|
|
393
|
-
*
|
|
394
|
-
* The argument to setMulticastTTL() is a number of hops between 0 and 255.
|
|
395
|
-
* The default on most systems is 1.
|
|
396
|
-
*
|
|
397
|
-
* NOTE: The Chrome version of this function is async, whereas the node
|
|
398
|
-
* version is sync. Keep this in mind.
|
|
399
|
-
*
|
|
400
|
-
* @param {number} ttl
|
|
401
|
-
* @param {function} callback CHROME-SPECIFIC: Called when the configuration
|
|
402
|
-
* operation is done.
|
|
403
|
-
*/
|
|
404
|
-
Socket.prototype.setMulticastTTL = function (ttl, callback) {
|
|
405
|
-
const self = this
|
|
406
|
-
if (!callback) callback = function () {}
|
|
407
|
-
if (self._bindState === BIND_STATE_BOUND) {
|
|
408
|
-
setMulticastTTL(callback)
|
|
409
|
-
} else {
|
|
410
|
-
self._bindTasks.push({
|
|
411
|
-
fn: setMulticastTTL,
|
|
412
|
-
callback
|
|
413
|
-
})
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
function setMulticastTTL (callback) {
|
|
417
|
-
chrome.sockets.udp.setMulticastTimeToLive(self.id, ttl, callback)
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
/**
|
|
422
|
-
* Sets or clears the IP_MULTICAST_LOOP socket option. When this option is
|
|
423
|
-
* set, multicast packets will also be received on the local interface.
|
|
424
|
-
*
|
|
425
|
-
* NOTE: The Chrome version of this function is async, whereas the node
|
|
426
|
-
* version is sync. Keep this in mind.
|
|
427
|
-
*
|
|
428
|
-
* @param {boolean} flag
|
|
429
|
-
* @param {function} callback CHROME-SPECIFIC: Called when the configuration
|
|
430
|
-
* operation is done.
|
|
431
|
-
*/
|
|
432
|
-
Socket.prototype.setMulticastLoopback = function (flag, callback) {
|
|
433
|
-
const self = this
|
|
434
|
-
if (!callback) callback = function () {}
|
|
435
|
-
if (self._bindState === BIND_STATE_BOUND) {
|
|
436
|
-
setMulticastLoopback(callback)
|
|
437
|
-
} else {
|
|
438
|
-
self._bindTasks.push({
|
|
439
|
-
fn: setMulticastLoopback,
|
|
440
|
-
callback
|
|
441
|
-
})
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
function setMulticastLoopback (callback) {
|
|
445
|
-
chrome.sockets.udp.setMulticastLoopbackMode(self.id, flag, callback)
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
/**
|
|
450
|
-
* Tells the kernel to join a multicast group with IP_ADD_MEMBERSHIP socket
|
|
451
|
-
* option.
|
|
452
|
-
*
|
|
453
|
-
* If multicastInterface is not specified, the OS will try to add membership
|
|
454
|
-
* to all valid interfaces.
|
|
455
|
-
*
|
|
456
|
-
* NOTE: The Chrome version of this function is async, whereas the node
|
|
457
|
-
* version is sync. Keep this in mind.
|
|
458
|
-
*
|
|
459
|
-
* @param {string} multicastAddress
|
|
460
|
-
* @param {string} [multicastInterface] Optional
|
|
461
|
-
* @param {function} callback CHROME-SPECIFIC: Called when the configuration
|
|
462
|
-
* operation is done.
|
|
463
|
-
*/
|
|
464
|
-
Socket.prototype.addMembership = function (multicastAddress,
|
|
465
|
-
multicastInterface,
|
|
466
|
-
callback) {
|
|
467
|
-
const self = this
|
|
468
|
-
if (!callback) callback = function () {}
|
|
469
|
-
chrome.sockets.udp.joinGroup(self.id, multicastAddress, callback)
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
/**
|
|
473
|
-
* Opposite of addMembership - tells the kernel to leave a multicast group
|
|
474
|
-
* with IP_DROP_MEMBERSHIP socket option. This is automatically called by the
|
|
475
|
-
* kernel when the socket is closed or process terminates, so most apps will
|
|
476
|
-
* never need to call this.
|
|
477
|
-
*
|
|
478
|
-
* NOTE: The Chrome version of this function is async, whereas the node
|
|
479
|
-
* version is sync. Keep this in mind.
|
|
480
|
-
*
|
|
481
|
-
* If multicastInterface is not specified, the OS will try to drop membership
|
|
482
|
-
* to all valid interfaces.
|
|
483
|
-
*
|
|
484
|
-
* @param {[type]} multicastAddress
|
|
485
|
-
* @param {[type]} multicastInterface Optional
|
|
486
|
-
* @param {function} callback CHROME-SPECIFIC: Called when the configuration
|
|
487
|
-
* operation is done.
|
|
488
|
-
*/
|
|
489
|
-
Socket.prototype.dropMembership = function (multicastAddress,
|
|
490
|
-
multicastInterface,
|
|
491
|
-
callback) {
|
|
492
|
-
const self = this
|
|
493
|
-
if (!callback) callback = function () {}
|
|
494
|
-
chrome.sockets.udp.leaveGroup(self.id, multicastAddress, callback)
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
Socket.prototype.unref = function () {
|
|
498
|
-
// No chrome.sockets equivalent
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
Socket.prototype.ref = function () {
|
|
502
|
-
// No chrome.sockets equivalent
|
|
503
|
-
}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
// automatically generated by the FlatBuffers compiler, do not modify
|
|
2
|
-
|
|
3
|
-
/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion */
|
|
4
|
-
|
|
5
|
-
export enum ClientPacketType {
|
|
6
|
-
TCPSocket = 0,
|
|
7
|
-
TCPSocketConnect = 1,
|
|
8
|
-
TCPSocketDestroy = 2,
|
|
9
|
-
TCPSocketDestroySoon = 3,
|
|
10
|
-
TCPSocketEnd = 4,
|
|
11
|
-
TCPSocketPause = 5,
|
|
12
|
-
TCPSocketResetAndDestroy = 6,
|
|
13
|
-
TCPSocketResume = 7,
|
|
14
|
-
TCPSocketSetEncoding = 8,
|
|
15
|
-
TCPSocketSetKeepAlive = 9,
|
|
16
|
-
TCPSocketSetNoDelay = 10,
|
|
17
|
-
TCPSocketSetTimeout = 11,
|
|
18
|
-
TCPSocketWrite = 12,
|
|
19
|
-
TCPServer = 13,
|
|
20
|
-
TCPServerClose = 14,
|
|
21
|
-
TCPServerGetConnections = 15,
|
|
22
|
-
TCPServerListen = 16,
|
|
23
|
-
UDPSocket = 17,
|
|
24
|
-
UDPSocketAddMembership = 18,
|
|
25
|
-
UDPSocketAddSourceSpecificMembership = 19,
|
|
26
|
-
UDPSocketBind = 20,
|
|
27
|
-
UDPSocketClose = 21,
|
|
28
|
-
UDPSocketConnect = 22,
|
|
29
|
-
UDPSocketDisconnect = 23,
|
|
30
|
-
UDPSocketDropMembership = 24,
|
|
31
|
-
UDPSocketDropSourceSpecificMembership = 25,
|
|
32
|
-
UDPSocketSend = 26,
|
|
33
|
-
UDPSocketSetBroadcast = 27,
|
|
34
|
-
UDPSocketSetMulticastInterface = 28,
|
|
35
|
-
UDPSocketSetMulticastLoopback = 29,
|
|
36
|
-
UDPSocketSetMulticastTTL = 30,
|
|
37
|
-
UDPSocketSetRecvBufferSize = 31,
|
|
38
|
-
UDPSocketSetSendBufferSize = 32,
|
|
39
|
-
UDPSocketSetTTL = 33
|
|
40
|
-
}
|