Skip to content

Commit 4468e3c

Browse files
author
Guy Bedford
authored
feat: expose purgeSurrogateKey hostcall (#953)
1 parent aea826f commit 4468e3c

File tree

14 files changed

+1287
-5999
lines changed

14 files changed

+1287
-5999
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ jobs:
5656
path: "/home/runner/.cargo/bin/${{ matrix.crate }}"
5757
key: crate-cache-${{ matrix.crate }}-${{ matrix.version }}
5858
- name: Install ${{ matrix.crate }} ${{ matrix.version }}
59+
if: steps.cache-crate.output.cache-hit != 'true'
5960
run: cargo install ${{ matrix.crate }} ${{ matrix.options }} --version ${{ matrix.version }} --force
6061

6162
shellcheck:
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
8+
# purgeSurrogateKey
9+
10+
The **`purgeSurrogateKey()`** function is used to purge the given surrogate key string from Fastly's cache.
11+
12+
There are two purge modes: soft purge and hard purge, with hard purge as the default which clears all items
13+
from the cache immediately. When using a soft purge, stale entries are maintained in the cache, reducing
14+
orgin load, while also enabling stale revalidations.
15+
16+
See the [Fastly Purge Documentation](https://www.fastly.com/documentation/guides/concepts/edge-state/cache/purging/#surrogate-key-purge) for more information on caching and purge operations.
17+
18+
## Syntax
19+
20+
```js
21+
purgeSurrogateKey(surrogateKey, soft?)
22+
```
23+
24+
### Parameters
25+
26+
- `surrogateKey` _: string_
27+
- The surrogate key string
28+
- `soft?` _: boolean_
29+
- Enables a soft purge, retaining stale entries in the cache. Default is a hard purge.
30+
31+
### Return value
32+
33+
`undefined`.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
8+
# vCpuTime
9+
10+
The **`vCpuTime()`** function provides the vCPU time used by the current request handler in milliseconds.
11+
12+
## Syntax
13+
14+
```js
15+
vCpuTime()
16+
```
17+
18+
### Parameters
19+
20+
None.
21+
22+
### Return value
23+
24+
`undefined`.

integration-tests/js-compute/compare-downstream-response.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,9 @@ export async function compareDownstreamResponse(
2020
let errors = [];
2121
// Status
2222
if (configResponse.status != actualResponse.statusCode) {
23-
let bodySummary = '';
24-
try {
25-
bodySummary = (await actualResponse.body.text()).slice(0, 1000);
26-
} catch {}
2723
errors.push(
2824
new Error(
29-
`[DownstreamResponse: Status mismatch] Expected: ${configResponse.status} - Got: ${actualResponse.statusCode}\n${bodySummary ? `BODY: "${bodySummary}"` : ''}`,
25+
`[DownstreamResponse: Status mismatch] Expected: ${configResponse.status} - Got: ${actualResponse.statusCode}\n${actualBodyChunks.length ? `"${Buffer.concat(actualBodyChunks)}"` : ''}`,
3026
),
3127
);
3228
}
Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import {
2-
pass,
3-
ok,
4-
strictEqual
5-
} from "./assertions-throwing.js";
6-
import { routes } from "./routes.js";
7-
import { vCpuTime } from "fastly:runtime";
1+
import { pass, ok, strictEqual, assertThrows } from './assertions-throwing.js';
2+
import { routes } from './routes.js';
3+
import { purgeSurrogateKey, vCpuTime } from 'fastly:runtime';
84

9-
routes.set("/runtime/get-vcpu-ms", () => {
5+
routes.set('/runtime/get-vcpu-ms', () => {
106
const cpuTime = vCpuTime();
117
strictEqual(typeof cpuTime, 'number');
128
ok(cpuTime > 0);
@@ -21,3 +17,24 @@ routes.set("/runtime/get-vcpu-ms", () => {
2117
ok(cpuTime2 - cpuTime < 3000);
2218
return pass('ok');
2319
});
20+
21+
routes.set('/runtime/purge-surrogate-key-invalid', () => {
22+
assertThrows(
23+
() => {
24+
purgeSurrogateKey();
25+
},
26+
TypeError,
27+
'purgeSurrogateKey: At least 1 argument required, but only 0 passed',
28+
);
29+
return pass('ok');
30+
});
31+
32+
routes.set('/runtime/purge-surrogate-key-hard', () => {
33+
purgeSurrogateKey('test');
34+
return pass('ok');
35+
});
36+
37+
routes.set('/runtime/purge-surrogate-key-soft', () => {
38+
purgeSurrogateKey('test', true);
39+
return pass('ok');
40+
});

0 commit comments

Comments
 (0)