@interactive-inc/flume 0.1.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
2
+
3
+ Copyright (c) 2026 Interactive Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,244 @@
1
+ # open-flume
2
+
3
+ Unified notification listener for Discord, Slack, and GitHub. Raw WebSocket + `fetch` + Zod. Zero SDK dependencies — no `discord.js`, no `@slack/bolt`, no `@slack/web-api`. Runs on Node 18+, Bun, Deno, Cloudflare Workers, or any environment with global `fetch` and `WebSocket`.
4
+
5
+ ```
6
+ Discord ─┐
7
+ Slack ─┼──→ FlumeSource.start(handler) ──→ FlumeEvent
8
+ GitHub ─┘
9
+ ```
10
+
11
+ Flume only **receives**. It opens the WebSocket / polls the API, parses the payload with Zod, and hands you a typed event. Sending replies is out of scope — bring your own HTTP call.
12
+
13
+ ## Install
14
+
15
+ ```bash
16
+ npm add @interactive-inc/flume
17
+ ```
18
+
19
+ ## Quick start
20
+
21
+ ```ts
22
+ import { Flume, createFlumeDefaultDeps } from "@interactive-inc/flume"
23
+
24
+ const flume = new Flume({
25
+ deps: createFlumeDefaultDeps(),
26
+ onLog: (log) => console.log(`[${log.level}] ${log.source}/${log.action}: ${log.message}`),
27
+ onStatus: (status, detail) => console.log(`status: ${status} ${detail ?? ""}`),
28
+ reconnect: { maxAttempts: 10, baseDelay: 1000, maxDelay: 30000 },
29
+ })
30
+
31
+ const discord = flume.discord({ token: process.env.DISCORD_BOT_TOKEN! })
32
+ const slack = flume.slack({ appToken: process.env.SLACK_APP_TOKEN! })
33
+ const github = flume.github({ token: process.env.GITHUB_TOKEN!, pollInterval: 60 })
34
+
35
+ await discord.start((event) => {
36
+ console.log(event.source, event.type, event.meta)
37
+ })
38
+
39
+ await slack.start((event) => { /* ... */ })
40
+ await github.start((event) => { /* ... */ })
41
+ ```
42
+
43
+ ## Direct source usage
44
+
45
+ Skip the `Flume` container and instantiate a source directly:
46
+
47
+ ```ts
48
+ import { FlumeDiscordSource, createFlumeDefaultDeps } from "@interactive-inc/flume"
49
+
50
+ const source = new FlumeDiscordSource({
51
+ token: process.env.DISCORD_BOT_TOKEN!,
52
+ deps: createFlumeDefaultDeps(),
53
+ reconnect: true,
54
+ onLog: (log) => console.log(log),
55
+ })
56
+
57
+ await source.start((event) => { /* ... */ })
58
+ ```
59
+
60
+ ## Sub-entries
61
+
62
+ Each source is also importable on its own. Use this to keep Slack's Socket Mode code out of a Discord-only bundle, or vice versa.
63
+
64
+ | sub-entry | exports |
65
+ |-----------------------|-------------------------------------------------------------------|
66
+ | `@interactive-inc/flume/discord` | `FlumeDiscordSource` |
67
+ | `@interactive-inc/flume/slack` | `FlumeSlackSource` |
68
+ | `@interactive-inc/flume/github` | `FlumeGitHubSource` |
69
+
70
+ ```ts
71
+ import { FlumeSlackSource } from "@interactive-inc/flume/slack"
72
+ import { FlumeDiscordSource } from "@interactive-inc/flume/discord"
73
+ import { FlumeGitHubSource } from "@interactive-inc/flume/github"
74
+ ```
75
+
76
+ The root entry exports the `Flume` container, every source class, every protocol class (`FlumeDiscordGateway`, `FlumeSlackSocketMode`, `FlumeGitHubPoller`), `FlumeReconnector`, `FlumeLogger`, `createFlumeDefaultDeps`, every Zod schema, every error class, and all public types.
77
+
78
+ ## Event shape
79
+
80
+ Every source emits the same `FlumeEvent`:
81
+
82
+ ```ts
83
+ type FlumeSourceName = "discord" | "slack" | "github"
84
+
85
+ type FlumeEvent = {
86
+ source: FlumeSourceName
87
+ type: string
88
+ data: unknown
89
+ meta: Record<string, string>
90
+ receivedAt: number
91
+ }
92
+ ```
93
+
94
+ `meta` is flat string keys tailored per source:
95
+
96
+ | source | meta keys |
97
+ |---------|------------------------------------------------------------------------|
98
+ | discord | `event_type`, `channel_id`, `guild_id`, `user_id` |
99
+ | slack | `event_type`, `channel_id`, `user_id`, `thread_ts`, `slack_event_type` |
100
+ | github | `event_type`, `reason`, `subject_type`, `repository`, `thread_id` |
101
+
102
+ `data` is the raw parsed payload (Zod-validated at the protocol boundary).
103
+
104
+ ## Observability
105
+
106
+ Flume never calls a third-party service. Every internal action is reported through the `onLog` callback you pass to `Flume` or each source — there are no silent paths.
107
+
108
+ ```ts
109
+ type FlumeLog = {
110
+ level: "debug" | "info" | "warn" | "error"
111
+ source: string
112
+ action: string
113
+ message: string
114
+ error?: Error
115
+ detail?: Record<string, unknown>
116
+ timestamp: number
117
+ }
118
+ ```
119
+
120
+ What gets logged:
121
+
122
+ - **HTTP boundary** — every request URL, response status, and parsed body shape (`http.request` / `http.response` / `http.body`). Slack's `apps.connections.open` call and GitHub's poll request both emit these.
123
+ - **WebSocket boundary** — every inbound frame (`ws.recv`), every outbound frame (`ws.send` / `ws.sent`), with a 200-byte preview and total byte count. Discord op / t / s is decoded into structured `detail`.
124
+ - **Protocol lifecycle** — Discord HELLO / READY / RESUMED / RECONNECT / INVALID_SESSION / HEARTBEAT / HEARTBEAT_ACK, Slack hello / disconnect / envelope ack, GitHub bootstrap / fresh / idle.
125
+ - **Parse failures** — any Zod schema mismatch emits a `warn` with the field paths and messages. Dropped GitHub notifications carry a per-item `parse.skip` and a `parse.summary` count. Slack envelopes that don't match `FlumeSlackEnvelopeSchema` emit `envelope.parse-fail` with the incoming `type` and the issues. Discord frames with an unknown op emit `ws.unknown-op`.
126
+ - **Reconnect** — `reconnect.scheduled` (with delay in ms), `reconnect.exhausted` (with attempt count), `reconnect.reset` (on successful connect after retries), `reconnect.cancel` (on stop).
127
+ - **Status transitions** — `status` action with `previous → next`.
128
+ - **Errors** — `level: "error"` carries the `error` field so you can `captureException` in your handler.
129
+
130
+ Route it anywhere — Sentry, Datadog, `console`, a file — your choice:
131
+
132
+ ```ts
133
+ onLog: (log) => {
134
+ if (log.level === "error" && log.error) {
135
+ Sentry.captureException(log.error, { tags: { source: log.source, action: log.action } })
136
+ }
137
+ if (log.level === "debug" && !process.env.FLUME_DEBUG) return
138
+ console.log(`[${log.level}] ${log.source}/${log.action}: ${log.message}`)
139
+ }
140
+ ```
141
+
142
+ ## Reconnect
143
+
144
+ `reconnect` accepts `true`, an options object, or is omitted (no reconnect).
145
+
146
+ ```ts
147
+ reconnect: {
148
+ maxAttempts: 10,
149
+ baseDelay: 1000, // first backoff
150
+ maxDelay: 30000, // backoff cap
151
+ }
152
+ ```
153
+
154
+ Exponential backoff with jitter. Discord resumes the session when possible — the session id and resume URL are carried across reconnects via `FlumeDiscordGatewaySession`.
155
+
156
+ ## Status
157
+
158
+ ```ts
159
+ onStatus: (status: "disconnected" | "connecting" | "connected" | "reconnecting", detail?: string) => void
160
+ ```
161
+
162
+ GitHub populates `detail` with the failure reason (e.g. `"HTTP 500"`, `"network error"`). Discord and Slack leave `detail` undefined.
163
+
164
+ ## Cancellation
165
+
166
+ Pass an `AbortSignal` to any source. Aborting prevents start and triggers `stop()`:
167
+
168
+ ```ts
169
+ const controller = new AbortController()
170
+ const flume = new Flume({ signal: controller.signal, deps: createFlumeDefaultDeps() })
171
+ // ...
172
+ controller.abort() // all sources stop
173
+ ```
174
+
175
+ ## Dependency injection
176
+
177
+ Every IO boundary (`fetch`, `WebSocket`, `now`, `random`, timers) lives in `FlumeRuntimeDeps`. The default factory wraps the global equivalents; tests pass mocks:
178
+
179
+ ```ts
180
+ import { createFlumeDefaultDeps } from "@interactive-inc/flume"
181
+
182
+ const deps = {
183
+ ...createFlumeDefaultDeps(),
184
+ fetch: mockFetch,
185
+ WebSocket: MockWebSocket,
186
+ now: () => 1_000,
187
+ random: () => 0.5,
188
+ }
189
+ ```
190
+
191
+ `Flume` accepts `Partial<FlumeRuntimeDeps>` and merges over the defaults — override only what you need.
192
+
193
+ ## Errors
194
+
195
+ Flume does not throw on protocol/network failures. Connection methods return `T | Error` and you check `instanceof`:
196
+
197
+ - `FlumeConnectionError` — WebSocket closed before ready
198
+ - `FlumeHttpError` — HTTP call returned an error payload (e.g. Slack `ok: false`)
199
+ - `FlumeParseError` — Unparseable WebSocket frame
200
+
201
+ Internal handler exceptions are caught and logged (never rethrown into the protocol loop).
202
+
203
+ ## Supported sources
204
+
205
+ | source | transport | auth |
206
+ |---------|----------------------------------|-----------------------------------------------|
207
+ | Discord | Gateway WebSocket v10 (JSON) | bot token |
208
+ | Slack | Socket Mode WebSocket | app token (`botToken` optional, for future) |
209
+ | GitHub | REST polling `/notifications` | personal access token |
210
+
211
+ GitHub also exposes `gh auth token` if you want to reuse the `gh` CLI's session:
212
+
213
+ ```ts
214
+ import { execSync } from "node:child_process"
215
+ const token = execSync("gh auth token").toString().trim()
216
+ const github = flume.github({ token })
217
+ ```
218
+
219
+ ## Module layout
220
+
221
+ - `Flume` — DI container; `.discord()` / `.slack()` / `.github()` build sources with shared deps
222
+ - `FlumeDiscordSource` / `FlumeSlackSource` / `FlumeGitHubSource` — high-level sources
223
+ - `FlumeDiscordGateway` / `FlumeSlackSocketMode` / `FlumeGitHubPoller` — protocol layer
224
+ - `FlumeReconnector` — exponential backoff with jitter
225
+ - `FlumeLogger` — structured log emitter (feeds `onLog`)
226
+ - `FlumeRuntimeDeps` — IO boundary port
227
+ - Zod schemas for every external boundary: `FlumeGatewayMessageSchema`, `FlumeSlackEnvelopeSchema`, `FlumeSlackConnectionResponseSchema`, `FlumeGitHubNotificationSchema`
228
+
229
+ ## Development
230
+
231
+ ```bash
232
+ bun install
233
+ bunx vp pack # build dist/
234
+ bunx tsc --noEmit # typecheck
235
+ bunx vitest run # tests
236
+ bunx vp lint # lint
237
+ bunx vp fmt # format
238
+ ```
239
+
240
+ The library itself is runtime-agnostic. The dev toolchain (build / test / lint) uses Bun + `vite-plus` + `vitest`, but the published `dist/` is plain ESM with TypeScript declarations and runs on Node 18+, Bun, Deno, Cloudflare Workers, or modern browsers.
241
+
242
+ ## License
243
+
244
+ MIT © Interactive Inc.