Skip to content

Commit e683066

Browse files
committed
fix: use Gatsby's digest function instead of sha
1 parent 6e483d7 commit e683066

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

source/node.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
* -------------------------------------------------------------------------
1414
*/
1515

16-
import { createHash } from 'crypto';
17-
1816
import { name } from '#.';
1917

2018
import type { NodeInput, NodePluginArgs } from 'gatsby';
@@ -86,7 +84,7 @@ export class NodeManager {
8684
const oldMap = new Map<string, NormalisedEntity>(
8785
(await this.cache.get('entityMap')) ?? [],
8886
);
89-
const newMap = computeEntityMap(entities);
87+
const newMap = computeEntityMap(entities, this.createContentDigest);
9088

9189
// for the usage of createNode
9290
// see https://www.gatsbyjs.com/docs/reference/config-files/actions/#createNode
@@ -283,10 +281,12 @@ export class NodeManager {
283281
/**
284282
* attach parent-child relationship to gatsby node
285283
* @param entities all sort of entities including database and page
284+
* @param hashFn a hash function for generating the content digest
286285
* @returns a map of entities with parent and children linked
287286
*/
288287
export function computeEntityMap(
289288
entities: FullEntity[],
289+
hashFn: (content: string | FullEntity) => string,
290290
): Map<string, NormalisedEntity> {
291291
// create a new working set
292292
const map = new Map<string, NormalisedEntity>();
@@ -295,7 +295,7 @@ export function computeEntityMap(
295295
...entity,
296296
parent: normaliseParent(entity.parent),
297297
children: [],
298-
digest: createHash('sha256').update(JSON.stringify(entity)).digest('hex'),
298+
digest: hashFn(entity),
299299
});
300300
}
301301

spec/node.spec.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
import { caching } from 'cache-manager';
17+
import { createHash } from 'crypto';
1718

1819
import { computeEntityMap, normaliseParent, NodeManager } from '#node';
1920

@@ -74,13 +75,17 @@ function generatePage({
7475
};
7576
}
7677

78+
function hashFn(content: string | FullDatabase | FullPage): string {
79+
return createHash('sha256').update(JSON.stringify(content)).digest('hex');
80+
}
81+
7782
describe('fn:computeEntityMap', () => {
7883
it('pass with a workspace parent', () => {
7984
const workspaceDatabase = generateDatabase({
8085
databaseID: 'database_under_a_workspace',
8186
});
8287

83-
const map = computeEntityMap([workspaceDatabase]);
88+
const map = computeEntityMap([workspaceDatabase], hashFn);
8489
const normalised = map.get('database:database_under_a_workspace');
8590
expect(normalised!.id).toEqual('database_under_a_workspace');
8691
expect(normalised!.parent).toEqual(null);
@@ -93,7 +98,7 @@ describe('fn:computeEntityMap', () => {
9398
parent: { type: 'page_id', page_id: 'parent-page' },
9499
});
95100

96-
const map = computeEntityMap([pageDatabase]);
101+
const map = computeEntityMap([pageDatabase], hashFn);
97102
const normalised = map.get('database:database_under_a_page');
98103
expect(normalised!.id).toEqual('database_under_a_page');
99104
expect(normalised!.parent).toEqual({ object: 'page', id: 'parent-page' });
@@ -106,7 +111,7 @@ describe('fn:computeEntityMap', () => {
106111
parent: { type: 'database_id', database_id: 'missing' },
107112
});
108113

109-
const map = computeEntityMap([dangledPage]);
114+
const map = computeEntityMap([dangledPage], hashFn);
110115
const normalised = map.get('page:dangled_page');
111116
expect(normalised!.id).toEqual('dangled_page');
112117
expect(normalised!.parent).toEqual({ object: 'database', id: 'missing' });
@@ -129,7 +134,10 @@ describe('fn:computeEntityMap', () => {
129134
parent: { type: 'page_id', page_id: 'page_with_pages' },
130135
});
131136

132-
const map = computeEntityMap([database, ...database.pages, page, subpage]);
137+
const map = computeEntityMap(
138+
[database, ...database.pages, page, subpage],
139+
hashFn,
140+
);
133141
expect(map.size).toEqual(4);
134142

135143
const normalisedDB = map.get('database:database_with_pages');
@@ -194,7 +202,7 @@ describe('cl:NodeManager', () => {
194202
it('always keep gatsby synced', async () => {
195203
const createNode = jest.fn();
196204
const deleteNode = jest.fn();
197-
const createContentDigest = jest.fn(() => 'digest');
205+
const createContentDigest = jest.fn(hashFn);
198206
const createNodeId = jest.fn((id) => id);
199207

200208
const manager = new NodeManager({

0 commit comments

Comments
 (0)