encodeurl

tag encodeurl-ci

Encode a URL to a percent-encoded form, excluding already-encoded sequences. Build for Deno, compatible with Browser. Based on https://github.com/pillarjs/encodeurl.

API

import { encodeUrl } from "https://deno.land/x/encodeurl/mod.ts";

encodeUrl(url: string): string

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

This function will take an already-encoded URL and encode all the non-URL code points (as UTF-8 byte sequences). This function 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.

This function is similar to the intrinsic function encodeURI, except it will not encode the % character if that is part of a valid sequence, will not encode [ and ] (for IPv6 hostnames) and will replace raw, unpaired surrogate pairs with the Unicode replacement character (instead of throwing).

Examples

Encode a URL containing user-controled data

import {
  serve,
  Response,
} from "https://deno.land/std/http/server.ts";
import { encodeUrl } from "https://deno.land/x/encodeurl/mod.ts";
import { escapeHtml } from "https://deno.land/x/escape_html/mod.ts";

const server = serve("127.0.0.1:3000");
console.log("Server listening on: 3000");

for await (const req of server) {
  const res: Response = {
    status: 404,
    body: `<p>Location ${escapeHtml(encodeUrl(req.url))} not found</p>`,
    headers: new Headers([
      ["Content-Type", "text/html; charset=UTF-8"],
    ]),
  };
  req.respond(res);
}

Encode a URL for use in a header field

import {
  serve,
  Response,
} from "https://deno.land/std/http/server.ts";

import { encodeUrl } from "https://deno.land/x/encodeurl/mod.ts";
import { escapeHtml } from "https://deno.land/x/escape_html/mod.ts";

const server = serve("127.0.0.1:3000");
console.log("Server listening on: 3000");

for await (const req of server) {
  const location = encodeUrl("https://www.google.com/");
  const res: Response = {
    status: 302,
    body: `<p>Redirecting to new site: ${escapeHtml(location)}</p>`,
    headers: new Headers([
      ["Content-Type", "text/html; charset=UTF-8"],
      ["Location", location],
    ]),
  };
  req.respond(res);
}

Testing

$ deno test

References

License

MIT