From b451ed4682eb1be9dcb56fd95c4dce975b407c44 Mon Sep 17 00:00:00 2001 From: Enok <416828041@qq.com> Date: Thu, 14 May 2020 10:27:28 +0800 Subject: [PATCH] Upgrade --- .github/workflows/ci.yml | 41 +++++++++++-------------- Cargo.lock | 36 +++++++++++----------- Cargo.toml | 9 +++--- deps.ts | 2 +- mod.ts | 4 +-- src/command/aggregation.rs | 6 ++-- src/command/connect.rs | 8 ++--- src/command/count.rs | 6 ++-- src/command/delete.rs | 9 ++---- src/command/find.rs | 8 ++--- src/command/indexes.rs | 6 ++-- src/command/insert.rs | 12 ++++---- src/command/list_collection_names.rs | 6 ++-- src/command/list_database_names.rs | 6 ++-- src/command/update.rs | 8 ++--- src/lib.rs | 14 ++++----- test.deps.ts | 6 ++-- test.ts | 45 ++++++++++++++-------------- ts/util.ts | 37 +++++++++++++++-------- 19 files changed, 132 insertions(+), 137 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a6fb6607..80540129 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,7 +19,7 @@ jobs: - name: Install rust uses: hecrj/setup-rust-action@v1 with: - rust-version: "1.42.0" + rust-version: "1.43.0" - name: Install clippy and rustfmt if: matrix.kind == 'lint' @@ -30,7 +30,7 @@ jobs: - name: Install Deno uses: denolib/setup-deno@master with: - deno-version: 0.39.0 + deno-version: 1.0.0 - name: Start MongoDB (MacOs) if: matrix.kind == 'test' && startsWith(matrix.os, 'mac') @@ -62,28 +62,21 @@ jobs: rustc --version cargo --version - - name: Cache cargo registry - uses: actions/cache@v1 - with: - path: ~/.cargo/registry - key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }} - - name: Cache cargo index - uses: actions/cache@v1 - with: - path: ~/.cargo/git - key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }} - - name: Cache cargo build - uses: actions/cache@v1 - with: - path: target - key: ${{ matrix.kind }}-${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }} + - name: Configure cargo data directory + run: echo "::set-env name=CARGO_HOME::$(pwd)/.cargo_home" - # Fix the problem that the second build of windows will fail - - name: Remove Some Cache - if: matrix.os == 'windows-2019' - run: | - rm target/release/gn_root -Recurse -ErrorAction Ignore - rm target/debug/gn_root -Recurse -ErrorAction Ignore + - name: Cache + uses: denoland/github-actions-cache@stable-prerelease + with: + path: |- + .cargo_home + target/*/.* + target/*/build + target/*/deps + key: + ${{ matrix.os }}-${{ matrix.kind }}-${{ hashFiles('Cargo.lock') }} + restore-keys: | + ${{ matrix.os }}-${{ matrix.kind }}- - name: Install python uses: actions/setup-python@v1 @@ -102,7 +95,7 @@ jobs: run: cargo test --release --locked --all-targets - name: Test TS if: matrix.kind == 'test' - run: deno run -A ./test.ts + run: deno test -A --unstable ./test.ts - name: Release uses: softprops/action-gh-release@v1 diff --git a/Cargo.lock b/Cargo.lock index 89297a9c..00c5471a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,9 +17,9 @@ checksum = "7825f6833612eb2414095684fcf6c635becf3ce97fe48cf6421321e93bfbd53c" [[package]] name = "async-trait" -version = "0.1.24" +version = "0.1.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "750b1c38a1dfadd108da0f01c08f4cdc7ff1bb39b325f9c82cc972361780a6e1" +checksum = "da71fef07bc806586090247e971229289f64c210a278ee5ae419314eb386b31d" dependencies = [ "proc-macro2 1.0.8", "quote 1.0.2", @@ -213,9 +213,9 @@ dependencies = [ [[package]] name = "deno_core" -version = "0.39.0" +version = "0.45.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "106fdf021a2bde6d532c1fa9c51ab8f19d4cd4314f5f8d01db20479fc9a9be0a" +checksum = "9e22a025647cd78e2444cf422022fb00e5fc6e7c30d63b0611ca1e6c39faddd5" dependencies = [ "downcast-rs", "futures 0.3.4", @@ -242,9 +242,9 @@ dependencies = [ [[package]] name = "derivative" -version = "1.0.3" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "942ca430eef7a3806595a6737bc388bf51adb888d3fc0dd1b50f1c170167ee3a" +checksum = "3c6d883546668a3e2011b6a716a7330b82eabb0151b138217f632c8243e17135" dependencies = [ "proc-macro2 0.4.30", "quote 0.6.13", @@ -572,9 +572,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.68" +version = "0.2.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dea0c0405123bba743ee3f91f49b1c7cfb684eef0da0a50110f758ccf24cdff0" +checksum = "99e85c08494b21a9054e7fe1374a732aeadaff3980b6990b94bfd3a70f690005" [[package]] name = "linked-hash-map" @@ -996,9 +996,9 @@ dependencies = [ [[package]] name = "ring" -version = "0.16.9" +version = "0.16.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6747f8da1f2b1fabbee1aaa4eb8a11abf9adef0bf58a41cee45db5d59cecdfac" +checksum = "1ba5a8ec64ee89a76c98c549af81ff14813df09c3e6dc4766c3856da48597a0c" dependencies = [ "cc", "lazy_static", @@ -1041,9 +1041,9 @@ dependencies = [ [[package]] name = "rusty_v8" -version = "0.3.10" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01403323dd70ffeace922e35555ca0c15223a20a4e1cf8f35cc4f68fadb3e285" +checksum = "acb0ad56a57c42009a8d16df5fa94ae882ad0ffe0e88fe1a23b261b3affbccf2" dependencies = [ "bitflags", "cargo_gn", @@ -1096,9 +1096,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.50" +version = "1.0.52" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78a7a12c167809363ec3bd7329fc0a3369056996de43c4b37ef3cd54a6ce4867" +checksum = "a7894c8ed05b7a3a279aeb79025fdec1d3158080b75b98a08faf2806bb799edd" dependencies = [ "indexmap", "itoa", @@ -1290,9 +1290,9 @@ dependencies = [ [[package]] name = "tokio" -version = "0.2.13" +version = "0.2.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fa5e81d6bc4e67fe889d5783bd2a128ab2e0cfa487e0be16b6a8d177b101616" +checksum = "05c1d570eb1a36f0345a5ce9c6c6e665b70b73d11236912c0b477616aeec47b1" dependencies = [ "bytes", "iovec", @@ -1304,9 +1304,9 @@ dependencies = [ [[package]] name = "trust-dns-proto" -version = "0.19.3" +version = "0.19.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d37cb22a372d86b16673d460fa379da31ace131a04cc3b1e78130607ce7cb85" +checksum = "cdd7061ba6f4d4d9721afedffbfd403f20f39a4301fee1b70d6fcd09cca69f28" dependencies = [ "async-trait", "backtrace", diff --git a/Cargo.toml b/Cargo.toml index 0368ae19..418e3ae3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,11 +8,10 @@ edition = "2018" crate-type = ["cdylib"] [dependencies] -mongodb = "0.9.2" +mongodb = { version = "0.9.2" } bson = "0.14.1" -serde_json = "1.0.50" +serde_json = "1.0.52" serde = "1.0.106" futures = "0.3.4" -deno_core = "0.39.0" -lazy_static = "1.4.0" -# deno = "0.29.0" \ No newline at end of file +deno_core = "0.45.0" +lazy_static = "1.4.0" \ No newline at end of file diff --git a/deps.ts b/deps.ts index 12d89de6..fa24e977 100644 --- a/deps.ts +++ b/deps.ts @@ -1 +1 @@ -export { prepare } from "https://deno.land/x/plugin_prepare@v0.3.1/mod.ts"; +export { prepare } from "https://deno.land/x/plugin_prepare@v0.6.0/mod.ts"; diff --git a/mod.ts b/mod.ts index 7cc83b01..0016ab91 100644 --- a/mod.ts +++ b/mod.ts @@ -4,5 +4,5 @@ export * from "./ts/database.ts"; export * from "./ts/result.ts"; export { ObjectId } from "./ts/types.ts"; export * from "./ts/util.ts"; -export const VERSION = "v0.5.2"; -export const RELEASE_URL = `https://github.com/manyuanrong/deno_mongo/releases/download/${VERSION}`; \ No newline at end of file +export const VERSION = "v0.6.0"; +export const RELEASE_URL = `https://github.com/manyuanrong/deno_mongo/releases/download/${VERSION}`; diff --git a/src/command/aggregation.rs b/src/command/aggregation.rs index fb47e221..76fd3147 100644 --- a/src/command/aggregation.rs +++ b/src/command/aggregation.rs @@ -12,7 +12,7 @@ struct AggregationArgs { pipeline: Vec, } -pub fn aggregate(command: Command) -> CoreOp { +pub fn aggregate(command: Command) -> Op { let fut = async move { let client = command.get_client(); let data = command.data; @@ -31,7 +31,7 @@ pub fn aggregate(command: Command) -> CoreOp { _ => None, }) .collect(); - Ok(util::async_result(&command.args, docs)) + util::async_result(&command.args, docs) }; - CoreOp::Async(fut.boxed()) + Op::Async(fut.boxed()) } diff --git a/src/command/connect.rs b/src/command/connect.rs index a5d15edd..2c7ab7b0 100644 --- a/src/command/connect.rs +++ b/src/command/connect.rs @@ -20,7 +20,7 @@ struct ConnectArgs { password: Option, } -pub fn connect_with_options(command: Command) -> CoreOp { +pub fn connect_with_options(command: Command) -> Op { let args: ConnectArgs = serde_json::from_slice(command.data.unwrap().as_ref()).unwrap(); let hosts = args .hosts @@ -56,14 +56,14 @@ pub fn connect_with_options(command: Command) -> CoreOp { let client = mongodb::Client::with_options(options).unwrap(); let client_id: usize = NEXT_CLIENT_ID.fetch_add(1, Ordering::SeqCst); CLIENTS.lock().unwrap().insert(client_id, client); - CoreOp::Sync(Buf::from(client_id.to_string().as_bytes())) + Op::Sync(Buf::from(client_id.to_string().as_bytes())) } -pub fn connect_with_uri(command: Command) -> CoreOp { +pub fn connect_with_uri(command: Command) -> Op { let uri: Vec = command.data.unwrap().as_ref().to_vec(); let uri = String::from_utf8(uri).unwrap(); let client = mongodb::Client::with_uri_str(&uri).unwrap(); let client_id: usize = NEXT_CLIENT_ID.fetch_add(1, Ordering::SeqCst); CLIENTS.lock().unwrap().insert(client_id, client); - CoreOp::Sync(Buf::from(client_id.to_string().as_bytes())) + Op::Sync(Buf::from(client_id.to_string().as_bytes())) } diff --git a/src/command/count.rs b/src/command/count.rs index 50130e13..8810e8a5 100644 --- a/src/command/count.rs +++ b/src/command/count.rs @@ -10,7 +10,7 @@ struct CountArgs { filter: Option, } -pub fn count(command: Command) -> CoreOp { +pub fn count(command: Command) -> Op { let fut = async move { let client = command.get_client(); let data = command.data; @@ -23,7 +23,7 @@ pub fn count(command: Command) -> CoreOp { let collection = database.collection(&collection_name); let count = collection.count_documents(filter, None).unwrap(); - Ok(util::async_result(&command.args, count)) + util::async_result(&command.args, count) }; - CoreOp::Async(fut.boxed()) + Op::Async(fut.boxed()) } diff --git a/src/command/delete.rs b/src/command/delete.rs index 36c26dc2..2260eabc 100644 --- a/src/command/delete.rs +++ b/src/command/delete.rs @@ -10,7 +10,7 @@ struct DeleteArgs { delete_one: bool, } -pub fn delete(command: Command) -> CoreOp { +pub fn delete(command: Command) -> Op { let fut = async move { let client = command.get_client(); let data = command.data; @@ -27,10 +27,7 @@ pub fn delete(command: Command) -> CoreOp { } else { collection.delete_many(query, None).unwrap() }; - Ok(util::async_result( - &command.args, - delete_result.deleted_count, - )) + util::async_result(&command.args, delete_result.deleted_count) }; - CoreOp::Async(fut.boxed()) + Op::Async(fut.boxed()) } diff --git a/src/command/find.rs b/src/command/find.rs index 0dc8f845..25be96df 100644 --- a/src/command/find.rs +++ b/src/command/find.rs @@ -16,7 +16,7 @@ struct FindArgs { limit: Option, } -pub fn find(command: Command) -> CoreOp { +pub fn find(command: Command) -> Op { let fut = async move { let client = command.get_client(); let data = command.data; @@ -32,7 +32,7 @@ pub fn find(command: Command) -> CoreOp { if args.find_one { let doc = collection.find_one(filter, None).unwrap(); - Ok(util::async_result(&command.args, doc)) + util::async_result(&command.args, doc) } else { let mut options: FindOptions = FindOptions::default(); options.skip = skip; @@ -45,8 +45,8 @@ pub fn find(command: Command) -> CoreOp { _ => None, }) .collect(); - Ok(util::async_result(&command.args, docs)) + util::async_result(&command.args, docs) } }; - CoreOp::Async(fut.boxed()) + Op::Async(fut.boxed()) } diff --git a/src/command/indexes.rs b/src/command/indexes.rs index e26862ee..7de73977 100644 --- a/src/command/indexes.rs +++ b/src/command/indexes.rs @@ -23,7 +23,7 @@ impl IndexModelArgs { } } -pub fn create_indexes(command: Command) -> CoreOp { +pub fn create_indexes(command: Command) -> Op { let fut = async move { let client = command.get_client(); let data = command.data; @@ -48,7 +48,7 @@ pub fn create_indexes(command: Command) -> CoreOp { let collection = database.collection(&collection_name); let result = collection.create_indexes(models).unwrap(); - Ok(util::async_result(&command.args, result)) + util::async_result(&command.args, result) }; - CoreOp::Async(fut.boxed()) + Op::Async(fut.boxed()) } diff --git a/src/command/insert.rs b/src/command/insert.rs index 1709a1c8..bbdf188c 100644 --- a/src/command/insert.rs +++ b/src/command/insert.rs @@ -18,7 +18,7 @@ struct InsertOneArgs { doc: Value, } -pub fn insert_one(command: Command) -> CoreOp { +pub fn insert_one(command: Command) -> Op { let fut = async move { let client = command.get_client(); let data = command.data; @@ -30,12 +30,12 @@ pub fn insert_one(command: Command) -> CoreOp { let collection = database.collection(&collection_name); let insert_result = collection.insert_one(doc, None).unwrap(); - Ok(util::async_result(&command.args, insert_result.inserted_id)) + util::async_result(&command.args, insert_result.inserted_id) }; - CoreOp::Async(fut.boxed()) + Op::Async(fut.boxed()) } -pub fn insert_many(command: Command) -> CoreOp { +pub fn insert_many(command: Command) -> Op { let fut = async move { let client = command.get_client(); let data = command.data; @@ -54,7 +54,7 @@ pub fn insert_many(command: Command) -> CoreOp { .map(|(_, id)| id.to_owned()) .collect(); - Ok(util::async_result(&command.args, ids)) + util::async_result(&command.args, ids) }; - CoreOp::Async(fut.boxed()) + Op::Async(fut.boxed()) } diff --git a/src/command/list_collection_names.rs b/src/command/list_collection_names.rs index 0638e9ec..1d7f0bb2 100644 --- a/src/command/list_collection_names.rs +++ b/src/command/list_collection_names.rs @@ -1,6 +1,6 @@ use crate::*; -pub fn list_collection_names(command: Command) -> CoreOp { +pub fn list_collection_names(command: Command) -> Op { let fut = async move { let client = command.get_client(); let data = command.data; @@ -9,7 +9,7 @@ pub fn list_collection_names(command: Command) -> CoreOp { let database = client.database(&db_name); let collection_names = database.list_collection_names(None::); - Ok(util::async_result(&command.args, collection_names.unwrap())) + util::async_result(&command.args, collection_names.unwrap()) }; - CoreOp::Async(fut.boxed()) + Op::Async(fut.boxed()) } diff --git a/src/command/list_database_names.rs b/src/command/list_database_names.rs index 0092bda3..386d5a18 100644 --- a/src/command/list_database_names.rs +++ b/src/command/list_database_names.rs @@ -1,12 +1,12 @@ use crate::*; -pub fn list_database_names(command: Command) -> CoreOp { +pub fn list_database_names(command: Command) -> Op { let fut = async move { let names = command .get_client() .list_database_names(None::); let data = names.unwrap(); - Ok(util::async_result(&command.args, data)) + util::async_result(&command.args, data) }; - CoreOp::Async(fut.boxed()) + Op::Async(fut.boxed()) } diff --git a/src/command/update.rs b/src/command/update.rs index 206f5d0d..bea7a93c 100644 --- a/src/command/update.rs +++ b/src/command/update.rs @@ -19,7 +19,7 @@ struct UpdateResultArgs { pub upserted_id: Option, } -pub fn update(command: Command) -> CoreOp { +pub fn update(command: Command) -> Op { let fut = async move { let client = command.get_client(); let data = command.data; @@ -37,14 +37,14 @@ pub fn update(command: Command) -> CoreOp { collection.update_many(query_doc, update_doc, None).unwrap() }; - Ok(util::async_result( + util::async_result( &command.args, UpdateResultArgs { matched_count: result.matched_count, modified_count: result.modified_count, upserted_id: result.upserted_id.map(|id| id.into()), }, - )) + ) }; - CoreOp::Async(fut.boxed()) + Op::Async(fut.boxed()) } diff --git a/src/lib.rs b/src/lib.rs index 237942d1..ddc787af 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,3 @@ -#[macro_use] extern crate deno_core; #[macro_use] extern crate lazy_static; @@ -8,9 +7,7 @@ extern crate bson; extern crate mongodb; extern crate serde; -use deno_core::CoreOp; -use deno_core::PluginInitContext; -use deno_core::{Buf, ZeroCopyBuf}; +use deno_core::plugin_api::{Buf, Interface, Op, ZeroCopyBuf}; use futures::FutureExt; use mongodb::Client; use serde::{Deserialize, Serialize}; @@ -26,8 +23,6 @@ lazy_static! { static ref NEXT_CLIENT_ID: AtomicUsize = AtomicUsize::new(0); } -init_fn!(init); - #[derive(Serialize, Deserialize)] pub enum CommandType { ConnectWithOptions, @@ -81,8 +76,9 @@ impl CommandArgs { } } -fn init(context: &mut dyn PluginInitContext) { - context.register_op("mongo_command", Box::new(op_command)); +#[no_mangle] +pub fn deno_plugin_init(interface: &mut dyn Interface) { + interface.register_op("mongo_command", op_command); } pub(crate) fn get_client(client_id: usize) -> Client { @@ -90,7 +86,7 @@ pub(crate) fn get_client(client_id: usize) -> Client { map.get(&client_id).unwrap().clone() } -fn op_command(data: &[u8], zero_copy: Option) -> CoreOp { +fn op_command(_interface: &mut dyn Interface, data: &[u8], zero_copy: Option) -> Op { let args = CommandArgs::new(data); let executor = match args.command_type { CommandType::ConnectWithOptions => command::connect_with_options, diff --git a/test.deps.ts b/test.deps.ts index 5c4c3668..9da49226 100644 --- a/test.deps.ts +++ b/test.deps.ts @@ -1,6 +1,6 @@ -export { exists } from "https://deno.land/std@v0.39.0/fs/mod.ts"; +export { exists } from "https://deno.land/std@55d2c6bd103879263c115fa5697f3cf3101158f4/fs/mod.ts"; export { assert, assertEquals, - assertThrows -} from "https://deno.land/std@v0.39.0/testing/asserts.ts"; + assertThrows, +} from "https://deno.land/std@v0.50.0/testing/asserts.ts"; diff --git a/test.ts b/test.ts index cb370f29..bdf7caf0 100644 --- a/test.ts +++ b/test.ts @@ -5,7 +5,7 @@ import "./ts/tests/type-convert.test.ts"; import "./ts/tests/types-check.test.ts"; import { ObjectId } from "./ts/types.ts"; -const { test, runTests } = Deno; +const { test } = Deno; const dateNow = Date.now(); function getClient(): MongoClient { @@ -14,7 +14,7 @@ function getClient(): MongoClient { return client; } -test(async function testConnectWithUri() { +test("testConnectWithUri", async () => { const client = new MongoClient(); client.connectWithUri("mongodb://localhost:27017"); const names = await client.listDatabases(); @@ -22,46 +22,46 @@ test(async function testConnectWithUri() { assert(names.length > 0); }); -test(async function testConnectWithOptions() { +test("testConnectWithOptions", async () => { const client = new MongoClient(); client.connectWithOptions({ - hosts: ["localhost:27017"] + hosts: ["localhost:27017"], }); const names = await client.listDatabases(); assert(names instanceof Array); assert(names.length > 0); }); -test(async function testListCollectionNames() { +test("testListCollectionNames", async () => { const db = getClient().database("local"); const names = await db.listCollectionNames(); assertEquals(names, ["startup_log"]); }); -test(async function testInsertOne() { +test("testInsertOne", async () => { const db = getClient().database("test"); const users = db.collection("mongo_test_users"); const insertId: ObjectId = await users.insertOne({ username: "user1", password: "pass1", - date: new Date(dateNow) + date: new Date(dateNow), }); assertEquals(Object.keys(insertId), ["$oid"]); const user1 = await users.findOne({ - _id: ObjectId(insertId.$oid) + _id: ObjectId(insertId.$oid), }); assertEquals(user1, { _id: insertId, username: "user1", password: "pass1", - date: new Date(dateNow) + date: new Date(dateNow), }); }); -test(async function testFindOne() { +test("testFindOne", async () => { const db = getClient().database("test"); const users = db.collection("mongo_test_users"); const user1 = await users.findOne(); @@ -72,38 +72,38 @@ test(async function testFindOne() { assertEquals(findNull, null); }); -test(async function testUpdateOne() { +test("testUpdateOne", async () => { const db = getClient().database("test"); const users = db.collection("mongo_test_users"); const result = await users.updateOne({}, { username: "USER1" }); assertEquals(result, { matchedCount: 1, modifiedCount: 1, upsertedId: null }); }); -test(async function testDeleteOne() { +test("testDeleteOne", async () => { const db = getClient().database("test"); const users = db.collection("mongo_test_users"); const deleteCount = await users.deleteOne({}); assertEquals(deleteCount, 1); }); -test(async function testInsertMany() { +test("testInsertMany", async () => { const db = getClient().database("test"); const users = db.collection("mongo_test_users"); const insertIds = await users.insertMany([ { username: "many", - password: "pass1" + password: "pass1", }, { username: "many", - password: "pass2" - } + password: "pass2", + }, ]); assertEquals(insertIds.length, 2); }); -test(async function testFind() { +test("testFind", async () => { const db = getClient().database("test"); const users = db.collection("mongo_test_users"); const findUsers = await users.find( @@ -117,24 +117,24 @@ test(async function testFind() { assertEquals(notFound, []); }); -test(async function testCount() { +test("testCount", async () => { const db = getClient().database("test"); const users = db.collection("mongo_test_users"); const count = await users.count({ username: "many" }); assertEquals(count, 2); }); -test(async function testAggregation() { +test("testAggregation", async () => { const db = getClient().database("test"); const users = db.collection("mongo_test_users"); const docs = await users.aggregate([ { $match: { username: "many" } }, - { $group: { _id: "$username", total: { $sum: 1 } } } + { $group: { _id: "$username", total: { $sum: 1 } } }, ]); assertEquals(docs, [{ _id: "many", total: 2 }]); }); -test(async function testUpdateMany() { +test("testUpdateMany", async () => { const db = getClient().database("test"); const users = db.collection("mongo_test_users"); const result = await users.updateMany( @@ -144,7 +144,7 @@ test(async function testUpdateMany() { assertEquals(result, { matchedCount: 2, modifiedCount: 2, upsertedId: null }); }); -test(async function testDeleteMany() { +test("testDeleteMany", async () => { const db = getClient().database("test"); const users = db.collection("mongo_test_users"); const deleteCount = await users.deleteMany({ username: "MANY" }); @@ -166,4 +166,3 @@ if (await exists(".deno_plugins")) { } await cargoBuild(); await init("file://./target/release"); -await runTests({}); diff --git a/ts/util.ts b/ts/util.ts index ae2126c8..d6c47493 100644 --- a/ts/util.ts +++ b/ts/util.ts @@ -2,9 +2,20 @@ import { prepare } from "../deps.ts"; import { RELEASE_URL } from "../mod.ts"; import { CommandType } from "./types.ts"; +// @ts-ignore +const DenoCore = Deno.core as { + ops: () => { [key: string]: number }; + setAsyncHandler(rid: number, handler: Function): void; + dispatch( + rid: number, + msg: any, + buf?: ArrayBufferView + ): Uint8Array | undefined; +}; + const PLUGIN_NAME = "deno_mongo"; -let dispatcher: Deno.PluginOp | null = null; +let mongoPluginId: number; const decoder = new TextDecoder(); const encoder = new TextEncoder(); @@ -22,15 +33,17 @@ export async function init(releaseUrl = RELEASE_URL) { const options = { name: PLUGIN_NAME, urls: { - mac: `${releaseUrl}/lib${PLUGIN_NAME}.dylib`, - win: `${releaseUrl}/${PLUGIN_NAME}.dll`, + darwin: `${releaseUrl}/lib${PLUGIN_NAME}.dylib`, + windows: `${releaseUrl}/${PLUGIN_NAME}.dll`, linux: `${releaseUrl}/lib${PLUGIN_NAME}.so`, }, }; - const Mongo = await prepare(options); - dispatcher = Mongo.ops["mongo_command"] as Deno.PluginOp; - dispatcher.setAsyncHandler((msg: Uint8Array) => { + await prepare(options); + + mongoPluginId = DenoCore.ops()["mongo_command"]; + + DenoCore.setAsyncHandler(mongoPluginId, (msg: Uint8Array) => { const { command_id, data } = JSON.parse(decoder.decode(msg)); const resolver = pendingCommands.get(command_id); resolver && resolver(data); @@ -47,10 +60,10 @@ export function decode(data: Uint8Array): string { export function dispatch(command: Command, data?: ArrayBufferView): Uint8Array { const control = encoder.encode(JSON.stringify(command)); - if (!dispatcher) { + if (!mongoPluginId) { throw new Error("The plugin must be initialized before use"); } - return dispatcher.dispatch(control, data)!; + return DenoCore.dispatch(mongoPluginId, control, data)!; } export function dispatchAsync( @@ -66,11 +79,9 @@ export function dispatchAsync( command_id: commandId, }) ); - if (!dispatcher) { - if (!dispatcher) { - throw new Error("The plugin must be initialized before use"); - } + if (!mongoPluginId) { + throw new Error("The plugin must be initialized before use"); } - dispatcher.dispatch(control, data); + DenoCore.dispatch(mongoPluginId, control, data); }); }