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.
Files changed (105) hide show
  1. package/LICENSE +39 -39
  2. package/README.md +441 -441
  3. package/dist/light/protobuf.js +8582 -8454
  4. package/dist/light/protobuf.js.map +1 -1
  5. package/dist/light/protobuf.min.js +3 -3
  6. package/dist/light/protobuf.min.js.map +1 -1
  7. package/dist/minimal/protobuf.js +3071 -2999
  8. package/dist/minimal/protobuf.js.map +1 -1
  9. package/dist/minimal/protobuf.min.js +3 -3
  10. package/dist/minimal/protobuf.min.js.map +1 -1
  11. package/dist/protobuf.js +10486 -10358
  12. package/dist/protobuf.js.map +1 -1
  13. package/dist/protobuf.min.js +3 -3
  14. package/dist/protobuf.min.js.map +1 -1
  15. package/ext/README.md +70 -70
  16. package/ext/debug/README.md +4 -4
  17. package/ext/debug/index.js +71 -71
  18. package/ext/descriptor/README.md +5 -5
  19. package/ext/descriptor/index.d.ts +2 -2
  20. package/ext/descriptor/index.js +2 -2
  21. package/ext/descriptor.d.ts +87 -87
  22. package/ext/descriptor.js +1354 -1354
  23. package/ext/protojson.LICENSE +201 -201
  24. package/ext/protojson.d.ts +20 -20
  25. package/ext/protojson.js +945 -925
  26. package/ext/textformat.d.ts +19 -19
  27. package/ext/textformat.js +1256 -1256
  28. package/google/LICENSE +27 -27
  29. package/google/README.md +1 -1
  30. package/google/api/annotations.json +82 -82
  31. package/google/api/annotations.proto +10 -10
  32. package/google/api/http.json +85 -85
  33. package/google/api/http.proto +30 -30
  34. package/google/protobuf/api.json +117 -117
  35. package/google/protobuf/api.proto +33 -33
  36. package/google/protobuf/compiler/plugin.json +126 -126
  37. package/google/protobuf/compiler/plugin.proto +47 -47
  38. package/google/protobuf/descriptor.json +1381 -1381
  39. package/google/protobuf/descriptor.proto +534 -534
  40. package/google/protobuf/source_context.json +19 -19
  41. package/google/protobuf/source_context.proto +7 -7
  42. package/google/protobuf/type.json +201 -201
  43. package/google/protobuf/type.proto +89 -89
  44. package/index.d.ts +30 -0
  45. package/index.js +4 -4
  46. package/light.d.ts +2 -2
  47. package/light.js +3 -3
  48. package/minimal.d.ts +2 -2
  49. package/minimal.js +4 -4
  50. package/package.json +93 -93
  51. package/src/common.js +399 -399
  52. package/src/converter.js +344 -344
  53. package/src/decoder.js +214 -208
  54. package/src/encoder.js +111 -111
  55. package/src/enum.js +231 -231
  56. package/src/field.js +497 -497
  57. package/src/index-light.js +104 -104
  58. package/src/index-minimal.js +35 -36
  59. package/src/index.js +12 -12
  60. package/src/mapfield.js +136 -136
  61. package/src/message.js +137 -137
  62. package/src/method.js +175 -175
  63. package/src/namespace.js +566 -565
  64. package/src/object.js +382 -382
  65. package/src/oneof.js +225 -225
  66. package/src/parse.js +1068 -1068
  67. package/src/reader.js +558 -543
  68. package/src/reader_buffer.js +72 -72
  69. package/src/root.js +416 -416
  70. package/src/roots.js +18 -18
  71. package/src/rpc/service.js +148 -148
  72. package/src/rpc.js +36 -36
  73. package/src/service.js +198 -198
  74. package/src/tokenize.js +421 -421
  75. package/src/type.js +654 -655
  76. package/src/types.js +196 -196
  77. package/src/typescript.js +25 -25
  78. package/src/util/aspromise.d.ts +13 -13
  79. package/src/util/aspromise.js +52 -52
  80. package/src/util/base64.d.ts +32 -32
  81. package/src/util/base64.js +146 -146
  82. package/src/util/codegen.d.ts +31 -31
  83. package/src/util/codegen.js +113 -113
  84. package/src/util/eventemitter.d.ts +45 -45
  85. package/src/util/eventemitter.js +86 -86
  86. package/src/util/fetch.d.ts +56 -56
  87. package/src/util/fetch.js +112 -112
  88. package/src/util/float.d.ts +83 -83
  89. package/src/util/float.js +335 -335
  90. package/src/util/fs.js +11 -11
  91. package/src/util/longbits.js +200 -200
  92. package/src/util/minimal.js +515 -515
  93. package/src/util/path.d.ts +22 -22
  94. package/src/util/path.js +100 -72
  95. package/src/util/patterns.js +7 -7
  96. package/src/util/pool.d.ts +32 -32
  97. package/src/util/pool.js +48 -48
  98. package/src/util/utf8.d.ts +24 -24
  99. package/src/util/utf8.js +188 -130
  100. package/src/util.js +264 -242
  101. package/src/verifier.js +180 -180
  102. package/src/wrappers.js +106 -106
  103. package/src/writer.js +495 -495
  104. package/src/writer_buffer.js +102 -102
  105. package/tsconfig.json +6 -6
package/src/types.js CHANGED
@@ -1,196 +1,196 @@
1
- "use strict";
2
-
3
- /**
4
- * Common type constants.
5
- * @namespace
6
- */
7
- var types = exports;
8
-
9
- var util = require("./util");
10
-
11
- var s = [
12
- "double", // 0
13
- "float", // 1
14
- "int32", // 2
15
- "uint32", // 3
16
- "sint32", // 4
17
- "fixed32", // 5
18
- "sfixed32", // 6
19
- "int64", // 7
20
- "uint64", // 8
21
- "sint64", // 9
22
- "fixed64", // 10
23
- "sfixed64", // 11
24
- "bool", // 12
25
- "string", // 13
26
- "bytes" // 14
27
- ];
28
-
29
- function bake(values, offset) {
30
- var i = 0, o = Object.create(null);
31
- offset |= 0;
32
- while (i < values.length) o[s[i + offset]] = values[i++];
33
- return o;
34
- }
35
-
36
- /**
37
- * Basic type wire types.
38
- * @type {Object.<string,number>}
39
- * @const
40
- * @property {number} double=1 Fixed64 wire type
41
- * @property {number} float=5 Fixed32 wire type
42
- * @property {number} int32=0 Varint wire type
43
- * @property {number} uint32=0 Varint wire type
44
- * @property {number} sint32=0 Varint wire type
45
- * @property {number} fixed32=5 Fixed32 wire type
46
- * @property {number} sfixed32=5 Fixed32 wire type
47
- * @property {number} int64=0 Varint wire type
48
- * @property {number} uint64=0 Varint wire type
49
- * @property {number} sint64=0 Varint wire type
50
- * @property {number} fixed64=1 Fixed64 wire type
51
- * @property {number} sfixed64=1 Fixed64 wire type
52
- * @property {number} bool=0 Varint wire type
53
- * @property {number} string=2 Ldelim wire type
54
- * @property {number} bytes=2 Ldelim wire type
55
- */
56
- types.basic = bake([
57
- /* double */ 1,
58
- /* float */ 5,
59
- /* int32 */ 0,
60
- /* uint32 */ 0,
61
- /* sint32 */ 0,
62
- /* fixed32 */ 5,
63
- /* sfixed32 */ 5,
64
- /* int64 */ 0,
65
- /* uint64 */ 0,
66
- /* sint64 */ 0,
67
- /* fixed64 */ 1,
68
- /* sfixed64 */ 1,
69
- /* bool */ 0,
70
- /* string */ 2,
71
- /* bytes */ 2
72
- ]);
73
-
74
- /**
75
- * Basic type defaults.
76
- * @type {Object.<string,*>}
77
- * @const
78
- * @property {number} double=0 Double default
79
- * @property {number} float=0 Float default
80
- * @property {number} int32=0 Int32 default
81
- * @property {number} uint32=0 Uint32 default
82
- * @property {number} sint32=0 Sint32 default
83
- * @property {number} fixed32=0 Fixed32 default
84
- * @property {number} sfixed32=0 Sfixed32 default
85
- * @property {number} int64=0 Int64 default
86
- * @property {number} uint64=0 Uint64 default
87
- * @property {number} sint64=0 Sint32 default
88
- * @property {number} fixed64=0 Fixed64 default
89
- * @property {number} sfixed64=0 Sfixed64 default
90
- * @property {boolean} bool=false Bool default
91
- * @property {string} string="" String default
92
- * @property {Array.<number>} bytes=Array(0) Bytes default
93
- * @property {null} message=null Message default
94
- */
95
- types.defaults = bake([
96
- /* double */ 0,
97
- /* float */ 0,
98
- /* int32 */ 0,
99
- /* uint32 */ 0,
100
- /* sint32 */ 0,
101
- /* fixed32 */ 0,
102
- /* sfixed32 */ 0,
103
- /* int64 */ 0,
104
- /* uint64 */ 0,
105
- /* sint64 */ 0,
106
- /* fixed64 */ 0,
107
- /* sfixed64 */ 0,
108
- /* bool */ false,
109
- /* string */ "",
110
- /* bytes */ util.emptyArray,
111
- /* message */ null
112
- ]);
113
-
114
- /**
115
- * Basic long type wire types.
116
- * @type {Object.<string,number>}
117
- * @const
118
- * @property {number} int64=0 Varint wire type
119
- * @property {number} uint64=0 Varint wire type
120
- * @property {number} sint64=0 Varint wire type
121
- * @property {number} fixed64=1 Fixed64 wire type
122
- * @property {number} sfixed64=1 Fixed64 wire type
123
- */
124
- types.long = bake([
125
- /* int64 */ 0,
126
- /* uint64 */ 0,
127
- /* sint64 */ 0,
128
- /* fixed64 */ 1,
129
- /* sfixed64 */ 1
130
- ], 7);
131
-
132
- /**
133
- * Allowed types for map keys with their associated wire type.
134
- * @type {Object.<string,number>}
135
- * @const
136
- * @property {number} int32=0 Varint wire type
137
- * @property {number} uint32=0 Varint wire type
138
- * @property {number} sint32=0 Varint wire type
139
- * @property {number} fixed32=5 Fixed32 wire type
140
- * @property {number} sfixed32=5 Fixed32 wire type
141
- * @property {number} int64=0 Varint wire type
142
- * @property {number} uint64=0 Varint wire type
143
- * @property {number} sint64=0 Varint wire type
144
- * @property {number} fixed64=1 Fixed64 wire type
145
- * @property {number} sfixed64=1 Fixed64 wire type
146
- * @property {number} bool=0 Varint wire type
147
- * @property {number} string=2 Ldelim wire type
148
- */
149
- types.mapKey = bake([
150
- /* int32 */ 0,
151
- /* uint32 */ 0,
152
- /* sint32 */ 0,
153
- /* fixed32 */ 5,
154
- /* sfixed32 */ 5,
155
- /* int64 */ 0,
156
- /* uint64 */ 0,
157
- /* sint64 */ 0,
158
- /* fixed64 */ 1,
159
- /* sfixed64 */ 1,
160
- /* bool */ 0,
161
- /* string */ 2
162
- ], 2);
163
-
164
- /**
165
- * Allowed types for packed repeated fields with their associated wire type.
166
- * @type {Object.<string,number>}
167
- * @const
168
- * @property {number} double=1 Fixed64 wire type
169
- * @property {number} float=5 Fixed32 wire type
170
- * @property {number} int32=0 Varint wire type
171
- * @property {number} uint32=0 Varint wire type
172
- * @property {number} sint32=0 Varint wire type
173
- * @property {number} fixed32=5 Fixed32 wire type
174
- * @property {number} sfixed32=5 Fixed32 wire type
175
- * @property {number} int64=0 Varint wire type
176
- * @property {number} uint64=0 Varint wire type
177
- * @property {number} sint64=0 Varint wire type
178
- * @property {number} fixed64=1 Fixed64 wire type
179
- * @property {number} sfixed64=1 Fixed64 wire type
180
- * @property {number} bool=0 Varint wire type
181
- */
182
- types.packed = bake([
183
- /* double */ 1,
184
- /* float */ 5,
185
- /* int32 */ 0,
186
- /* uint32 */ 0,
187
- /* sint32 */ 0,
188
- /* fixed32 */ 5,
189
- /* sfixed32 */ 5,
190
- /* int64 */ 0,
191
- /* uint64 */ 0,
192
- /* sint64 */ 0,
193
- /* fixed64 */ 1,
194
- /* sfixed64 */ 1,
195
- /* bool */ 0
196
- ]);
1
+ "use strict";
2
+
3
+ /**
4
+ * Common type constants.
5
+ * @namespace
6
+ */
7
+ var types = exports;
8
+
9
+ var util = require("./util");
10
+
11
+ var s = [
12
+ "double", // 0
13
+ "float", // 1
14
+ "int32", // 2
15
+ "uint32", // 3
16
+ "sint32", // 4
17
+ "fixed32", // 5
18
+ "sfixed32", // 6
19
+ "int64", // 7
20
+ "uint64", // 8
21
+ "sint64", // 9
22
+ "fixed64", // 10
23
+ "sfixed64", // 11
24
+ "bool", // 12
25
+ "string", // 13
26
+ "bytes" // 14
27
+ ];
28
+
29
+ function bake(values, offset) {
30
+ var i = 0, o = Object.create(null);
31
+ offset |= 0;
32
+ while (i < values.length) o[s[i + offset]] = values[i++];
33
+ return o;
34
+ }
35
+
36
+ /**
37
+ * Basic type wire types.
38
+ * @type {Object.<string,number>}
39
+ * @const
40
+ * @property {number} double=1 Fixed64 wire type
41
+ * @property {number} float=5 Fixed32 wire type
42
+ * @property {number} int32=0 Varint wire type
43
+ * @property {number} uint32=0 Varint wire type
44
+ * @property {number} sint32=0 Varint wire type
45
+ * @property {number} fixed32=5 Fixed32 wire type
46
+ * @property {number} sfixed32=5 Fixed32 wire type
47
+ * @property {number} int64=0 Varint wire type
48
+ * @property {number} uint64=0 Varint wire type
49
+ * @property {number} sint64=0 Varint wire type
50
+ * @property {number} fixed64=1 Fixed64 wire type
51
+ * @property {number} sfixed64=1 Fixed64 wire type
52
+ * @property {number} bool=0 Varint wire type
53
+ * @property {number} string=2 Ldelim wire type
54
+ * @property {number} bytes=2 Ldelim wire type
55
+ */
56
+ types.basic = bake([
57
+ /* double */ 1,
58
+ /* float */ 5,
59
+ /* int32 */ 0,
60
+ /* uint32 */ 0,
61
+ /* sint32 */ 0,
62
+ /* fixed32 */ 5,
63
+ /* sfixed32 */ 5,
64
+ /* int64 */ 0,
65
+ /* uint64 */ 0,
66
+ /* sint64 */ 0,
67
+ /* fixed64 */ 1,
68
+ /* sfixed64 */ 1,
69
+ /* bool */ 0,
70
+ /* string */ 2,
71
+ /* bytes */ 2
72
+ ]);
73
+
74
+ /**
75
+ * Basic type defaults.
76
+ * @type {Object.<string,*>}
77
+ * @const
78
+ * @property {number} double=0 Double default
79
+ * @property {number} float=0 Float default
80
+ * @property {number} int32=0 Int32 default
81
+ * @property {number} uint32=0 Uint32 default
82
+ * @property {number} sint32=0 Sint32 default
83
+ * @property {number} fixed32=0 Fixed32 default
84
+ * @property {number} sfixed32=0 Sfixed32 default
85
+ * @property {number} int64=0 Int64 default
86
+ * @property {number} uint64=0 Uint64 default
87
+ * @property {number} sint64=0 Sint32 default
88
+ * @property {number} fixed64=0 Fixed64 default
89
+ * @property {number} sfixed64=0 Sfixed64 default
90
+ * @property {boolean} bool=false Bool default
91
+ * @property {string} string="" String default
92
+ * @property {Array.<number>} bytes=Array(0) Bytes default
93
+ * @property {null} message=null Message default
94
+ */
95
+ types.defaults = bake([
96
+ /* double */ 0,
97
+ /* float */ 0,
98
+ /* int32 */ 0,
99
+ /* uint32 */ 0,
100
+ /* sint32 */ 0,
101
+ /* fixed32 */ 0,
102
+ /* sfixed32 */ 0,
103
+ /* int64 */ 0,
104
+ /* uint64 */ 0,
105
+ /* sint64 */ 0,
106
+ /* fixed64 */ 0,
107
+ /* sfixed64 */ 0,
108
+ /* bool */ false,
109
+ /* string */ "",
110
+ /* bytes */ util.emptyArray,
111
+ /* message */ null
112
+ ]);
113
+
114
+ /**
115
+ * Basic long type wire types.
116
+ * @type {Object.<string,number>}
117
+ * @const
118
+ * @property {number} int64=0 Varint wire type
119
+ * @property {number} uint64=0 Varint wire type
120
+ * @property {number} sint64=0 Varint wire type
121
+ * @property {number} fixed64=1 Fixed64 wire type
122
+ * @property {number} sfixed64=1 Fixed64 wire type
123
+ */
124
+ types.long = bake([
125
+ /* int64 */ 0,
126
+ /* uint64 */ 0,
127
+ /* sint64 */ 0,
128
+ /* fixed64 */ 1,
129
+ /* sfixed64 */ 1
130
+ ], 7);
131
+
132
+ /**
133
+ * Allowed types for map keys with their associated wire type.
134
+ * @type {Object.<string,number>}
135
+ * @const
136
+ * @property {number} int32=0 Varint wire type
137
+ * @property {number} uint32=0 Varint wire type
138
+ * @property {number} sint32=0 Varint wire type
139
+ * @property {number} fixed32=5 Fixed32 wire type
140
+ * @property {number} sfixed32=5 Fixed32 wire type
141
+ * @property {number} int64=0 Varint wire type
142
+ * @property {number} uint64=0 Varint wire type
143
+ * @property {number} sint64=0 Varint wire type
144
+ * @property {number} fixed64=1 Fixed64 wire type
145
+ * @property {number} sfixed64=1 Fixed64 wire type
146
+ * @property {number} bool=0 Varint wire type
147
+ * @property {number} string=2 Ldelim wire type
148
+ */
149
+ types.mapKey = bake([
150
+ /* int32 */ 0,
151
+ /* uint32 */ 0,
152
+ /* sint32 */ 0,
153
+ /* fixed32 */ 5,
154
+ /* sfixed32 */ 5,
155
+ /* int64 */ 0,
156
+ /* uint64 */ 0,
157
+ /* sint64 */ 0,
158
+ /* fixed64 */ 1,
159
+ /* sfixed64 */ 1,
160
+ /* bool */ 0,
161
+ /* string */ 2
162
+ ], 2);
163
+
164
+ /**
165
+ * Allowed types for packed repeated fields with their associated wire type.
166
+ * @type {Object.<string,number>}
167
+ * @const
168
+ * @property {number} double=1 Fixed64 wire type
169
+ * @property {number} float=5 Fixed32 wire type
170
+ * @property {number} int32=0 Varint wire type
171
+ * @property {number} uint32=0 Varint wire type
172
+ * @property {number} sint32=0 Varint wire type
173
+ * @property {number} fixed32=5 Fixed32 wire type
174
+ * @property {number} sfixed32=5 Fixed32 wire type
175
+ * @property {number} int64=0 Varint wire type
176
+ * @property {number} uint64=0 Varint wire type
177
+ * @property {number} sint64=0 Varint wire type
178
+ * @property {number} fixed64=1 Fixed64 wire type
179
+ * @property {number} sfixed64=1 Fixed64 wire type
180
+ * @property {number} bool=0 Varint wire type
181
+ */
182
+ types.packed = bake([
183
+ /* double */ 1,
184
+ /* float */ 5,
185
+ /* int32 */ 0,
186
+ /* uint32 */ 0,
187
+ /* sint32 */ 0,
188
+ /* fixed32 */ 5,
189
+ /* sfixed32 */ 5,
190
+ /* int64 */ 0,
191
+ /* uint64 */ 0,
192
+ /* sint64 */ 0,
193
+ /* fixed64 */ 1,
194
+ /* sfixed64 */ 1,
195
+ /* bool */ 0
196
+ ]);
package/src/typescript.js CHANGED
@@ -1,25 +1,25 @@
1
- "use strict";
2
- /* eslint-disable no-unused-vars */
3
-
4
- // Global TypeScript helper typedefs. The unused vars anchor these typedefs so
5
- // both TypeScript checkJs and pbts can discover them from this script file.
6
-
7
- /**
8
- * Constructor type.
9
- * @template T
10
- * @typedef {Function & { new(...params: any[]): T; prototype: T }} Constructor
11
- */
12
- var Constructor;
13
-
14
- /**
15
- * Properties type.
16
- * @template T
17
- * @typedef {{ [P in keyof T]?: T[P] }} Properties
18
- */
19
- var Properties;
20
-
21
- /**
22
- * Dynamically reflected message type.
23
- * @typedef {Message<{}> & { [k: string]: any }} ReflectedMessage
24
- */
25
- var ReflectedMessage;
1
+ "use strict";
2
+ /* eslint-disable no-unused-vars */
3
+
4
+ // Global TypeScript helper typedefs. The unused vars anchor these typedefs so
5
+ // both TypeScript checkJs and pbts can discover them from this script file.
6
+
7
+ /**
8
+ * Constructor type.
9
+ * @template T
10
+ * @typedef {Function & { new(...params: any[]): T; prototype: T }} Constructor
11
+ */
12
+ var Constructor;
13
+
14
+ /**
15
+ * Properties type.
16
+ * @template T
17
+ * @typedef {{ [P in keyof T]?: T[P] }} Properties
18
+ */
19
+ var Properties;
20
+
21
+ /**
22
+ * Dynamically reflected message type.
23
+ * @typedef {Message<{}> & { [k: string]: any }} ReflectedMessage
24
+ */
25
+ var ReflectedMessage;
@@ -1,13 +1,13 @@
1
- export = asPromise;
2
-
3
- type asPromiseCallback = (error: Error | null, ...params: any[]) => {};
4
-
5
- /**
6
- * Returns a promise from a node-style callback function.
7
- * @memberof util
8
- * @param {asPromiseCallback} fn Function to call
9
- * @param {*} ctx Function context
10
- * @param {...*} params Function arguments
11
- * @returns {Promise<*>} Promisified function
12
- */
13
- declare function asPromise(fn: asPromiseCallback, ctx: any, ...params: any[]): Promise<any>;
1
+ export = asPromise;
2
+
3
+ type asPromiseCallback = (error: Error | null, ...params: any[]) => {};
4
+
5
+ /**
6
+ * Returns a promise from a node-style callback function.
7
+ * @memberof util
8
+ * @param {asPromiseCallback} fn Function to call
9
+ * @param {*} ctx Function context
10
+ * @param {...*} params Function arguments
11
+ * @returns {Promise<*>} Promisified function
12
+ */
13
+ declare function asPromise(fn: asPromiseCallback, ctx: any, ...params: any[]): Promise<any>;
@@ -1,52 +1,52 @@
1
- "use strict";
2
- module.exports = asPromise;
3
-
4
- /**
5
- * Callback as used by {@link util.asPromise}.
6
- * @typedef asPromiseCallback
7
- * @type {function}
8
- * @param {Error|null} error Error, if any
9
- * @param {...*} params Additional arguments
10
- * @returns {undefined}
11
- */
12
-
13
- /**
14
- * Returns a promise from a node-style callback function.
15
- * @memberof util
16
- * @param {asPromiseCallback} fn Function to call
17
- * @param {*} ctx Function context
18
- * @param {...*} params Function arguments
19
- * @returns {Promise<*>} Promisified function
20
- */
21
- function asPromise(fn, ctx/*, varargs */) {
22
- var params = new Array(arguments.length - 1),
23
- offset = 0,
24
- index = 2,
25
- pending = true;
26
- while (index < arguments.length)
27
- params[offset++] = arguments[index++];
28
- return new Promise(function executor(resolve, reject) {
29
- params[offset] = function callback(err/*, varargs */) {
30
- if (pending) {
31
- pending = false;
32
- if (err)
33
- reject(err);
34
- else {
35
- var params = new Array(arguments.length - 1),
36
- offset = 0;
37
- while (offset < params.length)
38
- params[offset++] = arguments[offset];
39
- resolve.apply(null, params);
40
- }
41
- }
42
- };
43
- try {
44
- fn.apply(ctx || null, params);
45
- } catch (err) {
46
- if (pending) {
47
- pending = false;
48
- reject(err);
49
- }
50
- }
51
- });
52
- }
1
+ "use strict";
2
+ module.exports = asPromise;
3
+
4
+ /**
5
+ * Callback as used by {@link util.asPromise}.
6
+ * @typedef asPromiseCallback
7
+ * @type {function}
8
+ * @param {Error|null} error Error, if any
9
+ * @param {...*} params Additional arguments
10
+ * @returns {undefined}
11
+ */
12
+
13
+ /**
14
+ * Returns a promise from a node-style callback function.
15
+ * @memberof util
16
+ * @param {asPromiseCallback} fn Function to call
17
+ * @param {*} ctx Function context
18
+ * @param {...*} params Function arguments
19
+ * @returns {Promise<*>} Promisified function
20
+ */
21
+ function asPromise(fn, ctx/*, varargs */) {
22
+ var params = new Array(arguments.length - 1),
23
+ offset = 0,
24
+ index = 2,
25
+ pending = true;
26
+ while (index < arguments.length)
27
+ params[offset++] = arguments[index++];
28
+ return new Promise(function executor(resolve, reject) {
29
+ params[offset] = function callback(err/*, varargs */) {
30
+ if (pending) {
31
+ pending = false;
32
+ if (err)
33
+ reject(err);
34
+ else {
35
+ var params = new Array(arguments.length - 1),
36
+ offset = 0;
37
+ while (offset < params.length)
38
+ params[offset++] = arguments[offset];
39
+ resolve.apply(null, params);
40
+ }
41
+ }
42
+ };
43
+ try {
44
+ fn.apply(ctx || null, params);
45
+ } catch (err) {
46
+ if (pending) {
47
+ pending = false;
48
+ reject(err);
49
+ }
50
+ }
51
+ });
52
+ }
@@ -1,32 +1,32 @@
1
- /**
2
- * Calculates the byte length of a base64 encoded string.
3
- * @param {string} string Base64 encoded string
4
- * @returns {number} Byte length
5
- */
6
- export function length(string: string): number;
7
-
8
- /**
9
- * Encodes a buffer to a base64 encoded string.
10
- * @param {Uint8Array} buffer Source buffer
11
- * @param {number} start Source start
12
- * @param {number} end Source end
13
- * @returns {string} Base64 encoded string
14
- */
15
- export function encode(buffer: Uint8Array, start: number, end: number): string;
16
-
17
- /**
18
- * Decodes a base64 encoded string to a buffer.
19
- * @param {string} string Source string
20
- * @param {Uint8Array} buffer Destination buffer
21
- * @param {number} offset Destination offset
22
- * @returns {number} Number of bytes written
23
- * @throws {Error} If encoding is invalid
24
- */
25
- export function decode(string: string, buffer: Uint8Array, offset: number): number;
26
-
27
- /**
28
- * Tests if the specified string appears to be base64 encoded.
29
- * @param {string} string String to test
30
- * @returns {boolean} `true` if it appears to be base64 encoded, otherwise false
31
- */
32
- export function test(string: string): boolean;
1
+ /**
2
+ * Calculates the byte length of a base64 encoded string.
3
+ * @param {string} string Base64 encoded string
4
+ * @returns {number} Byte length
5
+ */
6
+ export function length(string: string): number;
7
+
8
+ /**
9
+ * Encodes a buffer to a base64 encoded string.
10
+ * @param {Uint8Array} buffer Source buffer
11
+ * @param {number} start Source start
12
+ * @param {number} end Source end
13
+ * @returns {string} Base64 encoded string
14
+ */
15
+ export function encode(buffer: Uint8Array, start: number, end: number): string;
16
+
17
+ /**
18
+ * Decodes a base64 encoded string to a buffer.
19
+ * @param {string} string Source string
20
+ * @param {Uint8Array} buffer Destination buffer
21
+ * @param {number} offset Destination offset
22
+ * @returns {number} Number of bytes written
23
+ * @throws {Error} If encoding is invalid
24
+ */
25
+ export function decode(string: string, buffer: Uint8Array, offset: number): number;
26
+
27
+ /**
28
+ * Tests if the specified string appears to be base64 encoded.
29
+ * @param {string} string String to test
30
+ * @returns {boolean} `true` if it appears to be base64 encoded, otherwise false
31
+ */
32
+ export function test(string: string): boolean;