Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
fs.writeFileSync(handlerPath, handler, 'utf8')
console.log()
console.log('compiling entrypoint with ncc...')
const ncc = require(path.join(nccPath, 'node_modules/@zeit/ncc'))
const { code, assets } = await ncc(handlerPath)
const outputHandlerPath = path.join('user', 'fts-handler.js')
const blob = new FileBlob({ data: code })
// move all user code to 'user' subdirectory
preparedFiles[outputHandlerPath] = blob
// eslint-disable-next-line no-restricted-syntax
for (const assetName of Object.keys(assets)) {
const { source: data, permissions: mode } = assets[assetName]
const blob2 = new FileBlob({ data, mode })
preparedFiles[
path.join('user', path.dirname(entrypoint), assetName)
] = blob2
}
return {
preparedFiles,
handlerPath: outputHandlerPath
}
}
async function createLambdaForNode(buildConfig, lambdas, workPath) {
console.log(`Creating lambda for ${buildConfig.name} (${buildConfig.target})`);
const launcherPath = path.join(__dirname, 'launcher.js');
let launcherData = await fs.readFile(launcherPath, 'utf8');
launcherData = launcherData.replace(
'// PLACEHOLDER',
[
`listener = require('./index.js');`,
'if (listener.default) listener = listener.default;'
].join(' ')
);
const preparedFiles = {
'launcher.js': new FileBlob({ data: launcherData }),
'bridge.js': new FileFsRef({ fsPath: nodeBridge }),
'index.js': new FileFsRef({
fsPath: require.resolve(path.join(workPath, buildConfig.outputTo))
})
};
const lambda = await createLambda({
files: { ...preparedFiles },
handler: 'launcher.launcher',
runtime: 'nodejs8.10'
});
lambdas[buildConfig.outputTo] = lambda;
}
const handler = `
import * as ftsHttp from 'fts-http'
import * as handler from '${input.replace('.ts', '')}'
const definition = ${definitionData}
export default ftsHttp.createHttpHandler(definition, handler)
`
fs.writeFileSync(handlerPath, handler, 'utf8')
console.log()
console.log('compiling entrypoint with ncc...')
const ncc = require(path.join(nccPath, 'node_modules/@zeit/ncc'))
const { code, assets } = await ncc(handlerPath)
const outputHandlerPath = path.join('user', 'fts-handler.js')
const blob = new FileBlob({ data: code })
// move all user code to 'user' subdirectory
preparedFiles[outputHandlerPath] = blob
// eslint-disable-next-line no-restricted-syntax
for (const assetName of Object.keys(assets)) {
const { source: data, permissions: mode } = assets[assetName]
const blob2 = new FileBlob({ data, mode })
preparedFiles[
path.join('user', path.dirname(entrypoint), assetName)
] = blob2
}
return {
preparedFiles,
handlerPath: outputHandlerPath
}
// setting up launcher
const launcherPath = path.join(__dirname, 'launcher.js')
let launcherData = await fs.readFile(launcherPath, 'utf8')
launcherData = launcherData.replace(
'// PLACEHOLDER',
[
'process.chdir("./user");',
`listener = require("${handlerPath}");`,
'if (listener.default) listener = listener.default;'
].join(' ')
)
const launcherFiles = {
'launcher.js': new FileBlob({ data: launcherData }),
'bridge.js': new FileFsRef({ fsPath: require('@now/node-bridge') })
}
const lambda = await createLambda({
files: { ...preparedFiles, ...launcherFiles },
handler: 'launcher.launcher',
runtime: 'nodejs8.10'
})
return { [entrypoint]: lambda }
}