@naanlang/naan 1.0.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 (98) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +46 -0
  3. package/dist/env_web.js +431 -0
  4. package/dist/naan.min.js +73 -0
  5. package/frameworks/browser/browser.nlg +30 -0
  6. package/frameworks/browser/pouchdb/pouchdb.min.js +7 -0
  7. package/frameworks/browser/spark-md5/spark-md5.min.js +1 -0
  8. package/frameworks/browser/sworker.js +192 -0
  9. package/frameworks/browser/terminals.nlg +787 -0
  10. package/frameworks/browser/workers.nlg +386 -0
  11. package/frameworks/client/apiclient.nlg +183 -0
  12. package/frameworks/client/client.nlg +29 -0
  13. package/frameworks/client/psm_client.nlg +572 -0
  14. package/frameworks/common/common.nlg +464 -0
  15. package/frameworks/common/preferences.nlg +131 -0
  16. package/frameworks/common/repltools.nlg +100 -0
  17. package/frameworks/common/watching.nlg +97 -0
  18. package/frameworks/node/apiserver.nlg +399 -0
  19. package/frameworks/node/filesystem.nlg +1012 -0
  20. package/frameworks/node/gitter.nlg +234 -0
  21. package/frameworks/node/node.nlg +81 -0
  22. package/frameworks/node/psm_server.nlg +465 -0
  23. package/frameworks/node/worker.nlg +560 -0
  24. package/frameworks/project/build.nlg +1229 -0
  25. package/frameworks/project/jszip.js +30 -0
  26. package/frameworks/project/jszip.nlg +87 -0
  27. package/frameworks/project/proj_cliser.nlg +139 -0
  28. package/frameworks/project/proj_console.nlg +137 -0
  29. package/frameworks/project/proj_folder.nlg +143 -0
  30. package/frameworks/project/proj_lambda.nlg +137 -0
  31. package/frameworks/project/proj_static.nlg +138 -0
  32. package/frameworks/project/project.nlg +29 -0
  33. package/frameworks/project/projects.nlg +587 -0
  34. package/frameworks/project/uglifyjs.js +1 -0
  35. package/frameworks/project/uglifyjs.nlg +79 -0
  36. package/frameworks/running/debugcom.nlg +163 -0
  37. package/frameworks/running/debugnub.nlg +565 -0
  38. package/frameworks/running/debugutil.nlg +510 -0
  39. package/frameworks/running/executors.nlg +767 -0
  40. package/frameworks/running/running.nlg +151 -0
  41. package/frameworks/running/sourcecode.nlg +346 -0
  42. package/frameworks/running/taskexec.nlg +196 -0
  43. package/frameworks/storage/dbt_pouch.nlg +773 -0
  44. package/frameworks/storage/file_manager.nlg +772 -0
  45. package/frameworks/storage/psm.nlg +326 -0
  46. package/frameworks/storage/psm_dbtables.nlg +814 -0
  47. package/frameworks/storage/resources.nlg +445 -0
  48. package/frameworks/storage/storage.nlg +52 -0
  49. package/lib/browser/env_web.js +431 -0
  50. package/lib/browser/env_webworker.js +146 -0
  51. package/lib/browser/require.js +1653 -0
  52. package/lib/core/naanlib.js +73 -0
  53. package/lib/env_node.js +282 -0
  54. package/lib/env_nodeworker.js +165 -0
  55. package/lib/node_repl.js +25 -0
  56. package/lib/node_repl_init.nlg +52 -0
  57. package/package.json +13 -0
  58. package/plugins/gitClient/clientops.nlg +317 -0
  59. package/plugins/gitClient/gitClient.nlg +63 -0
  60. package/plugins/gitClient/gitutils.nlg +1331 -0
  61. package/plugins/gitClient/minimatch.js +945 -0
  62. package/plugins/gitClient/minimatch_license.txt +15 -0
  63. package/plugins/gitClient/minimatch_readme.md +208 -0
  64. package/plugins/nodeLib/nodeLib.nlg +26 -0
  65. package/plugins/nodeLib/node_cp.nlg +65 -0
  66. package/plugins/nodeLib/node_fs.nlg +137 -0
  67. package/plugins/nodeLib/node_https.nlg +172 -0
  68. package/plugins/nodeLib/node_util.nlg +180 -0
  69. package/plugins/serviceAws/aws/aws-sdk.min.js +88 -0
  70. package/plugins/serviceAws/aws/lambda_rest_index.js +122 -0
  71. package/plugins/serviceAws/aws/lambda_rest_init.nlg +58 -0
  72. package/plugins/serviceAws/aws/lambda_ws_index.js +498 -0
  73. package/plugins/serviceAws/aws/lambda_ws_init.nlg +49 -0
  74. package/plugins/serviceAws/aws_cloudwatchlogs.nlg +122 -0
  75. package/plugins/serviceAws/aws_cognito.nlg +76 -0
  76. package/plugins/serviceAws/aws_dynamo.nlg +509 -0
  77. package/plugins/serviceAws/aws_lambda.nlg +95 -0
  78. package/plugins/serviceAws/aws_s3.nlg +134 -0
  79. package/plugins/serviceAws/aws_utils.nlg +86 -0
  80. package/plugins/serviceAws/dbt_aws.nlg +769 -0
  81. package/plugins/serviceAws/lambda_naan_control.nlg +175 -0
  82. package/plugins/serviceAws/psm_aws.nlg +258 -0
  83. package/plugins/serviceAws/serviceAws.nlg +38 -0
  84. package/plugins/serviceGitHub/gitops.nlg +522 -0
  85. package/plugins/serviceGitHub/psm_github.nlg +205 -0
  86. package/plugins/serviceGitHub/serviceGitHub.nlg +65 -0
  87. package/plugins/serviceGitLab/gitops.nlg +528 -0
  88. package/plugins/serviceGitLab/psm_gitlab.nlg +205 -0
  89. package/plugins/serviceGitLab/serviceGitLab.nlg +65 -0
  90. package/test/harness.nlg +312 -0
  91. package/test/node_test.js +33 -0
  92. package/test/test_01_core.nlg +768 -0
  93. package/test/test_02_context.nlg +269 -0
  94. package/test/test_03_jsinterop.nlg +137 -0
  95. package/test/test_04_numerics.nlg +450 -0
  96. package/test/test_05_strings.nlg +63 -0
  97. package/test/test_06_lingoparse.nlg +500 -0
  98. package/test/test_07_lingo.nlg +623 -0
@@ -0,0 +1,464 @@
1
+ /*
2
+ * common.nlg
3
+ * Naanlib/frameworks/common
4
+ *
5
+ * Root compoment for common utility functionality applicable to both browser and NodeJS.
6
+ *
7
+ * column positioning: // // !
8
+ *
9
+ * Copyright (c) 2020-2022 by Richard C. Zulch
10
+ *
11
+ */
12
+
13
+
14
+ /*
15
+ * EncodeQuery
16
+ *
17
+ * Encode a dictionary of query terms into a query string for a URI.
18
+ *
19
+ */
20
+
21
+ function EncodeQuery(prefix, items, local result, item) {
22
+ result = []
23
+ for item in items
24
+ result.push(EncodeURIComponent(item).concat("=", EncodeURIComponent(items[item])))
25
+ result = result.join("&")
26
+ if prefix && result.length > 0
27
+ prefix.concat(result)
28
+ else
29
+ result
30
+ };
31
+
32
+
33
+ /*
34
+ * UUID
35
+ *
36
+ * Return a new v4 variant 1 UUID, which is the random variety. It will be a 36-character string.
37
+ *
38
+ */
39
+
40
+ function UUID(local rando, cc) {
41
+ if !RandomBytes
42
+ return (false)
43
+ rando = RandomBytes(36) // length of UUID
44
+ "00000000-0000-4000-8000-000000000000".replace(RegExp("[018]", "g"), function(cc, offset) {
45
+ if cc == "4"
46
+ cc = 4
47
+ else if cc == "8"
48
+ cc = bitor(8, bitand(rando[offset], 3))
49
+ else
50
+ cc = bitand(rando[offset], 15)
51
+ "0123456789abcdef".charAt(cc)
52
+ })
53
+ };
54
+
55
+
56
+ /*
57
+ * JsonParse
58
+ *
59
+ * Parse a string as JSON, returning a result tuple instead of an exception.
60
+ *
61
+ */
62
+
63
+ function JsonParse(data) {
64
+ try {
65
+ data = JSONparse(data)
66
+ list(false, data)
67
+ } catch {
68
+ if true
69
+ list(exception)
70
+ }
71
+ };
72
+
73
+
74
+ /*
75
+ * JsonStringify
76
+ *
77
+ * Convert an object to a JSON string, returning a result tuple instead of an exception.
78
+ *
79
+ */
80
+
81
+ function JsonStringify(data) {
82
+ try {
83
+ data = JSONstringify(data)
84
+ list(false, data)
85
+ } catch {
86
+ if true
87
+ list(exception)
88
+ }
89
+ };
90
+
91
+
92
+ /*
93
+ * JsLoadScript
94
+ *
95
+ * Load the specified script, or return its main entry point if already loaded. This blocks the
96
+ * caller until the script load completes. The return value is false if an error occurred.
97
+ *
98
+ */
99
+
100
+ jsScripts = { }; // script values become false on reload
101
+
102
+ function JsLoadScript(libpath, globalID, local scriptTag, fpath) {
103
+ if jsScripts[jsScripts]
104
+ return // already loaded
105
+ if libpath.startsWith("/")
106
+ fpath = libpath
107
+ else
108
+ fpath = "/".concat(libpath)
109
+ if js.s.importScripts && tostring(js.s.importScripts) == "[Function importScripts]" {
110
+ js.s.importScripts(fpath) // load in worker context
111
+ jsScripts[libpath] = js.s[globalID]
112
+ } else if js.w { // load in browser context
113
+ pending = new(nonce)
114
+ scriptTag = js.w.document.createElement("script")
115
+ scriptTag.src = js.w.location.origin.concat(fpath)
116
+ scriptTag.onload = function (event) {
117
+ jsScripts[libpath] = js.w[globalID]
118
+ pending.signal(JSZipLib)
119
+ true
120
+ }
121
+ scriptTag.onerror = function (event) {
122
+ debuglog("jsScriptLoader:", libpath, "not found")
123
+ pending.signal(false)
124
+ true
125
+ }
126
+ js.w.document.body.appendChild(scriptTag)
127
+ pending.wait()
128
+ } else if js.g {
129
+ if !libpath.startsWith("/")
130
+ fpath = "./".concat(libpath)
131
+ jsScripts[libpath] = js.r(fpath) // load in NodeJS context
132
+ }
133
+ jsScripts[libpath]
134
+ };
135
+
136
+
137
+ /*
138
+ * LoadComponentOnDemand
139
+ *
140
+ * Create and export a new load-on-demand function in the caller's module for the key or array
141
+ * of keys specified. On successful load the component will automatically replace the export with the
142
+ * actual function. This requires that the export be called as a procedure. Throw's on failure, which
143
+ * shouldn't happen unless the component or the exported function are unavailable.
144
+ *
145
+ */
146
+
147
+ macro LoadComponentOnDemand(functionKeys, componentPath, local modobj, key) {
148
+ modobj = car(nsrights().lookup("module"))
149
+ functionKeys = eval(functionKeys)
150
+ componentPath = eval(componentPath)
151
+ if name(functionKeys)
152
+ functionKeys = list(functionKeys)
153
+ for key in functionKeys
154
+ closure (key, path, local exproc) {
155
+ function loadComponent args { // called when exported function first called
156
+ exproc = require(path)[key]
157
+ if !exproc || exproc === loadComponent
158
+ throw("Unable to load ".concat(key, " in ", path))
159
+ apply(exproc, args) // call exported function with args
160
+ }
161
+ modobj.exports[key] = loadComponent
162
+ } (key, componentPath)
163
+ true
164
+ };
165
+
166
+
167
+ /*
168
+ * LiveImport
169
+ * LiveExport
170
+ *
171
+ * These are used to ensure that imported symbols have up-to-date values. This is needed because
172
+ * well-known JavaScript objects are cleared on reload, so imported xobjects become invalid. One
173
+ * cannot simply call letimport again on reload because the import may occur before the symbol values
174
+ * are updated. We'd rather not call a function on each reference.
175
+ * Our solution is LiveImport(), exported by the exporting module so that importers can call it
176
+ * to receive updates. Because other modules besides common will need this, LiveExport() will add a
177
+ * LiveImport() function to a module's exports.
178
+ *
179
+ * To use LiveImport, call from the importing module as follows:
180
+ * require("frameworks/common/common.nlg").LiveImport(<fixproc>);
181
+ * The optional <fixproc> is called after the imports in case the foreign module wants to fix up
182
+ * anything after the import:
183
+ * fixproc(exporterMod);
184
+ * If LiveImport is called from the same module as the exporter then there is nothing to import, but
185
+ * <fixproc> will be called, which allows components in the module to update internal state.
186
+ *
187
+ * To use LiveExport, call it as follows:
188
+ * exportUpdate = LiveExport()
189
+ * This will create a LiveImport function in the module.exports of the caller. Call exportUpdate()
190
+ * after updating the exported symbols.
191
+ *
192
+ */
193
+
194
+ macro LiveExport() {
195
+ closure (exporterMod, local importList) {
196
+ importList = []
197
+ //
198
+ // LiveImport
199
+ // Return a new LiveImport function to be exported by the module who called MakeLiveImport.
200
+ //
201
+ macro LiveImport(fixproc, local importingMod, mdex, updater) {
202
+ importingMod = car(nsrights().lookup("module"))
203
+ mdex = importList.indexOf(importingMod)
204
+ if mdex >= 0
205
+ updater = importList[mdex] // already registered
206
+ else {
207
+ updater = closure(importingMod, fixproc) { // create new updater
208
+ putproc(compress(updateImports), function updateImports(local goback) {
209
+ if importingMod !== exporterMod {
210
+ goback = makeActivator()
211
+ nsactive(cons(nsrights(), nsactive())) // make importer namespace active
212
+ letimport(exporterMod.exports) } // import to active namespace
213
+ try {
214
+ call(fixproc, exporterMod) // let caller do any fixups
215
+ } catch {
216
+ if (true)
217
+ debuglog("LiveImport fixup failed in", nsrights(), "exception:", exception)
218
+ }
219
+ goback()
220
+ })
221
+ updateImports
222
+ } (importingMod, eval(fixproc))
223
+ importList.push(updater)
224
+ }
225
+ call(updater) // update the imports
226
+ updater // return the updater
227
+ }
228
+ exporterMod.exports.LiveImport = LiveImport
229
+
230
+ //
231
+ // updateExports
232
+ // Update our exports into all registered importers.
233
+ //
234
+ function updateExports(local updater) { // defined in exporter's namespace
235
+ for updater in importList
236
+ call(updater)
237
+ true
238
+ }
239
+ } (car(nsrights().lookup("module"))) // module of caller
240
+ };
241
+
242
+
243
+ /*
244
+ * comrInit
245
+ *
246
+ * Initialize the common module.
247
+ *
248
+ */
249
+
250
+ updateExports = false; // explicit globals used below
251
+ RandomBytes = false;
252
+ HashMD5 = false;
253
+ HashMD5_base64 = false;
254
+ JSpath = false;
255
+ TextEncoder = false;
256
+ TextDecoder = false;
257
+ EncodeURIComponent = false;
258
+ DecodeURIComponent = false;
259
+ JSONstringify = false;
260
+ JSONparse = false;
261
+
262
+ function comrInit(local manifest) {
263
+ manifest = `(EncodeQuery, UUID, JsLoadScript, LoadComponentOnDemand, LiveExport, comrInit)
264
+
265
+ Naan.module.build(module.id, "nideCommon", closure(modobj, compobj) {
266
+ compobj.manifest = manifest
267
+ modobj.exports.EncodeQuery = EncodeQuery
268
+ modobj.exports.UUID = UUID
269
+ modobj.exports.JsLoadScript = JsLoadScript
270
+ modobj.exports.LoadComponentOnDemand = LoadComponentOnDemand
271
+ modobj.exports.LiveExport = LiveExport // allow others to create export updaters
272
+ updateExports = LiveExport() // export LiveImport() so we can update
273
+
274
+ // comrReload
275
+ //
276
+ // Initialize environment functionality when we reload.
277
+ //
278
+ closure comrReload(local wsg, crypto) {
279
+ wsg = js.w || js.s || js.g // any global: browser or web worker or nodejs
280
+ RandomBytes = false
281
+ HashMD5 = false
282
+ HashMD5_base64 = false
283
+ JSpath = js.r("path")
284
+ jspath = JSpath // ### temporary during transition
285
+ //
286
+ // everywhere: Text
287
+ TextEncoder = wsg.TextEncoder
288
+ TextDecoder = wsg.TextDecoder
289
+ EncodeURIComponent = wsg.encodeURIComponent
290
+ DecodeURIComponent = wsg.decodeURIComponent
291
+ //
292
+ // everywhere: JSON
293
+ JSONstringify = wsg.JSON.stringify
294
+ JSONparse = wsg.JSON.parse
295
+ //
296
+ // binaryFrom - convert arbitrary item to binary buffer or text.
297
+ //
298
+ function binaryFrom(data) {
299
+ if xobject(data) {
300
+ if js.o.getPrototypeOf(data).constructor !== wsg.ArrayBuffer
301
+ && tostring(data) != "[object ArrayBuffer]"
302
+ && !jsTypedArray(data)
303
+ JSONstringify(data)
304
+ else
305
+ data
306
+ } else if !string(data)
307
+ JSONstringify(data)
308
+ else
309
+ data
310
+ }
311
+ //
312
+ // hexFormat - create padded hex string from binary array
313
+ //
314
+ function hexFormat(data, padlen, local result) {
315
+ result = toint(xnew(wsg.Uint8Array, data)).tostring({radix:16})
316
+ if result.length > padlen
317
+ result.slice(-padlen)
318
+ else if result.length < padlen
319
+ "0".repeat(padlen-result.length).concat(result)
320
+ else
321
+ result
322
+ }
323
+ //
324
+ // base64Format - create base 64 string from binary array
325
+ //
326
+ function base64Format(data, local blob, reader, pending, result) {
327
+ blob = xnew(wsg.Blob, [data])
328
+ reader = xnew(wsg.FileReader)
329
+ pending = new(nonce)
330
+ reader.onload = function(event) { pending.signal(event.target.result) }
331
+ reader.readAsDataURL(blob)
332
+ result = pending.wait()
333
+ result.slice(37)
334
+ }
335
+ //
336
+ // DecodeBase64url - decode base64url to original string
337
+ // See https://en.wikipedia.org/wiki/Base64 for details on variants
338
+ //
339
+ DecodeBase64url = function decodeBase64url(base64url, local padding) {
340
+ base64url = base64url.replace(RegExp("-", "g"), '+').replace(RegExp("_", "g"), '/')
341
+ wsg.atob(base64url)
342
+ }
343
+ //
344
+ // For browser windows and web workers
345
+ //
346
+ if js.w || js.s {
347
+ //
348
+ // MD5 functions
349
+ JsLoadScript("frameworks/browser/spark-md5/spark-md5.min.js", "SparkMD5")
350
+ //
351
+ // hasMD5 in hex
352
+ HashMD5 = function browserMD5(data, raw) {
353
+ data = binaryFrom(data)
354
+ if xobject(data)
355
+ wsg.SparkMD5.ArrayBuffer.hash(data, raw)
356
+ else
357
+ wsg.SparkMD5.hash(data, raw) }
358
+ //
359
+ // HashMD5 in base64
360
+ HashMD5_base64 = function browserMD5_base64(data) {
361
+ wsg.btoa(browserMD5(data, true)) }
362
+ //
363
+ // browserDigest - only in secure contexts (localhost and HTTPS)
364
+ //
365
+ function browserDigest(data, algo, local error, result) {
366
+ if !wsg.crypto.subtle
367
+ throw(Error("crypto functions unavailable"))
368
+ data = binaryFrom(data)
369
+ if string(data)
370
+ data = xnew(TextEncoder).encode(data)
371
+ `(error, result) = await(wsg.crypto.subtle.digest(algo, data))
372
+ if error
373
+ throw(Error("crypto digest failed", error))
374
+ result
375
+ }
376
+ //
377
+ // HashSHA1 in hex
378
+ HashSHA1 = function browserSHA1(data) {
379
+ hexFormat(browserDigest(data, "SHA-1"), 40)
380
+ }
381
+ //
382
+ // HashSHA-256 in hex
383
+ HashSHA256 = function browserSHA256(data) {
384
+ hexFormat(browserDigest(data, "SHA-256"), 64)
385
+ }
386
+ //
387
+ // HashSHA-256 in base64
388
+ HashSHA256_base64 = function browserSHA256_base64(data) {
389
+ base64Format(browserDigest(data, "SHA-256"))
390
+ }
391
+ //
392
+ // random bytes as UInt8Array - use toint() for integer
393
+ if wsg.crypto
394
+ RandomBytes = function browserRandomBytes(bytes) {
395
+ wsg.crypto.getRandomValues(xnew(wsg.Uint8Array,bytes))
396
+ }
397
+ }
398
+ //
399
+ // For NodeJS
400
+ //
401
+ if js.g {
402
+ if !crypto
403
+ crypto = js.r("crypto")
404
+ if crypto {
405
+ //
406
+ // hasMD5 in hex
407
+ HashMD5 = function nodejsMD5(text) {
408
+ crypto.createHash("MD5").update(binaryFrom(text)).digest("hex") }
409
+ //
410
+ // HashMD5 in base64
411
+ HashMD5_base64 = function nodejsMD5_base64(text) {
412
+ crypto.createHash("MD5").update(binaryFrom(text)).digest("base64") }
413
+ //
414
+ // HashSHA1 in hex
415
+ HashSHA1 = function nodejsSHA1(text) {
416
+ crypto.createHash("sha1").update(binaryFrom(text)).digest("hex") }
417
+ //
418
+ // HashSHA1 in hex
419
+ HashSHA256 = function nodejsSHA256(text) {
420
+ crypto.createHash("sha256").update(binaryFrom(text)).digest("hex") }
421
+ //
422
+ // HashSHA1 in base64
423
+ HashSHA256_base64 = function nodejsSHA256_base64(text) {
424
+ crypto.createHash("sha256").update(binaryFrom(text)).digest("base64") }
425
+ //
426
+ // random bytes as UInt8Array - use toint() for integer
427
+ RandomBytes = function nodeRandomBytes(bytes) {
428
+ crypto.randomBytes(bytes)
429
+ }
430
+ } else {
431
+ HashMD5 =
432
+ HashMD5_base64 =
433
+ HashSHA1 =
434
+ RandomBytes = function() { throw(Error("crypto functions unavailable")) }
435
+ }
436
+ }
437
+ //
438
+ // Exports for all environments
439
+ //
440
+ modobj.exports.JSpath = JSpath
441
+ modobj.exports.TextEncoder = TextEncoder
442
+ modobj.exports.TextDecoder = TextDecoder
443
+ modobj.exports.EncodeURIComponent = EncodeURIComponent
444
+ modobj.exports.DecodeURIComponent = DecodeURIComponent
445
+ modobj.exports.JSONstringify = JSONstringify
446
+ modobj.exports.JSONparse = JSONparse
447
+ modobj.exports.JsonStringify = JsonStringify
448
+ modobj.exports.JsonParse = JsonParse
449
+ modobj.exports.DecodeBase64url = DecodeBase64url
450
+ modobj.exports.HashMD5 = HashMD5
451
+ modobj.exports.HashMD5_base64 = HashMD5_base64
452
+ modobj.exports.HashSHA1 = HashSHA1
453
+ modobj.exports.HashSHA256 = HashSHA256
454
+ modobj.exports.HashSHA256_base64 = HashSHA256_base64
455
+ modobj.exports.RandomBytes = RandomBytes
456
+ //
457
+ // Update any importers who cared enough to call LiveImport
458
+ //
459
+ updateExports()
460
+ }()
461
+ module.reload = comrReload
462
+ })
463
+
464
+ } ();
@@ -0,0 +1,131 @@
1
+ /*
2
+ * preferences.nlg
3
+ * naanlib/frameworks/common/preferences
4
+ *
5
+ * A preference storage facility.
6
+ *
7
+ * column positioning: // // !
8
+ *
9
+ * Copyright (c) 2017-2021 by Richard C. Zulch
10
+ *
11
+ */
12
+
13
+
14
+ /*
15
+ * Preferences
16
+ *
17
+ * Preferences object for persistent preferences outside of projects. This also opens the Nide
18
+ * database for local browser storage.
19
+ *
20
+ */
21
+
22
+ closure Preferences(nicc, dbname, local prefs, error, data) {
23
+ `(error, data) = require("../storage/dbt_pouch.nlg").PouchDB(dbname)
24
+ if error {
25
+ ErrorDebuglog("Can't access Nide local browser database", error)
26
+ return (false)
27
+ }
28
+ nicc.db = data
29
+ `(error, data) = nicc.db.table("NideData")
30
+ if error {
31
+ ErrorDebuglog("Can't access NideData table", error)
32
+ return (false)
33
+ }
34
+ nicc.dbtable = data
35
+ prefs = new(object, this)
36
+
37
+ // save
38
+ // Save preferences to pouchDB
39
+ prefs.save = function save() {
40
+ if prefs.doc {
41
+ prefs.doc.content = prefs.data
42
+ `(error, data) = nicc.dbtable.update(prefs.doc)
43
+ prefs.doc = data }
44
+ else {
45
+ `(error, data) = nicc.dbtable.add("NidePreferences", prefs.data)
46
+ prefs.doc = data }
47
+ if error
48
+ nicc.alert("<b>Error saving preferences</b><br>".concat(ErrorString(error)), { class: "alert-danger" })
49
+ }
50
+
51
+ // default
52
+ // Load default preferences
53
+ prefs.default = closure default(local pending) {
54
+ pending = new(nonce)
55
+ nicc.api.readFile("nide_options.cfg", function(error, req, event, local config) {
56
+ if error {
57
+ ErrorDebuglog("Can't read nide_options.cfg", error)
58
+ config = { } }
59
+ else
60
+ config = new(js.w.JSON.parse(req.responseText))
61
+ pending.signal(config)
62
+ })
63
+ pending.wait()
64
+ }
65
+
66
+ // load
67
+ // Load preferences from pouchDB
68
+ prefs.load = function load() {
69
+ `(error, data) = nicc.dbtable.get("NidePreferences")
70
+ if error { // missing preferences or other error
71
+ if error.status != 404
72
+ ErrorDebuglog("Can't load preferences", error)
73
+ prefs.data = { } }
74
+ else {
75
+ prefs.doc = data
76
+ prefs.data = data.content
77
+ }
78
+ data = default()
79
+ for key in data
80
+ if !prefs.data[key]
81
+ prefs.data[key] = data[key] // bring in any new keys from configuration
82
+ }
83
+ prefs.load()
84
+
85
+ // loadplugins
86
+ // Load listed plugins
87
+ prefs.loadplugins = function loadplugins(local folder, path, name) {
88
+ folder = "plugins/"
89
+ path = folder.concat("plugins.cfg")
90
+ pending = new(nonce)
91
+ nicc.api.readFile(path, function(error, req, event, local config) {
92
+ if error
93
+ ErrorDebuglog("Can't read ".concat(path), error)
94
+ else
95
+ prefs.plugins = new(js.w.JSON.parse(req.responseText))
96
+ pending.signal(config)
97
+ })
98
+ pending.wait()
99
+ for name in prefs.plugins.load {
100
+ path = folder.concat(name)
101
+ try {
102
+ require(path)
103
+ } catch {
104
+ if true
105
+ ErrorDebuglog("Exception requiring: ", path, exception)
106
+ }
107
+ }
108
+ }
109
+
110
+ // finis
111
+ prefs
112
+ };
113
+
114
+
115
+ /*
116
+ * prefsInit
117
+ *
118
+ * Initialize the preferences component.
119
+ *
120
+ */
121
+
122
+ function prefsInit(local manifest) {
123
+ manifest = `(Preferences, prefsInit)
124
+
125
+ Naan.module.build(module.id, "preferences", function(modobj, compobj) {
126
+ require("../common/common.nlg")
127
+ compobj.manifest = manifest
128
+ })
129
+
130
+ module.exports.Preferences = Preferences
131
+ } ();
@@ -0,0 +1,100 @@
1
+ /*
2
+ * repltools.nlg
3
+ *
4
+ * Developer tools for Naan REPLs.
5
+ *
6
+ * column positioning: // // !
7
+ *
8
+ * Copyright (c) 2017-2021 by Richard C. Zulch
9
+ *
10
+ */
11
+
12
+
13
+ /*
14
+ * dtrace
15
+ *
16
+ * Turn on low-level tracing for the duration of evaluating an expression.
17
+ *
18
+ */
19
+
20
+ macro dtrace(xx, level, local sdl, result) {
21
+ sdl = loglevel()
22
+ if not level
23
+ level = 5
24
+ loglevel(level)
25
+ try {
26
+ result = eval(xx)
27
+ } finally {
28
+ loglevel(sdl)
29
+ }
30
+ result
31
+ };
32
+
33
+
34
+ /*
35
+ * debug
36
+ *
37
+ * Enter the debugger just before evaluating the specified expression.
38
+ *
39
+ */
40
+
41
+ macro debug(expr) {
42
+ if atom(expr)
43
+ first(expr)
44
+ else {
45
+ expr = list(list(Naan.debug.debugger, 1), expr)
46
+ second(eval(expr)) }
47
+ };
48
+
49
+
50
+ /*
51
+ * dcaller
52
+ *
53
+ * Report the caller of the specified function for debugging. By default this ignores lambda
54
+ * functions, but you can pass true for argument two to report them.
55
+ *
56
+ */
57
+
58
+ function dcaller(proc, lambdaOK, local x, found) {
59
+ for x in stacktrace()
60
+ if procedure(list(x.0)) {
61
+ if !lambdaOK && lambda(x.1)
62
+ continue
63
+ if found && !proc
64
+ return (x.1) // found procedure name symbol
65
+ else if x.1 == dcaller
66
+ found = true
67
+ else if proc && x.1 == proc
68
+ proc = false }
69
+ false
70
+ };
71
+
72
+
73
+ /*
74
+ * ImportReplTools
75
+ *
76
+ * Import the REPL tools on behalf of the caller.
77
+ *
78
+ */
79
+
80
+ macro ImportReplTools() {
81
+ letimport(dtrace, debug, dcaller)
82
+ };
83
+
84
+
85
+ /*
86
+ * retoInit
87
+ *
88
+ * Initialize the component.
89
+ *
90
+ */
91
+
92
+ function retoInit(local manifest) {
93
+ manifest = `(dtrace, debug, dcaller, ImportReplTools, retoInit)
94
+
95
+ Naan.module.build(module.id, "repltools", function(modobj, compobj) {
96
+ require("./common.nlg")
97
+ compobj.manifest = manifest
98
+ modobj.exports.ImportReplTools = ImportReplTools
99
+ })
100
+ } ();