@backstage/plugin-search-backend-node 1.3.14-next.0 → 1.3.15-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # @backstage/plugin-search-backend-node
2
2
 
3
+ ## 1.3.15-next.0
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies
8
+ - @backstage/backend-plugin-api@1.4.3-next.0
9
+
10
+ ## 1.3.14
11
+
12
+ ### Patch Changes
13
+
14
+ - Updated dependencies
15
+ - @backstage/backend-plugin-api@1.4.2
16
+
3
17
  ## 1.3.14-next.0
4
18
 
5
19
  ### Patch Changes
@@ -1 +1 @@
1
- {"version":3,"file":"IndexBuilder.cjs.js","sources":["../src/IndexBuilder.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n DocumentDecoratorFactory,\n DocumentTypeInfo,\n} from '@backstage/plugin-search-common';\nimport { pipeline, Transform } from 'stream';\nimport { Scheduler } from './Scheduler';\nimport {\n IndexBuilderOptions,\n RegisterCollatorParameters,\n RegisterDecoratorParameters,\n SearchEngine,\n} from './types';\nimport { LoggerService } from '@backstage/backend-plugin-api';\n\n/**\n * Used for adding collators, decorators and compile them into tasks which are added to a scheduler returned to the caller.\n * @public\n */\nexport class IndexBuilder {\n private collators: Record<string, RegisterCollatorParameters>;\n private decorators: Record<string, DocumentDecoratorFactory[]>;\n private documentTypes: Record<string, DocumentTypeInfo>;\n private searchEngine: SearchEngine;\n private logger: LoggerService;\n\n constructor(options: IndexBuilderOptions) {\n this.collators = {};\n this.decorators = {};\n this.documentTypes = {};\n this.logger = options.logger;\n this.searchEngine = options.searchEngine;\n }\n\n /**\n * Responsible for returning the registered search engine.\n */\n getSearchEngine(): SearchEngine {\n return this.searchEngine;\n }\n\n /**\n * Responsible for returning the registered document types.\n */\n getDocumentTypes(): Record<string, DocumentTypeInfo> {\n return this.documentTypes;\n }\n\n /**\n * Makes the index builder aware of a collator that should be executed at the\n * given refresh interval.\n */\n addCollator(options: RegisterCollatorParameters): void {\n const { factory, schedule } = options;\n\n this.logger.info(\n `Added ${factory.constructor.name} collator factory for type ${factory.type}`,\n );\n this.collators[factory.type] = {\n factory,\n schedule,\n };\n this.documentTypes[factory.type] = {\n visibilityPermission: factory.visibilityPermission,\n };\n }\n\n /**\n * Makes the index builder aware of a decorator. If no types are provided on\n * the decorator, it will be applied to documents from all known collators,\n * otherwise it will only be applied to documents of the given types.\n */\n addDecorator(options: RegisterDecoratorParameters): void {\n const { factory } = options;\n const types = factory.types || ['*'];\n this.logger.info(\n `Added decorator ${factory.constructor.name} to types ${types.join(\n ', ',\n )}`,\n );\n types.forEach(type => {\n if (this.decorators.hasOwnProperty(type)) {\n this.decorators[type].push(factory);\n } else {\n this.decorators[type] = [factory];\n }\n });\n }\n\n /**\n * Compiles collators and decorators into tasks, which are added to a\n * scheduler returned to the caller.\n */\n async build(): Promise<{ scheduler: Scheduler }> {\n const scheduler = new Scheduler({\n logger: this.logger,\n });\n\n Object.keys(this.collators).forEach(type => {\n const taskLogger = this.logger.child({ documentType: type });\n scheduler.addToSchedule({\n id: `search_index_${type.replace('-', '_').toLocaleLowerCase('en-US')}`,\n scheduledRunner: this.collators[type].schedule,\n task: async () => {\n // Instantiate the collator.\n const collator = await this.collators[type].factory.getCollator();\n taskLogger.info(\n `Collating documents for ${type} via ${this.collators[type].factory.constructor.name}`,\n );\n\n // Instantiate all relevant decorators.\n const decorators: Transform[] = await Promise.all(\n (this.decorators['*'] || [])\n .concat(this.decorators[type] || [])\n .map(async factory => {\n const decorator = await factory.getDecorator();\n taskLogger.info(\n `Attached decorator via ${factory.constructor.name} to ${type} index pipeline.`,\n );\n return decorator;\n }),\n );\n\n // Instantiate the indexer.\n const indexer = await this.searchEngine.getIndexer(type);\n\n // Compose collator/decorators/indexer into a pipeline\n return new Promise<void>((resolve, reject) => {\n pipeline(\n [collator, ...decorators, indexer],\n (error: NodeJS.ErrnoException | null) => {\n if (error) {\n taskLogger.error(\n `Collating documents for ${type} failed: ${error}`,\n );\n reject(error);\n } else {\n // Signal index pipeline completion!\n taskLogger.info(`Collating documents for ${type} succeeded`);\n resolve();\n }\n },\n );\n });\n },\n });\n });\n\n return {\n scheduler,\n };\n }\n}\n"],"names":["Scheduler","pipeline"],"mappings":";;;;;AAkCO,MAAM,YAAa,CAAA;AAAA,EAChB,SAAA;AAAA,EACA,UAAA;AAAA,EACA,aAAA;AAAA,EACA,YAAA;AAAA,EACA,MAAA;AAAA,EAER,YAAY,OAA8B,EAAA;AACxC,IAAA,IAAA,CAAK,YAAY,EAAC;AAClB,IAAA,IAAA,CAAK,aAAa,EAAC;AACnB,IAAA,IAAA,CAAK,gBAAgB,EAAC;AACtB,IAAA,IAAA,CAAK,SAAS,OAAQ,CAAA,MAAA;AACtB,IAAA,IAAA,CAAK,eAAe,OAAQ,CAAA,YAAA;AAAA;AAC9B;AAAA;AAAA;AAAA,EAKA,eAAgC,GAAA;AAC9B,IAAA,OAAO,IAAK,CAAA,YAAA;AAAA;AACd;AAAA;AAAA;AAAA,EAKA,gBAAqD,GAAA;AACnD,IAAA,OAAO,IAAK,CAAA,aAAA;AAAA;AACd;AAAA;AAAA;AAAA;AAAA,EAMA,YAAY,OAA2C,EAAA;AACrD,IAAM,MAAA,EAAE,OAAS,EAAA,QAAA,EAAa,GAAA,OAAA;AAE9B,IAAA,IAAA,CAAK,MAAO,CAAA,IAAA;AAAA,MACV,SAAS,OAAQ,CAAA,WAAA,CAAY,IAAI,CAAA,2BAA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ACjC,CAAC,KAAwC,KAAA;AACvC,gBAAA,IAAI,KAAO,EAAA;AACT,kBAAW,UAAA,CAAA,KAAA;AAAA,oBACT,CAAA,wBAAA,EAA2B,IAAI,CAAA,SAAA,EAAY,KAAK,CAAA;AAAA,mBAClD;AACA,kBAAA,MAAA,CAAO,KAAK,CAAA;AAAA,iBACP,MAAA;AAEL,kBAAW,UAAA,CAAA,IAAA,CAAK,CAA2B,wBAAA,EAAA,IAAI,CAAY,UAAA,CAAA,CAAA;AAC3D,kBAAQ,OAAA,EAAA;AAAA;AACV;AACF,aACF;AAAA,WACD,CAAA;AAAA;AACH,OACD,CAAA;AAAA,KACF,CAAA;AAED,IAAO,OAAA;AAAA,MACL;AAAA,KACF;AAAA;AAEJ;;;;"}
1
+ {"version":3,"file":"IndexBuilder.cjs.js","sources":["../src/IndexBuilder.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n DocumentDecoratorFactory,\n DocumentTypeInfo,\n} from '@backstage/plugin-search-common';\nimport { pipeline, Transform } from 'stream';\nimport { Scheduler } from './Scheduler';\nimport {\n IndexBuilderOptions,\n RegisterCollatorParameters,\n RegisterDecoratorParameters,\n SearchEngine,\n} from './types';\nimport { LoggerService } from '@backstage/backend-plugin-api';\n\n/**\n * Used for adding collators, decorators and compile them into tasks which are added to a scheduler returned to the caller.\n * @public\n */\nexport class IndexBuilder {\n private collators: Record<string, RegisterCollatorParameters>;\n private decorators: Record<string, DocumentDecoratorFactory[]>;\n private documentTypes: Record<string, DocumentTypeInfo>;\n private searchEngine: SearchEngine;\n private logger: LoggerService;\n\n constructor(options: IndexBuilderOptions) {\n this.collators = {};\n this.decorators = {};\n this.documentTypes = {};\n this.logger = options.logger;\n this.searchEngine = options.searchEngine;\n }\n\n /**\n * Responsible for returning the registered search engine.\n */\n getSearchEngine(): SearchEngine {\n return this.searchEngine;\n }\n\n /**\n * Responsible for returning the registered document types.\n */\n getDocumentTypes(): Record<string, DocumentTypeInfo> {\n return this.documentTypes;\n }\n\n /**\n * Makes the index builder aware of a collator that should be executed at the\n * given refresh interval.\n */\n addCollator(options: RegisterCollatorParameters): void {\n const { factory, schedule } = options;\n\n this.logger.info(\n `Added ${factory.constructor.name} collator factory for type ${factory.type}`,\n );\n this.collators[factory.type] = {\n factory,\n schedule,\n };\n this.documentTypes[factory.type] = {\n visibilityPermission: factory.visibilityPermission,\n };\n }\n\n /**\n * Makes the index builder aware of a decorator. If no types are provided on\n * the decorator, it will be applied to documents from all known collators,\n * otherwise it will only be applied to documents of the given types.\n */\n addDecorator(options: RegisterDecoratorParameters): void {\n const { factory } = options;\n const types = factory.types || ['*'];\n this.logger.info(\n `Added decorator ${factory.constructor.name} to types ${types.join(\n ', ',\n )}`,\n );\n types.forEach(type => {\n if (this.decorators.hasOwnProperty(type)) {\n this.decorators[type].push(factory);\n } else {\n this.decorators[type] = [factory];\n }\n });\n }\n\n /**\n * Compiles collators and decorators into tasks, which are added to a\n * scheduler returned to the caller.\n */\n async build(): Promise<{ scheduler: Scheduler }> {\n const scheduler = new Scheduler({\n logger: this.logger,\n });\n\n Object.keys(this.collators).forEach(type => {\n const taskLogger = this.logger.child({ documentType: type });\n scheduler.addToSchedule({\n id: `search_index_${type.replace('-', '_').toLocaleLowerCase('en-US')}`,\n scheduledRunner: this.collators[type].schedule,\n task: async () => {\n // Instantiate the collator.\n const collator = await this.collators[type].factory.getCollator();\n taskLogger.info(\n `Collating documents for ${type} via ${this.collators[type].factory.constructor.name}`,\n );\n\n // Instantiate all relevant decorators.\n const decorators: Transform[] = await Promise.all(\n (this.decorators['*'] || [])\n .concat(this.decorators[type] || [])\n .map(async factory => {\n const decorator = await factory.getDecorator();\n taskLogger.info(\n `Attached decorator via ${factory.constructor.name} to ${type} index pipeline.`,\n );\n return decorator;\n }),\n );\n\n // Instantiate the indexer.\n const indexer = await this.searchEngine.getIndexer(type);\n\n // Compose collator/decorators/indexer into a pipeline\n return new Promise<void>((resolve, reject) => {\n pipeline(\n [collator, ...decorators, indexer],\n (error: NodeJS.ErrnoException | null) => {\n if (error) {\n taskLogger.error(\n `Collating documents for ${type} failed: ${error}`,\n );\n reject(error);\n } else {\n // Signal index pipeline completion!\n taskLogger.info(`Collating documents for ${type} succeeded`);\n resolve();\n }\n },\n );\n });\n },\n });\n });\n\n return {\n scheduler,\n };\n }\n}\n"],"names":["Scheduler","pipeline"],"mappings":";;;;;AAkCO,MAAM,YAAA,CAAa;AAAA,EAChB,SAAA;AAAA,EACA,UAAA;AAAA,EACA,aAAA;AAAA,EACA,YAAA;AAAA,EACA,MAAA;AAAA,EAER,YAAY,OAAA,EAA8B;AACxC,IAAA,IAAA,CAAK,YAAY,EAAC;AAClB,IAAA,IAAA,CAAK,aAAa,EAAC;AACnB,IAAA,IAAA,CAAK,gBAAgB,EAAC;AACtB,IAAA,IAAA,CAAK,SAAS,OAAA,CAAQ,MAAA;AACtB,IAAA,IAAA,CAAK,eAAe,OAAA,CAAQ,YAAA;AAAA,EAC9B;AAAA;AAAA;AAAA;AAAA,EAKA,eAAA,GAAgC;AAC9B,IAAA,OAAO,IAAA,CAAK,YAAA;AAAA,EACd;AAAA;AAAA;AAAA;AAAA,EAKA,gBAAA,GAAqD;AACnD,IAAA,OAAO,IAAA,CAAK,aAAA;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,YAAY,OAAA,EAA2C;AACrD,IAAA,MAAM,EAAE,OAAA,EAAS,QAAA,EAAS,GAAI,OAAA;AAE9B,IAAA,IAAA,CAAK,MAAA,CAAO,IAAA;AAAA,MACV,SAAS,OAAA,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eAAA;AAAA,cACE,CAAC,QAAA,EAAU,GAAG,UAAA,EAAY,OAAO,CAAA;AAAA,cACjC,CAAC,KAAA,KAAwC;AACvC,gBAAA,IAAI,KAAA,EAAO;AACT,kBAAA,UAAA,CAAW,KAAA;AAAA,oBACT,CAAA,wBAAA,EAA2B,IAAI,CAAA,SAAA,EAAY,KAAK,CAAA;AAAA,mBAClD;AACA,kBAAA,MAAA,CAAO,KAAK,CAAA;AAAA,gBACd,CAAA,MAAO;AAEL,kBAAA,UAAA,CAAW,IAAA,CAAK,CAAA,wBAAA,EAA2B,IAAI,CAAA,UAAA,CAAY,CAAA;AAC3D,kBAAA,OAAA,EAAQ;AAAA,gBACV;AAAA,cACF;AAAA,aACF;AAAA,UACF,CAAC,CAAA;AAAA,QACH;AAAA,OACD,CAAA;AAAA,IACH,CAAC,CAAA;AAED,IAAA,OAAO;AAAA,MACL;AAAA,KACF;AAAA,EACF;AACF;;;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"Scheduler.cjs.js","sources":["../src/Scheduler.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n LoggerService,\n SchedulerServiceTaskRunner,\n SchedulerServiceTaskFunction,\n} from '@backstage/backend-plugin-api';\n\ntype TaskEnvelope = {\n task: SchedulerServiceTaskFunction;\n scheduledRunner: SchedulerServiceTaskRunner;\n};\n\n/**\n * ScheduleTaskParameters\n * @public\n */\nexport type ScheduleTaskParameters = {\n id: string;\n task: SchedulerServiceTaskFunction;\n scheduledRunner: SchedulerServiceTaskRunner;\n};\n\n/**\n * Scheduler responsible for all search tasks.\n * @public\n */\nexport class Scheduler {\n private logger: LoggerService;\n private schedule: { [id: string]: TaskEnvelope };\n private abortControllers: AbortController[];\n private isRunning: boolean;\n\n constructor(options: { logger: LoggerService }) {\n this.logger = options.logger;\n this.schedule = {};\n this.abortControllers = [];\n this.isRunning = false;\n }\n\n /**\n * Adds each task and interval to the schedule.\n * When running the tasks, the scheduler waits at least for the time specified\n * in the interval once the task was completed, before running it again.\n */\n addToSchedule(options: ScheduleTaskParameters) {\n const { id, task, scheduledRunner } = options;\n\n if (this.isRunning) {\n throw new Error(\n 'Cannot add task to schedule that has already been started.',\n );\n }\n\n if (this.schedule[id]) {\n throw new Error(`Task with id ${id} already exists.`);\n }\n\n this.schedule[id] = { task, scheduledRunner };\n }\n\n /**\n * Starts the scheduling process for each task\n */\n start() {\n this.logger.info('Starting all scheduled search tasks.');\n this.isRunning = true;\n Object.keys(this.schedule).forEach(id => {\n const abortController = new AbortController();\n this.abortControllers.push(abortController);\n const { task, scheduledRunner } = this.schedule[id];\n scheduledRunner.run({\n id,\n fn: task,\n signal: abortController.signal,\n });\n });\n }\n\n /**\n * Stop all scheduled tasks.\n */\n stop() {\n this.logger.info('Stopping all scheduled search tasks.');\n for (const abortController of this.abortControllers) {\n abortController.abort();\n }\n this.abortControllers = [];\n this.isRunning = false;\n }\n}\n"],"names":[],"mappings":";;AAyCO,MAAM,SAAU,CAAA;AAAA,EACb,MAAA;AAAA,EACA,QAAA;AAAA,EACA,gBAAA;AAAA,EACA,SAAA;AAAA,EAER,YAAY,OAAoC,EAAA;AAC9C,IAAA,IAAA,CAAK,SAAS,OAAQ,CAAA,MAAA;AACtB,IAAA,IAAA,CAAK,WAAW,EAAC;AACjB,IAAA,IAAA,CAAK,mBAAmB,EAAC;AACzB,IAAA,IAAA,CAAK,SAAY,GAAA,KAAA;AAAA;AACnB;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,cAAc,OAAiC,EAAA;AAC7C,IAAA,MAAM,EAAE,E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AAY,GAAA,KAAA;AAAA;AAErB;;;;"}
1
+ {"version":3,"file":"Scheduler.cjs.js","sources":["../src/Scheduler.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n LoggerService,\n SchedulerServiceTaskRunner,\n SchedulerServiceTaskFunction,\n} from '@backstage/backend-plugin-api';\n\ntype TaskEnvelope = {\n task: SchedulerServiceTaskFunction;\n scheduledRunner: SchedulerServiceTaskRunner;\n};\n\n/**\n * ScheduleTaskParameters\n * @public\n */\nexport type ScheduleTaskParameters = {\n id: string;\n task: SchedulerServiceTaskFunction;\n scheduledRunner: SchedulerServiceTaskRunner;\n};\n\n/**\n * Scheduler responsible for all search tasks.\n * @public\n */\nexport class Scheduler {\n private logger: LoggerService;\n private schedule: { [id: string]: TaskEnvelope };\n private abortControllers: AbortController[];\n private isRunning: boolean;\n\n constructor(options: { logger: LoggerService }) {\n this.logger = options.logger;\n this.schedule = {};\n this.abortControllers = [];\n this.isRunning = false;\n }\n\n /**\n * Adds each task and interval to the schedule.\n * When running the tasks, the scheduler waits at least for the time specified\n * in the interval once the task was completed, before running it again.\n */\n addToSchedule(options: ScheduleTaskParameters) {\n const { id, task, scheduledRunner } = options;\n\n if (this.isRunning) {\n throw new Error(\n 'Cannot add task to schedule that has already been started.',\n );\n }\n\n if (this.schedule[id]) {\n throw new Error(`Task with id ${id} already exists.`);\n }\n\n this.schedule[id] = { task, scheduledRunner };\n }\n\n /**\n * Starts the scheduling process for each task\n */\n start() {\n this.logger.info('Starting all scheduled search tasks.');\n this.isRunning = true;\n Object.keys(this.schedule).forEach(id => {\n const abortController = new AbortController();\n this.abortControllers.push(abortController);\n const { task, scheduledRunner } = this.schedule[id];\n scheduledRunner.run({\n id,\n fn: task,\n signal: abortController.signal,\n });\n });\n }\n\n /**\n * Stop all scheduled tasks.\n */\n stop() {\n this.logger.info('Stopping all scheduled search tasks.');\n for (const abortController of this.abortControllers) {\n abortController.abort();\n }\n this.abortControllers = [];\n this.isRunning = false;\n }\n}\n"],"names":[],"mappings":";;AAyCO,MAAM,SAAA,CAAU;AAAA,EACb,MAAA;AAAA,EACA,QAAA;AAAA,EACA,gBAAA;AAAA,EACA,SAAA;AAAA,EAER,YAAY,OAAA,EAAoC;AAC9C,IAAA,IAAA,CAAK,SAAS,OAAA,CAAQ,MAAA;AACtB,IAAA,IAAA,CAAK,WAAW,EAAC;AACjB,IAAA,IAAA,CAAK,mBAAmB,EAAC;AACzB,IAAA,IAAA,CAAK,SAAA,GAAY,KAAA;AAAA,EACnB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,cAAc,OAAA,EAAiC;AAC7C,IAAA,MAAM,EAAE,EAAA,EAAI,IAAA,EAAM,eAAA,EAAgB,GAAI,OAAA;AAEtC,IAAA,IAAI,KAAK,SAAA,EAAW;AAClB,MAAA,MAAM,IAAI,KAAA;AAAA,QACR;AAAA,OACF;AAAA,IACF;AAEA,IAAA,IAAI,IAAA,CAAK,QAAA,CAAS,EAAE,CAAA,EAAG;AACrB,MAAA,MAAM,IAAI,KAAA,CAAM,CAAA,aAAA,EAAgB,EAAE,CAAA,gBAAA,CAAkB,CAAA;AAAA,IACtD;AAEA,IAAA,IAAA,CAAK,QAAA,CAAS,EAAE,CAAA,GAAI,EAAE,MAAM,eAAA,EAAgB;AAAA,EAC9C;AAAA;AAAA;AAAA;AAAA,EAKA,KAAA,GAAQ;AACN,IAAA,IAAA,CAAK,MAAA,CAAO,KAAK,sCAAsC,CAAA;AACvD,IAAA,IAAA,CAAK,SAAA,GAAY,IAAA;AACjB,IAAA,MAAA,CAAO,IAAA,CAAK,IAAA,CAAK,QAAQ,CAAA,CAAE,QAAQ,CAAA,EAAA,KAAM;AACvC,MAAA,MAAM,eAAA,GAAkB,IAAI,eAAA,EAAgB;AAC5C,MAAA,IAAA,CAAK,gBAAA,CAAiB,KAAK,eAAe,CAAA;AAC1C,MAAA,MAAM,EAAE,IAAA,EAAM,eAAA,EAAgB,GAAI,IAAA,CAAK,SAAS,EAAE,CAAA;AAClD,MAAA,eAAA,CAAgB,GAAA,CAAI;AAAA,QAClB,EAAA;AAAA,QACA,EAAA,EAAI,IAAA;AAAA,QACJ,QAAQ,eAAA,CAAgB;AAAA,OACzB,CAAA;AAAA,IACH,CAAC,CAAA;AAAA,EACH;AAAA;AAAA;AAAA;AAAA,EAKA,IAAA,GAAO;AACL,IAAA,IAAA,CAAK,MAAA,CAAO,KAAK,sCAAsC,CAAA;AACvD,IAAA,KAAA,MAAW,eAAA,IAAmB,KAAK,gBAAA,EAAkB;AACnD,MAAA,eAAA,CAAgB,KAAA,EAAM;AAAA,IACxB;AACA,IAAA,IAAA,CAAK,mBAAmB,EAAC;AACzB,IAAA,IAAA,CAAK,SAAA,GAAY,KAAA;AAAA,EACnB;AACF;;;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"alpha.cjs.js","sources":["../src/alpha.ts"],"sourcesContent":["/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n coreServices,\n createExtensionPoint,\n createServiceFactory,\n createServiceRef,\n LoggerService,\n} from '@backstage/backend-plugin-api';\nimport { DocumentTypeInfo } from '@backstage/plugin-search-common';\nimport {\n IndexBuilder,\n RegisterCollatorParameters,\n RegisterDecoratorParameters,\n Scheduler,\n SearchEngine,\n} from '@backstage/plugin-search-backend-node';\n\n/**\n * @alpha\n * Options for the init method on {@link SearchIndexService}.\n */\nexport type SearchIndexServiceInitOptions = {\n searchEngine: SearchEngine;\n collators: RegisterCollatorParameters[];\n decorators: RegisterDecoratorParameters[];\n};\n\n/**\n * @alpha\n * Interface for implementation of index service.\n */\nexport interface SearchIndexService {\n /**\n * Initializes state in preparation for starting the search index service\n */\n init(options: SearchIndexServiceInitOptions): void;\n\n /**\n * Starts indexing process\n */\n start(): Promise<void>;\n\n /**\n * Stops indexing process\n */\n stop(): Promise<void>;\n\n /**\n * Returns an index types list.\n */\n getDocumentTypes(): Record<string, DocumentTypeInfo>;\n}\n\n/**\n * @alpha\n * Interface for search index registry extension point.\n */\nexport interface SearchIndexRegistryExtensionPoint {\n addCollator(options: RegisterCollatorParameters): void;\n addDecorator(options: RegisterDecoratorParameters): void;\n}\n\n/**\n * @alpha\n * Interface for search engine registry extension point.\n */\nexport interface SearchEngineRegistryExtensionPoint {\n setSearchEngine(searchEngine: SearchEngine): void;\n}\n\ntype DefaultSearchIndexServiceOptions = {\n logger: LoggerService;\n};\n\n/**\n * @alpha\n * Responsible for register the indexing task and start the schedule.\n */\nclass DefaultSearchIndexService implements SearchIndexService {\n private readonly logger: LoggerService;\n private indexBuilder: IndexBuilder | null = null;\n private scheduler: Scheduler | null = null;\n\n private constructor(options: DefaultSearchIndexServiceOptions) {\n this.logger = options.logger;\n }\n\n static fromConfig(options: DefaultSearchIndexServiceOptions) {\n return new DefaultSearchIndexService(options);\n }\n\n init(options: SearchIndexServiceInitOptions): void {\n this.indexBuilder = new IndexBuilder({\n logger: this.logger,\n searchEngine: options.searchEngine,\n });\n\n options.collators.forEach(collator =>\n this.indexBuilder?.addCollator(collator),\n );\n\n options.decorators.forEach(decorator =>\n this.indexBuilder?.addDecorator(decorator),\n );\n }\n\n async start(): Promise<void> {\n if (!this.indexBuilder) {\n throw new Error('IndexBuilder is not initialized, call init first');\n }\n const { scheduler } = await this.indexBuilder.build();\n this.scheduler = scheduler;\n this.scheduler!.start();\n }\n\n async stop(): Promise<void> {\n if (this.scheduler) {\n this.scheduler.stop();\n this.scheduler = null;\n }\n }\n\n getDocumentTypes(): Record<string, DocumentTypeInfo> {\n return this.indexBuilder?.getDocumentTypes() ?? {};\n }\n}\n\n/**\n * @alpha\n * Service that builds a search index.\n */\nexport const searchIndexServiceRef = createServiceRef<SearchIndexService>({\n id: 'search.index.service',\n defaultFactory: async service =>\n createServiceFactory({\n service,\n deps: {\n logger: coreServices.logger,\n },\n factory({ logger }) {\n return DefaultSearchIndexService.fromConfig({\n logger,\n });\n },\n }),\n});\n\n/**\n * @alpha\n * Extension point for register a search engine.\n */\nexport const searchEngineRegistryExtensionPoint =\n createExtensionPoint<SearchEngineRegistryExtensionPoint>({\n id: 'search.engine.registry',\n });\n\n/**\n * @alpha\n * Extension point for registering collators and decorators\n */\nexport const searchIndexRegistryExtensionPoint =\n createExtensionPoint<SearchIndexRegistryExtensionPoint>({\n id: 'search.index.registry',\n });\n"],"names":["IndexBuilder","createServiceRef","createServiceFactory","coreServices","createExtensionPoint"],"mappings":";;;;;AA6FA,MAAM,yBAAwD,CAAA;AAAA,EAC3C,MAAA;AAAA,EACT,YAAoC,GAAA,IAAA;AAAA,EACpC,SAA8B,GAAA,IAAA;AAAA,EAE9B,YAAY,OAA2C,EAAA;AAC7D,IAAA,IAAA,CAAK,SAAS,OAAQ,CAAA,MAAA;AAAA;AACxB,EAEA,OAAO,WAAW,OAA2C,EAAA;AAC3D,IAAO,OAAA,IAAI,0BAA0B,OAAO,CAAA;AAAA;AAC9C,EAEA,KAAK,OAA8C,EAAA;AACjD,IAAK,IAAA,CAAA,YAAA,GAAe,IAAIA,oCAAa,CAAA;AAAA,MACnC,QAAQ,IAAK,CAAA,MAAA;AAAA,MACb,cAAc,OAAQ,CAAA;AAAA,KACvB,CAAA;AAED,I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AA;AACN,CAAC;AAMI,MAAM,oCACXA,qCAAwD,CAAA;AAAA,EACtD,EAAI,EAAA;AACN,CAAC;;;;;;"}
1
+ {"version":3,"file":"alpha.cjs.js","sources":["../src/alpha.ts"],"sourcesContent":["/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n coreServices,\n createExtensionPoint,\n createServiceFactory,\n createServiceRef,\n LoggerService,\n} from '@backstage/backend-plugin-api';\nimport { DocumentTypeInfo } from '@backstage/plugin-search-common';\nimport {\n IndexBuilder,\n RegisterCollatorParameters,\n RegisterDecoratorParameters,\n Scheduler,\n SearchEngine,\n} from '@backstage/plugin-search-backend-node';\n\n/**\n * @alpha\n * Options for the init method on {@link SearchIndexService}.\n */\nexport type SearchIndexServiceInitOptions = {\n searchEngine: SearchEngine;\n collators: RegisterCollatorParameters[];\n decorators: RegisterDecoratorParameters[];\n};\n\n/**\n * @alpha\n * Interface for implementation of index service.\n */\nexport interface SearchIndexService {\n /**\n * Initializes state in preparation for starting the search index service\n */\n init(options: SearchIndexServiceInitOptions): void;\n\n /**\n * Starts indexing process\n */\n start(): Promise<void>;\n\n /**\n * Stops indexing process\n */\n stop(): Promise<void>;\n\n /**\n * Returns an index types list.\n */\n getDocumentTypes(): Record<string, DocumentTypeInfo>;\n}\n\n/**\n * @alpha\n * Interface for search index registry extension point.\n */\nexport interface SearchIndexRegistryExtensionPoint {\n addCollator(options: RegisterCollatorParameters): void;\n addDecorator(options: RegisterDecoratorParameters): void;\n}\n\n/**\n * @alpha\n * Interface for search engine registry extension point.\n */\nexport interface SearchEngineRegistryExtensionPoint {\n setSearchEngine(searchEngine: SearchEngine): void;\n}\n\ntype DefaultSearchIndexServiceOptions = {\n logger: LoggerService;\n};\n\n/**\n * @alpha\n * Responsible for register the indexing task and start the schedule.\n */\nclass DefaultSearchIndexService implements SearchIndexService {\n private readonly logger: LoggerService;\n private indexBuilder: IndexBuilder | null = null;\n private scheduler: Scheduler | null = null;\n\n private constructor(options: DefaultSearchIndexServiceOptions) {\n this.logger = options.logger;\n }\n\n static fromConfig(options: DefaultSearchIndexServiceOptions) {\n return new DefaultSearchIndexService(options);\n }\n\n init(options: SearchIndexServiceInitOptions): void {\n this.indexBuilder = new IndexBuilder({\n logger: this.logger,\n searchEngine: options.searchEngine,\n });\n\n options.collators.forEach(collator =>\n this.indexBuilder?.addCollator(collator),\n );\n\n options.decorators.forEach(decorator =>\n this.indexBuilder?.addDecorator(decorator),\n );\n }\n\n async start(): Promise<void> {\n if (!this.indexBuilder) {\n throw new Error('IndexBuilder is not initialized, call init first');\n }\n const { scheduler } = await this.indexBuilder.build();\n this.scheduler = scheduler;\n this.scheduler!.start();\n }\n\n async stop(): Promise<void> {\n if (this.scheduler) {\n this.scheduler.stop();\n this.scheduler = null;\n }\n }\n\n getDocumentTypes(): Record<string, DocumentTypeInfo> {\n return this.indexBuilder?.getDocumentTypes() ?? {};\n }\n}\n\n/**\n * @alpha\n * Service that builds a search index.\n */\nexport const searchIndexServiceRef = createServiceRef<SearchIndexService>({\n id: 'search.index.service',\n defaultFactory: async service =>\n createServiceFactory({\n service,\n deps: {\n logger: coreServices.logger,\n },\n factory({ logger }) {\n return DefaultSearchIndexService.fromConfig({\n logger,\n });\n },\n }),\n});\n\n/**\n * @alpha\n * Extension point for register a search engine.\n */\nexport const searchEngineRegistryExtensionPoint =\n createExtensionPoint<SearchEngineRegistryExtensionPoint>({\n id: 'search.engine.registry',\n });\n\n/**\n * @alpha\n * Extension point for registering collators and decorators\n */\nexport const searchIndexRegistryExtensionPoint =\n createExtensionPoint<SearchIndexRegistryExtensionPoint>({\n id: 'search.index.registry',\n });\n"],"names":["IndexBuilder","createServiceRef","createServiceFactory","coreServices","createExtensionPoint"],"mappings":";;;;;AA6FA,MAAM,yBAAA,CAAwD;AAAA,EAC3C,MAAA;AAAA,EACT,YAAA,GAAoC,IAAA;AAAA,EACpC,SAAA,GAA8B,IAAA;AAAA,EAE9B,YAAY,OAAA,EAA2C;AAC7D,IAAA,IAAA,CAAK,SAAS,OAAA,CAAQ,MAAA;AAAA,EACxB;AAAA,EAEA,OAAO,WAAW,OAAA,EAA2C;AAC3D,IAAA,OAAO,IAAI,0BAA0B,OAAO,CAAA;AAAA,EAC9C;AAAA,EAEA,KAAK,OAAA,EAA8C;AACjD,IAAA,IAAA,CAAK,YAAA,GAAe,IAAIA,oCAAA,CAAa;AAAA,MACnC,QAAQ,IAAA,CAAK,MAAA;AAAA,MACb,cAAc,OAAA,CAAQ;AAAA,KACvB,CAAA;AAED,IAAA,OAAA,CAAQ,SAAA,CAAU,OAAA;AAAA,MAAQ,CAAA,QAAA,KACxB,IAAA,CAAK,YAAA,EAAc,WAAA,CAAY,QAAQ;AAAA,KACzC;AAEA,IAAA,OAAA,CAAQ,UAAA,CAAW,OAAA;AAAA,MAAQ,CAAA,SAAA,KACzB,IAAA,CAAK,YAAA,EAAc,YAAA,CAAa,SAAS;AAAA,KAC3C;AAAA,EACF;AAAA,EAEA,MAAM,KAAA,GAAuB;AAC3B,IAAA,IAAI,CAAC,KAAK,YAAA,EAAc;AACtB,MAAA,MAAM,IAAI,MAAM,kDAAkD,CAAA;AAAA,IACpE;AACA,IAAA,MAAM,EAAE,SAAA,EAAU,GAAI,MAAM,IAAA,CAAK,aAAa,KAAA,EAAM;AACpD,IAAA,IAAA,CAAK,SAAA,GAAY,SAAA;AACjB,IAAA,IAAA,CAAK,UAAW,KAAA,EAAM;AAAA,EACxB;AAAA,EAEA,MAAM,IAAA,GAAsB;AAC1B,IAAA,IAAI,KAAK,SAAA,EAAW;AAClB,MAAA,IAAA,CAAK,UAAU,IAAA,EAAK;AACpB,MAAA,IAAA,CAAK,SAAA,GAAY,IAAA;AAAA,IACnB;AAAA,EACF;AAAA,EAEA,gBAAA,GAAqD;AACnD,IAAA,OAAO,IAAA,CAAK,YAAA,EAAc,gBAAA,EAAiB,IAAK,EAAC;AAAA,EACnD;AACF;AAMO,MAAM,wBAAwBC,iCAAA,CAAqC;AAAA,EACxE,EAAA,EAAI,sBAAA;AAAA,EACJ,cAAA,EAAgB,OAAM,OAAA,KACpBC,qCAAA,CAAqB;AAAA,IACnB,OAAA;AAAA,IACA,IAAA,EAAM;AAAA,MACJ,QAAQC,6BAAA,CAAa;AAAA,KACvB;AAAA,IACA,OAAA,CAAQ,EAAE,MAAA,EAAO,EAAG;AAClB,MAAA,OAAO,0BAA0B,UAAA,CAAW;AAAA,QAC1C;AAAA,OACD,CAAA;AAAA,IACH;AAAA,GACD;AACL,CAAC;AAMM,MAAM,qCACXC,qCAAA,CAAyD;AAAA,EACvD,EAAA,EAAI;AACN,CAAC;AAMI,MAAM,oCACXA,qCAAA,CAAwD;AAAA,EACtD,EAAA,EAAI;AACN,CAAC;;;;;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"NewlineDelimitedJsonCollatorFactory.cjs.js","sources":["../../src/collators/NewlineDelimitedJsonCollatorFactory.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Config } from '@backstage/config';\nimport { DocumentCollatorFactory } from '@backstage/plugin-search-common';\nimport { Permission } from '@backstage/plugin-permission-common';\nimport { Readable } from 'stream';\nimport { parse as parseNdjson } from 'ndjson';\nimport { LoggerService, UrlReaderService } from '@backstage/backend-plugin-api';\n\n/**\n * Options for instantiate NewlineDelimitedJsonCollatorFactory\n * @public\n */\nexport type NewlineDelimitedJsonCollatorFactoryOptions = {\n type: string;\n searchPattern: string;\n reader: UrlReaderService;\n logger: LoggerService;\n visibilityPermission?: Permission;\n};\n\n/**\n * Factory class producing a collator that can be used to index documents\n * sourced from the latest newline delimited JSON file matching a given search\n * pattern. \"Latest\" is determined by the name of the file (last alphabetically\n * is considered latest).\n *\n * @remarks\n * The reader provided must implement the `search()` method as well as the\n * `readUrl` method whose response includes the `stream()` method. Naturally,\n * the reader must also be configured to understand the given search pattern.\n *\n * @example\n * Here's an example configuration using Google Cloud Storage, which would\n * return the latest file under the `bucket` GCS bucket with files like\n * `xyz-2021.ndjson` or `xyz-2022.ndjson`.\n * ```ts\n * indexBuilder.addCollator({\n * schedule,\n * factory: NewlineDelimitedJsonCollatorFactory.fromConfig(env.config, {\n * type: 'techdocs',\n * searchPattern: 'https://storage.cloud.google.com/bucket/xyz-*',\n * reader: env.reader,\n * logger: env.logger,\n * })\n * });\n * ```\n *\n * @public\n */\nexport class NewlineDelimitedJsonCollatorFactory\n implements DocumentCollatorFactory\n{\n readonly type: string;\n\n public readonly visibilityPermission: Permission | undefined;\n\n private constructor(\n type: string,\n private readonly searchPattern: string,\n private readonly reader: UrlReaderService,\n private readonly logger: LoggerService,\n visibilityPermission: Permission | undefined,\n ) {\n this.type = type;\n this.visibilityPermission = visibilityPermission;\n }\n\n /**\n * Returns a NewlineDelimitedJsonCollatorFactory instance from configuration\n * and a set of options.\n */\n static fromConfig(\n _config: Config,\n options: NewlineDelimitedJsonCollatorFactoryOptions,\n ): NewlineDelimitedJsonCollatorFactory {\n return new NewlineDelimitedJsonCollatorFactory(\n options.type,\n options.searchPattern,\n options.reader,\n options.logger.child({ documentType: options.type }),\n options.visibilityPermission,\n );\n }\n\n /**\n * Returns the \"latest\" URL for the given search pattern (e.g. the one at the\n * end of the list, sorted alphabetically).\n */\n private async lastUrl(): Promise<string | undefined> {\n try {\n // Search for files matching the given pattern, then sort/reverse. The\n // first item in the list will be the \"latest\" file.\n this.logger.info(\n `Attempting to find latest .ndjson matching ${this.searchPattern}`,\n );\n const { files } = await this.reader.search(this.searchPattern);\n const candidates = files\n .filter(file => file.url.endsWith('.ndjson'))\n .sort((a, b) => a.url.localeCompare(b.url))\n .reverse();\n\n return candidates[0]?.url;\n } catch (e) {\n this.logger.error(`Could not search for ${this.searchPattern}`, e);\n throw e;\n }\n }\n\n async getCollator(): Promise<Readable> {\n // Search for files matching the given pattern.\n const lastUrl = await this.lastUrl();\n\n // Abort if no such file could be found.\n if (!lastUrl) {\n const noMatchingFile = `Could not find an .ndjson file matching ${this.searchPattern}`;\n this.logger.error(noMatchingFile);\n throw new Error(noMatchingFile);\n } else {\n this.logger.info(`Using latest .ndjson file ${lastUrl}`);\n }\n\n // Use the UrlReader to try and stream the file.\n const readerResponse = await this.reader.readUrl!(lastUrl);\n const stream = readerResponse.stream!();\n\n // Use ndjson's parser to turn the raw file into an object-mode stream.\n return stream.pipe(parseNdjson());\n }\n}\n"],"names":["parseNdjson"],"mappings":";;;;AAgEO,MAAM,mCAEb,CAAA;AAAA,EAKU,WACN,CAAA,IAAA,EACiB,aACA,EAAA,MAAA,EACA,QACjB,oBACA,EAAA;AAJiB,IAAA,IAAA,CAAA,aAAA,GAAA,aAAA;AACA,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA;AACA,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA;AAGjB,IAAA,IAAA,CAAK,IAAO,GAAA,IAAA;AACZ,IAAA,IAAA,CAAK,oBAAuB,GAAA,oBAAA;AAAA;AAC9B,EAbS,IAAA;AAAA,EAEO,oBAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAiBhB,OAAO,UACL,CAAA,OAAA,EACA,OACqC,EAAA;AACrC,IAAA,OAAO,IAAI,mCAAA;AAAA,MACT,OAAQ,CAAA,IAAA;AAA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,CAAK,MAAO,CAAA,IAAA,CAAK,CAA6B,0BAAA,EAAA,OAAO,CAAE,CAAA,CAAA;AAAA;AAIzD,IAAA,MAAM,cAAiB,GAAA,MAAM,IAAK,CAAA,MAAA,CAAO,QAAS,OAAO,CAAA;AACzD,IAAM,MAAA,MAAA,GAAS,eAAe,MAAQ,EAAA;AAGtC,IAAO,OAAA,MAAA,CAAO,IAAK,CAAAA,YAAA,EAAa,CAAA;AAAA;AAEpC;;;;"}
1
+ {"version":3,"file":"NewlineDelimitedJsonCollatorFactory.cjs.js","sources":["../../src/collators/NewlineDelimitedJsonCollatorFactory.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Config } from '@backstage/config';\nimport { DocumentCollatorFactory } from '@backstage/plugin-search-common';\nimport { Permission } from '@backstage/plugin-permission-common';\nimport { Readable } from 'stream';\nimport { parse as parseNdjson } from 'ndjson';\nimport { LoggerService, UrlReaderService } from '@backstage/backend-plugin-api';\n\n/**\n * Options for instantiate NewlineDelimitedJsonCollatorFactory\n * @public\n */\nexport type NewlineDelimitedJsonCollatorFactoryOptions = {\n type: string;\n searchPattern: string;\n reader: UrlReaderService;\n logger: LoggerService;\n visibilityPermission?: Permission;\n};\n\n/**\n * Factory class producing a collator that can be used to index documents\n * sourced from the latest newline delimited JSON file matching a given search\n * pattern. \"Latest\" is determined by the name of the file (last alphabetically\n * is considered latest).\n *\n * @remarks\n * The reader provided must implement the `search()` method as well as the\n * `readUrl` method whose response includes the `stream()` method. Naturally,\n * the reader must also be configured to understand the given search pattern.\n *\n * @example\n * Here's an example configuration using Google Cloud Storage, which would\n * return the latest file under the `bucket` GCS bucket with files like\n * `xyz-2021.ndjson` or `xyz-2022.ndjson`.\n * ```ts\n * indexBuilder.addCollator({\n * schedule,\n * factory: NewlineDelimitedJsonCollatorFactory.fromConfig(env.config, {\n * type: 'techdocs',\n * searchPattern: 'https://storage.cloud.google.com/bucket/xyz-*',\n * reader: env.reader,\n * logger: env.logger,\n * })\n * });\n * ```\n *\n * @public\n */\nexport class NewlineDelimitedJsonCollatorFactory\n implements DocumentCollatorFactory\n{\n readonly type: string;\n\n public readonly visibilityPermission: Permission | undefined;\n\n private constructor(\n type: string,\n private readonly searchPattern: string,\n private readonly reader: UrlReaderService,\n private readonly logger: LoggerService,\n visibilityPermission: Permission | undefined,\n ) {\n this.type = type;\n this.visibilityPermission = visibilityPermission;\n }\n\n /**\n * Returns a NewlineDelimitedJsonCollatorFactory instance from configuration\n * and a set of options.\n */\n static fromConfig(\n _config: Config,\n options: NewlineDelimitedJsonCollatorFactoryOptions,\n ): NewlineDelimitedJsonCollatorFactory {\n return new NewlineDelimitedJsonCollatorFactory(\n options.type,\n options.searchPattern,\n options.reader,\n options.logger.child({ documentType: options.type }),\n options.visibilityPermission,\n );\n }\n\n /**\n * Returns the \"latest\" URL for the given search pattern (e.g. the one at the\n * end of the list, sorted alphabetically).\n */\n private async lastUrl(): Promise<string | undefined> {\n try {\n // Search for files matching the given pattern, then sort/reverse. The\n // first item in the list will be the \"latest\" file.\n this.logger.info(\n `Attempting to find latest .ndjson matching ${this.searchPattern}`,\n );\n const { files } = await this.reader.search(this.searchPattern);\n const candidates = files\n .filter(file => file.url.endsWith('.ndjson'))\n .sort((a, b) => a.url.localeCompare(b.url))\n .reverse();\n\n return candidates[0]?.url;\n } catch (e) {\n this.logger.error(`Could not search for ${this.searchPattern}`, e);\n throw e;\n }\n }\n\n async getCollator(): Promise<Readable> {\n // Search for files matching the given pattern.\n const lastUrl = await this.lastUrl();\n\n // Abort if no such file could be found.\n if (!lastUrl) {\n const noMatchingFile = `Could not find an .ndjson file matching ${this.searchPattern}`;\n this.logger.error(noMatchingFile);\n throw new Error(noMatchingFile);\n } else {\n this.logger.info(`Using latest .ndjson file ${lastUrl}`);\n }\n\n // Use the UrlReader to try and stream the file.\n const readerResponse = await this.reader.readUrl!(lastUrl);\n const stream = readerResponse.stream!();\n\n // Use ndjson's parser to turn the raw file into an object-mode stream.\n return stream.pipe(parseNdjson());\n }\n}\n"],"names":["parseNdjson"],"mappings":";;;;AAgEO,MAAM,mCAAA,CAEb;AAAA,EAKU,WAAA,CACN,IAAA,EACiB,aAAA,EACA,MAAA,EACA,QACjB,oBAAA,EACA;AAJiB,IAAA,IAAA,CAAA,aAAA,GAAA,aAAA;AACA,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA;AACA,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA;AAGjB,IAAA,IAAA,CAAK,IAAA,GAAO,IAAA;AACZ,IAAA,IAAA,CAAK,oBAAA,GAAuB,oBAAA;AAAA,EAC9B;AAAA,EAbS,IAAA;AAAA,EAEO,oBAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAiBhB,OAAO,UAAA,CACL,OAAA,EACA,OAAA,EACqC;AACrC,IAAA,OAAO,IAAI,mCAAA;AAAA,MACT,OAAA,CAAQ,IAAA;AAAA,MACR,OAAA,CAAQ,aAAA;AAAA,MACR,OAAA,CAAQ,MAAA;AAAA,MACR,QAAQ,MAAA,CAAO,KAAA,CAAM,EAAE,YAAA,EAAc,OAAA,CAAQ,MAAM,CAAA;AAAA,MACnD,OAAA,CAAQ;AAAA,KACV;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAc,OAAA,GAAuC;AACnD,IAAA,IAAI;AAGF,MAAA,IAAA,CAAK,MAAA,CAAO,IAAA;AAAA,QACV,CAAA,2CAAA,EAA8C,KAAK,aAAa,CAAA;AAAA,OAClE;AACA,MAAA,MAAM,EAAE,OAAM,GAAI,MAAM,KAAK,MAA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ourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n IndexableDocument,\n IndexableResultSet,\n SearchQuery,\n} from '@backstage/plugin-search-common';\nimport { QueryTranslator, SearchEngine } from '../types';\nimport { MissingIndexError } from '../errors';\nimport lunr from 'lunr';\nimport { v4 as uuid } from 'uuid';\nimport { LunrSearchEngineIndexer } from './LunrSearchEngineIndexer';\nimport { LoggerService } from '@backstage/backend-plugin-api';\n\n/**\n * Type of translated query for the Lunr Search Engine.\n * @public\n */\nexport type ConcreteLunrQuery = {\n lunrQueryBuilder: lunr.Index.QueryBuilder;\n documentTypes?: string[];\n pageSize: number;\n};\n\ntype LunrResultEnvelope = {\n result: lunr.Index.Result;\n type: string;\n};\n\n/**\n * Translator responsible for translating search term and filters to a query that the Lunr Search Engine understands.\n * @public\n */\nexport type LunrQueryTranslator = (query: SearchQuery) => ConcreteLunrQuery;\n\n/**\n * Lunr specific search engine implementation.\n * @public\n */\nexport class LunrSearchEngine implements SearchEngine {\n protected lunrIndices: Record<string, lunr.Index> = {};\n protected docStore: Record<string, IndexableDocument>;\n protected logger: LoggerService;\n protected highlightPreTag: string;\n protected highlightPostTag: string;\n\n constructor(options: { logger: LoggerService }) {\n this.logger = options.logger;\n this.docStore = {};\n const uuidTag = uuid();\n this.highlightPreTag = `<${uuidTag}>`;\n this.highlightPostTag = `</${uuidTag}>`;\n }\n\n protected translator: QueryTranslator = ({\n term,\n filters,\n types,\n pageLimit,\n }: SearchQuery): ConcreteLunrQuery => {\n const pageSize = pageLimit || 25;\n\n return {\n lunrQueryBuilder: q => {\n const termToken = lunr.tokenizer(term);\n\n // Support for typeahead search is based on https://github.com/olivernn/lunr.js/issues/256#issuecomment-295407852\n // look for an exact match and apply a large positive boost\n q.term(termToken, {\n usePipeline: true,\n boost: 100,\n });\n // look for terms that match the beginning of this term and apply a\n // medium boost\n q.term(termToken, {\n usePipeline: false,\n boost: 10,\n wildcard: lunr.Query.wildcard.TRAILING,\n });\n // look for terms that match with an edit distance of 2 and apply a\n // small boost\n q.term(termToken, {\n usePipeline: false,\n editDistance: 2,\n boost: 1,\n });\n\n if (filters) {\n Object.entries(filters).forEach(([field, fieldValue]) => {\n if (!q.allFields.includes(field)) {\n // Throw for unknown field, as this will be a non match\n throw new Error(`unrecognised field ${field}`);\n }\n // Arrays are poorly supported, but we can make it better for single-item arrays,\n // which should be a common case\n const value =\n Array.isArray(fieldValue) && fieldValue.length === 1\n ? fieldValue[0]\n : fieldValue;\n\n // Require that the given field has the given value\n if (['string', 'number', 'boolean'].includes(typeof value)) {\n q.term(\n lunr\n .tokenizer(value?.toString())\n .map(lunr.stopWordFilter)\n .filter(element => element !== undefined),\n {\n presence: lunr.Query.presence.REQUIRED,\n fields: [field],\n },\n );\n } else if (Array.isArray(value)) {\n // Illustrate how multi-value filters could work.\n // But warn that Lurn supports this poorly.\n this.logger.warn(\n `Non-scalar filter value used for field ${field}. Consider using a different Search Engine for better results.`,\n );\n q.term(lunr.tokenizer(value), {\n presence: lunr.Query.presence.OPTIONAL,\n fields: [field],\n });\n } else {\n // Log a warning or something about unknown filter value\n this.logger.warn(`Unknown filter type used on field ${field}`);\n }\n });\n }\n },\n documentTypes: types,\n pageSize,\n };\n };\n\n setTranslator(translator: LunrQueryTranslator) {\n this.translator = translator;\n }\n\n async getIndexer(type: string) {\n const indexer = new LunrSearchEngineIndexer();\n const indexerLogger = this.logger.child({ documentType: type });\n let errorThrown: Error | undefined;\n\n indexer.on('error', err => {\n errorThrown = err;\n });\n\n indexer.on('close', () => {\n // Once the stream is closed, build the index and store the documents in\n // memory for later retrieval.\n const newDocuments = indexer.getDocumentStore();\n const docStoreExists = this.lunrIndices[type] !== undefined;\n const documentsIndexed = Object.keys(newDocuments).length;\n\n // Do not set the index if there was an error or if no documents were\n // indexed. This ensures search continues to work for an index, even in\n // case of transient issues in underlying collators.\n if (!errorThrown && documentsIndexed > 0) {\n this.lunrIndices[type] = indexer.buildIndex();\n this.docStore = { ...this.docStore, ...newDocuments };\n } else {\n indexerLogger.warn(\n `Index for ${type} was not ${\n docStoreExists ? 'replaced' : 'created'\n }: ${\n errorThrown\n ? 'an error was encountered'\n : 'indexer received 0 documents'\n }`,\n );\n }\n });\n\n return indexer;\n }\n\n async query(query: SearchQuery): Promise<IndexableResultSet> {\n const { lunrQueryBuilder, documentTypes, pageSize } = this.translator(\n query,\n ) as ConcreteLunrQuery;\n\n const results: LunrResultEnvelope[] = [];\n\n const indexKeys = Object.keys(this.lunrIndices).filter(\n type => !documentTypes || documentTypes.includes(type),\n );\n\n if (documentTypes?.length && !indexKeys.length) {\n throw new MissingIndexError(\n `Missing index for ${documentTypes?.toString()}. This could be because the index hasn't been created yet or there was a problem during index creation.`,\n );\n }\n\n // Iterate over the filtered list of this.lunrIndex keys.\n indexKeys.forEach(type => {\n try {\n results.push(\n ...this.lunrIndices[type].query(lunrQueryBuilder).map(result => {\n return {\n result: result,\n type: type,\n };\n }),\n );\n } catch (err) {\n // if a field does not exist on a index, we can see that as a no-match\n if (\n err instanceof Error &&\n err.message.startsWith('unrecognised field')\n ) {\n return;\n }\n throw err;\n }\n });\n\n // Sort results.\n results.sort((doc1, doc2) => {\n return doc2.result.score - doc1.result.score;\n });\n\n // Perform paging\n const { page } = decodePageCursor(query.pageCursor);\n const offset = page * pageSize;\n const hasPreviousPage = page > 0;\n const hasNextPage = results.length > offset + pageSize;\n const nextPageCursor = hasNextPage\n ? encodePageCursor({ page: page + 1 })\n : undefined;\n const previousPageCursor = hasPreviousPage\n ? encodePageCursor({ page: page - 1 })\n : undefined;\n\n // Translate results into IndexableResultSet\n const realResultSet: IndexableResultSet = {\n results: results.slice(offset, offset + pageSize).map((d, index) => ({\n type: d.type,\n document: this.docStore[d.result.ref],\n rank: page * pageSize + index + 1,\n highlight: {\n preTag: this.highlightPreTag,\n postTag: this.highlightPostTag,\n fields: parseHighlightFields({\n preTag: this.highlightPreTag,\n postTag: this.highlightPostTag,\n doc: this.docStore[d.result.ref],\n positionMetadata: d.result.matchData.metadata as any,\n }),\n },\n })),\n numberOfResults: results.length,\n nextPageCursor,\n previousPageCursor,\n };\n\n return realResultSet;\n }\n}\n\nexport function decodePageCursor(pageCursor?: string): { page: number } {\n if (!pageCursor) {\n return { page: 0 };\n }\n\n return {\n page: Number(Buffer.from(pageCursor, 'base64').toString('utf-8')),\n };\n}\n\nexport function encodePageCursor({ page }: { page: number }): string {\n return Buffer.from(`${page}`, 'utf-8').toString('base64');\n}\n\ntype ParseHighlightFieldsProps = {\n preTag: string;\n postTag: string;\n doc: any;\n positionMetadata: {\n [term: string]: {\n [field: string]: {\n position: number[][];\n };\n };\n };\n};\n\nexport function parseHighlightFields({\n preTag,\n postTag,\n doc,\n positionMetadata,\n}: ParseHighlightFieldsProps): { [field: string]: string } {\n // Merge the field positions across all query terms\n const highlightFieldPositions = Object.values(positionMetadata).reduce(\n (fieldPositions, metadata) => {\n Object.keys(metadata).map(fieldKey => {\n const validFieldMetadataPositions = metadata[\n fieldKey\n ]?.position?.filter(position => Array.isArray(position));\n if (validFieldMetadataPositions.length) {\n fieldPositions[fieldKey] = fieldPositions[fieldKey] ?? [];\n fieldPositions[fieldKey].push(...validFieldMetadataPositions);\n }\n });\n\n return fieldPositions;\n },\n {} as { [field: string]: number[][] },\n );\n\n return Object.fromEntries(\n Object.entries(highlightFieldPositions).map(([field, positions]) => {\n positions.sort((a, b) => b[0] - a[0]);\n\n const highlightedField = positions.reduce((content, pos) => {\n return (\n `${String(content).substring(0, pos[0])}${preTag}` +\n `${String(content).substring(pos[0], pos[0] + pos[1])}` +\n `${postTag}${String(content).substring(pos[0] + pos[1])}`\n );\n }, doc[field] ?? '');\n\n return [field, highlightedField];\n }),\n );\n}\n"],"names":["uuid","lunr","LunrSearchEngineIndexer","MissingIndexError"],"mappings":";;;;;;;;;;;AAqDO,MAAM,gBAAyC,CAAA;AAAA,EAC1C,cAA0C,EAAC;AAAA,EAC3C,QAAA;AAAA,EACA,MAAA;AAAA,EACA,eAAA;AAAA,EACA,gBAAA;AAAA,EAEV,YAAY,OAAoC,EAAA;AAC9C,IAAA,IAAA,CAAK,SAAS,OAAQ,CAAA,MAAA;AACtB,IAAA,IAAA,CAAK,WAAW,EAAC;AACjB,IAAA,MAAM,UAAUA,OAAK,EAAA;AACrB,IAAK,IAAA,CAAA,eAAA,GAAkB,IAAI,OAAO,CAAA,CAAA,CAAA;AAClC,IAAK,IAAA,CAAA,gBAAA,GAAmB,KAAK,OAAO,CAAA,CAAA,CAAA;AAAA;AACtC,EAEU,aAA8B,CAAC;AAAA,IACvC,IAAA;AAAA,IACA,OAAA;AAAA,IACA,KAAA;AAAA,IACA;AAAA,GACoC,KAAA;AACpC,IAAA,MAAM,WAAW,SAAa,IAAA,EAAA;AAE9B,IAAO,OAAA;AAAA,MACL,kBAAkB,CAAK,CAAA,KAAA;AACrB,QAAM,MAAA,SAAA,GAAYC,qBAAK,CAAA,SAAA,CAAU,IAAI,CAAA;AAIrC,QAAA,CAAA,CAAE,KAAK,SAAW,EAAA;AAAA,UAChB,WAAa,EAAA,IAAA;AAAA,UACb,KAAO,EAAA;AAAA,SACR,CAAA;AAGD,QAAA,CAAA,CAAE,KAAK,SAAW,EAAA;AAAA,UAChB,WAAa,EAAA,KAAA;AAAA,UACb,KAAO,EAAA,EAAA;AAAA,UACP,QAAA,EAAUA,qBAAK,CAAA,KAAA,CAAM,QAAS,CAAA;AAAA,SAC/B,CAAA;AAGD,QAAA,CAAA,CAAE,KAAK,SAAW,EAAA;AAAA,UAChB,WAAa,EAAA,KAAA;AAAA,UACb,YAAc,EAAA,CAAA;AAAA,UAC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,2BAA2B,CAAA;AAAA;AAC9D,OACD,CAAA;AAED,MAAO,OAAA,cAAA;AAAA,KACT;AAAA,IACA;AAAC,GACH;AAEA,EAAA,OAAO,MAAO,CAAA,WAAA;AAAA,IACZ,MAAA,CAAO,QAAQ,uBAAuB,CAAA,CAAE,IAAI,CAAC,CAAC,KAAO,EAAA,SAAS,CAAM,KAAA;AAClE,MAAU,SAAA,CAAA,IAAA,CAAK,CAAC,CAAG,EAAA,CAAA,KAAM,EAAE,CAAC,CAAA,GAAI,CAAE,CAAA,CAAC,CAAC,CAAA;AAEpC,MAAA,MAAM,gBAAmB,GAAA,SAAA,CAAU,MAAO,CAAA,CAAC,SAAS,GAAQ,KAAA;AAC1D,QAAA,OACE,GAAG,MAAO,CAAA,OAAO,EAAE,SAAU,CAAA,CAAA,EAAG,IAAI,CAAC,CAAC,CAAC,CAAA,EAAG,MAAM,CAC7C,EAAA,MAAA,CAAO,OAAO,CAAE,CAAA,SAAA,CAAU,IAAI,CAAC,CAAA,EAAG,GAAI,CAAA,CAAC,IAAI,GAAI,CAAA,CAAC,CAAC,CAAC,CAAA,EAClD,OAAO,CAAG,EAAA,MAAA,CAAO,OAAO,CAAA,CAAE,UAAU,GAAI,CAAA,CAAC,IAAI,GAAI,CAAA,CAAC,CAAC,CAAC,CAAA,CAAA;AAAA,OAExD,EAAA,GAAA,CAAI,KAAK,CAAA,IAAK,EAAE,CAAA;AAEnB,MAAO,OAAA,CAAC,OAAO,gBAAgB,CAAA;AAAA,KAChC;AAAA,GACH;AACF;;;;;;;"}
1
+ {"version":3,"file":"LunrSearchEngine.cjs.js","sources":["../../src/engines/LunrSearchEngine.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n IndexableDocument,\n IndexableResultSet,\n SearchQuery,\n} from '@backstage/plugin-search-common';\nimport { QueryTranslator, SearchEngine } from '../types';\nimport { MissingIndexError } from '../errors';\nimport lunr from 'lunr';\nimport { v4 as uuid } from 'uuid';\nimport { LunrSearchEngineIndexer } from './LunrSearchEngineIndexer';\nimport { LoggerService } from '@backstage/backend-plugin-api';\n\n/**\n * Type of translated query for the Lunr Search Engine.\n * @public\n */\nexport type ConcreteLunrQuery = {\n lunrQueryBuilder: lunr.Index.QueryBuilder;\n documentTypes?: string[];\n pageSize: number;\n};\n\ntype LunrResultEnvelope = {\n result: lunr.Index.Result;\n type: string;\n};\n\n/**\n * Translator responsible for translating search term and filters to a query that the Lunr Search Engine understands.\n * @public\n */\nexport type LunrQueryTranslator = (query: SearchQuery) => ConcreteLunrQuery;\n\n/**\n * Lunr specific search engine implementation.\n * @public\n */\nexport class LunrSearchEngine implements SearchEngine {\n protected lunrIndices: Record<string, lunr.Index> = {};\n protected docStore: Record<string, IndexableDocument>;\n protected logger: LoggerService;\n protected highlightPreTag: string;\n protected highlightPostTag: string;\n\n constructor(options: { logger: LoggerService }) {\n this.logger = options.logger;\n this.docStore = {};\n const uuidTag = uuid();\n this.highlightPreTag = `<${uuidTag}>`;\n this.highlightPostTag = `</${uuidTag}>`;\n }\n\n protected translator: QueryTranslator = ({\n term,\n filters,\n types,\n pageLimit,\n }: SearchQuery): ConcreteLunrQuery => {\n const pageSize = pageLimit || 25;\n\n return {\n lunrQueryBuilder: q => {\n const termToken = lunr.tokenizer(term);\n\n // Support for typeahead search is based on https://github.com/olivernn/lunr.js/issues/256#issuecomment-295407852\n // look for an exact match and apply a large positive boost\n q.term(termToken, {\n usePipeline: true,\n boost: 100,\n });\n // look for terms that match the beginning of this term and apply a\n // medium boost\n q.term(termToken, {\n usePipeline: false,\n boost: 10,\n wildcard: lunr.Query.wildcard.TRAILING,\n });\n // look for terms that match with an edit distance of 2 and apply a\n // small boost\n q.term(termToken, {\n usePipeline: false,\n editDistance: 2,\n boost: 1,\n });\n\n if (filters) {\n Object.entries(filters).forEach(([field, fieldValue]) => {\n if (!q.allFields.includes(field)) {\n // Throw for unknown field, as this will be a non match\n throw new Error(`unrecognised field ${field}`);\n }\n // Arrays are poorly supported, but we can make it better for single-item arrays,\n // which should be a common case\n const value =\n Array.isArray(fieldValue) && fieldValue.length === 1\n ? fieldValue[0]\n : fieldValue;\n\n // Require that the given field has the given value\n if (['string', 'number', 'boolean'].includes(typeof value)) {\n q.term(\n lunr\n .tokenizer(value?.toString())\n .map(lunr.stopWordFilter)\n .filter(element => element !== undefined),\n {\n presence: lunr.Query.presence.REQUIRED,\n fields: [field],\n },\n );\n } else if (Array.isArray(value)) {\n // Illustrate how multi-value filters could work.\n // But warn that Lurn supports this poorly.\n this.logger.warn(\n `Non-scalar filter value used for field ${field}. Consider using a different Search Engine for better results.`,\n );\n q.term(lunr.tokenizer(value), {\n presence: lunr.Query.presence.OPTIONAL,\n fields: [field],\n });\n } else {\n // Log a warning or something about unknown filter value\n this.logger.warn(`Unknown filter type used on field ${field}`);\n }\n });\n }\n },\n documentTypes: types,\n pageSize,\n };\n };\n\n setTranslator(translator: LunrQueryTranslator) {\n this.translator = translator;\n }\n\n async getIndexer(type: string) {\n const indexer = new LunrSearchEngineIndexer();\n const indexerLogger = this.logger.child({ documentType: type });\n let errorThrown: Error | undefined;\n\n indexer.on('error', err => {\n errorThrown = err;\n });\n\n indexer.on('close', () => {\n // Once the stream is closed, build the index and store the documents in\n // memory for later retrieval.\n const newDocuments = indexer.getDocumentStore();\n const docStoreExists = this.lunrIndices[type] !== undefined;\n const documentsIndexed = Object.keys(newDocuments).length;\n\n // Do not set the index if there was an error or if no documents were\n // indexed. This ensures search continues to work for an index, even in\n // case of transient issues in underlying collators.\n if (!errorThrown && documentsIndexed > 0) {\n this.lunrIndices[type] = indexer.buildIndex();\n this.docStore = { ...this.docStore, ...newDocuments };\n } else {\n indexerLogger.warn(\n `Index for ${type} was not ${\n docStoreExists ? 'replaced' : 'created'\n }: ${\n errorThrown\n ? 'an error was encountered'\n : 'indexer received 0 documents'\n }`,\n );\n }\n });\n\n return indexer;\n }\n\n async query(query: SearchQuery): Promise<IndexableResultSet> {\n const { lunrQueryBuilder, documentTypes, pageSize } = this.translator(\n query,\n ) as ConcreteLunrQuery;\n\n const results: LunrResultEnvelope[] = [];\n\n const indexKeys = Object.keys(this.lunrIndices).filter(\n type => !documentTypes || documentTypes.includes(type),\n );\n\n if (documentTypes?.length && !indexKeys.length) {\n throw new MissingIndexError(\n `Missing index for ${documentTypes?.toString()}. This could be because the index hasn't been created yet or there was a problem during index creation.`,\n );\n }\n\n // Iterate over the filtered list of this.lunrIndex keys.\n indexKeys.forEach(type => {\n try {\n results.push(\n ...this.lunrIndices[type].query(lunrQueryBuilder).map(result => {\n return {\n result: result,\n type: type,\n };\n }),\n );\n } catch (err) {\n // if a field does not exist on a index, we can see that as a no-match\n if (\n err instanceof Error &&\n err.message.startsWith('unrecognised field')\n ) {\n return;\n }\n throw err;\n }\n });\n\n // Sort results.\n results.sort((doc1, doc2) => {\n return doc2.result.score - doc1.result.score;\n });\n\n // Perform paging\n const { page } = decodePageCursor(query.pageCursor);\n const offset = page * pageSize;\n const hasPreviousPage = page > 0;\n const hasNextPage = results.length > offset + pageSize;\n const nextPageCursor = hasNextPage\n ? encodePageCursor({ page: page + 1 })\n : undefined;\n const previousPageCursor = hasPreviousPage\n ? encodePageCursor({ page: page - 1 })\n : undefined;\n\n // Translate results into IndexableResultSet\n const realResultSet: IndexableResultSet = {\n results: results.slice(offset, offset + pageSize).map((d, index) => ({\n type: d.type,\n document: this.docStore[d.result.ref],\n rank: page * pageSize + index + 1,\n highlight: {\n preTag: this.highlightPreTag,\n postTag: this.highlightPostTag,\n fields: parseHighlightFields({\n preTag: this.highlightPreTag,\n postTag: this.highlightPostTag,\n doc: this.docStore[d.result.ref],\n positionMetadata: d.result.matchData.metadata as any,\n }),\n },\n })),\n numberOfResults: results.length,\n nextPageCursor,\n previousPageCursor,\n };\n\n return realResultSet;\n }\n}\n\nexport function decodePageCursor(pageCursor?: string): { page: number } {\n if (!pageCursor) {\n return { page: 0 };\n }\n\n return {\n page: Number(Buffer.from(pageCursor, 'base64').toString('utf-8')),\n };\n}\n\nexport function encodePageCursor({ page }: { page: number }): string {\n return Buffer.from(`${page}`, 'utf-8').toString('base64');\n}\n\ntype ParseHighlightFieldsProps = {\n preTag: string;\n postTag: string;\n doc: any;\n positionMetadata: {\n [term: string]: {\n [field: string]: {\n position: number[][];\n };\n };\n };\n};\n\nexport function parseHighlightFields({\n preTag,\n postTag,\n doc,\n positionMetadata,\n}: ParseHighlightFieldsProps): { [field: string]: string } {\n // Merge the field positions across all query terms\n const highlightFieldPositions = Object.values(positionMetadata).reduce(\n (fieldPositions, metadata) => {\n Object.keys(metadata).map(fieldKey => {\n const validFieldMetadataPositions = metadata[\n fieldKey\n ]?.position?.filter(position => Array.isArray(position));\n if (validFieldMetadataPositions.length) {\n fieldPositions[fieldKey] = fieldPositions[fieldKey] ?? [];\n fieldPositions[fieldKey].push(...validFieldMetadataPositions);\n }\n });\n\n return fieldPositions;\n },\n {} as { [field: string]: number[][] },\n );\n\n return Object.fromEntries(\n Object.entries(highlightFieldPositions).map(([field, positions]) => {\n positions.sort((a, b) => b[0] - a[0]);\n\n const highlightedField = positions.reduce((content, pos) => {\n return (\n `${String(content).substring(0, pos[0])}${preTag}` +\n `${String(content).substring(pos[0], pos[0] + pos[1])}` +\n `${postTag}${String(content).substring(pos[0] + pos[1])}`\n );\n }, doc[field] ?? '');\n\n return [field, highlightedField];\n }),\n );\n}\n"],"names":["uuid","lunr","LunrSearchEngineIndexer","MissingIndexError"],"mappings":";;;;;;;;;;;AAqDO,MAAM,gBAAA,CAAyC;AAAA,EAC1C,cAA0C,EAAC;AAAA,EAC3C,QAAA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AAA,IACA;AAAC,GACH;AAEA,EAAA,OAAO,MAAA,CAAO,WAAA;AAAA,IACZ,MAAA,CAAO,QAAQ,uBAAuB,CAAA,CAAE,IAAI,CAAC,CAAC,KAAA,EAAO,SAAS,CAAA,KAAM;AAClE,MAAA,SAAA,CAAU,IAAA,CAAK,CAAC,CAAA,EAAG,CAAA,KAAM,EAAE,CAAC,CAAA,GAAI,CAAA,CAAE,CAAC,CAAC,CAAA;AAEpC,MAAA,MAAM,gBAAA,GAAmB,SAAA,CAAU,MAAA,CAAO,CAAC,SAAS,GAAA,KAAQ;AAC1D,QAAA,OACE,GAAG,MAAA,CAAO,OAAO,EAAE,SAAA,CAAU,CAAA,EAAG,IAAI,CAAC,CAAC,CAAC,CAAA,EAAG,MAAM,CAAA,EAC7C,MAAA,CAAO,OAAO,CAAA,CAAE,SAAA,CAAU,IAAI,CAAC,CAAA,EAAG,GAAA,CAAI,CAAC,IAAI,GAAA,CAAI,CAAC,CAAC,CAAC,CAAA,EAClD,OAAO,CAAA,EAAG,MAAA,CAAO,OAAO,CAAA,CAAE,UAAU,GAAA,CAAI,CAAC,IAAI,GAAA,CAAI,CAAC,CAAC,CAAC,CAAA,CAAA;AAAA,MAE3D,CAAA,EAAG,GAAA,CAAI,KAAK,CAAA,IAAK,EAAE,CAAA;AAEnB,MAAA,OAAO,CAAC,OAAO,gBAAgB,CAAA;AAAA,IACjC,CAAC;AAAA,GACH;AACF;;;;;;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"LunrSearchEngineIndexer.cjs.js","sources":["../../src/engines/LunrSearchEngineIndexer.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { IndexableDocument } from '@backstage/plugin-search-common';\nimport lunr from 'lunr';\nimport { BatchSearchEngineIndexer } from '../indexing';\n\n/**\n * Lunr specific search engine indexer\n * @public\n */\nexport class LunrSearchEngineIndexer extends BatchSearchEngineIndexer {\n private schemaInitialized = false;\n private builder: lunr.Builder;\n private docStore: Record<string, IndexableDocument> = {};\n\n constructor() {\n super({ batchSize: 1000 });\n\n this.builder = new lunr.Builder();\n this.builder.pipeline.add(lunr.trimmer, lunr.stopWordFilter, lunr.stemmer);\n this.builder.searchPipeline.add(lunr.stemmer);\n this.builder.metadataWhitelist = ['position'];\n }\n\n // No async initialization required.\n async initialize(): Promise<void> {}\n async finalize(): Promise<void> {}\n\n async index(documents: IndexableDocument[]): Promise<void> {\n if (!this.schemaInitialized) {\n // Make this lunr index aware of all relevant fields.\n Object.keys(documents[0]).forEach(field => {\n this.builder.field(field);\n });\n\n // Set \"location\" field as reference field\n this.builder.ref('location');\n\n this.schemaInitialized = true;\n }\n\n documents.forEach(document => {\n // Add document to Lunar index\n this.builder.add(document);\n\n // Store documents in memory to be able to look up document using the ref during query time\n // This is not how you should implement your SearchEngine implementation! Do not copy!\n this.docStore[document.location] = document;\n });\n }\n\n buildIndex() {\n return this.builder.build();\n }\n\n getDocumentStore() {\n return this.docStore;\n }\n}\n"],"names":["BatchSearchEngineIndexer","lunr"],"mappings":";;;;;;;;;;;AAwBO,MAAM,gCAAgCA,iDAAyB,CAAA;AAAA,EAC5D,iBAAoB,GAAA,KAAA;AAAA,EACpB,OAAA;AAAA,EACA,WAA8C,EAAC;AAAA,EAEvD,WAAc,GAAA;AACZ,IAAM,KAAA,CAAA,EAAE,SAAW,EAAA,GAAA,EAAM,CAAA;AAEzB,IAAK,IAAA,CAAA,OAAA,GAAU,IAAIC,qBAAA,CAAK,OAAQ,EAAA;AAChC,IAAK,IAAA,CAAA,OAAA,CAAQ,SAAS,GAAI,CAAAA,qBAAA,CAAK,SAASA,qBAAK,CAAA,cAAA,EAAgBA,sBAAK,OAAO,CAAA;AACzE,IAAA,IAAA,CAAK,OAAQ,CAAA,cAAA,CAAe,GAAI,CAAAA,qBAAA,CAAK,OAAO,CAAA;AAC5C,IAAK,IAAA,CAAA,OAAA,CAAQ,iBAAoB,GAAA,CAAC,UAAU,CAAA;AAAA;AAC9C;AAAA,EAGA,MAAM,UAA4B,GAAA;AAAA;AAAC,EACnC,MAAM,QAA0B,GAAA;AAAA;AAAC,EAEjC,MAAM,MAAM,SAA+C,EAAA;AACzD,IAAI,IAAA,CAAC,KAAK,iBAAmB,EAAA;AAE3B,MAAA,MAAA,CAAO,KAAK,SAAU,CAAA,CAAC,CAAC,CAAA,CAAE,QAAQ,CAAS,KAAA,KAAA;AACzC,QAAK,IAAA,CAAA,OAAA,CAAQ,MAAM,KAAK,CAAA;AAAA,OACzB,CAAA;AAGD,MAAK,IAAA,CAAA,OAAA,CAAQ,IAAI,UAAU,CAAA;AAE3B,MAAA,IAAA,CAAK,iBAAoB,GAAA,IAAA;AAAA;AAG3B,IAAA,SAAA,CAAU,QAAQ,CAAY,QAAA,KAAA;AAE5B,MAAK,IAAA,CAAA,OAAA,CAAQ,IAAI,QAAQ,CAAA;AAIzB,MAAK,IAAA,CAAA,QAAA,CAAS,QAAS,CAAA,QAAQ,CAAI,GAAA,QAAA;AAAA,KACpC,CAAA;AAAA;AACH,EAEA,UAAa,GAAA;AACX,IAAO,OAAA,IAAA,CAAK,QAAQ,KAAM,EAAA;AAAA;AAC5B,EAEA,gBAAmB,GAAA;AACjB,IAAA,OAAO,IAAK,CAAA,QAAA;AAAA;AAEhB;;;;"}
1
+ {"version":3,"file":"LunrSearchEngineIndexer.cjs.js","sources":["../../src/engines/LunrSearchEngineIndexer.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { IndexableDocument } from '@backstage/plugin-search-common';\nimport lunr from 'lunr';\nimport { BatchSearchEngineIndexer } from '../indexing';\n\n/**\n * Lunr specific search engine indexer\n * @public\n */\nexport class LunrSearchEngineIndexer extends BatchSearchEngineIndexer {\n private schemaInitialized = false;\n private builder: lunr.Builder;\n private docStore: Record<string, IndexableDocument> = {};\n\n constructor() {\n super({ batchSize: 1000 });\n\n this.builder = new lunr.Builder();\n this.builder.pipeline.add(lunr.trimmer, lunr.stopWordFilter, lunr.stemmer);\n this.builder.searchPipeline.add(lunr.stemmer);\n this.builder.metadataWhitelist = ['position'];\n }\n\n // No async initialization required.\n async initialize(): Promise<void> {}\n async finalize(): Promise<void> {}\n\n async index(documents: IndexableDocument[]): Promise<void> {\n if (!this.schemaInitialized) {\n // Make this lunr index aware of all relevant fields.\n Object.keys(documents[0]).forEach(field => {\n this.builder.field(field);\n });\n\n // Set \"location\" field as reference field\n this.builder.ref('location');\n\n this.schemaInitialized = true;\n }\n\n documents.forEach(document => {\n // Add document to Lunar index\n this.builder.add(document);\n\n // Store documents in memory to be able to look up document using the ref during query time\n // This is not how you should implement your SearchEngine implementation! Do not copy!\n this.docStore[document.location] = document;\n });\n }\n\n buildIndex() {\n return this.builder.build();\n }\n\n getDocumentStore() {\n return this.docStore;\n }\n}\n"],"names":["BatchSearchEngineIndexer","lunr"],"mappings":";;;;;;;;;;;AAwBO,MAAM,gCAAgCA,iDAAA,CAAyB;AAAA,EAC5D,iBAAA,GAAoB,KAAA;AAAA,EACpB,OAAA;AAAA,EACA,WAA8C,EAAC;AAAA,EAEvD,WAAA,GAAc;AACZ,IAAA,KAAA,CAAM,EAAE,SAAA,EAAW,GAAA,EAAM,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 @@
1
- {"version":3,"file":"errors.cjs.js","sources":["../src/errors.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { isError } from '@backstage/errors';\n\n/**\n * Failed to query documents for index that does not exist.\n * @public\n */\nexport class MissingIndexError extends Error {\n /**\n * An inner error that caused this error to be thrown, if any.\n */\n readonly cause?: Error | undefined;\n\n constructor(message?: string, cause?: Error | unknown) {\n super(message);\n\n Error.captureStackTrace?.(this, this.constructor);\n\n this.name = this.constructor.name;\n this.cause = isError(cause) ? cause : undefined;\n }\n}\n"],"names":["isError"],"mappings":";;;;AAsBO,MAAM,0BAA0B,KAAM,CAAA;AAAA;AAAA;AAAA;AAAA,EAIlC,KAAA;AAAA,EAET,WAAA,CAAY,SAAkB,KAAyB,EAAA;AACrD,IAAA,KAAA,CAAM,OAAO,CAAA;AAEb,IAAM,KAAA,CAAA,iBAAA,GAAoB,IAAM,EAAA,IAAA,CAAK,WAAW,CAAA;AAEhD,IAAK,IAAA,CAAA,IAAA,GAAO,KAAK,WAAY,CAAA,IAAA;AAC7B,IAAA,IAAA,CAAK,KAAQ,GAAAA,cAAA,CAAQ,KAAK,CAAA,GAAI,KAAQ,GAAA,KAAA,CAAA;AAAA;AAE1C;;;;"}
1
+ {"version":3,"file":"errors.cjs.js","sources":["../src/errors.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { isError } from '@backstage/errors';\n\n/**\n * Failed to query documents for index that does not exist.\n * @public\n */\nexport class MissingIndexError extends Error {\n /**\n * An inner error that caused this error to be thrown, if any.\n */\n readonly cause?: Error | undefined;\n\n constructor(message?: string, cause?: Error | unknown) {\n super(message);\n\n Error.captureStackTrace?.(this, this.constructor);\n\n this.name = this.constructor.name;\n this.cause = isError(cause) ? cause : undefined;\n }\n}\n"],"names":["isError"],"mappings":";;;;AAsBO,MAAM,0BAA0B,KAAA,CAAM;AAAA;AAAA;AAAA;AAAA,EAIlC,KAAA;AAAA,EAET,WAAA,CAAY,SAAkB,KAAA,EAAyB;AACrD,IAAA,KAAA,CAAM,OAAO,CAAA;AAEb,IAAA,KAAA,CAAM,iBAAA,GAAoB,IAAA,EAAM,IAAA,CAAK,WAAW,CAAA;AAEhD,IAAA,IAAA,CAAK,IAAA,GAAO,KAAK,WAAA,CAAY,IAAA;AAC7B,IAAA,IAAA,CAAK,KAAA,GAAQA,cAAA,CAAQ,KAAK,CAAA,GAAI,KAAA,GAAQ,MAAA;AAAA,EACxC;AACF;;;;"}
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { DocumentCollatorFactory, DocumentDecoratorFactory, SearchQuery, IndexableResultSet, DocumentTypeInfo, IndexableDocument } from '@backstage/plugin-search-common';
2
- import { SchedulerServiceTaskFunction, SchedulerServiceTaskRunner, LoggerService, BackstageCredentials, UrlReaderService } from '@backstage/backend-plugin-api';
1
+ import { SearchQuery, IndexableResultSet, DocumentCollatorFactory, DocumentDecoratorFactory, DocumentTypeInfo, IndexableDocument } from '@backstage/plugin-search-common';
2
+ import { LoggerService, SchedulerServiceTaskFunction, SchedulerServiceTaskRunner, BackstageCredentials, UrlReaderService } from '@backstage/backend-plugin-api';
3
3
  import { Writable, Readable, Transform } from 'stream';
4
4
  import { Config } from '@backstage/config';
5
5
  import { Permission } from '@backstage/plugin-permission-common';
@@ -1 +1 @@
1
- {"version":3,"file":"BatchSearchEngineIndexer.cjs.js","sources":["../../src/indexing/BatchSearchEngineIndexer.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { assertError } from '@backstage/errors';\nimport { IndexableDocument } from '@backstage/plugin-search-common';\nimport { Writable } from 'stream';\n\n/**\n * Options for {@link BatchSearchEngineIndexer}\n * @public\n */\nexport type BatchSearchEngineOptions = {\n batchSize: number;\n};\n\n/**\n * Base class encapsulating batch-based stream processing. Useful as a base\n * class for search engine indexers.\n * @public\n */\nexport abstract class BatchSearchEngineIndexer extends Writable {\n private batchSize: number;\n private currentBatch: IndexableDocument[] = [];\n\n constructor(options: BatchSearchEngineOptions) {\n super({ objectMode: true });\n this.batchSize = options.batchSize;\n }\n\n /**\n * Receives an array of indexable documents (of size this.batchSize) which\n * should be written to the search engine. This method won't be called again\n * at least until it resolves.\n */\n public abstract index(documents: IndexableDocument[]): Promise<void>;\n\n /**\n * Any asynchronous setup tasks can be performed here.\n */\n public abstract initialize(): Promise<void>;\n\n /**\n * Any asynchronous teardown tasks can be performed here.\n */\n public abstract finalize(): Promise<void>;\n\n /**\n * Encapsulates initialization logic.\n * @internal\n */\n async _construct(done: (error?: Error | null | undefined) => void) {\n try {\n await this.initialize();\n done();\n } catch (e) {\n assertError(e);\n done(e);\n }\n }\n\n /**\n * Encapsulates batch stream write logic.\n * @internal\n */\n async _write(\n doc: IndexableDocument,\n _e: any,\n done: (error?: Error | null) => void,\n ) {\n this.currentBatch.push(doc);\n if (this.currentBatch.length < this.batchSize) {\n done();\n return;\n }\n\n try {\n await this.index(this.currentBatch);\n this.currentBatch = [];\n done();\n } catch (e) {\n assertError(e);\n done(e);\n }\n }\n\n /**\n * Encapsulates finalization and final error handling logic.\n * @internal\n */\n async _final(done: (error?: Error | null) => void) {\n try {\n // Index any remaining documents.\n if (this.currentBatch.length) {\n await this.index(this.currentBatch);\n this.currentBatch = [];\n }\n await this.finalize();\n done();\n } catch (e) {\n assertError(e);\n done(e);\n }\n }\n}\n"],"names":["Writable","assertError"],"mappings":";;;;;AAiCO,MAAe,iCAAiCA,eAAS,CAAA;AAAA,EACtD,SAAA;AAAA,EACA,eAAoC,EAAC;AAAA,EAE7C,YAAY,OAAmC,EAAA;AAC7C,IAAM,KAAA,CAAA,EAAE,UAAY,EAAA,IAAA,EAAM,CAAA;AAC1B,IAAA,IAAA,CAAK,YAAY,OAAQ,CAAA,SAAA;AAAA;AAC3B;AAAA;AAAA;AAAA;AAAA,EAuBA,MAAM,WAAW,IAAkD,EAAA;AACjE,IAAI,IAAA;AACF,MAAA,MAAM,KAAK,UAAW,EAAA;AACtB,MAAK,IAAA,EAAA;AAAA,aACE,CAAG,EAAA;AACV,MAAAC,kBAAA,CAAY,CAAC,CAAA;AACb,MAAA,IAAA,CAAK,CAAC,CAAA;AAAA;AACR;AACF;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,MAAA,CACJ,GACA,EAAA,EAAA,EACA,IACA,EAAA;AACA,IAAK,IAAA,CAAA,YAAA,CAAa,KAAK,GAAG,CAAA;AAC1B,IAAA,IAAI,IAAK,CAAA,YAAA,CAAa,MAAS,GAAA,IAAA,CAAK,SAAW,EAAA;AAC7C,MAAK,IAAA,EAAA;AACL,MAAA;AAAA;AAGF,IAAI,IAAA;AACF,MAAM,MAAA,IAAA,CAAK,KAAM,CAAA,IAAA,CAAK,YAAY,CAAA;AAClC,MAAA,IAAA,CAAK,eAAe,EAAC;AACrB,MAAK,IAAA,EAAA;AAAA,aACE,CAAG,EAAA;AACV,MAAAA,kBAAA,CAAY,CAAC,CAAA;AACb,MAAA,IAAA,CAAK,CAAC,CAAA;AAAA;AACR;AACF;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,OAAO,IAAsC,EAAA;AACjD,IAAI,IAAA;AAEF,MAAI,IAAA,IAAA,CAAK,aAAa,MAAQ,EAAA;AAC5B,QAAM,MAAA,IAAA,CAAK,KAAM,CAAA,IAAA,CAAK,YAAY,CAAA;AAClC,QAAA,IAAA,CAAK,eAAe,EAAC;AAAA;AAEvB,MAAA,MAAM,KAAK,QAAS,EAAA;AACpB,MAAK,IAAA,EAAA;AAAA,aACE,CAAG,EAAA;AACV,MAAAA,kBAAA,CAAY,CAAC,CAAA;AACb,MAAA,IAAA,CAAK,CAAC,CAAA;AAAA;AACR;AAEJ;;;;"}
1
+ {"version":3,"file":"BatchSearchEngineIndexer.cjs.js","sources":["../../src/indexing/BatchSearchEngineIndexer.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { assertError } from '@backstage/errors';\nimport { IndexableDocument } from '@backstage/plugin-search-common';\nimport { Writable } from 'stream';\n\n/**\n * Options for {@link BatchSearchEngineIndexer}\n * @public\n */\nexport type BatchSearchEngineOptions = {\n batchSize: number;\n};\n\n/**\n * Base class encapsulating batch-based stream processing. Useful as a base\n * class for search engine indexers.\n * @public\n */\nexport abstract class BatchSearchEngineIndexer extends Writable {\n private batchSize: number;\n private currentBatch: IndexableDocument[] = [];\n\n constructor(options: BatchSearchEngineOptions) {\n super({ objectMode: true });\n this.batchSize = options.batchSize;\n }\n\n /**\n * Receives an array of indexable documents (of size this.batchSize) which\n * should be written to the search engine. This method won't be called again\n * at least until it resolves.\n */\n public abstract index(documents: IndexableDocument[]): Promise<void>;\n\n /**\n * Any asynchronous setup tasks can be performed here.\n */\n public abstract initialize(): Promise<void>;\n\n /**\n * Any asynchronous teardown tasks can be performed here.\n */\n public abstract finalize(): Promise<void>;\n\n /**\n * Encapsulates initialization logic.\n * @internal\n */\n async _construct(done: (error?: Error | null | undefined) => void) {\n try {\n await this.initialize();\n done();\n } catch (e) {\n assertError(e);\n done(e);\n }\n }\n\n /**\n * Encapsulates batch stream write logic.\n * @internal\n */\n async _write(\n doc: IndexableDocument,\n _e: any,\n done: (error?: Error | null) => void,\n ) {\n this.currentBatch.push(doc);\n if (this.currentBatch.length < this.batchSize) {\n done();\n return;\n }\n\n try {\n await this.index(this.currentBatch);\n this.currentBatch = [];\n done();\n } catch (e) {\n assertError(e);\n done(e);\n }\n }\n\n /**\n * Encapsulates finalization and final error handling logic.\n * @internal\n */\n async _final(done: (error?: Error | null) => void) {\n try {\n // Index any remaining documents.\n if (this.currentBatch.length) {\n await this.index(this.currentBatch);\n this.currentBatch = [];\n }\n await this.finalize();\n done();\n } catch (e) {\n assertError(e);\n done(e);\n }\n }\n}\n"],"names":["Writable","assertError"],"mappings":";;;;;AAiCO,MAAe,iCAAiCA,eAAA,CAAS;AAAA,EACtD,SAAA;AAAA,EACA,eAAoC,EAAC;AAAA,EAE7C,YAAY,OAAA,EAAmC;AAC7C,IAAA,KAAA,CAAM,EAAE,UAAA,EAAY,IAAA,EAAM,CAAA;AAC1B,IAAA,IAAA,CAAK,YAAY,OAAA,CAAQ,SAAA;AAAA,EAC3B;AAAA;AAAA;AAAA;AAAA;AAAA,EAuBA,MAAM,WAAW,IAAA,EAAkD;AACjE,IAAA,IAAI;AACF,MAAA,MAAM,KAAK,UAAA,EAAW;AACtB,MAAA,IAAA,EAAK;AAAA,IACP,SAAS,CAAA,EAAG;AACV,MAAAC,kBAAA,CAAY,CAAC,CAAA;AACb,MAAA,IAAA,CAAK,CAAC,CAAA;AAAA,IACR;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,MAAA,CACJ,GAAA,EACA,EAAA,EACA,IAAA,EACA;AACA,IAAA,IAAA,CAAK,YAAA,CAAa,KAAK,GAAG,CAAA;AAC1B,IAAA,IAAI,IAAA,CAAK,YAAA,CAAa,MAAA,GAAS,IAAA,CAAK,SAAA,EAAW;AAC7C,MAAA,IAAA,EAAK;AACL,MAAA;AAAA,IACF;AAEA,IAAA,IAAI;AACF,MAAA,MAAM,IAAA,CAAK,KAAA,CAAM,IAAA,CAAK,YAAY,CAAA;AAClC,MAAA,IAAA,CAAK,eAAe,EAAC;AACrB,MAAA,IAAA,EAAK;AAAA,IACP,SAAS,CAAA,EAAG;AACV,MAAAA,kBAAA,CAAY,CAAC,CAAA;AACb,MAAA,IAAA,CAAK,CAAC,CAAA;AAAA,IACR;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,OAAO,IAAA,EAAsC;AACjD,IAAA,IAAI;AAEF,MAAA,IAAI,IAAA,CAAK,aAAa,MAAA,EAAQ;AAC5B,QAAA,MAAM,IAAA,CAAK,KAAA,CAAM,IAAA,CAAK,YAAY,CAAA;AAClC,QAAA,IAAA,CAAK,eAAe,EAAC;AAAA,MACvB;AACA,MAAA,MAAM,KAAK,QAAA,EAAS;AACpB,MAAA,IAAA,EAAK;AAAA,IACP,SAAS,CAAA,EAAG;AACV,MAAAA,kBAAA,CAAY,CAAC,CAAA;AACb,MAAA,IAAA,CAAK,CAAC,CAAA;AAAA,IACR;AAAA,EACF;AACF;;;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"DecoratorBase.cjs.js","sources":["../../src/indexing/DecoratorBase.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { assertError } from '@backstage/errors';\nimport { IndexableDocument } from '@backstage/plugin-search-common';\nimport { Transform } from 'stream';\n\n/**\n * Base class encapsulating simple async transformations. Useful as a base\n * class for Backstage search decorators.\n * @public\n */\nexport abstract class DecoratorBase extends Transform {\n constructor() {\n super({ objectMode: true });\n }\n\n /**\n * Any asynchronous setup tasks can be performed here.\n */\n public abstract initialize(): Promise<void>;\n\n /**\n * Receives a single indexable document. In your decorate method, you can:\n *\n * - Resolve `undefined` to indicate the record should be omitted.\n * - Resolve a single modified document, which could contain new fields,\n * edited fields, or removed fields.\n * - Resolve an array of indexable documents, if the purpose if the decorator\n * is to convert one document into multiple derivative documents.\n */\n public abstract decorate(\n document: IndexableDocument,\n ): Promise<IndexableDocument | IndexableDocument[] | undefined>;\n\n /**\n * Any asynchronous teardown tasks can be performed here.\n */\n public abstract finalize(): Promise<void>;\n\n /**\n * Encapsulates initialization logic.\n * @internal\n */\n async _construct(done: (error?: Error | null | undefined) => void) {\n try {\n await this.initialize();\n done();\n } catch (e) {\n assertError(e);\n done(e);\n }\n }\n\n /**\n * Encapsulates simple transform stream logic.\n * @internal\n */\n async _transform(\n document: IndexableDocument,\n _: any,\n done: (error?: Error | null) => void,\n ) {\n try {\n const decorated = await this.decorate(document);\n\n // If undefined was returned, omit the record and move on.\n if (decorated === undefined) {\n done();\n return;\n }\n\n // If an array of documents was given, push them all.\n if (Array.isArray(decorated)) {\n decorated.forEach(doc => {\n this.push(doc);\n });\n done();\n return;\n }\n\n // Otherwise, just push the decorated document.\n this.push(decorated);\n done();\n } catch (e) {\n assertError(e);\n done(e);\n }\n }\n\n /**\n * Encapsulates finalization and final error handling logic.\n * @internal\n */\n async _final(done: (error?: Error | null) => void) {\n try {\n await this.finalize();\n done();\n } catch (e) {\n assertError(e);\n done(e);\n }\n }\n}\n"],"names":["Transform","assertError"],"mappings":";;;;;AAyBO,MAAe,sBAAsBA,gBAAU,CAAA;AAAA,EACpD,WAAc,GAAA;AACZ,IAAM,KAAA,CAAA,EAAE,UAAY,EAAA,IAAA,EAAM,CAAA;AAAA;AAC5B;AAAA;AAAA;AAAA;AAAA,EA6BA,MAAM,WAAW,IAAkD,EAAA;AACjE,IAAI,IAAA;AACF,MAAA,MAAM,KAAK,UAAW,EAAA;AACtB,MAAK,IAAA,EAAA;AAAA,aACE,CAAG,EAAA;AACV,MAAAC,kBAAA,CAAY,CAAC,CAAA;AACb,MAAA,IAAA,CAAK,CAAC,CAAA;AAAA;AACR;AACF;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,UAAA,CACJ,QACA,EAAA,CAAA,EACA,IACA,EAAA;AACA,IAAI,IAAA;AACF,MAAA,MAAM,SAAY,GAAA,MAAM,IAAK,CAAA,QAAA,CAAS,QAAQ,CAAA;AAG9C,MAAA,IAAI,cAAc,KAAW,CAAA,EAAA;AAC3B,QAAK,IAAA,EAAA;AACL,QAAA;AAAA;AAIF,MAAI,IAAA,KAAA,CAAM,OAAQ,CAAA,SAAS,CAAG,EAAA;AAC5B,QAAA,SAAA,CAAU,QAAQ,CAAO,GAAA,KAAA;AACvB,UAAA,IAAA,CAAK,KAAK,GAAG,CAAA;AAAA,SACd,CAAA;AACD,QAAK,IAAA,EAAA;AACL,QAAA;AAAA;AAIF,MAAA,IAAA,CAAK,KAAK,SAAS,CAAA;AACnB,MAAK,IAAA,EAAA;AAAA,aACE,CAAG,EAAA;AACV,MAAAA,kBAAA,CAAY,CAAC,CAAA;AACb,MAAA,IAAA,CAAK,CAAC,CAAA;AAAA;AACR;AACF;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,OAAO,IAAsC,EAAA;AACjD,IAAI,IAAA;AACF,MAAA,MAAM,KAAK,QAAS,EAAA;AACpB,MAAK,IAAA,EAAA;AAAA,aACE,CAAG,EAAA;AACV,MAAAA,kBAAA,CAAY,CAAC,CAAA;AACb,MAAA,IAAA,CAAK,CAAC,CAAA;AAAA;AACR;AAEJ;;;;"}
1
+ {"version":3,"file":"DecoratorBase.cjs.js","sources":["../../src/indexing/DecoratorBase.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { assertError } from '@backstage/errors';\nimport { IndexableDocument } from '@backstage/plugin-search-common';\nimport { Transform } from 'stream';\n\n/**\n * Base class encapsulating simple async transformations. Useful as a base\n * class for Backstage search decorators.\n * @public\n */\nexport abstract class DecoratorBase extends Transform {\n constructor() {\n super({ objectMode: true });\n }\n\n /**\n * Any asynchronous setup tasks can be performed here.\n */\n public abstract initialize(): Promise<void>;\n\n /**\n * Receives a single indexable document. In your decorate method, you can:\n *\n * - Resolve `undefined` to indicate the record should be omitted.\n * - Resolve a single modified document, which could contain new fields,\n * edited fields, or removed fields.\n * - Resolve an array of indexable documents, if the purpose if the decorator\n * is to convert one document into multiple derivative documents.\n */\n public abstract decorate(\n document: IndexableDocument,\n ): Promise<IndexableDocument | IndexableDocument[] | undefined>;\n\n /**\n * Any asynchronous teardown tasks can be performed here.\n */\n public abstract finalize(): Promise<void>;\n\n /**\n * Encapsulates initialization logic.\n * @internal\n */\n async _construct(done: (error?: Error | null | undefined) => void) {\n try {\n await this.initialize();\n done();\n } catch (e) {\n assertError(e);\n done(e);\n }\n }\n\n /**\n * Encapsulates simple transform stream logic.\n * @internal\n */\n async _transform(\n document: IndexableDocument,\n _: any,\n done: (error?: Error | null) => void,\n ) {\n try {\n const decorated = await this.decorate(document);\n\n // If undefined was returned, omit the record and move on.\n if (decorated === undefined) {\n done();\n return;\n }\n\n // If an array of documents was given, push them all.\n if (Array.isArray(decorated)) {\n decorated.forEach(doc => {\n this.push(doc);\n });\n done();\n return;\n }\n\n // Otherwise, just push the decorated document.\n this.push(decorated);\n done();\n } catch (e) {\n assertError(e);\n done(e);\n }\n }\n\n /**\n * Encapsulates finalization and final error handling logic.\n * @internal\n */\n async _final(done: (error?: Error | null) => void) {\n try {\n await this.finalize();\n done();\n } catch (e) {\n assertError(e);\n done(e);\n }\n }\n}\n"],"names":["Transform","assertError"],"mappings":";;;;;AAyBO,MAAe,sBAAsBA,gBAAA,CAAU;AAAA,EACpD,WAAA,GAAc;AACZ,IAAA,KAAA,CAAM,EAAE,UAAA,EAAY,IAAA,EAAM,CAAA;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA;AAAA,EA6BA,MAAM,WAAW,IAAA,EAAkD;AACjE,IAAA,IAAI;AACF,MAAA,MAAM,KAAK,UAAA,EAAW;AACtB,MAAA,IAAA,EAAK;AAAA,IACP,SAAS,CAAA,EAAG;AACV,MAAAC,kBAAA,CAAY,CAAC,CAAA;AACb,MAAA,IAAA,CAAK,CAAC,CAAA;AAAA,IACR;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,UAAA,CACJ,QAAA,EACA,CAAA,EACA,IAAA,EACA;AACA,IAAA,IAAI;AACF,MAAA,MAAM,SAAA,GAAY,MAAM,IAAA,CAAK,QAAA,CAAS,QAAQ,CAAA;AAG9C,MAAA,IAAI,cAAc,KAAA,CAAA,EAAW;AAC3B,QAAA,IAAA,EAAK;AACL,QAAA;AAAA,MACF;AAGA,MAAA,IAAI,KAAA,CAAM,OAAA,CAAQ,SAAS,CAAA,EAAG;AAC5B,QAAA,SAAA,CAAU,QAAQ,CAAA,GAAA,KAAO;AACvB,UAAA,IAAA,CAAK,KAAK,GAAG,CAAA;AAAA,QACf,CAAC,CAAA;AACD,QAAA,IAAA,EAAK;AACL,QAAA;AAAA,MACF;AAGA,MAAA,IAAA,CAAK,KAAK,SAAS,CAAA;AACnB,MAAA,IAAA,EAAK;AAAA,IACP,SAAS,CAAA,EAAG;AACV,MAAAA,kBAAA,CAAY,CAAC,CAAA;AACb,MAAA,IAAA,CAAK,CAAC,CAAA;AAAA,IACR;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,OAAO,IAAA,EAAsC;AACjD,IAAA,IAAI;AACF,MAAA,MAAM,KAAK,QAAA,EAAS;AACpB,MAAA,IAAA,EAAK;AAAA,IACP,SAAS,CAAA,EAAG;AACV,MAAAA,kBAAA,CAAY,CAAC,CAAA;AACb,MAAA,IAAA,CAAK,CAAC,CAAA;AAAA,IACR;AAAA,EACF;AACF;;;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"TestPipeline.cjs.js","sources":["../../src/test-utils/TestPipeline.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { IndexableDocument } from '@backstage/plugin-search-common';\nimport { pipeline, Readable, Transform, Writable } from 'stream';\n\n/**\n * Object resolved after a test pipeline is executed.\n * @public\n */\nexport type TestPipelineResult = {\n /**\n * If an error was emitted by the pipeline, it will be set here.\n */\n error: unknown;\n\n /**\n * A list of documents collected at the end of the pipeline. If the subject\n * under test is an indexer, this will be an empty array (because your\n * indexer should have received the documents instead).\n */\n documents: IndexableDocument[];\n};\n\n/**\n * Test utility for Backstage Search collators, decorators, and indexers.\n *\n * @example\n * An example test checking that a collator provides expected documents.\n * ```\n * it('provides expected documents', async () => {\n * const testSubject = await yourCollatorFactory.getCollator();\n * const pipeline = TestPipeline.fromCollator(testSubject);\n *\n * const { documents } = await pipeline.execute();\n *\n * expect(documents).toHaveLength(2);\n * })\n * ```\n *\n * @example\n * An example test checking that a decorator behaves as expected.\n * ```\n * it('filters private documents', async () => {\n * const testSubject = await yourDecoratorFactory.getDecorator();\n * const pipeline = TestPipeline\n * .fromDecorator(testSubject)\n * .withDocuments([{ title: 'Private', location: '/private', text: '' }]);\n *\n * const { documents } = await pipeline.execute();\n *\n * expect(documents).toHaveLength(0);\n * })\n * ```\n *\n * @public\n */\nexport class TestPipeline {\n private collator?: Readable;\n private decorator?: Transform;\n private indexer?: Writable;\n\n private constructor({\n collator,\n decorator,\n indexer,\n }: {\n collator?: Readable;\n decorator?: Transform;\n indexer?: Writable;\n }) {\n this.collator = collator;\n this.decorator = decorator;\n this.indexer = indexer;\n }\n\n /**\n * Provide the collator, decorator, or indexer to be tested.\n *\n * @deprecated Use `fromCollator`, `fromDecorator` or `fromIndexer` static\n * methods to create a test pipeline instead.\n */\n static withSubject(subject: Readable | Transform | Writable) {\n if (subject instanceof Transform) {\n return new TestPipeline({ decorator: subject });\n }\n\n if (subject instanceof Writable) {\n return new TestPipeline({ indexer: subject });\n }\n\n if (subject.readable || subject instanceof Readable) {\n return new TestPipeline({ collator: subject });\n }\n\n throw new Error(\n 'Unknown test subject: are you passing a readable, writable, or transform stream?',\n );\n }\n\n /**\n * Create a test pipeline given a collator you want to test.\n */\n static fromCollator(collator: Readable) {\n return new TestPipeline({ collator });\n }\n\n /**\n * Add a collator to the test pipeline.\n */\n withCollator(collator: Readable): this {\n this.collator = collator;\n return this;\n }\n\n /**\n * Create a test pipeline given a decorator you want to test.\n */\n static fromDecorator(decorator: Transform) {\n return new TestPipeline({ decorator });\n }\n\n /**\n * Add a decorator to the test pipeline.\n */\n withDecorator(decorator: Transform): this {\n this.decorator = decorator;\n return this;\n }\n\n /**\n * Create a test pipeline given an indexer you want to test.\n */\n static fromIndexer(indexer: Writable) {\n return new TestPipeline({ indexer });\n }\n\n /**\n * Add an indexer to the test pipeline.\n */\n withIndexer(indexer: Writable): this {\n this.indexer = indexer;\n return this;\n }\n\n /**\n * Provide documents for testing decorators and indexers.\n */\n withDocuments(documents: IndexableDocument[]): TestPipeline {\n if (this.collator) {\n throw new Error('Cannot provide documents when testing a collator.');\n }\n\n // Set a naive readable stream that just pushes all given documents.\n this.collator = new Readable({ objectMode: true });\n this.collator._read = () => {};\n process.nextTick(() => {\n documents.forEach(document => {\n this.collator!.push(document);\n });\n this.collator!.push(null);\n });\n\n return this;\n }\n\n /**\n * Execute the test pipeline so that you can make assertions about the result\n * or behavior of the given test subject.\n */\n async execute(): Promise<TestPipelineResult> {\n const documents: IndexableDocument[] = [];\n if (!this.collator) {\n throw new Error(\n 'Cannot execute pipeline without a collator or documents',\n );\n }\n\n // If we are here and there is no indexer, we are testing a collator or a\n // decorator. Set up a naive writable that captures documents in memory.\n if (!this.indexer) {\n this.indexer = new Writable({ objectMode: true });\n this.indexer._write = (document: IndexableDocument, _, done) => {\n documents.push(document);\n done();\n };\n }\n\n return new Promise<TestPipelineResult>(done => {\n const pipes: (Readable | Transform | Writable)[] = [this.collator!];\n if (this.decorator) {\n pipes.push(this.decorator);\n }\n pipes.push(this.indexer!);\n\n pipeline(pipes, (error: NodeJS.ErrnoException | null) => {\n done({\n error,\n documents,\n });\n });\n });\n }\n}\n"],"names":["Transform","Writable","Readable","pipeline"],"mappings":";;;;AAsEO,MAAM,YAAa,CAAA;AAAA,EAChB,QAAA;AAAA,EACA,SAAA;AAAA,EACA,OAAA;AAAA,EAEA,WAAY,CAAA;AAAA,IAClB,QAAA;AAAA,IACA,SAAA;AAAA,IACA;AAAA,GAKC,EAAA;AACD,IAAA,IAAA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,QAAS,CAAA;AAClE,MAAA,IAAI,KAAK,SAAW,EAAA;AAClB,QAAM,KAAA,CAAA,IAAA,CAAK,KAAK,SAAS,CAAA;AAAA;AAE3B,MAAM,KAAA,CAAA,IAAA,CAAK,KAAK,OAAQ,CAAA;AAExB,MAASE,eAAA,CAAA,KAAA,EAAO,CAAC,KAAwC,KAAA;AACvD,QAAK,IAAA,CAAA;AAAA,UACH,KAAA;AAAA,UACA;AAAA,SACD,CAAA;AAAA,OACF,CAAA;AAAA,KACF,CAAA;AAAA;AAEL;;;;"}
1
+ {"version":3,"file":"TestPipeline.cjs.js","sources":["../../src/test-utils/TestPipeline.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { IndexableDocument } from '@backstage/plugin-search-common';\nimport { pipeline, Readable, Transform, Writable } from 'stream';\n\n/**\n * Object resolved after a test pipeline is executed.\n * @public\n */\nexport type TestPipelineResult = {\n /**\n * If an error was emitted by the pipeline, it will be set here.\n */\n error: unknown;\n\n /**\n * A list of documents collected at the end of the pipeline. If the subject\n * under test is an indexer, this will be an empty array (because your\n * indexer should have received the documents instead).\n */\n documents: IndexableDocument[];\n};\n\n/**\n * Test utility for Backstage Search collators, decorators, and indexers.\n *\n * @example\n * An example test checking that a collator provides expected documents.\n * ```\n * it('provides expected documents', async () => {\n * const testSubject = await yourCollatorFactory.getCollator();\n * const pipeline = TestPipeline.fromCollator(testSubject);\n *\n * const { documents } = await pipeline.execute();\n *\n * expect(documents).toHaveLength(2);\n * })\n * ```\n *\n * @example\n * An example test checking that a decorator behaves as expected.\n * ```\n * it('filters private documents', async () => {\n * const testSubject = await yourDecoratorFactory.getDecorator();\n * const pipeline = TestPipeline\n * .fromDecorator(testSubject)\n * .withDocuments([{ title: 'Private', location: '/private', text: '' }]);\n *\n * const { documents } = await pipeline.execute();\n *\n * expect(documents).toHaveLength(0);\n * })\n * ```\n *\n * @public\n */\nexport class TestPipeline {\n private collator?: Readable;\n private decorator?: Transform;\n private indexer?: Writable;\n\n private constructor({\n collator,\n decorator,\n indexer,\n }: {\n collator?: Readable;\n decorator?: Transform;\n indexer?: Writable;\n }) {\n this.collator = collator;\n this.decorator = decorator;\n this.indexer = indexer;\n }\n\n /**\n * Provide the collator, decorator, or indexer to be tested.\n *\n * @deprecated Use `fromCollator`, `fromDecorator` or `fromIndexer` static\n * methods to create a test pipeline instead.\n */\n static withSubject(subject: Readable | Transform | Writable) {\n if (subject instanceof Transform) {\n return new TestPipeline({ decorator: subject });\n }\n\n if (subject instanceof Writable) {\n return new TestPipeline({ indexer: subject });\n }\n\n if (subject.readable || subject instanceof Readable) {\n return new TestPipeline({ collator: subject });\n }\n\n throw new Error(\n 'Unknown test subject: are you passing a readable, writable, or transform stream?',\n );\n }\n\n /**\n * Create a test pipeline given a collator you want to test.\n */\n static fromCollator(collator: Readable) {\n return new TestPipeline({ collator });\n }\n\n /**\n * Add a collator to the test pipeline.\n */\n withCollator(collator: Readable): this {\n this.collator = collator;\n return this;\n }\n\n /**\n * Create a test pipeline given a decorator you want to test.\n */\n static fromDecorator(decorator: Transform) {\n return new TestPipeline({ decorator });\n }\n\n /**\n * Add a decorator to the test pipeline.\n */\n withDecorator(decorator: Transform): this {\n this.decorator = decorator;\n return this;\n }\n\n /**\n * Create a test pipeline given an indexer you want to test.\n */\n static fromIndexer(indexer: Writable) {\n return new TestPipeline({ indexer });\n }\n\n /**\n * Add an indexer to the test pipeline.\n */\n withIndexer(indexer: Writable): this {\n this.indexer = indexer;\n return this;\n }\n\n /**\n * Provide documents for testing decorators and indexers.\n */\n withDocuments(documents: IndexableDocument[]): TestPipeline {\n if (this.collator) {\n throw new Error('Cannot provide documents when testing a collator.');\n }\n\n // Set a naive readable stream that just pushes all given documents.\n this.collator = new Readable({ objectMode: true });\n this.collator._read = () => {};\n process.nextTick(() => {\n documents.forEach(document => {\n this.collator!.push(document);\n });\n this.collator!.push(null);\n });\n\n return this;\n }\n\n /**\n * Execute the test pipeline so that you can make assertions about the result\n * or behavior of the given test subject.\n */\n async execute(): Promise<TestPipelineResult> {\n const documents: IndexableDocument[] = [];\n if (!this.collator) {\n throw new Error(\n 'Cannot execute pipeline without a collator or documents',\n );\n }\n\n // If we are here and there is no indexer, we are testing a collator or a\n // decorator. Set up a naive writable that captures documents in memory.\n if (!this.indexer) {\n this.indexer = new Writable({ objectMode: true });\n this.indexer._write = (document: IndexableDocument, _, done) => {\n documents.push(document);\n done();\n };\n }\n\n return new Promise<TestPipelineResult>(done => {\n const pipes: (Readable | Transform | Writable)[] = [this.collator!];\n if (this.decorator) {\n pipes.push(this.decorator);\n }\n pipes.push(this.indexer!);\n\n pipeline(pipes, (error: NodeJS.ErrnoException | null) => {\n done({\n error,\n documents,\n });\n });\n });\n }\n}\n"],"names":["Transform","Writable","Readable","pipeline"],"mappings":";;;;AAsEO,MAAM,YAAA,CAAa;AAAA,EAChB,QAAA;AAAA,EACA,SAAA;AAAA,EACA,OAAA;AAAA,EAEA,WAAA,CAAY;AAAA,IAClB,QAAA;AAAA,IACA,SAAA;AAAA,IACA;AAAA,GACF,EAIG;AACD,IAAA,IAAA,CAAK,QAAA,GAAW,QAAA;AAChB,IAAA,IAAA,CAAK,SAAA,GAAY,SAAA;AACjB,IAAA,IAAA,CAAK,OAAA,GAAU,OAAA;AAAA,EACjB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,OAAO,YAAY,OAAA,EAA0C;AAC3D,IAAA,IAAI,mBAAmBA,gBAAA,EAAW;AAChC,MAAA,OAAO,IAAI,YAAA,CAAa,EAAE,SAAA,EAAW,SAAS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AAA,CAAS,KAAA,EAAO,CAAC,KAAA,KAAwC;AACvD,QAAA,IAAA,CAAK;AAAA,UACH,KAAA;AAAA,UACA;AAAA,SACD,CAAA;AAAA,MACH,CAAC,CAAA;AAAA,IACH,CAAC,CAAA;AAAA,EACH;AACF;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backstage/plugin-search-backend-node",
3
- "version": "1.3.14-next.0",
3
+ "version": "1.3.15-next.0",
4
4
  "description": "A library for Backstage backend plugins that want to interact with the search backend plugin",
5
5
  "backstage": {
6
6
  "role": "node-library",
@@ -61,7 +61,7 @@
61
61
  "test": "backstage-cli package test"
62
62
  },
63
63
  "dependencies": {
64
- "@backstage/backend-plugin-api": "1.4.2-next.0",
64
+ "@backstage/backend-plugin-api": "1.4.3-next.0",
65
65
  "@backstage/config": "1.3.3",
66
66
  "@backstage/errors": "1.2.7",
67
67
  "@backstage/plugin-permission-common": "0.9.1",
@@ -73,9 +73,9 @@
73
73
  "uuid": "^11.0.0"
74
74
  },
75
75
  "devDependencies": {
76
- "@backstage/backend-defaults": "0.11.2-next.0",
77
- "@backstage/backend-test-utils": "1.7.1-next.0",
78
- "@backstage/cli": "0.33.2-next.0",
76
+ "@backstage/backend-defaults": "0.12.1-next.0",
77
+ "@backstage/backend-test-utils": "1.9.0-next.1",
78
+ "@backstage/cli": "0.34.2-next.1",
79
79
  "@types/ndjson": "^2.0.1"
80
80
  }
81
81
  }