File
Blob: src/node/dgram.ts
| 1 | // Copyright (c) 2017-2022 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | // |
| 5 | // Copyright Joyent, Inc. and other Node contributors. |
| 6 | // |
| 7 | // Permission is hereby granted, free of charge, to any person obtaining a |
| 8 | // copy of this software and associated documentation files (the |
| 9 | // "Software"), to deal in the Software without restriction, including |
| 10 | // without limitation the rights to use, copy, modify, merge, publish, |
| 11 | // distribute, sublicense, and/or sell copies of the Software, and to permit |
| 12 | // persons to whom the Software is furnished to do so, subject to the |
| 13 | // following conditions: |
| 14 | // |
| 15 | // The above copyright notice and this permission notice shall be included |
| 16 | // in all copies or substantial portions of the Software. |
| 17 | // |
| 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 19 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 21 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 22 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 23 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 24 | // USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 25 | |
| 26 | import { validateObject, validateFunction } from 'node-internal:validators'; |
| 27 | import { Buffer } from 'node-internal:internal_buffer'; |
| 28 | import { EventEmitter } from 'node-internal:events'; |
| 29 | |
| 30 | import type { |
| 31 | SocketOptions, |
| 32 | SocketType, |
| 33 | RemoteInfo, |
| 34 | Socket as DgramSocket, |
| 35 | } from 'node:dgram'; |
| 36 | |
| 37 | type SocketClassType = typeof DgramSocket; |
| 38 | |
| 39 | export function Socket( |
| 40 | this: SocketClassType, |
| 41 | type?: SocketType | SocketOptions, |
| 42 | callback?: (msg: Buffer, rinfo: RemoteInfo) => void |
| 43 | ): SocketClassType { |
| 44 | EventEmitter.call(this as unknown as EventEmitter); |
| 45 | if (typeof type === 'string') { |
| 46 | type = { type }; |
| 47 | } |
| 48 | validateObject(type, 'type'); |
| 49 | if (callback !== undefined) { |
| 50 | validateFunction(callback, 'callback'); |
| 51 | } |
| 52 | return this; |
| 53 | } |
| 54 | Object.setPrototypeOf(Socket.prototype, EventEmitter.prototype); |
| 55 | Object.setPrototypeOf(Socket, EventEmitter); |
| 56 | |
| 57 | export function createSocket( |
| 58 | type?: SocketType | SocketOptions, |
| 59 | callback?: (msg: Buffer, rinfo: RemoteInfo) => void |
| 60 | ): SocketClassType { |
| 61 | // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-call |
| 62 | return new (Socket as any)(type, callback) as SocketClassType; |
| 63 | } |
| 64 | |
| 65 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 66 | Socket.prototype.bind = function ( |
| 67 | this: SocketClassType, |
| 68 | _1: unknown, |
| 69 | _2: unknown, |
| 70 | _3: unknown |
| 71 | ): SocketClassType { |
| 72 | return this; |
| 73 | }; |
| 74 | |
| 75 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 76 | Socket.prototype.connect = function ( |
| 77 | _1: unknown, |
| 78 | _2: unknown, |
| 79 | _3: unknown |
| 80 | ): void { |
| 81 | // no-op |
| 82 | }; |
| 83 | |
| 84 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 85 | Socket.prototype.disconnect = function (): void { |
| 86 | // no-op |
| 87 | }; |
| 88 | |
| 89 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 90 | Socket.prototype.sendto = function ( |
| 91 | _1: unknown, |
| 92 | _2: unknown, |
| 93 | _3: unknown, |
| 94 | _4: unknown, |
| 95 | _5: unknown, |
| 96 | _6: unknown |
| 97 | ): void { |
| 98 | // no-op |
| 99 | }; |
| 100 | |
| 101 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 102 | Socket.prototype.send = function ( |
| 103 | _1: unknown, |
| 104 | _2: unknown, |
| 105 | _3: unknown, |
| 106 | _4: unknown, |
| 107 | _5: unknown, |
| 108 | _6: unknown |
| 109 | ): void { |
| 110 | // no-op |
| 111 | }; |
| 112 | |
| 113 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 114 | Socket.prototype.close = function ( |
| 115 | this: SocketClassType, |
| 116 | _1: unknown |
| 117 | ): SocketClassType { |
| 118 | return this; |
| 119 | }; |
| 120 | |
| 121 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 122 | Socket.prototype[Symbol.asyncDispose] = async function (): Promise<void> { |
| 123 | // no-op |
| 124 | }; |
| 125 | |
| 126 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 127 | Socket.prototype.address = function (): object { |
| 128 | return {}; |
| 129 | }; |
| 130 | |
| 131 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 132 | Socket.prototype.remoteAddress = function (): object { |
| 133 | return {}; |
| 134 | }; |
| 135 | |
| 136 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 137 | Socket.prototype.setBroadcast = function (_: unknown): void {}; |
| 138 | |
| 139 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 140 | Socket.prototype.setTTL = function (_: unknown): void {}; |
| 141 | |
| 142 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 143 | Socket.prototype.setMulticastTTL = function (_: unknown): void {}; |
| 144 | |
| 145 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 146 | Socket.prototype.setMulticastLoopback = function (_: unknown): void {}; |
| 147 | |
| 148 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 149 | Socket.prototype.setMulticastInterface = function (_: unknown): void {}; |
| 150 | |
| 151 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 152 | Socket.prototype.addMembership = function (_1: unknown, _2: unknown): void {}; |
| 153 | |
| 154 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 155 | Socket.prototype.dropMembership = function (_1: unknown, _2: unknown): void {}; |
| 156 | |
| 157 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 158 | Socket.prototype.addSourceSpecificMembership = function ( |
| 159 | _1: unknown, |
| 160 | _2: unknown, |
| 161 | _3: unknown |
| 162 | ): void {}; |
| 163 | |
| 164 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 165 | Socket.prototype.dropSourceSpecificMembership = function ( |
| 166 | _1: unknown, |
| 167 | _2: unknown, |
| 168 | _3: unknown |
| 169 | ): void {}; |
| 170 | |
| 171 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 172 | Socket.prototype.ref = function (this: SocketClassType): SocketClassType { |
| 173 | return this; |
| 174 | }; |
| 175 | |
| 176 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 177 | Socket.prototype.unref = function (this: SocketClassType): SocketClassType { |
| 178 | return this; |
| 179 | }; |
| 180 | |
| 181 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 182 | Socket.prototype.setRecvBufferSize = function (_: number): void {}; |
| 183 | |
| 184 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 185 | Socket.prototype.setSendBufferSize = function (_: number): void {}; |
| 186 | |
| 187 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 188 | Socket.prototype.getRecvBufferSize = function (): number { |
| 189 | return 0; |
| 190 | }; |
| 191 | |
| 192 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 193 | Socket.prototype.getSendBufferSize = function (): number { |
| 194 | return 0; |
| 195 | }; |
| 196 | |
| 197 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 198 | Socket.prototype.getSendQueueSize = function (): number { |
| 199 | return 0; |
| 200 | }; |
| 201 | |
| 202 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 203 | Socket.prototype.getSendQueueCount = function (): number { |
| 204 | return 0; |
| 205 | }; |
| 206 | |
| 207 | export default { |
| 208 | createSocket, |
| 209 | Socket, |
| 210 | }; |