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

refactor(api/internal/monkeypatch.js): patch missing file api #861

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
120 changes: 70 additions & 50 deletions api/internal/monkeypatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ import geolocation from './geolocation.js'
import permissions from './permissions.js'
import WebAssembly from './webassembly.js'

import {
File,
FileSystemHandle,
FileSystemFileHandle,
FileSystemDirectoryHandle,
FileSystemWritableFileStream
} from '../fs/web.js'

import {
showDirectoryPicker,
showOpenFilePicker,
Expand Down Expand Up @@ -37,6 +45,9 @@ export function init () {
if (typeof target[actualName] === 'object' && target[actualName] !== null) {
for (const key in implementation) {
const nativeImplementation = target[actualName][key] || null
if (nativeImplementation === implementation[key]) {
continue
}
// let this fail, the environment implementation may not be writable
try {
target[actualName][key] = implementation[key]
Expand All @@ -56,65 +67,67 @@ export function init () {
}
} else {
const nativeImplementation = target[actualName] || null
// let this fail, the environment implementation may not be writable
try {
target[actualName] = implementation
} catch {}

patches[actualName] = implementation
if (
(typeof nativeImplementation === 'function' && nativeImplementation.prototype) &&
(typeof implementation === 'function' && implementation.prototype)
) {
const nativeDescriptors = Object.getOwnPropertyDescriptors(nativeImplementation.prototype)
const descriptors = Object.getOwnPropertyDescriptors(implementation.prototype)
implementation[Symbol.species] = nativeImplementation
for (const key in nativeDescriptors) {
const nativeDescriptor = nativeDescriptors[key]
const descriptor = descriptors[key]

if (key === 'constructor') {
continue
}
if (nativeImplementation !== implementation) {
// let this fail, the environment implementation may not be writable
try {
target[actualName] = implementation
} catch {}

if (descriptor) {
if (nativeDescriptor.set && nativeDescriptor.get) {
descriptors[key] = { ...nativeDescriptor, ...descriptor }
} else {
descriptors[key] = { writable: true, configurable: true, ...descriptor }
patches[actualName] = implementation
if (
(typeof nativeImplementation === 'function' && nativeImplementation.prototype) &&
(typeof implementation === 'function' && implementation.prototype)
) {
const nativeDescriptors = Object.getOwnPropertyDescriptors(nativeImplementation.prototype)
const descriptors = Object.getOwnPropertyDescriptors(implementation.prototype)
implementation[Symbol.species] = nativeImplementation
for (const key in nativeDescriptors) {
const nativeDescriptor = nativeDescriptors[key]
const descriptor = descriptors[key]

if (key === 'constructor') {
continue
}
} else {
descriptors[key] = { writable: true, configurable: true }
}

if (descriptors[key] && typeof descriptors[key] === 'object') {
if ('get' in descriptors[key] && typeof descriptors[key].get !== 'function') {
delete descriptors[key].get
if (descriptor) {
if (nativeDescriptor.set && nativeDescriptor.get) {
descriptors[key] = { ...nativeDescriptor, ...descriptor }
} else {
descriptors[key] = { writable: true, configurable: true, ...descriptor }
}
} else {
descriptors[key] = { writable: true, configurable: true }
}

if ('set' in descriptors[key] && typeof descriptors[key].set !== 'function') {
delete descriptors[key].set
}
if (descriptors[key] && typeof descriptors[key] === 'object') {
if ('get' in descriptors[key] && typeof descriptors[key].get !== 'function') {
delete descriptors[key].get
}

if ('set' in descriptors[key] && typeof descriptors[key].set !== 'function') {
delete descriptors[key].set
}

if (descriptors[key].get || descriptors[key].set) {
delete descriptors[key].writable
delete descriptors[key].value
if (descriptors[key].get || descriptors[key].set) {
delete descriptors[key].writable
delete descriptors[key].value
}
}
}
}

Object.defineProperties(implementation.prototype, descriptors)
Object.setPrototypeOf(implementation.prototype, nativeImplementation.prototype)
}
Object.defineProperties(implementation.prototype, descriptors)
Object.setPrototypeOf(implementation.prototype, nativeImplementation.prototype)
}

if (nativeImplementation !== null) {
const nativeName = ['_', 'native', ...name.split('.')].join('_')
natives[name] = nativeImplementation
Object.defineProperty(globalThis, nativeName, {
enumerable: false,
configurable: false,
value: nativeImplementation
})
if (nativeImplementation !== null) {
const nativeName = ['_', 'native', ...name.split('.')].join('_')
natives[name] = nativeImplementation
Object.defineProperty(globalThis, nativeName, {
enumerable: false,
configurable: false,
value: nativeImplementation
})
}
}
}
}
Expand Down Expand Up @@ -149,7 +162,14 @@ export function init () {
showSaveFilePicker,

// events
ApplicationURLEvent
ApplicationURLEvent,

// file
File,
FileSystemHandle,
FileSystemFileHandle,
FileSystemDirectoryHandle,
FileSystemWritableFileStream
})

// navigator
Expand Down