From 66e62c5acb66860b52a94d998b62a3e469662c6c Mon Sep 17 00:00:00 2001 From: Terncode Date: Thu, 2 Apr 2026 20:46:36 +0200 Subject: [PATCH] Switch to node-fetch --- src/ts/server/boot.ts | 15 +++++++++++++++ src/ts/server/internal.ts | 26 ++++++++++++++++++-------- src/ts/tests/lib.ts | 15 +++++++++++++-- 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/src/ts/server/boot.ts b/src/ts/server/boot.ts index cc6b66a..4e348f6 100644 --- a/src/ts/server/boot.ts +++ b/src/ts/server/boot.ts @@ -8,6 +8,7 @@ import * as Bluebird from 'bluebird'; import * as fs from 'fs'; import * as yargs from 'yargs'; import { hideBin } from 'yargs/helpers'; +import { noop } from 'lodash'; const argv = (yargs as any)(hideBin(process.argv)) .option('beta', { @@ -29,4 +30,18 @@ const argv = (yargs as any)(hideBin(process.argv)) (global as any).TESTS = false; (global as any).performance = Date; +function PerformanceDate(...args: any[]) { + //@ts-ignore + return new Date(...args); +} + +PerformanceDate.now = Date.now; +PerformanceDate.parse = Date.parse; +PerformanceDate.UTC = Date.UTC; + +PerformanceDate.markResourceTiming = noop; + +(global as any).performance = PerformanceDate; + + Bluebird.promisifyAll(fs); diff --git a/src/ts/server/internal.ts b/src/ts/server/internal.ts index 48cf7d3..f3a6a9b 100644 --- a/src/ts/server/internal.ts +++ b/src/ts/server/internal.ts @@ -1,4 +1,3 @@ -import * as request from 'request-promise'; import { noop, flatMap, uniq } from 'lodash'; import { InternalGameServerState, ServerStatus, InternalLoginApi, InternalLoginServerState, InternalApi, HidingStats @@ -82,13 +81,24 @@ export function getServer(id: string) { export function createApi(host: string, url: string, apiToken: string): T { return new Proxy({} as any, { get: (_, key) => - (...args: any[]) => - Promise.resolve(request(`http://${host}/${url}/api`, { - json: true, - headers: { 'api-token': apiToken }, - method: 'post', - body: { method: key, args }, - })), + async (...args: any[]) => { + const response = await fetch(`http://${host}/${url}/api`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'api-token': apiToken, + }, + body: JSON.stringify({ method: key, args }), + }); + + if (!response.ok) { + throw new Error( + `API call failed — ${response.status} ${response.statusText} on method "${String(key)}"` + ); + } + + return response.json() as Promise; + }, }); } diff --git a/src/ts/tests/lib.ts b/src/ts/tests/lib.ts index d6d42b1..2e2b500 100644 --- a/src/ts/tests/lib.ts +++ b/src/ts/tests/lib.ts @@ -5,7 +5,7 @@ import * as mongoose from 'mongoose'; import * as fs from 'fs'; import * as path from 'path'; import { deleteAsync } from 'del'; -import { once, mapValues } from 'lodash'; +import { once, mapValues, noop } from 'lodash'; import { spawnSync } from 'child_process'; import { createStubInstance, SinonStubbedInstance, stub } from 'sinon'; import '../server/canvasUtilsNode'; @@ -22,7 +22,18 @@ require('chai').use(require('chai-as-promised')); (mongoose as any).modelSchemas = {}; (global as any).TESTS = true; (global as any).TOOLS = true; -(global as any).performance = Date; +function PerformanceDate(...args: any[]) { + //@ts-ignore + return new Date(...args); +} + +PerformanceDate.now = Date.now; +PerformanceDate.parse = Date.parse; +PerformanceDate.UTC = Date.UTC; + +PerformanceDate.markResourceTiming = noop; + +(global as any).performance = PerformanceDate; setPaletteManager(mockPaletteManager);