Skip to content

WIP: Module Reloading Stratagies#3809

Closed
ScriptedAlchemy wants to merge 15 commits intomainfrom
module-reloaders
Closed

WIP: Module Reloading Stratagies#3809
ScriptedAlchemy wants to merge 15 commits intomainfrom
module-reloaders

Conversation

@ScriptedAlchemy
Copy link
Member

@ScriptedAlchemy ScriptedAlchemy commented May 31, 2025

Description

Investigating better stratagies for hot reloading node servers when federated modules change.

Related Issue

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I have updated the documentation.

@changeset-bot
Copy link

changeset-bot bot commented May 31, 2025

⚠️ No Changeset found

Latest commit: 28a003a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@netlify
Copy link

netlify bot commented May 31, 2025

Deploy Preview for module-federation-docs ready!

Name Link
🔨 Latest commit 28a003a
🔍 Latest deploy log https://app.netlify.com/projects/module-federation-docs/deploys/68804bf69facbc0008268914
😎 Deploy Preview https://deploy-preview-3809--module-federation-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

const vm = require('vm');

// Webpack runtime code for in-memory HMR - only executed when webpack is available
function injectInMemoryHMRRuntime(__webpack_require__) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy over the runtime module from HMR plugin - the plugin (in node targets) is designed to read filesystem only to check for updates. I replace some of the methods to allow in-memory patches, so that i could fetch() the update from a KV store or database etc and not need to manually write to disk.

/******/
/******/ // no external install chunk
/******/
/******/ function loadUpdateChunk(chunkId, updatedModulesList) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

node federation plugin already patches require.f.readFileVM but i never updated it to include the HMR stuff - in future this code could live in the node runtime plugin where other chunk loading stuff already exists.

const event = {
id: `evt_${now}_${i}`,
type: EVENT_TYPES[Math.floor(Math.random() * EVENT_TYPES.length)],
userId: `user_${Math.floor(Math.random() * 100)}`,

Check failure

Code scanning / CodeQL

Insecure randomness High

This uses a cryptographically insecure random number generated at
Math.random()
in a security context.

Copilot Autofix

AI 8 months ago

To fix the issue, replace the use of Math.random() with a cryptographically secure random number generator. In Node.js, the crypto module provides a secure way to generate random values. Specifically, crypto.randomBytes can be used to generate random bytes, which can then be converted into a number or string as needed.

For the user ID generation, we can use crypto.randomBytes to generate a random number or string that is sufficiently unpredictable. This ensures that the user ID cannot be easily guessed or brute-forced.


Suggested changeset 1
apps/vm-hotreload/webpack-custom-root/src/analytics.js

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/apps/vm-hotreload/webpack-custom-root/src/analytics.js b/apps/vm-hotreload/webpack-custom-root/src/analytics.js
--- a/apps/vm-hotreload/webpack-custom-root/src/analytics.js
+++ b/apps/vm-hotreload/webpack-custom-root/src/analytics.js
@@ -2,2 +2,3 @@
 const Logger = require('./utils/logger');
+const crypto = require('crypto');
 const DataManager = require('./utils/dataManager');
@@ -141,3 +142,3 @@
       type: EVENT_TYPES[Math.floor(Math.random() * EVENT_TYPES.length)],
-      userId: `user_${Math.floor(Math.random() * 100)}`,
+      userId: `user_${crypto.randomBytes(4).toString('hex')}`,
       sessionId: `session_${Math.floor(Math.random() * 20)}`,
EOF
@@ -2,2 +2,3 @@
const Logger = require('./utils/logger');
const crypto = require('crypto');
const DataManager = require('./utils/dataManager');
@@ -141,3 +142,3 @@
type: EVENT_TYPES[Math.floor(Math.random() * EVENT_TYPES.length)],
userId: `user_${Math.floor(Math.random() * 100)}`,
userId: `user_${crypto.randomBytes(4).toString('hex')}`,
sessionId: `session_${Math.floor(Math.random() * 20)}`,
Copilot is powered by AI and may make mistakes. Always verify output.
id: `evt_${now}_${i}`,
type: EVENT_TYPES[Math.floor(Math.random() * EVENT_TYPES.length)],
userId: `user_${Math.floor(Math.random() * 100)}`,
sessionId: `session_${Math.floor(Math.random() * 20)}`,

Check failure

Code scanning / CodeQL

Insecure randomness High

This uses a cryptographically insecure random number generated at
Math.random()
in a security context.

Copilot Autofix

AI 8 months ago

To fix the issue, we need to replace the use of Math.random() with a cryptographically secure random number generator. In Node.js, the crypto module provides a secure method for generating random values. Specifically, we can use crypto.randomBytes to generate random bytes and convert them into a secure session ID.

The fix involves:

  1. Importing the crypto module.
  2. Replacing the insecure Math.random() logic with a secure random value generated using crypto.randomBytes.
  3. Ensuring the session ID remains in the same format as before (e.g., prefixed with session_).

Suggested changeset 1
apps/vm-hotreload/webpack-custom-root/src/analytics.js

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/apps/vm-hotreload/webpack-custom-root/src/analytics.js b/apps/vm-hotreload/webpack-custom-root/src/analytics.js
--- a/apps/vm-hotreload/webpack-custom-root/src/analytics.js
+++ b/apps/vm-hotreload/webpack-custom-root/src/analytics.js
@@ -3,2 +3,3 @@
 const DataManager = require('./utils/dataManager');
+const crypto = require('crypto');
 const Metrics = require('./utils/metrics');
@@ -142,3 +143,3 @@
       userId: `user_${Math.floor(Math.random() * 100)}`,
-      sessionId: `session_${Math.floor(Math.random() * 20)}`,
+      sessionId: `session_${crypto.randomBytes(4).toString('hex')}`,
       timestamp: new Date(now - Math.random() * 86400000).toISOString(), // Last 24 hours
EOF
@@ -3,2 +3,3 @@
const DataManager = require('./utils/dataManager');
const crypto = require('crypto');
const Metrics = require('./utils/metrics');
@@ -142,3 +143,3 @@
userId: `user_${Math.floor(Math.random() * 100)}`,
sessionId: `session_${Math.floor(Math.random() * 20)}`,
sessionId: `session_${crypto.randomBytes(4).toString('hex')}`,
timestamp: new Date(now - Math.random() * 86400000).toISOString(), // Last 24 hours
Copilot is powered by AI and may make mistakes. Always verify output.
Comment on lines +265 to +267
app.get('/admin', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'admin.html'));
});

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
a file system access
, but is not rate-limited.

Copilot Autofix

AI 7 months ago

To address the issue, we will introduce rate limiting to the application using the express-rate-limit package. This package allows us to define a rate-limiting middleware that restricts the number of requests a client can make to the server within a specified time window. Specifically, we will:

  1. Install the express-rate-limit package.
  2. Configure a rate limiter with appropriate settings (e.g., a maximum of 100 requests per 15 minutes).
  3. Apply the rate limiter middleware to the /admin route to limit access to the admin.html file.

This fix ensures that the /admin route is protected against excessive requests, reducing the risk of a DoS attack.


Suggested changeset 2
apps/vm-hotreload/full-demo/backend/server.js

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/apps/vm-hotreload/full-demo/backend/server.js b/apps/vm-hotreload/full-demo/backend/server.js
--- a/apps/vm-hotreload/full-demo/backend/server.js
+++ b/apps/vm-hotreload/full-demo/backend/server.js
@@ -6,2 +6,3 @@
 const WebSocket = require('ws');
+const rateLimit = require('express-rate-limit');
 
@@ -264,3 +265,7 @@
 // Serve admin interface
-app.get('/admin', (req, res) => {
+const adminLimiter = rateLimit({
+  windowMs: 15 * 60 * 1000, // 15 minutes
+  max: 100, // Limit each IP to 100 requests per windowMs
+});
+app.get('/admin', adminLimiter, (req, res) => {
   res.sendFile(path.join(__dirname, 'public', 'admin.html'));
EOF
@@ -6,2 +6,3 @@
const WebSocket = require('ws');
const rateLimit = require('express-rate-limit');

@@ -264,3 +265,7 @@
// Serve admin interface
app.get('/admin', (req, res) => {
const adminLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // Limit each IP to 100 requests per windowMs
});
app.get('/admin', adminLimiter, (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'admin.html'));
apps/vm-hotreload/full-demo/backend/package.json
Outside changed files

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/apps/vm-hotreload/full-demo/backend/package.json b/apps/vm-hotreload/full-demo/backend/package.json
--- a/apps/vm-hotreload/full-demo/backend/package.json
+++ b/apps/vm-hotreload/full-demo/backend/package.json
@@ -13,3 +13,4 @@
     "body-parser": "^1.20.2",
-    "ws": "^8.14.2"
+    "ws": "^8.14.2",
+    "express-rate-limit": "^8.0.1"
   },
EOF
@@ -13,3 +13,4 @@
"body-parser": "^1.20.2",
"ws": "^8.14.2"
"ws": "^8.14.2",
"express-rate-limit": "^8.0.1"
},
This fix introduces these dependencies
Package Version Security advisories
express-rate-limit (npm) 8.0.1 None
Copilot is powered by AI and may make mistakes. Always verify output.
@2heal1
Copy link
Member

2heal1 commented Sep 5, 2025

This pr is inactive for a longtime, closed first . If wanna be merged , just reopen :D

@2heal1 2heal1 closed this Sep 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants