-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add types and refactor fastify plugin
- Loading branch information
Showing
23 changed files
with
924 additions
and
68 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,58 @@ | ||
import fastify from 'fastify' | ||
import { serverFactory, getUws, WebSocketStream } from './src/server.js' | ||
|
||
import fastifyUwsPlugin from './src/plugin.js' | ||
|
||
const app = fastify({ | ||
serverFactory | ||
}) | ||
|
||
await app.register(fastifyUwsPlugin) | ||
|
||
app.addHook('onReady', async () => { | ||
// access to uws app | ||
// eslint-disable-next-line no-unused-vars | ||
const uwsApp = getUws(app) | ||
}) | ||
|
||
app.websocketServer.on('open', ws => { | ||
console.log('OPEN') | ||
}) | ||
|
||
app.websocketServer.on('close', ws => { | ||
console.log('CLOSE') | ||
}) | ||
|
||
app | ||
.route({ | ||
method: 'GET', | ||
url: '/', | ||
handler (req, reply) { | ||
return 'hello from http endpoint' | ||
}, | ||
uws: { | ||
// cache subscription topics to produce less memory allocations | ||
topics: [ | ||
'home/sensors/ligth', | ||
'home/sensors/temp' | ||
] | ||
}, | ||
uwsHandler (conn) { | ||
conn.subscribe('home/sensors/temp') | ||
conn.on('message', (message) => { | ||
conn.publish('home/sensors/temp', 'random message') | ||
}) | ||
conn.send(JSON.stringify({ hello: 'world' })) | ||
} | ||
}) | ||
.get('/stream', { uws: true }, (conn) => { | ||
const stream = new WebSocketStream(conn) | ||
stream.on('data', data => { | ||
console.log('stream data from /stream') | ||
}) | ||
}) | ||
.listen({ | ||
port: 3000 | ||
}, (err) => { | ||
err && console.error(err) | ||
}) |
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,19 @@ | ||
import * as fs from 'node:fs' | ||
import { $ } from 'execa' | ||
|
||
await $`tsc src/server.js src/plugin.js --declaration --allowJs --emitDeclarationOnly --outDir types` | ||
|
||
const types = fs.readFileSync('types/plugin.d.ts', 'utf-8') | ||
fs.writeFileSync( | ||
'types/plugin.d.ts', | ||
types + 'import "./fastify-overload.d.ts"' | ||
) | ||
|
||
// for some reason tsc exclude uwebsocket.js module | ||
for (const file of ['server.d.ts', 'websocket-server.d.ts']) { | ||
const types = fs.readFileSync(`types/${file}`, 'utf-8') | ||
fs.writeFileSync( | ||
`types/${file}`, | ||
types + 'import uws from "uWebSockets.js"' | ||
) | ||
} |
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
import { STATUS_CODES } from 'http' | ||
|
||
import { Writable } from 'streamx' | ||
|
Oops, something went wrong.