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