fix: 7z and streams

This commit is contained in:
DecDuck
2025-11-20 13:39:05 +11:00
parent 894f2b354a
commit efab43720f
5 changed files with 55 additions and 73 deletions

View File

@ -67,7 +67,7 @@ test("read file", async (t) => {
let finalString = ""; let finalString = "";
for await (const chunk of stream.getStream()) { for await (const chunk of stream) {
// Do something with each 'chunk' // Do something with each 'chunk'
finalString += String.fromCharCode.apply(null, chunk); finalString += String.fromCharCode.apply(null, chunk);
} }
@ -94,7 +94,7 @@ test("read file offset", async (t) => {
let finalString = ""; let finalString = "";
for await (const chunk of stream.getStream()) { for await (const chunk of stream) {
// Do something with each 'chunk' // Do something with each 'chunk'
finalString += String.fromCharCode.apply(null, chunk); finalString += String.fromCharCode.apply(null, chunk);
} }
@ -121,7 +121,7 @@ test.skip("zip speed test", async (t) => {
const timeThreshold = BigInt(1_000_000_000); const timeThreshold = BigInt(1_000_000_000);
let runningTotal = 0; let runningTotal = 0;
let runningTime = BigInt(0); let runningTime = BigInt(0);
for await (const chunk of stream.getStream()) { for await (const chunk of stream) {
// Do something with each 'chunk' // Do something with each 'chunk'
const currentTime = process.hrtime.bigint(); const currentTime = process.hrtime.bigint();
const timeDiff = currentTime - lastTime; const timeDiff = currentTime - lastTime;
@ -147,12 +147,16 @@ test.skip("zip speed test", async (t) => {
}); });
test("zip manifest test", async (t) => { test("zip manifest test", async (t) => {
const zipFiles = fs.readdirSync("./assets").filter((v) => v.endsWith(".zip"));
const dropletHandler = new DropletHandler(); const dropletHandler = new DropletHandler();
for (const zipFile of zipFiles) {
console.log("generating manifest for " + zipFile);
const manifest = JSON.parse( const manifest = JSON.parse(
await new Promise((r, e) => await new Promise((r, e) =>
generateManifest( generateManifest(
dropletHandler, dropletHandler,
"./assets/TheGame.zip", "./assets/" + zipFile,
(_, __) => {}, (_, __) => {},
(_, __) => {}, (_, __) => {},
(err, manifest) => (err ? e(err) : r(manifest)) (err, manifest) => (err ? e(err) : r(manifest))
@ -161,18 +165,18 @@ test("zip manifest test", async (t) => {
); );
for (const [filename, data] of Object.entries(manifest)) { for (const [filename, data] of Object.entries(manifest)) {
console.log(filename);
let start = 0; let start = 0;
for (const [chunkIndex, length] of data.lengths.entries()) { for (const [chunkIndex, length] of data.lengths.entries()) {
const hash = createHash("md5"); const hash = createHash("md5");
const stream = ( const stream = (
await dropletHandler.readFile( await dropletHandler.readFile(
"./assets/TheGame.zip", "./assets/" + zipFile,
filename, filename,
BigInt(start), BigInt(start),
BigInt(start + length) BigInt(start + length)
) )
).getStream(); );
console.log(stream);
let streamLength = 0; let streamLength = 0;
await stream.pipeTo( await stream.pipeTo(
@ -198,6 +202,7 @@ test("zip manifest test", async (t) => {
start += length; start += length;
} }
} }
}
t.pass(); t.pass();
}); });

6
index.d.ts vendored
View File

@ -8,11 +8,7 @@ export declare class DropletHandler {
hasBackendForPath(path: string): boolean hasBackendForPath(path: string): boolean
listFiles(path: string): Array<string> listFiles(path: string): Array<string>
peekFile(path: string, subPath: string): bigint peekFile(path: string, subPath: string): bigint
readFile(path: string, subPath: string, start?: bigint | undefined | null, end?: bigint | undefined | null): JsDropStreamable readFile(path: string, subPath: string, start?: bigint | undefined | null, end?: bigint | undefined | null): ReadableStream
}
export declare class JsDropStreamable {
getStream(): any
} }
export declare class Script { export declare class Script {

View File

@ -377,7 +377,6 @@ if (!nativeBinding) {
module.exports = nativeBinding module.exports = nativeBinding
module.exports.DropletHandler = nativeBinding.DropletHandler module.exports.DropletHandler = nativeBinding.DropletHandler
module.exports.JsDropStreamable = nativeBinding.JsDropStreamable
module.exports.Script = nativeBinding.Script module.exports.Script = nativeBinding.Script
module.exports.ScriptEngine = nativeBinding.ScriptEngine module.exports.ScriptEngine = nativeBinding.ScriptEngine
module.exports.callAltThreadFunc = nativeBinding.callAltThreadFunc module.exports.callAltThreadFunc = nativeBinding.callAltThreadFunc

View File

@ -1,6 +1,6 @@
{ {
"name": "@drop-oss/droplet", "name": "@drop-oss/droplet",
"version": "3.3.0", "version": "3.4.0",
"main": "index.js", "main": "index.js",
"types": "index.d.ts", "types": "index.d.ts",
"napi": { "napi": {

View File

@ -38,9 +38,7 @@ pub fn create_backend_constructor<'a>(
let status = test.status().ok()?; let status = test.status().ok()?;
if status.code().unwrap_or(1) == 0 { if status.code().unwrap_or(1) == 0 {
let buf = path.to_path_buf(); let buf = path.to_path_buf();
return Some(Box::new(move || { return Some(Box::new(move || Ok(Box::new(ZipVersionBackend::new(buf)?))));
Ok(Box::new(ZipVersionBackend::new(buf)?))
}));
} }
} }
@ -111,7 +109,7 @@ impl<'a> DropletHandler<'a> {
Ok(file.size) Ok(file.size)
} }
#[napi] #[napi(ts_return_type = "ReadableStream")]
pub fn read_file( pub fn read_file(
&mut self, &mut self,
reference: Reference<DropletHandler<'static>>, reference: Reference<DropletHandler<'static>>,
@ -120,7 +118,7 @@ impl<'a> DropletHandler<'a> {
env: Env, env: Env,
start: Option<BigInt>, start: Option<BigInt>,
end: Option<BigInt>, end: Option<BigInt>,
) -> anyhow::Result<JsDropStreamable> { ) -> anyhow::Result<*mut napi_value__> {
let stream = reference.share_with(env, |handler| { let stream = reference.share_with(env, |handler| {
let backend = handler let backend = handler
.create_backend_for_path(path) .create_backend_for_path(path)
@ -149,25 +147,9 @@ impl<'a> DropletHandler<'a> {
// Apply Result::map_err to transform Err(std::io::Error) to Err(napi::Error) // Apply Result::map_err to transform Err(std::io::Error) to Err(napi::Error)
.map_err(napi::Error::from) // napi::Error implements From<tokio::io::Error> .map_err(napi::Error::from) // napi::Error implements From<tokio::io::Error>
}); });
// Create the napi-rs ReadableStream from the tokio_stream::Stream
// The unwrap() here means if stream creation fails, it will panic.
// For a production system, consider returning Result<Option<...>> and handling this.
ReadableStream::create_with_stream_bytes(&env, stream) ReadableStream::create_with_stream_bytes(&env, stream)
})?; })?;
Ok(JsDropStreamable { inner: stream }) Ok(stream.raw())
}
}
#[napi]
pub struct JsDropStreamable {
inner: SharedReference<DropletHandler<'static>, ReadableStream<'static, BufferSlice<'static>>>,
}
#[napi]
impl JsDropStreamable {
#[napi]
pub fn get_stream(&self) -> *mut napi_value__ {
self.inner.raw()
} }
} }