diff --git a/packages/cli/src/cli.ts b/packages/cli/src/cli.ts index 6df39d9204..4b16e24f02 100644 --- a/packages/cli/src/cli.ts +++ b/packages/cli/src/cli.ts @@ -57,7 +57,7 @@ async function main() { log.info(`Processsed args: ${JSON.stringify(processedArgs, null, 4)}`) if (processedArgs.api) { await new ReloadingAPI(envPath, config, pair, processedArgs).start().then(() => { - log.info('Running API restart process...') + log.info('Reloading API started...') }) } else { process.exit(0) diff --git a/packages/cli/src/reloader.ts b/packages/cli/src/reloader.ts index 9e4249d2b5..f3ef7a6277 100644 --- a/packages/cli/src/reloader.ts +++ b/packages/cli/src/reloader.ts @@ -59,6 +59,7 @@ export default class ReloadingAPI { private async _watchEnv() { return fs.watchFile(this._envPath, async () => { + log.info('env file change detected') if (!this._restarting) { this._restarting = true await this.stop() diff --git a/packages/cli/src/start.ts b/packages/cli/src/start.ts index 06b6aec34d..ba761be525 100644 --- a/packages/cli/src/start.ts +++ b/packages/cli/src/start.ts @@ -11,9 +11,9 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -import { CombinedApiPaths } from '@prosopo/types' +import type { CombinedApiPaths } from '@prosopo/types' import { ProviderEnvironment } from '@prosopo/env' -import { Server } from 'node:net' +import type { Server } from 'node:net' import { getDB, getSecret } from './process.env.js' import { getPairAsync } from '@prosopo/contract' import { i18nMiddleware } from '@prosopo/common' @@ -25,7 +25,7 @@ import getConfig from './prosopo.config.js' import rateLimit from 'express-rate-limit' function startApi(env: ProviderEnvironment, admin = false): Server { - env.logger.info(`Starting Prosopo API`) + env.logger.info('Starting Prosopo API') const apiApp = express() const apiPort = env.config.server.port @@ -55,9 +55,6 @@ export async function start(env?: ProviderEnvironment, admin?: boolean) { if (!env) { loadEnv() - // Fail to start api if db is not defined - getDB() - const secret = getSecret() const config = getConfig(undefined, undefined, undefined, { solved: { count: 2 }, @@ -67,6 +64,9 @@ export async function start(env?: ProviderEnvironment, admin?: boolean) { const pair = await getPairAsync(config.networks[config.defaultNetwork], secret, '') env = new ProviderEnvironment(config, pair) } + // Fail to start api if db env var is not defined + getDB() + await env.isReady() // Start the scheduled job diff --git a/packages/env/src/env.ts b/packages/env/src/env.ts index a39d82cfd1..430717b729 100644 --- a/packages/env/src/env.ts +++ b/packages/env/src/env.ts @@ -116,9 +116,9 @@ export class Environment implements ProsopoEnvironment { }) } if (this.db && this.db.connection?.readyState !== 1) { - this.logger.warn(`Database connection is not ready, reconnecting...`) + this.logger.warn('Database connection is not ready, reconnecting...') await this.db.connect() - this.logger.info(`Connected to db`) + this.logger.info('Connected to db') } } catch (err) { throw new ProsopoEnvError('GENERAL.ENVIRONMENT_NOT_READY', { context: { error: err }, logger: this.logger }) diff --git a/packages/procaptcha-bundle/stats.html b/packages/procaptcha-bundle/stats.html index fa61c2296f..c4459143d7 100644 --- a/packages/procaptcha-bundle/stats.html +++ b/packages/procaptcha-bundle/stats.html @@ -1,14028 +1,4842 @@ - - - - - - - Rollup Visualizer - + + +
+ - - + } + } + + // If the fill character is not "0", grouping is applied before padding. + if (comma && !zero) value = group(value, Infinity); + + // Compute the padding. + var length = valuePrefix.length + value.length + valueSuffix.length, + padding = length < width ? new Array(width - length + 1).join(fill) : ""; + + // If the fill character is "0", grouping is applied after padding. + if (comma && zero) value = group(padding + value, padding.length ? width - valueSuffix.length : Infinity), padding = ""; + + // Reconstruct the final output based on the desired alignment. + switch (align) { + case "<": value = valuePrefix + value + valueSuffix + padding; break; + case "=": value = valuePrefix + padding + value + valueSuffix; break; + case "^": value = padding.slice(0, length = padding.length >> 1) + valuePrefix + value + valueSuffix + padding.slice(length); break; + default: value = padding + valuePrefix + value + valueSuffix; break; + } + + return numerals(value); + } + + format.toString = function() { + return specifier + ""; + }; + + return format; + } + + function formatPrefix(specifier, value) { + var f = newFormat((specifier = formatSpecifier(specifier), specifier.type = "f", specifier)), + e = Math.max(-8, Math.min(8, Math.floor(exponent(value) / 3))) * 3, + k = Math.pow(10, -e), + prefix = prefixes[8 + e / 3]; + return function(value) { + return f(k * value) + prefix; + }; + } + + return { + format: newFormat, + formatPrefix: formatPrefix + }; + } + + var locale; + var format; + var formatPrefix; + + defaultLocale({ + thousands: ",", + grouping: [3], + currency: ["$", ""] + }); + + function defaultLocale(definition) { + locale = formatLocale(definition); + format = locale.format; + formatPrefix = locale.formatPrefix; + return locale; + } + + function precisionFixed(step) { + return Math.max(0, -exponent(Math.abs(step))); + } + + function precisionPrefix(step, value) { + return Math.max(0, Math.max(-8, Math.min(8, Math.floor(exponent(value) / 3))) * 3 - exponent(Math.abs(step))); + } + + function precisionRound(step, max) { + step = Math.abs(step), max = Math.abs(max) - step; + return Math.max(0, exponent(max) - exponent(step)) + 1; + } + + function tickFormat(start, stop, count, specifier) { + var step = tickStep(start, stop, count), + precision; + specifier = formatSpecifier(specifier == null ? ",f" : specifier); + switch (specifier.type) { + case "s": { + var value = Math.max(Math.abs(start), Math.abs(stop)); + if (specifier.precision == null && !isNaN(precision = precisionPrefix(step, value))) specifier.precision = precision; + return formatPrefix(specifier, value); + } + case "": + case "e": + case "g": + case "p": + case "r": { + if (specifier.precision == null && !isNaN(precision = precisionRound(step, Math.max(Math.abs(start), Math.abs(stop))))) specifier.precision = precision - (specifier.type === "e"); + break; + } + case "f": + case "%": { + if (specifier.precision == null && !isNaN(precision = precisionFixed(step))) specifier.precision = precision - (specifier.type === "%") * 2; + break; + } + } + return format(specifier); + } + + function linearish(scale) { + var domain = scale.domain; + + scale.ticks = function(count) { + var d = domain(); + return ticks(d[0], d[d.length - 1], count == null ? 10 : count); + }; + + scale.tickFormat = function(count, specifier) { + var d = domain(); + return tickFormat(d[0], d[d.length - 1], count == null ? 10 : count, specifier); + }; + + scale.nice = function(count) { + if (count == null) count = 10; + + var d = domain(); + var i0 = 0; + var i1 = d.length - 1; + var start = d[i0]; + var stop = d[i1]; + var prestep; + var step; + var maxIter = 10; + + if (stop < start) { + step = start, start = stop, stop = step; + step = i0, i0 = i1, i1 = step; + } + + while (maxIter-- > 0) { + step = tickIncrement(start, stop, count); + if (step === prestep) { + d[i0] = start; + d[i1] = stop; + return domain(d); + } else if (step > 0) { + start = Math.floor(start / step) * step; + stop = Math.ceil(stop / step) * step; + } else if (step < 0) { + start = Math.ceil(start * step) / step; + stop = Math.floor(stop * step) / step; + } else { + break; + } + prestep = step; + } + + return scale; + }; + + return scale; + } + + function linear() { + var scale = continuous(); + + scale.copy = function() { + return copy$1(scale, linear()); + }; + + initRange.apply(scale, arguments); + + return linearish(scale); + } + + function transformer() { + var x0 = 0, + x1 = 1, + t0, + t1, + k10, + transform, + interpolator = identity$1, + clamp = false, + unknown; + + function scale(x) { + return x == null || isNaN(x = +x) ? unknown : interpolator(k10 === 0 ? 0.5 : (x = (transform(x) - t0) * k10, clamp ? Math.max(0, Math.min(1, x)) : x)); + } + + scale.domain = function(_) { + return arguments.length ? ([x0, x1] = _, t0 = transform(x0 = +x0), t1 = transform(x1 = +x1), k10 = t0 === t1 ? 0 : 1 / (t1 - t0), scale) : [x0, x1]; + }; + + scale.clamp = function(_) { + return arguments.length ? (clamp = !!_, scale) : clamp; + }; + + scale.interpolator = function(_) { + return arguments.length ? (interpolator = _, scale) : interpolator; + }; + + function range(interpolate) { + return function(_) { + var r0, r1; + return arguments.length ? ([r0, r1] = _, interpolator = interpolate(r0, r1), scale) : [interpolator(0), interpolator(1)]; + }; + } + + scale.range = range(interpolate); + + scale.rangeRound = range(interpolateRound); + + scale.unknown = function(_) { + return arguments.length ? (unknown = _, scale) : unknown; + }; + + return function(t) { + transform = t, t0 = t(x0), t1 = t(x1), k10 = t0 === t1 ? 0 : 1 / (t1 - t0); + return scale; + }; + } + + function copy(source, target) { + return target + .domain(source.domain()) + .interpolator(source.interpolator()) + .clamp(source.clamp()) + .unknown(source.unknown()); + } + + function sequential() { + var scale = linearish(transformer()(identity$1)); + + scale.copy = function() { + return copy(scale, sequential()); + }; + + return initInterpolator.apply(scale, arguments); + } + + const COLOR_BASE = "#cecece"; + + // https://www.w3.org/TR/WCAG20/#relativeluminancedef + const rc = 0.2126; + const gc = 0.7152; + const bc = 0.0722; + // low-gamma adjust coefficient + const lowc = 1 / 12.92; + function adjustGamma(p) { + return Math.pow((p + 0.055) / 1.055, 2.4); + } + function relativeLuminance(o) { + const rsrgb = o.r / 255; + const gsrgb = o.g / 255; + const bsrgb = o.b / 255; + const r = rsrgb <= 0.03928 ? rsrgb * lowc : adjustGamma(rsrgb); + const g = gsrgb <= 0.03928 ? gsrgb * lowc : adjustGamma(gsrgb); + const b = bsrgb <= 0.03928 ? bsrgb * lowc : adjustGamma(bsrgb); + return r * rc + g * gc + b * bc; + } + const createRainbowColor = (root) => { + const colorParentMap = new Map(); + colorParentMap.set(root, COLOR_BASE); + if (root.children != null) { + const colorScale = sequential([0, root.children.length], (n) => hsl(360 * n, 0.3, 0.85)); + root.children.forEach((c, id) => { + colorParentMap.set(c, colorScale(id).toString()); + }); + } + const colorMap = new Map(); + const lightScale = linear().domain([0, root.height]).range([0.9, 0.3]); + const getBackgroundColor = (node) => { + const parents = node.ancestors(); + const colorStr = parents.length === 1 + ? colorParentMap.get(parents[0]) + : colorParentMap.get(parents[parents.length - 2]); + const hslColor = hsl(colorStr); + hslColor.l = lightScale(node.depth); + return hslColor; + }; + return (node) => { + if (!colorMap.has(node)) { + const backgroundColor = getBackgroundColor(node); + const l = relativeLuminance(backgroundColor.rgb()); + const fontColor = l > 0.19 ? "#000" : "#fff"; + colorMap.set(node, { + backgroundColor: backgroundColor.toString(), + fontColor, + }); + } + return colorMap.get(node); + }; + }; + + const StaticContext = F$1({}); + const drawChart = (parentNode, data, width, height) => { + const availableSizeProperties = getAvailableSizeOptions(data.options); + console.time("layout create"); + const layout = treemap() + .size([width, height]) + .paddingOuter(PADDING) + .paddingTop(TOP_PADDING) + .paddingInner(PADDING) + .round(true) + .tile(treemapResquarify); + console.timeEnd("layout create"); + console.time("rawHierarchy create"); + const rawHierarchy = hierarchy(data.tree); + console.timeEnd("rawHierarchy create"); + const nodeSizesCache = new Map(); + const nodeIdsCache = new Map(); + const getModuleSize = (node, sizeKey) => { var _a, _b; return (_b = (_a = nodeSizesCache.get(node)) === null || _a === void 0 ? void 0 : _a[sizeKey]) !== null && _b !== void 0 ? _b : 0; }; + console.time("rawHierarchy eachAfter cache"); + rawHierarchy.eachAfter((node) => { + var _a; + const nodeData = node.data; + nodeIdsCache.set(nodeData, { + nodeUid: generateUniqueId("node"), + clipUid: generateUniqueId("clip"), + }); + const sizes = { renderedLength: 0, gzipLength: 0, brotliLength: 0 }; + if (isModuleTree(nodeData)) { + for (const sizeKey of availableSizeProperties) { + sizes[sizeKey] = nodeData.children.reduce((acc, child) => getModuleSize(child, sizeKey) + acc, 0); + } + } + else { + for (const sizeKey of availableSizeProperties) { + sizes[sizeKey] = (_a = data.nodeParts[nodeData.uid][sizeKey]) !== null && _a !== void 0 ? _a : 0; + } + } + nodeSizesCache.set(nodeData, sizes); + }); + console.timeEnd("rawHierarchy eachAfter cache"); + const getModuleIds = (node) => nodeIdsCache.get(node); + console.time("color"); + const getModuleColor = createRainbowColor(rawHierarchy); + console.timeEnd("color"); + q$1(u$1(StaticContext.Provider, { value: { + data, + availableSizeProperties, + width, + height, + getModuleSize, + getModuleIds, + getModuleColor, + rawHierarchy, + layout, + }, children: u$1(Main, {}) }), parentNode); + }; + + exports.StaticContext = StaticContext; + exports.default = drawChart; + + Object.defineProperty(exports, '__esModule', { value: true }); + + return exports; + +})({}); + + /*-->*/ + + + + diff --git a/packages/provider/src/tasks/powCaptcha/powTasks.ts b/packages/provider/src/tasks/powCaptcha/powTasks.ts index 43a139d68f..0c2cf62a73 100644 --- a/packages/provider/src/tasks/powCaptcha/powTasks.ts +++ b/packages/provider/src/tasks/powCaptcha/powTasks.ts @@ -12,11 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. import { u8aToHex } from '@polkadot/util' -import { PoWCaptcha } from '@prosopo/types' -import { Database } from '@prosopo/types-database' +import type { PoWCaptcha } from '@prosopo/types' +import type { Database } from '@prosopo/types-database' import { ProsopoEnvError } from '@prosopo/common' import { stringToHex } from '@polkadot/util' -import { KeyringPair } from '@polkadot/keyring/types' +import type { KeyringPair } from '@polkadot/keyring/types' import { checkRecentPowSolution, checkPowSignature, checkPowSolution } from './powTasksUtils.js' export class PowCaptchaManager { diff --git a/packages/provider/src/tasks/tasks.ts b/packages/provider/src/tasks/tasks.ts index 17d32109eb..225a1c85bb 100644 --- a/packages/provider/src/tasks/tasks.ts +++ b/packages/provider/src/tasks/tasks.ts @@ -35,7 +35,7 @@ export class Tasks { constructor(env: ProviderEnvironment) { this.config = env.config - this.db = env.db as Database + this.db = env.getDb() this.captchaConfig = env.config.captchas this.logger = getLogger(env.config.logLevel, 'Tasks') if (!env.pair) { diff --git a/packages/types-env/src/env.ts b/packages/types-env/src/env.ts index 73717cead1..05953195fc 100644 --- a/packages/types-env/src/env.ts +++ b/packages/types-env/src/env.ts @@ -11,13 +11,12 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -import { ApiPromise } from '@polkadot/api/promise/Api' -import { AssetsResolver, EnvironmentTypes, NetworkNames } from '@prosopo/types' -import { Database } from '@prosopo/types-database' -import { Keyring } from '@polkadot/keyring' -import { KeyringPair } from '@polkadot/keyring/types' -import { Logger } from '@prosopo/common' -import { ProsopoBasicConfigOutput } from '@prosopo/types' +import type { AssetsResolver, EnvironmentTypes, NetworkNames } from '@prosopo/types' +import type { Database } from '@prosopo/types-database' +import type { Keyring } from '@polkadot/keyring' +import type { KeyringPair } from '@polkadot/keyring/types' +import type { Logger } from '@prosopo/common' +import type { ProsopoBasicConfigOutput } from '@prosopo/types' export interface ProsopoEnvironment { config: ProsopoBasicConfigOutput @@ -30,6 +29,7 @@ export interface ProsopoEnvironment { assetsResolver: AssetsResolver | undefined keyring: Keyring pair: KeyringPair | undefined + getDb(): Database isReady(): Promise importDatabase(): Promise }