@robinpath/cli 1.73.0 → 1.75.0
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/README.md +111 -111
- package/dist/cli.mjs +344 -301
- package/modules/_helpers.js +33 -33
- package/modules/assert.js +270 -270
- package/modules/buffer.js +245 -245
- package/modules/child.js +176 -176
- package/modules/crypto.js +352 -352
- package/modules/dns.js +146 -146
- package/modules/events.js +174 -174
- package/modules/file.js +361 -361
- package/modules/http.js +268 -268
- package/modules/index.js +76 -76
- package/modules/net.js +189 -189
- package/modules/os.js +219 -219
- package/modules/path.js +162 -162
- package/modules/process.js +214 -214
- package/modules/stream.js +322 -322
- package/modules/string_decoder.js +106 -106
- package/modules/timer.js +167 -167
- package/modules/tls.js +264 -264
- package/modules/tty.js +169 -169
- package/modules/url.js +189 -189
- package/modules/util.js +275 -275
- package/modules/zlib.js +126 -126
- package/package.json +1 -1
|
@@ -1,106 +1,106 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Native string_decoder module for RobinPath.
|
|
3
|
-
* Decode Buffer sequences to strings, handling multi-byte characters.
|
|
4
|
-
*/
|
|
5
|
-
import { StringDecoder } from 'node:string_decoder';
|
|
6
|
-
import { toStr, requireArgs } from './_helpers.js';
|
|
7
|
-
|
|
8
|
-
const _decoders = new Map();
|
|
9
|
-
let _nextId = 1;
|
|
10
|
-
|
|
11
|
-
export const StringDecoderFunctions = {
|
|
12
|
-
|
|
13
|
-
create: (args) => {
|
|
14
|
-
const encoding = toStr(args[0], 'utf-8');
|
|
15
|
-
const id = `decoder_${_nextId++}`;
|
|
16
|
-
_decoders.set(id, new StringDecoder(encoding));
|
|
17
|
-
return id;
|
|
18
|
-
},
|
|
19
|
-
|
|
20
|
-
write: (args) => {
|
|
21
|
-
requireArgs('stringDecoder.write', args, 2);
|
|
22
|
-
const id = toStr(args[0]);
|
|
23
|
-
const decoder = _decoders.get(id);
|
|
24
|
-
if (!decoder) throw new Error(`stringDecoder.write: decoder ${id} not found`);
|
|
25
|
-
const buf = Buffer.from(toStr(args[1]), 'base64');
|
|
26
|
-
return decoder.write(buf);
|
|
27
|
-
},
|
|
28
|
-
|
|
29
|
-
end: (args) => {
|
|
30
|
-
requireArgs('stringDecoder.end', args, 1);
|
|
31
|
-
const id = toStr(args[0]);
|
|
32
|
-
const decoder = _decoders.get(id);
|
|
33
|
-
if (!decoder) throw new Error(`stringDecoder.end: decoder ${id} not found`);
|
|
34
|
-
const result = decoder.end();
|
|
35
|
-
_decoders.delete(id);
|
|
36
|
-
return result;
|
|
37
|
-
},
|
|
38
|
-
|
|
39
|
-
decode: (args) => {
|
|
40
|
-
requireArgs('stringDecoder.decode', args, 1);
|
|
41
|
-
const encoding = toStr(args[1], 'utf-8');
|
|
42
|
-
const buf = Buffer.from(toStr(args[0]), 'base64');
|
|
43
|
-
const decoder = new StringDecoder(encoding);
|
|
44
|
-
return decoder.write(buf) + decoder.end();
|
|
45
|
-
},
|
|
46
|
-
|
|
47
|
-
destroy: (args) => {
|
|
48
|
-
requireArgs('stringDecoder.destroy', args, 1);
|
|
49
|
-
const id = toStr(args[0]);
|
|
50
|
-
if (_decoders.has(id)) {
|
|
51
|
-
_decoders.delete(id);
|
|
52
|
-
return true;
|
|
53
|
-
}
|
|
54
|
-
return false;
|
|
55
|
-
},
|
|
56
|
-
|
|
57
|
-
active: () => Array.from(_decoders.keys())
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
export const StringDecoderFunctionMetadata = {
|
|
61
|
-
create: {
|
|
62
|
-
description: 'Create a string decoder for an encoding',
|
|
63
|
-
parameters: [{ name: 'encoding', dataType: 'string', description: 'Encoding (utf-8, ascii, base64, hex, etc.)', formInputType: 'text', required: false, defaultValue: 'utf-8' }],
|
|
64
|
-
returnType: 'string', returnDescription: 'Decoder handle ID', example: 'stringDecoder.create "utf-8"'
|
|
65
|
-
},
|
|
66
|
-
write: {
|
|
67
|
-
description: 'Write buffer data through decoder',
|
|
68
|
-
parameters: [
|
|
69
|
-
{ name: 'decoderId', dataType: 'string', description: 'Decoder handle', formInputType: 'text', required: true },
|
|
70
|
-
{ name: 'buffer', dataType: 'string', description: 'Base64-encoded buffer data', formInputType: 'text', required: true }
|
|
71
|
-
],
|
|
72
|
-
returnType: 'string', returnDescription: 'Decoded string', example: 'stringDecoder.write $dec $buf'
|
|
73
|
-
},
|
|
74
|
-
end: {
|
|
75
|
-
description: 'Flush remaining bytes and close decoder',
|
|
76
|
-
parameters: [{ name: 'decoderId', dataType: 'string', description: 'Decoder handle', formInputType: 'text', required: true }],
|
|
77
|
-
returnType: 'string', returnDescription: 'Any remaining decoded bytes', example: 'stringDecoder.end $dec'
|
|
78
|
-
},
|
|
79
|
-
decode: {
|
|
80
|
-
description: 'One-shot decode: buffer to string',
|
|
81
|
-
parameters: [
|
|
82
|
-
{ name: 'buffer', dataType: 'string', description: 'Base64-encoded buffer', formInputType: 'text', required: true },
|
|
83
|
-
{ name: 'encoding', dataType: 'string', description: 'Encoding (default: utf-8)', formInputType: 'text', required: false, defaultValue: 'utf-8' }
|
|
84
|
-
],
|
|
85
|
-
returnType: 'string', returnDescription: 'Decoded string', example: 'stringDecoder.decode $buf "utf-8"'
|
|
86
|
-
},
|
|
87
|
-
destroy: {
|
|
88
|
-
description: 'Destroy a decoder',
|
|
89
|
-
parameters: [{ name: 'decoderId', dataType: 'string', description: 'Decoder handle', formInputType: 'text', required: true }],
|
|
90
|
-
returnType: 'boolean', returnDescription: 'true if destroyed', example: 'stringDecoder.destroy $dec'
|
|
91
|
-
},
|
|
92
|
-
active: { description: 'List active decoders', parameters: [], returnType: 'array', returnDescription: 'Array of decoder IDs', example: 'stringDecoder.active' }
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
export const StringDecoderModuleMetadata = {
|
|
96
|
-
description: 'String decoder: convert Buffer sequences to strings with multi-byte character handling',
|
|
97
|
-
methods: Object.keys(StringDecoderFunctions)
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
export default {
|
|
101
|
-
name: 'stringDecoder',
|
|
102
|
-
functions: StringDecoderFunctions,
|
|
103
|
-
functionMetadata: StringDecoderFunctionMetadata,
|
|
104
|
-
moduleMetadata: StringDecoderModuleMetadata,
|
|
105
|
-
global: false
|
|
106
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* Native string_decoder module for RobinPath.
|
|
3
|
+
* Decode Buffer sequences to strings, handling multi-byte characters.
|
|
4
|
+
*/
|
|
5
|
+
import { StringDecoder } from 'node:string_decoder';
|
|
6
|
+
import { toStr, requireArgs } from './_helpers.js';
|
|
7
|
+
|
|
8
|
+
const _decoders = new Map();
|
|
9
|
+
let _nextId = 1;
|
|
10
|
+
|
|
11
|
+
export const StringDecoderFunctions = {
|
|
12
|
+
|
|
13
|
+
create: (args) => {
|
|
14
|
+
const encoding = toStr(args[0], 'utf-8');
|
|
15
|
+
const id = `decoder_${_nextId++}`;
|
|
16
|
+
_decoders.set(id, new StringDecoder(encoding));
|
|
17
|
+
return id;
|
|
18
|
+
},
|
|
19
|
+
|
|
20
|
+
write: (args) => {
|
|
21
|
+
requireArgs('stringDecoder.write', args, 2);
|
|
22
|
+
const id = toStr(args[0]);
|
|
23
|
+
const decoder = _decoders.get(id);
|
|
24
|
+
if (!decoder) throw new Error(`stringDecoder.write: decoder ${id} not found`);
|
|
25
|
+
const buf = Buffer.from(toStr(args[1]), 'base64');
|
|
26
|
+
return decoder.write(buf);
|
|
27
|
+
},
|
|
28
|
+
|
|
29
|
+
end: (args) => {
|
|
30
|
+
requireArgs('stringDecoder.end', args, 1);
|
|
31
|
+
const id = toStr(args[0]);
|
|
32
|
+
const decoder = _decoders.get(id);
|
|
33
|
+
if (!decoder) throw new Error(`stringDecoder.end: decoder ${id} not found`);
|
|
34
|
+
const result = decoder.end();
|
|
35
|
+
_decoders.delete(id);
|
|
36
|
+
return result;
|
|
37
|
+
},
|
|
38
|
+
|
|
39
|
+
decode: (args) => {
|
|
40
|
+
requireArgs('stringDecoder.decode', args, 1);
|
|
41
|
+
const encoding = toStr(args[1], 'utf-8');
|
|
42
|
+
const buf = Buffer.from(toStr(args[0]), 'base64');
|
|
43
|
+
const decoder = new StringDecoder(encoding);
|
|
44
|
+
return decoder.write(buf) + decoder.end();
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
destroy: (args) => {
|
|
48
|
+
requireArgs('stringDecoder.destroy', args, 1);
|
|
49
|
+
const id = toStr(args[0]);
|
|
50
|
+
if (_decoders.has(id)) {
|
|
51
|
+
_decoders.delete(id);
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
return false;
|
|
55
|
+
},
|
|
56
|
+
|
|
57
|
+
active: () => Array.from(_decoders.keys())
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export const StringDecoderFunctionMetadata = {
|
|
61
|
+
create: {
|
|
62
|
+
description: 'Create a string decoder for an encoding',
|
|
63
|
+
parameters: [{ name: 'encoding', dataType: 'string', description: 'Encoding (utf-8, ascii, base64, hex, etc.)', formInputType: 'text', required: false, defaultValue: 'utf-8' }],
|
|
64
|
+
returnType: 'string', returnDescription: 'Decoder handle ID', example: 'stringDecoder.create "utf-8"'
|
|
65
|
+
},
|
|
66
|
+
write: {
|
|
67
|
+
description: 'Write buffer data through decoder',
|
|
68
|
+
parameters: [
|
|
69
|
+
{ name: 'decoderId', dataType: 'string', description: 'Decoder handle', formInputType: 'text', required: true },
|
|
70
|
+
{ name: 'buffer', dataType: 'string', description: 'Base64-encoded buffer data', formInputType: 'text', required: true }
|
|
71
|
+
],
|
|
72
|
+
returnType: 'string', returnDescription: 'Decoded string', example: 'stringDecoder.write $dec $buf'
|
|
73
|
+
},
|
|
74
|
+
end: {
|
|
75
|
+
description: 'Flush remaining bytes and close decoder',
|
|
76
|
+
parameters: [{ name: 'decoderId', dataType: 'string', description: 'Decoder handle', formInputType: 'text', required: true }],
|
|
77
|
+
returnType: 'string', returnDescription: 'Any remaining decoded bytes', example: 'stringDecoder.end $dec'
|
|
78
|
+
},
|
|
79
|
+
decode: {
|
|
80
|
+
description: 'One-shot decode: buffer to string',
|
|
81
|
+
parameters: [
|
|
82
|
+
{ name: 'buffer', dataType: 'string', description: 'Base64-encoded buffer', formInputType: 'text', required: true },
|
|
83
|
+
{ name: 'encoding', dataType: 'string', description: 'Encoding (default: utf-8)', formInputType: 'text', required: false, defaultValue: 'utf-8' }
|
|
84
|
+
],
|
|
85
|
+
returnType: 'string', returnDescription: 'Decoded string', example: 'stringDecoder.decode $buf "utf-8"'
|
|
86
|
+
},
|
|
87
|
+
destroy: {
|
|
88
|
+
description: 'Destroy a decoder',
|
|
89
|
+
parameters: [{ name: 'decoderId', dataType: 'string', description: 'Decoder handle', formInputType: 'text', required: true }],
|
|
90
|
+
returnType: 'boolean', returnDescription: 'true if destroyed', example: 'stringDecoder.destroy $dec'
|
|
91
|
+
},
|
|
92
|
+
active: { description: 'List active decoders', parameters: [], returnType: 'array', returnDescription: 'Array of decoder IDs', example: 'stringDecoder.active' }
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
export const StringDecoderModuleMetadata = {
|
|
96
|
+
description: 'String decoder: convert Buffer sequences to strings with multi-byte character handling',
|
|
97
|
+
methods: Object.keys(StringDecoderFunctions)
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
export default {
|
|
101
|
+
name: 'stringDecoder',
|
|
102
|
+
functions: StringDecoderFunctions,
|
|
103
|
+
functionMetadata: StringDecoderFunctionMetadata,
|
|
104
|
+
moduleMetadata: StringDecoderModuleMetadata,
|
|
105
|
+
global: false
|
|
106
|
+
};
|
package/modules/timer.js
CHANGED
|
@@ -1,167 +1,167 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Native timer module for RobinPath.
|
|
3
|
-
* Sleep, delay, and interval utilities.
|
|
4
|
-
*/
|
|
5
|
-
import { toNum, requireArgs } from './_helpers.js';
|
|
6
|
-
|
|
7
|
-
const _intervals = new Map();
|
|
8
|
-
const _timeouts = new Map();
|
|
9
|
-
let _nextId = 1;
|
|
10
|
-
|
|
11
|
-
export const TimerFunctions = {
|
|
12
|
-
|
|
13
|
-
sleep: (args) => {
|
|
14
|
-
const ms = toNum(args[0], 1000);
|
|
15
|
-
return new Promise(resolve => setTimeout(() => resolve(true), ms));
|
|
16
|
-
},
|
|
17
|
-
|
|
18
|
-
delay: (args) => {
|
|
19
|
-
const ms = toNum(args[0], 1000);
|
|
20
|
-
return new Promise(resolve => setTimeout(() => resolve(true), ms));
|
|
21
|
-
},
|
|
22
|
-
|
|
23
|
-
setTimeout: (args, callback) => {
|
|
24
|
-
requireArgs('timer.setTimeout', args, 1);
|
|
25
|
-
const ms = toNum(args[0], 1000);
|
|
26
|
-
const id = `timeout_${_nextId++}`;
|
|
27
|
-
const handle = setTimeout(() => {
|
|
28
|
-
_timeouts.delete(id);
|
|
29
|
-
if (callback) callback([id]);
|
|
30
|
-
}, ms);
|
|
31
|
-
_timeouts.set(id, handle);
|
|
32
|
-
return id;
|
|
33
|
-
},
|
|
34
|
-
|
|
35
|
-
setInterval: (args, callback) => {
|
|
36
|
-
requireArgs('timer.setInterval', args, 1);
|
|
37
|
-
const ms = toNum(args[0], 1000);
|
|
38
|
-
const id = `interval_${_nextId++}`;
|
|
39
|
-
const handle = setInterval(() => {
|
|
40
|
-
if (callback) callback([id]);
|
|
41
|
-
}, ms);
|
|
42
|
-
_intervals.set(id, handle);
|
|
43
|
-
return id;
|
|
44
|
-
},
|
|
45
|
-
|
|
46
|
-
clearTimeout: (args) => {
|
|
47
|
-
requireArgs('timer.clearTimeout', args, 1);
|
|
48
|
-
const id = String(args[0]);
|
|
49
|
-
const handle = _timeouts.get(id);
|
|
50
|
-
if (handle) {
|
|
51
|
-
clearTimeout(handle);
|
|
52
|
-
_timeouts.delete(id);
|
|
53
|
-
return true;
|
|
54
|
-
}
|
|
55
|
-
return false;
|
|
56
|
-
},
|
|
57
|
-
|
|
58
|
-
clearInterval: (args) => {
|
|
59
|
-
requireArgs('timer.clearInterval', args, 1);
|
|
60
|
-
const id = String(args[0]);
|
|
61
|
-
const handle = _intervals.get(id);
|
|
62
|
-
if (handle) {
|
|
63
|
-
clearInterval(handle);
|
|
64
|
-
_intervals.delete(id);
|
|
65
|
-
return true;
|
|
66
|
-
}
|
|
67
|
-
return false;
|
|
68
|
-
},
|
|
69
|
-
|
|
70
|
-
clearAll: () => {
|
|
71
|
-
for (const handle of _timeouts.values()) clearTimeout(handle);
|
|
72
|
-
for (const handle of _intervals.values()) clearInterval(handle);
|
|
73
|
-
_timeouts.clear();
|
|
74
|
-
_intervals.clear();
|
|
75
|
-
return true;
|
|
76
|
-
},
|
|
77
|
-
|
|
78
|
-
active: () => {
|
|
79
|
-
return {
|
|
80
|
-
timeouts: Array.from(_timeouts.keys()),
|
|
81
|
-
intervals: Array.from(_intervals.keys())
|
|
82
|
-
};
|
|
83
|
-
},
|
|
84
|
-
|
|
85
|
-
measure: async (args, callback) => {
|
|
86
|
-
const start = process.hrtime.bigint();
|
|
87
|
-
if (callback) await callback([]);
|
|
88
|
-
const end = process.hrtime.bigint();
|
|
89
|
-
const ms = Number(end - start) / 1_000_000;
|
|
90
|
-
return ms;
|
|
91
|
-
},
|
|
92
|
-
|
|
93
|
-
timestamp: () => Date.now(),
|
|
94
|
-
|
|
95
|
-
now: () => performance.now()
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
export const TimerFunctionMetadata = {
|
|
99
|
-
sleep: {
|
|
100
|
-
description: 'Pause execution for a duration',
|
|
101
|
-
parameters: [{ name: 'milliseconds', dataType: 'number', description: 'Duration in ms (default: 1000)', formInputType: 'number', required: false, defaultValue: 1000 }],
|
|
102
|
-
returnType: 'boolean', returnDescription: 'true when done', example: 'timer.sleep 2000'
|
|
103
|
-
},
|
|
104
|
-
delay: {
|
|
105
|
-
description: 'Alias for sleep',
|
|
106
|
-
parameters: [{ name: 'milliseconds', dataType: 'number', description: 'Duration in ms', formInputType: 'number', required: false, defaultValue: 1000 }],
|
|
107
|
-
returnType: 'boolean', returnDescription: 'true when done', example: 'timer.delay 500'
|
|
108
|
-
},
|
|
109
|
-
setTimeout: {
|
|
110
|
-
description: 'Execute callback after a delay',
|
|
111
|
-
parameters: [{ name: 'milliseconds', dataType: 'number', description: 'Delay in ms', formInputType: 'number', required: true }],
|
|
112
|
-
returnType: 'string', returnDescription: 'Timeout handle ID', example: 'timer.setTimeout 1000'
|
|
113
|
-
},
|
|
114
|
-
setInterval: {
|
|
115
|
-
description: 'Execute callback repeatedly at an interval',
|
|
116
|
-
parameters: [{ name: 'milliseconds', dataType: 'number', description: 'Interval in ms', formInputType: 'number', required: true }],
|
|
117
|
-
returnType: 'string', returnDescription: 'Interval handle ID', example: 'timer.setInterval 5000'
|
|
118
|
-
},
|
|
119
|
-
clearTimeout: {
|
|
120
|
-
description: 'Cancel a pending timeout',
|
|
121
|
-
parameters: [{ name: 'id', dataType: 'string', description: 'Timeout handle ID', formInputType: 'text', required: true }],
|
|
122
|
-
returnType: 'boolean', returnDescription: 'true if cleared', example: 'timer.clearTimeout $id'
|
|
123
|
-
},
|
|
124
|
-
clearInterval: {
|
|
125
|
-
description: 'Cancel a repeating interval',
|
|
126
|
-
parameters: [{ name: 'id', dataType: 'string', description: 'Interval handle ID', formInputType: 'text', required: true }],
|
|
127
|
-
returnType: 'boolean', returnDescription: 'true if cleared', example: 'timer.clearInterval $id'
|
|
128
|
-
},
|
|
129
|
-
clearAll: {
|
|
130
|
-
description: 'Cancel all active timeouts and intervals',
|
|
131
|
-
parameters: [],
|
|
132
|
-
returnType: 'boolean', returnDescription: 'true', example: 'timer.clearAll'
|
|
133
|
-
},
|
|
134
|
-
active: {
|
|
135
|
-
description: 'List all active timers',
|
|
136
|
-
parameters: [],
|
|
137
|
-
returnType: 'object', returnDescription: 'Object with timeouts and intervals arrays', example: 'timer.active'
|
|
138
|
-
},
|
|
139
|
-
measure: {
|
|
140
|
-
description: 'Measure execution time of a callback in milliseconds',
|
|
141
|
-
parameters: [],
|
|
142
|
-
returnType: 'number', returnDescription: 'Execution time in ms', example: 'timer.measure'
|
|
143
|
-
},
|
|
144
|
-
timestamp: {
|
|
145
|
-
description: 'Get current Unix timestamp in milliseconds',
|
|
146
|
-
parameters: [],
|
|
147
|
-
returnType: 'number', returnDescription: 'Unix timestamp (ms)', example: 'timer.timestamp'
|
|
148
|
-
},
|
|
149
|
-
now: {
|
|
150
|
-
description: 'Get high-resolution monotonic time (performance.now)',
|
|
151
|
-
parameters: [],
|
|
152
|
-
returnType: 'number', returnDescription: 'Time in ms', example: 'timer.now'
|
|
153
|
-
}
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
export const TimerModuleMetadata = {
|
|
157
|
-
description: 'Timer operations: sleep, delay, setTimeout, setInterval, measure, and timestamp',
|
|
158
|
-
methods: Object.keys(TimerFunctions)
|
|
159
|
-
};
|
|
160
|
-
|
|
161
|
-
export default {
|
|
162
|
-
name: 'timer',
|
|
163
|
-
functions: TimerFunctions,
|
|
164
|
-
functionMetadata: TimerFunctionMetadata,
|
|
165
|
-
moduleMetadata: TimerModuleMetadata,
|
|
166
|
-
global: false
|
|
167
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* Native timer module for RobinPath.
|
|
3
|
+
* Sleep, delay, and interval utilities.
|
|
4
|
+
*/
|
|
5
|
+
import { toNum, requireArgs } from './_helpers.js';
|
|
6
|
+
|
|
7
|
+
const _intervals = new Map();
|
|
8
|
+
const _timeouts = new Map();
|
|
9
|
+
let _nextId = 1;
|
|
10
|
+
|
|
11
|
+
export const TimerFunctions = {
|
|
12
|
+
|
|
13
|
+
sleep: (args) => {
|
|
14
|
+
const ms = toNum(args[0], 1000);
|
|
15
|
+
return new Promise(resolve => setTimeout(() => resolve(true), ms));
|
|
16
|
+
},
|
|
17
|
+
|
|
18
|
+
delay: (args) => {
|
|
19
|
+
const ms = toNum(args[0], 1000);
|
|
20
|
+
return new Promise(resolve => setTimeout(() => resolve(true), ms));
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
setTimeout: (args, callback) => {
|
|
24
|
+
requireArgs('timer.setTimeout', args, 1);
|
|
25
|
+
const ms = toNum(args[0], 1000);
|
|
26
|
+
const id = `timeout_${_nextId++}`;
|
|
27
|
+
const handle = setTimeout(() => {
|
|
28
|
+
_timeouts.delete(id);
|
|
29
|
+
if (callback) callback([id]);
|
|
30
|
+
}, ms);
|
|
31
|
+
_timeouts.set(id, handle);
|
|
32
|
+
return id;
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
setInterval: (args, callback) => {
|
|
36
|
+
requireArgs('timer.setInterval', args, 1);
|
|
37
|
+
const ms = toNum(args[0], 1000);
|
|
38
|
+
const id = `interval_${_nextId++}`;
|
|
39
|
+
const handle = setInterval(() => {
|
|
40
|
+
if (callback) callback([id]);
|
|
41
|
+
}, ms);
|
|
42
|
+
_intervals.set(id, handle);
|
|
43
|
+
return id;
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
clearTimeout: (args) => {
|
|
47
|
+
requireArgs('timer.clearTimeout', args, 1);
|
|
48
|
+
const id = String(args[0]);
|
|
49
|
+
const handle = _timeouts.get(id);
|
|
50
|
+
if (handle) {
|
|
51
|
+
clearTimeout(handle);
|
|
52
|
+
_timeouts.delete(id);
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
return false;
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
clearInterval: (args) => {
|
|
59
|
+
requireArgs('timer.clearInterval', args, 1);
|
|
60
|
+
const id = String(args[0]);
|
|
61
|
+
const handle = _intervals.get(id);
|
|
62
|
+
if (handle) {
|
|
63
|
+
clearInterval(handle);
|
|
64
|
+
_intervals.delete(id);
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
return false;
|
|
68
|
+
},
|
|
69
|
+
|
|
70
|
+
clearAll: () => {
|
|
71
|
+
for (const handle of _timeouts.values()) clearTimeout(handle);
|
|
72
|
+
for (const handle of _intervals.values()) clearInterval(handle);
|
|
73
|
+
_timeouts.clear();
|
|
74
|
+
_intervals.clear();
|
|
75
|
+
return true;
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
active: () => {
|
|
79
|
+
return {
|
|
80
|
+
timeouts: Array.from(_timeouts.keys()),
|
|
81
|
+
intervals: Array.from(_intervals.keys())
|
|
82
|
+
};
|
|
83
|
+
},
|
|
84
|
+
|
|
85
|
+
measure: async (args, callback) => {
|
|
86
|
+
const start = process.hrtime.bigint();
|
|
87
|
+
if (callback) await callback([]);
|
|
88
|
+
const end = process.hrtime.bigint();
|
|
89
|
+
const ms = Number(end - start) / 1_000_000;
|
|
90
|
+
return ms;
|
|
91
|
+
},
|
|
92
|
+
|
|
93
|
+
timestamp: () => Date.now(),
|
|
94
|
+
|
|
95
|
+
now: () => performance.now()
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
export const TimerFunctionMetadata = {
|
|
99
|
+
sleep: {
|
|
100
|
+
description: 'Pause execution for a duration',
|
|
101
|
+
parameters: [{ name: 'milliseconds', dataType: 'number', description: 'Duration in ms (default: 1000)', formInputType: 'number', required: false, defaultValue: 1000 }],
|
|
102
|
+
returnType: 'boolean', returnDescription: 'true when done', example: 'timer.sleep 2000'
|
|
103
|
+
},
|
|
104
|
+
delay: {
|
|
105
|
+
description: 'Alias for sleep',
|
|
106
|
+
parameters: [{ name: 'milliseconds', dataType: 'number', description: 'Duration in ms', formInputType: 'number', required: false, defaultValue: 1000 }],
|
|
107
|
+
returnType: 'boolean', returnDescription: 'true when done', example: 'timer.delay 500'
|
|
108
|
+
},
|
|
109
|
+
setTimeout: {
|
|
110
|
+
description: 'Execute callback after a delay',
|
|
111
|
+
parameters: [{ name: 'milliseconds', dataType: 'number', description: 'Delay in ms', formInputType: 'number', required: true }],
|
|
112
|
+
returnType: 'string', returnDescription: 'Timeout handle ID', example: 'timer.setTimeout 1000'
|
|
113
|
+
},
|
|
114
|
+
setInterval: {
|
|
115
|
+
description: 'Execute callback repeatedly at an interval',
|
|
116
|
+
parameters: [{ name: 'milliseconds', dataType: 'number', description: 'Interval in ms', formInputType: 'number', required: true }],
|
|
117
|
+
returnType: 'string', returnDescription: 'Interval handle ID', example: 'timer.setInterval 5000'
|
|
118
|
+
},
|
|
119
|
+
clearTimeout: {
|
|
120
|
+
description: 'Cancel a pending timeout',
|
|
121
|
+
parameters: [{ name: 'id', dataType: 'string', description: 'Timeout handle ID', formInputType: 'text', required: true }],
|
|
122
|
+
returnType: 'boolean', returnDescription: 'true if cleared', example: 'timer.clearTimeout $id'
|
|
123
|
+
},
|
|
124
|
+
clearInterval: {
|
|
125
|
+
description: 'Cancel a repeating interval',
|
|
126
|
+
parameters: [{ name: 'id', dataType: 'string', description: 'Interval handle ID', formInputType: 'text', required: true }],
|
|
127
|
+
returnType: 'boolean', returnDescription: 'true if cleared', example: 'timer.clearInterval $id'
|
|
128
|
+
},
|
|
129
|
+
clearAll: {
|
|
130
|
+
description: 'Cancel all active timeouts and intervals',
|
|
131
|
+
parameters: [],
|
|
132
|
+
returnType: 'boolean', returnDescription: 'true', example: 'timer.clearAll'
|
|
133
|
+
},
|
|
134
|
+
active: {
|
|
135
|
+
description: 'List all active timers',
|
|
136
|
+
parameters: [],
|
|
137
|
+
returnType: 'object', returnDescription: 'Object with timeouts and intervals arrays', example: 'timer.active'
|
|
138
|
+
},
|
|
139
|
+
measure: {
|
|
140
|
+
description: 'Measure execution time of a callback in milliseconds',
|
|
141
|
+
parameters: [],
|
|
142
|
+
returnType: 'number', returnDescription: 'Execution time in ms', example: 'timer.measure'
|
|
143
|
+
},
|
|
144
|
+
timestamp: {
|
|
145
|
+
description: 'Get current Unix timestamp in milliseconds',
|
|
146
|
+
parameters: [],
|
|
147
|
+
returnType: 'number', returnDescription: 'Unix timestamp (ms)', example: 'timer.timestamp'
|
|
148
|
+
},
|
|
149
|
+
now: {
|
|
150
|
+
description: 'Get high-resolution monotonic time (performance.now)',
|
|
151
|
+
parameters: [],
|
|
152
|
+
returnType: 'number', returnDescription: 'Time in ms', example: 'timer.now'
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
export const TimerModuleMetadata = {
|
|
157
|
+
description: 'Timer operations: sleep, delay, setTimeout, setInterval, measure, and timestamp',
|
|
158
|
+
methods: Object.keys(TimerFunctions)
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
export default {
|
|
162
|
+
name: 'timer',
|
|
163
|
+
functions: TimerFunctions,
|
|
164
|
+
functionMetadata: TimerFunctionMetadata,
|
|
165
|
+
moduleMetadata: TimerModuleMetadata,
|
|
166
|
+
global: false
|
|
167
|
+
};
|