import { createServer } from 'node:http';
import { pathToFileURL } from 'node:url';
import { parseAPI } from '@parseapi/sdk';
export async function searchIndustries(query, parse, signal) {
query = typeof query === 'string' ? query.trim() : '';
if (query.length < 2 || query.length > 100) throw new Error('invalid_query');
// example:request
const industries = await parse.industry.search(query, { limit: 6, signal });
// /example:request
if (!industries || industries.country !== 'US' || industries.year !== 2022 ||
!Array.isArray(industries.results) || industries.results.length > 6 ||
industries.results.some(item => !item || typeof item.naics !== 'string' ||
!/^(?:[0-9]{2,6}|31-33|44-45|48-49)$/.test(item.naics) ||
typeof item.name !== 'string' || !item.name || item.name.length > 500 ||
!Number.isInteger(item.level) || item.level < 2 || item.level > 6)) throw new Error('unexpected_response');
return { country: industries.country, year: industries.year, results: industries.results.map(item => ({ naics: item.naics, name: item.name, level: item.level })) };
}
export const PAGE = `
Choose your business industryWhat does your business do?
Find and choose the US NAICS 2022 classification that fits your main business activity.
Search to see matching industry titles.
Choose the most specific title that fits. A classification is a business choice, not a company lookup or a legal determination.
`;
export function createAppServer(parse) {
return createServer(async (request, response) => {
response.setHeader('Cache-Control', 'no-store');
if (request.method !== 'GET') { response.writeHead(405, { Allow: 'GET' }); response.end(); return; }
const url = new URL(request.url, 'http://127.0.0.1');
if (url.pathname === '/') { response.setHeader('Content-Type', 'text/html; charset=utf-8'); response.end(PAGE); return; }
if (url.pathname !== '/industries') { response.writeHead(404); response.end(); return; }
response.setHeader('Content-Type', 'application/json');
const query = url.searchParams.get('q')?.trim() || '';
if (query.length < 2 || query.length > 100) { response.writeHead(400); response.end(JSON.stringify({ error: 'Enter 2 to 100 characters.' })); return; }
const controller = new AbortController();
response.once('close', () => { if (!response.writableEnded) controller.abort(); });
try { response.end(JSON.stringify(await searchIndustries(query, parse, controller.signal))); }
catch { if (!response.destroyed) { response.writeHead(502); response.end(JSON.stringify({ error: 'The industry lookup could not complete.' })); } }
});
}
function main() {
const port = Number(process.env.PORT || 8000);
if (!Number.isInteger(port) || port < 1 || port > 65535) throw new Error('PORT must be between 1 and 65535.');
const key = process.env.PARSEAPI_KEY;
if (!key) throw new Error('Set PARSEAPI_KEY to your secret server key.');
const parse = parseAPI(key, { timeoutMs: 8000, retries: 0 });
createAppServer(parse).listen(port, '127.0.0.1', () => console.log('Open http://127.0.0.1:' + port));
}
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
try { main(); } catch (error) { console.error(error.message); process.exitCode = 1; }
}