Skip to content
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

Loading dictionaries fails if dictionaries are hosted on different domain #10

Open
Emosewaj opened this issue Dec 14, 2022 · 0 comments

Comments

@Emosewaj
Copy link

JavaScript Code on domain-a.com using kuroshiro.js and kuroshiro-analyzer-kuromoji.js built using npm run build:

kuroshiro.init(new KuromojiAnalyzer({ dict: "https://domain-b.com/js/kuromoji/dict" }));

This request will fail as the URL is mangled to https:/domain-b.com/js/kuromoji/dict, which will cause the XMLHttpRequest to attempt to load it from https://domain-a.com/domain-b.com/js/kuromoji/dict instead.

This is caused by the posix version of path.normalize():

// path.normalize(path)
// posix version
exports.normalize = function(path) {
  // *snip*  

  // Normalize the path
  // Before: "https://domain-b.com/js/kuromoji/dict"
  path = normalizeArray(filter(path.split('/'), function(p) {  // ["https:", "", "domain-b.com", "js", "kuromoji", "dict"]
    return !!p;
  }), !isAbsolute).join('/');  // "https:/domain-b.com/js/kuromoji/dict"
  
  // *snip*
};

The URL is split into its components using path.split('/'), which results in one empty element between the "https" and "domain-b.com" elements, which is then filtered out by return !!p, resulting in the missing slash.

Workaround

As a temporary workaround, one could replace the snippet with the following, which still filters normal paths but retains paths with a protocol:

path = path.split('/');

if(!path[0].includes(":")) {
  path = normalizeArray(filter(path, function(p) {
    return !!p;
  }), !isAbsolute);
}

path = path.join('/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant