graphquire
Advanced tools
Comparing version 0.5.5 to 0.6.0
@@ -37,2 +37,7 @@ #!/usr/bin/env node | ||
} | ||
function isNoSource() { | ||
return params.some(function onEach(param) { | ||
return param === '--no-source' | ||
}) | ||
} | ||
@@ -72,3 +77,7 @@ if (isVerbose()) { | ||
var options = { cachePath: './node_modules', location: getLocation() } | ||
var options = { | ||
cachePath: './node_modules', | ||
location: getLocation(), | ||
includeSource: !isNoSource(), | ||
} | ||
graphquire.getGraph(options, function onGraph(error, graph) { | ||
@@ -75,0 +84,0 @@ if (error) return failure(error) |
@@ -54,6 +54,5 @@ /* vim:set ts=2 sw=2 sts=2 expandtab */ | ||
} | ||
function isAbsolute(uri) { return uri && uri.charAt(0) !== '.' } | ||
function resolve(uri, base) { | ||
var path, paths, last | ||
if (isAbsolute(uri)) return uri | ||
if (!isRelativeURI(uri)) return uri | ||
paths = uri.split('/') | ||
@@ -81,5 +80,2 @@ base = base ? base.split('/') : [ '.' ] | ||
} | ||
function resolveURI(uri, base) { | ||
return normalizeURI(resolveID(uri, base)) | ||
} | ||
function resolvePluginURI(id) { | ||
@@ -95,34 +91,30 @@ return extractPluginName(id) + '://' + normalizeURI(extractURI(id)) | ||
var get = options.protocol === 'http:' ? http.get : https.get | ||
var buffer = '' | ||
get(options, function onResponse(response) { | ||
response.on('error', callback) | ||
response.on('data', function onData(buffer) { | ||
callback(null, buffer) | ||
response.on('data', function onData(chunk) { buffer += chunk }) | ||
response.on('end', function onEnd() { | ||
callback(null, buffer, true) | ||
}) | ||
}).on('error', callback) | ||
} | ||
exports.readURL = readURL | ||
function fetchSource(module, callback) { | ||
readURL(module.uri, function onRead(error, data) { | ||
if (error) callback(error) | ||
else callback(error, module.source = data) | ||
}) | ||
} | ||
function getSource(metadata, module, onComplete, onProgress) { | ||
if (!isPluginURI(module.id) && !isRelativeURI(module.id)) { | ||
function getSource(graph, module, onComplete, onProgress) { | ||
var path = graph.resolvePath(module.id) | ||
var uri = graph.resolveURI(module.id) | ||
if (path) { | ||
if (onProgress) onProgress(READ_FILE, module.path) | ||
fs.readFile(path, function onRead(error, buffer) { | ||
if (!error || error.code !== 'ENOENT') return onComplete(error, buffer) | ||
if (onProgress) onProgress(FETCH_URL, uri) | ||
readURL(uri, onComplete) | ||
}) | ||
} else { | ||
module.isNative = true | ||
delete module.path | ||
delete module.uri | ||
return onComplete(null, module) | ||
} | ||
var location = path.join(path.dirname(metadata.location), module.path) | ||
if (onProgress) onProgress(READ_FILE, location) | ||
fs.readFile(location, function onRead(error, buffer) { | ||
if (!error || error.code !== 'ENOENT') return onComplete(error, buffer) | ||
if (onProgress) onProgress(FETCH_URL, module.uri) | ||
fetchSource(module, onComplete) | ||
}) | ||
} | ||
function getDependency(metadata, requirer, next, onProgress, dependencyID) { | ||
function getDependency(graph, requirer, next, onProgress, dependencyID) { | ||
var id, module; | ||
@@ -133,16 +125,9 @@ id = resolveID(dependencyID, requirer.id) | ||
// If module is already loaded or is being fetched we just go next. | ||
if ((module = metadata.modules[id])) | ||
return next(null, metadata, module, next) | ||
if ((module = graph.modules[id])) | ||
return next(null, graph, module, next) | ||
// Otherwise we create module and start resolving it's dependencies | ||
module = metadata.modules[id] = { id: id } | ||
module.path = isPluginURI(id) ? | ||
path.join(metadata.cachePath, id) : id | ||
module = graph.modules[id] = { id: id } | ||
if (isPluginURI(id)) | ||
module.uri = resolvePluginURI(id) | ||
else if (requirer.uri) | ||
module.uri = resolveURI(dependencyID, requirer.uri) | ||
resolveRequirements(metadata, module, next, onProgress) | ||
resolveRequirements(graph, module, next, onProgress) | ||
} | ||
@@ -152,12 +137,12 @@ | ||
var current = 0 | ||
return function next(error, metadata, module) { | ||
return function next(error, graph, module) { | ||
if (error) return onComplete(error) | ||
if (++ current === total) | ||
onComplete(error, metadata, module) | ||
onComplete(error, graph, module) | ||
} | ||
} | ||
function resolveRequirements(metadata, module, onComplete, onProgress) { | ||
function resolveRequirements(graph, module, onComplete, onProgress) { | ||
if (onProgress) onProgress(GET_MODULE, module) | ||
getSource(metadata, module, function onSource(error, source) { | ||
getSource(graph, module, function onSource(error, source, isRemote) { | ||
var dependencies, resolved = 0 | ||
@@ -167,2 +152,3 @@ | ||
if (onProgress) onProgress(GOT_MODULE, module) | ||
if (isRemote && graph.includesSource) module.source = source | ||
// Extracting module dependencies by analyzing it's source. | ||
@@ -173,3 +159,3 @@ dependencies = extractDependencies(source) | ||
if (!dependencies.length) | ||
return onComplete(error, metadata, module) | ||
return onComplete(error, graph, module) | ||
@@ -184,3 +170,3 @@ // If we got this far we know module has dependencies, so we create it's | ||
var next = Next(dependencies.length, onComplete) | ||
dependencies.forEach(getDependency.bind(null, metadata, module, next, onProgress)) | ||
dependencies.forEach(getDependency.bind(null, graph, module, next, onProgress)) | ||
}, onProgress) | ||
@@ -197,8 +183,20 @@ } | ||
function getGraph(options, onComplete, onProgress) { | ||
var location = normalizePackageLocation(options.location) | ||
var graph = { | ||
location: normalizePackageLocation(options.location), | ||
cachePath: options.cachePath || './' | ||
path: isURI(location) ? './' : location, | ||
uri: isURI(location) ? location : './', | ||
cachePath: options.cachePath || './', | ||
includesSource: options.includeSource || false, | ||
resolvePath: function resolvePath(id) { | ||
var root = path.dirname(graph.path) | ||
return isPluginURI(id) ? path.join(root, graph.cachePath, id) : | ||
isRelativeURI(id) ? path.join(root, id) : null | ||
}, | ||
resolveURI: function resolveURI(id) { | ||
return isPluginURI(id) ? resolvePluginURI(id) : | ||
isRelativeURI(id) ? resolveID(id, graph.uri) : null | ||
} | ||
} | ||
if (onProgress) onProgress(GET_METADATA, graph.location) | ||
getMetadata(graph.location, function onMetadata(error, content) { | ||
if (onProgress) onProgress(GET_METADATA, location) | ||
getMetadata(location, function onMetadata(error, content) { | ||
if (error) return onComplete(error) | ||
@@ -213,4 +211,2 @@ | ||
graph.modules[main.id] = main | ||
if (isURI(graph.location)) main.uri = url.resolve(graph.location, main.id) | ||
else main.path = normalizeURI(main.id) | ||
@@ -217,0 +213,0 @@ resolveRequirements(graph, main, onComplete, onProgress) |
# History # | ||
## 0.6.0 / 2011-06-09 ## | ||
- Fix bug in read url (big files can be read now). | ||
- Adding `--no-source` option in order to exclude source. | ||
- Graph no longer contains paths / URLs for modules, instead it has resolvePath | ||
/ resolveURI function that take module id and return path / uri. | ||
- Changed structure of graph, making it cleaner. | ||
## 0.5.5 / 2011-06-01 ## | ||
@@ -4,0 +12,0 @@ |
{ | ||
"name": "graphquire", | ||
"id": "graphquire", | ||
"version": "0.5.5", | ||
"version": "0.6.0", | ||
"description": "module graph builder and installer.", | ||
@@ -6,0 +6,0 @@ "keywords": [ "dependencies", "graph", "modules", "require", "linker" ], |
@@ -38,4 +38,4 @@ # graphquire # | ||
"devDependencies": { | ||
"graphquire": ">=0.5.0" | ||
"dependencies": { | ||
"graphquire": ">=0.6.0" | ||
} | ||
@@ -74,4 +74,2 @@ | ||
"./index.js": { | ||
"id": "./index.js", | ||
"path": "./index.js", | ||
"requirements": { | ||
@@ -83,4 +81,2 @@ "http!foo.org/a": "http!foo.org/a.js" | ||
"id": "http!foo.org/a.js", | ||
"path": "node_modules/http!foo.org/a.js", | ||
"uri": "http://foo.org/a.js", | ||
"requirements": { | ||
@@ -92,4 +88,2 @@ "./nested/b": "http!foo.org/nested/b.js" | ||
"id": "http!foo.org/nested/b.js", | ||
"path": "node_modules/http!foo.org/nested/b.js", | ||
"uri": "http://foo.org/nested/b.js", | ||
"requirements": { | ||
@@ -101,4 +95,2 @@ "http!bar.org/c": "http!bar.org/c.js" | ||
"id": "http!bar.org/c.js", | ||
"path": "node_modules/http!bar.org/c.js", | ||
"uri": "http://bar.org/c.js" | ||
} | ||
@@ -124,3 +116,2 @@ } | ||
"./index.js": { | ||
"uri": "https://github.com/Gozala/graphquire/raw/master/test/fixtures/pckg2/index.js", | ||
"id": "./index.js", | ||
@@ -134,4 +125,2 @@ "source": ".....", | ||
"id": "https!github.com/Gozala/models/raw/master/lib/models.js", | ||
"path": "node_modules/https!github.com/Gozala/models/raw/master/lib/models.js", | ||
"uri": "https://github.com/Gozala/models/raw/master/lib/models.js", | ||
"source": "....", | ||
@@ -144,4 +133,2 @@ "requirements": { | ||
"id": "https!github.com/Gozala/models/raw/master/lib/events.js", | ||
"path": "node_modules/https!github.com/Gozala/models/raw/master/lib/events.js", | ||
"uri": "https://github.com/Gozala/models/raw/master/lib/events.js", | ||
"source": "....", | ||
@@ -154,4 +141,2 @@ "requirements": { | ||
"id": "https!github.com/Gozala/models/raw/master/lib/extendables.js", | ||
"path": "node_modules/https!github.com/Gozala/models/raw/master/lib/extendables.js", | ||
"uri": "https://github.com/Gozala/models/raw/master/lib/extendables.js", | ||
"source": "....." | ||
@@ -158,0 +143,0 @@ } |
@@ -19,41 +19,28 @@ /* vim:set ts=2 sw=2 sts=2 expandtab */ | ||
exports['test basic'] = function(assert, done) { | ||
var options = optionsFor('pckg1') | ||
var options = optionsFor('pckg-cached') | ||
getGraph(options, function onGraph(error, graph) { | ||
assert.deepEqual(graph, { | ||
"cachePath": "./node_modules", | ||
"location": options.location + '/package.json', | ||
"metadata": { | ||
"name": "pckg1" | ||
assert.deepEqual(graph.metadata, { "name": "pckg1" }, "metadat is correct") | ||
assert.deepEqual(graph.modules, { | ||
"./index.js": { | ||
"id": "./index.js", | ||
"requirements": { | ||
"http!foo.org/a": "http!foo.org/a.js" | ||
} | ||
}, | ||
"modules": { | ||
"./index.js": { | ||
"id": "./index.js", | ||
"path": "./index.js", | ||
"requirements": { | ||
"http!foo.org/a": "http!foo.org/a.js" | ||
} | ||
}, | ||
"http!foo.org/a.js": { | ||
"id": "http!foo.org/a.js", | ||
"path": "node_modules/http!foo.org/a.js", | ||
"uri": "http://foo.org/a.js", | ||
"requirements": { | ||
"./nested/b": "http!foo.org/nested/b.js" | ||
} | ||
}, | ||
"http!foo.org/nested/b.js": { | ||
"id": "http!foo.org/nested/b.js", | ||
"path": "node_modules/http!foo.org/nested/b.js", | ||
"uri": "http://foo.org/nested/b.js", | ||
"requirements": { | ||
"http!bar.org/c": "http!bar.org/c.js" | ||
} | ||
}, | ||
"http!bar.org/c.js": { | ||
"id": "http!bar.org/c.js", | ||
"path": "node_modules/http!bar.org/c.js", | ||
"uri": "http://bar.org/c.js", | ||
"http!foo.org/a.js": { | ||
"id": "http!foo.org/a.js", | ||
"requirements": { | ||
"./nested/b": "http!foo.org/nested/b.js" | ||
} | ||
}, | ||
"http!foo.org/nested/b.js": { | ||
"id": "http!foo.org/nested/b.js", | ||
"requirements": { | ||
"http!bar.org/c": "http!bar.org/c.js" | ||
} | ||
}, | ||
"http!bar.org/c.js": { | ||
"id": "http!bar.org/c.js", | ||
} | ||
}, 'correct graph is generated') | ||
}, 'modules linked correctly') | ||
done() | ||
@@ -60,0 +47,0 @@ }) |
@@ -18,12 +18,10 @@ /* vim:set ts=2 sw=2 sts=2 expandtab */ | ||
function hasPath(modules, id) { return !!modules[id].path } | ||
function getPath(modules, id) { return modules[id].path } | ||
function clean(graph, onComplete, onProgress) { | ||
var modules = graph.modules | ||
var root = path.dirname(graph.location) | ||
var root = path.dirname(graph.path) | ||
var http = path.join(root, graph.cachePath, 'http!') | ||
var https = path.join(root, graph.cachePath, 'https!') | ||
var paths = Object.keys(modules).filter(hasPath.bind(null, modules)) | ||
.map(getPath.bind(null, modules)) | ||
var paths = Object.keys(modules).map(graph.resolvePath.bind(graph)) | ||
.map(path.join.bind(path, root)) | ||
var location = path.join(root, graph.cachePath) | ||
@@ -55,3 +53,3 @@ utils.reduceTree(location, onComplete, function onReduce(entry) { | ||
steps ++ | ||
utils.writeFile(module.path, module.source, next.bind(null, module)) | ||
utils.writeFile(graph.resolvePath(module.id), module.source, next.bind(null, module)) | ||
;delete module.source // removing source as it's no longer necessary | ||
@@ -58,0 +56,0 @@ if (onProgress) onProgress(WRITE_MODULE, module) |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
28
661
32176
146
12