Comparing version 2.7.0 to 2.8.0
# Changelog | ||
## [2.8.0] | ||
### Added | ||
- `downloadFile` and `getSkylinkUrl` now accept a `subdomain` option which makes them return the skylink in subdomain format. | ||
- `parseSkylink` now accepts a `subdomain` option which parses the skylink as a base32 subdomain in a URL. | ||
### Changed | ||
- Trying to use the skykeyName or skykeyId parameters now results in an error, as previously users may not have realized they were unimplemented. | ||
## [2.7.0] | ||
@@ -4,0 +15,0 @@ |
@@ -61,2 +61,5 @@ "use strict"; | ||
var _a; | ||
if (config.skykeyName || config.skykeyId) { | ||
throw new Error("Unimplemented: skykeys have not been implemented in this SDK"); | ||
} | ||
var url = config.url; | ||
@@ -63,0 +66,0 @@ if (!url) { |
@@ -7,2 +7,3 @@ import { SkynetClient } from "./client"; | ||
* @param {string} [customOptions.endpointPath="/"] - The relative URL path of the portal endpoint to contact. | ||
* @param {boolean} [customOptions.subdomain=false] - Whether to return the final skylink in subdomain format. | ||
*/ | ||
@@ -15,2 +16,3 @@ export declare function downloadFile(this: SkynetClient, skylink: string, customOptions?: any): void; | ||
* @param {string} [customOptions.endpointPath="/hns"] - The relative URL path of the portal endpoint to contact. | ||
* @param {boolean} [customOptions.subdomain=false] - Whether to return the final URL with the HNS domain as a subdomain. | ||
*/ | ||
@@ -17,0 +19,0 @@ export declare function downloadFileHns(this: SkynetClient, domain: string, customOptions?: any): Promise<void>; |
@@ -53,3 +53,3 @@ "use strict"; | ||
var defaultDownloadOptions = __assign({}, utils_1.defaultOptions("/")); | ||
var defaultDownloadHnsOptions = __assign({}, utils_1.defaultOptions("/hns")); | ||
var defaultDownloadHnsOptions = __assign(__assign({}, utils_1.defaultOptions("/hns")), { hnsSubdomain: "hns" }); | ||
var defaultResolveHnsOptions = __assign({}, utils_1.defaultOptions("/hnsres")); | ||
@@ -61,2 +61,3 @@ /** | ||
* @param {string} [customOptions.endpointPath="/"] - The relative URL path of the portal endpoint to contact. | ||
* @param {boolean} [customOptions.subdomain=false] - Whether to return the final skylink in subdomain format. | ||
*/ | ||
@@ -76,2 +77,3 @@ function downloadFile(skylink, customOptions) { | ||
* @param {string} [customOptions.endpointPath="/hns"] - The relative URL path of the portal endpoint to contact. | ||
* @param {boolean} [customOptions.subdomain=false] - Whether to return the final URL with the HNS domain as a subdomain. | ||
*/ | ||
@@ -96,3 +98,9 @@ function downloadFileHns(domain, customOptions) { | ||
var query = opts.download ? { attachment: true } : {}; | ||
var url = utils_1.makeUrl(this.portalUrl, opts.endpointPath, utils_1.parseSkylink(skylink)); | ||
skylink = utils_1.parseSkylink(skylink); | ||
if (opts.subdomain) { | ||
skylink = utils_1.convertSkylinkToBase32(skylink); | ||
} | ||
var url = opts.subdomain | ||
? utils_1.addSubdomain(this.portalUrl, skylink) | ||
: utils_1.makeUrl(this.portalUrl, opts.endpointPath, skylink); | ||
return utils_1.addUrlQuery(url, query); | ||
@@ -105,3 +113,6 @@ } | ||
var query = opts.download ? { attachment: true } : {}; | ||
var url = utils_1.makeUrl(this.portalUrl, opts.endpointPath, utils_1.trimUriPrefix(domain, utils_1.uriHandshakePrefix)); | ||
domain = utils_1.trimUriPrefix(domain, utils_1.uriHandshakePrefix); | ||
var url = opts.subdomain | ||
? utils_1.addSubdomain(utils_1.addSubdomain(this.portalUrl, opts.hnsSubdomain), domain) | ||
: utils_1.makeUrl(this.portalUrl, opts.endpointPath, domain); | ||
return utils_1.addUrlQuery(url, query); | ||
@@ -113,3 +124,4 @@ } | ||
var opts = __assign(__assign(__assign({}, defaultResolveHnsOptions), this.customOptions), customOptions); | ||
return utils_1.makeUrl(this.portalUrl, opts.endpointPath, utils_1.trimUriPrefix(domain, utils_1.uriHandshakeResolverPrefix)); | ||
domain = utils_1.trimUriPrefix(domain, utils_1.uriHandshakeResolverPrefix); | ||
return utils_1.makeUrl(this.portalUrl, opts.endpointPath, domain); | ||
} | ||
@@ -116,0 +128,0 @@ exports.getHnsresUrl = getHnsresUrl; |
@@ -5,3 +5,5 @@ export declare const defaultSkynetPortalUrl = "https://siasky.net"; | ||
export declare const uriSkynetPrefix = "sia:"; | ||
export declare function addSubdomain(url: string, subdomain: string): string; | ||
export declare function addUrlQuery(url: string, query: Record<string, unknown>): string; | ||
export declare function convertSkylinkToBase32(input: string): string; | ||
export declare function defaultOptions(endpointPath: string): { | ||
@@ -20,3 +22,9 @@ endpointPath: string; | ||
export declare function makeUrl(...args: string[]): string; | ||
export declare function parseSkylink(skylink: string): string; | ||
/** | ||
* Parses the given string for a base64 skylink, or base32 if opts.subdomain is given. | ||
* @param skylinkStr - plain skylink, skylink with URI prefix, or URL with skylink as the first path element. | ||
* @param [opts={}] - Additional settings that can optionally be set. | ||
* @param [opts.subdomain=false] - Whether to parse the skylink as a base32 subdomain in a URL. | ||
*/ | ||
export declare function parseSkylink(skylinkStr: string, opts?: any): string; | ||
export declare function trimUriPrefix(str: string, prefix: string): string; | ||
@@ -23,0 +31,0 @@ export declare function randomNumber(low: number, high: number): number; |
@@ -13,3 +13,5 @@ "use strict"; | ||
exports.__esModule = true; | ||
exports.getFileMimeType = exports.readData = exports.toHexString = exports.hexToUint8Array = exports.stringToUint8Array = exports.randomNumber = exports.trimUriPrefix = exports.parseSkylink = exports.makeUrl = exports.getRootDirectory = exports.getRelativeFilePath = exports.defaultPortalUrl = exports.defaultOptions = exports.addUrlQuery = exports.uriSkynetPrefix = exports.uriHandshakeResolverPrefix = exports.uriHandshakePrefix = exports.defaultSkynetPortalUrl = void 0; | ||
exports.getFileMimeType = exports.readData = exports.toHexString = exports.hexToUint8Array = exports.stringToUint8Array = exports.randomNumber = exports.trimUriPrefix = exports.parseSkylink = exports.makeUrl = exports.getRootDirectory = exports.getRelativeFilePath = exports.defaultPortalUrl = exports.defaultOptions = exports.convertSkylinkToBase32 = exports.addUrlQuery = exports.addSubdomain = exports.uriSkynetPrefix = exports.uriHandshakeResolverPrefix = exports.uriHandshakePrefix = exports.defaultSkynetPortalUrl = void 0; | ||
var base64_js_1 = __importDefault(require("base64-js")); | ||
var base32_encode_1 = __importDefault(require("base32-encode")); | ||
var mime_db_1 = __importDefault(require("mime-db")); | ||
@@ -24,2 +26,13 @@ var path_browserify_1 = __importDefault(require("path-browserify")); | ||
exports.uriSkynetPrefix = "sia:"; | ||
// TODO: Use a third-party library to make this more robust. | ||
function addSubdomain(url, subdomain) { | ||
var urlObj = new URL(url); | ||
urlObj.hostname = subdomain + "." + urlObj.hostname; | ||
var str = urlObj.toString(); | ||
if (str.endsWith("/")) { | ||
return str.substring(0, str.length - 1); | ||
} | ||
return str; | ||
} | ||
exports.addSubdomain = addSubdomain; | ||
function addUrlQuery(url, query) { | ||
@@ -31,2 +44,7 @@ var parsed = url_parse_1["default"](url); | ||
exports.addUrlQuery = addUrlQuery; | ||
function convertSkylinkToBase32(input) { | ||
var decoded = base64_js_1["default"].toByteArray(input.padEnd(input.length + 4 - (input.length % 4), "=")); | ||
return base32_encode_1["default"](decoded, "RFC4648-HEX", { padding: false }).toLowerCase(); | ||
} | ||
exports.convertSkylinkToBase32 = convertSkylinkToBase32; | ||
function defaultOptions(endpointPath) { | ||
@@ -77,26 +95,47 @@ return { | ||
var SKYLINK_MATCHER = "([a-zA-Z0-9_-]{46})"; | ||
var SKYLINK_MATCHER_SUBDOMAIN = "([a-z0-9_-]{55})"; | ||
var SKYLINK_DIRECT_REGEX = new RegExp("^" + SKYLINK_MATCHER + "$"); | ||
var SKYLINK_PATHNAME_REGEX = new RegExp("^/?" + SKYLINK_MATCHER + "([/?].*)?$"); | ||
var SKYLINK_PATHNAME_REGEX = new RegExp("^/?" + SKYLINK_MATCHER + "(/.*)?$"); | ||
var SKYLINK_SUBDOMAIN_REGEX = new RegExp("^" + SKYLINK_MATCHER_SUBDOMAIN + "(\\..*)?$"); | ||
var SKYLINK_REGEXP_MATCH_POSITION = 1; | ||
function parseSkylink(skylink) { | ||
if (typeof skylink !== "string") | ||
throw new Error("Skylink has to be a string, " + typeof skylink + " provided"); | ||
// check for direct skylink match | ||
var matchDirect = skylink.match(SKYLINK_DIRECT_REGEX); | ||
/** | ||
* Parses the given string for a base64 skylink, or base32 if opts.subdomain is given. | ||
* @param skylinkStr - plain skylink, skylink with URI prefix, or URL with skylink as the first path element. | ||
* @param [opts={}] - Additional settings that can optionally be set. | ||
* @param [opts.subdomain=false] - Whether to parse the skylink as a base32 subdomain in a URL. | ||
*/ | ||
function parseSkylink(skylinkStr, opts) { | ||
if (opts === void 0) { opts = {}; } | ||
if (typeof skylinkStr !== "string") | ||
throw new Error("Skylink has to be a string, " + typeof skylinkStr + " provided"); | ||
if (opts.subdomain) { | ||
return parseSkylinkBase32(skylinkStr); | ||
} | ||
// Check for skylink prefixed with sia: or sia:// and extract it. | ||
// Example: sia:XABvi7JtJbQSMAcDwnUnmp2FKDPjg8_tTTFP4BwMSxVdEg | ||
// Example: sia://XABvi7JtJbQSMAcDwnUnmp2FKDPjg8_tTTFP4BwMSxVdEg | ||
skylinkStr = trimUriPrefix(skylinkStr, exports.uriSkynetPrefix); | ||
// Check for direct base64 skylink match. | ||
var matchDirect = skylinkStr.match(SKYLINK_DIRECT_REGEX); | ||
if (matchDirect) | ||
return matchDirect[SKYLINK_REGEXP_MATCH_POSITION]; | ||
// check for skylink prefixed with sia: or sia:// and extract it | ||
// example: sia:XABvi7JtJbQSMAcDwnUnmp2FKDPjg8_tTTFP4BwMSxVdEg | ||
// example: sia://XABvi7JtJbQSMAcDwnUnmp2FKDPjg8_tTTFP4BwMSxVdEg | ||
skylink = trimUriPrefix(skylink, exports.uriSkynetPrefix); | ||
// check for skylink passed in an url and extract it | ||
// example: https://siasky.net/XABvi7JtJbQSMAcDwnUnmp2FKDPjg8_tTTFP4BwMSxVdEg | ||
// pass empty object as second param to disable using location as base url when parsing in browser | ||
var parsed = url_parse_1["default"](skylink, {}); | ||
// Check for skylink passed in an url and extract it. | ||
// Example: https://siasky.net/XABvi7JtJbQSMAcDwnUnmp2FKDPjg8_tTTFP4BwMSxVdEg | ||
// Example: https://bg06v2tidkir84hg0s1s4t97jaeoaa1jse1svrad657u070c9calq4g.siasky.net (if opts.subdomain = true) | ||
// Pass empty object as second param to disable using location as base url when parsing in browser. | ||
var parsed = url_parse_1["default"](skylinkStr, {}); | ||
var matchPathname = parsed.pathname.match(SKYLINK_PATHNAME_REGEX); | ||
if (matchPathname) | ||
return matchPathname[SKYLINK_REGEXP_MATCH_POSITION]; | ||
throw new Error("Could not extract skylink from '" + skylink + "'"); | ||
return null; | ||
} | ||
exports.parseSkylink = parseSkylink; | ||
function parseSkylinkBase32(skylinkStr) { | ||
// Pass empty object as second param to disable using location as base url when parsing in browser. | ||
var parsed = url_parse_1["default"](skylinkStr, {}); | ||
var matchHostname = parsed.hostname.match(SKYLINK_SUBDOMAIN_REGEX); | ||
if (matchHostname) | ||
return matchHostname[SKYLINK_REGEXP_MATCH_POSITION]; | ||
return null; | ||
} | ||
function trimUriPrefix(str, prefix) { | ||
@@ -103,0 +142,0 @@ var longPrefix = prefix + "//"; |
{ | ||
"name": "skynet-js", | ||
"version": "2.7.0", | ||
"version": "2.8.0", | ||
"description": "Sia Skynet Javascript Client", | ||
@@ -58,2 +58,4 @@ "main": "dist/index.js", | ||
"axios": "^0.21.0", | ||
"base32-encode": "^1.1.1", | ||
"base64-js": "^1.3.1", | ||
"blakejs": "^1.1.0", | ||
@@ -75,2 +77,3 @@ "buffer": "^6.0.1", | ||
"@babel/preset-typescript": "^7.10.4", | ||
"@types/base64-js": "^1.3.0", | ||
"@types/jest": "^26.0.14", | ||
@@ -77,0 +80,0 @@ "@types/mime-db": "^1.43.0", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
87968
1396
12
26
+ Addedbase32-encode@^1.1.1
+ Addedbase64-js@^1.3.1
+ Addedbase32-encode@1.2.0(transitive)
+ Addedto-data-view@1.1.0(transitive)