happy path working with stub companion app
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
#!/usr/bin/env node
|
||||
// Serves companion/dist/ over HTTPS using the mkcert-issued test.locqr.dev
|
||||
// cert (dev.md — same domain as the test website, different port, so the
|
||||
// companion stub gets a secure context for getUserMedia()). Requires
|
||||
// `npm run build` in companion/ first — this serves the built output, not
|
||||
// the TypeScript source, mirroring how the extension is loaded from
|
||||
// extension/dist/.
|
||||
//
|
||||
// Plain Node https + fs — same shape as serve-website.js.
|
||||
|
||||
import { createServer } from 'node:https';
|
||||
import { readFile, stat } from 'node:fs/promises';
|
||||
import { extname, join, normalize } from 'node:path';
|
||||
import { dirname } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const ROOT = dirname(dirname(fileURLToPath(import.meta.url)));
|
||||
const COMPANION_DIST_DIR = join(ROOT, 'companion', 'dist');
|
||||
const PORT = 5174;
|
||||
|
||||
const MIME = {
|
||||
'.html': 'text/html; charset=utf-8',
|
||||
'.js': 'text/javascript; charset=utf-8',
|
||||
'.css': 'text/css; charset=utf-8',
|
||||
'.json': 'application/json',
|
||||
};
|
||||
|
||||
async function resolveFile(urlPath) {
|
||||
const safePath = normalize(urlPath === '/' ? '/index.html' : urlPath).replace(/^(\.\.[/\\])+/, '');
|
||||
const filePath = join(COMPANION_DIST_DIR, safePath);
|
||||
if (!filePath.startsWith(COMPANION_DIST_DIR)) return null; // path traversal guard
|
||||
|
||||
try {
|
||||
const stats = await stat(filePath);
|
||||
return stats.isFile() ? filePath : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const options = {
|
||||
cert: await readFile(join(ROOT, 'dev', 'certs', 'test.locqr.dev.pem')),
|
||||
key: await readFile(join(ROOT, 'dev', 'certs', 'test.locqr.dev-key.pem')),
|
||||
};
|
||||
|
||||
createServer(options, async (req, res) => {
|
||||
const url = new URL(req.url ?? '/', 'https://test.locqr.dev');
|
||||
const filePath = await resolveFile(url.pathname);
|
||||
|
||||
if (!filePath) {
|
||||
res.writeHead(404).end('Not found — did you run `npm run build` in companion/?');
|
||||
return;
|
||||
}
|
||||
|
||||
const body = await readFile(filePath);
|
||||
// No caching, ever — this is a dev stub rebuilt on every source edit,
|
||||
// and mobile browsers in particular are prone to heuristically caching
|
||||
// a same-URL response across page reloads when the server sends no
|
||||
// explicit Cache-Control at all, silently serving a stale bundle.
|
||||
res.writeHead(200, { 'Content-Type': MIME[extname(filePath)] ?? 'application/octet-stream', 'Cache-Control': 'no-store' });
|
||||
res.end(body);
|
||||
}).listen(PORT, () => {
|
||||
console.log(`Serving companion/dist/ -> https://test.locqr.dev:${PORT}`);
|
||||
});
|
||||
@@ -0,0 +1,55 @@
|
||||
#!/usr/bin/env bash
|
||||
# Makes test.locqr.dev and api.locqr.dev resolvable from a phone on the same
|
||||
# LAN, for testing the companion stub (companion/, dev.md) against a real
|
||||
# camera. Not needed for desktop-only development — that uses /etc/hosts
|
||||
# (dev.md), which a phone can't be pointed at as easily.
|
||||
#
|
||||
# Runs dnsmasq in the foreground, bound only to the Wi-Fi interface (not a
|
||||
# systemd service, doesn't touch /etc/resolv.conf or /etc/dnsmasq.conf):
|
||||
# resolves the two dev hostnames to this machine's own LAN IP and forwards
|
||||
# everything else to a real upstream resolver. Ctrl+C stops it; nothing
|
||||
# persists after that.
|
||||
#
|
||||
# Requires sudo (binding port 53). Usage: sudo scripts/serve-lan-dns.sh [interface]
|
||||
# If no interface is given, the script guesses the one carrying a private
|
||||
# (RFC 1918) IPv4 address — print `ip -4 addr show` yourself if it guesses
|
||||
# wrong on a machine with multiple such interfaces.
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
if [[ $EUID -ne 0 ]]; then
|
||||
echo "Needs root (binds port 53). Re-run as: sudo $0 $*" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
IFACE="${1:-}"
|
||||
if [[ -z "$IFACE" ]]; then
|
||||
IFACE=$(ip -4 -o addr show scope global | awk '$4 ~ /^(10\.|172\.(1[6-9]|2[0-9]|3[01])\.|192\.168\.)/ { print $2; exit }')
|
||||
fi
|
||||
if [[ -z "$IFACE" ]]; then
|
||||
echo "Could not auto-detect a LAN interface. Pass one explicitly: $0 <interface>" >&2
|
||||
echo "(check \`ip -4 addr show\` for the one with your 192.168.x.x / 10.x.x.x address)" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
LAN_IP=$(ip -4 -o addr show dev "$IFACE" | awk '{ print $4 }' | cut -d/ -f1)
|
||||
if [[ -z "$LAN_IP" ]]; then
|
||||
echo "Interface '$IFACE' has no IPv4 address." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Resolving test.locqr.dev and api.locqr.dev -> $LAN_IP on interface $IFACE"
|
||||
echo "Point your phone's Wi-Fi DNS server at $LAN_IP, then browse to https://test.locqr.dev:5174"
|
||||
echo "(Ctrl+C to stop)"
|
||||
echo
|
||||
|
||||
exec dnsmasq \
|
||||
--no-daemon \
|
||||
--no-resolv \
|
||||
--no-hosts \
|
||||
--server=1.1.1.1 \
|
||||
--server=8.8.8.8 \
|
||||
--interface="$IFACE" \
|
||||
--bind-interfaces \
|
||||
--address="/test.locqr.dev/$LAN_IP" \
|
||||
--address="/api.locqr.dev/$LAN_IP"
|
||||
Reference in New Issue
Block a user