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KAAK,eACL,cAAc,KAAK,cACnB,KAAK,kBAAe,EAE5B,CACA,UAAM,GACF,KAAK,MAAM,CAAC,IAAI,QACpB,CACA,MAAA,CAAO,GACH,OAAO,IAAI,SAAQ,CAAC,EAAS,KACzB,IAAK,KAAK,OACN,OAAO,EAAO,IAAI,MAAM,uEAC5B,MAAM,EAAM,KAAK,OAAO,MAAM,EAAO,MAAM,IAChC,EAAQ,IACjB,GAEV,CACA,aAAM,GAEF,IADA,KAAK,SAAU,EACR,KAAK,MAAM,QAAQ,CACtB,MAAM,EAAO,KAAK,MAAM,QACnB,SAEC,KAAK,OAAO,EACtB,OACM,KAAK,OAAO,IACd,KAAK,MAAM,OACX,QAAQ,UAAS,KACb,KAAK,SAAS,IAIlB,KAAK,SAAU,CAEvB,CAMA,WAAM,CAAM,GACR,IAAK,MAAM,KAAO,EACd,KAAK,MAAM,WAAW,UAAU,OAAO,IAEvC,KAAK,MAAM,SAAW,KAAK,SAC3B,KAAK,SAEb,CACA,SAAA,CAAU,GACN,KAAK,MAAM,KAAK,GACZ,KAAK,MAAM,SAAW,KAAK,SAC3B,KAAK,SAEb,CAEA,cAAO,CAAQ,GAEX,MAAM,GADN,EAAM,EAAI,QAAQ,cAAe,KACf,MAAM,aACxB,OAAK,EAEE,CACH,MAAO,EAAM,GACb,UAAW,EAAM,IAHV,IAKf,QAEK,KAAK,sBACL,wBAA2B,2BAC3B,OAAQ","file":"/npm/@tak-ps/node-tak@11.25.1/dist/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/@tak-ps/node-tak@11.25.1/dist/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","import EventEmitter from 'node:events';\nimport tls from 'node:tls';\nimport CoT, { CoTParser } from '@tak-ps/node-cot';\nimport TAKAPI from './lib/api.js';\nexport * from './lib/auth.js';\n/* eslint-disable no-control-regex */\nexport const REGEX_CONTROL = /[\\u000B-\\u001F\\u007F-\\u009F]/g;\n// Match <event .../> or <event> but not <events>\nexport const REGEX_EVENT = /(<event[ >][\\s\\S]*?<\\/event>)([\\s\\S]*)/;\nexport default class TAK extends EventEmitter {\n    id;\n    type;\n    url;\n    auth;\n    open;\n    destroyed;\n    queue;\n    writing;\n    cotOptions;\n    pingInterval;\n    client;\n    version;\n    /**\n     * @param url   - Full URL of Streaming COT Endpoint IE: \"https://ops.cotak.gov:8089\"\n     * @param auth  - TAK Certificate Pair\n     * @param opts  - Options Object\n     * @param opts.id   - When using multiple connections in a script, allows a unique ID per connection\n     * @param opts.type - When using multiple connections in a script, allows specifying a script provided connection type\n     */\n    constructor(url, auth, opts = {}) {\n        super();\n        if (!opts)\n            opts = {};\n        this.id = opts.id || crypto.randomUUID();\n        this.type = opts.type || 'unknown';\n        this.url = url;\n        this.auth = auth;\n        this.writing = false;\n        this.cotOptions = opts.cot || {};\n        this.open = false;\n        this.destroyed = false;\n        this.queue = [];\n    }\n    static async connect(url, auth, opts = {}) {\n        const tak = new TAK(url, auth, opts);\n        if (url.protocol === 'ssl:') {\n            if (!tak.auth.cert)\n                throw new Error('auth.cert required');\n            if (!tak.auth.key)\n                throw new Error('auth.key required');\n            return await tak.connect_ssl();\n        }\n        else {\n            throw new Error('Unknown TAK Server Protocol');\n        }\n    }\n    connect_ssl() {\n        return new Promise((resolve) => {\n            this.destroyed = false;\n            this.client = tls.connect({\n                host: this.url.hostname,\n                port: parseInt(this.url.port),\n                rejectUnauthorized: this.auth.rejectUnauthorized ?? false,\n                cert: this.auth.cert,\n                key: this.auth.key,\n                passphrase: this.auth.passphrase,\n                ca: this.auth.ca,\n            });\n            this.client.setNoDelay();\n            this.client.on('connect', () => {\n                console.error(`ok - ${this.id} @ connect:${this.client ? this.client.authorized : 'NO CLIENT'} - ${this.client ? this.client.authorizationError : 'NO CLIENT'}`);\n            });\n            this.client.on('secureConnect', () => {\n                console.error(`ok - ${this.id} @ secure:${this.client ? this.client.authorized : 'NO CLIENT'} - ${this.client ? this.client.authorizationError : 'NO CLIENT'}`);\n                this.emit('secureConnect');\n                this.ping();\n            });\n            let buff = '';\n            this.client.on('data', async (data) => {\n                // Eventually Parse ProtoBuf\n                buff = buff + data.toString();\n                let result = TAK.findCoT(buff);\n                while (result && result.event) {\n                    try {\n                        const cot = await CoTParser.from_xml(result.event, this.cotOptions);\n                        if (cot.raw.event._attributes.type === 't-x-c-t-r') {\n                            this.open = true;\n                            this.emit('ping');\n                        }\n                        else if (cot.raw.event._attributes.type === 't-x-takp-v'\n                            && cot.raw.event.detail\n                            && cot.raw.event.detail.TakControl\n                            && cot.raw.event.detail.TakControl.TakServerVersionInfo\n                            && cot.raw.event.detail.TakControl.TakServerVersionInfo._attributes) {\n                            this.version = cot.raw.event.detail.TakControl.TakServerVersionInfo._attributes.serverVersion;\n                        }\n                        else {\n                            this.emit('cot', cot);\n                        }\n                    }\n                    catch (e) {\n                        console.error('Error parsing', e, data.toString());\n                    }\n                    buff = result.remainder;\n                    result = TAK.findCoT(buff);\n                }\n            }).on('timeout', () => {\n                this.emit('timeout');\n            }).on('error', (err) => {\n                this.emit('error', err);\n            }).on('end', () => {\n                this.open = false;\n                this.emit('end');\n            });\n            this.pingInterval = setInterval(() => {\n                this.ping();\n            }, 5000);\n            return resolve(this);\n        });\n    }\n    async reconnect() {\n        if (this.destroyed) {\n            await this.connect_ssl();\n        }\n        else {\n            this.destroy();\n            await this.connect_ssl();\n        }\n    }\n    destroy() {\n        this.destroyed = true;\n        if (this.client) {\n            this.client.destroy();\n        }\n        if (this.pingInterval) {\n            clearInterval(this.pingInterval);\n            this.pingInterval = undefined;\n        }\n    }\n    async ping() {\n        this.write([CoT.ping()]);\n    }\n    writer(body) {\n        return new Promise((resolve, reject) => {\n            if (!this.client)\n                return reject(new Error('A Connection Client must first be created before it can be written'));\n            const res = this.client.write(body + '\\n', () => {\n                return resolve(res);\n            });\n        });\n    }\n    async process() {\n        this.writing = true;\n        while (this.queue.length) {\n            const body = this.queue.shift();\n            if (!body)\n                continue;\n            await this.writer(body);\n        }\n        await this.writer('');\n        if (this.queue.length) {\n            process.nextTick(() => {\n                this.process();\n            });\n        }\n        else {\n            this.writing = false;\n        }\n    }\n    /**\n     * Write a CoT to the TAK Connection\n     *\n     * @param {CoT} cots CoT Object\n     */\n    async write(cots) {\n        for (const cot of cots) {\n            this.queue.push(await CoTParser.to_xml(cot));\n        }\n        if (this.queue.length && !this.writing) {\n            this.process();\n        }\n    }\n    write_xml(body) {\n        this.queue.push(body);\n        if (this.queue.length && !this.writing) {\n            this.process();\n        }\n    }\n    // https://github.com/vidterra/multitak/blob/main/app/lib/helper.js#L4\n    static findCoT(str) {\n        str = str.replace(REGEX_CONTROL, \"\");\n        const match = str.match(REGEX_EVENT); // find first CoT\n        if (!match)\n            return null;\n        return {\n            event: match[1],\n            remainder: match[2],\n        };\n    }\n}\nexport * from './lib/api.js';\nexport { CommandOutputFormat } from './lib/commands.js';\nexport { TAKAPI, CoT, };\n"]}