🎨 Updated uuid to crypto.randomUUID()

This commit is contained in:
stevejubx 2024-08-23 13:56:58 +07:00
parent 5f9c0d21c5
commit d4489642d7
21 changed files with 52 additions and 54 deletions

View File

@ -3,7 +3,7 @@ const fs = require('fs-extra');
const path = require('path'); const path = require('path');
const os = require('os'); const os = require('os');
const glob = require('glob'); const glob = require('glob');
const uuid = require('uuid'); const crypto = require('crypto');
const config = require('../../../shared/config'); const config = require('../../../shared/config');
const {extract} = require('@tryghost/zip'); const {extract} = require('@tryghost/zip');
const tpl = require('@tryghost/tpl'); const tpl = require('@tryghost/tpl');
@ -220,7 +220,7 @@ class ImportManager {
* @returns {Promise<string>} full path to the extracted folder * @returns {Promise<string>} full path to the extracted folder
*/ */
async extractZip(filePath) { async extractZip(filePath) {
const tmpDir = path.join(os.tmpdir(), uuid.v4()); const tmpDir = path.join(os.tmpdir(), crypto.randomUUID());
this.fileToDelete = tmpDir; this.fileToDelete = tmpDir;
try { try {

View File

@ -1,6 +1,6 @@
const debug = require('@tryghost/debug')('importer:posts'); const debug = require('@tryghost/debug')('importer:posts');
const _ = require('lodash'); const _ = require('lodash');
const uuid = require('uuid'); const crypto = require('crypto');
const BaseImporter = require('./Base'); const BaseImporter = require('./Base');
const mobiledocLib = require('../../../../lib/mobiledoc'); const mobiledocLib = require('../../../../lib/mobiledoc');
const validator = require('@tryghost/validator'); const validator = require('@tryghost/validator');
@ -29,7 +29,7 @@ class PostsImporter extends BaseImporter {
sanitizeAttributes() { sanitizeAttributes() {
_.each(this.dataToImport, (obj) => { _.each(this.dataToImport, (obj) => {
if (!validator.isUUID(obj.uuid || '')) { if (!validator.isUUID(obj.uuid || '')) {
obj.uuid = uuid.v4(); obj.uuid = crypto.randomUUID();
} }
// we used to have post.page=true/false // we used to have post.page=true/false

View File

@ -1,5 +1,5 @@
const logging = require('@tryghost/logging'); const logging = require('@tryghost/logging');
const uuid = require('uuid'); const crypto = require('crypto');
const {createTransactionalMigration} = require('../../utils'); const {createTransactionalMigration} = require('../../utils');
module.exports = createTransactionalMigration( module.exports = createTransactionalMigration(
@ -10,7 +10,7 @@ module.exports = createTransactionalMigration(
// eslint-disable-next-line no-restricted-syntax // eslint-disable-next-line no-restricted-syntax
for (const newsletter of newslettersWithoutUUID) { for (const newsletter of newslettersWithoutUUID) {
await knex('newsletters').update('uuid', uuid.v4()).where('id', newsletter.id); await knex('newsletters').update('uuid', crypto.randomUUID()).where('id', newsletter.id);
} }
}, },
async function down() { async function down() {

View File

@ -1,5 +1,5 @@
const ObjectId = require('bson-objectid').default; const ObjectId = require('bson-objectid').default;
const uuid = require('uuid'); const crypto = require('crypto');
const logging = require('@tryghost/logging'); const logging = require('@tryghost/logging');
const startsWith = require('lodash/startsWith'); const startsWith = require('lodash/startsWith');
const {createTransactionalMigration} = require('../../utils'); const {createTransactionalMigration} = require('../../utils');
@ -9,7 +9,7 @@ module.exports = createTransactionalMigration(
// This uses the default settings from core/server/data/schema/default-settings/default-settings.json // This uses the default settings from core/server/data/schema/default-settings/default-settings.json
const newsletter = { const newsletter = {
id: (new ObjectId()).toHexString(), id: (new ObjectId()).toHexString(),
uuid: uuid.v4(), uuid: crypto.randomUUID(),
name: 'Ghost', name: 'Ghost',
description: '', description: '',
slug: 'default-newsletter', slug: 'default-newsletter',

View File

@ -1,4 +1,4 @@
const uuid = require('uuid'); const crypto = require('crypto');
const ghostBookshelf = require('./base'); const ghostBookshelf = require('./base');
const Email = ghostBookshelf.Model.extend({ const Email = ghostBookshelf.Model.extend({
@ -6,7 +6,7 @@ const Email = ghostBookshelf.Model.extend({
defaults: function defaults() { defaults: function defaults() {
return { return {
uuid: uuid.v4(), uuid: crypto.randomUUID(),
status: 'pending', status: 'pending',
recipient_filter: 'status:-free', recipient_filter: 'status:-free',
track_opens: false, track_opens: false,

View File

@ -1,5 +1,5 @@
const ghostBookshelf = require('./base'); const ghostBookshelf = require('./base');
const uuid = require('uuid'); const crypto = require('crypto');
const _ = require('lodash'); const _ = require('lodash');
const config = require('../../shared/config'); const config = require('../../shared/config');
const {gravatar} = require('../lib/image'); const {gravatar} = require('../lib/image');
@ -10,8 +10,8 @@ const Member = ghostBookshelf.Model.extend({
defaults() { defaults() {
return { return {
status: 'free', status: 'free',
uuid: uuid.v4(), uuid: crypto.randomUUID(),
transient_id: uuid.v4(), transient_id: crypto.randomUUID(),
email_count: 0, email_count: 0,
email_opened_count: 0, email_opened_count: 0,
enable_comment_notifications: true enable_comment_notifications: true

View File

@ -1,6 +1,6 @@
const ghostBookshelf = require('./base'); const ghostBookshelf = require('./base');
const ObjectID = require('bson-objectid').default; const ObjectID = require('bson-objectid').default;
const uuid = require('uuid'); const crypto = require('crypto');
const urlUtils = require('../../shared/url-utils'); const urlUtils = require('../../shared/url-utils');
const Newsletter = ghostBookshelf.Model.extend({ const Newsletter = ghostBookshelf.Model.extend({
@ -8,7 +8,7 @@ const Newsletter = ghostBookshelf.Model.extend({
defaults: function defaults() { defaults: function defaults() {
return { return {
uuid: uuid.v4(), uuid: crypto.randomUUID(),
sender_reply_to: 'newsletter', sender_reply_to: 'newsletter',
status: 'active', status: 'active',
visibility: 'members', visibility: 'members',

View File

@ -1,6 +1,6 @@
// # Post Model // # Post Model
const _ = require('lodash'); const _ = require('lodash');
const uuid = require('uuid'); const crypto = require('crypto');
const moment = require('moment'); const moment = require('moment');
const {sequence} = require('@tryghost/promise'); const {sequence} = require('@tryghost/promise');
const tpl = require('@tryghost/tpl'); const tpl = require('@tryghost/tpl');
@ -89,7 +89,7 @@ Post = ghostBookshelf.Model.extend({
} }
return { return {
uuid: uuid.v4(), uuid: crypto.randomUUID(),
status: 'draft', status: 'draft',
featured: false, featured: false,
type: 'post', type: 'post',

View File

@ -1,5 +1,4 @@
const _ = require('lodash'); const _ = require('lodash');
const uuid = require('uuid');
const crypto = require('crypto'); const crypto = require('crypto');
const keypair = require('keypair'); const keypair = require('keypair');
const ObjectID = require('bson-objectid').default; const ObjectID = require('bson-objectid').default;
@ -50,7 +49,7 @@ function parseDefaultSettings() {
const defaultSettingsFlattened = {}; const defaultSettingsFlattened = {};
const dynamicDefault = { const dynamicDefault = {
db_hash: () => uuid.v4(), db_hash: () => crypto.randomUUID(),
public_hash: () => crypto.randomBytes(15).toString('hex'), public_hash: () => crypto.randomBytes(15).toString('hex'),
admin_session_secret: () => crypto.randomBytes(32).toString('hex'), admin_session_secret: () => crypto.randomBytes(32).toString('hex'),
theme_session_secret: () => crypto.randomBytes(32).toString('hex'), theme_session_secret: () => crypto.randomBytes(32).toString('hex'),

View File

@ -1,4 +1,4 @@
const uuid = require('uuid'); const crypto = require('crypto');
/** /**
* @TODO: move this middleware to Framework monorepo? * @TODO: move this middleware to Framework monorepo?
@ -8,7 +8,7 @@ const uuid = require('uuid');
* @param {import('express').NextFunction} next * @param {import('express').NextFunction} next
*/ */
module.exports = function requestIdMw(req, res, next) { module.exports = function requestIdMw(req, res, next) {
const requestId = req.get('X-Request-ID') || uuid.v4(); const requestId = req.get('X-Request-ID') || crypto.randomUUID();
// Set a value for internal use // Set a value for internal use
req.requestId = requestId; req.requestId = requestId;

View File

@ -220,7 +220,6 @@
"sanitize-html": "2.13.0", "sanitize-html": "2.13.0",
"semver": "7.6.3", "semver": "7.6.3",
"stoppable": "1.1.0", "stoppable": "1.1.0",
"uuid": "9.0.1",
"ws": "8.18.0", "ws": "8.18.0",
"xml": "1.0.1", "xml": "1.0.1",
"y-protocols": "1.0.6", "y-protocols": "1.0.6",

View File

@ -1,7 +1,7 @@
const {agentProvider, mockManager, fixtureManager, matchers} = require('../../utils/e2e-framework'); const {agentProvider, mockManager, fixtureManager, matchers} = require('../../utils/e2e-framework');
const {anyContentVersion, anyEtag, anyString, anyContentLength} = matchers; const {anyContentVersion, anyEtag, anyString, anyContentLength} = matchers;
const uuid = require('uuid'); const crypto = require('crypto');
const should = require('should'); const should = require('should');
const Papa = require('papaparse'); const Papa = require('papaparse');
const models = require('../../../core/server/models'); const models = require('../../../core/server/models');
@ -9,7 +9,7 @@ const moment = require('moment');
async function createMember(data) { async function createMember(data) {
const member = await models.Member.add({ const member = await models.Member.add({
email: uuid.v4() + '@example.com', email: crypto.randomUUID() + '@example.com',
name: '', name: '',
email_disabled: false, email_disabled: false,
...data ...data

View File

@ -2,7 +2,7 @@ const path = require('path');
const _ = require('lodash'); const _ = require('lodash');
const os = require('os'); const os = require('os');
const fs = require('fs-extra'); const fs = require('fs-extra');
const uuid = require('uuid'); const crypto = require('crypto');
const should = require('should'); const should = require('should');
const supertest = require('supertest'); const supertest = require('supertest');
const sinon = require('sinon'); const sinon = require('sinon');
@ -61,7 +61,7 @@ describe('DB API', function () {
}); });
it('can export & import', function () { it('can export & import', function () {
const exportFolder = path.join(os.tmpdir(), uuid.v4()); const exportFolder = path.join(os.tmpdir(), crypto.randomUUID());
const exportPath = path.join(exportFolder, 'export.json'); const exportPath = path.join(exportFolder, 'export.json');
return request.put(localUtils.API.getApiQuery('settings/')) return request.put(localUtils.API.getApiQuery('settings/'))

View File

@ -3,7 +3,7 @@ const supertest = require('supertest');
const fs = require('fs-extra'); const fs = require('fs-extra');
const path = require('path'); const path = require('path');
const os = require('os'); const os = require('os');
const uuid = require('uuid'); const crypto = require('crypto');
const localUtils = require('./utils'); const localUtils = require('./utils');
const config = require('../../../../core/shared/config'); const config = require('../../../../core/shared/config');
@ -23,7 +23,7 @@ describe('Redirects API', function () {
// which is currently impossible with available test utils. // which is currently impossible with available test utils.
// The test itself should be broken down into a unit test for the // The test itself should be broken down into a unit test for the
// Redirects service class. // Redirects service class.
const contentFolder = path.join(os.tmpdir(), uuid.v4(), 'ghost-test'); const contentFolder = path.join(os.tmpdir(), crypto.randomUUID(), 'ghost-test');
fs.ensureDirSync(contentFolder); fs.ensureDirSync(contentFolder);
fs.ensureDirSync(path.join(contentFolder, 'data')); fs.ensureDirSync(path.join(contentFolder, 'data'));
fs.writeFileSync(path.join(contentFolder, 'data', 'redirects.json'), JSON.stringify([])); fs.writeFileSync(path.join(contentFolder, 'data', 'redirects.json'), JSON.stringify([]));

View File

@ -20,7 +20,7 @@ const {AsymmetricMatcher} = require('expect');
const fs = require('fs-extra'); const fs = require('fs-extra');
const path = require('path'); const path = require('path');
const os = require('os'); const os = require('os');
const uuid = require('uuid'); const crypto = require('crypto');
const fixtureUtils = require('./fixture-utils'); const fixtureUtils = require('./fixture-utils');
const redirectsUtils = require('./redirects'); const redirectsUtils = require('./redirects');
@ -64,7 +64,7 @@ const startGhost = async (options = {}) => {
* We never use the root content folder for testing! * We never use the root content folder for testing!
* We use a tmp folder. * We use a tmp folder.
*/ */
const contentFolder = path.join(os.tmpdir(), uuid.v4(), 'ghost-test'); const contentFolder = path.join(os.tmpdir(), crypto.randomUUID(), 'ghost-test');
await prepareContentFolder({contentFolder}); await prepareContentFolder({contentFolder});
// NOTE: need to pass this config to the server instance // NOTE: need to pass this config to the server instance

View File

@ -4,7 +4,7 @@ const _ = require('lodash');
const fs = require('fs-extra'); const fs = require('fs-extra');
const path = require('path'); const path = require('path');
const os = require('os'); const os = require('os');
const uuid = require('uuid'); const crypto = require('crypto');
// Ghost Internals // Ghost Internals
const boot = require('../../core/boot'); const boot = require('../../core/boot');
@ -213,7 +213,7 @@ const startGhost = async (options) => {
forceStart: false, forceStart: false,
copyThemes: false, copyThemes: false,
copySettings: false, copySettings: false,
contentFolder: path.join(os.tmpdir(), uuid.v4(), 'ghost-test'), contentFolder: path.join(os.tmpdir(), crypto.randomUUID(), 'ghost-test'),
subdir: false subdir: false
}, options); }, options);

View File

@ -2,7 +2,7 @@
const _ = require('lodash'); const _ = require('lodash');
const path = require('path'); const path = require('path');
const fs = require('fs-extra'); const fs = require('fs-extra');
const uuid = require('uuid'); const crypto = require('crypto');
const ObjectId = require('bson-objectid').default; const ObjectId = require('bson-objectid').default;
const KnexMigrator = require('knex-migrator'); const KnexMigrator = require('knex-migrator');
const {sequence} = require('@tryghost/promise'); const {sequence} = require('@tryghost/promise');
@ -160,7 +160,7 @@ const fixtures = {
let i; let i;
for (i = 0; i < max; i += 1) { for (i = 0; i < max; i += 1) {
tagName = uuid.v4().split('-')[0]; tagName = crypto.randomUUID().split('-')[0];
tags.push(DataGenerator.forKnex.createBasic({name: tagName, slug: tagName})); tags.push(DataGenerator.forKnex.createBasic({name: tagName, slug: tagName}));
} }

View File

@ -1,5 +1,5 @@
const _ = require('lodash'); const _ = require('lodash');
const uuid = require('uuid'); const crypto = require('crypto');
const ObjectId = require('bson-objectid').default; const ObjectId = require('bson-objectid').default;
const moment = require('moment'); const moment = require('moment');
const constants = require('@tryghost/constants'); const constants = require('@tryghost/constants');
@ -1037,7 +1037,7 @@ DataGenerator.forKnex = (function () {
return _.defaults(newObj, { return _.defaults(newObj, {
id: ObjectId().toHexString(), id: ObjectId().toHexString(),
uuid: uuid.v4(), uuid: crypto.randomUUID(),
title: 'title', title: 'title',
status: 'published', status: 'published',
feature_image: null, feature_image: null,
@ -1107,7 +1107,7 @@ DataGenerator.forKnex = (function () {
return _.defaults(newObj, { return _.defaults(newObj, {
id: ObjectId().toHexString(), id: ObjectId().toHexString(),
uuid: uuid.v4(), uuid: crypto.randomUUID(),
secret: 'not_available', secret: 'not_available',
redirection_uri: 'http://localhost:9999', redirection_uri: 'http://localhost:9999',
client_uri: 'http://localhost:9000', client_uri: 'http://localhost:9000',
@ -1156,7 +1156,7 @@ DataGenerator.forKnex = (function () {
const newObj = _.cloneDeep(overrides); const newObj = _.cloneDeep(overrides);
return _.defaults(newObj, { return _.defaults(newObj, {
id: ObjectId().toHexString(), id: ObjectId().toHexString(),
uuid: uuid.v4(), uuid: crypto.randomUUID(),
slug: 'daily-newsletter', slug: 'daily-newsletter',
name: 'Daily Newsletter', name: 'Daily Newsletter',
sender_name: 'Jamie Larsen', sender_name: 'Jamie Larsen',
@ -1299,7 +1299,7 @@ DataGenerator.forKnex = (function () {
return _.defaults(newObj, { return _.defaults(newObj, {
id: ObjectId().toHexString(), id: ObjectId().toHexString(),
token: uuid.v4(), token: crypto.randomUUID(),
expires: Date.now() + constants.ONE_DAY_MS expires: Date.now() + constants.ONE_DAY_MS
}); });
} }
@ -1309,7 +1309,7 @@ DataGenerator.forKnex = (function () {
return _.defaults(newObj, { return _.defaults(newObj, {
id: ObjectId().toHexString(), id: ObjectId().toHexString(),
token: uuid.v4(), token: crypto.randomUUID(),
email: 'test@ghost.org', email: 'test@ghost.org',
role_id: DataGenerator.Content.roles[0].id, role_id: DataGenerator.Content.roles[0].id,
expires: Date.now() + (60 * 1000), expires: Date.now() + (60 * 1000),

View File

@ -7,7 +7,7 @@ const {SubscriptionActivatedEvent, MemberCreatedEvent, SubscriptionCreatedEvent,
const ObjectId = require('bson-objectid').default; const ObjectId = require('bson-objectid').default;
const {NotFoundError} = require('@tryghost/errors'); const {NotFoundError} = require('@tryghost/errors');
const validator = require('@tryghost/validator'); const validator = require('@tryghost/validator');
const uuid = require('uuid'); const crypto = require('crypto');
const messages = { const messages = {
noStripeConnection: 'Cannot {action} without a Stripe Connection', noStripeConnection: 'Cannot {action} without a Stripe Connection',
@ -228,7 +228,7 @@ module.exports = class MemberRepository {
} }
_generateTransientId() { _generateTransientId() {
return uuid.v4(); return crypto.randomUUID();
} }
async cycleTransientId({id, email}) { async cycleTransientId({id, email}) {

View File

@ -1,12 +1,12 @@
require('./utils'); require('./utils');
const should = require('should'); const should = require('should');
const uuid = require('uuid'); const crypto = require('crypto');
const security = require('../'); const security = require('../');
describe('Utils: tokens', function () { describe('Utils: tokens', function () {
it('generate', function () { it('generate', function () {
const expires = Date.now() + 60 * 1000; const expires = Date.now() + 60 * 1000;
const dbHash = uuid.v4(); const dbHash = crypto.randomUUID();
let token; let token;
token = security.tokens.resetToken.generateHash({ token = security.tokens.resetToken.generateHash({
@ -22,7 +22,7 @@ describe('Utils: tokens', function () {
it('compare: success', function () { it('compare: success', function () {
const expires = Date.now() + 60 * 1000; const expires = Date.now() + 60 * 1000;
const dbHash = uuid.v4(); const dbHash = crypto.randomUUID();
let token; let token;
let tokenIsCorrect; let tokenIsCorrect;
@ -45,7 +45,7 @@ describe('Utils: tokens', function () {
it('compare: error from invalid password', function () { it('compare: error from invalid password', function () {
const expires = Date.now() + 60 * 1000; const expires = Date.now() + 60 * 1000;
const dbHash = uuid.v4(); const dbHash = crypto.randomUUID();
let token; let token;
let tokenIsCorrect; let tokenIsCorrect;
@ -68,7 +68,7 @@ describe('Utils: tokens', function () {
it('compare: error from invalid expires parameter', function () { it('compare: error from invalid expires parameter', function () {
const invalidDate = 'not a date'; const invalidDate = 'not a date';
const dbHash = uuid.v4(); const dbHash = crypto.randomUUID();
let token; let token;
let tokenIsCorrect; let tokenIsCorrect;
@ -91,7 +91,7 @@ describe('Utils: tokens', function () {
it('compare: error from expired token', function () { it('compare: error from expired token', function () {
const dateInThePast = Date.now() - 60 * 1000; const dateInThePast = Date.now() - 60 * 1000;
const dbHash = uuid.v4(); const dbHash = crypto.randomUUID();
let token; let token;
let tokenIsCorrect; let tokenIsCorrect;
@ -114,7 +114,7 @@ describe('Utils: tokens', function () {
it('extract', function () { it('extract', function () {
const expires = Date.now() + 60 * 1000; const expires = Date.now() + 60 * 1000;
const dbHash = uuid.v4(); const dbHash = crypto.randomUUID();
let token; let token;
let parts; let parts;
const email = 'test1@ghost.org'; const email = 'test1@ghost.org';
@ -138,7 +138,7 @@ describe('Utils: tokens', function () {
it('extract - hashed password', function () { it('extract - hashed password', function () {
const expires = Date.now() + 60 * 1000; const expires = Date.now() + 60 * 1000;
const dbHash = uuid.v4(); const dbHash = crypto.randomUUID();
let token; let token;
let parts; let parts;
const email = 'test3@ghost.org'; const email = 'test3@ghost.org';
@ -163,7 +163,7 @@ describe('Utils: tokens', function () {
it('can validate an URI encoded reset token', function () { it('can validate an URI encoded reset token', function () {
const expires = Date.now() + 60 * 1000; const expires = Date.now() + 60 * 1000;
const email = 'test1@ghost.org'; const email = 'test1@ghost.org';
const dbHash = uuid.v4(); const dbHash = crypto.randomUUID();
let token; let token;
let tokenIsCorrect; let tokenIsCorrect;
let parts; let parts;

View File

@ -2,7 +2,7 @@ require('./utils');
const sinon = require('sinon'); const sinon = require('sinon');
const moment = require('moment'); const moment = require('moment');
const uuid = require('uuid'); const crypto = require('crypto');
const assert = require('assert/strict'); const assert = require('assert/strict');
const logging = require('@tryghost/logging'); const logging = require('@tryghost/logging');
@ -196,7 +196,7 @@ describe('Update Check', function () {
id: 1, id: 1,
custom: 0, custom: 0,
messages: [{ messages: [{
id: uuid.v4(), id: crypto.randomUUID(),
version: '999.9.x', version: '999.9.x',
content: '<p>Hey there! This is for 999.9.0 version</p>', content: '<p>Hey there! This is for 999.9.0 version</p>',
dismissible: true, dismissible: true,
@ -268,7 +268,7 @@ describe('Update Check', function () {
const notification = { const notification = {
id: 1, id: 1,
messages: [{ messages: [{
id: uuid.v4(), id: crypto.randomUUID(),
version: 'custom1', version: 'custom1',
content: '<p>Critical message. Upgrade your site!</p>', content: '<p>Critical message. Upgrade your site!</p>',
dismissible: false, dismissible: false,