@ovineko/spa-guard-fastify 0.0.1-alpha-18
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/dist/fastify/index.d.ts +33 -0
- package/dist/index.js +75 -0
- package/package.json +47 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { FastifyPluginAsync, FastifyReply, FastifyRequest } from "fastify";
|
|
2
|
+
import type { BeaconSchema } from "@ovineko/spa-guard/schema";
|
|
3
|
+
export { BeaconError } from "@ovineko/spa-guard";
|
|
4
|
+
export interface BeaconHandlerResult {
|
|
5
|
+
/**
|
|
6
|
+
* If true, skips default logging behavior
|
|
7
|
+
*/
|
|
8
|
+
skipDefaultLog?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface FastifySPAGuardOptions {
|
|
11
|
+
/**
|
|
12
|
+
* Custom handler for beacon data
|
|
13
|
+
* @param beacon - Parsed beacon data
|
|
14
|
+
* @param request - Fastify request object
|
|
15
|
+
* @param reply - Fastify reply object
|
|
16
|
+
* @returns Object with options to control default behavior
|
|
17
|
+
*/
|
|
18
|
+
onBeacon?: (beacon: BeaconSchema, request: FastifyRequest, reply: FastifyReply) => BeaconHandlerResult | Promise<BeaconHandlerResult | void> | void;
|
|
19
|
+
/**
|
|
20
|
+
* Custom handler for invalid/unknown beacon data
|
|
21
|
+
* @param body - Raw body data
|
|
22
|
+
* @param request - Fastify request object
|
|
23
|
+
* @param reply - Fastify reply object
|
|
24
|
+
* @returns Object with options to control default behavior
|
|
25
|
+
*/
|
|
26
|
+
onUnknownBeacon?: (body: unknown, request: FastifyRequest, reply: FastifyReply) => BeaconHandlerResult | Promise<BeaconHandlerResult | void> | void;
|
|
27
|
+
/**
|
|
28
|
+
* The route path for the beacon endpoint
|
|
29
|
+
* @example "/api/beacon"
|
|
30
|
+
*/
|
|
31
|
+
path: string;
|
|
32
|
+
}
|
|
33
|
+
export declare const fastifySPAGuard: FastifyPluginAsync<FastifySPAGuardOptions>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// src/fastify/index.ts
|
|
2
|
+
import fp from "fastify-plugin";
|
|
3
|
+
import { BeaconError } from "@ovineko/spa-guard";
|
|
4
|
+
import { logMessage } from "@ovineko/spa-guard/_internal";
|
|
5
|
+
import { parseBeacon } from "@ovineko/spa-guard/schema/parse";
|
|
6
|
+
|
|
7
|
+
// package.json
|
|
8
|
+
var name = "@ovineko/spa-guard-fastify";
|
|
9
|
+
|
|
10
|
+
// src/fastify/index.ts
|
|
11
|
+
var parseStringBody = (body) => {
|
|
12
|
+
try {
|
|
13
|
+
return JSON.parse(body);
|
|
14
|
+
} catch {
|
|
15
|
+
return body;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
var handleBeaconRequest = async (params) => {
|
|
19
|
+
const { body, options, reply, request } = params;
|
|
20
|
+
let beacon;
|
|
21
|
+
try {
|
|
22
|
+
beacon = parseBeacon(body);
|
|
23
|
+
} catch {
|
|
24
|
+
if (options.onUnknownBeacon) {
|
|
25
|
+
const result = await options.onUnknownBeacon(body, request, reply);
|
|
26
|
+
if (!result?.skipDefaultLog) {
|
|
27
|
+
request.log.warn({ bodyType: typeof body }, logMessage("Unknown beacon format"));
|
|
28
|
+
}
|
|
29
|
+
} else {
|
|
30
|
+
request.log.warn({ bodyType: typeof body }, logMessage("Unknown beacon format"));
|
|
31
|
+
}
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const logPayload = {
|
|
35
|
+
...beacon.appName && { appName: beacon.appName },
|
|
36
|
+
errorMessage: beacon.errorMessage,
|
|
37
|
+
eventMessage: beacon.eventMessage,
|
|
38
|
+
eventName: beacon.eventName,
|
|
39
|
+
serialized: beacon.serialized
|
|
40
|
+
};
|
|
41
|
+
if (options.onBeacon) {
|
|
42
|
+
const result = await options.onBeacon(beacon, request, reply);
|
|
43
|
+
if (!result?.skipDefaultLog) {
|
|
44
|
+
request.log.info(logPayload, logMessage("Beacon received"));
|
|
45
|
+
}
|
|
46
|
+
} else {
|
|
47
|
+
request.log.info(logPayload, logMessage("Beacon received"));
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
var fastifySPAGuardPlugin = async (fastify, options) => {
|
|
51
|
+
const { onBeacon, onUnknownBeacon, path } = options;
|
|
52
|
+
fastify.post(path, async (request, reply) => {
|
|
53
|
+
if (typeof request.body !== "string") {
|
|
54
|
+
request.log.warn(
|
|
55
|
+
{ bodyType: typeof request.body },
|
|
56
|
+
logMessage("Invalid beacon body type, expected string")
|
|
57
|
+
);
|
|
58
|
+
return reply.status(400).send({ error: "Invalid body type" });
|
|
59
|
+
}
|
|
60
|
+
const body = parseStringBody(request.body);
|
|
61
|
+
await handleBeaconRequest({ body, options: { onBeacon, onUnknownBeacon }, reply, request });
|
|
62
|
+
if (!reply.sent) {
|
|
63
|
+
return reply.status(200).send({ success: true });
|
|
64
|
+
}
|
|
65
|
+
return reply;
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
var fastifySPAGuard = fp(fastifySPAGuardPlugin, {
|
|
69
|
+
fastify: "5.x || 4.x",
|
|
70
|
+
name: `${name}/fastify`
|
|
71
|
+
});
|
|
72
|
+
export {
|
|
73
|
+
BeaconError,
|
|
74
|
+
fastifySPAGuard
|
|
75
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ovineko/spa-guard-fastify",
|
|
3
|
+
"version": "0.0.1-alpha-18",
|
|
4
|
+
"description": "Fastify plugin for spa-guard beacon endpoint",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"spa",
|
|
7
|
+
"fastify",
|
|
8
|
+
"beacon",
|
|
9
|
+
"monitoring"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/ovineko/ovineko/tree/main/spa-guard/fastify",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/ovineko/ovineko/issues"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/ovineko/ovineko.git",
|
|
18
|
+
"directory": "spa-guard/fastify"
|
|
19
|
+
},
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"author": "Alexander Svinarev <shibanet0@gmail.com> (shibanet0.com)",
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"type": "module",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/fastify/index.d.ts",
|
|
27
|
+
"default": "./dist/fastify/index.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist",
|
|
32
|
+
"README.md"
|
|
33
|
+
],
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@ovineko/spa-guard": "0.0.1-alpha-18"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"fastify": "^5 || ^4",
|
|
39
|
+
"fastify-plugin": "^5 || ^4"
|
|
40
|
+
},
|
|
41
|
+
"engines": {
|
|
42
|
+
"node": ">=22.15.0"
|
|
43
|
+
},
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public"
|
|
46
|
+
}
|
|
47
|
+
}
|