1
0

resolver-uds.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright 2019 gRPC authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import { Resolver, ResolverListener, registerResolver } from './resolver';
  17. import { Endpoint } from './subchannel-address';
  18. import { GrpcUri } from './uri-parser';
  19. import { ChannelOptions } from './channel-options';
  20. class UdsResolver implements Resolver {
  21. private hasReturnedResult = false;
  22. private endpoints: Endpoint[] = [];
  23. constructor(
  24. target: GrpcUri,
  25. private listener: ResolverListener,
  26. channelOptions: ChannelOptions
  27. ) {
  28. let path: string;
  29. if (target.authority === '') {
  30. path = '/' + target.path;
  31. } else {
  32. path = target.path;
  33. }
  34. this.endpoints = [{ addresses: [{ path }] }];
  35. }
  36. updateResolution(): void {
  37. if (!this.hasReturnedResult) {
  38. this.hasReturnedResult = true;
  39. process.nextTick(
  40. this.listener.onSuccessfulResolution,
  41. this.endpoints,
  42. null,
  43. null,
  44. null,
  45. {}
  46. );
  47. }
  48. }
  49. destroy() {
  50. this.hasReturnedResult = false;
  51. }
  52. static getDefaultAuthority(target: GrpcUri): string {
  53. return 'localhost';
  54. }
  55. }
  56. export function setup() {
  57. registerResolver('unix', UdsResolver);
  58. }