@adonisjs/session 7.4.2 → 7.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/{chunk-Y3BSCW5X.js → chunk-WUWXIKIB.js} +14 -1
- package/build/chunk-WUWXIKIB.js.map +1 -0
- package/build/dynamodb-3PG52TE3.js +107 -0
- package/build/dynamodb-3PG52TE3.js.map +1 -0
- package/build/factories/main.js +1 -1
- package/build/index.js +1 -1
- package/build/src/define_config.d.ts +2 -1
- package/build/src/stores/dynamodb.d.ts +43 -0
- package/build/src/types.d.ts +12 -0
- package/package.json +81 -65
- package/build/chunk-Y3BSCW5X.js.map +0 -1
|
@@ -122,6 +122,19 @@ var stores = {
|
|
|
122
122
|
return new CookieStore(sessionConfig.cookie, ctx);
|
|
123
123
|
};
|
|
124
124
|
});
|
|
125
|
+
},
|
|
126
|
+
dynamodb: (config) => {
|
|
127
|
+
return configProvider.create(async () => {
|
|
128
|
+
const { DynamoDBStore } = await import("./dynamodb-3PG52TE3.js");
|
|
129
|
+
const { DynamoDBClient } = await import("@aws-sdk/client-dynamodb");
|
|
130
|
+
const client = "clientConfig" in config ? new DynamoDBClient(config.clientConfig) : config.client;
|
|
131
|
+
return (_, sessionConfig) => {
|
|
132
|
+
return new DynamoDBStore(client, sessionConfig.age, {
|
|
133
|
+
tableName: config.tableName,
|
|
134
|
+
keyAttribute: config.keyAttribute
|
|
135
|
+
});
|
|
136
|
+
};
|
|
137
|
+
});
|
|
125
138
|
}
|
|
126
139
|
};
|
|
127
140
|
|
|
@@ -131,4 +144,4 @@ export {
|
|
|
131
144
|
defineConfig,
|
|
132
145
|
stores
|
|
133
146
|
};
|
|
134
|
-
//# sourceMappingURL=chunk-
|
|
147
|
+
//# sourceMappingURL=chunk-WUWXIKIB.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../stubs/main.ts","../configure.ts","../src/define_config.ts","../src/stores/memory.ts"],"sourcesContent":["/*\n * @adonisjs/session\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport { getDirname } from '@poppinss/utils'\n\nexport const stubsRoot = getDirname(import.meta.url)\n","/*\n * @adonisjs/session\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport type Configure from '@adonisjs/core/commands/configure'\nimport { stubsRoot } from './stubs/main.js'\n\n/**\n * Configures the package\n */\nexport async function configure(command: Configure) {\n const codemods = await command.createCodemods()\n\n /**\n * Publish config file\n */\n await codemods.makeUsingStub(stubsRoot, 'config/session.stub', {})\n\n /**\n * Define environment variables\n */\n await codemods.defineEnvVariables({ SESSION_DRIVER: 'cookie' })\n\n /**\n * Define environment variables validations\n */\n await codemods.defineEnvValidations({\n variables: {\n SESSION_DRIVER: `Env.schema.enum(['cookie', 'memory'] as const)`,\n },\n leadingComment: 'Variables for configuring session package',\n })\n\n /**\n * Register middleware\n */\n await codemods.registerMiddleware('router', [\n {\n path: '@adonisjs/session/session_middleware',\n },\n ])\n\n /**\n * Register provider\n */\n await codemods.updateRcFile((rcFile) => {\n rcFile.addProvider('@adonisjs/session/session_provider')\n })\n}\n","/*\n * @adonisjs/session\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\n/// <reference types=\"@adonisjs/redis/redis_provider\" />\n\nimport string from '@poppinss/utils/string'\nimport { configProvider } from '@adonisjs/core'\nimport type { ConfigProvider } from '@adonisjs/core/types'\nimport { InvalidArgumentsException } from '@poppinss/utils'\nimport type { CookieOptions } from '@adonisjs/core/types/http'\n\nimport debug from './debug.js'\nimport { MemoryStore } from './stores/memory.js'\nimport type {\n SessionConfig,\n FileStoreConfig,\n RedisStoreConfig,\n SessionStoreFactory,\n DynamoDBStoreConfig,\n} from './types.js'\n\n/**\n * Resolved config with stores\n */\ntype ResolvedConfig<KnownStores extends Record<string, SessionStoreFactory>> = SessionConfig & {\n store: keyof KnownStores\n stores: KnownStores\n cookie: Partial<CookieOptions>\n}\n\n/**\n * Helper to normalize session config\n */\nexport function defineConfig<\n KnownStores extends Record<string, SessionStoreFactory | ConfigProvider<SessionStoreFactory>>,\n>(\n config: Partial<SessionConfig> & {\n store: keyof KnownStores | 'memory'\n stores: KnownStores\n }\n): ConfigProvider<\n ResolvedConfig<{\n [K in keyof KnownStores]: SessionStoreFactory\n }>\n> {\n debug('processing session config %O', config)\n\n /**\n * Make sure a store is defined\n */\n if (!config.store) {\n throw new InvalidArgumentsException('Missing \"store\" property inside the session config')\n }\n\n /**\n * Destructuring config with the default values. We pull out\n * stores and cookie values, since we have to transform\n * them in the output value.\n */\n const { stores, cookie, ...rest } = {\n enabled: true,\n age: '2h',\n cookieName: 'adonis_session',\n clearWithBrowser: false,\n ...config,\n }\n\n const cookieOptions: Partial<CookieOptions> = { ...cookie }\n\n /**\n * Define maxAge property when session id cookie is\n * not a session cookie.\n */\n if (!rest.clearWithBrowser) {\n cookieOptions.maxAge = string.seconds.parse(rest.age)\n debug('computing maxAge \"%s\" for session id cookie', cookieOptions.maxAge)\n }\n\n return configProvider.create(async (app) => {\n const storesNames = Object.keys(config.stores)\n\n /**\n * List of stores with memory store always configured\n */\n const storesList = {\n memory: () => new MemoryStore(),\n } as Record<string, SessionStoreFactory>\n\n /**\n * Looping for stores and resolving them\n */\n for (let storeName of storesNames) {\n const store = config.stores[storeName]\n if (typeof store === 'function') {\n storesList[storeName] = store\n } else {\n storesList[storeName] = await store.resolver(app)\n }\n }\n\n const transformedConfig = {\n ...rest,\n cookie: cookieOptions,\n stores: storesList as { [K in keyof KnownStores]: SessionStoreFactory },\n }\n\n debug('transformed session config %O', transformedConfig)\n return transformedConfig\n })\n}\n\n/**\n * Inbuilt stores to store the session data.\n */\nexport const stores: {\n file: (config: FileStoreConfig) => ConfigProvider<SessionStoreFactory>\n redis: (config: RedisStoreConfig) => ConfigProvider<SessionStoreFactory>\n cookie: () => ConfigProvider<SessionStoreFactory>\n dynamodb: (config: DynamoDBStoreConfig) => ConfigProvider<SessionStoreFactory>\n} = {\n file: (config) => {\n return configProvider.create(async () => {\n const { FileStore } = await import('./stores/file.js')\n return (_, sessionConfig: SessionConfig) => {\n return new FileStore(config, sessionConfig.age)\n }\n })\n },\n redis: (config) => {\n return configProvider.create(async (app) => {\n const { RedisStore } = await import('./stores/redis.js')\n const redis = await app.container.make('redis')\n\n return (_, sessionConfig: SessionConfig) => {\n return new RedisStore(redis.connection(config.connection), sessionConfig.age)\n }\n })\n },\n cookie: () => {\n return configProvider.create(async () => {\n const { CookieStore } = await import('./stores/cookie.js')\n return (ctx, sessionConfig: SessionConfig) => {\n return new CookieStore(sessionConfig.cookie, ctx)\n }\n })\n },\n dynamodb: (config) => {\n return configProvider.create(async () => {\n const { DynamoDBStore } = await import('./stores/dynamodb.js')\n const { DynamoDBClient } = await import('@aws-sdk/client-dynamodb')\n\n const client =\n 'clientConfig' in config ? new DynamoDBClient(config.clientConfig) : config.client\n\n return (_, sessionConfig: SessionConfig) => {\n return new DynamoDBStore(client, sessionConfig.age, {\n tableName: config.tableName,\n keyAttribute: config.keyAttribute,\n })\n }\n })\n },\n}\n","/**\n * @adonisjs/session\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport type { SessionData, SessionStoreContract } from '../types.js'\n\n/**\n * Memory store is meant to be used for writing tests.\n */\nexport class MemoryStore implements SessionStoreContract {\n static sessions: Map<string, SessionData> = new Map()\n\n /**\n * Read session id value from the memory\n */\n read(sessionId: string): SessionData | null {\n return MemoryStore.sessions.get(sessionId) || null\n }\n\n /**\n * Save in memory value for a given session id\n */\n write(sessionId: string, values: SessionData): void {\n MemoryStore.sessions.set(sessionId, values)\n }\n\n /**\n * Cleanup for a single session\n */\n destroy(sessionId: string): void {\n MemoryStore.sessions.delete(sessionId)\n }\n\n touch(): void {}\n}\n"],"mappings":";;;;;AASA,SAAS,kBA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AA,IACF,CAAC;AAAA,EACH;AAAA,EACA,QAAQ,MAAM;AACZ,WAAO,eAAe,OAAO,YAAY;AACvC,YAAM,EAAE,YAAY,IAAI,MAAM,OAAO,sBAAoB;AACzD,aAAO,CAAC,KAAK,kBAAiC;AAC5C,eAAO,IAAI,YAAY,cAAc,QAAQ,GAAG;AAAA,MAClD;AAAA,IACF,CAAC;AAAA,EACH;AAAA,EACA,UAAU,CAAC,WAAW;AACpB,WAAO,eAAe,OAAO,YAAY;AACvC,YAAM,EAAE,cAAc,IAAI,MAAM,OAAO,wBAAsB;AAC7D,YAAM,EAAE,eAAe,IAAI,MAAM,OAAO,0BAA0B;AAElE,YAAM,SACJ,kBAAkB,SAAS,IAAI,eAAe,OAAO,YAAY,IAAI,OAAO;AAE9E,aAAO,CAAC,GAAG,kBAAiC;AAC1C,eAAO,IAAI,cAAc,QAAQ,cAAc,KAAK;AAAA,UAClD,WAAW,OAAO;AAAA,UAClB,cAAc,OAAO;AAAA,QACvB,CAAC;AAAA,MACH;AAAA,IACF,CAAC;AAAA,EACH;AACF;","names":["stores"]}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import {
|
|
2
|
+
debug_default
|
|
3
|
+
} from "./chunk-ZVSEMDIC.js";
|
|
4
|
+
|
|
5
|
+
// src/stores/dynamodb.ts
|
|
6
|
+
import string from "@poppinss/utils/string";
|
|
7
|
+
import { MessageBuilder } from "@adonisjs/core/helpers";
|
|
8
|
+
import { marshall, unmarshall } from "@aws-sdk/util-dynamodb";
|
|
9
|
+
import {
|
|
10
|
+
GetItemCommand,
|
|
11
|
+
PutItemCommand,
|
|
12
|
+
DeleteItemCommand,
|
|
13
|
+
UpdateItemCommand
|
|
14
|
+
} from "@aws-sdk/client-dynamodb";
|
|
15
|
+
var DynamoDBStore = class {
|
|
16
|
+
#client;
|
|
17
|
+
#tableName;
|
|
18
|
+
#keyAttribute;
|
|
19
|
+
#ttlSeconds;
|
|
20
|
+
#valueAttribute = "value";
|
|
21
|
+
#expiresAtAttribute = "expires_at";
|
|
22
|
+
constructor(client, age, options) {
|
|
23
|
+
this.#client = client;
|
|
24
|
+
this.#tableName = options?.tableName ?? "Session";
|
|
25
|
+
this.#keyAttribute = options?.keyAttribute ?? "key";
|
|
26
|
+
this.#ttlSeconds = string.seconds.parse(age);
|
|
27
|
+
debug_default("initiating dynamodb store");
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Returns session data. A new item will be created if it's
|
|
31
|
+
* missing.
|
|
32
|
+
*/
|
|
33
|
+
async read(sessionId) {
|
|
34
|
+
debug_default("dynamodb store: reading session data %s", sessionId);
|
|
35
|
+
const command = new GetItemCommand({
|
|
36
|
+
TableName: this.#tableName,
|
|
37
|
+
Key: marshall({ [this.#keyAttribute]: sessionId })
|
|
38
|
+
});
|
|
39
|
+
const response = await this.#client.send(command);
|
|
40
|
+
if (!response.Item) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
if (!response.Item[this.#valueAttribute]) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
const item = unmarshall(response.Item);
|
|
47
|
+
const contents = item[this.#valueAttribute];
|
|
48
|
+
const expiresAt = item[this.#expiresAtAttribute];
|
|
49
|
+
if (Date.now() > expiresAt) {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
try {
|
|
53
|
+
return new MessageBuilder().verify(contents, sessionId);
|
|
54
|
+
} catch {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Write session values to DynamoDB
|
|
60
|
+
*/
|
|
61
|
+
async write(sessionId, values) {
|
|
62
|
+
debug_default("dynamodb store: writing session data %s, %O", sessionId, values);
|
|
63
|
+
const message = new MessageBuilder().build(values, void 0, sessionId);
|
|
64
|
+
const command = new PutItemCommand({
|
|
65
|
+
TableName: this.#tableName,
|
|
66
|
+
Item: marshall({
|
|
67
|
+
[this.#keyAttribute]: sessionId,
|
|
68
|
+
[this.#valueAttribute]: message,
|
|
69
|
+
[this.#expiresAtAttribute]: Date.now() + this.#ttlSeconds * 1e3
|
|
70
|
+
})
|
|
71
|
+
});
|
|
72
|
+
await this.#client.send(command);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Cleanup session item by removing it
|
|
76
|
+
*/
|
|
77
|
+
async destroy(sessionId) {
|
|
78
|
+
debug_default("dynamodb store: destroying session data %s", sessionId);
|
|
79
|
+
const command = new DeleteItemCommand({
|
|
80
|
+
TableName: this.#tableName,
|
|
81
|
+
Key: marshall({ [this.#keyAttribute]: sessionId })
|
|
82
|
+
});
|
|
83
|
+
await this.#client.send(command);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Updates the value expiry
|
|
87
|
+
*/
|
|
88
|
+
async touch(sessionId) {
|
|
89
|
+
debug_default("dynamodb store: touching session data %s", sessionId);
|
|
90
|
+
const command = new UpdateItemCommand({
|
|
91
|
+
TableName: this.#tableName,
|
|
92
|
+
Key: marshall({ [this.#keyAttribute]: sessionId }),
|
|
93
|
+
UpdateExpression: "SET #expires_at = :expires_at",
|
|
94
|
+
ExpressionAttributeNames: {
|
|
95
|
+
"#expires_at": this.#expiresAtAttribute
|
|
96
|
+
},
|
|
97
|
+
ExpressionAttributeValues: marshall({
|
|
98
|
+
":expires_at": Date.now() + this.#ttlSeconds * 1e3
|
|
99
|
+
})
|
|
100
|
+
});
|
|
101
|
+
await this.#client.send(command);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
export {
|
|
105
|
+
DynamoDBStore
|
|
106
|
+
};
|
|
107
|
+
//# sourceMappingURL=dynamodb-3PG52TE3.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/stores/dynamodb.ts"],"sourcesContent":["/**\n * @adonisjs/session\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport string from '@poppinss/utils/string'\nimport { MessageBuilder } from '@adonisjs/core/helpers'\nimport { marshall, unmarshall } from '@aws-sdk/util-dynamodb'\nimport {\n DynamoDBClient,\n GetItemCommand,\n PutItemCommand,\n DeleteItemCommand,\n UpdateItemCommand,\n} from '@aws-sdk/client-dynamodb'\n\nimport debug from '../debug.js'\nimport type { SessionStoreContract, SessionData } from '../types.js'\n\n/**\n * DynamoDB store to read/write session to DynamoDB\n */\nexport class DynamoDBStore implements SessionStoreContract {\n #client: DynamoDBClient\n #tableName: string\n #keyAttribute: string\n #ttlSeconds: number\n #valueAttribute: string = 'value'\n #expiresAtAttribute: string = 'expires_at'\n\n constructor(\n client: DynamoDBClient,\n age: string | number,\n options?: {\n /**\n * Defaults to \"Session\"\n */\n tableName?: string\n\n /**\n * Defaults to \"key\"\n */\n keyAttribute?: string\n }\n ) {\n this.#client = client\n this.#tableName = options?.tableName ?? 'Session'\n this.#keyAttribute = options?.keyAttribute ?? 'key'\n this.#ttlSeconds = string.seconds.parse(age)\n debug('initiating dynamodb store')\n }\n\n /**\n * Returns session data. A new item will be created if it's\n * missing.\n */\n async read(sessionId: string): Promise<SessionData | null> {\n debug('dynamodb store: reading session data %s', sessionId)\n\n const command = new GetItemCommand({\n TableName: this.#tableName,\n Key: marshall({ [this.#keyAttribute]: sessionId }),\n })\n\n const response = await this.#client.send(command)\n if (!response.Item) {\n return null\n }\n\n if (!response.Item[this.#valueAttribute]) {\n return null\n }\n\n const item = unmarshall(response.Item)\n const contents = item[this.#valueAttribute] as string\n const expiresAt = item[this.#expiresAtAttribute] as number\n\n /**\n * Check if the item has been expired and return null (if expired)\n */\n if (Date.now() > expiresAt) {\n return null\n }\n\n /**\n * Verify contents with the session id and return them as an object. The verify\n * method can fail when the contents is not JSON.\n */\n try {\n return new MessageBuilder().verify<SessionData>(contents, sessionId)\n } catch {\n return null\n }\n }\n\n /**\n * Write session values to DynamoDB\n */\n async write(sessionId: string, values: Object): Promise<void> {\n debug('dynamodb store: writing session data %s, %O', sessionId, values)\n\n const message = new MessageBuilder().build(values, undefined, sessionId)\n const command = new PutItemCommand({\n TableName: this.#tableName,\n Item: marshall({\n [this.#keyAttribute]: sessionId,\n [this.#valueAttribute]: message,\n [this.#expiresAtAttribute]: Date.now() + this.#ttlSeconds * 1000,\n }),\n })\n\n await this.#client.send(command)\n }\n\n /**\n * Cleanup session item by removing it\n */\n async destroy(sessionId: string): Promise<void> {\n debug('dynamodb store: destroying session data %s', sessionId)\n\n const command = new DeleteItemCommand({\n TableName: this.#tableName,\n Key: marshall({ [this.#keyAttribute]: sessionId }),\n })\n\n await this.#client.send(command)\n }\n\n /**\n * Updates the value expiry\n */\n async touch(sessionId: string): Promise<void> {\n debug('dynamodb store: touching session data %s', sessionId)\n\n const command = new UpdateItemCommand({\n TableName: this.#tableName,\n Key: marshall({ [this.#keyAttribute]: sessionId }),\n UpdateExpression: 'SET #expires_at = :expires_at',\n ExpressionAttributeNames: {\n '#expires_at': this.#expiresAtAttribute,\n },\n ExpressionAttributeValues: marshall({\n ':expires_at': Date.now() + this.#ttlSeconds * 1000,\n }),\n })\n\n await this.#client.send(command)\n }\n}\n"],"mappings":";;;;;AASA,OAAO,YAAY;AACnB,SAAS,sBAAsB;AAC/B,SAAS,UAAU,kBAAkB;AACrC;AAAA,EAEE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAQA,IAAM,gBAAN,MAAoD;AAAA,EACzD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,kBAA0B;AAAA,EAC1B,sBAA8B;AAAA,EAE9B,YACE,QACA,KACA,SAWA;AACA,SAAK,UAAU;AACf,SAAK,aAAa,SAAS,aAAa;AACxC,SAAK,gBAAgB,SAAS,gBAAgB;AAC9C,SAAK,cAAc,OAAO,QAAQ,MAAM,GAAG;AAC3C,kBAAM,2BAA2B;AAAA,EACnC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,KAAK,WAAgD;AACzD,kBAAM,2CAA2C,SAAS;AAE1D,UAAM,UAAU,IA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C;AAAA,IACnD,CAAC;AAED,UAAM,KAAK,QAAQ,KAAK,OAAO;AAAA,EACjC;AAAA;AAAA;AAAA;AAAA,EAKA,MAAM,MAAM,WAAkC;AAC5C,kBAAM,4CAA4C,SAAS;AAE3D,UAAM,UAAU,IAAI,kBAAkB;AAAA,MACpC,WAAW,KAAK;AAAA,MAChB,KAAK,SAAS,EAAE,CAAC,KAAK,aAAa,GAAG,UAAU,CAAC;AAAA,MACjD,kBAAkB;AAAA,MAClB,0BAA0B;AAAA,QACxB,eAAe,KAAK;AAAA,MACtB;AAAA,MACA,2BAA2B,SAAS;AAAA,QAClC,eAAe,KAAK,IAAI,IAAI,KAAK,cAAc;AAAA,MACjD,CAAC;AAAA,IACH,CAAC;AAED,UAAM,KAAK,QAAQ,KAAK,OAAO;AAAA,EACjC;AACF;","names":[]}
|
package/build/factories/main.js
CHANGED
package/build/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ConfigProvider } from '@adonisjs/core/types';
|
|
2
2
|
import type { CookieOptions } from '@adonisjs/core/types/http';
|
|
3
|
-
import type { SessionConfig, FileStoreConfig, RedisStoreConfig, SessionStoreFactory } from './types.js';
|
|
3
|
+
import type { SessionConfig, FileStoreConfig, RedisStoreConfig, SessionStoreFactory, DynamoDBStoreConfig } from './types.js';
|
|
4
4
|
/**
|
|
5
5
|
* Resolved config with stores
|
|
6
6
|
*/
|
|
@@ -25,5 +25,6 @@ export declare const stores: {
|
|
|
25
25
|
file: (config: FileStoreConfig) => ConfigProvider<SessionStoreFactory>;
|
|
26
26
|
redis: (config: RedisStoreConfig) => ConfigProvider<SessionStoreFactory>;
|
|
27
27
|
cookie: () => ConfigProvider<SessionStoreFactory>;
|
|
28
|
+
dynamodb: (config: DynamoDBStoreConfig) => ConfigProvider<SessionStoreFactory>;
|
|
28
29
|
};
|
|
29
30
|
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @adonisjs/session
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
|
|
10
|
+
import type { SessionStoreContract, SessionData } from '../types.js';
|
|
11
|
+
/**
|
|
12
|
+
* DynamoDB store to read/write session to DynamoDB
|
|
13
|
+
*/
|
|
14
|
+
export declare class DynamoDBStore implements SessionStoreContract {
|
|
15
|
+
#private;
|
|
16
|
+
constructor(client: DynamoDBClient, age: string | number, options?: {
|
|
17
|
+
/**
|
|
18
|
+
* Defaults to "Session"
|
|
19
|
+
*/
|
|
20
|
+
tableName?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Defaults to "key"
|
|
23
|
+
*/
|
|
24
|
+
keyAttribute?: string;
|
|
25
|
+
});
|
|
26
|
+
/**
|
|
27
|
+
* Returns session data. A new item will be created if it's
|
|
28
|
+
* missing.
|
|
29
|
+
*/
|
|
30
|
+
read(sessionId: string): Promise<SessionData | null>;
|
|
31
|
+
/**
|
|
32
|
+
* Write session values to DynamoDB
|
|
33
|
+
*/
|
|
34
|
+
write(sessionId: string, values: Object): Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* Cleanup session item by removing it
|
|
37
|
+
*/
|
|
38
|
+
destroy(sessionId: string): Promise<void>;
|
|
39
|
+
/**
|
|
40
|
+
* Updates the value expiry
|
|
41
|
+
*/
|
|
42
|
+
touch(sessionId: string): Promise<void>;
|
|
43
|
+
}
|
package/build/src/types.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { HttpContext } from '@adonisjs/core/http';
|
|
2
2
|
import { RedisConnections } from '@adonisjs/redis/types';
|
|
3
3
|
import type { CookieOptions } from '@adonisjs/core/types/http';
|
|
4
|
+
import type { DynamoDBClient, DynamoDBClientConfig } from '@aws-sdk/client-dynamodb';
|
|
4
5
|
/**
|
|
5
6
|
* The values allowed by the `session.put` method
|
|
6
7
|
*/
|
|
@@ -80,6 +81,17 @@ export type FileStoreConfig = {
|
|
|
80
81
|
export type RedisStoreConfig = {
|
|
81
82
|
connection: keyof RedisConnections;
|
|
82
83
|
};
|
|
84
|
+
/**
|
|
85
|
+
* Configuration used by the dynamodb store.
|
|
86
|
+
*/
|
|
87
|
+
export type DynamoDBStoreConfig = ({
|
|
88
|
+
client: DynamoDBClient;
|
|
89
|
+
} | {
|
|
90
|
+
clientConfig: DynamoDBClientConfig;
|
|
91
|
+
}) & {
|
|
92
|
+
tableName?: string;
|
|
93
|
+
keyAttribute?: string;
|
|
94
|
+
};
|
|
83
95
|
/**
|
|
84
96
|
* Factory function to instantiate session store
|
|
85
97
|
*/
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adonisjs/session",
|
|
3
3
|
"description": "Session provider for AdonisJS",
|
|
4
|
-
"version": "7.
|
|
4
|
+
"version": "7.5.0",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=18.16.0"
|
|
7
7
|
},
|
|
@@ -27,66 +27,66 @@
|
|
|
27
27
|
"scripts": {
|
|
28
28
|
"pretest": "npm run lint",
|
|
29
29
|
"test": "cross-env NODE_DEBUG=adonisjs:session c8 npm run quick:test",
|
|
30
|
-
"
|
|
30
|
+
"lint": "eslint",
|
|
31
|
+
"format": "prettier --write .",
|
|
31
32
|
"typecheck": "tsc --noEmit",
|
|
32
33
|
"copy:templates": "copyfiles \"stubs/**/*.stub\" --up=\"1\" build",
|
|
33
|
-
"precompile": "npm run lint
|
|
34
|
+
"precompile": "npm run lint",
|
|
34
35
|
"compile": "tsup-node && tsc --emitDeclarationOnly --declaration",
|
|
35
36
|
"postcompile": "npm run copy:templates",
|
|
36
37
|
"build": "npm run compile",
|
|
37
|
-
"prepublishOnly": "npm run build",
|
|
38
|
-
"lint": "eslint . --ext=.ts",
|
|
39
|
-
"format": "prettier --write .",
|
|
40
|
-
"release": "np",
|
|
41
38
|
"version": "npm run build",
|
|
42
|
-
"
|
|
43
|
-
"
|
|
39
|
+
"prepublishOnly": "npm run build",
|
|
40
|
+
"release": "release-it",
|
|
41
|
+
"quick:test": "node --import=ts-node-maintained/register/esm --enable-source-maps bin/test.ts"
|
|
44
42
|
},
|
|
45
43
|
"devDependencies": {
|
|
46
|
-
"@adonisjs/assembler": "^7.
|
|
47
|
-
"@adonisjs/core": "^6.
|
|
48
|
-
"@adonisjs/eslint-config": "^
|
|
49
|
-
"@adonisjs/i18n": "^2.1.
|
|
50
|
-
"@adonisjs/prettier-config": "^1.
|
|
44
|
+
"@adonisjs/assembler": "^7.8.2",
|
|
45
|
+
"@adonisjs/core": "^6.14.0",
|
|
46
|
+
"@adonisjs/eslint-config": "^2.0.0-beta.7",
|
|
47
|
+
"@adonisjs/i18n": "^2.1.1",
|
|
48
|
+
"@adonisjs/prettier-config": "^1.4.0",
|
|
51
49
|
"@adonisjs/redis": "^9.1.0",
|
|
52
|
-
"@adonisjs/tsconfig": "^1.
|
|
50
|
+
"@adonisjs/tsconfig": "^1.4.0",
|
|
51
|
+
"@aws-sdk/client-dynamodb": "^3.658.0",
|
|
52
|
+
"@aws-sdk/util-dynamodb": "^3.658.0",
|
|
53
53
|
"@japa/api-client": "^2.0.3",
|
|
54
54
|
"@japa/assert": "^3.0.0",
|
|
55
55
|
"@japa/browser-client": "^2.0.3",
|
|
56
56
|
"@japa/file-system": "^2.3.0",
|
|
57
57
|
"@japa/plugin-adonisjs": "^3.0.1",
|
|
58
58
|
"@japa/runner": "^3.1.4",
|
|
59
|
-
"@japa/snapshot": "^2.0.
|
|
60
|
-
"@
|
|
61
|
-
"@
|
|
62
|
-
"@types/
|
|
59
|
+
"@japa/snapshot": "^2.0.6",
|
|
60
|
+
"@release-it/conventional-changelog": "^8.0.2",
|
|
61
|
+
"@swc/core": "^1.7.28",
|
|
62
|
+
"@types/node": "^22.7.0",
|
|
63
|
+
"@types/set-cookie-parser": "^2.4.10",
|
|
63
64
|
"@types/supertest": "^6.0.2",
|
|
64
65
|
"@vinejs/vine": "^2.1.0",
|
|
65
66
|
"c8": "^10.1.2",
|
|
66
67
|
"copyfiles": "^2.4.1",
|
|
67
68
|
"cross-env": "^7.0.3",
|
|
68
|
-
"
|
|
69
|
-
"
|
|
70
|
-
"eslint": "^8.57.0",
|
|
69
|
+
"edge.js": "^6.2.0",
|
|
70
|
+
"eslint": "^9.11.1",
|
|
71
71
|
"get-port": "^7.1.0",
|
|
72
|
-
"
|
|
73
|
-
"
|
|
74
|
-
"
|
|
75
|
-
"
|
|
76
|
-
"prettier": "^3.3.2",
|
|
77
|
-
"set-cookie-parser": "^2.6.0",
|
|
72
|
+
"playwright": "^1.47.2",
|
|
73
|
+
"prettier": "^3.3.3",
|
|
74
|
+
"release-it": "^17.6.0",
|
|
75
|
+
"set-cookie-parser": "^2.7.0",
|
|
78
76
|
"supertest": "^7.0.0",
|
|
79
|
-
"ts-node": "^10.9.
|
|
80
|
-
"tsup": "^8.
|
|
81
|
-
"typescript": "^5.
|
|
77
|
+
"ts-node-maintained": "^10.9.4",
|
|
78
|
+
"tsup": "^8.3.0",
|
|
79
|
+
"typescript": "^5.6.2"
|
|
82
80
|
},
|
|
83
81
|
"dependencies": {
|
|
84
|
-
"@poppinss/macroable": "^1.0.
|
|
85
|
-
"@poppinss/utils": "^6.
|
|
82
|
+
"@poppinss/macroable": "^1.0.3",
|
|
83
|
+
"@poppinss/utils": "^6.8.3"
|
|
86
84
|
},
|
|
87
85
|
"peerDependencies": {
|
|
88
86
|
"@adonisjs/core": "^6.6.0",
|
|
89
87
|
"@adonisjs/redis": "^8.0.1 || ^9.0.0",
|
|
88
|
+
"@aws-sdk/client-dynamodb": "^3.658.0",
|
|
89
|
+
"@aws-sdk/util-dynamodb": "^3.658.0",
|
|
90
90
|
"@japa/api-client": "^2.0.3",
|
|
91
91
|
"@japa/browser-client": "^2.0.3",
|
|
92
92
|
"edge.js": "^6.0.2"
|
|
@@ -98,6 +98,12 @@
|
|
|
98
98
|
"edge.js": {
|
|
99
99
|
"optional": true
|
|
100
100
|
},
|
|
101
|
+
"@aws-sdk/client-dynamodb": {
|
|
102
|
+
"optional": true
|
|
103
|
+
},
|
|
104
|
+
"@aws-sdk/util-dynamodb": {
|
|
105
|
+
"optional": true
|
|
106
|
+
},
|
|
101
107
|
"@japa/api-client": {
|
|
102
108
|
"optional": true
|
|
103
109
|
},
|
|
@@ -107,48 +113,21 @@
|
|
|
107
113
|
},
|
|
108
114
|
"author": "virk,adonisjs",
|
|
109
115
|
"license": "MIT",
|
|
110
|
-
"homepage": "https://github.com/adonisjs/
|
|
116
|
+
"homepage": "https://github.com/adonisjs/session#readme",
|
|
111
117
|
"repository": {
|
|
112
118
|
"type": "git",
|
|
113
|
-
"url": "git+https://github.com/adonisjs/
|
|
119
|
+
"url": "git+https://github.com/adonisjs/session.git"
|
|
114
120
|
},
|
|
115
121
|
"bugs": {
|
|
116
|
-
"url": "https://github.com/adonisjs/
|
|
122
|
+
"url": "https://github.com/adonisjs/session/issues"
|
|
117
123
|
},
|
|
118
124
|
"keywords": [
|
|
119
125
|
"session",
|
|
120
126
|
"adonisjs"
|
|
121
127
|
],
|
|
122
|
-
"eslintConfig": {
|
|
123
|
-
"extends": "@adonisjs/eslint-config/package"
|
|
124
|
-
},
|
|
125
|
-
"prettier": "@adonisjs/prettier-config",
|
|
126
|
-
"commitlint": {
|
|
127
|
-
"extends": [
|
|
128
|
-
"@commitlint/config-conventional"
|
|
129
|
-
]
|
|
130
|
-
},
|
|
131
128
|
"publishConfig": {
|
|
132
129
|
"access": "public",
|
|
133
|
-
"
|
|
134
|
-
},
|
|
135
|
-
"np": {
|
|
136
|
-
"message": "chore(release): %s",
|
|
137
|
-
"tag": "latest",
|
|
138
|
-
"branch": "main",
|
|
139
|
-
"anyBranch": false
|
|
140
|
-
},
|
|
141
|
-
"c8": {
|
|
142
|
-
"reporter": [
|
|
143
|
-
"text",
|
|
144
|
-
"html"
|
|
145
|
-
],
|
|
146
|
-
"exclude": [
|
|
147
|
-
"tests/**",
|
|
148
|
-
"stubs/**",
|
|
149
|
-
"factories/**",
|
|
150
|
-
"bin/**"
|
|
151
|
-
]
|
|
130
|
+
"provenance": true
|
|
152
131
|
},
|
|
153
132
|
"tsup": {
|
|
154
133
|
"entry": [
|
|
@@ -168,5 +147,42 @@
|
|
|
168
147
|
"dts": false,
|
|
169
148
|
"sourcemap": true,
|
|
170
149
|
"target": "esnext"
|
|
171
|
-
}
|
|
150
|
+
},
|
|
151
|
+
"release-it": {
|
|
152
|
+
"git": {
|
|
153
|
+
"requireCleanWorkingDir": true,
|
|
154
|
+
"requireUpstream": true,
|
|
155
|
+
"commitMessage": "chore(release): ${version}",
|
|
156
|
+
"tagAnnotation": "v${version}",
|
|
157
|
+
"push": true,
|
|
158
|
+
"tagName": "v${version}"
|
|
159
|
+
},
|
|
160
|
+
"github": {
|
|
161
|
+
"release": true
|
|
162
|
+
},
|
|
163
|
+
"npm": {
|
|
164
|
+
"publish": true,
|
|
165
|
+
"skipChecks": true
|
|
166
|
+
},
|
|
167
|
+
"plugins": {
|
|
168
|
+
"@release-it/conventional-changelog": {
|
|
169
|
+
"preset": {
|
|
170
|
+
"name": "angular"
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
},
|
|
175
|
+
"c8": {
|
|
176
|
+
"reporter": [
|
|
177
|
+
"text",
|
|
178
|
+
"html"
|
|
179
|
+
],
|
|
180
|
+
"exclude": [
|
|
181
|
+
"tests/**",
|
|
182
|
+
"stubs/**",
|
|
183
|
+
"factories/**",
|
|
184
|
+
"bin/**"
|
|
185
|
+
]
|
|
186
|
+
},
|
|
187
|
+
"prettier": "@adonisjs/prettier-config"
|
|
172
188
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../stubs/main.ts","../configure.ts","../src/define_config.ts","../src/stores/memory.ts"],"sourcesContent":["/*\n * @adonisjs/session\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport { getDirname } from '@poppinss/utils'\n\nexport const stubsRoot = getDirname(import.meta.url)\n","/*\n * @adonisjs/session\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport type Configure from '@adonisjs/core/commands/configure'\nimport { stubsRoot } from './stubs/main.js'\n\n/**\n * Configures the package\n */\nexport async function configure(command: Configure) {\n const codemods = await command.createCodemods()\n\n /**\n * Publish config file\n */\n await codemods.makeUsingStub(stubsRoot, 'config/session.stub', {})\n\n /**\n * Define environment variables\n */\n await codemods.defineEnvVariables({ SESSION_DRIVER: 'cookie' })\n\n /**\n * Define environment variables validations\n */\n await codemods.defineEnvValidations({\n variables: {\n SESSION_DRIVER: `Env.schema.enum(['cookie', 'memory'] as const)`,\n },\n leadingComment: 'Variables for configuring session package',\n })\n\n /**\n * Register middleware\n */\n await codemods.registerMiddleware('router', [\n {\n path: '@adonisjs/session/session_middleware',\n },\n ])\n\n /**\n * Register provider\n */\n await codemods.updateRcFile((rcFile) => {\n rcFile.addProvider('@adonisjs/session/session_provider')\n })\n}\n","/*\n * @adonisjs/session\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\n/// <reference types=\"@adonisjs/redis/redis_provider\" />\n\nimport string from '@poppinss/utils/string'\nimport { configProvider } from '@adonisjs/core'\nimport type { ConfigProvider } from '@adonisjs/core/types'\nimport { InvalidArgumentsException } from '@poppinss/utils'\nimport type { CookieOptions } from '@adonisjs/core/types/http'\n\nimport debug from './debug.js'\nimport { MemoryStore } from './stores/memory.js'\nimport type {\n SessionConfig,\n FileStoreConfig,\n RedisStoreConfig,\n SessionStoreFactory,\n} from './types.js'\n\n/**\n * Resolved config with stores\n */\ntype ResolvedConfig<KnownStores extends Record<string, SessionStoreFactory>> = SessionConfig & {\n store: keyof KnownStores\n stores: KnownStores\n cookie: Partial<CookieOptions>\n}\n\n/**\n * Helper to normalize session config\n */\nexport function defineConfig<\n KnownStores extends Record<string, SessionStoreFactory | ConfigProvider<SessionStoreFactory>>,\n>(\n config: Partial<SessionConfig> & {\n store: keyof KnownStores | 'memory'\n stores: KnownStores\n }\n): ConfigProvider<\n ResolvedConfig<{\n [K in keyof KnownStores]: SessionStoreFactory\n }>\n> {\n debug('processing session config %O', config)\n\n /**\n * Make sure a store is defined\n */\n if (!config.store) {\n throw new InvalidArgumentsException('Missing \"store\" property inside the session config')\n }\n\n /**\n * Destructuring config with the default values. We pull out\n * stores and cookie values, since we have to transform\n * them in the output value.\n */\n const { stores, cookie, ...rest } = {\n enabled: true,\n age: '2h',\n cookieName: 'adonis_session',\n clearWithBrowser: false,\n ...config,\n }\n\n const cookieOptions: Partial<CookieOptions> = { ...cookie }\n\n /**\n * Define maxAge property when session id cookie is\n * not a session cookie.\n */\n if (!rest.clearWithBrowser) {\n cookieOptions.maxAge = string.seconds.parse(rest.age)\n debug('computing maxAge \"%s\" for session id cookie', cookieOptions.maxAge)\n }\n\n return configProvider.create(async (app) => {\n const storesNames = Object.keys(config.stores)\n\n /**\n * List of stores with memory store always configured\n */\n const storesList = {\n memory: () => new MemoryStore(),\n } as Record<string, SessionStoreFactory>\n\n /**\n * Looping for stores and resolving them\n */\n for (let storeName of storesNames) {\n const store = config.stores[storeName]\n if (typeof store === 'function') {\n storesList[storeName] = store\n } else {\n storesList[storeName] = await store.resolver(app)\n }\n }\n\n const transformedConfig = {\n ...rest,\n cookie: cookieOptions,\n stores: storesList as { [K in keyof KnownStores]: SessionStoreFactory },\n }\n\n debug('transformed session config %O', transformedConfig)\n return transformedConfig\n })\n}\n\n/**\n * Inbuilt stores to store the session data.\n */\nexport const stores: {\n file: (config: FileStoreConfig) => ConfigProvider<SessionStoreFactory>\n redis: (config: RedisStoreConfig) => ConfigProvider<SessionStoreFactory>\n cookie: () => ConfigProvider<SessionStoreFactory>\n} = {\n file: (config) => {\n return configProvider.create(async () => {\n const { FileStore } = await import('./stores/file.js')\n return (_, sessionConfig: SessionConfig) => {\n return new FileStore(config, sessionConfig.age)\n }\n })\n },\n redis: (config) => {\n return configProvider.create(async (app) => {\n const { RedisStore } = await import('./stores/redis.js')\n const redis = await app.container.make('redis')\n\n return (_, sessionConfig: SessionConfig) => {\n return new RedisStore(redis.connection(config.connection), sessionConfig.age)\n }\n })\n },\n cookie: () => {\n return configProvider.create(async () => {\n const { CookieStore } = await import('./stores/cookie.js')\n return (ctx, sessionConfig: SessionConfig) => {\n return new CookieStore(sessionConfig.cookie, ctx)\n }\n })\n },\n}\n","/**\n * @adonisjs/session\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport type { SessionData, SessionStoreContract } from '../types.js'\n\n/**\n * Memory store is meant to be used for writing tests.\n */\nexport class MemoryStore implements SessionStoreContract {\n static sessions: Map<string, SessionData> = new Map()\n\n /**\n * Read session id value from the memory\n */\n read(sessionId: string): SessionData | null {\n return MemoryStore.sessions.get(sessionId) || null\n }\n\n /**\n * Save in memory value for a given session id\n */\n write(sessionId: string, values: SessionData): void {\n MemoryStore.sessions.set(sessionId, values)\n }\n\n /**\n * Cleanup for a single session\n */\n destroy(sessionId: string): void {\n MemoryStore.sessions.delete(sessionId)\n }\n\n touch(): void {}\n}\n"],"mappings":";;;;;AASA,SAAS,kBAAkB;AAEpB,IAAM,YAAY,WAAW,YAAY,GAAG;;;ACInD,eAAsB,UAAU,SAAoB;AAClD,QAAM,WAAW,MAAM,QAAQ,eAAe;AAK9C,QAAM,SAAS,cAAc,WAAW,uBAAuB,CAAC,CAAC;AAKjE,QAAM,SAAS,mBAAmB,EAAE,gBAAgB,SAAS,CAAC;AAK9D,QAAM,SAAS,qBAAqB;AAAA,IAClC,WAAW;AAAA,MACT,gBAAgB;AAAA,IAClB;AAAA,IACA,gBAAgB;AAAA,EAClB,CAAC;AAKD,QAAM,SAAS,mBAAmB,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