mirror of
https://codeberg.org/timelimit/timelimit-server.git
synced 2026-08-31 19:03:45 +02:00
Compare commits
2
Commits
2021-01-27
...
2021-02-01
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
964397cfa9 | ||
|
|
99762d5d36 |
@@ -1,3 +1,4 @@
|
|||||||
build
|
build
|
||||||
node_modules
|
node_modules
|
||||||
test.db
|
test.db
|
||||||
|
tempdb
|
||||||
|
|||||||
+1
-1
@@ -5,7 +5,7 @@
|
|||||||
"description": "",
|
"description": "",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node ./build/index.js",
|
"start": "node ./build/index.js",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1",
|
"test": "node scripts/test-launch-with-different-databases.js",
|
||||||
"lint": "tslint --project .",
|
"lint": "tslint --project .",
|
||||||
"lint:fix": "tslint --project . --fix",
|
"lint:fix": "tslint --project . --fix",
|
||||||
"build": "npm run build:clean && npm run build:json && npm run build:ts && npm run lint && npm run build:doc",
|
"build": "npm run build:clean && npm run build:json && npm run build:ts && npm run lint && npm run build:doc",
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
* server component for the TimeLimit App
|
||||||
|
* Copyright (C) 2019 - 2021 Jonas Lochmann
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
* published by the Free Software Foundation, version 3 of the License.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const { databaseLaunchers } = require('./util/database')
|
||||||
|
const { startMainApp } = require('./util/mainapp.js')
|
||||||
|
|
||||||
|
// use export PATH="$PATH:/usr/lib/postgresql/11/bin"
|
||||||
|
// if the postgres binaries are not in the path
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
let log = ''
|
||||||
|
|
||||||
|
for (const launcher of databaseLaunchers) {
|
||||||
|
const database = await launcher()
|
||||||
|
|
||||||
|
console.log('Test with ' + database.type)
|
||||||
|
|
||||||
|
try {
|
||||||
|
const task = await startMainApp({
|
||||||
|
DATABASE_URL: database.connectionUrl
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log('test successfull')
|
||||||
|
log += 'Worked with ' + database.type + '\n'
|
||||||
|
|
||||||
|
task.shutdown()
|
||||||
|
} catch (ex) {
|
||||||
|
log += 'Failure with ' + database.type + '\n'
|
||||||
|
console.warn('test failed', ex)
|
||||||
|
}
|
||||||
|
|
||||||
|
database.shutdown()
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('\nRESULTS\n\n' + log)
|
||||||
|
process.exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
main().catch((ex) => {
|
||||||
|
console.warn(ex)
|
||||||
|
process.exit(1)
|
||||||
|
})
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
/*
|
||||||
|
* server component for the TimeLimit App
|
||||||
|
* Copyright (C) 2019 - 2021 Jonas Lochmann
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
* published by the Free Software Foundation, version 3 of the License.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const { resolve } = require('path')
|
||||||
|
|
||||||
|
const tempDir = resolve(__dirname, '../../../tempdb')
|
||||||
|
|
||||||
|
module.exports = { tempDir }
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* server component for the TimeLimit App
|
||||||
|
* Copyright (C) 2019 - 2021 Jonas Lochmann
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
* published by the Free Software Foundation, version 3 of the License.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const { startPostgres } = require('./postgres.js')
|
||||||
|
const { startMariadb } = require('./mariadb.js')
|
||||||
|
const { startSqlite } = require('./sqlite.js')
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
startPostgres,
|
||||||
|
databaseLaunchers: [ startMariadb, startPostgres, startSqlite ]
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
/*
|
||||||
|
* server component for the TimeLimit App
|
||||||
|
* Copyright (C) 2019 - 2021 Jonas Lochmann
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
* published by the Free Software Foundation, version 3 of the License.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const { resolve } = require('path')
|
||||||
|
const { spawn } = require('child_process')
|
||||||
|
const { tempDir } = require('./helper.js')
|
||||||
|
const { generateShortToken, generateToken } = require('../token.js')
|
||||||
|
const { rimrafAsync, mkdirAsync, readFileAsync, writeFileAsync } = require('../filesystem.js')
|
||||||
|
const { spawnAsync } = require('../process.js')
|
||||||
|
const { sleep } = require('../sleep.js')
|
||||||
|
|
||||||
|
async function startMariadb() {
|
||||||
|
try { await mkdirAsync(tempDir) } catch (ex) {/* ignore */}
|
||||||
|
|
||||||
|
const instanceDir = resolve(tempDir, generateShortToken())
|
||||||
|
const dataDir = resolve(instanceDir, 'data')
|
||||||
|
const socketPath = resolve(instanceDir, 'socket')
|
||||||
|
|
||||||
|
await rimrafAsync(instanceDir)
|
||||||
|
await mkdirAsync(instanceDir); await mkdirAsync(dataDir)
|
||||||
|
|
||||||
|
await spawnAsync('mysql_install_db', ['--datadir=' + dataDir, '--user=' + process.env.USER], { stdio: 'inherit' })
|
||||||
|
|
||||||
|
const task = await spawn('mysqld_safe', ['--no-defaults', '--datadir=' + dataDir, '--socket=' + socketPath, '--skip-networking'], { stdio: 'inherit' })
|
||||||
|
task.on('exit', () => rimrafAsync(instanceDir))
|
||||||
|
|
||||||
|
for (let i = 0; i < 100; i++) {
|
||||||
|
const { status } = await spawnAsync('mysqladmin', ['ping', '-S', socketPath])
|
||||||
|
|
||||||
|
if (status === 0) break
|
||||||
|
|
||||||
|
await sleep(100)
|
||||||
|
}
|
||||||
|
|
||||||
|
const database = generateShortToken()
|
||||||
|
const username = generateShortToken()
|
||||||
|
const password = generateToken()
|
||||||
|
|
||||||
|
const commands = [
|
||||||
|
'CREATE DATABASE `' + database + '` DEFAULT CHARACTER SET `utf8mb4` COLLATE `utf8mb4_bin`',
|
||||||
|
// all users of the system can see this password because it is passed as command
|
||||||
|
// line parameter - don't do this for anything important
|
||||||
|
'CREATE USER `' + username + '`@localhost IDENTIFIED BY \'' + password + '\'',
|
||||||
|
'GRANT ALL PRIVILEGES ON `' + database + '`.* TO `' + username + '`@localhost'
|
||||||
|
]
|
||||||
|
|
||||||
|
for (command of commands) {
|
||||||
|
console.log(command)
|
||||||
|
await spawnAsync('mysql', ['-S', socketPath, '-u', 'root', '-e', command], { stdio: 'inherit' })
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
shutdown: () => task.kill('SIGINT'),
|
||||||
|
socketPath,
|
||||||
|
dataDir,
|
||||||
|
database,
|
||||||
|
username,
|
||||||
|
password,
|
||||||
|
connectionUrl: 'mariadb://' + username + ':' + password + '@localhost/' + database + '?socketPath=' + encodeURIComponent(socketPath),
|
||||||
|
type: 'mariadb'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { startMariadb }
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
/*
|
||||||
|
* server component for the TimeLimit App
|
||||||
|
* Copyright (C) 2019 - 2021 Jonas Lochmann
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
* published by the Free Software Foundation, version 3 of the License.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const { resolve } = require('path')
|
||||||
|
const { spawn } = require('child_process')
|
||||||
|
const { tempDir } = require('./helper.js')
|
||||||
|
const { generateShortToken, generateToken } = require('../token.js')
|
||||||
|
const { rimrafAsync, mkdirAsync, readFileAsync, writeFileAsync } = require('../filesystem.js')
|
||||||
|
const { spawnAsync } = require('../process.js')
|
||||||
|
const { sleep } = require('../sleep.js')
|
||||||
|
|
||||||
|
async function startPostgres() {
|
||||||
|
try { await mkdirAsync(tempDir) } catch (ex) {/* ignore */}
|
||||||
|
|
||||||
|
const instanceDir = resolve(tempDir, generateShortToken())
|
||||||
|
const dataDir = resolve(instanceDir, 'data')
|
||||||
|
const socketDir = resolve(instanceDir, 'socket')
|
||||||
|
|
||||||
|
await rimrafAsync(instanceDir)
|
||||||
|
await mkdirAsync(instanceDir); await mkdirAsync(dataDir); await mkdirAsync(socketDir)
|
||||||
|
|
||||||
|
await spawnAsync('initdb', ['--locale=en_US.UTF-8', '-E', ' UTF8', '-D', dataDir], { stdio: 'inherit' })
|
||||||
|
|
||||||
|
const configFilePath = resolve(dataDir, 'postgresql.conf')
|
||||||
|
const configFileContent = (await readFileAsync(configFilePath)) + '\n'
|
||||||
|
+ 'unix_socket_directories = \'' + socketDir + '\'' + '\n'
|
||||||
|
+ 'listen_addresses = \'\' # do not listen using TCP'
|
||||||
|
|
||||||
|
await writeFileAsync(configFilePath, configFileContent)
|
||||||
|
|
||||||
|
const task = spawn('postgres', ['-D', dataDir], { stdio: 'inherit' })
|
||||||
|
task.on('exit', () => rimrafAsync(instanceDir))
|
||||||
|
|
||||||
|
for (let i = 0; i < 100; i++) {
|
||||||
|
const { status } = await spawnAsync('pg_isready', ['-h', socketDir])
|
||||||
|
|
||||||
|
if (status === 0) break
|
||||||
|
|
||||||
|
await sleep(100)
|
||||||
|
}
|
||||||
|
|
||||||
|
const database = generateShortToken()
|
||||||
|
const username = generateShortToken()
|
||||||
|
const password = generateToken() // this database accepts anything
|
||||||
|
|
||||||
|
await spawnAsync('createuser', ['-h', socketDir, username], { stdio: 'inherit' })
|
||||||
|
await spawnAsync('createdb', ['-h', socketDir, database], { stdio: 'inherit' })
|
||||||
|
|
||||||
|
return {
|
||||||
|
shutdown: () => task.kill('SIGINT'),
|
||||||
|
socketDir,
|
||||||
|
dataDir,
|
||||||
|
database,
|
||||||
|
username,
|
||||||
|
password,
|
||||||
|
connectionUrl: 'postgres://' + username + ':' + password + '@localhost/' + database + '?host=' + encodeURIComponent(socketDir),
|
||||||
|
type: 'postgres'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { startPostgres }
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* server component for the TimeLimit App
|
||||||
|
* Copyright (C) 2019 - 2021 Jonas Lochmann
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
* published by the Free Software Foundation, version 3 of the License.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const { resolve } = require('path')
|
||||||
|
const { spawn } = require('child_process')
|
||||||
|
const { tempDir } = require('./helper.js')
|
||||||
|
const { generateShortToken, generateToken } = require('../token.js')
|
||||||
|
const { rimrafAsync, mkdirAsync, readFileAsync, writeFileAsync } = require('../filesystem.js')
|
||||||
|
const { spawnAsync } = require('../process.js')
|
||||||
|
const { sleep } = require('../sleep.js')
|
||||||
|
|
||||||
|
async function startSqlite() {
|
||||||
|
try { await mkdirAsync(tempDir) } catch (ex) {/* ignore */}
|
||||||
|
|
||||||
|
const instanceDir = resolve(tempDir, generateShortToken())
|
||||||
|
|
||||||
|
await rimrafAsync(instanceDir)
|
||||||
|
await mkdirAsync(instanceDir)
|
||||||
|
|
||||||
|
return {
|
||||||
|
shutdown: () => rimrafAsync(instanceDir),
|
||||||
|
instanceDir,
|
||||||
|
connectionUrl: 'sqlite:///' + instanceDir + '/test.db',
|
||||||
|
type: 'sqlite'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { startSqlite }
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
* server component for the TimeLimit App
|
||||||
|
* Copyright (C) 2019 - 2021 Jonas Lochmann
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
* published by the Free Software Foundation, version 3 of the License.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const rimraf = require('rimraf')
|
||||||
|
const { mkdir, readFile, writeFile } = require('fs')
|
||||||
|
|
||||||
|
function mkdirAsync(path) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
mkdir(path, (err) => {
|
||||||
|
if (err) reject(err)
|
||||||
|
else resolve()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function rimrafAsync(path) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
rimraf(path, (err) => {
|
||||||
|
if (err) reject(err)
|
||||||
|
else resolve()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function readFileAsync(path) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
readFile(path, (err, res) => {
|
||||||
|
if (err) reject(err)
|
||||||
|
else resolve(res)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function writeFileAsync(path, content) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
writeFile(path, content, (err) => {
|
||||||
|
if (err) reject(err)
|
||||||
|
else resolve()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { mkdirAsync, rimrafAsync, readFileAsync, writeFileAsync }
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* server component for the TimeLimit App
|
||||||
|
* Copyright (C) 2019 - 2021 Jonas Lochmann
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
* published by the Free Software Foundation, version 3 of the License.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const { spawn } = require('child_process')
|
||||||
|
|
||||||
|
function startMainApp(env) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const task = spawn('npm', ['start'], {
|
||||||
|
stdio: ['inherit', 'pipe', 'inherit'],
|
||||||
|
env: { ...process.env, PORT: 0 /* random port */, ...env }
|
||||||
|
})
|
||||||
|
|
||||||
|
task.on('exit', () => reject(new Error('task terminated too early')))
|
||||||
|
task.on('error', (ex) => reject(ex))
|
||||||
|
|
||||||
|
task.stdout.on('data', (data) => {
|
||||||
|
if (data.toString('utf8').split('\n').indexOf('ready') !== -1) resolve(task)
|
||||||
|
|
||||||
|
process.stdout.write(data)
|
||||||
|
})
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
reject(new Error('timeout'))
|
||||||
|
|
||||||
|
task.kill('SIGINT')
|
||||||
|
}, 1000 * 30)
|
||||||
|
}).then((task) => {
|
||||||
|
return {
|
||||||
|
shutdown: () => task.kill('SIGINT')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { startMainApp }
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* server component for the TimeLimit App
|
||||||
|
* Copyright (C) 2019 - 2021 Jonas Lochmann
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
* published by the Free Software Foundation, version 3 of the License.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const { spawn } = require('child_process')
|
||||||
|
|
||||||
|
function spawnAsync(command, args, options) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const task = spawn(command, args, options)
|
||||||
|
|
||||||
|
task.on('error', (ex) => reject(ex))
|
||||||
|
task.on('exit', (status) => resolve({ status }))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { spawnAsync }
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* server component for the TimeLimit App
|
||||||
|
* Copyright (C) 2019 - 2021 Jonas Lochmann
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
* published by the Free Software Foundation, version 3 of the License.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
function sleep(delay) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
setTimeout(() => resolve(), delay)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { sleep }
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
/*
|
||||||
|
* server component for the TimeLimit App
|
||||||
|
* Copyright (C) 2019 - 2021 Jonas Lochmann
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
* published by the Free Software Foundation, version 3 of the License.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const TokenGenerator = require('tokgen')
|
||||||
|
|
||||||
|
const tokenGenerator = new TokenGenerator({
|
||||||
|
length: 32,
|
||||||
|
chars: 'a-zA-Z0-9'
|
||||||
|
})
|
||||||
|
|
||||||
|
const shortTokenGenerator = new TokenGenerator({
|
||||||
|
length: 8,
|
||||||
|
chars: 'a-zA-Z0-9'
|
||||||
|
})
|
||||||
|
|
||||||
|
function generateToken() { return tokenGenerator.generate() }
|
||||||
|
function generateShortToken() { return shortTokenGenerator.generate() }
|
||||||
|
|
||||||
|
module.exports = { generateToken, generateShortToken }
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* server component for the TimeLimit App
|
* server component for the TimeLimit App
|
||||||
* Copyright (C) 2019 - 2020 Jonas Lochmann
|
* Copyright (C) 2019 - 2021 Jonas Lochmann
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU Affero General Public License as
|
* it under the terms of the GNU Affero General Public License as
|
||||||
@@ -58,12 +58,24 @@ export async function up (queryInterface: QueryInterface, sequelize: Sequelize)
|
|||||||
...usedTimeAttributesVersion3
|
...usedTimeAttributesVersion3
|
||||||
}, { transaction })
|
}, { transaction })
|
||||||
|
|
||||||
await sequelize.query(`
|
const dialect = sequelize.getDialect()
|
||||||
INSERT INTO UsedTimes (familyId, categoryId, dayOfEpoch, usedTime, lastUpdate, startMinuteOfDay, endMinuteOfDay)
|
const isMysql = dialect === 'mysql' || dialect === 'mariadb'
|
||||||
SELECT familyId, categoryId, dayOfEpoch, usedTime, lastUpdate,
|
|
||||||
${MinuteOfDay.MIN} AS startMinuteOfDay, ${MinuteOfDay.MAX} AS endMinuteOfDay
|
if (isMysql) {
|
||||||
FROM UsedTimesOld
|
await sequelize.query(`
|
||||||
`, { transaction })
|
INSERT INTO UsedTimes (familyId, categoryId, dayOfEpoch, usedTime, lastUpdate, startMinuteOfDay, endMinuteOfDay)
|
||||||
|
SELECT familyId, categoryId, dayOfEpoch, usedTime, lastUpdate,
|
||||||
|
${MinuteOfDay.MIN} AS startMinuteOfDay, ${MinuteOfDay.MAX} AS endMinuteOfDay
|
||||||
|
FROM UsedTimesOld
|
||||||
|
`, { transaction })
|
||||||
|
} else {
|
||||||
|
await sequelize.query(`
|
||||||
|
INSERT INTO "UsedTimes" ("familyId", "categoryId", "dayOfEpoch", "usedTime", "lastUpdate", "startMinuteOfDay", "endMinuteOfDay")
|
||||||
|
SELECT "familyId", "categoryId", "dayOfEpoch", "usedTime", "lastUpdate",
|
||||||
|
${MinuteOfDay.MIN} AS "startMinuteOfDay", ${MinuteOfDay.MAX} AS "endMinuteOfDay"
|
||||||
|
FROM "UsedTimesOld"
|
||||||
|
`, { transaction })
|
||||||
|
}
|
||||||
|
|
||||||
await queryInterface.dropTable('UsedTimesOld', { transaction })
|
await queryInterface.dropTable('UsedTimesOld', { transaction })
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* server component for the TimeLimit App
|
* server component for the TimeLimit App
|
||||||
* Copyright (C) 2019 - 2020 Jonas Lochmann
|
* Copyright (C) 2019 - 2021 Jonas Lochmann
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU Affero General Public License as
|
* it under the terms of the GNU Affero General Public License as
|
||||||
@@ -25,11 +25,22 @@ export async function up (queryInterface: QueryInterface, sequelize: Sequelize)
|
|||||||
await queryInterface.renameTable('Purchases', 'PurchasesOld', { transaction })
|
await queryInterface.renameTable('Purchases', 'PurchasesOld', { transaction })
|
||||||
await queryInterface.createTable('Purchases', purchaseAttributes, { transaction })
|
await queryInterface.createTable('Purchases', purchaseAttributes, { transaction })
|
||||||
|
|
||||||
await sequelize.query(`
|
const dialect = sequelize.getDialect()
|
||||||
INSERT INTO Purchases (familyId, service, transactionId, type, loggedAt, previousFullVersionEndTime, newFullVersionEndTime)
|
const isMysql = dialect === 'mysql' || dialect === 'mariadb'
|
||||||
SELECT familyId, service, transactionId, type, 0 AS loggedAt, 0 AS previousFullVersionEndTime, loggedAt AS newFullVersionEndTime
|
|
||||||
FROM PurchasesOld
|
if (isMysql) {
|
||||||
`, { transaction })
|
await sequelize.query(`
|
||||||
|
INSERT INTO Purchases (familyId, service, transactionId, type, loggedAt, previousFullVersionEndTime, newFullVersionEndTime)
|
||||||
|
SELECT familyId, service, transactionId, type, 0 AS loggedAt, 0 AS previousFullVersionEndTime, loggedAt AS newFullVersionEndTime
|
||||||
|
FROM PurchasesOld
|
||||||
|
`, { transaction })
|
||||||
|
} else {
|
||||||
|
await sequelize.query(`
|
||||||
|
INSERT INTO "Purchases" ("familyId", service, "transactionId", type, "loggedAt", "previousFullVersionEndTime", "newFullVersionEndTime")
|
||||||
|
SELECT "familyId", service, "transactionId", type, 0 AS "loggedAt", 0 AS "previousFullVersionEndTime", "loggedAt" AS "newFullVersionEndTime"
|
||||||
|
FROM "PurchasesOld"
|
||||||
|
`, { transaction })
|
||||||
|
}
|
||||||
|
|
||||||
await queryInterface.dropTable('PurchasesOld', { transaction })
|
await queryInterface.dropTable('PurchasesOld', { transaction })
|
||||||
})
|
})
|
||||||
|
|||||||
+22
-8
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* server component for the TimeLimit App
|
* server component for the TimeLimit App
|
||||||
* Copyright (C) 2019 - 2020 Jonas Lochmann
|
* Copyright (C) 2019 - 2021 Jonas Lochmann
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU Affero General Public License as
|
* it under the terms of the GNU Affero General Public License as
|
||||||
@@ -21,14 +21,28 @@ export async function up (queryInterface: QueryInterface, sequelize: Sequelize)
|
|||||||
await sequelize.transaction({
|
await sequelize.transaction({
|
||||||
type: Transaction.TYPES.EXCLUSIVE
|
type: Transaction.TYPES.EXCLUSIVE
|
||||||
}, async (transaction) => {
|
}, async (transaction) => {
|
||||||
await sequelize.query(
|
const dialect = sequelize.getDialect()
|
||||||
'CREATE TABLE `UserLimitLoginCategories`' +
|
const isMysql = dialect === 'mysql' || dialect === 'mariadb'
|
||||||
'(`familyId` VARCHAR(10) NOT NULL, `userId` VARCHAR(6) NOT NULL, `categoryId` VARCHAR(6) NOT NULL,' +
|
|
||||||
'PRIMARY KEY(`familyId`, `userId`), FOREIGN KEY(`familyId`, `userId`) REFERENCES `Users`(`familyId`, `userId`) ON UPDATE CASCADE ON DELETE CASCADE , FOREIGN KEY(`familyId`, `categoryId`) REFERENCES `Categories`(`familyId`, `categoryId`) ON UPDATE CASCADE ON DELETE CASCADE )',
|
|
||||||
{ transaction }
|
|
||||||
)
|
|
||||||
|
|
||||||
await sequelize.query('CREATE INDEX `UserLimitLoginCategoriesIndexCategoryId` ON `UserLimitLoginCategories` (`familyId`, `categoryId`)', { transaction })
|
if (isMysql) {
|
||||||
|
await sequelize.query(
|
||||||
|
'CREATE TABLE `UserLimitLoginCategories`' +
|
||||||
|
'(`familyId` VARCHAR(10) NOT NULL, `userId` VARCHAR(6) NOT NULL, `categoryId` VARCHAR(6) NOT NULL,' +
|
||||||
|
'PRIMARY KEY(`familyId`, `userId`), FOREIGN KEY(`familyId`, `userId`) REFERENCES `Users`(`familyId`, `userId`) ON UPDATE CASCADE ON DELETE CASCADE , FOREIGN KEY(`familyId`, `categoryId`) REFERENCES `Categories`(`familyId`, `categoryId`) ON UPDATE CASCADE ON DELETE CASCADE )',
|
||||||
|
{ transaction }
|
||||||
|
)
|
||||||
|
|
||||||
|
await sequelize.query('CREATE INDEX `UserLimitLoginCategoriesIndexCategoryId` ON `UserLimitLoginCategories` (`familyId`, `categoryId`)', { transaction })
|
||||||
|
} else {
|
||||||
|
await sequelize.query(
|
||||||
|
'CREATE TABLE "UserLimitLoginCategories"' +
|
||||||
|
'("familyId" VARCHAR(10) NOT NULL, "userId" VARCHAR(6) NOT NULL, "categoryId" VARCHAR(6) NOT NULL,' +
|
||||||
|
'PRIMARY KEY("familyId", "userId"), FOREIGN KEY("familyId", "userId") REFERENCES "Users" ("familyId", "userId") ON UPDATE CASCADE ON DELETE CASCADE , FOREIGN KEY("familyId", "categoryId") REFERENCES "Categories" ("familyId", "categoryId") ON UPDATE CASCADE ON DELETE CASCADE )',
|
||||||
|
{ transaction }
|
||||||
|
)
|
||||||
|
|
||||||
|
await sequelize.query('CREATE INDEX "UserLimitLoginCategoriesIndexCategoryId" ON "UserLimitLoginCategories" ("familyId", "categoryId")', { transaction })
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* server component for the TimeLimit App
|
* server component for the TimeLimit App
|
||||||
* Copyright (C) 2019 - 2020 Jonas Lochmann
|
* Copyright (C) 2019 - 2021 Jonas Lochmann
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU Affero General Public License as
|
* it under the terms of the GNU Affero General Public License as
|
||||||
@@ -21,14 +21,28 @@ export async function up (_: QueryInterface, sequelize: Sequelize) {
|
|||||||
await sequelize.transaction({
|
await sequelize.transaction({
|
||||||
type: Transaction.TYPES.EXCLUSIVE
|
type: Transaction.TYPES.EXCLUSIVE
|
||||||
}, async (transaction) => {
|
}, async (transaction) => {
|
||||||
await sequelize.query(
|
const dialect = sequelize.getDialect()
|
||||||
'CREATE TABLE `CategoryNetworkIds` ' +
|
const isMysql = dialect === 'mysql' || dialect === 'mariadb'
|
||||||
'(`familyId` VARCHAR(10) NOT NULL, `categoryId` VARCHAR(6) NOT NULL,' +
|
|
||||||
'`networkItemId` VARCHAR(6) NOT NULL, `hashedNetworkId` VARCHAR(8) NOT NULL,' +
|
if (isMysql) {
|
||||||
'PRIMARY KEY(`familyId`, `categoryId`, `networkItemId`), FOREIGN KEY(`familyId`, `categoryId`)' +
|
await sequelize.query(
|
||||||
'REFERENCES `Categories`(`familyId`, `categoryId`) ON UPDATE CASCADE ON DELETE CASCADE )',
|
'CREATE TABLE `CategoryNetworkIds` ' +
|
||||||
{ transaction }
|
'(`familyId` VARCHAR(10) NOT NULL, `categoryId` VARCHAR(6) NOT NULL,' +
|
||||||
)
|
'`networkItemId` VARCHAR(6) NOT NULL, `hashedNetworkId` VARCHAR(8) NOT NULL,' +
|
||||||
|
'PRIMARY KEY(`familyId`, `categoryId`, `networkItemId`), FOREIGN KEY(`familyId`, `categoryId`)' +
|
||||||
|
'REFERENCES `Categories`(`familyId`, `categoryId`) ON UPDATE CASCADE ON DELETE CASCADE )',
|
||||||
|
{ transaction }
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
await sequelize.query(
|
||||||
|
'CREATE TABLE "CategoryNetworkIds" ' +
|
||||||
|
'("familyId" VARCHAR(10) NOT NULL, "categoryId" VARCHAR(6) NOT NULL,' +
|
||||||
|
'"networkItemId" VARCHAR(6) NOT NULL, "hashedNetworkId" VARCHAR(8) NOT NULL,' +
|
||||||
|
'PRIMARY KEY("familyId", "categoryId", "networkItemId"), FOREIGN KEY("familyId", "categoryId")' +
|
||||||
|
'REFERENCES "Categories"("familyId", "categoryId") ON UPDATE CASCADE ON DELETE CASCADE )',
|
||||||
|
{ transaction }
|
||||||
|
)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* server component for the TimeLimit App
|
* server component for the TimeLimit App
|
||||||
* Copyright (C) 2019 - 2020 Jonas Lochmann
|
* Copyright (C) 2019 - 2021 Jonas Lochmann
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU Affero General Public License as
|
* it under the terms of the GNU Affero General Public License as
|
||||||
@@ -22,18 +22,36 @@ export async function up (queryInterface: QueryInterface, sequelize: Sequelize)
|
|||||||
await sequelize.transaction({
|
await sequelize.transaction({
|
||||||
type: Transaction.TYPES.EXCLUSIVE
|
type: Transaction.TYPES.EXCLUSIVE
|
||||||
}, async (transaction) => {
|
}, async (transaction) => {
|
||||||
await sequelize.query(
|
const dialect = sequelize.getDialect()
|
||||||
'CREATE TABLE `ChildTasks` (' +
|
const isMysql = dialect === 'mysql' || dialect === 'mariadb'
|
||||||
'`familyId` VARCHAR(10) NOT NULL, `taskId` VARCHAR(6) NOT NULL,' +
|
|
||||||
'`categoryId` VARCHAR(6) NOT NULL, `taskTitle` VARCHAR(50) NOT NULL,' +
|
if (isMysql) {
|
||||||
'`extraTimeDuration` INTEGER NOT NULL, `pendingRequest` INTEGER NOT NULL,' +
|
await sequelize.query(
|
||||||
'`lastGrantTimestamp` LONG NOT NULL,' +
|
'CREATE TABLE `ChildTasks` (' +
|
||||||
'PRIMARY KEY(`familyId`, `taskId`),' +
|
'`familyId` VARCHAR(10) NOT NULL, `taskId` VARCHAR(6) NOT NULL,' +
|
||||||
'FOREIGN KEY(`familyId`, `categoryId`) REFERENCES `Categories`(`familyId`, `categoryId`) ' +
|
'`categoryId` VARCHAR(6) NOT NULL, `taskTitle` VARCHAR(50) NOT NULL,' +
|
||||||
'ON UPDATE CASCADE ON DELETE CASCADE' +
|
'`extraTimeDuration` INTEGER NOT NULL, `pendingRequest` INTEGER NOT NULL,' +
|
||||||
')',
|
'`lastGrantTimestamp` LONG NOT NULL,' +
|
||||||
{ transaction }
|
'PRIMARY KEY(`familyId`, `taskId`),' +
|
||||||
)
|
'FOREIGN KEY(`familyId`, `categoryId`) REFERENCES `Categories`(`familyId`, `categoryId`) ' +
|
||||||
|
'ON UPDATE CASCADE ON DELETE CASCADE' +
|
||||||
|
')',
|
||||||
|
{ transaction }
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
await sequelize.query(
|
||||||
|
'CREATE TABLE "ChildTasks" (' +
|
||||||
|
'"familyId" VARCHAR(10) NOT NULL, "taskId" VARCHAR(6) NOT NULL,' +
|
||||||
|
'"categoryId" VARCHAR(6) NOT NULL, "taskTitle" VARCHAR(50) NOT NULL,' +
|
||||||
|
'"extraTimeDuration" INTEGER NOT NULL, "pendingRequest" INTEGER NOT NULL,' +
|
||||||
|
'"lastGrantTimestamp" BIGINT NOT NULL,' +
|
||||||
|
'PRIMARY KEY("familyId", "taskId"),' +
|
||||||
|
'FOREIGN KEY("familyId", "categoryId") REFERENCES "Categories"("familyId", "categoryId") ' +
|
||||||
|
'ON UPDATE CASCADE ON DELETE CASCADE' +
|
||||||
|
')',
|
||||||
|
{ transaction }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
await queryInterface.addColumn('Categories', 'taskListVersion', {
|
await queryInterface.addColumn('Categories', 'taskListVersion', {
|
||||||
...categoryAttributes.taskListVersion
|
...categoryAttributes.taskListVersion
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* server component for the TimeLimit App
|
* server component for the TimeLimit App
|
||||||
* Copyright (C) 2019 - 2020 Jonas Lochmann
|
* Copyright (C) 2019 - 2021 Jonas Lochmann
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU Affero General Public License as
|
* it under the terms of the GNU Affero General Public License as
|
||||||
@@ -61,7 +61,7 @@ export const attributesVersion1: SequelizeAttributes<TimelimitRuleAttributesVers
|
|||||||
categoryId: { ...idWithinFamilyColumn },
|
categoryId: { ...idWithinFamilyColumn },
|
||||||
applyToExtraTimeUsage: { ...booleanColumn },
|
applyToExtraTimeUsage: { ...booleanColumn },
|
||||||
dayMaskAsBitmask: {
|
dayMaskAsBitmask: {
|
||||||
type: Sequelize.TINYINT,
|
type: Sequelize.SMALLINT,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
validate: {
|
validate: {
|
||||||
min: 0,
|
min: 0,
|
||||||
|
|||||||
Reference in New Issue
Block a user