Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "h3", | ||
"version": "1.0.0", | ||
"description": "A better HTMLElement constructor", | ||
"homepage": "https://github.com/twhb/h3-js", | ||
"bugs": "https://github.com/twhb/h3-js/issues", | ||
"license": "ISC", | ||
"author": "Tristan Berger <tristanberger@gmail.com>", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/twhb/h3-js.git" | ||
} | ||
} | ||
"version": "1.0.1", | ||
"description": "Tiny JavaScript Server", | ||
"repository": "unjs/h3", | ||
"license": "MIT", | ||
"sideEffects": false, | ||
"exports": { | ||
"./package.json": "./package.json", | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"import": "./dist/index.mjs", | ||
"require": "./dist/index.cjs" | ||
} | ||
}, | ||
"main": "./dist/index.cjs", | ||
"module": "./dist/index.mjs", | ||
"types": "./dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "unbuild", | ||
"dev": "vitest", | ||
"lint": "eslint --ext ts,mjs,cjs .", | ||
"play": "jiti ./playground/index.ts", | ||
"profile": "0x -o -D .profile -P 'autocannon -c 100 -p 10 -d 40 http://localhost:$PORT' ./playground/server.cjs", | ||
"release": "pnpm test && pnpm build && changelogen --release && pnpm publish && git push --follow-tags", | ||
"test": "pnpm lint && vitest run --coverage" | ||
}, | ||
"dependencies": { | ||
"cookie-es": "^0.5.0", | ||
"destr": "^1.2.1", | ||
"radix3": "^1.0.0", | ||
"ufo": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"0x": "^5.4.1", | ||
"@types/express": "^4.17.14", | ||
"@types/node": "^18.11.9", | ||
"@types/supertest": "^2.0.12", | ||
"@vitest/coverage-c8": "^0.25.2", | ||
"autocannon": "^7.10.0", | ||
"changelogen": "^0.4.0", | ||
"connect": "^3.7.0", | ||
"eslint": "^8.27.0", | ||
"eslint-config-unjs": "^0.0.2", | ||
"express": "^4.18.2", | ||
"get-port": "^6.1.2", | ||
"jiti": "^1.16.0", | ||
"listhen": "^1.0.0", | ||
"node-fetch-native": "^1.0.1", | ||
"supertest": "^6.3.1", | ||
"typescript": "^4.8.4", | ||
"unbuild": "^0.9.4", | ||
"vitest": "^0.25.2" | ||
}, | ||
"packageManager": "pnpm@7.16.0" | ||
} |
167
README.md
@@ -1,38 +0,155 @@ | ||
# h3-js | ||
[![npm downloads](https://img.shields.io/npm/dm/h3.svg?style=flat-square)](https://npmjs.com/package/h3) | ||
[![version](https://img.shields.io/npm/v/h3/latest.svg?style=flat-square)](https://npmjs.com/package/h3) | ||
[![bundlephobia](https://img.shields.io/bundlephobia/min/h3/latest.svg?style=flat-square)](https://bundlephobia.com/result?p=h3) | ||
[![build status](https://img.shields.io/github/workflow/status/unjs/h3/ci/main?style=flat-square)](https://github.com/unjs/h3/actions) | ||
[![coverage](https://img.shields.io/codecov/c/gh/unjs/h3/main?style=flat-square)](https://codecov.io/gh/unjs/h3) | ||
[![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue?style=flat-square)](https://www.jsdocs.io/package/h3) | ||
A better HTMLElement constructor | ||
> H3 is a minimal h(ttp) framework built for high performance and portability | ||
## Example | ||
<!-- ![h3 - Tiny JavaScript Server](.github/banner.svg) --> | ||
```javascript | ||
var body = h('div', 'example', | ||
h('h1', null, 'Example'), | ||
h('label', {htmlFor: 'task'}, | ||
h('input', {id: 'task', type: 'checkbox', checked: true}), | ||
'Check out h3-js' | ||
) | ||
); | ||
## Features | ||
✔️ **Portable:** Works perfectly in Serverless, Workers, and Node.js | ||
✔️ **Minimal:** Small and tree-shakable | ||
✔️ **Modern:** Native promise support | ||
✔️ **Extendable:** Ships with a set of composable utilities but can be extended | ||
✔️ **Router:** Super fast route matching using [unjs/radix3](https://github.com/unjs/radix3) | ||
✔️ **Compatible:** Compatibility layer with node/connect/express middleware | ||
## Install | ||
```bash | ||
# Using npm | ||
npm install h3 | ||
# Using yarn | ||
yarn add h3 | ||
# Using pnpm | ||
pnpm add h3 | ||
``` | ||
## API | ||
## Usage | ||
### h(tagName, props, ...children) | ||
```ts | ||
import { createServer } from 'http' | ||
import { createApp, eventHandler, toNodeListener } from 'h3' | ||
Creates and returns a new HTMLElement. | ||
const app = createApp() | ||
app.use('/', eventHandler(() => 'Hello world!')) | ||
Primary interface: `tagName` sets the tag name, each key-value pair of `props` is copied onto the result, and each of `children` is appended to the result. | ||
createServer(toNodeListener(app)).listen(process.env.PORT || 3000) | ||
``` | ||
Conveniences: | ||
<details> | ||
<summary>Example using <a href="https://github.com/unjs/listhen">listhen</a> for an elegant listener.</summary> | ||
- `props.style` sets `result.style.cssText` | ||
- If `props` is falsy then it is ignored | ||
- If `props` is a string then it instead sets `result.className` | ||
- Children that are falsy are ignored | ||
- Children that are strings are converted to `Text` instances | ||
- Children that are Arrays are flattened into `children` | ||
```ts | ||
import { createApp, toNodeListener } from 'h3' | ||
import { listen } from 'listhen' | ||
## Setup | ||
const app = createApp() | ||
app.use('/', eventHandler(() => 'Hello world!')) | ||
Install: `npm install h3` | ||
listen(toNodeListener(app)) | ||
``` | ||
</details> | ||
Import: `const h = require('h3');` | ||
## Router | ||
The `app` instance created by `h3` uses a middleware stack (see [how it works](#how-it-works)) with the ability to match route prefix and apply matched middleware. | ||
To opt-in using a more advanced and convenient routing system, we can create a router instance and register it to app instance. | ||
```ts | ||
import { createApp, eventHandler, createRouter } from 'h3' | ||
const app = createApp() | ||
const router = createRouter() | ||
.get('/', eventHandler(() => 'Hello World!')) | ||
.get('/hello/:name', eventHandler(event => `Hello ${event.context.params.name}!`)) | ||
app.use(router) | ||
``` | ||
**Tip:** We can register same route more than once with different methods. | ||
Routes are internally stored in a [Radix Tree](https://en.wikipedia.org/wiki/Radix_tree) and matched using [unjs/radix3](https://github.com/unjs/radix3). | ||
## More app usage examples | ||
```js | ||
// Handle can directly return object or Promise<object> for JSON response | ||
app.use('/api', eventHandler((event) => ({ url: event.node.req.url })) | ||
// We can have better matching other than quick prefix match | ||
app.use('/odd', eventHandler(() => 'Is odd!'), { match: url => url.substr(1) % 2 }) | ||
// Handle can directly return string for HTML response | ||
app.use(eventHandler(() => '<h1>Hello world!</h1>')) | ||
// We can chain calls to .use() | ||
app.use('/1', eventHandler(() => '<h1>Hello world!</h1>')) | ||
.use('/2', eventHandler(() => '<h1>Goodbye!</h1>')) | ||
// Legacy middleware with 3rd argument are automatically promisified | ||
app.use(fromNodeMiddleware((req, res, next) => { req.setHeader('x-foo', 'bar'); next() })) | ||
// Lazy loaded routes using { lazy: true } | ||
app.use('/big', () => import('./big-handler'), { lazy: true }) | ||
``` | ||
## Utilities | ||
H3 has concept of compasable utilities that accept `event` (from `eventHandler((event) => {})`) as their first argument. This has several performance benefits over injecting them to `event` or `app` instances and global middleware commonly used in Node.js frameworks such as Express, which Only required code is evaluated and bundled and rest of utils can be tree-shaken when not used. | ||
### Built-in | ||
- `useRawBody(event, encoding?)` | ||
- `useBody(event)` | ||
- `useCookies(event)` | ||
- `useCookie(event, name)` | ||
- `setCookie(event, name, value, opts?)` | ||
- `deleteCookie(event, name, opts?)` | ||
- `useQuery(event)` | ||
- `getRouterParams(event)` | ||
- `send(event, data, type?)` | ||
- `sendRedirect(event, location, code=302)` | ||
- `getRequestHeaders(event, headers)` (alias: `getHeaders`) | ||
- `getRequestHeader(event, name)` (alias: `getHeader`) | ||
- `setResponseHeaders(event, headers)` (alias: `setHeaders`) | ||
- `setResponseHeader(event, name, value)` (alias: `setHeader`) | ||
- `appendResponseHeaders(event, headers)` (alias: `appendHeaders`) | ||
- `appendResponseHeader(event, name, value)` (alias: `appendHeader`) | ||
- `writeEarlyHints(event, links, callback)` | ||
- `sendStream(event, data)` | ||
- `sendError(event, error, debug?)` | ||
- `useMethod(event, default?)` | ||
- `isMethod(event, expected, allowHead?)` | ||
- `assertMethod(event, expected, allowHead?)` | ||
- `createError({ statusCode, statusMessage, data? })` | ||
- `sendProxy(event, { target, headers?, fetchOptions?, fetch?, sendStream? })` | ||
- `proxyRequest(event, { target, headers?, fetchOptions?, fetch?, sendStream? })` | ||
👉 You can learn more about usage in [JSDocs Documentation](https://www.jsdocs.io/package/h3#package-functions). | ||
### Add-ons | ||
More composable utilities can be found in community packages. | ||
- `validateBody(event, schema)` from [h3-typebox](https://github.com/kevinmarrec/h3-typebox) | ||
- `validateQuery(event, schema)` from [h3-typebox](https://github.com/kevinmarrec/h3-typebox) | ||
- `useValidatedBody(event, schema)` from [h3-zod](https://github.com/wobsoriano/h3-zod) | ||
- `useValidatedQuery(event, schema)` from [h3-zod](https://github.com/wobsoriano/h3-zod) | ||
## License | ||
MIT |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
74256
6
2022
0
156
4
19
2
1
1
+ Addedcookie-es@^0.5.0
+ Addeddestr@^1.2.1
+ Addedradix3@^1.0.0
+ Addedufo@^1.0.0
+ Addedcookie-es@0.5.0(transitive)
+ Addeddestr@1.2.2(transitive)
+ Addedradix3@1.1.2(transitive)
+ Addedufo@1.5.4(transitive)