Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/compile/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const MINIFY = (() => {
}
})()

module.exports = ({ content, cwd }) =>
module.exports = ({ content, cwd, nodePaths = [] }) =>
esbuild.build({
stdin: {
contents: content,
Expand All @@ -24,5 +24,6 @@ module.exports = ({ content, cwd }) =>
platform: 'node',
legalComments: 'eof',
target: 'node24',
nodePaths,
...MINIFY
})
28 changes: 23 additions & 5 deletions src/compile/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { mkdirSync } = require('fs')
const { mkdirSync, readFileSync } = require('fs')
const path = require('path')

const transformDependencies = require('./transform-dependencies')
Expand All @@ -24,22 +24,40 @@ const enqueueInstall = (tmpdir, dependencies, allow) => {
return next
}

module.exports = async (snippet, { tmpdir = DEFAULT_TMPDIR, allow = {} } = {}) => {
module.exports = async (snippet, { tmpdir = DEFAULT_TMPDIR, allow = {}, nodePaths = [] } = {}) => {
let content = template(snippet)
const phases = { install: 0 }

const dependencies = detectDependencies(content)
const allDependencies = detectDependencies(content)
installDependencies.validateDependencies(allDependencies, allow.dependencies)
const dependencies = nodePaths.length
? allDependencies.filter(dep => {
const name = installDependencies.extractPackageName(dep)
const version = dep.slice(name.length + 1)
try {
const pkgPath = require.resolve(path.join(name, 'package.json'), { paths: nodePaths })
if (version === 'latest') return false
return JSON.parse(readFileSync(pkgPath, 'utf8')).version !== version
} catch {
return true
}
})
Comment thread
cursor[bot] marked this conversation as resolved.
Comment thread
cursor[bot] marked this conversation as resolved.
: allDependencies

if (dependencies.length) {
content = transformDependencies(content)
mkdirSync(tmpdir, { recursive: true })
const elapsed = timeSpan()
await enqueueInstall(tmpdir, dependencies, allow)
phases.install = elapsed()
} else if (allDependencies.length) {
content = transformDependencies(content)
Comment thread
cursor[bot] marked this conversation as resolved.
mkdirSync(tmpdir, { recursive: true })
}

const cwd = dependencies.length ? tmpdir : process.cwd()
const cwd = allDependencies.length ? tmpdir : process.cwd()
const elapsed = timeSpan()
const result = await build({ content, cwd })
const result = await build({ content, cwd, nodePaths })
Comment thread
cursor[bot] marked this conversation as resolved.
phases.build = elapsed()
content = result.outputFiles[0].text

Expand Down
3 changes: 3 additions & 0 deletions src/compile/install-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,6 @@ module.exports = async ({ dependencies, cwd, allow = {} }) => {
validateDependencies(dependencies, allow.dependencies)
return $(`${install} ${dependencies.join(' ')}`, { cwd, env: { ...process.env, CI: true } })
}

module.exports.validateDependencies = validateDependencies
module.exports.extractPackageName = extractPackageName
2 changes: 2 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export interface AllowOptions {
export interface CreateOptions {
/** Directory for installing code dependencies. Reused across invocations. */
tmpdir?: string
/** Additional directories for resolving dependencies. Dependencies found here with a matching version skip npm install. */
nodePaths?: string[]
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ const spawn = ({ args, env, timeout }) => {
return $('node', ['-', args], spawnOpts)
}

module.exports = ({ tmpdir } = {}) => {
module.exports = ({ tmpdir, nodePaths } = {}) => {
const isolatedFunction = (snippet, { timeout, memory, throwError = true, allow = {} } = {}) => {
if (!['function', 'string'].includes(typeof snippet)) throw new TypeError('Expected a function')
const { permissions = [] } = allow
const compilePromise = compile(snippet, { tmpdir, allow })
const compilePromise = compile(snippet, { tmpdir, allow, nodePaths })

return async (...args) => {
let total
Expand Down