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 ADDED
@@ -0,0 +1 @@
1
+
package/helper.js ADDED
@@ -0,0 +1,216 @@
1
+ var ts = require("./typescript.js");
2
+ var util = require("util");
3
+ var vm = require("vm");
4
+
5
+ function sendResults(node,send,_msgid,msgs,cloneFirstMessage) {
6
+ if (msgs == null) {
7
+ return;
8
+ } else if (!util.isArray(msgs)) {
9
+ msgs = [msgs];
10
+ }
11
+ var msgCount = 0;
12
+ for (var m=0; m<msgs.length; m++) {
13
+ if (msgs[m]) {
14
+ if (!util.isArray(msgs[m])) {
15
+ msgs[m] = [msgs[m]];
16
+ }
17
+ for (var n=0; n < msgs[m].length; n++) {
18
+ var msg = msgs[m][n];
19
+ if (msg !== null && msg !== undefined) {
20
+ if (typeof msg === 'object' && !Buffer.isBuffer(msg) && !util.isArray(msg)) {
21
+ if (msgCount === 0 && cloneFirstMessage !== false) {
22
+ msgs[m][n] = RED.util.cloneMessage(msgs[m][n]);
23
+ msg = msgs[m][n];
24
+ }
25
+ msg._msgid = _msgid;
26
+ msgCount++;
27
+ } else {
28
+ var type = typeof msg;
29
+ if (type === 'object') {
30
+ type = Buffer.isBuffer(msg)?'Buffer':(util.isArray(msg)?'Array':'Date');
31
+ }
32
+ node.error(RED._("function.error.non-message-returned",{ type: type }));
33
+ }
34
+ }
35
+ }
36
+ }
37
+ }
38
+ if (msgCount>0) {
39
+ send(msgs);
40
+ }
41
+ }
42
+
43
+ function createVMOpt(node, kind) {
44
+ var opt = {
45
+ filename: 'Function node'+kind+':'+node.id+(node.name?' ['+node.name+']':''), // filename for stack traces
46
+ displayErrors: true
47
+ // Using the following options causes node 4/6 to not include the line number
48
+ // in the stack output. So don't use them.
49
+ // lineOffset: -11, // line number offset to be used for stack traces
50
+ // columnOffset: 0, // column number offset to be used for stack traces
51
+ };
52
+ return opt;
53
+ }
54
+
55
+ function createSandbox(RED, node) {
56
+ node.outstandingTimers = [];
57
+ node.outstandingIntervals = [];
58
+
59
+ var sandbox = {
60
+ exports: {},
61
+ console:console,
62
+ util:util,
63
+ Buffer:Buffer,
64
+ Date: Date,
65
+ RED: {
66
+ util: RED.util
67
+ },
68
+ node: {
69
+ id: node.id,
70
+ name: node.name,
71
+ path: node._path,
72
+ outputCount: node.outputs,
73
+ log: function() {
74
+ node.log.apply(node, arguments);
75
+ },
76
+ error: function() {
77
+ node.error.apply(node, arguments);
78
+ },
79
+ warn: function() {
80
+ node.warn.apply(node, arguments);
81
+ },
82
+ debug: function() {
83
+ node.debug.apply(node, arguments);
84
+ },
85
+ trace: function() {
86
+ node.trace.apply(node, arguments);
87
+ },
88
+ send: function(send, id, msgs, cloneMsg) {
89
+ sendResults(node, send, id, msgs, cloneMsg);
90
+ },
91
+ on: function() {
92
+ node.on.apply(node, arguments);
93
+ },
94
+ status: function() {
95
+ node.status.apply(node, arguments);
96
+ },
97
+ send: function(msg) {
98
+ node.send(msg);
99
+ }
100
+ },
101
+ context: {
102
+ set: function() {
103
+ node.context().set.apply(node,arguments);
104
+ },
105
+ get: function() {
106
+ return node.context().get.apply(node,arguments);
107
+ },
108
+ keys: function() {
109
+ return node.context().keys.apply(node,arguments);
110
+ },
111
+ get global() {
112
+ return node.context().global;
113
+ },
114
+ get flow() {
115
+ return node.context().flow;
116
+ }
117
+ },
118
+ flow: {
119
+ set: function() {
120
+ node.context().flow.set.apply(node,arguments);
121
+ },
122
+ get: function() {
123
+ return node.context().flow.get.apply(node,arguments);
124
+ },
125
+ keys: function() {
126
+ return node.context().flow.keys.apply(node,arguments);
127
+ }
128
+ },
129
+ global: {
130
+ set: function() {
131
+ node.context().global.set.apply(node,arguments);
132
+ },
133
+ get: function() {
134
+ return node.context().global.get.apply(node,arguments);
135
+ },
136
+ keys: function() {
137
+ return node.context().global.keys.apply(node,arguments);
138
+ }
139
+ },
140
+ env: {
141
+ get: function(envVar) {
142
+ return RED.util.getSetting(node, envVar);
143
+ }
144
+ },
145
+ setTimeout: function () {
146
+ var func = arguments[0];
147
+ var timerId;
148
+ arguments[0] = function() {
149
+ sandbox.clearTimeout(timerId);
150
+ try {
151
+ func.apply(node,arguments);
152
+ } catch(err) {
153
+ node.error(err,{});
154
+ }
155
+ };
156
+ timerId = setTimeout.apply(node,arguments);
157
+ node.outstandingTimers.push(timerId);
158
+ return timerId;
159
+ },
160
+ clearTimeout: function(id) {
161
+ clearTimeout(id);
162
+ var index = node.outstandingTimers.indexOf(id);
163
+ if (index > -1) {
164
+ node.outstandingTimers.splice(index,1);
165
+ }
166
+ },
167
+ setInterval: function() {
168
+ var func = arguments[0];
169
+ var timerId;
170
+ arguments[0] = function() {
171
+ try {
172
+ func.apply(node,arguments);
173
+ } catch(err) {
174
+ node.error(err,{});
175
+ }
176
+ };
177
+ timerId = setInterval.apply(node,arguments);
178
+ node.outstandingIntervals.push(timerId);
179
+ return timerId;
180
+ },
181
+ clearInterval: function(id) {
182
+ clearInterval(id);
183
+ var index = node.outstandingIntervals.indexOf(id);
184
+ if (index > -1) {
185
+ node.outstandingIntervals.splice(index,1);
186
+ }
187
+ }
188
+ };
189
+ if (util.hasOwnProperty('promisify')) {
190
+ sandbox.setTimeout[util.promisify.custom] = function(after, value) {
191
+ return new Promise(function(resolve, reject) {
192
+ sandbox.setTimeout(function(){ resolve(value); }, after);
193
+ });
194
+ };
195
+ sandbox.promisify = util.promisify;
196
+ }
197
+
198
+ return sandbox;
199
+ }
200
+
201
+ function createScript(node, iniOpt, code) {
202
+ //console.log(ts.transpile(code));
203
+ var iniText = `
204
+ (async function(__send__) {
205
+ node.status({});
206
+ ${ts.transpile(code)}
207
+ })();`;
208
+ return new vm.Script(iniText, iniOpt);
209
+ }
210
+
211
+ module.exports = {
212
+ createSandbox: createSandbox,
213
+ createScript: createScript,
214
+ createVMOpt: createVMOpt,
215
+ transpile: ts.transpile
216
+ };
@@ -0,0 +1,230 @@
1
+ var ts = require("./typescript.js");
2
+ var util = require("util");
3
+ var vm = require("vm");
4
+
5
+ function sendResults(node,send,_msgid,msgs,cloneFirstMessage) {
6
+ if (msgs == null) {
7
+ return;
8
+ } else if (!util.isArray(msgs)) {
9
+ msgs = [msgs];
10
+ }
11
+ var msgCount = 0;
12
+ for (var m=0; m<msgs.length; m++) {
13
+ if (msgs[m]) {
14
+ if (!util.isArray(msgs[m])) {
15
+ msgs[m] = [msgs[m]];
16
+ }
17
+ for (var n=0; n < msgs[m].length; n++) {
18
+ var msg = msgs[m][n];
19
+ if (msg !== null && msg !== undefined) {
20
+ if (typeof msg === 'object' && !Buffer.isBuffer(msg) && !util.isArray(msg)) {
21
+ if (msgCount === 0 && cloneFirstMessage !== false) {
22
+ msgs[m][n] = RED.util.cloneMessage(msgs[m][n]);
23
+ msg = msgs[m][n];
24
+ }
25
+ msg._msgid = _msgid;
26
+ msgCount++;
27
+ } else {
28
+ var type = typeof msg;
29
+ if (type === 'object') {
30
+ type = Buffer.isBuffer(msg)?'Buffer':(util.isArray(msg)?'Array':'Date');
31
+ }
32
+ node.error(RED._("function.error.non-message-returned",{ type: type }));
33
+ }
34
+ }
35
+ }
36
+ }
37
+ }
38
+ if (msgCount>0) {
39
+ send(msgs);
40
+ }
41
+ }
42
+
43
+ function createVMOpt(node, kind) {
44
+ var opt = {
45
+ filename: 'Function node'+kind+':'+node.id+(node.name?' ['+node.name+']':''), // filename for stack traces
46
+ displayErrors: true
47
+ // Using the following options causes node 4/6 to not include the line number
48
+ // in the stack output. So don't use them.
49
+ // lineOffset: -11, // line number offset to be used for stack traces
50
+ // columnOffset: 0, // column number offset to be used for stack traces
51
+ };
52
+ return opt;
53
+ }
54
+
55
+ function sendDataToClient2(RED, node, data) {
56
+ try {
57
+ RED.comms.publish(
58
+ "typescript-log-" + node.id,
59
+ {
60
+ id: node.id,
61
+ data: data
62
+ },
63
+ true
64
+ );
65
+ }
66
+ catch (e) {
67
+ console.log(e);
68
+ }
69
+ }
70
+
71
+ function createSandbox(RED, node) {
72
+ node.outstandingTimers = [];
73
+ node.outstandingIntervals = [];
74
+
75
+ var sandbox = {
76
+ exports: {},
77
+ console:console,
78
+ util:util,
79
+ Buffer:Buffer,
80
+ Date: Date,
81
+ RED: {
82
+ util: RED.util
83
+ },
84
+ node: {
85
+ id: node.id,
86
+ name: node.name,
87
+ path: node._path,
88
+ outputCount: node.outputs,
89
+ log: (data) => sendDataToClient2(RED, node, data),
90
+ error: function() {
91
+ node.error.apply(node, arguments);
92
+ },
93
+ warn: function() {
94
+ node.warn.apply(node, arguments);
95
+ },
96
+ debug: function() {
97
+ node.debug.apply(node, arguments);
98
+ },
99
+ trace: function() {
100
+ node.trace.apply(node, arguments);
101
+ },
102
+ send: function(send, id, msgs, cloneMsg) {
103
+ sendResults(node, send, id, msgs, cloneMsg);
104
+ },
105
+ on: function() {
106
+ node.on.apply(node, arguments);
107
+ },
108
+ status: function() {
109
+ node.status.apply(node, arguments);
110
+ },
111
+ send: function(msg) {
112
+ node.send(msg);
113
+ }
114
+ },
115
+ context: {
116
+ set: function() {
117
+ node.context().set.apply(node,arguments);
118
+ },
119
+ get: function() {
120
+ return node.context().get.apply(node,arguments);
121
+ },
122
+ keys: function() {
123
+ return node.context().keys.apply(node,arguments);
124
+ },
125
+ get global() {
126
+ return node.context().global;
127
+ },
128
+ get flow() {
129
+ return node.context().flow;
130
+ }
131
+ },
132
+ flow: {
133
+ set: function() {
134
+ node.context().flow.set.apply(node,arguments);
135
+ },
136
+ get: function() {
137
+ return node.context().flow.get.apply(node,arguments);
138
+ },
139
+ keys: function() {
140
+ return node.context().flow.keys.apply(node,arguments);
141
+ }
142
+ },
143
+ global: {
144
+ set: function() {
145
+ node.context().global.set.apply(node,arguments);
146
+ },
147
+ get: function() {
148
+ return node.context().global.get.apply(node,arguments);
149
+ },
150
+ keys: function() {
151
+ return node.context().global.keys.apply(node,arguments);
152
+ }
153
+ },
154
+ env: {
155
+ get: function(envVar) {
156
+ return RED.util.getSetting(node, envVar);
157
+ }
158
+ },
159
+ setTimeout: function () {
160
+ var func = arguments[0];
161
+ var timerId;
162
+ arguments[0] = function() {
163
+ sandbox.clearTimeout(timerId);
164
+ try {
165
+ func.apply(node,arguments);
166
+ } catch(err) {
167
+ node.error(err,{});
168
+ }
169
+ };
170
+ timerId = setTimeout.apply(node,arguments);
171
+ node.outstandingTimers.push(timerId);
172
+ return timerId;
173
+ },
174
+ clearTimeout: function(id) {
175
+ clearTimeout(id);
176
+ var index = node.outstandingTimers.indexOf(id);
177
+ if (index > -1) {
178
+ node.outstandingTimers.splice(index,1);
179
+ }
180
+ },
181
+ setInterval: function() {
182
+ var func = arguments[0];
183
+ var timerId;
184
+ arguments[0] = function() {
185
+ try {
186
+ func.apply(node,arguments);
187
+ } catch(err) {
188
+ node.error(err,{});
189
+ }
190
+ };
191
+ timerId = setInterval.apply(node,arguments);
192
+ node.outstandingIntervals.push(timerId);
193
+ return timerId;
194
+ },
195
+ clearInterval: function(id) {
196
+ clearInterval(id);
197
+ var index = node.outstandingIntervals.indexOf(id);
198
+ if (index > -1) {
199
+ node.outstandingIntervals.splice(index,1);
200
+ }
201
+ }
202
+ };
203
+ if (util.hasOwnProperty('promisify')) {
204
+ sandbox.setTimeout[util.promisify.custom] = function(after, value) {
205
+ return new Promise(function(resolve, reject) {
206
+ sandbox.setTimeout(function(){ resolve(value); }, after);
207
+ });
208
+ };
209
+ sandbox.promisify = util.promisify;
210
+ }
211
+
212
+ return sandbox;
213
+ }
214
+
215
+ function createScript(node, iniOpt, code) {
216
+ //console.log(ts.transpile(code));
217
+ var iniText = `
218
+ (async function(__send__) {
219
+ node.status({});
220
+ ${ts.transpile(code)}
221
+ })();`;
222
+ return new vm.Script(iniText, iniOpt);
223
+ }
224
+
225
+ module.exports = {
226
+ createSandbox: createSandbox,
227
+ createScript: createScript,
228
+ createVMOpt: createVMOpt,
229
+ transpile: ts.transpile
230
+ };
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "node-red-contrib-typescript",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "author": "kezziny",
6
+ "license": "ISC",
7
+ "node-red" : {
8
+ "nodes": {
9
+ "instance": "src/instance/instance.js"
10
+ }
11
+ }
12
+ }