aboutsummaryrefslogtreecommitdiff
path: root/node_modules/time-require/src/requireHook.js
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2017-05-28 00:38:50 +0200
committerFlorian Dold <florian.dold@gmail.com>2017-05-28 00:40:43 +0200
commit7fff4499fd915bcea3fa93b1aa8b35f4fe7a6027 (patch)
tree6de9a1aebd150a23b7f8c273ec657a5d0a18fe3e /node_modules/time-require/src/requireHook.js
parent963b7a41feb29cc4be090a2446bdfe0c1f1bcd81 (diff)
add linting (and some initial fixes)
Diffstat (limited to 'node_modules/time-require/src/requireHook.js')
-rw-r--r--node_modules/time-require/src/requireHook.js93
1 files changed, 93 insertions, 0 deletions
diff --git a/node_modules/time-require/src/requireHook.js b/node_modules/time-require/src/requireHook.js
new file mode 100644
index 000000000..ebafbb603
--- /dev/null
+++ b/node_modules/time-require/src/requireHook.js
@@ -0,0 +1,93 @@
+/**
+ * requireHook - module wrapping a function that register require() listener and returns hook/unhook control
+ *
+ * @author Ciprian Popa (cyparu)
+ * @since 0.0.1
+ * @version 0.0.1
+ */
+
+"use strict";
+
+var Module = require("module").Module,
+ _load = Module._load,
+ _hookedAt, _listener;
+
+/**
+ * Module hooker function that will replace Module._load and invoke the _listener with module and timing information
+ *
+ * @function _hooker
+ */
+function _hooker(name, parent) {
+ var timeIn = Date.now(),
+ exports = _load.apply(Module, arguments),
+ timeOut = Date.now(),
+ mod = parent.children[parent.children.length - 1]; // should be the last loaded children
+ // call the listener
+ _listener({
+ name: name,
+ parent: parent,
+ module: mod,
+ filename: mod ? mod.filename : name,
+ exports: exports,
+ requiredOn: timeIn,
+ startedIn: timeOut - timeIn
+ });
+ return exports;
+}
+
+/**
+ * Hook Node's require() so the configured callback will be invocked with additional module and time loading information information
+ *
+ * @param {Function} [listener] - optional listener if
+ * @method hook
+ */
+function _hook(listener) {
+ if (typeof listener !== "undefined") {
+ if (typeof listener !== "function") {
+ throw new Error("The optional parameter for hook() should be a function but was " + (typeof listener));
+ }
+ // set the listener
+ _listener = listener;
+ }
+ // set the hoocker loader
+ Module._load = _hooker;
+ // mark hooked time
+ _hookedAt = new Date();
+}
+
+/**
+ * Unhook Node's require() to the original function
+ *
+ * @method unhook
+ */
+function _unhook() {
+ // set the original loader
+ Module._load = _load;
+ // reset hooking time
+ _hookedAt = undefined;
+}
+
+/**
+ * Export a function that set the callback and return hook/unhook control functionality
+ *
+ * @function
+ * @param {Function} listener - require() listener
+ * @param {Boolean} [autohook=true] - optional flag telling if the hooking will be started automatically
+ * @return hook/unhook control function
+ */
+module.exports = function(listener, autohook) {
+ if (typeof listener !== "function") {
+ throw new Error("The hooking function should be set");
+ }
+ // set the listener
+ _listener = listener;
+ // if autohook (by default),
+ if (autohook !== false) {
+ _hook();
+ }
+ return {
+ hookedAt: _hookedAt,
+ hook: _hook,
+ unhook: _unhook
+ };
+};