Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for debounce function #664

Merged
merged 1 commit into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion packages/helix-shared-bounce/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,28 @@
.with(bounce, { responder: fast });
```

## Disabling Bouncing for certain requests

The plugin options can take a `debounce` function property that is called with the `request` and
`context`. if it returns a non falsy value the wrapped function will not be bounced.

```js

function debounce(req, context) {
if (context.pathInfo.suffix === '/about') {
return true;
}
return false;
}

module.exports.main = wrap(main)
.with(bounce, { responder: fast, debounce });

```

## Disabling Bouncing for Tests

If you are testing a function that is using `bounce` locally, you might want to disable the bouncing for the duration of the test, because you would otherwise have
to mock a larger part of the runtime.

To disable bouncing for tests, set the `HELIX_DEBOUNCE` environment variable to any value.
To disable bouncing for tests, set the `HELIX_DEBOUNCE` environment variable to any value.
6 changes: 4 additions & 2 deletions packages/helix-shared-bounce/src/bounce.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ const timer = {
setTimeout: async (delay) => new Promise((resolve) => setTimeout(resolve, delay)),
};

function bounce(func, { responder, timeout = 500 }) {
function bounce(func, { responder, timeout = 500, debounce = () => '' }) {
return async (request, context) => {
const id = request.headers.get('x-hlx-bounce-id') || process.env.HELIX_DEBOUNCE;
const id = request.headers.get('x-hlx-bounce-id')
|| process.env.HELIX_DEBOUNCE
|| debounce(request, context);
if (id) {
// use the provided bounce id
context.invocation = context.invocation || {};
Expand Down
51 changes: 46 additions & 5 deletions packages/helix-shared-bounce/test/bounce.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ describe('Bounce Wrapper Unit Tests', () => {
invocation: {
},
});
assert.equal(response.status, 200, 'universal function should be executed');
assert.equal(slowBounceId, fastBounceId);
assert.strictEqual(response.status, 200, 'universal function should be executed');
assert.strictEqual(slowBounceId, fastBounceId);
assert.ok((await response.text()).startsWith('ok, job '));
});

Expand Down Expand Up @@ -117,9 +117,8 @@ describe('Bounce Wrapper Unit Tests', () => {
}), {
log,
});
assert.equal(response.status, 200, 'universal function should be executed');
console.log(slowBounceId, fastBounceId);
assert.equal(slowBounceId, fastBounceId);
assert.strictEqual(response.status, 200, 'universal function should be executed');
assert.strictEqual(slowBounceId, fastBounceId);
assert.ok((await response.text()).startsWith('I am ready soon, check status at '));
});

Expand Down Expand Up @@ -201,4 +200,46 @@ describe('Bounce Wrapper Unit Tests', () => {
});
assert.equal(response.status, 502, 'failing bounce should become 502');
});

it('Debounces with function', async () => {
let slowBounceId;

const slowfunction = async (_, context) => {
slowBounceId = context.invocation.bounceId;
return new Response(`ok, job ${context.invocation.bounceId} completed.`);
};

const actualfunct = wrap(slowfunction).with(bounce, { debounce: () => '1234' });

nock('http://localhost').post('/').reply(async function fakeruntime(uri, requestBody) {
const request = new Request(`http://localhost${uri}`, {
headers: this.req.headers,
body: JSON.stringify((requestBody)),
method: this.req.method,
});

const response = await actualfunct(request, {
log,
invocation: {
},
});
const body = await response.text();
return [response.status, body];
});

const response = await actualfunct(new Request('http://localhost', {
body: JSON.stringify({ foo: 'bar' }),
method: 'POST',
headers: {
'content-type': 'application/json',
},
}), {
log,
invocation: {
},
});
assert.strictEqual(response.status, 200, 'universal function should be executed');
assert.strictEqual(slowBounceId, '1234');
assert.ok((await response.text()).startsWith('ok, job '));
});
});