Skip to content

Commit

Permalink
Support for hapi v21, node v18, ESM tests (#132)
Browse files Browse the repository at this point in the history
* Support node v18, drop node v12, test ESM

* Fix tests for hapi v20/21 and node v18, handling IPv6 server defaults

* Fix tests for broader OS and hapi version support

* Use hrtime.bigint() rather than legacy hrtime()

Co-authored-by: Big Room Studios <[email protected]>
  • Loading branch information
devinivy and Big Room Studios authored Sep 20, 2022
1 parent 071810c commit 99ea9d0
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 93 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci-plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ on:
jobs:
test:
uses: hapijs/.github/.github/workflows/ci-plugin.yml@master
with:
min-node-version: 14
min-hapi-version: 20
16 changes: 6 additions & 10 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,12 @@ const H2o2 = require('@hapi/h2o2');
const start = async function() {

const server = Hapi.server();
try {
await server.register(H2o2);
await server.start();

console.log(`Server started at: ${server.info.uri}`);
}
catch(e) {
console.log('Failed to load h2o2');
}
}

await server.register(H2o2);
await server.start();

console.log(`Server started at: ${server.info.uri}`);
};

start();
```
Expand Down
5 changes: 3 additions & 2 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Copyright (c) 2012-2020, Sideway Inc, and project contributors
Copyright (c) 2012-2014, Walmart.
Copyright (c) 2012-2022, Project contributors
Copyright (c) 2012-2020, Sideway Inc
Copyright (c) 2012-2014, Walmart.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Expand Down
22 changes: 11 additions & 11 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const Http = require('http');
const Https = require('https');
const Url = require('url');

const Hoek = require('@hapi/hoek');
const Validate = require('@hapi/validate');
Expand Down Expand Up @@ -64,9 +65,8 @@ internals.schema = Validate.object({
exports.plugin = {
pkg: require('../package.json'),
requirements: {
hapi: '>=19.0.0'
hapi: '>=20.0.0'
},

register: function (server, options) {

internals.defaults = Hoek.applyToDefaults(internals.defaults, options);
Expand All @@ -83,7 +83,7 @@ internals.handler = function (route, handlerOptions) {
const settings = Hoek.applyToDefaults(internals.defaults, handlerOptions, { shallow: ['agent'] });
Validate.assert(handlerOptions, internals.schema, 'Invalid proxy handler options (' + route.path + ')');
Hoek.assert(!route.settings.payload || ((route.settings.payload.output === 'data' || route.settings.payload.output === 'stream') && !route.settings.payload.parse), 'Cannot proxy if payload is parsed or if output is not stream or data');
settings.mapUri = handlerOptions.mapUri || internals.mapUri(handlerOptions.protocol, handlerOptions.host, handlerOptions.port, handlerOptions.uri);
settings.mapUri = handlerOptions.mapUri ?? internals.mapUri(handlerOptions.protocol, handlerOptions.host, handlerOptions.port, handlerOptions.uri);

if (settings.ttl === 'upstream') {
settings._upstreamTtl = true;
Expand Down Expand Up @@ -158,7 +158,7 @@ internals.handler = function (route, handlerOptions) {

let downstreamStartTime;
if (settings.downstreamResponseTime) {
downstreamStartTime = process.hrtime();
downstreamStartTime = process.hrtime.bigint();
}

const promise = settings.httpClient.request(request.method, uri, options);
Expand All @@ -175,14 +175,14 @@ internals.handler = function (route, handlerOptions) {
try {
var res = await promise;
if (settings.downstreamResponseTime) {
const downstreamResponseTime = process.hrtime(downstreamStartTime);
request.log(['h2o2', 'success'], { downstreamResponseTime: downstreamResponseTime[0] * internals.NS_PER_SEC + downstreamResponseTime[1] });
const downstreamResponseTime = Number(process.hrtime.bigint() - downstreamStartTime);
request.log(['h2o2', 'success'], { downstreamResponseTime });
}
}
catch (err) {
if (settings.downstreamResponseTime) {
const downstreamResponseTime = process.hrtime(downstreamStartTime);
request.log(['h2o2', 'error'], { downstreamResponseTime: downstreamResponseTime[0] * internals.NS_PER_SEC + downstreamResponseTime[1] });
const downstreamResponseTime = Number(process.hrtime.bigint() - downstreamStartTime);
request.log(['h2o2', 'error'], { downstreamResponseTime });
}

if (settings.onResponse) {
Expand Down Expand Up @@ -266,10 +266,10 @@ internals.mapUri = function (protocol, host, port, uri) {
protocol += ':';
}

protocol = protocol || 'http:';
protocol = protocol ?? 'http:';

port = port || (protocol === 'http:' ? 80 : 443);
const baseUrl = protocol + '//' + host + ':' + port;
port = port ?? (protocol === 'http:' ? 80 : 443);
const baseUrl = Url.format({ protocol, hostname: host, port });

return function (request) {

Expand Down
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"repository": "git://github.com/hapijs/h2o2",
"main": "lib/index.js",
"engines": {
"node": ">=12.0.0"
"node": ">=14.0.0"
},
"files": [
"lib"
Expand All @@ -23,18 +23,18 @@
]
},
"dependencies": {
"@hapi/boom": "9.x.x",
"@hapi/hoek": "9.x.x",
"@hapi/validate": "1.x.x",
"@hapi/wreck": "17.x.x"
"@hapi/boom": "^10.0.0",
"@hapi/hoek": "^10.0.0",
"@hapi/validate": "^2.0.0",
"@hapi/wreck": "^18.0.0"
},
"devDependencies": {
"@hapi/code": "8.x.x",
"@hapi/eslint-plugin": "*",
"@hapi/hapi": "20.x.x",
"@hapi/inert": "6.x.x",
"@hapi/lab": "24.x.x",
"@hapi/teamwork": "5.x.x"
"@hapi/code": "^9.0.0",
"@hapi/eslint-plugin": "^6.0.0",
"@hapi/hapi": "^21.0.0-beta.1",
"@hapi/inert": "^7.0.0",
"@hapi/lab": "^25.0.1",
"@hapi/teamwork": "^6.0.0"
},
"scripts": {
"test": "lab -a @hapi/code -t 100 -L",
Expand Down
27 changes: 27 additions & 0 deletions test/esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

const Code = require('@hapi/code');
const Lab = require('@hapi/lab');


const { before, describe, it } = exports.lab = Lab.script();
const expect = Code.expect;


describe('import()', () => {

let H2o2;

before(async () => {

H2o2 = await import('../lib/index.js');
});

it('exposes all methods and classes as named imports', () => {

expect(Object.keys(H2o2)).to.equal([
'default',
'plugin'
]);
});
});
Loading

0 comments on commit 99ea9d0

Please sign in to comment.