@paciolan/remote-module-loader
Advanced tools
Comparing version 2.5.3 to 2.6.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "default", { | ||
enumerable: true, | ||
get: function get() { | ||
return _loadRemoteModule.createLoadRemoteModule; | ||
} | ||
}); | ||
Object.defineProperty(exports, "createRequires", { | ||
enumerable: true, | ||
get: function get() { | ||
return _createRequires.createRequires; | ||
} | ||
}); | ||
var _loadRemoteModule = require("./lib/loadRemoteModule"); | ||
var _createRequires = require("./lib/createRequires"); | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
exports.__esModule = true; | ||
exports.createRequires = exports["default"] = void 0; | ||
var loadRemoteModule_1 = require("./lib/loadRemoteModule"); | ||
__createBinding(exports, loadRemoteModule_1, "createLoadRemoteModule", "default"); | ||
var createRequires_1 = require("./lib/createRequires"); | ||
__createBinding(exports, createRequires_1, "createRequires"); | ||
//# sourceMappingURL=index.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
exports.createRequires = void 0; | ||
var createRequires = function createRequires(dependencies) { | ||
return function (name) { | ||
if (!(name in (dependencies || {}))) { | ||
throw new Error("Could not require '".concat(name, "'. '").concat(name, "' does not exist in dependencies.")); | ||
var createRequires = function (dependencies) { return function (name) { | ||
var _dependencies = dependencies || {}; | ||
if (!(name in _dependencies)) { | ||
throw new Error("Could not require '" + name + "'. '" + name + "' does not exist in dependencies."); | ||
} | ||
return dependencies[name]; | ||
}; | ||
}; | ||
return _dependencies[name]; | ||
}; }; | ||
exports.createRequires = createRequires; | ||
//# sourceMappingURL=createRequires.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
exports.createLoadRemoteModule = void 0; | ||
var _memoize = _interopRequireDefault(require("./memoize")); | ||
var _xmlHttpRequestFetcher = _interopRequireDefault(require("./xmlHttpRequestFetcher")); | ||
var _nodeFetcher = _interopRequireDefault(require("./nodeFetcher")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var memoize_1 = require("./memoize"); | ||
var index_1 = require("./xmlHttpRequestFetcher/index"); | ||
var nodeFetcher_1 = require("./nodeFetcher"); | ||
var isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; | ||
/* istanbul ignore next - difficult to test */ | ||
var defaultFetcher = isBrowser ? _xmlHttpRequestFetcher.default : _nodeFetcher.default; | ||
var defaultRequires = function defaultRequires(name) { | ||
throw new Error("Could not require '".concat(name, "'. The 'requires' function was not provided.")); | ||
var defaultFetcher = isBrowser ? index_1["default"] : nodeFetcher_1["default"]; | ||
var defaultRequires = function (name) { | ||
throw new Error("Could not require '" + name + "'. The 'requires' function was not provided."); | ||
}; | ||
var createLoadRemoteModule = function createLoadRemoteModule() { | ||
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
_ref$requires = _ref.requires, | ||
requires = _ref$requires === void 0 ? defaultRequires : _ref$requires, | ||
_ref$fetcher = _ref.fetcher, | ||
fetcher = _ref$fetcher === void 0 ? defaultFetcher : _ref$fetcher; | ||
return (0, _memoize.default)(function (url) { | ||
return fetcher(url).then(function (data) { | ||
var exports = {}; | ||
var module = { | ||
exports: exports | ||
}; | ||
var func = new Function("require", "module", "exports", data); | ||
func(requires, module, exports); | ||
return module.exports; | ||
var createLoadRemoteModule = function (_a) { | ||
var _b = _a === void 0 ? {} : _a, requires = _b.requires, fetcher = _b.fetcher; | ||
var _requires = requires || defaultRequires; | ||
var _fetcher = fetcher || defaultFetcher; | ||
return memoize_1["default"](function (url) { | ||
return _fetcher(url).then(function (data) { | ||
var exports = {}; | ||
var module = { exports: exports }; | ||
var func = new Function("require", "module", "exports", data); | ||
func(_requires, module, exports); | ||
return module.exports; | ||
}); | ||
}); | ||
}); | ||
}; | ||
exports.createLoadRemoteModule = createLoadRemoteModule; | ||
//# sourceMappingURL=loadRemoteModule.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
exports.__esModule = true; | ||
/** | ||
@@ -14,15 +9,12 @@ * Memoizes a 1-arity function | ||
*/ | ||
var memoize = function memoize(func) { | ||
var cache = {}; | ||
return function (x) { | ||
if (x in cache == false) { | ||
cache[x] = func(x); | ||
} | ||
return cache[x]; | ||
}; | ||
var memoize = function (func) { | ||
var cache = {}; | ||
return function (key) { | ||
if (key in cache == false) { | ||
cache[key] = func(key); | ||
} | ||
return cache[key]; | ||
}; | ||
}; | ||
var _default = memoize; | ||
exports.default = _default; | ||
exports["default"] = memoize; | ||
//# sourceMappingURL=memoize.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
}; | ||
exports.__esModule = true; | ||
var http = require("http"); | ||
var https = require("https"); | ||
/** | ||
* Get's a url. Compatible with http and https. | ||
* @param {string} url | ||
* @param {...any} args | ||
*/ | ||
var get = function get(url) { | ||
if (typeof url !== "string") { | ||
return { | ||
on: function on(eventName, callback) { | ||
callback(new Error("URL must be a string.")); | ||
} | ||
}; | ||
} | ||
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
return url.indexOf("https://") === 0 ? https.get.apply(https, [url].concat(args)) : http.get.apply(http, [url].concat(args)); | ||
var get = function (url) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
if (typeof url !== "string") { | ||
return { | ||
on: function (eventName, callback) { | ||
callback(new Error("URL must be a string.")); | ||
} | ||
}; | ||
} | ||
return url.indexOf("https://") === 0 | ||
? https.get.apply(https, __spreadArrays([url], args)) : http.get.apply(http, __spreadArrays([url], args)); | ||
}; | ||
/** | ||
* Get's a URL and returns a Promise | ||
* @param {string} url | ||
* @returns {Promise<string>} | ||
*/ | ||
var nodeFetcher = function nodeFetcher(url) { | ||
return new Promise(function (resolve, reject) { | ||
get(url, function (res) { | ||
var data = null; // called when a data chunk is received. | ||
res.on("data", function (chunk) { | ||
if (data === null) { | ||
data = chunk; | ||
return; | ||
} | ||
data += chunk; | ||
}); // called when the complete response is received. | ||
res.on("end", function () { | ||
return resolve(data); | ||
}); | ||
}).on("error", reject); | ||
}); | ||
var nodeFetcher = function (url) { | ||
return new Promise(function (resolve, reject) { | ||
get(url, function (res) { | ||
var data = null; | ||
// called when a data chunk is received. | ||
res.on("data", function (chunk) { | ||
if (data === null) { | ||
data = chunk; | ||
return; | ||
} | ||
data += chunk; | ||
}); | ||
// called when the complete response is received. | ||
res.on("end", function () { return resolve(data); }); | ||
}).on("error", reject); | ||
}); | ||
}; | ||
var _default = nodeFetcher; | ||
exports.default = _default; | ||
exports["default"] = nodeFetcher; | ||
//# sourceMappingURL=nodeFetcher.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
var _readyState = require("./readyState"); | ||
var _status = require("./status"); | ||
var xmlHttpRequestFetcher = function xmlHttpRequestFetcher(url) { | ||
return new Promise(function (resolve, reject) { | ||
var xhr = new XMLHttpRequest(); | ||
xhr.onreadystatechange = function () { | ||
if (xhr.readyState !== _readyState.DONE) return; | ||
xhr.status === _status.OK ? resolve(xhr.responseText) : reject("".concat(xhr.status, " ").concat(xhr.statusText)); | ||
}; | ||
xhr.open("GET", url, true); | ||
xhr.send(); | ||
}); | ||
exports.__esModule = true; | ||
var readyState_1 = require("./readyState"); | ||
var status_1 = require("./status"); | ||
var xmlHttpRequestFetcher = function (url) { | ||
return new Promise(function (resolve, reject) { | ||
var xhr = new XMLHttpRequest(); | ||
xhr.onreadystatechange = function () { | ||
if (xhr.readyState !== readyState_1.DONE) | ||
return; | ||
xhr.status === status_1.OK | ||
? resolve(xhr.responseText) | ||
: reject(xhr.status + " " + xhr.statusText); | ||
}; | ||
xhr.open("GET", url, true); | ||
xhr.send(); | ||
}); | ||
}; | ||
var _default = xmlHttpRequestFetcher; | ||
exports.default = _default; | ||
exports["default"] = xmlHttpRequestFetcher; | ||
//# sourceMappingURL=index.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
exports.DONE = exports.OPENED = exports.UNSENT = void 0; | ||
var UNSENT = 0; // Client has been created. open() not called yet. | ||
exports.UNSENT = UNSENT; | ||
var OPENED = 1; // open() has been called. | ||
exports.UNSENT = 0; // Client has been created. open() not called yet. | ||
exports.OPENED = 1; // open() has been called. | ||
// export const HEADERS_RECEIVED = 2; // send() has been called, and headers and status are available. | ||
// export const LOADING = 3; // Downloading; responseText holds partial data. | ||
exports.OPENED = OPENED; | ||
var DONE = 4; // The operation is complete. | ||
exports.DONE = DONE; | ||
exports.DONE = 4; // The operation is complete. | ||
//# sourceMappingURL=readyState.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
exports.InternalServerError = exports.OK = void 0; | ||
// export const Continue = 100; | ||
// export const Switchingprotocols = 101; | ||
var OK = 200; // export const Created = 201; | ||
exports.OK = 200; | ||
// export const Created = 201; | ||
// export const Accepted = 202; | ||
@@ -40,5 +38,4 @@ // export const NonAuthoritativeInformation = 203; | ||
// export const ExpectationFailed = 417; | ||
exports.OK = OK; | ||
var InternalServerError = 500; // export const NotImplemented = 501; | ||
exports.InternalServerError = 500; | ||
// export const NotImplemented = 501; | ||
// export const BadGateway = 502; | ||
@@ -48,4 +45,2 @@ // export const ServiceUnavailable = 503; | ||
// export const HTTPVersionNotSupported = 505; | ||
exports.InternalServerError = InternalServerError; | ||
//# sourceMappingURL=status.js.map |
{ | ||
"name": "@paciolan/remote-module-loader", | ||
"version": "2.5.3", | ||
"version": "2.6.0", | ||
"description": "", | ||
@@ -27,3 +27,5 @@ "main": "dist/index.js", | ||
"scripts": { | ||
"build": "npm run clean && npx babel src --ignore **/*.test.js --out-dir dist --source-maps", | ||
"prebuild": "npm run clean", | ||
"build": "tsc --declaration", | ||
"watch": "npm run build -- --watch", | ||
"clean": "rimraf dist", | ||
@@ -41,5 +43,7 @@ "cz": "git-cz", | ||
"@babel/preset-env": "^7.5.5", | ||
"@babel/preset-typescript": "^7.12.7", | ||
"@commitlint/cli": "^8.1.0", | ||
"@commitlint/config-conventional": "^8.1.0", | ||
"@types/jest": "^24.0.17", | ||
"@types/jest": "^24.9.1", | ||
"@types/node": "^14.14.10", | ||
"babel-loader": "^8.0.6", | ||
@@ -55,3 +59,5 @@ "cross-env": "^5.2.0", | ||
"regenerator-runtime": "^0.13.3", | ||
"rimraf": "^2.6.3" | ||
"rimraf": "^2.6.3", | ||
"ts-jest": "^26.4.4", | ||
"typescript": "^4.1.2" | ||
}, | ||
@@ -58,0 +64,0 @@ "husky": { |
# Remote Module Loader ![coverage:100%](https://img.shields.io/badge/coverage-100%25-brightgreen.svg) | ||
Loads a CommonJS module from a remote URL for the Browser or Node.js. | ||
![Lunar Module](https://raw.githubusercontent.com/Paciolan/remote-module-loader/master/media/logo-small.png) | ||
Loads a CommonJS module from a remote url. | ||
# Use Cases | ||
@@ -62,4 +62,6 @@ | ||
By default `loadRemoteModule` will use the `XMLHttpRequest` object avaiable in the browser. This can be overridden if you want to use an alternate method. | ||
The default loader can be overridden if you want to use an alternate method. | ||
This example uses `axios` for the fetcher. | ||
```javascript | ||
@@ -126,4 +128,2 @@ /** | ||
```javascript | ||
var name = "myModule"; | ||
function helloWorld() { | ||
@@ -133,3 +133,2 @@ console.log("Hello World!"); | ||
exports.name = name; | ||
exports.default = helloWorld; | ||
@@ -152,3 +151,3 @@ ``` | ||
Setting up Webpack to export a CommonJS is pretty easy. | ||
Webpack can be setup to export as CommonJS. | ||
@@ -165,5 +164,5 @@ Inside `webpack.config.js`, set the `libraryTarget` to `"commonjs"`. | ||
Dependencies that will be provided by the Web Application that uses your Remote Module can be added to webpack's `externals` section. | ||
Dependencies should be excluded from the bundle because they will be provided by the Web Application can be added to webpack's `externals` section. | ||
This will prevent webpack from bundling unwanted 3rd party libraries, decreasing the bundle size. | ||
This will prevent webpack from bundling duplicate 3rd party libraries, decreasing the bundle size. | ||
@@ -176,4 +175,3 @@ ```javascript | ||
externals: { | ||
react: "react", | ||
"prop-types": "prop-types" | ||
react: "react" | ||
} | ||
@@ -180,0 +178,0 @@ }; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33
329
22294
21
180