Switch to node-fetch

This commit is contained in:
2026-04-02 20:46:36 +02:00
parent efea0eb133
commit 66e62c5acb
3 changed files with 46 additions and 10 deletions
+15
View File
@@ -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);
+18 -8
View File
@@ -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<T extends {}>(host: string, url: string, apiToken: string): T {
return new Proxy<T>({} as any, {
get: (_, key) =>
(...args: any[]) =>
Promise.resolve<T>(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<T>;
},
});
}
+13 -2
View File
@@ -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);