Debian Codegraph @

node_modules/@sigstore/tuf/dist/client.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.TUFClient = void 0;
7 /*
8 Copyright 2023 The Sigstore Authors.
9
10 Licensed under the Apache License, Version 2.0 (the "License");
11 you may not use this file except in compliance with the License.
12 You may obtain a copy of the License at
13
14 http://www.apache.org/licenses/LICENSE-2.0
15
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
21 */
22 const fs_1 = __importDefault(require("fs"));
23 const path_1 = __importDefault(require("path"));
24 const tuf_js_1 = require("tuf-js");
25 const _1 = require(".");
26 const package_json_1 = require("../package.json");
27 const target_1 = require("./target");
28 const TARGETS_DIR_NAME = 'targets';
29 class TUFClient {
30 updater;
31 constructor(options) {
32 const url = new URL(options.mirrorURL);
33 const repoName = encodeURIComponent(url.host + url.pathname.replace(/\/$/, ''));
34 const cachePath = path_1.default.join(options.cachePath, repoName);
35 initTufCache(cachePath);
36 seedCache({
37 cachePath,
38 mirrorURL: options.mirrorURL,
39 tufRootPath: options.rootPath,
40 forceInit: options.forceInit,
41 });
42 this.updater = initClient({
43 mirrorURL: options.mirrorURL,
44 cachePath,
45 forceCache: options.forceCache,
46 retry: options.retry,
47 timeout: options.timeout,
48 });
49 }
50 async refresh() {
51 return this.updater.refresh();
52 }
53 getTarget(targetName) {
54 return (0, target_1.readTarget)(this.updater, targetName);
55 }
56 }
57 exports.TUFClient = TUFClient;
58 // Initializes the TUF cache directory structure including the initial
59 // root.json file. If the cache directory does not exist, it will be
60 // created. If the targets directory does not exist, it will be created.
61 // If the root.json file does not exist, it will be copied from the
62 // rootPath argument.
63 function initTufCache(cachePath) {
64 const targetsPath = path_1.default.join(cachePath, TARGETS_DIR_NAME);
65 if (!fs_1.default.existsSync(cachePath)) {
66 fs_1.default.mkdirSync(cachePath, { recursive: true });
67 }
68 /* istanbul ignore else */
69 if (!fs_1.default.existsSync(targetsPath)) {
70 fs_1.default.mkdirSync(targetsPath);
71 }
72 }
73 // Populates the TUF cache with the initial root.json file. If the root.json
74 // file does not exist (or we're forcing re-initialization), copy it from either
75 // the rootPath argument or from one of the repo seeds.
76 function seedCache({ cachePath, mirrorURL, tufRootPath, forceInit, }) {
77 const cachedRootPath = path_1.default.join(cachePath, 'root.json');
78 // If the root.json file does not exist (or we're forcing re-initialization),
79 // populate it either from the supplied rootPath or from one of the repo seeds.
80 /* istanbul ignore else */
81 if (!fs_1.default.existsSync(cachedRootPath) || forceInit) {
82 if (tufRootPath) {
83 fs_1.default.copyFileSync(tufRootPath, cachedRootPath);
84 }
85 else {
86 const seeds = require('../seeds.json');
87 const repoSeed = seeds[mirrorURL];
88 if (!repoSeed) {
89 throw new _1.TUFError({
90 code: 'TUF_INIT_CACHE_ERROR',
91 message: `No root.json found for mirror: ${mirrorURL}`,
92 });
93 }
94 fs_1.default.writeFileSync(cachedRootPath, Buffer.from(repoSeed['root.json'], 'base64'));
95 // Copy any seed targets into the cache
96 Object.entries(repoSeed.targets).forEach(([targetName, target]) => {
97 fs_1.default.writeFileSync(path_1.default.join(cachePath, TARGETS_DIR_NAME, targetName), Buffer.from(target, 'base64'));
98 });
99 }
100 }
101 }
102 function initClient(options) {
103 const config = {
104 fetchTimeout: options.timeout,
105 fetchRetry: options.retry,
106 userAgent: `${encodeURIComponent(package_json_1.name)}/${package_json_1.version}`,
107 };
108 return new tuf_js_1.Updater({
109 metadataBaseUrl: options.mirrorURL,
110 targetBaseUrl: `${options.mirrorURL}/targets`,
111 metadataDir: options.cachePath,
112 targetDir: path_1.default.join(options.cachePath, TARGETS_DIR_NAME),
113 forceCache: options.forceCache,
114 config,
115 });
116 }