@symbo.ls/socket 2.11.132 → 2.11.164

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 (3) hide show
  1. package/client.js +26 -19
  2. package/package.json +3 -2
  3. package/server.js +24 -11
package/client.js CHANGED
@@ -9,28 +9,28 @@ const { window } = globals.default || globals
9
9
 
10
10
  const ENV = process.env.NODE_ENV
11
11
 
12
- const SOCKET_BACKEND_URL = window && window.location &&
13
- window.location.host.includes('local')
14
- ? 'localhost:13335'
15
- : 'socket.symbols.app' ||
16
- 'https://socket.symbols.app'
17
-
18
- let socket
19
- let tryConnect = 0
20
12
  const defautlOpts = {}
21
- const tryConnectMax = 1
22
13
 
23
14
  export const connect = (key, options = {}) => {
15
+ const isDev = options.development ||
16
+ (window && window.location && window.location.host.includes('local')) ||
17
+ (ENV === 'test' || ENV === 'development')
18
+
19
+ const SOCKET_BACKEND_URL = isDev
20
+ ? 'http://localhost:13336/'
21
+ : 'https://socket.symbols.app/'
22
+
24
23
  const socketUrls = isArray(options.socketUrl)
25
24
  ? options.socketUrl
26
25
  : [options.socketUrl || SOCKET_BACKEND_URL]
26
+
27
27
  const primaryUrl = socketUrls[0]
28
28
  const secondaryUrl = socketUrls[1] || 'socket.symbols.app'
29
29
 
30
- socket = io(primaryUrl || SOCKET_BACKEND_URL)
30
+ const socket = io(primaryUrl || SOCKET_BACKEND_URL)
31
31
 
32
32
  socket.on('connect', () => {
33
- if (ENV === 'test' || ENV === 'development') {
33
+ if (isDev) {
34
34
  console.log(
35
35
  `Connected to %c${primaryUrl || SOCKET_BACKEND_URL} %c${key} %c${socket.id}`,
36
36
  'font-weight: bold; color: green;',
@@ -39,7 +39,7 @@ export const connect = (key, options = {}) => {
39
39
  )
40
40
  }
41
41
 
42
- socket.emit('initConnect', options)
42
+ socket.emit('initConnect', { key, ...options })
43
43
 
44
44
  try {
45
45
  if (isFunction(options.onConnect)) {
@@ -50,15 +50,18 @@ export const connect = (key, options = {}) => {
50
50
  }
51
51
  })
52
52
 
53
+ let CONNECT_ATTEPT = 0
54
+ const CONNECT_ATTEPT_MAX_ALLOWED = 1
55
+
53
56
  socket.on('connect_error', (err) => {
54
57
  console.log(`event: connect_error | reason: ${err.message}`)
55
58
  try {
56
59
  if (isFunction(options.onError)) options.onError(err, socket)
57
60
 
58
- if (tryConnect < tryConnectMax) {
61
+ if (CONNECT_ATTEPT < CONNECT_ATTEPT_MAX_ALLOWED) {
59
62
  socket.disconnect()
60
63
 
61
- tryConnect++
64
+ CONNECT_ATTEPT++
62
65
 
63
66
  if (ENV === 'test' || ENV === 'development') {
64
67
  console.log(
@@ -89,17 +92,21 @@ export const connect = (key, options = {}) => {
89
92
  if (event === 'connect') return
90
93
 
91
94
  try {
92
- if (isFunction(options.onChange)) options.onChange(event, args[0], socket)
95
+ if (isFunction(options.onChange)) {
96
+ options.onChange(event, args[0], socket)
97
+ }
93
98
  } catch (e) {
94
99
  console.error(e)
95
100
  }
96
101
  })
102
+
103
+ return socket
97
104
  }
98
105
 
99
- export const send = (event = 'change', changes, options) => {
100
- socket.emit(event, changes, { ...options, ...defautlOpts })
106
+ export function send (event = 'change', changes, options) {
107
+ this.emit(event, changes, { ...options, ...defautlOpts })
101
108
  }
102
109
 
103
- export const disconnect = () => {
104
- socket.disconnect()
110
+ export function disconnect () {
111
+ this.disconnect()
105
112
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@symbo.ls/socket",
3
- "version": "2.11.132",
3
+ "version": "2.11.164",
4
4
  "description": "Connect your app to Symbols editor",
5
5
  "main": "server.js",
6
6
  "author": "symbo.ls",
@@ -11,11 +11,12 @@
11
11
  },
12
12
  "dependencies": {
13
13
  "@domql/utils": "latest",
14
+ "chalk": "^5.0.0",
14
15
  "express": "^4.18.1",
15
16
  "http": "^0.0.1-security",
16
17
  "merge-deep": "^3.0.3",
17
18
  "socket.io": "^4.5.2",
18
19
  "socket.io-client": "^4.5.2"
19
20
  },
20
- "gitHead": "13027443598f113dd2b79ddf7aaf1adbd46d97d7"
21
+ "gitHead": "943a48800e5959b8c1f15d5d8d49224565038c23"
21
22
  }
package/server.js CHANGED
@@ -1,12 +1,13 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  import fs from 'fs'
4
+ import chalk from 'chalk'
4
5
  import express from 'express'
5
6
  import http from 'http'
6
7
  import { Server } from 'socket.io'
7
8
  import { createRequire } from 'module'
8
9
  import * as utils from '@domql/utils'
9
- const { overwriteDeep } = utils
10
+ const { overwriteDeep } = utils.default
10
11
 
11
12
  const require = createRequire(import.meta.url) // construct the require method
12
13
  const DES_SYS_DEFAULT_FILE = require('@symbo.ls/init/dynamic.json') // Bring in the ability to create the 'require' method
@@ -14,20 +15,32 @@ const DES_SYS_DEFAULT_FILE = require('@symbo.ls/init/dynamic.json') // Bring in
14
15
  const app = express()
15
16
  let io
16
17
 
18
+ const debugMsg = chalk.dim('Use --verbose to debug the error or open the issue at https://github.com/symbo-ls/smbls')
19
+
17
20
  export const updateDynamycFile = (changes, options = {}) => {
18
- const { key, live } = options
21
+ const { verbose, prettify, verboseCode } = options
19
22
  const file = require('@symbo.ls/init/dynamic.json')
20
23
 
21
- const newMerge = overwriteDeep(changes, file)
24
+ const newMerge = overwriteDeep(file, changes)
22
25
  const mergeStr = JSON.stringify(newMerge, null, 2)
23
26
  const initPath = process.cwd() + '/node_modules/@symbo.ls/init/dynamic.json'
24
27
 
25
- if (live) {
26
- io.to(key).emit('liveChange', mergeStr)
27
- } else {
28
- fs.writeFile(initPath, mergeStr, function (err) {
29
- if (err) { return console.log(err) }
30
- })
28
+ console.log(chalk.dim('\n----------------\n'))
29
+
30
+ console.log(chalk.dim('Received update:'))
31
+ console.log(Object.keys(changes).join(', '))
32
+ if (verboseCode) console.log(chalk.dim(JSON.stringify(changes, null, prettify ?? 2)))
33
+
34
+ try {
35
+ fs.writeFileSync(initPath, mergeStr)
36
+ if (verbose) {
37
+ console.log(chalk.bold.green('\nChanges wrote to the file'))
38
+ }
39
+ } catch (e) {
40
+ console.log('')
41
+ console.log(chalk.bold.red('Error writing file'))
42
+ if (verbose) console.error(e)
43
+ else console.log(debugMsg)
31
44
  }
32
45
  }
33
46
 
@@ -75,7 +88,7 @@ export const sync = (desSysFile = DES_SYS_DEFAULT_FILE, options = {}) => {
75
88
  })
76
89
  })
77
90
 
78
- server.listen(13335, () => {
79
- console.log('listening on *:13335')
91
+ server.listen(13336, () => {
92
+ console.log('listening on *:13336')
80
93
  })
81
94
  }