+11
-11
@@ -5,4 +5,3 @@ 'use strict' | ||
| const { ALIASES } = require('pear-aliases') | ||
| const hypercoreid = require('hypercore-id-encoding') | ||
| const { encode } = hypercoreid | ||
| const hid = require('hypercore-id-encoding') | ||
| const FILE = 'file:' | ||
@@ -14,3 +13,3 @@ const PEAR = 'pear:' | ||
| try { | ||
| return hypercoreid.decode(v) | ||
| return hid.decode(v) | ||
| } catch (err) { | ||
@@ -29,11 +28,12 @@ if (typeof v === 'string') throw ERR_INVALID_LINK('alias not found: "' + v + '"', { ...info, err }) | ||
| serialize ({ protocol, pathname = '', search = '', hash = '', drive }) { | ||
| serialize (o) { | ||
| o = hid.isValid(o) ? { drive: { key: o }} : o | ||
| let { protocol, pathname = '', search = '', hash = '', drive } = o | ||
| if (protocol === FILE) return `${protocol}//${pathname}${search}${hash}` | ||
| if (!protocol && drive) protocol = PEAR | ||
| if (protocol === PEAR) { | ||
| const key = drive?.alias || encode(drive?.key) | ||
| const base = [drive.fork, drive.length, key, drive.hash && encode(drive.hash)].filter((p) => (p ?? '') + '').join('.') | ||
| const key = drive.alias || hid.normalize(drive.key) | ||
| const base = [drive.fork, drive.length, key, drive.hash && hid.encode(drive.hash)].filter((p) => (p ?? '') + '').join('.') | ||
| return `${protocol}//${base}${pathname}${search}${hash}` | ||
| } | ||
| throw ERR_INVALID_LINK('Unsupported protocol', { protocol, pathname, search, hash, drive }) | ||
@@ -46,3 +46,3 @@ } | ||
| const isRelativePath = isPath && link[0] !== '/' && link[1] !== ':' | ||
| const keys = Object.fromEntries(Object.entries(ALIASES).map(([k, v]) => [encode(v), k])) | ||
| const keys = Object.fromEntries(Object.entries(ALIASES).map(([k, v]) => [hid.encode(v), k])) | ||
| const { | ||
@@ -80,3 +80,3 @@ protocol, | ||
| const key = ALIASES[hostname] || decode(hostname, info) | ||
| const origin = keys[encode(key)] ? `${protocol}//${keys[encode(key)]}` : `${protocol}//${hostname}` | ||
| const origin = keys[hid.encode(key)] ? `${protocol}//${keys[hid.encode(key)]}` : `${protocol}//${hostname}` | ||
| const alias = ALIASES[hostname] ? hostname : null | ||
@@ -105,3 +105,3 @@ return { | ||
| const key = ALIASES[keyOrAlias] || decode(keyOrAlias, info) | ||
| const origin = keys[encode(key)] ? `${protocol}//${keys[encode(key)]}` : `${protocol}//${keyOrAlias}` | ||
| const origin = keys[hid.encode(key)] ? `${protocol}//${keys[hid.encode(key)]}` : `${protocol}//${keyOrAlias}` | ||
@@ -139,3 +139,3 @@ if (parts === 3) { // pear://fork.length.keyOrAlias[/some/path] | ||
| fork: Number(fork), | ||
| hash: hypercoreid.decode(apphash), | ||
| hash: hid.decode(apphash), | ||
| alias | ||
@@ -142,0 +142,0 @@ } |
+1
-1
| { | ||
| "name": "pear-link", | ||
| "version": "4.1.2", | ||
| "version": "4.2.0", | ||
| "description": "Parse Pear Links", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+6
-2
@@ -61,8 +61,12 @@ # pear-link | ||
| #### `const link = plink.serialize({ protocol, pathname, search, hash, drive })` | ||
| #### `const link = plink.serialize({ protocol, pathname, search, hash, drive })` | `const link = plink.serialize(key)` | ||
| Returns the serialized version of the object describing a URL as a string. | ||
| Expects either | ||
| * a parsed object as returned by plink.parse | ||
| * a `hypercore-id-encoding` key, eg `drive.key` - the key in `pear://<key>` - `key` may be buffer or string. | ||
| Returns a `pear://` link as a string. | ||
| ## License | ||
| Apache-2.0 |
19457
1.08%133
0.76%72
5.88%