-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: sse * lint: deno lint + fmt * Update router.ts Co-authored-by: Marco Antônio <[email protected]> * fix: import in deps.ts * chore: ignore weird ts error * fix: improve how we call router function --------- Co-authored-by: Marco Antônio <[email protected]>
- Loading branch information
1 parent
844627e
commit 849863b
Showing
8 changed files
with
140 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Controller, Module, SSE } from '../mod.ts'; | ||
import { assertEquals } from '../src/deps_test.ts'; | ||
import { DanetApplication } from '../src/app.ts'; | ||
import { SSEEvent } from '../src/sse/event.ts'; | ||
|
||
@Controller() | ||
class SSEExampleController { | ||
@SSE('sse') | ||
sendUpdate(): EventTarget { | ||
const eventTarget = new EventTarget(); | ||
let id = 0; | ||
const interval = setInterval(() => { | ||
if (id >= 4) { | ||
clearInterval(interval); | ||
const event = new SSEEvent({ | ||
retry: 1000, | ||
id: `${id}`, | ||
data: 'close', | ||
event: 'close', | ||
}); | ||
eventTarget.dispatchEvent(event); | ||
return; | ||
} | ||
const event = new SSEEvent({ | ||
retry: 1000, | ||
id: `${id}`, | ||
data: 'world', | ||
event: 'hello', | ||
}); | ||
eventTarget.dispatchEvent(event); | ||
id++; | ||
}, 100); | ||
return eventTarget; | ||
} | ||
} | ||
|
||
@Module({ | ||
controllers: [SSEExampleController], | ||
}) | ||
class ExampleModule {} | ||
|
||
Deno.test('Body', async () => { | ||
return new Promise<void>(async (resolve, reject) => { | ||
const app = new DanetApplication(); | ||
await app.init(ExampleModule); | ||
const listenEvent = await app.listen(0); | ||
let eventReceived = 0; | ||
const eventSource = new EventSource( | ||
`http://localhost:${listenEvent.port}/sse`, | ||
); | ||
|
||
eventSource.addEventListener('hello', async (event) => { | ||
if (event.data === 'world') { | ||
eventReceived++; | ||
} | ||
}); | ||
|
||
eventSource.addEventListener('close', async (event) => { | ||
assertEquals(eventReceived, 4); | ||
await eventSource.close(); | ||
await app.close(); | ||
resolve(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,12 +11,14 @@ export { | |
Hono as Application, | ||
type MiddlewareHandler, | ||
type Next, | ||
} from 'https://deno.land/x/hono/mod.ts'; | ||
export { type HandlerInterface } from 'https://deno.land/x/hono/types.ts'; | ||
export { HonoRequest } from 'https://deno.land/x/hono/request.ts'; | ||
export { getPath } from 'https://deno.land/x/hono/utils/url.ts'; | ||
} from 'https://deno.land/x/hono@v4.1.5/mod.ts'; | ||
export { type HandlerInterface } from 'https://deno.land/x/hono@v4.1.5/types.ts'; | ||
export { HonoRequest } from 'https://deno.land/x/hono@v4.1.5/request.ts'; | ||
export { getPath } from 'https://deno.land/x/hono@v4.1.5/utils/url.ts'; | ||
export { | ||
RegExpRouter, | ||
SmartRouter, | ||
TrieRouter, | ||
} from 'https://deno.land/x/hono/mod.ts'; | ||
} from 'https://deno.land/x/[email protected]/mod.ts'; | ||
export { SSEStreamingApi, streamSSE } from 'https://deno.land/x/[email protected]/helper.ts'; | ||
export { cors } from 'https://deno.land/x/[email protected]/middleware.ts' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { SSEMessage } from './message.ts'; | ||
|
||
export class SSEEvent extends CustomEvent<SSEMessage> { | ||
constructor(message: SSEMessage) { | ||
super('message', { detail: message }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export interface SSEMessage { | ||
data: string; | ||
event?: string; | ||
id?: string; | ||
retry?: number; | ||
} |