BREAKING CHANGE(localtsmdb): add Unix socket support and change LocalTsmDb API to return connection info instead of a MongoClient
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import * as net from 'net';
|
||||
import * as fs from 'fs/promises';
|
||||
import * as plugins from '../plugins.js';
|
||||
import { WireProtocol, OP_QUERY } from './WireProtocol.js';
|
||||
import { CommandRouter } from './CommandRouter.js';
|
||||
@@ -10,10 +11,12 @@ import type { IStorageAdapter } from '../storage/IStorageAdapter.js';
|
||||
* Server configuration options
|
||||
*/
|
||||
export interface ITsmdbServerOptions {
|
||||
/** Port to listen on (default: 27017) */
|
||||
/** Port to listen on (default: 27017) - ignored if socketPath is set */
|
||||
port?: number;
|
||||
/** Host to bind to (default: 127.0.0.1) */
|
||||
/** Host to bind to (default: 127.0.0.1) - ignored if socketPath is set */
|
||||
host?: string;
|
||||
/** Unix socket path - if set, server listens on socket instead of TCP */
|
||||
socketPath?: string;
|
||||
/** Storage type: 'memory' or 'file' (default: 'memory') */
|
||||
storage?: 'memory' | 'file';
|
||||
/** Path for file storage (required if storage is 'file') */
|
||||
@@ -54,7 +57,7 @@ interface IConnectionState {
|
||||
* ```
|
||||
*/
|
||||
export class TsmdbServer {
|
||||
private options: Required<ITsmdbServerOptions>;
|
||||
private options: Required<Omit<ITsmdbServerOptions, 'socketPath'>> & { socketPath: string };
|
||||
private server: net.Server | null = null;
|
||||
private storage: IStorageAdapter;
|
||||
private commandRouter: CommandRouter;
|
||||
@@ -62,11 +65,14 @@ export class TsmdbServer {
|
||||
private connectionIdCounter = 0;
|
||||
private isRunning = false;
|
||||
private startTime: Date = new Date();
|
||||
private useSocket: boolean;
|
||||
|
||||
constructor(options: ITsmdbServerOptions = {}) {
|
||||
this.useSocket = !!options.socketPath;
|
||||
this.options = {
|
||||
port: options.port ?? 27017,
|
||||
host: options.host ?? '127.0.0.1',
|
||||
socketPath: options.socketPath ?? '',
|
||||
storage: options.storage ?? 'memory',
|
||||
storagePath: options.storagePath ?? './data',
|
||||
persistPath: options.persistPath ?? '',
|
||||
@@ -119,6 +125,18 @@ export class TsmdbServer {
|
||||
// Initialize storage
|
||||
await this.storage.initialize();
|
||||
|
||||
// Clean up stale socket file if using Unix socket
|
||||
if (this.useSocket && this.options.socketPath) {
|
||||
try {
|
||||
await fs.unlink(this.options.socketPath);
|
||||
} catch (err: any) {
|
||||
// Ignore ENOENT (file doesn't exist)
|
||||
if (err.code !== 'ENOENT') {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this.server = net.createServer((socket) => {
|
||||
this.handleConnection(socket);
|
||||
@@ -132,11 +150,21 @@ export class TsmdbServer {
|
||||
}
|
||||
});
|
||||
|
||||
this.server.listen(this.options.port, this.options.host, () => {
|
||||
this.isRunning = true;
|
||||
this.startTime = new Date();
|
||||
resolve();
|
||||
});
|
||||
if (this.useSocket && this.options.socketPath) {
|
||||
// Listen on Unix socket
|
||||
this.server.listen(this.options.socketPath, () => {
|
||||
this.isRunning = true;
|
||||
this.startTime = new Date();
|
||||
resolve();
|
||||
});
|
||||
} else {
|
||||
// Listen on TCP
|
||||
this.server.listen(this.options.port, this.options.host, () => {
|
||||
this.isRunning = true;
|
||||
this.startTime = new Date();
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -161,9 +189,22 @@ export class TsmdbServer {
|
||||
await this.storage.close();
|
||||
|
||||
return new Promise((resolve) => {
|
||||
this.server!.close(() => {
|
||||
this.server!.close(async () => {
|
||||
this.isRunning = false;
|
||||
this.server = null;
|
||||
|
||||
// Clean up socket file if using Unix socket
|
||||
if (this.useSocket && this.options.socketPath) {
|
||||
try {
|
||||
await fs.unlink(this.options.socketPath);
|
||||
} catch (err: any) {
|
||||
// Ignore ENOENT (file doesn't exist)
|
||||
if (err.code !== 'ENOENT') {
|
||||
console.error('Failed to remove socket file:', err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
@@ -275,9 +316,21 @@ export class TsmdbServer {
|
||||
* Get the connection URI for this server
|
||||
*/
|
||||
getConnectionUri(): string {
|
||||
if (this.useSocket && this.options.socketPath) {
|
||||
// URL-encode the socket path (replace / with %2F)
|
||||
const encodedPath = encodeURIComponent(this.options.socketPath);
|
||||
return `mongodb://${encodedPath}`;
|
||||
}
|
||||
return `mongodb://${this.options.host}:${this.options.port}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the socket path (if using Unix socket mode)
|
||||
*/
|
||||
get socketPath(): string | undefined {
|
||||
return this.useSocket ? this.options.socketPath : undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the server is running
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user