Skip to content

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

@Emosewaj

Description

@Emosewaj

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('/');

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