@silexlabs/silex 3.0.0-alpha.152 → 3.0.0-alpha.154

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.
@@ -31,16 +31,40 @@ export function onAll(editor: Editor, cbk: (c: Component) => void) {
31
31
  * SHA256 hash a string
32
32
  */
33
33
  export async function hashString(str: string): Promise<string> {
34
- // Convert the string to an ArrayBuffer
35
- const encoder = new TextEncoder()
36
- const data = encoder.encode(str)
34
+ if (crypto.subtle != undefined) {
35
+ // Convert the string to an ArrayBuffer
36
+ const encoder = new TextEncoder()
37
+ const data = encoder.encode(str)
37
38
 
38
- // Hash the data with SHA-256
39
- const hashBuffer = await crypto.subtle.digest('SHA-256', data)
39
+ // Hash the data with SHA-256
40
+ const hashBuffer = await crypto.subtle.digest('SHA-256', data)
40
41
 
41
- // Convert the ArrayBuffer to hex string
42
- const hashArray = Array.from(new Uint8Array(hashBuffer))
43
- const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('')
42
+ // Convert the ArrayBuffer to hex string
43
+ const hashArray = Array.from(new Uint8Array(hashBuffer))
44
+ const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('')
44
45
 
45
- return hashHex
46
+ return hashHex
47
+ }
48
+ else {return 'local'}
49
+ }
50
+
51
+ /**
52
+ * Select the <body> element in the editor.
53
+ * @param editor The GrapesJS editor.
54
+ */
55
+
56
+ export function selectBody(editor: Editor): void {
57
+ editor.select(editor.DomComponents.getWrapper())
58
+ }
59
+
60
+ export function isTextOrInputField(element: HTMLElement): boolean {
61
+ const isInput: boolean = element.tagName === 'INPUT' && element.getAttribute('type') !== 'submit'
62
+ const isOtherFormElement: boolean = ['TEXTAREA', 'OPTION', 'OPTGROUP', 'SELECT'].includes(element.tagName)
63
+
64
+ return isInput || isOtherFormElement
65
+ }
66
+
67
+ export function titleCase(str: string, sep: string = ' '): string {
68
+ const split = str.split(sep)
69
+ return split.map(w => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase()).join(sep)
46
70
  }
@@ -18,7 +18,7 @@
18
18
  import { API_CONNECTOR_LOGIN_CALLBACK, API_CONNECTOR_PATH, API_PATH, WEBSITE_DATA_FILE } from '../../constants'
19
19
  import { ServerConfig } from '../../server/config'
20
20
  import { ConnectorFile, ConnectorFileContent, StatusCallback, StorageConnector, contentToBuffer, contentToString, toConnectorData } from '../../server/connectors/connectors'
21
- import { ApiError, ConnectorType, ConnectorUser, WebsiteData, WebsiteId, WebsiteMeta, WebsiteMetaFileContent } from '../../types'
21
+ import { ApiError, ConnectorType, ConnectorUser, WebsiteData, WebsiteId, WebsiteMeta, WebsiteMetaFileContent, JobStatus } from '../../types'
22
22
  import fetch from 'node-fetch'
23
23
  import crypto, { createHash } from 'crypto'
24
24
  import { join } from 'path'
@@ -41,10 +41,10 @@ export interface GitlabOptions {
41
41
  domain: string
42
42
  }
43
43
 
44
- interface GitlabToken {
45
- state: string
46
- codeVerifier: string
47
- codeChallenge: string
44
+ export interface GitlabToken {
45
+ state?: string
46
+ codeVerifier?: string
47
+ codeChallenge?: string
48
48
  token?: {
49
49
  access_token: string
50
50
  token_type: string
@@ -58,9 +58,7 @@ interface GitlabToken {
58
58
  username?: string
59
59
  }
60
60
 
61
- interface GitlabSession {
62
- gitlab?: GitlabToken
63
- }
61
+ export type GitlabSession = Record<string, GitlabToken>
64
62
 
65
63
  interface GitlabAction {
66
64
  action: 'create' | 'delete' | 'move' | 'update'
@@ -106,10 +104,6 @@ interface MetaRepoFileContent {
106
104
  }
107
105
  }
108
106
 
109
- async function wait(ms: number) {
110
- return new Promise(resolve => setTimeout(resolve, ms))
111
- }
112
-
113
107
  const svg = `<?xml version="1.0" encoding="UTF-8" standalone="no"?>
114
108
  <svg
115
109
  xmlns:svg="http://www.w3.org/2000/svg"
@@ -200,26 +194,28 @@ export default class GitlabConnector implements StorageConnector {
200
194
  return encodeURIComponent(join(this.options.assetsFolder, path))
201
195
  }
202
196
 
203
- private async createFile(session: GitlabSession, websiteId: WebsiteId, path: string, content: string, isBase64 = false): Promise<void> {
197
+ async createFile(session: GitlabSession, websiteId: WebsiteId, path: string, content: string, isBase64 = false): Promise<void> {
204
198
  // Remove leading slash
205
199
  const safePath = path.replace(/^\//, '')
200
+ const encodePath = decodeURIComponent(path)
206
201
  return this.callApi(session, `api/v4/projects/${websiteId}/repository/files/${safePath}`, 'POST', {
207
202
  id: websiteId,
208
203
  branch: this.options.branch,
209
204
  content,
210
- commit_message: `Create file ${path} from Silex`,
205
+ commit_message: `Create file ${encodePath} from Silex`,
211
206
  encoding: isBase64 ? 'base64' : undefined,
212
207
  })
213
208
  }
214
209
 
215
- private async updateFile(session: GitlabSession, websiteId: WebsiteId, path: string, content: string, isBase64 = false): Promise<void> {
210
+ async updateFile(session: GitlabSession, websiteId: WebsiteId, path: string, content: string, isBase64 = false): Promise<void> {
216
211
  // Remove leading slash
217
212
  const safePath = path.replace(/^\//, '')
213
+ const encodePath = decodeURIComponent(path)
218
214
  return this.callApi(session, `api/v4/projects/${websiteId}/repository/files/${safePath}`, 'PUT', {
219
215
  id: websiteId,
220
216
  branch: this.options.branch,
221
217
  content: await contentToString(content),
222
- commit_message: `Update website asset ${path} from Silex`,
218
+ commit_message: `Update website asset ${encodePath} from Silex`,
223
219
  encoding: isBase64 ? 'base64' : undefined,
224
220
  })
225
221
  }
@@ -227,7 +223,7 @@ export default class GitlabConnector implements StorageConnector {
227
223
  async readFile(session: GitlabSession, websiteId: string, fileName: string): Promise<Buffer> {
228
224
  const safePath = fileName.replace(/^\//, '')
229
225
  // Call the API
230
- const url = `${this.options.domain}/api/v4/projects/${websiteId}/repository/files/${safePath}?ref=${this.options.branch}&access_token=${session.gitlab?.token?.access_token}`
226
+ const url = `${this.options.domain}/api/v4/projects/${websiteId}/repository/files/${safePath}?ref=${this.options.branch}&access_token=${this.getSessionToken(session).token?.access_token}`
231
227
  const response = await fetch(url, {
232
228
  method: 'GET',
233
229
  headers: {
@@ -247,8 +243,8 @@ export default class GitlabConnector implements StorageConnector {
247
243
  * The meta repo contains a JSON file which contains the list of websites
248
244
  */
249
245
  //private getMetaRepoPath(session: GitlabSession): string {
250
- // if(!session.gitlab?.username) throw new ApiError('Missing Gitlab user ID. User not logged in?', 401)
251
- // return encodeURIComponent(`${session.gitlab.username}/${this.options.metaRepo}`)
246
+ // if(!this.getSessionToken(session).username) throw new ApiError('Missing Gitlab user ID. User not logged in?', 401)
247
+ // return encodeURIComponent(`${this.getSessionToken(session).username}/${this.options.metaRepo}`)
252
248
  //}
253
249
 
254
250
  ///**
@@ -272,8 +268,8 @@ export default class GitlabConnector implements StorageConnector {
272
268
  /**
273
269
  * Call the Gitlab API with the user's token and handle errors
274
270
  */
275
- private async callApi(session: GitlabSession, path: string, method: 'POST' | 'GET' | 'PUT' | 'DELETE' = 'GET', body: GitlabWriteFile | GitlabGetToken | GitlabWebsiteName | GitlabCreateBranch | null = null, params: any = {}): Promise<any> {
276
- const token = session?.gitlab?.token
271
+ async callApi(session: GitlabSession, path: string, method: 'POST' | 'GET' | 'PUT' | 'DELETE' = 'GET', body: GitlabWriteFile | GitlabGetToken | GitlabWebsiteName | GitlabCreateBranch | null = null, params: any = {}): Promise<any> {
272
+ const token = this.getSessionToken(session).token
277
273
  const tokenParam = token ? `access_token=${token.access_token}&` : ''
278
274
  const paramsStr = Object.entries(params).map(([k, v]) => `${k}=${encodeURIComponent((v as any).toString())}`).join('&')
279
275
  const url = `${this.options.domain}/${path}?${tokenParam}${paramsStr}`
@@ -298,12 +294,12 @@ export default class GitlabConnector implements StorageConnector {
298
294
  if(!response.ok) {
299
295
  if (text.includes('A file with this name doesn\'t exist')) {
300
296
  throw new ApiError('Gitlab API error (5): Not Found', 404)
301
- } else if (response.status === 401 && session?.gitlab?.token?.refresh_token) {
297
+ } else if (response.status === 401 && this.getSessionToken(session).token?.refresh_token) {
302
298
  // Refresh the token
303
- const token = session?.gitlab?.token
299
+ const token = this.getSessionToken(session).token
304
300
  const body = {
305
301
  grant_type: 'refresh_token',
306
- refresh_token: token.refresh_token,
302
+ refresh_token: token?.refresh_token,
307
303
  client_id: this.options.clientId,
308
304
  client_secret: this.options.clientSecret,
309
305
  }
@@ -316,14 +312,16 @@ export default class GitlabConnector implements StorageConnector {
316
312
  })
317
313
  const refreshJson = await response.json()
318
314
  if (response.ok) {
319
- session.gitlab.token = {
320
- ...token,
321
- ...refreshJson,
322
- }
315
+ this.setSessionToken(session, {
316
+ token: {
317
+ ...token,
318
+ ...refreshJson,
319
+ },
320
+ } as GitlabToken)
323
321
  return await this.callApi(session, path, method, body as any, params)
324
322
  } else {
325
323
  const message = typeof refreshJson?.message === 'object' ? Object.entries(refreshJson.message).map(entry => entry.join(' ')).join(' ') : refreshJson?.message ?? refreshJson?.error ?? response.statusText
326
- console.error('Gitlab API error (2) - could not refresh token', response.status, response.statusText, {message}, 'refresh_token:', token.refresh_token)
324
+ console.error('Gitlab API error (2) - could not refresh token', response.status, response.statusText, {message}, 'refresh_token:', token?.refresh_token)
327
325
  // Workaround for when the token is invalid
328
326
  // It happens often which is not normal (refresh token should last 6 months)
329
327
  this.logout(session)
@@ -332,6 +330,7 @@ export default class GitlabConnector implements StorageConnector {
332
330
  }
333
331
  } else {
334
332
  const message = typeof json?.message === 'object' ? Object.entries(json.message).map(entry => entry.join(' ')).join(' ') : json?.message ?? json?.error ?? response.statusText
333
+ console.error('Gitlab API error (1)', response.status, response.statusText, {url, method, body, params, text, message})
335
334
  throw new ApiError(`Gitlab API error (1): ${message}`, response.status)
336
335
  }
337
336
  }
@@ -388,13 +387,23 @@ export default class GitlabConnector implements StorageConnector {
388
387
  const codeChallenge = await this.generateCodeChallenge(codeVerifier)
389
388
 
390
389
  // Store the code verifier and code challenge in the session
391
- session.gitlab = {
392
- ...session.gitlab,
390
+ this.setSessionToken(session, {
391
+ ...this.getSessionToken(session),
393
392
  state,
394
393
  codeVerifier,
395
394
  codeChallenge,
396
- }
397
- return `${this.options.domain}/oauth/authorize?client_id=${this.options.clientId}&redirect_uri=${redirect_uri}&response_type=code&state=${session.gitlab.state}&scope=${this.options.scope}&code_challenge=${codeChallenge}&code_challenge_method=S256`
395
+ })
396
+ return `${this.options.domain}/oauth/authorize?client_id=${this.options.clientId}&redirect_uri=${redirect_uri}&response_type=code&state=${this.getSessionToken(session).state}&scope=${this.options.scope}&code_challenge=${codeChallenge}&code_challenge_method=S256`
397
+ }
398
+
399
+ getSessionToken(session: GitlabSession | undefined): GitlabToken {
400
+ return (session ?? {})[this.connectorId] ?? {}
401
+ }
402
+ setSessionToken(session: GitlabSession, token: GitlabToken): void {
403
+ session[this.connectorId] = token
404
+ }
405
+ resetSessionToken(session: GitlabSession): void {
406
+ delete session[this.connectorId]
398
407
  }
399
408
 
400
409
  getOptions(formData: object): object {
@@ -410,7 +419,7 @@ export default class GitlabConnector implements StorageConnector {
410
419
  }
411
420
 
412
421
  async isLoggedIn(session: GitlabSession): Promise<boolean> {
413
- return !!session?.gitlab?.token
422
+ return !!this.getSessionToken(session).token
414
423
  }
415
424
 
416
425
  /**
@@ -419,9 +428,10 @@ export default class GitlabConnector implements StorageConnector {
419
428
  * OAuth2 Step #2 from https://docs.gitlab.com/ee/api/oauth2.html#authorization-code-with-proof-key-for-code-exchange-pkce
420
429
  */
421
430
  async setToken(session: GitlabSession, loginResult: any): Promise<void> {
422
- if(!loginResult.state || loginResult.state !== session.gitlab?.state) throw new ApiError('Invalid state', 401)
423
- if(!session.gitlab?.codeVerifier) throw new ApiError('Missing code verifier', 401)
424
- if(!session.gitlab?.codeChallenge) throw new ApiError('Missing code challenge', 401)
431
+ const sessionToken = this.getSessionToken(session)
432
+ if(!loginResult.state || loginResult.state !== sessionToken?.state) throw new ApiError('Invalid state', 401)
433
+ if(!sessionToken?.codeVerifier) throw new ApiError('Missing code verifier', 401)
434
+ if(!sessionToken?.codeChallenge) throw new ApiError('Missing code challenge', 401)
425
435
 
426
436
  const response = await fetch(this.options.domain + '/oauth/token', {
427
437
  method: 'POST',
@@ -434,23 +444,31 @@ export default class GitlabConnector implements StorageConnector {
434
444
  code: loginResult.code,
435
445
  grant_type: 'authorization_code',
436
446
  redirect_uri: this.getRedirect(),
437
- code_verifier: session.gitlab.codeVerifier,
447
+ code_verifier: sessionToken.codeVerifier,
438
448
  }),
439
449
  })
440
450
 
441
451
  const token = await response.json()
442
452
 
443
453
  // Store the token in the session
444
- session.gitlab = { token, state: session.gitlab.state, codeVerifier: session.gitlab.codeVerifier, codeChallenge: session.gitlab.codeChallenge }
454
+ this.setSessionToken(session, {
455
+ token,
456
+ ...this.getSessionToken(session),
457
+ })
445
458
 
446
459
  // We need to get the user ID for listWebsites
447
460
  const user = await this.callApi(session, 'api/v4/user') as any
448
- session.gitlab.userId = user.id
449
- session.gitlab.username = user.username
461
+
462
+ // Store the user details in the session
463
+ this.setSessionToken(session, {
464
+ ...this.getSessionToken(session),
465
+ userId: user.id,
466
+ username: user.username,
467
+ })
450
468
  }
451
469
 
452
470
  async logout(session: GitlabSession): Promise<void> {
453
- delete session.gitlab
471
+ this.resetSessionToken(session)
454
472
  }
455
473
 
456
474
  async getUser(session: GitlabSession): Promise<ConnectorUser> {
@@ -486,7 +504,7 @@ export default class GitlabConnector implements StorageConnector {
486
504
  // throw e
487
505
  // }
488
506
  //}
489
- const projects = await this.callApi(session, `api/v4/users/${session.gitlab?.userId}/projects`) as any[]
507
+ const projects = await this.callApi(session, `api/v4/users/${this.getSessionToken(session).userId}/projects`) as any[]
490
508
  return projects
491
509
  .filter(p => p.name.startsWith(this.options.repoPrefix))
492
510
  .map(p => ({
@@ -647,6 +665,7 @@ export default class GitlabConnector implements StorageConnector {
647
665
  }
648
666
 
649
667
  async writeAssets(session: GitlabSession, websiteId: string, files: ConnectorFile[], status?: StatusCallback | undefined): Promise<void> {
668
+ status && await status({ message: 'in progress...', status: JobStatus.IN_PROGRESS })
650
669
  // For each file
651
670
  for (const file of files) {
652
671
  // Convert to base64
@@ -660,10 +679,12 @@ export default class GitlabConnector implements StorageConnector {
660
679
  if (e.statusCode === 404 || e.httpStatusCode === 404 || e.message.endsWith('A file with this name doesn\'t exist')) {
661
680
  await this.createFile(session, websiteId, path, content, true)
662
681
  } else {
682
+ status && await status({ message: 'Error', status: JobStatus.ERROR })
663
683
  throw e
664
684
  }
665
685
  }
666
686
  }
687
+ status && await status({ message: 'Successfull', status: JobStatus.SUCCESS })
667
688
  }
668
689
 
669
690
  async readAsset(session: GitlabSession, websiteId: string, fileName: string): Promise<ConnectorFileContent> {
@@ -0,0 +1,97 @@
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
+ * Gitlab hosting connector
20
+ * @fileoverview Gitlab hosting connector for Silex, connect to the user's Gitlab account to host websites with .gitlab-ci.yml file template for plain html by default
21
+ * @see https://docs.gitlab.com/ee/api/oauth2.html
22
+ * @see https://docs.gitlab.com/ee/user/project/pages/getting_started/pages_ci_cd_template.html
23
+ */
24
+
25
+ import GitlabConnector, { GitlabOptions, GitlabSession} from './GitlabConnector'
26
+ import { HostingConnector, ConnectorFile } from '../../server/connectors/connectors'
27
+ import { ConnectorType, WebsiteId, JobData, JobStatus, PublicationJobData } from '../../types'
28
+ import { JobManager } from '../../server/jobs'
29
+ import { join } from 'path'
30
+ import { ServerConfig } from '../../server/config'
31
+
32
+ export default class GitlabHostingConnector extends GitlabConnector implements HostingConnector {
33
+
34
+ displayName = 'Gitlab hosting'
35
+ connectorType = ConnectorType.HOSTING
36
+
37
+ constructor( config: ServerConfig, opts: Partial<GitlabOptions>) {
38
+ super (config, opts)
39
+ /* public directory for standard gitlab pages */
40
+ this.options.assetsFolder= 'public'
41
+ }
42
+
43
+ async publish(session: GitlabSession, websiteId: WebsiteId, files: ConnectorFile[], {startJob, jobSuccess, jobError}: JobManager): Promise<JobData> {
44
+ const job = startJob(`Publishing to ${this.displayName}`) as PublicationJobData
45
+ job.logs = [[`Publishing to ${this.displayName}`]]
46
+ job.errors = [[]]
47
+ /* Configuration file .gitlab-ci.yml contains template for plain html Gitlab pages*/
48
+ const pathYml = '.gitlab-ci.yml'
49
+ const contentYml = `image: busybox
50
+ pages:
51
+ stage: deploy
52
+ environment: production
53
+ script:${files.find(file => file.path.includes('.11tydata.')) ? `
54
+ - npx @11ty/eleventy@canary --input=public --output=_site
55
+ - mkdir -p public/css public/assets && cp -R public/css public/assets _site/
56
+ - rm -rf public && mv _site public` : `
57
+ - echo "The site will be deployed to $CI_PAGES_URL"`}
58
+ artifacts:
59
+ paths:
60
+ - public
61
+ rules:
62
+ - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
63
+ `
64
+ try {
65
+ await this.readFile(session, websiteId, pathYml)
66
+ } catch (e) {
67
+ // If the file .gitlab-ci.yml does not exist, create it, otherwise do nothing (do not overwriting existing one)
68
+ if (e.statusCode === 404 || e.httpStatusCode === 404 || e.message.endsWith('A file with this name doesn\'t exist')) {
69
+ await this.createFile(session, websiteId, pathYml, contentYml)
70
+ }
71
+ }
72
+
73
+ /* publishing all files for website*/
74
+ await this.writeAssets(session, websiteId, files, async ({status, message}) => {
75
+ // Update the job status
76
+ job.status = status
77
+ job.message = message
78
+ job.logs[0].push(message)
79
+ if(status === JobStatus.SUCCESS) {
80
+ const gitlabUrl = await this.getUrl(session, websiteId)
81
+ job.message = gitlabUrl
82
+ job.logs[0].push(gitlabUrl)
83
+ jobSuccess(job.jobId, 'Gitlab pages PUBLISHED: ' + '<a href="' + gitlabUrl + '" target="_blank">' + gitlabUrl + '</a>')
84
+ } else if(status === JobStatus.ERROR) {
85
+ job.errors[0].push(message)
86
+ jobError(job.jobId, message)
87
+ }
88
+ })
89
+ return job
90
+ }
91
+
92
+ /* Get and return Url Gitlab Pages */
93
+ async getUrl(session: GitlabSession, websiteId: WebsiteId): Promise<string> {
94
+ const response = await this.callApi(session, `api/v4/projects/${websiteId}/pages`, 'GET')
95
+ return response.url
96
+ }
97
+ }