Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,7 @@
"test": "vscode-test"
},
"dependencies": {
"@discloudapp/ws": "^0.1.0",
"@vscode/l10n": "^0.0.18",
"adm-zip": "^0.5.16",
"bytes": "^3.1.2",
Expand Down
34 changes: 23 additions & 11 deletions src/services/discloud/socket/actions/commit.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SocketClient, type SocketEventUploadData } from "@discloudapp/ws";
import { t } from "@vscode/l10n";
import bytes from "bytes";
import { Routes } from "discloud.app";
Expand All @@ -8,12 +9,10 @@ import extension from "../../../../extension";
import AppTreeItem from "../../../../structures/AppTreeItem";
import type TeamAppTreeItem from "../../../../structures/TeamAppTreeItem";
import { MAX_FILE_SIZE } from "../../constants";
import SocketClient from "../client";
import { type SocketEventUploadData } from "../types";

export async function socketCommit(task: TaskData, buffer: Buffer, app: AppTreeItem | TeamAppTreeItem) {
await new Promise<void>((resolve, reject) => {
const debugCode = Date.now();
const debugCode = app.appId;

function debug(message: string, ...args: unknown[]) {
extension.debug(`%o ${message}`, debugCode, ...args);
Expand Down Expand Up @@ -47,7 +46,12 @@ export async function socketCommit(task: TaskData, buffer: Buffer, app: AppTreeI
uploading: false,
};

const ws = new SocketClient<SocketEventUploadData>(url)
const socket = new SocketClient<SocketEventUploadData>(url, {
headers: {
"api-token": extension.token!,
...extension.api.options.userAgent ? { "User-Agent": `${extension.api.options.userAgent}` } : {},
},
})
.once("close", async (code, reason) => {
debug("close", code);

Expand Down Expand Up @@ -76,7 +80,7 @@ export async function socketCommit(task: TaskData, buffer: Buffer, app: AppTreeI

task.progress.report({ increment: -1, message: t("committing") });

await ws.sendBuffer(buffer, (data) => {
await socket.sendBuffer(buffer, (data) => {
debug("progress received %o/%o", data.current, data.total);
task.progress.report({ increment: 100 / data.total });
});
Expand All @@ -90,8 +94,11 @@ export async function socketCommit(task: TaskData, buffer: Buffer, app: AppTreeI
task.progress.report({ increment: -1, message: t("socket.connecting") });
})
.on("connectionFailed", async () => {
debug(t("socket.connecting.fail"));
await window.showErrorMessage(t("socket.connecting.fail"));
resolve();
const message = t("socket.connecting.fail");
debug(message);
showLog(message);
await window.showErrorMessage(message);
})
.on("data", (data) => {
debug("data received with status %s %o", data.status, data.statusCode);
Expand All @@ -106,6 +113,8 @@ export async function socketCommit(task: TaskData, buffer: Buffer, app: AppTreeI

if (data.logs) showLog(data.logs);

if (status.uploading) return;

if (data.statusCode !== 102) {
const isDone = data.statusCode === 200;
const isCodeError = !isDone;
Expand All @@ -121,13 +130,16 @@ export async function socketCommit(task: TaskData, buffer: Buffer, app: AppTreeI
showError(error);
})
.on("unauthorized", async () => {
debug(t("socket.authentication.fail"));
await window.showErrorMessage(t("socket.authentication.fail"));
resolve();
const message = t("socket.authentication.fail");
debug(message);
showLog(message);
await window.showErrorMessage(message);
});

extension.context.subscriptions.push(ws);
extension.context.subscriptions.push(socket);

ws.connect().catch(reject);
socket.connect().catch(reject);
});
}

Expand Down
36 changes: 23 additions & 13 deletions src/services/discloud/socket/actions/upload.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SocketClient, type SocketEventUploadData } from "@discloudapp/ws";
import { t } from "@vscode/l10n";
import bytes from "bytes";
import { Routes, type DiscloudConfig } from "discloud.app";
Expand All @@ -6,8 +7,6 @@ import { window } from "vscode";
import { type ApiVscodeApp, type TaskData } from "../../../../@types";
import extension from "../../../../extension";
import { MAX_FILE_SIZE } from "../../constants";
import SocketClient from "../client";
import { type SocketEventUploadData } from "../types";

export async function socketUpload(task: TaskData, buffer: Buffer, dConfig: DiscloudConfig) {
await new Promise<void>((resolve, reject) => {
Expand Down Expand Up @@ -44,16 +43,21 @@ export async function socketUpload(task: TaskData, buffer: Buffer, dConfig: Disc
uploading: false,
};

const ws = new SocketClient<SocketEventUploadData>(url)
const socket = new SocketClient<SocketEventUploadData>(url, {
headers: {
"api-token": extension.token!,
...extension.api.options.userAgent ? { "User-Agent": `${extension.api.options.userAgent}` } : {},
},
})
.once("close", async (code, reason) => {
debug("close", code);

resolve();

setTimeout(() => logger.dispose(), 60_000);

if (!status.connected || !status.authenticated) return;

setTimeout(() => logger.dispose(), 60_000);

if (code !== 1000) {
await window.showErrorMessage(t(`socket.close.${code}`));
return;
Expand All @@ -74,9 +78,12 @@ export async function socketUpload(task: TaskData, buffer: Buffer, dConfig: Disc
task.progress.report({ increment: -1, message: t("socket.connecting") });
})
.on("connectionFailed", async () => {
debug(t("socket.connecting.fail"));
resolve();
await window.showErrorMessage(t("socket.connecting.fail"));
const message = t("socket.connecting.fail");
debug(message);
showLog(message);
await window.showErrorMessage(message);
setTimeout(() => logger.dispose(), 60_000);
})
.on("connected", async () => {
debug("connected");
Expand All @@ -86,7 +93,7 @@ export async function socketUpload(task: TaskData, buffer: Buffer, dConfig: Disc

task.progress.report({ increment: -1, message: t("uploading") });

await ws.sendBuffer(buffer, (data) => {
await socket.sendBuffer(buffer, (data) => {
debug("progress received %o/%o", data.current, data.total);
task.progress.report({ increment: 100 / data.total });
});
Expand All @@ -110,7 +117,7 @@ export async function socketUpload(task: TaskData, buffer: Buffer, dConfig: Disc
dConfig.update({ ID: data.app.id, AVATAR: data.app.avatarURL });

const app: ApiVscodeApp = {
apts: dConfig.data.APT as any,
apts: dConfig.data.APT ?? [],
clusterName: "",
exitCode: data.statusCode === 200 ? 0 : 1,
online: data.statusCode === 200,
Expand All @@ -129,14 +136,17 @@ export async function socketUpload(task: TaskData, buffer: Buffer, dConfig: Disc
showError(error);
})
.on("unauthorized", async () => {
debug(t("socket.authentication.fail"));
resolve();
await window.showErrorMessage(t("socket.authentication.fail"));
const message = t("socket.authentication.fail");
debug(message);
showLog(message);
await window.showErrorMessage(message);
setTimeout(() => logger.dispose(), 60_000);
});

extension.context.subscriptions.push(logger, ws);
extension.context.subscriptions.push(logger, socket);

ws.connect().catch(reject);
socket.connect().catch(reject);
});
}

Expand Down
201 changes: 0 additions & 201 deletions src/services/discloud/socket/client.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/services/discloud/socket/errors/BufferOverflow.ts

This file was deleted.

Loading
Loading