@milaboratories/pl-client 2.11.8 → 2.11.9

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 (242) hide show
  1. package/dist/core/auth.cjs +24 -0
  2. package/dist/core/auth.cjs.map +1 -0
  3. package/dist/core/auth.d.ts +0 -1
  4. package/dist/core/auth.js +21 -0
  5. package/dist/core/auth.js.map +1 -0
  6. package/dist/core/cache.d.ts +0 -1
  7. package/dist/core/client.cjs +288 -0
  8. package/dist/core/client.cjs.map +1 -0
  9. package/dist/core/client.d.ts +0 -1
  10. package/dist/core/client.js +267 -0
  11. package/dist/core/client.js.map +1 -0
  12. package/dist/core/config.cjs +111 -0
  13. package/dist/core/config.cjs.map +1 -0
  14. package/dist/core/config.d.ts +0 -1
  15. package/dist/core/config.js +95 -0
  16. package/dist/core/config.js.map +1 -0
  17. package/dist/core/default_client.cjs +143 -0
  18. package/dist/core/default_client.cjs.map +1 -0
  19. package/dist/core/default_client.d.ts +0 -1
  20. package/dist/core/default_client.js +120 -0
  21. package/dist/core/default_client.js.map +1 -0
  22. package/dist/core/driver.cjs +14 -0
  23. package/dist/core/driver.cjs.map +1 -0
  24. package/dist/core/driver.d.ts +0 -1
  25. package/dist/core/driver.js +12 -0
  26. package/dist/core/driver.js.map +1 -0
  27. package/dist/core/error_resource.cjs +9 -0
  28. package/dist/core/error_resource.cjs.map +1 -0
  29. package/dist/core/error_resource.d.ts +0 -1
  30. package/dist/core/error_resource.js +7 -0
  31. package/dist/core/error_resource.js.map +1 -0
  32. package/dist/core/errors.cjs +106 -0
  33. package/dist/core/errors.cjs.map +1 -0
  34. package/dist/core/errors.d.ts +0 -1
  35. package/dist/core/errors.js +93 -0
  36. package/dist/core/errors.js.map +1 -0
  37. package/dist/core/final.cjs +98 -0
  38. package/dist/core/final.cjs.map +1 -0
  39. package/dist/core/final.d.ts +0 -1
  40. package/dist/core/final.js +96 -0
  41. package/dist/core/final.js.map +1 -0
  42. package/dist/core/grpc.d.ts +0 -1
  43. package/dist/core/http.d.ts +1 -1
  44. package/dist/core/ll_client.cjs +266 -0
  45. package/dist/core/ll_client.cjs.map +1 -0
  46. package/dist/core/ll_client.d.ts +0 -1
  47. package/dist/core/ll_client.js +264 -0
  48. package/dist/core/ll_client.js.map +1 -0
  49. package/dist/core/ll_transaction.cjs +236 -0
  50. package/dist/core/ll_transaction.cjs.map +1 -0
  51. package/dist/core/ll_transaction.d.ts +0 -1
  52. package/dist/core/ll_transaction.js +233 -0
  53. package/dist/core/ll_transaction.js.map +1 -0
  54. package/dist/core/stat.cjs +74 -0
  55. package/dist/core/stat.cjs.map +1 -0
  56. package/dist/core/stat.d.ts +0 -1
  57. package/dist/core/stat.js +70 -0
  58. package/dist/core/stat.js.map +1 -0
  59. package/dist/core/transaction.cjs +626 -0
  60. package/dist/core/transaction.cjs.map +1 -0
  61. package/dist/core/transaction.d.ts +0 -1
  62. package/dist/core/transaction.js +613 -0
  63. package/dist/core/transaction.js.map +1 -0
  64. package/dist/core/type_conversion.cjs +106 -0
  65. package/dist/core/type_conversion.cjs.map +1 -0
  66. package/dist/core/type_conversion.d.ts +0 -1
  67. package/dist/core/type_conversion.js +102 -0
  68. package/dist/core/type_conversion.js.map +1 -0
  69. package/dist/core/types.cjs +159 -0
  70. package/dist/core/types.cjs.map +1 -0
  71. package/dist/core/types.d.ts +0 -1
  72. package/dist/core/types.js +134 -0
  73. package/dist/core/types.js.map +1 -0
  74. package/dist/core/unauth_client.cjs +43 -0
  75. package/dist/core/unauth_client.cjs.map +1 -0
  76. package/dist/core/unauth_client.d.ts +0 -1
  77. package/dist/core/unauth_client.js +41 -0
  78. package/dist/core/unauth_client.js.map +1 -0
  79. package/dist/helpers/pl.cjs +124 -0
  80. package/dist/helpers/pl.cjs.map +1 -0
  81. package/dist/helpers/pl.d.ts +0 -1
  82. package/dist/helpers/pl.js +94 -0
  83. package/dist/helpers/pl.js.map +1 -0
  84. package/dist/helpers/poll.cjs +148 -0
  85. package/dist/helpers/poll.cjs.map +1 -0
  86. package/dist/helpers/poll.d.ts +0 -1
  87. package/dist/helpers/poll.js +123 -0
  88. package/dist/helpers/poll.js.map +1 -0
  89. package/dist/helpers/rich_resource_types.d.ts +1 -1
  90. package/dist/helpers/smart_accessors.d.ts +1 -1
  91. package/dist/helpers/state_helpers.d.ts +0 -1
  92. package/dist/helpers/tx_helpers.cjs +25 -0
  93. package/dist/helpers/tx_helpers.cjs.map +1 -0
  94. package/dist/helpers/tx_helpers.d.ts +0 -1
  95. package/dist/helpers/tx_helpers.js +23 -0
  96. package/dist/helpers/tx_helpers.js.map +1 -0
  97. package/dist/index.cjs +99 -0
  98. package/dist/index.cjs.map +1 -0
  99. package/dist/index.d.ts +0 -1
  100. package/dist/index.js +16 -1
  101. package/dist/index.js.map +1 -1
  102. package/dist/proto/github.com/googleapis/googleapis/google/rpc/status.cjs +71 -0
  103. package/dist/proto/github.com/googleapis/googleapis/google/rpc/status.cjs.map +1 -0
  104. package/dist/proto/github.com/googleapis/googleapis/google/rpc/status.d.ts +0 -1
  105. package/dist/proto/github.com/googleapis/googleapis/google/rpc/status.js +69 -0
  106. package/dist/proto/github.com/googleapis/googleapis/google/rpc/status.js.map +1 -0
  107. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/downloadapi/protocol.client.d.ts +3 -5
  108. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/downloadapi/protocol.d.ts +0 -1
  109. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/lsapi/protocol.client.d.ts +3 -5
  110. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/lsapi/protocol.d.ts +0 -1
  111. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/progressapi/protocol.client.d.ts +3 -5
  112. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/progressapi/protocol.d.ts +0 -1
  113. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/streamingapi/protocol.client.d.ts +3 -5
  114. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/streamingapi/protocol.d.ts +0 -1
  115. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api.cjs +12238 -0
  116. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api.cjs.map +1 -0
  117. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api.client.cjs +223 -0
  118. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api.client.cjs.map +1 -0
  119. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api.client.d.ts +3 -5
  120. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api.client.js +221 -0
  121. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api.client.js.map +1 -0
  122. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api.d.ts +0 -1
  123. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api.js +12086 -0
  124. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api.js.map +1 -0
  125. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api_types.cjs +1127 -0
  126. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api_types.cjs.map +1 -0
  127. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api_types.d.ts +0 -1
  128. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api_types.js +1113 -0
  129. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api_types.js.map +1 -0
  130. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/base_types.cjs +152 -0
  131. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/base_types.cjs.map +1 -0
  132. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/base_types.d.ts +0 -1
  133. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/base_types.js +149 -0
  134. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/base_types.js.map +1 -0
  135. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/import.d.ts +0 -1
  136. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/resource_types.cjs +500 -0
  137. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/resource_types.cjs.map +1 -0
  138. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/resource_types.d.ts +0 -1
  139. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/resource_types.js +496 -0
  140. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/resource_types.js.map +1 -0
  141. package/dist/proto/google/api/http.d.ts +0 -1
  142. package/dist/proto/google/protobuf/any.cjs +151 -0
  143. package/dist/proto/google/protobuf/any.cjs.map +1 -0
  144. package/dist/proto/google/protobuf/any.d.ts +0 -1
  145. package/dist/proto/google/protobuf/any.js +149 -0
  146. package/dist/proto/google/protobuf/any.js.map +1 -0
  147. package/dist/proto/google/protobuf/descriptor.d.ts +0 -1
  148. package/dist/proto/google/protobuf/duration.cjs +105 -0
  149. package/dist/proto/google/protobuf/duration.cjs.map +1 -0
  150. package/dist/proto/google/protobuf/duration.d.ts +0 -1
  151. package/dist/proto/google/protobuf/duration.js +103 -0
  152. package/dist/proto/google/protobuf/duration.js.map +1 -0
  153. package/dist/proto/google/protobuf/empty.d.ts +0 -1
  154. package/dist/proto/google/protobuf/struct.d.ts +0 -1
  155. package/dist/proto/google/protobuf/timestamp.cjs +133 -0
  156. package/dist/proto/google/protobuf/timestamp.cjs.map +1 -0
  157. package/dist/proto/google/protobuf/timestamp.d.ts +0 -1
  158. package/dist/proto/google/protobuf/timestamp.js +131 -0
  159. package/dist/proto/google/protobuf/timestamp.js.map +1 -0
  160. package/dist/proto/google/protobuf/wrappers.d.ts +0 -1
  161. package/dist/test/test_config.cjs +149 -0
  162. package/dist/test/test_config.cjs.map +1 -0
  163. package/dist/test/test_config.d.ts +0 -1
  164. package/dist/test/test_config.js +123 -0
  165. package/dist/test/test_config.js.map +1 -0
  166. package/dist/util/branding.d.ts +0 -1
  167. package/dist/util/pl.cjs +8 -0
  168. package/dist/util/pl.cjs.map +1 -0
  169. package/dist/util/pl.d.ts +0 -1
  170. package/dist/util/pl.js +6 -0
  171. package/dist/util/pl.js.map +1 -0
  172. package/dist/util/util.cjs +17 -0
  173. package/dist/util/util.cjs.map +1 -0
  174. package/dist/util/util.d.ts +0 -1
  175. package/dist/util/util.js +15 -0
  176. package/dist/util/util.js.map +1 -0
  177. package/package.json +14 -11
  178. package/src/core/client.test.ts +1 -0
  179. package/src/core/config.test.ts +1 -0
  180. package/src/core/error.test.ts +1 -0
  181. package/src/core/ll_client.test.ts +1 -0
  182. package/src/core/ll_transaction.test.ts +1 -0
  183. package/src/core/transaction.test.ts +1 -0
  184. package/src/core/types.test.ts +1 -0
  185. package/src/core/unauth_client.test.ts +1 -0
  186. package/src/helpers/rich_resource_types.test.ts +2 -0
  187. package/src/test/test_config.test.ts +1 -0
  188. package/src/util/util.test.ts +1 -0
  189. package/dist/core/auth.d.ts.map +0 -1
  190. package/dist/core/cache.d.ts.map +0 -1
  191. package/dist/core/client.d.ts.map +0 -1
  192. package/dist/core/config.d.ts.map +0 -1
  193. package/dist/core/default_client.d.ts.map +0 -1
  194. package/dist/core/driver.d.ts.map +0 -1
  195. package/dist/core/error_resource.d.ts.map +0 -1
  196. package/dist/core/errors.d.ts.map +0 -1
  197. package/dist/core/final.d.ts.map +0 -1
  198. package/dist/core/grpc.d.ts.map +0 -1
  199. package/dist/core/http.d.ts.map +0 -1
  200. package/dist/core/ll_client.d.ts.map +0 -1
  201. package/dist/core/ll_transaction.d.ts.map +0 -1
  202. package/dist/core/stat.d.ts.map +0 -1
  203. package/dist/core/transaction.d.ts.map +0 -1
  204. package/dist/core/type_conversion.d.ts.map +0 -1
  205. package/dist/core/types.d.ts.map +0 -1
  206. package/dist/core/unauth_client.d.ts.map +0 -1
  207. package/dist/helpers/pl.d.ts.map +0 -1
  208. package/dist/helpers/poll.d.ts.map +0 -1
  209. package/dist/helpers/rich_resource_types.d.ts.map +0 -1
  210. package/dist/helpers/smart_accessors.d.ts.map +0 -1
  211. package/dist/helpers/state_helpers.d.ts.map +0 -1
  212. package/dist/helpers/tx_helpers.d.ts.map +0 -1
  213. package/dist/index.d.ts.map +0 -1
  214. package/dist/index.mjs +0 -14537
  215. package/dist/index.mjs.map +0 -1
  216. package/dist/proto/github.com/googleapis/googleapis/google/rpc/status.d.ts.map +0 -1
  217. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/downloadapi/protocol.client.d.ts.map +0 -1
  218. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/downloadapi/protocol.d.ts.map +0 -1
  219. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/lsapi/protocol.client.d.ts.map +0 -1
  220. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/lsapi/protocol.d.ts.map +0 -1
  221. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/progressapi/protocol.client.d.ts.map +0 -1
  222. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/progressapi/protocol.d.ts.map +0 -1
  223. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/streamingapi/protocol.client.d.ts.map +0 -1
  224. package/dist/proto/github.com/milaboratory/pl/controllers/shared/grpc/streamingapi/protocol.d.ts.map +0 -1
  225. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api.client.d.ts.map +0 -1
  226. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api.d.ts.map +0 -1
  227. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/api_types.d.ts.map +0 -1
  228. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/base_types.d.ts.map +0 -1
  229. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/import.d.ts.map +0 -1
  230. package/dist/proto/github.com/milaboratory/pl/plapi/plapiproto/resource_types.d.ts.map +0 -1
  231. package/dist/proto/google/api/http.d.ts.map +0 -1
  232. package/dist/proto/google/protobuf/any.d.ts.map +0 -1
  233. package/dist/proto/google/protobuf/descriptor.d.ts.map +0 -1
  234. package/dist/proto/google/protobuf/duration.d.ts.map +0 -1
  235. package/dist/proto/google/protobuf/empty.d.ts.map +0 -1
  236. package/dist/proto/google/protobuf/struct.d.ts.map +0 -1
  237. package/dist/proto/google/protobuf/timestamp.d.ts.map +0 -1
  238. package/dist/proto/google/protobuf/wrappers.d.ts.map +0 -1
  239. package/dist/test/test_config.d.ts.map +0 -1
  240. package/dist/util/branding.d.ts.map +0 -1
  241. package/dist/util/pl.d.ts.map +0 -1
  242. package/dist/util/util.d.ts.map +0 -1
@@ -0,0 +1,143 @@
1
+ 'use strict';
2
+
3
+ var fs = require('node:fs');
4
+ var config = require('./config.cjs');
5
+ var canonicalize = require('canonicalize');
6
+ var YAML = require('yaml');
7
+ var os = require('node:os');
8
+ var path = require('node:path');
9
+ var tsHelpers = require('@milaboratories/ts-helpers');
10
+ var unauth_client = require('./unauth_client.cjs');
11
+ var client = require('./client.cjs');
12
+ var node_crypto = require('node:crypto');
13
+ var auth = require('./auth.cjs');
14
+
15
+ function _interopNamespaceDefault(e) {
16
+ var n = Object.create(null);
17
+ if (e) {
18
+ Object.keys(e).forEach(function (k) {
19
+ if (k !== 'default') {
20
+ var d = Object.getOwnPropertyDescriptor(e, k);
21
+ Object.defineProperty(n, k, d.get ? d : {
22
+ enumerable: true,
23
+ get: function () { return e[k]; }
24
+ });
25
+ }
26
+ });
27
+ }
28
+ n.default = e;
29
+ return Object.freeze(n);
30
+ }
31
+
32
+ var os__namespace = /*#__PURE__*/_interopNamespaceDefault(os);
33
+ var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
34
+
35
+ const CONFIG_FILE_LOCAL_JSON = 'pl.json';
36
+ const CONFIG_FILE_USER_JSON = path__namespace.join(os__namespace.homedir(), '.pl.json');
37
+ const CONFIG_FILE_LOCAL_YAML = 'pl.yaml';
38
+ const CONFIG_FILE_USER_YAML = path__namespace.join(os__namespace.homedir(), '.pl.yaml');
39
+ const CONF_FILE_SEQUENCE = [
40
+ CONFIG_FILE_LOCAL_JSON,
41
+ CONFIG_FILE_LOCAL_YAML,
42
+ CONFIG_FILE_USER_JSON,
43
+ CONFIG_FILE_USER_YAML,
44
+ ];
45
+ const AUTH_DATA_FILE = '.pl_auth.json';
46
+ const FILE_CONFIG_OVERRIDE_FIELDS = [
47
+ 'grpcProxy',
48
+ 'httpProxy',
49
+ 'user',
50
+ 'password',
51
+ 'alternativeRoot',
52
+ 'defaultROTransactionTimeout',
53
+ 'defaultRWTransactionTimeout',
54
+ 'defaultRequestTimeout',
55
+ 'authTTLSeconds',
56
+ 'authMaxRefreshSeconds',
57
+ ];
58
+ function tryGetFileConfig() {
59
+ for (const confPath of CONF_FILE_SEQUENCE)
60
+ if (fs.existsSync(confPath)) {
61
+ const fileContent = fs.readFileSync(confPath, { encoding: 'utf-8' });
62
+ if (confPath.endsWith('json'))
63
+ return [JSON.parse(fileContent), confPath];
64
+ else
65
+ return [YAML.parse(fileContent), confPath];
66
+ }
67
+ return undefined;
68
+ }
69
+ function saveAuthInfoCallback(confHash, authMaxRefreshSeconds) {
70
+ return (newAuthInfo) => {
71
+ fs.writeFileSync(AUTH_DATA_FILE, Buffer.from(JSON.stringify({
72
+ confHash,
73
+ authInformation: newAuthInfo,
74
+ expiration: auth.inferAuthRefreshTime(newAuthInfo, authMaxRefreshSeconds),
75
+ })), 'utf8');
76
+ };
77
+ }
78
+ const cleanAuthInfoCallback = () => {
79
+ fs.rmSync(AUTH_DATA_FILE);
80
+ };
81
+ /** Uses default algorithm to construct a pl client from the environment */
82
+ async function defaultPlClient() {
83
+ let config$1 = undefined;
84
+ if (process.env.PL_ADDRESS !== undefined) {
85
+ config$1 = config.plAddressToConfig(process.env.PL_ADDRESS);
86
+ }
87
+ else {
88
+ const fromFile = tryGetFileConfig();
89
+ if (fromFile !== undefined) {
90
+ const [fileConfig, configPath] = fromFile;
91
+ const address = tsHelpers.notEmpty(fileConfig.address, `no pl address in file: ${configPath}`);
92
+ config$1 = config.plAddressToConfig(address);
93
+ // applying overrides
94
+ for (const field of FILE_CONFIG_OVERRIDE_FIELDS)
95
+ if (fileConfig[field] !== undefined)
96
+ config$1[field] = fileConfig[field];
97
+ }
98
+ }
99
+ if (config$1 === undefined)
100
+ throw new Error('Can\'t find configuration to create default platform client.');
101
+ if (process.env.PL_USER !== undefined)
102
+ config$1.user = process.env.PL_USER;
103
+ if (process.env.PL_PASSWORD !== undefined)
104
+ config$1.user = process.env.PL_PASSWORD;
105
+ const confHash = node_crypto.createHash('sha256')
106
+ .update(Buffer.from(canonicalize(config$1)))
107
+ .digest('base64');
108
+ let authInformation = undefined;
109
+ // try recover auth information from cache
110
+ if (fs.existsSync(AUTH_DATA_FILE)) {
111
+ const cache = JSON.parse(fs.readFileSync(AUTH_DATA_FILE, { encoding: 'utf-8' }));
112
+ if (cache.confHash === confHash && cache.expiration > Date.now())
113
+ authInformation = cache.authInformation;
114
+ }
115
+ if (authInformation === undefined) {
116
+ const client = new unauth_client.UnauthenticatedPlClient(config$1);
117
+ if (await client.requireAuth()) {
118
+ if (config$1.user === undefined || config$1.password === undefined)
119
+ throw new Error(`No auth information for found to authenticate with PL server.`);
120
+ authInformation = await client.login(config$1.user, config$1.password);
121
+ }
122
+ else {
123
+ // No authorization is required
124
+ authInformation = {};
125
+ }
126
+ // saving cache
127
+ fs.writeFileSync(AUTH_DATA_FILE, Buffer.from(JSON.stringify({
128
+ confHash,
129
+ authInformation,
130
+ expiration: auth.inferAuthRefreshTime(authInformation, config$1.authMaxRefreshSeconds),
131
+ })), 'utf8');
132
+ }
133
+ return await client.PlClient.init(config$1, {
134
+ authInformation,
135
+ onUpdate: (_newAuthInfo) => saveAuthInfoCallback(confHash, config$1.authMaxRefreshSeconds),
136
+ onUpdateError: cleanAuthInfoCallback,
137
+ onAuthError: cleanAuthInfoCallback,
138
+ });
139
+ }
140
+
141
+ exports.defaultPlClient = defaultPlClient;
142
+ exports.tryGetFileConfig = tryGetFileConfig;
143
+ //# sourceMappingURL=default_client.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"default_client.cjs","sources":["../../src/core/default_client.ts"],"sourcesContent":["import fs from 'node:fs';\nimport type { AuthInformation, PlClientConfig } from './config';\nimport { plAddressToConfig } from './config';\nimport canonicalize from 'canonicalize';\nimport YAML from 'yaml';\nimport * as os from 'node:os';\nimport * as path from 'node:path';\nimport { notEmpty } from '@milaboratories/ts-helpers';\nimport { UnauthenticatedPlClient } from './unauth_client';\nimport { PlClient } from './client';\nimport { createHash } from 'node:crypto';\nimport { inferAuthRefreshTime } from './auth';\n\nconst CONFIG_FILE_LOCAL_JSON = 'pl.json';\nconst CONFIG_FILE_USER_JSON = path.join(os.homedir(), '.pl.json');\nconst CONFIG_FILE_LOCAL_YAML = 'pl.yaml';\nconst CONFIG_FILE_USER_YAML = path.join(os.homedir(), '.pl.yaml');\nconst CONF_FILE_SEQUENCE = [\n CONFIG_FILE_LOCAL_JSON,\n CONFIG_FILE_LOCAL_YAML,\n CONFIG_FILE_USER_JSON,\n CONFIG_FILE_USER_YAML,\n];\n\nconst AUTH_DATA_FILE = '.pl_auth.json';\n\ntype FileConfigOverrideFields =\n | 'grpcProxy'\n | 'httpProxy'\n | 'user'\n | 'password'\n | 'alternativeRoot'\n | 'defaultROTransactionTimeout'\n | 'defaultRWTransactionTimeout'\n | 'defaultRequestTimeout'\n | 'authTTLSeconds'\n | 'authMaxRefreshSeconds';\nconst FILE_CONFIG_OVERRIDE_FIELDS: FileConfigOverrideFields[] = [\n 'grpcProxy',\n 'httpProxy',\n 'user',\n 'password',\n 'alternativeRoot',\n 'defaultROTransactionTimeout',\n 'defaultRWTransactionTimeout',\n 'defaultRequestTimeout',\n 'authTTLSeconds',\n 'authMaxRefreshSeconds',\n];\n\ntype PlConfigFile = {\n address: string;\n} & Partial<Pick<PlClientConfig, FileConfigOverrideFields>>;\n\ninterface AuthCache {\n /** To check if config changed */\n confHash: string;\n expiration: number;\n authInformation: AuthInformation;\n}\n\nexport function tryGetFileConfig(): [PlConfigFile, string] | undefined {\n for (const confPath of CONF_FILE_SEQUENCE)\n if (fs.existsSync(confPath)) {\n const fileContent = fs.readFileSync(confPath, { encoding: 'utf-8' });\n if (confPath.endsWith('json')) return [JSON.parse(fileContent) as PlConfigFile, confPath];\n else return [YAML.parse(fileContent) as PlConfigFile, confPath];\n }\n return undefined;\n}\n\nfunction saveAuthInfoCallback(\n confHash: string,\n authMaxRefreshSeconds: number,\n): (newAuthInfo: AuthInformation) => void {\n return (newAuthInfo) => {\n fs.writeFileSync(\n AUTH_DATA_FILE,\n Buffer.from(\n JSON.stringify({\n confHash,\n authInformation: newAuthInfo,\n expiration: inferAuthRefreshTime(newAuthInfo, authMaxRefreshSeconds),\n } as AuthCache),\n ),\n 'utf8',\n );\n };\n}\n\nconst cleanAuthInfoCallback = () => {\n fs.rmSync(AUTH_DATA_FILE);\n};\n\n/** Uses default algorithm to construct a pl client from the environment */\nexport async function defaultPlClient(): Promise<PlClient> {\n let config: PlClientConfig | undefined = undefined;\n if (process.env.PL_ADDRESS !== undefined) {\n config = plAddressToConfig(process.env.PL_ADDRESS);\n } else {\n const fromFile = tryGetFileConfig();\n if (fromFile !== undefined) {\n const [fileConfig, configPath] = fromFile;\n const address = notEmpty(fileConfig.address, `no pl address in file: ${configPath}`);\n config = plAddressToConfig(address);\n // applying overrides\n for (const field of FILE_CONFIG_OVERRIDE_FIELDS)\n if (fileConfig[field] !== undefined) (config as any)[field] = fileConfig[field];\n }\n }\n\n if (config === undefined)\n throw new Error('Can\\'t find configuration to create default platform client.');\n\n if (process.env.PL_USER !== undefined) config.user = process.env.PL_USER;\n\n if (process.env.PL_PASSWORD !== undefined) config.user = process.env.PL_PASSWORD;\n\n const confHash = createHash('sha256')\n .update(Buffer.from(canonicalize(config)!))\n .digest('base64');\n\n let authInformation: AuthInformation | undefined = undefined;\n\n // try recover auth information from cache\n if (fs.existsSync(AUTH_DATA_FILE)) {\n const cache: AuthCache = JSON.parse(fs.readFileSync(AUTH_DATA_FILE, { encoding: 'utf-8' }));\n if (cache.confHash === confHash && cache.expiration > Date.now())\n authInformation = cache.authInformation;\n }\n\n if (authInformation === undefined) {\n const client = new UnauthenticatedPlClient(config);\n\n if (await client.requireAuth()) {\n if (config.user === undefined || config.password === undefined)\n throw new Error(`No auth information for found to authenticate with PL server.`);\n authInformation = await client.login(config.user, config.password);\n } else {\n // No authorization is required\n authInformation = {};\n }\n\n // saving cache\n fs.writeFileSync(\n AUTH_DATA_FILE,\n Buffer.from(\n JSON.stringify({\n confHash,\n authInformation,\n expiration: inferAuthRefreshTime(authInformation, config.authMaxRefreshSeconds),\n } as AuthCache),\n ),\n 'utf8',\n );\n }\n\n return await PlClient.init(config, {\n authInformation,\n onUpdate: (_newAuthInfo) => saveAuthInfoCallback(confHash, config.authMaxRefreshSeconds),\n onUpdateError: cleanAuthInfoCallback,\n onAuthError: cleanAuthInfoCallback,\n });\n}\n"],"names":["path","os","inferAuthRefreshTime","config","plAddressToConfig","notEmpty","createHash","UnauthenticatedPlClient","PlClient"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAaA,MAAM,sBAAsB,GAAG,SAAS;AACxC,MAAM,qBAAqB,GAAGA,eAAI,CAAC,IAAI,CAACC,aAAE,CAAC,OAAO,EAAE,EAAE,UAAU,CAAC;AACjE,MAAM,sBAAsB,GAAG,SAAS;AACxC,MAAM,qBAAqB,GAAGD,eAAI,CAAC,IAAI,CAACC,aAAE,CAAC,OAAO,EAAE,EAAE,UAAU,CAAC;AACjE,MAAM,kBAAkB,GAAG;IACzB,sBAAsB;IACtB,sBAAsB;IACtB,qBAAqB;IACrB,qBAAqB;CACtB;AAED,MAAM,cAAc,GAAG,eAAe;AAatC,MAAM,2BAA2B,GAA+B;IAC9D,WAAW;IACX,WAAW;IACX,MAAM;IACN,UAAU;IACV,iBAAiB;IACjB,6BAA6B;IAC7B,6BAA6B;IAC7B,uBAAuB;IACvB,gBAAgB;IAChB,uBAAuB;CACxB;SAae,gBAAgB,G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A,IAAA,IAAI,eAAe,KAAK,SAAS,EAAE;AACjC,QAAA,MAAM,MAAM,GAAG,IAAII,qCAAuB,CAACJ,QAAM,CAAC;AAElD,QAAA,IAAI,MAAM,MAAM,CAAC,WAAW,EAAE,EAAE;YAC9B,IAAIA,QAAM,CAAC,IAAI,KAAK,SAAS,IAAIA,QAAM,CAAC,QAAQ,KAAK,SAAS;AAC5D,gBAAA,MAAM,IAAI,KAAK,CAAC,CAAA,6DAAA,CAA+D,CAAC;AAClF,YAAA,eAAe,GAAG,MAAM,MAAM,CAAC,KAAK,CAACA,QAAM,CAAC,IAAI,EAAEA,QAAM,CAAC,QAAQ,CAAC;QACpE;aAAO;;YAEL,eAAe,GAAG,EAAE;QACtB;;AAGA,QAAA,EAAE,CAAC,aAAa,CACd,cAAc,EACd,MAAM,CAAC,IAAI,CACT,IAAI,CAAC,SAAS,CAAC;YACb,QAAQ;YACR,eAAe;YACf,UAAU,EAAED,yBAAoB,CAAC,eAAe,EAAEC,QAAM,CAAC,qBAAqB,CAAC;AACnE,SAAA,CAAC,CAChB,EACD,MAAM,CACP;IACH;AAEA,IAAA,OAAO,MAAMK,eAAQ,CAAC,IAAI,CAACL,QAAM,EAAE;QACjC,eAAe;AACf,QAAA,QAAQ,EAAE,CAAC,YAAY,KAAK,oBAAoB,CAAC,QAAQ,EAAEA,QAAM,CAAC,qBAAqB,CAAC;AACxF,QAAA,aAAa,EAAE,qBAAqB;AACpC,QAAA,WAAW,EAAE,qBAAqB;AACnC,KAAA,CAAC;AACJ;;;;;"}
@@ -8,4 +8,3 @@ export declare function tryGetFileConfig(): [PlConfigFile, string] | undefined;
8
8
  /** Uses default algorithm to construct a pl client from the environment */
9
9
  export declare function defaultPlClient(): Promise<PlClient>;
10
10
  export {};
11
- //# sourceMappingURL=default_client.d.ts.map
@@ -0,0 +1,120 @@
1
+ import fs__default from 'node:fs';
2
+ import { plAddressToConfig } from './config.js';
3
+ import canonicalize from 'canonicalize';
4
+ import YAML from 'yaml';
5
+ import * as os from 'node:os';
6
+ import * as path from 'node:path';
7
+ import { notEmpty } from '@milaboratories/ts-helpers';
8
+ import { UnauthenticatedPlClient } from './unauth_client.js';
9
+ import { PlClient } from './client.js';
10
+ import { createHash } from 'node:crypto';
11
+ import { inferAuthRefreshTime } from './auth.js';
12
+
13
+ const CONFIG_FILE_LOCAL_JSON = 'pl.json';
14
+ const CONFIG_FILE_USER_JSON = path.join(os.homedir(), '.pl.json');
15
+ const CONFIG_FILE_LOCAL_YAML = 'pl.yaml';
16
+ const CONFIG_FILE_USER_YAML = path.join(os.homedir(), '.pl.yaml');
17
+ const CONF_FILE_SEQUENCE = [
18
+ CONFIG_FILE_LOCAL_JSON,
19
+ CONFIG_FILE_LOCAL_YAML,
20
+ CONFIG_FILE_USER_JSON,
21
+ CONFIG_FILE_USER_YAML,
22
+ ];
23
+ const AUTH_DATA_FILE = '.pl_auth.json';
24
+ const FILE_CONFIG_OVERRIDE_FIELDS = [
25
+ 'grpcProxy',
26
+ 'httpProxy',
27
+ 'user',
28
+ 'password',
29
+ 'alternativeRoot',
30
+ 'defaultROTransactionTimeout',
31
+ 'defaultRWTransactionTimeout',
32
+ 'defaultRequestTimeout',
33
+ 'authTTLSeconds',
34
+ 'authMaxRefreshSeconds',
35
+ ];
36
+ function tryGetFileConfig() {
37
+ for (const confPath of CONF_FILE_SEQUENCE)
38
+ if (fs__default.existsSync(confPath)) {
39
+ const fileContent = fs__default.readFileSync(confPath, { encoding: 'utf-8' });
40
+ if (confPath.endsWith('json'))
41
+ return [JSON.parse(fileContent), confPath];
42
+ else
43
+ return [YAML.parse(fileContent), confPath];
44
+ }
45
+ return undefined;
46
+ }
47
+ function saveAuthInfoCallback(confHash, authMaxRefreshSeconds) {
48
+ return (newAuthInfo) => {
49
+ fs__default.writeFileSync(AUTH_DATA_FILE, Buffer.from(JSON.stringify({
50
+ confHash,
51
+ authInformation: newAuthInfo,
52
+ expiration: inferAuthRefreshTime(newAuthInfo, authMaxRefreshSeconds),
53
+ })), 'utf8');
54
+ };
55
+ }
56
+ const cleanAuthInfoCallback = () => {
57
+ fs__default.rmSync(AUTH_DATA_FILE);
58
+ };
59
+ /** Uses default algorithm to construct a pl client from the environment */
60
+ async function defaultPlClient() {
61
+ let config = undefined;
62
+ if (process.env.PL_ADDRESS !== undefined) {
63
+ config = plAddressToConfig(process.env.PL_ADDRESS);
64
+ }
65
+ else {
66
+ const fromFile = tryGetFileConfig();
67
+ if (fromFile !== undefined) {
68
+ const [fileConfig, configPath] = fromFile;
69
+ const address = notEmpty(fileConfig.address, `no pl address in file: ${configPath}`);
70
+ config = plAddressToConfig(address);
71
+ // applying overrides
72
+ for (const field of FILE_CONFIG_OVERRIDE_FIELDS)
73
+ if (fileConfig[field] !== undefined)
74
+ config[field] = fileConfig[field];
75
+ }
76
+ }
77
+ if (config === undefined)
78
+ throw new Error('Can\'t find configuration to create default platform client.');
79
+ if (process.env.PL_USER !== undefined)
80
+ config.user = process.env.PL_USER;
81
+ if (process.env.PL_PASSWORD !== undefined)
82
+ config.user = process.env.PL_PASSWORD;
83
+ const confHash = createHash('sha256')
84
+ .update(Buffer.from(canonicalize(config)))
85
+ .digest('base64');
86
+ let authInformation = undefined;
87
+ // try recover auth information from cache
88
+ if (fs__default.existsSync(AUTH_DATA_FILE)) {
89
+ const cache = JSON.parse(fs__default.readFileSync(AUTH_DATA_FILE, { encoding: 'utf-8' }));
90
+ if (cache.confHash === confHash && cache.expiration > Date.now())
91
+ authInformation = cache.authInformation;
92
+ }
93
+ if (authInformation === undefined) {
94
+ const client = new UnauthenticatedPlClient(config);
95
+ if (await client.requireAuth()) {
96
+ if (config.user === undefined || config.password === undefined)
97
+ throw new Error(`No auth information for found to authenticate with PL server.`);
98
+ authInformation = await client.login(config.user, config.password);
99
+ }
100
+ else {
101
+ // No authorization is required
102
+ authInformation = {};
103
+ }
104
+ // saving cache
105
+ fs__default.writeFileSync(AUTH_DATA_FILE, Buffer.from(JSON.stringify({
106
+ confHash,
107
+ authInformation,
108
+ expiration: inferAuthRefreshTime(authInformation, config.authMaxRefreshSeconds),
109
+ })), 'utf8');
110
+ }
111
+ return await PlClient.init(config, {
112
+ authInformation,
113
+ onUpdate: (_newAuthInfo) => saveAuthInfoCallback(confHash, config.authMaxRefreshSeconds),
114
+ onUpdateError: cleanAuthInfoCallback,
115
+ onAuthError: cleanAuthInfoCallback,
116
+ });
117
+ }
118
+
119
+ export { defaultPlClient, tryGetFileConfig };
120
+ //# sourceMappingURL=default_client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"default_client.js","sources":["../../src/core/default_client.ts"],"sourcesContent":["import fs from 'node:fs';\nimport type { AuthInformation, PlClientConfig } from './config';\nimport { plAddressToConfig } from './config';\nimport canonicalize from 'canonicalize';\nimport YAML from 'yaml';\nimport * as os from 'node:os';\nimport * as path from 'node:path';\nimport { notEmpty } from '@milaboratories/ts-helpers';\nimport { UnauthenticatedPlClient } from './unauth_client';\nimport { PlClient } from './client';\nimport { createHash } from 'node:crypto';\nimport { inferAuthRefreshTime } from './auth';\n\nconst CONFIG_FILE_LOCAL_JSON = 'pl.json';\nconst CONFIG_FILE_USER_JSON = path.join(os.homedir(), '.pl.json');\nconst CONFIG_FILE_LOCAL_YAML = 'pl.yaml';\nconst CONFIG_FILE_USER_YAML = path.join(os.homedir(), '.pl.yaml');\nconst CONF_FILE_SEQUENCE = [\n CONFIG_FILE_LOCAL_JSON,\n CONFIG_FILE_LOCAL_YAML,\n CONFIG_FILE_USER_JSON,\n CONFIG_FILE_USER_YAML,\n];\n\nconst AUTH_DATA_FILE = '.pl_auth.json';\n\ntype FileConfigOverrideFields =\n | 'grpcProxy'\n | 'httpProxy'\n | 'user'\n | 'password'\n | 'alternativeRoot'\n | 'defaultROTransactionTimeout'\n | 'defaultRWTransactionTimeout'\n | 'defaultRequestTimeout'\n | 'authTTLSeconds'\n | 'authMaxRefreshSeconds';\nconst FILE_CONFIG_OVERRIDE_FIELDS: FileConfigOverrideFields[] = [\n 'grpcProxy',\n 'httpProxy',\n 'user',\n 'password',\n 'alternativeRoot',\n 'defaultROTransactionTimeout',\n 'defaultRWTransactionTimeout',\n 'defaultRequestTimeout',\n 'authTTLSeconds',\n 'authMaxRefreshSeconds',\n];\n\ntype PlConfigFile = {\n address: string;\n} & Partial<Pick<PlClientConfig, FileConfigOverrideFields>>;\n\ninterface AuthCache {\n /** To check if config changed */\n confHash: string;\n expiration: number;\n authInformation: AuthInformation;\n}\n\nexport function tryGetFileConfig(): [PlConfigFile, string] | undefined {\n for (const confPath of CONF_FILE_SEQUENCE)\n if (fs.existsSync(confPath)) {\n const fileContent = fs.readFileSync(confPath, { encoding: 'utf-8' });\n if (confPath.endsWith('json')) return [JSON.parse(fileContent) as PlConfigFile, confPath];\n else return [YAML.parse(fileContent) as PlConfigFile, confPath];\n }\n return undefined;\n}\n\nfunction saveAuthInfoCallback(\n confHash: string,\n authMaxRefreshSeconds: number,\n): (newAuthInfo: AuthInformation) => void {\n return (newAuthInfo) => {\n fs.writeFileSync(\n AUTH_DATA_FILE,\n Buffer.from(\n JSON.stringify({\n confHash,\n authInformation: newAuthInfo,\n expiration: inferAuthRefreshTime(newAuthInfo, authMaxRefreshSeconds),\n } as AuthCache),\n ),\n 'utf8',\n );\n };\n}\n\nconst cleanAuthInfoCallback = () => {\n fs.rmSync(AUTH_DATA_FILE);\n};\n\n/** Uses default algorithm to construct a pl client from the environment */\nexport async function defaultPlClient(): Promise<PlClient> {\n let config: PlClientConfig | undefined = undefined;\n if (process.env.PL_ADDRESS !== undefined) {\n config = plAddressToConfig(process.env.PL_ADDRESS);\n } else {\n const fromFile = tryGetFileConfig();\n if (fromFile !== undefined) {\n const [fileConfig, configPath] = fromFile;\n const address = notEmpty(fileConfig.address, `no pl address in file: ${configPath}`);\n config = plAddressToConfig(address);\n // applying overrides\n for (const field of FILE_CONFIG_OVERRIDE_FIELDS)\n if (fileConfig[field] !== undefined) (config as any)[field] = fileConfig[field];\n }\n }\n\n if (config === undefined)\n throw new Error('Can\\'t find configuration to create default platform client.');\n\n if (process.env.PL_USER !== undefined) config.user = process.env.PL_USER;\n\n if (process.env.PL_PASSWORD !== undefined) config.user = process.env.PL_PASSWORD;\n\n const confHash = createHash('sha256')\n .update(Buffer.from(canonicalize(config)!))\n .digest('base64');\n\n let authInformation: AuthInformation | undefined = undefined;\n\n // try recover auth information from cache\n if (fs.existsSync(AUTH_DATA_FILE)) {\n const cache: AuthCache = JSON.parse(fs.readFileSync(AUTH_DATA_FILE, { encoding: 'utf-8' }));\n if (cache.confHash === confHash && cache.expiration > Date.now())\n authInformation = cache.authInformation;\n }\n\n if (authInformation === undefined) {\n const client = new UnauthenticatedPlClient(config);\n\n if (await client.requireAuth()) {\n if (config.user === undefined || config.password === undefined)\n throw new Error(`No auth information for found to authenticate with PL server.`);\n authInformation = await client.login(config.user, config.password);\n } else {\n // No authorization is required\n authInformation = {};\n }\n\n // saving cache\n fs.writeFileSync(\n AUTH_DATA_FILE,\n Buffer.from(\n JSON.stringify({\n confHash,\n authInformation,\n expiration: inferAuthRefreshTime(authInformation, config.authMaxRefreshSeconds),\n } as AuthCache),\n ),\n 'utf8',\n );\n }\n\n return await PlClient.init(config, {\n authInformation,\n onUpdate: (_newAuthInfo) => saveAuthInfoCallback(confHash, config.authMaxRefreshSeconds),\n onUpdateError: cleanAuthInfoCallback,\n onAuthError: cleanAuthInfoCallback,\n });\n}\n"],"names":["fs"],"mappings":";;;;;;;;;;;;AAaA,MAAM,sBAAsB,GAAG,SAAS;AACxC,MAAM,qBAAqB,GAAG,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,EAAE,UAAU,CAAC;AACjE,MAAM,sBAAsB,GAAG,SAAS;AACxC,MAAM,qBAAqB,GAAG,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,EAAE,UAAU,CAAC;AACjE,MAAM,kBAAkB,GAAG;IACzB,sBAAsB;IACtB,sBAAsB;IACtB,qBAAqB;IACrB,qBAAqB;CACtB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necessary for the platform core
5
+ // to proxy the call to the proper controller.
6
+ function addRTypeToMetadata(rType, options) {
7
+ options = options ?? {};
8
+ options.meta = options.meta ?? {};
9
+ options.meta['resourceType'] = `${rType.name}:${rType.version}`;
10
+ return options;
11
+ }
12
+
13
+ exports.addRTypeToMetadata = addRTypeToMetadata;
14
+ //# sourceMappingURL=driver.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"driver.cjs","sources":["../../src/core/driver.ts"],"sourcesContent":["import type { PlClient } from './client';\nimport type { RpcOptions } from '@protobuf-ts/runtime-rpc';\nimport type { Dispatcher } from 'undici';\nimport type { ResourceType } from './types';\nimport type { GrpcClientProviderFactory } from './grpc';\n\n/** Drivers must implement this interface */\nexport interface PlDriver {\n close(): void;\n}\n\n/** Definition to use driver via {@link PlClient} */\nexport interface PlDriverDefinition<Drv extends PlDriver> {\n /** Used as key to only once instantiate specific drivers */\n readonly name: string;\n\n /** Initialization routine, will be executed only once for each driver in a specific client */\n init(pl: PlClient, grpcClientProviderFactory: GrpcClientProviderFactory, httpDispatcher: Dispatcher): Drv;\n}\n\n// addRTypeToMetadata adds a metadata with resource type\n// for every RPC call. It is necessary for the platform core\n// to proxy the call to the proper controller.\nexport function addRTypeToMetadata(rType: ResourceType, options?: RpcOptions) {\n options = options ?? {};\n options.meta = options.meta ?? {};\n options.meta['resourceType'] = `${rType.name}:${rType.version}`;\n\n return options;\n}\n"],"names":[],"mappings":";;AAoBA;AACA;AACA;AACM,SAAU,kBAAkB,CAAC,KAAmB,EAAE,OAAoB,EAAA;AAC1E,IAAA,OAAO,GAAG,OAAO,IAAI,EAAE;IACvB,OAAO,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,EAAE;AACjC,IAAA,OAAO,CAAC,IAAI,CAAC,cAAc,CAAC,GAAG,CAAA,EAAG,KAAK,CAAC,IAAI,CAAA,CAAA,EAAI,KAAK,CAAC,OAAO,EAAE;AAE/D,IAAA,OAAO,OAAO;AAChB;;;;"}
@@ -15,4 +15,3 @@ export interface PlDriverDefinition<Drv extends PlDriver> {
15
15
  init(pl: PlClient, grpcClientProviderFactory: GrpcClientProviderFactory, httpDispatcher: Dispatcher): Drv;
16
16
  }
17
17
  export declare function addRTypeToMetadata(rType: ResourceType, options?: RpcOptions): RpcOptions;
18
- //# sourceMappingURL=driver.d.ts.map
@@ -0,0 +1,12 @@
1
+ // addRTypeToMetadata adds a metadata with resource type
2
+ // for every RPC call. It is necessary for the platform core
3
+ // to proxy the call to the proper controller.
4
+ function addRTypeToMetadata(rType, options) {
5
+ options = options ?? {};
6
+ options.meta = options.meta ?? {};
7
+ options.meta['resourceType'] = `${rType.name}:${rType.version}`;
8
+ return options;
9
+ }
10
+
11
+ export { addRTypeToMetadata };
12
+ //# sourceMappingURL=driver.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"driver.js","sources":["../../src/core/driver.ts"],"sourcesContent":["import type { PlClient } from './client';\nimport type { RpcOptions } from '@protobuf-ts/runtime-rpc';\nimport type { Dispatcher } from 'undici';\nimport type { ResourceType } from './types';\nimport type { GrpcClientProviderFactory } from './grpc';\n\n/** Drivers must implement this interface */\nexport interface PlDriver {\n close(): void;\n}\n\n/** Definition to use driver via {@link PlClient} */\nexport interface PlDriverDefinition<Drv extends PlDriver> {\n /** Used as key to only once instantiate specific drivers */\n readonly name: string;\n\n /** Initialization routine, will be executed only once for each driver in a specific client */\n init(pl: PlClient, grpcClientProviderFactory: GrpcClientProviderFactory, httpDispatcher: Dispatcher): Drv;\n}\n\n// addRTypeToMetadata adds a metadata with resource type\n// for every RPC call. It is necessary for the platform core\n// to proxy the call to the proper controller.\nexport function addRTypeToMetadata(rType: ResourceType, options?: RpcOptions) {\n options = options ?? {};\n options.meta = options.meta ?? {};\n options.meta['resourceType'] = `${rType.name}:${rType.version}`;\n\n return options;\n}\n"],"names":[],"mappings":"AAoBA;AACA;AACA;AACM,SAAU,kBAAkB,CAAC,KAAmB,EAAE,OAAoB,EAAA;AAC1E,IAAA,OAAO,GAAG,OAAO,IAAI,EAAE;IACvB,OAAO,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,EAAE;AACjC,IAAA,OAAO,CAAC,IAAI,CAAC,cAAc,CAAC,GAAG,CAAA,EAAG,KAAK,CAAC,IAAI,CAAA,CAAA,EAAI,KAAK,CAAC,OAAO,EAAE;AAE/D,IAAA,OAAO,OAAO;AAChB;;;;"}
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ const ErrorResourceType = {
4
+ name: 'json/resourceError',
5
+ version: '1',
6
+ };
7
+
8
+ exports.ErrorResourceType = ErrorResourceType;
9
+ //# sourceMappingURL=error_resource.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error_resource.cjs","sources":["../../src/core/error_resource.ts"],"sourcesContent":["import type { ResourceType } from './types';\n\nexport type ErrorResourceData = {\n message: string;\n};\n\nexport const ErrorResourceType: ResourceType = {\n name: 'json/resourceError',\n version: '1',\n};\n"],"names":[],"mappings":";;AAMO,MAAM,iBAAiB,GAAiB;AAC7C,IAAA,IAAI,EAAE,oBAAoB;AAC1B,IAAA,OAAO,EAAE,GAAG;;;;;"}
@@ -3,4 +3,3 @@ export type ErrorResourceData = {
3
3
  message: string;
4
4
  };
5
5
  export declare const ErrorResourceType: ResourceType;
6
- //# sourceMappingURL=error_resource.d.ts.map
@@ -0,0 +1,7 @@
1
+ const ErrorResourceType = {
2
+ name: 'json/resourceError',
3
+ version: '1',
4
+ };
5
+
6
+ export { ErrorResourceType };
7
+ //# sourceMappingURL=error_resource.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error_resource.js","sources":["../../src/core/error_resource.ts"],"sourcesContent":["import type { ResourceType } from './types';\n\nexport type ErrorResourceData = {\n message: string;\n};\n\nexport const ErrorResourceType: ResourceType = {\n name: 'json/resourceError',\n version: '1',\n};\n"],"names":[],"mappings":"AAMO,MAAM,iBAAiB,GAAiB;AAC7C,IAAA,IAAI,EAAE,oBAAoB;AAC1B,IAAA,OAAO,EAAE,GAAG;;;;;"}
@@ -0,0 +1,106 @@
1
+ 'use strict';
2
+
3
+ var tsHelpers = require('@milaboratories/ts-helpers');
4
+
5
+ function isConnectionProblem(err, nested = false) {
6
+ if (err instanceof DisconnectedError)
7
+ return true;
8
+ if (err.name == 'RpcError' && err.code == 'UNAVAILABLE')
9
+ return true;
10
+ if (err.cause !== undefined && !nested)
11
+ // nested limits the depth of search
12
+ return isConnectionProblem(err.cause, true);
13
+ return false;
14
+ }
15
+ function isUnauthenticated(err, nested = false) {
16
+ if (err instanceof UnauthenticatedError)
17
+ return true;
18
+ if (err.name == 'RpcError' && err.code == 'UNAUTHENTICATED')
19
+ return true;
20
+ if (err.cause !== undefined && !nested)
21
+ // nested limits the depth of search
22
+ return isUnauthenticated(err.cause, true);
23
+ return false;
24
+ }
25
+ function isTimeoutOrCancelError(err, nested = false) {
26
+ if (err instanceof tsHelpers.Aborted || err.name == 'AbortError')
27
+ return true;
28
+ if (err.code == 'ABORT_ERR')
29
+ return true;
30
+ if (err.name == 'RpcError'
31
+ && (err.code == 'CANCELLED' || err.code == 'DEADLINE_EXCEEDED'))
32
+ return true;
33
+ if (err.cause !== undefined && !nested)
34
+ // nested limits the depth of search
35
+ return isTimeoutOrCancelError(err.cause, true);
36
+ return false;
37
+ }
38
+ const PlErrorCodeNotFound = 5;
39
+ class PlError extends Error {
40
+ status;
41
+ name = 'PlError';
42
+ constructor(status) {
43
+ super(`code=${status.code} ${status.message}`);
44
+ this.status = status;
45
+ }
46
+ }
47
+ function throwPlNotFoundError(message) {
48
+ throw new RecoverablePlError({ code: PlErrorCodeNotFound, message, details: [] });
49
+ }
50
+ class RecoverablePlError extends PlError {
51
+ name = 'RecoverablePlError';
52
+ constructor(status) {
53
+ super(status);
54
+ }
55
+ }
56
+ class UnrecoverablePlError extends PlError {
57
+ name = 'UnrecoverablePlError';
58
+ constructor(status) {
59
+ super(status);
60
+ }
61
+ }
62
+ function isNotFoundError(err, nested = false) {
63
+ if (err.name == 'RpcError' && err.code == 'NOT_FOUND')
64
+ return true;
65
+ if (err.cause !== undefined && !nested)
66
+ return isNotFoundError(err.cause, true);
67
+ return err instanceof RecoverablePlError && err.status.code === PlErrorCodeNotFound;
68
+ }
69
+ class UnauthenticatedError extends Error {
70
+ name = 'UnauthenticatedError';
71
+ constructor(message) {
72
+ super('LoginFailed: ' + message);
73
+ }
74
+ }
75
+ class DisconnectedError extends Error {
76
+ name = 'DisconnectedError';
77
+ constructor(message) {
78
+ super('Disconnected: ' + message);
79
+ }
80
+ }
81
+ function rethrowMeaningfulError(error, wrapIfUnknown = false) {
82
+ if (isUnauthenticated(error))
83
+ throw new UnauthenticatedError(error.message);
84
+ if (isConnectionProblem(error))
85
+ throw new DisconnectedError(error.message);
86
+ if (isTimeoutOrCancelError(error))
87
+ throw new tsHelpers.Aborted(error);
88
+ if (wrapIfUnknown)
89
+ throw new Error(error.message, { cause: error });
90
+ else
91
+ throw error;
92
+ }
93
+
94
+ exports.DisconnectedError = DisconnectedError;
95
+ exports.PlError = PlError;
96
+ exports.PlErrorCodeNotFound = PlErrorCodeNotFound;
97
+ exports.RecoverablePlError = RecoverablePlError;
98
+ exports.UnauthenticatedError = UnauthenticatedError;
99
+ exports.UnrecoverablePlError = UnrecoverablePlError;
100
+ exports.isConnectionProblem = isConnectionProblem;
101
+ exports.isNotFoundError = isNotFoundError;
102
+ exports.isTimeoutOrCancelError = isTimeoutOrCancelError;
103
+ exports.isUnauthenticated = isUnauthenticated;
104
+ exports.rethrowMeaningfulError = rethrowMeaningfulError;
105
+ exports.throwPlNotFoundError = throwPlNotFoundError;
106
+ //# sourceMappingURL=errors.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.cjs","sources":["../../src/core/errors.ts"],"sourcesContent":["import type { Status } from '../proto/github.com/googleapis/googleapis/google/rpc/status';\nimport { Aborted } from '@milaboratories/ts-helpers';\n\nexport function isConnectionProblem(err: unknown, nested: boolean = false): boolean {\n if (err instanceof DisconnectedError) return true;\n if ((err as any).name == 'RpcError' && (err as any).code == 'UNAVAILABLE') return true;\n if ((err as any).cause !== undefined && !nested)\n // nested limits the depth of search\n return isConnectionProblem((err as any).cause, true);\n return false;\n}\n\nexport function isUnauthenticated(err: unknown, nested: boolean = false): boolean {\n if (err instanceof UnauthenticatedError) return true;\n if ((err as any).name == 'RpcError' && (err as any).code == 'UNAUTHENTICATED') return true;\n if ((err as any).cause !== undefined && !nested)\n // nested limits the depth of search\n return isUnauthenticated((err as any).cause, true);\n return false;\n}\n\nexport function isTimeoutOrCancelError(err: unknown, nested: boolean = false): boolean {\n if (err instanceof Aborted || (err as any).name == 'AbortError') return true;\n if ((err as any).code == 'ABORT_ERR') return true;\n if (\n (err as any).name == 'RpcError'\n && ((err as any).code == 'CANCELLED' || (err as any).code == 'DEADLINE_EXCEEDED')\n )\n return true;\n if ((err as any).cause !== undefined && !nested)\n // nested limits the depth of search\n return isTimeoutOrCancelError((err as any).cause, true);\n return false;\n}\n\nexport const PlErrorCodeNotFound = 5;\n\nexport class PlError extends Error {\n name = 'PlError';\n constructor(public readonly status: Status) {\n super(`code=${status.code} ${status.message}`);\n }\n}\n\nexport function throwPlNotFoundError(message: string): never {\n throw new RecoverablePlError({ code: PlErrorCodeNotFound, message, details: [] });\n}\n\nexport class RecoverablePlError extends PlError {\n name = 'RecoverablePlError';\n constructor(status: Status) {\n super(status);\n }\n}\n\nexport class UnrecoverablePlError extends PlError {\n name = 'UnrecoverablePlError';\n constructor(status: Status) {\n super(status);\n }\n}\n\nexport function isNotFoundError(err: unknown, nested: boolean = false): boolean {\n if ((err as any).name == 'RpcError' && (err as any).code == 'NOT_FOUND') return true;\n if ((err as any).cause !== undefined && !nested) return isNotFoundError((err as any).cause, true);\n return err instanceof RecoverablePlError && err.status.code === PlErrorCodeNotFound;\n}\n\nexport class UnauthenticatedError extends Error {\n name = 'UnauthenticatedError';\n constructor(message: string) {\n super('LoginFailed: ' + message);\n }\n}\n\nexport class DisconnectedError extends Error {\n name = 'DisconnectedError';\n constructor(message: string) {\n super('Disconnected: ' + message);\n }\n}\n\nexport function rethrowMeaningfulError(error: any, wrapIfUnknown: boolean = false): never {\n if (isUnauthenticated(error)) throw new UnauthenticatedError(error.message);\n if (isConnectionProblem(error)) throw new DisconnectedError(error.message);\n if (isTimeoutOrCancelError(error)) throw new Aborted(error);\n if (wrapIfUnknown) throw new Error(error.message, { cause: error });\n else throw error;\n}\n"],"names":["Aborted"],"mappings":";;;;SAGgB,mBAAmB,CAAC,GAAY,EAAE,SAAkB,KAAK,EAAA;IACvE,IAAI,GAAG,YAAY,iBAAiB;AAAE,QAAA,OAAO,IAAI;IACjD,IAAK,GAAW,CAAC,IAAI,IAAI,UAAU,IAAK,GAAW,CAAC,IAAI,IAAI,aAAa;AAAE,QAAA,OAAO,IAAI;AACtF,IAAA,IAAK,GAAW,CAAC,KAAK,KAAK,SAAS,IAAI,CAAC,MAAM;;QAE7C,OAAO,mBAAmB,CAAE,GAAW,CAAC,KAAK,EAAE,IAAI,CAAC;AACtD,IAAA,OAAO,KAAK;AACd;SAEgB,iBAAiB,CAAC,GAAY,EAAE,SAAkB,KAAK,EAAA;IACrE,IAAI,GAAG,YAAY,oBAAoB;AAAE,QAAA,OAAO,IAAI;IACpD,IAAK,GAAW,CAAC,IAAI,IAAI,UAAU,IAAK,GAAW,CAAC,IAAI,IAAI,iBAAiB;AAAE,QAAA,OAAO,IAAI;AAC1F,IAAA,IAAK,GAAW,CAAC,KAAK,KAAK,SAAS,IAAI,CAAC,MAAM;;QAE7C,OAAO,iBAAiB,CAAE,GAAW,CAAC,KAAK,EAAE,IAAI,CAAC;AACpD,IAAA,OAAO,KAAK;AACd;SAEgB,sBAAsB,CAAC,GAAY,EAAE,SAAkB,KAAK,EAAA;IAC1E,IAAI,GAAG,YAAYA,iBAAO,IAAK,GAAW,CAAC,IAAI,IAAI,YAAY;AAAE,QAAA,OAAO,IAAI;AAC5E,IAAA,IAAK,GAAW,CAAC,IAAI,IAAI,WA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AI;AACpF,IAAA,IAAK,GAAW,CAAC,KAAK,KAAK,SAAS,IAAI,CAAC,MAAM;QAAE,OAAO,eAAe,CAAE,GAAW,CAAC,KAAK,EAAE,IAAI,CAAC;IACjG,OAAO,GAAG,YAAY,kBAAkB,IAAI,GAAG,CAAC,MAAM,CAAC,IAAI,KAAK,mBAAmB;AACrF;AAEM,MAAO,oBAAqB,SAAQ,KAAK,CAAA;IAC7C,IAAI,GAAG,sBAAsB;AAC7B,IAAA,WAAA,CAAY,OAAe,EAAA;AACzB,QAAA,KAAK,CAAC,eAAe,GAAG,OAAO,CAAC;IAClC;AACD;AAEK,MAAO,iBAAkB,SAAQ,KAAK,CAAA;IAC1C,IAAI,GAAG,mBAAmB;AAC1B,IAAA,WAAA,CAAY,OAAe,EAAA;AACzB,QAAA,KAAK,CAAC,gBAAgB,GAAG,OAAO,CAAC;IACnC;AACD;SAEe,sBAAsB,CAAC,KAAU,EAAE,gBAAyB,KAAK,EAAA;IAC/E,IAAI,iBAAiB,CAAC,KAAK,CAAC;AAAE,QAAA,MAAM,IAAI,oBAAoB,CAAC,KAAK,CAAC,OAAO,CAAC;IAC3E,IAAI,mBAAmB,CAAC,KAAK,CAAC;AAAE,QAAA,MAAM,IAAI,iBAAiB,CAAC,KAAK,CAAC,OAAO,CAAC;IAC1E,IAAI,sBAAsB,CAAC,KAAK,CAAC;AAAE,QAAA,MAAM,IAAIA,iBAAO,CAAC,KAAK,CAAC;AAC3D,IAAA,IAAI,aAAa;AAAE,QAAA,MAAM,IAAI,KAAK,CAAC,KAAK,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;;AAC9D,QAAA,MAAM,KAAK;AAClB;;;;;;;;;;;;;;;"}
@@ -27,4 +27,3 @@ export declare class DisconnectedError extends Error {
27
27
  constructor(message: string);
28
28
  }
29
29
  export declare function rethrowMeaningfulError(error: any, wrapIfUnknown?: boolean): never;
30
- //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1,93 @@
1
+ import { Aborted } from '@milaboratories/ts-helpers';
2
+
3
+ function isConnectionProblem(err, nested = false) {
4
+ if (err instanceof DisconnectedError)
5
+ return true;
6
+ if (err.name == 'RpcError' && err.code == 'UNAVAILABLE')
7
+ return true;
8
+ if (err.cause !== undefined && !nested)
9
+ // nested limits the depth of search
10
+ return isConnectionProblem(err.cause, true);
11
+ return false;
12
+ }
13
+ function isUnauthenticated(err, nested = false) {
14
+ if (err instanceof UnauthenticatedError)
15
+ return true;
16
+ if (err.name == 'RpcError' && err.code == 'UNAUTHENTICATED')
17
+ return true;
18
+ if (err.cause !== undefined && !nested)
19
+ // nested limits the depth of search
20
+ return isUnauthenticated(err.cause, true);
21
+ return false;
22
+ }
23
+ function isTimeoutOrCancelError(err, nested = false) {
24
+ if (err instanceof Aborted || err.name == 'AbortError')
25
+ return true;
26
+ if (err.code == 'ABORT_ERR')
27
+ return true;
28
+ if (err.name == 'RpcError'
29
+ && (err.code == 'CANCELLED' || err.code == 'DEADLINE_EXCEEDED'))
30
+ return true;
31
+ if (err.cause !== undefined && !nested)
32
+ // nested limits the depth of search
33
+ return isTimeoutOrCancelError(err.cause, true);
34
+ return false;
35
+ }
36
+ const PlErrorCodeNotFound = 5;
37
+ class PlError extends Error {
38
+ status;
39
+ name = 'PlError';
40
+ constructor(status) {
41
+ super(`code=${status.code} ${status.message}`);
42
+ this.status = status;
43
+ }
44
+ }
45
+ function throwPlNotFoundError(message) {
46
+ throw new RecoverablePlError({ code: PlErrorCodeNotFound, message, details: [] });
47
+ }
48
+ class RecoverablePlError extends PlError {
49
+ name = 'RecoverablePlError';
50
+ constructor(status) {
51
+ super(status);
52
+ }
53
+ }
54
+ class UnrecoverablePlError extends PlError {
55
+ name = 'UnrecoverablePlError';
56
+ constructor(status) {
57
+ super(status);
58
+ }
59
+ }
60
+ function isNotFoundError(err, nested = false) {
61
+ if (err.name == 'RpcError' && err.code == 'NOT_FOUND')
62
+ return true;
63
+ if (err.cause !== undefined && !nested)
64
+ return isNotFoundError(err.cause, true);
65
+ return err instanceof RecoverablePlError && err.status.code === PlErrorCodeNotFound;
66
+ }
67
+ class UnauthenticatedError extends Error {
68
+ name = 'UnauthenticatedError';
69
+ constructor(message) {
70
+ super('LoginFailed: ' + message);
71
+ }
72
+ }
73
+ class DisconnectedError extends Error {
74
+ name = 'DisconnectedError';
75
+ constructor(message) {
76
+ super('Disconnected: ' + message);
77
+ }
78
+ }
79
+ function rethrowMeaningfulError(error, wrapIfUnknown = false) {
80
+ if (isUnauthenticated(error))
81
+ throw new UnauthenticatedError(error.message);
82
+ if (isConnectionProblem(error))
83
+ throw new DisconnectedError(error.message);
84
+ if (isTimeoutOrCancelError(error))
85
+ throw new Aborted(error);
86
+ if (wrapIfUnknown)
87
+ throw new Error(error.message, { cause: error });
88
+ else
89
+ throw error;
90
+ }
91
+
92
+ export { DisconnectedError, PlError, PlErrorCodeNotFound, RecoverablePlError, UnauthenticatedError, UnrecoverablePlError, isConnectionProblem, isNotFoundError, isTimeoutOrCancelError, isUnauthenticated, rethrowMeaningfulError, throwPlNotFoundError };
93
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sources":["../../src/core/errors.ts"],"sourcesContent":["import type { Status } from '../proto/github.com/googleapis/googleapis/google/rpc/status';\nimport { Aborted } from '@milaboratories/ts-helpers';\n\nexport function isConnectionProblem(err: unknown, nested: boolean = false): boolean {\n if (err instanceof DisconnectedError) return true;\n if ((err as any).name == 'RpcError' && (err as any).code == 'UNAVAILABLE') return true;\n if ((err as any).cause !== undefined && !nested)\n // nested limits the depth of search\n return isConnectionProblem((err as any).cause, true);\n return false;\n}\n\nexport function isUnauthenticated(err: unknown, nested: boolean = false): boolean {\n if (err instanceof UnauthenticatedError) return true;\n if ((err as any).name == 'RpcError' && (err as any).code == 'UNAUTHENTICATED') return true;\n if ((err as any).cause !== undefined && !nested)\n // nested limits the depth of search\n return isUnauthenticated((err as any).cause, true);\n return false;\n}\n\nexport function isTimeoutOrCancelError(err: unknown, nested: boolean = false): boolean {\n if (err instanceof Aborted || (err as any).name == 'AbortError') return true;\n if ((err as any).code == 'ABORT_ERR') return true;\n if (\n (err as any).name == 'RpcError'\n && ((err as any).code == 'CANCELLED' || (err as any).code == 'DEADLINE_EXCEEDED')\n )\n return true;\n if ((err as any).cause !== undefined && !nested)\n // nested limits the depth of search\n return isTimeoutOrCancelError((err as any).cause, true);\n return false;\n}\n\nexport const PlErrorCodeNotFound = 5;\n\nexport class PlError extends Error {\n name = 'PlError';\n constructor(public readonly status: Status) {\n super(`code=${status.code} ${status.message}`);\n }\n}\n\nexport function throwPlNotFoundError(message: string): never {\n throw new RecoverablePlError({ code: PlErrorCodeNotFound, message, details: [] });\n}\n\nexport class RecoverablePlError extends PlError {\n name = 'RecoverablePlError';\n constructor(status: Status) {\n super(status);\n }\n}\n\nexport class UnrecoverablePlError extends PlError {\n name = 'UnrecoverablePlError';\n constructor(status: Status) {\n super(status);\n }\n}\n\nexport function isNotFoundError(err: unknown, nested: boolean = false): boolean {\n if ((err as any).name == 'RpcError' && (err as any).code == 'NOT_FOUND') return true;\n if ((err as any).cause !== undefined && !nested) return isNotFoundError((err as any).cause, true);\n return err instanceof RecoverablePlError && err.status.code === PlErrorCodeNotFound;\n}\n\nexport class UnauthenticatedError extends Error {\n name = 'UnauthenticatedError';\n constructor(message: string) {\n super('LoginFailed: ' + message);\n }\n}\n\nexport class DisconnectedError extends Error {\n name = 'DisconnectedError';\n constructor(message: string) {\n super('Disconnected: ' + message);\n }\n}\n\nexport function rethrowMeaningfulError(error: any, wrapIfUnknown: boolean = false): never {\n if (isUnauthenticated(error)) throw new UnauthenticatedError(error.message);\n if (isConnectionProblem(error)) throw new DisconnectedError(error.message);\n if (isTimeoutOrCancelError(error)) throw new Aborted(error);\n if (wrapIfUnknown) throw new Error(error.message, { cause: error });\n else throw error;\n}\n"],"names":[],"mappings":";;SAGgB,mBAAmB,CAAC,GAAY,EAAE,SAAkB,KAAK,EA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