mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-20 07:43:12 +09:00
Replace remaining usage of phin (#862)
Replace the remaining places in the codebase using phin with fetch. With the exception of HttpCloser which uses the http/https request method instead.
This commit is contained in:
1 parent
ef0baaa30f
commit
05b6bf8c04
13 files changed
+91
-102
No files matched your search
+18
-20
@@ -3,7 +3,6 @@ import path from "path";
|
||||
import events from "events";
|
||||
import pidusage from "pidusage";
|
||||
import setBlocking from "set-blocking";
|
||||
import phin from "phin";
|
||||
import stream from "stream";
|
||||
import util from "util";
|
||||
|
||||
@@ -585,17 +584,17 @@ export default class Host extends lib.Link {
|
||||
|
||||
let url = new URL(this.config.get("host.controller_url"));
|
||||
url.pathname += `api/stream/${streamId}`;
|
||||
let response = await phin({
|
||||
url, method: "GET",
|
||||
stream: true,
|
||||
});
|
||||
let response = await fetch(url);
|
||||
if (!response.body) {
|
||||
throw new Error(`Empty body downloading stream ${streamId} from controller`);
|
||||
}
|
||||
|
||||
const file = lib.ModInfo.filename(mod.name, mod.version);
|
||||
const filePath = path.join(this.modStore.modsDirectory, file);
|
||||
const tempFilePath = filePath.replace(/(\.zip)?$/, ".tmp.zip");
|
||||
const writeStream = fs.createWriteStream(tempFilePath, { flags: "w" });
|
||||
await events.once(writeStream, "open");
|
||||
response.pipe(writeStream);
|
||||
stream.Readable.fromWeb(response.body).pipe(writeStream);
|
||||
await finished(writeStream);
|
||||
await fs.rename(tempFilePath, filePath);
|
||||
|
||||
@@ -1018,14 +1017,11 @@ export default class Host extends lib.Link {
|
||||
|
||||
let url = new URL(this.config.get("host.controller_url"));
|
||||
url.pathname += `api/stream/${streamId}`;
|
||||
let response = await phin({
|
||||
url, method: "GET",
|
||||
stream: true,
|
||||
});
|
||||
let response = await fetch(url);
|
||||
|
||||
if (response.statusCode !== 200) {
|
||||
let content = await lib.readStream(response);
|
||||
throw new lib.RequestError(`Stream returned ${response.statusCode}: ${content.toString()}`);
|
||||
if (response.status !== 200 || !response.body) {
|
||||
let body = Buffer.from(await response.arrayBuffer());
|
||||
throw new lib.RequestError(`Stream returned ${response.status}: ${body.toString()}`);
|
||||
}
|
||||
|
||||
let savesDir = path.join(instance.path, "saves");
|
||||
@@ -1044,7 +1040,7 @@ export default class Host extends lib.Link {
|
||||
}
|
||||
}
|
||||
}
|
||||
response.pipe(writeStream);
|
||||
stream.Readable.fromWeb(response.body).pipe(writeStream);
|
||||
await finished(writeStream);
|
||||
|
||||
name = await lib.findUnusedName(savesDir, name, ".zip");
|
||||
@@ -1059,10 +1055,11 @@ export default class Host extends lib.Link {
|
||||
checkRequestSaveName(name);
|
||||
let instance = this.getRequestInstance(instanceId);
|
||||
|
||||
let content: Buffer;
|
||||
let content = fs.createReadStream(path.join(instance.path, "saves", name));
|
||||
try {
|
||||
// phin doesn't support streaming requests :(
|
||||
content = await fs.readFile(path.join(instance.path, "saves", name));
|
||||
// TODO: Due to a bug in fs-extra the ready event is not emitted here. Replace
|
||||
// "open" with "ready" when fs-extra is replaced with node's fs module.
|
||||
await events.once(content, "open");
|
||||
} catch (err: any) {
|
||||
if (err.code === "ENOENT") {
|
||||
throw new lib.RequestError(`${name} does not exist`);
|
||||
@@ -1072,9 +1069,10 @@ export default class Host extends lib.Link {
|
||||
|
||||
let url = new URL(this.config.get("host.controller_url"));
|
||||
url.pathname += `api/stream/${streamId}`;
|
||||
phin({
|
||||
url, method: "PUT",
|
||||
data: content,
|
||||
fetch(url, {
|
||||
method: "PUT",
|
||||
body: content,
|
||||
duplex: "half",
|
||||
}).catch(err => {
|
||||
logger.error(`Error pushing save to controller:\n${err.stack}`, this.instanceLogMeta(instanceId));
|
||||
});
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import fs from "fs-extra";
|
||||
import path from "path";
|
||||
import pidusage from "pidusage";
|
||||
import phin from "phin";
|
||||
import util from "util";
|
||||
import type { Static } from "@sinclair/typebox";
|
||||
import { exec } from "child_process";
|
||||
@@ -1292,16 +1291,18 @@ end`.replace(/\r?\n/g, " ");
|
||||
let url = new URL(this._host.config.get("host.controller_url"));
|
||||
url.pathname += "api/upload-export";
|
||||
url.searchParams.set("mod_pack_id", String(this.activeModPack.id));
|
||||
let response = await phin({
|
||||
url, method: "PUT",
|
||||
data: content,
|
||||
let response = await fetch(url, {
|
||||
method: "PUT",
|
||||
body: content,
|
||||
duplex: "half",
|
||||
headers: {
|
||||
"Content-Type": "application/zip",
|
||||
"x-access-token": this._host.config.get("host.controller_token"),
|
||||
},
|
||||
});
|
||||
if (response.statusCode !== 200) {
|
||||
throw Error(`Upload failed: ${response.statusCode} ${response.statusMessage}: ${response.body}`);
|
||||
if (response.status !== 200) {
|
||||
const body = Buffer.from(await response.arrayBuffer());
|
||||
throw Error(`Upload failed: ${response.status} ${response.statusText}: ${body.toString()}`);
|
||||
}
|
||||
|
||||
} finally {
|
||||
|
||||
Reference in new issue
Block a user