devsite/node_modules/@iarna/toml/parse-async.js

31 lines
863 B
JavaScript
Raw Permalink Normal View History

2024-07-08 01:49:38 +00:00
'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))
}
}
}