index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.deserialize = deserialize;
  6. exports.serialize = serialize;
  7. exports.readFileSync = readFileSync;
  8. exports.writeFileSync = writeFileSync;
  9. exports.default = void 0;
  10. function _fs() {
  11. const data = _interopRequireDefault(require('fs'));
  12. _fs = function _fs() {
  13. return data;
  14. };
  15. return data;
  16. }
  17. function _v() {
  18. const data = _interopRequireDefault(require('v8'));
  19. _v = function _v() {
  20. return data;
  21. };
  22. return data;
  23. }
  24. function _interopRequireDefault(obj) {
  25. return obj && obj.__esModule ? obj : {default: obj};
  26. }
  27. /**
  28. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  29. *
  30. * This source code is licensed under the MIT license found in the
  31. * LICENSE file in the root directory of this source tree.
  32. */
  33. // TODO: Remove this
  34. /// <reference path="../v8.d.ts" />
  35. // JSON and V8 serializers are both stable when it comes to compatibility. The
  36. // current JSON specification is well defined in RFC 8259, and V8 ensures that
  37. // the versions are compatible by encoding the serialization version in the own
  38. // generated buffer.
  39. const JS_TYPE = '__$t__';
  40. const JS_VALUE = '__$v__';
  41. const JS_VF = '__$f__';
  42. function replacer(_key, value) {
  43. // NaN cannot be in a switch statement, because NaN !== NaN.
  44. if (Number.isNaN(value)) {
  45. return {
  46. [JS_TYPE]: 'n'
  47. };
  48. }
  49. switch (value) {
  50. case undefined:
  51. return {
  52. [JS_TYPE]: 'u'
  53. };
  54. case +Infinity:
  55. return {
  56. [JS_TYPE]: '+'
  57. };
  58. case -Infinity:
  59. return {
  60. [JS_TYPE]: '-'
  61. };
  62. }
  63. switch (value && value.constructor) {
  64. case Date:
  65. return {
  66. [JS_TYPE]: 'd',
  67. [JS_VALUE]: value.getTime()
  68. };
  69. case RegExp:
  70. return {
  71. [JS_TYPE]: 'r',
  72. [JS_VALUE]: value.source,
  73. [JS_VF]: value.flags
  74. };
  75. case Set:
  76. return {
  77. [JS_TYPE]: 's',
  78. [JS_VALUE]: Array.from(value)
  79. };
  80. case Map:
  81. return {
  82. [JS_TYPE]: 'm',
  83. [JS_VALUE]: Array.from(value)
  84. };
  85. case Buffer:
  86. return {
  87. [JS_TYPE]: 'b',
  88. [JS_VALUE]: value.toString('latin1')
  89. };
  90. }
  91. return value;
  92. }
  93. function reviver(_key, value) {
  94. if (!value || (typeof value !== 'object' && !value.hasOwnProperty(JS_TYPE))) {
  95. return value;
  96. }
  97. switch (value[JS_TYPE]) {
  98. case 'u':
  99. return undefined;
  100. case 'n':
  101. return NaN;
  102. case '+':
  103. return +Infinity;
  104. case '-':
  105. return -Infinity;
  106. case 'd':
  107. return new Date(value[JS_VALUE]);
  108. case 'r':
  109. return new RegExp(value[JS_VALUE], value[JS_VF]);
  110. case 's':
  111. return new Set(value[JS_VALUE]);
  112. case 'm':
  113. return new Map(value[JS_VALUE]);
  114. case 'b':
  115. return Buffer.from(value[JS_VALUE], 'latin1');
  116. }
  117. return value;
  118. }
  119. function jsonStringify(content) {
  120. // Not pretty, but the ES JSON spec says that "toJSON" will be called before
  121. // getting into your replacer, so we have to remove them beforehand. See
  122. // https://www.ecma-international.org/ecma-262/#sec-serializejsonproperty
  123. // section 2.b for more information.
  124. const dateToJSON = Date.prototype.toJSON;
  125. const bufferToJSON = Buffer.prototype.toJSON;
  126. /* eslint-disable no-extend-native */
  127. try {
  128. // @ts-ignore intentional removal of "toJSON" property.
  129. Date.prototype.toJSON = undefined; // @ts-ignore intentional removal of "toJSON" property.
  130. Buffer.prototype.toJSON = undefined;
  131. return JSON.stringify(content, replacer);
  132. } finally {
  133. Date.prototype.toJSON = dateToJSON;
  134. Buffer.prototype.toJSON = bufferToJSON;
  135. }
  136. /* eslint-enable no-extend-native */
  137. }
  138. function jsonParse(content) {
  139. return JSON.parse(content, reviver);
  140. } // In memory functions.
  141. function deserialize(buffer) {
  142. return _v().default.deserialize
  143. ? _v().default.deserialize(buffer)
  144. : jsonParse(buffer.toString('utf8'));
  145. }
  146. function serialize(content) {
  147. return _v().default.serialize
  148. ? _v().default.serialize(content)
  149. : Buffer.from(jsonStringify(content));
  150. } // Synchronous filesystem functions.
  151. function readFileSync(filePath) {
  152. return _v().default.deserialize
  153. ? _v().default.deserialize(_fs().default.readFileSync(filePath))
  154. : jsonParse(_fs().default.readFileSync(filePath, 'utf8'));
  155. }
  156. function writeFileSync(filePath, content) {
  157. return _v().default.serialize
  158. ? _fs().default.writeFileSync(filePath, _v().default.serialize(content))
  159. : _fs().default.writeFileSync(filePath, jsonStringify(content), 'utf8');
  160. }
  161. var _default = {
  162. deserialize,
  163. readFileSync,
  164. serialize,
  165. writeFileSync
  166. };
  167. exports.default = _default;