Skip to content

Commit

Permalink
Merge branches 'main' and 'store-import-roles' of https://github.com/…
Browse files Browse the repository at this point in the history
…TBD54566975/web5-js into store-import-roles
  • Loading branch information
codesandbot committed Jan 31, 2024
2 parents 2bd93a8 + 4d1ada1 commit 03655c5
Show file tree
Hide file tree
Showing 9 changed files with 610 additions and 37 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ Each `Record` instance has the following instance methods:
- **`text`** - _`function`_: returns the data as a string.
- **`send`** - _`function`_: sends the record the instance represents to the DWeb Node endpoints of a provided DID.
- **`update`** - _`function`_: takes in a new request object matching the expected method signature of a `write` and overwrites the record. This is a convenience method that allows you to easily overwrite records with less verbosity.
- **`store`** - _`function`_: stores the record in the local DWN instance, offering the following options:
- `import`: imports the record as with an owner-signed override (still subject to Protocol rules, when a record is Protocol-based)
- **`import`** - _`function`_: signs a record with an owner override to import the record into the local DWN instance:
- `store` - _`boolean`_: when false is passed, the record will only be signed with an owner override, not stored in the local DWN instance. Defaults to `true`.

### **`web5.dwn.records.query(request)`**

Expand Down
29 changes: 18 additions & 11 deletions packages/agent/src/dwn-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type DwnMessage = {
data?: Blob;
}

const dwnMessageCreators = {
const dwnMessageConstructors = {
[DwnInterfaceName.Events + DwnMethodName.Get] : EventsGet,
[DwnInterfaceName.Messages + DwnMethodName.Get] : MessagesGet,
[DwnInterfaceName.Records + DwnMethodName.Read] : RecordsRead,
Expand Down Expand Up @@ -245,14 +245,14 @@ export class DwnManager {
request: ProcessDwnRequest
}) {
const { request } = options;

const rawMessage = request.rawMessage as any;
let readableStream: Readable | undefined;

// TODO: Consider refactoring to move data transformations imposed by fetch() limitations to the HTTP transport-related methods.
if (request.messageType === 'RecordsWrite') {
const messageOptions = request.messageOptions as RecordsWriteOptions;

if (request.dataStream && !messageOptions.data) {
if (request.dataStream && !messageOptions?.data) {
const { dataStream } = request;
let isomorphicNodeReadable: Readable;

Expand All @@ -266,21 +266,28 @@ export class DwnManager {
readableStream = webReadableToIsomorphicNodeReadable(forProcessMessage);
}

// @ts-ignore
messageOptions.dataCid = await Cid.computeDagPbCidFromStream(isomorphicNodeReadable);
// @ts-ignore
messageOptions.dataSize ??= isomorphicNodeReadable['bytesRead'];
if (!rawMessage) {
// @ts-ignore
messageOptions.dataCid = await Cid.computeDagPbCidFromStream(isomorphicNodeReadable);
// @ts-ignore
messageOptions.dataSize ??= isomorphicNodeReadable['bytesRead'];
}
}
}

const dwnSigner = await this.constructDwnSigner(request.author);

const messageCreator = dwnMessageCreators[request.messageType];
const dwnMessage = await messageCreator.create({
const dwnMessageConstructor = dwnMessageConstructors[request.messageType];
const dwnMessage = rawMessage ? await dwnMessageConstructor.parse(rawMessage) : await dwnMessageConstructor.create({
...<any>request.messageOptions,
signer: dwnSigner
});

if (dwnMessageConstructor === RecordsWrite){
if (request.signAsOwner) {
await (dwnMessage as RecordsWrite).signAsOwner(dwnSigner);
}

Check warning on line 288 in packages/agent/src/dwn-manager.ts

View check run for this annotation

Codecov / codecov/patch

packages/agent/src/dwn-manager.ts#L287-L288

Added lines #L287 - L288 were not covered by tests
}

return { message: dwnMessage.message, dataStream: readableStream };
}

Expand Down Expand Up @@ -411,7 +418,7 @@ export class DwnManager {

const dwnSigner = await this.constructDwnSigner(author);

const messageCreator = dwnMessageCreators[messageType];
const messageCreator = dwnMessageConstructors[messageType];

const dwnMessage = await messageCreator.create({
...<any>messageOptions,
Expand Down
4 changes: 3 additions & 1 deletion packages/agent/src/types/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ export type DwnRequest = {
*/
export type ProcessDwnRequest = DwnRequest & {
dataStream?: Blob | ReadableStream | Readable;
messageOptions: unknown;
rawMessage?: unknown;
messageOptions?: unknown;
store?: boolean;
signAsOwner?: boolean;
};

export type SendDwnRequest = DwnRequest & (ProcessDwnRequest | { messageCid: string })
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/dwn-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ export class DwnApi {
const { entries, status, cursor } = reply;

const records = entries.map((entry: RecordsQueryReplyEntry) => {

const recordOptions = {
/**
* Extract the `author` DID from the record entry since records may be signed by the
Expand Down
Loading

0 comments on commit 03655c5

Please sign in to comment.