@orkestrel/server 0.0.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/LICENSE +21 -0
- package/README.md +34 -0
- package/dist/src/core/Negotiator.d.ts +32 -0
- package/dist/src/core/constants.d.ts +68 -0
- package/dist/src/core/errors.d.ts +73 -0
- package/dist/src/core/factories.d.ts +21 -0
- package/dist/src/core/helpers.d.ts +890 -0
- package/dist/src/core/index.d.ts +6 -0
- package/dist/src/core/index.js +1728 -0
- package/dist/src/core/index.js.map +1 -0
- package/dist/src/core/types.d.ts +362 -0
- package/dist/src/server/Server.d.ts +63 -0
- package/dist/src/server/factories.d.ts +26 -0
- package/dist/src/server/helpers.d.ts +45 -0
- package/dist/src/server/index.cjs +1330 -0
- package/dist/src/server/index.cjs.map +1 -0
- package/dist/src/server/index.d.ts +4 -0
- package/dist/src/server/types.d.ts +163 -0
- package/package.json +87 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Orkestrel
|
|
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,34 @@
|
|
|
1
|
+
# @orkestrel/server
|
|
2
|
+
|
|
3
|
+
A typed HTTP server for the `@orkestrel` line — composes the
|
|
4
|
+
`@orkestrel/router` dispatcher behind a managed lifecycle over a pluggable
|
|
5
|
+
node adapter seam. Built to sit beside `@orkestrel/contract` (validation),
|
|
6
|
+
`@orkestrel/emitter` (observable lifecycle), and `@orkestrel/abort`
|
|
7
|
+
(cancellation), reusing all three as it takes shape. Its middleware
|
|
8
|
+
architecture is still under design.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npm install @orkestrel/server
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Requirements
|
|
17
|
+
|
|
18
|
+
- Node.js >= 24
|
|
19
|
+
- ESM-only (no CommonJS build)
|
|
20
|
+
|
|
21
|
+
## Status
|
|
22
|
+
|
|
23
|
+
The public API is under design and not yet implemented — this package
|
|
24
|
+
currently ships no runtime code. An upcoming `PROPOSAL.md` will define the
|
|
25
|
+
server surface (lifecycle, middleware, node adapter) before implementation
|
|
26
|
+
begins.
|
|
27
|
+
|
|
28
|
+
## Package
|
|
29
|
+
|
|
30
|
+
Published as a single entry point per the `exports` field in `package.json`.
|
|
31
|
+
|
|
32
|
+
## License
|
|
33
|
+
|
|
34
|
+
MIT © [Orkestrel](https://github.com/orkestrel) — see [LICENSE](./LICENSE).
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Encoding, FormatHandlerMap, MiddlewareContext, NegotiatorInterface } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* The content-negotiation machine over the weighted `Accept` family — a
|
|
4
|
+
* reusable, cross-middleware ENTITY (not a middleware). Implements exactly
|
|
5
|
+
* {@link NegotiatorInterface}.
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* `negotiate` is the generic media-type primitive (exact / subtype-wildcard /
|
|
9
|
+
* any-range, q-sorted, `;q=0` rejected); `encoding` / `language` are its
|
|
10
|
+
* sibling axes and REUSE the same {@link parseAcceptHeader} q-value parser the
|
|
11
|
+
* rest of the substrate uses — one parser, never two. `format` reads the
|
|
12
|
+
* request `Accept`, negotiates a {@link FormatHandlerMap}'s keys, and invokes
|
|
13
|
+
* the winner — or answers `406 Not Acceptable`.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const negotiator = new Negotiator()
|
|
18
|
+
* negotiator.negotiate('text/html, application/json;q=0.9', ['application/json', 'text/html'])
|
|
19
|
+
* // 'text/html'
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export declare class Negotiator implements NegotiatorInterface {
|
|
23
|
+
negotiate(header: string, available: readonly string[]): string | undefined;
|
|
24
|
+
encoding(header: string, available: readonly Encoding[]): Encoding | undefined;
|
|
25
|
+
language(header: string, available: readonly string[]): string | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* @remarks
|
|
28
|
+
* Errors thrown by the negotiated handler propagate UNMODIFIED — the
|
|
29
|
+
* server boundary classifies them, never this negotiator.
|
|
30
|
+
*/
|
|
31
|
+
format<TState>(request: Request, context: MiddlewareContext<TState>, handlers: FormatHandlerMap<TState>): Promise<Response>;
|
|
32
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { Encoding } from './types.js';
|
|
2
|
+
/** Default graceful-stop deadline (ms) the server gives in-flight requests on `stop()`. */
|
|
3
|
+
export declare const DEFAULT_DRAIN_MS = 10000;
|
|
4
|
+
/** Default maximum request body size (bytes) `readBody` accepts before a 413. */
|
|
5
|
+
export declare const DEFAULT_BODY_LIMIT = 1048576;
|
|
6
|
+
/**
|
|
7
|
+
* Default maximum DECOMPRESSED request body size (bytes) — the zip-bomb cap
|
|
8
|
+
* the body pipeline's byte-counting `TransformStream` enforces when
|
|
9
|
+
* transparently decompressing a `Content-Encoding` request body.
|
|
10
|
+
*
|
|
11
|
+
* @remarks
|
|
12
|
+
* A small compressed payload (under {@link DEFAULT_BODY_LIMIT} on the wire)
|
|
13
|
+
* can inflate by many orders of magnitude — a classic decompression bomb.
|
|
14
|
+
* Capping the decompressed byte count makes the pipe ABORT the moment the
|
|
15
|
+
* output would exceed this, BEFORE the full bomb is materialized, so the
|
|
16
|
+
* inflation can never exhaust memory. Defaults to `16 MiB` — a generous
|
|
17
|
+
* ceiling for a legitimate compressed JSON / form body, far below an OOM. A
|
|
18
|
+
* consumer raises it via {@link import('./types.js').BodyOptions}
|
|
19
|
+
* `decompression` for a workload that genuinely ships larger decompressed
|
|
20
|
+
* bodies.
|
|
21
|
+
*/
|
|
22
|
+
export declare const DEFAULT_DECOMPRESSED_LIMIT = 16777216;
|
|
23
|
+
/**
|
|
24
|
+
* The SSE response headers the `openStream` seam always sets.
|
|
25
|
+
*
|
|
26
|
+
* @remarks
|
|
27
|
+
* `text/event-stream` is the media type browsers dispatch as SSE; `no-cache`
|
|
28
|
+
* keeps a proxy from caching the stream; `keep-alive` holds the connection
|
|
29
|
+
* open; `X-Accel-Buffering: no` opts a buffering reverse proxy (nginx) out so
|
|
30
|
+
* events flush promptly rather than batching. Frozen so a consumer can read
|
|
31
|
+
* but never mutate the shared default; `openStream` merges any
|
|
32
|
+
* {@link import('./types.js').StreamOptions.headers} UNDER these.
|
|
33
|
+
*/
|
|
34
|
+
export declare const SSE_HEADERS: Readonly<Record<string, string>>;
|
|
35
|
+
/**
|
|
36
|
+
* The strict charset `isValidRequestId` requires an incoming `X-Request-ID`
|
|
37
|
+
* to match — `^[A-Za-z0-9_-]{1,200}$` — so a CRLF / log-injection / oversized
|
|
38
|
+
* / control-char-bearing incoming id is REJECTED (a fresh id is minted
|
|
39
|
+
* instead) rather than ever riding into a response header or `context.state`
|
|
40
|
+
* (AGENTS §14 totality). Frozen so a consumer can read but never mutate the
|
|
41
|
+
* shared default.
|
|
42
|
+
*/
|
|
43
|
+
export declare const REQUEST_ID_PATTERN: Readonly<RegExp>;
|
|
44
|
+
/**
|
|
45
|
+
* The set of bare `Content-Type`s `isCompressibleType` treats as
|
|
46
|
+
* COMPRESSIBLE, beyond the `text/*` prefix + structured-suffix (`+json` /
|
|
47
|
+
* `+xml`) rules that helper also applies.
|
|
48
|
+
*
|
|
49
|
+
* @remarks
|
|
50
|
+
* The text-shaped application types worth compressing (JSON / JavaScript /
|
|
51
|
+
* XML / SVG / WASM / a few document formats) — NOT already-compressed
|
|
52
|
+
* binaries (`image/png`, `image/jpeg`, `video/*`, `application/zip`, a font's
|
|
53
|
+
* `woff2`), which gzip/deflate would only bloat. Frozen so a consumer reads
|
|
54
|
+
* but never mutates the shared default.
|
|
55
|
+
*/
|
|
56
|
+
export declare const COMPRESSIBLE_TYPES: ReadonlySet<string>;
|
|
57
|
+
/**
|
|
58
|
+
* The default {@link Encoding} content-codings the substrate offers, in
|
|
59
|
+
* PREFERENCE order — `gzip` / `deflate`.
|
|
60
|
+
*
|
|
61
|
+
* @remarks
|
|
62
|
+
* `identity` (no compression) is always implicitly acceptable and is NOT
|
|
63
|
+
* listed here — it is the fall-through when a client accepts none of these.
|
|
64
|
+
* Brotli is omitted per the core's `CompressionStream`-only decision (see
|
|
65
|
+
* {@link Encoding}). Frozen so a consumer can read but never mutate the
|
|
66
|
+
* shared default.
|
|
67
|
+
*/
|
|
68
|
+
export declare const DEFAULT_ENCODINGS: readonly Encoding[];
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An error a handler (or middleware) throws to produce an HTTP response of a
|
|
3
|
+
* specific status.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* Carries the HTTP `status` to send and an optional `context` record (the
|
|
7
|
+
* offending field / value). The server's built-in error boundary catches it
|
|
8
|
+
* and renders a response of `status` with the error's `message` as the body
|
|
9
|
+
* — so an `HTTPError`'s message is ALWAYS client-facing (it is the handler's
|
|
10
|
+
* deliberate signal), unlike a generic throw whose message is hidden unless
|
|
11
|
+
* `expose` is set. Subclass it (as {@link ContentTooLargeError} does) for
|
|
12
|
+
* specific statuses, or throw it directly. Narrow a caught value with
|
|
13
|
+
* {@link isHTTPError}.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { HTTPError } from '@src/core'
|
|
18
|
+
*
|
|
19
|
+
* const handler = async (request: Request): Promise<Response> => {
|
|
20
|
+
* const user = await find(new URL(request.url).searchParams.get('id'))
|
|
21
|
+
* if (user === undefined) throw new HTTPError(404, 'user not found')
|
|
22
|
+
* return Response.json(user)
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export declare class HTTPError extends Error {
|
|
27
|
+
readonly status: number;
|
|
28
|
+
readonly context?: Readonly<Record<string, unknown>>;
|
|
29
|
+
constructor(status: number, message: string, context?: Readonly<Record<string, unknown>>);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The {@link HTTPError} thrown when a request body exceeds the body
|
|
33
|
+
* pipeline's size limit — a `413 Content Too Large`.
|
|
34
|
+
*
|
|
35
|
+
* @remarks
|
|
36
|
+
* Thrown by `readBody` (and the middleware context's cached `body()`) when
|
|
37
|
+
* the received bytes exceed the configured {@link
|
|
38
|
+
* import('./types.js').BodyOptions.limit}, carrying the `limit` in its
|
|
39
|
+
* `context`. The server's error boundary renders it as a 413.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* import { ContentTooLargeError, isHTTPError } from '@src/core'
|
|
44
|
+
*
|
|
45
|
+
* try {
|
|
46
|
+
* await readBody(request, { limit: 1024 })
|
|
47
|
+
* } catch (error) {
|
|
48
|
+
* if (isHTTPError(error)) console.log(error.status) // 413
|
|
49
|
+
* }
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export declare class ContentTooLargeError extends HTTPError {
|
|
53
|
+
constructor(limit: number);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Narrow an unknown caught value to an {@link HTTPError} (including its
|
|
57
|
+
* subclasses, e.g. {@link ContentTooLargeError}).
|
|
58
|
+
*
|
|
59
|
+
* @param value - The value to test (typically a `catch` binding)
|
|
60
|
+
* @returns `true` when `value` is an {@link HTTPError}
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* import { isHTTPError } from '@src/core'
|
|
65
|
+
*
|
|
66
|
+
* try {
|
|
67
|
+
* await handle(request)
|
|
68
|
+
* } catch (error) {
|
|
69
|
+
* if (isHTTPError(error)) console.log(error.status, error.message)
|
|
70
|
+
* }
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
export declare function isHTTPError(value: unknown): value is HTTPError;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { NegotiatorInterface } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Create a {@link NegotiatorInterface} — the reusable content-negotiation
|
|
4
|
+
* machine over the weighted `Accept` family.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* Prefer this over `new Negotiator()` at call sites that only need the
|
|
8
|
+
* interface.
|
|
9
|
+
*
|
|
10
|
+
* @returns A {@link NegotiatorInterface}
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { createNegotiator } from '@src/core'
|
|
15
|
+
*
|
|
16
|
+
* const negotiator = createNegotiator()
|
|
17
|
+
* negotiator.negotiate('text/html, application/json;q=0.9', ['application/json', 'text/html'])
|
|
18
|
+
* // 'text/html'
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare function createNegotiator(): NegotiatorInterface;
|