
NetGrepは、HTTPプロトコルを使用したWASM上のRipgrepの実験的な移植です。このプロジェクトの範囲は、小さなファイルベースのデータベースを使用してアプリケーション用のインデックスベースの検索エンジンに実行可能な代替品を提供することです。 WASMで実行可能にするために、十分な変更を加えた元のripgrepリポジトリのフォークを使用します。
現時点では、NetGrepはripgrepコア検索エンジンを活用するリモートファイルにパターンが存在するかどうかを判断するだけです。これは、パフォーマンスを最大化するためにファイルがダウンロードされているときに発生します。
静的サイトジェネレーターを介して作成されたブログでの投稿を検索することは、この実験の興味深いユースケースです。 NetGrepを使用して、RAW POSTファイルからリアルタイム検索エンジンを作成できます。この動作のライブ例は私のブログにあります(ソースコードを見ることができます)。
警告このライブラリは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 ,
} ,
} ;その後、バンドルされたWASMファイルがバックグラウンドで非同期にロードされている間に、 netgrepを直接実行することが可能になります。
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ライセンスの下にあります。