@react-foundry/fastify 0.1.8 → 0.2.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.
@@ -1,20 +1,16 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.defaultsTrue = exports.defaultsFalse = exports.NodeEnv = exports.Mode = void 0;
4
- var Mode;
1
+ export var Mode;
5
2
  (function (Mode) {
6
3
  Mode["Server"] = "server";
7
4
  Mode["Serverless"] = "serverless";
8
5
  Mode["StaticGenerator"] = "static-generator";
9
- })(Mode || (exports.Mode = Mode = {}));
6
+ })(Mode || (Mode = {}));
10
7
  ;
11
- var NodeEnv;
8
+ export var NodeEnv;
12
9
  (function (NodeEnv) {
13
10
  NodeEnv["Development"] = "development";
14
11
  NodeEnv["Production"] = "production";
15
- })(NodeEnv || (exports.NodeEnv = NodeEnv = {}));
12
+ })(NodeEnv || (NodeEnv = {}));
16
13
  ;
17
14
  const parseBoolean = (pattern) => ((s) => !!(s && s.match(pattern)));
18
- exports.defaultsFalse = parseBoolean(/(yes|on|true|1)/i);
19
- const defaultsTrue = (s) => !parseBoolean(/(no|off|false|0)/i)(s);
20
- exports.defaultsTrue = defaultsTrue;
15
+ export const defaultsFalse = parseBoolean(/(yes|on|true|1)/i);
16
+ export const defaultsTrue = (s) => !parseBoolean(/(no|off|false|0)/i)(s);
package/dist/index.js CHANGED
@@ -1,52 +1,9 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
36
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
37
- };
38
- var __importDefault = (this && this.__importDefault) || function (mod) {
39
- return (mod && mod.__esModule) ? mod : { "default": mod };
40
- };
41
- Object.defineProperty(exports, "__esModule", { value: true });
42
- exports.SessionStore = exports.AuthMethod = exports.Fastify = void 0;
43
- const close_with_grace_1 = __importDefault(require("close-with-grace"));
44
- const fastify_1 = __importDefault(require("fastify"));
45
- const rate_limit_1 = __importDefault(require("@fastify/rate-limit"));
46
- const fastify_auth_1 = __importStar(require("@react-foundry/fastify-auth"));
47
- Object.defineProperty(exports, "AuthMethod", { enumerable: true, get: function () { return fastify_auth_1.AuthMethod; } });
48
- const fastify_harden_1 = __importDefault(require("@react-foundry/fastify-harden"));
49
- const config_helpers_1 = require("./config-helpers");
1
+ import closeWithGrace from 'close-with-grace';
2
+ import _Fastify from 'fastify';
3
+ import fastifyRateLimit from '@fastify/rate-limit';
4
+ import fastifyAuth, { AuthMethod } from '@react-foundry/fastify-auth';
5
+ import fastifyHarden from '@react-foundry/fastify-harden';
6
+ import { NodeEnv } from './config-helpers';
50
7
  const is = () => true;
51
8
  const probeHandler = (isFn) => async (_req, reply) => {
52
9
  if (await isFn()) {
@@ -63,9 +20,9 @@ const parseForwarded = (hdr) => {
63
20
  : hdr);
64
21
  return str?.split(',')[0]?.trim();
65
22
  };
66
- const Fastify = ({ auth: _auth, cookies = {
23
+ export const Fastify = ({ auth: _auth, cookies = {
67
24
  secret: 'changeme'
68
- }, contentSecurityPolicy, dev = process.env.NODE_ENV === config_helpers_1.NodeEnv.Development, isLive = is, isReady = is, logger: _logger = {}, onClose, permissionsPolicy, rateLimit: _rateLimit, session, ...options }) => {
25
+ }, contentSecurityPolicy, dev = process.env.NODE_ENV === NodeEnv.Development, isLive = is, isReady = is, logger: _logger = {}, onClose, permissionsPolicy, rateLimit: _rateLimit, session, ...options }) => {
69
26
  const isTTY = process.stdout.isTTY;
70
27
  const stdLogger = {
71
28
  level: 'info',
@@ -102,11 +59,11 @@ const Fastify = ({ auth: _auth, cookies = {
102
59
  ..._logger
103
60
  }
104
61
  : _logger);
105
- const httpd = (0, fastify_1.default)({
62
+ const httpd = _Fastify({
106
63
  logger,
107
64
  ...options
108
65
  });
109
- const auth = ((session?.store === undefined) && (_auth === undefined || _auth.method === fastify_auth_1.AuthMethod.None)
66
+ const auth = ((session?.store === undefined) && (_auth === undefined || _auth.method === AuthMethod.None)
110
67
  ? undefined
111
68
  : {
112
69
  rateLimit: _rateLimit,
@@ -123,16 +80,16 @@ const Fastify = ({ auth: _auth, cookies = {
123
80
  httpd.log.debug(`Privacy mode: ${!!auth?.privacy}`);
124
81
  httpd.log.debug(`Authentication method: ${auth?.method || 'none'}`);
125
82
  httpd.log.debug(`Session store: ${session?.store}`);
126
- httpd.register(fastify_harden_1.default, {
83
+ httpd.register(fastifyHarden, {
127
84
  contentSecurityPolicy,
128
85
  dev,
129
86
  permissionsPolicy
130
87
  });
131
88
  if (rateLimit) {
132
- httpd.register(rate_limit_1.default, rateLimit);
89
+ httpd.register(fastifyRateLimit, rateLimit);
133
90
  }
134
91
  if (auth) {
135
- httpd.register(fastify_auth_1.default, auth);
92
+ httpd.register(fastifyAuth, auth);
136
93
  }
137
94
  httpd.get('/healthz', probeHandler(isLive));
138
95
  httpd.get('/readiness', probeHandler(isReady));
@@ -141,7 +98,7 @@ const Fastify = ({ auth: _auth, cookies = {
141
98
  await onClose();
142
99
  });
143
100
  }
144
- const signalListeners = (0, close_with_grace_1.default)(async ({ signal, err }) => {
101
+ const signalListeners = closeWithGrace(async ({ signal, err }) => {
145
102
  if (err) {
146
103
  httpd.log.error({ err }, 'server shutting down due to error...');
147
104
  }
@@ -155,8 +112,7 @@ const Fastify = ({ auth: _auth, cookies = {
155
112
  });
156
113
  return httpd;
157
114
  };
158
- exports.Fastify = Fastify;
159
- exports.default = exports.Fastify;
160
- var fastify_auth_2 = require("@react-foundry/fastify-auth");
161
- Object.defineProperty(exports, "SessionStore", { enumerable: true, get: function () { return fastify_auth_2.SessionStore; } });
162
- __exportStar(require("./config-helpers"), exports);
115
+ export default Fastify;
116
+ export { AuthMethod };
117
+ export { SessionStore } from '@react-foundry/fastify-auth';
118
+ export * from './config-helpers';
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@react-foundry/fastify",
3
- "version": "0.1.8",
3
+ "version": "0.2.0",
4
4
  "description": "A customised Fastify server for running websites.",
5
+ "type": "module",
5
6
  "main": "dist/index.js",
6
7
  "exports": {
7
8
  ".": {
8
9
  "types": "./dist/index.d.ts",
9
- "import": "./dist/index.mjs",
10
- "require": "./dist/index.js",
11
- "default": "./dist/index.mjs"
10
+ "import": "./dist/index.js",
11
+ "default": "./dist/index.js"
12
12
  }
13
13
  },
14
14
  "files": [
@@ -23,24 +23,22 @@
23
23
  "@fastify/rate-limit": "^10.3.0",
24
24
  "close-with-grace": "^2.5.0",
25
25
  "fastify": "^5.8.4",
26
- "@react-foundry/fastify-auth": "^0.1.8",
27
- "@react-foundry/fastify-dev-logger": "^0.1.8",
28
- "@react-foundry/fastify-harden": "^0.1.8"
26
+ "@react-foundry/fastify-auth": "^0.2.0",
27
+ "@react-foundry/fastify-dev-logger": "^0.2.0",
28
+ "@react-foundry/fastify-harden": "^0.2.0"
29
29
  },
30
30
  "devDependencies": {
31
31
  "jest": "30.3.0",
32
32
  "jest-environment-jsdom": "30.3.0",
33
- "ts-jest": "29.4.6",
33
+ "ts-jest": "29.4.9",
34
34
  "typescript": "5.9.3",
35
- "@react-foundry/types-helpers": "0.1.8"
35
+ "@react-foundry/types-helpers": "0.2.0"
36
36
  },
37
37
  "scripts": {
38
38
  "test": "NODE_OPTIONS=--experimental-vm-modules jest",
39
- "build": "npm run build:esm && npm run build:cjs",
40
- "build:esm": "tsc -m es2022 && find dist -name '*.js' -exec sh -c 'mv \"$0\" \"${0%.js}.mjs\"' {} \\;",
41
- "build:cjs": "tsc",
39
+ "build": "tsc",
42
40
  "clean": "rm -rf dist tsconfig.tsbuildinfo"
43
41
  },
44
- "module": "dist/index.mjs",
42
+ "module": "dist/index.js",
45
43
  "typings": "dist/index.d.ts"
46
44
  }
@@ -1,16 +0,0 @@
1
- export var Mode;
2
- (function (Mode) {
3
- Mode["Server"] = "server";
4
- Mode["Serverless"] = "serverless";
5
- Mode["StaticGenerator"] = "static-generator";
6
- })(Mode || (Mode = {}));
7
- ;
8
- export var NodeEnv;
9
- (function (NodeEnv) {
10
- NodeEnv["Development"] = "development";
11
- NodeEnv["Production"] = "production";
12
- })(NodeEnv || (NodeEnv = {}));
13
- ;
14
- const parseBoolean = (pattern) => ((s) => !!(s && s.match(pattern)));
15
- export const defaultsFalse = parseBoolean(/(yes|on|true|1)/i);
16
- export const defaultsTrue = (s) => !parseBoolean(/(no|off|false|0)/i)(s);
package/dist/index.mjs DELETED
@@ -1,118 +0,0 @@
1
- import closeWithGrace from 'close-with-grace';
2
- import _Fastify from 'fastify';
3
- import fastifyRateLimit from '@fastify/rate-limit';
4
- import fastifyAuth, { AuthMethod } from '@react-foundry/fastify-auth';
5
- import fastifyHarden from '@react-foundry/fastify-harden';
6
- import { NodeEnv } from './config-helpers';
7
- const is = () => true;
8
- const probeHandler = (isFn) => async (_req, reply) => {
9
- if (await isFn()) {
10
- return 'OK';
11
- }
12
- else {
13
- reply.statusCode = 503;
14
- return 'Service Unavailable';
15
- }
16
- };
17
- const parseForwarded = (hdr) => {
18
- const str = (Array.isArray(hdr)
19
- ? hdr[0]
20
- : hdr);
21
- return str?.split(',')[0]?.trim();
22
- };
23
- export const Fastify = ({ auth: _auth, cookies = {
24
- secret: 'changeme'
25
- }, contentSecurityPolicy, dev = process.env.NODE_ENV === NodeEnv.Development, isLive = is, isReady = is, logger: _logger = {}, onClose, permissionsPolicy, rateLimit: _rateLimit, session, ...options }) => {
26
- const isTTY = process.stdout.isTTY;
27
- const stdLogger = {
28
- level: 'info',
29
- serializers: {
30
- req: (req) => ({
31
- method: req.method,
32
- url: req.url,
33
- version: req.headers && req.headers['accept-version']?.toString(),
34
- host: req.host,
35
- remoteAddress: (req.ip === '127.0.0.1'
36
- ? parseForwarded(req.headers['x-forwarded-for']) || req.ip
37
- : req.ip),
38
- remotePort: req.socket?.remotePort,
39
- userAgent: req.headers && req.headers['user-agent']
40
- }),
41
- res: (reply) => ({
42
- statusCode: reply.statusCode,
43
- contentLength: (typeof reply.getHeaders === 'function') && reply.getHeaders()['content-length']
44
- })
45
- }
46
- };
47
- const devLogger = {
48
- ...stdLogger,
49
- level: 'debug',
50
- transport: {
51
- target: '@react-foundry/fastify-dev-logger'
52
- }
53
- };
54
- const logger = (_logger instanceof Object
55
- ? {
56
- ...(!(dev && isTTY)
57
- ? stdLogger
58
- : devLogger),
59
- ..._logger
60
- }
61
- : _logger);
62
- const httpd = _Fastify({
63
- logger,
64
- ...options
65
- });
66
- const auth = ((session?.store === undefined) && (_auth === undefined || _auth.method === AuthMethod.None)
67
- ? undefined
68
- : {
69
- rateLimit: _rateLimit,
70
- ...(_auth || {}),
71
- session: {
72
- ...(session || {}),
73
- cookies
74
- }
75
- });
76
- const rateLimit = _rateLimit && {
77
- ..._rateLimit,
78
- global: true
79
- };
80
- httpd.log.debug(`Privacy mode: ${!!auth?.privacy}`);
81
- httpd.log.debug(`Authentication method: ${auth?.method || 'none'}`);
82
- httpd.log.debug(`Session store: ${session?.store}`);
83
- httpd.register(fastifyHarden, {
84
- contentSecurityPolicy,
85
- dev,
86
- permissionsPolicy
87
- });
88
- if (rateLimit) {
89
- httpd.register(fastifyRateLimit, rateLimit);
90
- }
91
- if (auth) {
92
- httpd.register(fastifyAuth, auth);
93
- }
94
- httpd.get('/healthz', probeHandler(isLive));
95
- httpd.get('/readiness', probeHandler(isReady));
96
- if (onClose) {
97
- httpd.addHook('onClose', async (_fastify) => {
98
- await onClose();
99
- });
100
- }
101
- const signalListeners = closeWithGrace(async ({ signal, err }) => {
102
- if (err) {
103
- httpd.log.error({ err }, 'server shutting down due to error...');
104
- }
105
- else {
106
- httpd.log.info(`${signal} received; server shutting down...`);
107
- }
108
- await httpd.close();
109
- });
110
- httpd.addHook('onClose', async (_fastify) => {
111
- signalListeners.uninstall();
112
- });
113
- return httpd;
114
- };
115
- export default Fastify;
116
- export { AuthMethod };
117
- export { SessionStore } from '@react-foundry/fastify-auth';
118
- export * from './config-helpers';