protobufjs 6.11.0 → 6.11.3

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 (205) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/bin/pbjs +0 -0
  3. package/bin/pbts +0 -0
  4. package/index.d.ts +3 -0
  5. package/package-lock.json +7870 -0
  6. package/package.json +3 -3
  7. package/scripts/changelog.js +150 -0
  8. package/src/field.js +3 -3
  9. package/src/namespace.js +2 -1
  10. package/src/util.js +3 -0
  11. package/cli/node_modules/.package-lock.json +0 -256
  12. package/cli/node_modules/acorn/CHANGELOG.md +0 -620
  13. package/cli/node_modules/acorn/LICENSE +0 -21
  14. package/cli/node_modules/acorn/README.md +0 -269
  15. package/cli/node_modules/acorn/bin/acorn +0 -4
  16. package/cli/node_modules/acorn/dist/acorn.d.ts +0 -209
  17. package/cli/node_modules/acorn/dist/acorn.js +0 -5186
  18. package/cli/node_modules/acorn/dist/acorn.js.map +0 -1
  19. package/cli/node_modules/acorn/dist/acorn.mjs +0 -5155
  20. package/cli/node_modules/acorn/dist/acorn.mjs.d.ts +0 -2
  21. package/cli/node_modules/acorn/dist/acorn.mjs.map +0 -1
  22. package/cli/node_modules/acorn/dist/bin.js +0 -64
  23. package/cli/node_modules/acorn/package.json +0 -35
  24. package/cli/node_modules/acorn-jsx/LICENSE +0 -19
  25. package/cli/node_modules/acorn-jsx/README.md +0 -40
  26. package/cli/node_modules/acorn-jsx/index.js +0 -488
  27. package/cli/node_modules/acorn-jsx/package.json +0 -27
  28. package/cli/node_modules/acorn-jsx/xhtml.js +0 -255
  29. package/cli/node_modules/balanced-match/.npmignore +0 -5
  30. package/cli/node_modules/balanced-match/LICENSE.md +0 -21
  31. package/cli/node_modules/balanced-match/README.md +0 -91
  32. package/cli/node_modules/balanced-match/index.js +0 -59
  33. package/cli/node_modules/balanced-match/package.json +0 -49
  34. package/cli/node_modules/brace-expansion/LICENSE +0 -21
  35. package/cli/node_modules/brace-expansion/README.md +0 -129
  36. package/cli/node_modules/brace-expansion/index.js +0 -201
  37. package/cli/node_modules/brace-expansion/package.json +0 -47
  38. package/cli/node_modules/concat-map/.travis.yml +0 -4
  39. package/cli/node_modules/concat-map/LICENSE +0 -18
  40. package/cli/node_modules/concat-map/README.markdown +0 -62
  41. package/cli/node_modules/concat-map/example/map.js +0 -6
  42. package/cli/node_modules/concat-map/index.js +0 -13
  43. package/cli/node_modules/concat-map/package.json +0 -43
  44. package/cli/node_modules/concat-map/test/map.js +0 -39
  45. package/cli/node_modules/deep-is/.npmignore +0 -1
  46. package/cli/node_modules/deep-is/.travis.yml +0 -6
  47. package/cli/node_modules/deep-is/LICENSE +0 -22
  48. package/cli/node_modules/deep-is/README.markdown +0 -70
  49. package/cli/node_modules/deep-is/example/cmp.js +0 -11
  50. package/cli/node_modules/deep-is/index.js +0 -102
  51. package/cli/node_modules/deep-is/package.json +0 -61
  52. package/cli/node_modules/deep-is/test/NaN.js +0 -16
  53. package/cli/node_modules/deep-is/test/cmp.js +0 -23
  54. package/cli/node_modules/deep-is/test/neg-vs-pos-0.js +0 -15
  55. package/cli/node_modules/escodegen/LICENSE.BSD +0 -21
  56. package/cli/node_modules/escodegen/README.md +0 -84
  57. package/cli/node_modules/escodegen/bin/escodegen.js +0 -77
  58. package/cli/node_modules/escodegen/bin/esgenerate.js +0 -64
  59. package/cli/node_modules/escodegen/escodegen.js +0 -2647
  60. package/cli/node_modules/escodegen/package.json +0 -62
  61. package/cli/node_modules/eslint-visitor-keys/CHANGELOG.md +0 -25
  62. package/cli/node_modules/eslint-visitor-keys/LICENSE +0 -201
  63. package/cli/node_modules/eslint-visitor-keys/README.md +0 -98
  64. package/cli/node_modules/eslint-visitor-keys/lib/index.js +0 -81
  65. package/cli/node_modules/eslint-visitor-keys/lib/visitor-keys.json +0 -284
  66. package/cli/node_modules/eslint-visitor-keys/package.json +0 -40
  67. package/cli/node_modules/espree/CHANGELOG.md +0 -509
  68. package/cli/node_modules/espree/LICENSE +0 -25
  69. package/cli/node_modules/espree/README.md +0 -233
  70. package/cli/node_modules/espree/espree.js +0 -177
  71. package/cli/node_modules/espree/lib/ast-node-types.js +0 -96
  72. package/cli/node_modules/espree/lib/espree.js +0 -286
  73. package/cli/node_modules/espree/lib/features.js +0 -29
  74. package/cli/node_modules/espree/lib/options.js +0 -106
  75. package/cli/node_modules/espree/lib/token-translator.js +0 -263
  76. package/cli/node_modules/espree/lib/visitor-keys.js +0 -123
  77. package/cli/node_modules/espree/package.json +0 -64
  78. package/cli/node_modules/esprima/ChangeLog +0 -235
  79. package/cli/node_modules/esprima/LICENSE.BSD +0 -21
  80. package/cli/node_modules/esprima/README.md +0 -46
  81. package/cli/node_modules/esprima/bin/esparse.js +0 -139
  82. package/cli/node_modules/esprima/bin/esvalidate.js +0 -236
  83. package/cli/node_modules/esprima/dist/esprima.js +0 -6709
  84. package/cli/node_modules/esprima/package.json +0 -112
  85. package/cli/node_modules/estraverse/.jshintrc +0 -16
  86. package/cli/node_modules/estraverse/LICENSE.BSD +0 -19
  87. package/cli/node_modules/estraverse/README.md +0 -153
  88. package/cli/node_modules/estraverse/estraverse.js +0 -801
  89. package/cli/node_modules/estraverse/gulpfile.js +0 -70
  90. package/cli/node_modules/estraverse/package.json +0 -40
  91. package/cli/node_modules/esutils/LICENSE.BSD +0 -19
  92. package/cli/node_modules/esutils/README.md +0 -174
  93. package/cli/node_modules/esutils/lib/ast.js +0 -144
  94. package/cli/node_modules/esutils/lib/code.js +0 -135
  95. package/cli/node_modules/esutils/lib/keyword.js +0 -165
  96. package/cli/node_modules/esutils/lib/utils.js +0 -33
  97. package/cli/node_modules/esutils/package.json +0 -44
  98. package/cli/node_modules/fast-levenshtein/LICENSE.md +0 -25
  99. package/cli/node_modules/fast-levenshtein/README.md +0 -104
  100. package/cli/node_modules/fast-levenshtein/levenshtein.js +0 -136
  101. package/cli/node_modules/fast-levenshtein/package.json +0 -39
  102. package/cli/node_modules/fs.realpath/LICENSE +0 -43
  103. package/cli/node_modules/fs.realpath/README.md +0 -33
  104. package/cli/node_modules/fs.realpath/index.js +0 -66
  105. package/cli/node_modules/fs.realpath/old.js +0 -303
  106. package/cli/node_modules/fs.realpath/package.json +0 -26
  107. package/cli/node_modules/glob/LICENSE +0 -21
  108. package/cli/node_modules/glob/README.md +0 -375
  109. package/cli/node_modules/glob/changelog.md +0 -67
  110. package/cli/node_modules/glob/common.js +0 -240
  111. package/cli/node_modules/glob/glob.js +0 -790
  112. package/cli/node_modules/glob/package.json +0 -46
  113. package/cli/node_modules/glob/sync.js +0 -486
  114. package/cli/node_modules/inflight/LICENSE +0 -15
  115. package/cli/node_modules/inflight/README.md +0 -37
  116. package/cli/node_modules/inflight/inflight.js +0 -54
  117. package/cli/node_modules/inflight/package.json +0 -29
  118. package/cli/node_modules/inherits/LICENSE +0 -16
  119. package/cli/node_modules/inherits/README.md +0 -42
  120. package/cli/node_modules/inherits/inherits.js +0 -9
  121. package/cli/node_modules/inherits/inherits_browser.js +0 -27
  122. package/cli/node_modules/inherits/package.json +0 -29
  123. package/cli/node_modules/levn/LICENSE +0 -22
  124. package/cli/node_modules/levn/README.md +0 -196
  125. package/cli/node_modules/levn/lib/cast.js +0 -298
  126. package/cli/node_modules/levn/lib/coerce.js +0 -285
  127. package/cli/node_modules/levn/lib/index.js +0 -22
  128. package/cli/node_modules/levn/lib/parse-string.js +0 -113
  129. package/cli/node_modules/levn/lib/parse.js +0 -102
  130. package/cli/node_modules/levn/package.json +0 -47
  131. package/cli/node_modules/minimatch/LICENSE +0 -15
  132. package/cli/node_modules/minimatch/README.md +0 -209
  133. package/cli/node_modules/minimatch/minimatch.js +0 -923
  134. package/cli/node_modules/minimatch/package.json +0 -30
  135. package/cli/node_modules/once/LICENSE +0 -15
  136. package/cli/node_modules/once/README.md +0 -79
  137. package/cli/node_modules/once/once.js +0 -42
  138. package/cli/node_modules/once/package.json +0 -33
  139. package/cli/node_modules/optionator/CHANGELOG.md +0 -56
  140. package/cli/node_modules/optionator/LICENSE +0 -22
  141. package/cli/node_modules/optionator/README.md +0 -238
  142. package/cli/node_modules/optionator/lib/help.js +0 -260
  143. package/cli/node_modules/optionator/lib/index.js +0 -465
  144. package/cli/node_modules/optionator/lib/util.js +0 -54
  145. package/cli/node_modules/optionator/package.json +0 -44
  146. package/cli/node_modules/path-is-absolute/index.js +0 -20
  147. package/cli/node_modules/path-is-absolute/license +0 -21
  148. package/cli/node_modules/path-is-absolute/package.json +0 -43
  149. package/cli/node_modules/path-is-absolute/readme.md +0 -59
  150. package/cli/node_modules/prelude-ls/CHANGELOG.md +0 -99
  151. package/cli/node_modules/prelude-ls/LICENSE +0 -22
  152. package/cli/node_modules/prelude-ls/README.md +0 -15
  153. package/cli/node_modules/prelude-ls/lib/Func.js +0 -65
  154. package/cli/node_modules/prelude-ls/lib/List.js +0 -686
  155. package/cli/node_modules/prelude-ls/lib/Num.js +0 -130
  156. package/cli/node_modules/prelude-ls/lib/Obj.js +0 -154
  157. package/cli/node_modules/prelude-ls/lib/Str.js +0 -92
  158. package/cli/node_modules/prelude-ls/lib/index.js +0 -178
  159. package/cli/node_modules/prelude-ls/package.json +0 -52
  160. package/cli/node_modules/rimraf/CHANGELOG.md +0 -65
  161. package/cli/node_modules/rimraf/LICENSE +0 -15
  162. package/cli/node_modules/rimraf/README.md +0 -101
  163. package/cli/node_modules/rimraf/bin.js +0 -68
  164. package/cli/node_modules/rimraf/package.json +0 -32
  165. package/cli/node_modules/rimraf/rimraf.js +0 -360
  166. package/cli/node_modules/source-map/CHANGELOG.md +0 -301
  167. package/cli/node_modules/source-map/LICENSE +0 -28
  168. package/cli/node_modules/source-map/README.md +0 -742
  169. package/cli/node_modules/source-map/dist/source-map.debug.js +0 -3234
  170. package/cli/node_modules/source-map/dist/source-map.js +0 -3233
  171. package/cli/node_modules/source-map/dist/source-map.min.js +0 -2
  172. package/cli/node_modules/source-map/dist/source-map.min.js.map +0 -1
  173. package/cli/node_modules/source-map/lib/array-set.js +0 -121
  174. package/cli/node_modules/source-map/lib/base64-vlq.js +0 -140
  175. package/cli/node_modules/source-map/lib/base64.js +0 -67
  176. package/cli/node_modules/source-map/lib/binary-search.js +0 -111
  177. package/cli/node_modules/source-map/lib/mapping-list.js +0 -79
  178. package/cli/node_modules/source-map/lib/quick-sort.js +0 -114
  179. package/cli/node_modules/source-map/lib/source-map-consumer.js +0 -1145
  180. package/cli/node_modules/source-map/lib/source-map-generator.js +0 -425
  181. package/cli/node_modules/source-map/lib/source-node.js +0 -413
  182. package/cli/node_modules/source-map/lib/util.js +0 -488
  183. package/cli/node_modules/source-map/package.json +0 -73
  184. package/cli/node_modules/source-map/source-map.d.ts +0 -98
  185. package/cli/node_modules/source-map/source-map.js +0 -8
  186. package/cli/node_modules/tmp/CHANGELOG.md +0 -288
  187. package/cli/node_modules/tmp/LICENSE +0 -21
  188. package/cli/node_modules/tmp/README.md +0 -365
  189. package/cli/node_modules/tmp/lib/tmp.js +0 -780
  190. package/cli/node_modules/tmp/package.json +0 -58
  191. package/cli/node_modules/type-check/LICENSE +0 -22
  192. package/cli/node_modules/type-check/README.md +0 -210
  193. package/cli/node_modules/type-check/lib/check.js +0 -126
  194. package/cli/node_modules/type-check/lib/index.js +0 -16
  195. package/cli/node_modules/type-check/lib/parse-type.js +0 -196
  196. package/cli/node_modules/type-check/package.json +0 -40
  197. package/cli/node_modules/word-wrap/LICENSE +0 -21
  198. package/cli/node_modules/word-wrap/README.md +0 -182
  199. package/cli/node_modules/word-wrap/index.d.ts +0 -50
  200. package/cli/node_modules/word-wrap/index.js +0 -46
  201. package/cli/node_modules/word-wrap/package.json +0 -77
  202. package/cli/node_modules/wrappy/LICENSE +0 -15
  203. package/cli/node_modules/wrappy/README.md +0 -36
  204. package/cli/node_modules/wrappy/package.json +0 -29
  205. package/cli/node_modules/wrappy/wrappy.js +0 -33
@@ -1,780 +0,0 @@
1
- /*!
2
- * Tmp
3
- *
4
- * Copyright (c) 2011-2017 KARASZI Istvan <github@spam.raszi.hu>
5
- *
6
- * MIT Licensed
7
- */
8
-
9
- /*
10
- * Module dependencies.
11
- */
12
- const fs = require('fs');
13
- const os = require('os');
14
- const path = require('path');
15
- const crypto = require('crypto');
16
- const _c = { fs: fs.constants, os: os.constants };
17
- const rimraf = require('rimraf');
18
-
19
- /*
20
- * The working inner variables.
21
- */
22
- const
23
- // the random characters to choose from
24
- RANDOM_CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
25
-
26
- TEMPLATE_PATTERN = /XXXXXX/,
27
-
28
- DEFAULT_TRIES = 3,
29
-
30
- CREATE_FLAGS = (_c.O_CREAT || _c.fs.O_CREAT) | (_c.O_EXCL || _c.fs.O_EXCL) | (_c.O_RDWR || _c.fs.O_RDWR),
31
-
32
- // constants are off on the windows platform and will not match the actual errno codes
33
- IS_WIN32 = os.platform() === 'win32',
34
- EBADF = _c.EBADF || _c.os.errno.EBADF,
35
- ENOENT = _c.ENOENT || _c.os.errno.ENOENT,
36
-
37
- DIR_MODE = 0o700 /* 448 */,
38
- FILE_MODE = 0o600 /* 384 */,
39
-
40
- EXIT = 'exit',
41
-
42
- // this will hold the objects need to be removed on exit
43
- _removeObjects = [],
44
-
45
- // API change in fs.rmdirSync leads to error when passing in a second parameter, e.g. the callback
46
- FN_RMDIR_SYNC = fs.rmdirSync.bind(fs),
47
- FN_RIMRAF_SYNC = rimraf.sync;
48
-
49
- let
50
- _gracefulCleanup = false;
51
-
52
- /**
53
- * Gets a temporary file name.
54
- *
55
- * @param {(Options|tmpNameCallback)} options options or callback
56
- * @param {?tmpNameCallback} callback the callback function
57
- */
58
- function tmpName(options, callback) {
59
- const
60
- args = _parseArguments(options, callback),
61
- opts = args[0],
62
- cb = args[1];
63
-
64
- try {
65
- _assertAndSanitizeOptions(opts);
66
- } catch (err) {
67
- return cb(err);
68
- }
69
-
70
- let tries = opts.tries;
71
- (function _getUniqueName() {
72
- try {
73
- const name = _generateTmpName(opts);
74
-
75
- // check whether the path exists then retry if needed
76
- fs.stat(name, function (err) {
77
- /* istanbul ignore else */
78
- if (!err) {
79
- /* istanbul ignore else */
80
- if (tries-- > 0) return _getUniqueName();
81
-
82
- return cb(new Error('Could not get a unique tmp filename, max tries reached ' + name));
83
- }
84
-
85
- cb(null, name);
86
- });
87
- } catch (err) {
88
- cb(err);
89
- }
90
- }());
91
- }
92
-
93
- /**
94
- * Synchronous version of tmpName.
95
- *
96
- * @param {Object} options
97
- * @returns {string} the generated random name
98
- * @throws {Error} if the options are invalid or could not generate a filename
99
- */
100
- function tmpNameSync(options) {
101
- const
102
- args = _parseArguments(options),
103
- opts = args[0];
104
-
105
- _assertAndSanitizeOptions(opts);
106
-
107
- let tries = opts.tries;
108
- do {
109
- const name = _generateTmpName(opts);
110
- try {
111
- fs.statSync(name);
112
- } catch (e) {
113
- return name;
114
- }
115
- } while (tries-- > 0);
116
-
117
- throw new Error('Could not get a unique tmp filename, max tries reached');
118
- }
119
-
120
- /**
121
- * Creates and opens a temporary file.
122
- *
123
- * @param {(Options|null|undefined|fileCallback)} options the config options or the callback function or null or undefined
124
- * @param {?fileCallback} callback
125
- */
126
- function file(options, callback) {
127
- const
128
- args = _parseArguments(options, callback),
129
- opts = args[0],
130
- cb = args[1];
131
-
132
- // gets a temporary filename
133
- tmpName(opts, function _tmpNameCreated(err, name) {
134
- /* istanbul ignore else */
135
- if (err) return cb(err);
136
-
137
- // create and open the file
138
- fs.open(name, CREATE_FLAGS, opts.mode || FILE_MODE, function _fileCreated(err, fd) {
139
- /* istanbu ignore else */
140
- if (err) return cb(err);
141
-
142
- if (opts.discardDescriptor) {
143
- return fs.close(fd, function _discardCallback(possibleErr) {
144
- // the chance of getting an error on close here is rather low and might occur in the most edgiest cases only
145
- return cb(possibleErr, name, undefined, _prepareTmpFileRemoveCallback(name, -1, opts, false));
146
- });
147
- } else {
148
- // detachDescriptor passes the descriptor whereas discardDescriptor closes it, either way, we no longer care
149
- // about the descriptor
150
- const discardOrDetachDescriptor = opts.discardDescriptor || opts.detachDescriptor;
151
- cb(null, name, fd, _prepareTmpFileRemoveCallback(name, discardOrDetachDescriptor ? -1 : fd, opts, false));
152
- }
153
- });
154
- });
155
- }
156
-
157
- /**
158
- * Synchronous version of file.
159
- *
160
- * @param {Options} options
161
- * @returns {FileSyncObject} object consists of name, fd and removeCallback
162
- * @throws {Error} if cannot create a file
163
- */
164
- function fileSync(options) {
165
- const
166
- args = _parseArguments(options),
167
- opts = args[0];
168
-
169
- const discardOrDetachDescriptor = opts.discardDescriptor || opts.detachDescriptor;
170
- const name = tmpNameSync(opts);
171
- var fd = fs.openSync(name, CREATE_FLAGS, opts.mode || FILE_MODE);
172
- /* istanbul ignore else */
173
- if (opts.discardDescriptor) {
174
- fs.closeSync(fd);
175
- fd = undefined;
176
- }
177
-
178
- return {
179
- name: name,
180
- fd: fd,
181
- removeCallback: _prepareTmpFileRemoveCallback(name, discardOrDetachDescriptor ? -1 : fd, opts, true)
182
- };
183
- }
184
-
185
- /**
186
- * Creates a temporary directory.
187
- *
188
- * @param {(Options|dirCallback)} options the options or the callback function
189
- * @param {?dirCallback} callback
190
- */
191
- function dir(options, callback) {
192
- const
193
- args = _parseArguments(options, callback),
194
- opts = args[0],
195
- cb = args[1];
196
-
197
- // gets a temporary filename
198
- tmpName(opts, function _tmpNameCreated(err, name) {
199
- /* istanbul ignore else */
200
- if (err) return cb(err);
201
-
202
- // create the directory
203
- fs.mkdir(name, opts.mode || DIR_MODE, function _dirCreated(err) {
204
- /* istanbul ignore else */
205
- if (err) return cb(err);
206
-
207
- cb(null, name, _prepareTmpDirRemoveCallback(name, opts, false));
208
- });
209
- });
210
- }
211
-
212
- /**
213
- * Synchronous version of dir.
214
- *
215
- * @param {Options} options
216
- * @returns {DirSyncObject} object consists of name and removeCallback
217
- * @throws {Error} if it cannot create a directory
218
- */
219
- function dirSync(options) {
220
- const
221
- args = _parseArguments(options),
222
- opts = args[0];
223
-
224
- const name = tmpNameSync(opts);
225
- fs.mkdirSync(name, opts.mode || DIR_MODE);
226
-
227
- return {
228
- name: name,
229
- removeCallback: _prepareTmpDirRemoveCallback(name, opts, true)
230
- };
231
- }
232
-
233
- /**
234
- * Removes files asynchronously.
235
- *
236
- * @param {Object} fdPath
237
- * @param {Function} next
238
- * @private
239
- */
240
- function _removeFileAsync(fdPath, next) {
241
- const _handler = function (err) {
242
- if (err && !_isENOENT(err)) {
243
- // reraise any unanticipated error
244
- return next(err);
245
- }
246
- next();
247
- };
248
-
249
- if (0 <= fdPath[0])
250
- fs.close(fdPath[0], function () {
251
- fs.unlink(fdPath[1], _handler);
252
- });
253
- else fs.unlink(fdPath[1], _handler);
254
- }
255
-
256
- /**
257
- * Removes files synchronously.
258
- *
259
- * @param {Object} fdPath
260
- * @private
261
- */
262
- function _removeFileSync(fdPath) {
263
- let rethrownException = null;
264
- try {
265
- if (0 <= fdPath[0]) fs.closeSync(fdPath[0]);
266
- } catch (e) {
267
- // reraise any unanticipated error
268
- if (!_isEBADF(e) && !_isENOENT(e)) throw e;
269
- } finally {
270
- try {
271
- fs.unlinkSync(fdPath[1]);
272
- }
273
- catch (e) {
274
- // reraise any unanticipated error
275
- if (!_isENOENT(e)) rethrownException = e;
276
- }
277
- }
278
- if (rethrownException !== null) {
279
- throw rethrownException;
280
- }
281
- }
282
-
283
- /**
284
- * Prepares the callback for removal of the temporary file.
285
- *
286
- * Returns either a sync callback or a async callback depending on whether
287
- * fileSync or file was called, which is expressed by the sync parameter.
288
- *
289
- * @param {string} name the path of the file
290
- * @param {number} fd file descriptor
291
- * @param {Object} opts
292
- * @param {boolean} sync
293
- * @returns {fileCallback | fileCallbackSync}
294
- * @private
295
- */
296
- function _prepareTmpFileRemoveCallback(name, fd, opts, sync) {
297
- const removeCallbackSync = _prepareRemoveCallback(_removeFileSync, [fd, name], sync);
298
- const removeCallback = _prepareRemoveCallback(_removeFileAsync, [fd, name], sync, removeCallbackSync);
299
-
300
- if (!opts.keep) _removeObjects.unshift(removeCallbackSync);
301
-
302
- return sync ? removeCallbackSync : removeCallback;
303
- }
304
-
305
- /**
306
- * Prepares the callback for removal of the temporary directory.
307
- *
308
- * Returns either a sync callback or a async callback depending on whether
309
- * tmpFileSync or tmpFile was called, which is expressed by the sync parameter.
310
- *
311
- * @param {string} name
312
- * @param {Object} opts
313
- * @param {boolean} sync
314
- * @returns {Function} the callback
315
- * @private
316
- */
317
- function _prepareTmpDirRemoveCallback(name, opts, sync) {
318
- const removeFunction = opts.unsafeCleanup ? rimraf : fs.rmdir.bind(fs);
319
- const removeFunctionSync = opts.unsafeCleanup ? FN_RIMRAF_SYNC : FN_RMDIR_SYNC;
320
- const removeCallbackSync = _prepareRemoveCallback(removeFunctionSync, name, sync);
321
- const removeCallback = _prepareRemoveCallback(removeFunction, name, sync, removeCallbackSync);
322
- if (!opts.keep) _removeObjects.unshift(removeCallbackSync);
323
-
324
- return sync ? removeCallbackSync : removeCallback;
325
- }
326
-
327
- /**
328
- * Creates a guarded function wrapping the removeFunction call.
329
- *
330
- * The cleanup callback is save to be called multiple times.
331
- * Subsequent invocations will be ignored.
332
- *
333
- * @param {Function} removeFunction
334
- * @param {string} fileOrDirName
335
- * @param {boolean} sync
336
- * @param {cleanupCallbackSync?} cleanupCallbackSync
337
- * @returns {cleanupCallback | cleanupCallbackSync}
338
- * @private
339
- */
340
- function _prepareRemoveCallback(removeFunction, fileOrDirName, sync, cleanupCallbackSync) {
341
- let called = false;
342
-
343
- // if sync is true, the next parameter will be ignored
344
- return function _cleanupCallback(next) {
345
-
346
- /* istanbul ignore else */
347
- if (!called) {
348
- // remove cleanupCallback from cache
349
- const toRemove = cleanupCallbackSync || _cleanupCallback;
350
- const index = _removeObjects.indexOf(toRemove);
351
- /* istanbul ignore else */
352
- if (index >= 0) _removeObjects.splice(index, 1);
353
-
354
- called = true;
355
- if (sync || removeFunction === FN_RMDIR_SYNC || removeFunction === FN_RIMRAF_SYNC) {
356
- return removeFunction(fileOrDirName);
357
- } else {
358
- return removeFunction(fileOrDirName, next || function() {});
359
- }
360
- }
361
- };
362
- }
363
-
364
- /**
365
- * The garbage collector.
366
- *
367
- * @private
368
- */
369
- function _garbageCollector() {
370
- /* istanbul ignore else */
371
- if (!_gracefulCleanup) return;
372
-
373
- // the function being called removes itself from _removeObjects,
374
- // loop until _removeObjects is empty
375
- while (_removeObjects.length) {
376
- try {
377
- _removeObjects[0]();
378
- } catch (e) {
379
- // already removed?
380
- }
381
- }
382
- }
383
-
384
- /**
385
- * Random name generator based on crypto.
386
- * Adapted from http://blog.tompawlak.org/how-to-generate-random-values-nodejs-javascript
387
- *
388
- * @param {number} howMany
389
- * @returns {string} the generated random name
390
- * @private
391
- */
392
- function _randomChars(howMany) {
393
- let
394
- value = [],
395
- rnd = null;
396
-
397
- // make sure that we do not fail because we ran out of entropy
398
- try {
399
- rnd = crypto.randomBytes(howMany);
400
- } catch (e) {
401
- rnd = crypto.pseudoRandomBytes(howMany);
402
- }
403
-
404
- for (var i = 0; i < howMany; i++) {
405
- value.push(RANDOM_CHARS[rnd[i] % RANDOM_CHARS.length]);
406
- }
407
-
408
- return value.join('');
409
- }
410
-
411
- /**
412
- * Helper which determines whether a string s is blank, that is undefined, or empty or null.
413
- *
414
- * @private
415
- * @param {string} s
416
- * @returns {Boolean} true whether the string s is blank, false otherwise
417
- */
418
- function _isBlank(s) {
419
- return s === null || _isUndefined(s) || !s.trim();
420
- }
421
-
422
- /**
423
- * Checks whether the `obj` parameter is defined or not.
424
- *
425
- * @param {Object} obj
426
- * @returns {boolean} true if the object is undefined
427
- * @private
428
- */
429
- function _isUndefined(obj) {
430
- return typeof obj === 'undefined';
431
- }
432
-
433
- /**
434
- * Parses the function arguments.
435
- *
436
- * This function helps to have optional arguments.
437
- *
438
- * @param {(Options|null|undefined|Function)} options
439
- * @param {?Function} callback
440
- * @returns {Array} parsed arguments
441
- * @private
442
- */
443
- function _parseArguments(options, callback) {
444
- /* istanbul ignore else */
445
- if (typeof options === 'function') {
446
- return [{}, options];
447
- }
448
-
449
- /* istanbul ignore else */
450
- if (_isUndefined(options)) {
451
- return [{}, callback];
452
- }
453
-
454
- // copy options so we do not leak the changes we make internally
455
- const actualOptions = {};
456
- for (const key of Object.getOwnPropertyNames(options)) {
457
- actualOptions[key] = options[key];
458
- }
459
-
460
- return [actualOptions, callback];
461
- }
462
-
463
- /**
464
- * Generates a new temporary name.
465
- *
466
- * @param {Object} opts
467
- * @returns {string} the new random name according to opts
468
- * @private
469
- */
470
- function _generateTmpName(opts) {
471
-
472
- const tmpDir = opts.tmpdir;
473
-
474
- /* istanbul ignore else */
475
- if (!_isUndefined(opts.name))
476
- return path.join(tmpDir, opts.dir, opts.name);
477
-
478
- /* istanbul ignore else */
479
- if (!_isUndefined(opts.template))
480
- return path.join(tmpDir, opts.dir, opts.template).replace(TEMPLATE_PATTERN, _randomChars(6));
481
-
482
- // prefix and postfix
483
- const name = [
484
- opts.prefix ? opts.prefix : 'tmp',
485
- '-',
486
- process.pid,
487
- '-',
488
- _randomChars(12),
489
- opts.postfix ? '-' + opts.postfix : ''
490
- ].join('');
491
-
492
- return path.join(tmpDir, opts.dir, name);
493
- }
494
-
495
- /**
496
- * Asserts whether the specified options are valid, also sanitizes options and provides sane defaults for missing
497
- * options.
498
- *
499
- * @param {Options} options
500
- * @private
501
- */
502
- function _assertAndSanitizeOptions(options) {
503
-
504
- options.tmpdir = _getTmpDir(options);
505
-
506
- const tmpDir = options.tmpdir;
507
-
508
- /* istanbul ignore else */
509
- if (!_isUndefined(options.name))
510
- _assertIsRelative(options.name, 'name', tmpDir);
511
- /* istanbul ignore else */
512
- if (!_isUndefined(options.dir))
513
- _assertIsRelative(options.dir, 'dir', tmpDir);
514
- /* istanbul ignore else */
515
- if (!_isUndefined(options.template)) {
516
- _assertIsRelative(options.template, 'template', tmpDir);
517
- if (!options.template.match(TEMPLATE_PATTERN))
518
- throw new Error(`Invalid template, found "${options.template}".`);
519
- }
520
- /* istanbul ignore else */
521
- if (!_isUndefined(options.tries) && isNaN(options.tries) || options.tries < 0)
522
- throw new Error(`Invalid tries, found "${options.tries}".`);
523
-
524
- // if a name was specified we will try once
525
- options.tries = _isUndefined(options.name) ? options.tries || DEFAULT_TRIES : 1;
526
- options.keep = !!options.keep;
527
- options.detachDescriptor = !!options.detachDescriptor;
528
- options.discardDescriptor = !!options.discardDescriptor;
529
- options.unsafeCleanup = !!options.unsafeCleanup;
530
-
531
- // sanitize dir, also keep (multiple) blanks if the user, purportedly sane, requests us to
532
- options.dir = _isUndefined(options.dir) ? '' : path.relative(tmpDir, _resolvePath(options.dir, tmpDir));
533
- options.template = _isUndefined(options.template) ? undefined : path.relative(tmpDir, _resolvePath(options.template, tmpDir));
534
- // sanitize further if template is relative to options.dir
535
- options.template = _isBlank(options.template) ? undefined : path.relative(options.dir, options.template);
536
-
537
- // for completeness' sake only, also keep (multiple) blanks if the user, purportedly sane, requests us to
538
- options.name = _isUndefined(options.name) ? undefined : _sanitizeName(options.name);
539
- options.prefix = _isUndefined(options.prefix) ? '' : options.prefix;
540
- options.postfix = _isUndefined(options.postfix) ? '' : options.postfix;
541
- }
542
-
543
- /**
544
- * Resolve the specified path name in respect to tmpDir.
545
- *
546
- * The specified name might include relative path components, e.g. ../
547
- * so we need to resolve in order to be sure that is is located inside tmpDir
548
- *
549
- * @param name
550
- * @param tmpDir
551
- * @returns {string}
552
- * @private
553
- */
554
- function _resolvePath(name, tmpDir) {
555
- const sanitizedName = _sanitizeName(name);
556
- if (sanitizedName.startsWith(tmpDir)) {
557
- return path.resolve(sanitizedName);
558
- } else {
559
- return path.resolve(path.join(tmpDir, sanitizedName));
560
- }
561
- }
562
-
563
- /**
564
- * Sanitize the specified path name by removing all quote characters.
565
- *
566
- * @param name
567
- * @returns {string}
568
- * @private
569
- */
570
- function _sanitizeName(name) {
571
- if (_isBlank(name)) {
572
- return name;
573
- }
574
- return name.replace(/["']/g, '');
575
- }
576
-
577
- /**
578
- * Asserts whether specified name is relative to the specified tmpDir.
579
- *
580
- * @param {string} name
581
- * @param {string} option
582
- * @param {string} tmpDir
583
- * @throws {Error}
584
- * @private
585
- */
586
- function _assertIsRelative(name, option, tmpDir) {
587
- if (option === 'name') {
588
- // assert that name is not absolute and does not contain a path
589
- if (path.isAbsolute(name))
590
- throw new Error(`${option} option must not contain an absolute path, found "${name}".`);
591
- // must not fail on valid .<name> or ..<name> or similar such constructs
592
- let basename = path.basename(name);
593
- if (basename === '..' || basename === '.' || basename !== name)
594
- throw new Error(`${option} option must not contain a path, found "${name}".`);
595
- }
596
- else { // if (option === 'dir' || option === 'template') {
597
- // assert that dir or template are relative to tmpDir
598
- if (path.isAbsolute(name) && !name.startsWith(tmpDir)) {
599
- throw new Error(`${option} option must be relative to "${tmpDir}", found "${name}".`);
600
- }
601
- let resolvedPath = _resolvePath(name, tmpDir);
602
- if (!resolvedPath.startsWith(tmpDir))
603
- throw new Error(`${option} option must be relative to "${tmpDir}", found "${resolvedPath}".`);
604
- }
605
- }
606
-
607
- /**
608
- * Helper for testing against EBADF to compensate changes made to Node 7.x under Windows.
609
- *
610
- * @private
611
- */
612
- function _isEBADF(error) {
613
- return _isExpectedError(error, -EBADF, 'EBADF');
614
- }
615
-
616
- /**
617
- * Helper for testing against ENOENT to compensate changes made to Node 7.x under Windows.
618
- *
619
- * @private
620
- */
621
- function _isENOENT(error) {
622
- return _isExpectedError(error, -ENOENT, 'ENOENT');
623
- }
624
-
625
- /**
626
- * Helper to determine whether the expected error code matches the actual code and errno,
627
- * which will differ between the supported node versions.
628
- *
629
- * - Node >= 7.0:
630
- * error.code {string}
631
- * error.errno {number} any numerical value will be negated
632
- *
633
- * CAVEAT
634
- *
635
- * On windows, the errno for EBADF is -4083 but os.constants.errno.EBADF is different and we must assume that ENOENT
636
- * is no different here.
637
- *
638
- * @param {SystemError} error
639
- * @param {number} errno
640
- * @param {string} code
641
- * @private
642
- */
643
- function _isExpectedError(error, errno, code) {
644
- return IS_WIN32 ? error.code === code : error.code === code && error.errno === errno;
645
- }
646
-
647
- /**
648
- * Sets the graceful cleanup.
649
- *
650
- * If graceful cleanup is set, tmp will remove all controlled temporary objects on process exit, otherwise the
651
- * temporary objects will remain in place, waiting to be cleaned up on system restart or otherwise scheduled temporary
652
- * object removals.
653
- */
654
- function setGracefulCleanup() {
655
- _gracefulCleanup = true;
656
- }
657
-
658
- /**
659
- * Returns the currently configured tmp dir from os.tmpdir().
660
- *
661
- * @private
662
- * @param {?Options} options
663
- * @returns {string} the currently configured tmp dir
664
- */
665
- function _getTmpDir(options) {
666
- return path.resolve(_sanitizeName(options && options.tmpdir || os.tmpdir()));
667
- }
668
-
669
- // Install process exit listener
670
- process.addListener(EXIT, _garbageCollector);
671
-
672
- /**
673
- * Configuration options.
674
- *
675
- * @typedef {Object} Options
676
- * @property {?boolean} keep the temporary object (file or dir) will not be garbage collected
677
- * @property {?number} tries the number of tries before give up the name generation
678
- * @property (?int) mode the access mode, defaults are 0o700 for directories and 0o600 for files
679
- * @property {?string} template the "mkstemp" like filename template
680
- * @property {?string} name fixed name relative to tmpdir or the specified dir option
681
- * @property {?string} dir tmp directory relative to the root tmp directory in use
682
- * @property {?string} prefix prefix for the generated name
683
- * @property {?string} postfix postfix for the generated name
684
- * @property {?string} tmpdir the root tmp directory which overrides the os tmpdir
685
- * @property {?boolean} unsafeCleanup recursively removes the created temporary directory, even when it's not empty
686
- * @property {?boolean} detachDescriptor detaches the file descriptor, caller is responsible for closing the file, tmp will no longer try closing the file during garbage collection
687
- * @property {?boolean} discardDescriptor discards the file descriptor (closes file, fd is -1), tmp will no longer try closing the file during garbage collection
688
- */
689
-
690
- /**
691
- * @typedef {Object} FileSyncObject
692
- * @property {string} name the name of the file
693
- * @property {string} fd the file descriptor or -1 if the fd has been discarded
694
- * @property {fileCallback} removeCallback the callback function to remove the file
695
- */
696
-
697
- /**
698
- * @typedef {Object} DirSyncObject
699
- * @property {string} name the name of the directory
700
- * @property {fileCallback} removeCallback the callback function to remove the directory
701
- */
702
-
703
- /**
704
- * @callback tmpNameCallback
705
- * @param {?Error} err the error object if anything goes wrong
706
- * @param {string} name the temporary file name
707
- */
708
-
709
- /**
710
- * @callback fileCallback
711
- * @param {?Error} err the error object if anything goes wrong
712
- * @param {string} name the temporary file name
713
- * @param {number} fd the file descriptor or -1 if the fd had been discarded
714
- * @param {cleanupCallback} fn the cleanup callback function
715
- */
716
-
717
- /**
718
- * @callback fileCallbackSync
719
- * @param {?Error} err the error object if anything goes wrong
720
- * @param {string} name the temporary file name
721
- * @param {number} fd the file descriptor or -1 if the fd had been discarded
722
- * @param {cleanupCallbackSync} fn the cleanup callback function
723
- */
724
-
725
- /**
726
- * @callback dirCallback
727
- * @param {?Error} err the error object if anything goes wrong
728
- * @param {string} name the temporary file name
729
- * @param {cleanupCallback} fn the cleanup callback function
730
- */
731
-
732
- /**
733
- * @callback dirCallbackSync
734
- * @param {?Error} err the error object if anything goes wrong
735
- * @param {string} name the temporary file name
736
- * @param {cleanupCallbackSync} fn the cleanup callback function
737
- */
738
-
739
- /**
740
- * Removes the temporary created file or directory.
741
- *
742
- * @callback cleanupCallback
743
- * @param {simpleCallback} [next] function to call whenever the tmp object needs to be removed
744
- */
745
-
746
- /**
747
- * Removes the temporary created file or directory.
748
- *
749
- * @callback cleanupCallbackSync
750
- */
751
-
752
- /**
753
- * Callback function for function composition.
754
- * @see {@link https://github.com/raszi/node-tmp/issues/57|raszi/node-tmp#57}
755
- *
756
- * @callback simpleCallback
757
- */
758
-
759
- // exporting all the needed methods
760
-
761
- // evaluate _getTmpDir() lazily, mainly for simplifying testing but it also will
762
- // allow users to reconfigure the temporary directory
763
- Object.defineProperty(module.exports, 'tmpdir', {
764
- enumerable: true,
765
- configurable: false,
766
- get: function () {
767
- return _getTmpDir();
768
- }
769
- });
770
-
771
- module.exports.dir = dir;
772
- module.exports.dirSync = dirSync;
773
-
774
- module.exports.file = file;
775
- module.exports.fileSync = fileSync;
776
-
777
- module.exports.tmpName = tmpName;
778
- module.exports.tmpNameSync = tmpNameSync;
779
-
780
- module.exports.setGracefulCleanup = setGracefulCleanup;