From 44265700cae2557a42a5be5b9257686cdb9b546d Mon Sep 17 00:00:00 2001 From: guanbinrui Date: Sun, 20 Feb 2022 19:23:55 +0800 Subject: [PATCH 1/2] chore: add SDK --- packages/configuration/README.md | 21 +++++++++++++++ packages/configuration/package.json | 10 +++++++ packages/configuration/src/constants.ts | 2 ++ packages/configuration/src/index.ts | 36 +++++++++++++++++++++++++ packages/configuration/tsconfig.json | 10 +++++++ packages/mask/.webpack/config.ts | 1 + tsconfig.json | 1 + 7 files changed, 81 insertions(+) create mode 100644 packages/configuration/README.md create mode 100644 packages/configuration/package.json create mode 100644 packages/configuration/src/constants.ts create mode 100644 packages/configuration/src/index.ts create mode 100644 packages/configuration/tsconfig.json diff --git a/packages/configuration/README.md b/packages/configuration/README.md new file mode 100644 index 000000000000..73779adc31c4 --- /dev/null +++ b/packages/configuration/README.md @@ -0,0 +1,21 @@ +# Configuration + +This package is an client of [Mask Configuration](https://github.com/DimensionDev/Mask-Configuration). It fetches configuration from a remote server. In brief, it returns the stale data while emitting an updating request to keep data fresh finally. + +## Usage + +```ts +const WHITELIST = create('whitelist', [ + '0x0000000000000000000000000000000000000000', + '0x0000000000000000000000000000000000000001', + '0x0000000000000000000000000000000000000002', + '0x0000000000000000000000000000000000000003', +]) + +// at first, it returns the initial whitelist +// at the same time, it emits a revalidate request +const whitelist = WHITELIST.get() + +// a few moments later, the server returns the fresh data +const whitelistSynced = WHITELIST.get() +``` diff --git a/packages/configuration/package.json b/packages/configuration/package.json new file mode 100644 index 000000000000..cff900427748 --- /dev/null +++ b/packages/configuration/package.json @@ -0,0 +1,10 @@ +{ + "name": "@masknet/configuration", + "private": true, + "main": "./dist/index.js", + "types": "./dist/index.d.ts", + "type": "module", + "dependencies": { + "urlcat": "^2.0.4" + } +} diff --git a/packages/configuration/src/constants.ts b/packages/configuration/src/constants.ts new file mode 100644 index 000000000000..849c20ba1aa4 --- /dev/null +++ b/packages/configuration/src/constants.ts @@ -0,0 +1,2 @@ +export const DEFAULT_HOST = 'https://configuration.r2d2.to' +export const DEFAULT_PREFIX = 'com.maskbook' diff --git a/packages/configuration/src/index.ts b/packages/configuration/src/index.ts new file mode 100644 index 000000000000..3b7ff75c8a2f --- /dev/null +++ b/packages/configuration/src/index.ts @@ -0,0 +1,36 @@ +import urlcat from 'urlcat' +import { DEFAULT_HOST, DEFAULT_PREFIX } from './constants' + +class Configuration { + private ac: AbortController | undefined + + constructor(private url: string, private data: T | undefined) { + this.revalidate() + } + + private async revalidate() { + if (this.ac) this.ac.abort() + this.ac = new AbortController() + + const response = await fetch(this.url, { + signal: this.ac.signal, + }) + this.data = (await response.json()) as T + } + + get() { + this.revalidate() + return this.data + } +} + +const cache = new Map>() + +export function create(name: string, initialData?: T) { + const url = urlcat(`${DEFAULT_HOST}', ':prefix.:name.json`, { + name, + prefix: DEFAULT_PREFIX, + }) + if (!cache.has(url)) cache.set(url, new Configuration(url, initialData)) + return cache.get(url) as Configuration +} diff --git a/packages/configuration/tsconfig.json b/packages/configuration/tsconfig.json new file mode 100644 index 000000000000..8bcad252c078 --- /dev/null +++ b/packages/configuration/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "rootDir": "./src/", + "outDir": "./dist/", + "tsBuildInfoFile": "./dist/.tsbuildinfo" + }, + "include": ["./src"], + "references": [] +} diff --git a/packages/mask/.webpack/config.ts b/packages/mask/.webpack/config.ts index 72edf9f10e57..00454c9c7169 100644 --- a/packages/mask/.webpack/config.ts +++ b/packages/mask/.webpack/config.ts @@ -64,6 +64,7 @@ export function createConfiguration(rawFlags: BuildFlags): Configuration { // Those packages are also installed as dependencies so they appears in node_modules // By aliasing them to the original position, // we can speed up the compile because there is no need to wait tsc build them to the dist folder. + '@masknet/configuration': join(__dirname, '../../configuration/src/'), '@masknet/dashboard$': require.resolve('../../dashboard/src/entry.tsx'), '@masknet/injected-script': join(__dirname, '../../injected-script/sdk'), '@masknet/gun-utils': join(__dirname, '../../gun-utils/src/'), diff --git a/tsconfig.json b/tsconfig.json index 47d398c3eae7..116e9f0f0dd0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -58,6 +58,7 @@ // which is unwanted. We need to take care of this. "@masknet/backup-format": ["./packages/backup-format/src"], + "@masknet/configuration": ["./packages/configuration/src"], // ! dashboard is explicitly opt-out of this feature, it and the /packages/mask has circulare reference that TypeScript does not allows. // "@masknet/dashboard": ["./packages/dashboard/src"], "@masknet/encryption": ["./packages/encryption/src"], From eb2af1b51a84ad634f11aa87c5097727f87a609a Mon Sep 17 00:00:00 2001 From: guanbinrui Date: Tue, 22 Feb 2022 16:03:49 +0800 Subject: [PATCH 2/2] fix: lock file --- pnpm-lock.yaml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2aa10db9a6c4..5036f662449b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -126,6 +126,12 @@ importers: dependencies: '@msgpack/msgpack': 2.7.2 + packages/configuration: + specifiers: + urlcat: ^2.0.4 + dependencies: + urlcat: 2.0.4 + packages/dashboard: specifiers: '@babel/core': ^7.17.2