Skip to content

Commit

Permalink
fix(commons): handle no json file
Browse files Browse the repository at this point in the history
fix(commons): switch port detection to server
fix(commons): filter out databroker logging
  • Loading branch information
blakebyrnes committed Jun 26, 2024
1 parent 58186f0 commit 28c6243
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 20 deletions.
6 changes: 6 additions & 0 deletions commons/interfaces/ITypedEventEmitter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import IRegisteredEventListener from './IRegisteredEventListener';

export default interface ITypedEventEmitter<T> {
on<K extends keyof T & (string | symbol)>(
eventType: K,
Expand Down Expand Up @@ -42,4 +44,8 @@ export default interface ITypedEventEmitter<T> {
): this;

removeAllListeners(event?: string | symbol): this;
addEventEmitter<K extends keyof T & (string | symbol)>(
emitter: ITypedEventEmitter<T>,
eventTypes: K[],
): IRegisteredEventListener[];
}
2 changes: 1 addition & 1 deletion commons/lib/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@ function extractPathFromModule(module: NodeModule): string {
moduleNamesByPath[fullPath] = fullPath
.replace(/^(.*)[/\\]unblocked[/\\](.+)$/, '$2')
.replace(/^(.*)[/\\]ulixee[/\\](.+)$/, '$2')
.replace(/^(.*)[/\\]payments[/\\](.+)$/, '$2')
.replace(/^(.*)[/\\]@ulixee[/\\](.+)$/, '$2')
.replace(/^(.*)[/\\]commons[/\\](.+)$/, '$2')
.replace(/^.*[/\\]packages[/\\](.+)$/, '$1');
Expand Down Expand Up @@ -370,6 +369,7 @@ registerNamespaceMapping((ns, active, skip) => {
/net\/.*/,
/cloud\/.*/,
/datastore[/-].*/,
/broker\/.*/,
/mainchain[/-].*/,
/sidechain[/-].*/,
/ramps[/-].*/,
Expand Down
2 changes: 1 addition & 1 deletion commons/lib/fileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export async function copyDir(fromDir: string, toDir: string): Promise<void> {
}

export async function readFileAsJson<T>(path: string): Promise<T> {
const buffer = await Fs.readFile(path, 'utf8');
const buffer = await Fs.readFile(path, 'utf8').catch(() => null);
if (!buffer) return null;
return JSON.parse(buffer) as T;
}
Expand Down
27 changes: 17 additions & 10 deletions commons/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,29 @@ export function toUrl(hostOrUrlFragment: string, defaultProtocol = 'ws:'): URL {

export function isPortInUse(port: number | string): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
const client = new net.Socket();
const server = net.createServer();

let isInUse = true;
client.once('error', err => {
if ((err as any).code === 'ECONNREFUSED') {
isInUse = false;
resolve(isInUse);
server.once('error', err => {
if ((err as any).code === 'EADDRINUSE') {
resolve(true); // Port is in use
} else {
reject(err);
}
client.removeAllListeners().end().destroy().unref();
cleanup();
});
client.connect(Number(port), () => {
resolve(isInUse);
client.removeAllListeners().end().destroy().unref();

server.once('listening', () => {
resolve(false);
cleanup();
});

const cleanup = (): void => {
server.removeAllListeners();
server.unref();
server.close();
};

server.listen(Number(port));
});
}

Expand Down
3 changes: 2 additions & 1 deletion net/lib/ApiRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default class ApiRegistry<IHandlerMetadata = any> {
public apiHandlerMetadataFn: (
apiRequest: ICoreRequestPayload<any, any>,
logger: IBoundLog,
remoteId: string,
) => IHandlerMetadata;

public handlersByCommand: { [command: string]: IAsyncFunc } = {};
Expand Down Expand Up @@ -67,7 +68,7 @@ export default class ApiRegistry<IHandlerMetadata = any> {
if (!Array.isArray(args)) args = [apiRequest.args];

const handlerMetadata = this.apiHandlerMetadataFn
? this.apiHandlerMetadataFn(apiRequest, logger)
? this.apiHandlerMetadataFn(apiRequest, logger, transport.remoteId)
: { logger };
data = await handler(...args, handlerMetadata);
} catch (error) {
Expand Down
7 changes: 1 addition & 6 deletions tsconfig.dist.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,5 @@
"outDir": "./build-dist",
"sourceMap": true,
"inlineSourceMap": false
},
"references": [
{
"path": "specification/tsconfig.dist.json"
}
]
}
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7646,7 +7646,7 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==

typescript@^5.3.3, typescript@^5.4.5:
typescript@^5.4.5:
version "5.4.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.4.5.tgz#42ccef2c571fdbd0f6718b1d1f5e6e5ef006f611"
integrity sha512-vcI4UpRgg81oIRUFwR0WSIHKt11nJ7SAVlYNIu+QpqeyXP+gpQJy/Z4+F0aGxSE4MqwjyXvW/TzgkLAx2AGHwQ==
Expand Down

0 comments on commit 28c6243

Please sign in to comment.