@smallpen/local-package 0.1.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,526 @@
1
+ // Minimal Kiwi schema runtime carried from the migration source. It decodes the
2
+ // schema embedded in Figma clipboard payloads and intentionally has no codec schema,
3
+ // network access, filesystem access, or third-party runtime dependency.
4
+ //#region src/schema-runtime/bb.ts
5
+ let int32 = /* @__PURE__ */ new Int32Array(1);
6
+ let float32 = new Float32Array(int32.buffer);
7
+ const textDecoder = new TextDecoder();
8
+ var ByteBuffer = class {
9
+ _data;
10
+ _index;
11
+ length;
12
+ constructor(data) {
13
+ if (data && !(data instanceof Uint8Array)) throw new Error("Must initialize a ByteBuffer with a Uint8Array");
14
+ this._data = data || /* @__PURE__ */ new Uint8Array(256);
15
+ this._index = 0;
16
+ this.length = data ? data.length : 0;
17
+ }
18
+ get offset() {
19
+ return this._index;
20
+ }
21
+ set offset(value) {
22
+ this._index = value;
23
+ }
24
+ /**
25
+ * Returns a view into the internal buffer, not a copy.
26
+ *
27
+ * Consumers transferring this Uint8Array across thread boundaries (e.g. via
28
+ * postMessage with transferables) MUST copy first: `new Uint8Array(buffer)`.
29
+ * Otherwise, if multiple Uint8Arrays share the same underlying ArrayBuffer and
30
+ * one is transferred, all views into that buffer become detached.
31
+ */
32
+ toUint8Array() {
33
+ return this._data.subarray(0, this.length);
34
+ }
35
+ readByte() {
36
+ return this._data[this._index++];
37
+ }
38
+ readByteArray() {
39
+ const length = this.readVarUint();
40
+ const start = this._index;
41
+ this._index = start + length;
42
+ return this._data.slice(start, start + length);
43
+ }
44
+ skipByteArray() {
45
+ const length = this.readVarUint();
46
+ this._index += length;
47
+ }
48
+ readVarFloat() {
49
+ const index = this._index;
50
+ const data = this._data;
51
+ const first = data[index];
52
+ if (first === 0) {
53
+ this._index = index + 1;
54
+ return 0;
55
+ }
56
+ let bits = first | data[index + 1] << 8 | data[index + 2] << 16 | data[index + 3] << 24;
57
+ this._index = index + 4;
58
+ bits = bits << 23 | bits >>> 9;
59
+ int32[0] = bits;
60
+ return float32[0];
61
+ }
62
+ readVarUint() {
63
+ const data = this._data;
64
+ let i = this._index;
65
+ let b = data[i++];
66
+ let value = b & 127;
67
+ if (b < 128) {
68
+ this._index = i;
69
+ return value;
70
+ }
71
+ b = data[i++];
72
+ value |= (b & 127) << 7;
73
+ if (b < 128) {
74
+ this._index = i;
75
+ return value;
76
+ }
77
+ b = data[i++];
78
+ value |= (b & 127) << 14;
79
+ if (b < 128) {
80
+ this._index = i;
81
+ return value;
82
+ }
83
+ b = data[i++];
84
+ value |= (b & 127) << 21;
85
+ if (b < 128) {
86
+ this._index = i;
87
+ return value;
88
+ }
89
+ b = data[i++];
90
+ value |= (b & 127) << 28;
91
+ this._index = i;
92
+ return value >>> 0;
93
+ }
94
+ readVarInt() {
95
+ let value = this.readVarUint() | 0;
96
+ return value & 1 ? ~(value >>> 1) : value >>> 1;
97
+ }
98
+ readVarUint64() {
99
+ let value = BigInt(0);
100
+ let shift = BigInt(0);
101
+ let seven = BigInt(7);
102
+ let byte;
103
+ while ((byte = this.readByte()) & 128 && shift < 56) {
104
+ value |= BigInt(byte & 127) << shift;
105
+ shift += seven;
106
+ }
107
+ value |= BigInt(byte) << shift;
108
+ return value;
109
+ }
110
+ readVarInt64() {
111
+ let value = this.readVarUint64();
112
+ let one = BigInt(1);
113
+ let sign = value & one;
114
+ value >>= one;
115
+ return sign ? ~value : value;
116
+ }
117
+ readString() {
118
+ const start = this._index;
119
+ const i = this.findStringTerminator(start);
120
+ this._index = i + 1;
121
+ return textDecoder.decode(this._data.subarray(start, i));
122
+ }
123
+ skipString() {
124
+ this._index = this.findStringTerminator(this._index) + 1;
125
+ }
126
+ findStringTerminator(start) {
127
+ const data = this._data;
128
+ let index = start;
129
+ while (index < data.length && data[index] !== 0) index++;
130
+ if (index >= data.length) throw new Error("Unterminated string in Kiwi message");
131
+ return index;
132
+ }
133
+ _growBy(amount) {
134
+ if (this.length + amount > this._data.length) {
135
+ let data = new Uint8Array(this.length + amount << 1);
136
+ data.set(this._data);
137
+ this._data = data;
138
+ }
139
+ this.length += amount;
140
+ }
141
+ writeByte(value) {
142
+ let index = this.length;
143
+ this._growBy(1);
144
+ this._data[index] = value;
145
+ }
146
+ writeByteArray(value) {
147
+ this.writeVarUint(value.length);
148
+ let index = this.length;
149
+ this._growBy(value.length);
150
+ this._data.set(value, index);
151
+ }
152
+ writeVarFloat(value) {
153
+ let index = this.length;
154
+ float32[0] = value;
155
+ let bits = int32[0];
156
+ bits = bits >>> 23 | bits << 9;
157
+ if ((bits & 255) === 0) {
158
+ this.writeByte(0);
159
+ return;
160
+ }
161
+ this._growBy(4);
162
+ let data = this._data;
163
+ data[index] = bits;
164
+ data[index + 1] = bits >> 8;
165
+ data[index + 2] = bits >> 16;
166
+ data[index + 3] = bits >> 24;
167
+ }
168
+ writeVarUint(value) {
169
+ if (value < 0 || value > 4294967295) throw new Error("Outside uint range: " + value);
170
+ do {
171
+ let byte = value & 127;
172
+ value >>>= 7;
173
+ this.writeByte(value ? byte | 128 : byte);
174
+ } while (value);
175
+ }
176
+ writeVarInt(value) {
177
+ if (value < -2147483648 || value > 2147483647) throw new Error("Outside int range: " + value);
178
+ this.writeVarUint((value << 1 ^ value >> 31) >>> 0);
179
+ }
180
+ writeVarUint64(value) {
181
+ if (typeof value === "string") value = BigInt(value);
182
+ else if (typeof value !== "bigint") throw new Error(`Expected bigint but got ${typeof value}: ${String(value)}`);
183
+ if (value < 0 || value > BigInt("0xFFFFFFFFFFFFFFFF")) throw new Error("Outside uint64 range: " + value);
184
+ let mask = BigInt(127);
185
+ let seven = BigInt(7);
186
+ for (let i = 0; value > mask && i < 8; i++) {
187
+ this.writeByte(Number(value & mask) | 128);
188
+ value >>= seven;
189
+ }
190
+ this.writeByte(Number(value));
191
+ }
192
+ writeVarInt64(value) {
193
+ if (typeof value === "string") value = BigInt(value);
194
+ else if (typeof value !== "bigint") throw new Error(`Expected bigint but got ${typeof value}: ${String(value)}`);
195
+ if (value < -BigInt("0x8000000000000000") || value > BigInt("0x7FFFFFFFFFFFFFFF")) throw new Error("Outside int64 range: " + value);
196
+ let one = BigInt(1);
197
+ this.writeVarUint64(value < 0 ? ~(value << one) : value << one);
198
+ }
199
+ writeString(value) {
200
+ let codePoint;
201
+ for (let i = 0; i < value.length; i++) {
202
+ let a = value.charCodeAt(i);
203
+ if (i + 1 === value.length || a < 55296 || a >= 56320) codePoint = a;
204
+ else {
205
+ let b = value.charCodeAt(++i);
206
+ codePoint = (a << 10) + b + -56613888;
207
+ }
208
+ if (codePoint === 0) throw new Error("Cannot encode a string containing the null character");
209
+ if (codePoint < 128) this.writeByte(codePoint);
210
+ else {
211
+ if (codePoint < 2048) this.writeByte(codePoint >> 6 & 31 | 192);
212
+ else {
213
+ if (codePoint < 65536) this.writeByte(codePoint >> 12 & 15 | 224);
214
+ else {
215
+ this.writeByte(codePoint >> 18 & 7 | 240);
216
+ this.writeByte(codePoint >> 12 & 63 | 128);
217
+ }
218
+ this.writeByte(codePoint >> 6 & 63 | 128);
219
+ }
220
+ this.writeByte(codePoint & 63 | 128);
221
+ }
222
+ }
223
+ this.writeByte(0);
224
+ }
225
+ };
226
+ //#endregion
227
+ //#region src/schema-runtime/util.ts
228
+ function quote(text) {
229
+ return JSON.stringify(text);
230
+ }
231
+ function error(text, line, column) {
232
+ var error = new Error(text);
233
+ error.line = line;
234
+ error.column = column;
235
+ throw error;
236
+ }
237
+ //#endregion
238
+ //#region src/schema-runtime/js.ts
239
+ function compileDecode(definition, definitions) {
240
+ let lines = [];
241
+ let indent = " ";
242
+ lines.push("function (bb) {");
243
+ lines.push(" var result = {};");
244
+ lines.push(" if (!(bb instanceof this.ByteBuffer)) {");
245
+ lines.push(" bb = new this.ByteBuffer(bb);");
246
+ lines.push(" }");
247
+ lines.push("");
248
+ if (definition.kind === "MESSAGE") {
249
+ lines.push(" while (true) {");
250
+ lines.push(" switch (bb.readVarUint()) {");
251
+ lines.push(" case 0:");
252
+ lines.push(" return result;");
253
+ lines.push("");
254
+ indent = " ";
255
+ }
256
+ for (let i = 0; i < definition.fields.length; i++) {
257
+ let field = definition.fields[i];
258
+ let code;
259
+ switch (field.type) {
260
+ case "bool":
261
+ code = "!!bb.readByte()";
262
+ break;
263
+ case "byte":
264
+ code = "bb.readByte()";
265
+ break;
266
+ case "int":
267
+ code = "bb.readVarInt()";
268
+ break;
269
+ case "uint":
270
+ code = "bb.readVarUint()";
271
+ break;
272
+ case "float":
273
+ code = "bb.readVarFloat()";
274
+ break;
275
+ case "string":
276
+ code = "bb.readString()";
277
+ break;
278
+ case "int64":
279
+ code = "bb.readVarInt64()";
280
+ break;
281
+ case "uint64":
282
+ code = "bb.readVarUint64()";
283
+ break;
284
+ default: {
285
+ let type = definitions[field.type];
286
+ if (!type) error("Invalid type " + quote(field.type) + " for field " + quote(field.name), field.line, field.column);
287
+ else if (type.kind === "ENUM") code = "this[" + quote(type.name) + "][bb.readVarUint()]";
288
+ else code = "this[" + quote("decode" + type.name) + "](bb)";
289
+ }
290
+ }
291
+ if (definition.kind === "MESSAGE") lines.push(" case " + field.value + ":");
292
+ if (field.isArray) {
293
+ if (field.isDeprecated) {
294
+ if (field.type === "byte") lines.push(indent + "bb.readByteArray();");
295
+ else {
296
+ lines.push(indent + "var length = bb.readVarUint();");
297
+ lines.push(indent + "while (length-- > 0) " + code + ";");
298
+ }
299
+ } else if (field.type === "byte") lines.push(indent + "result[" + quote(field.name) + "] = bb.readByteArray();");
300
+ else {
301
+ lines.push(indent + "var length = bb.readVarUint();");
302
+ lines.push(indent + "var values = result[" + quote(field.name) + "] = Array(length);");
303
+ lines.push(indent + "for (var i = 0; i < length; i++) values[i] = " + code + ";");
304
+ }
305
+ } else if (field.isDeprecated) lines.push(indent + code + ";");
306
+ else lines.push(indent + "result[" + quote(field.name) + "] = " + code + ";");
307
+ if (definition.kind === "MESSAGE") {
308
+ lines.push(" break;");
309
+ lines.push("");
310
+ }
311
+ }
312
+ if (definition.kind === "MESSAGE") {
313
+ lines.push(" default:");
314
+ lines.push(" throw new Error(\"Attempted to parse invalid message\");");
315
+ lines.push(" }");
316
+ lines.push(" }");
317
+ } else lines.push(" return result;");
318
+ lines.push("}");
319
+ return lines.join("\n");
320
+ }
321
+ function compileEncode(definition, definitions) {
322
+ let lines = [];
323
+ lines.push("function (message, bb) {");
324
+ lines.push(" var isTopLevel = !bb;");
325
+ lines.push(" if (isTopLevel) bb = new this.ByteBuffer();");
326
+ for (let j = 0; j < definition.fields.length; j++) {
327
+ let field = definition.fields[j];
328
+ let code;
329
+ if (field.isDeprecated) continue;
330
+ switch (field.type) {
331
+ case "bool":
332
+ code = "bb.writeByte(value);";
333
+ break;
334
+ case "byte":
335
+ code = "bb.writeByte(value);";
336
+ break;
337
+ case "int":
338
+ code = "bb.writeVarInt(value);";
339
+ break;
340
+ case "uint":
341
+ code = "bb.writeVarUint(value);";
342
+ break;
343
+ case "float":
344
+ code = "bb.writeVarFloat(value);";
345
+ break;
346
+ case "string":
347
+ code = "bb.writeString(value);";
348
+ break;
349
+ case "int64":
350
+ code = "bb.writeVarInt64(value);";
351
+ break;
352
+ case "uint64":
353
+ code = "bb.writeVarUint64(value);";
354
+ break;
355
+ default: {
356
+ let type = definitions[field.type];
357
+ if (!type) throw new Error("Invalid type " + quote(field.type) + " for field " + quote(field.name));
358
+ else if (type.kind === "ENUM") code = "var encoded = this[" + quote(type.name) + "][value]; if (encoded === void 0) throw new Error(\"Invalid value \" + JSON.stringify(value) + " + quote(" for enum " + quote(type.name)) + "); bb.writeVarUint(encoded);";
359
+ else code = "this[" + quote("encode" + type.name) + "](value, bb);";
360
+ }
361
+ }
362
+ lines.push("");
363
+ lines.push(" var value = message[" + quote(field.name) + "];");
364
+ lines.push(" if (value != null) {");
365
+ if (definition.kind === "MESSAGE") lines.push(" bb.writeVarUint(" + field.value + ");");
366
+ if (field.isArray) {
367
+ if (field.type === "byte") lines.push(" bb.writeByteArray(value);");
368
+ else {
369
+ lines.push(" var values = value, n = values.length;");
370
+ lines.push(" bb.writeVarUint(n);");
371
+ lines.push(" for (var i = 0; i < n; i++) {");
372
+ lines.push(" value = values[i];");
373
+ lines.push(" " + code);
374
+ lines.push(" }");
375
+ }
376
+ } else lines.push(" " + code);
377
+ if (definition.kind === "STRUCT") {
378
+ lines.push(" } else {");
379
+ lines.push(" throw new Error(" + quote("Missing required field " + quote(field.name)) + ");");
380
+ }
381
+ lines.push(" }");
382
+ }
383
+ if (definition.kind === "MESSAGE") lines.push(" bb.writeVarUint(0);");
384
+ lines.push("");
385
+ lines.push(" if (isTopLevel) return bb.toUint8Array();");
386
+ lines.push("}");
387
+ return lines.join("\n");
388
+ }
389
+ function compileSchemaJS(schema) {
390
+ let definitions = {};
391
+ let name = schema.package;
392
+ let js = [];
393
+ if (name !== null) js.push("var " + name + " = exports || " + name + " || {}, exports;");
394
+ else {
395
+ js.push("var exports = exports || {};");
396
+ name = "exports";
397
+ }
398
+ js.push(name + ".ByteBuffer = " + name + ".ByteBuffer || require(\"kiwi-schema\").ByteBuffer;");
399
+ for (let i = 0; i < schema.definitions.length; i++) {
400
+ let definition = schema.definitions[i];
401
+ definitions[definition.name] = definition;
402
+ }
403
+ for (let i = 0; i < schema.definitions.length; i++) {
404
+ let definition = schema.definitions[i];
405
+ switch (definition.kind) {
406
+ case "ENUM": {
407
+ let value = {};
408
+ for (let j = 0; j < definition.fields.length; j++) {
409
+ let field = definition.fields[j];
410
+ value[field.name] = field.value;
411
+ value[field.value] = field.name;
412
+ }
413
+ js.push(name + "[" + quote(definition.name) + "] = " + JSON.stringify(value, null, 2) + ";");
414
+ break;
415
+ }
416
+ case "STRUCT":
417
+ case "MESSAGE":
418
+ js.push("");
419
+ js.push(name + "[" + quote("decode" + definition.name) + "] = " + compileDecode(definition, definitions) + ";");
420
+ js.push("");
421
+ js.push(name + "[" + quote("encode" + definition.name) + "] = " + compileEncode(definition, definitions) + ";");
422
+ break;
423
+ default: error("Invalid definition kind " + quote(definition.kind), definition.line, definition.column);
424
+ }
425
+ }
426
+ js.push("");
427
+ return js.join("\n");
428
+ }
429
+ function compileSchema(schema) {
430
+ let result = { ByteBuffer };
431
+ new Function("exports", compileSchemaJS(schema))(result);
432
+ return result;
433
+ }
434
+ //#endregion
435
+ //#region src/schema-runtime/binary.ts
436
+ let types = [
437
+ "bool",
438
+ "byte",
439
+ "int",
440
+ "uint",
441
+ "float",
442
+ "string",
443
+ "int64",
444
+ "uint64"
445
+ ];
446
+ let kinds = [
447
+ "ENUM",
448
+ "STRUCT",
449
+ "MESSAGE"
450
+ ];
451
+ function decodeBinarySchema(buffer) {
452
+ let bb = buffer instanceof ByteBuffer ? buffer : new ByteBuffer(buffer);
453
+ let definitionCount = bb.readVarUint();
454
+ let definitions = [];
455
+ for (let i = 0; i < definitionCount; i++) {
456
+ let definitionName = bb.readString();
457
+ let kind = bb.readByte();
458
+ let fieldCount = bb.readVarUint();
459
+ let fields = [];
460
+ for (let j = 0; j < fieldCount; j++) {
461
+ let fieldName = bb.readString();
462
+ let type = bb.readVarInt();
463
+ let isArray = !!(bb.readByte() & 1);
464
+ let value = bb.readVarUint();
465
+ fields.push({
466
+ name: fieldName,
467
+ line: 0,
468
+ column: 0,
469
+ type: kinds[kind] === "ENUM" ? null : type,
470
+ isArray,
471
+ isDeprecated: false,
472
+ value
473
+ });
474
+ }
475
+ definitions.push({
476
+ name: definitionName,
477
+ line: 0,
478
+ column: 0,
479
+ kind: kinds[kind],
480
+ fields
481
+ });
482
+ }
483
+ for (let i = 0; i < definitionCount; i++) {
484
+ let fields = definitions[i].fields;
485
+ for (let j = 0; j < fields.length; j++) {
486
+ let field = fields[j];
487
+ let type = field.type;
488
+ if (type !== null && type < 0) {
489
+ if (~type >= types.length) throw new Error("Invalid type " + type);
490
+ field.type = types[~type];
491
+ } else {
492
+ if (type !== null && type >= definitions.length) throw new Error("Invalid type " + type);
493
+ field.type = type === null ? null : definitions[type].name;
494
+ }
495
+ }
496
+ }
497
+ return {
498
+ package: null,
499
+ definitions
500
+ };
501
+ }
502
+ function encodeBinarySchema(schema) {
503
+ let bb = new ByteBuffer();
504
+ let definitions = schema.definitions;
505
+ let definitionIndex = {};
506
+ bb.writeVarUint(definitions.length);
507
+ for (let i = 0; i < definitions.length; i++) definitionIndex[definitions[i].name] = i;
508
+ for (let i = 0; i < definitions.length; i++) {
509
+ let definition = definitions[i];
510
+ bb.writeString(definition.name);
511
+ bb.writeByte(kinds.indexOf(definition.kind));
512
+ bb.writeVarUint(definition.fields.length);
513
+ for (let j = 0; j < definition.fields.length; j++) {
514
+ let field = definition.fields[j];
515
+ let type = types.indexOf(field.type);
516
+ bb.writeString(field.name);
517
+ bb.writeVarInt(type === -1 ? definitionIndex[field.type] : ~type);
518
+ bb.writeByte(field.isArray ? 1 : 0);
519
+ bb.writeVarUint(field.value);
520
+ }
521
+ }
522
+ return bb.toUint8Array();
523
+ }
524
+ //#endregion
525
+
526
+ export { ByteBuffer, compileSchema, decodeBinarySchema, encodeBinarySchema };