'use strict';

const debug = require('debug');
const d = debug('breadboard:setup');

module.exports = (substitutes) => {
  const natives = Object.keys(process.binding('natives'));

  return new Promise((resolve) => {
    const nativeModules = natives.reduce((nms, nativeModuleName) => {
      if (substitutes.some(substituteName => substituteName === nativeModuleName)) {
        return nms;
      }
      if (!/^internal\/|_/.test(nativeModuleName)) {
        Object.defineProperty(nms, nativeModuleName, {
          enumerable: true,
          get: () => {
            d(`Requiring ${nativeModuleName}`);

            return require(nativeModuleName);
          }
        });
      }

      return nms;
    }, {});
    d('Native modules', Object.keys(nativeModules));

    return resolve(nativeModules);
  });
};

@@ -81,350 +81,158 @@
 ');%0A
-%0Amodule.exports = (substitutes) =%3E %7B%0A  const natives = Object.keys(process.binding('natives'));%0A%0A  return new Promise((resolve) =%3E %7B%0A    const nativeModules = natives.reduce((nms, nativeModuleName) =%3E %7B%0A      if (substitutes.some(substituteName =%3E substituteName === nativeModuleName)) %7B%0A        return nms;%0A      %7D%0A      if (!/%5Einternal%5C/%7C_/
+const loadSanitisedNativeModules = (modules, nativeModuleName) =%3E %7B%0A  const excludedNativesRegExp = /%5Einternal%5C/%7C_%7Cv8%5C//;%0A  if (!excludedNativesRegExp
 .tes
@@ -258,20 +258,16 @@
 ) %7B%0A    
- 
-   
 Object.d
@@ -280,18 +280,22 @@
 roperty(
-n
 m
+odule
 s, nativ
@@ -315,20 +315,16 @@
 %7B%0A      
-  
-  
 enumerab
@@ -333,20 +333,16 @@
 : true,%0A
-    
       ge
@@ -360,20 +360,16 @@
         
-    
 d(%60Requi
@@ -405,20 +405,16 @@
         
- 
-   
 return r
@@ -449,58 +449,426 @@
     
+%7D%0A
     %7D
+);
 %0A  
-      %7D);%0A      %7D%0A%0A      return nms;%0A    %7D
+%7D%0A%0A  return modules;%0A%7D;%0Aconst excludeSubstitutes = (modules, substitutes) =%3E %7B%0A  return modules.filter(module =%3E !substitutes.some(substitute =%3E substitute === module));%0A%7D;%0A%0Amodule.exports = (substitutes) =%3E %7B%0A  const natives = excludeSubstitutes(Object.keys(process.binding('natives')), substitutes);%0A%0A  return new Promise((resolve) =%3E %7B%0A    const nativeModules = natives.reduce(loadSanitisedNativeModules
 , %7B%7D

exclude V8/ from native Node modules