1
-
2
1
/**
3
2
* Module dependencies.
4
3
*/
5
4
6
- var fs = require ( 'fs' )
7
- , path = require ( 'path' )
8
- , fileURLToPath = require ( 'file-uri-to-path' )
9
- , join = path . join
10
- , dirname = path . dirname
11
- , exists = ( ( fs . accessSync && function ( path ) { try { fs . accessSync ( path ) ; } catch ( e ) { return false ; } return true ; } )
12
- || fs . existsSync || path . existsSync )
13
- , defaults = {
14
- arrow : process . env . NODE_BINDINGS_ARROW || ' → '
15
- , compiled : process . env . NODE_BINDINGS_COMPILED_DIR || 'compiled'
16
- , platform : process . platform
17
- , arch : process . arch
18
- , nodePreGyp : 'node-v' + process . versions . modules + '-' + process . platform + '-' + process . arch
19
- , version : process . versions . node
20
- , bindings : 'bindings.node'
21
- , try : [
22
- // node-gyp's linked version in the "build" dir
23
- [ 'module_root' , 'build' , 'bindings' ]
24
- // node-waf and gyp_addon (a.k.a node-gyp)
25
- , [ 'module_root' , 'build' , 'Debug' , 'bindings' ]
26
- , [ 'module_root' , 'build' , 'Release' , 'bindings' ]
27
- // Debug files, for development (legacy behavior, remove for node v0.9)
28
- , [ 'module_root' , 'out' , 'Debug' , 'bindings' ]
29
- , [ 'module_root' , 'Debug' , 'bindings' ]
30
- // Release files, but manually compiled (legacy behavior, remove for node v0.9)
31
- , [ 'module_root' , 'out' , 'Release' , 'bindings' ]
32
- , [ 'module_root' , 'Release' , 'bindings' ]
33
- // Legacy from node-waf, node <= 0.4.x
34
- , [ 'module_root' , 'build' , 'default' , 'bindings' ]
35
- // Production "Release" buildtype binary (meh...)
36
- , [ 'module_root' , 'compiled' , 'version' , 'platform' , 'arch' , 'bindings' ]
37
- // node-qbs builds
38
- , [ 'module_root' , 'addon-build' , 'release' , 'install-root' , 'bindings' ]
39
- , [ 'module_root' , 'addon-build' , 'debug' , 'install-root' , 'bindings' ]
40
- , [ 'module_root' , 'addon-build' , 'default' , 'install-root' , 'bindings' ]
41
- // node-pre-gyp path ./lib/binding/{node_abi}-{platform}-{arch}
42
- , [ 'module_root' , 'lib' , 'binding' , 'nodePreGyp' , 'bindings' ]
43
- ]
44
- }
5
+ var fs = require ( 'fs' ) ,
6
+ path = require ( 'path' ) ,
7
+ fileURLToPath = require ( 'file-uri-to-path' ) ,
8
+ join = path . join ,
9
+ dirname = path . dirname ,
10
+ exists =
11
+ ( fs . accessSync &&
12
+ function ( path ) {
13
+ try {
14
+ fs . accessSync ( path ) ;
15
+ } catch ( e ) {
16
+ return false ;
17
+ }
18
+ return true ;
19
+ } ) ||
20
+ fs . existsSync ||
21
+ path . existsSync ,
22
+ defaults = {
23
+ arrow : process . env . NODE_BINDINGS_ARROW || ' → ' ,
24
+ compiled : process . env . NODE_BINDINGS_COMPILED_DIR || 'compiled' ,
25
+ platform : process . platform ,
26
+ arch : process . arch ,
27
+ nodePreGyp :
28
+ 'node-v' +
29
+ process . versions . modules +
30
+ '-' +
31
+ process . platform +
32
+ '-' +
33
+ process . arch ,
34
+ version : process . versions . node ,
35
+ bindings : 'bindings.node' ,
36
+ try : [
37
+ // node-gyp's linked version in the "build" dir
38
+ [ 'module_root' , 'build' , 'bindings' ] ,
39
+ // node-waf and gyp_addon (a.k.a node-gyp)
40
+ [ 'module_root' , 'build' , 'Debug' , 'bindings' ] ,
41
+ [ 'module_root' , 'build' , 'Release' , 'bindings' ] ,
42
+ // Debug files, for development (legacy behavior, remove for node v0.9)
43
+ [ 'module_root' , 'out' , 'Debug' , 'bindings' ] ,
44
+ [ 'module_root' , 'Debug' , 'bindings' ] ,
45
+ // Release files, but manually compiled (legacy behavior, remove for node v0.9)
46
+ [ 'module_root' , 'out' , 'Release' , 'bindings' ] ,
47
+ [ 'module_root' , 'Release' , 'bindings' ] ,
48
+ // Legacy from node-waf, node <= 0.4.x
49
+ [ 'module_root' , 'build' , 'default' , 'bindings' ] ,
50
+ // Production "Release" buildtype binary (meh...)
51
+ [ 'module_root' , 'compiled' , 'version' , 'platform' , 'arch' , 'bindings' ] ,
52
+ // node-qbs builds
53
+ [ 'module_root' , 'addon-build' , 'release' , 'install-root' , 'bindings' ] ,
54
+ [ 'module_root' , 'addon-build' , 'debug' , 'install-root' , 'bindings' ] ,
55
+ [ 'module_root' , 'addon-build' , 'default' , 'install-root' , 'bindings' ] ,
56
+ // node-pre-gyp path ./lib/binding/{node_abi}-{platform}-{arch}
57
+ [ 'module_root' , 'lib' , 'binding' , 'nodePreGyp' , 'bindings' ]
58
+ ]
59
+ } ;
45
60
46
61
/**
47
62
* The main `bindings()` function loads the compiled bindings for a given module.
48
63
* It uses V8's Error API to determine the parent filename that this function is
49
64
* being invoked from, which is then used to find the root directory.
50
65
*/
51
66
52
- function bindings ( opts ) {
53
-
67
+ function bindings ( opts ) {
54
68
// Argument surgery
55
69
if ( typeof opts == 'string' ) {
56
- opts = { bindings : opts }
70
+ opts = { bindings : opts } ;
57
71
} else if ( ! opts ) {
58
- opts = { }
72
+ opts = { } ;
59
73
}
60
74
61
75
// maps `defaults` onto `opts` object
@@ -65,95 +79,106 @@ function bindings (opts) {
65
79
66
80
// Get the module root
67
81
if ( ! opts . module_root ) {
68
- opts . module_root = exports . getRoot ( exports . getFileName ( ) )
82
+ opts . module_root = exports . getRoot ( exports . getFileName ( ) ) ;
69
83
}
70
84
71
85
// Ensure the given bindings name ends with .node
72
86
if ( path . extname ( opts . bindings ) != '.node' ) {
73
- opts . bindings += '.node'
87
+ opts . bindings += '.node' ;
74
88
}
75
89
76
90
// https://github.com/webpack/webpack/issues/4175#issuecomment-342931035
77
- var requireFunc = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require
78
-
79
- var tries = [ ]
80
- , i = 0
81
- , l = opts . try . length
82
- , n
83
- , b
84
- , err
85
-
86
- for ( ; i < l ; i ++ ) {
87
- n = join . apply ( null , opts . try [ i ] . map ( function ( p ) {
88
- return opts [ p ] || p
89
- } ) )
90
- tries . push ( n )
91
+ var requireFunc =
92
+ typeof __webpack_require__ === 'function'
93
+ ? __non_webpack_require__
94
+ : require ;
95
+
96
+ var tries = [ ] ,
97
+ i = 0 ,
98
+ l = opts . try . length ,
99
+ n ,
100
+ b ,
101
+ err ;
102
+
103
+ for ( ; i < l ; i ++ ) {
104
+ n = join . apply (
105
+ null ,
106
+ opts . try [ i ] . map ( function ( p ) {
107
+ return opts [ p ] || p ;
108
+ } )
109
+ ) ;
110
+ tries . push ( n ) ;
91
111
try {
92
- b = opts . path ? requireFunc . resolve ( n ) : requireFunc ( n )
112
+ b = opts . path ? requireFunc . resolve ( n ) : requireFunc ( n ) ;
93
113
if ( ! opts . path ) {
94
- b . path = n
114
+ b . path = n ;
95
115
}
96
- return b
116
+ return b ;
97
117
} catch ( e ) {
98
118
if ( ! / n o t f i n d / i. test ( e . message ) ) {
99
- throw e
119
+ throw e ;
100
120
}
101
121
}
102
122
}
103
123
104
- err = new Error ( 'Could not locate the bindings file. Tried:\n'
105
- + tries . map ( function ( a ) { return opts . arrow + a } ) . join ( '\n' ) )
106
- err . tries = tries
107
- throw err
124
+ err = new Error (
125
+ 'Could not locate the bindings file. Tried:\n' +
126
+ tries
127
+ . map ( function ( a ) {
128
+ return opts . arrow + a ;
129
+ } )
130
+ . join ( '\n' )
131
+ ) ;
132
+ err . tries = tries ;
133
+ throw err ;
108
134
}
109
- module . exports = exports = bindings
110
-
135
+ module . exports = exports = bindings ;
111
136
112
137
/**
113
138
* Gets the filename of the JavaScript file that invokes this function.
114
139
* Used to help find the root directory of a module.
115
140
* Optionally accepts an filename argument to skip when searching for the invoking filename
Has a conversation. Original line has a conversation. 116
141
*/
117
142
118
- exports . getFileName = function getFileName ( calling_file ) {
119
- var origPST = Error . prepareStackTrace
120
- , origSTL = Error . stackTraceLimit
121
- , dummy = { }
122
- , fileName
143
+ exports . getFileName = function getFileName ( calling_file ) {
144
+ var origPST = Error . prepareStackTrace ,
145
+ origSTL = Error . stackTraceLimit ,
146
+ dummy = { } ,
147
+ fileName ;
123
148
124
- Error . stackTraceLimit = 10
149
+ Error . stackTraceLimit = 10 ;
125
150
126
- Error . prepareStackTrace = function ( e , st ) {
127
- for ( var i = 0 , l = st . length ; i < l ; i ++ ) {
128
- fileName = st [ i ] . getFileName ( )
151
+ Error . prepareStackTrace = function ( e , st ) {
152
+ for ( var i = 0 , l = st . length ; i < l ; i ++ ) {
153
+ fileName = st [ i ] . getFileName ( ) ;
129
154
if ( fileName !== __filename ) {
130
155
if ( calling_file ) {
131
- if ( fileName !== calling_file ) {
132
- return
133
- }
156
+ if ( fileName !== calling_file ) {
157
+ return ;
158
+ }
134
159
} else {
135
- return
160
+ return ;
136
161
}
137
162
}
138
163
}
139
- }
164
+ } ;
140
165
141
166
// run the 'prepareStackTrace' function above
142
- Error . captureStackTrace ( dummy )
143
- dummy . stack
167
+ Error . captureStackTrace ( dummy ) ;
168
+ dummy . stack ;
144
169
145
170
// cleanup
146
- Error . prepareStackTrace = origPST
147
- Error . stackTraceLimit = origSTL
171
+ Error . prepareStackTrace = origPST ;
172
+ Error . stackTraceLimit = origSTL ;
148
173
149
174
// handle filename that starts with "file://"
150
- var fileSchema = " file://" ;
175
+ var fileSchema = ' file://' ;
151
176
if ( fileName . indexOf ( fileSchema ) === 0 ) {
152
177
fileName = fileURLToPath ( fileName ) ;
153
178
}
154
179
155
- return fileName
156
- }
180
+ return fileName ;
181
+ } ;
157
182
158
183
/**
159
184
* Gets the root directory of a module, given an arbitrary filename
@@ -164,25 +189,31 @@ exports.getFileName = function getFileName (calling_file) {
164
189
* Out: /home/nate/node-native-module
165
190
*/
166
191
167
- exports . getRoot = function getRoot ( file ) {
168
- var dir = dirname ( file )
169
- , prev
192
+ exports . getRoot = function getRoot ( file ) {
193
+ var dir = dirname ( file ) ,
194
+ prev ;
170
195
while ( true ) {
171
196
if ( dir === '.' ) {
172
197
// Avoids an infinite loop in rare cases, like the REPL
173
- dir = process . cwd ( )
198
+ dir = process . cwd ( ) ;
174
199
}
175
- if ( exists ( join ( dir , 'package.json' ) ) || exists ( join ( dir , 'node_modules' ) ) ) {
200
+ if (
201
+ exists ( join ( dir , 'package.json' ) ) ||
202
+ exists ( join ( dir , 'node_modules' ) )
203
+ ) {
176
204
// Found the 'package.json' file or 'node_modules' dir; we're done
177
- return dir
205
+ return dir ;
178
206
}
179
207
if ( prev === dir ) {
180
208
// Got to the top
181
- throw new Error ( 'Could not find module root given file: "' + file
182
- + '". Do you have a `package.json` file? ' )
209
+ throw new Error (
210
+ 'Could not find module root given file: "' +
211
+ file +
212
+ '". Do you have a `package.json` file? '
213
+ ) ;
183
214
}
184
215
// Try the parent dir next
185
- prev = dir
186
- dir = join ( dir , '..' )
216
+ prev = dir ;
217
+ dir = join ( dir , '..' ) ;
187
218
}
188
- }
219
+ } ;