-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Use
waitUntil
for Report call if available (#1838)
This attaches a `waitUntil` function to the SDK context and uses it on the call to Report if it is available. I've added the lookup of the `waitUntil` function on Vercel in Arcjet core, since Vercel supports many frameworks. Closes #884
- Loading branch information
1 parent
07e68dc
commit 2851021
Showing
4 changed files
with
95 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -687,6 +687,60 @@ describe("createClient", () => { | |
expect(decision.isAllowed()).toBe(true); | ||
}); | ||
|
||
test("calling `report` will use `waitUntil` if available", async () => { | ||
const [promise, resolve] = deferred(); | ||
|
||
const key = "test-key"; | ||
const fingerprint = | ||
"fp_1_ac8547705f1f45c5050f1424700dfa3f6f2f681b550ca4f3c19571585aea7a2c"; | ||
const context = { | ||
key, | ||
fingerprint, | ||
runtime: "test", | ||
log, | ||
characteristics: [], | ||
getBody: () => Promise.resolve(undefined), | ||
waitUntil: jest.fn((promise: Promise<unknown>) => { | ||
promise.then(() => resolve()); | ||
}), | ||
}; | ||
const details = { | ||
ip: "172.100.1.1", | ||
method: "GET", | ||
protocol: "http", | ||
host: "example.com", | ||
path: "/", | ||
headers: new Headers([["User-Agent", "curl/8.1.2"]]), | ||
extra: { | ||
"extra-test": "extra-test-value", | ||
}, | ||
email: "[email protected]", | ||
}; | ||
|
||
const router = { | ||
report: () => { | ||
return new ReportResponse({}); | ||
}, | ||
}; | ||
|
||
const client = createClient({ | ||
...defaultRemoteClientOptions, | ||
transport: createRouterTransport(({ service }) => { | ||
service(DecideService, router); | ||
}), | ||
}); | ||
const decision = new ArcjetAllowDecision({ | ||
ttl: 0, | ||
reason: new ArcjetTestReason(), | ||
results: [], | ||
}); | ||
client.report(context, details, decision, []); | ||
|
||
await promise; | ||
|
||
expect(context.waitUntil).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test("calling `report` will make RPC call with ALLOW decision", async () => { | ||
const key = "test-key"; | ||
const fingerprint = | ||
|