-
Notifications
You must be signed in to change notification settings - Fork 0
/
node-library.js
279 lines (194 loc) · 6.71 KB
/
node-library.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
var path = require("path")
if (Error.stackTraceLimit == 10) {
Error.stackTraceLimit = 30
}
var generateConstructor = require("./library")
var Tree = require("string-tree")
var Library = generateConstructor(Tree)
global._wtf =
function wtf(whatnot) {
if (typeof whatnot == "function") {
return whatnot.toString()
} else if (typeof whatnot == "object") {
if (Array.isArray(whatnot)) {
return JSON.stringify(whatnot)
}
var keys = Object.keys(whatnot)
for(var key in whatnot) {
if (keys.indexOf(key) == -1) {
keys.push(key)
}
}
return "[ object "+whatnot.constructor.name+" with keys "+keys.join(", ")+" ]"
} else {
return JSON.stringify(whatnot)
}
}
global._wtf.log =
function consoleLogWtfSomethingIs(whatnot) {
console.log(_wtf(whatnot))
}
global._wtf.json =
function consoleLogSomethingToJSON(whatnot) {
console.log(JSON.stringify(whatnot, null, 2))
}
// Debugging
Library.prototype.dump = function(logger) {
(logger || console.log)("library", JSON.stringify(this._dump(true), null, 2))
if (this != this.root) {
this.root._dump(true)
}
}
Library.prototype._dump = function(isRoot) {
var names = Object.keys(this.singletonCache)
if (this.parent) {
names = names.filter(differentThanParent.bind(null, this, this.parent))
}
function differentThanParent(child, parent, name) {
if (!parent) { return true }
return child.singletonCache[name] != parent.singletonCache[name]
}
var singletons = this.singletonCache
var singletonLabels = names.map(
function(name) {
var label = name
var id = singletons[name].__nrtvId
if (id) {
name += "@"+id
}
return name
}
)
var kids = this.children.map(function(child) { return child._dump(false) })
var dump = {
id: this.id
}
if (isRoot) {
dump.root = true
dump.modules = Object.keys(this.modules)
}
dump.singletons = singletonLabels
if (kids.length > 0) {
dump.children = kids
}
return dump
}
// Exports
Library.prototype.export =
function() {
var module = this.define.apply(this, arguments)
module.require = this.require
var singleton = this._generateSingleton(module)
return singleton
}
try {
var dir = __dirname
var fullPath = path.join(__dirname, "..", "..", "package.json")
var package = require(fullPath)
} catch (e) {}
Library.useLoader(
function(require, identifier, library, forName) {
if (package && identifier == package.name && package.main) {
identifier = "./"+package.main
}
if (identifier.substr(0,2) == "./") {
var consumer = library.modules[forName]
var consumerRequire = consumer && consumer.require
}
try {
var singleton = (consumerRequire || require)(identifier)
} catch (e) {
var notFound = e.code == "MODULE_NOT_FOUND"
console.log()
if (forName) {
e.message += ". Error happened while we were trying to load "+identifier+" for "+forName
}
var probablyPackage = !identifier.match(/[\.\/]/)
if (notFound && identifier.match(/[A-Z]/)) {
e.message += " (is '"+identifier+"' capitalized right? usually modules are lowercase.)"
} else if (notFound && probablyPackage && !e.message.match(/package\.json point/)) {
e.message += " (Is it in your node_modules folder? Does the \"main\" attribute in the package.json point to the right file?)"
}
e.message += "\n\n "+library.contents()+"\n"
throw e
}
if (singleton) {
return processCommonJsSingleton(identifier, singleton, library)
}
}
)
function processCommonJsSingleton(path, singleton, library) {
if (singleton.__isNrtvLibraryModule == true) {
throw new Error("Commonjs module "+path+" exported a nrtv module ("+singleton.name+"). Try module.exports = library.export(...) instead of module.exports = library.define(...)")
}
if (module = singleton.__nrtvModule) {
if (!library.modules[module.name]) {
library.addModule(module)
}
if (module.name != path) {
var pathIsAName = !path.match(/\//) && path != "."
if (pathIsAName) {
console.log(" ⚡ WARNING ⚡ The commonjs module", path, "returned a module-library module called", module.name)
}
library.setPath(path, module.name)
}
return library._getSingleton(path)
} else {
var isObject = typeof singleton == "object"
var isEmpty = isObject && Object.keys(singleton).length < 1
if (isObject && isEmpty) {
throw new Error("The "+path+" module just returned an empty object. Did you forget to do module.exports = library.export?")
}
library.singletonCache[path] = singleton
return singleton
}
}
Library.require = require
var library = new Library()
require.__nrtvLibrary = library
function exportLibraryModule(moduleSingleton, localLibrary) {
var argumentsForDefine = Array.prototype.slice.call(arguments, 2)
moduleSingleton.exports = localLibrary.export.apply(localLibrary, argumentsForDefine)
}
function libraryFactory(alternateRequire) {
if (!alternateRequire) {
throw new Error("You need to pass require to module-library. Like this: var library = require(\"module-library\")(require)")
}
var boundFunc = alternateRequire.__nrtvModuleFunction
if (alternateRequire.require) {
var alternateModule = alternateRequire
alternateRequire = alternateModule.require
}
if (boundFunc) {
return boundFunc
}
var newLibrary = library.clone()
newLibrary.require = alternateRequire
if (alternateModule) {
boundFunc = exportLibraryModule.bind(null, alternateModule, newLibrary)
boundFunc.checkOut = newLibrary.using.bind(newLibrary)
boundFunc.__isNrtvLibrary = true
boundFunc.ref = library.ref
boundFunc.reference = newLibrary.ref
alternateModule.__nrtvModuleFunction = boundFunc
return boundFunc
}
boundFunc = newLibrary.define.bind(newLibrary)
boundFunc.define = boundFunc
boundFunc.dealias = newLibrary.dealias.bind(newLibrary)
boundFunc.get = newLibrary.get.bind(newLibrary)
boundFunc.getModule = newLibrary.getModule.bind(newLibrary)
boundFunc.using = newLibrary.using.bind(newLibrary)
boundFunc.run = boundFunc.using
boundFunc.export = newLibrary.export.bind(newLibrary)
boundFunc.__isNrtvLibrary = true
boundFunc.ref = newLibrary.ref
alternateRequire.__nrtvModuleFunction = boundFunc
return boundFunc
}
libraryFactory.Library = Library
libraryFactory.define = libraryFactory.export = libraryFactory.using = function() {
throw new Error("You tried to use the library factory as a library. Did you remember to do require(\"module-library\')(require)?")
}
libraryFactory.generator = generateConstructor
module.exports = libraryFactory