Skip to content

Commit

Permalink
J'accuse pem! (#375)
Browse files Browse the repository at this point in the history
Turns out `pem`'s main export will install its own global `process.emit`
source map handler, for who-the-muffin-knows-why. But in any case, (a)
it shouldn't, but thankfully (b) it's at least possible to avoid it
doing that. So this PR avoids the installation and adds a test to make
sure it doesn't start happening again. FWIW I was a bit wary when I
started using `pem` in the first place, and this experience makes me
reluctant to leave it in, in the long term.

See <Dexus/pem#389> and
<jestjs/jest#15077> for more details. And
thanks very much to @SimenB and @nwalters512 for their assistance.
  • Loading branch information
danfuzz authored May 21, 2024
2 parents 34d4b4b + fa708e3 commit d12284f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/net-util/export/CertUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

import * as net from 'node:net';

import pem from 'pem';
import pem from 'pem/lib/pem.js';

import { MustBe } from '@this/typey';

// Note: See <https://github.com/Dexus/pem/issues/389> about the import of
// `pem/lib/pem`.

/**
* Utilities for handling various sorts of certificatey stuff.
Expand Down
14 changes: 14 additions & 0 deletions src/net-util/tests/CertUtil.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2022-2024 the Lactoserv Authors (Dan Bornstein et alia).
// SPDX-License-Identifier: Apache-2.0

import process from 'node:process';

import { CertUtil } from '@this/net-util';


Expand Down Expand Up @@ -67,9 +69,21 @@ describe('makeSelfSignedPair()', () => {
test('produces a valid-looking result, given valid arguments', async () => {
const got = await CertUtil.makeSelfSignedPair(['localhost', '127.0.0.1', '::1', '*.foo.bar']);

// ...and also, doesn't try to override `process.emit` as inherited in
// `process` from `EventEmitter`. See
// <https://github.com/Dexus/pem/issues/389> and
// <https://github.com/jestjs/jest/issues/15077>.
const processEmit = process.emit;
const hasOwnEmit = Object.hasOwn(process, 'emit');

expect(got).toContainAllKeys(['certificate', 'privateKey']);

expect(() => CertUtil.checkCertificateChain(got.certificate)).not.toThrow();
expect(() => CertUtil.checkPrivateKey(got.privateKey)).not.toThrow();

expect(process.emit).toBe(processEmit);
if (!hasOwnEmit) {
expect(Object.hasOwn(process, 'emit')).toBeFalse();
}
});
});

0 comments on commit d12284f

Please sign in to comment.