Skip to content

Commit

Permalink
feat: remove whitespace before stack trace lines
Browse files Browse the repository at this point in the history
  • Loading branch information
stfsy committed Dec 4, 2023
1 parent 23990e1 commit 4507068
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ const result = await executor('process.exit(0)')
// "message": "process is not defined",
// "stack": [
// "ReferenceError: process is not defined",
// " at userSuppliedScript (file:///user-supplied-script.js:1:1)",
// " at runtime.js:38:24"
// "at userSuppliedScript (file:///user-supplied-script.js:1:1)",
// "at runtime.js:38:24"
// ]
// },
// "durationMillis": 1
Expand All @@ -165,9 +165,9 @@ eval(1+1)
// "message": "\"eval\" is not allowed in this context.",
// "stack": [
// "Error: \"eval\" is not allowed in this context.",
// " at global.<computed> (file:///code-generation.js:1:19)",
// " at userSuppliedScript (file:///user-supplied-script.js:2:9)",
// " at runtime.js:38:24"
// "at global.<computed> (file:///code-generation.js:1:19)",
// "at userSuppliedScript (file:///user-supplied-script.js:2:9)",
// "at runtime.js:38:24"
// ]
// },
// "durationMillis": 0
Expand Down
2 changes: 1 addition & 1 deletion lib/script-handler-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
cause: e.cause,
code: e.code,
message: e.message,
stack: e.stack.split('\n')
stack: e.stack.split('\n').map(line => line.trim())
}, { reference: true })
} finally {
const end = Date.now()
Expand Down
12 changes: 7 additions & 5 deletions test/spec/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,19 @@ describe('FunctionExecutor', () => {
it('sets line number 1 correctly in stack trace', async () => {
const { result, error } = await run('global["String"].a="a"; return global["String"].a', { a: 1, b: 3 })
expect(result).to.be.undefined
expect(error.stack).to.include(" at userSuppliedScript (file:///user-supplied-script.js:1:19)")
expect(error.stack).to.include("at userSuppliedScript (file:///user-supplied-script.js:1:19)")
})

it('sets line number 2 correctly in stack trace', async () => {
const { result, error } = await run(`
global["String"].a="a"; return global["String"].a
`, { a: 1, b: 3 })
expect(result).to.be.undefined
expect(error.stack).to.include(" at userSuppliedScript (file:///user-supplied-script.js:2:27)")
expect(error.stack).to.include("at userSuppliedScript (file:///user-supplied-script.js:2:27)")
})

it('does not provide NodeJS globals like process', async () => {
const {result, error} = await run(`process.exit(0)`)
const { result, error } = await run(`process.exit(0)`)
expect(result).to.be.undefined
expect(error.message).to.equal('process is not defined')
})
Expand All @@ -119,7 +119,7 @@ describe('FunctionExecutor', () => {
global["String"].a="a"; return global["String"].a
`, { a: 1, b: 3 })
expect(result).to.be.undefined
expect(error.stack).to.include(" at userSuppliedScript (file:///user-supplied-script.js:2:27)")
expect(error.stack).to.include("at userSuppliedScript (file:///user-supplied-script.js:2:27)")
})

it('is aware of all v8 globals', async () => {
Expand Down Expand Up @@ -149,9 +149,11 @@ describe('FunctionExecutor', () => {
})

it('does not allow execution of eval', async () => {
let { result, error } = await run(`
let res;
let { result, error } = res = await run(`
eval(1+1)
`)
console.log(JSON.stringify(res, null, 2))
expect(result).to.be.undefined
expect(error.message).to.equal('"eval" is not allowed in this context.')
})
Expand Down

0 comments on commit 4507068

Please sign in to comment.