Skip to content

Commit

Permalink
refactor!: ESM support and use of built-in dependencies
Browse files Browse the repository at this point in the history
Release-As: 2.0.0
  • Loading branch information
dlenroc authored Dec 3, 2023
1 parent 566d124 commit ca8f02c
Show file tree
Hide file tree
Showing 172 changed files with 2,095 additions and 1,034 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 Corneliu Duplachi
Copyright (c) Corneliu Duplachi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
"./packages/*"
],
"scripts": {
"build": "npm run build -ws"
"build": "npm run build -ws",
"test": "npm run test -ws --if-present"
},
"devDependencies": {
"@tsconfig/node20": "^20.1.2",
"@tsconfig/strictest": "^2.0.2",
"@types/node": "^20.9.1",
"@types/sinon": "^17.0.1",
"@types/node": "^20.10.2",
"@types/sinon": "^17.0.2",
"sinon": "^17.0.1",
"tsx": "^4.1.3",
"typescript": "^5.2.2"
"tsx": "^4.6.2",
"typescript": "^5.3.2"
}
}
2 changes: 1 addition & 1 deletion packages/debug-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const plugins = await getPlugins(ctx);
console.log(plugins);

// raw
const rawResult = await ctx.execute('plugins', []);
const rawResult = await ctx.execute('plugins');
console.log(rawResult);
```

Expand Down
10 changes: 4 additions & 6 deletions packages/debug-server/src/DebugServerError.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
export class DebugServerError extends Error {
public readonly cmd: string;
public readonly args: string[];
public readonly command: string;
public readonly output: string;

constructor(options: { cmd: string; args: string[]; output: string }) {
super(`Command "${options.cmd}" failed: ${options.output}`);
constructor(options: { command: string; output: string }) {
super(`Command "${options.command}" failed with output: ${options.output}`);
this.name = 'DebugServerError';
this.cmd = options.cmd;
this.args = options.args || [];
this.command = options.command;
this.output = options.output;
}
}
53 changes: 16 additions & 37 deletions packages/debug-server/src/DebugServerExecutor.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { createConnection } from 'node:net';
import type { Executor } from './Executor.js';

type DebugServerExecutorOptions = {
signal?: AbortSignal;
};
import { connect } from 'node:net';
import { Readable } from 'node:stream';
import type { DebugServerExecutorOptions } from './DebugServerExecutorOptions.ts';
import type { Executor } from './Executor.ts';

export class DebugServerExecutor
implements Executor<DebugServerExecutorOptions>
{
#hostname: string;
#port: number;
#signal?: AbortSignal | undefined;
#signal?: AbortSignal;

constructor(options: {
hostname: string;
Expand All @@ -19,12 +17,11 @@ export class DebugServerExecutor
}) {
this.#hostname = options.hostname;
this.#port = options.port;
this.#signal = options.signal;
this.#signal = options.signal!;
}

async execute(
command: string,
args: string[],
config?: DebugServerExecutorOptions
): Promise<string> {
const signal =
Expand All @@ -33,35 +30,17 @@ export class DebugServerExecutor
AbortSignal.any([this.#signal, config.signal])
: this.#signal || config?.signal;

return new Promise((resolve, reject) => {
const cmd = `${command} ${args.join(' ')}`.replace(/\s/g, ' ').trimEnd();

const client = createConnection(
{ host: this.#hostname, port: this.#port, signal },
() => {
client.write(`${cmd}\nq\n`);
}
);

let chunks: Buffer[] = [];

client.on('data', (chunk) => {
chunks.push(chunk);
});

client.on('end', () => {
const response = Buffer.concat(chunks)
.toString()
.replace(/\r\n?/g, '\n');
const socket = connect(
{ host: this.#hostname, port: this.#port, signal },
() => {
socket.write(`${command}\nq\n`);
}
);

const start = response.indexOf('\n>') + 2;
const end = response.lastIndexOf('\n>');
resolve(response.slice(start, end).trim());
});
const result = await new Response(Readable.toWeb(socket)).text();

client.on('error', (error) => {
reject(error);
});
});
return result
.slice(result.indexOf('\n>') + 2, result.lastIndexOf('\n>'))
.trim();
}
}
3 changes: 3 additions & 0 deletions packages/debug-server/src/DebugServerExecutorOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface DebugServerExecutorOptions {
signal?: AbortSignal;
}
4 changes: 2 additions & 2 deletions packages/debug-server/src/Executor.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export interface Executor<Config> {
execute(command: string, args: string[], config?: Config): Promise<string>;
export interface Executor<Config = {}> {
execute(command: string, config?: Config): Promise<string>;
}
9 changes: 5 additions & 4 deletions packages/debug-server/src/commands/clearLaunchCaches.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import type { Executor } from '../Executor.js';
import { execute, type Config } from '../internal/execute.js';
import type { Executor } from '../Executor.ts';
import { execute } from '../internal/execute.js';
import type { Config } from '../internal/types.d.ts';

const pattern = /Done/;

/**
* Clear all caches that can affect channel launch time.
*/
export async function clearLaunchCaches<Context extends Executor<{}>>(
export async function clearLaunchCaches<Context extends Executor>(
ctx: Context,
config?: Config<Context>
): Promise<void> {
await execute(ctx, 'clear_launch_caches', [], [pattern], config);
await execute(ctx, 'clear_launch_caches', [pattern], config);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import type { Executor } from '../Executor.js';
import { execute, type Config } from '../internal/execute.js';
import type { Executor } from '../Executor.ts';
import { execute } from '../internal/execute.js';
import type { Config } from '../internal/types.d.ts';

const pattern = /^\s*$/;

/**
* Clear SceneGraph node operation performance metrics.
*/
export async function clearSceneGraphPerformanceMetrics<
Context extends Executor<{}>
Context extends Executor
>(ctx: Context, config?: Config<Context>): Promise<void> {
await execute(ctx, 'sgperf', ['clear'], [pattern], config);
await execute(ctx, 'sgperf clear', [pattern], config);
}
8 changes: 4 additions & 4 deletions packages/debug-server/src/commands/createDeveloperKey.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Executor } from '../Executor.js';
import { execute, type Config } from '../internal/execute.js';
import type { Executor } from '../Executor.ts';
import { execute } from '../internal/execute.js';
import type { Config } from '../internal/types.d.ts';
import type { DeveloperKey } from '../types/DeveloperKey.ts';

const idPattern = /DevID: (.*)/;
Expand All @@ -8,14 +9,13 @@ const passwordPattern = /Password: (.*)/;
/**
* Generate a new developer key.
*/
export async function createDeveloperKey<Context extends Executor<{}>>(
export async function createDeveloperKey<Context extends Executor>(
ctx: Context,
config?: Config<Context>
): Promise<DeveloperKey> {
const result = await execute(
ctx,
'genkey',
[],
[idPattern, passwordPattern],
config
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import type { Executor } from '../Executor.js';
import { execute, type Config } from '../internal/execute.js';
import type { Executor } from '../Executor.ts';
import { execute } from '../internal/execute.js';
import type { Config } from '../internal/types.d.ts';

const pattern = /rendezvous logging is off/;

/**
* Disable rendezvous logging.
*/
export async function disableRendezvousLogging<Context extends Executor<{}>>(
export async function disableRendezvousLogging<Context extends Executor>(
ctx: Context,
config?: Config<Context>
): Promise<void> {
await execute(ctx, 'logrendezvous', ['off'], [pattern], config);
await execute(ctx, 'logrendezvous off', [pattern], config);
}
9 changes: 5 additions & 4 deletions packages/debug-server/src/commands/enableRendezvousLogging.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import type { Executor } from '../Executor.js';
import { execute, type Config } from '../internal/execute.js';
import type { Executor } from '../Executor.ts';
import { execute } from '../internal/execute.js';
import type { Config } from '../internal/types.d.ts';

const pattern = /rendezvous logging is on/;

/**
* Enable rendezvous logging.
*/
export async function enableRendezvousLogging<Context extends Executor<{}>>(
export async function enableRendezvousLogging<Context extends Executor>(
ctx: Context,
config?: Config<Context>
): Promise<void> {
await execute(ctx, 'logrendezvous', ['on'], [pattern], config);
await execute(ctx, 'logrendezvous on', [pattern], config);
}
11 changes: 6 additions & 5 deletions packages/debug-server/src/commands/getChannelPerformanceStats.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Executor } from '../Executor.js';
import { execute, type Config } from '../internal/execute.js';
import type { ChannelStats } from '../types/ChannelStats.js';
import type { Executor } from '../Executor.ts';
import { execute } from '../internal/execute.js';
import type { Config } from '../internal/types.d.ts';
import type { ChannelStats } from '../types/ChannelStats.ts';

const pattern =
/mem=(?<mem>\d+)KiB{anon=(?<anon>\d+),file=(?<file>\d+),shared=(?<shared>\d+),swap=(?<swap>\d+)},%cpu=(?<cpu>\d+){user=(?<user>\d+),sys=(?<sys>\d+)}/;
Expand All @@ -10,11 +11,11 @@ const pattern =
*
* The channel manifest must include the **run_as_process=1** attribute to use this command.
*/
export async function getChannelPerformanceStats<Context extends Executor<{}>>(
export async function getChannelPerformanceStats<Context extends Executor>(
ctx: Context,
config?: Config<Context>
): Promise<ChannelStats> {
const [[result]] = await execute(ctx, 'chanperf', [], [pattern], config);
const [[result]] = await execute(ctx, 'chanperf', [pattern], config);

return {
cpu: {
Expand Down
9 changes: 5 additions & 4 deletions packages/debug-server/src/commands/getDeveloperKey.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import type { Executor } from '../Executor.js';
import { execute, type Config } from '../internal/execute.js';
import type { Executor } from '../Executor.ts';
import { execute } from '../internal/execute.js';
import type { Config } from '../internal/types.d.ts';

const pattern = /Dev ID: (.+)/;

/**
* Returns current developer key.
*/
export async function getDeveloperKey<Context extends Executor<{}>>(
export async function getDeveloperKey<Context extends Executor>(
ctx: Context,
config?: Config<Context>
): Promise<string> {
const [[result]] = await execute(ctx, 'showkey', [], [pattern], config);
const [[result]] = await execute(ctx, 'showkey', [pattern], config);
return result[1];
}
11 changes: 5 additions & 6 deletions packages/debug-server/src/commands/getLoadedTextures.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import type { Executor } from '../Executor.js';
import { execute, type Config } from '../internal/execute.js';
import type { Executor } from '../Executor.ts';
import { execute } from '../internal/execute.js';
import type { Config } from '../internal/types.d.ts';
import type { LoadedTextures } from '../types/LoadedTextures.ts';

const pattern = /\* .+ Textures +\*/;

/**
* Returns loaded textures.
*/
export async function getLoadedTextures<Context extends Executor<{}>>(
export async function getLoadedTextures<Context extends Executor>(
ctx: Context,
payload?: {
/**
Expand All @@ -17,11 +18,9 @@ export async function getLoadedTextures<Context extends Executor<{}>>(
},
config?: Config<Context>
): Promise<LoadedTextures> {
const args = payload?.overlay ? [payload.overlay] : [];
const [[match]] = await execute(
ctx,
'loaded_textures',
args,
`loaded_textures${payload?.overlay ? ' ' + payload.overlay : ''}`,
[pattern],
config
);
Expand Down
8 changes: 4 additions & 4 deletions packages/debug-server/src/commands/getMaxWarningCount.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import type { Executor } from '../Executor.js';
import { execute, type Config } from '../internal/execute.js';
import type { Executor } from '../Executor.ts';
import { execute } from '../internal/execute.js';
import type { Config } from '../internal/types.d.ts';

const pattern = /brightscript warning limit: (\d+)/;

/**
* Returns the maximum number of BrightScript warnings that can be displayed
* in the BrightScript console (port 8085).
*/
export async function getMaxWarningCount<Context extends Executor<{}>>(
export async function getMaxWarningCount<Context extends Executor>(
ctx: Context,
config?: Config<Context>
): Promise<number> {
const [[result]] = await execute(
ctx,
'brightscript_warnings',
[],
[pattern],
config
);
Expand Down
8 changes: 4 additions & 4 deletions packages/debug-server/src/commands/getMemoryStats.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Executor } from '../Executor.js';
import { execute, type Config } from '../internal/execute.js';
import type { Executor } from '../Executor.ts';
import { execute } from '../internal/execute.js';
import type { Config } from '../internal/types.d.ts';
import type { MemoryStats } from '../types/MemoryStats.ts';

const memoryPattern =
Expand All @@ -9,14 +10,13 @@ const swapPattern = /Swap:\s*(?<total>\d+)\s*(?<used>\d+)\s*(?<free>\d+)/;
/**
* Returns memory stats.
*/
export async function getMemoryStats<Context extends Executor<{}>>(
export async function getMemoryStats<Context extends Executor>(
ctx: Context,
config?: Config<Context>
): Promise<MemoryStats> {
const [[memory], [swap]] = await execute(
ctx,
'free',
[],
[memoryPattern, swapPattern],
config
);
Expand Down
Loading

0 comments on commit ca8f02c

Please sign in to comment.