|
| 1 | +import urlcat from 'urlcat' |
| 2 | +import { has } from 'lodash-es' |
| 3 | +import { Flags } from './Flags.js' |
| 4 | + |
| 5 | +interface FetchResult<T extends Record<string, unknown>> { |
| 6 | + flags?: T |
| 7 | + timestamp: number |
| 8 | +} |
| 9 | + |
| 10 | +export class RemoteFlags<T extends Record<string, unknown>> extends Flags<T> { |
| 11 | + private readonly KEY = 'mask-last-fetch-result' |
| 12 | + |
| 13 | + private lastFetchResult: FetchResult<T> | null = null |
| 14 | + |
| 15 | + private get lastStorageResult() { |
| 16 | + try { |
| 17 | + const json = localStorage.getItem(this.KEY) |
| 18 | + const result: FetchResult<T> | null = json ? JSON.parse(json) : null |
| 19 | + return result |
| 20 | + } catch (error) { |
| 21 | + return null |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + constructor( |
| 26 | + private remoteFlagsURL: string, |
| 27 | + defaults: T, |
| 28 | + private initials?: { |
| 29 | + // the valid duration for remote fetched flags |
| 30 | + fetchTTL?: number |
| 31 | + // the valid duration for local storage flags |
| 32 | + storageTTL?: number |
| 33 | + }, |
| 34 | + ) { |
| 35 | + super(defaults) |
| 36 | + |
| 37 | + this.sync() |
| 38 | + } |
| 39 | + |
| 40 | + get options() { |
| 41 | + return { |
| 42 | + fetchTTL: 60 * 60 * 1000, // 1hr |
| 43 | + storageTTL: 2 * 60 * 60 * 1000, // 2hr |
| 44 | + ...this.initials, |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + get isLastFetchResultFresh() { |
| 49 | + return Date.now() - this.lastFetchTimestamp < this.options.fetchTTL |
| 50 | + } |
| 51 | + |
| 52 | + get isLastStorageResultFresh() { |
| 53 | + return Date.now() - this.lastStorageTimestamp < this.options.storageTTL |
| 54 | + } |
| 55 | + |
| 56 | + get lastFetchTimestamp() { |
| 57 | + return this.lastFetchResult?.timestamp ?? 0 |
| 58 | + } |
| 59 | + |
| 60 | + get lastStorageTimestamp() { |
| 61 | + return this.lastStorageResult?.timestamp ?? 0 |
| 62 | + } |
| 63 | + |
| 64 | + override get accessor(): Readonly<T> { |
| 65 | + return new Proxy(this.defaults, { |
| 66 | + get: (target, key, receiver) => { |
| 67 | + if (has(this.lastFetchResult?.flags, key)) return this.lastFetchResult?.flags?.[key as string] |
| 68 | + return Reflect.get(target, key, receiver) |
| 69 | + }, |
| 70 | + set: (target, key, value, receiver) => { |
| 71 | + throw new Error('Not allowed') |
| 72 | + }, |
| 73 | + }) |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Sync with the local storage. |
| 78 | + */ |
| 79 | + sync() { |
| 80 | + if (!this.isLastStorageResultFresh) { |
| 81 | + localStorage.removeItem(this.KEY) |
| 82 | + } |
| 83 | + |
| 84 | + const lastFetchResult = this.lastFetchResult |
| 85 | + const lastStorageResult = this.lastStorageResult |
| 86 | + |
| 87 | + if ( |
| 88 | + (lastFetchResult?.timestamp && !lastStorageResult?.timestamp) || |
| 89 | + (lastFetchResult?.timestamp && |
| 90 | + lastStorageResult?.timestamp && |
| 91 | + lastStorageResult.timestamp < lastFetchResult.timestamp) |
| 92 | + ) { |
| 93 | + console.log('[RemoteFlags] sync from remote') |
| 94 | + localStorage.setItem(this.KEY, JSON.stringify(lastFetchResult)) |
| 95 | + } else if (lastStorageResult?.timestamp) { |
| 96 | + console.log('[RemoteFlags] sync from storage') |
| 97 | + this.lastFetchResult = lastStorageResult |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Fetch flags from the remote server. |
| 103 | + */ |
| 104 | + async fetch() { |
| 105 | + const response = await fetch( |
| 106 | + urlcat(this.remoteFlagsURL, { |
| 107 | + architecture: process.env.architecture, |
| 108 | + channel: process.env.channel, |
| 109 | + engine: process.env.engine, |
| 110 | + NODE_ENV: process.env.NODE_ENV, |
| 111 | + }), |
| 112 | + ) |
| 113 | + const flags: T = await response.json() |
| 114 | + return flags |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Fetch flags from the remote server and patch updates right after fetched. |
| 119 | + */ |
| 120 | + async fetchAndActive() { |
| 121 | + // Prevent fetching too frequently |
| 122 | + if (this.isLastFetchResultFresh) return |
| 123 | + |
| 124 | + this.lastFetchResult = { |
| 125 | + flags: await this.fetch(), |
| 126 | + timestamp: Date.now(), |
| 127 | + } |
| 128 | + this.sync() |
| 129 | + } |
| 130 | +} |
0 commit comments