process-ipc-message-fn.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright 2019-2024 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. // this is a function and not an iife so use it carefully
  5. (function (message) {
  6. if (
  7. message instanceof ArrayBuffer ||
  8. ArrayBuffer.isView(message) ||
  9. Array.isArray(message)
  10. ) {
  11. return {
  12. contentType: 'application/octet-stream',
  13. data: message
  14. }
  15. } else {
  16. const data = JSON.stringify(message, (_k, val) => {
  17. // if this value changes, make sure to update it in:
  18. // 1. ipc.js
  19. // 2. core.ts
  20. const SERIALIZE_TO_IPC_FN = '__TAURI_TO_IPC_KEY__'
  21. if (val instanceof Map) {
  22. return Object.fromEntries(val.entries())
  23. } else if (val instanceof Uint8Array) {
  24. return Array.from(val)
  25. } else if (val instanceof ArrayBuffer) {
  26. return Array.from(new Uint8Array(val))
  27. } else if (typeof val === "object" && val !== null && SERIALIZE_TO_IPC_FN in val) {
  28. return val[SERIALIZE_TO_IPC_FN]()
  29. } else {
  30. return val
  31. }
  32. })
  33. return {
  34. contentType: 'application/json',
  35. data
  36. }
  37. }
  38. })