82 lines
4.3 KiB
JavaScript
82 lines
4.3 KiB
JavaScript
|
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||
|
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
||
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||
|
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||
|
|
});
|
||
|
|
};
|
||
|
|
import { utils as didUtils } from '@web5/dids';
|
||
|
|
import { ReadableWebToNodeStream } from 'readable-web-to-node-stream';
|
||
|
|
import { DateSort, DwnInterfaceName, DwnMethodName, Message, Records } from '@tbd54566975/dwn-sdk-js';
|
||
|
|
export function blobToIsomorphicNodeReadable(blob) {
|
||
|
|
return webReadableToIsomorphicNodeReadable(blob.stream());
|
||
|
|
}
|
||
|
|
export function getDwnServiceEndpointUrls(didUri, dereferencer) {
|
||
|
|
return __awaiter(this, void 0, void 0, function* () {
|
||
|
|
// Attempt to dereference the DID service with ID fragment #dwn.
|
||
|
|
const dereferencingResult = yield dereferencer.dereference(`${didUri}#dwn`);
|
||
|
|
if (dereferencingResult.dereferencingMetadata.error) {
|
||
|
|
throw new Error(`Failed to dereference '${didUri}#dwn': ${dereferencingResult.dereferencingMetadata.error}`);
|
||
|
|
}
|
||
|
|
if (didUtils.isDwnDidService(dereferencingResult.contentStream)) {
|
||
|
|
const { serviceEndpoint } = dereferencingResult.contentStream;
|
||
|
|
const serviceEndpointUrls = typeof serviceEndpoint === 'string'
|
||
|
|
// If the service endpoint is a string, format it as a single-element array.
|
||
|
|
? [serviceEndpoint]
|
||
|
|
: Array.isArray(serviceEndpoint) && serviceEndpoint.every(endpoint => typeof endpoint === 'string')
|
||
|
|
// If the service endpoint is an array of strings, use it as is.
|
||
|
|
? serviceEndpoint
|
||
|
|
// If the service endpoint is neither a string nor an array of strings, return an empty array.
|
||
|
|
: [];
|
||
|
|
if (serviceEndpointUrls.length > 0) {
|
||
|
|
return serviceEndpointUrls;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
// If the DID service with ID fragment #dwn was not found or is not valid, return an empty array.
|
||
|
|
return [];
|
||
|
|
});
|
||
|
|
}
|
||
|
|
export function getRecordAuthor(record) {
|
||
|
|
return Records.getAuthor(record);
|
||
|
|
}
|
||
|
|
export function isRecordsWrite(obj) {
|
||
|
|
// Validate that the given value is an object.
|
||
|
|
if (!obj || typeof obj !== 'object' || obj === null)
|
||
|
|
return false;
|
||
|
|
// Validate that the object has the necessary properties of RecordsWrite.
|
||
|
|
return ('message' in obj && typeof obj.message === 'object' && obj.message !== null &&
|
||
|
|
'descriptor' in obj.message && typeof obj.message.descriptor === 'object' && obj.message.descriptor !== null &&
|
||
|
|
'interface' in obj.message.descriptor && obj.message.descriptor.interface === DwnInterfaceName.Records &&
|
||
|
|
'method' in obj.message.descriptor && obj.message.descriptor.method === DwnMethodName.Write);
|
||
|
|
}
|
||
|
|
/**
|
||
|
|
* Get the CID of the given RecordsWriteMessage.
|
||
|
|
*/
|
||
|
|
export function getRecordMessageCid(message) {
|
||
|
|
return Message.getCid(message);
|
||
|
|
}
|
||
|
|
/**
|
||
|
|
* Get the pagination cursor for the given RecordsWriteMessage and DateSort.
|
||
|
|
*
|
||
|
|
* @param message The RecordsWriteMessage for which to get the pagination cursor.
|
||
|
|
* @param dateSort The date sort that will be used in the query or subscription to which the cursor will be applied.
|
||
|
|
*/
|
||
|
|
export function getPaginationCursor(message, dateSort) {
|
||
|
|
return __awaiter(this, void 0, void 0, function* () {
|
||
|
|
const value = dateSort === DateSort.CreatedAscending || dateSort === DateSort.CreatedDescending ?
|
||
|
|
message.descriptor.dateCreated : message.descriptor.datePublished;
|
||
|
|
if (value === undefined) {
|
||
|
|
throw new Error('The dateCreated or datePublished property is missing from the record descriptor.');
|
||
|
|
}
|
||
|
|
return {
|
||
|
|
messageCid: yield getRecordMessageCid(message),
|
||
|
|
value
|
||
|
|
};
|
||
|
|
});
|
||
|
|
}
|
||
|
|
export function webReadableToIsomorphicNodeReadable(webReadable) {
|
||
|
|
return new ReadableWebToNodeStream(webReadable);
|
||
|
|
}
|
||
|
|
//# sourceMappingURL=utils.js.map
|