tkserver 1.7.24 → 2.0.0-beta.2

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/server.js DELETED
@@ -1,125 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- const http = require('http')
4
- const logger = require('twikoo-func/utils/logger')
5
-
6
- const dbUrl = process.env.MONGODB_URI || process.env.MONGO_URL || null
7
- const twikoo = dbUrl ? require('./mongo') : require('./index')
8
- const server = http.createServer()
9
- const sockets = new Set()
10
- const TWIKOO_SHUTDOWN_TIMEOUT = parseInt(process.env.TWIKOO_SHUTDOWN_TIMEOUT) || 5000
11
- let isShuttingDown = false
12
- let shuttingDownPromise = null
13
-
14
- server.on('request', async function (request, response) {
15
- if (isShuttingDown) {
16
- response.writeHead(503, {
17
- Connection: 'close',
18
- 'Content-Type': 'application/json'
19
- })
20
- response.end(JSON.stringify({ code: 503, message: 'Twikoo server is shutting down' }))
21
- return
22
- }
23
- try {
24
- const buffers = []
25
- for await (const chunk of request) {
26
- buffers.push(chunk)
27
- }
28
- request.body = JSON.parse(Buffer.concat(buffers).toString())
29
- } catch (e) {
30
- request.body = {}
31
- }
32
- response.status = function (code) {
33
- this.statusCode = code
34
- return this
35
- }
36
- response.json = function (json) {
37
- if (!response.writableEnded) {
38
- this.writeHead(200, { 'Content-Type': 'application/json' })
39
- this.end(JSON.stringify(json))
40
- }
41
- return this
42
- }
43
- return await twikoo(request, response)
44
- })
45
-
46
- server.on('connection', (socket) => {
47
- sockets.add(socket)
48
- socket.on('close', () => sockets.delete(socket))
49
- })
50
-
51
- const port = parseInt(process.env.TWIKOO_PORT) || 8080
52
- const host = process.env.TWIKOO_HOST || (process.env.TWIKOO_LOCALHOST_ONLY === 'true' ? 'localhost' : '::')
53
-
54
- server.listen(port, host, function () {
55
- logger.info(`Twikoo is using ${dbUrl ? 'mongo' : 'loki'} database`)
56
- logger.info(`Twikoo function started on host ${host} port ${port}`)
57
- })
58
-
59
- function closeServer () {
60
- return new Promise((resolve, reject) => {
61
- server.close((err) => {
62
- if (err) {
63
- reject(err)
64
- } else {
65
- resolve()
66
- }
67
- })
68
- })
69
- }
70
-
71
- function destroySockets () {
72
- for (const socket of sockets) {
73
- socket.destroy()
74
- }
75
- }
76
-
77
- async function shutdownWithTimeout () {
78
- let timeoutId
79
- const closeTask = closeServer()
80
- const timeoutTask = new Promise((resolve) => {
81
- timeoutId = setTimeout(() => {
82
- logger.warn(`Twikoo server shutdown timed out after ${TWIKOO_SHUTDOWN_TIMEOUT}ms, destroying open connections`)
83
- destroySockets()
84
- resolve()
85
- }, TWIKOO_SHUTDOWN_TIMEOUT)
86
- })
87
- await Promise.race([closeTask, timeoutTask])
88
- clearTimeout(timeoutId)
89
- await closeTask
90
- }
91
-
92
- async function runShutdown (signal) {
93
- isShuttingDown = true
94
- logger.info(`Received ${signal}, shutting down Twikoo server...`)
95
- try {
96
- await shutdownWithTimeout()
97
- } catch (e) {
98
- logger.error('Twikoo HTTP server shutdown failed:', e)
99
- } finally {
100
- if (typeof twikoo.shutdown === 'function') {
101
- await twikoo.shutdown()
102
- }
103
- }
104
- logger.info('Twikoo server stopped')
105
- }
106
-
107
- async function shutdown (signal) {
108
- if (!shuttingDownPromise) {
109
- shuttingDownPromise = runShutdown(signal)
110
- }
111
- return await shuttingDownPromise
112
- }
113
-
114
- async function handleSignal (signal) {
115
- try {
116
- await shutdown(signal)
117
- process.exitCode = 0
118
- } catch (e) {
119
- logger.error('Twikoo server shutdown failed:', e)
120
- process.exitCode = 1
121
- }
122
- }
123
-
124
- process.on('SIGTERM', () => handleSignal('SIGTERM'))
125
- process.on('SIGINT', () => handleSignal('SIGINT'))