Skip to content

Commit 318a4bf

Browse files
committed
chore: debug
1 parent 5246121 commit 318a4bf

2 files changed

Lines changed: 52 additions & 27 deletions

File tree

src/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module.exports = (snippet, { tmpdir, timeout, memory, throwError = true, allow =
3232

3333
const fn = async (...args) => {
3434
let duration
35+
let _stdout
3536
try {
3637
const { content, cleanupPromise } = await compilePromise
3738

@@ -46,6 +47,7 @@ module.exports = (snippet, { tmpdir, timeout, memory, throwError = true, allow =
4647
})
4748
Readable.from(content).pipe(subprocess.stdin)
4849
const [{ stdout }] = await Promise.all([subprocess, cleanupPromise])
50+
_stdout = stdout
4951
const { isFulfilled, value, profiling, logging } = JSON.parse(stdout)
5052
profiling.duration = duration()
5153
debug('node', {
@@ -61,6 +63,12 @@ module.exports = (snippet, { tmpdir, timeout, memory, throwError = true, allow =
6163
})()
6264
: { isFulfilled: false, value: deserializeError(value), profiling, logging }
6365
} catch (error) {
66+
console.log('=========== DEBUG ===========')
67+
console.log('error', error)
68+
console.log('args', JSON.stringify(args))
69+
console.log('_stdout', JSON.stringify(_stdout))
70+
71+
console.log('=========== DEBUG ===========')
6472
if (error.signalCode === 'SIGTRAP') {
6573
throw createError({
6674
name: 'MemoryError',

src/template/index.js

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,52 @@ const SERIALIZE_ERROR = require('./serialize-error')
44

55
module.exports = snippet => `
66
;(async () => {
7-
const args = JSON.parse(process.argv[2])
7+
try {
8+
const args = JSON.parse(process.argv[2])
89
9-
/* https://github.com/Kikobeats/null-prototype-object */
10-
const logging = new (/* @__PURE__ */ (() => { let e = function(){}; return e.prototype = Object.create(null), Object.freeze(e.prototype), e })());
10+
/* https://github.com/Kikobeats/null-prototype-object */
11+
const logging = new (/* @__PURE__ */ (() => { let e = function(){}; return e.prototype = Object.create(null), Object.freeze(e.prototype), e })());
1112
12-
for (const method of ['log', 'info', 'debug', 'warn', 'error']) {
13-
console[method] = function (...args) {
14-
logging[method] === undefined ? logging[method] = [args] : logging[method].push(args)
13+
for (const method of ['log', 'info', 'debug', 'warn', 'error']) {
14+
console[method] = function (...args) {
15+
logging[method] === undefined ? logging[method] = [args] : logging[method].push(args)
16+
}
1517
}
16-
}
1718
18-
;await (async (send) => {
19-
process.stdout.write = function () {}
20-
let value
21-
let isFulfilled
19+
;await (async (send) => {
20+
process.stdout.write = function () {}
21+
let value
22+
let isFulfilled
2223
23-
try {
24-
value = await (${snippet.toString()})(...args)
25-
isFulfilled = true
26-
} catch (error) {
27-
value = ${SERIALIZE_ERROR}(error)
28-
isFulfilled = false
29-
} finally {
30-
send(JSON.stringify({
31-
isFulfilled,
32-
logging,
33-
value,
34-
profiling: { memory: process.memoryUsage().rss }
35-
}))
36-
}
37-
})(process.stdout.write.bind(process.stdout))
38-
})()`
24+
try {
25+
value = await (${snippet.toString()})(...args)
26+
isFulfilled = true
27+
} catch (error) {
28+
value = ${SERIALIZE_ERROR}(error)
29+
isFulfilled = false
30+
} finally {
31+
send(JSON.stringify({
32+
isFulfilled,
33+
logging,
34+
value,
35+
profiling: { memory: process.memoryUsage().rss }
36+
}))
37+
}
38+
})(process.stdout.write.bind(process.stdout))
39+
} catch (error) {
40+
process.stdout.write(JSON.stringify({
41+
isFulfilled: false,
42+
logging: {},
43+
value: ${SERIALIZE_ERROR}(error),
44+
profiling: { memory: process.memoryUsage().rss }
45+
}))
46+
}
47+
})().catch(err => {
48+
process.stdout.write(JSON.stringify({
49+
isFulfilled: false,
50+
logging: {},
51+
value: ${SERIALIZE_ERROR}(err),
52+
profiling: { memory: process.memoryUsage().rss }
53+
}))
54+
process.exitCode = 1
55+
})`

0 commit comments

Comments
 (0)