Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ export const SENTRY_DSN =
'https://966a5b01ac8d4941b81e4ebd0ab4c991@sentry.io/1882540';

export const ELECTRON_DTS = 'electron.d.ts';

// These are the limits GitHub enforces for gist sizes.
// We use these to fail fast locally when creating/updating a new gist.
export const GIST_MAX_FILE_SIZE = 10 * 1024 * 1024; // 10 MB per file
export const GIST_MAX_FILE_COUNT = 300;
15 changes: 14 additions & 1 deletion src/main/constants.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import * as fs from 'node:fs';
import * as path from 'node:path';

import { app } from 'electron';

export const STATIC_DIR = path.resolve(__dirname, '../static');
// Find the root dir for static assets (eg `show-me/`, `electron-quick-start/`).
// In production, the bundled main script lives in `.webpack/main/` and webpack
// copies static assets to `.webpack/static/`.
// In tests (vitest loads the source TypeScript directly), `__dirname` is
// `src/main/` and the static folder lives at the repository root.
function resolveStaticDir(): string {
const paths = ['../static', '../../static'].map((p) =>
path.resolve(__dirname, p),
);
return paths.find(fs.existsSync) ?? paths[0];
}

export const STATIC_DIR = resolveStaticDir();

export const ELECTRON_DOWNLOAD_PATH = path.join(
app.getPath('userData'),
Expand Down
13 changes: 7 additions & 6 deletions src/main/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { IpcMainInvokeEvent, app, safeStorage } from 'electron';

import { getTemplate } from './content';
import { ipcMainManager } from './ipc';
import { GIST_MAX_FILE_COUNT, GIST_MAX_FILE_SIZE } from '../constants';
import { EditorValues, GistLoadResult, GistRevision } from '../interfaces';
import { IpcEvents } from '../ipc-events';
import { isSupportedFile } from '../utils/editor-utils';
Expand All @@ -25,10 +26,6 @@ const SHA_PATTERN = /^[0-9a-f]{40}$/;

const MAX_DESCRIPTION_LENGTH = 256;

const MAX_FILE_SIZE = 10 * 1024 * 1024; // 10 MB per file — GitHub's gist limit

const MAX_FILE_COUNT = 300; // GitHub's gist file limit

function isValidToken(token: unknown): token is string {
return typeof token === 'string' && TOKEN_PATTERN.test(token);
}
Expand Down Expand Up @@ -62,7 +59,8 @@ function areValidGistFiles(

const entries = Object.entries(files as Record<string, unknown>);

if (entries.length === 0 || entries.length > MAX_FILE_COUNT) return false;
if (entries.length === 0 || entries.length > GIST_MAX_FILE_COUNT)
return false;

for (const [key, value] of entries) {
// null entries are used to delete files during update
Expand All @@ -75,7 +73,7 @@ function areValidGistFiles(
if (filename.length === 0) return false;
if (filename !== key) return false;
if (typeof content !== 'string') return false;
if (content.length > MAX_FILE_SIZE) return false;
if (content.length > GIST_MAX_FILE_SIZE) return false;
}

return true;
Expand Down Expand Up @@ -437,6 +435,7 @@ export function setupGitHub() {
// Exported for testing
export const testing = {
fetchExample,
getCredentialsPath,
handleGistCreate,
handleGistDelete,
handleGistListCommits,
Expand All @@ -445,4 +444,6 @@ export const testing = {
handleTokenCheckAuth,
handleTokenSignIn,
handleTokenSignOut,
loadToken,
saveToken,
};
3 changes: 0 additions & 3 deletions tests/main/content.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ import { getTemplate, getTestTemplate } from '../../src/main/content';
import { isReleasedMajor } from '../../src/main/versions';

vi.unmock('fs-extra');
vi.mock('../../src/main/constants', () => ({
STATIC_DIR: path.join(__dirname, '../../static'),
}));
vi.mock('../../src/main/versions', () => ({
isReleasedMajor: vi.fn(),
}));
Expand Down
Loading
Loading