store.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.store = void 0;
  6. var _fs = _interopRequireDefault(require("fs"));
  7. var _path = _interopRequireDefault(require("path"));
  8. var _globals = require("./common/globals");
  9. var _utilsBundle = require("playwright-core/lib/utilsBundle");
  10. var _utils = require("playwright-core/lib/utils");
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. /**
  13. * Copyright Microsoft Corporation. All rights reserved.
  14. *
  15. * Licensed under the Apache License, Version 2.0 (the "License");
  16. * you may not use this file except in compliance with the License.
  17. * You may obtain a copy of the License at
  18. *
  19. * http://www.apache.org/licenses/LICENSE-2.0
  20. *
  21. * Unless required by applicable law or agreed to in writing, software
  22. * distributed under the License is distributed on an "AS IS" BASIS,
  23. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24. * See the License for the specific language governing permissions and
  25. * limitations under the License.
  26. */
  27. class JsonStore {
  28. async delete(name) {
  29. const file = this.path(name);
  30. await _fs.default.promises.rm(file, {
  31. force: true
  32. });
  33. }
  34. async get(name) {
  35. const file = this.path(name);
  36. try {
  37. const type = contentType(name);
  38. if (type === 'binary') return await _fs.default.promises.readFile(file);
  39. const text = await _fs.default.promises.readFile(file, 'utf-8');
  40. if (type === 'json') return JSON.parse(text);
  41. return text;
  42. } catch (e) {
  43. return undefined;
  44. }
  45. }
  46. path(name) {
  47. return _path.default.join(this.root(), name);
  48. }
  49. root() {
  50. const config = (0, _globals.currentConfig)();
  51. if (!config) throw new Error('Cannot access store before config is loaded');
  52. return config.storeDir;
  53. }
  54. async set(name, value) {
  55. const file = this.path(name);
  56. if (value === undefined) {
  57. await _fs.default.promises.rm(file, {
  58. force: true
  59. });
  60. return;
  61. }
  62. let data = '';
  63. switch (contentType(name)) {
  64. case 'json':
  65. {
  66. if (Buffer.isBuffer(value)) throw new Error('JSON value must be an Object');
  67. data = JSON.stringify(value, undefined, 2);
  68. break;
  69. }
  70. case 'text':
  71. {
  72. if (!(0, _utils.isString)(value)) throw new Error('Textual value must be a string');
  73. data = value;
  74. break;
  75. }
  76. case 'binary':
  77. {
  78. if (!Buffer.isBuffer(value)) throw new Error('Binary value must be a Buffer');
  79. data = value;
  80. break;
  81. }
  82. }
  83. await _fs.default.promises.mkdir(_path.default.dirname(file), {
  84. recursive: true
  85. });
  86. await _fs.default.promises.writeFile(file, data);
  87. }
  88. }
  89. function contentType(name) {
  90. var _mime$getType;
  91. const mimeType = (_mime$getType = _utilsBundle.mime.getType(_path.default.basename(name))) !== null && _mime$getType !== void 0 ? _mime$getType : 'application/octet-string';
  92. if ((0, _utils.isJsonMimeType)(mimeType)) return 'json';
  93. if ((0, _utils.isTextualMimeType)(mimeType)) return 'text';
  94. return 'binary';
  95. }
  96. const store = exports.store = new JsonStore();