-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(yieldable-parser): parseAsync sometimes blocks for a *long* time #36 #37
Open
lukas8219
wants to merge
13
commits into
ibmruntimes:master
Choose a base branch
from
lukas8219:issue_36
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
60671fb
wip(parser): moved indexOf to processing by chunks
lukas8219 6e8df78
wip(parser): passed all test cases, added new for big escaped strings
lukas8219 b5c59f6
wip: create asyncinfying function for generator
lukas8219 e7e4610
wip: parsing without blocking eventloop - migrate from Promise
lukas8219 a9e2945
wip: moved async to iterator for backwards compat
lukas8219 fc008f9
wip: moved ugly generators to delegation
lukas8219 d3fea3f
wip: fixes package.json files
lukas8219 1abe52d
wip: removed async from parser
lukas8219 240b97b
wip: added constants for MAX_STRING_SIZE
lukas8219 d6fefa6
test: adjusted test cases
lukas8219 1bab7a2
wip: removed all comments from indexOf
lukas8219 d089040
wip: removed unnecessary change in main control loop
lukas8219 833b6f6
chore(index-of-generator.js): added comment to explain the MAX_STRING…
lukas8219 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const tap = require('tap'); | ||
const { indexOfGenerator } = require('../utils/index-of-generator'); | ||
|
||
function promisifyGenerator(generator){ | ||
return (...args) => { | ||
const gen = generator(...args); | ||
|
||
function handleNext(resolvedValue){ | ||
if(resolvedValue.done){ | ||
return Promise.resolve(resolvedValue.value); | ||
} | ||
return Promise.resolve(gen.next(resolvedValue.value)).then(handleNext); | ||
} | ||
|
||
|
||
return Promise.resolve(gen.next()) | ||
.then((resolvedValue) => handleNext(resolvedValue), (error) => Promise.reject(gen.throw(error))); | ||
} | ||
} | ||
|
||
const asyncIndexOf = promisifyGenerator(indexOfGenerator); | ||
|
||
const stringAsArray = ['s', 'o', 'm', 'e']; | ||
const text = stringAsArray.join(''); | ||
|
||
tap.test('should return -1 if not found due to initialIndex', async () => { | ||
const index = await asyncIndexOf(text, 's', 1); | ||
const syncIndexOf = text.indexOf('s', 1); | ||
tap.equal(index, syncIndexOf); | ||
}) | ||
|
||
tap.test('should return proper index', async () => { | ||
const index = await asyncIndexOf(text, 'e'); | ||
const syncIndexOf = text.indexOf('e'); | ||
tap.equal(index, syncIndexOf); | ||
}) | ||
|
||
tap.test('should return proper test with initialIndex', async () => { | ||
const index = await asyncIndexOf(text, 'e', 2); | ||
const syncIndexOf = text.indexOf('e', 2); | ||
tap.equal(index, syncIndexOf); | ||
}) | ||
|
||
tap.test('should return correct indexOf if initialIndex is same', async () => { | ||
const index = await asyncIndexOf(text, 'e', 3); | ||
const syncIndexOf = text.indexOf('e', 3); | ||
tap.equal(index, syncIndexOf); | ||
}) | ||
|
||
tap.test('should return -1 if not found', async () => { | ||
const index = await asyncIndexOf(text, 'x'); | ||
const syncIndexOf = text.indexOf('x'); | ||
tap.equal(index, syncIndexOf); | ||
}) |
lukas8219 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
const yj = require('../index'); | ||
const tap = require('tap'); | ||
const { readFileSync } = require('fs'); | ||
const str = readFileSync('./test/big-json-with-string.txt'); | ||
const nodeJSNativeParse = JSON.parse(str); | ||
|
||
// Make sure the API works well without the optional parameters. | ||
yj.parseAsync(str, (err, obj) => { | ||
if (!err) | ||
tap.equal(obj.text, nodeJSNativeParse.text, 'Failed to parse big string message - omitting logs', { diagnostic: false }); | ||
else | ||
tap.fail(err); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//write more performant option for indexOf | ||
//if more than 50k characters, use BHM Algorithm | ||
//consider a big string to be 10k characters | ||
const MAX_STRING_SIZE = 10000; | ||
lukas8219 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
module.exports.indexOfGenerator = function* indexOfGenerator(stringInput, searchInput, initialIndex = 0) { | ||
if (!stringInput || !stringInput.length) { | ||
return -1; | ||
} | ||
stringInput = stringInput.slice(initialIndex); | ||
let innerAt = 0; | ||
let index = -1; | ||
while (true) { | ||
const at = Math.max(innerAt - 1, 0); | ||
yield chunk = stringInput.slice(at, innerAt + MAX_STRING_SIZE); | ||
if (!chunk.length) { | ||
return index; | ||
} | ||
const indexOf = chunk.indexOf(searchInput); | ||
if (indexOf !== -1) { | ||
yield index = indexOf + at + initialIndex; | ||
return index; | ||
} | ||
innerAt += chunk.length + 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one may require attention into the JSON info. I copied and pasted from the shared file in the issue