Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Abstract out 'uid-number' to a separate dependency

  • Loading branch information...
commit 7fb3d9aaf5e9617269245876a4172b4919873a16 1 parent d4b80af
Isaac Z. Schlueter isaacs authored
2  lib/npm.js
@@ -25,7 +25,7 @@ var EventEmitter = require("events").EventEmitter
25 25 , which = require("which")
26 26 , semver = require("semver")
27 27 , findPrefix = require("./utils/find-prefix.js")
28   - , getUid = require("./utils/uid-number.js")
  28 + , getUid = require("uid-number")
29 29 , mkdir = require("./utils/mkdir-p.js")
30 30 , slide = require("slide")
31 31 , chain = slide.chain
2  lib/utils/mkdir-p.js
@@ -4,7 +4,7 @@ var log = require("./log.js")
4 4 , path = require("path")
5 5 , npm = require("../npm.js")
6 6 , exec = require("./exec.js")
7   - , uidNumber = require("./uid-number.js")
  7 + , uidNumber = require("uid-number")
8 8 , umask = process.umask()
9 9 , umaskOrig = umask
10 10 , addedUmaskExit = false
2  lib/utils/tar.js
@@ -5,7 +5,7 @@ var npm = require("../npm.js")
5 5 , fs = require("graceful-fs")
6 6 , path = require("path")
7 7 , log = require("./log.js")
8   - , uidNumber = require("./uid-number.js")
  8 + , uidNumber = require("uid-number")
9 9 , rm = require("rimraf")
10 10 , readJson = require("./read-json.js")
11 11 , relativize = require("./relativize.js")
55 lib/utils/uid-number.js
... ... @@ -1,55 +0,0 @@
1   -module.exports = uidNumber
2   -
3   -// This module calls into bin/npm-get-uid-gid.js, which sets the
4   -// uid and gid to the supplied argument, in order to find out their
5   -// numeric value. This can't be done in the main node process,
6   -// because otherwise npm would be running as that user.
7   -
8   -var exec = require("./exec.js")
9   - , path = require("path")
10   - , log = require("./log.js")
11   - , constants = require("constants")
12   - , npm = require("../npm.js")
13   - , uidSupport = process.getuid && process.setuid
14   - , uidCache = {}
15   - , gidCache = {}
16   -
17   -function uidNumber (uid, gid, cb) {
18   - if (!uidSupport || npm.config.get("unsafe-perm")) return cb()
19   - if (typeof cb !== "function") cb = gid, gid = null
20   - if (typeof cb !== "function") cb = uid, uid = null
21   - if (gid == null) gid = process.getgid()
22   - if (uid == null) uid = process.getuid()
23   - if (!isNaN(gid)) gid = +gid
24   - if (!isNaN(uid)) uid = +uid
25   -
26   - if (uidCache[uid]) uid = uidCache[uid]
27   - if (gidCache[gid]) gid = gidCache[gid]
28   -
29   - if (typeof gid === "number" && typeof uid === "number") {
30   - return cb(null, uid, gid)
31   - }
32   -
33   - var getter = path.join(__dirname, "..", "..", "bin", "npm-get-uid-gid.js")
34   - return exec( process.execPath, [getter, uid, gid], process.env, false
35   - , null, process.getuid(), process.getgid()
36   - , function (er, code, out, err) {
37   - if (er) return log.er(cb, "Could not get uid/gid "+err)(er)
38   - log.silly(out, "output from getuid/gid")
39   - out = JSON.parse(out+"")
40   - if (out.error) {
41   - if (!npm.config.get("unsafe-perm")) {
42   - var er = new Error(out.error)
43   - er.errno = out.errno
44   - return cb(er)
45   - } else {
46   - return cb(null, +process.getuid(), +process.getgid())
47   - }
48   - }
49   - if (isNaN(out.uid) || isNaN(out.gid)) return cb(new Error(
50   - "Could not get uid/gid: "+JSON.stringify(out)))
51   - uidCache[uid] = out.uid
52   - uidCache[gid] = out.gid
53   - cb(null, out.uid, out.gid)
54   - })
55   -}
17 node_modules/uid-number/README.md
Source Rendered
... ... @@ -0,0 +1,17 @@
  1 +Use this module to convert a username/groupname to a uid/gid number.
  2 +
  3 +Usage:
  4 +
  5 +```
  6 +npm install uid-number
  7 +```
  8 +
  9 +Then, in your node program:
  10 +
  11 +```javascript
  12 +var uidNumber = require("uid-number")
  13 +uidNumber("isaacs", function (er, uid, gid) {
  14 + // gid is null because we didn't ask for a group name
  15 + // uid === 24561 because that's my number.
  16 +})
  17 +```
8 bin/npm-get-uid-gid.js → node_modules/uid-number/get-uid-gid.js
... ... @@ -1,3 +1,11 @@
  1 +if (module !== require.main) {
  2 + throw new Error("This file should not be loaded with require()")
  3 +}
  4 +
  5 +if (!process.getuid || !process.getgid) {
  6 + throw new Error("this file should not be called without uid/gid support")
  7 +}
  8 +
1 9 var argv = process.argv.slice(2)
2 10 , user = argv[0] || process.getuid()
3 11 , group = argv[1] || process.getgid()
34 node_modules/uid-number/package.json
... ... @@ -0,0 +1,34 @@
  1 +{
  2 + "author": {
  3 + "name": "Isaac Z. Schlueter",
  4 + "email": "i@izs.me",
  5 + "url": "http://blog.izs.me/"
  6 + },
  7 + "name": "uid-number",
  8 + "description": "Convert a username/group name to a uid/gid number",
  9 + "version": "0.0.3",
  10 + "repository": {
  11 + "type": "git",
  12 + "url": "git://github.com/isaacs/uid-number.git"
  13 + },
  14 + "main": "uid-number.js",
  15 + "dependencies": {},
  16 + "devDependencies": {},
  17 + "optionalDependencies": {},
  18 + "engines": {
  19 + "node": "*"
  20 + },
  21 + "_npmUser": {
  22 + "name": "isaacs",
  23 + "email": "i@izs.me"
  24 + },
  25 + "_id": "uid-number@0.0.3",
  26 + "_engineSupported": true,
  27 + "_npmVersion": "1.1.12",
  28 + "_nodeVersion": "v0.7.7-pre",
  29 + "_defaultsLoaded": true,
  30 + "dist": {
  31 + "shasum": "be40aeab1db6ba45d2344d4ed6015109fc9d98d4"
  32 + },
  33 + "_from": "uid-number@0.0.3"
  34 +}
54 node_modules/uid-number/uid-number.js
... ... @@ -0,0 +1,54 @@
  1 +module.exports = uidNumber
  2 +
  3 +// This module calls into get-uid-gid.js, which sets the
  4 +// uid and gid to the supplied argument, in order to find out their
  5 +// numeric value. This can't be done in the main node process,
  6 +// because otherwise node would be running as that user from this
  7 +// point on.
  8 +
  9 +var child_process = require("child_process")
  10 + , path = require("path")
  11 + , uidSupport = process.getuid && process.setuid
  12 + , uidCache = {}
  13 + , gidCache = {}
  14 +
  15 +function uidNumber (uid, gid, cb) {
  16 + if (!uidSupport) return cb()
  17 + if (typeof cb !== "function") cb = gid, gid = null
  18 + if (typeof cb !== "function") cb = uid, uid = null
  19 + if (gid == null) gid = process.getgid()
  20 + if (uid == null) uid = process.getuid()
  21 + if (!isNaN(gid)) gid = uidCache[gid] = +gid
  22 + if (!isNaN(uid)) uid = uidCache[uid] = +uid
  23 +
  24 + if (uidCache.hasOwnProperty(uid)) uid = uidCache[uid]
  25 + if (gidCache.hasOwnProperty(gid)) gid = gidCache[gid]
  26 +
  27 + if (typeof gid === "number" && typeof uid === "number") {
  28 + return process.nextTick(cb.bind(null, null, uid, gid))
  29 + }
  30 +
  31 + var getter = require.resolve("./get-uid-gid.js")
  32 +
  33 + child_process.execFile( process.execPath
  34 + , [getter, uid, gid]
  35 + , function (code, out, err) {
  36 + if (er) return cb(new Error("could not get uid/gid\n" + err))
  37 + try {
  38 + out = JSON.parse(out+"")
  39 + } catch (ex) {
  40 + return cb(ex)
  41 + }
  42 +
  43 + if (out.error) {
  44 + var er = new Error(out.error)
  45 + er.errno = out.errno
  46 + return cb(er)
  47 + }
  48 +
  49 + if (isNaN(out.uid) || isNaN(out.gid)) return cb(new Error(
  50 + "Could not get uid/gid: "+JSON.stringify(out)))
  51 +
  52 + cb(null, uidCache[uid] = +out.uid, uidCache[gid] = +out.gid)
  53 + })
  54 +}
3  package.json
@@ -54,7 +54,8 @@
54 54 "read": "0",
55 55 "lru-cache": "1",
56 56 "node-gyp": "~0.3.7",
57   - "fstream-npm": "0.0"
  57 + "fstream-npm": "0.0",
  58 + "uid-number": "0.0"
58 59 },
59 60 "bundleDependencies": [
60 61 "slide",

0 comments on commit 7fb3d9a

Please sign in to comment.
Something went wrong with that request. Please try again.