Debian Codegraph @

debian/tests/test_modules/@tufjs/repo-mock/dist/index.js

raw
1 "use strict";
2 var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4 };
5 Object.defineProperty(exports, "__esModule", { value: true });
6 exports.initializeTUFRepo = exports.tufHandlers = void 0;
7 exports.mockRepo = mockRepo;
8 exports.clearMock = clearMock;
9 const fs_1 = __importDefault(require("fs"));
10 const nock_1 = __importDefault(require("nock"));
11 const os_1 = __importDefault(require("os"));
12 const path_1 = __importDefault(require("path"));
13 const handler_1 = require("./handler");
14 const mock_1 = require("./mock");
15 const repo_1 = require("./repo");
16 var handler_2 = require("./handler");
17 Object.defineProperty(exports, "tufHandlers", { enumerable: true, get: function () { return handler_2.tufHandlers; } });
18 var repo_2 = require("./repo");
19 Object.defineProperty(exports, "initializeTUFRepo", { enumerable: true, get: function () { return repo_2.initializeTUFRepo; } });
20 function mockRepo(baseURL, targets, options = {}) {
21 const tufRepo = (0, repo_1.initializeTUFRepo)(targets);
22 const handlers = (0, handler_1.tufHandlers)(tufRepo, options);
23 handlers.forEach((handler) => {
24 // Don't set-up a mock for the 1.root.json file as this should never be
25 // fetched in a normal TUF flow.
26 if (handler.path.endsWith('1.root.json')) {
27 return;
28 }
29 (0, mock_1.mock)(baseURL, handler);
30 });
31 return JSON.stringify(tufRepo.rootMeta.toJSON());
32 }
33 function clearMock() {
34 nock_1.default.cleanAll();
35 }
36 class Scope {
37 targets;
38 options;
39 baseURL;
40 cachePath;
41 constructor(targets, options = {}) {
42 this.targets = targets;
43 this.options = options;
44 this.baseURL =
45 options.baseURL ??
46 `http://${Math.random().toString(36).substring(2)}.com`;
47 if (options.cachePath) {
48 fs_1.default.mkdirSync(options.cachePath, { recursive: true });
49 this.cachePath = options.cachePath;
50 }
51 else {
52 this.cachePath = fs_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), 'tuf-cache-test-'));
53 }
54 this.setup();
55 }
56 reset() {
57 this.teardown();
58 this.setup();
59 }
60 teardown() {
61 clearMock();
62 fs_1.default.rmSync(this.cachePath, { recursive: true });
63 }
64 setup() {
65 const rootJSON = mockRepo(this.baseURL, this.targets, this.options);
66 fs_1.default.writeFileSync(path_1.default.join(this.cachePath, 'root.json'), rootJSON);
67 }
68 }
69 exports.default = (targets, options = {}) => {
70 if (!Array.isArray(targets)) {
71 targets = [targets];
72 }
73 return new Scope(targets, options);
74 };