Skip to content

Commit

Permalink
feat: support cjs and esm both (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jun 8, 2024
1 parent 5324225 commit b555b32
Show file tree
Hide file tree
Showing 83 changed files with 382 additions and 270 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ name: Node.js CI
on:
push:
branches: [ master ]

pull_request:
branches: [ master ]

Expand All @@ -13,4 +12,6 @@ jobs:
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
os: 'ubuntu-latest, macos-latest, windows-latest'
version: '16.13.0, 16, 18, 20, 21'
version: '18, 20, 22'
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ dist
lib
!lib/application.test-d.ts
package-lock.json
.tshy*
1 change: 0 additions & 1 deletion .mailmap

This file was deleted.

17 changes: 9 additions & 8 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# @eggjs/koa

@eggjs/koa is forked from [Koa v2.x](https://github.com/koajs/koa/tree/v2.x) for LTS and drop Node.js < 16.13.0 support.
@eggjs/koa is forked from [Koa v2.x](https://github.com/koajs/koa/tree/v2.x) for LTS and drop Node.js < 18.7.0 support.

<img height="240px" src="/docs/logo.png" alt="Koa middleware framework for nodejs"/>

Expand All @@ -19,7 +19,7 @@ Koa is not bundled with any middleware.

## Installation

@eggjs/koa requires __node v16.3.0__ or higher for Node.js LTS support.
@eggjs/koa requires __node v18.7.0__ or higher for Node.js LTS support.

```bash
npm install @eggjs/koa
Expand All @@ -28,11 +28,12 @@ npm install @eggjs/koa
## Hello Koa

```ts
const Koa = require('@eggjs/koa');
const app = new Koa();
import { Application } from '@eggjs/koa';

const app = new Application();

// response
app.use(ctx => {
app.use((ctx) => {
ctx.body = 'Hello Koa';
});

Expand Down Expand Up @@ -86,7 +87,7 @@ Each middleware receives a Koa `Context` object that encapsulates an incoming
http message and the corresponding response to that message. `ctx` is often used
as the parameter name for the context object.

```js
```ts
app.use(async (ctx, next) => {
await next();
});
Expand Down Expand Up @@ -180,6 +181,6 @@ See [AUTHORS](AUTHORS).
- [Wiki](https://github.com/koajs/koa/wiki)
- [中文文档 v2.x](https://github.com/demopark/koa-docs-Zh-CN)

# License
## License

[MIT](https://github.com/eggjs/koa/blob/master/LICENSE)
[MIT](LICENSE)
3 changes: 0 additions & 3 deletions index.ts

This file was deleted.

94 changes: 64 additions & 30 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
{
"name": "@eggjs/koa",
"version": "2.17.0",
"description": "Koa web app framework",
"main": "lib/application.js",
"types": "lib/application.d.ts",
"files": [
"lib/*.d.ts",
"lib/*.js"
],
"engines": {
"node": ">= 18.7.0"
},
"publishConfig": {
"access": "public"
},
"description": "Koa web app framework for https://eggjs.org",
"scripts": {
"test": "egg-bin test",
"ci": "egg-bin cov && npm run tsd",
"test": "npm run lint -- --fix && egg-bin test",
"ci": "npm run lint && egg-bin cov && npm run prepublishOnly",
"lint": "eslint src test",
"tsd": "npm run tsc && tsd",
"authors": "git log --format='%aN <%aE>' | sort -u > AUTHORS",
"clean": "tsc -b --clean",
"tsc": "tsc",
"prepublishOnly": "npm run tsc"
"prepublishOnly": "tshy && tshy-after"
},
"repository": {
"type": "git",
Expand All @@ -34,9 +31,9 @@
"license": "MIT",
"dependencies": {
"accepts": "^1.3.5",
"cache-content-type": "^1.0.0",
"content-disposition": "~0.5.2",
"content-type": "^1.0.4",
"cache-content-type": "^2.0.0",
"content-disposition": "~0.5.4",
"content-type": "^1.0.5",
"cookies": "~0.8.0",
"delegates": "^1.0.0",
"destroy": "^1.0.4",
Expand All @@ -45,32 +42,69 @@
"fresh": "~0.5.2",
"gals": "^1.0.1",
"http-assert": "^1.3.0",
"http-errors": "^1.6.3",
"is-generator-function": "^1.0.7",
"http-errors": "^2.0.0",
"is-type-of": "^2.1.0",
"koa-compose": "^4.1.0",
"on-finished": "^2.3.0",
"only": "~0.0.2",
"parseurl": "^1.3.2",
"statuses": "^1.5.0",
"type-is": "^1.6.16",
"on-finished": "^2.4.1",
"parseurl": "^1.3.3",
"statuses": "^2.0.1",
"type-is": "^1.6.18",
"vary": "^1.1.2"
},
"devDependencies": {
"@eggjs/tsconfig": "^1.3.3",
"@types/content-type": "^1.1.8",
"@types/delegates": "^1.0.3",
"@types/destroy": "^1.0.3",
"@types/encodeurl": "^1.0.2",
"@types/escape-html": "^1.0.4",
"@types/fresh": "^0.5.2",
"@types/http-errors": "^2.0.4",
"@types/koa-compose": "^3.2.8",
"@types/mocha": "^10.0.1",
"@types/node": "^20.2.5",
"@types/on-finished": "^2.3.4",
"@types/parseurl": "^1.3.3",
"@types/statuses": "^2.0.5",
"@types/supertest": "^6.0.2",
"@types/type-is": "^1.6.6",
"@types/vary": "^1.1.3",
"egg-bin": "^6.4.0",
"eslint": "^8.41.0",
"eslint-config-egg": "^13.1.0",
"mm": "^3.3.0",
"supertest": "^3.1.0",
"tsd": "^0.28.1",
"typescript": "^5.0.4"
"tsd": "^0.31.0",
"tshy": "^1.15.1",
"tshy-after": "^1.0.0",
"typescript": "^5.4.5"
},
"engines": {
"node": ">= 16.13.0"
"type": "module",
"tshy": {
"exports": {
"./package.json": "./package.json",
".": "./src/index.ts"
}
},
"publishConfig": {
"access": "public"
}
"exports": {
"./package.json": "./package.json",
".": {
"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"
}
}
},
"files": [
"dist",
"src"
],
"main": "./dist/commonjs/index.js",
"types": "./dist/commonjs/index.d.ts"
}
42 changes: 16 additions & 26 deletions src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,23 @@ import http from 'node:http';
import type { AsyncLocalStorage } from 'node:async_hooks';
import type { IncomingMessage, ServerResponse } from 'node:http';
import { getAsyncLocalStorage } from 'gals';
import isGeneratorFunction from 'is-generator-function';
import { isGeneratorFunction } from 'is-type-of';
import onFinished from 'on-finished';
import statuses from 'statuses';
import compose from 'koa-compose';
import only from 'only';
import { HttpError } from 'http-errors';
import Context from './context';
import Request from './request';
import Response from './response';
import type { ContextDelegation } from './context';
import type { CustomError, ProtoImplClass, AnyProto } from './types';
import Context from './context.js';
import Request from './request.js';
import Response from './response.js';
import type { ContextDelegation } from './context.js';
import type { CustomError, AnyProto } from './types.js';

const debug = debuglog('koa:application');

export type ProtoImplClass<T = object> = new(...args: any[]) => T;
export type Next = () => Promise<void>;
export type MiddlewareFunc = (ctx: ContextDelegation, next: Next) => Promise<void> | void;

export type { ContextDelegation as Context } from './context';
export type { CustomError, ProtoImplClass } from './types';
export type { ContextDelegation as Context } from './context.js';

/**
* Expose `Application` class.
Expand Down Expand Up @@ -108,11 +106,11 @@ export default class Application extends Emitter {
* We only bother showing settings.
*/
toJSON() {
return only(this, [
'subdomainOffset',
'proxy',
'env',
]);
return {
subdomainOffset: this.subdomainOffset,
proxy: this.proxy,
env: this.env,
};
}

/**
Expand Down Expand Up @@ -165,10 +163,10 @@ export default class Application extends Emitter {
}

/**
* return currnect contenxt from async local storage
* return current context from async local storage
*/
get currentContext() {
if (this.ctxStorage) return this.ctxStorage.getStore();
return this.ctxStorage.getStore();
}

/**
Expand All @@ -178,7 +176,7 @@ export default class Application extends Emitter {
async #handleRequest(ctx: ContextDelegation, fnMiddleware: (ctx: ContextDelegation) => Promise<void>) {
const res = ctx.res;
res.statusCode = 404;
const onerror = (err: Error) => ctx.onerror(err);
const onerror = (err: any) => ctx.onerror(err);
onFinished(res, onerror);
try {
await fnMiddleware(ctx);
Expand Down Expand Up @@ -216,14 +214,6 @@ export default class Application extends Emitter {
console.error(`\n${msg.replace(/^/gm, ' ')}\n`);
}

createAsyncCtxStorageMiddleware() {
return async (ctx: ContextDelegation, next: Next) => {
await this.ctxStorage.run(ctx, async () => {
return await next();
});
};
}

/**
* Response helper.
*/
Expand Down
Loading

0 comments on commit b555b32

Please sign in to comment.