@jsreport/jsreport-cli 3.0.0-beta.2 → 3.1.1
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/LICENSE +21 -21
- package/README.md +16 -12
- package/cli.js +93 -93
- package/example.config.json +43 -43
- package/example.server.js +14 -14
- package/index.js +20 -22
- package/jsreport.config.js +8 -8
- package/lib/cliExtension.js +59 -59
- package/lib/{createCommandParser.js → commander/createCommandParser.js} +44 -45
- package/lib/commander/createCustomCommandBuilder.js +150 -0
- package/lib/commander/createLogger.js +51 -0
- package/lib/commander/executeCommand.js +37 -0
- package/lib/commander/getCommandEventName.js +4 -0
- package/lib/commander/index.js +279 -0
- package/lib/commander/jsreportInstance.js +98 -0
- package/lib/commander/registerCommand.js +91 -0
- package/lib/commander/startCommand.js +234 -0
- package/lib/commander/startProcessing.js +208 -0
- package/lib/commands/_initializeApp.js +206 -208
- package/lib/commands/configure.js +393 -395
- package/lib/commands/help.js +453 -454
- package/lib/commands/init.js +76 -77
- package/lib/commands/kill.js +85 -86
- package/lib/commands/render.js +490 -488
- package/lib/commands/repair.js +55 -56
- package/lib/commands/start.js +72 -74
- package/lib/commands/win-install.js +124 -126
- package/lib/commands/win-uninstall.js +100 -102
- package/lib/daemonHandler.js +164 -164
- package/lib/daemonInstance.js +225 -225
- package/lib/{registerExtensionsCommands.js → detectAndRegisterExtensionsCommands.js} +74 -74
- package/lib/instanceHandler.js +316 -316
- package/lib/keepAliveProcess.js +205 -205
- package/lib/{errorUtils.js → utils/error.js} +149 -127
- package/lib/{getTempPaths.js → utils/getTempPaths.js} +39 -39
- package/lib/{normalizePathOptionOrArg.js → utils/normalizePathOptionOrArg.js} +41 -41
- package/lib/{normalizeSocketPath.js → utils/normalizeSocketPath.js} +9 -9
- package/lib/{startSocketServer.js → utils/startSocketServer.js} +53 -53
- package/package.json +105 -106
- package/test/testUtils.js +197 -198
- package/lib/commander.js +0 -1108
|
@@ -1,127 +1,149 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
function formatError (err) {
|
|
4
|
-
return err.message || ''
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
function addStack (err, stack, { stackPrefix = '', stripMessage = false } = {}) {
|
|
8
|
-
if (stack != null && stack !== '') {
|
|
9
|
-
let newStack = stack
|
|
10
|
-
let originalStack = ''
|
|
11
|
-
|
|
12
|
-
if (err.stack != null && err.stack !== '') {
|
|
13
|
-
originalStack = `${err.stack}\n`
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
if (stripMessage) {
|
|
17
|
-
// to avoid duplicating message we strip the message
|
|
18
|
-
// from the stack if it is equals to the message of error
|
|
19
|
-
newStack = newStack.replace(/(\S+:) (.+)(\r?\n)/, (match, gLabel, gMessage, gRest) => {
|
|
20
|
-
if (err.message === gMessage) {
|
|
21
|
-
return `${gLabel}${gRest}`
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
return match
|
|
25
|
-
})
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
err.stack = `${originalStack}${stackPrefix}${newStack}`
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function getErrors (err) {
|
|
33
|
-
let parent = err
|
|
34
|
-
const errors = []
|
|
35
|
-
|
|
36
|
-
while (parent != null) {
|
|
37
|
-
const customProps = Object.assign({}, parent)
|
|
38
|
-
let currentStack = parent.stack || ''
|
|
39
|
-
let cleanState = false
|
|
40
|
-
|
|
41
|
-
if (parent.cleanState === true) {
|
|
42
|
-
cleanState = true
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// making sure custom props like "originalError", "cleanState" are not part of meta
|
|
46
|
-
delete customProps.originalError
|
|
47
|
-
delete customProps.disableExit
|
|
48
|
-
delete customProps.cleanState
|
|
49
|
-
|
|
50
|
-
const fakeE = { message: parent.message, stack: '' }
|
|
51
|
-
|
|
52
|
-
addStack(fakeE, parent.stack || '', {
|
|
53
|
-
stripMessage: true
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
currentStack = fakeE.stack
|
|
57
|
-
|
|
58
|
-
errors.push({
|
|
59
|
-
message: parent.message,
|
|
60
|
-
stack: currentStack || '',
|
|
61
|
-
meta: Object.keys(customProps).length > 0 ? customProps : undefined,
|
|
62
|
-
cleanState
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
parent = parent.originalError
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
return errors
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function getErrorMessages (err) {
|
|
72
|
-
const errors = getErrors(err)
|
|
73
|
-
let count = errors.length
|
|
74
|
-
let cleanState = false
|
|
75
|
-
const messages = []
|
|
76
|
-
const stacks = []
|
|
77
|
-
|
|
78
|
-
errors.forEach((error) => {
|
|
79
|
-
let customProps = error.meta || {}
|
|
80
|
-
const currentStack = error.stack
|
|
81
|
-
|
|
82
|
-
if (!cleanState && error.cleanState === true) {
|
|
83
|
-
cleanState = true
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
customProps = JSON.stringify(customProps)
|
|
87
|
-
|
|
88
|
-
if (cleanState) {
|
|
89
|
-
messages.push(`${formatError(error)}`)
|
|
90
|
-
} else {
|
|
91
|
-
messages.push(`${formatError(error)} (${count})`)
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
if (customProps === '{}') {
|
|
95
|
-
customProps = ''
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (currentStack !== '') {
|
|
99
|
-
const causedBy = `caused by error (${count}):`
|
|
100
|
-
|
|
101
|
-
if (customProps !== '') {
|
|
102
|
-
stacks.push(`${causedBy}\n-> meta = ${customProps}\n-> stack\n${currentStack}`)
|
|
103
|
-
} else {
|
|
104
|
-
stacks.push(`${causedBy}\n-> stack\n${currentStack}`)
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
count--
|
|
109
|
-
})
|
|
110
|
-
|
|
111
|
-
if (!cleanState && stacks.length > 0) {
|
|
112
|
-
messages.push(`\n${stacks.join('\n')}`)
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
return messages
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
function formatError (err) {
|
|
4
|
+
return err.message || ''
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function addStack (err, stack, { stackPrefix = '', stripMessage = false } = {}) {
|
|
8
|
+
if (stack != null && stack !== '') {
|
|
9
|
+
let newStack = stack
|
|
10
|
+
let originalStack = ''
|
|
11
|
+
|
|
12
|
+
if (err.stack != null && err.stack !== '') {
|
|
13
|
+
originalStack = `${err.stack}\n`
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (stripMessage) {
|
|
17
|
+
// to avoid duplicating message we strip the message
|
|
18
|
+
// from the stack if it is equals to the message of error
|
|
19
|
+
newStack = newStack.replace(/(\S+:) (.+)(\r?\n)/, (match, gLabel, gMessage, gRest) => {
|
|
20
|
+
if (err.message === gMessage) {
|
|
21
|
+
return `${gLabel}${gRest}`
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return match
|
|
25
|
+
})
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
err.stack = `${originalStack}${stackPrefix}${newStack}`
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function getErrors (err) {
|
|
33
|
+
let parent = err
|
|
34
|
+
const errors = []
|
|
35
|
+
|
|
36
|
+
while (parent != null) {
|
|
37
|
+
const customProps = Object.assign({}, parent)
|
|
38
|
+
let currentStack = parent.stack || ''
|
|
39
|
+
let cleanState = false
|
|
40
|
+
|
|
41
|
+
if (parent.cleanState === true) {
|
|
42
|
+
cleanState = true
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// making sure custom props like "originalError", "cleanState" are not part of meta
|
|
46
|
+
delete customProps.originalError
|
|
47
|
+
delete customProps.disableExit
|
|
48
|
+
delete customProps.cleanState
|
|
49
|
+
|
|
50
|
+
const fakeE = { message: parent.message, stack: '' }
|
|
51
|
+
|
|
52
|
+
addStack(fakeE, parent.stack || '', {
|
|
53
|
+
stripMessage: true
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
currentStack = fakeE.stack
|
|
57
|
+
|
|
58
|
+
errors.push({
|
|
59
|
+
message: parent.message,
|
|
60
|
+
stack: currentStack || '',
|
|
61
|
+
meta: Object.keys(customProps).length > 0 ? getSimpleProperties(customProps) : undefined,
|
|
62
|
+
cleanState
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
parent = parent.originalError
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return errors
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function getErrorMessages (err) {
|
|
72
|
+
const errors = getErrors(err)
|
|
73
|
+
let count = errors.length
|
|
74
|
+
let cleanState = false
|
|
75
|
+
const messages = []
|
|
76
|
+
const stacks = []
|
|
77
|
+
|
|
78
|
+
errors.forEach((error) => {
|
|
79
|
+
let customProps = error.meta || {}
|
|
80
|
+
const currentStack = error.stack
|
|
81
|
+
|
|
82
|
+
if (!cleanState && error.cleanState === true) {
|
|
83
|
+
cleanState = true
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
customProps = JSON.stringify(customProps)
|
|
87
|
+
|
|
88
|
+
if (cleanState) {
|
|
89
|
+
messages.push(`${formatError(error)}`)
|
|
90
|
+
} else {
|
|
91
|
+
messages.push(`${formatError(error)} (${count})`)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (customProps === '{}') {
|
|
95
|
+
customProps = ''
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (currentStack !== '') {
|
|
99
|
+
const causedBy = `caused by error (${count}):`
|
|
100
|
+
|
|
101
|
+
if (customProps !== '') {
|
|
102
|
+
stacks.push(`${causedBy}\n-> meta = ${customProps}\n-> stack\n${currentStack}`)
|
|
103
|
+
} else {
|
|
104
|
+
stacks.push(`${causedBy}\n-> stack\n${currentStack}`)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
count--
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
if (!cleanState && stacks.length > 0) {
|
|
112
|
+
messages.push(`\n${stacks.join('\n')}`)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return messages
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function getSimpleProperties (obj) {
|
|
119
|
+
const newObj = {}
|
|
120
|
+
|
|
121
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
122
|
+
if (
|
|
123
|
+
value == null ||
|
|
124
|
+
typeof value === 'string' ||
|
|
125
|
+
typeof value === 'number' ||
|
|
126
|
+
typeof value === 'boolean' ||
|
|
127
|
+
typeof value === 'bigint'
|
|
128
|
+
) {
|
|
129
|
+
newObj[key] = value
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (Object.keys(newObj).length === 0) {
|
|
134
|
+
return undefined
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return newObj
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function printError (err, logger) {
|
|
141
|
+
const messages = getErrorMessages(err)
|
|
142
|
+
|
|
143
|
+
logger.error(messages.join('. '))
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
module.exports.printError = printError
|
|
147
|
+
module.exports.getErrors = getErrors
|
|
148
|
+
module.exports.getErrorMessages = getErrorMessages
|
|
149
|
+
module.exports.addStack = addStack
|
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
const path = require('path')
|
|
2
|
-
const fs = require('fs')
|
|
3
|
-
const os = require('os')
|
|
4
|
-
|
|
5
|
-
module.exports = () => {
|
|
6
|
-
const useCustomTempDirectory = process.env.cli_tempDirectory != null
|
|
7
|
-
const useCustomSocketDirectory = process.env.cli_socketsDirectory != null
|
|
8
|
-
const ROOT_PATH = !useCustomTempDirectory ? path.join(os.tmpdir(), 'jsreport') : process.env.cli_tempDirectory
|
|
9
|
-
const CLI_PATH = path.join(ROOT_PATH, 'cli')
|
|
10
|
-
const MAIN_SOCK_PATH = !useCustomSocketDirectory ? path.join(CLI_PATH, 'sock') : process.env.cli_socketsDirectory
|
|
11
|
-
const WORKER_SOCK_PATH = path.join(path.dirname(MAIN_SOCK_PATH), 'wSock')
|
|
12
|
-
|
|
13
|
-
return {
|
|
14
|
-
useCustomSocketDirectory,
|
|
15
|
-
rootPath: ROOT_PATH,
|
|
16
|
-
cliPath: CLI_PATH,
|
|
17
|
-
mainSockPath: MAIN_SOCK_PATH,
|
|
18
|
-
workerSockPath: WORKER_SOCK_PATH,
|
|
19
|
-
createPaths: () => {
|
|
20
|
-
if (!useCustomSocketDirectory) {
|
|
21
|
-
tryCreate(ROOT_PATH)
|
|
22
|
-
tryCreate(CLI_PATH)
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
tryCreate(MAIN_SOCK_PATH)
|
|
26
|
-
tryCreate(WORKER_SOCK_PATH)
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function tryCreate (dir) {
|
|
32
|
-
try {
|
|
33
|
-
fs.mkdirSync(dir, '0755')
|
|
34
|
-
} catch (err) {
|
|
35
|
-
if (err.code !== 'EEXIST') {
|
|
36
|
-
throw err
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
1
|
+
const path = require('path')
|
|
2
|
+
const fs = require('fs')
|
|
3
|
+
const os = require('os')
|
|
4
|
+
|
|
5
|
+
module.exports = () => {
|
|
6
|
+
const useCustomTempDirectory = process.env.cli_tempDirectory != null
|
|
7
|
+
const useCustomSocketDirectory = process.env.cli_socketsDirectory != null
|
|
8
|
+
const ROOT_PATH = !useCustomTempDirectory ? path.join(os.tmpdir(), 'jsreport') : process.env.cli_tempDirectory
|
|
9
|
+
const CLI_PATH = path.join(ROOT_PATH, 'cli')
|
|
10
|
+
const MAIN_SOCK_PATH = !useCustomSocketDirectory ? path.join(CLI_PATH, 'sock') : process.env.cli_socketsDirectory
|
|
11
|
+
const WORKER_SOCK_PATH = path.join(path.dirname(MAIN_SOCK_PATH), 'wSock')
|
|
12
|
+
|
|
13
|
+
return {
|
|
14
|
+
useCustomSocketDirectory,
|
|
15
|
+
rootPath: ROOT_PATH,
|
|
16
|
+
cliPath: CLI_PATH,
|
|
17
|
+
mainSockPath: MAIN_SOCK_PATH,
|
|
18
|
+
workerSockPath: WORKER_SOCK_PATH,
|
|
19
|
+
createPaths: () => {
|
|
20
|
+
if (!useCustomSocketDirectory) {
|
|
21
|
+
tryCreate(ROOT_PATH)
|
|
22
|
+
tryCreate(CLI_PATH)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
tryCreate(MAIN_SOCK_PATH)
|
|
26
|
+
tryCreate(WORKER_SOCK_PATH)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function tryCreate (dir) {
|
|
32
|
+
try {
|
|
33
|
+
fs.mkdirSync(dir, '0755')
|
|
34
|
+
} catch (err) {
|
|
35
|
+
if (err.code !== 'EEXIST') {
|
|
36
|
+
throw err
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -1,41 +1,41 @@
|
|
|
1
|
-
const path = require('path')
|
|
2
|
-
const fs = require('fs')
|
|
3
|
-
|
|
4
|
-
module.exports = function normalizePathOption (cwd, optionOrArgName, value, options = {}) {
|
|
5
|
-
const type = options.type ? options.type : 'option'
|
|
6
|
-
|
|
7
|
-
if (typeof value === 'string') {
|
|
8
|
-
const pathVal = path.resolve(cwd, value)
|
|
9
|
-
|
|
10
|
-
if (options.strict) {
|
|
11
|
-
if (!path.isAbsolute(pathVal)) {
|
|
12
|
-
throw new Error(`${optionOrArgName} ${type} must be a valid file path`)
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const read = options.read != null ? options.read : true
|
|
17
|
-
if (!read) {
|
|
18
|
-
return pathVal
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
try {
|
|
22
|
-
if (options.json) {
|
|
23
|
-
return JSON.parse(fs.readFileSync(pathVal).toString())
|
|
24
|
-
} else {
|
|
25
|
-
return fs.readFileSync(pathVal).toString()
|
|
26
|
-
}
|
|
27
|
-
} catch (e) {
|
|
28
|
-
if (e.code !== 'ENOENT' && options.json) {
|
|
29
|
-
throw new Error(`${optionOrArgName} ${type} error: file in ${pathVal} doesn't have a valid JSON content`)
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
throw new Error(`${optionOrArgName} ${type} error: can't read file in ${pathVal}`)
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
if (options.strict) {
|
|
37
|
-
throw new Error(`${optionOrArgName} ${type} must be a file path`)
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return value
|
|
41
|
-
}
|
|
1
|
+
const path = require('path')
|
|
2
|
+
const fs = require('fs')
|
|
3
|
+
|
|
4
|
+
module.exports = function normalizePathOption (cwd, optionOrArgName, value, options = {}) {
|
|
5
|
+
const type = options.type ? options.type : 'option'
|
|
6
|
+
|
|
7
|
+
if (typeof value === 'string') {
|
|
8
|
+
const pathVal = path.resolve(cwd, value)
|
|
9
|
+
|
|
10
|
+
if (options.strict) {
|
|
11
|
+
if (!path.isAbsolute(pathVal)) {
|
|
12
|
+
throw new Error(`${optionOrArgName} ${type} must be a valid file path`)
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const read = options.read != null ? options.read : true
|
|
17
|
+
if (!read) {
|
|
18
|
+
return pathVal
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
try {
|
|
22
|
+
if (options.json) {
|
|
23
|
+
return JSON.parse(fs.readFileSync(pathVal).toString())
|
|
24
|
+
} else {
|
|
25
|
+
return fs.readFileSync(pathVal).toString()
|
|
26
|
+
}
|
|
27
|
+
} catch (e) {
|
|
28
|
+
if (e.code !== 'ENOENT' && options.json) {
|
|
29
|
+
throw new Error(`${optionOrArgName} ${type} error: file in ${pathVal} doesn't have a valid JSON content`)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
throw new Error(`${optionOrArgName} ${type} error: can't read file in ${pathVal}`)
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (options.strict) {
|
|
37
|
+
throw new Error(`${optionOrArgName} ${type} must be a file path`)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return value
|
|
41
|
+
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
module.exports = function normalizeSocketPath (socketPath) {
|
|
4
|
-
if (process.platform === 'win32') {
|
|
5
|
-
socketPath = '\\\\.\\pipe\\' + socketPath
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
return socketPath
|
|
9
|
-
}
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
module.exports = function normalizeSocketPath (socketPath) {
|
|
4
|
+
if (process.platform === 'win32') {
|
|
5
|
+
socketPath = '\\\\.\\pipe\\' + socketPath
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
return socketPath
|
|
9
|
+
}
|
|
@@ -1,53 +1,53 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const path = require('path')
|
|
4
|
-
const fs = require('fs')
|
|
5
|
-
const nanoid = require('nanoid')
|
|
6
|
-
const nssocket = require('nssocket')
|
|
7
|
-
const normalizeSocketPath = require('./normalizeSocketPath')
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Starts a server using a socket file randomly generated
|
|
11
|
-
* and resolves when it is ready to listen connections
|
|
12
|
-
*/
|
|
13
|
-
module.exports = function startSocketServer (options, cb) {
|
|
14
|
-
const socketPath = options.socketPath
|
|
15
|
-
const socketPrefix = options.socketPrefix || ''
|
|
16
|
-
const createSymbolicForSocket = options.createSymbolicForSocket
|
|
17
|
-
const protocol = options.protocol
|
|
18
|
-
const uid = nanoid(7)
|
|
19
|
-
const server = nssocket.createServer(protocol)
|
|
20
|
-
|
|
21
|
-
const socketFile = path.join(socketPath, [
|
|
22
|
-
socketPrefix,
|
|
23
|
-
uid,
|
|
24
|
-
'sock'
|
|
25
|
-
].join('.'))
|
|
26
|
-
|
|
27
|
-
if (createSymbolicForSocket) {
|
|
28
|
-
fs.openSync(socketFile, 'w')
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const normalizedSocketFile = normalizeSocketPath(socketFile)
|
|
32
|
-
|
|
33
|
-
server.on('listening', () => {
|
|
34
|
-
cb(null, {
|
|
35
|
-
uid: uid,
|
|
36
|
-
socketFile: socketFile,
|
|
37
|
-
normalizedSocketFile: normalizedSocketFile,
|
|
38
|
-
server: server
|
|
39
|
-
})
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
server.on('error', (err) => {
|
|
43
|
-
if (err.code === 'EADDRINUSE') {
|
|
44
|
-
return startSocketServer(options, cb)
|
|
45
|
-
} else {
|
|
46
|
-
cb(err)
|
|
47
|
-
}
|
|
48
|
-
})
|
|
49
|
-
|
|
50
|
-
server.listen(normalizedSocketFile)
|
|
51
|
-
|
|
52
|
-
return server
|
|
53
|
-
}
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const path = require('path')
|
|
4
|
+
const fs = require('fs')
|
|
5
|
+
const { nanoid } = require('nanoid')
|
|
6
|
+
const nssocket = require('nssocket')
|
|
7
|
+
const normalizeSocketPath = require('./normalizeSocketPath')
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Starts a server using a socket file randomly generated
|
|
11
|
+
* and resolves when it is ready to listen connections
|
|
12
|
+
*/
|
|
13
|
+
module.exports = function startSocketServer (options, cb) {
|
|
14
|
+
const socketPath = options.socketPath
|
|
15
|
+
const socketPrefix = options.socketPrefix || ''
|
|
16
|
+
const createSymbolicForSocket = options.createSymbolicForSocket
|
|
17
|
+
const protocol = options.protocol
|
|
18
|
+
const uid = nanoid(7)
|
|
19
|
+
const server = nssocket.createServer(protocol)
|
|
20
|
+
|
|
21
|
+
const socketFile = path.join(socketPath, [
|
|
22
|
+
socketPrefix,
|
|
23
|
+
uid,
|
|
24
|
+
'sock'
|
|
25
|
+
].join('.'))
|
|
26
|
+
|
|
27
|
+
if (createSymbolicForSocket) {
|
|
28
|
+
fs.openSync(socketFile, 'w')
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const normalizedSocketFile = normalizeSocketPath(socketFile)
|
|
32
|
+
|
|
33
|
+
server.on('listening', () => {
|
|
34
|
+
cb(null, {
|
|
35
|
+
uid: uid,
|
|
36
|
+
socketFile: socketFile,
|
|
37
|
+
normalizedSocketFile: normalizedSocketFile,
|
|
38
|
+
server: server
|
|
39
|
+
})
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
server.on('error', (err) => {
|
|
43
|
+
if (err.code === 'EADDRINUSE') {
|
|
44
|
+
return startSocketServer(options, cb)
|
|
45
|
+
} else {
|
|
46
|
+
cb(err)
|
|
47
|
+
}
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
server.listen(normalizedSocketFile)
|
|
51
|
+
|
|
52
|
+
return server
|
|
53
|
+
}
|