@trifrost/core
Version:
Blazingly fast, runtime-agnostic server framework for modern edge and node environments
81 lines (80 loc) • 2.8 kB
JavaScript
/// <reference types="@cloudflare/workers-types" />
import { ConsoleExporter, JsonExporter } from '../../modules/Logger';
import { determineTrustProxy, isDevMode } from '../../utils/Generic';
import { WorkerdContext } from './Context';
export class WorkerdRuntime {
/* Global logger instance when runtime has started */
#logger = null;
/* Runtime Start Options */
#cfg = null;
/* Runtime-Enriched Config */
#runtimeCfg = null;
/* Incoming handler which is to be called by the runtime when an incoming request happens */
#onIncoming = null;
/**
* MARK: Workerd handlers
*/
exports = {
fetch: async (request, env, cloudflareCtx) => {
if (!this.#onIncoming)
return new Response('Internal Server Error', { status: 500 });
/* JIT Runtime config resolution */
if (!this.#runtimeCfg) {
const runtimeEnv = { ...env, ...(this.#cfg.env || {}) };
this.#runtimeCfg = {
...this.#cfg,
env: runtimeEnv,
trustProxy: determineTrustProxy(runtimeEnv, true),
};
}
/* Instantiate context */
const ctx = new WorkerdContext(this.#runtimeCfg, this.#logger, request, cloudflareCtx);
try {
await this.#onIncoming(ctx);
if (!ctx.response)
throw new Error('Handler did not respond');
return ctx.response;
}
catch (err) {
this.#logger.error(err, { ctx: ctx.method, path: ctx.path });
return new Response('Internal Server Error', { status: 500 });
}
},
};
/**
* MARK: Runtime Implementation
*/
get name() {
return 'Workerd';
}
get version() {
return null;
}
async boot(opts) {
/* Set onIncoming handler */
this.#onIncoming = opts.onIncoming;
/* Set logger instance */
this.#logger = opts.logger;
/**
* Set global config.
* Take Note: Given that cloudflare workers/workerd by design runs in a trusted environment
* we default trustProxy to true here.
*/
this.#cfg = { ...opts.cfg };
this.#logger.debug('WorkerdRuntime@boot');
}
defaultExporter(env) {
return isDevMode(env) ? new ConsoleExporter() : new JsonExporter();
}
async shutdown() {
if (!this.#onIncoming)
return;
this.#logger.debug('WorkerdRuntime@stop');
/* Clear onIncoming handler */
this.#onIncoming = null;
/* Clear logger instance */
this.#logger = null;
/* Clear global config */
this.#cfg = null;
}
}