How do I create BYTEA from JavaScript array?

1

How do I create BYTEA from JavaScript array ? According to PL/V8 doc JS arrays should be converted to BYTEA but it's not happening for me.

For example consider following function

CREATE OR REPLACE FUNCTION test2(input bytea) RETURNS bytea AS $$
    var arr = [255, 254];
    return arr;
$$ LANGUAGE plv8 IMMUTABLE STRICT;

It returns 28 (!!!) bytes that looks like this

E'\\x020000400800009008000010200000000080FF00200000000080FE00'

Tested on Amazon RDS with Postgresql 9.5 with PLV8 1.4.3

What am I missing ?

Update:

Looks like I figured out how it creates BYTEA. For example for this function

CREATE OR REPLACE FUNCTION test2(input bytea) RETURNS bytea AS $$
    return '          '.split('');
$$ LANGUAGE plv8 IMMUTABLE STRICT;

It produces E'\\x0A000040 01000080 01000000 01000000 01000000 01000000 01000000 01000000 01000000 01000000 01000000 20202020202020202020';

So it contains length in lower bytes of first DWORD (which for some reason has 0x40 in higher byte), which is followed by 0x80000001 and then followed by Len-1 copies of 0x1. Then it's followed by bytes of actual source string.

javascript
postgresql
plv8
asked on Stack Overflow Oct 25, 2016 by expert • edited Oct 25, 2016 by expert

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0