Skip to content

Commit 6ea4d18

Browse files
committed
Implement http* in a JSPython-cli #185
1 parent 4e27ec3 commit 6ea4d18

File tree

3 files changed

+131
-2
lines changed

3 files changed

+131
-2
lines changed

rollup.config.dev.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import typescript from 'rollup-plugin-typescript2';
2+
const pkg = require('./package.json');
23

34
export default {
45

56
input: 'src/index.ts',
67
output: {
78
name: 'JSPython-cli',
8-
file: 'bin/JSPython-cli',
9+
file: pkg.bin['jspython'],
910
format: 'cjs',
1011
sourcemap: true,
1112
banner: '#!/usr/bin/env node'

src/http.ts

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import https from 'https';
2+
import { OutgoingHttpHeaders, IncomingMessage, ClientRequest } from 'http';
3+
import http from 'http';
4+
5+
export function httpGet(url: string, headers?: OutgoingHttpHeaders): Promise<any> {
6+
return new Promise((resolve, reject) => {
7+
const options: https.RequestOptions = {}
8+
if (headers) {
9+
options.headers = headers;
10+
}
11+
https.get(url, options, (resp) => {
12+
let data: string = '';
13+
resp.on('data', (chunk) => {
14+
data += chunk;
15+
});
16+
17+
resp.on('end', () => {
18+
resolve(formatResponseData(resp, data));
19+
});
20+
}).on('error', (err) => {
21+
reject(err);
22+
});
23+
});
24+
}
25+
26+
export function httpPost(url: string, body: any, headers?: object): Promise<any> {
27+
return new Promise((resolve, reject) => {
28+
const data = typeof body !== 'string' ? JSON.stringify(body) : body;
29+
30+
const {hostname, port, pathname} = new URL(url);
31+
const options = {
32+
hostname, port, pathname,
33+
method: 'POST',
34+
headers: {
35+
'Content-Type': 'application/json',
36+
'Content-Length': data.length
37+
}
38+
}
39+
40+
if (headers) {
41+
Object.assign(options.headers, headers);
42+
}
43+
44+
const req = createReq(options, resolve, reject);
45+
req.write(data);
46+
req.end();
47+
})
48+
}
49+
50+
export function httpPut(url: string, body: any, headers?: object): Promise<any> {
51+
return new Promise((resolve, reject) => {
52+
const data = typeof body !== 'string' ? JSON.stringify(body) : body;
53+
54+
const {hostname, port, pathname} = new URL(url);
55+
56+
const options = {
57+
hostname, port: 443, pathname,
58+
method: 'PUT',
59+
headers: {
60+
'Content-Type': 'application/json',
61+
'Content-Length': data.length
62+
}
63+
}
64+
65+
if (headers) {
66+
Object.assign(options.headers, headers);
67+
}
68+
69+
const req = createReq(options, resolve, reject);
70+
req.write(data);
71+
req.end();
72+
})
73+
}
74+
75+
export function httpDelete(url: string, headers?: object): Promise<any> {
76+
return new Promise((resolve, reject) => {
77+
const {hostname, port, pathname} = new URL(url);
78+
79+
const options: any = {
80+
hostname, port, pathname,
81+
method: 'DELETE',
82+
}
83+
84+
if (headers) {
85+
Object.assign(options.headers, headers);
86+
}
87+
88+
const req = createReq(options, resolve, reject);
89+
req.end();
90+
})
91+
}
92+
93+
function createReq(options: https.RequestOptions, resolve: Function, reject: Function): ClientRequest {
94+
const req = http.request(options, (res) => {
95+
let data: string = '';
96+
res.on('data', (chunk) => {
97+
data += chunk;
98+
});
99+
100+
res.on('end', () => {
101+
resolve(formatResponseData(res, data));
102+
});
103+
})
104+
105+
req.on('error', (error) => {
106+
reject(error);
107+
});
108+
109+
return req;
110+
}
111+
112+
function formatResponseData(resp: IncomingMessage, data: string) {
113+
if (resp.headers['content-type']?.includes('application/json')) {
114+
try {
115+
data = JSON.parse(data);
116+
} catch (e) {
117+
console.error(e);
118+
} finally {
119+
return data
120+
}
121+
}
122+
123+
return data;
124+
}

src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import arg from 'arg';
22
import fs from 'fs';
33
import { jsPython, Interpreter, PackageLoader } from 'jspython-interpreter';
4+
import { httpGet, httpPost, httpDelete, httpPut } from './http';
45

56
const pkg = require('../package.json');
67

78
export const interpreter: Interpreter = jsPython() as Interpreter;
8-
9+
interpreter.addFunction('httpGet', httpGet);
10+
interpreter.addFunction('httpPost', httpPost);
11+
interpreter.addFunction('httpDelete', httpDelete);
12+
interpreter.addFunction('httpPut', httpPut);
913
run();
1014

1115
async function run() {

0 commit comments

Comments
 (0)