GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - pillarjs/encodeurl: Encode a URL to a percent-encoded form, excluding already-encoded sequences

BranchesTags
Open more actions menu

Latest commit

 

History

58 Commits
58 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
src
src
 
 
.editorconfig
.editorconfig
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
package-lock.json
package-lock.json
 
 
package.json
package.json
 
 
tsconfig.build.json
tsconfig.build.json
 
 
tsconfig.json
tsconfig.json
 
 

Repository files navigation

Encode URL

NPM Version NPM Downloads Build Status Test Coverage

Encode a URL to a percent-encoded form, excluding already-encoded sequences.

Installation

npm install encodeurl

API

import { encodeUrl } from "encodeurl";

Requires Node.js 22 or later. TypeScript declarations are included.

encodeUrl(url)

Encode a URL to a percent-encoded form, excluding already-encoded sequences.

This function accepts a URL and encodes all the non-URL code points (as UTF-8 byte sequences). It will not encode the "%" character unless it is not part of a valid sequence (%20 will be left as-is, but %foo will be encoded as %25foo).

This encode is meant to be "safe" and does not throw errors. It will try as hard as it can to properly encode the given URL, including replacing any raw, unpaired surrogate pairs with the Unicode replacement character prior to encoding.

Examples

Encode a URL containing user-controlled data

import { encodeUrl } from "encodeurl";
import escapeHtml from "escape-html";

http.createServer(function onRequest(req, res) {
  // get encoded form of inbound url
  var url = encodeUrl(req.url);

  // create html message
  var body = "<p>Location " + escapeHtml(url) + " not found</p>";

  // send a 404
  res.statusCode = 404;
  res.setHeader("Content-Type", "text/html; charset=UTF-8");
  res.setHeader("Content-Length", String(Buffer.byteLength(body, "utf-8")));
  res.end(body, "utf-8");
});

Encode a URL for use in a header field

import { encodeUrl } from "encodeurl";
import escapeHtml from "escape-html";
import * as url from "node:url";

http.createServer(function onRequest(req, res) {
  // parse inbound url
  var href = url.parse(req);

  // set new host for redirect
  href.host = "localhost";
  href.protocol = "https:";
  href.slashes = true;

  // create location header
  var location = encodeUrl(url.format(href));

  // create html message
  var body = "<p>Redirecting to new site: " + escapeHtml(location) + "</p>";

  // send a 301
  res.statusCode = 301;
  res.setHeader("Content-Type", "text/html; charset=UTF-8");
  res.setHeader("Content-Length", String(Buffer.byteLength(body, "utf-8")));
  res.setHeader("Location", location);
  res.end(body, "utf-8");
});

Similarities

This function is similar to the intrinsic function encodeURI. However, it will not encode:

  • The \, ^, or | characters
  • The % character when it's part of a valid sequence
  • [ and ] (for IPv6 hostnames)
  • Replaces raw, unpaired surrogate pairs with the Unicode replacement character

As a result, the encoding aligns closely with the behavior in the WHATWG URL specification. However, this package only encodes strings and does not do any URL parsing or formatting.

It is expected that any output from new URL(url) will not change when used with this package, as the output has already been encoded. Additionally, if we were to encode before new URL(url), we do not expect the before and after encoded formats to be parsed any differently.

Testing

$ npm install
$ npm test
$ npm run build

References

  • RFC 3986: Uniform Resource Identifier (URI): Generic Syntax
  • WHATWG URL Living Standard

License

MIT

About

Encode a URL to a percent-encoded form, excluding already-encoded sequences

Resources

Readme
MIT license

Code of conduct

Code of conduct

Security policy

Security policy
Activity
Custom properties

Stars

40 stars

Watchers

6 watching

Forks

24 forks
Report repository

Releases

Sponsor this project

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.