@forge/cli
14.1.0-next.1-experimental-13c2f8d14.1.0-next.21-experimental-554d417
out/service/local-storage-service.js−
out/service/local-storage-service.jsDeleted−204
Index: package/out/service/local-storage-service.js
===================================================================
--- package/out/service/local-storage-service.js
+++ package/out/service/local-storage-service.js
@@ -1,204 +0,0 @@
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-exports.LocalStorageService = exports.LocalStorageApiError = exports.LocalStorageConfigurationError = void 0;
-const tslib_1 = require("tslib");
-const fs_1 = tslib_1.__importDefault(require("fs"));
-const cli_shared_1 = require("@forge/cli-shared");
-const local_storage_manifest_1 = require("./local-storage-manifest");
-class LocalStorageConfigurationError extends cli_shared_1.UserError {
-}
-exports.LocalStorageConfigurationError = LocalStorageConfigurationError;
-class LocalStorageApiError extends cli_shared_1.UserError {
- operationIndex;
- constructor(message, operationIndex) {
- super(message);
- this.operationIndex = operationIndex;
- }
-}
-exports.LocalStorageApiError = LocalStorageApiError;
-class LocalStorageService {
- configFile;
- lifecycle;
- appDirectory;
- fetchClient;
- constructor(configFile, lifecycle, fetchClient = fetch, appDirectory = process.cwd()) {
- this.configFile = configFile;
- this.lifecycle = lifecycle;
- this.appDirectory = appDirectory;
- this.fetchClient = fetchClient;
- }
- async manifest() {
- return (0, local_storage_manifest_1.inspectLocalStorageManifest)(await this.configFile.readConfig(), this.appDirectory);
- }
- async start() {
- const manifest = await this.manifest();
- await this.ensureAvailable(manifest, (0, local_storage_manifest_1.configuredOfferings)(manifest));
- return manifest;
- }
- async status() {
- const manifest = await this.manifest();
- const empty = { state: 'stopped', healthy: false };
- const offerings = (0, local_storage_manifest_1.configuredOfferings)(manifest);
- const discovered = offerings.length ? await this.lifecycle.status() : undefined;
- const runtime = Object.fromEntries(['kvs', 'sql'].map((offering) => [
- offering,
- manifest.configured[offering] && discovered ? discovered[offering] : empty
- ]));
- return { manifest, runtime };
- }
- async stop() {
- await this.lifecycle.stop();
- }
- async seed(options) {
- const manifest = await this.manifest();
- const requested = [options.kvsPath && 'kvs', options.sqlPath && 'sql'].filter(Boolean);
- if (!requested.length)
- return [];
- this.assertConfigured(manifest, requested);
- const kvsSeed = options.kvsPath ? fs_1.default.readFileSync(options.kvsPath, 'utf8') : undefined;
- const sqlSeed = options.sqlPath ? fs_1.default.readFileSync(options.sqlPath, 'utf8') : undefined;
- await this.ensureAvailable(manifest, requested);
- const tasks = [];
- if (kvsSeed !== undefined)
- tasks.push(this.seedKvs(manifest, kvsSeed));
- if (sqlSeed !== undefined)
- tasks.push(this.seedSql(manifest, sqlSeed));
- return Promise.all(tasks);
- }
- async reset(options) {
- if (options.kvs && options.sql) {
- throw new LocalStorageConfigurationError('Use bare reset to clear every configured local-storage offering.');
- }
- const manifest = await this.manifest();
- const explicit = options.kvs ? ['kvs'] : options.sql ? ['sql'] : undefined;
- const requested = explicit ?? (0, local_storage_manifest_1.configuredOfferings)(manifest);
- this.assertConfigured(manifest, requested);
- await this.ensureAvailable(manifest, requested);
- return Promise.all(requested.map((offering) => this.resetOffering(manifest, offering)));
- }
- async startForTunnel() {
- const manifest = await this.start();
- const environment = (0, local_storage_manifest_1.localStorageEnvironment)(manifest.namespace);
- for (const [key, value] of Object.entries(environment))
- process.env[key] = value;
- return manifest;
- }
- async stopForTunnel() {
- try {
- await this.stop();
- }
- finally {
- for (const key of Object.keys((0, local_storage_manifest_1.localStorageEnvironment)({ appId: '', environmentId: '', installationId: '' }))) {
- delete process.env[key];
- }
- }
- }
- async ensureAvailable(manifest, offerings) {
- if (!offerings.length)
- return;
- try {
- await this.lifecycle.start(offerings);
- await Promise.all(offerings.map((offering) => this.bootstrap(manifest, offering)));
- }
- catch (error) {
- try {
- await this.lifecycle.stop();
- }
- catch { }
- throw error;
- }
- }
- assertConfigured(manifest, offerings) {
- for (const offering of offerings) {
- if (!manifest.configured[offering]) {
- const declaration = offering === 'kvs' ? 'the storage:app permission scope' : 'a modules.sql entry';
- throw new LocalStorageConfigurationError(`${offering.toUpperCase()} is not configured; add ${declaration} to manifest.yml.`);
- }
- }
- }
- async bootstrap(manifest, offering) {
- if (offering === 'kvs') {
- await this.post('http://127.0.0.1:18090/_local/v1/bootstrap', { entities: manifest.entities }, manifest);
- }
- else {
- await this.post('http://127.0.0.1:18091/_local/v1/bootstrap', {}, manifest);
- }
- }
- async seedKvs(manifest, seed) {
- try {
- const result = await this.postRaw('http://127.0.0.1:18090/_local/v1/seed', seed, manifest);
- return {
- offering: 'kvs',
- ok: true,
- records: typeof result.recordsSeeded === 'number' ? result.recordsSeeded : undefined
- };
- }
- catch (error) {
- return { offering: 'kvs', ok: false, error: error };
- }
- }
- async seedSql(manifest, sql) {
- try {
- const result = await this.post('http://127.0.0.1:18091/_local/v1/seed', { sql }, manifest);
- return {
- offering: 'sql',
- ok: true,
- statements: typeof result.statementsExecuted === 'number' ? result.statementsExecuted : undefined
- };
- }
- catch (error) {
- return { offering: 'sql', ok: false, error: error };
- }
- }
- async resetOffering(manifest, offering) {
- const endpoint = offering === 'kvs' ? 'http://127.0.0.1:18090/_local/v1/reset' : 'http://127.0.0.1:18091/_local/v1/reset';
- try {
- const result = await this.post(endpoint, {}, manifest);
- return {
- offering,
- ok: true,
- removed: typeof result.recordsRemoved === 'number' ? result.recordsRemoved : undefined
- };
- }
- catch (error) {
- return { offering, ok: false, error: error };
- }
- }
- async post(url, body, manifest) {
- return this.postRaw(url, JSON.stringify(body), manifest);
- }
- async postRaw(url, body, manifest) {
- let response;
- try {
- response = await this.fetchClient(url, {
- method: 'POST',
- headers: (0, local_storage_manifest_1.localStorageHeaders)(manifest.namespace),
- body
- });
- }
- catch (error) {
- throw new LocalStorageApiError(`Local storage request failed: ${error.message}`);
- }
- const text = await response.text();
- let parsed = {};
- if (text) {
- try {
- parsed = JSON.parse(text);
- }
- catch {
- throw new LocalStorageApiError(`Local storage returned invalid JSON (HTTP ${response.status}): ${text}`);
- }
- }
- if (!response.ok) {
- const debug = isObject(parsed.debug) ? parsed.debug : {};
- const operationIndex = typeof debug.errorOperationIndex === 'number' ? debug.errorOperationIndex : undefined;
- const detail = typeof parsed.message === 'string' ? parsed.message : text || response.statusText;
- const location = typeof parsed.location === 'string' ? `${parsed.location}: ` : '';
- throw new LocalStorageApiError(`Local storage returned HTTP ${response.status}: ${location}${detail}`, operationIndex);
- }
- return parsed;
- }
-}
-exports.LocalStorageService = LocalStorageService;
-const isObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
-//# sourceMappingURL=local-storage-service.js.map
\ No newline at end of file