Skip to content

Commit

Permalink
feat: drop crypto dependency, convert to ESM (#6603)
Browse files Browse the repository at this point in the history
Co-authored-by: Balázs Orbán <info@balazsorban.com>

BREAKING CHANGE:
- This package now only ships ESM, as all maintained Node.js versions have native support
- Dropped the `crypto` Node.js import in favor of `uuid`. When `globalThis.crypto` is the default in the future, we can remove `uuid` again
  • Loading branch information
ThangHuuVu authored Feb 5, 2023
1 parent 1e7538a commit 28583b8
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 98 deletions.
22 changes: 18 additions & 4 deletions packages/adapter-dynamodb/package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
{
"name": "@next-auth/dynamodb-adapter",
"repository": "https://github.com/nextauthjs/next-auth",
"version": "1.2.0",
"version": "2.0.0",
"description": "AWS DynamoDB adapter for next-auth.",
"keywords": [
"next-auth",
"next.js",
"oauth",
"dynamodb"
],
"type": "module",
"types": "./index.d.ts",
"homepage": "https://authjs.dev",
"bugs": {
"url": "https://github.com/nextauthjs/next-auth/issues"
},
"main": "dist/index.js",
"exports": {
".": {
"types": "./index.d.ts",
"import": "./index.js"
}
},
"private": false,
"publishConfig": {
"access": "public"
Expand All @@ -26,7 +33,10 @@
},
"files": [
"README.md",
"dist"
"index.js",
"index.d.ts",
"index.d.ts.map",
"src"
],
"author": "Pol Marnette",
"license": "ISC",
Expand All @@ -41,7 +51,11 @@
"@next-auth/adapter-test": "workspace:*",
"@next-auth/tsconfig": "workspace:*",
"@shelf/jest-dynamodb": "^2.1.0",
"@types/uuid": "^9.0.0",
"jest": "^27.4.3",
"next-auth": "workspace:*"
},
"dependencies": {
"uuid": "^9.0.0"
}
}
}
106 changes: 86 additions & 20 deletions packages/adapter-dynamodb/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { randomBytes } from "crypto"
import { v4 as uuid } from "uuid"

import type {
BatchWriteCommandInput,
Expand All @@ -12,16 +12,12 @@ import type {
VerificationToken,
} from "next-auth/adapters"

import { format, generateUpdateExpression } from "./utils"

export { format, generateUpdateExpression }

export interface DynamoDBAdapterOptions {
tableName?: string,
partitionKey?: string,
sortKey?: string,
indexName?: string,
indexPartitionKey?: string,
tableName?: string
partitionKey?: string
sortKey?: string
indexName?: string
indexPartitionKey?: string
indexSortKey?: string
}

Expand All @@ -30,17 +26,17 @@ export function DynamoDBAdapter(
options?: DynamoDBAdapterOptions
): Adapter {
const TableName = options?.tableName ?? "next-auth"
const pk = options?.partitionKey ?? 'pk'
const sk = options?.sortKey ?? 'sk'
const IndexName = options?.indexName ?? 'GSI1'
const GSI1PK = options?.indexPartitionKey ?? 'GSI1PK'
const GSI1SK = options?.indexSortKey ?? 'GSI1SK'
const pk = options?.partitionKey ?? "pk"
const sk = options?.sortKey ?? "sk"
const IndexName = options?.indexName ?? "GSI1"
const GSI1PK = options?.indexPartitionKey ?? "GSI1PK"
const GSI1SK = options?.indexSortKey ?? "GSI1SK"

return {
async createUser(data) {
const user: AdapterUser = {
...(data as any),
id: randomBytes(16).toString("hex"),
id: uuid(),
}

await client.put({
Expand All @@ -50,8 +46,8 @@ export function DynamoDBAdapter(
[pk]: `USER#${user.id}`,
[sk]: `USER#${user.id}`,
type: "USER",
[GSI1PK]: `USER#${user.email as string}`,
[GSI1SK]: `USER#${user.email as string}`,
[GSI1PK]: `USER#${user.email}`,
[GSI1SK]: `USER#${user.email}`,
}),
})

Expand Down Expand Up @@ -165,7 +161,7 @@ export function DynamoDBAdapter(
async linkAccount(data) {
const item = {
...data,
id: randomBytes(16).toString("hex"),
id: uuid(),
[pk]: `USER#${data.userId}`,
[sk]: `ACCOUNT#${data.provider}#${data.providerAccountId}`,
[GSI1PK]: `ACCOUNT#${data.provider}`,
Expand Down Expand Up @@ -229,7 +225,7 @@ export function DynamoDBAdapter(
},
async createSession(data) {
const session = {
id: randomBytes(16).toString("hex"),
id: uuid(),
...data,
}
await client.put({
Expand Down Expand Up @@ -327,3 +323,73 @@ export function DynamoDBAdapter(
},
}
}

// https://github.com/honeinc/is-iso-date/blob/master/index.js
const isoDateRE =
/(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))/
function isDate(value: any) {
return value && isoDateRE.test(value) && !isNaN(Date.parse(value))
}

const format = {
/** Takes a plain old JavaScript object and turns it into a Dynamodb object */
to(object: Record<string, any>) {
const newObject: Record<string, unknown> = {}
for (const key in object) {
const value = object[key]
if (value instanceof Date) {
// DynamoDB requires the TTL attribute be a UNIX timestamp (in secs).
if (key === "expires") newObject[key] = value.getTime() / 1000
else newObject[key] = value.toISOString()
} else newObject[key] = value
}
return newObject
},
/** Takes a Dynamo object and returns a plain old JavaScript object */
from<T = Record<string, unknown>>(object?: Record<string, any>): T | null {
if (!object) return null
const newObject: Record<string, unknown> = {}
for (const key in object) {
// Filter DynamoDB specific attributes so it doesn't get passed to core,
// to avoid revealing the type of database
if (["pk", "sk", "GSI1PK", "GSI1SK"].includes(key)) continue

const value = object[key]

if (isDate(value)) newObject[key] = new Date(value)
// hack to keep type property in account
else if (key === "type" && ["SESSION", "VT", "USER"].includes(value))
continue
// The expires property is stored as a UNIX timestamp in seconds, but
// JavaScript needs it in milliseconds, so multiply by 1000.
else if (key === "expires" && typeof value === "number")
newObject[key] = new Date(value * 1000)
else newObject[key] = value
}
return newObject as T
},
}

function generateUpdateExpression(object: Record<string, any>): {
UpdateExpression: string
ExpressionAttributeNames: Record<string, string>
ExpressionAttributeValues: Record<string, unknown>
} {
const formatedSession = format.to(object)
let UpdateExpression = "set"
const ExpressionAttributeNames: Record<string, string> = {}
const ExpressionAttributeValues: Record<string, unknown> = {}
for (const property in formatedSession) {
UpdateExpression += ` #${property} = :${property},`
ExpressionAttributeNames["#" + property] = property
ExpressionAttributeValues[":" + property] = formatedSession[property]
}
UpdateExpression = UpdateExpression.slice(0, -1)
return {
UpdateExpression,
ExpressionAttributeNames,
ExpressionAttributeValues,
}
}

export { format, generateUpdateExpression }
67 changes: 0 additions & 67 deletions packages/adapter-dynamodb/src/utils.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/adapter-dynamodb/tests/format.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { format } from "../src/utils"
import { format } from "../src/"

describe("dynamodb utils.format", () => {
it("format.to() preserves non-Date non-expires properties", () => {
Expand Down
10 changes: 9 additions & 1 deletion packages/adapter-dynamodb/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@
"extends": "@next-auth/tsconfig/tsconfig.adapters.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "dist"
"outDir": ".",
"target": "ES2020",
"module": "ESNext",
"moduleResolution": "node",
"skipDefaultLibCheck": true,
"strictNullChecks": true,
"stripInternal": true,
"declarationMap": true,
"declaration": true
},
"exclude": ["tests", "dist", "jest.config.js", "jest-dynamodb-config.js"]
}
17 changes: 12 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit 28583b8

@vercel
Copy link

@vercel vercel bot commented on 28583b8 Feb 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.