Skip to content

Commit

Permalink
Fixes #9, resolves #9, closes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmadnasriya committed Aug 31, 2024
1 parent 2067217 commit 40907bf
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 43 deletions.
72 changes: 40 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nasriya/hypercloud",
"version": "1.2.0",
"version": "1.2.1",
"description": "Nasriya HyperCloud is a lightweight Node.js HTTP2 framework.",
"main": "./dist/cjs/hypercloud.js",
"module": "./dist/esm/hypercloud.js",
Expand Down Expand Up @@ -60,18 +60,18 @@
"author": "Nasriya Software, LLC.",
"license": "Nasriya License",
"devDependencies": {
"@nasriya/postbuild": "^1.1.4",
"@nasriya/postbuild": "^1.1.5",
"@types/ejs": "^3.1.5",
"@types/jest": "^29.5.12",
"@types/ms": "^0.7.34",
"@types/node": "^22.1.0",
"ts-jest": "^29.2.4",
"@types/node": "^22.5.1",
"ts-jest": "^29.2.5",
"typescript": "^5.5.4"
},
"dependencies": {
"ejs": "^3.1.10",
"ms": "^2.1.3",
"tldts": "^6.1.38"
"tldts": "^6.1.41"
},
"bugs": {
"url": "https://github.com/nasriyasoftware/HyperCloud/issues"
Expand Down
4 changes: 4 additions & 0 deletions src/docs/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -760,11 +760,15 @@ export interface DownloadFileOptions {
serverErrorFile?: string;
/**eTags ae useful for caching */
eTag?: string
/**A filename to send as the name of the downloaded filename */
fileName?: string;
}

export interface SendFileOptions extends DownloadFileOptions {
/** Set this to `true` if you want to download the file */
download?: boolean;
/**A filename to send as the name of the downloaded filename. */
fileName?: string;
}


Expand Down
21 changes: 16 additions & 5 deletions src/services/handler/assets/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ export class HyperCloudResponse {
* @param {DownloadFileOptions} options Options for sending the file
* @returns {http2.Http2ServerResponse|undefined}
*/
downloadFile(filePath: string, options: DownloadFileOptions): http2.Http2ServerResponse | undefined {
downloadFile(filePath: string, options?: DownloadFileOptions): http2.Http2ServerResponse | undefined {
const sendOptions: SendFileOptions = helpers.is.realObject(options) ? { ...options, download: true } : { download: true }
return this.sendFile(filePath, sendOptions);
}
Expand Down Expand Up @@ -541,8 +541,15 @@ export class HyperCloudResponse {
if (fileAvail.errors.notAccessible) { throw new Error(`You don't have enough permissions to access the file path: ${filePath}`) }
}

const paths = filePath.split('\\');
const fileName = paths[paths.length - 1];
const fileName = (() => {
if (options && 'fileName' in options) {
if (helpers.isNot.validString(options.fileName)) { throw new TypeError(`The procided filename is not a string but a ${typeof options.fileName}`) }
return options.fileName as string;
} else {
const paths = filePath.split('\\');
return paths[paths.length - 1];
}
})();

// Handling dotFiles
if (fileName.startsWith('.')) {
Expand Down Expand Up @@ -672,8 +679,12 @@ export class HyperCloudResponse {
// Check if the download option is triggered or not
if (options && 'download' in options) {
if (typeof options.download !== 'boolean') { throw new TypeError(`The download property should be a boolean value, but instead got ${typeof options.download}`) }
this.setHeader('Content-Type', 'application/octet-stream');
this.setHeader('Content-Disposition', `attachment; filename="${fileName}"`);
if (options.download === true) {
this.setHeader('Content-Type', 'application/octet-stream');
this.setHeader('Content-Disposition', `attachment; filename="${fileName}"`);
} else {
this.setHeader('Content-Type', mime);
}
} else {
this.setHeader('Content-Type', mime);
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/rateLimiter/rateLimiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ export class RateLimitingManager {
if (this.#_server instanceof HyperCloudServer) {
this.#_server._handlers['mainRateLimiter'] = handler;
} else {
throw new Error(`The rate limitter's main handler can only be used on an instance that was created by the server`)
throw new Error(`The rate limiter's main handler can only be used on an instance that was created by the server`)
}
}
}
Expand Down

0 comments on commit 40907bf

Please sign in to comment.