@silexlabs/silex 3.0.0-alpha.39 → 3.0.0-alpha.41
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/.silex.js +38 -8
- package/dist/client/client/config.d.ts +11 -41
- package/dist/client/client/grapesjs/LoginDialog.d.ts +13 -0
- package/dist/client/client/grapesjs/PublicationManager.d.ts +69 -0
- package/dist/client/client/grapesjs/PublicationUi.d.ts +58 -0
- package/dist/client/client/grapesjs/index.d.ts +18 -44
- package/dist/client/client/grapesjs/storage.d.ts +1 -0
- package/dist/client/client/services.d.ts +35 -0
- package/dist/client/constants.d.ts +17 -0
- package/dist/client/css/admin.css +86 -3
- package/dist/client/js/main.js +201 -90
- package/dist/client/js/main.js.LICENSE.txt +6 -0
- package/dist/client/types.d.ts +200 -29
- package/dist/constants.d.ts +21 -0
- package/dist/constants.js +38 -1
- package/dist/constants.js.map +1 -0
- package/dist/events.d.ts +2 -0
- package/dist/events.js +17 -0
- package/dist/events.js.map +1 -0
- package/dist/plugins/AuthPlugin.d.ts +1 -0
- package/dist/plugins/AuthPlugin.js +38 -0
- package/dist/plugins/AuthPlugin.js.map +1 -0
- package/dist/plugins/Cache.d.ts +2 -0
- package/dist/plugins/Cache.js +32 -0
- package/dist/plugins/Cache.js.map +1 -0
- package/dist/plugins/FtpBackend.d.ts +92 -0
- package/dist/plugins/FtpBackend.js +604 -0
- package/dist/plugins/FtpBackend.js.map +1 -0
- package/dist/plugins/HooksPlugin.d.ts +7 -0
- package/dist/plugins/HooksPlugin.js +76 -0
- package/dist/plugins/HooksPlugin.js.map +1 -0
- package/dist/plugins/PublicationPlugin.d.ts +17 -0
- package/dist/plugins/PublicationPlugin.js +170 -0
- package/dist/plugins/PublicationPlugin.js.map +1 -0
- package/dist/plugins/SslPlugin.d.ts +1 -0
- package/dist/plugins/SslPlugin.js +86 -0
- package/dist/plugins/SslPlugin.js.map +1 -0
- package/dist/plugins/StaticPlugin.d.ts +1 -0
- package/dist/plugins/StaticPlugin.js +66 -0
- package/dist/plugins/StaticPlugin.js.map +1 -0
- package/dist/plugins/WebsitePlugin.d.ts +14 -0
- package/dist/plugins/WebsitePlugin.js +302 -0
- package/dist/plugins/WebsitePlugin.js.map +1 -0
- package/dist/server/backends/FsBackend.d.ts +42 -0
- package/dist/server/backends/FsBackend.js +269 -0
- package/dist/server/backends/FsBackend.js.map +1 -0
- package/dist/server/backends/index.d.ts +95 -0
- package/dist/server/backends/index.js +255 -0
- package/dist/server/backends/index.js.map +1 -0
- package/dist/server/cli.js +0 -1
- package/dist/server/config.d.ts +58 -0
- package/dist/server/config.js +128 -24
- package/dist/server/config.js.map +1 -0
- package/dist/server/express.js +31 -3
- package/dist/server/index.js +9 -20
- package/dist/server/jobs.d.ts +6 -0
- package/dist/server/jobs.js +75 -0
- package/dist/server/jobs.js.map +1 -0
- package/dist/server/utils/validation.d.ts +7 -0
- package/dist/server/utils/validation.js +37 -0
- package/dist/server/utils/validation.js.map +1 -0
- package/dist/types.d.ts +270 -0
- package/dist/types.js +35 -28
- package/dist/types.js.map +1 -0
- package/package.json +19 -8
- package/public/silex.js +17 -0
- package/src/css/dialog.scss +9 -2
- package/src/css/grapesjs.scss +79 -0
- package/src/css/project-bar.scss +2 -2
- package/src/example-client-config.js +17 -0
- package/src/html/templates/includes.pug +0 -1
- package/src/ts/client/config.ts +7 -6
- package/src/ts/client/grapesjs/LoginDialog.ts +190 -0
- package/src/ts/client/grapesjs/PublicationManager.ts +333 -0
- package/src/ts/client/grapesjs/PublicationUi.ts +320 -0
- package/src/ts/client/grapesjs/index.ts +88 -85
- package/src/ts/client/grapesjs/storage.ts +110 -0
- package/src/ts/client/index.ts +5 -7
- package/src/ts/client/services.ts +129 -0
- package/src/ts/constants.ts +21 -0
- package/src/ts/plugins/AuthPlugin.ts +38 -0
- package/src/ts/plugins/Cache.ts +28 -0
- package/src/ts/plugins/FtpBackend.ts +689 -0
- package/src/ts/plugins/HooksPlugin.ts +79 -0
- package/src/ts/plugins/PublicationPlugin.ts +170 -0
- package/src/{plugins/SslPlugin.js → ts/plugins/SslPlugin.ts} +24 -6
- package/src/ts/plugins/StaticPlugin.ts +68 -0
- package/src/ts/plugins/WebsitePlugin.ts +318 -0
- package/src/ts/server/backends/FsBackend.ts +266 -0
- package/src/ts/server/backends/index.ts +309 -0
- package/src/ts/server/cli.ts +0 -2
- package/src/ts/server/config.ts +103 -33
- package/src/ts/server/express.ts +37 -3
- package/src/ts/server/index.ts +24 -14
- package/src/ts/server/jobs.test.ts +74 -0
- package/src/ts/server/jobs.ts +77 -0
- package/src/ts/server/utils/validation.ts +33 -0
- package/src/ts/types.ts +167 -59
- package/dist/client/client/grapesjs/publish.d.ts +0 -18
- package/dist/page.js +0 -15
- package/src/plugins/AuthPlugin.js +0 -21
- package/src/plugins/Cache.js +0 -11
- package/src/plugins/DefaultBackend.js +0 -149
- package/src/plugins/ExpressPlugin.js +0 -39
- package/src/plugins/HooksPlugin.js +0 -58
- package/src/plugins/PublishPlugin.js +0 -69
- package/src/plugins/StaticPlugin.js +0 -48
- package/src/plugins/WebsitePlugin.js +0 -207
- package/src/ts/client/grapesjs/publish.ts +0 -403
- /package/.silex/default/{.silex.data.json → website.json} +0 -0
package/src/ts/server/config.ts
CHANGED
|
@@ -27,14 +27,24 @@
|
|
|
27
27
|
import { Config, Plugin } from '@silexlabs/silex-plugins'
|
|
28
28
|
import { resolve } from 'path'
|
|
29
29
|
import { CLIENT_CONFIG_FILE_NAME } from '../constants'
|
|
30
|
-
import { Router } from 'express'
|
|
30
|
+
import { Application, Request, Response, Router } from 'express'
|
|
31
31
|
import { readFile } from 'fs/promises'
|
|
32
|
-
import {
|
|
32
|
+
import { HostingProvider, StorageProvider, toBackendEnum } from './backends'
|
|
33
|
+
import { FsBackend } from './backends/FsBackend'
|
|
34
|
+
import { Backend } from './backends'
|
|
35
|
+
import { BackendType } from '../types'
|
|
33
36
|
|
|
34
37
|
/**
|
|
35
38
|
* Config types definitions
|
|
36
39
|
*/
|
|
37
40
|
export class ServerConfig extends Config {
|
|
41
|
+
public expressOptions = {
|
|
42
|
+
jsonLimit: process.env.SILEX_EXPRESS_JSON_LIMIT || '1mb',
|
|
43
|
+
textLimit: process.env.SILEX_EXPRESS_TEXT_LIMIT || '10mb',
|
|
44
|
+
sessionName: process.env.SILEX_SESSION_NAME || 'silex-session',
|
|
45
|
+
sessionSecret: process.env.SILEX_SESSION_SECRET || 'replace this session secret in env vars',
|
|
46
|
+
cors: process.env.SILEX_CORS_URL,
|
|
47
|
+
}
|
|
38
48
|
constructor(
|
|
39
49
|
public url: string,
|
|
40
50
|
public debug: boolean,
|
|
@@ -43,41 +53,99 @@ export class ServerConfig extends Config {
|
|
|
43
53
|
) {
|
|
44
54
|
super()
|
|
45
55
|
}
|
|
46
|
-
}
|
|
47
56
|
|
|
48
|
-
//
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
57
|
+
// All backends or just the storage or hosting providers
|
|
58
|
+
getBackends<T extends Backend>(type: BackendType | string | null = null): T[] {
|
|
59
|
+
// All backends including storage and hosting providers
|
|
60
|
+
if (!type) {
|
|
61
|
+
// Concat storage and hosting providers
|
|
62
|
+
const allBackends: Backend[] = (this.getStorageProviders() as Backend[]).concat(this.getHostingProviders() as Backend[])
|
|
53
63
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
64
|
+
// Remove duplicates (if a backend is both a storage and a hosting provider)
|
|
65
|
+
return Array.from(new Set(allBackends)) as unknown as T[]
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Convert to enum in case it is a string
|
|
69
|
+
const backendType = toBackendEnum(type.toString())
|
|
70
|
+
|
|
71
|
+
// Only one type of backend
|
|
72
|
+
switch(backendType) {
|
|
73
|
+
case BackendType.HOSTING: return this.getHostingProviders() as unknown as T[]
|
|
74
|
+
case BackendType.STORAGE: return this.getStorageProviders() as unknown as T[]
|
|
75
|
+
default: throw new Error(`Unknown backend type ${type}`)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Storage providers to store the website data and assets
|
|
80
|
+
private storageProviders: StorageProvider[] = [new FsBackend(null, { type: BackendType.STORAGE })]
|
|
81
|
+
addStorageProvider(storage: StorageProvider | StorageProvider[]) {
|
|
82
|
+
this.setStorageProviders(this.storageProviders.concat(storage))
|
|
83
|
+
}
|
|
84
|
+
setStorageProviders(storageProviders: StorageProvider[]) {
|
|
85
|
+
this.storageProviders =storageProviders
|
|
86
|
+
}
|
|
87
|
+
getStorageProviders(): StorageProvider[] {
|
|
88
|
+
return this.storageProviders
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Hosting providers to publish the website online
|
|
92
|
+
private hostingProviders: HostingProvider[] = [new FsBackend(null, { type: BackendType.HOSTING })]
|
|
93
|
+
addHostingProvider(hosting: HostingProvider | HostingProvider[]) {
|
|
94
|
+
this.setHostingProviders(this.hostingProviders.concat(hosting))
|
|
95
|
+
}
|
|
96
|
+
setHostingProviders(hostings: HostingProvider[]) {
|
|
97
|
+
this.hostingProviders = hostings
|
|
98
|
+
}
|
|
99
|
+
getHostingProviders(): HostingProvider[] { return this.hostingProviders }
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Add routes to serve the client config file
|
|
103
|
+
*/
|
|
104
|
+
async addRoutes(app: Application) {
|
|
105
|
+
const path = process.env.SILEX_CLIENT_CONFIG
|
|
106
|
+
if (path) {
|
|
107
|
+
console.log(`> Serving client side config ${path} at ${CLIENT_CONFIG_FILE_NAME}`)
|
|
108
|
+
// Load the client config file
|
|
109
|
+
let clientConfig = (await readFile(path as string)).toString()
|
|
110
|
+
// Serve the config file
|
|
111
|
+
app.get(`/${CLIENT_CONFIG_FILE_NAME}`, async (req: Request, res: Response) => {
|
|
67
112
|
// Reload each time in debug mode
|
|
68
|
-
if (
|
|
69
|
-
console.log('[Debug mode] Reloading config file',
|
|
70
|
-
clientConfig = (await readFile(
|
|
113
|
+
if (this.debug) {
|
|
114
|
+
console.log('[Debug mode] Reloading config file', path)
|
|
115
|
+
clientConfig = (await readFile(path)).toString()
|
|
71
116
|
}
|
|
72
117
|
// Send the config file
|
|
73
|
-
|
|
118
|
+
res.json(clientConfig)
|
|
74
119
|
})
|
|
75
|
-
}
|
|
120
|
+
}
|
|
76
121
|
}
|
|
77
122
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
123
|
+
/**
|
|
124
|
+
* Load config files
|
|
125
|
+
* This is the main config file and the user config file
|
|
126
|
+
*/
|
|
127
|
+
async loadConfigFiles() {
|
|
128
|
+
// Load the user config file
|
|
129
|
+
await loadUserConfig(this)
|
|
130
|
+
// Load the main config file
|
|
131
|
+
await loadSilexConfig(this)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Get config async function
|
|
136
|
+
export function createConfig(): ServerConfig {
|
|
137
|
+
const port = process.env.PORT || '6805' // 6805 is the date of sexual revolution started in paris france 8-)
|
|
138
|
+
const debug = process.env.SILEX_DEBUG === 'true'
|
|
139
|
+
|
|
140
|
+
const url = process.env.SERVER_URL || `http://localhost:${port}`
|
|
141
|
+
return new ServerConfig(url, debug, port, '/api')
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Load user config file
|
|
146
|
+
* This is the config file passed as env var CONFIG
|
|
147
|
+
*/
|
|
148
|
+
export async function loadUserConfig(config: ServerConfig) {
|
|
81
149
|
if (process.env.CONFIG) {
|
|
82
150
|
const userConfigPath: Plugin = process.env.CONFIG
|
|
83
151
|
console.log('> Loading user config', userConfigPath)
|
|
@@ -88,8 +156,13 @@ export default async function(): Promise<ServerConfig> {
|
|
|
88
156
|
throw new Error(`User config file ${userConfigPath} error: ${e.message}`)
|
|
89
157
|
}
|
|
90
158
|
}
|
|
159
|
+
}
|
|
91
160
|
|
|
92
|
-
|
|
161
|
+
/**
|
|
162
|
+
* Load .silex.js in the root folder
|
|
163
|
+
* This is the main config file
|
|
164
|
+
*/
|
|
165
|
+
export async function loadSilexConfig(config: ServerConfig) {
|
|
93
166
|
const configFilePath: Plugin = resolve(__dirname, '../../.silex.js')
|
|
94
167
|
console.log('> Loading config', configFilePath)
|
|
95
168
|
try {
|
|
@@ -102,7 +175,4 @@ export default async function(): Promise<ServerConfig> {
|
|
|
102
175
|
throw new Error(`Error in config file ${configFilePath}: ${e.message}`)
|
|
103
176
|
}
|
|
104
177
|
}
|
|
105
|
-
|
|
106
|
-
// Return the config file
|
|
107
|
-
return config
|
|
108
178
|
}
|
package/src/ts/server/express.ts
CHANGED
|
@@ -16,14 +16,48 @@
|
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
18
|
import express, { Application } from 'express'
|
|
19
|
+
import bodyParser from 'body-parser'
|
|
20
|
+
import compression from 'compression'
|
|
21
|
+
import cookieParser from 'cookie-parser'
|
|
22
|
+
import session from 'cookie-session'
|
|
23
|
+
import cors from 'cors'
|
|
19
24
|
|
|
20
25
|
import { ServerConfig } from './config'
|
|
21
26
|
import { EVENT_STARTUP_START, EVENT_STARTUP_END } from '../events'
|
|
22
27
|
|
|
23
|
-
export
|
|
24
|
-
//
|
|
28
|
+
export function create(config: ServerConfig): Application {
|
|
29
|
+
// Express app
|
|
25
30
|
const app = express()
|
|
26
|
-
|
|
31
|
+
app.set('config', config)
|
|
32
|
+
|
|
33
|
+
// CORS
|
|
34
|
+
const options = config.expressOptions
|
|
35
|
+
if (options.cors) {
|
|
36
|
+
console.log('> CORS are ENABLED:', options.cors)
|
|
37
|
+
app.use(cors({
|
|
38
|
+
origin: options.cors,
|
|
39
|
+
}))
|
|
40
|
+
}
|
|
41
|
+
// compress gzip when possible
|
|
42
|
+
app.use(compression() as any)
|
|
43
|
+
|
|
44
|
+
// cookie & session
|
|
45
|
+
app.use(bodyParser.json({ limit: options.jsonLimit }))
|
|
46
|
+
app.use(bodyParser.text({ limit: options.textLimit }))
|
|
47
|
+
console.log('> Session name:', options.sessionName)
|
|
48
|
+
app.use(cookieParser() as any)
|
|
49
|
+
app.use(session({
|
|
50
|
+
name: options.sessionName,
|
|
51
|
+
secret: options.sessionSecret,
|
|
52
|
+
}) as any)
|
|
53
|
+
return app
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function start(app: Application): Promise<Application> {
|
|
57
|
+
const config = app.get('config') as ServerConfig
|
|
58
|
+
|
|
59
|
+
// Plugins hook to create API routes
|
|
60
|
+
config.emit(EVENT_STARTUP_START, { app })
|
|
27
61
|
|
|
28
62
|
// Start server
|
|
29
63
|
return new Promise((resolve, reject) => {
|
package/src/ts/server/index.ts
CHANGED
|
@@ -15,28 +15,38 @@
|
|
|
15
15
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
|
-
import
|
|
19
|
-
import
|
|
18
|
+
import * as backends from './backends'
|
|
19
|
+
import * as silexApp from './express'
|
|
20
|
+
import { createConfig } from './config'
|
|
20
21
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
export { config, types, events, constants, page, start, getConfig }
|
|
22
|
+
//export * from './express'
|
|
23
|
+
//export * from './config'
|
|
24
|
+
//export * from './backends'
|
|
25
|
+
//export * from '../events'
|
|
26
|
+
//export * from '../constants'
|
|
27
|
+
//export * from '../page'
|
|
28
28
|
|
|
29
29
|
// Main app
|
|
30
30
|
export default async function main() {
|
|
31
31
|
// Get the default config object
|
|
32
|
-
const config =
|
|
33
|
-
|
|
34
|
-
// Here one can mutate the config object
|
|
35
|
-
// Check the docs, look for the "add silex to your project as an npm dependency"
|
|
32
|
+
const config = createConfig()
|
|
36
33
|
|
|
37
34
|
// start silex
|
|
38
|
-
const app =
|
|
35
|
+
const app = silexApp.create(config)
|
|
36
|
+
|
|
37
|
+
// Serve the client config file
|
|
38
|
+
await config.addRoutes(app)
|
|
39
|
+
|
|
40
|
+
// Load the config files
|
|
41
|
+
await config.loadConfigFiles()
|
|
42
|
+
|
|
43
|
+
// Add the backend routes
|
|
44
|
+
backends.addRoutes(config, app)
|
|
45
|
+
|
|
46
|
+
// Start the server
|
|
47
|
+
await silexApp.start(app)
|
|
39
48
|
|
|
49
|
+
// All good, server is ready
|
|
40
50
|
console.info(`
|
|
41
51
|
I'm ready, listening to port ${config.port}
|
|
42
52
|
`)
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Silex website builder, free/libre no-code tool for makers.
|
|
3
|
+
* Copyright (c) 2023 lexoyo and Silex Labs foundation
|
|
4
|
+
*
|
|
5
|
+
* This program is free software: you can redistribute it and/or modify
|
|
6
|
+
* it under the terms of the GNU Affero General Public License as published by
|
|
7
|
+
* the Free Software Foundation, either version 3 of the License, or any later version.
|
|
8
|
+
*
|
|
9
|
+
* This program is distributed in the hope that it will be useful,
|
|
10
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+
* GNU Affero General Public License for more details.
|
|
13
|
+
*
|
|
14
|
+
* You should have received a copy of the GNU Affero General Public License
|
|
15
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { JobData, JobStatus } from '../types'
|
|
19
|
+
import { startJob, jobSuccess, jobError, getJob, killJob } from './jobs'
|
|
20
|
+
|
|
21
|
+
let job: JobData | undefined
|
|
22
|
+
afterEach(() => {
|
|
23
|
+
// Avoid memory leak
|
|
24
|
+
if(job) killJob(job)
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
describe('Job Module', () => {
|
|
28
|
+
it('should start a job', () => {
|
|
29
|
+
const {jobId} = startJob('Test job')
|
|
30
|
+
job = getJob(jobId)
|
|
31
|
+
expect(job).not.toBeNull()
|
|
32
|
+
if (job) {
|
|
33
|
+
expect(job.jobId).toEqual(jobId)
|
|
34
|
+
expect(job.status).toEqual(JobStatus.IN_PROGRESS)
|
|
35
|
+
expect(job.message).toEqual('Test job')
|
|
36
|
+
killJob(job)
|
|
37
|
+
expect(getJob(jobId)).toBeUndefined()
|
|
38
|
+
}
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('should end a job with success', () => {
|
|
42
|
+
const {jobId} = startJob('Test job')
|
|
43
|
+
const result = jobSuccess(jobId, 'Job completed successfully')
|
|
44
|
+
expect(result).toBeTruthy()
|
|
45
|
+
job = getJob(jobId)
|
|
46
|
+
expect(job).not.toBeNull()
|
|
47
|
+
if (job) {
|
|
48
|
+
expect(job.status).toEqual(JobStatus.SUCCESS)
|
|
49
|
+
expect(job.message).toEqual('Job completed successfully')
|
|
50
|
+
}
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('should end a job with error', () => {
|
|
54
|
+
const {jobId} = startJob('Test job')
|
|
55
|
+
const result = jobError(jobId, 'Job ended with error')
|
|
56
|
+
expect(result).toBeTruthy()
|
|
57
|
+
job = getJob(jobId)
|
|
58
|
+
expect(job).not.toBeNull()
|
|
59
|
+
if (job) {
|
|
60
|
+
expect(job.status).toEqual(JobStatus.ERROR)
|
|
61
|
+
expect(job.message).toEqual('Job ended with error')
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
it('should not find a job after ending', done => {
|
|
66
|
+
const {jobId} = startJob('Test job')
|
|
67
|
+
jobSuccess(jobId, 'Job completed successfully')
|
|
68
|
+
setTimeout(() => {
|
|
69
|
+
job = getJob(jobId)
|
|
70
|
+
expect(job).not.toBeUndefined()
|
|
71
|
+
done()
|
|
72
|
+
}, 1000)
|
|
73
|
+
})
|
|
74
|
+
})
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Silex website builder, free/libre no-code tool for makers.
|
|
3
|
+
* Copyright (c) 2023 lexoyo and Silex Labs foundation
|
|
4
|
+
*
|
|
5
|
+
* This program is free software: you can redistribute it and/or modify
|
|
6
|
+
* it under the terms of the GNU Affero General Public License as published by
|
|
7
|
+
* the Free Software Foundation, either version 3 of the License, or any later version.
|
|
8
|
+
*
|
|
9
|
+
* This program is distributed in the hope that it will be useful,
|
|
10
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+
* GNU Affero General Public License for more details.
|
|
13
|
+
*
|
|
14
|
+
* You should have received a copy of the GNU Affero General Public License
|
|
15
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { JobData, JobStatus, JobId } from '../types'
|
|
19
|
+
|
|
20
|
+
// Delay after which the job is deleted from memory
|
|
21
|
+
const TIME_TO_KEEP_AFTER_STOP = 60*10*1000 // 10 min
|
|
22
|
+
|
|
23
|
+
// Store the jobs in memory
|
|
24
|
+
const jobs = new Array<JobData>()
|
|
25
|
+
|
|
26
|
+
// Key which will change on server restart
|
|
27
|
+
const processKey = Math.ceil(Math.random() * 1000000)
|
|
28
|
+
|
|
29
|
+
// Next id for the next job
|
|
30
|
+
let nextJobId = 0
|
|
31
|
+
|
|
32
|
+
// Create a new job
|
|
33
|
+
export function startJob(message = ''): JobData {
|
|
34
|
+
const jobId = `${processKey}_${++nextJobId}`
|
|
35
|
+
const job: JobData = {
|
|
36
|
+
jobId,
|
|
37
|
+
status: JobStatus.IN_PROGRESS,
|
|
38
|
+
message: message
|
|
39
|
+
}
|
|
40
|
+
jobs.push(job)
|
|
41
|
+
return job
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Retriev a job if it exists
|
|
45
|
+
export function getJob(id: JobId): JobData | undefined {
|
|
46
|
+
return jobs.find(job => job.jobId === id)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
// End the job with success
|
|
51
|
+
export function jobSuccess(id: JobId, message = ''): boolean {
|
|
52
|
+
return end(id, JobStatus.SUCCESS, message)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// End the job with error
|
|
56
|
+
export function jobError(id: JobId, message = ''): boolean {
|
|
57
|
+
return end(id, JobStatus.ERROR, message)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Remove a job from memory
|
|
61
|
+
// This is also used in unit tests to avoid memory lea
|
|
62
|
+
export function killJob(job: JobData) {
|
|
63
|
+
clearTimeout(job._timeout)
|
|
64
|
+
jobs.splice(jobs.findIndex(j => job.jobId === j.jobId), 1)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// End the job
|
|
68
|
+
function end(id: JobId, status: JobStatus, message = ''): boolean {
|
|
69
|
+
const job = getJob(id)
|
|
70
|
+
if(job) {
|
|
71
|
+
job.status = status
|
|
72
|
+
if(message) job.message = message
|
|
73
|
+
job._timeout = setTimeout(() => killJob(job), TIME_TO_KEEP_AFTER_STOP)
|
|
74
|
+
return true
|
|
75
|
+
}
|
|
76
|
+
return false
|
|
77
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Silex website builder, free/libre no-code tool for makers.
|
|
3
|
+
* Copyright (c) 2023 lexoyo and Silex Labs foundation
|
|
4
|
+
*
|
|
5
|
+
* This program is free software: you can redistribute it and/or modify
|
|
6
|
+
* it under the terms of the GNU Affero General Public License as published by
|
|
7
|
+
* the Free Software Foundation, either version 3 of the License, or any later version.
|
|
8
|
+
*
|
|
9
|
+
* This program is distributed in the hope that it will be useful,
|
|
10
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+
* GNU Affero General Public License for more details.
|
|
13
|
+
*
|
|
14
|
+
* You should have received a copy of the GNU Affero General Public License
|
|
15
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Throw an error if a parameter is missing
|
|
20
|
+
* @param value the value to check
|
|
21
|
+
* @param name the name of the parameter
|
|
22
|
+
* @throws Error if the parameter is missing
|
|
23
|
+
*/
|
|
24
|
+
export function requiredParam<T>(value: T | undefined, name: string, defaultValue?: T): NonNullable<T> {
|
|
25
|
+
if (value === undefined) {
|
|
26
|
+
if (defaultValue !== undefined) {
|
|
27
|
+
return defaultValue as NonNullable<T>
|
|
28
|
+
}
|
|
29
|
+
console.error(`Missing required parameter ${name}`)
|
|
30
|
+
throw new Error(`Missing required parameter ${name}`)
|
|
31
|
+
}
|
|
32
|
+
return value as NonNullable<T>
|
|
33
|
+
}
|