Skip to content
Closed
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
33 changes: 31 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,40 @@ function Hook (modules, options, onrequire) {

let matchFound = false
if (hasWhitelist) {
if (!id.startsWith('.') && modules.includes(id)) {
if (id.startsWith('.') === true && modules.includes(id) === false) {
// Try to resolve a package export relative to the base package.
// If found, and the package export is in the allow list, return
// success. Basically, we take a required module like
// `../dist/foo/bar.cjs` and narrow it down to what is likely present
// in the package exports.
let doWork = true
const parts = id.split('/')
const fileName = parts[parts.length - 1]
parts[parts.length - 1] = path.basename(fileName, path.extname(fileName))
do {
try {
parts.shift()
if (parts.length === 0) {
// We couldn't find anything, so let the rest of the algorithm
// play out.
doWork = false
continue
}

const exportName = moduleName + '/' + parts.join('/')
require.resolve(exportName)
moduleName = exportName
if (modules.includes(exportName) === true) {
matchFound = true
}
doWork = false
} catch {}
} while (doWork === true)
} else if (!id.startsWith('.') && modules.includes(id)) {
// Not starting with '.' means `id` is identifying a module path,
// as opposed to a local file path. (Note: I'm not sure about
// absolute paths, but those are handled above.)
// If this `id` is in `modules`, then this could be a match to an
// If this `id` is in `modules`, then this could be a match to a
// package "exports" entry point that wouldn't otherwise match below.
moduleName = id
matchFound = true
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@babel/preset-env": "^7.9.5",
"@babel/preset-typescript": "^7.9.0",
"@babel/register": "^7.9.0",
"@langchain/core": "^0.1.57",
"ipp-printer": "^1.0.0",
"patterns": "^1.0.3",
"roundround": "^0.2.0",
Expand Down
29 changes: 29 additions & 0 deletions test/mapped-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { Hook } = require('../')

// Mapped export tests require Node.js >=12.17.0 for "exports" support in package.json.
const nodeSupportsExports = semver.lt(process.version, '12.17.0')
const node18AndAbove = semver.gte(process.version, '18.0.0')

test('handles mapped exports: mapped-exports/foo', { skip: nodeSupportsExports }, function (t) {
t.plan(2)
Expand Down Expand Up @@ -42,3 +43,31 @@ test('handles mapped exports: mapped-exports/bar', { skip: nodeSupportsExports }

hook.unhook()
})

test(
'handles mapped exports: picks up allow listed resolved module',
{ skip: nodeSupportsExports || node18AndAbove === false },
function (t) {
t.plan(3)

let hookHit = false
const hook = new Hook(['@langchain/core/callbacks/manager'], function (exports, name) {
t.equal(name, '@langchain/core/callbacks/manager', 'hook name matches')
hookHit = true
return exports
})

const { Tool } = require('@langchain/core/tools')
const MyTool = class MyTool extends Tool {
_call () {
t.pass('tool was executed successfully')
}
}

const tool = new MyTool()
tool.call('foo', [() => {}])
t.equal(hookHit, true, 'hook was hit successfully')

hook.unhook()
}
)