feat(core): #createServer promisified
MODIFY
packages/@integration/tsconfig.json
packages/@integration/tsconfig.json
"lib": [
"dom",
"es2018"
- ],
+ ]
},
"inputs": [
"src"

MODIFY
packages/core/src/+internal/testing/http.helper.ts
packages/core/src/+internal/testing/http.helper.ts
-import * as http from 'http';
import {
HttpRequest,
HttpResponse,
@@ -48,10 +47,3 @@ export const createHttpResponse = (data: HttpResponseMockParams = {}) =>
new class extends EventEmitter {
statusCode = data.statusCode;
} as any as HttpResponse;
-
-export const mockHttpServer = (mocks: HttpServerMocks = {}) =>
- jest.spyOn(http, 'createServer').mockImplementation(jest.fn(() => ({
- listen: mocks.listen || jest.fn(),
- close: mocks.close || jest.fn(callback => callback()),
- on: mocks.on || jest.fn(),
- })));

MODIFY
packages/core/src/server/server.factory.ts
packages/core/src/server/server.factory.ts
@@ -29,11 +29,15 @@ export const createServer = (config: CreateServerConfig): Server => {
event$(serverEventSubject.pipe(takeWhile(e => !isCloseEvent(e))), server, metadata).subscribe();
}
- const listen = () => server.listen(port, hostname);
+ const listen = () => new Promise<https.Server | http.Server>((resolve, reject) => {
+ const runningServer = server.listen(port, hostname);
+ runningServer.once('listening', () => resolve(runningServer));
+ runningServer.once('error', error => reject(error));
+ });
listen.config = {
+ ...httpListenerWithContext.config,
server,
- routing: httpListenerWithContext.config.routing,
};
return listen;

MODIFY
packages/core/src/server/server.interface.ts
packages/core/src/server/server.interface.ts
@@ -15,7 +15,7 @@ export interface CreateServerConfig {
}
export interface Server {
- (): https.Server | http.Server;
+ (): Promise<https.Server | http.Server>;
config: ServerConfig;
}

MODIFY
packages/core/src/server/specs/server.factory.spec.ts
packages/core/src/server/specs/server.factory.spec.ts
@@ -18,8 +18,8 @@ import {
isClientErrorEvent,
isRequestEvent,
} from '../server.event';
-import { EffectFactory } from '../../effects/effects.factory';
import { EventEmitter } from 'events';
+import { r } from '../../router/router.ixbuilder';
describe('#createServer', () => {
let server: https.Server | http.Server;
@@ -30,46 +30,39 @@ describe('#createServer', () => {
afterEach(done => {
if (server) {
- server.close(() => done());
+ server.close(done);
} else {
done();
}
});
- test('creates http server and starts listening', done => {
+ test('creates http server and starts listening', async () => {
// given
const hostname = '127.0.0.1';
const app = httpListener({ effects: [] });
// when
- server = createServer({
+ server = await createServer({
hostname,
httpListener: app,
})();
- server.on('error', console.error);
-
- server.on('listening', () => {
+ // then
expect(server.listening).toBe(true);
- done();
- });
});
- test('creates http server and starts listening without specified port and hostname', done => {
+ test('creates http server and starts listening without specified port and hostname', async () => {
// given
const app = httpListener({ effects: [] });
// when
- server = createServer({ httpListener: app })();
+ server = await createServer({ httpListener: app })();
// then
- server.on('listening', () => {
expect(server.listening).toBe(true);
- done();
- });
});
- test('creates https server', done => {
+ test('creates https server', async () => {
// given
const app = httpListener({ effects: [] });
const httpsOptions: https.ServerOptions = {
@@ -78,29 +71,26 @@ describe('#createServer', () => {
};
// when
- server = createServer({
+ server = await createServer({
httpListener: app,
options: { httpsOptions },
})();
// then
- setTimeout(() => {
expect(server.listening).toBe(true);
- server.close(done);
- }, 100);
});
- test('returns server and routing', () => {
+ test('returns server and routing', async () => {
// given
- const effect$ = EffectFactory
- .matchPath('/')
- .matchType('GET')
- .use(req$ => req$.pipe(mapTo({ status: 200 })));
+ const effect$ = r.pipe(
+ r.matchPath('/'),
+ r.matchType('GET'),
+ r.useEffect(req$ => req$.pipe(mapTo({ status: 200 }))));
const app = httpListener({ effects: [effect$] });
// when
const marbleServer = createServer({ httpListener: app });
- server = marbleServer();
+ server = await marbleServer();
// then
expect(marbleServer.config).toBeDefined();
@@ -113,12 +103,12 @@ describe('#createServer', () => {
).toBeDefined();
});
- test(`emits server events`, done => {
+ test(`emits server events`, async done => {
// given
const app = httpListener({ effects: [] });
// then
- server = createServer({
+ server = await createServer({
httpListener: app,
event$: event$ => forkJoin(
event$.pipe(filter(isErrorEvent), take(1)),

MODIFY
packages/websockets/tsconfig.json
packages/websockets/tsconfig.json
{
"extends": "../../tsconfig.json",
"compilerOptions": {
- "outDir": "./dist/",
+ "outDir": "./dist/"
},
- "include": ["src/**/*.ts"],
+ "include": ["src/**/*.ts"]
}

feat(core): #createServer promisified