Skip to content

Commit 99ea9d0

Browse files
devinivyBig Room Studios
andauthored
Support for hapi v21, node v18, ESM tests (#132)
* 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 <bigroomstudios@Jaclyns-MacBook-Pro.local>
1 parent 071810c commit 99ea9d0

File tree

7 files changed

+134
-93
lines changed

7 files changed

+134
-93
lines changed

.github/workflows/ci-plugin.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ on:
1010
jobs:
1111
test:
1212
uses: hapijs/.github/.github/workflows/ci-plugin.yml@master
13+
with:
14+
min-node-version: 14
15+
min-hapi-version: 20

API.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,12 @@ const H2o2 = require('@hapi/h2o2');
1313
const start = async function() {
1414

1515
const server = Hapi.server();
16-
try {
17-
await server.register(H2o2);
18-
await server.start();
19-
20-
console.log(`Server started at: ${server.info.uri}`);
21-
}
22-
catch(e) {
23-
console.log('Failed to load h2o2');
24-
}
25-
}
16+
17+
await server.register(H2o2);
18+
await server.start();
19+
20+
console.log(`Server started at: ${server.info.uri}`);
21+
};
2622

2723
start();
2824
```

LICENSE.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
Copyright (c) 2012-2020, Sideway Inc, and project contributors
2-
Copyright (c) 2012-2014, Walmart.
1+
Copyright (c) 2012-2022, Project contributors
2+
Copyright (c) 2012-2020, Sideway Inc
3+
Copyright (c) 2012-2014, Walmart.
34
All rights reserved.
45

56
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

lib/index.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const Http = require('http');
44
const Https = require('https');
5+
const Url = require('url');
56

67
const Hoek = require('@hapi/hoek');
78
const Validate = require('@hapi/validate');
@@ -64,9 +65,8 @@ internals.schema = Validate.object({
6465
exports.plugin = {
6566
pkg: require('../package.json'),
6667
requirements: {
67-
hapi: '>=19.0.0'
68+
hapi: '>=20.0.0'
6869
},
69-
7070
register: function (server, options) {
7171

7272
internals.defaults = Hoek.applyToDefaults(internals.defaults, options);
@@ -83,7 +83,7 @@ internals.handler = function (route, handlerOptions) {
8383
const settings = Hoek.applyToDefaults(internals.defaults, handlerOptions, { shallow: ['agent'] });
8484
Validate.assert(handlerOptions, internals.schema, 'Invalid proxy handler options (' + route.path + ')');
8585
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');
86-
settings.mapUri = handlerOptions.mapUri || internals.mapUri(handlerOptions.protocol, handlerOptions.host, handlerOptions.port, handlerOptions.uri);
86+
settings.mapUri = handlerOptions.mapUri ?? internals.mapUri(handlerOptions.protocol, handlerOptions.host, handlerOptions.port, handlerOptions.uri);
8787

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

159159
let downstreamStartTime;
160160
if (settings.downstreamResponseTime) {
161-
downstreamStartTime = process.hrtime();
161+
downstreamStartTime = process.hrtime.bigint();
162162
}
163163

164164
const promise = settings.httpClient.request(request.method, uri, options);
@@ -175,14 +175,14 @@ internals.handler = function (route, handlerOptions) {
175175
try {
176176
var res = await promise;
177177
if (settings.downstreamResponseTime) {
178-
const downstreamResponseTime = process.hrtime(downstreamStartTime);
179-
request.log(['h2o2', 'success'], { downstreamResponseTime: downstreamResponseTime[0] * internals.NS_PER_SEC + downstreamResponseTime[1] });
178+
const downstreamResponseTime = Number(process.hrtime.bigint() - downstreamStartTime);
179+
request.log(['h2o2', 'success'], { downstreamResponseTime });
180180
}
181181
}
182182
catch (err) {
183183
if (settings.downstreamResponseTime) {
184-
const downstreamResponseTime = process.hrtime(downstreamStartTime);
185-
request.log(['h2o2', 'error'], { downstreamResponseTime: downstreamResponseTime[0] * internals.NS_PER_SEC + downstreamResponseTime[1] });
184+
const downstreamResponseTime = Number(process.hrtime.bigint() - downstreamStartTime);
185+
request.log(['h2o2', 'error'], { downstreamResponseTime });
186186
}
187187

188188
if (settings.onResponse) {
@@ -266,10 +266,10 @@ internals.mapUri = function (protocol, host, port, uri) {
266266
protocol += ':';
267267
}
268268

269-
protocol = protocol || 'http:';
269+
protocol = protocol ?? 'http:';
270270

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

274274
return function (request) {
275275

package.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"repository": "git://github.com/hapijs/h2o2",
66
"main": "lib/index.js",
77
"engines": {
8-
"node": ">=12.0.0"
8+
"node": ">=14.0.0"
99
},
1010
"files": [
1111
"lib"
@@ -23,18 +23,18 @@
2323
]
2424
},
2525
"dependencies": {
26-
"@hapi/boom": "9.x.x",
27-
"@hapi/hoek": "9.x.x",
28-
"@hapi/validate": "1.x.x",
29-
"@hapi/wreck": "17.x.x"
26+
"@hapi/boom": "^10.0.0",
27+
"@hapi/hoek": "^10.0.0",
28+
"@hapi/validate": "^2.0.0",
29+
"@hapi/wreck": "^18.0.0"
3030
},
3131
"devDependencies": {
32-
"@hapi/code": "8.x.x",
33-
"@hapi/eslint-plugin": "*",
34-
"@hapi/hapi": "20.x.x",
35-
"@hapi/inert": "6.x.x",
36-
"@hapi/lab": "24.x.x",
37-
"@hapi/teamwork": "5.x.x"
32+
"@hapi/code": "^9.0.0",
33+
"@hapi/eslint-plugin": "^6.0.0",
34+
"@hapi/hapi": "^21.0.0-beta.1",
35+
"@hapi/inert": "^7.0.0",
36+
"@hapi/lab": "^25.0.1",
37+
"@hapi/teamwork": "^6.0.0"
3838
},
3939
"scripts": {
4040
"test": "lab -a @hapi/code -t 100 -L",

test/esm.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
const Code = require('@hapi/code');
4+
const Lab = require('@hapi/lab');
5+
6+
7+
const { before, describe, it } = exports.lab = Lab.script();
8+
const expect = Code.expect;
9+
10+
11+
describe('import()', () => {
12+
13+
let H2o2;
14+
15+
before(async () => {
16+
17+
H2o2 = await import('../lib/index.js');
18+
});
19+
20+
it('exposes all methods and classes as named imports', () => {
21+
22+
expect(Object.keys(H2o2)).to.equal([
23+
'default',
24+
'plugin'
25+
]);
26+
});
27+
});

0 commit comments

Comments
 (0)