File size: 1,523 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import debugModule from 'debug';
import { StoredItems } from './types';

const debug = debugModule( 'calypso:support-user' );

// This module defines a series of methods which bypasse all persistent storage.
// Any calls to read/write data using browser-storage instead access a temporary
// in-memory store which is lost on page reload. This driver is used to sandbox
// a user's data while support-user is active, ensuring it does not contaminate
// the original user, and vice versa.

const memoryStore = new Map();

export async function getAllStoredItems( pattern?: RegExp ): Promise< StoredItems > {
	debug( 'browser-storage bypass', 'getAllStoredItems' );

	// Return everything.
	if ( ! pattern ) {
		return Object.fromEntries( memoryStore.entries() );
	}

	// Return only the entries that match the pattern.
	const entries = Array.from( memoryStore.entries() );
	return Object.fromEntries( entries.filter( ( [ key ] ) => pattern.test( key ) ) );
}

export async function getStoredItem< T >( key: string ): Promise< T | undefined > {
	debug( 'browser-storage bypass', 'getStoredItem', key );

	if ( memoryStore.has( key ) ) {
		return memoryStore.get( key );
	}

	return undefined;
}

export async function setStoredItem< T >( key: string, value: T ) {
	debug( 'browser-storage bypass', 'setStoredItem', key );
	memoryStore.set( key, value );
}

export async function clearStorage() {
	debug( 'browser-storage bypass', 'clearStorage' );
	memoryStore.clear();
}

export function activate() {
	memoryStore.clear();
}