@ -461,8 +461,7 @@ function existsSync (path) {
process . paths = [ path . join ( process . installPrefix , "lib/node/libraries" )
process . paths = [ ] ;
] ;
if ( process . env [ "HOME" ] ) {
if ( process . env [ "HOME" ] ) {
process . paths . unshift ( path . join ( process . env [ "HOME" ] , ".node_libraries" ) ) ;
process . paths . unshift ( path . join ( process . env [ "HOME" ] , ".node_libraries" ) ) ;
@ -553,6 +552,8 @@ function resolveModulePath(request, parent) {
var id , paths ;
var id , paths ;
if ( request . charAt ( 0 ) == "." && ( request . charAt ( 1 ) == "/" || request . charAt ( 1 ) == "." ) ) {
if ( request . charAt ( 0 ) == "." && ( request . charAt ( 1 ) == "/" || request . charAt ( 1 ) == "." ) ) {
// Relative request
// Relative request
debug ( "RELATIVE: requested:" + request + " set ID to: " + id + " from " + parent . id ) ;
var exts = [ 'js' , 'node' ] , ext ;
var exts = [ 'js' , 'node' ] , ext ;
for ( ext in extensionCache ) {
for ( ext in extensionCache ) {
exts . push ( ext . slice ( 1 ) ) ;
exts . push ( ext . slice ( 1 ) ) ;
@ -561,7 +562,6 @@ function resolveModulePath(request, parent) {
var parentIdPath = path . dirname ( parent . id +
var parentIdPath = path . dirname ( parent . id +
( path . basename ( parent . filename ) . match ( new RegExp ( '^index\\.(' + exts . join ( '|' ) + ')$' ) ) ? "/" : "" ) ) ;
( path . basename ( parent . filename ) . match ( new RegExp ( '^index\\.(' + exts . join ( '|' ) + ')$' ) ) ? "/" : "" ) ) ;
id = path . join ( parentIdPath , request ) ;
id = path . join ( parentIdPath , request ) ;
// debug("RELATIVE: requested:"+request+" set ID to: "+id+" from "+parent.id+"("+parentIdPath+")");
paths = [ path . dirname ( parent . filename ) ] ;
paths = [ path . dirname ( parent . filename ) ] ;
} else {
} else {
id = request ;
id = request ;
@ -573,20 +573,40 @@ function resolveModulePath(request, parent) {
}
}
function loadModuleSync ( request , parent ) {
function loadModule ( request , parent , callback ) {
var resolvedModule = resolveModulePath ( request , parent ) ;
var resolvedModule = resolveModulePath ( request , parent ) ,
var id = resolvedModule [ 0 ] ;
id = resolvedModule [ 0 ] ,
var paths = resolvedModule [ 1 ] ;
paths = resolvedModule [ 1 ] ;
debug ( "loadModuleSync REQUEST " + ( request ) + " parent: " + parent . id ) ;
debug ( "loadModule REQUEST " + ( request ) + " parent: " + parent . id ) ;
var cachedModule = internalModuleCache [ id ] || parent . moduleCache [ id ] ;
var cachedModule = internalModuleCache [ id ] || parent . moduleCache [ id ] ;
if ( ! cachedModule ) {
// Try to compile from native modules
if ( process . natives [ id ] ) {
debug ( 'load native module ' + id ) ;
cachedModule = new Module ( id ) ;
var e = cachedModule . _ compile ( process . natives [ id ] , id ) ;
if ( e ) throw e ;
internalModuleCache [ id ] = cachedModule ;
}
}
if ( cachedModule ) {
if ( cachedModule ) {
debug ( "found " + JSON . stringify ( id ) + " in cache" ) ;
debug ( "found " + JSON . stringify ( id ) + " in cache" ) ;
if ( callback ) {
callback ( null , cachedModule . exports ) ;
} else {
return cachedModule . exports ;
return cachedModule . exports ;
}
} else {
} else {
// Not in cache
debug ( "looking for " + JSON . stringify ( id ) + " in " + JSON . stringify ( paths ) ) ;
debug ( "looking for " + JSON . stringify ( id ) + " in " + JSON . stringify ( paths ) ) ;
if ( ! callback ) {
// sync
var filename = findModulePath ( request , paths ) ;
var filename = findModulePath ( request , paths ) ;
if ( ! filename ) {
if ( ! filename ) {
throw new Error ( "Cannot find module '" + request + "'" ) ;
throw new Error ( "Cannot find module '" + request + "'" ) ;
@ -595,35 +615,20 @@ function loadModuleSync (request, parent) {
module . loadSync ( filename ) ;
module . loadSync ( filename ) ;
return module . exports ;
return module . exports ;
}
}
}
}
function loadModule ( request , parent , callback ) {
var
resolvedModule = resolveModulePath ( request , parent ) ,
id = resolvedModule [ 0 ] ,
paths = resolvedModule [ 1 ] ;
debug ( "loadModule REQUEST " + ( request ) + " parent: " + parent . id ) ;
var cachedModule = internalModuleCache [ id ] || parent . moduleCache [ id ] ;
if ( cachedModule ) {
debug ( "found " + JSON . stringify ( id ) + " in cache" ) ;
if ( callback ) callback ( null , cachedModule . exports ) ;
} else {
} else {
debug ( "looking for " + JSON . stringify ( id ) + " in " + JSON . stringify ( paths ) ) ;
// async
// Not in cache
findModulePath ( request , paths , function ( filename ) {
findModulePath ( request , paths , function ( filename ) {
if ( ! filename ) {
if ( ! filename ) {
var err = new Error ( "Cannot find module '" + request + "'" ) ;
var err = new Error ( "Cannot find module '" + request + "'" ) ;
if ( callback ) callback ( err ) ;
callback ( err ) ;
} else {
} else {
var module = new Module ( id , parent ) ;
var module = new Module ( id , parent ) ;
module . load ( filename , callback ) ;
module . load ( filename , callback ) ;
}
}
} ) ;
} ) ;
}
}
}
} ;
} ;
@ -713,7 +718,8 @@ function cat (id, callback) {
}
}
Module . prototype . _ loadContent = function ( content , filename ) {
// Returns exception if any
Module . prototype . _ compile = function ( content , filename ) {
var self = this ;
var self = this ;
// remove shebang
// remove shebang
content = content . replace ( /^\#\!.*/ , '' ) ;
content = content . replace ( /^\#\!.*/ , '' ) ;
@ -729,7 +735,7 @@ Module.prototype._loadContent = function (content, filename) {
}
}
function require ( path ) {
function require ( path ) {
return loadModuleSync ( path , self ) ;
return loadModule ( path , self ) ;
}
}
require . paths = process . paths ;
require . paths = process . paths ;
@ -765,7 +771,7 @@ Module.prototype._loadScriptSync = function (filename) {
// remove shebang
// remove shebang
content = content . replace ( /^\#\!.*/ , '' ) ;
content = content . replace ( /^\#\!.*/ , '' ) ;
var e = this . _ loadContent ( content , filename ) ;
var e = this . _ compile ( content , filename ) ;
if ( e ) {
if ( e ) {
throw e ;
throw e ;
} else {
} else {
@ -781,7 +787,7 @@ Module.prototype._loadScript = function (filename, callback) {
if ( err ) {
if ( err ) {
if ( callback ) callback ( err ) ;
if ( callback ) callback ( err ) ;
} else {
} else {
var e = self . _ loadContent ( content , filename ) ;
var e = self . _ compile ( content , filename ) ;
if ( e ) {
if ( e ) {
if ( callback ) callback ( e ) ;
if ( callback ) callback ( e ) ;
} else {
} else {