mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-16 03:50:52 +00:00
35 lines
1.5 KiB
JavaScript
35 lines
1.5 KiB
JavaScript
import { CancellationTokenSource } from "@netcatty/plugin-sdk";
|
|
import { queryExample } from "./protocol.mjs";
|
|
import { endpoint } from "./fixture-config.mjs";
|
|
|
|
export default {
|
|
activate(context) {
|
|
let pending;
|
|
const register = (suffix, handler) => context.subscriptions.add(context.commands.registerCommand(
|
|
`com.netcatty.database-minimal.${suffix}`, handler,
|
|
));
|
|
const cancel = () => { pending?.cancel(); return { cancelled: true }; };
|
|
register("cancel", cancel);
|
|
register("endpoint", () => ({ endpoint }));
|
|
context.subscriptions.add({ dispose: cancel });
|
|
register("query", async (args) => {
|
|
if (pending) throw new Error("An example connection is already active");
|
|
if (!args || Array.isArray(args) || Object.keys(args).some((key) => !["username", "password"].includes(key))
|
|
|| typeof args.username !== "string" || typeof args.password !== "string") throw new Error("Enter disposable fixture credentials");
|
|
const cancellation = new CancellationTokenSource();
|
|
pending = cancellation;
|
|
let connection;
|
|
try {
|
|
connection = await context.network.connect({ endpoint }, cancellation.token);
|
|
return await queryExample({ read: () => connection.readable.read(), write: (bytes) => connection.writable.write(bytes),
|
|
close: () => connection.close() }, args.username, args.password);
|
|
} finally {
|
|
args.password = "";
|
|
await connection?.close().catch(() => {});
|
|
cancellation.dispose();
|
|
pending = undefined;
|
|
}
|
|
});
|
|
},
|
|
};
|