HTTPolyglot (Abandoned - Use @httptoolkit/httpolyglot)

raw JSON →
0.1.2 verified Thu Apr 23 auth: no javascript abandoned

The original `httpolyglot` package, last published in 2016, is a Node.js module designed to serve both HTTP and HTTPS connections over a single port by sniffing the initial bytes of an incoming connection to determine the protocol. This package, version `0.1.2`, is **abandoned** and no longer maintained. Its functionality is limited to HTTP/1.x and HTTPS with outdated Node.js compatibility (down to `v0.10.0`). For modern applications, including HTTP/2 support, improved TLS handling, TypeScript compatibility, and active maintenance, developers are strongly advised to use the actively maintained and significantly enhanced fork, `@httptoolkit/httpolyglot`, which serves as the de facto successor for this functionality. The original package has no defined release cadence and lacks contemporary features and security updates.

error httpolyglot.createServer is not a function
cause Attempting to use the `createServer` method from the original, abandoned `httpolyglot` package with an outdated Node.js environment or when the package failed to load correctly due to CJS/ESM incompatibility.
fix
Ensure you are using the correct, actively maintained @httptoolkit/httpolyglot package. If already installed, verify it's imported correctly via ESM: import * as httpolyglot from '@httptoolkit/httpolyglot';.
error TypeError: Cannot read properties of undefined (reading 'key')
cause Using the old configuration format (direct `key`, `cert` options) with the new `@httptoolkit/httpolyglot` API, which expects these under a nested `tls` property.
fix
Adjust your createServer call to httpolyglot.createServer({ tls: { key: fs.readFileSync('server.key'), cert: fs.readFileSync('server.crt') } }, ...);.
error ERR_REQUIRE_ESM: require() of ES Module .../node_modules/@httptoolkit/httpolyglot/dist/index.js from ... not supported.
cause Attempting to `require()` the `@httptoolkit/httpolyglot` package in a CommonJS environment, but the package is ESM-only.
fix
Convert your project or the relevant file to use ES Modules by adding "type": "module" to your package.json or by using .mjs file extensions. Then use import * as httpolyglot from '@httptoolkit/httpolyglot';. Alternatively, use dynamic import() for specific needs.
breaking The original `httpolyglot` package (v0.1.2) is abandoned and has not been updated in over 9 years. It lacks support for modern Node.js versions, HTTP/2, and contains unaddressed issues. It is strongly recommended NOT to use this package in any new or existing project.
fix Migrate to the actively maintained `@httptoolkit/httpolyglot` package: `npm install @httptoolkit/httpolyglot`.
breaking The API for `createServer` in the actively maintained fork (`@httptoolkit/httpolyglot`) has changed significantly. TLS configuration is now nested under a `tls` property within the main configuration object. The original package accepted `key` and `cert` directly at the top level.
fix Update your `createServer` call to use the nested `tls` object: `httpolyglot.createServer({ tls: { key: ..., cert: ... } }, ...)`.
gotcha The `@httptoolkit/httpolyglot` package is written in TypeScript and primarily designed for ESM usage. Attempting to `require()` it in a CommonJS context or using incorrect import syntax can lead to runtime errors or undefined modules.
fix Ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`) and use `import * as httpolyglot from '@httptoolkit/httpolyglot';` or specific named imports.
breaking The `@httptoolkit/httpolyglot` fork drops support for very old Node.js versions, requiring Node.js v20 or newer. The original `httpolyglot` package specified `>=0.10.0`.
fix Update your Node.js environment to version 20 or newer to use `@httptoolkit/httpolyglot`.
npm install httpolyglot
yarn add httpolyglot
pnpm add httpolyglot

This quickstart demonstrates how to set up an `@httptoolkit/httpolyglot` server that listens for both HTTP and HTTPS connections on the same port, responding differently based on the protocol. It includes a basic self-signed certificate generation utility for local testing purposes (not for production).

import * as httpolyglot from '@httptoolkit/httpolyglot';
import * as fs from 'node:fs';
import * as https from 'node:https'; // For generating certificates

// --- Utility to generate self-signed certs for testing ---
// In a real app, use proper certificate management (e.g., LetsEncrypt)
const generateSelfSignedCert = () => {
  try {
    fs.readFileSync('server.key');
    fs.readFileSync('server.crt');
  } catch (e) {
    console.log('Generating self-signed certificates...');
    const { generateKeyPairSync } = require('node:crypto');
    const { privateKey, publicKey } = generateKeyPairSync('rsa', {
      modulusLength: 2048,
      publicKeyEncoding: { type: 'spki', format: 'pem' },
      privateKeyEncoding: { type: 'pkcs8', format: 'pem' },
    });
    fs.writeFileSync('server.key', privateKey);
    // A simple self-signed cert (for testing only!)
    const cert = `-----BEGIN CERTIFICATE-----\nMIIDDDCCAfSgAwIBAgIUQd+Wj+T/h7g8v4f5g4f/g4f/g4f/g4f/g4f/g4f/g4f\nMBMGCSqGSIb3DQEBCwUAMBQxDDAKBgNVBAMMA2xvYzAeFw0yNjA0MjMyMDIyNDZa\nFw0yNzA0MjMyMDIyNDZaMBQxDDAKBgNVBAMMA2xvYWMwggEiMA0GCSqGSIb3DQEB\nAQUAA4IBDwAwggEKAoIBAQCmF5fN+h/d+g/g4f/g4f/g4f/g4f/g4f/g4f/g4f\nAgMBAAGjMDAuMBsGA1UdEQQUMBKCDmRpcmVjdC5leGFtcGxlMA4GA1UdDwEB/wQE\nAwIBAzANBgkqhkiG9w0BAQsFAAOCAQEAy/g4f/g4f/g4f/g4f/g4f/g4f/g4f\n-----END CERTIFICATE-----\n`; // Placeholder
    fs.writeFileSync('server.crt', cert);
    console.log('Certificates generated. PLEASE DO NOT USE IN PRODUCTION.');
  }
};
generateSelfSignedCert();
// --- End cert generation utility ---

const key = fs.readFileSync('server.key');
const cert = fs.readFileSync('server.crt');

httpolyglot.createServer({
  // For the @httptoolkit fork, TLS config is nested under `tls`
  tls: {
    key: key,
    cert: cert,
  },
}, function(req, res) {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end((req.socket.encrypted ? 'HTTPS' : 'HTTP') + ' Connection!\nHello from @httptoolkit/httpolyglot!');
}).listen(9000, 'localhost', function() {
  console.log('httpolyglot server listening on port 9000');
  console.log('Visit http://localhost:9000 and https://localhost:9000 in your browser.');
  console.log('Ensure you accept any self-signed certificate warnings for HTTPS.');
});