authored 0.0.2 → 0.0.4
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/LICENSE +1 -1
- package/{index.d.ts → dist/index.d.ts} +95 -78
- package/dist/index.js +4 -0
- package/package.json +23 -12
- package/index.js +0 -1
- package/jsconfig.json +0 -27
package/LICENSE
CHANGED
|
@@ -1,46 +1,23 @@
|
|
|
1
|
-
import { Client } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import { Client } from 'pg';
|
|
2
|
+
import { obj } from '@coff-r/x';
|
|
3
|
+
|
|
4
|
+
declare class Signator {
|
|
5
|
+
salt: string;
|
|
6
|
+
constructor(salt: string);
|
|
7
|
+
getSignature(val: string): string;
|
|
8
|
+
deriveKey(): Buffer<ArrayBufferLike>;
|
|
9
|
+
sign(val: string): string;
|
|
10
|
+
unsign(signedVal: string): boolean;
|
|
11
|
+
loadUnsign(vals: string): string | undefined;
|
|
12
|
+
verifySignature(val: string, sig: string): boolean;
|
|
11
13
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
COOKIE_SECURE: boolean;
|
|
18
|
-
REFRESH_EACH_REQUEST: boolean;
|
|
19
|
-
COOKIE_SAMESITE: string;
|
|
20
|
-
KEY_PREFIX: string;
|
|
21
|
-
PERMANENT: boolean;
|
|
22
|
-
USE_SIGNER: boolean;
|
|
23
|
-
ID_LENGTH: number;
|
|
24
|
-
FILE_THRESHOLD: number;
|
|
25
|
-
LIFETIME: number;
|
|
26
|
-
MAX_COOKIE_SIZE: number;
|
|
27
|
-
INTERFACE: dbs;
|
|
28
|
-
STORAGE: string;
|
|
29
|
-
JWT_STORAGE: string;
|
|
30
|
-
JWT_LIFETIME: number;
|
|
31
|
-
};
|
|
32
|
-
type dbs = "fs" | "postgres";
|
|
33
|
-
export declare class Auth {
|
|
34
|
-
postgresClient?: Client;
|
|
35
|
-
config: authConfig;
|
|
36
|
-
constructor({ type, dir }?: {
|
|
37
|
-
type?: dbs;
|
|
38
|
-
dir?: string;
|
|
39
|
-
});
|
|
40
|
-
initStorage(path: string): this;
|
|
41
|
-
get session(): AuthInterface;
|
|
42
|
-
get jwt(): FSInterface;
|
|
14
|
+
|
|
15
|
+
declare class sidGenerator {
|
|
16
|
+
signer: Signator;
|
|
17
|
+
constructor(salt: string);
|
|
18
|
+
generate(len?: number): string;
|
|
43
19
|
}
|
|
20
|
+
|
|
44
21
|
declare class callBack {
|
|
45
22
|
[Key: string]: any;
|
|
46
23
|
data: obj<string>;
|
|
@@ -53,7 +30,7 @@ declare class callBack {
|
|
|
53
30
|
has(target: any, prop: string): boolean;
|
|
54
31
|
deleteProperty(target: any, val: string): boolean;
|
|
55
32
|
}
|
|
56
|
-
|
|
33
|
+
declare class ServerSide extends callBack {
|
|
57
34
|
sid: string;
|
|
58
35
|
[Key: string]: any;
|
|
59
36
|
modified: boolean;
|
|
@@ -61,22 +38,8 @@ export declare class ServerSide extends callBack {
|
|
|
61
38
|
constructor(sid?: string, initial?: obj<string>, readonly?: boolean);
|
|
62
39
|
get session(): ServerSide;
|
|
63
40
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
constructor(salt: string);
|
|
67
|
-
getSignature(val: string): string;
|
|
68
|
-
deriveKey(): Buffer;
|
|
69
|
-
sign(val: string): string;
|
|
70
|
-
unsign(signedVal: string): boolean;
|
|
71
|
-
loadUnsign(vals: string): string | undefined;
|
|
72
|
-
verifySignature(val: string, sig: string): boolean;
|
|
73
|
-
}
|
|
74
|
-
declare class sidGenerator {
|
|
75
|
-
signer: Signator;
|
|
76
|
-
constructor(salt: string);
|
|
77
|
-
generate(len?: number): string;
|
|
78
|
-
}
|
|
79
|
-
export declare class AuthInterface extends sidGenerator {
|
|
41
|
+
|
|
42
|
+
declare class AuthInterface extends sidGenerator {
|
|
80
43
|
config: authConfig;
|
|
81
44
|
constructor(config: authConfig, salt?: string);
|
|
82
45
|
openSession(sid?: string, readonly?: boolean): Promise<ServerSide>;
|
|
@@ -88,7 +51,10 @@ export declare class AuthInterface extends sidGenerator {
|
|
|
88
51
|
setCookie(xsesh: ServerSide, life: Date | number, _sameSite?: string): string;
|
|
89
52
|
loadHeader(req: any, readonly?: boolean): Promise<ServerSide>;
|
|
90
53
|
}
|
|
91
|
-
|
|
54
|
+
|
|
55
|
+
interface bs$1 {
|
|
56
|
+
f_timed?: number;
|
|
57
|
+
[key: string]: string | undefined | boolean | number;
|
|
92
58
|
}
|
|
93
59
|
interface ffcache {
|
|
94
60
|
[key: string]: string | undefined | boolean | number;
|
|
@@ -96,7 +62,7 @@ interface ffcache {
|
|
|
96
62
|
data: string;
|
|
97
63
|
life: number;
|
|
98
64
|
}
|
|
99
|
-
declare class FSCached<T extends bs> {
|
|
65
|
+
declare class FSCached<T extends bs$1> {
|
|
100
66
|
path: string;
|
|
101
67
|
data: Map<any, T>;
|
|
102
68
|
constructor(folderpath: string);
|
|
@@ -106,6 +72,9 @@ declare class FSCached<T extends bs> {
|
|
|
106
72
|
set(val: string, data: T): Promise<void>;
|
|
107
73
|
delete(key: string): Promise<void>;
|
|
108
74
|
}
|
|
75
|
+
|
|
76
|
+
declare class FSession extends ServerSide {
|
|
77
|
+
}
|
|
109
78
|
declare class FSInterface extends AuthInterface {
|
|
110
79
|
isJWT: boolean;
|
|
111
80
|
cacher: FSCached<ffcache>;
|
|
@@ -115,7 +84,12 @@ declare class FSInterface extends AuthInterface {
|
|
|
115
84
|
fetchSession(sid: string, readonly?: boolean): Promise<ServerSide>;
|
|
116
85
|
saveSession(sesh: ServerSide, X?: Headers, deleteMe?: boolean): Promise<void>;
|
|
117
86
|
}
|
|
118
|
-
|
|
87
|
+
|
|
88
|
+
interface bs {
|
|
89
|
+
f_timed?: number;
|
|
90
|
+
[key: string]: string | undefined | boolean | number;
|
|
91
|
+
}
|
|
92
|
+
declare class PGCache<T extends bs> {
|
|
119
93
|
client: Client;
|
|
120
94
|
query: string;
|
|
121
95
|
f_timed: number;
|
|
@@ -128,7 +102,26 @@ export declare class PGCache<T extends bs> {
|
|
|
128
102
|
set(data: T): Promise<void>;
|
|
129
103
|
delete(key: string): Promise<void>;
|
|
130
104
|
}
|
|
131
|
-
|
|
105
|
+
|
|
106
|
+
declare class PostgreSession extends ServerSide {
|
|
107
|
+
}
|
|
108
|
+
interface sesh_db {
|
|
109
|
+
sid: string;
|
|
110
|
+
data: string;
|
|
111
|
+
expiration: string;
|
|
112
|
+
f_timed?: number;
|
|
113
|
+
[key: string]: string | undefined | boolean | number;
|
|
114
|
+
}
|
|
115
|
+
declare class PGInterface extends AuthInterface {
|
|
116
|
+
sclass: typeof ServerSide;
|
|
117
|
+
client: Client;
|
|
118
|
+
pgc: PGCache<sesh_db>;
|
|
119
|
+
constructor(client: Client, config: authConfig);
|
|
120
|
+
fetchSession(sid: string): Promise<ServerSide>;
|
|
121
|
+
saveSession(xsesh: ServerSide, rsx?: any, deleteMe?: boolean, sameSite?: string): Promise<void>;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
declare class JWTSession extends sidGenerator {
|
|
132
125
|
salt: string;
|
|
133
126
|
constructor();
|
|
134
127
|
sign(payload: obj<any>): string;
|
|
@@ -149,21 +142,45 @@ export declare class JWTInterface extends sidGenerator {
|
|
|
149
142
|
save(xjwts: ServerSide): string;
|
|
150
143
|
new(payload: obj<any>): string;
|
|
151
144
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
145
|
+
|
|
146
|
+
type dbs = "fs" | "postgres";
|
|
147
|
+
interface authConfig {
|
|
148
|
+
COOKIE_NAME: string;
|
|
149
|
+
COOKIE_DOMAIN: string;
|
|
150
|
+
COOKIE_PATH: string;
|
|
151
|
+
COOKIE_HTTPONLY: boolean;
|
|
152
|
+
COOKIE_SECURE: boolean;
|
|
153
|
+
REFRESH_EACH_REQUEST: boolean;
|
|
154
|
+
COOKIE_SAMESITE: string;
|
|
155
|
+
KEY_PREFIX: string;
|
|
156
|
+
PERMANENT: boolean;
|
|
157
|
+
USE_SIGNER: boolean;
|
|
158
|
+
ID_LENGTH: number;
|
|
159
|
+
FILE_THRESHOLD: number;
|
|
160
|
+
LIFETIME: number;
|
|
161
|
+
MAX_COOKIE_SIZE: number;
|
|
162
|
+
INTERFACE: dbs;
|
|
163
|
+
STORAGE: string;
|
|
164
|
+
JWT_STORAGE: string;
|
|
165
|
+
JWT_LIFETIME: number;
|
|
166
|
+
}
|
|
167
|
+
declare class Auth {
|
|
168
|
+
postgresClient?: Client;
|
|
169
|
+
config: authConfig;
|
|
170
|
+
constructor({ type, dir }?: {
|
|
171
|
+
type?: dbs;
|
|
172
|
+
dir?: string;
|
|
162
173
|
});
|
|
163
|
-
|
|
164
|
-
get(
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
174
|
+
initStorage(path: string): this;
|
|
175
|
+
get session(): AuthInterface;
|
|
176
|
+
get jwt(): FSInterface;
|
|
177
|
+
}
|
|
178
|
+
declare class Session {
|
|
179
|
+
session: AuthInterface;
|
|
180
|
+
jwt: AuthInterface;
|
|
181
|
+
jwtInt: JWTSession;
|
|
182
|
+
init(sh: Auth): void;
|
|
168
183
|
}
|
|
169
|
-
|
|
184
|
+
|
|
185
|
+
export { Auth, AuthInterface, FSCached, FSInterface, FSession, PGCache, PGInterface, PostgreSession, ServerSide, Session };
|
|
186
|
+
export type { authConfig };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
var ot=Object.create;var{getPrototypeOf:lt,defineProperty:ee,getOwnPropertyNames:ct}=Object;var ut=Object.prototype.hasOwnProperty;var ft=(e,t,s)=>{s=e!=null?ot(lt(e)):{};let r=t||!e||!e.__esModule?ee(s,"default",{value:e,enumerable:!0}):s;for(let i of ct(e))if(!ut.call(r,i))ee(r,i,{get:()=>e[i],enumerable:!0});return r};var te=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports);var j=import.meta.require;var Xe=te((ks,bt)=>{bt.exports={name:"dotenv",version:"17.2.3",description:"Loads environment variables from .env file",main:"lib/main.js",types:"lib/main.d.ts",exports:{".":{types:"./lib/main.d.ts",require:"./lib/main.js",default:"./lib/main.js"},"./config":"./config.js","./config.js":"./config.js","./lib/env-options":"./lib/env-options.js","./lib/env-options.js":"./lib/env-options.js","./lib/cli-options":"./lib/cli-options.js","./lib/cli-options.js":"./lib/cli-options.js","./package.json":"./package.json"},scripts:{"dts-check":"tsc --project tests/types/tsconfig.json",lint:"standard",pretest:"npm run lint && npm run dts-check",test:"tap run tests/**/*.js --allow-empty-coverage --disable-coverage --timeout=60000","test:coverage":"tap run tests/**/*.js --show-full-coverage --timeout=60000 --coverage-report=text --coverage-report=lcov",prerelease:"npm test",release:"standard-version"},repository:{type:"git",url:"git://github.com/motdotla/dotenv.git"},homepage:"https://github.com/motdotla/dotenv#readme",funding:"https://dotenvx.com",keywords:["dotenv","env",".env","environment","variables","config","settings"],readmeFilename:"README.md",license:"BSD-2-Clause",devDependencies:{"@types/node":"^18.11.3",decache:"^4.6.2",sinon:"^14.0.1",standard:"^17.0.0","standard-version":"^9.5.0",tap:"^19.2.0",typescript:"^4.8.4"},engines:{node:">=12"},browser:{fs:!1}}});var et=te((Hs,y)=>{var U=j("fs"),$=j("path"),wt=j("os"),Et=j("crypto"),Ot=Xe(),G=Ot.version,qe=["\uD83D\uDD10 encrypt with Dotenvx: https://dotenvx.com","\uD83D\uDD10 prevent committing .env to code: https://dotenvx.com/precommit","\uD83D\uDD10 prevent building .env in docker: https://dotenvx.com/prebuild","\uD83D\uDCE1 add observability to secrets: https://dotenvx.com/ops","\uD83D\uDC65 sync secrets across teammates & machines: https://dotenvx.com/ops","\uD83D\uDDC2\uFE0F backup and recover secrets: https://dotenvx.com/ops","\u2705 audit secrets and track compliance: https://dotenvx.com/ops","\uD83D\uDD04 add secrets lifecycle management: https://dotenvx.com/ops","\uD83D\uDD11 add access controls to secrets: https://dotenvx.com/ops","\uD83D\uDEE0\uFE0F run anywhere with `dotenvx run -- yourcommand`","\u2699\uFE0F specify custom .env file path with { path: '/custom/path/.env' }","\u2699\uFE0F enable debug logging with { debug: true }","\u2699\uFE0F override existing env vars with { override: true }","\u2699\uFE0F suppress all logs with { quiet: true }","\u2699\uFE0F write to custom object with { processEnv: myObject }","\u2699\uFE0F load multiple .env files with { path: ['.env.local', '.env'] }"];function Mt(){return qe[Math.floor(Math.random()*qe.length)]}function O(e){if(typeof e==="string")return!["false","0","no","off",""].includes(e.toLowerCase());return Boolean(e)}function Tt(){return process.stdout.isTTY}function xt(e){return Tt()?`\x1B[2m${e}\x1B[0m`:e}var At=/(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/mg;function It(e){let t={},s=e.toString();s=s.replace(/\r\n?/mg,`
|
|
3
|
+
`);let r;while((r=At.exec(s))!=null){let i=r[1],n=r[2]||"";n=n.trim();let a=n[0];if(n=n.replace(/^(['"`])([\s\S]*)\1$/mg,"$2"),a==='"')n=n.replace(/\\n/g,`
|
|
4
|
+
`),n=n.replace(/\\r/g,"\r");t[i]=n}return t}function Ct(e){e=e||{};let t=ze(e);e.path=t;let s=f.configDotenv(e);if(!s.parsed){let a=Error(`MISSING_DATA: Cannot parse ${t} for an unknown reason`);throw a.code="MISSING_DATA",a}let r=Ve(e).split(","),i=r.length,n;for(let a=0;a<i;a++)try{let o=r[a].trim(),c=Nt(s,o);n=f.decrypt(c.ciphertext,c.key);break}catch(o){if(a+1>=i)throw o}return f.parse(n)}function jt(e){console.error(`[dotenv@${G}][WARN] ${e}`)}function I(e){console.log(`[dotenv@${G}][DEBUG] ${e}`)}function Qe(e){console.log(`[dotenv@${G}] ${e}`)}function Ve(e){if(e&&e.DOTENV_KEY&&e.DOTENV_KEY.length>0)return e.DOTENV_KEY;if(process.env.DOTENV_KEY&&process.env.DOTENV_KEY.length>0)return process.env.DOTENV_KEY;return""}function Nt(e,t){let s;try{s=new URL(t)}catch(o){if(o.code==="ERR_INVALID_URL"){let c=Error("INVALID_DOTENV_KEY: Wrong format. Must be in valid uri format like dotenv://:key_1234@dotenvx.com/vault/.env.vault?environment=development");throw c.code="INVALID_DOTENV_KEY",c}throw o}let r=s.password;if(!r){let o=Error("INVALID_DOTENV_KEY: Missing key part");throw o.code="INVALID_DOTENV_KEY",o}let i=s.searchParams.get("environment");if(!i){let o=Error("INVALID_DOTENV_KEY: Missing environment part");throw o.code="INVALID_DOTENV_KEY",o}let n=`DOTENV_VAULT_${i.toUpperCase()}`,a=e.parsed[n];if(!a){let o=Error(`NOT_FOUND_DOTENV_ENVIRONMENT: Cannot locate environment ${n} in your .env.vault file.`);throw o.code="NOT_FOUND_DOTENV_ENVIRONMENT",o}return{ciphertext:a,key:r}}function ze(e){let t=null;if(e&&e.path&&e.path.length>0)if(Array.isArray(e.path)){for(let s of e.path)if(U.existsSync(s))t=s.endsWith(".vault")?s:`${s}.vault`}else t=e.path.endsWith(".vault")?e.path:`${e.path}.vault`;else t=$.resolve(process.cwd(),".env.vault");if(U.existsSync(t))return t;return null}function Ze(e){return e[0]==="~"?$.join(wt.homedir(),e.slice(1)):e}function _t(e){let t=O(process.env.DOTENV_CONFIG_DEBUG||e&&e.debug),s=O(process.env.DOTENV_CONFIG_QUIET||e&&e.quiet);if(t||!s)Qe("Loading env from encrypted .env.vault");let r=f._parseVault(e),i=process.env;if(e&&e.processEnv!=null)i=e.processEnv;return f.populate(i,r,e),{parsed:r}}function Rt(e){let t=$.resolve(process.cwd(),".env"),s="utf8",r=process.env;if(e&&e.processEnv!=null)r=e.processEnv;let i=O(r.DOTENV_CONFIG_DEBUG||e&&e.debug),n=O(r.DOTENV_CONFIG_QUIET||e&&e.quiet);if(e&&e.encoding)s=e.encoding;else if(i)I("No encoding is specified. UTF-8 is used by default");let a=[t];if(e&&e.path)if(!Array.isArray(e.path))a=[Ze(e.path)];else{a=[];for(let l of e.path)a.push(Ze(l))}let o,c={};for(let l of a)try{let u=f.parse(U.readFileSync(l,{encoding:s}));f.populate(c,u,e)}catch(u){if(i)I(`Failed to load ${l} ${u.message}`);o=u}let h=f.populate(r,c,e);if(i=O(r.DOTENV_CONFIG_DEBUG||i),n=O(r.DOTENV_CONFIG_QUIET||n),i||!n){let l=Object.keys(h).length,u=[];for(let g of a)try{let m=$.relative(process.cwd(),g);u.push(m)}catch(m){if(i)I(`Failed to load ${g} ${m.message}`);o=m}Qe(`injecting env (${l}) from ${u.join(",")} ${xt(`-- tip: ${Mt()}`)}`)}if(o)return{parsed:c,error:o};else return{parsed:c}}function Dt(e){if(Ve(e).length===0)return f.configDotenv(e);let t=ze(e);if(!t)return jt(`You set DOTENV_KEY but you are missing a .env.vault file at ${t}. Did you forget to build it?`),f.configDotenv(e);return f._configVault(e)}function $t(e,t){let s=Buffer.from(t.slice(-64),"hex"),r=Buffer.from(e,"base64"),i=r.subarray(0,12),n=r.subarray(-16);r=r.subarray(12,-16);try{let a=Et.createDecipheriv("aes-256-gcm",s,i);return a.setAuthTag(n),`${a.update(r)}${a.final()}`}catch(a){let o=a instanceof RangeError,c=a.message==="Invalid key length",h=a.message==="Unsupported state or unable to authenticate data";if(o||c){let l=Error("INVALID_DOTENV_KEY: It must be 64 characters long (or more)");throw l.code="INVALID_DOTENV_KEY",l}else if(h){let l=Error("DECRYPTION_FAILED: Please check your DOTENV_KEY");throw l.code="DECRYPTION_FAILED",l}else throw a}}function kt(e,t,s={}){let r=Boolean(s&&s.debug),i=Boolean(s&&s.override),n={};if(typeof t!=="object"){let a=Error("OBJECT_REQUIRED: Please check the processEnv argument being passed to populate");throw a.code="OBJECT_REQUIRED",a}for(let a of Object.keys(t))if(Object.prototype.hasOwnProperty.call(e,a)){if(i===!0)e[a]=t[a],n[a]=t[a];if(r)if(i===!0)I(`"${a}" is already defined and WAS overwritten`);else I(`"${a}" is already defined and was NOT overwritten`)}else e[a]=t[a],n[a]=t[a];return n}var f={configDotenv:Rt,_configVault:_t,_parseVault:Ct,config:Dt,decrypt:$t,parse:It,populate:kt};Hs.configDotenv=f.configDotenv;Hs._configVault=f._configVault;Hs._parseVault=f._parseVault;Hs.config=f.config;Hs.decrypt=f.decrypt;Hs.parse=f.parse;Hs.populate=f.populate;y.exports=f});import{randomBytes as Bt}from"crypto";var{getPrototypeOf:is,defineProperty:ht,getOwnPropertyNames:as}=Object;var gt=(e,t)=>{for(var s in t)ht(e,s,{get:t[s],enumerable:!0,configurable:!0,set:(r)=>t[s]=()=>r})};var se={};gt(se,{isWhitespace:()=>Ie,isWeakSet:()=>Se,isWeakMap:()=>ye,isUndefined:()=>P,isUint8Array:()=>ce,isSymbol:()=>ie,isSubclassOf:()=>Je,isString:()=>N,isSet:()=>pe,isRegExp:()=>De,isRecord:()=>he,isReadonlyArray:()=>ne,isPromise:()=>ke,isPrimitive:()=>Fe,isPositiveNumber:()=>Ne,isPlainObject:()=>T,isObjectLike:()=>be,isObject:()=>R,isNumberLike:()=>ae,isNumber:()=>b,isNullish:()=>Me,isNull:()=>Ee,isNotNullish:()=>Te,isNotNull:()=>w,isNonNegativeNumber:()=>_e,isNonFunctionObject:()=>we,isNonEmptyString:()=>Ae,isNonEmptyObject:()=>fe,isModule:()=>ve,isMap:()=>me,isIterable:()=>ge,isInteger:()=>Ce,isInstanceOf:()=>Pe,isFunction:()=>W,isFiniteNumber:()=>je,isError:()=>$e,isEmptyString:()=>xe,isEmpty:()=>He,isDefined:()=>Oe,isDate:()=>Re,isClass:()=>We,isBoolean:()=>L,isBinaryLike:()=>ue,isBigInt:()=>re,isAsyncIterable:()=>de,isAsyncFunction:()=>Le,isArrayBufferView:()=>le,isArrayBuffer:()=>oe,isArray:()=>_,IS_NOT_BROWSER:()=>J,IS_BROWSER:()=>Ye});var L=(e)=>typeof e==="boolean",N=(e)=>typeof e==="string",b=(e)=>typeof e==="number"&&!Number.isNaN(e),re=(e)=>typeof e==="bigint",ie=(e)=>typeof e==="symbol",W=(e)=>typeof e==="function",ae=(e)=>{let t=typeof e==="number"?e:parseFloat(String(e));return!Number.isNaN(t)&&Number.isFinite(t)},_=(e)=>Array.isArray(e),ne=(e)=>Array.isArray(e),oe=(e)=>e instanceof ArrayBuffer,le=(e)=>typeof ArrayBuffer<"u"&&ArrayBuffer.isView(e),ce=(e)=>e instanceof Uint8Array,ue=(e)=>e instanceof Uint8Array||e instanceof ArrayBuffer||typeof e==="string",R=(e)=>w(e)&&typeof e==="object",T=(e)=>w(e)&&R(e)&&(Object.getPrototypeOf(e)===Object.prototype||Object.getPrototypeOf(e)===null),fe=(e)=>T(e)&&Object.keys(e).length>0,he=(e)=>T(e),ge=(e)=>e!=null&&typeof e[Symbol.iterator]==="function",de=(e)=>e!=null&&typeof e[Symbol.asyncIterator]==="function",me=(e)=>e instanceof Map,pe=(e)=>e instanceof Set,ye=(e)=>e instanceof WeakMap,Se=(e)=>e instanceof WeakSet,be=(e)=>e!==null&&(typeof e==="object"||typeof e==="function"),we=(e)=>e!==null&&typeof e==="object",Ee=(e)=>e===null,P=(e)=>e===void 0,Oe=(e)=>!P(e),Me=(e)=>e==null,w=(e)=>e!==null,Te=(e)=>e!=null,xe=(e)=>typeof e==="string"&&e.length===0,Ae=(e)=>typeof e==="string"&&e.length>0,Ie=(e)=>typeof e==="string"&&e.length>0&&e.trim().length===0,Ce=(e)=>b(e)&&Number.isInteger(e),je=(e)=>b(e)&&Number.isFinite(e),Ne=(e)=>b(e)&&Number.isFinite(e)&&e>0,_e=(e)=>b(e)&&Number.isFinite(e)&&e>=0,Re=(e)=>e instanceof Date&&!Number.isNaN(e.getTime()),De=(e)=>e instanceof RegExp,$e=(e)=>e instanceof Error,ke=(e)=>!!e&&typeof e.then==="function",He=(e)=>e==null||typeof e==="string"&&e.length===0||Array.isArray(e)&&e.length===0||(e instanceof Map||e instanceof Set)&&e.size===0||T(e)&&Object.keys(e).length===0,Fe=(e)=>e===null||typeof e!=="object"&&typeof e!=="function",ve=(e)=>w(e)&&R(e)&&e[Symbol.toStringTag]==="Module",Ye=typeof window<"u",J=typeof window>"u",Le=(e)=>W(e)&&w(e.constructor)&&e.constructor.name==="AsyncFunction",We=(e)=>typeof e==="function"&&/^class\s/.test(Function.prototype.toString.call(e)),Pe=(e,t)=>e instanceof t,Je=(e,t)=>typeof e==="function"&&typeof t==="function"&&e.prototype.isPrototypeOf(t.prototype);var x=(e)=>{return new TextEncoder().encode(e)};var A=(e)=>{return new TextDecoder().decode(e)};var D=Object.assign;var K=(e)=>Object.keys(e).length;var S=(e,t=2)=>String(e).padStart(t,"0"),Ke=new Map;function E(e,t){let s=JSON.stringify({locale:t,...e}),r=Ke.get(s);if(!r)r=new Intl.DateTimeFormat(t,e),Ke.set(s,r);return r}var B={YYYY:(e)=>String(e.getFullYear()),YY:(e)=>S(e.getFullYear()%100),MMMM:(e,t)=>E({month:"long"},t).format(e),MMM:(e,t)=>E({month:"short"},t).format(e),MM:(e)=>S(e.getMonth()+1),M:(e)=>String(e.getMonth()+1),dddd:(e,t)=>E({weekday:"long"},t).format(e),ddd:(e,t)=>E({weekday:"short"},t).format(e),DD:(e)=>S(e.getDate()),D:(e)=>String(e.getDate()),HH:(e)=>S(e.getHours()),H:(e)=>String(e.getHours()),hh:(e)=>{let t=e.getHours()%12||12;return S(t)},h:(e)=>String(e.getHours()%12||12),A:(e,t)=>{return(E({hour:"numeric",hour12:!0},t).formatToParts(e).find((s)=>s.type==="dayPeriod")?.value??(e.getHours()<12?"AM":"PM")).toUpperCase()},a:(e,t)=>{return(E({hour:"numeric",hour12:!0},t).formatToParts(e).find((s)=>s.type==="dayPeriod")?.value??(e.getHours()<12?"AM":"PM")).toLowerCase()},mm:(e)=>S(e.getMinutes()),m:(e)=>String(e.getMinutes()),ss:(e)=>S(e.getSeconds()),s:(e)=>String(e.getSeconds()),SSS:(e)=>S(e.getMilliseconds(),3),Q:(e)=>String(Math.floor(e.getMonth()/3)+1),DoY:(e)=>{let t=new Date(e.getFullYear(),0,1),s=Math.floor((+e-+t)/86400000)+1;return String(s)}};function St(){if(globalThis.__dateTokenRegex)return globalThis.__dateTokenRegex;let e=new RegExp(Object.keys(B).sort((t,s)=>s.length-t.length).map((t)=>t.replace(/([.*+?^${}()|[\]\\])/g,"\\$1")).join("|"),"g");return globalThis.__dateTokenRegex=e,e}function Ge(e,t="YYYY-MM-DD HH:mm:ss",s){let r=typeof e==="number"?new Date(e):typeof e==="string"?new Date(e):e,i=St(),n=new Map;return t.replace(i,(a)=>{if(n.has(a))return n.get(a);let o=B[a],c=o?o(r,s):a;return n.set(a,c),c})}Date.prototype.format=function(e,t){return Ge(this,e,t)};Date.registerFormat=function(e,t){B[e]=t,globalThis.__dateTokenRegex=null};Date.format=function(e,t){return new Date().format(e,t)};class p{date;constructor(e){this.date=e?new Date(e):new Date}timed(e,t=1){let s=[["year","FullYear"],["month","Month"],["day","Date"],["hour","Hours"],["minute","Minutes"],["second","Seconds"],["millisecond","Milliseconds"]],r=this.date;if(e.quarter)r.setMonth(r.getMonth()+3*e.quarter);if(e.week)r.setDate(r.getDate()+7*e.week);for(let[i,n]of s){let a=e[i];if(a!==void 0&&a!==0)r[`set${n}`](r[`get${n}`]()+a)}if(e.startOfMonth)r.setDate(1),r.setHours(0,0,0,0);if(e.endOfMonth)r.setMonth(r.getMonth()+1,0),r.setHours(23,59,59,999);if(e.week!==void 0&&t!==void 0){let i=(r.getDay()-t+7)%7;r.setDate(r.getDate()-i)}return r}static timed(e,t=1){return new p().timed(e,t)}static local(e){return new Date(e).toLocaleString()}local(){return this.date.toLocaleDateString()}format(e="YYYY-MM-DD HH:mm:ss",t){return Ge(this.date,e,t)}static format(e="YYYY-MM-DD HH:mm:ss",t){let s=new Date,r=typeof s==="number"?new Date(s):typeof s==="string"?new Date(s):s,i=(m,nt=2)=>String(m).padStart(nt,"0"),n=r.getHours(),a=n%12||12,o=new Intl.DateTimeFormat(t,{month:"long"}).format(r),c=new Intl.DateTimeFormat(t,{month:"short"}).format(r),h=new Intl.DateTimeFormat(t,{weekday:"long"}).format(r),l=new Intl.DateTimeFormat(t,{weekday:"short"}).format(r),u=new Intl.DateTimeFormat(t,{hour:"numeric",hour12:!0}).formatToParts(r).find((m)=>m.type==="dayPeriod")?.value||(n<12?"AM":"PM"),g={YYYY:String(r.getFullYear()),YY:i(r.getFullYear()%100),MMMM:o,MMM:c,MM:i(r.getMonth()+1),M:String(r.getMonth()+1),dddd:h,ddd:l,DD:i(r.getDate()),D:String(r.getDate()),HH:i(n),H:String(n),hh:i(a),h:String(a),A:u.toUpperCase(),a:u.toLowerCase(),mm:i(r.getMinutes()),m:String(r.getMinutes()),ss:i(r.getSeconds()),s:String(r.getSeconds()),SSS:i(r.getMilliseconds(),3),Q:String(Math.floor(r.getMonth()/3)+1)};return e.replace(/YYYY|YY|MMMM|MMM|MM|M|dddd|ddd|DD|D|HH|H|hh|h|A|a|mm|m|ss|s|SSS|Q/g,(m)=>g[m])}static get now(){return Date.now()}random(e=new Date){let t=this.date.getTime(),s=e.getTime(),r=t+Math.random()*(s-t);return new Date(r)}static random(e,t=new Date){let s=e.getTime(),r=t.getTime(),i=s+Math.random()*(r-s);return new Date(i)}delta(e){let t=this.date;if(e.year)t.setFullYear(t.getFullYear()+e.year);if(e.month)t.setMonth(t.getMonth()+e.month);if(e.day)t.setDate(t.getDate()+e.day);if(e.hour)t.setHours(t.getHours()+e.hour);if(e.minute)t.setMinutes(t.getMinutes()+e.minute);if(e.second)t.setSeconds(t.getSeconds()+e.second);return t}static delta(e){return new p().delta(e)}}var Kt=ft(et(),1),{file:Ws}=globalThis.Bun,Jt=async(e)=>{return await Bun.file(e).exists()},tt=async(e,t="")=>{if(await Jt(e))return!0;return await Bun.write(e,t),!0};var k=()=>{let e=process.env.SECRET_KEY;if(!e)throw Error("'SECRET_KEY' not found in .env file");return e};var{file:Ps,write:Js}=globalThis.Bun;var X=(e,...t)=>{let s=new Bun.CryptoHasher("sha256",e);return t.forEach((r)=>{s.update(r)}),s.digest()};class q{salt;constructor(e){this.salt=e}getSignature(e){let t=this.deriveKey().toString();return X(t,e).toString("base64")}deriveKey(){return X(this.salt)}sign(e){let t=this.getSignature(e),s=x(e+"."+t);return A(s)}unsign(e){if(!(e.indexOf(".")>-1))throw Error("No sep found");let t=e.indexOf("."),s=e.slice(0,t),r=e.slice(t+1);return this.verifySignature(s,r)}loadUnsign(e){if(this.unsign(e)){let t=x(e),s=x(".").toString()[0];if(s){if(!(s in t))throw Error("No sep found");let r=t.indexOf(s),i=t.subarray(0,r);return Buffer.from(i.toString(),"base64").toString("utf-8")}}return}verifySignature(e,t){return this.getSignature(e)==t?!0:!1}}var{CryptoHasher:Ut}=globalThis.Bun;class C{signer;constructor(e){this.signer=new q(e)}generate(e=21){let s=Bt(e).toString("base64");if(s.endsWith("="))s=s.slice(0,-1);return this.signer.sign(s)}}function H(e){let t=new Ut("md5");return t.update(e),t.digest("hex")}class st{data;modified;new=!0;length=0;constructor(e={}){if(this.modified=!0,this.data={},this.length=K(e),this.length)this.new=!1;D(this.data,e)}set(e,t,s){if(!this.readonly&&e.data[t]!=s){if(this.modified=!0,!(t in e.data))this.length++;return e.data[t]=s,!0}return!1}get(e,t){if(t in e)return e[t];return e.data[t]}has(e,t){if(t in e.data)return!0;return!1}deleteProperty(e,t){if(!this.readonly&&t in e.data)this.modified=!0,delete e.data[t],this.length--;return!0}}class d extends st{sid;modified;readOnly;constructor(e="",t={},s=!1){super(t);this.sid=e;this.modified=!1,this.readOnly=s}get session(){return new Proxy(this,this)}}var rt=(e,t="",s={})=>{let{maxAge:r,expires:i,path:n,domain:a,secure:o,httpOnly:c,sameSite:h}=s,l=r instanceof Date?Math.floor(r.getTime()/1000):typeof r==="number"?r:void 0,u;if(i instanceof Date)u=i.toUTCString();else if(typeof i==="number")u=new Date(i).toUTCString();else if(typeof i==="string")u=i;else if(i===0)u=new Date().toUTCString();let g=[];if(a)g.push(`Domain=${a}`);if(u)g.push(`Expires=${u}`);if(l!==void 0)g.push(`Max-Age=${l}`);if(o)g.push("Secure");if(c)g.push("HttpOnly");if(n!=null)g.push(`Path=${n}`);if(h!=null)g.push(`SameSite=${h}`);return[`${e}=${t}`,...g].join("; ")};class M extends C{config;constructor(e,t){super(t??"salty");this.config=e}async openSession(e,t){if(e&&this.signer.unsign(e))return await this.fetchSession(e,t);return this.new}async fetchSession(e,t){return this.new}async saveSession(e,t,s=!1){return}get new(){return new d(this.generate(),{}).session}get readonly(){return new d(this.generate(),{},!0).session}get getExpiration(){let e=new Date,t=this.config.LIFETIME;return e.setDate(e.getDate()+t).toString()}setCookie(e,t,s=""){let r=null,i={};if(this.config.COOKIE_SAMESITE)r=this.config.COOKIE_SAMESITE;if(s)r=s;if(t===0)i.maxAge=t.toString();else i.expires=t;return rt(this.config.COOKIE_NAME,e.sid,{domain:"",path:this.config.COOKIE_PATH,httpOnly:this.config.COOKIE_HTTPONLY,secure:this.config.COOKIE_SECURE,sameSite:r,...i})}async loadHeader(e,t){let s=async(i)=>{let n="";if(i)n=i.split(";").reduce((c,h)=>{let[l,u]=h.trim().split(/=(.*)/s);return c[l]=u,c},{}).session;let a=n;return await this.openSession(a,t)},r=e.headers;if(r){if("get"in r)return await s(r.get("cookie"));else if("cookie"in r)return await s(r.cookie)}return this.new}}var{file:it,gunzipSync:Gt,gzipSync:Xt,write:qt}=globalThis.Bun;import{promises as Zt}from"fs";class F{path;data;constructor(e){this.data=new Map,this.path=e+"/"}async init(e){let t=H(e),s=this.path+t,r=it(s);if(await r.exists()){let i=await r.arrayBuffer();try{let n=JSON.parse(A(Gt(i)));return n.f_timed=Date.now(),this.data.set(t,n),n}catch(n){}}return null}async checkLast(e){let t=new Date(e);if(t.setMinutes(t.getMinutes()+60),t.getTime()<Date.now())return!0;return!1}async get(e){if(e){let t=this.data.get(e);if(t==null)return await this.init(e);else{if(t&&"f_timed"in t){if(await this.checkLast(t.f_timed))return await this.init(e)}return t}}return null}async set(e,t){let s=H(e),r=this.path+s;tt(r,""),await qt(r,Xt(JSON.stringify(t))),t.f_timed=Date.now(),this.data.set(e,t)}async delete(e){let t=H(e);this.data.delete(t);let s=this.path+t;it(s).exists().then(async(r)=>{await Zt.unlink(s)}).catch()}}class Z extends d{}class v extends M{isJWT;cacher;side=Z;constructor(e,t=".sessions",s=!1){super(e);this.isJWT=s;this.cacher=new F(t)}life(e,t){let{LIFETIME:s,JWT_LIFETIME:r}=this.config;if(new p(t).timed({day:this.isJWT?r:s}).getTime()-new Date().getTime()>0)return!0;else return this.cacher.delete(e),!1}async fetchSession(e,t){let s=this.config.KEY_PREFIX+e,r=await this.cacher.get(s),i={};if(r){let n=!0;if("life"in r)n=this.life(s,r.life);i=n?JSON.parse(r.data):{}}return new this.side(e,i,t).session}async saveSession(e,t,s=!1){let r=(n)=>{if(t){let a=this.setCookie(e,n);if(t)t.set("Set-Cookie",a)}},i=this.config.KEY_PREFIX+e.sid;if(!e.length){if(!e.new&&(e.modified||s))this.cacher.delete(i),r(0);return}if(e.new&&e.modified){let n=new p().timed({day:this.config.LIFETIME}),a=JSON.stringify(e.data);await this.cacher.set(i,{data:a,life:p.now}),r(n)}return}}class Y{client;query;f_timed;data;key;constructor(e,t,s){this.query=s,this.key=t,this.f_timed=Date.now(),this.data=new Map,this.client=e}async init(e){let t=await this.client.query({text:this.query+` where ${this.key} = $1`,values:[e]});for(let[s,r]of this.data)if(!r)this.data.delete(s);if(t.rowCount){let s=t.rows[0];return s.f_timed=Date.now(),this.data.set(e,s),s}else return this.data.set(e,null),null}async checkLast(e){let t=new Date(e);if(t.setMinutes(t.getMinutes()+15),t.getTime()<Date.now())return!0;return!1}async get(e){if(e){let t=this.data.get(e);if(t==null)return await this.init(e);else{if(t&&"f_timed"in t){if(await this.checkLast(t.f_timed))return await this.init(e)}return t}}return null}async set(e){if(this.key in e)e.f_timed=Date.now(),this.data.set(e[this.key],e)}async delete(e){this.data.delete(e)}}class Q extends d{}class V extends M{sclass=Q;client;pgc;constructor(e,t){super(t);this.client=e,this.pgc=new Y(e,"sid","SELECT * FROM session")}async fetchSession(e){let t=this.config.KEY_PREFIX+e,s=await this.pgc.get(t),r={};if(s)r=JSON.parse(s.data);return new this.sclass(e,r).session}async saveSession(e,t,s,r=""){let i=this.config.KEY_PREFIX+e.sid;if(!Object.entries(e.data).length){if(e.modified||s){if(t){await this.client.query({text:"DELETE FROM session WHERE sid = $1",values:[i]}),await this.pgc.delete(i);let o=this.setCookie(e,0);t.header={"Set-Cookie":o}}}return}let n=new p().timed({day:this.config.LIFETIME}),a=JSON.stringify(e.data);if(t){let o=this.getExpiration;await this.client.query({text:"INSERT INTO session(sid, data, expiration) VALUES($1, $2, $3)",values:[i,a,o?o:null]}),await this.pgc.set({sid:i,data:a,expiration:o??"",life:p.now});let c=this.setCookie(e,n);t.header={"Set-Cookie":c}}}}var{CryptoHasher:Qt}=globalThis.Bun;import{sign as at,verify as Vt}from"jsonwebtoken";import{randomBytes as zt}from"crypto";class z extends C{salt;constructor(){super("salty_jwt");this.salt="salty_jwt"}sign(e){let t={issuer:this.salt};return at({data:e},k(),t)}get random(){let e={issuer:this.salt},t={data:es()};return at(t,k(),e)}jwt(){let e=this.generate();return new d(e).session}verify(e,t){try{let s=Vt(e,k());if(s){let{data:r,iat:i,iss:n}=s;if(n==this.salt)if(t){let{days:a,hours:o,minutes:c,seconds:h}=t,l=new Date(i*1000);if(a)l=new Date(l.setDate(l.getDate()+a));else if(o)l=new Date(l.setHours(l.getHours()+o));else if(c)l=new Date(l.setMinutes(l.getMinutes()+c));else if(h)l=new Date(l.setSeconds(l.getSeconds()+h));if(l.getTime()-Date.now()>0)return r}else return r}}catch(s){}return null}open(e,t){if(e){let s=this.verify(e,t);if(s)return new d(e,s,!0).session}return this.jwt()}save(e){let t=e.data;if("access_token"in t)delete t.access_token;return this.sign(t)}new(e){return this.sign(e)}}function es(e=64){return new Qt("sha256").update(zt(e)).digest("hex")}class ts{postgresClient;config={COOKIE_NAME:"session",COOKIE_DOMAIN:"127.0.0.1",COOKIE_PATH:"/",COOKIE_HTTPONLY:!0,COOKIE_SECURE:!0,REFRESH_EACH_REQUEST:!1,COOKIE_SAMESITE:"Strict",KEY_PREFIX:"session:",PERMANENT:!0,USE_SIGNER:!1,ID_LENGTH:32,FILE_THRESHOLD:500,LIFETIME:31,MAX_COOKIE_SIZE:4093,INTERFACE:"fs",STORAGE:".sessions",JWT_STORAGE:".jwt",JWT_LIFETIME:5};constructor({type:e="fs",dir:t}={}){e&&(this.config.INTERFACE=e),t&&this.initStorage(t)}initStorage(e){return this.config.STORAGE=e+"/"+this.config.STORAGE,this.config.JWT_STORAGE=e+"/"+this.config.JWT_STORAGE,this}get session(){if(this.config.INTERFACE==="postgres"&&this.postgresClient)return new V(this.postgresClient,this.config);return new v(this.config,this.config.STORAGE)}get jwt(){return new v(this.config,this.config.JWT_STORAGE)}}class ss{init(e){this.jwtInt=new z,this.session=e.session,this.jwt=e.jwt}}export{ss as Session,d as ServerSide,Q as PostgreSession,V as PGInterface,Y as PGCache,Z as FSession,v as FSInterface,F as FSCached,M as AuthInterface,ts as Auth};
|
package/package.json
CHANGED
|
@@ -1,11 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "authored",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "authored",
|
|
5
5
|
"author": "Marky <markjotep@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
7
|
-
"
|
|
8
|
-
"
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"util": "cp -r ../misc/util ./",
|
|
14
|
+
"dirs": "for dir in ./src ./dist ./test ./util ./types; do mkdir -p $dir; done",
|
|
15
|
+
"@": "ln -sf ../../misc/@/ ./src",
|
|
16
|
+
"ts": "ln -sf ../misc/tsconfig.json ./;",
|
|
17
|
+
"init": "bun run dirs && bun run util && bun run @ && bun run ts;",
|
|
18
|
+
"build": "rm -r ./types ; tsc && bun build ./src/index.ts --outdir ./dist --minify --packages external --target bun && rollup -c ./util/rollup.config.js"
|
|
19
|
+
},
|
|
9
20
|
"keywords": [
|
|
10
21
|
"ts",
|
|
11
22
|
"bun"
|
|
@@ -18,19 +29,19 @@
|
|
|
18
29
|
"url": "https://github.com/MARKjotep/authored/issues"
|
|
19
30
|
},
|
|
20
31
|
"homepage": "https://github.com/MARKjotep/authored#readme",
|
|
21
|
-
"scripts": {
|
|
22
|
-
"build": "tsc ; dist=../../DIST/authored/ ; cp -r package.json $dist ; cp -r ./types/types.d.ts $dist/types; bun install --cwd $dist; "
|
|
23
|
-
},
|
|
24
32
|
"dependencies": {
|
|
25
|
-
"jsonwebtoken": "^9.0.
|
|
26
|
-
"pg": "^8.
|
|
33
|
+
"jsonwebtoken": "^9.0.3",
|
|
34
|
+
"pg": "^8.17.2"
|
|
27
35
|
},
|
|
28
36
|
"devDependencies": {
|
|
29
|
-
"@types/jsonwebtoken": "^9.0.
|
|
30
|
-
"@types/pg": "^8.
|
|
37
|
+
"@types/jsonwebtoken": "^9.0.10",
|
|
38
|
+
"@types/pg": "^8.16.0",
|
|
39
|
+
"@coff-r/oven": "link:@coff-r/oven",
|
|
40
|
+
"@coff-r/x": "link:@coff-r/x"
|
|
31
41
|
},
|
|
32
42
|
"peerDependencies": {
|
|
33
|
-
"typescript": "^5.
|
|
43
|
+
"typescript": "^5.8.3"
|
|
34
44
|
},
|
|
35
|
-
"bun": true
|
|
45
|
+
"bun": true,
|
|
46
|
+
"type": "module"
|
|
36
47
|
}
|
package/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{randomBytes as T}from"node:crypto";import{CryptoHasher as k,file as H,gunzipSync as c,gzipSync as o,write as C}from"bun";import{promises as h}from"node:fs";import{sign as b,verify as p}from"jsonwebtoken";import{CryptoHasher as x,file as O}from"bun";import{mkdirSync as S,writeFileSync as m}from"node:fs";var f=new TextDecoder,E={bool:(Y)=>typeof Y==="boolean",str:(Y)=>typeof Y==="string",arr:(Y)=>Array.isArray(Y),file:async(Y,Z)=>{try{m(Y,Z??"",{flag:"wx"})}catch($){}return!0},dir:(Y)=>{return S(Y,{recursive:!0}),!0},number:(Y)=>{return!isNaN(parseFloat(Y))&&isFinite(Y)},dict:(Y)=>{return typeof Y==="object"&&Y!==null&&!Array.isArray(Y)},arraybuff:(Y)=>{return Y instanceof Uint8Array||Y instanceof ArrayBuffer||typeof Y==="string"}};var z={vals:Object.values,keys:Object.keys,items:Object.entries,has:Object.hasOwn,define:Object.defineProperty,ass:Object.assign,length:(Y)=>{return Object.keys(Y).length}},g={numSequence:(Y)=>Array.from({length:Y},(Z,$)=>$)},B={charU:"ABCDEFGHIJKLMNOPQRSTUVWXYZ",charL:"abcdefghijklmnopqrstuvwxyz",nums:g.numSequence(9).join(""),rbytes:new RegExp(/(\d+)(\d*)/,"m"),strip:(Y,Z)=>Y.replace(new RegExp(`^${Z}|${Z}\$`,"g"),""),decode(Y){return f.decode(Y)},buffer(Y){return Buffer.from(Y)},digest(...Y){let Z=new Bun.CryptoHasher("sha256",L.secret());return Y.forEach(($)=>{Z.update($)}),Z.digest()},stringify(Y){return JSON.stringify(Y)},parse(Y){return JSON.parse(Y)}};class R{date;constructor(Y){this.date=Y?new Date(Y):new Date}delta(Y=null,Z=!1){let $=R.delta(this.date.getTime(),Y);return Z?new Date($):$}timed(Y){let Z=this.date.getTime(),$=this.date;if(Y){let{year:U,month:W,day:K,hour:Q,minute:q,second:P}=Y;if(U)$=new Date($.setFullYear($.getFullYear()+U));if(W)$=new Date($.setMonth($.getMonth()+W));if(K)$=new Date($.setDate($.getDate()+K));if(Q)$=new Date($.setHours($.getHours()+Q));if(q)$=new Date($.setMinutes($.getMinutes()+q));if(P)$=new Date($.setSeconds($.getSeconds()+P))}return $}static delta(Y,Z=null){if(Z)return Z-Y;else return Y-Date.now()}static get now(){return Date.now()}}var L={ok:12,secret:()=>{let Y=process.env.SECRET_KEY;if(!Y)throw new Error("'SECRET_KEY' not found in .env file");return Y},tls:(Y)=>{return z.items(process.env).filter((Z)=>{if(Z[0].startsWith("TLS_"))return Z}).reduce((Z,$)=>{let[U,W]=$;if(W){let K=U.replace("TLS_","").toLowerCase();Z[K]=O(Y+"/"+W)}return Z},{})},byteRange:(Y,Z)=>{let $=0,U=Y-1,[W,K]=Z.replace(/bytes=/,"").split("-");return $=parseInt(W,10),U=K?parseInt(K,10):U,[$,U,Y]},mimeType:(Y)=>{return O(Y).type},args:(Y,Z)=>{return Y.reduce(($,U,W)=>{return $[U]=Z[W],$},{})}},M={attr:(Y)=>{return z.items(Y).reduce((Z,[$,U])=>{return Z.push(E.bool(U)?$:`${$}="${U}"`),Z},[""]).join(" ")},head:(Y)=>{if(Y)return z.items(Y).reduce((Z,[$,U])=>{if(E.str(U))Z.push(`<${$}>${U}</${$}>`);else if(E.arr(U)){let W=U.reduce((K,Q)=>{let q="";if($=="script"){let P="";if("importmap"in Q)Q.type="importmap",P=JSON.stringify(Q.importmap),delete Q.importmap;else if("body"in Q)P=Q.body,delete Q.body;q=`${P}</${$}>`}return K.push(`<${$}${M.attr(Q)}>${q}`),K},[]);Z.push(...W)}return Z},[]);return[]},cookie:(Y,Z="",{maxAge:$,expires:U,path:W,domain:K,secure:Q,httpOnly:q,sameSite:P})=>{if($ instanceof Date)$=$.getSeconds();if(U instanceof Date)U=U.toUTCString();else if(U===0)U=new Date().toUTCString();return[["Domain",K],["Expires",U],["Max-Age",$],["Secure",Q],["HttpOnly",q],["Path",W],["SameSite",P]].reduce((j,[G,I])=>{if(I!==void 0)j.push(`${G}=${I}`);return j},[`${Y}=${Z}`]).join("; ")},html:(Y,Z,$)=>{let U=y.ID(5),W=Z,K=`<!DOCTYPE html><html lang="${$}">`;return K+=`<head>${W}</head>`,K+=`<body id="${U}">${Y}</body>`,K+="</html>",K}};function V(Y){let Z=new x("md5");return Z.update(Y),Z.digest("hex")}var y={ID:(Y)=>{let{charU:Z,charL:$,nums:U}=B,W=Z+$,K="",Q=0;while(Q<Y){let q=W+(Q==0?"":U),P=q.length;K+=q.charAt(Math.floor(Math.random()*P)),Q+=1}return K}};function n(Y=64){return new k("sha256").update(T(Y)).digest("hex")}class v{postgresClient;config={COOKIE_NAME:"session",COOKIE_DOMAIN:"127.0.0.1",COOKIE_PATH:"/",COOKIE_HTTPONLY:!0,COOKIE_SECURE:!0,REFRESH_EACH_REQUEST:!1,COOKIE_SAMESITE:"Strict",KEY_PREFIX:"session:",PERMANENT:!0,USE_SIGNER:!1,ID_LENGTH:32,FILE_THRESHOLD:500,LIFETIME:31,MAX_COOKIE_SIZE:4093,INTERFACE:"fs",STORAGE:".sessions",JWT_STORAGE:".jwt",JWT_LIFETIME:5};constructor({type:Y="fs",dir:Z}={}){Y&&(this.config.INTERFACE=Y),Z&&this.initStorage(Z)}initStorage(Y){return this.config.STORAGE=Y+"/"+this.config.STORAGE,this.config.JWT_STORAGE=Y+"/"+this.config.JWT_STORAGE,this}get session(){return new A(this.config,this.config.STORAGE)}get jwt(){return new A(this.config,this.config.JWT_STORAGE)}}class F{data;modified;new=!0;length=0;constructor(Y={}){if(this.modified=!0,this.data={},this.length=z.length(Y),this.length)this.new=!1;z.ass(this.data,Y)}set(Y,Z,$){if(!this.readonly&&Y.data[Z]!=$){if(this.modified=!0,!(Z in Y.data))this.length++;return Y.data[Z]=$,!0}return!1}get(Y,Z){if(Z in Y)return Y[Z];return Y.data[Z]}has(Y,Z){if(Z in Y.data)return!0;return!1}deleteProperty(Y,Z){if(!this.readonly&&Z in Y.data)this.modified=!0,delete Y.data[Z],this.length--;return!0}}class N extends F{Y;modified;readOnly;constructor(Y="",Z={},$=!1){super(Z);this.sid=Y;this.modified=!1,this.readOnly=$}get session(){return new Proxy(this,this)}}class _{Y;constructor(Y){this.salt=Y}getSignature(Y){let Z=this.deriveKey().toString();return B.digest(Z,Y).toString("base64")}deriveKey(){return B.digest(this.salt)}sign(Y){let Z=this.getSignature(Y),$=B.buffer(Y+"."+Z);return B.decode($)}unsign(Y){if(!(Y.indexOf(".")>-1))throw Error("No sep found");let Z=Y.indexOf("."),$=Y.slice(0,Z),U=Y.slice(Z+1);return this.verifySignature($,U)}loadUnsign(Y){if(this.unsign(Y)){let Z=B.buffer(Y),$=B.buffer(".").toString()[0];if(!($ in Z))throw Error("No sep found");let U=Z.indexOf($),W=Z.subarray(0,U);return Buffer.from(W.toString(),"base64").toString("utf-8")}}verifySignature(Y,Z){return this.getSignature(Y)==Z?!0:!1}}class X{signer;constructor(Y){this.signer=new _(Y)}generate(Y=21){let $=T(Y).toString("base64");if($.endsWith("="))$=$.slice(0,-1);return this.signer.sign($)}}class w extends X{Y;constructor(Y,Z){super(Z??"salty");this.config=Y}async openSession(Y,Z){if(Y&&this.signer.unsign(Y))return await this.fetchSession(Y,Z);return this.new}async fetchSession(Y,Z){return this.new}async saveSession(Y,Z,$=!1){return}get new(){return new N(this.generate(),{}).session}get readonly(){return new N(this.generate(),{},!0).session}get getExpiration(){let Y=new Date,Z=this.config.LIFETIME;return Y.setDate(Y.getDate()+Z).toString()}setCookie(Y,Z,$=""){let U=null,W={};if(this.config.COOKIE_SAMESITE)U=this.config.COOKIE_SAMESITE;if($)U=$;if(Z===0)W.maxAge=Z.toString();else W.expires=Z;return M.cookie(this.config.COOKIE_NAME,Y.sid,{domain:"",path:this.config.COOKIE_PATH,httpOnly:this.config.COOKIE_HTTPONLY,secure:this.config.COOKIE_SECURE,sameSite:U,...W})}async loadHeader(Y,Z){let $=async(W)=>{let K="";if(W)K=W.split(";").reduce((P,J)=>{let[j,G]=J.trim().split(/=(.*)/s);return P[j]=G,P},{}).session;let Q=K;return await this.openSession(Q,Z)},U=Y.headers;if(U){if("get"in U)return await $(U.get("cookie"));else if("cookie"in U)return await $(U.cookie)}return this.new}}class u extends N{}class D{path;data;constructor(Y){this.data=new Map,this.path=Y+"/"}async init(Y){let Z=V(Y),$=this.path+Z,U=H($);if(await U.exists()){let W=await U.arrayBuffer();try{let K=JSON.parse(B.decode(c(W)));return K.f_timed=Date.now(),this.data.set(Z,K),K}catch(K){}}return null}async checkLast(Y){let Z=new Date(Y);if(Z.setMinutes(Z.getMinutes()+60),Z.getTime()<Date.now())return!0;return!1}async get(Y){if(Y){let Z=this.data.get(Y);if(Z==null)return await this.init(Y);else{if(Z&&"f_timed"in Z){if(await this.checkLast(Z.f_timed))return await this.init(Y)}return Z}}return null}async set(Y,Z){let $=V(Y),U=this.path+$;await E.file(U,""),await C(U,o(JSON.stringify(Z))),Z.f_timed=Date.now(),this.data.set(Y,Z)}async delete(Y){let Z=V(Y);this.data.delete(Z);let $=this.path+Z;H($).exists().then(async(U)=>{await h.unlink($)}).catch()}}class A extends w{$;cacher;side=u;constructor(Y,Z=".sessions",$=!1){super(Y);this.isJWT=$;this.cacher=new D(Z)}life(Y,Z){let{LIFETIME:$,JWT_LIFETIME:U}=this.config;if(new R(Z).timed({day:this.isJWT?U:$}).getTime()-new Date().getTime()>0)return!0;else return this.cacher.delete(Y),!1}async fetchSession(Y,Z){let $=this.config.KEY_PREFIX+Y,U=await this.cacher.get($),W={};if(U){let K=!0;if("life"in U)K=this.life($,U.life);W=K?JSON.parse(U.data):{}}return new this.side(Y,W,Z).session}async saveSession(Y,Z,$=!1){let U=(K)=>{if(Z){let Q=this.setCookie(Y,K);if(Z)Z.set("Set-Cookie",Q)}},W=this.config.KEY_PREFIX+Y.sid;if(!Y.length){if(!Y.new&&(Y.modified||$))this.cacher.delete(W),U(0);return}if(Y.new&&Y.modified){let K=new R().timed({day:this.config.LIFETIME}),Q=JSON.stringify(Y.data);await this.cacher.set(W,{data:Q,life:R.now}),U(K)}return}}class l{client;query;f_timed;data;key;constructor(Y,Z,$){this.query=$,this.key=Z,this.f_timed=Date.now(),this.data=new Map,this.client=Y}async init(Y){let Z=await this.client.query({text:this.query+` where ${this.key} = \$1`,values:[Y]});for(let[$,U]of this.data)if(!U)this.data.delete($);if(Z.rowCount){let $=Z.rows[0];return $.f_timed=Date.now(),this.data.set(Y,$),$}else return this.data.set(Y,null),null}async checkLast(Y){let Z=new Date(Y);if(Z.setMinutes(Z.getMinutes()+15),Z.getTime()<Date.now())return!0;return!1}async get(Y){if(Y){let Z=this.data.get(Y);if(Z==null)return await this.init(Y);else{if(Z&&"f_timed"in Z){if(await this.checkLast(Z.f_timed))return await this.init(Y)}return Z}}return null}async set(Y){if(this.key in Y)Y.f_timed=Date.now(),this.data.set(Y[this.key],Y)}async delete(Y){this.data.delete(Y)}}class i extends X{salt;constructor(){super("salty_jwt");this.salt="salty_jwt"}sign(Y){let Z={issuer:this.salt};return b({data:Y},L.secret(),Z)}get random(){let Y={issuer:this.salt},Z={data:n()};return b(Z,L.secret(),Y)}jwt(){let Y=this.generate();return new N(Y).session}verify(Y,Z){try{let $=p(Y,L.secret());if($){let{data:U,iat:W,iss:K}=$;if(K==this.salt)if(Z){let{days:Q,hours:q,minutes:P,seconds:J}=Z,j=new Date(W*1000);if(Q)j=new Date(j.setDate(j.getDate()+Q));else if(q)j=new Date(j.setHours(j.getHours()+q));else if(P)j=new Date(j.setMinutes(j.getMinutes()+P));else if(J)j=new Date(j.setSeconds(j.getSeconds()+J));if(j.getTime()-Date.now()>0)return U}else return U}}catch($){}return null}open(Y,Z){if(Y){let $=this.verify(Y,Z);if($)return new N(Y,$,!0).session}return this.jwt()}save(Y){let Z=Y.data;if("access_token"in Z)delete Z.access_token;return this.sign(Z)}new(Y){return this.sign(Y)}}class d{fs;f_timed;data;key;dir;constructor({dir:Y,fs:Z,key:$}){this.dir=Y+"/ffs",this.key=$,this.f_timed=Date.now(),this.data=new Map,this.fs=this.dir+`/${Z}.json`}async init(){if(E.dir(this.dir)&&await E.file(this.fs,"{}"))H(this.fs).text().then((Y)=>{let Z=JSON.parse(Y);this.data=new Map(z.items(Z))}).catch((Y)=>{})}async get(Y){let Z=this.data.get(Y);if(Z)return Z;return null}async set(Y){if(this.key in Y){let Z=await H(this.fs).text();if(Z){let $=JSON.parse(Z),U=Y[this.key];$[U]=Y,await C(this.fs,JSON.stringify($))}this.data.set(Y[this.key],Y)}}async delete(Y){if(await this.get(Y)){let Z=await H(this.fs).text();if(Z){let $=JSON.parse(Z.toString());if(Y in $)delete $[Y],await C(this.fs,JSON.stringify($));this.data.delete(Y)}}}async json(){let Y=await H(this.fs).text(),Z=JSON.parse(Y);return z.vals(Z)}}export{N as ServerSide,l as PGCache,i as JWTInterface,d as Fjson,u as FSession,w as AuthInterface,v as Auth};
|
package/jsconfig.json
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
// Enable latest features
|
|
4
|
-
"lib": ["ESNext", "DOM"],
|
|
5
|
-
"target": "ESNext",
|
|
6
|
-
"module": "ESNext",
|
|
7
|
-
"moduleDetection": "force",
|
|
8
|
-
"jsx": "react-jsx",
|
|
9
|
-
"allowJs": true,
|
|
10
|
-
|
|
11
|
-
// Bundler mode
|
|
12
|
-
"moduleResolution": "bundler",
|
|
13
|
-
"allowImportingTsExtensions": true,
|
|
14
|
-
"verbatimModuleSyntax": true,
|
|
15
|
-
"noEmit": true,
|
|
16
|
-
|
|
17
|
-
// Best practices
|
|
18
|
-
"strict": true,
|
|
19
|
-
"skipLibCheck": true,
|
|
20
|
-
"noFallthroughCasesInSwitch": true,
|
|
21
|
-
|
|
22
|
-
// Some stricter flags (disabled by default)
|
|
23
|
-
"noUnusedLocals": false,
|
|
24
|
-
"noUnusedParameters": false,
|
|
25
|
-
"noPropertyAccessFromIndexSignature": false
|
|
26
|
-
}
|
|
27
|
-
}
|