diff --git a/packages/polyfill/extend.js b/packages/polyfill/extend.js new file mode 100644 index 0000000..6c11879 --- /dev/null +++ b/packages/polyfill/extend.js @@ -0,0 +1,12 @@ +import './index.js'; + +/** + * polyfill for WebSocket + */ +import { WebSocket as _WebSocket } from './web/websocket'; +if (!globalThis.WebSocket) { + globalThis.WebSocket = _WebSocket; + WebSocket = _WebSocket; +} + +export { WebSocket } from './web/websocket'; diff --git a/packages/polyfill/index.js b/packages/polyfill/index.js index 05fec96..c2efcb0 100644 --- a/packages/polyfill/index.js +++ b/packages/polyfill/index.js @@ -8,6 +8,5 @@ export { TextDecoder, TextEncoder } from './web/encoding'; export { XMLHttpRequest } from './web/xhr'; export { fetch, Headers, Request, Response } from './web/fetch'; export { crypto } from './web/crypto'; -export { WebSocket } from './web/websocket'; export { LocalStorage } from './web/storage'; export { navigator } from './web/navigator'; diff --git a/packages/polyfill/node/index.js b/packages/polyfill/node/index.js index b262177..5ab4a29 100644 --- a/packages/polyfill/node/index.js +++ b/packages/polyfill/node/index.js @@ -1,12 +1,12 @@ import * as harmonyFS from './fs'; -import * as process from 'process/browser' +// import * as process from 'process/browser' if (!globalThis.fs) { globalThis.fs = harmonyFS; } if (!globalThis.process) { - globalThis.process = process; + globalThis.process = {}; } if (!globalThis.process.versions) { @@ -19,14 +19,14 @@ if (!globalThis.process.versions.node) { globalThis['eval'] = function (functionBody) { if (functionBody == 'require') { - return function protoRequire(moduleName){ + return function protoRequire(moduleName) { if (moduleName == 'fs') { return harmonyFS; - }else{ - return __webpack_require__(moduleName) + } else { + // eslint-disable-next-line + return __webpack_require__(moduleName); } }; } return eval(functionBody); }; - diff --git a/packages/polyfill/web/encoding.js b/packages/polyfill/web/encoding.js index 55ce986..c7a7c68 100644 --- a/packages/polyfill/web/encoding.js +++ b/packages/polyfill/web/encoding.js @@ -2,7 +2,6 @@ import util from '@ohos.util'; /** * since: API 7 */ -const TextDecoder = util.TextDecoder; -const TextEncoder = util.TextEncoder; +const TextDecoder = util?.TextDecoder; +const TextEncoder = util?.TextEncoder; export { TextDecoder, TextEncoder }; - diff --git a/packages/polyfill/web/index.js b/packages/polyfill/web/index.js index 2ad7230..b9e25a7 100644 --- a/packages/polyfill/web/index.js +++ b/packages/polyfill/web/index.js @@ -52,15 +52,6 @@ if (!globalThis.crypto) { globalThis.crypto = crypto; } -/** - * polyfill for WebSocket - */ -import { WebSocket as _WebSocket } from './websocket'; -if (!globalThis.WebSocket) { - globalThis.WebSocket = _WebSocket; - WebSocket = _WebSocket; -} - /** * polyfill for Canvas */ diff --git a/packages/polyfill/web/url.js b/packages/polyfill/web/url.js index e208f89..d77dcb7 100644 --- a/packages/polyfill/web/url.js +++ b/packages/polyfill/web/url.js @@ -3,6 +3,6 @@ import url from '@ohos.url'; /** * since: API 7 */ -const URL = url.URL; -const URLSearchParams = url.URLSearchParams; +const URL = url?.URL; +const URLSearchParams = url?.URLSearchParams; export { URL, URLSearchParams };