Skip to content

Commit

Permalink
feat: use http for remote request handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Dec 13, 2024
1 parent 657d7b1 commit 5264ec4
Show file tree
Hide file tree
Showing 11 changed files with 274 additions and 738 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,6 @@
"is-node-process": "^1.2.0",
"outvariant": "^1.4.3",
"path-to-regexp": "^6.3.0",
"socket.io": "^4.8.1",
"socket.io-client": "^4.8.1",
"strict-event-emitter": "^0.5.1",
"type-fest": "^4.26.1",
"yargs": "^17.7.2"
Expand Down
42 changes: 0 additions & 42 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 21 additions & 83 deletions src/core/handlers/RemoteRequestHandler.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import type { Socket } from 'socket.io-client'
import { createRequestId } from '@mswjs/interceptors'
import { DeferredPromise } from '@open-draft/deferred-promise'
import type { SyncServerEventsMap } from '../../node/setupRemoteServer'
import {
serializeRequest,
deserializeResponse,
} from '../utils/request/serializeUtils'
import type { ResponseResolutionContext } from '../utils/executeHandlers'
import {
RequestHandler,
type ResponseResolver,
type RequestHandlerDefaultInfo,
} from './RequestHandler'
import { RemoteClient } from 'node/setupRemoteServer'

interface RemoteRequestHandlerParsedResult {
response: Response | undefined
Expand All @@ -26,25 +20,23 @@ export class RemoteRequestHandler extends RequestHandler<
RemoteRequestHandlerParsedResult,
RemoteRequestHandlerResolverExtras
> {
private socket: Socket<SyncServerEventsMap>
private contextId?: string

constructor(args: {
socket: Socket<SyncServerEventsMap>
contextId?: string
}) {
protected remoteClient: RemoteClient
protected contextId?: string

constructor(
readonly args: {
remoteClient: RemoteClient
contextId?: string
},
) {
super({
info: {
header: 'RemoteRequestHandler',
},
resolver() {},
})

console.log('[RemoteRequestHandler] constructor', {
contextId: args.contextId,
})

this.socket = args.socket
this.remoteClient = args.remoteClient
this.contextId = args.contextId
}

Expand All @@ -53,49 +45,19 @@ export class RemoteRequestHandler extends RequestHandler<
resolutionContext?: ResponseResolutionContext
}): Promise<RemoteRequestHandlerParsedResult> {
const parsedResult = await super.parse(args)
const responsePromise = new DeferredPromise<Response | undefined>()

// eslint-disable-next-line no-console
console.log(
'[RemoteRequestHandler] REQUEST!',
args.request.method,
args.request.url,
this.contextId,
this.args.contextId,
)

// If the socket is disconnected, or gets disconnected,
// skip this remote handler and continue with the request locally.
if (this.socket.disconnected) {
console.log(
'[RemoteRequestHandler] socket already disconnected, skipping...',
)

return {
...parsedResult,
response: undefined,
}
if (!this.remoteClient.connected) {
return parsedResult
}

this.socket.once('disconnect', () => {
console.log('[RemoteRequestHandler] socket disconnected, skipping...')
responsePromise.resolve(undefined)
})

console.log(
'[RemoteRequestHandler] sending request to remote...',
new Date(),
)

// this.socket.send(JSON.stringify({ a: { b: { c: 1 } } }))
// this.socket.emit(
// 'foo',
// JSON.stringify({
// requestId: createRequestId(),
// contextId: this.contextId,
// serializeRequest: await serializeRequest(args.request),
// }),
// )

/**
* @note Remote request handler is special.
* It cannot await the mocked response from the remote process in
Expand All @@ -105,38 +67,14 @@ export class RemoteRequestHandler extends RequestHandler<
* Instead, the remote handler await the mocked response during the
* parsing phase since that's the only async phase before predicate.
*/
debugger
this.socket.emit(
'request',
{
requestId: createRequestId(),
serializedRequest: await serializeRequest(args.request),
contextId: this.contextId,
},
(serializedResponse) => {
// Wait for the remote server to acknowledge this request with
// a response before continuing with the request handling.
responsePromise.resolve(
serializedResponse
? deserializeResponse(serializedResponse)
: undefined,
)
},
)

/**
* @todo Handle timeouts.
* @todo Handle socket errors.
*/

console.log('[RemoteRequestHandler] waiting for response from remote...')
const response = await this.remoteClient.handleRequest({
contextId: this.contextId,
requestId: createRequestId(),
request: args.request,
})

return {
...parsedResult,
// Set the received mocked response on the parsed result so it
// can be accessed in the predicate and response resolver functions.
response: await responsePromise,
}
parsedResult.response = response
return parsedResult
}

predicate(args: {
Expand Down
99 changes: 0 additions & 99 deletions src/core/utils/internal/emitterUtils.ts

This file was deleted.

24 changes: 0 additions & 24 deletions src/core/utils/logging/requestToLoggableObject.test.ts

This file was deleted.

Loading

0 comments on commit 5264ec4

Please sign in to comment.