51 lines
1.0 KiB
JavaScript
51 lines
1.0 KiB
JavaScript
'use strict'
|
|
|
|
const path = require('path')
|
|
|
|
/**
|
|
* Application storage layout (ADR architecture).
|
|
* $STORAGE/
|
|
* worlds/<id>/
|
|
* anvil/
|
|
* meta.json
|
|
* corestore/ (future)
|
|
* identity/ (future)
|
|
*/
|
|
|
|
function worldsRoot(storageDir) {
|
|
return path.join(storageDir, 'worlds')
|
|
}
|
|
|
|
function worldDir(storageDir, worldId) {
|
|
return path.join(worldsRoot(storageDir), safeId(worldId))
|
|
}
|
|
|
|
function worldAnvilDir(storageDir, worldId) {
|
|
return path.join(worldDir(storageDir, worldId), 'anvil')
|
|
}
|
|
|
|
function worldMetaPath(storageDir, worldId) {
|
|
return path.join(worldDir(storageDir, worldId), 'meta.json')
|
|
}
|
|
|
|
function safeId(id) {
|
|
const s = String(id || '')
|
|
.trim()
|
|
.toLowerCase()
|
|
.replace(/[^a-z0-9_-]+/g, '-')
|
|
.replace(/^-+|-+$/g, '')
|
|
if (!s || s === '.' || s === '..') {
|
|
throw new Error(`Invalid world id: ${id}`)
|
|
}
|
|
if (s.length > 64) throw new Error('World id too long (max 64)')
|
|
return s
|
|
}
|
|
|
|
module.exports = {
|
|
worldsRoot,
|
|
worldDir,
|
|
worldAnvilDir,
|
|
worldMetaPath,
|
|
safeId
|
|
}
|