Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

cache.js: preparation for git module (factor out getCacheStat) #4765

Closed
wants to merge 2 commits into from

2 participants

@robertkowalski
Collaborator

This is one of the first steps to factor out the git part of
cache.js into a separate module which can be tested separately.

@robertkowalski
Collaborator

Note: @isaacs already got publish rights for the module, will start a transfer if we merge this in

@domenic domenic commented on the diff
node_modules/npm-cache-dir/test/index.js
@@ -0,0 +1,56 @@
+var cacheDir = require("../")
+ , test = require("tap").test
+ , mkdirp = require("mkdirp")
+ , rimraf = require("rimraf")
+ , path = require("path")
+ , fs = require("fs")
+ , fixtures = path.join(__dirname, "fixtures")
+
+var npm = { cache: path.join(fixtures, "mycache") }
@domenic Collaborator
domenic added a note

If all you use from the npm object is the cache property, perhaps this module should just accept a cache value?

@robertkowalski Collaborator

You are absolutely right. Missed the forest for the trees :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
robertkowalski added some commits
@robertkowalski robertkowalski add npm-cache-dir@0.0.2 cef8c40
@robertkowalski robertkowalski Factor out getCacheStat
This is one of the first steps to factor out the git part of
`cache.js` into a separate module which can be tested separately.
3bbb49e
@robertkowalski
Collaborator

fixed! :)

@domenic
Collaborator

So this LGreatTM but I want to make sure @isaacs is on board with this direction of refactoring cache.js. I know our original and/or long-term plan was #4159 but this might be a nice way to go in the meantime. Actually it might still be necessary anyway even after that.

@robertkowalski
Collaborator

Side-note: #4159 discusses mainly caching http agents, not how the git part should work. I'm planning to integrate the git module into npm-fetch anyway which is also suggested there.

@domenic
Collaborator

Well, I guess @othiym23's recent cache refactor supersedes this...

@domenic domenic closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 23, 2014
  1. @robertkowalski

    add npm-cache-dir@0.0.2

    robertkowalski authored
  2. @robertkowalski

    Factor out getCacheStat

    robertkowalski authored
    This is one of the first steps to factor out the git part of
    `cache.js` into a separate module which can be tested separately.
This page is out of date. Refresh to see the latest.
View
67 lib/cache.js
@@ -84,6 +84,7 @@ var mkdir = require("mkdirp")
, which = require("which")
, isGitUrl = require("./utils/is-git-url.js")
, pathIsInside = require("path-is-inside")
+ , getCacheStat = require("npm-cache-dir").getCacheStat
cache.usage = "npm cache add <tarball file>"
+ "\nnpm cache add <folder>"
@@ -547,7 +548,7 @@ function archiveGitRemote (p, u, co, origUrl, cb) {
log.silly("verifyOwnership", "skipping for windows")
resolveHead()
} else {
- getCacheStat(function(er, cs) {
+ getCacheStat(npm.cache, log, function(er, cs) {
if (er) {
log.error("Could not get cache stat")
return cb(er)
@@ -969,65 +970,9 @@ function addLocalTarball (p, name, shasum, cb_) {
})
}
-// to maintain the cache dir's permissions consistently.
-var cacheStat = null
-function getCacheStat (cb) {
- if (cacheStat) return cb(null, cacheStat)
- fs.stat(npm.cache, function (er, st) {
- if (er) return makeCacheDir(cb)
- if (!st.isDirectory()) {
- log.error("getCacheStat", "invalid cache dir %j", npm.cache)
- return cb(er)
- }
- return cb(null, cacheStat = st)
- })
-}
-
-function makeCacheDir (cb) {
- if (!process.getuid) return mkdir(npm.cache, cb)
-
- var uid = +process.getuid()
- , gid = +process.getgid()
-
- if (uid === 0) {
- if (process.env.SUDO_UID) uid = +process.env.SUDO_UID
- if (process.env.SUDO_GID) gid = +process.env.SUDO_GID
- }
- if (uid !== 0 || !process.env.HOME) {
- cacheStat = {uid: uid, gid: gid}
- return mkdir(npm.cache, afterMkdir)
- }
-
- fs.stat(process.env.HOME, function (er, st) {
- if (er) {
- log.error("makeCacheDir", "homeless?")
- return cb(er)
- }
- cacheStat = st
- log.silly("makeCacheDir", "cache dir uid, gid", [st.uid, st.gid])
- return mkdir(npm.cache, afterMkdir)
- })
-
- function afterMkdir (er, made) {
- if (er || !cacheStat || isNaN(cacheStat.uid) || isNaN(cacheStat.gid)) {
- return cb(er, cacheStat)
- }
-
- if (!made) return cb(er, cacheStat)
-
- // ensure that the ownership is correct.
- chownr(made, cacheStat.uid, cacheStat.gid, function (er) {
- return cb(er, cacheStat)
- })
- }
-}
-
-
-
-
function addPlacedTarball (p, name, shasum, cb) {
if (!cb) cb = name, name = ""
- getCacheStat(function (er, cs) {
+ getCacheStat(npm.cache, log, function (er, cs) {
if (er) return cb(er)
return addPlacedTarball_(p, name, cs.uid, cs.gid, shasum, cb)
})
@@ -1188,7 +1133,7 @@ function addLocalDirectory (p, name, shasum, cb) {
, data.version, "package.tgz" )
, placeDirect = path.basename(p) === "package"
, tgz = placeDirect ? placed : tmptgz
- getCacheStat(function (er, cs) {
+ getCacheStat(npm.cache, log, function (er, cs) {
mkdir(path.dirname(tgz), function (er, made) {
if (er) return cb(er)
@@ -1217,7 +1162,7 @@ function addLocalDirectory (p, name, shasum, cb) {
function addTmpTarball (tgz, name, shasum, cb) {
if (!cb) cb = name, name = ""
- getCacheStat(function (er, cs) {
+ getCacheStat(npm.cache, log, function (er, cs) {
if (er) return cb(er)
var contents = path.dirname(tgz)
tar.unpack( tgz, path.resolve(contents, "package")
@@ -1278,7 +1223,7 @@ function lockFileName (u) {
var myLocks = {}
function lock (u, cb) {
// the cache dir needs to exist already for this.
- getCacheStat(function (er, cs) {
+ getCacheStat(npm.cache, log, function (er, cs) {
if (er) return cb(er)
var opts = { stale: npm.config.get("cache-lock-stale")
, retries: npm.config.get("cache-lock-retries")
View
27 node_modules/npm-cache-dir/LICENSE
@@ -0,0 +1,27 @@
+Copyright (c) Robert Kowalski and Isaac Z. Schlueter ("Authors")
+All rights reserved.
+
+The BSD License
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS
+BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
View
15 node_modules/npm-cache-dir/README.md
@@ -0,0 +1,15 @@
+[![Build Status](https://travis-ci.org/robertkowalski/npm-cache-dir.png?branch=master)](https://travis-ci.org/robertkowalski/npm-cache-dir)
+
+# npm-cache-dir
+
+Get stats about npm's cache dir and create the directory
+lazily
+
+## API
+
+### .getCacheStat(npmCache, log, cb)
+Get fs.stats about npm's cache dir, and create the cachedir
+if it does not exist
+
+### .makeCacheDir(npmCache, log, cb)
+Create the cache dir according to the setting in `npm.cache`
View
60 node_modules/npm-cache-dir/index.js
@@ -0,0 +1,60 @@
+var fs = require("fs")
+ , mkdir = require("mkdirp")
+ , chownr = require("chownr")
+
+// to maintain the cache dir's permissions consistently.
+var cacheStat = null
+
+exports.getCacheStat = getCacheStat
+function getCacheStat (npmCache, log, cb) {
+ if (cacheStat) return cb(null, cacheStat)
+ fs.stat(npmCache, function (er, st) {
+ if (er) return makeCacheDir(npmCache, log, cb)
+ if (!st.isDirectory()) {
+ log.error("getCacheStat", "invalid cache dir %j", npmCache)
+ return cb(er)
+ }
+ return cb(null, cacheStat = st)
+ })
+}
+
+exports.makeCacheDir = makeCacheDir
+function makeCacheDir (npmCache, log, cb) {
+ if (!process.getuid) return mkdir(npmCache, cb)
+
+ var uid = +process.getuid()
+ , gid = +process.getgid()
+
+ if (uid === 0) {
+ if (process.env.SUDO_UID) uid = +process.env.SUDO_UID
+ if (process.env.SUDO_GID) gid = +process.env.SUDO_GID
+ }
+
+ if (uid !== 0 || !process.env.HOME) {
+ cacheStat = {uid: uid, gid: gid}
+ return mkdir(npmCache, afterMkdir)
+ }
+
+ fs.stat(process.env.HOME, function (er, st) {
+ if (er) {
+ log.error("makeCacheDir", "homeless?")
+ return cb(er)
+ }
+ cacheStat = st
+ log.silly("makeCacheDir", "cache dir uid, gid", [st.uid, st.gid])
+ return mkdir(npmCache, afterMkdir)
+ })
+
+ function afterMkdir (er, made) {
+ if (er || !cacheStat || isNaN(cacheStat.uid) || isNaN(cacheStat.gid)) {
+ return cb(er, cacheStat)
+ }
+
+ if (!made) return cb(er, cacheStat)
+
+ // ensure that the ownership is correct.
+ chownr(made, cacheStat.uid, cacheStat.gid, function (er) {
+ return cb(er, cacheStat)
+ })
+ }
+}
View
42 node_modules/npm-cache-dir/package.json
@@ -0,0 +1,42 @@
+{
+ "name": "npm-cache-dir",
+ "version": "1.0.0",
+ "description": "I am checking and creating npm's cache dir",
+ "main": "index.js",
+ "dependencies": {
+ "mkdirp": "~0.3.5",
+ "chownr": "0"
+ },
+ "devDependencies": {
+ "rimraf": "~2.2.6",
+ "tap": "~0.4.8"
+ },
+ "scripts": {
+ "test": "tap test/*.js"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/robertkowalski/npm-cache-dir.git"
+ },
+ "homepage": "https://github.com/robertkowalski/npm-cache-dir",
+ "keywords": [
+ "npm,",
+ "cache"
+ ],
+ "author": {
+ "name": "Robert Kowalski",
+ "email": "rok@kowalski.gd"
+ },
+ "license": "BSD-2-Clause",
+ "bugs": {
+ "url": "https://github.com/robertkowalski/npm-cache-dir/issues"
+ },
+ "readme": "[![Build Status](https://travis-ci.org/robertkowalski/npm-cache-dir.png?branch=master)](https://travis-ci.org/robertkowalski/npm-cache-dir)\n\n# npm-cache-dir\n\nGet stats about npm's cache dir and create the directory\nlazily\n\n## API\n\n### .getCacheStat(npmCache, log, cb)\nGet fs.stats about npm's cache dir, and create the cachedir\nif it does not exist\n\n### .makeCacheDir(npmCache, log, cb)\nCreate the cache dir according to the setting in `npm.cache`\n",
+ "readmeFilename": "README.md",
+ "_id": "npm-cache-dir@1.0.0",
+ "dist": {
+ "shasum": "2a9ce717292daa16492fd9a1aafed4a6be540bb0"
+ },
+ "_from": "npm-cache-dir@1.0.0",
+ "_resolved": "https://registry.npmjs.org/npm-cache-dir/-/npm-cache-dir-1.0.0.tgz"
+}
View
56 node_modules/npm-cache-dir/test/index.js
@@ -0,0 +1,56 @@
+var cacheDir = require("../")
+ , test = require("tap").test
+ , mkdirp = require("mkdirp")
+ , rimraf = require("rimraf")
+ , path = require("path")
+ , fs = require("fs")
+ , fixtures = path.join(__dirname, "fixtures")
+
+var npm = { cache: path.join(fixtures, "mycache") }
@domenic Collaborator
domenic added a note

If all you use from the npm object is the cache property, perhaps this module should just accept a cache value?

@robertkowalski Collaborator

You are absolutely right. Missed the forest for the trees :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ , log = function () {}
+
+function setup () {
+ rimraf.sync(fixtures)
+ mkdirp.sync(fixtures)
+}
+
+test("getCacheStat creates a directory if not exists", function (t) {
+ setup()
+ fs.exists(npm.cache, function (e) {
+ t.notOk(e, "cache does not exist")
+ cacheDir.getCacheStat(npm.cache, log, function () {
+ fs.exists(npm.cache, function (e) {
+ t.ok(e, "cache exists")
+ t.end()
+ })
+ })
+ })
+})
+
+test("getCacheStat returns stats", function (t) {
+ setup()
+ cacheDir.getCacheStat(npm.cache, log, function (err, d) {
+ t.ok(d.uid, "uid set")
+ t.ok(d.gid, "gid set")
+ t.end()
+ })
+})
+
+test("makeCacheDir creates directories", function (t) {
+ setup()
+ npm.cache = path.join(fixtures, "npm-test-2")
+ fs.exists(npm.cache, function (e) {
+ t.notOk(e, "cache does not exist")
+ cacheDir.makeCacheDir(npm.cache, log, function () {
+ fs.exists(npm.cache, function (e) {
+ t.ok(e, "cache exists")
+ t.end()
+ })
+ })
+ })
+})
+
+test("cleanup", function (t) {
+ rimraf.sync(fixtures)
+ t.end()
+})
View
2  package.json
@@ -60,6 +60,7 @@
"mkdirp": "~0.3.5",
"node-gyp": "~0.12.2",
"nopt": "^2.2.0",
+ "npm-cache-dir": "~1.0.0",
"npm-install-checks": "~1.0.0",
"npm-registry-client": "^0.4.4",
"npm-user-validate": "0.0.3",
@@ -113,6 +114,7 @@
"node-gyp",
"nopt",
"normalize-package-data",
+ "npm-cache-dir",
"npm-install-checks",
"npm-registry-client",
"npm-user-validate",
Something went wrong with that request. Please try again.