lemon-core 4.3.0 → 5.0.1

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.
Files changed (167) hide show
  1. package/README.md +127 -2
  2. package/dist/common/lemon-error.d.ts +34 -0
  3. package/dist/common/lemon-error.js +52 -0
  4. package/dist/common/lemon-error.js.map +1 -0
  5. package/dist/controllers/general-api-controller.d.ts +7 -0
  6. package/dist/controllers/general-api-controller.js +7 -0
  7. package/dist/controllers/general-api-controller.js.map +1 -1
  8. package/dist/cores/api/api-service.js +10 -43
  9. package/dist/cores/api/api-service.js.map +1 -1
  10. package/dist/cores/aws/aws-kms-service.d.ts +13 -5
  11. package/dist/cores/aws/aws-kms-service.js +23 -9
  12. package/dist/cores/aws/aws-kms-service.js.map +1 -1
  13. package/dist/cores/aws/aws-s3-service.d.ts +30 -4
  14. package/dist/cores/aws/aws-s3-service.js +35 -15
  15. package/dist/cores/aws/aws-s3-service.js.map +1 -1
  16. package/dist/cores/aws/aws-sns-service.js +59 -9
  17. package/dist/cores/aws/aws-sns-service.js.map +1 -1
  18. package/dist/cores/aws/aws-sqs-service.d.ts +1 -1
  19. package/dist/cores/aws/aws-sqs-service.js +54 -12
  20. package/dist/cores/aws/aws-sqs-service.js.map +1 -1
  21. package/dist/cores/cache/cache-service.d.ts +10 -9
  22. package/dist/cores/cache/cache-service.js +9 -346
  23. package/dist/cores/cache/cache-service.js.map +1 -1
  24. package/dist/cores/config/config-service.d.ts +3 -15
  25. package/dist/cores/config/config-service.js +5 -34
  26. package/dist/cores/config/config-service.js.map +1 -1
  27. package/dist/cores/dynamo/dynamo-query-service.js +42 -2
  28. package/dist/cores/dynamo/dynamo-query-service.js.map +1 -1
  29. package/dist/cores/dynamo/dynamo-scan-service.js +42 -2
  30. package/dist/cores/dynamo/dynamo-scan-service.js.map +1 -1
  31. package/dist/cores/dynamo/dynamo-service.d.ts +45 -14
  32. package/dist/cores/dynamo/dynamo-service.js +48 -21
  33. package/dist/cores/dynamo/dynamo-service.js.map +1 -1
  34. package/dist/cores/dynamo/tools/expressions.js +32 -22
  35. package/dist/cores/dynamo/tools/expressions.js.map +1 -1
  36. package/dist/cores/dynamo/tools/lang.d.ts +30 -0
  37. package/dist/cores/dynamo/tools/lang.js +75 -0
  38. package/dist/cores/dynamo/tools/lang.js.map +1 -0
  39. package/dist/cores/dynamo/tools/query.js +20 -21
  40. package/dist/cores/dynamo/tools/query.js.map +1 -1
  41. package/dist/cores/dynamo/tools/scan.js +15 -16
  42. package/dist/cores/dynamo/tools/scan.js.map +1 -1
  43. package/dist/cores/dynamo/tools/serializer.js +40 -38
  44. package/dist/cores/dynamo/tools/serializer.js.map +1 -1
  45. package/dist/cores/dynamo/tools/utils.d.ts +6 -2
  46. package/dist/cores/dynamo/tools/utils.js +11 -8
  47. package/dist/cores/dynamo/tools/utils.js.map +1 -1
  48. package/dist/cores/elastic/elastic6-query-service.d.ts +1 -104
  49. package/dist/cores/elastic/elastic6-query-service.js +15 -489
  50. package/dist/cores/elastic/elastic6-query-service.js.map +1 -1
  51. package/dist/cores/elastic/elastic6-service.d.ts +1 -482
  52. package/dist/cores/elastic/elastic6-service.js +15 -1153
  53. package/dist/cores/elastic/elastic6-service.js.map +1 -1
  54. package/dist/cores/elastic/index.d.ts +2 -2
  55. package/dist/cores/elastic/index.js +3 -2
  56. package/dist/cores/elastic/index.js.map +1 -1
  57. package/dist/cores/elastic/os2-query-service.d.ts +104 -0
  58. package/dist/cores/elastic/os2-query-service.js +495 -0
  59. package/dist/cores/elastic/os2-query-service.js.map +1 -0
  60. package/dist/cores/elastic/os2-service.d.ts +372 -0
  61. package/dist/cores/elastic/os2-service.js +943 -0
  62. package/dist/cores/elastic/os2-service.js.map +1 -0
  63. package/dist/cores/index.d.ts +6 -1
  64. package/dist/cores/index.js +9 -2
  65. package/dist/cores/index.js.map +1 -1
  66. package/dist/cores/lambda/index.js +2 -1
  67. package/dist/cores/lambda/index.js.map +1 -1
  68. package/dist/cores/lambda/lambda-cron-handler.d.ts +1 -0
  69. package/dist/cores/lambda/lambda-cron-handler.js +5 -1
  70. package/dist/cores/lambda/lambda-cron-handler.js.map +1 -1
  71. package/dist/cores/lambda/lambda-dynamo-stream-handler.d.ts +5 -4
  72. package/dist/cores/lambda/lambda-dynamo-stream-handler.js +18 -5
  73. package/dist/cores/lambda/lambda-dynamo-stream-handler.js.map +1 -1
  74. package/dist/cores/lambda/lambda-handler.d.ts +22 -2
  75. package/dist/cores/lambda/lambda-handler.js +85 -23
  76. package/dist/cores/lambda/lambda-handler.js.map +1 -1
  77. package/dist/cores/lambda/lambda-sns-handler.d.ts +1 -0
  78. package/dist/cores/lambda/lambda-sns-handler.js +5 -0
  79. package/dist/cores/lambda/lambda-sns-handler.js.map +1 -1
  80. package/dist/cores/lambda/lambda-sqs-handler.d.ts +1 -0
  81. package/dist/cores/lambda/lambda-sqs-handler.js +20 -5
  82. package/dist/cores/lambda/lambda-sqs-handler.js.map +1 -1
  83. package/dist/cores/lambda/lambda-web-handler.d.ts +10 -0
  84. package/dist/cores/lambda/lambda-web-handler.js +107 -41
  85. package/dist/cores/lambda/lambda-web-handler.js.map +1 -1
  86. package/dist/cores/protocol/aws-lambda-helper.js +58 -11
  87. package/dist/cores/protocol/aws-lambda-helper.js.map +1 -1
  88. package/dist/cores/protocol/index.js +3 -0
  89. package/dist/cores/protocol/index.js.map +1 -1
  90. package/dist/cores/protocol/protocol-service.d.ts +26 -5
  91. package/dist/cores/protocol/protocol-service.js +42 -12
  92. package/dist/cores/protocol/protocol-service.js.map +1 -1
  93. package/dist/cores/protocol/protocol-sign.d.ts +72 -0
  94. package/dist/cores/protocol/protocol-sign.js +194 -0
  95. package/dist/cores/protocol/protocol-sign.js.map +1 -0
  96. package/dist/cores/storage/index.d.ts +0 -1
  97. package/dist/cores/storage/index.js +0 -1
  98. package/dist/cores/storage/index.js.map +1 -1
  99. package/dist/cores/storage/model-fields.d.ts +7 -0
  100. package/dist/cores/storage/model-fields.js +21 -0
  101. package/dist/cores/storage/model-fields.js.map +1 -0
  102. package/dist/cores/storage/model-manager.d.ts +5 -2
  103. package/dist/cores/storage/model-manager.js +11 -3
  104. package/dist/cores/storage/model-manager.js.map +1 -1
  105. package/dist/engine/builder.js +20 -2
  106. package/dist/engine/builder.js.map +1 -1
  107. package/dist/engine/engine.d.ts +2 -2
  108. package/dist/engine/engine.js +5 -5
  109. package/dist/engine/engine.js.map +1 -1
  110. package/dist/engine/utilities.d.ts +20 -12
  111. package/dist/engine/utilities.js +57 -86
  112. package/dist/engine/utilities.js.map +1 -1
  113. package/dist/environ.d.ts +0 -6
  114. package/dist/environ.js +1 -12
  115. package/dist/environ.js.map +1 -1
  116. package/dist/esm/index.mjs +141 -0
  117. package/dist/esm/testing.mjs +12 -0
  118. package/dist/extended/abstract-service.d.ts +48 -139
  119. package/dist/extended/abstract-service.js +46 -382
  120. package/dist/extended/abstract-service.js.map +1 -1
  121. package/dist/extended/cores/abstract-controllers.d.ts +25 -0
  122. package/dist/extended/cores/abstract-controllers.js +77 -7
  123. package/dist/extended/cores/abstract-controllers.js.map +1 -1
  124. package/dist/extended/cores/abstract-rest-apis.d.ts +2 -1
  125. package/dist/extended/cores/abstract-rest-apis.js +2 -1
  126. package/dist/extended/cores/abstract-rest-apis.js.map +1 -1
  127. package/dist/extended/cores/abstract-services.d.ts +48 -87
  128. package/dist/extended/cores/abstract-services.js +121 -248
  129. package/dist/extended/cores/abstract-services.js.map +1 -1
  130. package/dist/extended/cores/commons.d.ts +4 -113
  131. package/dist/extended/cores/commons.js +26 -180
  132. package/dist/extended/cores/commons.js.map +1 -1
  133. package/dist/extended/cores/fields-admin.d.ts +91 -0
  134. package/dist/extended/cores/fields-admin.js +142 -0
  135. package/dist/extended/cores/fields-admin.js.map +1 -0
  136. package/dist/extended/cores/index.d.ts +2 -0
  137. package/dist/extended/cores/index.js +2 -0
  138. package/dist/extended/cores/index.js.map +1 -1
  139. package/dist/extended/cores/types.d.ts +3 -225
  140. package/dist/extended/cores/types.js +28 -0
  141. package/dist/extended/cores/types.js.map +1 -1
  142. package/dist/helpers/helpers.d.ts +10 -5
  143. package/dist/helpers/helpers.js +46 -58
  144. package/dist/helpers/helpers.js.map +1 -1
  145. package/dist/helpers/types.d.ts +22 -10
  146. package/dist/index.d.ts +1 -5
  147. package/dist/index.js +8 -14
  148. package/dist/index.js.map +1 -1
  149. package/dist/lib/http-fetch.d.ts +54 -0
  150. package/dist/lib/http-fetch.js +133 -0
  151. package/dist/lib/http-fetch.js.map +1 -0
  152. package/dist/lib/lazy.d.ts +22 -0
  153. package/dist/lib/lazy.js +54 -0
  154. package/dist/lib/lazy.js.map +1 -0
  155. package/dist/lib/perf.d.ts +35 -0
  156. package/dist/lib/perf.js +52 -0
  157. package/dist/lib/perf.js.map +1 -0
  158. package/dist/tools/tools.d.ts +15 -3
  159. package/dist/tools/tools.js +21 -4
  160. package/dist/tools/tools.js.map +1 -1
  161. package/package.json +96 -39
  162. package/dist/cores/storage/redis-storage-service.d.ts +0 -183
  163. package/dist/cores/storage/redis-storage-service.js +0 -377
  164. package/dist/cores/storage/redis-storage-service.js.map +0 -1
  165. package/dist/generated/field-registry.d.ts +0 -12
  166. package/dist/generated/field-registry.js +0 -21
  167. package/dist/generated/field-registry.js.map +0 -1
@@ -32,11 +32,8 @@ var __importStar = (this && this.__importStar) || (function () {
32
32
  return result;
33
33
  };
34
34
  })();
35
- var __importDefault = (this && this.__importDefault) || function (mod) {
36
- return (mod && mod.__esModule) ? mod : { "default": mod };
37
- };
38
35
  Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.$ES6 = exports._ES6 = exports.Elastic6Instance = exports.AbstractElastic6Instance = exports.Elastic6Synchronizer = exports.AbstractProxy = exports.ManagerProxy = exports.CoreManager = exports.CoreService = exports.filterFields = void 0;
36
+ exports.AbstractOS2Instance = exports.OS2Synchronizer = exports.AbstractProxy = exports.ManagerProxy = exports.CoreManager = exports.CoreService = exports.filterFields = void 0;
40
37
  exports.asIdentityId = asIdentityId;
41
38
  exports.sourceToItem = sourceToItem;
42
39
  /**
@@ -47,12 +44,14 @@ exports.sourceToItem = sourceToItem;
47
44
  * @date 2021-02-23 initial version
48
45
  * @author Steve <steve@lemoncloud.io>
49
46
  * @date 2022-02-18 optimized search w/ ES6.8
50
- * @date 2022-02-22 optimized w/ `lemon-core#3.0` and `@elastic/elasticsearch`
47
+ * @date 2022-02-22 optimized w/ `lemon-core#3.0` and the elastic client
51
48
  * @date 2022-02-24 use `$id` in elastic-search as `_id` in dynamo-table.
52
49
  * @date 2022-03-15 optimized w/ `AbstractProxy`
53
50
  * @date 2022-03-17 optimized w/ `lemon-core#3.0.2` and use `env.ES6_DOCTYPE`
54
51
  * @date 2022-03-31 optimized w/ unit test spec.
55
52
  * @date 2022-05-19 optimized `CacheService` w/ typed key.
53
+ * @date 2026-10-27 v5 B2-b: `OS2Synchronizer`/`AbstractOS2Instance`; the L1 search instance, its factory and proxy are removed.
54
+ * @date 2026-10-28 v5 B4: `CoreManager` `fields` may be a model class.
56
55
  *
57
56
  * @origin see `lemon-accounts-api/src/service/core-service.ts`
58
57
  * @copyright (C) 2021 LemonCloud Co Ltd. - All Rights Reserved.
@@ -61,9 +60,6 @@ const cores_1 = __importStar(require("../cores/"));
61
60
  const engine_1 = require("../engine/");
62
61
  const test_helper_1 = require("../common/test-helper");
63
62
  const helpers_1 = require("../helpers");
64
- const sig_v4_1 = require("./libs/sig-v4");
65
- const request_1 = __importDefault(require("request"));
66
- const query_string_1 = __importDefault(require("query-string"));
67
63
  const NS = engine_1.$U.NS('back', 'blue'); // NAMESPACE TO BE PRINTED.
68
64
  /**
69
65
  * authentication helper - get identity-id from context
@@ -143,6 +139,7 @@ class CoreManager extends cores_1.AbstractManager {
143
139
  * constructor
144
140
  * @protected
145
141
  */
142
+ //* v5 B4: `fields` may be a model class (resolved in `AbstractManager`).
146
143
  constructor(type, parent, fields, uniqueField) {
147
144
  super(type, parent, fields, uniqueField);
148
145
  }
@@ -602,23 +599,19 @@ class AbstractProxy {
602
599
  context;
603
600
  /** (internal) backend-service */
604
601
  service;
605
- /** (internal) cache service instance */
602
+ /** (internal) cache service instance (v5: no longer created from `CACHE_ENDPOINT`, so `undefined` unless a subclass sets it) */
606
603
  cache;
607
604
  /**
608
605
  * constructor of proxy.
609
606
  * @param service user service instance
610
607
  * @param parrallel parrallel count (default 2)
611
- * @param cacheScope prefix of cache-key (like `lemon:SS:` or `lemon:SS:user`)
608
+ * @param cacheScope prefix of cache-key (like `lemon:SS:` or `lemon:SS:user`). v5: kept for signature compat, unused.
612
609
  */
610
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
613
611
  constructor(context, service, parrallel = 2, cacheScope) {
614
612
  this.context = context;
615
613
  this.service = service;
616
614
  this.parrallel = parrallel;
617
- // create cache
618
- const endpoint = engine_1.$U.env('CACHE_ENDPOINT', '');
619
- if (cacheScope && endpoint.startsWith('redis:')) {
620
- this.cache = cores_1.CacheService.create({ type: 'redis', endpoint, ns: cacheScope });
621
- }
622
615
  }
623
616
  /**
624
617
  * say hello().
@@ -782,24 +775,13 @@ class AbstractProxy {
782
775
  throw new Error(`400 NOT ALLOWED - getCurrentIdentity(${identityId || ''})`);
783
776
  return identityId;
784
777
  }
785
- /**
786
- * get the current identity object (or throw access-error)
787
- *
788
- * @deprecated useless anymore since 3.2.10
789
- */
790
- async getCurrentIdentity$(throwable = true) {
791
- const identityId = await this.getCurrentIdentityId(throwable);
792
- if (!identityId && !throwable)
793
- return null;
794
- return this.getIdentity$(identityId);
795
- }
796
778
  }
797
779
  exports.AbstractProxy = AbstractProxy;
798
780
  /**
799
- * class `Elastic6Synchronizer`
800
- * - listen DynamoDBStream events and index into Elasticsearch
781
+ * class `OS2Synchronizer`
782
+ * - listen DynamoDBStream events and index into OpenSearch
801
783
  */
802
- class Elastic6Synchronizer {
784
+ class OS2Synchronizer {
803
785
  /**
804
786
  * model synchronizer map
805
787
  * @private
@@ -811,8 +793,8 @@ class Elastic6Synchronizer {
811
793
  */
812
794
  defModelSynchronizer;
813
795
  /**
814
- * constructor
815
- * @param elastic (optional) elastic6-service instance
796
+ * constructor (registers one dynamo-stream listener)
797
+ * @param elastic (optional) OS2CoreService instance
816
798
  * @param dynamoOptions (optional) dynamo config options
817
799
  */
818
800
  constructor(elastic, dynamoOptions) {
@@ -825,7 +807,7 @@ class Elastic6Synchronizer {
825
807
  idName: dynamoOptions?.idName || '_id', // id (global) of dynamo-table. should be `_id`.
826
808
  });
827
809
  //* create sync-handler w/ this.
828
- const listener = cores_1.LambdaDynamoStreamHandler.createSyncToElastic6(options, elastic, this.filter.bind(this), this.onBeforeSync.bind(this), this.onAfterSync.bind(this));
810
+ const listener = cores_1.LambdaDynamoStreamHandler.createSyncToOS2(options, elastic, this.filter.bind(this), this.onBeforeSync.bind(this), this.onAfterSync.bind(this));
829
811
  cores_1.default.lambda.dynamos.addListener(listener); // register DynamoStream event listener
830
812
  //* prepare default synchro
831
813
  this.synchronizerMap = new Map();
@@ -874,51 +856,38 @@ class Elastic6Synchronizer {
874
856
  await handler.onAfterSync?.(id, eventName, item, diff, prev);
875
857
  };
876
858
  }
877
- exports.Elastic6Synchronizer = Elastic6Synchronizer;
859
+ exports.OS2Synchronizer = OS2Synchronizer;
878
860
  /**
879
- * class `AbstractElastic6Instance`
880
- * - to manipulate the shared Elasticsearch resources.
861
+ * class `AbstractOS2Instance`
862
+ * - to manipulate the shared OpenSearch resources.
863
+ * - the constructor builds nothing; the subclass (`OS2Instance`) builds one service set.
881
864
  */
882
- class AbstractElastic6Instance {
883
- /**
884
- * Elasticsearch client
885
- */
886
- client;
865
+ class AbstractOS2Instance {
887
866
  /**
888
- * Elastic6Service instance
867
+ * OS2CoreService instance
889
868
  */
890
869
  elastic;
891
870
  /**
892
- * Elastic6QueryService instance
871
+ * OS2QueryService instance
893
872
  */
894
873
  query;
895
874
  /**
896
- * Elastic6Synchronizer instance
875
+ * OS2Synchronizer instance
897
876
  */
898
877
  synchronizer;
899
878
  /**
900
- * default constructor
901
- */
902
- constructor({ endpoint, indexName, esVersion, esDocType, tableName, autocompleteFields, }) {
903
- // initialize Elasticsearch only if there are valid endpoint & index
904
- if (endpoint && indexName) {
905
- const options = {
906
- endpoint,
907
- indexName,
908
- version: esVersion,
909
- autocompleteFields,
910
- };
911
- if (esDocType)
912
- options.docType = esDocType;
913
- this.elastic = new cores_1.Elastic6Service(options);
914
- this.client = this.elastic.client;
915
- this.query = new cores_1.Elastic6QueryService(this.elastic);
916
- }
917
- // WARN `this.elastic` can be null!
918
- this.synchronizer = new Elastic6Synchronizer(this.elastic, { tableName });
879
+ * default constructor (empty: no service, no client, no listener)
880
+ */
881
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
882
+ constructor() { }
883
+ /**
884
+ * the OpenSearch client of `elastic` (created on first access).
885
+ */
886
+ get client() {
887
+ return this.elastic?.client;
919
888
  }
920
889
  /**
921
- * read the current elastic6-option.
890
+ * read the current option.
922
891
  */
923
892
  get options() {
924
893
  if (!this.elastic)
@@ -926,34 +895,17 @@ class AbstractElastic6Instance {
926
895
  return this.elastic.options;
927
896
  }
928
897
  /**
929
- * create Elasticsearch index w/ custom settings
898
+ * create the index w/ the default typeless settings (one implementation for every instance)
930
899
  */
931
900
  async createIndex() {
932
- if (this.elastic) {
933
- const { docType, idName } = this.options;
934
- const settings = cores_1.Elastic6Service.prepareSettings({ docType, idName }); // default setting
935
- const { version } = this.elastic.options;
936
- // force set type of 'score_' field
937
- const ver = engine_1.$U.F(version, 7.0);
938
- const $set = { score_: { type: 'half_float' } };
939
- if (ver < 7.0) {
940
- settings.mappings[docType].properties = {
941
- ...(settings.mappings[docType]?.properties || {}),
942
- ...$set,
943
- };
944
- }
945
- else {
946
- settings.mappings.properties = {
947
- ...(settings.mappings?.properties || {}),
948
- ...$set,
949
- };
950
- }
951
- return this.elastic.createIndex(settings);
952
- }
953
- return null;
901
+ if (!this.elastic)
902
+ return null;
903
+ const { idName } = this.options;
904
+ const settings = cores_1.OS2CoreService.prepareSettings({ idName });
905
+ return this.elastic.createIndex(settings);
954
906
  }
955
907
  /**
956
- * destroy Elasticsearch index
908
+ * destroy the index
957
909
  */
958
910
  async destroyIndex() {
959
911
  return this.elastic && (await this.elastic.destroyIndex());
@@ -965,13 +917,12 @@ class AbstractElastic6Instance {
965
917
  return this.elastic && (await this.elastic.describe());
966
918
  }
967
919
  /**
968
- * multi get
920
+ * multi get (typeless: index + docs only)
969
921
  * @param _ids _id list
970
922
  */
971
923
  async mget(_ids) {
972
924
  const $res = await this.client.mget({
973
925
  index: this.options.indexName,
974
- type: this.options.docType,
975
926
  body: {
976
927
  docs: _ids.map(_id => ({ _id })),
977
928
  },
@@ -983,22 +934,22 @@ class AbstractElastic6Instance {
983
934
  }
984
935
  /**
985
936
  * search raw query
986
- * @param body Elasticsearch Query DSL
987
- * @param params see 'search_type' in Elasticsearch documentation
937
+ * @param body OpenSearch Query DSL
938
+ * @param params see 'search_type' in OpenSearch documentation
988
939
  */
989
940
  async search(body, params) {
990
941
  if (!this.elastic)
991
942
  throw new Error(`Could not read Elasticsearch endpoint or index setting.`);
992
943
  const searchType = params?.searchType;
993
944
  const elastic = params?.indexName
994
- ? new cores_1.Elastic6Service({ ...this.options, indexName: params.indexName })
945
+ ? new cores_1.OS2CoreService({ ...this.options, indexName: params.indexName })
995
946
  : this.elastic;
996
947
  return elastic.search(body, searchType);
997
948
  }
998
949
  /**
999
950
  * create async generator that yields items queried until last
1000
- * @param body Elasticsearch Query DSL
1001
- * @param searchType see 'search_type' in Elasticsearch documentation
951
+ * @param body OpenSearch Query DSL
952
+ * @param searchType see 'search_type' in OpenSearch documentation
1002
953
  */
1003
954
  async *generateSearchResult(body, searchType) {
1004
955
  if (!body.sort)
@@ -1010,57 +961,7 @@ class AbstractElastic6Instance {
1010
961
  } while (body.search_after);
1011
962
  }
1012
963
  }
1013
- exports.AbstractElastic6Instance = AbstractElastic6Instance;
1014
- /**
1015
- * class: `Elastic6Instance`
1016
- * - default agent to handle search.
1017
- */
1018
- class Elastic6Instance extends AbstractElastic6Instance {
1019
- constructor(params) {
1020
- super(params);
1021
- }
1022
- hello() {
1023
- return `Elastic6Instance`;
1024
- }
1025
- /**
1026
- * expose the internal helpers. (ONLY for debugging)
1027
- */
1028
- get $X() {
1029
- return $X;
1030
- }
1031
- }
1032
- exports.Elastic6Instance = Elastic6Instance;
1033
- /**
1034
- * (internal) class: `Elastic6Proxy`
1035
- * - proxied agent to handle `search()`
1036
- */
1037
- class Elastic6Proxy extends Elastic6Instance {
1038
- proxy;
1039
- constructor(params, proxy) {
1040
- super(params);
1041
- this.proxy = proxy;
1042
- }
1043
- hello() {
1044
- return `Elastic6Proxy`;
1045
- }
1046
- /**
1047
- * search raw query
1048
- *
1049
- * @override
1050
- */
1051
- async search(body, params) {
1052
- const _hmac = (s, delim = ':', prefix = 'v1') => [prefix, engine_1.$U.hmac(Array.isArray(s) ? s.join(delim) : s, 'base64')].join(delim);
1053
- //* use proxy if possible.
1054
- if (this.proxy) {
1055
- const service = (0, helpers_1.$info)()?.service;
1056
- const indexName = params?.indexName ?? this.options?.indexName ?? '';
1057
- const signature = params?.signature ?? _hmac([_hmac(service), indexName]);
1058
- const $body = { body, service, index: indexName, signature };
1059
- return this.proxy.doProxy('POST', null, null, params, $body);
1060
- }
1061
- return super.search(body, params);
1062
- }
1063
- }
964
+ exports.AbstractOS2Instance = AbstractOS2Instance;
1064
965
  /**
1065
966
  * from Elasticsearch document to model item
1066
967
  * - replace the elastic's `$id` field to `_id` of dynamo-table.
@@ -1076,241 +977,4 @@ function sourceToItem(_source, idName = '$id') {
1076
977
  }
1077
978
  return item;
1078
979
  }
1079
- /**
1080
- * internal helper function.
1081
- */
1082
- const $X = {
1083
- /**
1084
- * describe (or parse) the endpoint url
1085
- * - it detects if endpoint is the proxied search.
1086
- * - it detects if endpoint needs the tunneling.
1087
- *
1088
- * @param url
1089
- */
1090
- describeEndpointUrl: (url, options) => {
1091
- const errScope = options?.errScope ?? `describeEndpointUrl()`;
1092
- const throwable = options?.throwable ?? true;
1093
- url = /^\/\/[a-z0-9]+/.test(url) ? `https:${url}` : url;
1094
- if (throwable) {
1095
- if (!url)
1096
- throw new Error(`@url(string) is required - ${errScope}`);
1097
- if (!(url?.startsWith('http://') || url?.startsWith('https://')))
1098
- throw new Error(`@url[${url ?? ''}] is invalid (no http) - ${errScope}`);
1099
- }
1100
- const $url = new URL(url);
1101
- const host = engine_1.$U.S($url.hostname || $url.host);
1102
- const isTunnel = $url.hostname === 'localhost';
1103
- const protocol = engine_1.$U.S($url.protocol).split(':')[0] ?? '';
1104
- const port = engine_1.$U.N($url.port, url?.startsWith('https') ? 443 : 80);
1105
- const hosts = host.split('.');
1106
- //* use `/search/0/proxy` working with deployed enpoint (requires access-key)
1107
- const isProxy = host.endsWith('.amazonaws.com') && hosts[hosts.length - 4] == 'execute-api';
1108
- const region = isProxy ? hosts[hosts.length - 3] : undefined;
1109
- return {
1110
- /** protocol */
1111
- protocol,
1112
- /** host name */
1113
- host,
1114
- /** port number */
1115
- port,
1116
- /** region in cloud */
1117
- region,
1118
- /** flag to use tunneling w/ ssh */
1119
- isTunnel,
1120
- /** flag to use proxy */
1121
- isProxy,
1122
- };
1123
- },
1124
- /**
1125
- * create http-web-proxy agent which using endpoint as proxy server.
1126
- * - originally refer to `createHttpWebProxy()`
1127
- *
1128
- * # as cases.
1129
- * as proxy agent: GET <endpoint>/<host?>/<path?>
1130
- * as direct agent: GET <endpoint>/<id?>/<cmd?>
1131
- *
1132
- * @param endpoint service url (ex: `https://xyz.execute-api.~/dev/search/0/proxy`)
1133
- * @param options optionals parameters.
1134
- */
1135
- createHttpSearchProxy: (endpoint, options) => {
1136
- const name = options?.name ?? engine_1.$U.env('NAME');
1137
- const errScope = `createHttpSearchProxy(${name ?? ''})`;
1138
- if (!endpoint)
1139
- throw new Error(`@endpoint (url) is required - ${errScope}`);
1140
- const NS = engine_1.$U.NS(`X${name}`, 'magenta'); // NAMESPACE TO BE PRINTED.
1141
- const headers = options?.headers;
1142
- const encoder = options?.encoder ?? ((name, path) => path);
1143
- const relayHeaderKey = options?.relayHeaderKey || '';
1144
- const resultKey = options?.resultKey ?? '';
1145
- // initialize AWS SigV4 Client
1146
- const _client = () => {
1147
- if (!options?.credentials)
1148
- return null;
1149
- const $cred = options?.credentials;
1150
- const $info = $X.describeEndpointUrl(endpoint, { throwable: false });
1151
- const config = {
1152
- accessKey: $cred?.accessKeyId,
1153
- secretKey: $cred?.secretAccessKey,
1154
- serviceName: 'execute-api',
1155
- host: $info?.host,
1156
- region: options?.region ?? $info?.region,
1157
- endpoint,
1158
- };
1159
- return (0, sig_v4_1.sigV4Client)(config);
1160
- };
1161
- const sigClient = _client();
1162
- /**
1163
- * class: `ApiHttpProxy`
1164
- * - http proxy client via backbone's web.
1165
- */
1166
- return new (class {
1167
- // eslint-disable-next-line @typescript-eslint/no-empty-function
1168
- constructor() { }
1169
- hello = () => `http-search-proxy:${name}`;
1170
- doProxy(method, path1, path2, $param, $body, ctx) {
1171
- if (!method)
1172
- throw new Error(`@method is required - ${errScope}`);
1173
- (0, engine_1._log)(NS, `doProxy(${method})..`);
1174
- const _isNa = (a) => a === undefined || a === null;
1175
- (0, engine_1._log)(NS, '> endpoint =', endpoint);
1176
- _isNa(path1) && (0, engine_1._log)(NS, `> host(id) =`, typeof path1, path1);
1177
- _isNa(path2) && (0, engine_1._log)(NS, `> path(cmd) =`, typeof path2, path2);
1178
- // eslint-disable-next-line prettier/prettier
1179
- const query_string = _isNa($param) ? '' : (typeof $param == 'object' ? query_string_1.default.stringify($param) : `${$param}`);
1180
- const url = endpoint +
1181
- (_isNa(path1) ? '' : `/${encoder('host', path1)}`) +
1182
- (_isNa(path1) && _isNa(path2) ? '' : `/${encoder('path', path2)}`) +
1183
- (!query_string ? '' : '?' + query_string);
1184
- const request = request_1.default;
1185
- const options = {
1186
- method,
1187
- uri: url,
1188
- headers: { ...headers },
1189
- body: $body === null ? undefined : $body,
1190
- json: typeof $body === 'string' ? false : true,
1191
- };
1192
- //* build signed url
1193
- if (sigClient) {
1194
- const signedRequest = sigClient.signRequest({
1195
- method,
1196
- path: (_isNa(path1) ? '' : `/${encoder('host', path1)}`) +
1197
- (_isNa(path1) && _isNa(path2) ? '' : `/${encoder('path', path2)}`),
1198
- queryParams: $param,
1199
- headers: options.headers,
1200
- body: $body,
1201
- });
1202
- options.headers = { ...options.headers, ...signedRequest.headers };
1203
- options.uri = signedRequest.url;
1204
- }
1205
- //* relay HEADERS to `WEB-API`
1206
- if (headers) {
1207
- options.headers = Object.keys(headers).reduce((H, key) => {
1208
- const val = headers[key];
1209
- const name = `${relayHeaderKey}${key}`;
1210
- const text = `${val ?? ''}`;
1211
- H[name] = text;
1212
- return H;
1213
- }, options.headers);
1214
- }
1215
- (0, engine_1._log)(NS, ' url :=', options.method, url);
1216
- (0, engine_1._log)(NS, '*', options.method, url, options.json ? 'json' : 'plain');
1217
- (0, engine_1._log)(NS, '> options =', engine_1.$U.json(options));
1218
- //* returns promise
1219
- return new Promise((resolve, reject) => {
1220
- //* start request..
1221
- request(options, function (error, response, body) {
1222
- error && (0, engine_1._err)(NS, '>>>>> requested! err=', error);
1223
- if (error)
1224
- return reject(error instanceof Error ? error : new Error((0, test_helper_1.GETERR)(error)));
1225
- //* detect trouble.
1226
- const statusCode = response.statusCode;
1227
- const statusMessage = response.statusMessage;
1228
- //* if not in success
1229
- if (statusCode !== 200 && statusCode !== 201) {
1230
- const msg = body ? (0, test_helper_1.GETERR)(body) : `${statusMessage || ''}`;
1231
- if (statusCode === 400 || statusCode === 404) {
1232
- const title = `${(statusCode == 404 ? '' : statusMessage) || 'NOT FOUND'}`.toUpperCase();
1233
- const message = msg.startsWith('404 NOT FOUND')
1234
- ? msg
1235
- : `${statusCode} ${title} - ${msg}`;
1236
- return reject(new Error(message));
1237
- }
1238
- statusMessage && (0, engine_1._log)(NS, `> statusMessage[${statusCode}] =`, statusMessage);
1239
- body && (0, engine_1._log)(NS, `> body[${statusCode}] =`, engine_1.$U.json(body));
1240
- return reject(new Error(`${statusCode} ${statusMessage || 'FAILURE'} - ${msg}`));
1241
- }
1242
- //* try to parse body.
1243
- try {
1244
- if (body && typeof body == 'string' && body.startsWith('{') && body.endsWith('}')) {
1245
- body = JSON.parse(body);
1246
- }
1247
- else if (body && typeof body == 'string' && body.startsWith('[') && body.endsWith(']')) {
1248
- body = JSON.parse(body);
1249
- }
1250
- }
1251
- catch (e) {
1252
- (0, engine_1._err)(NS, '!WARN! parse(body) =', e instanceof Error ? e : engine_1.$U.json(e));
1253
- }
1254
- //* ok! succeeded.
1255
- resolve(body);
1256
- });
1257
- }).then((res) => {
1258
- if (resultKey && res && res[resultKey] !== undefined)
1259
- return res[resultKey];
1260
- return res;
1261
- });
1262
- }
1263
- })();
1264
- },
1265
- };
1266
- /**
1267
- * (internal) factory function for `$ES6`
1268
- *
1269
- * @param options (optional) for debugging
1270
- */
1271
- const _ES6 = (options) => {
1272
- // 0. load from env configuration.
1273
- const endpoint = options?.endpoint ?? engine_1.$U.env('ES6_ENDPOINT', '');
1274
- const indexName = options?.indexName ?? engine_1.$U.env('ES6_INDEX', 'test-v1');
1275
- const esVersion = engine_1.$U.env('ES6_VERSION', '6.8'); //* version of elastic server (default 6.8)
1276
- const esDocType = engine_1.$U.env('ES6_DOCTYPE', ''); //* version of elastic server (default `_doc`)
1277
- const tableName = engine_1.$U.env('MY_DYNAMO_TABLE', 'Test');
1278
- const autocompleteFields = helpers_1.$T.SS(engine_1.$U.env('ES6_AUTOCOMPLETE_FIELDS', ''));
1279
- // use search-proxy by detectiong automatically.
1280
- const _isProxy = () => {
1281
- const isProxy = engine_1.$U.env('ES6_IS_PROXY', '')?.toLowerCase();
1282
- if (isProxy == '1' || isProxy == 'y' || isProxy == 'true')
1283
- return true;
1284
- if (endpoint) {
1285
- const $res = $X.describeEndpointUrl(endpoint, { throwable: false });
1286
- if ($res?.isProxy)
1287
- return true;
1288
- }
1289
- return false;
1290
- };
1291
- const useProxy = options?.useProxy ?? _isProxy();
1292
- // prepare constructor parameters.
1293
- const params = {
1294
- endpoint,
1295
- indexName,
1296
- esVersion,
1297
- esDocType,
1298
- tableName,
1299
- autocompleteFields,
1300
- };
1301
- //* use proxy.
1302
- if (useProxy) {
1303
- const credentials = options?.credentials;
1304
- const proxy = $X.createHttpSearchProxy(endpoint, { credentials });
1305
- return new Elastic6Proxy(params, proxy);
1306
- }
1307
- //* make a default instance.
1308
- return new Elastic6Instance(params);
1309
- };
1310
- exports._ES6 = _ES6;
1311
- /**
1312
- * const `$ES6`
1313
- * - default instance as a singleton by env configuration.
1314
- */
1315
- exports.$ES6 = (0, exports._ES6)();
1316
980
  //# sourceMappingURL=abstract-service.js.map