Skip to content

Instantly share code, notes, and snippets.

@sylvaindethier
Created May 7, 2024 06:48
Show Gist options
  • Select an option

  • Save sylvaindethier/d6d374da4fdfbc75fa4c2a945e5f3f33 to your computer and use it in GitHub Desktop.

Select an option

Save sylvaindethier/d6d374da4fdfbc75fa4c2a945e5f3f33 to your computer and use it in GitHub Desktop.

Revisions

  1. sylvaindethier created this gist May 7, 2024.
    34 changes: 34 additions & 0 deletions fastify-https-anchor.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    //@ts-expect-error no declaration files
    import {createSNICallback} from "anchor-pki/auto-cert/sni-callback";

    //@ts-expect-error no declaration files
    import { TermsOfServiceAcceptor } from "anchor-pki/auto-cert/terms-of-service-acceptor";

    import https from "node:https";
    import type { FastifyServerFactory } from "fastify";
    const serverFactory: FastifyServerFactory = (handler) => {
    const SNICallback = createSNICallback({
    name: 'sni-callback',
    tosAcceptors: TermsOfServiceAcceptor.createAny(),
    cacheDir: 'tmp/acme'

    // The following are defaults
    //
    // directoryUrl: process.env.ACME_DIRECTORY_URL,
    // contact: process.env.ACME_CONTACT,
    // externalAccountBinding: {
    // kid: process.env.ACME_KID,
    // hmacKey: process.env.ACME_HMAC_KEY
    // },
    });

    return https.createServer({ SNICallback }, handler);
    }

    import Fastify from "fasitify";
    import { env } from "node:process";
    const fastify = Fastify({
    // serverFactory only for local environment, assuming it's when NODE_ENV is "development"
    ...(env["NODE_ENV"] === "development" ? { serverFactory } : {})
    serverFactory,
    });