chore: format backend server code
This commit is contained in:
parent
1416c225a2
commit
ad9457b157
@ -1,5 +1,7 @@
|
||||
{
|
||||
"watch": ["src"],
|
||||
"watch": [
|
||||
"src"
|
||||
],
|
||||
"ext": "ts",
|
||||
"exec": "concurrently \"npx tsc --watch\" \"ts-node src/index.ts\""
|
||||
}
|
@ -1,37 +1,33 @@
|
||||
import { SSHSocketClient, SSHConfig } from "./SSHSocketClient"
|
||||
import { SSHConfig, SSHSocketClient } from "./SSHSocketClient"
|
||||
|
||||
export interface DokkuResponse {
|
||||
ok: boolean;
|
||||
output: string;
|
||||
ok: boolean
|
||||
output: string
|
||||
}
|
||||
|
||||
export class DokkuClient extends SSHSocketClient {
|
||||
|
||||
constructor(config: SSHConfig) {
|
||||
super(
|
||||
config,
|
||||
"/var/run/dokku-daemon/dokku-daemon.sock"
|
||||
)
|
||||
super(config, "/var/run/dokku-daemon/dokku-daemon.sock")
|
||||
}
|
||||
|
||||
async sendCommand(command: string): Promise<DokkuResponse> {
|
||||
try {
|
||||
const response = await this.sendData(command);
|
||||
const response = await this.sendData(command)
|
||||
|
||||
if (typeof response !== "string") {
|
||||
throw new Error("Received data is not a string");
|
||||
throw new Error("Received data is not a string")
|
||||
}
|
||||
|
||||
return JSON.parse(response);
|
||||
return JSON.parse(response)
|
||||
} catch (error: any) {
|
||||
throw new Error(`Failed to send command: ${error.message}`);
|
||||
throw new Error(`Failed to send command: ${error.message}`)
|
||||
}
|
||||
}
|
||||
|
||||
async listApps(): Promise<string[]> {
|
||||
const response = await this.sendCommand("apps:list");
|
||||
return response.output.split("\n").slice(1); // Split by newline and ignore the first line (header)
|
||||
const response = await this.sendCommand("apps:list")
|
||||
return response.output.split("\n").slice(1) // Split by newline and ignore the first line (header)
|
||||
}
|
||||
}
|
||||
|
||||
export { SSHConfig };
|
||||
export { SSHConfig }
|
||||
|
@ -1,72 +1,72 @@
|
||||
import { Client } from "ssh2";
|
||||
import { Client } from "ssh2"
|
||||
|
||||
export interface SSHConfig {
|
||||
host: string;
|
||||
port?: number;
|
||||
username: string;
|
||||
privateKey: Buffer;
|
||||
host: string
|
||||
port?: number
|
||||
username: string
|
||||
privateKey: Buffer
|
||||
}
|
||||
|
||||
export class SSHSocketClient {
|
||||
private conn: Client;
|
||||
private config: SSHConfig;
|
||||
private socketPath: string;
|
||||
private isConnected: boolean = false;
|
||||
private conn: Client
|
||||
private config: SSHConfig
|
||||
private socketPath: string
|
||||
private isConnected: boolean = false
|
||||
|
||||
constructor(config: SSHConfig, socketPath: string) {
|
||||
this.conn = new Client();
|
||||
this.config = { ...config, port: 22};
|
||||
this.socketPath = socketPath;
|
||||
this.conn = new Client()
|
||||
this.config = { ...config, port: 22 }
|
||||
this.socketPath = socketPath
|
||||
|
||||
this.setupTerminationHandlers();
|
||||
this.setupTerminationHandlers()
|
||||
}
|
||||
|
||||
private setupTerminationHandlers() {
|
||||
process.on("SIGINT", this.closeConnection.bind(this));
|
||||
process.on("SIGTERM", this.closeConnection.bind(this));
|
||||
process.on("SIGINT", this.closeConnection.bind(this))
|
||||
process.on("SIGTERM", this.closeConnection.bind(this))
|
||||
}
|
||||
|
||||
private closeConnection() {
|
||||
console.log("Closing SSH connection...");
|
||||
this.conn.end();
|
||||
this.isConnected = false;
|
||||
process.exit(0);
|
||||
console.log("Closing SSH connection...")
|
||||
this.conn.end()
|
||||
this.isConnected = false
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
connect(): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.conn
|
||||
.on("ready", () => {
|
||||
console.log("SSH connection established");
|
||||
this.isConnected = true;
|
||||
resolve();
|
||||
console.log("SSH connection established")
|
||||
this.isConnected = true
|
||||
resolve()
|
||||
})
|
||||
.on("error", (err) => {
|
||||
console.error("SSH connection error:", err);
|
||||
this.isConnected = false;
|
||||
reject(err);
|
||||
console.error("SSH connection error:", err)
|
||||
this.isConnected = false
|
||||
reject(err)
|
||||
})
|
||||
.on("close", () => {
|
||||
console.log("SSH connection closed");
|
||||
this.isConnected = false;
|
||||
console.log("SSH connection closed")
|
||||
this.isConnected = false
|
||||
})
|
||||
.connect(this.config)
|
||||
})
|
||||
.connect(this.config);
|
||||
});
|
||||
}
|
||||
|
||||
sendData(data: string): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!this.isConnected) {
|
||||
reject(new Error("SSH connection is not established"));
|
||||
return;
|
||||
reject(new Error("SSH connection is not established"))
|
||||
return
|
||||
}
|
||||
|
||||
this.conn.exec(
|
||||
`echo "${data}" | nc -U ${this.socketPath}`,
|
||||
(err, stream) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
return;
|
||||
reject(err)
|
||||
return
|
||||
}
|
||||
|
||||
stream
|
||||
@ -75,16 +75,16 @@ export class SSHSocketClient {
|
||||
new Error(
|
||||
`Stream closed with code ${code} and signal ${signal}`
|
||||
)
|
||||
);
|
||||
)
|
||||
})
|
||||
.on("data", (data: Buffer) => {
|
||||
resolve(data.toString());
|
||||
resolve(data.toString())
|
||||
})
|
||||
.stderr.on("data", (data: Buffer) => {
|
||||
reject(new Error(data.toString()));
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
reject(new Error(data.toString()))
|
||||
})
|
||||
}
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -1,82 +1,84 @@
|
||||
import simpleGit, { SimpleGit } from "simple-git";
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
import os from "os";
|
||||
import fs from "fs"
|
||||
import os from "os"
|
||||
import path from "path"
|
||||
import simpleGit, { SimpleGit } from "simple-git"
|
||||
|
||||
export type FileData = {
|
||||
id: string;
|
||||
data: string;
|
||||
};
|
||||
id: string
|
||||
data: string
|
||||
}
|
||||
|
||||
export class SecureGitClient {
|
||||
private gitUrl: string;
|
||||
private sshKeyPath: string;
|
||||
private gitUrl: string
|
||||
private sshKeyPath: string
|
||||
|
||||
constructor(gitUrl: string, sshKeyPath: string) {
|
||||
this.gitUrl = gitUrl;
|
||||
this.sshKeyPath = sshKeyPath;
|
||||
this.gitUrl = gitUrl
|
||||
this.sshKeyPath = sshKeyPath
|
||||
}
|
||||
|
||||
async pushFiles(fileData: FileData[], repository: string): Promise<void> {
|
||||
let tempDir: string | undefined;
|
||||
let tempDir: string | undefined
|
||||
|
||||
try {
|
||||
// Create a temporary directory
|
||||
tempDir = fs.mkdtempSync(path.posix.join(os.tmpdir(), 'git-push-'));
|
||||
console.log(`Temporary directory created: ${tempDir}`);
|
||||
tempDir = fs.mkdtempSync(path.posix.join(os.tmpdir(), "git-push-"))
|
||||
console.log(`Temporary directory created: ${tempDir}`)
|
||||
|
||||
// Write files to the temporary directory
|
||||
console.log(`Writing ${fileData.length} files.`);
|
||||
console.log(`Writing ${fileData.length} files.`)
|
||||
for (const { id, data } of fileData) {
|
||||
const filePath = path.posix.join(tempDir, id);
|
||||
const dirPath = path.dirname(filePath);
|
||||
const filePath = path.posix.join(tempDir, id)
|
||||
const dirPath = path.dirname(filePath)
|
||||
|
||||
if (!fs.existsSync(dirPath)) {
|
||||
fs.mkdirSync(dirPath, { recursive: true });
|
||||
fs.mkdirSync(dirPath, { recursive: true })
|
||||
}
|
||||
fs.writeFileSync(filePath, data);
|
||||
fs.writeFileSync(filePath, data)
|
||||
}
|
||||
|
||||
// Initialize the simple-git instance with the temporary directory and custom SSH command
|
||||
const git: SimpleGit = simpleGit(tempDir, {
|
||||
config: [
|
||||
'core.sshCommand=ssh -i ' + this.sshKeyPath + ' -o IdentitiesOnly=yes'
|
||||
]
|
||||
"core.sshCommand=ssh -i " +
|
||||
this.sshKeyPath +
|
||||
" -o IdentitiesOnly=yes",
|
||||
],
|
||||
}).outputHandler((_command, stdout, stderr) => {
|
||||
stdout.pipe(process.stdout);
|
||||
stderr.pipe(process.stderr);
|
||||
});;
|
||||
stdout.pipe(process.stdout)
|
||||
stderr.pipe(process.stderr)
|
||||
})
|
||||
|
||||
// Initialize a new Git repository
|
||||
await git.init();
|
||||
await git.init()
|
||||
|
||||
// Add remote repository
|
||||
await git.addRemote("origin", `${this.gitUrl}:${repository}`);
|
||||
await git.addRemote("origin", `${this.gitUrl}:${repository}`)
|
||||
|
||||
// Add files to the repository
|
||||
for (const {id, data} of fileData) {
|
||||
await git.add(id);
|
||||
for (const { id, data } of fileData) {
|
||||
await git.add(id)
|
||||
}
|
||||
|
||||
// Commit the changes
|
||||
await git.commit("Add files.");
|
||||
await git.commit("Add files.")
|
||||
|
||||
// Push the changes to the remote repository
|
||||
await git.push("origin", "master", {'--force': null});
|
||||
await git.push("origin", "master", { "--force": null })
|
||||
|
||||
console.log("Files successfully pushed to the repository");
|
||||
console.log("Files successfully pushed to the repository")
|
||||
|
||||
if (tempDir) {
|
||||
fs.rmSync(tempDir, { recursive: true, force: true });
|
||||
console.log(`Temporary directory removed: ${tempDir}`);
|
||||
fs.rmSync(tempDir, { recursive: true, force: true })
|
||||
console.log(`Temporary directory removed: ${tempDir}`)
|
||||
}
|
||||
} catch (error) {
|
||||
if (tempDir) {
|
||||
fs.rmSync(tempDir, { recursive: true, force: true });
|
||||
console.log(`Temporary directory removed: ${tempDir}`);
|
||||
fs.rmSync(tempDir, { recursive: true, force: true })
|
||||
console.log(`Temporary directory removed: ${tempDir}`)
|
||||
}
|
||||
console.error("Error pushing files to the repository:", error);
|
||||
throw error;
|
||||
console.error("Error pushing files to the repository:", error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
@ -1,13 +1,13 @@
|
||||
import { Sandbox, ProcessHandle } from "e2b";
|
||||
import { ProcessHandle, Sandbox } from "e2b"
|
||||
|
||||
// Terminal class to manage a pseudo-terminal (PTY) in a sandbox environment
|
||||
export class Terminal {
|
||||
private pty: ProcessHandle | undefined; // Holds the PTY process handle
|
||||
private sandbox: Sandbox; // Reference to the sandbox environment
|
||||
private pty: ProcessHandle | undefined // Holds the PTY process handle
|
||||
private sandbox: Sandbox // Reference to the sandbox environment
|
||||
|
||||
// Constructor initializes the Terminal with a sandbox
|
||||
constructor(sandbox: Sandbox) {
|
||||
this.sandbox = sandbox;
|
||||
this.sandbox = sandbox
|
||||
}
|
||||
|
||||
// Initialize the terminal with specified rows, columns, and data handler
|
||||
@ -16,9 +16,9 @@ export class Terminal {
|
||||
cols = 80,
|
||||
onData,
|
||||
}: {
|
||||
rows?: number;
|
||||
cols?: number;
|
||||
onData: (responseData: string) => void;
|
||||
rows?: number
|
||||
cols?: number
|
||||
onData: (responseData: string) => void
|
||||
}): Promise<void> {
|
||||
// Create a new PTY process
|
||||
this.pty = await this.sandbox.pty.create({
|
||||
@ -26,35 +26,38 @@ export class Terminal {
|
||||
cols,
|
||||
timeout: 0,
|
||||
onData: (data: Uint8Array) => {
|
||||
onData(new TextDecoder().decode(data)); // Convert received data to string and pass to handler
|
||||
onData(new TextDecoder().decode(data)) // Convert received data to string and pass to handler
|
||||
},
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
// Send data to the terminal
|
||||
async sendData(data: string) {
|
||||
if (this.pty) {
|
||||
await this.sandbox.pty.sendInput(this.pty.pid, new TextEncoder().encode(data));
|
||||
await this.sandbox.pty.sendInput(
|
||||
this.pty.pid,
|
||||
new TextEncoder().encode(data)
|
||||
)
|
||||
} else {
|
||||
console.log("Cannot send data because pty is not initialized.");
|
||||
console.log("Cannot send data because pty is not initialized.")
|
||||
}
|
||||
}
|
||||
|
||||
// Resize the terminal
|
||||
async resize(size: { cols: number; rows: number }): Promise<void> {
|
||||
if (this.pty) {
|
||||
await this.sandbox.pty.resize(this.pty.pid, size);
|
||||
await this.sandbox.pty.resize(this.pty.pid, size)
|
||||
} else {
|
||||
console.log("Cannot resize terminal because pty is not initialized.");
|
||||
console.log("Cannot resize terminal because pty is not initialized.")
|
||||
}
|
||||
}
|
||||
|
||||
// Close the terminal, killing the PTY process and stopping the input stream
|
||||
async close(): Promise<void> {
|
||||
if (this.pty) {
|
||||
await this.pty.kill();
|
||||
await this.pty.kill()
|
||||
} else {
|
||||
console.log("Cannot kill pty because it is not initialized.");
|
||||
console.log("Cannot kill pty because it is not initialized.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +1,7 @@
|
||||
import * as dotenv from "dotenv";
|
||||
import {
|
||||
R2FileBody,
|
||||
R2Files,
|
||||
Sandbox,
|
||||
TFile,
|
||||
TFileData,
|
||||
TFolder,
|
||||
} from "./types";
|
||||
import * as dotenv from "dotenv"
|
||||
import { R2Files, TFile, TFileData, TFolder } from "./types"
|
||||
|
||||
dotenv.config();
|
||||
dotenv.config()
|
||||
|
||||
export const getSandboxFiles = async (id: string) => {
|
||||
const res = await fetch(
|
||||
@ -18,13 +11,13 @@ export const getSandboxFiles = async (id: string) => {
|
||||
Authorization: `${process.env.WORKERS_KEY}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
const data: R2Files = await res.json();
|
||||
)
|
||||
const data: R2Files = await res.json()
|
||||
|
||||
const paths = data.objects.map((obj) => obj.key);
|
||||
const processedFiles = await processFiles(paths, id);
|
||||
return processedFiles;
|
||||
};
|
||||
const paths = data.objects.map((obj) => obj.key)
|
||||
const processedFiles = await processFiles(paths, id)
|
||||
return processedFiles
|
||||
}
|
||||
|
||||
export const getFolder = async (folderId: string) => {
|
||||
const res = await fetch(
|
||||
@ -34,39 +27,39 @@ export const getFolder = async (folderId: string) => {
|
||||
Authorization: `${process.env.WORKERS_KEY}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
const data: R2Files = await res.json();
|
||||
)
|
||||
const data: R2Files = await res.json()
|
||||
|
||||
return data.objects.map((obj) => obj.key);
|
||||
};
|
||||
return data.objects.map((obj) => obj.key)
|
||||
}
|
||||
|
||||
const processFiles = async (paths: string[], id: string) => {
|
||||
const root: TFolder = { id: "/", type: "folder", name: "/", children: [] };
|
||||
const fileData: TFileData[] = [];
|
||||
const root: TFolder = { id: "/", type: "folder", name: "/", children: [] }
|
||||
const fileData: TFileData[] = []
|
||||
|
||||
paths.forEach((path) => {
|
||||
const allParts = path.split("/");
|
||||
const allParts = path.split("/")
|
||||
if (allParts[1] !== id) {
|
||||
return;
|
||||
return
|
||||
}
|
||||
|
||||
const parts = allParts.slice(2);
|
||||
let current: TFolder = root;
|
||||
const parts = allParts.slice(2)
|
||||
let current: TFolder = root
|
||||
|
||||
for (let i = 0; i < parts.length; i++) {
|
||||
const part = parts[i];
|
||||
const isFile = i === parts.length - 1 && part.length;
|
||||
const existing = current.children.find((child) => child.name === part);
|
||||
const part = parts[i]
|
||||
const isFile = i === parts.length - 1 && part.length
|
||||
const existing = current.children.find((child) => child.name === part)
|
||||
|
||||
if (existing) {
|
||||
if (!isFile) {
|
||||
current = existing as TFolder;
|
||||
current = existing as TFolder
|
||||
}
|
||||
} else {
|
||||
if (isFile) {
|
||||
const file: TFile = { id: path, type: "file", name: part };
|
||||
current.children.push(file);
|
||||
fileData.push({ id: path, data: "" });
|
||||
const file: TFile = { id: path, type: "file", name: part }
|
||||
current.children.push(file)
|
||||
fileData.push({ id: path, data: "" })
|
||||
} else {
|
||||
const folder: TFolder = {
|
||||
// id: path, // todo: wrong id. for example, folder "src" ID is: projects/a7vgttfqbgy403ratp7du3ln/src/App.css
|
||||
@ -74,26 +67,26 @@ const processFiles = async (paths: string[], id: string) => {
|
||||
type: "folder",
|
||||
name: part,
|
||||
children: [],
|
||||
};
|
||||
current.children.push(folder);
|
||||
current = folder;
|
||||
}
|
||||
current.children.push(folder)
|
||||
current = folder
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
await Promise.all(
|
||||
fileData.map(async (file) => {
|
||||
const data = await fetchFileContent(file.id);
|
||||
file.data = data;
|
||||
const data = await fetchFileContent(file.id)
|
||||
file.data = data
|
||||
})
|
||||
);
|
||||
)
|
||||
|
||||
return {
|
||||
files: root.children,
|
||||
fileData,
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const fetchFileContent = async (fileId: string): Promise<string> => {
|
||||
try {
|
||||
@ -104,13 +97,13 @@ const fetchFileContent = async (fileId: string): Promise<string> => {
|
||||
Authorization: `${process.env.WORKERS_KEY}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
return await fileRes.text();
|
||||
)
|
||||
return await fileRes.text()
|
||||
} catch (error) {
|
||||
console.error("ERROR fetching file:", error);
|
||||
return "";
|
||||
console.error("ERROR fetching file:", error)
|
||||
return ""
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export const createFile = async (fileId: string) => {
|
||||
const res = await fetch(`${process.env.STORAGE_WORKER_URL}/api`, {
|
||||
@ -120,9 +113,9 @@ export const createFile = async (fileId: string) => {
|
||||
Authorization: `${process.env.WORKERS_KEY}`,
|
||||
},
|
||||
body: JSON.stringify({ fileId }),
|
||||
});
|
||||
return res.ok;
|
||||
};
|
||||
})
|
||||
return res.ok
|
||||
}
|
||||
|
||||
export const renameFile = async (
|
||||
fileId: string,
|
||||
@ -136,9 +129,9 @@ export const renameFile = async (
|
||||
Authorization: `${process.env.WORKERS_KEY}`,
|
||||
},
|
||||
body: JSON.stringify({ fileId, newFileId, data }),
|
||||
});
|
||||
return res.ok;
|
||||
};
|
||||
})
|
||||
return res.ok
|
||||
}
|
||||
|
||||
export const saveFile = async (fileId: string, data: string) => {
|
||||
const res = await fetch(`${process.env.STORAGE_WORKER_URL}/api/save`, {
|
||||
@ -148,9 +141,9 @@ export const saveFile = async (fileId: string, data: string) => {
|
||||
Authorization: `${process.env.WORKERS_KEY}`,
|
||||
},
|
||||
body: JSON.stringify({ fileId, data }),
|
||||
});
|
||||
return res.ok;
|
||||
};
|
||||
})
|
||||
return res.ok
|
||||
}
|
||||
|
||||
export const deleteFile = async (fileId: string) => {
|
||||
const res = await fetch(`${process.env.STORAGE_WORKER_URL}/api`, {
|
||||
@ -160,9 +153,9 @@ export const deleteFile = async (fileId: string) => {
|
||||
Authorization: `${process.env.WORKERS_KEY}`,
|
||||
},
|
||||
body: JSON.stringify({ fileId }),
|
||||
});
|
||||
return res.ok;
|
||||
};
|
||||
})
|
||||
return res.ok
|
||||
}
|
||||
|
||||
export const getProjectSize = async (id: string) => {
|
||||
const res = await fetch(
|
||||
@ -172,6 +165,6 @@ export const getProjectSize = async (id: string) => {
|
||||
Authorization: `${process.env.WORKERS_KEY}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
return (await res.json()).size;
|
||||
};
|
||||
)
|
||||
return (await res.json()).size
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,70 +1,70 @@
|
||||
// DB Types
|
||||
|
||||
export type User = {
|
||||
id: string;
|
||||
name: string;
|
||||
email: string;
|
||||
generations: number;
|
||||
sandbox: Sandbox[];
|
||||
usersToSandboxes: UsersToSandboxes[];
|
||||
};
|
||||
id: string
|
||||
name: string
|
||||
email: string
|
||||
generations: number
|
||||
sandbox: Sandbox[]
|
||||
usersToSandboxes: UsersToSandboxes[]
|
||||
}
|
||||
|
||||
export type Sandbox = {
|
||||
id: string;
|
||||
name: string;
|
||||
type: "react" | "node";
|
||||
visibility: "public" | "private";
|
||||
createdAt: Date;
|
||||
userId: string;
|
||||
usersToSandboxes: UsersToSandboxes[];
|
||||
};
|
||||
id: string
|
||||
name: string
|
||||
type: "react" | "node"
|
||||
visibility: "public" | "private"
|
||||
createdAt: Date
|
||||
userId: string
|
||||
usersToSandboxes: UsersToSandboxes[]
|
||||
}
|
||||
|
||||
export type UsersToSandboxes = {
|
||||
userId: string;
|
||||
sandboxId: string;
|
||||
sharedOn: Date;
|
||||
};
|
||||
userId: string
|
||||
sandboxId: string
|
||||
sharedOn: Date
|
||||
}
|
||||
|
||||
export type TFolder = {
|
||||
id: string;
|
||||
type: "folder";
|
||||
name: string;
|
||||
children: (TFile | TFolder)[];
|
||||
};
|
||||
id: string
|
||||
type: "folder"
|
||||
name: string
|
||||
children: (TFile | TFolder)[]
|
||||
}
|
||||
|
||||
export type TFile = {
|
||||
id: string;
|
||||
type: "file";
|
||||
name: string;
|
||||
};
|
||||
id: string
|
||||
type: "file"
|
||||
name: string
|
||||
}
|
||||
|
||||
export type TFileData = {
|
||||
id: string;
|
||||
data: string;
|
||||
};
|
||||
id: string
|
||||
data: string
|
||||
}
|
||||
|
||||
export type R2Files = {
|
||||
objects: R2FileData[];
|
||||
truncated: boolean;
|
||||
delimitedPrefixes: any[];
|
||||
};
|
||||
objects: R2FileData[]
|
||||
truncated: boolean
|
||||
delimitedPrefixes: any[]
|
||||
}
|
||||
|
||||
export type R2FileData = {
|
||||
storageClass: string;
|
||||
uploaded: string;
|
||||
checksums: any;
|
||||
httpEtag: string;
|
||||
etag: string;
|
||||
size: number;
|
||||
version: string;
|
||||
key: string;
|
||||
};
|
||||
storageClass: string
|
||||
uploaded: string
|
||||
checksums: any
|
||||
httpEtag: string
|
||||
etag: string
|
||||
size: number
|
||||
version: string
|
||||
key: string
|
||||
}
|
||||
|
||||
export type R2FileBody = R2FileData & {
|
||||
body: ReadableStream;
|
||||
bodyUsed: boolean;
|
||||
arrayBuffer: Promise<ArrayBuffer>;
|
||||
text: Promise<string>;
|
||||
json: Promise<any>;
|
||||
blob: Promise<Blob>;
|
||||
};
|
||||
body: ReadableStream
|
||||
bodyUsed: boolean
|
||||
arrayBuffer: Promise<ArrayBuffer>
|
||||
text: Promise<string>
|
||||
json: Promise<any>
|
||||
blob: Promise<Blob>
|
||||
}
|
||||
|
@ -1,23 +1,23 @@
|
||||
export class LockManager {
|
||||
private locks: { [key: string]: Promise<any> };
|
||||
private locks: { [key: string]: Promise<any> }
|
||||
|
||||
constructor() {
|
||||
this.locks = {};
|
||||
this.locks = {}
|
||||
}
|
||||
|
||||
async acquireLock<T>(key: string, task: () => Promise<T>): Promise<T> {
|
||||
if (!this.locks[key]) {
|
||||
this.locks[key] = new Promise<T>(async (resolve, reject) => {
|
||||
try {
|
||||
const result = await task();
|
||||
resolve(result);
|
||||
const result = await task()
|
||||
resolve(result)
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
reject(error)
|
||||
} finally {
|
||||
delete this.locks[key];
|
||||
delete this.locks[key]
|
||||
}
|
||||
});
|
||||
})
|
||||
}
|
||||
return await this.locks[key];
|
||||
return await this.locks[key]
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user