@owlmeans/route 0.1.21 → 0.1.22-rc.0

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 CHANGED
@@ -16,7 +16,7 @@ context that asks, so the same declaration serves a server and a browser alike.
16
16
  ## Installation
17
17
 
18
18
  ```bash
19
- bun add @owlmeans/route@^0.1.21
19
+ bun add @owlmeans/route@^0.1.22-rc.0
20
20
  ```
21
21
 
22
22
  ## Concepts
@@ -140,6 +140,30 @@ export const storyProtocols = {
140
140
  } as const
141
141
  ```
142
142
 
143
+ ### A streamed upload body
144
+
145
+ The built-in HTTP protocol owns one `protocolOptions` kind: `streamBody({ maxBytes, types? })`, a
146
+ route whose request body is raw bytes. The server hands the handler the request stream unbuffered
147
+ (`streamedBody(request)` of `@owlmeans/server-api`: 413 past `maxBytes`, 415 outside `types`); the
148
+ client sends a `Blob`, bytes or a Node stream as is.
149
+
150
+ ```ts
151
+ import { contract, protocol, typed, type BinaryBody } from '@owlmeans/entrypoint'
152
+ import { backend, route, RouteMethod, streamBody } from '@owlmeans/route'
153
+
154
+ export const materialProtocols = {
155
+ upload: protocol(
156
+ route('my-app:api:material:upload', '/material/:id/file', backend({
157
+ method: RouteMethod.PUT,
158
+ protocolOptions: streamBody({ maxBytes: 12 * 1024 * 1024 }), // types default to ['application/octet-stream']
159
+ })),
160
+ contract.request({ params: typed<{ id: string }>(IdSchema), body: typed<BinaryBody>() }, typed<Material>()),
161
+ ),
162
+ } as const
163
+
164
+ streamBodyOf(materialProtocols.upload.route.route) // { maxBytes: 12582912, types: ['application/octet-stream'] }
165
+ ```
166
+
143
167
  ### Reading where a route answers
144
168
 
145
169
  Once protocols are registered on a context, the entrypoint accessors answer address questions. The
@@ -183,6 +207,10 @@ addresses.resolveAddress(declaration)
183
207
  | `service(alias, opts?)` | function | Set `service` on `opts` (mutated and returned) |
184
208
  | `rtype(type, opts?)` | function | Set an arbitrary `AppType`; the primitive behind `backend()` / `frontend()` |
185
209
  | `normalizePath(path)` | function | Trim whitespace and one leading and trailing `/` |
210
+ | `streamBody({ maxBytes, types? })` | function | HTTP `protocolOptions` of a streamed-body route; validated eagerly, frozen |
211
+ | `streamBodyOf(route)` | function | The route's `StreamBody` (`{ maxBytes, types }`, default type filled in), or `null` for any other route |
212
+ | `STREAM_BODY`, `DEFAULT_STREAM_TYPE` | const | `'stream'`, `'application/octet-stream'` |
213
+ | `StreamBodyDeclaration`, `StreamBodyOptions`, `StreamBody` | interface | The declaration, the builder's input and the resolved form |
186
214
  | `RouteMethod` | enum | `GET = 'get'`, `POST = 'post'`, `PATCH = 'patch'`, `PUT = 'put'`, `DELETE = 'delete'` |
187
215
  | `RouteProtocols` | enum | Generic built-ins: `WEB = 'http'`, `SOCKET = 'ws'` |
188
216
  | `SEP` | const | Path separator `'/'` |
@@ -226,7 +254,8 @@ and `serviceRouteHelper` are pure and take no context.
226
254
  - `path` is only this route's segment. Do not repeat the parent's path in it; `resolvePath()` joins
227
255
  the chain.
228
256
  - Custom transport options are opaque here. Validate them through the package that owns the
229
- protocol.
257
+ protocol. The one HTTP kind, `streamBody()`, is validated by the builder and by `streamBodyOf()`.
258
+ - A streamed body is POST, PUT or PATCH; `@owlmeans/server-api` refuses to serve any other method.
230
259
  - Do not pass a context to the pure `./utils` helpers: `routeDeclarationHelper.overrideParams(ctx, decl)`
231
260
  writes route fields into the context object instead of resolving anything.
232
261
  - A mis-wired tree (no `cfg.services`, a service without a host, a parentship cycle) throws
@@ -253,7 +282,7 @@ This package ships embedded agent skills under `agent-meta/`. After installing y
253
282
  your project's skill store (`.agents/skills/`):
254
283
 
255
284
  ```sh
256
- npx @owlmeans/agent-skills@^0.1.21
285
+ npx @owlmeans/agent-skills@^0.1.22-rc.0
257
286
  ```
258
287
 
259
288
  The embedded files are version-matched to this package release. Do not edit them
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "schemaVersion": 3,
3
3
  "package": "@owlmeans/route",
4
- "version": "0.1.21",
5
- "generatedAt": "2026-11-05T11:16:21.552Z",
4
+ "version": "0.1.22-rc.0",
5
+ "generatedAt": "2026-11-11T20:57:15.717Z",
6
6
  "canonicalRepo": "https://github.com/owlmeans/common",
7
7
  "entries": [
8
8
  {
@@ -8,7 +8,7 @@ user-invocable: false
8
8
  # @owlmeans/route
9
9
 
10
10
  **Layer:** Core
11
- **Install:** `"@owlmeans/route": "^0.1.21"` in `dependencies`
11
+ **Install:** `"@owlmeans/route": "^0.1.22-rc.0"` in `dependencies`
12
12
 
13
13
  ## Key Exports
14
14
 
@@ -31,6 +31,9 @@ user-invocable: false
31
31
  | `RouteAddress` | `{ host, port, base, secure, protocol }` — where a route answers |
32
32
  | `CommonServiceRoute` / `ResolvedServiceRoute` | A service entry, and one that knows its host |
33
33
  | `normalizePath(path)`, `SEP`, `PARAM` | Path primitives |
34
+ | `streamBody({ maxBytes, types? })` | HTTP `protocolOptions` declaring a raw streamed request body (`StreamBodyDeclaration`) |
35
+ | `streamBodyOf(route)` | The route's `StreamBody` `{ maxBytes, types }` (default type filled in), or `null` |
36
+ | `STREAM_BODY`, `DEFAULT_STREAM_TYPE` | `'stream'` and `'application/octet-stream'` |
34
37
 
35
38
  ## Declaration and model
36
39
 
@@ -50,6 +53,25 @@ stores an opaque protocol identifier and `protocolOptions`. The package defining
50
53
  owns its marker, option type, validation and transport; the generic route package does not describe
51
54
  that transport's implementation semantics.
52
55
 
56
+ ## HTTP protocol options: a streamed body
57
+
58
+ The built-in HTTP protocol (`RouteProtocols.WEB`, also a route naming no protocol) owns one kind of
59
+ `protocolOptions`: `streamBody({ maxBytes, types? })`, a route whose request body travels as raw
60
+ bytes. `@owlmeans/server-api` hands the handler the request stream unbuffered (413 past
61
+ `maxBytes`, 415 outside `types`), `@owlmeans/api` sends a binary body as is under the first declared
62
+ type, and `@owlmeans/client-entrypoint` skips body validation for it. The builder validates eagerly
63
+ (`maxBytes` a positive integer, `types` a non-empty list, lower-cased; default
64
+ `['application/octet-stream']`) and freezes its result; transports read it with `streamBodyOf(route)`,
65
+ which answers `null` for any other route and throws `SyntaxError` on a malformed hand-written one. It
66
+ survives `protocol()`, `bind`/`bindAll` and the markers as plain declaration data:
67
+
68
+ ```typescript
69
+ route('material:upload', '/materials/:id/file', backend({
70
+ method: RouteMethod.PUT,
71
+ protocolOptions: streamBody({ maxBytes: 12 * 1024 * 1024, types: ['application/octet-stream'] }),
72
+ }))
73
+ ```
74
+
53
75
  ## Subpath Exports
54
76
 
55
77
  `./utils` holds two kinds of helper, and their signatures differ. The six that answer *where* a
package/build/consts.d.ts CHANGED
@@ -16,4 +16,8 @@ export declare enum RouteProtocols {
16
16
  WEB = "http",
17
17
  SOCKET = "ws"
18
18
  }
19
+ /** `protocolOptions.body` of an HTTP route whose request body is handed to the handler as a raw stream. */
20
+ export declare const STREAM_BODY = "stream";
21
+ /** The content type a streamed-body route accepts when its declaration names none. */
22
+ export declare const DEFAULT_STREAM_TYPE = "application/octet-stream";
19
23
  //# sourceMappingURL=consts.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"consts.d.ts","sourceRoot":"","sources":["../src/consts.ts"],"names":[],"mappings":"AACA,oBAAY,WAAW;IACrB,GAAG,QAAQ;IACX,IAAI,SAAS;IACb,KAAK,UAAU;IACf,GAAG,QAAQ;IACX,MAAM,WAAW;CAClB;AAED,eAAO,MAAM,GAAG,MAAM,CAAA;AAEtB,eAAO,MAAM,KAAK,MAAM,CAAA;AAExB;;;;GAIG;AACH,oBAAY,cAAc;IACxB,GAAG,SAAS;IACZ,MAAM,OAAO;CACd"}
1
+ {"version":3,"file":"consts.d.ts","sourceRoot":"","sources":["../src/consts.ts"],"names":[],"mappings":"AACA,oBAAY,WAAW;IACrB,GAAG,QAAQ;IACX,IAAI,SAAS;IACb,KAAK,UAAU;IACf,GAAG,QAAQ;IACX,MAAM,WAAW;CAClB;AAED,eAAO,MAAM,GAAG,MAAM,CAAA;AAEtB,eAAO,MAAM,KAAK,MAAM,CAAA;AAExB;;;;GAIG;AACH,oBAAY,cAAc;IACxB,GAAG,SAAS;IACZ,MAAM,OAAO;CACd;AAED,2GAA2G;AAC3G,eAAO,MAAM,WAAW,WAAW,CAAA;AAEnC,sFAAsF;AACtF,eAAO,MAAM,mBAAmB,6BAA6B,CAAA"}
package/build/consts.js CHANGED
@@ -18,4 +18,8 @@ export var RouteProtocols;
18
18
  RouteProtocols["WEB"] = "http";
19
19
  RouteProtocols["SOCKET"] = "ws";
20
20
  })(RouteProtocols || (RouteProtocols = {}));
21
+ /** `protocolOptions.body` of an HTTP route whose request body is handed to the handler as a raw stream. */
22
+ export const STREAM_BODY = 'stream';
23
+ /** The content type a streamed-body route accepts when its declaration names none. */
24
+ export const DEFAULT_STREAM_TYPE = 'application/octet-stream';
21
25
  //# sourceMappingURL=consts.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"consts.js","sourceRoot":"","sources":["../src/consts.ts"],"names":[],"mappings":"AACA,MAAM,CAAN,IAAY,WAMX;AAND,WAAY,WAAW;IACrB,0BAAW,CAAA;IACX,4BAAa,CAAA;IACb,8BAAe,CAAA;IACf,0BAAW,CAAA;IACX,gCAAiB,CAAA;AACnB,CAAC,EANW,WAAW,KAAX,WAAW,QAMtB;AAED,MAAM,CAAC,MAAM,GAAG,GAAG,GAAG,CAAA;AAEtB,MAAM,CAAC,MAAM,KAAK,GAAG,GAAG,CAAA;AAExB;;;;GAIG;AACH,MAAM,CAAN,IAAY,cAGX;AAHD,WAAY,cAAc;IACxB,8BAAY,CAAA;IACZ,+BAAa,CAAA;AACf,CAAC,EAHW,cAAc,KAAd,cAAc,QAGzB"}
1
+ {"version":3,"file":"consts.js","sourceRoot":"","sources":["../src/consts.ts"],"names":[],"mappings":"AACA,MAAM,CAAN,IAAY,WAMX;AAND,WAAY,WAAW;IACrB,0BAAW,CAAA;IACX,4BAAa,CAAA;IACb,8BAAe,CAAA;IACf,0BAAW,CAAA;IACX,gCAAiB,CAAA;AACnB,CAAC,EANW,WAAW,KAAX,WAAW,QAMtB;AAED,MAAM,CAAC,MAAM,GAAG,GAAG,GAAG,CAAA;AAEtB,MAAM,CAAC,MAAM,KAAK,GAAG,GAAG,CAAA;AAExB;;;;GAIG;AACH,MAAM,CAAN,IAAY,cAGX;AAHD,WAAY,cAAc;IACxB,8BAAY,CAAA;IACZ,+BAAa,CAAA;AACf,CAAC,EAHW,cAAc,KAAd,cAAc,QAGzB;AAED,2GAA2G;AAC3G,MAAM,CAAC,MAAM,WAAW,GAAG,QAAQ,CAAA;AAEnC,sFAAsF;AACtF,MAAM,CAAC,MAAM,mBAAmB,GAAG,0BAA0B,CAAA"}
package/build/index.d.ts CHANGED
@@ -2,4 +2,5 @@ export * from './types.js';
2
2
  export * from './consts.js';
3
3
  export * from './helper.js';
4
4
  export * from './model.js';
5
+ export * from './stream.js';
5
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA"}
package/build/index.js CHANGED
@@ -2,4 +2,5 @@ export * from './types.js';
2
2
  export * from './consts.js';
3
3
  export * from './helper.js';
4
4
  export * from './model.js';
5
+ export * from './stream.js';
5
6
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA"}
@@ -0,0 +1,17 @@
1
+ import type { RouteDeclaration, StreamBody, StreamBodyDeclaration, StreamBodyOptions } from './types.js';
2
+ /**
3
+ * Declare an HTTP route's body as a raw stream — put the result in the route's `protocolOptions`:
4
+ *
5
+ * route(alias, '/:id/file', backend({ method: RouteMethod.PUT, protocolOptions: streamBody({ maxBytes }) }))
6
+ *
7
+ * A declaration-builder: it validates eagerly, so a mis-declared route fails where it is declared.
8
+ */
9
+ export declare const streamBody: (options: StreamBodyOptions) => StreamBodyDeclaration;
10
+ /**
11
+ * The streamed body an HTTP route declares, or `null` for every other route — a JSON route, a
12
+ * socket or a package-owned transport. Read by the transports (`@owlmeans/server-api`,
13
+ * `@owlmeans/api`, `@owlmeans/client-entrypoint`); a declaration written without `streamBody()` is
14
+ * validated here and throws `SyntaxError` when malformed.
15
+ */
16
+ export declare const streamBodyOf: (route: Pick<RouteDeclaration, 'alias' | 'protocol' | 'protocolOptions'>) => StreamBody | null;
17
+ //# sourceMappingURL=stream.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,UAAU,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAsBxG;;;;;;GAMG;AACH,eAAO,MAAM,UAAU,YAAa,iBAAiB,KAAG,qBAItD,CAAA;AAEF;;;;;GAKG;AACH,eAAO,MAAM,YAAY,UAAW,IAAI,CAAC,gBAAgB,EAAE,OAAO,GAAG,UAAU,GAAG,iBAAiB,CAAC,KAAG,UAAU,GAAG,IAanH,CAAA"}
@@ -0,0 +1,49 @@
1
+ import { DEFAULT_STREAM_TYPE, RouteProtocols, STREAM_BODY } from './consts.js';
2
+ const assertMaxBytes = (maxBytes, where) => {
3
+ if (typeof maxBytes !== 'number' || !Number.isSafeInteger(maxBytes) || maxBytes <= 0) {
4
+ throw new SyntaxError(`${where}: a streamed body needs maxBytes as a positive integer, got ${String(maxBytes)}`);
5
+ }
6
+ return maxBytes;
7
+ };
8
+ const typesOf = (types, where) => {
9
+ if (types == null) {
10
+ return Object.freeze([DEFAULT_STREAM_TYPE]);
11
+ }
12
+ if (!Array.isArray(types) || types.length === 0
13
+ || types.some(type => typeof type !== 'string' || type.trim() === '')) {
14
+ throw new SyntaxError(`${where}: a streamed body's types must be a non-empty list of content types`);
15
+ }
16
+ return Object.freeze(types.map(type => type.trim().toLowerCase()));
17
+ };
18
+ /**
19
+ * Declare an HTTP route's body as a raw stream — put the result in the route's `protocolOptions`:
20
+ *
21
+ * route(alias, '/:id/file', backend({ method: RouteMethod.PUT, protocolOptions: streamBody({ maxBytes }) }))
22
+ *
23
+ * A declaration-builder: it validates eagerly, so a mis-declared route fails where it is declared.
24
+ */
25
+ export const streamBody = (options) => Object.freeze({
26
+ body: STREAM_BODY,
27
+ maxBytes: assertMaxBytes(options.maxBytes, 'streamBody'),
28
+ ...(options.types == null ? {} : { types: typesOf(options.types, 'streamBody') }),
29
+ });
30
+ /**
31
+ * The streamed body an HTTP route declares, or `null` for every other route — a JSON route, a
32
+ * socket or a package-owned transport. Read by the transports (`@owlmeans/server-api`,
33
+ * `@owlmeans/api`, `@owlmeans/client-entrypoint`); a declaration written without `streamBody()` is
34
+ * validated here and throws `SyntaxError` when malformed.
35
+ */
36
+ export const streamBodyOf = (route) => {
37
+ if (route.protocol != null && route.protocol !== RouteProtocols.WEB) {
38
+ return null;
39
+ }
40
+ const options = route.protocolOptions;
41
+ if (options == null || typeof options !== 'object' || options.body !== STREAM_BODY) {
42
+ return null;
43
+ }
44
+ return {
45
+ maxBytes: assertMaxBytes(options.maxBytes, route.alias),
46
+ types: typesOf(options.types, route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,GAAG,CAAC,OAA0B,EAAyB,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC;IAC7F,IAAI,EAAE,WAAW;IACjB,QAAQ,EAAE,cAAc,CAAC,OAAO,CAAC,QAAQ,EAAE,YAAY,CAAC;IACxD,GAAG,CAAC,OAAO,CAAC,KAAK,IAAI,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,OAAO,CAAC,KAAK,EAAE,YAAY,CAAC,EAAE,CAAC;CAClF,CAAC,CAAA;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,KAAuE,EAAqB,EAAE;IACzH,IAAI,KAAK,CAAC,QAAQ,IAAI,IAAI,IAAI,KAAK,CAAC,QAAQ,KAAK,cAAc,CAAC,GAAG,EAAE,CAAC;QACpE,OAAO,IAAI,CAAA;IACb,CAAC;IACD,MAAM,OAAO,GAAG,KAAK,CAAC,eAAoE,CAAA;IAC1F,IAAI,OAAO,IAAI,IAAI,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,CAAC,IAAI,KAAK,WAAW,EAAE,CAAC;QACnF,OAAO,IAAI,CAAA;IACb,CAAC;IAED,OAAO;QACL,QAAQ,EAAE,cAAc,CAAC,OAAO,CAAC,QAAQ,EAAE,KAAK,CAAC,KAAK,CAAC;QACvD,KAAK,EAAE,OAAO,CAAC,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC;KAC3C,CAAA;AACH,CAAC,CAAA"}
package/build/types.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { AppType, EntrypointReference } from '@owlmeans/context';
2
- import type { RouteProtocols, RouteMethod } from './consts';
2
+ import type { RouteProtocols, RouteMethod, STREAM_BODY } from './consts';
3
3
  /** A built-in or package-owned transport identifier. */
4
4
  export type RouteProtocol = RouteProtocols | (string & {});
5
5
  export interface BasicRoute {
@@ -24,7 +24,10 @@ export interface RouteDeclaration extends BasicRoute {
24
24
  method?: RouteMethod;
25
25
  protocol?: RouteProtocol;
26
26
  secure?: boolean;
27
- /** Transport-owned declaration data. The package defining `protocol` owns and validates it. */
27
+ /**
28
+ * Transport-owned declaration data. The package defining `protocol` owns and validates it; for
29
+ * the built-in HTTP protocol it is a {@link StreamBodyDeclaration} or nothing.
30
+ */
28
31
  protocolOptions?: unknown;
29
32
  /** How long a caller waits for an answer, in milliseconds, when the transport supports it. */
30
33
  timeout?: number;
@@ -57,4 +60,27 @@ export interface RouteOptions extends Omit<Partial<RouteDeclaration>, 'parent'>
57
60
  /** A parent protocol is preferred in application declarations; strings remain adapter input. */
58
61
  parent?: RouteParent;
59
62
  }
63
+ /**
64
+ * `protocolOptions` of an HTTP (`RouteProtocols.WEB`) route whose request body travels as raw bytes:
65
+ * the server hands the handler the request stream unbuffered, the client sends a binary body as is.
66
+ * Made with `streamBody()`.
67
+ */
68
+ export interface StreamBodyDeclaration {
69
+ readonly body: typeof STREAM_BODY;
70
+ /** The largest body accepted, in bytes; a larger one answers 413. */
71
+ readonly maxBytes: number;
72
+ /** The accepted content types; any other answers 415. Defaults to `application/octet-stream`. */
73
+ readonly types?: readonly string[];
74
+ }
75
+ /** What `streamBody()` is given. */
76
+ export interface StreamBodyOptions {
77
+ maxBytes: number;
78
+ types?: readonly string[];
79
+ }
80
+ /** A streamed body as the transports read it — `streamBodyOf(route)` — with the default type filled in. */
81
+ export interface StreamBody {
82
+ readonly maxBytes: number;
83
+ /** Never empty; the first one is what a client sends a binary body as when the call names none. */
84
+ readonly types: readonly string[];
85
+ }
60
86
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AACrE,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,UAAU,CAAA;AAE3D,wDAAwD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1
  {
2
2
  "name": "@owlmeans/route",
3
- "version": "0.1.21",
3
+ "version": "0.1.22-rc.0",
4
4
  "license": "MIT",
5
5
  "homepage": "https://owlmeans.com/docs/",
6
6
  "repository": {
package/src/consts.ts CHANGED
@@ -20,3 +20,9 @@ export enum RouteProtocols {
20
20
  WEB = 'http',
21
21
  SOCKET = 'ws'
22
22
  }
23
+
24
+ /** `protocolOptions.body` of an HTTP route whose request body is handed to the handler as a raw stream. */
25
+ export const STREAM_BODY = 'stream'
26
+
27
+ /** The content type a streamed-body route accepts when its declaration names none. */
28
+ export const DEFAULT_STREAM_TYPE = 'application/octet-stream'
package/src/index.ts CHANGED
@@ -3,3 +3,4 @@ export * from './types.js'
3
3
  export * from './consts.js'
4
4
  export * from './helper.js'
5
5
  export * from './model.js'
6
+ export * from './stream.js'
package/src/stream.ts ADDED
@@ -0,0 +1,56 @@
1
+ import { DEFAULT_STREAM_TYPE, RouteProtocols, STREAM_BODY } from './consts.js'
2
+ import type { RouteDeclaration, StreamBody, StreamBodyDeclaration, StreamBodyOptions } from './types.js'
3
+
4
+ const assertMaxBytes = (maxBytes: unknown, where: string): number => {
5
+ if (typeof maxBytes !== 'number' || !Number.isSafeInteger(maxBytes) || maxBytes <= 0) {
6
+ throw new SyntaxError(`${where}: a streamed body needs maxBytes as a positive integer, got ${String(maxBytes)}`)
7
+ }
8
+
9
+ return maxBytes
10
+ }
11
+
12
+ const typesOf = (types: unknown, where: string): readonly string[] => {
13
+ if (types == null) {
14
+ return Object.freeze([DEFAULT_STREAM_TYPE])
15
+ }
16
+ if (!Array.isArray(types) || types.length === 0
17
+ || types.some(type => typeof type !== 'string' || type.trim() === '')) {
18
+ throw new SyntaxError(`${where}: a streamed body's types must be a non-empty list of content types`)
19
+ }
20
+
21
+ return Object.freeze(types.map(type => (type as string).trim().toLowerCase()))
22
+ }
23
+
24
+ /**
25
+ * Declare an HTTP route's body as a raw stream — put the result in the route's `protocolOptions`:
26
+ *
27
+ * route(alias, '/:id/file', backend({ method: RouteMethod.PUT, protocolOptions: streamBody({ maxBytes }) }))
28
+ *
29
+ * A declaration-builder: it validates eagerly, so a mis-declared route fails where it is declared.
30
+ */
31
+ export const streamBody = (options: StreamBodyOptions): StreamBodyDeclaration => Object.freeze({
32
+ body: STREAM_BODY,
33
+ maxBytes: assertMaxBytes(options.maxBytes, 'streamBody'),
34
+ ...(options.types == null ? {} : { types: typesOf(options.types, 'streamBody') }),
35
+ })
36
+
37
+ /**
38
+ * The streamed body an HTTP route declares, or `null` for every other route — a JSON route, a
39
+ * socket or a package-owned transport. Read by the transports (`@owlmeans/server-api`,
40
+ * `@owlmeans/api`, `@owlmeans/client-entrypoint`); a declaration written without `streamBody()` is
41
+ * validated here and throws `SyntaxError` when malformed.
42
+ */
43
+ export const streamBodyOf = (route: Pick<RouteDeclaration, 'alias' | 'protocol' | 'protocolOptions'>): StreamBody | null => {
44
+ if (route.protocol != null && route.protocol !== RouteProtocols.WEB) {
45
+ return null
46
+ }
47
+ const options = route.protocolOptions as Partial<StreamBodyDeclaration> | null | undefined
48
+ if (options == null || typeof options !== 'object' || options.body !== STREAM_BODY) {
49
+ return null
50
+ }
51
+
52
+ return {
53
+ maxBytes: assertMaxBytes(options.maxBytes, route.alias),
54
+ types: typesOf(options.types, route.alias),
55
+ }
56
+ }
package/src/types.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { AppType, EntrypointReference } from '@owlmeans/context'
2
- import type { RouteProtocols, RouteMethod } from './consts'
2
+ import type { RouteProtocols, RouteMethod, STREAM_BODY } from './consts'
3
3
 
4
4
  /** A built-in or package-owned transport identifier. */
5
5
  export type RouteProtocol = RouteProtocols | (string & {})
@@ -27,7 +27,10 @@ export interface RouteDeclaration extends BasicRoute {
27
27
  method?: RouteMethod
28
28
  protocol?: RouteProtocol
29
29
  secure?: boolean
30
- /** Transport-owned declaration data. The package defining `protocol` owns and validates it. */
30
+ /**
31
+ * Transport-owned declaration data. The package defining `protocol` owns and validates it; for
32
+ * the built-in HTTP protocol it is a {@link StreamBodyDeclaration} or nothing.
33
+ */
31
34
  protocolOptions?: unknown
32
35
  /** How long a caller waits for an answer, in milliseconds, when the transport supports it. */
33
36
  timeout?: number
@@ -66,3 +69,29 @@ export interface RouteOptions extends Omit<Partial<RouteDeclaration>, 'parent'>
66
69
  /** A parent protocol is preferred in application declarations; strings remain adapter input. */
67
70
  parent?: RouteParent
68
71
  }
72
+
73
+ /**
74
+ * `protocolOptions` of an HTTP (`RouteProtocols.WEB`) route whose request body travels as raw bytes:
75
+ * the server hands the handler the request stream unbuffered, the client sends a binary body as is.
76
+ * Made with `streamBody()`.
77
+ */
78
+ export interface StreamBodyDeclaration {
79
+ readonly body: typeof STREAM_BODY
80
+ /** The largest body accepted, in bytes; a larger one answers 413. */
81
+ readonly maxBytes: number
82
+ /** The accepted content types; any other answers 415. Defaults to `application/octet-stream`. */
83
+ readonly types?: readonly string[]
84
+ }
85
+
86
+ /** What `streamBody()` is given. */
87
+ export interface StreamBodyOptions {
88
+ maxBytes: number
89
+ types?: readonly string[]
90
+ }
91
+
92
+ /** A streamed body as the transports read it — `streamBodyOf(route)` — with the default type filled in. */
93
+ export interface StreamBody {
94
+ readonly maxBytes: number
95
+ /** Never empty; the first one is what a client sends a binary body as when the call names none. */
96
+ readonly types: readonly string[]
97
+ }