Skip to content
This repository has been archived by the owner on Jun 9, 2024. It is now read-only.

Commit

Permalink
feat!: migrate to fastify org (#1)
Browse files Browse the repository at this point in the history
* feat!: migrate to fastify org

* update devDepenencies

* disable package-lock generation

* move types into types foler

* ues gitignore from skeleton

* rename dependabod.yaml to dependabot.yaml

* add pre-commit and stream line npm scripts

* move http2 to http2.test.js

* sync types with code

* fix typo in warning

* rename parameters to be more self explaining

* improve http2 test

* extract formatEntry, write tests

* fix linting

* add benchmarks for formatEntry
simplify formatEntry
improve typings

* remove unreachable code

* add newline

* improve performance of formatEntry

* add pre-commit

* improve performance, add warn option

* revert socket write only mode
add example.js

* add stresstest

* improve testing

Co-authored-by: uzlopak <aras.abbasi@googlemail.com>
  • Loading branch information
Eomm and Uzlopak authored Sep 29, 2022
1 parent ac8a7b2 commit ceae16c
Show file tree
Hide file tree
Showing 20 changed files with 675 additions and 164 deletions.
13 changes: 13 additions & 0 deletions .github/dependabot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "monthly"
open-pull-requests-limit: 10

- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "weekly"
open-pull-requests-limit: 10
32 changes: 16 additions & 16 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
name: CI workflow
on: [push, pull_request]
name: CI

on:
push:
paths-ignore:
- 'docs/**'
- '*.md'
pull_request:
paths-ignore:
- 'docs/**'
- '*.md'

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10.x, 12.x, 13.x, 14.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Install Dependencies
run: npm install --ignore-scripts
- name: Test
run: npm test
uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3
with:
lint: true
license-check: true
95 changes: 88 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
Expand All @@ -16,11 +21,12 @@ lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
Expand All @@ -36,15 +42,27 @@ build/Release
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

Expand All @@ -54,12 +72,75 @@ typings/
# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# next.js build output
# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# npm
# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# Vim swap files
*.swp

# macOS files
.DS_Store

# lock files
package-lock.json
yarn.lock
pnpm-lock.yaml
yarn.lock

# editor files
.vscode
.idea
Footer
4 changes: 0 additions & 4 deletions .npmignore

This file was deleted.

1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
6 changes: 4 additions & 2 deletions .taprc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
esm: false
ts: false
jsx: false
coverage: true
flow: true
flow: true

files:
- test/**/*.test.js
31 changes: 20 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
# Fastify Early Hints
# @fastify/early-hints


[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/)
![Continuous
Integration](https://github.com/zekth/fastify-early-hints/workflows/CI%20workflow/badge.svg)
Integration](https://github.com/fastify/fastify-early-hints/workflows/CI%20workflow/badge.svg)

Draft proposal of plugin handling the HTTP 103 code.
Based on : https://github.com/fastify/fastify/issues/2683

## Install
```
npm i fastify-early-hints
npm i @fastify/early-hints
```
## Options

You can pass the following options during the plugin registration:


You can pass the following options during the plugin registration:

```js
await fastify.register(import('@fastify/early-hints'), {
warn: true // default: false
})
```

- `warn` : indicates if the plugin should log warnings if invalid values are supplied as early hints

## Usage

- `eh.add`: Every call writes to the socket and returns a promise. Altought all the promises created throught the reply lifecycle are awaited in the `onSend` hook.

```javascript
const Fastify = require("fastify");
const eh = require("fastify-early-hints");
const eh = require("@fastify/early-hints");

const fastify = Fastify({ logger: true });
fastify.register(eh);
Expand All @@ -41,7 +55,7 @@ fastify.get("/", async (request, reply) => {

const start = async () => {
try {
await fastify.listen(3000);
await fastify.listen({ port: 3000 });
fastify.log.info(`server listening on ${fastify.server.address().port}`);
} catch (err) {
fastify.log.error(err);
Expand Down Expand Up @@ -76,16 +90,11 @@ Connection: keep-alive
{"hello":"world"}
```

## Todo

- Handling different cases combinations
- improve tests

## References

- https://httpwg.org/specs/rfc8297.html
- https://www.w3.org/TR/resource-hints/

## License

Licensed under [MIT](./LICENSE).<br/>
Licensed under [MIT](./LICENSE).
13 changes: 13 additions & 0 deletions benchmark/formatEntry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

const benchmark = require('benchmark')
const formatEntry = require('../lib/formatEntry')

const earlyHintString = 'Link: </style.css>; rel=preload; as=style'
const earlyHintObject = { href: '/', rel: 'preconnect', cors: 'anonymous', as: 'script' }

new benchmark.Suite()
.add('formatEntry: object', function () { formatEntry(earlyHintObject) }, { minSamples: 100 })
.add('formatEntry: string', function () { formatEntry(earlyHintString) }, { minSamples: 100 })
.on('cycle', function onCycle (event) { console.log(String(event.target)) })
.run()
32 changes: 32 additions & 0 deletions examples/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const Fastify = require('fastify')
const eh = require('../index')

const fastify = Fastify({ logger: false })
fastify.register(eh)

fastify.get('/', async (request, reply) => {
await reply.eh.add([
'Link: </style.css>; rel=preload; as=style',
'Link: </script.js>; rel=preload; as=script'
])
return { hello: 'world' }
})

fastify.get('/style.css', async (request, reply) => {
return 'body { }'
})
fastify.get('/script.js', async (request, reply) => {
return 'var v = 0xbadc0ffee'
})

const start = async () => {
try {
await fastify.listen({ port: 3000 })
fastify.log.info(`server listening on ${fastify.server.address().port}`)
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}

start()
20 changes: 0 additions & 20 deletions index.d.ts

This file was deleted.

Loading

0 comments on commit ceae16c

Please sign in to comment.