child_process = require("child_process") logger = require 'logger-sharelatex' metrics = require('metrics-sharelatex') BATCH_SIZE = 100 class ASpellWorker constructor: (language) -> @language = language @count = 0 @pipe = child_process.spawn("aspell", ["pipe", "-t", "--encoding=utf-8", "-d", language]) logger.log process: @pipe.pid, lang: @language, "starting new aspell worker" metrics.inc "aspellWorker-start-" + @language @pipe.on 'exit', () => @state = 'killed' logger.log process: @pipe.pid, lang: @language, "aspell worker has exited" metrics.inc "aspellWorker-exit-" + @language @pipe.on 'error', (err) => @state = 'error' unless @state == 'killed' logger.log process: @pipe.pid, error: err, stdout: output.slice(-1024), stderr: error.slice(-1024), lang: @language, "aspell worker error" if @callback? @callback err, [] @pipe.stdin.on 'error', (err) => @state = 'error' unless @state == 'killed' logger.log process: @pipe.pid, error: err, stdout: output.slice(-1024), stderr: error.slice(-1024), lang: @language, "aspell worker error on stdin" if @callback? @callback err, [] output = "" endMarker = new RegExp("^(#{language}|Error)$", "m") @pipe.stdout.on "data", (chunk) => output = output + chunk # We receive the language code from Aspell as the end of data marker if chunk.toString().match(endMarker) @callback(null, output.slice()) @state = 'ready' output = "" error = "" error = "" @pipe.stderr.on "data", (chunk) => error = error + chunk @pipe.stdout.on "end", () => # process has ended @state = "end" isReady: () -> return @state == 'ready' check: (words, callback) -> # we will now send data to aspell, and be ready again when we # receive the end of data marker @state = 'busy' @callback = callback @setTerseMode() @write(words) @flush() write: (words) -> i = 0 while i < words.length # batch up the words to check for efficiency batch = words.slice(i, i + BATCH_SIZE) @sendWords batch i += BATCH_SIZE flush: () -> # get aspell to send an end of data marker "*" when ready #@sendCommand("%") # take the aspell pipe out of terse mode so we can look for a '*' #@sendCommand("^ENDOFSTREAMMARKER") # send our marker which will generate a '*' #@sendCommand("!") # go back into terse mode @sendCommand("$$l") shutdown: (reason) -> logger.log process: @pipe.pid, reason: reason, 'shutting down' @state = "closing" @pipe.stdin.end() kill: (reason) -> logger.log process: @pipe.pid, reason: reason, 'killing' return if @state == 'killed' @pipe.kill('SIGKILL') setTerseMode: () -> @sendCommand("!") sendWord: (word) -> @sendCommand("^" + word) sendWords: (words) -> # Aspell accepts multiple words to check on the same line # ^word1 word2 word3 ... # See aspell.info, writing programs to use Aspell Through A Pipe @sendCommand("^" + words.join(" ")) @count++ sendCommand: (command) -> @pipe.stdin.write(command + "\n") module.exports = ASpellWorker