UNPKG

@z0mt3c/f1-telemetry-client

Version:

[![Node.js CI](https://github.com/z0mt3c/f1-telemetry-client/actions/workflows/node.js.yml/badge.svg)](https://github.com/z0mt3c/f1-telemetry-client/actions/workflows/node.js.yml) [![NPM Release](https://img.shields.io/npm/v/@z0mt3c/f1-telemetry-client.sv

87 lines (86 loc) 3.58 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const __1 = require(".."); const fs = __importStar(require("fs")); const n_readlines_1 = __importDefault(require("n-readlines")); const cli_progress_1 = require("cli-progress"); const target = { ip: process.env.REPLAY_IP ?? '0.0.0.0', port: parseInt(process.env.REPLAY_PORT ?? '20999') }; const client = new __1.F1TelemetryClient({ port: 99999, bigintEnabled: true, forwardAddresses: [target], }); const sleep = async (ms) => { await new Promise((resolve) => setTimeout(resolve, ms)); }; const file = process.env.REPLAY_FILE ?? ''; const speed = parseFloat(process.env.REPLAY_SPEED ?? '1'); if (!fs.existsSync(file)) throw new Error(`REPLAY_FILE file not found: ${file}`); const fileSize = fs.statSync(file).size; const liner = new n_readlines_1.default(file); const startLine = parseInt(process.env.REPLAY_START_LINE ?? '0'); console.log(`Replay target: ${target.ip}:${target.port}`); console.log(`Replay file: ${file}`); console.log(`Start line: ${startLine}`); const progressBar = new cli_progress_1.SingleBar({}, cli_progress_1.Presets.shades_classic); progressBar.start(Math.round((fileSize / 1024 / 1024) * 10) / 10, 0); let read = 0; let no = 0; const start = async () => { let lastEventAt = null; let line = null; while ((line = liner?.next()) !== false && line != null) { read += line.length; if (no++ >= startLine) { const data = JSON.parse(line.toString()); const bufferData = data?.message?.data ?? data?.message; const buffer = Buffer.from(bufferData); const time = new Date(data.time); const timeToSleep = lastEventAt != null ? (time.getTime() - lastEventAt) / speed : 0; if (timeToSleep > 0) await sleep(timeToSleep); lastEventAt = time.getTime(); progressBar.update(Math.round((read / 1024 / 1024) * 10) / 10); client.bridgeMessage(buffer); } } progressBar.stop(); }; start().catch(console.log);