@adonisjs/session 7.7.0 → 8.0.0-next.1
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/cookie-aLBno-zS.js +31 -0
- package/build/debug-Ba-0Cgn9.js +3 -0
- package/build/dynamodb-CU8BrQfU.js +72 -0
- package/build/factories/main.d.ts +1 -1
- package/build/factories/main.js +25 -49
- package/build/factories/session_middleware_factory.d.ts +2 -2
- package/build/file-CNxCs957.js +71 -0
- package/build/index.d.ts +6 -7
- package/build/index.js +5 -33
- package/build/providers/session_provider.d.ts +2 -3
- package/build/providers/session_provider.js +27 -64
- package/build/redis-Bcjum7z7.js +36 -0
- package/build/session-CBqhcnvJ.js +209 -0
- package/build/session-Cc1LPXRc.js +107 -0
- package/build/session_middleware-CS0R7hmq.js +27 -0
- package/build/src/client.d.ts +46 -10
- package/build/src/client.js +38 -9
- package/build/src/debug.d.ts +1 -1
- package/build/src/define_config.d.ts +49 -10
- package/build/src/errors.d.ts +20 -9
- package/build/src/plugins/edge.d.ts +16 -2
- package/build/src/plugins/edge.js +74 -126
- package/build/src/plugins/japa/api_client.d.ts +22 -4
- package/build/src/plugins/japa/api_client.js +76 -97
- package/build/src/plugins/japa/browser_client.d.ts +18 -4
- package/build/src/plugins/japa/browser_client.js +58 -82
- package/build/src/session.d.ts +170 -65
- package/build/src/session_middleware.d.ts +28 -5
- package/build/src/session_middleware.js +5 -11
- package/build/src/stores/cookie.d.ts +41 -7
- package/build/src/stores/dynamodb.d.ts +44 -8
- package/build/src/stores/file.d.ts +34 -8
- package/build/src/stores/memory.d.ts +32 -15
- package/build/src/stores/redis.d.ts +39 -16
- package/build/src/types.d.ts +151 -82
- package/build/src/types.js +1 -1
- package/build/src/values_store.d.ts +113 -21
- package/build/values_store-smX0sQBJ.js +78 -0
- package/package.json +57 -56
- package/build/chunk-5ECC6OWF.js +0 -8
- package/build/chunk-5ECC6OWF.js.map +0 -1
- package/build/chunk-6BSSM3HO.js +0 -44
- package/build/chunk-6BSSM3HO.js.map +0 -1
- package/build/chunk-G7KFHA57.js +0 -59
- package/build/chunk-G7KFHA57.js.map +0 -1
- package/build/chunk-GCO66PQD.js +0 -85
- package/build/chunk-GCO66PQD.js.map +0 -1
- package/build/chunk-PZ5AY32C.js +0 -10
- package/build/chunk-PZ5AY32C.js.map +0 -1
- package/build/chunk-Q24L5XPC.js +0 -186
- package/build/chunk-Q24L5XPC.js.map +0 -1
- package/build/chunk-TE5JP3SX.js +0 -151
- package/build/chunk-TE5JP3SX.js.map +0 -1
- package/build/chunk-V3OAEXMJ.js +0 -8
- package/build/chunk-V3OAEXMJ.js.map +0 -1
- package/build/chunk-XP3CBOXR.js +0 -414
- package/build/chunk-XP3CBOXR.js.map +0 -1
- package/build/commands/commands.json +0 -1
- package/build/commands/main.d.ts +0 -4
- package/build/commands/main.js +0 -36
- package/build/commands/make_session_table.d.ts +0 -9
- package/build/commands/make_session_table.js +0 -21
- package/build/commands/make_session_table.js.map +0 -1
- package/build/cookie-3C33DMWA.js +0 -57
- package/build/cookie-3C33DMWA.js.map +0 -1
- package/build/database-443FB7MM.js +0 -118
- package/build/database-443FB7MM.js.map +0 -1
- package/build/dynamodb-53YXEFIJ.js +0 -108
- package/build/dynamodb-53YXEFIJ.js.map +0 -1
- package/build/factories/main.js.map +0 -1
- package/build/file-K3GBSVSU.js +0 -117
- package/build/file-K3GBSVSU.js.map +0 -1
- package/build/index.js.map +0 -1
- package/build/make/migration/sessions.stub +0 -26
- package/build/providers/session_provider.js.map +0 -1
- package/build/redis-YGX2CNE2.js +0 -119
- package/build/redis-YGX2CNE2.js.map +0 -1
- package/build/src/client.js.map +0 -1
- package/build/src/plugins/edge.js.map +0 -1
- package/build/src/plugins/japa/api_client.js.map +0 -1
- package/build/src/plugins/japa/browser_client.js.map +0 -1
- package/build/src/session_collection.d.ts +0 -55
- package/build/src/session_middleware.js.map +0 -1
- package/build/src/stores/database.d.ts +0 -55
- package/build/src/types.js.map +0 -1
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { t as debug_default } from "./debug-Ba-0Cgn9.js";
|
|
2
|
+
import { n as ValuesStore, t as ReadOnlyValuesStore } from "./values_store-smX0sQBJ.js";
|
|
3
|
+
import "node:module";
|
|
4
|
+
import { createError } from "@adonisjs/core/exceptions";
|
|
5
|
+
import { randomUUID } from "node:crypto";
|
|
6
|
+
import Macroable from "@poppinss/macroable";
|
|
7
|
+
import lodash from "@poppinss/utils/lodash";
|
|
8
|
+
var __defProp = Object.defineProperty;
|
|
9
|
+
var __export = (all, symbols) => {
|
|
10
|
+
let target = {};
|
|
11
|
+
for (var name in all) __defProp(target, name, {
|
|
12
|
+
get: all[name],
|
|
13
|
+
enumerable: true
|
|
14
|
+
});
|
|
15
|
+
if (symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
16
|
+
return target;
|
|
17
|
+
};
|
|
18
|
+
var errors_exports = /* @__PURE__ */ __export({
|
|
19
|
+
E_SESSION_NOT_MUTABLE: () => E_SESSION_NOT_MUTABLE,
|
|
20
|
+
E_SESSION_NOT_READY: () => E_SESSION_NOT_READY
|
|
21
|
+
});
|
|
22
|
+
const E_SESSION_NOT_MUTABLE = createError("Session store is in readonly mode and cannot be mutated", "E_SESSION_NOT_MUTABLE", 500);
|
|
23
|
+
const E_SESSION_NOT_READY = createError("Session store has not been initiated. Make sure you have registered the session middleware", "E_SESSION_NOT_READY", 500);
|
|
24
|
+
var Session = class extends Macroable {
|
|
25
|
+
#store;
|
|
26
|
+
#emitter;
|
|
27
|
+
#ctx;
|
|
28
|
+
#readonly = false;
|
|
29
|
+
#valuesStore;
|
|
30
|
+
#sessionId;
|
|
31
|
+
#sessionIdFromCookie;
|
|
32
|
+
responseFlashMessages = new ValuesStore({});
|
|
33
|
+
flashMessages = new ValuesStore({});
|
|
34
|
+
flashKey = "__flash__";
|
|
35
|
+
get sessionId() {
|
|
36
|
+
return this.#sessionId;
|
|
37
|
+
}
|
|
38
|
+
get fresh() {
|
|
39
|
+
return this.#sessionIdFromCookie === void 0;
|
|
40
|
+
}
|
|
41
|
+
get readonly() {
|
|
42
|
+
return this.#readonly;
|
|
43
|
+
}
|
|
44
|
+
get initiated() {
|
|
45
|
+
return !!this.#valuesStore;
|
|
46
|
+
}
|
|
47
|
+
get hasRegeneratedSession() {
|
|
48
|
+
return !!(this.#sessionIdFromCookie && this.#sessionIdFromCookie !== this.#sessionId);
|
|
49
|
+
}
|
|
50
|
+
get isEmpty() {
|
|
51
|
+
return this.#valuesStore?.isEmpty ?? true;
|
|
52
|
+
}
|
|
53
|
+
get hasBeenModified() {
|
|
54
|
+
return this.#valuesStore?.hasBeenModified ?? false;
|
|
55
|
+
}
|
|
56
|
+
constructor(config, storeFactory, emitter, ctx) {
|
|
57
|
+
super();
|
|
58
|
+
this.config = config;
|
|
59
|
+
this.#ctx = ctx;
|
|
60
|
+
this.#emitter = emitter;
|
|
61
|
+
this.#store = storeFactory(ctx, config);
|
|
62
|
+
this.#sessionIdFromCookie = ctx.request.cookie(config.cookieName, void 0);
|
|
63
|
+
this.#sessionId = this.#sessionIdFromCookie || randomUUID();
|
|
64
|
+
}
|
|
65
|
+
#getFlashStore(mode) {
|
|
66
|
+
if (!this.#valuesStore) throw new E_SESSION_NOT_READY();
|
|
67
|
+
if (mode === "write" && this.readonly) throw new E_SESSION_NOT_MUTABLE();
|
|
68
|
+
return this.responseFlashMessages;
|
|
69
|
+
}
|
|
70
|
+
#getValuesStore(mode) {
|
|
71
|
+
if (!this.#valuesStore) throw new E_SESSION_NOT_READY();
|
|
72
|
+
if (mode === "write" && this.readonly) throw new E_SESSION_NOT_MUTABLE();
|
|
73
|
+
return this.#valuesStore;
|
|
74
|
+
}
|
|
75
|
+
async initiate(readonly) {
|
|
76
|
+
if (this.#valuesStore) return;
|
|
77
|
+
debug_default("initiating session (readonly: %s)", readonly);
|
|
78
|
+
this.#readonly = readonly;
|
|
79
|
+
this.#valuesStore = new ValuesStore(await this.#store.read(this.#sessionId));
|
|
80
|
+
if (this.has(this.flashKey)) {
|
|
81
|
+
debug_default("reading flash data");
|
|
82
|
+
if (this.#readonly) this.flashMessages.update(this.get(this.flashKey, null));
|
|
83
|
+
else this.flashMessages.update(this.pull(this.flashKey, null));
|
|
84
|
+
}
|
|
85
|
+
if ("view" in this.#ctx) this.#ctx.view.share({
|
|
86
|
+
session: new ReadOnlyValuesStore(this.#valuesStore.all()),
|
|
87
|
+
flashMessages: new ReadOnlyValuesStore(this.flashMessages.all()),
|
|
88
|
+
old: function(key, defaultValue) {
|
|
89
|
+
return this.flashMessages.get(key, defaultValue);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
this.#emitter.emit("session:initiated", { session: this });
|
|
93
|
+
}
|
|
94
|
+
put(key, value) {
|
|
95
|
+
this.#getValuesStore("write").set(key, value);
|
|
96
|
+
}
|
|
97
|
+
has(key) {
|
|
98
|
+
return this.#getValuesStore("read").has(key);
|
|
99
|
+
}
|
|
100
|
+
get(key, defaultValue) {
|
|
101
|
+
return this.#getValuesStore("read").get(key, defaultValue);
|
|
102
|
+
}
|
|
103
|
+
all() {
|
|
104
|
+
return this.#getValuesStore("read").all();
|
|
105
|
+
}
|
|
106
|
+
forget(key) {
|
|
107
|
+
return this.#getValuesStore("write").unset(key);
|
|
108
|
+
}
|
|
109
|
+
pull(key, defaultValue) {
|
|
110
|
+
return this.#getValuesStore("write").pull(key, defaultValue);
|
|
111
|
+
}
|
|
112
|
+
increment(key, steps = 1) {
|
|
113
|
+
return this.#getValuesStore("write").increment(key, steps);
|
|
114
|
+
}
|
|
115
|
+
decrement(key, steps = 1) {
|
|
116
|
+
return this.#getValuesStore("write").decrement(key, steps);
|
|
117
|
+
}
|
|
118
|
+
clear() {
|
|
119
|
+
return this.#getValuesStore("write").clear();
|
|
120
|
+
}
|
|
121
|
+
flash(key, value) {
|
|
122
|
+
if (typeof key === "string") {
|
|
123
|
+
if (value) this.#getFlashStore("write").set(key, value);
|
|
124
|
+
} else this.#getFlashStore("write").merge(key);
|
|
125
|
+
}
|
|
126
|
+
flashErrors(errorsCollection) {
|
|
127
|
+
this.flash({ errorsBag: errorsCollection });
|
|
128
|
+
}
|
|
129
|
+
flashValidationErrors(error, withInput = true) {
|
|
130
|
+
const errorsBag = error.messages.reduce((result, message) => {
|
|
131
|
+
if (result[message.field]) result[message.field].push(message.message);
|
|
132
|
+
else result[message.field] = [message.message];
|
|
133
|
+
return result;
|
|
134
|
+
}, {});
|
|
135
|
+
if (withInput) this.flashExcept([
|
|
136
|
+
"_csrf",
|
|
137
|
+
"_method",
|
|
138
|
+
"password",
|
|
139
|
+
"password_confirmation"
|
|
140
|
+
]);
|
|
141
|
+
let summary = "The form could not be saved. Please check the errors below.";
|
|
142
|
+
if ("i18n" in this.#ctx) summary = this.#ctx.i18n.t(`errors.${error.code}`, { count: error.messages.length }, summary);
|
|
143
|
+
this.flashErrors({ [String(error.code)]: summary });
|
|
144
|
+
this.flash("inputErrorsBag", errorsBag);
|
|
145
|
+
}
|
|
146
|
+
flashAll() {
|
|
147
|
+
return this.#getFlashStore("write").set("input", this.#ctx.request.original());
|
|
148
|
+
}
|
|
149
|
+
flashExcept(keys) {
|
|
150
|
+
this.#getFlashStore("write").set("input", lodash.omit(this.#ctx.request.original(), keys));
|
|
151
|
+
}
|
|
152
|
+
flashOnly(keys) {
|
|
153
|
+
this.#getFlashStore("write").set("input", lodash.pick(this.#ctx.request.original(), keys));
|
|
154
|
+
}
|
|
155
|
+
reflash() {
|
|
156
|
+
this.#getFlashStore("write").set("reflashed", this.flashMessages.all());
|
|
157
|
+
}
|
|
158
|
+
reflashOnly(keys) {
|
|
159
|
+
this.#getFlashStore("write").set("reflashed", lodash.pick(this.flashMessages.all(), keys));
|
|
160
|
+
}
|
|
161
|
+
reflashExcept(keys) {
|
|
162
|
+
this.#getFlashStore("write").set("reflashed", lodash.omit(this.flashMessages.all(), keys));
|
|
163
|
+
}
|
|
164
|
+
regenerate() {
|
|
165
|
+
this.#sessionId = randomUUID();
|
|
166
|
+
}
|
|
167
|
+
async commit() {
|
|
168
|
+
if (!this.#valuesStore || this.readonly) return;
|
|
169
|
+
if (!this.responseFlashMessages.isEmpty) {
|
|
170
|
+
const { input, reflashed, ...others } = this.responseFlashMessages.all();
|
|
171
|
+
this.put(this.flashKey, {
|
|
172
|
+
...reflashed,
|
|
173
|
+
...input,
|
|
174
|
+
...others
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
debug_default("committing session data");
|
|
178
|
+
this.#ctx.response.cookie(this.config.cookieName, this.#sessionId, this.config.cookie);
|
|
179
|
+
if (this.isEmpty) {
|
|
180
|
+
if (this.#sessionIdFromCookie) await this.#store.destroy(this.#sessionIdFromCookie);
|
|
181
|
+
this.#emitter.emit("session:committed", { session: this });
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
if (!this.hasBeenModified) {
|
|
185
|
+
if (this.#sessionIdFromCookie && this.#sessionIdFromCookie !== this.#sessionId) {
|
|
186
|
+
await this.#store.destroy(this.#sessionIdFromCookie);
|
|
187
|
+
await this.#store.write(this.#sessionId, this.#valuesStore.toJSON());
|
|
188
|
+
this.#emitter.emit("session:migrated", {
|
|
189
|
+
fromSessionId: this.#sessionIdFromCookie,
|
|
190
|
+
toSessionId: this.sessionId,
|
|
191
|
+
session: this
|
|
192
|
+
});
|
|
193
|
+
} else await this.#store.touch(this.#sessionId);
|
|
194
|
+
this.#emitter.emit("session:committed", { session: this });
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
if (this.#sessionIdFromCookie && this.#sessionIdFromCookie !== this.#sessionId) {
|
|
198
|
+
await this.#store.destroy(this.#sessionIdFromCookie);
|
|
199
|
+
await this.#store.write(this.#sessionId, this.#valuesStore.toJSON());
|
|
200
|
+
this.#emitter.emit("session:migrated", {
|
|
201
|
+
fromSessionId: this.#sessionIdFromCookie,
|
|
202
|
+
toSessionId: this.sessionId,
|
|
203
|
+
session: this
|
|
204
|
+
});
|
|
205
|
+
} else await this.#store.write(this.#sessionId, this.#valuesStore.toJSON());
|
|
206
|
+
this.#emitter.emit("session:committed", { session: this });
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
export { errors_exports as n, Session as t };
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { t as debug_default } from "./debug-Ba-0Cgn9.js";
|
|
2
|
+
import { InvalidArgumentsException } from "@adonisjs/core/exceptions";
|
|
3
|
+
import { configProvider } from "@adonisjs/core";
|
|
4
|
+
import string from "@adonisjs/core/helpers/string";
|
|
5
|
+
const stubsRoot = import.meta.dirname;
|
|
6
|
+
async function configure(command) {
|
|
7
|
+
const codemods = await command.createCodemods();
|
|
8
|
+
await codemods.makeUsingStub(stubsRoot, "config/session.stub", {});
|
|
9
|
+
await codemods.defineEnvVariables({ SESSION_DRIVER: "cookie" });
|
|
10
|
+
await codemods.defineEnvValidations({
|
|
11
|
+
variables: { SESSION_DRIVER: `Env.schema.enum(['cookie', 'memory'] as const)` },
|
|
12
|
+
leadingComment: "Variables for configuring session package"
|
|
13
|
+
});
|
|
14
|
+
await codemods.registerMiddleware("router", [{ path: "@adonisjs/session/session_middleware" }]);
|
|
15
|
+
await codemods.updateRcFile((rcFile) => {
|
|
16
|
+
rcFile.addProvider("@adonisjs/session/session_provider");
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
var MemoryStore = class MemoryStore {
|
|
20
|
+
static sessions = /* @__PURE__ */ new Map();
|
|
21
|
+
read(sessionId) {
|
|
22
|
+
return MemoryStore.sessions.get(sessionId) || null;
|
|
23
|
+
}
|
|
24
|
+
write(sessionId, values) {
|
|
25
|
+
MemoryStore.sessions.set(sessionId, values);
|
|
26
|
+
}
|
|
27
|
+
destroy(sessionId) {
|
|
28
|
+
MemoryStore.sessions.delete(sessionId);
|
|
29
|
+
}
|
|
30
|
+
touch(_) {}
|
|
31
|
+
};
|
|
32
|
+
function defineConfig(config) {
|
|
33
|
+
debug_default("processing session config %O", config);
|
|
34
|
+
if (!config.store) throw new InvalidArgumentsException("Missing \"store\" property inside the session config");
|
|
35
|
+
const { stores: stores$1, cookie, ...rest } = {
|
|
36
|
+
enabled: true,
|
|
37
|
+
age: "2h",
|
|
38
|
+
cookieName: "adonis_session",
|
|
39
|
+
clearWithBrowser: false,
|
|
40
|
+
...config
|
|
41
|
+
};
|
|
42
|
+
const cookieOptions = { ...cookie };
|
|
43
|
+
if (!rest.clearWithBrowser) {
|
|
44
|
+
cookieOptions.maxAge = string.seconds.parse(rest.age);
|
|
45
|
+
debug_default("computing maxAge \"%s\" for session id cookie", cookieOptions.maxAge);
|
|
46
|
+
} else {
|
|
47
|
+
cookieOptions.maxAge = void 0;
|
|
48
|
+
cookieOptions.expires = void 0;
|
|
49
|
+
}
|
|
50
|
+
return configProvider.create(async (app) => {
|
|
51
|
+
const storesNames = Object.keys(config.stores);
|
|
52
|
+
const storesList = { memory: () => new MemoryStore() };
|
|
53
|
+
for (let storeName of storesNames) {
|
|
54
|
+
const store = config.stores[storeName];
|
|
55
|
+
if (typeof store === "function") storesList[storeName] = store;
|
|
56
|
+
else storesList[storeName] = await store.resolver(app);
|
|
57
|
+
}
|
|
58
|
+
const transformedConfig = {
|
|
59
|
+
...rest,
|
|
60
|
+
cookie: cookieOptions,
|
|
61
|
+
stores: storesList
|
|
62
|
+
};
|
|
63
|
+
debug_default("transformed session config %O", transformedConfig);
|
|
64
|
+
return transformedConfig;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
const stores = {
|
|
68
|
+
file: (config) => {
|
|
69
|
+
return configProvider.create(async () => {
|
|
70
|
+
const { FileStore } = await import("./file-CNxCs957.js");
|
|
71
|
+
return (_, sessionConfig) => {
|
|
72
|
+
return new FileStore(config, sessionConfig.age);
|
|
73
|
+
};
|
|
74
|
+
});
|
|
75
|
+
},
|
|
76
|
+
redis: (config) => {
|
|
77
|
+
return configProvider.create(async (app) => {
|
|
78
|
+
const { RedisStore } = await import("./redis-Bcjum7z7.js");
|
|
79
|
+
const redis = await app.container.make("redis");
|
|
80
|
+
return (_, sessionConfig) => {
|
|
81
|
+
return new RedisStore(redis.connection(config.connection), sessionConfig.age);
|
|
82
|
+
};
|
|
83
|
+
});
|
|
84
|
+
},
|
|
85
|
+
cookie: () => {
|
|
86
|
+
return configProvider.create(async () => {
|
|
87
|
+
const { CookieStore } = await import("./cookie-aLBno-zS.js");
|
|
88
|
+
return (ctx, sessionConfig) => {
|
|
89
|
+
return new CookieStore(sessionConfig.cookie, ctx);
|
|
90
|
+
};
|
|
91
|
+
});
|
|
92
|
+
},
|
|
93
|
+
dynamodb: (config) => {
|
|
94
|
+
return configProvider.create(async () => {
|
|
95
|
+
const { DynamoDBStore } = await import("./dynamodb-CU8BrQfU.js");
|
|
96
|
+
const { DynamoDBClient } = await import("@aws-sdk/client-dynamodb");
|
|
97
|
+
const client = "clientConfig" in config ? new DynamoDBClient(config.clientConfig) : config.client;
|
|
98
|
+
return (_, sessionConfig) => {
|
|
99
|
+
return new DynamoDBStore(client, sessionConfig.age, {
|
|
100
|
+
tableName: config.tableName,
|
|
101
|
+
keyAttribute: config.keyAttribute
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
export { stubsRoot as i, stores as n, configure as r, defineConfig as t };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { t as Session } from "./session-CBqhcnvJ.js";
|
|
2
|
+
import { ExceptionHandler } from "@adonisjs/core/http";
|
|
3
|
+
const originalErrorHandler = ExceptionHandler.prototype.renderValidationErrorAsHTML;
|
|
4
|
+
ExceptionHandler.macro("renderValidationErrorAsHTML", async function(error, ctx) {
|
|
5
|
+
if (ctx.session) {
|
|
6
|
+
const withInput = ctx.request.header("X-Inertia") ? false : true;
|
|
7
|
+
ctx.session.flashValidationErrors(error, withInput);
|
|
8
|
+
ctx.response.redirect("back", true);
|
|
9
|
+
} else return originalErrorHandler(error, ctx);
|
|
10
|
+
});
|
|
11
|
+
var SessionMiddleware = class {
|
|
12
|
+
#config;
|
|
13
|
+
#emitter;
|
|
14
|
+
constructor(config, emitter) {
|
|
15
|
+
this.#config = config;
|
|
16
|
+
this.#emitter = emitter;
|
|
17
|
+
}
|
|
18
|
+
async handle(ctx, next) {
|
|
19
|
+
if (!this.#config.enabled) return next();
|
|
20
|
+
ctx.session = new Session(this.#config, this.#config.stores[this.#config.store], this.#emitter, ctx);
|
|
21
|
+
await ctx.session.initiate(false);
|
|
22
|
+
const response = await next();
|
|
23
|
+
await ctx.session.commit();
|
|
24
|
+
return response;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
export { SessionMiddleware as t };
|
package/build/src/client.d.ts
CHANGED
|
@@ -1,37 +1,73 @@
|
|
|
1
|
-
import type { SessionData, SessionStoreContract } from './types.
|
|
1
|
+
import type { SessionData, SessionStoreContract } from './types.ts';
|
|
2
2
|
/**
|
|
3
|
-
* Session client exposes the API to set session data as a client
|
|
3
|
+
* Session client exposes the API to set session data as a client.
|
|
4
|
+
* Useful for testing or programmatic session manipulation.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* const client = new SessionClient(store)
|
|
8
|
+
* client.merge({ userId: 123 })
|
|
9
|
+
* client.flash({ success: 'Login successful' })
|
|
10
|
+
* await client.commit()
|
|
4
11
|
*/
|
|
5
12
|
export declare class SessionClient {
|
|
6
13
|
#private;
|
|
7
14
|
/**
|
|
8
|
-
* Session key for
|
|
15
|
+
* Session key used for storing flash messages
|
|
9
16
|
*/
|
|
10
17
|
flashKey: string;
|
|
11
18
|
/**
|
|
12
|
-
* Session to use when no explicit session id is
|
|
13
|
-
|
|
19
|
+
* Session ID to use when no explicit session id is defined
|
|
20
|
+
*/
|
|
21
|
+
sessionId: `${string}-${string}-${string}-${string}-${string}`;
|
|
22
|
+
/**
|
|
23
|
+
* Creates a new session client
|
|
24
|
+
*
|
|
25
|
+
* @param store - Session store contract implementation
|
|
14
26
|
*/
|
|
15
|
-
sessionId: string;
|
|
16
27
|
constructor(store: SessionStoreContract);
|
|
17
28
|
/**
|
|
18
|
-
*
|
|
29
|
+
* Merges session data with existing values
|
|
30
|
+
*
|
|
31
|
+
* @param values - Session data to merge
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* client.merge({ userId: 123, theme: 'dark' })
|
|
19
35
|
*/
|
|
20
36
|
merge(values: SessionData): this;
|
|
21
37
|
/**
|
|
22
|
-
*
|
|
38
|
+
* Merges flash messages with existing flash data
|
|
39
|
+
*
|
|
40
|
+
* @param values - Flash message data to merge
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* client.flash({ success: 'Operation completed', info: 'Check your email' })
|
|
23
44
|
*/
|
|
24
45
|
flash(values: SessionData): this;
|
|
25
46
|
/**
|
|
26
|
-
* Commits data to the session store
|
|
47
|
+
* Commits data to the session store
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* await client.commit() // Saves all changes to the store
|
|
27
51
|
*/
|
|
28
52
|
commit(): Promise<void>;
|
|
29
53
|
/**
|
|
30
|
-
* Destroys the session data
|
|
54
|
+
* Destroys the session data from the store
|
|
55
|
+
*
|
|
56
|
+
* @param sessionId - Optional session ID to destroy (defaults to current session)
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* await client.destroy() // Destroy current session
|
|
60
|
+
* await client.destroy('abc123') // Destroy specific session
|
|
31
61
|
*/
|
|
32
62
|
destroy(sessionId?: string): Promise<void>;
|
|
33
63
|
/**
|
|
34
64
|
* Loads session data from the session store
|
|
65
|
+
*
|
|
66
|
+
* @param sessionId - Optional session ID to load (defaults to current session)
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* const { values, flashMessages } = await client.load()
|
|
70
|
+
* const data = await client.load('abc123') // Load specific session
|
|
35
71
|
*/
|
|
36
72
|
load(sessionId?: string): Promise<{
|
|
37
73
|
values: any;
|
package/build/src/client.js
CHANGED
|
@@ -1,10 +1,39 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
} from "
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import { t as debug_default } from "../debug-Ba-0Cgn9.js";
|
|
2
|
+
import { n as ValuesStore } from "../values_store-smX0sQBJ.js";
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
4
|
+
var SessionClient = class {
|
|
5
|
+
#valuesStore = new ValuesStore({});
|
|
6
|
+
#flashMessagesStore = new ValuesStore({});
|
|
7
|
+
#store;
|
|
8
|
+
flashKey = "__flash__";
|
|
9
|
+
sessionId = randomUUID();
|
|
10
|
+
constructor(store) {
|
|
11
|
+
this.#store = store;
|
|
12
|
+
}
|
|
13
|
+
merge(values) {
|
|
14
|
+
this.#valuesStore.merge(values);
|
|
15
|
+
return this;
|
|
16
|
+
}
|
|
17
|
+
flash(values) {
|
|
18
|
+
this.#flashMessagesStore.merge(values);
|
|
19
|
+
return this;
|
|
20
|
+
}
|
|
21
|
+
async commit() {
|
|
22
|
+
if (!this.#flashMessagesStore.isEmpty) this.#valuesStore.set(this.flashKey, this.#flashMessagesStore.toJSON());
|
|
23
|
+
debug_default("committing session data during api request");
|
|
24
|
+
if (!this.#valuesStore.isEmpty) this.#store.write(this.sessionId, this.#valuesStore.toJSON());
|
|
25
|
+
}
|
|
26
|
+
async destroy(sessionId) {
|
|
27
|
+
debug_default("destroying session data during api request");
|
|
28
|
+
this.#store.destroy(sessionId || this.sessionId);
|
|
29
|
+
}
|
|
30
|
+
async load(sessionId) {
|
|
31
|
+
const store = new ValuesStore(await this.#store.read(sessionId || this.sessionId));
|
|
32
|
+
const flashMessages = store.pull(this.flashKey, {});
|
|
33
|
+
return {
|
|
34
|
+
values: store.all(),
|
|
35
|
+
flashMessages
|
|
36
|
+
};
|
|
37
|
+
}
|
|
9
38
|
};
|
|
10
|
-
|
|
39
|
+
export { SessionClient };
|
package/build/src/debug.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default: import("util").DebugLogger;
|
|
1
|
+
declare const _default: import("node:util").DebugLogger;
|
|
2
2
|
export default _default;
|
|
@@ -1,31 +1,70 @@
|
|
|
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, DynamoDBStoreConfig
|
|
3
|
+
import type { SessionConfig, FileStoreConfig, RedisStoreConfig, SessionStoreFactory, DynamoDBStoreConfig } from './types.ts';
|
|
4
|
+
type ConfigInput<KnownStores extends Record<string, SessionStoreFactory | ConfigProvider<SessionStoreFactory>>> = Partial<SessionConfig> & {
|
|
5
|
+
store: keyof KnownStores | 'memory';
|
|
6
|
+
stores: KnownStores;
|
|
7
|
+
/**
|
|
8
|
+
* Whether to clear the session cookie when the browser is closed.
|
|
9
|
+
* When true, creates a session cookie that expires on browser close.
|
|
10
|
+
* Note: Persisted session data continues to exist until it expires.
|
|
11
|
+
*/
|
|
12
|
+
clearWithBrowser?: boolean;
|
|
13
|
+
cookie?: Omit<Partial<CookieOptions>, 'maxAge' | 'expires'>;
|
|
14
|
+
};
|
|
4
15
|
/**
|
|
5
|
-
* Resolved
|
|
16
|
+
* Resolved session configuration with all stores resolved
|
|
6
17
|
*/
|
|
7
18
|
type ResolvedConfig<KnownStores extends Record<string, SessionStoreFactory>> = SessionConfig & {
|
|
8
19
|
store: keyof KnownStores;
|
|
9
20
|
stores: KnownStores;
|
|
10
|
-
cookie: Partial<CookieOptions>;
|
|
11
21
|
};
|
|
12
22
|
/**
|
|
13
|
-
*
|
|
23
|
+
* Defines and validates session configuration with store setup.
|
|
24
|
+
* Handles default values and store resolution.
|
|
25
|
+
*
|
|
26
|
+
* @param config - Session configuration with stores
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* import { defineConfig, stores } from '@adonisjs/session'
|
|
30
|
+
*
|
|
31
|
+
* export default defineConfig({
|
|
32
|
+
* enabled: true,
|
|
33
|
+
* cookieName: 'adonis_session',
|
|
34
|
+
* age: '2 hours',
|
|
35
|
+
* store: 'cookie',
|
|
36
|
+
* stores: {
|
|
37
|
+
* cookie: stores.cookie(),
|
|
38
|
+
* redis: stores.redis({ connection: 'main' })
|
|
39
|
+
* }
|
|
40
|
+
* })
|
|
14
41
|
*/
|
|
15
|
-
export declare function defineConfig<KnownStores extends Record<string, SessionStoreFactory | ConfigProvider<SessionStoreFactory>>>(config:
|
|
16
|
-
store: keyof KnownStores | 'memory';
|
|
17
|
-
stores: KnownStores;
|
|
18
|
-
}): ConfigProvider<ResolvedConfig<{
|
|
42
|
+
export declare function defineConfig<KnownStores extends Record<string, SessionStoreFactory | ConfigProvider<SessionStoreFactory>>>(config: ConfigInput<KnownStores>): ConfigProvider<ResolvedConfig<{
|
|
19
43
|
[K in keyof KnownStores]: SessionStoreFactory;
|
|
20
44
|
}>>;
|
|
21
45
|
/**
|
|
22
|
-
*
|
|
46
|
+
* Built-in session stores for different storage backends.
|
|
47
|
+
* Each store provides a different persistence mechanism for session data.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* import { stores } from '@adonisjs/session'
|
|
51
|
+
*
|
|
52
|
+
* // File store - stores on filesystem
|
|
53
|
+
* stores.file({ location: './tmp/sessions' })
|
|
54
|
+
*
|
|
55
|
+
* // Redis store - stores in Redis
|
|
56
|
+
* stores.redis({ connection: 'main' })
|
|
57
|
+
*
|
|
58
|
+
* // Cookie store - stores in encrypted cookies
|
|
59
|
+
* stores.cookie()
|
|
60
|
+
*
|
|
61
|
+
* // DynamoDB store - stores in AWS DynamoDB
|
|
62
|
+
* stores.dynamodb({ tableName: 'Sessions' })
|
|
23
63
|
*/
|
|
24
64
|
export declare const stores: {
|
|
25
65
|
file: (config: FileStoreConfig) => ConfigProvider<SessionStoreFactory>;
|
|
26
66
|
redis: (config: RedisStoreConfig) => ConfigProvider<SessionStoreFactory>;
|
|
27
67
|
cookie: () => ConfigProvider<SessionStoreFactory>;
|
|
28
68
|
dynamodb: (config: DynamoDBStoreConfig) => ConfigProvider<SessionStoreFactory>;
|
|
29
|
-
database: (config?: DatabaseStoreConfig) => ConfigProvider<SessionStoreFactory>;
|
|
30
69
|
};
|
|
31
70
|
export {};
|
package/build/src/errors.d.ts
CHANGED
|
@@ -1,13 +1,24 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Error thrown when attempting to mutate a readonly session store.
|
|
3
|
+
* This occurs when trying to write to a session that was initiated in readonly mode.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* // This will throw E_SESSION_NOT_MUTABLE
|
|
7
|
+
* await session.initiate(true) // readonly mode
|
|
8
|
+
* session.put('key', 'value') // Throws error
|
|
3
9
|
*/
|
|
4
|
-
export declare const E_SESSION_NOT_MUTABLE: new (args?: any, options?: ErrorOptions) => import("@
|
|
10
|
+
export declare const E_SESSION_NOT_MUTABLE: new (args?: any, options?: ErrorOptions) => import("@adonisjs/core/exceptions").Exception;
|
|
5
11
|
/**
|
|
6
|
-
*
|
|
12
|
+
* Error thrown when attempting to use session before it's been initiated.
|
|
13
|
+
* This usually means the session middleware hasn't been registered or called.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* // This will throw E_SESSION_NOT_READY
|
|
17
|
+
* const session = new Session(config, storeFactory, emitter, ctx)
|
|
18
|
+
* session.put('key', 'value') // Throws error - need to call initiate first
|
|
19
|
+
*
|
|
20
|
+
* // Correct usage:
|
|
21
|
+
* await session.initiate(false)
|
|
22
|
+
* session.put('key', 'value') // Works fine
|
|
7
23
|
*/
|
|
8
|
-
export declare const E_SESSION_NOT_READY: new (args?: any, options?: ErrorOptions) => import("@
|
|
9
|
-
/**
|
|
10
|
-
* Raised when trying to use tagging with a store that
|
|
11
|
-
* doesn't support tagging operations
|
|
12
|
-
*/
|
|
13
|
-
export declare const E_SESSION_TAGGING_NOT_SUPPORTED: new (args?: any, options?: ErrorOptions) => import("@poppinss/utils").Exception;
|
|
24
|
+
export declare const E_SESSION_NOT_READY: new (args?: any, options?: ErrorOptions) => import("@adonisjs/core/exceptions").Exception;
|
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
import type { PluginFn } from 'edge.js/types';
|
|
2
2
|
/**
|
|
3
|
-
* The edge plugin for AdonisJS Session adds tags to read
|
|
4
|
-
*
|
|
3
|
+
* The edge plugin for AdonisJS Session adds template tags to read flash messages.
|
|
4
|
+
* Provides @flashMessage, @inputError, @error, and \@errors tags for templates.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* // Register the plugin with Edge
|
|
9
|
+
* import { edgePluginSession } from '@adonisjs/session/plugins/edge'
|
|
10
|
+
* edge.use(edgePluginSession)
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* ```edge
|
|
14
|
+
* // Use in templates
|
|
15
|
+
* @flashMessage('success')
|
|
16
|
+
* <div class="alert alert-success">{{ $message }}</div>
|
|
17
|
+
* @end
|
|
18
|
+
* ```
|
|
5
19
|
*/
|
|
6
20
|
export declare const edgePluginSession: PluginFn<undefined>;
|