@busy-app/busy-lib 0.0.2 → 0.0.4

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/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var f=Object.defineProperty;var u=(e,t,r)=>t in e?f(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r;var s=(e,t,r)=>u(e,typeof t!="symbol"?t+"":t,r);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("openapi-fetch");function l(e){const t=e.split(".");if(t.length!==4)return!1;for(const r of t){if(r.length===0||r.length>1&&r[0]==="0"||!/^\d+$/.test(r))return!1;const i=Number(r);if(i<0||i>255)return!1}return!0}const p=(e,t)=>{if(typeof FormData<"u"&&e instanceof FormData||typeof Buffer<"u"&&typeof Buffer.isBuffer=="function"&&Buffer.isBuffer(e)||typeof File<"u"&&e instanceof File||typeof Blob<"u"&&e instanceof Blob||typeof ArrayBuffer<"u"&&e instanceof ArrayBuffer||typeof ArrayBuffer<"u"&&ArrayBuffer.isView&&ArrayBuffer.isView(e))return e;let r;return t&&(t instanceof Headers?r=t.get("Content-Type")??t.get("content-type")??void 0:typeof t=="object"&&(r=t["Content-Type"]??t["content-type"]),r==="application/x-www-form-urlencoded")?e&&typeof e=="object"&&!(e instanceof URLSearchParams)?new URLSearchParams(e).toString():String(e):JSON.stringify(e)};let n=null;function w(e){n=c({baseUrl:e,bodySerializer:p})}async function d(e){const{appId:t,fileName:r,file:i}=e;if(!n)throw new Error("API client is not initialized");const{data:a,error:o}=await n.POST("/v0/assets/upload",{params:{query:{app_id:t,file:r}},headers:{"Content-Type":"application/octet-stream"},body:i});if(o)throw o;return a}async function y(e){const{appId:t}=e;if(!n)throw new Error("API client is not initialized");const{data:r,error:i}=await n.DELETE("/v0/assets/upload",{params:{query:{app_id:t}}});if(i)throw i;return r}const h={timeout:5,x:0,y:0,display:"front"};function m(e){return{...h,...e}}async function E(e){const{appId:t,elements:r}=e;if(!n)throw new Error("API client is not initialized");const i=r.map(m),{data:a,error:o}=await n.POST("/v0/display/draw",{body:{app_id:t,elements:i}});if(o)throw o;return a}async function A(){if(!n)throw new Error("API client is not initialized");const{data:e,error:t}=await n.DELETE("/v0/display/draw");if(t)throw t;return e}async function B(e){const{appId:t,path:r}=e;if(!n)throw new Error("API client is not initialized");const{data:i,error:a}=await n.POST("/v0/audio/play",{params:{query:{app_id:t,path:r}}});if(a)throw a;return i}async function P(){if(!n)throw new Error("API client is not initialized");const{data:e,error:t}=await n.DELETE("/v0/audio/play");if(t)throw t;return e}class S{constructor(t="10.0.4.20"){s(this,"ip");if(!l(t))throw new Error(`Incorrect IPv4: ${t}`);this.ip=t,console.log(this.ip),w(`http://${this.ip}/api/`)}async uploadAsset(t){return await d(t)}async deleteAssets(t){return await y(t)}async drawDisplay(t){return await E(t)}async clearDisplay(){return await A()}async playSound(t){return await B(t)}async stopSound(){return await P()}}exports.BusyBar=S;
1
+ "use strict";var d=Object.defineProperty;var w=(e,t,n)=>t in e?d(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var a=(e,t,n)=>w(e,typeof t!="symbol"?t+"":t,n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=require("openapi-fetch");function m(e){const t=e.split(".");if(t.length!==4)return!1;for(const n of t){if(n.length===0||n.length>1&&n[0]==="0"||!/^\d+$/.test(n))return!1;const r=Number(n);if(r<0||r>255)return!1}return!0}const g=(e,t)=>{if(typeof FormData<"u"&&e instanceof FormData||typeof Buffer<"u"&&typeof Buffer.isBuffer=="function"&&Buffer.isBuffer(e)||typeof File<"u"&&e instanceof File||typeof Blob<"u"&&e instanceof Blob||typeof ArrayBuffer<"u"&&e instanceof ArrayBuffer||typeof ArrayBuffer<"u"&&ArrayBuffer.isView&&ArrayBuffer.isView(e))return e;let n;return t&&(t instanceof Headers?n=t.get("Content-Type")??t.get("content-type")??void 0:typeof t=="object"&&(n=t["Content-Type"]??t["content-type"]),n==="application/x-www-form-urlencoded")?e&&typeof e=="object"&&!(e instanceof URLSearchParams)?new URLSearchParams(e).toString():String(e):JSON.stringify(e)};let o=null;function E(e){o=y({baseUrl:e,bodySerializer:g})}async function S(e){const{appId:t,fileName:n,file:r}=e;if(!o)throw new Error("API client is not initialized");const{data:i,error:s}=await o.POST("/v0/assets/upload",{params:{query:{app_id:t,file:n}},headers:{"Content-Type":"application/octet-stream"},body:r});if(s)throw s;return i}async function A(e){const{appId:t}=e;if(!o)throw new Error("API client is not initialized");const{data:n,error:r}=await o.DELETE("/v0/assets/upload",{params:{query:{app_id:t}}});if(r)throw r;return n}const B={timeout:5,x:0,y:0,display:"front"};function k(e){return{...B,...e}}async function D(e){const{appId:t,elements:n}=e;if(!o)throw new Error("API client is not initialized");const r=n.map(k),{data:i,error:s}=await o.POST("/v0/display/draw",{body:{app_id:t,elements:r}});if(s)throw s;return i}async function T(){if(!o)throw new Error("API client is not initialized");const{data:e,error:t}=await o.DELETE("/v0/display/draw");if(t)throw t;return e}async function L(e){const{appId:t,path:n}=e;if(!o)throw new Error("API client is not initialized");const{data:r,error:i}=await o.POST("/v0/audio/play",{params:{query:{app_id:t,path:n}}});if(i)throw i;return r}async function P(){if(!o)throw new Error("API client is not initialized");const{data:e,error:t}=await o.DELETE("/v0/audio/play");if(t)throw t;return e}class I{constructor(t="10.0.4.20"){a(this,"ip");if(!m(t))throw new Error(`Incorrect IPv4: ${t}`);this.ip=t,console.log(this.ip),E(`http://${this.ip}/api/`)}async uploadAsset(t){return await S(t)}async deleteAssets(t){return await A(t)}async drawDisplay(t){return await D(t)}async clearDisplay(){return await T()}async playSound(t){return await L(t)}async stopSound(){return await P()}}var l=(e=>(e[e.FRONT=0]="FRONT",e[e.BACK=1]="BACK",e))(l||{});const v=3e3,C=new Set([1001,1006,1012,1013,1014,3008]);function h(e,t){if(t<0||t>=e.length)throw new Error(`Index ${t} is out of bounds (0…${e.length-1})`);const n=e[t];if(n===void 0)throw new Error(`Unexpected undefined at index ${t}`);return n}function O(e,t){let n=0;const r=e.length,i=[];for(;n<r;){const s=h(e,n);if(n+=1,(s&128)!==0){const c=s&127;for(let f=0;f<c*t;f++)i.push(e[n+f]);n+=c*t}else{const c=s,f=e.slice(n,n+t);for(let p=0;p<c;p++)for(let u=0;u<t;u++)i.push(f[u]);n+=t}}return new Uint8Array(i)}function U(e){const t=new Uint8Array(e.length*2);let n=0,r=0;for(;n<e.length;){const i=h(e,n),s=i&15,c=i>>4&15;t[r]=s,t[r+1]=c,n+=1,r+=2}return t}class N{constructor(t){a(this,"connected",!1);a(this,"dataListeners",[]);a(this,"stopListeners",[]);a(this,"errorListeners",[]);a(this,"socket",null);if(this.config=t,!(()=>typeof window<"u"&&typeof window.document<"u"))throw new Error("not browser")}onData(t){this.dataListeners.push(t)}onStop(t){this.stopListeners.push(t)}onError(t){this.errorListeners.push(t)}emitData(t){for(const n of this.dataListeners)n(t)}emitStop(){for(const t of this.stopListeners)t()}emitError(t){for(const n of this.errorListeners)n(t)}async openWebsocket(){this.socket&&await this.closeWebsocket();let t="";this.config.mode==="cloud"?t=`${this.config.domain}/bars/${this.config.idDevice}/ws`:this.config.mode==="local"&&(t=`${this.config.barUrl}/api/v0/screen/ws`),this.socket=new WebSocket(t),this.socket.onopen=()=>{this.socket&&(this.config.mode==="cloud"?this.socket.send(JSON.stringify({token:this.config.token,display:this.config.deviceScreen})):this.config.mode==="local"&&this.socket.send(JSON.stringify({display:this.config.deviceScreen})),this.connected=!0)},this.socket.binaryType="arraybuffer",this.socket.onmessage=n=>{try{if(typeof n.data=="string")return;const r=new Uint8Array(n.data);if(this.config.mode==="cloud")this.emitData(r);else if(this.config.mode==="local"){let i;const s=this.config.deviceScreen===l.FRONT?3:2;try{const c=O(r,s);this.config.deviceScreen===l.BACK?i=U(c):i=c,this.emitData(i)}catch{this.emitData(r)}}}catch{this.connected=!1,this.emitStop()}},this.socket.onerror=n=>{this.connected=!1,this.emitError({code:1006,message:"WebSocket error occurred",raw:n}),this.emitStop()},this.socket.onclose=async n=>{if(this.socket=null,this.connected=!1,n.code===v||C.has(n.code)){this.emitError({code:n.code,message:n.reason,raw:n});return}this.emitStop()}}closeWebsocket(){return this.connected=!1,new Promise(t=>{this.socket?(this.socket.onclose=()=>{t()},this.socket.close(),this.socket=null):t(),this.emitStop()})}}exports.BusyBar=I;exports.DeviceScreen=l;exports.ScreenStream=N;
package/dist/index.d.ts CHANGED
@@ -3,6 +3,10 @@ declare interface AudioParams {
3
3
  path: paths["/v0/audio/play"]["post"]["parameters"]["query"]["path"];
4
4
  }
5
5
 
6
+ declare interface BaseConfig {
7
+ deviceScreen: DeviceScreen;
8
+ }
9
+
6
10
  /**
7
11
  * Main library class for interacting with the Busy Bar API.
8
12
  *
@@ -258,17 +262,39 @@ declare type CustomImageElement = MakeOptional<components["schemas"]["ImageEleme
258
262
 
259
263
  declare type CustomTextElement = MakeOptional<components["schemas"]["TextElement"], OptionalFields>;
260
264
 
265
+ declare type DataListener = (data: Uint8Array) => void;
266
+
261
267
  declare interface DeleteParams {
262
268
  appId: paths["/v0/assets/upload"]["delete"]["parameters"]["query"]["app_id"];
263
269
  }
264
270
 
271
+ declare type DeviceConfig = LocalConfig | SiteConfig;
272
+
273
+ export declare enum DeviceScreen {
274
+ FRONT = 0,
275
+ BACK = 1
276
+ }
277
+
265
278
  declare interface DrawParams {
266
279
  appId: paths["/v0/display/draw"]["post"]["requestBody"]["content"]["application/json"]["app_id"];
267
280
  elements: CustomElement[];
268
281
  }
269
282
 
283
+ declare type ErrorListener = (payload: ErrorPayload) => void;
284
+
285
+ declare interface ErrorPayload {
286
+ code: number;
287
+ message: string;
288
+ raw: Error | CloseEvent | Event;
289
+ }
290
+
270
291
  declare type IPv4 = string;
271
292
 
293
+ declare interface LocalConfig extends BaseConfig {
294
+ mode: "local";
295
+ barUrl: string;
296
+ }
297
+
272
298
  declare type MakeOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
273
299
 
274
300
  declare interface operations {
@@ -796,6 +822,33 @@ declare interface paths {
796
822
  };
797
823
  }
798
824
 
825
+ export declare class ScreenStream {
826
+ private config;
827
+ connected: boolean;
828
+ private dataListeners;
829
+ private stopListeners;
830
+ private errorListeners;
831
+ private socket;
832
+ constructor(config: DeviceConfig);
833
+ onData(listener: DataListener): void;
834
+ onStop(listener: StopListener): void;
835
+ onError(listener: ErrorListener): void;
836
+ protected emitData(data: Uint8Array): void;
837
+ protected emitStop(): void;
838
+ protected emitError(payload: ErrorPayload): void;
839
+ openWebsocket(): Promise<void>;
840
+ closeWebsocket(): Promise<void>;
841
+ }
842
+
843
+ declare interface SiteConfig extends BaseConfig {
844
+ mode: "cloud";
845
+ domain: string;
846
+ token: string;
847
+ idDevice: string;
848
+ }
849
+
850
+ declare type StopListener = () => void;
851
+
799
852
  declare interface UploadParams {
800
853
  appId: paths["/v0/assets/upload"]["post"]["parameters"]["query"]["app_id"];
801
854
  fileName: paths["/v0/assets/upload"]["post"]["parameters"]["query"]["file"];
package/dist/index.js CHANGED
@@ -1,42 +1,42 @@
1
- var s = Object.defineProperty;
2
- var u = (r, t, e) => t in r ? s(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
3
- var f = (r, t, e) => u(r, typeof t != "symbol" ? t + "" : t, e);
4
- import c from "openapi-fetch";
5
- function p(r) {
6
- const t = r.split(".");
1
+ var d = Object.defineProperty;
2
+ var w = (e, t, n) => t in e ? d(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3
+ var a = (e, t, n) => w(e, typeof t != "symbol" ? t + "" : t, n);
4
+ import y from "openapi-fetch";
5
+ function m(e) {
6
+ const t = e.split(".");
7
7
  if (t.length !== 4)
8
8
  return !1;
9
- for (const e of t) {
10
- if (e.length === 0 || e.length > 1 && e[0] === "0" || !/^\d+$/.test(e))
9
+ for (const n of t) {
10
+ if (n.length === 0 || n.length > 1 && n[0] === "0" || !/^\d+$/.test(n))
11
11
  return !1;
12
- const i = Number(e);
12
+ const i = Number(n);
13
13
  if (i < 0 || i > 255)
14
14
  return !1;
15
15
  }
16
16
  return !0;
17
17
  }
18
- const l = (r, t) => {
19
- if (typeof FormData < "u" && r instanceof FormData || typeof Buffer < "u" && typeof Buffer.isBuffer == "function" && Buffer.isBuffer(r) || typeof File < "u" && r instanceof File || typeof Blob < "u" && r instanceof Blob || typeof ArrayBuffer < "u" && r instanceof ArrayBuffer || typeof ArrayBuffer < "u" && ArrayBuffer.isView && ArrayBuffer.isView(r))
20
- return r;
21
- let e;
22
- return t && (t instanceof Headers ? e = t.get("Content-Type") ?? t.get("content-type") ?? void 0 : typeof t == "object" && (e = t["Content-Type"] ?? t["content-type"]), e === "application/x-www-form-urlencoded") ? r && typeof r == "object" && !(r instanceof URLSearchParams) ? new URLSearchParams(r).toString() : String(r) : JSON.stringify(r);
18
+ const g = (e, t) => {
19
+ if (typeof FormData < "u" && e instanceof FormData || typeof Buffer < "u" && typeof Buffer.isBuffer == "function" && Buffer.isBuffer(e) || typeof File < "u" && e instanceof File || typeof Blob < "u" && e instanceof Blob || typeof ArrayBuffer < "u" && e instanceof ArrayBuffer || typeof ArrayBuffer < "u" && ArrayBuffer.isView && ArrayBuffer.isView(e))
20
+ return e;
21
+ let n;
22
+ return t && (t instanceof Headers ? n = t.get("Content-Type") ?? t.get("content-type") ?? void 0 : typeof t == "object" && (n = t["Content-Type"] ?? t["content-type"]), n === "application/x-www-form-urlencoded") ? e && typeof e == "object" && !(e instanceof URLSearchParams) ? new URLSearchParams(e).toString() : String(e) : JSON.stringify(e);
23
23
  };
24
- let n = null;
25
- function w(r) {
26
- n = c({
27
- baseUrl: r,
28
- bodySerializer: l
24
+ let o = null;
25
+ function E(e) {
26
+ o = y({
27
+ baseUrl: e,
28
+ bodySerializer: g
29
29
  });
30
30
  }
31
- async function d(r) {
32
- const { appId: t, fileName: e, file: i } = r;
33
- if (!n)
31
+ async function A(e) {
32
+ const { appId: t, fileName: n, file: i } = e;
33
+ if (!o)
34
34
  throw new Error("API client is not initialized");
35
- const { data: a, error: o } = await n.POST("/v0/assets/upload", {
35
+ const { data: r, error: s } = await o.POST("/v0/assets/upload", {
36
36
  params: {
37
37
  query: {
38
38
  app_id: t,
39
- file: e
39
+ file: n
40
40
  }
41
41
  },
42
42
  headers: {
@@ -44,15 +44,15 @@ async function d(r) {
44
44
  },
45
45
  body: i
46
46
  });
47
- if (o)
48
- throw o;
49
- return a;
47
+ if (s)
48
+ throw s;
49
+ return r;
50
50
  }
51
- async function y(r) {
52
- const { appId: t } = r;
53
- if (!n)
51
+ async function S(e) {
52
+ const { appId: t } = e;
53
+ if (!o)
54
54
  throw new Error("API client is not initialized");
55
- const { data: e, error: i } = await n.DELETE("/v0/assets/upload", {
55
+ const { data: n, error: i } = await o.DELETE("/v0/assets/upload", {
56
56
  params: {
57
57
  query: {
58
58
  app_id: t
@@ -61,59 +61,59 @@ async function y(r) {
61
61
  });
62
62
  if (i)
63
63
  throw i;
64
- return e;
64
+ return n;
65
65
  }
66
- const m = { timeout: 5, x: 0, y: 0, display: "front" };
67
- function h(r) {
68
- return { ...m, ...r };
66
+ const B = { timeout: 5, x: 0, y: 0, display: "front" };
67
+ function k(e) {
68
+ return { ...B, ...e };
69
69
  }
70
- async function E(r) {
71
- const { appId: t, elements: e } = r;
72
- if (!n)
70
+ async function D(e) {
71
+ const { appId: t, elements: n } = e;
72
+ if (!o)
73
73
  throw new Error("API client is not initialized");
74
- const i = e.map(h), { data: a, error: o } = await n.POST("/v0/display/draw", {
74
+ const i = n.map(k), { data: r, error: s } = await o.POST("/v0/display/draw", {
75
75
  body: {
76
76
  app_id: t,
77
77
  elements: i
78
78
  }
79
79
  });
80
- if (o)
81
- throw o;
82
- return a;
80
+ if (s)
81
+ throw s;
82
+ return r;
83
83
  }
84
- async function A() {
85
- if (!n)
84
+ async function L() {
85
+ if (!o)
86
86
  throw new Error("API client is not initialized");
87
- const { data: r, error: t } = await n.DELETE("/v0/display/draw");
87
+ const { data: e, error: t } = await o.DELETE("/v0/display/draw");
88
88
  if (t)
89
89
  throw t;
90
- return r;
90
+ return e;
91
91
  }
92
- async function B(r) {
93
- const { appId: t, path: e } = r;
94
- if (!n)
92
+ async function T(e) {
93
+ const { appId: t, path: n } = e;
94
+ if (!o)
95
95
  throw new Error("API client is not initialized");
96
- const { data: i, error: a } = await n.POST("/v0/audio/play", {
96
+ const { data: i, error: r } = await o.POST("/v0/audio/play", {
97
97
  params: {
98
98
  query: {
99
99
  app_id: t,
100
- path: e
100
+ path: n
101
101
  }
102
102
  }
103
103
  });
104
- if (a)
105
- throw a;
104
+ if (r)
105
+ throw r;
106
106
  return i;
107
107
  }
108
108
  async function I() {
109
- if (!n)
109
+ if (!o)
110
110
  throw new Error("API client is not initialized");
111
- const { data: r, error: t } = await n.DELETE("/v0/audio/play");
111
+ const { data: e, error: t } = await o.DELETE("/v0/audio/play");
112
112
  if (t)
113
113
  throw t;
114
- return r;
114
+ return e;
115
115
  }
116
- class T {
116
+ class $ {
117
117
  /**
118
118
  * Creates an instance of BUSY Bar.
119
119
  * Initializes the API client with the provided IPv4 address.
@@ -127,10 +127,10 @@ class T {
127
127
  * @type {IPv4}
128
128
  * @readonly
129
129
  */
130
- f(this, "ip");
131
- if (!p(t))
130
+ a(this, "ip");
131
+ if (!m(t))
132
132
  throw new Error(`Incorrect IPv4: ${t}`);
133
- this.ip = t, console.log(this.ip), w(`http://${this.ip}/api/`);
133
+ this.ip = t, console.log(this.ip), E(`http://${this.ip}/api/`);
134
134
  }
135
135
  /**
136
136
  * Uploads an asset to the device.
@@ -142,7 +142,7 @@ class T {
142
142
  * @returns {Promise<{ result: string }>} Result of the upload operation.
143
143
  */
144
144
  async uploadAsset(t) {
145
- return await d(t);
145
+ return await A(t);
146
146
  }
147
147
  /**
148
148
  * Deletes all assets for a specific application from the device.
@@ -152,7 +152,7 @@ class T {
152
152
  * @returns {Promise<{ result: string }>} Result of the delete operation.
153
153
  */
154
154
  async deleteAssets(t) {
155
- return await y(t);
155
+ return await S(t);
156
156
  }
157
157
  /**
158
158
  * Draws elements on the device display.
@@ -163,7 +163,7 @@ class T {
163
163
  * @returns {Promise<{ result: string }>} Result of the draw operation.
164
164
  */
165
165
  async drawDisplay(t) {
166
- return await E(t);
166
+ return await D(t);
167
167
  }
168
168
  /**
169
169
  * Clears the device display and stops the Canvas application if running.
@@ -171,7 +171,7 @@ class T {
171
171
  * @returns {Promise<{ result: string }>} Result of the clear operation.
172
172
  */
173
173
  async clearDisplay() {
174
- return await A();
174
+ return await L();
175
175
  }
176
176
  /**
177
177
  * Plays an audio file from the assets directory.
@@ -182,7 +182,7 @@ class T {
182
182
  * @returns {Promise<{ result: string }>} Result of the play operation.
183
183
  */
184
184
  async playSound(t) {
185
- return await B(t);
185
+ return await T(t);
186
186
  }
187
187
  /**
188
188
  * Stops any currently playing audio on the device.
@@ -193,6 +193,135 @@ class T {
193
193
  return await I();
194
194
  }
195
195
  }
196
+ var u = /* @__PURE__ */ ((e) => (e[e.FRONT = 0] = "FRONT", e[e.BACK = 1] = "BACK", e))(u || {});
197
+ const P = 3e3, C = /* @__PURE__ */ new Set([1001, 1006, 1012, 1013, 1014, 3008]);
198
+ function h(e, t) {
199
+ if (t < 0 || t >= e.length)
200
+ throw new Error(`Index ${t} is out of bounds (0…${e.length - 1})`);
201
+ const n = e[t];
202
+ if (n === void 0)
203
+ throw new Error(`Unexpected undefined at index ${t}`);
204
+ return n;
205
+ }
206
+ function O(e, t) {
207
+ let n = 0;
208
+ const i = e.length, r = [];
209
+ for (; n < i; ) {
210
+ const s = h(e, n);
211
+ if (n += 1, (s & 128) !== 0) {
212
+ const c = s & 127;
213
+ for (let f = 0; f < c * t; f++)
214
+ r.push(e[n + f]);
215
+ n += c * t;
216
+ } else {
217
+ const c = s, f = e.slice(n, n + t);
218
+ for (let p = 0; p < c; p++)
219
+ for (let l = 0; l < t; l++)
220
+ r.push(f[l]);
221
+ n += t;
222
+ }
223
+ }
224
+ return new Uint8Array(r);
225
+ }
226
+ function U(e) {
227
+ const t = new Uint8Array(e.length * 2);
228
+ let n = 0, i = 0;
229
+ for (; n < e.length; ) {
230
+ const r = h(e, n), s = r & 15, c = r >> 4 & 15;
231
+ t[i] = s, t[i + 1] = c, n += 1, i += 2;
232
+ }
233
+ return t;
234
+ }
235
+ class F {
236
+ constructor(t) {
237
+ a(this, "connected", !1);
238
+ a(this, "dataListeners", []);
239
+ a(this, "stopListeners", []);
240
+ a(this, "errorListeners", []);
241
+ a(this, "socket", null);
242
+ if (this.config = t, !(() => typeof window < "u" && typeof window.document < "u"))
243
+ throw new Error("not browser");
244
+ }
245
+ onData(t) {
246
+ this.dataListeners.push(t);
247
+ }
248
+ onStop(t) {
249
+ this.stopListeners.push(t);
250
+ }
251
+ onError(t) {
252
+ this.errorListeners.push(t);
253
+ }
254
+ emitData(t) {
255
+ for (const n of this.dataListeners)
256
+ n(t);
257
+ }
258
+ emitStop() {
259
+ for (const t of this.stopListeners)
260
+ t();
261
+ }
262
+ emitError(t) {
263
+ for (const n of this.errorListeners)
264
+ n(t);
265
+ }
266
+ async openWebsocket() {
267
+ this.socket && await this.closeWebsocket();
268
+ let t = "";
269
+ this.config.mode === "cloud" ? t = `${this.config.domain}/bars/${this.config.idDevice}/ws` : this.config.mode === "local" && (t = `${this.config.barUrl}/api/v0/screen/ws`), this.socket = new WebSocket(t), this.socket.onopen = () => {
270
+ this.socket && (this.config.mode === "cloud" ? this.socket.send(
271
+ JSON.stringify({
272
+ token: this.config.token,
273
+ display: this.config.deviceScreen
274
+ })
275
+ ) : this.config.mode === "local" && this.socket.send(JSON.stringify({ display: this.config.deviceScreen })), this.connected = !0);
276
+ }, this.socket.binaryType = "arraybuffer", this.socket.onmessage = (n) => {
277
+ try {
278
+ if (typeof n.data == "string")
279
+ return;
280
+ const i = new Uint8Array(n.data);
281
+ if (this.config.mode === "cloud")
282
+ this.emitData(i);
283
+ else if (this.config.mode === "local") {
284
+ let r;
285
+ const s = this.config.deviceScreen === u.FRONT ? 3 : 2;
286
+ try {
287
+ const c = O(i, s);
288
+ this.config.deviceScreen === u.BACK ? r = U(c) : r = c, this.emitData(r);
289
+ } catch {
290
+ this.emitData(i);
291
+ }
292
+ }
293
+ } catch {
294
+ this.connected = !1, this.emitStop();
295
+ }
296
+ }, this.socket.onerror = (n) => {
297
+ this.connected = !1, this.emitError({
298
+ code: 1006,
299
+ // Standard «abnormal closure» code per RFC-6455
300
+ message: "WebSocket error occurred",
301
+ raw: n
302
+ }), this.emitStop();
303
+ }, this.socket.onclose = async (n) => {
304
+ if (this.socket = null, this.connected = !1, n.code === P || C.has(n.code)) {
305
+ this.emitError({
306
+ code: n.code,
307
+ message: n.reason,
308
+ raw: n
309
+ });
310
+ return;
311
+ }
312
+ this.emitStop();
313
+ };
314
+ }
315
+ closeWebsocket() {
316
+ return this.connected = !1, new Promise((t) => {
317
+ this.socket ? (this.socket.onclose = () => {
318
+ t();
319
+ }, this.socket.close(), this.socket = null) : t(), this.emitStop();
320
+ });
321
+ }
322
+ }
196
323
  export {
197
- T as BusyBar
324
+ $ as BusyBar,
325
+ u as DeviceScreen,
326
+ F as ScreenStream
198
327
  };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@busy-app/busy-lib",
3
3
  "private": false,
4
4
  "description": "A library for interacting with the BUSY Bar API",
5
- "version": "0.0.2",
5
+ "version": "0.0.4",
6
6
  "publishConfig": {
7
7
  "access": "public"
8
8
  },