bajo 2.23.0 → 2.25.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/.jsdoc.conf.json +2 -1
- package/class/app.js +220 -107
- package/class/bajo.js +257 -177
- package/class/base.js +85 -24
- package/class/cache.js +8 -16
- package/class/err.js +31 -10
- package/class/log.js +46 -38
- package/class/plugin.js +53 -30
- package/class/print.js +18 -15
- package/class/tools.js +17 -7
- package/docs/App.html +51 -1
- package/docs/Bajo.html +1 -1
- package/docs/Base.html +34 -1
- package/docs/Cache.html +1 -1
- package/docs/Err.html +2 -2
- package/docs/Log.html +2 -2
- package/docs/Main.html +3 -0
- package/docs/Plugin.html +1 -1
- package/docs/Print.html +1 -1
- package/docs/Tools.html +1 -1
- package/docs/class_app.js.html +222 -109
- package/docs/class_bajo.js.html +260 -179
- package/docs/class_base.js.html +87 -26
- package/docs/class_cache.js.html +10 -18
- package/docs/class_err.js.html +33 -12
- package/docs/class_log.js.html +48 -40
- package/docs/class_plugin.js.html +55 -32
- package/docs/class_print.js.html +20 -17
- package/docs/class_tools.js.html +19 -9
- package/docs/data/search.json +1 -1
- package/docs/global.html +22 -1
- package/docs/index.html +1 -1
- package/docs/index.js.html +53 -16
- package/docs/lib_applet-hook.js.html +328 -0
- package/docs/lib_config.js.html +167 -0
- package/docs/lib_helper.js.html +186 -192
- package/docs/module-Applet.html +3 -0
- package/docs/module-Helper.html +1 -8
- package/docs/module-Hook.html +1 -1
- package/docs/tutorial-01_WELCOME.html +3 -0
- package/docs/tutorial-02_GETTING_STARTED.html +125 -0
- package/docs/tutorial-03_USER_GUIDE.html +54 -0
- package/docs/tutorial-04_DEV_GUIDE.html +178 -0
- package/docs/tutorial-05_ECOSYSTEM.html +3 -0
- package/extend/bajo/intl/en-US.json +2 -1
- package/extend/bajo/intl/id.json +2 -1
- package/index.js +51 -14
- package/lib/applet-hook.js +325 -0
- package/lib/config.js +164 -0
- package/lib/helper.js +185 -191
- package/package.json +6 -2
- package/test/00-global-hooks.test.js +44 -0
- package/test/e2e/_run.js +19 -0
- package/test/e2e/e2e-applet-process.test.js +66 -0
- package/test/e2e/e2e-boot-process.test.js +62 -0
- package/test/e2e/e2e-hooks-process.test.js +68 -0
- package/test/integration/_setup.js +55 -0
- package/test/integration/aspect-01-boot-instance.test.js +24 -0
- package/test/integration/aspect-02-plugin-loading.test.js +24 -0
- package/test/integration/aspect-03-config-merge.test.js +27 -0
- package/test/integration/aspect-04-intl-loading.test.js +29 -0
- package/test/integration/aspect-05-cache-roundtrip.test.js +26 -0
- package/test/integration/aspect-06-log-file-save.test.js +28 -0
- package/test/integration/aspect-07-plugin-data-dir.test.js +25 -0
- package/test/integration/aspect-08-plugin-lookup.test.js +25 -0
- package/test/integration/aspect-09-config-formats.test.js +26 -0
- package/test/integration/aspect-10-hooks.test.js +38 -0
- package/test/unit/app.test.js +115 -0
- package/test/unit/applet-hook.test.js +10 -0
- package/test/unit/bajo.test.js +151 -0
- package/test/unit/base.test.js +90 -0
- package/test/unit/cache.test.js +89 -0
- package/test/unit/config.test.js +14 -0
- package/test/unit/err.test.js +57 -0
- package/test/unit/helper.test.js +116 -0
- package/test/unit/index.test.js +40 -0
- package/test/unit/log.test.js +84 -0
- package/test/unit/plugin.test.js +95 -0
- package/test/unit/print.test.js +72 -0
- package/test/unit/tools.test.js +29 -0
- package/wiki/CHANGES.md +10 -0
- package/wiki/tutorials/01_WELCOME.md +18 -0
- package/wiki/tutorials/02_GETTING_STARTED.md +366 -0
- package/wiki/tutorials/03_USER_GUIDE.md +262 -0
- package/wiki/tutorials/04_DEV_GUIDE.md +386 -0
- package/wiki/tutorials/05_ECOSYSTEM.md +95 -0
- package/wiki/tutorials/tutorials.json +17 -0
- package/docs/lib_hook.js.html +0 -231
- package/docs/module-Lib.html +0 -3
- package/lib/hook.js +0 -228
- package/test/app.test.js +0 -183
- package/test/bajo.test.js +0 -124
- package/test/base.test.js +0 -75
- package/test/cache.test.js +0 -94
- package/test/e2e.test.js +0 -137
- package/test/err.test.js +0 -73
- package/test/helper.test.js +0 -39
- package/test/import-module.test.js +0 -138
- package/test/integration.test.js +0 -218
- package/test/log.test.js +0 -119
- package/test/plugin.test.js +0 -116
- package/test/print.test.js +0 -100
- package/test/tools.test.js +0 -38
- package/wiki/CONFIG.md +0 -36
- package/wiki/DEV_GUIDE.md +0 -3
- package/wiki/ECOSYSTEM.md +0 -93
- package/wiki/GETTING-STARTED.md +0 -356
- package/wiki/USER-GUIDE.md +0 -256
package/test/app.test.js
DELETED
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
/* global describe, it, beforeEach, afterEach */
|
|
2
|
-
|
|
3
|
-
import os from 'node:os'
|
|
4
|
-
import path from 'node:path'
|
|
5
|
-
import { expect } from 'chai'
|
|
6
|
-
import fs from 'fs-extra'
|
|
7
|
-
|
|
8
|
-
import App from '../class/app.js'
|
|
9
|
-
import Plugin from '../class/plugin.js'
|
|
10
|
-
|
|
11
|
-
const createTempRoot = () => fs.mkdtempSync(path.join(os.tmpdir(), 'bajo-app-test-'))
|
|
12
|
-
|
|
13
|
-
describe('App', () => {
|
|
14
|
-
let rootDir
|
|
15
|
-
let app
|
|
16
|
-
|
|
17
|
-
beforeEach(() => {
|
|
18
|
-
rootDir = createTempRoot()
|
|
19
|
-
app = new App({ cwd: rootDir })
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
afterEach(() => {
|
|
23
|
-
if (rootDir) fs.rmSync(rootDir, { recursive: true, force: true })
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
it('adds plugins and base class mappings', () => {
|
|
27
|
-
const plugin = new Plugin('my-plugin', app)
|
|
28
|
-
const MyBase = class {}
|
|
29
|
-
|
|
30
|
-
app.addPlugin(plugin, MyBase)
|
|
31
|
-
|
|
32
|
-
expect(app.myPlugin).to.equal(plugin)
|
|
33
|
-
expect(app.baseClass.MyPlugin).to.equal(MyBase)
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
it('throws when adding the same plugin namespace twice', () => {
|
|
37
|
-
const plugin = new Plugin('dup-plugin', app)
|
|
38
|
-
app.addPlugin(plugin)
|
|
39
|
-
|
|
40
|
-
expect(() => app.addPlugin(plugin)).to.throw("Plugin 'dupPlugin' added already")
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
it('lists namespaces and loaded plugins', () => {
|
|
44
|
-
app.pluginPkgs = ['alpha-plugin', 'beta-plugin']
|
|
45
|
-
app.alphaPlugin = { ns: 'alphaPlugin' }
|
|
46
|
-
app.betaPlugin = { ns: 'betaPlugin' }
|
|
47
|
-
|
|
48
|
-
expect(app.getAllNs()).to.deep.equal(['alphaPlugin', 'betaPlugin'])
|
|
49
|
-
expect(app.getPlugins()).to.deep.equal([app.alphaPlugin, app.betaPlugin])
|
|
50
|
-
expect(app.getPlugins(['betaPlugin'])).to.deep.equal([app.betaPlugin])
|
|
51
|
-
expect(app.getAllPlugins()).to.deep.equal([app.alphaPlugin, app.betaPlugin])
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
it('gets plugin by namespace, alias, and package name', () => {
|
|
55
|
-
const plugin = new Plugin('my-plugin', app)
|
|
56
|
-
plugin.alias = 'mine'
|
|
57
|
-
app.addPlugin(plugin)
|
|
58
|
-
app.bajo = { error: (code, value) => new Error(`${code}:${value}`) }
|
|
59
|
-
|
|
60
|
-
expect(app.getPlugin('myPlugin')).to.equal(plugin)
|
|
61
|
-
expect(app.getPlugin('mine')).to.equal(plugin)
|
|
62
|
-
expect(app.getPlugin('my-plugin')).to.equal(plugin)
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
it('supports silent and throwing behavior for missing plugin lookup', () => {
|
|
66
|
-
app.bajo = { error: (code, value) => new Error(`${code}:${value}`) }
|
|
67
|
-
|
|
68
|
-
expect(app.getPlugin('missing', true)).to.equal(false)
|
|
69
|
-
expect(() => app.getPlugin('missing')).to.throw('pluginWithNameAliasNotLoaded%s:missing')
|
|
70
|
-
})
|
|
71
|
-
|
|
72
|
-
it('creates and returns plugin data directory', () => {
|
|
73
|
-
const plugin = new Plugin('my-plugin', app)
|
|
74
|
-
app.addPlugin(plugin)
|
|
75
|
-
app.bajo = { dir: { data: path.join(rootDir, 'data') } }
|
|
76
|
-
|
|
77
|
-
const dir = app.getPluginDataDir('myPlugin')
|
|
78
|
-
|
|
79
|
-
expect(dir).to.equal(path.join(rootDir, 'data', 'plugins', 'myPlugin'))
|
|
80
|
-
expect(fs.existsSync(dir)).to.equal(true)
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
it('resolves plugin-scoped files and node_modules fallback paths', async () => {
|
|
84
|
-
const plugin = new Plugin('my-plugin', app)
|
|
85
|
-
plugin.dir = { pkg: path.join(rootDir, 'plugins', 'my-plugin') }
|
|
86
|
-
await fs.ensureDir(plugin.dir.pkg)
|
|
87
|
-
app.addPlugin(plugin)
|
|
88
|
-
|
|
89
|
-
app.bajo = {
|
|
90
|
-
breakNsPath: () => ({ ns: 'myPlugin', path: '/src/feature.js' })
|
|
91
|
-
}
|
|
92
|
-
expect(app.getPluginFile('myPlugin:/src/feature.js')).to.equal(path.join(plugin.dir.pkg, 'src/feature.js'))
|
|
93
|
-
|
|
94
|
-
const fallbackFile = path.join(rootDir, 'plugins', 'dep', 'index.js')
|
|
95
|
-
await fs.ensureDir(path.dirname(fallbackFile))
|
|
96
|
-
await fs.writeFile(fallbackFile, 'export default 1', 'utf8')
|
|
97
|
-
app.bajo.breakNsPath = () => ({ ns: 'myPlugin', path: 'node_modules/dep/index.js' })
|
|
98
|
-
|
|
99
|
-
expect(app.getPluginFile('myPlugin:node_modules/dep/index.js')).to.equal(`${plugin.dir.pkg}/../dep/index.js`)
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
it('translates and checks translation existence', () => {
|
|
103
|
-
app.pluginPkgs = ['my-plugin']
|
|
104
|
-
app.myPlugin = {
|
|
105
|
-
intl: {
|
|
106
|
-
id: {
|
|
107
|
-
greet: 'Halo %s',
|
|
108
|
-
list: 'Daftar: %s'
|
|
109
|
-
},
|
|
110
|
-
en: {
|
|
111
|
-
greet: 'Hello %s'
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
app.bajo = {
|
|
116
|
-
config: {
|
|
117
|
-
lang: 'id',
|
|
118
|
-
intl: {
|
|
119
|
-
fallback: 'en',
|
|
120
|
-
supported: ['id', 'en']
|
|
121
|
-
}
|
|
122
|
-
},
|
|
123
|
-
join: (items) => items.join(', '),
|
|
124
|
-
log: { warn: () => {} }
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
expect(app.t('myPlugin', 'greet', 'Ardhi')).to.equal('Halo Ardhi')
|
|
128
|
-
expect(app.t('myPlugin', 'list', ['A', 'B'])).to.equal('Daftar: A, B')
|
|
129
|
-
expect(app.t('myPlugin', 'unknown %s', 'X')).to.equal('unknown X')
|
|
130
|
-
expect(app.te('myPlugin', 'greet')).to.equal(true)
|
|
131
|
-
expect(app.te('myPlugin', 'not.exists')).to.equal(false)
|
|
132
|
-
})
|
|
133
|
-
|
|
134
|
-
it('returns supported config formats', () => {
|
|
135
|
-
app.configHandlers = [{ ext: '.json' }, { ext: '.yaml' }]
|
|
136
|
-
|
|
137
|
-
expect(app.getConfigFormats()).to.deep.equal(['.json', '.yaml'])
|
|
138
|
-
})
|
|
139
|
-
|
|
140
|
-
it('starts plugin by namespace with arguments', () => {
|
|
141
|
-
const calls = []
|
|
142
|
-
app.sample = {
|
|
143
|
-
start: (...args) => {
|
|
144
|
-
calls.push(args)
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
app.startPlugin('sample', 1, 'x')
|
|
149
|
-
|
|
150
|
-
expect(calls).to.deep.equal([[1, 'x']])
|
|
151
|
-
})
|
|
152
|
-
|
|
153
|
-
it('kills process with signal and exits abruptly when true is passed', () => {
|
|
154
|
-
const originalKill = process.kill
|
|
155
|
-
const originalExit = process.exit
|
|
156
|
-
const killCalls = []
|
|
157
|
-
let exitArg
|
|
158
|
-
|
|
159
|
-
process.kill = (...args) => {
|
|
160
|
-
killCalls.push(args)
|
|
161
|
-
return true
|
|
162
|
-
}
|
|
163
|
-
process.exit = (arg) => {
|
|
164
|
-
exitArg = arg
|
|
165
|
-
throw new Error('__EXIT__')
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
try {
|
|
169
|
-
app.exit('SIGTERM')
|
|
170
|
-
try {
|
|
171
|
-
app.exit(true)
|
|
172
|
-
} catch (err) {
|
|
173
|
-
if (err.message !== '__EXIT__') throw err
|
|
174
|
-
}
|
|
175
|
-
} finally {
|
|
176
|
-
process.kill = originalKill
|
|
177
|
-
process.exit = originalExit
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
expect(killCalls).to.deep.equal([[process.pid, 'SIGTERM']])
|
|
181
|
-
expect(exitArg).to.equal('1')
|
|
182
|
-
})
|
|
183
|
-
})
|
package/test/bajo.test.js
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
/* global describe, it, beforeEach, afterEach */
|
|
2
|
-
|
|
3
|
-
import os from 'node:os'
|
|
4
|
-
import path from 'node:path'
|
|
5
|
-
import { expect } from 'chai'
|
|
6
|
-
import fs from 'fs-extra'
|
|
7
|
-
|
|
8
|
-
import App from '../class/app.js'
|
|
9
|
-
import Bajo from '../class/bajo.js'
|
|
10
|
-
|
|
11
|
-
const createTempRoot = () => fs.mkdtempSync(path.join(os.tmpdir(), 'bajo-core-test-'))
|
|
12
|
-
|
|
13
|
-
describe('Bajo', () => {
|
|
14
|
-
let root
|
|
15
|
-
let app
|
|
16
|
-
let bajo
|
|
17
|
-
|
|
18
|
-
beforeEach(() => {
|
|
19
|
-
root = createTempRoot()
|
|
20
|
-
app = new App({ cwd: root })
|
|
21
|
-
bajo = new Bajo(app)
|
|
22
|
-
app.bajo = bajo
|
|
23
|
-
app.main = { dir: { pkg: root } }
|
|
24
|
-
app.mainNs = 'main'
|
|
25
|
-
app.main = { pkgName: 'main', dir: { pkg: root } }
|
|
26
|
-
app.demo = {
|
|
27
|
-
dir: { pkg: path.join(root, 'demo') },
|
|
28
|
-
greet: (name) => `hi ${name}`
|
|
29
|
-
}
|
|
30
|
-
bajo.config = {
|
|
31
|
-
env: 'dev',
|
|
32
|
-
lang: 'en-US',
|
|
33
|
-
intl: {
|
|
34
|
-
unitSys: { 'en-US': 'metric' },
|
|
35
|
-
format: {
|
|
36
|
-
emptyValue: '-',
|
|
37
|
-
datetime: { dateStyle: 'medium', timeStyle: 'short', timeZone: 'UTC' },
|
|
38
|
-
date: { dateStyle: 'medium', timeZone: 'UTC' },
|
|
39
|
-
time: { timeStyle: 'short', timeZone: 'UTC' },
|
|
40
|
-
float: { maximumFractionDigits: 2 },
|
|
41
|
-
double: { maximumFractionDigits: 2 },
|
|
42
|
-
integer: {},
|
|
43
|
-
smallint: {}
|
|
44
|
-
}
|
|
45
|
-
},
|
|
46
|
-
log: {
|
|
47
|
-
level: 'trace'
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
bajo.t = (text) => text
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
afterEach(() => {
|
|
54
|
-
if (root) fs.rmSync(root, { recursive: true, force: true })
|
|
55
|
-
})
|
|
56
|
-
|
|
57
|
-
it('builds and breaks namespace paths', () => {
|
|
58
|
-
const built = bajo.buildNsPath({ ns: 'demo', subNs: 'api', subSubNs: 'v1', path: '/users' })
|
|
59
|
-
const broken = bajo.breakNsPath('demo.api:/users/:id|42?q=x')
|
|
60
|
-
|
|
61
|
-
expect(built).to.equal('demo.api.v1:/users')
|
|
62
|
-
expect(broken).to.include({ ns: 'demo', subNs: 'api', path: '/users/:id', realPath: '/users/42' })
|
|
63
|
-
expect(broken.qs).to.deep.equal({ q: 'x' })
|
|
64
|
-
})
|
|
65
|
-
|
|
66
|
-
it('breaks ns path details from filename', () => {
|
|
67
|
-
const info = bajo.breakNsPathFromFile({
|
|
68
|
-
file: 'x/extend/route/demo.api@user-list.js',
|
|
69
|
-
dir: 'x/extend/',
|
|
70
|
-
suffix: '',
|
|
71
|
-
ns: 'demo',
|
|
72
|
-
getType: true
|
|
73
|
-
})
|
|
74
|
-
|
|
75
|
-
expect(info).to.include({ ns: 'demo', subNs: 'api', path: 'userList', type: 'route' })
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
it('returns unit format and formats values', () => {
|
|
79
|
-
const unit = bajo.getUnitFormat({ lang: 'en-US' })
|
|
80
|
-
const joined = bajo.format(['a', 'b'], 'array')
|
|
81
|
-
const integer = bajo.format(12345, 'integer')
|
|
82
|
-
|
|
83
|
-
expect(unit.unitSys).to.equal('metric')
|
|
84
|
-
expect(unit.format).to.be.an('object')
|
|
85
|
-
expect(joined).to.equal('a, b')
|
|
86
|
-
expect(integer).to.be.a('string')
|
|
87
|
-
})
|
|
88
|
-
|
|
89
|
-
it('gets method by ns path and supports non-throw mode', () => {
|
|
90
|
-
const fn = bajo.getMethod('demo:greet')
|
|
91
|
-
const miss = bajo.getMethod('demo:notFound', false)
|
|
92
|
-
|
|
93
|
-
expect(fn('a')).to.equal('hi a')
|
|
94
|
-
expect(miss).to.equal(undefined)
|
|
95
|
-
})
|
|
96
|
-
|
|
97
|
-
it('validates app/plugin package and utility helpers', async () => {
|
|
98
|
-
const appPkg = path.join(root, 'appdir')
|
|
99
|
-
const pluginPkg = path.join(root, 'plugindir')
|
|
100
|
-
await fs.ensureDir(appPkg)
|
|
101
|
-
await fs.ensureDir(pluginPkg)
|
|
102
|
-
await fs.ensureDir(path.join(root, 'empty'))
|
|
103
|
-
await fs.writeJson(path.join(appPkg, 'package.json'), { bajo: { type: 'app' } })
|
|
104
|
-
await fs.writeJson(path.join(pluginPkg, 'package.json'), { bajo: { type: 'plugin' } })
|
|
105
|
-
|
|
106
|
-
expect(bajo.isValidApp(appPkg)).to.equal(true)
|
|
107
|
-
expect(bajo.isValidPlugin(pluginPkg)).to.equal(true)
|
|
108
|
-
expect(bajo.join(['a', 'b', 'c'])).to.equal('a, b and c')
|
|
109
|
-
expect(bajo.numUnit('10mb', 'kb')).to.equal('10mb')
|
|
110
|
-
})
|
|
111
|
-
|
|
112
|
-
it('reads/writes json helpers', async () => {
|
|
113
|
-
const file = path.join(root, 'x.json')
|
|
114
|
-
const content = bajo.toJson({ a: 1 })
|
|
115
|
-
await fs.writeFile(file, content, 'utf8')
|
|
116
|
-
|
|
117
|
-
expect(bajo.fromJson(content)).to.deep.equal({ a: 1 })
|
|
118
|
-
expect(bajo.readJson(file)).to.deep.equal({ a: 1 })
|
|
119
|
-
|
|
120
|
-
const out = path.join(root, 'out.json')
|
|
121
|
-
bajo.toJson({ b: 2 }, { writeToFile: true, saveAsFile: out })
|
|
122
|
-
expect(fs.existsSync(out)).to.equal(true)
|
|
123
|
-
})
|
|
124
|
-
})
|
package/test/base.test.js
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
/* global describe, it, beforeEach */
|
|
2
|
-
|
|
3
|
-
import { expect } from 'chai'
|
|
4
|
-
import lodash from 'lodash'
|
|
5
|
-
|
|
6
|
-
import Base from '../class/base.js'
|
|
7
|
-
|
|
8
|
-
describe('Base', () => {
|
|
9
|
-
let app
|
|
10
|
-
|
|
11
|
-
beforeEach(() => {
|
|
12
|
-
app = {
|
|
13
|
-
mainNs: 'main',
|
|
14
|
-
getAllNs: () => ['alpha', 'beta'],
|
|
15
|
-
lib: {
|
|
16
|
-
_: lodash,
|
|
17
|
-
aneka: {
|
|
18
|
-
defaultsDeep: (...objs) => lodash.defaultsDeep(...objs)
|
|
19
|
-
},
|
|
20
|
-
parseObject: (obj) => obj
|
|
21
|
-
},
|
|
22
|
-
bajo: {
|
|
23
|
-
dir: { base: '/app', data: '/data' },
|
|
24
|
-
config: { env: 'dev' },
|
|
25
|
-
log: { trace: () => {} },
|
|
26
|
-
getModuleDir: () => '/modules/my-plugin',
|
|
27
|
-
readAllConfigs: async () => ({ fromFile: true })
|
|
28
|
-
},
|
|
29
|
-
env: {
|
|
30
|
-
myPlugin: { x: 3 }
|
|
31
|
-
},
|
|
32
|
-
argv: {
|
|
33
|
-
myPlugin: { y: 2 }
|
|
34
|
-
},
|
|
35
|
-
options: {
|
|
36
|
-
config: {
|
|
37
|
-
myPlugin: { z: 1 }
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
it('initializes defaults', () => {
|
|
44
|
-
const base = new Base('my-plugin', app)
|
|
45
|
-
|
|
46
|
-
expect(base.ns).to.equal('myPlugin')
|
|
47
|
-
expect(base.dependencies).to.deep.equal([])
|
|
48
|
-
expect(base.state).to.deep.equal({})
|
|
49
|
-
expect(base.pkg).to.deep.equal({})
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
it('loads config and sets package/data directories', async () => {
|
|
53
|
-
const base = new Base('my-plugin', app)
|
|
54
|
-
base.config = { title: 'my app', z: 9, x: 0, y: 0 }
|
|
55
|
-
|
|
56
|
-
await base.loadConfig()
|
|
57
|
-
|
|
58
|
-
expect(base.dir).to.deep.equal({
|
|
59
|
-
pkg: '/modules/my-plugin',
|
|
60
|
-
data: '/data/plugins/myPlugin'
|
|
61
|
-
})
|
|
62
|
-
expect(base.getConfig('x')).to.equal(3)
|
|
63
|
-
expect(base.getConfig('y')).to.equal(2)
|
|
64
|
-
expect(base.getConfig('z')).to.equal(1)
|
|
65
|
-
expect(base.getConfig('title')).to.equal('my app')
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
it('runs no-op lifecycle methods and exits by disposing', async () => {
|
|
69
|
-
const base = new Base('my-plugin', app)
|
|
70
|
-
|
|
71
|
-
await base.init()
|
|
72
|
-
await base.start()
|
|
73
|
-
await base.stop()
|
|
74
|
-
})
|
|
75
|
-
})
|
package/test/cache.test.js
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
/* global describe, it, beforeEach, afterEach */
|
|
2
|
-
|
|
3
|
-
import os from 'node:os'
|
|
4
|
-
import path from 'node:path'
|
|
5
|
-
import { expect } from 'chai'
|
|
6
|
-
import fs from 'fs-extra'
|
|
7
|
-
import fastGlob from 'fast-glob'
|
|
8
|
-
import lodash from 'lodash'
|
|
9
|
-
|
|
10
|
-
import Cache from '../class/cache.js'
|
|
11
|
-
|
|
12
|
-
const createTempRoot = () => fs.mkdtempSync(path.join(os.tmpdir(), 'bajo-cache-test-'))
|
|
13
|
-
|
|
14
|
-
describe('Cache', () => {
|
|
15
|
-
let root
|
|
16
|
-
let app
|
|
17
|
-
let cache
|
|
18
|
-
|
|
19
|
-
beforeEach(() => {
|
|
20
|
-
root = createTempRoot()
|
|
21
|
-
app = {
|
|
22
|
-
getPluginDataDir: () => path.join(root, 'bajo'),
|
|
23
|
-
bajo: {
|
|
24
|
-
breakNsPath: (name) => {
|
|
25
|
-
const [nsPart, p] = name.split(':')
|
|
26
|
-
const [ns, subNs] = nsPart.split('.')
|
|
27
|
-
return { ns, subNs, path: p }
|
|
28
|
-
}
|
|
29
|
-
},
|
|
30
|
-
lib: {
|
|
31
|
-
aneka: {
|
|
32
|
-
parseDuration: (v) => typeof v === 'number' ? v : 0
|
|
33
|
-
},
|
|
34
|
-
fs,
|
|
35
|
-
fastGlob,
|
|
36
|
-
_: lodash
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
cache = new Cache(app)
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
afterEach(() => {
|
|
43
|
-
if (root) fs.rmSync(root, { recursive: true, force: true })
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
it('prepares cache location only for namespaced key and non-zero ttl', () => {
|
|
47
|
-
expect(cache.prep('main:path', 0)).to.equal(undefined)
|
|
48
|
-
expect(cache.prep('main:path', 1000)).to.equal(undefined)
|
|
49
|
-
|
|
50
|
-
const prep = cache.prep('main.api:path/to/item', 1000)
|
|
51
|
-
expect(prep.file).to.include('/main/api/1000/path/to/item')
|
|
52
|
-
expect(fs.existsSync(prep.dir)).to.equal(true)
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
it('saves and loads json/object content', async () => {
|
|
56
|
-
await cache.save('main.api:item', { a: 1 }, 1000)
|
|
57
|
-
const item = await cache.load('main.api:item', 1000)
|
|
58
|
-
|
|
59
|
-
expect(item).to.deep.equal({ a: 1 })
|
|
60
|
-
})
|
|
61
|
-
|
|
62
|
-
it('returns undefined for expired cache and removes ttl directory', async () => {
|
|
63
|
-
await cache.save('main.api:item', 'ok', 1)
|
|
64
|
-
const { dir } = cache.prep('main.api:item', 1)
|
|
65
|
-
const old = Date.now() - 5000
|
|
66
|
-
fs.utimesSync(dir, old / 1000, old / 1000)
|
|
67
|
-
|
|
68
|
-
const result = await cache.load('main.api:item', 1)
|
|
69
|
-
|
|
70
|
-
expect(result).to.equal(undefined)
|
|
71
|
-
expect(fs.existsSync(dir)).to.equal(false)
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
it('purges by name and wildcard', () => {
|
|
75
|
-
const a = cache.prep('main.api:a', 1000)
|
|
76
|
-
const b = cache.prep('main.web:b', 1000)
|
|
77
|
-
fs.writeFileSync(a.file, 'a', 'utf8')
|
|
78
|
-
fs.writeFileSync(b.file, 'b', 'utf8')
|
|
79
|
-
|
|
80
|
-
cache.purge('main')
|
|
81
|
-
expect(fs.existsSync(cache.getRootDir() + '/main')).to.equal(false)
|
|
82
|
-
|
|
83
|
-
const c = cache.prep('other.api:c', 1000)
|
|
84
|
-
fs.writeFileSync(c.file, 'c', 'utf8')
|
|
85
|
-
cache._purgeItem('*')
|
|
86
|
-
expect(fs.existsSync(cache.getRootDir() + '/other')).to.equal(false)
|
|
87
|
-
})
|
|
88
|
-
|
|
89
|
-
it('disposes app reference', async () => {
|
|
90
|
-
await cache.dispose()
|
|
91
|
-
|
|
92
|
-
expect(cache.app).to.equal(null)
|
|
93
|
-
})
|
|
94
|
-
})
|
package/test/e2e.test.js
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
/* global describe, it, beforeEach, afterEach */
|
|
2
|
-
|
|
3
|
-
import os from 'node:os'
|
|
4
|
-
import path from 'node:path'
|
|
5
|
-
import { spawn } from 'node:child_process'
|
|
6
|
-
import { pathToFileURL } from 'node:url'
|
|
7
|
-
import { expect } from 'chai'
|
|
8
|
-
import fs from 'fs-extra'
|
|
9
|
-
|
|
10
|
-
const createTempRoot = () => fs.mkdtempSync(path.join(os.tmpdir(), 'bajo-e2e-test-'))
|
|
11
|
-
|
|
12
|
-
const runNode = (cwd, file, timeoutMs = 8000) => {
|
|
13
|
-
return new Promise((resolve) => {
|
|
14
|
-
const child = spawn(process.execPath, [file], {
|
|
15
|
-
cwd,
|
|
16
|
-
stdio: ['ignore', 'pipe', 'pipe']
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
let stdout = ''
|
|
20
|
-
let stderr = ''
|
|
21
|
-
|
|
22
|
-
child.stdout.on('data', chunk => {
|
|
23
|
-
stdout += chunk.toString()
|
|
24
|
-
})
|
|
25
|
-
child.stderr.on('data', chunk => {
|
|
26
|
-
stderr += chunk.toString()
|
|
27
|
-
})
|
|
28
|
-
const timer = setTimeout(() => {
|
|
29
|
-
child.kill('SIGTERM')
|
|
30
|
-
resolve({ code: null, stdout, stderr, timedOut: true })
|
|
31
|
-
}, timeoutMs)
|
|
32
|
-
child.on('close', code => {
|
|
33
|
-
clearTimeout(timer)
|
|
34
|
-
resolve({ code, stdout, stderr, timedOut: false })
|
|
35
|
-
})
|
|
36
|
-
})
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
describe('E2E', () => {
|
|
40
|
-
let rootDir
|
|
41
|
-
|
|
42
|
-
beforeEach(() => {
|
|
43
|
-
rootDir = createTempRoot()
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
afterEach(() => {
|
|
47
|
-
if (rootDir) fs.rmSync(rootDir, { recursive: true, force: true })
|
|
48
|
-
})
|
|
49
|
-
|
|
50
|
-
it('boots a real app in a separate process and runs a real plugin end to end', async function () {
|
|
51
|
-
this.timeout(12000)
|
|
52
|
-
const packageEntry = pathToFileURL(path.join('/mnt/d/Projects/Bajo/bajo', 'index.js')).href
|
|
53
|
-
const pluginName = 'demo-e2e-plugin'
|
|
54
|
-
const pluginDir = path.join(rootDir, 'node_modules', pluginName)
|
|
55
|
-
const appMainDir = path.join(rootDir, 'main')
|
|
56
|
-
const outputFile = path.join(rootDir, 'e2e-output.txt')
|
|
57
|
-
|
|
58
|
-
await fs.ensureDir(pluginDir)
|
|
59
|
-
await fs.ensureDir(appMainDir)
|
|
60
|
-
|
|
61
|
-
await fs.writeJson(path.join(rootDir, 'package.json'), {
|
|
62
|
-
name: 'bajo-e2e-app',
|
|
63
|
-
type: 'module',
|
|
64
|
-
bajo: {
|
|
65
|
-
plugins: [pluginName]
|
|
66
|
-
}
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
await fs.writeJson(path.join(pluginDir, 'package.json'), {
|
|
70
|
-
name: pluginName,
|
|
71
|
-
version: '1.0.0',
|
|
72
|
-
type: 'module',
|
|
73
|
-
main: 'index.js'
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
await fs.writeFile(path.join(pluginDir, 'index.js'), `
|
|
77
|
-
async function factory (pkgName) {
|
|
78
|
-
const me = this
|
|
79
|
-
|
|
80
|
-
return class E2ePlugin extends this.app.baseClass.Base {
|
|
81
|
-
constructor () {
|
|
82
|
-
super(pkgName, me.app)
|
|
83
|
-
this.config = {}
|
|
84
|
-
this.start = async () => {
|
|
85
|
-
await this.app.lib.fs.writeFile(this.app.dir + '/e2e-output.txt', 'started:' + this.ns, 'utf8')
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export default factory
|
|
92
|
-
`, 'utf8')
|
|
93
|
-
|
|
94
|
-
await fs.writeFile(path.join(appMainDir, 'index.js'), `
|
|
95
|
-
async function factory (pkgName) {
|
|
96
|
-
const me = this
|
|
97
|
-
|
|
98
|
-
return class Main extends this.app.baseClass.Base {
|
|
99
|
-
constructor () {
|
|
100
|
-
super(pkgName, me.app)
|
|
101
|
-
this.config = {}
|
|
102
|
-
this.start = async () => {
|
|
103
|
-
console.log('MAIN_STARTED')
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export default factory
|
|
110
|
-
`, 'utf8')
|
|
111
|
-
|
|
112
|
-
await fs.ensureDir(path.join(rootDir, 'data', 'config'))
|
|
113
|
-
await fs.writeJson(path.join(rootDir, 'data', 'config', 'bajo.json'), {
|
|
114
|
-
log: { level: 'silent', save: false },
|
|
115
|
-
exitHandler: false,
|
|
116
|
-
cache: { purgeIntvDur: '1h' }
|
|
117
|
-
})
|
|
118
|
-
|
|
119
|
-
await fs.writeFile(path.join(rootDir, 'run-app.js'), `
|
|
120
|
-
import boot from ${JSON.stringify(packageEntry)}
|
|
121
|
-
|
|
122
|
-
const app = await boot({ cwd: process.cwd() })
|
|
123
|
-
console.log('BOOT_OK:' + !!app.getPlugin(${JSON.stringify(pluginName)}, true))
|
|
124
|
-
process.exit(0)
|
|
125
|
-
`, 'utf8')
|
|
126
|
-
|
|
127
|
-
const result = await runNode(rootDir, 'run-app.js')
|
|
128
|
-
|
|
129
|
-
expect(result.timedOut).to.equal(false)
|
|
130
|
-
expect(result.code).to.equal(0)
|
|
131
|
-
expect(result.stderr).to.equal('')
|
|
132
|
-
expect(result.stdout).to.include('MAIN_STARTED')
|
|
133
|
-
expect(result.stdout).to.include('BOOT_OK:true')
|
|
134
|
-
expect(fs.existsSync(outputFile)).to.equal(true)
|
|
135
|
-
expect(fs.readFileSync(outputFile, 'utf8')).to.match(/^started:/)
|
|
136
|
-
})
|
|
137
|
-
})
|
package/test/err.test.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
/* global describe, it */
|
|
2
|
-
|
|
3
|
-
import { expect } from 'chai'
|
|
4
|
-
import lodash from 'lodash'
|
|
5
|
-
|
|
6
|
-
import Err from '../class/err.js'
|
|
7
|
-
|
|
8
|
-
describe('Err', () => {
|
|
9
|
-
const makePlugin = () => ({
|
|
10
|
-
ns: 'myPlugin',
|
|
11
|
-
app: {
|
|
12
|
-
lib: {
|
|
13
|
-
_: lodash,
|
|
14
|
-
aneka: { without: (arr) => arr.filter(Boolean) }
|
|
15
|
-
},
|
|
16
|
-
exit: () => {}
|
|
17
|
-
},
|
|
18
|
-
t: (msg, ctx) => {
|
|
19
|
-
if (msg === 'error') return 'Error'
|
|
20
|
-
if (msg === 'fieldError%s%s') return `${ctx}`
|
|
21
|
-
return msg
|
|
22
|
-
}
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
it('writes error object with payload and metadata', () => {
|
|
26
|
-
const plugin = makePlugin()
|
|
27
|
-
const err = new Err(plugin, 'hello %s', 'x', { code: 'E1', foo: 'bar' }).write()
|
|
28
|
-
|
|
29
|
-
expect(err).to.be.instanceOf(Error)
|
|
30
|
-
expect(err.message).to.equal('hello %s')
|
|
31
|
-
expect(err.code).to.equal('E1')
|
|
32
|
-
expect(err.foo).to.equal('bar')
|
|
33
|
-
expect(err.ns).to.equal('myPlugin')
|
|
34
|
-
expect(err.orgMessage).to.equal('hello %s')
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
it('formats details payload into detailsMessage', () => {
|
|
38
|
-
const plugin = makePlugin()
|
|
39
|
-
plugin.t = (msg) => {
|
|
40
|
-
if (msg === 'error') return 'Error'
|
|
41
|
-
if (msg === 'fieldError%s%s') return 'field-error'
|
|
42
|
-
if (msg.startsWith('validation.')) return 'invalid value'
|
|
43
|
-
return msg
|
|
44
|
-
}
|
|
45
|
-
const item = {
|
|
46
|
-
type: 'string.base',
|
|
47
|
-
message: '~invalid value',
|
|
48
|
-
context: { key: 'name', value: 1 }
|
|
49
|
-
}
|
|
50
|
-
const err = new Err(plugin, 'bad', { details: [item] }).write()
|
|
51
|
-
|
|
52
|
-
expect(err).to.have.property('detailsMessage').that.includes('Error:')
|
|
53
|
-
expect(err.details[0]).to.include({ field: 'name', error: 'invalid value', value: 1 })
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
it('prints and exits on fatal', () => {
|
|
57
|
-
const plugin = makePlugin()
|
|
58
|
-
let exitArg
|
|
59
|
-
plugin.app.exit = (arg) => { exitArg = arg }
|
|
60
|
-
const originalError = console.error
|
|
61
|
-
let called = false
|
|
62
|
-
console.error = () => { called = true }
|
|
63
|
-
|
|
64
|
-
try {
|
|
65
|
-
new Err(plugin, 'boom').fatal()
|
|
66
|
-
} finally {
|
|
67
|
-
console.error = originalError
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
expect(called).to.equal(true)
|
|
71
|
-
expect(exitArg).to.equal(true)
|
|
72
|
-
})
|
|
73
|
-
})
|