fix(cors): return EMPTY stream if CORS request is handled
MODIFY
packages/middleware-cors/src/middleware.ts
packages/middleware-cors/src/middleware.ts
import { isString } from 'util';
-import { tap } from 'rxjs/operators';
+import { of, EMPTY } from 'rxjs';
+import { mergeMap } from 'rxjs/operators';
import { HttpMethod, HttpMiddlewareEffect, HttpStatus } from '@marblejs/core';
-
import { configurePreflightResponse } from './configurePreflightResponse';
import { configureResponse } from './configureResponse';
@@ -26,17 +26,21 @@ export const cors$ = (options: CORSOptions = {}): HttpMiddlewareEffect => (req$)
options = { ...DEFAULT_OPTIONS, ...options };
return req$.pipe(
- tap(req => {
+ mergeMap(req => {
// skip if not a CORS request
if (!isString(req.headers.origin)) {
- return;
+ return of(req);
}
if (req.method === 'OPTIONS') {
configurePreflightResponse(req, req.response, options);
req.response.end();
+
+ return EMPTY;
} else {
configureResponse(req, req.response, options);
+
+ return of(req);
}
}),
);

MODIFY
packages/middleware-cors/src/spec/middleware.spec.ts
packages/middleware-cors/src/spec/middleware.spec.ts
@@ -28,10 +28,14 @@ describe('CORS middleware', () => {
const middleware$ = cors$();
- middleware$(request$, ctx).subscribe(() => {
+ middleware$(request$, ctx).subscribe(
+ () => fail('Should not return any further value.'),
+ undefined,
+ () => {
expect(request.response.setHeader).toBeCalledWith('Access-Control-Allow-Origin', 'fake-origin');
done();
- });
+ },
+ );
});
test('handle CORS request', done => {

fix(cors): return EMPTY stream if CORS request is handled