protobufjs 8.6.2 → 8.6.4
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 +39 -39
- package/README.md +441 -441
- package/dist/light/protobuf.js +8582 -8454
- package/dist/light/protobuf.js.map +1 -1
- package/dist/light/protobuf.min.js +3 -3
- package/dist/light/protobuf.min.js.map +1 -1
- package/dist/minimal/protobuf.js +3071 -2999
- package/dist/minimal/protobuf.js.map +1 -1
- package/dist/minimal/protobuf.min.js +3 -3
- package/dist/minimal/protobuf.min.js.map +1 -1
- package/dist/protobuf.js +10486 -10358
- package/dist/protobuf.js.map +1 -1
- package/dist/protobuf.min.js +3 -3
- package/dist/protobuf.min.js.map +1 -1
- package/ext/README.md +70 -70
- package/ext/debug/README.md +4 -4
- package/ext/debug/index.js +71 -71
- package/ext/descriptor/README.md +5 -5
- package/ext/descriptor/index.d.ts +2 -2
- package/ext/descriptor/index.js +2 -2
- package/ext/descriptor.d.ts +87 -87
- package/ext/descriptor.js +1354 -1354
- package/ext/protojson.LICENSE +201 -201
- package/ext/protojson.d.ts +20 -20
- package/ext/protojson.js +945 -925
- package/ext/textformat.d.ts +19 -19
- package/ext/textformat.js +1256 -1256
- package/google/LICENSE +27 -27
- package/google/README.md +1 -1
- package/google/api/annotations.json +82 -82
- package/google/api/annotations.proto +10 -10
- package/google/api/http.json +85 -85
- package/google/api/http.proto +30 -30
- package/google/protobuf/api.json +117 -117
- package/google/protobuf/api.proto +33 -33
- package/google/protobuf/compiler/plugin.json +126 -126
- package/google/protobuf/compiler/plugin.proto +47 -47
- package/google/protobuf/descriptor.json +1381 -1381
- package/google/protobuf/descriptor.proto +534 -534
- package/google/protobuf/source_context.json +19 -19
- package/google/protobuf/source_context.proto +7 -7
- package/google/protobuf/type.json +201 -201
- package/google/protobuf/type.proto +89 -89
- package/index.d.ts +30 -0
- package/index.js +4 -4
- package/light.d.ts +2 -2
- package/light.js +3 -3
- package/minimal.d.ts +2 -2
- package/minimal.js +4 -4
- package/package.json +93 -93
- package/src/common.js +399 -399
- package/src/converter.js +344 -344
- package/src/decoder.js +214 -208
- package/src/encoder.js +111 -111
- package/src/enum.js +231 -231
- package/src/field.js +497 -497
- package/src/index-light.js +104 -104
- package/src/index-minimal.js +35 -36
- package/src/index.js +12 -12
- package/src/mapfield.js +136 -136
- package/src/message.js +137 -137
- package/src/method.js +175 -175
- package/src/namespace.js +566 -565
- package/src/object.js +382 -382
- package/src/oneof.js +225 -225
- package/src/parse.js +1068 -1068
- package/src/reader.js +558 -543
- package/src/reader_buffer.js +72 -72
- package/src/root.js +416 -416
- package/src/roots.js +18 -18
- package/src/rpc/service.js +148 -148
- package/src/rpc.js +36 -36
- package/src/service.js +198 -198
- package/src/tokenize.js +421 -421
- package/src/type.js +654 -655
- package/src/types.js +196 -196
- package/src/typescript.js +25 -25
- package/src/util/aspromise.d.ts +13 -13
- package/src/util/aspromise.js +52 -52
- package/src/util/base64.d.ts +32 -32
- package/src/util/base64.js +146 -146
- package/src/util/codegen.d.ts +31 -31
- package/src/util/codegen.js +113 -113
- package/src/util/eventemitter.d.ts +45 -45
- package/src/util/eventemitter.js +86 -86
- package/src/util/fetch.d.ts +56 -56
- package/src/util/fetch.js +112 -112
- package/src/util/float.d.ts +83 -83
- package/src/util/float.js +335 -335
- package/src/util/fs.js +11 -11
- package/src/util/longbits.js +200 -200
- package/src/util/minimal.js +515 -515
- package/src/util/path.d.ts +22 -22
- package/src/util/path.js +100 -72
- package/src/util/patterns.js +7 -7
- package/src/util/pool.d.ts +32 -32
- package/src/util/pool.js +48 -48
- package/src/util/utf8.d.ts +24 -24
- package/src/util/utf8.js +188 -130
- package/src/util.js +264 -242
- package/src/verifier.js +180 -180
- package/src/wrappers.js +106 -106
- package/src/writer.js +495 -495
- package/src/writer_buffer.js +102 -102
- package/tsconfig.json +6 -6
package/src/util/base64.js
CHANGED
|
@@ -1,146 +1,146 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* A minimal base64 implementation for number arrays.
|
|
5
|
-
* @memberof util
|
|
6
|
-
* @namespace
|
|
7
|
-
*/
|
|
8
|
-
var base64 = exports;
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Calculates the byte length of a base64 encoded string.
|
|
12
|
-
* @param {string} string Base64 encoded string
|
|
13
|
-
* @returns {number} Byte length
|
|
14
|
-
*/
|
|
15
|
-
base64.length = function length(string) {
|
|
16
|
-
var p = string.length;
|
|
17
|
-
if (!p)
|
|
18
|
-
return 0;
|
|
19
|
-
while (p > 0 && string.charAt(p - 1) === "=")
|
|
20
|
-
--p;
|
|
21
|
-
return Math.floor(p * 3 / 4);
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
// Base64 encoding table
|
|
25
|
-
var b64 = new Array(64);
|
|
26
|
-
|
|
27
|
-
// Base64 decoding table
|
|
28
|
-
var s64 = new Array(123);
|
|
29
|
-
|
|
30
|
-
// 65..90, 97..122, 48..57, 43, 47
|
|
31
|
-
for (var i = 0; i < 64;)
|
|
32
|
-
s64[b64[i] = i < 26 ? i + 65 : i < 52 ? i + 71 : i < 62 ? i - 4 : i - 59 | 43] = i++;
|
|
33
|
-
|
|
34
|
-
s64[45] = 62; // - -> +
|
|
35
|
-
s64[95] = 63; // _ -> /
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Encodes a buffer to a base64 encoded string.
|
|
39
|
-
* @param {Uint8Array} buffer Source buffer
|
|
40
|
-
* @param {number} start Source start
|
|
41
|
-
* @param {number} end Source end
|
|
42
|
-
* @returns {string} Base64 encoded string
|
|
43
|
-
*/
|
|
44
|
-
base64.encode = function encode(buffer, start, end) {
|
|
45
|
-
var parts = null,
|
|
46
|
-
chunk = [];
|
|
47
|
-
var i = 0, // output index
|
|
48
|
-
j = 0, // goto index
|
|
49
|
-
t; // temporary
|
|
50
|
-
while (start < end) {
|
|
51
|
-
var b = buffer[start++];
|
|
52
|
-
switch (j) {
|
|
53
|
-
case 0:
|
|
54
|
-
chunk[i++] = b64[b >> 2];
|
|
55
|
-
t = (b & 3) << 4;
|
|
56
|
-
j = 1;
|
|
57
|
-
break;
|
|
58
|
-
case 1:
|
|
59
|
-
chunk[i++] = b64[t | b >> 4];
|
|
60
|
-
t = (b & 15) << 2;
|
|
61
|
-
j = 2;
|
|
62
|
-
break;
|
|
63
|
-
case 2:
|
|
64
|
-
chunk[i++] = b64[t | b >> 6];
|
|
65
|
-
chunk[i++] = b64[b & 63];
|
|
66
|
-
j = 0;
|
|
67
|
-
break;
|
|
68
|
-
}
|
|
69
|
-
if (i > 8191) {
|
|
70
|
-
(parts || (parts = [])).push(String.fromCharCode.apply(String, chunk));
|
|
71
|
-
i = 0;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
if (j) {
|
|
75
|
-
chunk[i++] = b64[t];
|
|
76
|
-
chunk[i++] = 61;
|
|
77
|
-
if (j === 1)
|
|
78
|
-
chunk[i++] = 61;
|
|
79
|
-
}
|
|
80
|
-
if (parts) {
|
|
81
|
-
if (i)
|
|
82
|
-
parts.push(String.fromCharCode.apply(String, chunk.slice(0, i)));
|
|
83
|
-
return parts.join("");
|
|
84
|
-
}
|
|
85
|
-
return String.fromCharCode.apply(String, chunk.slice(0, i));
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
var invalidEncoding = "invalid encoding";
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Decodes a base64 encoded string to a buffer.
|
|
92
|
-
* @param {string} string Source string
|
|
93
|
-
* @param {Uint8Array} buffer Destination buffer
|
|
94
|
-
* @param {number} offset Destination offset
|
|
95
|
-
* @returns {number} Number of bytes written
|
|
96
|
-
* @throws {Error} If encoding is invalid
|
|
97
|
-
*/
|
|
98
|
-
base64.decode = function decode(string, buffer, offset) {
|
|
99
|
-
var start = offset;
|
|
100
|
-
var j = 0, // goto index
|
|
101
|
-
t; // temporary
|
|
102
|
-
for (var i = 0; i < string.length;) {
|
|
103
|
-
var c = string.charCodeAt(i++);
|
|
104
|
-
if (c === 61 && j > 1)
|
|
105
|
-
break;
|
|
106
|
-
if ((c = s64[c]) === undefined)
|
|
107
|
-
throw Error(invalidEncoding);
|
|
108
|
-
switch (j) {
|
|
109
|
-
case 0:
|
|
110
|
-
t = c;
|
|
111
|
-
j = 1;
|
|
112
|
-
break;
|
|
113
|
-
case 1:
|
|
114
|
-
buffer[offset++] = t << 2 | (c & 48) >> 4;
|
|
115
|
-
t = c;
|
|
116
|
-
j = 2;
|
|
117
|
-
break;
|
|
118
|
-
case 2:
|
|
119
|
-
buffer[offset++] = (t & 15) << 4 | (c & 60) >> 2;
|
|
120
|
-
t = c;
|
|
121
|
-
j = 3;
|
|
122
|
-
break;
|
|
123
|
-
case 3:
|
|
124
|
-
buffer[offset++] = (t & 3) << 6 | c;
|
|
125
|
-
j = 0;
|
|
126
|
-
break;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
if (j === 1)
|
|
130
|
-
throw Error(invalidEncoding);
|
|
131
|
-
return offset - start;
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
var base64Re = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/,
|
|
135
|
-
base64UrlRe = /[-_]/,
|
|
136
|
-
base64UrlNoPaddingRe = /^(?:[A-Za-z0-9_-]{4})*(?:[A-Za-z0-9_-]{2}(?:==)?|[A-Za-z0-9_-]{3}=?)?$/;
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Tests if the specified string appears to be base64 encoded.
|
|
140
|
-
* @param {string} string String to test
|
|
141
|
-
* @returns {boolean} `true` if probably base64 encoded, otherwise false
|
|
142
|
-
*/
|
|
143
|
-
base64.test = function test(string) {
|
|
144
|
-
return base64Re.test(string)
|
|
145
|
-
|| base64UrlRe.test(string) && base64UrlNoPaddingRe.test(string);
|
|
146
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A minimal base64 implementation for number arrays.
|
|
5
|
+
* @memberof util
|
|
6
|
+
* @namespace
|
|
7
|
+
*/
|
|
8
|
+
var base64 = exports;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Calculates the byte length of a base64 encoded string.
|
|
12
|
+
* @param {string} string Base64 encoded string
|
|
13
|
+
* @returns {number} Byte length
|
|
14
|
+
*/
|
|
15
|
+
base64.length = function length(string) {
|
|
16
|
+
var p = string.length;
|
|
17
|
+
if (!p)
|
|
18
|
+
return 0;
|
|
19
|
+
while (p > 0 && string.charAt(p - 1) === "=")
|
|
20
|
+
--p;
|
|
21
|
+
return Math.floor(p * 3 / 4);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// Base64 encoding table
|
|
25
|
+
var b64 = new Array(64);
|
|
26
|
+
|
|
27
|
+
// Base64 decoding table
|
|
28
|
+
var s64 = new Array(123);
|
|
29
|
+
|
|
30
|
+
// 65..90, 97..122, 48..57, 43, 47
|
|
31
|
+
for (var i = 0; i < 64;)
|
|
32
|
+
s64[b64[i] = i < 26 ? i + 65 : i < 52 ? i + 71 : i < 62 ? i - 4 : i - 59 | 43] = i++;
|
|
33
|
+
|
|
34
|
+
s64[45] = 62; // - -> +
|
|
35
|
+
s64[95] = 63; // _ -> /
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Encodes a buffer to a base64 encoded string.
|
|
39
|
+
* @param {Uint8Array} buffer Source buffer
|
|
40
|
+
* @param {number} start Source start
|
|
41
|
+
* @param {number} end Source end
|
|
42
|
+
* @returns {string} Base64 encoded string
|
|
43
|
+
*/
|
|
44
|
+
base64.encode = function encode(buffer, start, end) {
|
|
45
|
+
var parts = null,
|
|
46
|
+
chunk = [];
|
|
47
|
+
var i = 0, // output index
|
|
48
|
+
j = 0, // goto index
|
|
49
|
+
t; // temporary
|
|
50
|
+
while (start < end) {
|
|
51
|
+
var b = buffer[start++];
|
|
52
|
+
switch (j) {
|
|
53
|
+
case 0:
|
|
54
|
+
chunk[i++] = b64[b >> 2];
|
|
55
|
+
t = (b & 3) << 4;
|
|
56
|
+
j = 1;
|
|
57
|
+
break;
|
|
58
|
+
case 1:
|
|
59
|
+
chunk[i++] = b64[t | b >> 4];
|
|
60
|
+
t = (b & 15) << 2;
|
|
61
|
+
j = 2;
|
|
62
|
+
break;
|
|
63
|
+
case 2:
|
|
64
|
+
chunk[i++] = b64[t | b >> 6];
|
|
65
|
+
chunk[i++] = b64[b & 63];
|
|
66
|
+
j = 0;
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
if (i > 8191) {
|
|
70
|
+
(parts || (parts = [])).push(String.fromCharCode.apply(String, chunk));
|
|
71
|
+
i = 0;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (j) {
|
|
75
|
+
chunk[i++] = b64[t];
|
|
76
|
+
chunk[i++] = 61;
|
|
77
|
+
if (j === 1)
|
|
78
|
+
chunk[i++] = 61;
|
|
79
|
+
}
|
|
80
|
+
if (parts) {
|
|
81
|
+
if (i)
|
|
82
|
+
parts.push(String.fromCharCode.apply(String, chunk.slice(0, i)));
|
|
83
|
+
return parts.join("");
|
|
84
|
+
}
|
|
85
|
+
return String.fromCharCode.apply(String, chunk.slice(0, i));
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
var invalidEncoding = "invalid encoding";
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Decodes a base64 encoded string to a buffer.
|
|
92
|
+
* @param {string} string Source string
|
|
93
|
+
* @param {Uint8Array} buffer Destination buffer
|
|
94
|
+
* @param {number} offset Destination offset
|
|
95
|
+
* @returns {number} Number of bytes written
|
|
96
|
+
* @throws {Error} If encoding is invalid
|
|
97
|
+
*/
|
|
98
|
+
base64.decode = function decode(string, buffer, offset) {
|
|
99
|
+
var start = offset;
|
|
100
|
+
var j = 0, // goto index
|
|
101
|
+
t; // temporary
|
|
102
|
+
for (var i = 0; i < string.length;) {
|
|
103
|
+
var c = string.charCodeAt(i++);
|
|
104
|
+
if (c === 61 && j > 1)
|
|
105
|
+
break;
|
|
106
|
+
if ((c = s64[c]) === undefined)
|
|
107
|
+
throw Error(invalidEncoding);
|
|
108
|
+
switch (j) {
|
|
109
|
+
case 0:
|
|
110
|
+
t = c;
|
|
111
|
+
j = 1;
|
|
112
|
+
break;
|
|
113
|
+
case 1:
|
|
114
|
+
buffer[offset++] = t << 2 | (c & 48) >> 4;
|
|
115
|
+
t = c;
|
|
116
|
+
j = 2;
|
|
117
|
+
break;
|
|
118
|
+
case 2:
|
|
119
|
+
buffer[offset++] = (t & 15) << 4 | (c & 60) >> 2;
|
|
120
|
+
t = c;
|
|
121
|
+
j = 3;
|
|
122
|
+
break;
|
|
123
|
+
case 3:
|
|
124
|
+
buffer[offset++] = (t & 3) << 6 | c;
|
|
125
|
+
j = 0;
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (j === 1)
|
|
130
|
+
throw Error(invalidEncoding);
|
|
131
|
+
return offset - start;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
var base64Re = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/,
|
|
135
|
+
base64UrlRe = /[-_]/,
|
|
136
|
+
base64UrlNoPaddingRe = /^(?:[A-Za-z0-9_-]{4})*(?:[A-Za-z0-9_-]{2}(?:==)?|[A-Za-z0-9_-]{3}=?)?$/;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Tests if the specified string appears to be base64 encoded.
|
|
140
|
+
* @param {string} string String to test
|
|
141
|
+
* @returns {boolean} `true` if probably base64 encoded, otherwise false
|
|
142
|
+
*/
|
|
143
|
+
base64.test = function test(string) {
|
|
144
|
+
return base64Re.test(string)
|
|
145
|
+
|| base64UrlRe.test(string) && base64UrlNoPaddingRe.test(string);
|
|
146
|
+
};
|
package/src/util/codegen.d.ts
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
export = codegen;
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Appends code to the function's body.
|
|
5
|
-
* @param [formatStringOrScope] Format string or, to finish the function, an object of additional scope variables, if any
|
|
6
|
-
* @param [formatParams] Format parameters
|
|
7
|
-
* @returns Itself or the generated function if finished
|
|
8
|
-
* @throws {Error} If format parameter counts do not match
|
|
9
|
-
*/
|
|
10
|
-
type Codegen = (formatStringOrScope?: (string|{ [k: string]: any }), ...formatParams: any[]) => (Codegen|Function);
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Begins generating a function.
|
|
14
|
-
* @param functionParams Function parameter names
|
|
15
|
-
* @param [functionName] Function name if not anonymous
|
|
16
|
-
* @returns Appender that appends code to the function's body
|
|
17
|
-
*/
|
|
18
|
-
declare function codegen(functionParams: string[], functionName?: string): Codegen;
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Begins generating a function.
|
|
22
|
-
* @param [functionName] Function name if not anonymous
|
|
23
|
-
* @returns Appender that appends code to the function's body
|
|
24
|
-
*/
|
|
25
|
-
declare function codegen(functionName?: string): Codegen;
|
|
26
|
-
|
|
27
|
-
declare namespace codegen {
|
|
28
|
-
|
|
29
|
-
/** When set to `true`, codegen will log generated code to console. Useful for debugging. */
|
|
30
|
-
let verbose: boolean;
|
|
31
|
-
}
|
|
1
|
+
export = codegen;
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Appends code to the function's body.
|
|
5
|
+
* @param [formatStringOrScope] Format string or, to finish the function, an object of additional scope variables, if any
|
|
6
|
+
* @param [formatParams] Format parameters
|
|
7
|
+
* @returns Itself or the generated function if finished
|
|
8
|
+
* @throws {Error} If format parameter counts do not match
|
|
9
|
+
*/
|
|
10
|
+
type Codegen = (formatStringOrScope?: (string|{ [k: string]: any }), ...formatParams: any[]) => (Codegen|Function);
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Begins generating a function.
|
|
14
|
+
* @param functionParams Function parameter names
|
|
15
|
+
* @param [functionName] Function name if not anonymous
|
|
16
|
+
* @returns Appender that appends code to the function's body
|
|
17
|
+
*/
|
|
18
|
+
declare function codegen(functionParams: string[], functionName?: string): Codegen;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Begins generating a function.
|
|
22
|
+
* @param [functionName] Function name if not anonymous
|
|
23
|
+
* @returns Appender that appends code to the function's body
|
|
24
|
+
*/
|
|
25
|
+
declare function codegen(functionName?: string): Codegen;
|
|
26
|
+
|
|
27
|
+
declare namespace codegen {
|
|
28
|
+
|
|
29
|
+
/** When set to `true`, codegen will log generated code to console. Useful for debugging. */
|
|
30
|
+
let verbose: boolean;
|
|
31
|
+
}
|
package/src/util/codegen.js
CHANGED
|
@@ -1,113 +1,113 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
module.exports = codegen;
|
|
3
|
-
|
|
4
|
-
var patterns = require("./patterns");
|
|
5
|
-
var reservedRe = patterns.reservedRe;
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Begins generating a function.
|
|
9
|
-
* @memberof util
|
|
10
|
-
* @param {string[]} functionParams Function parameter names
|
|
11
|
-
* @param {string} [functionName] Function name if not anonymous
|
|
12
|
-
* @returns {Codegen} Appender that appends code to the function's body
|
|
13
|
-
*/
|
|
14
|
-
function codegen(functionParams, functionName) {
|
|
15
|
-
|
|
16
|
-
/* istanbul ignore if */
|
|
17
|
-
if (typeof functionParams === "string") {
|
|
18
|
-
functionName = functionParams;
|
|
19
|
-
functionParams = undefined;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
var body = [];
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Appends code to the function's body or finishes generation.
|
|
26
|
-
* @typedef Codegen
|
|
27
|
-
* @type {function}
|
|
28
|
-
* @param {string|Object.<string,*>} [formatStringOrScope] Format string or, to finish the function, an object of additional scope variables, if any
|
|
29
|
-
* @param {...*} [formatParams] Format parameters
|
|
30
|
-
* @returns {Codegen|Function} Itself or the generated function if finished
|
|
31
|
-
* @throws {Error} If format parameter counts do not match
|
|
32
|
-
*/
|
|
33
|
-
|
|
34
|
-
function Codegen(formatStringOrScope) {
|
|
35
|
-
// note that explicit array handling below makes this ~50% faster
|
|
36
|
-
|
|
37
|
-
// finish the function
|
|
38
|
-
if (typeof formatStringOrScope !== "string") {
|
|
39
|
-
var source = toString();
|
|
40
|
-
if (codegen.verbose)
|
|
41
|
-
console.log("codegen: " + source); // eslint-disable-line no-console
|
|
42
|
-
source = "return " + source;
|
|
43
|
-
if (formatStringOrScope) {
|
|
44
|
-
var scopeKeys = Object.keys(formatStringOrScope),
|
|
45
|
-
scopeParams = new Array(scopeKeys.length + 1),
|
|
46
|
-
scopeValues = new Array(scopeKeys.length),
|
|
47
|
-
scopeOffset = 0;
|
|
48
|
-
while (scopeOffset < scopeKeys.length) {
|
|
49
|
-
scopeParams[scopeOffset] = scopeKeys[scopeOffset];
|
|
50
|
-
scopeValues[scopeOffset] = formatStringOrScope[scopeKeys[scopeOffset++]];
|
|
51
|
-
}
|
|
52
|
-
scopeParams[scopeOffset] = source;
|
|
53
|
-
return Function.apply(null, scopeParams).apply(null, scopeValues); // eslint-disable-line no-new-func
|
|
54
|
-
}
|
|
55
|
-
return Function(source)(); // eslint-disable-line no-new-func
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// otherwise append to body
|
|
59
|
-
var formatParams = new Array(arguments.length - 1),
|
|
60
|
-
formatOffset = 0;
|
|
61
|
-
while (formatOffset < formatParams.length)
|
|
62
|
-
formatParams[formatOffset] = arguments[++formatOffset];
|
|
63
|
-
formatOffset = 0;
|
|
64
|
-
formatStringOrScope = formatStringOrScope.replace(/%([%dfijs])/g, function replace($0, $1) {
|
|
65
|
-
var value = formatParams[formatOffset++];
|
|
66
|
-
switch ($1) {
|
|
67
|
-
case "d": case "f": return String(Number(value));
|
|
68
|
-
case "i": return String(Math.floor(value));
|
|
69
|
-
case "j": return JSON.stringify(value);
|
|
70
|
-
case "s": return String(value);
|
|
71
|
-
}
|
|
72
|
-
return "%";
|
|
73
|
-
});
|
|
74
|
-
if (formatOffset !== formatParams.length)
|
|
75
|
-
throw Error("parameter count mismatch");
|
|
76
|
-
body.push(formatStringOrScope);
|
|
77
|
-
return Codegen;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function toString(functionNameOverride) {
|
|
81
|
-
return "function " + safeFunctionName(functionNameOverride || functionName) + "(" + (functionParams && functionParams.join(",") || "") + "){\n " + body.join("\n ") + "\n}";
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
Codegen.toString = toString;
|
|
85
|
-
return Codegen;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Begins generating a function.
|
|
90
|
-
* @memberof util
|
|
91
|
-
* @function codegen
|
|
92
|
-
* @param {string} [functionName] Function name if not anonymous
|
|
93
|
-
* @returns {Codegen} Appender that appends code to the function's body
|
|
94
|
-
* @variation 2
|
|
95
|
-
*/
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* When set to `true`, codegen will log generated code to console. Useful for debugging.
|
|
99
|
-
* @name util.codegen.verbose
|
|
100
|
-
* @type {boolean}
|
|
101
|
-
*/
|
|
102
|
-
codegen.verbose = false;
|
|
103
|
-
|
|
104
|
-
function safeFunctionName(name) {
|
|
105
|
-
if (!name)
|
|
106
|
-
return "";
|
|
107
|
-
name = String(name).replace(/[^\w$]/g, "");
|
|
108
|
-
if (!name)
|
|
109
|
-
return "";
|
|
110
|
-
if (/^\d/.test(name))
|
|
111
|
-
name = "_" + name;
|
|
112
|
-
return reservedRe.test(name) ? name + "_" : name;
|
|
113
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
module.exports = codegen;
|
|
3
|
+
|
|
4
|
+
var patterns = require("./patterns");
|
|
5
|
+
var reservedRe = patterns.reservedRe;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Begins generating a function.
|
|
9
|
+
* @memberof util
|
|
10
|
+
* @param {string[]} functionParams Function parameter names
|
|
11
|
+
* @param {string} [functionName] Function name if not anonymous
|
|
12
|
+
* @returns {Codegen} Appender that appends code to the function's body
|
|
13
|
+
*/
|
|
14
|
+
function codegen(functionParams, functionName) {
|
|
15
|
+
|
|
16
|
+
/* istanbul ignore if */
|
|
17
|
+
if (typeof functionParams === "string") {
|
|
18
|
+
functionName = functionParams;
|
|
19
|
+
functionParams = undefined;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
var body = [];
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Appends code to the function's body or finishes generation.
|
|
26
|
+
* @typedef Codegen
|
|
27
|
+
* @type {function}
|
|
28
|
+
* @param {string|Object.<string,*>} [formatStringOrScope] Format string or, to finish the function, an object of additional scope variables, if any
|
|
29
|
+
* @param {...*} [formatParams] Format parameters
|
|
30
|
+
* @returns {Codegen|Function} Itself or the generated function if finished
|
|
31
|
+
* @throws {Error} If format parameter counts do not match
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
function Codegen(formatStringOrScope) {
|
|
35
|
+
// note that explicit array handling below makes this ~50% faster
|
|
36
|
+
|
|
37
|
+
// finish the function
|
|
38
|
+
if (typeof formatStringOrScope !== "string") {
|
|
39
|
+
var source = toString();
|
|
40
|
+
if (codegen.verbose)
|
|
41
|
+
console.log("codegen: " + source); // eslint-disable-line no-console
|
|
42
|
+
source = "return " + source;
|
|
43
|
+
if (formatStringOrScope) {
|
|
44
|
+
var scopeKeys = Object.keys(formatStringOrScope),
|
|
45
|
+
scopeParams = new Array(scopeKeys.length + 1),
|
|
46
|
+
scopeValues = new Array(scopeKeys.length),
|
|
47
|
+
scopeOffset = 0;
|
|
48
|
+
while (scopeOffset < scopeKeys.length) {
|
|
49
|
+
scopeParams[scopeOffset] = scopeKeys[scopeOffset];
|
|
50
|
+
scopeValues[scopeOffset] = formatStringOrScope[scopeKeys[scopeOffset++]];
|
|
51
|
+
}
|
|
52
|
+
scopeParams[scopeOffset] = source;
|
|
53
|
+
return Function.apply(null, scopeParams).apply(null, scopeValues); // eslint-disable-line no-new-func
|
|
54
|
+
}
|
|
55
|
+
return Function(source)(); // eslint-disable-line no-new-func
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// otherwise append to body
|
|
59
|
+
var formatParams = new Array(arguments.length - 1),
|
|
60
|
+
formatOffset = 0;
|
|
61
|
+
while (formatOffset < formatParams.length)
|
|
62
|
+
formatParams[formatOffset] = arguments[++formatOffset];
|
|
63
|
+
formatOffset = 0;
|
|
64
|
+
formatStringOrScope = formatStringOrScope.replace(/%([%dfijs])/g, function replace($0, $1) {
|
|
65
|
+
var value = formatParams[formatOffset++];
|
|
66
|
+
switch ($1) {
|
|
67
|
+
case "d": case "f": return String(Number(value));
|
|
68
|
+
case "i": return String(Math.floor(value));
|
|
69
|
+
case "j": return JSON.stringify(value);
|
|
70
|
+
case "s": return String(value);
|
|
71
|
+
}
|
|
72
|
+
return "%";
|
|
73
|
+
});
|
|
74
|
+
if (formatOffset !== formatParams.length)
|
|
75
|
+
throw Error("parameter count mismatch");
|
|
76
|
+
body.push(formatStringOrScope);
|
|
77
|
+
return Codegen;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function toString(functionNameOverride) {
|
|
81
|
+
return "function " + safeFunctionName(functionNameOverride || functionName) + "(" + (functionParams && functionParams.join(",") || "") + "){\n " + body.join("\n ") + "\n}";
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
Codegen.toString = toString;
|
|
85
|
+
return Codegen;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Begins generating a function.
|
|
90
|
+
* @memberof util
|
|
91
|
+
* @function codegen
|
|
92
|
+
* @param {string} [functionName] Function name if not anonymous
|
|
93
|
+
* @returns {Codegen} Appender that appends code to the function's body
|
|
94
|
+
* @variation 2
|
|
95
|
+
*/
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* When set to `true`, codegen will log generated code to console. Useful for debugging.
|
|
99
|
+
* @name util.codegen.verbose
|
|
100
|
+
* @type {boolean}
|
|
101
|
+
*/
|
|
102
|
+
codegen.verbose = false;
|
|
103
|
+
|
|
104
|
+
function safeFunctionName(name) {
|
|
105
|
+
if (!name)
|
|
106
|
+
return "";
|
|
107
|
+
name = String(name).replace(/[^\w$]/g, "");
|
|
108
|
+
if (!name)
|
|
109
|
+
return "";
|
|
110
|
+
if (/^\d/.test(name))
|
|
111
|
+
name = "_" + name;
|
|
112
|
+
return reservedRe.test(name) ? name + "_" : name;
|
|
113
|
+
}
|
|
@@ -1,45 +1,45 @@
|
|
|
1
|
-
export = EventEmitter;
|
|
2
|
-
|
|
3
|
-
type EventEmitterListener = (...args: any[]) => {};
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Constructs a new event emitter instance.
|
|
7
|
-
* @classdesc A minimal event emitter.
|
|
8
|
-
* @memberof util
|
|
9
|
-
* @constructor
|
|
10
|
-
*/
|
|
11
|
-
declare class EventEmitter {
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Constructs a new event emitter instance.
|
|
15
|
-
* @classdesc A minimal event emitter.
|
|
16
|
-
* @memberof util
|
|
17
|
-
* @constructor
|
|
18
|
-
*/
|
|
19
|
-
constructor();
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Registers an event listener.
|
|
23
|
-
* @param {string} evt Event name
|
|
24
|
-
* @param {EventEmitterListener} fn Listener
|
|
25
|
-
* @param {*} [ctx] Listener context
|
|
26
|
-
* @returns {this} `this`
|
|
27
|
-
*/
|
|
28
|
-
public on(evt: string, fn: EventEmitterListener, ctx?: any): EventEmitter;
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Removes an event listener or any matching listeners if arguments are omitted.
|
|
32
|
-
* @param {string} [evt] Event name. Removes all listeners if omitted.
|
|
33
|
-
* @param {EventEmitterListener} [fn] Listener to remove. Removes all listeners of `evt` if omitted.
|
|
34
|
-
* @returns {this} `this`
|
|
35
|
-
*/
|
|
36
|
-
public off(evt?: string, fn?: EventEmitterListener): EventEmitter;
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Emits an event by calling its listeners with the specified arguments.
|
|
40
|
-
* @param {string} evt Event name
|
|
41
|
-
* @param {...*} args Arguments
|
|
42
|
-
* @returns {this} `this`
|
|
43
|
-
*/
|
|
44
|
-
public emit(evt: string, ...args: any[]): EventEmitter;
|
|
45
|
-
}
|
|
1
|
+
export = EventEmitter;
|
|
2
|
+
|
|
3
|
+
type EventEmitterListener = (...args: any[]) => {};
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Constructs a new event emitter instance.
|
|
7
|
+
* @classdesc A minimal event emitter.
|
|
8
|
+
* @memberof util
|
|
9
|
+
* @constructor
|
|
10
|
+
*/
|
|
11
|
+
declare class EventEmitter {
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Constructs a new event emitter instance.
|
|
15
|
+
* @classdesc A minimal event emitter.
|
|
16
|
+
* @memberof util
|
|
17
|
+
* @constructor
|
|
18
|
+
*/
|
|
19
|
+
constructor();
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Registers an event listener.
|
|
23
|
+
* @param {string} evt Event name
|
|
24
|
+
* @param {EventEmitterListener} fn Listener
|
|
25
|
+
* @param {*} [ctx] Listener context
|
|
26
|
+
* @returns {this} `this`
|
|
27
|
+
*/
|
|
28
|
+
public on(evt: string, fn: EventEmitterListener, ctx?: any): EventEmitter;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Removes an event listener or any matching listeners if arguments are omitted.
|
|
32
|
+
* @param {string} [evt] Event name. Removes all listeners if omitted.
|
|
33
|
+
* @param {EventEmitterListener} [fn] Listener to remove. Removes all listeners of `evt` if omitted.
|
|
34
|
+
* @returns {this} `this`
|
|
35
|
+
*/
|
|
36
|
+
public off(evt?: string, fn?: EventEmitterListener): EventEmitter;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Emits an event by calling its listeners with the specified arguments.
|
|
40
|
+
* @param {string} evt Event name
|
|
41
|
+
* @param {...*} args Arguments
|
|
42
|
+
* @returns {this} `this`
|
|
43
|
+
*/
|
|
44
|
+
public emit(evt: string, ...args: any[]): EventEmitter;
|
|
45
|
+
}
|