Skip to content

Commit

Permalink
Remove klona in favor of structuredClone.
Browse files Browse the repository at this point in the history
  • Loading branch information
aljones15 committed Jun 12, 2024
1 parent 631d751 commit 6a6fa72
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 25 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@
"base64url-universal": "^2.0.0",
"chai": "^4.3.7",
"credentials-context": "^2.0.0",
"data-integrity-test-suite-assertion": "github:w3c-ccg/data-integrity-test-suite-assertion#make-vc-gen-suite-configurable",
"data-integrity-test-suite-assertion": "github:w3c-ccg/data-integrity-test-suite-assertion#add-suite-verifier-tests",
"jsonld-document-loader": "^2.0.0",
"klona": "^2.0.6",
"mocha": "^10.2.0",
"uuid": "^9.0.0",
"vc-test-suite-implementations": "github:w3c/vc-test-suite-implementations"
Expand Down
5 changes: 2 additions & 3 deletions tests/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import * as bs58 from 'base58-universal';
import * as bs64 from 'base64url-universal';
import {createRequire} from 'node:module';
import {klona} from 'klona';
import {v4 as uuidv4} from 'uuid';

// remove first element and decode
Expand Down Expand Up @@ -57,8 +56,8 @@ export const createInitialVc = async ({
addIssuanceDate = true
}) => {
const {settings: {id: issuerId, options = {}}} = issuer;
const testOptions = klona(options);
const credential = klona(vc);
const testOptions = structuredClone(options);
const credential = structuredClone(vc);
credential.id = `urn:uuid:${uuidv4()}`;
credential.issuer = issuerId;
if(addIssuanceDate) {
Expand Down
5 changes: 2 additions & 3 deletions tests/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
deriveCredentials,
issueCredentials
} from './vc-generator/index.js';
import {klona} from 'klona';

export async function verifySetup({credentials, keyTypes, suite}) {
const testVectors = {
Expand Down Expand Up @@ -40,7 +39,7 @@ export async function verifySetup({credentials, keyTypes, suite}) {
// transforms the vectors
const transformVectors = (obj, func) => Object.entries(obj).map(input => {
const [vcVersion, vector] = input;
return [vcVersion, func(klona(vector))];
return [vcVersion, func(structuredClone(vector))];
});
const disclosedBaseVectors = transformVectors(
subjectNestedObjects,
Expand Down Expand Up @@ -84,7 +83,7 @@ export async function verifySetup({credentials, keyTypes, suite}) {
});
// select full arrays
testVectors.disclosed.array.full = await deriveCredentials({
vectors: Object.entries(klona(subjectHasArrays)),
vectors: Object.entries(structuredClone(subjectHasArrays)),
suite,
keyTypes
});
Expand Down
9 changes: 4 additions & 5 deletions tests/suites/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
verificationFail,
verificationSuccess
} from '../assertions.js';
import {klona} from 'klona';
import {supportsVc} from '../helpers.js';

export function verifySuite({
Expand Down Expand Up @@ -70,7 +69,7 @@ export function verifySuite({
it('If the "proofValue" string does not start with "u", an ' +
'error MUST be raised.', async function() {
const credential = getTestVector(disclosed?.base);
const signedCredentialCopy = klona(credential);
const signedCredentialCopy = structuredClone(credential);
// intentionally modify proofValue to not start with 'u'
signedCredentialCopy.proof.proofValue = 'a';
await verificationFail({
Expand All @@ -80,23 +79,23 @@ export function verifySuite({
it('If the "cryptosuite" field is not the string "bbs-2023", ' +
'an error MUST be raised.', async function() {
const credential = getTestVector(disclosed?.base);
const signedCredentialCopy = klona(credential);
const signedCredentialCopy = structuredClone(credential);
signedCredentialCopy.proof.cryptosuite = 'invalid-cryptosuite';
await verificationFail({
credential: signedCredentialCopy, verifier
});
});
it('MUST fail to verify a base proof.', async function() {
const credential = getTestVector(signed);
const signedCredentialCopy = klona(credential);
const signedCredentialCopy = structuredClone(credential);
await verificationFail({
credential: signedCredentialCopy, verifier
});
});
it('MUST fail to verify a modified disclosed credential.',
async function() {
const credential = getTestVector(disclosed?.base);
const signedCredentialCopy = klona(credential);
const signedCredentialCopy = structuredClone(credential);
// intentionally modify `credentialSubject` ID
signedCredentialCopy.credentialSubject.id = 'urn:invalid';
await verificationFail({
Expand Down
9 changes: 4 additions & 5 deletions tests/test-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* Copyright 2023-2024 Digital Bazaar, Inc.
* SPDX-License-Identifier: BSD-3-Clause
*/
import {klona} from 'klona';
import {require} from './helpers.js';

const _runner = require('../config/runner.json');
Expand Down Expand Up @@ -31,7 +30,7 @@ const openVectorFiles = vectorFiles => {
const value = vectorFiles[property];
// assume strings are paths to be opened
if(typeof value === 'string') {
vectorFiles[property] = klona(require(value));
vectorFiles[property] = structuredClone(require(value));
continue;
}
// assume everything else recurs
Expand All @@ -42,7 +41,7 @@ const openVectorFiles = vectorFiles => {

const _createVectorConfig = suite => {
// prevent mutation to require cache
const vectorConfig = klona(_vectors.suites[suite]);
const vectorConfig = structuredClone(_vectors.suites[suite]);
// open test data in credentials section
if(vectorConfig.credentials) {
const {credentials} = vectorConfig;
Expand All @@ -59,14 +58,14 @@ const _createSuiteConfig = suite => {
throw new Error(`Could not find config for suite ${suite}`);
}
// return a deep copy to prevent test data mutation
return klona(suiteConfig);
return structuredClone(suiteConfig);
};

export const getSuiteConfig = suite => {
// if cached config use it
if(_cache.get(suite)) {
// return a deep copy to prevent test data mutation
return klona(_cache.get(suite));
return structuredClone(_cache.get(suite));
}
// create an initial config
const suiteConfig = _createSuiteConfig(suite);
Expand Down
7 changes: 3 additions & 4 deletions tests/vc-generator/contexts.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@ import * as credentialsV2Ctx from '@digitalbazaar/credentials-v2-context';
import credentialsCtx from 'credentials-context';
import dataIntegrityCtx from '@digitalbazaar/data-integrity-context';
import didCtx from '@digitalcredentials/did-context';
import {klona} from 'klona';
import multikeyCtx from '@digitalbazaar/multikey-context';

const contextMap = new Map();
const setContexts = contexts => {
for(const [key, value] of contexts) {
contextMap.set(key, klona(value));
contextMap.set(key, structuredClone(value));
}
};

/*
const _dataIntegrityCtx = klona(dataIntegrityCtx.CONTEXT);
const _dataIntegrityCtx = structuredClone(dataIntegrityCtx.CONTEXT);
const diCtx = _dataIntegrityCtx['@context'];
// add UnknownProofType to local context for test data
diCtx.UnknownProofType =
klona(_dataIntegrityCtx['@context'].DataIntegrityProof);
structuredClone(_dataIntegrityCtx['@context'].DataIntegrityProof);
// add invalidPurpose to context for test data
diCtx.DataIntegrityProof['@context'].proofPurpose['@context'].invalidPurpose = {
'@id': 'https://w3id.org/security#invalidPurpose',
Expand Down
5 changes: 2 additions & 3 deletions tests/vc-generator/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/*!
* Copyright 2023 Digital Bazaar, Inc.
* Copyright 2023-2024 Digital Bazaar, Inc.
* SPDX-License-Identifier: BSD-3-Clause
*/
import * as vc from '@digitalbazaar/vc';
import {documentLoader as defaultLoader} from './documentLoader.js';
import {getMultikeys} from './key-gen.js';
import {getSuite} from './cryptosuites.js';
import {klona} from 'klona';

/**
* Issues test data locally and then returns a Map
Expand Down Expand Up @@ -56,7 +55,7 @@ export async function issueCredential({
suite,
mandatoryPointers = []
}) {
const _credential = klona(credential);
const _credential = structuredClone(credential);
_credential.issuer = issuer;
return vc.issue({
credential: _credential,
Expand Down

0 comments on commit 6a6fa72

Please sign in to comment.