fastify-citgm
Advanced tools
Comparing version 4.23.2-dev-14 to 4.23.2-dev-15
'use strict' | ||
const VERSION = '4.23.2-dev-14' | ||
const VERSION = '4.23.2-dev-15' | ||
@@ -5,0 +5,0 @@ const Avvio = require('avvio') |
{ | ||
"name": "fastify-citgm", | ||
"version": "4.23.2-dev-14", | ||
"version": "4.23.2-dev-15", | ||
"description": "Fast and low overhead web framework, for Node.js", | ||
@@ -175,4 +175,4 @@ "main": "fastify.js", | ||
"markdownlint-cli2": "^0.10.0", | ||
"node-forge": "^1.3.1", | ||
"proxyquire": "^2.1.3", | ||
"self-cert": "^2.0.0", | ||
"send": "^0.18.0", | ||
@@ -179,0 +179,0 @@ "simple-get": "^4.0.1", |
'use strict' | ||
const selfCert = require('self-cert') | ||
const os = require('os') | ||
const forge = require('node-forge') | ||
// from self-cert module | ||
function selfCert (opts) { | ||
const options = opts || {} | ||
const log = opts.logger || require('abstract-logging') | ||
const now = new Date() | ||
if (!options.attrs) options.attrs = {} | ||
if (!options.expires) { | ||
options.expires = new Date( | ||
now.getFullYear() + 5, now.getMonth() + 1, now.getDate() | ||
) | ||
} | ||
log.debug('generating key pair') | ||
const keys = forge.pki.rsa.generateKeyPair(options.bits || 2048) | ||
log.debug('key pair generated') | ||
log.debug('generating self-signed certificate') | ||
const cert = forge.pki.createCertificate() | ||
cert.publicKey = keys.publicKey | ||
cert.serialNumber = '01' | ||
cert.validity.notBefore = now | ||
cert.validity.notAfter = options.expires | ||
const attrs = [ | ||
{ name: 'commonName', value: options.attrs.commonName || os.hostname() }, | ||
{ name: 'countryName', value: options.attrs.countryName || 'US' }, | ||
{ name: 'stateOrProvinceName', value: options.attrs.stateName || 'Georgia' }, | ||
{ name: 'localityName', value: options.attrs.locality || 'Atlanta' }, | ||
{ name: 'organizationName', value: options.attrs.orgName || 'None' }, | ||
{ shortName: 'OU', value: options.attrs.shortName || 'example' } | ||
] | ||
cert.setSubject(attrs) | ||
cert.setIssuer(attrs) | ||
cert.setExtensions([ | ||
{ name: 'basicConstraints', cA: true }, | ||
{ | ||
name: 'keyUsage', | ||
keyCertSign: true, | ||
digitalSignature: true, | ||
nonRepudiation: true, | ||
keyEncipherment: true, | ||
dataEncipherment: true | ||
}, | ||
{ | ||
name: 'extKeyUsage', | ||
serverAuth: true, | ||
clientAuth: true, | ||
codeSigning: true, | ||
emailProtection: true, | ||
timeStamping: true | ||
}, | ||
{ | ||
name: 'nsCertType', | ||
client: true, | ||
server: true, | ||
email: true, | ||
objsign: true, | ||
sslCA: true, | ||
emailCA: true, | ||
objCA: true | ||
}, | ||
{ name: 'subjectKeyIdentifier' }, | ||
{ | ||
name: 'subjectAltName', | ||
altNames: [{ type: 6 /* URI */, value: 'DNS: ' + attrs[0].value }].concat((function () { | ||
const interfaces = os.networkInterfaces() | ||
// fix citgm: skip invalid ips (aix72-ppc64) | ||
// TODO memoize | ||
const ips = Object.values(interfaces).flat() | ||
.filter(i => !!forge.util.bytesFromIP(i.address)) | ||
.map(i => ({ type: 7 /* IP */, ip: i.address })) | ||
console.log('*** DEBUG ***') | ||
console.log('os.networkInterfaces()') | ||
console.log(interfaces) | ||
console.log('ips') | ||
console.log(ips) | ||
// original | ||
// const ips = [] | ||
// Object.keys(interfaces).forEach((k) => { | ||
// interfaces[k].forEach((i) => { | ||
// ips.push({ type: 7 /* IP */, ip: i.address }) | ||
// }) | ||
// }) | ||
return ips | ||
}())) | ||
} | ||
]) | ||
cert.sign(keys.privateKey) | ||
log.debug('certificate generated') | ||
return { | ||
privateKey: forge.pki.privateKeyToPem(keys.privateKey), | ||
publicKey: forge.pki.publicKeyToPem(keys.publicKey), | ||
certificate: forge.pki.certificateToPem(cert) | ||
} | ||
} | ||
async function buildCertificate () { | ||
@@ -6,0 +110,0 @@ // "global" is used in here because "t.context" is only supported by "t.beforeEach" and "t.afterEach" |
@@ -424,15 +424,4 @@ 'use strict' | ||
// const DNS_LOOKUP_LOCALHOST_IPV4 = { address: '127.0.0.1', family: 4 } | ||
// const DNS_LOOKUP_LOCALHOST_IPV6 = { address: '::1', family: 6 } | ||
function dnsLookup (host, options) { | ||
// 10/10 dns lookup works on CITGM | ||
// TODO remove at the end | ||
// if (process.env.CITGM) { | ||
// if (options?.all) { | ||
// return [DNS_LOOKUP_LOCALHOST_IPV4, DNS_LOOKUP_LOCALHOST_IPV6] | ||
// } | ||
// return DNS_LOOKUP_LOCALHOST_IPV6 | ||
// } | ||
// TODO memoize | ||
return dns.lookup(host, options) | ||
@@ -439,0 +428,0 @@ } |
@@ -9,2 +9,5 @@ 'use strict' | ||
// fix citgm @aix72-ppc64 | ||
const LISTEN_READYNESS = process.env.CITGM ? 250 : 50 | ||
let localhost | ||
@@ -1070,8 +1073,3 @@ before(async function () { | ||
t.error(err) | ||
if (!process.env.CITGM) { | ||
t.ok(new Date() - startDate < 50) | ||
} else { | ||
console.log('*** DEBUG aix72-ppc64 ***', new Date() - startDate) | ||
t.ok(true) | ||
} | ||
t.ok(new Date() - startDate < LISTEN_READYNESS) | ||
}) | ||
@@ -1094,8 +1092,3 @@ }) | ||
}) | ||
if (!process.env.CITGM) { | ||
t.ok(new Date() - startDate < 50) | ||
} else { | ||
console.log('*** DEBUG aix72-ppc64 ***', new Date() - startDate) | ||
t.ok(true) | ||
} | ||
t.ok(new Date() - startDate < LISTEN_READYNESS) | ||
}) |
@@ -7,4 +7,2 @@ 'use strict' | ||
const { test, before } = require('tap') | ||
const dnsCb = require('node:dns') | ||
const sget = require('simple-get').concat | ||
const Fastify = require('..') | ||
@@ -91,161 +89,1 @@ const helper = require('./helper') | ||
}) | ||
test('listen twice on the same port without callback rejects', t => { | ||
t.plan(1) | ||
const fastify = Fastify() | ||
t.teardown(fastify.close.bind(fastify)) | ||
fastify.listen({ port: 0 }) | ||
.then(() => { | ||
const s2 = Fastify() | ||
t.teardown(s2.close.bind(s2)) | ||
s2.listen({ port: fastify.server.address().port }) | ||
.catch(err => { | ||
t.ok(err) | ||
}) | ||
}) | ||
.catch(err => t.error(err)) | ||
}) | ||
test('listen twice on the same port without callback rejects with (address)', t => { | ||
t.plan(2) | ||
const fastify = Fastify() | ||
t.teardown(fastify.close.bind(fastify)) | ||
fastify.listen({ port: 0 }) | ||
.then(address => { | ||
const s2 = Fastify() | ||
t.teardown(s2.close.bind(s2)) | ||
t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`) | ||
s2.listen({ port: fastify.server.address().port }) | ||
.catch(err => { | ||
t.ok(err) | ||
}) | ||
}) | ||
.catch(err => t.error(err)) | ||
}) | ||
test('listen on invalid port without callback rejects', t => { | ||
const fastify = Fastify() | ||
t.teardown(fastify.close.bind(fastify)) | ||
return fastify.listen({ port: -1 }) | ||
.catch(err => { | ||
t.ok(err) | ||
return true | ||
}) | ||
}) | ||
test('listen logs the port as info', t => { | ||
t.plan(1) | ||
const fastify = Fastify() | ||
t.teardown(fastify.close.bind(fastify)) | ||
const msgs = [] | ||
fastify.log.info = function (msg) { | ||
msgs.push(msg) | ||
} | ||
fastify.listen({ port: 0 }) | ||
.then(() => { | ||
t.ok(/http:\/\//.test(msgs[0])) | ||
}) | ||
}) | ||
test('listen on localhost binds IPv4 and IPv6 - promise interface', async t => { | ||
const localAddresses = await helper.dnsLookup('localhost', { all: true }) | ||
t.plan(2 * localAddresses.length) | ||
const app = Fastify() | ||
app.get('/', async () => 'hello localhost') | ||
t.teardown(app.close.bind(app)) | ||
await app.listen({ port: 0, host: 'localhost' }) | ||
for (const lookup of localAddresses) { | ||
await new Promise((resolve, reject) => { | ||
sget({ | ||
method: 'GET', | ||
url: getUrl(app, lookup) | ||
}, (err, response, body) => { | ||
if (err) { return reject(err) } | ||
t.equal(response.statusCode, 200) | ||
t.same(body.toString(), 'hello localhost') | ||
resolve() | ||
}) | ||
}) | ||
} | ||
}) | ||
test('listen on localhost binds to all interfaces (both IPv4 and IPv6 if present) - callback interface', t => { | ||
dnsCb.lookup('localhost', { all: true }, (err, lookups) => { | ||
t.plan(2 + (3 * lookups.length)) | ||
t.error(err) | ||
const app = Fastify() | ||
app.get('/', async () => 'hello localhost') | ||
app.listen({ port: 0, host: 'localhost' }, (err) => { | ||
t.error(err) | ||
t.teardown(app.close.bind(app)) | ||
for (const lookup of lookups) { | ||
sget({ | ||
method: 'GET', | ||
url: getUrl(app, lookup) | ||
}, (err, response, body) => { | ||
t.error(err) | ||
t.equal(response.statusCode, 200) | ||
t.same(body.toString(), 'hello localhost') | ||
}) | ||
} | ||
}) | ||
}) | ||
}) | ||
test('addresses getter', async t => { | ||
const localAddresses = await helper.dnsLookup('localhost', { all: true }) | ||
t.plan(4) | ||
const app = Fastify() | ||
app.get('/', async () => 'hello localhost') | ||
t.same(app.addresses(), [], 'before ready') | ||
await app.ready() | ||
t.same(app.addresses(), [], 'after ready') | ||
await app.listen({ port: 0, host: 'localhost' }) | ||
const { port } = app.server.address() | ||
for (const address of localAddresses) { | ||
address.port = port | ||
if (typeof address.family === 'number') { | ||
address.family = 'IPv' + address.family | ||
} | ||
} | ||
const appAddresses = app.addresses() | ||
for (const address of appAddresses) { | ||
if (typeof address.family === 'number') { | ||
address.family = 'IPv' + address.family | ||
} | ||
} | ||
localAddresses.sort((a, b) => a.address.localeCompare(b.address)) | ||
appAddresses.sort((a, b) => a.address.localeCompare(b.address)) | ||
// citgm flaky @ rhel8-s390x rhel8-ppc64le debian10-x64 | ||
console.log('*** DEBUG ***') | ||
console.log('localAddresses') | ||
console.log(localAddresses) | ||
console.log('appAddresses') | ||
console.log(appAddresses) | ||
t.same(appAddresses, localAddresses, 'after listen') | ||
await app.close() | ||
t.same(app.addresses(), [], 'after close') | ||
}) | ||
function getUrl (fastify, lookup) { | ||
const { port } = fastify.server.address() | ||
if (lookup.family === 6) { | ||
return `http://[${lookup.address}]:${port}/` | ||
} else { | ||
return `http://${lookup.address}:${port}/` | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2334450
311
57002
3