-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/cacheflowe/haxademic.js
- Loading branch information
Showing
19 changed files
with
19,877 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
////////////////////////////////////// | ||
// Basic node api server | ||
// - From: https://www.digitalocean.com/community/tutorials/how-to-create-a-web-server-in-node-js-with-the-http-module | ||
// Use nodemon to automatically restart the server when changes are made | ||
// Usage: `nodemon node src/nodejs/backend.js` | ||
////////////////////////////////////// | ||
|
||
import http from "http"; | ||
import { promises as fs } from "fs"; | ||
import { fileURLToPath } from "url"; | ||
import { dirname } from "path"; | ||
|
||
const host = "localhost"; | ||
const port = 8000; | ||
|
||
// handle incoming requests ------------------- | ||
|
||
const requestListener = function (req, res) { | ||
// Set CORS headers | ||
res.setHeader("Access-Control-Allow-Origin", "*"); | ||
res.setHeader( | ||
"Access-Control-Allow-Headers", | ||
"X-Requested-With,content-type" | ||
); | ||
|
||
// grab path and parse path components | ||
const { url } = req; | ||
const path = url.split("/").filter((p) => p && p.length > 0); | ||
console.log(path); | ||
// handle different paths | ||
if (url === "/") { | ||
handleHomeRoute(res); | ||
} else if (path[0] == "html") { | ||
handleHtml(res); | ||
} else if (path[0] == "current") { | ||
handleCurrentMessage(res); | ||
} else { | ||
handleNotFoundRoute(res); | ||
} | ||
}; | ||
|
||
const handleHomeRoute = (res) => { | ||
res.setHeader("Content-Type", "text/html"); | ||
res.writeHead(200); | ||
res.end("Hello! This is the home route."); | ||
}; | ||
|
||
const handleHtml = (res) => { | ||
res.setHeader("Content-Type", "text/html"); | ||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = dirname(__filename); | ||
fs.readFile(__dirname + "/index.html").then((contents) => { | ||
res.setHeader("Content-Type", "text/html"); | ||
res.writeHead(200); | ||
res.end(contents); | ||
}); | ||
}; | ||
|
||
const handleCurrentMessage = async (res) => { | ||
try { | ||
const response = await fetch("https://some.api/endpoint", { | ||
body: JSON.stringify({ test: "data" }), | ||
headers: { "Content-Type": "application/json" }, | ||
method: "POST", | ||
}); | ||
if (!response.ok) { | ||
res.writeHead(500); | ||
res.end({ error: error.message }); | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
|
||
const data = await response.text(); | ||
res.setHeader("Content-Type", "application/json"); | ||
res.writeHead(200); | ||
res.end(data); | ||
} catch (error) { | ||
console.error("Fetch failed:", error); | ||
res.writeHead(500); | ||
res.end("Error fetching data from fake API"); | ||
} | ||
}; | ||
|
||
const handleNotFoundRoute = (res) => { | ||
res.setHeader("Content-Type", "text/html"); | ||
res.writeHead(404); | ||
res.end("404 Not Found"); | ||
}; | ||
|
||
// start server -------------------------------- | ||
|
||
const server = http.createServer(requestListener); | ||
server.listen(port, host, () => { | ||
console.log(`Server is on http://${host}:${port}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { exec, spawn } from "node:child_process"; | ||
|
||
exec("cmd.exe /c dir", (err, stdout, stderr) => { | ||
if (err) { | ||
console.error(err); | ||
return; | ||
} | ||
console.log(stdout); | ||
}); | ||
|
||
exec("kill-java.cmd", (err, stdout, stderr) => { | ||
if (err) { | ||
console.error(err); | ||
return; | ||
} | ||
console.log(stdout); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { activeWindow, openWindows } from "get-windows"; | ||
|
||
console.log(await activeWindow({})); | ||
console.log(await openWindows({})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import AppStoreDistributed from "../app-store-distributed.js"; | ||
|
||
// self-registering child components | ||
import "./websocket-indicator.js"; | ||
import "./app-store-debug.js"; | ||
|
||
class AppStateDistributed extends HTMLElement { | ||
connectedCallback() { | ||
this.shadow = this.attachShadow({ mode: "open" }); | ||
this.initSharedState(); | ||
this.addChildren(); | ||
// if (this.isDebug()) this.storeDebug.show(); | ||
} | ||
|
||
initSharedState() { | ||
// connect to websocket server | ||
this.webSocketHost = "ws://" + document.location.hostname + ":3001/ws"; | ||
this.appStore = new AppStoreDistributed(this.webSocketHost); | ||
|
||
// listen for data/events | ||
_store.addListener(this); | ||
_store.addListener(this, "AppStoreDistributed_CONNECTED"); // emitted by AppStoreDistributed when connected | ||
} | ||
|
||
isDebug() { | ||
return this.hasAttribute("debug"); | ||
} | ||
|
||
addChildren() { | ||
this.shadow.innerHTML = this.isDebug() | ||
? /*html*/ ` | ||
<websocket-indicator></websocket-indicator> | ||
<app-store-debug></app-store-debug> | ||
` | ||
: /*html*/ ` | ||
<app-store-debug></app-store-debug> | ||
`; | ||
this.storeDebug = this.shadow.querySelector("app-store-debug"); | ||
} | ||
|
||
// AppStore listeners | ||
|
||
AppStoreDistributed_CONNECTED(val) { | ||
_store.set("CLIENT_CONNECTED", true, true); // let desktop app know that we're here | ||
} | ||
|
||
storeUpdated(key, val) {} | ||
|
||
static register() { | ||
customElements.define("app-state-distributed", AppStateDistributed); | ||
} | ||
} | ||
|
||
AppStateDistributed.register(); | ||
|
||
export default AppStateDistributed; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import MobileUtil from "../mobile-util.js"; | ||
import AppStoreElement from "./app-store-element.js"; | ||
|
||
class AppStoreButton extends AppStoreElement { | ||
storeUpdated(key, value) { | ||
if (key != this.storeKey) return; | ||
// disable button if clicked | ||
if (value == this.storeValue) this.button.setAttribute("disabled", true); | ||
else this.button.removeAttribute("disabled"); | ||
} | ||
|
||
clickEvent() { | ||
return MobileUtil.isMobileBrowser() ? "touchstart" : "click"; | ||
} | ||
|
||
initStoreListener() { | ||
this.button = this.el.querySelector("button"); | ||
this.button.addEventListener(this.clickEvent(), (e) => { | ||
if (!this.storeKey) return; | ||
// if the storeValue is "true" or "false", send the boolean instead of the string | ||
const value = | ||
this.storeValue == "true" | ||
? true | ||
: this.storeValue == "false" | ||
? false | ||
: this.storeValue; | ||
_store.set(this.storeKey, value, true); | ||
}); | ||
|
||
super.initStoreListener(); | ||
} | ||
|
||
css() { | ||
return /*css*/ ` | ||
`; | ||
} | ||
|
||
html() { | ||
return /*html*/ ` | ||
<button> | ||
${this.initialHTML} | ||
</button> | ||
`; | ||
} | ||
|
||
static register() { | ||
customElements.define("app-store-button", AppStoreButton); | ||
} | ||
} | ||
|
||
AppStoreButton.register(); | ||
|
||
export default AppStoreButton; |
Oops, something went wrong.