@standardserver/node 0.0.15 → 0.0.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +115 -0
- package/package.json +6 -5
package/README.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# @standardserver/node
|
|
2
|
+
|
|
3
|
+
<div align="center">
|
|
4
|
+
<a href="https://codecov.io/gh/middleapi/standardserver">
|
|
5
|
+
<img alt="codecov" src="https://codecov.io/gh/middleapi/standardserver/branch/main/graph/badge.svg">
|
|
6
|
+
</a>
|
|
7
|
+
<a href="https://www.npmjs.com/package/@standardserver/node">
|
|
8
|
+
<img alt="weekly downloads" src="https://img.shields.io/npm/dw/%40standardserver%2Fnode?logo=npm" />
|
|
9
|
+
</a>
|
|
10
|
+
<a href="https://github.com/middleapi/standardserver/blob/main/LICENSE">
|
|
11
|
+
<img alt="MIT License" src="https://img.shields.io/github/license/middleapi/standardserver?logo=open-source-initiative" />
|
|
12
|
+
</a>
|
|
13
|
+
<a href="https://discord.gg/TXEbwRBvQn">
|
|
14
|
+
<img alt="Discord" src="https://img.shields.io/discord/1308966753044398161?color=7389D8&label&logo=discord&logoColor=ffffff" />
|
|
15
|
+
</a>
|
|
16
|
+
<a href="https://deepwiki.com/middleapi/standardserver">
|
|
17
|
+
<img src="https://deepwiki.com/badge.svg" alt="Ask DeepWiki">
|
|
18
|
+
</a>
|
|
19
|
+
</div>
|
|
20
|
+
|
|
21
|
+
`@standardserver/node` adapts Node.js HTTP request and response objects to the transport-agnostic request and response model defined by Standard Server.
|
|
22
|
+
|
|
23
|
+
Standard Server provides a unified interface for client-server communication across HTTP and message-based transports. It lets you write handlers against the same request, response, body, and streaming primitives whether the underlying transport is the Fetch API, Node.js HTTP, HTTP/2, or a peer-style message channel.
|
|
24
|
+
|
|
25
|
+
This package is the Node.js adapter for that model. It converts between native Node request and response objects and the corresponding Standard Server shapes from `@standardserver/core`, while also exposing lower-level utilities for body parsing, URL normalization, abort signals, and server-sent events.
|
|
26
|
+
|
|
27
|
+
## Entry Point
|
|
28
|
+
|
|
29
|
+
The package exports a single entry point:
|
|
30
|
+
|
|
31
|
+
| Export | Purpose |
|
|
32
|
+
| ---------------------- | -------------------------------------------------------- |
|
|
33
|
+
| `@standardserver/node` | Node.js adapter helpers for requests, responses, and SSE |
|
|
34
|
+
|
|
35
|
+
## Package overview
|
|
36
|
+
|
|
37
|
+
The main entry point exposes four groups of helpers:
|
|
38
|
+
|
|
39
|
+
| Group | Exports | Purpose |
|
|
40
|
+
| ----------------------- | ---------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------ |
|
|
41
|
+
| Request and response | `toStandardLazyRequest()`, `sendStandardResponse()` | Adapt Node request and response objects to Standard Server |
|
|
42
|
+
| Body and event streams | `toStandardBody()`, `toNodeHttpBody()`, `toEventIterator()`, `toEventStream()` | Parse incoming bodies and serialize outgoing bodies, including SSE |
|
|
43
|
+
| Request utilities | `toStandardMethod()`, `toStandardUrl()`, `toAbortSignal()` | Normalize Node request metadata and connection lifecycle state |
|
|
44
|
+
| Types and option shapes | `NodeHttpRequest`, `NodeHttpResponse`, `ToStandardBodyOptions`, `ToNodeHttpBodyOptions`, `SendStandardResponseOptions` | Type request/response inputs and serializer options |
|
|
45
|
+
|
|
46
|
+
Use these helpers when you want Standard Server handlers to run in Node runtimes such as `node:http`, `node:http2`, Express-style middleware, or frameworks that expose Node-compatible request and response objects.
|
|
47
|
+
|
|
48
|
+
## Server-side request handling
|
|
49
|
+
|
|
50
|
+
Use `toStandardLazyRequest()` to convert an incoming Node request into a `StandardLazyRequest`, then `sendStandardResponse()` to write the resulting `StandardResponse` back to the client.
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import type { StandardLazyRequest, StandardResponse } from '@standardserver/core'
|
|
54
|
+
import { createServer } from 'node:http'
|
|
55
|
+
import { sendStandardResponse, toStandardLazyRequest } from '@standardserver/node'
|
|
56
|
+
|
|
57
|
+
async function handle(request: StandardLazyRequest): Promise<StandardResponse> {
|
|
58
|
+
const body = await request.resolveBody()
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
status: 200,
|
|
62
|
+
headers: { 'content-type': 'application/json' },
|
|
63
|
+
body: {
|
|
64
|
+
ok: true,
|
|
65
|
+
method: request.method,
|
|
66
|
+
url: request.url,
|
|
67
|
+
received: body,
|
|
68
|
+
},
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
createServer(async (req, res) => {
|
|
73
|
+
const standardRequest = toStandardLazyRequest(req, res)
|
|
74
|
+
const standardResponse = await handle(standardRequest)
|
|
75
|
+
|
|
76
|
+
await sendStandardResponse(res, standardResponse, {/** options */})
|
|
77
|
+
}).listen(3000)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
> [!TIP]
|
|
81
|
+
> When sending responses, you can pass additional options such as event-stream keep-alive.
|
|
82
|
+
|
|
83
|
+
## Resolving Body
|
|
84
|
+
|
|
85
|
+
`resolveBody(hint?)` determines how to parse the body using the following priority:
|
|
86
|
+
|
|
87
|
+
1. If the `standard-server` header is present, use it as the `StandardBodyHint`.
|
|
88
|
+
2. Otherwise, if `hint?` is provided, use it as the `StandardBodyHint`.
|
|
89
|
+
3. Otherwise, if `content-type` is one of the common types, parse accordingly.
|
|
90
|
+
4. Otherwise, if `content-length` exists, treat the body as `file`; if not, treat it as `octet-stream`.
|
|
91
|
+
|
|
92
|
+
For efficient communication, set the `standard-server` header to explicitly hint the body type, especially for file or binary streaming. For example, if you upload a file with a common `content-type` such as `application/json` but omit the `standard-server` header, the server may interpret it as JSON and parse it unexpectedly.
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
const response = await fetch('/upload', {
|
|
96
|
+
method: 'POST',
|
|
97
|
+
headers: {
|
|
98
|
+
'content-type': 'application/json',
|
|
99
|
+
'standard-server': 'file', // <- hint the body type to avoid misinterpretation
|
|
100
|
+
},
|
|
101
|
+
body: new Blob(['{"message": "Hello, world!"}'], { type: 'application/json' }),
|
|
102
|
+
})
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Learn more
|
|
106
|
+
|
|
107
|
+
For the higher-level project overview, see the root [Standard Server README](../../README.md).
|
|
108
|
+
|
|
109
|
+
## Sponsors
|
|
110
|
+
|
|
111
|
+
<p align="center">
|
|
112
|
+
<a href="https://cdn.jsdelivr.net/gh/middleapi/static/sponsors.svg">
|
|
113
|
+
<img src='https://cdn.jsdelivr.net/gh/middleapi/static/sponsors.svg' alt="Sponsors"/>
|
|
114
|
+
</a>
|
|
115
|
+
</p>
|
package/package.json
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@standardserver/node",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.19",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://standardserver.dev",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
|
-
"url": "git+https://github.com/
|
|
9
|
+
"url": "git+https://github.com/middleapi/standardserver.git",
|
|
10
10
|
"directory": "packages/node"
|
|
11
11
|
},
|
|
12
12
|
"sideEffects": false,
|
|
13
13
|
"exports": {
|
|
14
|
+
"./package.json": "./package.json",
|
|
14
15
|
".": {
|
|
15
16
|
"types": "./dist/index.d.mts",
|
|
16
17
|
"import": "./dist/index.mjs",
|
|
@@ -21,9 +22,9 @@
|
|
|
21
22
|
"dist"
|
|
22
23
|
],
|
|
23
24
|
"dependencies": {
|
|
24
|
-
"@standardserver/
|
|
25
|
-
"@standardserver/shared": "0.0.
|
|
26
|
-
"@standardserver/
|
|
25
|
+
"@standardserver/fetch": "0.0.19",
|
|
26
|
+
"@standardserver/shared": "0.0.19",
|
|
27
|
+
"@standardserver/core": "0.0.19"
|
|
27
28
|
},
|
|
28
29
|
"devDependencies": {
|
|
29
30
|
"@types/node": "^25.0.3",
|