Socket
Socket
Sign inDemoInstall

postman-url-encoder

Package Overview
Dependencies
Maintainers
2
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

postman-url-encoder - npm Package Compare versions

Comparing version 2.1.3 to 3.0.0-beta.1

parser/index.js

331

encoder/encode-set.js

@@ -30,6 +30,3 @@ /**

const STRING = 'string',
FUNCTION = 'function',
QUERY_ENCODE_CHARS = [' ', '"', '#', '\'', '<', '>'],
const QUERY_ENCODE_CHARS = [' ', '"', '#', '\'', '<', '>'],
FRAGMENT_EXTEND_CHARS = [' ', '"', '<', '>', '`'],

@@ -47,3 +44,3 @@ PATH_EXTEND_CHARS = ['#', '?', '{', '}'],

function charCode (char) {
const code = (typeof char === STRING) ?
const code = (typeof char === 'string') ?
// get char code from string

@@ -74,3 +71,3 @@ char.charCodeAt(0) :

// iterate over fixed / known size set
encodeSet._set.forEach(function (encoded, index) {
encodeSet._set.forEach((encoded, index) => {
if (!encoded && chars[index]) {

@@ -86,7 +83,7 @@ // encode charCodeAt(index)

// check if the input characters are iterable or not
if (!(chars && typeof chars.forEach === FUNCTION)) {
if (!(chars && typeof chars.forEach === 'function')) {
return encodeSet;
}
chars.forEach(function (char) {
chars.forEach((char) => {
encodeSet.add(char);

@@ -100,187 +97,189 @@ });

* Represents a set of characters / bytes that should be percent-encoded.
*
* @constructor
* @param {CharSet} chars Character set to encode
*/
function EncodeSet (chars) {
class EncodeSet {
/**
* Indexes in Uint8Array represents char codes for characters to encode.
*
* Size: 128, ASCII range [0, 0x7F]
*
* where,
* 1 -> encode
* 0 -> don't encode
*
* @private
* @type {Uint8Array}
* @param {CharSet} chars Character set to encode
*/
this._set = new Uint8Array(0x80);
constructor (chars) {
/**
* Indexes in Uint8Array represents char codes for characters to encode.
*
* Size: 128, ASCII range [0, 0x7F]
*
* where,
* 1 -> encode
* 0 -> don't encode
*
* @private
* @type {Uint8Array}
*/
this._set = new Uint8Array(0x80);
// encode C0 control codes [00, 0x1F] AND 0x7F
this._set.fill(1, 0, 0x20); // 0 to 31
this._set[0x7F] = 1; // 127
// encode C0 control codes [00, 0x1F] AND 0x7F
this._set.fill(1, 0, 0x20); // 0 to 31
this._set[0x7F] = 1; // 127
/**
* A Boolean indicating whether or not this EncodeSet is sealed.
*
* @private
* @type {Boolean}
*/
this._sealed = false;
// extend this set with input characters
extendEncodeSet(this, chars);
}
/**
* A Boolean indicating whether or not this EncodeSet is sealed.
* Appends a new character to the EncodeSet.
*
* @private
* @type {Boolean}
* @example
* var xyzEncodeSet = new EncodeSet(['x', 'y', 'z'])
*
* xyzEncodeSet
* .add('X')
* .add(89) // Y
* .add(0x5a) // Z
*
* @param {Char} char Character or character code
* @returns {EncodeSet} Current EncodeSet
*/
this._sealed = false;
add (char) {
// bail out if the EncodeSet is sealed
if (this._sealed) {
return this;
}
// extend this set with input characters
extendEncodeSet(this, chars);
}
const code = charCode(char);
/**
* Appends a new character to the EncodeSet.
*
* @example
* var xyzEncodeSet = new EncodeSet(['x', 'y', 'z'])
*
* xyzEncodeSet
* .add('X')
* .add(89) // Y
* .add(0x5a) // Z
*
* @param {Char} char Character or character code
* @returns {EncodeSet} Current EncodeSet
*/
EncodeSet.prototype.add = function (char) {
// bail out if the EncodeSet is sealed
if (this._sealed) {
// ensure ASCII range
if (code < 0x80) {
this._set[code] = 1;
}
// chaining
return this;
}
const code = charCode(char);
/**
* Returns a boolean asserting whether the given char code will be encoded in
* the EncodeSet or not.
*
* @note Always encode C0 control codes in the range U+0000 to U+001F and U+007F
* Refer: https://infra.spec.whatwg.org/#c0-control
*
* @example
* var tildeEncodeSet = new EncodeSet(['~'])
*
* // returns true
* tildeEncodeSet.has('~'.charCodeAt(0))
*
* // returns false
* tildeEncodeSet.has(65) // A
*
* // returns true
* tildeEncodeSet.has(31) // \u001f (control character)
*
* @param {Number} code Character code
* @returns {Boolean} Returns true if the character with the specified char code
* exists in the EncodeSet; otherwise false
*/
has (code) {
// encode if not in ASCII range (-∞, 0) OR (127, ∞)
if (code < 0 || code > 0x7F) {
return true;
}
// ensure ASCII range
if (code < 0x80) {
this._set[code] = 1;
// encode if present in the set
return Boolean(this._set[code]);
}
// chaining
return this;
};
/**
* Returns a boolean asserting whether the given char code will be encoded in
* the EncodeSet or not.
*
* @note Always encode C0 control codes in the range U+0000 to U+001F and U+007F
* Refer: https://infra.spec.whatwg.org/#c0-control
*
* @example
* var tildeEncodeSet = new EncodeSet(['~'])
*
* // returns true
* tildeEncodeSet.has('~'.charCodeAt(0))
*
* // returns false
* tildeEncodeSet.has(65) // A
*
* // returns true
* tildeEncodeSet.has(31) // \u001f (control character)
*
* @param {Number} code Character code
* @returns {Boolean} Returns true if the character with the specified char code
* exists in the EncodeSet; otherwise false
*/
EncodeSet.prototype.has = function (code) {
// encode if not in ASCII range (-∞, 0) OR (127, ∞)
if (code < 0 || code > 0x7F) {
return true;
/**
* Creates a copy of the current EncodeSet.
*
* @example
* var set1 = new EncodeSet(['<', '>'])
* var set1Copy = set1.clone().add('=')
*
* @returns {EncodeSet} New EncodeSet instance
*/
clone () {
return new EncodeSet(this._set);
}
// encode if present in the set
return Boolean(this._set[code]);
};
/**
* Seals the current EncodeSet to prevent new characters being added to it.
*
* @example
* var set = new EncodeSet()
*
* set.add(95)
* set.has(95) // returns true
*
* set.seal()
* set.add(100)
* set.has(100) // returns false
*
* @returns {EncodeSet} Current EncodeSet
*/
seal () {
this._sealed = true;
/**
* Creates a copy of the current EncodeSet.
*
* @example
* var set1 = new EncodeSet(['<', '>'])
* var set1Copy = set1.clone().add('=')
*
* @returns {EncodeSet} New EncodeSet instance
*/
EncodeSet.prototype.clone = function () {
return new EncodeSet(this._set);
};
try {
// @note Cannot freeze array buffer views with elements.
// So, rely upon the alternative `Object.seal` method and avoid mutations
// via EncodeSet~add method.
// Also, sealed Uint8Array enumerates faster in V8!
Object.seal(this._set);
}
catch (_) {
// silently swallow exceptions
}
/**
* Seals the current EncodeSet to prevent new characters being added to it.
*
* @example
* var set = new EncodeSet()
*
* set.add(95)
* set.has(95) // returns true
*
* set.seal()
* set.add(100)
* set.has(100) // returns false
*
* @returns {EncodeSet} Current EncodeSet
*/
EncodeSet.prototype.seal = function () {
this._sealed = true;
try {
// @note Cannot freeze array buffer views with elements.
// So, rely upon the alternative `Object.seal` method and avoid mutations
// via EncodeSet~add method.
// Also, sealed Uint8Array enumerates faster in V8!
Object.seal(this._set);
return this;
}
catch (_) {
// silently swallow exceptions
}
return this;
};
/**
* Creates a new EncodeSet by extending the input EncodeSet with additional
* characters.
*
* @example
* var fooEncodeSet = new EncodeSet(['f', 'o'])
* var foobarEncodeSet = EncodeSet.extend(fooEncodeSet, new Set(['b', 'a', 'r']))
*
* @param {EncodeSet} encodeSet Instance of EncodeSet
* @param {CharSet} chars Character set to encode
* @returns {EncodeSet} Copy of given `encodeSet` with extended `chars`
* @throws {TypeError} Argument `encodeSet` must be of type {@link EncodeSet}
*/
static extend (encodeSet, chars) {
if (!EncodeSet.isEncodeSet(encodeSet)) {
throw new TypeError('Argument `encodeSet` must be EncodeSet');
}
/**
* Creates a new EncodeSet by extending the input EncodeSet with additional
* characters.
*
* @example
* var fooEncodeSet = new EncodeSet(['f', 'o'])
* var foobarEncodeSet = EncodeSet.extend(fooEncodeSet, new Set(['b', 'a', 'r']))
*
* @param {EncodeSet} encodeSet Instance of EncodeSet
* @param {CharSet} chars Character set to encode
* @returns {EncodeSet} Copy of given `encodeSet` with extended `chars`
* @throws {TypeError} Argument `encodeSet` must be of type {@link EncodeSet}
*/
EncodeSet.extend = function (encodeSet, chars) {
if (!EncodeSet.isEncodeSet(encodeSet)) {
throw new TypeError('Argument `encodeSet` must be EncodeSet');
// extend the cloned encodeSet to avoid mutations
return extendEncodeSet(encodeSet.clone(), chars);
}
// extend the cloned encodeSet to avoid mutations
return extendEncodeSet(encodeSet.clone(), chars);
};
/**
* Determines whether the input value is an EncodeSet or not.
*
* @example
* // returns true
* EncodeSet.isEncodeSet(new EncodeSet([40, 41]))
*
* // returns false
* EncodeSet.isEncodeSet(new Set([28, 05]))
*
* @param {*} value The value to be tested
* @returns {Boolean} true if the given value is an EncodeSet; otherwise, false
*/
static isEncodeSet (value) {
return Boolean(value) && (value instanceof EncodeSet);
}
}
/**
* Determines whether the input value is an EncodeSet or not.
*
* @example
* // returns true
* EncodeSet.isEncodeSet(new EncodeSet([40, 41]))
*
* // returns false
* EncodeSet.isEncodeSet(new Set([28, 05]))
*
* @param {*} value The value to be tested
* @returns {Boolean} true if the given value is an EncodeSet; otherwise, false
*/
EncodeSet.isEncodeSet = function (value) {
return Boolean(value) && (value instanceof EncodeSet);
};
const // eslint-disable-line one-var
var
/**

@@ -287,0 +286,0 @@ * The C0 control percent-encode set are the C0 controls and all code points

@@ -9,3 +9,3 @@ /**

* // returns 'xn--48jwgn17gdel797d.com'
* var hostname = encoder.encodeHost('郵便屋さん.com'])
* encoder.encodeHost('郵便屋さん.com')
*

@@ -23,3 +23,2 @@ * @example <caption>Using EncodeSet</caption>

*
*
* @module postman-url-encoder/encoder

@@ -91,3 +90,3 @@ * @see {@link https://url.spec.whatwg.org/#url-representation}

domainToASCII = (function () {
var domainToASCII = url.domainToASCII;
const domainToASCII = url.domainToASCII;

@@ -226,3 +225,3 @@ // @note In Electron v3.1.8, the Node.js native url.domainToASCII

var key = param.key,
let key = param.key,
value = param.value,

@@ -259,3 +258,3 @@ result;

function encodeQueryParams (params) {
var i,
let i,
j,

@@ -302,3 +301,2 @@ ii,

for (j = 0, jj = paramValue.length; j < jj; j++) {
notFirstParam && (result += AMPERSAND);

@@ -305,0 +303,0 @@ notFirstParam = true;

@@ -35,3 +35,3 @@ /**

* @private
* @param {Number} byte
* @param {Number} byte Byte
* @returns {Boolean}

@@ -72,3 +72,4 @@ */

function encodeCharCode (code) {
var hex = code.toString(16).toUpperCase();
let hex = code.toString(16).toUpperCase();
(hex.length === 1) && (hex = ZERO + hex);

@@ -91,3 +92,3 @@

function encode (value, encodeSet) {
var i,
let i,
ii,

@@ -94,0 +95,0 @@ charCode,

@@ -20,6 +20,6 @@ /**

const sdk = require('postman-collection'),
querystring = require('querystring'),
const querystring = require('querystring'),
legacy = require('./legacy'),
parser = require('./parser'),
encoder = require('./encoder'),

@@ -42,3 +42,5 @@ QUERY_ENCODE_SET = require('./encoder/encode-set').QUERY_ENCODE_SET,

QUERY_SEPARATOR = '?',
PARAMS_SEPARATOR = '&',
SEARCH_SEPARATOR = '#',
DOMAIN_SEPARATOR = '.',
AUTH_CREDENTIALS_SEPARATOR = '@',

@@ -79,3 +81,3 @@

function getUrlTill (url, urlPart) {
var result = '';
let result = '';

@@ -173,8 +175,8 @@ if (url.protocol) {

*
* @param {PostmanUrl|String} url
* @param {Boolean} disableEncoding
* @returns {Url}
* @param {PostmanUrl|String} url URL string or PostmanUrl object
* @param {Boolean} disableEncoding Turn encoding off
* @returns {Url} Node.js like parsed and encoded object
*/
function toNodeUrl (url, disableEncoding) {
var nodeUrl = {
let nodeUrl = {
protocol: null,

@@ -197,37 +199,45 @@ slashes: null,

authUser,
authPassword,
queryParams;
queryParams,
authPassword;
// convert URL string to PostmanUrl
if (typeof url === STRING) {
url = new sdk.Url(url);
}
// Check if PostmanUrl instance and prepare segments
if (url && url.constructor && url.constructor._postman_propertyName === 'Url') {
// @note getPath() always adds a leading '/', similar to Node.js API
pathname = url.getPath();
hostname = url.getHost().toLowerCase();
// bail out if given url is not a PostmanUrl instance
if (!sdk.Url.isUrl(url)) {
return nodeUrl;
}
if (url.query && url.query.count()) {
queryParams = url.getQueryString({ ignoreDisabled: true });
queryParams = disableEncoding ? queryParams : encoder.encodeQueryParam(queryParams);
// @note getPath() always adds a leading '/', similar to Node.js API
pathname = url.getPath();
hostname = url.getHost().toLowerCase();
// either all the params are disabled or a single param is like { key: '' } (http://localhost?)
// in that case, query separator ? must be included in the raw URL.
// @todo Add helper in SDK to handle this
if (queryParams === E) {
// check if there's any enabled param, if so, set queryString to empty string
// otherwise (all disabled), it will be set as undefined
queryParams = url.query.find(function (param) { return !(param && param.disabled); }) && E;
}
}
if (url.query && url.query.count()) {
queryParams = url.getQueryString({ ignoreDisabled: true });
queryParams = disableEncoding ? queryParams : encoder.encodeQueryParam(queryParams);
// either all the params are disabled or a single param is like { key: '' } (http://localhost?)
// in that case, query separator ? must be included in the raw URL.
// @todo Add helper in SDK to handle this
if (queryParams === E) {
// check if there's any enabled param, if so, set queryString to empty string
// otherwise (all disabled), it will be set as undefined
queryParams = url.query.find(function (param) { return !(param && param.disabled); }) && E;
if (url.auth) {
authUser = url.auth.user;
authPassword = url.auth.password;
}
}
// Parser URL string and prepare segments
else if (typeof url === STRING) {
url = parser.parse(url);
if (url.auth) {
authUser = url.auth.user;
authPassword = url.auth.password;
pathname = PATH_SEPARATOR + (url.path || []).join(PATH_SEPARATOR);
hostname = (url.host || []).join(DOMAIN_SEPARATOR).toLowerCase();
queryParams = url.query && (queryParams = url.query.join(PARAMS_SEPARATOR)) &&
(disableEncoding ? queryParams : encoder.encodeQueryParam(queryParams));
authUser = (url.auth || [])[0];
authPassword = (url.auth || [])[1];
}
// bail out with empty URL object for invalid input
else {
return nodeUrl;
}

@@ -346,3 +356,3 @@ // @todo Add helper in SDK to normalize port

var i,
let i,
ii,

@@ -358,3 +368,3 @@ index,

for (i = 0, ii = requiredProps.length; i < ii; i++) {
if (!base.hasOwnProperty(requiredProps[i])) {
if (!Object.hasOwnProperty.call(base, requiredProps[i])) {
return relative;

@@ -361,0 +371,0 @@ }

@@ -1,2 +0,1 @@

/* istanbul ignore file */
var url = require('url'),

@@ -56,3 +55,2 @@

encoder = {
/**

@@ -64,5 +62,7 @@ * Percent encode a character with given code.

*/
percentEncode: function (c) {
percentEncode (c) {
var hex = c.toString(16).toUpperCase();
(hex.length === 1) && (hex = ZERO + hex);
return PERCENT + hex;

@@ -74,7 +74,7 @@ },

*
* @param {Buffer} buffer
* @param {Number} i
* @param {Buffer} buffer -
* @param {Number} i -
* @returns {Boolean}
*/
isPreEncoded: function (buffer, i) {
isPreEncoded (buffer, i) {
// If it is % check next two bytes for percent encode characters

@@ -91,6 +91,6 @@ // looking for pattern %00 - %FF

*
* @param {Number} byte
* @param {Number} byte -
* @returns {Boolean}
*/
isPreEncodedCharacter: function (byte) {
isPreEncodedCharacter (byte) {
return (byte >= 0x30 && byte <= 0x39) || // 0-9

@@ -104,6 +104,6 @@ (byte >= 0x41 && byte <= 0x46) || // A-F

*
* @param {Number} byte
* @param {Number} byte -
* @returns {Boolean}
*/
charactersToPercentEncode: function (byte) {
charactersToPercentEncode (byte) {
return (byte < 0x23 || byte > 0x7E || // Below # and after ~

@@ -123,6 +123,6 @@ byte === 0x3C || byte === 0x3E || // > and <

*
* @param {String} value
* @param {String} value -
* @returns {String}
*/
encode: function (value) {
encode (value) {
if (!value) { return E; }

@@ -136,3 +136,2 @@

for (i = 0, ii = buffer.length; i < ii; ++i) {
if (encoder.charactersToPercentEncode(buffer[i]) && !encoder.isPreEncoded(buffer, i)) {

@@ -142,3 +141,3 @@ ret += encoder.percentEncode(buffer[i]);

else {
ret += String.fromCodePoint(buffer[i]); // Only works in ES6 (available in Node v4+)
ret += String.fromCodePoint(buffer[i]); // Only works in ES6 (available in Node v4+)
}

@@ -155,3 +154,3 @@ }

* @private
* @param {String} string
* @param {String} string -
* @returns {*}

@@ -195,3 +194,3 @@ */

* @private
* @param {Object[]} parameters
* @param {Object[]} parameters -
* @returns {string}

@@ -224,3 +223,3 @@ */

* @private
* @param {String} urlString
* @param {String} urlString -
* @returns {Url}

@@ -227,0 +226,0 @@ */

{
"name": "postman-url-encoder",
"version": "3.0.0-beta.1",
"description": "Implementation of the WHATWG URL Standard",
"author": "Postman Labs <help@getpostman.com>",
"version": "2.1.3",
"author": "Postman Inc.",
"license": "Apache-2.0",
"main": "index.js",
"homepage": "https://github.com/postmanlabs/postman-url-encoder#readme",
"bugs": {
"url": "https://github.com/postmanlabs/postman-url-encoder/issues",
"email": "help@postman.com"
},
"repository": {
"type": "git",
"url": "git+https://github.com/postmanlabs/postman-url-encoder.git"
},
"keywords": [
"postman",
"url-encoder",
"url-parser",
"whatwg-url"
],
"repository": {
"type": "git",
"url": "git+https://github.com/postmanlabs/postman-url-encoder.git"
},
"bugs": {
"url": "https://github.com/postmanlabs/postman-url-encoder/issues"
},
"homepage": "https://github.com/postmanlabs/postman-url-encoder#readme",
"main": "index.js",
"scripts": {
"build-docs": "node npm/build-docs.js",
"publish-docs": "node npm/publish-docs.js",
"release": "node npm/create-release.js",
"test": "npm run test-lint && npm run test-unit",
"test-benchmark": "node npm/test-benchmark.js",
"test-lint": "node npm/test-lint.js",
"test-unit": "node npm/test-unit.js",
"test-benchmark": "node npm/test-benchmark.js"
"test-system": "node npm/test-system.js",
"test-unit": "nyc --nycrc-path=.nycrc.js node npm/test-unit.js"
},
"dependencies": {
"postman-collection": "^3.6.4",
"punycode": "^2.1.1"

@@ -35,17 +37,26 @@ },

"@postman/csv-parse": "^4.0.2",
"@postman/shipit": "0.1.0",
"async": "^3.1.1",
"bipbip": "^0.4.1",
"chai": "^4.2.0",
"chalk": "^2.4.2",
"chalk": "4.1.0",
"colors": "^1.4.0",
"eslint": "^5.16.0",
"eslint-plugin-jsdoc": "^18.11.0",
"editorconfig": "^0.15.3",
"eslint": "^7.10.0",
"eslint-plugin-jsdoc": "^30.6.2",
"eslint-plugin-lodash": "^7.1.0",
"eslint-plugin-mocha": "^8.0.0",
"eslint-plugin-security": "^1.4.0",
"jsdoc": "^3.6.5",
"mocha": "^7.2.0",
"nyc": "^14.1.1",
"postman-jsdoc-theme": "0.0.3",
"jsdoc": "^3.6.6",
"mocha": "^8.1.3",
"nyc": "^15.1.0",
"parse-gitignore": "^1.0.1",
"postman-collection": "^3.6.7",
"postman-jsdoc-theme": "^0.0.3",
"recursive-readdir": "^2.2.2",
"shelljs": "^0.8.3"
},
"engines": {
"node": ">=10"
}
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc