Skip to content

Commit

Permalink
fix(deps): import local punycode package instead of core (#26)
Browse files Browse the repository at this point in the history
There are two `punycode` packages available:

* [The cross-platform npm package](https://github.com/mathiasbynens/punycode.js)
* [The node deprecated native library](https://nodejs.org/api/punycode.html)

We wrote [`import * as punycode from 'punycode'`](https://github.com/Jigsaw-Code/outline-shadowsocksconfig/blob/v0.2.0/src/shadowsocks_config.ts#L17) (which is compiled to [`var punycode = require("punycode")`](https://github.com/Jigsaw-Code/outline-shadowsocksconfig/blob/v0.2.0/build/shadowsocks_config.js#L28)) to use it. However when the code is running in node or electron, this import `require("punycode")` actually means using the node's deprecated native library. This will cause more issues when electron's `contextIsolation` is turned on.

According to the [latest documentation](https://github.com/mathiasbynens/punycode.js#installation), we should use `import * as punycode from 'punycode'/` (which will be compiled to `require("punycode/")`) in order to reference the npm package.

Therefore in this PR, I updated this import statement and this is also a prerequisite of Jigsaw-Code/outline-apps#1365.
  • Loading branch information
jyyi1 authored Jul 30, 2022
1 parent 05bdd7f commit 6d01c4b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 183 deletions.
8 changes: 4 additions & 4 deletions build/shadowsocks_config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ export declare const SIP002_URI: {
stringify: (config: Config) => string;
};
export interface ConfigFetchParams {
readonly location: string;
readonly certFingerprint?: string;
readonly httpMethod?: string;
readonly location: string;
readonly certFingerprint?: string;
readonly httpMethod?: string;
}
export declare const ONLINE_CONFIG_PROTOCOL = 'ssconf';
export declare const ONLINE_CONFIG_PROTOCOL = "ssconf";
export declare function parseOnlineConfigUrl(url: string): ConfigFetchParams;
65 changes: 33 additions & 32 deletions build/shadowsocks_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ var __extends = (this && this.__extends) || (function () {
})();
Object.defineProperty(exports, "__esModule", { value: true });
var ipaddr = require("ipaddr.js");
var js_base64_1 = require('js-base64');
var punycode = require("punycode");
var js_base64_1 = require("js-base64");
var punycode = require("punycode/");
// Custom error base class
var ShadowsocksConfigError = /** @class */ (function (_super) {
__extends(ShadowsocksConfigError, _super);
Expand Down Expand Up @@ -299,7 +299,7 @@ exports.LEGACY_BASE64_URI = {
var b64EncodedData = js_base64_1.Base64.encode(data);
// Remove "=" padding
while (b64EncodedData.slice(-1) === '=') {
b64EncodedData = b64EncodedData.slice(0, -1);
b64EncodedData = b64EncodedData.slice(0, -1);
}
return "ss://" + b64EncodedData + hash;
},
Expand Down Expand Up @@ -367,34 +367,35 @@ exports.ONLINE_CONFIG_PROTOCOL = 'ssconf';
// Parses access parameters to retrieve a Shadowsocks proxy config from an
// online config URL. See: https://github.com/shadowsocks/shadowsocks-org/issues/89
function parseOnlineConfigUrl(url) {
if (!url || !url.startsWith(exports.ONLINE_CONFIG_PROTOCOL + ':')) {
throw new InvalidUri('URI protocol must be "' + exports.ONLINE_CONFIG_PROTOCOL + '"');
}
// Replace the protocol "ssconf" with "https" to ensure correct results,
// otherwise some Safari versions fail to parse it.
var inputForUrlParser = url.replace(new RegExp('^' + exports.ONLINE_CONFIG_PROTOCOL), 'https');
// The built-in URL parser throws as desired when given URIs with invalid syntax.
var urlParserResult = new URL(inputForUrlParser);
// Use ValidatedConfigFields subclasses (Host, Port, Tag) to throw on validation failure.
var uriFormattedHost = urlParserResult.hostname;
var host;
try {
host = new Host(uriFormattedHost);
} catch (_) {
// Could be IPv6 host formatted with surrounding brackets, so try stripping first and last
// characters. If this throws, give up and let the exception propagate.
host = new Host(uriFormattedHost.substring(1, uriFormattedHost.length - 1));
}
// The default URL parser fails to recognize the default HTTPs port (443).
var port = new Port(urlParserResult.port || '443');
// Parse extra parameters from the tag, which has the URL search parameters format.
var tag = new Tag(urlParserResult.hash.substring(1));
var params = new URLSearchParams(tag.data);
return {
// Build the access URL with the parsed parameters Exclude the query string and tag.
location: 'https://' + uriFormattedHost + ':' + port.data + urlParserResult.pathname,
certFingerprint: params.get('certFp') || undefined,
httpMethod: params.get('httpMethod') || undefined
};
if (!url || !url.startsWith(exports.ONLINE_CONFIG_PROTOCOL + ":")) {
throw new InvalidUri("URI protocol must be \"" + exports.ONLINE_CONFIG_PROTOCOL + "\"");
}
// Replace the protocol "ssconf" with "https" to ensure correct results,
// otherwise some Safari versions fail to parse it.
var inputForUrlParser = url.replace(new RegExp("^" + exports.ONLINE_CONFIG_PROTOCOL), 'https');
// The built-in URL parser throws as desired when given URIs with invalid syntax.
var urlParserResult = new URL(inputForUrlParser);
// Use ValidatedConfigFields subclasses (Host, Port, Tag) to throw on validation failure.
var uriFormattedHost = urlParserResult.hostname;
var host;
try {
host = new Host(uriFormattedHost);
}
catch (_) {
// Could be IPv6 host formatted with surrounding brackets, so try stripping first and last
// characters. If this throws, give up and let the exception propagate.
host = new Host(uriFormattedHost.substring(1, uriFormattedHost.length - 1));
}
// The default URL parser fails to recognize the default HTTPs port (443).
var port = new Port(urlParserResult.port || '443');
// Parse extra parameters from the tag, which has the URL search parameters format.
var tag = new Tag(urlParserResult.hash.substring(1));
var params = new URLSearchParams(tag.data);
return {
// Build the access URL with the parsed parameters Exclude the query string and tag.
location: "https://" + uriFormattedHost + ":" + port.data + urlParserResult.pathname,
certFingerprint: params.get('certFp') || undefined,
httpMethod: params.get('httpMethod') || undefined
};
}
exports.parseOnlineConfigUrl = parseOnlineConfigUrl;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "outline-shadowsocksconfig",
"version": "0.2.0",
"version": "0.2.1",
"license": "Apache-2.0",
"scripts": {
"build": "tsc",
Expand All @@ -12,6 +12,7 @@
"devDependencies": {
"@types/jasmine": "^2.8.6",
"@types/node": "^8.0.41",
"@types/punycode": "2.1.0",
"clang-format": "^1.2.2",
"husky": "^1.3.1",
"jasmine": "^3.1.0",
Expand Down
144 changes: 0 additions & 144 deletions punycode.d.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/shadowsocks_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import * as ipaddr from 'ipaddr.js';
import {Base64} from 'js-base64';
import * as punycode from 'punycode';
import * as punycode from 'punycode/';

// Custom error base class
export class ShadowsocksConfigError extends Error {
Expand Down Expand Up @@ -207,7 +207,7 @@ export const SHADOWSOCKS_URI = {
try {
return uriType.parse(uri);
} catch (e) {
error = e;
error = e as Error;
}
}
if (!(error instanceof InvalidUri)) {
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
version "8.0.45"
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.0.45.tgz#89fad82439d5624e1b5c6b42f0f5d85136dcdecc"

"@types/[email protected]":
version "2.1.0"
resolved "https://registry.yarnpkg.com/@types/punycode/-/punycode-2.1.0.tgz#89e4f3d09b3f92e87a80505af19be7e0c31d4e83"
integrity sha512-PG5aLpW6PJOeV2fHRslP4IOMWn+G+Uq8CfnyJ+PDS8ndCbU+soO+fB3NKCKo0p/Jh2Y4aPaiQZsrOXFdzpcA6g==

ansi-regex@^2.0.0:
version "2.1.1"
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-2.1.1.tgz#c3b33ab5ee360d86e0e628f0468ae7ef27d654df"
Expand Down

0 comments on commit 6d01c4b

Please sign in to comment.