Update http request and move versions to core #483
74
package-lock.json
generated
74
package-lock.json
generated
@ -27,6 +27,7 @@
|
||||
"@types/tar": "^6.1.3",
|
||||
"mocha": "^10.1.0",
|
||||
"ts-node": "^10.9.1",
|
||||
"tsconfig-paths": "^4.1.0",
|
||||
"typedoc": "^0.23.19",
|
||||
"typescript": "^4.8.4"
|
||||
},
|
||||
@ -2682,6 +2683,18 @@
|
||||
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz",
|
||||
"integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg=="
|
||||
},
|
||||
"node_modules/json5": {
|
||||
"version": "2.2.1",
|
||||
"resolved": "https://registry.npmjs.org/json5/-/json5-2.2.1.tgz",
|
||||
"integrity": "sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA==",
|
||||
"dev": true,
|
||||
"bin": {
|
||||
"json5": "lib/cli.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=6"
|
||||
}
|
||||
},
|
||||
"node_modules/jsonc-parser": {
|
||||
"version": "3.2.0",
|
||||
"resolved": "https://registry.npmjs.org/jsonc-parser/-/jsonc-parser-3.2.0.tgz",
|
||||
@ -2877,6 +2890,15 @@
|
||||
"node": ">=10"
|
||||
}
|
||||
},
|
||||
"node_modules/minimist": {
|
||||
"version": "1.2.7",
|
||||
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.7.tgz",
|
||||
"integrity": "sha512-bzfL1YUZsP41gmu/qjrEk0Q6i2ix/cVeAhbCbqH9u3zYutS1cLg00qhrD0M2MVdCcx4Sc0UpP2eBWo9rotpq6g==",
|
||||
"dev": true,
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/ljharb"
|
||||
}
|
||||
},
|
||||
"node_modules/minipass": {
|
||||
"version": "3.3.5",
|
||||
"resolved": "https://registry.npmjs.org/minipass/-/minipass-3.3.5.tgz",
|
||||
@ -3692,6 +3714,15 @@
|
||||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/strip-bom": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz",
|
||||
"integrity": "sha512-vavAMRXOgBVNF6nyEEmL3DBK19iRpDcoIwW+swQ+CbGiu7lju6t+JklA1MHweoWtadgt4ISVUsXLyDq34ddcwA==",
|
||||
"dev": true,
|
||||
"engines": {
|
||||
"node": ">=4"
|
||||
}
|
||||
},
|
||||
"node_modules/strip-json-comments": {
|
||||
"version": "3.1.1",
|
||||
"resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.1.1.tgz",
|
||||
@ -3843,6 +3874,20 @@
|
||||
"node": ">=0.3.1"
|
||||
}
|
||||
},
|
||||
"node_modules/tsconfig-paths": {
|
||||
"version": "4.1.0",
|
||||
"resolved": "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-4.1.0.tgz",
|
||||
"integrity": "sha512-AHx4Euop/dXFC+Vx589alFba8QItjF+8hf8LtmuiCwHyI4rHXQtOOENaM8kvYf5fR0dRChy3wzWIZ9WbB7FWow==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"json5": "^2.2.1",
|
||||
"minimist": "^1.2.6",
|
||||
"strip-bom": "^3.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=6"
|
||||
}
|
||||
},
|
||||
"node_modules/tslib": {
|
||||
"version": "2.4.1",
|
||||
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.4.1.tgz",
|
||||
@ -6280,6 +6325,12 @@
|
||||
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz",
|
||||
"integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg=="
|
||||
},
|
||||
"json5": {
|
||||
"version": "2.2.1",
|
||||
"resolved": "https://registry.npmjs.org/json5/-/json5-2.2.1.tgz",
|
||||
"integrity": "sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA==",
|
||||
"dev": true
|
||||
},
|
||||
"jsonc-parser": {
|
||||
"version": "3.2.0",
|
||||
"resolved": "https://registry.npmjs.org/jsonc-parser/-/jsonc-parser-3.2.0.tgz",
|
||||
@ -6418,6 +6469,12 @@
|
||||
"brace-expansion": "^2.0.1"
|
||||
}
|
||||
},
|
||||
"minimist": {
|
||||
"version": "1.2.7",
|
||||
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.7.tgz",
|
||||
"integrity": "sha512-bzfL1YUZsP41gmu/qjrEk0Q6i2ix/cVeAhbCbqH9u3zYutS1cLg00qhrD0M2MVdCcx4Sc0UpP2eBWo9rotpq6g==",
|
||||
"dev": true
|
||||
},
|
||||
"minipass": {
|
||||
"version": "3.3.5",
|
||||
"resolved": "https://registry.npmjs.org/minipass/-/minipass-3.3.5.tgz",
|
||||
@ -7023,6 +7080,12 @@
|
||||
"ansi-regex": "^5.0.1"
|
||||
}
|
||||
},
|
||||
"strip-bom": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz",
|
||||
"integrity": "sha512-vavAMRXOgBVNF6nyEEmL3DBK19iRpDcoIwW+swQ+CbGiu7lju6t+JklA1MHweoWtadgt4ISVUsXLyDq34ddcwA==",
|
||||
"dev": true
|
||||
},
|
||||
"strip-json-comments": {
|
||||
"version": "3.1.1",
|
||||
"resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.1.1.tgz",
|
||||
@ -7124,6 +7187,17 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"tsconfig-paths": {
|
||||
"version": "4.1.0",
|
||||
"resolved": "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-4.1.0.tgz",
|
||||
"integrity": "sha512-AHx4Euop/dXFC+Vx589alFba8QItjF+8hf8LtmuiCwHyI4rHXQtOOENaM8kvYf5fR0dRChy3wzWIZ9WbB7FWow==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"json5": "^2.2.1",
|
||||
"minimist": "^1.2.6",
|
||||
"strip-bom": "^3.0.0"
|
||||
}
|
||||
},
|
||||
"tslib": {
|
||||
"version": "2.4.1",
|
||||
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.4.1.tgz",
|
||||
|
@ -16,7 +16,7 @@
|
||||
"docs": "typedoc --readme none --out docs src/index.ts",
|
||||
"dev": "nodemon",
|
||||
"build": "tsc",
|
||||
"test:partial": "mocha -r ts-node/register --exit",
|
||||
"test:partial": "mocha -r ts-node/register -r tsconfig-paths/register --exit",
|
||||
"test": "npm run test:partial -- 'tests/**/*.ts'"
|
||||
},
|
||||
"repository": {
|
||||
@ -50,6 +50,7 @@
|
||||
"@types/tar": "^6.1.3",
|
||||
"mocha": "^10.1.0",
|
||||
"ts-node": "^10.9.1",
|
||||
"tsconfig-paths": "^4.1.0",
|
||||
"typedoc": "^0.23.19",
|
||||
"typescript": "^4.8.4"
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ import * as globalPlatfroms from "./globalPlatfroms";
|
||||
import { platformManeger } from "@the-bds-maneger/server_versions";
|
||||
import { pathControl, bdsPlatformOptions } from "./platformPathManeger";
|
||||
import { commendExists } from "./lib/childPromisses";
|
||||
import * as httpRequest from "./lib/httpRequest";
|
||||
import * as httpLarge from "@http/large";
|
||||
import extendsFs, { exists, readdirrecursive } from "./lib/extendsFs";
|
||||
import { randomPort } from "./lib/randomPort";
|
||||
import { manegerConfigProprieties } from "./configManipulate";
|
||||
@ -28,7 +28,7 @@ export async function installServer(version: string|boolean, platformOptions: bd
|
||||
await fs.readdir(serverPath).then(files => Promise.all(files.filter(file => !saveFileFolder.test(file)).map(file => fs.rm(path.join(serverPath, file), {recursive: true, force: true}))));
|
||||
|
||||
const serverConfigProperties = (await fs.readFile(path.join(serverPath, "server.properties"), "utf8").catch(() => "")).trim();
|
||||
await httpRequest.extractZip({url, folderTarget: serverPath});
|
||||
await httpLarge.extractZip({url, folderTarget: serverPath});
|
||||
if (serverConfigProperties) await fs.writeFile(path.join(serverPath, "server.properties"), serverConfigProperties);
|
||||
await fs.writeFile(path.join(serverRoot, "version_installed.json"), JSON.stringify({version: bedrockData.version, date: bedrockData.date, installDate: new Date()}));
|
||||
|
||||
@ -286,4 +286,4 @@ export async function addResourcePacksToWorld(resourceId: string, platformOption
|
||||
resourcesData.push({pack_id: packInfo.header.uuid, version: packInfo.header.version});
|
||||
await fs.writeFile(path.join(serverPath, "worlds", serverConfigObject["level-name"], "world_resource_packs.json"), JSON.stringify(resourcesData, null, 2));
|
||||
return resourcesData;
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ import fs from "node:fs/promises";
|
||||
import tar from "tar";
|
||||
import { bdsRoot } from "./platformPathManeger";
|
||||
import { exists } from "./lib/extendsFs";
|
||||
import { getExternalIP } from "./lib/httpRequest";
|
||||
import { getExternalIP } from "@http/client";
|
||||
|
||||
export type payload = {
|
||||
httpVersion?: string,
|
||||
@ -153,4 +153,4 @@ export async function importBds(option: {host: string, port: number, authToken:
|
||||
tar_extract.on("error", reject);
|
||||
socket.once("close", () => tar_extract.on("finish", done));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
18
src/index.ts
18
src/index.ts
@ -1,5 +1,10 @@
|
||||
// HTTP
|
||||
import * as httpSimples from "@http/simples";
|
||||
import * as httpLarge from "@http/large";
|
||||
import * as httpGithub from "@http/github";
|
||||
import * as httpClient from "@http/client";
|
||||
|
||||
// Utils
|
||||
import * as httpRequest from "./lib/httpRequest";
|
||||
import * as platformPathManeger from "./platformPathManeger"
|
||||
import * as globalPlatfroms from "./globalPlatfroms";
|
||||
import * as pluginManeger from "./plugin/plugin";
|
||||
@ -15,7 +20,7 @@ import * as Spigot from "./spigot";
|
||||
import * as Powernukkit from "./pwnuukit";
|
||||
import * as PaperMC from "./paper";
|
||||
|
||||
export {platformPathManeger, globalPlatfroms, pluginManeger, export_import, PocketmineMP, pluginHooks, Powernukkit, httpRequest, PaperMC, Bedrock, Spigot, proxy, Java};
|
||||
export {platformPathManeger, globalPlatfroms, pluginManeger, export_import, PocketmineMP, pluginHooks, Powernukkit, PaperMC, Bedrock, Spigot, proxy, Java};
|
||||
export default {
|
||||
Bedrock,
|
||||
Java,
|
||||
@ -28,8 +33,13 @@ export default {
|
||||
globalPlatfroms,
|
||||
pluginManeger,
|
||||
pluginHooks,
|
||||
httpRequest,
|
||||
export_import,
|
||||
proxy
|
||||
proxy,
|
||||
httpRequest: {
|
||||
httpSimples,
|
||||
httpLarge,
|
||||
httpGithub,
|
||||
httpClient
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -4,7 +4,7 @@ import fsOld from "node:fs";
|
||||
import os from "node:os";
|
||||
import { platformManeger } from "@the-bds-maneger/server_versions";
|
||||
import { actionV2, actionsV2 } from "./globalPlatfroms";
|
||||
import { saveFile } from "./lib/httpRequest";
|
||||
import { saveFile } from "@http/large";
|
||||
import { pathControl, bdsPlatformOptions } from "./platformPathManeger";
|
||||
import { manegerConfigProprieties } from "./configManipulate";
|
||||
import { randomPort } from "./lib/randomPort";
|
||||
@ -181,4 +181,4 @@ export async function serverConfig(platformOptions: bdsPlatformOptions = {id: "d
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -1,236 +0,0 @@
|
||||
import type { Method } from "got";
|
||||
import os from "node:os";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import crypto from "node:crypto";
|
||||
import tar from "tar";
|
||||
import AdmZip from "adm-zip";
|
||||
import stream from "node:stream";
|
||||
import { exists } from "./extendsFs";
|
||||
|
||||
let got: (typeof import("got"))["default"];
|
||||
const gotCjs = async () => got||(await (eval('import("got")') as Promise<typeof import("got")>)).default.extend({enableUnixSockets: true});
|
||||
gotCjs().then(res => got = res);
|
||||
|
||||
export type requestOptions = {
|
||||
url?: string,
|
||||
socket?: {
|
||||
socketPath: string,
|
||||
path?: string,
|
||||
}
|
||||
method?: Method,
|
||||
headers?: {[headerName: string]: string[]|string},
|
||||
body?: any,
|
||||
};
|
||||
|
||||
export async function pipeFetch(options: requestOptions & {stream: fs.WriteStream|stream.Writable, waitFinish?: boolean}) {
|
||||
if (!(options?.url||options?.socket)) throw new Error("Host blank")
|
||||
const urlRequest = (typeof options.url === "string")?options.url:`http://unix:${options.socket.socketPath}:${options.socket.path||"/"}`;
|
||||
const gotStream = (await gotCjs()).stream(urlRequest, {
|
||||
isStream: true,
|
||||
headers: options.headers||{},
|
||||
method: options.method||"GET",
|
||||
json: options.body,
|
||||
});
|
||||
await new Promise<void>((done, reject) => {
|
||||
gotStream.pipe(options.stream);
|
||||
options.stream.on("error", reject);
|
||||
gotStream.on("error", reject);
|
||||
gotStream.once("end", () => {
|
||||
if (options.waitFinish) return options.stream.once("finish", done);
|
||||
return done();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export async function bufferFetch(options: string|requestOptions) {
|
||||
if (typeof options === "string") options = {url: options};
|
||||
if (!(options.url||options.socket)) throw new Error("Host blank")
|
||||
const urlRequest = (typeof options.url === "string")?options.url:`http://unix:${options.socket.socketPath}:${options.socket.path||"/"}`;
|
||||
return (await gotCjs())(urlRequest, {
|
||||
headers: options.headers||{},
|
||||
method: options.method||"GET",
|
||||
json: options.body,
|
||||
responseType: "buffer",
|
||||
}).then(res => ({headers: res.headers, data: Buffer.from(res.body), response: res}));
|
||||
}
|
||||
|
||||
export async function getJSON<JSONReturn = any>(request: string|requestOptions): Promise<JSONReturn> {
|
||||
if (typeof request === "string") request = {url: request};
|
||||
const res = (await bufferFetch(request)).data.toString("utf8");
|
||||
return JSON.parse(res) as JSONReturn;
|
||||
}
|
||||
|
||||
export async function saveFile(request: string|requestOptions & {filePath?: string}) {
|
||||
if (typeof request === "string") request = {url: request};
|
||||
const filePath = request.filePath||path.join(os.tmpdir(), `raw_bdscore_${Date.now()}_${(path.parse(request.url||request.socket?.path||crypto.randomBytes(16).toString("hex"))).name}`);
|
||||
await pipeFetch({...request, waitFinish: true, stream: fs.createWriteStream(filePath, {autoClose: false})});
|
||||
return filePath;
|
||||
}
|
||||
|
||||
export async function tarExtract(request: requestOptions & {folderPath?: string}) {
|
||||
const folderToExtract = request.folderPath||path.join(os.tmpdir(), `raw_bdscore_${Date.now()}_${(path.parse(request.url||request.socket?.path||crypto.randomBytes(16).toString("hex"))).name}`);
|
||||
if (!await exists(folderToExtract)) await fs.promises.mkdir(folderToExtract, {recursive: true});
|
||||
await pipeFetch({
|
||||
...request,
|
||||
waitFinish: true,
|
||||
stream: tar.extract({
|
||||
cwd: folderToExtract,
|
||||
noChmod: false,
|
||||
noMtime: false,
|
||||
preserveOwner: true,
|
||||
keep: true,
|
||||
p: true
|
||||
})
|
||||
});
|
||||
return folderToExtract
|
||||
}
|
||||
|
||||
const githubAchive = /github.com\/[\S\w]+\/[\S\w]+\/archive\//;
|
||||
export async function extractZip(request: requestOptions & {folderTarget: string}) {
|
||||
const zip = new AdmZip(await saveFile(request));
|
||||
const targetFolder = githubAchive.test(request.url)?await fs.promises.mkdtemp(path.join(os.tmpdir(), "githubRoot_"), "utf8"):request.folderTarget;
|
||||
await new Promise<void>((done, reject) => {
|
||||
zip.extractAllToAsync(targetFolder, true, true, (err) => {
|
||||
if (!err) return done();
|
||||
return reject(err);
|
||||
})
|
||||
});
|
||||
|
||||
if (githubAchive.test(request.url)) {
|
||||
const files = await fs.promises.readdir(targetFolder);
|
||||
if (files.length === 0) throw new Error("Invalid extract");
|
||||
await fs.promises.cp(path.join(targetFolder, files[0]), request.folderTarget, {recursive: true, force: true, preserveTimestamps: true, verbatimSymlinks: true});
|
||||
return await fs.promises.rm(targetFolder, {recursive: true, force: true});
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
export type testIp<protocolType extends "ipv4"|"ipv6" = "ipv4"> = {
|
||||
ip: string,
|
||||
type: protocolType,
|
||||
subtype: string,
|
||||
via: string,
|
||||
padding: string,
|
||||
asn: string,
|
||||
asnlist: string,
|
||||
asn_name: string,
|
||||
country: string,
|
||||
protocol: "HTTP/2.0"|"HTTP/1.1"|"HTTP/1.0"
|
||||
};
|
||||
|
||||
export async function getExternalIP(): Promise<{ipv4: string, ipv6?: string, rawRequest?: {ipv4: testIp<"ipv4">, ipv6?: testIp<"ipv6">}}> {
|
||||
const ipv6: testIp<"ipv6"> = await getJSON("https://ipv6.lookup.test-ipv6.com/ip/").catch(() => undefined);
|
||||
const ipv4: testIp<"ipv4"> = await getJSON("https://ipv4.lookup.test-ipv6.com/ip/");
|
||||
return {
|
||||
ipv4: ipv4.ip,
|
||||
ipv6: ipv6?.ip,
|
||||
rawRequest: {ipv4, ipv6}
|
||||
};
|
||||
}
|
||||
|
||||
export type githubRelease = {
|
||||
url: string;
|
||||
assets_url: string;
|
||||
upload_url: string;
|
||||
html_url: string;
|
||||
id: number;
|
||||
tarball_url: string;
|
||||
zipball_url: string;
|
||||
body: string;
|
||||
author: {
|
||||
login: string;
|
||||
id: number;
|
||||
node_id: string;
|
||||
avatar_url: string;
|
||||
gravatar_id: string;
|
||||
url: string;
|
||||
html_url: string;
|
||||
followers_url: string;
|
||||
following_url: string;
|
||||
gists_url: string;
|
||||
starred_url: string;
|
||||
subscriptions_url: string;
|
||||
organizations_url: string;
|
||||
repos_url: string;
|
||||
events_url: string;
|
||||
received_events_url: string;
|
||||
type: string;
|
||||
site_admin: boolean;
|
||||
};
|
||||
node_id: string;
|
||||
tag_name: string;
|
||||
target_commitish: string;
|
||||
name: string;
|
||||
draft: boolean;
|
||||
prerelease: boolean;
|
||||
created_at: string;
|
||||
published_at: string;
|
||||
assets: Array<{
|
||||
url: string;
|
||||
id: number;
|
||||
node_id: string;
|
||||
name: string;
|
||||
label: string;
|
||||
content_type: string;
|
||||
state: string;
|
||||
size: number;
|
||||
download_count: number;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
browser_download_url: string;
|
||||
uploader: {
|
||||
login: string;
|
||||
id: number;
|
||||
node_id: string;
|
||||
avatar_url: string;
|
||||
gravatar_id: string;
|
||||
url: string;
|
||||
html_url: string;
|
||||
followers_url: string;
|
||||
following_url: string;
|
||||
gists_url: string;
|
||||
starred_url: string;
|
||||
subscriptions_url: string;
|
||||
organizations_url: string;
|
||||
repos_url: string;
|
||||
events_url: string;
|
||||
received_events_url: string;
|
||||
type: string;
|
||||
site_admin: boolean;
|
||||
};
|
||||
}>;
|
||||
};
|
||||
|
||||
export async function GithubRelease(username: string, repo: string, releaseTag: string): Promise<githubRelease>;
|
||||
export async function GithubRelease(username: string, repo: string): Promise<githubRelease[]>;
|
||||
export async function GithubRelease(username: string): Promise<githubRelease[]>;
|
||||
export async function GithubRelease(username: string, repo?: string, releaseTag?: string): Promise<githubRelease|githubRelease[]> {
|
||||
let fullRepo = username;
|
||||
if (!username) throw new Error("Repository is required, example: GithubRelease(\"Username/repo\") or GithubRelease(\"Username\", \"repo\")");
|
||||
if (repo) {
|
||||
if (!/\//.test(fullRepo)) fullRepo += "/"+repo;
|
||||
}
|
||||
if (releaseTag) return getJSON<githubRelease>(`https://api.github.com/repos/${fullRepo}/releases/tags/${releaseTag}`);
|
||||
return getJSON<githubRelease[]>(`https://api.github.com/repos/${fullRepo}/releases?per_page=100`);
|
||||
}
|
||||
|
||||
export type githubTree = {
|
||||
"sha": string,
|
||||
"url": string,
|
||||
"truncated": boolean,
|
||||
"tree": {
|
||||
"path": string,
|
||||
"mode": string,
|
||||
"type": "blob"|"tree",
|
||||
"sha": string,
|
||||
"size": number,
|
||||
"url": string
|
||||
}[],
|
||||
};
|
||||
export async function githubTree(username: string, repo: string, tree: string = "main") {
|
||||
const validate = /^[a-zA-Z0-9_\-]+$/;
|
||||
if (!validate.test(username)) throw new Error("Invalid username");
|
||||
if (!validate.test(repo)) throw new Error("Invalid repository name");
|
||||
return getJSON<githubTree>(`https://api.github.com/repos/${username}/${repo}/git/trees/${tree}?recursive=true`);
|
||||
}
|
@ -4,7 +4,7 @@ import {spigot} from "../db/spigot";
|
||||
export const urlRegex = /http[s]:\/\/.*/;
|
||||
async function Find() {
|
||||
const { document } = (new jsdom.JSDOM(await bufferFetch("https://getbukkit.org/download/spigot").then(({data}) => data.toString("utf8")).catch(err => {console.log(err); return "<html></html>"}))).window;
|
||||
const Versions = await Promise.all(([...document.querySelectorAll("#download > div > div > div > div")]).map(async DOM => {
|
||||
const Versions = await Promise.all(([...(document.querySelectorAll("#download > div > div > div > div") as any)]).map(async DOM => {
|
||||
const download = (new jsdom.JSDOM(await bufferFetch(DOM.querySelector("div > div.col-sm-4 > div.btn-group > a")["href"]).then(({data}) => data.toString("utf8"))));
|
||||
const serverInfo = {
|
||||
version: String(DOM.querySelector("div:nth-child(1) > h2").textContent),
|
||||
|
@ -5,7 +5,7 @@ import os from "node:os";
|
||||
import Proprieties from "./lib/Proprieties";
|
||||
import * as globalPlatfroms from "./globalPlatfroms";
|
||||
import { platformManeger } from "@the-bds-maneger/server_versions";
|
||||
import { saveFile } from "./lib/httpRequest";
|
||||
import { saveFile } from "@http/large";
|
||||
import { pathControl, bdsPlatformOptions } from "./platformPathManeger";
|
||||
import { spigotProprieties } from "./spigot";
|
||||
|
||||
@ -115,4 +115,4 @@ export async function updateConfig(config: {key: string, value: any}, platformOp
|
||||
const { serverPath } = await pathControl("paper", platformOptions);
|
||||
currentConfig[config.key] = config.value;
|
||||
return fs.writeFile(path.join(serverPath, "server.properties"), Proprieties.stringify(currentConfig));
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ import path from "node:path";
|
||||
import fs from "node:fs/promises";
|
||||
import { actionsV2 } from "../globalPlatfroms";
|
||||
import { execFileAsync } from "../lib/childPromisses";
|
||||
import { saveFile } from "../lib/httpRequest";
|
||||
import { saveFile } from "@http/large";
|
||||
|
||||
export type hooksPlatform = "bedrock"|"java"|"pocketmine"|"spigot"|"powernukkit"|"paper";
|
||||
export type hooksPlatformGeneric = hooksPlatform|"generic";
|
||||
@ -80,4 +80,4 @@ export class script_hook {
|
||||
this.#localFolder = hookFolder;
|
||||
if (platformActions) this.#loadLocalScript();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
import path from "node:path";
|
||||
import * as http_request from "../lib/httpRequest";
|
||||
import type { bdsPlatform } from "../platformPathManeger";
|
||||
import { getJSON } from "@http/simples";
|
||||
import { saveFile } from "@http/large";
|
||||
|
||||
export type pluginConfig = {
|
||||
version?: 1,
|
||||
@ -40,11 +41,11 @@ export class pluginManeger {
|
||||
const urlandbds = /http[s]:\/\/|bdsplugin:\/\//;
|
||||
if (/bdsplugin:\/\//.test(plugin)) plugin = `https://raw.githubusercontent.com/The-Bds-Maneger/plugin_list/main/plugins/${plugin.replace(urlandbds, "").replace(".json", "")}.json`;
|
||||
else if (!/http[s]:\/\/\//.test(plugin)) plugin = "bdsplugin://"+plugin;
|
||||
const info = await http_request.getJSON<pluginConfig|pluginV2>(plugin);
|
||||
const info = await getJSON<pluginConfig|pluginV2>(plugin);
|
||||
if (info.version === 2) {
|
||||
const platformData = info.platform[this.#platform];
|
||||
if (!platformData) throw new Error("Platform not supported to Plugin!");
|
||||
await http_request.saveFile({
|
||||
await saveFile({
|
||||
url: platformData.url,
|
||||
filePath: path.join(this.#pluginFolder, platformData.fileName||path.basename(platformData.url))
|
||||
});
|
||||
@ -52,10 +53,10 @@ export class pluginManeger {
|
||||
return;
|
||||
}
|
||||
if (!info.platforms.includes(this.#platform)) throw new Error("Platform not supported to Plugin!");
|
||||
await http_request.saveFile({
|
||||
await saveFile({
|
||||
url: info.url,
|
||||
filePath: path.join(this.#pluginFolder, info.fileName||path.basename(info.url))
|
||||
});
|
||||
if (info.dependecies) await Promise.all(info.dependecies.map(dep => this.installPlugin(dep)));
|
||||
}
|
||||
};
|
||||
};
|
||||
|
@ -7,8 +7,10 @@ import { existsSync as fsExistsSync, Stats } from "node:fs";
|
||||
import { promisify } from "node:util";
|
||||
import { platformManeger } from "@the-bds-maneger/server_versions";
|
||||
import { execFileAsync, execAsync } from "./lib/childPromisses";
|
||||
import * as httpRequest from "./lib/httpRequest";
|
||||
import { extractZip, saveFile, tarExtract } from "@http/large";
|
||||
import { bufferFetch } from "@http/simples";
|
||||
import { pathControl, bdsPlatformOptions } from "./platformPathManeger";
|
||||
import {GithubRelease, githubRelease} from "@http/github";
|
||||
|
||||
async function findPhp(serverPath: string, extraPath?: string): Promise<string> {
|
||||
if (!extraPath) extraPath = path.join(serverPath, "bin");
|
||||
@ -57,7 +59,7 @@ async function buildPhp(serverPath: string, buildFolder: string) {
|
||||
await promisify((new AdmZip(path.join(buildFolder, (await fs.readdir(buildFolder)).find(file => file.endsWith(".zip"))))).extractAllToAsync)(serverPath, false, true);
|
||||
} else {
|
||||
const buildFile = path.join(buildFolder, "build.sh");
|
||||
await fs.writeFile(buildFile, (await httpRequest.bufferFetch({url: "https://raw.githubusercontent.com/pmmp/php-build-scripts/stable/compile.sh"})).data);
|
||||
await fs.writeFile(buildFile, (await bufferFetch({url: "https://raw.githubusercontent.com/pmmp/php-build-scripts/stable/compile.sh"})).data);
|
||||
await fs.chmod(buildFile, "777");
|
||||
await execFileAsync(buildFile, ["-j"+os.cpus().length], {cwd: buildFolder, stdio: "inherit"});
|
||||
await fs.cp(path.join(buildFolder, "bin", (await fs.readdir(path.join(buildFolder, "bin")))[0]), path.join(serverPath, "bin"), {force: true, recursive: true, preserveTimestamps: true, verbatimSymlinks: true});
|
||||
@ -67,20 +69,20 @@ async function buildPhp(serverPath: string, buildFolder: string) {
|
||||
}
|
||||
|
||||
async function installPhp(serverPath: string, buildFolder: string): Promise<void> {
|
||||
const releases: (httpRequest.githubRelease["assets"][0])[] = [];
|
||||
(await httpRequest.GithubRelease("The-Bds-Maneger", "Build-PHP-Bins")).map(re => re.assets).forEach(res => releases.push(...res));
|
||||
const releases: (githubRelease["assets"][0])[] = [];
|
||||
(await GithubRelease("The-Bds-Maneger", "Build-PHP-Bins")).map(re => re.assets).forEach(res => releases.push(...res));
|
||||
if (fsExistsSync(path.resolve(serverPath, "bin"))) await fs.rm(path.resolve(serverPath, "bin"), {recursive: true});
|
||||
await fs.writeFile(path.join(os.tmpdir(), "bds_test.php"), `<?php echo "Hello World";`);
|
||||
if (process.platform === "win32") {
|
||||
let url: string = releases.filter(assert => assert.name.endsWith(".zip")).find(assert => /win32|windows/.test(assert.name))?.browser_download_url;
|
||||
if (!url) throw new Error("Cannnot get php url");
|
||||
return promisify((new AdmZip(await httpRequest.saveFile({url}))).extractAllToAsync)(serverPath, false, true);
|
||||
return promisify((new AdmZip(await saveFile({url}))).extractAllToAsync)(serverPath, false, true);
|
||||
} else {
|
||||
const fileTest = RegExp(`${process.platform.toLowerCase()}.*${process.arch.toLowerCase()}`);
|
||||
const file = releases.find(re => fileTest.test(re.name.toLowerCase()));
|
||||
if (file) {
|
||||
if (/\.zip/.test(file.name)) await httpRequest.extractZip({url: file.browser_download_url, folderTarget: serverPath});
|
||||
else await httpRequest.tarExtract({url: file.browser_download_url, folderPath: path.join(serverPath, "bin")});
|
||||
if (/\.zip/.test(file.name)) await extractZip({url: file.browser_download_url, folderTarget: serverPath});
|
||||
else await tarExtract({url: file.browser_download_url, folderPath: path.join(serverPath, "bin")});
|
||||
|
||||
// Update zts
|
||||
if (process.platform === "linux"||process.platform === "android"||process.platform === "darwin") {
|
||||
@ -107,7 +109,7 @@ export async function installServer(version: string|boolean, platformOptions: bd
|
||||
const { serverPath, buildFolder, id } = await pathControl("pocketmine", platformOptions);
|
||||
await installPhp(serverPath, buildFolder);
|
||||
const info = await platformManeger.pocketmine.find(version);
|
||||
await httpRequest.saveFile({url: info?.url, filePath: path.join(serverPath, "pocketmine.phar")});
|
||||
await saveFile({url: info?.url, filePath: path.join(serverPath, "pocketmine.phar")});
|
||||
return {
|
||||
id,
|
||||
version: info.version,
|
||||
@ -151,4 +153,4 @@ export async function startServer(platformOptions: bdsPlatformOptions = {id: "de
|
||||
processConfig: {command: await findPhp(serverPath), args: [serverPhar, "--no-wizard", "--enable-ansi"], options: {cwd: serverPath, maxBuffer: Infinity, logPath: {stdout: logFileOut}}},
|
||||
hooks: pocketmineHooks
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import * as globalPlatfroms from "./globalPlatfroms";
|
||||
import { platformManeger } from "@the-bds-maneger/server_versions";
|
||||
import { saveFile } from "./lib/httpRequest";
|
||||
import { saveFile } from "@http/large";
|
||||
import { pathControl, bdsPlatformOptions } from "./platformPathManeger";
|
||||
|
||||
export async function installServer(version: string|boolean, platformOptions: bdsPlatformOptions = {id: "default"}) {
|
||||
|
@ -3,7 +3,9 @@ import fs from "node:fs/promises";
|
||||
import fsOld from "node:fs";
|
||||
import os from "node:os";
|
||||
import * as globalPlatfroms from "./globalPlatfroms";
|
||||
import { bufferFetch, getJSON, GithubRelease, saveFile } from "./lib/httpRequest";
|
||||
import { bufferFetch, getJSON } from "@http/simples";
|
||||
import { saveFile } from "@http/large";
|
||||
import { GithubRelease } from "@http/github";
|
||||
import { pathControl, bdsPlatformOptions } from "./platformPathManeger";
|
||||
import Proprieties from "./lib/Proprieties"
|
||||
|
||||
@ -194,4 +196,4 @@ export async function updateConfig(config: {key: string, value: any}, platformOp
|
||||
const { serverPath } = await pathControl("spigot", platformOptions);
|
||||
currentConfig[config.key] = config.value;
|
||||
return fs.writeFile(path.join(serverPath, "server.properties"), Proprieties.stringify(currentConfig));
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,16 @@
|
||||
import stream from "node:stream";
|
||||
import { getExternalIP, getJSON, saveFile, pipeFetch, githubTree, GithubRelease } from "../../src/lib/httpRequest";
|
||||
import { getJSON, pipeFetch } from "../../src/lib/request/simples";
|
||||
import { saveFile } from "../../src/lib/request/large";
|
||||
import { GithubRelease, githubTree } from "../../src/lib/request/github";
|
||||
import { getExternalIP } from "../../src/lib/request/client";
|
||||
|
||||
describe("HTTP Request", function(){
|
||||
this.timeout(Infinity);
|
||||
const Stream = new stream.Writable({write(chunk, encoding, callback) {callback()}});
|
||||
const Stream = new stream.Writable({write(_chunk, _encoding, callback) {callback()}});
|
||||
it("External IP", async () => getExternalIP());
|
||||
it("Github Releases", async () => GithubRelease("The-Bds-Maneger", "Bds-Maneger-Core"));
|
||||
it("Github Tree", async () => githubTree("The-Bds-Maneger", "Bds-Maneger-Core"));
|
||||
it("JSON", async () => getJSON("https://raw.githubusercontent.com/The-Bds-Maneger/Bds-Maneger-Core/main/package.json"));
|
||||
it("Stream", async () => pipeFetch({stream: Stream, url: "https://raw.githubusercontent.com/The-Bds-Maneger/Bds-Maneger-Core/main/package.json"}));
|
||||
it("Save File", async () => saveFile("https://raw.githubusercontent.com/The-Bds-Maneger/Bds-Maneger-Core/main/package.json"));
|
||||
});
|
||||
});
|
||||
|
@ -15,15 +15,11 @@
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"@http/*": [
|
||||
"./src/lib/request/*"
|
||||
"src/lib/request/*"
|
||||
]
|
||||
}
|
||||
},
|
||||
"include": [
|
||||
"src/**/*"
|
||||
],
|
||||
"exclude": [
|
||||
"node_modules/",
|
||||
"tests/"
|
||||
]
|
||||
}
|
||||
|
Reference in New Issue
Block a user