@ocap/proto 1.30.3 → 1.30.4

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.
package/lib/schema.js ADDED
@@ -0,0 +1,240 @@
1
+ /**
2
+ * `@ocap/proto/schema` — schema-only entry.
3
+ *
4
+ * Derives enums, message metadata, and typeUrls from `gen/spec.json` without
5
+ * touching any `*_pb.js` constructor. New Client SDK / Worker / Kernel builds
6
+ * import from here so bundlers can tree-shake the `google-protobuf` runtime
7
+ * and every generated `*_pb.js` out of the CBOR path.
8
+ *
9
+ * Public API (locked in phase_4_decisions q10):
10
+ * - Functions: getFields, getEnum, listTxTypes, getTypeUrl, fromTypeUrl
11
+ * - Constants: enums, typeUrls, messages, transactions, stakes, compactSpec
12
+ *
13
+ * Does NOT expose `getMessageType(t).fn` — that is a runtime concern
14
+ * (`@ocap/proto/runtime`) because pulling in the jspb constructor is what
15
+ * drags `*_pb.js` into the bundle.
16
+ *
17
+ * @module @ocap/proto/schema
18
+ */
19
+
20
+ // Encode path reads only the core partition; Request/Response/service descriptors
21
+ // stay out of bundles. `lib/runtime.js` merges `spec.rpc.json` on top for chain
22
+ // side consumers. See tools/split-spec.js for the partition contract.
23
+ //
24
+ // `structuredClone` is mandatory because `compactSpec` mutates its input.
25
+ // Node's require cache is shared with `lib/runtime.js`, so skipping the clone
26
+ // here would flatten the JSON under ocap.nested and break runtime's merge.
27
+ const rawSpec = structuredClone(require('./gen/spec.core.json'));
28
+
29
+ // Dotted-path lookup. Replaces lodash/get so the encode bundle stays off
30
+ // lodash; callers pass `vendor.Version` style keys (see getMessageType).
31
+ const getByPath = (obj, path) => {
32
+ if (!obj || !path) return undefined;
33
+ if (Object.prototype.hasOwnProperty.call(obj, path)) return obj[path];
34
+ return path.split('.').reduce((o, k) => (o == null ? o : o[k]), obj);
35
+ };
36
+
37
+ const txTypePattern = /Tx$/;
38
+ const stateTypePattern = /State$/;
39
+ const stakeTypePattern = /^StakeFor/i;
40
+ const requestTypePattern = /^Request/i;
41
+ const responseTypePattern = /^Response/i;
42
+
43
+ const lowerUnder = (x) =>
44
+ x
45
+ .split(/(?=[A-Z])/)
46
+ .join('_')
47
+ .toLowerCase();
48
+
49
+ const compactSpec = (object) => {
50
+ if (object.nested) {
51
+ object = object.nested;
52
+ }
53
+
54
+ Object.keys(object).forEach((x) => {
55
+ if (object[x] && typeof object[x] === 'object') {
56
+ object[x] = compactSpec(object[x]);
57
+ }
58
+ });
59
+
60
+ return object;
61
+ };
62
+
63
+ const spec = compactSpec(rawSpec);
64
+ const packageName = 'ocap';
65
+ const { [packageName]: abi } = spec;
66
+ Object.assign(spec, spec[packageName]);
67
+
68
+ const messages = {};
69
+ const enums = Object.keys(abi)
70
+ .filter((x) => abi[x].values)
71
+ .reduce((obj, x) => {
72
+ messages[x] = {};
73
+ obj[x] = Object.keys(abi[x].values).reduce((values, k) => {
74
+ values[k.toUpperCase()] = abi[x].values[k];
75
+ messages[x][abi[x].values[k]] = k.toUpperCase();
76
+ return values;
77
+ }, {});
78
+
79
+ return obj;
80
+ }, {});
81
+
82
+ function createTypeUrls(abi) {
83
+ return Object.keys(abi).reduce((obj, type) => {
84
+ let typeUrl = type;
85
+ if (!requestTypePattern.test(type) && !responseTypePattern.test(type)) {
86
+ if (txTypePattern.test(type)) {
87
+ typeUrl = `fg:t:${lowerUnder(type.replace(txTypePattern, ''))}`;
88
+ }
89
+ if (stateTypePattern.test(type)) {
90
+ typeUrl = `fg:s:${lowerUnder(type.replace(stateTypePattern, ''))}`;
91
+ }
92
+ if (stakeTypePattern.test(type)) {
93
+ typeUrl = `fg:x:${lowerUnder(`Stake${type.replace(stakeTypePattern, '')}`)}`;
94
+ }
95
+ if (['TransactionInfo'].includes(type)) {
96
+ typeUrl = `fg:x:${lowerUnder(type)}`;
97
+ }
98
+ if (type === 'AssetFactoryState') {
99
+ typeUrl = 'fg:s:asset_factory_state';
100
+ }
101
+ if (type === 'AssetFactory') {
102
+ typeUrl = 'fg:x:asset_factory';
103
+ }
104
+ if (type === 'DummyCodec') {
105
+ typeUrl = 'fg:x:address';
106
+ }
107
+ }
108
+
109
+ obj[type] = typeUrl;
110
+ return obj;
111
+ }, {});
112
+ }
113
+
114
+ const typeUrls = createTypeUrls(abi);
115
+
116
+ // `transactions` / `stakes` are derived from spec keys — no dependency on the
117
+ // generated `*_pb.js` constructors. Runtime.js also computes these via
118
+ // Object.keys(types); both paths converge on the same list because every tx /
119
+ // stake message appears in spec.json.
120
+ const transactions = Object.keys(abi).filter(
121
+ (x) => txTypePattern.test(x) && !requestTypePattern.test(x) && !responseTypePattern.test(x)
122
+ );
123
+ const stakes = Object.keys(abi).filter((x) => stakeTypePattern.test(x));
124
+
125
+ enums.SupportedTxs = transactions;
126
+ enums.SupportedStakes = stakes;
127
+
128
+ // Historical multisign subsets. These are runtime conventions rather than
129
+ // schema data (no equivalent field in spec.json), but the Client SDK needs
130
+ // them to wire up multi-sign extension methods and the list is independent of
131
+ // the jspb constructors — exposing it here keeps the client off `/runtime`.
132
+ const multiSignTxs = ['ExchangeV2Tx'].filter((x) => transactions.includes(x));
133
+ const multiSignV2Txs = [
134
+ 'TransferV3Tx',
135
+ 'AcquireAssetV3Tx',
136
+ 'StakeTx',
137
+ 'UpdateRollupTx',
138
+ 'JoinRollupTx',
139
+ 'LeaveRollupTx',
140
+ 'PauseRollupTx',
141
+ 'CloseRollupTx',
142
+ 'MigrateRollupTx',
143
+ 'ResumeRollupTx',
144
+ 'DepositTokenV2Tx',
145
+ 'WithdrawTokenV2Tx',
146
+ 'CreateRollupBlockTx',
147
+ 'ClaimBlockRewardTx',
148
+ 'CreateAssetTx',
149
+ 'ConsumeAssetTx',
150
+ 'MintTokenTx',
151
+ 'BurnTokenTx',
152
+ ].filter((x) => transactions.includes(x));
153
+
154
+ /**
155
+ * Look up proto field descriptors for a message type. Schema-only — no jspb
156
+ * constructor returned. Callers needing the constructor must use
157
+ * `@ocap/proto/runtime` `getMessageType(type).fn`.
158
+ *
159
+ * Returns an array of `{name, type, id, rule?}` so callers can iterate
160
+ * without relying on insertion order of the underlying object literal.
161
+ *
162
+ * @param {string} type - e.g. 'Transaction', 'TransferTx'
163
+ * @returns {Array<{name: string, type: string, id: number, rule?: string}>}
164
+ */
165
+ function getFields(type) {
166
+ const descriptor = getByPath(spec, type) || getByPath(spec, `vendor.${type}`) || {};
167
+ const fields = descriptor.fields;
168
+ if (!fields) {
169
+ return [];
170
+ }
171
+ return Object.keys(fields).map((name) => Object.assign({ name }, fields[name]));
172
+ }
173
+
174
+ /**
175
+ * Look up an enum descriptor (numeric values keyed by uppercased name).
176
+ *
177
+ * @param {string} name - e.g. 'KeyType', 'RoleType'
178
+ * @returns {object|undefined}
179
+ */
180
+ function getEnum(name) {
181
+ return enums[name];
182
+ }
183
+
184
+ /**
185
+ * List every transaction type name known to the schema.
186
+ *
187
+ * @returns {string[]}
188
+ */
189
+ function listTxTypes() {
190
+ return transactions.slice();
191
+ }
192
+
193
+ /**
194
+ * Schema-only `getMessageType` — returns `{ fields, oneofs }` for registry
195
+ * integration in `@ocap/message/provider`. Never returns `fn`; callers who
196
+ * need the jspb constructor must use `@ocap/proto/runtime`.
197
+ *
198
+ * Emits the raw spec.json object (keyed by field name) so the canonical CBOR
199
+ * encoder can iterate `Object.entries(fields)` the same way it does against
200
+ * the runtime provider. For a plain array, use `getFields(type)`.
201
+ *
202
+ * @param {string} type
203
+ * @returns {{fields?: object, oneofs?: object}}
204
+ */
205
+ function getMessageType(type) {
206
+ const descriptor = getByPath(spec, type) || getByPath(spec, `vendor.${type}`) || {};
207
+ return { fields: descriptor.fields, oneofs: descriptor.oneofs };
208
+ }
209
+
210
+ function getTypeUrl(type) {
211
+ return typeUrls[type] || type;
212
+ }
213
+
214
+ function fromTypeUrl(url) {
215
+ const found = Object.entries(typeUrls).find(([, value]) => value === url);
216
+ if (found) {
217
+ return found[0];
218
+ }
219
+
220
+ return url;
221
+ }
222
+
223
+ module.exports = {
224
+ enums,
225
+ messages,
226
+ typeUrls,
227
+ transactions,
228
+ stakes,
229
+ multiSignTxs,
230
+ multiSignV2Txs,
231
+ compactSpec,
232
+ getFields,
233
+ getEnum,
234
+ listTxTypes,
235
+ getMessageType,
236
+ getTypeUrl,
237
+ fromTypeUrl,
238
+ // backwards-compatible alias; `toTypeUrl` was the historic name
239
+ toTypeUrl: getTypeUrl,
240
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ocap/proto",
3
3
  "description": "Static modules generated from forge-sdk protobuf files",
4
- "version": "1.30.3",
4
+ "version": "1.30.4",
5
5
  "author": {
6
6
  "name": "wangshijun",
7
7
  "email": "shijun@arcblock.io",
@@ -26,6 +26,22 @@
26
26
  ],
27
27
  "license": "Apache-2.0",
28
28
  "main": "lib/index.js",
29
+ "types": "lib/index.d.ts",
30
+ "exports": {
31
+ ".": {
32
+ "types": "./lib/index.d.ts",
33
+ "default": "./lib/index.js"
34
+ },
35
+ "./schema": {
36
+ "types": "./lib/schema.d.ts",
37
+ "default": "./lib/schema.js"
38
+ },
39
+ "./runtime": {
40
+ "types": "./lib/runtime.d.ts",
41
+ "default": "./lib/runtime.js"
42
+ },
43
+ "./package.json": "./package.json"
44
+ },
29
45
  "files": [
30
46
  "lib"
31
47
  ],
@@ -37,15 +53,13 @@
37
53
  "compile": "make build && make build-deps",
38
54
  "lint": "biome check",
39
55
  "lint:fix": "biome check --write",
40
- "gen-proto": "node tools/gen-proto.js",
41
- "gen-dts": "j2d lib/index.js && node tools/gen-dts.js && bash tools/patch-dts.sh",
56
+ "gen-dts": "node tools/gen-dts.js && bash tools/patch-dts.sh",
42
57
  "strip-classes": "node tools/strip-classes.js",
43
58
  "test": "bun test",
44
59
  "coverage": "npm run test -- --coverage"
45
60
  },
46
61
  "dependencies": {
47
- "debug": "^4.4.3",
48
- "lodash": "^4.17.23"
62
+ "debug": "^4.4.3"
49
63
  },
50
64
  "gitHead": "87990c8b5e215107fc587c1ced0d6b3e2cd2483e"
51
65
  }