Add support for running using deno (#362)

* Add missing CRLF line ending

* Add support for running using deno

Prefix node imports with "node:" and gate a socket API that is not
implemented in [deno](https://deno.land) so that the library can be used
there. This should not break node and doesn't in my brief testing.
This commit is contained in:
Tom 2023-10-10 09:25:57 +00:00 committed by GitHub
parent 150fa0035f
commit 01794f6339
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 32 additions and 14 deletions

View file

@ -1,6 +1,6 @@
import * as dns from 'dns'
import * as dns from 'node:dns'
import punycode from 'punycode/punycode.js'
import { promisify } from 'util'
import { promisify } from 'node:util'
const dnsLookupAsync = promisify(dns.lookup)
const dnsResolveAsync = promisify(dns.resolve)

View file

@ -1,6 +1,6 @@
import * as path from 'path'
import { fileURLToPath } from 'url'
import * as fs from 'fs'
import * as path from 'node:path'
import { fileURLToPath } from 'node:url'
import * as fs from 'node:fs'
export default class GameResolver {
constructor () {

View file

@ -1,6 +1,6 @@
import { createSocket } from 'dgram'
import { createSocket } from 'node:dgram'
import { debugDump } from './HexUtil.js'
import { promisify } from 'util'
import { promisify } from 'node:util'
import Logger from './Logger.js'
export default class GlobalUdpSocket {
@ -18,7 +18,10 @@ export default class GlobalUdpSocket {
type: 'udp4',
reuseAddr: true
})
udpSocket.unref()
// https://github.com/denoland/deno/issues/20138
if (typeof Deno === "undefined") {
udpSocket.unref();
}
udpSocket.on('message', (buffer, rinfo) => {
const fromAddress = rinfo.address
const fromPort = rinfo.port
@ -59,7 +62,7 @@ export default class GlobalUdpSocket {
this.debuggingCallbacks.add(callback)
this.logger.debugEnabled = true
}
}
}
removeCallback (callback) {
this.callbacks.delete(callback)

View file

@ -1,4 +1,5 @@
import { debugDump } from './HexUtil.js'
import { Buffer} from 'node:buffer'
export default class Logger {
constructor () {

View file

@ -69,12 +69,21 @@ export default class QueryRunner {
for (const attempt of attempts) {
for (let retry = 0; retry < numRetries; retry++) {
attemptNum++
let result
try {
return await this._attempt(attempt)
result = await this._attempt(attempt)
} catch (e) {
e.stack = 'Attempt #' + attemptNum + ' - Port=' + attempt.port + ' Retry=' + (retry) + ':\n' + e.stack
errors.push(e)
} finally {
// Deno doesn't support unref, so we must close the socket after every connection
// https://github.com/denoland/deno/issues/20138
if (typeof Deno !== "undefined") {
this.udpSocket?.socket?.close()
delete this.udpSocket
}
}
if (result) return result
}
}

View file

@ -1,6 +1,6 @@
import Iconv from 'iconv-lite'
import Long from 'long'
import { Buffer } from 'buffer'
import { Buffer } from 'node:buffer'
import Varint from 'varint'
function readUInt64BE (buffer, offset) {