@fastify/sse 0.3.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.
@@ -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) => {}))