Skip to content

refc!: Updates for v0.2 #2

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

Draft
wants to merge 38 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
05cc223
docs: update license and readme
gamemaker1 Aug 26, 2023
1569e08
docs: fix formatting in readme
gamemaker1 Aug 27, 2023
d045b3a
ci: add `publish` step
gamemaker1 Aug 27, 2023
a137b20
refc: rename and move some types around, add comments
gamemaker1 Aug 27, 2023
2bb0072
refc: extract some functions into a `utilities.ts` file
gamemaker1 Aug 27, 2023
351f405
chore: fix formatting
gamemaker1 Aug 27, 2023
146610a
build: use `pkgroll` to bundle library
gamemaker1 Aug 27, 2023
49489fe
chore: update `package-lock.json`
gamemaker1 Aug 27, 2023
9e6a84b
feat: run examples as an external test
gamemaker1 Aug 27, 2023
3aab4dc
refc: extract more functions out, add a lot more comments
gamemaker1 Aug 28, 2023
c2747bc
chore: move jest config to proper location
gamemaker1 Sep 18, 2023
94e272f
chore: rename `source/ratelimit-header-parser.ts` -> `source/parser.ts`
gamemaker1 Sep 18, 2023
8965007
feat: support more headers
gamemaker1 Sep 18, 2023
e5eb05c
chore!: rename `parseRateLimit` to `getRateLimit`
gamemaker1 Sep 18, 2023
199cc12
docs: add installation section to readme
gamemaker1 Sep 18, 2023
ecbcb44
chore: fix permissions for git hook
gamemaker1 Sep 18, 2023
857480b
feat: add support for `x-mws-quota` headers (amazon)
gamemaker1 Sep 19, 2023
6c2b2d0
docs: fix syntax error in readme example
gamemaker1 Sep 19, 2023
e66d904
test: add more examples
gamemaker1 Sep 19, 2023
a06fd71
test: add `node-fetch` example
gamemaker1 Sep 20, 2023
915b5c9
test: add `deno/fetch` example
gamemaker1 Sep 20, 2023
6c0390e
ci: setup deno in example testing step
gamemaker1 Sep 20, 2023
90e16d8
chore: fix jest config
gamemaker1 Sep 21, 2023
b67fae7
feat: add support for parsing multiple rate limit headers
gamemaker1 Sep 21, 2023
ce1cff4
test: use `getRateLimits` in `node/http-get` example
gamemaker1 Sep 21, 2023
524e351
fix: convert all header names to lowercase if headers object is json
gamemaker1 Sep 21, 2023
9122e16
test: increase coverage
gamemaker1 Sep 21, 2023
30549d2
test: use `ServerResponse` instead of `Response`
gamemaker1 Sep 22, 2023
4d6c318
test: `Response` only works with recent versions
gamemaker1 Sep 22, 2023
fe96c7e
test: add date tests
gamemaker1 Sep 22, 2023
09e8a9c
ci: enable provenance statement generation
gamemaker1 Oct 9, 2023
0e2d81d
sort multiple limits so that the one with the fewest remaining comes …
nfriedly Oct 27, 2023
a7a353c
split tests into separate files
nfriedly Oct 27, 2023
0332c1f
fix flakey test by mocking clock
nfriedly Oct 27, 2023
54670f0
make remaining optional
nfriedly Oct 27, 2023
d75430f
docs: fix heading formatting
gamemaker1 Oct 31, 2023
a9df287
test: add failing test for ms auto-parse
gamemaker1 Oct 31, 2023
ae3dae1
docs: add test badge
gamemaker1 Oct 31, 2023
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
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# /.gitattributes
# Makes sure all line endings are LF

* text=auto eol=lf
101 changes: 101 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# /.github/workflows/ci.yaml
# GitHub actions workflow

name: CI

on: [push, pull_request]

jobs:
lint:
name: Lint
strategy:
matrix:
node-version: [lts/*]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout the repository
uses: actions/checkout@v3
- name: Use Node ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Lint code
run: |
npm ci
npm run lint
test-library:
name: Test (Library)
strategy:
matrix:
node-version: [lts/gallium, lts/*, latest]
os: [ubuntu-latest, windows-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout the repository
uses: actions/checkout@v3
- name: Use Node ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Run tests
run: |
npm ci
npm run test:lib
test-examples:
name: Test (Examples)
strategy:
matrix:
node-version: [lts/*]
deno-version: [v1.x]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout the repository
uses: actions/checkout@v3
- name: Use Node ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Use Deno ${{ matrix.deno-version }}
uses: denoland/setup-deno@v1
with:
deno-version: ${{ matrix.deno-version }}
- name: Run tests
run: |
npm ci
cd examples/
npm ci
npm test
publish:
name: Publish
needs: [lint, test-library, test-examples]
if: startsWith(github.ref, 'refs/tags/v')
runs-on: ubuntu-latest
permissions:
contents: write
id-token: write
steps:
- name: Checkout the repository
uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: lts/*
registry-url: https://registry.npmjs.org/
- name: Install dependencies
run: npm ci
- name: Publish to npm
run: npm publish --provenance
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Build package to upload to GitHub releases
run: |
npm pack
mv ratelimit-header-parser-*.tgz ratelimit-header-parser.tgz
- name: Create a Github release
uses: softprops/action-gh-release@v1
with:
files: ratelimit-header-parser.tgz
body:
You can view the changelog
[here](https://github.com/express-rate-limit/ratelimit-header-parser/blob/main/changelog.md).
46 changes: 0 additions & 46 deletions .github/workflows/ci.yml

This file was deleted.

6 changes: 6 additions & 0 deletions .npmrc
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
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# /.prettierignore
# Tells Prettier to ignore certain files

package.json
package-lock.json

Expand Down
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

59 changes: 0 additions & 59 deletions README.md

This file was deleted.

4 changes: 4 additions & 0 deletions config/husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run pre-commit
6 changes: 6 additions & 0 deletions examples/.npmrc
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
37 changes: 0 additions & 37 deletions examples/combined-fetch.js

This file was deleted.

10 changes: 10 additions & 0 deletions examples/deno/fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// /examples/deno/fetch.ts
// Uses `fetch` to hit the Imgur API.

import { getRateLimit } from '../../dist/index.mjs'

// Make a GET request to the Imgur API.
const response = await fetch('https://api.imgur.com/post/v1/posts/t/aww')
console.log('imgur ratelimit:', getRateLimit(response))

// > imgur ratelimit: { limit: 500, used: 1, remaining: 499, reset: 2023-08-25T04:16:48.000Z }
8 changes: 0 additions & 8 deletions examples/github-fetch.mjs

This file was deleted.

11 changes: 11 additions & 0 deletions examples/node/axios.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// /examples/node/axios.ts
// Uses `axios` to hit the GitHub API.

import axios from 'axios'
import { getRateLimit } from 'ratelimit-header-parser'

// Make a GET request to the GitHub API.
const response = await axios('https://api.github.com/orgs/express-rate-limit')
console.log('github ratelimit:', getRateLimit(response))

// > github ratelimit: { limit: 60, used: 1, remaining: 59, reset: 2023-08-25T04:16:48.000Z }
48 changes: 48 additions & 0 deletions examples/node/draft-7-fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// /examples/node/draft-7-fetch.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 { getRateLimit } 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:', getRateLimit(response))
// > parsed rate limit info: { limit: 5, used: 1, remaining: 4, reset: 2023-08-25T04:41:31.546Z }

// Cleanup the server.
server.close()
14 changes: 14 additions & 0 deletions examples/node/http-get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// /examples/node/http-get.ts
// Uses `http.get` to hit the Imgur API.

import https from 'node:https'
import { getRateLimits } from 'ratelimit-header-parser'

// Make a GET request to the Imgur API.
https.get('https://api.imgur.com/post/v1/posts/t/aww', (response) => {
console.log('imgur ratelimit:', getRateLimits(response))
return response.resume()
})

// > imgur ratelimit: { limit: 500, used: 1, remaining: 499, reset: 2023-09-19T14:19:30.297Z }
// > { limit: 12500, used: 1, remaining: 12499, reset: 2023-09-19T14:19:30.297Z }
Loading