File size: 3,067 Bytes
c592d77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
 * Shared utilities for MCP tools that communicate with the browser.
 * This module provides a common infrastructure for request-response
 * communication between MCP endpoints and browser sessions via HMR.
 */ "use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
0 && (module.exports = {
    DEFAULT_BROWSER_REQUEST_TIMEOUT_MS: null,
    createBrowserRequest: null,
    handleBrowserPageResponse: null
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: all[name]
    });
}
_export(exports, {
    DEFAULT_BROWSER_REQUEST_TIMEOUT_MS: function() {
        return DEFAULT_BROWSER_REQUEST_TIMEOUT_MS;
    },
    createBrowserRequest: function() {
        return createBrowserRequest;
    },
    handleBrowserPageResponse: function() {
        return handleBrowserPageResponse;
    }
});
const _nanoid = require("next/dist/compiled/nanoid");
const DEFAULT_BROWSER_REQUEST_TIMEOUT_MS = 5000;
const pendingRequests = new Map();
function createBrowserRequest(messageType, sendHmrMessage, getActiveConnectionCount, timeoutMs) {
    const connectionCount = getActiveConnectionCount();
    if (connectionCount === 0) {
        return Promise.resolve([]);
    }
    const requestId = `mcp-${messageType}-${(0, _nanoid.nanoid)()}`;
    const responsePromise = new Promise((resolve, reject)=>{
        const timeout = setTimeout(()=>{
            const pending = pendingRequests.get(requestId);
            if (pending && pending.responses.length > 0) {
                resolve(pending.responses);
            } else {
                reject(Object.defineProperty(new Error(`Timeout waiting for response from frontend. The browser may not be responding to HMR messages.`), "__NEXT_ERROR_CODE", {
                    value: "E825",
                    enumerable: false,
                    configurable: true
                }));
            }
            pendingRequests.delete(requestId);
        }, timeoutMs);
        pendingRequests.set(requestId, {
            responses: [],
            expectedCount: connectionCount,
            resolve: resolve,
            reject,
            timeout
        });
    });
    sendHmrMessage({
        type: messageType,
        requestId
    });
    return responsePromise;
}
function handleBrowserPageResponse(requestId, data, url) {
    if (!url) {
        throw Object.defineProperty(new Error('URL is required in MCP browser response. This is a bug in Next.js.'), "__NEXT_ERROR_CODE", {
            value: "E824",
            enumerable: false,
            configurable: true
        });
    }
    const pending = pendingRequests.get(requestId);
    if (pending) {
        pending.responses.push({
            url,
            data
        });
        if (pending.responses.length >= pending.expectedCount) {
            clearTimeout(pending.timeout);
            pending.resolve(pending.responses);
            pendingRequests.delete(requestId);
        }
    }
}

//# sourceMappingURL=browser-communication.js.map