@redmix/api-server 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.
Files changed (66) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +64 -0
  3. package/dist/apiCLIConfig.d.ts +6 -0
  4. package/dist/apiCLIConfig.d.ts.map +1 -0
  5. package/dist/apiCLIConfig.js +71 -0
  6. package/dist/apiCLIConfigHandler.d.ts +3 -0
  7. package/dist/apiCLIConfigHandler.d.ts.map +1 -0
  8. package/dist/apiCLIConfigHandler.js +68 -0
  9. package/dist/bin.d.ts +2 -0
  10. package/dist/bin.d.ts.map +1 -0
  11. package/dist/bin.js +850 -0
  12. package/dist/bothCLIConfig.d.ts +6 -0
  13. package/dist/bothCLIConfig.d.ts.map +1 -0
  14. package/dist/bothCLIConfig.js +76 -0
  15. package/dist/bothCLIConfigHandler.d.ts +3 -0
  16. package/dist/bothCLIConfigHandler.d.ts.map +1 -0
  17. package/dist/bothCLIConfigHandler.js +107 -0
  18. package/dist/buildManager.d.ts +15 -0
  19. package/dist/buildManager.d.ts.map +1 -0
  20. package/dist/buildManager.js +74 -0
  21. package/dist/cliHelpers.d.ts +5 -0
  22. package/dist/cliHelpers.d.ts.map +1 -0
  23. package/dist/cliHelpers.js +52 -0
  24. package/dist/createServer.d.ts +30 -0
  25. package/dist/createServer.d.ts.map +1 -0
  26. package/dist/createServer.js +142 -0
  27. package/dist/createServerHelpers.d.ts +39 -0
  28. package/dist/createServerHelpers.d.ts.map +1 -0
  29. package/dist/createServerHelpers.js +101 -0
  30. package/dist/fastify.d.ts +14 -0
  31. package/dist/fastify.d.ts.map +1 -0
  32. package/dist/fastify.js +88 -0
  33. package/dist/logFormatter/ambient.d.js +1 -0
  34. package/dist/logFormatter/bin.d.ts +2 -0
  35. package/dist/logFormatter/bin.d.ts.map +1 -0
  36. package/dist/logFormatter/bin.js +369 -0
  37. package/dist/logFormatter/formatters.d.ts +30 -0
  38. package/dist/logFormatter/formatters.d.ts.map +1 -0
  39. package/dist/logFormatter/formatters.js +275 -0
  40. package/dist/logFormatter/index.d.ts +2 -0
  41. package/dist/logFormatter/index.d.ts.map +1 -0
  42. package/dist/logFormatter/index.js +184 -0
  43. package/dist/plugins/api.d.ts +13 -0
  44. package/dist/plugins/api.d.ts.map +1 -0
  45. package/dist/plugins/api.js +77 -0
  46. package/dist/plugins/graphql.d.ts +10 -0
  47. package/dist/plugins/graphql.d.ts.map +1 -0
  48. package/dist/plugins/graphql.js +115 -0
  49. package/dist/plugins/lambdaLoader.d.ts +23 -0
  50. package/dist/plugins/lambdaLoader.d.ts.map +1 -0
  51. package/dist/plugins/lambdaLoader.js +116 -0
  52. package/dist/requestHandlers/awsLambdaFastify.d.ts +5 -0
  53. package/dist/requestHandlers/awsLambdaFastify.d.ts.map +1 -0
  54. package/dist/requestHandlers/awsLambdaFastify.js +103 -0
  55. package/dist/requestHandlers/utils.d.ts +26 -0
  56. package/dist/requestHandlers/utils.d.ts.map +1 -0
  57. package/dist/requestHandlers/utils.js +55 -0
  58. package/dist/serverManager.d.ts +8 -0
  59. package/dist/serverManager.d.ts.map +1 -0
  60. package/dist/serverManager.js +138 -0
  61. package/dist/types.d.ts +19 -0
  62. package/dist/types.d.ts.map +1 -0
  63. package/dist/watch.d.ts +2 -0
  64. package/dist/watch.d.ts.map +1 -0
  65. package/dist/watch.js +274 -0
  66. package/package.json +77 -0
package/dist/watch.js ADDED
@@ -0,0 +1,274 @@
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 __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
18
+ // If the importer is in node compatibility mode or this is not an ESM
19
+ // file that has been converted to a CommonJS file using a Babel-
20
+ // compatible transform (i.e. "__esModule" has not been set), then set
21
+ // "default" to the CommonJS "module.exports" for node compatibility.
22
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
23
+ mod
24
+ ));
25
+
26
+ // src/watch.ts
27
+ var import_path2 = __toESM(require("path"));
28
+ var import_chalk2 = __toESM(require("chalk"));
29
+ var import_chokidar = __toESM(require("chokidar"));
30
+ var import_dotenv = __toESM(require("dotenv"));
31
+ var import_api = require("@redmix/internal/dist/build/api");
32
+ var import_validateSchema = require("@redmix/internal/dist/validateSchema");
33
+ var import_project_config2 = require("@redmix/project-config");
34
+
35
+ // src/buildManager.ts
36
+ var import_lodash = require("lodash");
37
+ var BuildManager = class {
38
+ shouldRebuild;
39
+ shouldClean;
40
+ debouncedBuild;
41
+ buildFn;
42
+ constructor(buildFn) {
43
+ this.shouldRebuild = true;
44
+ this.shouldClean = false;
45
+ this.buildFn = buildFn;
46
+ this.debouncedBuild = (0, import_lodash.debounce)(
47
+ async (options) => {
48
+ try {
49
+ await this.buildFn({
50
+ ...options,
51
+ rebuild: this.shouldRebuild,
52
+ clean: this.shouldClean
53
+ });
54
+ } finally {
55
+ this.shouldRebuild = true;
56
+ this.shouldClean = false;
57
+ }
58
+ },
59
+ // We want to delay execution when multiple files are modified on the filesystem,
60
+ // this usually happens when running RedwoodJS generator commands.
61
+ // Local writes are very fast, but writes in e2e environments are not,
62
+ // so allow the default to be adjusted with an env-var.
63
+ //
64
+ process.env.RWJS_DELAY_RESTART ? parseInt(process.env.RWJS_DELAY_RESTART, 10) : 500
65
+ );
66
+ }
67
+ // Wrapper method to handle options and set precedence flags.
68
+ // If we ever see a `rebuild: false` option while debouncing, we never want to rebuild.
69
+ // If we ever see a `clean: true` option, we always want to clean.
70
+ async run(options) {
71
+ if (options.rebuild === false) {
72
+ this.shouldRebuild = false;
73
+ }
74
+ if (options.clean) {
75
+ this.shouldClean = true;
76
+ }
77
+ await this.debouncedBuild(options);
78
+ }
79
+ cancelScheduledBuild() {
80
+ this.debouncedBuild.cancel();
81
+ }
82
+ };
83
+
84
+ // src/serverManager.ts
85
+ var import_child_process = require("child_process");
86
+ var import_fs = __toESM(require("fs"));
87
+ var import_path = __toESM(require("path"));
88
+ var import_chalk = __toESM(require("chalk"));
89
+ var import_yargs = __toESM(require("yargs"));
90
+ var import_helpers = require("yargs/helpers");
91
+ var import_project_config = require("@redmix/project-config");
92
+ var argv = (0, import_yargs.default)((0, import_helpers.hideBin)(process.argv)).option("debugPort", {
93
+ description: "Port on which to expose API server debugger",
94
+ type: "number",
95
+ alias: ["debug-port", "dp"]
96
+ }).option("port", {
97
+ description: "The port to listen at",
98
+ type: "number",
99
+ alias: "p"
100
+ }).parseSync();
101
+ var rwjsPaths = (0, import_project_config.getPaths)();
102
+ var ServerManager = class {
103
+ httpServerProcess = null;
104
+ async startApiServer() {
105
+ const forkOpts = {
106
+ execArgv: process.execArgv
107
+ };
108
+ if ((0, import_project_config.getConfig)().experimental.opentelemetry.enabled) {
109
+ const opentelemetrySDKScriptPath = import_path.default.join(
110
+ rwjsPaths.api.dist,
111
+ "opentelemetry.js"
112
+ );
113
+ const opentelemetrySDKScriptPathRelative = import_path.default.relative(
114
+ rwjsPaths.base,
115
+ opentelemetrySDKScriptPath
116
+ );
117
+ console.log(
118
+ `Setting up OpenTelemetry using the setup file: ${opentelemetrySDKScriptPathRelative}`
119
+ );
120
+ if (import_fs.default.existsSync(opentelemetrySDKScriptPath)) {
121
+ forkOpts.execArgv = forkOpts.execArgv.concat([
122
+ `--require=${opentelemetrySDKScriptPath}`
123
+ ]);
124
+ } else {
125
+ console.error(
126
+ `OpenTelemetry setup file does not exist at ${opentelemetrySDKScriptPathRelative}`
127
+ );
128
+ }
129
+ }
130
+ const debugPort = argv["debug-port"];
131
+ if (debugPort) {
132
+ forkOpts.execArgv = forkOpts.execArgv.concat([`--inspect=${debugPort}`]);
133
+ }
134
+ const port = argv.port ?? (0, import_project_config.getConfig)().api.port;
135
+ const serverFile = (0, import_project_config.resolveFile)(`${rwjsPaths.api.dist}/server`);
136
+ if (serverFile) {
137
+ this.httpServerProcess = (0, import_child_process.fork)(
138
+ serverFile,
139
+ ["--apiPort", port.toString()],
140
+ forkOpts
141
+ );
142
+ } else {
143
+ this.httpServerProcess = (0, import_child_process.fork)(
144
+ import_path.default.join(__dirname, "bin.js"),
145
+ ["api", "--port", port.toString()],
146
+ forkOpts
147
+ );
148
+ }
149
+ }
150
+ async restartApiServer() {
151
+ await this.killApiServer();
152
+ await this.startApiServer();
153
+ }
154
+ async killApiServer() {
155
+ if (!this.httpServerProcess) {
156
+ return;
157
+ }
158
+ await new Promise((resolve) => {
159
+ console.log(import_chalk.default.yellow("Shutting down API server."));
160
+ const cleanup = () => {
161
+ this.httpServerProcess?.removeAllListeners("exit");
162
+ clearTimeout(forceKillTimeout);
163
+ };
164
+ this.httpServerProcess?.on("exit", () => {
165
+ console.log(import_chalk.default.yellow("API server exited."));
166
+ cleanup();
167
+ resolve();
168
+ });
169
+ const forceKillTimeout = setTimeout(() => {
170
+ console.log(
171
+ import_chalk.default.yellow(
172
+ "API server did not exit within 2 seconds, forcefully closing it."
173
+ )
174
+ );
175
+ cleanup();
176
+ this.httpServerProcess?.kill("SIGKILL");
177
+ resolve();
178
+ }, 2e3);
179
+ this.httpServerProcess?.kill();
180
+ });
181
+ }
182
+ };
183
+ var serverManager = new ServerManager();
184
+
185
+ // src/watch.ts
186
+ var rwjsPaths2 = (0, import_project_config2.getPaths)();
187
+ if (!process.env.REDWOOD_ENV_FILES_LOADED) {
188
+ import_dotenv.default.config({
189
+ path: import_path2.default.join(rwjsPaths2.base, ".env"),
190
+ // @ts-expect-error The types for dotenv-defaults are using an outdated version of dotenv
191
+ defaults: import_path2.default.join(rwjsPaths2.base, ".env.defaults"),
192
+ multiline: true
193
+ });
194
+ process.env.REDWOOD_ENV_FILES_LOADED = "true";
195
+ }
196
+ async function buildAndServe(options) {
197
+ const buildTs = Date.now();
198
+ console.log(import_chalk2.default.dim.italic("Building..."));
199
+ if (options.clean) {
200
+ await (0, import_api.cleanApiBuild)();
201
+ }
202
+ if (options.rebuild) {
203
+ await (0, import_api.rebuildApi)();
204
+ } else {
205
+ await (0, import_api.buildApi)();
206
+ }
207
+ await serverManager.restartApiServer();
208
+ console.log(import_chalk2.default.dim.italic("Took " + (Date.now() - buildTs) + " ms"));
209
+ }
210
+ var buildManager = new BuildManager(buildAndServe);
211
+ async function validateSdls() {
212
+ try {
213
+ await (0, import_validateSchema.loadAndValidateSdls)();
214
+ return true;
215
+ } catch (e) {
216
+ serverManager.killApiServer();
217
+ console.error(
218
+ import_chalk2.default.redBright(`[GQL Server Error] - Schema validation failed`)
219
+ );
220
+ console.error(import_chalk2.default.red(e?.message));
221
+ console.error(import_chalk2.default.redBright("-".repeat(40)));
222
+ buildManager.cancelScheduledBuild();
223
+ return false;
224
+ }
225
+ }
226
+ var IGNORED_API_PATHS = [
227
+ "api/dist",
228
+ // use this, because using rwjsPaths.api.dist seems to not ignore on first build
229
+ rwjsPaths2.api.types,
230
+ rwjsPaths2.api.db
231
+ ].map((path3) => (0, import_project_config2.ensurePosixPath)(path3));
232
+ import_chokidar.default.watch([rwjsPaths2.api.src], {
233
+ persistent: true,
234
+ ignoreInitial: true,
235
+ ignored: (file) => {
236
+ const x = file.includes("node_modules") || IGNORED_API_PATHS.some((ignoredPath) => file.includes(ignoredPath)) || [
237
+ ".DS_Store",
238
+ ".db",
239
+ ".sqlite",
240
+ "-journal",
241
+ ".test.js",
242
+ ".test.ts",
243
+ ".scenarios.ts",
244
+ ".scenarios.js",
245
+ ".d.ts",
246
+ ".log"
247
+ ].some((ext) => file.endsWith(ext));
248
+ return x;
249
+ }
250
+ }).on("ready", async () => {
251
+ await buildManager.run({ clean: true, rebuild: false });
252
+ await validateSdls();
253
+ }).on("all", async (eventName, filePath) => {
254
+ if (eventName === "addDir" && filePath === rwjsPaths2.api.base) {
255
+ return;
256
+ }
257
+ if (eventName) {
258
+ if (filePath.includes(".sdl")) {
259
+ const isValid = await validateSdls();
260
+ if (!isValid) {
261
+ return;
262
+ }
263
+ }
264
+ }
265
+ console.log(
266
+ import_chalk2.default.dim(`[${eventName}] ${filePath.replace(rwjsPaths2.api.base, "")}`)
267
+ );
268
+ buildManager.cancelScheduledBuild();
269
+ if (eventName === "add" || eventName === "unlink") {
270
+ await buildManager.run({ rebuild: false });
271
+ } else {
272
+ await buildManager.run({ rebuild: true });
273
+ }
274
+ });
package/package.json ADDED
@@ -0,0 +1,77 @@
1
+ {
2
+ "name": "@redmix/api-server",
3
+ "version": "0.0.1",
4
+ "description": "Redmix's HTTP server for Serverless Functions",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/redmix-run/redmix.git",
8
+ "directory": "packages/api-server"
9
+ },
10
+ "license": "MIT",
11
+ "main": "./dist/createServer.js",
12
+ "types": "./dist/createServer.d.ts",
13
+ "bin": {
14
+ "rw-api-server-watch": "./dist/watch.js",
15
+ "rw-log-formatter": "./dist/logFormatter/bin.js",
16
+ "rw-server": "./dist/bin.js"
17
+ },
18
+ "files": [
19
+ "dist"
20
+ ],
21
+ "scripts": {
22
+ "build": "tsx ./build.mts && yarn build:types",
23
+ "build:pack": "yarn pack -o redmix-api-server.tgz",
24
+ "build:types": "tsc --build --verbose tsconfig.build.json",
25
+ "build:watch": "nodemon --watch src --ext \"js,jsx,ts,tsx\" --ignore dist --exec \"yarn build && yarn fix:permissions\"",
26
+ "fix:permissions": "chmod +x dist/index.js; chmod +x dist/watch.js",
27
+ "prepublishOnly": "NODE_ENV=production yarn build",
28
+ "test": "vitest run",
29
+ "test:watch": "vitest watch"
30
+ },
31
+ "dependencies": {
32
+ "@fastify/multipart": "8.3.1",
33
+ "@fastify/url-data": "5.4.0",
34
+ "@redmix/context": "0.0.1",
35
+ "@redmix/fastify-web": "0.0.1",
36
+ "@redmix/internal": "0.0.1",
37
+ "@redmix/project-config": "0.0.1",
38
+ "@redmix/web-server": "0.0.1",
39
+ "chalk": "4.1.2",
40
+ "chokidar": "3.6.0",
41
+ "dotenv-defaults": "5.0.2",
42
+ "fast-glob": "3.3.2",
43
+ "fast-json-parse": "1.0.3",
44
+ "fastify": "4.28.1",
45
+ "fastify-raw-body": "4.3.0",
46
+ "lodash": "4.17.21",
47
+ "pretty-bytes": "5.6.0",
48
+ "pretty-ms": "7.0.1",
49
+ "qs": "6.13.0",
50
+ "split2": "4.2.0",
51
+ "yargs": "17.7.2"
52
+ },
53
+ "devDependencies": {
54
+ "@redmix/framework-tools": "0.0.1",
55
+ "@types/aws-lambda": "8.10.145",
56
+ "@types/lodash": "4.17.15",
57
+ "@types/qs": "6.9.16",
58
+ "@types/split2": "4.2.3",
59
+ "@types/yargs": "17.0.33",
60
+ "pino-abstract-transport": "1.2.0",
61
+ "tsx": "4.19.3",
62
+ "typescript": "5.6.2",
63
+ "vitest": "2.1.9"
64
+ },
65
+ "peerDependencies": {
66
+ "@redmix/graphql-server": "workspace:*"
67
+ },
68
+ "peerDependenciesMeta": {
69
+ "@redmix/graphql-server": {
70
+ "optional": true
71
+ }
72
+ },
73
+ "publishConfig": {
74
+ "access": "public"
75
+ },
76
+ "gitHead": "688027c97502c500ebbede9cdc7cc51545a8dcf3"
77
+ }