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

fix: set response type as arraybuffer when downloading file content #97

Merged
merged 1 commit into from
Nov 12, 2023
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
4 changes: 3 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Test ignores
test/**/*.md
test/**/*.json
test/**/*.json
# Markdown ignores
*.md
31 changes: 27 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ The generated file has the following characteristics:
```json
[
{
"package": "asset1Package",
"name": "asset1",
"package": "package1",
"hold": false,
"name": "asset1",
"localPath": "path/asset1.min.js",
"assetManager": {
"name": "cdnjs",
Expand All @@ -104,16 +104,39 @@ The generated file has the following characteristics:
"currentVersion": "0.0.1"
},
{
"package": "asset2Package",
"name": "asset2",
"package": "package2",
"hold": false,
"name": "asset2",
"localPath": "./path/asset2.min.js",
"assetManager": {
"name": "github",
"owner": "owner",
"repository": "repository",
"filePath": "dir/asset2.min.js"
}
},
{
"package": "package3",
"hold": false,
"name": "asset3",
"localPath": "./path/asset3.min.js",
"assetManager": {
"name": "unpkg",
"library": "asset3",
"filePath": "dist/asset3.min.js"
}
},
{
"package": "package4",
"hold": false,
"name": "asset4",
"localPath": "./path/asset4.min.js",
"assetManager": {
"name": "jsdelivr",
"cdn": "npm",
"package": "asset4",
"filePath": "dist/asset4.min.js"
}
}
]
```
Expand Down
8 changes: 3 additions & 5 deletions src/model/assetManagers/cdnjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const CDNJS_API_URL = 'https://api.cdnjs.com';
const CDNJS_LIBRARIES_PATH = 'libraries';
const CDNJS_LIBS_HOST = 'https://cdnjs.cloudflare.com';
const CDNJS_LIBS_ROOT_PATH = 'ajax/libs';
const CDNJS_LIBS_ENCODING = 'utf-8';

export class Cdnjs extends AssetManager {
@IsDefined()
Expand Down Expand Up @@ -64,16 +63,15 @@ export class Cdnjs extends AssetManager {
maxContentLength: 100000000,
maxBodyLength: 100000000,
method: 'get',
responseType: 'arraybuffer',
url: url,
})
.then((response: any) => {
if (response.data) {
return Promise.resolve(
Buffer.from(response.data, CDNJS_LIBS_ENCODING)
);
return Promise.resolve(Buffer.from(response.data));
} else {
throw new BmycError(
`Cannot get content of library ${this.fileName} (${assetVersion})`
`Cannot get content of ${this.fileName} (${assetVersion})`
);
}
})
Expand Down
9 changes: 8 additions & 1 deletion src/model/assetManagers/jsdelivr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,17 @@ export class Jsdelivr extends AssetManager {
const url = `${JSDELIVR_DATA_URL}/${this.cdn}/${this.package}@${assetVersion}/${this.filePath}`;
return axios({
method: 'get',
responseType: 'arraybuffer',
url: url,
})
.then((response: any) => {
return Promise.resolve(response.data);
if (response.data) {
return Promise.resolve(Buffer.from(response.data));
} else {
throw new BmycError(
`Cannot get content of ${this.filePath} (${assetVersion})`
);
}
})
.catch((error: Error) => {
throw new BmycError(`${url}:\n${error.message}`);
Expand Down
9 changes: 8 additions & 1 deletion src/model/assetManagers/unpkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,17 @@ export class Unpkg extends AssetManager {
const url = `${UNPKG_API_URL}/${this.library}@${assetVersion}/${this.filePath}`;
return axios({
method: 'get',
responseType: 'arraybuffer',
url: url,
})
.then((response: any) => {
return Promise.resolve(response.data);
if (response.data) {
return Promise.resolve(Buffer.from(response.data));
} else {
throw new BmycError(
`Cannot get content of ${this.filePath} (${assetVersion})`
);
}
})
.catch((error: Error) => {
throw new BmycError(`${url}:\n${error.message}`);
Expand Down
Empty file.
Empty file.
8 changes: 4 additions & 4 deletions test/resources/config-ok.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"localPath": "./path/asset3.min.js",
"assetManager": {
"name": "unpkg",
"library": "axios",
"filePath": "dist/axios.min.js"
"library": "asset3",
"filePath": "dist/asset3.min.js"
}
},
{
Expand All @@ -42,8 +42,8 @@
"assetManager": {
"name": "jsdelivr",
"cdn": "npm",
"package": "axios",
"filePath": "dist/axios.min.js"
"package": "asset4",
"filePath": "dist/asset4.min.js"
}
}
]