Skip to content

Commit

Permalink
Updated dwn-sdk-js dependency to v0.5.1 & adapt to breaking changes (#…
Browse files Browse the repository at this point in the history
…169)

This will use the new `RecordsRead` reply structure.
  • Loading branch information
thehenrytsai authored Oct 8, 2024
1 parent 1e931e5 commit e14fb80
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 31 deletions.
22 changes: 11 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@web5/dwn-server",
"type": "module",
"version": "0.5.0",
"version": "0.6.0",
"files": [
"dist",
"src"
Expand All @@ -26,8 +26,8 @@
"url": "https://github.com/TBD54566975/dwn-server/issues"
},
"dependencies": {
"@tbd54566975/dwn-sdk-js": "0.4.7",
"@tbd54566975/dwn-sql-store": "0.6.7",
"@tbd54566975/dwn-sdk-js": "0.5.1",
"@tbd54566975/dwn-sql-store": "0.6.8",
"@web5/common": "^1.0.2",
"@web5/crypto": "^1.0.3",
"@web5/dids": "^1.1.3",
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const config = {
/**
* The base external URL of this DWN.
* This is used to construct URL paths such as the `Request URI` in the Web5 Connect flow.
* Should NOT be pointing to `localhost` for production use.
*/
baseUrl: process.env.DWN_BASE_URL || 'http://localhost:3000',

Expand Down
16 changes: 8 additions & 8 deletions src/http-api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { RecordsReadReply } from '@tbd54566975/dwn-sdk-js';
import { type Dwn, DateSort, RecordsRead, RecordsQuery, ProtocolsQuery } from '@tbd54566975/dwn-sdk-js';

import cors from 'cors';
Expand Down Expand Up @@ -111,13 +112,12 @@ export class HttpApi {

const leadTailSlashRegex = /^\/|\/$/;

function readReplyHandler(res, reply): any {
function readReplyHandler(res, reply: RecordsReadReply): any {
if (reply.status.code === 200) {
if (reply?.record?.data) {
const stream = reply.record.data;
delete reply.record.data;
if (reply?.entry?.data) {
const stream = reply.entry.data;

res.setHeader('content-type', reply.record.descriptor.dataFormat);
res.setHeader('content-type', reply.entry.recordsWrite.descriptor.dataFormat);
res.setHeader('dwn-response', JSON.stringify(reply));

return stream.pipe(res);
Expand Down Expand Up @@ -292,17 +292,17 @@ export class HttpApi {
});

this.#api.post('/', async (req: Request, res) => {
const dwnRequest = req.headers['dwn-request'] as any;
const dwnRpcRequestString = req.headers['dwn-request'] as string;

if (!dwnRequest) {
if (!dwnRpcRequestString) {
const reply = createJsonRpcErrorResponse(uuidv4(), JsonRpcErrorCodes.BadRequest, 'request payload required.');

return res.status(400).json(reply);
}

let dwnRpcRequest: JsonRpcRequest;
try {
dwnRpcRequest = JSON.parse(dwnRequest);
dwnRpcRequest = JSON.parse(dwnRpcRequestString);
} catch (e) {
const reply = createJsonRpcErrorResponse(uuidv4(), JsonRpcErrorCodes.BadRequest, e.message);

Expand Down
7 changes: 2 additions & 5 deletions src/json-rpc-handlers/dwn/process-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,11 @@ export const handleDwnProcessMessage: JsonRpcHandler = async (
});


const { record, entry } = reply;
const { entry } = reply;
// RecordsRead or MessagesRead messages optionally return data as a stream to accommodate large amounts of data
// we remove the data stream from the reply that will be serialized and return it as a separate property in the response payload.
let recordDataStream: IsomorphicReadable;
if (record !== undefined && record.data !== undefined) {
recordDataStream = reply.record.data;
delete reply.record.data; // not serializable via JSON
} else if (entry !== undefined && entry.data !== undefined) {
if (entry !== undefined && entry.data !== undefined) {
recordDataStream = entry.data;
delete reply.entry.data; // not serializable via JSON
}
Expand Down
2 changes: 1 addition & 1 deletion tests/common-scenario-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export default class CommonScenarioValidator {
expect(recordsReadJsonRpcResponse.id).to.equal(recordsReadRequestId);
expect(recordsReadJsonRpcResponse.error).to.not.exist;
expect(recordsReadJsonRpcResponse.result.reply.status.code).to.equal(200);
expect(recordsReadJsonRpcResponse.result.reply.record).to.exist;
expect(recordsReadJsonRpcResponse.result.reply.entry.recordsWrite).to.exist;

// can't get response as stream from supertest :(
const cid = await Cid.computeDagPbCidFromStream(recordsReadResponse.body as Readable);
Expand Down
6 changes: 3 additions & 3 deletions tests/http-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -770,10 +770,10 @@ describe('http api', function () {
expect(blob.size).to.equal(size);

// get dwn message response
const { status, record } = getDwnResponse(recordReadResponse);
const { status, entry } = getDwnResponse(recordReadResponse);
expect(status.code).to.equal(200);
expect(record).to.exist;
expect(record.recordId).to.equal(recordsWrite.message.recordId);
expect(entry).to.exist;
expect(entry.recordsWrite.recordId).to.equal(recordsWrite.message.recordId);
});

it('removes the trailing slash from the protocol path', async function () {
Expand Down

0 comments on commit e14fb80

Please sign in to comment.