forked from janhq/jan
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: file prefix replace utils & add unit test (janhq#1676)
* refactor: file prefix replace utils * chore: add unit tests for core module
- Loading branch information
Showing
21 changed files
with
204 additions
and
386 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,4 @@ coverage | |
.vscode | ||
.idea | ||
dist | ||
compiled | ||
.awcache | ||
.rpt2_cache | ||
docs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
moduleNameMapper: { | ||
'@/(.*)': '<rootDir>/src/$1', | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,8 @@ | |
"module": "dist/core.es5.js", | ||
"typings": "dist/types/index.d.ts", | ||
"files": [ | ||
"dist" | ||
"dist", | ||
"types" | ||
], | ||
"author": "Jan <[email protected]>", | ||
"exports": { | ||
|
@@ -38,18 +39,23 @@ | |
}, | ||
"scripts": { | ||
"lint": "tslint --project tsconfig.json -t codeFrame 'src/**/*.ts' 'test/**/*.ts'", | ||
"test": "jest", | ||
"prebuild": "rimraf dist", | ||
"build": "tsc --module commonjs && rollup -c rollup.config.ts", | ||
"start": "rollup -c rollup.config.ts -w" | ||
}, | ||
"devDependencies": { | ||
"jest": "^25.4.0", | ||
"@types/jest": "^29.5.11", | ||
"@types/node": "^12.0.2", | ||
"eslint-plugin-jest": "^23.8.2", | ||
"rollup": "^2.38.5", | ||
"rollup-plugin-commonjs": "^9.1.8", | ||
"rollup-plugin-json": "^3.1.0", | ||
"rollup-plugin-node-resolve": "^5.2.0", | ||
"rollup-plugin-sourcemaps": "^0.6.3", | ||
"rollup-plugin-typescript2": "^0.36.0", | ||
"ts-jest": "^26.1.1", | ||
"tslib": "^2.6.2", | ||
"typescript": "^5.2.2" | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,58 @@ | ||
import { DownloadRoute } from '../../../api' | ||
import { join } from 'path' | ||
import { userSpacePath } from '../../extension/manager' | ||
import { DownloadManager } from '../../download' | ||
import { HttpServer } from '../HttpServer' | ||
import { createWriteStream } from 'fs' | ||
import { DownloadRoute } from "../../../api"; | ||
import { join } from "path"; | ||
import { userSpacePath } from "../../extension/manager"; | ||
import { DownloadManager } from "../../download"; | ||
import { HttpServer } from "../HttpServer"; | ||
import { createWriteStream } from "fs"; | ||
import { normalizeFilePath } from "../../path"; | ||
|
||
export const downloadRouter = async (app: HttpServer) => { | ||
app.post(`/${DownloadRoute.downloadFile}`, async (req, res) => { | ||
const strictSSL = !(req.query.ignoreSSL === 'true'); | ||
const proxy = req.query.proxy?.startsWith('http') ? req.query.proxy : undefined; | ||
const body = JSON.parse(req.body as any) | ||
const strictSSL = !(req.query.ignoreSSL === "true"); | ||
const proxy = req.query.proxy?.startsWith("http") ? req.query.proxy : undefined; | ||
const body = JSON.parse(req.body as any); | ||
const normalizedArgs = body.map((arg: any) => { | ||
if (typeof arg === 'string' && arg.includes('file:/')) { | ||
return join(userSpacePath, arg.replace('file:/', '')) | ||
if (typeof arg === "string") { | ||
return join(userSpacePath, normalizeFilePath(arg)); | ||
} | ||
return arg | ||
}) | ||
return arg; | ||
}); | ||
|
||
const localPath = normalizedArgs[1] | ||
const fileName = localPath.split('/').pop() ?? '' | ||
const localPath = normalizedArgs[1]; | ||
const fileName = localPath.split("/").pop() ?? ""; | ||
|
||
const request = require('request') | ||
const progress = require('request-progress') | ||
const rq = request({ url: normalizedArgs[0], strictSSL, proxy }) | ||
const request = require("request"); | ||
const progress = require("request-progress"); | ||
|
||
const rq = request({ url: normalizedArgs[0], strictSSL, proxy }); | ||
progress(rq, {}) | ||
.on('progress', function (state: any) { | ||
console.log('download onProgress', state) | ||
.on("progress", function (state: any) { | ||
console.log("download onProgress", state); | ||
}) | ||
.on('error', function (err: Error) { | ||
console.log('download onError', err) | ||
.on("error", function (err: Error) { | ||
console.log("download onError", err); | ||
}) | ||
.on('end', function () { | ||
console.log('download onEnd') | ||
.on("end", function () { | ||
console.log("download onEnd"); | ||
}) | ||
.pipe(createWriteStream(normalizedArgs[1])) | ||
.pipe(createWriteStream(normalizedArgs[1])); | ||
|
||
DownloadManager.instance.setRequest(fileName, rq) | ||
}) | ||
DownloadManager.instance.setRequest(fileName, rq); | ||
}); | ||
|
||
app.post(`/${DownloadRoute.abortDownload}`, async (req, res) => { | ||
const body = JSON.parse(req.body as any) | ||
const body = JSON.parse(req.body as any); | ||
const normalizedArgs = body.map((arg: any) => { | ||
if (typeof arg === 'string' && arg.includes('file:/')) { | ||
return join(userSpacePath, arg.replace('file:/', '')) | ||
if (typeof arg === "string") { | ||
return join(userSpacePath, normalizeFilePath(arg)); | ||
} | ||
return arg | ||
}) | ||
return arg; | ||
}); | ||
|
||
const localPath = normalizedArgs[0] | ||
const fileName = localPath.split('/').pop() ?? '' | ||
console.debug('fileName', fileName) | ||
const rq = DownloadManager.instance.networkRequests[fileName] | ||
DownloadManager.instance.networkRequests[fileName] = undefined | ||
rq?.abort() | ||
}) | ||
} | ||
const localPath = normalizedArgs[0]; | ||
const fileName = localPath.split("/").pop() ?? ""; | ||
const rq = DownloadManager.instance.networkRequests[fileName]; | ||
DownloadManager.instance.networkRequests[fileName] = undefined; | ||
rq?.abort(); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Normalize file path | ||
* Remove all file protocol prefix | ||
* @param path | ||
* @returns | ||
*/ | ||
export function normalizeFilePath(path: string): string { | ||
return path.replace(/^(file:[\\/]+)([^:\s]+)$/, "$2"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { normalizeFilePath } from "../../src/node/path"; | ||
|
||
describe("Test file normalize", () => { | ||
test("returns no file protocol prefix on Unix", async () => { | ||
expect(normalizeFilePath("file://test.txt")).toBe("test.txt"); | ||
expect(normalizeFilePath("file:/test.txt")).toBe("test.txt"); | ||
}); | ||
test("returns no file protocol prefix on Windows", async () => { | ||
expect(normalizeFilePath("file:\\\\test.txt")).toBe("test.txt"); | ||
expect(normalizeFilePath("file:\\test.txt")).toBe("test.txt"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.