load-remote-module 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +97 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +19 -0
- package/package.json +19 -0
- package/src/index.d.ts +8 -0
- package/src/index.ts +25 -0
- package/tsconfig.json +15 -0
package/README.md
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# load-remote-module
|
|
2
|
+
|
|
3
|
+
Load JavaScript modules dynamically from remote URLs — safely, asynchronously, and with zero configuration.
|
|
4
|
+
|
|
5
|
+
`load-remote-module` lets you fetch and execute remote ES modules at runtime.
|
|
6
|
+
Perfect for plugin systems, dynamic feature loading, micro‑frontends, or any scenario where your code needs to pull logic from the outside world.
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## ✨ Features
|
|
11
|
+
|
|
12
|
+
- 🚀 **Load ES modules from any URL**
|
|
13
|
+
- 🔒 **Isolated execution** using native dynamic `import()`
|
|
14
|
+
- 🧩 **Works with any module** that exports functions, objects, classes, etc.
|
|
15
|
+
- ⏱️ **Promise‑based API**
|
|
16
|
+
- 🪶 **Tiny & dependency‑free**
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## 📦 Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install load-remote-module
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## 🛠️ Usage
|
|
29
|
+
|
|
30
|
+
### Basic Example
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
import loadRemoteModule from 'load-remote-module';
|
|
34
|
+
|
|
35
|
+
async function test() {
|
|
36
|
+
const mod = await loadRemoteModule('https://test.com/test');
|
|
37
|
+
|
|
38
|
+
console.log(mod.sayHello('Alice'));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
test();
|
|
42
|
+
```
|
|
43
|
+
### What the remote module might look like
|
|
44
|
+
```js
|
|
45
|
+
function sayHello(name) {
|
|
46
|
+
return `Hello, ${name}!`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
module.exports = { sayHello };
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
## 📚 API
|
|
55
|
+
|
|
56
|
+
### `loadRemoteModule(url: string): Promise<any>`
|
|
57
|
+
Loads a remote ES module and returns its exported object.
|
|
58
|
+
|
|
59
|
+
### Parameters
|
|
60
|
+
|
|
61
|
+
| Name | Type | Description |
|
|
62
|
+
|------|--------|------------------------------------------|
|
|
63
|
+
| url | string | Full URL to the remote JavaScript module |
|
|
64
|
+
### Returns
|
|
65
|
+
A Promise resolving to the module’s exports.
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
## ⚠️ Security Notes
|
|
70
|
+
Loading remote code always carries risk.
|
|
71
|
+
You should only load modules from trusted sources and consider implementing:
|
|
72
|
+
|
|
73
|
+
- URL allowlists
|
|
74
|
+
|
|
75
|
+
- Integrity checks
|
|
76
|
+
|
|
77
|
+
- Version pinning
|
|
78
|
+
|
|
79
|
+
- Sandboxing strategies
|
|
80
|
+
|
|
81
|
+
---
|
|
82
|
+
|
|
83
|
+
## 🧪 Example Use Cases
|
|
84
|
+
- Plugin systems
|
|
85
|
+
|
|
86
|
+
- Dynamic feature flags
|
|
87
|
+
|
|
88
|
+
- Remote configuration logic
|
|
89
|
+
|
|
90
|
+
- Micro‑frontend architectures
|
|
91
|
+
|
|
92
|
+
- A/B testing logic loaded on demand
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
## 📄 License
|
|
97
|
+
MIT — feel free to use it however you like.
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
const axios_1 = __importDefault(require("axios"));
|
|
6
|
+
const module_1 = require("module");
|
|
7
|
+
async function loadRemoteModule(url, options) {
|
|
8
|
+
if (typeof url !== 'string') {
|
|
9
|
+
throw new TypeError('Expected a string URL');
|
|
10
|
+
}
|
|
11
|
+
const res = await (0, axios_1.default)({ method: 'GET', url, ...options });
|
|
12
|
+
const code = res.data.toString();
|
|
13
|
+
const m = new module_1.Module(url);
|
|
14
|
+
m.filename = url;
|
|
15
|
+
// internal Node API
|
|
16
|
+
m._compile(code, url);
|
|
17
|
+
return m.exports;
|
|
18
|
+
}
|
|
19
|
+
module.exports = loadRemoteModule;
|
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "load-remote-module",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"author": "Antonio",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"axios": "^1.7.0"
|
|
13
|
+
},
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@types/node": "^20.0.0",
|
|
16
|
+
"ts-node": "^10.9.1",
|
|
17
|
+
"typescript": "^5.2.0"
|
|
18
|
+
}
|
|
19
|
+
}
|
package/src/index.d.ts
ADDED
package/src/index.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import axios, { AxiosRequestConfig } from 'axios';
|
|
2
|
+
import { Module } from 'module';
|
|
3
|
+
|
|
4
|
+
async function loadRemoteModule(
|
|
5
|
+
url: string,
|
|
6
|
+
options?: AxiosRequestConfig
|
|
7
|
+
): Promise<any> {
|
|
8
|
+
if (typeof url !== 'string') {
|
|
9
|
+
throw new TypeError('Expected a string URL');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const res = await axios({ method: 'GET', url, ...options });
|
|
13
|
+
const code = res.data.toString();
|
|
14
|
+
|
|
15
|
+
const m = new Module(url);
|
|
16
|
+
m.filename = url;
|
|
17
|
+
|
|
18
|
+
// internal Node API
|
|
19
|
+
(m as any)._compile(code, url);
|
|
20
|
+
|
|
21
|
+
return m.exports;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// 🔥 IMPORTANT: CommonJS export
|
|
25
|
+
export = loadRemoteModule;
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "CommonJS",
|
|
5
|
+
"moduleResolution": "node",
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"declaration": true,
|
|
8
|
+
"outDir": "./dist",
|
|
9
|
+
"strict": true
|
|
10
|
+
},
|
|
11
|
+
"include": ["src/**/*"],
|
|
12
|
+
"exclude": [
|
|
13
|
+
"node_modules"
|
|
14
|
+
]
|
|
15
|
+
}
|