electrode-hapi-compat
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -0,3 +1,8 @@ | ||
## [v1.1.0] 09-13-2019 | ||
- CEECORE-912, update electrode-hapi-compat to be aware of @hapi/hapi | ||
## [v1.0.0] 10-11-2018 | ||
- Initial release. |
"use strict"; | ||
/* eslint-disable global-require */ | ||
const isValidObject = require("./is-valid-object"); | ||
const tryRequire = require("./try-require"); | ||
const HAPI16 = 16; | ||
const HAPI17 = 17; | ||
const HAPI18 = 18; | ||
function hapiMajor() { | ||
let hapiPkg; | ||
try { | ||
hapiPkg = require("hapi/package"); | ||
} catch (err) { | ||
// ignore | ||
let hapiPkg = tryRequire("@hapi/hapi/package"); | ||
if (!hapiPkg) { | ||
hapiPkg = tryRequire("hapi/package"); | ||
} | ||
return hapiPkg ? +hapiPkg.version.split(".")[0] : HAPI16; | ||
if (isValidObject(hapiPkg) && hapiPkg.version) { | ||
return +hapiPkg.version.split(".")[0]; | ||
} | ||
return HAPI16; | ||
} | ||
let _isHapi17 = hapiMajor() >= HAPI17; | ||
const hapiV = hapiMajor(); | ||
let _isHapi17 = hapiV >= HAPI17; | ||
let _isHapi18 = hapiV >= HAPI18; | ||
function isHapi17() { | ||
@@ -22,2 +31,6 @@ return _isHapi17; | ||
function isHapi18OrUp() { | ||
return _isHapi18; | ||
} | ||
function _testSetHapi17(isHapi17Flag) { | ||
@@ -27,6 +40,10 @@ _isHapi17 = isHapi17Flag; | ||
function _testSetHapi18(isHapi18Flag) { | ||
_isHapi18 = isHapi18Flag; | ||
} | ||
/** | ||
* Detects the installed Hapi version and use the appropriate plugin | ||
* | ||
* @param {*} registers: { hapi16, hapi17 } contains registers for hapi16, hapi17 | ||
* @param {*} registers: { hapi16, hapi17 } contains registers for hapi16, hapi17/18 | ||
* @param {*} pkg : package for a hapi plugin | ||
@@ -37,5 +54,5 @@ * @returns {*}: Hapi plugin appropriate for installed version | ||
function universalHapiPlugin(registers, pkg) { | ||
if (isHapi17()) { | ||
if (_isHapi17 || _isHapi18) { | ||
return { | ||
register: registers.hapi17, | ||
register: registers.hapi17OrUp || registers.hapi17, | ||
pkg | ||
@@ -52,5 +69,8 @@ }; | ||
module.exports = { | ||
hapiMajor, | ||
isHapi17, | ||
isHapi18OrUp, | ||
universalHapiPlugin, | ||
_testSetHapi17 | ||
_testSetHapi17, | ||
_testSetHapi18 | ||
}; |
{ | ||
"name": "electrode-hapi-compat", | ||
"version": "1.0.0", | ||
"description": "Electrode Hapi 16/17 utility", | ||
"version": "1.1.0", | ||
"description": "Electrode Hapi 16/17/18 utility", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -1,2 +0,2 @@ | ||
# Electorde Hapi Compatibility Utility | ||
# Electrode Hapi Compatibility Utility | ||
@@ -26,2 +26,47 @@ A utility function that detects the Hapi version and return the appropriate plugin function. | ||
## Checking for Hapi 17 | ||
```js | ||
const {isHapi17} = require("electrode-hapi-compat"); | ||
if(isHapi17()) { | ||
// hapi 17 | ||
} else { | ||
// hapi 16 | ||
} | ||
``` | ||
## Checking for Hapi 18 | ||
```js | ||
// this is to identify if @hapi/hapi v18 and above | ||
const {isHapi18OrUp} = require("electrode-hapi-compat"); | ||
if(isHapi18OrUp()) { | ||
// @hapi/hapi >= 18 | ||
} else { | ||
// hapi 16/17 | ||
} | ||
``` | ||
## Testing | ||
To test a module that uses this library, use the `_testSetHapi17()` function. | ||
```js | ||
const {_testSetHapi17} = require("electrode-hapi-compat"); | ||
it("Test Hapi 17", () => { | ||
_testSetHapi17(true); | ||
delete require.cache["my-module-that-uses-hapi-compat"]; | ||
const module = require("my-module-that-uses-hapi-compat"); | ||
// test hapi 17 module | ||
module.isHapi17(); // true | ||
}); | ||
``` | ||
```_testSetHapi18()``` would do the same as above but for @hapi/hapi v18 and above | ||
Note the function needs to be called before you import the library. Also delete your require cache for that library. | ||
# Install | ||
@@ -28,0 +73,0 @@ |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
4986
7
80
94
1