-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Main function for ParcelV3 and FileSystem implementation (#9758)
* checkpoint * getting somewhere * async for napi * tidy up * Tidy up * Tidy * integraiton tests * reverting send/sync * Update * Unused dependency * linting * lock file * Refactor to use thread-safe functions directly * unused imports, utf8 * integration tests * Fix warnings and add logs * Remove some log lines * Revert +nightly bound from rustfmt --------- Co-authored-by: pyamada (Remote Dev Environment) <[email protected]>
- Loading branch information
1 parent
5bfc9aa
commit e12a947
Showing
14 changed files
with
771 additions
and
543 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
name = "json_comments" | ||
version = "0.2.1" | ||
authors = ["Thayne McCombs <[email protected]>"] | ||
edition = "2018" | ||
edition = "2021" | ||
license = "Apache-2.0" | ||
readme = "README.md" | ||
repository = "https://github.com/tmccombs/json-comments-rs" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use std::path::Path; | ||
use std::path::PathBuf; | ||
|
||
use napi::bindgen_prelude::FromNapiValue; | ||
use napi::threadsafe_function::ThreadsafeFunctionCallMode; | ||
use napi::Env; | ||
use napi::JsFunction; | ||
use napi::JsObject; | ||
use parcel_filesystem::FileSystem; | ||
use serde::de::DeserializeOwned; | ||
use serde::Serialize; | ||
|
||
// TODO error handling | ||
|
||
pub struct FileSystemNapi { | ||
read_file_fn: Box<dyn Fn((PathBuf, String)) -> String + Send + Sync>, | ||
is_file_fn: Box<dyn Fn(PathBuf) -> bool + Send + Sync>, | ||
is_dir_fn: Box<dyn Fn(PathBuf) -> bool + Send + Sync>, | ||
} | ||
|
||
impl FileSystemNapi { | ||
pub fn new(env: &Env, js_file_system: JsObject) -> napi::Result<Self> { | ||
Ok(Self { | ||
read_file_fn: Box::new(create_js_thread_safe_method( | ||
&env, | ||
&js_file_system, | ||
"readFileSync", | ||
)?), | ||
is_file_fn: Box::new(create_js_thread_safe_method( | ||
&env, | ||
&js_file_system, | ||
"isFile", | ||
)?), | ||
is_dir_fn: Box::new(create_js_thread_safe_method( | ||
&env, | ||
&js_file_system, | ||
"isDir", | ||
)?), | ||
}) | ||
} | ||
} | ||
|
||
// These methods must be run off the nodejs main/worker | ||
// thread or they will cause JavaScript to deadlock | ||
impl FileSystem for FileSystemNapi { | ||
fn read_to_string(&self, path: &Path) -> std::io::Result<String> { | ||
Ok((*self.read_file_fn)(( | ||
path.to_path_buf(), | ||
"utf8".to_string(), | ||
))) | ||
} | ||
|
||
fn is_file(&self, path: &Path) -> bool { | ||
(*self.is_file_fn)(path.to_path_buf()) | ||
} | ||
|
||
fn is_dir(&self, path: &Path) -> bool { | ||
(*self.is_dir_fn)(path.to_path_buf()) | ||
} | ||
} | ||
|
||
fn create_js_thread_safe_method< | ||
Params: Send + Serialize + 'static, | ||
Response: Send + DeserializeOwned + 'static + FromNapiValue, | ||
>( | ||
env: &Env, | ||
js_file_system: &JsObject, | ||
method_name: &str, | ||
) -> Result<impl Fn(Params) -> Response, napi::Error> { | ||
let jsfn: JsFunction = js_file_system.get_property(env.create_string(method_name)?)?; | ||
|
||
let threadsafe_function = env.create_threadsafe_function( | ||
&jsfn, | ||
0, | ||
|ctx: napi::threadsafe_function::ThreadSafeCallContext<Params>| { | ||
Ok(vec![ctx.env.to_js_value(&ctx.value)?]) | ||
}, | ||
)?; | ||
let result = move |params| { | ||
let (tx, rx) = std::sync::mpsc::sync_channel(1); | ||
threadsafe_function.call_with_return_value( | ||
Ok(params), | ||
ThreadsafeFunctionCallMode::Blocking, | ||
move |result: Response| { | ||
let _ = tx.send(result); | ||
Ok(()) | ||
}, | ||
); | ||
rx.recv().unwrap() | ||
}; | ||
|
||
Ok(result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod file_system_napi; | ||
pub use self::file_system_napi::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod parcel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use std::path::PathBuf; | ||
use std::sync::Arc; | ||
|
||
use napi::Env; | ||
use napi::JsObject; | ||
use napi_derive::napi; | ||
use parcel_core::FileSystemRef; | ||
use parcel_core::Parcel; | ||
use parcel_core::ParcelOptions; | ||
|
||
use crate::file_system::FileSystemNapi; | ||
|
||
#[napi] | ||
pub struct ParcelNapi { | ||
internal: Arc<Parcel>, | ||
} | ||
|
||
#[napi] | ||
impl ParcelNapi { | ||
#[napi(constructor)] | ||
pub fn new(env: Env, options: JsObject) -> napi::Result<Self> { | ||
let _ = tracing_subscriber::fmt::try_init(); | ||
|
||
let thread_id = std::thread::current().id(); | ||
tracing::trace!(?thread_id, "parcel-napi initialize"); | ||
|
||
let mut fs = None::<FileSystemRef>; | ||
|
||
if options.has_named_property("fs")? { | ||
let fs_raw: JsObject = options.get_named_property("fs")?; | ||
fs.replace(Arc::new(FileSystemNapi::new(&env, fs_raw)?)); | ||
} | ||
|
||
let parcel = Parcel::new(ParcelOptions { fs }); | ||
|
||
Ok(Self { | ||
internal: Arc::new(parcel), | ||
}) | ||
} | ||
|
||
// Temporary, for testing | ||
#[napi] | ||
pub async fn _testing_temp_fs_read_to_string(&self, path: String) -> napi::Result<String> { | ||
Ok(self.internal.fs.read_to_string(&PathBuf::from(path))?) | ||
} | ||
|
||
#[napi] | ||
pub async fn _testing_temp_fs_is_file(&self, path: String) -> napi::Result<bool> { | ||
Ok(self.internal.fs.is_file(&PathBuf::from(path))) | ||
} | ||
|
||
#[napi] | ||
pub async fn _testing_temp_fs_is_dir(&self, path: String) -> napi::Result<bool> { | ||
Ok(self.internal.fs.is_dir(&PathBuf::from(path))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,24 @@ | ||
// TODO eventual public API for Parcel | ||
pub struct Parcel {} | ||
use std::sync::Arc; | ||
|
||
use parcel_filesystem::os_file_system::OsFileSystem; | ||
use parcel_filesystem::FileSystem; | ||
|
||
pub type FileSystemRef = Arc<dyn FileSystem + Send + Sync>; | ||
|
||
pub struct Parcel { | ||
pub fs: FileSystemRef, | ||
} | ||
|
||
pub struct ParcelOptions { | ||
pub fs: Option<FileSystemRef>, | ||
} | ||
|
||
impl Parcel { | ||
pub fn run() { | ||
todo!(); | ||
} | ||
pub fn new(options: ParcelOptions) -> Self { | ||
let fs = options | ||
.fs | ||
.unwrap_or_else(|| Arc::new(OsFileSystem::default())); | ||
|
||
pub fn watch() { | ||
todo!(); | ||
Self { fs } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// @flow | ||
|
||
import assert from 'assert'; | ||
import path from 'path'; | ||
import {bundle, run} from '@parcel/test-utils'; | ||
import * as napi from '@parcel/rust'; | ||
import {inputFS} from '@parcel/test-utils'; | ||
|
||
describe('parcel-v3', function () { | ||
// Duplicated temporarily for convenience, will remove once the Rust stuff works | ||
it.skip('should produce a basic JS bundle with CommonJS requires', async function () { | ||
let b = await bundle( | ||
path.join(__dirname, '/integration/commonjs/index.js'), | ||
{ | ||
featureFlags: {parcelV3: true}, | ||
}, | ||
); | ||
|
||
// assert.equal(b.assets.size, 8); | ||
// assert.equal(b.childBundles.size, 1); | ||
|
||
let output = await run(b); | ||
assert.equal(typeof output, 'function'); | ||
assert.equal(output(), 3); | ||
}); | ||
|
||
it('should run the main-thread bootstrap function', async function () { | ||
let p = new napi.ParcelNapi({ | ||
fs: { | ||
readFileSync: (_, [...args]) => inputFS.readFileSync(...args), | ||
isFile: (_, path) => inputFS.statSync(path).isFile(), | ||
isDir: (_, path) => inputFS.statSync(path).isDirectory(), | ||
}, | ||
}); | ||
|
||
assert(typeof (await p.testingTempFsReadToString(__filename)) === 'string'); | ||
assert(!(await p.testingTempFsIsDir(__filename))); | ||
assert(await p.testingTempFsIsFile(__filename)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
edition = "2021" | ||
tab_spaces = 2 |