1
- import { join } from 'path' ;
2
- import { promises as fs } from 'fs' ;
3
- import { Script } from 'vm' ;
1
+ import { join } from 'node: path' ;
2
+ import { promises as fs } from 'node: fs' ;
3
+ import { Script } from 'node: vm' ;
4
4
5
5
const kSyntaxError = Symbol . for ( 'syntax error' ) ;
6
6
const kResources = Symbol . for ( 'resources base' ) ;
7
7
8
- class FlowScriptError extends Error {
8
+ export class FlowScriptError extends Error {
9
9
constructor ( fromErr ) {
10
10
super ( fromErr . message ) ;
11
11
this . name = this . constructor . name ;
@@ -23,11 +23,11 @@ class FlowScriptError extends Error {
23
23
} ) ;
24
24
}
25
25
toString ( ) {
26
- return '[FlowScriptError ] ' + this . message + '\n' + this . stack ;
26
+ return '[' + this . name + ' ] ' + this . message + '\n' + this . stack ;
27
27
}
28
28
}
29
29
30
- class FlowSyntaxError extends Error {
30
+ export class FlowSyntaxError extends Error {
31
31
constructor ( fromErr ) {
32
32
super ( fromErr . message ) ;
33
33
this . name = this . constructor . name ;
@@ -45,17 +45,23 @@ class FlowSyntaxError extends Error {
45
45
} ) ;
46
46
}
47
47
toString ( ) {
48
- return '[FlowSyntaxError ] ' + this . message + '\n' + this . stack ;
48
+ return '[' + this . name + ' ] ' + this . message + '\n' + this . stack ;
49
49
}
50
50
}
51
51
52
- export { FlowScripts , JavaScript , JavaScriptResource } ;
52
+ export class FlowResourceError extends FlowScriptError {
53
+ constructor ( fromErr , filename ) {
54
+ super ( fromErr ) ;
55
+ this . filename = filename ;
56
+ this . inner = fromErr ;
57
+ }
58
+ }
53
59
54
- function FlowScripts ( flowName , resourceBase , runContext , timeout = 60000 ) {
55
- this . _name = flowName ;
56
- this . _scripts = { } ;
57
- this . _timeout = timeout ;
58
- this . _runContext = runContext ;
60
+ export function FlowScripts ( flowName , resourceBase , runContext , timeout = 60000 ) {
61
+ this . flowName = flowName ;
62
+ this . scripts = new Map ( ) ;
63
+ this . timeout = timeout ;
64
+ this . runContext = runContext ;
59
65
this [ kResources ] = resourceBase ;
60
66
}
61
67
@@ -83,22 +89,33 @@ FlowScripts.prototype.register = function register({ id, type, behaviour }) {
83
89
84
90
language = 'javascript' ;
85
91
86
- const name = this . _name ;
87
- const filename = `${ name } /${ type } /${ id } ` ;
92
+ const flowName = this . flowName ;
93
+ const filename = `${ flowName } /${ type } /${ id } ` ;
88
94
if ( scriptBody ) {
89
- this . _scripts [ id ] = new JavaScript ( name , scriptBody , this . _runContext , { filename, timeout : this . _timeout } ) ;
95
+ this . scripts . set ( id , new JavaScript ( flowName , scriptBody , this . runContext , { filename, timeout : this . timeout } ) ) ;
90
96
} else if ( resource ) {
91
- this . _scripts [ id ] = new JavaScriptResource ( name , resource , this [ kResources ] , this . _runContext , { filename, timeout : this . _timeout } ) ;
97
+ this . scripts . set (
98
+ id ,
99
+ new JavaScriptResource ( flowName , resource , this [ kResources ] , this . runContext , { filename, timeout : this . timeout } ) ,
100
+ ) ;
92
101
}
93
102
} ;
94
103
95
104
FlowScripts . prototype . getScript = function getScript ( scriptType , { id } ) {
96
- return this . _scripts [ id ] ;
105
+ return this . scripts . get ( id ) ;
97
106
} ;
98
107
99
- function JavaScript ( flowName , scriptBody , runContext , options ) {
108
+ /**
109
+ * Java script
110
+ * @param {string } flowName
111
+ * @param {string|Buffer } scriptBody
112
+ * @param {any } [runContext]
113
+ * @param {import('node:vm').ScriptOptions } options
114
+ */
115
+ export function JavaScript ( flowName , scriptBody , runContext , options ) {
100
116
this . flowName = flowName ;
101
- this . _runContext = runContext ;
117
+ this . options = options ;
118
+ this . runContext = runContext ;
102
119
this . timeout = options ?. timeout ;
103
120
104
121
try {
@@ -124,7 +141,7 @@ JavaScript.prototype.execute = async function execute(executionContext, callback
124
141
from : Buffer . from ,
125
142
} ,
126
143
contextName : this . flowName ,
127
- ...this . _runContext ,
144
+ ...this . runContext ,
128
145
next,
129
146
} ,
130
147
{
@@ -143,28 +160,43 @@ JavaScript.prototype.execute = async function execute(executionContext, callback
143
160
}
144
161
} ;
145
162
146
- function JavaScriptResource ( flowName , resource , resourceBase , runContext , options ) {
163
+ export function JavaScriptResource ( flowName , resource , resourceBase , runContext , options ) {
147
164
this . flowName = flowName ;
148
165
this . resource = resource ;
166
+ this . runContext = runContext ;
149
167
this . options = options ;
150
- this . _runContext = runContext ;
151
- this [ kResources ] = resourceBase ;
168
+ this . timeout = options ?. timeout ;
169
+ this . resourceBase = resourceBase ;
152
170
}
153
171
172
+ /**
173
+ * Get javascript resource content
174
+ * @param {string } resourceBase Resource base
175
+ * @param {* } resource Resource name or path
176
+ * @returns {Promise<string|Buffer } Resource content
177
+ */
178
+ JavaScriptResource . prototype . getResourceContent = function getResourceContent ( resourceBase , resource ) {
179
+ return fs . readFile ( join ( resourceBase , resource ) ) ;
180
+ } ;
181
+
154
182
JavaScriptResource . prototype . execute = async function execute ( executionContext , callback ) {
155
183
let resource ;
156
184
try {
157
185
resource = executionContext . resolveExpression ( this . resource ) ;
158
- var scriptBody = await fs . readFile ( join ( this [ kResources ] , resource ) ) ; // eslint-disable-line no-var
186
+ var scriptBody = await this . getResourceContent ( this . resourceBase , resource ) ; // eslint-disable-line no-var
187
+ if ( ! scriptBody ) throw new TypeError ( `${ this . options . filename } : script resource ${ resource || this . resource } is empty` ) ;
159
188
} catch ( err ) {
160
189
if ( err instanceof SyntaxError ) {
161
190
return callback ( err ) ;
162
191
}
163
- const { filename } = this . options ;
164
- return callback ( new Error ( `${ filename } : script resource ${ resource || this . resource } not found` ) ) ;
192
+ return callback ( new FlowResourceError ( err , this . options . filename ) ) ;
193
+ }
194
+
195
+ if ( ! scriptBody ) {
196
+ throw new FlowResourceError ( ) ;
165
197
}
166
198
167
- const script = new JavaScript ( this . flowName , scriptBody , this . _runContext , {
199
+ const script = new JavaScript ( this . flowName , scriptBody , this . runContext , {
168
200
...this . options ,
169
201
filename : `${ this . options . filename } /${ resource } ` ,
170
202
} ) ;
0 commit comments