@adonisjs/session 8.0.0-next.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.js +25 -45
- package/build/file-CNxCs957.js +71 -0
- package/build/index.js +5 -25
- package/build/providers/session_provider.js +27 -48
- 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.js +38 -7
- package/build/src/debug.d.ts +1 -1
- package/build/src/define_config.d.ts +12 -5
- package/build/src/plugins/edge.d.ts +5 -1
- package/build/src/plugins/edge.js +74 -124
- package/build/src/plugins/japa/api_client.js +76 -96
- package/build/src/plugins/japa/browser_client.js +58 -81
- package/build/src/session_middleware.js +5 -9
- package/build/src/types.d.ts +1 -8
- package/build/src/types.js +1 -0
- package/build/values_store-smX0sQBJ.js +78 -0
- package/package.json +48 -39
- package/build/chunk-DFXWYDMY.js +0 -62
- package/build/chunk-HAD4PFFM.js +0 -229
- package/build/chunk-MVBWJOEG.js +0 -187
- package/build/chunk-SBOMJK4T.js +0 -14
- package/build/chunk-SHD6OX52.js +0 -488
- package/build/chunk-Y566BNUT.js +0 -113
- package/build/cookie-YBBGLCO5.js +0 -88
- package/build/dynamodb-PLZABBFD.js +0 -153
- package/build/file-CCJ5ESE2.js +0 -151
- package/build/redis-NXJWWWVB.js +0 -89
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { t as debug_default } from "./debug-Ba-0Cgn9.js";
|
|
2
|
+
var CookieStore = class {
|
|
3
|
+
#ctx;
|
|
4
|
+
#config;
|
|
5
|
+
constructor(config, ctx) {
|
|
6
|
+
this.#config = config;
|
|
7
|
+
this.#ctx = ctx;
|
|
8
|
+
debug_default("initiating cookie store %O", this.#config);
|
|
9
|
+
}
|
|
10
|
+
read(sessionId) {
|
|
11
|
+
debug_default("cookie store: reading session data %s", sessionId);
|
|
12
|
+
const cookieValue = this.#ctx.request.encryptedCookie(sessionId);
|
|
13
|
+
if (typeof cookieValue !== "object") return null;
|
|
14
|
+
return cookieValue;
|
|
15
|
+
}
|
|
16
|
+
write(sessionId, values) {
|
|
17
|
+
debug_default("cookie store: writing session data %s: %O", sessionId, values);
|
|
18
|
+
this.#ctx.response.encryptedCookie(sessionId, values, this.#config);
|
|
19
|
+
}
|
|
20
|
+
destroy(sessionId) {
|
|
21
|
+
debug_default("cookie store: destroying session data %s", sessionId);
|
|
22
|
+
if (this.#ctx.request.cookiesList()[sessionId]) this.#ctx.response.clearCookie(sessionId);
|
|
23
|
+
}
|
|
24
|
+
touch(sessionId) {
|
|
25
|
+
const value = this.read(sessionId);
|
|
26
|
+
debug_default("cookie store: touching session data %s", sessionId);
|
|
27
|
+
if (!value) return;
|
|
28
|
+
this.write(sessionId, value);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
export { CookieStore };
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { t as debug_default } from "./debug-Ba-0Cgn9.js";
|
|
2
|
+
import string from "@adonisjs/core/helpers/string";
|
|
3
|
+
import { MessageBuilder } from "@adonisjs/core/helpers";
|
|
4
|
+
import { marshall, unmarshall } from "@aws-sdk/util-dynamodb";
|
|
5
|
+
import { DeleteItemCommand, GetItemCommand, PutItemCommand, UpdateItemCommand } from "@aws-sdk/client-dynamodb";
|
|
6
|
+
var DynamoDBStore = class {
|
|
7
|
+
#client;
|
|
8
|
+
#tableName;
|
|
9
|
+
#keyAttribute;
|
|
10
|
+
#ttlSeconds;
|
|
11
|
+
#valueAttribute = "value";
|
|
12
|
+
#expiresAtAttribute = "expires_at";
|
|
13
|
+
constructor(client, age, options) {
|
|
14
|
+
this.#client = client;
|
|
15
|
+
this.#tableName = options?.tableName ?? "Session";
|
|
16
|
+
this.#keyAttribute = options?.keyAttribute ?? "key";
|
|
17
|
+
this.#ttlSeconds = string.seconds.parse(age);
|
|
18
|
+
debug_default("initiating dynamodb store");
|
|
19
|
+
}
|
|
20
|
+
async read(sessionId) {
|
|
21
|
+
debug_default("dynamodb store: reading session data %s", sessionId);
|
|
22
|
+
const command = new GetItemCommand({
|
|
23
|
+
TableName: this.#tableName,
|
|
24
|
+
Key: marshall({ [this.#keyAttribute]: sessionId })
|
|
25
|
+
});
|
|
26
|
+
const response = await this.#client.send(command);
|
|
27
|
+
if (!response.Item) return null;
|
|
28
|
+
if (!response.Item[this.#valueAttribute]) return null;
|
|
29
|
+
const item = unmarshall(response.Item);
|
|
30
|
+
const contents = item[this.#valueAttribute];
|
|
31
|
+
const expiresAt = item[this.#expiresAtAttribute];
|
|
32
|
+
if (Date.now() > expiresAt) return null;
|
|
33
|
+
try {
|
|
34
|
+
return new MessageBuilder().verify(contents, sessionId);
|
|
35
|
+
} catch {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async write(sessionId, values) {
|
|
40
|
+
debug_default("dynamodb store: writing session data %s, %O", sessionId, values);
|
|
41
|
+
const message = new MessageBuilder().build(values, void 0, sessionId);
|
|
42
|
+
const command = new PutItemCommand({
|
|
43
|
+
TableName: this.#tableName,
|
|
44
|
+
Item: marshall({
|
|
45
|
+
[this.#keyAttribute]: sessionId,
|
|
46
|
+
[this.#valueAttribute]: message,
|
|
47
|
+
[this.#expiresAtAttribute]: Date.now() + this.#ttlSeconds * 1e3
|
|
48
|
+
})
|
|
49
|
+
});
|
|
50
|
+
await this.#client.send(command);
|
|
51
|
+
}
|
|
52
|
+
async destroy(sessionId) {
|
|
53
|
+
debug_default("dynamodb store: destroying session data %s", sessionId);
|
|
54
|
+
const command = new DeleteItemCommand({
|
|
55
|
+
TableName: this.#tableName,
|
|
56
|
+
Key: marshall({ [this.#keyAttribute]: sessionId })
|
|
57
|
+
});
|
|
58
|
+
await this.#client.send(command);
|
|
59
|
+
}
|
|
60
|
+
async touch(sessionId) {
|
|
61
|
+
debug_default("dynamodb store: touching session data %s", sessionId);
|
|
62
|
+
const command = new UpdateItemCommand({
|
|
63
|
+
TableName: this.#tableName,
|
|
64
|
+
Key: marshall({ [this.#keyAttribute]: sessionId }),
|
|
65
|
+
UpdateExpression: "SET #expires_at = :expires_at",
|
|
66
|
+
ExpressionAttributeNames: { "#expires_at": this.#expiresAtAttribute },
|
|
67
|
+
ExpressionAttributeValues: marshall({ ":expires_at": Date.now() + this.#ttlSeconds * 1e3 })
|
|
68
|
+
});
|
|
69
|
+
await this.#client.send(command);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
export { DynamoDBStore };
|
package/build/factories/main.js
CHANGED
|
@@ -1,49 +1,29 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
} from "../chunk-DFXWYDMY.js";
|
|
7
|
-
import "../chunk-SHD6OX52.js";
|
|
8
|
-
import "../chunk-HAD4PFFM.js";
|
|
9
|
-
import "../chunk-SBOMJK4T.js";
|
|
10
|
-
|
|
11
|
-
// factories/session_middleware_factory.ts
|
|
1
|
+
import "../session-CBqhcnvJ.js";
|
|
2
|
+
import { t as defineConfig } from "../session-Cc1LPXRc.js";
|
|
3
|
+
import "../debug-Ba-0Cgn9.js";
|
|
4
|
+
import "../values_store-smX0sQBJ.js";
|
|
5
|
+
import { t as SessionMiddleware } from "../session_middleware-CS0R7hmq.js";
|
|
12
6
|
import { Emitter } from "@adonisjs/core/events";
|
|
13
7
|
import { AppFactory } from "@adonisjs/core/factories/app";
|
|
14
8
|
var SessionMiddlewareFactory = class {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
if (options.emitter) {
|
|
35
|
-
this.#emitter = options.emitter;
|
|
36
|
-
}
|
|
37
|
-
return this;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Creates an instance of the session middleware
|
|
41
|
-
*/
|
|
42
|
-
async create() {
|
|
43
|
-
const config = await defineConfig(this.#config).resolver(this.#getApp());
|
|
44
|
-
return new SessionMiddleware(config, this.#getEmitter());
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
export {
|
|
48
|
-
SessionMiddlewareFactory
|
|
9
|
+
#config = {
|
|
10
|
+
store: "memory",
|
|
11
|
+
stores: {}
|
|
12
|
+
};
|
|
13
|
+
#emitter;
|
|
14
|
+
#getApp() {
|
|
15
|
+
return new AppFactory().create(new URL("./", import.meta.url), () => {});
|
|
16
|
+
}
|
|
17
|
+
#getEmitter() {
|
|
18
|
+
return this.#emitter || new Emitter(this.#getApp());
|
|
19
|
+
}
|
|
20
|
+
merge(options) {
|
|
21
|
+
if (options.config) this.#config = options.config;
|
|
22
|
+
if (options.emitter) this.#emitter = options.emitter;
|
|
23
|
+
return this;
|
|
24
|
+
}
|
|
25
|
+
async create() {
|
|
26
|
+
return new SessionMiddleware(await defineConfig(this.#config).resolver(this.#getApp()), this.#getEmitter());
|
|
27
|
+
}
|
|
49
28
|
};
|
|
29
|
+
export { SessionMiddlewareFactory };
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { t as debug_default } from "./debug-Ba-0Cgn9.js";
|
|
2
|
+
import string from "@adonisjs/core/helpers/string";
|
|
3
|
+
import { MessageBuilder } from "@adonisjs/core/helpers";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
|
+
import { access, mkdir, readFile, rm, stat, utimes, writeFile } from "node:fs/promises";
|
|
6
|
+
var FileStore = class {
|
|
7
|
+
#config;
|
|
8
|
+
#age;
|
|
9
|
+
constructor(config, age) {
|
|
10
|
+
this.#config = config;
|
|
11
|
+
this.#age = age;
|
|
12
|
+
debug_default("initiating file store %O", this.#config);
|
|
13
|
+
}
|
|
14
|
+
#getFilePath(sessionId) {
|
|
15
|
+
return join(this.#config.location, `${sessionId}.txt`);
|
|
16
|
+
}
|
|
17
|
+
async #pathExists(path) {
|
|
18
|
+
try {
|
|
19
|
+
await access(path);
|
|
20
|
+
return true;
|
|
21
|
+
} catch {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
async #stats(path) {
|
|
26
|
+
try {
|
|
27
|
+
return await stat(path);
|
|
28
|
+
} catch {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async #outputFile(path, contents) {
|
|
33
|
+
const pathDirname = dirname(path);
|
|
34
|
+
if (!await this.#pathExists(pathDirname)) await mkdir(pathDirname, { recursive: true });
|
|
35
|
+
await writeFile(path, contents, "utf-8");
|
|
36
|
+
}
|
|
37
|
+
async read(sessionId) {
|
|
38
|
+
const filePath = this.#getFilePath(sessionId);
|
|
39
|
+
debug_default("file store: reading session data %", sessionId);
|
|
40
|
+
const stats = await this.#stats(filePath);
|
|
41
|
+
if (!stats) return null;
|
|
42
|
+
const sessionWillExpireAt = stats.mtimeMs + string.seconds.parse(this.#age) * 1e3;
|
|
43
|
+
if (Date.now() > sessionWillExpireAt) {
|
|
44
|
+
debug_default("file store: expired session data %s", sessionId);
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
let contents = await readFile(filePath, "utf-8");
|
|
48
|
+
contents = contents.trim();
|
|
49
|
+
if (!contents) return null;
|
|
50
|
+
try {
|
|
51
|
+
return new MessageBuilder().verify(contents, sessionId);
|
|
52
|
+
} catch {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
async write(sessionId, values) {
|
|
57
|
+
debug_default("file store: writing session data %s: %O", sessionId, values);
|
|
58
|
+
const filePath = this.#getFilePath(sessionId);
|
|
59
|
+
const message = new MessageBuilder().build(values, void 0, sessionId);
|
|
60
|
+
await this.#outputFile(filePath, message);
|
|
61
|
+
}
|
|
62
|
+
async destroy(sessionId) {
|
|
63
|
+
debug_default("file store: destroying session data %s", sessionId);
|
|
64
|
+
await rm(this.#getFilePath(sessionId), { force: true });
|
|
65
|
+
}
|
|
66
|
+
async touch(sessionId) {
|
|
67
|
+
debug_default("file store: touching session data %s", sessionId);
|
|
68
|
+
await utimes(this.#getFilePath(sessionId), /* @__PURE__ */ new Date(), /* @__PURE__ */ new Date());
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
export { FileStore };
|
package/build/index.js
CHANGED
|
@@ -1,25 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
} from "./chunk-MVBWJOEG.js";
|
|
7
|
-
import {
|
|
8
|
-
Session,
|
|
9
|
-
errors_exports
|
|
10
|
-
} from "./chunk-SHD6OX52.js";
|
|
11
|
-
import {
|
|
12
|
-
ReadOnlyValuesStore,
|
|
13
|
-
ValuesStore
|
|
14
|
-
} from "./chunk-HAD4PFFM.js";
|
|
15
|
-
import "./chunk-SBOMJK4T.js";
|
|
16
|
-
export {
|
|
17
|
-
ReadOnlyValuesStore,
|
|
18
|
-
Session,
|
|
19
|
-
ValuesStore,
|
|
20
|
-
configure,
|
|
21
|
-
defineConfig,
|
|
22
|
-
errors_exports as errors,
|
|
23
|
-
stores,
|
|
24
|
-
stubsRoot
|
|
25
|
-
};
|
|
1
|
+
import { n as errors_exports, t as Session } from "./session-CBqhcnvJ.js";
|
|
2
|
+
import { i as stubsRoot, n as stores, r as configure, t as defineConfig } from "./session-Cc1LPXRc.js";
|
|
3
|
+
import "./debug-Ba-0Cgn9.js";
|
|
4
|
+
import { n as ValuesStore, t as ReadOnlyValuesStore } from "./values_store-smX0sQBJ.js";
|
|
5
|
+
export { ReadOnlyValuesStore, Session, ValuesStore, configure, defineConfig, errors_exports as errors, stores, stubsRoot };
|
|
@@ -1,51 +1,30 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import "../
|
|
5
|
-
import "../chunk-HAD4PFFM.js";
|
|
6
|
-
import "../chunk-SBOMJK4T.js";
|
|
7
|
-
|
|
8
|
-
// providers/session_provider.ts
|
|
9
|
-
import { configProvider } from "@adonisjs/core";
|
|
1
|
+
import "../session-CBqhcnvJ.js";
|
|
2
|
+
import "../debug-Ba-0Cgn9.js";
|
|
3
|
+
import "../values_store-smX0sQBJ.js";
|
|
4
|
+
import { t as SessionMiddleware } from "../session_middleware-CS0R7hmq.js";
|
|
10
5
|
import { RuntimeException } from "@adonisjs/core/exceptions";
|
|
6
|
+
import { configProvider } from "@adonisjs/core";
|
|
11
7
|
var SessionProvider = class {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
if (!config) {
|
|
34
|
-
throw new RuntimeException(
|
|
35
|
-
'Invalid "config/session.ts" file. Make sure you are using the "defineConfig" method'
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
const emitter = await resolver.make("emitter");
|
|
39
|
-
return new SessionMiddleware(config, emitter);
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Adding edge tags (if edge is installed)
|
|
44
|
-
*/
|
|
45
|
-
async boot() {
|
|
46
|
-
await this.registerEdgePlugin();
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
export {
|
|
50
|
-
SessionProvider as default
|
|
8
|
+
constructor(app) {
|
|
9
|
+
this.app = app;
|
|
10
|
+
}
|
|
11
|
+
async registerEdgePlugin() {
|
|
12
|
+
if (this.app.usingEdgeJS) {
|
|
13
|
+
const edge = await import("edge.js");
|
|
14
|
+
const { edgePluginSession } = await import("../src/plugins/edge.js");
|
|
15
|
+
edge.default.use(edgePluginSession);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
register() {
|
|
19
|
+
this.app.container.singleton(SessionMiddleware, async (resolver) => {
|
|
20
|
+
const sessionConfigProvider = this.app.config.get("session", {});
|
|
21
|
+
const config = await configProvider.resolve(this.app, sessionConfigProvider);
|
|
22
|
+
if (!config) throw new RuntimeException("Invalid \"config/session.ts\" file. Make sure you are using the \"defineConfig\" method");
|
|
23
|
+
return new SessionMiddleware(config, await resolver.make("emitter"));
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
async boot() {
|
|
27
|
+
await this.registerEdgePlugin();
|
|
28
|
+
}
|
|
51
29
|
};
|
|
30
|
+
export { SessionProvider as default };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { t as debug_default } from "./debug-Ba-0Cgn9.js";
|
|
2
|
+
import string from "@adonisjs/core/helpers/string";
|
|
3
|
+
import { MessageBuilder } from "@adonisjs/core/helpers";
|
|
4
|
+
var RedisStore = class {
|
|
5
|
+
#connection;
|
|
6
|
+
#ttlSeconds;
|
|
7
|
+
constructor(connection, age) {
|
|
8
|
+
this.#connection = connection;
|
|
9
|
+
this.#ttlSeconds = string.seconds.parse(age);
|
|
10
|
+
debug_default("initiating redis store");
|
|
11
|
+
}
|
|
12
|
+
async read(sessionId) {
|
|
13
|
+
debug_default("redis store: reading session data %s", sessionId);
|
|
14
|
+
const contents = await this.#connection.get(sessionId);
|
|
15
|
+
if (!contents) return null;
|
|
16
|
+
try {
|
|
17
|
+
return new MessageBuilder().verify(contents, sessionId);
|
|
18
|
+
} catch {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
async write(sessionId, values) {
|
|
23
|
+
debug_default("redis store: writing session data %s, %O", sessionId, values);
|
|
24
|
+
const message = new MessageBuilder().build(values, void 0, sessionId);
|
|
25
|
+
await this.#connection.setex(sessionId, this.#ttlSeconds, message);
|
|
26
|
+
}
|
|
27
|
+
async destroy(sessionId) {
|
|
28
|
+
debug_default("redis store: destroying session data %s", sessionId);
|
|
29
|
+
await this.#connection.del(sessionId);
|
|
30
|
+
}
|
|
31
|
+
async touch(sessionId) {
|
|
32
|
+
debug_default("redis store: touching session data %s", sessionId);
|
|
33
|
+
await this.#connection.expire(sessionId, this.#ttlSeconds);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
export { RedisStore };
|
|
@@ -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 };
|