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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@
"@types/node-fetch": "^2.5.12",
"@types/proper-lockfile": "^4.1.4",
"@types/run-parallel": "^1.1.2",
"@types/sql.js": "^1.4.9",
"@types/string-similarity": "^4.0.0",
"cors": "^2.8.5",
"current-git-branch": "^1.1.0",
Expand All @@ -333,6 +334,7 @@
"node-fetch": "^3.3.2",
"proper-lockfile": "^4.1.2",
"run-parallel": "^1.2.0",
"sql.js": "^1.10.3",
"string-similarity": "^4.0.4",
"untildify": "^5.0.0",
"websocket": "^1.0.34"
Expand Down
54 changes: 54 additions & 0 deletions src/database/sqlite_connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import fs from 'fs';
import path from 'path';
import initSqlJs, { Database } from 'sql.js';
import { generateSettings } from '../settings';

class SQLiteConnection {
filebuffer: any;
db: any;

constructor() {
try {
const settings = generateSettings();
if (!fs.existsSync(settings.DATABASE_FILE_PATH)) {
fs.openSync(settings.DATABASE_FILE_PATH, "w+");
}
this.filebuffer = fs.readFileSync(settings.DATABASE_FILE_PATH);

} catch (err) {
console.error("Error initializing SQLite database:", err);
}
}

async setup() {
const SQL = await initSqlJs({
// Required to load the wasm binary asynchronously. Of course, you can host it wherever you want
// You can omit locateFile completely when running in node
locateFile: file => path.join(__dirname, ".." , `node_modules/sql.js/dist/${file}`)
});
this.db = new SQL.Database(this.filebuffer);
}

// static getInstance() {
// if (!SQLiteConnection.instance) {
// SQLiteConnection.instance = new SQLiteConnection();
// }
// return SQLiteConnection.instance;
// }

getDatabase() {
return this.db;
}

disconnect() {
try {
if (this.db) {
this.db.close();
}
} catch (err) {
console.error("Error disconnecting SQLite database:", err);
}
}
}

module.exports = SQLiteConnection;
1 change: 1 addition & 0 deletions src/init/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export class initUtils {
const filePathAndURLs = uploadResponse.urls;
const uploaderUtils = new s3UploaderUtils();
uploaderUtils.saveURLs(this.repoPath, branch, filePathAndURLs);
CodeSyncLogger.debug(`Saved s3 URLs, branch=${branch} repo=${this.repoPath}`);
// Reset state values
CodeSyncState.set(syncingBranchKey, false);
CodeSyncState.set(CODESYNC_STATES.IS_SYNCING_BRANCH, false);
Expand Down
8 changes: 4 additions & 4 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { CodeSyncLogger } from "../logger";
import { CODESYNC_STATES, CodeSyncState } from "../utils/state_utils";
import { createUserWithApi } from "../utils/api_utils";
import { UserState } from "../utils/user_utils";
import { createRedirectUri } from "../utils/url_utils";
import { WEB_APP_URL } from "../settings";

export const initExpressServer = () => {
const msgs = {
Expand All @@ -36,20 +36,20 @@ export const initExpressServer = () => {

// define a route handler for the authorization callback
expressApp.get(Auth0URLs.LOGIN_CALLBACK_PATH, async (req: any, res: any) => {
if (!req.query.access_token || !req.query.id_token) return;
try {
await createUser(req.query.access_token, req.query.id_token);
} catch (e) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
CodeSyncLogger.critical("Login callback failed", e.stack);
}
res.send("OK");
res.redirect(WEB_APP_URL);
});

expressApp.get(Auth0URLs.LOGOUT_CALLBACK_PATH, async (req: any, res: any) => {
postSuccessLogout();
const login_callback = createRedirectUri(Auth0URLs.LOGIN_CALLBACK_PATH);
res.send(JSON.stringify({login_callback}));
res.redirect(WEB_APP_URL);
});

// define a route handler for the default home page
Expand Down
5 changes: 4 additions & 1 deletion src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,15 @@ export const generateSettings = () => {
ALERTS: path.join(rootRepo, "alerts.yml")
};

const DATABASE_FILE_PATH = path.join(rootRepo, "codesync-v1-vscode.db");

return {
...systemDirectories,
...lockFiles,
...alerts,
deprecatedFiles: [
systemDirectories.SEQUENCE_TOKEN_PATH
]
],
DATABASE_FILE_PATH
};
};
2 changes: 1 addition & 1 deletion src/utils/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const showSignUpButtons = () => {
NOTIFICATION.WELCOME_MSG, ...[
NOTIFICATION.LOGIN,
NOTIFICATION.IGNORE
]).then(async selection => {
]).then(selection => {
if (selection === NOTIFICATION.LOGIN) {
authHandler();
}
Expand Down
41 changes: 41 additions & 0 deletions src/utils/setup_utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import fs from 'fs';
import path from 'path';
import yaml from 'js-yaml';
import vscode, { Extension } from 'vscode';
import initSqlJs, { Database } from 'sql.js';

import {
COMMAND,
contextVariables,
Expand Down Expand Up @@ -140,6 +143,8 @@ export const generateRandomNumber = (min = 0, max = 100) => {
export const setupCodeSync = async (repoPath: string) => {
const settings = createSystemDirectories();
new RepoState(repoPath).setSubDirState();
// Setup Database
await setupDatabase();
await createOrUpdateSyncignore();
await addPluginUser();
const userFilePath = settings.USER_PATH;
Expand Down Expand Up @@ -305,3 +310,39 @@ export const uuidv4 = () => {
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
};

export const setupDatabase = async () => {
const settings = generateSettings();
if (!fs.existsSync(settings.DATABASE_FILE_PATH)) {
fs.openSync(settings.DATABASE_FILE_PATH, "w+");
}
const filebuffer = fs.readFileSync(settings.DATABASE_FILE_PATH);
const SQL = await initSqlJs({
// Required to load the wasm binary asynchronously. Of course, you can host it wherever you want
// You can omit locateFile completely when running in node
locateFile: file => path.join(__dirname, ".." , `node_modules/sql.js/dist/${file}`)
});
const db = new SQL.Database(filebuffer);

try {
// const res = db.exec("SELECT * FROM hello");
db.run('BEGIN TRANSACTION');
db.run("CREATE TABLE IF NOT EXISTS test1 (col1, col2);");
// Commit the transaction
db.run('COMMIT');
console.log("Commiting to DB");
// writeToDB(db);
} catch (e) {
console.log("error", e);
// Testing a comment with Latest Sanic version on Server
// How about now with intermediate changes
}
};


const writeToDB = (db: Database) => {
const settings = generateSettings();
const data = db.export();
const buffer = Buffer.from(data);
fs.writeFileSync(settings.DATABASE_FILE_PATH, buffer);
};
18 changes: 18 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2423,6 +2423,11 @@
resolved "https://registry.yarnpkg.com/@types/diff-match-patch/-/diff-match-patch-1.0.32.tgz#d9c3b8c914aa8229485351db4865328337a3d09f"
integrity sha512-bPYT5ECFiblzsVzyURaNhljBH2Gh1t9LowgUwciMrNAhFewLkHT2H0Mto07Y4/3KCOGZHRQll3CTtQZ0X11D/A==

"@types/emscripten@*":
version "1.39.6"
resolved "https://registry.yarnpkg.com/@types/emscripten/-/emscripten-1.39.6.tgz#698b90fe60d44acf93c31064218fbea93fbfd85a"
integrity sha512-H90aoynNhhkQP6DRweEjJp5vfUVdIj7tdPLsu7pq89vODD/lcugKfZOsfgwpvM6XUewEp2N5dCg1Uf3Qe55Dcg==

"@types/express-serve-static-core@^4.17.33":
version "4.17.35"
resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.17.35.tgz#c95dd4424f0d32e525d23812aa8ab8e4d3906c4f"
Expand Down Expand Up @@ -2573,6 +2578,14 @@
"@types/mime" "*"
"@types/node" "*"

"@types/sql.js@^1.4.9":
version "1.4.9"
resolved "https://registry.yarnpkg.com/@types/sql.js/-/sql.js-1.4.9.tgz#31d2bed39aa63e56ada7a4469aa6f19ee2c74b94"
integrity sha512-ep8b36RKHlgWPqjNG9ToUrPiwkhwh0AEzy883mO5Xnd+cL6VBH1EvSjBAAuxLUFF2Vn/moE3Me6v9E1Lo+48GQ==
dependencies:
"@types/emscripten" "*"
"@types/node" "*"

"@types/stack-utils@^2.0.0":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.1.tgz#20f18294f797f2209b5f65c8e3b5c8e8261d127c"
Expand Down Expand Up @@ -5633,6 +5646,11 @@ sprintf-js@~1.0.2:
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
integrity sha512-D9cPgkvLlV3t3IzL0D0YLvGA9Ahk4PcvVwUbN0dSGr1aP0Nrt4AEnTUbuGvquEC0mA64Gqt1fzirlRs5ibXx8g==

sql.js@^1.10.3:
version "1.10.3"
resolved "https://registry.yarnpkg.com/sql.js/-/sql.js-1.10.3.tgz#cd05043a9e2667010cd0520180577c90df8dd990"
integrity sha512-H46aWtQkdyjZwFQgraUruy5h/DyJBbAK3EA/WEMqiqF6PGPfKBSKBj/er3dVyYqVIoYfRf5TFM/loEjtQIrqJg==

stack-utils@^2.0.3:
version "2.0.6"
resolved "https://registry.yarnpkg.com/stack-utils/-/stack-utils-2.0.6.tgz#aaf0748169c02fc33c8232abccf933f54a1cc34f"
Expand Down