@arkstack/common 0.1.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/README.md +8 -0
- package/dist/app.d.ts +22 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +95 -0
- package/dist/index.js.map +1 -0
- package/package.json +38 -0
package/README.md
ADDED
package/dist/app.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
declare global {
|
|
2
|
+
interface String {
|
|
3
|
+
/**
|
|
4
|
+
* Converts the string to title case.
|
|
5
|
+
*/
|
|
6
|
+
titleCase (): string;
|
|
7
|
+
/**
|
|
8
|
+
* Converts the string to camel case.
|
|
9
|
+
*/
|
|
10
|
+
camelCase (): string;
|
|
11
|
+
/**
|
|
12
|
+
* Truncates the string to a specified length while preserving words
|
|
13
|
+
* and adds a suffix if necessary.
|
|
14
|
+
*
|
|
15
|
+
* @param len Length of the string
|
|
16
|
+
* @param suffix Suffix to add to the string
|
|
17
|
+
*/
|
|
18
|
+
truncate (len: number, suffix?: string): string;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/// <reference path="./app.d.ts" />
|
|
2
|
+
//#region src/lifecycle.d.ts
|
|
3
|
+
declare const bindGracefulShutdown: (shutdown: () => Promise<void> | void) => void;
|
|
4
|
+
//#endregion
|
|
5
|
+
//#region src/network.d.ts
|
|
6
|
+
declare const bootWithDetectedPort: (boot: (port: number) => Promise<void>, preferredPort?: number) => Promise<void>;
|
|
7
|
+
declare const buildHtmlErrorResponse: ({
|
|
8
|
+
message,
|
|
9
|
+
stack,
|
|
10
|
+
title,
|
|
11
|
+
code
|
|
12
|
+
}: {
|
|
13
|
+
message?: string;
|
|
14
|
+
stack?: string;
|
|
15
|
+
code?: number;
|
|
16
|
+
title?: string;
|
|
17
|
+
}) => string;
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/prototypes.d.ts
|
|
20
|
+
declare const loadPrototypes: () => void;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { bindGracefulShutdown, bootWithDetectedPort, buildHtmlErrorResponse, loadPrototypes };
|
|
23
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { detect } from "detect-port";
|
|
2
|
+
|
|
3
|
+
//#region src/lifecycle.ts
|
|
4
|
+
const bindGracefulShutdown = (shutdown) => {
|
|
5
|
+
[
|
|
6
|
+
"SIGINT",
|
|
7
|
+
"SIGTERM",
|
|
8
|
+
"SIGQUIT"
|
|
9
|
+
].forEach((signal) => {
|
|
10
|
+
process.on(signal, async () => {
|
|
11
|
+
await shutdown();
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region src/network.ts
|
|
18
|
+
const bootWithDetectedPort = async (boot, preferredPort = 3e3) => {
|
|
19
|
+
await boot(await detect(preferredPort));
|
|
20
|
+
};
|
|
21
|
+
const buildHtmlErrorResponse = ({ message = "An unexpected error occurred.", stack, title, code = 500 }) => {
|
|
22
|
+
title = {
|
|
23
|
+
400: "Bad Request",
|
|
24
|
+
401: "Unauthorized",
|
|
25
|
+
403: "Forbidden",
|
|
26
|
+
404: "Not Found",
|
|
27
|
+
500: "Internal Server Error",
|
|
28
|
+
502: "Bad Gateway",
|
|
29
|
+
503: "Service Unavailable",
|
|
30
|
+
504: "Gateway Timeout"
|
|
31
|
+
}[code] || title || "Error";
|
|
32
|
+
return `
|
|
33
|
+
<html>
|
|
34
|
+
<head>
|
|
35
|
+
<title>${code} | ${title}</title>
|
|
36
|
+
<style>
|
|
37
|
+
body {
|
|
38
|
+
font-family: Arial, sans-serif;
|
|
39
|
+
background-color: #f8f8f8;
|
|
40
|
+
color: #333;
|
|
41
|
+
padding: 20px;
|
|
42
|
+
}
|
|
43
|
+
h1 {
|
|
44
|
+
color: #e74c3c;
|
|
45
|
+
}
|
|
46
|
+
h3 {
|
|
47
|
+
color: #3498db;
|
|
48
|
+
}
|
|
49
|
+
p {
|
|
50
|
+
color: #555;
|
|
51
|
+
}
|
|
52
|
+
pre {
|
|
53
|
+
background-color: #eee;
|
|
54
|
+
padding: 10px;
|
|
55
|
+
border-radius: 5px;
|
|
56
|
+
overflow-x: auto;
|
|
57
|
+
}
|
|
58
|
+
</style>
|
|
59
|
+
</head>
|
|
60
|
+
<body>
|
|
61
|
+
<h1>${code}</h1>
|
|
62
|
+
<h3>${title}</h3>
|
|
63
|
+
<p>${message}</p>
|
|
64
|
+
${stack ? `<h2>Stack Trace:</h2><pre>${stack}</pre>` : ""}
|
|
65
|
+
</body>
|
|
66
|
+
</html>
|
|
67
|
+
`;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
//#endregion
|
|
71
|
+
//#region src/prototypes.ts
|
|
72
|
+
const loadPrototypes = () => {
|
|
73
|
+
String.prototype.titleCase = function() {
|
|
74
|
+
return this.toLowerCase().replace(/_/g, " ").replace(/-/g, " ").replace(/(?:^|\s)\w/g, function(match) {
|
|
75
|
+
return match.toUpperCase();
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
String.prototype.camelCase = function() {
|
|
79
|
+
return this.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, function(match, index) {
|
|
80
|
+
if (+match === 0) return "";
|
|
81
|
+
return index === 0 ? match.toLowerCase() : match.toUpperCase();
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
String.prototype.truncate = function(len = 20, suffix = "...") {
|
|
85
|
+
if (this.length <= len) return this.toString();
|
|
86
|
+
const truncated = this.substring(0, len);
|
|
87
|
+
const lastSpaceIndex = truncated.lastIndexOf(" ");
|
|
88
|
+
if (lastSpaceIndex > 0) return truncated.substring(0, lastSpaceIndex) + suffix;
|
|
89
|
+
return truncated + suffix;
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
//#endregion
|
|
94
|
+
export { bindGracefulShutdown, bootWithDetectedPort, buildHtmlErrorResponse, loadPrototypes };
|
|
95
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/lifecycle.ts","../src/network.ts","../src/prototypes.ts"],"sourcesContent":["export const bindGracefulShutdown = (shutdown: () => Promise<void> | void) => {\n [\"SIGINT\", \"SIGTERM\", \"SIGQUIT\"].forEach((signal) => {\n process.on(signal, async () => {\n await shutdown();\n });\n });\n};\n","import { detect } from \"detect-port\";\n\nexport const bootWithDetectedPort = async (\n boot: (port: number) => Promise<void>,\n preferredPort: number = 3000,\n) => {\n const port = await detect(preferredPort);\n await boot(port);\n};\n\n\nexport const buildHtmlErrorResponse = ({\n message = \"An unexpected error occurred.\",\n stack,\n title,\n code = 500,\n}: {\n message?: string;\n stack?: string;\n code?: number;\n title?: string;\n}) => {\n const titleMap: Record<number, string> = {\n 400: \"Bad Request\",\n 401: \"Unauthorized\",\n 403: \"Forbidden\",\n 404: \"Not Found\",\n 500: \"Internal Server Error\",\n 502: \"Bad Gateway\",\n 503: \"Service Unavailable\",\n 504: \"Gateway Timeout\",\n };\n\n title = titleMap[code] || title || \"Error\";\n\n return `\n <html>\n <head>\n <title>${code} | ${title}</title>\n <style>\n body {\n font-family: Arial, sans-serif;\n background-color: #f8f8f8;\n color: #333;\n padding: 20px;\n }\n h1 {\n color: #e74c3c;\n }\n h3 {\n color: #3498db;\n }\n p {\n color: #555;\n }\n pre {\n background-color: #eee;\n padding: 10px;\n border-radius: 5px;\n overflow-x: auto;\n }\n </style>\n </head>\n <body>\n <h1>${code}</h1>\n <h3>${title}</h3>\n <p>${message}</p>\n ${stack ? `<h2>Stack Trace:</h2><pre>${stack}</pre>` : \"\"}\n </body>\n </html>\n `;\n};","export const loadPrototypes = () => {\n String.prototype.titleCase = function () {\n return this.toLowerCase()\n .replace(/_/g, ' ')\n .replace(/-/g, ' ')\n .replace(/(?:^|\\s)\\w/g, function (match) {\n return match.toUpperCase();\n });\n };\n\n String.prototype.camelCase = function () {\n return this.replace(/(?:^\\w|[A-Z]|\\b\\w|\\s+)/g, function (match, index) {\n if (+match === 0) return \"\"; // or if (/\\s+/.test(match)) for white spaces\n return index === 0 ? match.toLowerCase() : match.toUpperCase();\n });\n }\n\n String.prototype.truncate = function (len: number = 20, suffix: string = '...') {\n if (this.length <= len) {\n return this.toString();\n }\n\n const truncated = this.substring(0, len);\n const lastSpaceIndex = truncated.lastIndexOf(' ');\n if (lastSpaceIndex > 0) {\n return truncated.substring(0, lastSpaceIndex) + suffix;\n }\n return truncated + suffix;\n }\n} \n"],"mappings":";;;AAAA,MAAa,wBAAwB,aAAyC;AAC5E;EAAC;EAAU;EAAW;EAAU,CAAC,SAAS,WAAW;AACnD,UAAQ,GAAG,QAAQ,YAAY;AAC7B,SAAM,UAAU;IAChB;GACF;;;;;ACHJ,MAAa,uBAAuB,OAClC,MACA,gBAAwB,QACrB;AAEH,OAAM,KADO,MAAM,OAAO,cAAc,CACxB;;AAIlB,MAAa,0BAA0B,EACrC,UAAU,iCACV,OACA,OACA,OAAO,UAMH;AAYJ,SAXyC;EACvC,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACN,CAEgB,SAAS,SAAS;AAEnC,QAAO;;;iBAGQ,KAAK,KAAK,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;cA0BnB,KAAK;cACL,MAAM;aACP,QAAQ;UACX,QAAQ,6BAA6B,MAAM,UAAU,GAAG;;;;;;;;ACnElE,MAAa,uBAAuB;AAChC,QAAO,UAAU,YAAY,WAAY;AACrC,SAAO,KAAK,aAAa,CACpB,QAAQ,MAAM,IAAI,CAClB,QAAQ,MAAM,IAAI,CAClB,QAAQ,eAAe,SAAU,OAAO;AACrC,UAAO,MAAM,aAAa;IAC5B;;AAGV,QAAO,UAAU,YAAY,WAAY;AACrC,SAAO,KAAK,QAAQ,2BAA2B,SAAU,OAAO,OAAO;AACnE,OAAI,CAAC,UAAU,EAAG,QAAO;AACzB,UAAO,UAAU,IAAI,MAAM,aAAa,GAAG,MAAM,aAAa;IAChE;;AAGN,QAAO,UAAU,WAAW,SAAU,MAAc,IAAI,SAAiB,OAAO;AAC5E,MAAI,KAAK,UAAU,IACf,QAAO,KAAK,UAAU;EAG1B,MAAM,YAAY,KAAK,UAAU,GAAG,IAAI;EACxC,MAAM,iBAAiB,UAAU,YAAY,IAAI;AACjD,MAAI,iBAAiB,EACjB,QAAO,UAAU,UAAU,GAAG,eAAe,GAAG;AAEpD,SAAO,YAAY"}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@arkstack/common",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Common package for Arkstack providing common implementations of core Arkstack features such as routing, middleware, and database integration.",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/arkstack-hq/arkstack.git",
|
|
9
|
+
"directory": "packages/common"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"common",
|
|
13
|
+
"utilities",
|
|
14
|
+
"helpers",
|
|
15
|
+
"logging",
|
|
16
|
+
"configuration",
|
|
17
|
+
"error-handling",
|
|
18
|
+
"validation",
|
|
19
|
+
"arkstack"
|
|
20
|
+
],
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"exports": {
|
|
28
|
+
".": "./dist/index.js",
|
|
29
|
+
"./package.json": "./package.json"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"detect-port": "^2.1.0"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsdown --config-loader unconfig",
|
|
36
|
+
"version:patch": "pnpm version patch --no-git-tag-version"
|
|
37
|
+
}
|
|
38
|
+
}
|