Skip to content

Commit

Permalink
Merge branch 'main' into pr/yassilah/746
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jan 16, 2023
2 parents 71e1ec8 + b773ebe commit ca1cbc3
Show file tree
Hide file tree
Showing 53 changed files with 2,057 additions and 1,336 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"quotes": 0,
"keyword-spacing": 0,
"no-undef": 0,
"indent": 0,
"unicorn/catch-error-name": 0,
"unicorn/no-null": 0,
"unicorn/no-useless-undefined": 0,
Expand Down
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.txt text eol=lf
20 changes: 18 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ on:
- main

jobs:
ci:
lint:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
with:
Expand All @@ -23,6 +22,23 @@ jobs:
cache: pnpm
- run: pnpm install
- run: pnpm lint

ci:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]

steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- run: corepack enable
- uses: actions/setup-node@v3
with:
node-version: "16"
cache: pnpm
- run: pnpm install
- run: pnpm test:types
- run: pnpm build
- run: pnpm vitest --coverage
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<h1 align="center">⚗️ Nitro</h1>
<p align="center">Build and Deploy Universal JavaScript Servers</p>

## Why using Nitro?
## Why use Nitro?

Nitro provides a powerful toolchain and a runtime framework from the [UnJS](https://github.com/unjs) ecosystem to build and deploy **any JavaScript server, anywhere!**

Expand Down
2 changes: 1 addition & 1 deletion docs/content/1.guide/1.introduction/3.routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default eventHandler(async event => {
```js
// routes/user.post.ts
export default eventHandler(async event => {
const body = await useBody(event)
const body = await readBody(event)
// TODO: Handle body and update user
return `User updated!`
})
Expand Down
87 changes: 58 additions & 29 deletions docs/content/1.guide/1.introduction/5.cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,58 +6,87 @@ description: Nitro provides a powerful caching system built on top of the storag
## Usage

```js
const cachedFn = cachedEventHandler(fn, options)
const cachedFn = cachedEventHandler(fn, options);
```

### Options

* `name`: Handler name. It will be guessed from function name if not provided and fallback to `_` otherwise.
* `group`: Part of cache name. Useful to organize cache storage.
* `getKey`: A function that accepts same arguments of normal function and should generate cache key. If not provided, a built-in hash function will be used.
* `integrity`: A value that changing it, will invalidate all caches for function. By default will be computed from **function code**.
* `maxAge`: Maximum age that cache is valid in seconds. Default is `1` second.
* `swr`: Enable Stale-While-Revalidate behavior. Enabled by default.
* `shouldInvalidateCache`: A function that returns a boolean to invalidate the current cache and create a new one.
- `name`: Handler name. It will be guessed from function name if not provided and fallback to `_` otherwise.
- `group`: Part of cache name. Useful to organize cache storage.
- `getKey`: A function that accepts same arguments of normal function and should generate cache key. If not provided, a built-in hash function will be used.
- `integrity`: A value that changing it, will invalidate all caches for function. By default will be computed from **function code**.
- `maxAge`: Maximum age that cache is valid in seconds. Default is `1` second.
- `swr`: Enable Stale-While-Revalidate behavior. Enabled by default.
- `base`: Name of the storage mointpoint to use for caching (`/cache` by default)
- `shouldInvalidateCache`: A function that returns a boolean to invalidate the current cache and create a new one.

## Examples

**Example:** Cache an API handler

```js
// routes/cached.ts
const myFn = cachedEventHandler(async () => {
new Promise(resolve => setTimeout(resolve, 1000))
return `Response generated at ${new Date().toISOString()}`
}, {
swr: true
})
const myFn = cachedEventHandler(
async () => {
new Promise((resolve) => setTimeout(resolve, 1000));
return `Response generated at ${new Date().toISOString()}`;
},
{
swr: true,
}
);
```

**Example:** Cache a utility function

```js
// utils/index.ts
const myFn = cachedFunction(async () => {
new Promise(resolve => setTimeout(resolve, 1000))
return Math.random()
}, {
swr: true
})
const myFn = cachedFunction(
async () => {
new Promise((resolve) => setTimeout(resolve, 1000));
return Math.random();
},
{
swr: true,
}
);
```

**Example:** Enable Cache on a group of routes (**🧪 Experimental!**)

```js
// nitro.config.ts
import {
defineNitroConfig
} from 'nitropack'
import { defineNitroConfig } from "nitropack";

export default defineNitroConfig({
routeRules: {
'/blog/**': {
swr: true
}
}
})
routeRules: {
"/blog/**": {
swr: true,
},
},
});
```

**Example:** Set cache storage mountpoint for a group of routes (**🧪 Experimental!**)

```js
// nitro.config.ts
import { defineNitroConfig } from "nitropack";

export default defineNitroConfig({
storage: {
"my-custom-storage": {
driver: "redis",
url: "redis://localhost:6379",
},
},
routeRules: {
"/blog/**": {
swr: true,
cache: {
base: "/my-custom-storage",
},
},
},
});
```
20 changes: 20 additions & 0 deletions docs/content/2.deploy/providers/render.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,24 @@ Nitro supports deploying on [Render](https://render.com/) with minimal configura

1. Click 'Create Web Service'.

## Infrastructure as Code (IaC)

1. Create a file called `render.yaml` with following content at the root of your repository.
> This file followed by [Infrastructure as Code](https://render.com/docs/infrastructure-as-code) on Render
```
services:
- type: web
name: <PROJECTNAME>
env: node
branch: main
startCommand: node .output/server/index.mjs
buildCommand: npm install && npm run build
envVars:
- key: NITRO_PRESET
value: render-com
```
2. [Create a new Blueprint Instance](https://dashboard.render.com/select-repo?type=blueprint) and select the repository containing your `render.yaml` file.


You should be good to go!
14 changes: 4 additions & 10 deletions docs/content/3.config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ Server runtime configuration.

**Note:**: `nitro` namespace is reserved.


<!-- Features -->

## `experimental`
Expand All @@ -59,7 +58,10 @@ Storage configuration.

- Default: `false`

Enable timing information.
Enable timing information:

- Nitro startup time log
- `Server-Timing` header on HTTP responses

## `renderer`

Expand Down Expand Up @@ -171,8 +173,6 @@ An array of paths to nitro plugins. They will be executed by order on the first

A map from dynamic virtual import names to their contents or an (async) function that returns it.



<!-- Routing -->

## `baseURL`
Expand Down Expand Up @@ -242,7 +242,6 @@ Route options. It is a map from route pattern (following [unjs/radix3](https://g

When `cache` option is set, handlers matching pattern will be automatically wrapped with `defineCachedEventHandler`. See [Cache API](/guide/introduction/cache) for all available cache options. (`swr: true|number` is shortcut for `cache: { swr: true, maxAge: number }`.)


**Example:**

```js
Expand All @@ -267,8 +266,6 @@ Prerendered options. Any route specified will be fetched during the build and co

If `crawlLinks` option is set to `true`, nitro starts with `/` by default (or all routes in `routes` array) and for HTML pages extracts `<a href="">` tags and prerender them as well.



<!-- Directories -->

## `rootDir`
Expand Down Expand Up @@ -297,8 +294,6 @@ nitro's temporary working directory for generating build-related files.

Output directories for production bundle.



<!-- Advanced -->

## `dev`
Expand Down Expand Up @@ -335,7 +330,6 @@ Preview and deploy command hints are usually filled by deployment presets.

A custom error handler function for development errors.


<!-- Rollup -->

## `rollupConfig`
Expand Down
29 changes: 15 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"docs:dev": "vitepress dev docs",
"docs:serve": "vitepress serve docs",
"lint": "eslint --ext .ts,.mjs,.cjs . && prettier -c src",
"lint:fix": "eslint --fix --ext .ts,.mjs,.cjs . && prettier --write -c src",
"nitro": "jiti ./src/cli.ts",
"prepack": "pnpm build",
"release": "pnpm test && pnpm build && changelogen --release && pnpm publish && git push --follow-tags",
Expand All @@ -43,14 +44,14 @@
},
"dependencies": {
"@cloudflare/kv-asset-handler": "^0.3.0",
"@netlify/functions": "^1.3.0",
"@netlify/functions": "^1.4.0",
"@rollup/plugin-alias": "^4.0.2",
"@rollup/plugin-commonjs": "^24.0.0",
"@rollup/plugin-inject": "^5.0.3",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-replace": "^5.0.2",
"@rollup/plugin-terser": "^0.2.1",
"@rollup/plugin-terser": "^0.3.0",
"@rollup/plugin-wasm": "^6.1.1",
"@rollup/pluginutils": "^5.0.2",
"@vercel/nft": "^0.22.6",
Expand All @@ -63,7 +64,7 @@
"defu": "^6.1.1",
"destr": "^1.2.2",
"dot-prop": "^7.2.0",
"esbuild": "^0.16.14",
"esbuild": "^0.17.0",
"escape-string-regexp": "^5.0.0",
"etag": "^1.8.1",
"fs-extra": "^11.1.0",
Expand All @@ -73,12 +74,12 @@
"hookable": "^5.4.2",
"http-proxy": "^1.18.1",
"is-primitive": "^3.0.1",
"jiti": "^1.16.1",
"jiti": "^1.16.2",
"klona": "^2.0.5",
"knitwork": "^1.0.0",
"listhen": "^1.0.1",
"mime": "^3.0.0",
"mlly": "^1.0.0",
"mlly": "^1.1.0",
"mri": "^1.2.0",
"node-fetch-native": "^1.0.1",
"ofetch": "^1.0.0",
Expand All @@ -88,7 +89,7 @@
"pkg-types": "^1.0.1",
"pretty-bytes": "^6.0.0",
"radix3": "^1.0.0",
"rollup": "^3.9.1",
"rollup": "^3.10.0",
"rollup-plugin-visualizer": "^5.9.0",
"scule": "^1.0.0",
"semver": "^7.3.8",
Expand All @@ -98,32 +99,32 @@
"std-env": "^3.3.1",
"ufo": "^1.0.1",
"unenv": "^1.0.1",
"unimport": "^1.1.0",
"unimport": "^1.2.0",
"unstorage": "^1.0.1"
},
"devDependencies": {
"@types/aws-lambda": "^8.10.109",
"@types/etag": "^1.8.1",
"@types/fs-extra": "^9.0.13",
"@types/fs-extra": "^11.0.1",
"@types/http-proxy": "^1.17.9",
"@types/node-fetch": "^2.6.2",
"@types/semver": "^7.3.13",
"@types/serve-static": "^1.15.0",
"@vitest/coverage-c8": "^0.26.3",
"@vitest/coverage-c8": "^0.27.1",
"c8": "^7.12.0",
"changelogen": "^0.4.0",
"edge-runtime": "2.0.2",
"eslint": "^8.31.0",
"eslint": "^8.32.0",
"eslint-config-unjs": "^0.0.3",
"execa": "^6.1.0",
"expect-type": "^0.15.0",
"miniflare": "^2.11.0",
"prettier": "^2.8.1",
"prettier": "^2.8.3",
"typescript": "^4.9.4",
"unbuild": "^1.0.2",
"vitest": "^0.26.3"
"unbuild": "^1.1.1",
"vitest": "^0.27.1"
},
"packageManager": "pnpm@7.22.0",
"packageManager": "pnpm@7.25.0",
"engines": {
"node": "^14.16.0 || ^16.11.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"
},
Expand Down
Loading

0 comments on commit ca1cbc3

Please sign in to comment.