Skip to content

Commit

Permalink
implemented protocol.send and record.send
Browse files Browse the repository at this point in the history
Co-Authored-By: Frank Hinek <[email protected]>
  • Loading branch information
mistermoe and frankhinek committed May 16, 2023
1 parent a52ae70 commit 90891af
Show file tree
Hide file tree
Showing 21 changed files with 929 additions and 768 deletions.
908 changes: 469 additions & 439 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/dids/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"dependencies": {
"@decentralized-identity/ion-tools": "1.0.7",
"@tbd54566975/crypto": "0.1.0",
"@tbd54566975/dwn-sdk-js": "0.0.31-unstable-2023-05-11-32b5f91",
"@tbd54566975/dwn-sdk-js": "0.0.32-unstable-2023-05-15-1350300",
"cross-fetch": "3.1.5"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/web5-agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"lint:fix": "eslint . --ext .js --fix"
},
"dependencies": {
"readable-stream": "4.3.0",
"@tbd54566975/dwn-sdk-js": "0.0.31-unstable-2023-05-11-32b5f91"
"readable-stream": "4.4.0",
"@tbd54566975/dwn-sdk-js": "0.0.32-unstable-2023-05-15-1350300"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "5.59.0",
Expand Down
32 changes: 14 additions & 18 deletions packages/web5-agent/src/web5-agent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Readable } from 'readable-stream';

import type {
import {
MessageReply,
RecordsQueryMessage,
ProtocolsQueryMessage,
Expand All @@ -13,8 +13,8 @@ import type {

import type { JsonRpcResponse } from './json-rpc.js';
export interface Web5Agent {
processDwnRequest(request: ProcessDwnRequest): Promise<ProcessDwnResponse>
sendDwnRequest(request: ProcessDwnRequest): Promise<SendDwnResponse>;
processDwnRequest(request: ProcessDwnRequest): Promise<DwnResponse>
sendDwnRequest(request: SendDwnRequest): Promise<DwnResponse>;
}

export type DwnMessages = {
Expand All @@ -29,33 +29,29 @@ export type DwnMessages = {

export type DwnMessageType = keyof DwnMessages;

/**
* TODO: add JSDoc
*/
export type ProcessDwnRequest = {
export type DwnRequest = {
author: string;
dataStream?: Blob | ReadableStream | Readable;
messageType: string;
messageOptions: unknown;
target: string;
messageType: string;
}

/**
* TODO: add JSDoc
*/
export type ProcessDwnResponse = {
message?: unknown;
reply: MessageReply;
export type ProcessDwnRequest = DwnRequest & {
dataStream?: Blob | ReadableStream | Readable;
messageOptions: unknown;
};

export type SendDwnRequest = DwnRequest & (ProcessDwnRequest | { messageCid: string })

/**
* TODO: add JSDoc
*/
export type SendDwnRequest = {
author: string;
messageCid: string;
messageType: string;
target: string;
export type DwnResponse = {
message?: unknown;
messageCid?: string;
reply: MessageReply;
};


Expand Down
6 changes: 3 additions & 3 deletions packages/web5-proxy-agent/src/web5-proxy-agent.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { ProcessDwnRequest, ProcessDwnResponse, SendDwnRequest, Web5Agent } from '@tbd54566975/web5-agent';
import type { DwnRequest, DwnResponse, Web5Agent } from '@tbd54566975/web5-agent';

// TODO: concretely define json-rpc types specific to each Web5Agent interface method
// TODO: write http transport. that transport will be shareable with client for dwn-server
// TODO: write ws transport. that transport will be shareable with client for dwn-server
// TODO: figure out where to put DidConnect.
export class Web5ProxyAgent implements Web5Agent {
processDwnRequest(_request: ProcessDwnRequest): Promise<ProcessDwnResponse> {
processDwnRequest(_request: DwnRequest): Promise<DwnResponse> {
throw new Error('Method not implemented.');
}

sendDwnRequest(_request: ProcessDwnRequest): Promise<any> {
sendDwnRequest(_request: DwnRequest): Promise<any> {
throw new Error('Method not implemented.');
}
}
3 changes: 3 additions & 0 deletions packages/web5-user-agent/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"enable-source-maps": true
}
4 changes: 2 additions & 2 deletions packages/web5-user-agent/karma.conf.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ module.exports = function (config) {
// list of files / patterns to load in the browser
files: [
// { pattern: 'tests/test-user-agent.ts', watched: false },
{ pattern: 'tests/**/*.spec.ts', watched: false },
{ pattern: 'tests/common/**/*.spec.ts', watched: false },
],

// preprocess matching files before serving them to the browser
// available preprocessors: https://www.npmjs.com/search?q=keywords:karma-preprocessor
preprocessors: {
// 'tests/test-user-agent.ts' : ['esbuild'],
'tests/**/*.spec.ts': ['esbuild'],
'tests/common/**/*.spec.ts': ['esbuild'],
},

esbuild: esbuildBrowserConfig,
Expand Down
2 changes: 1 addition & 1 deletion packages/web5-user-agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"dependencies": {
"@decentralized-identity/ion-tools": "1.0.7",
"@tbd54566975/dids": "0.1.0",
"@tbd54566975/dwn-sdk-js": "0.0.31-unstable-2023-05-11-32b5f91",
"@tbd54566975/dwn-sdk-js": "0.0.32-unstable-2023-05-15-1350300",
"abstract-level": "1.0.3",
"cross-fetch": "3.1.5",
"flat": "5.0.2",
Expand Down
48 changes: 17 additions & 31 deletions packages/web5-user-agent/src/dwn-rpc-client.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
import { DwnRpc, DwnRpcRequest, DwnRpcResponse, createJsonRpcErrorResponse } from '@tbd54566975/web5-agent';

import crossFetch from 'cross-fetch';
import type { DwnRpc, DwnRpcRequest, DwnRpcResponse, JsonRpcResponse } from '@tbd54566975/web5-agent';

import { v4 as uuidv4 } from 'uuid';
import { JsonRpcResponse, createJsonRpcRequest, parseJson, JsonRpcErrorCodes } from '@tbd54566975/web5-agent';
import { createJsonRpcRequest, parseJson } from '@tbd54566975/web5-agent';

// TODO: move what's below to dwn-server repo. i wrote this here for expediency

/**
* Supports fetch in: browser, browser extensions, Node, and React Native.
* In node, it uses node-fetch, and in a browser or React Native, it uses
* Github's whatwg-fetch.
*
* WARNING for browser extension background service workers:
* 'cross-fetch' is a ponyfill that uses `XMLHTTPRequest` under the hood.
* `XMLHTTPRequest` cannot be used in browser extension background service
* workers. Browser extensions get even more strict with `fetch` in that it
* cannot be referenced indirectly.
*
* use presence of File to decide whether we're in the browser or not
* TODO: ask Adam or Tim if globalThis is available in react native
*/
const fetch = globalThis.File ? globalThis.fetch : crossFetch;

/**
* Client used to communicate with Dwn Servers
*/
Expand Down Expand Up @@ -89,16 +71,12 @@ class HttpDwnRpcClient implements DwnRpc {
fetchOpts.headers['content-type'] = 'application/octet-stream';
fetchOpts['body'] = request.data;
}
let resp;
try {
resp = await fetch(request.dwnUrl, fetchOpts);
} catch(e) {
return createJsonRpcErrorResponse(requestId, JsonRpcErrorCodes.TransportError, e.message);
}

const resp = await fetch(request.dwnUrl, fetchOpts);
let dwnRpcResponse: DwnRpcResponse;

// check to see if response is in header first. if it is, that means the response is a ReadableStream
let dataStream;
const { headers } = resp;
if (headers.has('dwn-response')) {
const jsonRpcResponse = parseJson(headers.get('dwn-response')) as JsonRpcResponse;
Expand All @@ -107,15 +85,23 @@ class HttpDwnRpcClient implements DwnRpc {
throw new Error(`failed to parse json rpc response. dwn url: ${request.dwnUrl}`);
}

dwnRpcResponse = {
...jsonRpcResponse,
dataStream: resp.body
};
dataStream = resp.body;
dwnRpcResponse = jsonRpcResponse;
} else {
// TODO: wonder if i need to try/catch this?
dwnRpcResponse = await resp.json() as JsonRpcResponse;
}

return dwnRpcResponse;
if (dwnRpcResponse.error) {
const { code, message } = dwnRpcResponse.error;
throw new Error(`(${code}) - ${message}`);
}

const { reply } = dwnRpcResponse.result;
if (dataStream) {
reply['record']['data'] = dataStream;
}

return reply;
}
}
6 changes: 5 additions & 1 deletion packages/web5-user-agent/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@ import type { Readable } from 'readable-stream';
import { ReadableWebToNodeStream } from 'readable-web-to-node-stream';

export function blobToIsomorphicNodeReadable(blob: Blob): Readable {
return new ReadableWebToNodeStream(blob.stream());
return webReadableToIsomorphicNodeReadable(blob.stream());
}

export function webReadableToIsomorphicNodeReadable(webReadable: ReadableStream) {
return new ReadableWebToNodeStream(webReadable);
}
Loading

0 comments on commit 90891af

Please sign in to comment.