Skip to content

Commit

Permalink
Remove custom node server
Browse files Browse the repository at this point in the history
Server.handle() expects standard `Request` objects and returns
`Response` objects. For Node.js you'll need some way to convert to and
from those types. We're using @hono/node-server here to provide an API
similar to Deno.serve or Bun.serve. See this discussion:
nodejs/node#42529
  • Loading branch information
anderspitman committed Jan 8, 2025
1 parent 0f68ac4 commit 2c7b2bc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 86 deletions.
27 changes: 20 additions & 7 deletions examples/minimal/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as decentauth from '../../index.js';
import { argv } from 'node:process';
import { serve } from '@hono/node-server';

const authPrefix = '/auth';

const server = new decentauth.Server({
port: 3000,
const authServer = new decentauth.Server({
config: {
path_prefix: authPrefix,
login_methods: [
Expand All @@ -17,9 +17,22 @@ const server = new decentauth.Server({
},
});

const handler = async (req, ctx) => {
const url = new URL(req.url);
return Response.redirect(`${url.origin}${authPrefix}`, 303);
};
// decentauth.Server.handle() expects standard `Request` objects and returns
// `Response` objects. For Node.js you'll need some way to convert to and from
// those types. We're using @hono/node-server here to provide an API similar to
// Deno.serve or Bun.serve. See this discussion:
// https://github.com/nodejs/node/issues/42529
serve({
async fetch(req) {
const url = new URL(req.url);

if (url.pathname.startsWith(authPrefix)) {
return authServer.handle(req);
}

console.log("Session:", await authServer.getSession(req));

server.serve(handler);
return Response.redirect(`${url.origin}${authPrefix}`, 303);
},
port: 3000,
});
27 changes: 1 addition & 26 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createNodeHandler, callPluginFunction, createWasmHandler } from './wasm.js';
import { callPluginFunction, createWasmHandler } from './wasm.js';
import { JsonKvStore, SqliteKvStore } from './kv.js';

const LOGIN_METHOD_ATPROTO = 'ATProto';
Expand All @@ -14,12 +14,10 @@ class Server {
#kvStore;
#storagePrefix = 'decent_auth';
#config = null;
#port = 3000;
#wasmHandlerPromise;

constructor(opt) {
this.#config = opt?.config;
this.#port = opt?.port;
this.#kvStore = opt?.kvStore;

if (!this.#kvStore) {
Expand All @@ -39,29 +37,6 @@ class Server {
const handler = await this.#wasmHandlerPromise;
return handler(req);
}

async serve(handler) {

await this.#kvStore.ready;

const http = await import('node:http');

const internalHandler = async (req) => {
const url = new URL(req.url);
if (url.pathname.startsWith(this.#config.path_prefix)) {
return this.handle(req);
}
else {
const session = await callPluginFunction('extism_get_session', this.#config, this.#kvStore, req);
const ctx = {
session,
};
return handler(req, ctx);
}
};

http.createServer(createNodeHandler(internalHandler)).listen(this.#port);
}
}

async function getSession(req, kvStore) {
Expand Down
53 changes: 0 additions & 53 deletions wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,60 +151,7 @@ async function createWasmHandler(config, kvStore) {
return handler;
}

function createNodeHandler(handler) {
async function nodeHandler(nodeReq, nodeRes) {

const headers = {};
for (const key in nodeReq.headers) {
if (!headers[key]) {
headers[key] = [];
}
headers[key].push(nodeReq.headers[key]);
}

let body;
if (nodeReq.method !== 'GET' && nodeReq.method !== 'HEAD') {
// TODO: convert this into a readable stream
let data = '';
nodeReq.on('data', (chunk) => {
data += chunk;
});

await new Promise((resolve, reject) => {
nodeReq.on('end', () => {
resolve();
});
});

body = data;
}

const host = headers.host || process.env.HOST || 'localhost';

const req = new Request(`http://${host}${nodeReq.url}`, {
method: nodeReq.method,
headers: nodeReq.headers,
body,
});

const res = await handler(req);

nodeRes.setHeaders(res.headers);
nodeRes.writeHead(res.status);

if (res.body) {
for await (const chunk of res.body) {
nodeRes.write(chunk);
}
}

nodeRes.end();
}
return nodeHandler;
}

export {
createNodeHandler,
createWasmHandler,
createWasmPlugin,
callPluginFunction,
Expand Down

0 comments on commit 2c7b2bc

Please sign in to comment.