Skip to content

Commit 6d01c4b

Browse files
authored
fix(deps): import local punycode package instead of core (#26)
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.
1 parent 05bdd7f commit 6d01c4b

File tree

6 files changed

+46
-183
lines changed

6 files changed

+46
-183
lines changed

build/shadowsocks_config.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ export declare const SIP002_URI: {
6262
stringify: (config: Config) => string;
6363
};
6464
export interface ConfigFetchParams {
65-
readonly location: string;
66-
readonly certFingerprint?: string;
67-
readonly httpMethod?: string;
65+
readonly location: string;
66+
readonly certFingerprint?: string;
67+
readonly httpMethod?: string;
6868
}
69-
export declare const ONLINE_CONFIG_PROTOCOL = 'ssconf';
69+
export declare const ONLINE_CONFIG_PROTOCOL = "ssconf";
7070
export declare function parseOnlineConfigUrl(url: string): ConfigFetchParams;

build/shadowsocks_config.js

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ var __extends = (this && this.__extends) || (function () {
2424
})();
2525
Object.defineProperty(exports, "__esModule", { value: true });
2626
var ipaddr = require("ipaddr.js");
27-
var js_base64_1 = require('js-base64');
28-
var punycode = require("punycode");
27+
var js_base64_1 = require("js-base64");
28+
var punycode = require("punycode/");
2929
// Custom error base class
3030
var ShadowsocksConfigError = /** @class */ (function (_super) {
3131
__extends(ShadowsocksConfigError, _super);
@@ -299,7 +299,7 @@ exports.LEGACY_BASE64_URI = {
299299
var b64EncodedData = js_base64_1.Base64.encode(data);
300300
// Remove "=" padding
301301
while (b64EncodedData.slice(-1) === '=') {
302-
b64EncodedData = b64EncodedData.slice(0, -1);
302+
b64EncodedData = b64EncodedData.slice(0, -1);
303303
}
304304
return "ss://" + b64EncodedData + hash;
305305
},
@@ -367,34 +367,35 @@ exports.ONLINE_CONFIG_PROTOCOL = 'ssconf';
367367
// Parses access parameters to retrieve a Shadowsocks proxy config from an
368368
// online config URL. See: https://github.com/shadowsocks/shadowsocks-org/issues/89
369369
function parseOnlineConfigUrl(url) {
370-
if (!url || !url.startsWith(exports.ONLINE_CONFIG_PROTOCOL + ':')) {
371-
throw new InvalidUri('URI protocol must be "' + exports.ONLINE_CONFIG_PROTOCOL + '"');
372-
}
373-
// Replace the protocol "ssconf" with "https" to ensure correct results,
374-
// otherwise some Safari versions fail to parse it.
375-
var inputForUrlParser = url.replace(new RegExp('^' + exports.ONLINE_CONFIG_PROTOCOL), 'https');
376-
// The built-in URL parser throws as desired when given URIs with invalid syntax.
377-
var urlParserResult = new URL(inputForUrlParser);
378-
// Use ValidatedConfigFields subclasses (Host, Port, Tag) to throw on validation failure.
379-
var uriFormattedHost = urlParserResult.hostname;
380-
var host;
381-
try {
382-
host = new Host(uriFormattedHost);
383-
} catch (_) {
384-
// Could be IPv6 host formatted with surrounding brackets, so try stripping first and last
385-
// characters. If this throws, give up and let the exception propagate.
386-
host = new Host(uriFormattedHost.substring(1, uriFormattedHost.length - 1));
387-
}
388-
// The default URL parser fails to recognize the default HTTPs port (443).
389-
var port = new Port(urlParserResult.port || '443');
390-
// Parse extra parameters from the tag, which has the URL search parameters format.
391-
var tag = new Tag(urlParserResult.hash.substring(1));
392-
var params = new URLSearchParams(tag.data);
393-
return {
394-
// Build the access URL with the parsed parameters Exclude the query string and tag.
395-
location: 'https://' + uriFormattedHost + ':' + port.data + urlParserResult.pathname,
396-
certFingerprint: params.get('certFp') || undefined,
397-
httpMethod: params.get('httpMethod') || undefined
398-
};
370+
if (!url || !url.startsWith(exports.ONLINE_CONFIG_PROTOCOL + ":")) {
371+
throw new InvalidUri("URI protocol must be \"" + exports.ONLINE_CONFIG_PROTOCOL + "\"");
372+
}
373+
// Replace the protocol "ssconf" with "https" to ensure correct results,
374+
// otherwise some Safari versions fail to parse it.
375+
var inputForUrlParser = url.replace(new RegExp("^" + exports.ONLINE_CONFIG_PROTOCOL), 'https');
376+
// The built-in URL parser throws as desired when given URIs with invalid syntax.
377+
var urlParserResult = new URL(inputForUrlParser);
378+
// Use ValidatedConfigFields subclasses (Host, Port, Tag) to throw on validation failure.
379+
var uriFormattedHost = urlParserResult.hostname;
380+
var host;
381+
try {
382+
host = new Host(uriFormattedHost);
383+
}
384+
catch (_) {
385+
// Could be IPv6 host formatted with surrounding brackets, so try stripping first and last
386+
// characters. If this throws, give up and let the exception propagate.
387+
host = new Host(uriFormattedHost.substring(1, uriFormattedHost.length - 1));
388+
}
389+
// The default URL parser fails to recognize the default HTTPs port (443).
390+
var port = new Port(urlParserResult.port || '443');
391+
// Parse extra parameters from the tag, which has the URL search parameters format.
392+
var tag = new Tag(urlParserResult.hash.substring(1));
393+
var params = new URLSearchParams(tag.data);
394+
return {
395+
// Build the access URL with the parsed parameters Exclude the query string and tag.
396+
location: "https://" + uriFormattedHost + ":" + port.data + urlParserResult.pathname,
397+
certFingerprint: params.get('certFp') || undefined,
398+
httpMethod: params.get('httpMethod') || undefined
399+
};
399400
}
400401
exports.parseOnlineConfigUrl = parseOnlineConfigUrl;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "outline-shadowsocksconfig",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"license": "Apache-2.0",
55
"scripts": {
66
"build": "tsc",
@@ -12,6 +12,7 @@
1212
"devDependencies": {
1313
"@types/jasmine": "^2.8.6",
1414
"@types/node": "^8.0.41",
15+
"@types/punycode": "2.1.0",
1516
"clang-format": "^1.2.2",
1617
"husky": "^1.3.1",
1718
"jasmine": "^3.1.0",

punycode.d.ts

Lines changed: 0 additions & 144 deletions
This file was deleted.

src/shadowsocks_config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import * as ipaddr from 'ipaddr.js';
1616
import {Base64} from 'js-base64';
17-
import * as punycode from 'punycode';
17+
import * as punycode from 'punycode/';
1818

1919
// Custom error base class
2020
export class ShadowsocksConfigError extends Error {
@@ -207,7 +207,7 @@ export const SHADOWSOCKS_URI = {
207207
try {
208208
return uriType.parse(uri);
209209
} catch (e) {
210-
error = e;
210+
error = e as Error;
211211
}
212212
}
213213
if (!(error instanceof InvalidUri)) {

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
version "8.0.45"
1111
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.0.45.tgz#89fad82439d5624e1b5c6b42f0f5d85136dcdecc"
1212

13+
14+
version "2.1.0"
15+
resolved "https://registry.yarnpkg.com/@types/punycode/-/punycode-2.1.0.tgz#89e4f3d09b3f92e87a80505af19be7e0c31d4e83"
16+
integrity sha512-PG5aLpW6PJOeV2fHRslP4IOMWn+G+Uq8CfnyJ+PDS8ndCbU+soO+fB3NKCKo0p/Jh2Y4aPaiQZsrOXFdzpcA6g==
17+
1318
ansi-regex@^2.0.0:
1419
version "2.1.1"
1520
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-2.1.1.tgz#c3b33ab5ee360d86e0e628f0468ae7ef27d654df"

0 commit comments

Comments
 (0)