Skip to content
This repository was archived by the owner on Dec 17, 2024. It is now read-only.

Commit 44a75f3

Browse files
committed
fix for require('fs') in compositions
Fixes #934
1 parent bf9ec5c commit 44a75f3

File tree

8 files changed

+59
-4
lines changed

8 files changed

+59
-4
lines changed

app/plugins/modules/composer/lib/create-from-source.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,10 @@ exports.compileToFSM = (src, opts={}) => new Promise((resolve, reject) => {
113113
require: m => {
114114
if (m === '@ibm-functions/composer') {
115115
return openwhiskComposer
116+
} else if (m.charAt(0) !== '.') {
117+
return require(m)
116118
} else {
117-
return require(path.resolve(dir, m))
118-
119+
return require(path.resolve(m))
119120
}
120121
}
121122
}
@@ -134,7 +135,20 @@ exports.compileToFSM = (src, opts={}) => new Promise((resolve, reject) => {
134135
}
135136
const sandboxWithComposer = Object.assign(sandbox, { composer: openwhiskComposer })
136137

137-
let res = vm.runInNewContext(code, sandboxWithComposer)
138+
// we need to be in the directory of the
139+
// source, in case it does relative requires
140+
// or reads from that relative location
141+
const curdir = process.cwd()
142+
process.chdir(ui.findFile(dir))
143+
144+
let res
145+
try {
146+
res = vm.runInNewContext(code, sandboxWithComposer)
147+
} finally {
148+
// make sure we change back to where we started
149+
process.chdir(curdir)
150+
}
151+
138152
debug('res', typeof res, res)
139153

140154
if (typeof res === 'function') {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"hello": "world"
3+
}

tests/data/composer-source/echo.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = n => `echo${n}`

tests/data/composer-source/fs-read.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function foo() {
2+
const fs = require('fs');
3+
const authorMap = JSON.parse(fs.readFileSync('author-map.json', 'utf8'));
4+
return composer.let({ am: authorMap }, p => {
5+
return am[p.author] == undefined ? {} : am[p.author]
6+
})
7+
}
8+
composer.sequence(foo())
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require('fs')
2+
composer.sequence('echo1','echo2')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
composer.sequence(require('./echo')(1),
2+
require('./echo')(2))

tests/tests/passes/07/composer-create.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,8 @@ describe('app create and sessions', function() {
318318
fs.readdirSync(srcDir).forEach((file,idx) => {
319319
const name = `${seqName2}-${idx}`
320320

321-
if (file.endsWith('.js')) {
321+
// echo.js is used by require-relative.js, it isn't a composition on its own
322+
if (file.endsWith('.js') && file !== 'echo.js') {
322323
it(`should create a composer sequence from source ${file}`, () => cli.do(`app create ${name} ${path.join(srcDir, file)}`, this.app)
323324
.then(cli.expectOK)
324325
.then(sidecar.expectOpen)

tests/tests/passes/07/composer-viz.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ const fsm = input('fsm.json'), fsmStruct = JSON.parse(fs.readFileSync(fsm.path).
4747
demo = composerInput('demo.js'),
4848
demoRetain = composerInput('demo-retain.js'),
4949
mask = composerInput('mask.js'),
50+
requireAbsolute = composerInput('require-absolute.js'),
51+
requireRelative = composerInput('require-relative.js'),
52+
fsRead = composerInput('fs-read.js'),
5053
addSubscription = composerErrorInput('addSubscription.js')
5154

5255
/**
@@ -237,6 +240,27 @@ describe('show the composer visualization without creating openwhisk assets', fu
237240
.then(verifyEdgeExists('echo1', 'echo2'))
238241
.catch(common.oops(this)))
239242

243+
/** test: from the openwhisk-composer/samples directory */
244+
it(`show visualization from javascript source ${requireAbsolute.path}`, () => cli.do(`app viz ${requireAbsolute.path}`, this.app)
245+
.then(verifyTheBasicStuff(requireAbsolute.file, 'composerLib'))
246+
.then(verifyNodeExists('echo1'))
247+
.then(verifyNodeExists('echo2'))
248+
.then(verifyEdgeExists('echo1', 'echo2'))
249+
.catch(common.oops(this)))
250+
251+
/** test: from the openwhisk-composer/samples directory */
252+
it(`show visualization from javascript source ${requireRelative.path}`, () => cli.do(`app viz ${requireRelative.path}`, this.app)
253+
.then(verifyTheBasicStuff(requireRelative.file, 'composerLib'))
254+
.then(verifyNodeExists('echo1'))
255+
.then(verifyNodeExists('echo2'))
256+
.then(verifyEdgeExists('echo1', 'echo2'))
257+
.catch(common.oops(this)))
258+
259+
/** test: from the openwhisk-composer/samples directory */
260+
it(`show visualization from javascript source ${fsRead.path}`, () => cli.do(`app viz ${fsRead.path}`, this.app)
261+
.then(verifyTheBasicStuff(fsRead.file, 'composerLib'))
262+
.catch(common.oops(this)))
263+
240264
it(`fail to show visualization for addSubscription without -e for env var assignment`, () => cli.do(`preview ${addSubscription.path}`, this.app)
241265
.then(cli.expectError(0, 'SLACK_TOKEN required in environment'))
242266
.catch(common.oops(this)))

0 commit comments

Comments
 (0)