Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : KA Maps Engine8 * File : sdk/web/tests/e2e/server.mjs9 * Module : sdk10 * Repository : https://git.spboucher.ai/ka-maps-engine11 * Created : 2026-12-0112 * Last Modified : 2026-12-0113 *14 * Author : Simon-Pierre Boucher15 * Position : Professor of Finance16 * Institution : Université du Québec en Outaouais (UQO)17 * Organization : SP Boucher18 * Website : https://spboucher.ai19 * Git : https://git.spboucher.ai20 *21 * Preferred Contact: support@spboucher.ai22 * Development : dev@spboucher.ai23 * Support : support@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Serveur statique des essais de bout en bout : sert la démonstration construite (demo/dist) sous43// la CSP stricte que les sites appliqueront, sur trois ports :44//45// 5311 page, actifs du moteur de même origine — CSP sans `blob:` ni 'unsafe-inline'46// 5312 « CDN » : seulement /ka-maps/<version>/…, avec CORS (tient le rôle de maps.spboucher.ai/sdk/)47// 5313 page dont les actifs viennent du port 5312 — CSP avec `worker-src blob:` (inévitable dans ce cas)48import { createServer } from 'node:http';49import { createReadStream, existsSync, statSync } from 'node:fs';50import { extname, join, normalize } from 'node:path';51import { fileURLToPath } from 'node:url';5253const ROOT = fileURLToPath(new URL('../../demo/dist/', import.meta.url));54const HOST = '127.0.0.1';55export const PORTS = { page: 5311, cdn: 5312, cdnPage: 5313 };56const SERVICES = 'https://tiles.spboucher.ai https://terrain.spboucher.ai https://geo.spboucher.ai https://maps.spboucher.ai';5758export const CSP_SAME_ORIGIN = [59 "default-src 'none'",60 "script-src 'self' 'wasm-unsafe-eval'",61 "worker-src 'self'",62 "style-src 'self'",63 "img-src 'self' data:",64 `connect-src 'self' ${SERVICES}`,65 "base-uri 'none'",66 "form-action 'none'",67 "frame-ancestors 'none'",68].join('; ');6970export const CSP_CDN = [71 "default-src 'none'",72 `script-src 'self' 'wasm-unsafe-eval' http://${HOST}:${PORTS.cdn} https://maps.spboucher.ai`,73 "worker-src 'self' blob:",74 "style-src 'self'",75 "img-src 'self' data:",76 `connect-src 'self' http://${HOST}:${PORTS.cdn} ${SERVICES}`,77 "base-uri 'none'",78 "form-action 'none'",79 "frame-ancestors 'none'",80].join('; ');8182const TYPES = {83 '.html': 'text/html; charset=utf-8',84 '.js': 'text/javascript; charset=utf-8',85 '.css': 'text/css; charset=utf-8',86 '.wasm': 'application/wasm',87 '.json': 'application/json',88 '.png': 'image/png',89 '.svg': 'image/svg+xml',90};9192function serve(kind) {93 return (req, res) => {94 const url = new URL(req.url ?? '/', `http://${HOST}`);95 let path = decodeURIComponent(url.pathname);96 if (path.endsWith('/')) path += 'index.html';97 const file = normalize(join(ROOT, path));98 if (!file.startsWith(ROOT) || !existsSync(file) || !statSync(file).isFile()) {99 res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });100 res.end('introuvable');101 return;102 }103 if (kind === 'cdn' && !path.startsWith('/ka-maps/')) {104 res.writeHead(403, { 'Content-Type': 'text/plain; charset=utf-8' });105 res.end('actifs seulement');106 return;107 }108 const headers = {109 'Content-Type': TYPES[extname(file)] ?? 'application/octet-stream',110 'Content-Length': statSync(file).size,111 'X-Content-Type-Options': 'nosniff',112 'Cache-Control': path.startsWith('/ka-maps/') ? 'public, max-age=31536000, immutable' : 'no-cache',113 };114 if (kind === 'cdn') {115 headers['Access-Control-Allow-Origin'] = '*';116 headers['Cross-Origin-Resource-Policy'] = 'cross-origin';117 } else if (extname(file) === '.html') {118 headers['Content-Security-Policy'] = kind === 'cdnPage' ? CSP_CDN : CSP_SAME_ORIGIN;119 }120 res.writeHead(200, headers);121 createReadStream(file).pipe(res);122 };123}124125if (process.argv[1] === fileURLToPath(import.meta.url)) {126 if (!existsSync(join(ROOT, 'index.html'))) {127 console.error('demo/dist absent : lancer `npm run demo:build`');128 process.exit(1);129 }130 for (const [kind, port] of Object.entries(PORTS)) createServer(serve(kind)).listen(port, HOST);131 console.log(`essais : http://${HOST}:${PORTS.page}/ (CSP stricte), CDN ${PORTS.cdn}, page CDN ${PORTS.cdnPage}`);132}133