-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: run examples as an external test
- Loading branch information
1 parent
49489fe
commit 9e6a84b
Showing
9 changed files
with
1,269 additions
and
324 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# .npmrc | ||
# Configuration for npm and pnpm | ||
|
||
# Uses the exact version instead of any within-patch-range version of an | ||
# installed package | ||
save-exact=true |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// /examples/draft-7-fetch.example.ts | ||
// Use `fetch`, and parse the `RateLimit` header from the IETF spec's 7th draft. | ||
|
||
// Note that example has a server and client together - normally they'd be in | ||
// separate files, likely on separate devices. | ||
|
||
// --- | ||
// `server.ts` | ||
// ---- | ||
|
||
import { default as express } from 'express' | ||
import { rateLimit } from 'express-rate-limit' | ||
|
||
// Create a rate-limited server. | ||
const app = express() | ||
app.use( | ||
rateLimit({ | ||
max: 5, | ||
windowMs: 60 * 1000, // 1 minute windows. | ||
legacyHeader: false, // Disable the `X-RateLimit-*` headers. | ||
standardHeaders: 'draft-7', // Use the combined `RateLimit` header. | ||
}), | ||
) | ||
|
||
// Register routes, and start the server. | ||
app.get('/', (req, res) => res.send('Hallo there!')) | ||
const { port, server } = await new Promise((resolve) => { | ||
const server = app.listen(0, () => | ||
resolve({ port: server.address().port, server }), | ||
) | ||
}) | ||
|
||
// --- | ||
// `client.ts` | ||
// --- | ||
|
||
import { parseRateLimit } from 'ratelimit-header-parser' | ||
|
||
// Fetch a response from the server. | ||
const response = await fetch(`http://localhost:${port}`) | ||
|
||
console.log('`RateLimit` header content:', response.headers.get('RateLimit')) | ||
// > `RateLimit` header content: limit=5, remaining=4, reset=60 | ||
console.log('parsed rate limit info:', parseRateLimit(response)) | ||
// > parsed rate limit info: { limit: 5, used: 1, remaining: 4, reset: 2023-08-25T04:41:31.546Z } | ||
|
||
// Cleanup the server. | ||
server.close() | ||
This comment has been minimized.
Sorry, something went wrong. |
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
Oops, something went wrong.
I bet we could move this to the
app.get('/', ...)
handler, possibly inside aprocess.nextTick()
. Then it wouldn't look like it was part of theclient.ts
half