-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create @amazeelabs/save-webpage
- Loading branch information
Showing
8 changed files
with
248 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
extends: ['@amazeelabs/eslint-config'], | ||
root: true, | ||
}; |
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,2 @@ | ||
build | ||
node_modules |
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 @@ | ||
!build |
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 @@ | ||
"@amazeelabs/prettier-config" |
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,27 @@ | ||
{ | ||
"name": "@amazeelabs/save-webpage", | ||
"version": "1.0.0", | ||
"description": "Saves a webpage with all resources.", | ||
"type": "module", | ||
"main": "./build/index.js", | ||
"types": "./build/index.d.ts", | ||
"private": false, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"scripts": { | ||
"build": "rm -rf build && tsc", | ||
"test:static": "tsc --noEmit && eslint '**/*.{ts,tsx,js,jsx}' --ignore-path='./.gitignore'" | ||
}, | ||
"dependencies": { | ||
"website-scraper": "^5.3.1" | ||
}, | ||
"devDependencies": { | ||
"@amazeelabs/eslint-config": "1.4.43", | ||
"@amazeelabs/prettier-config": "1.1.3", | ||
"@types/website-scraper": "^1.2.10", | ||
"typescript": "5.3.3" | ||
}, | ||
"keywords": [], | ||
"author": "AmazeeLabs <[email protected]>" | ||
} |
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,57 @@ | ||
import * as scrape from 'website-scraper'; | ||
|
||
export async function saveWebpage(args: { | ||
/** | ||
* URL of the target webpage. | ||
*/ | ||
url: string; | ||
/** | ||
* Replaces the actual webpage content. | ||
*/ | ||
content?: string; | ||
/** | ||
* Path to directory where downloaded files will be saved. Must not exist. | ||
*/ | ||
directory: string; | ||
}): Promise<void> { | ||
await scrape({ | ||
directory: args.directory, | ||
urls: [args.url], | ||
plugins: args.content ? [new ReplaceContentPlugin(args.content)] : [], | ||
}); | ||
} | ||
|
||
// Replaces the first request response with given content. | ||
class ReplaceContentPlugin { | ||
isFirstRequest = true; | ||
|
||
constructor(public firstRequestResponse: string) {} | ||
|
||
apply( | ||
registerAction: ( | ||
event: 'afterResponse', | ||
handler: <T>(args: { response: T }) => Promise< | ||
| T | ||
| { | ||
body: string; | ||
encoding: 'utf8'; | ||
metadata: {}; | ||
} | ||
>, | ||
) => void, | ||
): void { | ||
registerAction('afterResponse', async ({ response }) => { | ||
if (this.isFirstRequest) { | ||
this.isFirstRequest = false; | ||
return { | ||
body: this.firstRequestResponse, | ||
encoding: 'utf8', | ||
metadata: {}, | ||
}; | ||
} else { | ||
this.isFirstRequest = false; | ||
return response; | ||
} | ||
}); | ||
} | ||
} |
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 @@ | ||
{ | ||
"compilerOptions": { | ||
"declaration": true, | ||
"target": "esnext", | ||
"module": "esnext", | ||
"strict": true, | ||
"skipLibCheck": true, | ||
"outDir": "build" | ||
}, | ||
"include": ["src"], | ||
"$schema": "https://json.schemastore.org/tsconfig" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.