forked from iarna/iarna-toml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse-async.js
30 lines (28 loc) · 863 Bytes
/
parse-async.js
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
'use strict'
module.exports = parseAsync
const TOMLParser = require('./lib/toml-parser.js')
const prettyError = require('./parse-pretty-error.js')
function parseAsync (str, opts) {
if (!opts) opts = {}
const index = 0
const blocksize = opts.blocksize || 40960
const parser = new TOMLParser()
return new Promise((resolve, reject) => {
setImmediate(parseAsyncNext, index, blocksize, resolve, reject)
})
function parseAsyncNext (index, blocksize, resolve, reject) {
if (index >= str.length) {
try {
return resolve(parser.finish())
} catch (err) {
return reject(prettyError(err, str))
}
}
try {
parser.parse(str.slice(index, index + blocksize))
setImmediate(parseAsyncNext, index + blocksize, blocksize, resolve, reject)
} catch (err) {
reject(prettyError(err, str))
}
}
}