@polygonlabs/servercore 1.0.0-dev.28 → 1.0.0-dev.29
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.
@@ -0,0 +1,79 @@
|
|
1
|
+
import { handleResponse, handleError } from "./response_handler";
|
2
|
+
import { ApiError } from "../errors";
|
3
|
+
class HealthCheck {
|
4
|
+
urls;
|
5
|
+
constructor(urls) {
|
6
|
+
this.urls = urls;
|
7
|
+
}
|
8
|
+
async checkHealth() {
|
9
|
+
try {
|
10
|
+
const results = await Promise.all(
|
11
|
+
this.urls.map(async (url) => {
|
12
|
+
const response = await fetch(`${url}`);
|
13
|
+
if (!response.ok) {
|
14
|
+
throw new Error(`Health check failed for ${url}`);
|
15
|
+
}
|
16
|
+
return response;
|
17
|
+
})
|
18
|
+
);
|
19
|
+
if (results.every((res) => res.ok)) {
|
20
|
+
return "ok";
|
21
|
+
}
|
22
|
+
throw new Error("One or more URLs failed the health check.");
|
23
|
+
} catch (error) {
|
24
|
+
const failedUrls = this.urls.filter(async (url) => {
|
25
|
+
const response = await fetch(`${url}`);
|
26
|
+
return !response.ok;
|
27
|
+
});
|
28
|
+
throw new ApiError(
|
29
|
+
`Health check failed for the following URLs: ${failedUrls.join(
|
30
|
+
", "
|
31
|
+
)}`
|
32
|
+
);
|
33
|
+
}
|
34
|
+
}
|
35
|
+
}
|
36
|
+
function createResponseContext(c) {
|
37
|
+
c.status = c.status || 200;
|
38
|
+
return {
|
39
|
+
status: (statusCode) => {
|
40
|
+
c.status = statusCode;
|
41
|
+
return createResponseContext(c);
|
42
|
+
},
|
43
|
+
json: (body) => {
|
44
|
+
return new Response(JSON.stringify(body), {
|
45
|
+
status: c.status,
|
46
|
+
headers: { "Content-Type": "application/json" }
|
47
|
+
});
|
48
|
+
}
|
49
|
+
};
|
50
|
+
}
|
51
|
+
function setupHealthCheckServer(urls, serverPort) {
|
52
|
+
const healthCheck = new HealthCheck(urls);
|
53
|
+
Bun.serve({
|
54
|
+
port: serverPort,
|
55
|
+
routes: {
|
56
|
+
"/health-check": {
|
57
|
+
GET: async (c) => {
|
58
|
+
try {
|
59
|
+
await healthCheck.checkHealth();
|
60
|
+
return handleResponse(createResponseContext(c), "ok");
|
61
|
+
} catch (error) {
|
62
|
+
return handleError(
|
63
|
+
createResponseContext(c),
|
64
|
+
error
|
65
|
+
);
|
66
|
+
}
|
67
|
+
}
|
68
|
+
}
|
69
|
+
}
|
70
|
+
});
|
71
|
+
console.log(
|
72
|
+
`Health check server running on http://localhost:${serverPort}/health-check`
|
73
|
+
);
|
74
|
+
}
|
75
|
+
export {
|
76
|
+
HealthCheck,
|
77
|
+
setupHealthCheckServer
|
78
|
+
};
|
79
|
+
//# sourceMappingURL=health-check.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/api/health-check.ts"],"sourcesContent":["import { handleResponse, handleError } from \"./response_handler\";\nimport type { ResponseContext } from \"../types\";\nimport { ApiError } from \"../errors\";\n\nexport class HealthCheck {\n private urls: string[];\n\n constructor(urls: string[]) {\n this.urls = urls;\n }\n\n async checkHealth(): Promise<string> {\n try {\n const results = await Promise.all(\n this.urls.map(async (url) => {\n const response = await fetch(`${url}`);\n if (!response.ok) {\n throw new Error(`Health check failed for ${url}`);\n }\n return response;\n })\n );\n\n if (results.every((res) => res.ok)) {\n return \"ok\";\n }\n throw new Error(\"One or more URLs failed the health check.\");\n } catch (error) {\n const failedUrls = this.urls.filter(async (url) => {\n const response = await fetch(`${url}`);\n return !response.ok;\n });\n throw new ApiError(\n `Health check failed for the following URLs: ${failedUrls.join(\n \", \"\n )}`\n );\n }\n }\n}\n\n// Utility to convert Bun context to ResponseContext\nfunction createResponseContext(c: any): ResponseContext {\n c.status = c.status || 200; // Default status to 200 if undefined\n return {\n status: (statusCode: number) => {\n c.status = statusCode;\n return createResponseContext(c); // Return the updated context\n },\n json: (body: any) => {\n return new Response(JSON.stringify(body), {\n status: c.status,\n headers: { \"Content-Type\": \"application/json\" },\n });\n },\n };\n}\n\n// Setup Bun server with health check route\nexport function setupHealthCheckServer(\n urls: string[],\n serverPort: number\n): void {\n const healthCheck = new HealthCheck(urls);\n\n Bun.serve({\n port: serverPort,\n routes: {\n \"/health-check\": {\n GET: async (c: any) => {\n try {\n await healthCheck.checkHealth();\n return handleResponse(createResponseContext(c), \"ok\");\n } catch (error) {\n return handleError(\n createResponseContext(c),\n error as ApiError\n );\n }\n },\n },\n },\n });\n\n console.log(\n `Health check server running on http://localhost:${serverPort}/health-check`\n );\n}\n"],"mappings":"AAAA,SAAS,gBAAgB,mBAAmB;AAE5C,SAAS,gBAAgB;AAElB,MAAM,YAAY;AAAA,EACb;AAAA,EAER,YAAY,MAAgB;AACxB,SAAK,OAAO;AAAA,EAChB;AAAA,EAEA,MAAM,cAA+B;AACjC,QAAI;AACA,YAAM,UAAU,MAAM,QAAQ;AAAA,QAC1B,KAAK,KAAK,IAAI,OAAO,QAAQ;AACzB,gBAAM,WAAW,MAAM,MAAM,GAAG,GAAG,EAAE;AACrC,cAAI,CAAC,SAAS,IAAI;AACd,kBAAM,IAAI,MAAM,2BAA2B,GAAG,EAAE;AAAA,UACpD;AACA,iBAAO;AAAA,QACX,CAAC;AAAA,MACL;AAEA,UAAI,QAAQ,MAAM,CAAC,QAAQ,IAAI,EAAE,GAAG;AAChC,eAAO;AAAA,MACX;AACA,YAAM,IAAI,MAAM,2CAA2C;AAAA,IAC/D,SAAS,OAAO;AACZ,YAAM,aAAa,KAAK,KAAK,OAAO,OAAO,QAAQ;AAC/C,cAAM,WAAW,MAAM,MAAM,GAAG,GAAG,EAAE;AACrC,eAAO,CAAC,SAAS;AAAA,MACrB,CAAC;AACD,YAAM,IAAI;AAAA,QACN,+CAA+C,WAAW;AAAA,UACtD;AAAA,QACJ,CAAC;AAAA,MACL;AAAA,IACJ;AAAA,EACJ;AACJ;AAGA,SAAS,sBAAsB,GAAyB;AACpD,IAAE,SAAS,EAAE,UAAU;AACvB,SAAO;AAAA,IACH,QAAQ,CAAC,eAAuB;AAC5B,QAAE,SAAS;AACX,aAAO,sBAAsB,CAAC;AAAA,IAClC;AAAA,IACA,MAAM,CAAC,SAAc;AACjB,aAAO,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG;AAAA,QACtC,QAAQ,EAAE;AAAA,QACV,SAAS,EAAE,gBAAgB,mBAAmB;AAAA,MAClD,CAAC;AAAA,IACL;AAAA,EACJ;AACJ;AAGO,SAAS,uBACZ,MACA,YACI;AACJ,QAAM,cAAc,IAAI,YAAY,IAAI;AAExC,MAAI,MAAM;AAAA,IACN,MAAM;AAAA,IACN,QAAQ;AAAA,MACJ,iBAAiB;AAAA,QACb,KAAK,OAAO,MAAW;AACnB,cAAI;AACA,kBAAM,YAAY,YAAY;AAC9B,mBAAO,eAAe,sBAAsB,CAAC,GAAG,IAAI;AAAA,UACxD,SAAS,OAAO;AACZ,mBAAO;AAAA,cACH,sBAAsB,CAAC;AAAA,cACvB;AAAA,YACJ;AAAA,UACJ;AAAA,QACJ;AAAA,MACJ;AAAA,IACJ;AAAA,EACJ,CAAC;AAED,UAAQ;AAAA,IACJ,mDAAmD,UAAU;AAAA,EACjE;AACJ;","names":[]}
|
package/dist/api/index.d.ts
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
export { handleError, handleResponse } from './response_handler.js';
|
2
2
|
export { validateBody, validateParams, validateQuery } from './zod_utils.js';
|
3
|
+
export { HealthCheck, setupHealthCheckServer } from './health-check.js';
|
3
4
|
import '../errors/api_errors.js';
|
4
5
|
import '../errors/base_error.js';
|
5
6
|
import '../errors/database_errors.js';
|
package/dist/api/index.js
CHANGED
package/dist/api/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/api/index.ts"],"sourcesContent":["export * from \"./response_handler\";\nexport * from \"./zod_utils\";\n"],"mappings":"AAAA,cAAc;AACd,cAAc;","names":[]}
|
1
|
+
{"version":3,"sources":["../../src/api/index.ts"],"sourcesContent":["export * from \"./response_handler\";\nexport * from \"./zod_utils\";\nexport * from \"./health-check\";\n"],"mappings":"AAAA,cAAc;AACd,cAAc;AACd,cAAc;","names":[]}
|
package/dist/index.d.ts
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
export { handleError, handleResponse } from './api/response_handler.js';
|
2
2
|
export { validateBody, validateParams, validateQuery } from './api/zod_utils.js';
|
3
|
+
export { HealthCheck, setupHealthCheckServer } from './api/health-check.js';
|
3
4
|
export { Logger } from './logger/logger.js';
|
4
5
|
export { errorCodes } from './constants/error_codes.js';
|
5
6
|
export { httpResposneCodes } from './constants/http_success_codes.js';
|