Skip to content

Commit

Permalink
Rename cache.createId to cache.createContentId.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlongley committed Nov 17, 2024
1 parent 246532f commit 9cbf152
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
15 changes: 7 additions & 8 deletions lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,19 @@ bedrock.events.on('bedrock-mongodb.ready', async () => {
});

/**
* Creates a cache entry ID from an object. This utility function is useful
* for applications that want to generate cache entries for JSON objects
* without storing the objects in the cache.
* Creates a cache entry ID from some content (object, string, boolean, etc.).
* This utility function is useful for applications that want to consistently
* (re-)generate IDs to use in the cache based on content (aka Content-based
* IDs or CIDS), without having to directly store the content in the cache.
*
* @param {object} options - Options to use.
* @param {object} [options.object] - The object to generate an ID from.
* @param {*} [options.content] - The content to generate an ID from.
*
* @returns {Promise<object>} Resolves to an object with `id`.
*/
export async function createId({object} = {}) {
assert.object(object, 'object');

export async function createContentId({content} = {}) {
// canonicalize object to a string
const string = canonicalize(object);
const string = canonicalize(content);
// hash string
const digest = await _sha256({string});
// express digest as multibase-multihash string
Expand Down
10 changes: 5 additions & 5 deletions test/mocha/10-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import crypto from 'node:crypto';
import {tokenizers} from '@bedrock/tokenizer';

describe('Cache', function() {
describe('cache.createId()', () => {
it('should create the ID for equivalent objects', async () => {
describe('cache.createContentId()', () => {
it('should create same content ID for equivalent objects', async () => {
const object1 = {
a: 'a',
b: 'b',
Expand All @@ -20,9 +20,9 @@ describe('Cache', function() {
const object2 = {...object1};
const object3 = {...object1, a: 'different'};

const {id: id1} = await cache.createId({object: object1});
const {id: id2} = await cache.createId({object: object2});
const {id: id3} = await cache.createId({object: object3});
const {id: id1} = await cache.createContentId({content: object1});
const {id: id2} = await cache.createContentId({content: object2});
const {id: id3} = await cache.createContentId({content: object3});

id1.should.equal(id2);
id1.should.not.equal(id3);
Expand Down

0 comments on commit 9cbf152

Please sign in to comment.