@micro-frame/marko
Version:
A Marko tag for building SSR friendly micro frontends.
79 lines (78 loc) • 1.92 kB
JavaScript
var __defProp = Object.defineProperty;
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
var __export = (target, all) => {
__markAsModule(target);
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
__export(exports, {
default: () => parser_default
});
var parser_default = (read) => function parser(iterator) {
let buf = "";
let isDone = false;
function extractOneEventFromBuf() {
const index = buf.indexOf("\n\n");
if (index > -1) {
const eventStr = buf.substring(0, index);
buf = buf.substring(index + 2);
return eventStr;
} else if (isDone) {
const ret = buf;
buf = "";
return ret;
}
return;
}
return {
async next() {
while (true) {
const eventStr = extractOneEventFromBuf();
if (eventStr) {
const ev = parseEvent(eventStr);
return {
value: read ? read(ev) : [ev.lastEventId, ev.data, true],
done: false
};
}
if (isDone) {
return {
value: void 0,
done: true
};
} else {
const { value, done } = await iterator.next();
if (done) {
isDone = true;
} else {
buf += value.toString();
}
}
}
}
};
};
function parseEvent(eventStr) {
const ev = {
id: void 0,
data: "",
event: void 0
};
eventStr.split("\n").forEach((line) => {
const lineMatch = /^(id|event|data): (.*)$/.exec(line);
if (lineMatch) {
if (lineMatch[1] === "data") {
ev[lineMatch[1]] += lineMatch[2];
} else {
ev[lineMatch[1]] = lineMatch[2];
}
}
});
return {
lastEventId: ev.id,
data: ev.data,
type: ev.event
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {});