Initial commit

This commit is contained in:
2023-12-24 11:38:32 +01:00
commit f71174d884
1963 changed files with 24504 additions and 0 deletions

17
api/hooks/config.js Normal file
View File

@@ -0,0 +1,17 @@
module.exports = {
projectName: "__PROJECT_NAME__",
operatorEmail: "__OPERATOR_EMAIL_",
apiBase: "http://localhost:8080/api/v1/_/__NAMESPACE__/",
frontendBase: "http://localhost:5501/",
publicToken: "__PUBLIC_TOKEN__",
ssrToken: "__SSR_TOKEN__",
ssrValidatePath: function(path) {
// TODO validate if path ssr rendering is ok, -1 = NOTFOUND, 0 = NO SSR, 1 = SSR
// pe. use context.readCollection("product", {filter: {path: path}}) ... to validate dynamic urls
return path.match(/^\/(home|service)\/?$/)
}
}

View File

@@ -0,0 +1,68 @@
// @ts-check
let utils = require("../lib/utils")
const config = require("../config")
const { operatorEmail } = config
// const { objectToText } = require("../lib/helper")
;(function () {
/** @type {import("tibi-types").HookResponse} */
let hookResponse
const type = context.request().query("type")
if (!type) {
throw {
status: 403,
error: "invalid data",
}
}
if (typeof context.data != "object") {
throw {
status: 400,
error: "invalid body data",
}
}
let to = ""
let from = ""
let fromName = ""
let replyTo = ""
let subject
let plainText
let html
if (utils.isPublicToken(context)) {
to = operatorEmail
from = context.data.email
fromName = context.data.name
replyTo = context.data.email
if (type === "contactForm") {
subject = utils.tpl(context, "templates/operator_contact_form_subject.de.txt")
html = utils.tpl(context, "templates/operator_contact_form_body.de.html")
}
}
if ((!plainText && !html) || !subject) {
throw {
status: 403,
error: "invalid mail data",
}
}
context.smtp.sendMail({
to,
from,
fromName,
subject,
html,
// attach: ["attachments/AGB.pdf"],
})
throw {
status: 200,
message: "ok",
}
return hookResponse
})()

View File

@@ -0,0 +1,11 @@
// @ts-check
const { generateUrlString } = require("../lib/helper")
;(function () {
/** @type {import("tibi-types").HookResponse} */
let hookResponse
context.data.path = generateUrlString(context.data.path)
return hookResponse
})()

View File

@@ -0,0 +1,11 @@
// @ts-check
const { generateUrlString } = require("../lib/helper")
;(function () {
/** @type {import("tibi-types").HookResponse} */
let hookResponse
context.data.path = generateUrlString(context.data.path)
return hookResponse
})()

14
api/hooks/lib/helper.js Normal file
View File

@@ -0,0 +1,14 @@
const generateUrlString = (text) => {
if (text) {
return text
.replace(/[^a-zA-Z0-9 \/]/g, "")
.replace(/\s/g, "-")
.toLowerCase()
}
return ""
}
module.exports = {
generateUrlString,
}

37
api/hooks/lib/ssr.js Normal file
View File

@@ -0,0 +1,37 @@
/**
* convert object to string
* @param {any} obj object
*/
function obj2str(obj) {
if (Array.isArray(obj)) {
return JSON.stringify(
obj.map(function (idx) {
return obj2str(idx)
})
)
} else if (typeof obj === "object" && obj !== null) {
var elements = Object.keys(obj)
.sort()
.map(function (key) {
var val = obj2str(obj[key])
if (val) {
return key + ":" + val
}
})
var elementsCleaned = []
for (var i = 0; i < elements.length; i++) {
if (elements[i]) elementsCleaned.push(elements[i])
}
return "{" + elementsCleaned.join("|") + "}"
}
if (obj) return obj
}
// can be used by client code, so DONT INCLUDE hooks/config.js (SECRETS INSIDE)
module.exports = {
obj2str,
}

216
api/hooks/lib/utils.js Normal file
View File

@@ -0,0 +1,216 @@
// @ts-check
var config = require("../config")
/**
*
* @param {any} str
*/
function log(str) {
console.log(JSON.stringify(str, undefined, 4))
}
function rand() {
return Math.random().toString(36).substr(2) // remove `0.`
}
/**
* @returns {string}
*/
function randomToken() {
return rand() + rand()
}
/**
*
* @param {import('tibi-types').HookContext} c
* @returns {boolean}
*/
function isPublicToken(c) {
var r = c.request()
var t = r.query("token") || r.header("token")
return t == config.publicToken
}
/**
*
* @param {import('tibi-types').HookContext} c
* @returns {boolean}
*/
function isSsrToken(c) {
var r = c.request()
var t = r.query("token") || r.header("token")
return t == config.ssrToken
}
/**
*
* @param {import('tibi-types').HookContext} c
* @param {string} filename
* @returns {string}
*/
function tpl(c, filename) {
return c.tpl.execute(c.fs.readFile(filename), {
context: c,
config: config,
/**
* @param {number} v
* @param {number} vat
*/
formatPrice: function (v, vat) {
if (vat) {
v *= vat / 100 + 1
}
return v.toFixed(2).toString().replace(".", ",")
},
})
}
/**
*
* Base64 encode / decode
* http://www.webtoolkit.info/
*
**/
var Base64 = {
// private property
_keyStr:
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
// public method for encoding
encode: function (input) {
var output = ""
var chr1, chr2, chr3, enc1, enc2, enc3, enc4
var i = 0
input = Base64._utf8_encode(input)
while (i < input.length) {
chr1 = input.charCodeAt(i++)
chr2 = input.charCodeAt(i++)
chr3 = input.charCodeAt(i++)
enc1 = chr1 >> 2
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4)
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6)
enc4 = chr3 & 63
if (isNaN(chr2)) {
enc3 = enc4 = 64
} else if (isNaN(chr3)) {
enc4 = 64
}
output =
output +
this._keyStr.charAt(enc1) +
this._keyStr.charAt(enc2) +
this._keyStr.charAt(enc3) +
this._keyStr.charAt(enc4)
}
return output
},
// public method for decoding
decode: function (input) {
var output = ""
var chr1, chr2, chr3
var enc1, enc2, enc3, enc4
var i = 0
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "")
while (i < input.length) {
enc1 = this._keyStr.indexOf(input.charAt(i++))
enc2 = this._keyStr.indexOf(input.charAt(i++))
enc3 = this._keyStr.indexOf(input.charAt(i++))
enc4 = this._keyStr.indexOf(input.charAt(i++))
chr1 = (enc1 << 2) | (enc2 >> 4)
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2)
chr3 = ((enc3 & 3) << 6) | enc4
output = output + String.fromCharCode(chr1)
if (enc3 != 64) {
output = output + String.fromCharCode(chr2)
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3)
}
}
output = Base64._utf8_decode(output)
return output
},
// private method for UTF-8 encoding
_utf8_encode: function (string) {
string = string.replace(/\r\n/g, "\n")
var utftext = ""
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n)
if (c < 128) {
utftext += String.fromCharCode(c)
} else if (c > 127 && c < 2048) {
utftext += String.fromCharCode((c >> 6) | 192)
utftext += String.fromCharCode((c & 63) | 128)
} else {
utftext += String.fromCharCode((c >> 12) | 224)
utftext += String.fromCharCode(((c >> 6) & 63) | 128)
utftext += String.fromCharCode((c & 63) | 128)
}
}
return utftext
},
// private method for UTF-8 decoding
_utf8_decode: function (utftext) {
var string = ""
var i = 0
var c = 0
var c1 = 0
var c2 = 0
var c3 = 0
while (i < utftext.length) {
c = utftext.charCodeAt(i)
if (c < 128) {
string += String.fromCharCode(c)
i++
} else if (c > 191 && c < 224) {
c2 = utftext.charCodeAt(i + 1)
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63))
i += 2
} else {
c2 = utftext.charCodeAt(i + 1)
c3 = utftext.charCodeAt(i + 2)
string += String.fromCharCode(
((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)
)
i += 3
}
}
return string
},
}
/**
*
* @param {string} d
*/
function parseDate(d) {
return new Date(
d
.toString()
// go time objects output is not parseable by goja, so fix it
.replace(
/[T ](\d{2}:\d{2}:\d{2})(?:\.\d+)? ([\+\-])(\d{4}).*$/,
"T$1$2$3"
)
)
}
/**
* clear SSR cache
*/
function clearSSRCache() {
var info = context.db.deleteMany("ssr", {})
context.response.header("X-SSR-Cleared", info.removed)
}
module.exports = {
log,
randomToken,
isPublicToken,
isSsrToken,
tpl: tpl,
Base64,
parseDate,
clearSSRCache,
ssrValidatePath: config.ssRValidatePath,
}

208
api/hooks/ssr/get_read.js Normal file
View File

@@ -0,0 +1,208 @@
// @ts-check
var utils = require("../lib/utils")
;(function () {
var request = context.request()
var url = request.query("url")
var noCache = request.query("noCache")
var trace_id = context.debug.sentryTraceId()
function addSentryTrace(content) {
return content.replace(
"</head>",
'<meta name="sentry-trace" content="' + trace_id + '" /></head>'
)
}
context.response.header("sentry-trace", trace_id)
if (url) {
var comment = ""
url = url.split("?")[0]
comment += "url: " + url
if (url && url.length > 1) {
url = url.replace(/\/$/, "")
}
if (url == "/noindex" || !url) {
url = "/" // see .htaccess
}
var cache =
!noCache &&
context.db.find("ssr", {
filter: {
path: url,
},
})
if (cache && cache.length) {
// use cache
throw {
status: 200,
html: addSentryTrace(cache[0].content),
}
}
// validate url
var status = 200
var pNorender = false
var pNotfound = false
var pR = utils.ssrValidatePath(url)
if (pR < 0) {
pNotfound = true
} else if (!pR) {
pNorender = true
}
var head = ""
var html = ""
var error = ""
comment += ", path: " + url
var cacheIt = false
if (pNorender) {
html = "<!-- NO SSR RENDERING -->"
} else if (pNotfound) {
status = 404
html = "404 NOT FOUND"
} else {
try {
// if error, output plain html without prerendering
// @ts-ignore
context.ssrCache = {}
// @ts-ignore
context.ssrFetch = function (endpoint, options) {
var data
if (
endpoint == "product" ||
endpoint == "category" ||
endpoint == "country" ||
endpoint == "content"
) {
var _options = Object.assign({}, options)
if (_options.sort) _options.sort = [_options.sort]
try {
/*console.log(
"SSR",
endpoint,
JSON.stringify(_options)
)*/
var goSlice = context.db.find(
endpoint,
_options || {}
)
// need to deep copy, so shift and delete on pure js is possible
data = JSON.parse(JSON.stringify(goSlice))
} catch (e) {
console.log("ERROR", JSON.stringify(e))
data = []
}
} else {
console.log("SSR forbidden", endpoint)
data = []
}
var count = (data && data.length) || 0
if (options && count == options.limit) {
// read count from db
count = context.db.count(endpoint, _options || {})
}
var r = { data: data, count: count }
// @ts-ignore
context.ssrCache[
utils.obj2str({ endpoint: endpoint, options: options })
] = r
return r
}
// @ts-ignore
var app = require("../lib/app.server")
var rendered = app.default.render({
url: url,
})
head = rendered.head
html = rendered.html
head +=
"\n\n" +
"<script>window.__SSR_CACHE__ = " +
// @ts-ignore
JSON.stringify(context.ssrCache) +
"</script>"
// status from webapp
// @ts-ignore
if (context.is404) {
status = 404
} else {
cacheIt = true
}
} catch (e) {
utils.log(e.message)
utils.log(e.stack)
error = "error: " + e.message + "\n\n" + e.stack
}
}
var tpl = context.fs.readFile("templates/spa.html")
tpl = tpl.replace("<!--HEAD-->", head)
tpl = tpl.replace("<!--HTML-->", html)
tpl = tpl.replace(
"<!--SSR.ERROR-->",
error ? "<!--" + error + "-->" : ""
)
tpl = tpl.replace(
"<!--SSR.COMMENT-->",
comment ? "<!--" + comment + "-->" : ""
)
if (cacheIt && !noCache) {
// save cache
context.db.create("ssr", {
path: url,
content: tpl,
})
}
tpl.replace(
"</head>",
'<meta name="sentry-trace" content="' + trace_id + '" /></head>'
)
throw {
status: status,
html: addSentryTrace(tpl),
}
} else {
var auth = context.user.auth()
if (!auth || auth.role !== 0) {
// only admins are allowed
throw {
status: 403,
message: "invalid auth",
auth: auth,
}
}
}
})()
/*
require("../lib/hook.test")
console.log("hook test ende")
throw {
status: 500,
msg: "TEST",
}
*/

View File

@@ -0,0 +1,8 @@
// @ts-check
;(function () {
throw {
status: 500,
message: "ssr is only a dummy collection",
}
})()