- Complete KidsAI Explorer application - Multi-language support (English/German) - AI-powered educational guidance using OpenAI - Interactive chat interface for children - Proper placeholder translation fixes - Mobile-responsive design - Educational framework for critical thinking
58 lines
2.4 KiB
JavaScript
58 lines
2.4 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.path = exports.createPathTagFunction = void 0;
|
|
exports.encodeURIPath = encodeURIPath;
|
|
const error_1 = require("../../core/error.js");
|
|
/**
|
|
* Percent-encode everything that isn't safe to have in a path without encoding safe chars.
|
|
*
|
|
* Taken from https://datatracker.ietf.org/doc/html/rfc3986#section-3.3:
|
|
* > unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
|
|
* > sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
|
|
* > pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
|
|
*/
|
|
function encodeURIPath(str) {
|
|
return str.replace(/[^A-Za-z0-9\-._~!$&'()*+,;=:@]+/g, encodeURIComponent);
|
|
}
|
|
const createPathTagFunction = (pathEncoder = encodeURIPath) => function path(statics, ...params) {
|
|
// If there are no params, no processing is needed.
|
|
if (statics.length === 1)
|
|
return statics[0];
|
|
let postPath = false;
|
|
const path = statics.reduce((previousValue, currentValue, index) => {
|
|
if (/[?#]/.test(currentValue)) {
|
|
postPath = true;
|
|
}
|
|
return (previousValue +
|
|
currentValue +
|
|
(index === params.length ? '' : (postPath ? encodeURIComponent : pathEncoder)(String(params[index]))));
|
|
}, '');
|
|
const pathOnly = path.split(/[?#]/, 1)[0];
|
|
const invalidSegments = [];
|
|
const invalidSegmentPattern = /(?<=^|\/)(?:\.|%2e){1,2}(?=\/|$)/gi;
|
|
let match;
|
|
// Find all invalid segments
|
|
while ((match = invalidSegmentPattern.exec(pathOnly)) !== null) {
|
|
invalidSegments.push({
|
|
start: match.index,
|
|
length: match[0].length,
|
|
});
|
|
}
|
|
if (invalidSegments.length > 0) {
|
|
let lastEnd = 0;
|
|
const underline = invalidSegments.reduce((acc, segment) => {
|
|
const spaces = ' '.repeat(segment.start - lastEnd);
|
|
const arrows = '^'.repeat(segment.length);
|
|
lastEnd = segment.start + segment.length;
|
|
return acc + spaces + arrows;
|
|
}, '');
|
|
throw new error_1.OpenAIError(`Path parameters result in path with invalid segments:\n${path}\n${underline}`);
|
|
}
|
|
return path;
|
|
};
|
|
exports.createPathTagFunction = createPathTagFunction;
|
|
/**
|
|
* URI-encodes path params and ensures no unsafe /./ or /../ path segments are introduced.
|
|
*/
|
|
exports.path = (0, exports.createPathTagFunction)(encodeURIPath);
|
|
//# sourceMappingURL=path.js.map
|