rabbit_rabbit
rabbit_rabbit13mo ago

Fresh migrating from serveTls

In my main.ts file for Fresh I have some code I borrowed from source a few minor versions ago to serve HTTPS content when running on localhost. It looks like so
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />

import { ServerContext, start } from '$fresh/server.ts'
import { serveTls } from '$std/http/server.ts'
import manifest from './fresh.gen.ts'
import config from './fresh.config.ts'

const self = Deno.env.get('SELF_URL')

const servePlainHttp = self !== 'https://localhost:8000' ||
Deno.env.get('SERVE_HTTP')

if (servePlainHttp) {
await start(manifest, config)
} else {
const ctx = await ServerContext.fromManifest(manifest, config)
// deno-lint-ignore no-explicit-any
await serveTls(ctx.handler() as any, {
...config,
certFile: './local-certs/localhost.crt',
keyFile: './local-certs/localhost.key',
})
}
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />

import { ServerContext, start } from '$fresh/server.ts'
import { serveTls } from '$std/http/server.ts'
import manifest from './fresh.gen.ts'
import config from './fresh.config.ts'

const self = Deno.env.get('SELF_URL')

const servePlainHttp = self !== 'https://localhost:8000' ||
Deno.env.get('SERVE_HTTP')

if (servePlainHttp) {
await start(manifest, config)
} else {
const ctx = await ServerContext.fromManifest(manifest, config)
// deno-lint-ignore no-explicit-any
await serveTls(ctx.handler() as any, {
...config,
certFile: './local-certs/localhost.crt',
keyFile: './local-certs/localhost.key',
})
}
This is working, but serveTls is marked deprecated in favor of Deno.serve. However, Deno.serve takes a single handler argument, not a handler and a config. How might I go about migrating to Deno.serve here? Thanks!
4 Replies
Deno AI Helper
Deno AI Helper13mo ago
I wasn't able to find the answer to your question in the official Deno documentation. However, based on the code snippet you've provided, I can offer some general advice. The serveTls function is indeed deprecated, and you should now use Deno.serve to create a TLS server. The Deno.serve function has a different API compared to serveTls. To use Deno.serve with TLS, you would need to pass the TLS options within the options object, like so:
import { serve } from "https://deno.land/std/http/server.ts";

const handler = (request: Request): Response => {
// Your request handling logic here
};

await serve(handler, {
hostname: "localhost",
port: 8000,
certFile: "./local-certs/localhost.crt",
keyFile: "./local-certs/localhost.key",
// ... any other config options
});
import { serve } from "https://deno.land/std/http/server.ts";

const handler = (request: Request): Response => {
// Your request handling logic here
};

await serve(handler, {
hostname: "localhost",
port: 8000,
certFile: "./local-certs/localhost.crt",
keyFile: "./local-certs/localhost.key",
// ... any other config options
});
Please note that the above code is a general example and not based on the Deno documentation. You should double-check the accuracy of this advice and consult the latest Deno API documentation or other resources for the most up-to-date information on using Deno.serve with TLS. In your specific case, you would need to adapt the ServerContext and its handler to work with Deno.serve. The ServerContext should provide a handler function that you can pass directly to Deno.serve. If the ServerContext's handler method returns a function compatible with Deno.serve, you can use it like this:
const ctx = await ServerContext.fromManifest(manifest, config);
await Deno.serve(ctx.handler(), {
hostname: "localhost",
port: 8000,
certFile: "./local-certs/localhost.crt",
keyFile: "./local-certs/localhost.key",
// ... any other config options
});
const ctx = await ServerContext.fromManifest(manifest, config);
await Deno.serve(ctx.handler(), {
hostname: "localhost",
port: 8000,
certFile: "./local-certs/localhost.crt",
keyFile: "./local-certs/localhost.key",
// ... any other config options
});
Again, please verify this with the current Deno APIs and the Fresh framework documentation to ensure
marvinh.
marvinh.13mo ago
@rabbit_rabbit The way Deno.serve works is that it will serve via HTTPS when you give it a cert + key and won't if you don't. Since Fresh uses Deno.serve already under the hood, you can simplify your code to something like this:
const servePlainHttp = self !== "https://localhost:8000" ||
Deno.env.get("SERVE_HTTP");

// Add certificate + key for HTTPS
if (!servePlainHttp) {
config.server = {
...config.server,
cert: "./local-certs/localhost.crt",
key: "./local-certs/localhost.key",
};
}

await start(manifest, config);
const servePlainHttp = self !== "https://localhost:8000" ||
Deno.env.get("SERVE_HTTP");

// Add certificate + key for HTTPS
if (!servePlainHttp) {
config.server = {
...config.server,
cert: "./local-certs/localhost.crt",
key: "./local-certs/localhost.key",
};
}

await start(manifest, config);
rabbit_rabbit
rabbit_rabbitOP13mo ago
Thanks @marvinh. ! I tried that and it's still serving HTTP. For a cleaner test to ensure it's not an issue with environment variables I cleared some of the other stuff, so here's my attempt
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />

import { start } from '$fresh/server.ts'
import manifest from './fresh.gen.ts'
import config from './fresh.config.ts'

config.server = {
...config.server,
cert: './local-certs/localhost.crt',
key: './local-certs/localhost.key',
}

await start(manifest, config)
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />

import { start } from '$fresh/server.ts'
import manifest from './fresh.gen.ts'
import config from './fresh.config.ts'

config.server = {
...config.server,
cert: './local-certs/localhost.crt',
key: './local-certs/localhost.key',
}

await start(manifest, config)
where I see
$ deno task start
Task start deno task run:trusted --watch=chatbot/,db/,islands/,scheduling/,util/,components/,external-clients/,routes/,static/ dev.ts
Task run:trusted DENO_TLS_CA_STORE=system deno run -A --unsafely-ignore-certificate-errors "--watch=chatbot/,db/,islands/,scheduling/,util/,components/,external-clients/,routes/,static/" "dev.ts"
Watcher Process started.
DANGER: TLS certificate validation is disabled for all hostnames
The manifest has been generated for 41 routes and 22 islands.

🍋 Fresh ready
Local: http://localhost:8000/
$ deno task start
Task start deno task run:trusted --watch=chatbot/,db/,islands/,scheduling/,util/,components/,external-clients/,routes/,static/ dev.ts
Task run:trusted DENO_TLS_CA_STORE=system deno run -A --unsafely-ignore-certificate-errors "--watch=chatbot/,db/,islands/,scheduling/,util/,components/,external-clients/,routes/,static/" "dev.ts"
Watcher Process started.
DANGER: TLS certificate validation is disabled for all hostnames
The manifest has been generated for 41 routes and 22 islands.

🍋 Fresh ready
Local: http://localhost:8000/