Compare commits

..

No commits in common. "c5d1880589e7deccb64697edd8902057ee4ab064" and "c7edf8c66c5630a50b6cf8eef66296b23a8eeb3c" have entirely different histories.

View File

@ -52,19 +52,13 @@ export function createTransport(opts: TransportOpts): Transport {
function connectWs() {
if (closed) return;
// The browser WebSocket constructor does NOT accept custom headers, so
// we encode credentials into the Sec-WebSocket-Protocol list (the
// standard workaround). The bot reads `req.headers['sec-websocket-
// protocol']` and parses these tokens. See bot's `webchat/subprotocol.ts`.
const wsUrl = opts.serverUrl.replace(/^http/, 'ws') + '/webchat/ws';
const subprotocols = [
'messenzy.v1',
`messenzy-bot.${opts.botId}`,
`messenzy-visitor.${opts.visitorId}`,
`messenzy-key.${opts.apiKey}`,
];
const wsUrl =
opts.serverUrl.replace(/^http/, 'ws') +
`/webchat/ws?botId=${encodeURIComponent(opts.botId)}` +
`&apiKey=${encodeURIComponent(opts.apiKey)}` +
`&visitorId=${encodeURIComponent(opts.visitorId)}`;
try {
ws = new WebSocket(wsUrl, subprotocols);
ws = new WebSocket(wsUrl);
} catch {
onWsClose();
return;
@ -124,19 +118,14 @@ export function createTransport(opts: TransportOpts): Transport {
async function startPolling() {
pollTimer = setInterval(async () => {
if (closed) return;
// HTTP fallback uses Authorization: Bearer (browser fetch DOES allow
// custom headers, unlike the WebSocket constructor). botId/visitorId
// are public identifiers and stay in the query string.
const url =
`${opts.serverUrl}/webchat/history?` +
`botId=${encodeURIComponent(opts.botId)}` +
`&apiKey=${encodeURIComponent(opts.apiKey)}` +
`&visitorId=${encodeURIComponent(opts.visitorId)}` +
(lastSeenAt ? `&since=${encodeURIComponent(lastSeenAt)}` : '');
try {
const res = await fetch(url, {
credentials: 'omit',
headers: { authorization: `Bearer ${opts.apiKey}` },
});
const res = await fetch(url, { credentials: 'omit' });
if (!res.ok) return;
const data = (await res.json()) as {
messages?: Array<{ id: string; text: string; at: string }>;
@ -164,12 +153,10 @@ export function createTransport(opts: TransportOpts): Transport {
try {
await fetch(`${opts.serverUrl}/webchat/msg`, {
method: 'POST',
headers: {
'content-type': 'application/json',
authorization: `Bearer ${opts.apiKey}`,
},
headers: { 'content-type': 'application/json' },
body: JSON.stringify({
botId: opts.botId,
apiKey: opts.apiKey,
visitorId: opts.visitorId,
text: msg.text,
idempotency_key: msg.id,