protons 7.7.0 → 8.0.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.
Files changed (60) hide show
  1. package/README.md +5 -5
  2. package/dist/bin/protons.js +1 -1
  3. package/dist/src/fields/array-field.d.ts +18 -0
  4. package/dist/src/fields/array-field.d.ts.map +1 -0
  5. package/dist/src/fields/array-field.js +83 -0
  6. package/dist/src/fields/array-field.js.map +1 -0
  7. package/dist/src/fields/enum-field.d.ts +9 -0
  8. package/dist/src/fields/enum-field.d.ts.map +1 -0
  9. package/dist/src/fields/enum-field.js +21 -0
  10. package/dist/src/fields/enum-field.js.map +1 -0
  11. package/dist/src/fields/field.d.ts +45 -0
  12. package/dist/src/fields/field.d.ts.map +1 -0
  13. package/dist/src/fields/field.js +147 -0
  14. package/dist/src/fields/field.js.map +1 -0
  15. package/dist/src/fields/map-field.d.ts +22 -0
  16. package/dist/src/fields/map-field.d.ts.map +1 -0
  17. package/dist/src/fields/map-field.js +83 -0
  18. package/dist/src/fields/map-field.js.map +1 -0
  19. package/dist/src/fields/message-field.d.ts +9 -0
  20. package/dist/src/fields/message-field.d.ts.map +1 -0
  21. package/dist/src/fields/message-field.js +23 -0
  22. package/dist/src/fields/message-field.js.map +1 -0
  23. package/dist/src/index.d.ts +190 -16
  24. package/dist/src/index.d.ts.map +1 -1
  25. package/dist/src/index.js +5 -963
  26. package/dist/src/index.js.map +1 -1
  27. package/dist/src/types/enum.d.ts +21 -0
  28. package/dist/src/types/enum.d.ts.map +1 -0
  29. package/dist/src/types/enum.js +87 -0
  30. package/dist/src/types/enum.js.map +1 -0
  31. package/dist/src/types/index.d.ts +20 -0
  32. package/dist/src/types/index.d.ts.map +1 -0
  33. package/dist/src/types/index.js +2 -0
  34. package/dist/src/types/index.js.map +1 -0
  35. package/dist/src/types/message.d.ts +49 -0
  36. package/dist/src/types/message.d.ts.map +1 -0
  37. package/dist/src/types/message.js +478 -0
  38. package/dist/src/types/message.js.map +1 -0
  39. package/dist/src/types/module.d.ts +30 -0
  40. package/dist/src/types/module.d.ts.map +1 -0
  41. package/dist/src/types/module.js +184 -0
  42. package/dist/src/types/module.js.map +1 -0
  43. package/dist/src/types/primitive.d.ts +13 -0
  44. package/dist/src/types/primitive.d.ts.map +1 -0
  45. package/dist/src/types/primitive.js +174 -0
  46. package/dist/src/types/primitive.js.map +1 -0
  47. package/dist/typedoc-urls.json +2 -4
  48. package/package.json +103 -16
  49. package/src/fields/array-field.ts +109 -0
  50. package/src/fields/enum-field.ts +30 -0
  51. package/src/fields/field.ts +201 -0
  52. package/src/fields/map-field.ts +107 -0
  53. package/src/fields/message-field.ts +29 -0
  54. package/src/index.ts +6 -1202
  55. package/src/types/enum.ts +112 -0
  56. package/src/types/index.ts +21 -0
  57. package/src/types/message.ts +558 -0
  58. package/src/types/module.ts +234 -0
  59. package/src/types/primitive.ts +215 -0
  60. package/LICENSE +0 -4
@@ -0,0 +1,234 @@
1
+ import { NoMessagesFoundError } from 'protons-runtime'
2
+ import { Enum, isEnumDef } from './enum.ts'
3
+ import { Message } from './message.ts'
4
+ import { Primitive } from './primitive.ts'
5
+ import type { EnumDef } from './enum.ts'
6
+ import type { MessageDef } from './message.ts'
7
+ import type { Flags } from '../index.ts'
8
+ import type { Type } from './index.ts'
9
+
10
+ const types: Record<string, string> = {
11
+ bool: 'boolean',
12
+ bytes: 'Uint8Array',
13
+ double: 'number',
14
+ fixed32: 'number',
15
+ fixed64: 'bigint',
16
+ float: 'number',
17
+ int32: 'number',
18
+ int64: 'bigint',
19
+ sfixed32: 'number',
20
+ sfixed64: 'bigint',
21
+ sint32: 'number',
22
+ sint64: 'bigint',
23
+ string: 'string',
24
+ uint32: 'number',
25
+ uint64: 'bigint'
26
+ }
27
+
28
+ interface Import {
29
+ symbol: string
30
+ alias?: string
31
+ type: boolean
32
+ }
33
+
34
+ export interface ModuleDef {
35
+ nested?: Record<string, MessageDef | EnumDef>
36
+ }
37
+
38
+ export class Module {
39
+ private imports: Map<string, Import[]>
40
+ private types: Record<string, Type>
41
+ private eslintIgnores: string[]
42
+ public flags: Flags
43
+ public globals: Array<Message | Enum>
44
+
45
+ constructor (def: ModuleDef, flags: Flags) {
46
+ this.imports = new Map()
47
+ this.flags = flags
48
+ this.types = {}
49
+ this.globals = []
50
+ this.eslintIgnores = []
51
+
52
+ Object.entries(types).forEach(([pbType, jsType]) => {
53
+ this.types[pbType] = new Primitive(pbType, jsType)
54
+ })
55
+
56
+ const defs = def.nested
57
+
58
+ if (defs == null) {
59
+ throw new NoMessagesFoundError('No top-level messages found in protobuf')
60
+ }
61
+
62
+ for (const [name, def] of Object.entries(defs)) {
63
+ let type: Message | Enum
64
+
65
+ if (isEnumDef(def)) {
66
+ type = new Enum(name, name, def)
67
+ } else {
68
+ type = new Message(name, name, def, this)
69
+ }
70
+
71
+ this.globals.push(type)
72
+ this.types[name] = type
73
+ }
74
+
75
+ this.globals.forEach(type => type.init())
76
+ }
77
+
78
+ addImport (module: string, symbol: string, alias?: string): void {
79
+ const defs = this._findDefs(module)
80
+
81
+ for (const def of defs) {
82
+ // check if we already have a definition for this symbol
83
+ if (def.symbol === symbol) {
84
+ if (alias !== def.alias) {
85
+ throw new Error(`Type symbol ${symbol} imported from ${module} with alias ${def.alias} does not match alias ${alias}`)
86
+ }
87
+
88
+ // if it was a type before it's not now
89
+ def.type = false
90
+ return
91
+ }
92
+ }
93
+
94
+ defs.push({
95
+ symbol,
96
+ alias,
97
+ type: false
98
+ })
99
+ }
100
+
101
+ addTypeImport (module: string, symbol: string, alias?: string): void {
102
+ const defs = this._findDefs(module)
103
+
104
+ for (const def of defs) {
105
+ // check if we already have a definition for this symbol
106
+ if (def.symbol === symbol) {
107
+ if (alias !== def.alias) {
108
+ throw new Error(`Type symbol ${symbol} imported from ${module} with alias ${def.alias} does not match alias ${alias}`)
109
+ }
110
+
111
+ return
112
+ }
113
+ }
114
+
115
+ defs.push({
116
+ symbol,
117
+ alias,
118
+ type: true
119
+ })
120
+ }
121
+
122
+ addEslintIgnore (rule: string): void {
123
+ if (this.eslintIgnores.includes(rule)) {
124
+ return
125
+ }
126
+
127
+ this.eslintIgnores.push(rule)
128
+ }
129
+
130
+ _findDefs (module: string): Import[] {
131
+ let defs = this.imports.get(module)
132
+
133
+ if (defs == null) {
134
+ defs = []
135
+ this.imports.set(module, defs)
136
+ }
137
+
138
+ return defs
139
+ }
140
+
141
+ compile (): string {
142
+ const compiled = this.globals.map(def => def.compile(this)).flat()
143
+
144
+ // TODO: make these conditional
145
+ // const ignores: string[] = [
146
+ // '/* eslint-disable import/export */',
147
+ // '/* eslint-disable complexity */',
148
+ // '/* eslint-disable @typescript-eslint/no-namespace */',
149
+ // '/* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */',
150
+ // '/* eslint-disable @typescript-eslint/no-empty-interface */',
151
+ // '/* eslint-disable import/consistent-type-specifier-style */',
152
+ // '/* eslint-disable @typescript-eslint/no-unused-vars */',
153
+ // '/* eslint-disable require-yield */'
154
+ // ]
155
+
156
+ const imports = []
157
+ const importedModules = Array.from([...this.imports.entries()])
158
+ .sort((a, b) => {
159
+ return a[0].localeCompare(b[0])
160
+ })
161
+ .sort((a, b) => {
162
+ const aAllTypes = a[1].reduce((acc, curr) => {
163
+ return acc && curr.type
164
+ }, true)
165
+
166
+ const bAllTypes = b[1].reduce((acc, curr) => {
167
+ return acc && curr.type
168
+ }, true)
169
+
170
+ if (aAllTypes && !bAllTypes) {
171
+ return 1
172
+ }
173
+
174
+ if (!aAllTypes && bAllTypes) {
175
+ return -1
176
+ }
177
+
178
+ return 0
179
+ })
180
+
181
+ // add imports
182
+ for (const imp of importedModules) {
183
+ const symbols = imp[1]
184
+ .filter(imp => !imp.type)
185
+ .sort((a, b) => {
186
+ return a.symbol.localeCompare(b.symbol)
187
+ }).map(imp => {
188
+ return `${imp.symbol}${imp.alias != null ? ` as ${imp.alias}` : ''}`
189
+ }).join(', ')
190
+
191
+ if (symbols.length > 0) {
192
+ imports.push(`import { ${symbols} } from '${imp[0]}'`)
193
+ }
194
+ }
195
+
196
+ // add type imports
197
+ for (const imp of importedModules) {
198
+ const symbols = imp[1]
199
+ .filter(imp => imp.type)
200
+ .sort((a, b) => {
201
+ return a.symbol.localeCompare(b.symbol)
202
+ }).map(imp => {
203
+ return `${imp.symbol}${imp.alias != null ? ` as ${imp.alias}` : ''}`
204
+ }).join(', ')
205
+
206
+ if (symbols.length > 0) {
207
+ imports.push(`import type { ${symbols} } from '${imp[0]}'`)
208
+ }
209
+ }
210
+
211
+ return [
212
+ ...this.eslintIgnores.map(rule => `/* eslint-disable ${rule} */`),
213
+ '',
214
+ ...imports,
215
+ '',
216
+ ...compiled
217
+ ]
218
+ .join('\n')
219
+ .split('\n')
220
+ .map((line) => line.trim() === '' ? '' : line)
221
+ .join('\n')
222
+ .trim()
223
+ }
224
+
225
+ findType (jsType: string, override?: 'string' | 'number'): Type {
226
+ const type = this.types[override ?? jsType]
227
+
228
+ if (type == null) {
229
+ throw new Error(`Type for "${jsType}"${override == null ? '' : ` (overridden to "${override}")`} not found in ${[...Object.keys(this.types)].join(', ')}`)
230
+ }
231
+
232
+ return type
233
+ }
234
+ }
@@ -0,0 +1,215 @@
1
+ import { ArrayField } from '../fields/array-field.ts'
2
+ import type { Type } from './index.ts'
3
+ import type { Field } from '../fields/field.ts'
4
+
5
+ const decoderGenerators: Record<string, (jsTypeOverride?: 'number' | 'string') => string> = {
6
+ bool: () => 'reader.bool()',
7
+ bytes: () => 'reader.bytes()',
8
+ double: () => 'reader.double()',
9
+ fixed32: () => 'reader.fixed32()',
10
+ fixed64: (jsTypeOverride) => {
11
+ if (jsTypeOverride === 'number') {
12
+ return 'reader.fixed64Number()'
13
+ }
14
+
15
+ if (jsTypeOverride === 'string') {
16
+ return 'reader.fixed64String()'
17
+ }
18
+
19
+ return 'reader.fixed64()'
20
+ },
21
+ float: () => 'reader.float()',
22
+ int32: () => 'reader.int32()',
23
+ int64: (jsTypeOverride) => {
24
+ if (jsTypeOverride === 'number') {
25
+ return 'reader.int64Number()'
26
+ }
27
+
28
+ if (jsTypeOverride === 'string') {
29
+ return 'reader.int64String()'
30
+ }
31
+
32
+ return 'reader.int64()'
33
+ },
34
+ sfixed32: () => 'reader.sfixed32()',
35
+ sfixed64: (jsTypeOverride) => {
36
+ if (jsTypeOverride === 'number') {
37
+ return 'reader.sfixed64Number()'
38
+ }
39
+
40
+ if (jsTypeOverride === 'string') {
41
+ return 'reader.sfixed64String()'
42
+ }
43
+
44
+ return 'reader.sfixed64()'
45
+ },
46
+ sint32: () => 'reader.sint32()',
47
+ sint64: (jsTypeOverride) => {
48
+ if (jsTypeOverride === 'number') {
49
+ return 'reader.sint64Number()'
50
+ }
51
+
52
+ if (jsTypeOverride === 'string') {
53
+ return 'reader.sint64String()'
54
+ }
55
+
56
+ return 'reader.sint64()'
57
+ },
58
+ string: () => 'reader.string()',
59
+ uint32: () => 'reader.uint32()',
60
+ uint64: (jsTypeOverride) => {
61
+ if (jsTypeOverride === 'number') {
62
+ return 'reader.uint64Number()'
63
+ }
64
+
65
+ if (jsTypeOverride === 'string') {
66
+ return 'reader.uint64String()'
67
+ }
68
+
69
+ return 'reader.uint64()'
70
+ }
71
+ }
72
+
73
+ const encoderGenerators: Record<string, (val: string, jsTypeOverride?: 'number' | 'string') => string> = {
74
+ bool: (val) => `w.bool(${val})`,
75
+ bytes: (val) => `w.bytes(${val})`,
76
+ double: (val) => `w.double(${val})`,
77
+ fixed32: (val) => `w.fixed32(${val})`,
78
+ fixed64: (val, jsTypeOverride) => {
79
+ if (jsTypeOverride === 'number') {
80
+ return `w.fixed64Number(${val})`
81
+ }
82
+
83
+ if (jsTypeOverride === 'string') {
84
+ return `w.fixed64String(${val})`
85
+ }
86
+
87
+ return `w.fixed64(${val})`
88
+ },
89
+ float: (val) => `w.float(${val})`,
90
+ int32: (val) => `w.int32(${val})`,
91
+ int64: (val, jsTypeOverride) => {
92
+ if (jsTypeOverride === 'number') {
93
+ return `w.int64Number(${val})`
94
+ }
95
+
96
+ if (jsTypeOverride === 'string') {
97
+ return `w.int64String(${val})`
98
+ }
99
+
100
+ return `w.int64(${val})`
101
+ },
102
+ sfixed32: (val) => `w.sfixed32(${val})`,
103
+ sfixed64: (val, jsTypeOverride) => {
104
+ if (jsTypeOverride === 'number') {
105
+ return `w.sfixed64Number(${val})`
106
+ }
107
+
108
+ if (jsTypeOverride === 'string') {
109
+ return `w.sfixed64String(${val})`
110
+ }
111
+
112
+ return `w.sfixed64(${val})`
113
+ },
114
+ sint32: (val) => `w.sint32(${val})`,
115
+ sint64: (val, jsTypeOverride) => {
116
+ if (jsTypeOverride === 'number') {
117
+ return `w.sint64Number(${val})`
118
+ }
119
+
120
+ if (jsTypeOverride === 'string') {
121
+ return `w.sint64String(${val})`
122
+ }
123
+
124
+ return `w.sint64(${val})`
125
+ },
126
+ string: (val) => `w.string(${val})`,
127
+ uint32: (val) => `w.uint32(${val})`,
128
+ uint64: (val, jsTypeOverride) => {
129
+ if (jsTypeOverride === 'number') {
130
+ return `w.uint64Number(${val})`
131
+ }
132
+
133
+ if (jsTypeOverride === 'string') {
134
+ return `w.uint64String(${val})`
135
+ }
136
+
137
+ return `w.uint64(${val})`
138
+ }
139
+ }
140
+
141
+ interface DefaultValueTestGenerator {
142
+ (field: string): string
143
+ }
144
+
145
+ const defaultValueTestGenerators: Record<string, DefaultValueTestGenerator> = {
146
+ bool: (field) => `(${field} != null && ${field} !== false)`,
147
+ bytes: (field) => `(${field} != null && ${field}.byteLength > 0)`,
148
+ double: (field) => `(${field} != null && ${field} !== 0)`,
149
+ fixed32: (field) => `(${field} != null && ${field} !== 0)`,
150
+ fixed64: (field) => `(${field} != null && ${field} !== 0n)`,
151
+ float: (field) => `(${field} != null && ${field} !== 0)`,
152
+ int32: (field) => `(${field} != null && ${field} !== 0)`,
153
+ int64: (field) => `(${field} != null && ${field} !== 0n)`,
154
+ sfixed32: (field) => `(${field} != null && ${field} !== 0)`,
155
+ sfixed64: (field) => `(${field} != null && ${field} !== 0n)`,
156
+ sint32: (field) => `(${field} != null && ${field} !== 0)`,
157
+ sint64: (field) => `(${field} != null && ${field} !== 0n)`,
158
+ string: (field) => `(${field} != null && ${field} !== '')`,
159
+ uint32: (field) => `(${field} != null && ${field} !== 0)`,
160
+ uint64: (field) => `(${field} != null && ${field} !== 0n)`
161
+ }
162
+
163
+ const defaultValueTestGeneratorsJsTypeOverrides: Record<string, DefaultValueTestGenerator> = {
164
+ number: (field) => `(${field} != null && ${field} !== 0)`,
165
+ string: (field) => `(${field} != null && ${field} !== '')`
166
+ }
167
+
168
+ export class Primitive implements Type {
169
+ public jsType: string
170
+ public pbType: string
171
+
172
+ constructor (pbType: string, jsType: string) {
173
+ this.jsType = jsType
174
+ this.pbType = pbType
175
+ }
176
+
177
+ init (): void {
178
+
179
+ }
180
+
181
+ getDecoder (field: Field): string {
182
+ return decoderGenerators[this.pbType](field.jsTypeOverride)
183
+ }
184
+
185
+ getStreamingDecoder (field: Field, prefix: string): string {
186
+ const generator = decoderGenerators[this.pbType](field.jsTypeOverride)
187
+
188
+ if (field instanceof ArrayField) {
189
+ return `yield {
190
+ field: ${prefix},
191
+ index: obj.${field.name},
192
+ value: ${generator}
193
+ }`
194
+ }
195
+
196
+ return generator
197
+ }
198
+
199
+ getValueTest (field: Field, accessor: string): string {
200
+ // proto3 singular fields should only be written out if they are not the default value
201
+ if (!field.optional && !field.proto2Required) {
202
+ if (field.jsTypeOverride != null) {
203
+ return defaultValueTestGeneratorsJsTypeOverrides[field.jsTypeOverride](accessor)
204
+ }
205
+
206
+ return defaultValueTestGenerators[field.type](accessor)
207
+ }
208
+
209
+ return `${accessor} != null`
210
+ }
211
+
212
+ getEncoder (field: Field, accessor: string): string {
213
+ return encoderGenerators[this.pbType](accessor, field.jsTypeOverride)
214
+ }
215
+ }
package/LICENSE DELETED
@@ -1,4 +0,0 @@
1
- This project is dual licensed under MIT and Apache-2.0.
2
-
3
- MIT: https://www.opensource.org/licenses/mit
4
- Apache-2.0: https://www.apache.org/licenses/license-2.0