Skip to content

Commit

Permalink
Make it so ProfileFilesystem saves the _allowedFetches when you say t…
Browse files Browse the repository at this point in the history
…o save one.

Part of #45.
  • Loading branch information
jkomoros committed Jul 22, 2023
1 parent f48de7f commit b0d6cfb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,10 @@ export const requiredSeedReference = z.object({

export type AbsoluteSeedReference = z.infer<typeof requiredSeedReference>;

export const urlDomain = z.string();

//e.g. 'komoroske.com' or 'localhost'
export type URLDomain = string;
export type URLDomain = z.infer<typeof urlDomain>;

//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_]*)?)');
Expand Down
38 changes: 36 additions & 2 deletions tools/profile_filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import {
MemoryID,
StoreID,
StoreKey,
StoreValue
StoreValue,
seedPacketAbsoluteRemoteLocation,
urlDomain
} from '../src/types.js';

import {
Expand Down Expand Up @@ -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<typeof profileMetadata>;
Expand Down Expand Up @@ -74,6 +80,32 @@ export class ProfileFilesystem extends Profile {
return super.garden;
}

override async allowFetch(remotePacketLocation: string, domain: string): Promise<boolean> {
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);
Expand All @@ -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'));
Expand Down

0 comments on commit b0d6cfb

Please sign in to comment.