@redmix/fastify-web 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/dist/helpers.d.ts +3 -0
- package/dist/helpers.d.ts.map +1 -0
- package/dist/helpers.js +32 -0
- package/dist/resolveOptions.d.ts +12 -0
- package/dist/resolveOptions.d.ts.map +1 -0
- package/dist/types.d.ts +23 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/web.d.ts +6 -0
- package/dist/web.d.ts.map +1 -0
- package/dist/web.js +164 -0
- package/package.json +41 -0
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 Redmix
|
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.
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAC7D,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,UAK1C"}
|
package/dist/helpers.js
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __defProp = Object.defineProperty;
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
+
var __export = (target, all) => {
|
7
|
+
for (var name in all)
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
9
|
+
};
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
12
|
+
for (let key of __getOwnPropNames(from))
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
15
|
+
}
|
16
|
+
return to;
|
17
|
+
};
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
19
|
+
var helpers_exports = {};
|
20
|
+
__export(helpers_exports, {
|
21
|
+
coerceRootPath: () => coerceRootPath
|
22
|
+
});
|
23
|
+
module.exports = __toCommonJS(helpers_exports);
|
24
|
+
function coerceRootPath(path) {
|
25
|
+
const prefix = !path.startsWith("/") ? "/" : "";
|
26
|
+
const suffix = !path.endsWith("/") ? "/" : "";
|
27
|
+
return `${prefix}${path}${suffix}`;
|
28
|
+
}
|
29
|
+
// Annotate the CommonJS export names for ESM import in node:
|
30
|
+
0 && (module.exports = {
|
31
|
+
coerceRootPath
|
32
|
+
});
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import type { RedwoodFastifyWebOptions } from './types';
|
2
|
+
export declare function resolveOptions(options: RedwoodFastifyWebOptions): {
|
3
|
+
redwoodOptions: {
|
4
|
+
apiUrl?: string;
|
5
|
+
apiProxyTarget?: string;
|
6
|
+
apiHost?: string;
|
7
|
+
};
|
8
|
+
flags: {
|
9
|
+
shouldRegisterApiUrl: boolean;
|
10
|
+
};
|
11
|
+
};
|
12
|
+
//# sourceMappingURL=resolveOptions.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"resolveOptions.d.ts","sourceRoot":"","sources":["../src/resolveOptions.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,SAAS,CAAA;AAEvD,wBAAgB,cAAc,CAAC,OAAO,EAAE,wBAAwB;;;;;;;;;EA0E/D"}
|
package/dist/types.d.ts
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
export interface RedwoodFastifyWebOptions {
|
2
|
+
redwood?: {
|
3
|
+
/**
|
4
|
+
* Specify the URL to your API server.
|
5
|
+
* This can be a relative URL on the current domain (`/.redwood/functions`),
|
6
|
+
* in which case the `apiProxyTarget` option must be set,
|
7
|
+
* or a fully-qualified URL (`https://api.redwood.horse`).
|
8
|
+
*
|
9
|
+
* Note: This should not include the path to the GraphQL Server.
|
10
|
+
**/
|
11
|
+
apiUrl?: string;
|
12
|
+
/**
|
13
|
+
* The fully-qualified URL to proxy requests to from `apiUrl`.
|
14
|
+
* Only valid when `apiUrl` is a relative URL.
|
15
|
+
*/
|
16
|
+
apiProxyTarget?: string;
|
17
|
+
/**
|
18
|
+
* @deprecated Use `apiProxyTarget` instead.
|
19
|
+
*/
|
20
|
+
apiHost?: string;
|
21
|
+
};
|
22
|
+
}
|
23
|
+
//# sourceMappingURL=types.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,wBAAwB;IACvC,OAAO,CAAC,EAAE;QACR;;;;;;;YAOI;QACJ,MAAM,CAAC,EAAE,MAAM,CAAA;QACf;;;WAGG;QACH,cAAc,CAAC,EAAE,MAAM,CAAA;QAEvB;;WAEG;QACH,OAAO,CAAC,EAAE,MAAM,CAAA;KACjB,CAAA;CACF"}
|
package/dist/web.d.ts
ADDED
@@ -0,0 +1,6 @@
|
|
1
|
+
import type { FastifyInstance } from 'fastify';
|
2
|
+
import { coerceRootPath } from './helpers';
|
3
|
+
import type { RedwoodFastifyWebOptions } from './types';
|
4
|
+
export { coerceRootPath, RedwoodFastifyWebOptions };
|
5
|
+
export declare function redwoodFastifyWeb(fastify: FastifyInstance, opts: RedwoodFastifyWebOptions): Promise<void>;
|
6
|
+
//# sourceMappingURL=web.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"web.d.ts","sourceRoot":"","sources":["../src/web.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,eAAe,EAAgC,MAAM,SAAS,CAAA;AAI5E,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAA;AAE1C,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,SAAS,CAAA;AAEvD,OAAO,EAAE,cAAc,EAAE,wBAAwB,EAAE,CAAA;AAEnD,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,eAAe,EACxB,IAAI,EAAE,wBAAwB,iBA0G/B"}
|
package/dist/web.js
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __create = Object.create;
|
3
|
+
var __defProp = Object.defineProperty;
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
8
|
+
var __export = (target, all) => {
|
9
|
+
for (var name in all)
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
11
|
+
};
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
14
|
+
for (let key of __getOwnPropNames(from))
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
17
|
+
}
|
18
|
+
return to;
|
19
|
+
};
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
26
|
+
mod
|
27
|
+
));
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
29
|
+
|
30
|
+
// src/web.ts
|
31
|
+
var web_exports = {};
|
32
|
+
__export(web_exports, {
|
33
|
+
coerceRootPath: () => coerceRootPath,
|
34
|
+
redwoodFastifyWeb: () => redwoodFastifyWeb
|
35
|
+
});
|
36
|
+
module.exports = __toCommonJS(web_exports);
|
37
|
+
var import_fs = __toESM(require("fs"));
|
38
|
+
var import_path = __toESM(require("path"));
|
39
|
+
var import_http_proxy = __toESM(require("@fastify/http-proxy"));
|
40
|
+
var import_static = __toESM(require("@fastify/static"));
|
41
|
+
var import_url_data = __toESM(require("@fastify/url-data"));
|
42
|
+
var import_fast_glob = __toESM(require("fast-glob"));
|
43
|
+
var import_project_config2 = require("@redmix/project-config");
|
44
|
+
|
45
|
+
// src/helpers.ts
|
46
|
+
function coerceRootPath(path2) {
|
47
|
+
const prefix = !path2.startsWith("/") ? "/" : "";
|
48
|
+
const suffix = !path2.endsWith("/") ? "/" : "";
|
49
|
+
return `${prefix}${path2}${suffix}`;
|
50
|
+
}
|
51
|
+
|
52
|
+
// src/resolveOptions.ts
|
53
|
+
var import_project_config = require("@redmix/project-config");
|
54
|
+
function resolveOptions(options) {
|
55
|
+
const redwoodOptions = options.redwood ?? {};
|
56
|
+
const flags = {
|
57
|
+
shouldRegisterApiUrl: false
|
58
|
+
};
|
59
|
+
redwoodOptions.apiUrl ??= (0, import_project_config.getConfig)().web.apiUrl;
|
60
|
+
const apiUrlIsFullyQualifiedUrl = isFullyQualifiedUrl(redwoodOptions.apiUrl);
|
61
|
+
if (redwoodOptions.apiHost && !redwoodOptions.apiProxyTarget) {
|
62
|
+
redwoodOptions.apiProxyTarget = redwoodOptions.apiHost;
|
63
|
+
delete redwoodOptions.apiHost;
|
64
|
+
}
|
65
|
+
if (redwoodOptions.apiUrl.trim() === "") {
|
66
|
+
throw new Error(`\`apiUrl\` cannot be an empty string`);
|
67
|
+
}
|
68
|
+
if (redwoodOptions.apiProxyTarget && !isFullyQualifiedUrl(redwoodOptions.apiProxyTarget)) {
|
69
|
+
throw new Error(
|
70
|
+
`If you provide \`apiProxyTarget\`, it has to be a fully-qualified URL. \`apiProxyTarget\` is '${redwoodOptions.apiProxyTarget}'`
|
71
|
+
);
|
72
|
+
}
|
73
|
+
if (!redwoodOptions.apiUrl && redwoodOptions.apiProxyTarget) {
|
74
|
+
throw new Error(
|
75
|
+
`If you provide \`apiProxyTarget\`, \`apiUrl\` has to be a relative URL. \`apiUrl\` is '${redwoodOptions.apiUrl}'`
|
76
|
+
);
|
77
|
+
}
|
78
|
+
if (apiUrlIsFullyQualifiedUrl && redwoodOptions.apiProxyTarget) {
|
79
|
+
throw new Error(
|
80
|
+
`If you provide \`apiProxyTarget\`, \`apiUrl\` cannot be a fully-qualified URL. \`apiUrl\` is '${redwoodOptions.apiUrl}'`
|
81
|
+
);
|
82
|
+
}
|
83
|
+
if (!apiUrlIsFullyQualifiedUrl && !redwoodOptions.apiProxyTarget) {
|
84
|
+
flags.shouldRegisterApiUrl = true;
|
85
|
+
}
|
86
|
+
return { redwoodOptions, flags };
|
87
|
+
}
|
88
|
+
function isFullyQualifiedUrl(url) {
|
89
|
+
try {
|
90
|
+
new URL(url);
|
91
|
+
return true;
|
92
|
+
} catch {
|
93
|
+
return false;
|
94
|
+
}
|
95
|
+
}
|
96
|
+
|
97
|
+
// src/web.ts
|
98
|
+
async function redwoodFastifyWeb(fastify, opts) {
|
99
|
+
const { redwoodOptions, flags } = resolveOptions(opts);
|
100
|
+
fastify.register(import_url_data.default);
|
101
|
+
fastify.register(import_static.default, { root: (0, import_project_config2.getPaths)().web.dist });
|
102
|
+
if (redwoodOptions.apiProxyTarget) {
|
103
|
+
fastify.register(import_http_proxy.default, {
|
104
|
+
prefix: redwoodOptions.apiUrl,
|
105
|
+
upstream: redwoodOptions.apiProxyTarget,
|
106
|
+
disableCache: true,
|
107
|
+
replyOptions: {
|
108
|
+
rewriteRequestHeaders: (req, headers) => ({
|
109
|
+
...headers,
|
110
|
+
// preserve the original host header, instead of letting it be overwritten by the proxy
|
111
|
+
host: req.headers.host
|
112
|
+
})
|
113
|
+
}
|
114
|
+
});
|
115
|
+
}
|
116
|
+
if (redwoodOptions.apiUrl && flags.shouldRegisterApiUrl) {
|
117
|
+
const apiUrlHandler = (_req, reply) => {
|
118
|
+
reply.code(200);
|
119
|
+
reply.send({
|
120
|
+
data: null,
|
121
|
+
errors: [
|
122
|
+
{
|
123
|
+
message: `Bad Gateway: you may have misconfigured apiUrl and apiProxyTarget. If apiUrl is a relative URL, you must provide apiProxyTarget.`,
|
124
|
+
extensions: {
|
125
|
+
code: "BAD_GATEWAY",
|
126
|
+
httpStatus: 502
|
127
|
+
}
|
128
|
+
}
|
129
|
+
]
|
130
|
+
});
|
131
|
+
};
|
132
|
+
const apiUrlWarningPath = coerceRootPath(redwoodOptions.apiUrl);
|
133
|
+
fastify.all(apiUrlWarningPath, apiUrlHandler);
|
134
|
+
fastify.all(`${apiUrlWarningPath}*`, apiUrlHandler);
|
135
|
+
}
|
136
|
+
const prerenderedFiles = await (0, import_fast_glob.default)("**/*.html", {
|
137
|
+
cwd: (0, import_project_config2.getPaths)().web.dist,
|
138
|
+
ignore: ["index.html", "200.html", "404.html"]
|
139
|
+
});
|
140
|
+
for (const prerenderedFile of prerenderedFiles) {
|
141
|
+
const [pathName] = prerenderedFile.split(".html");
|
142
|
+
fastify.get(`/${pathName}`, (_, reply) => {
|
143
|
+
reply.header("Content-Type", "text/html; charset=UTF-8");
|
144
|
+
reply.sendFile(prerenderedFile);
|
145
|
+
});
|
146
|
+
}
|
147
|
+
const prerenderIndexPath = import_path.default.join((0, import_project_config2.getPaths)().web.dist, "200.html");
|
148
|
+
const fallbackIndexPath = import_fs.default.existsSync(prerenderIndexPath) ? "200.html" : "index.html";
|
149
|
+
fastify.setNotFoundHandler({}, (req, reply) => {
|
150
|
+
const urlData = req.urlData();
|
151
|
+
const requestHasExtension = !!import_path.default.extname(urlData.path ?? "");
|
152
|
+
if (requestHasExtension && urlData.path?.startsWith("/assets/")) {
|
153
|
+
reply.code(404);
|
154
|
+
return reply.send("Not Found");
|
155
|
+
}
|
156
|
+
reply.header("Content-Type", "text/html; charset=UTF-8");
|
157
|
+
return reply.sendFile(fallbackIndexPath);
|
158
|
+
});
|
159
|
+
}
|
160
|
+
// Annotate the CommonJS export names for ESM import in node:
|
161
|
+
0 && (module.exports = {
|
162
|
+
coerceRootPath,
|
163
|
+
redwoodFastifyWeb
|
164
|
+
});
|
package/package.json
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
{
|
2
|
+
"name": "@redmix/fastify-web",
|
3
|
+
"version": "0.0.1",
|
4
|
+
"repository": {
|
5
|
+
"type": "git",
|
6
|
+
"url": "git+https://github.com/redmix-run/redmix.git",
|
7
|
+
"directory": "packages/adapters/fastify/web"
|
8
|
+
},
|
9
|
+
"license": "MIT",
|
10
|
+
"main": "./dist/web.js",
|
11
|
+
"types": "./dist/web.d.ts",
|
12
|
+
"files": [
|
13
|
+
"dist"
|
14
|
+
],
|
15
|
+
"scripts": {
|
16
|
+
"build": "tsx ./build.mts && yarn build:types",
|
17
|
+
"build:pack": "yarn pack -o redmix-fastify-web.tgz",
|
18
|
+
"build:types": "tsc --build --verbose",
|
19
|
+
"prepublishOnly": "NODE_ENV=production yarn build",
|
20
|
+
"test": "vitest run",
|
21
|
+
"test:watch": "vitest watch"
|
22
|
+
},
|
23
|
+
"dependencies": {
|
24
|
+
"@fastify/http-proxy": "9.5.0",
|
25
|
+
"@fastify/static": "6.12.0",
|
26
|
+
"@fastify/url-data": "5.4.0",
|
27
|
+
"@redmix/project-config": "0.0.1",
|
28
|
+
"fast-glob": "3.3.2"
|
29
|
+
},
|
30
|
+
"devDependencies": {
|
31
|
+
"@redmix/framework-tools": "0.0.1",
|
32
|
+
"fastify": "4.28.1",
|
33
|
+
"tsx": "4.19.3",
|
34
|
+
"typescript": "5.6.2",
|
35
|
+
"vitest": "2.1.9"
|
36
|
+
},
|
37
|
+
"publishConfig": {
|
38
|
+
"access": "public"
|
39
|
+
},
|
40
|
+
"gitHead": "688027c97502c500ebbede9cdc7cc51545a8dcf3"
|
41
|
+
}
|