diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 541c8d3..2c94d3a 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -98,7 +98,9 @@ jobs: build: yarn build --target aarch64-apple-darwin - host: ubuntu-latest target: aarch64-unknown-linux-gnu - build: yarn build --target aarch64-unknown-linux-gnu --use-napi-cross + build: | + export CFLAGS_aarch64_unknown_linux_gnu="-D__ARM_ARCH=8" + yarn build --target aarch64-unknown-linux-gnu --use-napi-cross name: stable - ${{ matrix.settings.target }} - node@22 runs-on: ${{ matrix.settings.host }} steps: diff --git a/Cargo.toml b/Cargo.toml index 0da75ad..6be8908 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,10 +10,10 @@ crate-type = ["cdylib"] [dependencies] anyhow = "1.0.99" -atlas-local = { git = "https://github.com/mongodb/atlas-local-lib.git", rev = "7d8de1e2b9b865fafeb4120c765b1223d500bbc1" } +atlas-local = { git = "https://github.com/mongodb/atlas-local-lib.git", rev = "df190245295e6675949fb5c63a164fadc0007dab" } bollard = "0.19.2" -napi = { version = "3.0.0", features = ["async", "anyhow"] } -napi-derive = "3.0.0" +napi = { version = "^3.3.0", features = ["async", "anyhow"] } +napi-derive = "^3.2.5" semver = "1.0.26" [build-dependencies] diff --git a/__test__/index.spec.ts b/__test__/index.spec.ts index 74b13a9..72881d4 100644 --- a/__test__/index.spec.ts +++ b/__test__/index.spec.ts @@ -41,6 +41,12 @@ test('smoke test', async (t) => { let getDeployment = await client.getDeployment(createDeploymentOptions.name) t.is(getDeployment.name,createDeploymentOptions.name) + let getConnectionStringOptions = { + containerIdOrName: createDeploymentOptions.name, + } + let connString = await client.getConnectionString(getConnectionStringOptions) + t.assert(connString === `mongodb://127.0.0.1:${getDeployment.portBindings.port}/?directConnection=true`) + // Count deployments after creation let after_create_deployment_count = (await client.listDeployments()).length t.assert(after_create_deployment_count - start_deployments_count === 1) diff --git a/deny.toml b/deny.toml index 0760a29..d6b0eaf 100644 --- a/deny.toml +++ b/deny.toml @@ -38,7 +38,7 @@ targets = [ # they are connected to another crate in the graph that hasn't been pruned, # so it should be used with care. The identifiers are [Package ID Specifications] # (https://doc.rust-lang.org/cargo/reference/pkgid-spec.html) -#exclude = [] +exclude = ["mongodb"] # If true, metadata will be collected with `--all-features`. Note that this can't # be toggled off if true, if you want to conditionally enable `--all-features` it # is recommended to pass `--all-features` on the cmd line instead diff --git a/index.d.ts b/index.d.ts index 854c8dc..6c98179 100644 --- a/index.d.ts +++ b/index.d.ts @@ -6,6 +6,7 @@ export declare class Client { listDeployments(): Promise> deleteDeployment(deploymentName: string): Promise getDeployment(deploymentName: string): Promise + getConnectionString(options: GetConnectionStringOptions): Promise } export declare const enum BindingType { @@ -66,6 +67,12 @@ export interface Deployment { telemetryBaseUrl?: string } +export interface GetConnectionStringOptions { + containerIdOrName: string + dbUsername?: string + dbPassword?: string +} + export interface MongoDbPortBinding { type: BindingType ip: string diff --git a/index.js b/index.js index 1702dd6..4faeb3a 100644 --- a/index.js +++ b/index.js @@ -66,7 +66,7 @@ const isMuslFromChildProcess = () => { function requireNative() { if (process.env.NAPI_RS_NATIVE_LIBRARY_PATH) { try { - nativeBinding = require(process.env.NAPI_RS_NATIVE_LIBRARY_PATH); + return require(process.env.NAPI_RS_NATIVE_LIBRARY_PATH); } catch (err) { loadErrors.push(err) } @@ -78,7 +78,12 @@ function requireNative() { loadErrors.push(e) } try { - return require('@mongodb-js-preview/atlas-local-android-arm64') + const binding = require('@mongodb-js-preview/atlas-local-android-arm64') + const bindingPackageVersion = require('@mongodb-js-preview/atlas-local-android-arm64/package.json').version + if (bindingPackageVersion !== '0.0.0-preview.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.0-preview.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + } + return binding } catch (e) { loadErrors.push(e) } @@ -89,7 +94,12 @@ function requireNative() { loadErrors.push(e) } try { - return require('@mongodb-js-preview/atlas-local-android-arm-eabi') + const binding = require('@mongodb-js-preview/atlas-local-android-arm-eabi') + const bindingPackageVersion = require('@mongodb-js-preview/atlas-local-android-arm-eabi/package.json').version + if (bindingPackageVersion !== '0.0.0-preview.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.0-preview.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + } + return binding } catch (e) { loadErrors.push(e) } @@ -104,7 +114,12 @@ function requireNative() { loadErrors.push(e) } try { - return require('@mongodb-js-preview/atlas-local-win32-x64-msvc') + const binding = require('@mongodb-js-preview/atlas-local-win32-x64-msvc') + const bindingPackageVersion = require('@mongodb-js-preview/atlas-local-win32-x64-msvc/package.json').version + if (bindingPackageVersion !== '0.0.0-preview.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.0-preview.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + } + return binding } catch (e) { loadErrors.push(e) } @@ -115,7 +130,12 @@ function requireNative() { loadErrors.push(e) } try { - return require('@mongodb-js-preview/atlas-local-win32-ia32-msvc') + const binding = require('@mongodb-js-preview/atlas-local-win32-ia32-msvc') + const bindingPackageVersion = require('@mongodb-js-preview/atlas-local-win32-ia32-msvc/package.json').version + if (bindingPackageVersion !== '0.0.0-preview.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.0-preview.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + } + return binding } catch (e) { loadErrors.push(e) } @@ -126,7 +146,12 @@ function requireNative() { loadErrors.push(e) } try { - return require('@mongodb-js-preview/atlas-local-win32-arm64-msvc') + const binding = require('@mongodb-js-preview/atlas-local-win32-arm64-msvc') + const bindingPackageVersion = require('@mongodb-js-preview/atlas-local-win32-arm64-msvc/package.json').version + if (bindingPackageVersion !== '0.0.0-preview.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.0-preview.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + } + return binding } catch (e) { loadErrors.push(e) } @@ -140,7 +165,12 @@ function requireNative() { loadErrors.push(e) } try { - return require('@mongodb-js-preview/atlas-local-darwin-universal') + const binding = require('@mongodb-js-preview/atlas-local-darwin-universal') + const bindingPackageVersion = require('@mongodb-js-preview/atlas-local-darwin-universal/package.json').version + if (bindingPackageVersion !== '0.0.0-preview.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.0-preview.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + } + return binding } catch (e) { loadErrors.push(e) } @@ -151,7 +181,12 @@ function requireNative() { loadErrors.push(e) } try { - return require('@mongodb-js-preview/atlas-local-darwin-x64') + const binding = require('@mongodb-js-preview/atlas-local-darwin-x64') + const bindingPackageVersion = require('@mongodb-js-preview/atlas-local-darwin-x64/package.json').version + if (bindingPackageVersion !== '0.0.0-preview.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.0-preview.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + } + return binding } catch (e) { loadErrors.push(e) } @@ -162,7 +197,12 @@ function requireNative() { loadErrors.push(e) } try { - return require('@mongodb-js-preview/atlas-local-darwin-arm64') + const binding = require('@mongodb-js-preview/atlas-local-darwin-arm64') + const bindingPackageVersion = require('@mongodb-js-preview/atlas-local-darwin-arm64/package.json').version + if (bindingPackageVersion !== '0.0.0-preview.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.0-preview.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + } + return binding } catch (e) { loadErrors.push(e) } @@ -177,7 +217,12 @@ function requireNative() { loadErrors.push(e) } try { - return require('@mongodb-js-preview/atlas-local-freebsd-x64') + const binding = require('@mongodb-js-preview/atlas-local-freebsd-x64') + const bindingPackageVersion = require('@mongodb-js-preview/atlas-local-freebsd-x64/package.json').version + if (bindingPackageVersion !== '0.0.0-preview.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.0-preview.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + } + return binding } catch (e) { loadErrors.push(e) } @@ -188,7 +233,12 @@ function requireNative() { loadErrors.push(e) } try { - return require('@mongodb-js-preview/atlas-local-freebsd-arm64') + const binding = require('@mongodb-js-preview/atlas-local-freebsd-arm64') + const bindingPackageVersion = require('@mongodb-js-preview/atlas-local-freebsd-arm64/package.json').version + if (bindingPackageVersion !== '0.0.0-preview.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.0-preview.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + } + return binding } catch (e) { loadErrors.push(e) } @@ -204,7 +254,12 @@ function requireNative() { loadErrors.push(e) } try { - return require('@mongodb-js-preview/atlas-local-linux-x64-musl') + const binding = require('@mongodb-js-preview/atlas-local-linux-x64-musl') + const bindingPackageVersion = require('@mongodb-js-preview/atlas-local-linux-x64-musl/package.json').version + if (bindingPackageVersion !== '0.0.0-preview.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.0-preview.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + } + return binding } catch (e) { loadErrors.push(e) } @@ -215,7 +270,12 @@ function requireNative() { loadErrors.push(e) } try { - return require('@mongodb-js-preview/atlas-local-linux-x64-gnu') + const binding = require('@mongodb-js-preview/atlas-local-linux-x64-gnu') + const bindingPackageVersion = require('@mongodb-js-preview/atlas-local-linux-x64-gnu/package.json').version + if (bindingPackageVersion !== '0.0.0-preview.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.0-preview.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + } + return binding } catch (e) { loadErrors.push(e) } @@ -228,7 +288,12 @@ function requireNative() { loadErrors.push(e) } try { - return require('@mongodb-js-preview/atlas-local-linux-arm64-musl') + const binding = require('@mongodb-js-preview/atlas-local-linux-arm64-musl') + const bindingPackageVersion = require('@mongodb-js-preview/atlas-local-linux-arm64-musl/package.json').version + if (bindingPackageVersion !== '0.0.0-preview.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.0-preview.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + } + return binding } catch (e) { loadErrors.push(e) } @@ -239,7 +304,12 @@ function requireNative() { loadErrors.push(e) } try { - return require('@mongodb-js-preview/atlas-local-linux-arm64-gnu') + const binding = require('@mongodb-js-preview/atlas-local-linux-arm64-gnu') + const bindingPackageVersion = require('@mongodb-js-preview/atlas-local-linux-arm64-gnu/package.json').version + if (bindingPackageVersion !== '0.0.0-preview.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.0-preview.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + } + return binding } catch (e) { loadErrors.push(e) } @@ -252,7 +322,12 @@ function requireNative() { loadErrors.push(e) } try { - return require('@mongodb-js-preview/atlas-local-linux-arm-musleabihf') + const binding = require('@mongodb-js-preview/atlas-local-linux-arm-musleabihf') + const bindingPackageVersion = require('@mongodb-js-preview/atlas-local-linux-arm-musleabihf/package.json').version + if (bindingPackageVersion !== '0.0.0-preview.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.0-preview.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + } + return binding } catch (e) { loadErrors.push(e) } @@ -263,7 +338,46 @@ function requireNative() { loadErrors.push(e) } try { - return require('@mongodb-js-preview/atlas-local-linux-arm-gnueabihf') + const binding = require('@mongodb-js-preview/atlas-local-linux-arm-gnueabihf') + const bindingPackageVersion = require('@mongodb-js-preview/atlas-local-linux-arm-gnueabihf/package.json').version + if (bindingPackageVersion !== '0.0.0-preview.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.0-preview.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + } + return binding + } catch (e) { + loadErrors.push(e) + } + } + } else if (process.arch === 'loong64') { + if (isMusl()) { + try { + return require('./atlas-local.linux-loong64-musl.node') + } catch (e) { + loadErrors.push(e) + } + try { + const binding = require('@mongodb-js-preview/atlas-local-linux-loong64-musl') + const bindingPackageVersion = require('@mongodb-js-preview/atlas-local-linux-loong64-musl/package.json').version + if (bindingPackageVersion !== '0.0.0-preview.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.0-preview.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + } + return binding + } catch (e) { + loadErrors.push(e) + } + } else { + try { + return require('./atlas-local.linux-loong64-gnu.node') + } catch (e) { + loadErrors.push(e) + } + try { + const binding = require('@mongodb-js-preview/atlas-local-linux-loong64-gnu') + const bindingPackageVersion = require('@mongodb-js-preview/atlas-local-linux-loong64-gnu/package.json').version + if (bindingPackageVersion !== '0.0.0-preview.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.0-preview.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + } + return binding } catch (e) { loadErrors.push(e) } @@ -276,7 +390,12 @@ function requireNative() { loadErrors.push(e) } try { - return require('@mongodb-js-preview/atlas-local-linux-riscv64-musl') + const binding = require('@mongodb-js-preview/atlas-local-linux-riscv64-musl') + const bindingPackageVersion = require('@mongodb-js-preview/atlas-local-linux-riscv64-musl/package.json').version + if (bindingPackageVersion !== '0.0.0-preview.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.0-preview.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + } + return binding } catch (e) { loadErrors.push(e) } @@ -287,7 +406,12 @@ function requireNative() { loadErrors.push(e) } try { - return require('@mongodb-js-preview/atlas-local-linux-riscv64-gnu') + const binding = require('@mongodb-js-preview/atlas-local-linux-riscv64-gnu') + const bindingPackageVersion = require('@mongodb-js-preview/atlas-local-linux-riscv64-gnu/package.json').version + if (bindingPackageVersion !== '0.0.0-preview.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.0-preview.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + } + return binding } catch (e) { loadErrors.push(e) } @@ -299,7 +423,12 @@ function requireNative() { loadErrors.push(e) } try { - return require('@mongodb-js-preview/atlas-local-linux-ppc64-gnu') + const binding = require('@mongodb-js-preview/atlas-local-linux-ppc64-gnu') + const bindingPackageVersion = require('@mongodb-js-preview/atlas-local-linux-ppc64-gnu/package.json').version + if (bindingPackageVersion !== '0.0.0-preview.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.0-preview.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + } + return binding } catch (e) { loadErrors.push(e) } @@ -310,7 +439,12 @@ function requireNative() { loadErrors.push(e) } try { - return require('@mongodb-js-preview/atlas-local-linux-s390x-gnu') + const binding = require('@mongodb-js-preview/atlas-local-linux-s390x-gnu') + const bindingPackageVersion = require('@mongodb-js-preview/atlas-local-linux-s390x-gnu/package.json').version + if (bindingPackageVersion !== '0.0.0-preview.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.0-preview.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + } + return binding } catch (e) { loadErrors.push(e) } @@ -320,34 +454,49 @@ function requireNative() { } else if (process.platform === 'openharmony') { if (process.arch === 'arm64') { try { - return require('./atlas-local.linux-arm64-ohos.node') + return require('./atlas-local.openharmony-arm64.node') } catch (e) { loadErrors.push(e) } try { - return require('@mongodb-js-preview/atlas-local-linux-arm64-ohos') + const binding = require('@mongodb-js-preview/atlas-local-openharmony-arm64') + const bindingPackageVersion = require('@mongodb-js-preview/atlas-local-openharmony-arm64/package.json').version + if (bindingPackageVersion !== '0.0.0-preview.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.0-preview.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + } + return binding } catch (e) { loadErrors.push(e) } } else if (process.arch === 'x64') { try { - return require('./atlas-local.linux-x64-ohos.node') + return require('./atlas-local.openharmony-x64.node') } catch (e) { loadErrors.push(e) } try { - return require('@mongodb-js-preview/atlas-local-linux-x64-ohos') + const binding = require('@mongodb-js-preview/atlas-local-openharmony-x64') + const bindingPackageVersion = require('@mongodb-js-preview/atlas-local-openharmony-x64/package.json').version + if (bindingPackageVersion !== '0.0.0-preview.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.0-preview.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + } + return binding } catch (e) { loadErrors.push(e) } } else if (process.arch === 'arm') { try { - return require('./atlas-local.linux-arm-ohos.node') + return require('./atlas-local.openharmony-arm.node') } catch (e) { loadErrors.push(e) } try { - return require('@mongodb-js-preview/atlas-local-linux-arm-ohos') + const binding = require('@mongodb-js-preview/atlas-local-openharmony-arm') + const bindingPackageVersion = require('@mongodb-js-preview/atlas-local-openharmony-arm/package.json').version + if (bindingPackageVersion !== '0.0.0-preview.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { + throw new Error(`Native binding package version mismatch, expected 0.0.0-preview.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) + } + return binding } catch (e) { loadErrors.push(e) } @@ -362,22 +511,32 @@ function requireNative() { nativeBinding = requireNative() if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) { + let wasiBinding = null + let wasiBindingError = null try { - nativeBinding = require('./atlas-local.wasi.cjs') + wasiBinding = require('./atlas-local.wasi.cjs') + nativeBinding = wasiBinding } catch (err) { if (process.env.NAPI_RS_FORCE_WASI) { - loadErrors.push(err) + wasiBindingError = err } } if (!nativeBinding) { try { - nativeBinding = require('@mongodb-js-preview/atlas-local-wasm32-wasi') + wasiBinding = require('@mongodb-js-preview/atlas-local-wasm32-wasi') + nativeBinding = wasiBinding } catch (err) { if (process.env.NAPI_RS_FORCE_WASI) { + wasiBindingError.cause = err loadErrors.push(err) } } } + if (process.env.NAPI_RS_FORCE_WASI === 'error' && !wasiBinding) { + const error = new Error('WASI binding not found and NAPI_RS_FORCE_WASI is set to error') + error.cause = wasiBindingError + throw error + } } if (!nativeBinding) { @@ -386,7 +545,12 @@ if (!nativeBinding) { `Cannot find native binding. ` + `npm has a bug related to optional dependencies (https://github.com/npm/cli/issues/4828). ` + 'Please try `npm i` again after removing both package-lock.json and node_modules directory.', - { cause: loadErrors } + { + cause: loadErrors.reduce((err, cur) => { + cur.cause = err + return cur + }), + }, ) } throw new Error(`Failed to load native binding`) diff --git a/src/lib.rs b/src/lib.rs index dbce04d..e6f2fc0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,4 +69,17 @@ impl Client { .context("get deployment") .map(|d| d.into()) } + + #[napi] + pub async fn get_connection_string( + &self, + options: crate::models::get_connection_string::GetConnectionStringOptions, + ) -> Result { + let options: atlas_local::models::GetConnectionStringOptions = options.into(); + self + .client + .get_connection_string(options) + .await + .context("get connection string") + } } diff --git a/src/models/get_connection_string.rs b/src/models/get_connection_string.rs new file mode 100644 index 0000000..96b48a7 --- /dev/null +++ b/src/models/get_connection_string.rs @@ -0,0 +1,49 @@ +use napi_derive::napi; + +#[napi(object)] +pub struct GetConnectionStringOptions { + pub container_id_or_name: String, + pub db_username: Option, + pub db_password: Option, +} + +impl From for atlas_local::models::GetConnectionStringOptions { + fn from(source: GetConnectionStringOptions) -> Self { + Self { + container_id_or_name: source.container_id_or_name, + db_username: source.db_username, + db_password: source.db_password, + } + } +} + +// test +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_get_connection_string_options() { + let options = GetConnectionStringOptions { + container_id_or_name: "test_container".into(), + db_username: Some("test_user".into()), + db_password: Some("test_pass".into()), + }; + + let get_connection_string_options: atlas_local::models::GetConnectionStringOptions = + options.into(); + + assert_eq!( + get_connection_string_options.container_id_or_name, + "test_container" + ); + assert_eq!( + get_connection_string_options.db_username, + Some("test_user".into()) + ); + assert_eq!( + get_connection_string_options.db_password, + Some("test_pass".into()) + ); + } +} diff --git a/src/models/mod.rs b/src/models/mod.rs index 917fa27..7a58349 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -1,2 +1,3 @@ pub mod create_deployment; +pub mod get_connection_string; pub mod list_deployments;