|
| 1 | +// Fern's pinned Algolia integration has no public pre-initialization opt-out. |
| 2 | +// Keep necessary search, but disable its optional analytics for every visitor. |
| 3 | +// Audit these transport/storage contracts whenever the Fern bundle changes. |
| 4 | +(function () { |
| 5 | + if (window.__vapiDocsSearchPrivacy) return; |
| 6 | + window.__vapiDocsSearchPrivacy = true; |
| 7 | + var optional = ['userToken', 'authenticatedUserToken', 'analyticsTags']; |
| 8 | + var success = JSON.stringify({ status: 200, message: 'OK' }); |
| 9 | + function target(input) { |
| 10 | + try { |
| 11 | + var url = new URL(typeof input === 'string' || input instanceof URL ? input : input.url, window.location.href); |
| 12 | + var host = url.hostname.toLowerCase(); |
| 13 | + if (url.origin === window.location.origin && url.pathname === '/api/fern-docs/search/v2/key') return { url: url, key: true }; |
| 14 | + if (url.protocol !== 'https:' || url.port) return { url: url }; |
| 15 | + if (/^(insights|insights\.(us|de))\.algolia\.io$/.test(host) && url.pathname === '/1/events') return { url: url, insights: true }; |
| 16 | + if (/^p6vyurbgg0(?:-dsn)?\.algolia\.net$|^p6vyurbgg0-[123]\.algolianet\.com$/.test(host) && /^\/1\/indexes\/[^/]+\/(queries|query)$/.test(url.pathname)) return { url: url, search: true }; |
| 17 | + return { url: url }; |
| 18 | + } catch (_) { return {}; } |
| 19 | + } |
| 20 | + function object(value) { |
| 21 | + if (!value || typeof value !== 'object' || Array.isArray(value)) throw new TypeError('Unsupported Algolia search encoding'); |
| 22 | + optional.forEach(function (name) { delete value[name]; }); |
| 23 | + if ('requests' in value) { |
| 24 | + if (!Array.isArray(value.requests)) throw new TypeError('Unsupported Algolia search encoding'); |
| 25 | + delete value.analytics; |
| 26 | + delete value.clickAnalytics; |
| 27 | + value.requests = value.requests.map(object); |
| 28 | + } else { |
| 29 | + value.analytics = false; |
| 30 | + value.clickAnalytics = false; |
| 31 | + } |
| 32 | + if ('params' in value) value.params = typeof value.params === 'string' ? parameters(value.params).toString() : object(value.params); |
| 33 | + return value; |
| 34 | + } |
| 35 | + function parameters(value) { |
| 36 | + var params = new URLSearchParams(value); |
| 37 | + optional.forEach(function (name) { params.delete(name); }); |
| 38 | + params.set('analytics', 'false'); |
| 39 | + params.set('clickAnalytics', 'false'); |
| 40 | + if (params.has('params')) params.set('params', parameters(params.get('params')).toString()); |
| 41 | + if (params.has('requests')) params.set('requests', JSON.stringify(JSON.parse(params.get('requests')).map(object))); |
| 42 | + return params; |
| 43 | + } |
| 44 | + function body(value) { |
| 45 | + if (value == null || value === '') return value; |
| 46 | + if (value instanceof URLSearchParams) return parameters(value); |
| 47 | + if (typeof value !== 'string') throw new TypeError('Unsupported Algolia search encoding'); |
| 48 | + try { return JSON.stringify(object(JSON.parse(value))); } |
| 49 | + catch (error) { |
| 50 | + // The pinned client sends JSON. Also support documented form parameters; |
| 51 | + // an unknown future encoding must not silently restore optional collection. |
| 52 | + if (!value.includes('=') || /^[\s]*[\[{]/.test(value)) throw error; |
| 53 | + return parameters(value).toString(); |
| 54 | + } |
| 55 | + } |
| 56 | + function searchURL(info) { |
| 57 | + info.url.search = parameters(info.url.search).toString(); |
| 58 | + return info.url.href; |
| 59 | + } |
| 60 | + function optionalHeader(name) { return /^x-algolia-(user-token|usertoken|authenticated-user-token|authenticatedusertoken)$/i.test(name); } |
| 61 | + if (typeof window.fetch === 'function') { |
| 62 | + var fetch = window.fetch; |
| 63 | + window.fetch = function (input, init) { |
| 64 | + var info = target(input); |
| 65 | + if (info.insights) return Promise.resolve(new Response(success, { status: 200, headers: { 'Content-Type': 'application/json' } })); |
| 66 | + if (!info.search && !info.key) return fetch.call(this, input, init); |
| 67 | + var receiver = this; |
| 68 | + return Promise.resolve().then(async function () { |
| 69 | + var request = new Request(input instanceof Request ? input : info.url.href, init); |
| 70 | + var headers = new Headers(request.headers); |
| 71 | + if (info.key) headers.delete('X-User-Token'); |
| 72 | + if (info.search) Array.from(headers.keys()).forEach(function (name) { if (optionalHeader(name)) headers.delete(name); }); |
| 73 | + var next = new Request(info.search ? searchURL(info) : info.url.href, request); |
| 74 | + var options = { headers: headers }; |
| 75 | + if (info.search && request.method !== 'GET' && request.method !== 'HEAD') options.body = body(await request.clone().text()); |
| 76 | + return fetch.call(receiver, new Request(next, options)); |
| 77 | + }); |
| 78 | + }; |
| 79 | + } |
| 80 | + if (navigator && typeof navigator.sendBeacon === 'function') { |
| 81 | + var beacon = navigator.sendBeacon; |
| 82 | + navigator.sendBeacon = function (url, data) { |
| 83 | + if (target(url).insights) return true; // Accepted locally: do not trigger SDK fallback. |
| 84 | + return beacon.call(this, url, data); |
| 85 | + }; |
| 86 | + } |
| 87 | + if (typeof XMLHttpRequest !== 'undefined') { |
| 88 | + var targets = new WeakMap(); |
| 89 | + var localResponses = new WeakSet(); |
| 90 | + function clearLocalResponse(xhr) { |
| 91 | + if (!localResponses.has(xhr)) return; |
| 92 | + ['readyState', 'status', 'statusText', 'responseText', 'response'].forEach(function (name) { delete xhr[name]; }); |
| 93 | + localResponses.delete(xhr); |
| 94 | + } |
| 95 | + var open = XMLHttpRequest.prototype.open; |
| 96 | + var send = XMLHttpRequest.prototype.send; |
| 97 | + var header = XMLHttpRequest.prototype.setRequestHeader; |
| 98 | + var abort = XMLHttpRequest.prototype.abort; |
| 99 | + XMLHttpRequest.prototype.open = function (method, url) { |
| 100 | + // Remove a prior local response before reusing this native XHR instance. |
| 101 | + clearLocalResponse(this); |
| 102 | + var info = target(url); |
| 103 | + targets.set(this, info); |
| 104 | + var args = Array.prototype.slice.call(arguments); |
| 105 | + if (info.search) args[1] = searchURL(info); |
| 106 | + try { return open.apply(this, args); } |
| 107 | + catch (error) { targets.delete(this); throw error; } |
| 108 | + }; |
| 109 | + XMLHttpRequest.prototype.setRequestHeader = function (name, value) { |
| 110 | + var info = targets.get(this); |
| 111 | + if (info && ((info.key && /^x-user-token$/i.test(name)) || (info.search && optionalHeader(name)))) return; |
| 112 | + return header.call(this, name, value); |
| 113 | + }; |
| 114 | + XMLHttpRequest.prototype.abort = function () { |
| 115 | + targets.delete(this); |
| 116 | + clearLocalResponse(this); |
| 117 | + return abort.apply(this, arguments); |
| 118 | + }; |
| 119 | + XMLHttpRequest.prototype.send = function (data) { |
| 120 | + var info = targets.get(this); |
| 121 | + if (!info || !info.insights) return send.call(this, info && info.search ? body(data) : data); |
| 122 | + var xhr = this; |
| 123 | + // The SDK's XHR sender requires readyState=4, HTTP 200, and JSON success. |
| 124 | + // Settle asynchronously without invoking native send or a retrying error. |
| 125 | + setTimeout(function () { |
| 126 | + if (targets.get(xhr) !== info) return; |
| 127 | + localResponses.add(xhr); |
| 128 | + Object.defineProperties(xhr, { |
| 129 | + readyState: { configurable: true, value: 4 }, status: { configurable: true, value: 200 }, |
| 130 | + statusText: { configurable: true, value: 'OK' }, |
| 131 | + responseText: { configurable: true, value: success }, |
| 132 | + response: { configurable: true, value: xhr.responseType === 'json' ? JSON.parse(success) : success } |
| 133 | + }); |
| 134 | + ['readystatechange', 'load', 'loadend'].forEach(function (name) { |
| 135 | + if (targets.get(xhr) === info) xhr.dispatchEvent(new Event(name)); |
| 136 | + }); |
| 137 | + }, 0); |
| 138 | + }; |
| 139 | + } |
| 140 | + // Only audited optional keys: do not disturb Algolia's necessary host cache. |
| 141 | + var keys = ['algolia-user-token', 'AlgoliaObjectQueryCache']; |
| 142 | + if (typeof Storage !== 'undefined') { |
| 143 | + var get = Storage.prototype.getItem; |
| 144 | + var set = Storage.prototype.setItem; |
| 145 | + var remove = Storage.prototype.removeItem; |
| 146 | + ['localStorage', 'sessionStorage'].forEach(function (name) { |
| 147 | + try { keys.forEach(function (key) { remove.call(window[name], key); }); } catch (_) { /* Storage may be unavailable. */ } |
| 148 | + }); |
| 149 | + Storage.prototype.getItem = function (key) { return keys.includes(String(key)) ? null : get.apply(this, arguments); }; |
| 150 | + Storage.prototype.setItem = function (key) { if (!keys.includes(String(key))) return set.apply(this, arguments); }; |
| 151 | + } |
| 152 | + if (typeof Document !== 'undefined') { |
| 153 | + var cookie = Object.getOwnPropertyDescriptor(Document.prototype, 'cookie'); |
| 154 | + if (cookie && cookie.get && cookie.set && cookie.configurable) { |
| 155 | + // Remove the known cookie at the current and ancestor domain scopes. |
| 156 | + var domains = window.location.hostname.split('.'); |
| 157 | + try { |
| 158 | + cookie.set.call(document, '_ALGOLIA=; Max-Age=0; Path=/'); |
| 159 | + while (domains.length > 1) { |
| 160 | + cookie.set.call(document, '_ALGOLIA=; Max-Age=0; Path=/; Domain=' + domains.join('.')); |
| 161 | + domains.shift(); |
| 162 | + } |
| 163 | + } catch (_) { /* Cookies may be unavailable. */ } |
| 164 | + Object.defineProperty(Document.prototype, 'cookie', { |
| 165 | + configurable: true, enumerable: cookie.enumerable, |
| 166 | + get: function () { return cookie.get.call(this).split(';').filter(function (part) { return !/^\s*_ALGOLIA=/.test(part); }).join(';'); }, |
| 167 | + set: function (value) { if (!/^\s*_ALGOLIA=/.test(String(value))) cookie.set.call(this, value); } |
| 168 | + }); |
| 169 | + } |
| 170 | + } |
| 171 | +})(); |
0 commit comments