From 3b972683d70b235d10b32b2d2d67090437c01bc4 Mon Sep 17 00:00:00 2001 From: Christoph Guttandin Date: Tue, 17 Jan 2023 15:43:55 +0100 Subject: [PATCH 1/2] chore(deps): bump json5 from 0.2.6 to 0.4.3 (#1519) --- package.json | 2 +- yarn.lock | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index d90a02551..25147b789 100644 --- a/package.json +++ b/package.json @@ -102,7 +102,7 @@ "node-fetch": "^2.6.7", "outvariant": "^1.3.0", "path-to-regexp": "^6.2.0", - "strict-event-emitter": "^0.2.6", + "strict-event-emitter": "^0.4.3", "type-fest": "^2.19.0", "yargs": "^17.3.1" }, diff --git a/yarn.lock b/yarn.lock index ca69c4631..50ffa4d3d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9230,12 +9230,10 @@ strict-event-emitter@^0.2.4: dependencies: events "^3.3.0" -strict-event-emitter@^0.2.6: - version "0.2.6" - resolved "https://registry.yarnpkg.com/strict-event-emitter/-/strict-event-emitter-0.2.6.tgz#7bb2022bdabcbf0058cec7118a7bbbfd64367366" - integrity sha512-qDZOqEBoNtKLPb/qAutkXUt7hs3zXgYA1xX4pVa+gZHCZZVLr2r81AzHsK5YrQQhRNphMtkOUyAyOr9e1IxJTw== - dependencies: - events "^3.3.0" +strict-event-emitter@^0.4.3: + version "0.4.3" + resolved "https://registry.yarnpkg.com/strict-event-emitter/-/strict-event-emitter-0.4.3.tgz#45d0f75e1dc071eeb7cfbdff864fd2b9172bc257" + integrity sha512-uD0y7Wp3+ifPyfzIS+JrMvSnxFExHytmD5gTkndF1fi8Vrk2uiOY/2At73AwzLHz+RwrchegD8tHdowsfG7raQ== string-argv@^0.3.1: version "0.3.1" From a7cfc73cc0db93b7b328c699bb9e997b5397a69e Mon Sep 17 00:00:00 2001 From: Christoph Guttandin Date: Tue, 17 Jan 2023 15:44:55 +0100 Subject: [PATCH 2/2] fix: replace StrictEventEmitter with Emitter --- src/SetupApi.ts | 22 +++++++++++----------- src/setupWorker/glossary.ts | 4 ++-- src/sharedOptions.ts | 22 ++++++++++------------ src/utils/handleRequest.test.ts | 4 ++-- src/utils/handleRequest.ts | 4 ++-- src/utils/internal/pipeEvents.ts | 8 ++++---- 6 files changed, 31 insertions(+), 33 deletions(-) diff --git a/src/SetupApi.ts b/src/SetupApi.ts index 9895fe91a..d345c82fa 100644 --- a/src/SetupApi.ts +++ b/src/SetupApi.ts @@ -1,5 +1,5 @@ import { invariant } from 'outvariant' -import { EventMapType, StrictEventEmitter } from 'strict-event-emitter' +import { EventMap, Emitter } from 'strict-event-emitter' import { DefaultBodyType, RequestHandler, @@ -14,11 +14,11 @@ import { MockedRequest } from './utils/request/MockedRequest' /** * Generic class for the mock API setup. */ -export abstract class SetupApi { +export abstract class SetupApi { protected initialHandlers: ReadonlyArray protected currentHandlers: Array - protected readonly emitter: StrictEventEmitter - protected readonly publicEmitter: StrictEventEmitter + protected readonly emitter: Emitter + protected readonly publicEmitter: Emitter public readonly events: LifeCycleEventEmitter @@ -28,8 +28,8 @@ export abstract class SetupApi { this.initialHandlers = toReadonlyArray(initialHandlers) this.currentHandlers = [...initialHandlers] - this.emitter = new StrictEventEmitter() - this.publicEmitter = new StrictEventEmitter() + this.emitter = new Emitter() + this.publicEmitter = new Emitter() pipeEvents(this.emitter, this.publicEmitter) this.events = this.createLifeCycleEvents() @@ -81,13 +81,13 @@ export abstract class SetupApi { private createLifeCycleEvents(): LifeCycleEventEmitter { return { - on: (...args) => { - return this.publicEmitter.on(...args) + on: (...args: any[]) => { + return (this.publicEmitter.on as any)(...args) }, - removeListener: (...args) => { - return this.publicEmitter.removeListener(...args) + removeListener: (...args: any[]) => { + return (this.publicEmitter.removeListener as any)(...args) }, - removeAllListeners: (...args: any) => { + removeAllListeners: (...args: any[]) => { return this.publicEmitter.removeAllListeners(...args) }, } diff --git a/src/setupWorker/glossary.ts b/src/setupWorker/glossary.ts index bcfa5b202..145b37668 100644 --- a/src/setupWorker/glossary.ts +++ b/src/setupWorker/glossary.ts @@ -1,5 +1,5 @@ import { FlatHeadersObject } from 'headers-polyfill' -import { StrictEventEmitter } from 'strict-event-emitter' +import { Emitter } from 'strict-event-emitter' import { LifeCycleEventEmitter, LifeCycleEventsMap, @@ -103,7 +103,7 @@ export interface SetupWorkerInternalContext { worker: ServiceWorker | null registration: ServiceWorkerRegistration | null requestHandlers: RequestHandler[] - emitter: StrictEventEmitter + emitter: Emitter keepAliveInterval?: number workerChannel: { /** diff --git a/src/sharedOptions.ts b/src/sharedOptions.ts index f015ceb7c..2ed9f012c 100644 --- a/src/sharedOptions.ts +++ b/src/sharedOptions.ts @@ -1,4 +1,4 @@ -import { StrictEventEmitter } from 'strict-event-emitter' +import { Emitter } from 'strict-event-emitter' import { MockedRequest } from './utils/request/MockedRequest' import { UnhandledRequestStrategy } from './utils/request/onUnhandledRequest' @@ -15,18 +15,16 @@ export interface SharedOptions { } export interface LifeCycleEventsMap { - 'request:start': (request: MockedRequest) => void - 'request:match': (request: MockedRequest) => void - 'request:unhandled': (request: MockedRequest) => void - 'request:end': (request: MockedRequest) => void - 'response:mocked': (response: ResponseType, requestId: string) => void - 'response:bypass': (response: ResponseType, requestId: string) => void - unhandledException: (error: Error, request: MockedRequest) => void + 'request:start': [MockedRequest] + 'request:match': [MockedRequest] + 'request:unhandled': [MockedRequest] + 'request:end': [MockedRequest] + 'response:mocked': [response: ResponseType, requestId: string] + 'response:bypass': [response: ResponseType, requestId: string] + unhandledException: [error: Error, request: MockedRequest] + [key: string]: Array } export type LifeCycleEventEmitter< ResponseType extends Record, -> = Pick< - StrictEventEmitter, - 'on' | 'removeListener' | 'removeAllListeners' -> +> = Pick, 'on' | 'removeListener' | 'removeAllListeners'> diff --git a/src/utils/handleRequest.test.ts b/src/utils/handleRequest.test.ts index 8131107db..38a963585 100644 --- a/src/utils/handleRequest.test.ts +++ b/src/utils/handleRequest.test.ts @@ -1,5 +1,5 @@ import { Headers } from 'headers-polyfill' -import { StrictEventEmitter } from 'strict-event-emitter' +import { Emitter } from 'strict-event-emitter' import { ServerLifecycleEventsMap } from '../node/glossary' import { SharedOptions } from '../sharedOptions' import { RequestHandler } from '../handlers/RequestHandler' @@ -18,7 +18,7 @@ const callbacks: Partial, any>> = { } function setup() { - const emitter = new StrictEventEmitter() + const emitter = new Emitter() const listener = jest.fn() const createMockListener = (name: string) => { diff --git a/src/utils/handleRequest.ts b/src/utils/handleRequest.ts index c3fbe215a..71e4f2ae1 100644 --- a/src/utils/handleRequest.ts +++ b/src/utils/handleRequest.ts @@ -1,5 +1,5 @@ import { until } from '@open-draft/until' -import { StrictEventEmitter } from 'strict-event-emitter' +import { Emitter } from 'strict-event-emitter' import { RequestHandler } from '../handlers/RequestHandler' import { ServerLifecycleEventsMap } from '../node/glossary' import { MockedResponse } from '../response' @@ -45,7 +45,7 @@ export async function handleRequest< request: MockedRequest, handlers: RequestHandler[], options: RequiredDeep, - emitter: StrictEventEmitter, + emitter: Emitter, handleRequestOptions?: HandleRequestOptions, ): Promise { emitter.emit('request:start', request) diff --git a/src/utils/internal/pipeEvents.ts b/src/utils/internal/pipeEvents.ts index b6e1e5128..b9ba47886 100644 --- a/src/utils/internal/pipeEvents.ts +++ b/src/utils/internal/pipeEvents.ts @@ -1,11 +1,11 @@ -import { EventEmitter } from 'stream' +import { Emitter, EventMap } from 'strict-event-emitter' /** * Pipes all emitted events from one emitter to another. */ -export function pipeEvents( - source: EventEmitter, - destination: EventEmitter, +export function pipeEvents( + source: Emitter, + destination: Emitter, ): void { const rawEmit = source.emit