| 48 | */ |
| 49 | |
| 50 | export class Client { |
| 51 | constructor(dictionary) { |
| 52 | // port, host, address, path (everything after port) |
| 53 | this.path = dictionary.path ?? "/"; |
| 54 | this.host = dictionary.host ?? dictionary.address; |
| 55 | this.headers = dictionary.headers ?? []; |
| 56 | this.protocol = dictionary.protocol; |
| 57 | this.state = 0; |
| 58 | this.flags = 0; |
| 59 | |
| 60 | if (dictionary.socket) |
| 61 | this.socket = dictionary.socket; |
| 62 | else { |
| 63 | dictionary.port ??= 80; |
| 64 | if (dictionary.Socket) |
| 65 | this.socket = new dictionary.Socket(Object.assign({}, dictionary.Socket, dictionary)); |
| 66 | else |
| 67 | this.socket = new Socket(dictionary); |
| 68 | } |
| 69 | this.socket.callback = callback.bind(this); |
| 70 | this.doMask = true; |
| 71 | } |
| 72 | |
| 73 | write(message) { |
| 74 | //@@ implement masking |
| 75 | const type = (message instanceof ArrayBuffer) ? 0x82 : 0x81; |
| 76 | if (0x81 === type) |
| 77 | message = ArrayBuffer.fromString(message); |
| 78 | |
| 79 | const length = message.byteLength; |
| 80 | // Note: WS spec requires XOR masking for clients, but w/ strongly random mask. We |
| 81 | // can't achieve that on this device for now, so just punt and use 0x00000000 for |
| 82 | // a no-op mask. |
| 83 | if (length < 126) { |
| 84 | if (this.doMask) |
| 85 | this.socket.write(type, length | 0x80, 0, 0, 0, 0, message); |
| 86 | else |
| 87 | this.socket.write(type, length, message); |
| 88 | } |
| 89 | else if (length < 65536) { |
| 90 | if (this.doMask) |
| 91 | this.socket.write(type, 126 | 0x80, length >> 8, length & 0x0ff, 0, 0, 0, 0, message); |
| 92 | else |
| 93 | this.socket.write(type, 126, length >> 8, length & 0x0ff, message); |
| 94 | } |
| 95 | else |
| 96 | throw new Error("message too long"); |
| 97 | } |
| 98 | detach() { |
| 99 | const socket = this.socket; |
| 100 | delete this.socket.callback; |
| 101 | delete this.socket; |
| 102 | return socket; |
| 103 | } |
| 104 | close() { |
| 105 | this.socket?.close(); |
| 106 | delete this.socket; |
| 107 |
nothing calls this directly
no outgoing calls
no test coverage detected