Skip to content

Commit eb2d207

Browse files
committed
plugin: make compatible with fylr stdin support for export / export transport
see #77710
1 parent 2b26705 commit eb2d207

File tree

3 files changed

+121
-6
lines changed

3 files changed

+121
-6
lines changed

manifest.master.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,23 @@ extensions:
229229
# built-in callbacks which happen at certain points in the code flow of fylr server
230230
callbacks:
231231
export_transport:
232+
copy_file_stdin:
233+
exec:
234+
timeoutSec: 60
235+
service: "node"
236+
commands:
237+
- prog: "node"
238+
# info.json does not contain "export"
239+
stdin:
240+
type: body
241+
stdout:
242+
type: body
243+
args:
244+
- type: "value"
245+
value: "%_exec.pluginDir%/server/export_transport/copy_file.js"
246+
- type: "value"
247+
# info.json does not contain "export"
248+
value: "%info.json%"
232249
copy_file:
233250
exec:
234251
timeoutSec: 60
@@ -241,6 +258,7 @@ callbacks:
241258
- type: "value"
242259
value: "%_exec.pluginDir%/server/export_transport/copy_file.js"
243260
- type: "value"
261+
# info.json contains "export"
244262
value: "%info.json%"
245263
module_missing:
246264
exec:
@@ -285,6 +303,8 @@ callbacks:
285303
service: "node"
286304
commands:
287305
- prog: "node"
306+
stdin:
307+
type: "body"
288308
stdout:
289309
type: body
290310
args:

server/export/md5_sums.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,42 @@ const fs = require('fs');
22
const https = require('http');
33
const crypto = require('crypto');
44

5+
6+
// Start reading stdin immediately
7+
let stdinInput = '';
8+
let stdinEnded = false;
9+
let stdinResolve = null;
10+
11+
process.stdin.setEncoding('utf8');
12+
process.stdin.on('data', (chunk) => (stdinInput += chunk));
13+
process.stdin.on('end', () => {
14+
stdinEnded = true;
15+
if (stdinResolve) stdinResolve();
16+
});
17+
18+
function getStdinData() {
19+
return new Promise((resolve) => {
20+
const parseAndResolve = () => {
21+
if (!stdinInput) {
22+
resolve(null);
23+
} else {
24+
try {
25+
resolve(JSON.parse(stdinInput));
26+
} catch (e) {
27+
console.error('Failed to parse stdin JSON:', e.message);
28+
resolve(null);
29+
}
30+
}
31+
};
32+
33+
if (stdinEnded) {
34+
parseAndResolve();
35+
} else {
36+
stdinResolve = parseAndResolve;
37+
}
38+
});
39+
}
40+
541
const getMD5FromURL = async (url) => {
642
const md5sum = crypto.createHash('md5');
743
return new Promise(
@@ -40,6 +76,12 @@ try {
4076
}
4177

4278
(async() => {
79+
// Read stdin and add it to info
80+
const stdinData = await getStdinData();
81+
info.stdin = stdinData
82+
if (stdinData) {
83+
info.export = stdinData.export
84+
}
4385

4486
if (info.plugin_action == "produce?infos.json") {
4587
for (let i = 0, j = info.export._files.length; i < j; i++) {

server/export_transport/copy_file.js

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,41 @@ const fs = require('fs');
1919
const http = require('http');
2020
const https = require('https');
2121

22+
// Start reading stdin immediately
23+
let stdinInput = '';
24+
let stdinEnded = false;
25+
let stdinResolve = null;
26+
27+
process.stdin.setEncoding('utf8');
28+
process.stdin.on('data', (chunk) => (stdinInput += chunk));
29+
process.stdin.on('end', () => {
30+
stdinEnded = true;
31+
if (stdinResolve) stdinResolve();
32+
});
33+
34+
function getStdinData() {
35+
return new Promise((resolve) => {
36+
const parseAndResolve = () => {
37+
if (!stdinInput) {
38+
resolve(null);
39+
} else {
40+
try {
41+
resolve(JSON.parse(stdinInput));
42+
} catch (e) {
43+
console.error('Failed to parse stdin JSON:', e.message);
44+
resolve(null);
45+
}
46+
}
47+
};
48+
49+
if (stdinEnded) {
50+
parseAndResolve();
51+
} else {
52+
stdinResolve = parseAndResolve;
53+
}
54+
});
55+
}
56+
2257
if (process.argv.length < 3) {
2358
console.error(`Wrong number of parameters. Usage: "node export_transport_demo.js <info>"`)
2459
process.exit(1);
@@ -70,22 +105,40 @@ const sendDataToURL = async (url, data) => {
70105
}
71106

72107
(async() => {
108+
// Read stdin and add it to info
109+
const stdinData = await getStdinData();
110+
if (stdinData !== null) {
111+
info.stdin = stdinData;
112+
}
113+
114+
let hasError = false;
115+
73116
if (tOpts.url) {
74-
await sendDataToURL(tOpts.url, JSON.stringify(info));
75-
tLog.push(`data sent: ${tOpts.url}`)
117+
try {
118+
await sendDataToURL(tOpts.url, JSON.stringify(info));
119+
tLog.push(`data sent: ${tOpts.url}`)
120+
} catch (e) {
121+
tLog.push(`failed to send data to ${tOpts.url}: ${e.message}`)
122+
hasError = true;
123+
}
76124
}
77125
if (tOpts.file) {
78-
fs.writeFileSync(tOpts.file, JSON.stringify(info,""," "));
79-
tLog.push(`file written: ${tOpts.file}`)
126+
try {
127+
fs.writeFileSync(tOpts.file, JSON.stringify(info,""," "));
128+
tLog.push(`file written: ${tOpts.file}`)
129+
} catch (e) {
130+
tLog.push(`failed to write file ${tOpts.file}: ${e.message}`)
131+
hasError = true;
132+
}
80133
}
81134
if (tOpts["pw:secret"]) {
82135
tLog.push(`pw:secret: `+ tOpts["pw:secret"])
83136
}
84137
// write back modified export json
85138
console.log(JSON.stringify({
86-
"_state": "done",
139+
"_state": hasError ? "failed" : "done",
87140
"_transport_log": tLog
88141
}));
89-
process.exit(0);
142+
process.exit(hasError ? 1 : 0);
90143
})();
91144

0 commit comments

Comments
 (0)