From b0d6cfb6c64e0e1f279f7fe5473d351aa682c5a8 Mon Sep 17 00:00:00 2001 From: Alex Komoroske Date: Sat, 22 Jul 2023 15:54:04 -0700 Subject: [PATCH] Make it so ProfileFilesystem saves the _allowedFetches when you say to save one. Part of #45. --- src/types.ts | 4 +++- tools/profile_filesystem.ts | 38 +++++++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/types.ts b/src/types.ts index 1bae0ac..6e014ad 100644 --- a/src/types.ts +++ b/src/types.ts @@ -290,8 +290,10 @@ export const requiredSeedReference = z.object({ export type AbsoluteSeedReference = z.infer; +export const urlDomain = z.string(); + //e.g. 'komoroske.com' or 'localhost' -export type URLDomain = string; +export type URLDomain = z.infer; //we want a regexp, and z.string().url() does not use a URL so just munge one const urlRegExp = new RegExp('((http(s)?:\\/\\/)?(www\\.)?(([a-zA-Z\\d-]+\\.)+[a-zA-Z]{2,}|((\\d{1,3}\\.){3}\\d{1,3}))(\\:\\d+)?(\\/[-a-zA-Z\\d%_.~+]*)*(\\?[;&a-zA-Z\\d%_.~+=-]*)?(\\#[-a-zA-Z\\d_]*)?)'); diff --git a/tools/profile_filesystem.ts b/tools/profile_filesystem.ts index 10aec9b..78708d3 100644 --- a/tools/profile_filesystem.ts +++ b/tools/profile_filesystem.ts @@ -7,7 +7,9 @@ import { MemoryID, StoreID, StoreKey, - StoreValue + StoreValue, + seedPacketAbsoluteRemoteLocation, + urlDomain } from '../src/types.js'; import { @@ -45,6 +47,10 @@ const CURRENT_PROFILE_VERSION = 0; const profileMetadata = z.object({ version: z.literal(0), + allowedFetches: z.record( + seedPacketAbsoluteRemoteLocation, + z.record(urlDomain, z.literal(true)) + ) }); type ProfileMetadata = z.infer; @@ -74,6 +80,32 @@ export class ProfileFilesystem extends Profile { return super.garden; } + override async allowFetch(remotePacketLocation: string, domain: string): Promise { + if (this._allowedFetches[remotePacketLocation]) { + if (this._allowedFetches[remotePacketLocation][domain]) return true; + } + const ALLOW_ONCE = 'Allow once'; + const ALLOW_FOREVER = 'Allow forever'; + const DISALLOW = 'Disallow'; + const answers = await inquirer.prompt([ + { + name: 'question', + type: 'list', + message: `Do you want to allow a seed from ${remotePacketLocation} to fetch from domain ${domain}?`, + choices: [ALLOW_ONCE, ALLOW_FOREVER, DISALLOW], + default: ALLOW_ONCE + } + ]); + const answer = answers.question as string; + if (answer == DISALLOW) return false; + if (answer == ALLOW_ONCE) return true; + //They want to save the answer + if (!this._allowedFetches[remotePacketLocation]) this._allowedFetches[remotePacketLocation] = {}; + this._allowedFetches[remotePacketLocation][domain] = true; + this._saveMetadata(); + return true; + } + _loadMetadata() : void { const metadataFile = path.join(this._profileDir, METADATA_FILE); @@ -86,12 +118,14 @@ export class ProfileFilesystem extends Profile { const parsedData = profileMetadata.parse(JSON.parse(data)); if (parsedData.version != CURRENT_PROFILE_VERSION) throw new Error('Profile has a different version than expected'); this._loaded = true; + this._allowedFetches = parsedData.allowedFetches; } _saveMetadata() : void { const metadataFile = path.join(this._profileDir, METADATA_FILE); const data : ProfileMetadata = { - version: 0 + version: 0, + allowedFetches: this._allowedFetches }; ensureFolder(this._profileDir); fs.writeFileSync(metadataFile, JSON.stringify(data, null, '\t'));