Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

node-fs-compat.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. 'use strict';
  2. var inspect = require('util').inspect;
  3. function assert(value, message) {
  4. if (!value)
  5. throw new ERR_INTERNAL_ASSERTION(message);
  6. }
  7. assert.fail = function fail(message) {
  8. throw new ERR_INTERNAL_ASSERTION(message);
  9. };
  10. // Only use this for integers! Decimal numbers do not work with this function.
  11. function addNumericalSeparator(val) {
  12. var res = '';
  13. var i = val.length;
  14. var start = val[0] === '-' ? 1 : 0;
  15. for (; i >= start + 4; i -= 3)
  16. res = `_${val.slice(i - 3, i)}${res}`;
  17. return `${val.slice(0, i)}${res}`;
  18. }
  19. function oneOf(expected, thing) {
  20. assert(typeof thing === 'string', '`thing` has to be of type string');
  21. if (Array.isArray(expected)) {
  22. var len = expected.length;
  23. assert(len > 0, 'At least one expected value needs to be specified');
  24. expected = expected.map((i) => String(i));
  25. if (len > 2) {
  26. return `one of ${thing} ${expected.slice(0, len - 1).join(', ')}, or `
  27. + expected[len - 1];
  28. } else if (len === 2) {
  29. return `one of ${thing} ${expected[0]} or ${expected[1]}`;
  30. } else {
  31. return `of ${thing} ${expected[0]}`;
  32. }
  33. } else {
  34. return `of ${thing} ${String(expected)}`;
  35. }
  36. }
  37. exports.ERR_INTERNAL_ASSERTION = class ERR_INTERNAL_ASSERTION extends Error {
  38. constructor(message) {
  39. super();
  40. Error.captureStackTrace(this, ERR_INTERNAL_ASSERTION);
  41. var suffix = 'This is caused by either a bug in ssh2-streams '
  42. + 'or incorrect usage of ssh2-streams internals.\n'
  43. + 'Please open an issue with this stack trace at '
  44. + 'https://github.com/mscdex/ssh2-streams/issues\n';
  45. this.message = (message === undefined ? suffix : `${message}\n${suffix}`);
  46. }
  47. };
  48. var MAX_32BIT_INT = Math.pow(2, 32);
  49. var MAX_32BIT_BIGINT = (function() {
  50. try {
  51. return new Function('return 2n ** 32n')();
  52. } catch (ex) {}
  53. })();
  54. exports.ERR_OUT_OF_RANGE = class ERR_OUT_OF_RANGE extends RangeError {
  55. constructor(str, range, input, replaceDefaultBoolean) {
  56. super();
  57. Error.captureStackTrace(this, ERR_OUT_OF_RANGE);
  58. assert(range, 'Missing "range" argument');
  59. var msg = (replaceDefaultBoolean
  60. ? str
  61. : `The value of "${str}" is out of range.`);
  62. var received;
  63. if (Number.isInteger(input) && Math.abs(input) > MAX_32BIT_INT) {
  64. received = addNumericalSeparator(String(input));
  65. } else if (typeof input === 'bigint') {
  66. received = String(input);
  67. if (input > MAX_32BIT_BIGINT || input < -MAX_32BIT_BIGINT)
  68. received = addNumericalSeparator(received);
  69. received += 'n';
  70. } else {
  71. received = inspect(input);
  72. }
  73. msg += ` It must be ${range}. Received ${received}`;
  74. this.message = msg;
  75. }
  76. };
  77. exports.ERR_INVALID_ARG_TYPE = class ERR_INVALID_ARG_TYPE extends TypeError {
  78. constructor(name, expected, actual) {
  79. super();
  80. Error.captureStackTrace(this, ERR_INVALID_ARG_TYPE);
  81. assert(typeof name === 'string', `'name' must be a string`);
  82. // determiner: 'must be' or 'must not be'
  83. var determiner;
  84. if (typeof expected === 'string' && expected.startsWith('not ')) {
  85. determiner = 'must not be';
  86. expected = expected.replace(/^not /, '');
  87. } else {
  88. determiner = 'must be';
  89. }
  90. var msg;
  91. if (name.endsWith(' argument')) {
  92. // For cases like 'first argument'
  93. msg = `The ${name} ${determiner} ${oneOf(expected, 'type')}`;
  94. } else {
  95. var type = (name.includes('.') ? 'property' : 'argument');
  96. msg = `The "${name}" ${type} ${determiner} ${oneOf(expected, 'type')}`;
  97. }
  98. msg += `. Received type ${typeof actual}`;
  99. this.message = msg;
  100. }
  101. };
  102. exports.validateNumber = function validateNumber(value, name) {
  103. if (typeof value !== 'number')
  104. throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
  105. };
  106. // =============================================================================
  107. // Following code is only needed to support node v6.x ....
  108. // Undocumented cb() API, needed for core, not for public API
  109. exports.destroyImpl = function destroy(err, cb) {
  110. const readableDestroyed = this._readableState &&
  111. this._readableState.destroyed;
  112. const writableDestroyed = this._writableState &&
  113. this._writableState.destroyed;
  114. if (readableDestroyed || writableDestroyed) {
  115. if (cb) {
  116. cb(err);
  117. } else if (err) {
  118. if (!this._writableState) {
  119. process.nextTick(emitErrorNT, this, err);
  120. } else if (!this._writableState.errorEmitted) {
  121. this._writableState.errorEmitted = true;
  122. process.nextTick(emitErrorNT, this, err);
  123. }
  124. }
  125. return this;
  126. }
  127. // We set destroyed to true before firing error callbacks in order
  128. // to make it re-entrance safe in case destroy() is called within callbacks
  129. if (this._readableState) {
  130. this._readableState.destroyed = true;
  131. }
  132. // If this is a duplex stream mark the writable part as destroyed as well
  133. if (this._writableState) {
  134. this._writableState.destroyed = true;
  135. }
  136. this._destroy(err || null, (err) => {
  137. if (!cb && err) {
  138. if (!this._writableState) {
  139. process.nextTick(emitErrorAndCloseNT, this, err);
  140. } else if (!this._writableState.errorEmitted) {
  141. this._writableState.errorEmitted = true;
  142. process.nextTick(emitErrorAndCloseNT, this, err);
  143. } else {
  144. process.nextTick(emitCloseNT, this);
  145. }
  146. } else if (cb) {
  147. process.nextTick(emitCloseNT, this);
  148. cb(err);
  149. } else {
  150. process.nextTick(emitCloseNT, this);
  151. }
  152. });
  153. return this;
  154. };
  155. function emitErrorAndCloseNT(self, err) {
  156. emitErrorNT(self, err);
  157. emitCloseNT(self);
  158. }
  159. function emitCloseNT(self) {
  160. if (self._writableState && !self._writableState.emitClose)
  161. return;
  162. if (self._readableState && !self._readableState.emitClose)
  163. return;
  164. self.emit('close');
  165. }
  166. // =============================================================================