bitrix24-mcp-bridge 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,11 +11,20 @@
11
11
  for (let [k, v] of Object.entries(params)) add(k, v);
12
12
  return out.toString();
13
13
  }
14
+ function applyPathParams(endpoint, params) {
15
+ let rest = { ...params };
16
+ return { endpoint: endpoint.replace(/\{(\w+)\}/g, (_match, key) => {
17
+ let value = rest[key];
18
+ if (value == null || value === "")
19
+ throw new Error(`missing path param "${key}" for endpoint ${endpoint}`);
20
+ return delete rest[key], encodeURIComponent(String(value));
21
+ }), rest };
22
+ }
14
23
  function buildRequest(origin, req, sessid) {
15
- let q = req.action ? `?action=${encodeURIComponent(req.action)}` : "", url = `${origin}${req.endpoint}${q}`;
24
+ let { endpoint, rest } = applyPathParams(req.endpoint, req.params), q = req.action ? `?action=${encodeURIComponent(req.action)}` : "", url = `${origin}${endpoint}${q}`;
16
25
  if (req.bodyType === "json")
17
- return { url, body: JSON.stringify(req.params), contentType: "application/json" };
18
- let body = encodeForm({ ...req.params, sessid });
26
+ return { url, body: JSON.stringify(rest), contentType: "application/json" };
27
+ let body = encodeForm({ ...rest, sessid });
19
28
  return { url, body, contentType: "application/x-www-form-urlencoded" };
20
29
  }
21
30
  function interpret(json) {
@@ -26,6 +35,30 @@
26
35
  }
27
36
  return { ok: !0, data: json };
28
37
  }
38
+ var BINARY_CHUNK_BYTES = 1024 * 1024;
39
+ function toBase64(bytes) {
40
+ let binary = "";
41
+ for (let i = 0; i < bytes.length; i += 32768)
42
+ binary += String.fromCharCode(...bytes.subarray(i, i + 32768));
43
+ return btoa(binary);
44
+ }
45
+ function looksLikeLoginPage(contentType, contentDisposition) {
46
+ return /^\s*text\/html/i.test(contentType) && !/attachment/i.test(contentDisposition ?? "");
47
+ }
48
+ function fileNameFromDisposition(contentDisposition) {
49
+ if (!contentDisposition) return null;
50
+ let encoded = /filename\*=(?:UTF-8'')?([^;]+)/i.exec(contentDisposition);
51
+ if (encoded)
52
+ try {
53
+ return decodeURIComponent(encoded[1].trim().replace(/^"|"$/g, ""));
54
+ } catch {
55
+ }
56
+ let plain = /filename="?([^";]+)"?/i.exec(contentDisposition);
57
+ return plain ? plain[1].trim() : null;
58
+ }
59
+ function interpretText(text, contentType, status) {
60
+ return status >= 400 ? { ok: !1, error: `HTTP ${status}` } : text.trim() ? { ok: !0, data: { contentType, text } } : { ok: !1, error: "empty response" };
61
+ }
29
62
 
30
63
  // extension/src/bridge-protocol.ts
31
64
  function parseConfig(raw) {
@@ -56,7 +89,7 @@
56
89
  }
57
90
 
58
91
  // extension/src/connector.ts
59
- var ORIGIN = location.origin, SESSID_TIMEOUT_MS = 2e3, RECONNECT_MS = 3e3, socket = null, nonceSeq = 0;
92
+ var ORIGIN = location.origin, SESSID_TIMEOUT_MS = 2e3, RECONNECT_MS = 3e3, MAX_DOWNLOAD_BYTES = 512 * 1024 * 1024, socket = null, nonceSeq = 0;
60
93
  function freshSessid() {
61
94
  let nonce = `${Date.now()}-${nonceSeq++}-${Math.random().toString(36).slice(2)}`;
62
95
  return new Promise((resolve) => {
@@ -72,23 +105,48 @@
72
105
  window.addEventListener("message", onMsg), window.postMessage(buildSessidRequest(nonce), ORIGIN);
73
106
  });
74
107
  }
75
- async function handleCall(req) {
108
+ async function fetchPortal(req) {
76
109
  let sessid = await freshSessid();
77
- if (!sessid) return { ok: !1, error: "session context not ready \u2014 open a normal Bitrix24 portal tab" };
110
+ if (!sessid) return { error: "session context not ready \u2014 open a normal Bitrix24 portal tab" };
78
111
  let { url, body, contentType } = buildRequest(ORIGIN, req, sessid), init = {
79
112
  method: req.method,
80
113
  credentials: "include",
81
114
  // sessid rides the CSRF header for all ajax (v2 form + json); form bodies also carry it inline.
82
115
  headers: { "Content-Type": contentType, "X-Bitrix-Csrf-Token": sessid }
83
116
  }, finalUrl = url;
84
- req.method === "POST" ? init.body = body : finalUrl += (url.includes("?") ? "&" : "?") + body;
85
- let json = await (await fetch(finalUrl, init)).json();
117
+ return req.method === "POST" ? init.body = body : finalUrl += (url.includes("?") ? "&" : "?") + body, fetch(finalUrl, init);
118
+ }
119
+ async function handleCall(req) {
120
+ let resp = await fetchPortal(req);
121
+ if (!(resp instanceof Response)) return { ok: !1, error: resp.error };
122
+ if (req.responseType === "text")
123
+ return interpretText(await resp.text(), resp.headers.get("content-type") ?? "", resp.status);
124
+ let json = await resp.json();
86
125
  return interpret(json);
87
126
  }
127
+ async function handleBinaryCall(req, ws) {
128
+ let resp = await fetchPortal(req);
129
+ if (!(resp instanceof Response)) return { ok: !1, error: resp.error };
130
+ if (resp.status >= 400) return { ok: !1, error: `HTTP ${resp.status}` };
131
+ let contentType = resp.headers.get("content-type") ?? "", disposition = resp.headers.get("content-disposition");
132
+ if (looksLikeLoginPage(contentType, disposition))
133
+ return { ok: !1, error: "portal answered with an HTML page, not a file \u2014 session likely expired" };
134
+ let bytes = new Uint8Array(await resp.arrayBuffer());
135
+ if (bytes.length > MAX_DOWNLOAD_BYTES)
136
+ return { ok: !1, error: `file is ${bytes.length} bytes, over the ${MAX_DOWNLOAD_BYTES} limit` };
137
+ let fileName = fileNameFromDisposition(disposition);
138
+ ws.send(JSON.stringify({ type: "binary-begin", id: req.id, contentType, bytes: bytes.length, fileName }));
139
+ let seq = 0;
140
+ for (let at = 0; at < bytes.length; at += BINARY_CHUNK_BYTES) {
141
+ let slice = bytes.subarray(at, at + BINARY_CHUNK_BYTES);
142
+ ws.send(JSON.stringify({ type: "binary-chunk", id: req.id, seq: seq++, data: toBase64(slice) }));
143
+ }
144
+ return { ok: !0, data: { contentType, bytes: bytes.length, fileName, chunks: seq } };
145
+ }
88
146
  function connect(config) {
89
147
  let ws = new WebSocket(`ws://127.0.0.1:${config.port}`);
90
148
  ws.addEventListener("open", () => {
91
- ws.send(JSON.stringify({ type: "auth", token: config.token })), socket = ws;
149
+ ws.send(JSON.stringify({ type: "auth", token: config.token, version: chrome.runtime.getManifest().version })), socket = ws;
92
150
  }), ws.addEventListener("message", async (ev) => {
93
151
  let req;
94
152
  try {
@@ -99,7 +157,7 @@
99
157
  }
100
158
  if (req.type === "call")
101
159
  try {
102
- let r = await handleCall(req);
160
+ let r = req.responseType === "binary" ? await handleBinaryCall(req, ws) : await handleCall(req);
103
161
  ws.send(JSON.stringify({ type: "result", id: req.id, ok: r.ok, data: r.data, error: r.error }));
104
162
  } catch (e) {
105
163
  ws.send(JSON.stringify({ type: "result", id: req.id, ok: !1, error: String(e) }));
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/bridge-core.ts", "../src/bridge-protocol.ts", "../src/connector.ts"],
4
- "sourcesContent": ["// Pure, browser-agnostic \u2014 unit-tested.\n\n// Wire types come from the shared single source; re-exported so downstream\n// extension modules keep importing them from here.\nimport type { CallRequest } from \"../../shared/wire.ts\";\nexport type { CallRequest, CapturedEntry } from \"../../shared/wire.ts\";\n\n// Extension-only: the shape handleCall returns to the bridge (not a wire message).\nexport interface InterpretResult {\n ok: boolean;\n data?: unknown;\n error?: string;\n}\n\nexport function encodeForm(params: Record<string, unknown>): string {\n const out = new URLSearchParams();\n const add = (key: string, val: unknown): void => {\n if (val === null || val === undefined) return;\n if (Array.isArray(val)) val.forEach((v, i) => add(`${key}[${i}]`, v));\n else if (typeof val === \"object\") for (const [k, v] of Object.entries(val)) add(`${key}[${k}]`, v);\n else out.set(key, String(val));\n };\n for (const [k, v] of Object.entries(params)) add(k, v);\n return out.toString();\n}\n\nexport function buildRequest(\n origin: string,\n req: CallRequest,\n sessid: string,\n): { url: string; body: string; contentType: string } {\n const q = req.action ? `?action=${encodeURIComponent(req.action)}` : \"\";\n const url = `${origin}${req.endpoint}${q}`;\n // JSON actions (ui.entityselector.*, tasks.v2.*) send a JSON body; sessid rides the\n // X-Bitrix-Csrf-Token header (added by the caller), never the body.\n if (req.bodyType === \"json\") {\n return { url, body: JSON.stringify(req.params), contentType: \"application/json\" };\n }\n const body = encodeForm({ ...req.params, sessid });\n return { url, body, contentType: \"application/x-www-form-urlencoded\" };\n}\n\n// Bitrix wraps errors in HTTP 200: { status:\"error\", errors:[{code}] } or { error, error_description }.\n// NOTE: successful ajax responses carry an EMPTY `errors: []` array \u2014 a truthy value in JS \u2014 so we\n// must treat only a NON-EMPTY errors list (or status:\"error\"/a top-level error) as a real failure.\nexport function interpret(json: any): InterpretResult {\n const list = json && (Array.isArray(json.errors) ? json.errors : json.error ? [{ code: json.error }] : []);\n const hasErrors = Array.isArray(list) && list.length > 0;\n if (json && (json.status === \"error\" || hasErrors)) {\n const first = list[0];\n const code = (first && (first.code || first.message)) || json.error || \"bitrix_error\";\n const description = json.error_description || (first && first.message);\n const error = description && description !== code ? `${code}: ${description}` : code;\n return { ok: false, error, data: json };\n }\n return { ok: true, data: json };\n}\n", "// Pure, browser-agnostic \u2014 unit-tested. No DOM / chrome / window.BX here.\n//\n// Two concerns live together because both are the *contract* between the extension's\n// two content scripts and the packaged config, and both are pure/testable:\n// 1) parseConfig \u2014 validate the per-user config.json ({ token, port }).\n// 2) the postMessage protocol \u2014 the envelopes the ISOLATED connector and the MAIN\n// script exchange over window.postMessage (sessid request/response, and \u2014 in the\n// capture build \u2014 MAIN-observed capture entries forwarded to the connector).\n\nimport type { CapturedEntry } from \"./bridge-core.ts\";\n\nexport interface BridgeConfig {\n token: string;\n port: number;\n}\n\n/**\n * Parse and validate the packaged config.json body. Throws with a clear,\n * user-facing message (mentioning config.json) on any malformed input so the\n * connector can surface why it failed to start.\n */\nexport function parseConfig(raw: string): BridgeConfig {\n let doc: unknown;\n try {\n doc = JSON.parse(raw);\n } catch {\n throw new Error(\"config.json is not valid JSON\");\n }\n if (typeof doc !== \"object\" || doc === null) {\n throw new Error(\"config.json must be an object like { token, port }\");\n }\n const obj = doc as Record<string, unknown>;\n const token = typeof obj.token === \"string\" ? obj.token.trim() : \"\";\n if (!token) throw new Error(\"config.json is missing a non-empty `token`\");\n const port = Number(obj.port);\n if (!Number.isInteger(port) || port < 1 || port > 65535) {\n throw new Error(\"config.json is missing a numeric `port`\");\n }\n return { token, port };\n}\n\n// --- sessid postMessage protocol -------------------------------------------\n//\n// window.postMessage is delivered to BOTH the MAIN and ISOLATED worlds of the same\n// frame (they share the DOM window), and each world also receives its own posts. So\n// every envelope is tagged with a fixed `source` and a `kind`, and each side ignores\n// anything that isn't addressed to it. Responses additionally carry the request nonce\n// so the connector can match a reply to the exact in-flight request it issued.\n\nexport const BRIDGE_MSG_SOURCE = \"bitrix24-mcp-bridge\";\n\nexport interface SessidRequestMsg {\n source: typeof BRIDGE_MSG_SOURCE;\n kind: \"sessid-request\";\n nonce: string;\n}\n\nexport interface SessidResponseMsg {\n source: typeof BRIDGE_MSG_SOURCE;\n kind: \"sessid-response\";\n nonce: string;\n sessid: string;\n}\n\nexport function buildSessidRequest(nonce: string): SessidRequestMsg {\n return { source: BRIDGE_MSG_SOURCE, kind: \"sessid-request\", nonce };\n}\n\nexport function buildSessidResponse(nonce: string, sessid: string): SessidResponseMsg {\n return { source: BRIDGE_MSG_SOURCE, kind: \"sessid-response\", nonce, sessid };\n}\n\nfunction isRecord(v: unknown): v is Record<string, unknown> {\n return typeof v === \"object\" && v !== null;\n}\n\n/** For the MAIN shim: a well-formed request from our connector, or null. */\nexport function parseSessidRequest(data: unknown): SessidRequestMsg | null {\n if (!isRecord(data)) return null;\n if (data.source !== BRIDGE_MSG_SOURCE || data.kind !== \"sessid-request\") return null;\n if (typeof data.nonce !== \"string\") return null;\n return { source: BRIDGE_MSG_SOURCE, kind: \"sessid-request\", nonce: data.nonce };\n}\n\n/** For the ISOLATED connector: a response matching `expectedNonce`, or null. */\nexport function parseSessidResponse(data: unknown, expectedNonce: string): SessidResponseMsg | null {\n if (!isRecord(data)) return null;\n if (data.source !== BRIDGE_MSG_SOURCE || data.kind !== \"sessid-response\") return null;\n if (typeof data.nonce !== \"string\" || data.nonce !== expectedNonce) return null;\n if (typeof data.sessid !== \"string\") return null;\n return { source: BRIDGE_MSG_SOURCE, kind: \"sessid-response\", nonce: data.nonce, sessid: data.sessid };\n}\n\n// --- capture forwarding (capture build only) -------------------------------\n//\n// The capture hooks must observe the PAGE's fetch/XHR, so they run in the MAIN world.\n// The socket lives only in the ISOLATED connector, so MAIN forwards each observed entry\n// to the connector over postMessage; the connector relays it to the daemon.\n\nexport interface CaptureForwardMsg {\n source: typeof BRIDGE_MSG_SOURCE;\n kind: \"capture\";\n call: CapturedEntry;\n}\n\nexport function buildCaptureForward(call: CapturedEntry): CaptureForwardMsg {\n return { source: BRIDGE_MSG_SOURCE, kind: \"capture\", call };\n}\n\n/** For the ISOLATED connector: a capture entry forwarded by the MAIN script, or null. */\nexport function parseCaptureForward(data: unknown): CaptureForwardMsg | null {\n if (!isRecord(data)) return null;\n if (data.source !== BRIDGE_MSG_SOURCE || data.kind !== \"capture\") return null;\n if (!isRecord(data.call)) return null;\n return { source: BRIDGE_MSG_SOURCE, kind: \"capture\", call: data.call as unknown as CapturedEntry };\n}\n", "// ISOLATED-world connector. This is where the token and the WebSocket live \u2014 never in\n// the page's MAIN world, so the page cannot read the token without knowing the dynamic\n// (`use_dynamic_url`) resource URL for config.json, and cannot hijack the socket. It has\n// `chrome.runtime`, so it loads the per-user config from the packaged config.json at\n// runtime (nothing per-user is baked into this JS \u2014 the file is static). The actual\n// boundary is `use_dynamic_url: true` on config.json in the manifest: it makes the\n// resource URL unguessable from the page while `chrome.runtime.getURL()` here still\n// resolves it.\n//\n// Per daemon call it asks the MAIN shim for a FRESH sessid over postMessage (nonce-matched),\n// then reuses the pure bridge-core helpers to build the request, fetch it with the page's\n// cookies, and interpret the response.\n\nimport { buildRequest, interpret, type CallRequest, type InterpretResult } from \"./bridge-core.ts\";\nimport {\n parseConfig,\n buildSessidRequest,\n parseSessidResponse,\n parseCaptureForward,\n type BridgeConfig,\n} from \"./bridge-protocol.ts\";\n\n// Gates the capture relay listener; stripped from normal builds via esbuild `define`.\ndeclare const __BITRIX_CAPTURE__: boolean;\n\n// Minimal ambient for the one chrome API we use (no @types/chrome dependency).\ndeclare const chrome: { runtime: { getURL(path: string): string } };\n\nconst ORIGIN = location.origin;\nconst SESSID_TIMEOUT_MS = 2000;\nconst RECONNECT_MS = 3000;\n\nlet socket: WebSocket | null = null;\nlet nonceSeq = 0;\n\n/** Ask the MAIN shim for a fresh sessid; resolves \"\" if it doesn't answer in time. */\nfunction freshSessid(): Promise<string> {\n const nonce = `${Date.now()}-${nonceSeq++}-${Math.random().toString(36).slice(2)}`;\n return new Promise((resolve) => {\n const onMsg = (ev: MessageEvent): void => {\n if (ev.source !== window) return;\n const res = parseSessidResponse(ev.data, nonce);\n if (!res) return;\n cleanup();\n resolve(res.sessid);\n };\n const timer = setTimeout(() => {\n cleanup();\n resolve(\"\");\n }, SESSID_TIMEOUT_MS);\n const cleanup = (): void => {\n clearTimeout(timer);\n window.removeEventListener(\"message\", onMsg);\n };\n window.addEventListener(\"message\", onMsg);\n window.postMessage(buildSessidRequest(nonce), ORIGIN);\n });\n}\n\nasync function handleCall(req: CallRequest): Promise<InterpretResult> {\n const sessid = await freshSessid();\n if (!sessid) return { ok: false, error: \"session context not ready \u2014 open a normal Bitrix24 portal tab\" }; // G5\n const { url, body, contentType } = buildRequest(ORIGIN, req, sessid);\n const init: RequestInit = {\n method: req.method,\n credentials: \"include\",\n // sessid rides the CSRF header for all ajax (v2 form + json); form bodies also carry it inline.\n headers: { \"Content-Type\": contentType, \"X-Bitrix-Csrf-Token\": sessid },\n };\n let finalUrl = url;\n if (req.method === \"POST\") init.body = body;\n else finalUrl += (url.includes(\"?\") ? \"&\" : \"?\") + body;\n const resp = await fetch(finalUrl, init);\n const json = await resp.json();\n return interpret(json); // G3\n}\n\nfunction connect(config: BridgeConfig): void {\n const ws = new WebSocket(`ws://127.0.0.1:${config.port}`);\n ws.addEventListener(\"open\", () => {\n ws.send(JSON.stringify({ type: \"auth\", token: config.token })); // auth first (server closes non-auth)\n socket = ws;\n });\n ws.addEventListener(\"message\", async (ev: MessageEvent) => {\n let req: CallRequest;\n try {\n req = JSON.parse(ev.data) as CallRequest;\n } catch (e) {\n console.error(\"[bitrix-bridge] malformed WS frame, dropping:\", e);\n return;\n }\n if (req.type !== \"call\") return;\n try {\n const r = await handleCall(req);\n ws.send(JSON.stringify({ type: \"result\", id: req.id, ok: r.ok, data: r.data, error: r.error }));\n } catch (e) {\n ws.send(JSON.stringify({ type: \"result\", id: req.id, ok: false, error: String(e) }));\n }\n });\n ws.addEventListener(\"close\", () => {\n if (socket === ws) socket = null;\n setTimeout(() => connect(config), RECONNECT_MS); // auto-reconnect\n });\n}\n\n// Capture build only: relay MAIN-observed capture entries to the daemon.\nif (__BITRIX_CAPTURE__) {\n window.addEventListener(\"message\", (ev: MessageEvent) => {\n if (ev.source !== window) return;\n const fwd = parseCaptureForward(ev.data);\n if (!fwd) return;\n if (socket && socket.readyState === WebSocket.OPEN) {\n socket.send(JSON.stringify({ type: \"capture\", call: fwd.call }));\n }\n });\n}\n\nasync function start(): Promise<void> {\n let config: BridgeConfig;\n try {\n const resp = await fetch(chrome.runtime.getURL(\"config.json\"));\n if (!resp.ok) throw new Error(`HTTP ${resp.status}`);\n config = parseConfig(await resp.text());\n } catch (e) {\n // Nothing to fall back to \u2014 surface why the bridge won't start and stop.\n console.error(\"[bitrix-bridge] cannot load config.json:\", e);\n return;\n }\n connect(config);\n}\n\nstart();\n"],
5
- "mappings": ";;;AAcO,WAAS,WAAW,QAAyC;AAClE,QAAM,MAAM,IAAI,gBAAgB,GAC1B,MAAM,CAAC,KAAa,QAAuB;AAC/C,UAAI,OAAQ;AACZ,YAAI,MAAM,QAAQ,GAAG,EAAG,KAAI,QAAQ,CAAC,GAAG,MAAM,IAAI,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC;AAAA,iBAC3D,OAAO,OAAQ,SAAU,UAAW,CAAC,GAAG,CAAC,KAAK,OAAO,QAAQ,GAAG,EAAG,KAAI,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC;AAA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SAAS;AACjB,YAAI;AACF,cAAM,IAAI,MAAM,WAAW,GAAG;AAC9B,aAAG,KAAK,KAAK,UAAU,EAAE,MAAM,UAAU,IAAI,IAAI,IAAI,IAAI,EAAE,IAAI,MAAM,EAAE,MAAM,OAAO,EAAE,MAAM,CAAC,CAAC;AAAA,QAChG,SAAS,GAAG;AACV,aAAG,KAAK,KAAK,UAAU,EAAE,MAAM,UAAU,IAAI,IAAI,IAAI,IAAI,IAAO,OAAO,OAAO,CAAC,EAAE,CAAC,CAAC;AAAA,QACrF;AAAA,IACF,CAAC,GACD,GAAG,iBAAiB,SAAS,MAAM;AACjC,MAAI,WAAW,OAAI,SAAS,OAC5B,WAAW,MAAM,QAAQ,MAAM,GAAG,YAAY;AAAA,IAChD,CAAC;AAAA,EACH;AAcA,iBAAe,QAAuB;AACpC,QAAI;AACJ,QAAI;AACF,UAAM,OAAO,MAAM,MAAM,OAAO,QAAQ,OAAO,aAAa,CAAC;AAC7D,UAAI,CAAC,KAAK,GAAI,OAAM,IAAI,MAAM,QAAQ,KAAK,MAAM,EAAE;AACnD,eAAS,YAAY,MAAM,KAAK,KAAK,CAAC;AAAA,IACxC,SAAS,GAAG;AAEV,cAAQ,MAAM,4CAA4C,CAAC;AAC3D;AAAA,IACF;AACA,YAAQ,MAAM;AAAA,EAChB;AAEA,QAAM;",
4
+ "sourcesContent": ["// Pure, browser-agnostic \u2014 unit-tested.\n\n// Wire types come from the shared single source; re-exported so downstream\n// extension modules keep importing them from here.\nimport type { CallRequest } from \"../../shared/wire.ts\";\nexport type { CallRequest, CapturedEntry } from \"../../shared/wire.ts\";\n\n// Extension-only: the shape handleCall returns to the bridge (not a wire message).\nexport interface InterpretResult {\n ok: boolean;\n data?: unknown;\n error?: string;\n}\n\nexport function encodeForm(params: Record<string, unknown>): string {\n const out = new URLSearchParams();\n const add = (key: string, val: unknown): void => {\n if (val === null || val === undefined) return;\n if (Array.isArray(val)) val.forEach((v, i) => add(`${key}[${i}]`, v));\n else if (typeof val === \"object\") for (const [k, v] of Object.entries(val)) add(`${key}[${k}]`, v);\n else out.set(key, String(val));\n };\n for (const [k, v] of Object.entries(params)) add(k, v);\n return out.toString();\n}\n\n// Endpoints may carry `{name}` placeholders (e.g. \"/call/detail/{callId}\"); the value comes\n// from params and is consumed there, so it never rides along in the body/query as well.\nexport function applyPathParams(\n endpoint: string,\n params: Record<string, unknown>,\n): { endpoint: string; rest: Record<string, unknown> } {\n const rest = { ...params };\n const filled = endpoint.replace(/\\{(\\w+)\\}/g, (_match, key: string) => {\n const value = rest[key];\n if (value === null || value === undefined || value === \"\") {\n throw new Error(`missing path param \"${key}\" for endpoint ${endpoint}`);\n }\n delete rest[key];\n return encodeURIComponent(String(value));\n });\n return { endpoint: filled, rest };\n}\n\nexport function buildRequest(\n origin: string,\n req: CallRequest,\n sessid: string,\n): { url: string; body: string; contentType: string } {\n const { endpoint, rest } = applyPathParams(req.endpoint, req.params);\n const q = req.action ? `?action=${encodeURIComponent(req.action)}` : \"\";\n const url = `${origin}${endpoint}${q}`;\n // JSON actions (ui.entityselector.*, tasks.v2.*) send a JSON body; sessid rides the\n // X-Bitrix-Csrf-Token header (added by the caller), never the body.\n if (req.bodyType === \"json\") {\n return { url, body: JSON.stringify(rest), contentType: \"application/json\" };\n }\n const body = encodeForm({ ...rest, sessid });\n return { url, body, contentType: \"application/x-www-form-urlencoded\" };\n}\n\n// Bitrix wraps errors in HTTP 200: { status:\"error\", errors:[{code}] } or { error, error_description }.\n// NOTE: successful ajax responses carry an EMPTY `errors: []` array \u2014 a truthy value in JS \u2014 so we\n// must treat only a NON-EMPTY errors list (or status:\"error\"/a top-level error) as a real failure.\nexport function interpret(json: any): InterpretResult {\n const list = json && (Array.isArray(json.errors) ? json.errors : json.error ? [{ code: json.error }] : []);\n const hasErrors = Array.isArray(list) && list.length > 0;\n if (json && (json.status === \"error\" || hasErrors)) {\n const first = list[0];\n const code = (first && (first.code || first.message)) || json.error || \"bitrix_error\";\n const description = json.error_description || (first && first.message);\n const error = description && description !== code ? `${code}: ${description}` : code;\n return { ok: false, error, data: json };\n }\n return { ok: true, data: json };\n}\n\n// Chunk size for binary downloads: base64 inflates it by ~33%, so a 1 MiB slice stays a\n// comfortable WS frame while keeping the message count low for large videos.\nexport const BINARY_CHUNK_BYTES = 1024 * 1024;\n\n// `btoa` needs a binary string, and String.fromCharCode blows the call stack on big arrays \u2014\n// so the array is walked in small windows.\nexport function toBase64(bytes: Uint8Array): string {\n let binary = \"\";\n const window = 0x8000;\n for (let i = 0; i < bytes.length; i += window) {\n binary += String.fromCharCode(...bytes.subarray(i, i + window));\n }\n return btoa(binary);\n}\n\n/**\n * The portal answers an unauthenticated download with HTTP 200 and a login PAGE, so the status\n * code cannot be trusted. A genuine file download is marked as an attachment; an HTML body\n * without that marker is the login page, not a .html the user asked for.\n */\nexport function looksLikeLoginPage(contentType: string, contentDisposition: string | null): boolean {\n return /^\\s*text\\/html/i.test(contentType) && !/attachment/i.test(contentDisposition ?? \"\");\n}\n\n/** Server-suggested file name, e.g. `attachment; filename=\"image (29).png\"` (RFC 5987 aware). */\nexport function fileNameFromDisposition(contentDisposition: string | null): string | null {\n if (!contentDisposition) return null;\n const encoded = /filename\\*=(?:UTF-8'')?([^;]+)/i.exec(contentDisposition);\n if (encoded) {\n try {\n return decodeURIComponent(encoded[1].trim().replace(/^\"|\"$/g, \"\"));\n } catch {\n // fall through to the plain form\n }\n }\n const plain = /filename=\"?([^\";]+)\"?/i.exec(contentDisposition);\n return plain ? plain[1].trim() : null;\n}\n\n// HTML sub-domain (responseType \"text\"): no Bitrix envelope to read, so the HTTP status is the\n// only failure signal we get. A login redirect answers 200 with a page \u2014 that one is caught\n// downstream, where the parser knows what the payload was supposed to look like.\nexport function interpretText(text: string, contentType: string, status: number): InterpretResult {\n if (status >= 400) return { ok: false, error: `HTTP ${status}` };\n if (!text.trim()) return { ok: false, error: \"empty response\" };\n return { ok: true, data: { contentType, text } };\n}\n", "// Pure, browser-agnostic \u2014 unit-tested. No DOM / chrome / window.BX here.\n//\n// Two concerns live together because both are the *contract* between the extension's\n// two content scripts and the packaged config, and both are pure/testable:\n// 1) parseConfig \u2014 validate the per-user config.json ({ token, port }).\n// 2) the postMessage protocol \u2014 the envelopes the ISOLATED connector and the MAIN\n// script exchange over window.postMessage (sessid request/response, and \u2014 in the\n// capture build \u2014 MAIN-observed capture entries forwarded to the connector).\n\nimport type { CapturedEntry } from \"./bridge-core.ts\";\n\nexport interface BridgeConfig {\n token: string;\n port: number;\n}\n\n/**\n * Parse and validate the packaged config.json body. Throws with a clear,\n * user-facing message (mentioning config.json) on any malformed input so the\n * connector can surface why it failed to start.\n */\nexport function parseConfig(raw: string): BridgeConfig {\n let doc: unknown;\n try {\n doc = JSON.parse(raw);\n } catch {\n throw new Error(\"config.json is not valid JSON\");\n }\n if (typeof doc !== \"object\" || doc === null) {\n throw new Error(\"config.json must be an object like { token, port }\");\n }\n const obj = doc as Record<string, unknown>;\n const token = typeof obj.token === \"string\" ? obj.token.trim() : \"\";\n if (!token) throw new Error(\"config.json is missing a non-empty `token`\");\n const port = Number(obj.port);\n if (!Number.isInteger(port) || port < 1 || port > 65535) {\n throw new Error(\"config.json is missing a numeric `port`\");\n }\n return { token, port };\n}\n\n// --- sessid postMessage protocol -------------------------------------------\n//\n// window.postMessage is delivered to BOTH the MAIN and ISOLATED worlds of the same\n// frame (they share the DOM window), and each world also receives its own posts. So\n// every envelope is tagged with a fixed `source` and a `kind`, and each side ignores\n// anything that isn't addressed to it. Responses additionally carry the request nonce\n// so the connector can match a reply to the exact in-flight request it issued.\n\nexport const BRIDGE_MSG_SOURCE = \"bitrix24-mcp-bridge\";\n\nexport interface SessidRequestMsg {\n source: typeof BRIDGE_MSG_SOURCE;\n kind: \"sessid-request\";\n nonce: string;\n}\n\nexport interface SessidResponseMsg {\n source: typeof BRIDGE_MSG_SOURCE;\n kind: \"sessid-response\";\n nonce: string;\n sessid: string;\n}\n\nexport function buildSessidRequest(nonce: string): SessidRequestMsg {\n return { source: BRIDGE_MSG_SOURCE, kind: \"sessid-request\", nonce };\n}\n\nexport function buildSessidResponse(nonce: string, sessid: string): SessidResponseMsg {\n return { source: BRIDGE_MSG_SOURCE, kind: \"sessid-response\", nonce, sessid };\n}\n\nfunction isRecord(v: unknown): v is Record<string, unknown> {\n return typeof v === \"object\" && v !== null;\n}\n\n/** For the MAIN shim: a well-formed request from our connector, or null. */\nexport function parseSessidRequest(data: unknown): SessidRequestMsg | null {\n if (!isRecord(data)) return null;\n if (data.source !== BRIDGE_MSG_SOURCE || data.kind !== \"sessid-request\") return null;\n if (typeof data.nonce !== \"string\") return null;\n return { source: BRIDGE_MSG_SOURCE, kind: \"sessid-request\", nonce: data.nonce };\n}\n\n/** For the ISOLATED connector: a response matching `expectedNonce`, or null. */\nexport function parseSessidResponse(data: unknown, expectedNonce: string): SessidResponseMsg | null {\n if (!isRecord(data)) return null;\n if (data.source !== BRIDGE_MSG_SOURCE || data.kind !== \"sessid-response\") return null;\n if (typeof data.nonce !== \"string\" || data.nonce !== expectedNonce) return null;\n if (typeof data.sessid !== \"string\") return null;\n return { source: BRIDGE_MSG_SOURCE, kind: \"sessid-response\", nonce: data.nonce, sessid: data.sessid };\n}\n\n// --- capture forwarding (capture build only) -------------------------------\n//\n// The capture hooks must observe the PAGE's fetch/XHR, so they run in the MAIN world.\n// The socket lives only in the ISOLATED connector, so MAIN forwards each observed entry\n// to the connector over postMessage; the connector relays it to the daemon.\n\nexport interface CaptureForwardMsg {\n source: typeof BRIDGE_MSG_SOURCE;\n kind: \"capture\";\n call: CapturedEntry;\n}\n\nexport function buildCaptureForward(call: CapturedEntry): CaptureForwardMsg {\n return { source: BRIDGE_MSG_SOURCE, kind: \"capture\", call };\n}\n\n/** For the ISOLATED connector: a capture entry forwarded by the MAIN script, or null. */\nexport function parseCaptureForward(data: unknown): CaptureForwardMsg | null {\n if (!isRecord(data)) return null;\n if (data.source !== BRIDGE_MSG_SOURCE || data.kind !== \"capture\") return null;\n if (!isRecord(data.call)) return null;\n return { source: BRIDGE_MSG_SOURCE, kind: \"capture\", call: data.call as unknown as CapturedEntry };\n}\n", "// ISOLATED-world connector. This is where the token and the WebSocket live \u2014 never in\n// the page's MAIN world, so the page cannot read the token without knowing the dynamic\n// (`use_dynamic_url`) resource URL for config.json, and cannot hijack the socket. It has\n// `chrome.runtime`, so it loads the per-user config from the packaged config.json at\n// runtime (nothing per-user is baked into this JS \u2014 the file is static). The actual\n// boundary is `use_dynamic_url: true` on config.json in the manifest: it makes the\n// resource URL unguessable from the page while `chrome.runtime.getURL()` here still\n// resolves it.\n//\n// Per daemon call it asks the MAIN shim for a FRESH sessid over postMessage (nonce-matched),\n// then reuses the pure bridge-core helpers to build the request, fetch it with the page's\n// cookies, and interpret the response.\n\nimport {\n buildRequest,\n interpret,\n interpretText,\n toBase64,\n looksLikeLoginPage,\n fileNameFromDisposition,\n BINARY_CHUNK_BYTES,\n type CallRequest,\n type InterpretResult,\n} from \"./bridge-core.ts\";\nimport {\n parseConfig,\n buildSessidRequest,\n parseSessidResponse,\n parseCaptureForward,\n type BridgeConfig,\n} from \"./bridge-protocol.ts\";\n\n// Gates the capture relay listener; stripped from normal builds via esbuild `define`.\ndeclare const __BITRIX_CAPTURE__: boolean;\n\n// Minimal ambient for the one chrome API we use (no @types/chrome dependency).\ndeclare const chrome: { runtime: { getURL(path: string): string; getManifest(): { version?: string } } };\n\nconst ORIGIN = location.origin;\nconst SESSID_TIMEOUT_MS = 2000;\nconst RECONNECT_MS = 3000;\n// The whole body is buffered here before streaming, so this caps peak tab memory.\nconst MAX_DOWNLOAD_BYTES = 512 * 1024 * 1024;\n\nlet socket: WebSocket | null = null;\nlet nonceSeq = 0;\n\n/** Ask the MAIN shim for a fresh sessid; resolves \"\" if it doesn't answer in time. */\nfunction freshSessid(): Promise<string> {\n const nonce = `${Date.now()}-${nonceSeq++}-${Math.random().toString(36).slice(2)}`;\n return new Promise((resolve) => {\n const onMsg = (ev: MessageEvent): void => {\n if (ev.source !== window) return;\n const res = parseSessidResponse(ev.data, nonce);\n if (!res) return;\n cleanup();\n resolve(res.sessid);\n };\n const timer = setTimeout(() => {\n cleanup();\n resolve(\"\");\n }, SESSID_TIMEOUT_MS);\n const cleanup = (): void => {\n clearTimeout(timer);\n window.removeEventListener(\"message\", onMsg);\n };\n window.addEventListener(\"message\", onMsg);\n window.postMessage(buildSessidRequest(nonce), ORIGIN);\n });\n}\n\nasync function fetchPortal(req: CallRequest): Promise<Response | { error: string }> {\n const sessid = await freshSessid();\n if (!sessid) return { error: \"session context not ready \u2014 open a normal Bitrix24 portal tab\" }; // G5\n const { url, body, contentType } = buildRequest(ORIGIN, req, sessid);\n const init: RequestInit = {\n method: req.method,\n credentials: \"include\",\n // sessid rides the CSRF header for all ajax (v2 form + json); form bodies also carry it inline.\n headers: { \"Content-Type\": contentType, \"X-Bitrix-Csrf-Token\": sessid },\n };\n let finalUrl = url;\n if (req.method === \"POST\") init.body = body;\n else finalUrl += (url.includes(\"?\") ? \"&\" : \"?\") + body;\n return fetch(finalUrl, init);\n}\n\nasync function handleCall(req: CallRequest): Promise<InterpretResult> {\n const resp = await fetchPortal(req);\n if (!(resp instanceof Response)) return { ok: false, error: resp.error };\n if (req.responseType === \"text\") {\n return interpretText(await resp.text(), resp.headers.get(\"content-type\") ?? \"\", resp.status);\n }\n const json = await resp.json();\n return interpret(json); // G3\n}\n\n/** Streams the body to the daemon as base64 chunks; the daemon writes them to a file. */\nasync function handleBinaryCall(req: CallRequest, ws: WebSocket): Promise<InterpretResult> {\n const resp = await fetchPortal(req);\n if (!(resp instanceof Response)) return { ok: false, error: resp.error };\n if (resp.status >= 400) return { ok: false, error: `HTTP ${resp.status}` };\n\n const contentType = resp.headers.get(\"content-type\") ?? \"\";\n const disposition = resp.headers.get(\"content-disposition\");\n if (looksLikeLoginPage(contentType, disposition)) {\n return { ok: false, error: \"portal answered with an HTML page, not a file \u2014 session likely expired\" };\n }\n\n const bytes = new Uint8Array(await resp.arrayBuffer());\n if (bytes.length > MAX_DOWNLOAD_BYTES) {\n return { ok: false, error: `file is ${bytes.length} bytes, over the ${MAX_DOWNLOAD_BYTES} limit` };\n }\n const fileName = fileNameFromDisposition(disposition);\n ws.send(JSON.stringify({ type: \"binary-begin\", id: req.id, contentType, bytes: bytes.length, fileName }));\n let seq = 0;\n for (let at = 0; at < bytes.length; at += BINARY_CHUNK_BYTES) {\n const slice = bytes.subarray(at, at + BINARY_CHUNK_BYTES);\n ws.send(JSON.stringify({ type: \"binary-chunk\", id: req.id, seq: seq++, data: toBase64(slice) }));\n }\n return { ok: true, data: { contentType, bytes: bytes.length, fileName, chunks: seq } };\n}\n\nfunction connect(config: BridgeConfig): void {\n const ws = new WebSocket(`ws://127.0.0.1:${config.port}`);\n ws.addEventListener(\"open\", () => {\n ws.send(JSON.stringify({ type: \"auth\", token: config.token, version: chrome.runtime.getManifest().version }));\n socket = ws;\n });\n ws.addEventListener(\"message\", async (ev: MessageEvent) => {\n let req: CallRequest;\n try {\n req = JSON.parse(ev.data) as CallRequest;\n } catch (e) {\n console.error(\"[bitrix-bridge] malformed WS frame, dropping:\", e);\n return;\n }\n if (req.type !== \"call\") return;\n try {\n const r = req.responseType === \"binary\" ? await handleBinaryCall(req, ws) : await handleCall(req);\n ws.send(JSON.stringify({ type: \"result\", id: req.id, ok: r.ok, data: r.data, error: r.error }));\n } catch (e) {\n ws.send(JSON.stringify({ type: \"result\", id: req.id, ok: false, error: String(e) }));\n }\n });\n ws.addEventListener(\"close\", () => {\n if (socket === ws) socket = null;\n setTimeout(() => connect(config), RECONNECT_MS); // auto-reconnect\n });\n}\n\n// Capture build only: relay MAIN-observed capture entries to the daemon.\nif (__BITRIX_CAPTURE__) {\n window.addEventListener(\"message\", (ev: MessageEvent) => {\n if (ev.source !== window) return;\n const fwd = parseCaptureForward(ev.data);\n if (!fwd) return;\n if (socket && socket.readyState === WebSocket.OPEN) {\n socket.send(JSON.stringify({ type: \"capture\", call: fwd.call }));\n }\n });\n}\n\nasync function start(): Promise<void> {\n let config: BridgeConfig;\n try {\n const resp = await fetch(chrome.runtime.getURL(\"config.json\"));\n if (!resp.ok) throw new Error(`HTTP ${resp.status}`);\n config = parseConfig(await resp.text());\n } catch (e) {\n // Nothing to fall back to \u2014 surface why the bridge won't start and stop.\n console.error(\"[bitrix-bridge] cannot load config.json:\", e);\n return;\n }\n connect(config);\n}\n\nstart();\n"],
5
+ "mappings": ";;;AAcO,WAAS,WAAW,QAAyC;AAClE,QAAM,MAAM,IAAI,gBAAgB,GAC1B,MAAM,CAAC,KAAa,QAAuB;AAC/C,UAAI,OAAQ;AACZ,YAAI,MAAM,QAAQ,GAAG,EAAG,KAAI,QAAQ,CAAC,GAAG,MAAM,IAAI,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC;AAAA,iBAC3D,OAAO,OAAQ,SAAU,UAAW,CAAC,GAAG,CAAC,KAAK,OAAO,QAAQ,GAAG,EAAG,KAAI,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC;AAAA,YAC5F,KAAI,IAAI,KAAK,OAAO,GAAG,CAAC;AAAA,IAC/B;AACA,aAAW,CAAC,GAAG,CAAC,KAAK,OAAO,QAAQ,MAAM,EAAG,KAAI,GAAG,CAAC;AACrD,WAAO,IAAI,SAAS;AAAA,EACtB;AAIO,WAAS,gBACd,UACA,QACqD;AACrD,QAAM,OAAO,EAAE,GAAG,OAAO;AASzB,WAAO,EAAE,UARM,SAAS,QAAQ,cAAc,CAAC,QAAQ,QAAgB;AACrE,UAAM,QAAQ,KAAK,GAAG;AACtB,UAAI,SAAU,QAA+B,UAAU;AACrD,cAAM,IAAI,MAAM,uBAAuB,GAAG,kBAAkB,QAAQ,EAAE;AAExE,oBAAO,KAAK,GAAG,GACR,mBAAmB,OAAO,KAAK,CAAC;AAAA,IACzC,CAAC,GAC0B,KAAK;AAAA,EAClC;AAEO,WAAS,aACd,QACA,KACA,QACoD;AACpD,QAAM,EAAE,UAAU,KAAK,IAAI,gBAAgB,IAAI,UA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;AAAA,QACrF;AAAA,IACF,CAAC,GACD,GAAG,iBAAiB,SAAS,MAAM;AACjC,MAAI,WAAW,OAAI,SAAS,OAC5B,WAAW,MAAM,QAAQ,MAAM,GAAG,YAAY;AAAA,IAChD,CAAC;AAAA,EACH;AAcA,iBAAe,QAAuB;AACpC,QAAI;AACJ,QAAI;AACF,UAAM,OAAO,MAAM,MAAM,OAAO,QAAQ,OAAO,aAAa,CAAC;AAC7D,UAAI,CAAC,KAAK,GAAI,OAAM,IAAI,MAAM,QAAQ,KAAK,MAAM,EAAE;AACnD,eAAS,YAAY,MAAM,KAAK,KAAK,CAAC;AAAA,IACxC,SAAS,GAAG;AAEV,cAAQ,MAAM,4CAA4C,CAAC;AAC3D;AAAA,IACF;AACA,YAAQ,MAAM;AAAA,EAChB;AAEA,QAAM;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "bitrix24-mcp-bridge",
4
- "version": "0.3.0",
4
+ "version": "0.5.0",
5
5
  "description": "MCP bridge server for Bitrix24 — exposes REST API access, tasks, projects and chat to MCP clients like Claude Code.",
6
6
  "license": "MIT",
7
7
  "repository": {
@@ -34,6 +34,7 @@
34
34
  },
35
35
  "dependencies": {
36
36
  "@modelcontextprotocol/sdk": "^1.12.0",
37
+ "node-html-parser": "^9.0.0",
37
38
  "ws": "^8.18.0",
38
39
  "zod": "^3.23.0"
39
40
  },