Skip to content
This repository has been archived by the owner on Aug 22, 2020. It is now read-only.

Commit

Permalink
add Rust middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
galvez committed Mar 10, 2019
1 parent f32da48 commit e05145b
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 0 deletions.
6 changes: 6 additions & 0 deletions middleware/.gitignore
@@ -0,0 +1,6 @@
native/target
native/index.node
native/artifacts.json
**/*~
**/node_modules
**/.DS_Store
3 changes: 3 additions & 0 deletions middleware/README.md
@@ -0,0 +1,3 @@
# middleware

Nuxt Server Middleware
3 changes: 3 additions & 0 deletions middleware/lib/index.js
@@ -0,0 +1,3 @@
const { middleware } = require('../native')

export default middleware
128 changes: 128 additions & 0 deletions middleware/native/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions middleware/native/Cargo.toml
@@ -0,0 +1,17 @@
[package]
name = "middleware"
version = "0.1.0"
authors = ["Jonas Galvez <jonasgalvez@gmail.com>"]
license = "MIT"
build = "build.rs"
exclude = ["artifacts.json", "index.node"]

[lib]
name = "middleware"
crate-type = ["dylib"]

[build-dependencies]
neon-build = "0.2.0"

[dependencies]
neon = "0.2.0"
7 changes: 7 additions & 0 deletions middleware/native/build.rs
@@ -0,0 +1,7 @@
extern crate neon_build;

fn main() {
neon_build::setup(); // must be called in build.rs

// add project-specific build logic here...
}
31 changes: 31 additions & 0 deletions middleware/native/src/lib.rs
@@ -0,0 +1,31 @@
#[macro_use]
extern crate neon;

use neon::prelude::*;

fn middleware(mut ctx: FunctionContext) -> JsResult<JsUndefined> {
// If we also need the request object:
// let req = ctx.argument::<JsObject>(0)?;
let res = ctx.argument::<JsObject>(1)?;
let next = ctx.argument::<JsFunction>(2)?;

// Create a JsString with the Neon helper
let message = ctx.string("Hello from Rust");
res.set(&mut ctx, "neonMessage", message)?;

// Empt argument list for next(), must be Vec<Handle>
let args: Vec<Handle<JsValue>> = vec![];
// Get a null reference with the Neon helper
let null = ctx.null();
// Call serverMiddleware's next() function
next.call(&mut ctx, null, args)?;

// Must return JsUndefined
Ok(ctx.undefined())
}

// Neon has no support for default exports yet
register_module!(mut ctx, {
ctx.export_function("middleware", middleware)
});

14 changes: 14 additions & 0 deletions middleware/package.json
@@ -0,0 +1,14 @@
{
"name": "middleware",
"version": "0.1.0",
"description": "Nuxt Server Middleware",
"main": "lib/index.js",
"author": "Jonas Galvez <jonasgalvez@gmail.com>",
"license": "MIT",
"dependencies": {
"neon-cli": "^0.2.0"
},
"scripts": {
"install": "neon build"
}
}

0 comments on commit e05145b

Please sign in to comment.