Skip to content

Commit 028d6b5

Browse files
authored
[PRO-4275] Enforce docs analytics consent with Fern 5.169.0 (#1405)
Pin Fern 5.169.0, enforce native analytics consent, preserve necessary search and direct API playground requests, and remove ungated optional collectors.
1 parent 6ab5ffa commit 028d6b5

22 files changed

Lines changed: 1447 additions & 129 deletions

‎.github/actions/setup-fern/action.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ runs:
99
- name: Install pinned Fern
1010
shell: bash
1111
run: |
12-
npm install --prefix "$RUNNER_TEMP/vapi-fern" --no-save --no-audit --no-fund fern-api@5.151.1
12+
npm install --prefix "$RUNNER_TEMP/vapi-fern" --no-save --no-audit --no-fund fern-api@5.169.0
1313
echo "FERN_CLI_PATH=$RUNNER_TEMP/vapi-fern/node_modules/fern-api/cli.cjs" >> "$GITHUB_ENV"
1414
- name: Test cache safeguards
1515
shell: bash
16-
run: node --test scripts/fern/example-cache.test.cjs
16+
run: node --test scripts/fern/*.test.cjs
1717
- uses: actions/cache@v4
1818
with:
1919
path: .cache/fern-examples

‎.github/workflows/check.yml‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,25 @@ jobs:
1717

1818
- name: Check API is valid
1919
run: node scripts/fern/run.cjs check
20+
21+
docs-consent:
22+
runs-on: ubuntu-24.04
23+
steps:
24+
- uses: actions/checkout@v4
25+
- uses: ./.github/actions/setup-fern
26+
- name: Install browser regression dependencies
27+
run: npm ci --ignore-scripts --no-audit --no-fund
28+
- name: Install Chromium
29+
run: npx playwright install --with-deps chromium
30+
- name: Test consent and subscription logic
31+
run: |
32+
npm run test:privacy
33+
node fern/custom.spec.js
34+
- name: Verify generated local Fern consent behavior
35+
run: npm run test:privacy:browser
36+
- uses: actions/upload-artifact@v4
37+
if: always()
38+
with:
39+
name: docs-consent-requests
40+
path: ${{ runner.temp }}/docs-privacy-results
41+
retention-days: 7

‎README.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,22 @@ You can suggest edits by making a pull request.
2727
To run a local development server with hot-reloading you can run the following command
2828

2929
```sh
30-
npm install -g fern-api
30+
npm install -g fern-api@5.169.0
3131
fern docs dev
3232
```
3333

34+
Consent behavior, regression checks, publication prerequisites, and safe disablement are documented in the [docs consent runbook](scripts/privacy/README.md).
35+
3436
#### Hosted URL
3537

3638
To update your documentation on a hosted URL, run
3739
```
38-
# npm install -g fern-api
40+
# npm install -g fern-api@5.169.0
3941
fern generate --docs
4042
```
4143
To preview your documentation, run
4244
```
43-
# npm install -g fern-api
45+
# npm install -g fern-api@5.169.0
4446
fern generate --docs --preview
4547
```
4648
The repository contains GitHub workflows that will automatically run these commands for you. For example, when you make a PR a preview link will be auto-generated and when you merge to main the docs site will update.

‎advanced.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ This repository contains our Fern Configuration:
99
## Setup
1010

1111
```sh
12-
npm install -g fern-api
12+
npm install -g fern-api@5.169.0
1313
```
1414

1515
## Validating your OpenAPI Specs
@@ -59,12 +59,12 @@ fern docs dev
5959

6060
To update your documentation on a hosted URL, run
6161
```
62-
# npm install -g fern-api
62+
# npm install -g fern-api@5.169.0
6363
fern generate --docs
6464
```
6565
To preview your documentation, run
6666
```
67-
# npm install -g fern-api
67+
# npm install -g fern-api@5.169.0
6868
fern generate --docs --preview
6969
```
7070
The repository contains GitHub workflows that will automatically run these commands for you. For example, when you make a PR a preview link will be auto-generated and when you merge to main the docs site will update.

‎fern/assets/privacy-requests.js‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Necessary Vapi API requests still run when optional collection is denied.
2+
// Stamp only the audited API origins, at dispatch, without modifying auth,
3+
// credentials, body, signal, or other caller headers.
4+
(function () {
5+
if (window.__vapiDocsConsentRequests) return;
6+
window.__vapiDocsConsentRequests = true;
7+
function applicable(input) {
8+
try {
9+
var url = new URL(typeof input === 'string' || input instanceof URL ? input : input.url, window.location.href);
10+
return url.origin === 'https://api.vapi.ai' || url.origin === 'https://api.eu.vapi.ai';
11+
} catch (_) { return false; }
12+
}
13+
function values() {
14+
var state = window.vapiDocsConsent && window.vapiDocsConsent.getSnapshot();
15+
return {
16+
'X-Vapi-Consent-Analytics': state && state.ready && state.analytics ? 'granted' : 'denied',
17+
'X-Vapi-Consent-Marketing': state && state.ready && state.marketing ? 'granted' : 'denied'
18+
};
19+
}
20+
if (typeof window.fetch === 'function') {
21+
var fetch = window.fetch;
22+
window.fetch = function (input, init) {
23+
if (!applicable(input)) return fetch.call(this, input, init);
24+
var source = init && init.headers !== undefined ? init.headers : input && input.headers;
25+
var headers = new Headers(source);
26+
var consent = values();
27+
Object.keys(consent).forEach(function (name) { headers.set(name, consent[name]); });
28+
return fetch.call(this, input, Object.assign({}, init, { headers: headers }));
29+
};
30+
}
31+
if (typeof XMLHttpRequest !== 'undefined') {
32+
var targets = new WeakMap();
33+
var open = XMLHttpRequest.prototype.open;
34+
var setHeader = XMLHttpRequest.prototype.setRequestHeader;
35+
var send = XMLHttpRequest.prototype.send;
36+
XMLHttpRequest.prototype.open = function () {
37+
// open() can synchronously fire readystatechange; callers may send from
38+
// that callback, so classify before entering the native method.
39+
targets.set(this, applicable(arguments[1]));
40+
try { return open.apply(this, arguments); }
41+
catch (error) { targets.delete(this); throw error; }
42+
};
43+
XMLHttpRequest.prototype.setRequestHeader = function (name, value) {
44+
// These two reserved headers are adapter-owned. Avoid XHR appending a
45+
// stale caller grant to the current value supplied immediately at send.
46+
if (targets.get(this) && /^x-vapi-consent-(analytics|marketing)$/i.test(name)) return;
47+
return setHeader.call(this, name, value);
48+
};
49+
XMLHttpRequest.prototype.send = function () {
50+
if (targets.get(this)) {
51+
var consent = values();
52+
var xhr = this;
53+
Object.keys(consent).forEach(function (name) { setHeader.call(xhr, name, consent[name]); });
54+
}
55+
return send.apply(this, arguments);
56+
};
57+
}
58+
})();

‎fern/assets/privacy-search.js‎

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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

Comments
 (0)