tune-sdk 0.3.4 → 0.3.6
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/dist/tune.js +18 -8
- package/package.json +1 -1
- package/src/contextws.js +11 -4
- package/src/man.js +2 -2
- package/src/ws.js +24 -1
package/dist/tune.js
CHANGED
|
@@ -1605,6 +1605,7 @@ async function text2ast(text, ctx, resolve) {
|
|
|
1605
1605
|
})(ctx.stack || []);
|
|
1606
1606
|
var resolved;
|
|
1607
1607
|
resolved = await ctx.resolve(name);
|
|
1608
|
+
if (!Array.isArray(resolved)) resolved = [resolved];
|
|
1608
1609
|
if (proc) {
|
|
1609
1610
|
while (pargs = proc.shift()) {
|
|
1610
1611
|
var pname;
|
|
@@ -1616,7 +1617,16 @@ async function text2ast(text, ctx, resolve) {
|
|
|
1616
1617
|
if (!p) throw new TuneError(("'" + pname + "' processor not found"), filename, row, col);
|
|
1617
1618
|
if ((typeof p.exec !== "function")) throw new TuneError(("'" + pname + "' does not have exec function"), filename, row, col);
|
|
1618
1619
|
try {
|
|
1619
|
-
|
|
1620
|
+
(function(it) {
|
|
1621
|
+
it = it.flat(Infinity);
|
|
1622
|
+
it = it.filter((function(item) {
|
|
1623
|
+
return (item !== "ignore");
|
|
1624
|
+
}));
|
|
1625
|
+
it = (resolved = it);
|
|
1626
|
+
return it;
|
|
1627
|
+
})(await Promise.all(resolved.map((function(r) {
|
|
1628
|
+
return p.exec.call(ctx, r, pargs[0], ctx);
|
|
1629
|
+
}))));
|
|
1620
1630
|
} catch (e) {
|
|
1621
1631
|
var err;
|
|
1622
1632
|
err = new TuneError(e.message, (p.fullname || p.name), undefined, undefined, [], e);
|
|
@@ -1629,7 +1639,7 @@ async function text2ast(text, ctx, resolve) {
|
|
|
1629
1639
|
}
|
|
1630
1640
|
}
|
|
1631
1641
|
}
|
|
1632
|
-
_ref0 =
|
|
1642
|
+
_ref0 = resolved.concat();
|
|
1633
1643
|
for (_i0 = 0, _len = _ref0.length; _i0 < _len; ++_i0) {
|
|
1634
1644
|
resolved = _ref0[_i0];
|
|
1635
1645
|
if (!resolved) throw new TuneError(("'" + name + "' not found"), filename, row, col);
|
|
@@ -2290,7 +2300,7 @@ function text2run(text, ctx, opts) {
|
|
|
2290
2300
|
}
|
|
2291
2301
|
text2run;
|
|
2292
2302
|
async function file2run(args, params, ctx) {
|
|
2293
|
-
var lctx, text, stop, errors, turnsSaved, node, longFormatRegex, isLong, initialText, response, res, r, chunk,
|
|
2303
|
+
var lctx, text, stop, errors, turnsSaved, node, longFormatRegex, isLong, initialText, response, res, r, chunk, itergXF7bT2, _ref;
|
|
2294
2304
|
var lctx;
|
|
2295
2305
|
lctx = ctx.clone();
|
|
2296
2306
|
if (params) lctx.ms.unshift(envmd(params));
|
|
@@ -2382,7 +2392,7 @@ async function file2run(args, params, ctx) {
|
|
|
2382
2392
|
hookTurnEnd: save
|
|
2383
2393
|
});
|
|
2384
2394
|
chunk = {};
|
|
2385
|
-
|
|
2395
|
+
itergXF7bT2 = new AsyncIter();
|
|
2386
2396
|
(async function($lastRes) {
|
|
2387
2397
|
var _ref;
|
|
2388
2398
|
try {
|
|
@@ -2390,20 +2400,20 @@ async function file2run(args, params, ctx) {
|
|
|
2390
2400
|
chunk = await r.next();
|
|
2391
2401
|
res = (chunk.value || "");
|
|
2392
2402
|
$lastRes = transformOutput(res) || $lastRes;
|
|
2393
|
-
|
|
2403
|
+
itergXF7bT2.result = {
|
|
2394
2404
|
value: $lastRes
|
|
2395
2405
|
}
|
|
2396
2406
|
}
|
|
2397
|
-
_ref =
|
|
2407
|
+
_ref = itergXF7bT2.result = {
|
|
2398
2408
|
value: $lastRes,
|
|
2399
2409
|
done: true
|
|
2400
2410
|
}
|
|
2401
2411
|
} catch (e) {
|
|
2402
|
-
_ref = (
|
|
2412
|
+
_ref = (itergXF7bT2.err = e);
|
|
2403
2413
|
}
|
|
2404
2414
|
return _ref;
|
|
2405
2415
|
})();
|
|
2406
|
-
_ref =
|
|
2416
|
+
_ref = itergXF7bT2;
|
|
2407
2417
|
}
|
|
2408
2418
|
return _ref;
|
|
2409
2419
|
}
|
package/package.json
CHANGED
package/src/contextws.js
CHANGED
|
@@ -72,7 +72,6 @@ function ContextWebsocket(url, { debug } = {}) {
|
|
|
72
72
|
assert(typeof url !== 'undefined', 'url is not set');
|
|
73
73
|
|
|
74
74
|
socket = new WebSocket(url);
|
|
75
|
-
console.log(socket)
|
|
76
75
|
|
|
77
76
|
this.socket = socket;
|
|
78
77
|
this.callbacks = Object.create(null);
|
|
@@ -105,7 +104,7 @@ ContextWebsocket.prototype._onClose = function () {
|
|
|
105
104
|
|
|
106
105
|
ContextWebsocket.prototype._send = async function (payload) {
|
|
107
106
|
const data = JSON.stringify(payload);
|
|
108
|
-
this.debug('==>', data);
|
|
107
|
+
this.debug('tune ==>', data);
|
|
109
108
|
await this.ready;
|
|
110
109
|
this.socket.send(data);
|
|
111
110
|
};
|
|
@@ -135,7 +134,7 @@ ContextWebsocket.prototype._onMessage = function (event) {
|
|
|
135
134
|
return;
|
|
136
135
|
}
|
|
137
136
|
|
|
138
|
-
this.debug('<==', msg);
|
|
137
|
+
this.debug('tune <==', msg);
|
|
139
138
|
if (!msg || !msg.id) return;
|
|
140
139
|
|
|
141
140
|
const cb = this.callbacks[msg.id];
|
|
@@ -176,6 +175,7 @@ ContextWebsocket.prototype.read = function (name, binary) {
|
|
|
176
175
|
return this._call('read', [name, binary], false);
|
|
177
176
|
};
|
|
178
177
|
|
|
178
|
+
|
|
179
179
|
ContextWebsocket.prototype.write = function (name, content) {
|
|
180
180
|
return this._call('write', [name, content], false);
|
|
181
181
|
};
|
|
@@ -197,5 +197,12 @@ if (typeof module !== 'undefined' && module.exports) {
|
|
|
197
197
|
module.exports = ContextWebsocket;
|
|
198
198
|
} else {
|
|
199
199
|
window.ContextWebsocket = ContextWebsocket;
|
|
200
|
-
window.ctx = new ContextWebsocket(window.location.origin.replace(/^http/, 'ws'));
|
|
200
|
+
window.ctx = new ContextWebsocket(window.location.origin.replace(/^http/, 'ws'), { debug: console.debug });
|
|
201
|
+
window.addEventListener('error', async (event) => {
|
|
202
|
+
window.ctx._call('log', [event.error?.stack || event.message ], false);
|
|
203
|
+
})
|
|
204
|
+
window.addEventListener('unhandledrejection', async (event) => {
|
|
205
|
+
const err = event.reason instanceof Error ? event.reason : new Error(String(event.reason));
|
|
206
|
+
window.ctx._call('log', [err.stack || err.message], false);
|
|
207
|
+
});
|
|
201
208
|
}
|
package/src/man.js
CHANGED
|
@@ -66,7 +66,7 @@ module.exports = ({ mount } = {}) => {
|
|
|
66
66
|
type: "text",
|
|
67
67
|
name: `${mount}/`,
|
|
68
68
|
source: "docs",
|
|
69
|
-
read: async () => mans.map(man => `${man.name} - ${man.description}`).join("\n")
|
|
69
|
+
read: async () => mans.map(man => `${mount}/${man.name} - ${man.description}`).join("\n")
|
|
70
70
|
})
|
|
71
71
|
}
|
|
72
72
|
|
|
@@ -108,7 +108,7 @@ module.exports = ({ mount } = {}) => {
|
|
|
108
108
|
return {
|
|
109
109
|
type: "text",
|
|
110
110
|
name,
|
|
111
|
-
read: async () => mans.map(man => `${man.name} - ${man.description}`).join("\n")
|
|
111
|
+
read: async () => mans.map(man => `${mount}/${man.name} - ${man.description}`).join("\n")
|
|
112
112
|
}
|
|
113
113
|
}
|
|
114
114
|
|
package/src/ws.js
CHANGED
|
@@ -1,9 +1,27 @@
|
|
|
1
1
|
const http = require('http');
|
|
2
2
|
const fs = require('fs');
|
|
3
3
|
const path = require('path');
|
|
4
|
+
const os = require('os');
|
|
4
5
|
const WebSocket = require('ws');
|
|
5
6
|
const mime = require('mime-types');
|
|
6
7
|
|
|
8
|
+
// skips loopback, docker (172.x, 192.168.x docker ranges), and common vpn (tun/tap) interfaces
|
|
9
|
+
function getPublicIPv4() {
|
|
10
|
+
const ifaces = os.networkInterfaces();
|
|
11
|
+
const skipNames = /^(docker|br-|veth|tun|tap|vmnet|vbox)/i;
|
|
12
|
+
const skipRanges = /^(172\.(1[6-9]|2\d|3[01])\.|10\.)/; // docker bridge ranges
|
|
13
|
+
|
|
14
|
+
for (const [name, addrs] of Object.entries(ifaces)) {
|
|
15
|
+
if (skipNames.test(name)) continue;
|
|
16
|
+
for (const addr of addrs) {
|
|
17
|
+
if (addr.family === 'IPv4' && !addr.internal && !skipRanges.test(addr.address)) {
|
|
18
|
+
return addr.address;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return 'localhost';
|
|
23
|
+
}
|
|
24
|
+
|
|
7
25
|
function makeServer({ port, static, root, ctx }) {
|
|
8
26
|
root = path.join(root || process.cwd());
|
|
9
27
|
|
|
@@ -46,6 +64,7 @@ function makeServer({ port, static, root, ctx }) {
|
|
|
46
64
|
let lctx = ctx.clone()
|
|
47
65
|
let { id, method, args, ...rest } = JSON.parse(msg.toString());
|
|
48
66
|
let result = { id }
|
|
67
|
+
console.log("[context]", method, ...args)
|
|
49
68
|
if (method === "read") {
|
|
50
69
|
const [ name, binary ] = args
|
|
51
70
|
let content = await lctx.read(name, binary)
|
|
@@ -104,6 +123,9 @@ function makeServer({ port, static, root, ctx }) {
|
|
|
104
123
|
}
|
|
105
124
|
})()
|
|
106
125
|
}
|
|
126
|
+
} else if (method === "log") {
|
|
127
|
+
console.log("[browser]:", ...args)
|
|
128
|
+
result.done = true
|
|
107
129
|
} else {
|
|
108
130
|
result.error = { message: `method not found ${method}`}
|
|
109
131
|
}
|
|
@@ -115,7 +137,8 @@ function makeServer({ port, static, root, ctx }) {
|
|
|
115
137
|
if (Number.isNaN(parseInt(port))) {
|
|
116
138
|
console.log(`listening ${port}`);
|
|
117
139
|
} else {
|
|
118
|
-
|
|
140
|
+
const host = getPublicIPv4();
|
|
141
|
+
console.log(`listening http://${host}:${port}`);
|
|
119
142
|
}
|
|
120
143
|
});
|
|
121
144
|
}
|