Rename 'Plugin' to 'Middleware' across the server code

This commit is contained in:
Lucien Greathouse 2018-04-04 16:26:08 -07:00
parent e3e28d225c
commit 95d7c61921
11 changed files with 62 additions and 57 deletions

View file

@ -110,3 +110,8 @@ If a mismatch is detected, the server should immediately abort the request and r
If the client detects an error in this way, it should delete any connection metadata, including `Id` mappings -- this data is no longer relevant to any new connections. If the client detects an error in this way, it should delete any connection metadata, including `Id` mappings -- this data is no longer relevant to any new connections.
If the returned project name matches the value that the client was using before, the client should begin a *session restart* by reinitializing the connection to the server and creating new metadata. If the returned project name matches the value that the client was using before, the client should begin a *session restart* by reinitializing the connection to the server and creating new metadata.
## Other Changes
These changes aren't directly related to this refactor, but are changes that need to be made to Rojo.
* Unknown instances will no longer sync as `StringValue` instances

View file

@ -14,8 +14,8 @@ pub mod project;
pub mod pathext; pub mod pathext;
pub mod vfs; pub mod vfs;
pub mod rbx; pub mod rbx;
pub mod plugin; pub mod middleware;
pub mod plugins; pub mod middlewares;
pub mod commands; pub mod commands;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};

View file

@ -6,8 +6,8 @@ use std::thread;
use rand; use rand;
use project::{Project, ProjectLoadError}; use project::{Project, ProjectLoadError};
use plugin::{PluginChain}; use middleware::{MiddlewareChain};
use plugins::{DefaultPlugin, JsonModelPlugin, ScriptPlugin}; use middlewares::{DefaultMiddleware, JsonModelMiddleware, ScriptMiddleware};
use vfs::{VfsSession, VfsWatcher}; use vfs::{VfsSession, VfsWatcher};
use web; use web;
@ -59,10 +59,10 @@ pub fn serve(project_path: &PathBuf, verbose: bool, port: Option<u64>) {
}; };
lazy_static! { lazy_static! {
static ref PLUGIN_CHAIN: PluginChain = PluginChain::new(vec![ static ref PLUGIN_CHAIN: MiddlewareChain = MiddlewareChain::new(vec![
Box::new(ScriptPlugin::new()), Box::new(ScriptMiddleware::new()),
Box::new(JsonModelPlugin::new()), Box::new(JsonModelMiddleware::new()),
Box::new(DefaultPlugin::new()), Box::new(DefaultMiddleware::new()),
]); ]);
} }

View file

@ -21,10 +21,10 @@ pub enum FileChangeResult {
Pass, Pass,
} }
pub trait Plugin { pub trait Middleware {
/// Invoked when a file is read from the filesystem and needs to be turned /// Invoked when a file is read from the filesystem and needs to be turned
/// into a Roblox instance. /// into a Roblox instance.
fn transform_file(&self, plugins: &PluginChain, vfs_item: &VfsItem) -> TransformFileResult; fn transform_file(&self, plugins: &MiddlewareChain, vfs_item: &VfsItem) -> TransformFileResult;
/// Invoked when a Roblox Instance change is reported by the Roblox Studio /// Invoked when a Roblox Instance change is reported by the Roblox Studio
/// plugin and needs to be turned into a file to save. /// plugin and needs to be turned into a file to save.
@ -36,13 +36,13 @@ pub trait Plugin {
} }
/// A set of plugins that are composed in order. /// A set of plugins that are composed in order.
pub struct PluginChain { pub struct MiddlewareChain {
plugins: Vec<Box<Plugin + Send + Sync>>, plugins: Vec<Box<Middleware + Send + Sync>>,
} }
impl PluginChain { impl MiddlewareChain {
pub fn new(plugins: Vec<Box<Plugin + Send + Sync>>) -> PluginChain { pub fn new(plugins: Vec<Box<Middleware + Send + Sync>>) -> MiddlewareChain {
PluginChain { MiddlewareChain {
plugins, plugins,
} }
} }

View file

@ -1,23 +1,23 @@
use std::collections::HashMap; use std::collections::HashMap;
use core::Route; use core::Route;
use plugin::{Plugin, PluginChain, TransformFileResult, RbxChangeResult, FileChangeResult}; use middleware::{Middleware, MiddlewareChain, TransformFileResult, RbxChangeResult, FileChangeResult};
use rbx::{RbxInstance, RbxValue}; use rbx::{RbxInstance, RbxValue};
use vfs::VfsItem; use vfs::VfsItem;
/// A plugin with simple transforms: /// A middleware with simple transforms:
/// * Directories become Folder instances /// * Directories become Folder instances
/// * Files become StringValue objects with 'Value' as their contents /// * Files become StringValue objects with 'Value' as their contents
pub struct DefaultPlugin; pub struct DefaultMiddleware;
impl DefaultPlugin { impl DefaultMiddleware {
pub fn new() -> DefaultPlugin { pub fn new() -> DefaultMiddleware {
DefaultPlugin DefaultMiddleware
} }
} }
impl Plugin for DefaultPlugin { impl Middleware for DefaultMiddleware {
fn transform_file(&self, plugins: &PluginChain, vfs_item: &VfsItem) -> TransformFileResult { fn transform_file(&self, middlewares: &MiddlewareChain, vfs_item: &VfsItem) -> TransformFileResult {
match vfs_item { match vfs_item {
&VfsItem::File { ref contents, .. } => { &VfsItem::File { ref contents, .. } => {
let mut properties = HashMap::new(); let mut properties = HashMap::new();
@ -38,7 +38,7 @@ impl Plugin for DefaultPlugin {
let mut rbx_children = Vec::new(); let mut rbx_children = Vec::new();
for (_, child_item) in children { for (_, child_item) in children {
match plugins.transform_file(child_item) { match middlewares.transform_file(child_item) {
Some(rbx_item) => { Some(rbx_item) => {
rbx_children.push(rbx_item); rbx_children.push(rbx_item);
}, },

View file

@ -2,7 +2,7 @@ use regex::Regex;
use serde_json; use serde_json;
use core::Route; use core::Route;
use plugin::{Plugin, PluginChain, TransformFileResult, RbxChangeResult, FileChangeResult}; use middleware::{Middleware, MiddlewareChain, TransformFileResult, RbxChangeResult, FileChangeResult};
use rbx::RbxInstance; use rbx::RbxInstance;
use vfs::VfsItem; use vfs::VfsItem;
@ -10,16 +10,16 @@ lazy_static! {
static ref JSON_MODEL_PATTERN: Regex = Regex::new(r"^(.*?)\.model\.json$").unwrap(); static ref JSON_MODEL_PATTERN: Regex = Regex::new(r"^(.*?)\.model\.json$").unwrap();
} }
pub struct JsonModelPlugin; pub struct JsonModelMiddleware;
impl JsonModelPlugin { impl JsonModelMiddleware {
pub fn new() -> JsonModelPlugin { pub fn new() -> JsonModelMiddleware {
JsonModelPlugin JsonModelMiddleware
} }
} }
impl Plugin for JsonModelPlugin { impl Middleware for JsonModelMiddleware {
fn transform_file(&self, _plugins: &PluginChain, vfs_item: &VfsItem) -> TransformFileResult { fn transform_file(&self, _middlewares: &MiddlewareChain, vfs_item: &VfsItem) -> TransformFileResult {
match vfs_item { match vfs_item {
&VfsItem::File { ref contents, .. } => { &VfsItem::File { ref contents, .. } => {
let rbx_name = match JSON_MODEL_PATTERN.captures(vfs_item.name()) { let rbx_name = match JSON_MODEL_PATTERN.captures(vfs_item.name()) {

View file

@ -0,0 +1,7 @@
mod default_middleware;
mod script_middleware;
mod json_model_middleware;
pub use self::default_middleware::*;
pub use self::script_middleware::*;
pub use self::json_model_middleware::*;

View file

@ -3,7 +3,7 @@ use std::collections::HashMap;
use regex::Regex; use regex::Regex;
use core::Route; use core::Route;
use plugin::{Plugin, PluginChain, TransformFileResult, RbxChangeResult, FileChangeResult}; use middleware::{Middleware, MiddlewareChain, TransformFileResult, RbxChangeResult, FileChangeResult};
use rbx::{RbxInstance, RbxValue}; use rbx::{RbxInstance, RbxValue};
use vfs::VfsItem; use vfs::VfsItem;
@ -17,16 +17,16 @@ static SERVER_INIT: &'static str = "init.server.lua";
static CLIENT_INIT: &'static str = "init.client.lua"; static CLIENT_INIT: &'static str = "init.client.lua";
static MODULE_INIT: &'static str = "init.lua"; static MODULE_INIT: &'static str = "init.lua";
pub struct ScriptPlugin; pub struct ScriptMiddleware;
impl ScriptPlugin { impl ScriptMiddleware {
pub fn new() -> ScriptPlugin { pub fn new() -> ScriptMiddleware {
ScriptPlugin ScriptMiddleware
} }
} }
impl Plugin for ScriptPlugin { impl Middleware for ScriptMiddleware {
fn transform_file(&self, plugins: &PluginChain, vfs_item: &VfsItem) -> TransformFileResult { fn transform_file(&self, middlewares: &MiddlewareChain, vfs_item: &VfsItem) -> TransformFileResult {
match vfs_item { match vfs_item {
&VfsItem::File { ref contents, .. } => { &VfsItem::File { ref contents, .. } => {
let name = vfs_item.name(); let name = vfs_item.name();
@ -69,10 +69,10 @@ impl Plugin for ScriptPlugin {
} }
}; };
let mut rbx_item = match self.transform_file(plugins, init_item) { let mut rbx_item = match self.transform_file(middlewares, init_item) {
TransformFileResult::Value(Some(item)) => item, TransformFileResult::Value(Some(item)) => item,
_ => { _ => {
eprintln!("Inconsistency detected in ScriptPlugin!"); eprintln!("Inconsistency detected in ScriptMiddleware!");
return TransformFileResult::Pass; return TransformFileResult::Pass;
}, },
}; };
@ -85,7 +85,7 @@ impl Plugin for ScriptPlugin {
continue; continue;
} }
match plugins.transform_file(child_item) { match middlewares.transform_file(child_item) {
Some(child_rbx_item) => { Some(child_rbx_item) => {
rbx_item.children.push(child_rbx_item); rbx_item.children.push(child_rbx_item);
}, },

View file

@ -1,7 +0,0 @@
mod default_plugin;
mod script_plugin;
mod json_model_plugin;
pub use self::default_plugin::*;
pub use self::script_plugin::*;
pub use self::json_model_plugin::*;

View file

@ -4,7 +4,7 @@ use std::io::Read;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::time::Instant; use std::time::Instant;
use plugin::PluginChain; use middleware::MiddlewareChain;
use vfs::VfsItem; use vfs::VfsItem;
/// Represents a virtual layer over multiple parts of the filesystem. /// Represents a virtual layer over multiple parts of the filesystem.
@ -24,7 +24,7 @@ pub struct VfsSession {
/// When the Vfs was initialized; used for change tracking. /// When the Vfs was initialized; used for change tracking.
start_time: Instant, start_time: Instant,
plugin_chain: &'static PluginChain, middleware_chain: &'static MiddlewareChain,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
@ -35,12 +35,12 @@ pub struct VfsChange {
} }
impl VfsSession { impl VfsSession {
pub fn new(plugin_chain: &'static PluginChain) -> VfsSession { pub fn new(middleware_chain: &'static MiddlewareChain) -> VfsSession {
VfsSession { VfsSession {
partitions: HashMap::new(), partitions: HashMap::new(),
start_time: Instant::now(), start_time: Instant::now(),
change_history: Vec::new(), change_history: Vec::new(),
plugin_chain, middleware_chain,
} }
} }
@ -164,7 +164,7 @@ impl VfsSession {
/// Register a new change to the filesystem at the given timestamp and VFS /// Register a new change to the filesystem at the given timestamp and VFS
/// route. /// route.
pub fn add_change(&mut self, timestamp: f64, route: Vec<String>) { pub fn add_change(&mut self, timestamp: f64, route: Vec<String>) {
match self.plugin_chain.handle_file_change(&route) { match self.middleware_chain.handle_file_change(&route) {
Some(routes) => { Some(routes) => {
for route in routes { for route in routes {
self.change_history.push(VfsChange { self.change_history.push(VfsChange {

View file

@ -8,7 +8,7 @@ use serde_json;
use project::Project; use project::Project;
use vfs::{VfsSession, VfsChange}; use vfs::{VfsSession, VfsChange};
use rbx::RbxInstance; use rbx::RbxInstance;
use plugin::PluginChain; use middleware::MiddlewareChain;
static MAX_BODY_SIZE: usize = 25 * 1024 * 1024; // 25 MiB static MAX_BODY_SIZE: usize = 25 * 1024 * 1024; // 25 MiB
@ -117,7 +117,7 @@ where
} }
/// Start the Rojo web server and park our current thread. /// Start the Rojo web server and park our current thread.
pub fn start(config: WebConfig, project: Project, plugin_chain: &'static PluginChain, vfs: Arc<Mutex<VfsSession>>) { pub fn start(config: WebConfig, project: Project, middleware_chain: &'static MiddlewareChain, vfs: Arc<Mutex<VfsSession>>) {
let address = format!("localhost:{}", config.port); let address = format!("localhost:{}", config.port);
let server_id = config.server_id.to_string(); let server_id = config.server_id.to_string();
@ -190,7 +190,7 @@ pub fn start(config: WebConfig, project: Project, plugin_chain: &'static PluginC
.iter() .iter()
.map(|item| { .map(|item| {
match *item { match *item {
Some(ref item) => plugin_chain.transform_file(item), Some(ref item) => middleware_chain.transform_file(item),
None => None, None => None,
} }
}) })