|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | + |
| 7 | +import 'package:gcloud/service_scope.dart'; |
| 8 | +import 'package:logging/logging.dart'; |
| 9 | +import 'package:pub_dev/service/entrypoint/logging.dart'; |
| 10 | +import 'package:pub_dev/service/services.dart'; |
| 11 | +import 'package:pub_dev/shared/env_config.dart'; |
| 12 | +import 'package:pub_dev/shared/logging.dart'; |
| 13 | + |
| 14 | +import '../../../service/entrypoint/_isolate.dart'; |
| 15 | + |
| 16 | +import '../../search/sdk_mem_index.dart'; |
| 17 | +import '../../search/search_service.dart'; |
| 18 | + |
| 19 | +final _logger = Logger('sdk_search_index'); |
| 20 | + |
| 21 | +/// Entry point for the SDK search index isolate. |
| 22 | +Future<void> main(List<String> args, var message) async { |
| 23 | + final timer = Timer.periodic(Duration(milliseconds: 250), (_) {}); |
| 24 | + |
| 25 | + final ServicesWrapperFn servicesWrapperFn; |
| 26 | + if (envConfig.isRunningInAppengine) { |
| 27 | + servicesWrapperFn = withServices; |
| 28 | + setupAppEngineLogging(); |
| 29 | + } else { |
| 30 | + servicesWrapperFn = (fn) => withFakeServices(fn: fn); |
| 31 | + setupDebugEnvBasedLogging(); |
| 32 | + } |
| 33 | + |
| 34 | + await fork(() async { |
| 35 | + await servicesWrapperFn(() async { |
| 36 | + final sdkMemIndex = await createSdkMemIndex(); |
| 37 | + await runIsolateFunctions( |
| 38 | + message: message, |
| 39 | + logger: _logger, |
| 40 | + fn: (payload) async { |
| 41 | + final args = payload as List; |
| 42 | + final rs = sdkMemIndex!.search( |
| 43 | + args[0] as String, |
| 44 | + limit: args[1] as int?, |
| 45 | + skipFlutter: args[2] as bool, |
| 46 | + ); |
| 47 | + return ReplyMessage.result(rs.map((e) => e.toJson()).toList()); |
| 48 | + }); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + timer.cancel(); |
| 53 | +} |
| 54 | + |
| 55 | +/// Implementation of [SdkMemIndex] that uses [RequestMessage]s to send requests |
| 56 | +/// across isolate boundaries. The instance should be registered inside the |
| 57 | +/// `frontend` isolate, and it calls the `sdk-index` isolate as a delegate. |
| 58 | +class SdkIsolateIndex implements SdkIndex { |
| 59 | + final IsolateRunner _runner; |
| 60 | + SdkIsolateIndex(this._runner); |
| 61 | + |
| 62 | + @override |
| 63 | + Future<List<SdkLibraryHit>> search( |
| 64 | + String query, { |
| 65 | + int? limit, |
| 66 | + bool skipFlutter = false, |
| 67 | + }) async { |
| 68 | + try { |
| 69 | + final rs = await _runner.sendRequest( |
| 70 | + [query, limit, skipFlutter], |
| 71 | + timeout: Duration(seconds: 2), |
| 72 | + ); |
| 73 | + return (rs as List) |
| 74 | + .map((v) => SdkLibraryHit.fromJson(v as Map<String, dynamic>)) |
| 75 | + .toList(); |
| 76 | + } catch (e, st) { |
| 77 | + _logger.warning('Failed to search SDK index.', e, st); |
| 78 | + return <SdkLibraryHit>[]; |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments