Skip to content

Commit

Permalink
use deno to build rust
Browse files Browse the repository at this point in the history
  • Loading branch information
manyuanrong committed Mar 3, 2020
1 parent c0bc8ea commit d533a8e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
.deno_plugins
**/*.rs.bk
27 changes: 27 additions & 0 deletions build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { existsSync } from "https://deno.land/std/fs/exists.ts";
import { resolve } from "https://deno.land/std/path/mod.ts";

export async function copyTargets() {
await Promise.all(
["libdeno_mongo.dylib", "libdeno_mongo.so", "deno_mongo.dll"].map(
async target => {
const targetPath = resolve("./target/release", target);
const targetToPath = resolve("./target/release", target);
if (existsSync(targetPath)) {
console.log(`Copy "${targetPath}"`);
await Deno.copyFile(targetPath, targetToPath);
}
}
)
);
}
export async function cargoBuild() {
const cargoCommand = Deno.run({
args: ["cargo", "build", "--release", "--locked"],
stderr: "inherit",
stdin: "inherit",
stdout: "inherit"
});
await cargoCommand.status();
await copyTargets();
}
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { ClientOptions, MongoClient } from "./ts/client.ts";
export { Database } from "./ts/database.ts";
export { init } from "./ts/util.ts";
5 changes: 4 additions & 1 deletion test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { assert, assertEquals } from "https://deno.land/std/testing/asserts.ts";
import { MongoClient } from "./mod.ts";
import { cargoBuild } from "./build.ts";
import { init, MongoClient } from "./mod.ts";

const { test, runTests } = Deno;

Expand Down Expand Up @@ -72,4 +73,6 @@ test(async function testDeleteOne() {
assertEquals(deleteCount, 1);
});

await cargoBuild();
await init();
await runTests();
43 changes: 33 additions & 10 deletions ts/util.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { pluginFilename } from "https://raw.githubusercontent.com/denoland/deno/fe26c4253da9cf769301b1c78a5eed127b996c6e/std/plugins/plugin_filename.ts";
import { prepare } from "https://raw.githubusercontent.com/manyuanrong/deno-plugin-prepare/master/mod.ts";
import { CommandType } from "./types.ts";

const Mongo = Deno.openPlugin(
`./target/release/${pluginFilename("deno_mongo")}`
);
const PLUGIN_NAME = "deno_mongo";

let dispatcher: Deno.PluginOp | null = null;

const dispatcher = Mongo.ops["mongo_command"];
const decoder = new TextDecoder();
const encoder = new TextEncoder();
const pendingCommands: Map<number, (data: unknown) => void> = new Map();
Expand All @@ -18,11 +17,27 @@ interface Command {
command_id?: number;
}

dispatcher.setAsyncHandler((msg: Uint8Array) => {
const { command_id, data } = JSON.parse(decoder.decode(msg));
const resolver = pendingCommands.get(command_id);
resolver && resolver(data);
});
export async function init() {
let releaseUrl =
"https://github.com/manyuanrong/deno_mongo/releases/download/master";

const options = {
name: PLUGIN_NAME,
urls: {
mac: `${releaseUrl}/lib${PLUGIN_NAME}.dylib`,
win: `${releaseUrl}/${PLUGIN_NAME}.dll`,
linux: `${releaseUrl}/lib${PLUGIN_NAME}`
}
};

const Mongo = await prepare(options);
dispatcher = Mongo.ops["mongo_command"];
dispatcher.setAsyncHandler((msg: Uint8Array) => {
const { command_id, data } = JSON.parse(decoder.decode(msg));
const resolver = pendingCommands.get(command_id);
resolver && resolver(data);
});
}

export function encode(str: string): Uint8Array {
return encoder.encode(str);
Expand All @@ -34,6 +49,9 @@ export function decode(data: Uint8Array): string {

export function dispatch(command: Command, data?: ArrayBufferView): Uint8Array {
const control = encoder.encode(JSON.stringify(command));
if (!dispatcher) {
throw new Error("The plugin must be initialized before use");
}
return dispatcher.dispatch(control, data)!;
}

Expand All @@ -50,6 +68,11 @@ export function dispatchAsync(
command_id: commandId
})
);
if (!dispatcher) {
if (!dispatcher) {
throw new Error("The plugin must be initialized before use");
}
}
dispatcher.dispatch(control, data);
});
}

0 comments on commit d533a8e

Please sign in to comment.