Skip to content

Commit

Permalink
Fixes bugs in web5.dwn.records.write and Record.data (#51)
Browse files Browse the repository at this point in the history
* Fixes bugs in web5.dwn.records.write and Record.data

Signed-off-by: Frank Hinek <[email protected]>

* 0.6.1

---------

Signed-off-by: Frank Hinek <[email protected]>
  • Loading branch information
frankhinek authored Apr 29, 2023
1 parent d5caeaf commit 8300edf
Show file tree
Hide file tree
Showing 19 changed files with 787 additions and 301 deletions.
11 changes: 6 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# Ignore underlying DWN stores and indexes
**/DATASTORE/
**/EVENTLOG/
**/INDEX/
**/MESSAGESTORE/

# bundle metadata
bundle-metadata.json

# Created by https://www.toptal.com/developers/gitignore/api/node
# Edit at https://www.toptal.com/developers/gitignore?templates=node

### Node ###
# Logs
logs
Expand Down Expand Up @@ -143,5 +146,3 @@ dist

# SvelteKit build / generate output
.svelte-kit

# End of https://www.toptal.com/developers/gitignore/api/node
11 changes: 7 additions & 4 deletions karma.conf.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
const playwright = require('playwright');
const esbuildBrowserConfig = require('./build/esbuild-browser-config.cjs');

// use playwright firefox exec path as run target for webkit tests
// use playwright chrome exec path as run target for chromium tests
process.env.CHROME_BIN = playwright.chromium.executablePath();

// use playwright webkit exec path as run target for safari tests
process.env.WEBKIT_HEADLESS_BIN = playwright.webkit.executablePath();

// use playwright firefox exec path as run target for firefox tests
Expand All @@ -19,7 +22,7 @@ module.exports = function (config) {
'karma-webkit-launcher',
'karma-esbuild',
'karma-mocha',
'karma-mocha-reporter'
'karma-mocha-reporter',
],

// frameworks to use
Expand All @@ -35,7 +38,7 @@ module.exports = function (config) {
// preprocess matching files before serving them to the browser
// available preprocessors: https://www.npmjs.com/search?q=keywords:karma-preprocessor
preprocessors: {
'tests/**/*.spec.js': ['esbuild']
'tests/**/*.spec.js': ['esbuild'],
},

esbuild: esbuildBrowserConfig,
Expand Down Expand Up @@ -66,6 +69,6 @@ module.exports = function (config) {

// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true
singleRun: true,
});
};
4 changes: 2 additions & 2 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tbd54566975/web5",
"version": "0.6.0",
"version": "0.6.1",
"description": "SDK for accessing the features and capabilities of Web5",
"type": "module",
"main": "./dist/cjs/index.cjs",
Expand Down Expand Up @@ -28,7 +28,7 @@
"lint": "eslint . --ext .js --max-warnings 0",
"lint:fix": "eslint . --ext .js --fix",
"publish:unstable": "./build/publish-unstable.sh",
"test:node": "mocha \"tests/**/*.spec.js\"",
"test:node": "mocha \"tests/**/*.spec.js\" --exit",
"test:browser": "karma start karma.conf.cjs"
},
"keywords": [],
Expand Down
2 changes: 1 addition & 1 deletion src/dwn/interfaces/records.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class Records extends Interface {
let dataBytes, dataFormat;
if (request?.data) {
// If `data` is specified, convert string/object data to bytes before further processing.
({ dataBytes, dataFormat } = dataToBytes(request.data, request.message.dataFormat));
({ dataBytes, dataFormat } = dataToBytes(request.data, request.message?.dataFormat));
} else {
// If not, `dataFormat` must be specified in the request message.
dataFormat = request.message.dataFormat;
Expand Down
2 changes: 1 addition & 1 deletion src/dwn/models/record.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class Record {
},
async text() {
if (self.#encodedData) return Encoder.bytesToString(self.#encodedData);
if (self.#readableStream) return self.#readableStream.then(DataStream.toBytes).then(Encoder.bytesToString);
if (self.#readableStream) return this.stream().then(DataStream.toBytes).then(Encoder.bytesToString);
return null;
},
async stream() {
Expand Down
54 changes: 54 additions & 0 deletions tests/did/manager.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { expect } from 'chai';
import sinon from 'sinon';

import { Web5Did } from '../../src/did/web5-did.js';

describe('DidManager', async () => {
let web5did;

beforeEach(function () {
web5did = new Web5Did();
});

before(function () {
this.clock = sinon.useFakeTimers();
});

after(function () {
this.clock.restore();
});

it('should never expire managed DIDs', async function () {
let resolved;
const did = 'did:ion:abcd1234';
const didData = {
connected: true,
endpoint: 'http://localhost:55500',
};

await web5did.manager.set(did, didData);

resolved = await web5did.resolve(did);
expect(resolved).to.not.be.undefined;
expect(resolved).to.equal(didData);

this.clock.tick(2147483647); // Time travel 23.85 days

resolved = await web5did.resolve(did);
expect(resolved).to.not.be.undefined;
expect(resolved).to.equal(didData);
});

it('should return object with keys undefined if key data not provided', async () => {
const did = 'did:ion:abcd1234';
const didData = {
connected: true,
endpoint: 'http://localhost:55500',
};

await web5did.manager.set(did, didData);

const resolved = await web5did.resolve(did);
expect(resolved.keys).to.be.undefined;
});
});
14 changes: 7 additions & 7 deletions tests/did/methods/ion.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { expect } from 'chai';
import sinon from 'sinon';

import { Web5Did } from '../../../src/did/web5-did.js';
import * as didDocuments from '../../data/did-documents.js';
import * as didDocuments from '../../fixtures/did-documents.js';

describe('Web5Did', async () => {
describe('did:ion method', async () => {
let web5did;

beforeEach(function () {
Expand All @@ -19,7 +19,7 @@ describe('Web5Did', async () => {
this.clock.restore();
});

describe('create', async () => {
describe('create()', async () => {
it('should return one key when creating a did:ion DID', async () => {
const did = await web5did.create('ion');
expect(did.keys).to.have.lengthOf(1);
Expand All @@ -43,7 +43,7 @@ describe('Web5Did', async () => {
});
});

describe('getDidDocument', async () => {
describe('getDidDocument()', async () => {
it('should return a didDocument for a valid did:ion DID', async () => {
sinon.stub(web5did, 'resolve').resolves(didDocuments.ion.oneVerificationMethodJwk);

Expand All @@ -62,7 +62,7 @@ describe('Web5Did', async () => {
});
});

describe('getServices', async () => {
describe('getServices()', async () => {
it('should return array of services when defined in DID document', async () => {
sinon.stub(web5did, 'resolve').resolves(didDocuments.ion.oneService);

Expand All @@ -81,7 +81,7 @@ describe('Web5Did', async () => {
});
});

describe('resolve', async () => {
describe('resolve()', async () => {
it('should not call ion-tools resolve() when managed DID is cached', async () => {
// If managed DID isn't cached, the fetch() call to resolve over the network
// will take far more than 10ms timeout, causing the test to fail.
Expand Down Expand Up @@ -114,4 +114,4 @@ describe('Web5Did', async () => {
expect(resolved.didResolutionMetadata.error).to.equal(`unable to resolve ${did}, got http status 404`);
});
});
});
});
12 changes: 6 additions & 6 deletions tests/did/methods/key.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import { expect } from 'chai';
import sinon from 'sinon';

import { Web5Did } from '../../../src/did/web5-did.js';
import * as didDocuments from '../../data/did-documents.js';
import * as didDocuments from '../../fixtures/did-documents.js';

describe('Web5Did', async () => {
describe('did:key method', async () => {
let web5did;

beforeEach(function () {
web5did = new Web5Did();
});

describe('create', async () => {
describe('create()', async () => {
it('should return two keys when creating a did:key DID', async () => {
const did = await web5did.create('key');
expect(did.keys).to.have.lengthOf(2);
Expand Down Expand Up @@ -44,7 +44,7 @@ describe('Web5Did', async () => {
});
});

describe('getDidDocument', async () => {
describe('getDidDocument()', async () => {
it('should return a didDocument for a valid did:key DID', async () => {
sinon.stub(web5did, 'resolve').resolves(didDocuments.key.oneVerificationMethodJwk);

Expand All @@ -63,7 +63,7 @@ describe('Web5Did', async () => {
});
});

describe('resolve', async () => {
describe('resolve()', async () => {
it('should return a didResolutionResult for a valid DID', async () => {
const did = 'did:key:z6MkhvthBZDxVvLUswRey729CquxMiaoYXrT5SYbCAATc8V9';

Expand All @@ -82,4 +82,4 @@ describe('Web5Did', async () => {
expect(resolved.didResolutionMetadata.error).to.equal('invalidDid');
});
});
});
});
Loading

0 comments on commit 8300edf

Please sign in to comment.