Skip to content

Commit

Permalink
Merge pull request #9 from simplyStyle/main
Browse files Browse the repository at this point in the history
  • Loading branch information
tianyingchun committed May 22, 2024
2 parents e20e59d + 0d04d54 commit 902701f
Show file tree
Hide file tree
Showing 16 changed files with 168 additions and 151 deletions.
3 changes: 3 additions & 0 deletions next-mate.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,8 @@
"./packages/next-prisma",
],
"typescript.tsdk": "root/node_modules/typescript/lib",
"[prisma]": {
"editor.defaultFormatter": "Prisma.prisma",
},
},
}
6 changes: 3 additions & 3 deletions packages/next-auth/copyPrisma.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from 'fs';
import * as path from 'path';
import { promisify } from 'util';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { promisify } from 'node:util';

const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
Expand Down
5 changes: 2 additions & 3 deletions packages/next-auth/src/lucia/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { TimeSpan, createDate, isWithinExpirationDate } from 'oslo';
import type { Cookie, CookieAttributes } from 'oslo/cookie';
import { CookieController } from 'oslo/cookie';

import type { PasswordHashingAlgorithm } from './crypto.js';
import { generateIdFromEntropySize } from './crypto.js';
import type { Adapter, DatabaseAuth, DatabaseUser } from './database.js';
import type {
Expand All @@ -12,8 +12,7 @@ import type {
RegisteredDatabaseAuthAttributes,
RegisteredLucia,
UserId,
PasswordHashingAlgorithm,
} from './index.js';
} from './type.js';

type SessionAttributes =
RegisteredLucia extends Lucia<infer _SessionAttributes, any>
Expand Down
30 changes: 0 additions & 30 deletions packages/next-auth/src/lucia/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,3 @@ export class Scrypt implements PasswordHashingAlgorithm {
return constantTimeEqual(targetKey, decodeHex(key));
}
}

export class LegacyScrypt implements PasswordHashingAlgorithm {
async hash(password: string): Promise<string> {
const salt = encodeHex(crypto.getRandomValues(new Uint8Array(16)));
const key = await generateScryptKey(password.normalize('NFKC'), salt);
return `s2:${salt}:${encodeHex(key)}`;
}
async verify(hash: string, password: string): Promise<boolean> {
const parts = hash.split(':');
if (parts.length === 2) {
const [salt, key] = parts;
const targetKey = await generateScryptKey(
password.normalize('NFKC'),
salt,
8
);
return constantTimeEqual(targetKey, decodeHex(key));
}
if (parts.length !== 3) return false;
const [version, salt, key] = parts;
if (version === 's2') {
const targetKey = await generateScryptKey(
password.normalize('NFKC'),
salt
);
return constantTimeEqual(targetKey, decodeHex(key));
}
return false;
}
}
2 changes: 1 addition & 1 deletion packages/next-auth/src/lucia/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
RegisteredDatabaseUserAttributes,
RegisteredDatabaseAuthAttributes,
UserId,
} from './index.js';
} from './type.js';

export interface Adapter {
getSessionAndUser(
Expand Down
46 changes: 2 additions & 44 deletions packages/next-auth/src/lucia/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
/* eslint-disable @typescript-eslint/ban-types */
/* eslint-disable @typescript-eslint/no-explicit-any */
export { Lucia } from './core.js';
export {
Scrypt,
LegacyScrypt,
generateId,
generateIdFromEntropySize,
} from './crypto.js';
export { Scrypt, generateId, generateIdFromEntropySize } from './crypto.js';
export { TimeSpan } from 'oslo';
export { Cookie } from 'oslo/cookie';
export { verifyRequestOrigin } from 'oslo/request';
Expand All @@ -25,39 +18,4 @@ export type {
} from './database.js';
export type { PasswordHashingAlgorithm } from './crypto.js';
export type { CookieAttributes } from 'oslo/cookie';

import type { Lucia } from './core.js';

export interface Register {}

export type UserId = Register extends {
UserId: infer _UserId;
}
? _UserId
: string;

export type RegisteredLucia = Register extends {
Lucia: infer _Lucia;
}
? _Lucia extends Lucia<any, any>
? _Lucia
: Lucia
: Lucia;

export type RegisteredDatabaseUserAttributes = Register extends {
DatabaseUserAttributes: infer _DatabaseUserAttributes;
}
? _DatabaseUserAttributes
: {};

export type RegisteredDatabaseSessionAttributes = Register extends {
DatabaseSessionAttributes: infer _DatabaseSessionAttributes;
}
? _DatabaseSessionAttributes
: {};

export type RegisteredDatabaseAuthAttributes = Register extends {
DatabaseAuthAttributes: infer _DatabaseAuthAttributes;
}
? _DatabaseAuthAttributes
: {};
export * from './type.js';
37 changes: 37 additions & 0 deletions packages/next-auth/src/lucia/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* eslint-disable @typescript-eslint/ban-types */
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { Lucia } from './core.js';

export interface Register {}

export type UserId = Register extends {
UserId: infer _UserId;
}
? _UserId
: string;

export type RegisteredLucia = Register extends {
Lucia: infer _Lucia;
}
? _Lucia extends Lucia<any, any>
? _Lucia
: Lucia
: Lucia;

export type RegisteredDatabaseUserAttributes = Register extends {
DatabaseUserAttributes: infer _DatabaseUserAttributes;
}
? _DatabaseUserAttributes
: {};

export type RegisteredDatabaseSessionAttributes = Register extends {
DatabaseSessionAttributes: infer _DatabaseSessionAttributes;
}
? _DatabaseSessionAttributes
: {};

export type RegisteredDatabaseAuthAttributes = Register extends {
DatabaseAuthAttributes: infer _DatabaseAuthAttributes;
}
? _DatabaseAuthAttributes
: {};
5 changes: 1 addition & 4 deletions packages/next-prisma/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@
"@prisma/generator-helper": "5.13.0",
"@prisma/internals": "5.13.0",
"dotenv": "16.4.5",
"fs": "^0.0.1-security",
"glob": "^10.3.15",
"path": "^0.12.7",
"util": "^0.12.5"
"glob": "^10.3.15"
},
"devDependencies": {
"@armit/eslint-config-bases": "^0.1.5",
Expand Down
6 changes: 3 additions & 3 deletions packages/next-prisma/src/merge.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import * as fs from 'fs';
import * as path from 'path';
import { promisify } from 'util';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { promisify } from 'node:util';
import {
type DMMF,
type DataSource,
Expand Down
29 changes: 29 additions & 0 deletions packages/next-prisma/tests/__snapshots__/prisma-merge.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`merge prisma 1`] = `
"// *** GENERATED BY next-prisma :: DO NOT EDIT ***
model User {
id Int @unique
name String
content Int
desc String
}
model Post {
id String @unique
name String
}"
`;

exports[`merge prisma cover 1`] = `
"// *** GENERATED BY next-prisma :: DO NOT EDIT ***
model User {
id Int @unique
name Int
content String
desc String
}
model Post {
id String @unique
name String
}"
`;
46 changes: 46 additions & 0 deletions packages/next-prisma/tests/prisma-merge.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as fs from 'node:fs';
import { promisify } from 'node:util';

import { prismaMerge } from '../src/index.js';

const readFile = promisify(fs.readFile);

test('merge prisma', async () => {
const luciaPrismaContent = `
model User {
id String @unique
name String
}
`;

await prismaMerge({
prePrismaModel: luciaPrismaContent,
input: ['tests/prisma/test1.prisma'],
output: 'tests/prisma/testMerge1.prisma',
});

const prismaContent = await readFile('tests/prisma/testMerge1.prisma', {
encoding: 'utf-8',
});
expect(prismaContent).toMatchSnapshot();
});

test('merge prisma cover', async () => {
const luciaPrismaContent = `
model User {
id String @unique
name String
}
`;

await prismaMerge({
prePrismaModel: luciaPrismaContent,
input: ['tests/prisma/test1.prisma', 'tests/prisma/test2.prisma'],
output: 'tests/prisma/testMerge2.prisma',
});

const prismaContent = await readFile('tests/prisma/testMerge2.prisma', {
encoding: 'utf-8',
});
expect(prismaContent).toMatchSnapshot();
});
10 changes: 10 additions & 0 deletions packages/next-prisma/tests/prisma/test1.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
model User {
id Int @unique
content Int
desc String
}

model Post {
id String @unique
name String
}
5 changes: 5 additions & 0 deletions packages/next-prisma/tests/prisma/test2.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
model User {
id Int @unique
name Int
content String
}
11 changes: 11 additions & 0 deletions packages/next-prisma/tests/prisma/testMerge1.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// *** GENERATED BY next-prisma :: DO NOT EDIT ***
model User {
id Int @unique
name String
content Int
desc String
}
model Post {
id String @unique
name String
}
11 changes: 11 additions & 0 deletions packages/next-prisma/tests/prisma/testMerge2.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// *** GENERATED BY next-prisma :: DO NOT EDIT ***
model User {
id Int @unique
name Int
content String
desc String
}
model Post {
id String @unique
name String
}
Loading

0 comments on commit 902701f

Please sign in to comment.