This repository has been archived by the owner on May 5, 2023. It is now read-only.
/
test.ts
177 lines (170 loc) · 4.41 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import fs from 'fs';
import path from 'path';
import assert from 'assert';
import degenerator, { compile } from '../src';
describe('degenerator()', () => {
it('should support "async" output functions', () => {
function aPlusB(a: () => string, b: () => string): string {
return a() + b();
}
const compiled = degenerator('' + aPlusB, ['a']);
assert.equal(
compiled.replace(/\s+/g, ' '),
'async function aPlusB(a, b) { return await a() + b(); }'
);
});
it('should be the default "output" mode (without options)', () => {
function foo(a: () => string): string {
return a();
}
const compiled = degenerator('' + foo, ['a']);
assert.equal(
compiled.replace(/\s+/g, ' '),
'async function foo(a) { return await a(); }'
);
});
describe('"expected" fixture tests', () => {
fs.readdirSync(__dirname)
.sort()
.forEach((n) => {
if (n === 'test.js') return;
if (/\.expected\.js$/.test(n)) return;
if (/\.ts$/.test(n)) return;
if (/\.map/.test(n)) return;
const expectedName = `${path.basename(n, '.js')}.expected.js`;
it(`${n} → ${expectedName}`, function () {
const sourceName = path.resolve(__dirname, n);
const compiledName = path.resolve(__dirname, expectedName);
const js = fs.readFileSync(sourceName, 'utf8');
const expected = fs.readFileSync(compiledName, 'utf8');
// the test case can define the `names` to use as a
// comment on the first line of the file
const m = js.match(/\/\/\s*(.*)/);
let names;
if (m) {
// the comment should be a comma-separated list of function names
names = m[1].split(/,\s*/);
} else {
// if no function names were passed in then convert them all
names = [/.*/];
}
const compiled = degenerator(js, names);
assert.equal(
compiled.trim().replace(/\r/g, ''),
expected.trim().replace(/\r/g, '')
);
});
});
});
describe('`compile()`', () => {
it('should compile code into an invocable async function', async () => {
const a = (v: string) => Promise.resolve(v);
const b = () => 'b';
function aPlusB(v: string): string {
return a(v) + b();
}
const fn = compile<string, [string]>('' + aPlusB, 'aPlusB', ['a'], {
sandbox: { a, b },
});
const val = await fn('c');
assert.equal(val, 'cb');
});
it('should contain the compiled code in `toString()` output', () => {
const a = () => 'a';
const b = () => 'b';
function aPlusB(): string {
return a() + b();
}
const fn = compile<() => Promise<string>>(
'' + aPlusB,
'aPlusB',
['b'],
{
sandbox: { a, b },
}
);
assert(/await b\(\)/.test(fn + ''));
});
it('should be able to await non-promises', () => {
const a = () => 'a';
const b = () => 'b';
function aPlusB(): string {
return a() + b();
}
const fn = compile<() => Promise<string>>(
'' + aPlusB,
'aPlusB',
['a'],
{
sandbox: { a, b },
}
);
return fn().then((val) => {
assert.equal(val, 'ab');
});
});
it('should be able to compile functions with no async', () => {
const a = () => 'a';
const b = () => 'b';
function aPlusB(): string {
return a() + b();
}
const fn = compile<string>('' + aPlusB, 'aPlusB', [], {
sandbox: { a, b },
});
return fn().then((val: string) => {
assert.equal(val, 'ab');
});
});
it('should throw an Error if no function is returned from the `vm`', () => {
let err;
try {
compile<() => Promise<string>>('const foo = 1', 'foo', []);
} catch (_err) {
err = _err;
}
assert(err);
assert.equal(
err.message,
'Expected a "function" to be returned for `foo`, but got "number"'
);
});
it('should be compile if branches', () => {
function ifA(): string {
if (a()) {
return 'foo';
}
return 'bar';
}
function a() {
if (b()) {
return false;
}
return true;
}
function b() {
return false;
}
const fn = compile<string>(`${ifA};${a}`, 'ifA', ['b'], {
sandbox: { b },
});
return fn().then((val: string) => {
assert.equal(val, 'foo');
});
});
it('should prevent privilege escalation of untrusted code', async() => {
let err;
try {
const fn = compile<typeof process>(
`const f = this.constructor.constructor('return process');`,
'f',
[],
);
await fn();
} catch(_err) {
err = _err;
}
assert.equal(err.message,'process is not defined')
});
});
});