@naanlang/naan 1.5.0 → 1.6.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.
Files changed (44) hide show
  1. package/LICENSE.md +2 -2
  2. package/README.md +2 -2
  3. package/bin/index.js +7 -3
  4. package/dist/env_web.js +162 -20
  5. package/dist/naan.min.js +5 -5
  6. package/frameworks/browser/https_request.nlg +2 -2
  7. package/frameworks/browser/sworker.js +28 -26
  8. package/frameworks/browser/terminals.nlg +329 -693
  9. package/frameworks/browser/worker.nlg +255 -0
  10. package/frameworks/browser/workers.nlg +5 -5
  11. package/frameworks/browser/ws_client.nlg +15 -10
  12. package/frameworks/client/apiclient.nlg +181 -9
  13. package/frameworks/client/relaycon.nlg +87 -161
  14. package/frameworks/common/events.nlg +101 -0
  15. package/frameworks/common/repltools.nlg +80 -1
  16. package/frameworks/node/apiserver.nlg +39 -246
  17. package/frameworks/node/filesystem.nlg +31 -6
  18. package/frameworks/node/http_request.nlg +34 -13
  19. package/frameworks/node/https_request.nlg +8 -3
  20. package/frameworks/node/node.nlg +2 -0
  21. package/frameworks/node/pty.nlg +346 -0
  22. package/frameworks/node/worker.nlg +409 -539
  23. package/frameworks/node/ws_client.nlg +2 -2
  24. package/frameworks/project/build.nlg +1 -1
  25. package/frameworks/project/projects.nlg +1 -1
  26. package/frameworks/running/debugnub.nlg +17 -4
  27. package/frameworks/running/executors.nlg +27 -274
  28. package/frameworks/running/instances.nlg +393 -0
  29. package/frameworks/running/remote_req.nlg +143 -0
  30. package/frameworks/running/remote_res.nlg +89 -0
  31. package/frameworks/running/taskexec.nlg +2 -2
  32. package/frameworks/service/imp.nlg +736 -0
  33. package/frameworks/service/model.nlg +71 -0
  34. package/frameworks/service/nubnode.nlg +105 -0
  35. package/frameworks/service/nubweb.nlg +106 -0
  36. package/frameworks/service/service.nlg +28 -0
  37. package/lib/browser/env_web.js +169 -27
  38. package/lib/browser/env_webworker.js +91 -4
  39. package/lib/core/naanlib.js +5 -5
  40. package/lib/env_node.js +184 -8
  41. package/lib/env_nodeworker.js +74 -1
  42. package/package.json +1 -1
  43. package/plugins/serviceNexara/speechrec.nlg +10 -6
  44. package/test/test_02_context.nlg +24 -1
package/lib/env_node.js CHANGED
@@ -6,7 +6,7 @@
6
6
  *
7
7
  * column positioning: // // !
8
8
  *
9
- * Copyright (c) 2017-2024 by Richard C. Zulch
9
+ * Copyright (c) 2017-2026 by Richard C. Zulch
10
10
  *
11
11
  */
12
12
 
@@ -49,6 +49,7 @@ exports.NaanNodeREPL = function NaanNodeREPL(options) {
49
49
  if (typeof(options) != 'object')
50
50
  options = { };
51
51
  var repl = require('repl');
52
+ const { Transform } = require('node:stream');
52
53
  var replServer;
53
54
  var process = require('process');
54
55
  var Naan = require('./core/naanlib');
@@ -62,8 +63,16 @@ exports.NaanNodeREPL = function NaanNodeREPL(options) {
62
63
  if (outEnable && process.env["RUNKIT_ENDPOINT_URL"])
63
64
  useConsoleOut = true; // use console.log for output in RunKit
64
65
  var dontSaveUntilWorking; // true when state loaded, must be reset to save state again
66
+ var dontSave; // true to avoid saving, i.e. in debugger
65
67
  var prefs = { };
66
68
 
69
+ // kpMode
70
+ // 0: normal (readline & echo)
71
+ // 1: readline, but echo CR only
72
+ // 2: readline, but don't echo
73
+ // 3: raw, no echo or readline, but key-events
74
+ var kpMode = 0;
75
+
67
76
  //==========================================================================
68
77
  // API
69
78
  //--------------------------------------------------------------------------
@@ -112,7 +121,27 @@ exports.NaanNodeREPL = function NaanNodeREPL(options) {
112
121
 
113
122
  if (process.stdin.isTTY)
114
123
  { // must have TTY for local REPL
115
- replServer = repl.start({ prompt: '', eval: myEval});
124
+ const replOutputStream = new Transform({
125
+ writableObjectMode: true,
126
+ readableObjectMode: true,
127
+ transform(chunk, encoding, callback) {
128
+ if (kpMode > 0) {
129
+ const str = chunk.toString();
130
+ if (kpMode != 1 || str != "\r\n")
131
+ return callback(); // output goes to the bit bucket
132
+ }
133
+ return callback(null, chunk); // normal output
134
+ }
135
+ });
136
+ replOutputStream.pipe(process.stdout); // output filter for raw and no-echo
137
+
138
+ replServer = repl.start({
139
+ prompt: '',
140
+ eval: myEval,
141
+ input: process.stdin,
142
+ output: replOutputStream,
143
+ terminal: true
144
+ });
116
145
 
117
146
  replServer.on('line', function(cmd) { // process raw command line
118
147
  if (options.replDisable)
@@ -122,7 +151,21 @@ exports.NaanNodeREPL = function NaanNodeREPL(options) {
122
151
  naanlib.textLine(cmd + "\n");
123
152
  }
124
153
  });
125
-
154
+
155
+ const origListenersData = process.stdin.listeners('data');
156
+ process.stdin.removeAllListeners('data');
157
+
158
+ process.stdin.on('data', (data) => {
159
+ if (kpMode == 3) {
160
+ servSelf.ioctl_event("raw-key", { // raw mode
161
+ key: data.toString(),
162
+ origin: 0 // assume keystroke
163
+ });
164
+ return;
165
+ }
166
+ origListenersData.forEach(listener => listener(data));
167
+ });
168
+
126
169
  //
127
170
  // Hook the ^C interrupt
128
171
  //
@@ -131,8 +174,10 @@ exports.NaanNodeREPL = function NaanNodeREPL(options) {
131
174
  if (options.replDisable) {
132
175
  console.log("^C");
133
176
  process.exit();
134
- } else
177
+ } else {
178
+ setKpMode(0);
135
179
  naanlib.escape();
180
+ }
136
181
  };
137
182
 
138
183
  //
@@ -142,6 +187,17 @@ exports.NaanNodeREPL = function NaanNodeREPL(options) {
142
187
  replServer.on('exit', function() {
143
188
  process.exit();
144
189
  });
190
+
191
+ //
192
+ // Process resize notifications
193
+ //
194
+
195
+ process.stdout.on('resize', () => {
196
+ servSelf.ioctl_event("term-resize", {
197
+ cols: process.stdout.columns,
198
+ rows: process.stdout.rows
199
+ });
200
+ });
145
201
  }
146
202
 
147
203
  //
@@ -177,6 +233,43 @@ exports.NaanNodeREPL = function NaanNodeREPL(options) {
177
233
  textToRemoteTerm(text);
178
234
  }
179
235
  });
236
+
237
+ //
238
+ // setKpMode
239
+ //
240
+
241
+ function setKpMode(mode) {
242
+ if (!replServer)
243
+ return;
244
+ kpMode = mode;
245
+ process.stdin.resume();
246
+ }
247
+
248
+ //
249
+ // repl_keystroke
250
+ //
251
+
252
+ function repl_keystroke(str) {
253
+ if (replServer)
254
+ replServer.write(str, null);
255
+ }
256
+
257
+ //
258
+ // repl_ioctl
259
+ //
260
+
261
+ this.repl_ioctl = function repl_ioctl(op, arg) {
262
+ if (op == "sane")
263
+ setKpMode(0);
264
+ else if (op == "raw")
265
+ setKpMode(arg ? 3 : 0);
266
+ else if (op == "keystroke")
267
+ repl_keystroke(arg);
268
+ else if (op == "echo") {
269
+ if (arg >= 0 && arg <= 2)
270
+ setKpMode(arg);
271
+ }
272
+ };
180
273
 
181
274
 
182
275
  //==========================================================================
@@ -196,7 +289,7 @@ exports.NaanNodeREPL = function NaanNodeREPL(options) {
196
289
  //
197
290
  // Detach
198
291
  //
199
- // Detach the terminal from the interpreter.
292
+ // Detach the terminal from our interpreter.
200
293
  //
201
294
 
202
295
  this.Detach = function(tops) {
@@ -226,7 +319,8 @@ exports.NaanNodeREPL = function NaanNodeREPL(options) {
226
319
  if (termops)
227
320
  {
228
321
  setTimeout(function () {
229
- termops.messageout(data);
322
+ if (termops)
323
+ termops.messageout(data);
230
324
  }, 1);
231
325
  }
232
326
  return (data);
@@ -242,7 +336,8 @@ exports.NaanNodeREPL = function NaanNodeREPL(options) {
242
336
  if (termops)
243
337
  {
244
338
  setTimeout(function () {
245
- termops.debugout(data);
339
+ if (termops)
340
+ termops.debugout(data);
246
341
  }, 1);
247
342
  }
248
343
  return (data);
@@ -277,6 +372,10 @@ exports.NaanNodeREPL = function NaanNodeREPL(options) {
277
372
  naanlib.textLine(msg.text); // keyboard text from terminal
278
373
  }, 1);
279
374
  }
375
+ else if (msg.id == "typed")
376
+ {
377
+ echoTyped(msg.text);
378
+ }
280
379
  else if (msg.id == "targetin")
281
380
  {
282
381
  setTimeout(function () {
@@ -291,6 +390,8 @@ exports.NaanNodeREPL = function NaanNodeREPL(options) {
291
390
  debugListener(msg.data); // received a debugger message
292
391
  }, 1);
293
392
  }
393
+ else if (msg.id == "ioctl-event")
394
+ this.ioctl_event(msg.msg, msg.arg);
294
395
  };
295
396
 
296
397
  // textToRemoteTerm
@@ -301,7 +402,8 @@ exports.NaanNodeREPL = function NaanNodeREPL(options) {
301
402
  if (termops)
302
403
  {
303
404
  setTimeout(function () {
304
- termops.textout(text);
405
+ if (termops)
406
+ termops.textout(text);
305
407
  }, 1);
306
408
  }
307
409
  }
@@ -323,6 +425,68 @@ exports.NaanNodeREPL = function NaanNodeREPL(options) {
323
425
  }
324
426
  }, 1000);
325
427
 
428
+ //==========================================================================
429
+ // ioctl terminal control
430
+ //--------------------------------------------------------------------------
431
+
432
+ //
433
+ // ioctl_event
434
+ //
435
+ // Events incoming from terminal.
436
+ //
437
+
438
+ this.ioctl_event = function ioctl_event(msg, arg) {
439
+ if (listeners[msg])
440
+ listeners[msg].forEach(function(proc) {
441
+ setTimeout(function () {
442
+ proc(msg, arg);
443
+ }, 1);
444
+ });
445
+ };
446
+
447
+ //
448
+ // ioctl_add_listener
449
+ // ioctl_remove_listener
450
+ //
451
+ // Add a listener for a terminal event.
452
+ //
453
+
454
+ var listeners = { };
455
+
456
+ this.ioctl_add_listener = function ioctl_add_listener(msg, proc) {
457
+ if (typeof(msg) != "string" || typeof(proc) != "function")
458
+ return (false);
459
+ this.ioctl_remove_listener(msg, proc);
460
+ if (!listeners[msg])
461
+ listeners[msg] = [];
462
+ listeners[msg].push(proc);
463
+ return (true);
464
+ };
465
+
466
+ this.ioctl_remove_listener = function ioctl_remove_listener(msg, proc) {
467
+ if (typeof(msg) != "string" || typeof(proc) != "function")
468
+ return (false);
469
+ if (listeners[msg]) {
470
+ var index = listeners[msg].indexOf(proc);
471
+ if (index >= 0)
472
+ listeners[msg].splice(index, 1);
473
+ }
474
+ return (true);
475
+ };
476
+
477
+ //
478
+ // ioctl
479
+ //
480
+ // Control of terminals (and their controller.)
481
+ //
482
+
483
+ this.ioctl = function ioctl(op, arg) {
484
+ setTimeout(function () {
485
+ servSelf.repl_ioctl(op, arg);
486
+ if (termops)
487
+ termops.ioctl(op, arg);
488
+ }, 1);
489
+ };
326
490
 
327
491
  //==========================================================================
328
492
  // State load/save
@@ -352,6 +516,16 @@ exports.NaanNodeREPL = function NaanNodeREPL(options) {
352
516
  dontSaveUntilWorking = false;
353
517
  };
354
518
 
519
+ //
520
+ // Saving
521
+ //
522
+ // Turn state saving on or off; on by default.
523
+ //
524
+
525
+ this.Saving = function Saving(onOff) {
526
+ dontSave = !onOff;
527
+ };
528
+
355
529
  // MakeState
356
530
  //
357
531
  // Save our state into a new object.
@@ -359,6 +533,8 @@ exports.NaanNodeREPL = function NaanNodeREPL(options) {
359
533
  this.MakeState = function MakeState() {
360
534
  if (dontSaveUntilWorking)
361
535
  return (false); // didn't get far enough to call Working()
536
+ if (dontSave)
537
+ return (false); // not saving right now
362
538
  var statedoc = {};
363
539
  statedoc.curversion = kStateCurrentVersion;
364
540
  statedoc.firstver = kStateFirstVersion;
@@ -6,7 +6,7 @@
6
6
  *
7
7
  * column positioning: // // !
8
8
  *
9
- * Copyright (c) 2019-2024 by Richard C. Zulch
9
+ * Copyright (c) 2019-2026 by Richard C. Zulch
10
10
  *
11
11
  */
12
12
 
@@ -63,6 +63,7 @@ exports.NaanWorkerActivate = function NaanWorkerActivate(cbReady) {
63
63
  workSelf.anaan = naanlib.js.n;
64
64
  naanlib.js.t = workSelf;
65
65
  naanlib.js.k = threads;
66
+ var workerMetadata = { };
66
67
 
67
68
 
68
69
  //==========================================================================
@@ -84,7 +85,74 @@ exports.NaanWorkerActivate = function NaanWorkerActivate(cbReady) {
84
85
  this.textCommand = function textCommand(cmd) { // send a command
85
86
  naanlib.textLine(cmd);
86
87
  };
88
+
89
+ this.metadata = function metadata() { // our creation metadata
90
+ return (workerMetadata);
91
+ };
92
+
93
+ this.msgPort = msgPort; // our msgPort
94
+
95
+ //==========================================================================
96
+ // ioctl terminal control
97
+ //--------------------------------------------------------------------------
98
+
99
+ //
100
+ // ioctl_event
101
+ //
102
+ // Events incoming from terminal.
103
+ //
104
+
105
+ workSelf.ioctl_event = function ioctl_event(msg, arg) {
106
+ if (listeners[msg])
107
+ listeners[msg].forEach(function(proc) {
108
+ proc(msg, arg);
109
+ });
110
+ };
111
+
112
+ //
113
+ // ioctl_add_listener
114
+ // ioctl_remove_listener
115
+ //
116
+ // Add a listener for a terminal event.
117
+ //
118
+
119
+ var listeners = { };
120
+
121
+ workSelf.ioctl_add_listener = function ioctl_add_listener(msg, proc) {
122
+ if (typeof(msg) != "string" || typeof(proc) != "function")
123
+ return (false);
124
+ workSelf.ioctl_remove_listener(msg, proc);
125
+ if (!listeners[msg])
126
+ listeners[msg] = [];
127
+ listeners[msg].push(proc);
128
+ return (true);
129
+ };
130
+
131
+ workSelf.ioctl_remove_listener = function ioctl_remove_listener(msg, proc) {
132
+ if (typeof(msg) != "string" || typeof(proc) != "function")
133
+ return (false);
134
+ if (listeners[msg]) {
135
+ var index = listeners[msg].indexOf(proc);
136
+ if (index >= 0)
137
+ listeners[msg].splice(index, 1);
138
+ }
139
+ return (true);
140
+ };
87
141
 
142
+ //
143
+ // ioctl
144
+ //
145
+ // Control of terminals (and their controller.)
146
+ //
147
+
148
+ workSelf.ioctl = function ioctl(op, arg) {
149
+ msgPort.postMessage({
150
+ id: "ioctl",
151
+ op: op,
152
+ arg: arg
153
+ });
154
+ return (undefined);
155
+ };
88
156
 
89
157
  //==========================================================================
90
158
  // NodeJS Worker Terminal Interface
@@ -162,10 +230,13 @@ exports.NaanWorkerActivate = function NaanWorkerActivate(cbReady) {
162
230
  naanlib.escape(); // interrupt request from terminal
163
231
  else if (msg.id == "keyline")
164
232
  naanlib.textLine(msg.text); // keyboard text from terminal
233
+ else if (msg.id == "typed")
234
+ echoTyped(msg.text);
165
235
  else if (msg.id == "start")
166
236
  { // start execution with optional state
167
237
  workSelf.workerID = msg.workerID;
168
238
  workSelf.workerGUID = msg.workerGUID;
239
+ workerMetadata = msg.metadata || { };
169
240
  if (typeof(__dirname) !== "undefined")
170
241
  naanlib.js.h = naanlib.js.d; // set host directory path
171
242
  if (msg.dirpath)
@@ -197,6 +268,8 @@ exports.NaanWorkerActivate = function NaanWorkerActivate(cbReady) {
197
268
  if (targetReceiver)
198
269
  targetReceiver(msg.data);
199
270
  }
271
+ else if (msg.id == "ioctl-event")
272
+ workSelf.ioctl_event(msg.msg, msg.arg);
200
273
  });
201
274
 
202
275
  naanlib.banner();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naanlang/naan",
3
- "version": "1.5.0",
3
+ "version": "1.6.0",
4
4
  "author": "Richard C. Zulch",
5
5
  "description": "Naan™ software platform",
6
6
  "main": "./lib/core/naanlib.js",
@@ -100,7 +100,7 @@ closure NexaraSpeechRec(local nxspr) {
100
100
  // Convert a Nexara response to a Yandex SpeechKit structure. Hey, it was first.
101
101
  //
102
102
  function nexaraToYCSK(resp,
103
- local output, wordx, segment, chunk, word, wordet) {
103
+ local output, wordx, segment, chunk, syncerror, word, wordet, wordtrim, tstart) {
104
104
  output = {
105
105
  \@type: "type.googleapis.com/yandex.cloud.ai.stt.v2.LongRunningRecognitionResponse"
106
106
  chunks: []
@@ -120,14 +120,18 @@ closure NexaraSpeechRec(local nxspr) {
120
120
  ]
121
121
  }
122
122
  for word in segment.text.split(" ") {
123
+ if word == ""
124
+ continue // ignore extra spaces
123
125
  wordet = resp.words[wordx++]
124
- if wordet.word != word {
125
- debuglog("nexaraToYCSK: expected ${word} but got ${wordet.word}")
126
- return (false)
126
+ wordtrim = wordet.word.trim() // detected word without spaces
127
+ tstart = wordet.start.toFixed(3).concat("s")
128
+ if !syncerror && wordtrim != word {
129
+ syncerror = true
130
+ debuglog("nexaraToYCSK: warning: at ${tstart} expected '${word}' but got '${wordtrim}'")
127
131
  }
128
132
  chunk.alternatives.0.words.push({
129
- word: wordet.word
130
- startTime: wordet.start.toFixed(3).concat("s")
133
+ word: wordtrim
134
+ startTime: tstart
131
135
  endTime: wordet.end.toFixed(3).concat("s")
132
136
  })
133
137
  }
@@ -265,5 +265,28 @@ RegisterTestCategory("context", [
265
265
  [ "closure Xx(y, local xx) { xx = new(object, this) };", "Xx" ],
266
266
  [ "x = Xx(33);", "{object Xx}" ],
267
267
  [ "car(x@\\.closure.proc.2.0)", "33" ],
268
- [ "new(object, 3)@*", "false" ]
268
+ [ "new(object, 3)@*", "false" ],
269
+
270
+ //
271
+ // undefined objects
272
+ //
273
+
274
+ [ "closure undef(local o) {\n"
275
+ " o = new(object, undef)\n"
276
+ " put(o,'.undefined', function udef args {\n"
277
+ " print('udef:', args)\n"
278
+ " })\n"
279
+ " o\n"
280
+ "};", "undef" ],
281
+ [ "oo = undef();", "{object undef}"],
282
+ [ "closure(c) {\n"
283
+ "xset(c, joe2, macro mjoe args {\n"
284
+ " printline(args)\n"
285
+ " deref(c, joe, args)\n"
286
+ "})\n"
287
+ "}(oo);>", "(macro mjoe args (printline args) (deref c joe args))"],
288
+ [ "oo.joe(8,2+5);", "udef:(joe 8 7)|(joe, 8, 7)" ],
289
+ [ "oo.joe2(5,9+3);", "(5 (+ 9 3))|udef:(joe 5 (+ 9 3))|(joe, 5, 9+3)" ],
290
+ [ "dargs = list(3,quote(2+4));", "(3, 2+4)"],
291
+ [ "deref(oo, joe, dargs);", "udef:(joe 3 (+ 2 4))|(joe, 3, 2+4)"]
269
292
  ]);