Example

On macOS 11+, this will just call through to window.webkit.messageHandlers.x.postMessage

Eg: for a foo message defined in Swift that accepted the payload {"bar": "baz"}, the following would occur:

const json = await window.webkit.messageHandlers.foo.postMessage({ bar: "baz" });
const response = JSON.parse(json)

Example

On macOS 10 however, the process is a little more involved. A method will be appended to window that allows the response to be delivered there instead. It's not exactly this, but you can visualize the flow as being something along the lines of:

// add the window method
window["_0123456"] = (response) => {
// decrypt `response` and deliver the result to the caller here
// then remove the temporary method
delete window["_0123456"]
};

// send the data + `messageHanding` values
window.webkit.messageHandlers.foo.postMessage({
bar: "baz",
messagingHandling: {
methodName: "_0123456",
secret: "super-secret",
key: [1, 2, 45, 2],
iv: [34, 4, 43],
}
});

// later in swift, the following JavaScript snippet will be executed
(() => {
window["_0123456"]({
ciphertext: [12, 13, 4],
tag: [3, 5, 67, 56]
})
})()

Implements

Hierarchy

  • WebkitMessagingTransport

Constructors

Properties

algoObj: {
    length: number;
    name: string;
} = ...

Type declaration

  • length: number
  • name: string
globals: {
    Arrayfrom: {
        <T>(arrayLike): T[];
        <T, U>(arrayLike, mapfn, thisArg?): U[];
        <T>(iterable): T[];
        <T, U>(iterable, mapfn, thisArg?): U[];
    };
    JSONparse: ((text, reviver?) => any);
    JSONstringify: {
        (value, replacer?, space?): string;
        (value, replacer?, space?): string;
    };
    ObjectDefineProperty: (<T>(o, p, attributes) => T);
    Promise: PromiseConstructor;
    TextDecoder: (new (label?, options?) => TextDecoder);
    TextEncoder: (new () => TextEncoder);
    Uint16Array: Uint16ArrayConstructor;
    Uint32Array: Uint32ArrayConstructor;
    Uint8Array: Uint8ArrayConstructor;
    addEventListener: {
        <K>(type, listener, options?): void;
        (type, listener, options?): void;
    } & {
        <K>(type, listener, options?): void;
        (type, listener, options?): void;
    };
    capturedWebkitHandlers: Record<string, any>;
    decrypt: ((algorithm, key, data) => Promise<ArrayBuffer>);
    encrypt: ((algorithm, key, data) => Promise<ArrayBuffer>);
    exportKey: {
        (format, key): Promise<JsonWebKey>;
        (format, key): Promise<ArrayBuffer>;
    };
    generateKey: {
        (algorithm, extractable, keyUsages): Promise<CryptoKeyPair>;
        (algorithm, extractable, keyUsages): Promise<CryptoKey>;
        (algorithm, extractable, keyUsages): Promise<CryptoKey | CryptoKeyPair>;
        (algorithm, extractable, keyUsages): Promise<CryptoKeyPair>;
        (algorithm, extractable, keyUsages): Promise<CryptoKey>;
        (algorithm, extractable, keyUsages): Promise<CryptoKey | CryptoKeyPair>;
    };
    getRandomValues: (<T>(array) => T);
    importKey: {
        (format, keyData, algorithm, extractable, keyUsages): Promise<CryptoKey>;
        (format, keyData, algorithm, extractable, keyUsages): Promise<CryptoKey>;
        (format, keyData, algorithm, extractable, keyUsages): Promise<CryptoKey>;
        (format, keyData, algorithm, extractable, keyUsages): Promise<CryptoKey>;
    };
    window: Window & typeof globalThis;
}

Type declaration

  • Arrayfrom: {
        <T>(arrayLike): T[];
        <T, U>(arrayLike, mapfn, thisArg?): U[];
        <T>(iterable): T[];
        <T, U>(iterable, mapfn, thisArg?): U[];
    }
      • <T>(arrayLike): T[]
      • Creates an array from an array-like object.

        Type Parameters

        • T

        Parameters

        • arrayLike: ArrayLike<T>

          An array-like object to convert to an array.

        Returns T[]

      • <T, U>(arrayLike, mapfn, thisArg?): U[]
      • Creates an array from an iterable object.

        Type Parameters

        • T

        • U

        Parameters

        • arrayLike: ArrayLike<T>

          An array-like object to convert to an array.

        • mapfn: ((v, k) => U)

          A mapping function to call on every element of the array.

            • (v, k): U
            • Parameters

              • v: T
              • k: number

              Returns U

        • Optional thisArg: any

          Value of 'this' used to invoke the mapfn.

        Returns U[]

      • <T>(iterable): T[]
      • Creates an array from an iterable object.

        Type Parameters

        • T

        Parameters

        • iterable: Iterable<T> | ArrayLike<T>

          An iterable object to convert to an array.

        Returns T[]

      • <T, U>(iterable, mapfn, thisArg?): U[]
      • Creates an array from an iterable object.

        Type Parameters

        • T

        • U

        Parameters

        • iterable: Iterable<T> | ArrayLike<T>

          An iterable object to convert to an array.

        • mapfn: ((v, k) => U)

          A mapping function to call on every element of the array.

            • (v, k): U
            • Parameters

              • v: T
              • k: number

              Returns U

        • Optional thisArg: any

          Value of 'this' used to invoke the mapfn.

        Returns U[]

  • JSONparse: ((text, reviver?) => any)
      • (text, reviver?): any
      • Converts a JavaScript Object Notation (JSON) string into an object.

        Parameters

        • text: string

          A valid JSON string.

        • Optional reviver: ((this, key, value) => any)

          A function that transforms the results. This function is called for each member of the object. If a member contains nested objects, the nested objects are transformed before the parent object is.

            • (this, key, value): any
            • Parameters

              • this: any
              • key: string
              • value: any

              Returns any

        Returns any

  • JSONstringify: {
        (value, replacer?, space?): string;
        (value, replacer?, space?): string;
    }
      • (value, replacer?, space?): string
      • Converts a JavaScript value to a JavaScript Object Notation (JSON) string.

        Parameters

        • value: any

          A JavaScript value, usually an object or array, to be converted.

        • Optional replacer: ((this, key, value) => any)

          A function that transforms the results.

            • (this, key, value): any
            • Parameters

              • this: any
              • key: string
              • value: any

              Returns any

        • Optional space: string | number

          Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.

        Returns string

      • (value, replacer?, space?): string
      • Converts a JavaScript value to a JavaScript Object Notation (JSON) string.

        Parameters

        • value: any

          A JavaScript value, usually an object or array, to be converted.

        • Optional replacer: null | (string | number)[]

          An array of strings and numbers that acts as an approved list for selecting the object properties that will be stringified.

        • Optional space: string | number

          Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.

        Returns string

  • ObjectDefineProperty: (<T>(o, p, attributes) => T)
      • <T>(o, p, attributes): T
      • Adds a property to an object, or modifies attributes of an existing property.

        Type Parameters

        • T

        Parameters

        • o: T

          Object on which to add or modify the property. This can be a native JavaScript object (that is, a user-defined object or a built in object) or a DOM object.

        • p: PropertyKey

          The property name.

        • attributes: PropertyDescriptor & ThisType<any>

          Descriptor for the property. It can be for a data property or an accessor property.

        Returns T

  • Promise: PromiseConstructor
  • TextDecoder: (new (label?, options?) => TextDecoder)
      • new (label?, options?): TextDecoder
      • Parameters

        • Optional label: string
        • Optional options: TextDecoderOptions

        Returns TextDecoder

  • TextEncoder: (new () => TextEncoder)
      • new (): TextEncoder
      • Returns TextEncoder

  • Uint16Array: Uint16ArrayConstructor
  • Uint32Array: Uint32ArrayConstructor
  • Uint8Array: Uint8ArrayConstructor
  • addEventListener: {
        <K>(type, listener, options?): void;
        (type, listener, options?): void;
    } & {
        <K>(type, listener, options?): void;
        (type, listener, options?): void;
    }
  • capturedWebkitHandlers: Record<string, any>
  • decrypt: ((algorithm, key, data) => Promise<ArrayBuffer>)
      • (algorithm, key, data): Promise<ArrayBuffer>
      • Parameters

        • algorithm: AlgorithmIdentifier | RsaOaepParams | AesCtrParams | AesCbcParams | AesGcmParams
        • key: CryptoKey
        • data: BufferSource

        Returns Promise<ArrayBuffer>

  • encrypt: ((algorithm, key, data) => Promise<ArrayBuffer>)
      • (algorithm, key, data): Promise<ArrayBuffer>
      • Parameters

        • algorithm: AlgorithmIdentifier | RsaOaepParams | AesCtrParams | AesCbcParams | AesGcmParams
        • key: CryptoKey
        • data: BufferSource

        Returns Promise<ArrayBuffer>

  • exportKey: {
        (format, key): Promise<JsonWebKey>;
        (format, key): Promise<ArrayBuffer>;
    }
      • (format, key): Promise<JsonWebKey>
      • Parameters

        • format: "jwk"
        • key: CryptoKey

        Returns Promise<JsonWebKey>

      • (format, key): Promise<ArrayBuffer>
      • Parameters

        • format: "pkcs8" | "raw" | "spki"
        • key: CryptoKey

        Returns Promise<ArrayBuffer>

  • generateKey: {
        (algorithm, extractable, keyUsages): Promise<CryptoKeyPair>;
        (algorithm, extractable, keyUsages): Promise<CryptoKey>;
        (algorithm, extractable, keyUsages): Promise<CryptoKey | CryptoKeyPair>;
        (algorithm, extractable, keyUsages): Promise<CryptoKeyPair>;
        (algorithm, extractable, keyUsages): Promise<CryptoKey>;
        (algorithm, extractable, keyUsages): Promise<CryptoKey | CryptoKeyPair>;
    }
      • (algorithm, extractable, keyUsages): Promise<CryptoKeyPair>
      • Parameters

        • algorithm: RsaHashedKeyGenParams | EcKeyGenParams
        • extractable: boolean
        • keyUsages: readonly KeyUsage[]

        Returns Promise<CryptoKeyPair>

      • (algorithm, extractable, keyUsages): Promise<CryptoKey>
      • Parameters

        • algorithm: AesKeyGenParams | HmacKeyGenParams | Pbkdf2Params
        • extractable: boolean
        • keyUsages: readonly KeyUsage[]

        Returns Promise<CryptoKey>

      • (algorithm, extractable, keyUsages): Promise<CryptoKey | CryptoKeyPair>
      • Parameters

        • algorithm: AlgorithmIdentifier
        • extractable: boolean
        • keyUsages: KeyUsage[]

        Returns Promise<CryptoKey | CryptoKeyPair>

      • (algorithm, extractable, keyUsages): Promise<CryptoKeyPair>
      • Parameters

        • algorithm: RsaHashedKeyGenParams | EcKeyGenParams
        • extractable: boolean
        • keyUsages: readonly KeyUsage[]

        Returns Promise<CryptoKeyPair>

      • (algorithm, extractable, keyUsages): Promise<CryptoKey>
      • Parameters

        • algorithm: AesKeyGenParams | HmacKeyGenParams | Pbkdf2Params
        • extractable: boolean
        • keyUsages: readonly KeyUsage[]

        Returns Promise<CryptoKey>

      • (algorithm, extractable, keyUsages): Promise<CryptoKey | CryptoKeyPair>
      • Parameters

        • algorithm: AlgorithmIdentifier
        • extractable: boolean
        • keyUsages: Iterable<KeyUsage>

        Returns Promise<CryptoKey | CryptoKeyPair>

  • getRandomValues: (<T>(array) => T)
      • <T>(array): T
      • Type Parameters

        • T extends null | ArrayBufferView

        Parameters

        • array: T

        Returns T

  • importKey: {
        (format, keyData, algorithm, extractable, keyUsages): Promise<CryptoKey>;
        (format, keyData, algorithm, extractable, keyUsages): Promise<CryptoKey>;
        (format, keyData, algorithm, extractable, keyUsages): Promise<CryptoKey>;
        (format, keyData, algorithm, extractable, keyUsages): Promise<CryptoKey>;
    }
      • (format, keyData, algorithm, extractable, keyUsages): Promise<CryptoKey>
      • Parameters

        • format: "jwk"
        • keyData: JsonWebKey
        • algorithm: AlgorithmIdentifier | RsaHashedImportParams | EcKeyImportParams | HmacImportParams | AesKeyAlgorithm
        • extractable: boolean
        • keyUsages: readonly KeyUsage[]

        Returns Promise<CryptoKey>

      • (format, keyData, algorithm, extractable, keyUsages): Promise<CryptoKey>
      • Parameters

        • format: "pkcs8" | "raw" | "spki"
        • keyData: BufferSource
        • algorithm: AlgorithmIdentifier | RsaHashedImportParams | EcKeyImportParams | HmacImportParams | AesKeyAlgorithm
        • extractable: boolean
        • keyUsages: KeyUsage[]

        Returns Promise<CryptoKey>

      • (format, keyData, algorithm, extractable, keyUsages): Promise<CryptoKey>
      • Parameters

        • format: "jwk"
        • keyData: JsonWebKey
        • algorithm: AlgorithmIdentifier | RsaHashedImportParams | EcKeyImportParams | HmacImportParams | AesKeyAlgorithm
        • extractable: boolean
        • keyUsages: readonly KeyUsage[]

        Returns Promise<CryptoKey>

      • (format, keyData, algorithm, extractable, keyUsages): Promise<CryptoKey>
      • Parameters

        • format: "pkcs8" | "raw" | "spki"
        • keyData: BufferSource
        • algorithm: AlgorithmIdentifier | RsaHashedImportParams | EcKeyImportParams | HmacImportParams | AesKeyAlgorithm
        • extractable: boolean
        • keyUsages: Iterable<KeyUsage>

        Returns Promise<CryptoKey>

  • window: Window & typeof globalThis

Methods

  • When required (such as on macos 10.x), capture the postMessage method on each webkit messageHandler

    Parameters

    • handlerNames: string[]

    Returns void

  • Parameters

    • ciphertext: BufferSource
    • key: BufferSource
    • iv: Uint8Array

    Returns Promise<string>

  • Generate a random method name and adds it to the global scope The native layer will use this method to send the response

    Parameters

    • randomMethodName: string | number
    • callback: Function

    Returns void

  • Parameters

    • name: string
    • Optional data: Record<string, any> = {}

    Returns void

  • Parameters

    • name: string
    • Optional data: Record<string, any> = {}

    Returns Promise<any>

Generated using TypeDoc