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

fix: re-export type from core/server #5624

Merged
merged 2 commits into from
Apr 10, 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
3 changes: 1 addition & 2 deletions packages/server/core/src/base/adapters/node/bff.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { isWebOnly } from '@modern-js/utils';
import type { ServerBaseOptions } from '../../../core/server';
import {
BindRenderHandleOptions,
getRenderHandler,
} from '../../../base/middlewares';
import { createMiddlewareCollecter, getRuntimeEnv } from '../../utils';
import { ServerBase } from '../../serverBase';
import { ServerBase, type ServerBaseOptions } from '../../serverBase';
import { ServerNodeMiddleware } from './hono';

export const bindBFFHandler = async (
Expand Down
11 changes: 4 additions & 7 deletions packages/server/core/src/base/adapters/node/hono.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NodeRequest, NodeResponse } from '../../../core/plugin';
import {
HonoContext,
Context,
HonoRequest,
ServerEnv,
Middleware,
Expand All @@ -24,17 +24,14 @@ export type ServerNodeEnv = {
};

export type ServerNodeMiddleware = Middleware<ServerNodeEnv>;
export type ServerNodeContext = HonoContext<ServerNodeEnv>;
export type ServerNodeContext = Context<ServerNodeEnv>;

type Handler = (req: NodeRequest, res: NodeResponse) => void | Promise<void>;

// when using the node.js http callback as hono middleware,
// it needs to be the last middleware, because it's possible to send res directly in the http callback.
export const httpCallBack2HonoMid = (handler: Handler) => {
return async (
context: HonoContext<ServerNodeEnv & ServerEnv>,
next: Next,
) => {
return async (context: Context<ServerNodeEnv & ServerEnv>, next: Next) => {
const { req, res } = context.env.node;
// for bff.enableHandleWeb
req.__honoRequest = context.req;
Expand All @@ -61,7 +58,7 @@ type ConnectMiddleware =
const noop = () => {};

export const connectMid2HonoMid = (handler: ConnectMiddleware): Middleware => {
return async (context: HonoContext<ServerNodeEnv>, next: Next) => {
return async (context: Context<ServerNodeEnv>, next: Next) => {
return new Promise((resolve, reject) => {
const { req, res } = context.env.node;
if (handler.length < 3) {
Expand Down
2 changes: 1 addition & 1 deletion packages/server/core/src/base/adapters/node/loadServer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';
import { fs, dotenv, dotenvExpand } from '@modern-js/utils';
import { ServerBaseOptions } from '../../../core/server';
import type { ServerBaseOptions } from '../../serverBase';

/** 读取 .env.{process.env.MODERN_ENV} 文件,加载环境变量 */
export async function loadServerEnv(options: ServerBaseOptions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import {
ROUTE_MANIFEST_FILE,
SERVER_BUNDLE_DIRECTORY,
} from '@modern-js/utils';
import {
HonoMiddleware,
ServerEnv,
ServerManifest,
} from '../../../../core/server';
import { Middleware, ServerEnv, ServerManifest } from '../../../../core/server';

async function getServerManifest(
pwd: string,
Expand Down Expand Up @@ -69,7 +65,7 @@ async function getServerManifest(
export function injectServerManifest(
pwd: string,
routes?: ServerRoute[],
): HonoMiddleware<ServerEnv> {
): Middleware<ServerEnv> {
return async (c, next) => {
if (routes && !c.get('serverManifest')) {
const serverManifest = await getServerManifest(pwd, routes);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path';
import { ServerRoute } from '@modern-js/types';
import { fileReader } from '@modern-js/runtime-utils/fileReader';
import { HonoMiddleware, ServerEnv } from '../../../../core/server';
import { Middleware, ServerEnv } from '../../../../core/server';

export async function getHtmlTemplates(pwd: string, routes: ServerRoute[]) {
const htmls = await Promise.all(
Expand All @@ -16,7 +16,7 @@ export async function getHtmlTemplates(pwd: string, routes: ServerRoute[]) {
return [route.entryName!, html];
}) || [],
);
// eslint-disable-next-line node/no-unsupported-features/es-builtins

const templates: Record<string, string> = Object.fromEntries(htmls);

return templates;
Expand All @@ -25,7 +25,7 @@ export async function getHtmlTemplates(pwd: string, routes: ServerRoute[]) {
export function injectTemplates(
pwd: string,
routes?: ServerRoute[],
): HonoMiddleware<ServerEnv> {
): Middleware<ServerEnv> {
return async (c, next) => {
if (routes && !c.get('templates')) {
const templates = await getHtmlTemplates(pwd, routes);
Expand Down
11 changes: 4 additions & 7 deletions packages/server/core/src/base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@ export {
} from './middlewares';
export type { BindRenderHandleOptions } from './middlewares';

export type { ServerBase } from './serverBase';
export type { ServerBase, ServerBaseOptions } from './serverBase';
export { createServerBase } from './serverBase';

export type {
ServerBaseOptions,
Next,
Middleware,
Context,
Next,
HonoRequest,
ServerEnv,
ServerManifest,
HonoContext,
HonoEnv,
HonoMiddleware,
HonoRequest,
} from '../core/server';
12 changes: 6 additions & 6 deletions packages/server/core/src/base/middlewares/customServer/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
ModernResponse,
} from '@modern-js/types';
import { getCookie } from 'hono/cookie';
import type { HonoContext, HonoRequest, ServerEnv } from '../../../core/server';
import type { Context, HonoRequest, ServerEnv } from '../../../core/server';
import { getHost } from '../../utils';

export function createBaseHookContext(c: HonoContext<ServerEnv>): HookContext {
export function createBaseHookContext(c: Context<ServerEnv>): HookContext {
const logger = c.get('logger');
const metrics = c.get('metrics');

Expand All @@ -23,13 +23,13 @@ export function createBaseHookContext(c: HonoContext<ServerEnv>): HookContext {
class BaseHookRequest implements ModernRequest {
private req: HonoRequest;

private c: HonoContext;
private c: Context;

private headersData: Record<string, string | undefined> = {};

#headers: Record<string, string | undefined>;

constructor(c: HonoContext) {
constructor(c: Context) {
this.c = c;
this.req = c.req;

Expand Down Expand Up @@ -125,9 +125,9 @@ class BaseHookResponse implements ModernResponse {
* */
private_overrided: boolean = false;

private c: HonoContext;
private c: Context;

constructor(c: HonoContext) {
constructor(c: Context) {
this.c = c;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ServerRoute,
HookContext,
} from '@modern-js/types';
import { HonoContext, ServerEnv } from '../../../core/server';
import { Context, ServerEnv } from '../../../core/server';
import type { ServerNodeEnv } from '../../adapters/node/hono';
import { RouterAPI } from './routerApi';
import { TemplateApi } from './template';
Expand All @@ -24,7 +24,7 @@ export function getAfterMatchCtx(
}

export async function getAfterRenderCtx(
c: HonoContext,
c: Context,
baseHookCtx: HookContext,
route: Partial<ServerRoute>,
): Promise<AfterRenderContext> {
Expand All @@ -38,12 +38,10 @@ export async function getAfterRenderCtx(
}

export function createCustomMiddlewaresCtx(
c: HonoContext<ServerNodeEnv & ServerEnv>,
c: Context<ServerNodeEnv & ServerEnv>,
locals: Record<string, any>,
): MiddlewareContext {
const baseContext = createBaseHookContext(
c as unknown as HonoContext<ServerEnv>,
);
const baseContext = createBaseHookContext(c as unknown as Context<ServerEnv>);

const reporter = c.get('reporter');

Expand Down
10 changes: 5 additions & 5 deletions packages/server/core/src/base/middlewares/monitor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Logger, Metrics, Reporter } from '@modern-js/types';
import { time } from '@modern-js/runtime-utils/time';
import { ServerReportTimings } from '../constants';
import type { HonoContext, Next, ServerEnv } from '../../core/server';
import type { Context, Next, ServerEnv } from '../../core/server';

const defaultReporter: Reporter = {
init() {
Expand All @@ -23,7 +23,7 @@ const defaultReporter: Reporter = {

// TODO: unify
export function injectReporter() {
return async (c: HonoContext<ServerEnv>, next: Next) => {
return async (c: Context<ServerEnv>, next: Next) => {
const reporter = c.get('reporter');
if (!reporter) {
c.set('reporter', defaultReporter);
Expand All @@ -33,7 +33,7 @@ export function injectReporter() {
}

export function initReporter(entryName: string) {
return async (c: HonoContext<ServerEnv>, next: Next) => {
return async (c: Context<ServerEnv>, next: Next) => {
const reporter = c.get('reporter');

if (!reporter) {
Expand All @@ -54,7 +54,7 @@ export function initReporter(entryName: string) {
}

export function injectLogger(inputLogger: Logger) {
return async (c: HonoContext<ServerEnv>, next: Next) => {
return async (c: Context<ServerEnv>, next: Next) => {
const logger = c.get('logger');
if (!logger && inputLogger) {
c.set('logger', inputLogger);
Expand All @@ -64,7 +64,7 @@ export function injectLogger(inputLogger: Logger) {
}

export function injectMetrics(inputMetrics: Metrics) {
return async (c: HonoContext<ServerEnv>, next: Next) => {
return async (c: Context<ServerEnv>, next: Next) => {
const metrics = c.get('metrics');

if (!metrics) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Render } from '../../../core/render';
import { Middleware, ServerBaseOptions, ServerEnv } from '../../../core/server';
import { ServerBase } from '../../serverBase';
import { Middleware, ServerEnv } from '../../../core/server';
import { ServerBase, type ServerBaseOptions } from '../../serverBase';
import { checkIsProd, sortRoutes, getRuntimeEnv } from '../../utils';
import type { ServerNodeEnv } from '../../adapters/node/hono';
import { initReporter } from '../monitor';
Expand Down
29 changes: 23 additions & 6 deletions packages/server/core/src/base/serverBase.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { INTERNAL_SERVER_PLUGINS } from '@modern-js/utils/universal/constants';
import { ISAppContext } from '@modern-js/types';
import { ISAppContext, InternalPlugins, ServerRoute } from '@modern-js/types';
import { Hono } from 'hono';
import type * as modernUtilsModule from '@modern-js/utils';
import type * as loadPluginModule from '../core/loadPlugins';
Expand All @@ -10,8 +10,10 @@ import {
ServerHookRunner,
serverManager,
createPlugin,
ServerPlugin,
} from '../core';
import type { HonoEnv, ServerBaseOptions } from '../core/server';
import type { Env } from '../core/server';
import type { ServerOptions } from '../types/config';
import type * as serverConfigModule from './utils/serverConfig';
import { getRuntimeEnv } from './utils';

Expand All @@ -21,7 +23,24 @@ declare module '@modern-js/types' {
}
}

export class ServerBase<E extends HonoEnv = any> {
export type ServerBaseOptions = {
/** server working directory, and then also dist directory */
pwd: string;
config: ServerOptions;
serverConfigFile?: string;
routes?: ServerRoute[];
plugins?: ServerPlugin[];
internalPlugins?: InternalPlugins;
appContext: {
appDirectory?: string;
sharedDirectory?: string;
apiDirectory?: string;
lambdaDirectory?: string;
};
runMode?: 'apiOnly' | 'ssrOnly' | 'webOnly';
};

export class ServerBase<E extends Env = any> {
public options: ServerBaseOptions;

public runner!: ServerHookRunner;
Expand Down Expand Up @@ -260,9 +279,7 @@ export class ServerBase<E extends HonoEnv = any> {
}
}

export function createServerBase<E extends HonoEnv>(
options: ServerBaseOptions,
) {
export function createServerBase<E extends Env>(options: ServerBaseOptions) {
if (options == null) {
throw new Error('can not start server without options');
}
Expand Down
7 changes: 0 additions & 7 deletions packages/server/core/src/core/hono.ts

This file was deleted.

7 changes: 2 additions & 5 deletions packages/server/core/src/core/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import type {
AfterStreamingRenderContext,
} from '@modern-js/types';

import { MiddlewareHandler as Middleware } from 'hono';
import type { BffUserConfig, UserConfig } from '../types/config';
import { HonoMiddleware } from './hono';
import { Render } from './render';

// collect all middleware register in server plugins
Expand Down Expand Up @@ -75,10 +75,7 @@ type Change = {
event: 'add' | 'change' | 'unlink';
};

const prepareApiServer = createAsyncPipeline<
APIServerStartInput,
HonoMiddleware
>();
const prepareApiServer = createAsyncPipeline<APIServerStartInput, Middleware>();

const onApiChange = createAsyncWaterfall<Change[]>();

Expand Down
Loading