Skip to content

Commit

Permalink
feat: add configurable Root.prototype.fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
recih authored Jun 12, 2020
1 parent 1b8aa8f commit ad3cffd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
8 changes: 8 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,14 @@ export class Root extends NamespaceBase {
*/
public resolvePath(origin: string, target: string): (string|null);

/**
* Fetch content from file path or url
* This method exists so you can override it with your own logic.
* @param path File path or url
* @param callback Callback function
*/
public fetch(path: string, callback: FetchCallback): void;

/**
* Loads one or multiple .proto or preprocessed .json files into this root namespace and calls the callback.
* @param filename Names of one or multiple files to load
Expand Down
12 changes: 11 additions & 1 deletion src/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ Root.fromJSON = function fromJSON(json, root) {
*/
Root.prototype.resolvePath = util.path.resolve;

/**
* Fetch content from file path or url
* This method exists so you can override it with your own logic.
* @function
* @param {string} path File path or url
* @param {FetchCallback} callback Callback function
* @returns {undefined}
*/
Root.prototype.fetch = util.fetch;

// A symbol-like function to safely signal synchronous loading
/* istanbul ignore next */
function SYNC() {} // eslint-disable-line no-empty-function
Expand Down Expand Up @@ -168,7 +178,7 @@ Root.prototype.load = function load(filename, options, callback) {
process(filename, source);
} else {
++queued;
util.fetch(filename, function(err, source) {
self.fetch(filename, function(err, source) {
--queued;
/* istanbul ignore if */
if (!callback)
Expand Down

0 comments on commit ad3cffd

Please sign in to comment.