Skip to content

Commit 548e70e

Browse files
committed
fix: link.target setter
There were guards in place to protect when setting a promise as a target, which is not something the code actually does, which was discovered after unpacking load-actual. This removes those guards which are dangerous because either an attribute is a promise or it's not. Letting things access attributes as non-promises that are sometimes promises is dangerous.
1 parent 2db6c08 commit 548e70e

File tree

2 files changed

+0
-39
lines changed

2 files changed

+0
-39
lines changed

workspaces/arborist/lib/link.js

-22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const debug = require('./debug.js')
21
const relpath = require('./relpath.js')
32
const Node = require('./node.js')
43
const _loadDeps = Symbol.for('Arborist.Node._loadDeps')
@@ -53,27 +52,6 @@ class Link extends Node {
5352
return
5453
}
5554

56-
if (current && current.then) {
57-
debug(() => {
58-
throw Object.assign(new Error('cannot set target while awaiting'), {
59-
path: this.path,
60-
realpath: this.realpath,
61-
})
62-
})
63-
}
64-
65-
if (target && target.then) {
66-
// can set to a promise during an async tree build operation
67-
// wait until then to assign it.
68-
this[_target] = target
69-
// eslint-disable-next-line promise/always-return, promise/catch-or-return
70-
target.then(node => {
71-
this[_target] = null
72-
this.target = node
73-
})
74-
return
75-
}
76-
7755
if (!target) {
7856
if (current && current.linksIn) {
7957
current.linksIn.delete(this)

workspaces/arborist/test/link.js

-17
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,6 @@ t.test('link.target setter', async t => {
9494
t.equal(oldTarget.linksIn.size, 0, 'old target still has no links')
9595
t.equal(newTarget.linksIn.size, 0, 'new target has no links in now')
9696

97-
const laterTarget = new Promise((res) =>
98-
setTimeout(() => res(new Node({
99-
path: '/node-later',
100-
realpath: '/node-later',
101-
pkg: { name: 'node-later', version: '1.2.3' },
102-
}))))
103-
link.target = laterTarget
104-
t.equal(link.target, laterTarget, 'waiting for a new target to resolve')
105-
t.throws(() => link.target = oldTarget, {
106-
message: 'cannot set target while awaiting',
107-
path: link.path,
108-
realpath: link.realpath,
109-
})
110-
const node = await laterTarget
111-
t.equal(link.target, node, 'target resolved and assigned')
112-
t.equal(link.package, node.package, 'took on new targets package')
113-
t.equal(node.linksIn.has(link), true, 'link in node.linksIn')
11497
link.target = null
11598
t.equal(link.target, null, 'target is now null')
11699
t.strictSame(link.package, {}, 'removed target, package is now empty')

0 commit comments

Comments
 (0)