diff --git a/lib/node_modules/@stdlib/_tools/github/get/lib/resolve.js b/lib/node_modules/@stdlib/_tools/github/get/lib/resolve.js index 1f4a99ccfdc2..f0946c59b9bb 100644 --- a/lib/node_modules/@stdlib/_tools/github/get/lib/resolve.js +++ b/lib/node_modules/@stdlib/_tools/github/get/lib/resolve.js @@ -21,8 +21,8 @@ // MODULES // var logger = require( 'debug' ); -var parseHeader = require( 'parse-link-header' ); var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var parseHeader = require( '@stdlib/_tools/github/parse-link-header' ); var request = require( './request.js' ); var flatten = require( './flatten.js' ); var getOptions = require( './options.js' ); diff --git a/lib/node_modules/@stdlib/_tools/github/parse-link-header/README.md b/lib/node_modules/@stdlib/_tools/github/parse-link-header/README.md new file mode 100644 index 000000000000..e962470ac9fd --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/github/parse-link-header/README.md @@ -0,0 +1,109 @@ + + +# Parse Link Header + +> Parse a Link header. + +
+ +## Usage + +```javascript +var parseLinkHeader = require( '@stdlib/_tools/github/parse-link-header' ); +``` + +#### parseLinkHeader( header ) + +Parses a `Link` header string. + +```javascript +var out = parseLinkHeader( '; rel="next"' ); +// returns { 'next': { 'url': 'https://api.github.com/user/repos?page=2&per_page=1', 'page': '2', 'per_page': '1', 'rel': 'next' } } +``` + +If unable to parse a header, the function returns `null`. + +```javascript +var out = parseLinkHeader( 'beep; boop' ); +// returns null +``` + +
+ + + +
+ +## Notes + +- The implementation is intended for pragmatic parsing of pagination `Link` headers and is **not** a standards-complete general-purpose parser. + +- The function returns `null` if provided a first argument which is not a `string`. + + ```javascript + var out = parseLinkHeader( null ); + // returns null + ``` + +- The function returns `null` for header values longer than `2000` characters. + +
+ + + +
+ +## Examples + + + +```javascript +var parseLinkHeader = require( '@stdlib/_tools/github/parse-link-header' ); + +var out = parseLinkHeader( '; rel="next", ; rel="last"' ); +// returns { 'next': {...}, 'last': {...} } + +out = parseLinkHeader( '; rel="next prev"; title="next, page"' ); +// returns { 'next': {...}, 'prev': {...} } + +out = parseLinkHeader( 'beep; boop' ); +// returns null +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/_tools/github/parse-link-header/docs/usage.txt b/lib/node_modules/@stdlib/_tools/github/parse-link-header/docs/usage.txt new file mode 100644 index 000000000000..4f0402695a8f --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/github/parse-link-header/docs/usage.txt @@ -0,0 +1,5 @@ + +Usage: parseLinkHeader( header ) + +Parses a Link header and returns a mapping of link relations to parsed +link values. diff --git a/lib/node_modules/@stdlib/_tools/github/parse-link-header/examples/index.js b/lib/node_modules/@stdlib/_tools/github/parse-link-header/examples/index.js new file mode 100644 index 000000000000..804d81b649de --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/github/parse-link-header/examples/index.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var parseLinkHeader = require( './../lib' ); + + +// MAIN // + +var out = parseLinkHeader( '; rel="next", ; rel="last"' ); +console.log( out ); + +out = parseLinkHeader( '; rel="next prev"; title="next, page"' ); +console.log( out ); + +out = parseLinkHeader( 'beep; boop' ); +console.log( out ); diff --git a/lib/node_modules/@stdlib/_tools/github/parse-link-header/lib/index.js b/lib/node_modules/@stdlib/_tools/github/parse-link-header/lib/index.js new file mode 100644 index 000000000000..6fca9f6392ea --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/github/parse-link-header/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Parse a Link header. +* +* @module @stdlib/_tools/github/parse-link-header +* +* @example +* var parseLinkHeader = require( '@stdlib/_tools/github/parse-link-header' ); +* +* var out = parseLinkHeader( '; rel="next"' ); +* // returns { 'next': { 'url': 'https://api.github.com/user/repos?page=2', 'page': '2', 'rel': 'next' } } +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/_tools/github/parse-link-header/lib/main.js b/lib/node_modules/@stdlib/_tools/github/parse-link-header/lib/main.js new file mode 100644 index 000000000000..44d1d2076f70 --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/github/parse-link-header/lib/main.js @@ -0,0 +1,196 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var parseURL = require( 'url' ).parse; +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var trim = require( '@stdlib/string/base/trim' ); +var objectKeys = require( '@stdlib/utils/keys' ); + + +// VARIABLES // + +var MAX_LENGTH = 2000; + + +// FUNCTIONS // + +/** +* Splits a link header into individual link values. +* +* @private +* @param {string} str - header string +* @returns {StringArray} link values +*/ +function splitValues( str ) { + var values; + var quoted; + var angled; + var start; + var i; + + values = []; + start = 0; + quoted = false; + angled = false; + + for ( i = 0; i < str.length; i++ ) { + if ( str.charCodeAt( i ) === 34 ) { + quoted = !quoted; + } else if ( quoted === false ) { + if ( str.charCodeAt( i ) === 60 ) { + angled = true; + } else if ( str.charCodeAt( i ) === 62 ) { + angled = false; + } else if ( str.charCodeAt( i ) === 44 && angled === false ) { + values.push( str.substring( start, i ) ); + start = i + 1; + } + } + } + values.push( str.substring( start ) ); + return values; +} + +/** +* Removes wrapping double quotes from a string. +* +* @private +* @param {string} str - input string +* @returns {string} unwrapped string +*/ +function unwrap( str ) { + if ( + str.length >= 2 && + str.charCodeAt( 0 ) === 34 && + str.charCodeAt( str.length-1 ) === 34 + ) { + return str.substring( 1, str.length-1 ); + } + return str; +} + +/** +* Parses an individual link header value. +* +* @private +* @param {string} str - link value +* @returns {(Object|null)} parsed link or null +*/ +function parseValue( str ) { + var rels; + var out; + var url; + var idx; + var q; + var v; + var k; + var i; + + str = trim( str ); + if ( str.length === 0 || str.charCodeAt( 0 ) !== 60 ) { + return null; + } + idx = str.indexOf( '>' ); + if ( idx < 0 ) { + return null; + } + url = str.substring( 1, idx ); + out = { + 'url': url + }; + + q = parseURL( url, true ).query; + for ( k in q ) { + if ( hasOwnProp( q, k ) ) { + out[ k ] = q[ k ]; + } + } + str = str.substring( idx+1 ); + str = str.split( ';' ); + + for ( i = 0; i < str.length; i++ ) { + v = trim( str[ i ] ); + if ( v.length === 0 ) { + continue; + } + idx = v.indexOf( '=' ); + if ( idx < 0 ) { + continue; + } + k = trim( v.substring( 0, idx ) ); + v = unwrap( trim( v.substring( idx+1 ) ) ); + out[ k ] = v; + } + if ( typeof out.rel !== 'string' || out.rel.length === 0 ) { + return null; + } + rels = out.rel.split( /\s+/ ); + return { + 'rels': rels, + 'value': out + }; +} + + +// MAIN // + +/** +* Parses a Link header. +* +* @param {string} header - link header +* @returns {(Object|null)} parsed links or null +* +* @example +* var out = parseLinkHeader( '; rel="next"' ); +* // returns { 'next': { 'url': 'https://api.github.com/user/repos?page=2', 'page': '2', 'rel': 'next' } } +*/ +function parseLinkHeader( header ) { + var values; + var out; + var obj; + var i; + var j; + + if ( typeof header !== 'string' || header.length === 0 || header.length > MAX_LENGTH ) { + return null; + } + values = splitValues( header ); + out = {}; + for ( i = 0; i < values.length; i++ ) { + obj = parseValue( values[ i ] ); + if ( obj === null ) { + continue; + } + for ( j = 0; j < obj.rels.length; j++ ) { + out[ obj.rels[ j ] ] = obj.value; + } + } + if ( objectKeys( out ).length === 0 ) { + return null; + } + return out; +} + + +// EXPORTS // + +module.exports = parseLinkHeader; diff --git a/lib/node_modules/@stdlib/_tools/github/parse-link-header/package.json b/lib/node_modules/@stdlib/_tools/github/parse-link-header/package.json new file mode 100644 index 000000000000..238b26b7f266 --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/github/parse-link-header/package.json @@ -0,0 +1,63 @@ +{ + "name": "@stdlib/_tools/github/parse-link-header", + "version": "0.0.0", + "description": "Parse a Link header.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "tools", + "tool", + "github", + "git", + "gh", + "parse", + "link", + "header", + "headers", + "http", + "pagination" + ] +} diff --git a/lib/node_modules/@stdlib/_tools/github/parse-link-header/test/test.js b/lib/node_modules/@stdlib/_tools/github/parse-link-header/test/test.js new file mode 100644 index 000000000000..ff3c94a0c6eb --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/github/parse-link-header/test/test.js @@ -0,0 +1,129 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var parseLinkHeader = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof parseLinkHeader, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `null` if not provided a string', function test( t ) { + var values; + var i; + + values = [ + 3.14, + NaN, + true, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( parseLinkHeader( values[ i ] ), null, 'returns expected value when provided '+values[ i ] ); + } + t.end(); +}); + +tape( 'the function returns `null` if provided an empty header', function test( t ) { + t.strictEqual( parseLinkHeader( '' ), null, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns `null` if provided a malformed header', function test( t ) { + t.strictEqual( parseLinkHeader( 'beep; boop' ), null, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns `null` if provided a header exceeding the maximum length', function test( t ) { + var header; + var tail; + var i; + + header = '; rel="next", '; + tail = ''; + for ( i = 0; i < 2100; i++ ) { + tail += 'a'; + } + header += tail; + + t.strictEqual( header.length > 2000, true, 'test fixture exceeds maximum length' ); + t.strictEqual( parseLinkHeader( header ), null, 'returns expected value' ); + t.end(); +}); + +tape( 'the function parses paginated GitHub link headers', function test( t ) { + var header; + var out; + + header = '; rel="next", ; rel="last"'; + out = parseLinkHeader( header ); + + t.deepEqual( out, { + 'next': { + 'url': 'https://api.github.com/user/9287/repos?page=2&per_page=1', + 'page': '2', + 'per_page': '1', + 'rel': 'next' + }, + 'last': { + 'url': 'https://api.github.com/user/9287/repos?page=3&per_page=1', + 'page': '3', + 'per_page': '1', + 'rel': 'last' + } + }, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports multiple relation types for a single link value', function test( t ) { + var header; + var out; + + header = '; rel="prev previous"'; + out = parseLinkHeader( header ); + + t.strictEqual( out.prev, out.previous, 'relations reference the same value' ); + t.strictEqual( out.prev.page, '1', 'sets query parameter values' ); + t.strictEqual( out.prev.rel, 'prev previous', 'retains original relation string' ); + t.end(); +}); + +tape( 'the function preserves additional link parameters', function test( t ) { + var header; + var out; + + header = '; rel="next"; type="application/json"; title="next, page"'; + out = parseLinkHeader( header ); + + t.strictEqual( out.next.type, 'application/json', 'sets the media type' ); + t.strictEqual( out.next.title, 'next, page', 'sets the title' ); + t.end(); +}); diff --git a/package.json b/package.json index e168d891abb3..fd456a83c80b 100644 --- a/package.json +++ b/package.json @@ -155,7 +155,6 @@ "mathjax-node-sre": "^3.0.0", "mkdirp": "^0.5.1", "mustache": "^4.0.0", - "parse-link-header": "^1.0.1", "plato": "^1.5.0", "process": "^0.11.10", "proxyquire": "^2.0.0",