Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc / src / grpc_extension.js
1 /**
2  * @license
3  * Copyright 2016 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 /**
20  * @module
21  * @private
22  */
23
24 'use strict';
25
26 var binary = require('node-pre-gyp/lib/pre-binding');
27 var path = require('path');
28 var binding_path =
29     binary.find(path.resolve(path.join(__dirname, '../package.json')));
30 var binding;
31 try {
32   binding = require(binding_path);
33 } catch (e) {
34   let fs = require('fs');
35   let searchPath = path.dirname(path.dirname(binding_path));
36   let searchName = path.basename(path.dirname(binding_path));
37   let foundNames;
38   try {
39     foundNames = fs.readdirSync(searchPath);
40   } catch (readDirError) {
41     let message = `The gRPC binary module was not installed. This may be fixed by running "npm rebuild"
42 Original error: ${e.message}`;
43     let error = new Error(message);
44     error.code = e.code;
45     throw error;
46   }
47   if (foundNames.indexOf(searchName) === -1) {
48     let message = `Failed to load gRPC binary module because it was not installed for the current system
49 Expected directory: ${searchName}
50 Found: [${foundNames.join(', ')}]
51 This problem can often be fixed by running "npm rebuild" on the current system
52 Original error: ${e.message}`;
53     let error = new Error(message);
54     error.code = e.code;
55     throw error;
56   } else {
57     e.message = `Failed to load ${binding_path}. ${e.message}`;
58     throw e;
59   }
60 }
61
62 module.exports = binding;