@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,134 @@
1
+ import { cachedDeserialize, notEmpty } from '@milaboratories/ts-helpers';
2
+
3
+ const NullResourceId = 0n;
4
+ function isNullResourceId(resourceId) {
5
+ return resourceId === NullResourceId;
6
+ }
7
+ function isNotNullResourceId(resourceId) {
8
+ return resourceId !== NullResourceId;
9
+ }
10
+ function ensureResourceIdNotNull(resourceId) {
11
+ if (!isNotNullResourceId(resourceId))
12
+ throw new Error('null resource id');
13
+ return resourceId;
14
+ }
15
+ function isAnyResourceId(resourceId) {
16
+ return resourceId !== 0n;
17
+ }
18
+ function resourceType(name, version) {
19
+ return { name, version };
20
+ }
21
+ function resourceTypeToString(rt) {
22
+ return `${rt.name}:${rt.version}`;
23
+ }
24
+ function resourceTypesEqual(type1, type2) {
25
+ return type1.name === type2.name && type1.version === type2.version;
26
+ }
27
+ function extractBasicResourceData(rd) {
28
+ const { id, originalResourceId, kind, type, data, error, inputsLocked, outputsLocked, resourceReady, final, } = rd;
29
+ return {
30
+ id,
31
+ originalResourceId,
32
+ kind,
33
+ type,
34
+ data,
35
+ error,
36
+ inputsLocked,
37
+ outputsLocked,
38
+ resourceReady,
39
+ final,
40
+ };
41
+ }
42
+ const jsonToData = (data) => Buffer.from(JSON.stringify(data));
43
+ const resDataToJson = (res) => cachedDeserialize(notEmpty(res.data));
44
+ function getField(r, name) {
45
+ return notEmpty(r.fields.find((f) => f.name === name));
46
+ }
47
+ //
48
+ // Local / Global ResourceId arithmetics
49
+ //
50
+ // Note: txId and other numerical values are made numbers but not bigint intentionally,
51
+ // after implementing security model based on signed resource ids this will make
52
+ // much more sense
53
+ const ResourceIdRootMask = 1n << 63n;
54
+ const ResourceIdLocalMask = 1n << 62n;
55
+ const NoFlagsIdMask = 0x3fffffffffffffffn;
56
+ const LocalResourceIdTxIdOffset = 24n;
57
+ const MaxLocalId = 0xffffff;
58
+ const MaxTxId = 0xffffffff;
59
+ /** Mask valid after applying shift */
60
+ const TxIdMask = BigInt(MaxTxId);
61
+ const LocalIdMask = BigInt(MaxLocalId);
62
+ // /** Basically removes embedded tx id */
63
+ // const LocalIdCleanMask = 0xFF00000000FFFFFFn;
64
+ function isRootResourceId(id) {
65
+ return (id & ResourceIdRootMask) !== 0n;
66
+ }
67
+ function isLocalResourceId(id) {
68
+ return (id & ResourceIdLocalMask) !== 0n;
69
+ }
70
+ function createLocalResourceId(isRoot, localCounterValue, localTxId) {
71
+ if (localCounterValue > MaxLocalId
72
+ || localTxId > MaxTxId
73
+ || localCounterValue < 0
74
+ || localTxId <= 0)
75
+ throw Error('wrong local id or tx id');
76
+ return ((isRoot ? ResourceIdRootMask : 0n)
77
+ | ResourceIdLocalMask
78
+ | BigInt(localCounterValue)
79
+ | (BigInt(localTxId) << LocalResourceIdTxIdOffset));
80
+ }
81
+ function createGlobalResourceId(isRoot, unmaskedId) {
82
+ return ((isRoot ? ResourceIdRootMask : 0n) | unmaskedId);
83
+ }
84
+ function extractTxId(localResourceId) {
85
+ return Number((localResourceId >> LocalResourceIdTxIdOffset) & TxIdMask);
86
+ }
87
+ function checkLocalityOfResourceId(resourceId, expectedTxId) {
88
+ if (!isLocalResourceId(resourceId))
89
+ return;
90
+ if (extractTxId(resourceId) !== expectedTxId)
91
+ throw Error('local id from another transaction, globalize id before leaking it from the transaction');
92
+ }
93
+ function resourceIdToString(resourceId) {
94
+ if (isNullResourceId(resourceId))
95
+ return 'XX:0x0';
96
+ if (isLocalResourceId(resourceId))
97
+ return ((isRootResourceId(resourceId) ? 'R' : 'N')
98
+ + 'L:0x'
99
+ + (LocalIdMask & resourceId).toString(16)
100
+ + '[0x'
101
+ + extractTxId(resourceId).toString(16)
102
+ + ']');
103
+ else
104
+ return ((isRootResourceId(resourceId) ? 'R' : 'N')
105
+ + 'G:0x'
106
+ + (NoFlagsIdMask & resourceId).toString(16));
107
+ }
108
+ const resourceIdRegexp = /^(?:(?<xx>XX)|(?<rn>[XRN])(?<lg>[XLG])):0x(?<rid>[0-9a-fA-F]+)(?:\[0x(?<txid>[0-9a-fA-F]+)])?$/;
109
+ function resourceIdFromString(str) {
110
+ const match = str.match(resourceIdRegexp);
111
+ if (match === null)
112
+ return undefined;
113
+ const { xx, rn, lg, rid, txid } = match.groups;
114
+ if (xx)
115
+ return NullResourceId;
116
+ if (lg === 'L')
117
+ return createLocalResourceId(rn === 'R', Number.parseInt(rid, 16), Number.parseInt(txid, 16));
118
+ else
119
+ return createGlobalResourceId(rn === 'R', BigInt('0x' + rid));
120
+ }
121
+ /** Converts bigint to global resource id */
122
+ function bigintToResourceId(resourceId) {
123
+ if (isLocalResourceId(resourceId))
124
+ throw new Error(`Local resource id: ${resourceIdToString(resourceId)}`);
125
+ if (isNullResourceId(resourceId))
126
+ throw new Error(`Null resource id.`);
127
+ return resourceId;
128
+ }
129
+ function stringifyWithResourceId(object) {
130
+ return JSON.stringify(object, (key, value) => typeof value === 'bigint' ? resourceIdToString(value) : value);
131
+ }
132
+
133
+ export { MaxLocalId, MaxTxId, NullResourceId, bigintToResourceId, checkLocalityOfResourceId, createGlobalResourceId, createLocalResourceId, ensureResourceIdNotNull, extractBasicResourceData, extractTxId, getField, isAnyResourceId, isLocalResourceId, isNotNullResourceId, isNullResourceId, isRootResourceId, jsonToData, resDataToJson, resourceIdFromString, resourceIdToString, resourceType, resourceTypeToString, resourceTypesEqual, stringifyWithResourceId };
134
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sources":["../../src/core/types.ts"],"sourcesContent":["import { cachedDeserialize, notEmpty } from '@milaboratories/ts-helpers';\n\n// more details here: https://egghead.io/blog/using-branded-types-in-typescript\ndeclare const __resource_id_type__: unique symbol;\ntype BrandResourceId<B> = bigint & { [__resource_id_type__]: B };\n\n/** Global resource id */\nexport type ResourceId = BrandResourceId<'global'>;\n\n/** Null resource id */\nexport type NullResourceId = BrandResourceId<'null'>;\n\n/** Local resource id */\nexport type LocalResourceId = BrandResourceId<'local'>;\n\n/** Any non-null resource id */\nexport type AnyResourceId = ResourceId | LocalResourceId;\n\n/** Any resource id */\nexport type OptionalResourceId = NullResourceId | ResourceId;\n\n/** All possible resource flavours */\nexport type OptionalAnyResourceId = NullResourceId | ResourceId | LocalResourceId;\n\nexport const NullResourceId = 0n as NullResourceId;\n\nexport function isNullResourceId(resourceId: bigint): resourceId is NullResourceId {\n return resourceId === NullResourceId;\n}\n\nexport function isNotNullResourceId(resourceId: OptionalResourceId): resourceId is ResourceId {\n return resourceId !== NullResourceId;\n}\n\nexport function ensureResourceIdNotNull(resourceId: OptionalResourceId): ResourceId {\n if (!isNotNullResourceId(resourceId)) throw new Error('null resource id');\n return resourceId;\n}\n\nexport function isAnyResourceId(resourceId: bigint): resourceId is AnyResourceId {\n return resourceId !== 0n;\n}\n\n// see local / global resource logic below...\n\nexport type ResourceKind = 'Structural' | 'Value';\n\nexport type FieldType = 'Input' | 'Output' | 'Service' | 'OTW' | 'Dynamic' | 'MTW';\n\nexport type FutureFieldType = 'Output' | 'Input' | 'Service';\n\nexport type FieldStatus = 'Empty' | 'Assigned' | 'Resolved';\n\nexport interface ResourceType {\n readonly name: string;\n readonly version: string;\n}\n\nexport function resourceType(name: string, version: string): ResourceType {\n return { name, version };\n}\n\nexport function resourceTypeToString(rt: ResourceType): string {\n return `${rt.name}:${rt.version}`;\n}\n\nexport function resourceTypesEqual(type1: ResourceType, type2: ResourceType): boolean {\n return type1.name === type2.name && type1.version === type2.version;\n}\n\n/** Readonly fields here marks properties of resource that can't change according to pl's state machine. */\nexport type BasicResourceData = {\n readonly id: ResourceId;\n readonly originalResourceId: OptionalResourceId;\n\n readonly kind: ResourceKind;\n readonly type: ResourceType;\n\n readonly data?: Uint8Array;\n\n readonly error: OptionalResourceId;\n\n readonly inputsLocked: boolean;\n readonly outputsLocked: boolean;\n readonly resourceReady: boolean;\n\n /** This value is derived from resource state by the server and can be used as\n * a robust criteria to determine resource is in final state. */\n readonly final: boolean;\n};\n\nexport function extractBasicResourceData(rd: ResourceData): BasicResourceData {\n const {\n id,\n originalResourceId,\n kind,\n type,\n data,\n error,\n inputsLocked,\n outputsLocked,\n resourceReady,\n final,\n } = rd;\n return {\n id,\n originalResourceId,\n kind,\n type,\n data,\n error,\n inputsLocked,\n outputsLocked,\n resourceReady,\n final,\n };\n}\n\nexport const jsonToData = (data: unknown) => Buffer.from(JSON.stringify(data));\n\nexport const resDataToJson = (res: ResourceData) => cachedDeserialize(notEmpty(res.data));\n\nexport type ResourceData = BasicResourceData & {\n readonly fields: FieldData[];\n};\n\nexport function getField(r: ResourceData, name: string): FieldData {\n return notEmpty(r.fields.find((f) => f.name === name));\n}\n\nexport type FieldData = {\n readonly name: string;\n readonly type: FieldType;\n readonly status: FieldStatus;\n readonly value: OptionalResourceId;\n readonly error: OptionalResourceId;\n\n /** True if value the fields points to is in final state. */\n readonly valueIsFinal: boolean;\n};\n\n//\n// Local / Global ResourceId arithmetics\n//\n\n// Note: txId and other numerical values are made numbers but not bigint intentionally,\n// after implementing security model based on signed resource ids this will make\n// much more sense\n\nconst ResourceIdRootMask = 1n << 63n;\nconst ResourceIdLocalMask = 1n << 62n;\nconst NoFlagsIdMask = 0x3fffffffffffffffn;\nconst LocalResourceIdTxIdOffset = 24n;\nexport const MaxLocalId = 0xffffff;\nexport const MaxTxId = 0xffffffff;\n/** Mask valid after applying shift */\nconst TxIdMask = BigInt(MaxTxId);\nconst LocalIdMask = BigInt(MaxLocalId);\n\n// /** Basically removes embedded tx id */\n// const LocalIdCleanMask = 0xFF00000000FFFFFFn;\n\nexport function isRootResourceId(id: bigint) {\n return (id & ResourceIdRootMask) !== 0n;\n}\n\nexport function isLocalResourceId(id: bigint): id is LocalResourceId {\n return (id & ResourceIdLocalMask) !== 0n;\n}\n\nexport function createLocalResourceId(\n isRoot: boolean,\n localCounterValue: number,\n localTxId: number,\n): LocalResourceId {\n if (\n localCounterValue > MaxLocalId\n || localTxId > MaxTxId\n || localCounterValue < 0\n || localTxId <= 0\n )\n throw Error('wrong local id or tx id');\n return ((isRoot ? ResourceIdRootMask : 0n)\n | ResourceIdLocalMask\n | BigInt(localCounterValue)\n | (BigInt(localTxId) << LocalResourceIdTxIdOffset)) as LocalResourceId;\n}\n\nexport function createGlobalResourceId(isRoot: boolean, unmaskedId: bigint): ResourceId {\n return ((isRoot ? ResourceIdRootMask : 0n) | unmaskedId) as ResourceId;\n}\n\nexport function extractTxId(localResourceId: LocalResourceId): number {\n return Number((localResourceId >> LocalResourceIdTxIdOffset) & TxIdMask);\n}\n\nexport function checkLocalityOfResourceId(resourceId: AnyResourceId, expectedTxId: number): void {\n if (!isLocalResourceId(resourceId)) return;\n if (extractTxId(resourceId) !== expectedTxId)\n throw Error(\n 'local id from another transaction, globalize id before leaking it from the transaction',\n );\n}\n\nexport function resourceIdToString(resourceId: OptionalAnyResourceId): string {\n if (isNullResourceId(resourceId)) return 'XX:0x0';\n if (isLocalResourceId(resourceId))\n return (\n (isRootResourceId(resourceId) ? 'R' : 'N')\n + 'L:0x'\n + (LocalIdMask & resourceId).toString(16)\n + '[0x'\n + extractTxId(resourceId).toString(16)\n + ']'\n );\n else\n return (\n (isRootResourceId(resourceId) ? 'R' : 'N')\n + 'G:0x'\n + (NoFlagsIdMask & resourceId).toString(16)\n );\n}\n\nconst resourceIdRegexp\n = /^(?:(?<xx>XX)|(?<rn>[XRN])(?<lg>[XLG])):0x(?<rid>[0-9a-fA-F]+)(?:\\[0x(?<txid>[0-9a-fA-F]+)])?$/;\n\nexport function resourceIdFromString(str: string): OptionalAnyResourceId | undefined {\n const match = str.match(resourceIdRegexp);\n if (match === null) return undefined;\n const { xx, rn, lg, rid, txid } = match.groups!;\n if (xx) return NullResourceId;\n if (lg === 'L')\n return createLocalResourceId(rn === 'R', Number.parseInt(rid, 16), Number.parseInt(txid, 16));\n else return createGlobalResourceId(rn === 'R', BigInt('0x' + rid));\n}\n\n/** Converts bigint to global resource id */\nexport function bigintToResourceId(resourceId: bigint): ResourceId {\n if (isLocalResourceId(resourceId))\n throw new Error(`Local resource id: ${resourceIdToString(resourceId)}`);\n if (isNullResourceId(resourceId)) throw new Error(`Null resource id.`);\n return resourceId as ResourceId;\n}\n\nexport function stringifyWithResourceId(object: unknown): string {\n return JSON.stringify(object, (key, value) =>\n typeof value === 'bigint' ? resourceIdToString(value as OptionalAnyResourceId) : value,\n );\n}\n"],"names":[],"mappings":";;AAwBO,MAAM,cAAc,GAAG;AAExB,SAAU,gBAAgB,CAAC,UAAkB,EAAA;IACjD,OAAO,UAAU,KAAK,cAAc;AACtC;AAEM,SAAU,mBAAmB,CAAC,UAA8B,EAAA;IAChE,OAAO,UAAU,KAAK,cAAc;AACtC;AAEM,SAAU,uBAAuB,CAAC,UAA8B,EAAA;AACpE,IAAA,IAAI,CAAC,mBAAmB,CAAC,UAAU,CAAC;AAAE,QAAA,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC;AACzE,IAAA,OAAO,UAAU;AACnB;AAEM,SAAU,eAAe,CAAC,UAAkB,EAAA;IAChD,OAAO,UAAU,KAAK,EAAE;AAC1B;AAiBM,SAAU,YAAY,CAAC,IAAY,EAAE,OAAe,EAAA;AACxD,IAAA,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE;AAC1B;AAEM,SAAU,oBAAoB,CAAC,EAAgB,EAAA;IACnD,OAAO,CAAA,EAAG,EAAE,CAAC,IAAI,IAAI,EAAE,CAAC,OAAO,CAAA,CAAE;AACnC;AAEM,SAAU,kBAAkB,CAAC,KAAmB,EAAE,K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D,IAAA,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,KAAK,KACvC,OAAO,KAAK,KAAK,QAAQ,GAAG,kBAAkB,CAAC,KAA8B,CAAC,GAAG,KAAK,CACvF;AACH;;;;"}
@@ -0,0 +1,43 @@
1
+ 'use strict';
2
+
3
+ var ll_client = require('./ll_client.cjs');
4
+ var tsHelpers = require('@milaboratories/ts-helpers');
5
+ var errors = require('./errors.cjs');
6
+
7
+ /** Primarily used for initial authentication (login) */
8
+ class UnauthenticatedPlClient {
9
+ ll;
10
+ constructor(configOrAddress) {
11
+ this.ll = new ll_client.LLPlClient(configOrAddress);
12
+ }
13
+ async ping() {
14
+ return (await this.ll.grpcPl.get().ping({})).response;
15
+ }
16
+ async authMethods() {
17
+ return (await this.ll.grpcPl.get().authMethods({})).response;
18
+ }
19
+ async requireAuth() {
20
+ return (await this.authMethods()).methods.length > 0;
21
+ }
22
+ async login(user, password) {
23
+ try {
24
+ const response = await this.ll.grpcPl.get().getJWTToken({ expiration: { seconds: BigInt(this.ll.conf.authTTLSeconds), nanos: 0 } }, {
25
+ meta: {
26
+ authorization: 'Basic ' + Buffer.from(user + ':' + password).toString('base64'),
27
+ },
28
+ }).response;
29
+ const jwtToken = tsHelpers.notEmpty(response.token);
30
+ if (jwtToken === '')
31
+ throw new Error('empty token');
32
+ return { jwtToken };
33
+ }
34
+ catch (e) {
35
+ if (e.code === 'UNAUTHENTICATED')
36
+ throw new errors.UnauthenticatedError(e.message);
37
+ throw new Error(e);
38
+ }
39
+ }
40
+ }
41
+
42
+ exports.UnauthenticatedPlClient = UnauthenticatedPlClient;
43
+ //# sourceMappingURL=unauth_client.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unauth_client.cjs","sources":["../../src/core/unauth_client.ts"],"sourcesContent":["import type { AuthInformation, PlClientConfig } from './config';\nimport type {\n AuthAPI_ListMethods_Response,\n MaintenanceAPI_Ping_Response,\n} from '../proto/github.com/milaboratory/pl/plapi/plapiproto/api';\nimport { LLPlClient } from './ll_client';\nimport { notEmpty } from '@milaboratories/ts-helpers';\nimport { UnauthenticatedError } from './errors';\n\n/** Primarily used for initial authentication (login) */\nexport class UnauthenticatedPlClient {\n public readonly ll: LLPlClient;\n\n constructor(configOrAddress: PlClientConfig | string) {\n this.ll = new LLPlClient(configOrAddress);\n }\n\n public async ping(): Promise<MaintenanceAPI_Ping_Response> {\n return (await this.ll.grpcPl.get().ping({})).response;\n }\n\n public async authMethods(): Promise<AuthAPI_ListMethods_Response> {\n return (await this.ll.grpcPl.get().authMethods({})).response;\n }\n\n public async requireAuth(): Promise<boolean> {\n return (await this.authMethods()).methods.length > 0;\n }\n\n public async login(user: string, password: string): Promise<AuthInformation> {\n try {\n const response = await this.ll.grpcPl.get().getJWTToken(\n { expiration: { seconds: BigInt(this.ll.conf.authTTLSeconds), nanos: 0 } },\n {\n meta: {\n authorization: 'Basic ' + Buffer.from(user + ':' + password).toString('base64'),\n },\n },\n ).response;\n const jwtToken = notEmpty(response.token);\n if (jwtToken === '') throw new Error('empty token');\n return { jwtToken };\n } catch (e: any) {\n if (e.code === 'UNAUTHENTICATED') throw new UnauthenticatedError(e.message);\n throw new Error(e);\n }\n }\n}\n"],"names":["LLPlClient","notEmpty","UnauthenticatedError"],"mappings":";;;;;;AASA;MACa,uBAAuB,CAAA;AAClB,IAAA,EAAE;AAElB,IAAA,WAAA,CAAY,eAAwC,EAAA;QAClD,IAAI,CAAC,EAAE,GAAG,IAAIA,oBAAU,CAAC,eAAe,CAAC;IAC3C;AAEO,IAAA,MAAM,IAAI,GAAA;AACf,QAAA,OAAO,CAAC,MAAM,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,QAAQ;IACvD;AAEO,IAAA,MAAM,WAAW,GAAA;AACtB,QAAA,OAAO,CAAC,MAAM,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,WAAW,CAAC,EAAE,CAAC,EAAE,QAAQ;IAC9D;AAEO,IAAA,MAAM,WAAW,GAAA;AACtB,QAAA,OAAO,CAAC,MAAM,IAAI,CAAC,WAAW,EAAE,EAAE,OAAO,CAAC,MAAM,GAAG,CAAC;IACtD;AAEO,IAAA,MAAM,KAAK,CAAC,IAAY,EAAE,QAAgB,EAAA;AAC/C,QAAA,IAAI;AACF,YAAA,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,WAAW,CACrD,EAAE,UAAU,EAAE,EAAE,OAAO,EAAE,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,cAAc,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,EAC1E;AACE,gBAAA,IAAI,EAAE;AACJ,oBAAA,aAAa,EAAE,QAAQ,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,GAAG,GAAG,GAAG,QAAQ,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC;AAChF,iBAAA;aACF,CACF,CAAC,QAAQ;YACV,MAAM,QAAQ,GAAGC,kBAAQ,CAAC,QAAQ,CAAC,KAAK,CAAC;YACzC,IAAI,QAAQ,KAAK,EAAE;AAAE,gBAAA,MAAM,IAAI,KAAK,CAAC,aAAa,CAAC;YACnD,OAAO,EAAE,QAAQ,EAAE;QACrB;QAAE,OAAO,CAAM,EAAE;AACf,YAAA,IAAI,CAAC,CAAC,IAAI,KAAK,iBAAiB;AAAE,gBAAA,MAAM,IAAIC,2BAAoB,CAAC,CAAC,CAAC,OAAO,CAAC;AAC3E,YAAA,MAAM,IAAI,KAAK,CAAC,CAAC,CAAC;QACpB;IACF;AACD;;;;"}
@@ -10,4 +10,3 @@ export declare class UnauthenticatedPlClient {
10
10
  requireAuth(): Promise<boolean>;
11
11
  login(user: string, password: string): Promise<AuthInformation>;
12
12
  }
13
- //# sourceMappingURL=unauth_client.d.ts.map
@@ -0,0 +1,41 @@
1
+ import { LLPlClient } from './ll_client.js';
2
+ import { notEmpty } from '@milaboratories/ts-helpers';
3
+ import { UnauthenticatedError } from './errors.js';
4
+
5
+ /** Primarily used for initial authentication (login) */
6
+ class UnauthenticatedPlClient {
7
+ ll;
8
+ constructor(configOrAddress) {
9
+ this.ll = new LLPlClient(configOrAddress);
10
+ }
11
+ async ping() {
12
+ return (await this.ll.grpcPl.get().ping({})).response;
13
+ }
14
+ async authMethods() {
15
+ return (await this.ll.grpcPl.get().authMethods({})).response;
16
+ }
17
+ async requireAuth() {
18
+ return (await this.authMethods()).methods.length > 0;
19
+ }
20
+ async login(user, password) {
21
+ try {
22
+ const response = await this.ll.grpcPl.get().getJWTToken({ expiration: { seconds: BigInt(this.ll.conf.authTTLSeconds), nanos: 0 } }, {
23
+ meta: {
24
+ authorization: 'Basic ' + Buffer.from(user + ':' + password).toString('base64'),
25
+ },
26
+ }).response;
27
+ const jwtToken = notEmpty(response.token);
28
+ if (jwtToken === '')
29
+ throw new Error('empty token');
30
+ return { jwtToken };
31
+ }
32
+ catch (e) {
33
+ if (e.code === 'UNAUTHENTICATED')
34
+ throw new UnauthenticatedError(e.message);
35
+ throw new Error(e);
36
+ }
37
+ }
38
+ }
39
+
40
+ export { UnauthenticatedPlClient };
41
+ //# sourceMappingURL=unauth_client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unauth_client.js","sources":["../../src/core/unauth_client.ts"],"sourcesContent":["import type { AuthInformation, PlClientConfig } from './config';\nimport type {\n AuthAPI_ListMethods_Response,\n MaintenanceAPI_Ping_Response,\n} from '../proto/github.com/milaboratory/pl/plapi/plapiproto/api';\nimport { LLPlClient } from './ll_client';\nimport { notEmpty } from '@milaboratories/ts-helpers';\nimport { UnauthenticatedError } from './errors';\n\n/** Primarily used for initial authentication (login) */\nexport class UnauthenticatedPlClient {\n public readonly ll: LLPlClient;\n\n constructor(configOrAddress: PlClientConfig | string) {\n this.ll = new LLPlClient(configOrAddress);\n }\n\n public async ping(): Promise<MaintenanceAPI_Ping_Response> {\n return (await this.ll.grpcPl.get().ping({})).response;\n }\n\n public async authMethods(): Promise<AuthAPI_ListMethods_Response> {\n return (await this.ll.grpcPl.get().authMethods({})).response;\n }\n\n public async requireAuth(): Promise<boolean> {\n return (await this.authMethods()).methods.length > 0;\n }\n\n public async login(user: string, password: string): Promise<AuthInformation> {\n try {\n const response = await this.ll.grpcPl.get().getJWTToken(\n { expiration: { seconds: BigInt(this.ll.conf.authTTLSeconds), nanos: 0 } },\n {\n meta: {\n authorization: 'Basic ' + Buffer.from(user + ':' + password).toString('base64'),\n },\n },\n ).response;\n const jwtToken = notEmpty(response.token);\n if (jwtToken === '') throw new Error('empty token');\n return { jwtToken };\n } catch (e: any) {\n if (e.code === 'UNAUTHENTICATED') throw new UnauthenticatedError(e.message);\n throw new Error(e);\n }\n }\n}\n"],"names":[],"mappings":";;;;AASA;MACa,uBAAuB,CAAA;AAClB,IAAA,EAAE;AAElB,IAAA,WAAA,CAAY,eAAwC,EAAA;QAClD,IAAI,CAAC,EAAE,GAAG,IAAI,UAAU,CAAC,eAAe,CAAC;IAC3C;AAEO,IAAA,MAAM,IAAI,GAAA;AACf,QAAA,OAAO,CAAC,MAAM,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,QAAQ;IACvD;AAEO,IAAA,MAAM,WAAW,GAAA;AACtB,QAAA,OAAO,CAAC,MAAM,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,WAAW,CAAC,EAAE,CAAC,EAAE,QAAQ;IAC9D;AAEO,IAAA,MAAM,WAAW,GAAA;AACtB,QAAA,OAAO,CAAC,MAAM,IAAI,CAAC,WAAW,EAAE,EAAE,OAAO,CAAC,MAAM,GAAG,CAAC;IACtD;AAEO,IAAA,MAAM,KAAK,CAAC,IAAY,EAAE,QAAgB,EAAA;AAC/C,QAAA,IAAI;AACF,YAAA,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,WAAW,CACrD,EAAE,UAAU,EAAE,EAAE,OAAO,EAAE,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,cAAc,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,EAC1E;AACE,gBAAA,IAAI,EAAE;AACJ,oBAAA,aAAa,EAAE,QAAQ,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,GAAG,GAAG,GAAG,QAAQ,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC;AAChF,iBAAA;aACF,CACF,CAAC,QAAQ;YACV,MAAM,QAAQ,GAAG,QAAQ,CAAC,QAAQ,CAAC,KAAK,CAAC;YACzC,IAAI,QAAQ,KAAK,EAAE;AAAE,gBAAA,MAAM,IAAI,KAAK,CAAC,aAAa,CAAC;YACnD,OAAO,EAAE,QAAQ,EAAE;QACrB;QAAE,OAAO,CAAM,EAAE;AACf,YAAA,IAAI,CAAC,CAAC,IAAI,KAAK,iBAAiB;AAAE,gBAAA,MAAM,IAAI,oBAAoB,CAAC,CAAC,CAAC,OAAO,CAAC;AAC3E,YAAA,MAAM,IAAI,KAAK,CAAC,CAAC,CAAC;QACpB;IACF;AACD;;;;"}
@@ -0,0 +1,124 @@
1
+ 'use strict';
2
+
3
+ var transaction = require('../core/transaction.cjs');
4
+
5
+ /**
6
+ * @packageDocumentation
7
+ *
8
+ * This file is exported under Pl "namespace" to the client users.
9
+ *
10
+ * It defines well known pl types, and methods to manipulate them.
11
+ *
12
+ */
13
+ function rt(name, version) {
14
+ return { name, version };
15
+ }
16
+ const ClientRoot = rt('ClientRoot', '1');
17
+ const StructTestResource = rt('StructTest', '1');
18
+ const ValueTestResource = rt('ValueTest', '1');
19
+ const JsonString = rt('json/string', '1');
20
+ const JsonBool = rt('json/bool', '1');
21
+ const JsonObject = rt('json/object', '1');
22
+ const JsonGzObject = rt('json-gz/object', '1');
23
+ const JsonArray = rt('json/array', '1');
24
+ const JsonNumber = rt('json/number', '1');
25
+ const JsonNull = rt('json/null', '1');
26
+ const RNull = rt('Null', '1');
27
+ const EphStdMap = rt('EphStdMap', '1');
28
+ const StdMap = rt('StdMap', '1');
29
+ //
30
+ // Standard value resources
31
+ //
32
+ function createPlNull(tx) {
33
+ return tx.createValue(JsonNull, Buffer.from(JSON.stringify(null)));
34
+ }
35
+ function createPlBool(tx, val) {
36
+ return tx.createValue(JsonBool, Buffer.from(JSON.stringify(val)));
37
+ }
38
+ function createPlNumber(tx, val) {
39
+ return tx.createValue(JsonNumber, Buffer.from(JSON.stringify(val)));
40
+ }
41
+ function createPlString(tx, val) {
42
+ return tx.createValue(JsonString, Buffer.from(JSON.stringify(val)));
43
+ }
44
+ function createPlArray(tx, val) {
45
+ return tx.createValue(JsonArray, Buffer.from(JSON.stringify(val)));
46
+ }
47
+ function createPlObject(tx, val) {
48
+ return tx.createValue(JsonObject, Buffer.from(JSON.stringify(val)));
49
+ }
50
+ function plEntry(key, ref) {
51
+ return [key, ref];
52
+ }
53
+ function plEntries(record, fields) {
54
+ return fields === undefined
55
+ ? Object.entries(record)
56
+ : fields.map((key) => plEntry(key, record[key]));
57
+ }
58
+ /** Helper method to build standard pl map from a set of entries */
59
+ function createPlMap(tx, entries, ephemeral, type) {
60
+ const actualType = type ?? (ephemeral ? EphStdMap : StdMap);
61
+ const rId = ephemeral ? tx.createEphemeral(actualType) : tx.createStruct(actualType);
62
+ for (const [name, value] of Array.isArray(entries) ? entries : plEntries(entries))
63
+ tx.createField(transaction.field(rId, name), 'Input', value);
64
+ tx.lock(rId);
65
+ return rId;
66
+ }
67
+ function futureRecord(tx, rId, keys, fieldType, prefix = '') {
68
+ return Object.fromEntries(keys.map((k) => plEntry(k, tx.getFutureFieldValue(rId, `${prefix}${k}`, fieldType))));
69
+ }
70
+ //
71
+ // Holder
72
+ //
73
+ /** Name of the field in block holder, that references the actual block-pack. */
74
+ const Holder = StdMap;
75
+ const EphHolder = EphStdMap;
76
+ const HolderRefField = 'ref';
77
+ function wrapInHolder(tx, ref) {
78
+ const holder = tx.createStruct(Holder);
79
+ const mainHolderField = transaction.field(holder, HolderRefField);
80
+ tx.createField(mainHolderField, 'Input', ref);
81
+ tx.lock(holder);
82
+ return holder;
83
+ }
84
+ function wrapInEphHolder(tx, ref) {
85
+ const holder = tx.createEphemeral(EphHolder);
86
+ const mainHolderField = transaction.field(holder, HolderRefField);
87
+ tx.createField(mainHolderField, 'Input', ref);
88
+ tx.lock(holder);
89
+ return holder;
90
+ }
91
+ function unwrapHolder(tx, ref) {
92
+ return tx.getFutureFieldValue(ref, HolderRefField, 'Input');
93
+ }
94
+
95
+ exports.ClientRoot = ClientRoot;
96
+ exports.EphHolder = EphHolder;
97
+ exports.EphStdMap = EphStdMap;
98
+ exports.Holder = Holder;
99
+ exports.HolderRefField = HolderRefField;
100
+ exports.JsonArray = JsonArray;
101
+ exports.JsonBool = JsonBool;
102
+ exports.JsonGzObject = JsonGzObject;
103
+ exports.JsonNull = JsonNull;
104
+ exports.JsonNumber = JsonNumber;
105
+ exports.JsonObject = JsonObject;
106
+ exports.JsonString = JsonString;
107
+ exports.RNull = RNull;
108
+ exports.StdMap = StdMap;
109
+ exports.StructTestResource = StructTestResource;
110
+ exports.ValueTestResource = ValueTestResource;
111
+ exports.createPlArray = createPlArray;
112
+ exports.createPlBool = createPlBool;
113
+ exports.createPlMap = createPlMap;
114
+ exports.createPlNull = createPlNull;
115
+ exports.createPlNumber = createPlNumber;
116
+ exports.createPlObject = createPlObject;
117
+ exports.createPlString = createPlString;
118
+ exports.futureRecord = futureRecord;
119
+ exports.plEntries = plEntries;
120
+ exports.plEntry = plEntry;
121
+ exports.unwrapHolder = unwrapHolder;
122
+ exports.wrapInEphHolder = wrapInEphHolder;
123
+ exports.wrapInHolder = wrapInHolder;
124
+ //# sourceMappingURL=pl.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pl.cjs","sources":["../../src/helpers/pl.ts"],"sourcesContent":["/**\n * @packageDocumentation\n *\n * This file is exported under Pl \"namespace\" to the client users.\n *\n * It defines well known pl types, and methods to manipulate them.\n *\n */\n\nimport type { FutureFieldType, ResourceType } from '../core/types';\nimport type { AnyRef, FieldRef, PlTransaction, ResourceRef } from '../core/transaction';\nimport { field } from '../core/transaction';\n\nfunction rt(name: string, version: string): ResourceType {\n return { name, version };\n}\n\nexport const ClientRoot = rt('ClientRoot', '1');\n\nexport const StructTestResource = rt('StructTest', '1');\nexport const ValueTestResource = rt('ValueTest', '1');\n\nexport const JsonString = rt('json/string', '1');\nexport const JsonBool = rt('json/bool', '1');\nexport const JsonObject = rt('json/object', '1');\nexport const JsonGzObject = rt('json-gz/object', '1');\nexport const JsonArray = rt('json/array', '1');\nexport const JsonNumber = rt('json/number', '1');\nexport const JsonNull = rt('json/null', '1');\n\nexport const RNull = rt('Null', '1');\n\nexport const EphStdMap: ResourceType = rt('EphStdMap', '1');\nexport const StdMap: ResourceType = rt('StdMap', '1');\n\n//\n// Standard value resources\n//\n\nexport function createPlNull(tx: PlTransaction): ResourceRef {\n return tx.createValue(JsonNull, Buffer.from(JSON.stringify(null)));\n}\n\nexport function createPlBool(tx: PlTransaction, val: boolean): ResourceRef {\n return tx.createValue(JsonBool, Buffer.from(JSON.stringify(val)));\n}\n\nexport function createPlNumber(tx: PlTransaction, val: number): ResourceRef {\n return tx.createValue(JsonNumber, Buffer.from(JSON.stringify(val)));\n}\n\nexport function createPlString(tx: PlTransaction, val: string): ResourceRef {\n return tx.createValue(JsonString, Buffer.from(JSON.stringify(val)));\n}\n\nexport function createPlArray(tx: PlTransaction, val: any[]): ResourceRef {\n return tx.createValue(JsonArray, Buffer.from(JSON.stringify(val)));\n}\n\nexport function createPlObject(tx: PlTransaction, val: object): ResourceRef {\n return tx.createValue(JsonObject, Buffer.from(JSON.stringify(val)));\n}\n\n//\n// Pl Map\n//\n\nexport type PlRecordEntry<Key extends string = string, Ref extends AnyRef = AnyRef> = [Key, Ref];\n\nexport type PlRecord<Key extends string = string, Ref extends AnyRef = AnyRef> = Record<Key, Ref>;\n\nexport function plEntry<Key extends string = string, Ref extends AnyRef = AnyRef>(\n key: Key,\n ref: Ref,\n): PlRecordEntry<Key, Ref> {\n return [key, ref];\n}\n\nexport function plEntries<Key extends string = string, Ref extends AnyRef = AnyRef>(\n record: PlRecord<Key, Ref>,\n fields?: Key[],\n): PlRecordEntry<Key, Ref>[] {\n return fields === undefined\n ? (Object.entries(record) as PlRecordEntry<Key, Ref>[])\n : fields.map((key) => plEntry(key, record[key]));\n}\n\n/** Helper method to build standard pl map from a set of entries */\nexport function createPlMap(\n tx: PlTransaction,\n entries: PlRecordEntry[] | PlRecord,\n ephemeral: boolean,\n type?: ResourceType,\n): ResourceRef {\n const actualType = type ?? (ephemeral ? EphStdMap : StdMap);\n const rId = ephemeral ? tx.createEphemeral(actualType) : tx.createStruct(actualType);\n\n for (const [name, value] of Array.isArray(entries) ? entries : plEntries(entries))\n tx.createField(field(rId, name), 'Input', value);\n\n tx.lock(rId);\n\n return rId;\n}\n\nexport function futureRecord<Key extends string>(\n tx: PlTransaction,\n rId: AnyRef,\n keys: Key[],\n fieldType: FutureFieldType,\n prefix: string = '',\n): PlRecord<Key, FieldRef> {\n return Object.fromEntries(\n keys.map((k) => plEntry(k, tx.getFutureFieldValue(rId, `${prefix}${k}`, fieldType))),\n ) as PlRecord<Key, FieldRef>;\n}\n\n//\n// Holder\n//\n\n/** Name of the field in block holder, that references the actual block-pack. */\nexport const Holder = StdMap;\nexport const EphHolder = EphStdMap;\nexport const HolderRefField = 'ref';\n\nexport function wrapInHolder(tx: PlTransaction, ref: AnyRef): ResourceRef {\n const holder = tx.createStruct(Holder);\n const mainHolderField = field(holder, HolderRefField);\n tx.createField(mainHolderField, 'Input', ref);\n tx.lock(holder);\n return holder;\n}\n\nexport function wrapInEphHolder(tx: PlTransaction, ref: AnyRef): ResourceRef {\n const holder = tx.createEphemeral(EphHolder);\n const mainHolderField = field(holder, HolderRefField);\n tx.createField(mainHolderField, 'Input', ref);\n tx.lock(holder);\n return holder;\n}\n\nexport function unwrapHolder(tx: PlTransaction, ref: AnyRef): FieldRef {\n return tx.getFutureFieldValue(ref, HolderRefField, 'Input');\n}\n"],"names":["field"],"mappings":";;;;AAAA;;;;;;;AAOG;AAMH,SAAS,EAAE,CAAC,IAAY,EAAE,OAAe,EAAA;AACvC,IAAA,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE;AAC1B;AAEO,MAAM,UAAU,GAAG,EAAE,CAAC,YAAY,EAAE,GAAG;AAEvC,MAAM,kBAAkB,GAAG,EAAE,CAAC,YAAY,EAAE,GAAG;AAC/C,MAAM,iBAAiB,GAAG,EAAE,CAAC,WAAW,EAAE,GAAG;AAE7C,MAAM,UAAU,GAAG,EAAE,CAAC,aAAa,EAAE,GAAG;AACxC,MAAM,QAAQ,GAAG,EAAE,CAAC,WAAW,EAAE,GAAG;AACpC,MAAM,UAAU,GAAG,EAAE,CAAC,aAAa,EAAE,GAAG;AACxC,MAAM,YAAY,GAAG,EAAE,CAAC,gBAAgB,EAAE,GAAG;AAC7C,MAAM,SAAS,GAAG,EAAE,CAAC,YAAY,EAAE,GAAG;AACtC,MAAM,UAAU,GAAG,EAAE,CAAC,aAAa,EAAE,GAAG;AACxC,MAAM,QAAQ,GAAG,EAAE,CAAC,WAAW,EAAE,GAAG;AAEpC,MAAM,KAAK,GAAG,EAAE,CAAC,MAAM,EAAE,GAAG;AAE5B,MAAM,SAAS,GAAiB,EAAE,CAAC,WAAW,EAAE,GAAG;AACnD,MAAM,MAAM,GAAiB,EAAE,CAAC,QAAQ,EAAE,GAAG;AAEpD;AACA;AACA;AAEM,SAAU,YAAY,CAAC,EAAiB,EAAA;AAC5C,IAAA,OAAO,EAAE,CAAC,WAAW,CAAC,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC;AACpE;AAEM,SAAU,YAA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E,OAAO,EAAE,GAAG,CAAC;AAC7C,IAAA,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC;AACf,IAAA,OAAO,MAAM;AACf;AAEM,SAAU,eAAe,CAAC,EAAiB,EAAE,GAAW,EAAA;IAC5D,MAAM,MAAM,GAAG,EAAE,CAAC,eAAe,CAAC,SAAS,CAAC;IAC5C,MAAM,eAAe,GAAGA,iBAAK,CAAC,MAAM,EAAE,cAAc,CAAC;IACrD,EAAE,CAAC,WAAW,CAAC,eAAe,EAAE,OAAO,EAAE,GAAG,CAAC;AAC7C,IAAA,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC;AACf,IAAA,OAAO,MAAM;AACf;AAEM,SAAU,YAAY,CAAC,EAAiB,EAAE,GAAW,EAAA;IACzD,OAAO,EAAE,CAAC,mBAAmB,CAAC,GAAG,EAAE,cAAc,EAAE,OAAO,CAAC;AAC7D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -33,4 +33,3 @@ export declare const HolderRefField = "ref";
33
33
  export declare function wrapInHolder(tx: PlTransaction, ref: AnyRef): ResourceRef;
34
34
  export declare function wrapInEphHolder(tx: PlTransaction, ref: AnyRef): ResourceRef;
35
35
  export declare function unwrapHolder(tx: PlTransaction, ref: AnyRef): FieldRef;
36
- //# sourceMappingURL=pl.d.ts.map
@@ -0,0 +1,94 @@
1
+ import { field } from '../core/transaction.js';
2
+
3
+ /**
4
+ * @packageDocumentation
5
+ *
6
+ * This file is exported under Pl "namespace" to the client users.
7
+ *
8
+ * It defines well known pl types, and methods to manipulate them.
9
+ *
10
+ */
11
+ function rt(name, version) {
12
+ return { name, version };
13
+ }
14
+ const ClientRoot = rt('ClientRoot', '1');
15
+ const StructTestResource = rt('StructTest', '1');
16
+ const ValueTestResource = rt('ValueTest', '1');
17
+ const JsonString = rt('json/string', '1');
18
+ const JsonBool = rt('json/bool', '1');
19
+ const JsonObject = rt('json/object', '1');
20
+ const JsonGzObject = rt('json-gz/object', '1');
21
+ const JsonArray = rt('json/array', '1');
22
+ const JsonNumber = rt('json/number', '1');
23
+ const JsonNull = rt('json/null', '1');
24
+ const RNull = rt('Null', '1');
25
+ const EphStdMap = rt('EphStdMap', '1');
26
+ const StdMap = rt('StdMap', '1');
27
+ //
28
+ // Standard value resources
29
+ //
30
+ function createPlNull(tx) {
31
+ return tx.createValue(JsonNull, Buffer.from(JSON.stringify(null)));
32
+ }
33
+ function createPlBool(tx, val) {
34
+ return tx.createValue(JsonBool, Buffer.from(JSON.stringify(val)));
35
+ }
36
+ function createPlNumber(tx, val) {
37
+ return tx.createValue(JsonNumber, Buffer.from(JSON.stringify(val)));
38
+ }
39
+ function createPlString(tx, val) {
40
+ return tx.createValue(JsonString, Buffer.from(JSON.stringify(val)));
41
+ }
42
+ function createPlArray(tx, val) {
43
+ return tx.createValue(JsonArray, Buffer.from(JSON.stringify(val)));
44
+ }
45
+ function createPlObject(tx, val) {
46
+ return tx.createValue(JsonObject, Buffer.from(JSON.stringify(val)));
47
+ }
48
+ function plEntry(key, ref) {
49
+ return [key, ref];
50
+ }
51
+ function plEntries(record, fields) {
52
+ return fields === undefined
53
+ ? Object.entries(record)
54
+ : fields.map((key) => plEntry(key, record[key]));
55
+ }
56
+ /** Helper method to build standard pl map from a set of entries */
57
+ function createPlMap(tx, entries, ephemeral, type) {
58
+ const actualType = type ?? (ephemeral ? EphStdMap : StdMap);
59
+ const rId = ephemeral ? tx.createEphemeral(actualType) : tx.createStruct(actualType);
60
+ for (const [name, value] of Array.isArray(entries) ? entries : plEntries(entries))
61
+ tx.createField(field(rId, name), 'Input', value);
62
+ tx.lock(rId);
63
+ return rId;
64
+ }
65
+ function futureRecord(tx, rId, keys, fieldType, prefix = '') {
66
+ return Object.fromEntries(keys.map((k) => plEntry(k, tx.getFutureFieldValue(rId, `${prefix}${k}`, fieldType))));
67
+ }
68
+ //
69
+ // Holder
70
+ //
71
+ /** Name of the field in block holder, that references the actual block-pack. */
72
+ const Holder = StdMap;
73
+ const EphHolder = EphStdMap;
74
+ const HolderRefField = 'ref';
75
+ function wrapInHolder(tx, ref) {
76
+ const holder = tx.createStruct(Holder);
77
+ const mainHolderField = field(holder, HolderRefField);
78
+ tx.createField(mainHolderField, 'Input', ref);
79
+ tx.lock(holder);
80
+ return holder;
81
+ }
82
+ function wrapInEphHolder(tx, ref) {
83
+ const holder = tx.createEphemeral(EphHolder);
84
+ const mainHolderField = field(holder, HolderRefField);
85
+ tx.createField(mainHolderField, 'Input', ref);
86
+ tx.lock(holder);
87
+ return holder;
88
+ }
89
+ function unwrapHolder(tx, ref) {
90
+ return tx.getFutureFieldValue(ref, HolderRefField, 'Input');
91
+ }
92
+
93
+ export { ClientRoot, EphHolder, EphStdMap, Holder, HolderRefField, JsonArray, JsonBool, JsonGzObject, JsonNull, JsonNumber, JsonObject, JsonString, RNull, StdMap, StructTestResource, ValueTestResource, createPlArray, createPlBool, createPlMap, createPlNull, createPlNumber, createPlObject, createPlString, futureRecord, plEntries, plEntry, unwrapHolder, wrapInEphHolder, wrapInHolder };
94
+ //# sourceMappingURL=pl.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pl.js","sources":["../../src/helpers/pl.ts"],"sourcesContent":["/**\n * @packageDocumentation\n *\n * This file is exported under Pl \"namespace\" to the client users.\n *\n * It defines well known pl types, and methods to manipulate them.\n *\n */\n\nimport type { FutureFieldType, ResourceType } from '../core/types';\nimport type { AnyRef, FieldRef, PlTransaction, ResourceRef } from '../core/transaction';\nimport { field } from '../core/transaction';\n\nfunction rt(name: string, version: string): ResourceType {\n return { name, version };\n}\n\nexport const ClientRoot = rt('ClientRoot', '1');\n\nexport const StructTestResource = rt('StructTest', '1');\nexport const ValueTestResource = rt('ValueTest', '1');\n\nexport const JsonString = rt('json/string', '1');\nexport const JsonBool = rt('json/bool', '1');\nexport const JsonObject = rt('json/object', '1');\nexport const JsonGzObject = rt('json-gz/object', '1');\nexport const JsonArray = rt('json/array', '1');\nexport const JsonNumber = rt('json/number', '1');\nexport const JsonNull = rt('json/null', '1');\n\nexport const RNull = rt('Null', '1');\n\nexport const EphStdMap: ResourceType = rt('EphStdMap', '1');\nexport const StdMap: ResourceType = rt('StdMap', '1');\n\n//\n// Standard value resources\n//\n\nexport function createPlNull(tx: PlTransaction): ResourceRef {\n return tx.createValue(JsonNull, Buffer.from(JSON.stringify(null)));\n}\n\nexport function createPlBool(tx: PlTransaction, val: boolean): ResourceRef {\n return tx.createValue(JsonBool, Buffer.from(JSON.stringify(val)));\n}\n\nexport function createPlNumber(tx: PlTransaction, val: number): ResourceRef {\n return tx.createValue(JsonNumber, Buffer.from(JSON.stringify(val)));\n}\n\nexport function createPlString(tx: PlTransaction, val: string): ResourceRef {\n return tx.createValue(JsonString, Buffer.from(JSON.stringify(val)));\n}\n\nexport function createPlArray(tx: PlTransaction, val: any[]): ResourceRef {\n return tx.createValue(JsonArray, Buffer.from(JSON.stringify(val)));\n}\n\nexport function createPlObject(tx: PlTransaction, val: object): ResourceRef {\n return tx.createValue(JsonObject, Buffer.from(JSON.stringify(val)));\n}\n\n//\n// Pl Map\n//\n\nexport type PlRecordEntry<Key extends string = string, Ref extends AnyRef = AnyRef> = [Key, Ref];\n\nexport type PlRecord<Key extends string = string, Ref extends AnyRef = AnyRef> = Record<Key, Ref>;\n\nexport function plEntry<Key extends string = string, Ref extends AnyRef = AnyRef>(\n key: Key,\n ref: Ref,\n): PlRecordEntry<Key, Ref> {\n return [key, ref];\n}\n\nexport function plEntries<Key extends string = string, Ref extends AnyRef = AnyRef>(\n record: PlRecord<Key, Ref>,\n fields?: Key[],\n): PlRecordEntry<Key, Ref>[] {\n return fields === undefined\n ? (Object.entries(record) as PlRecordEntry<Key, Ref>[])\n : fields.map((key) => plEntry(key, record[key]));\n}\n\n/** Helper method to build standard pl map from a set of entries */\nexport function createPlMap(\n tx: PlTransaction,\n entries: PlRecordEntry[] | PlRecord,\n ephemeral: boolean,\n type?: ResourceType,\n): ResourceRef {\n const actualType = type ?? (ephemeral ? EphStdMap : StdMap);\n const rId = ephemeral ? tx.createEphemeral(actualType) : tx.createStruct(actualType);\n\n for (const [name, value] of Array.isArray(entries) ? entries : plEntries(entries))\n tx.createField(field(rId, name), 'Input', value);\n\n tx.lock(rId);\n\n return rId;\n}\n\nexport function futureRecord<Key extends string>(\n tx: PlTransaction,\n rId: AnyRef,\n keys: Key[],\n fieldType: FutureFieldType,\n prefix: string = '',\n): PlRecord<Key, FieldRef> {\n return Object.fromEntries(\n keys.map((k) => plEntry(k, tx.getFutureFieldValue(rId, `${prefix}${k}`, fieldType))),\n ) as PlRecord<Key, FieldRef>;\n}\n\n//\n// Holder\n//\n\n/** Name of the field in block holder, that references the actual block-pack. */\nexport const Holder = StdMap;\nexport const EphHolder = EphStdMap;\nexport const HolderRefField = 'ref';\n\nexport function wrapInHolder(tx: PlTransaction, ref: AnyRef): ResourceRef {\n const holder = tx.createStruct(Holder);\n const mainHolderField = field(holder, HolderRefField);\n tx.createField(mainHolderField, 'Input', ref);\n tx.lock(holder);\n return holder;\n}\n\nexport function wrapInEphHolder(tx: PlTransaction, ref: AnyRef): ResourceRef {\n const holder = tx.createEphemeral(EphHolder);\n const mainHolderField = field(holder, HolderRefField);\n tx.createField(mainHolderField, 'Input', ref);\n tx.lock(holder);\n return holder;\n}\n\nexport function unwrapHolder(tx: PlTransaction, ref: AnyRef): FieldRef {\n return tx.getFutureFieldValue(ref, HolderRefField, 'Input');\n}\n"],"names":[],"mappings":";;AAAA;;;;;;;AAOG;AAMH,SAAS,EAAE,CAAC,IAAY,EAAE,OAAe,EAAA;AACvC,IAAA,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE;AAC1B;AAEO,MAAM,UAAU,GAAG,EAAE,CAAC,YAAY,EAAE,GAAG;AAEvC,MAAM,kBAAkB,GAAG,EAAE,CAAC,YAAY,EAAE,GAAG;AAC/C,MAAM,iBAAiB,GAAG,EAAE,CAAC,WAAW,EAAE,GAAG;AAE7C,MAAM,UAAU,GAAG,EAAE,CAAC,aAAa,EAAE,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