Repository navigation
Expand file tree
/
Copy pathOutputBuffer.js
More file actions
87 lines (87 loc) · 2.62 KB
/
Copy pathOutputBuffer.js
File metadata and controls
87 lines (87 loc) · 2.62 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
export class OutputBufferStack {
buffers = [];
start() {
this.buffers.push("");
return true;
}
write(data) {
if (this.buffers.length > 0) {
this.buffers[this.buffers.length - 1] += data;
}
}
getClean() {
if (this.buffers.length === 0)
return false;
return this.buffers.pop() || "";
}
getContents() {
if (this.buffers.length === 0)
return false;
return this.buffers[this.buffers.length - 1];
}
getLength() {
const contents = this.getContents();
return contents === false ? false : Buffer.byteLength(contents, "utf8");
}
getFlush(ctx) {
const contents = this.getContents();
if (contents === false)
return false;
this.flush(ctx);
return contents;
}
flush(ctx) {
if (this.buffers.length === 0)
return false;
const content = this.buffers.pop() || "";
if (this.buffers.length > 0) {
this.buffers[this.buffers.length - 1] += content;
}
else if (ctx) {
ctx.writeStdout(content);
}
return true;
}
endClean() {
if (this.buffers.length === 0)
return false;
this.buffers.pop();
return true;
}
flushAll(ctx) {
while (this.buffers.length > 0) {
this.flush(ctx);
}
}
getLevel() {
return this.buffers.length;
}
isActive() {
return this.buffers.length > 0;
}
}
export class OutputBufferRuntime {
static functions = {
"flush": async (ctx) => { ctx.flushHeaders(); return true; },
"ob_start": async (ctx) => ctx.outputBuffer.start(),
"ob_get_clean": async (ctx) => ctx.outputBuffer.getClean(),
"ob_get_contents": async (ctx) => ctx.outputBuffer.getContents(),
"ob_get_length": async (ctx) => ctx.outputBuffer.getLength(),
"ob_get_flush": async (ctx) => ctx.outputBuffer.getFlush(ctx),
"ob_flush": async (ctx) => ctx.outputBuffer.flush(ctx),
"ob_end_flush": async (ctx) => ctx.outputBuffer.flush(ctx),
"ob_end_clean": async (ctx) => ctx.outputBuffer.endClean(),
"ob_clean": async (ctx) => {
if (ctx.outputBuffer.isActive()) {
ctx.outputBuffer.getClean();
ctx.outputBuffer.start();
}
return true;
},
"ob_get_level": async (ctx) => ctx.outputBuffer.getLevel(),
};
static register(engine) {
engine.registerFunctions(OutputBufferRuntime.functions);
}
}
//# sourceMappingURL=OutputBuffer.js.map