process-ipc-message-fn.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 (val instanceof Map) {
  18. return Object.fromEntries(val.entries())
  19. } else if (val instanceof Uint8Array) {
  20. return Array.from(val)
  21. } else if (val instanceof ArrayBuffer) {
  22. return Array.from(new Uint8Array(val))
  23. } else if (
  24. typeof val === "object" &&
  25. '__TAURI_CHANNEL_MARKER__' in val &&
  26. typeof val.id === 'number'
  27. ) {
  28. return `__CHANNEL__:${val.id}`
  29. } else if (typeof val === "object" &&'toIPC' in val){
  30. return val.toIPC()
  31. } else {
  32. return val
  33. }
  34. })
  35. return {
  36. contentType: 'application/json',
  37. data
  38. }
  39. }
  40. })