Skip to content

Commit

Permalink
[add] Range Download method & example (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
TechQuery committed Mar 30, 2024
1 parent 7c81d39 commit f5b128b
Show file tree
Hide file tree
Showing 6 changed files with 1,451 additions and 1,413 deletions.
4 changes: 0 additions & 4 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
#!/bin/sh

. "$(dirname "$0")/_/husky.sh"

npm test
4 changes: 0 additions & 4 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
#!/bin/sh

. "$(dirname "$0")/_/husky.sh"

npm run build
34 changes: 33 additions & 1 deletion ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ client.get('/path/to/your/API').then(console.log);

### Up/Download files

#### Single HTTP request based on XMLHTTPRequest `progress` events

(based on [Iterable Observer][6])

```javascript
Expand All @@ -123,10 +125,40 @@ document.querySelector('input[type="file"]').onchange = async ({
};
```

#### Multiple HTTP requests based on `Range` header

```shell
npm i native-file-system-adapter # Web standard API polyfill
```

```javascript
import { showSaveFilePicker } from 'native-file-system-adapter';
import { HTTPClient } from 'koajax';

const bufferClient = new HTTPClient({ responseType: 'arraybuffer' });

document.querySelector('#download').onclick = async () => {
const fileURL = 'https://your.server/with/Range/header/supported/file.zip';
const suggestedName = fileURL.split('/').at(-1);

const fileHandle = await showSaveFilePicker({ suggestedName });
const writer = await fileHandle.createWritable(),
stream = bufferClient.download();

for await (const { total, loaded, percent, buffer } of stream) {
writer.write(buffer);

console.table({ total, loaded, percent });
}
write.close();
window.alert(`File ${fileHandle.name} downloaded successfully!`);
};
```

### Global Error fallback

```shell
npm install browser-unhandled-rejection
npm install browser-unhandled-rejection # Web standard API polyfill
```

```javascript
Expand Down
40 changes: 20 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "koajax",
"version": "0.9.6",
"version": "1.0.0",
"license": "LGPL-3.0",
"author": "[email protected]",
"description": "HTTP Client based on Koa-like middlewares",
Expand All @@ -25,35 +25,35 @@
"main": "dist/index.js",
"module": "dist/index.esm.js",
"dependencies": {
"@swc/helpers": "^0.5.3",
"@swc/helpers": "^0.5.8",
"iterable-observer": "^1.0.1",
"regenerator-runtime": "^0.14.0",
"web-utility": "^4.1.3"
"regenerator-runtime": "^0.14.1",
"web-utility": "^4.3.0"
},
"peerDependencies": {
"jsdom": ">=21"
},
"devDependencies": {
"@parcel/packager-ts": "~2.10.3",
"@parcel/transformer-typescript-types": "~2.10.3",
"@parcel/packager-ts": "~2.12.0",
"@parcel/transformer-typescript-types": "~2.12.0",
"@types/core-js": "^2.5.8",
"@types/jest": "^29.5.10",
"@types/jest": "^29.5.12",
"@types/jsdom": "^21.1.6",
"@types/node": "^18.18.13",
"core-js": "^3.33.3",
"@types/node": "^18.19.28",
"core-js": "^3.36.1",
"cross-env": "^7.0.3",
"husky": "^8.0.3",
"husky": "^9.0.11",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"jsdom": "^22.1.0",
"lint-staged": "^15.1.0",
"open-cli": "^7.2.0",
"parcel": "~2.10.3",
"prettier": "^3.1.0",
"ts-jest": "^29.1.1",
"typedoc": "^0.25.3",
"typedoc-plugin-mdn-links": "^3.1.4",
"typescript": "~5.3.2"
"jsdom": "^24.0.0",
"lint-staged": "^15.2.2",
"open-cli": "^8.0.0",
"parcel": "~2.12.0",
"prettier": "^3.2.5",
"ts-jest": "^29.1.2",
"typedoc": "^0.25.12",
"typedoc-plugin-mdn-links": "^3.1.18",
"typescript": "~5.4.3"
},
"prettier": {
"singleQuote": true,
Expand All @@ -76,7 +76,7 @@
"types": false
},
"scripts": {
"prepare": "husky install",
"prepare": "husky",
"test": "lint-staged && jest",
"pack-dist": "rm -rf dist/ && tsc --emitDeclarationOnly && parcel build",
"pack-docs": "rm -rf docs/ && typedoc source/",
Expand Down
Loading

0 comments on commit f5b128b

Please sign in to comment.