@shellicar/core-config 1.0.1 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -4
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +44 -43
- package/dist/index.d.ts +44 -43
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +16 -14
package/README.md
CHANGED
|
@@ -16,11 +16,13 @@ pnpm add @shellicar/core-config
|
|
|
16
16
|
```
|
|
17
17
|
|
|
18
18
|
```ts
|
|
19
|
-
import {
|
|
19
|
+
import { createFactory } from '@shellicar/core-config';
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
console.log(
|
|
21
|
+
const factory = createFactory();
|
|
22
|
+
|
|
23
|
+
console.log(factory.string('myPassword123'));
|
|
24
|
+
console.log(factory.connectionString('Server=myserver.uri;Password=myPassword123'));
|
|
25
|
+
console.log(factory.url(new URL('http://myuser:myPassword123@myserver.uri')));
|
|
24
26
|
```
|
|
25
27
|
|
|
26
28
|
## Documentation
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var e=require("crypto");function t(e){return e&&e.__esModule?e:{default:e}}var r=t(require("util")),s=Object.defineProperty,n=(e,t)=>s(e,"name",{value:t,configurable:!0}),a=class{static{n(this,"EncryptedValue")}encryptedData;iv;authTag;key;constructor(t){this.key=e.randomBytes(32),this.iv=e.randomBytes(16);const r=e.createCipheriv("aes-256-gcm",this.key,this.iv),s=[];s.push(r.update(t,"utf8")),s.push(r.final()),this.encryptedData=Buffer.concat(s),this.authTag=r.getAuthTag()}getValue(){const t=e.createDecipheriv("aes-256-gcm",this.key,this.iv);t.setAuthTag(this.authTag);try{const e=[];return e.push(t.update(this.encryptedData)),e.push(t.final()),Buffer.concat(e).toString("utf8")}catch(e){throw new Error("Failed to decrypt value: Invalid key or corrupted data")}}toString(){return"[EncryptedValue]"}toJSON(){return{type:"EncryptedValue",encrypted:!0}}},c={sha256:n((()=>({hash:n((t=>e.createHash("sha256").update(t).digest("hex")),"hash"),prefix:"sha256"})),"sha256"),hs256:n((t=>({hash:n((r=>e.createHmac("sha256",t).update(r).digest("hex")),"hash"),prefix:"hs256"})),"hs256")},i=n((e=>null==e?c.sha256():c.hs256(e)),"getAlgorithm"),o=n(((e,t)=>{const r=i(t),s=r.hash(e);return`${r.prefix}:${s}`}),"hash"),u=class{static{n(this,"ISecureString")}},h=class{static{n(this,"ISecureConnectionString")}},l=class{static{n(this,"ISecureURL")}},p=class e extends u{static{n(this,"SecureString")}#e;#t;get secretValue(){return this.#e.getValue()}constructor(e,t){super(),this.#e=t.encryptionProvider.encrypt(e),this.#t=o(e,t.secret)}static factory(t){return r=>e.from(r,t)}static from(t,r){return null===t?null:void 0!==t?new e(t,r):void 0}toString(){return this.#t}toJSON(){return this.toString()}[r.default.inspect.custom](e,t,r){if(e<0)return"[SecureString]";const s=Object.assign({},t,{depth:null==t.depth?null:t.depth-1});return r(this.toJSON(),s)}},d=class e extends h{static{n(this,"SecureConnectionString")}#e;#r;get secretValue(){return this.#e.getValue()}constructor(e,t){super(),this.#e=t.encryptionProvider.encrypt(e),this.#r=this.parseConnectionString(e,t.secretKeys.map((e=>e.toLocaleLowerCase())),t)}parseConnectionString(e,t,r){return e.split(";").filter(Boolean).map((e=>{const[s,n]=e.split("="),a=n??"",c=t.includes(s.toLocaleLowerCase())?p.from(a,r):a;return[s,c]}))}static factory(t){return r=>e.from(r,t)}static from(t,r){return null===t?null:void 0!==t?new e(t,r):void 0}toString(){return this.#r.map((([e,t])=>`${e}=${t}`)).join(";")}toJSON(){return Object.fromEntries(this.#r)}[r.default.inspect.custom](e,t,r){if(e<0)return"[SecureConnectionString]";const s=Object.assign({},t,{depth:null==t.depth?null:t.depth-1});return r(this.toJSON(),s)}},y=class e extends l{static{n(this,"SecureURL")}#e;#s;get secretValue(){return new URL(this.#e.getValue())}constructor(e,t){super(),this.#e=t.encryptionProvider.encrypt(e.href),this.#s=p.from(e.password||null,t)}static factory(t){return r=>e.from(r,t)}static from(t,r){return null===t?null:void 0!==t?new e(t,r):void 0}toString(){const e=this.secretValue,t=new URL(e.href);return null!==this.#s&&(t.password=this.#s.toString()),t.href}toJSON(){const e=this.secretValue,t=new URL(e.href);t.password="";const r=new URLSearchParams(t.searchParams);let s;t.search="",null!==this.#s&&(s=this.#s.toString());const n={href:t.href};return void 0!==s&&(n.password=s),r.size>0&&(n.searchParams=Object.fromEntries(r)),n}[r.default.inspect.custom](e,t,r){if(e<0)return"[SecureURL]";const s=Object.assign({},t,{depth:null==t.depth?null:t.depth-1});return r(this.toJSON(),s)}},f=["AccessKey","SharedAccessKey","Password","AccountKey","Secret","SecretKey","ApiKey","Token","Key","MasterKey","PrimaryKey","SecondaryKey"],S={encrypt:n((e=>new a(e)),"encrypt")},g=n((e=>{const t={secret:e?.secret??null,encryptionProvider:e?.encryptionProvider??S,secretKeys:e?.secretKeys??f};return{string:p.factory(t),connectionString:d.factory(t),url:y.factory(t)}}),"createFactory");exports.EncryptedValue=a,exports.ISecureConnectionString=h,exports.ISecureString=u,exports.ISecureURL=l,exports.createFactory=g,exports.defaultSecureKeys=f;//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/hash.ts","../src/types.ts","../src/SecureString.ts","../src/defaults.ts","../src/SecureConnectionString.ts","../src/SecureURL.ts","../src/createFactory.ts"],"names":["createHash","createHmac","hash","util","value"],"mappings":";;AAOA,IAAM,UAAa,GAAA;AAAA,EACjB,wBAAe,MAAA,CAAA,OAAA;AAAA,IACb,IAAA,kBAAO,MAAA,CAAA,CAAA,CAAA,KAAcA,iBAAW,CAAA,QAAQ,CAAE,CAAA,MAAA,CAAO,CAAC,CAAA,CAAE,MAAO,CAAA,KAAK,CAA1D,EAAA,MAAA,CAAA;AAAA,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UAAU,QAAS,EAAA;AAAA;AAGrC,IAAA,MAAM,MAAoB,GAAA;AAAA,MACxB,MAAM,MAAO,CAAA;AAAA,KACf;AACA,IAAA,IAAI,aAAa,SAAW,EAAA;AAC1B,MAAA,MAAA,CAAO,QAAW,GAAA,QAAA;AAAA;AAEpB,IAAI,IAAA,YAAA,CAAa,OAAO,CAAG,EAAA;AACzB,MAAO,MAAA,CAAA,YAAA,GAAe,MAAO,CAAA,WAAA,CAAY,YAAY,CAAA;AAAA;AAEvD,IAAO,OAAA,MAAA;AAAA;AACT,EACA,CAAUA,qBAAK,CAAA,OAAA,CAAQ,MAAM,CAAE,CAAA,KAAA,EAAe,SAAyB,OAAkC,EAAA;AACvG,IAAA,IAAI,QAAQ,CAAG,EAAA;AACb,MAAO,OAAA,aAAA;AAAA;AAET,IAAA,MAAM,UAAa,GAAA,MAAA,CAAO,MAAO,CAAA,IAAI,OAAS,EAAA;AAAA,MAC5C,OAAO,OAAQ,CAAA,KAAA,IAAS,IAAO,GAAA,IAAA,GAAO,QAAQ,KAAQ,GAAA;AAAA,KACvD,CAAA;AACD,IAAA,OAAO,OAAQ,CAAA,IAAA,CAAK,MAAO,EAAA,EAAG,UAAU,CAAA;AAAA;AAE5C;;;AC5Ea,IAAA,aAAA,2BAAiB,MAAmC,KAAA;AAC/D,EAAO,OAAA;AAAA,IACL,MAAA,EAAQ,YAAa,CAAA,OAAA,CAAQ,MAAM,CAAA;AAAA,IACnC,gBAAA,EAAkB,sBAAuB,CAAA,OAAA,CAAQ,MAAM,CAAA;AAAA,IACvD,GAAA,EAAK,SAAU,CAAA,OAAA,CAAQ,MAAM;AAAA,GAC/B;AACF,CAN6B,EAAA,eAAA","file":"index.cjs","sourcesContent":["import { createHash, createHmac } from 'node:crypto';\n\ntype HashAlgorithm = {\n hash: (x: string) => string;\n prefix: string;\n};\n\nconst algorithms = {\n sha256: () => ({\n hash: (x: string) => createHash('sha256').update(x).digest('hex'),\n prefix: 'sha256',\n }),\n hs256: (secret: string) => ({\n hash: (x: string) => createHmac('sha256', secret).update(x).digest('hex'),\n prefix: 'hs256',\n }),\n} as const;\n\nconst getAlgorithm = (secret?: string): HashAlgorithm => {\n return secret === undefined ? algorithms.sha256() : algorithms.hs256(secret);\n};\n\nexport const hash = (input: string, secret?: string) => {\n const alg = getAlgorithm(secret);\n const hash = alg.hash(input);\n return `${alg.prefix}:${hash}`;\n};\n","import util, { type InspectOptions } from 'node:util';\nimport type { SecureConnectionString } from './SecureConnectionString';\nimport type { SecureString } from './SecureString';\nimport type { SecureURL } from './SecureURL';\n\nexport type InspectFunction = typeof util.inspect;\n\nexport abstract class BaseObject {\n public abstract toString(): string;\n public abstract toJSON(): string | object;\n public abstract [util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string;\n}\n\nexport type SecureFactory = {\n string: (value: string) => SecureString;\n connectionString: (value: string, secretKeys?: readonly string[]) => SecureConnectionString;\n url: (value: URL) => SecureURL;\n};\n\nexport type SecureKeys = readonly string[];\n","import util, { type InspectOptions } from 'node:util';\nimport { hash } from './hash';\nimport { BaseObject, type InspectFunction } from './types';\n\nexport class SecureString extends BaseObject {\n readonly #value: string;\n readonly #hash: string;\n\n public get secretValue(): string {\n return this.#value;\n }\n\n private constructor(value: string, secret?: string) {\n super();\n this.#value = value;\n this.#hash = hash(value, secret);\n }\n\n static factory(secret: string | undefined): (value: string) => SecureString {\n return (value: string) => SecureString.from(value, secret);\n }\n\n public static from<T extends string | null | undefined>(value: T, secret?: string): T extends string ? SecureString : T {\n if (value === null) {\n return null as T extends string ? SecureString : T;\n }\n if (value === undefined) {\n return undefined as T extends string ? SecureString : T;\n }\n return new SecureString(value, secret) as T extends string ? SecureString : T;\n }\n\n public override toString() {\n return this.#hash;\n }\n public override toJSON() {\n return this.toString();\n }\n override [util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string {\n if (depth < 0) {\n return '[SecureString]';\n }\n const newOptions = Object.assign({}, options, {\n depth: options.depth == null ? null : options.depth - 1,\n });\n return inspect(this.toJSON(), newOptions);\n }\n}\n","import type { SecureKeys } from './types';\n\nexport const defaultSecureKeys = ['AccessKey', 'SharedAccessKey', 'Password', 'AccountKey', 'Secret', 'SecretKey', 'ApiKey', 'Token', 'Key', 'MasterKey', 'PrimaryKey', 'SecondaryKey'] as const satisfies SecureKeys;\n","import util, { type InspectOptions } from 'node:util';\nimport { SecureString } from './SecureString';\nimport { defaultSecureKeys } from './defaults';\nimport { BaseObject, type InspectFunction, type SecureKeys } from './types';\n\nexport class SecureConnectionString extends BaseObject {\n readonly #value: string;\n readonly #data: [string, string | SecureString][];\n\n public get secretValue(): string {\n return this.#value;\n }\n\n private constructor(value: string, secretKeys: SecureKeys, secret: string | undefined) {\n super();\n this.#value = value;\n this.#data = this.parseConnectionString(\n value,\n secretKeys.map((x) => x.toLocaleLowerCase()),\n secret,\n );\n }\n\n private parseConnectionString(value: string, secretKeys: SecureKeys, secret: string | undefined): [string, string | SecureString][] {\n return value\n .split(';')\n .filter(Boolean)\n .map((pair) => {\n const [key, value] = pair.split('=');\n const v = value ?? '';\n const val = secretKeys.includes(key.toLocaleLowerCase()) ? SecureString.from(v, secret) : v;\n return [key, val];\n });\n }\n\n static factory(secret: string | undefined, secretKeys?: SecureKeys): (value: string) => SecureConnectionString {\n return (value: string) => SecureConnectionString.from(value, secretKeys, secret);\n }\n\n public static from<T extends string | null | undefined>(value: T, secretKeys?: SecureKeys, secret?: string): T extends string ? SecureConnectionString : T {\n if (value === null) {\n return null as T extends string ? SecureConnectionString : T;\n }\n if (value === undefined) {\n return undefined as T extends string ? SecureConnectionString : T;\n }\n return new SecureConnectionString(value, secretKeys ?? defaultSecureKeys, secret) as T extends string ? SecureConnectionString : T;\n }\n\n public override toString(): string {\n return this.#data.map(([key, value]) => `${key}=${value}`).join(';');\n }\n\n public override toJSON(): object {\n return Object.fromEntries(this.#data);\n }\n\n public override [util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string {\n if (depth < 0) {\n return '[SecureConnectionString]';\n }\n const newOptions = Object.assign({}, options, {\n depth: options.depth == null ? null : options.depth - 1,\n });\n return inspect(this.toJSON(), newOptions);\n }\n}\n","import util, { type InspectOptions } from 'node:util';\nimport { SecureString } from './SecureString';\nimport { BaseObject, type InspectFunction } from './types';\n\ntype UrlObject = {\n href: string;\n password?: string;\n searchParams?: Record<string, string>;\n};\n\n/**\n * SecureURL obfuscates the password from the URL.\n */\nexport class SecureURL extends BaseObject {\n readonly #value: URL;\n readonly #password: SecureString | null;\n\n public get secretValue(): URL {\n return this.#value;\n }\n\n private constructor(value: URL, secret: string | undefined) {\n super();\n this.#value = value;\n this.#password = SecureString.from(value.password || null, secret);\n }\n\n static factory(secret: string | undefined): (value: URL) => SecureURL {\n return (value: URL) => SecureURL.from(value, secret);\n }\n\n public static from<T extends URL | null | undefined>(value: T, secret?: string): T extends URL ? SecureURL : T {\n if (value === null) {\n return null as T extends URL ? SecureURL : T;\n }\n if (value === undefined) {\n return undefined as T extends URL ? SecureURL : T;\n }\n return new SecureURL(value, secret) as T extends URL ? SecureURL : T;\n }\n\n override toString(): string {\n const newUrl = new URL(this.#value.href);\n if (this.#password !== null) {\n newUrl.password = this.#password.toString();\n }\n return newUrl.href;\n }\n\n override toJSON(): UrlObject {\n const newUrl = new URL(this.#value.href);\n newUrl.password = '';\n\n const searchParams = new URLSearchParams(newUrl.searchParams);\n newUrl.search = '';\n\n let password: string | undefined;\n if (this.#password !== null) {\n password = this.#password.toString();\n }\n\n const result: UrlObject = {\n href: newUrl.href,\n };\n if (password !== undefined) {\n result.password = password;\n }\n if (searchParams.size > 0) {\n result.searchParams = Object.fromEntries(searchParams);\n }\n return result;\n }\n override [util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string {\n if (depth < 0) {\n return '[SecureURL]';\n }\n const newOptions = Object.assign({}, options, {\n depth: options.depth == null ? null : options.depth - 1,\n });\n return inspect(this.toJSON(), newOptions);\n }\n}\n","import { SecureConnectionString } from './SecureConnectionString';\nimport { SecureString } from './SecureString';\nimport { SecureURL } from './SecureURL';\nimport type { SecureFactory } from './types';\n\nexport const createFactory = (secret?: string): SecureFactory => {\n return {\n string: SecureString.factory(secret),\n connectionString: SecureConnectionString.factory(secret),\n url: SecureURL.factory(secret),\n };\n};\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/EncryptedValue.ts","../src/hash.ts","../src/interfaces.ts","../src/SecureString.ts","../src/SecureConnectionString.ts","../src/SecureURL.ts","../src/defaults.ts","../src/createFactory.ts"],"names":["randomBytes","createCipheriv","createDecipheriv","createHash","createHmac","hash","util","value"],"mappings":";;AAEO,IAAM,iBAAN,MAAqB;AAAA,EAF5B;AAE4B,IAAA,MAAA,CAAA,IAAA,EAAA,gBAAA,CAAA;AAAA;AAAA,EACT,aAAA;AAAA,EACA,EAAA;AAAA,EACA,OAAA;AAAA,EACA,GAAA;AAAA,EAEV,YAAY,KAAe,EAAA;AAChC,IAAK,IAAA,CAAA,GAAA,GAAMA,mBAAY,EAAE,CAAA;AACzB,IAAK,IAAA,CAAA,EAAA,GAAKA,mBAAY,EAAE,CAAA;AAExB,IAAA,MAAM,SAASC,qBAAe,CAAA,aAAA,EAAe,IAAK,CAAA,GAAA,EAAK,KAAK,EAAE,CAAA;AAE9D,IAAA,MAAM,kBAA4B,EAAC;AACnC,IAAA,eAAA,CAAgB,IAAK,CAAA,MAAA,CAAO,MAAO,CAAA,KAAA,EAAO,MAAM,CAAC,CAAA;AACjD,IAAgB,eAAA,CAAA,IAAA,CAAK,MAAO,CAAA,KAAA,EAAO,CAAA;AAEnC,IAAK,IAAA,CAAA,aAAA,GAAgB,MAAO,CAAA,MAAA,CAAO,eAAe,CAAA;AAClD,IAAK,IAAA,CAAA,OAAA,GAAU,OAAO,UAAW,EAAA;AAAA;AACnC,EAEO,QAAmB,GAAA;AACxB,IAAA,MAAM,WAAWC,uBAAiB,CAAA,aAAA,EAAe,IAAK,CAAA,GAAA,EAAK,KAAK,EAAE,CAAA;AAClE,IAAS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AAM,CAAA;AAAA,IACnC,gBAAA,EAAkB,sBAAuB,CAAA,OAAA,CAAQ,MAAM,CAAA;AAAA,IACvD,GAAA,EAAK,SAAU,CAAA,OAAA,CAAQ,MAAM;AAAA,GAC/B;AACF,CAZ6B,EAAA,eAAA","file":"index.cjs","sourcesContent":["import { createCipheriv, createDecipheriv, randomBytes } from 'node:crypto';\n\nexport class EncryptedValue {\n private readonly encryptedData: Buffer;\n private readonly iv: Buffer;\n private readonly authTag: Buffer;\n private readonly key: Buffer;\n\n public constructor(value: string) {\n this.key = randomBytes(32);\n this.iv = randomBytes(16);\n\n const cipher = createCipheriv('aes-256-gcm', this.key, this.iv);\n\n const encryptedChunks: Buffer[] = [];\n encryptedChunks.push(cipher.update(value, 'utf8'));\n encryptedChunks.push(cipher.final());\n\n this.encryptedData = Buffer.concat(encryptedChunks);\n this.authTag = cipher.getAuthTag();\n }\n\n public getValue(): string {\n const decipher = createDecipheriv('aes-256-gcm', this.key, this.iv);\n decipher.setAuthTag(this.authTag);\n\n try {\n const decryptedChunks: Buffer[] = [];\n decryptedChunks.push(decipher.update(this.encryptedData));\n decryptedChunks.push(decipher.final());\n\n return Buffer.concat(decryptedChunks).toString('utf8');\n } catch (_) {\n // Hide internal error details\n throw new Error('Failed to decrypt value: Invalid key or corrupted data');\n }\n }\n\n public toString(): string {\n return '[EncryptedValue]';\n }\n\n public toJSON(): object {\n return { type: 'EncryptedValue', encrypted: true };\n }\n}\n","import { createHash, createHmac } from 'node:crypto';\n\ntype HashAlgorithm = {\n hash: (x: string) => string;\n prefix: string;\n};\n\nconst algorithms = {\n sha256: () => ({\n hash: (x: string) => createHash('sha256').update(x).digest('hex'),\n prefix: 'sha256',\n }),\n hs256: (secret: string) => ({\n hash: (x: string) => createHmac('sha256', secret).update(x).digest('hex'),\n prefix: 'hs256',\n }),\n} as const;\n\nconst getAlgorithm = (secret: string | null): HashAlgorithm => {\n return secret == null ? algorithms.sha256() : algorithms.hs256(secret);\n};\n\nexport const hash = (input: string, secret: string | null) => {\n const alg = getAlgorithm(secret);\n const hash = alg.hash(input);\n return `${alg.prefix}:${hash}`;\n};\n","import util, { type InspectOptions } from 'node:util';\nimport type { BaseObject, InspectFunction } from './types';\n\nexport abstract class ISecureString implements BaseObject {\n public abstract toString(): string;\n public abstract toJSON(): string | object;\n public abstract [util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string;\n public abstract get secretValue(): string;\n}\n\nexport abstract class ISecureConnectionString implements BaseObject {\n public abstract toString(): string;\n public abstract toJSON(): string | object;\n public abstract [util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string;\n public abstract get secretValue(): string;\n}\n\nexport abstract class ISecureURL implements BaseObject {\n public abstract toString(): string;\n public abstract toJSON(): object;\n public abstract [util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string;\n public abstract get secretValue(): URL;\n}\n\nexport abstract class ISecureFactory {\n public abstract string(value: string): ISecureString;\n public abstract connectionString(value: string, secretKeys?: readonly string[]): ISecureConnectionString;\n public abstract url(value: URL): ISecureURL;\n}\n","import util, { type InspectOptions } from 'node:util';\nimport { hash } from './hash';\nimport { ISecureString } from './interfaces';\nimport type { IEncryptedValue, InspectFunction, SecureConfig } from './types';\n\nexport class SecureString extends ISecureString {\n readonly #encryptedValue: IEncryptedValue;\n readonly #hash: string;\n\n public get secretValue(): string {\n return this.#encryptedValue.getValue();\n }\n\n private constructor(value: string, config: SecureConfig) {\n super();\n this.#encryptedValue = config.encryptionProvider.encrypt(value);\n this.#hash = hash(value, config.secret);\n }\n\n static factory(config: SecureConfig): (value: string) => SecureString {\n return (value: string) => SecureString.from(value, config);\n }\n\n public static from<T extends string | null | undefined>(value: T, config: SecureConfig): T extends string ? SecureString : T {\n if (value === null) {\n return null as T extends string ? SecureString : T;\n }\n if (value === undefined) {\n return undefined as T extends string ? SecureString : T;\n }\n return new SecureString(value, config) as T extends string ? SecureString : T;\n }\n\n public override toString() {\n return this.#hash;\n }\n public override toJSON() {\n return this.toString();\n }\n override [util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string {\n if (depth < 0) {\n return '[SecureString]';\n }\n const newOptions = Object.assign({}, options, {\n depth: options.depth == null ? null : options.depth - 1,\n });\n return inspect(this.toJSON(), newOptions);\n }\n}\n","import util, { type InspectOptions } from 'node:util';\nimport { SecureString } from './SecureString';\nimport { ISecureConnectionString } from './interfaces';\nimport type { IEncryptedValue, InspectFunction, SecureConfig, SecureKeys } from './types';\n\nexport class SecureConnectionString extends ISecureConnectionString {\n readonly #encryptedValue: IEncryptedValue;\n readonly #data: [string, string | SecureString][];\n\n public get secretValue(): string {\n return this.#encryptedValue.getValue();\n }\n\n private constructor(value: string, config: SecureConfig) {\n super();\n this.#encryptedValue = config.encryptionProvider.encrypt(value);\n this.#data = this.parseConnectionString(\n value,\n config.secretKeys.map((x) => x.toLocaleLowerCase()),\n config,\n );\n }\n\n private parseConnectionString(value: string, secretKeys: SecureKeys, config: SecureConfig): [string, string | SecureString][] {\n return value\n .split(';')\n .filter(Boolean)\n .map((pair) => {\n const [key, value] = pair.split('=');\n const v = value ?? '';\n const val = secretKeys.includes(key.toLocaleLowerCase()) ? SecureString.from(v, config) : v;\n return [key, val];\n });\n }\n\n static factory(config: SecureConfig): (value: string) => SecureConnectionString {\n return (value: string) => SecureConnectionString.from(value, config);\n }\n\n public static from<T extends string | null | undefined>(value: T, config: SecureConfig): T extends string ? SecureConnectionString : T {\n if (value === null) {\n return null as T extends string ? SecureConnectionString : T;\n }\n if (value === undefined) {\n return undefined as T extends string ? SecureConnectionString : T;\n }\n return new SecureConnectionString(value, config) as T extends string ? SecureConnectionString : T;\n }\n\n public override toString(): string {\n return this.#data.map(([key, value]) => `${key}=${value}`).join(';');\n }\n\n public override toJSON(): object {\n return Object.fromEntries(this.#data);\n }\n\n public override [util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string {\n if (depth < 0) {\n return '[SecureConnectionString]';\n }\n const newOptions = Object.assign({}, options, {\n depth: options.depth == null ? null : options.depth - 1,\n });\n return inspect(this.toJSON(), newOptions);\n }\n}\n","import util, { type InspectOptions } from 'node:util';\nimport { SecureString } from './SecureString';\nimport { ISecureURL } from './interfaces';\nimport type { IEncryptedValue, InspectFunction, SecureConfig } from './types';\n\ntype UrlObject = {\n href: string;\n password?: string;\n searchParams?: Record<string, string>;\n};\n\nexport class SecureURL extends ISecureURL {\n readonly #encryptedValue: IEncryptedValue;\n readonly #password: SecureString | null;\n\n public get secretValue(): URL {\n return new URL(this.#encryptedValue.getValue());\n }\n\n private constructor(value: URL, config: SecureConfig) {\n super();\n this.#encryptedValue = config.encryptionProvider.encrypt(value.href);\n this.#password = SecureString.from(value.password || null, config);\n }\n\n static factory(config: SecureConfig): (value: URL) => ISecureURL {\n return (value: URL) => SecureURL.from(value, config);\n }\n\n public static from<T extends URL | null | undefined>(value: T, config: SecureConfig): T extends URL ? SecureURL : T {\n if (value === null) {\n return null as T extends URL ? SecureURL : T;\n }\n if (value === undefined) {\n return undefined as T extends URL ? SecureURL : T;\n }\n return new SecureURL(value, config) as T extends URL ? SecureURL : T;\n }\n\n override toString(): string {\n const originalUrl = this.secretValue;\n const newUrl = new URL(originalUrl.href);\n if (this.#password !== null) {\n newUrl.password = this.#password.toString();\n }\n return newUrl.href;\n }\n\n override toJSON(): UrlObject {\n const originalUrl = this.secretValue;\n const newUrl = new URL(originalUrl.href);\n newUrl.password = '';\n\n const searchParams = new URLSearchParams(newUrl.searchParams);\n newUrl.search = '';\n\n let password: string | undefined;\n if (this.#password !== null) {\n password = this.#password.toString();\n }\n\n const result: UrlObject = {\n href: newUrl.href,\n };\n if (password !== undefined) {\n result.password = password;\n }\n if (searchParams.size > 0) {\n result.searchParams = Object.fromEntries(searchParams);\n }\n return result;\n }\n override [util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string {\n if (depth < 0) {\n return '[SecureURL]';\n }\n const newOptions = Object.assign({}, options, {\n depth: options.depth == null ? null : options.depth - 1,\n });\n return inspect(this.toJSON(), newOptions);\n }\n}\n","import type { SecureKeys } from './types';\n\nexport const defaultSecureKeys = ['AccessKey', 'SharedAccessKey', 'Password', 'AccountKey', 'Secret', 'SecretKey', 'ApiKey', 'Token', 'Key', 'MasterKey', 'PrimaryKey', 'SecondaryKey'] as const satisfies SecureKeys;\n","import { EncryptedValue } from './EncryptedValue';\nimport { SecureConnectionString } from './SecureConnectionString';\nimport { SecureString } from './SecureString';\nimport { SecureURL } from './SecureURL';\nimport { defaultSecureKeys } from './defaults';\nimport type { ISecureFactory } from './interfaces';\nimport type { IEncryptionProvider, SecureConfig } from './types';\n\nexport const defaultEncryptionProvider: IEncryptionProvider = {\n encrypt: (value: string) => new EncryptedValue(value),\n};\n\nexport const createFactory = (options?: Partial<SecureConfig>): ISecureFactory => {\n const config: SecureConfig = {\n secret: options?.secret ?? null,\n encryptionProvider: options?.encryptionProvider ?? defaultEncryptionProvider,\n secretKeys: options?.secretKeys ?? defaultSecureKeys,\n };\n\n return {\n string: SecureString.factory(config),\n connectionString: SecureConnectionString.factory(config),\n url: SecureURL.factory(config),\n };\n};\n"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,33 +1,14 @@
|
|
|
1
1
|
import util, { InspectOptions } from 'node:util';
|
|
2
2
|
|
|
3
|
-
declare class
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
private
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
declare class EncryptedValue {
|
|
4
|
+
private readonly encryptedData;
|
|
5
|
+
private readonly iv;
|
|
6
|
+
private readonly authTag;
|
|
7
|
+
private readonly key;
|
|
8
|
+
constructor(value: string);
|
|
9
|
+
getValue(): string;
|
|
9
10
|
toString(): string;
|
|
10
|
-
toJSON():
|
|
11
|
-
[util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
type UrlObject = {
|
|
15
|
-
href: string;
|
|
16
|
-
password?: string;
|
|
17
|
-
searchParams?: Record<string, string>;
|
|
18
|
-
};
|
|
19
|
-
/**
|
|
20
|
-
* SecureURL obfuscates the password from the URL.
|
|
21
|
-
*/
|
|
22
|
-
declare class SecureURL extends BaseObject {
|
|
23
|
-
#private;
|
|
24
|
-
get secretValue(): URL;
|
|
25
|
-
private constructor();
|
|
26
|
-
static factory(secret: string | undefined): (value: URL) => SecureURL;
|
|
27
|
-
static from<T extends URL | null | undefined>(value: T, secret?: string): T extends URL ? SecureURL : T;
|
|
28
|
-
toString(): string;
|
|
29
|
-
toJSON(): UrlObject;
|
|
30
|
-
[util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string;
|
|
11
|
+
toJSON(): object;
|
|
31
12
|
}
|
|
32
13
|
|
|
33
14
|
type InspectFunction = typeof util.inspect;
|
|
@@ -36,27 +17,47 @@ declare abstract class BaseObject {
|
|
|
36
17
|
abstract toJSON(): string | object;
|
|
37
18
|
abstract [util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string;
|
|
38
19
|
}
|
|
39
|
-
type SecureFactory = {
|
|
40
|
-
string: (value: string) => SecureString;
|
|
41
|
-
connectionString: (value: string, secretKeys?: readonly string[]) => SecureConnectionString;
|
|
42
|
-
url: (value: URL) => SecureURL;
|
|
43
|
-
};
|
|
44
20
|
type SecureKeys = readonly string[];
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
#private;
|
|
48
|
-
get secretValue(): string;
|
|
49
|
-
private constructor();
|
|
50
|
-
private parseConnectionString;
|
|
51
|
-
static factory(secret: string | undefined, secretKeys?: SecureKeys): (value: string) => SecureConnectionString;
|
|
52
|
-
static from<T extends string | null | undefined>(value: T, secretKeys?: SecureKeys, secret?: string): T extends string ? SecureConnectionString : T;
|
|
21
|
+
interface IEncryptedValue {
|
|
22
|
+
getValue(): string;
|
|
53
23
|
toString(): string;
|
|
54
24
|
toJSON(): object;
|
|
55
|
-
|
|
25
|
+
}
|
|
26
|
+
interface IEncryptionProvider {
|
|
27
|
+
encrypt(value: string): IEncryptedValue;
|
|
28
|
+
}
|
|
29
|
+
interface SecureConfig {
|
|
30
|
+
secret: string | null;
|
|
31
|
+
encryptionProvider: IEncryptionProvider;
|
|
32
|
+
secretKeys: SecureKeys;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
declare abstract class ISecureString implements BaseObject {
|
|
36
|
+
abstract toString(): string;
|
|
37
|
+
abstract toJSON(): string | object;
|
|
38
|
+
abstract [util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string;
|
|
39
|
+
abstract get secretValue(): string;
|
|
40
|
+
}
|
|
41
|
+
declare abstract class ISecureConnectionString implements BaseObject {
|
|
42
|
+
abstract toString(): string;
|
|
43
|
+
abstract toJSON(): string | object;
|
|
44
|
+
abstract [util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string;
|
|
45
|
+
abstract get secretValue(): string;
|
|
46
|
+
}
|
|
47
|
+
declare abstract class ISecureURL implements BaseObject {
|
|
48
|
+
abstract toString(): string;
|
|
49
|
+
abstract toJSON(): object;
|
|
50
|
+
abstract [util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string;
|
|
51
|
+
abstract get secretValue(): URL;
|
|
52
|
+
}
|
|
53
|
+
declare abstract class ISecureFactory {
|
|
54
|
+
abstract string(value: string): ISecureString;
|
|
55
|
+
abstract connectionString(value: string, secretKeys?: readonly string[]): ISecureConnectionString;
|
|
56
|
+
abstract url(value: URL): ISecureURL;
|
|
56
57
|
}
|
|
57
58
|
|
|
58
|
-
declare const createFactory: (
|
|
59
|
+
declare const createFactory: (options?: Partial<SecureConfig>) => ISecureFactory;
|
|
59
60
|
|
|
60
61
|
declare const defaultSecureKeys: readonly ["AccessKey", "SharedAccessKey", "Password", "AccountKey", "Secret", "SecretKey", "ApiKey", "Token", "Key", "MasterKey", "PrimaryKey", "SecondaryKey"];
|
|
61
62
|
|
|
62
|
-
export {
|
|
63
|
+
export { EncryptedValue, ISecureConnectionString, ISecureString, ISecureURL, createFactory, defaultSecureKeys };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,33 +1,14 @@
|
|
|
1
1
|
import util, { InspectOptions } from 'node:util';
|
|
2
2
|
|
|
3
|
-
declare class
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
private
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
declare class EncryptedValue {
|
|
4
|
+
private readonly encryptedData;
|
|
5
|
+
private readonly iv;
|
|
6
|
+
private readonly authTag;
|
|
7
|
+
private readonly key;
|
|
8
|
+
constructor(value: string);
|
|
9
|
+
getValue(): string;
|
|
9
10
|
toString(): string;
|
|
10
|
-
toJSON():
|
|
11
|
-
[util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
type UrlObject = {
|
|
15
|
-
href: string;
|
|
16
|
-
password?: string;
|
|
17
|
-
searchParams?: Record<string, string>;
|
|
18
|
-
};
|
|
19
|
-
/**
|
|
20
|
-
* SecureURL obfuscates the password from the URL.
|
|
21
|
-
*/
|
|
22
|
-
declare class SecureURL extends BaseObject {
|
|
23
|
-
#private;
|
|
24
|
-
get secretValue(): URL;
|
|
25
|
-
private constructor();
|
|
26
|
-
static factory(secret: string | undefined): (value: URL) => SecureURL;
|
|
27
|
-
static from<T extends URL | null | undefined>(value: T, secret?: string): T extends URL ? SecureURL : T;
|
|
28
|
-
toString(): string;
|
|
29
|
-
toJSON(): UrlObject;
|
|
30
|
-
[util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string;
|
|
11
|
+
toJSON(): object;
|
|
31
12
|
}
|
|
32
13
|
|
|
33
14
|
type InspectFunction = typeof util.inspect;
|
|
@@ -36,27 +17,47 @@ declare abstract class BaseObject {
|
|
|
36
17
|
abstract toJSON(): string | object;
|
|
37
18
|
abstract [util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string;
|
|
38
19
|
}
|
|
39
|
-
type SecureFactory = {
|
|
40
|
-
string: (value: string) => SecureString;
|
|
41
|
-
connectionString: (value: string, secretKeys?: readonly string[]) => SecureConnectionString;
|
|
42
|
-
url: (value: URL) => SecureURL;
|
|
43
|
-
};
|
|
44
20
|
type SecureKeys = readonly string[];
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
#private;
|
|
48
|
-
get secretValue(): string;
|
|
49
|
-
private constructor();
|
|
50
|
-
private parseConnectionString;
|
|
51
|
-
static factory(secret: string | undefined, secretKeys?: SecureKeys): (value: string) => SecureConnectionString;
|
|
52
|
-
static from<T extends string | null | undefined>(value: T, secretKeys?: SecureKeys, secret?: string): T extends string ? SecureConnectionString : T;
|
|
21
|
+
interface IEncryptedValue {
|
|
22
|
+
getValue(): string;
|
|
53
23
|
toString(): string;
|
|
54
24
|
toJSON(): object;
|
|
55
|
-
|
|
25
|
+
}
|
|
26
|
+
interface IEncryptionProvider {
|
|
27
|
+
encrypt(value: string): IEncryptedValue;
|
|
28
|
+
}
|
|
29
|
+
interface SecureConfig {
|
|
30
|
+
secret: string | null;
|
|
31
|
+
encryptionProvider: IEncryptionProvider;
|
|
32
|
+
secretKeys: SecureKeys;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
declare abstract class ISecureString implements BaseObject {
|
|
36
|
+
abstract toString(): string;
|
|
37
|
+
abstract toJSON(): string | object;
|
|
38
|
+
abstract [util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string;
|
|
39
|
+
abstract get secretValue(): string;
|
|
40
|
+
}
|
|
41
|
+
declare abstract class ISecureConnectionString implements BaseObject {
|
|
42
|
+
abstract toString(): string;
|
|
43
|
+
abstract toJSON(): string | object;
|
|
44
|
+
abstract [util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string;
|
|
45
|
+
abstract get secretValue(): string;
|
|
46
|
+
}
|
|
47
|
+
declare abstract class ISecureURL implements BaseObject {
|
|
48
|
+
abstract toString(): string;
|
|
49
|
+
abstract toJSON(): object;
|
|
50
|
+
abstract [util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string;
|
|
51
|
+
abstract get secretValue(): URL;
|
|
52
|
+
}
|
|
53
|
+
declare abstract class ISecureFactory {
|
|
54
|
+
abstract string(value: string): ISecureString;
|
|
55
|
+
abstract connectionString(value: string, secretKeys?: readonly string[]): ISecureConnectionString;
|
|
56
|
+
abstract url(value: URL): ISecureURL;
|
|
56
57
|
}
|
|
57
58
|
|
|
58
|
-
declare const createFactory: (
|
|
59
|
+
declare const createFactory: (options?: Partial<SecureConfig>) => ISecureFactory;
|
|
59
60
|
|
|
60
61
|
declare const defaultSecureKeys: readonly ["AccessKey", "SharedAccessKey", "Password", "AccountKey", "Secret", "SecretKey", "ApiKey", "Token", "Key", "MasterKey", "PrimaryKey", "SecondaryKey"];
|
|
61
62
|
|
|
62
|
-
export {
|
|
63
|
+
export { EncryptedValue, ISecureConnectionString, ISecureString, ISecureURL, createFactory, defaultSecureKeys };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import t
|
|
1
|
+
import{randomBytes as t,createCipheriv as e,createDecipheriv as r,createHash as s,createHmac as n}from"node:crypto";import a from"node:util";var c=Object.defineProperty,i=(t,e)=>c(t,"name",{value:e,configurable:!0}),o=class{static{i(this,"EncryptedValue")}encryptedData;iv;authTag;key;constructor(r){this.key=t(32),this.iv=t(16);const s=e("aes-256-gcm",this.key,this.iv),n=[];n.push(s.update(r,"utf8")),n.push(s.final()),this.encryptedData=Buffer.concat(n),this.authTag=s.getAuthTag()}getValue(){const t=r("aes-256-gcm",this.key,this.iv);t.setAuthTag(this.authTag);try{const e=[];return e.push(t.update(this.encryptedData)),e.push(t.final()),Buffer.concat(e).toString("utf8")}catch(t){throw new Error("Failed to decrypt value: Invalid key or corrupted data")}}toString(){return"[EncryptedValue]"}toJSON(){return{type:"EncryptedValue",encrypted:!0}}},u={sha256:i((()=>({hash:i((t=>s("sha256").update(t).digest("hex")),"hash"),prefix:"sha256"})),"sha256"),hs256:i((t=>({hash:i((e=>n("sha256",t).update(e).digest("hex")),"hash"),prefix:"hs256"})),"hs256")},h=i((t=>null==t?u.sha256():u.hs256(t)),"getAlgorithm"),l=i(((t,e)=>{const r=h(e),s=r.hash(t);return`${r.prefix}:${s}`}),"hash"),p=class{static{i(this,"ISecureString")}},d=class{static{i(this,"ISecureConnectionString")}},y=class{static{i(this,"ISecureURL")}},f=class t extends p{static{i(this,"SecureString")}#t;#e;get secretValue(){return this.#t.getValue()}constructor(t,e){super(),this.#t=e.encryptionProvider.encrypt(t),this.#e=l(t,e.secret)}static factory(e){return r=>t.from(r,e)}static from(e,r){return null===e?null:void 0!==e?new t(e,r):void 0}toString(){return this.#e}toJSON(){return this.toString()}[a.inspect.custom](t,e,r){if(t<0)return"[SecureString]";const s=Object.assign({},e,{depth:null==e.depth?null:e.depth-1});return r(this.toJSON(),s)}},g=class t extends d{static{i(this,"SecureConnectionString")}#t;#r;get secretValue(){return this.#t.getValue()}constructor(t,e){super(),this.#t=e.encryptionProvider.encrypt(t),this.#r=this.parseConnectionString(t,e.secretKeys.map((t=>t.toLocaleLowerCase())),e)}parseConnectionString(t,e,r){return t.split(";").filter(Boolean).map((t=>{const[s,n]=t.split("="),a=n??"",c=e.includes(s.toLocaleLowerCase())?f.from(a,r):a;return[s,c]}))}static factory(e){return r=>t.from(r,e)}static from(e,r){return null===e?null:void 0!==e?new t(e,r):void 0}toString(){return this.#r.map((([t,e])=>`${t}=${e}`)).join(";")}toJSON(){return Object.fromEntries(this.#r)}[a.inspect.custom](t,e,r){if(t<0)return"[SecureConnectionString]";const s=Object.assign({},e,{depth:null==e.depth?null:e.depth-1});return r(this.toJSON(),s)}},S=class t extends y{static{i(this,"SecureURL")}#t;#s;get secretValue(){return new URL(this.#t.getValue())}constructor(t,e){super(),this.#t=e.encryptionProvider.encrypt(t.href),this.#s=f.from(t.password||null,e)}static factory(e){return r=>t.from(r,e)}static from(e,r){return null===e?null:void 0!==e?new t(e,r):void 0}toString(){const t=this.secretValue,e=new URL(t.href);return null!==this.#s&&(e.password=this.#s.toString()),e.href}toJSON(){const t=this.secretValue,e=new URL(t.href);e.password="";const r=new URLSearchParams(e.searchParams);let s;e.search="",null!==this.#s&&(s=this.#s.toString());const n={href:e.href};return void 0!==s&&(n.password=s),r.size>0&&(n.searchParams=Object.fromEntries(r)),n}[a.inspect.custom](t,e,r){if(t<0)return"[SecureURL]";const s=Object.assign({},e,{depth:null==e.depth?null:e.depth-1});return r(this.toJSON(),s)}},m=["AccessKey","SharedAccessKey","Password","AccountKey","Secret","SecretKey","ApiKey","Token","Key","MasterKey","PrimaryKey","SecondaryKey"],w={encrypt:i((t=>new o(t)),"encrypt")},V=i((t=>{const e={secret:t?.secret??null,encryptionProvider:t?.encryptionProvider??w,secretKeys:t?.secretKeys??m};return{string:f.factory(e),connectionString:g.factory(e),url:S.factory(e)}}),"createFactory");export{o as EncryptedValue,d as ISecureConnectionString,p as ISecureString,y as ISecureURL,V as createFactory,m as defaultSecureKeys};//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/hash.ts","../src/types.ts","../src/SecureString.ts","../src/defaults.ts","../src/SecureConnectionString.ts","../src/SecureURL.ts","../src/createFactory.ts"],"names":["hash","value","util"],"mappings":";;AAOA,IAAM,UAAa,GAAA;AAAA,EACjB,wBAAe,MAAA,CAAA,OAAA;AAAA,IACb,IAAA,kBAAO,MAAA,CAAA,CAAA,CAAA,KAAc,UAAW,CAAA,QAAQ,CAAE,CAAA,MAAA,CAAO,CAAC,CAAA,CAAE,MAAO,CAAA,KAAK,CAA1D,EAAA,MAAA,CAAA;AAAA,IACN,MAAQ,EAAA;AAAA,GAFF,CAAA,EAAA,QAAA,CAAA;AAAA,EAIR,KAAA,0BAAQ,MAAoB,MAAA;AAAA,IAC1B,IAAM,kBAAA,MAAA,CAAA,CAAC,CAAc,KAAA,UAAA,CAAW,QAAU,EAAA,MAAM,CAAE,CAAA,MAAA,CAAO,CAAC,CAAA,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G,EAAA;AACzB,MAAO,MAAA,CAAA,YAAA,GAAe,MAAO,CAAA,WAAA,CAAY,YAAY,CAAA;AAAA;AAEvD,IAAO,OAAA,MAAA;AAAA;AACT,EACA,CAAUA,IAAK,CAAA,OAAA,CAAQ,MAAM,CAAE,CAAA,KAAA,EAAe,SAAyB,OAAkC,EAAA;AACvG,IAAA,IAAI,QAAQ,CAAG,EAAA;AACb,MAAO,OAAA,aAAA;AAAA;AAET,IAAA,MAAM,UAAa,GAAA,MAAA,CAAO,MAAO,CAAA,IAAI,OAAS,EAAA;AAAA,MAC5C,OAAO,OAAQ,CAAA,KAAA,IAAS,IAAO,GAAA,IAAA,GAAO,QAAQ,KAAQ,GAAA;AAAA,KACvD,CAAA;AACD,IAAA,OAAO,OAAQ,CAAA,IAAA,CAAK,MAAO,EAAA,EAAG,UAAU,CAAA;AAAA;AAE5C;;;AC5Ea,IAAA,aAAA,2BAAiB,MAAmC,KAAA;AAC/D,EAAO,OAAA;AAAA,IACL,MAAA,EAAQ,YAAa,CAAA,OAAA,CAAQ,MAAM,CAAA;AAAA,IACnC,gBAAA,EAAkB,sBAAuB,CAAA,OAAA,CAAQ,MAAM,CAAA;AAAA,IACvD,GAAA,EAAK,SAAU,CAAA,OAAA,CAAQ,MAAM;AAAA,GAC/B;AACF,CAN6B,EAAA,eAAA","file":"index.js","sourcesContent":["import { createHash, createHmac } from 'node:crypto';\n\ntype HashAlgorithm = {\n hash: (x: string) => string;\n prefix: string;\n};\n\nconst algorithms = {\n sha256: () => ({\n hash: (x: string) => createHash('sha256').update(x).digest('hex'),\n prefix: 'sha256',\n }),\n hs256: (secret: string) => ({\n hash: (x: string) => createHmac('sha256', secret).update(x).digest('hex'),\n prefix: 'hs256',\n }),\n} as const;\n\nconst getAlgorithm = (secret?: string): HashAlgorithm => {\n return secret === undefined ? algorithms.sha256() : algorithms.hs256(secret);\n};\n\nexport const hash = (input: string, secret?: string) => {\n const alg = getAlgorithm(secret);\n const hash = alg.hash(input);\n return `${alg.prefix}:${hash}`;\n};\n","import util, { type InspectOptions } from 'node:util';\nimport type { SecureConnectionString } from './SecureConnectionString';\nimport type { SecureString } from './SecureString';\nimport type { SecureURL } from './SecureURL';\n\nexport type InspectFunction = typeof util.inspect;\n\nexport abstract class BaseObject {\n public abstract toString(): string;\n public abstract toJSON(): string | object;\n public abstract [util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string;\n}\n\nexport type SecureFactory = {\n string: (value: string) => SecureString;\n connectionString: (value: string, secretKeys?: readonly string[]) => SecureConnectionString;\n url: (value: URL) => SecureURL;\n};\n\nexport type SecureKeys = readonly string[];\n","import util, { type InspectOptions } from 'node:util';\nimport { hash } from './hash';\nimport { BaseObject, type InspectFunction } from './types';\n\nexport class SecureString extends BaseObject {\n readonly #value: string;\n readonly #hash: string;\n\n public get secretValue(): string {\n return this.#value;\n }\n\n private constructor(value: string, secret?: string) {\n super();\n this.#value = value;\n this.#hash = hash(value, secret);\n }\n\n static factory(secret: string | undefined): (value: string) => SecureString {\n return (value: string) => SecureString.from(value, secret);\n }\n\n public static from<T extends string | null | undefined>(value: T, secret?: string): T extends string ? SecureString : T {\n if (value === null) {\n return null as T extends string ? SecureString : T;\n }\n if (value === undefined) {\n return undefined as T extends string ? SecureString : T;\n }\n return new SecureString(value, secret) as T extends string ? SecureString : T;\n }\n\n public override toString() {\n return this.#hash;\n }\n public override toJSON() {\n return this.toString();\n }\n override [util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string {\n if (depth < 0) {\n return '[SecureString]';\n }\n const newOptions = Object.assign({}, options, {\n depth: options.depth == null ? null : options.depth - 1,\n });\n return inspect(this.toJSON(), newOptions);\n }\n}\n","import type { SecureKeys } from './types';\n\nexport const defaultSecureKeys = ['AccessKey', 'SharedAccessKey', 'Password', 'AccountKey', 'Secret', 'SecretKey', 'ApiKey', 'Token', 'Key', 'MasterKey', 'PrimaryKey', 'SecondaryKey'] as const satisfies SecureKeys;\n","import util, { type InspectOptions } from 'node:util';\nimport { SecureString } from './SecureString';\nimport { defaultSecureKeys } from './defaults';\nimport { BaseObject, type InspectFunction, type SecureKeys } from './types';\n\nexport class SecureConnectionString extends BaseObject {\n readonly #value: string;\n readonly #data: [string, string | SecureString][];\n\n public get secretValue(): string {\n return this.#value;\n }\n\n private constructor(value: string, secretKeys: SecureKeys, secret: string | undefined) {\n super();\n this.#value = value;\n this.#data = this.parseConnectionString(\n value,\n secretKeys.map((x) => x.toLocaleLowerCase()),\n secret,\n );\n }\n\n private parseConnectionString(value: string, secretKeys: SecureKeys, secret: string | undefined): [string, string | SecureString][] {\n return value\n .split(';')\n .filter(Boolean)\n .map((pair) => {\n const [key, value] = pair.split('=');\n const v = value ?? '';\n const val = secretKeys.includes(key.toLocaleLowerCase()) ? SecureString.from(v, secret) : v;\n return [key, val];\n });\n }\n\n static factory(secret: string | undefined, secretKeys?: SecureKeys): (value: string) => SecureConnectionString {\n return (value: string) => SecureConnectionString.from(value, secretKeys, secret);\n }\n\n public static from<T extends string | null | undefined>(value: T, secretKeys?: SecureKeys, secret?: string): T extends string ? SecureConnectionString : T {\n if (value === null) {\n return null as T extends string ? SecureConnectionString : T;\n }\n if (value === undefined) {\n return undefined as T extends string ? SecureConnectionString : T;\n }\n return new SecureConnectionString(value, secretKeys ?? defaultSecureKeys, secret) as T extends string ? SecureConnectionString : T;\n }\n\n public override toString(): string {\n return this.#data.map(([key, value]) => `${key}=${value}`).join(';');\n }\n\n public override toJSON(): object {\n return Object.fromEntries(this.#data);\n }\n\n public override [util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string {\n if (depth < 0) {\n return '[SecureConnectionString]';\n }\n const newOptions = Object.assign({}, options, {\n depth: options.depth == null ? null : options.depth - 1,\n });\n return inspect(this.toJSON(), newOptions);\n }\n}\n","import util, { type InspectOptions } from 'node:util';\nimport { SecureString } from './SecureString';\nimport { BaseObject, type InspectFunction } from './types';\n\ntype UrlObject = {\n href: string;\n password?: string;\n searchParams?: Record<string, string>;\n};\n\n/**\n * SecureURL obfuscates the password from the URL.\n */\nexport class SecureURL extends BaseObject {\n readonly #value: URL;\n readonly #password: SecureString | null;\n\n public get secretValue(): URL {\n return this.#value;\n }\n\n private constructor(value: URL, secret: string | undefined) {\n super();\n this.#value = value;\n this.#password = SecureString.from(value.password || null, secret);\n }\n\n static factory(secret: string | undefined): (value: URL) => SecureURL {\n return (value: URL) => SecureURL.from(value, secret);\n }\n\n public static from<T extends URL | null | undefined>(value: T, secret?: string): T extends URL ? SecureURL : T {\n if (value === null) {\n return null as T extends URL ? SecureURL : T;\n }\n if (value === undefined) {\n return undefined as T extends URL ? SecureURL : T;\n }\n return new SecureURL(value, secret) as T extends URL ? SecureURL : T;\n }\n\n override toString(): string {\n const newUrl = new URL(this.#value.href);\n if (this.#password !== null) {\n newUrl.password = this.#password.toString();\n }\n return newUrl.href;\n }\n\n override toJSON(): UrlObject {\n const newUrl = new URL(this.#value.href);\n newUrl.password = '';\n\n const searchParams = new URLSearchParams(newUrl.searchParams);\n newUrl.search = '';\n\n let password: string | undefined;\n if (this.#password !== null) {\n password = this.#password.toString();\n }\n\n const result: UrlObject = {\n href: newUrl.href,\n };\n if (password !== undefined) {\n result.password = password;\n }\n if (searchParams.size > 0) {\n result.searchParams = Object.fromEntries(searchParams);\n }\n return result;\n }\n override [util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string {\n if (depth < 0) {\n return '[SecureURL]';\n }\n const newOptions = Object.assign({}, options, {\n depth: options.depth == null ? null : options.depth - 1,\n });\n return inspect(this.toJSON(), newOptions);\n }\n}\n","import { SecureConnectionString } from './SecureConnectionString';\nimport { SecureString } from './SecureString';\nimport { SecureURL } from './SecureURL';\nimport type { SecureFactory } from './types';\n\nexport const createFactory = (secret?: string): SecureFactory => {\n return {\n string: SecureString.factory(secret),\n connectionString: SecureConnectionString.factory(secret),\n url: SecureURL.factory(secret),\n };\n};\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/EncryptedValue.ts","../src/hash.ts","../src/interfaces.ts","../src/SecureString.ts","../src/SecureConnectionString.ts","../src/SecureURL.ts","../src/defaults.ts","../src/createFactory.ts"],"names":["hash","value","util"],"mappings":";;AAEO,IAAM,iBAAN,MAAqB;AAAA,EAF5B;AAE4B,IAAA,MAAA,CAAA,IAAA,EAAA,gBAAA,CAAA;AAAA;AAAA,EACT,aAAA;AAAA,EACA,EAAA;AAAA,EACA,OAAA;AAAA,EACA,GAAA;AAAA,EAEV,YAAY,KAAe,EAAA;AAChC,IAAK,IAAA,CAAA,GAAA,GAAM,YAAY,EAAE,CAAA;AACzB,IAAK,IAAA,CAAA,EAAA,GAAK,YAAY,EAAE,CAAA;AAExB,IAAA,MAAM,SAAS,cAAe,CAAA,aAAA,EAAe,IAAK,CAAA,GAAA,EAAK,KAAK,EAAE,CAAA;AAE9D,IAAA,MAAM,kBAA4B,EAAC;AACnC,IAAA,eAAA,CAAgB,IAAK,CAAA,MAAA,CAAO,MAAO,CAAA,KAAA,EAAO,MAAM,CAAC,CAAA;AACjD,IAAgB,eAAA,CAAA,IAAA,CAAK,MAAO,CAAA,KAAA,EAAO,CAAA;AAEnC,IAAK,IAAA,CAAA,aAAA,GAAgB,MAAO,CAAA,MAAA,CAAO,eAAe,CAAA;AAClD,IAAK,IAAA,CAAA,OAAA,GAAU,OAAO,UAAW,EAAA;AAAA;AACnC,EAEO,QAAmB,GAAA;AACxB,IAAA,MAAM,WAAW,gBAAiB,CAAA,aAAA,EAAe,IAAK,CAAA,GAAA,EAAK,KAAK,EAAE,CAAA;AAClE,IAAS,QAAA,CAAA,UAAA,CAAW,KAAK,OAAO,CAAA;AAEhC,IAAI,IAAA;AACF,MAAA,MAAM,kBAA4B,EAAC;AACnC,MAAA,eAAA,CAAgB,IAAK,CAAA,QAAA,CAAS,MAAO,CAAA,IAAA,CAAK,aAAa,CAAC,CAAA;AACxD,MAAgB,eAAA,CAAA,IAAA,CAAK,QAAS,CAAA,KAAA,EAAO,CAAA;AAErC,MAAA,OAAO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AkC,EAAA;AACvG,IAAA,IAAI,QAAQ,CAAG,EAAA;AACb,MAAO,OAAA,aAAA;AAAA;AAET,IAAA,MAAM,UAAa,GAAA,MAAA,CAAO,MAAO,CAAA,IAAI,OAAS,EAAA;AAAA,MAC5C,OAAO,OAAQ,CAAA,KAAA,IAAS,IAAO,GAAA,IAAA,GAAO,QAAQ,KAAQ,GAAA;AAAA,KACvD,CAAA;AACD,IAAA,OAAO,OAAQ,CAAA,IAAA,CAAK,MAAO,EAAA,EAAG,UAAU,CAAA;AAAA;AAE5C,CAAA;;;AC/EO,IAAM,iBAAoB,GAAA,CAAC,WAAa,EAAA,iBAAA,EAAmB,UAAY,EAAA,YAAA,EAAc,QAAU,EAAA,WAAA,EAAa,QAAU,EAAA,OAAA,EAAS,KAAO,EAAA,WAAA,EAAa,cAAc,cAAc;;;ACM/K,IAAM,yBAAiD,GAAA;AAAA,EAC5D,yBAAU,MAAA,CAAA,CAAA,KAAA,KAAkB,IAAI,cAAA,CAAe,KAAK,CAA3C,EAAA,SAAA;AACX,CAAA;AAEa,IAAA,aAAA,2BAAiB,OAAoD,KAAA;AAChF,EAAA,MAAM,MAAuB,GAAA;AAAA,IAC3B,MAAA,EAAQ,SAAS,MAAU,IAAA,IAAA;AAAA,IAC3B,kBAAA,EAAoB,SAAS,kBAAsB,IAAA,yBAAA;AAAA,IACnD,UAAA,EAAY,SAAS,UAAc,IAAA;AAAA,GACrC;AAEA,EAAO,OAAA;AAAA,IACL,MAAA,EAAQ,YAAa,CAAA,OAAA,CAAQ,MAAM,CAAA;AAAA,IACnC,gBAAA,EAAkB,sBAAuB,CAAA,OAAA,CAAQ,MAAM,CAAA;AAAA,IACvD,GAAA,EAAK,SAAU,CAAA,OAAA,CAAQ,MAAM;AAAA,GAC/B;AACF,CAZ6B,EAAA,eAAA","file":"index.js","sourcesContent":["import { createCipheriv, createDecipheriv, randomBytes } from 'node:crypto';\n\nexport class EncryptedValue {\n private readonly encryptedData: Buffer;\n private readonly iv: Buffer;\n private readonly authTag: Buffer;\n private readonly key: Buffer;\n\n public constructor(value: string) {\n this.key = randomBytes(32);\n this.iv = randomBytes(16);\n\n const cipher = createCipheriv('aes-256-gcm', this.key, this.iv);\n\n const encryptedChunks: Buffer[] = [];\n encryptedChunks.push(cipher.update(value, 'utf8'));\n encryptedChunks.push(cipher.final());\n\n this.encryptedData = Buffer.concat(encryptedChunks);\n this.authTag = cipher.getAuthTag();\n }\n\n public getValue(): string {\n const decipher = createDecipheriv('aes-256-gcm', this.key, this.iv);\n decipher.setAuthTag(this.authTag);\n\n try {\n const decryptedChunks: Buffer[] = [];\n decryptedChunks.push(decipher.update(this.encryptedData));\n decryptedChunks.push(decipher.final());\n\n return Buffer.concat(decryptedChunks).toString('utf8');\n } catch (_) {\n // Hide internal error details\n throw new Error('Failed to decrypt value: Invalid key or corrupted data');\n }\n }\n\n public toString(): string {\n return '[EncryptedValue]';\n }\n\n public toJSON(): object {\n return { type: 'EncryptedValue', encrypted: true };\n }\n}\n","import { createHash, createHmac } from 'node:crypto';\n\ntype HashAlgorithm = {\n hash: (x: string) => string;\n prefix: string;\n};\n\nconst algorithms = {\n sha256: () => ({\n hash: (x: string) => createHash('sha256').update(x).digest('hex'),\n prefix: 'sha256',\n }),\n hs256: (secret: string) => ({\n hash: (x: string) => createHmac('sha256', secret).update(x).digest('hex'),\n prefix: 'hs256',\n }),\n} as const;\n\nconst getAlgorithm = (secret: string | null): HashAlgorithm => {\n return secret == null ? algorithms.sha256() : algorithms.hs256(secret);\n};\n\nexport const hash = (input: string, secret: string | null) => {\n const alg = getAlgorithm(secret);\n const hash = alg.hash(input);\n return `${alg.prefix}:${hash}`;\n};\n","import util, { type InspectOptions } from 'node:util';\nimport type { BaseObject, InspectFunction } from './types';\n\nexport abstract class ISecureString implements BaseObject {\n public abstract toString(): string;\n public abstract toJSON(): string | object;\n public abstract [util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string;\n public abstract get secretValue(): string;\n}\n\nexport abstract class ISecureConnectionString implements BaseObject {\n public abstract toString(): string;\n public abstract toJSON(): string | object;\n public abstract [util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string;\n public abstract get secretValue(): string;\n}\n\nexport abstract class ISecureURL implements BaseObject {\n public abstract toString(): string;\n public abstract toJSON(): object;\n public abstract [util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string;\n public abstract get secretValue(): URL;\n}\n\nexport abstract class ISecureFactory {\n public abstract string(value: string): ISecureString;\n public abstract connectionString(value: string, secretKeys?: readonly string[]): ISecureConnectionString;\n public abstract url(value: URL): ISecureURL;\n}\n","import util, { type InspectOptions } from 'node:util';\nimport { hash } from './hash';\nimport { ISecureString } from './interfaces';\nimport type { IEncryptedValue, InspectFunction, SecureConfig } from './types';\n\nexport class SecureString extends ISecureString {\n readonly #encryptedValue: IEncryptedValue;\n readonly #hash: string;\n\n public get secretValue(): string {\n return this.#encryptedValue.getValue();\n }\n\n private constructor(value: string, config: SecureConfig) {\n super();\n this.#encryptedValue = config.encryptionProvider.encrypt(value);\n this.#hash = hash(value, config.secret);\n }\n\n static factory(config: SecureConfig): (value: string) => SecureString {\n return (value: string) => SecureString.from(value, config);\n }\n\n public static from<T extends string | null | undefined>(value: T, config: SecureConfig): T extends string ? SecureString : T {\n if (value === null) {\n return null as T extends string ? SecureString : T;\n }\n if (value === undefined) {\n return undefined as T extends string ? SecureString : T;\n }\n return new SecureString(value, config) as T extends string ? SecureString : T;\n }\n\n public override toString() {\n return this.#hash;\n }\n public override toJSON() {\n return this.toString();\n }\n override [util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string {\n if (depth < 0) {\n return '[SecureString]';\n }\n const newOptions = Object.assign({}, options, {\n depth: options.depth == null ? null : options.depth - 1,\n });\n return inspect(this.toJSON(), newOptions);\n }\n}\n","import util, { type InspectOptions } from 'node:util';\nimport { SecureString } from './SecureString';\nimport { ISecureConnectionString } from './interfaces';\nimport type { IEncryptedValue, InspectFunction, SecureConfig, SecureKeys } from './types';\n\nexport class SecureConnectionString extends ISecureConnectionString {\n readonly #encryptedValue: IEncryptedValue;\n readonly #data: [string, string | SecureString][];\n\n public get secretValue(): string {\n return this.#encryptedValue.getValue();\n }\n\n private constructor(value: string, config: SecureConfig) {\n super();\n this.#encryptedValue = config.encryptionProvider.encrypt(value);\n this.#data = this.parseConnectionString(\n value,\n config.secretKeys.map((x) => x.toLocaleLowerCase()),\n config,\n );\n }\n\n private parseConnectionString(value: string, secretKeys: SecureKeys, config: SecureConfig): [string, string | SecureString][] {\n return value\n .split(';')\n .filter(Boolean)\n .map((pair) => {\n const [key, value] = pair.split('=');\n const v = value ?? '';\n const val = secretKeys.includes(key.toLocaleLowerCase()) ? SecureString.from(v, config) : v;\n return [key, val];\n });\n }\n\n static factory(config: SecureConfig): (value: string) => SecureConnectionString {\n return (value: string) => SecureConnectionString.from(value, config);\n }\n\n public static from<T extends string | null | undefined>(value: T, config: SecureConfig): T extends string ? SecureConnectionString : T {\n if (value === null) {\n return null as T extends string ? SecureConnectionString : T;\n }\n if (value === undefined) {\n return undefined as T extends string ? SecureConnectionString : T;\n }\n return new SecureConnectionString(value, config) as T extends string ? SecureConnectionString : T;\n }\n\n public override toString(): string {\n return this.#data.map(([key, value]) => `${key}=${value}`).join(';');\n }\n\n public override toJSON(): object {\n return Object.fromEntries(this.#data);\n }\n\n public override [util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string {\n if (depth < 0) {\n return '[SecureConnectionString]';\n }\n const newOptions = Object.assign({}, options, {\n depth: options.depth == null ? null : options.depth - 1,\n });\n return inspect(this.toJSON(), newOptions);\n }\n}\n","import util, { type InspectOptions } from 'node:util';\nimport { SecureString } from './SecureString';\nimport { ISecureURL } from './interfaces';\nimport type { IEncryptedValue, InspectFunction, SecureConfig } from './types';\n\ntype UrlObject = {\n href: string;\n password?: string;\n searchParams?: Record<string, string>;\n};\n\nexport class SecureURL extends ISecureURL {\n readonly #encryptedValue: IEncryptedValue;\n readonly #password: SecureString | null;\n\n public get secretValue(): URL {\n return new URL(this.#encryptedValue.getValue());\n }\n\n private constructor(value: URL, config: SecureConfig) {\n super();\n this.#encryptedValue = config.encryptionProvider.encrypt(value.href);\n this.#password = SecureString.from(value.password || null, config);\n }\n\n static factory(config: SecureConfig): (value: URL) => ISecureURL {\n return (value: URL) => SecureURL.from(value, config);\n }\n\n public static from<T extends URL | null | undefined>(value: T, config: SecureConfig): T extends URL ? SecureURL : T {\n if (value === null) {\n return null as T extends URL ? SecureURL : T;\n }\n if (value === undefined) {\n return undefined as T extends URL ? SecureURL : T;\n }\n return new SecureURL(value, config) as T extends URL ? SecureURL : T;\n }\n\n override toString(): string {\n const originalUrl = this.secretValue;\n const newUrl = new URL(originalUrl.href);\n if (this.#password !== null) {\n newUrl.password = this.#password.toString();\n }\n return newUrl.href;\n }\n\n override toJSON(): UrlObject {\n const originalUrl = this.secretValue;\n const newUrl = new URL(originalUrl.href);\n newUrl.password = '';\n\n const searchParams = new URLSearchParams(newUrl.searchParams);\n newUrl.search = '';\n\n let password: string | undefined;\n if (this.#password !== null) {\n password = this.#password.toString();\n }\n\n const result: UrlObject = {\n href: newUrl.href,\n };\n if (password !== undefined) {\n result.password = password;\n }\n if (searchParams.size > 0) {\n result.searchParams = Object.fromEntries(searchParams);\n }\n return result;\n }\n override [util.inspect.custom](depth: number, options: InspectOptions, inspect: InspectFunction): string {\n if (depth < 0) {\n return '[SecureURL]';\n }\n const newOptions = Object.assign({}, options, {\n depth: options.depth == null ? null : options.depth - 1,\n });\n return inspect(this.toJSON(), newOptions);\n }\n}\n","import type { SecureKeys } from './types';\n\nexport const defaultSecureKeys = ['AccessKey', 'SharedAccessKey', 'Password', 'AccountKey', 'Secret', 'SecretKey', 'ApiKey', 'Token', 'Key', 'MasterKey', 'PrimaryKey', 'SecondaryKey'] as const satisfies SecureKeys;\n","import { EncryptedValue } from './EncryptedValue';\nimport { SecureConnectionString } from './SecureConnectionString';\nimport { SecureString } from './SecureString';\nimport { SecureURL } from './SecureURL';\nimport { defaultSecureKeys } from './defaults';\nimport type { ISecureFactory } from './interfaces';\nimport type { IEncryptionProvider, SecureConfig } from './types';\n\nexport const defaultEncryptionProvider: IEncryptionProvider = {\n encrypt: (value: string) => new EncryptedValue(value),\n};\n\nexport const createFactory = (options?: Partial<SecureConfig>): ISecureFactory => {\n const config: SecureConfig = {\n secret: options?.secret ?? null,\n encryptionProvider: options?.encryptionProvider ?? defaultEncryptionProvider,\n secretKeys: options?.secretKeys ?? defaultSecureKeys,\n };\n\n return {\n string: SecureString.factory(config),\n connectionString: SecureConnectionString.factory(config),\n url: SecureURL.factory(config),\n };\n};\n"]}
|
package/package.json
CHANGED
|
@@ -1,17 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shellicar/core-config",
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "2.0.0",
|
|
3
5
|
"type": "module",
|
|
4
|
-
"version": "1.0.1",
|
|
5
|
-
"description": "A library for securely handling sensitive configuration values like connection strings, URLs, and secrets.",
|
|
6
6
|
"license": "MIT",
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"type": "git",
|
|
10
|
-
"url": "git+https://github.com/shellicar/core-config.git"
|
|
11
|
-
},
|
|
12
|
-
"bugs": {
|
|
13
|
-
"url": "https://github.com/shellicar/core-config/issues"
|
|
14
|
-
},
|
|
7
|
+
"author": "Stephen Hellicar",
|
|
8
|
+
"description": "A library for securely handling sensitive configuration values like connection strings, URLs, and secrets.",
|
|
15
9
|
"keywords": [
|
|
16
10
|
"secure",
|
|
17
11
|
"connection",
|
|
@@ -21,6 +15,17 @@
|
|
|
21
15
|
"secret",
|
|
22
16
|
"URL"
|
|
23
17
|
],
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/shellicar/core-config.git"
|
|
21
|
+
},
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/shellicar/core-config/issues"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/shellicar/core-config#readme",
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
24
29
|
"exports": {
|
|
25
30
|
".": {
|
|
26
31
|
"import": "./dist/index.js",
|
|
@@ -29,7 +34,7 @@
|
|
|
29
34
|
},
|
|
30
35
|
"main": "dist/index.cjs",
|
|
31
36
|
"module": "dist/index.js",
|
|
32
|
-
"types": "dist/index.d.
|
|
37
|
+
"types": "dist/index.d.cts",
|
|
33
38
|
"typesVersions": {
|
|
34
39
|
"*": {
|
|
35
40
|
"*": [
|
|
@@ -42,9 +47,6 @@
|
|
|
42
47
|
"dist",
|
|
43
48
|
"*.md"
|
|
44
49
|
],
|
|
45
|
-
"publishConfig": {
|
|
46
|
-
"access": "public"
|
|
47
|
-
},
|
|
48
50
|
"devDependencies": {
|
|
49
51
|
"@tsconfig/node20": "^20.1.4",
|
|
50
52
|
"@types/node": "^22.10.5",
|