Skip to content

Commit

Permalink
browser compatibility - udp
Browse files Browse the repository at this point in the history
  • Loading branch information
foomoon committed Dec 17, 2023
1 parent 91b0b3b commit f7b3b0f
Show file tree
Hide file tree
Showing 18 changed files with 185 additions and 226 deletions.
60 changes: 20 additions & 40 deletions dist/lib/light.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
import { generateRandomHex } from "./utils.js";
import axios from "axios";
import delay from "delay";
import * as udp from "node:dgram";
// dynamically import udp for compatibility with browser
// import * as udp from "node:dgram";
import { Led } from "./led.js";
import { Frame } from "./frame.js";
import { Movie } from "./movie.js";
// let generateRandomHex: (bytes: any) => any;
// if (typeof window === "undefined") {
// // Node.js environment
// generateRandomHex = async (bytes: any) => {
// const cryptoModule = await import("node:crypto");
// const randomBytes = cryptoModule.randomBytes;
// return randomBytes(bytes).toString("hex");
// };
// } else if (window.crypto && window.crypto.getRandomValues) {
// // Modern browser with window.crypto support
// generateRandomHex = async (bytes: any) => {
// const randomBytes = new Uint8Array(bytes);
// window.crypto.getRandomValues(randomBytes);
// const hexArray = Array.from(randomBytes, (byte) =>
// byte.toString(16).padStart(2, "0")
// );
// return hexArray.join("");
// };
// } else {
// // Fallback for older browsers
// generateRandomHex = (bytes: any) => {
// const randomBytes = new Array(bytes);
// for (let i = 0; i < bytes; i++) {
// randomBytes[i] = Math.floor(Math.random() * 256);
// }
// const hexArray = randomBytes.map((byte) =>
// byte.toString(16).padStart(2, "0")
// );
// return hexArray.join("");
// };
// }
import { deviceMode, applicationResponseCode, } from "./interfaces.js";
// create error
let errNoToken = Error("No valid token");
Expand All @@ -62,20 +32,30 @@ export class Light {
*/
constructor(ipaddr, timeout = 20000) {
this.ipaddr = ipaddr;
// this.challenge = randomBytes(256).toString("hex");
// this.challenge = generateRandomHex(256);
this.challenge = ""; // default value, will be set in login()
this.net = axios.create({
baseURL: `http://${this.ipaddr}/xled/v1/`,
timeout: timeout,
});
this.activeLoginCall = false;
if (typeof window === "undefined") {
this.udpClient = udp.createSocket("udp4");
}
else {
this.udpClient = null;
}
// dynamically import udp asynchroniously with IIFE
(() => __awaiter(this, void 0, void 0, function* () {
let udp;
if (typeof window === "undefined") {
// Handle the case for Node.js environments
try {
udp = yield import("node:dgram");
this.udpClient = udp.createSocket("udp4");
}
catch (error) {
throw new Error("Failed to import node:dgram: " + error.message);
}
}
else {
// Handle the case for non-Node.js environments
this.udpClient = null;
}
}))();
}
autoEndLoginCall() {
return __awaiter(this, void 0, void 0, function* () {
Expand Down
16 changes: 8 additions & 8 deletions docs/classes/AuthenticationToken.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ <h4>Hierarchy</h4>
<ul class="tsd-hierarchy">
<li><span class="target">AuthenticationToken</span></li></ul></section><aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/foomoon/xled-js/blob/25fabf6/src/lib/light.ts#L703">lib/light.ts:703</a></li></ul></aside>
<li>Defined in <a href="https://github.com/foomoon/xled-js/blob/91b0b3b/src/lib/light.ts#L682">lib/light.ts:682</a></li></ul></aside>
<section class="tsd-panel-group tsd-index-group">
<section class="tsd-panel tsd-index-panel">
<details class="tsd-index-content tsd-index-accordion" open><summary class="tsd-accordion-summary tsd-index-summary">
Expand Down Expand Up @@ -63,24 +63,24 @@ <h5><span class="tsd-kind-parameter">res</span>: <span class="tsd-signature-type
</div></li></ul></div>
<h4 class="tsd-returns-title">Returns <a href="AuthenticationToken.html" class="tsd-signature-type tsd-kind-class">AuthenticationToken</a></h4><aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/foomoon/xled-js/blob/25fabf6/src/lib/light.ts#L714">lib/light.ts:714</a></li></ul></aside></li></ul></section></section>
<li>Defined in <a href="https://github.com/foomoon/xled-js/blob/91b0b3b/src/lib/light.ts#L693">lib/light.ts:693</a></li></ul></aside></li></ul></section></section>
<section class="tsd-panel-group tsd-member-group">
<h2>Properties</h2>
<section class="tsd-panel tsd-member"><a id="challengeResponse" class="tsd-anchor"></a>
<h3 class="tsd-anchor-link"><span>challenge<wbr/>Response</span><a href="#challengeResponse" aria-label="Permalink" class="tsd-anchor-icon"><svg class="icon icon-tabler icon-tabler-link" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><use href="#icon-anchor-a"></use><use href="#icon-anchor-b"></use><use href="#icon-anchor-c"></use></svg></a></h3>
<div class="tsd-signature"><span class="tsd-kind-property">challenge<wbr/>Response</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">string</span></div><aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/foomoon/xled-js/blob/25fabf6/src/lib/light.ts#L706">lib/light.ts:706</a></li></ul></aside></section>
<li>Defined in <a href="https://github.com/foomoon/xled-js/blob/91b0b3b/src/lib/light.ts#L685">lib/light.ts:685</a></li></ul></aside></section>
<section class="tsd-panel tsd-member"><a id="expiry" class="tsd-anchor"></a>
<h3 class="tsd-anchor-link"><span>expiry</span><a href="#expiry" aria-label="Permalink" class="tsd-anchor-icon"><svg class="icon icon-tabler icon-tabler-link" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><use href="#icon-anchor-a"></use><use href="#icon-anchor-b"></use><use href="#icon-anchor-c"></use></svg></a></h3>
<div class="tsd-signature"><span class="tsd-kind-property">expiry</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type ">Date</span></div><aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/foomoon/xled-js/blob/25fabf6/src/lib/light.ts#L705">lib/light.ts:705</a></li></ul></aside></section>
<li>Defined in <a href="https://github.com/foomoon/xled-js/blob/91b0b3b/src/lib/light.ts#L684">lib/light.ts:684</a></li></ul></aside></section>
<section class="tsd-panel tsd-member"><a id="token" class="tsd-anchor"></a>
<h3 class="tsd-anchor-link"><span>token</span><a href="#token" aria-label="Permalink" class="tsd-anchor-icon"><svg class="icon icon-tabler icon-tabler-link" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><use href="#icon-anchor-a"></use><use href="#icon-anchor-b"></use><use href="#icon-anchor-c"></use></svg></a></h3>
<div class="tsd-signature"><span class="tsd-kind-property">token</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">string</span></div><aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/foomoon/xled-js/blob/25fabf6/src/lib/light.ts#L704">lib/light.ts:704</a></li></ul></aside></section></section>
<li>Defined in <a href="https://github.com/foomoon/xled-js/blob/91b0b3b/src/lib/light.ts#L683">lib/light.ts:683</a></li></ul></aside></section></section>
<section class="tsd-panel-group tsd-member-group">
<h2>Methods</h2>
<section class="tsd-panel tsd-member"><a id="getChallengeResponse" class="tsd-anchor"></a>
Expand All @@ -92,7 +92,7 @@ <h3 class="tsd-anchor-link"><span>get<wbr/>Challenge<wbr/>Response</span><a href
<h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">string</span></h4><p>Challenge response generated by the XLED instance</p>
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/foomoon/xled-js/blob/25fabf6/src/lib/light.ts#L741">lib/light.ts:741</a></li></ul></aside></li></ul></section>
<li>Defined in <a href="https://github.com/foomoon/xled-js/blob/91b0b3b/src/lib/light.ts#L720">lib/light.ts:720</a></li></ul></aside></li></ul></section>
<section class="tsd-panel tsd-member"><a id="getToken" class="tsd-anchor"></a>
<h3 class="tsd-anchor-link"><span>get<wbr/>Token</span><a href="#getToken" aria-label="Permalink" class="tsd-anchor-icon"><svg class="icon icon-tabler icon-tabler-link" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><use href="#icon-anchor-a"></use><use href="#icon-anchor-b"></use><use href="#icon-anchor-c"></use></svg></a></h3>
<ul class="tsd-signatures">
Expand All @@ -102,7 +102,7 @@ <h3 class="tsd-anchor-link"><span>get<wbr/>Token</span><a href="#getToken" aria-
<h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">string</span></h4><p>Token as string</p>
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/foomoon/xled-js/blob/25fabf6/src/lib/light.ts#L725">lib/light.ts:725</a></li></ul></aside></li></ul></section>
<li>Defined in <a href="https://github.com/foomoon/xled-js/blob/91b0b3b/src/lib/light.ts#L704">lib/light.ts:704</a></li></ul></aside></li></ul></section>
<section class="tsd-panel tsd-member"><a id="getTokenDecoded" class="tsd-anchor"></a>
<h3 class="tsd-anchor-link"><span>get<wbr/>Token<wbr/>Decoded</span><a href="#getTokenDecoded" aria-label="Permalink" class="tsd-anchor-icon"><svg class="icon icon-tabler icon-tabler-link" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><use href="#icon-anchor-a"></use><use href="#icon-anchor-b"></use><use href="#icon-anchor-c"></use></svg></a></h3>
<ul class="tsd-signatures">
Expand All @@ -112,7 +112,7 @@ <h3 class="tsd-anchor-link"><span>get<wbr/>Token<wbr/>Decoded</span><a href="#ge
<h4 class="tsd-returns-title">Returns <span class="tsd-signature-type ">Buffer</span></h4><p>Token as buffer, for UDP use</p>
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/foomoon/xled-js/blob/25fabf6/src/lib/light.ts#L733">lib/light.ts:733</a></li></ul></aside></li></ul></section></section></div>
<li>Defined in <a href="https://github.com/foomoon/xled-js/blob/91b0b3b/src/lib/light.ts#L712">lib/light.ts:712</a></li></ul></aside></li></ul></section></section></div>
<div class="col-sidebar">
<div class="page-menu">
<div class="tsd-navigation settings">
Expand Down
10 changes: 5 additions & 5 deletions docs/classes/Frame.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ <h4>Hierarchy</h4>
<ul class="tsd-hierarchy">
<li><a href="OneColorFrame.html" class="tsd-signature-type tsd-kind-class">OneColorFrame</a></li></ul></li></ul></section><aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/foomoon/xled-js/blob/25fabf6/src/lib/frame.ts#L10">lib/frame.ts:10</a></li></ul></aside>
<li>Defined in <a href="https://github.com/foomoon/xled-js/blob/91b0b3b/src/lib/frame.ts#L10">lib/frame.ts:10</a></li></ul></aside>
<section class="tsd-panel-group tsd-index-group">
<section class="tsd-panel tsd-index-panel">
<details class="tsd-index-content tsd-index-accordion" open><summary class="tsd-accordion-summary tsd-index-summary">
Expand Down Expand Up @@ -63,14 +63,14 @@ <h5><span class="tsd-kind-parameter">leds</span>: <a href="Led.html" class="tsd-
</div></li></ul></div>
<h4 class="tsd-returns-title">Returns <a href="Frame.html" class="tsd-signature-type tsd-kind-class">Frame</a></h4><aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/foomoon/xled-js/blob/25fabf6/src/lib/frame.ts#L19">lib/frame.ts:19</a></li></ul></aside></li></ul></section></section>
<li>Defined in <a href="https://github.com/foomoon/xled-js/blob/91b0b3b/src/lib/frame.ts#L19">lib/frame.ts:19</a></li></ul></aside></li></ul></section></section>
<section class="tsd-panel-group tsd-member-group">
<h2>Properties</h2>
<section class="tsd-panel tsd-member"><a id="leds" class="tsd-anchor"></a>
<h3 class="tsd-anchor-link"><span>leds</span><a href="#leds" aria-label="Permalink" class="tsd-anchor-icon"><svg class="icon icon-tabler icon-tabler-link" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><use href="#icon-anchor-a"></use><use href="#icon-anchor-b"></use><use href="#icon-anchor-c"></use></svg></a></h3>
<div class="tsd-signature"><span class="tsd-kind-property">leds</span><span class="tsd-signature-symbol">:</span> <a href="Led.html" class="tsd-signature-type tsd-kind-class">Led</a><span class="tsd-signature-symbol">[]</span></div><aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/foomoon/xled-js/blob/25fabf6/src/lib/frame.ts#L11">lib/frame.ts:11</a></li></ul></aside></section></section>
<li>Defined in <a href="https://github.com/foomoon/xled-js/blob/91b0b3b/src/lib/frame.ts#L11">lib/frame.ts:11</a></li></ul></aside></section></section>
<section class="tsd-panel-group tsd-member-group">
<h2>Methods</h2>
<section class="tsd-panel tsd-member"><a id="getNLeds" class="tsd-anchor"></a>
Expand All @@ -82,7 +82,7 @@ <h3 class="tsd-anchor-link"><span>getNLeds</span><a href="#getNLeds" aria-label=
</div>
<h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">number</span></h4><aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/foomoon/xled-js/blob/25fabf6/src/lib/frame.ts#L44">lib/frame.ts:44</a></li></ul></aside></li></ul></section>
<li>Defined in <a href="https://github.com/foomoon/xled-js/blob/91b0b3b/src/lib/frame.ts#L44">lib/frame.ts:44</a></li></ul></aside></li></ul></section>
<section class="tsd-panel tsd-member"><a id="toOctet" class="tsd-anchor"></a>
<h3 class="tsd-anchor-link"><span>to<wbr/>Octet</span><a href="#toOctet" aria-label="Permalink" class="tsd-anchor-icon"><svg class="icon icon-tabler icon-tabler-link" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><use href="#icon-anchor-a"></use><use href="#icon-anchor-b"></use><use href="#icon-anchor-c"></use></svg></a></h3>
<ul class="tsd-signatures">
Expand All @@ -92,7 +92,7 @@ <h3 class="tsd-anchor-link"><span>to<wbr/>Octet</span><a href="#toOctet" aria-la
</div>
<h4 class="tsd-returns-title">Returns <span class="tsd-signature-type ">Uint8Array</span></h4><aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/foomoon/xled-js/blob/25fabf6/src/lib/frame.ts#L28">lib/frame.ts:28</a></li></ul></aside></li></ul></section></section></div>
<li>Defined in <a href="https://github.com/foomoon/xled-js/blob/91b0b3b/src/lib/frame.ts#L28">lib/frame.ts:28</a></li></ul></aside></li></ul></section></section></div>
<div class="col-sidebar">
<div class="page-menu">
<div class="tsd-navigation settings">
Expand Down
Loading

0 comments on commit f7b3b0f

Please sign in to comment.