-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherror.js
More file actions
178 lines (144 loc) · 4.04 KB
/
Copy patherror.js
File metadata and controls
178 lines (144 loc) · 4.04 KB
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
178
/* eslint-disable */
const test = require('ava')
const isolatedFunction = require('..')
const [nodeMajor] = process.version.slice(1).split('.').map(Number)
test('throw an error if snippet is not a function or string', t => {
t.throws(
() => {
isolatedFunction(NaN)
},
{ message: 'Expected a function' }
)
})
test('throw code errors by default', async t => {
const [fn, cleanup] = isolatedFunction(() => {
throw new TypeError('oops')
})
t.teardown(cleanup)
await t.throwsAsync(fn(), { message: 'oops' })
})
test('pass `throwError: false`', async t => {
{
const [fn, cleanup] = isolatedFunction(
() => {
throw new TypeError('oops')
},
{ throwError: false }
)
t.teardown(cleanup)
const result = await fn()
t.is(result.isFulfilled, false)
t.is(result.value.message, 'oops')
t.is(result.value.name, 'TypeError')
t.is(typeof result.profiling, 'object')
}
{
const [fn, cleanup] = isolatedFunction(
() => {
throw 'oops'
},
{ throwError: false }
)
t.teardown(cleanup)
const result = await fn()
t.is(result.isFulfilled, false)
t.is(result.value.message, '"oops"')
t.is(result.value.name, 'NonError')
t.is(typeof result.profiling, 'object')
}
})
test('handle timeout', async t => {
const [fn, cleanup] = isolatedFunction(
() => {
let i = 0
while (true) {
i += 1
}
},
{ timeout: 100 }
)
t.teardown(cleanup)
const error = await t.throwsAsync(fn())
t.is(error.message, 'Execution timed out')
t.is(typeof error.profiling.duration, 'number')
})
test('handle OOM', async t => {
const [fn, cleanup] = isolatedFunction(
() => {
const storage = []
const twoMegabytes = 1024 * 1024 * 2
while (true) {
const array = new Uint8Array(twoMegabytes)
for (let ii = 0; ii < twoMegabytes; ii += 4096) {
array[ii] = 1 // we have to put something in the array to flush to real memory
}
storage.push(array)
}
},
{ memory: 1 }
)
t.teardown(cleanup)
const error = await t.throwsAsync(fn())
t.is(error.message, 'Out of memory')
t.is(typeof error.profiling.duration, 'number')
})
test('handle filesystem permissions', async t => {
{
const [fn, cleanup] = isolatedFunction(() => {
const fs = require('fs')
fs.readFileSync('/etc/passwd', 'utf8')
})
t.teardown(cleanup)
const error = await t.throwsAsync(fn())
t.is(error.message, "Access to 'FileSystemRead' has been restricted")
}
{
const [fn, cleanup] = isolatedFunction(() => {
const fs = require('fs')
fs.writeFileSync('/etc/passwd', 'foo')
})
t.teardown(cleanup)
const error = await t.throwsAsync(fn())
t.is(error.message, "Access to 'FileSystemWrite' has been restricted")
}
})
test('handle child process', async t => {
{
const [fn, cleanup] = isolatedFunction(() => {
const { execSync } = require('child_process')
return execSync('echo hello').toString()
})
t.teardown(cleanup)
const error = await t.throwsAsync(fn())
t.is(error.message, "Access to 'ChildProcess' has been restricted")
}
})
;(nodeMajor >= 25 ? test : test.skip)('handle network access', async t => {
{
const [fn, cleanup] = isolatedFunction(async () => {
function doFetch (url) {
return new Promise((resolve, reject) => {
const req = require('node:http').get(url, res => {
let data = ''
res.on('data', chunk => {
data += chunk
})
res.on('end', () => {
resolve({
statusCode: res.statusCode,
headers: res.headers,
body: data
})
})
})
req.on('error', reject)
})
}
const { statusCode } = await doFetch('http://example.com')
return statusCode
})
t.teardown(cleanup)
const error = await t.throwsAsync(fn())
t.is(error.message, "Access to 'network' has been restricted")
}
})