This commit is contained in:
2021-03-22 15:59:05 +01:00
parent dd27483b16
commit 626e83d010
46 changed files with 5636 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,25 @@
// @ts-check
/**
* @typedef {import('../types') }
*/
var utils = require("../lib/utils")
;(function () {
if (utils.isPublicToken(context)) {
// js captcha
var checksum = context.request().query("cs")
var email = context.data.email
if (!email || (email.length * 1000).toString(16) + "x" !== checksum) {
throw {
status: 403,
error: "forbidden data",
}
}
}
/** @type {import('../types').HookResponse} */
// @ts-ignore
var response = null
return response
})()

View File

@@ -0,0 +1,37 @@
// @ts-check
/**
* @typedef {import('../types') }
*/
var config = require("../config")
var utils = require("../lib/utils")
;(function () {
if (utils.isPublicToken(context)) {
var emailFrom = context.data.email
var emailFromName =
(context.data.firstname || "") +
(context.data.firstname && context.data.lastname && " ") +
(context.data.lastname || "")
context.mail({
to: config.operatorEmail,
from: emailFrom,
fromName: emailFromName,
subject: utils.tpl(
context,
"templates/operator_contact_form_subject.de.txt"
),
html: utils.tpl(
context,
"templates/operator_contact_form_body.de.html"
),
// attach: ["attachments/AGB.pdf"],
})
}
/** @type {import('../types').HookResponse} */
// @ts-ignore
var response = null
return response
})()

View File

@@ -0,0 +1,10 @@
// @ts-check
/**
* @typedef {import('../types') }
*/
var utils = require("../lib/utils")
;(function () {
utils.clearSSRCache()
})()

View File

@@ -0,0 +1,10 @@
// @ts-check
/**
* @typedef {import('../types') }
*/
var utils = require("../lib/utils")
;(function () {
utils.clearSSRCache()
})()

View File

@@ -0,0 +1,10 @@
// @ts-check
/**
* @typedef {import('../types') }
*/
var utils = require("../lib/utils")
;(function () {
utils.clearSSRCache()
})()

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

@@ -0,0 +1,253 @@
// @ts-check
/**
* @typedef {import('../types') }
*/
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('../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('../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('../types').HookContext} c
* @param {string} filename
* @returns {string}
*/
function tpl(c, filename) {
return c.template(c.file(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.deleteDocuments("ssr", {})
context.header("X-SSR-Cleared", info.removed)
}
/**
* 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
}
module.exports = {
log: log,
randomToken: randomToken,
isPublicToken: isPublicToken,
isSsrToken: isSsrToken,
tpl: tpl,
Base64: Base64,
parseDate: parseDate,
clearSSRCache: clearSSRCache,
obj2str: obj2str,
ssrValidatePath: config.ssRValidatePath,
}

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

@@ -0,0 +1,210 @@
// @ts-check
/**
* @typedef {import('../types') }
*/
var utils = require("../lib/utils")
;(function () {
var request = context.request()
var url = request.query("url")
var noCache = request.query("noCache")
var trace_id = context.sentryTraceId()
function addSentryTrace(content) {
return content.replace(
"</head>",
'<meta name="sentry-trace" content="' + trace_id + '" /></head>'
)
}
context.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.readCollection("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.readCollection(
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.readCollectionCount(
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>"
cacheIt = true
} catch (e) {
utils.log(e)
for (var property in e) {
utils.log(property + ": " + e[property])
}
error = JSON.stringify(e)
}
}
var tpl = context.file("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.createDocument("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.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,11 @@
// @ts-check
/**
* @typedef {import('../types') }
*/
;(function () {
throw {
status: 500,
message: "ssr is only a dummy collection",
}
})()

247
api/hooks/types.d.ts vendored Normal file
View File

@@ -0,0 +1,247 @@
export interface CollectionDocument {
id?: string
insertTime?: Date
updateTime?: Date
[key: string]: any
}
export interface ReadCollectionOptions {
filter?: {
[key: string]: any
}
selector?: {
[key: string]: any
}
projection?: string
offset?: number
limit?: number
sort?: string[]
}
interface GetHookData {
/**
* true if only one document was requested via /COLLECTION/ID
*/
one?: boolean
/**
* get list of documents (only valid after stage "read" in "get" hook)
*/
results(): CollectionDocument[]
/**
* filter map only valid for "get" hooks
*/
filter?: {
[key: string]: any
}
/**
* selector map only valid for "get" hooks
*/
selector?: {
[key: string]: any
}
/**
* offset only valid for "get" hooks
*/
offset?: number
/**
* limit only valid for "get" hooks
*/
limit?: number
/**
* sort only valid for "get" hooks
*/
sort?: string[] | string
}
interface PostHookData {
/**
* post data only valid in "post" and "put" hooks
*/
data?: CollectionDocument
}
export interface HookContext extends GetHookData, PostHookData {
request(): {
method: string
remoteAddr: string
host: string
url: string
path: string
param(p: string): string
query(q: string): string
header(h: string): string
body(): string
}
/**
* read results from a collection
*
* @param colName collection name
* @param options options map
*/
readCollection(
colName: string,
options?: ReadCollectionOptions
): CollectionDocument[]
/**
* read count of documents for filter from a collection
*
* @param colName collection name
* @param options options map (only filter is valid)
*/
readCollectionCount(
colName: string,
options?: ReadCollectionOptions
): number
/**
* create a document in a collection
*
* @param colName collection name
* @param data data map
*/
createDocument(
colName: string,
data: CollectionDocument
): CollectionDocument
/**
* update a document in a collection
*
* @param colName collection name
* @param id id of entry
* @param data new/changed data
*/
updateDocument(
colName: string,
id: string,
data: CollectionDocument
): CollectionDocument
/**
* deletes one document by id from collection
*
* @param colName collection name
* @param id id of entry
*/
deleteDocument(colName: string, id: string): { message: "ok" }
/**
* deletes documents by filter from collection
*
* @param colName collection name
* @param options options map, only filter valid
*/
deleteDocuments(
colName: string,
options?: ReadCollectionOptions
): { message: "ok"; removed: number }
/**
* send an email
*
* @param options email options map
*/
mail(options: {
to: string | string[]
cc?: string | string[]
bcc?: string | string[]
subject?: string
from: string
fromName?: string
plain?: string
html?: string
attach?: string | string[]
}): void
/**
* execute a template code and return result
*
* @param code template code
* @param contextData template context map
*/
template(
code: string,
contextData?: {
[key: string]: any
}
): string
/**
* read a file relative to config dir and return its content
*
* @param path relative file path
*/
file(path: string): string
/**
* http request
*
* @param url url for request
* @param options request options
*/
fetch(
url: string,
options?: {
method?: string
headers?: { [key: string]: string }
body?: string
}
): {
status: number
statusText: string
headers: { [key: string]: string }
trailer: { [key: string]: string }
url: string
body: {
text(): string
json(): any
}
}
/**
* dumps data to header and server log
*
* @param toDump data to dump
*/
dump(...toDump: any): void
/**
* set response header
*
* @param name header name
* @param value value
*/
header(name: string, value: any): void
/**
* get JWT authentication
*/
auth(): {
id: string
username: string
role: number
permissions: string[]
}
/**
* get Sentry trace id
*/
sentryTraceId(): string
}
export interface HookException {
status?: number
html?: string
[key: string]: any
}
export interface HookResponse extends GetHookData, PostHookData {
data?: CollectionDocument
results?: any
}
declare global {
var context: HookContext
}