oc-fastify-server-adapter 0.1.1 → 0.1.3
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/dist/index.d.ts +10 -4
- package/dist/index.js +32 -9
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type http from 'node:http';
|
|
2
|
+
import { type FastifyInstance } from 'fastify';
|
|
2
3
|
type CookieOptions = {
|
|
3
4
|
domain?: string;
|
|
4
5
|
encode?: (value: string) => string;
|
|
@@ -66,7 +67,7 @@ type OcResponse = {
|
|
|
66
67
|
};
|
|
67
68
|
type OcHandler = (req: OcRequest, res: OcResponse) => void | Promise<void>;
|
|
68
69
|
type ExpressMiddleware = (req: any, res: any, next: (err?: unknown) => void) => void;
|
|
69
|
-
export type HttpServerAdapter = {
|
|
70
|
+
export type HttpServerAdapter<TNative = unknown> = {
|
|
70
71
|
name: string;
|
|
71
72
|
enableBodyParser(opts: {
|
|
72
73
|
limit?: number | string;
|
|
@@ -92,7 +93,7 @@ export type HttpServerAdapter = {
|
|
|
92
93
|
onServerError(cb: (err: Error) => void): void;
|
|
93
94
|
close(cb: (err?: Error) => void): void;
|
|
94
95
|
isListening(): boolean;
|
|
95
|
-
native():
|
|
96
|
+
native(): TNative;
|
|
96
97
|
httpServer(): http.Server;
|
|
97
98
|
};
|
|
98
99
|
export interface FastifyServerAdapterOptions {
|
|
@@ -100,5 +101,10 @@ export interface FastifyServerAdapterOptions {
|
|
|
100
101
|
port?: number | string;
|
|
101
102
|
trustProxy?: boolean | string | string[] | number;
|
|
102
103
|
}
|
|
103
|
-
export
|
|
104
|
-
|
|
104
|
+
export type HttpServerAdapterFactory<TOptions = unknown, TNative = unknown> = {
|
|
105
|
+
(options?: unknown): HttpServerAdapter<TNative>;
|
|
106
|
+
readonly __serverAdapterOptions?: TOptions;
|
|
107
|
+
};
|
|
108
|
+
type FastifyServerAdapterFactory = HttpServerAdapterFactory<FastifyServerAdapterOptions | number | string, FastifyInstance>;
|
|
109
|
+
declare const createFastifyAdapter: FastifyServerAdapterFactory;
|
|
110
|
+
export default createFastifyAdapter;
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.default = createFastifyAdapter;
|
|
7
6
|
const node_fs_1 = require("node:fs");
|
|
8
7
|
const promises_1 = require("node:fs/promises");
|
|
9
8
|
const node_path_1 = __importDefault(require("node:path"));
|
|
@@ -21,9 +20,8 @@ const ocResponseSym = Symbol('ocResponse');
|
|
|
21
20
|
const timingStartSym = Symbol('timingStart');
|
|
22
21
|
const multipartParsedSym = Symbol('multipartParsed');
|
|
23
22
|
const defaultBodyLimit = 100 * 1024;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
23
|
+
const createFastifyAdapter = ((options) => new FastifyHttpServerAdapter(toFastifyAdapterOptions(options)));
|
|
24
|
+
exports.default = createFastifyAdapter;
|
|
27
25
|
class FastifyHttpServerAdapter {
|
|
28
26
|
name = 'fastify';
|
|
29
27
|
app;
|
|
@@ -479,11 +477,6 @@ function parseBodyLimit(limit) {
|
|
|
479
477
|
: 1;
|
|
480
478
|
return Math.floor(value * multiplier);
|
|
481
479
|
}
|
|
482
|
-
function toFastifyAdapterOptions(options) {
|
|
483
|
-
return typeof options === 'object' && options !== null
|
|
484
|
-
? options
|
|
485
|
-
: { port: options };
|
|
486
|
-
}
|
|
487
480
|
function normalisePort(port) {
|
|
488
481
|
if (typeof port === 'number') {
|
|
489
482
|
return port;
|
|
@@ -494,6 +487,36 @@ function normalisePort(port) {
|
|
|
494
487
|
}
|
|
495
488
|
return parsed;
|
|
496
489
|
}
|
|
490
|
+
function toFastifyAdapterOptions(options) {
|
|
491
|
+
if (typeof options === 'undefined' ||
|
|
492
|
+
typeof options === 'number' ||
|
|
493
|
+
typeof options === 'string') {
|
|
494
|
+
return { port: options };
|
|
495
|
+
}
|
|
496
|
+
if (isFastifyAdapterOptions(options)) {
|
|
497
|
+
return options;
|
|
498
|
+
}
|
|
499
|
+
throw new Error('Invalid Fastify server adapter options');
|
|
500
|
+
}
|
|
501
|
+
function isFastifyAdapterOptions(options) {
|
|
502
|
+
if (!options || typeof options !== 'object') {
|
|
503
|
+
return false;
|
|
504
|
+
}
|
|
505
|
+
const candidate = options;
|
|
506
|
+
const port = candidate.port;
|
|
507
|
+
const host = candidate.host;
|
|
508
|
+
const trustProxy = candidate.trustProxy;
|
|
509
|
+
return ((typeof port === 'undefined' ||
|
|
510
|
+
typeof port === 'number' ||
|
|
511
|
+
typeof port === 'string') &&
|
|
512
|
+
(typeof host === 'undefined' || typeof host === 'string') &&
|
|
513
|
+
(typeof trustProxy === 'undefined' ||
|
|
514
|
+
typeof trustProxy === 'boolean' ||
|
|
515
|
+
typeof trustProxy === 'string' ||
|
|
516
|
+
typeof trustProxy === 'number' ||
|
|
517
|
+
(Array.isArray(trustProxy) &&
|
|
518
|
+
trustProxy.every((item) => typeof item === 'string'))));
|
|
519
|
+
}
|
|
497
520
|
function toFastifyRoutePath(routePath) {
|
|
498
521
|
return routePath.replace(/\*[A-Za-z0-9_]+/g, '*');
|
|
499
522
|
}
|