adding new version and remove typescript

main
HerrHase 2 weeks ago
parent 4006b650f9
commit 8b173841c0

@ -1,6 +1,6 @@
# Nano Session # Nano Session
Functions and Classes for handle session with [@nano/sqlite](https://git.node001.net/nano/sqlite) in [Bun](https://bun.sh/). Functions and Classes for handle session with [@nano/sqlite](https://git.node001.net/nano/sqlite).
## API ## API

Binary file not shown.

@ -1,2 +0,0 @@
[install.scopes]
"@nano" = "https://git.node001.net/api/packages/nano/npm/"

@ -0,0 +1,7 @@
import Session from './src/session.js'
import SessionStore from './src/sessionStore.js'
export {
Session,
SessionStore
}

@ -1,7 +0,0 @@
import Session from './src/session.ts'
import SessionStore from './src/sessionStore.ts'
export {
Session,
SessionStore
}

496
package-lock.json generated

@ -0,0 +1,496 @@
{
"name": "@nano/session",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@nano/session",
"version": "1.0.0",
"license": "MIT",
"dependencies": {
"@nano/sqlite": "^1.0.1",
"dayjs": "^1.11.13"
},
"devDependencies": {
"dotenv": "^16.4.5"
}
},
"node_modules/@nano/sqlite": {
"version": "1.0.1",
"resolved": "https://git.node001.net/api/packages/nano/npm/%40nano%2Fsqlite/-/1.0.1/sqlite-1.0.1.tgz",
"integrity": "sha512-3ALr1A6BYHcUR/PBb14DAat+Sg5vPmmeoHayt5g124+uH+KHZ+U2qt2nhjo3VWsAMdWH3xA/GjoUu0DozXUuHg==",
"license": "MIT",
"dependencies": {
"better-sqlite3": "12.6.*"
}
},
"node_modules/base64-js": {
"version": "1.5.1",
"resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz",
"integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"license": "MIT"
},
"node_modules/better-sqlite3": {
"version": "12.6.2",
"resolved": "https://registry.npmjs.org/better-sqlite3/-/better-sqlite3-12.6.2.tgz",
"integrity": "sha512-8VYKM3MjCa9WcaSAI3hzwhmyHVlH8tiGFwf0RlTsZPWJ1I5MkzjiudCo4KC4DxOaL/53A5B1sI/IbldNFDbsKA==",
"hasInstallScript": true,
"license": "MIT",
"dependencies": {
"bindings": "^1.5.0",
"prebuild-install": "^7.1.1"
},
"engines": {
"node": "20.x || 22.x || 23.x || 24.x || 25.x"
}
},
"node_modules/bindings": {
"version": "1.5.0",
"resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz",
"integrity": "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==",
"license": "MIT",
"dependencies": {
"file-uri-to-path": "1.0.0"
}
},
"node_modules/bl": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz",
"integrity": "sha512-1W07cM9gS6DcLperZfFSj+bWLtaPGSOHWhPiGzXmvVJbRLdG82sH/Kn8EtW1VqWVA54AKf2h5k5BbnIbwF3h6w==",
"license": "MIT",
"dependencies": {
"buffer": "^5.5.0",
"inherits": "^2.0.4",
"readable-stream": "^3.4.0"
}
},
"node_modules/buffer": {
"version": "5.7.1",
"resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.1.tgz",
"integrity": "sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"license": "MIT",
"dependencies": {
"base64-js": "^1.3.1",
"ieee754": "^1.1.13"
}
},
"node_modules/chownr": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/chownr/-/chownr-1.1.4.tgz",
"integrity": "sha512-jJ0bqzaylmJtVnNgzTeSOs8DPavpbYgEr/b0YL8/2GO3xJEhInFmhKMUnEJQjZumK7KXGFhUy89PrsJWlakBVg==",
"license": "ISC"
},
"node_modules/dayjs": {
"version": "1.11.13",
"license": "MIT"
},
"node_modules/decompress-response": {
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/decompress-response/-/decompress-response-6.0.0.tgz",
"integrity": "sha512-aW35yZM6Bb/4oJlZncMH2LCoZtJXTRxES17vE3hoRiowU2kWHaJKFkSBDnDR+cm9J+9QhXmREyIfv0pji9ejCQ==",
"license": "MIT",
"dependencies": {
"mimic-response": "^3.1.0"
},
"engines": {
"node": ">=10"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/deep-extend": {
"version": "0.6.0",
"resolved": "https://registry.npmjs.org/deep-extend/-/deep-extend-0.6.0.tgz",
"integrity": "sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA==",
"license": "MIT",
"engines": {
"node": ">=4.0.0"
}
},
"node_modules/detect-libc": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.1.2.tgz",
"integrity": "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ==",
"license": "Apache-2.0",
"engines": {
"node": ">=8"
}
},
"node_modules/dotenv": {
"version": "16.4.5",
"dev": true,
"license": "BSD-2-Clause",
"engines": {
"node": ">=12"
},
"funding": {
"url": "https://dotenvx.com"
}
},
"node_modules/end-of-stream": {
"version": "1.4.5",
"resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.5.tgz",
"integrity": "sha512-ooEGc6HP26xXq/N+GCGOT0JKCLDGrq2bQUZrQ7gyrJiZANJ/8YDTxTpQBXGMn+WbIQXNVpyWymm7KYVICQnyOg==",
"license": "MIT",
"dependencies": {
"once": "^1.4.0"
}
},
"node_modules/expand-template": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/expand-template/-/expand-template-2.0.3.tgz",
"integrity": "sha512-XYfuKMvj4O35f/pOXLObndIRvyQ+/+6AhODh+OKWj9S9498pHHn/IMszH+gt0fBCRWMNfk1ZSp5x3AifmnI2vg==",
"license": "(MIT OR WTFPL)",
"engines": {
"node": ">=6"
}
},
"node_modules/file-uri-to-path": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz",
"integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==",
"license": "MIT"
},
"node_modules/fs-constants": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/fs-constants/-/fs-constants-1.0.0.tgz",
"integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==",
"license": "MIT"
},
"node_modules/github-from-package": {
"version": "0.0.0",
"resolved": "https://registry.npmjs.org/github-from-package/-/github-from-package-0.0.0.tgz",
"integrity": "sha512-SyHy3T1v2NUXn29OsWdxmK6RwHD+vkj3v8en8AOBZ1wBQ/hCAQ5bAQTD02kW4W9tUp/3Qh6J8r9EvntiyCmOOw==",
"license": "MIT"
},
"node_modules/ieee754": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz",
"integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"license": "BSD-3-Clause"
},
"node_modules/inherits": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==",
"license": "ISC"
},
"node_modules/ini": {
"version": "1.3.8",
"resolved": "https://registry.npmjs.org/ini/-/ini-1.3.8.tgz",
"integrity": "sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==",
"license": "ISC"
},
"node_modules/mimic-response": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/mimic-response/-/mimic-response-3.1.0.tgz",
"integrity": "sha512-z0yWI+4FDrrweS8Zmt4Ej5HdJmky15+L2e6Wgn3+iK5fWzb6T3fhNFq2+MeTRb064c6Wr4N/wv0DzQTjNzHNGQ==",
"license": "MIT",
"engines": {
"node": ">=10"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/minimist": {
"version": "1.2.8",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz",
"integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==",
"license": "MIT",
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/mkdirp-classic": {
"version": "0.5.3",
"resolved": "https://registry.npmjs.org/mkdirp-classic/-/mkdirp-classic-0.5.3.tgz",
"integrity": "sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A==",
"license": "MIT"
},
"node_modules/napi-build-utils": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/napi-build-utils/-/napi-build-utils-2.0.0.tgz",
"integrity": "sha512-GEbrYkbfF7MoNaoh2iGG84Mnf/WZfB0GdGEsM8wz7Expx/LlWf5U8t9nvJKXSp3qr5IsEbK04cBGhol/KwOsWA==",
"license": "MIT"
},
"node_modules/node-abi": {
"version": "3.94.0",
"resolved": "https://registry.npmjs.org/node-abi/-/node-abi-3.94.0.tgz",
"integrity": "sha512-W5ZNO5KRPB5TkYmGVD9F6YqhsglXJzE6etpbmT+f6EQElhiX/UTG551cnsRGvLG3fyZEg9HwaDmNmj5nwJ4z9g==",
"license": "MIT",
"dependencies": {
"semver": "^7.3.5"
},
"engines": {
"node": ">=10"
}
},
"node_modules/once": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
"integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==",
"license": "ISC",
"dependencies": {
"wrappy": "1"
}
},
"node_modules/prebuild-install": {
"version": "7.1.3",
"resolved": "https://registry.npmjs.org/prebuild-install/-/prebuild-install-7.1.3.tgz",
"integrity": "sha512-8Mf2cbV7x1cXPUILADGI3wuhfqWvtiLA1iclTDbFRZkgRQS0NqsPZphna9V+HyTEadheuPmjaJMsbzKQFOzLug==",
"deprecated": "No longer maintained. Please contact the author of the relevant native addon; alternatives are available.",
"license": "MIT",
"dependencies": {
"detect-libc": "^2.0.0",
"expand-template": "^2.0.3",
"github-from-package": "0.0.0",
"minimist": "^1.2.3",
"mkdirp-classic": "^0.5.3",
"napi-build-utils": "^2.0.0",
"node-abi": "^3.3.0",
"pump": "^3.0.0",
"rc": "^1.2.7",
"simple-get": "^4.0.0",
"tar-fs": "^2.0.0",
"tunnel-agent": "^0.6.0"
},
"bin": {
"prebuild-install": "bin.js"
},
"engines": {
"node": ">=10"
}
},
"node_modules/pump": {
"version": "3.0.4",
"resolved": "https://registry.npmjs.org/pump/-/pump-3.0.4.tgz",
"integrity": "sha512-VS7sjc6KR7e1ukRFhQSY5LM2uBWAUPiOPa/A3mkKmiMwSmRFUITt0xuj+/lesgnCv+dPIEYlkzrcyXgquIHMcA==",
"license": "MIT",
"dependencies": {
"end-of-stream": "^1.1.0",
"once": "^1.3.1"
}
},
"node_modules/rc": {
"version": "1.2.8",
"resolved": "https://registry.npmjs.org/rc/-/rc-1.2.8.tgz",
"integrity": "sha512-y3bGgqKj3QBdxLbLkomlohkvsA8gdAiUQlSBJnBhfn+BPxg4bc62d8TcBW15wavDfgexCgccckhcZvywyQYPOw==",
"license": "(BSD-2-Clause OR MIT OR Apache-2.0)",
"dependencies": {
"deep-extend": "^0.6.0",
"ini": "~1.3.0",
"minimist": "^1.2.0",
"strip-json-comments": "~2.0.1"
},
"bin": {
"rc": "cli.js"
}
},
"node_modules/readable-stream": {
"version": "3.6.2",
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz",
"integrity": "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==",
"license": "MIT",
"dependencies": {
"inherits": "^2.0.3",
"string_decoder": "^1.1.1",
"util-deprecate": "^1.0.1"
},
"engines": {
"node": ">= 6"
}
},
"node_modules/safe-buffer": {
"version": "5.2.1",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz",
"integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"license": "MIT"
},
"node_modules/semver": {
"version": "7.8.5",
"resolved": "https://registry.npmjs.org/semver/-/semver-7.8.5.tgz",
"integrity": "sha512-Y7/KDsb8LjooZpwaqGyulO6DQlksgCncchHGk+sZIY4SBvUocMBEFH5Ur1fI4dV+Jvl0w6cjvucaIi40puRioA==",
"license": "ISC",
"bin": {
"semver": "bin/semver.js"
},
"engines": {
"node": ">=10"
}
},
"node_modules/simple-concat": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/simple-concat/-/simple-concat-1.0.1.tgz",
"integrity": "sha512-cSFtAPtRhljv69IK0hTVZQ+OfE9nePi/rtJmw5UjHeVyVroEqJXP1sFztKUy1qU+xvz3u/sfYJLa947b7nAN2Q==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"license": "MIT"
},
"node_modules/simple-get": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/simple-get/-/simple-get-4.0.1.tgz",
"integrity": "sha512-brv7p5WgH0jmQJr1ZDDfKDOSeWWg+OVypG99A/5vYGPqJ6pxiaHLy8nxtFjBA7oMa01ebA9gfh1uMCFqOuXxvA==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"license": "MIT",
"dependencies": {
"decompress-response": "^6.0.0",
"once": "^1.3.1",
"simple-concat": "^1.0.0"
}
},
"node_modules/string_decoder": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz",
"integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==",
"license": "MIT",
"dependencies": {
"safe-buffer": "~5.2.0"
}
},
"node_modules/strip-json-comments": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-2.0.1.tgz",
"integrity": "sha512-4gB8na07fecVVkOI6Rs4e7T6NOTki5EmL7TUduTs6bu3EdnSycntVJ4re8kgZA+wx9IueI2Y11bfbgwtzuE0KQ==",
"license": "MIT",
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/tar-fs": {
"version": "2.1.5",
"resolved": "https://registry.npmjs.org/tar-fs/-/tar-fs-2.1.5.tgz",
"integrity": "sha512-OboTd8mmMhZDNPV+UjQcK9yKAatXu2aJ+r1w4im1Otd4M4fl2hwvdoXUxIYHFTHWK/3y3FarBP70v3vwmGlOxw==",
"license": "MIT",
"dependencies": {
"chownr": "^1.1.1",
"mkdirp-classic": "^0.5.2",
"pump": "^3.0.0",
"tar-stream": "^2.1.4"
}
},
"node_modules/tar-stream": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/tar-stream/-/tar-stream-2.2.0.tgz",
"integrity": "sha512-ujeqbceABgwMZxEJnk2HDY2DlnUZ+9oEcb1KzTVfYHio0UE6dG71n60d8D2I4qNvleWrrXpmjpt7vZeF1LnMZQ==",
"license": "MIT",
"dependencies": {
"bl": "^4.0.3",
"end-of-stream": "^1.4.1",
"fs-constants": "^1.0.0",
"inherits": "^2.0.3",
"readable-stream": "^3.1.1"
},
"engines": {
"node": ">=6"
}
},
"node_modules/tunnel-agent": {
"version": "0.6.0",
"resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz",
"integrity": "sha512-McnNiV1l8RYeY8tBgEpuodCC1mLUdbSN+CYBL7kJsJNInOP8UjDDEwdk6Mw60vdLLrr5NHKZhMAOSrR2NZuQ+w==",
"license": "Apache-2.0",
"dependencies": {
"safe-buffer": "^5.0.1"
},
"engines": {
"node": "*"
}
},
"node_modules/util-deprecate": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
"integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==",
"license": "MIT"
},
"node_modules/wrappy": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
"integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==",
"license": "ISC"
}
}
}

@ -1,19 +1,20 @@
{ {
"name": "@nano/session", "name": "@nano/session",
"version": "0.1.1", "version": "1.0.0",
"description": "Handle User Session with Sqlite", "description": "Handle User Session with Sqlite3",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git@git.node001.net:nano/session.git" "url": "git@git.node001.net:nano/session.git"
}, },
"author": "Björn Hase <me@herr-hase.wtf>", "author": "Björn Hase <herrhase@node001.net>",
"license": "MIT", "license": "MIT",
"type": "module", "type": "module",
"main": "index.js",
"scripts": { "scripts": {
"test": "bun test ./test/*" "test": "node --test ./test/*"
}, },
"dependencies": { "dependencies": {
"@nano/sqlite": "0.2.1", "@nano/sqlite": "^1.0.1",
"dayjs": "^1.11.13" "dayjs": "^1.11.13"
}, },
"devDependencies": { "devDependencies": {

@ -1,7 +1,7 @@
import { createHmac, createHash, randomBytes } from 'node:crypto' import { createHmac, createHash, randomBytes } from 'node:crypto'
import dayjs from 'dayjs' import dayjs from 'dayjs'
import SessionStore from './sessionStore.ts' import SessionStore from './sessionStore.js'
/** /**
* Session * Session
@ -13,25 +13,18 @@ import SessionStore from './sessionStore.ts'
*/ */
class Session { class Session {
// database object
private db
// store for sessions
private sessionStore
// max logins for one user
private maxLogins = 5;
// duration for expired_at
private duration = 60;
/** /**
* *
* @param object db * @param object db
* *
*/ */
constructor(db: object) { constructor(db) {
this.db = db
// max logins for one user
this._maxLogins = 5;
// duration for expired_at
this._duration = 60;
this.sessionStore = new SessionStore(db) this.sessionStore = new SessionStore(db)
if (!process.env.NANO_SESSION_SECRET) { if (!process.env.NANO_SESSION_SECRET) {
@ -40,12 +33,12 @@ class Session {
// if NANO_MAX_LOGINS is set in .env // if NANO_MAX_LOGINS is set in .env
if (process.env.NANO_SESSION_MAX_LOGINS) { if (process.env.NANO_SESSION_MAX_LOGINS) {
this.maxLogins = process.env.NANO_SESSION_MAX_LOGINS this._maxLogins = process.env.NANO_SESSION_MAX_LOGINS
} }
// if NANO_SESSION_DURATION is set in .env // if NANO_SESSION_DURATION is set in .env
if (process.env.NANO_SESSION_DURATION) { if (process.env.NANO_SESSION_DURATION) {
this.duration = process.env.NANO_SESSION_DURATION this._duration = process.env.NANO_SESSION_DURATION
} }
} }
@ -59,19 +52,19 @@ class Session {
* @return object * @return object
* *
*/ */
public async create(userId: number, agent: string, language: string, ip = ''): object { async create(userId, agent, language, ip) {
const userHash = this.createUserHash(userId) const userHash = this._createUserHash(userId)
const userHmac = this.createUserHmac(userHash) const userHmac = this._createUserHmac(userHash)
const users = await this.sessionStore.findOneByUser(userHmac) const users = await this.sessionStore.findOneByUser(userHmac)
// if users are not longer can login, drop oldest // if users are not longer can login, drop oldest
if (users && users.length > this.maxLogins) { if (users && users.length > this._maxLogins) {
const session = await this.sessionStore.dropOldest(userHash) const session = await this.sessionStore.dropOldest(userHash)
} }
const token = randomBytes(64).toString('hex') const token = randomBytes(64).toString('hex')
const tokenHmac = this.createTokenHmac(token, agent, language, ip) const tokenHmac = this._createTokenHmac(token, agent, language, ip)
// create date // create date
const date = dayjs() const date = dayjs()
@ -80,7 +73,7 @@ class Session {
'user_id': userId, 'user_id': userId,
'user' : userHmac, 'user' : userHmac,
'token' : tokenHmac, 'token' : tokenHmac,
'expired_at': date.add(this.duration, 'minute').toISOString(), 'expired_at': date.add(this._duration, 'minute').toISOString(),
'created_at': date.toISOString() 'created_at': date.toISOString()
}) })
@ -96,7 +89,7 @@ class Session {
* @param object userId * @param object userId
* *
*/ */
public async destroy(sessionId: number): void { async destroy(sessionId) {
await this.sessionStore.remove(sessionId) await this.sessionStore.remove(sessionId)
} }
@ -106,8 +99,8 @@ class Session {
* @param number userId * @param number userId
* *
*/ */
public async destroyAll(userId: number): void { async destroyAll(userId) {
const userHmac = this.createUserHmac(this.createUserHash(userId)) const userHmac = this._createUserHmac(this._createUserHash(userId))
const sessions = await this.sessionStore.findByUser(userHmac) const sessions = await this.sessionStore.findByUser(userHmac)
for (const index in sessions) { for (const index in sessions) {
@ -126,9 +119,9 @@ class Session {
* @return string * @return string
* *
*/ */
public async get(userHash: string, token: string, agent: string, language: string, ip = ''): string { async get(userHash, token, agent, language, ip) {
const userHmac = this.createUserHmac(userHash) const userHmac = this._createUserHmac(userHash)
const tokenHmac = this.createTokenHmac(token, agent, language, ip) const tokenHmac = this._createTokenHmac(token, agent, language, ip)
let session = await this.sessionStore.findOneByUserAndToken(userHmac, tokenHmac) let session = await this.sessionStore.findOneByUserAndToken(userHmac, tokenHmac)
@ -136,7 +129,7 @@ class Session {
if (session && session.expired_at) { if (session && session.expired_at) {
if (dayjs().isAfter(dayjs(session.expired_at))) { if (dayjs().isAfter(dayjs(session.expired_at))) {
await this.destroy(session.id) await this.destroy(session.id)
session = null session = undefined
} }
} }
@ -150,9 +143,9 @@ class Session {
* @return string * @return string
* *
*/ */
private createUserHash(userId: number): string { _createUserHash(userId) {
const hash = createHash('sha256') const hash = createHash('sha256')
const buffer = new Buffer(userId) const buffer = Buffer.from(String(userId))
return hash.update(buffer).digest('hex') return hash.update(buffer).digest('hex')
} }
@ -164,9 +157,9 @@ class Session {
* @return string * @return string
* *
*/ */
private createUserHmac(userHash: string): string { _createUserHmac(userHash) {
const hmac = createHmac('sha512', process.env.NANO_SESSION_SECRET) const hmac = createHmac('sha512', process.env.NANO_SESSION_SECRET)
const buffer = new Buffer(userHash) const buffer = Buffer.from(String(userHash))
return hmac.update(buffer).digest('base64') return hmac.update(buffer).digest('base64')
} }
@ -181,9 +174,9 @@ class Session {
* @return string * @return string
* *
*/ */
private createTokenHmac(token: string, agent: string, language: string, ip = ''): string { _createTokenHmac(token, agent, language, ip) {
const hmac = createHmac('sha512', process.env.NANO_SESSION_SECRET) const hmac = createHmac('sha512', process.env.NANO_SESSION_SECRET)
const buffer = new Buffer(token + agent + language + ip) const buffer = Buffer.from(String(token + agent + language + ip))
return hmac.update(buffer).digest('base64') return hmac.update(buffer).digest('base64')
} }

@ -0,0 +1,40 @@
import { Store } from '@nano/sqlite'
/**
* SessionStore
*
* @author Björn Hase <me@herr-hase.wtf>
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://git.node001.net/nano/session.git
*
*/
class SessionStore extends Store {
constructor(db) {
super(db, 'sessions')
}
async findOneByUser(userHmac) {
return this._db.prepare('SELECT * FROM ' + this._tableName + ' WHERE user = $user')
.get({
'user': userHmac
})
}
async findByUser(userHmac) {
return this._db.prepare('SELECT * FROM ' + this._tableName + ' WHERE user = $user')
.all({
'user': userHmac
})
}
async findOneByUserAndToken(userHmac, tokenHmac) {
return this._db.prepare('SELECT * FROM ' + this._tableName + ' WHERE user = $user AND token = $token')
.get({
'user' : userHmac,
'token' : tokenHmac
})
}
}
export default SessionStore

@ -1,40 +0,0 @@
import { Store } from '@nano/sqlite'
/**
* SessionStore
*
* @author Björn Hase <me@herr-hase.wtf>
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://git.node001.net/nano/session.git
*
*/
class SessionStore extends Store {
constructor(db) {
super(db, 'sessions')
}
public async findOneByUser(userHmac) {
return this.db.query('SELECT * FROM ' + this.tableName + ' WHERE user = $user')
.get({
'$user': userHmac
})
}
public async findByUser(userHmac) {
return this.db.query('SELECT * FROM ' + this.tableName + ' WHERE user = $user')
.all({
'$user': userHmac
})
}
public async findOneByUserAndToken(userHmac, tokenHmac) {
return this.db.query('SELECT * FROM ' + this.tableName + ' WHERE user = $user AND token = $token')
.get({
'$user' : userHmac,
'$token' : tokenHmac
})
}
}
export default SessionStore

@ -0,0 +1,15 @@
import { test } from 'node:test'
import assert from 'node:assert/strict'
import path from 'path'
import { runMigrationSqlite, getOrCreateSqlite } from '@nano/sqlite'
test('migration', async () => {
const db = getOrCreateSqlite({ 'uri': ':memory:', 'create': true, 'readwrite': true })
await runMigrationSqlite(path.resolve(path.resolve(), './src/migration'), db)
// check for boxes
const results = db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='sessions'").get()
assert.equal(results.name, 'sessions')
})

@ -1,15 +0,0 @@
import { expect, test } from 'bun:test'
import path from 'path'
import { runMigrationSqlite, getOrCreateSqlite } from '@nano/sqlite'
import { Database } from 'bun:sqlite'
test('migration', async () => {
const db = getOrCreateSqlite({ 'name': ':memory:', 'create': true, 'readwrite': true })
await runMigrationSqlite(path.resolve(__dirname, './../src/migration'), db)
// check for boxes
const results = db.query("SELECT name FROM sqlite_master WHERE type='table' AND name='sessions'").get()
expect(results).toEqual({ name: 'sessions' })
})

@ -1,14 +1,15 @@
import { expect, test } from 'bun:test' import { test } from 'node:test'
import assert from 'node:assert/strict'
import { runMigrationSqlite, getOrCreateSqlite } from '@nano/sqlite' import { runMigrationSqlite, getOrCreateSqlite } from '@nano/sqlite'
import SessionStore from './../src/sessionStore.ts' import SessionStore from './../src/sessionStore.js'
import dayjs from 'dayjs' import dayjs from 'dayjs'
import path from 'path' import path from 'path'
import dotenv from 'dotenv' import dotenv from 'dotenv'
import Session from './../src/session.ts' import Session from './../src/session.js'
const userHashResult = '6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d' const userHashResult = '6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b'
const userHmacResult = 'qMMo5J3Kq0q3+IPadNretbmitllWakjUTD/6HnTRdSNoifrjkLEf5p6sc0qx2q0bSGTeqXI4OaP5VuUQk0EzuQ==' const userHmacResult = 'RrnHJRqdfuq/kHK+RChE8q9T84xSj40IT/nqD5X6yenlacHj4/XbPsTAYFQ7diBxCADzrqnIE9jrtN+FHiWrbw=='
const request = { const request = {
agent: 'Mozilla/2.0 (X11; Windows x86;) Gecko/20100101 Firefox/120.0', agent: 'Mozilla/2.0 (X11; Windows x86;) Gecko/20100101 Firefox/120.0',
agentChrome: 'Mozilla/2.0 (X11; Windows x86;) Gecko/20100101 Chrome/121.0', agentChrome: 'Mozilla/2.0 (X11; Windows x86;) Gecko/20100101 Chrome/121.0',
@ -16,51 +17,45 @@ const request = {
ip: '1.1.1.1' ip: '1.1.1.1'
} }
test('session / create', async () => { dotenv.config({ path: './.env.test' })
const db = getOrCreateSqlite({ 'name': ':memory:', 'create': true, 'readwrite': true })
await runMigrationSqlite(path.resolve(__dirname, './../src/migration'), db)
dotenv.config('./../.env.test') test('session / create', async () => {
const db = getOrCreateSqlite({ 'uri': ':memory:', 'create': true, 'readwrite': true })
await runMigrationSqlite(path.resolve(path.resolve(), './src/migration'), db)
const session = new Session(db) const session = new Session(db)
const result = await session.create(1, request.agent, request.language, request.ip) const result = await session.create(1, request.agent, request.language, request.ip)
expect(result.user).toEqual(userHashResult) assert.equal(result.user, userHashResult)
}) })
test('session / find', async () => { test('session / find', async () => {
const db = getOrCreateSqlite({ 'name': ':memory:', 'create': true, 'readwrite': true }) const db = getOrCreateSqlite({ 'uri': ':memory:', 'create': true, 'readwrite': true })
await runMigrationSqlite(path.resolve(__dirname, './../src/migration'), db) await runMigrationSqlite(path.resolve(path.resolve(), './src/migration'), db)
dotenv.config('./../.env.test')
const session = new Session(db) const session = new Session(db)
const result = await session.create(1, request.agent, request.language, request.ip) const result = await session.create(1, request.agent, request.language, request.ip)
const userSession = await session.get(result.user, result.token, request.agent, request.language, request.ip) const userSession = await session.get(result.user, result.token, request.agent, request.language, request.ip)
expect(userHmacResult).toEqual(userSession.user) assert.equal(userSession.user, userHmacResult)
}) })
test('session / find / change browser', async () => { test('session / find / change browser', async () => {
const db = getOrCreateSqlite({ 'name': ':memory:', 'create': true, 'readwrite': true }) const db = getOrCreateSqlite({ 'uri': ':memory:', 'create': true, 'readwrite': true })
await runMigrationSqlite(path.resolve(__dirname, './../src/migration'), db) await runMigrationSqlite(path.resolve(path.resolve(), './src/migration'), db)
dotenv.config('./../.env.test')
const session = new Session(db) const session = new Session(db)
const result = await session.create(1, request.agent, request.language, request.ip) const result = await session.create(1, request.agent, request.language, request.ip)
const userSession = await session.get(result.user, result.token, request.agentChrome, request.language, request.ip) const userSession = await session.get(result.user, result.token, request.agentChrome, request.language, request.ip)
expect(null).toEqual(userSession) assert.equal(userSession, undefined)
}) })
test('session / expired', async () => { test('session / expired', async () => {
const db = getOrCreateSqlite({ 'name': ':memory:', 'create': true, 'readwrite': true }) const db = getOrCreateSqlite({ 'uri': ':memory:', 'create': true, 'readwrite': true })
await runMigrationSqlite(path.resolve(__dirname, './../src/migration'), db) await runMigrationSqlite(path.resolve(path.resolve(), './src/migration'), db)
dotenv.config('./../.env.test')
const session = new Session(db) const session = new Session(db)
const result = await session.create(1, request.agent, request.language, request.ip) const result = await session.create(1, request.agent, request.language, request.ip)
@ -72,14 +67,12 @@ test('session / expired', async () => {
// getting user // getting user
const user = await session.get(result.user, result.token, request.agent, request.language, request.ip) const user = await session.get(result.user, result.token, request.agent, request.language, request.ip)
expect(null).toEqual(user) // user not found, because expired user is deleted assert.equal(user, undefined) // user not found, because expired user is deleted
}) })
test('session / destroy all', async () => { test('session / destroy all', async () => {
const db = getOrCreateSqlite({ 'name': ':memory:', 'create': true, 'readwrite': true }) const db = getOrCreateSqlite({ 'uri': ':memory:', 'create': true, 'readwrite': true })
await runMigrationSqlite(path.resolve(__dirname, './../src/migration'), db) await runMigrationSqlite(path.resolve(path.resolve(), './src/migration'), db)
dotenv.config('./../.env.test')
const session = new Session(db) const session = new Session(db)
@ -90,7 +83,7 @@ test('session / destroy all', async () => {
await session.create(1, request.agent, request.language, request.ip) await session.create(1, request.agent, request.language, request.ip)
await session.destroyAll(1) await session.destroyAll(1)
const result = db.query("SELECT * FROM sessions").all() const result = db.prepare("SELECT * FROM sessions").all()
expect([]).toEqual(result) assert.equal(result.length, 0)
}) })
Loading…
Cancel
Save