|
| 1 | +/** |
| 2 | + * Copyright (c) 2002-2018 "Neo4j," |
| 3 | + * Neo4j Sweden AB [http://neo4j.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import {isEmptyObjectOrNull} from './util'; |
| 21 | + |
| 22 | +let _trustOnFirstUseAvailable = null; |
| 23 | +let _trustAllCertificatesAvailable = null; |
| 24 | +let _dnsLookupAvailable = null; |
| 25 | +let _nodeSocketAvailable = null; |
| 26 | +let _nodeBufferAvailable = null; |
| 27 | + |
| 28 | +export default class Platform { |
| 29 | + |
| 30 | + static trustOnFirstUseAvailable() { |
| 31 | + if (_trustOnFirstUseAvailable == null) { |
| 32 | + try { |
| 33 | + // We are verifying that we have a version of getPeerCertificate |
| 34 | + // that supports reading the whole certificate, eg this commit: |
| 35 | + // https://github.com/nodejs/node/commit/345c40b6 |
| 36 | + require.resolve('tls'); |
| 37 | + const getPeerCertificateFunction = require('tls').TLSSocket.prototype.getPeerCertificate; |
| 38 | + const numberOfParameters = getPeerCertificateFunction.length; |
| 39 | + _trustOnFirstUseAvailable = numberOfParameters >= 1; |
| 40 | + } catch (e) { |
| 41 | + _trustOnFirstUseAvailable = false; |
| 42 | + } |
| 43 | + } |
| 44 | + return _trustOnFirstUseAvailable; |
| 45 | + } |
| 46 | + |
| 47 | + static trustAllCertificatesAvailable() { |
| 48 | + if (_trustAllCertificatesAvailable == null) { |
| 49 | + try { |
| 50 | + require.resolve('tls'); |
| 51 | + const getPeerCertificateFunction = require('tls').TLSSocket.prototype.getPeerCertificate; |
| 52 | + _trustAllCertificatesAvailable = getPeerCertificateFunction && typeof getPeerCertificateFunction === 'function'; |
| 53 | + } catch (e) { |
| 54 | + _trustAllCertificatesAvailable = false; |
| 55 | + } |
| 56 | + } |
| 57 | + return _trustAllCertificatesAvailable; |
| 58 | + } |
| 59 | + |
| 60 | + static dnsLookupAvailable() { |
| 61 | + if (_dnsLookupAvailable == null) { |
| 62 | + try { |
| 63 | + require.resolve('dns'); |
| 64 | + const lookupFunction = require('dns').lookup; |
| 65 | + _dnsLookupAvailable = lookupFunction && typeof lookupFunction === 'function'; |
| 66 | + } catch (e) { |
| 67 | + _dnsLookupAvailable = false; |
| 68 | + } |
| 69 | + } |
| 70 | + return _dnsLookupAvailable; |
| 71 | + } |
| 72 | + |
| 73 | + static nodeSocketAvailable() { |
| 74 | + if (_nodeSocketAvailable == null) { |
| 75 | + try { |
| 76 | + require.resolve('net'); |
| 77 | + const netModule = require('net'); |
| 78 | + _nodeSocketAvailable = !isEmptyObjectOrNull(netModule); |
| 79 | + } catch (e) { |
| 80 | + _nodeSocketAvailable = false; |
| 81 | + } |
| 82 | + } |
| 83 | + return _nodeSocketAvailable; |
| 84 | + } |
| 85 | + |
| 86 | + static nodeBufferAvailable() { |
| 87 | + if (_nodeBufferAvailable == null) { |
| 88 | + try { |
| 89 | + require.resolve('buffer'); |
| 90 | + const bufferModule = require('buffer'); |
| 91 | + _nodeBufferAvailable = !isEmptyObjectOrNull(bufferModule); |
| 92 | + } catch (e) { |
| 93 | + _nodeBufferAvailable = false; |
| 94 | + } |
| 95 | + } |
| 96 | + return _nodeBufferAvailable; |
| 97 | + } |
| 98 | +} |
0 commit comments