Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes 1225 #1230

Merged
merged 4 commits into from
Nov 6, 2018
Merged
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
3 changes: 2 additions & 1 deletion packages/insomnia-app/app/common/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as packageJSON from '../../package.json';
import * as electron from 'electron';
import path from 'path';
import mkdirp from 'mkdirp';
import { getDataDirectory } from './misc';

// App Stuff

Expand Down Expand Up @@ -67,7 +68,7 @@ export const HUGE_RESPONSE_MB = 100;
export const FLEXIBLE_URL_REGEX = /^(http|https):\/\/[\wàâäèéêëîïôóœùûüÿçÀÂÄÈÉÊËÎÏÔŒÙÛÜŸÇ\-_.]+[/\wàâäèéêëîïôóœùûüÿçÀÂÄÈÉÊËÎÏÔŒÙÛÜŸÇ.\-+=:\][@%^*&!#?;$]*/;
export const CHECK_FOR_UPDATES_INTERVAL = 1000 * 60 * 60 * 3; // 3 hours
export const PLUGIN_PATH = path.join(
(electron.remote || electron).app.getPath('userData'),
getDataDirectory(),
'plugins'
);

Expand Down
5 changes: 2 additions & 3 deletions packages/insomnia-app/app/common/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import NeDB from 'nedb';
import fsPath from 'path';
import { DB_PERSIST_INTERVAL } from './constants';
import uuid from 'uuid';
import { getDataDirectory } from './misc';

export const CHANGE_INSERT = 'insert';
export const CHANGE_UPDATE = 'update';
Expand All @@ -26,9 +27,7 @@ function allTypes() {

function getDBFilePath(modelType) {
// NOTE: Do not EVER change this. EVER!
const { app } = electron.remote || electron;
const basePath = app.getPath('userData');
return fsPath.join(basePath, `insomnia.${modelType}.db`);
return fsPath.join(getDataDirectory(), `insomnia.${modelType}.db`);
}

export async function initClient() {
Expand Down
5 changes: 5 additions & 0 deletions packages/insomnia-app/app/common/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,8 @@ export async function waitForStreamToFinish(s: Readable | Writable): Promise<voi
});
});
}

export function getDataDirectory(): string {
const { app } = electron.remote || electron;
return process.env.INSOMNIA_DATA_PATH || app.getPath('userData');
}
4 changes: 2 additions & 2 deletions packages/insomnia-app/app/main/window-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export function createWindow() {
{
label: 'Show App Data Folder',
click: (menuItem, window, e) => {
const directory = app.getPath('userData');
const directory = misc.getDataDirectory();
shell.showItemInFolder(directory);
}
},
Expand Down Expand Up @@ -413,7 +413,7 @@ function getZoomFactor() {
}

function initLocalStorage() {
const localStoragePath = path.join(app.getPath('userData'), 'localStorage');
const localStoragePath = path.join(misc.getDataDirectory(), 'localStorage');
localStorage = new LocalStorage(localStoragePath);
}

Expand Down
10 changes: 5 additions & 5 deletions packages/insomnia-app/app/models/__tests__/response.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import path from 'path';
import zlib from 'zlib';
import fs from 'fs';
import * as electron from 'electron';
import * as models from '../../models';
import { globalBeforeEach } from '../../__jest__/before-each';
import { getDataDirectory } from '../../common/misc';

describe('migrate()', () => {
beforeEach(async () => {
Expand All @@ -17,7 +17,7 @@ describe('migrate()', () => {

const newModel = await models.initModel(models.response.type, initialModel);
const expectedBodyPath = path.join(
electron.remote.app.getPath('userData'),
getDataDirectory(),
`responses/fc3ff98e8c6a0d3087d515c0473f8677.zip`
);
const storedBody = models.response.getBodyBuffer(newModel);
Expand All @@ -37,7 +37,7 @@ describe('migrate()', () => {
const newModel = await models.initModel(models.response.type, initialModel);
jest.runAllTimers();
const expectedBodyPath = path.join(
electron.remote.app.getPath('userData'),
getDataDirectory(),
`responses/fc3ff98e8c6a0d3087d515c0473f8677.zip`
);
const storedBody = models.response.getBodyBuffer(newModel);
Expand All @@ -59,7 +59,7 @@ describe('migrate()', () => {
jest.runAllTimers();

const expectedBodyPath = path.join(
electron.remote.app.getPath('userData'),
getDataDirectory(),
'responses/d41d8cd98f00b204e9800998ecf8427e.zip'
);
const storedBody = models.response.getBodyBuffer(newModel);
Expand Down Expand Up @@ -87,7 +87,7 @@ describe('migrate()', () => {
});

it('does it', async () => {
const bodyPath = path.join(electron.remote.app.getPath('userData'), 'foo.zip');
const bodyPath = path.join(getDataDirectory(), 'foo.zip');
fs.writeFileSync(bodyPath, zlib.gzipSync('Hello World!'));

const response = await models.initModel(models.response.type, { bodyPath });
Expand Down
5 changes: 2 additions & 3 deletions packages/insomnia-app/app/models/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import crypto from 'crypto';
import path from 'path';
import zlib from 'zlib';
import mkdirp from 'mkdirp';
import * as electron from 'electron';
import { MAX_RESPONSES } from '../common/constants';
import * as db from '../common/database';
import { getDataDirectory } from '../common/misc';

export const name = 'Response';
export const type = 'Response';
Expand Down Expand Up @@ -201,8 +201,7 @@ function getBodyBufferFromPath<T>(
async function migrateBodyToFileSystem(doc: Object) {
if (doc.hasOwnProperty('body') && doc._id && !doc.bodyPath) {
const bodyBuffer = Buffer.from(doc.body, doc.encoding || 'utf8');
const { app } = electron.remote || electron;
const root = app.getPath('userData');
const root = getDataDirectory();
const dir = path.join(root, 'responses');

mkdirp.sync(dir);
Expand Down
8 changes: 3 additions & 5 deletions packages/insomnia-app/app/network/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { parse as urlParse, resolve as urlResolve } from 'url';
import { Curl } from 'insomnia-libcurl';
import { join as pathJoin } from 'path';
import uuid from 'uuid';
import * as electron from 'electron';
import * as models from '../models';
import {
AUTH_AWS_IAM,
Expand All @@ -37,7 +36,8 @@ import {
hasAuthHeader,
hasContentTypeHeader,
hasUserAgentHeader,
waitForStreamToFinish
waitForStreamToFinish,
getDataDirectory
} from '../common/misc';
import {
buildQueryStringFromParams,
Expand All @@ -56,8 +56,6 @@ import { urlMatchesCertHost } from './url-matches-cert-host';
import aws4 from 'aws4';
import { buildMultipart } from './multipart';

const { app } = electron.remote || electron;

export type ResponsePatch = {
statusMessage?: string,
error?: string,
Expand Down Expand Up @@ -641,7 +639,7 @@ export async function _actuallySend(
setOpt(Curl.option.HTTPHEADER, headerStrings);

let responseBodyBytes = 0;
const responsesDir = pathJoin(app.getPath('userData'), 'responses');
const responsesDir = pathJoin(getDataDirectory(), 'responses');
mkdirp.sync(responsesDir);
const responseBodyPath = pathJoin(responsesDir, uuid.v4() + '.response');
const responseBodyWriteStream = fs.createWriteStream(responseBodyPath);
Expand Down
3 changes: 1 addition & 2 deletions packages/insomnia-app/app/sync/storage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import electron from 'electron';
import NeDB from 'nedb';
import fsPath from 'path';
import crypto from 'crypto';
Expand Down Expand Up @@ -198,7 +197,7 @@ function _initConfig(data) {

export function initDB(config, forceReset) {
if (!_database || forceReset) {
const basePath = electron.remote.app.getPath('userData');
const basePath = util.getDataDirectory();
_database = {};

// NOTE: Do not EVER change this. EVER!
Expand Down