initial
This commit is contained in:
77
ts/adapters/adapter.factory.ts
Normal file
77
ts/adapters/adapter.factory.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import * as plugins from '../plugins.js';
|
||||
import type { ISmartServeOptions, TRuntime } from '../core/smartserve.interfaces.js';
|
||||
import { UnsupportedRuntimeError } from '../core/smartserve.errors.js';
|
||||
import type { BaseAdapter } from './adapter.base.js';
|
||||
|
||||
/**
|
||||
* Factory for creating runtime-specific adapters
|
||||
* Uses @push.rocks/smartenv for runtime detection
|
||||
*/
|
||||
export class AdapterFactory {
|
||||
private static smartenv = new plugins.smartenv.Smartenv();
|
||||
|
||||
/**
|
||||
* Detect current runtime
|
||||
*/
|
||||
static detectRuntime(): TRuntime {
|
||||
if (this.smartenv.isBrowser) {
|
||||
throw new UnsupportedRuntimeError('browser');
|
||||
}
|
||||
|
||||
// Check for Deno
|
||||
if (typeof (globalThis as any).Deno !== 'undefined') {
|
||||
return 'deno';
|
||||
}
|
||||
|
||||
// Check for Bun
|
||||
if (typeof (globalThis as any).Bun !== 'undefined') {
|
||||
return 'bun';
|
||||
}
|
||||
|
||||
// Default to Node.js
|
||||
return 'node';
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an adapter for the current runtime
|
||||
*/
|
||||
static async createAdapter(options: ISmartServeOptions): Promise<BaseAdapter> {
|
||||
const runtime = this.detectRuntime();
|
||||
|
||||
switch (runtime) {
|
||||
case 'deno': {
|
||||
const { DenoAdapter } = await import('./adapter.deno.js');
|
||||
return new DenoAdapter(options);
|
||||
}
|
||||
|
||||
case 'bun': {
|
||||
const { BunAdapter } = await import('./adapter.bun.js');
|
||||
return new BunAdapter(options);
|
||||
}
|
||||
|
||||
case 'node': {
|
||||
const { NodeAdapter } = await import('./adapter.node.js');
|
||||
return new NodeAdapter(options);
|
||||
}
|
||||
|
||||
default:
|
||||
throw new UnsupportedRuntimeError(runtime);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a specific runtime is available
|
||||
*/
|
||||
static isRuntimeAvailable(runtime: TRuntime): boolean {
|
||||
switch (runtime) {
|
||||
case 'deno':
|
||||
return typeof (globalThis as any).Deno !== 'undefined';
|
||||
case 'bun':
|
||||
return typeof (globalThis as any).Bun !== 'undefined';
|
||||
case 'node':
|
||||
return typeof process !== 'undefined' && !!process.versions?.node;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user