@witchcraft/nuxt-logger 0.1.0 → 0.1.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/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "logger",
3
3
  "configKey": "logger",
4
- "version": "0.1.0",
4
+ "version": "0.1.1",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.3",
7
7
  "unbuild": "unknown"
@@ -10,9 +10,10 @@ import pino from "pino";
10
10
  * - We can't use useRuntimeConfig so it's injected by using vite define instead ({@link STATIC}).
11
11
  * - We need to pass the logPath which is defined at runtime.
12
12
  * - We need to use some electron only imports which causes problems for the regular server.
13
- * - Pino needs to be told were to find it's workers electron side in prod.
13
+ * - Pino needs to be told were to find it's workers electron side in prod both when using a packed asar and when not.
14
14
  * {@link https://github.com/pinojs/pino/blob/master/docs/bundling.md}
15
15
  * Using vite's ?worker&url imports almost works (the paths are correct) but something about the transpiling breaks things I think.
16
+ *
16
17
  */
17
18
  export declare function useElectronLogger(pinoConfig?: PublicRuntimeConfig["logger"], windows?: () => BrowserWindow[], { receiveFromRenderer }?: {
18
19
  receiveFromRenderer?: boolean;
@@ -6,6 +6,7 @@ import { receiveFromRendererOnMain } from "./receiveFromRendererOnMain.js";
6
6
  import { getBaseOptions } from "../../helpers/getBaseOptions.js";
7
7
  import { ELECTRON_LOG_TYPE } from "../types.js";
8
8
  globalThis.__bundlerPathsOverrides = {
9
+ nodeModulesLocation: app.getAppPath(),
9
10
  "pino-worker": `./node_modules/pino/lib/worker.js`,
10
11
  "pino-worker-pipeline": `./node_modules/pino/lib/worker-pipeline.js`,
11
12
  "thread-stream-worker": `./node_modules/thread-stream/lib/worker.js`,
@@ -13,9 +14,8 @@ globalThis.__bundlerPathsOverrides = {
13
14
  "pino-pretty": `./node_modules/pino-pretty/index.js`
14
15
  };
15
16
  const bundlerOverrides = globalThis.__bundlerPathsOverrides;
16
- const appPath = app.getAppPath();
17
17
  for (const key of keys(bundlerOverrides)) {
18
- bundlerOverrides[key] = path.resolve(appPath, bundlerOverrides[key]);
18
+ bundlerOverrides[key] = path.resolve(bundleOverrides.nodeModulesLocation, bundlerOverrides[key]);
19
19
  }
20
20
  let initiated = false;
21
21
  const levels = ["error", "warn", "debug", "trace", "info", "fatal"];
@@ -1,7 +1,13 @@
1
1
  import { ELECTRON_LOG_TYPE } from "../types.js";
2
2
  export function createElectronLoggerApi(ipcRenderer) {
3
3
  return {
4
- log: (level, data) => ipcRenderer.send(ELECTRON_LOG_TYPE.TO_MAIN, { level, data }),
4
+ log: (level, data) => {
5
+ try {
6
+ ipcRenderer.send(ELECTRON_LOG_TYPE.TO_MAIN, { level, data });
7
+ } catch (error) {
8
+ throw new Error(`[nuxt-logger] could not forward a "${level}" log to electron's main process, its payload must be cloneable, pass raw values (e.g. \`toRaw\` on a reactive object). Log: ${JSON.stringify(data)}`, { cause: error });
9
+ }
10
+ },
5
11
  _logs: {
6
12
  on(cb) {
7
13
  ipcRenderer.on(ELECTRON_LOG_TYPE.FROM_MAIN_ON_RENDERER, (_event, level, data) => {
@@ -90,7 +90,7 @@ export function getBaseOptions(config, serverPathResolver) {
90
90
  send: (level, logEvent) => {
91
91
  if (!logEvent.messages[0]?.ns?.startsWith("main") && levels.values[level] < levels.values[writeLevel]) {
92
92
  logEvent.messages[0].ns &&= `renderer:${logEvent.messages[0].ns}`;
93
- window.electron.api.log(logEvent);
93
+ window.electron.api.log(level, logEvent);
94
94
  }
95
95
  }
96
96
  } : void 0
@@ -1,3 +1,4 @@
1
+ import { toRaw } from "vue";
1
2
  import { defineNuxtPlugin, useRuntimeConfig } from "#imports";
2
3
  import { useLogger } from "../composables/useLogger.js";
3
4
  export default defineNuxtPlugin({
@@ -5,7 +6,7 @@ export default defineNuxtPlugin({
5
6
  setup(nuxtApp) {
6
7
  if (import.meta.client) {
7
8
  nuxtApp.hook("app:created", () => {
8
- const config = useRuntimeConfig().public;
9
+ const config = toRaw(useRuntimeConfig().public);
9
10
  const initialLog = { appInfo: config.appInfo };
10
11
  if (window.navigator !== void 0) {
11
12
  initialLog.environmentInfo = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@witchcraft/nuxt-logger",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Nuxt module for logging.",
5
5
  "repository": "witchcraftjs/nuxt-logger",
6
6
  "license": "MIT",
@@ -11,6 +11,7 @@ import { ELECTRON_LOG_TYPE } from "../types.js"
11
11
 
12
12
  declare const globalThis: any
13
13
  globalThis.__bundlerPathsOverrides = {
14
+ nodeModulesLocation: app.getAppPath(),
14
15
  "pino-worker": `./node_modules/pino/lib/worker.js`,
15
16
  "pino-worker-pipeline": `./node_modules/pino/lib/worker-pipeline.js`,
16
17
  "thread-stream-worker": `./node_modules/thread-stream/lib/worker.js`,
@@ -19,9 +20,8 @@ globalThis.__bundlerPathsOverrides = {
19
20
  }
20
21
  const bundlerOverrides = globalThis.__bundlerPathsOverrides
21
22
 
22
- const appPath = app.getAppPath()
23
23
  for (const key of keys(bundlerOverrides)) {
24
- bundlerOverrides[key] = path.resolve(appPath, bundlerOverrides[key])
24
+ bundlerOverrides[key] = path.resolve(bundleOverrides.nodeModulesLocation, bundlerOverrides[key])
25
25
  }
26
26
 
27
27
  let initiated = false
@@ -37,16 +37,20 @@ const logger: Record<typeof levels[number], ((data: any) => void)> = {} as any
37
37
  * - We can't use useRuntimeConfig so it's injected by using vite define instead ({@link STATIC}).
38
38
  * - We need to pass the logPath which is defined at runtime.
39
39
  * - We need to use some electron only imports which causes problems for the regular server.
40
- * - Pino needs to be told were to find it's workers electron side in prod.
40
+ * - Pino needs to be told were to find it's workers electron side in prod both when using a packed asar and when not.
41
41
  * {@link https://github.com/pinojs/pino/blob/master/docs/bundling.md}
42
42
  * Using vite's ?worker&url imports almost works (the paths are correct) but something about the transpiling breaks things I think.
43
+ *
43
44
  */
44
45
 
45
46
  export function useElectronLogger(
46
47
  pinoConfig?: PublicRuntimeConfig["logger"],
47
- windows?: () => BrowserWindow[], {
48
+ windows?: () => BrowserWindow[],
49
+ {
48
50
  receiveFromRenderer = true
49
- }: { receiveFromRenderer?: boolean } = {}
51
+ }: {
52
+ receiveFromRenderer?: boolean
53
+ } = {}
50
54
  ) {
51
55
  if (initiated && pinoConfig) {
52
56
  throw new Error("Config can only be passed the first time when initiating.")
@@ -10,7 +10,13 @@ export type ElectronLoggerApi = {
10
10
 
11
11
  export function createElectronLoggerApi(ipcRenderer: typeof IpcRenderer): ElectronLoggerApi {
12
12
  return {
13
- log: (level: Levels, data: any) => ipcRenderer.send(ELECTRON_LOG_TYPE.TO_MAIN, { level, data }),
13
+ log: (level: Levels, data: any) => {
14
+ try {
15
+ ipcRenderer.send(ELECTRON_LOG_TYPE.TO_MAIN, { level, data })
16
+ } catch (error) {
17
+ throw new Error(`[nuxt-logger] could not forward a "${level}" log to electron's main process, its payload must be cloneable, pass raw values (e.g. \`toRaw\` on a reactive object). Log: ${JSON.stringify(data)}`, { cause: error })
18
+ }
19
+ },
14
20
  _logs: {
15
21
  on(cb: (level: Levels, data: any) => void) {
16
22
  ipcRenderer.on(ELECTRON_LOG_TYPE.FROM_MAIN_ON_RENDERER, (_event, level, data) => {
@@ -7,6 +7,8 @@ import type { PublicRuntimeConfig } from "@nuxt/schema"
7
7
  import type { LoggerOptions, TransportMultiOptions } from "pino"
8
8
  import { pino } from "pino"
9
9
 
10
+ import type { ElectronLoggerApi } from "../electron/preload/createElectronLoggerApi.js"
11
+
10
12
  // normally we would access logger.levels, but because we create
11
13
  // the shared config here we can't
12
14
  const levels = {
@@ -129,7 +131,7 @@ export function getBaseOptions(
129
131
  send: (level, logEvent) => {
130
132
  if (!logEvent.messages[0]?.ns?.startsWith("main") && levels.values[level] < levels.values[writeLevel]) {
131
133
  logEvent.messages[0].ns &&= `renderer:${logEvent.messages[0].ns}`
132
- ;(window as any).electron.api.log(logEvent)
134
+ ;((window as any).electron.api as ElectronLoggerApi).log(level, logEvent)
133
135
  }
134
136
  }
135
137
  }
@@ -1,3 +1,5 @@
1
+ import { toRaw } from "vue"
2
+
1
3
  import { defineNuxtPlugin, useRuntimeConfig } from "#imports"
2
4
 
3
5
  import { useLogger } from "../composables/useLogger"
@@ -7,7 +9,7 @@ export default defineNuxtPlugin({
7
9
  setup(nuxtApp) {
8
10
  if (import.meta.client) {
9
11
  nuxtApp.hook("app:created", () => {
10
- const config = useRuntimeConfig().public
12
+ const config = toRaw(useRuntimeConfig().public)
11
13
  const initialLog: any = { appInfo: config.appInfo }
12
14
  if (window.navigator !== undefined) {
13
15
  initialLog.environmentInfo = {