From de7f3bc0b36be8b54f84353ed6bfa5e3d3c0da5d Mon Sep 17 00:00:00 2001 From: Sam Thorogood Date: Mon, 29 Aug 2022 10:31:57 +1000 Subject: [PATCH] try esm --- package-lock.json | 562 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 2 + src/buffer.js | 23 ++ src/lowlevel.js | 141 ++++++++++++ src/o-decoder.js | 78 +++++++ src/o-encoder.js | 25 +++ src/polyfill.js | 11 + src/shared.js | 13 ++ src/support.js | 2 + src/xhr.js | 29 +++ test.mjs | 14 +- text.js | 50 +++-- tsconfig.json | 24 ++ 13 files changed, 951 insertions(+), 23 deletions(-) create mode 100644 src/buffer.js create mode 100644 src/lowlevel.js create mode 100644 src/o-decoder.js create mode 100644 src/o-encoder.js create mode 100644 src/polyfill.js create mode 100644 src/shared.js create mode 100644 src/support.js create mode 100644 src/xhr.js create mode 100644 tsconfig.json diff --git a/package-lock.json b/package-lock.json index 5ce5302..f7c6751 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,9 +9,33 @@ "version": "1.0.4", "license": "Apache-2.0", "devDependencies": { + "@types/node": "^18.7.13", + "esbuild": "^0.15.5", "google-closure-compiler": "^20220601.0.0" } }, + "node_modules/@esbuild/linux-loong64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.15.5.tgz", + "integrity": "sha512-UHkDFCfSGTuXq08oQltXxSZmH1TXyWsL+4QhZDWvvLl6mEJQqk3u7/wq1LjhrrAXYIllaTtRSzUXl4Olkf2J8A==", + "cpu": [ + "loong64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@types/node": { + "version": "18.7.13", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.7.13.tgz", + "integrity": "sha512-46yIhxSe5xEaJZXWdIBP7GU4HDTG8/eo0qd9atdiL+lFpA03y8KS+lkTN834TWJj5767GbWv4n/P6efyTFt1Dw==", + "dev": true + }, "node_modules/ansi-styles": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", @@ -94,6 +118,362 @@ "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac= sha512-3lqz5YjWTYnW6dlDa5TLaTCcShfar1e40rmcJVwCBJC6mWlFuj0eCHIElmG1g5kyuJ/GD+8Wn4FFCcz4gJPfaQ==", "dev": true }, + "node_modules/esbuild": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.15.5.tgz", + "integrity": "sha512-VSf6S1QVqvxfIsSKb3UKr3VhUCis7wgDbtF4Vd9z84UJr05/Sp2fRKmzC+CSPG/dNAPPJZ0BTBLTT1Fhd6N9Gg==", + "dev": true, + "hasInstallScript": true, + "bin": { + "esbuild": "bin/esbuild" + }, + "engines": { + "node": ">=12" + }, + "optionalDependencies": { + "@esbuild/linux-loong64": "0.15.5", + "esbuild-android-64": "0.15.5", + "esbuild-android-arm64": "0.15.5", + "esbuild-darwin-64": "0.15.5", + "esbuild-darwin-arm64": "0.15.5", + "esbuild-freebsd-64": "0.15.5", + "esbuild-freebsd-arm64": "0.15.5", + "esbuild-linux-32": "0.15.5", + "esbuild-linux-64": "0.15.5", + "esbuild-linux-arm": "0.15.5", + "esbuild-linux-arm64": "0.15.5", + "esbuild-linux-mips64le": "0.15.5", + "esbuild-linux-ppc64le": "0.15.5", + "esbuild-linux-riscv64": "0.15.5", + "esbuild-linux-s390x": "0.15.5", + "esbuild-netbsd-64": "0.15.5", + "esbuild-openbsd-64": "0.15.5", + "esbuild-sunos-64": "0.15.5", + "esbuild-windows-32": "0.15.5", + "esbuild-windows-64": "0.15.5", + "esbuild-windows-arm64": "0.15.5" + } + }, + "node_modules/esbuild-android-64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-android-64/-/esbuild-android-64-0.15.5.tgz", + "integrity": "sha512-dYPPkiGNskvZqmIK29OPxolyY3tp+c47+Fsc2WYSOVjEPWNCHNyqhtFqQadcXMJDQt8eN0NMDukbyQgFcHquXg==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-android-arm64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-android-arm64/-/esbuild-android-arm64-0.15.5.tgz", + "integrity": "sha512-YyEkaQl08ze3cBzI/4Cm1S+rVh8HMOpCdq8B78JLbNFHhzi4NixVN93xDrHZLztlocEYqi45rHHCgA8kZFidFg==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-darwin-64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-darwin-64/-/esbuild-darwin-64-0.15.5.tgz", + "integrity": "sha512-Cr0iIqnWKx3ZTvDUAzG0H/u9dWjLE4c2gTtRLz4pqOBGjfjqdcZSfAObFzKTInLLSmD0ZV1I/mshhPoYSBMMCQ==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-darwin-arm64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-darwin-arm64/-/esbuild-darwin-arm64-0.15.5.tgz", + "integrity": "sha512-WIfQkocGtFrz7vCu44ypY5YmiFXpsxvz2xqwe688jFfSVCnUsCn2qkEVDo7gT8EpsLOz1J/OmqjExePL1dr1Kg==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-freebsd-64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-freebsd-64/-/esbuild-freebsd-64-0.15.5.tgz", + "integrity": "sha512-M5/EfzV2RsMd/wqwR18CELcenZ8+fFxQAAEO7TJKDmP3knhWSbD72ILzrXFMMwshlPAS1ShCZ90jsxkm+8FlaA==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-freebsd-arm64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-freebsd-arm64/-/esbuild-freebsd-arm64-0.15.5.tgz", + "integrity": "sha512-2JQQ5Qs9J0440F/n/aUBNvY6lTo4XP/4lt1TwDfHuo0DY3w5++anw+jTjfouLzbJmFFiwmX7SmUhMnysocx96w==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-linux-32": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-linux-32/-/esbuild-linux-32-0.15.5.tgz", + "integrity": "sha512-gO9vNnIN0FTUGjvTFucIXtBSr1Woymmx/aHQtuU+2OllGU6YFLs99960UD4Dib1kFovVgs59MTXwpFdVoSMZoQ==", + "cpu": [ + "ia32" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-linux-64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-linux-64/-/esbuild-linux-64-0.15.5.tgz", + "integrity": "sha512-ne0GFdNLsm4veXbTnYAWjbx3shpNKZJUd6XpNbKNUZaNllDZfYQt0/zRqOg0sc7O8GQ+PjSMv9IpIEULXVTVmg==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-linux-arm": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-linux-arm/-/esbuild-linux-arm-0.15.5.tgz", + "integrity": "sha512-wvAoHEN+gJ/22gnvhZnS/+2H14HyAxM07m59RSLn3iXrQsdS518jnEWRBnJz3fR6BJa+VUTo0NxYjGaNt7RA7Q==", + "cpu": [ + "arm" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-linux-arm64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-linux-arm64/-/esbuild-linux-arm64-0.15.5.tgz", + "integrity": "sha512-7EgFyP2zjO065XTfdCxiXVEk+f83RQ1JsryN1X/VSX2li9rnHAt2swRbpoz5Vlrl6qjHrCmq5b6yxD13z6RheA==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-linux-mips64le": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-linux-mips64le/-/esbuild-linux-mips64le-0.15.5.tgz", + "integrity": "sha512-KdnSkHxWrJ6Y40ABu+ipTZeRhFtc8dowGyFsZY5prsmMSr1ZTG9zQawguN4/tunJ0wy3+kD54GaGwdcpwWAvZQ==", + "cpu": [ + "mips64el" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-linux-ppc64le": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-linux-ppc64le/-/esbuild-linux-ppc64le-0.15.5.tgz", + "integrity": "sha512-QdRHGeZ2ykl5P0KRmfGBZIHmqcwIsUKWmmpZTOq573jRWwmpfRmS7xOhmDHBj9pxv+6qRMH8tLr2fe+ZKQvCYw==", + "cpu": [ + "ppc64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-linux-riscv64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-linux-riscv64/-/esbuild-linux-riscv64-0.15.5.tgz", + "integrity": "sha512-p+WE6RX+jNILsf+exR29DwgV6B73khEQV0qWUbzxaycxawZ8NE0wA6HnnTxbiw5f4Gx9sJDUBemh9v49lKOORA==", + "cpu": [ + "riscv64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-linux-s390x": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-linux-s390x/-/esbuild-linux-s390x-0.15.5.tgz", + "integrity": "sha512-J2ngOB4cNzmqLHh6TYMM/ips8aoZIuzxJnDdWutBw5482jGXiOzsPoEF4j2WJ2mGnm7FBCO4StGcwzOgic70JQ==", + "cpu": [ + "s390x" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-netbsd-64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-netbsd-64/-/esbuild-netbsd-64-0.15.5.tgz", + "integrity": "sha512-MmKUYGDizYjFia0Rwt8oOgmiFH7zaYlsoQ3tIOfPxOqLssAsEgG0MUdRDm5lliqjiuoog8LyDu9srQk5YwWF3w==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-openbsd-64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-openbsd-64/-/esbuild-openbsd-64-0.15.5.tgz", + "integrity": "sha512-2mMFfkLk3oPWfopA9Plj4hyhqHNuGyp5KQyTT9Rc8hFd8wAn5ZrbJg+gNcLMo2yzf8Uiu0RT6G9B15YN9WQyMA==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-sunos-64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-sunos-64/-/esbuild-sunos-64-0.15.5.tgz", + "integrity": "sha512-2sIzhMUfLNoD+rdmV6AacilCHSxZIoGAU2oT7XmJ0lXcZWnCvCtObvO6D4puxX9YRE97GodciRGDLBaiC6x1SA==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "sunos" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-windows-32": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-windows-32/-/esbuild-windows-32-0.15.5.tgz", + "integrity": "sha512-e+duNED9UBop7Vnlap6XKedA/53lIi12xv2ebeNS4gFmu7aKyTrok7DPIZyU5w/ftHD4MUDs5PJUkQPP9xJRzg==", + "cpu": [ + "ia32" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-windows-64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-windows-64/-/esbuild-windows-64-0.15.5.tgz", + "integrity": "sha512-v+PjvNtSASHOjPDMIai9Yi+aP+Vwox+3WVdg2JB8N9aivJ7lyhp4NVU+J0MV2OkWFPnVO8AE/7xH+72ibUUEnw==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild-windows-arm64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-windows-arm64/-/esbuild-windows-arm64-0.15.5.tgz", + "integrity": "sha512-Yz8w/D8CUPYstvVQujByu6mlf48lKmXkq6bkeSZZxTA626efQOJb26aDGLzmFWx6eg/FwrXgt6SZs9V8Pwy/aA==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=12" + } + }, "node_modules/escape-string-regexp": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", @@ -308,6 +688,19 @@ } }, "dependencies": { + "@esbuild/linux-loong64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.15.5.tgz", + "integrity": "sha512-UHkDFCfSGTuXq08oQltXxSZmH1TXyWsL+4QhZDWvvLl6mEJQqk3u7/wq1LjhrrAXYIllaTtRSzUXl4Olkf2J8A==", + "dev": true, + "optional": true + }, + "@types/node": { + "version": "18.7.13", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.7.13.tgz", + "integrity": "sha512-46yIhxSe5xEaJZXWdIBP7GU4HDTG8/eo0qd9atdiL+lFpA03y8KS+lkTN834TWJj5767GbWv4n/P6efyTFt1Dw==", + "dev": true + }, "ansi-styles": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", @@ -378,6 +771,175 @@ "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac= sha512-3lqz5YjWTYnW6dlDa5TLaTCcShfar1e40rmcJVwCBJC6mWlFuj0eCHIElmG1g5kyuJ/GD+8Wn4FFCcz4gJPfaQ==", "dev": true }, + "esbuild": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.15.5.tgz", + "integrity": "sha512-VSf6S1QVqvxfIsSKb3UKr3VhUCis7wgDbtF4Vd9z84UJr05/Sp2fRKmzC+CSPG/dNAPPJZ0BTBLTT1Fhd6N9Gg==", + "dev": true, + "requires": { + "@esbuild/linux-loong64": "0.15.5", + "esbuild-android-64": "0.15.5", + "esbuild-android-arm64": "0.15.5", + "esbuild-darwin-64": "0.15.5", + "esbuild-darwin-arm64": "0.15.5", + "esbuild-freebsd-64": "0.15.5", + "esbuild-freebsd-arm64": "0.15.5", + "esbuild-linux-32": "0.15.5", + "esbuild-linux-64": "0.15.5", + "esbuild-linux-arm": "0.15.5", + "esbuild-linux-arm64": "0.15.5", + "esbuild-linux-mips64le": "0.15.5", + "esbuild-linux-ppc64le": "0.15.5", + "esbuild-linux-riscv64": "0.15.5", + "esbuild-linux-s390x": "0.15.5", + "esbuild-netbsd-64": "0.15.5", + "esbuild-openbsd-64": "0.15.5", + "esbuild-sunos-64": "0.15.5", + "esbuild-windows-32": "0.15.5", + "esbuild-windows-64": "0.15.5", + "esbuild-windows-arm64": "0.15.5" + } + }, + "esbuild-android-64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-android-64/-/esbuild-android-64-0.15.5.tgz", + "integrity": "sha512-dYPPkiGNskvZqmIK29OPxolyY3tp+c47+Fsc2WYSOVjEPWNCHNyqhtFqQadcXMJDQt8eN0NMDukbyQgFcHquXg==", + "dev": true, + "optional": true + }, + "esbuild-android-arm64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-android-arm64/-/esbuild-android-arm64-0.15.5.tgz", + "integrity": "sha512-YyEkaQl08ze3cBzI/4Cm1S+rVh8HMOpCdq8B78JLbNFHhzi4NixVN93xDrHZLztlocEYqi45rHHCgA8kZFidFg==", + "dev": true, + "optional": true + }, + "esbuild-darwin-64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-darwin-64/-/esbuild-darwin-64-0.15.5.tgz", + "integrity": "sha512-Cr0iIqnWKx3ZTvDUAzG0H/u9dWjLE4c2gTtRLz4pqOBGjfjqdcZSfAObFzKTInLLSmD0ZV1I/mshhPoYSBMMCQ==", + "dev": true, + "optional": true + }, + "esbuild-darwin-arm64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-darwin-arm64/-/esbuild-darwin-arm64-0.15.5.tgz", + "integrity": "sha512-WIfQkocGtFrz7vCu44ypY5YmiFXpsxvz2xqwe688jFfSVCnUsCn2qkEVDo7gT8EpsLOz1J/OmqjExePL1dr1Kg==", + "dev": true, + "optional": true + }, + "esbuild-freebsd-64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-freebsd-64/-/esbuild-freebsd-64-0.15.5.tgz", + "integrity": "sha512-M5/EfzV2RsMd/wqwR18CELcenZ8+fFxQAAEO7TJKDmP3knhWSbD72ILzrXFMMwshlPAS1ShCZ90jsxkm+8FlaA==", + "dev": true, + "optional": true + }, + "esbuild-freebsd-arm64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-freebsd-arm64/-/esbuild-freebsd-arm64-0.15.5.tgz", + "integrity": "sha512-2JQQ5Qs9J0440F/n/aUBNvY6lTo4XP/4lt1TwDfHuo0DY3w5++anw+jTjfouLzbJmFFiwmX7SmUhMnysocx96w==", + "dev": true, + "optional": true + }, + "esbuild-linux-32": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-linux-32/-/esbuild-linux-32-0.15.5.tgz", + "integrity": "sha512-gO9vNnIN0FTUGjvTFucIXtBSr1Woymmx/aHQtuU+2OllGU6YFLs99960UD4Dib1kFovVgs59MTXwpFdVoSMZoQ==", + "dev": true, + "optional": true + }, + "esbuild-linux-64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-linux-64/-/esbuild-linux-64-0.15.5.tgz", + "integrity": "sha512-ne0GFdNLsm4veXbTnYAWjbx3shpNKZJUd6XpNbKNUZaNllDZfYQt0/zRqOg0sc7O8GQ+PjSMv9IpIEULXVTVmg==", + "dev": true, + "optional": true + }, + "esbuild-linux-arm": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-linux-arm/-/esbuild-linux-arm-0.15.5.tgz", + "integrity": "sha512-wvAoHEN+gJ/22gnvhZnS/+2H14HyAxM07m59RSLn3iXrQsdS518jnEWRBnJz3fR6BJa+VUTo0NxYjGaNt7RA7Q==", + "dev": true, + "optional": true + }, + "esbuild-linux-arm64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-linux-arm64/-/esbuild-linux-arm64-0.15.5.tgz", + "integrity": "sha512-7EgFyP2zjO065XTfdCxiXVEk+f83RQ1JsryN1X/VSX2li9rnHAt2swRbpoz5Vlrl6qjHrCmq5b6yxD13z6RheA==", + "dev": true, + "optional": true + }, + "esbuild-linux-mips64le": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-linux-mips64le/-/esbuild-linux-mips64le-0.15.5.tgz", + "integrity": "sha512-KdnSkHxWrJ6Y40ABu+ipTZeRhFtc8dowGyFsZY5prsmMSr1ZTG9zQawguN4/tunJ0wy3+kD54GaGwdcpwWAvZQ==", + "dev": true, + "optional": true + }, + "esbuild-linux-ppc64le": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-linux-ppc64le/-/esbuild-linux-ppc64le-0.15.5.tgz", + "integrity": "sha512-QdRHGeZ2ykl5P0KRmfGBZIHmqcwIsUKWmmpZTOq573jRWwmpfRmS7xOhmDHBj9pxv+6qRMH8tLr2fe+ZKQvCYw==", + "dev": true, + "optional": true + }, + "esbuild-linux-riscv64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-linux-riscv64/-/esbuild-linux-riscv64-0.15.5.tgz", + "integrity": "sha512-p+WE6RX+jNILsf+exR29DwgV6B73khEQV0qWUbzxaycxawZ8NE0wA6HnnTxbiw5f4Gx9sJDUBemh9v49lKOORA==", + "dev": true, + "optional": true + }, + "esbuild-linux-s390x": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-linux-s390x/-/esbuild-linux-s390x-0.15.5.tgz", + "integrity": "sha512-J2ngOB4cNzmqLHh6TYMM/ips8aoZIuzxJnDdWutBw5482jGXiOzsPoEF4j2WJ2mGnm7FBCO4StGcwzOgic70JQ==", + "dev": true, + "optional": true + }, + "esbuild-netbsd-64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-netbsd-64/-/esbuild-netbsd-64-0.15.5.tgz", + "integrity": "sha512-MmKUYGDizYjFia0Rwt8oOgmiFH7zaYlsoQ3tIOfPxOqLssAsEgG0MUdRDm5lliqjiuoog8LyDu9srQk5YwWF3w==", + "dev": true, + "optional": true + }, + "esbuild-openbsd-64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-openbsd-64/-/esbuild-openbsd-64-0.15.5.tgz", + "integrity": "sha512-2mMFfkLk3oPWfopA9Plj4hyhqHNuGyp5KQyTT9Rc8hFd8wAn5ZrbJg+gNcLMo2yzf8Uiu0RT6G9B15YN9WQyMA==", + "dev": true, + "optional": true + }, + "esbuild-sunos-64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-sunos-64/-/esbuild-sunos-64-0.15.5.tgz", + "integrity": "sha512-2sIzhMUfLNoD+rdmV6AacilCHSxZIoGAU2oT7XmJ0lXcZWnCvCtObvO6D4puxX9YRE97GodciRGDLBaiC6x1SA==", + "dev": true, + "optional": true + }, + "esbuild-windows-32": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-windows-32/-/esbuild-windows-32-0.15.5.tgz", + "integrity": "sha512-e+duNED9UBop7Vnlap6XKedA/53lIi12xv2ebeNS4gFmu7aKyTrok7DPIZyU5w/ftHD4MUDs5PJUkQPP9xJRzg==", + "dev": true, + "optional": true + }, + "esbuild-windows-64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-windows-64/-/esbuild-windows-64-0.15.5.tgz", + "integrity": "sha512-v+PjvNtSASHOjPDMIai9Yi+aP+Vwox+3WVdg2JB8N9aivJ7lyhp4NVU+J0MV2OkWFPnVO8AE/7xH+72ibUUEnw==", + "dev": true, + "optional": true + }, + "esbuild-windows-arm64": { + "version": "0.15.5", + "resolved": "https://registry.npmjs.org/esbuild-windows-arm64/-/esbuild-windows-arm64-0.15.5.tgz", + "integrity": "sha512-Yz8w/D8CUPYstvVQujByu6mlf48lKmXkq6bkeSZZxTA626efQOJb26aDGLzmFWx6eg/FwrXgt6SZs9V8Pwy/aA==", + "dev": true, + "optional": true + }, "escape-string-regexp": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", diff --git a/package.json b/package.json index 63e0d01..6ecb1fa 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,8 @@ "test": "node --test ./test.mjs" }, "devDependencies": { + "@types/node": "^18.7.13", + "esbuild": "^0.15.5", "google-closure-compiler": "^20220601.0.0" } } diff --git a/src/buffer.js b/src/buffer.js new file mode 100644 index 0000000..8139204 --- /dev/null +++ b/src/buffer.js @@ -0,0 +1,23 @@ + +/** + * @param {Uint8Array} bytes + * @param {string} encoding + * @return {string} + */ +export function decodeBuffer(bytes, encoding) { + /** @type {Buffer} */ + let b; + if (bytes instanceof Buffer) { + b = bytes; + } else { + b = Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength); + } + return b.toString(/** @type {BufferEncoding} */(encoding)); +} + + +/** + * @param {string} string + * @return {Uint8Array} + */ +export const encodeBuffer = (string) => Buffer.from(string); diff --git a/src/lowlevel.js b/src/lowlevel.js new file mode 100644 index 0000000..cf585b3 --- /dev/null +++ b/src/lowlevel.js @@ -0,0 +1,141 @@ + +/** + * @param {Uint8Array} bytes + * @return {string} + */ +export function decodeFallback(bytes) { + let inputIndex = 0; + + // Create a working buffer for UTF-16 code points, but don't generate one + // which is too large for small input sizes. UTF-8 to UCS-16 conversion is + // going to be at most 1:1, if all code points are ASCII. The other extreme + // is 4-byte UTF-8, which results in two UCS-16 points, but this is still 50% + // fewer entries in the output. + const pendingSize = Math.min(256 * 256, bytes.length + 1); + const pending = new Uint16Array(pendingSize); + const chunks = []; + let pendingIndex = 0; + + for (; ;) { + const more = inputIndex < bytes.length; + + // If there's no more data or there'd be no room for two UTF-16 values, + // create a chunk. This isn't done at the end by simply slicing the data + // into equal sized chunks as we might hit a surrogate pair. + if (!more || (pendingIndex >= pendingSize - 1)) { + // nb. .apply and friends are *really slow*. Low-hanging fruit is to + // expand this to literally pass pending[0], pending[1], ... etc, but + // the output code expands pretty fast in this case. + // These extra vars get compiled out: they're just to make TS happy. + // Turns out you can pass an ArrayLike to .apply(). + const subarray = pending.subarray(0, pendingIndex); + const arraylike = /** @type {number[]} */ (/** @type {unknown} */ (subarray)); + chunks.push(String.fromCharCode.apply(null, arraylike)); + + if (!more) { + return chunks.join(''); + } + + // Move the buffer forward and create another chunk. + bytes = bytes.subarray(inputIndex); + inputIndex = 0; + pendingIndex = 0; + } + + // The native TextDecoder will generate "REPLACEMENT CHARACTER" where the + // input data is invalid. Here, we blindly parse the data even if it's + // wrong: e.g., if a 3-byte sequence doesn't have two valid continuations. + + const byte1 = bytes[inputIndex++]; + if ((byte1 & 0x80) === 0) { // 1-byte or null + pending[pendingIndex++] = byte1; + } else if ((byte1 & 0xe0) === 0xc0) { // 2-byte + const byte2 = bytes[inputIndex++] & 0x3f; + pending[pendingIndex++] = ((byte1 & 0x1f) << 6) | byte2; + } else if ((byte1 & 0xf0) === 0xe0) { // 3-byte + const byte2 = bytes[inputIndex++] & 0x3f; + const byte3 = bytes[inputIndex++] & 0x3f; + pending[pendingIndex++] = ((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3; + } else if ((byte1 & 0xf8) === 0xf0) { // 4-byte + const byte2 = bytes[inputIndex++] & 0x3f; + const byte3 = bytes[inputIndex++] & 0x3f; + const byte4 = bytes[inputIndex++] & 0x3f; + + // this can be > 0xffff, so possibly generate surrogates + let codepoint = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4; + if (codepoint > 0xffff) { + // codepoint &= ~0x10000; + codepoint -= 0x10000; + pending[pendingIndex++] = (codepoint >>> 10) & 0x3ff | 0xd800; + codepoint = 0xdc00 | codepoint & 0x3ff; + } + pending[pendingIndex++] = codepoint; + } else { + // invalid initial byte + } + } +} + + +/** + * @param {string} string + * @return {Uint8Array} + */ +export function encodeFallback(string) { + let pos = 0; + const len = string.length; + + let at = 0; // output position + let tlen = Math.max(32, len + (len >>> 1) + 7); // 1.5x size + let target = new Uint8Array((tlen >>> 3) << 3); // ... but at 8 byte offset + + while (pos < len) { + let value = string.charCodeAt(pos++); + if (value >= 0xd800 && value <= 0xdbff) { + // high surrogate + if (pos < len) { + const extra = string.charCodeAt(pos); + if ((extra & 0xfc00) === 0xdc00) { + ++pos; + value = ((value & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000; + } + } + if (value >= 0xd800 && value <= 0xdbff) { + continue; // drop lone surrogate + } + } + + // expand the buffer if we couldn't write 4 bytes + if (at + 4 > target.length) { + tlen += 8; // minimum extra + tlen *= (1.0 + (pos / string.length) * 2); // take 2x the remaining + tlen = (tlen >>> 3) << 3; // 8 byte offset + + const update = new Uint8Array(tlen); + update.set(target); + target = update; + } + + if ((value & 0xffffff80) === 0) { // 1-byte + target[at++] = value; // ASCII + continue; + } else if ((value & 0xfffff800) === 0) { // 2-byte + target[at++] = ((value >>> 6) & 0x1f) | 0xc0; + } else if ((value & 0xffff0000) === 0) { // 3-byte + target[at++] = ((value >>> 12) & 0x0f) | 0xe0; + target[at++] = ((value >>> 6) & 0x3f) | 0x80; + } else if ((value & 0xffe00000) === 0) { // 4-byte + target[at++] = ((value >>> 18) & 0x07) | 0xf0; + target[at++] = ((value >>> 12) & 0x3f) | 0x80; + target[at++] = ((value >>> 6) & 0x3f) | 0x80; + } else { + continue; // out of range + } + + target[at++] = (value & 0x3f) | 0x80; + } + + // Use subarray if slice isn't supported (IE11). This will use more memory + // because the original array still exists. + return target.slice ? target.slice(0, at) : target.subarray(0, at); +} diff --git a/src/o-decoder.js b/src/o-decoder.js new file mode 100644 index 0000000..9b8d06b --- /dev/null +++ b/src/o-decoder.js @@ -0,0 +1,78 @@ +import { decodeBuffer } from './buffer.js'; +import { decodeFallback } from './lowlevel.js'; +import { failedToString, maybeThrowFailedToOption } from './shared.js'; +import { hasBufferFrom } from './support.js'; +import { decodeSyncXHR } from './xhr.js'; + +const trySyncXHR = !hasBufferFrom && (typeof Blob === 'function' && typeof URL === 'function' && typeof URL.createObjectURL === 'function'); +const validUtfLabels = ['utf-8', 'utf8', 'unicode-1-1-utf-8']; + +/** @type {(bytes: Uint8Array, encoding: string) => string} */ +let decodeImpl = decodeFallback; +if (hasBufferFrom) { + decodeImpl = decodeBuffer; +} else if (trySyncXHR) { + decodeImpl = (string) => { + try { + return decodeSyncXHR(string); + } catch (e) { + return decodeFallback(string); + } + }; +} + + +const errorPrefix = `${failedToString} construct 'TextDecoder': the `; + + +/** + * @constructor + * @param {string=} utfLabel + * @param {{fatal: boolean}=} options + */ +export function FastTextDecoder(utfLabel = 'utf-8', options) { + maybeThrowFailedToOption(options && options.fatal, `construct 'TextDecoder'`, 'fatal'); + + /** @type {boolean} */ + let ok; + if (hasBufferFrom) { + ok = Buffer.isEncoding(utfLabel); + } else { + ok = validUtfLabels.indexOf(utfLabel.toLowerCase()) !== -1; + } + if (!ok) { + throw new RangeError(`${errorPrefix} encoding label provided ('${utfLabel}') is invalid.`); + } + + this.encoding = utfLabel; + this.fatal = false; + this.ignoreBOM = false; +} + +/** + * @param {(ArrayBuffer|ArrayBufferView)} buffer + * @param {{stream: boolean}=} options + * @return {string} + */ +FastTextDecoder.prototype.decode = function (buffer, options) { + maybeThrowFailedToOption(options && options.stream, 'decode', 'stream'); + + let bytes; + + if (buffer instanceof Uint8Array) { + // Accept Uint8Array instances as-is. This is also a Node buffer. + bytes = buffer; + } else if (buffer['buffer'] instanceof ArrayBuffer) { + // Look for ArrayBufferView, which isn't a real type, but basically + // represents all the valid TypedArray types plus DataView. They all have + // ".buffer" as an instance of ArrayBuffer. + bytes = new Uint8Array(/** @type {ArrayBufferView} */(buffer).buffer); + } else { + // The only other valid argument here is that "buffer" is an ArrayBuffer. + // We also try to convert anything else passed to a Uint8Array, as this + // catches anything that's array-like. Native code would throw here. + bytes = new Uint8Array(/** @type {any} */(buffer)); + } + + return decodeImpl(bytes, this.encoding); +}; diff --git a/src/o-encoder.js b/src/o-encoder.js new file mode 100644 index 0000000..9ada541 --- /dev/null +++ b/src/o-encoder.js @@ -0,0 +1,25 @@ +import { encodeBuffer } from './buffer.js'; +import { encodeFallback } from './lowlevel.js'; +import { maybeThrowFailedToOption } from './shared.js'; +import { hasBufferFrom } from './support.js'; + +export const encodeImpl = hasBufferFrom ? encodeFallback : encodeBuffer; + +/** + * @constructor + */ +export function FastTextEncoder() { + // This does not accept an encoding, and always uses UTF-8: + // https://www.w3.org/TR/encoding/#dom-textencoder + this.encoding = 'utf-8'; +} + +/** + * @param {string} string + * @param {{stream: boolean}=} options + * @return {Uint8Array} + */ +FastTextEncoder.prototype.encode = function (string, options) { + maybeThrowFailedToOption(options && options.stream, 'encode', 'stream'); + return encodeImpl(string); +}; diff --git a/src/polyfill.js b/src/polyfill.js new file mode 100644 index 0000000..241bc71 --- /dev/null +++ b/src/polyfill.js @@ -0,0 +1,11 @@ + +import { FastTextEncoder } from './o-encoder.js'; +import { FastTextDecoder } from './o-decoder.js'; + +/** @type {object} */ +const scope = typeof window !== 'undefined' ? window : (typeof global !== 'undefined' ? global : this); + +scope['TextEncoder'] = scope['TextEncoder'] || FastTextEncoder; +scope['TextDecoder'] = scope['TextDecoder'] || FastTextDecoder; + +export {}; diff --git a/src/shared.js b/src/shared.js new file mode 100644 index 0000000..f225fa3 --- /dev/null +++ b/src/shared.js @@ -0,0 +1,13 @@ + +export const failedToString = 'Failed to '; + +/** + * @param {boolean|undefined} check + * @param {string} operation + * @param {string} fieldName + */ +export const maybeThrowFailedToOption = (check, operation, fieldName) => { + if (check) { + throw new Error(`${failedToString}${operation}: the '${fieldName}' option is unsupported.`); + } +}; \ No newline at end of file diff --git a/src/support.js b/src/support.js new file mode 100644 index 0000000..2551dc7 --- /dev/null +++ b/src/support.js @@ -0,0 +1,2 @@ + +export const hasBufferFrom = (typeof Buffer === 'function' && Buffer.from); \ No newline at end of file diff --git a/src/xhr.js b/src/xhr.js new file mode 100644 index 0000000..159e1ea --- /dev/null +++ b/src/xhr.js @@ -0,0 +1,29 @@ + +/** + * This is a horrible hack which works in some old browsers. We can tell them to decode bytes via + * sync XHR. + * + * Throws if fails. Should be wrapped in something to check that. + * + * @param {Uint8Array} bytes + * @return {string} + */ +export function decodeSyncXHR(bytes) { + let u; + + // This hack will fail in non-Edgium Edge because sync XHRs are disabled (and + // possibly in other places), so ensure there's a fallback call. + try { + const b = new Blob([bytes], { type: 'text/plain;charset=UTF-8' }); + u = URL.createObjectURL(b); + + const x = new XMLHttpRequest(); + x.open('GET', u, false); + x.send(); + return x.responseText; + } finally { + if (u) { + URL.revokeObjectURL(u); + } + } +} \ No newline at end of file diff --git a/test.mjs b/test.mjs index 0dc9089..f0ec83d 100644 --- a/test.mjs +++ b/test.mjs @@ -17,16 +17,16 @@ import assert from 'node:assert'; * @param {typeof TextEncoder} TextEncoder * @param {typeof TextDecoder} TextDecoder */ -export function tests(isNative, TextEncoder, TextDecoder) { +export async function tests(isNative, TextEncoder, TextDecoder) { const dec = new TextDecoder(); const enc = new TextEncoder('utf-8'); console.info('running', { isNative, TextEncoder, TextDecoder }); - test(isNative ? 'native suite' : 'polyfill suite', async (c) => { + await test(isNative ? 'native suite' : 'polyfill suite', async (c) => { const test = c.test.bind(c); - await test('really large string', () => { + await test('really large string', async () => { const chunks = new Array(64); for (let i = 0; i < chunks.length; ++i) { const s = new Array(65535).fill('x'.charCodeAt(0)); @@ -106,6 +106,10 @@ export function tests(isNative, TextEncoder, TextDecoder) { assert.deepEqual(dec.decode(buffer), s); }); + await test('nodejs encodings', () => { + const d = new TextDecoder('utf16le'); + }); + }); await test('encoder', async (c) => { @@ -160,5 +164,5 @@ export function tests(isNative, TextEncoder, TextDecoder) { } -tests(true, NativeTextEncoder, NativeTextDecoder); -tests(false, TextEncoder, TextDecoder); +await tests(true, NativeTextEncoder, NativeTextDecoder); +await tests(false, TextEncoder, TextDecoder); diff --git a/text.js b/text.js index d180e9c..895090f 100644 --- a/text.js +++ b/text.js @@ -28,6 +28,17 @@ if (scope['TextEncoder'] && scope['TextDecoder']) { return false; } +// Decoding a string is pretty slow, but use alternative options where possible. +let decodeImpl = decodeFallback; +if (typeof Buffer === 'function' && Buffer.from) { + // Buffer.from was added in Node v5.10.0 (2015-11-17). + decodeImpl = decodeBuffer; +} else if (typeof Blob === 'function' && typeof URL === 'function' && typeof URL.createObjectURL === 'function') { + // Blob and URL.createObjectURL are available from IE10, Safari 6, Chrome 19 + // (all released in 2012), Firefox 19 (2013), ... + decodeImpl = decodeSyncXHR; +} + // used for FastTextDecoder const validUtfLabels = ['utf-8', 'utf8', 'unicode-1-1-utf-8']; @@ -115,27 +126,41 @@ FastTextEncoder.prototype['encode'] = function(string, options={stream: false}) * @param {{fatal: boolean}=} options */ function FastTextDecoder(utfLabel='utf-8', options={fatal: false}) { - if (validUtfLabels.indexOf(utfLabel.toLowerCase()) === -1) { + /** @type {boolean} */ + let ok; + if (decodeImpl === decodeBuffer) { + ok = Buffer.isEncoding(utfLabel); + } else { + ok = validUtfLabels.indexOf(utfLabel.toLowerCase()) !== -1; + } + if (!ok) { throw new RangeError( `Failed to construct 'TextDecoder': The encoding label provided ('${utfLabel}') is invalid.`); } if (options.fatal) { throw new Error(`Failed to construct 'TextDecoder': the 'fatal' option is unsupported.`); } + this.encoding = utfLabel; } -Object.defineProperty(FastTextDecoder.prototype, 'encoding', {value: 'utf-8'}); - Object.defineProperty(FastTextDecoder.prototype, 'fatal', {value: false}); Object.defineProperty(FastTextDecoder.prototype, 'ignoreBOM', {value: false}); /** * @param {!Uint8Array} bytes + * @param {string} encoding * @return {string} */ -function decodeBuffer(bytes) { - return Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString('utf-8'); +function decodeBuffer(bytes, encoding) { + /** @type {Buffer} */ + let b; + if (bytes instanceof Buffer) { + b = bytes; + } else { + b = Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength); + } + return b.toString(encoding); } /** @@ -237,17 +262,6 @@ function decodeFallback(bytes) { } } -// Decoding a string is pretty slow, but use alternative options where possible. -let decodeImpl = decodeFallback; -if (typeof Buffer === 'function' && Buffer.from) { - // Buffer.from was added in Node v5.10.0 (2015-11-17). - decodeImpl = decodeBuffer; -} else if (typeof Blob === 'function' && typeof URL === 'function' && typeof URL.createObjectURL === 'function') { - // Blob and URL.createObjectURL are available from IE10, Safari 6, Chrome 19 - // (all released in 2012), Firefox 19 (2013), ... - decodeImpl = decodeSyncXHR; -} - /** * @param {(!ArrayBuffer|!ArrayBufferView)} buffer * @param {{stream: boolean}=} options @@ -261,7 +275,7 @@ FastTextDecoder.prototype['decode'] = function(buffer, options={stream: false}) let bytes; if (buffer instanceof Uint8Array) { - // Accept Uint8Array instances as-is. + // Accept Uint8Array instances as-is. This is also a Node buffer. bytes = buffer; } else if (buffer.buffer instanceof ArrayBuffer) { // Look for ArrayBufferView, which isn't a real type, but basically @@ -275,7 +289,7 @@ FastTextDecoder.prototype['decode'] = function(buffer, options={stream: false}) bytes = new Uint8Array(buffer); } - return decodeImpl(/** @type {!Uint8Array} */ (bytes)); + return decodeImpl(/** @type {!Uint8Array} */ (bytes), this.encoding); } scope['TextEncoder'] = FastTextEncoder; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..78d3369 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "checkJs": true, + "noEmit": true, + + // if you'd like to warn if you're using modern features, change these + // both to e.g., "es2017" + "module": "esnext", + "target": "esnext", + + // configure as you like: these are my preferred defaults! + "strict": true, + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true, + + // "strict" implies this, but you'll want to enable it when you're + // ready: it's a huge reason your project will start complaining + "noImplicitAny": false, + }, + "include": [ + // include the JS files you'd like to check here + "src/**/*.js", + ], +} \ No newline at end of file