From be3763d98862e8919c4a2e3fab5d1ce1cf65e798 Mon Sep 17 00:00:00 2001 From: 12inchpenguin Date: Tue, 8 Apr 2025 14:20:13 +0300 Subject: [PATCH] fixed some type errors --- package.json | 2 +- pnpm-lock.yaml | 66 ++++++++++++++++------------ src/index.ts | 57 +++++++++++++++---------- tsconfig.json | 2 +- types.d.ts | 114 +++++++++++++++++++++++++++++++++++++++---------- 5 files changed, 167 insertions(+), 74 deletions(-) diff --git a/package.json b/package.json index 93fd9ed..af9353c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@debros/network", - "version": "0.0.21-alpha", + "version": "0.0.22-alpha", "description": "Debros network core functionality for IPFS, libp2p and OrbitDB", "type": "module", "main": "dist/index.js", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5e2dddd..165aefa 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1276,8 +1276,8 @@ packages: '@types/babel__core@7.20.5': resolution: {integrity: sha512-qoQprZvz5wQFJwMDqeseRXWv3rqMvhgpbXFfVyWhbx9X47POIA6i/+dXefEmZKoAgOaTdaIgNSMqMIU61yRyzA==} - '@types/babel__generator@7.6.8': - resolution: {integrity: sha512-ASsj+tpEDsEiFr1arWrlN6V3mdfjRMZt6LtK/Vp/kreFLnr5QH5+DhvD5nINYZXzwJvXeGq+05iUXcAzVrqWtw==} + '@types/babel__generator@7.27.0': + resolution: {integrity: sha512-ufFd2Xi92OAVPYsy+P4n7/U7e68fex0+Ee8gSG9KX7eo084CWiQ4sdxktvdl0bOPupXtVJPY19zk6EwWqUQ8lg==} '@types/babel__template@7.4.4': resolution: {integrity: sha512-h/NUaSyG5EyxBIp8YRxo4RMe2/qQgvyowRwVMzhYhBCONbW8PUsg4lkFMrhgZhUe5z3L3MiLDuvyJ/CaPa2A8A==} @@ -1333,6 +1333,9 @@ packages: '@types/node@22.13.16': resolution: {integrity: sha512-15tM+qA4Ypml/N7kyRdvfRjBQT2RL461uF1Bldn06K0Nzn1lY3nAPgHlsVrJxdZ9WhZiW0Fmc1lOYMtDsAuB3w==} + '@types/node@22.14.0': + resolution: {integrity: sha512-Kmpl+z84ILoG+3T/zQFyAJsU6EPTmOCj8/2+83fSN6djd6I4o7uOuGIH6vq3PrjY5BGitSbFuMN18j3iknubbA==} + '@types/qs@6.9.18': resolution: {integrity: sha512-kK7dgTYDyGqS+e2Q4aK9X3D7q234CIZ1Bv0q/7Z5IwRDoADNU81xXJK/YVyLbLTZCoIwUoDoffFeF+p/eIklAA==} @@ -1657,8 +1660,8 @@ packages: resolution: {integrity: sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==} engines: {node: '>=10'} - caniuse-lite@1.0.30001707: - resolution: {integrity: sha512-3qtRjw/HQSMlDWf+X79N206fepf4SOOU6SQLMaq/0KkZLmSjPxAkBOQQ+FxbHKfHmYLZFfdWsO3KA90ceHPSnw==} + caniuse-lite@1.0.30001712: + resolution: {integrity: sha512-MBqPpGYYdQ7/hfKiet9SCI+nmN5/hp4ZzveOJubl5DTAMa5oggjAuoi0Z4onBpKPFI2ePGnQuQIzF3VxDjDJig==} catering@2.1.1: resolution: {integrity: sha512-K7Qy8O9p76sL3/3m7/zLKbRkyOlSZAgzEaLhyj2mXS8PsCud2Eo4hAb8aLtZqHh0QGqLcb9dlJSu6lHRVENm1w==} @@ -1890,8 +1893,8 @@ packages: ee-first@1.1.1: resolution: {integrity: sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==} - electron-to-chromium@1.5.129: - resolution: {integrity: sha512-JlXUemX4s0+9f8mLqib/bHH8gOHf5elKS6KeWG3sk3xozb/JTq/RLXIv8OKUWiK4Ah00Wm88EFj5PYkFr4RUPA==} + electron-to-chromium@1.5.134: + resolution: {integrity: sha512-zSwzrLg3jNP3bwsLqWHmS5z2nIOQ5ngMnfMZOWWtXnqqQkPVyOipxK98w+1beLw1TB+EImPNcG8wVP/cLVs2Og==} emoji-regex@10.4.0: resolution: {integrity: sha512-EC+0oUMY1Rqm4O6LLrgjtYDvcVYTy7chDnM4Q7030tP4Kwj3u/pR6gP9ygnp2CJMK5Gq+9Q2oqmrFJAz01DXjw==} @@ -2330,8 +2333,8 @@ packages: resolution: {integrity: sha512-hsBTNUqQTDwkWtcdYI2i06Y/nUBEsNEDJKjWdigLvegy8kDuJAS8uRlpkkcQpyEXL0Z/pjDy5HBmMjRCJ2gq+g==} engines: {node: '>= 4'} - image-size@1.2.0: - resolution: {integrity: sha512-4S8fwbO6w3GeCVN6OPtA9I5IGKkcDMPcKndtUlpJuCwu7JLjtj7JZpwqLuyY2nrmQT3AWsCJLSKPsc2mPBSl3w==} + image-size@1.2.1: + resolution: {integrity: sha512-rH+46sQJ2dlwfjfhCyNx5thzrv+dtmBIhPHk0zgRUukHzZ/kRueTJXoYYsclBaKcSMBWuGbOFXtioLpzTb5euw==} engines: {node: '>=16.x'} hasBin: true @@ -3778,6 +3781,9 @@ packages: undici-types@6.20.0: resolution: {integrity: sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg==} + undici-types@6.21.0: + resolution: {integrity: sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==} + undici@6.21.2: resolution: {integrity: sha512-uROZWze0R0itiAKVPsYhFov9LxrPMHLMEQFszeI2gCN6bnIIZ8twzBCJcN2LJrBBLfrP0t1FW0g+JmKVl8Vk1g==} engines: {node: '>=18.17'} @@ -5143,14 +5149,14 @@ snapshots: dependencies: '@jest/fake-timers': 29.7.0 '@jest/types': 29.6.3 - '@types/node': 22.13.16 + '@types/node': 22.14.0 jest-mock: 29.7.0 '@jest/fake-timers@29.7.0': dependencies: '@jest/types': 29.6.3 '@sinonjs/fake-timers': 10.3.0 - '@types/node': 22.13.16 + '@types/node': 22.14.0 jest-message-util: 29.7.0 jest-mock: 29.7.0 jest-util: 29.7.0 @@ -5184,7 +5190,7 @@ snapshots: '@jest/schemas': 29.6.3 '@types/istanbul-lib-coverage': 2.0.6 '@types/istanbul-reports': 3.0.4 - '@types/node': 22.13.16 + '@types/node': 22.14.0 '@types/yargs': 17.0.33 chalk: 4.1.2 @@ -5964,11 +5970,11 @@ snapshots: dependencies: '@babel/parser': 7.27.0 '@babel/types': 7.27.0 - '@types/babel__generator': 7.6.8 + '@types/babel__generator': 7.27.0 '@types/babel__template': 7.4.4 '@types/babel__traverse': 7.20.7 - '@types/babel__generator@7.6.8': + '@types/babel__generator@7.27.0': dependencies: '@babel/types': 7.27.0 @@ -6011,7 +6017,7 @@ snapshots: '@types/graceful-fs@4.1.9': dependencies: - '@types/node': 22.13.16 + '@types/node': 22.14.0 '@types/http-errors@2.0.4': {} @@ -6042,6 +6048,10 @@ snapshots: dependencies: undici-types: 6.20.0 + '@types/node@22.14.0': + dependencies: + undici-types: 6.21.0 + '@types/qs@6.9.18': {} '@types/range-parser@1.2.7': {} @@ -6428,8 +6438,8 @@ snapshots: browserslist@4.24.4: dependencies: - caniuse-lite: 1.0.30001707 - electron-to-chromium: 1.5.129 + caniuse-lite: 1.0.30001712 + electron-to-chromium: 1.5.134 node-releases: 2.0.19 update-browserslist-db: 1.1.3(browserslist@4.24.4) @@ -6477,7 +6487,7 @@ snapshots: camelcase@6.3.0: {} - caniuse-lite@1.0.30001707: {} + caniuse-lite@1.0.30001712: {} catering@2.1.1: {} @@ -6494,7 +6504,7 @@ snapshots: chrome-launcher@0.15.2: dependencies: - '@types/node': 22.13.16 + '@types/node': 22.14.0 escape-string-regexp: 4.0.0 is-wsl: 2.2.0 lighthouse-logger: 1.4.2 @@ -6503,7 +6513,7 @@ snapshots: chromium-edge-launcher@0.2.0: dependencies: - '@types/node': 22.13.16 + '@types/node': 22.14.0 escape-string-regexp: 4.0.0 is-wsl: 2.2.0 lighthouse-logger: 1.4.2 @@ -6698,7 +6708,7 @@ snapshots: ee-first@1.1.1: {} - electron-to-chromium@1.5.129: {} + electron-to-chromium@1.5.134: {} emoji-regex@10.4.0: {} @@ -7187,7 +7197,7 @@ snapshots: ignore@5.3.2: {} - image-size@1.2.0: + image-size@1.2.1: dependencies: queue: 6.0.2 @@ -7507,7 +7517,7 @@ snapshots: '@jest/environment': 29.7.0 '@jest/fake-timers': 29.7.0 '@jest/types': 29.6.3 - '@types/node': 22.13.16 + '@types/node': 22.14.0 jest-mock: 29.7.0 jest-util: 29.7.0 @@ -7517,7 +7527,7 @@ snapshots: dependencies: '@jest/types': 29.6.3 '@types/graceful-fs': 4.1.9 - '@types/node': 22.13.16 + '@types/node': 22.14.0 anymatch: 3.1.3 fb-watchman: 2.0.2 graceful-fs: 4.2.11 @@ -7544,7 +7554,7 @@ snapshots: jest-mock@29.7.0: dependencies: '@jest/types': 29.6.3 - '@types/node': 22.13.16 + '@types/node': 22.14.0 jest-util: 29.7.0 jest-regex-util@29.6.3: {} @@ -7552,7 +7562,7 @@ snapshots: jest-util@29.7.0: dependencies: '@jest/types': 29.6.3 - '@types/node': 22.13.16 + '@types/node': 22.14.0 chalk: 4.1.2 ci-info: 3.9.0 graceful-fs: 4.2.11 @@ -7569,7 +7579,7 @@ snapshots: jest-worker@29.7.0: dependencies: - '@types/node': 22.13.16 + '@types/node': 22.14.0 jest-util: 29.7.0 merge-stream: 2.0.0 supports-color: 8.1.1 @@ -7944,7 +7954,7 @@ snapshots: flow-enums-runtime: 0.0.6 graceful-fs: 4.2.11 hermes-parser: 0.25.1 - image-size: 1.2.0 + image-size: 1.2.1 invariant: 2.2.4 jest-worker: 29.7.0 jsc-safe-url: 0.2.4 @@ -8884,6 +8894,8 @@ snapshots: undici-types@6.20.0: {} + undici-types@6.21.0: {} + undici@6.21.2: {} unicode-canonical-property-names-ecmascript@2.0.1: {} diff --git a/src/index.ts b/src/index.ts index d4caac0..fc754b5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,6 @@ import { config, defaultConfig, type DebrosConfig } from './config'; import { validateConfig, type ValidationResult } from './ipfs/config/configValidator'; // Database service exports (new abstracted layer) -import dbService from './db/dbService'; import { init as initDB, create, @@ -23,33 +22,39 @@ import { getMetrics, resetMetrics, closeConnection, - stop as stopDB + stop as stopDB, } from './db/dbService'; import { ErrorCode, StoreType } from './db/types'; // Import types -import type { - Transaction, - CreateResult, - UpdateResult, - PaginatedResult, - ListOptions, - QueryOptions, - FileUploadResult, - FileResult, - CollectionSchema, - SchemaDefinition, - Metrics +import type { + Transaction, + CreateResult, + UpdateResult, + PaginatedResult, + ListOptions, + QueryOptions, + FileUploadResult, + FileResult, + CollectionSchema, + SchemaDefinition, + Metrics, } from './db/types'; import { DBError } from './db/core/error'; // Legacy exports (internal use only, not exposed in default export) -import { init as initIpfs, stop as stopIpfs, getHelia } from './ipfs/ipfsService'; -import { init as initOrbitDB, openDB } from './orbit/orbitDBService'; +import { getConnectedPeers, logPeersStatus } from './ipfs/ipfsService'; + +// Load balancer exports +import loadBalancerController from './ipfs/loadBalancerController'; // Logger exports -import logger, { createServiceLogger, createDebrosLogger, type LoggerOptions } from './utils/logger'; +import logger, { + createServiceLogger, + createDebrosLogger, + type LoggerOptions, +} from './utils/logger'; // Export public API export { @@ -59,7 +64,7 @@ export { validateConfig, type DebrosConfig, type ValidationResult, - + // Database Service (Main public API) initDB, create, @@ -82,7 +87,12 @@ export { stopDB, ErrorCode, StoreType, - + + // Load Balancer + loadBalancerController, + getConnectedPeers, + logPeersStatus, + // Types type Transaction, type DBError, @@ -96,7 +106,7 @@ export { type FileUploadResult, type FileResult, type Metrics, - + // Logger logger, createServiceLogger, @@ -130,8 +140,11 @@ export default { closeConnection, stop: stopDB, ErrorCode, - StoreType + StoreType, }, + loadBalancerController, + logPeersStatus, + getConnectedPeers, logger, createServiceLogger, -}; \ No newline at end of file +}; diff --git a/tsconfig.json b/tsconfig.json index fcce5e0..f7c463c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -50,7 +50,7 @@ // ] // }, }, - "include": ["src/**/*", "orbitdb.d.ts"], + "include": ["src/**/*", "orbitdb.d.ts", "types.d.ts"], "exclude": ["coverage", "dist", "eslint.config.js", "node_modules"], "ts-node": { "esm": true diff --git a/types.d.ts b/types.d.ts index 2e5f05b..8c4cff3 100644 --- a/types.d.ts +++ b/types.d.ts @@ -2,8 +2,8 @@ // Project: https://github.com/debros/anchat-relay // Definitions by: Debros Team -declare module "@debros/network" { - import { Request, Response, NextFunction } from "express"; +declare module '@debros/network' { + import { Request, Response, NextFunction } from 'express'; // Config types export interface DebrosConfig { @@ -51,7 +51,7 @@ declare module "@debros/network" { DOCSTORE = 'docstore', FEED = 'feed', EVENTLOG = 'eventlog', - COUNTER = 'counter' + COUNTER = 'counter', } // Error handling @@ -65,7 +65,7 @@ declare module "@debros/network" { FILE_NOT_FOUND = 'ERR_FILE_NOT_FOUND', INVALID_PARAMETERS = 'ERR_INVALID_PARAMS', CONNECTION_ERROR = 'ERR_CONNECTION', - STORE_TYPE_ERROR = 'ERR_STORE_TYPE' + STORE_TYPE_ERROR = 'ERR_STORE_TYPE', } export class DBError extends Error { @@ -182,38 +182,75 @@ declare module "@debros/network" { // Database Operations export function initDB(connectionId?: string): Promise; - export function create>(collection: string, id: string, data: Omit, options?: { connectionId?: string, storeType?: StoreType }): Promise; - export function get>(collection: string, id: string, options?: { connectionId?: string; skipCache?: boolean, storeType?: StoreType }): Promise; - export function update>(collection: string, id: string, data: Partial>, options?: { connectionId?: string; upsert?: boolean, storeType?: StoreType }): Promise; - export function remove(collection: string, id: string, options?: { connectionId?: string, storeType?: StoreType }): Promise; - export function list>(collection: string, options?: ListOptions): Promise>; - export function query>(collection: string, filter: (doc: T) => boolean, options?: QueryOptions): Promise>; - + export function create>( + collection: string, + id: string, + data: Omit, + options?: { connectionId?: string; storeType?: StoreType }, + ): Promise; + export function get>( + collection: string, + id: string, + options?: { connectionId?: string; skipCache?: boolean; storeType?: StoreType }, + ): Promise; + export function update>( + collection: string, + id: string, + data: Partial>, + options?: { connectionId?: string; upsert?: boolean; storeType?: StoreType }, + ): Promise; + export function remove( + collection: string, + id: string, + options?: { connectionId?: string; storeType?: StoreType }, + ): Promise; + export function list>( + collection: string, + options?: ListOptions, + ): Promise>; + export function query>( + collection: string, + filter: (doc: T) => boolean, + options?: QueryOptions, + ): Promise>; + // Schema operations export function defineSchema(collection: string, schema: CollectionSchema): void; - + // Transaction operations export function createTransaction(connectionId?: string): Transaction; - export function commitTransaction(transaction: Transaction): Promise<{ success: boolean; results: any[] }>; - + export function commitTransaction( + transaction: Transaction, + ): Promise<{ success: boolean; results: any[] }>; + // Index operations - export function createIndex(collection: string, field: string, options?: { connectionId?: string, storeType?: StoreType }): Promise; - + export function createIndex( + collection: string, + field: string, + options?: { connectionId?: string; storeType?: StoreType }, + ): Promise; + // Subscription API - export function subscribe(event: 'document:created' | 'document:updated' | 'document:deleted', callback: (data: any) => void): () => void; - + export function subscribe( + event: 'document:created' | 'document:updated' | 'document:deleted', + callback: (data: any) => void, + ): () => void; + // File operations - export function uploadFile(fileData: Buffer, options?: { filename?: string; connectionId?: string; metadata?: Record; }): Promise; + export function uploadFile( + fileData: Buffer, + options?: { filename?: string; connectionId?: string; metadata?: Record }, + ): Promise; export function getFile(cid: string, options?: { connectionId?: string }): Promise; export function deleteFile(cid: string, options?: { connectionId?: string }): Promise; - + // Connection management export function closeConnection(connectionId: string): Promise; - + // Metrics export function getMetrics(): Metrics; export function resetMetrics(): void; - + // Stop export function stopDB(): Promise; @@ -227,6 +264,26 @@ declare module "@debros/network" { export function createServiceLogger(name: string, options?: LoggerOptions): any; export function createDebrosLogger(options?: LoggerOptions): any; + // Load Balancer + export interface LoadBalancerControllerModule { + getNodeInfo: (req: Request, res: Response, next: NextFunction) => void; + getOptimalPeer: (req: Request, res: Response, next: NextFunction) => void; + getAllPeers: (req: Request, res: Response, next: NextFunction) => void; + } + export const loadBalancerController: LoadBalancerControllerModule; + + export const getConnectedPeers: () => Map< + string, + { + lastSeen: number; + load: number; + publicAddress: string; + fingerprint: string; + } + >; + + export const logPeersStatus: () => void; + // Default export const defaultExport: { config: DebrosConfig; @@ -254,8 +311,19 @@ declare module "@debros/network" { ErrorCode: typeof ErrorCode; StoreType: typeof StoreType; }; + loadBalancerController: LoadBalancerControllerModule; + getConnectedPeers: () => Map< + string, + { + lastSeen: number; + load: number; + publicAddress: string; + fingerprint: string; + } + >; + logPeersStatus: () => void; logger: any; createServiceLogger: typeof createServiceLogger; }; export default defaultExport; -} \ No newline at end of file +}