Skip to content

Commit

Permalink
release beta
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jul 8, 2024
1 parent 9f66f32 commit 9f327a4
Show file tree
Hide file tree
Showing 24 changed files with 315 additions and 296 deletions.
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"node": ">= 18.19.0"
},
"publishConfig": {
"tag": "next"
"tag": "beta"
},
"description": "A web application framework for Node.js",
"keywords": [
Expand Down Expand Up @@ -58,6 +58,7 @@
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.15.3",
"@eggjs/koa": "^2.19.1",
"@eggjs/tsconfig": "1",
"@types/koa-bodyparser": "^4.3.12",
"@types/mocha": "^10.0.7",
Expand All @@ -82,13 +83,13 @@
"sdk-base": "^4.2.1",
"spy": "^1.0.0",
"supertest": "^6.2.4",
"tshy": "^1.18.0",
"tshy-after": "^1.1.1",
"tshy": "2",
"tshy-after": "1",
"typescript": "5"
},
"scripts": {
"lint": "eslint src test --ext .ts",
"pretest": "npm run lint -- --fix",
"pretest": "npm run lint -- --fix && npm run prepublishOnly",
"test": "egg-bin test",
"test:changed": "egg-bin test --changed",
"cov": "egg-bin cov --timeout 100000",
Expand Down Expand Up @@ -132,12 +133,10 @@
"exports": {
".": {
"import": {
"source": "./src/index.ts",
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"source": "./src/index.ts",
"types": "./dist/commonjs/index.d.ts",
"default": "./dist/commonjs/index.js"
}
Expand Down
2 changes: 1 addition & 1 deletion src/agent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BaseHookClass } from './lib/core/base_hook_class.js';

export class EggAgentHook extends BaseHookClass {
export default class EggAgentHook extends BaseHookClass {
configDidLoad() {
this.agent._wrapMessenger();
}
Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
*/

import { BaseContextClass } from './lib/core/base_context_class.js';
import { startEgg } from './lib/start.js';

// export types
export * from './lib/egg.js';
export * from './lib/type.js';
export * from './lib/start.js';

/**
* Start egg application with cluster mode
Expand All @@ -16,7 +22,7 @@ export { startCluster } from 'egg-cluster';
* Start egg application with single process mode
* @since 1.0.0
*/
export { startEgg as start } from './lib/start.js';
export const start = startEgg;

/**
* @member {Application} Egg#Application
Expand Down
12 changes: 6 additions & 6 deletions src/lib/agent.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { EggLogger } from 'egg-logger';
import { EggApplication, EggApplicationOptions } from './egg.js';
import { EggApplicationCore, EggApplicationCoreOptions } from './egg.js';
import { AgentWorkerLoader } from './loader/index.js';

const EGG_LOADER = Symbol.for('egg#loader');

/**
* Singleton instance in Agent Worker, extend {@link EggApplication}
* @augments EggApplication
* Singleton instance in Agent Worker, extend {@link EggApplicationCore}
* @augments EggApplicationCore
*/
export class Agent extends EggApplication {
export class Agent extends EggApplicationCore {
readonly #agentAliveHandler: NodeJS.Timeout;

/**
* @class
* @param {Object} options - see {@link EggApplication}
* @param {Object} options - see {@link EggApplicationCore}
*/
constructor(options?: Omit<EggApplicationOptions, 'type'>) {
constructor(options?: Omit<EggApplicationCoreOptions, 'type'>) {
super({
...options,
type: 'agent',
Expand Down
18 changes: 9 additions & 9 deletions src/lib/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Socket } from 'node:net';
import graceful from 'graceful';
import { assign } from 'utility';
import { utils as eggUtils } from '@eggjs/core';
import { EggApplication, EggApplicationContext, EggApplicationOptions } from './egg.js';
import { EggApplicationCore, EggContext, EggApplicationCoreOptions } from './egg.js';
import { AppWorkerLoader } from './loader/index.js';
import { BaseContextClass } from './core/base_context_class.js';

Expand Down Expand Up @@ -42,10 +42,10 @@ function escapeHeaderValue(value: string) {
class HelperClass extends BaseContextClass {}

/**
* Singleton instance in App Worker, extend {@link EggApplication}
* @augments EggApplication
* Singleton instance in App Worker, extend {@link EggApplicationCore}
* @augments EggApplicationCore
*/
export class Application extends EggApplication {
export class Application extends EggApplicationCore {
// will auto set after 'server' event emit
server?: http.Server;
#locals: Record<string, any> = {};
Expand All @@ -57,9 +57,9 @@ export class Application extends EggApplication {

/**
* @class
* @param {Object} options - see {@link EggApplication}
* @param {Object} options - see {@link EggApplicationCore}
*/
constructor(options?: Omit<EggApplicationOptions, 'type'>) {
constructor(options?: Omit<EggApplicationCoreOptions, 'type'>) {
super({
...options,
type: 'application',
Expand Down Expand Up @@ -228,7 +228,7 @@ export class Application extends EggApplication {
* @see Context#runInBackground
* @param {Function} scope - the first args is an anonymous ctx
*/
runInBackground(scope: (ctx: EggApplicationContext) => void) {
runInBackground(scope: (ctx: EggContext) => void) {
const ctx = this.createAnonymousContext();
if (!scope.name) {
Reflect.set(scope, '_name', eggUtils.getCalleeFromStack(true));
Expand All @@ -244,13 +244,13 @@ export class Application extends EggApplication {
* @param {Function} scope - the first args is an anonymous ctx, scope should be async function
* @param {Request} [req] - if you want to mock request like querystring, you can pass an object to this function.
*/
async runInAnonymousContextScope(scope: (ctx: EggApplicationContext) => Promise<void>, req?: unknown) {
async runInAnonymousContextScope(scope: (ctx: EggContext) => Promise<void>, req?: unknown) {
const ctx = this.createAnonymousContext(req);
if (!scope.name) {
Reflect.set(scope, '_name', eggUtils.getCalleeFromStack(true));
}
return await this.ctxStorage.run(ctx, async () => {
return await scope(ctx as EggApplicationContext);
return await scope(ctx as EggContext);
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib/core/base_context_class.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseContextClass as EggCoreBaseContextClass } from '@eggjs/core';
import type { EggApplicationContext } from '../egg.js';
import type { EggContext } from '../egg.js';
import { BaseContextLogger } from './base_context_logger.js';

/**
Expand All @@ -8,7 +8,7 @@ import { BaseContextLogger } from './base_context_logger.js';
* {@link Helper}, {@link Service} is extending it.
*/
export class BaseContextClass extends EggCoreBaseContextClass {
declare ctx: EggApplicationContext;
declare ctx: EggContext;
protected pathName?: string;
#logger?: BaseContextLogger;

Expand Down
6 changes: 3 additions & 3 deletions src/lib/core/base_context_logger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { EggApplicationContext } from '../egg.js';
import type { EggContext } from '../egg.js';

export class BaseContextLogger {
readonly #ctx: EggApplicationContext;
readonly #ctx: EggContext;
readonly #pathName?: string;

/**
Expand All @@ -10,7 +10,7 @@ export class BaseContextLogger {
* @param {String} pathName - class path name
* @since 1.0.0
*/
constructor(ctx: EggApplicationContext, pathName?: string) {
constructor(ctx: EggContext, pathName?: string) {
/**
* @member {Context} BaseContextLogger#ctx
* @since 1.2.0
Expand Down
8 changes: 4 additions & 4 deletions src/lib/core/context_httpclient.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { EggApplicationContext, EggApplication } from '../egg.js';
import type { EggContext, EggApplicationCore } from '../egg.js';
import type {
HttpClientRequestURL, HttpClientRequestOptions,
} from './httpclient.js';

export class ContextHttpClient {
ctx: EggApplicationContext;
app: EggApplication;
ctx: EggContext;
app: EggApplicationCore;

constructor(ctx: EggApplicationContext) {
constructor(ctx: EggContext) {
this.ctx = ctx;
this.app = ctx.app;
}
Expand Down
8 changes: 4 additions & 4 deletions src/lib/core/httpclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
RequestOptions,
} from 'urllib';
import ms from 'ms';
import type { EggApplication } from '../egg.js';
import type { EggApplicationCore } from '../egg.js';

export type {
HttpClientResponse,
Expand All @@ -18,9 +18,9 @@ export interface HttpClientRequestOptions extends RequestOptions {
}

export class HttpClient extends RawHttpClient {
readonly #app: EggApplication & { tracer?: unknown };
readonly #app: EggApplicationCore & { tracer?: unknown };

constructor(app: EggApplication) {
constructor(app: EggApplicationCore) {
normalizeConfig(app);
const config = app.config.httpclient;
super({
Expand All @@ -44,7 +44,7 @@ export class HttpClient extends RawHttpClient {
}
}

function normalizeConfig(app: EggApplication) {
function normalizeConfig(app: EggApplicationCore) {
const config = app.config.httpclient;
if (typeof config.request?.timeout === 'string') {
config.request.timeout = ms(config.request.timeout as string);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/core/logger.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { EggLoggers, EggLoggersOptions } from 'egg-logger';
import { setCustomLogger } from 'onelogger';
import type { EggApplication } from '../egg.js';
import type { EggApplicationCore } from '../egg.js';

export function createLoggers(app: EggApplication) {
export function createLoggers(app: EggApplicationCore) {
const loggerOptions = {
...app.config.logger,
type: app.type,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/core/messenger/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Messenger as LocalMessenger } from './local.js';
import { Messenger as IPCMessenger } from './ipc.js';
import type { IMessenger } from './IMessenger.js';
import type { EggApplication } from '../../egg.js';
import type { EggApplicationCore } from '../../egg.js';

export type { IMessenger } from './IMessenger.js';

/**
* @class Messenger
*/
export function create(egg: EggApplication): IMessenger {
export function create(egg: EggApplicationCore): IMessenger {
return egg.options.mode === 'single'
? new LocalMessenger(egg)
: new IPCMessenger();
Expand Down
6 changes: 3 additions & 3 deletions src/lib/core/messenger/local.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { debuglog } from 'node:util';
import EventEmitter from 'node:events';
import type { IMessenger } from './IMessenger.js';
import type { EggApplication } from '../../egg.js';
import type { EggApplicationCore } from '../../egg.js';

const debug = debuglog('egg:lib:core:messenger:local');

Expand All @@ -10,9 +10,9 @@ const debug = debuglog('egg:lib:core:messenger:local');
*/
export class Messenger extends EventEmitter implements IMessenger {
readonly pid: string;
readonly egg: EggApplication;
readonly egg: EggApplicationCore;

constructor(egg: EggApplication) {
constructor(egg: EggApplicationCore) {
super();
this.egg = egg;
this.pid = String(process.pid);
Expand Down
8 changes: 4 additions & 4 deletions src/lib/core/singleton.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import assert from 'node:assert';
import { isAsyncFunction } from 'is-type-of';
import type { EggApplication } from '../egg.js';
import type { EggApplicationCore } from '../egg.js';

export type SingletonCreateMethod =
(config: Record<string, any>, app: EggApplication, clientName: string) => unknown | Promise<unknown>;
(config: Record<string, any>, app: EggApplicationCore, clientName: string) => unknown | Promise<unknown>;

export interface SingletonOptions {
name: string;
app: EggApplication;
app: EggApplicationCore;
create: SingletonCreateMethod;
}

export class Singleton {
readonly clients = new Map<string, any>();
readonly app: EggApplication;
readonly app: EggApplicationCore;
readonly create: SingletonCreateMethod;
readonly name: string;
readonly options: Record<string, any>;
Expand Down
16 changes: 8 additions & 8 deletions src/lib/egg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ import type { EggApplicationLoader } from './loader/index.js';

const EGG_PATH = Symbol.for('egg#eggPath');

export interface EggApplicationOptions extends Omit<EggCoreOptions, 'baseDir'> {
export interface EggApplicationCoreOptions extends Omit<EggCoreOptions, 'baseDir'> {
mode?: 'cluster' | 'single';
clusterPort?: number;
baseDir?: string;
}

export interface EggApplicationContext extends EggCoreContext {
app: EggApplication;
export interface EggContext extends EggCoreContext {
app: EggApplicationCore;
/**
* Request start time
* @member {Number} Context#starttime
Expand All @@ -59,7 +59,7 @@ export interface EggApplicationContext extends EggCoreContext {
* @see https://github.com/eggjs/koa/blob/master/src/application.ts
* @augments EggCore
*/
export class EggApplication extends EggCore {
export class EggApplicationCore extends EggCore {
// export context base classes, let framework can impl sub class and over context extend easily.
ContextCookies = ContextCookies;
ContextLogger = ContextLogger;
Expand Down Expand Up @@ -105,7 +105,7 @@ export class EggApplication extends EggCore {
*/
Boot = BaseHookClass;

declare options: Required<EggApplicationOptions>;
declare options: Required<EggApplicationCoreOptions>;

#httpClient?: HttpClient;
#loggers?: EggLoggers;
Expand All @@ -124,7 +124,7 @@ export class EggApplication extends EggCore {
* - {Object} [plugins] - custom plugin config, use it in unittest
* - {String} [mode] - process mode, can be cluster / single, default is `cluster`
*/
constructor(options?: EggApplicationOptions) {
constructor(options?: EggApplicationCoreOptions) {
options = {
mode: 'cluster',
type: 'application',
Expand Down Expand Up @@ -620,8 +620,8 @@ export class EggApplication extends EggCore {
* @param {Res} res - node native Response object
* @return {Context} context object
*/
createContext(req: IncomingMessage, res: ServerResponse): EggApplicationContext {
const context = Object.create(this.context) as EggApplicationContext;
createContext(req: IncomingMessage, res: ServerResponse): EggContext {
const context = Object.create(this.context) as EggContext;
const request = context.request = Object.create(this.request);
const response = context.response = Object.create(this.response);
context.app = request.app = response.app = this;
Expand Down
Loading

0 comments on commit 9f327a4

Please sign in to comment.