@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.
- package/LICENSE +21 -0
- package/README.md +64 -0
- package/dist/apiCLIConfig.d.ts +6 -0
- package/dist/apiCLIConfig.d.ts.map +1 -0
- package/dist/apiCLIConfig.js +71 -0
- package/dist/apiCLIConfigHandler.d.ts +3 -0
- package/dist/apiCLIConfigHandler.d.ts.map +1 -0
- package/dist/apiCLIConfigHandler.js +68 -0
- package/dist/bin.d.ts +2 -0
- package/dist/bin.d.ts.map +1 -0
- package/dist/bin.js +850 -0
- package/dist/bothCLIConfig.d.ts +6 -0
- package/dist/bothCLIConfig.d.ts.map +1 -0
- package/dist/bothCLIConfig.js +76 -0
- package/dist/bothCLIConfigHandler.d.ts +3 -0
- package/dist/bothCLIConfigHandler.d.ts.map +1 -0
- package/dist/bothCLIConfigHandler.js +107 -0
- package/dist/buildManager.d.ts +15 -0
- package/dist/buildManager.d.ts.map +1 -0
- package/dist/buildManager.js +74 -0
- package/dist/cliHelpers.d.ts +5 -0
- package/dist/cliHelpers.d.ts.map +1 -0
- package/dist/cliHelpers.js +52 -0
- package/dist/createServer.d.ts +30 -0
- package/dist/createServer.d.ts.map +1 -0
- package/dist/createServer.js +142 -0
- package/dist/createServerHelpers.d.ts +39 -0
- package/dist/createServerHelpers.d.ts.map +1 -0
- package/dist/createServerHelpers.js +101 -0
- package/dist/fastify.d.ts +14 -0
- package/dist/fastify.d.ts.map +1 -0
- package/dist/fastify.js +88 -0
- package/dist/logFormatter/ambient.d.js +1 -0
- package/dist/logFormatter/bin.d.ts +2 -0
- package/dist/logFormatter/bin.d.ts.map +1 -0
- package/dist/logFormatter/bin.js +369 -0
- package/dist/logFormatter/formatters.d.ts +30 -0
- package/dist/logFormatter/formatters.d.ts.map +1 -0
- package/dist/logFormatter/formatters.js +275 -0
- package/dist/logFormatter/index.d.ts +2 -0
- package/dist/logFormatter/index.d.ts.map +1 -0
- package/dist/logFormatter/index.js +184 -0
- package/dist/plugins/api.d.ts +13 -0
- package/dist/plugins/api.d.ts.map +1 -0
- package/dist/plugins/api.js +77 -0
- package/dist/plugins/graphql.d.ts +10 -0
- package/dist/plugins/graphql.d.ts.map +1 -0
- package/dist/plugins/graphql.js +115 -0
- package/dist/plugins/lambdaLoader.d.ts +23 -0
- package/dist/plugins/lambdaLoader.d.ts.map +1 -0
- package/dist/plugins/lambdaLoader.js +116 -0
- package/dist/requestHandlers/awsLambdaFastify.d.ts +5 -0
- package/dist/requestHandlers/awsLambdaFastify.d.ts.map +1 -0
- package/dist/requestHandlers/awsLambdaFastify.js +103 -0
- package/dist/requestHandlers/utils.d.ts +26 -0
- package/dist/requestHandlers/utils.d.ts.map +1 -0
- package/dist/requestHandlers/utils.js +55 -0
- package/dist/serverManager.d.ts +8 -0
- package/dist/serverManager.d.ts.map +1 -0
- package/dist/serverManager.js +138 -0
- package/dist/types.d.ts +19 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/watch.d.ts +2 -0
- package/dist/watch.d.ts.map +1 -0
- package/dist/watch.js +274 -0
- package/package.json +77 -0
@@ -0,0 +1,23 @@
|
|
1
|
+
import type { Handler } from 'aws-lambda';
|
2
|
+
import type { Options as FastGlobOptions } from 'fast-glob';
|
3
|
+
import type { FastifyReply, FastifyRequest, RequestGenericInterface } from 'fastify';
|
4
|
+
export type Lambdas = Record<string, Handler>;
|
5
|
+
export declare const LAMBDA_FUNCTIONS: Lambdas;
|
6
|
+
export declare const setLambdaFunctions: (foundFunctions: string[]) => Promise<void>;
|
7
|
+
type LoadFunctionsFromDistOptions = {
|
8
|
+
fastGlobOptions?: FastGlobOptions;
|
9
|
+
};
|
10
|
+
export declare const loadFunctionsFromDist: (options?: LoadFunctionsFromDistOptions) => Promise<void>;
|
11
|
+
interface LambdaHandlerRequest extends RequestGenericInterface {
|
12
|
+
Params: {
|
13
|
+
routeName: string;
|
14
|
+
};
|
15
|
+
}
|
16
|
+
/**
|
17
|
+
This will take a fastify request
|
18
|
+
Then convert it to a lambdaEvent, and pass it to the the appropriate handler for the routeName
|
19
|
+
The LAMBDA_FUNCTIONS lookup has been populated already by this point
|
20
|
+
**/
|
21
|
+
export declare const lambdaRequestHandler: (req: FastifyRequest<LambdaHandlerRequest>, reply: FastifyReply) => Promise<void>;
|
22
|
+
export {};
|
23
|
+
//# sourceMappingURL=lambdaLoader.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"lambdaLoader.d.ts","sourceRoot":"","sources":["../../src/plugins/lambdaLoader.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAGzC,OAAO,KAAK,EAAE,OAAO,IAAI,eAAe,EAAE,MAAM,WAAW,CAAA;AAC3D,OAAO,KAAK,EACV,YAAY,EACZ,cAAc,EACd,uBAAuB,EACxB,MAAM,SAAS,CAAA;AAOhB,MAAM,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;AAC7C,eAAO,MAAM,gBAAgB,EAAE,OAAY,CAAA;AAI3C,eAAO,MAAM,kBAAkB,mBAA0B,MAAM,EAAE,kBA8BhE,CAAA;AAED,KAAK,4BAA4B,GAAG;IAClC,eAAe,CAAC,EAAE,eAAe,CAAA;CAClC,CAAA;AAGD,eAAO,MAAM,qBAAqB,aACvB,4BAA4B,kBActC,CAAA;AAgBD,UAAU,oBAAqB,SAAQ,uBAAuB;IAC5D,MAAM,EAAE;QACN,SAAS,EAAE,MAAM,CAAA;KAClB,CAAA;CACF;AAED;;;;IAII;AACJ,eAAO,MAAM,oBAAoB,QAC1B,cAAc,CAAC,oBAAoB,CAAC,SAClC,YAAY,kBAsBpB,CAAA"}
|
@@ -0,0 +1,116 @@
|
|
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 lambdaLoader_exports = {};
|
30
|
+
__export(lambdaLoader_exports, {
|
31
|
+
LAMBDA_FUNCTIONS: () => LAMBDA_FUNCTIONS,
|
32
|
+
lambdaRequestHandler: () => lambdaRequestHandler,
|
33
|
+
loadFunctionsFromDist: () => loadFunctionsFromDist,
|
34
|
+
setLambdaFunctions: () => setLambdaFunctions
|
35
|
+
});
|
36
|
+
module.exports = __toCommonJS(lambdaLoader_exports);
|
37
|
+
var import_path = __toESM(require("path"));
|
38
|
+
var import_chalk = __toESM(require("chalk"));
|
39
|
+
var import_fast_glob = __toESM(require("fast-glob"));
|
40
|
+
var import_lodash = require("lodash");
|
41
|
+
var import_project_config = require("@redmix/project-config");
|
42
|
+
var import_awsLambdaFastify = require("../requestHandlers/awsLambdaFastify");
|
43
|
+
const LAMBDA_FUNCTIONS = {};
|
44
|
+
const setLambdaFunctions = async (foundFunctions) => {
|
45
|
+
const tsImport = Date.now();
|
46
|
+
console.log(import_chalk.default.dim.italic("Importing Server Functions... "));
|
47
|
+
const imports = foundFunctions.map(async (fnPath) => {
|
48
|
+
const ts = Date.now();
|
49
|
+
const routeName = import_path.default.basename(fnPath).replace(".js", "");
|
50
|
+
const { handler } = await import(`file://${fnPath}`);
|
51
|
+
LAMBDA_FUNCTIONS[routeName] = handler;
|
52
|
+
if (!handler) {
|
53
|
+
console.warn(
|
54
|
+
routeName,
|
55
|
+
"at",
|
56
|
+
fnPath,
|
57
|
+
"does not have a function called handler defined."
|
58
|
+
);
|
59
|
+
}
|
60
|
+
console.log(
|
61
|
+
import_chalk.default.magenta("/" + routeName),
|
62
|
+
import_chalk.default.dim.italic(Date.now() - ts + " ms")
|
63
|
+
);
|
64
|
+
});
|
65
|
+
await Promise.all(imports);
|
66
|
+
console.log(
|
67
|
+
import_chalk.default.dim.italic("...Done importing in " + (Date.now() - tsImport) + " ms")
|
68
|
+
);
|
69
|
+
};
|
70
|
+
const loadFunctionsFromDist = async (options = {}) => {
|
71
|
+
const serverFunctions = findApiDistFunctions(
|
72
|
+
(0, import_project_config.getPaths)().api.base,
|
73
|
+
options?.fastGlobOptions
|
74
|
+
);
|
75
|
+
const i = serverFunctions.findIndex((x) => x.includes("graphql"));
|
76
|
+
if (i >= 0) {
|
77
|
+
const graphQLFn = serverFunctions.splice(i, 1)[0];
|
78
|
+
serverFunctions.unshift(graphQLFn);
|
79
|
+
}
|
80
|
+
await setLambdaFunctions(serverFunctions);
|
81
|
+
};
|
82
|
+
function findApiDistFunctions(cwd = (0, import_project_config.getPaths)().api.base, options = {}) {
|
83
|
+
return import_fast_glob.default.sync("dist/functions/**/*.{ts,js}", {
|
84
|
+
cwd,
|
85
|
+
deep: 2,
|
86
|
+
// We don't support deeply nested api functions, to maximise compatibility with deployment providers
|
87
|
+
absolute: true,
|
88
|
+
...options
|
89
|
+
});
|
90
|
+
}
|
91
|
+
const lambdaRequestHandler = async (req, reply) => {
|
92
|
+
const { routeName } = req.params;
|
93
|
+
if (!LAMBDA_FUNCTIONS[routeName]) {
|
94
|
+
const errorMessage = `Function "${routeName}" was not found.`;
|
95
|
+
req.log.error(errorMessage);
|
96
|
+
reply.status(404);
|
97
|
+
if (process.env.NODE_ENV === "development") {
|
98
|
+
const devError = {
|
99
|
+
error: errorMessage,
|
100
|
+
availableFunctions: Object.keys(LAMBDA_FUNCTIONS)
|
101
|
+
};
|
102
|
+
reply.send(devError);
|
103
|
+
} else {
|
104
|
+
reply.send((0, import_lodash.escape)(errorMessage));
|
105
|
+
}
|
106
|
+
return;
|
107
|
+
}
|
108
|
+
return (0, import_awsLambdaFastify.requestHandler)(req, reply, LAMBDA_FUNCTIONS[routeName]);
|
109
|
+
};
|
110
|
+
// Annotate the CommonJS export names for ESM import in node:
|
111
|
+
0 && (module.exports = {
|
112
|
+
LAMBDA_FUNCTIONS,
|
113
|
+
lambdaRequestHandler,
|
114
|
+
loadFunctionsFromDist,
|
115
|
+
setLambdaFunctions
|
116
|
+
});
|
@@ -0,0 +1,5 @@
|
|
1
|
+
import type { APIGatewayProxyEvent, Handler } from 'aws-lambda';
|
2
|
+
import type { FastifyRequest, FastifyReply } from 'fastify';
|
3
|
+
export declare const lambdaEventForFastifyRequest: (request: FastifyRequest) => APIGatewayProxyEvent;
|
4
|
+
export declare const requestHandler: (req: FastifyRequest, reply: FastifyReply, handler: Handler) => Promise<void>;
|
5
|
+
//# sourceMappingURL=awsLambdaFastify.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"awsLambdaFastify.d.ts","sourceRoot":"","sources":["../../src/requestHandlers/awsLambdaFastify.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,oBAAoB,EACpB,OAAO,EACR,MAAM,YAAY,CAAA;AACnB,OAAO,KAAK,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAK3D,eAAO,MAAM,4BAA4B,YAC9B,cAAc,KACtB,oBAcF,CAAA;AAoCD,eAAO,MAAM,cAAc,QACpB,cAAc,SACZ,YAAY,WACV,OAAO,kBAmCjB,CAAA"}
|
@@ -0,0 +1,103 @@
|
|
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 awsLambdaFastify_exports = {};
|
30
|
+
__export(awsLambdaFastify_exports, {
|
31
|
+
lambdaEventForFastifyRequest: () => lambdaEventForFastifyRequest,
|
32
|
+
requestHandler: () => requestHandler
|
33
|
+
});
|
34
|
+
module.exports = __toCommonJS(awsLambdaFastify_exports);
|
35
|
+
var import_qs = __toESM(require("qs"));
|
36
|
+
var import_utils = require("./utils");
|
37
|
+
const lambdaEventForFastifyRequest = (request) => {
|
38
|
+
return {
|
39
|
+
httpMethod: request.method,
|
40
|
+
headers: request.headers,
|
41
|
+
path: request.urlData("path"),
|
42
|
+
queryStringParameters: import_qs.default.parse(request.url.split(/\?(.+)/)[1]),
|
43
|
+
requestContext: {
|
44
|
+
requestId: request.id,
|
45
|
+
identity: {
|
46
|
+
sourceIp: request.ip
|
47
|
+
}
|
48
|
+
},
|
49
|
+
...(0, import_utils.parseBody)(request.rawBody || "")
|
50
|
+
// adds `body` and `isBase64Encoded`
|
51
|
+
};
|
52
|
+
};
|
53
|
+
const fastifyResponseForLambdaResult = (reply, lambdaResult) => {
|
54
|
+
const {
|
55
|
+
statusCode = 200,
|
56
|
+
headers,
|
57
|
+
body = "",
|
58
|
+
multiValueHeaders
|
59
|
+
} = lambdaResult;
|
60
|
+
const mergedHeaders = (0, import_utils.mergeMultiValueHeaders)(headers, multiValueHeaders);
|
61
|
+
Object.entries(mergedHeaders).forEach(
|
62
|
+
([name, values]) => values.forEach((value) => reply.header(name, value))
|
63
|
+
);
|
64
|
+
reply.status(statusCode);
|
65
|
+
if (lambdaResult.isBase64Encoded) {
|
66
|
+
return reply.send(Buffer.from(body, "base64"));
|
67
|
+
} else {
|
68
|
+
return reply.send(body);
|
69
|
+
}
|
70
|
+
};
|
71
|
+
const fastifyResponseForLambdaError = (req, reply, error) => {
|
72
|
+
req.log.error(error);
|
73
|
+
reply.status(500).send();
|
74
|
+
};
|
75
|
+
const requestHandler = async (req, reply, handler) => {
|
76
|
+
const event = lambdaEventForFastifyRequest(req);
|
77
|
+
const handlerCallback = (reply2) => (error, lambdaResult) => {
|
78
|
+
if (error) {
|
79
|
+
fastifyResponseForLambdaError(req, reply2, error);
|
80
|
+
return;
|
81
|
+
}
|
82
|
+
fastifyResponseForLambdaResult(reply2, lambdaResult);
|
83
|
+
};
|
84
|
+
const handlerPromise = handler(
|
85
|
+
event,
|
86
|
+
// @ts-expect-error - Add support for context: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/0bb210867d16170c4a08d9ce5d132817651a0f80/types/aws-lambda/index.d.ts#L443-L467
|
87
|
+
{},
|
88
|
+
handlerCallback(reply)
|
89
|
+
);
|
90
|
+
if (handlerPromise && typeof handlerPromise.then === "function") {
|
91
|
+
try {
|
92
|
+
const lambdaResponse = await handlerPromise;
|
93
|
+
return fastifyResponseForLambdaResult(reply, lambdaResponse);
|
94
|
+
} catch (error) {
|
95
|
+
return fastifyResponseForLambdaError(req, reply, error);
|
96
|
+
}
|
97
|
+
}
|
98
|
+
};
|
99
|
+
// Annotate the CommonJS export names for ESM import in node:
|
100
|
+
0 && (module.exports = {
|
101
|
+
lambdaEventForFastifyRequest,
|
102
|
+
requestHandler
|
103
|
+
});
|
@@ -0,0 +1,26 @@
|
|
1
|
+
type ParseBodyResult = {
|
2
|
+
body: string;
|
3
|
+
isBase64Encoded: boolean;
|
4
|
+
};
|
5
|
+
type FastifyHeaderValue = string | number | boolean;
|
6
|
+
type FastifyMergedHeaders = {
|
7
|
+
[name: string]: FastifyHeaderValue[];
|
8
|
+
};
|
9
|
+
type FastifyRequestHeader = {
|
10
|
+
[header: string]: FastifyHeaderValue;
|
11
|
+
};
|
12
|
+
type FastifyLambdaHeaders = FastifyRequestHeader | undefined;
|
13
|
+
type FastifyLambdaMultiValueHeaders = FastifyMergedHeaders | undefined;
|
14
|
+
export declare const parseBody: (rawBody: string | Buffer) => ParseBodyResult;
|
15
|
+
/**
|
16
|
+
* `headers` and `multiValueHeaders` are merged into a single object where the
|
17
|
+
* key is the header name in lower-case and the value is a list of values for
|
18
|
+
* that header. Most multi-values are merged into a single value separated by a
|
19
|
+
* semi-colon. The only exception is set-cookie. set-cookie headers should not
|
20
|
+
* be merged, they should be set individually by multiple calls to
|
21
|
+
* reply.header(). See
|
22
|
+
* https://www.fastify.io/docs/latest/Reference/Reply/#set-cookie
|
23
|
+
*/
|
24
|
+
export declare const mergeMultiValueHeaders: (headers: FastifyLambdaHeaders, multiValueHeaders: FastifyLambdaMultiValueHeaders) => FastifyMergedHeaders;
|
25
|
+
export {};
|
26
|
+
//# sourceMappingURL=utils.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/requestHandlers/utils.ts"],"names":[],"mappings":"AAAA,KAAK,eAAe,GAAG;IACrB,IAAI,EAAE,MAAM,CAAA;IACZ,eAAe,EAAE,OAAO,CAAA;CACzB,CAAA;AAED,KAAK,kBAAkB,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAA;AAEnD,KAAK,oBAAoB,GAAG;IAAE,CAAC,IAAI,EAAE,MAAM,GAAG,kBAAkB,EAAE,CAAA;CAAE,CAAA;AAEpE,KAAK,oBAAoB,GAAG;IAAE,CAAC,MAAM,EAAE,MAAM,GAAG,kBAAkB,CAAA;CAAE,CAAA;AAEpE,KAAK,oBAAoB,GAAG,oBAAoB,GAAG,SAAS,CAAA;AAE5D,KAAK,8BAA8B,GAAG,oBAAoB,GAAG,SAAS,CAAA;AAEtE,eAAO,MAAM,SAAS,YAAa,MAAM,GAAG,MAAM,KAAG,eAQpD,CAAA;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,sBAAsB,YACxB,oBAAoB,qBACV,8BAA8B,yBAqBlD,CAAA"}
|
@@ -0,0 +1,55 @@
|
|
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 utils_exports = {};
|
20
|
+
__export(utils_exports, {
|
21
|
+
mergeMultiValueHeaders: () => mergeMultiValueHeaders,
|
22
|
+
parseBody: () => parseBody
|
23
|
+
});
|
24
|
+
module.exports = __toCommonJS(utils_exports);
|
25
|
+
const parseBody = (rawBody) => {
|
26
|
+
if (typeof rawBody === "string") {
|
27
|
+
return { body: rawBody, isBase64Encoded: false };
|
28
|
+
}
|
29
|
+
if (rawBody instanceof Buffer) {
|
30
|
+
return { body: rawBody.toString("base64"), isBase64Encoded: true };
|
31
|
+
}
|
32
|
+
return { body: "", isBase64Encoded: false };
|
33
|
+
};
|
34
|
+
const mergeMultiValueHeaders = (headers, multiValueHeaders) => {
|
35
|
+
const mergedHeaders = Object.entries(
|
36
|
+
headers || {}
|
37
|
+
).reduce((acc, [name, value]) => {
|
38
|
+
acc[name.toLowerCase()] = [value];
|
39
|
+
return acc;
|
40
|
+
}, {});
|
41
|
+
Object.entries(multiValueHeaders || {}).forEach(([headerName, values]) => {
|
42
|
+
const name = headerName.toLowerCase();
|
43
|
+
if (name.toLowerCase() === "set-cookie") {
|
44
|
+
mergedHeaders["set-cookie"] = values;
|
45
|
+
} else {
|
46
|
+
mergedHeaders[name] = [values.join("; ")];
|
47
|
+
}
|
48
|
+
});
|
49
|
+
return mergedHeaders;
|
50
|
+
};
|
51
|
+
// Annotate the CommonJS export names for ESM import in node:
|
52
|
+
0 && (module.exports = {
|
53
|
+
mergeMultiValueHeaders,
|
54
|
+
parseBody
|
55
|
+
});
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"serverManager.d.ts","sourceRoot":"","sources":["../src/serverManager.ts"],"names":[],"mappings":"AA0BA,qBAAa,aAAa;IACxB,OAAO,CAAC,iBAAiB,CAA4B;YAEvC,cAAc;IAuDtB,gBAAgB;IAKhB,aAAa;CAmCpB;AAED,eAAO,MAAM,aAAa,eAAsB,CAAA"}
|
@@ -0,0 +1,138 @@
|
|
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 serverManager_exports = {};
|
30
|
+
__export(serverManager_exports, {
|
31
|
+
ServerManager: () => ServerManager,
|
32
|
+
serverManager: () => serverManager
|
33
|
+
});
|
34
|
+
module.exports = __toCommonJS(serverManager_exports);
|
35
|
+
var import_child_process = require("child_process");
|
36
|
+
var import_fs = __toESM(require("fs"));
|
37
|
+
var import_path = __toESM(require("path"));
|
38
|
+
var import_chalk = __toESM(require("chalk"));
|
39
|
+
var import_yargs = __toESM(require("yargs"));
|
40
|
+
var import_helpers = require("yargs/helpers");
|
41
|
+
var import_project_config = require("@redmix/project-config");
|
42
|
+
const argv = (0, import_yargs.default)((0, import_helpers.hideBin)(process.argv)).option("debugPort", {
|
43
|
+
description: "Port on which to expose API server debugger",
|
44
|
+
type: "number",
|
45
|
+
alias: ["debug-port", "dp"]
|
46
|
+
}).option("port", {
|
47
|
+
description: "The port to listen at",
|
48
|
+
type: "number",
|
49
|
+
alias: "p"
|
50
|
+
}).parseSync();
|
51
|
+
const rwjsPaths = (0, import_project_config.getPaths)();
|
52
|
+
class ServerManager {
|
53
|
+
httpServerProcess = null;
|
54
|
+
async startApiServer() {
|
55
|
+
const forkOpts = {
|
56
|
+
execArgv: process.execArgv
|
57
|
+
};
|
58
|
+
if ((0, import_project_config.getConfig)().experimental.opentelemetry.enabled) {
|
59
|
+
const opentelemetrySDKScriptPath = import_path.default.join(
|
60
|
+
rwjsPaths.api.dist,
|
61
|
+
"opentelemetry.js"
|
62
|
+
);
|
63
|
+
const opentelemetrySDKScriptPathRelative = import_path.default.relative(
|
64
|
+
rwjsPaths.base,
|
65
|
+
opentelemetrySDKScriptPath
|
66
|
+
);
|
67
|
+
console.log(
|
68
|
+
`Setting up OpenTelemetry using the setup file: ${opentelemetrySDKScriptPathRelative}`
|
69
|
+
);
|
70
|
+
if (import_fs.default.existsSync(opentelemetrySDKScriptPath)) {
|
71
|
+
forkOpts.execArgv = forkOpts.execArgv.concat([
|
72
|
+
`--require=${opentelemetrySDKScriptPath}`
|
73
|
+
]);
|
74
|
+
} else {
|
75
|
+
console.error(
|
76
|
+
`OpenTelemetry setup file does not exist at ${opentelemetrySDKScriptPathRelative}`
|
77
|
+
);
|
78
|
+
}
|
79
|
+
}
|
80
|
+
const debugPort = argv["debug-port"];
|
81
|
+
if (debugPort) {
|
82
|
+
forkOpts.execArgv = forkOpts.execArgv.concat([`--inspect=${debugPort}`]);
|
83
|
+
}
|
84
|
+
const port = argv.port ?? (0, import_project_config.getConfig)().api.port;
|
85
|
+
const serverFile = (0, import_project_config.resolveFile)(`${rwjsPaths.api.dist}/server`);
|
86
|
+
if (serverFile) {
|
87
|
+
this.httpServerProcess = (0, import_child_process.fork)(
|
88
|
+
serverFile,
|
89
|
+
["--apiPort", port.toString()],
|
90
|
+
forkOpts
|
91
|
+
);
|
92
|
+
} else {
|
93
|
+
this.httpServerProcess = (0, import_child_process.fork)(
|
94
|
+
import_path.default.join(__dirname, "bin.js"),
|
95
|
+
["api", "--port", port.toString()],
|
96
|
+
forkOpts
|
97
|
+
);
|
98
|
+
}
|
99
|
+
}
|
100
|
+
async restartApiServer() {
|
101
|
+
await this.killApiServer();
|
102
|
+
await this.startApiServer();
|
103
|
+
}
|
104
|
+
async killApiServer() {
|
105
|
+
if (!this.httpServerProcess) {
|
106
|
+
return;
|
107
|
+
}
|
108
|
+
await new Promise((resolve) => {
|
109
|
+
console.log(import_chalk.default.yellow("Shutting down API server."));
|
110
|
+
const cleanup = () => {
|
111
|
+
this.httpServerProcess?.removeAllListeners("exit");
|
112
|
+
clearTimeout(forceKillTimeout);
|
113
|
+
};
|
114
|
+
this.httpServerProcess?.on("exit", () => {
|
115
|
+
console.log(import_chalk.default.yellow("API server exited."));
|
116
|
+
cleanup();
|
117
|
+
resolve();
|
118
|
+
});
|
119
|
+
const forceKillTimeout = setTimeout(() => {
|
120
|
+
console.log(
|
121
|
+
import_chalk.default.yellow(
|
122
|
+
"API server did not exit within 2 seconds, forcefully closing it."
|
123
|
+
)
|
124
|
+
);
|
125
|
+
cleanup();
|
126
|
+
this.httpServerProcess?.kill("SIGKILL");
|
127
|
+
resolve();
|
128
|
+
}, 2e3);
|
129
|
+
this.httpServerProcess?.kill();
|
130
|
+
});
|
131
|
+
}
|
132
|
+
}
|
133
|
+
const serverManager = new ServerManager();
|
134
|
+
// Annotate the CommonJS export names for ESM import in node:
|
135
|
+
0 && (module.exports = {
|
136
|
+
ServerManager,
|
137
|
+
serverManager
|
138
|
+
});
|
package/dist/types.d.ts
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
import type { FastifyInstance } from 'fastify';
|
2
|
+
import type { RedwoodFastifyAPIOptions } from './plugins/api';
|
3
|
+
export type FastifySideConfigFnOptions = {
|
4
|
+
side: 'api' | 'web';
|
5
|
+
};
|
6
|
+
export type FastifySideConfigFn = (fastify: FastifyInstance, options?: FastifySideConfigFnOptions & Pick<RedwoodFastifyAPIOptions['redwood'], 'apiRootPath'>) => Promise<FastifyInstance> | void;
|
7
|
+
export type APIParsedOptions = {
|
8
|
+
port?: number;
|
9
|
+
host?: string;
|
10
|
+
loadEnvFiles?: boolean;
|
11
|
+
} & Omit<RedwoodFastifyAPIOptions['redwood'], 'fastGlobOptions'>;
|
12
|
+
export type BothParsedOptions = {
|
13
|
+
webPort?: number;
|
14
|
+
webHost?: string;
|
15
|
+
apiPort?: number;
|
16
|
+
apiHost?: string;
|
17
|
+
apiRootPath?: string;
|
18
|
+
} & Omit<RedwoodFastifyAPIOptions['redwood'], 'fastGlobOptions'>;
|
19
|
+
//# 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,eAAe,EAAE,MAAM,SAAS,CAAA;AAE9C,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,eAAe,CAAA;AAG7D,MAAM,MAAM,0BAA0B,GAAG;IACvC,IAAI,EAAE,KAAK,GAAG,KAAK,CAAA;CACpB,CAAA;AAED,MAAM,MAAM,mBAAmB,GAAG,CAChC,OAAO,EAAE,eAAe,EACxB,OAAO,CAAC,EAAE,0BAA0B,GAClC,IAAI,CAAC,wBAAwB,CAAC,SAAS,CAAC,EAAE,aAAa,CAAC,KACvD,OAAO,CAAC,eAAe,CAAC,GAAG,IAAI,CAAA;AAEpC,MAAM,MAAM,gBAAgB,GAAG;IAC7B,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,YAAY,CAAC,EAAE,OAAO,CAAA;CACvB,GAAG,IAAI,CAAC,wBAAwB,CAAC,SAAS,CAAC,EAAE,iBAAiB,CAAC,CAAA;AAEhE,MAAM,MAAM,iBAAiB,GAAG;IAC9B,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,WAAW,CAAC,EAAE,MAAM,CAAA;CACrB,GAAG,IAAI,CAAC,wBAAwB,CAAC,SAAS,CAAC,EAAE,iBAAiB,CAAC,CAAA"}
|
package/dist/watch.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"watch.d.ts","sourceRoot":"","sources":["../src/watch.ts"],"names":[],"mappings":""}
|