forked from cms/tibi-svelte-starter
yarn upgrade
This commit is contained in:
5
api/hooks/clear_cache.js
Normal file
5
api/hooks/clear_cache.js
Normal file
@@ -0,0 +1,5 @@
|
||||
var utils = require("./lib/utils")
|
||||
|
||||
;(function () {
|
||||
utils.clearSSRCache()
|
||||
})()
|
||||
20
api/hooks/config-client.js
Normal file
20
api/hooks/config-client.js
Normal file
@@ -0,0 +1,20 @@
|
||||
const release = "__PROJECT__.dirty"
|
||||
|
||||
const originURL = "https://__PROJECT__.code.testversion.online"
|
||||
const apiClientBaseURL = "/api/"
|
||||
|
||||
const cryptchaSiteId = "6628f06a0938460001505119"
|
||||
|
||||
// @ts-ignore
|
||||
if (release && typeof context !== "undefined") {
|
||||
context.response.header("X-Release", release)
|
||||
}
|
||||
|
||||
const isMaster = release.includes("master")
|
||||
|
||||
module.exports = {
|
||||
release,
|
||||
apiClientBaseURL,
|
||||
cryptchaSiteId,
|
||||
originURL,
|
||||
}
|
||||
@@ -1,17 +1,73 @@
|
||||
module.exports = {
|
||||
projectName: "__PROJECT_NAME__",
|
||||
operatorEmail: "__OPERATOR_EMAIL_",
|
||||
const apiSsrBaseURL =
|
||||
"http://localhost:" + context.config.server().api.port + "/api/v1/_/" + context.api().namespace + "/"
|
||||
|
||||
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)\/?$/)
|
||||
}
|
||||
const now = { $date: new Date().toISOString() }
|
||||
const publishedFilter = {
|
||||
active: true,
|
||||
$or: [
|
||||
{ publication: { $exists: false } },
|
||||
{ publication: null },
|
||||
{
|
||||
$and: [
|
||||
{
|
||||
$or: [
|
||||
{ "publication.from": { $exists: false } },
|
||||
{ "publication.from": null },
|
||||
{ "publication.from": { $lte: now } },
|
||||
],
|
||||
},
|
||||
{
|
||||
$or: [
|
||||
{ "publication.to": { $exists: false } },
|
||||
{ "publication.to": null },
|
||||
{ "publication.to": { $gte: now } },
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
apiSsrBaseURL,
|
||||
publishedFilter,
|
||||
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 fixed url
|
||||
if (path == "/cart") return 1
|
||||
|
||||
// path starts with /products/ is product
|
||||
if (path?.startsWith("/products/")) {
|
||||
const slug = path?.replace(/^\/products\//, "")
|
||||
const resp = context.db.find("product", {
|
||||
filter: {
|
||||
$and: [{ slug: slug }, publishedFilter],
|
||||
},
|
||||
|
||||
selector: { _id: 1 },
|
||||
})
|
||||
if (resp && resp.length) {
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
// // all other sites are in db
|
||||
//path = path?.replace(/^\//, "")
|
||||
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", "product"],
|
||||
}
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
// @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
|
||||
})()
|
||||
@@ -1,11 +0,0 @@
|
||||
// @ts-check
|
||||
const { generateUrlString } = require("../lib/helper")
|
||||
|
||||
;(function () {
|
||||
/** @type {import("tibi-types").HookResponse} */
|
||||
let hookResponse
|
||||
|
||||
context.data.path = generateUrlString(context.data.path)
|
||||
|
||||
return hookResponse
|
||||
})()
|
||||
@@ -1,11 +0,0 @@
|
||||
// @ts-check
|
||||
const { generateUrlString } = require("../lib/helper")
|
||||
|
||||
;(function () {
|
||||
/** @type {import("tibi-types").HookResponse} */
|
||||
let hookResponse
|
||||
|
||||
context.data.path = generateUrlString(context.data.path)
|
||||
|
||||
return hookResponse
|
||||
})()
|
||||
20
api/hooks/filter_public.js
Normal file
20
api/hooks/filter_public.js
Normal file
@@ -0,0 +1,20 @@
|
||||
const { publishedFilter } = require("./config")
|
||||
|
||||
;(function () {
|
||||
if (!context.user.auth()) {
|
||||
// it is public
|
||||
|
||||
/** @type {HookResponse} */
|
||||
const hookResponse = {}
|
||||
|
||||
if (context.filter && Object.keys(context.filter).length > 0) {
|
||||
hookResponse.filter = {
|
||||
$and: [context.filter, publishedFilter],
|
||||
}
|
||||
} else {
|
||||
hookResponse.filter = publishedFilter
|
||||
}
|
||||
|
||||
return hookResponse
|
||||
}
|
||||
})()
|
||||
74
api/hooks/lib/ssr-server.js
Normal file
74
api/hooks/lib/ssr-server.js
Normal file
@@ -0,0 +1,74 @@
|
||||
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: { publication: 1 },
|
||||
projection: null,
|
||||
}
|
||||
)
|
||||
const publishSearch = context.db.find(endpoint, _optionsPublishSearch)
|
||||
publishSearch?.forEach((item) => {
|
||||
const publicationFrom = item.publication?.from ? new Date(item.publication.from.unixMilli()) : null
|
||||
const publicationTo = item.publication?.to ? new Date(item.publication.to.unixMilli()) : null
|
||||
|
||||
if (publicationFrom && publicationFrom > new Date()) {
|
||||
// entry has a publish date that is further in in the future than current, set global validUntil
|
||||
if (validUntil == null || validUntil > publicationFrom) {
|
||||
validUntil = publicationFrom
|
||||
}
|
||||
}
|
||||
if (publicationTo && publicationTo > new Date()) {
|
||||
// entry has a unpublish date that is further in in the future than current, set global validUntil
|
||||
if (validUntil == null || validUntil > publicationTo) {
|
||||
validUntil = publicationTo
|
||||
}
|
||||
}
|
||||
})
|
||||
// @ts-ignore
|
||||
context.ssrCacheValidUntil = validUntil
|
||||
}
|
||||
|
||||
// console.log("############ FETCHING ", apiSsrBaseURL + url)
|
||||
|
||||
const response = context.http.fetch(apiSsrBaseURL + url, {
|
||||
method: options.method,
|
||||
headers: options.headers,
|
||||
})
|
||||
|
||||
// console.log(JSON.stringify(response.headers, null, 2))
|
||||
|
||||
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,
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
const { apiClientBaseURL } = require("../config-client")
|
||||
|
||||
/**
|
||||
* convert object to string
|
||||
* @param {any} obj object
|
||||
* @returns {string} string
|
||||
*/
|
||||
function obj2str(obj) {
|
||||
if (Array.isArray(obj)) {
|
||||
@@ -30,8 +33,105 @@ function obj2str(obj) {
|
||||
if (obj) return obj
|
||||
}
|
||||
|
||||
// can be used by client code, so DONT INCLUDE hooks/config.js (SECRETS INSIDE)
|
||||
/**
|
||||
* api request via client or server
|
||||
* server function ssrRequest is called via context.ssrRequest, binded in ssr hook
|
||||
*
|
||||
* @param {string} endpoint
|
||||
* @param {ApiOptions} options
|
||||
* @param {any} body
|
||||
* @param {import("../../../frontend/src/sentry")} sentry
|
||||
* @param {typeof fetch} _fetch
|
||||
* @returns {Promise<ApiResult<any>>}
|
||||
*/
|
||||
function apiRequest(endpoint, options, body, sentry, _fetch) {
|
||||
// TODO cache only for GET
|
||||
|
||||
// first check cache if on client
|
||||
const cacheKey = obj2str({ endpoint: endpoint, options: options })
|
||||
|
||||
let method = options?.method || "GET"
|
||||
|
||||
// @ts-ignore
|
||||
if (typeof window !== "undefined" && window.__SSR_CACHE__ && method === "GET") {
|
||||
// @ts-ignore
|
||||
const cache = window.__SSR_CACHE__[cacheKey]
|
||||
console.log("SSR HIT:", cacheKey, cache)
|
||||
if (cache) {
|
||||
return Promise.resolve(cache)
|
||||
}
|
||||
}
|
||||
|
||||
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])
|
||||
})
|
||||
}
|
||||
|
||||
/** @type {{[key: string]: string}} */
|
||||
let headers = {
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
if (options?.headers) headers = { ...headers, ...options.headers }
|
||||
|
||||
if (typeof window === "undefined" && method === "GET") {
|
||||
// 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.startsWith("/") ? "" : apiClientBaseURL) + endpoint + (query ? "?" + query : "")
|
||||
|
||||
const span = sentry?.currentTransaction()?.startChild({
|
||||
op: "fetch",
|
||||
description: method + " " + url,
|
||||
data: Object.assign({}, options, { url }),
|
||||
})
|
||||
const trace_id = span?.toTraceparent()
|
||||
if (trace_id) {
|
||||
headers["sentry-trace"] = trace_id
|
||||
}
|
||||
|
||||
/** @type {{[key: string]: any}} */
|
||||
const requestOptions = {
|
||||
method,
|
||||
mode: "cors",
|
||||
headers,
|
||||
}
|
||||
|
||||
if (method === "POST" || method === "PUT") {
|
||||
requestOptions.body = JSON.stringify(body)
|
||||
}
|
||||
|
||||
const response = _fetch(url, requestOptions).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 })
|
||||
})
|
||||
})
|
||||
|
||||
span?.end()
|
||||
|
||||
// @ts-ignore
|
||||
return response
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
obj2str,
|
||||
apiRequest,
|
||||
}
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
// @ts-check
|
||||
|
||||
var config = require("../config")
|
||||
const { cryptchaSiteId } = require("../config-client")
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -9,208 +7,83 @@ var config = require("../config")
|
||||
function log(str) {
|
||||
console.log(JSON.stringify(str, undefined, 4))
|
||||
}
|
||||
function rand() {
|
||||
return Math.random().toString(36).substr(2) // remove `0.`
|
||||
}
|
||||
|
||||
/**
|
||||
* convert object to string
|
||||
* @param {any} obj object
|
||||
* @returns {string}
|
||||
*/
|
||||
function randomToken() {
|
||||
return rand() + rand()
|
||||
}
|
||||
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
|
||||
}
|
||||
})
|
||||
|
||||
/**
|
||||
*
|
||||
* @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)
|
||||
var elementsCleaned = []
|
||||
for (var i = 0; i < elements.length; i++) {
|
||||
if (elements[i]) elementsCleaned.push(elements[i])
|
||||
}
|
||||
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"
|
||||
)
|
||||
)
|
||||
return "{" + elementsCleaned.join("|") + "}"
|
||||
}
|
||||
|
||||
if (obj) return obj
|
||||
}
|
||||
|
||||
/**
|
||||
* clear SSR cache
|
||||
* @returns {number}
|
||||
*/
|
||||
function clearSSRCache() {
|
||||
var info = context.db.deleteMany("ssr", {})
|
||||
context.response.header("X-SSR-Cleared", info.removed)
|
||||
return info.removed
|
||||
}
|
||||
|
||||
function cryptchaCheck() {
|
||||
const solutionId = context.data._sId
|
||||
const solution = context.data._s
|
||||
|
||||
const body = JSON.stringify({ solution: solution })
|
||||
console.log(body)
|
||||
const resp = context.http.fetch(
|
||||
"https://cryptcha.webmakers.de/api/command?siteId=" +
|
||||
cryptchaSiteId +
|
||||
"&cmd=check&clear=1&solutionId=" +
|
||||
solutionId,
|
||||
{
|
||||
method: "POST",
|
||||
body,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
}
|
||||
)
|
||||
const j = resp.body.json()
|
||||
|
||||
const corrent = j.status == "ok" && resp.status == 200
|
||||
if (!corrent) {
|
||||
throw {
|
||||
status: 400,
|
||||
log: false,
|
||||
error: "incorrect data",
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
log,
|
||||
randomToken,
|
||||
isPublicToken,
|
||||
isSsrToken,
|
||||
tpl: tpl,
|
||||
Base64,
|
||||
parseDate,
|
||||
clearSSRCache,
|
||||
ssrValidatePath: config.ssRValidatePath,
|
||||
obj2str,
|
||||
cryptchaCheck,
|
||||
}
|
||||
|
||||
@@ -1,23 +1,42 @@
|
||||
// @ts-check
|
||||
const utils = require("../lib/utils")
|
||||
|
||||
var utils = require("../lib/utils")
|
||||
const { release } = require("../config-client")
|
||||
|
||||
const { ssrValidatePath } = require("../config")
|
||||
const { ssrRequest } = require("../lib/ssr-server")
|
||||
;(function () {
|
||||
var request = context.request()
|
||||
var url = request.query("url")
|
||||
var noCache = request.query("noCache")
|
||||
|
||||
var trace_id = context.debug.sentryTraceId()
|
||||
var trackingCall = request.header("x-ssr-skip")
|
||||
if (trackingCall) {
|
||||
// skip tracking
|
||||
// no cache header
|
||||
context.response.header("Cache-Control", "no-cache, no-store, must-revalidate")
|
||||
throw {
|
||||
status: parseInt(trackingCall),
|
||||
html: "",
|
||||
log: false,
|
||||
}
|
||||
}
|
||||
|
||||
let url = request.query("url") || request.header("x-ssr-url") || "/"
|
||||
const noCache = request.query("noCache")
|
||||
|
||||
const trace_id = context.debug.sentryTraceId()
|
||||
/**
|
||||
* @param {string} content
|
||||
*/
|
||||
function addSentryTrace(content) {
|
||||
return content.replace(
|
||||
"</head>",
|
||||
'<meta name="sentry-trace" content="' + trace_id + '" /></head>'
|
||||
)
|
||||
return content.replace("</head>", '<meta name="sentry-trace" content="' + trace_id + '" /></head>')
|
||||
}
|
||||
|
||||
context.response.header("sentry-trace", trace_id)
|
||||
const auth = context.user.auth()
|
||||
if (auth && auth.role == 0) {
|
||||
} else if (url) {
|
||||
/** @type {Date} */ // @ts-ignore
|
||||
context.ssrCacheValidUntil = null
|
||||
|
||||
if (url) {
|
||||
var comment = ""
|
||||
|
||||
url = url.split("?")[0]
|
||||
@@ -26,23 +45,36 @@ var utils = require("../lib/utils")
|
||||
if (url && url.length > 1) {
|
||||
url = url.replace(/\/$/, "")
|
||||
}
|
||||
if (url == "/noindex" || !url) {
|
||||
if (url == "/index" || !url) {
|
||||
url = "/" // see .htaccess
|
||||
}
|
||||
|
||||
var cache =
|
||||
!noCache &&
|
||||
context.db.find("ssr", {
|
||||
filter: {
|
||||
path: url,
|
||||
},
|
||||
})
|
||||
function useCache(/** @type {string} */ _url) {
|
||||
var cache =
|
||||
!noCache &&
|
||||
context.db.find("ssr", {
|
||||
filter: {
|
||||
path: _url,
|
||||
},
|
||||
})
|
||||
|
||||
if (cache && cache.length) {
|
||||
// use cache
|
||||
throw {
|
||||
status: 200,
|
||||
html: addSentryTrace(cache[0].content),
|
||||
if (cache && cache.length) {
|
||||
// check that entry is still allowed to be published
|
||||
const validUntil = cache[0].validUntil ? new Date(cache[0].validUntil.unixMilli()) : null
|
||||
|
||||
if (!validUntil || validUntil > new Date()) {
|
||||
// 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")
|
||||
context.db.delete("ssr", cache[0].id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,16 +84,30 @@ var utils = require("../lib/utils")
|
||||
var pNorender = false
|
||||
var pNotfound = false
|
||||
|
||||
var pR = utils.ssrValidatePath(url)
|
||||
if (pR < 0) {
|
||||
const pR = ssrValidatePath(url)
|
||||
if (pR === -1) {
|
||||
pNotfound = true
|
||||
comment += ", notFound"
|
||||
} else if (!pR) {
|
||||
pNorender = true
|
||||
comment += ", noRender"
|
||||
} else if (typeof pR === "string") {
|
||||
url = pR
|
||||
comment += ", cache url: " + url
|
||||
}
|
||||
|
||||
var head = ""
|
||||
var html = ""
|
||||
var error = ""
|
||||
if (noCache) {
|
||||
comment += ", noCache"
|
||||
}
|
||||
|
||||
if (!pNorender && !pNotfound) {
|
||||
// check if we have a cache
|
||||
useCache(url)
|
||||
}
|
||||
|
||||
let head = ""
|
||||
let html = ""
|
||||
let error = ""
|
||||
|
||||
comment += ", path: " + url
|
||||
|
||||
@@ -72,64 +118,17 @@ var utils = require("../lib/utils")
|
||||
status = 404
|
||||
html = "404 NOT FOUND"
|
||||
} else {
|
||||
// @ts-ignore
|
||||
context.ssrCache = {}
|
||||
// @ts-ignore
|
||||
context.ssrRequest = ssrRequest
|
||||
|
||||
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)
|
||||
const app = require("../lib/app.server")
|
||||
|
||||
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({
|
||||
const rendered = app.default.render({
|
||||
url: url,
|
||||
})
|
||||
head = rendered.head
|
||||
@@ -149,60 +148,47 @@ var utils = require("../lib/utils")
|
||||
} else {
|
||||
cacheIt = true
|
||||
}
|
||||
} catch (e) {
|
||||
} catch (/** @type {any} */ e) {
|
||||
utils.log(e.message)
|
||||
utils.log(e.stack)
|
||||
error = "error: " + e.message + "\n\n" + e.stack
|
||||
|
||||
// utils.log(e)
|
||||
// for (var property in e) {
|
||||
// utils.log(property + ": " + e[property])
|
||||
// }
|
||||
// error = JSON.stringify(e)
|
||||
}
|
||||
}
|
||||
|
||||
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 + "-->" : ""
|
||||
)
|
||||
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,
|
||||
// @ts-ignore
|
||||
validUntil: context.ssrCacheValidUntil,
|
||||
})
|
||||
}
|
||||
|
||||
tpl.replace(
|
||||
"</head>",
|
||||
'<meta name="sentry-trace" content="' + trace_id + '" /></head>'
|
||||
)
|
||||
|
||||
throw {
|
||||
status: status,
|
||||
log: false,
|
||||
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,
|
||||
}
|
||||
// only admins are allowed
|
||||
throw {
|
||||
status: 403,
|
||||
message: "invalid auth",
|
||||
auth: auth,
|
||||
release: release,
|
||||
}
|
||||
}
|
||||
})()
|
||||
|
||||
/*
|
||||
require("../lib/hook.test")
|
||||
console.log("hook test ende")
|
||||
throw {
|
||||
status: 500,
|
||||
msg: "TEST",
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -1,8 +1,25 @@
|
||||
// @ts-check
|
||||
const { release } = require("../config-client")
|
||||
var utils = require("../lib/utils")
|
||||
|
||||
;(function () {
|
||||
if (context.request().query("clear")) {
|
||||
console.log("CLEARING SSR CACHE")
|
||||
var removed = utils.clearSSRCache()
|
||||
var stats = {
|
||||
status: 200,
|
||||
message: "ok",
|
||||
removed: removed,
|
||||
release: release,
|
||||
}
|
||||
|
||||
utils.log(stats)
|
||||
|
||||
throw stats
|
||||
}
|
||||
|
||||
throw {
|
||||
status: 500,
|
||||
message: "ssr is only a dummy collection",
|
||||
release: release,
|
||||
}
|
||||
})()
|
||||
|
||||
Reference in New Issue
Block a user