MCPcopy Index your code
hub / github.com/browserless/browserless / handler

Method handler

src/shared/function.http.ts:59–95  ·  view source on GitHub ↗
(
    req: Request,
    res: ServerResponse,
    logger: Logger,
    browser: BrowserInstance,
  )

Source from the content-addressed store, hash-verified

57 path = [HTTPRoutes.chromiumFunction, HTTPRoutes.function];
58 tags = [APITags.browserAPI];
59 async handler(
60 req: Request,
61 res: ServerResponse,
62 logger: Logger,
63 browser: BrowserInstance,
64 ): Promise<void> {
65 const config = this.config();
66 const timeout = req.parsed.searchParams.get('timeout');
67 const handler = functionHandler(config, logger, {
68 protocolTimeout: timeout ? +timeout : undefined,
69 });
70 const { contentType, payload, page } = await handler(req, browser);
71
72 logger.debug(`Got function response of "${contentType}"`);
73 page.removeAllListeners();
74 page.close().catch(() => {});
75
76 if (contentType === 'uint8array') {
77 const response = new Uint8Array(payload as Buffer);
78 const type = ((await fileTypeFromBuffer(response)) || { mime: undefined })
79 .mime;
80
81 if (!type) {
82 throw new BadRequest(`Couldn't determine function's response type.`);
83 } else {
84 logger.debug(`Sending file-type response of "${type}"`);
85 const readStream = new Stream.PassThrough();
86 readStream.end(response);
87 res.setHeader('Content-Type', type);
88 return new Promise((r) => readStream.pipe(res).once('close', r));
89 }
90 } else {
91 writeResponse(res, 200, payload as string, contentType as contentTypes);
92 }
93
94 return;
95 }
96}

Callers

nothing calls this directly

Calls 5

handlerFunction · 0.85
writeResponseFunction · 0.85
getMethod · 0.80
debugMethod · 0.80
closeMethod · 0.45

Tested by

no test coverage detected