Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move schema/crypto/spec to platform #23

Merged
merged 2 commits into from
Jun 26, 2024
Merged
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
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
22 changes: 16 additions & 6 deletions commons/lib/asyncUtils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
export function debounce(func: () => any, wait: number): () => void {
export function debounce<T extends (...args: any[]) => void | Promise<void>>(
func: T,
wait: number,
maxWait?: number,
): T {
let timeout: NodeJS.Timeout;
let lastRun: number;

return function runLater() {
return function runLater(...args: any[]) {
function later(): void {
timeout = undefined;
func();
void func(...args);
}

clearTimeout(timeout);
timeout = setTimeout(later, wait).unref();
};

if (maxWait && Date.now() - lastRun > maxWait) {
void func(...args);
} else {
timeout = setTimeout(later, wait).unref();
}
lastRun = Date.now();
} as T;
}

export function length(source: AsyncIterable<unknown>): Promise<number> {
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
15 changes: 15 additions & 0 deletions commons/lib/objectUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,18 @@ export function omit<T, Keys extends keyof T & string>(
}
return result;
}

export function pick<T, Keys extends keyof T & string>(
object: T,
keys: Keys[],
): Pick<T, Exclude<keyof T, Keys>> {
object = Object(object);
const result = {} as any;

for (const [key, value] of Object.entries(object)) {
if (keys.includes(key as any)) {
result[key] = value;
}
}
return result;
}
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
199 changes: 0 additions & 199 deletions crypto/CHANGELOG.md

This file was deleted.

6 changes: 0 additions & 6 deletions crypto/bin/cli.ts

This file was deleted.

Loading