
NetGrep هو نقل تجريبي لـ RIPGREP على WASM باستخدام بروتوكول HTTP. يتمثل نطاق هذا المشروع في توفير بديل قابل للتطبيق لمحركات البحث المستندة إلى الفهرس للتطبيقات مع قاعدة بيانات صغيرة قائمة على الملفات. يستخدم شوكة من مستودع ripgrep الأصلي مع تغييرات كافية فقط لجعلها قابلة للتشغيل على WASM.
في الوقت الحالي ، ستعرف NetGrep فقط ما إذا كان النمط موجودًا على ملف بعيد يستفيد من محرك البحث الأساسي ripgrep . يحدث هذا أثناء تنزيل الملف من أجل زيادة الأداء إلى الحد الأقصى.
لاحظ أن البحث عن المنشورات على مدونة تم إنشاؤه من خلال مولد موقع ثابت هو حالة استخدام مثيرة للاهتمام لهذه التجربة. يمكن استخدام NetGrep بسهولة لإنشاء محرك بحث في الوقت الفعلي من ملفات النشر الخام. يمكن العثور على مثال مباشر لهذا السلوك على مدونتي (يمكنك إلقاء نظرة على الكود المصدري).
تحذير في الوقت الحالي يتم تصدير هذه المكتبة فقط على أنها ESM ، وبالتالي مطلوب حزمة مثل WebPack لاستخدامها.
لاحظ أن هذا البرنامج التعليمي القصير يفترض أن WebPack 5 هو الحزمة المستخدمة في التطبيق حيث سيتم دمج
netgrep. يتوفر مثال كامل في حزمةexample.
بادئ ذي بدء ، قم بتثبيت الوحدة النمطية:
# Using yarn
yarn add @netgrep/netgrep
# Using npm
npm install @netgrep/netgrep يجب تمكين علامة تجربة asyncWebAssembbly داخل webpack.config.js :
module . exports = {
//...
experiments : {
// ...
asyncWebAssembly : true ,
} ,
} ; بعد ذلك ، سيكون من الممكن تنفيذ netgrep مباشرةً بينما سيتم تحميل ملف WASM المجمعة بشكل غير متزامن في الخلفية:
import { Netgrep } from '@netgrep/netgrep' ;
// Create a Netgrep instance, here it's
// possible to pass an initial configuration.
const NG = new Netgrep ( ) ;
// Execute a Netgrep search on the url using
// the given pattern.
NG . search ( "url" , "pattern" )
. then ( ( output ) => {
console . log ( 'The pattern has matched' , output . result )
} ) ;
// It's possible to pass custom metadata during
// the search. These will be returned back in the result
// for convenience.
NG . search ( "url" , "pattern" , { post } )
. then ( ( output ) => {
console . log ( 'The pattern has matched' , output . result )
console . log ( 'Metadata' , output . metadata )
} ) ;
// There is a convenient method to do batched searches
// to multiple urls. Using `searchBatch` the resulting `Promise`
// will resolve only after the completion of all the searches.
NG . searchBatch ( [
{ url : 'url1' } ,
{ url : 'url2' } ,
{ url : 'url3' }
] , "pattern" )
. then ( ( outputs ) => {
outputs . forEach ( ( output ) => {
console . log ( `The pattern has matched for ${ output . url } ` , output . result )
} ) ;
} ) ;
// If you want to avoid waiting for the completion of
// all the searches, the method `searchBatchWithCallback` will
// execute a callback every time a search completes.
NG . searchBatchWithCallback ( [
{ url : 'url1' } ,
{ url : 'url2' } ,
{ url : 'url3' }
] , "pattern" , ( output ) => {
console . log ( `The pattern has matched for ${ output . url } ` , output . result )
} ) ; NetGrep تحت رخصة معهد ماساتشوستس للتكنولوجيا.