Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : KA Maps Engine8 * File : sdk/web/src/next.ts9 * 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// @ka/maps/next — aide Next.js (16, app router ; fonctionne aussi avec les versions 13 à 15).43//44// // next.config.ts45// import { withKaMaps } from '@ka/maps/next';46// export default withKaMaps({ /* votre configuration */ });47//48// Effets : copie les actifs du moteur dans `public/ka-maps/<version>/` au chargement de la49// configuration (dev et build), et ajoute les en-têtes `Content-Type: application/wasm` +50// cache immuable sur ce dossier. Rien n'est ajouté à la configuration de l'empaqueteur51// (Turbopack ou webpack) : le SDK charge ses actifs par adresse, pas par import.5253import { join } from 'node:path';54import { copyAssets, defaultPublicPath, VERSION } from './assets-node.js';5556export interface KaMapsNextOptions {57 /** Dossier public du site. Défaut : `<cwd>/public`. */58 publicDir?: string;59 /** Faux : ne rien copier (actifs servis par maps.spboucher.ai ou copiés par un autre moyen). */60 copy?: boolean;61}6263type Header = { key: string; value: string };64type HeaderRule = { source: string; headers: Header[] };65// Type générique : une configuration typée `NextConfig` passe telle quelle et ressort avec son type.66type WithHeaders = { headers?: () => Promise<HeaderRule[]> | HeaderRule[] };6768/** Copie les actifs dans `public/ka-maps/<version>/`. Utilisable seul dans un script `prebuild`. */69export function copyKaMapsAssets(publicDir: string = join(process.cwd(), 'public')): { dir: string; written: string[] } {70 return copyAssets(publicDir);71}7273export function withKaMaps<T extends object>(config: T = {} as T, options: KaMapsNextOptions = {}): T {74 if (options.copy !== false) copyKaMapsAssets(options.publicDir);75 const userHeaders = (config as WithHeaders).headers;76 return {77 ...config,78 async headers() {79 const base = userHeaders ? await userHeaders() : [];80 return [81 ...base,82 {83 source: `/ka-maps/${VERSION}/:file*`,84 headers: [{ key: 'Cache-Control', value: 'public, max-age=31536000, immutable' }],85 },86 {87 source: `/ka-maps/${VERSION}/km_wasm_bg.wasm`,88 headers: [{ key: 'Content-Type', value: 'application/wasm' }],89 },90 ];91 },92 } as T;93}9495/**96 * Directives CSP nécessaires au SDK, à fusionner avec celles du site.97 * `cdn: true` si les actifs viennent de https://maps.spboucher.ai (worker amorcé par `blob:`).98 */99export function kaMapsCsp(options: { cdn?: boolean; endpoints?: string[] } = {}): Record<string, string[]> {100 const endpoints = options.endpoints ?? ['https://tiles.spboucher.ai', 'https://terrain.spboucher.ai', 'https://geo.spboucher.ai', 'https://maps.spboucher.ai'];101 return {102 'script-src': options.cdn ? ["'wasm-unsafe-eval'", 'https://maps.spboucher.ai'] : ["'wasm-unsafe-eval'"],103 'worker-src': options.cdn ? ["'self'", 'blob:'] : ["'self'"],104 'connect-src': endpoints,105 };106}107108export { defaultPublicPath, VERSION };109