Skip to content

Commit

Permalink
move staticCacheDir to InstanceSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
netroy committed Oct 30, 2023
1 parent c8da7d1 commit 609156b
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 15 deletions.
9 changes: 5 additions & 4 deletions packages/cli/src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
LoadMappingOptions,
LoadNodeParameterOptions,
LoadNodeListSearch,
InstanceSettings,
} from 'n8n-core';

import type {
Expand Down Expand Up @@ -59,7 +60,6 @@ import { getSharedWorkflowIds } from '@/WorkflowHelpers';
import { workflowsController } from '@/workflows/workflows.controller';
import {
EDITOR_UI_DIST_DIR,
GENERATED_STATIC_DIR,
inDevelopment,
inE2ETests,
N8N_VERSION,
Expand Down Expand Up @@ -1229,11 +1229,12 @@ export class Server extends AbstractServer {
);
}

const { staticCacheDir } = Container.get(InstanceSettings);
if (frontendService) {
const staticOptions: ServeStaticOptions = {
cacheControl: false,
setHeaders: (res: express.Response, path: string) => {
const isIndex = path === pathJoin(GENERATED_STATIC_DIR, 'index.html');
const isIndex = path === pathJoin(staticCacheDir, 'index.html');
const cacheControl = isIndex
? 'no-cache, no-store, must-revalidate'
: 'max-age=86400, immutable';
Expand All @@ -1259,7 +1260,7 @@ export class Server extends AbstractServer {

this.app.use(
'/',
express.static(GENERATED_STATIC_DIR),
express.static(staticCacheDir),
express.static(EDITOR_UI_DIST_DIR, staticOptions),
);

Expand All @@ -1269,7 +1270,7 @@ export class Server extends AbstractServer {
next();
});
} else {
this.app.use('/', express.static(GENERATED_STATIC_DIR));
this.app.use('/', express.static(staticCacheDir));
}
}

Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { ActiveWorkflowRunner } from '@/ActiveWorkflowRunner';
import * as Db from '@/Db';
import * as GenericHelpers from '@/GenericHelpers';
import { Server } from '@/Server';
import { EDITOR_UI_DIST_DIR, GENERATED_STATIC_DIR } from '@/constants';
import { EDITOR_UI_DIST_DIR } from '@/constants';
import { eventBus } from '@/eventbus';
import { BaseCommand } from './BaseCommand';
import { InternalHooks } from '@/InternalHooks';
Expand Down Expand Up @@ -155,10 +155,11 @@ export class Start extends BaseCommand {
}

const closingTitleTag = '</title>';
const { staticCacheDir } = this.instanceSettings;
const compileFile = async (fileName: string) => {
const filePath = path.join(EDITOR_UI_DIST_DIR, fileName);
if (/(index\.html)|.*\.(js|css)/.test(filePath) && existsSync(filePath)) {
const destFile = path.join(GENERATED_STATIC_DIR, fileName);
const destFile = path.join(staticCacheDir, fileName);
await mkdir(path.dirname(destFile), { recursive: true });
const streams = [
createReadStream(filePath, 'utf-8'),
Expand Down
6 changes: 0 additions & 6 deletions packages/cli/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { readFileSync } from 'fs';
import { resolve, join, dirname } from 'path';
import { Container } from 'typedi';
import type { n8n } from 'n8n-core';
import { InstanceSettings } from 'n8n-core';
import { jsonParse } from 'n8n-workflow';

const { NODE_ENV, E2E_TESTS } = process.env;
Expand All @@ -17,10 +15,6 @@ export const CUSTOM_API_CALL_KEY = '__CUSTOM_API_CALL__';
export const CLI_DIR = resolve(__dirname, '..');
export const TEMPLATES_DIR = join(CLI_DIR, 'templates');
export const NODES_BASE_DIR = dirname(require.resolve('n8n-nodes-base'));
export const GENERATED_STATIC_DIR = join(
Container.get(InstanceSettings).userHome,
'.cache/n8n/public',
);
export const EDITOR_UI_DIST_DIR = join(dirname(require.resolve('n8n-editor-ui')), 'dist');

export function getN8nPackageJson() {
Expand Down
8 changes: 5 additions & 3 deletions packages/cli/src/services/frontend.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
} from 'n8n-workflow';
import { InstanceSettings } from 'n8n-core';

import { GENERATED_STATIC_DIR, LICENSE_FEATURES } from '@/constants';
import { LICENSE_FEATURES } from '@/constants';
import { CredentialsOverwrites } from '@/CredentialsOverwrites';
import { CredentialTypes } from '@/CredentialTypes';
import { LoadNodesAndCredentials } from '@/LoadNodesAndCredentials';
Expand Down Expand Up @@ -205,8 +205,9 @@ export class FrontendService {
async generateTypes() {
this.overwriteCredentialsProperties();

const { staticCacheDir } = this.instanceSettings;
// pre-render all the node and credential types as static json files
await mkdir(path.join(GENERATED_STATIC_DIR, 'types'), { recursive: true });
await mkdir(path.join(staticCacheDir, 'types'), { recursive: true });
const { credentials, nodes } = this.loadNodesAndCredentials.types;
this.writeStaticJSON('nodes', nodes);
this.writeStaticJSON('credentials', credentials);
Expand Down Expand Up @@ -303,7 +304,8 @@ export class FrontendService {
}

private writeStaticJSON(name: string, data: INodeTypeBaseDescription[] | ICredentialType[]) {
const filePath = path.join(GENERATED_STATIC_DIR, `types/${name}.json`);
const { staticCacheDir } = this.instanceSettings;
const filePath = path.join(staticCacheDir, `types/${name}.json`);
const stream = createWriteStream(filePath, 'utf-8');
stream.write('[\n');
data.forEach((entry, index) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/InstanceSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export class InstanceSettings {
/** The path to the n8n folder in which all n8n related data gets saved */
readonly n8nFolder = path.join(this.userHome, '.n8n');

readonly staticCacheDir = path.join(this.userHome, '.cache/n8n/public');

/** The path to the folder containing custom nodes and credentials */
readonly customExtensionDir = path.join(this.n8nFolder, 'custom');

Expand Down

0 comments on commit 609156b

Please sign in to comment.