@silexlabs/silex 3.0.3 → 3.0.4

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.
@@ -21,11 +21,13 @@
21
21
  *
22
22
  */
23
23
 
24
+ import { Component, ComponentView } from 'grapesjs'
24
25
  import { DEV_MESSAGE } from '../constants'
25
26
  import { ClientConfig } from './config'
26
27
  import { ClientEvent } from './events'
27
28
  import { initEditor, getEditor } from './grapesjs/index'
28
29
  import { cmdPauseAutoSave } from './grapesjs/storage'
30
+ import { displayedToStored, storedToDisplayed } from './assetUrl'
29
31
 
30
32
  // Expose API to calling app as window.silex
31
33
  export * from './expose'
@@ -83,6 +85,42 @@ export async function start(options = {}): Promise<void> {
83
85
  // Add default plugins
84
86
  await config.addDefaultPlugins()
85
87
 
88
+ const domc = editor.DomComponents
89
+ const imgType = domc.getType('image')
90
+ domc.addType('image', {
91
+ ...imgType,
92
+ view: {
93
+ ...imgType.view,
94
+ onRender() {
95
+ const view = this as ComponentView
96
+ imgType.view.onRender && imgType.view.onRender.call(this) // call original
97
+ const el = view.el
98
+ const elImg = view.el as HTMLImageElement
99
+ const src = this.model.getAttributes().src
100
+ if (src && !src.startsWith('http')) {
101
+ elImg.src = storedToDisplayed(src, config.websiteId, config.storageId)
102
+ }
103
+ },
104
+ },
105
+ model: {
106
+ ...imgType.model,
107
+ init() {
108
+ const model = this as Component
109
+ imgType.model.init && imgType.model.init.call(this) // call original
110
+ // Handle changes by the AssetManager
111
+ model.on('change', () => {
112
+ if(model.get('src')) {
113
+ const newSrc = displayedToStored(model.get('src'))
114
+ if(newSrc !== model.get('src')) {
115
+ model.set('src', newSrc)
116
+ model.view.render()
117
+ }
118
+ }
119
+ })
120
+ },
121
+ },
122
+ })
123
+
86
124
  // Load the site
87
125
  try {
88
126
  editor.runCommand(cmdPauseAutoSave)
@@ -198,7 +198,18 @@ function doTransformPermalink(editor: Editor, cssValue: string): string {
198
198
  export function transformBgImage(editor: Editor, style: StyleProps): StyleProps {
199
199
  const cssValue = style['background-image']
200
200
  if (cssValue) {
201
- const newCssValue = typeof cssValue === 'string' ? doTransformPermalink(editor, cssValue) : cssValue.map(value => doTransformPermalink(editor, value))
201
+ let newCssValue
202
+ if (Array.isArray(cssValue)) {
203
+ newCssValue = cssValue
204
+ .map(value => doTransformPermalink(editor, value))
205
+ .join(', ')
206
+ } else if (typeof cssValue === 'string') {
207
+ newCssValue = doTransformPermalink(editor, cssValue)
208
+ } else if (newCssValue === cssValue) {
209
+ // No change
210
+ return style
211
+ }
212
+ // Set the new value
202
213
  return {
203
214
  ...style,
204
215
  'background-image': newCssValue,
@@ -22,6 +22,7 @@ import { ApiError, ConnectorType, ConnectorUser, WebsiteData, WebsiteId, Website
22
22
  import fetch from 'node-fetch'
23
23
  import crypto, { createHash } from 'crypto'
24
24
  import { join } from 'path'
25
+ import { Agent } from 'https'
25
26
 
26
27
  /**
27
28
  * Gitlab connector
@@ -247,6 +248,7 @@ export default class GitlabConnector implements StorageConnector {
247
248
  // Call the API
248
249
  const url = `${this.options.domain}/api/v4/projects/${websiteId}/repository/files/${safePath}?ref=${this.options.branch}&access_token=${this.getSessionToken(session).token?.access_token}`
249
250
  const response = await fetch(url, {
251
+ agent: this.getAgent(),
250
252
  method: 'GET',
251
253
  headers: {
252
254
  'Content-Type': 'application/json',
@@ -305,10 +307,12 @@ export default class GitlabConnector implements StorageConnector {
305
307
  let response
306
308
  try {
307
309
  response = await fetch(url, body && method !== 'GET' ? {
310
+ agent: this.getAgent(),
308
311
  method,
309
312
  headers,
310
313
  body: body ? JSON.stringify(body) : undefined
311
314
  } : {
315
+ agent: this.getAgent(),
312
316
  method,
313
317
  headers,
314
318
  })
@@ -332,6 +336,7 @@ export default class GitlabConnector implements StorageConnector {
332
336
  client_secret: this.options.clientSecret,
333
337
  }
334
338
  const response = await fetch(this.options.domain + '/oauth/token', {
339
+ agent: this.getAgent(),
335
340
  method: 'POST',
336
341
  headers: {
337
342
  'Content-Type': 'application/json',
@@ -399,6 +404,16 @@ export default class GitlabConnector implements StorageConnector {
399
404
  return `${this.config.url}${API_PATH}${API_CONNECTOR_PATH}${API_CONNECTOR_LOGIN_CALLBACK}?${params}`
400
405
  }
401
406
 
407
+ // Force IPv4 when running locally
408
+ private getAgent(): Agent | undefined {
409
+ if (this.config.url.startsWith('http://localhost')) {
410
+ return new Agent({
411
+ family: 4,
412
+ })
413
+ }
414
+ return undefined
415
+ }
416
+
402
417
  /**
403
418
  * Get the OAuth URL to redirect the user to
404
419
  * The URL should look like
@@ -471,6 +486,7 @@ export default class GitlabConnector implements StorageConnector {
471
486
  }
472
487
 
473
488
  const response = await fetch(this.options.domain + '/oauth/token', {
489
+ agent: this.getAgent(),
474
490
  method: 'POST',
475
491
  headers: {
476
492
  'Content-Type': 'application/json',
@@ -489,8 +505,8 @@ export default class GitlabConnector implements StorageConnector {
489
505
 
490
506
  // Store the token in the session
491
507
  this.setSessionToken(session, {
492
- token,
493
508
  ...this.getSessionToken(session),
509
+ token,
494
510
  })
495
511
 
496
512
  // We need to get the user ID for listWebsites
@@ -49,17 +49,24 @@ export default function(config: ServerConfig) {
49
49
  * Method to validate HTTP status codes
50
50
  * Returns a suitable error code if it is not valid
51
51
  */
52
- function validateStatus(status: number, _default = 500): number {
52
+ function validateStatus(status: number | string, _default = 500): number {
53
53
  if(!status) {
54
54
  console.warn(`Status code is undefined, returning default ${_default}`)
55
55
  return _default
56
56
  }
57
- // Make sure it is a string
58
- status = parseInt(status.toString())
57
+ switch (status) {
58
+ case 'ETIMEDOUT':
59
+ case 'ECONNREFUSED':
60
+ case 'ECONNRESET':
61
+ console.warn(`Connection error ${status}, returning default ${_default}`)
62
+ return _default
63
+ }
64
+ // Make sure it is a number
65
+ const statusNum = parseInt(status.toString())
59
66
  // Check the status code
60
- if (status >= 100 && status < 600) return status
67
+ if (statusNum >= 100 && statusNum < 600) return statusNum
61
68
  // Invalid status code
62
- console.warn(`Invalid status code ${status}, returning default ${_default}}`)
69
+ console.warn(`Invalid status code ${statusNum} (${status.toString()}, ${status}), returning default ${_default}`)
63
70
  return _default
64
71
  }
65
72
 
@@ -241,7 +248,7 @@ async function routeLogin(req: Request, res: Response) {
241
248
  async function routeLoginSuccess(req: Request, res: Response) {
242
249
  try {
243
250
  const query = req.query as ApiConnectorLoginCbkQuery
244
- if(query.error) throw new Error(query.error)
251
+ if (query.error) throw new Error(query.error)
245
252
 
246
253
  const body = req.body as ApiConnectorLoginCbkBody
247
254
  const session = requiredParam(req['session'], 'Session object')
@@ -265,9 +272,10 @@ async function routeLoginSuccess(req: Request, res: Response) {
265
272
  // End the auth flow
266
273
  res.send(getEndAuthHtml('Logged in', false, connectorId, type, connector.getOptions(body)))
267
274
  } catch (error) {
275
+ console.error('Error in the login callback', error, error?.code, error?.httpStatusCode)
268
276
  res
269
277
  .status(validateStatus(error?.code ?? error?.httpStatusCode, 500))
270
- .send(getEndAuthHtml(error.message, true, req.query.connectorId as ConnectorId, req.query.type as ConnectorType))
278
+ .send(getEndAuthHtml(`${error?.message} ${error?.code ?? error?.httpStatusCode}`, true, req.query.connectorId as ConnectorId, req.query.type as ConnectorType))
271
279
  }
272
280
  }
273
281