
NetGrep เป็นการพอร์ต การทดลอง ของ RIPGREP บน WASM โดยใช้โปรโตคอล HTTP ขอบเขตของโครงการนี้คือการให้ทางเลือกที่เป็นไปได้สำหรับเครื่องมือค้นหาที่ใช้ดัชนีสำหรับแอปพลิเคชันที่มีฐานข้อมูลที่ใช้ไฟล์ขนาดเล็ก มันใช้ส้อมของที่เก็บ ripgrep ดั้งเดิมที่มีการเปลี่ยนแปลงเพียงพอที่จะทำให้มันทำงานได้บน WASM
ในขณะที่ NetGrep กำลังจะบอกว่ามีรูปแบบอยู่ในไฟล์ระยะไกลที่ใช้ประโยชน์จากเครื่องมือค้นหา Core ripgrep หรือไม่ สิ่งนี้จะเกิดขึ้น ในขณะที่ไฟล์กำลังดาวน์โหลด เพื่อเพิ่มประสิทธิภาพสูงสุด
หมายเหตุ การค้นหาโพสต์ในบล็อกที่สร้างขึ้นผ่านตัวสร้างไซต์แบบคงที่เป็นกรณีการใช้งานที่น่าสนใจสำหรับการทดลองนี้ NetGrep สามารถใช้งานได้อย่างง่ายดายเพื่อสร้างเครื่องมือค้นหาแบบเรียลไทม์จากไฟล์ RAW Post ตัวอย่างสดสำหรับพฤติกรรมนี้สามารถพบได้ในบล็อกของฉัน (คุณสามารถดูซอร์สโค้ดได้)
คำเตือน ในขณะนี้ห้องสมุดนี้จะถูกส่งออกเป็น ESM เท่านั้นดังนั้นจึงจำเป็นต้องใช้ Bundler เช่น WebPack เพื่อใช้งาน
หมายเหตุ การสอนสั้น ๆ นี้สันนิษฐานว่า WebPack 5 เป็น Bundler ที่ใช้ในแอปพลิเคชันที่จะรวม
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 อยู่ภายใต้ใบอนุญาต MIT