@robinpath/cli 1.73.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.
@@ -0,0 +1,268 @@
1
+ /**
2
+ * Native HTTP module for RobinPath.
3
+ * HTTP client (fetch-based) and server (Node.js http).
4
+ */
5
+ import { createServer } from 'node:http';
6
+ import { toStr, toNum, requireArgs } from './_helpers.js';
7
+
8
+ const _servers = new Map();
9
+ let _nextId = 1;
10
+
11
+ async function doFetch(url, method, bodyArg, headersArg) {
12
+ const opts = { method };
13
+
14
+ // Headers
15
+ const headers = {};
16
+ if (headersArg && typeof headersArg === 'object') {
17
+ for (const [k, v] of Object.entries(headersArg)) headers[k] = toStr(v);
18
+ }
19
+
20
+ // Body
21
+ if (bodyArg != null && method !== 'GET' && method !== 'HEAD') {
22
+ if (typeof bodyArg === 'object') {
23
+ headers['Content-Type'] = headers['Content-Type'] || 'application/json';
24
+ opts.body = JSON.stringify(bodyArg);
25
+ } else {
26
+ opts.body = toStr(bodyArg);
27
+ }
28
+ }
29
+
30
+ opts.headers = headers;
31
+
32
+ const res = await fetch(url, opts);
33
+ const contentType = res.headers.get('content-type') || '';
34
+ let data;
35
+ if (contentType.includes('application/json')) {
36
+ try { data = await res.json(); } catch { data = await res.text(); }
37
+ } else {
38
+ data = await res.text();
39
+ }
40
+
41
+ return {
42
+ status: res.status,
43
+ statusText: res.statusText,
44
+ headers: Object.fromEntries(res.headers.entries()),
45
+ data,
46
+ ok: res.ok,
47
+ url: res.url
48
+ };
49
+ }
50
+
51
+ export const HttpFunctions = {
52
+
53
+ get: async (args) => {
54
+ requireArgs('http.get', args, 1);
55
+ return doFetch(toStr(args[0]), 'GET', null, args[1]);
56
+ },
57
+
58
+ post: async (args) => {
59
+ requireArgs('http.post', args, 1);
60
+ return doFetch(toStr(args[0]), 'POST', args[1], args[2]);
61
+ },
62
+
63
+ put: async (args) => {
64
+ requireArgs('http.put', args, 1);
65
+ return doFetch(toStr(args[0]), 'PUT', args[1], args[2]);
66
+ },
67
+
68
+ patch: async (args) => {
69
+ requireArgs('http.patch', args, 1);
70
+ return doFetch(toStr(args[0]), 'PATCH', args[1], args[2]);
71
+ },
72
+
73
+ delete: async (args) => {
74
+ requireArgs('http.delete', args, 1);
75
+ return doFetch(toStr(args[0]), 'DELETE', args[1], args[2]);
76
+ },
77
+
78
+ head: async (args) => {
79
+ requireArgs('http.head', args, 1);
80
+ const res = await fetch(toStr(args[0]), { method: 'HEAD' });
81
+ return {
82
+ status: res.status,
83
+ statusText: res.statusText,
84
+ headers: Object.fromEntries(res.headers.entries()),
85
+ ok: res.ok
86
+ };
87
+ },
88
+
89
+ request: async (args) => {
90
+ requireArgs('http.request', args, 1);
91
+ const opts = args[0];
92
+ if (typeof opts !== 'object' || opts === null) {
93
+ throw new Error('http.request requires an options object: {url, method, body?, headers?}');
94
+ }
95
+ const url = toStr(opts.url || opts.href);
96
+ const method = toStr(opts.method || 'GET').toUpperCase();
97
+ return doFetch(url, method, opts.body || opts.data, opts.headers);
98
+ },
99
+
100
+ serve: (args, callback) => {
101
+ requireArgs('http.serve', args, 1);
102
+ const port = toNum(args[0], 3000);
103
+ const host = toStr(args[1], '0.0.0.0');
104
+ const id = `http_${_nextId++}`;
105
+
106
+ const server = createServer(async (req, res) => {
107
+ const body = await new Promise((resolve) => {
108
+ let data = '';
109
+ req.on('data', (chunk) => { data += chunk; });
110
+ req.on('end', () => resolve(data));
111
+ });
112
+
113
+ let parsedBody = body;
114
+ try { parsedBody = JSON.parse(body); } catch { /* raw string */ }
115
+
116
+ const request = {
117
+ method: req.method,
118
+ url: req.url,
119
+ headers: req.headers,
120
+ body: parsedBody
121
+ };
122
+
123
+ if (callback) {
124
+ try {
125
+ const result = await callback([request]);
126
+ const statusCode = (result && result.status) ? result.status : 200;
127
+ const respHeaders = (result && result.headers) ? result.headers : { 'Content-Type': 'application/json' };
128
+ const respBody = (result && result.body != null) ? result.body : result;
129
+ res.writeHead(statusCode, respHeaders);
130
+ res.end(typeof respBody === 'object' ? JSON.stringify(respBody) : toStr(respBody));
131
+ } catch (err) {
132
+ res.writeHead(500, { 'Content-Type': 'application/json' });
133
+ res.end(JSON.stringify({ error: err.message }));
134
+ }
135
+ } else {
136
+ res.writeHead(200, { 'Content-Type': 'application/json' });
137
+ res.end(JSON.stringify(request));
138
+ }
139
+ });
140
+
141
+ server.listen(port, host);
142
+ _servers.set(id, server);
143
+ return id;
144
+ },
145
+
146
+ close: (args) => {
147
+ requireArgs('http.close', args, 1);
148
+ const id = toStr(args[0]);
149
+ const server = _servers.get(id);
150
+ if (server) {
151
+ server.close();
152
+ _servers.delete(id);
153
+ return true;
154
+ }
155
+ return false;
156
+ },
157
+
158
+ servers: () => Array.from(_servers.keys()),
159
+
160
+ download: async (args) => {
161
+ requireArgs('http.download', args, 2);
162
+ const url = toStr(args[0]);
163
+ const filePath = toStr(args[1]);
164
+ const res = await fetch(url);
165
+ if (!res.ok) throw new Error(`http.download: ${res.status} ${res.statusText}`);
166
+ const { writeFile } = await import('node:fs/promises');
167
+ const { resolve } = await import('node:path');
168
+ const buffer = Buffer.from(await res.arrayBuffer());
169
+ await writeFile(resolve(filePath), buffer);
170
+ return { size: buffer.length, path: filePath, status: res.status };
171
+ }
172
+ };
173
+
174
+ export const HttpFunctionMetadata = {
175
+ get: {
176
+ description: 'HTTP GET request',
177
+ parameters: [
178
+ { name: 'url', dataType: 'string', description: 'URL to request', formInputType: 'text', required: true },
179
+ { name: 'headers', dataType: 'object', description: 'Request headers', formInputType: 'json', required: false }
180
+ ],
181
+ returnType: 'object', returnDescription: 'Response with status, headers, data', example: 'http.get "https://api.example.com/data"'
182
+ },
183
+ post: {
184
+ description: 'HTTP POST request',
185
+ parameters: [
186
+ { name: 'url', dataType: 'string', description: 'URL', formInputType: 'text', required: true },
187
+ { name: 'body', dataType: 'any', description: 'Request body (object auto-serialized to JSON)', formInputType: 'json', required: false },
188
+ { name: 'headers', dataType: 'object', description: 'Request headers', formInputType: 'json', required: false }
189
+ ],
190
+ returnType: 'object', returnDescription: 'Response with status, headers, data', example: 'http.post "https://api.example.com/data" {"key": "value"}'
191
+ },
192
+ put: {
193
+ description: 'HTTP PUT request',
194
+ parameters: [
195
+ { name: 'url', dataType: 'string', description: 'URL', formInputType: 'text', required: true },
196
+ { name: 'body', dataType: 'any', description: 'Request body', formInputType: 'json', required: false },
197
+ { name: 'headers', dataType: 'object', description: 'Headers', formInputType: 'json', required: false }
198
+ ],
199
+ returnType: 'object', returnDescription: 'Response', example: 'http.put "https://api.example.com/data/1" {"key": "new"}'
200
+ },
201
+ patch: {
202
+ description: 'HTTP PATCH request',
203
+ parameters: [
204
+ { name: 'url', dataType: 'string', description: 'URL', formInputType: 'text', required: true },
205
+ { name: 'body', dataType: 'any', description: 'Request body', formInputType: 'json', required: false },
206
+ { name: 'headers', dataType: 'object', description: 'Headers', formInputType: 'json', required: false }
207
+ ],
208
+ returnType: 'object', returnDescription: 'Response', example: 'http.patch "https://api.example.com/data/1" {"key": "updated"}'
209
+ },
210
+ delete: {
211
+ description: 'HTTP DELETE request',
212
+ parameters: [
213
+ { name: 'url', dataType: 'string', description: 'URL', formInputType: 'text', required: true },
214
+ { name: 'body', dataType: 'any', description: 'Request body', formInputType: 'json', required: false },
215
+ { name: 'headers', dataType: 'object', description: 'Headers', formInputType: 'json', required: false }
216
+ ],
217
+ returnType: 'object', returnDescription: 'Response', example: 'http.delete "https://api.example.com/data/1"'
218
+ },
219
+ head: {
220
+ description: 'HTTP HEAD request (headers only)',
221
+ parameters: [{ name: 'url', dataType: 'string', description: 'URL', formInputType: 'text', required: true }],
222
+ returnType: 'object', returnDescription: 'Response with status and headers', example: 'http.head "https://example.com"'
223
+ },
224
+ request: {
225
+ description: 'Custom HTTP request with full options',
226
+ parameters: [{ name: 'options', dataType: 'object', description: 'Object with url, method, body, headers', formInputType: 'json', required: true }],
227
+ returnType: 'object', returnDescription: 'Response', example: 'http.request {"url": "https://api.example.com", "method": "POST", "body": {"key": 1}}'
228
+ },
229
+ serve: {
230
+ description: 'Start an HTTP server',
231
+ parameters: [
232
+ { name: 'port', dataType: 'number', description: 'Port to listen on (default: 3000)', formInputType: 'number', required: true },
233
+ { name: 'host', dataType: 'string', description: 'Host (default: 0.0.0.0)', formInputType: 'text', required: false, defaultValue: '0.0.0.0' }
234
+ ],
235
+ returnType: 'string', returnDescription: 'Server handle ID', example: 'http.serve 8080'
236
+ },
237
+ close: {
238
+ description: 'Close an HTTP server',
239
+ parameters: [{ name: 'serverId', dataType: 'string', description: 'Server handle ID', formInputType: 'text', required: true }],
240
+ returnType: 'boolean', returnDescription: 'true if closed', example: 'http.close $serverId'
241
+ },
242
+ servers: {
243
+ description: 'List all running HTTP servers',
244
+ parameters: [],
245
+ returnType: 'array', returnDescription: 'Array of server IDs', example: 'http.servers'
246
+ },
247
+ download: {
248
+ description: 'Download a file from a URL',
249
+ parameters: [
250
+ { name: 'url', dataType: 'string', description: 'URL to download', formInputType: 'text', required: true },
251
+ { name: 'path', dataType: 'string', description: 'Local file path to save', formInputType: 'text', required: true }
252
+ ],
253
+ returnType: 'object', returnDescription: 'Object with size, path, status', example: 'http.download "https://example.com/file.zip" "file.zip"'
254
+ }
255
+ };
256
+
257
+ export const HttpModuleMetadata = {
258
+ description: 'HTTP client and server: GET, POST, PUT, DELETE, serve, download',
259
+ methods: Object.keys(HttpFunctions)
260
+ };
261
+
262
+ export default {
263
+ name: 'http',
264
+ functions: HttpFunctions,
265
+ functionMetadata: HttpFunctionMetadata,
266
+ moduleMetadata: HttpModuleMetadata,
267
+ global: false
268
+ };
@@ -0,0 +1,76 @@
1
+ /**
2
+ * RobinPath Native Modules — Barrel Export
3
+ * All modules listed here are bundled into the CLI binary.
4
+ */
5
+
6
+ // Phase 1: Core System
7
+ import FileModule from './file.js';
8
+ import PathModule from './path.js';
9
+ import ProcessModule from './process.js';
10
+ import OsModule from './os.js';
11
+
12
+ // Phase 2: Data & Security
13
+ import CryptoModule from './crypto.js';
14
+ import BufferModule from './buffer.js';
15
+ import UrlModule from './url.js';
16
+ import ChildModule from './child.js';
17
+ import TimerModule from './timer.js';
18
+
19
+ // Phase 3a: Networking & I/O
20
+ import HttpModule from './http.js';
21
+ import NetModule from './net.js';
22
+ import DnsModule from './dns.js';
23
+ import EventsModule from './events.js';
24
+ import ZlibModule from './zlib.js';
25
+
26
+ // Phase 3b: Streams, TLS & Utilities
27
+ import StreamModule from './stream.js';
28
+ import TlsModule from './tls.js';
29
+ import UtilModule from './util.js';
30
+ import AssertModule from './assert.js';
31
+ import StringDecoderModule from './string_decoder.js';
32
+ import TtyModule from './tty.js';
33
+
34
+ // Phase 4: Document & Format Generation (uncomment as implemented)
35
+ // import ArchiveModule from './archive.js';
36
+ // import EmailModule from './email.js';
37
+ // import BarcodeModule from './barcode.js';
38
+ // import PdfModule from './pdf.js';
39
+ // import ExcelModule from './excel.js';
40
+
41
+ export const nativeModules = [
42
+ // Phase 1: Core System
43
+ FileModule,
44
+ PathModule,
45
+ ProcessModule,
46
+ OsModule,
47
+
48
+ // Phase 2: Data & Security
49
+ CryptoModule,
50
+ BufferModule,
51
+ UrlModule,
52
+ ChildModule,
53
+ TimerModule,
54
+
55
+ // Phase 3a: Networking & I/O
56
+ HttpModule,
57
+ NetModule,
58
+ DnsModule,
59
+ EventsModule,
60
+ ZlibModule,
61
+
62
+ // Phase 3b: Streams, TLS & Utilities
63
+ StreamModule,
64
+ TlsModule,
65
+ UtilModule,
66
+ AssertModule,
67
+ StringDecoderModule,
68
+ TtyModule,
69
+
70
+ // Phase 4
71
+ // ArchiveModule,
72
+ // EmailModule,
73
+ // BarcodeModule,
74
+ // PdfModule,
75
+ // ExcelModule,
76
+ ];
package/modules/net.js ADDED
@@ -0,0 +1,189 @@
1
+ /**
2
+ * Native net module for RobinPath.
3
+ * TCP client/server and socket operations.
4
+ */
5
+ import { createServer, createConnection, isIP, isIPv4, isIPv6 } from 'node:net';
6
+ import { toStr, toNum, requireArgs } from './_helpers.js';
7
+
8
+ const _servers = new Map();
9
+ const _sockets = new Map();
10
+ let _nextId = 1;
11
+
12
+ export const NetFunctions = {
13
+
14
+ connect: (args) => {
15
+ requireArgs('net.connect', args, 2);
16
+ const host = toStr(args[0]);
17
+ const port = toNum(args[1]);
18
+ const id = `sock_${_nextId++}`;
19
+
20
+ return new Promise((resolve, reject) => {
21
+ const socket = createConnection({ host, port }, () => {
22
+ _sockets.set(id, { socket, data: '' });
23
+ socket.on('data', (chunk) => {
24
+ const entry = _sockets.get(id);
25
+ if (entry) entry.data += chunk.toString();
26
+ });
27
+ socket.on('end', () => { _sockets.delete(id); });
28
+ socket.on('error', () => { _sockets.delete(id); });
29
+ resolve(id);
30
+ });
31
+ socket.on('error', (err) => reject(new Error(`net.connect: ${err.message}`)));
32
+ });
33
+ },
34
+
35
+ send: (args) => {
36
+ requireArgs('net.send', args, 2);
37
+ const id = toStr(args[0]);
38
+ const data = toStr(args[1]);
39
+ const entry = _sockets.get(id);
40
+ if (!entry) throw new Error(`net.send: socket ${id} not found`);
41
+ return new Promise((resolve, reject) => {
42
+ entry.socket.write(data, (err) => {
43
+ if (err) reject(err);
44
+ else resolve(true);
45
+ });
46
+ });
47
+ },
48
+
49
+ read: (args) => {
50
+ requireArgs('net.read', args, 1);
51
+ const id = toStr(args[0]);
52
+ const entry = _sockets.get(id);
53
+ if (!entry) throw new Error(`net.read: socket ${id} not found`);
54
+ const data = entry.data;
55
+ entry.data = '';
56
+ return data;
57
+ },
58
+
59
+ close: (args) => {
60
+ requireArgs('net.close', args, 1);
61
+ const id = toStr(args[0]);
62
+ const entry = _sockets.get(id);
63
+ if (entry) {
64
+ entry.socket.destroy();
65
+ _sockets.delete(id);
66
+ return true;
67
+ }
68
+ const server = _servers.get(id);
69
+ if (server) {
70
+ server.close();
71
+ _servers.delete(id);
72
+ return true;
73
+ }
74
+ return false;
75
+ },
76
+
77
+ createServer: (args, callback) => {
78
+ requireArgs('net.createServer', args, 1);
79
+ const port = toNum(args[0]);
80
+ const host = toStr(args[1], '0.0.0.0');
81
+ const serverId = `tcp_${_nextId++}`;
82
+
83
+ const server = createServer((socket) => {
84
+ const connId = `conn_${_nextId++}`;
85
+ _sockets.set(connId, { socket, data: '' });
86
+ socket.on('data', (chunk) => {
87
+ const entry = _sockets.get(connId);
88
+ if (entry) entry.data += chunk.toString();
89
+ if (callback) callback([connId, chunk.toString()]);
90
+ });
91
+ socket.on('end', () => { _sockets.delete(connId); });
92
+ socket.on('error', () => { _sockets.delete(connId); });
93
+ });
94
+
95
+ server.listen(port, host);
96
+ _servers.set(serverId, server);
97
+ return serverId;
98
+ },
99
+
100
+ isIP: (args) => {
101
+ requireArgs('net.isIP', args, 1);
102
+ return isIP(toStr(args[0]));
103
+ },
104
+
105
+ isIPv4: (args) => {
106
+ requireArgs('net.isIPv4', args, 1);
107
+ return isIPv4(toStr(args[0]));
108
+ },
109
+
110
+ isIPv6: (args) => {
111
+ requireArgs('net.isIPv6', args, 1);
112
+ return isIPv6(toStr(args[0]));
113
+ },
114
+
115
+ active: () => ({
116
+ servers: Array.from(_servers.keys()),
117
+ sockets: Array.from(_sockets.keys())
118
+ })
119
+ };
120
+
121
+ export const NetFunctionMetadata = {
122
+ connect: {
123
+ description: 'Connect to a TCP server',
124
+ parameters: [
125
+ { name: 'host', dataType: 'string', description: 'Host to connect to', formInputType: 'text', required: true },
126
+ { name: 'port', dataType: 'number', description: 'Port number', formInputType: 'number', required: true }
127
+ ],
128
+ returnType: 'string', returnDescription: 'Socket handle ID', example: 'net.connect "localhost" 8080'
129
+ },
130
+ send: {
131
+ description: 'Send data through a socket',
132
+ parameters: [
133
+ { name: 'socketId', dataType: 'string', description: 'Socket handle ID', formInputType: 'text', required: true },
134
+ { name: 'data', dataType: 'string', description: 'Data to send', formInputType: 'text', required: true }
135
+ ],
136
+ returnType: 'boolean', returnDescription: 'true on success', example: 'net.send $sock "hello"'
137
+ },
138
+ read: {
139
+ description: 'Read buffered data from a socket',
140
+ parameters: [{ name: 'socketId', dataType: 'string', description: 'Socket handle ID', formInputType: 'text', required: true }],
141
+ returnType: 'string', returnDescription: 'Buffered data', example: 'net.read $sock'
142
+ },
143
+ close: {
144
+ description: 'Close a socket or server',
145
+ parameters: [{ name: 'id', dataType: 'string', description: 'Socket or server handle ID', formInputType: 'text', required: true }],
146
+ returnType: 'boolean', returnDescription: 'true if closed', example: 'net.close $sock'
147
+ },
148
+ createServer: {
149
+ description: 'Create a TCP server',
150
+ parameters: [
151
+ { name: 'port', dataType: 'number', description: 'Port to listen on', formInputType: 'number', required: true },
152
+ { name: 'host', dataType: 'string', description: 'Host (default: 0.0.0.0)', formInputType: 'text', required: false, defaultValue: '0.0.0.0' }
153
+ ],
154
+ returnType: 'string', returnDescription: 'Server handle ID', example: 'net.createServer 9090'
155
+ },
156
+ isIP: {
157
+ description: 'Check if string is a valid IP (returns 0, 4, or 6)',
158
+ parameters: [{ name: 'address', dataType: 'string', description: 'Address to check', formInputType: 'text', required: true }],
159
+ returnType: 'number', returnDescription: '0 (invalid), 4 (IPv4), or 6 (IPv6)', example: 'net.isIP "192.168.1.1"'
160
+ },
161
+ isIPv4: {
162
+ description: 'Check if string is a valid IPv4 address',
163
+ parameters: [{ name: 'address', dataType: 'string', description: 'Address', formInputType: 'text', required: true }],
164
+ returnType: 'boolean', returnDescription: 'true if IPv4', example: 'net.isIPv4 "192.168.1.1"'
165
+ },
166
+ isIPv6: {
167
+ description: 'Check if string is a valid IPv6 address',
168
+ parameters: [{ name: 'address', dataType: 'string', description: 'Address', formInputType: 'text', required: true }],
169
+ returnType: 'boolean', returnDescription: 'true if IPv6', example: 'net.isIPv6 "::1"'
170
+ },
171
+ active: {
172
+ description: 'List active servers and sockets',
173
+ parameters: [],
174
+ returnType: 'object', returnDescription: 'Object with servers and sockets arrays', example: 'net.active'
175
+ }
176
+ };
177
+
178
+ export const NetModuleMetadata = {
179
+ description: 'TCP networking: connect, send, read, createServer, and IP utilities',
180
+ methods: Object.keys(NetFunctions)
181
+ };
182
+
183
+ export default {
184
+ name: 'net',
185
+ functions: NetFunctions,
186
+ functionMetadata: NetFunctionMetadata,
187
+ moduleMetadata: NetModuleMetadata,
188
+ global: false
189
+ };