@curt also these kinds of errors:
TypeError: parser.onIncoming is not a function
at HTTPParser.parserOnHeadersComplete (node:_http_common:117:17)
This error is coming from the Ziti SDK's HTTP parser integration with Node.js. The specific error:
This suggests there's an incompatibility between how the Ziti SDK is handling HTTP requests and how Node.js expects them to be handled. The error occurs in the socket handling layer when Ziti tries to process incoming HTTP requests....is what Cursor AI says
The code:
import express from 'express';
import next from 'next';
import { parse } from 'url';
import { initializeZiti, getZitiExpressWrapper } from './src/lib/ziti/config';
import type { Request, Response } from 'express';
import type { NextApiRequest } from 'next';
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
async function main() {
try {
// Initialize Next.js first
await app.prepare();
const identityFile = process.env.ZITI_IDENTITY_FILE || '/mnt/c/Users/user/Documents/c2c/openziti/node-server.json';
const serviceName = process.env.ZITI_SERVICE_NAME || 'openziti-dark-service';
// Initialize Ziti
const zitiInitialized = await initializeZiti({
serviceConfig: {
name: serviceName,
identityFile
}
});
if (!zitiInitialized) {
throw new Error('Failed to initialize Ziti');
}
// Create Express app first
const expressApp = express();
// Add basic middleware
expressApp.use(express.json());
expressApp.use(express.urlencoded({ extended: true }));
// Handle root path explicitly
expressApp.get('/', async (req: Request, res: Response) => {
try {
const query = (req as unknown as NextApiRequest).query;
await app.render(req, res, '/', query);
} catch (err) {
console.error('Error rendering root path:', err);
res.status(500).send('Internal Server Error');
}
});
// Handle all other routes
expressApp.all('*', async (req: Request, res: Response) => {
try {
const parsedUrl = parse(req.url!, true);
await handle(req, res, parsedUrl);
} catch (err) {
console.error('Error occurred handling', req.url, err);
res.status(500).send('Internal Server Error');
}
});
// Create Ziti-wrapped Express app
const server = getZitiExpressWrapper(expressApp, serviceName);
// Start listening
server.listen(3000, (err?: Error) => {
if (err) throw err;
console.log(`> Ready on Ziti service: ${serviceName}`);
});
// Add error handler for the server
server.on('error', (err: Error) => {
console.error('Server error:', err);
process.exit(1);
});
} catch (err) {
console.error(err);
process.exit(1);
}
}
main();
//server.ts
config.ts
import ziti from '@openziti/ziti-sdk-nodejs';
import express, { Express } from 'express';
import type { Server } from 'http';
export interface ZitiConfig {
serviceConfig: {
name: string;
identityFile: string;
};
}
export const initializeZiti = async (config: ZitiConfig) => {
try {
await ziti.init(config.serviceConfig.identityFile);
console.log('Ziti initialized successfully');
return true;
} catch (error) {
console.error('Failed to initialize Ziti:', error);
return false;
}
};
export const getZitiExpressWrapper = (_expressApp: Express, serviceName: string): Server => {
// Create a Ziti-wrapped Express app
const app = ziti.express(express, serviceName);
// Add error handler
app.on('error', (err: Error) => {
console.error('Ziti server error:', err);
});
return app;
};