Comparing version 1.0.2 to 1.1.0
@@ -11,2 +11,3 @@ import { BlobId, FeedId, MsgId } from 'ssb-typescript'; | ||
export declare function isFeedSSBURI(uri: string | null): boolean; | ||
export declare function isBendyButtV1FeedSSBURI(uri: string | null): boolean; | ||
export declare function isMessageSSBURI(uri: string | null): boolean; | ||
@@ -18,1 +19,28 @@ export declare function isBlobSSBURI(uri: string | null): boolean; | ||
export declare function isSSBURI(uri: string | null): boolean; | ||
interface CanonicalFeedParts { | ||
type: 'feed'; | ||
format: 'ed25519' | 'bendybutt-v1'; | ||
data: string; | ||
} | ||
interface CanonicalMessageParts { | ||
type: 'message'; | ||
format: 'sha256'; | ||
data: string; | ||
} | ||
interface CanonicalBlobParts { | ||
type: 'blob'; | ||
format: 'sha256'; | ||
data: string; | ||
} | ||
interface CanonicalAddressParts { | ||
type: 'address'; | ||
format: 'multiserver'; | ||
data: string; | ||
} | ||
declare type CanonicalParts = CanonicalFeedParts | CanonicalMessageParts | CanonicalBlobParts | CanonicalAddressParts; | ||
declare type RoughlyParts = { | ||
[k in keyof CanonicalParts]?: string; | ||
}; | ||
export declare function compose(parts: RoughlyParts): string; | ||
export declare function decompose(uri: string): CanonicalParts; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isSSBURI = exports.isExperimentalSSBURIWithAction = exports.isExperimentalSSBURI = exports.isAddressSSBURI = exports.isBlobSSBURI = exports.isMessageSSBURI = exports.isFeedSSBURI = exports.toMultiserverAddress = exports.toBlobSigil = exports.toMessageSigil = exports.toFeedSigil = exports.fromMultiserverAddress = exports.fromBlobSigil = exports.fromMessageSigil = exports.fromFeedSigil = void 0; | ||
exports.decompose = exports.compose = exports.isSSBURI = exports.isExperimentalSSBURIWithAction = exports.isExperimentalSSBURI = exports.isAddressSSBURI = exports.isBlobSSBURI = exports.isMessageSSBURI = exports.isBendyButtV1FeedSSBURI = exports.isFeedSSBURI = exports.toMultiserverAddress = exports.toBlobSigil = exports.toMessageSigil = exports.toFeedSigil = exports.fromMultiserverAddress = exports.fromBlobSigil = exports.fromMessageSigil = exports.fromFeedSigil = void 0; | ||
const urlParse = require('url-parse'); | ||
@@ -13,3 +13,3 @@ const Base64 = { | ||
}; | ||
function getSSBURIBase64Part(pathname) { | ||
function getSigilData(pathname) { | ||
var _a; | ||
@@ -24,14 +24,14 @@ if (!pathname) | ||
function fromFeedSigil(sigil) { | ||
const ref = Base64.unsafeToSafe(sigil.slice(1, -8)); | ||
return `ssb:feed/ed25519/${ref}`; | ||
const data = Base64.unsafeToSafe(sigil.slice(1, -8)); | ||
return `ssb:feed/ed25519/${data}`; | ||
} | ||
exports.fromFeedSigil = fromFeedSigil; | ||
function fromMessageSigil(sigil) { | ||
const ref = Base64.unsafeToSafe(sigil.slice(1, -7)); | ||
return `ssb:message/sha256/${ref}`; | ||
const data = Base64.unsafeToSafe(sigil.slice(1, -7)); | ||
return `ssb:message/sha256/${data}`; | ||
} | ||
exports.fromMessageSigil = fromMessageSigil; | ||
function fromBlobSigil(sigil) { | ||
const ref = Base64.unsafeToSafe(sigil.slice(1, -7)); | ||
return `ssb:blob/sha256/${ref}`; | ||
const data = Base64.unsafeToSafe(sigil.slice(1, -7)); | ||
return `ssb:blob/sha256/${data}`; | ||
} | ||
@@ -45,20 +45,22 @@ exports.fromBlobSigil = fromBlobSigil; | ||
function toFeedSigil(uri) { | ||
const ref = getSSBURIBase64Part(urlParse(uri, true).pathname); | ||
if (!ref) | ||
if (!isFeedSSBURI(uri)) | ||
return null; | ||
return `@${ref}.ed25519`; | ||
const sigilData = getSigilData(urlParse(uri, true).pathname); | ||
if (!sigilData) | ||
return null; | ||
return `@${sigilData}.ed25519`; | ||
} | ||
exports.toFeedSigil = toFeedSigil; | ||
function toMessageSigil(uri) { | ||
const ref = getSSBURIBase64Part(urlParse(uri, true).pathname); | ||
if (!ref) | ||
const sigilData = getSigilData(urlParse(uri, true).pathname); | ||
if (!sigilData) | ||
return null; | ||
return `%${ref}.sha256`; | ||
return `%${sigilData}.sha256`; | ||
} | ||
exports.toMessageSigil = toMessageSigil; | ||
function toBlobSigil(uri) { | ||
const ref = getSSBURIBase64Part(urlParse(uri, true).pathname); | ||
if (!ref) | ||
const sigilData = getSigilData(urlParse(uri, true).pathname); | ||
if (!sigilData) | ||
return null; | ||
return `&${ref}.sha256`; | ||
return `&${sigilData}.sha256`; | ||
} | ||
@@ -76,5 +78,14 @@ exports.toBlobSigil = toBlobSigil; | ||
uri.startsWith('ssb://feed/ed25519/')) && | ||
!!getSSBURIBase64Part(urlParse(uri, true).pathname)); | ||
!!getSigilData(urlParse(uri, true).pathname)); | ||
} | ||
exports.isFeedSSBURI = isFeedSSBURI; | ||
function isBendyButtV1FeedSSBURI(uri) { | ||
if (!uri) | ||
return false; | ||
return ((uri.startsWith('ssb:feed:bendybutt-v1:') || | ||
uri.startsWith('ssb:feed/bendybutt-v1/') || | ||
uri.startsWith('ssb://feed/bendybutt-v1/')) && | ||
!!getSigilData(urlParse(uri, true).pathname)); | ||
} | ||
exports.isBendyButtV1FeedSSBURI = isBendyButtV1FeedSSBURI; | ||
function isMessageSSBURI(uri) { | ||
@@ -86,3 +97,3 @@ if (!uri) | ||
uri.startsWith('ssb://message/sha256/')) && | ||
!!getSSBURIBase64Part(urlParse(uri, true).pathname)); | ||
!!getSigilData(urlParse(uri, true).pathname)); | ||
} | ||
@@ -96,3 +107,3 @@ exports.isMessageSSBURI = isMessageSSBURI; | ||
uri.startsWith('ssb://blob/sha256/')) && | ||
!!getSSBURIBase64Part(urlParse(uri, true).pathname)); | ||
!!getSigilData(urlParse(uri, true).pathname)); | ||
} | ||
@@ -131,1 +142,41 @@ exports.isBlobSSBURI = isBlobSSBURI; | ||
exports.isSSBURI = isSSBURI; | ||
function validateParts(parts) { | ||
if (!parts.type) | ||
throw new Error('Missing required "type" property'); | ||
if (!parts.format) | ||
throw new Error('Missing required "format" property'); | ||
if (!parts.data) | ||
throw new Error('Missing required "data" property'); | ||
if (parts.type === 'feed' && | ||
parts.format !== 'ed25519' && | ||
parts.format !== 'bendybutt-v1') { | ||
throw new Error('Unknown format for type "feed": ' + parts.format); | ||
} | ||
else if (parts.type === 'message' && parts.format !== 'sha256') { | ||
throw new Error('Unknown format for type "message": ' + parts.format); | ||
} | ||
else if (parts.type === 'blob' && parts.format !== 'sha256') { | ||
throw new Error('Unknown format for type "blob": ' + parts.format); | ||
} | ||
else if (parts.type === 'address' && parts.format !== 'multiserver') { | ||
throw new Error('Unknown format for type "address": ' + parts.format); | ||
} | ||
} | ||
function compose(parts) { | ||
validateParts(parts); | ||
const { type, format, data } = parts; | ||
return `ssb:${type}/${format}/${Base64.unsafeToSafe(data)}`; | ||
} | ||
exports.compose = compose; | ||
function decompose(uri) { | ||
const pathname = urlParse(uri, true).pathname; | ||
if (!pathname) { | ||
throw new Error('Invalid SSB URI: ' + uri); | ||
} | ||
let [type, format, data] = pathname.split('/'); | ||
data = Base64.safeToUnsafe(data); | ||
const parts = { type, format, data }; | ||
validateParts(parts); | ||
return parts; | ||
} | ||
exports.decompose = decompose; |
{ | ||
"name": "ssb-uri2", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Utilities for recognizing and converting SSB URIs", | ||
@@ -11,2 +11,5 @@ "repository": { | ||
"types": "lib/index.d.ts", | ||
"files": [ | ||
"lib/*" | ||
], | ||
"author": "Andre Staltz <contact@staltz.com>", | ||
@@ -39,2 +42,2 @@ "license": "LGPL-3.0", | ||
} | ||
} | ||
} |
@@ -16,10 +16,11 @@ # ssb-uri2 | ||
```js | ||
const ssbUri = require('ssb-uri2') | ||
const ssbUri = require('ssb-uri2'); | ||
const exampleURI = 'ssb:message/sha256/g3hPVPDEO1Aj_uPl0-J2NlhFB2bbFLIHlty-YuqFZ3w=' | ||
const exampleURI = | ||
'ssb:message/sha256/g3hPVPDEO1Aj_uPl0-J2NlhFB2bbFLIHlty-YuqFZ3w='; | ||
ssbUri.isMessageSSBURI(exampleURI) | ||
ssbUri.isMessageSSBURI(exampleURI); | ||
// true | ||
ssbUri.toMessageSigil(exampleURI) | ||
ssbUri.toMessageSigil(exampleURI); | ||
// '%g3hPVPDEO1Aj/uPl0+J2NlhFB2bbFLIHlty+YuqFZ3w=.sha256' | ||
@@ -34,2 +35,4 @@ ``` | ||
### `isBendyButtV1FeedSSBURI(uri: string | null): boolean` | ||
### `isMessageSSBURI(uri: string | null): boolean` | ||
@@ -63,5 +66,10 @@ | ||
### `compose(parts: {type, format, data}): string` | ||
### `decompose(uri: string): {type, format, data}` | ||
The object `{type, format, data}` is such that it matches `ssb:${type}/${format}/${data}`, except the `data` is always in normal Base64 (i.e. **not** URI safe). | ||
## License | ||
LGPL-3.0 |
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
18832
220
73