Skip to content

Erroneous detection of dependencies can lead to syntax error in Worker #45

@davidtaylorhq

Description

@davidtaylorhq

This library attempts to find dependencies using a regular expression:

// main bundle deps
var re = new RegExp('(\\\\n|\\W)' + quoteRegExp(webpackRequireName) + dependencyRegExp, 'g')
var match
while ((match = re.exec(fnString))) {
if (match[3] === 'dll-reference') continue
retval[queueName].push(match[3])
}

In minified code, webpackRequireName will often be a single-character function name like r, so this regex will detect all occurrences of r(…). Unfortuantely, the r() function name may also be used in other scopes for a totally different function, so this dependency detection can pick up a totally random argument and treat it as a dependency.

In our case (as users of hls.min.js) this function call is minified to r('init', null), and so 'init' is erroneously detected as a dependency.

Ordinarily this fails silently and doesn't affect functionality. When assembling the WebWorker blob, for id='init', the call to sources.main[id] returns undefined and things carry on without raising any errors. However, if there is variable/function defined on the Array class with the name init, then it will be returned, stringified, and lead to a syntax error in the Worker. (In our case, that init function is added to the Array prototype by ember.js)

So, with both of those very unlucky circumstances, we end up with this when the Worker is initialized:

Uncaught SyntaxError: Unexpected token '{' (at efc643e5-eb1d-4408-939d-225e166586ac:135:20)

I imagine a robust fix to the dependency-detection will be tricky - parsing JS with Regex is naturally not going to be perfect. But I wonder if it might be possible to mitigate the errors when it does happen 🤔

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions