ASpellWorker.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Sanity-check the conversion and remove this comment.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS102: Remove unnecessary code created because of implicit returns
  6. * DS205: Consider reworking code to avoid use of IIFEs
  7. * DS207: Consider shorter variations of null checks
  8. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  9. */
  10. const childProcess = require('child_process')
  11. const logger = require('logger-sharelatex')
  12. const metrics = require('metrics-sharelatex')
  13. const _ = require('underscore')
  14. const BATCH_SIZE = 100
  15. class ASpellWorker {
  16. constructor(language) {
  17. this.language = language
  18. this.count = 0
  19. this.pipe = childProcess.spawn('aspell', [
  20. 'pipe',
  21. '-t',
  22. '--encoding=utf-8',
  23. '-d',
  24. language
  25. ])
  26. logger.info(
  27. { process: this.pipe.pid, lang: this.language },
  28. 'starting new aspell worker'
  29. )
  30. metrics.inc('aspellWorker', 1, { status: 'start', method: this.language })
  31. this.pipe.on('exit', () => {
  32. this.state = 'killed'
  33. logger.info(
  34. { process: this.pipe.pid, lang: this.language },
  35. 'aspell worker has exited'
  36. )
  37. return metrics.inc('aspellWorker', 1, {
  38. status: 'exit',
  39. method: this.language
  40. })
  41. })
  42. this.pipe.on('close', () => {
  43. if (this.state !== 'killed') {
  44. this.state = 'closed'
  45. }
  46. if (this.callback != null) {
  47. logger.err(
  48. { process: this.pipe.pid, lang: this.language },
  49. 'aspell worker closed output streams with uncalled callback'
  50. )
  51. this.callback(
  52. new Error(
  53. 'aspell worker closed output streams with uncalled callback'
  54. ),
  55. []
  56. )
  57. return (this.callback = null)
  58. }
  59. })
  60. this.pipe.on('error', err => {
  61. if (this.state !== 'killed') {
  62. this.state = 'error'
  63. }
  64. logger.log(
  65. {
  66. process: this.pipe.pid,
  67. error: err,
  68. stdout: output.slice(-1024),
  69. stderr: error.slice(-1024),
  70. lang: this.language
  71. },
  72. 'aspell worker error'
  73. )
  74. if (this.callback != null) {
  75. this.callback(err, [])
  76. return (this.callback = null)
  77. }
  78. })
  79. this.pipe.stdin.on('error', err => {
  80. if (this.state !== 'killed') {
  81. this.state = 'error'
  82. }
  83. logger.info(
  84. {
  85. process: this.pipe.pid,
  86. error: err,
  87. stdout: output.slice(-1024),
  88. stderr: error.slice(-1024),
  89. lang: this.language
  90. },
  91. 'aspell worker error on stdin'
  92. )
  93. if (this.callback != null) {
  94. this.callback(err, [])
  95. return (this.callback = null)
  96. }
  97. })
  98. var output = ''
  99. const endMarker = new RegExp('^[a-z][a-z]', 'm')
  100. this.pipe.stdout.on('data', chunk => {
  101. output = output + chunk
  102. // We receive the language code from Aspell as the end of data marker
  103. if (chunk.toString().match(endMarker)) {
  104. if (this.callback != null) {
  105. this.callback(null, output.slice())
  106. this.callback = null // only allow one callback in use
  107. } else {
  108. logger.err(
  109. { process: this.pipe.pid, lang: this.language },
  110. 'end of data marker received when callback already used'
  111. )
  112. }
  113. this.state = 'ready'
  114. output = ''
  115. }
  116. })
  117. var error = ''
  118. this.pipe.stderr.on('data', chunk => {
  119. return (error = error + chunk)
  120. })
  121. this.pipe.stdout.on('end', () => {
  122. // process has ended
  123. return (this.state = 'end')
  124. })
  125. }
  126. isReady() {
  127. return this.state === 'ready'
  128. }
  129. check(words, callback) {
  130. // we will now send data to aspell, and be ready again when we
  131. // receive the end of data marker
  132. this.state = 'busy'
  133. if (this.callback != null) {
  134. // only allow one callback in use
  135. logger.err(
  136. { process: this.pipe.pid, lang: this.language },
  137. 'CALLBACK ALREADY IN USE'
  138. )
  139. return this.callback(
  140. new Error('Aspell callback already in use - SHOULD NOT HAPPEN')
  141. )
  142. }
  143. this.callback = _.once(callback) // extra defence against double callback
  144. this.setTerseMode()
  145. this.write(words)
  146. return this.flush()
  147. }
  148. write(words) {
  149. let i = 0
  150. return (() => {
  151. const result = []
  152. while (i < words.length) {
  153. // batch up the words to check for efficiency
  154. const batch = words.slice(i, i + BATCH_SIZE)
  155. this.sendWords(batch)
  156. result.push((i += BATCH_SIZE))
  157. }
  158. return result
  159. })()
  160. }
  161. flush() {
  162. // get aspell to send an end of data marker "*" when ready
  163. // @sendCommand("%") # take the aspell pipe out of terse mode so we can look for a '*'
  164. // @sendCommand("^ENDOFSTREAMMARKER") # send our marker which will generate a '*'
  165. // @sendCommand("!") # go back into terse mode
  166. return this.sendCommand('$$l')
  167. }
  168. shutdown(reason) {
  169. logger.info({ process: this.pipe.pid, reason }, 'shutting down')
  170. this.state = 'closing'
  171. return this.pipe.stdin.end()
  172. }
  173. kill(reason) {
  174. logger.info({ process: this.pipe.pid, reason }, 'killing')
  175. if (this.state === 'killed') {
  176. return
  177. }
  178. return this.pipe.kill('SIGKILL')
  179. }
  180. setTerseMode() {
  181. return this.sendCommand('!')
  182. }
  183. sendWord(word) {
  184. return this.sendCommand(`^${word}`)
  185. }
  186. sendWords(words) {
  187. // Aspell accepts multiple words to check on the same line
  188. // ^word1 word2 word3 ...
  189. // See aspell.info, writing programs to use Aspell Through A Pipe
  190. this.sendCommand(`^${words.join(' ')}`)
  191. return this.count++
  192. }
  193. sendCommand(command) {
  194. return this.pipe.stdin.write(command + '\n')
  195. }
  196. }
  197. module.exports = ASpellWorker