Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: resolves locally referenced package exports #90

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 24 additions & 0 deletions test/mapped-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,27 @@ test('handles mapped exports: mapped-exports/bar', { skip: nodeSupportsExports }

hook.unhook()
})

test('handles mapped exports: picks up allow listed resolved module', { skip: nodeSupportsExports }, function (t) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

skip needs to also skip out if node < 18, because that's the min node supported by @langchain/core being used for the test. That should get the other tests to run.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue addressed.

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()
})
Loading