UNPKG

7.6 kBSource Map (JSON)View Raw
1{"version":3,"file":"instance-lookup.js","names":["_dns","_interopRequireDefault","require","_abortError","_sender","_withTimeout","obj","__esModule","default","SQL_SERVER_BROWSER_PORT","TIMEOUT","RETRIES","MYSTERY_HEADER_LENGTH","instanceLookup","options","server","TypeError","instanceName","timeout","undefined","retries","lookup","dns","port","signal","aborted","AbortError","response","i","withTimeout","request","Buffer","from","sendMessage","err","Error","name","message","toString","foundPort","parseBrowserResponse","getPort","instances","split","len","length","instance","parts","p","partsLen","value","parseInt","toUpperCase"],"sources":["../src/instance-lookup.ts"],"sourcesContent":["import dns from 'dns';\n\nimport AbortError from './errors/abort-error';\nimport { sendMessage } from './sender';\nimport { withTimeout } from './utils/with-timeout';\n\nconst SQL_SERVER_BROWSER_PORT = 1434;\nconst TIMEOUT = 2 * 1000;\nconst RETRIES = 3;\n// There are three bytes at the start of the response, whose purpose is unknown.\nconst MYSTERY_HEADER_LENGTH = 3;\n\ntype LookupFunction = (hostname: string, options: dns.LookupAllOptions, callback: (err: NodeJS.ErrnoException | null, addresses: dns.LookupAddress[]) => void) => void;\n\n// Most of the functionality has been determined from from jTDS's MSSqlServerInfo class.\nexport async function instanceLookup(options: { server: string, instanceName: string, timeout?: number, retries?: number, port?: number, lookup?: LookupFunction, signal: AbortSignal }) {\n const server = options.server;\n if (typeof server !== 'string') {\n throw new TypeError('Invalid arguments: \"server\" must be a string');\n }\n\n const instanceName = options.instanceName;\n if (typeof instanceName !== 'string') {\n throw new TypeError('Invalid arguments: \"instanceName\" must be a string');\n }\n\n const timeout = options.timeout === undefined ? TIMEOUT : options.timeout;\n if (typeof timeout !== 'number') {\n throw new TypeError('Invalid arguments: \"timeout\" must be a number');\n }\n\n const retries = options.retries === undefined ? RETRIES : options.retries;\n if (typeof retries !== 'number') {\n throw new TypeError('Invalid arguments: \"retries\" must be a number');\n }\n\n if (options.lookup !== undefined && typeof options.lookup !== 'function') {\n throw new TypeError('Invalid arguments: \"lookup\" must be a function');\n }\n const lookup = options.lookup ?? dns.lookup;\n\n if (options.port !== undefined && typeof options.port !== 'number') {\n throw new TypeError('Invalid arguments: \"port\" must be a number');\n }\n const port = options.port ?? SQL_SERVER_BROWSER_PORT;\n\n const signal = options.signal;\n\n if (signal.aborted) {\n throw new AbortError();\n }\n\n let response;\n\n for (let i = 0; i <= retries; i++) {\n try {\n response = await withTimeout(timeout, async (signal) => {\n const request = Buffer.from([0x02]);\n return await sendMessage(options.server, port, lookup, signal, request);\n }, signal);\n } catch (err) {\n // If the current attempt timed out, continue with the next\n if (!signal.aborted && err instanceof Error && err.name === 'TimeoutError') {\n continue;\n }\n\n throw err;\n }\n }\n\n if (!response) {\n throw new Error('Failed to get response from SQL Server Browser on ' + server);\n }\n\n const message = response.toString('ascii', MYSTERY_HEADER_LENGTH);\n const foundPort = parseBrowserResponse(message, instanceName);\n\n if (!foundPort) {\n throw new Error('Port for ' + instanceName + ' not found in ' + options.server);\n }\n\n return foundPort;\n}\n\nexport function parseBrowserResponse(response: string, instanceName: string) {\n let getPort;\n\n const instances = response.split(';;');\n for (let i = 0, len = instances.length; i < len; i++) {\n const instance = instances[i];\n const parts = instance.split(';');\n\n for (let p = 0, partsLen = parts.length; p < partsLen; p += 2) {\n const name = parts[p];\n const value = parts[p + 1];\n\n if (name === 'tcp' && getPort) {\n const port = parseInt(value, 10);\n return port;\n }\n\n if (name === 'InstanceName') {\n if (value.toUpperCase() === instanceName.toUpperCase()) {\n getPort = true;\n } else {\n getPort = false;\n }\n }\n }\n }\n}\n"],"mappings":";;;;;;;AAAA,IAAAA,IAAA,GAAAC,sBAAA,CAAAC,OAAA;AAEA,IAAAC,WAAA,GAAAF,sBAAA,CAAAC,OAAA;AACA,IAAAE,OAAA,GAAAF,OAAA;AACA,IAAAG,YAAA,GAAAH,OAAA;AAAmD,SAAAD,uBAAAK,GAAA,WAAAA,GAAA,IAAAA,GAAA,CAAAC,UAAA,GAAAD,GAAA,KAAAE,OAAA,EAAAF,GAAA;AAEn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