Skip to content

Commit fd395b1

Browse files
committed
fix(files): instrument native directory readers in browser checks
1 parent 9beab14 commit fd395b1

1 file changed

Lines changed: 28 additions & 12 deletions

File tree

‎apps/sim/scripts/files-e2e/upload-lifecycle.ts‎

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,36 @@ export async function runUploadLifecycleChecks(context: FilesE2EContext, directo
2424
const recovery = join(directory, 'after-reader-cancel.txt')
2525
await writeFile(recovery, 'Reader cancellation recovered\n')
2626
const reader = await page.evaluateHandle(() => {
27-
const original = FileSystemDirectoryReader.prototype.readEntries
2827
const pending: (() => void)[] = []
2928
let calls = 0
3029
let released = false
31-
FileSystemDirectoryReader.prototype.readEntries = function (success, failure) {
32-
calls += 1
33-
original.call(
34-
this,
35-
(entries) => {
36-
if (released) success(entries)
37-
else pending.push(() => success(entries))
38-
},
39-
failure
40-
)
30+
let prototype: FileSystemDirectoryReader | undefined
31+
let original: FileSystemDirectoryReader['readEntries'] | undefined
32+
const interceptDrop = (event: DragEvent) => {
33+
if (prototype) return
34+
for (const item of Array.from(event.dataTransfer?.items ?? [])) {
35+
const entry = item.webkitGetAsEntry()
36+
if (!entry?.isDirectory) continue
37+
const nativeReader = (entry as FileSystemDirectoryEntry).createReader()
38+
const readerPrototype = Object.getPrototypeOf(nativeReader) as FileSystemDirectoryReader
39+
const readEntries = readerPrototype.readEntries
40+
prototype = readerPrototype
41+
original = readEntries
42+
readerPrototype.readEntries = function (success, failure) {
43+
calls += 1
44+
readEntries.call(
45+
this,
46+
(entries) => {
47+
if (released) success(entries)
48+
else pending.push(() => success(entries))
49+
},
50+
failure
51+
)
52+
}
53+
break
54+
}
4155
}
56+
window.addEventListener('drop', interceptDrop, true)
4257
return {
4358
get calls() {
4459
return calls
@@ -51,7 +66,8 @@ export async function runUploadLifecycleChecks(context: FilesE2EContext, directo
5166
for (const finish of pending.splice(0)) finish()
5267
},
5368
restore() {
54-
FileSystemDirectoryReader.prototype.readEntries = original
69+
window.removeEventListener('drop', interceptDrop, true)
70+
if (prototype && original) prototype.readEntries = original
5571
},
5672
}
5773
})

0 commit comments

Comments
 (0)