Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions benchmarks/adonisjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import { createServer } from 'node:http'
import { Logger } from '@adonisjs/logger'
import { Emitter } from '@adonisjs/events'
import { Encryption } from '@adonisjs/encryption'
import { EncryptionManager } from '@boringnode/encryption'
import { Application } from '@adonisjs/application'

import { defineConfig, Server } from '../build/index.js'
Expand All @@ -21,7 +21,12 @@ const app = new Application(new URL('./', import.meta.url), {
})
await app.init()

const encryption = new Encryption({ secret: 'averylongrandom32charslongsecret' })
const encryption = new EncryptionManager({
default: 'nova',
list: {
nova: { keys: ['averylongrandom32charslongsecret'] },
},
})

const server = new Server(
app,
Expand Down
8 changes: 4 additions & 4 deletions factories/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import { Socket } from 'node:net'
import proxyAddr from 'proxy-addr'
import { randomUUID } from 'node:crypto'
import type { Encryption } from '@adonisjs/encryption'
import type { EncryptionManager } from '@boringnode/encryption'
import { IncomingMessage, ServerResponse } from 'node:http'
import { EncryptionFactory } from '@adonisjs/encryption/factories'
import { EncryptionManagerFactory } from '@boringnode/encryption/factories'

import { Request } from '../src/request.ts'
import { QsParserFactory } from './qs_parser_factory.ts'
Expand All @@ -23,7 +23,7 @@ type FactoryParameters = {
method: string
req: IncomingMessage
res: ServerResponse
encryption: Encryption
encryption: EncryptionManager<any>
config: Partial<RequestConfig>
}

Expand Down Expand Up @@ -82,7 +82,7 @@ export class RequestFactory {
* signed URLs
*/
#createEncryption() {
return this.#parameters.encryption || new EncryptionFactory().create()
return this.#parameters.encryption || new EncryptionManagerFactory().create()
}

/**
Expand Down
8 changes: 4 additions & 4 deletions factories/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
*/

import { Socket } from 'node:net'
import type { Encryption } from '@adonisjs/encryption'
import type { EncryptionManager } from '@boringnode/encryption'
import { IncomingMessage, ServerResponse } from 'node:http'
import { EncryptionFactory } from '@adonisjs/encryption/factories'
import { EncryptionManagerFactory } from '@boringnode/encryption/factories'

import { RouterFactory } from './router.ts'
import { Response } from '../src/response.ts'
Expand All @@ -21,7 +21,7 @@ import { type ResponseConfig } from '../src/types/response.ts'
type FactoryParameters = {
req: IncomingMessage
res: ServerResponse
encryption: Encryption
encryption: EncryptionManager<any>
config: Partial<ResponseConfig>
router: Router
}
Expand Down Expand Up @@ -81,7 +81,7 @@ export class ResponseFactory {
* signed URLs
*/
#createEncryption() {
return this.#parameters.encryption || new EncryptionFactory().create()
return this.#parameters.encryption || new EncryptionManagerFactory().create()
}

/**
Expand Down
8 changes: 4 additions & 4 deletions factories/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
* file that was distributed with this source code.
*/

import type { Encryption } from '@adonisjs/encryption'
import type { EncryptionManager } from '@boringnode/encryption'
import type { Application } from '@adonisjs/application'
import { AppFactory } from '@adonisjs/application/factories'
import { EncryptionFactory } from '@adonisjs/encryption/factories'
import { EncryptionManagerFactory } from '@boringnode/encryption/factories'

import { Router } from '../src/router/main.ts'
import { QsParserFactory } from './qs_parser_factory.ts'

type FactoryParameters = {
app: Application<any>
encryption: Encryption
encryption: EncryptionManager<any>
}

/**
Expand All @@ -42,7 +42,7 @@ export class RouterFactory {
* signed URLs
*/
#createEncryption() {
return this.#parameters.encryption || new EncryptionFactory().create()
return this.#parameters.encryption || new EncryptionManagerFactory().create()
}

/**
Expand Down
8 changes: 4 additions & 4 deletions factories/server_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

import { Logger } from '@adonisjs/logger'
import { Emitter } from '@adonisjs/events'
import type { Encryption } from '@adonisjs/encryption'
import type { EncryptionManager } from '@boringnode/encryption'
import type { Application } from '@adonisjs/application'
import { AppFactory } from '@adonisjs/application/factories'
import { EncryptionFactory } from '@adonisjs/encryption/factories'
import { EncryptionManagerFactory } from '@boringnode/encryption/factories'

import { Server } from '../src/server/main.ts'
import { defineConfig } from '../src/define_config.ts'
Expand All @@ -21,7 +21,7 @@ import type { ServerConfig } from '../src/types/server.ts'
type FactoryParameters = {
app: Application<any>
logger: Logger
encryption: Encryption
encryption: EncryptionManager<any>
emitter: Emitter<any>
config: Partial<ServerConfig>
}
Expand Down Expand Up @@ -69,7 +69,7 @@ export class ServerFactory {
* signed URLs
*/
#createEncryption() {
return this.#parameters.encryption || new EncryptionFactory().create()
return this.#parameters.encryption || new EncryptionManagerFactory().create()
}

/**
Expand Down
8 changes: 4 additions & 4 deletions factories/url_builder_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* file that was distributed with this source code.
*/

import type { Encryption } from '@adonisjs/encryption'
import { EncryptionFactory } from '@adonisjs/encryption/factories'
import type { EncryptionManager } from '@boringnode/encryption'
import { EncryptionManagerFactory } from '@boringnode/encryption/factories'

import { RouterFactory } from './router.ts'
import type { Router } from '../src/router/main.ts'
Expand All @@ -18,7 +18,7 @@ import { createSignedUrlBuilder } from '../src/router/signed_url_builder.ts'

type FactoryParameters = {
router: Router
encryption: Encryption
encryption: EncryptionManager<any>
}

/**
Expand All @@ -42,7 +42,7 @@ export class URLBuilderFactory<Routes extends LookupList> {
* signed URLs
*/
#createEncryption() {
return this.#parameters.encryption || new EncryptionFactory().create()
return this.#parameters.encryption || new EncryptionManagerFactory().create()
}

/**
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@adonisjs/logger": "^7.1.0-next.0",
"@adonisjs/prettier-config": "^1.4.5",
"@adonisjs/tsconfig": "^2.0.0-next.0",
"@boringnode/encryption": "^0.1.0",
"@fastify/middie": "^9.0.3",
"@japa/assert": "^4.1.1",
"@japa/expect-type": "^2.0.3",
Expand Down
6 changes: 3 additions & 3 deletions src/cookies/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* file that was distributed with this source code.
*/

import type { Encryption } from '@adonisjs/encryption'
import type { EncryptionManager } from '@boringnode/encryption'

import * as plainCookiesDriver from './drivers/plain.ts'
import * as signedCookiesDriver from './drivers/signed.ts'
Expand All @@ -34,14 +34,14 @@ export class CookieClient {
/**
* Private encryption instance used for signing and encrypting cookies
*/
#encryption: Encryption
#encryption: EncryptionManager<any>

/**
* Create a new instance of CookieClient
*
* @param encryption - The encryption instance for cookie operations
*/
constructor(encryption: Encryption) {
constructor(encryption: EncryptionManager<any>) {
this.#encryption = encryption
}

Expand Down
10 changes: 7 additions & 3 deletions src/cookies/drivers/encrypted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* file that was distributed with this source code.
*/

import type { Encryption } from '@adonisjs/encryption'
import type { EncryptionManager } from '@boringnode/encryption'

/**
* Encrypt a value to be set as cookie
Expand All @@ -17,7 +17,7 @@ import type { Encryption } from '@adonisjs/encryption'
* @param encryption - The encryption instance
* @returns The encrypted cookie string or null if value is null/undefined
*/
export function pack(key: string, value: any, encryption: Encryption): null | string {
export function pack(key: string, value: any, encryption: EncryptionManager<any>): null | string {
if (value === undefined || value === null) {
return null
}
Expand Down Expand Up @@ -45,7 +45,11 @@ export function canUnpack(encryptedValue: string) {
* @param encryption - The encryption instance
* @returns The decrypted value or null if decryption fails
*/
export function unpack(key: string, encryptedValue: string, encryption: Encryption): null | any {
export function unpack(
key: string,
encryptedValue: string,
encryption: EncryptionManager<any>
): null | any {
const value = encryptedValue.slice(2)
if (!value) {
return null
Expand Down
15 changes: 10 additions & 5 deletions src/cookies/drivers/signed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* file that was distributed with this source code.
*/

import type { Encryption } from '@adonisjs/encryption'
import type { EncryptionManager } from '@boringnode/encryption'

/**
* Signs a value to be shared as a cookie. The signed output has a
Expand All @@ -18,11 +18,12 @@ import type { Encryption } from '@adonisjs/encryption'
* @param encryption - The encryption instance
* @returns The signed cookie string or null if value is null/undefined
*/
export function pack(key: string, value: any, encryption: Encryption): null | string {
export function pack(key: string, value: any, encryption: EncryptionManager<any>): null | string {
if (value === undefined || value === null) {
return null
}
return `s:${encryption.verifier.sign(value, undefined, key)}`
console.log(encryption)
return `s:${encryption.getMessageVerifier().sign(value, undefined, key)}`
}

/**
Expand All @@ -45,11 +46,15 @@ export function canUnpack(signedValue: string) {
* @param encryption - The encryption instance
* @returns The original value or null if verification fails
*/
export function unpack(key: string, signedValue: string, encryption: Encryption): null | any {
export function unpack(
key: string,
signedValue: string,
encryption: EncryptionManager<any>
): null | any {
const value = signedValue.slice(2)
if (!value) {
return null
}

return encryption.verifier.unsign(value, key)
return encryption.getMessageVerifier().unsign(value, key)
}
4 changes: 2 additions & 2 deletions src/cookies/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import cookie from 'cookie'
import type { Encryption } from '@adonisjs/encryption'
import type { EncryptionManager } from '@boringnode/encryption'

import { CookieClient } from './client.ts'

Expand Down Expand Up @@ -54,7 +54,7 @@ export class CookieParser {
* @param cookieHeader - The raw cookie header string from the request
* @param encryption - The encryption instance for cookie operations
*/
constructor(cookieHeader: string, encryption: Encryption) {
constructor(cookieHeader: string, encryption: EncryptionManager<any>) {
this.#client = new CookieClient(encryption)
this.#cookies = this.#parse(cookieHeader)
}
Expand Down
4 changes: 2 additions & 2 deletions src/cookies/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* file that was distributed with this source code.
*/

import type { Encryption } from '@adonisjs/encryption'
import type { EncryptionManager } from '@boringnode/encryption'

import { CookieClient } from './client.ts'
import { serializeCookie } from '../helpers.ts'
Expand All @@ -29,7 +29,7 @@ export class CookieSerializer {
*
* @param encryption - The encryption instance for cookie operations
*/
constructor(encryption: Encryption) {
constructor(encryption: EncryptionManager<any>) {
this.#client = new CookieClient(encryption)
}

Expand Down
6 changes: 3 additions & 3 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import cookie from 'cookie'
// @ts-expect-error
import matchit from '@poppinss/matchit'
import string from '@poppinss/utils/string'
import { type Encryption } from '@adonisjs/encryption'
import { type EncryptionManager } from '@boringnode/encryption'
import { parseBindingReference } from '@adonisjs/fold'

import { type Qs } from './qs.ts'
Expand Down Expand Up @@ -91,7 +91,7 @@ export function createSignedURL(
identifier: string,
tokens: MatchItRouteToken[],
searchParamsStringifier: (qs: Record<string, any>) => string,
encryption: Encryption,
encryption: EncryptionManager<any>,
params?: any[] | { [param: string]: any },
options?: SignedURLOptions
): string {
Expand All @@ -103,7 +103,7 @@ export function createSignedURL(
* on their 2 different domains, but we ignore that case for now and can consider
* it later (when someone asks for it)
*/
const signature = encryption.verifier.sign(
const signature = encryption.getMessageVerifier().sign(
createURL(identifier, tokens, searchParamsStringifier, params, {
...options,
prefixUrl: undefined,
Expand Down
8 changes: 4 additions & 4 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import proxyaddr from 'proxy-addr'
import { safeEqual } from '@poppinss/utils'
import Macroable from '@poppinss/macroable'
import lodash from '@poppinss/utils/lodash'
import type { Encryption } from '@adonisjs/encryption'
import type { EncryptionManager } from '@boringnode/encryption'
import { type ServerResponse, type IncomingMessage, type IncomingHttpHeaders } from 'node:http'

import type { Qs } from './qs.ts'
Expand Down Expand Up @@ -44,7 +44,7 @@ export class Request extends Macroable {
* Encryption module to verify signed URLs and unsign/decrypt
* cookies
*/
#encryption: Encryption
#encryption: EncryptionManager<any>

/**
* Request config
Expand Down Expand Up @@ -118,7 +118,7 @@ export class Request extends Macroable {
public request: IncomingMessage,
/** Native Node.js server response instance */
public response: ServerResponse,
encryption: Encryption,
encryption: EncryptionManager<any>,
config: RequestConfig,
qsParser: Qs
) {
Expand Down Expand Up @@ -1043,7 +1043,7 @@ export class Request extends Macroable {
/*
* Return false when signature fails
*/
const signedUrl = this.#encryption.verifier.unsign(signature, purpose)
const signedUrl = this.#encryption.getMessageVerifier().unsign(signature, purpose)
if (!signedUrl) {
return false
}
Expand Down
4 changes: 2 additions & 2 deletions src/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Macroable from '@poppinss/macroable'
import { createReadStream } from 'node:fs'
import contentDisposition from 'content-disposition'
import { safeStringify } from '@poppinss/utils/json'
import type { Encryption } from '@adonisjs/encryption'
import type { EncryptionManager } from '@boringnode/encryption'
import { RuntimeException } from '@poppinss/utils/exception'
import { type ServerResponse, type IncomingMessage, type OutgoingHttpHeaders } from 'node:http'

Expand Down Expand Up @@ -167,7 +167,7 @@ export class Response extends Macroable {
constructor(
public request: IncomingMessage,
public response: ServerResponse,
encryption: Encryption,
encryption: EncryptionManager<any>,
config: ResponseConfig,
router: Router,
qs: Qs
Expand Down
Loading