Add osm-map-nano

This commit is contained in:
Corentin 2025-09-10 00:22:48 +09:00
commit bedef43e13
22 changed files with 739 additions and 0 deletions

78
scripts/build.js Normal file
View file

@ -0,0 +1,78 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { rollup } from 'rollup';
const root_dir = path.join(path.dirname(fileURLToPath(import.meta.url)), '..');
const root_info = JSON.parse(fs.readFileSync(path.join(root_dir, 'package.json')));
async function build(options)
{
let bundle;
let success = true;
try
{
bundle = await rollup(options.input);
await bundle.write(options.output);
}
catch(error)
{
success = false;
console.error(`Error building ${options}`);
console.error(error);
}
if(bundle)
{
await bundle.close();
}
return success;
}
for(const package_path of fs.globSync(root_info.workspaces))
{
const pkg_info = JSON.parse(fs.readFileSync(path.join(root_dir, package_path, 'package.json')));
const build_dir = path.join('build', 'packages', pkg_info.name);
const dist_dir = path.join(build_dir, 'dist');
// Building main package
fs.mkdirSync(dist_dir, {recursive: true});
if(!await build({
input: {
input: path.join(build_dir, 'src', 'index.js')
},
output: {
compact: true,
extend: true,
file: `${dist_dir}/${pkg_info.name}.js`,
name: 'OSM',
format: 'iife'
}}))
process.exit(1);
// Building tests
const html_test_template = fs.readFileSync(path.join(root_dir, 'scripts', 'test_template.html')).toString()
.replaceAll('{{bundle}}', `${pkg_info.name}.js`)
.replaceAll('{{data}}', '/data/shibuya.osm');
for(const test_file of fs.globSync(path.join(build_dir, 'tests', '*.js')))
{
const test_name = path.parse(test_file).name;
if(!await build({
input: {
input: test_file,
external: ['osm-map-nano'],
},
output: {
compact: true,
globals: { 'osm-map-nano': 'OSM' },
extend: true,
file: `${dist_dir}/${test_name}.js`,
name: 'OSM',
format: 'iife'
}}))
process.exit(1);
fs.writeFileSync(`${dist_dir}/${test_name}.html`,
html_test_template.replaceAll('{{test_script}}', `${test_name}.js`).replaceAll('{{test_name}}', test_name));
}
}

66
scripts/server.js Normal file
View file

@ -0,0 +1,66 @@
import fs from 'fs';
import http from 'http';
import path from 'path';
const port = 8080;
const server = http.createServer((request, response) => {
const mimeType = {
'.html': 'text/html',
'.js': 'text/javascript',
'.gz': 'application/javascript',
};
const mimeEncoding = {
'.gz': 'gzip',
};
let pathname = path.join('.', request.url);
if(!fs.existsSync(pathname))
{
// If the file is not found, return 404
response.statusCode = 404;
response.end(`File ${pathname} not found!`);
return;
}
// If is a directory, then look for index.html
if (fs.statSync(pathname).isDirectory()) {
pathname = path.join(pathname, 'index.html');
}
// Read file from file system
fs.readFile(pathname, function(error, data){
if(error)
{
response.statusCode = 500;
response.end(`Error getting the file: ${error}.`);
}
else
{
// Based on the URL path, extract the file extention. e.g. .js, .doc, ...
const extension = path.parse(pathname).ext;
// Set Content-Type
response.setHeader('Content-Type', mimeType[extension] || 'text/plain' );
// If the file is found, set Content-Encoding
if(mimeEncoding[extension])
{
response.setHeader('Content-Encoding', mimeEncoding[extension]);
}
response.end(data);
}
});
});
server.listen(port, (error) => {
if (error)
return console.log(`Server cannot listen port ${port} :`, error);
console.log(`Server is listening on port ${port}`);
});
const exit = () => {
server.close();
console.log('Server stopped');
};
process.on('SIGINT', () => {
process.stdout.write('\r \r');
exit();
});

67
scripts/test.cjs Normal file
View file

@ -0,0 +1,67 @@
const assert = require('node:assert');
const { spawn } = require('node:child_process');
const fs = require('node:fs');
const { Browser, Builder, By } = require('selenium-webdriver');
const firefox = require('selenium-webdriver/firefox');
async function test() {
const generate_env_var = process.env.OSM_MAP_GENERATE_DATA || 'false';
const generete_data = generate_env_var === '1' || generate_env_var.toLowerCase() === 'true';
const server = spawn('node', ['scripts/server.js'], {stdio: [null, null, 'inherit']}); // show stderr (stdout is read)
console.log('Creating selenium driver');
const options = new firefox.Options();
options.addArguments('--headless');
const driver = await new Builder().forBrowser(Browser.FIREFOX).setFirefoxOptions(options).build();
try
{
const max_try = 5;
let read_try = 0;
for(; read_try < max_try; read_try +=1)
{
const server_out = server.stdout.read();
if(server_out != null)
{
assert.equal('Server is listening on port 8080\n', server_out.toString(), 'Server not starting properly');
break;
}
await driver.sleep(50);
}
assert.notEqual(read_try, max_try, 'Timeout waiting for server starting');
const test_name = 'test';
console.log('Fetching test html');
await driver.get('http://localhost:8080/build/packages/osm-map-nano/dist/test.html');
assert.equal(`OSM Map Test : ${test_name}`, await driver.getTitle(), 'Wrong HTML title');
let canvas = await driver.findElement(By.id('canvas-test'));
const image_url_prefix = 'data:image/png;base64,';
const image_data = Buffer.from(
(await driver.executeScript('return arguments[0].toDataURL()', canvas)).substring(image_url_prefix.length), 'base64');
const expected_data = fs.readFileSync('data/test.png');
if(generete_data)
{
if(!expected_data.equals(image_data))
{
console.log(`Generating image for test ${test_name}`);
fs.writeFileSync('data/test.png', image_data);
}
}
else
assert.ok(expected_data.equals(image_data));
}
catch (error)
{
console.error('TEST FAILED');
console.error(error);
}
finally
{
console.log('Exiting selenium driver');
await driver.quit();
server.kill('SIGINT');
}
}
test();

View file

@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>OSM Map Test : {{test_name}}</title>
<style>
body { background: #333; color: #ddd}
</style>
<script src="{{bundle}}"></script>
</head>
<body>
<canvas id="canvas-test"></canvas>
<script src="{{test_script}}"></script>
<script>
fetch('{{data}}')
.then(response => response.text())
.then(data => { test(document.getElementById('canvas-test'), data); } );
</script>
</body>
</html>

12
scripts/tmpfs_dirs.sh Executable file
View file

@ -0,0 +1,12 @@
#! /bin/bash
CWD=$(cd -P . && pwd)
readarray -t TMPFS_DIRS < <(df -lt tmpfs --output=target | tail -n +2)
if [[ " ${TMPFS_DIRS[*]} " =~ " ${CWD}/build " ]]; then
echo "build alread a tmpfs"
else
echo "Mounting build as tmpfs"
sudo mount -m -t tmpfs tmpfs build
fi