process-ipc-message-fn.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 (
  28. typeof val === "object" &&
  29. '__TAURI_CHANNEL_MARKER__' in val &&
  30. typeof val.id === 'number'
  31. ) {
  32. return `__CHANNEL__:${val.id}`
  33. } else if (typeof val === "object" && SERIALIZE_TO_IPC_FN in val) {
  34. return val[SERIALIZE_TO_IPC_FN]()
  35. } else {
  36. return val
  37. }
  38. })
  39. return {
  40. contentType: 'application/json',
  41. data
  42. }
  43. }
  44. })