@pairdraw/kiwi 0.0.0-stage → 0.16.0
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/LICENSE +21 -0
- package/NOTICE +28 -0
- package/README.md +112 -2
- package/dist/chunks/fig/codec.js +1371 -0
- package/dist/chunks/fig/codec.js.map +1 -0
- package/dist/fig/codec.d.ts +477 -0
- package/dist/fig/codec.js +2 -0
- package/dist/fig/container.d.ts +48 -0
- package/dist/fig/container.js +2 -0
- package/dist/fig/guid.d.ts +13 -0
- package/dist/fig/guid.js +31 -0
- package/dist/fig/guid.js.map +1 -0
- package/dist/fig/parse.d.ts +33 -0
- package/dist/fig/parse.js +83 -0
- package/dist/fig/parse.js.map +1 -0
- package/dist/fig.d.ts +185 -0
- package/dist/fig.js +117 -0
- package/dist/fig.js.map +1 -0
- package/dist/index.d.ts +182 -0
- package/dist/page-manifest.d.ts +16 -0
- package/dist/schema-runtime.d.ts +2 -0
- package/dist/schema-runtime.js +2 -0
- package/dist/types.d.ts +26 -0
- package/package.json +52 -4
- package/src/fig/codec.ts +536 -0
- package/src/fig/container.ts +180 -0
- package/src/fig/guid.ts +24 -0
- package/src/fig/index.ts +5 -0
- package/src/fig/page-manifest.ts +149 -0
- package/src/fig/parse.ts +136 -0
- package/src/fig/protocol.ts +237 -0
- package/src/fig/schema/fig.kiwi +5628 -0
- package/src/fig/schema.ts +7 -0
- package/src/fig/types.ts +25 -0
- package/src/fig/variable-bindings.ts +11 -0
- package/src/global.d.ts +4 -0
- package/src/schema-runtime/bb.ts +284 -0
- package/src/schema-runtime/binary.ts +102 -0
- package/src/schema-runtime/index.ts +25 -0
- package/src/schema-runtime/interpreter.ts +465 -0
- package/src/schema-runtime/parser.ts +287 -0
- package/src/schema-runtime/schema.ts +24 -0
- package/src/schema-runtime/util.ts +10 -0
- package/src/schema-runtime/validate.ts +88 -0
package/src/fig/types.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export interface GUID {
|
|
2
|
+
sessionID: number
|
|
3
|
+
localID: number
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface Color {
|
|
7
|
+
r: number
|
|
8
|
+
g: number
|
|
9
|
+
b: number
|
|
10
|
+
a: number
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface Vector {
|
|
14
|
+
x: number
|
|
15
|
+
y: number
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface Matrix {
|
|
19
|
+
m00: number
|
|
20
|
+
m01: number
|
|
21
|
+
m02: number
|
|
22
|
+
m10: number
|
|
23
|
+
m11: number
|
|
24
|
+
m12: number
|
|
25
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { GUID } from './types'
|
|
2
|
+
|
|
3
|
+
/** Figma's plugin-facing variable id, as `VariableID:<session>:<local>`. */
|
|
4
|
+
export function parseVariableId(variableId: string): GUID | null {
|
|
5
|
+
const match = variableId.match(/VariableID:(\d+):(\d+)/)
|
|
6
|
+
if (!match) return null
|
|
7
|
+
return {
|
|
8
|
+
sessionID: Number.parseInt(match.at(1) ?? '0', 10),
|
|
9
|
+
localID: Number.parseInt(match.at(2) ?? '0', 10)
|
|
10
|
+
}
|
|
11
|
+
}
|
package/src/global.d.ts
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
let int32 = new Int32Array(1)
|
|
2
|
+
let float32 = new Float32Array(int32.buffer)
|
|
3
|
+
const textDecoder = new TextDecoder()
|
|
4
|
+
|
|
5
|
+
export class ByteBuffer {
|
|
6
|
+
private _data: Uint8Array
|
|
7
|
+
private _index: number
|
|
8
|
+
length: number
|
|
9
|
+
|
|
10
|
+
constructor(data?: Uint8Array) {
|
|
11
|
+
if (data && !(data instanceof Uint8Array)) {
|
|
12
|
+
throw new Error('Must initialize a ByteBuffer with a Uint8Array')
|
|
13
|
+
}
|
|
14
|
+
this._data = data || new Uint8Array(256)
|
|
15
|
+
this._index = 0
|
|
16
|
+
this.length = data ? data.length : 0
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
get offset(): number {
|
|
20
|
+
return this._index
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
set offset(value: number) {
|
|
24
|
+
this._index = value
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The bytes this buffer reads, as a view rather than a copy. */
|
|
28
|
+
get bytes(): Uint8Array {
|
|
29
|
+
return this._data
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Returns a view into the internal buffer, not a copy.
|
|
34
|
+
*
|
|
35
|
+
* Consumers transferring this Uint8Array across thread boundaries (e.g. via
|
|
36
|
+
* postMessage with transferables) MUST copy first: `new Uint8Array(buffer)`.
|
|
37
|
+
* Otherwise, if multiple Uint8Arrays share the same underlying ArrayBuffer and
|
|
38
|
+
* one is transferred, all views into that buffer become detached.
|
|
39
|
+
*/
|
|
40
|
+
toUint8Array(): Uint8Array {
|
|
41
|
+
return this._data.subarray(0, this.length)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
readByte(): number {
|
|
45
|
+
return this._data[this._index++]
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
readByteArray(): Uint8Array {
|
|
49
|
+
const length = this.readVarUint()
|
|
50
|
+
const start = this._index
|
|
51
|
+
this._index = start + length
|
|
52
|
+
return this._data.slice(start, start + length)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
skipByteArray(): void {
|
|
56
|
+
const length = this.readVarUint()
|
|
57
|
+
this._index += length
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
readVarFloat(): number {
|
|
61
|
+
const index = this._index
|
|
62
|
+
const data = this._data
|
|
63
|
+
const first = data[index]
|
|
64
|
+
if (first === 0) {
|
|
65
|
+
this._index = index + 1
|
|
66
|
+
return 0
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
let bits = first | (data[index + 1] << 8) | (data[index + 2] << 16) | (data[index + 3] << 24)
|
|
70
|
+
this._index = index + 4
|
|
71
|
+
bits = (bits << 23) | (bits >>> 9)
|
|
72
|
+
int32[0] = bits
|
|
73
|
+
return float32[0]
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
readVarUint(): number {
|
|
77
|
+
const data = this._data
|
|
78
|
+
let i = this._index
|
|
79
|
+
let b = data[i++]
|
|
80
|
+
let value = b & 127
|
|
81
|
+
if (b < 128) {
|
|
82
|
+
this._index = i
|
|
83
|
+
return value
|
|
84
|
+
}
|
|
85
|
+
b = data[i++]
|
|
86
|
+
value |= (b & 127) << 7
|
|
87
|
+
if (b < 128) {
|
|
88
|
+
this._index = i
|
|
89
|
+
return value
|
|
90
|
+
}
|
|
91
|
+
b = data[i++]
|
|
92
|
+
value |= (b & 127) << 14
|
|
93
|
+
if (b < 128) {
|
|
94
|
+
this._index = i
|
|
95
|
+
return value
|
|
96
|
+
}
|
|
97
|
+
b = data[i++]
|
|
98
|
+
value |= (b & 127) << 21
|
|
99
|
+
if (b < 128) {
|
|
100
|
+
this._index = i
|
|
101
|
+
return value
|
|
102
|
+
}
|
|
103
|
+
b = data[i++]
|
|
104
|
+
value |= (b & 127) << 28
|
|
105
|
+
this._index = i
|
|
106
|
+
return value >>> 0
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
readVarInt(): number {
|
|
110
|
+
let value = this.readVarUint() | 0
|
|
111
|
+
return value & 1 ? ~(value >>> 1) : value >>> 1
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
readVarUint64(): bigint {
|
|
115
|
+
let value = BigInt(0)
|
|
116
|
+
let shift = BigInt(0)
|
|
117
|
+
let seven = BigInt(7)
|
|
118
|
+
let byte = this.readByte()
|
|
119
|
+
while (byte & 128 && shift < 56) {
|
|
120
|
+
value |= BigInt(byte & 127) << shift
|
|
121
|
+
shift += seven
|
|
122
|
+
byte = this.readByte()
|
|
123
|
+
}
|
|
124
|
+
value |= BigInt(byte) << shift
|
|
125
|
+
return value
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
readVarInt64(): bigint {
|
|
129
|
+
let value = this.readVarUint64()
|
|
130
|
+
let one = BigInt(1)
|
|
131
|
+
let sign = value & one
|
|
132
|
+
value >>= one
|
|
133
|
+
return sign ? ~value : value
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
readString(): string {
|
|
137
|
+
const start = this._index
|
|
138
|
+
const i = this.findStringTerminator(start)
|
|
139
|
+
this._index = i + 1
|
|
140
|
+
return textDecoder.decode(this._data.subarray(start, i))
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
skipString(): void {
|
|
144
|
+
this._index = this.findStringTerminator(this._index) + 1
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
private findStringTerminator(start: number): number {
|
|
148
|
+
const data = this._data
|
|
149
|
+
let index = start
|
|
150
|
+
while (index < data.length && data[index] !== 0) index++
|
|
151
|
+
if (index >= data.length) throw new Error('Unterminated string in Kiwi message')
|
|
152
|
+
return index
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
private _growBy(amount: number): void {
|
|
156
|
+
if (this.length + amount > this._data.length) {
|
|
157
|
+
let data = new Uint8Array((this.length + amount) << 1)
|
|
158
|
+
data.set(this._data)
|
|
159
|
+
this._data = data
|
|
160
|
+
}
|
|
161
|
+
this.length += amount
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
writeByte(value: number): void {
|
|
165
|
+
let index = this.length
|
|
166
|
+
this._growBy(1)
|
|
167
|
+
this._data[index] = value
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
writeByteArray(value: Uint8Array): void {
|
|
171
|
+
if (!(value instanceof Uint8Array) && !Array.isArray(value)) {
|
|
172
|
+
throw new TypeError('Expected byte array')
|
|
173
|
+
}
|
|
174
|
+
this.writeVarUint(value.length)
|
|
175
|
+
let index = this.length
|
|
176
|
+
this._growBy(value.length)
|
|
177
|
+
this._data.set(value, index)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
writeVarFloat(value: number): void {
|
|
181
|
+
let index = this.length
|
|
182
|
+
|
|
183
|
+
// Reinterpret as an integer
|
|
184
|
+
float32[0] = value
|
|
185
|
+
let bits = int32[0]
|
|
186
|
+
|
|
187
|
+
// Move the exponent to the first 8 bits
|
|
188
|
+
bits = (bits >>> 23) | (bits << 9)
|
|
189
|
+
|
|
190
|
+
// Optimization: use a single byte to store zero and denormals (check for an exponent of 0)
|
|
191
|
+
if ((bits & 255) === 0) {
|
|
192
|
+
this.writeByte(0)
|
|
193
|
+
return
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Endian-independent 32-bit write
|
|
197
|
+
this._growBy(4)
|
|
198
|
+
let data = this._data
|
|
199
|
+
data[index] = bits
|
|
200
|
+
data[index + 1] = bits >> 8
|
|
201
|
+
data[index + 2] = bits >> 16
|
|
202
|
+
data[index + 3] = bits >> 24
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
writeVarUint(value: number): void {
|
|
206
|
+
if (value < 0 || value > 0xffff_ffff) throw new Error('Outside uint range: ' + value)
|
|
207
|
+
do {
|
|
208
|
+
let byte = value & 127
|
|
209
|
+
value >>>= 7
|
|
210
|
+
this.writeByte(value ? byte | 128 : byte)
|
|
211
|
+
} while (value)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
writeVarInt(value: number): void {
|
|
215
|
+
if (value < -0x8000_0000 || value > 0x7fff_ffff) throw new Error('Outside int range: ' + value)
|
|
216
|
+
this.writeVarUint(((value << 1) ^ (value >> 31)) >>> 0)
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
writeVarUint64(value: bigint | string): void {
|
|
220
|
+
if (typeof value === 'string') value = BigInt(value)
|
|
221
|
+
else if (typeof value !== 'bigint')
|
|
222
|
+
throw new Error(`Expected bigint but got ${typeof value}: ${String(value)}`)
|
|
223
|
+
if (value < 0 || value > BigInt('0xFFFFFFFFFFFFFFFF'))
|
|
224
|
+
throw new Error('Outside uint64 range: ' + value)
|
|
225
|
+
let mask = BigInt(127)
|
|
226
|
+
let seven = BigInt(7)
|
|
227
|
+
for (let i = 0; value > mask && i < 8; i++) {
|
|
228
|
+
this.writeByte(Number(value & mask) | 128)
|
|
229
|
+
value >>= seven
|
|
230
|
+
}
|
|
231
|
+
this.writeByte(Number(value))
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
writeVarInt64(value: bigint | string): void {
|
|
235
|
+
if (typeof value === 'string') value = BigInt(value)
|
|
236
|
+
else if (typeof value !== 'bigint')
|
|
237
|
+
throw new Error(`Expected bigint but got ${typeof value}: ${String(value)}`)
|
|
238
|
+
if (value < -BigInt('0x8000000000000000') || value > BigInt('0x7FFFFFFFFFFFFFFF'))
|
|
239
|
+
throw new Error('Outside int64 range: ' + value)
|
|
240
|
+
let one = BigInt(1)
|
|
241
|
+
this.writeVarUint64(value < 0 ? ~(value << one) : value << one)
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
writeString(value: string): void {
|
|
245
|
+
let codePoint
|
|
246
|
+
|
|
247
|
+
for (let i = 0; i < value.length; i++) {
|
|
248
|
+
// Decode UTF-16
|
|
249
|
+
let a = value.charCodeAt(i)
|
|
250
|
+
if (i + 1 === value.length || a < 0xd800 || a >= 0xdc00) {
|
|
251
|
+
codePoint = a
|
|
252
|
+
} else {
|
|
253
|
+
let b = value.charCodeAt(++i)
|
|
254
|
+
codePoint = (a << 10) + b + (0x10000 - (0xd800 << 10) - 0xdc00)
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// Strings are null-terminated
|
|
258
|
+
if (codePoint === 0) {
|
|
259
|
+
throw new Error('Cannot encode a string containing the null character')
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// Encode UTF-8
|
|
263
|
+
if (codePoint < 0x80) {
|
|
264
|
+
this.writeByte(codePoint)
|
|
265
|
+
} else {
|
|
266
|
+
if (codePoint < 0x800) {
|
|
267
|
+
this.writeByte(((codePoint >> 6) & 0x1f) | 0xc0)
|
|
268
|
+
} else {
|
|
269
|
+
if (codePoint < 0x10000) {
|
|
270
|
+
this.writeByte(((codePoint >> 12) & 0x0f) | 0xe0)
|
|
271
|
+
} else {
|
|
272
|
+
this.writeByte(((codePoint >> 18) & 0x07) | 0xf0)
|
|
273
|
+
this.writeByte(((codePoint >> 12) & 0x3f) | 0x80)
|
|
274
|
+
}
|
|
275
|
+
this.writeByte(((codePoint >> 6) & 0x3f) | 0x80)
|
|
276
|
+
}
|
|
277
|
+
this.writeByte((codePoint & 0x3f) | 0x80)
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// Strings are null-terminated
|
|
282
|
+
this.writeByte(0)
|
|
283
|
+
}
|
|
284
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { ByteBuffer } from './bb'
|
|
2
|
+
import { Schema, Field, Definition, DefinitionKind } from './schema'
|
|
3
|
+
|
|
4
|
+
let types: (string | null)[] = ['bool', 'byte', 'int', 'uint', 'float', 'string', 'int64', 'uint64']
|
|
5
|
+
let kinds: DefinitionKind[] = ['ENUM', 'STRUCT', 'MESSAGE']
|
|
6
|
+
|
|
7
|
+
export function decodeBinarySchema(buffer: Uint8Array | ByteBuffer): Schema {
|
|
8
|
+
let bb = buffer instanceof ByteBuffer ? buffer : new ByteBuffer(buffer)
|
|
9
|
+
let definitionCount = bb.readVarUint()
|
|
10
|
+
let definitions: Definition[] = []
|
|
11
|
+
|
|
12
|
+
// Read in the schema
|
|
13
|
+
for (let i = 0; i < definitionCount; i++) {
|
|
14
|
+
let definitionName = bb.readString()
|
|
15
|
+
let kind = bb.readByte()
|
|
16
|
+
let fieldCount = bb.readVarUint()
|
|
17
|
+
let fields: Field[] = []
|
|
18
|
+
|
|
19
|
+
for (let j = 0; j < fieldCount; j++) {
|
|
20
|
+
let fieldName = bb.readString()
|
|
21
|
+
let type = bb.readVarInt()
|
|
22
|
+
let isArray = !!(bb.readByte() & 1)
|
|
23
|
+
let value = bb.readVarUint()
|
|
24
|
+
|
|
25
|
+
fields.push({
|
|
26
|
+
name: fieldName,
|
|
27
|
+
line: 0,
|
|
28
|
+
column: 0,
|
|
29
|
+
type: kinds[kind] === 'ENUM' ? null : (type as any),
|
|
30
|
+
isArray: isArray,
|
|
31
|
+
isDeprecated: false,
|
|
32
|
+
value: value
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
definitions.push({
|
|
37
|
+
name: definitionName,
|
|
38
|
+
line: 0,
|
|
39
|
+
column: 0,
|
|
40
|
+
kind: kinds[kind],
|
|
41
|
+
fields: fields
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Bind type names afterwards
|
|
46
|
+
for (let i = 0; i < definitionCount; i++) {
|
|
47
|
+
let fields = definitions[i].fields
|
|
48
|
+
for (let j = 0; j < fields.length; j++) {
|
|
49
|
+
let field = fields[j]
|
|
50
|
+
let type = field.type as any as number | null
|
|
51
|
+
|
|
52
|
+
if (type !== null && type < 0) {
|
|
53
|
+
if (~type >= types.length) {
|
|
54
|
+
throw new Error('Invalid type ' + type)
|
|
55
|
+
}
|
|
56
|
+
field.type = types[~type]
|
|
57
|
+
} else {
|
|
58
|
+
if (type !== null && type >= definitions.length) {
|
|
59
|
+
throw new Error('Invalid type ' + type)
|
|
60
|
+
}
|
|
61
|
+
field.type = type === null ? null : definitions[type].name
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
package: null,
|
|
68
|
+
definitions: definitions
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function encodeBinarySchema(schema: Schema): Uint8Array {
|
|
73
|
+
let bb = new ByteBuffer()
|
|
74
|
+
let definitions = schema.definitions
|
|
75
|
+
let definitionIndex: { [name: string]: number } = {}
|
|
76
|
+
|
|
77
|
+
bb.writeVarUint(definitions.length)
|
|
78
|
+
|
|
79
|
+
for (let i = 0; i < definitions.length; i++) {
|
|
80
|
+
definitionIndex[definitions[i].name] = i
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
for (let i = 0; i < definitions.length; i++) {
|
|
84
|
+
let definition = definitions[i]
|
|
85
|
+
|
|
86
|
+
bb.writeString(definition.name)
|
|
87
|
+
bb.writeByte(kinds.indexOf(definition.kind))
|
|
88
|
+
bb.writeVarUint(definition.fields.length)
|
|
89
|
+
|
|
90
|
+
for (let j = 0; j < definition.fields.length; j++) {
|
|
91
|
+
let field = definition.fields[j]
|
|
92
|
+
let type = types.indexOf(field.type)
|
|
93
|
+
|
|
94
|
+
bb.writeString(field.name)
|
|
95
|
+
bb.writeVarInt(type === -1 ? definitionIndex[field.type!] : ~type)
|
|
96
|
+
bb.writeByte(field.isArray ? 1 : 0)
|
|
97
|
+
bb.writeVarUint(field.value)
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return bb.toUint8Array()
|
|
102
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export type { Schema, Definition, Field } from './schema'
|
|
2
|
+
export { ByteBuffer } from './bb'
|
|
3
|
+
export {
|
|
4
|
+
compileSchemaRuntime as compileSchema,
|
|
5
|
+
compileSchemaTables,
|
|
6
|
+
decodeWhole,
|
|
7
|
+
TYPE,
|
|
8
|
+
type CompiledDefinition,
|
|
9
|
+
type CompiledField,
|
|
10
|
+
type CompiledSchema,
|
|
11
|
+
type HeaderFields,
|
|
12
|
+
type RuntimeCodec,
|
|
13
|
+
type RuntimeMessage,
|
|
14
|
+
type RuntimeValue,
|
|
15
|
+
type TypeCode
|
|
16
|
+
} from './interpreter'
|
|
17
|
+
export { decodeBinarySchema, encodeBinarySchema } from './binary'
|
|
18
|
+
export { parseSchema } from './parser'
|
|
19
|
+
export {
|
|
20
|
+
validateSchema,
|
|
21
|
+
expectFieldNumber,
|
|
22
|
+
expectEnumValue,
|
|
23
|
+
findDefinition,
|
|
24
|
+
findField
|
|
25
|
+
} from './validate'
|