@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
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import { Schema, Definition, Field, DefinitionKind } from './schema'
|
|
2
|
+
import { error, quote } from './util'
|
|
3
|
+
|
|
4
|
+
export let nativeTypes = ['bool', 'byte', 'float', 'int', 'int64', 'string', 'uint', 'uint64']
|
|
5
|
+
|
|
6
|
+
// These are special names on the object returned by compileSchema()
|
|
7
|
+
export let reservedNames = ['ByteBuffer', 'package']
|
|
8
|
+
|
|
9
|
+
let regex = /((?:-|\b)\d+\b|[=;{}]|\[\]|\[deprecated\]|\b[A-Za-z_][A-Za-z0-9_]*\b|\/\/.*|\s+)/g
|
|
10
|
+
let identifier = /^[A-Za-z_][A-Za-z0-9_]*$/
|
|
11
|
+
let whitespace = /^\/\/.*|\s+$/
|
|
12
|
+
let equals = /^=$/
|
|
13
|
+
let endOfFile = /^$/
|
|
14
|
+
let semicolon = /^;$/
|
|
15
|
+
let integer = /^-?\d+$/
|
|
16
|
+
let leftBrace = /^\{$/
|
|
17
|
+
let rightBrace = /^\}$/
|
|
18
|
+
let arrayToken = /^\[\]$/
|
|
19
|
+
let enumKeyword = /^enum$/
|
|
20
|
+
let structKeyword = /^struct$/
|
|
21
|
+
let messageKeyword = /^message$/
|
|
22
|
+
let packageKeyword = /^package$/
|
|
23
|
+
let deprecatedToken = /^\[deprecated\]$/
|
|
24
|
+
|
|
25
|
+
interface Token {
|
|
26
|
+
text: string
|
|
27
|
+
line: number
|
|
28
|
+
column: number
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function tokenize(text: string): Token[] {
|
|
32
|
+
let parts = text.split(regex)
|
|
33
|
+
let tokens = []
|
|
34
|
+
let column = 0
|
|
35
|
+
let line = 0
|
|
36
|
+
|
|
37
|
+
for (let i = 0; i < parts.length; i++) {
|
|
38
|
+
let part = parts[i]
|
|
39
|
+
|
|
40
|
+
// Keep non-whitespace tokens
|
|
41
|
+
if (i & 1) {
|
|
42
|
+
if (!whitespace.test(part)) {
|
|
43
|
+
tokens.push({
|
|
44
|
+
text: part,
|
|
45
|
+
line: line + 1,
|
|
46
|
+
column: column + 1
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Detect syntax errors
|
|
52
|
+
else if (part !== '') {
|
|
53
|
+
error('Syntax error ' + quote(part), line + 1, column + 1)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Keep track of the line and column counts
|
|
57
|
+
let lines = part.split('\n')
|
|
58
|
+
if (lines.length > 1) column = 0
|
|
59
|
+
line += lines.length - 1
|
|
60
|
+
column += lines[lines.length - 1].length
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// End-of-file token
|
|
64
|
+
tokens.push({
|
|
65
|
+
text: '',
|
|
66
|
+
line: line,
|
|
67
|
+
column: column
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
return tokens
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function parse(tokens: Token[]): Schema {
|
|
74
|
+
function current(): Token {
|
|
75
|
+
return tokens[index]
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function eat(test: RegExp): boolean {
|
|
79
|
+
if (test.test(current().text)) {
|
|
80
|
+
index++
|
|
81
|
+
return true
|
|
82
|
+
}
|
|
83
|
+
return false
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function expect(test: RegExp, expected: string): void {
|
|
87
|
+
if (!eat(test)) {
|
|
88
|
+
let token = current()
|
|
89
|
+
error('Expected ' + expected + ' but found ' + quote(token.text), token.line, token.column)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function unexpectedToken(): never {
|
|
94
|
+
let token = current()
|
|
95
|
+
error('Unexpected token ' + quote(token.text), token.line, token.column)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
let definitions: Definition[] = []
|
|
99
|
+
let packageText = null
|
|
100
|
+
let index = 0
|
|
101
|
+
|
|
102
|
+
if (eat(packageKeyword)) {
|
|
103
|
+
packageText = current().text
|
|
104
|
+
expect(identifier, 'identifier')
|
|
105
|
+
expect(semicolon, '";"')
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
while (index < tokens.length && !eat(endOfFile)) {
|
|
109
|
+
let fields: Field[] = []
|
|
110
|
+
let kind: DefinitionKind
|
|
111
|
+
|
|
112
|
+
if (eat(enumKeyword)) kind = 'ENUM'
|
|
113
|
+
else if (eat(structKeyword)) kind = 'STRUCT'
|
|
114
|
+
else if (eat(messageKeyword)) kind = 'MESSAGE'
|
|
115
|
+
else unexpectedToken()
|
|
116
|
+
|
|
117
|
+
// All definitions start off the same
|
|
118
|
+
let name = current()
|
|
119
|
+
expect(identifier, 'identifier')
|
|
120
|
+
expect(leftBrace, '"{"')
|
|
121
|
+
|
|
122
|
+
// Parse fields
|
|
123
|
+
while (!eat(rightBrace)) {
|
|
124
|
+
let type: string | null = null
|
|
125
|
+
let isArray = false
|
|
126
|
+
let isDeprecated = false
|
|
127
|
+
|
|
128
|
+
// Enums don't have types
|
|
129
|
+
if (kind !== 'ENUM') {
|
|
130
|
+
type = current().text
|
|
131
|
+
expect(identifier, 'identifier')
|
|
132
|
+
isArray = eat(arrayToken)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
let field = current()
|
|
136
|
+
expect(identifier, 'identifier')
|
|
137
|
+
|
|
138
|
+
// Structs don't have explicit values
|
|
139
|
+
let value: Token | null = null
|
|
140
|
+
if (kind !== 'STRUCT') {
|
|
141
|
+
expect(equals, '"="')
|
|
142
|
+
value = current()
|
|
143
|
+
expect(integer, 'integer')
|
|
144
|
+
|
|
145
|
+
if ((+value.text | 0) + '' !== value.text) {
|
|
146
|
+
error('Invalid integer ' + quote(value.text), value.line, value.column)
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
let deprecated = current()
|
|
151
|
+
if (eat(deprecatedToken)) {
|
|
152
|
+
if (kind !== 'MESSAGE') {
|
|
153
|
+
error('Cannot deprecate this field', deprecated.line, deprecated.column)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
isDeprecated = true
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
expect(semicolon, '";"')
|
|
160
|
+
|
|
161
|
+
fields.push({
|
|
162
|
+
name: field.text,
|
|
163
|
+
line: field.line,
|
|
164
|
+
column: field.column,
|
|
165
|
+
type: type,
|
|
166
|
+
isArray: isArray,
|
|
167
|
+
isDeprecated: isDeprecated,
|
|
168
|
+
value: value !== null ? +value.text | 0 : fields.length + 1
|
|
169
|
+
})
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
definitions.push({
|
|
173
|
+
name: name.text,
|
|
174
|
+
line: name.line,
|
|
175
|
+
column: name.column,
|
|
176
|
+
kind: kind,
|
|
177
|
+
fields: fields
|
|
178
|
+
})
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return {
|
|
182
|
+
package: packageText,
|
|
183
|
+
definitions: definitions
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function verify(root: Schema): void {
|
|
188
|
+
let definedTypes = nativeTypes.slice()
|
|
189
|
+
let definitions: { [name: string]: Definition } = {}
|
|
190
|
+
|
|
191
|
+
// Define definitions
|
|
192
|
+
for (let i = 0; i < root.definitions.length; i++) {
|
|
193
|
+
let definition = root.definitions[i]
|
|
194
|
+
if (definedTypes.includes(definition.name)) {
|
|
195
|
+
error(
|
|
196
|
+
'The type ' + quote(definition.name) + ' is defined twice',
|
|
197
|
+
definition.line,
|
|
198
|
+
definition.column
|
|
199
|
+
)
|
|
200
|
+
}
|
|
201
|
+
if (reservedNames.includes(definition.name)) {
|
|
202
|
+
error(
|
|
203
|
+
'The type name ' + quote(definition.name) + ' is reserved',
|
|
204
|
+
definition.line,
|
|
205
|
+
definition.column
|
|
206
|
+
)
|
|
207
|
+
}
|
|
208
|
+
definedTypes.push(definition.name)
|
|
209
|
+
definitions[definition.name] = definition
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Check fields
|
|
213
|
+
for (let i = 0; i < root.definitions.length; i++) {
|
|
214
|
+
let definition = root.definitions[i]
|
|
215
|
+
let fields = definition.fields
|
|
216
|
+
|
|
217
|
+
if (definition.kind === 'ENUM' || fields.length === 0) {
|
|
218
|
+
continue
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// Check types
|
|
222
|
+
for (let j = 0; j < fields.length; j++) {
|
|
223
|
+
let field = fields[j]
|
|
224
|
+
if (!definedTypes.includes(field.type!)) {
|
|
225
|
+
error(
|
|
226
|
+
'The type ' + quote(field.type!) + ' is not defined for field ' + quote(field.name),
|
|
227
|
+
field.line,
|
|
228
|
+
field.column
|
|
229
|
+
)
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Check values
|
|
234
|
+
let values: number[] = []
|
|
235
|
+
for (let j = 0; j < fields.length; j++) {
|
|
236
|
+
let field = fields[j]
|
|
237
|
+
if (values.includes(field.value)) {
|
|
238
|
+
error('The id for field ' + quote(field.name) + ' is used twice', field.line, field.column)
|
|
239
|
+
}
|
|
240
|
+
if (field.value <= 0) {
|
|
241
|
+
error(
|
|
242
|
+
'The id for field ' + quote(field.name) + ' must be positive',
|
|
243
|
+
field.line,
|
|
244
|
+
field.column
|
|
245
|
+
)
|
|
246
|
+
}
|
|
247
|
+
// Figma schema uses sparse field IDs (up to 435), so skip sequential check
|
|
248
|
+
values.push(field.value)
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// Check that structs don't contain themselves
|
|
253
|
+
let state: { [name: string]: number } = {}
|
|
254
|
+
let check = (name: string): boolean => {
|
|
255
|
+
let definition = definitions[name]
|
|
256
|
+
if (definition && definition.kind === 'STRUCT') {
|
|
257
|
+
if (state[name] === 1) {
|
|
258
|
+
error(
|
|
259
|
+
'Recursive nesting of ' + quote(name) + ' is not allowed',
|
|
260
|
+
definition.line,
|
|
261
|
+
definition.column
|
|
262
|
+
)
|
|
263
|
+
}
|
|
264
|
+
if (state[name] !== 2 && definition) {
|
|
265
|
+
state[name] = 1
|
|
266
|
+
let fields = definition.fields
|
|
267
|
+
for (let i = 0; i < fields.length; i++) {
|
|
268
|
+
let field = fields[i]
|
|
269
|
+
if (!field.isArray) {
|
|
270
|
+
check(field.type!)
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
state[name] = 2
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
return true
|
|
277
|
+
}
|
|
278
|
+
for (let i = 0; i < root.definitions.length; i++) {
|
|
279
|
+
check(root.definitions[i].name)
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export function parseSchema(text: string): Schema {
|
|
284
|
+
let schema = parse(tokenize(text))
|
|
285
|
+
verify(schema)
|
|
286
|
+
return schema
|
|
287
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export interface Schema {
|
|
2
|
+
package: string | null
|
|
3
|
+
definitions: Definition[]
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export type DefinitionKind = 'ENUM' | 'STRUCT' | 'MESSAGE'
|
|
7
|
+
|
|
8
|
+
export interface Definition {
|
|
9
|
+
name: string
|
|
10
|
+
line: number
|
|
11
|
+
column: number
|
|
12
|
+
kind: DefinitionKind
|
|
13
|
+
fields: Field[]
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface Field {
|
|
17
|
+
name: string
|
|
18
|
+
line: number
|
|
19
|
+
column: number
|
|
20
|
+
type: string | null
|
|
21
|
+
isArray: boolean
|
|
22
|
+
isDeprecated: boolean
|
|
23
|
+
value: number
|
|
24
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function quote(text: string): string {
|
|
2
|
+
return JSON.stringify(text)
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export function error(text: string, line: number, column: number): never {
|
|
6
|
+
var error = new Error(text)
|
|
7
|
+
;(error as any).line = line
|
|
8
|
+
;(error as any).column = column
|
|
9
|
+
throw error
|
|
10
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { Definition, Field, Schema } from './schema'
|
|
2
|
+
import { error, quote } from './util'
|
|
3
|
+
|
|
4
|
+
export function findDefinition(schema: Schema, name: string): Definition | null {
|
|
5
|
+
return schema.definitions.find((definition) => definition.name === name) ?? null
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function findField(schema: Schema, definitionName: string, fieldName: string): Field | null {
|
|
9
|
+
return (
|
|
10
|
+
findDefinition(schema, definitionName)?.fields.find((field) => field.name === fieldName) ?? null
|
|
11
|
+
)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function expectFieldNumber(
|
|
15
|
+
schema: Schema,
|
|
16
|
+
definitionName: string,
|
|
17
|
+
fieldName: string,
|
|
18
|
+
expectedValue: number
|
|
19
|
+
): void {
|
|
20
|
+
const field = findField(schema, definitionName, fieldName)
|
|
21
|
+
if (!field) {
|
|
22
|
+
throw new Error(`Missing field ${definitionName}.${fieldName}`)
|
|
23
|
+
}
|
|
24
|
+
if (field.value !== expectedValue) {
|
|
25
|
+
throw new Error(
|
|
26
|
+
`Expected ${definitionName}.${fieldName} to use field ${expectedValue}, got ${field.value}`
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function expectEnumValue(
|
|
32
|
+
schema: Schema,
|
|
33
|
+
enumName: string,
|
|
34
|
+
memberName: string,
|
|
35
|
+
expectedValue: number
|
|
36
|
+
): void {
|
|
37
|
+
const definition = findDefinition(schema, enumName)
|
|
38
|
+
if (!definition) {
|
|
39
|
+
throw new Error(`Missing enum ${enumName}`)
|
|
40
|
+
}
|
|
41
|
+
if (definition.kind !== 'ENUM') {
|
|
42
|
+
throw new Error(`${enumName} is a ${definition.kind}, not an enum`)
|
|
43
|
+
}
|
|
44
|
+
const field = definition.fields.find((candidate) => candidate.name === memberName)
|
|
45
|
+
if (!field) {
|
|
46
|
+
throw new Error(`Missing enum member ${enumName}.${memberName}`)
|
|
47
|
+
}
|
|
48
|
+
if (field.value !== expectedValue) {
|
|
49
|
+
throw new Error(
|
|
50
|
+
`Expected ${enumName}.${memberName} to use value ${expectedValue}, got ${field.value}`
|
|
51
|
+
)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function validateSchema(schema: Schema): void {
|
|
56
|
+
for (const definition of schema.definitions) {
|
|
57
|
+
validateUniqueFieldNames(definition)
|
|
58
|
+
if (definition.kind === 'ENUM') validateUniqueEnumValues(definition)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function validateUniqueFieldNames(definition: Definition): void {
|
|
63
|
+
const fieldsByName = new Set<string>()
|
|
64
|
+
for (const field of definition.fields) {
|
|
65
|
+
if (fieldsByName.has(field.name)) {
|
|
66
|
+
error(
|
|
67
|
+
`The field ${quote(field.name)} is defined twice in ${quote(definition.name)}`,
|
|
68
|
+
field.line,
|
|
69
|
+
field.column
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
fieldsByName.add(field.name)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function validateUniqueEnumValues(definition: Definition): void {
|
|
77
|
+
const fieldsByValue = new Set<number>()
|
|
78
|
+
for (const field of definition.fields) {
|
|
79
|
+
if (fieldsByValue.has(field.value)) {
|
|
80
|
+
error(
|
|
81
|
+
`The enum value ${field.value} is used twice in ${quote(definition.name)}`,
|
|
82
|
+
field.line,
|
|
83
|
+
field.column
|
|
84
|
+
)
|
|
85
|
+
}
|
|
86
|
+
fieldsByValue.add(field.value)
|
|
87
|
+
}
|
|
88
|
+
}
|