保誠-保戶業務員媒合平台
HelenHuang
2022-06-09 9bdb95c9e34cef640534e5e5a1e2225a80442000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const { resolve } = require('path')
const { readFileSync } = require('fs')
const connect = require('connect')
const serveStatic = require('serve-static')
const getPort = require('get-port-please')
const { json, end, header } = require('node-res')
 
const { parseStack } = require('./utils')
const SSE = require('./sse')
 
class LoadingUI {
  constructor (options) {
    this.options = options
 
    this._lastBroadcast = 0
 
    this.states = []
    this.allDone = true
    this.hasErrors = false
 
    this.serveIndex = this.serveIndex.bind(this)
 
    this._init()
  }
 
  _init () {
    // Create a connect middleware stack
    this.app = connect()
 
    // Create an SSE handler instance
    this.sse = new SSE()
 
    // Fix CORS
    this.app.use((req, res, next) => {
      res.setHeader('Access-Control-Allow-Origin', '*')
      next()
    })
 
    // Subscribe to SSR channel
    this.app.use('/sse', (req, res) => this.sse.subscribe(req, res))
 
    // Serve state with JSON
    this.app.use('/json', (req, res) => json(req, res, this.state))
 
    // Load indexTemplate
    const distPath = resolve(__dirname, '../app-dist')
    this.indexTemplate = readFileSync(resolve(distPath, 'index.html'), 'utf-8')
 
    // Serve assets
    this.app.use('/assets', serveStatic(resolve(distPath, 'assets')))
  }
 
  async initAlt ({ url }) {
    if (this._server || this.options.baseURLAlt) {
      return
    }
 
    // Redirect users directly open this port
    this.app.use('/', (req, res) => {
      res.setHeader('Location', url)
      res.statusCode = 307
      res.end(url)
    })
 
    // Start listening on alternative port
    const port = await getPort({ random: true, name: 'nuxt_loading' })
 
    return new Promise((resolve, reject) => {
      this._server = this.app.listen(port, (err) => {
        if (err) { return reject(err) }
        this.options.baseURLAlt = `http://localhost:${port}`
        resolve()
      })
    })
  }
 
  close () {
    if (this._server) {
      return new Promise((resolve, reject) => {
        this._server.close((err) => {
          if (err) {
            return reject(err)
          }
          resolve()
        })
      })
    }
  }
 
  get state () {
    return {
      error: this.error,
      states: this.states,
      allDone: this.allDone,
      hasErrors: this.hasErrors
    }
  }
 
  setStates (states) {
    this.clearError()
    this.states = states
    this.allDone = this.states.every(state => state.progress === 0 || state.progress === 100)
    this.hasErrors = this.states.some(state => state.hasErrors === true)
    this.broadcastState()
  }
 
  setError (error) {
    this.clearStates(true)
    this.error = {
      description: error.toString(),
      stack: parseStack(error.stack).join('\n')
    }
    this.broadcastState()
  }
 
  clearError () {
    this.error = undefined
  }
 
  clearStates (hasErrors) {
    this.states = []
    this.allDone = false
    this.hasErrors = !!hasErrors
  }
 
  broadcastState () {
    const now = new Date()
 
    if ((now - this._lastBroadcast > 500) || this.allDone || this.hasErrors) {
      this.sse.broadcast('state', this.state)
      this._lastBroadcast = now
    }
  }
 
  serveIndex (req, res) {
    const html = this.indexTemplate
      .replace('__STATE__', JSON.stringify(this.state))
      .replace('__OPTIONS__', JSON.stringify(this.options))
      .replace(/__BASE_URL__/g, this.options.baseURL)
 
    header(res, 'Content-Type', 'text/html')
    end(res, html)
  }
}
 
module.exports = LoadingUI