@payfit/audit-log 3.18.0 → 3.18.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var E=Object.defineProperty;var b=(t,e,r)=>e in t?E(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r;var n=(t,e,r)=>b(t,typeof e!="symbol"?e+"":e,r);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const _=require("uuid"),d=require("winston"),A=require("hot-shots"),y=require("@aws-sdk/client-sqs"),L=require("zlib"),q="audit-logs",v="@payfit/audit-log",I=()=>{const t=process.env.LOG_LEVEL;return t&&t.trim()!==""?t.trim():"info"},U=(t,e)=>d.createLogger({level:I(),silent:process.env.NODE_ENV==="test",exitOnError:!1,defaultMeta:{namespace:q,sdk:v,service:t,environment:e},format:d.format.combine(d.format.timestamp(),d.format.errors({stack:!0}),d.format.json()),transports:[new d.transports.Console]}),M="payfit.audit_logs.",S=t=>Object.entries(t).filter(([,e])=>e!==void 0&&e!=="").map(([e,r])=>`${e}:${String(r)}`);class ${constructor(){n(this,"client");this.client=new A.StatsD({prefix:M,errorHandler:()=>{},cacheDns:!0})}increment(e,r){this.client.increment(e,1,S(r))}histogram(e,r,i){this.client.histogram(e,r,S(i))}async close(){await new Promise(e=>{this.client.close(()=>e())})}}const z=()=>new $;function C(t){return L.brotliCompressSync(new TextEncoder().encode(t)).toString("base64")}function O(t){return t instanceof Error&&t.message.includes("Message must be shorter than")}const D="eu-west-3",N={dev:"https://sqs.eu-west-3.amazonaws.com/533547282673/audit-logs-ingest",staging:"https://sqs.eu-west-3.amazonaws.com/589883667243/audit-logs-ingest",prod:"https://sqs.eu-west-3.amazonaws.com/614948436281/audit-logs-ingest"},x={working:"staging",production:"prod",development:"dev"};class R{constructor(e,r){n(this,"environment");n(this,"logger");n(this,"client");n(this,"queueUrl");this.environment=e,this.logger=r;const i=x[e]??e;if(this.queueUrl=N[i],!this.queueUrl){r==null||r.warn("audit-logs SDK: no SQS queue URL configured for this environment, audit logs are disabled",{environment:e});return}this.client=new y.SQSClient({region:D})}async create(e){var i;if(!this.client||!this.queueUrl)return{compressed:!1};const r=JSON.stringify(e);try{return await this.send(r),{compressed:!1}}catch(c){if(!O(c))throw c;return(i=this.logger)==null||i.info("audit-log payload exceeds the SQS size limit, retrying with Brotli compression",{id:e.id,namespace:e.namespace}),await this.send(C(r)),{compressed:!0}}}async send(e){await this.client.send(new y.SendMessageCommand({QueueUrl:this.queueUrl,MessageBody:e}))}}class o extends Error{constructor(r,i){super(`${r}: ${i}`);n(this,"path");n(this,"reason");this.path=r,this.reason=i,this.name="AuditLogValidationError"}}const T={countryCode:2,internalId:64,namespace:64,service:64,event:64,target:64,environment:64,holdingId:24,companyId:24,employeeId:24,individualId:24,roleId:24,specialRole:32,email:320},P=new Set(["namespace","service","event","target","environment"]),Q=t=>{for(let e=0;e<t.length;e+=1){const r=t.charCodeAt(e);if(r>=55296&&r<=56319){if(e+1>=t.length)return!0;const i=t.charCodeAt(e+1);if(i<56320||i>57343)return!0;e+=1}else if(r>=56320&&r<=57343)return!0}return!1},u=(t,e,r={})=>{if(t==null){if(r.required)throw new o(e,"must not be empty");return}if(typeof t!="string")throw new o(e,"must be a string");if(r.required&&t==="")throw new o(e,"must not be empty");if(t.includes("\0"))throw new o(e,"contains U+0000");if(Q(t))throw new o(e,"contains unpaired UTF-16 surrogate");if(r.limit!==void 0&&Array.from(t).length>r.limit)throw new o(e,`exceeds PostgreSQL limit of ${r.limit} characters`)},j=(t,e)=>/^[A-Za-z_][A-Za-z0-9_]*$/.test(e)?`${t}.${e}`:`${t}[key]`,m=(t,e,r)=>{if(typeof t=="string"){u(t,e);return}if(typeof t=="bigint")throw new o(e,"BigInt is not JSON-serializable");if(typeof t>"u"||typeof t=="function"||typeof t=="symbol")throw new o(e,`${typeof t} is not JSON-serializable`);if(typeof t=="number"&&!Number.isFinite(t))throw new o(e,"must be a finite number");if(!(t===null||typeof t!="object")){if(r.has(t))throw new o(e,"contains a circular reference");r.add(t),Array.isArray(t)?t.forEach((i,c)=>m(i,`${e}[${c}]`,r)):Object.entries(t).forEach(([i,c])=>{u(i,e),m(c,j(e,i),r)}),r.delete(t)}},F=t=>{for(const[e,r]of Object.entries(T))u(t[e],e,{limit:r,required:P.has(e)});t.payload!==void 0&&t.payload!==null&&m(t.payload,"payload",new Set),t.scope!==void 0&&t.scope!==null&&m(t.scope,"scope",new Set)};class V{constructor(e,r={}){n(this,"params");n(this,"transport");n(this,"logger");n(this,"metrics");this.params=e,this.logger=r.logger??U(e.service,e.environment),this.transport=r.transport??new R(e.environment,this.logger),this.metrics=r.metrics??z()}async log({scope:e,account:r,...i}){const c=_.v4(),{environment:f,service:h}=this.params,s={...i,...r,service:h,id:c,environment:f,...e&&{scope:e}},l={service:h,environment:f,namespace:s.namespace,event:s.event,target:s.target},w=Date.now();try{F(s);const{compressed:g}=await this.transport.create(s);this.metrics.increment("sent",{...l,compressed:String(g)}),this.metrics.histogram("send_duration_ms",Date.now()-w,l),this.logger.debug("audit-log sent",{id:s.id,event:s.event,target:s.target})}catch(g){const a=g instanceof o?g:void 0;this.metrics.increment(a?"validation_failed":"send_failed",l),this.metrics.histogram("send_duration_ms",Date.now()-w,l);const p=g.message;try{this.logger.error(a?"audit-log validation failed":"audit-log write failed",{audit_log:s,error:p,...a&&{validation:{path:a.path,reason:a.reason}}})}catch{this.metrics.increment("serialization_failed",l);try{this.logger.error(a?"audit-log validation failed (serialization failed)":"audit-log write failed (serialization failed)",{audit_log_meta:{id:s.id,service:s.service,environment:s.environment,namespace:s.namespace,event:s.event,target:s.target},error:p,...a&&{validation:{path:a.path,reason:a.reason}}})}catch{}}}}}exports.AuditLogger=V;
1
+ "use strict";var E=Object.defineProperty;var b=(t,e,r)=>e in t?E(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r;var n=(t,e,r)=>b(t,typeof e!="symbol"?e+"":e,r);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const _=require("uuid"),d=require("winston"),A=require("hot-shots"),y=require("@aws-sdk/client-sqs"),L=require("zlib"),q="audit-logs",v="@payfit/audit-log",I=()=>{const t=process.env.LOG_LEVEL;return t&&t.trim()!==""?t.trim():"info"},U=(t,e)=>d.createLogger({level:I(),silent:process.env.NODE_ENV==="test",exitOnError:!1,defaultMeta:{namespace:q,sdk:v,service:t,environment:e},format:d.format.combine(d.format.timestamp(),d.format.errors({stack:!0}),d.format.json()),transports:[new d.transports.Console]}),M="payfit.audit_logs.",S=t=>Object.entries(t).filter(([,e])=>e!==void 0&&e!=="").map(([e,r])=>`${e}:${String(r)}`);class ${constructor(){n(this,"client");this.client=new A.StatsD({prefix:M,errorHandler:()=>{},cacheDns:!0})}increment(e,r){this.client.increment(e,1,S(r))}histogram(e,r,i){this.client.histogram(e,r,S(i))}async close(){await new Promise(e=>{this.client.close(()=>e())})}}const z=()=>new $;function C(t){return L.brotliCompressSync(new TextEncoder().encode(t)).toString("base64")}function O(t){return t instanceof Error&&t.message.includes("Message must be shorter than")}const D="eu-west-3",N={dev:"https://sqs.eu-west-3.amazonaws.com/533547282673/audit-logs-ingest",staging:"https://sqs.eu-west-3.amazonaws.com/589883667243/audit-logs-ingest",prod:"https://sqs.eu-west-3.amazonaws.com/614948436281/audit-logs-ingest"},x={working:"staging",production:"prod",development:"dev"};class R{constructor(e,r){n(this,"environment");n(this,"logger");n(this,"client");n(this,"queueUrl");this.environment=e,this.logger=r;const i=x[e]??e;if(this.queueUrl=N[i],!this.queueUrl){r==null||r.warn("audit-logs SDK: no SQS queue URL configured for this environment, audit logs are disabled",{environment:e});return}this.client=new y.SQSClient({region:D})}async create(e){var i;if(!this.client||!this.queueUrl)return{compressed:!1};const r=JSON.stringify(e);try{return await this.send(r),{compressed:!1}}catch(c){if(!O(c))throw c;return(i=this.logger)==null||i.info("audit-log payload exceeds the SQS size limit, retrying with Brotli compression",{id:e.id,namespace:e.namespace}),await this.send(C(r)),{compressed:!0}}}async send(e){await this.client.send(new y.SendMessageCommand({QueueUrl:this.queueUrl,MessageBody:e}))}}class o extends Error{constructor(r,i){super(`${r}: ${i}`);n(this,"path");n(this,"reason");this.path=r,this.reason=i,this.name="AuditLogValidationError"}}const T={countryCode:2,internalId:380,namespace:64,service:64,event:64,target:64,environment:64,holdingId:24,companyId:24,employeeId:24,individualId:24,roleId:24,specialRole:32,email:320},P=new Set(["namespace","service","event","target","environment"]),Q=t=>{for(let e=0;e<t.length;e+=1){const r=t.charCodeAt(e);if(r>=55296&&r<=56319){if(e+1>=t.length)return!0;const i=t.charCodeAt(e+1);if(i<56320||i>57343)return!0;e+=1}else if(r>=56320&&r<=57343)return!0}return!1},u=(t,e,r={})=>{if(t==null){if(r.required)throw new o(e,"must not be empty");return}if(typeof t!="string")throw new o(e,"must be a string");if(r.required&&t==="")throw new o(e,"must not be empty");if(t.includes("\0"))throw new o(e,"contains U+0000");if(Q(t))throw new o(e,"contains unpaired UTF-16 surrogate");if(r.limit!==void 0&&Array.from(t).length>r.limit)throw new o(e,`exceeds PostgreSQL limit of ${r.limit} characters`)},j=(t,e)=>/^[A-Za-z_][A-Za-z0-9_]*$/.test(e)?`${t}.${e}`:`${t}[key]`,m=(t,e,r)=>{if(typeof t=="string"){u(t,e);return}if(typeof t=="bigint")throw new o(e,"BigInt is not JSON-serializable");if(typeof t>"u"||typeof t=="function"||typeof t=="symbol")throw new o(e,`${typeof t} is not JSON-serializable`);if(typeof t=="number"&&!Number.isFinite(t))throw new o(e,"must be a finite number");if(!(t===null||typeof t!="object")){if(r.has(t))throw new o(e,"contains a circular reference");r.add(t),Array.isArray(t)?t.forEach((i,c)=>m(i,`${e}[${c}]`,r)):Object.entries(t).forEach(([i,c])=>{u(i,e),m(c,j(e,i),r)}),r.delete(t)}},F=t=>{for(const[e,r]of Object.entries(T))u(t[e],e,{limit:r,required:P.has(e)});t.payload!==void 0&&t.payload!==null&&m(t.payload,"payload",new Set),t.scope!==void 0&&t.scope!==null&&m(t.scope,"scope",new Set)};class V{constructor(e,r={}){n(this,"params");n(this,"transport");n(this,"logger");n(this,"metrics");this.params=e,this.logger=r.logger??U(e.service,e.environment),this.transport=r.transport??new R(e.environment,this.logger),this.metrics=r.metrics??z()}async log({scope:e,account:r,...i}){const c=_.v4(),{environment:f,service:h}=this.params,s={...i,...r,service:h,id:c,environment:f,...e&&{scope:e}},l={service:h,environment:f,namespace:s.namespace,event:s.event,target:s.target},w=Date.now();try{F(s);const{compressed:g}=await this.transport.create(s);this.metrics.increment("sent",{...l,compressed:String(g)}),this.metrics.histogram("send_duration_ms",Date.now()-w,l),this.logger.debug("audit-log sent",{id:s.id,event:s.event,target:s.target})}catch(g){const a=g instanceof o?g:void 0;this.metrics.increment(a?"validation_failed":"send_failed",l),this.metrics.histogram("send_duration_ms",Date.now()-w,l);const p=g.message;try{this.logger.error(a?"audit-log validation failed":"audit-log write failed",{audit_log:s,error:p,...a&&{validation:{path:a.path,reason:a.reason}}})}catch{this.metrics.increment("serialization_failed",l);try{this.logger.error(a?"audit-log validation failed (serialization failed)":"audit-log write failed (serialization failed)",{audit_log_meta:{id:s.id,service:s.service,environment:s.environment,namespace:s.namespace,event:s.event,target:s.target},error:p,...a&&{validation:{path:a.path,reason:a.reason}}})}catch{}}}}}exports.AuditLogger=V;
package/dist/index.mjs CHANGED
@@ -105,7 +105,7 @@ class o extends Error {
105
105
  }
106
106
  const P = {
107
107
  countryCode: 2,
108
- internalId: 64,
108
+ internalId: 380,
109
109
  namespace: 64,
110
110
  service: 64,
111
111
  event: 64,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@payfit/audit-log",
3
- "version": "3.18.0",
3
+ "version": "3.18.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/PayFit/audit-logs.git",