@ts-pf/server-http 0.1.0-beta.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 +20 -0
- package/dist/cors-plugin.d.ts +29 -0
- package/dist/cors-plugin.d.ts.map +1 -0
- package/dist/cors-plugin.js +87 -0
- package/dist/cors-plugin.js.map +1 -0
- package/dist/handler.d.ts +29 -0
- package/dist/handler.d.ts.map +1 -0
- package/dist/handler.js +134 -0
- package/dist/handler.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/plugins.d.ts +21 -0
- package/dist/plugins.d.ts.map +1 -0
- package/dist/plugins.js +2 -0
- package/dist/plugins.js.map +1 -0
- package/dist/request-headers-plugin.d.ts +12 -0
- package/dist/request-headers-plugin.d.ts.map +1 -0
- package/dist/request-headers-plugin.js +10 -0
- package/dist/request-headers-plugin.js.map +1 -0
- package/dist/request-limit-plugin.d.ts +13 -0
- package/dist/request-limit-plugin.d.ts.map +1 -0
- package/dist/request-limit-plugin.js +40 -0
- package/dist/request-limit-plugin.js.map +1 -0
- package/dist/response-headers-plugin.d.ts +17 -0
- package/dist/response-headers-plugin.d.ts.map +1 -0
- package/dist/response-headers-plugin.js +31 -0
- package/dist/response-headers-plugin.js.map +1 -0
- package/package.json +51 -0
- package/skills/ts-pf-server-http/SKILL.md +50 -0
package/README.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# @ts-pf/server-http
|
|
2
|
+
|
|
3
|
+
`FetchHandler` and HTTP `HandlerPlugin`s (`CORSPlugin`, `RequestLimitPlugin`, header plugins). Calls `lookupProcedure` + `runProcedure` from [`@ts-pf/server`](../server). Analog of [`@ts-pf/message-server`](../message-server).
|
|
4
|
+
|
|
5
|
+
Agent skill: [`skills/ts-pf-server-http/`](skills/ts-pf-server-http/). Sync with `npx skills experimental_sync -y`.
|
|
6
|
+
|
|
7
|
+
`plugins` is HTTP-only. RPC call interceptors (`CallInterceptor` from `@ts-pf/server`) go on `{ interceptors }` — use `applyPlugins` to install `CallPlugin`s such as `DedupePlugin`.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { applyPlugins, DedupePlugin } from '@ts-pf/server'
|
|
11
|
+
import { CORSPlugin, FetchHandler } from '@ts-pf/server-http'
|
|
12
|
+
|
|
13
|
+
const handler = new FetchHandler(app, {
|
|
14
|
+
plugins: [new CORSPlugin()],
|
|
15
|
+
interceptors: applyPlugins([new DedupePlugin()]),
|
|
16
|
+
})
|
|
17
|
+
const result = await handler.handle(request, { prefix: '/rpc', context: { db } })
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Do not import this package from `@ts-pf/client-http`.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { HandlerPlugin } from './plugins.js';
|
|
2
|
+
export type CORSOrigin = string | readonly string[] | ((origin: string | null) => string | readonly string[] | null | Promise<string | readonly string[] | null>);
|
|
3
|
+
export type CORSPluginOptions = {
|
|
4
|
+
origin?: CORSOrigin;
|
|
5
|
+
allowMethods?: readonly string[];
|
|
6
|
+
allowHeaders?: readonly string[];
|
|
7
|
+
exposeHeaders?: readonly string[];
|
|
8
|
+
maxAge?: number;
|
|
9
|
+
credentials?: boolean;
|
|
10
|
+
};
|
|
11
|
+
export declare class CORSPlugin implements HandlerPlugin {
|
|
12
|
+
readonly name = "cors";
|
|
13
|
+
private readonly origin;
|
|
14
|
+
private readonly allowMethods;
|
|
15
|
+
private readonly allowHeaders;
|
|
16
|
+
private readonly exposeHeaders;
|
|
17
|
+
private readonly maxAge;
|
|
18
|
+
private readonly credentials;
|
|
19
|
+
constructor(options?: CORSPluginOptions);
|
|
20
|
+
onRequest({ request, }: {
|
|
21
|
+
request: Request;
|
|
22
|
+
}): Promise<Response | undefined>;
|
|
23
|
+
onResponse({ request, response, }: {
|
|
24
|
+
request: Request;
|
|
25
|
+
response: Response;
|
|
26
|
+
}): Promise<Response>;
|
|
27
|
+
private applyOrigin;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=cors-plugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cors-plugin.d.ts","sourceRoot":"","sources":["../src/cors-plugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAEjD,MAAM,MAAM,UAAU,GAClB,MAAM,GACN,SAAS,MAAM,EAAE,GACjB,CAAC,CACC,MAAM,EAAE,MAAM,GAAG,IAAI,KAEnB,MAAM,GACN,SAAS,MAAM,EAAE,GACjB,IAAI,GACJ,OAAO,CAAC,MAAM,GAAG,SAAS,MAAM,EAAE,GAAG,IAAI,CAAC,CAAC,CAAA;AAEnD,MAAM,MAAM,iBAAiB,GAAG;IAC9B,MAAM,CAAC,EAAE,UAAU,CAAA;IACnB,YAAY,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;IAChC,YAAY,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;IAChC,aAAa,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;IACjC,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,WAAW,CAAC,EAAE,OAAO,CAAA;CACtB,CAAA;AAED,qBAAa,UAAW,YAAW,aAAa;IAC9C,QAAQ,CAAC,IAAI,UAAS;IACtB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAY;IACnC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAmB;IAChD,OAAO,CAAC,QAAQ,CAAC,YAAY,CAA+B;IAC5D,OAAO,CAAC,QAAQ,CAAC,aAAa,CAA+B;IAC7D,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAoB;IAC3C,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;gBAEzB,OAAO,GAAE,iBAAsB;IAgBrC,SAAS,CAAC,EACd,OAAO,GACR,EAAE;QACD,OAAO,EAAE,OAAO,CAAA;KACjB,GAAG,OAAO,CAAC,QAAQ,GAAG,SAAS,CAAC;IAwB3B,UAAU,CAAC,EACf,OAAO,EACP,QAAQ,GACT,EAAE;QACD,OAAO,EAAE,OAAO,CAAA;QAChB,QAAQ,EAAE,QAAQ,CAAA;KACnB,GAAG,OAAO,CAAC,QAAQ,CAAC;YAmBP,WAAW;CAkB1B"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
export class CORSPlugin {
|
|
2
|
+
name = 'cors';
|
|
3
|
+
origin;
|
|
4
|
+
allowMethods;
|
|
5
|
+
allowHeaders;
|
|
6
|
+
exposeHeaders;
|
|
7
|
+
maxAge;
|
|
8
|
+
credentials;
|
|
9
|
+
constructor(options = {}) {
|
|
10
|
+
const origin = options.origin ?? '*';
|
|
11
|
+
const credentials = options.credentials ?? false;
|
|
12
|
+
if (credentials && origin === '*') {
|
|
13
|
+
throw new Error('CORSPlugin: credentials cannot be used with origin "*". Pass an origin list or a reflector.');
|
|
14
|
+
}
|
|
15
|
+
this.origin = origin;
|
|
16
|
+
this.allowMethods = options.allowMethods ?? ['POST'];
|
|
17
|
+
this.allowHeaders = options.allowHeaders;
|
|
18
|
+
this.exposeHeaders = options.exposeHeaders;
|
|
19
|
+
this.maxAge = options.maxAge;
|
|
20
|
+
this.credentials = credentials;
|
|
21
|
+
}
|
|
22
|
+
async onRequest({ request, }) {
|
|
23
|
+
if (request.method !== 'OPTIONS') {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
const headers = new Headers();
|
|
27
|
+
await this.applyOrigin(request, headers);
|
|
28
|
+
if (this.allowMethods.length > 0) {
|
|
29
|
+
headers.set('access-control-allow-methods', this.allowMethods.join(', '));
|
|
30
|
+
}
|
|
31
|
+
const allowHeaders = this.allowHeaders?.join(', ') ??
|
|
32
|
+
request.headers.get('access-control-request-headers');
|
|
33
|
+
if (allowHeaders) {
|
|
34
|
+
headers.set('access-control-allow-headers', allowHeaders);
|
|
35
|
+
}
|
|
36
|
+
if (this.maxAge !== undefined) {
|
|
37
|
+
headers.set('access-control-max-age', String(this.maxAge));
|
|
38
|
+
}
|
|
39
|
+
if (this.credentials) {
|
|
40
|
+
headers.set('access-control-allow-credentials', 'true');
|
|
41
|
+
}
|
|
42
|
+
return new Response(null, { status: 204, headers });
|
|
43
|
+
}
|
|
44
|
+
async onResponse({ request, response, }) {
|
|
45
|
+
const headers = new Headers(response.headers);
|
|
46
|
+
await this.applyOrigin(request, headers);
|
|
47
|
+
if (this.credentials) {
|
|
48
|
+
headers.set('access-control-allow-credentials', 'true');
|
|
49
|
+
}
|
|
50
|
+
if (this.exposeHeaders?.length) {
|
|
51
|
+
headers.set('access-control-expose-headers', this.exposeHeaders.join(', '));
|
|
52
|
+
}
|
|
53
|
+
return new Response(response.body, {
|
|
54
|
+
status: response.status,
|
|
55
|
+
statusText: response.statusText,
|
|
56
|
+
headers,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
async applyOrigin(request, headers) {
|
|
60
|
+
const requestOrigin = request.headers.get('origin');
|
|
61
|
+
const allowed = toOriginList(await valueOrigin(this.origin, requestOrigin));
|
|
62
|
+
if (allowed.includes('*')) {
|
|
63
|
+
headers.set('access-control-allow-origin', '*');
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (requestOrigin && allowed.includes(requestOrigin)) {
|
|
67
|
+
headers.set('access-control-allow-origin', requestOrigin);
|
|
68
|
+
}
|
|
69
|
+
const vary = headers.get('vary');
|
|
70
|
+
const hasOrigin = vary
|
|
71
|
+
?.split(',')
|
|
72
|
+
.some((part) => part.trim().toLowerCase() === 'origin');
|
|
73
|
+
if (!hasOrigin) {
|
|
74
|
+
headers.append('vary', 'Origin');
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function valueOrigin(origin, requestOrigin) {
|
|
79
|
+
return typeof origin === 'function' ? origin(requestOrigin) : origin;
|
|
80
|
+
}
|
|
81
|
+
function toOriginList(value) {
|
|
82
|
+
if (value == null) {
|
|
83
|
+
return [];
|
|
84
|
+
}
|
|
85
|
+
return typeof value === 'string' ? [value] : value;
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=cors-plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cors-plugin.js","sourceRoot":"","sources":["../src/cors-plugin.ts"],"names":[],"mappings":"AAsBA,MAAM,OAAO,UAAU;IACZ,IAAI,GAAG,MAAM,CAAA;IACL,MAAM,CAAY;IAClB,YAAY,CAAmB;IAC/B,YAAY,CAA+B;IAC3C,aAAa,CAA+B;IAC5C,MAAM,CAAoB;IAC1B,WAAW,CAAS;IAErC,YAAY,UAA6B,EAAE;QACzC,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,GAAG,CAAA;QACpC,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,IAAI,KAAK,CAAA;QAChD,IAAI,WAAW,IAAI,MAAM,KAAK,GAAG,EAAE,CAAC;YAClC,MAAM,IAAI,KAAK,CACb,6FAA6F,CAC9F,CAAA;QACH,CAAC;QACD,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC,YAAY,IAAI,CAAC,MAAM,CAAC,CAAA;QACpD,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC,YAAY,CAAA;QACxC,IAAI,CAAC,aAAa,GAAG,OAAO,CAAC,aAAa,CAAA;QAC1C,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAA;QAC5B,IAAI,CAAC,WAAW,GAAG,WAAW,CAAA;IAChC,CAAC;IAED,KAAK,CAAC,SAAS,CAAC,EACd,OAAO,GAGR;QACC,IAAI,OAAO,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;YACjC,OAAO,SAAS,CAAA;QAClB,CAAC;QACD,MAAM,OAAO,GAAG,IAAI,OAAO,EAAE,CAAA;QAC7B,MAAM,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,OAAO,CA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AAC,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,MAAM,CAAA;AACtE,CAAC;AAED,SAAS,YAAY,CACnB,KAAwC;IAExC,IAAI,KAAK,IAAI,IAAI,EAAE,CAAC;QAClB,OAAO,EAAE,CAAA;IACX,CAAC;IACD,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAA;AACpD,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type RpcCodec } from '@ts-pf/http';
|
|
2
|
+
import { type CallInterceptor, type ImplementedRouter } from '@ts-pf/server';
|
|
3
|
+
import type { HandlerPlugin } from './plugins.js';
|
|
4
|
+
export type HandleResult = {
|
|
5
|
+
matched: false;
|
|
6
|
+
response?: undefined;
|
|
7
|
+
} | {
|
|
8
|
+
matched: true;
|
|
9
|
+
response: Response;
|
|
10
|
+
};
|
|
11
|
+
export declare class FetchHandler<TCtx = unknown> {
|
|
12
|
+
private readonly router;
|
|
13
|
+
private readonly codec;
|
|
14
|
+
private readonly plugins;
|
|
15
|
+
private readonly interceptors;
|
|
16
|
+
constructor(router: ImplementedRouter, options?: {
|
|
17
|
+
codec?: RpcCodec;
|
|
18
|
+
plugins?: HandlerPlugin[];
|
|
19
|
+
interceptors?: readonly CallInterceptor[];
|
|
20
|
+
});
|
|
21
|
+
handle(request: Request, opts: {
|
|
22
|
+
prefix: string;
|
|
23
|
+
context: TCtx | ((req: Request) => TCtx | Promise<TCtx>);
|
|
24
|
+
}): Promise<HandleResult>;
|
|
25
|
+
private applyOnResponse;
|
|
26
|
+
private successResponse;
|
|
27
|
+
private errorResponse;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=handler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../src/handler.ts"],"names":[],"mappings":"AAAA,OAAO,EAML,KAAK,QAAQ,EAEd,MAAM,aAAa,CAAA;AAEpB,OAAO,EACL,KAAK,eAAe,EACpB,KAAK,iBAAiB,EAIvB,MAAM,eAAe,CAAA;AACtB,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAEjD,MAAM,MAAM,YAAY,GACpB;IAAE,OAAO,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,EAAE,SAAS,CAAA;CAAE,GACxC;IAAE,OAAO,EAAE,IAAI,CAAC;IAAC,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAA;AAEzC,qBAAa,YAAY,CAAC,IAAI,GAAG,OAAO;IAMpC,OAAO,CAAC,QAAQ,CAAC,MAAM;IALzB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAU;IAChC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAiB;IACzC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAA4B;gBAGtC,MAAM,EAAE,iBAAiB,EAC1C,OAAO,CAAC,EAAE;QACR,KAAK,CAAC,EAAE,QAAQ,CAAA;QAChB,OAAO,CAAC,EAAE,aAAa,EAAE,CAAA;QACzB,YAAY,CAAC,EAAE,SAAS,eAAe,EAAE,CAAA;KAC1C;IAOG,MAAM,CACV,OAAO,EAAE,OAAO,EAChB,IAAI,EAAE;QACJ,MAAM,EAAE,MAAM,CAAA;QACd,OAAO,EAAE,IAAI,GAAG,CAAC,CAAC,GAAG,EAAE,OAAO,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAA;KACzD,GACA,OAAO,CAAC,YAAY,CAAC;YA0GV,eAAe;YAmBf,eAAe;YAIf,aAAa;CAM5B"}
|
package/dist/handler.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { httpStatus, JSONCodec, PROTOCOL_HEADER, parseProcedurePath, } from '@ts-pf/http';
|
|
2
|
+
import { isPFError, PFError, PROTOCOL_VERSION } from '@ts-pf/protocol';
|
|
3
|
+
import { lookupProcedure, runProcedure, } from '@ts-pf/server';
|
|
4
|
+
export class FetchHandler {
|
|
5
|
+
router;
|
|
6
|
+
codec;
|
|
7
|
+
plugins;
|
|
8
|
+
interceptors;
|
|
9
|
+
constructor(router, options) {
|
|
10
|
+
this.router = router;
|
|
11
|
+
this.codec = options?.codec ?? new JSONCodec();
|
|
12
|
+
this.plugins = options?.plugins ?? [];
|
|
13
|
+
this.interceptors = options?.interceptors ?? [];
|
|
14
|
+
}
|
|
15
|
+
async handle(request, opts) {
|
|
16
|
+
const url = new URL(request.url);
|
|
17
|
+
const path = parseProcedurePath(url.pathname, opts.prefix);
|
|
18
|
+
if (path === null) {
|
|
19
|
+
return { matched: false };
|
|
20
|
+
}
|
|
21
|
+
let inbound = request;
|
|
22
|
+
let context;
|
|
23
|
+
try {
|
|
24
|
+
for (const plugin of this.plugins) {
|
|
25
|
+
const result = await plugin.onRequest?.({ request: inbound });
|
|
26
|
+
if (result instanceof Response) {
|
|
27
|
+
return {
|
|
28
|
+
matched: true,
|
|
29
|
+
response: await this.applyOnResponse(inbound, result, context),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
if (result instanceof Request) {
|
|
33
|
+
inbound = result;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (inbound.method !== 'POST') {
|
|
37
|
+
return {
|
|
38
|
+
matched: true,
|
|
39
|
+
response: await this.applyOnResponse(inbound, await this.errorResponse(new PFError({
|
|
40
|
+
code: 'METHOD_NOT_ALLOWED',
|
|
41
|
+
message: 'Method not allowed',
|
|
42
|
+
})), context),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
const decoded = await this.codec.decodeRequest(bodySource(inbound));
|
|
46
|
+
const procedure = lookupProcedure(this.router, path);
|
|
47
|
+
if (!procedure) {
|
|
48
|
+
throw new PFError({
|
|
49
|
+
code: 'NOT_FOUND',
|
|
50
|
+
message: 'Procedure not found',
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
context =
|
|
54
|
+
typeof opts.context === 'function'
|
|
55
|
+
? await opts.context(inbound)
|
|
56
|
+
: opts.context;
|
|
57
|
+
for (const plugin of this.plugins) {
|
|
58
|
+
const next = await plugin.onContext?.({
|
|
59
|
+
request: inbound,
|
|
60
|
+
context,
|
|
61
|
+
});
|
|
62
|
+
if (next !== undefined) {
|
|
63
|
+
context = next;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const runOpts = { signal: inbound.signal };
|
|
67
|
+
if (this.interceptors.length > 0) {
|
|
68
|
+
runOpts.interceptors = this.interceptors;
|
|
69
|
+
}
|
|
70
|
+
const output = await runProcedure(procedure, decoded.input, context, runOpts);
|
|
71
|
+
const response = await this.successResponse(output);
|
|
72
|
+
return {
|
|
73
|
+
matched: true,
|
|
74
|
+
response: await this.applyOnResponse(inbound, response, context),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
for (const plugin of this.plugins) {
|
|
79
|
+
await plugin.onError?.(context === undefined
|
|
80
|
+
? { request: inbound, error }
|
|
81
|
+
: { request: inbound, error, context });
|
|
82
|
+
}
|
|
83
|
+
const failure = isPFError(error)
|
|
84
|
+
? error
|
|
85
|
+
: new PFError({
|
|
86
|
+
code: 'INTERNAL',
|
|
87
|
+
message: 'Internal server error',
|
|
88
|
+
});
|
|
89
|
+
return {
|
|
90
|
+
matched: true,
|
|
91
|
+
response: await this.applyOnResponse(inbound, await this.errorResponse(failure), context),
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
async applyOnResponse(request, response, context) {
|
|
96
|
+
let current = response;
|
|
97
|
+
for (const plugin of this.plugins) {
|
|
98
|
+
const next = await plugin.onResponse?.(context === undefined
|
|
99
|
+
? { request, response: current }
|
|
100
|
+
: { request, response: current, context });
|
|
101
|
+
if (next) {
|
|
102
|
+
current = next;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return current;
|
|
106
|
+
}
|
|
107
|
+
async successResponse(output) {
|
|
108
|
+
return encodedResponse(await this.codec.encodeSuccess(output), 200);
|
|
109
|
+
}
|
|
110
|
+
async errorResponse(error) {
|
|
111
|
+
return encodedResponse(await this.codec.encodeFailure(error.toJSON()), httpStatus(error));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
function bodySource(request) {
|
|
115
|
+
return {
|
|
116
|
+
contentType: request.headers.get('content-type'),
|
|
117
|
+
text: () => request.text(),
|
|
118
|
+
formData: () => request.formData(),
|
|
119
|
+
body: () => request.body,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
function encodedResponse(encoded, status) {
|
|
123
|
+
const headers = new Headers();
|
|
124
|
+
headers.set(PROTOCOL_HEADER, PROTOCOL_VERSION);
|
|
125
|
+
if (!(encoded.body instanceof FormData)) {
|
|
126
|
+
headers.set('content-type', encoded.contentType);
|
|
127
|
+
}
|
|
128
|
+
if (encoded.body instanceof ReadableStream) {
|
|
129
|
+
headers.set('cache-control', 'no-cache, no-transform');
|
|
130
|
+
headers.set('x-accel-buffering', 'no');
|
|
131
|
+
}
|
|
132
|
+
return new Response(encoded.body, { status, headers });
|
|
133
|
+
}
|
|
134
|
+
//# sourceMappingURL=handler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handler.js","sourceRoot":"","sources":["../src/handler.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,SAAS,EACT,eAAe,EACf,kBAAkB,GAInB,MAAM,aAAa,CAAA;AACpB,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AACtE,OAAO,EAGL,eAAe,EAEf,YAAY,GACb,MAAM,eAAe,CAAA;AAOtB,MAAM,OAAO,YAAY;IAMJ;IALF,KAAK,CAAU;IACf,OAAO,CAAiB;IACxB,YAAY,CAA4B;IAEzD,YACmB,MAAyB,EAC1C,OAIC;QALgB,WAAM,GAAN,MAAM,CAAmB;QAO1C,IAAI,CAAC,KAAK,GAAG,OAAO,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AC,GAAG,CAAC,mBAAmB,EAAE,IAAI,CAAC,CAAA;IACxC,CAAC;IACD,OAAO,IAAI,QAAQ,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,CAAA;AACxD,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { type CORSOrigin, CORSPlugin, type CORSPluginOptions, } from './cors-plugin.js';
|
|
2
|
+
export { FetchHandler, type HandleResult } from './handler.js';
|
|
3
|
+
export type { HandlerPlugin } from './plugins.js';
|
|
4
|
+
export { RequestHeadersPlugin, type RequestHeadersPluginContext, } from './request-headers-plugin.js';
|
|
5
|
+
export { RequestLimitPlugin, type RequestLimitPluginOptions, } from './request-limit-plugin.js';
|
|
6
|
+
export { ResponseHeadersPlugin, type ResponseHeadersPluginContext, } from './response-headers-plugin.js';
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,UAAU,EACf,UAAU,EACV,KAAK,iBAAiB,GACvB,MAAM,kBAAkB,CAAA;AACzB,OAAO,EAAE,YAAY,EAAE,KAAK,YAAY,EAAE,MAAM,cAAc,CAAA;AAC9D,YAAY,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AACjD,OAAO,EACL,oBAAoB,EACpB,KAAK,2BAA2B,GACjC,MAAM,6BAA6B,CAAA;AACpC,OAAO,EACL,kBAAkB,EAClB,KAAK,yBAAyB,GAC/B,MAAM,2BAA2B,CAAA;AAClC,OAAO,EACL,qBAAqB,EACrB,KAAK,4BAA4B,GAClC,MAAM,8BAA8B,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { CORSPlugin, } from './cors-plugin.js';
|
|
2
|
+
export { FetchHandler } from './handler.js';
|
|
3
|
+
export { RequestHeadersPlugin, } from './request-headers-plugin.js';
|
|
4
|
+
export { RequestLimitPlugin, } from './request-limit-plugin.js';
|
|
5
|
+
export { ResponseHeadersPlugin, } from './response-headers-plugin.js';
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,UAAU,GAEX,MAAM,kBAAkB,CAAA;AACzB,OAAO,EAAE,YAAY,EAAqB,MAAM,cAAc,CAAA;AAE9D,OAAO,EACL,oBAAoB,GAErB,MAAM,6BAA6B,CAAA;AACpC,OAAO,EACL,kBAAkB,GAEnB,MAAM,2BAA2B,CAAA;AAClC,OAAO,EACL,qBAAqB,GAEtB,MAAM,8BAA8B,CAAA"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface HandlerPlugin {
|
|
2
|
+
readonly name: string;
|
|
3
|
+
onRequest?(ctx: {
|
|
4
|
+
request: Request;
|
|
5
|
+
}): Request | Response | undefined | Promise<Request | Response | undefined>;
|
|
6
|
+
onContext?(ctx: {
|
|
7
|
+
request: Request;
|
|
8
|
+
context: unknown;
|
|
9
|
+
}): unknown | Promise<unknown>;
|
|
10
|
+
onResponse?(ctx: {
|
|
11
|
+
request: Request;
|
|
12
|
+
response: Response;
|
|
13
|
+
context?: unknown;
|
|
14
|
+
}): Response | undefined | Promise<Response | undefined>;
|
|
15
|
+
onError?(ctx: {
|
|
16
|
+
request: Request;
|
|
17
|
+
error: unknown;
|
|
18
|
+
context?: unknown;
|
|
19
|
+
}): void | Promise<void>;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=plugins.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugins.d.ts","sourceRoot":"","sources":["../src/plugins.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,SAAS,CAAC,CAAC,GAAG,EAAE;QACd,OAAO,EAAE,OAAO,CAAA;KACjB,GAAG,OAAO,GAAG,QAAQ,GAAG,SAAS,GAAG,OAAO,CAAC,OAAO,GAAG,QAAQ,GAAG,SAAS,CAAC,CAAA;IAC5E,SAAS,CAAC,CAAC,GAAG,EAAE;QACd,OAAO,EAAE,OAAO,CAAA;QAChB,OAAO,EAAE,OAAO,CAAA;KACjB,GAAG,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;IAC9B,UAAU,CAAC,CAAC,GAAG,EAAE;QACf,OAAO,EAAE,OAAO,CAAA;QAChB,QAAQ,EAAE,QAAQ,CAAA;QAClB,OAAO,CAAC,EAAE,OAAO,CAAA;KAClB,GAAG,QAAQ,GAAG,SAAS,GAAG,OAAO,CAAC,QAAQ,GAAG,SAAS,CAAC,CAAA;IACxD,OAAO,CAAC,CAAC,GAAG,EAAE;QACZ,OAAO,EAAE,OAAO,CAAA;QAChB,KAAK,EAAE,OAAO,CAAA;QACd,OAAO,CAAC,EAAE,OAAO,CAAA;KAClB,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;CACzB"}
|
package/dist/plugins.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugins.js","sourceRoot":"","sources":["../src/plugins.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { HandlerPlugin } from './plugins.js';
|
|
2
|
+
export type RequestHeadersPluginContext = {
|
|
3
|
+
reqHeaders?: Headers;
|
|
4
|
+
};
|
|
5
|
+
export declare class RequestHeadersPlugin implements HandlerPlugin {
|
|
6
|
+
readonly name = "request-headers";
|
|
7
|
+
onContext({ request, context, }: {
|
|
8
|
+
request: Request;
|
|
9
|
+
context: unknown;
|
|
10
|
+
}): RequestHeadersPluginContext;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=request-headers-plugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-headers-plugin.d.ts","sourceRoot":"","sources":["../src/request-headers-plugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAEjD,MAAM,MAAM,2BAA2B,GAAG;IACxC,UAAU,CAAC,EAAE,OAAO,CAAA;CACrB,CAAA;AAED,qBAAa,oBAAqB,YAAW,aAAa;IACxD,QAAQ,CAAC,IAAI,qBAAoB;IAEjC,SAAS,CAAC,EACR,OAAO,EACP,OAAO,GACR,EAAE;QACD,OAAO,EAAE,OAAO,CAAA;QAChB,OAAO,EAAE,OAAO,CAAA;KACjB,GAAG,2BAA2B;CAMhC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-headers-plugin.js","sourceRoot":"","sources":["../src/request-headers-plugin.ts"],"names":[],"mappings":"AAMA,MAAM,OAAO,oBAAoB;IACtB,IAAI,GAAG,iBAAiB,CAAA;IAEjC,SAAS,CAAC,EACR,OAAO,EACP,OAAO,GAIR;QACC,OAAO;YACL,GAAI,OAAkB;YACtB,UAAU,EAAE,OAAO,CAAC,OAAO;SAC5B,CAAA;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { HandlerPlugin } from './plugins.js';
|
|
2
|
+
export type RequestLimitPluginOptions = {
|
|
3
|
+
maxBodySize: number;
|
|
4
|
+
};
|
|
5
|
+
export declare class RequestLimitPlugin implements HandlerPlugin {
|
|
6
|
+
readonly name = "request-limit";
|
|
7
|
+
private readonly maxBodySize;
|
|
8
|
+
constructor(options: RequestLimitPluginOptions);
|
|
9
|
+
onRequest({ request }: {
|
|
10
|
+
request: Request;
|
|
11
|
+
}): Request | undefined;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=request-limit-plugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-limit-plugin.d.ts","sourceRoot":"","sources":["../src/request-limit-plugin.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAEjD,MAAM,MAAM,yBAAyB,GAAG;IACtC,WAAW,EAAE,MAAM,CAAA;CACpB,CAAA;AAED,qBAAa,kBAAmB,YAAW,aAAa;IACtD,QAAQ,CAAC,IAAI,mBAAkB;IAC/B,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAQ;gBAExB,OAAO,EAAE,yBAAyB;IAI9C,SAAS,CAAC,EAAE,OAAO,EAAE,EAAE;QAAE,OAAO,EAAE,OAAO,CAAA;KAAE,GAAG,OAAO,GAAG,SAAS;CA2BlE"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { PFError } from '@ts-pf/protocol';
|
|
2
|
+
export class RequestLimitPlugin {
|
|
3
|
+
name = 'request-limit';
|
|
4
|
+
maxBodySize;
|
|
5
|
+
constructor(options) {
|
|
6
|
+
this.maxBodySize = options.maxBodySize;
|
|
7
|
+
}
|
|
8
|
+
onRequest({ request }) {
|
|
9
|
+
const contentLength = Number(request.headers.get('content-length'));
|
|
10
|
+
if (Number.isFinite(contentLength) && contentLength > this.maxBodySize) {
|
|
11
|
+
throw tooLarge();
|
|
12
|
+
}
|
|
13
|
+
if (!request.body) {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
const maxBodySize = this.maxBodySize;
|
|
17
|
+
let size = 0;
|
|
18
|
+
const limited = request.body.pipeThrough(new TransformStream({
|
|
19
|
+
transform(chunk, controller) {
|
|
20
|
+
size += chunk.byteLength;
|
|
21
|
+
if (size > maxBodySize) {
|
|
22
|
+
controller.error(tooLarge());
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
controller.enqueue(chunk);
|
|
26
|
+
},
|
|
27
|
+
}));
|
|
28
|
+
return new Request(request, {
|
|
29
|
+
body: limited,
|
|
30
|
+
duplex: 'half',
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function tooLarge() {
|
|
35
|
+
return new PFError({
|
|
36
|
+
code: 'PAYLOAD_TOO_LARGE',
|
|
37
|
+
message: 'Request body too large',
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=request-limit-plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-limit-plugin.js","sourceRoot":"","sources":["../src/request-limit-plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAA;AAOzC,MAAM,OAAO,kBAAkB;IACpB,IAAI,GAAG,eAAe,CAAA;IACd,WAAW,CAAQ;IAEpC,YAAY,OAAkC;QAC5C,IAAI,CAAC,WAAW,GAAG,OAAO,CAAC,WAAW,CAAA;IACxC,CAAC;IAED,SAAS,CAAC,EAAE,OAAO,EAAwB;QACzC,MAAM,aAAa,GAAG,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC,CAAA;QACnE,IAAI,MAAM,CAAC,QAAQ,CAAC,aAAa,CAAC,IAAI,aAAa,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC;YACvE,MAAM,QAAQ,EAAE,CAAA;QAClB,CAAC;QACD,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC;YAClB,OAAO,SAAS,CAAA;QAClB,CAAC;QACD,MAAM,WAAW,GAAG,IAAI,CAAC,WAAW,CAAA;QACpC,IAAI,IAAI,GAAG,CAAC,CAAA;QACZ,MAAM,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,WAAW,CACtC,IAAI,eAAe,CAAyB;YAC1C,SAAS,CAAC,KAAK,EAAE,UAAU;gBACzB,IAAI,IAAI,KAAK,CAAC,UAAU,CAAA;gBACxB,IAAI,IAAI,GAAG,WAAW,EAAE,CAAC;oBACvB,UAAU,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAA;oBAC5B,OAAM;gBACR,CAAC;gBACD,UAAU,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;YAC3B,CAAC;SACF,CAAC,CACH,CAAA;QACD,OAAO,IAAI,OAAO,CAAC,OAAO,EAAE;YAC1B,IAAI,EAAE,OAAO;YACb,MAAM,EAAE,MAAM;SACqB,CAAC,CAAA;IACxC,CAAC;CACF;AAED,SAAS,QAAQ;IACf,OAAO,IAAI,OAAO,CAAC;QACjB,IAAI,EAAE,mBAAmB;QACzB,OAAO,EAAE,wBAAwB;KAClC,CAAC,CAAA;AACJ,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { HandlerPlugin } from './plugins.js';
|
|
2
|
+
export type ResponseHeadersPluginContext = {
|
|
3
|
+
resHeaders?: Headers;
|
|
4
|
+
};
|
|
5
|
+
export declare class ResponseHeadersPlugin implements HandlerPlugin {
|
|
6
|
+
readonly name = "response-headers";
|
|
7
|
+
onContext({ context, }: {
|
|
8
|
+
request: Request;
|
|
9
|
+
context: unknown;
|
|
10
|
+
}): ResponseHeadersPluginContext;
|
|
11
|
+
onResponse({ response, context, }: {
|
|
12
|
+
request: Request;
|
|
13
|
+
response: Response;
|
|
14
|
+
context?: unknown;
|
|
15
|
+
}): Response | undefined;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=response-headers-plugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"response-headers-plugin.d.ts","sourceRoot":"","sources":["../src/response-headers-plugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAEjD,MAAM,MAAM,4BAA4B,GAAG;IACzC,UAAU,CAAC,EAAE,OAAO,CAAA;CACrB,CAAA;AAED,qBAAa,qBAAsB,YAAW,aAAa;IACzD,QAAQ,CAAC,IAAI,sBAAqB;IAElC,SAAS,CAAC,EACR,OAAO,GACR,EAAE;QACD,OAAO,EAAE,OAAO,CAAA;QAChB,OAAO,EAAE,OAAO,CAAA;KACjB,GAAG,4BAA4B;IAOhC,UAAU,CAAC,EACT,QAAQ,EACR,OAAO,GACR,EAAE;QACD,OAAO,EAAE,OAAO,CAAA;QAChB,QAAQ,EAAE,QAAQ,CAAA;QAClB,OAAO,CAAC,EAAE,OAAO,CAAA;KAClB,GAAG,QAAQ,GAAG,SAAS;CAoBzB"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export class ResponseHeadersPlugin {
|
|
2
|
+
name = 'response-headers';
|
|
3
|
+
onContext({ context, }) {
|
|
4
|
+
return {
|
|
5
|
+
...context,
|
|
6
|
+
resHeaders: new Headers(),
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
onResponse({ response, context, }) {
|
|
10
|
+
const resHeaders = context
|
|
11
|
+
?.resHeaders;
|
|
12
|
+
if (!resHeaders) {
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
15
|
+
const headers = new Headers(response.headers);
|
|
16
|
+
resHeaders.forEach((value, name) => {
|
|
17
|
+
if (name === 'set-cookie') {
|
|
18
|
+
headers.append(name, value);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
headers.set(name, value);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
return new Response(response.body, {
|
|
25
|
+
status: response.status,
|
|
26
|
+
statusText: response.statusText,
|
|
27
|
+
headers,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=response-headers-plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"response-headers-plugin.js","sourceRoot":"","sources":["../src/response-headers-plugin.ts"],"names":[],"mappings":"AAMA,MAAM,OAAO,qBAAqB;IACvB,IAAI,GAAG,kBAAkB,CAAA;IAElC,SAAS,CAAC,EACR,OAAO,GAIR;QACC,OAAO;YACL,GAAI,OAAkB;YACtB,UAAU,EAAE,IAAI,OAAO,EAAE;SAC1B,CAAA;IACH,CAAC;IAED,UAAU,CAAC,EACT,QAAQ,EACR,OAAO,GAKR;QACC,MAAM,UAAU,GAAI,OAAoD;YACtE,EAAE,UAAU,CAAA;QACd,IAAI,CAAC,UAAU,EAAE,CAAC;YAChB,OAAO,SAAS,CAAA;QAClB,CAAC;QACD,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAA;QAC7C,UAAU,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE;YACjC,IAAI,IAAI,KAAK,YAAY,EAAE,CAAC;gBAC1B,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,CAAA;YAC7B,CAAC;iBAAM,CAAC;gBACN,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,CAAA;YAC1B,CAAC;QACH,CAAC,CAAC,CAAA;QACF,OAAO,IAAI,QAAQ,CAAC,QAAQ,CAAC,IAAI,EAAE;YACjC,MAAM,EAAE,QAAQ,CAAC,MAAM;YACvB,UAAU,EAAE,QAAQ,CAAC,UAAU;YAC/B,OAAO;SACR,CAAC,CAAA;IACJ,CAAC;CACF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ts-pf/server-http",
|
|
3
|
+
"version": "0.1.0-beta.0",
|
|
4
|
+
"description": "Fetch Request/Response handler and HTTP HandlerPlugins",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/crobinson42/ts-pf.git",
|
|
9
|
+
"directory": "packages/server-http"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/crobinson42/ts-pf/tree/main/packages/server-http#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/crobinson42/ts-pf/issues"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./src/index.ts",
|
|
20
|
+
"import": "./src/index.ts"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"import": "./dist/index.js"
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"skills"
|
|
35
|
+
],
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "tsc -p tsconfig.build.json",
|
|
38
|
+
"test": "vitest run",
|
|
39
|
+
"type-check": "tsc --noEmit",
|
|
40
|
+
"lint": "biome check src tests"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@ts-pf/http": "0.1.0-beta.0",
|
|
44
|
+
"@ts-pf/protocol": "0.1.0-beta.0",
|
|
45
|
+
"@ts-pf/server": "0.1.0-beta.0"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@ts-pf/contract": "0.1.0-beta.0",
|
|
49
|
+
"zod": "^4.0.0"
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: ts-pf-server-http
|
|
3
|
+
description: Use when serving ts-pf over Fetch with FetchHandler, CORSPlugin, RequestLimitPlugin, or request/response header plugins. Triggers: @ts-pf/server-http, FetchHandler, HandlerPlugin, CORSPlugin.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# @ts-pf/server-http
|
|
7
|
+
|
|
8
|
+
`FetchHandler` and HTTP `HandlerPlugin`s. Calls `lookupProcedure` + `runProcedure`.
|
|
9
|
+
|
|
10
|
+
Install: `npm i @ts-pf/server-http@beta`
|
|
11
|
+
|
|
12
|
+
Link for agents: `npx skills experimental_sync -y`
|
|
13
|
+
|
|
14
|
+
## Do
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import { applyPlugins, DedupePlugin } from '@ts-pf/server'
|
|
18
|
+
import { CORSPlugin, FetchHandler, RequestLimitPlugin } from '@ts-pf/server-http'
|
|
19
|
+
|
|
20
|
+
const handler = new FetchHandler(app, {
|
|
21
|
+
plugins: [
|
|
22
|
+
new CORSPlugin({ origin: ['https://app.example.com'] }),
|
|
23
|
+
new RequestLimitPlugin({ maxBodySize: 1024 * 1024 }),
|
|
24
|
+
],
|
|
25
|
+
interceptors: applyPlugins([new DedupePlugin()]),
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
const result = await handler.handle(request, { prefix: '/rpc', context: { db } })
|
|
29
|
+
if (!result.matched) return new Response('Not Found', { status: 404 })
|
|
30
|
+
return result.response
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
`plugins` is HTTP-only (`HandlerPlugin`). RPC call interceptors go on `{ interceptors }` from `@ts-pf/server`. `CORSPlugin` answers `OPTIONS` preflight.
|
|
34
|
+
|
|
35
|
+
## API
|
|
36
|
+
|
|
37
|
+
- `FetchHandler`, `HandleResult`, `HandlerPlugin`
|
|
38
|
+
- `CORSPlugin`, `RequestLimitPlugin`, `RequestHeadersPlugin`, `ResponseHeadersPlugin`
|
|
39
|
+
|
|
40
|
+
## Pair with
|
|
41
|
+
|
|
42
|
+
- App: `ts-pf-server`
|
|
43
|
+
- Client: `ts-pf-client-http`
|
|
44
|
+
- Codecs: `ts-pf-file` / `ts-pf-stream` / `ts-pf-sse` via `{ codec }`
|
|
45
|
+
|
|
46
|
+
## Don't
|
|
47
|
+
|
|
48
|
+
- Serve REST, OpenAPI, or `catalog.json` from `FetchHandler`.
|
|
49
|
+
- Port `HandlerPlugin` onto WS / stdio / MessagePort.
|
|
50
|
+
- Put file size caps here — those are `MultipartCodec` options.
|