Skip to content

JIT specialized code gen for route handlers #4

@aklinker1

Description

@aklinker1

"Compile" request handler functions to custom functions for each route instead of having a single big function that covers all possible cases.

Here's the basic setup:

const schema = { properties: { id: { type: 'number' } } };

function compileHandler(routePath, schema) {
  // 1. Build the logic as a string
  const code = `
    return function handler(req, res) {
      ${schema.properties.id ? 'if(typeof req.body.id !== "number") return res.error();' : ''}
      // Actual logic...
      res.send("Success");
    }
    //# sourceURL=routes/${routePath}.js 
  `;

  // 2. Generate the function
  return new Function(code)();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions