Created
July 25, 2018 23:50
-
-
Save mwgamera/42da1b6196aff32866a4e940c6455177 to your computer and use it in GitHub Desktop.
Native messaging host
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# klg, Jun 2017 | |
use strict; | |
use JSON; | |
my $j = JSON->new->utf8->allow_nonref; | |
my $c = 0; | |
local $| = 1; | |
while (1) { | |
sysread \*STDIN, $_, 4 or last; | |
sysread \*STDIN, $_, unpack 'L' or die $!; | |
my $msg = $j->decode($_); | |
print pack 'L/a', $j->encode({ | |
echo => $msg, | |
count => ++$c, | |
argv => \@ARGV, | |
a => chr 261, | |
v => "perl $^V", | |
pid => $$ | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# klg, May 2018 | |
import os | |
import sys | |
import struct | |
import json | |
stdin = os.fdopen(sys.stdin.fileno(), 'rb', 0) | |
stdout = os.fdopen(sys.stdout.fileno(), 'wb', 0) | |
try: | |
chr = unichr | |
except: | |
pass | |
cnt = 0 | |
while True: | |
tag = stdin.read(4) | |
if not tag: | |
sys.exit() | |
length = struct.unpack('=L', tag)[0] | |
msg = json.loads(stdin.read(length)) | |
cnt += 1 | |
res = json.dumps({ | |
'echo': msg, | |
'count': cnt, | |
'argv': sys.argv, | |
'a': chr(261), | |
'v': 'python '+sys.version, | |
'pid': os.getpid(), | |
}) | |
res = res.encode('UTF-8') | |
stdout.write(struct.pack('=L', len(res)) + res) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# klg, May 2018 | |
require 'json' | |
cnt = 0 | |
loop do | |
len = $stdin.sysread(4).unpack('L')[0] | |
msg = JSON.parse($stdin.sysread(len)) | |
res = JSON.generate({ | |
echo: msg, | |
count: cnt += 1, | |
argv: ARGV, | |
a: [261].pack('U'), | |
v: "ruby #{RUBY_VERSION}", | |
pid: Process.pid | |
}) | |
$stdout.syswrite([res.bytes.length, res].pack 'La*') | |
rescue EOFError | |
exit | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that Python version above works just fine with either Python 2 or 3; there's no need to make is as ridiculous as they did in example at MDN.