Project

Profile

Help

Can Saxon-JS BrowserPlatform be extended by the user?

Added by Ryan Graham almost 2 years ago

I am trying to bundle Saxon-JS inside our internal library, however one requirement of our library is that no external HTTP requests are made - everything must be bundled inside the library.

Our XSLT makes an external request using doc() (we might be able to refactor this) and Saxon-JS makes an external request to opt/categories.json.

Those requests happen in BrowserPlatform.readFile and BrowserPlatform.readResource respectively.

There appears to be a public SaxonJS.setPlatform method, which I could use but that requires the caller to re-implement the entire platform interface. Is there any way Saxon could expose the BrowserPlatform.platform object so that we can re-engineer the loading techniques to work with the platform?

For completeness sake, this is what I had in mind:

// Expose BrowserPlatform as an object on SaxonJS namespace...
var myCustomPlatform = Object.assign({}, SaxonJS.BrowserPlatform, {
    readFile: function(uri, enc) {
        // I'll be using Webpack to 'require' local file...
        return require(uri);
    },
    readResource: function(href) {
        return require(href);
    }
});
SaxonJS.setPlatform(myCustomPlatform);

Replies (2)

RE: Can Saxon-JS BrowserPlatform be extended by the user? - Added by Michael Kay almost 2 years ago

Thanks for the suggestions. Certainly the Platform object is a key extensibility/customization interface so we'll keep this in mind as we move towards a 2.0 release which will cover Node.js and browser platforms.

At the moment I think you just have to hack your way through the jungle...

RE: Can Saxon-JS BrowserPlatform be extended by the user? - Added by Ryan Graham almost 2 years ago

Michael Kay wrote:

Thanks for the suggestions. Certainly the Platform object is a key extensibility/customization interface so we'll keep this in mind as we move towards a 2.0 release which will cover Node.js and browser platforms.

At the moment I think you just have to hack your way through the jungle...

Good to hear this is on the roadmap for v2.

I just went into the jungle and found an existing SaxonJS.getPlatform! My apologies for not digging far enough.

For future readers, this allowed me to implement the required functionality as follows:

// Copy all properties from Saxon's default BrowserPlatform into a new object,
// then overwrite the necessary functions...
const createPlatform = () => Object.assign({}, SaxonJS.getPlatform(), {
    readFile: function(uri, enc) {
        return require(uri);
    },
    readResource: function(href) {
        return require(href);
    }
});
const myCustomPlatform = createPlatform();
// Ensure 'setPlatform' is called before 'transform'...
SaxonJS.setPlatform(myCustomPlatform);
SaxonJS.transform({
    stylesheetNode,
    sourceText
});

And it works great for my use case.

    (1-2/2)

    Please register to reply