@wavegrid/canvas 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export { getCanvasHTML } from './ui';
package/index.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getCanvasHTML = void 0;
4
+ var ui_1 = require("./ui");
5
+ Object.defineProperty(exports, "getCanvasHTML", { enumerable: true, get: function () { return ui_1.getCanvasHTML; } });
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@wavegrid/canvas",
3
+ "version": "0.1.1",
4
+ "author": "Dan Lynch <pyramation@gmail.com>",
5
+ "description": "Artist-facing creative canvas for painting with light on the 7×7 grid",
6
+ "main": "index.js",
7
+ "module": "esm/index.js",
8
+ "types": "index.d.ts",
9
+ "homepage": "https://github.com/constructive-io/Illuminate",
10
+ "license": "SEE LICENSE IN LICENSE",
11
+ "publishConfig": {
12
+ "access": "public",
13
+ "directory": "dist"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/constructive-io/Illuminate"
18
+ },
19
+ "bugs": {
20
+ "url": "https://github.com/constructive-io/Illuminate/issues"
21
+ },
22
+ "scripts": {
23
+ "clean": "makage clean",
24
+ "prepack": "npm run build",
25
+ "build": "makage build",
26
+ "build:dev": "makage build --dev",
27
+ "lint": "ESLINT_USE_FLAT_CONFIG=false eslint src --fix",
28
+ "test": "jest --passWithNoTests",
29
+ "test:watch": "jest --watch",
30
+ "dev": "ts-node src/server.ts"
31
+ },
32
+ "keywords": [
33
+ "laser",
34
+ "lighting",
35
+ "canvas",
36
+ "creative",
37
+ "7x7"
38
+ ],
39
+ "dependencies": {
40
+ "ws": "^8.18.0"
41
+ },
42
+ "devDependencies": {
43
+ "@types/ws": "^8.5.13",
44
+ "makage": "^0.3.0"
45
+ },
46
+ "gitHead": "6f651b8d0dfbb6538c667dd3905ce6989ae18e46"
47
+ }
package/server.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import http from 'http';
2
+ declare const server: http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
3
+ export { server };
package/server.js ADDED
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.server = void 0;
7
+ const http_1 = __importDefault(require("http"));
8
+ const ws_1 = require("ws");
9
+ const ui_1 = require("./ui");
10
+ const PORT = parseInt(process.env.PORT || '3001', 10);
11
+ const SIMULATOR_URL = process.env.SIMULATOR_URL || 'ws://localhost:3000';
12
+ const server = http_1.default.createServer((_req, res) => {
13
+ res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
14
+ res.end((0, ui_1.getCanvasHTML)());
15
+ });
16
+ exports.server = server;
17
+ const wss = new ws_1.WebSocketServer({ server });
18
+ let simulatorWs = null;
19
+ let reconnectTimer = null;
20
+ function connectToSimulator() {
21
+ try {
22
+ simulatorWs = new ws_1.WebSocket(SIMULATOR_URL);
23
+ simulatorWs.on('open', () => {
24
+ console.log(' ✦ Connected to simulator');
25
+ });
26
+ simulatorWs.on('message', (data) => {
27
+ // Relay state from simulator to all canvas clients
28
+ const msg = data.toString();
29
+ wss.clients.forEach(client => {
30
+ if (client.readyState === ws_1.WebSocket.OPEN) {
31
+ client.send(msg);
32
+ }
33
+ });
34
+ });
35
+ simulatorWs.on('close', () => {
36
+ console.log(' ✦ Simulator disconnected, reconnecting...');
37
+ scheduleReconnect();
38
+ });
39
+ simulatorWs.on('error', () => {
40
+ scheduleReconnect();
41
+ });
42
+ }
43
+ catch (_e) {
44
+ scheduleReconnect();
45
+ }
46
+ }
47
+ function scheduleReconnect() {
48
+ if (reconnectTimer)
49
+ return;
50
+ reconnectTimer = setTimeout(() => {
51
+ reconnectTimer = null;
52
+ connectToSimulator();
53
+ }, 2000);
54
+ }
55
+ wss.on('connection', (ws) => {
56
+ ws.on('message', (raw) => {
57
+ // Relay commands from canvas clients to simulator
58
+ if (simulatorWs && simulatorWs.readyState === ws_1.WebSocket.OPEN) {
59
+ simulatorWs.send(raw);
60
+ }
61
+ });
62
+ });
63
+ server.listen(PORT, '0.0.0.0', () => {
64
+ console.log('');
65
+ console.log(' ╭──────────────────────────────────────╮');
66
+ console.log(' │ Illuminate · Canvas │');
67
+ console.log(' │ painting the sky with light │');
68
+ console.log(' ╰──────────────────────────────────────╯');
69
+ console.log('');
70
+ console.log(` → http://localhost:${PORT}`);
71
+ console.log(` → Simulator: ${SIMULATOR_URL}`);
72
+ console.log('');
73
+ connectToSimulator();
74
+ });
package/ui.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare function getCanvasHTML(): string;