@@ -472,16 +472,19 @@ namespace hdiffpatchNode
472472 DiffWindowAsyncWorker (Napi::Function& callback,
473473 std::string oldPath,
474474 std::string newPath,
475- std::string outDiffPath)
475+ std::string outDiffPath,
476+ size_t windowSize)
476477 : Napi::AsyncWorker(callback),
477478 oldPath_ (std::move(oldPath)),
478479 newPath_(std::move(newPath)),
479- outDiffPath_(std::move(outDiffPath)) {
480+ outDiffPath_(std::move(outDiffPath)),
481+ windowSize_(windowSize) {
480482 }
481483
482484 void Execute () override {
483485 try {
484- hdiff_window (oldPath_.c_str (), newPath_.c_str (), outDiffPath_.c_str ());
486+ hdiff_window (oldPath_.c_str (), newPath_.c_str (), outDiffPath_.c_str (),
487+ windowSize_);
485488 } catch (const std::exception& e) {
486489 SetError (e.what ());
487490 }
@@ -503,6 +506,7 @@ namespace hdiffpatchNode
503506 std::string oldPath_;
504507 std::string newPath_;
505508 std::string outDiffPath_;
509+ size_t windowSize_;
506510 };
507511
508512 // ============ 同步/异步 diffSingleStream ============
@@ -546,6 +550,9 @@ namespace hdiffpatchNode
546550 // single 格式(HDIFFSF20)的 window 模式生成:大块流式匹配 + 窗口内
547551 // 后缀串精修,匹配质量接近内存版 diff() 而内存占用保持流式档。
548552 // 产物与 diff()/diffSingleStream() 同格式,既有应用端可直接应用。
553+ // 签名:(oldPath, newPath, outDiffPath[, windowSize][, cb])
554+ // windowSize 为 old 数据滑动窗口字节数(缺省 2MB),调大可捕获更长
555+ // 距离的内容移动,内存占用近似线性增长。
549556 Napi::Value diffWindow (const Napi::CallbackInfo& info) {
550557 Napi::Env env = info.Env ();
551558
@@ -556,22 +563,36 @@ namespace hdiffpatchNode
556563 !getStringUtf8 (info[0 ], oldPath) ||
557564 !getStringUtf8 (info[1 ], newPath) ||
558565 !getStringUtf8 (info[2 ], outDiffPath)) {
559- Napi::TypeError::New (env, " Invalid arguments: expected (oldPath, newPath, outDiffPath)." )
566+ Napi::TypeError::New (env, " Invalid arguments: expected (oldPath, newPath, outDiffPath[, windowSize][, cb] )." )
560567 .ThrowAsJavaScriptException ();
561568 return env.Undefined ();
562569 }
563570
564- if (info.Length () > 3 && info[3 ].IsFunction ()) {
565- Napi::Function callback = info[3 ].As <Napi::Function>();
571+ size_t windowSize = 0 ; // 0 = 使用默认窗口(2MB)
572+ size_t argIdx = 3 ;
573+ if (info.Length () > argIdx && info[argIdx].IsNumber ()) {
574+ double raw = info[argIdx].As <Napi::Number>().DoubleValue ();
575+ if (!std::isfinite (raw) || raw < 0 ||
576+ raw > static_cast <double >(std::numeric_limits<size_t >::max ())) {
577+ Napi::TypeError::New (env, " Invalid windowSize: expected a non-negative integer." )
578+ .ThrowAsJavaScriptException ();
579+ return env.Undefined ();
580+ }
581+ windowSize = static_cast <size_t >(raw);
582+ argIdx++;
583+ }
584+
585+ if (info.Length () > argIdx && info[argIdx].IsFunction ()) {
586+ Napi::Function callback = info[argIdx].As <Napi::Function>();
566587 DiffWindowAsyncWorker* worker = new DiffWindowAsyncWorker (
567- callback, oldPath, newPath, outDiffPath
588+ callback, oldPath, newPath, outDiffPath, windowSize
568589 );
569590 worker->Queue ();
570591 return env.Undefined ();
571592 }
572593
573594 try {
574- hdiff_window (oldPath.c_str (), newPath.c_str (), outDiffPath.c_str ());
595+ hdiff_window (oldPath.c_str (), newPath.c_str (), outDiffPath.c_str (), windowSize );
575596 } catch (const std::exception& e) {
576597 Napi::Error::New (env, e.what ()).ThrowAsJavaScriptException ();
577598 return env.Undefined ();
0 commit comments