stage-browser 0.1.0 → 0.2.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/README.md +92 -45
- package/build/icon.icns +0 -0
- package/build/icon.png +0 -0
- package/dist/bin/stage.js +111 -0
- package/dist/main/cli-args.js +28 -0
- package/dist/main/context-menu.js +68 -0
- package/dist/main/context.js +2 -0
- package/dist/main/extensions.js +78 -0
- package/dist/main/ipc.js +73 -0
- package/dist/main/main.js +282 -0
- package/dist/main/menu.js +140 -0
- package/dist/main/reveal.js +170 -0
- package/dist/main/selftest.js +570 -0
- package/dist/main/store.js +85 -0
- package/dist/main/suggest.js +43 -0
- package/dist/main/tabs.js +324 -0
- package/dist/main/ui-window.js +133 -0
- package/dist/main/url.js +38 -0
- package/dist/main/window.js +133 -0
- package/dist/preload/extension-compat.js +163 -0
- package/dist/preload/ui-preload.js +34 -0
- package/dist/shared/contract.js +6 -0
- package/dist/ui/app.js +692 -0
- package/{src → dist}/ui/index.html +15 -5
- package/dist/ui/mock.js +500 -0
- package/{src → dist}/ui/styles.css +55 -14
- package/node_modules/@types/chrome/LICENSE +21 -0
- package/node_modules/@types/chrome/README.md +15 -0
- package/node_modules/@types/chrome/chrome-cast/index.d.ts +1156 -0
- package/node_modules/@types/chrome/har-format/index.d.ts +6 -0
- package/node_modules/@types/chrome/index.d.ts +13786 -0
- package/node_modules/@types/chrome/package.json +99 -0
- package/node_modules/@types/filesystem/LICENSE +21 -0
- package/node_modules/@types/filesystem/README.md +15 -0
- package/node_modules/@types/filesystem/index.d.ts +564 -0
- package/node_modules/@types/filesystem/package.json +26 -0
- package/node_modules/@types/filewriter/LICENSE +21 -0
- package/node_modules/@types/filewriter/README.md +15 -0
- package/node_modules/@types/filewriter/index.d.ts +166 -0
- package/node_modules/@types/filewriter/package.json +25 -0
- package/node_modules/@types/har-format/LICENSE +21 -0
- package/node_modules/@types/har-format/README.md +15 -0
- package/node_modules/@types/har-format/index.d.ts +938 -0
- package/node_modules/@types/har-format/package.json +31 -0
- package/node_modules/adm-zip/LICENSE +21 -0
- package/node_modules/adm-zip/README.md +90 -0
- package/node_modules/adm-zip/adm-zip.js +1020 -0
- package/node_modules/adm-zip/headers/entryHeader.js +396 -0
- package/node_modules/adm-zip/headers/index.js +2 -0
- package/node_modules/adm-zip/headers/mainHeader.js +177 -0
- package/node_modules/adm-zip/methods/deflater.js +33 -0
- package/node_modules/adm-zip/methods/index.js +3 -0
- package/node_modules/adm-zip/methods/inflater.js +64 -0
- package/node_modules/adm-zip/methods/zipcrypto.js +175 -0
- package/node_modules/adm-zip/package.json +56 -0
- package/node_modules/adm-zip/types.d.ts +223 -0
- package/node_modules/adm-zip/util/constants.js +142 -0
- package/node_modules/adm-zip/util/decoder.js +5 -0
- package/node_modules/adm-zip/util/errors.js +66 -0
- package/node_modules/adm-zip/util/fattr.js +76 -0
- package/node_modules/adm-zip/util/index.js +5 -0
- package/node_modules/adm-zip/util/utils.js +481 -0
- package/node_modules/adm-zip/zipEntry.js +415 -0
- package/node_modules/adm-zip/zipFile.js +466 -0
- package/node_modules/debug/LICENSE +20 -0
- package/node_modules/debug/README.md +481 -0
- package/node_modules/debug/package.json +64 -0
- package/node_modules/debug/src/browser.js +272 -0
- package/node_modules/debug/src/common.js +292 -0
- package/node_modules/debug/src/index.js +10 -0
- package/node_modules/debug/src/node.js +263 -0
- package/node_modules/electron-chrome-web-store/README.md +143 -0
- package/node_modules/electron-chrome-web-store/dist/chrome-web-store.preload.js +299 -0
- package/node_modules/electron-chrome-web-store/dist/cjs/browser/index.js +1674 -0
- package/node_modules/electron-chrome-web-store/dist/esm/browser/index.mjs +950 -0
- package/node_modules/electron-chrome-web-store/dist/types/browser/api.d.ts +2 -0
- package/node_modules/electron-chrome-web-store/dist/types/browser/crx3.d.ts +20 -0
- package/node_modules/electron-chrome-web-store/dist/types/browser/id.d.ts +9 -0
- package/node_modules/electron-chrome-web-store/dist/types/browser/index.d.ts +62 -0
- package/node_modules/electron-chrome-web-store/dist/types/browser/installer.d.ts +29 -0
- package/node_modules/electron-chrome-web-store/dist/types/browser/loader.d.ts +18 -0
- package/node_modules/electron-chrome-web-store/dist/types/browser/types.d.ts +21 -0
- package/node_modules/electron-chrome-web-store/dist/types/browser/updater.d.ts +6 -0
- package/node_modules/electron-chrome-web-store/dist/types/browser/utils.d.ts +4 -0
- package/node_modules/electron-chrome-web-store/dist/types/common/constants.d.ts +44 -0
- package/node_modules/electron-chrome-web-store/dist/types/renderer/chrome-web-store.preload.d.ts +1 -0
- package/node_modules/electron-chrome-web-store/package.json +43 -0
- package/node_modules/ms/index.js +162 -0
- package/node_modules/ms/license.md +21 -0
- package/node_modules/ms/package.json +38 -0
- package/node_modules/ms/readme.md +59 -0
- package/node_modules/pbf/LICENSE +27 -0
- package/node_modules/pbf/README.md +293 -0
- package/node_modules/pbf/bin/pbf +17 -0
- package/node_modules/pbf/compile.js +441 -0
- package/node_modules/pbf/dist/pbf-dev.js +838 -0
- package/node_modules/pbf/dist/pbf.js +1 -0
- package/node_modules/pbf/index.d.ts +206 -0
- package/node_modules/pbf/index.js +830 -0
- package/node_modules/pbf/package.json +63 -0
- package/node_modules/protocol-buffers-schema/.travis.yml +7 -0
- package/node_modules/protocol-buffers-schema/LICENSE +21 -0
- package/node_modules/protocol-buffers-schema/README.md +122 -0
- package/node_modules/protocol-buffers-schema/example.js +11 -0
- package/node_modules/protocol-buffers-schema/example.proto +13 -0
- package/node_modules/protocol-buffers-schema/index.js +6 -0
- package/node_modules/protocol-buffers-schema/package.json +31 -0
- package/node_modules/protocol-buffers-schema/parse.js +769 -0
- package/node_modules/protocol-buffers-schema/stringify.js +206 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/basic.json +90 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/basic.proto +11 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/comments.json +99 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/comments.proto +19 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/complex.json +125 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/complex.proto +30 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/enum.json +42 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/enum.proto +8 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/escaped-quotes.json +32 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/escaped-quotes.proto +3 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/extend.json +167 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/extend.proto +19 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/import.json +30 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/import.proto +6 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/map.json +33 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/map.proto +3 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/no-tags.proto +8 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/oneof.json +41 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/oneof.proto +6 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/option.json +364 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/option.proto +72 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/options.json +47 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/options.proto +8 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/pheromon-trajectories.proto +8 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/reserved.json +55 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/reserved.proto +17 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/search.json +30 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/search.proto +5 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/service.json +108 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/service.proto +30 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/valid-packed.proto +35 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/version.json +90 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/version.proto +13 -0
- package/node_modules/protocol-buffers-schema/test/index.js +157 -0
- package/node_modules/protocol-buffers-schema/tokenize.js +56 -0
- package/node_modules/resolve-protobuf-schema/.travis.yml +3 -0
- package/node_modules/resolve-protobuf-schema/LICENSE +21 -0
- package/node_modules/resolve-protobuf-schema/README.md +49 -0
- package/node_modules/resolve-protobuf-schema/index.js +50 -0
- package/node_modules/resolve-protobuf-schema/package.json +25 -0
- package/node_modules/resolve-protobuf-schema/test/a.proto +5 -0
- package/node_modules/resolve-protobuf-schema/test/b.proto +5 -0
- package/node_modules/resolve-protobuf-schema/test/c.proto +3 -0
- package/node_modules/resolve-protobuf-schema/test/index.js +49 -0
- package/package.json +35 -11
- package/bin/stage.js +0 -52
- package/src/main/cli-args.js +0 -28
- package/src/main/context-menu.js +0 -64
- package/src/main/extensions.js +0 -61
- package/src/main/main.js +0 -310
- package/src/main/menu.js +0 -171
- package/src/main/reveal.js +0 -141
- package/src/main/selftest.js +0 -383
- package/src/main/store.js +0 -77
- package/src/main/suggest.js +0 -50
- package/src/main/tabs.js +0 -351
- package/src/main/url.js +0 -37
- package/src/main/window.js +0 -71
- package/src/preload/extension-compat.js +0 -34
- package/src/preload/ui-preload.js +0 -40
- package/src/ui/app.js +0 -588
- package/src/ui/mock.js +0 -455
- /package/{src → dist}/ui/newtab.html +0 -0
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
/* eslint @stylistic/indent: 0 */
|
|
2
|
+
|
|
3
|
+
import {readFileSync} from 'fs';
|
|
4
|
+
|
|
5
|
+
const version = JSON.parse(readFileSync(new URL('package.json', import.meta.url))).version;
|
|
6
|
+
|
|
7
|
+
export function compile(proto) {
|
|
8
|
+
return new Function(`const exports = {};\n${compileRaw(proto, {legacy: true})}\nreturn exports;`)();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function compileRaw(proto, options = {}) {
|
|
12
|
+
const context = buildDefaults(buildContext(proto, null), proto.syntax);
|
|
13
|
+
return `${options.dev ? '' : `// code generated by pbf v${version}\n`}${writeContext(context, options)}`;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function writeContext(ctx, options) {
|
|
17
|
+
let code = '';
|
|
18
|
+
if (ctx._proto.fields) code += writeMessage(ctx, options);
|
|
19
|
+
if (ctx._proto.values) code += writeEnum(ctx, options);
|
|
20
|
+
|
|
21
|
+
for (let i = 0; i < ctx._children.length; i++) {
|
|
22
|
+
code += writeContext(ctx._children[i], options);
|
|
23
|
+
}
|
|
24
|
+
return code;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function writeMessage(ctx, options) {
|
|
28
|
+
const fields = ctx._proto.fields;
|
|
29
|
+
|
|
30
|
+
let code = '\n';
|
|
31
|
+
|
|
32
|
+
if (!options.noRead) {
|
|
33
|
+
const readName = `read${ctx._name}`;
|
|
34
|
+
code +=
|
|
35
|
+
`${writeFunctionExport(options, readName)}function ${readName}(pbf, end) {
|
|
36
|
+
return pbf.readFields(${readName}Field, ${compileDest(ctx)}, end);
|
|
37
|
+
}
|
|
38
|
+
function ${readName}Field(tag, obj, pbf) {
|
|
39
|
+
`;
|
|
40
|
+
for (let i = 0; i < fields.length; i++) {
|
|
41
|
+
const field = fields[i];
|
|
42
|
+
const {type, name, repeated, oneof, tag} = field;
|
|
43
|
+
const readCode = compileFieldRead(ctx, field);
|
|
44
|
+
const packed = willSupportPacked(ctx, field);
|
|
45
|
+
|
|
46
|
+
let fieldRead =
|
|
47
|
+
type === 'map' ? compileMapRead(readCode, name) :
|
|
48
|
+
repeated ? (packed ? readCode : `obj.${name}.push(${readCode})`) :
|
|
49
|
+
`obj.${name} = ${readCode}`;
|
|
50
|
+
|
|
51
|
+
if (oneof) {
|
|
52
|
+
fieldRead += `; obj.${oneof} = ${JSON.stringify(name)}`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
fieldRead = type === 'map' || oneof ? `{ ${fieldRead}; }` : `${fieldRead};`;
|
|
56
|
+
|
|
57
|
+
code +=
|
|
58
|
+
` ${i ? 'else ' : ''}if (tag === ${tag}) ${fieldRead}\n`;
|
|
59
|
+
}
|
|
60
|
+
code += '}\n';
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (!options.noWrite) {
|
|
64
|
+
const writeName = `write${ctx._name}`;
|
|
65
|
+
code += `${writeFunctionExport(options, writeName)}function ${writeName}(obj, pbf) {\n`;
|
|
66
|
+
for (const field of fields) {
|
|
67
|
+
const writeCode =
|
|
68
|
+
field.repeated && !isPacked(field) ? compileRepeatedWrite(ctx, field) :
|
|
69
|
+
field.type === 'map' ? compileMapWrite(ctx, field) : compileFieldWrite(ctx, field, `obj.${field.name}`);
|
|
70
|
+
code += getDefaultWriteTest(ctx, field);
|
|
71
|
+
code += `${writeCode};\n`;
|
|
72
|
+
}
|
|
73
|
+
code += '}\n';
|
|
74
|
+
}
|
|
75
|
+
return code;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function writeFunctionExport({legacy}, name) {
|
|
79
|
+
return legacy ? `exports.${name} = ${name};\n` : 'export ';
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function getEnumValues(ctx) {
|
|
83
|
+
const enums = {};
|
|
84
|
+
const ids = Object.keys(ctx._proto.values);
|
|
85
|
+
for (let i = 0; i < ids.length; i++) {
|
|
86
|
+
enums[ids[i]] = ctx._proto.values[ids[i]].value;
|
|
87
|
+
}
|
|
88
|
+
return enums;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function writeEnum(ctx, {legacy}) {
|
|
92
|
+
const enums = JSON.stringify(getEnumValues(ctx), null, 4);
|
|
93
|
+
const name = ctx._name;
|
|
94
|
+
return `\n${legacy ? `const ${name} = exports.${name}` : `export const ${name}`} = ${enums};\n`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function compileDest(ctx) {
|
|
98
|
+
const props = new Set();
|
|
99
|
+
for (const {name, oneof} of ctx._proto.fields) {
|
|
100
|
+
props.add(`${name}: ${JSON.stringify(ctx._defaults[name])}`);
|
|
101
|
+
if (oneof) props.add(`${oneof }: undefined`);
|
|
102
|
+
}
|
|
103
|
+
return `{${[...props].join(', ')}}`;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function isEnum(type) {
|
|
107
|
+
return type && type._proto.values;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function getType(ctx, field) {
|
|
111
|
+
if (field.type === 'map') {
|
|
112
|
+
return ctx[getMapMessageName(field.tag)];
|
|
113
|
+
}
|
|
114
|
+
const path = field.type.split('.');
|
|
115
|
+
return path.reduce((ctx, name) => ctx && ctx[name], ctx);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function fieldShouldUseStringAsNumber(field) {
|
|
119
|
+
if (field.options.jstype === 'JS_STRING') {
|
|
120
|
+
switch (field.type) {
|
|
121
|
+
case 'float':
|
|
122
|
+
case 'double':
|
|
123
|
+
case 'uint32':
|
|
124
|
+
case 'uint64':
|
|
125
|
+
case 'int32':
|
|
126
|
+
case 'int64':
|
|
127
|
+
case 'sint32':
|
|
128
|
+
case 'sint64':
|
|
129
|
+
case 'fixed32':
|
|
130
|
+
case 'fixed64':
|
|
131
|
+
case 'sfixed32':
|
|
132
|
+
case 'sfixed64': return true;
|
|
133
|
+
default: return false;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function compileFieldRead(ctx, field) {
|
|
140
|
+
const type = getType(ctx, field);
|
|
141
|
+
if (type) {
|
|
142
|
+
if (type._proto.fields) return `read${type._name}(pbf, pbf.readVarint() + pbf.pos)`;
|
|
143
|
+
if (!isEnum(type)) throw new Error(`Unexpected type: ${type._name}`);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const fieldType = isEnum(type) ? 'enum' : field.type;
|
|
147
|
+
|
|
148
|
+
let prefix = 'pbf.read';
|
|
149
|
+
const signed = fieldType === 'int32' || fieldType === 'int64' ? 'true' : '';
|
|
150
|
+
let suffix = `(${signed})`;
|
|
151
|
+
|
|
152
|
+
if (willSupportPacked(ctx, field)) {
|
|
153
|
+
prefix += 'Packed';
|
|
154
|
+
suffix = `(obj.${field.name}${signed ? `, ${signed}` : ''})`;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (fieldShouldUseStringAsNumber(field)) {
|
|
158
|
+
suffix += '.toString()';
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
switch (fieldType) {
|
|
162
|
+
case 'string': return `${prefix}String${suffix}`;
|
|
163
|
+
case 'float': return `${prefix}Float${suffix}`;
|
|
164
|
+
case 'double': return `${prefix}Double${suffix}`;
|
|
165
|
+
case 'bool': return `${prefix}Boolean${suffix}`;
|
|
166
|
+
case 'enum':
|
|
167
|
+
case 'uint32':
|
|
168
|
+
case 'uint64':
|
|
169
|
+
case 'int32':
|
|
170
|
+
case 'int64': return `${prefix}Varint${suffix}`;
|
|
171
|
+
case 'sint32':
|
|
172
|
+
case 'sint64': return `${prefix}SVarint${suffix}`;
|
|
173
|
+
case 'fixed32': return `${prefix}Fixed32${suffix}`;
|
|
174
|
+
case 'fixed64': return `${prefix}Fixed64${suffix}`;
|
|
175
|
+
case 'sfixed32': return `${prefix}SFixed32${suffix}`;
|
|
176
|
+
case 'sfixed64': return `${prefix}SFixed64${suffix}`;
|
|
177
|
+
case 'bytes': return `${prefix}Bytes${suffix}`;
|
|
178
|
+
default: throw new Error(`Unexpected type: ${field.type}`);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function compileFieldWrite(ctx, field, name) {
|
|
183
|
+
let prefix = 'pbf.write';
|
|
184
|
+
if (isPacked(field)) prefix += 'Packed';
|
|
185
|
+
|
|
186
|
+
if (fieldShouldUseStringAsNumber(field)) {
|
|
187
|
+
if (field.type === 'float' || field.type === 'double') {
|
|
188
|
+
name = `parseFloat(${name})`;
|
|
189
|
+
} else {
|
|
190
|
+
name = `parseInt(${name}, 10)`;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
const postfix = `${isPacked(field) ? '' : 'Field'}(${field.tag}, ${name})`;
|
|
194
|
+
|
|
195
|
+
const type = getType(ctx, field);
|
|
196
|
+
if (type) {
|
|
197
|
+
if (type._proto.fields) return `${prefix}Message(${field.tag}, write${type._name}, ${name})`;
|
|
198
|
+
if (type._proto.values) return `${prefix}Varint${postfix}`;
|
|
199
|
+
throw new Error(`Unexpected type: ${type._name}`);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
switch (field.type) {
|
|
203
|
+
case 'string': return `${prefix}String${postfix}`;
|
|
204
|
+
case 'float': return `${prefix}Float${postfix}`;
|
|
205
|
+
case 'double': return `${prefix}Double${postfix}`;
|
|
206
|
+
case 'bool': return `${prefix}Boolean${postfix}`;
|
|
207
|
+
case 'enum':
|
|
208
|
+
case 'uint32':
|
|
209
|
+
case 'uint64':
|
|
210
|
+
case 'int32':
|
|
211
|
+
case 'int64': return `${prefix}Varint${postfix}`;
|
|
212
|
+
case 'sint32':
|
|
213
|
+
case 'sint64': return `${prefix}SVarint${postfix}`;
|
|
214
|
+
case 'fixed32': return `${prefix}Fixed32${postfix}`;
|
|
215
|
+
case 'fixed64': return `${prefix}Fixed64${postfix}`;
|
|
216
|
+
case 'sfixed32': return `${prefix}SFixed32${postfix}`;
|
|
217
|
+
case 'sfixed64': return `${prefix}SFixed64${postfix}`;
|
|
218
|
+
case 'bytes': return `${prefix}Bytes${postfix}`;
|
|
219
|
+
default: throw new Error(`Unexpected type: ${field.type}`);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function compileMapRead(readCode, name) {
|
|
224
|
+
return `const {key, value} = ${readCode}; obj.${name}[key] = value`;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function compileRepeatedWrite(ctx, field) {
|
|
228
|
+
return `for (const item of obj.${field.name}) ${
|
|
229
|
+
compileFieldWrite(ctx, field, 'item')}`;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function compileMapWrite(ctx, field) {
|
|
233
|
+
const name = `obj.${field.name}`;
|
|
234
|
+
|
|
235
|
+
return `for (const key of Object.keys(${name})) ${
|
|
236
|
+
compileFieldWrite(ctx, field, `{key, value: ${name}[key]}`)}`;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function getMapMessageName(tag) {
|
|
240
|
+
return `_FieldEntry${tag}`;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function getMapField(name, type, tag) {
|
|
244
|
+
return {
|
|
245
|
+
name,
|
|
246
|
+
type,
|
|
247
|
+
tag,
|
|
248
|
+
map: null,
|
|
249
|
+
oneof: null,
|
|
250
|
+
required: false,
|
|
251
|
+
repeated: false,
|
|
252
|
+
options: {}
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function getMapMessage(field) {
|
|
257
|
+
return {
|
|
258
|
+
name: getMapMessageName(field.tag),
|
|
259
|
+
enums: [],
|
|
260
|
+
messages: [],
|
|
261
|
+
extensions: null,
|
|
262
|
+
fields: [
|
|
263
|
+
getMapField('key', field.map.from, 1),
|
|
264
|
+
getMapField('value', field.map.to, 2)
|
|
265
|
+
]
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// Protobuf identifier per spec: starts with a letter or underscore, followed by letters, digits, or underscores.
|
|
270
|
+
// Validated to prevent code injection via untrusted .proto schemas, since names are interpolated into generated JS.
|
|
271
|
+
const identifierRegex = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
272
|
+
|
|
273
|
+
function validateIdentifier(name) {
|
|
274
|
+
if (typeof name !== 'string' || !identifierRegex.test(name)) {
|
|
275
|
+
throw new Error(`Invalid protobuf identifier: ${JSON.stringify(name)}`);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function buildContext(proto, parent) {
|
|
280
|
+
const obj = Object.create(parent);
|
|
281
|
+
obj._proto = proto;
|
|
282
|
+
obj._children = [];
|
|
283
|
+
obj._defaults = {};
|
|
284
|
+
|
|
285
|
+
if (parent) {
|
|
286
|
+
validateIdentifier(proto.name);
|
|
287
|
+
parent[proto.name] = obj;
|
|
288
|
+
|
|
289
|
+
if (parent._name) {
|
|
290
|
+
obj._name = parent._name + proto.name;
|
|
291
|
+
} else {
|
|
292
|
+
obj._name = proto.name;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
if (proto.fields) {
|
|
297
|
+
for (const field of proto.fields) {
|
|
298
|
+
validateIdentifier(field.name);
|
|
299
|
+
if (field.oneof) validateIdentifier(field.oneof);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
if (proto.values) {
|
|
303
|
+
for (const valueName of Object.keys(proto.values)) {
|
|
304
|
+
validateIdentifier(valueName);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
for (let i = 0; proto.enums && i < proto.enums.length; i++) {
|
|
309
|
+
obj._children.push(buildContext(proto.enums[i], obj));
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
for (let i = 0; proto.messages && i < proto.messages.length; i++) {
|
|
313
|
+
obj._children.push(buildContext(proto.messages[i], obj));
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
for (let i = 0; proto.fields && i < proto.fields.length; i++) {
|
|
317
|
+
if (proto.fields[i].type === 'map') {
|
|
318
|
+
obj._children.push(buildContext(getMapMessage(proto.fields[i]), obj));
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
return obj;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function getDefaultValue(field, value) {
|
|
326
|
+
// Defaults not supported for repeated fields
|
|
327
|
+
if (field.repeated) return [];
|
|
328
|
+
let convertToStringIfNeeded = function (val) { return val; };
|
|
329
|
+
if (fieldShouldUseStringAsNumber(field)) {
|
|
330
|
+
convertToStringIfNeeded = function (val) { return val.toString(); };
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
switch (field.type) {
|
|
334
|
+
case 'float':
|
|
335
|
+
case 'double': return convertToStringIfNeeded(value ? parseFloat(value) : 0);
|
|
336
|
+
case 'uint32':
|
|
337
|
+
case 'uint64':
|
|
338
|
+
case 'int32':
|
|
339
|
+
case 'int64':
|
|
340
|
+
case 'sint32':
|
|
341
|
+
case 'sint64':
|
|
342
|
+
case 'fixed32':
|
|
343
|
+
case 'fixed64':
|
|
344
|
+
case 'sfixed32':
|
|
345
|
+
case 'sfixed64': return convertToStringIfNeeded(value ? parseInt(value, 10) : 0);
|
|
346
|
+
case 'string': return value || '';
|
|
347
|
+
case 'bool': return value === 'true';
|
|
348
|
+
case 'map': return {};
|
|
349
|
+
default: return undefined;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function willSupportPacked(ctx, field) {
|
|
354
|
+
const fieldType = isEnum(getType(ctx, field)) ? 'enum' : field.type;
|
|
355
|
+
|
|
356
|
+
switch (field.repeated && fieldType) {
|
|
357
|
+
case 'float':
|
|
358
|
+
case 'double':
|
|
359
|
+
case 'uint32':
|
|
360
|
+
case 'uint64':
|
|
361
|
+
case 'int32':
|
|
362
|
+
case 'int64':
|
|
363
|
+
case 'sint32':
|
|
364
|
+
case 'sint64':
|
|
365
|
+
case 'fixed32':
|
|
366
|
+
case 'fixed64':
|
|
367
|
+
case 'sfixed32':
|
|
368
|
+
case 'enum':
|
|
369
|
+
case 'bool': return true;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
return false;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function setPackedOption(ctx, field, syntax) {
|
|
376
|
+
// No default packed in older protobuf versions
|
|
377
|
+
if (syntax < 3) return;
|
|
378
|
+
|
|
379
|
+
// Packed option already set
|
|
380
|
+
if (field.options.packed !== undefined) return;
|
|
381
|
+
|
|
382
|
+
// Not a packed field type
|
|
383
|
+
if (!willSupportPacked(ctx, field)) return;
|
|
384
|
+
|
|
385
|
+
field.options.packed = 'true';
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
function setDefaultValue(ctx, field, syntax) {
|
|
389
|
+
const options = field.options;
|
|
390
|
+
const type = getType(ctx, field);
|
|
391
|
+
const enumValues = type && type._proto.values && getEnumValues(type);
|
|
392
|
+
|
|
393
|
+
// Proto3 does not support overriding defaults
|
|
394
|
+
const explicitDefault = syntax < 3 ? options.default : undefined;
|
|
395
|
+
|
|
396
|
+
// Set default for enum values
|
|
397
|
+
if (enumValues && !field.repeated) {
|
|
398
|
+
ctx._defaults[field.name] = enumValues[explicitDefault] || 0;
|
|
399
|
+
|
|
400
|
+
} else {
|
|
401
|
+
ctx._defaults[field.name] = getDefaultValue(field, explicitDefault);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function buildDefaults(ctx, syntax) {
|
|
406
|
+
const proto = ctx._proto;
|
|
407
|
+
|
|
408
|
+
for (let i = 0; i < ctx._children.length; i++) {
|
|
409
|
+
buildDefaults(ctx._children[i], syntax);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
if (proto.fields) {
|
|
413
|
+
for (let i = 0; i < proto.fields.length; i++) {
|
|
414
|
+
setPackedOption(ctx, proto.fields[i], syntax);
|
|
415
|
+
setDefaultValue(ctx, proto.fields[i], syntax);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
return ctx;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function getDefaultWriteTest(ctx, field) {
|
|
423
|
+
const def = ctx._defaults[field.name];
|
|
424
|
+
const type = getType(ctx, field);
|
|
425
|
+
let code = ` if (obj.${field.name}`;
|
|
426
|
+
|
|
427
|
+
if (!field.repeated && (!type || !type._proto.fields)) {
|
|
428
|
+
if (def === undefined || def || field.oneof) {
|
|
429
|
+
code += ' != null';
|
|
430
|
+
}
|
|
431
|
+
if (def) {
|
|
432
|
+
code += ` && obj.${field.name} !== ${JSON.stringify(def)}`;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
return `${code}) `;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
function isPacked(field) {
|
|
440
|
+
return field.options.packed === 'true';
|
|
441
|
+
}
|