Skip to content

Commit

Permalink
Merge branch 'main' into decode_case_insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
tugrul committed Jan 18, 2023
2 parents 766be66 + baa6683 commit dec878d
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 42 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: run-checks

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
- name: Install dependencies
run: npm install
- name: Build
run: npm run build
- name: Test
run: npm test
37 changes: 37 additions & 0 deletions .github/workflows/publish-on-tag.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: publish-on-tag

on:
push:
tags:
- '*'

jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
- name: Install dependencies
run: npm install
- name: Build
run: npm run build
- name: Test
run: npm test
- name: Publish punycode package
env:
NPM_TOKEN: ${{secrets.NPM_TOKEN_PUNYCODE}}
run: |
npm config set registry 'https://wombat-dressing-room.appspot.com/'
npm config set '//wombat-dressing-room.appspot.com/:_authToken' '${NPM_TOKEN}'
npm publish
- name: Publish punycode.js package
env:
NPM_TOKEN: ${{secrets.NPM_TOKEN_PUNYCODE_JS}}
run: |
npm config set registry 'https://wombat-dressing-room.appspot.com/'
npm config set '//wombat-dressing-room.appspot.com/:_authToken' '${NPM_TOKEN}'
npm publish
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
14
11 changes: 0 additions & 11 deletions .travis.yml

This file was deleted.

10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Punycode.js [![Build status](https://travis-ci.org/bestiejs/punycode.js.svg?branch=master)](https://travis-ci.org/bestiejs/punycode.js) [![Code coverage status](http://img.shields.io/codecov/c/github/bestiejs/punycode.js.svg)](https://codecov.io/gh/bestiejs/punycode.js) [![Dependency status](https://gemnasium.com/bestiejs/punycode.js.svg)](https://gemnasium.com/bestiejs/punycode.js)
# Punycode.js [![punycode on npm](https://img.shields.io/npm/v/punycode)](https://www.npmjs.com/package/emoji-test-regex-pattern) [![](https://data.jsdelivr.com/v1/package/npm/punycode/badge)](https://www.jsdelivr.com/package/npm/punycode)

Punycode.js is a robust Punycode converter that fully complies to [RFC 3492](https://tools.ietf.org/html/rfc3492) and [RFC 5891](https://tools.ietf.org/html/rfc5891).

Expand All @@ -12,7 +12,7 @@ This JavaScript library is the result of comparing, optimizing and documenting d

This project was [bundled](https://github.com/joyent/node/blob/master/lib/punycode.js) with Node.js from [v0.6.2+](https://github.com/joyent/node/compare/975f1930b1...61e796decc) until [v7](https://github.com/nodejs/node/pull/7941) (soft-deprecated).

The current version supports recent versions of Node.js only. It provides a CommonJS module and an ES6 module. For the old version that offers the same functionality with broader support, including Rhino, Ringo, Narwhal, and web browsers, see [v1.4.1](https://github.com/bestiejs/punycode.js/releases/tag/v1.4.1).
This project provides a CommonJS module that uses ES2015+ features and JavaScript module, which work in modern Node.js versions and browsers. For the old Punycode.js version that offers the same functionality in a UMD build with support for older pre-ES2015 runtimes, including Rhino, Ringo, and Narwhal, see [v1.4.1](https://github.com/mathiasbynens/punycode.js/releases/tag/v1.4.1).

## Installation

Expand All @@ -24,8 +24,12 @@ npm install punycode --save

In [Node.js](https://nodejs.org/):

> ⚠️ Note that userland modules don't hide core modules.
> For example, `require('punycode')` still imports the deprecated core module even if you executed `npm install punycode`.
> Use `require('punycode/')` to import userland modules rather than core modules.
```js
const punycode = require('punycode');
const punycode = require('punycode/');
```

## API
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "punycode",
"version": "2.1.1",
"version": "2.2.1",
"description": "A robust Punycode converter that fully complies to RFC 3492 and RFC 5891, and works on nearly all JavaScript platforms.",
"homepage": "https://mths.be/punycode",
"main": "punycode.js",
Expand Down Expand Up @@ -31,22 +31,22 @@
],
"repository": {
"type": "git",
"url": "https://github.com/bestiejs/punycode.js.git"
"url": "https://github.com/mathiasbynens/punycode.js.git"
},
"bugs": "https://github.com/bestiejs/punycode.js/issues",
"bugs": "https://github.com/mathiasbynens/punycode.js/issues",
"files": [
"LICENSE-MIT.txt",
"punycode.js",
"punycode.es6.js"
],
"scripts": {
"test": "mocha tests",
"prepublish": "node scripts/prepublish.js"
"build": "node scripts/prepublish.js"
},
"devDependencies": {
"codecov": "^1.0.1",
"istanbul": "^0.4.1",
"mocha": "^2.5.3"
"mocha": "^10.2.0"
},
"jspm": {
"map": {
Expand Down
45 changes: 24 additions & 21 deletions punycode.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const delimiter = '-'; // '\x2D'

/** Regular expressions */
const regexPunycode = /^xn--/i;
const regexNonASCII = /[^\0-\x7E]/; // non-ASCII chars
const regexNonASCII = /[^\0-\x7F]/; // Note: U+007F DEL is excluded too.
const regexSeparators = /[\x2E\u3002\uFF0E\uFF61]/g; // RFC 3490 separators

/** Error messages */
Expand Down Expand Up @@ -50,11 +50,11 @@ function error(type) {
* item.
* @returns {Array} A new array of values returned by the callback function.
*/
function map(array, fn) {
function map(array, callback) {
const result = [];
let length = array.length;
while (length--) {
result[length] = fn(array[length]);
result[length] = callback(array[length]);
}
return result;
}
Expand All @@ -66,22 +66,22 @@ function map(array, fn) {
* @param {String} domain The domain name or email address.
* @param {Function} callback The function that gets called for every
* character.
* @returns {Array} A new string of characters returned by the callback
* @returns {String} A new string of characters returned by the callback
* function.
*/
function mapDomain(string, fn) {
const parts = string.split('@');
function mapDomain(domain, callback) {
const parts = domain.split('@');
let result = '';
if (parts.length > 1) {
// In email addresses, only the domain name should be punycoded. Leave
// the local part (i.e. everything up to `@`) intact.
result = parts[0] + '@';
string = parts[1];
domain = parts[1];
}
// Avoid `split(regex)` for IE8 compatibility. See #17.
string = string.replace(regexSeparators, '\x2E');
const labels = string.split('.');
const encoded = map(labels, fn).join('.');
domain = domain.replace(regexSeparators, '\x2E');
const labels = domain.split('.');
const encoded = map(labels, callback).join('.');
return result + encoded;
}

Expand Down Expand Up @@ -130,7 +130,7 @@ function ucs2decode(string) {
* @param {Array} codePoints The array of numeric code points.
* @returns {String} The new Unicode string (UCS-2).
*/
const ucs2encode = array => String.fromCodePoint(...array);
const ucs2encode = codePoints => String.fromCodePoint(...codePoints);

/**
* Converts a basic code point into a digit/integer.
Expand All @@ -142,13 +142,13 @@ const ucs2encode = array => String.fromCodePoint(...array);
* the code point does not represent a value.
*/
const basicToDigit = function(codePoint) {
if (codePoint - 0x30 < 0x0A) {
return codePoint - 0x16;
if (codePoint >= 0x30 && codePoint < 0x3A) {
return 26 + (codePoint - 0x30);
}
if (codePoint - 0x41 < 0x1A) {
if (codePoint >= 0x41 && codePoint < 0x5B) {
return codePoint - 0x41;
}
if (codePoint - 0x61 < 0x1A) {
if (codePoint >= 0x61 && codePoint < 0x7B) {
return codePoint - 0x61;
}
return base;
Expand Down Expand Up @@ -228,7 +228,7 @@ const decode = function(input) {
// which gets added to `i`. The overflow checking is easier
// if we increase `i` as we go, then subtract off its starting
// value at the end to obtain `delta`.
let oldi = i;
const oldi = i;
for (let w = 1, k = base; /* no condition */; k += base) {

if (index >= inputLength) {
Expand All @@ -237,7 +237,10 @@ const decode = function(input) {

const digit = basicToDigit(input.charCodeAt(index++));

if (digit >= base || digit > floor((maxInt - i) / w)) {
if (digit >= base) {
error('invalid-input');
}
if (digit > floor((maxInt - i) / w)) {
error('overflow');
}

Expand Down Expand Up @@ -291,7 +294,7 @@ const encode = function(input) {
input = ucs2decode(input);

// Cache the length.
let inputLength = input.length;
const inputLength = input.length;

// Initialize the state.
let n = initialN;
Expand All @@ -305,7 +308,7 @@ const encode = function(input) {
}
}

let basicLength = output.length;
const basicLength = output.length;
let handledCPCount = basicLength;

// `handledCPCount` is the number of code points that have been handled;
Expand Down Expand Up @@ -342,7 +345,7 @@ const encode = function(input) {
if (currentValue < n && ++delta > maxInt) {
error('overflow');
}
if (currentValue == n) {
if (currentValue === n) {
// Represent delta as a generalized variable-length integer.
let q = delta;
for (let k = base; /* no condition */; k += base) {
Expand All @@ -359,7 +362,7 @@ const encode = function(input) {
}

output.push(stringFromCharCode(digitToBasic(q, 0)));
bias = adapt(delta, handledCPCountPlusOne, handledCPCount == basicLength);
bias = adapt(delta, handledCPCountPlusOne, handledCPCount === basicLength);
delta = 0;
++handledCPCount;
}
Expand Down
16 changes: 14 additions & 2 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const testData = {
* `b1abfaaepdrnnbgefbaDotcwatmq2g4l`
* Without mixed-case annotation it has to encode to:
* `b1abfaaepdrnnbgefbadotcwatmq2g4l`
* https://github.com/bestiejs/punycode.js/issues/3
* https://github.com/mathiasbynens/punycode.js/issues/3
*/
{
'description': 'Russian (Cyrillic)',
Expand Down Expand Up @@ -178,7 +178,7 @@ const testData = {
'decoded': 'ma\xF1ana.com',
'encoded': 'xn--maana-pta.com'
},
{ // https://github.com/bestiejs/punycode.js/issues/17
{ // https://github.com/mathiasbynens/punycode.js/issues/17
'decoded': 'example.com.',
'encoded': 'example.com.'
},
Expand Down Expand Up @@ -220,6 +220,10 @@ const testData = {
'description': 'Email address',
'decoded': '\u0434\u0436\u0443\u043C\u043B\u0430@\u0434\u0436p\u0443\u043C\u043B\u0430\u0442\u0435\u0441\u0442.b\u0440\u0444a',
'encoded': '\u0434\u0436\u0443\u043C\u043B\u0430@xn--p-8sbkgc5ag7bhce.xn--ba-lmcq'
},
{ // https://github.com/mathiasbynens/punycode.js/pull/115
'decoded': 'foo\x7F.example',
'encoded': 'foo\x7F.example'
}
],
'separators': [
Expand Down Expand Up @@ -303,6 +307,14 @@ describe('punycode.decode', function() {
it('handles uppercase Z', function() {
assert.deepEqual(punycode.decode('ZZZ'), '\u7BA5');
});
it('throws RangeError: Invalid input', function() {
assert.throws(
function() {
punycode.decode('ls8h=');
},
RangeError
);
});
});

describe('punycode.encode', function() {
Expand Down

0 comments on commit dec878d

Please sign in to comment.