-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.code.js
More file actions
62 lines (52 loc) · 1.44 KB
/
index.code.js
File metadata and controls
62 lines (52 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const fs = require('fs')
const path = require('path')
/**
* @typedef {import('@lamaani/stratis').StratisApiHandler} StratisApiHandler
*/
/** @type {StratisApiHandler} */
async function print_something({ to_print = '[undefined]' }, context) {
return `something ${to_print}`
}
/**
* Executes a request to another server on serverside.
* @type {StratisApiHandler}
* */
async function get_page({ url = 'https://www.google.com' }, context) {
return await (await context.requests.get(url)).to_string()
}
/**
* Executes a request to another server on serverside.
* @type {StratisApiHandler}
* */
async function get_page_binary({ url = 'https://www.google.com' }, context) {
return await context.requests.get(url)
}
/**
* Example for uploading a file. The command should be sent with content-type='binary'.
* @type {StratisApiHandler}
* */
async function upload_file({ fname = null }, context) {
fname = fname || 'test_write_upload.txt'
const fpath = path.join('/tmp', fname)
const encoding = context.req.headers['content-encoding'] || 'utf-8'
const ws = fs.createWriteStream(fpath, encoding)
context.req.pipe(ws)
await new Promise((res, rej) => {
context.req.on('end', () => {
res()
})
context.req.on('error', (err) => {
rej(err)
})
})
return await fs.promises.readFile(fpath, encoding)
}
module.exports = {
a_value: {
some: 'value',
},
print_something,
get_page,
upload_file,
get_page_binary,
}