-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement domainToASCII and domainToUnicode
- Loading branch information
Showing
8 changed files
with
222 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export function domainToASCII(domain: string): string; | ||
export function domainToUnicode(domain: string): string; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */ | ||
import { default as urlUtil } from 'node-internal:url'; | ||
import { ERR_MISSING_ARGS } from 'node-internal:internal_errors'; | ||
|
||
const URL = globalThis.URL; | ||
const URLSearchParams = globalThis.URLSearchParams; | ||
|
||
function domainToASCII(domain: string): string { | ||
if (arguments.length < 1) { | ||
throw new ERR_MISSING_ARGS('domain'); | ||
} | ||
|
||
return urlUtil.domainToASCII(`${domain}`); | ||
} | ||
|
||
function domainToUnicode(domain: string): string { | ||
if (arguments.length < 1) { | ||
throw new ERR_MISSING_ARGS('domain'); | ||
} | ||
|
||
return urlUtil.domainToASCII(`${domain}`); | ||
} | ||
|
||
export { domainToASCII, domainToUnicode, URL, URLSearchParams }; | ||
|
||
export default { | ||
domainToASCII, | ||
domainToUnicode, | ||
URL, | ||
URLSearchParams, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright (c) 2017-2022 Cloudflare, Inc. | ||
// Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
// https://opensource.org/licenses/Apache-2.0 | ||
// | ||
// Adapted from Node.js. Copyright Joyent, Inc. and other Node contributors. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a | ||
// copy of this software and associated documentation files (the | ||
// "Software"), to deal in the Software without restriction, including | ||
// without limitation the rights to use, copy, modify, merge, publish, | ||
// distribute, sublicense, and/or sell copies of the Software, and to permit | ||
// persons to whom the Software is furnished to do so, subject to the | ||
// following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included | ||
// in all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | ||
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | ||
// USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
import { strictEqual, throws, ok as assert } from 'node:assert'; | ||
import { | ||
domainToASCII, | ||
domainToUnicode, | ||
URL as URLImpl, | ||
URLSearchParams as URLSearchParamsImpl, | ||
} from 'node:url'; | ||
|
||
// Tests are taken from: | ||
// https://github.com/nodejs/node/blob/321a14b36d6b3304aedfd183e12ddba35dc704bd/test/parallel/test-url-domain-ascii-unicode.js | ||
export const testDomainAsciiUnicode = { | ||
test() { | ||
const domainWithASCII = [ | ||
['ıíd', 'xn--d-iga7r'], | ||
['يٴ', 'xn--mhb8f'], | ||
['www.ϧƽəʐ.com', 'www.xn--cja62apfr6c.com'], | ||
['новини.com', 'xn--b1amarcd.com'], | ||
['名がドメイン.com', 'xn--v8jxj3d1dzdz08w.com'], | ||
['افغانستا.icom.museum', 'xn--mgbaal8b0b9b2b.icom.museum'], | ||
['الجزائر.icom.fake', 'xn--lgbbat1ad8j.icom.fake'], | ||
['भारत.org', 'xn--h2brj9c.org'], | ||
]; | ||
|
||
for (const pair of domainWithASCII) { | ||
const domain = pair[0]; | ||
const ascii = pair[1]; | ||
const domainConvertedToASCII = domainToASCII(domain); | ||
strictEqual(domainConvertedToASCII, ascii); | ||
const asciiConvertedToUnicode = domainToUnicode(ascii); | ||
strictEqual(asciiConvertedToUnicode, domain); | ||
} | ||
|
||
{ | ||
for (const [i, { ascii, unicode }] of tests.entries()) { | ||
strictEqual(ascii, domainToASCII(unicode), `domainToASCII(${i + 1})`); | ||
strictEqual( | ||
unicode, | ||
domainToUnicode(ascii), | ||
`domainToUnicode(${i + 1})` | ||
); | ||
strictEqual( | ||
ascii, | ||
domainToASCII(domainToUnicode(ascii)), | ||
`domainToASCII(domainToUnicode(${i + 1}))` | ||
); | ||
strictEqual( | ||
unicode, | ||
domainToUnicode(domainToASCII(unicode)), | ||
`domainToUnicode(domainToASCII(${i + 1}))` | ||
); | ||
} | ||
} | ||
}, | ||
}; | ||
|
||
// Tests are taken from: | ||
// https://github.com/nodejs/node/blob/321a14b36d6b3304aedfd183e12ddba35dc704bd/test/parallel/test-whatwg-url-custom-domainto.js | ||
export const whatwgURLCustomDomainTo = { | ||
test() { | ||
{ | ||
const expectedError = { code: 'ERR_MISSING_ARGS', name: 'TypeError' }; | ||
throws(() => domainToASCII(), expectedError); | ||
throws(() => domainToUnicode(), expectedError); | ||
strictEqual(domainToASCII(undefined), 'undefined'); | ||
strictEqual(domainToUnicode(undefined), 'undefined'); | ||
} | ||
}, | ||
}; | ||
|
||
export const urlAndSearchParams = { | ||
test() { | ||
assert(URLImpl, 'URL should be exported from node:url'); | ||
assert( | ||
URLSearchParamsImpl, | ||
'URLSearchParams should be exported by node:url' | ||
); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Workerd = import "/workerd/workerd.capnp"; | ||
|
||
const unitTests :Workerd.Config = ( | ||
services = [ | ||
( name = "nodejs-url-test", | ||
worker = ( | ||
modules = [ | ||
(name = "worker", esModule = embed "url-nodejs-test.js") | ||
], | ||
compatibilityDate = "2023-10-01", | ||
compatibilityFlags = ["nodejs_compat"], | ||
) | ||
), | ||
], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) 2017-2022 Cloudflare, Inc. | ||
// Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
// https://opensource.org/licenses/Apache-2.0 | ||
#include "url.h" | ||
|
||
#include <ada.h> | ||
|
||
namespace workerd::api::node { | ||
|
||
kj::ArrayPtr<const char> UrlUtil::domainToUnicode(kj::String domain) { | ||
if (domain.size() == 0) { | ||
return {}; | ||
} | ||
|
||
auto out = ada::parse<ada::url_aggregator>("ws://x"); | ||
JSG_REQUIRE(out.has_value(), Error, "URL parsing failed"_kj); | ||
if (!out->set_hostname({domain.begin(), domain.size()})) { | ||
return {}; | ||
} | ||
auto result = out->get_hostname(); | ||
return kj::ArrayPtr<const char>(result.data(), result.size()); | ||
} | ||
|
||
kj::ArrayPtr<const char> UrlUtil::domainToASCII(kj::String domain) { | ||
if (domain.size() == 0) { | ||
return {}; | ||
} | ||
|
||
auto out = ada::parse<ada::url_aggregator>("ws://x"); | ||
JSG_REQUIRE(out.has_value(), Error, "URL parsing failed"_kj); | ||
if (!out->set_hostname({domain.begin(), domain.size()})) { | ||
return {}; | ||
} | ||
|
||
auto result = ada::idna::to_unicode(out->get_hostname()); | ||
return kj::ArrayPtr<const char>(result.data(), result.size()); | ||
} | ||
|
||
} // namespace workerd::api::node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright (c) 2017-2022 Cloudflare, Inc. | ||
// Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
// https://opensource.org/licenses/Apache-2.0 | ||
#pragma once | ||
|
||
#include <kj/string.h> | ||
#include <workerd/jsg/jsg.h> | ||
|
||
namespace workerd::api::node { | ||
|
||
class UrlUtil final: public jsg::Object { | ||
public: | ||
UrlUtil() = default; | ||
UrlUtil(jsg::Lock&, const jsg::Url&) {} | ||
|
||
kj::ArrayPtr<const char> domainToUnicode(kj::String domain); | ||
kj::ArrayPtr<const char> domainToASCII(kj::String domain); | ||
|
||
JSG_RESOURCE_TYPE(UrlUtil) { | ||
JSG_METHOD(domainToUnicode); | ||
JSG_METHOD(domainToASCII); | ||
} | ||
}; | ||
|
||
#define EW_NODE_URL_ISOLATE_TYPES api::node::UrlUtil | ||
|
||
} // namespace workerd::api::node |