@silexlabs/silex 3.0.0-alpha.43 → 3.0.0-alpha.45

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. package/.env.default +18 -0
  2. package/dist/client/client/api.d.ts +6 -6
  3. package/dist/client/client/config.d.ts +26 -211
  4. package/dist/client/client/events.d.ts +2 -0
  5. package/dist/client/client/expose.d.ts +0 -1
  6. package/dist/client/client/grapesjs/LoginDialog.d.ts +1 -0
  7. package/dist/client/client/grapesjs/index.d.ts +3 -209
  8. package/dist/client/client/index.d.ts +1 -0
  9. package/dist/client/constants.d.ts +2 -3
  10. package/dist/client/css/admin.css +2 -0
  11. package/dist/client/js/main.js +47 -47
  12. package/dist/client/types.d.ts +7 -1
  13. package/dist/plugins/client/client/events.d.ts +2 -0
  14. package/dist/plugins/client/client/events.js +2 -0
  15. package/dist/plugins/client/client/events.js.map +1 -1
  16. package/dist/plugins/client/plugins/client/template.js +4 -4
  17. package/dist/plugins/client/plugins/client/template.js.map +1 -1
  18. package/dist/plugins/client/types.d.ts +7 -1
  19. package/dist/plugins/client/types.js +8 -0
  20. package/dist/plugins/client/types.js.map +1 -1
  21. package/dist/plugins/server/constants.d.ts +2 -3
  22. package/dist/plugins/server/constants.js +3 -4
  23. package/dist/plugins/server/constants.js.map +1 -1
  24. package/dist/plugins/server/plugins/server/FtpConnector.d.ts +17 -28
  25. package/dist/plugins/server/plugins/server/FtpConnector.js +16 -16
  26. package/dist/plugins/server/plugins/server/FtpConnector.js.map +1 -1
  27. package/dist/plugins/server/plugins/server/GitlabConnector.d.ts +81 -0
  28. package/dist/plugins/server/plugins/server/GitlabConnector.js +373 -0
  29. package/dist/plugins/server/plugins/server/GitlabConnector.js.map +1 -0
  30. package/dist/plugins/server/server/config.js +1 -1
  31. package/dist/plugins/server/server/config.js.map +1 -1
  32. package/dist/plugins/server/server/connectors/FsStorage.d.ts +13 -12
  33. package/dist/plugins/server/server/connectors/FsStorage.js +22 -15
  34. package/dist/plugins/server/server/connectors/FsStorage.js.map +1 -1
  35. package/dist/plugins/server/server/connectors/connectors.d.ts +5 -2
  36. package/dist/plugins/server/server/connectors/connectors.js +31 -3
  37. package/dist/plugins/server/server/connectors/connectors.js.map +1 -1
  38. package/dist/plugins/server/types.d.ts +7 -1
  39. package/dist/plugins/server/types.js +10 -1
  40. package/dist/plugins/server/types.js.map +1 -1
  41. package/dist/server/constants.js +3 -4
  42. package/dist/server/server/api/connectorApi.js +16 -11
  43. package/dist/server/server/api/publicationApi.js +18 -20
  44. package/dist/server/server/api/websiteApi.js +36 -40
  45. package/dist/server/server/cli.js +34 -16
  46. package/dist/server/server/config.js +1 -1
  47. package/dist/server/server/connectors/FsStorage.js +19 -15
  48. package/dist/server/server/connectors/connectors.js +27 -3
  49. package/dist/server/server/expose.js +27 -0
  50. package/dist/server/server/index.js +28 -2
  51. package/dist/server/types.js +10 -1
  52. package/package.json +4 -1
  53. package/src/css/_variables.scss +1 -0
  54. package/src/css/loader.scss +2 -0
  55. package/src/ts/client/api.ts +8 -11
  56. package/src/ts/client/config.ts +30 -14
  57. package/src/ts/client/events.ts +2 -0
  58. package/src/ts/client/expose.ts +0 -1
  59. package/src/ts/client/grapesjs/LoginDialog.ts +12 -1
  60. package/src/ts/client/grapesjs/PublicationManager.ts +1 -1
  61. package/src/ts/client/grapesjs/index.ts +8 -13
  62. package/src/ts/client/grapesjs/storage.ts +18 -10
  63. package/src/ts/client/index.ts +32 -14
  64. package/src/ts/constants.ts +2 -3
  65. package/src/ts/plugins/client/template.ts +4 -4
  66. package/src/ts/plugins/server/FtpConnector.test.ts +82 -0
  67. package/src/ts/plugins/server/FtpConnector.ts +30 -37
  68. package/src/ts/plugins/server/GitlabConnector.ts +434 -0
  69. package/src/ts/server/api/connectorApi.ts +31 -26
  70. package/src/ts/server/api/publicationApi.ts +23 -23
  71. package/src/ts/server/api/websiteApi.ts +42 -43
  72. package/src/ts/server/cli.ts +23 -11
  73. package/src/ts/server/config.ts +1 -1
  74. package/src/ts/server/connectors/FsStorage.test.ts +51 -0
  75. package/src/ts/server/connectors/FsStorage.ts +35 -25
  76. package/src/ts/server/connectors/connectors.ts +32 -4
  77. package/{dist/plugins/client/server/utils/validation.js → src/ts/server/expose.ts} +3 -18
  78. package/src/ts/server/index.ts +24 -2
  79. package/src/ts/types.ts +10 -1
  80. package/dist/plugins/client/constants.d.ts +0 -30
  81. package/dist/plugins/client/constants.js +0 -47
  82. package/dist/plugins/client/constants.js.map +0 -1
  83. package/dist/plugins/client/server/config.d.ts +0 -59
  84. package/dist/plugins/client/server/config.js +0 -178
  85. package/dist/plugins/client/server/config.js.map +0 -1
  86. package/dist/plugins/client/server/connectors/FsHosting.d.ts +0 -13
  87. package/dist/plugins/client/server/connectors/FsHosting.js +0 -51
  88. package/dist/plugins/client/server/connectors/FsHosting.js.map +0 -1
  89. package/dist/plugins/client/server/connectors/FsStorage.d.ts +0 -41
  90. package/dist/plugins/client/server/connectors/FsStorage.js +0 -220
  91. package/dist/plugins/client/server/connectors/FsStorage.js.map +0 -1
  92. package/dist/plugins/client/server/connectors/connectors.d.ts +0 -83
  93. package/dist/plugins/client/server/connectors/connectors.js +0 -91
  94. package/dist/plugins/client/server/connectors/connectors.js.map +0 -1
  95. package/dist/plugins/client/server/events.d.ts +0 -26
  96. package/dist/plugins/client/server/events.js +0 -12
  97. package/dist/plugins/client/server/events.js.map +0 -1
  98. package/dist/plugins/client/server/jobs.d.ts +0 -6
  99. package/dist/plugins/client/server/jobs.js +0 -67
  100. package/dist/plugins/client/server/jobs.js.map +0 -1
  101. package/dist/plugins/client/server/utils/validation.d.ts +0 -7
  102. package/dist/plugins/client/server/utils/validation.js.map +0 -1
@@ -18,7 +18,7 @@
18
18
  import { Router } from 'express'
19
19
  import formidable from 'formidable'
20
20
  import PersistentFile from 'formidable/src/PersistentFile'
21
- import { API_WEBSITE_ASSETS_READ, API_WEBSITE_ASSETS_WRITE, API_WEBSITE_READ, API_WEBSITE_WRITE, API_WEBSITE_DELETE, WEBSITE_DATA_FILE, API_WEBSITE_META_READ, API_WEBSITE_META_WRITE, API_WEBSITE_LIST, API_WEBSITE_CREATE } from '../../constants'
21
+ import { API_WEBSITE_ASSET_READ, API_WEBSITE_ASSETS_WRITE, API_WEBSITE_READ, API_WEBSITE_WRITE, API_WEBSITE_DELETE, API_WEBSITE_META_READ, API_WEBSITE_META_WRITE, API_WEBSITE_LIST, API_WEBSITE_CREATE, API_PATH, API_WEBSITE_PATH } from '../../constants'
22
22
  import { createReadStream } from 'fs'
23
23
  import { ApiError, ApiWebsiteAssetsReadParams, ApiWebsiteAssetsReadQuery, ApiWebsiteAssetsReadResponse, ApiWebsiteAssetsWriteQuery, ApiWebsiteAssetsWriteResponse, ApiWebsiteDeleteQuery, ApiWebsiteReadQuery, ApiWebsiteReadResponse, ApiWebsiteWriteBody, ApiWebsiteWriteQuery, ConnectorId, ConnectorType, WebsiteMeta, WebsiteData, WebsiteId, ApiWebsiteListQuery, ApiWebsiteListResponse, ApiWebsiteMetaReadQuery, ApiWebsiteMetaReadResponse, ApiWebsiteMetaWriteQuery, ApiWebsiteMetaWriteBody, WebsiteMetaFileContent, ApiWebsiteMetaWriteResponse, ApiWebsiteWriteResponse, ApiWebsiteCreateQuery, ApiWebsiteCreateBody } from '../../types'
24
24
  import { ConnectorFile, ConnectorFileContent, ConnectorSession, StorageConnector, getConnector } from '../connectors/connectors'
@@ -33,17 +33,6 @@ import { ServerEvent, WebsiteStoreEndEventType, WebsiteStoreStartEventType, Webs
33
33
  * This plugin provides the website API to Silex server
34
34
  */
35
35
 
36
- /**
37
- * Error thrown by the website API
38
- * @param message error message
39
- * @param code http status code
40
- */
41
- export class WebsiteError extends Error {
42
- constructor(message: string, public code: number) {
43
- super(message)
44
- }
45
- }
46
-
47
36
  export default function (config: ServerConfig, opts = {}): Router {
48
37
  // Options with defaults
49
38
  const options = {
@@ -79,9 +68,8 @@ export default function (config: ServerConfig, opts = {}): Router {
79
68
  res.json(websiteData as ApiWebsiteReadResponse)
80
69
  }
81
70
  } catch (e) {
82
- console.error('Error getting website data', e)
83
- if (e instanceof WebsiteError) {
84
- res.status(e.code).json({ message: e.message } as ApiError)
71
+ if (e.httpStatusCode) {
72
+ res.status(e.httpStatusCode).json({ message: e.message } as ApiError)
85
73
  } else {
86
74
  res.status(500).json({ message: e.message } as ApiError)
87
75
  }
@@ -99,8 +87,8 @@ export default function (config: ServerConfig, opts = {}): Router {
99
87
  res.json(websites as ApiWebsiteListResponse)
100
88
  } catch (e) {
101
89
  console.error('Error getting website data', e)
102
- if (e instanceof WebsiteError) {
103
- res.status(e.code).json({ message: e.message } as ApiError)
90
+ if (e.httpStatusCode) {
91
+ res.status(e.httpStatusCode).json({ message: e.message } as ApiError)
104
92
  } else {
105
93
  res.status(500).json({ message: e.message } as ApiError)
106
94
  }
@@ -130,8 +118,8 @@ export default function (config: ServerConfig, opts = {}): Router {
130
118
  } catch (e) {
131
119
  console.error('Error saving website data', e)
132
120
  config.emit(ServerEvent.WEBSITE_STORE_END, e as WebsiteStoreEndEventType)
133
- if (e instanceof WebsiteError) {
134
- res.status(e.code).json({ message: e.message } as ApiError)
121
+ if (e.httpStatusCode) {
122
+ res.status(e.httpStatusCode).json({ message: e.message } as ApiError)
135
123
  } else {
136
124
  res.status(500).json({ message: e.message } as ApiError)
137
125
  }
@@ -149,7 +137,7 @@ export default function (config: ServerConfig, opts = {}): Router {
149
137
  const connectorId = query.connectorId
150
138
  const connector = await getConnector<StorageConnector>(config, session, ConnectorType.STORAGE, connectorId)
151
139
  if(!connector) {
152
- throw new WebsiteError(`Connector ${connectorId} not found`, 500)
140
+ throw new ApiError(`Connector ${connectorId} not found`, 500)
153
141
  }
154
142
  // Create website
155
143
  await connector.createWebsite(
@@ -159,8 +147,8 @@ export default function (config: ServerConfig, opts = {}): Router {
159
147
  res.json({ message: 'Website meta saved' } as ApiWebsiteMetaWriteResponse)
160
148
  } catch (e) {
161
149
  console.error('Error saving website meta', e)
162
- if (e instanceof WebsiteError) {
163
- res.status(e.code).json({ message: e.message } as ApiError)
150
+ if (e.httpStatusCode) {
151
+ res.status(e.httpStatusCode).json({ message: e.message } as ApiError)
164
152
  } else {
165
153
  res.status(500).json({ message: e.message } as ApiError)
166
154
  }
@@ -179,7 +167,7 @@ export default function (config: ServerConfig, opts = {}): Router {
179
167
  const connectorId = query.connectorId
180
168
  const connector = await getConnector<StorageConnector>(config, session, ConnectorType.STORAGE, connectorId)
181
169
  if(!connector) {
182
- throw new WebsiteError(`Connector ${connectorId} not found`, 500)
170
+ throw new ApiError(`Connector ${connectorId} not found`, 500)
183
171
  }
184
172
  // Update website meta
185
173
  await connector.setWebsiteMeta(
@@ -190,8 +178,8 @@ export default function (config: ServerConfig, opts = {}): Router {
190
178
  res.json({ message: 'Website meta saved' } as ApiWebsiteMetaWriteResponse)
191
179
  } catch (e) {
192
180
  console.error('Error saving website meta', e)
193
- if (e instanceof WebsiteError) {
194
- res.status(e.code).json({ message: e.message } as ApiError)
181
+ if (e.httpStatusCode) {
182
+ res.status(e.httpStatusCode).json({ message: e.message } as ApiError)
195
183
  } else {
196
184
  res.status(500).json({ message: e.message } as ApiError)
197
185
  }
@@ -207,14 +195,14 @@ export default function (config: ServerConfig, opts = {}): Router {
207
195
  const connectorId = query.connectorId
208
196
  const connector = await getConnector<StorageConnector>(config, session, ConnectorType.STORAGE, connectorId)
209
197
  if(!connector) {
210
- throw new WebsiteError(`Connector ${connectorId} not found`, 500)
198
+ throw new ApiError(`Connector ${connectorId} not found`, 500)
211
199
  }
212
200
  const websiteMeta: WebsiteMeta = await connector.getWebsiteMeta(session, websiteId)
213
201
  res.json(websiteMeta as ApiWebsiteMetaReadResponse)
214
202
  } catch (e) {
215
203
  console.error('Error getting website meta', e)
216
- if (e instanceof WebsiteError) {
217
- res.status(e.code).json({ message: e.message } as ApiError)
204
+ if (e.httpStatusCode) {
205
+ res.status(e.httpStatusCode).json({ message: e.message } as ApiError)
218
206
  } else {
219
207
  res.status(500).json({ message: e.message } as ApiError)
220
208
  }
@@ -230,8 +218,8 @@ export default function (config: ServerConfig, opts = {}): Router {
230
218
  res.status(200).json({ message: 'Website deleted' } as ApiError)
231
219
  } catch (e) {
232
220
  console.error('Error deleting website data', e)
233
- if (e instanceof WebsiteError) {
234
- res.status(e.code).json({ message: e.message } as ApiError)
221
+ if (e.httpStatusCode) {
222
+ res.status(e.httpStatusCode).json({ message: e.message } as ApiError)
235
223
  } else {
236
224
  res.status(500).json({ message: e.message } as ApiError)
237
225
  }
@@ -239,7 +227,7 @@ export default function (config: ServerConfig, opts = {}): Router {
239
227
  })
240
228
 
241
229
  // Load assets
242
- router.get(API_WEBSITE_ASSETS_READ, async (req, res) => {
230
+ router.get(API_WEBSITE_ASSET_READ + '/:path', async (req, res) => {
243
231
  {
244
232
  try {
245
233
  const query: ApiWebsiteAssetsReadQuery = req.query as any
@@ -259,8 +247,8 @@ export default function (config: ServerConfig, opts = {}): Router {
259
247
  }
260
248
  } catch (e) {
261
249
  console.error('Error getting asset', e)
262
- if (e instanceof WebsiteError) {
263
- res.status(e.code).json({ message: e.message } as ApiError)
250
+ if (e.httpStatusCode) {
251
+ res.status(e.httpStatusCode).json({ message: e.message } as ApiError)
264
252
  } else {
265
253
  res.status(500).json({ message: e.message } as ApiError)
266
254
  }
@@ -288,12 +276,12 @@ export default function (config: ServerConfig, opts = {}): Router {
288
276
  form.parse(req, async (err, fields, _files) => {
289
277
  if (err) {
290
278
  console.error('Error parsing upload data', err)
291
- reject(new WebsiteError('Error parsing upload data: ' + err.message, 400))
279
+ reject(new ApiError('Error parsing upload data: ' + err.message, 400))
292
280
  } else {
293
281
  const files = ([].concat(_files['files[]']) as PersistentFile[])
294
282
  .map(file => file.toJSON())
295
283
  .map(file => ({
296
- path: `${options.assetsPath}/${file.originalFilename}`,
284
+ path: `/${file.originalFilename}`,
297
285
  content: createReadStream(file.filepath),
298
286
  }))
299
287
  resolve(files)
@@ -305,19 +293,24 @@ export default function (config: ServerConfig, opts = {}): Router {
305
293
  config.emit(ServerEvent.WEBSITE_ASSET_STORE_START, { files, websiteId, connectorId } as WebsiteAssetStoreStartEventType)
306
294
 
307
295
  // Write the files
308
- const data = await writeAssets(req['session'], websiteId, files, connectorId)
296
+ await writeAssets(req['session'], websiteId, files, connectorId)
309
297
 
310
298
  // Return the file URLs to insert in the website
299
+ // As expected by grapesjs (https://grapesjs.com/docs/modules/Assets.html#uploading-assets)
311
300
  res.json({
312
- data, // As expected by grapesjs (https://grapesjs.com/docs/modules/Assets.html#uploading-assets)
301
+ data: files.map(file =>
302
+ API_PATH + API_WEBSITE_PATH + API_WEBSITE_ASSET_READ
303
+ + file.path
304
+ + `?websiteId=${websiteId}&connectorId=${connectorId ? connectorId : ''}` // As expected by wesite API (readAsset)
305
+ ),
313
306
  } as ApiWebsiteAssetsWriteResponse)
314
307
 
315
308
  // Hook for plugins
316
309
  config.emit(ServerEvent.WEBSITE_ASSET_STORE_END, null as WebsiteAssetStoreEndEventType)
317
310
  } catch (e) {
318
311
  console.error('Error uploading assets', e)
319
- if (e instanceof WebsiteError) {
320
- res.status(e.code).json({ message: e.message } as ApiError)
312
+ if (e.httpStatusCode) {
313
+ res.status(e.httpStatusCode).json({ message: e.message } as ApiError)
321
314
  } else {
322
315
  res.status(500).json({ message: e.message } as ApiError)
323
316
  }
@@ -334,11 +327,11 @@ export default function (config: ServerConfig, opts = {}): Router {
334
327
  const storageConnector = await getConnector(config, session, ConnectorType.STORAGE, connectorId) // ?? config.getStorageConnectors()[0]
335
328
 
336
329
  if (!storageConnector) {
337
- throw new WebsiteError('No storage connector found', 404)
330
+ throw new ApiError('No storage connector found', 404)
338
331
  }
339
332
 
340
333
  if (!await storageConnector.isLoggedIn(session)) {
341
- throw new WebsiteError('Not logged in', 401)
334
+ throw new ApiError('Not logged in', 401)
342
335
  }
343
336
 
344
337
  return storageConnector as StorageConnector
@@ -396,7 +389,7 @@ export default function (config: ServerConfig, opts = {}): Router {
396
389
  const storageConnector = await getStorageConnector(session, connectorId)
397
390
 
398
391
  // Read the asset from the connector
399
- return storageConnector.readAsset(session, websiteId, `/${options.assetsPath}/${fileName}`)
392
+ return storageConnector.readAsset(session, websiteId, `/${fileName}`)
400
393
  }
401
394
 
402
395
  /**
@@ -406,11 +399,17 @@ export default function (config: ServerConfig, opts = {}): Router {
406
399
  // Get the desired connector
407
400
  const storageConnector = await getStorageConnector(session, connectorId)
408
401
 
402
+ // Clean up the path
403
+ const cleanPathFiles = files.map(file => ({
404
+ ...file,
405
+ path: file.path.replace('/assets/', '/'), // Remove the assets folder added by GrapesJS
406
+ }))
407
+
409
408
  // Write the asset to the connector
410
409
  await storageConnector.writeAssets(
411
410
  session,
412
411
  websiteId,
413
- files
412
+ cleanPathFiles,
414
413
  )
415
414
 
416
415
  // Return the files URLs with the website id
@@ -21,31 +21,43 @@ import silex from './index'
21
21
  import { parse } from 'cli'
22
22
 
23
23
  const options = parse({
24
- config: [ 'c', 'Path for the server side config file to load at startup', 'file'],
25
- port: [ 'p', 'Port to listen to', 'int'],
26
- debug: [false, 'Debug mode, with live reload, source maps etc.', 'boolean'],
24
+ 'host': ['', 'Sets the host param of the express module "cookie-session".', 'string'],
25
+ 'port': [ 'p', 'Port to listen to', 'int'],
26
+ 'protocol': ['', 'Sets the protocol param of the express module "cookie-session".', 'string'],
27
+ 'config': [ 'c', 'Path for the server side config file to load at startup', 'file'],
28
+ 'client-config': ['', 'Path to client config file to be served on .silex-client.js', 'string'],
29
+ 'fs-root': ['', 'Path to the root folder where to store websites. Used by the default connector (fs).', 'string'],
30
+ 'session-name': ['', 'Sets the name param of the express module "cookie-session".', 'string'],
31
+ 'session-secret': ['s', 'Session secret', 'string'],
27
32
  'ssl-port': [ false, 'Port to listen to for SSL/HTTPS', 'int'],
28
33
  'force-https': [ false, 'Force HTTPS', 'boolean'],
29
34
  'ssl-private-key': [ false, 'Path to private key for SSL', 'file'],
30
35
  'ssl-certificate': [ false, 'Path to SSL certificate', 'file'],
31
36
  'force-https-trust-xfp-header': [ false, 'Sets the trustXFPHeader param of the express module "cookie-session". Use only with --force-https', 'boolean'],
32
- 'session-secret': ['s', 'Session secret', 'string'],
33
37
  'cors-url': ['', 'Enable CORS for URL (can be "*")', 'string'],
34
- 'client-config': ['', 'Path to client config file to be served on .silex-client.js', 'string'],
35
- 'fs-root': ['', 'Path to the root folder where to store websites. Used by the default connector (fs).', 'string'],
38
+ 'express-json-limit': ['', 'Sets the limit param of the express module "json".', 'string'],
39
+ 'express-text-limit': ['', 'Sets the limit param of the express module "text".', 'string'],
40
+ 'express-urlencoded-limit': ['', 'Sets the limit param of the express module "urlencoded".', 'string'],
41
+ 'debug': [false, 'Debug mode, with live reload, source maps etc.', 'boolean'],
36
42
  }, {})
37
43
 
38
- if(options.config) process.env.SILEX_CONFIG = options.config
44
+ if(options['host']) process.env.SILEX_HOST = options['host']
39
45
  if(options.port) process.env.SILEX_PORT = options.port
40
- if(options.debug) process.env.SILEX_DEBUG = options.debug
46
+ if(options['protocol']) process.env.SILEX_PROTOCOL = options['protocol']
47
+ if(options.config) process.env.SILEX_CONFIG = options.config
48
+ if(options['client-config']) process.env.SILEX_CLIENT_CONFIG = options['client-config']
49
+ if(options['fs-root']) process.env.SILEX_FS_ROOT = options['fs-root']
50
+ if(options['session-name']) process.env.SILEX_SESSION_NAME = options['session-name']
51
+ if(options['session-secret']) process.env.SILEX_SESSION_SECRET = options['session-secret']
41
52
  if(options['ssl-port']) process.env.SILEX_SSL_PORT = options['ssl-port']
42
53
  if(options['force-https']) process.env.SILEX_FORCE_HTTPS = options['force-https']
43
54
  if(options['ssl-private-key']) process.env.SILEX_SSL_PRIVATE_KEY = options['ssl-private-key']
44
55
  if(options['ssl-certificate']) process.env.SILEX_SSL_CERTIFICATE = options['ssl-certificate']
45
56
  if(options['force-https-trust-xfp-header']) process.env.SILEX_FORCE_HTTPS_TRUST_XFP_HEADER = options['force-https-trust-xfp-header']
46
- if(options['session-secret']) process.env.SILEX_SESSION_SECRET = options['session-secret']
47
57
  if(options['cors-url']) process.env.SILEX_CORS_URL = options['cors-url']
48
- if(options['client-config']) process.env.SILEX_CLIENT_CONFIG = options['client-config']
49
- if(options['fs-root']) process.env.SILEX_FS_ROOT = options['fs-root']
58
+ if(options['express-json-limit']) process.env.SILEX_EXPRESS_JSON_LIMIT = options['express-json-limit']
59
+ if(options['express-text-limit']) process.env.SILEX_EXPRESS_TEXT_LIMIT = options['express-text-limit']
60
+ if(options['express-urlencoded-limit']) process.env.SILEX_EXPRESS_URLENCODED_LIMIT = options['express-urlencoded-limit']
61
+ if(options.debug) process.env.SILEX_DEBUG = options.debug
50
62
 
51
63
  silex()
@@ -148,7 +148,7 @@ export class ServerConfig extends Config {
148
148
  // Initiate the process with the config file which is just another plugin
149
149
  await this.addPlugin(this.userConfigPath, {})
150
150
  } catch (e) {
151
- throw new Error(`User config file ${this.userConfigPath} error: ${e.message}`)
151
+ throw new Error(`\nUser config file ${this.userConfigPath} error:\n\n\t${e.message}\n\n`)
152
152
  }
153
153
  }
154
154
  }
@@ -0,0 +1,51 @@
1
+ import { expect, beforeEach, afterAll, it, describe } from '@jest/globals'
2
+ import { ServerConfig } from '../config'
3
+ import { FsStorage } from './FsStorage'
4
+ import { readFileSync, rmdirSync, statSync } from 'fs'
5
+ import { join } from 'path'
6
+
7
+ const storageRootPath = '/tmp/silex-tests'
8
+ const assetsFolder = 'assets'
9
+ const dummySession = {}
10
+ const dummyId = 'dummy id'
11
+ const dummyWebsite = {
12
+ pages: [],
13
+ assets: [],
14
+ styles: [],
15
+ settings: {},
16
+ fonts: [],
17
+ symbols: [],
18
+ publication: {},
19
+ }
20
+
21
+ beforeEach(() => {
22
+ })
23
+
24
+ afterAll(() => {
25
+ rmdirSync(storageRootPath, { recursive: true })
26
+ })
27
+
28
+ async function sleep(ms: number) {
29
+ return new Promise(resolve => setTimeout(resolve, ms))
30
+ }
31
+ describe('FsStorage website', () => {
32
+ it('should create the root folder', async () => {
33
+ expect(() => statSync(storageRootPath)).toThrow()
34
+ const connector = new FsStorage({} as ServerConfig, {
35
+ path: storageRootPath,
36
+ assetsFolder,
37
+ })
38
+ await sleep(100)
39
+ expect(() => statSync(storageRootPath)).not.toThrow()
40
+ })
41
+ it('should create a new website', async () => {
42
+ const connector = new FsStorage({} as ServerConfig, {
43
+ path: storageRootPath,
44
+ assetsFolder,
45
+ })
46
+ const id = await connector.createWebsite(dummySession, { name: 'dummy name', connectorUserSettings: {} })
47
+ expect(id).toBeDefined()
48
+ expect(() => statSync(join(storageRootPath, id, 'website.json'))).not.toThrow()
49
+ expect(readFileSync(join(storageRootPath, id, 'website.json'), 'utf8')).toBe(JSON.stringify({}))
50
+ })
51
+ })
@@ -18,7 +18,7 @@
18
18
  import fs, { stat } from 'fs/promises'
19
19
  import { createWriteStream } from 'fs'
20
20
  import { ConnectorFile, StorageConnector, StatusCallback, ConnectorSession, toConnectorData, ConnectorFileContent} from './connectors'
21
- import { join } from 'path'
21
+ import { dirname, join } from 'path'
22
22
  import { ConnectorUser, WebsiteMeta, JobStatus, WebsiteId, ConnectorType, WebsiteMetaFileContent, WebsiteData, defaultWebsiteData, ConnectorOptions } from '../../types'
23
23
  import { userInfo } from 'os'
24
24
  import { requiredParam } from '../utils/validation'
@@ -26,6 +26,14 @@ import { ServerConfig } from '../config'
26
26
  import { DEFAULT_WEBSITE_ID, WEBSITE_DATA_FILE, WEBSITE_META_DATA_FILE } from '../../constants'
27
27
  import { Readable } from 'stream'
28
28
  import { v4 as uuid } from 'uuid'
29
+ import { fileURLToPath } from 'url'
30
+
31
+ // Variables needed for jest tests
32
+ if(!globalThis.__dirname) {
33
+ // @ts-ignore
34
+ globalThis.__dirname = dirname(process.cwd() + '/src/ts/server/connectors/FsStorage.ts')
35
+ console.log('Redefining __dirname', globalThis.__dirname)
36
+ }
29
37
 
30
38
  type FsSession = ConnectorSession
31
39
 
@@ -33,7 +41,8 @@ const USER_ICON = 'data:image/svg+xml,%3Csvg xmlns=\'http://www.w3.org/2000/svg\
33
41
  const FILE_ICON = 'data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%2024%2024%22%20fill%3D%22none%22%20stroke%3D%22currentColor%22%20stroke-width%3D%222%22%20stroke-linecap%3D%22round%22%20stroke-linejoin%3D%22round%22%3E%3Cpath%20d%3D%22M6%202L6%2022%2018%2022%2018%207%2012%202%206%202Z%22%3E%3C%2Fpath%3E%3Cpath%20d%3D%22M18%202L12%202%2012%208%2018%208%2018%202Z%22%3E%3C%2Fpath%3E%3C%2Fsvg%3E'
34
42
 
35
43
  interface FsOptions {
36
- path?: string
44
+ path: string
45
+ assetsFolder: string
37
46
  }
38
47
 
39
48
  export class FsStorage implements StorageConnector<FsSession> {
@@ -41,22 +50,26 @@ export class FsStorage implements StorageConnector<FsSession> {
41
50
  displayName = 'File system storage'
42
51
  icon = FILE_ICON
43
52
  disableLogout = true
44
- options: { path: string }
53
+ options: FsOptions
45
54
  connectorType = ConnectorType.STORAGE
55
+ color = '#ffffff'
56
+ background = '#006400'
46
57
 
47
- constructor(config: ServerConfig | null, opts: FsOptions) {
58
+ constructor(config: ServerConfig | null, opts: Partial<FsOptions>) {
48
59
  this.options = {
49
- path: __dirname + '../../../../.silex',
60
+ path: join(__dirname, '..', '..', '..', '..', 'data'),
61
+ assetsFolder: '/assets',
50
62
  ...opts,
51
63
  }
52
64
  this.initFs()
53
65
  }
54
66
 
55
- async initFs() {
67
+ private async initFs() {
56
68
  const stat = await fs.stat(this.options.path).catch(() => null)
57
69
  if (!stat) {
70
+ // create data folder with a default website
58
71
  const id = DEFAULT_WEBSITE_ID
59
- await fs.mkdir(join(this.options.path, id), { recursive: true })
72
+ await fs.mkdir(join(this.options.path, id, this.options.assetsFolder), { recursive: true })
60
73
  await this.setWebsiteMeta({}, id, { name: 'Default website', connectorUserSettings: {} })
61
74
  await this.updateWebsite({}, id, defaultWebsiteData)
62
75
  }
@@ -65,14 +78,14 @@ export class FsStorage implements StorageConnector<FsSession> {
65
78
  // ********************
66
79
  // Job utils methods
67
80
  // ********************
68
- updateStatus(filesStatuses, status, statusCbk) {
81
+ private updateStatus(filesStatuses, status, statusCbk) {
69
82
  statusCbk && statusCbk({
70
83
  message: `<p>Writing files:<ul><li>${filesStatuses.map(({file, message}) => `${file.path}: ${message}`).join('</li><li>')}</li></ul></p>`,
71
84
  status,
72
85
  })
73
86
  }
74
87
 
75
- initStatus(files) {
88
+ private initStatus(files) {
76
89
  return files.map(file => ({
77
90
  file,
78
91
  message: 'Waiting',
@@ -87,13 +100,12 @@ export class FsStorage implements StorageConnector<FsSession> {
87
100
  return {}
88
101
  }
89
102
 
90
- async getOAuthUrl(session: object): Promise<string | null> {
91
- return null
92
- }
93
- async getLoginForm(session: object, redirectTo: string): Promise<string | null> {
103
+ async getOAuthUrl(session: FsSession): Promise<null> { return null }
104
+
105
+ async getLoginForm(session: FsSession, redirectTo: string): Promise<string | null> {
94
106
  return null
95
107
  }
96
- async getSettingsForm(session: object, redirectTo: string): Promise<string | null> {
108
+ async getSettingsForm(session: FsSession, redirectTo: string): Promise<string | null> {
97
109
  return null
98
110
  }
99
111
 
@@ -145,7 +157,7 @@ export class FsStorage implements StorageConnector<FsSession> {
145
157
  // ********************
146
158
  async createWebsite(session: FsSession, meta: WebsiteMetaFileContent): Promise<WebsiteId> {
147
159
  const id = uuid()
148
- await fs.mkdir(join(this.options.path, id), { recursive: true })
160
+ await fs.mkdir(join(this.options.path, id, this.options.assetsFolder), { recursive: true })
149
161
  await this.setWebsiteMeta(session, id, meta)
150
162
  await this.updateWebsite(session, id, defaultWebsiteData)
151
163
  return id
@@ -179,19 +191,17 @@ export class FsStorage implements StorageConnector<FsSession> {
179
191
  }
180
192
 
181
193
  async getAsset(session: FsSession, id: WebsiteId, path: string): Promise<ConnectorFile> {
182
- const fullPath = join(this.options.path, id, path)
194
+ const fullPath = join(this.options.path, id, this.options.assetsFolder, path)
183
195
  const content = await fs.readFile(fullPath)
184
196
  return { path, content }
185
197
  }
186
198
 
187
- async writeAssets(session: FsSession, id: WebsiteId, files: ConnectorFile[], statusCbk?: StatusCallback): Promise<string[]> {
199
+ async writeAssets(session: FsSession, id: WebsiteId, files: ConnectorFile[], statusCbk?: StatusCallback): Promise<void> {
188
200
  const filesStatuses = this.initStatus(files)
189
- let error = false
190
- const filePaths = [] as string[]
201
+ let error: Error | null = null
191
202
  for (const fileStatus of filesStatuses) {
192
203
  const {file} = fileStatus
193
- const path = join(this.options.path, id, file.path)
194
- filePaths.push(path)
204
+ const path = join(this.options.path, id, this.options.assetsFolder, file.path)
195
205
  if (typeof file.content === 'string' || Buffer.isBuffer(file.content)) {
196
206
  fileStatus.message = 'Writing'
197
207
  this.updateStatus(filesStatuses, JobStatus.IN_PROGRESS, statusCbk)
@@ -200,7 +210,7 @@ export class FsStorage implements StorageConnector<FsSession> {
200
210
  } catch(err) {
201
211
  fileStatus.message = `Error (${err})`
202
212
  this.updateStatus(filesStatuses, JobStatus.IN_PROGRESS, statusCbk)
203
- error = true
213
+ error = err
204
214
  continue
205
215
  }
206
216
  fileStatus.message = 'Success'
@@ -220,7 +230,7 @@ export class FsStorage implements StorageConnector<FsSession> {
220
230
  console.error('writeStream error', err)
221
231
  fileStatus.message = `Error (${err})`
222
232
  this.updateStatus(filesStatuses, JobStatus.IN_PROGRESS, statusCbk)
223
- error = true
233
+ error = err
224
234
  resolve(file)
225
235
  })
226
236
  })
@@ -230,7 +240,7 @@ export class FsStorage implements StorageConnector<FsSession> {
230
240
  }
231
241
  }
232
242
  this.updateStatus(filesStatuses, error ? JobStatus.ERROR : JobStatus.SUCCESS, statusCbk)
233
- return filePaths
243
+ if(error) throw error
234
244
  }
235
245
 
236
246
  async deleteAssets(session: FsSession, id: WebsiteId, paths: string[]): Promise<void> {
@@ -241,7 +251,7 @@ export class FsStorage implements StorageConnector<FsSession> {
241
251
 
242
252
  async readAsset(session: object, websiteId: string, fileName: string): Promise<ConnectorFileContent> {
243
253
  const id = requiredParam<WebsiteId>(websiteId, 'website id')
244
- const path = join(this.options.path, id, fileName)
254
+ const path = join(this.options.path, id, this.options.assetsFolder, fileName)
245
255
  return await fs.readFile(path)
246
256
  }
247
257
  }
@@ -54,6 +54,8 @@ export interface Connector<Session extends ConnectorSession = ConnectorSession>
54
54
  displayName: string
55
55
  icon: string
56
56
  disableLogout?: boolean
57
+ color: string
58
+ background: string
57
59
  // Get the URL to start the authentication process with OAuth (not used for basic auth)
58
60
  getOAuthUrl(session: Session): Promise<string | null>
59
61
  // Get the form to display to the user to authenticate (not used for OAuth)
@@ -87,7 +89,7 @@ export interface StorageConnector<Session extends ConnectorSession = ConnectorSe
87
89
  updateWebsite(session: Session, websiteId: WebsiteId, data: WebsiteData): Promise<void>
88
90
  deleteWebsite(session: Session, websiteId: WebsiteId): Promise<void>
89
91
  // CRUD on assets
90
- writeAssets(session: Session, websiteId: WebsiteId, files: ConnectorFile[], status?: StatusCallback): Promise<string[]> // Returns the files paths on storage
92
+ writeAssets(session: Session, websiteId: WebsiteId, files: ConnectorFile[], status?: StatusCallback): Promise<void>
91
93
  readAsset(session: Session, websiteId: WebsiteId, fileName: string): Promise<ConnectorFileContent>
92
94
  deleteAssets(session: Session, websiteId: WebsiteId, fileNames: string[]): Promise<void>
93
95
  //getFileUrl(session: Session, websiteId: WebsiteId, path: string): Promise<string>
@@ -101,9 +103,9 @@ export interface HostingConnector<Session extends ConnectorSession = ConnectorSe
101
103
  getUrl(session: Session, websiteId: WebsiteId): Promise<string>
102
104
  }
103
105
 
104
- export function toConnectorEnum(type: string): ConnectorType {
105
- const result = ConnectorType[type.toUpperCase() as keyof typeof ConnectorType]
106
- if(!result) throw new Error('Unknown connector ' + type)
106
+ export function toConnectorEnum(type: string | ConnectorType): ConnectorType {
107
+ const result = ConnectorType[type.toString().toUpperCase() as keyof typeof ConnectorType]
108
+ if(!result) throw new Error(`Unknown connector type ${type}. It should be one of ${Object.keys(ConnectorType).join(', ')}`)
107
109
  return result
108
110
  }
109
111
 
@@ -138,6 +140,8 @@ export async function toConnectorData(session: any, connector: Connector): Promi
138
140
  disableLogout: !!connector.disableLogout,
139
141
  isLoggedIn: await connector.isLoggedIn(session),
140
142
  oauthUrl: await connector.getOAuthUrl(session),
143
+ color: connector.color,
144
+ background: connector.background,
141
145
  }
142
146
  }
143
147
 
@@ -161,6 +165,30 @@ export async function contentToString(content: ConnectorFileContent): Promise<st
161
165
  })
162
166
  }
163
167
 
168
+ export async function contentToBuffer(content: ConnectorFileContent): Promise<Buffer> {
169
+ // String
170
+ if (typeof content === 'string') return Buffer.from(content, 'utf8')
171
+ // Buffer
172
+ if (Buffer.isBuffer(content)) return content
173
+ // Stream
174
+ if(content instanceof Readable) {
175
+ return new Promise((resolve, reject) => {
176
+ const chunks: Buffer[] = []
177
+ content.on('data', (chunk: Buffer) => {
178
+ chunks.push(chunk)
179
+ })
180
+ content.on('error', (err: Error) => {
181
+ reject(err)
182
+ })
183
+ content.on('end', () => {
184
+ resolve(Buffer.concat(chunks))
185
+ })
186
+ })
187
+ }
188
+ // Unknown
189
+ throw new Error('Unknown content type')
190
+ }
191
+
164
192
  export function contentToReadable(content: ConnectorFileContent): Readable {
165
193
  // String
166
194
  if (typeof content === 'string') return Readable.from([content])
@@ -14,21 +14,6 @@
14
14
  * You should have received a copy of the GNU Affero General Public License
15
15
  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
16
  */
17
- /**
18
- * Throw an error if a parameter is missing
19
- * @param value the value to check
20
- * @param name the name of the parameter
21
- * @throws Error if the parameter is missing
22
- */
23
- export function requiredParam(value, name, defaultValue) {
24
- if (value === undefined) {
25
- if (defaultValue !== undefined) {
26
- return defaultValue;
27
- }
28
- const error = new Error(`Missing required parameter ${name}`);
29
- console.error(`Missing required parameter ${name}`, error);
30
- throw error;
31
- }
32
- return value;
33
- }
34
- //# sourceMappingURL=validation.js.map
17
+
18
+
19
+ export * as events from './events'
@@ -19,15 +19,37 @@ import * as silexApp from './express'
19
19
  import api from './api/api'
20
20
  import { ServerConfig } from './config'
21
21
  import dotenv from 'dotenv'
22
+ import { join } from 'path'
23
+
24
+ // Expose API to calling app as function silex()
25
+ export * from './expose'
22
26
 
23
27
  // Main app
24
28
  export default async function silex() {
25
29
  // Load env vars from .env file if any
26
- dotenv.config()
30
+ const curDirEnv = dotenv.config()
31
+ if (curDirEnv.error) {
32
+ if(curDirEnv.error['code'] === 'ENOENT') {
33
+ console.info('> No .env file found in current directory')
34
+ } else {
35
+ throw curDirEnv.error
36
+ }
37
+ } else {
38
+ console.info('> Env vars loaded from .env file')
39
+ }
27
40
 
28
41
  // Load default env vars
29
42
  // This will not override existing env vars
30
- dotenv.config({ path: '.env.default' })
43
+ const DEFAULT_ENV_FILE = join(__dirname, '../../../.env.default')
44
+ const rootDirEnv = dotenv.config({ path: DEFAULT_ENV_FILE })
45
+ if (rootDirEnv.error) {
46
+ if(rootDirEnv.error['code'] === 'ENOENT') {
47
+ throw new Error(`\n\nFailed to load default env vars. File not found ${DEFAULT_ENV_FILE}\n\n`)
48
+ } else {
49
+ throw new Error(`\n\nFailed to load default env vars. Error in ${DEFAULT_ENV_FILE}: ${ rootDirEnv.error.message }\n\n`)
50
+ }
51
+ }
52
+ console.info('> Default env vars loaded')
31
53
 
32
54
  // Get the default config object
33
55
  const config = new ServerConfig()