(core) move some material to core that slipped through in a rebase

Summary:
This makes core independently buildable again, and adds a small
script to run as a sanity check.

Test Plan: checked that build_core.sh succeeds

Reviewers: dsagal

Reviewed By: dsagal

Subscribers: dsagal

Differential Revision: https://phab.getgrist.com/D2558
This commit is contained in:
Paul Fitzpatrick
2020-07-23 10:34:05 -04:00
parent b71f2f2a10
commit b7b4b0229b
3 changed files with 244 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
/**
* Get a revokable named exclusive lock with a TTL. This is convenient for housekeeping
* tasks, which can be done by any server, but should preferably be only done by one
* at a time.
*/
export interface IElectionStore {
/**
* Try to get a lock called <name> for a specified duration. If the named lock
* has already been taken, null is returned, otherwise a secret is returned.
* The secret can be used to remove the lock before the duration has expired.
*/
getElection(name: string, durationInMs: number): Promise<string|null>;
/**
* Remove a named lock, presenting the secret returned by getElection() as
* a cross-check.
*/
removeElection(name: string, electionKey: string): Promise<void>;
/**
* Close down access to the store.
*/
close(): Promise<void>;
}

58
app/server/lib/Permit.ts Normal file
View File

@@ -0,0 +1,58 @@
/**
* An exceptional grant of rights on a resource, for when work needs to be
* initiated by Grist systems rather than a user. Cases where this may happen:
*
* - Deletion of documents and workspaces in the trash
*
* Permits are stored in redis (or, in a single-process dev environment, in memory)
* as json, in keys that expire within minutes. The keys should be effectively
* unguessable.
*
* To use a permit:
*
* - Prepare a Permit object that includes the id of the document or
* workspace to be operated on.
*
* - It the operation you care about involves the database, check
* that "allowSpecialPermit" is enabled for it in HomeDBManager
* (currently only deletion of docs/workspaces has this enabled).
*
* - Save the permit in the permit store, with setPermit, noting its
* generated key.
*
* - Call the API with a "Permit: <permit-key>" header.
*
* - Optionally, remove the permit with removePermit().
*/
export interface Permit {
docId?: string;
workspaceId?: number;
org?: string|number;
}
/* A store of permits */
export interface IPermitStore {
// Store a permit, and return the key it is stored in.
// Permits are transient, and will expire.
setPermit(permit: Permit): Promise<string>;
// Get any permit associated with the given key, or null if none.
getPermit(permitKey: string): Promise<Permit|null>;
// Remove any permit associated with the given key.
removePermit(permitKey: string): Promise<void>;
// Close down the permit store.
close(): Promise<void>;
}
// Create a well formatted permit key from a seed string.
export function formatPermitKey(seed: string) {
return `permit-${seed}`;
}
// Check that permit key is well formatted.
export function checkPermitKey(key: string): boolean {
return key.startsWith('permit-');
}