MCPcopy Create free account
hub / github.com/appium/appium / configureHttp

Function configureHttp

packages/http-server/lib/server.ts:288–381  ·  view source on GitHub ↗

* Attaches Appium-specific behavior to the HTTP server and returns it as code AppiumServer. * Mutates the `httpServer` parameter. * * @param opts - Configuration options * @returns The same server instance typed as AppiumServer

({
  httpServer,
  basePath,
  reject,
  allowCors,
  keepAliveTimeout,
  gracefulShutdownTimeout,
}: ConfigureHttpOpts)

Source from the content-addressed store, hash-verified

286 * @returns The same server instance typed as AppiumServer
287 */
288function configureHttp({
289 httpServer,
290 basePath,
291 reject,
292 allowCors,
293 keepAliveTimeout,
294 gracefulShutdownTimeout,
295}: ConfigureHttpOpts): AppiumServer {
296 const appiumServer = Object.assign(httpServer, {
297 webSocketsMapping: {},
298 frontRouter: express.Router(),
299 basePath,
300 addWebSocketHandler,
301 removeWebSocketHandler,
302 removeAllWebSocketHandlers,
303 getWebSocketHandlers,
304 isSecure: () => httpServer instanceof https.Server,
305 }) as unknown as AppiumServer;
306
307 // This avoids Express middleware timeout issues with long-lived WebSocket connections
308 // See: https://github.com/appium/appium/issues/20760
309 // See: https://github.com/nodejs/node/pull/59824
310 if (hasShouldUpgradeCallback(httpServer)) {
311 // shouldUpgradeCallback only returns a boolean to indicate if the upgrade should proceed
312 (
313 appiumServer as unknown as {shouldUpgradeCallback?: (req: http.IncomingMessage) => boolean}
314 ).shouldUpgradeCallback = (req) => String(req.headers?.upgrade ?? '').toLowerCase() === 'websocket';
315 appiumServer.on('upgrade', (req, socket, head) => {
316 if (
317 !tryHandleWebSocketUpgrade({
318 req,
319 socket,
320 head,
321 webSocketsMapping: appiumServer.webSocketsMapping,
322 allowCors,
323 })
324 ) {
325 socket.destroy();
326 }
327 });
328 }
329
330 // http.Server.close() only stops new connections, but we need to wait until
331 // all connections are closed and the `close` event is emitted
332 const originalClose = appiumServer.close.bind(appiumServer) as (callback?: (err?: Error | null) => void) => void;
333 appiumServer.close = async () =>
334 await new Promise<void>((_resolve, _reject) => {
335 log.info('Closing Appium HTTP server');
336 const timer = new timing.Timer().start();
337 const onTimeout = setTimeout(() => {
338 if ((gracefulShutdownTimeout ?? 0) > 0) {
339 log.info(
340 `Not all active connections have been closed within ${gracefulShutdownTimeout}ms. ` +
341 `This timeout might be customized by the --shutdown-timeout command line ` +
342 `argument. Closing the server anyway.`,
343 );
344 }
345 process.exit(process.exitCode ?? 0);

Callers 1

serverFunction · 0.85

Calls 5

hasShouldUpgradeCallbackFunction · 0.85
startMethod · 0.80
infoMethod · 0.65
errorMethod · 0.65

Tested by

no test coverage detected