@fastify/sse 0.4.0 → 0.5.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.
@@ -0,0 +1,233 @@
1
+ import fastify, { FastifyInstance, RouteShorthandOptions } from 'fastify'
2
+ import fastifySSE, { SSEPluginOptions, SSEMessage, SSESource, SSEReplyInterface } from '.'
3
+ import { Readable } from 'stream'
4
+ import { expect } from 'tstyche'
5
+
6
+ // Test plugin registration
7
+ const app: FastifyInstance = fastify()
8
+
9
+ // Test plugin options - app.register returns the app instance
10
+ expect(app.register(fastifySSE)).type.toBeAssignableTo<FastifyInstance>()
11
+ expect(app.register(fastifySSE, {})).type.toBeAssignableTo<FastifyInstance>()
12
+ expect(app.register(fastifySSE, {
13
+ heartbeatInterval: 10000
14
+ })).type.toBeAssignableTo<FastifyInstance>()
15
+ expect(app.register(fastifySSE, {
16
+ serializer: (data: any) => JSON.stringify(data)
17
+ })).type.toBeAssignableTo<FastifyInstance>()
18
+ expect(app.register(fastifySSE, {
19
+ heartbeatInterval: 0,
20
+ serializer: (data: any) => String(data)
21
+ })).type.toBeAssignableTo<FastifyInstance>()
22
+
23
+ // Test invalid plugin options
24
+ expect(app.register).type.not.toBeCallableWith(fastifySSE, {
25
+ heartbeatInterval: 'invalid'
26
+ })
27
+
28
+ expect(app.register).type.not.toBeCallableWith(fastifySSE, {
29
+ serializer: 123
30
+ })
31
+
32
+ expect(app.register).type.not.toBeCallableWith(fastifySSE, {
33
+ unknownOption: true
34
+ })
35
+
36
+ // Test route options
37
+ app.get('/sse', { sse: true }, async (request, reply) => {
38
+ expect(reply.sse).type.toBe<SSEReplyInterface>()
39
+ return reply.sse.send({ data: 'test' })
40
+ })
41
+
42
+ app.get('/sse-with-options', {
43
+ sse: {
44
+ heartbeat: true,
45
+ serializer: (data: any) => JSON.stringify(data)
46
+ }
47
+ }, async (request, reply) => {
48
+ expect(reply.sse).type.toBe<SSEReplyInterface>()
49
+ return reply.sse.send({ data: 'test' })
50
+ })
51
+
52
+ // Test SSE message types
53
+ const message: SSEMessage = {
54
+ data: 'test'
55
+ }
56
+
57
+ const fullMessage: SSEMessage = {
58
+ id: '123',
59
+ event: 'custom',
60
+ data: { foo: 'bar' },
61
+ retry: 5000
62
+ }
63
+
64
+ expect(message.id).type.toBe<string | undefined>()
65
+ expect(message.event).type.toBe<string | undefined>()
66
+ expect(message.data).type.toBe<any>()
67
+ expect(message.retry).type.toBe<number | undefined>()
68
+ expect(fullMessage).type.toBeAssignableTo<SSEMessage>()
69
+
70
+ // Test SSE sources
71
+ const stringSource: SSESource = 'hello'
72
+ expect(stringSource).type.toBeAssignableTo<SSESource>()
73
+
74
+ const bufferSource: SSESource = Buffer.from('hello')
75
+ expect(bufferSource).type.toBeAssignableTo<SSESource>()
76
+
77
+ const messageSource: SSESource = { data: 'test' }
78
+ expect(messageSource).type.toBeAssignableTo<SSESource>()
79
+
80
+ const streamSource: SSESource = new Readable()
81
+ expect(streamSource).type.toBeAssignableTo<SSESource>()
82
+
83
+ async function * asyncGenerator (): AsyncIterable<SSEMessage> {
84
+ yield { data: 'test' }
85
+ }
86
+ const asyncIterableSource: SSESource = asyncGenerator()
87
+ expect(asyncIterableSource).type.toBeAssignableTo<SSESource>()
88
+
89
+ // Test SSE reply interface
90
+ app.get('/test-reply', { sse: true }, async (request, reply) => {
91
+ // Test properties
92
+ expect(reply.sse.lastEventId).type.toBe<string | null>()
93
+ expect(reply.sse.isConnected).type.toBe<boolean>()
94
+
95
+ // Test send method with different sources
96
+ expect(reply.sse.send('string')).type.toBe<Promise<void>>()
97
+ expect(reply.sse.send(Buffer.from('buffer'))).type.toBe<Promise<void>>()
98
+ expect(reply.sse.send({ data: 'object' })).type.toBe<Promise<void>>()
99
+ expect(reply.sse.send(new Readable())).type.toBe<Promise<void>>()
100
+ expect(reply.sse.send(asyncGenerator())).type.toBe<Promise<void>>()
101
+
102
+ // Test stream method
103
+ expect(reply.sse.stream()).type.toBe<NodeJS.WritableStream>()
104
+
105
+ // Test keepAlive method
106
+ expect(reply.sse.keepAlive()).type.toBe<void>()
107
+
108
+ // Test close method
109
+ expect(reply.sse.close()).type.toBe<void>()
110
+
111
+ // Test replay method
112
+ expect(reply.sse.replay(async (lastEventId) => {
113
+ expect(lastEventId).type.toBe<string>()
114
+ })).type.toBe<Promise<void>>()
115
+
116
+ // Test onClose method
117
+ expect(reply.sse.onClose(() => {
118
+ console.log('closed')
119
+ })).type.toBe<void>()
120
+ })
121
+
122
+ // Test that reply.sse is available only on sse routes
123
+ app.get('/no-sse', async (request, reply) => {
124
+ // Note: reply.sse is added at runtime by the plugin, so we can't test its absence at compile time
125
+ // This is expected behavior for Fastify plugins that decorate at runtime
126
+ })
127
+
128
+ // Test complex SSE message scenarios
129
+ const complexMessage: SSEMessage = {
130
+ id: '456',
131
+ event: 'update',
132
+ data: {
133
+ nested: {
134
+ property: 'value',
135
+ array: [1, 2, 3]
136
+ }
137
+ },
138
+ retry: 10000
139
+ }
140
+
141
+ expect(complexMessage).type.toBeAssignableTo<SSESource>()
142
+
143
+ // Test async iterator types
144
+ async function * typedAsyncGenerator (): AsyncIterable<string> {
145
+ yield 'test'
146
+ }
147
+
148
+ async function * mixedAsyncGenerator (): AsyncIterable<SSEMessage | string | Buffer> {
149
+ yield { data: 'message' }
150
+ yield 'string'
151
+ yield Buffer.from('buffer')
152
+ }
153
+
154
+ expect(typedAsyncGenerator()).type.toBeAssignableTo<SSESource>()
155
+ expect(mixedAsyncGenerator()).type.toBeAssignableTo<SSESource>()
156
+
157
+ // Test plugin options interface
158
+ const pluginOptions: SSEPluginOptions = {
159
+ heartbeatInterval: 15000,
160
+ serializer: (data) => {
161
+ expect(data).type.toBe<any>()
162
+ return JSON.stringify(data)
163
+ }
164
+ }
165
+ expect(pluginOptions).type.toBeAssignableTo<SSEPluginOptions>()
166
+
167
+ // Test serializer function type
168
+ const customSerializer = (data: any): string => {
169
+ if (typeof data === 'object') {
170
+ return JSON.stringify(data)
171
+ }
172
+ return String(data)
173
+ }
174
+ expect({
175
+ serializer: customSerializer
176
+ }).type.toBeAssignableTo<SSEPluginOptions>()
177
+
178
+ // Test route options with boolean
179
+ const routeOptions1: RouteShorthandOptions = {
180
+ sse: true
181
+ }
182
+ expect(routeOptions1).type.toBeAssignableTo<RouteShorthandOptions>()
183
+
184
+ const routeOptions2: RouteShorthandOptions = {
185
+ sse: false
186
+ }
187
+ expect(routeOptions2).type.toBeAssignableTo<RouteShorthandOptions>()
188
+
189
+ const routeOptions3: RouteShorthandOptions = {
190
+ sse: {
191
+ heartbeat: false,
192
+ serializer: (data) => String(data)
193
+ }
194
+ }
195
+ expect(routeOptions3).type.toBeAssignableTo<RouteShorthandOptions>()
196
+
197
+ // Test invalid route options - these tests verify type checking
198
+ app.get('/invalid', {
199
+ // @ts-expect-error Type '"invalid"' is not assignable to type 'SSERouteOptions'
200
+ sse: 'invalid'
201
+ }, async (request, reply) => { })
202
+ app.get('/invalid2', {
203
+ sse: {
204
+ // @ts-expect-error 'unknownOption' does not exist in type
205
+ unknownOption: true
206
+ }
207
+ }, async (request, reply) => { })
208
+
209
+ // Test valid kind shorthand
210
+ app.get('/only', { sse: 'only' }, async (request, reply) => {
211
+ return reply.sse.send({ data: 'hello' })
212
+ })
213
+ app.get('/dual', { sse: 'dual' }, async (request, reply) => {
214
+ if (reply.sse) return reply.sse.send({ data: 'hello' })
215
+ return { fallback: true }
216
+ })
217
+ app.post('/manual', { sse: 'manual' }, async (request, reply) => {
218
+ const body = request.body as { stream?: boolean }
219
+ if (body.stream) return reply.sse.send({ data: 'hello' })
220
+ return { fallback: true }
221
+ })
222
+
223
+ // Test object form with kind
224
+ app.get('/only-obj', {
225
+ sse: { kind: 'only', heartbeat: false }
226
+ }, async (request, reply) => {
227
+ return reply.sse.send({ data: 'hello' })
228
+ })
229
+ app.post('/manual-obj', {
230
+ sse: { kind: 'manual', heartbeat: false }
231
+ }, async (request, reply) => {
232
+ return reply.sse.send({ data: 'hello' })
233
+ })
@@ -1,21 +0,0 @@
1
- # Number of days of inactivity before an issue becomes stale
2
- daysUntilStale: 15
3
- # Number of days of inactivity before a stale issue is closed
4
- daysUntilClose: 7
5
- # Issues with these labels will never be considered stale
6
- exemptLabels:
7
- - "discussion"
8
- - "feature request"
9
- - "bug"
10
- - "help wanted"
11
- - "plugin suggestion"
12
- - "good first issue"
13
- # Label to use when marking an issue as stale
14
- staleLabel: stale
15
- # Comment to post when marking an issue as stale. Set to `false` to disable
16
- markComment: >
17
- This issue has been automatically marked as stale because it has not had
18
- recent activity. It will be closed if no further activity occurs. Thank you
19
- for your contributions.
20
- # Comment to post when closing a stale issue. Set to `false` to disable
21
- closeComment: false
@@ -1,8 +0,0 @@
1
- comment: |
2
- Hello! Thank you for contributing!
3
- It appears that you have changed the code, but the tests that verify your change are missing. Could you please add them?
4
- fileExtensions:
5
- - '.ts'
6
- - '.js'
7
-
8
- testDir: 'test'
@@ -1,24 +0,0 @@
1
- name: package-manager-ci
2
-
3
- on:
4
- push:
5
- branches:
6
- - main
7
- - next
8
- - 'v*'
9
- paths-ignore:
10
- - 'docs/**'
11
- - '*.md'
12
- pull_request:
13
- paths-ignore:
14
- - 'docs/**'
15
- - '*.md'
16
-
17
- permissions:
18
- contents: read
19
-
20
- jobs:
21
- test:
22
- permissions:
23
- contents: read
24
- uses: fastify/workflows/.github/workflows/plugins-ci-package-manager.yml@v5
@@ -1,192 +0,0 @@
1
- import { expectType, expectError, expectAssignable } from 'tsd'
2
- import fastify, { FastifyInstance, FastifyReply, RouteShorthandOptions } from 'fastify'
3
- import fastifySSE, { SSEPluginOptions, SSEMessage, SSESource, SSEReplyInterface } from '..'
4
- import { Readable } from 'stream'
5
-
6
- // Test plugin registration
7
- const app: FastifyInstance = fastify()
8
-
9
- // Test plugin options - app.register returns the app instance
10
- expectAssignable<FastifyInstance>(app.register(fastifySSE))
11
- expectAssignable<FastifyInstance>(app.register(fastifySSE, {}))
12
- expectAssignable<FastifyInstance>(app.register(fastifySSE, {
13
- heartbeatInterval: 10000
14
- }))
15
- expectAssignable<FastifyInstance>(app.register(fastifySSE, {
16
- serializer: (data: any) => JSON.stringify(data)
17
- }))
18
- expectAssignable<FastifyInstance>(app.register(fastifySSE, {
19
- heartbeatInterval: 0,
20
- serializer: (data: any) => String(data)
21
- }))
22
-
23
- // Test invalid plugin options
24
- expectError(app.register(fastifySSE, {
25
- heartbeatInterval: 'invalid'
26
- }))
27
- expectError(app.register(fastifySSE, {
28
- serializer: 123
29
- }))
30
- expectError(app.register(fastifySSE, {
31
- unknownOption: true
32
- }))
33
-
34
- // Test route options
35
- app.get('/sse', { sse: true }, async (request, reply) => {
36
- expectType<SSEReplyInterface>(reply.sse)
37
- return reply.sse.send({ data: 'test' })
38
- })
39
-
40
- app.get('/sse-with-options', {
41
- sse: {
42
- heartbeat: true,
43
- serializer: (data: any) => JSON.stringify(data)
44
- }
45
- }, async (request, reply) => {
46
- expectType<SSEReplyInterface>(reply.sse)
47
- return reply.sse.send({ data: 'test' })
48
- })
49
-
50
- // Test SSE message types
51
- const message: SSEMessage = {
52
- data: 'test'
53
- }
54
-
55
- const fullMessage: SSEMessage = {
56
- id: '123',
57
- event: 'custom',
58
- data: { foo: 'bar' },
59
- retry: 5000
60
- }
61
-
62
- expectType<string | undefined>(message.id)
63
- expectType<string | undefined>(message.event)
64
- expectType<any>(message.data)
65
- expectType<number | undefined>(message.retry)
66
-
67
- // Test SSE sources
68
- const stringSource: SSESource = 'hello'
69
- const bufferSource: SSESource = Buffer.from('hello')
70
- const messageSource: SSESource = { data: 'test' }
71
- const streamSource: SSESource = new Readable()
72
-
73
- async function* asyncGenerator(): AsyncIterable<SSEMessage> {
74
- yield { data: 'test' }
75
- }
76
- const asyncIterableSource: SSESource = asyncGenerator()
77
-
78
- // Test SSE reply interface
79
- app.get('/test-reply', { sse: true }, async (request, reply) => {
80
- // Test properties
81
- expectType<string | null>(reply.sse.lastEventId)
82
- expectType<boolean>(reply.sse.isConnected)
83
-
84
- // Test send method with different sources
85
- expectType<Promise<void>>(reply.sse.send('string'))
86
- expectType<Promise<void>>(reply.sse.send(Buffer.from('buffer')))
87
- expectType<Promise<void>>(reply.sse.send({ data: 'object' }))
88
- expectType<Promise<void>>(reply.sse.send(new Readable()))
89
- expectType<Promise<void>>(reply.sse.send(asyncGenerator()))
90
-
91
- // Test stream method
92
- expectType<NodeJS.WritableStream>(reply.sse.stream())
93
-
94
- // Test keepAlive method
95
- expectType<void>(reply.sse.keepAlive())
96
-
97
- // Test close method
98
- expectType<void>(reply.sse.close())
99
-
100
- // Test replay method
101
- expectType<Promise<void>>(reply.sse.replay(async (lastEventId: string) => {
102
- console.log(lastEventId)
103
- }))
104
-
105
- // Test onClose method
106
- expectType<void>(reply.sse.onClose(() => {
107
- console.log('closed')
108
- }))
109
- })
110
-
111
- // Test that reply.sse is available only on sse routes
112
- app.get('/no-sse', async (request, reply) => {
113
- // Note: reply.sse is added at runtime by the plugin, so we can't test its absence at compile time
114
- // This is expected behavior for Fastify plugins that decorate at runtime
115
- })
116
-
117
- // Test complex SSE message scenarios
118
- const complexMessage: SSEMessage = {
119
- id: '456',
120
- event: 'update',
121
- data: {
122
- nested: {
123
- property: 'value',
124
- array: [1, 2, 3]
125
- }
126
- },
127
- retry: 10000
128
- }
129
-
130
- expectAssignable<SSESource>(complexMessage)
131
-
132
- // Test async iterator types
133
- async function* typedAsyncGenerator(): AsyncIterable<string> {
134
- yield 'test'
135
- }
136
-
137
- async function* mixedAsyncGenerator(): AsyncIterable<SSEMessage | string | Buffer> {
138
- yield { data: 'message' }
139
- yield 'string'
140
- yield Buffer.from('buffer')
141
- }
142
-
143
- expectAssignable<SSESource>(typedAsyncGenerator())
144
- expectAssignable<SSESource>(mixedAsyncGenerator())
145
-
146
- // Test plugin options interface
147
- const pluginOptions: SSEPluginOptions = {
148
- heartbeatInterval: 15000,
149
- serializer: (data) => {
150
- expectType<any>(data)
151
- return JSON.stringify(data)
152
- }
153
- }
154
-
155
- // Test serializer function type
156
- const customSerializer = (data: any): string => {
157
- if (typeof data === 'object') {
158
- return JSON.stringify(data)
159
- }
160
- return String(data)
161
- }
162
-
163
- expectAssignable<SSEPluginOptions>({
164
- serializer: customSerializer
165
- })
166
-
167
- // Test route options with boolean
168
- const routeOptions1: RouteShorthandOptions = {
169
- sse: true
170
- }
171
-
172
- const routeOptions2: RouteShorthandOptions = {
173
- sse: false
174
- }
175
-
176
- const routeOptions3: RouteShorthandOptions = {
177
- sse: {
178
- heartbeat: false,
179
- serializer: (data) => String(data)
180
- }
181
- }
182
-
183
- // Test invalid route options - these tests verify type checking
184
- expectError(app.get('/invalid', {
185
- sse: 'invalid'
186
- }, async (request, reply) => {}))
187
-
188
- expectError(app.get('/invalid2', {
189
- sse: {
190
- unknownOption: true
191
- }
192
- }, async (request, reply) => {}))