node-red-contrib-typescript 1.0.0
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/README.md +1 -0
- package/helper.js +216 -0
- package/node-red-contrib-typescript/README.md +1 -0
- package/node-red-contrib-typescript/helper.js +230 -0
- package/node-red-contrib-typescript/package.json +12 -0
- package/node-red-contrib-typescript/src/instance/instance.html +271 -0
- package/node-red-contrib-typescript/src/instance/instance.js +157 -0
- package/node-red-contrib-typescript/test.d.ts +3 -0
- package/node-red-contrib-typescript/typescript.js +171678 -0
- package/package.json +12 -0
- package/src/instance/instance.html +270 -0
- package/src/instance/instance.js +157 -0
- package/test.d.ts +3 -0
- package/typescript.js +171678 -0
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
<script type="text/javascript">
|
|
2
|
+
var typescriptRenderLog = function (id, data, node) {
|
|
3
|
+
let $chart = document.getElementById("data-view-output-chart-" + id);
|
|
4
|
+
if (!$chart) {
|
|
5
|
+
const $container = document.getElementById(id)
|
|
6
|
+
if (!$container) { return }
|
|
7
|
+
const text = document.createElementNS("http://www.w3.org/2000/svg", 'text')
|
|
8
|
+
text.setAttribute('id', `data-view-output-text-${id}`)
|
|
9
|
+
text.setAttribute('x', `0`)
|
|
10
|
+
text.setAttribute('y', `45`)
|
|
11
|
+
text.setAttribute('text-anchor', `start`)
|
|
12
|
+
$container.insertBefore(text, $container.lastChild.nextSibling);
|
|
13
|
+
// Creat group
|
|
14
|
+
const group = document.createElementNS("http://www.w3.org/2000/svg", 'g');
|
|
15
|
+
group.setAttribute('id', `data-view-output-chart-${id}`);
|
|
16
|
+
group.setAttribute('innerHTML', `data-view-output-chart-${id}`);
|
|
17
|
+
$container.insertBefore(group, $container.lastChild.nextSibling);
|
|
18
|
+
$chart = $container
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
let $text = document.getElementById(`data-view-output-text-${id}`);
|
|
22
|
+
$text.textContent = ''
|
|
23
|
+
let x = ``
|
|
24
|
+
if (data === undefined) {
|
|
25
|
+
x = "";
|
|
26
|
+
} else if (typeof data == `object`) {
|
|
27
|
+
x = JSON.stringify(data, null, ' ')
|
|
28
|
+
} else {
|
|
29
|
+
x = data + ''
|
|
30
|
+
}
|
|
31
|
+
var
|
|
32
|
+
lines = x.split('\n'),
|
|
33
|
+
tn,
|
|
34
|
+
ts
|
|
35
|
+
|
|
36
|
+
lines.forEach(function (value, index) {
|
|
37
|
+
let countSpace = value.length - value.trim().length
|
|
38
|
+
tn = document.createTextNode(value);
|
|
39
|
+
ts = document.createElementNS("http://www.w3.org/2000/svg", "tspan");
|
|
40
|
+
ts.setAttribute('dy', "1.2em");
|
|
41
|
+
ts.setAttribute('x', (countSpace * 10) + 10);
|
|
42
|
+
ts.setAttribute('text-anchor', 'start');
|
|
43
|
+
ts.setAttribute('fill', '#555');
|
|
44
|
+
ts.setAttribute('stroke', 'transparent');
|
|
45
|
+
ts.setAttribute('font-size', `11`)
|
|
46
|
+
ts.appendChild(tn);
|
|
47
|
+
$text.append(ts);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function typescriptLogSubscriptionHandler(event, data) {
|
|
52
|
+
console.log(data);
|
|
53
|
+
if (data.hasOwnProperty("data")) {
|
|
54
|
+
let node = RED.nodes.node(data.id);
|
|
55
|
+
typescriptRenderLog(data.id, data.data, node);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
class TSEditor {
|
|
60
|
+
constructor(node, path) {
|
|
61
|
+
this.node = node;
|
|
62
|
+
|
|
63
|
+
const mainModel = monaco.editor.createModel(
|
|
64
|
+
node.code || "",
|
|
65
|
+
'typescript',
|
|
66
|
+
window.monaco.Uri.parse('file://' + path)
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
this.editor = RED.editor.createEditor({
|
|
70
|
+
id: "node-input-example-editor",
|
|
71
|
+
mode: 'ace/mode/typescript',
|
|
72
|
+
value: node.code || "",
|
|
73
|
+
theme: "vs-dark",
|
|
74
|
+
focus: true,
|
|
75
|
+
model: mainModel,
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
window.monaco.editor.getModels()[0].dispose();
|
|
79
|
+
|
|
80
|
+
window.monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
|
|
81
|
+
"paths": {
|
|
82
|
+
"/*": [
|
|
83
|
+
"file:///*"
|
|
84
|
+
],
|
|
85
|
+
"#*": [
|
|
86
|
+
"file:///lib/*"
|
|
87
|
+
]
|
|
88
|
+
},
|
|
89
|
+
experimentalDecorators: true,
|
|
90
|
+
moduleResolution: window.monaco.languages.typescript.ModuleResolutionKind.NodeJs,
|
|
91
|
+
allowNonTsExtensions: true,
|
|
92
|
+
module: monaco.languages.typescript.ModuleKind.ESNext,
|
|
93
|
+
target: monaco.languages.typescript.ScriptTarget.ESNext
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
recalculateGlobal(subflowId) {
|
|
98
|
+
let nodes = [];
|
|
99
|
+
let subflowNodes = [];
|
|
100
|
+
let workspaces = [];
|
|
101
|
+
let subflows = [];
|
|
102
|
+
|
|
103
|
+
RED.nodes.eachWorkspace(item => workspaces.push(item));
|
|
104
|
+
RED.nodes.eachSubflow(item => subflows.push(item));
|
|
105
|
+
RED.nodes.eachNode(item => {
|
|
106
|
+
if (item.type === "typescript")
|
|
107
|
+
nodes.push(item);
|
|
108
|
+
if (item.type.startsWith("subflow:"))
|
|
109
|
+
subflowNodes.push(item);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
function resolveFlow(id) {
|
|
113
|
+
let imports = {};
|
|
114
|
+
nodes
|
|
115
|
+
.filter(node => node.z === id)
|
|
116
|
+
.forEach(node => {
|
|
117
|
+
imports[`/${encodeURIComponent(node.name)}.ts`] = node.code;
|
|
118
|
+
});
|
|
119
|
+
subflowNodes
|
|
120
|
+
.filter(node => node.z === id)
|
|
121
|
+
.forEach(node => {
|
|
122
|
+
let subImports = resolveFlow(node.type.split(":")[1]);
|
|
123
|
+
for (let key in subImports) {
|
|
124
|
+
imports[`/${encodeURIComponent(node.name)}${key}`] = subImports[key];
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
return imports;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
let imports = {};
|
|
132
|
+
workspaces.forEach(workspace => {
|
|
133
|
+
let subImports = resolveFlow(workspace.id);
|
|
134
|
+
for (let key in subImports) {
|
|
135
|
+
if (key.indexOf("//") === -1)
|
|
136
|
+
imports[`/${encodeURIComponent(workspace.label)}${key}`] = subImports[key];
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
if (subflowId) {
|
|
141
|
+
let subflow = subflows.find(s => s.id === subflowId);
|
|
142
|
+
let subImports = resolveFlow(subflowId);
|
|
143
|
+
for (let key in subImports) {
|
|
144
|
+
if (key.indexOf("//") === -1)
|
|
145
|
+
imports[`/subflow${key}`] = subImports[key];
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
for (let key in imports) {
|
|
150
|
+
window.monaco.languages.typescript.typescriptDefaults.addExtraLib(imports[key], "file://" + key);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
window.monaco.languages.typescript.typescriptDefaults.addExtraLib(`
|
|
154
|
+
let node = {
|
|
155
|
+
error: (msg:any) => {},
|
|
156
|
+
send: (msg:any) => {},
|
|
157
|
+
on: (topic:"close"|"input", callback:any) => {},
|
|
158
|
+
status: (status:{fill?:"red"|"yellow"|"green"|"blue"|"grey", shape?:"dot"|"ring", text?:string}) => {},
|
|
159
|
+
log: (data:any) => {}
|
|
160
|
+
};
|
|
161
|
+
let flow = {
|
|
162
|
+
get: (topic:string):any => {};
|
|
163
|
+
set: (topic:string, value:any) => {};
|
|
164
|
+
};
|
|
165
|
+
let global = {
|
|
166
|
+
get: (topic:string):any => {};
|
|
167
|
+
set: (topic:string, value:any) => {};
|
|
168
|
+
};
|
|
169
|
+
let context = {
|
|
170
|
+
get: (topic:string):any => {};
|
|
171
|
+
set: (topic:string, value:any) => {};
|
|
172
|
+
};
|
|
173
|
+
let env = {
|
|
174
|
+
get: (topic:string):any => {};
|
|
175
|
+
set: (topic:string, value:any) => {};
|
|
176
|
+
};
|
|
177
|
+
`, 'global.d.ts'
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
getCode() {
|
|
182
|
+
return this.editor.getValue();
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
destroy() {
|
|
186
|
+
this.editor.destroy();
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
</script>
|
|
190
|
+
<script type="text/javascript">
|
|
191
|
+
RED.nodes.registerType('typescript',{
|
|
192
|
+
category: 'function',
|
|
193
|
+
color: '#3178c6',
|
|
194
|
+
defaults: {
|
|
195
|
+
name: {value:""},
|
|
196
|
+
outputs:{value:1},
|
|
197
|
+
code: {value:""},
|
|
198
|
+
},
|
|
199
|
+
inputs:1,
|
|
200
|
+
outputs:1,
|
|
201
|
+
icon: "function.svg",
|
|
202
|
+
label: function () {
|
|
203
|
+
RED.comms.unsubscribe('typescript-log-' + this.id, typescriptLogSubscriptionHandler);
|
|
204
|
+
RED.comms.subscribe('typescript-log-' + this.id, typescriptLogSubscriptionHandler);
|
|
205
|
+
return this.name || "typescript";
|
|
206
|
+
},
|
|
207
|
+
oneditprepare: function() {
|
|
208
|
+
let path = "/__editor__.tsx";
|
|
209
|
+
|
|
210
|
+
let workspaces = [];
|
|
211
|
+
let subflows = [];
|
|
212
|
+
|
|
213
|
+
RED.nodes.eachWorkspace(item => workspaces.push(item));
|
|
214
|
+
RED.nodes.eachSubflow(item => subflows.push(item));
|
|
215
|
+
|
|
216
|
+
let target = this;
|
|
217
|
+
if (workspaces.some(wf => wf.id === target.z)) {
|
|
218
|
+
target = workspaces.find(wf => wf.id === target.z);
|
|
219
|
+
path = `/${target.label}${path}`;
|
|
220
|
+
} else if (subflows.some(wf => wf.id === target.z)) {
|
|
221
|
+
target = subflows.find(wf => wf.id === target.z);
|
|
222
|
+
path = `/subflow${path}`;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
this.view = {
|
|
226
|
+
editor: new TSEditor(this, path),
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
this.view.editor.recalculateGlobal(path.startsWith("/subflow") ? this.z : null);
|
|
230
|
+
|
|
231
|
+
$( "#node-input-outputs" ).spinner({
|
|
232
|
+
min: 0,
|
|
233
|
+
max: 500,
|
|
234
|
+
change: function(event, ui) {
|
|
235
|
+
var value = parseInt(this.value);
|
|
236
|
+
value = isNaN(value) ? 1 : value;
|
|
237
|
+
value = Math.max(value, parseInt($(this).attr("aria-valuemin")));
|
|
238
|
+
value = Math.min(value, parseInt($(this).attr("aria-valuemax")));
|
|
239
|
+
if (value !== this.value) { $(this).spinner("value", value); }
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
},
|
|
243
|
+
oneditsave: function() {
|
|
244
|
+
this.code = this.view.editor.getCode();
|
|
245
|
+
this.view.editor.destroy();
|
|
246
|
+
delete this.view;
|
|
247
|
+
},
|
|
248
|
+
oneditcancel: function() {
|
|
249
|
+
this.view.editor.destroy();
|
|
250
|
+
delete this.view;
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
</script>
|
|
254
|
+
|
|
255
|
+
<script type="text/html" data-template-name="typescript">
|
|
256
|
+
<div class="form-row">
|
|
257
|
+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
258
|
+
<input type="text" id="node-input-name" placeholder="Name">
|
|
259
|
+
</div>
|
|
260
|
+
|
|
261
|
+
<div class="form-row">
|
|
262
|
+
<label for="node-input-outputs"><i class="fa fa-random"></i> <span data-i18n="function.label.outputs"></span></label>
|
|
263
|
+
<input id="node-input-outputs" style="width: 60px;" value="1">
|
|
264
|
+
</div>
|
|
265
|
+
|
|
266
|
+
<div style="height: calc(100% - 68px); min-height:250px; width: 800px" class="node-text-editor" id="node-input-example-editor"></div>
|
|
267
|
+
</script>
|
|
268
|
+
|
|
269
|
+
<script type="text/html" data-help-name="typescript">
|
|
270
|
+
<p>A simple node that converts the message payloads into all lower-case characters</p>
|
|
271
|
+
</script>
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
let registry = {};
|
|
2
|
+
module.exports = function(RED) {
|
|
3
|
+
var helper = require("../../helper.js");
|
|
4
|
+
var vm = require("vm");
|
|
5
|
+
|
|
6
|
+
function TypescriptInstance(config) {
|
|
7
|
+
RED.nodes.createNode(this,config);
|
|
8
|
+
var node = this;
|
|
9
|
+
node.config = config;
|
|
10
|
+
node.libs = config.libs;
|
|
11
|
+
node.modules = config.modules;
|
|
12
|
+
node.code = config.code;
|
|
13
|
+
|
|
14
|
+
var sandbox = helper.createSandbox(RED, node);
|
|
15
|
+
let moduleErrors = false;
|
|
16
|
+
|
|
17
|
+
// region Registry
|
|
18
|
+
let tabs = {};
|
|
19
|
+
RED.nodes.eachNode(n => {
|
|
20
|
+
if (n.type === "tab") tabs[n.id] = n;
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
let path = "/";
|
|
24
|
+
let segment = RED.nodes.getNode(node.z);
|
|
25
|
+
if (segment === null) {
|
|
26
|
+
path = `/${tabs[node.z].label}${path}`;
|
|
27
|
+
}
|
|
28
|
+
while(segment !== null) {
|
|
29
|
+
if (segment.node.name === undefined) {
|
|
30
|
+
path = `/${segment.node.id}${path}`;
|
|
31
|
+
} else {
|
|
32
|
+
path = `/${segment.node.name}${path}`;
|
|
33
|
+
}
|
|
34
|
+
let parent = RED.nodes.getNode(segment.node.z);
|
|
35
|
+
if (parent === null) {
|
|
36
|
+
path = `/${tabs[segment.node.z].label}${path}`;
|
|
37
|
+
}
|
|
38
|
+
segment = parent;
|
|
39
|
+
}
|
|
40
|
+
registry[path + node.config.name] = node;
|
|
41
|
+
node.on("close", () => {delete registry[path + node.config.name]});
|
|
42
|
+
|
|
43
|
+
if (node.config.name === "index") {
|
|
44
|
+
let pkgPath = path.substring(0, path.length - 1);
|
|
45
|
+
registry[pkgPath] = node;
|
|
46
|
+
node.on("close", () => {delete registry[pkgPath]});
|
|
47
|
+
}
|
|
48
|
+
// endregion
|
|
49
|
+
|
|
50
|
+
// region Dependencies
|
|
51
|
+
let modules = {};
|
|
52
|
+
let libraryPromises = [];
|
|
53
|
+
const regexp = /require\("(?<lib>.*)"\)/gm;
|
|
54
|
+
for (const match of helper.transpile(config.code).matchAll(regexp)) {
|
|
55
|
+
let target = match[1];
|
|
56
|
+
if (target.startsWith("/subflow")) {
|
|
57
|
+
target = path + target.substring(9);
|
|
58
|
+
}
|
|
59
|
+
if (target.startsWith("#")) {
|
|
60
|
+
target = "/lib/" + target.substring(1);
|
|
61
|
+
}
|
|
62
|
+
if (target.startsWith("$/")) {
|
|
63
|
+
target = target.substring(1);
|
|
64
|
+
}
|
|
65
|
+
if (target.startsWith("./")) {
|
|
66
|
+
target = path + target.substring(2);
|
|
67
|
+
}
|
|
68
|
+
if (target.endsWith(".pkg")) {
|
|
69
|
+
target = target.substring(0, target.length - 4) + "/index";
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (target.startsWith("/")) {
|
|
73
|
+
libraryPromises.push(
|
|
74
|
+
new Promise(async (resolve, reject) => {
|
|
75
|
+
let dependency = registry[target];
|
|
76
|
+
for(let i = 0; i < 100 && (dependency === null || dependency === undefined); i++) {
|
|
77
|
+
dependency = registry[target];
|
|
78
|
+
await new Promise(r => setTimeout(r, 50));
|
|
79
|
+
}
|
|
80
|
+
if (dependency === null || dependency === undefined) reject(new Error("Dependency not found: " + target));
|
|
81
|
+
else {
|
|
82
|
+
dependency.loading.then(() => resolve(dependency)).catch(error => reject(error));
|
|
83
|
+
}
|
|
84
|
+
})
|
|
85
|
+
);
|
|
86
|
+
} else {
|
|
87
|
+
libraryPromises.push(RED.import(target).then(lib => {
|
|
88
|
+
modules[target] = lib.default;
|
|
89
|
+
}).catch(err => {
|
|
90
|
+
node.error(RED._("function.error.moduleLoadError",{module:module.spec, error:err.toString()}))
|
|
91
|
+
throw err;
|
|
92
|
+
}));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// endregion
|
|
96
|
+
|
|
97
|
+
node.loading = new Promise((resolve, reject) => {
|
|
98
|
+
Promise.all(libraryPromises).then(() => {
|
|
99
|
+
var context = vm.createContext(sandbox);
|
|
100
|
+
node.exports = context.exports;
|
|
101
|
+
|
|
102
|
+
context.require = (target) => {
|
|
103
|
+
if (target.startsWith("/subflow")) {
|
|
104
|
+
target = path + target.substring(9);
|
|
105
|
+
}
|
|
106
|
+
if (target.startsWith("#")) {
|
|
107
|
+
target = "/lib/" + target.substring(1);
|
|
108
|
+
}
|
|
109
|
+
if (target.startsWith("$/")) {
|
|
110
|
+
target = target.substring(1);
|
|
111
|
+
}
|
|
112
|
+
if (target.startsWith("./")) {
|
|
113
|
+
target = path + target.substring(2);
|
|
114
|
+
}
|
|
115
|
+
if (target.endsWith(".pkg")) {
|
|
116
|
+
target = target.substring(0, target.length - 4) + "/index";
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (target.startsWith("/")) {
|
|
120
|
+
return registry[target].exports;
|
|
121
|
+
}
|
|
122
|
+
return modules[target];
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
try {
|
|
126
|
+
var iniOpt = helper.createVMOpt(node, " setup");
|
|
127
|
+
var iniScript = helper.createScript(node, iniOpt, config.code);
|
|
128
|
+
context.__initSend__ = function(msgs) { node.send(msgs); };
|
|
129
|
+
promise = iniScript.runInContext(context, iniOpt)
|
|
130
|
+
.then(() => resolve(node))
|
|
131
|
+
.catch(e => {
|
|
132
|
+
node.error(e);
|
|
133
|
+
node.error(e.stack);
|
|
134
|
+
node.status({fill: 'red', shape: 'dot', text: 'error'})
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
node.on("close", function() {
|
|
138
|
+
while (node.outstandingTimers.length > 0) {
|
|
139
|
+
clearTimeout(node.outstandingTimers.pop());
|
|
140
|
+
}
|
|
141
|
+
while (node.outstandingIntervals.length > 0) {
|
|
142
|
+
clearInterval(node.outstandingIntervals.pop());
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
catch(err) {
|
|
147
|
+
node.error(err);
|
|
148
|
+
node.error(err.stack);
|
|
149
|
+
}
|
|
150
|
+
}).catch(error => reject(error))
|
|
151
|
+
}).catch(error => {
|
|
152
|
+
node.error(error);
|
|
153
|
+
node.error(error.stack);
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
RED.nodes.registerType("typescript", TypescriptInstance);
|
|
157
|
+
}
|