@caido-utils/backend 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/process.d.ts +2 -0
- package/dist/process.js +58 -0
- package/dist/project.d.ts +2 -0
- package/dist/project.js +8 -0
- package/dist/storage.d.ts +9 -0
- package/dist/storage.js +45 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
1
|
export { Fetch, type FetchOptions } from "./fetcher";
|
|
2
2
|
export { createSignal, pausePool, resumePool, runPool, stopPool } from "./pool";
|
|
3
3
|
export type { PoolCallbacks, PoolConfig, PoolSignal } from "./pool";
|
|
4
|
+
export { spawnCommand } from "./process";
|
|
5
|
+
export { getProjectId } from "./project";
|
|
6
|
+
export { fileExists, readFile, storeFile } from "./storage";
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,5 @@
|
|
|
1
1
|
export { Fetch } from "./fetcher.js";
|
|
2
2
|
export { createSignal, pausePool, resumePool, runPool, stopPool } from "./pool.js";
|
|
3
|
+
export { spawnCommand } from "./process.js";
|
|
4
|
+
export { getProjectId } from "./project.js";
|
|
5
|
+
export { fileExists, readFile, storeFile } from "./storage.js";
|
package/dist/process.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {
|
|
2
|
+
spawn as nodeSpawn
|
|
3
|
+
} from "child_process";
|
|
4
|
+
import { access, constants } from "fs/promises";
|
|
5
|
+
import * as path from "path";
|
|
6
|
+
async function ensureBinaryExecutable(binaryPath, sdk) {
|
|
7
|
+
try {
|
|
8
|
+
await access(binaryPath, constants.F_OK | constants.X_OK);
|
|
9
|
+
} catch {
|
|
10
|
+
try {
|
|
11
|
+
await makeExecutable(binaryPath);
|
|
12
|
+
} catch (chmodError) {
|
|
13
|
+
sdk.console.error(
|
|
14
|
+
`Failed to make ${binaryPath} executable: ${chmodError}`
|
|
15
|
+
);
|
|
16
|
+
throw new Error(`Cannot make binary executable: ${chmodError}`);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
async function makeExecutable(binaryPath) {
|
|
21
|
+
const child = nodeSpawn("chmod", ["+x", binaryPath], {
|
|
22
|
+
shell: true
|
|
23
|
+
});
|
|
24
|
+
await driveChild(child);
|
|
25
|
+
}
|
|
26
|
+
export async function spawnCommand(sdk, binary, args, stdin) {
|
|
27
|
+
try {
|
|
28
|
+
const binaryPath = path.join(sdk.meta.assetsPath(), binary);
|
|
29
|
+
await ensureBinaryExecutable(binaryPath, sdk);
|
|
30
|
+
const child = nodeSpawn(binaryPath, args);
|
|
31
|
+
if (stdin !== void 0) {
|
|
32
|
+
child.stdin.write(stdin);
|
|
33
|
+
child.stdin.end();
|
|
34
|
+
}
|
|
35
|
+
const output = await driveChild(child);
|
|
36
|
+
return output;
|
|
37
|
+
} catch (error) {
|
|
38
|
+
sdk.console.error(`Spawn command error (${binary}): ${error}`);
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
async function driveChild(child, allowExitCode1 = false) {
|
|
43
|
+
let output = "";
|
|
44
|
+
child.stdout.on("data", (data) => {
|
|
45
|
+
output += data.toString();
|
|
46
|
+
});
|
|
47
|
+
let error = "";
|
|
48
|
+
child.stderr.on("data", (data) => {
|
|
49
|
+
error += data.toString();
|
|
50
|
+
});
|
|
51
|
+
const exitCode = await new Promise((resolve) => {
|
|
52
|
+
child.on("close", resolve);
|
|
53
|
+
});
|
|
54
|
+
if (exitCode !== void 0 && exitCode !== 0 && !(allowExitCode1 && exitCode === 1)) {
|
|
55
|
+
throw new Error(`subprocess error exit ${exitCode}, ${error}`);
|
|
56
|
+
}
|
|
57
|
+
return output;
|
|
58
|
+
}
|
package/dist/project.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { SDK } from "caido:plugin";
|
|
2
|
+
type StoreFileOptions = {
|
|
3
|
+
data: string;
|
|
4
|
+
contentType?: string;
|
|
5
|
+
};
|
|
6
|
+
export declare function storeFile(sdk: SDK, options: StoreFileOptions): Promise<string>;
|
|
7
|
+
export declare function readFile(fsPath: string): Promise<string>;
|
|
8
|
+
export declare function fileExists(fsPath: string): Promise<boolean>;
|
|
9
|
+
export {};
|
package/dist/storage.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import * as fs from "fs/promises";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
const extensionMap = {
|
|
4
|
+
"application/javascript": ".js",
|
|
5
|
+
"application/x-javascript": ".js",
|
|
6
|
+
"text/javascript": ".js",
|
|
7
|
+
"text/html": ".html",
|
|
8
|
+
"application/json": ".json",
|
|
9
|
+
"application/xml": ".xml",
|
|
10
|
+
"text/xml": ".xml",
|
|
11
|
+
"text/css": ".css",
|
|
12
|
+
"text/plain": ".txt",
|
|
13
|
+
"application/typescript": ".ts"
|
|
14
|
+
};
|
|
15
|
+
function getExtensionFromContentType(contentType) {
|
|
16
|
+
if (contentType === void 0) return "";
|
|
17
|
+
return extensionMap[contentType.toLowerCase()] ?? "";
|
|
18
|
+
}
|
|
19
|
+
function generateFileName(contentType) {
|
|
20
|
+
const randomName = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
21
|
+
const extension = getExtensionFromContentType(contentType);
|
|
22
|
+
return randomName + extension;
|
|
23
|
+
}
|
|
24
|
+
export async function storeFile(sdk, options) {
|
|
25
|
+
const { data, contentType } = options;
|
|
26
|
+
const dataDir = sdk.meta.path();
|
|
27
|
+
const filesDir = path.join(dataDir, "files");
|
|
28
|
+
await fs.mkdir(filesDir, { recursive: true });
|
|
29
|
+
const fileName = generateFileName(contentType);
|
|
30
|
+
const filePath = path.join(filesDir, fileName);
|
|
31
|
+
await fs.writeFile(filePath, data);
|
|
32
|
+
return filePath;
|
|
33
|
+
}
|
|
34
|
+
export async function readFile(fsPath) {
|
|
35
|
+
const content = await fs.readFile(fsPath);
|
|
36
|
+
return content.toString();
|
|
37
|
+
}
|
|
38
|
+
export async function fileExists(fsPath) {
|
|
39
|
+
try {
|
|
40
|
+
await fs.access(fsPath);
|
|
41
|
+
return true;
|
|
42
|
+
} catch {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
}
|