@spectrum-ts/fastify 8.0.0

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License Copyright (c) 2025 Photon AI
2
+
3
+ Permission is hereby granted,
4
+ free of charge, to any person obtaining a copy of this software and associated
5
+ documentation files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use, copy, modify, merge,
7
+ publish, distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to the
9
+ following conditions:
10
+
11
+ The above copyright notice and this permission notice
12
+ (including the next paragraph) shall be included in all copies or substantial
13
+ portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
16
+ ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
18
+ EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # @spectrum-ts/fastify
2
+
3
+ [Fastify](https://fastify.dev) webhook adapter for [spectrum-ts](https://github.com/photon-hq/spectrum-ts).
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ bun add spectrum-ts @spectrum-ts/fastify fastify
9
+ ```
10
+
11
+ ## Use
12
+
13
+ ```ts
14
+ import { Spectrum } from "spectrum-ts";
15
+ import { spectrum } from "@spectrum-ts/fastify";
16
+ import Fastify from "fastify";
17
+
18
+ const app = await Spectrum({
19
+ webhookSecret: process.env.SPECTRUM_WEBHOOK_SECRET,
20
+ });
21
+
22
+ const server = Fastify();
23
+ // The plugin owns its own raw-body parser in an encapsulated scope.
24
+ server.register(spectrum, {
25
+ app,
26
+ onMessage: async (space, message) => {
27
+ if (message.content.type === "text") {
28
+ await space.send(`echo: ${message.content.text}`);
29
+ }
30
+ },
31
+ });
32
+ // Await listen so a startup failure surfaces instead of going unhandled.
33
+ await server.listen({ port: 3000 });
34
+ ```
35
+
36
+ See the [spectrum-ts documentation](https://photon.codes/spectrum) for the full guide.
@@ -0,0 +1,60 @@
1
+ import { Message, Space, WebhookHandler, WebhookHandler as WebhookHandler$1 } from "@spectrum-ts/core";
2
+ import { FastifyInstance } from "fastify";
3
+
4
+ //#region src/index.d.ts
5
+ /**
6
+ * The minimal structural surface of a Spectrum instance the plugin needs. Kept
7
+ * structural (rather than importing the generic `SpectrumInstance<Providers>`)
8
+ * so the plugin stays decoupled from provider typing; a real instance is
9
+ * assignable via its raw (`{ body, headers }`) webhook overload.
10
+ */
11
+ interface WebhookReceiver {
12
+ webhook(request: {
13
+ body: Uint8Array | ArrayBuffer;
14
+ headers?: Record<string, string>;
15
+ }, handler: WebhookHandler$1): Promise<{
16
+ body: Uint8Array;
17
+ headers: Record<string, string>;
18
+ status: number;
19
+ }>;
20
+ }
21
+ interface SpectrumPluginOptions {
22
+ /** The Spectrum instance returned by `await Spectrum({...})`. */
23
+ app: WebhookReceiver;
24
+ /**
25
+ * Invoked once per inbound message, fire-and-forget after the response — the
26
+ * same `(space, message)` contract as `app.webhook(request, handler)`. Covers
27
+ * both native Spectrum webhooks and fusor webhooks identically.
28
+ */
29
+ onMessage: WebhookHandler$1;
30
+ /**
31
+ * Route the webhook is mounted on.
32
+ *
33
+ * @default "/spectrum/webhook"
34
+ */
35
+ path?: string;
36
+ }
37
+ /**
38
+ * Mount a Spectrum webhook endpoint on a Fastify app.
39
+ *
40
+ * @example
41
+ * ```ts
42
+ * import Fastify from "fastify";
43
+ * import { Spectrum } from "spectrum-ts";
44
+ * import { spectrum } from "@spectrum-ts/fastify";
45
+ *
46
+ * const app = await Spectrum({ ..., webhookSecret: process.env.SPECTRUM_WEBHOOK_SECRET });
47
+ *
48
+ * const server = Fastify();
49
+ * server.register(spectrum, {
50
+ * app,
51
+ * onMessage: async (space, message) => {
52
+ * if (message.content.type === "text") await space.send(`echo: ${message.content.text}`);
53
+ * },
54
+ * });
55
+ * server.listen({ port: 3000 });
56
+ * ```
57
+ */
58
+ declare function spectrum(fastify: FastifyInstance, options: SpectrumPluginOptions): Promise<void>;
59
+ //#endregion
60
+ export { type Message, type Space, SpectrumPluginOptions, type WebhookHandler, spectrum };
package/dist/index.js ADDED
@@ -0,0 +1,55 @@
1
+ //#region src/index.ts
2
+ /**
3
+ * Mount a Spectrum webhook endpoint on a Fastify app.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * import Fastify from "fastify";
8
+ * import { Spectrum } from "spectrum-ts";
9
+ * import { spectrum } from "@spectrum-ts/fastify";
10
+ *
11
+ * const app = await Spectrum({ ..., webhookSecret: process.env.SPECTRUM_WEBHOOK_SECRET });
12
+ *
13
+ * const server = Fastify();
14
+ * server.register(spectrum, {
15
+ * app,
16
+ * onMessage: async (space, message) => {
17
+ * if (message.content.type === "text") await space.send(`echo: ${message.content.text}`);
18
+ * },
19
+ * });
20
+ * server.listen({ port: 3000 });
21
+ * ```
22
+ */
23
+ async function spectrum(fastify, options) {
24
+ const { app, onMessage, path = "/spectrum/webhook" } = options;
25
+ fastify.removeAllContentTypeParsers();
26
+ fastify.addContentTypeParser("*", (_request, payload, done) => {
27
+ const chunks = [];
28
+ payload.on("data", (chunk) => {
29
+ chunks.push(chunk);
30
+ });
31
+ payload.on("end", () => {
32
+ done(null, Buffer.concat(chunks));
33
+ });
34
+ payload.on("error", (err) => {
35
+ done(err);
36
+ });
37
+ });
38
+ fastify.post(path, async (request, reply) => {
39
+ const result = await app.webhook({
40
+ body: request.body,
41
+ headers: normalizeHeaders(request.headers)
42
+ }, onMessage);
43
+ return reply.status(result.status).headers(result.headers).send(Buffer.from(result.body));
44
+ });
45
+ }
46
+ function normalizeHeaders(headers) {
47
+ const normalized = {};
48
+ for (const [key, value] of Object.entries(headers)) {
49
+ if (value === void 0) continue;
50
+ normalized[key] = Array.isArray(value) ? value[0] ?? "" : value;
51
+ }
52
+ return normalized;
53
+ }
54
+ //#endregion
55
+ export { spectrum };
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@spectrum-ts/fastify",
3
+ "version": "8.0.0",
4
+ "description": "Fastify webhook adapter for spectrum-ts.",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/photon-hq/spectrum-ts.git",
8
+ "directory": "packages/fastify"
9
+ },
10
+ "homepage": "https://photon.codes/spectrum",
11
+ "bugs": {
12
+ "url": "https://github.com/photon-hq/spectrum-ts/issues"
13
+ },
14
+ "type": "module",
15
+ "sideEffects": false,
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "default": "./dist/index.js"
23
+ }
24
+ },
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "peerDependencies": {
29
+ "@spectrum-ts/core": "^8.0.0",
30
+ "fastify": "^4 || ^5",
31
+ "typescript": "^5 || ^6.0.0"
32
+ },
33
+ "license": "MIT"
34
+ }