Initial commit

This commit is contained in:
2023-12-26 20:24:42 +01:00
commit 66d0377a00
657 changed files with 21841 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
const release = "tibi-docs.dirty"
const apiClientBaseURL = "/api/"
// @ts-ignore
if (release && typeof context !== "undefined") {
context.response.header("X-Release", release)
}
module.exports = {
release,
apiClientBaseURL,
}

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

@@ -0,0 +1,43 @@
const publishedFilter = {
$or: [
{ publishDate: { $exists: false } },
{ publishDate: null },
{
publishDate: { $lte: { $date: new Date().toISOString() } },
// publishDate: { $lte: new Date() },
},
],
}
const apiSsrBaseURL = "http://localhost:8080/api/v1/_/demo/"
module.exports = {
publishedFilter,
apiSsrBaseURL,
ssrValidatePath: function (/** @type {string} */ path) {
// 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
// // / is de home
// if (path == "/") return 1
// // all other sites are in db
// path = path?.replace(/^\//, "")
// filter for path or alternativePaths
const resp = context.db.find("content", {
filter: {
$and: [{ $or: [{ path }, { "alternativePaths.path": path }] }, publishedFilter],
},
selector: { _id: 1 },
})
if (resp && resp.length) {
return 1
}
// not found
return -1
},
ssrPublishCheckCollections: ["content"],
}

View File

View File

View File

View File

View File

View File

View File

View File

View File

View File

View File

View File

View File

@@ -0,0 +1,65 @@
const { apiSsrBaseURL, ssrPublishCheckCollections } = require("../config")
/**
* api request via server, cache result in context.ssrCache
* should be elimated in client code via tree shaking
*
* @param {string} cacheKey
* @param {string} endpoint
* @param {string} query
* @param {ApiOptions} options
* @returns {ApiResult<any>}
*/
function ssrRequest(cacheKey, endpoint, query, options) {
let url = endpoint + (query ? "?" + query : "")
if (ssrPublishCheckCollections.includes(endpoint)) {
// @ts-ignore
let validUntil = context.ssrCacheValidUntil
// check in db for publish date to invalidate cache
const _optionsPublishSearch = Object.assign(
{},
{ filter: options?.filter },
{
selector: { publishDate: 1 },
// projection: null,
}
)
const publishSearch = context.db.find(endpoint, _optionsPublishSearch)
publishSearch?.forEach((item) => {
const publishDate = item.publishDate ? new Date(item.publishDate.unixMilli()) : null
if (publishDate && publishDate > new Date()) {
// entry has a publish date in the future, set global validUntil
if (validUntil == null || validUntil > publishDate) {
validUntil = publishDate
}
}
})
// @ts-ignore
context.ssrCacheValidUntil = validUntil
}
// console.log("############ FETCHING ", apiSsrBaseURL + url)
const response = context.http.fetch(apiSsrBaseURL + url, {
method: options.method,
headers: options.headers,
})
const json = response.body.json()
const count = parseInt(response.headers["x-results-count"] || "0")
// json is go data structure and incompatible with js, so we need to convert it
const r = { data: JSON.parse(JSON.stringify(json)), count: count }
// @ts-ignore
context.ssrCache[cacheKey] = r
return r
}
module.exports = {
ssrRequest,
}

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

@@ -0,0 +1,169 @@
const { apiClientBaseURL } = require("../config-client")
/**
* 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
}
// fetch polyfill
// [MIT License](LICENSE.md) © [Jason Miller](https://jasonformat.com/)
const _f = function (url, options) {
if (typeof XMLHttpRequest === "undefined") {
return Promise.resolve(null)
}
options = options || {}
return new Promise((resolve, reject) => {
const request = new XMLHttpRequest()
const keys = []
const all = []
const headers = {}
const response = () => ({
ok: ((request.status / 100) | 0) == 2, // 200-299
statusText: request.statusText,
status: request.status,
url: request.responseURL,
text: () => Promise.resolve(request.responseText),
json: () => Promise.resolve(request.responseText).then(JSON.parse),
blob: () => Promise.resolve(new Blob([request.response])),
clone: response,
headers: {
// @ts-ignore
keys: () => keys,
// @ts-ignore
entries: () => all,
get: (n) => headers[n.toLowerCase()],
has: (n) => n.toLowerCase() in headers,
},
})
request.open(options.method || "get", url, true)
request.onload = () => {
request
.getAllResponseHeaders()
// @ts-ignore
.replace(/^(.*?):[^\S\n]*([\s\S]*?)$/gm, (m, key, value) => {
keys.push((key = key.toLowerCase()))
all.push([key, value])
headers[key] = headers[key] ? `${headers[key]},${value}` : value
})
resolve(response())
}
request.onerror = reject
request.withCredentials = options.credentials == "include"
for (const i in options.headers) {
request.setRequestHeader(i, options.headers[i])
}
request.send(options.body || null)
})
}
const _fetch = typeof fetch === "undefined" ? (typeof window === "undefined" ? _f : window.fetch || _f) : fetch
/**
* api request via client or server
* server function ssrRequest is called via context.ssrRequest, binded in ssr hook
*
* @param {string} endpoint
* @param {ApiOptions} options
* @returns {Promise<ApiResult>}
*/
function apiRequest(endpoint, options) {
// first check cache if on client
const cacheKey = obj2str({ endpoint: endpoint, options: options })
// @ts-ignore
if (typeof window !== "undefined" && window.__SSR_CACHE__) {
// @ts-ignore
const cache = window.__SSR_CACHE__[cacheKey]
console.log("SSR:", cacheKey, cache)
if (cache) {
return Promise.resolve(cache)
}
}
let method = "GET"
let query = "&count=1"
if (options?.filter) query += "&filter=" + encodeURIComponent(JSON.stringify(options.filter))
if (options?.sort) query += "&sort=" + options.sort + "&sort=_id"
if (options?.limit) query += "&limit=" + options.limit
if (options?.offset) query += "&offset=" + options.offset
if (options?.projection) query += "&projection=" + options.projection
if (options?.lookup) query += "&lookup=" + options.lookup
if (options?.params) {
Object.keys(options.params).forEach((p) => {
query += "&" + p + "=" + encodeURIComponent(options.params[p])
})
}
let headers = {
"Content-Type": "application/json",
}
if (options?.headers) headers = { ...headers, ...options.headers }
if (typeof window === "undefined") {
// server
// reference via context from get hook to tree shake in client
// @ts-ignore
const d = context.ssrRequest(cacheKey, endpoint, query, Object.assign({}, options, { method, headers }))
return d
} else {
// client
let url = endpoint + (query ? "?" + query : "")
console.log("fetch", apiClientBaseURL + url)
return _fetch(apiClientBaseURL + url, {
method,
mode: "cors",
headers,
}).then((response) => {
return response?.json().then((json) => {
if (response?.status < 200 || response?.status >= 400) {
return Promise.reject({ response, data: json })
}
return Promise.resolve({ data: json || null, count: response.headers?.get("x-results-count") || 0 })
})
})
}
}
module.exports = {
obj2str,
apiRequest,
}

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

@@ -0,0 +1,20 @@
/**
*
* @param {any} str
*/
function log(str) {
console.log(JSON.stringify(str, undefined, 4))
}
/**
* clear SSR cache
*/
function clearSSRCache() {
var info = context.db.deleteMany("ssr", {})
context.response.header("X-SSR-Cleared", info.removed)
}
module.exports = {
log,
clearSSRCache,
}

View File

@@ -0,0 +1,18 @@
;(function () {
const request = context.request()
//get id of deleted image entry (delete request URL is : .../api/_/demo/medialib/id)
const id = request.param("id")
//find all entrys where there is an image field with the id of this deleted one (image is an foreignKey widget, so stores the id as a string internally)
const referencingElements = context.db.find("democol", {
filter: {
image: id,
},
})
referencingElements.forEach((e) => {
//update all those entries to remove the image id from them, since its deleted...
context.db.update("democol", e.id, {
image: "",
})
})
})()

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

@@ -0,0 +1,174 @@
// TODO: add query string functionality to cache
const { ssrValidatePath } = require("../config")
const { log } = require("../lib/utils")
const { ssrRequest } = require("../lib/ssr-server")
;(function () {
/** @type {HookResponse} */
// @ts-ignore
let response = null
const request = context.request()
let url = request.query("url")
const noCache = request.query("noCache")
// add sentry trace id to head
const trace_id = context.debug.sentryTraceId()
/**
* @param {string} content
*/
function addSentryTrace(content) {
return content.replace("</head>", '<meta name="sentry-trace" content="' + trace_id + '" /></head>')
}
context.response.header("sentry-trace", trace_id)
if (url) {
// comment will be printed to html later
let comment = ""
/** @type {Date} */ // @ts-ignore
context.ssrCacheValidUntil = null
url = url.split("?")[0]
comment += "url: " + url
if (url && url.length > 1) {
url = url.replace(/\/$/, "")
}
if (url == "/noindex" || !url) {
url = "/" // see .htaccess
}
// check if url is in cache
/** @type {Ssr[]} */ // @ts-ignore
const cache =
!noCache &&
context.db.find("ssr", {
filter: {
path: url,
},
})
if (cache && cache.length) {
const validUntil = cache[0].validUntil ? new Date(cache[0].validUntil.unixMilli()) : null
// context.debug.dump("cache validUntil", validUntil)
if (!validUntil || validUntil > new Date()) {
// context.debug.dump("using cache")
// use cache
context.response.header("X-SSR-Cache", "true")
throw {
status: 200,
log: false,
html: addSentryTrace(cache[0].content),
}
} else {
// cache is invalid, delete it
context.response.header("X-SSR-Cache", "invalid")
// @ts-ignore
context.db.delete("ssr", cache[0].id)
}
}
// validate url
let status = 200
let pNorender = false
let pNotfound = false
const pR = ssrValidatePath(url)
if (pR < 0) {
pNotfound = true
} else if (!pR) {
pNorender = true
}
let head = ""
let html = ""
let error = ""
comment += ", path: " + url
let cacheIt = false
if (pNorender) {
html = "<!-- NO SSR RENDERING -->"
} else if (pNotfound) {
status = 404
html = "404 NOT FOUND"
} else {
// @ts-ignore
context.ssrCache = {}
// @ts-ignore
context.ssrRequest = ssrRequest
// try rendering, if error output plain html
try {
// include App.svelte and render it
// @ts-ignore
// console.log("####### RENDERING ", url)
const app = require("../lib/app.server")
const rendered = app.default.render({
url: url,
})
head = rendered.head
html = rendered.html
// add ssrCache to head, cache is built in ssr.js/apiRequest
head +=
"\n\n" +
"<script>window.__SSR_CACHE__ = " +
// @ts-ignore
JSON.stringify(context.ssrCache) +
"</script>"
// status from webapp
// @ts-ignore
if (context.is404) {
// console.log("########## 404")
status = 404
} else {
cacheIt = true
}
} catch (/** @type {any} */ e) {
// save error for later insert into html
log(e.message)
log(e.stack)
error = "error: " + e.message + "\n\n" + e.stack
}
}
// read html template and replace placeholders
let 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 + "-->" : "")
// save cache if adviced
if (cacheIt && !noCache) {
context.db.create("ssr", {
// context.debug.dump("ssr", {
path: url,
content: tpl,
// @ts-ignore
validUntil: context.ssrCacheValidUntil,
})
}
// return html
throw {
status: status,
log: false,
html: addSentryTrace(tpl),
}
} else {
// only admins are allowed to get without url parameter
const auth = context.user.auth()
if (!auth || auth.role !== 0) {
throw {
status: 403,
message: "invalid auth",
auth: auth,
}
}
}
})()

View File

@@ -0,0 +1,16 @@
const utils = require("../lib/utils")
;(function () {
if (context.request().query("clear")) {
utils.clearSSRCache()
throw {
status: 200,
message: "cache cleared",
}
}
throw {
status: 500,
message: "ssr is only a dummy collection",
}
})()