{"version":3,"file":"hocuspocus-logger.cjs","names":[],"sources":["../src/Logger.ts"],"sourcesContent":["import type {\n\tExtension,\n\tonChangePayload,\n\tonConfigurePayload,\n\tonConnectPayload,\n\tonDestroyPayload,\n\tonDisconnectPayload,\n\tonLoadDocumentPayload,\n\tonRequestPayload,\n\tonStoreDocumentPayload,\n\tonUpgradePayload,\n} from \"@hocuspocus/server\";\n\nexport interface LoggerConfiguration {\n\t/**\n\t * Prepend all logging message with a string.\n\t *\n\t * @deprecated\n\t */\n\tprefix: null | string;\n\t/**\n\t * Whether to log something for the `onLoadDocument` hook.\n\t */\n\tonLoadDocument: boolean;\n\t/**\n\t * Whether to log something for the `onChange` hook.\n\t */\n\tonChange: boolean;\n\t/**\n\t * Whether to log something for the `onStoreDocument` hook.\n\t */\n\tonStoreDocument: boolean;\n\t/**\n\t * Whether to log something for the `onConnect` hook.\n\t */\n\tonConnect: boolean;\n\t/**\n\t * Whether to log something for the `onDisconnect` hook.\n\t */\n\tonDisconnect: boolean;\n\t/**\n\t * Whether to log something for the `onUpgrade` hook.\n\t */\n\tonUpgrade: boolean;\n\t/**\n\t * Whether to log something for the `onRequest` hook.\n\t */\n\tonRequest: boolean;\n\t/**\n\t * Whether to log something for the `onDestroy` hook.\n\t */\n\tonDestroy: boolean;\n\t/**\n\t * Whether to log something for the `onConfigure` hook.\n\t */\n\tonConfigure: boolean;\n\t/**\n\t * A log function, if none is provided output will go to console\n\t */\n\tlog: (...args: any[]) => void;\n}\n\nexport class Logger implements Extension {\n\tname: string | null = null;\n\n\tconfiguration: LoggerConfiguration = {\n\t\tprefix: null,\n\t\tonLoadDocument: true,\n\t\tonChange: true,\n\t\tonStoreDocument: true,\n\t\tonConnect: true,\n\t\tonDisconnect: true,\n\t\tonUpgrade: true,\n\t\tonRequest: true,\n\t\tonDestroy: true,\n\t\tonConfigure: true,\n\t\tlog: console.log, // eslint-disable-line\n\t};\n\n\t/**\n\t * Constructor\n\t */\n\tconstructor(configuration?: Partial<LoggerConfiguration>) {\n\t\tthis.configuration = {\n\t\t\t...this.configuration,\n\t\t\t...configuration,\n\t\t};\n\t}\n\n\tasync onConfigure(data: onConfigurePayload) {\n\t\tthis.name = data.instance.configuration.name;\n\n\t\tif (!this.configuration.onConfigure) {\n\t\t\treturn;\n\t\t}\n\n\t\tif (this.configuration.prefix) {\n\t\t\tconsole.warn(\n\t\t\t\t\"[hocuspocus warn] The Logger 'prefix' is deprecated. Pass a 'name' to the Hocuspocus configuration instead.\",\n\t\t\t);\n\t\t}\n\t}\n\n\tasync onLoadDocument(data: onLoadDocumentPayload) {\n\t\tif (this.configuration.onLoadDocument) {\n\t\t\tthis.log(`Loaded document \"${data.documentName}\".`);\n\t\t}\n\t}\n\n\tasync onChange(data: onChangePayload) {\n\t\tif (this.configuration.onChange) {\n\t\t\tthis.log(`Document \"${data.documentName}\" changed.`);\n\t\t}\n\t}\n\n\tasync onStoreDocument(data: onStoreDocumentPayload) {\n\t\tif (this.configuration.onStoreDocument) {\n\t\t\tthis.log(`Store \"${data.documentName}\".`);\n\t\t}\n\t}\n\n\tasync onConnect(data: onConnectPayload) {\n\t\tif (this.configuration.onConnect) {\n\t\t\tthis.log(`New connection to \"${data.documentName}\".`);\n\t\t}\n\t}\n\n\tasync onDisconnect(data: onDisconnectPayload) {\n\t\tif (this.configuration.onDisconnect) {\n\t\t\tthis.log(`Connection to \"${data.documentName}\" closed.`);\n\t\t}\n\t}\n\n\tasync onUpgrade(data: onUpgradePayload) {\n\t\tif (this.configuration.onUpgrade) {\n\t\t\tthis.log(\"Upgrading connection …\");\n\t\t}\n\t}\n\n\tasync onRequest(data: onRequestPayload) {\n\t\tif (this.configuration.onRequest) {\n\t\t\tthis.log(`Incoming HTTP Request to ${data.request.url}`);\n\t\t}\n\t}\n\n\tasync onDestroy(data: onDestroyPayload) {\n\t\tif (this.configuration.onDestroy) {\n\t\t\tthis.log(\"Shut down.\");\n\t\t}\n\t}\n\n\tprivate log(message: string) {\n\t\tconst date = new Date().toISOString();\n\t\tlet meta = `${date}`;\n\n\t\tif (this.name) {\n\t\t\tmeta = `${this.name} ${meta}`;\n\t\t}\n\n\t\tmessage = `[${meta}] ${message}`;\n\n\t\tthis.configuration.log(message);\n\t}\n}\n"],"mappings":";;;AA8DA,IAAa,SAAb,MAAyC;;;;CAoBxC,YAAY,eAA8C;cAnBpC;uBAEe;GACpC,QAAQ;GACR,gBAAgB;GAChB,UAAU;GACV,iBAAiB;GACjB,WAAW;GACX,cAAc;GACd,WAAW;GACX,WAAW;GACX,WAAW;GACX,aAAa;GACb,KAAK,QAAQ;GACb;AAMA,OAAK,gBAAgB;GACpB,GAAG,KAAK;GACR,GAAG;GACH;;CAGF,MAAM,YAAY,MAA0B;AAC3C,OAAK,OAAO,KAAK,SAAS,cAAc;AAExC,MAAI,CAAC,KAAK,cAAc,YACvB;AAGD,MAAI,KAAK,cAAc,OACtB,SAAQ,KACP,8GACA;;CAIH,MAAM,eAAe,MAA6B;AACjD,MAAI,KAAK,cAAc,eACtB,MAAK,IAAI,oBAAoB,KAAK,aAAa,IAAI;;CAIrD,MAAM,SAAS,MAAuB;AACrC,MAAI,KAAK,cAAc,SACtB,MAAK,IAAI,aAAa,KAAK,aAAa,YAAY;;CAItD,MAAM,gBAAgB,MAA8B;AACnD,MAAI,KAAK,cAAc,gBACtB,MAAK,IAAI,UAAU,KAAK,aAAa,IAAI;;CAI3C,MAAM,UAAU,MAAwB;AACvC,MAAI,KAAK,cAAc,UACtB,MAAK,IAAI,sBAAsB,KAAK,aAAa,IAAI;;CAIvD,MAAM,aAAa,MAA2B;AAC7C,MAAI,KAAK,cAAc,aACtB,MAAK,IAAI,kBAAkB,KAAK,aAAa,WAAW;;CAI1D,MAAM,UAAU,MAAwB;AACvC,MAAI,KAAK,cAAc,UACtB,MAAK,IAAI,yBAAyB;;CAIpC,MAAM,UAAU,MAAwB;AACvC,MAAI,KAAK,cAAc,UACtB,MAAK,IAAI,4BAA4B,KAAK,QAAQ,MAAM;;CAI1D,MAAM,UAAU,MAAwB;AACvC,MAAI,KAAK,cAAc,UACtB,MAAK,IAAI,aAAa;;CAIxB,AAAQ,IAAI,SAAiB;EAE5B,IAAI,OAAO,oBADE,IAAI,MAAM,EAAC,aAAa;AAGrC,MAAI,KAAK,KACR,QAAO,GAAG,KAAK,KAAK,GAAG;AAGxB,YAAU,IAAI,KAAK,IAAI;AAEvB,OAAK,cAAc,IAAI,QAAQ"}