Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Set default behavior to automatically convert line endings
* text=auto eol=lf
49 changes: 49 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
commit-message:
include: "scope"
prefix: "ci"
schedule:
interval: "monthly"
cooldown:
default-days: 7
allow:
- dependency-name: "*"
update-types:
- "version-update:semver-major"
open-pull-requests-limit: 10

- package-ecosystem: "npm"
directory: "/"
commit-message:
include: "scope"
prefix: "build"
schedule:
interval: "monthly"
cooldown:
default-days: 7
versioning-strategy: "increase-if-necessary"
allow:
- dependency-name: "*"
update-types:
- "version-update:semver-major"
open-pull-requests-limit: 10
groups:
# Production dependencies with breaking changes
dependencies:
dependency-type: "production"
patterns:
- "*"
# TypeScript related dependencies
dev-dependencies-typescript:
patterns:
- "@types/*"
- "tstyche"
- "typescript"
# Development dependencies with breaking changes
dev-dependencies:
dependency-type: "development"
patterns:
- "*"
34 changes: 34 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: CI

on:
push:
branches:
- main
- next
- "v*"
paths-ignore:
- "docs/**"
- "*.md"
pull_request:
paths-ignore:
- "docs/**"
- "*.md"

# This allows a subsequently queued workflow run to interrupt previous runs
concurrency:
group: "${{ github.workflow }}-${{ github.event.pull_request.head.label || github.head_ref || github.ref }}"
cancel-in-progress: true

permissions:
contents: read

jobs:
test:
permissions:
contents: write
pull-requests: write
uses: fastify/workflows/.github/workflows/plugins-ci.yml@ef591e2186785d5ab36b9fe6a79c7ce2f1d94e57 # v7.0.0
with:
node-versions: '["24", "26"]'
license-check: true
lint: true
19 changes: 19 additions & 0 deletions .github/workflows/lock-threads.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Lock Threads

on:
schedule:
- cron: "0 0 1 * *"
workflow_dispatch:

concurrency:
group: lock

permissions:
contents: read

jobs:
lock-threads:
permissions:
issues: write
pull-requests: write
uses: fastify/workflows/.github/workflows/lock-threads.yml@2073dc8e1f9e172bf42daa3843c9dbd31af1e8cb # v6.0.0
143 changes: 143 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

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

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

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

# nyc test coverage
.nyc_output

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

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.*
!.env.example

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

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist
.output

# 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 directory
.temp

# Sveltekit cache directory
.svelte-kit/

# vitepress build output
**/.vitepress/dist

# vitepress cache directory
**/.vitepress/cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# Firebase cache directory
.firebase/

# TernJS port file
.tern-port

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

# pnpm
.pnpm-store

# yarn v3
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions

# Vite files
vite.config.js.timestamp-*
vite.config.ts.timestamp-*
.vite/
3 changes: 3 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ignore-scripts=true
min-release-age=7
package-lock=false
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# @fastify/server

## Install

```shell
npm install @fastify/server
```

## Example

This example show the basic usage of create web server.

```typescript
import { createServer } from '@fastify/server'

const server = createServer({}, function(request, response) {
response.writeHead(200, { "Content-Type": "application/json" });
response.end(JSON.stringify({ data: "Hello World!" }));
})
server.listen({ port: 3000 })
```

## API

## Difference from `node:http`, `node:https` and `node:http2`.

### All-in-one `createServer`

The `createServer` will determine using which `node` built-in server
based on the options provided. You do not need to change the import
module and just safely update the options accordingly.

### Remove overload of `.listen`

The `.listen` method only takes `options` and `callback` as arguments.
It simplify the API and the process of normalize the arguments.

### Solves `localhost` dual-stack problem

`localhost` may resolve to either `IPv4` or `IPv6` based on operating
system. Sometimes operating system will fallback to `IPv4` if you listen
on `IPv6`. We instead listen on both to prevent this problem.

|Host | IPv4 | IPv6 |
|----------------------------------------------------------------------------------|----------------|------|
|`::` | ✅<sup>*</sup> | ✅ |
|`::` + [`ipv6Only`](https://nodejs.org/api/net.html#serverlistenoptions-callback) | 🚫 | ✅ |
|`0.0.0.0` | ✅ | 🚫 |
|`localhost` | ✅ | ✅ |
|`127.0.0.1` | ✅ | 🚫 |
|`::1` | 🚫 | ✅ |

<sup>*</sup> Using `::` for the address will listen on all IPv6 addresses and,
depending on OS, may also listen on [all IPv4
addresses](https://nodejs.org/api/net.html#serverlistenport-host-backlog-callback).

### Dual `Promise API` and `Callback API`

Unlike `Node.js` built-in, the `.listen` and `.close` will return `Promise`
when you do not provide `callback` function. You can choose to use either
API in your application.

## License

Licensed under [MIT](./LICENSE).
49 changes: 49 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"$schema": "https://biomejs.dev/schemas/2.5.6/schema.json",
"vcs": {
"enabled": true,
"clientKind": "git",
"useIgnoreFile": false
},
"files": {
"ignoreUnknown": false
},
"formatter": {
"enabled": true,
"indentStyle": "space"
},
"linter": {
"enabled": true,
"rules": {
"preset": "recommended",
"correctness": {
"noInnerDeclarations": "off"
}
}
},
"javascript": {
"formatter": {
"quoteStyle": "double"
}
},
"assist": {
"enabled": true,
"actions": {
"source": {
"organizeImports": "on"
}
}
},
"overrides": [
{
"includes": ["test/security.test.js"],
"linter": {
"rules": {
"suspicious": {
"noProto": "off"
}
}
}
}
]
}
11 changes: 11 additions & 0 deletions lib/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { createError } = require("@fastify/error");

/** @type {import('../types/errors.d.ts').ERR_SERVER_ALREADY_LISTEN} */
const ERR_SERVER_ALREADY_LISTEN = createError(
"ERR_SERVER_ALREADY_LISTEN",
"Listen method has been called more than once without closing.",
);

module.exports = {
ERR_SERVER_ALREADY_LISTEN,
};
Loading
Loading