From c53e51749a5c510b8b0dd43aaa3c2a6b6638215f Mon Sep 17 00:00:00 2001 From: Sophie Alpert Date: Mon, 26 Aug 2024 17:46:21 -0700 Subject: [PATCH 1/4] Wrap thrown errors in JS Error objects with stacks We noticed in our usage of this package that thrown errors (eg: "Unknown frame descriptor") don't have a useful .stack property. This makes debugging harder. It seems this is expected behavior for errors created via napi at least on V8; it seems that the best way to remedy this is to recreate Error objects on the JS side? --- index.js | 18 ++++++++++++++++-- test/index.test.js | 13 +++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index e411226..6c15cbb 100644 --- a/index.js +++ b/index.js @@ -151,5 +151,19 @@ if (!nativeBinding) { const { compress, decompress } = nativeBinding; -module.exports.compress = compress; -module.exports.decompress = decompress; +// Error objects created via napi don't have JS stacks; wrap them so .stack is present +// https://github.com/nodejs/node/issues/25318#issuecomment-451068073 +module.exports.compress = async function (data) { + try { + return await compress(data); + } catch (e) { + throw new Error(`zstd: ${e.message}`); + } +}; +module.exports.decompress = async function (data) { + try { + return await decompress(data); + } catch (e) { + throw new Error(`zstd: ${e.message}`); + } +}; diff --git a/test/index.test.js b/test/index.test.js index 781f005..cd88abf 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -37,4 +37,17 @@ describe('zstd', () => { }); }); }); + + describe('#decompress', () => { + context('when decompressing invalid data', () => { + it('includes a stack trace', async () => { + try { + await decompress(Buffer.from('invalid')); + } catch (error) { + expect(error.message).to.be('zstd: Unknown frame descriptor'); + expect(error.stack).to.match(/at decompress/); + } + }); + }); + }); }); From 16e02927a7f4fa8a19fc44df88c6624ba13d6345 Mon Sep 17 00:00:00 2001 From: Sophie Alpert Date: Tue, 27 Aug 2024 16:24:07 -0700 Subject: [PATCH 2/4] Apply suggestions from code review Co-authored-by: Durran Jordan --- test/index.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/index.test.js b/test/index.test.js index cd88abf..7558ac3 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -44,8 +44,8 @@ describe('zstd', () => { try { await decompress(Buffer.from('invalid')); } catch (error) { - expect(error.message).to.be('zstd: Unknown frame descriptor'); - expect(error.stack).to.match(/at decompress/); + expect(error.message).to.equal('zstd: Unknown frame descriptor'); + expect(error.stack).to.match(/module.exports.decompress/); } }); }); From 41ef5c2475dcb4494541c5b9e224cce61faeac69 Mon Sep 17 00:00:00 2001 From: Sophie Alpert Date: Tue, 27 Aug 2024 17:03:48 -0700 Subject: [PATCH 3/4] move bindings to a different file, shorten function names --- bindings.js | 316 +++++++++++++++++++++++++++++++++++++++++++++ index.d.ts | 4 +- index.js | 163 ++--------------------- package.json | 5 +- test/index.test.js | 2 +- 5 files changed, 330 insertions(+), 160 deletions(-) create mode 100644 bindings.js diff --git a/bindings.js b/bindings.js new file mode 100644 index 0000000..37499e4 --- /dev/null +++ b/bindings.js @@ -0,0 +1,316 @@ +/* tslint:disable */ +/* eslint-disable */ +/* prettier-ignore */ + +/* auto-generated by NAPI-RS */ + +const { existsSync, readFileSync } = require('fs') +const { join } = require('path') + +const { platform, arch } = process + +let nativeBinding = null +let localFileExisted = false +let loadError = null + +function isMusl() { + // For Node 10 + if (!process.report || typeof process.report.getReport !== 'function') { + try { + const lddPath = require('child_process').execSync('which ldd').toString().trim() + return readFileSync(lddPath, 'utf8').includes('musl') + } catch (e) { + return true + } + } else { + const { glibcVersionRuntime } = process.report.getReport().header + return !glibcVersionRuntime + } +} + +switch (platform) { + case 'android': + switch (arch) { + case 'arm64': + localFileExisted = existsSync(join(__dirname, 'zstd.android-arm64.node')) + try { + if (localFileExisted) { + nativeBinding = require('./zstd.android-arm64.node') + } else { + nativeBinding = require('@mongodb-js/zstd-android-arm64') + } + } catch (e) { + loadError = e + } + break + case 'arm': + localFileExisted = existsSync(join(__dirname, 'zstd.android-arm-eabi.node')) + try { + if (localFileExisted) { + nativeBinding = require('./zstd.android-arm-eabi.node') + } else { + nativeBinding = require('@mongodb-js/zstd-android-arm-eabi') + } + } catch (e) { + loadError = e + } + break + default: + throw new Error(`Unsupported architecture on Android ${arch}`) + } + break + case 'win32': + switch (arch) { + case 'x64': + localFileExisted = existsSync( + join(__dirname, 'zstd.win32-x64-msvc.node') + ) + try { + if (localFileExisted) { + nativeBinding = require('./zstd.win32-x64-msvc.node') + } else { + nativeBinding = require('@mongodb-js/zstd-win32-x64-msvc') + } + } catch (e) { + loadError = e + } + break + case 'ia32': + localFileExisted = existsSync( + join(__dirname, 'zstd.win32-ia32-msvc.node') + ) + try { + if (localFileExisted) { + nativeBinding = require('./zstd.win32-ia32-msvc.node') + } else { + nativeBinding = require('@mongodb-js/zstd-win32-ia32-msvc') + } + } catch (e) { + loadError = e + } + break + case 'arm64': + localFileExisted = existsSync( + join(__dirname, 'zstd.win32-arm64-msvc.node') + ) + try { + if (localFileExisted) { + nativeBinding = require('./zstd.win32-arm64-msvc.node') + } else { + nativeBinding = require('@mongodb-js/zstd-win32-arm64-msvc') + } + } catch (e) { + loadError = e + } + break + default: + throw new Error(`Unsupported architecture on Windows: ${arch}`) + } + break + case 'darwin': + localFileExisted = existsSync(join(__dirname, 'zstd.darwin-universal.node')) + try { + if (localFileExisted) { + nativeBinding = require('./zstd.darwin-universal.node') + } else { + nativeBinding = require('@mongodb-js/zstd-darwin-universal') + } + break + } catch {} + switch (arch) { + case 'x64': + localFileExisted = existsSync(join(__dirname, 'zstd.darwin-x64.node')) + try { + if (localFileExisted) { + nativeBinding = require('./zstd.darwin-x64.node') + } else { + nativeBinding = require('@mongodb-js/zstd-darwin-x64') + } + } catch (e) { + loadError = e + } + break + case 'arm64': + localFileExisted = existsSync( + join(__dirname, 'zstd.darwin-arm64.node') + ) + try { + if (localFileExisted) { + nativeBinding = require('./zstd.darwin-arm64.node') + } else { + nativeBinding = require('@mongodb-js/zstd-darwin-arm64') + } + } catch (e) { + loadError = e + } + break + default: + throw new Error(`Unsupported architecture on macOS: ${arch}`) + } + break + case 'freebsd': + if (arch !== 'x64') { + throw new Error(`Unsupported architecture on FreeBSD: ${arch}`) + } + localFileExisted = existsSync(join(__dirname, 'zstd.freebsd-x64.node')) + try { + if (localFileExisted) { + nativeBinding = require('./zstd.freebsd-x64.node') + } else { + nativeBinding = require('@mongodb-js/zstd-freebsd-x64') + } + } catch (e) { + loadError = e + } + break + case 'linux': + switch (arch) { + case 'x64': + if (isMusl()) { + localFileExisted = existsSync( + join(__dirname, 'zstd.linux-x64-musl.node') + ) + try { + if (localFileExisted) { + nativeBinding = require('./zstd.linux-x64-musl.node') + } else { + nativeBinding = require('@mongodb-js/zstd-linux-x64-musl') + } + } catch (e) { + loadError = e + } + } else { + localFileExisted = existsSync( + join(__dirname, 'zstd.linux-x64-gnu.node') + ) + try { + if (localFileExisted) { + nativeBinding = require('./zstd.linux-x64-gnu.node') + } else { + nativeBinding = require('@mongodb-js/zstd-linux-x64-gnu') + } + } catch (e) { + loadError = e + } + } + break + case 'arm64': + if (isMusl()) { + localFileExisted = existsSync( + join(__dirname, 'zstd.linux-arm64-musl.node') + ) + try { + if (localFileExisted) { + nativeBinding = require('./zstd.linux-arm64-musl.node') + } else { + nativeBinding = require('@mongodb-js/zstd-linux-arm64-musl') + } + } catch (e) { + loadError = e + } + } else { + localFileExisted = existsSync( + join(__dirname, 'zstd.linux-arm64-gnu.node') + ) + try { + if (localFileExisted) { + nativeBinding = require('./zstd.linux-arm64-gnu.node') + } else { + nativeBinding = require('@mongodb-js/zstd-linux-arm64-gnu') + } + } catch (e) { + loadError = e + } + } + break + case 'arm': + if (isMusl()) { + localFileExisted = existsSync( + join(__dirname, 'zstd.linux-arm-musleabihf.node') + ) + try { + if (localFileExisted) { + nativeBinding = require('./zstd.linux-arm-musleabihf.node') + } else { + nativeBinding = require('@mongodb-js/zstd-linux-arm-musleabihf') + } + } catch (e) { + loadError = e + } + } else { + localFileExisted = existsSync( + join(__dirname, 'zstd.linux-arm-gnueabihf.node') + ) + try { + if (localFileExisted) { + nativeBinding = require('./zstd.linux-arm-gnueabihf.node') + } else { + nativeBinding = require('@mongodb-js/zstd-linux-arm-gnueabihf') + } + } catch (e) { + loadError = e + } + } + break + case 'riscv64': + if (isMusl()) { + localFileExisted = existsSync( + join(__dirname, 'zstd.linux-riscv64-musl.node') + ) + try { + if (localFileExisted) { + nativeBinding = require('./zstd.linux-riscv64-musl.node') + } else { + nativeBinding = require('@mongodb-js/zstd-linux-riscv64-musl') + } + } catch (e) { + loadError = e + } + } else { + localFileExisted = existsSync( + join(__dirname, 'zstd.linux-riscv64-gnu.node') + ) + try { + if (localFileExisted) { + nativeBinding = require('./zstd.linux-riscv64-gnu.node') + } else { + nativeBinding = require('@mongodb-js/zstd-linux-riscv64-gnu') + } + } catch (e) { + loadError = e + } + } + break + case 's390x': + localFileExisted = existsSync( + join(__dirname, 'zstd.linux-s390x-gnu.node') + ) + try { + if (localFileExisted) { + nativeBinding = require('./zstd.linux-s390x-gnu.node') + } else { + nativeBinding = require('@mongodb-js/zstd-linux-s390x-gnu') + } + } catch (e) { + loadError = e + } + break + default: + throw new Error(`Unsupported architecture on Linux: ${arch}`) + } + break + default: + throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`) +} + +if (!nativeBinding) { + if (loadError) { + throw loadError + } + throw new Error(`Failed to load native binding`) +} + +const { compress, decompress } = nativeBinding + +module.exports.compress = compress +module.exports.decompress = decompress diff --git a/index.d.ts b/index.d.ts index 8d16577..a56ed6b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -3,5 +3,5 @@ /* auto-generated by NAPI-RS */ -export function compress(data: Buffer, level?: number | undefined | null): Promise -export function decompress(data: Buffer): Promise +export declare function compress(data: Buffer, level?: number | undefined | null): Promise +export declare function decompress(data: Buffer): Promise diff --git a/index.js b/index.js index 6c15cbb..422ca93 100644 --- a/index.js +++ b/index.js @@ -1,168 +1,21 @@ -const { existsSync, readFileSync } = require('fs'); -const { join } = require('path'); +// NB: If you update any type signatures to diverge from bindings itself, make +// sure to update how index.d.ts is generated (napi build --dts ...) -const { platform, arch } = process; - -let nativeBinding = null; -let localFileExisted = false; -let loadError = null; - -function isMusl() { - // For Node 10 - if (!process.report || typeof process.report.getReport !== 'function') { - try { - return readFileSync('/usr/bin/ldd', 'utf8').includes('musl'); - } catch (e) { - return true; - } - } else { - const { glibcVersionRuntime } = process.report.getReport().header; - return !glibcVersionRuntime; - } -} - -switch (platform) { - case 'win32': - switch (arch) { - case 'x64': - localFileExisted = existsSync(join(__dirname, 'zstd.win32-x64-msvc.node')); - try { - if (localFileExisted) { - nativeBinding = require('./zstd.win32-x64-msvc.node'); - } else { - nativeBinding = require('@mongodb-js/zstd-win32-x64-msvc'); - } - } catch (e) { - loadError = e; - } - break; - default: - throw new Error(`Unsupported architecture on Windows: ${arch}`); - } - break; - case 'darwin': - switch (arch) { - case 'x64': - localFileExisted = existsSync(join(__dirname, 'zstd.darwin-x64.node')); - try { - if (localFileExisted) { - nativeBinding = require('./zstd.darwin-x64.node'); - } else { - nativeBinding = require('@mongodb-js/zstd-darwin-x64'); - } - } catch (e) { - loadError = e; - } - break; - case 'arm64': - localFileExisted = existsSync(join(__dirname, 'zstd.darwin-arm64.node')); - try { - if (localFileExisted) { - nativeBinding = require('./zstd.darwin-arm64.node'); - } else { - nativeBinding = require('@mongodb-js/zstd-darwin-arm64'); - } - } catch (e) { - loadError = e; - } - break; - default: - throw new Error(`Unsupported architecture on macOS: ${arch}`); - } - break; - case 'linux': - switch (arch) { - case 'x64': - if (isMusl()) { - localFileExisted = existsSync(join(__dirname, 'zstd.linux-x64-musl.node')); - try { - if (localFileExisted) { - nativeBinding = require('./zstd.linux-x64-musl.node'); - } else { - nativeBinding = require('@mongodb-js/zstd-linux-x64-musl'); - } - } catch (e) { - loadError = e; - } - } else { - localFileExisted = existsSync(join(__dirname, 'zstd.linux-x64-gnu.node')); - try { - if (localFileExisted) { - nativeBinding = require('./zstd.linux-x64-gnu.node'); - } else { - nativeBinding = require('@mongodb-js/zstd-linux-x64-gnu'); - } - } catch (e) { - loadError = e; - } - } - break; - case 'arm64': - if (isMusl()) { - localFileExisted = existsSync(join(__dirname, 'zstd.linux-arm64-musl.node')); - try { - if (localFileExisted) { - nativeBinding = require('./zstd.linux-arm64-musl.node'); - } else { - nativeBinding = require('@mongodb-js/zstd-linux-arm64-musl'); - } - } catch (e) { - loadError = e; - } - } else { - localFileExisted = existsSync(join(__dirname, 'zstd.linux-arm64-gnu.node')); - try { - if (localFileExisted) { - nativeBinding = require('./zstd.linux-arm64-gnu.node'); - } else { - nativeBinding = require('@mongodb-js/zstd-linux-arm64-gnu'); - } - } catch (e) { - loadError = e; - } - } - break; - case 'arm': - localFileExisted = existsSync(join(__dirname, 'zstd.linux-arm-gnueabihf.node')); - try { - if (localFileExisted) { - nativeBinding = require('./zstd.linux-arm-gnueabihf.node'); - } else { - nativeBinding = require('@mongodb-js/zstd-linux-arm-gnueabihf'); - } - } catch (e) { - loadError = e; - } - break; - default: - throw new Error(`Unsupported architecture on Linux: ${arch}`); - } - break; - default: - throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`); -} - -if (!nativeBinding) { - if (loadError) { - throw loadError; - } - throw new Error(`Failed to load native binding`); -} - -const { compress, decompress } = nativeBinding; +const { compress: _compress, decompress: _decompress } = require('./bindings'); // Error objects created via napi don't have JS stacks; wrap them so .stack is present // https://github.com/nodejs/node/issues/25318#issuecomment-451068073 -module.exports.compress = async function (data) { + +exports.compress = async function compress(data) { try { - return await compress(data); + return await _compress(data); } catch (e) { throw new Error(`zstd: ${e.message}`); } }; -module.exports.decompress = async function (data) { +exports.decompress = async function decompress(data) { try { - return await decompress(data); + return await _decompress(data); } catch (e) { throw new Error(`zstd: ${e.message}`); } diff --git a/package.json b/package.json index ad10054..12c90d3 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ } }, "files": [ + "bindings.js", "index.d.ts", "index.js" ], @@ -44,8 +45,8 @@ }, "scripts": { "artifacts": "napi artifacts", - "build": "napi build --platform --release", - "build:debug": "napi build --platform", + "build": "napi build --js bindings.js --platform --release", + "build:debug": "napi build --js bindings.js --platform", "format:js": "prettier --config ./package.json --write *.js", "format:rs": "cargo fmt", "prepublishOnly": "napi prepublish -t npm", diff --git a/test/index.test.js b/test/index.test.js index 7558ac3..8b90fa1 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -45,7 +45,7 @@ describe('zstd', () => { await decompress(Buffer.from('invalid')); } catch (error) { expect(error.message).to.equal('zstd: Unknown frame descriptor'); - expect(error.stack).to.match(/module.exports.decompress/); + expect(error.stack).to.match(/at decompress/); } }); }); From 65fed03bf47f49f90d4841db42ba935ab225992d Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Wed, 28 Aug 2024 19:22:25 +0200 Subject: [PATCH 4/4] refactor: use old index.js in bindings.js --- bindings.js | 281 +++++++++++----------------------------------------- 1 file changed, 60 insertions(+), 221 deletions(-) diff --git a/bindings.js b/bindings.js index 37499e4..e411226 100644 --- a/bindings.js +++ b/bindings.js @@ -1,316 +1,155 @@ -/* tslint:disable */ -/* eslint-disable */ -/* prettier-ignore */ +const { existsSync, readFileSync } = require('fs'); +const { join } = require('path'); -/* auto-generated by NAPI-RS */ +const { platform, arch } = process; -const { existsSync, readFileSync } = require('fs') -const { join } = require('path') - -const { platform, arch } = process - -let nativeBinding = null -let localFileExisted = false -let loadError = null +let nativeBinding = null; +let localFileExisted = false; +let loadError = null; function isMusl() { // For Node 10 if (!process.report || typeof process.report.getReport !== 'function') { try { - const lddPath = require('child_process').execSync('which ldd').toString().trim() - return readFileSync(lddPath, 'utf8').includes('musl') + return readFileSync('/usr/bin/ldd', 'utf8').includes('musl'); } catch (e) { - return true + return true; } } else { - const { glibcVersionRuntime } = process.report.getReport().header - return !glibcVersionRuntime + const { glibcVersionRuntime } = process.report.getReport().header; + return !glibcVersionRuntime; } } switch (platform) { - case 'android': - switch (arch) { - case 'arm64': - localFileExisted = existsSync(join(__dirname, 'zstd.android-arm64.node')) - try { - if (localFileExisted) { - nativeBinding = require('./zstd.android-arm64.node') - } else { - nativeBinding = require('@mongodb-js/zstd-android-arm64') - } - } catch (e) { - loadError = e - } - break - case 'arm': - localFileExisted = existsSync(join(__dirname, 'zstd.android-arm-eabi.node')) - try { - if (localFileExisted) { - nativeBinding = require('./zstd.android-arm-eabi.node') - } else { - nativeBinding = require('@mongodb-js/zstd-android-arm-eabi') - } - } catch (e) { - loadError = e - } - break - default: - throw new Error(`Unsupported architecture on Android ${arch}`) - } - break case 'win32': switch (arch) { case 'x64': - localFileExisted = existsSync( - join(__dirname, 'zstd.win32-x64-msvc.node') - ) + localFileExisted = existsSync(join(__dirname, 'zstd.win32-x64-msvc.node')); try { if (localFileExisted) { - nativeBinding = require('./zstd.win32-x64-msvc.node') + nativeBinding = require('./zstd.win32-x64-msvc.node'); } else { - nativeBinding = require('@mongodb-js/zstd-win32-x64-msvc') + nativeBinding = require('@mongodb-js/zstd-win32-x64-msvc'); } } catch (e) { - loadError = e + loadError = e; } - break - case 'ia32': - localFileExisted = existsSync( - join(__dirname, 'zstd.win32-ia32-msvc.node') - ) - try { - if (localFileExisted) { - nativeBinding = require('./zstd.win32-ia32-msvc.node') - } else { - nativeBinding = require('@mongodb-js/zstd-win32-ia32-msvc') - } - } catch (e) { - loadError = e - } - break - case 'arm64': - localFileExisted = existsSync( - join(__dirname, 'zstd.win32-arm64-msvc.node') - ) - try { - if (localFileExisted) { - nativeBinding = require('./zstd.win32-arm64-msvc.node') - } else { - nativeBinding = require('@mongodb-js/zstd-win32-arm64-msvc') - } - } catch (e) { - loadError = e - } - break + break; default: - throw new Error(`Unsupported architecture on Windows: ${arch}`) + throw new Error(`Unsupported architecture on Windows: ${arch}`); } - break + break; case 'darwin': - localFileExisted = existsSync(join(__dirname, 'zstd.darwin-universal.node')) - try { - if (localFileExisted) { - nativeBinding = require('./zstd.darwin-universal.node') - } else { - nativeBinding = require('@mongodb-js/zstd-darwin-universal') - } - break - } catch {} switch (arch) { case 'x64': - localFileExisted = existsSync(join(__dirname, 'zstd.darwin-x64.node')) + localFileExisted = existsSync(join(__dirname, 'zstd.darwin-x64.node')); try { if (localFileExisted) { - nativeBinding = require('./zstd.darwin-x64.node') + nativeBinding = require('./zstd.darwin-x64.node'); } else { - nativeBinding = require('@mongodb-js/zstd-darwin-x64') + nativeBinding = require('@mongodb-js/zstd-darwin-x64'); } } catch (e) { - loadError = e + loadError = e; } - break + break; case 'arm64': - localFileExisted = existsSync( - join(__dirname, 'zstd.darwin-arm64.node') - ) + localFileExisted = existsSync(join(__dirname, 'zstd.darwin-arm64.node')); try { if (localFileExisted) { - nativeBinding = require('./zstd.darwin-arm64.node') + nativeBinding = require('./zstd.darwin-arm64.node'); } else { - nativeBinding = require('@mongodb-js/zstd-darwin-arm64') + nativeBinding = require('@mongodb-js/zstd-darwin-arm64'); } } catch (e) { - loadError = e + loadError = e; } - break + break; default: - throw new Error(`Unsupported architecture on macOS: ${arch}`) - } - break - case 'freebsd': - if (arch !== 'x64') { - throw new Error(`Unsupported architecture on FreeBSD: ${arch}`) + throw new Error(`Unsupported architecture on macOS: ${arch}`); } - localFileExisted = existsSync(join(__dirname, 'zstd.freebsd-x64.node')) - try { - if (localFileExisted) { - nativeBinding = require('./zstd.freebsd-x64.node') - } else { - nativeBinding = require('@mongodb-js/zstd-freebsd-x64') - } - } catch (e) { - loadError = e - } - break + break; case 'linux': switch (arch) { case 'x64': if (isMusl()) { - localFileExisted = existsSync( - join(__dirname, 'zstd.linux-x64-musl.node') - ) + localFileExisted = existsSync(join(__dirname, 'zstd.linux-x64-musl.node')); try { if (localFileExisted) { - nativeBinding = require('./zstd.linux-x64-musl.node') + nativeBinding = require('./zstd.linux-x64-musl.node'); } else { - nativeBinding = require('@mongodb-js/zstd-linux-x64-musl') + nativeBinding = require('@mongodb-js/zstd-linux-x64-musl'); } } catch (e) { - loadError = e + loadError = e; } } else { - localFileExisted = existsSync( - join(__dirname, 'zstd.linux-x64-gnu.node') - ) + localFileExisted = existsSync(join(__dirname, 'zstd.linux-x64-gnu.node')); try { if (localFileExisted) { - nativeBinding = require('./zstd.linux-x64-gnu.node') + nativeBinding = require('./zstd.linux-x64-gnu.node'); } else { - nativeBinding = require('@mongodb-js/zstd-linux-x64-gnu') + nativeBinding = require('@mongodb-js/zstd-linux-x64-gnu'); } } catch (e) { - loadError = e + loadError = e; } } - break + break; case 'arm64': if (isMusl()) { - localFileExisted = existsSync( - join(__dirname, 'zstd.linux-arm64-musl.node') - ) + localFileExisted = existsSync(join(__dirname, 'zstd.linux-arm64-musl.node')); try { if (localFileExisted) { - nativeBinding = require('./zstd.linux-arm64-musl.node') + nativeBinding = require('./zstd.linux-arm64-musl.node'); } else { - nativeBinding = require('@mongodb-js/zstd-linux-arm64-musl') + nativeBinding = require('@mongodb-js/zstd-linux-arm64-musl'); } } catch (e) { - loadError = e + loadError = e; } } else { - localFileExisted = existsSync( - join(__dirname, 'zstd.linux-arm64-gnu.node') - ) + localFileExisted = existsSync(join(__dirname, 'zstd.linux-arm64-gnu.node')); try { if (localFileExisted) { - nativeBinding = require('./zstd.linux-arm64-gnu.node') + nativeBinding = require('./zstd.linux-arm64-gnu.node'); } else { - nativeBinding = require('@mongodb-js/zstd-linux-arm64-gnu') + nativeBinding = require('@mongodb-js/zstd-linux-arm64-gnu'); } } catch (e) { - loadError = e + loadError = e; } } - break + break; case 'arm': - if (isMusl()) { - localFileExisted = existsSync( - join(__dirname, 'zstd.linux-arm-musleabihf.node') - ) - try { - if (localFileExisted) { - nativeBinding = require('./zstd.linux-arm-musleabihf.node') - } else { - nativeBinding = require('@mongodb-js/zstd-linux-arm-musleabihf') - } - } catch (e) { - loadError = e - } - } else { - localFileExisted = existsSync( - join(__dirname, 'zstd.linux-arm-gnueabihf.node') - ) - try { - if (localFileExisted) { - nativeBinding = require('./zstd.linux-arm-gnueabihf.node') - } else { - nativeBinding = require('@mongodb-js/zstd-linux-arm-gnueabihf') - } - } catch (e) { - loadError = e - } - } - break - case 'riscv64': - if (isMusl()) { - localFileExisted = existsSync( - join(__dirname, 'zstd.linux-riscv64-musl.node') - ) - try { - if (localFileExisted) { - nativeBinding = require('./zstd.linux-riscv64-musl.node') - } else { - nativeBinding = require('@mongodb-js/zstd-linux-riscv64-musl') - } - } catch (e) { - loadError = e - } - } else { - localFileExisted = existsSync( - join(__dirname, 'zstd.linux-riscv64-gnu.node') - ) - try { - if (localFileExisted) { - nativeBinding = require('./zstd.linux-riscv64-gnu.node') - } else { - nativeBinding = require('@mongodb-js/zstd-linux-riscv64-gnu') - } - } catch (e) { - loadError = e - } - } - break - case 's390x': - localFileExisted = existsSync( - join(__dirname, 'zstd.linux-s390x-gnu.node') - ) + localFileExisted = existsSync(join(__dirname, 'zstd.linux-arm-gnueabihf.node')); try { if (localFileExisted) { - nativeBinding = require('./zstd.linux-s390x-gnu.node') + nativeBinding = require('./zstd.linux-arm-gnueabihf.node'); } else { - nativeBinding = require('@mongodb-js/zstd-linux-s390x-gnu') + nativeBinding = require('@mongodb-js/zstd-linux-arm-gnueabihf'); } } catch (e) { - loadError = e + loadError = e; } - break + break; default: - throw new Error(`Unsupported architecture on Linux: ${arch}`) + throw new Error(`Unsupported architecture on Linux: ${arch}`); } - break + break; default: - throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`) + throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`); } if (!nativeBinding) { if (loadError) { - throw loadError + throw loadError; } - throw new Error(`Failed to load native binding`) + throw new Error(`Failed to load native binding`); } -const { compress, decompress } = nativeBinding +const { compress, decompress } = nativeBinding; -module.exports.compress = compress -module.exports.decompress = decompress +module.exports.compress = compress; +module.exports.decompress = decompress;