Skip to content
This repository has been archived by the owner on Apr 5, 2022. It is now read-only.

Commit

Permalink
feat: add auto-opt-out of FLoC
Browse files Browse the repository at this point in the history
  • Loading branch information
manekenpix authored and humphd committed Oct 8, 2021
1 parent c459e47 commit 77fed11
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ const service = new Satellite({

- `helmet`: the options to pass to the [helmet](https://www.npmjs.com/package/helmet) middleware. By default all options are turned on. Use `helmet: false` to disable helmet.

- `optOutFloc`: Enable adding the appropriate headers to Satellite to opt out of [Google's Floc](https://www.wired.com/story/google-floc-privacy-ad-tracking-explainer/). Disabled by default.

```js
const service = new Satellite({
options.optOutFloc: true,
});
```

- `beforeParsers`: an optional hook function that allows access to the `app` during creation prior to adding the body parsers

```js
Expand Down
8 changes: 8 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ function createApp(router, options = {}) {
// Parse application/json
app.use(express.json());

// Allow adding auto-opt-out of FLoC (disabled by default)
if (options.optOutFloc) {
app.use((req, res, next) => {
res.setHeader('Permissions-Policy', 'interest-cohort=()');
next();
});
}

// If beforeRouter is defined, add all middleware to the app
// before we define the router. Useful for adding middleware just
// before the router.
Expand Down
27 changes: 27 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,33 @@ describe('Satellite()', () => {
});
});

describe('auto-oup-out FLoC', () => {
test('Satellite() instance should include the { Permission-Policy: interest-cohort } header when the option is enabled', (done) => {
const service = createSatelliteInstance({
name: 'test',
optOutFloc: true,
});
service.start(port, async () => {
const res = await fetch(`${url}/always-200`);
expect(res.ok).toBe(true);
expect(res.headers.get('permissions-policy')).toBe('interest-cohort=()');
service.stop(done);
});
});

test('Satellite() instance should not include the { Permission-Policy: interest-cohort } header when the option is disabled', (done) => {
const service = createSatelliteInstance({
name: 'test',
});
service.start(port, async () => {
const res = await fetch(`${url}/always-200`);
expect(res.ok).toBe(true);
expect(res.headers.get('permissions-policy')).toBe(null);
service.stop(done);
});
});
});

test('start() should throw if called twice', (done) => {
service.start(port, () => {
expect(() => service.start(port2, async () => {})).toThrow();
Expand Down

0 comments on commit 77fed11

Please sign in to comment.