CREATE FUNCTION valid_arraybuffer_bytea(len integer) RETURNS bytea
LANGUAGE plv8 IMMUTABLE STRICT
AS $$
  return new ArrayBuffer(len);
$$;

SELECT length(valid_arraybuffer_bytea(20));

CREATE FUNCTION valid_int8array_bytea(len integer) RETURNS bytea
LANGUAGE plv8 IMMUTABLE STRICT
AS $$
  return new Int8Array(len);
$$;

SELECT length(valid_int8array_bytea(20));

CREATE FUNCTION valid_int16array_bytea(len integer) RETURNS bytea
LANGUAGE plv8 IMMUTABLE STRICT
AS $$
  return new Int16Array(len);
$$;

SELECT length(valid_int16array_bytea(20));

DO $$
  const test = 'string test'
  const bytea = plv8.execute(`select $1::bytea`, [test])[0].bytea;
  const result = String.fromCharCode.apply(null, bytea);
  if (result === test) {
    plv8.elog(INFO, 'OK');
  } else {
    plv8.elog(WARN, 'FAIL');
  }
$$ language plv8;
