added doga
This commit is contained in:
1 parent
5174ab4cc4
commit
13254e5623
1149 files changed
+80161
No files matched your search
+33
@@ -0,0 +1,33 @@
|
||||
# [1.1.3] (2022-01-06)
|
||||
|
||||
### Fix
|
||||
|
||||
* 🐛🔨 Fix empty `dist` folder in package.
|
||||
|
||||
### Minor
|
||||
|
||||
* 📦🗑️ Remove unnecessary `package-lock.json` file.
|
||||
|
||||
# [1.1.2] (2021-10-22)
|
||||
|
||||
### Documentation
|
||||
|
||||
* 📚 Documentation updated
|
||||
|
||||
# [1.1.1] (2021-10-19)
|
||||
|
||||
### Documentation
|
||||
|
||||
* 📚 Documentation updated
|
||||
|
||||
# [1.1.0] (2021-10-19)
|
||||
|
||||
### Breaking Changes
|
||||
|
||||
* Minimal Node version now is `12.22.0`
|
||||
|
||||
# [1.0.0] (2021-10-02)
|
||||
|
||||
### Features
|
||||
|
||||
* Initial release
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2012-2014 TJ Holowaychuk <tj@vision-media.ca>
|
||||
Copyright (c) 2015-2016 Douglas Christopher Wilson <doug@somethingdoug.com>
|
||||
Copyright (c) 2021 Anton Trofimenko <r37r0m0d3l@protonmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+178
@@ -0,0 +1,178 @@
|
||||
# Header • Range • Parser
|
||||
|
||||

|
||||
|
||||
Range header field parser. Fork of a̶b̶a̶n̶d̶o̶n̶e̶d̶ [range-parser](https://github.com/jshttp/range-parser). If you write to me with a request to change or update something, I will do it. Honestly 👼.
|
||||
|
||||
[![NPM Version][npm-version-img]][npm-version-url]
|
||||
[![NPM Downloads][npm-downloads-img]][npm-downloads-url]
|
||||
[![GitHub Stars][gh-stars-img]][gh-stars-url]
|
||||
[![Node.js Version][node-image]][node-url]
|
||||
[![TypeScript Typings][ts-img]][ts-url]
|
||||
|
||||
[![GitHub Checks][gh-checks-img]][gh-checks-url]
|
||||
[![Travis CI][travis-img]][travis-url]
|
||||
[![Snyk][snyk-img]][snyk-url]
|
||||
|
||||
[![Maintainability Rating][sonarcloud-img]][sonarcloud-url]
|
||||
[![LGTM][lgtm-img]][lgtm-url]
|
||||
[![Codacy Badge][codacy-img]][codacy-url]
|
||||
[![CodeFactor][codefactor-img]][codefactor-url]
|
||||
|
||||
[comment]: <> ([![Dependabot][dependabot-img]][dependabot-url])
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
npm install header-range-parser
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
<!-- eslint-disable no-unused-vars -->
|
||||
|
||||
```js
|
||||
const {
|
||||
ERROR_INVALID_ARGUMENT,
|
||||
ERROR_STRING_IS_NOT_HEADER,
|
||||
ERROR_UNSATISFIABLE_RESULT,
|
||||
parseRange,
|
||||
} = require("header-range-parser");
|
||||
```
|
||||
|
||||
```typescript
|
||||
import {
|
||||
ERROR_INVALID_ARGUMENT,
|
||||
ERROR_STRING_IS_NOT_HEADER,
|
||||
ERROR_UNSATISFIABLE_RESULT,
|
||||
ResultInvalid,
|
||||
ResultUnsatisfiable,
|
||||
ResultWrongArgument,
|
||||
parseRange,
|
||||
} from "header-range-parser";
|
||||
```
|
||||
|
||||
### parseRange(size, header, options)
|
||||
|
||||
```typescript
|
||||
import {
|
||||
Result, Ranges, parseRange, Options,
|
||||
} from "header-range-parser";
|
||||
|
||||
declare function parseRange(
|
||||
size: number, header: string, options?: Options,
|
||||
): Ranges | Result;
|
||||
```
|
||||
|
||||
| Parameter | Type | Description |
|
||||
| :-------- | :---------| :---------------------------------------------------- |
|
||||
| `size` | `number` | **Required**. Size in bytes. |
|
||||
| `header` | `string` | **Required**. String containing header. |
|
||||
| `options` | `object` | Optional options: combine (bool), throwError (bool). |
|
||||
|
||||
Parse the given `header` string where `size` is the size of the selected
|
||||
representation that is to be partitioned into sub-ranges. An array of sub-ranges
|
||||
will be returned or negative numbers indicating an error parsing.
|
||||
|
||||
- `-1` or `ERROR_UNSATISFIABLE_RESULT` or ` esultUnsatisfiable` signals an unsatisfiable range
|
||||
|
||||
- `-2` or `ERROR_STRING_IS_NOT_HEADER` or `ResultInvalid` signals a malformed header string
|
||||
|
||||
- `-3` or `ERROR_INVALID_ARGUMENT` or `ResultWrongArgument` invalid parameters
|
||||
|
||||
<!-- eslint-disable no-undef -->
|
||||
|
||||
```js
|
||||
// parse header from request
|
||||
const subRanges = parseRange(
|
||||
size,
|
||||
request.headers.range,
|
||||
);
|
||||
|
||||
// the type of the subranges
|
||||
if (subRanges.type === "bytes") {
|
||||
// the ranges
|
||||
subRanges.forEach((range) => {
|
||||
// do something
|
||||
// with range.start
|
||||
// and range.end
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
#### Options
|
||||
|
||||
These properties are accepted in the options object.
|
||||
|
||||
##### combine
|
||||
|
||||
Specifies if overlapping and adjacent sub-ranges should be combined, defaults to `false`.
|
||||
|
||||
When `true`, ranges will be combined and returned as if they were specified that way in the header.
|
||||
|
||||
##### throwError
|
||||
|
||||
Throw or suppress errors. Defaults to `true`.
|
||||
|
||||
<!-- eslint-disable no-undef -->
|
||||
|
||||
```js
|
||||
parseRange(
|
||||
100,
|
||||
"bytes=50-55,0-10,5-10,56-60",
|
||||
{
|
||||
combine: true,
|
||||
throwError: false,
|
||||
});
|
||||
// [
|
||||
// { start: 0, end: 10 },
|
||||
// { start: 50, end: 60 }
|
||||
// ]
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
[💾 My other projects](https://r37r0m0d3l.icu/open_source_map)
|
||||
|
||||
<img alt="Open Source" src="https://raw.githubusercontent.com/r37r0m0d3l/r37r0m0d3l/master/osmap.svg?sanitize=true" width="960" height="520" style="display:block;height:auto;margin-left:auto;margin-right:auto;min-height:520px;min-width:960px;width:100%;">
|
||||
|
||||
<!-- Badges -->
|
||||
|
||||
[npm-version-img]: https://badgen.net/npm/v/header-range-parser?&icon=npm&label=npm&color=DD3636&v=1.1.1
|
||||
[npm-version-url]: https://npmjs.com/package/header-range-parser
|
||||
|
||||
[npm-downloads-img]: https://badgen.net/npm/dt/header-range-parser?&icon=terminal&label=downloads&color=009688&v=1.1.1
|
||||
[npm-downloads-url]: https://npmjs.com/package/header-range-parser
|
||||
|
||||
[gh-stars-img]: https://badgen.net/github/stars/r37r0m0d3l/header-range-parser?&icon=github&label=stars&color=FFCC33&v=1.1.1
|
||||
[gh-stars-url]: https://github.com/r37r0m0d3l/header-range-parser
|
||||
|
||||
[node-image]: https://badgen.net/npm/node/header-range-parser
|
||||
[node-url]: https://nodejs.org/en/download
|
||||
|
||||
[gh-checks-img]: https://badgen.net/github/checks/r37r0m0d3l/header-range-parser?&icon=github&v=1.1.1
|
||||
[gh-checks-url]: https://github.com/r37r0m0d3l/header-range-parser
|
||||
|
||||
[travis-img]: https://badgen.net/travis/r37r0m0d3l/header-range-parser?&icon=travis&label=build&v=1.1.1
|
||||
[travis-url]: https://travis-ci.com/github/r37r0m0d3l/header-range-parser
|
||||
|
||||
[ts-img]: https://badgen.net/npm/types/header-range-parser?&icon=typescript&label=types&color=1E90FF&v=1.1.1
|
||||
[ts-url]: https://github.com/r37r0m0d3l/header-range-parser/blob/main/dist/index.d.ts
|
||||
|
||||
[sonarcloud-img]: https://sonarcloud.io/api/project_badges/measure?project=r37r0m0d3l_header-range-parser&metric=sqale_rating&v=1.1.1
|
||||
[sonarcloud-url]: https://sonarcloud.io/dashboard?id=r37r0m0d3l_header-range-parser
|
||||
|
||||
[lgtm-img]: https://badgen.net/lgtm/grade/g/r37r0m0d3l/header-range-parser?&icon=lgtm&label=lgtm:js/ts&color=00C853&v=1.1.1
|
||||
[lgtm-url]: https://lgtm.com/projects/g/r37r0m0d3l/header-range-parser/context:javascript
|
||||
|
||||
[codacy-img]: https://app.codacy.com/project/badge/Grade/b3458c991041406bbe85fdfd87498006
|
||||
[codacy-url]: https://www.codacy.com/gh/r37r0m0d3l/header-range-parser/dashboard?&utm_source=github.com&utm_medium=referral&utm_content=r37r0m0d3l/header-range-parser&utm_campaign=Badge_Grade
|
||||
|
||||
[snyk-img]: https://badgen.net/snyk/r37r0m0d3l/header-range-parser?&v=1.1.1
|
||||
[snyk-url]: https://github.com/r37r0m0d3l/header-range-parser
|
||||
|
||||
[dependabot-img]: https://badgen.net/dependabot/r37r0m0d3l/header-range-parser?&icon=dependabot&v=1.1.1
|
||||
[dependabot-url]: https://github.com/r37r0m0d3l/header-range-parser
|
||||
|
||||
[codefactor-img]: https://www.codefactor.io/repository/github/r37r0m0d3l/header-range-parser/badge?&style=flat-square&v=1.1.1
|
||||
[codefactor-url]: https://www.codefactor.io/repository/github/r37r0m0d3l/header-range-parser
|
||||
+1
@@ -0,0 +1 @@
|
||||
var o=Object.defineProperty;var c=Object.getOwnPropertyDescriptor;var d=Object.getOwnPropertyNames;var f=Object.prototype.hasOwnProperty;var m=n=>o(n,"__esModule",{value:!0});var p=(n,e)=>{for(var t in e)o(n,t,{get:e[t],enumerable:!0})},x=(n,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of d(e))!f.call(n,s)&&(t||s!=="default")&&o(n,s,{get:()=>e[s],enumerable:!(i=c(e,s))||i.enumerable});return n};var b=(n=>(e,t)=>n&&n.get(e)||(t=x(m({}),e,1),n&&n.set(e,t),t))(typeof WeakMap!="undefined"?new WeakMap:0);var w={};p(w,{ERROR_INVALID_ARGUMENT:()=>u,ERROR_STRING_IS_NOT_HEADER:()=>g,ERROR_UNSATISFIABLE_RESULT:()=>l,Ranges:()=>R,parseRange:()=>W});var u=-3,g=-2,l=-1;function I(n){let e=n.map(h).sort(E),t=0;for(let s=1;s<e.length;s++){let a=e[s],r=e[t];a.start>r.end+1?e[++t]=a:a.end>r.end&&(r.end=a.end,r.index=Math.min(r.index,a.index))}e.length=t+1;let i=[...e].sort(N).map(y);return i.type=n.type,i}function h(n,e){return{end:n.end,index:e,start:n.start}}function y(n){return{end:n.end,start:n.start}}function N(n,e){return n.index-e.index}function E(n,e){return n.start-e.start}var R=class extends Array{constructor(){super(...arguments);this.type=""}toArray(){let e=Array.from(this);return e.type=this.type,e}};function A(n,e){let t=new R;for(let i of n){let s=i.split("-"),a=Number.parseInt(s[0],10),r=Number.parseInt(s[1],10);Number.isNaN(a)?(a=e-r,r=e-1):Number.isNaN(r)&&(r=e-1),r>e-1&&(r=e-1),!(Number.isNaN(a)||Number.isNaN(r)||a>r||a<0)&&t.push({end:r,start:a})}return t}function W(n,e,t){let i=!0;if(t&&"throwError"in t&&t.throwError===!1&&(i=!1),!Number.isInteger(n)){if(i)throw new TypeError("Argument 'size' must be an integer.");return u}if(typeof e!="string"){if(i)throw new TypeError("Argument 'header' must be a string.");return u}let s=e.indexOf("=");if(s===-1)return g;let a=e.slice(s+1).split(","),r=A(a,n);return r.length<1?l:(r.type=e.slice(0,s),t&&t.combine?I(r):r)}module.exports=b(w);
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"version": 3,
|
||||
"sources": ["../src/index.ts"],
|
||||
"sourcesContent": ["interface RangeWithIndex {\n end: number;\n index: number;\n start: number;\n}\n\nexport interface Range {\n end: number;\n start: number;\n}\n\nexport interface Options {\n /**\n * @description The \"combine\" option can be set to `true`\n * and overlapping & adjacent ranges\n * will be combined into a single range.\n */\n combine?: boolean;\n /**\n * @description Throw or suppress errors.\n */\n throwError?: boolean;\n}\n\nexport type ResultUnsatisfiable = -1;\n\nexport type ResultInvalid = -2;\n\nexport type ResultWrongArgument = -3;\n\nexport type Result = ResultInvalid | ResultUnsatisfiable | ResultWrongArgument;\n\nexport const ERROR_INVALID_ARGUMENT: ResultWrongArgument = -3 as const;\n\nexport const ERROR_STRING_IS_NOT_HEADER: ResultInvalid = -2 as const;\n\nexport const ERROR_UNSATISFIABLE_RESULT: ResultUnsatisfiable = -1 as const;\n\n/**\n * @description Combine overlapping & adjacent ranges.\n * @param {Ranges} ranges\n * @returns {Ranges}\n */\nfunction combineRanges(ranges: Ranges): Ranges {\n const ordered = ranges.map(mapWithIndex).sort(sortByRangeStart);\n let order = 0;\n for (let index = 1; index < ordered.length; index++) {\n const range: RangeWithIndex = ordered[index];\n const current: RangeWithIndex = ordered[order];\n if (range.start > current.end + 1) {\n ordered[++order] = range;\n } else if (range.end > current.end) {\n current.end = range.end;\n current.index = Math.min(current.index, range.index);\n }\n }\n ordered.length = order + 1;\n const combined = [...ordered].sort(sortByRangeIndex).map(mapWithoutIndex) as Ranges;\n combined.type = ranges.type;\n return combined;\n}\n\n/**\n * @description Map function to add index value to ranges.\n * @param {RangeWithIndex} range\n * @param {number} index\n * @returns {RangeWithIndex}\n */\nfunction mapWithIndex(range: Range | RangeWithIndex, index: number): RangeWithIndex {\n return { end: range.end, index, start: range.start };\n}\n\n/**\n * @description Map function to remove index value from ranges.\n * @param {RangeWithIndex} range\n * @returns {Range}\n */\nfunction mapWithoutIndex(range: RangeWithIndex): Range {\n return { end: range.end, start: range.start };\n}\n\n/**\n * @description Sort function to sort ranges by index.\n * @param {RangeWithIndex} alpha\n * @param {RangeWithIndex} beta\n * @returns {number}\n */\nfunction sortByRangeIndex(alpha: RangeWithIndex, beta: RangeWithIndex): number {\n return alpha.index - beta.index;\n}\n\n/**\n * @description Sort function to sort ranges by start position.\n * @param {Range} alpha\n * @param {Range} beta\n * @returns {number}\n */\nfunction sortByRangeStart(alpha: Range, beta: Range): number {\n return alpha.start - beta.start;\n}\n\nexport class Ranges extends Array<Range> {\n /**\n * @description Header name or type\n */\n public type = \"\";\n /**\n * @description Return plain JavaScript array with 'type' property\n * @returns {Array<Range>}\n */\n public toArray(): Array<Range> {\n const array = Array.from(this) as Ranges;\n array.type = this.type;\n return array;\n }\n}\n\nfunction csvToRanges(csv: string[], size: number): Ranges {\n const ranges = new Ranges();\n for (const item of csv) {\n const range = item.split(\"-\");\n let start = Number.parseInt(range[0], 10);\n let end = Number.parseInt(range[1], 10);\n if (Number.isNaN(start)) {\n start = size - end;\n end = size - 1;\n } else if (Number.isNaN(end)) {\n end = size - 1;\n }\n if (end > size - 1) {\n end = size - 1;\n }\n if (Number.isNaN(start) || Number.isNaN(end) || start > end || start < 0) {\n continue;\n }\n ranges.push({ end: end, start: start });\n }\n return ranges;\n}\n\n/**\n * @description Parse \"Range\" header `text` relative to the given file `size`.\n * @param {number} size - Size\n * @param {string} header - Header string\n * @param {Options=} options - Options\n * @returns {Ranges|Result}\n * @throws {TypeError}\n */\nexport function parseRange(size: number, header: string, options?: Options): Ranges | Result {\n let throwError = true;\n if (options && \"throwError\" in options && options.throwError === false) {\n throwError = false;\n }\n if (!Number.isInteger(size)) {\n if (throwError) {\n throw new TypeError(`Argument 'size' must be an integer.`);\n } else {\n return ERROR_INVALID_ARGUMENT;\n }\n }\n if (typeof header !== \"string\") {\n if (throwError) {\n throw new TypeError(`Argument 'header' must be a string.`);\n } else {\n return ERROR_INVALID_ARGUMENT;\n }\n }\n const indexOfEqualSign = header.indexOf(\"=\");\n if (indexOfEqualSign === -1) {\n return ERROR_STRING_IS_NOT_HEADER;\n }\n const csv = header.slice(indexOfEqualSign + 1).split(\",\");\n const ranges = csvToRanges(csv, size);\n if (ranges.length < 1) {\n return ERROR_UNSATISFIABLE_RESULT;\n }\n ranges.typLine truncated
|
||||
"mappings": "8gBAAA,6IAgCO,GAAM,GAA8C,GAE9C,EAA4C,GAE5C,EAAkD,GAO/D,WAAuB,EAAwB,CAC7C,GAAM,GAAU,EAAO,IAAI,GAAc,KAAK,GAC1C,EAAQ,EACZ,OAAS,GAAQ,EAAG,EAAQ,EAAQ,OAAQ,IAAS,CACnD,GAAM,GAAwB,EAAQ,GAChC,EAA0B,EAAQ,GACxC,AAAI,EAAM,MAAQ,EAAQ,IAAM,EAC9B,EAAQ,EAAE,GAAS,EACV,EAAM,IAAM,EAAQ,KAC7B,GAAQ,IAAM,EAAM,IACpB,EAAQ,MAAQ,KAAK,IAAI,EAAQ,MAAO,EAAM,QAGlD,EAAQ,OAAS,EAAQ,EACzB,GAAM,GAAW,CAAC,GAAG,GAAS,KAAK,GAAkB,IAAI,GACzD,SAAS,KAAO,EAAO,KAChB,EAST,WAAsB,EAA+B,EAA+B,CAClF,MAAO,CAAE,IAAK,EAAM,IAAK,QAAO,MAAO,EAAM,OAQ/C,WAAyB,EAA8B,CACrD,MAAO,CAAE,IAAK,EAAM,IAAK,MAAO,EAAM,OASxC,WAA0B,EAAuB,EAA8B,CAC7E,MAAO,GAAM,MAAQ,EAAK,MAS5B,WAA0B,EAAc,EAAqB,CAC3D,MAAO,GAAM,MAAQ,EAAK,MAGrB,mBAAqB,MAAa,CAAlC,aArGP,CAqGO,oBAIE,UAAO,GAKP,SAAwB,CAC7B,GAAM,GAAQ,MAAM,KAAK,MACzB,SAAM,KAAO,KAAK,KACX,IAIX,WAAqB,EAAe,EAAsB,CACxD,GAAM,GAAS,GAAI,GACnB,OAAW,KAAQ,GAAK,CACtB,GAAM,GAAQ,EAAK,MAAM,KACrB,EAAQ,OAAO,SAAS,EAAM,GAAI,IAClC,EAAM,OAAO,SAAS,EAAM,GAAI,IAUpC,AATA,AAAI,OAAO,MAAM,GACf,GAAQ,EAAO,EACf,EAAM,EAAO,GACJ,OAAO,MAAM,IACtB,GAAM,EAAO,GAEX,EAAM,EAAO,GACf,GAAM,EAAO,GAEX,SAAO,MAAM,IAAU,OAAO,MAAM,IAAQ,EAAQ,GAAO,EAAQ,IAGvE,EAAO,KAAK,CAAE,IAAK,EAAK,MAAO,IAEjC,MAAO,GAWF,WAAoB,EAAc,EAAgB,EAAoC,CAC3F,GAAI,GAAa,GAIjB,GAHI,GAAW,cAAgB,IAAW,EAAQ,aAAe,IAC/D,GAAa,IAEX,CAAC,OAAO,UAAU,GAAO,CAC3B,GAAI,EACF,KAAM,IAAI,WAAU,uCAEpB,MAAO,GAGX,GAAI,MAAO,IAAW,SAAU,CAC9B,GAAI,EACF,KAAM,IAAI,WAAU,uCAEpB,MAAO,GAGX,GAAM,GAAmB,EAAO,QAAQ,KACxC,GAAI,IAAqB,GACvB,MAAO,GAET,GAAM,GAAM,EAAO,MAAM,EAAmB,GAAG,MAAM,KAC/C,EAAS,EAAY,EAAK,GAChC,MAAI,GAAO,OAAS,EACX,EAET,GAAO,KAAO,EAAO,MAAM,EAAG,GACvB,GAAW,EAAQ,QAAU,EAAc,GAAU",
|
||||
"names": []
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
export interface Range {
|
||||
end: number;
|
||||
start: number;
|
||||
}
|
||||
export interface Options {
|
||||
/**
|
||||
* @description The "combine" option can be set to `true`
|
||||
* and overlapping & adjacent ranges
|
||||
* will be combined into a single range.
|
||||
*/
|
||||
combine?: boolean;
|
||||
/**
|
||||
* @description Throw or suppress errors.
|
||||
*/
|
||||
throwError?: boolean;
|
||||
}
|
||||
export declare type ResultUnsatisfiable = -1;
|
||||
export declare type ResultInvalid = -2;
|
||||
export declare type ResultWrongArgument = -3;
|
||||
export declare type Result = ResultInvalid | ResultUnsatisfiable | ResultWrongArgument;
|
||||
export declare const ERROR_INVALID_ARGUMENT: ResultWrongArgument;
|
||||
export declare const ERROR_STRING_IS_NOT_HEADER: ResultInvalid;
|
||||
export declare const ERROR_UNSATISFIABLE_RESULT: ResultUnsatisfiable;
|
||||
export declare class Ranges extends Array<Range> {
|
||||
/**
|
||||
* @description Header name or type
|
||||
*/
|
||||
type: string;
|
||||
/**
|
||||
* @description Return plain JavaScript array with 'type' property
|
||||
* @returns {Array<Range>}
|
||||
*/
|
||||
toArray(): Array<Range>;
|
||||
}
|
||||
/**
|
||||
* @description Parse "Range" header `text` relative to the given file `size`.
|
||||
* @param {number} size - Size
|
||||
* @param {string} header - Header string
|
||||
* @param {Options=} options - Options
|
||||
* @returns {Ranges|Result}
|
||||
* @throws {TypeError}
|
||||
*/
|
||||
export declare function parseRange(size: number, header: string, options?: Options): Ranges | Result;
|
||||
+1
@@ -0,0 +1 @@
|
||||
var o=-3,R=-2,g=-1;function l(t){let e=t.map(c).sort(m),s=0;for(let a=1;a<e.length;a++){let r=e[a],n=e[s];r.start>n.end+1?e[++s]=r:r.end>n.end&&(n.end=r.end,n.index=Math.min(n.index,r.index))}e.length=s+1;let i=[...e].sort(f).map(d);return i.type=t.type,i}function c(t,e){return{end:t.end,index:e,start:t.start}}function d(t){return{end:t.end,start:t.start}}function f(t,e){return t.index-e.index}function m(t,e){return t.start-e.start}var u=class extends Array{constructor(){super(...arguments);this.type=""}toArray(){let e=Array.from(this);return e.type=this.type,e}};function p(t,e){let s=new u;for(let i of t){let a=i.split("-"),r=Number.parseInt(a[0],10),n=Number.parseInt(a[1],10);Number.isNaN(r)?(r=e-n,n=e-1):Number.isNaN(n)&&(n=e-1),n>e-1&&(n=e-1),!(Number.isNaN(r)||Number.isNaN(n)||r>n||r<0)&&s.push({end:n,start:r})}return s}function x(t,e,s){let i=!0;if(s&&"throwError"in s&&s.throwError===!1&&(i=!1),!Number.isInteger(t)){if(i)throw new TypeError("Argument 'size' must be an integer.");return o}if(typeof e!="string"){if(i)throw new TypeError("Argument 'header' must be a string.");return o}let a=e.indexOf("=");if(a===-1)return R;let r=e.slice(a+1).split(","),n=p(r,t);return n.length<1?g:(n.type=e.slice(0,a),s&&s.combine?l(n):n)}export{o as ERROR_INVALID_ARGUMENT,R as ERROR_STRING_IS_NOT_HEADER,g as ERROR_UNSATISFIABLE_RESULT,u as Ranges,x as parseRange};
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"version": 3,
|
||||
"sources": ["../src/index.ts"],
|
||||
"sourcesContent": ["interface RangeWithIndex {\n end: number;\n index: number;\n start: number;\n}\n\nexport interface Range {\n end: number;\n start: number;\n}\n\nexport interface Options {\n /**\n * @description The \"combine\" option can be set to `true`\n * and overlapping & adjacent ranges\n * will be combined into a single range.\n */\n combine?: boolean;\n /**\n * @description Throw or suppress errors.\n */\n throwError?: boolean;\n}\n\nexport type ResultUnsatisfiable = -1;\n\nexport type ResultInvalid = -2;\n\nexport type ResultWrongArgument = -3;\n\nexport type Result = ResultInvalid | ResultUnsatisfiable | ResultWrongArgument;\n\nexport const ERROR_INVALID_ARGUMENT: ResultWrongArgument = -3 as const;\n\nexport const ERROR_STRING_IS_NOT_HEADER: ResultInvalid = -2 as const;\n\nexport const ERROR_UNSATISFIABLE_RESULT: ResultUnsatisfiable = -1 as const;\n\n/**\n * @description Combine overlapping & adjacent ranges.\n * @param {Ranges} ranges\n * @returns {Ranges}\n */\nfunction combineRanges(ranges: Ranges): Ranges {\n const ordered = ranges.map(mapWithIndex).sort(sortByRangeStart);\n let order = 0;\n for (let index = 1; index < ordered.length; index++) {\n const range: RangeWithIndex = ordered[index];\n const current: RangeWithIndex = ordered[order];\n if (range.start > current.end + 1) {\n ordered[++order] = range;\n } else if (range.end > current.end) {\n current.end = range.end;\n current.index = Math.min(current.index, range.index);\n }\n }\n ordered.length = order + 1;\n const combined = [...ordered].sort(sortByRangeIndex).map(mapWithoutIndex) as Ranges;\n combined.type = ranges.type;\n return combined;\n}\n\n/**\n * @description Map function to add index value to ranges.\n * @param {RangeWithIndex} range\n * @param {number} index\n * @returns {RangeWithIndex}\n */\nfunction mapWithIndex(range: Range | RangeWithIndex, index: number): RangeWithIndex {\n return { end: range.end, index, start: range.start };\n}\n\n/**\n * @description Map function to remove index value from ranges.\n * @param {RangeWithIndex} range\n * @returns {Range}\n */\nfunction mapWithoutIndex(range: RangeWithIndex): Range {\n return { end: range.end, start: range.start };\n}\n\n/**\n * @description Sort function to sort ranges by index.\n * @param {RangeWithIndex} alpha\n * @param {RangeWithIndex} beta\n * @returns {number}\n */\nfunction sortByRangeIndex(alpha: RangeWithIndex, beta: RangeWithIndex): number {\n return alpha.index - beta.index;\n}\n\n/**\n * @description Sort function to sort ranges by start position.\n * @param {Range} alpha\n * @param {Range} beta\n * @returns {number}\n */\nfunction sortByRangeStart(alpha: Range, beta: Range): number {\n return alpha.start - beta.start;\n}\n\nexport class Ranges extends Array<Range> {\n /**\n * @description Header name or type\n */\n public type = \"\";\n /**\n * @description Return plain JavaScript array with 'type' property\n * @returns {Array<Range>}\n */\n public toArray(): Array<Range> {\n const array = Array.from(this) as Ranges;\n array.type = this.type;\n return array;\n }\n}\n\nfunction csvToRanges(csv: string[], size: number): Ranges {\n const ranges = new Ranges();\n for (const item of csv) {\n const range = item.split(\"-\");\n let start = Number.parseInt(range[0], 10);\n let end = Number.parseInt(range[1], 10);\n if (Number.isNaN(start)) {\n start = size - end;\n end = size - 1;\n } else if (Number.isNaN(end)) {\n end = size - 1;\n }\n if (end > size - 1) {\n end = size - 1;\n }\n if (Number.isNaN(start) || Number.isNaN(end) || start > end || start < 0) {\n continue;\n }\n ranges.push({ end: end, start: start });\n }\n return ranges;\n}\n\n/**\n * @description Parse \"Range\" header `text` relative to the given file `size`.\n * @param {number} size - Size\n * @param {string} header - Header string\n * @param {Options=} options - Options\n * @returns {Ranges|Result}\n * @throws {TypeError}\n */\nexport function parseRange(size: number, header: string, options?: Options): Ranges | Result {\n let throwError = true;\n if (options && \"throwError\" in options && options.throwError === false) {\n throwError = false;\n }\n if (!Number.isInteger(size)) {\n if (throwError) {\n throw new TypeError(`Argument 'size' must be an integer.`);\n } else {\n return ERROR_INVALID_ARGUMENT;\n }\n }\n if (typeof header !== \"string\") {\n if (throwError) {\n throw new TypeError(`Argument 'header' must be a string.`);\n } else {\n return ERROR_INVALID_ARGUMENT;\n }\n }\n const indexOfEqualSign = header.indexOf(\"=\");\n if (indexOfEqualSign === -1) {\n return ERROR_STRING_IS_NOT_HEADER;\n }\n const csv = header.slice(indexOfEqualSign + 1).split(\",\");\n const ranges = csvToRanges(csv, size);\n if (ranges.length < 1) {\n return ERROR_UNSATISFIABLE_RESULT;\n }\n ranges.typLine truncated
|
||||
"mappings": "AAgCO,GAAM,GAA8C,GAE9C,EAA4C,GAE5C,EAAkD,GAO/D,WAAuB,EAAwB,CAC7C,GAAM,GAAU,EAAO,IAAI,GAAc,KAAK,GAC1C,EAAQ,EACZ,OAAS,GAAQ,EAAG,EAAQ,EAAQ,OAAQ,IAAS,CACnD,GAAM,GAAwB,EAAQ,GAChC,EAA0B,EAAQ,GACxC,AAAI,EAAM,MAAQ,EAAQ,IAAM,EAC9B,EAAQ,EAAE,GAAS,EACV,EAAM,IAAM,EAAQ,KAC7B,GAAQ,IAAM,EAAM,IACpB,EAAQ,MAAQ,KAAK,IAAI,EAAQ,MAAO,EAAM,QAGlD,EAAQ,OAAS,EAAQ,EACzB,GAAM,GAAW,CAAC,GAAG,GAAS,KAAK,GAAkB,IAAI,GACzD,SAAS,KAAO,EAAO,KAChB,EAST,WAAsB,EAA+B,EAA+B,CAClF,MAAO,CAAE,IAAK,EAAM,IAAK,QAAO,MAAO,EAAM,OAQ/C,WAAyB,EAA8B,CACrD,MAAO,CAAE,IAAK,EAAM,IAAK,MAAO,EAAM,OASxC,WAA0B,EAAuB,EAA8B,CAC7E,MAAO,GAAM,MAAQ,EAAK,MAS5B,WAA0B,EAAc,EAAqB,CAC3D,MAAO,GAAM,MAAQ,EAAK,MAGrB,mBAAqB,MAAa,CAAlC,aArGP,CAqGO,oBAIE,UAAO,GAKP,SAAwB,CAC7B,GAAM,GAAQ,MAAM,KAAK,MACzB,SAAM,KAAO,KAAK,KACX,IAIX,WAAqB,EAAe,EAAsB,CACxD,GAAM,GAAS,GAAI,GACnB,OAAW,KAAQ,GAAK,CACtB,GAAM,GAAQ,EAAK,MAAM,KACrB,EAAQ,OAAO,SAAS,EAAM,GAAI,IAClC,EAAM,OAAO,SAAS,EAAM,GAAI,IAUpC,AATA,AAAI,OAAO,MAAM,GACf,GAAQ,EAAO,EACf,EAAM,EAAO,GACJ,OAAO,MAAM,IACtB,GAAM,EAAO,GAEX,EAAM,EAAO,GACf,GAAM,EAAO,GAEX,SAAO,MAAM,IAAU,OAAO,MAAM,IAAQ,EAAQ,GAAO,EAAQ,IAGvE,EAAO,KAAK,CAAE,IAAK,EAAK,MAAO,IAEjC,MAAO,GAWF,WAAoB,EAAc,EAAgB,EAAoC,CAC3F,GAAI,GAAa,GAIjB,GAHI,GAAW,cAAgB,IAAW,EAAQ,aAAe,IAC/D,GAAa,IAEX,CAAC,OAAO,UAAU,GAAO,CAC3B,GAAI,EACF,KAAM,IAAI,WAAU,uCAEpB,MAAO,GAGX,GAAI,MAAO,IAAW,SAAU,CAC9B,GAAI,EACF,KAAM,IAAI,WAAU,uCAEpB,MAAO,GAGX,GAAM,GAAmB,EAAO,QAAQ,KACxC,GAAI,IAAqB,GACvB,MAAO,GAET,GAAM,GAAM,EAAO,MAAM,EAAmB,GAAG,MAAM,KAC/C,EAAS,EAAY,EAAK,GAChC,MAAI,GAAO,OAAS,EACX,EAET,GAAO,KAAO,EAAO,MAAM,EAAG,GACvB,GAAW,EAAQ,QAAU,EAAc,GAAU",
|
||||
"names": []
|
||||
}
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
{
|
||||
"author": "Anton Trofimenko",
|
||||
"bugs": {
|
||||
"url": "https://github.com/r37r0m0d3l/header-range-parser/issues"
|
||||
},
|
||||
"contributors": [
|
||||
{
|
||||
"email": "r37r0m0d3l@protonmail.com",
|
||||
"name": "Anton Trofimenko"
|
||||
}
|
||||
],
|
||||
"description": "Range header field string parser.",
|
||||
"devDependencies": {
|
||||
"@types/node": "17.0.4",
|
||||
"@typescript-eslint/eslint-plugin": "5.8.0",
|
||||
"@typescript-eslint/parser": "5.8.0",
|
||||
"esbuild": "0.14.8",
|
||||
"eslint": "8.5.0",
|
||||
"eslint-config-prettier": "8.3.0",
|
||||
"eslint-config-standard": "16.0.3",
|
||||
"eslint-plugin-import": "2.25.3",
|
||||
"eslint-plugin-jsdoc": "37.4.0",
|
||||
"eslint-plugin-node": "11.1.0",
|
||||
"eslint-plugin-prettier": "4.0.0",
|
||||
"jest": "27.4.5",
|
||||
"npm-dts": "1.3.10",
|
||||
"prettier": "2.5.1",
|
||||
"typescript": "4.5.4"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=12.22.0"
|
||||
},
|
||||
"exports": {
|
||||
"import": "./dist/index.js",
|
||||
"require": "./dist/index.cjs",
|
||||
"default": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts"
|
||||
},
|
||||
"files": [
|
||||
"dist/index.cjs",
|
||||
"dist/index.cjs.map",
|
||||
"dist/index.d.ts",
|
||||
"dist/index.js",
|
||||
"dist/index.js.map"
|
||||
],
|
||||
"homepage": "https://github.com/r37r0m0d3l/header-range-parser",
|
||||
"keywords": [
|
||||
"header",
|
||||
"http",
|
||||
"parser",
|
||||
"range"
|
||||
],
|
||||
"license": "MIT",
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.js",
|
||||
"name": "header-range-parser",
|
||||
"packageManager": "npm@6",
|
||||
"private": false,
|
||||
"readmeFilename": "README.md",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/r37r0m0d3l/header-range-parser.git"
|
||||
},
|
||||
"scripts": {
|
||||
"lint": "eslint --config .eslintrc.json \"./src/**/*.ts\"",
|
||||
"test": "jest --collect-coverage=false",
|
||||
"test:coverage": "jest --collect-coverage=true",
|
||||
"build": "npm run build:prod",
|
||||
"build:test": "node ./build.cjs -- --minify=false --sourcemap=false",
|
||||
"build:dev": "node ./build.cjs -- --minify=false --sourcemap=true",
|
||||
"build:prod": "node ./build.cjs -- --minify=true --sourcemap=true",
|
||||
"package:upgrade": "npx npm-check --update"
|
||||
},
|
||||
"type": "module",
|
||||
"types": "./dist/index.d.ts",
|
||||
"version": "1.1.3"
|
||||
}
|
||||
Reference in new issue
Block a user