Skip to content

Commit 0dc6ead

Browse files
committed
add toAsyncFunction on app instance
1 parent 411eac7 commit 0dc6ead

File tree

11 files changed

+140
-132
lines changed

11 files changed

+140
-132
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"is-type-of": "^2.1.0",
4747
"koa-bodyparser": "^4.4.1",
4848
"koa-override": "^4.0.0",
49-
"onelogger": "^1.0.0",
49+
"onelogger": "^1.0.1",
5050
"performance-ms": "^1.1.0",
5151
"sendmessage": "^3.0.1",
5252
"urllib": "^4.6.11",

src/lib/application.ts

+11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Socket } from 'node:net';
55
import { graceful } from 'graceful';
66
import { assign } from 'utility';
77
import { utils as eggUtils } from '@eggjs/core';
8+
import { isGeneratorFunction } from 'is-type-of';
89
import {
910
EggApplicationCore,
1011
type EggApplicationCoreOptions,
@@ -267,6 +268,16 @@ export class Application extends EggApplicationCore {
267268
return this._keys;
268269
}
269270

271+
/**
272+
* @deprecated keep compatible with egg 3.x
273+
*/
274+
toAsyncFunction(fn: (...args: any[]) => any) {
275+
if (isGeneratorFunction(fn)) {
276+
throw new Error('Generator function is not supported');
277+
}
278+
return fn;
279+
}
280+
270281
/**
271282
* bind app's events
272283
*

src/lib/core/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
isClass, isFunction, isGeneratorFunction, isAsyncFunction,
55
} from 'is-type-of';
66

7-
export function convertObject(obj: any, ignore: string | RegExp | (string | RegExp)[]) {
7+
export function convertObject(obj: any, ignore: string | RegExp | (string | RegExp)[] = []) {
88
if (!Array.isArray(ignore)) {
99
ignore = [ ignore ];
1010
}

test/fixtures/apps/multiple-view-engine/app.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
module.exports = app => {
42
app.view.use('ejs', require('./ejs'));
53
app.view.use('nunjucks', require('./nunjucks'));

test/fixtures/apps/multiple-view-engine/ejs.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const { sleep } = require('../../../utils');
1+
const { scheduler } = require('node:timers/promises');
22

33
class EjsView {
44
async render(filename, locals, options) {
5-
await sleep(10);
5+
await scheduler.wait(10);
66
return {
77
filename,
88
locals,
@@ -12,7 +12,7 @@ class EjsView {
1212
}
1313

1414
async renderString(tpl, locals, options) {
15-
await sleep(10);
15+
await scheduler.wait(10);
1616
return {
1717
tpl,
1818
locals,

test/fixtures/apps/multiple-view-engine/nunjucks.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const { sleep } = require('../../../utils');
1+
const { scheduler } = require('node:timers/promises');
22

33
class NunjucksView {
44
async render(filename, locals, options) {
5-
await sleep(10);
5+
await scheduler.wait(10);
66
return {
77
filename,
88
locals,

test/lib/core/logger.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import assert from 'node:assert';
1+
import { strict as assert } from 'node:assert';
22
import path from 'node:path';
33
import fs from 'node:fs';
44
import { scheduler } from 'node:timers/promises';

test/lib/core/router.test.js test/lib/core/router.test.ts

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
'use strict';
1+
import { strict as assert } from 'node:assert';
2+
import { mm } from '@eggjs/mock';
3+
import { MockApplication, createApp } from '../../utils.js';
24

3-
const assert = require('assert');
4-
const mm = require('egg-mock');
5-
const utils = require('../../utils');
6-
7-
describe('test/lib/core/router.test.js', () => {
8-
let app;
5+
describe('test/lib/core/router.test.ts', () => {
6+
let app: MockApplication;
97
before(() => {
10-
app = utils.app({
8+
app = createApp({
119
baseDir: 'apps/router-app',
1210
});
1311
return app.ready();
@@ -128,25 +126,25 @@ describe('test/lib/core/router.test.js', () => {
128126
assert(app.router.url('edit_post', { id: 1 }) === '/posts/1/edit');
129127
});
130128

131-
it('should work with unknow params', () => {
129+
it('should work with unknown params', () => {
132130
assert(
133-
app.router.url('posts', { name: 'foo', page: 2 }) === '/posts?name=foo&page=2'
131+
app.router.url('posts', { name: 'foo', page: 2 }) === '/posts?name=foo&page=2',
134132
);
135133
assert(
136-
app.router.url('posts', { name: 'foo&?', page: 2 }) === '/posts?name=foo%26%3F&page=2'
134+
app.router.url('posts', { name: 'foo&?', page: 2 }) === '/posts?name=foo%26%3F&page=2',
137135
);
138136
assert(
139-
app.router.url('edit_post', { id: 10, page: 2 }) === '/posts/10/edit?page=2'
137+
app.router.url('edit_post', { id: 10, page: 2 }) === '/posts/10/edit?page=2',
140138
);
141139
assert(app.router.url('edit_post', { i: 2, id: 10 }) === '/posts/10/edit?i=2');
142140
assert(
143-
app.router.url('edit_post', { id: 10, page: 2, tags: [ 'chair', 'develop' ] }) === '/posts/10/edit?page=2&tags=chair&tags=develop'
141+
app.router.url('edit_post', { id: 10, page: 2, tags: [ 'chair', 'develop' ] }) === '/posts/10/edit?page=2&tags=chair&tags=develop',
144142
);
145143
assert(
146-
app.router.url('edit_post', { id: [ 10 ], page: [ 2 ], tags: [ 'chair', 'develop' ] }) === '/posts/10/edit?page=2&tags=chair&tags=develop'
144+
app.router.url('edit_post', { id: [ 10 ], page: [ 2 ], tags: [ 'chair', 'develop' ] }) === '/posts/10/edit?page=2&tags=chair&tags=develop',
147145
);
148146
assert(
149-
app.router.url('edit_post', { id: [ 10, 11 ], page: [ 2 ], tags: [ 'chair', 'develop' ] }) === '/posts/10/edit?page=2&tags=chair&tags=develop'
147+
app.router.url('edit_post', { id: [ 10, 11 ], page: [ 2 ], tags: [ 'chair', 'develop' ] }) === '/posts/10/edit?page=2&tags=chair&tags=develop',
150148
);
151149
});
152150
});

test/lib/core/singleton.test.js test/lib/core/singleton.test.ts

+47-44
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
const assert = require('assert');
2-
const Singleton = require('../../../lib/core/singleton');
3-
const { sleep } = require('../../utils');
1+
import { strict as assert } from 'node:assert';
2+
import { scheduler } from 'node:timers/promises';
3+
import { Singleton } from '../../../src/lib/core/singleton.js';
4+
45
class DataService {
5-
constructor(config) {
6+
config: any;
7+
constructor(config: any) {
68
this.config = config;
79
}
810

@@ -11,41 +13,39 @@ class DataService {
1113
}
1214
}
1315

14-
function create(config) {
16+
function create(config: any) {
1517
return new DataService(config);
1618
}
1719

18-
async function asyncCreate(config) {
19-
await sleep(10);
20+
async function asyncCreate(config: any) {
21+
await scheduler.wait(10);
2022
return new DataService(config);
2123
}
2224

23-
describe('test/lib/core/singleton.test.js', () => {
24-
25+
describe('test/lib/core/singleton.test.ts', () => {
2526
afterEach(() => {
26-
delete DataService.prototype.createInstance;
27-
delete DataService.prototype.createInstanceAsync;
27+
delete (DataService as any).prototype.createInstance;
28+
delete (DataService as any).prototype.createInstanceAsync;
2829
});
2930

3031
describe('sync singleton creation tests', () => {
31-
3232
it('should init with client', async () => {
3333
const name = 'dataService';
3434

3535
const clients = [
3636
{ foo: 'bar' },
3737
];
3838
for (const client of clients) {
39-
const app = { config: { dataService: { client } } };
39+
const app: any = { config: { dataService: { client } } };
4040
const singleton = new Singleton({
4141
name,
4242
app,
4343
create,
4444
});
4545
singleton.init();
4646
assert(app.dataService instanceof DataService);
47-
assert(app.dataService.config.foo === 'bar');
48-
assert(typeof app.dataService.createInstance === 'function');
47+
assert.equal(app.dataService.config.foo, 'bar');
48+
assert.equal(typeof app.dataService.createInstance, 'function');
4949
}
5050
});
5151

@@ -57,21 +57,21 @@ describe('test/lib/core/singleton.test.js', () => {
5757
second: { foo: 'bar2' },
5858
};
5959

60-
const app = { config: { dataService: { clients } } };
60+
const app: any = { config: { dataService: { clients } } };
6161
const singleton = new Singleton({
6262
name,
6363
app,
6464
create,
6565
});
6666
singleton.init();
6767
assert(app.dataService instanceof Singleton);
68-
assert(app.dataService.get('first').config.foo === 'bar1');
69-
assert(app.dataService.get('second').config.foo === 'bar2');
70-
assert(typeof app.dataService.createInstance === 'function');
68+
assert.equal(app.dataService.get('first').config.foo, 'bar1');
69+
assert.equal(app.dataService.get('second').config.foo, 'bar2');
70+
assert.equal(typeof app.dataService.createInstance, 'function');
7171
});
7272

7373
it('should client support default', async () => {
74-
const app = {
74+
const app: any = {
7575
config: {
7676
dataService: {
7777
client: { foo: 'bar' },
@@ -88,13 +88,13 @@ describe('test/lib/core/singleton.test.js', () => {
8888
});
8989
singleton.init();
9090
assert(app.dataService instanceof DataService);
91-
assert(app.dataService.config.foo === 'bar');
92-
assert(app.dataService.config.foo1 === 'bar1');
93-
assert(typeof app.dataService.createInstance === 'function');
91+
assert.equal(app.dataService.config.foo, 'bar');
92+
assert.equal(app.dataService.config.foo1, 'bar1');
93+
assert.equal(typeof app.dataService.createInstance, 'function');
9494
});
9595

9696
it('should clients support default', async () => {
97-
const app = {
97+
const app: any = {
9898
config: {
9999
dataService: {
100100
clients: {
@@ -124,7 +124,7 @@ describe('test/lib/core/singleton.test.js', () => {
124124
});
125125

126126
it('should createInstance without client/clients support default', async () => {
127-
const app = {
127+
const app: any = {
128128
config: {
129129
dataService: {
130130
default: { foo: 'bar' },
@@ -148,12 +148,12 @@ describe('test/lib/core/singleton.test.js', () => {
148148
});
149149

150150
it('should work with unextensible', async () => {
151-
function create(config) {
151+
function create(config: any) {
152152
const d = new DataService(config);
153153
Object.preventExtensions(d);
154154
return d;
155155
}
156-
const app = {
156+
const app: any = {
157157
config: {
158158
dataService: {
159159
client: { foo: 'bar' },
@@ -176,12 +176,12 @@ describe('test/lib/core/singleton.test.js', () => {
176176
});
177177

178178
it('should work with frozen', async () => {
179-
function create(config) {
179+
function create(config: any) {
180180
const d = new DataService(config);
181181
Object.freeze(d);
182182
return d;
183183
}
184-
const app = {
184+
const app: any = {
185185
config: {
186186
dataService: {
187187
client: { foo: 'bar' },
@@ -211,17 +211,19 @@ describe('test/lib/core/singleton.test.js', () => {
211211
Object.freeze(d);
212212
return d;
213213
}
214-
const app = {
214+
const app: any = {
215215
config: {
216216
dataService: {
217217
client: { foo: 'bar' },
218218
default: { foo: 'bar' },
219219
},
220220
},
221-
logger: {
222-
warn(msg, name) {
223-
assert(name === 'dataService');
224-
warn = true;
221+
coreLogger: {
222+
warn(_msg: string, name?: string) {
223+
if (name) {
224+
assert.equal(name, 'dataService');
225+
warn = true;
226+
}
225227
},
226228
},
227229
};
@@ -243,12 +245,12 @@ describe('test/lib/core/singleton.test.js', () => {
243245
let success = true;
244246
const name = 'dataService';
245247
const clientName = 'customClient';
246-
function create(config, app, client) {
248+
function create(_config: any, _app: any, client: string) {
247249
if (client !== clientName) {
248250
success = false;
249251
}
250252
}
251-
const app = {
253+
const app: any = {
252254
config: {
253255
dataService: {
254256
clients: {
@@ -276,7 +278,7 @@ describe('test/lib/core/singleton.test.js', () => {
276278
{ foo: 'bar' },
277279
];
278280
for (const client of clients) {
279-
const app = { config: { dataService: { client } } };
281+
const app: any = { config: { dataService: { client } } };
280282
const singleton = new Singleton({
281283
name,
282284
app,
@@ -298,7 +300,7 @@ describe('test/lib/core/singleton.test.js', () => {
298300
second: { foo: 'bar2' },
299301
};
300302

301-
const app = { config: { dataService: { clients } } };
303+
const app: any = { config: { dataService: { clients } } };
302304
const singleton = new Singleton({
303305
name,
304306
app,
@@ -312,7 +314,7 @@ describe('test/lib/core/singleton.test.js', () => {
312314
});
313315

314316
it('should createInstanceAsync without client/clients support default', async () => {
315-
const app = {
317+
const app: any = {
316318
config: {
317319
dataService: {
318320
default: { foo: 'bar' },
@@ -336,7 +338,7 @@ describe('test/lib/core/singleton.test.js', () => {
336338
});
337339

338340
it('should createInstanceAsync throw error', async () => {
339-
const app = {
341+
const app: any = {
340342
config: {
341343
dataService: {
342344
default: { foo: 'bar' },
@@ -356,8 +358,9 @@ describe('test/lib/core/singleton.test.js', () => {
356358
try {
357359
app.dataService = await app.dataService.createInstance({ foo1: 'bar1' });
358360
throw new Error('should not execute');
359-
} catch (err) {
360-
assert(err.message === 'egg:singleton dataService only support create asynchronous, please use createInstanceAsync');
361+
} catch (err: any) {
362+
assert.equal(err.message,
363+
'egg:singleton dataService only support create asynchronous, please use createInstanceAsync');
361364
}
362365
});
363366

@@ -366,12 +369,12 @@ describe('test/lib/core/singleton.test.js', () => {
366369
const name = 'dataService';
367370
const clientName = 'customClient';
368371

369-
async function _create(config, app, client) {
372+
async function _create(_config: any, _app: any, client: string) {
370373
if (client !== clientName) {
371374
success = false;
372375
}
373376
}
374-
const app = {
377+
const app: any = {
375378
config: {
376379
dataService: {
377380
clients: {

0 commit comments

Comments
 (0)