@centralping/ergo 0.1.0-beta.1
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/CHANGELOG.md +25 -0
- package/LICENSE +21 -0
- package/README.md +139 -0
- package/http/accepts.js +69 -0
- package/http/authorization.js +65 -0
- package/http/body.js +311 -0
- package/http/cache-control.js +123 -0
- package/http/compress.js +157 -0
- package/http/cookie.js +39 -0
- package/http/cors.js +79 -0
- package/http/csrf.js +76 -0
- package/http/handler.js +74 -0
- package/http/index.js +13 -0
- package/http/json-api-query.js +53 -0
- package/http/logger.js +167 -0
- package/http/main.js +140 -0
- package/http/precondition.js +53 -0
- package/http/prefer.js +36 -0
- package/http/rate-limit.js +66 -0
- package/http/security-headers.js +62 -0
- package/http/send.js +399 -0
- package/http/timeout.js +47 -0
- package/http/url.js +47 -0
- package/http/validate.js +84 -0
- package/lib/accepts.js +49 -0
- package/lib/attach-instance.js +23 -0
- package/lib/authorization.js +187 -0
- package/lib/body/multiparse.js +173 -0
- package/lib/body/multipart/headers.js +69 -0
- package/lib/body/writer.js +73 -0
- package/lib/cookie/cookie.js +192 -0
- package/lib/cookie/index.js +14 -0
- package/lib/cookie/jar.js +106 -0
- package/lib/cookie/parse.js +101 -0
- package/lib/cors.js +191 -0
- package/lib/csrf.js +96 -0
- package/lib/from-connect.js +69 -0
- package/lib/json-api-query/index.js +25 -0
- package/lib/json-api-query/schema.json +105 -0
- package/lib/json-api-query/validate.js +56 -0
- package/lib/link.js +96 -0
- package/lib/prefer.js +52 -0
- package/lib/query.js +113 -0
- package/lib/rate-limit.js +115 -0
- package/lib/sanitize-quoted-string.js +28 -0
- package/lib/security-headers.js +125 -0
- package/lib/validate.js +80 -0
- package/lib/vary.js +40 -0
- package/package.json +158 -0
- package/types/http/accepts.d.ts +8 -0
- package/types/http/authorization.d.ts +8 -0
- package/types/http/body.d.ts +20 -0
- package/types/http/cache-control.d.ts +16 -0
- package/types/http/compress.d.ts +5 -0
- package/types/http/cookie.d.ts +2 -0
- package/types/http/cors.d.ts +9 -0
- package/types/http/csrf.d.ts +9 -0
- package/types/http/handler.d.ts +2 -0
- package/types/http/index.d.ts +1 -0
- package/types/http/json-api-query.d.ts +2 -0
- package/types/http/logger.d.ts +9 -0
- package/types/http/main.d.ts +142 -0
- package/types/http/precondition.d.ts +44 -0
- package/types/http/prefer.d.ts +2 -0
- package/types/http/rate-limit.d.ts +17 -0
- package/types/http/security-headers.d.ts +10 -0
- package/types/http/send.d.ts +8 -0
- package/types/http/timeout.d.ts +5 -0
- package/types/http/url.d.ts +2 -0
- package/types/http/validate.d.ts +6 -0
- package/types/lib/accepts.d.ts +7 -0
- package/types/lib/attach-instance.d.ts +19 -0
- package/types/lib/authorization.d.ts +6 -0
- package/types/lib/body/multiparse.d.ts +9 -0
- package/types/lib/body/multipart/headers.d.ts +2 -0
- package/types/lib/body/writer.d.ts +2 -0
- package/types/lib/cookie/cookie.d.ts +32 -0
- package/types/lib/cookie/index.d.ts +2 -0
- package/types/lib/cookie/jar.d.ts +8 -0
- package/types/lib/cookie/parse.d.ts +19 -0
- package/types/lib/cors.d.ts +9 -0
- package/types/lib/csrf.d.ts +32 -0
- package/types/lib/from-connect.d.ts +47 -0
- package/types/lib/json-api-query/index.d.ts +123 -0
- package/types/lib/json-api-query/validate.d.ts +5 -0
- package/types/lib/link.d.ts +37 -0
- package/types/lib/prefer.d.ts +36 -0
- package/types/lib/query.d.ts +6 -0
- package/types/lib/rate-limit.d.ts +76 -0
- package/types/lib/sanitize-quoted-string.d.ts +19 -0
- package/types/lib/security-headers.d.ts +24 -0
- package/types/lib/validate.d.ts +16 -0
- package/types/lib/vary.d.ts +17 -0
- package/types/utils/attempt.d.ts +2 -0
- package/types/utils/buffers/index.d.ts +2 -0
- package/types/utils/buffers/match.d.ts +10 -0
- package/types/utils/buffers/split.d.ts +10 -0
- package/types/utils/compose-with.d.ts +40 -0
- package/types/utils/compose.d.ts +83 -0
- package/types/utils/flat-array.d.ts +2 -0
- package/types/utils/get.d.ts +5 -0
- package/types/utils/http-errors.d.ts +22 -0
- package/types/utils/iterables/buffer-split.d.ts +2 -0
- package/types/utils/iterables/chain.d.ts +2 -0
- package/types/utils/iterables/exec-all.d.ts +2 -0
- package/types/utils/iterables/filter.d.ts +2 -0
- package/types/utils/iterables/for-each.d.ts +2 -0
- package/types/utils/iterables/from-stream.d.ts +2 -0
- package/types/utils/iterables/index.d.ts +10 -0
- package/types/utils/iterables/map.d.ts +2 -0
- package/types/utils/iterables/range.d.ts +24 -0
- package/types/utils/iterables/reduce.d.ts +2 -0
- package/types/utils/iterables/take.d.ts +2 -0
- package/types/utils/observables/buffer-split.d.ts +2 -0
- package/types/utils/observables/chain.d.ts +2 -0
- package/types/utils/observables/index.d.ts +4 -0
- package/types/utils/observables/map.d.ts +2 -0
- package/types/utils/observables/take.d.ts +2 -0
- package/types/utils/pick.d.ts +2 -0
- package/types/utils/set.d.ts +2 -0
- package/types/utils/streams/index.d.ts +2 -0
- package/types/utils/streams/meter.d.ts +5 -0
- package/types/utils/streams/tee.d.ts +2 -0
- package/types/utils/type.d.ts +2 -0
- package/utils/attempt.js +37 -0
- package/utils/buffers/index.js +13 -0
- package/utils/buffers/match.js +96 -0
- package/utils/buffers/split.js +55 -0
- package/utils/compose-with.js +232 -0
- package/utils/compose.js +165 -0
- package/utils/flat-array.js +24 -0
- package/utils/get.js +39 -0
- package/utils/http-errors.js +113 -0
- package/utils/iterables/buffer-split.js +117 -0
- package/utils/iterables/chain.js +32 -0
- package/utils/iterables/exec-all.js +42 -0
- package/utils/iterables/filter.js +35 -0
- package/utils/iterables/for-each.js +33 -0
- package/utils/iterables/from-stream.js +29 -0
- package/utils/iterables/index.js +21 -0
- package/utils/iterables/map.js +47 -0
- package/utils/iterables/range.js +34 -0
- package/utils/iterables/reduce.js +43 -0
- package/utils/iterables/take.js +36 -0
- package/utils/observables/buffer-split.js +109 -0
- package/utils/observables/chain.js +33 -0
- package/utils/observables/index.js +19 -0
- package/utils/observables/map.js +34 -0
- package/utils/observables/take.js +40 -0
- package/utils/pick.js +41 -0
- package/utils/set.js +38 -0
- package/utils/streams/index.js +11 -0
- package/utils/streams/meter.js +98 -0
- package/utils/streams/tee.js +84 -0
- package/utils/type.js +47 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
## [0.1.0-beta.1] - 2026-05-20
|
|
6
|
+
|
|
7
|
+
### Changed
|
|
8
|
+
|
|
9
|
+
- **BREAKING**: Renamed package from `ergo` to `@centralping/ergo`.
|
|
10
|
+
- Updated `content-type` dependency to v2.0.0 (string-only parse API).
|
|
11
|
+
- Added TypeScript declaration files (`.d.ts`) generated from JSDoc.
|
|
12
|
+
- Added `CHANGELOG.md` to published npm tarball.
|
|
13
|
+
- Updated release workflow to support pre-release dist-tags.
|
|
14
|
+
|
|
15
|
+
## [0.1.0] - 2026-03-20
|
|
16
|
+
|
|
17
|
+
### Added
|
|
18
|
+
|
|
19
|
+
- Initial development release as `ergo` (unscoped, never published to npm).
|
|
20
|
+
- Composable Fast Fail middleware pipeline with four-stage architecture (Negotiation, Authorization, Validation, Execution).
|
|
21
|
+
- RFC-compliant middleware: content negotiation, authorization (Basic/Bearer), body parsing (JSON/multipart/URL-encoded), cookie handling, CORS, CSRF protection, rate limiting, security headers, cache control, conditional requests, compression, and more.
|
|
22
|
+
- Structured error responses via RFC 9457 Problem Details.
|
|
23
|
+
- Prefer header support (RFC 7240).
|
|
24
|
+
- Web Linking support (RFC 8288).
|
|
25
|
+
- Pure ESM with Node.js >= 22.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019-present Jason Cust
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<picture>
|
|
3
|
+
<source media="(prefers-color-scheme: dark)" srcset="assets/logo-wordmark-dark.svg">
|
|
4
|
+
<source media="(prefers-color-scheme: light)" srcset="assets/logo-wordmark-light.svg">
|
|
5
|
+
<img alt="ergo" src="assets/logo-wordmark-light.svg" width="240">
|
|
6
|
+
</picture>
|
|
7
|
+
</p>
|
|
8
|
+
|
|
9
|
+
[](https://github.com/CentralPing/ergo/actions/workflows/ci.yml)
|
|
10
|
+
[](https://codecov.io/gh/CentralPing/ergo)
|
|
11
|
+
[](https://www.npmjs.com/package/@centralping/ergo)
|
|
12
|
+
[](https://scorecard.dev/viewer/?uri=github.com/CentralPing/ergo)
|
|
13
|
+
[](https://nodejs.org)
|
|
14
|
+
[](LICENSE)
|
|
15
|
+
|
|
16
|
+
A **Fast Fail** REST API toolkit for Node.js. _ergo_ (error or go) provides composable, stream-native middleware organized around the principle that a server should fail as early as possible -- before doing any expensive work -- through four ordered stages: **Negotiation, Authorization, Validation, and Execution**. Every behavior is backed by an IETF RFC or industry standard, not invented conventions.
|
|
17
|
+
|
|
18
|
+
## Why ergo?
|
|
19
|
+
|
|
20
|
+
- **Fail Fast by design** -- Errors are caught at the earliest possible stage. You never parse a request body for an unauthenticated user, and you never execute business logic on invalid input. This principle produces more robust software with fewer defects ([Shore, "Fail Fast", IEEE Software 2004](https://www.martinfowler.com/ieeeSoftware/failFast.pdf)) and is a core reliability pillar in the [AWS Well-Architected Framework](https://docs.aws.amazon.com/wellarchitected/latest/reliability-pillar/rel_mitigate_interaction_failure_limit_retries.html).
|
|
21
|
+
- **RESTful-first via RFCs** -- Every middleware implements a specific RFC or standard. Content negotiation follows [RFC 9110](https://www.rfc-editor.org/rfc/rfc9110), error responses use [RFC 9457 Problem Details](https://www.rfc-editor.org/rfc/rfc9457), cookies follow [RFC 6265](https://www.rfc-editor.org/rfc/rfc6265). No proprietary patterns.
|
|
22
|
+
- **Defense in depth** -- Input validation happens [as early as possible in the data flow](https://cheatsheetseries.owasp.org/cheatsheets/Input_Validation_Cheat_Sheet.html) per OWASP, and access control is enforced [at each API endpoint](https://cheatsheetseries.owasp.org/cheatsheets/REST_Security_Cheat_Sheet.html) before business logic. Authorization runs before body parsing, so unauthenticated requests never trigger expensive I/O.
|
|
23
|
+
- **Secure by default** -- Security headers ship with conservative defaults (`Content-Security-Policy: default-src 'none'`, `X-Frame-Options: DENY`). All user-input objects use [null prototypes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create#object_with_null_prototype) to prevent prototype pollution. Input parsing is bounded out of the box (query length, pair count, body size, decompressed size). The pipeline's design maps directly to the [OWASP API Security Top 10](https://owasp.org/API-Security/) and [REST Security Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/REST_Security_Cheat_Sheet.html).
|
|
24
|
+
- **Zero-overhead composition** -- Synchronous middleware avoids microtask overhead entirely. Independent middleware within a stage run concurrently. The pipeline is a single function call with no framework runtime.
|
|
25
|
+
|
|
26
|
+
## The Fast Fail Design
|
|
27
|
+
|
|
28
|
+
Every API request passes through exactly four stages, in order:
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
Request
|
|
32
|
+
|
|
|
33
|
+
+- Stage 1: Negotiation Parse and inspect the request.
|
|
34
|
+
| logger() -> [ cors() | accepts() | cookie() | url() ]
|
|
35
|
+
|
|
|
36
|
+
+- Stage 2: Authorization Authenticate user, verify request integrity.
|
|
37
|
+
| [ authorization() | csrf() ]
|
|
38
|
+
|
|
|
39
|
+
+- Stage 3: Validation Parse and validate the request body.
|
|
40
|
+
| body() . validate()
|
|
41
|
+
|
|
|
42
|
+
+- Stage 4: Execution Run the handler and send the response.
|
|
43
|
+
timeout() . compress() . [your route logic] . send()
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Stages run serially. If any middleware throws, the pipeline stops immediately and the error handler runs. Independent middleware within a stage are parallelizable (shown with `|`).
|
|
47
|
+
|
|
48
|
+
## Installation
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
npm install @centralping/ergo
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Requires **Node.js >= 22**.
|
|
55
|
+
|
|
56
|
+
## Quick Start
|
|
57
|
+
|
|
58
|
+
```js
|
|
59
|
+
import {createServer} from 'node:http';
|
|
60
|
+
import {compose, handler, logger, cors, authorization, body, send} from '@centralping/ergo';
|
|
61
|
+
|
|
62
|
+
const pipeline = compose(
|
|
63
|
+
[logger(), [], 'log'],
|
|
64
|
+
[cors(), [], 'cors'],
|
|
65
|
+
[authorization({strategies: [{type: 'Bearer', authorizer: (_, token) =>
|
|
66
|
+
token === 'my-token' ? {authorized: true, info: {uid: 1}} : {}
|
|
67
|
+
}]}), [], 'auth'],
|
|
68
|
+
[body(), [], 'body'],
|
|
69
|
+
(req, res, {auth, body}) => ({body: {user: auth, data: body.parsed}}),
|
|
70
|
+
send()
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
createServer(handler(pipeline, send())).listen(3000);
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Middleware Overview
|
|
77
|
+
|
|
78
|
+
| Middleware | Description | Standard |
|
|
79
|
+
|---|---|---|
|
|
80
|
+
| `logger()` | Request ID + structured request/response logging | -- |
|
|
81
|
+
| `cors()` | CORS preflight and simple request handling | [Fetch Standard](https://fetch.spec.whatwg.org/#http-cors-protocol) |
|
|
82
|
+
| `accepts()` | Content negotiation (type, encoding, language) | [RFC 9110 §12.5](https://www.rfc-editor.org/rfc/rfc9110#section-12.5) |
|
|
83
|
+
| `cookie()` | Cookie parsing with Set-Cookie builder | [RFC 6265](https://www.rfc-editor.org/rfc/rfc6265) |
|
|
84
|
+
| `url()` | URL and query string parsing | -- |
|
|
85
|
+
| `authorization()` | Bearer / Basic auth with pluggable strategies | [RFC 6750](https://www.rfc-editor.org/rfc/rfc6750), [RFC 7617](https://www.rfc-editor.org/rfc/rfc7617) |
|
|
86
|
+
| `csrf()` | Double-submit cookie CSRF protection | [OWASP CSRF Prevention](https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html) |
|
|
87
|
+
| `body()` | JSON, multipart/form-data, URL-encoded parsing | [RFC 7578](https://www.rfc-editor.org/rfc/rfc7578) |
|
|
88
|
+
| `validate()` | JSON Schema validation via AJV | -- |
|
|
89
|
+
| `timeout()` | Request timeout with automatic 408/504 | -- |
|
|
90
|
+
| `compress()` | Content-Encoding negotiation (gzip, deflate, br) | [RFC 9110 §12.5.3](https://www.rfc-editor.org/rfc/rfc9110#section-12.5.3) |
|
|
91
|
+
| `send()` | Response serialization, ETag, conditional requests, Problem Details errors | [RFC 9110](https://www.rfc-editor.org/rfc/rfc9110), [RFC 9457](https://www.rfc-editor.org/rfc/rfc9457) |
|
|
92
|
+
| `prefer()` | Prefer header parsing and response | [RFC 7240](https://www.rfc-editor.org/rfc/rfc7240) |
|
|
93
|
+
| `precondition()` | 428 Precondition Required enforcement | [RFC 6585 §3](https://www.rfc-editor.org/rfc/rfc6585#section-3) |
|
|
94
|
+
| `rateLimit()` | Sliding-window rate limiting with 429 responses | [RFC 6585 §4](https://www.rfc-editor.org/rfc/rfc6585#section-4) |
|
|
95
|
+
| `securityHeaders()` | HSTS, CSP, X-Content-Type-Options, etc. | [RFC 6797](https://www.rfc-editor.org/rfc/rfc6797) |
|
|
96
|
+
| `cacheControl()` | Cache-Control header management | [RFC 9111](https://www.rfc-editor.org/rfc/rfc9111) |
|
|
97
|
+
| `jsonApiQuery()` | JSON:API query parameter validation | [JSON:API](https://jsonapi.org/) |
|
|
98
|
+
|
|
99
|
+
See the [full API reference](https://centralping.github.io/api/ergo/) for detailed options and examples.
|
|
100
|
+
|
|
101
|
+
## Standards Compliance
|
|
102
|
+
|
|
103
|
+
| RFC / Standard | Description | ergo Feature |
|
|
104
|
+
|---|---|---|
|
|
105
|
+
| [RFC 9110](https://www.rfc-editor.org/rfc/rfc9110) | HTTP Semantics | `send()` conditional requests, ETag, content negotiation |
|
|
106
|
+
| [RFC 9457](https://www.rfc-editor.org/rfc/rfc9457) | Problem Details for HTTP APIs | `send()` error responses, `httpErrors` utility |
|
|
107
|
+
| [RFC 9111](https://www.rfc-editor.org/rfc/rfc9111) | HTTP Caching | `cacheControl()` |
|
|
108
|
+
| [RFC 9205](https://www.rfc-editor.org/rfc/rfc9205) | Building Protocols with HTTP (BCP 56) | Overall API design philosophy |
|
|
109
|
+
| [RFC 7240](https://www.rfc-editor.org/rfc/rfc7240) | Prefer Header for HTTP | `prefer()` |
|
|
110
|
+
| [RFC 8288](https://www.rfc-editor.org/rfc/rfc8288) | Web Linking | `lib/link.js` pagination helpers |
|
|
111
|
+
| [RFC 6585](https://www.rfc-editor.org/rfc/rfc6585) | Additional HTTP Status Codes (428, 429) | `precondition()`, `rateLimit()` |
|
|
112
|
+
| [RFC 6265](https://www.rfc-editor.org/rfc/rfc6265) | HTTP State Management (Cookies) | `cookie()` |
|
|
113
|
+
| [RFC 6750](https://www.rfc-editor.org/rfc/rfc6750) | Bearer Token Usage | `authorization()` |
|
|
114
|
+
| [RFC 7617](https://www.rfc-editor.org/rfc/rfc7617) | Basic HTTP Authentication | `authorization()` |
|
|
115
|
+
| [RFC 7578](https://www.rfc-editor.org/rfc/rfc7578) | multipart/form-data | `body()` |
|
|
116
|
+
| [RFC 6797](https://www.rfc-editor.org/rfc/rfc6797) | HTTP Strict Transport Security | `securityHeaders()` |
|
|
117
|
+
| [OWASP API Security Top 10](https://owasp.org/API-Security/) | Aligned with top API security risks | Pipeline stage ordering, `rateLimit()`, `securityHeaders()`, input bounding |
|
|
118
|
+
| [OWASP REST Security](https://cheatsheetseries.owasp.org/cheatsheets/REST_Security_Cheat_Sheet.html) | Aligned with REST security best practices | Auth enforcement, input validation, security headers, error redaction |
|
|
119
|
+
|
|
120
|
+
## Documentation
|
|
121
|
+
|
|
122
|
+
- [Getting Started](https://centralping.github.io/getting-started/)
|
|
123
|
+
- [API Reference](https://centralping.github.io/api/ergo/)
|
|
124
|
+
- [Architecture & Design](https://centralping.github.io/architecture/)
|
|
125
|
+
- [Benchmarks](https://centralping.github.io/benchmarks/)
|
|
126
|
+
|
|
127
|
+
## Development
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
npm install
|
|
131
|
+
npm test # lint + format check + tests with coverage
|
|
132
|
+
npm run test:watch # watch mode
|
|
133
|
+
npm run lint # ESLint
|
|
134
|
+
npm run format # Prettier
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## License
|
|
138
|
+
|
|
139
|
+
[MIT](LICENSE) © 2019-present Jason Cust
|
package/http/accepts.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview HTTP middleware factory for content negotiation.
|
|
3
|
+
*
|
|
4
|
+
* Wraps the `negotiator` library to inspect `Accept`, `Accept-Language`,
|
|
5
|
+
* `Accept-Charset`, and `Accept-Encoding` request headers and determine the best
|
|
6
|
+
* matching content type, language, charset, and encoding for the response.
|
|
7
|
+
*
|
|
8
|
+
* When `throwIfFail` is true, returns `{response: {statusCode: 406, detail}}` for any
|
|
9
|
+
* header that cannot be satisfied, enforcing strict content negotiation in the
|
|
10
|
+
* Fast Fail pipeline.
|
|
11
|
+
*
|
|
12
|
+
* @module http/accepts
|
|
13
|
+
* @version 0.1.0
|
|
14
|
+
* @since 0.1.0
|
|
15
|
+
* @requires ../lib/accepts.js
|
|
16
|
+
* @requires ../utils/http-errors.js
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* import {compose, accepts} from 'ergo';
|
|
20
|
+
*
|
|
21
|
+
* const pipeline = compose(
|
|
22
|
+
* [accepts({types: ['application/json']}), 'accepts'],
|
|
23
|
+
* // acc.accepts => {type: 'application/json', language: 'en', charset: 'utf-8', encoding: 'identity'}
|
|
24
|
+
* );
|
|
25
|
+
*
|
|
26
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc9110#section-12.5 RFC 9110 Section 12.5 - Content Negotiation}
|
|
27
|
+
*/
|
|
28
|
+
import accepts from '../lib/accepts.js';
|
|
29
|
+
|
|
30
|
+
const headerMap = {
|
|
31
|
+
type: 'Accept',
|
|
32
|
+
language: 'Accept-Language',
|
|
33
|
+
charset: 'Accept-Charset',
|
|
34
|
+
encoding: 'Accept-Encoding'
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Creates a content negotiation middleware.
|
|
39
|
+
*
|
|
40
|
+
* @param {object} [options] - Negotiation configuration
|
|
41
|
+
* @param {boolean} [options.throwIfFail=false] - Return `{response: {statusCode: 406, detail}}` if any negotiation key is undefined
|
|
42
|
+
* @param {string[]} [options.types] - Acceptable media types
|
|
43
|
+
* @param {string[]} [options.languages] - Acceptable languages
|
|
44
|
+
* @param {string[]} [options.charsets] - Acceptable character sets
|
|
45
|
+
* @param {string[]} [options.encodings] - Acceptable content encodings
|
|
46
|
+
* @returns {function} - Middleware `(req) => {type, language, charset, encoding}` on success, or `{response: {statusCode: 406, detail: string}}` when `throwIfFail` is true and any negotiation value is undefined
|
|
47
|
+
*/
|
|
48
|
+
export default ({throwIfFail = false, ...options} = {}) => {
|
|
49
|
+
const acceptor = accepts(options);
|
|
50
|
+
|
|
51
|
+
return ({headers = {}} = {}) => {
|
|
52
|
+
const accepted = acceptor(headers);
|
|
53
|
+
|
|
54
|
+
if (throwIfFail) {
|
|
55
|
+
for (const [k, v] of Object.entries(accepted)) {
|
|
56
|
+
if (v === undefined) {
|
|
57
|
+
return {
|
|
58
|
+
response: {
|
|
59
|
+
statusCode: 406,
|
|
60
|
+
detail: `Failed to negotiate content for: [${headerMap[k]}].`
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return accepted;
|
|
68
|
+
};
|
|
69
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview HTTP middleware factory for request authorization header parsing.
|
|
3
|
+
*
|
|
4
|
+
* Parses the `Authorization` header using one or more configured strategies (e.g. Bearer,
|
|
5
|
+
* Basic, API key). Each strategy provides a `type`, optional `attributes` matcher, and an
|
|
6
|
+
* `authorizer` function that receives the parsed credentials and returns `{authorized, info}`.
|
|
7
|
+
*
|
|
8
|
+
* On authorization failure, returns `{response: {statusCode: 403}}` by default. Strategies may return a custom
|
|
9
|
+
* `statusCode` (e.g. `401`) and an `authenticate` challenge string for the `WWW-Authenticate`
|
|
10
|
+
* response header (RFC 7235).
|
|
11
|
+
*
|
|
12
|
+
* @module http/authorization
|
|
13
|
+
* @version 0.1.0
|
|
14
|
+
* @since 0.1.0
|
|
15
|
+
* @requires ../lib/authorization.js
|
|
16
|
+
* @requires ../utils/http-errors.js
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* import {compose, authorization} from 'ergo';
|
|
20
|
+
*
|
|
21
|
+
* const pipeline = compose(
|
|
22
|
+
* [authorization({
|
|
23
|
+
* strategies: [{
|
|
24
|
+
* type: 'Bearer',
|
|
25
|
+
* authorizer: async (attributes, token) => {
|
|
26
|
+
* const user = await verifyJwt(token);
|
|
27
|
+
* return user
|
|
28
|
+
* ? {authorized: true, info: {user}}
|
|
29
|
+
* : {authorized: false, info: {statusCode: 401}};
|
|
30
|
+
* }
|
|
31
|
+
* }]
|
|
32
|
+
* }), 'auth'],
|
|
33
|
+
* // acc.auth => {user: {...}} on success
|
|
34
|
+
* );
|
|
35
|
+
*
|
|
36
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc6750 RFC 6750 - Bearer Token Usage}
|
|
37
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc7617 RFC 7617 - The 'Basic' HTTP Authentication Scheme}
|
|
38
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc7235 RFC 7235 - HTTP Authentication}
|
|
39
|
+
*/
|
|
40
|
+
import authorize from '../lib/authorization.js';
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Creates an authorization middleware that parses the Authorization header
|
|
44
|
+
* and dispatches to the matching strategy handler.
|
|
45
|
+
*
|
|
46
|
+
* @param {object} [options] - Authorization configuration
|
|
47
|
+
* @param {Array<{type: string, attributes?: object, authorizer: function}>} [options.strategies=[]] - Authentication strategy definitions
|
|
48
|
+
* @returns {function} - Async middleware `(req) => info` on success; on failure `{response: {statusCode: 401|403, headers?}}` with optional `WWW-Authenticate` header tuples from `authenticate`
|
|
49
|
+
*/
|
|
50
|
+
export default ({strategies = []} = {}) => {
|
|
51
|
+
const authorizer = authorize(strategies);
|
|
52
|
+
|
|
53
|
+
return async ({headers: {authorization = ''} = {}} = {}) => {
|
|
54
|
+
const {authorized, info} = await authorizer(authorization);
|
|
55
|
+
|
|
56
|
+
if (authorized === false) {
|
|
57
|
+
const {statusCode = 403, authenticate} = info;
|
|
58
|
+
const headers = authenticate ? [['WWW-Authenticate', authenticate]] : undefined;
|
|
59
|
+
|
|
60
|
+
return {response: {statusCode, headers}};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return info;
|
|
64
|
+
};
|
|
65
|
+
};
|
package/http/body.js
ADDED
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview HTTP middleware factory for request body parsing.
|
|
3
|
+
*
|
|
4
|
+
* Reads and parses the request body according to the declared `Content-Type`. Supports:
|
|
5
|
+
* - `application/json` and `application/vnd.api+json` — parsed via `JSON.parse`
|
|
6
|
+
* - `application/x-www-form-urlencoded` — parsed via the query string parser
|
|
7
|
+
* - `multipart/form-data` — parsed via the RFC 7578 streaming multipart parser
|
|
8
|
+
*
|
|
9
|
+
* Handles both `Content-Length` and `Transfer-Encoding: chunked` requests. Enforces
|
|
10
|
+
* a configurable byte limit (default 1 MiB). Returns `{response: {statusCode, detail}}` for:
|
|
11
|
+
* - `411 Length Required` if neither Content-Length nor chunked encoding is present
|
|
12
|
+
* - `413 Payload Too Large` if the body exceeds the limit
|
|
13
|
+
* - `415 Unsupported Media Type` for unrecognized content types
|
|
14
|
+
* - `400 Bad Request` if Content-Length doesn't match received bytes or the body is malformed
|
|
15
|
+
*
|
|
16
|
+
* Contains a fast path for identity-encoded `application/json` that bypasses the
|
|
17
|
+
* 3-stream pipeline for reduced per-request overhead.
|
|
18
|
+
*
|
|
19
|
+
* @module http/body
|
|
20
|
+
* @version 0.1.0
|
|
21
|
+
* @since 0.1.0
|
|
22
|
+
* @requires node:stream
|
|
23
|
+
* @requires node:zlib
|
|
24
|
+
* @requires content-type
|
|
25
|
+
* @requires ../utils/streams/meter.js
|
|
26
|
+
* @requires ../lib/body/writer.js
|
|
27
|
+
* @requires ../lib/query.js
|
|
28
|
+
* @requires ../lib/body/multiparse.js
|
|
29
|
+
* @requires ../utils/http-errors.js
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* import {compose, body} from 'ergo';
|
|
33
|
+
*
|
|
34
|
+
* const pipeline = compose(
|
|
35
|
+
* [body({limit: 2 * 1024 * 1024}), 'body'],
|
|
36
|
+
* // acc.body => {type, charset, encoding, length, received, raw, parsed}
|
|
37
|
+
* // For JSON types, acc.body.parsed is the decoded object
|
|
38
|
+
* );
|
|
39
|
+
*
|
|
40
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc7578 RFC 7578 - Returning Values from Forms: multipart/form-data}
|
|
41
|
+
*/
|
|
42
|
+
import zlib from 'node:zlib';
|
|
43
|
+
import {PassThrough} from 'node:stream';
|
|
44
|
+
import {pipeline} from 'node:stream/promises';
|
|
45
|
+
import {parse} from 'content-type';
|
|
46
|
+
|
|
47
|
+
import meter from '../utils/streams/meter.js';
|
|
48
|
+
import writer from '../lib/body/writer.js';
|
|
49
|
+
import formParse from '../lib/query.js';
|
|
50
|
+
import multiParse from '../lib/body/multiparse.js';
|
|
51
|
+
import httpErrors from '../utils/http-errors.js';
|
|
52
|
+
|
|
53
|
+
const errors = {
|
|
54
|
+
TooLarge({limit, length} = {}) {
|
|
55
|
+
return httpErrors(413, {
|
|
56
|
+
message: `Body exceeded limit: [${limit}]; received: [${length}]`
|
|
57
|
+
});
|
|
58
|
+
},
|
|
59
|
+
NoLength({length} = {}) {
|
|
60
|
+
return httpErrors(411, {
|
|
61
|
+
message: `Content-Length is required: [${length}]`
|
|
62
|
+
});
|
|
63
|
+
},
|
|
64
|
+
InvalidLength({length, received} = {}) {
|
|
65
|
+
return httpErrors(400, {
|
|
66
|
+
message: `Invalid Content-Length: [${length}]; received [${received}]`
|
|
67
|
+
});
|
|
68
|
+
},
|
|
69
|
+
Unsupported({prop, value, err} = {}) {
|
|
70
|
+
return httpErrors(415, {
|
|
71
|
+
message: `Unsupported ${prop}: [${value}]`,
|
|
72
|
+
originalError: err
|
|
73
|
+
});
|
|
74
|
+
},
|
|
75
|
+
Malformed({type, err} = {}) {
|
|
76
|
+
return httpErrors(400, {
|
|
77
|
+
message: `Malformed ${type} body`,
|
|
78
|
+
originalError: err
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const parsers = new Proxy(
|
|
84
|
+
{
|
|
85
|
+
'application/x-www-form-urlencoded': formParse,
|
|
86
|
+
'multipart/form-data': multiParse
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
get(o, p) {
|
|
90
|
+
return Object.hasOwn(o, p) ? o[p] : JSON.parse;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
const decompressors = new Proxy(
|
|
96
|
+
{
|
|
97
|
+
gzip: zlib.createGunzip,
|
|
98
|
+
deflate: zlib.createInflate,
|
|
99
|
+
br: zlib.createBrotliDecompress,
|
|
100
|
+
identity: () => new PassThrough()
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
get(o, p) {
|
|
104
|
+
if (Object.hasOwn(o, p)) {
|
|
105
|
+
return o[p]();
|
|
106
|
+
} else {
|
|
107
|
+
throw errors.Unsupported({prop: 'Content-Encoding', value: p});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Creates a request body parsing middleware.
|
|
115
|
+
*
|
|
116
|
+
* @param {object} [options] - Body parser configuration
|
|
117
|
+
* @param {number} [options.limit=DEFAULT_LIMIT] - Maximum body size in bytes (default 1 MiB)
|
|
118
|
+
* @param {number} [options.decompressedLimit] - Maximum decompressed body size (default 10 * limit, capped at MAX_DECOMPRESSED)
|
|
119
|
+
* @param {string[]} [options.types] - Allowed Content-Type MIME types
|
|
120
|
+
* @param {string} [options.charset='utf-8'] - Default character encoding
|
|
121
|
+
* @returns {function} - Async middleware `(req) => {type, charset, encoding, length, received, boundary, raw, parsed}` on success; on error `{response: {statusCode: 400|411|413|415, detail: string}}`. Errors without `statusCode` are rethrown.
|
|
122
|
+
*/
|
|
123
|
+
const DEFAULT_LIMIT = 1 << 20; // 1 MiB
|
|
124
|
+
const MAX_DECOMPRESSED = 10 * DEFAULT_LIMIT; // 10 MiB hard cap
|
|
125
|
+
|
|
126
|
+
export default ({
|
|
127
|
+
limit = DEFAULT_LIMIT,
|
|
128
|
+
decompressedLimit = Math.min(10 * limit, MAX_DECOMPRESSED),
|
|
129
|
+
types = [
|
|
130
|
+
'application/vnd.api+json',
|
|
131
|
+
'application/json',
|
|
132
|
+
'application/x-www-form-urlencoded',
|
|
133
|
+
'multipart/form-data'
|
|
134
|
+
],
|
|
135
|
+
charset = 'utf-8'
|
|
136
|
+
} = {}) =>
|
|
137
|
+
async req => {
|
|
138
|
+
try {
|
|
139
|
+
let type;
|
|
140
|
+
let boundary;
|
|
141
|
+
let charsetEncoding = charset;
|
|
142
|
+
const ctHeader = req.headers['content-type'] ?? '';
|
|
143
|
+
|
|
144
|
+
// Fast path: plain "application/json" with no parameters
|
|
145
|
+
if (ctHeader === 'application/json') {
|
|
146
|
+
type = 'application/json';
|
|
147
|
+
} else {
|
|
148
|
+
try {
|
|
149
|
+
({
|
|
150
|
+
type,
|
|
151
|
+
parameters: {charset: charsetEncoding = charsetEncoding, boundary}
|
|
152
|
+
} = parse(ctHeader));
|
|
153
|
+
} catch (err) {
|
|
154
|
+
throw errors.Unsupported({prop: 'Content-Type', value: ctHeader, err});
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (!types.includes(type)) {
|
|
159
|
+
throw errors.Unsupported({prop: 'Content-Type', value: type});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const hasContentLength = 'content-length' in req.headers;
|
|
163
|
+
// Per RFC 9112 §6.3, Transfer-Encoding overrides Content-Length when both present.
|
|
164
|
+
// Node's HTTP parser decodes chunked framing before this middleware sees chunks.
|
|
165
|
+
// Keeping the Content-Length check provides an extra integrity validation:
|
|
166
|
+
// if the proxy forwarded both headers, a mismatch results in a 400 rather than
|
|
167
|
+
// silent acceptance of inconsistent framing.
|
|
168
|
+
const isChunked = /\bchunked\b/i.test(req.headers['transfer-encoding'] ?? '');
|
|
169
|
+
|
|
170
|
+
let length;
|
|
171
|
+
|
|
172
|
+
if (hasContentLength) {
|
|
173
|
+
length = Number(req.headers['content-length']);
|
|
174
|
+
if (Number.isNaN(length) || length < 0) {
|
|
175
|
+
throw errors.NoLength({length: req.headers['content-length']});
|
|
176
|
+
}
|
|
177
|
+
if (length > limit) {
|
|
178
|
+
throw errors.TooLarge({limit, length});
|
|
179
|
+
}
|
|
180
|
+
} else if (!isChunked) {
|
|
181
|
+
throw errors.NoLength({length: req.headers['content-length']});
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const encoding = req.headers['content-encoding'];
|
|
185
|
+
const isIdentity = !encoding || encoding === 'identity';
|
|
186
|
+
|
|
187
|
+
const {data: raw, received} = isIdentity
|
|
188
|
+
? await readBodyDirect(req, {limit, expected: length, encoding: charsetEncoding})
|
|
189
|
+
: await readReqStream(req, {
|
|
190
|
+
limit,
|
|
191
|
+
decompressedLimit,
|
|
192
|
+
expected: length,
|
|
193
|
+
type: encoding,
|
|
194
|
+
encoding: charsetEncoding
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
const result = {type, charset: charsetEncoding, encoding, length, received, boundary, raw};
|
|
198
|
+
|
|
199
|
+
// Fast path: for JSON types, parse immediately instead of a lazy getter
|
|
200
|
+
if (
|
|
201
|
+
isIdentity &&
|
|
202
|
+
type !== 'multipart/form-data' &&
|
|
203
|
+
type !== 'application/x-www-form-urlencoded'
|
|
204
|
+
) {
|
|
205
|
+
try {
|
|
206
|
+
result.parsed = JSON.parse(raw);
|
|
207
|
+
} catch (err) {
|
|
208
|
+
throw errors.Malformed({type, err});
|
|
209
|
+
}
|
|
210
|
+
return result;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
Object.defineProperty(result, 'parsed', {
|
|
214
|
+
get() {
|
|
215
|
+
try {
|
|
216
|
+
return (this.parsed = parsers[type](this.raw, this.boundary));
|
|
217
|
+
} catch (err) {
|
|
218
|
+
throw errors.Malformed({type, err});
|
|
219
|
+
}
|
|
220
|
+
},
|
|
221
|
+
set(v) {
|
|
222
|
+
delete this.parsed;
|
|
223
|
+
this.parsed = v;
|
|
224
|
+
},
|
|
225
|
+
configurable: true
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
return result;
|
|
229
|
+
} catch (err) {
|
|
230
|
+
if (err?.statusCode) {
|
|
231
|
+
return {response: {statusCode: err.statusCode, detail: err.message}};
|
|
232
|
+
}
|
|
233
|
+
throw err;
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Fast path: buffer the request body directly without creating intermediate
|
|
239
|
+
* stream objects. Used when Content-Encoding is identity (no compression).
|
|
240
|
+
*
|
|
241
|
+
* Replicates the same size guards as the stream pipeline (meter + writer).
|
|
242
|
+
*
|
|
243
|
+
* @param {import('node:stream').Readable} stream - Incoming request body stream
|
|
244
|
+
* @param {object} options - Size-guard and charset options
|
|
245
|
+
* @returns {Promise<{data: string, received: number}>} - Resolved body string and byte count
|
|
246
|
+
*/
|
|
247
|
+
async function readBodyDirect(stream, {limit = Infinity, expected, encoding} = {}) {
|
|
248
|
+
const chunks = [];
|
|
249
|
+
let bytesRead = 0;
|
|
250
|
+
|
|
251
|
+
for await (const chunk of stream) {
|
|
252
|
+
bytesRead += chunk.length;
|
|
253
|
+
|
|
254
|
+
if (bytesRead > limit) {
|
|
255
|
+
stream.destroy();
|
|
256
|
+
throw errors.TooLarge({limit, length: bytesRead});
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (expected !== undefined && bytesRead > expected) {
|
|
260
|
+
stream.destroy();
|
|
261
|
+
throw errors.InvalidLength({length: expected, received: bytesRead});
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
chunks.push(chunk);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (expected !== undefined && bytesRead !== expected) {
|
|
268
|
+
throw errors.InvalidLength({length: expected, received: bytesRead});
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
try {
|
|
272
|
+
return {data: Buffer.concat(chunks, bytesRead).toString(encoding), received: bytesRead};
|
|
273
|
+
} catch (err) {
|
|
274
|
+
throw errors.Unsupported({prop: 'charset', value: encoding, err});
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Reads and optionally decompresses a request body through a stream pipeline
|
|
280
|
+
* with byte metering and size enforcement. A wire-side meter enforces `limit`
|
|
281
|
+
* on the compressed bytes; a second meter after decompression enforces
|
|
282
|
+
* `decompressedLimit` to protect against decompression bombs.
|
|
283
|
+
*
|
|
284
|
+
* @param {import('node:stream').Readable} stream - Incoming request body stream
|
|
285
|
+
* @param {object} options - Size-guard and decompression options
|
|
286
|
+
* @returns {Promise<{data: string, received: number}>} - Resolved body string and byte count
|
|
287
|
+
*/
|
|
288
|
+
async function readReqStream(
|
|
289
|
+
stream,
|
|
290
|
+
{limit = Infinity, decompressedLimit = Infinity, expected, encoding, type = 'identity'} = {}
|
|
291
|
+
) {
|
|
292
|
+
const wireMeter = meter({limit, expected});
|
|
293
|
+
const inflatedMeter = meter({limit: decompressedLimit});
|
|
294
|
+
const w = writer();
|
|
295
|
+
const decompressor = decompressors[type];
|
|
296
|
+
|
|
297
|
+
try {
|
|
298
|
+
await pipeline(stream, wireMeter, decompressor, inflatedMeter, w);
|
|
299
|
+
} catch (err) {
|
|
300
|
+
if (err?.type in errors) {
|
|
301
|
+
throw errors[err.type](err);
|
|
302
|
+
}
|
|
303
|
+
throw err;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
try {
|
|
307
|
+
return {data: w.data.toString(encoding), received: wireMeter.bytesRead};
|
|
308
|
+
} catch (err) {
|
|
309
|
+
throw errors.Unsupported({prop: 'charset', value: encoding, err});
|
|
310
|
+
}
|
|
311
|
+
}
|