agnostics 0.0.1

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 (68) hide show
  1. package/README.md +3 -0
  2. package/lib/helpers/_Express.js +30 -0
  3. package/lib/helpers/_Parsers.js +73 -0
  4. package/lib/helpers/_Window.js +0 -0
  5. package/lib/helpers/_Yargs.js +328 -0
  6. package/lib/helpers/index.js +9 -0
  7. package/lib/index.js +11 -0
  8. package/lib/inputs/_Callers.js +122 -0
  9. package/lib/inputs/_Checker.js +3 -0
  10. package/lib/inputs/_Native.js +223 -0
  11. package/lib/inputs/_Types.js +112 -0
  12. package/lib/inputs/index.js +10 -0
  13. package/lib/logger/_ASCII.js +39 -0
  14. package/lib/logger/_Defs.js +55 -0
  15. package/lib/logger/_Logger.js +159 -0
  16. package/lib/logger/_Settings.js +52 -0
  17. package/lib/logger/_System.js +191 -0
  18. package/lib/logger/chars.js +730 -0
  19. package/lib/logger/index.js +5 -0
  20. package/lib/schema/_Schema.js +33 -0
  21. package/lib/schema/index.js +16 -0
  22. package/lib/specification/_Properties.js +144 -0
  23. package/lib/specification/index.js +9 -0
  24. package/package.json +32 -0
  25. package/types/helpers/_Express.d.ts +2 -0
  26. package/types/helpers/_Express.d.ts.map +1 -0
  27. package/types/helpers/_Parsers.d.ts +2 -0
  28. package/types/helpers/_Parsers.d.ts.map +1 -0
  29. package/types/helpers/_Window.d.ts +2 -0
  30. package/types/helpers/_Window.d.ts.map +1 -0
  31. package/types/helpers/_Yargs.d.ts +44 -0
  32. package/types/helpers/_Yargs.d.ts.map +1 -0
  33. package/types/helpers/index.d.ts +2 -0
  34. package/types/helpers/index.d.ts.map +1 -0
  35. package/types/index.d.ts +4 -0
  36. package/types/index.d.ts.map +1 -0
  37. package/types/inputs/_Callers.d.ts +14 -0
  38. package/types/inputs/_Callers.d.ts.map +1 -0
  39. package/types/inputs/_Checker.d.ts +2 -0
  40. package/types/inputs/_Checker.d.ts.map +1 -0
  41. package/types/inputs/_Native.d.ts +212 -0
  42. package/types/inputs/_Native.d.ts.map +1 -0
  43. package/types/inputs/_Types.d.ts +34 -0
  44. package/types/inputs/_Types.d.ts.map +1 -0
  45. package/types/inputs/index.d.ts +3 -0
  46. package/types/inputs/index.d.ts.map +1 -0
  47. package/types/logger/_ASCII.d.ts +2 -0
  48. package/types/logger/_ASCII.d.ts.map +1 -0
  49. package/types/logger/_Defs.d.ts +54 -0
  50. package/types/logger/_Defs.d.ts.map +1 -0
  51. package/types/logger/_Logger.d.ts +5 -0
  52. package/types/logger/_Logger.d.ts.map +1 -0
  53. package/types/logger/_Settings.d.ts +24 -0
  54. package/types/logger/_Settings.d.ts.map +1 -0
  55. package/types/logger/_System.d.ts +10 -0
  56. package/types/logger/_System.d.ts.map +1 -0
  57. package/types/logger/chars.d.ts +93 -0
  58. package/types/logger/chars.d.ts.map +1 -0
  59. package/types/logger/index.d.ts +6 -0
  60. package/types/logger/index.d.ts.map +1 -0
  61. package/types/schema/_Schema.d.ts +11 -0
  62. package/types/schema/_Schema.d.ts.map +1 -0
  63. package/types/schema/index.d.ts +3 -0
  64. package/types/schema/index.d.ts.map +1 -0
  65. package/types/specification/_Properties.d.ts +158 -0
  66. package/types/specification/_Properties.d.ts.map +1 -0
  67. package/types/specification/index.d.ts +2 -0
  68. package/types/specification/index.d.ts.map +1 -0
@@ -0,0 +1,191 @@
1
+ //////////////////////////////////////////
2
+ // //
3
+ // //
4
+ // SYSTEM //
5
+ // //
6
+ // //
7
+ //////////////////////////////////////////
8
+
9
+ import { Timestamp } from './_Defs.js'
10
+ import { Settings } from './_Settings.js'
11
+ import { ANSI } from './_Defs.js'
12
+
13
+ function GetStack() {
14
+ const err = new Error()
15
+ const stack = err.stack.split('\n').slice(2).map(line => {
16
+ return ('').padStart(16) + line.trim()
17
+ }).join('\n')
18
+ return stack
19
+ }
20
+
21
+ const GetLine = (type, ...args) => {
22
+ const line = (type.toUpperCase()).padEnd(8) + args.join(' ')
23
+ const stack = (type == 'error' || type == 'debug' ? '\n' + GetStack() : '')
24
+ return line + stack
25
+ }
26
+
27
+ export function SystemLog(overrides = {} ) {
28
+
29
+ const config = { ...Settings, ...overrides }
30
+
31
+ const saved = {
32
+ log: console.log,
33
+ info: console.info,
34
+ error: console.error,
35
+ warn: console.warn,
36
+ trace: console.trace
37
+ }
38
+
39
+ globalThis.SAYLogger = saved
40
+
41
+ const filename = config.filename.replaceAll('$TIMESTAMP', Timestamp(...config.timestamp))
42
+
43
+ return new Promise( async resolve => {
44
+
45
+
46
+ if (typeof process !== 'undefined') {
47
+
48
+
49
+ const fs = await import('node:fs')
50
+ const path = await import('node:path')
51
+ const util = await import('node:util')
52
+
53
+ if (await fs.existsSync(filename)) throw Error(`${filename} already exists`)
54
+ const dir = path.dirname(filename)
55
+ try { await fs.mkdirSync(dir) } catch(err) {}
56
+ const output = fs.createWriteStream(filename, { flags: 'a' })
57
+
58
+ output.on('error', err => {
59
+ saved.error(err)
60
+ })
61
+
62
+ globalThis.SAYBuffer = {
63
+ log: (...args) => output.write(GetLine('log', ...args)+'\n'),
64
+ info: (...args) => output.write(GetLine('info', ...args)+'\n'),
65
+ error: (...args) => output.write(GetLine('error', ...args)+'\n'),
66
+ warn: (...args) => output.write(GetLine('warn', ...args)+'\n'),
67
+ }
68
+
69
+ console.log = (...args) => {
70
+ const line = GetLine('log', ...args)
71
+ output.write(line + '\n')
72
+ saved.log(...args)
73
+ }
74
+
75
+ console.error = (...args) => {
76
+ const line = GetLine('error', ...args)
77
+ output.write(line + '\n')
78
+ saved.error(...args)
79
+ }
80
+ console.warn = (...args) => {
81
+ const line = GetLine('warn', ...args)
82
+ output.write(line + '\n')
83
+ saved.warn(...args)
84
+ }
85
+ console.info = (...args) => {
86
+ const line = GetLine('info', ...args)
87
+ output.write(line + '\n')
88
+ saved.info(...args)
89
+ }
90
+
91
+ process.on('uncaughtException', async err => {
92
+ try {
93
+ console.error(err)
94
+ if (config?.onCrash) await config.onCrash()
95
+ } catch (err) {
96
+ saved.error('uncaught', err)
97
+ } finally {
98
+ if (output) output.end()
99
+ }
100
+ })
101
+
102
+ process.on('exit', async e => {
103
+ try {
104
+ if (config.onExit) await config.onExit(saypath)
105
+ } catch(err) {
106
+ saved.log('exit', e)
107
+ } finally {
108
+ if (output) output.end()
109
+ }
110
+ })
111
+
112
+ } else {
113
+
114
+ const buffer = []
115
+ const MAX_LINES = 200
116
+
117
+ const BufferPush = (line) => {
118
+ buffer.push(line)
119
+ if (buffer.length > MAX_LINES / 2) {
120
+ buffer = buffer.slice(MAX_LINES/-4) // remove 1/4
121
+ }
122
+ }
123
+
124
+ globalThis.SAYBuffer = {
125
+ log: (...args) => BufferPush(GetLine('log', ...args)),
126
+ info: (...args) => BufferPush(GetLine('info', ...args)),
127
+ error: (...args) => BufferPush(GetLine('error', ...args)),
128
+ warn: (...args) => BufferPush(GetLine('warn', ...args)),
129
+ }
130
+
131
+ console.log = (...args) => {
132
+ BufferPush(GetLine('log', ...args))
133
+ saved.log(...args)
134
+ }
135
+ console.error = (...args) => {
136
+ BufferPush(GetLine('error', ...args))
137
+ saved.error(...args)
138
+ }
139
+ console.warn = (...args) => {
140
+ BufferPush(GetLine('warn', ...args))
141
+ saved.warn(...args)
142
+ }
143
+ console.info = (...args) => {
144
+ BufferPush(GetLine('info', ...args))
145
+ saved.info(...args)
146
+ }
147
+
148
+ window.addEventListener('error', err => {
149
+ console.error(err.error.stack)
150
+ if (config.onCrash) config.onCrash(DownloadableLog(filename, buffer))
151
+ })
152
+
153
+ window.addEventListener('pagehide', async e => {
154
+ if (config.onExit) config.onExit(DownloadableLog(filename, buffer))
155
+ }, { capture: true })
156
+
157
+ }
158
+
159
+ resolve()
160
+ })
161
+ }
162
+
163
+ export const DownloadableLog = (filename, buffer) => {
164
+
165
+ const content = buffer.join('\n') + '\n'
166
+ const blob = new Blob([content], {type: 'text/plain'})
167
+ const url = URL.createObjectURL(blob)
168
+
169
+ const a = document.createElement('a')
170
+ a.href = url
171
+ a.download = filename
172
+ a.style.display = 'none'
173
+ document.body.appendChild(a)
174
+
175
+ return {
176
+ buffer,
177
+ filename,
178
+ content,
179
+ blob,
180
+ url,
181
+ download: () => {
182
+ a.click()
183
+ document.body.removeChild(a)
184
+ URL.revokeObjectURL(url)
185
+ }
186
+ }
187
+
188
+ }
189
+
190
+
191
+