@cedarjs/web-server 0.0.4

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Cedar
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.
package/README.md ADDED
@@ -0,0 +1,6 @@
1
+ # Redwood's server for the Web side
2
+
3
+ This package contains code for Redwood's web server.
4
+
5
+ This package isn't optimized for production use at scale on it's own.
6
+ It's recommended to use a CDN or a web server like Nginx as performant alternatives.
package/dist/bin.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=bin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bin.d.ts","sourceRoot":"","sources":["../src/bin.ts"],"names":[],"mappings":""}
package/dist/bin.js ADDED
@@ -0,0 +1,152 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __esm = (fn, res) => function __init() {
10
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
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
+
29
+ // src/webServer.ts
30
+ async function serveWeb(options = {}) {
31
+ const start = Date.now();
32
+ console.log(import_chalk.default.dim.italic("Starting Web Server..."));
33
+ const distIndexExists = await import_fs_extra.default.pathExists(
34
+ import_path.default.join((0, import_project_config.getPaths)().web.dist, "index.html")
35
+ );
36
+ if (!distIndexExists) {
37
+ throw new Error(
38
+ "no built files to serve; run `yarn rw build web` before serving the web side"
39
+ );
40
+ }
41
+ if (process.env.REDWOOD_WEB_PORT) {
42
+ options.port ??= parseInt(process.env.REDWOOD_WEB_PORT);
43
+ }
44
+ options.port ??= (0, import_project_config.getConfig)().web.port;
45
+ options.host ??= process.env.REDWOOD_WEB_HOST;
46
+ options.host ??= (0, import_project_config.getConfig)().web.host;
47
+ options.host ??= process.env.NODE_ENV === "production" ? "0.0.0.0" : "::";
48
+ if (process.env.NODE_ENV === "production" && options.host !== "0.0.0.0") {
49
+ console.warn(
50
+ `Warning: host '${options.host}' may need to be '0.0.0.0' in production for containerized deployments`
51
+ );
52
+ }
53
+ const fastify = (0, import_fastify.default)({
54
+ requestTimeout: 15e3,
55
+ logger: {
56
+ level: process.env.LOG_LEVEL ?? (process.env.NODE_ENV === "development" ? "debug" : "warn")
57
+ }
58
+ });
59
+ fastify.register(import_fastify_web.redwoodFastifyWeb, { redwood: options });
60
+ const address = await fastify.listen({
61
+ port: options.port,
62
+ host: options.host
63
+ });
64
+ console.log(import_chalk.default.dim.italic("Took " + (Date.now() - start) + " ms"));
65
+ console.log(`Web server listening at ${import_chalk.default.green(address)}`);
66
+ }
67
+ var import_path, import_chalk, import_fastify, import_fs_extra, import_fastify_web, import_project_config;
68
+ var init_webServer = __esm({
69
+ "src/webServer.ts"() {
70
+ "use strict";
71
+ import_path = __toESM(require("path"));
72
+ import_chalk = __toESM(require("chalk"));
73
+ import_fastify = __toESM(require("fastify"));
74
+ import_fs_extra = __toESM(require("fs-extra"));
75
+ import_fastify_web = require("@cedarjs/fastify-web");
76
+ import_project_config = require("@cedarjs/project-config");
77
+ }
78
+ });
79
+
80
+ // src/cliConfigHandler.ts
81
+ async function handler(options) {
82
+ try {
83
+ await serveWeb(options);
84
+ } catch (error) {
85
+ process.exitCode ||= 1;
86
+ console.error(`Error: ${error.message}`);
87
+ }
88
+ }
89
+ var init_cliConfigHandler = __esm({
90
+ "src/cliConfigHandler.ts"() {
91
+ "use strict";
92
+ init_webServer();
93
+ }
94
+ });
95
+
96
+ // src/bin.ts
97
+ var import_path2 = __toESM(require("path"));
98
+ var import_dotenv_defaults = require("dotenv-defaults");
99
+ var import_helpers = require("yargs/helpers");
100
+ var import_yargs = __toESM(require("yargs/yargs"));
101
+ var import_project_config2 = require("@cedarjs/project-config");
102
+
103
+ // package.json
104
+ var bin = {
105
+ "rw-web-server": "./dist/bin.js"
106
+ };
107
+
108
+ // src/cliConfig.ts
109
+ var description = "Start a server for serving the web side";
110
+ function builder(yargs2) {
111
+ yargs2.options({
112
+ port: {
113
+ description: "The port to listen on",
114
+ type: "number",
115
+ alias: "p"
116
+ },
117
+ host: {
118
+ description: "The host to listen on. Note that you most likely want this to be '0.0.0.0' in production",
119
+ type: "string"
120
+ },
121
+ apiProxyTarget: {
122
+ description: "Forward requests from the apiUrl (in the redwood.toml) to this target. apiUrl must be a relative URL",
123
+ type: "string",
124
+ alias: "api-proxy-target"
125
+ },
126
+ // Deprecated alias of `apiProxyTarget`
127
+ apiHost: {
128
+ hidden: true,
129
+ alias: "api-host"
130
+ }
131
+ });
132
+ }
133
+
134
+ // src/bin.ts
135
+ init_cliConfigHandler();
136
+ if (!process.env.REDWOOD_ENV_FILES_LOADED) {
137
+ (0, import_dotenv_defaults.config)({
138
+ path: import_path2.default.join((0, import_project_config2.getPaths)().base, ".env"),
139
+ defaults: import_path2.default.join((0, import_project_config2.getPaths)().base, ".env.defaults"),
140
+ multiline: true
141
+ });
142
+ process.env.REDWOOD_ENV_FILES_LOADED = "true";
143
+ }
144
+ process.env.NODE_ENV ??= "production";
145
+ var [scriptName] = Object.keys(bin);
146
+ (0, import_yargs.default)((0, import_helpers.hideBin)(process.argv)).scriptName(scriptName).alias("h", "help").alias("v", "version").strict().example(
147
+ "yarn $0 --api-url=/api --api-proxy-target=https://api.redwood.horse",
148
+ "Start the web server and proxy requests made to '/api' to 'https://api.redwood.horse'"
149
+ ).example(
150
+ "yarn $0 --api-url=https://api.redwood.horse",
151
+ "Start the web server send api requests to 'https://api.redwood.horse' (make sure to configure CORS)"
152
+ ).command("$0", description, builder, handler).parse();
@@ -0,0 +1,6 @@
1
+ import type { Argv } from 'yargs';
2
+ import type { ParsedOptions } from './types';
3
+ export declare const description = "Start a server for serving the web side";
4
+ export declare function builder(yargs: Argv<ParsedOptions>): void;
5
+ export declare function handler(options: ParsedOptions): Promise<void>;
6
+ //# sourceMappingURL=cliConfig.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cliConfig.d.ts","sourceRoot":"","sources":["../src/cliConfig.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,OAAO,CAAA;AAEjC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AAE5C,eAAO,MAAM,WAAW,4CAA4C,CAAA;AAEpE,wBAAgB,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,aAAa,CAAC,QAwBjD;AAED,wBAAsB,OAAO,CAAC,OAAO,EAAE,aAAa,iBAGnD"}
@@ -0,0 +1,69 @@
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
+ var cliConfig_exports = {};
30
+ __export(cliConfig_exports, {
31
+ builder: () => builder,
32
+ description: () => description,
33
+ handler: () => handler
34
+ });
35
+ module.exports = __toCommonJS(cliConfig_exports);
36
+ const description = "Start a server for serving the web side";
37
+ function builder(yargs) {
38
+ yargs.options({
39
+ port: {
40
+ description: "The port to listen on",
41
+ type: "number",
42
+ alias: "p"
43
+ },
44
+ host: {
45
+ description: "The host to listen on. Note that you most likely want this to be '0.0.0.0' in production",
46
+ type: "string"
47
+ },
48
+ apiProxyTarget: {
49
+ description: "Forward requests from the apiUrl (in the redwood.toml) to this target. apiUrl must be a relative URL",
50
+ type: "string",
51
+ alias: "api-proxy-target"
52
+ },
53
+ // Deprecated alias of `apiProxyTarget`
54
+ apiHost: {
55
+ hidden: true,
56
+ alias: "api-host"
57
+ }
58
+ });
59
+ }
60
+ async function handler(options) {
61
+ const { handler: handler2 } = await import("./cliConfigHandler.js");
62
+ await handler2(options);
63
+ }
64
+ // Annotate the CommonJS export names for ESM import in node:
65
+ 0 && (module.exports = {
66
+ builder,
67
+ description,
68
+ handler
69
+ });
@@ -0,0 +1,3 @@
1
+ import type { ParsedOptions } from './types';
2
+ export declare function handler(options: ParsedOptions): Promise<void>;
3
+ //# sourceMappingURL=cliConfigHandler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cliConfigHandler.d.ts","sourceRoot":"","sources":["../src/cliConfigHandler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AAG5C,wBAAsB,OAAO,CAAC,OAAO,EAAE,aAAa,iBAQnD"}
@@ -0,0 +1,36 @@
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 cliConfigHandler_exports = {};
20
+ __export(cliConfigHandler_exports, {
21
+ handler: () => handler
22
+ });
23
+ module.exports = __toCommonJS(cliConfigHandler_exports);
24
+ var import_webServer = require("./webServer");
25
+ async function handler(options) {
26
+ try {
27
+ await (0, import_webServer.serveWeb)(options);
28
+ } catch (error) {
29
+ process.exitCode ||= 1;
30
+ console.error(`Error: ${error.message}`);
31
+ }
32
+ }
33
+ // Annotate the CommonJS export names for ESM import in node:
34
+ 0 && (module.exports = {
35
+ handler
36
+ });
@@ -0,0 +1,6 @@
1
+ import type { RedwoodFastifyWebOptions } from '@cedarjs/fastify-web';
2
+ export type ParsedOptions = {
3
+ port?: number;
4
+ host?: string;
5
+ } & RedwoodFastifyWebOptions['redwood'];
6
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,sBAAsB,CAAA;AAEpE,MAAM,MAAM,aAAa,GAAG;IAC1B,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,MAAM,CAAA;CACd,GAAG,wBAAwB,CAAC,SAAS,CAAC,CAAA"}
@@ -0,0 +1,3 @@
1
+ import type { ParsedOptions } from './types';
2
+ export declare function serveWeb(options?: ParsedOptions): Promise<void>;
3
+ //# sourceMappingURL=webServer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webServer.d.ts","sourceRoot":"","sources":["../src/webServer.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AAE5C,wBAAsB,QAAQ,CAAC,OAAO,GAAE,aAAkB,iBA8CzD"}
@@ -0,0 +1,80 @@
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
+ var webServer_exports = {};
30
+ __export(webServer_exports, {
31
+ serveWeb: () => serveWeb
32
+ });
33
+ module.exports = __toCommonJS(webServer_exports);
34
+ var import_path = __toESM(require("path"));
35
+ var import_chalk = __toESM(require("chalk"));
36
+ var import_fastify = __toESM(require("fastify"));
37
+ var import_fs_extra = __toESM(require("fs-extra"));
38
+ var import_fastify_web = require("@cedarjs/fastify-web");
39
+ var import_project_config = require("@cedarjs/project-config");
40
+ async function serveWeb(options = {}) {
41
+ const start = Date.now();
42
+ console.log(import_chalk.default.dim.italic("Starting Web Server..."));
43
+ const distIndexExists = await import_fs_extra.default.pathExists(
44
+ import_path.default.join((0, import_project_config.getPaths)().web.dist, "index.html")
45
+ );
46
+ if (!distIndexExists) {
47
+ throw new Error(
48
+ "no built files to serve; run `yarn rw build web` before serving the web side"
49
+ );
50
+ }
51
+ if (process.env.REDWOOD_WEB_PORT) {
52
+ options.port ??= parseInt(process.env.REDWOOD_WEB_PORT);
53
+ }
54
+ options.port ??= (0, import_project_config.getConfig)().web.port;
55
+ options.host ??= process.env.REDWOOD_WEB_HOST;
56
+ options.host ??= (0, import_project_config.getConfig)().web.host;
57
+ options.host ??= process.env.NODE_ENV === "production" ? "0.0.0.0" : "::";
58
+ if (process.env.NODE_ENV === "production" && options.host !== "0.0.0.0") {
59
+ console.warn(
60
+ `Warning: host '${options.host}' may need to be '0.0.0.0' in production for containerized deployments`
61
+ );
62
+ }
63
+ const fastify = (0, import_fastify.default)({
64
+ requestTimeout: 15e3,
65
+ logger: {
66
+ level: process.env.LOG_LEVEL ?? (process.env.NODE_ENV === "development" ? "debug" : "warn")
67
+ }
68
+ });
69
+ fastify.register(import_fastify_web.redwoodFastifyWeb, { redwood: options });
70
+ const address = await fastify.listen({
71
+ port: options.port,
72
+ host: options.host
73
+ });
74
+ console.log(import_chalk.default.dim.italic("Took " + (Date.now() - start) + " ms"));
75
+ console.log(`Web server listening at ${import_chalk.default.green(address)}`);
76
+ }
77
+ // Annotate the CommonJS export names for ESM import in node:
78
+ 0 && (module.exports = {
79
+ serveWeb
80
+ });
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@cedarjs/web-server",
3
+ "version": "0.0.4",
4
+ "description": "CedarJS's server for the Web side",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/cedarjs/cedar.git",
8
+ "directory": "packages/web-server"
9
+ },
10
+ "license": "MIT",
11
+ "main": "./dist/cliConfig.js",
12
+ "types": "./dist/cliConfig.d.ts",
13
+ "bin": {
14
+ "rw-web-server": "./dist/bin.js"
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "scripts": {
20
+ "build": "tsx ./build.mts && yarn build:types",
21
+ "build:pack": "yarn pack -o cedar-web-server.tgz",
22
+ "build:types": "tsc --build --verbose",
23
+ "build:watch": "nodemon --watch src --ext \"js,jsx,ts,tsx\" --ignore dist --exec \"yarn build && yarn fix:permissions\"",
24
+ "fix:permissions": "chmod +x dist/index.js; chmod +x dist/watch.js",
25
+ "prepublishOnly": "NODE_ENV=production yarn build"
26
+ },
27
+ "dependencies": {
28
+ "@cedarjs/fastify-web": "0.0.4",
29
+ "@cedarjs/project-config": "0.0.4",
30
+ "chalk": "4.1.2",
31
+ "dotenv-defaults": "5.0.2",
32
+ "fastify": "4.28.1",
33
+ "fs-extra": "11.2.0",
34
+ "yargs": "17.7.2"
35
+ },
36
+ "devDependencies": {
37
+ "@cedarjs/framework-tools": "0.0.4",
38
+ "tsx": "4.19.3",
39
+ "typescript": "5.6.2"
40
+ },
41
+ "publishConfig": {
42
+ "access": "public"
43
+ },
44
+ "gitHead": "5b4f77f985bd86ee31ee7338312627accf0cb85b"
45
+ }