@@ -4,13 +4,9 @@ const { get: ccGet } = require('cacache')
4
4
const ccPath = require ( 'cacache/lib/content/path.js' )
5
5
const cp = require ( 'child_process' )
6
6
const fs = require ( 'fs' )
7
- const npa = require ( 'npm-package-arg' )
8
7
const path = require ( 'path' )
9
8
const ssri = require ( 'ssri' )
10
9
11
- // warm up npa
12
- npa ( 'idk' )
13
-
14
10
const INDEX_VERSION = module . exports . INDEX_VERSION = '2.0.0'
15
11
16
12
const pkgLockName = 'package-lock.json'
@@ -72,37 +68,38 @@ function resolve (...p) {
72
68
}
73
69
74
70
module . exports . depKey = depKey
75
- function depKey ( spec , dep ) {
76
- const prefix = `tinked-package:${ INDEX_VERSION } `
77
- if ( spec . registry ) {
78
- return `${ prefix } :${ dep . integrity || dep . resolved || spec . raw } `
79
- } else if ( spec . type === 'git' ) {
80
- return `${ prefix } :git:${ dep . version } `
81
- } else if ( spec . type === 'remote' ) {
82
- return `${ prefix } :${ dep . integrity || dep . version } `
83
- } else {
84
- return `${ prefix } :${ spec . name } :${ dep . version } :${ dep . resolved } :${ dep . integrity } `
85
- }
71
+ function depKey ( pkgName , dep ) {
72
+ return `tinked-package:${
73
+ INDEX_VERSION
74
+ } :${
75
+ pkgName
76
+ } :${
77
+ dep . version
78
+ } :${
79
+ dep . resolved
80
+ } :${
81
+ dep . integrity
82
+ } `
86
83
}
87
84
88
85
module . exports . resolveEntity = resolveEntity
89
86
function resolveEntity ( cache , scope , pkgName , filePath ) {
90
- const spec = npa ( pkgName , scope . version )
87
+ // const spec = npa(pkgName, scope.version)
91
88
if ( ! ( scope && scope . dependencies && scope . dependencies [ pkgName ] ) ) {
92
89
return false
93
90
}
94
91
let pkg
95
92
const tink = process . tink
96
93
try {
97
94
process . tink = null
98
- pkg = getPkg ( cache , spec , scope , pkgName )
95
+ pkg = getPkg ( cache , scope , pkgName )
99
96
} catch ( e ) {
100
97
if ( e . code === 'ENOENT' ) {
101
98
const dep = scope . dependencies [ pkgName ]
102
99
if ( dep ) {
103
100
dep . name = pkgName
104
101
fetchPackageSync ( cache , dep , dep . integrity )
105
- pkg = getPkg ( cache , spec , scope , pkgName )
102
+ pkg = getPkg ( cache , scope , pkgName )
106
103
} else {
107
104
return null
108
105
}
@@ -137,13 +134,12 @@ function resolveEntity (cache, scope, pkgName, filePath) {
137
134
}
138
135
139
136
module . exports . readPkgLock = readPkgLock
140
- function readPkgLock ( ... p ) {
141
- const resolved = path . resolve ( ... p )
142
- let modulesIdx = resolved . lastIndexOf ( 'node_modules' )
143
- while ( modulesIdx !== - 1 ) {
137
+ function readPkgLock ( resolved ) {
138
+ let modulesIdx = resolved . indexOf ( 'node_modules' )
139
+ let pkgLock
140
+ while ( ! pkgLock && modulesIdx !== - 1 ) {
144
141
let substr = resolved . substr ( 0 , modulesIdx - 1 )
145
142
const pkgLockPath = path . join ( substr , pkgLockName )
146
- let pkgLock
147
143
if ( pkgLockCache . has ( pkgLockPath ) ) {
148
144
pkgLock = pkgLockCache . get ( pkgLockPath )
149
145
} else {
@@ -154,13 +150,15 @@ function readPkgLock (...p) {
154
150
} catch ( e ) {
155
151
if ( e . code !== 'ENOENT' ) {
156
152
throw e
153
+ } else {
154
+ pkgLockCache . set ( pkgLockPath , null )
157
155
}
158
156
}
159
157
}
160
158
if ( pkgLock ) {
161
159
return { pkgLock, subPath : resolved . substr ( modulesIdx - 1 ) }
162
160
} else {
163
- modulesIdx = substr . lastIndexOf ( 'node_modules' )
161
+ modulesIdx = substr . indexOf ( 'node_modules' , modulesIdx )
164
162
}
165
163
}
166
164
return null
@@ -288,9 +286,17 @@ function statSync ({ cache, hash, pkg, resolvedPath, isDir }, verify) {
288
286
} )
289
287
}
290
288
291
- function getPkg ( cache , spec , scope , pkgName ) {
292
- const { metadata } = ccGet . sync ( cache , depKey ( spec , scope . dependencies [ pkgName ] ) , { memoize : true } )
293
- return JSON . parse ( metadata )
289
+ const pkgCache = new Map ( )
290
+ function getPkg ( cache , scope , pkgName ) {
291
+ const key = depKey ( pkgName , scope . dependencies [ pkgName ] )
292
+ if ( pkgCache . has ( key ) ) {
293
+ return pkgCache . get ( key )
294
+ } else {
295
+ const { metadata } = ccGet . sync ( cache , key )
296
+ const ret = JSON . parse ( metadata )
297
+ pkgCache . set ( key , ret )
298
+ return ret
299
+ }
294
300
}
295
301
296
302
let ensurePkg
0 commit comments