mirror of
https://github.com/laurent22/joplin.git
synced 2024-11-27 08:21:03 +02:00
Chore: Server: Remove direct dependecy from nanoid library (#9501)
This commit is contained in:
parent
bce94f1775
commit
04e0274055
@ -8,6 +8,7 @@ import { customAlphabet } from 'nanoid/non-secure';
|
||||
// > indefinitely
|
||||
const nanoid = customAlphabet('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', 22);
|
||||
|
||||
|
||||
export default {
|
||||
create: function(): string {
|
||||
return uuidv4().replace(/-/g, '');
|
||||
@ -19,3 +20,20 @@ export default {
|
||||
return customAlphabet('0123456789abcdefghijklmnopqrstuvwxyz', 8)();
|
||||
},
|
||||
};
|
||||
|
||||
type FuncUiidGen = (length?: number)=> string;
|
||||
|
||||
const cachedUuidgen: Record<number, FuncUiidGen> = {};
|
||||
const createUuidgenCustomAlphabet = (length: number) => customAlphabet('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', length);
|
||||
|
||||
const getCachedUuidgen = (length: number) => {
|
||||
if (cachedUuidgen[length]) return cachedUuidgen[length];
|
||||
|
||||
cachedUuidgen[length] = createUuidgenCustomAlphabet(length);
|
||||
return cachedUuidgen[length];
|
||||
};
|
||||
|
||||
export const uuidgen = (length = 22) => {
|
||||
const cachedUuidgen = getCachedUuidgen(length);
|
||||
return cachedUuidgen();
|
||||
};
|
||||
|
@ -43,7 +43,6 @@
|
||||
"ldapts": "7.0.6",
|
||||
"markdown-it": "13.0.2",
|
||||
"mustache": "4.2.0",
|
||||
"nanoid": "2.1.11",
|
||||
"node-cron": "3.0.3",
|
||||
"nodemailer": "6.9.7",
|
||||
"nodemon": "3.0.1",
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { Knex } from 'knex';
|
||||
import { DbConnection, defaultAdminEmail, defaultAdminPassword } from '../db';
|
||||
import { hashPassword } from '../utils/auth';
|
||||
import uuidgen from '../utils/uuidgen';
|
||||
import { uuidgen } from '@joplin/lib/uuid';
|
||||
|
||||
export const up = async (db: DbConnection) => {
|
||||
await db.schema.createTable('users', (table: Knex.CreateTableBuilder) => {
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { WithDates, WithUuid, databaseSchema, ItemType, Uuid, User } from '../services/database/types';
|
||||
import { DbConnection, QueryContext } from '../db';
|
||||
import TransactionHandler from '../utils/TransactionHandler';
|
||||
import uuidgen from '../utils/uuidgen';
|
||||
import { uuidgen } from '@joplin/lib/uuid';
|
||||
import { ErrorUnprocessableEntity, ErrorBadRequest } from '../utils/errors';
|
||||
import { Models, NewModelFactoryHandler } from './factory';
|
||||
import { Config, Env } from '../utils/types';
|
||||
|
@ -3,7 +3,7 @@ import { Uuid } from '../services/database/types';
|
||||
import { LockType, Lock, LockClientType, defaultLockTtl, activeLock } from '@joplin/lib/services/synchronizer/LockHandler';
|
||||
import { Value } from './KeyValueModel';
|
||||
import { ErrorConflict, ErrorUnprocessableEntity, ErrorCode } from '../utils/errors';
|
||||
import uuidgen from '../utils/uuidgen';
|
||||
import { uuidgen } from '@joplin/lib/uuid';
|
||||
|
||||
export default class LockModel extends BaseModel<Lock> {
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { Notification, NotificationLevel, Uuid } from '../services/database/types';
|
||||
import { ErrorUnprocessableEntity } from '../utils/errors';
|
||||
import uuidgen from '../utils/uuidgen';
|
||||
import { uuidgen } from '@joplin/lib/uuid';
|
||||
import BaseModel, { ValidateOptions } from './BaseModel';
|
||||
|
||||
export enum NotificationKey {
|
||||
|
@ -1,6 +1,6 @@
|
||||
import BaseModel from './BaseModel';
|
||||
import { User, Session, Uuid } from '../services/database/types';
|
||||
import uuidgen from '../utils/uuidgen';
|
||||
import { uuidgen } from '@joplin/lib/uuid';
|
||||
import { ErrorForbidden } from '../utils/errors';
|
||||
import { Hour } from '../utils/time';
|
||||
|
||||
|
@ -2,7 +2,7 @@ import { Knex } from 'knex';
|
||||
import { EmailSender, Subscription, User, UserFlagType, Uuid } from '../services/database/types';
|
||||
import { ErrorNotFound } from '../utils/errors';
|
||||
import { Day } from '../utils/time';
|
||||
import uuidgen from '../utils/uuidgen';
|
||||
import { uuidgen } from '@joplin/lib/uuid';
|
||||
import paymentFailedTemplate from '../views/emails/paymentFailedTemplate';
|
||||
import BaseModel from './BaseModel';
|
||||
import { AccountType } from './UserModel';
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { Token, User, Uuid } from '../services/database/types';
|
||||
import { ErrorForbidden, ErrorNotFound } from '../utils/errors';
|
||||
import uuidgen from '../utils/uuidgen';
|
||||
import { uuidgen } from '@joplin/lib/uuid';
|
||||
import BaseModel from './BaseModel';
|
||||
|
||||
export default class TokenModel extends BaseModel<Token> {
|
||||
|
@ -2,7 +2,7 @@ import { User } from '../../services/database/types';
|
||||
import routeHandler from '../../middleware/routeHandler';
|
||||
import { execRequest } from '../../utils/testing/apiUtils';
|
||||
import { beforeAllDb, afterAllTests, beforeEachDb, koaAppContext, createUserAndSession, models, checkContextError, expectHttpError } from '../../utils/testing/testUtils';
|
||||
import uuidgen from '../../utils/uuidgen';
|
||||
import { uuidgen } from '@joplin/lib/uuid';
|
||||
import { ErrorForbidden } from '../../utils/errors';
|
||||
|
||||
async function postUser(sessionId: string, email: string, password: string = null, props: any = null): Promise<User> {
|
||||
|
@ -11,7 +11,7 @@ import { View } from '../../services/MustacheService';
|
||||
import defaultView from '../../utils/defaultView';
|
||||
import { AclAction } from '../../models/BaseModel';
|
||||
import { AccountType, accountTypeOptions, accountTypeToString } from '../../models/UserModel';
|
||||
import uuidgen from '../../utils/uuidgen';
|
||||
import { uuidgen } from '@joplin/lib/uuid';
|
||||
import { formatMaxItemSize, formatMaxTotalSize, formatTotalSize, formatTotalSizePercent, yesOrNo } from '../../utils/strings';
|
||||
import { getCanShareFolder, totalSizeClass } from '../../models/utils/user';
|
||||
import { yesNoDefaultOptions, yesNoOptions } from '../../utils/views/select';
|
||||
|
@ -6,7 +6,7 @@ import { RouteType } from '../../utils/types';
|
||||
import { AppContext } from '../../utils/types';
|
||||
import { ErrorNotFound } from '../../utils/errors';
|
||||
import { AclAction } from '../../models/BaseModel';
|
||||
import uuidgen from '../../utils/uuidgen';
|
||||
import { uuidgen } from '@joplin/lib/uuid';
|
||||
|
||||
const router = new Router(RouteType.Api);
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { beforeAllDb, afterAllTests, beforeEachDb, createUserAndSession, models, expectHttpError } from '../../utils/testing/testUtils';
|
||||
import { execRequest } from '../../utils/testing/apiUtils';
|
||||
import uuidgen from '../../utils/uuidgen';
|
||||
import { uuidgen } from '@joplin/lib/uuid';
|
||||
import { ErrorNotFound } from '../../utils/errors';
|
||||
|
||||
describe('index/password', () => {
|
||||
|
@ -5,7 +5,7 @@ import { MB } from '../../utils/bytes';
|
||||
import { cookieGet } from '../../utils/cookies';
|
||||
import { execRequestC } from '../../utils/testing/apiUtils';
|
||||
import { beforeAllDb, afterAllTests, beforeEachDb, models } from '../../utils/testing/testUtils';
|
||||
import uuidgen from '../../utils/uuidgen';
|
||||
import { uuidgen } from '@joplin/lib/uuid';
|
||||
import { FormUser } from './signup';
|
||||
|
||||
describe('index_signup', () => {
|
||||
|
@ -4,7 +4,7 @@ import { AccountType } from '../../models/UserModel';
|
||||
import { betaUserTrialPeriodDays, isBetaUser, stripeConfig } from '../../utils/stripe';
|
||||
import { beforeAllDb, afterAllTests, beforeEachDb, models, koaAppContext, expectNotThrow } from '../../utils/testing/testUtils';
|
||||
import { AppContext } from '../../utils/types';
|
||||
import uuidgen from '../../utils/uuidgen';
|
||||
import { uuidgen } from '@joplin/lib/uuid';
|
||||
import { postHandlers } from './stripe';
|
||||
|
||||
interface StripeOptions {
|
||||
|
@ -5,7 +5,7 @@ import { cookieGet } from '../../utils/cookies';
|
||||
import { ErrorForbidden } from '../../utils/errors';
|
||||
import { execRequest, execRequestC } from '../../utils/testing/apiUtils';
|
||||
import { beforeAllDb, afterAllTests, beforeEachDb, koaAppContext, createUserAndSession, models, parseHtml, checkContextError, expectHttpError, expectThrow } from '../../utils/testing/testUtils';
|
||||
import uuidgen from '../../utils/uuidgen';
|
||||
import { uuidgen } from '@joplin/lib/uuid';
|
||||
|
||||
async function postUser(sessionId: string, email: string, password: string = null, props: any = null): Promise<User> {
|
||||
password = password === null ? uuidgen() : password;
|
||||
|
@ -36,7 +36,7 @@
|
||||
// import BaseController from '../BaseController';
|
||||
// import mustacheService from '../../services/MustacheService';
|
||||
// import { ErrorNotFound } from '../../utils/errors';
|
||||
// import uuidgen from '../../utils/uuidgen';
|
||||
// import { uuidgen } from '@joplin/lib/uuid';
|
||||
// import controllers from '../factory';
|
||||
|
||||
// export default class OAuthController extends BaseController {
|
||||
|
@ -4,7 +4,7 @@ import loadStorageDriver from '../models/items/storage/loadStorageDriver';
|
||||
import parseStorageConnectionString from '../models/items/storage/parseStorageConnectionString';
|
||||
import { Context } from '../models/items/storage/StorageDriverBase';
|
||||
import { StorageDriverConfig, StorageDriverType } from './types';
|
||||
import uuidgen from './uuidgen';
|
||||
import { uuidgen } from '@joplin/lib/uuid';
|
||||
|
||||
export default async function(connection: string | StorageDriverConfig, db: DbConnection, models: Models): Promise<string> {
|
||||
const storageConfig = typeof connection === 'string' ? parseStorageConnectionString(connection) : connection;
|
||||
|
@ -20,7 +20,7 @@ import { FolderEntity, NoteEntity, ResourceEntity } from '@joplin/lib/services/d
|
||||
import { ModelType } from '@joplin/lib/BaseModel';
|
||||
import { initializeJoplinUtils } from '../joplinUtils';
|
||||
import MustacheService from '../../services/MustacheService';
|
||||
import uuidgen from '../uuidgen';
|
||||
import { uuidgen } from '@joplin/lib/uuid';
|
||||
import { createCsrfToken } from '../csrf';
|
||||
import { cookieSet } from '../cookies';
|
||||
import { parseEnv } from '../../env';
|
||||
|
@ -1,10 +0,0 @@
|
||||
const generate = require('nanoid/generate');
|
||||
|
||||
// https://zelark.github.io/nano-id-cc/
|
||||
// https://security.stackexchange.com/a/41749/1873
|
||||
// > On the other hand, 128 bits (between 21 and 22 characters
|
||||
// > alphanumeric) is beyond the reach of brute-force attacks pretty much
|
||||
// > indefinitely
|
||||
export default function uuidgen(length = 22): string {
|
||||
return generate('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', length);
|
||||
}
|
@ -6949,7 +6949,6 @@ __metadata:
|
||||
ldapts: 7.0.6
|
||||
markdown-it: 13.0.2
|
||||
mustache: 4.2.0
|
||||
nanoid: 2.1.11
|
||||
node-cron: 3.0.3
|
||||
node-mocks-http: 1.13.0
|
||||
nodemailer: 6.9.7
|
||||
@ -30568,13 +30567,6 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"nanoid@npm:2.1.11":
|
||||
version: 2.1.11
|
||||
resolution: "nanoid@npm:2.1.11"
|
||||
checksum: 18cd14386816873849787eb4e65667021bfdeb019a8f14c74287c23594c67b7c0e8f42c7d69f6aedf05cd3d100f1ddc41184f9f9b6b17fbaea1c3ee3f0704eec
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"nanoid@npm:3.3.7":
|
||||
version: 3.3.7
|
||||
resolution: "nanoid@npm:3.3.7"
|
||||
|
Loading…
Reference in New Issue
Block a user