MCPcopy Create free account
hub / github.com/socketstream/socketstream / requires

Function requires

lib/utils/require.js:27–103  ·  view source on GitHub ↗
(id, contextPath, defaultId)

Source from the content-addressed store, hash-verified

25 module isn't found. If it returns something that will be the resolved module.
26 */
27 function requires(id, contextPath, defaultId) {
28 var context, callback;
29 if (typeof defaultId === 'function') {
30 callback = defaultId;
31 defaultId = null;
32 }
33 if (contextPath) {
34 context = contexts[contextPath];
35 if (context === undefined) {
36 context = contexts[contextPath] = {
37 rel : context,
38 prefix: path.join(__dirname, '..', contextPath),
39 inContext: function(id) {
40 try {
41 var p = path.join(context.prefix,id);
42 if (require.resolve(p)) { // gives full path
43 return require(p);
44 }
45 } catch(ex) {}
46 }
47 };
48 }
49 }
50
51 switch(typeof id) {
52 case 'object':
53 case 'function':
54 if (id) {
55 return id; // straight object/function will just resolve straight away
56 }
57 break;
58 case 'string':
59 if (context) {
60 // builtin modules take first priority
61 var inContext = context.inContext(id);
62 if (inContext) { return inContext; }
63
64 }
65
66 // if relative base on main script location
67 if (id.charAt(0) === '.') {
68 var inProject = path.join(ss.root,id);
69 if (fs.existsSync(inProject+'.js')) {
70 debug('found '+id);
71 return require(inProject);
72 }
73 }
74
75 // getting a packaged module
76 var mod = projectOrHereRequire(id,ss.root);
77 if (mod) {
78 debug('found '+id+' in project or SocketStream');
79 return require(mod);
80 }
81 break;
82 }
83
84 if (context && defaultId) {

Callers

nothing calls this directly

Calls 1

projectOrHereRequireFunction · 0.85

Tested by

no test coverage detected