steamutils 1.3.83 → 1.3.84

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/utils.js +141 -131
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "steamutils",
3
- "version": "1.3.83",
3
+ "version": "1.3.84",
4
4
  "main": "index.js",
5
5
  "dependencies": {
6
6
  "alpha-common-utils": "^1.0.6",
package/utils.js CHANGED
@@ -1,131 +1,141 @@
1
- const isBrowser = typeof window !== 'undefined'
2
-
3
- export const sleep = (ms) => {
4
- return new Promise(resolve => {
5
- setTimeout(resolve, ms)
6
- })
7
- }
8
-
9
-
10
- /**
11
- * const audioTrack = stream.getAudioTracks()[0]
12
- * const videoStream = UserMedia.createBlankVideoTrack()
13
- * videoStream.addTrack(audioTrack)
14
- * stream = videoStream
15
- * **/
16
- export const createBlankVideoTrack = (opts = {}) => {
17
- const {
18
- width = 1920,
19
- height = 1080
20
- } = opts
21
-
22
- const canvas = Object.assign(document.createElement('canvas'), {
23
- width,
24
- height,
25
- })
26
-
27
- canvas.getContext('2d').fillRect(0, 0, width, height)
28
-
29
- return canvas.captureStream()
30
- }
31
-
32
-
33
- const minDate = new Date(0),
34
- maxDate = new Date(parseInt('ffffffff', 16) * 1000)
35
-
36
- export function objectIdFromDate(date) {
37
- if (date < minDate || date > maxDate) {
38
- return 'Error: date must be between ' + minDate.getFullYear() + ' and ' + maxDate.getFullYear()
39
- }
40
- var pad = '00000000'
41
- var hexSeconds = Math.floor(date.getTime() / 1000).toString(16)
42
- return pad.substring(0, pad.length - hexSeconds.length) + hexSeconds + '0000000000000000'
43
- }
44
-
45
- export function dateFromObjectId(objectId) {
46
- return new Date(parseInt(objectId.substring(0, 8), 16) * 1000)
47
- }
48
-
49
- export function console_log(...args) {
50
- const params = []
51
- params.push(new Date().toUTCString())
52
- const errorStack = (new Error()).stack
53
- const fnName = errorStack.split('\n').map(e => e?.trim()).filter(e => e.startsWith('at') && !e.startsWith('at console_log') && !e.startsWith('at processTicksAndRejections'))[0].substr(3)
54
- params.push(fnName)
55
- console.log(params.filter(Boolean).map(p => `[${p.trim()}]`).join(' '), ...args)
56
- }
57
-
58
- export function removeSpaceKeys(object) {//mutate object
59
- if (!object || Array.isArray(object)) {
60
- return object
61
- }
62
-
63
- Object.entries(object).forEach(([key, value]) => {
64
- const newKey = key.replaceAll(/[^a-zA-Z0-9]/gi, '_')
65
- if (newKey !== key) {
66
- delete object[key]
67
- object[newKey] = value
68
- }
69
- })
70
- return object
71
- }
72
-
73
- export function getCleanObject(object) {//like removeSpaceKeys but not mutate object
74
- if (!object || Array.isArray(object)) {
75
- return object
76
- }
77
-
78
- const newObject = {}
79
- Object.entries(object).forEach(([key, value]) => {
80
- const newKey = key.replaceAll(/[^a-zA-Z0-9]/gi, '_')
81
- newObject[newKey] = value
82
- })
83
- return newObject
84
- }
85
-
86
- export function JSON_parse(data) {
87
- try {
88
- return JSON.parse(data)
89
- } catch (e) {
90
- return null
91
- }
92
- }
93
-
94
- export function JSON_stringify(data) {
95
- try {
96
- return JSON.stringify(data)
97
- } catch (e) {
98
- return null
99
- }
100
- }
101
-
102
-
103
- export async function throttle(fn, delay) {
104
- let canFire = true
105
- let queue = []
106
-
107
- async function pop() {
108
- if (queue.length < 1) return
109
-
110
- const [that, args] = queue.pop()
111
- await fn.apply(that, args)
112
- canFire = false
113
- setTimeout(async () => {
114
- canFire = true
115
- await pop()
116
- }, delay)
117
- }
118
-
119
- async function push() {
120
- queue.push([this, arguments])
121
- if (canFire) {
122
- await pop()
123
- }
124
- }
125
-
126
- push.cancel = () => {
127
- queue = []
128
- }
129
-
130
- return push
131
- }
1
+ const isBrowser = typeof window !== "undefined";
2
+
3
+ export const sleep = (ms) => {
4
+ return new Promise((resolve) => {
5
+ setTimeout(resolve, ms);
6
+ });
7
+ };
8
+
9
+ export const sleepRandom = async (startMs, endMs) => {
10
+ return await sleep(Math.random() * (endMs - startMs) + startMs);
11
+ };
12
+
13
+ /**
14
+ * const audioTrack = stream.getAudioTracks()[0]
15
+ * const videoStream = UserMedia.createBlankVideoTrack()
16
+ * videoStream.addTrack(audioTrack)
17
+ * stream = videoStream
18
+ * **/
19
+ export const createBlankVideoTrack = (opts = {}) => {
20
+ const { width = 1920, height = 1080 } = opts;
21
+
22
+ const canvas = Object.assign(document.createElement("canvas"), {
23
+ width,
24
+ height,
25
+ });
26
+
27
+ canvas.getContext("2d").fillRect(0, 0, width, height);
28
+
29
+ return canvas.captureStream();
30
+ };
31
+
32
+ const minDate = new Date(0),
33
+ maxDate = new Date(parseInt("ffffffff", 16) * 1000);
34
+
35
+ export function objectIdFromDate(date) {
36
+ if (date < minDate || date > maxDate) {
37
+ return `Error: date must be between ${minDate.getFullYear()} and ${maxDate.getFullYear()}`;
38
+ }
39
+ var pad = "00000000";
40
+ var hexSeconds = Math.floor(date.getTime() / 1000).toString(16);
41
+ return `${pad.substring(0, pad.length - hexSeconds.length) + hexSeconds}0000000000000000`;
42
+ }
43
+
44
+ export function dateFromObjectId(objectId) {
45
+ return new Date(parseInt(objectId.substring(0, 8), 16) * 1000);
46
+ }
47
+
48
+ export function console_log(...args) {
49
+ const params = [];
50
+ params.push(new Date().toUTCString());
51
+ const errorStack = new Error().stack;
52
+ const fnName = errorStack
53
+ .split("\n")
54
+ .map((e) => e?.trim())
55
+ .filter((e) => e.startsWith("at") && !e.startsWith("at console_log") && !e.startsWith("at processTicksAndRejections"))[0]
56
+ .substr(3);
57
+ params.push(fnName);
58
+ console.log(
59
+ params
60
+ .filter(Boolean)
61
+ .map((p) => `[${p.trim()}]`)
62
+ .join(" "),
63
+ ...args,
64
+ );
65
+ }
66
+
67
+ export function removeSpaceKeys(object) {
68
+ //mutate object
69
+ if (!object || Array.isArray(object)) {
70
+ return object;
71
+ }
72
+
73
+ Object.entries(object).forEach(([key, value]) => {
74
+ const newKey = key.replaceAll(/[^a-zA-Z0-9]/gi, "_");
75
+ if (newKey !== key) {
76
+ delete object[key];
77
+ object[newKey] = value;
78
+ }
79
+ });
80
+ return object;
81
+ }
82
+
83
+ export function getCleanObject(object) {
84
+ //like removeSpaceKeys but not mutate object
85
+ if (!object || Array.isArray(object)) {
86
+ return object;
87
+ }
88
+
89
+ const newObject = {};
90
+ Object.entries(object).forEach(([key, value]) => {
91
+ const newKey = key.replaceAll(/[^a-zA-Z0-9]/gi, "_");
92
+ newObject[newKey] = value;
93
+ });
94
+ return newObject;
95
+ }
96
+
97
+ export function JSON_parse(data) {
98
+ try {
99
+ return JSON.parse(data);
100
+ } catch (e) {
101
+ return null;
102
+ }
103
+ }
104
+
105
+ export function JSON_stringify(data) {
106
+ try {
107
+ return JSON.stringify(data);
108
+ } catch (e) {
109
+ return null;
110
+ }
111
+ }
112
+
113
+ export async function throttle(fn, delay) {
114
+ let canFire = true;
115
+ let queue = [];
116
+
117
+ async function pop() {
118
+ if (queue.length < 1) return;
119
+
120
+ const [that, args] = queue.pop();
121
+ await fn.apply(that, args);
122
+ canFire = false;
123
+ setTimeout(async () => {
124
+ canFire = true;
125
+ await pop();
126
+ }, delay);
127
+ }
128
+
129
+ async function push() {
130
+ queue.push([this, arguments]);
131
+ if (canFire) {
132
+ await pop();
133
+ }
134
+ }
135
+
136
+ push.cancel = () => {
137
+ queue = [];
138
+ };
139
+
140
+ return push;
141
+ }