Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions packages/oc/src/registry/domain/components-details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import getUnixUTCTimestamp from 'oc-get-unix-utc-timestamp';
import type { StorageAdapter } from 'oc-storage-adapters-utils';
import type {
Component,
ComponentDetail,
ComponentsDetails,
ComponentsList,
Config
Expand All @@ -29,21 +30,23 @@ export default function componentsDetails(conf: Config, cdn: StorageAdapter) {
componentsList: ComponentsList;
details?: ComponentsDetails;
}): Promise<ComponentsDetails> => {
const clone =
globalThis.structuredClone ||
((data: any) => JSON.parse(JSON.stringify(data)));
const details = { components: {} as ComponentsDetails['components'] };

const details = options.details
? clone(options.details)
: { components: {} as ComponentsDetails['components'] };
for (const [name, componentDetails] of Object.entries(
options.details?.components || {}
)) {
details.components[name] = { ...componentDetails };
}

const missing: Array<{ name: string; version: string }> = [];
for (const [name, versions] of Object.entries(
options.componentsList.components
)) {
details.components[name] = details.components[name] || {};
const componentDetails =
details.components[name] || ({} as ComponentDetail);
details.components[name] = componentDetails;
for (const version of versions) {
if (!details.components[name][version]) {
if (!componentDetails[version]) {
missing.push({ name, version });
}
}
Expand All @@ -58,7 +61,7 @@ export default function componentsDetails(conf: Config, cdn: StorageAdapter) {
`${conf.storage.options.componentsDir}/${name}/${version}/package.json`,
true
);
details.components[name][version] = {
details.components[name]![version] = {
publishDate: content.oc.date || 0,
templateSize: content.oc.files.template.size
};
Expand Down
38 changes: 25 additions & 13 deletions packages/oc/src/registry/domain/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default function repository(conf: Config) {
);

const local = {
components: undefined as string[] | undefined,
getCompiledView(componentName: string): string {
if (componentName === 'oc-client') {
return fs
Expand All @@ -63,20 +64,31 @@ export default function repository(conf: Config) {
.toString();
},
getComponents(): string[] {
const validComponents =
conf.components ||
fs.readdirSync(conf.path).filter((file) => {
const isDir = fs.lstatSync(path.join(conf.path, file)).isDirectory();
const isValidComponent = isDir
? fs
.readdirSync(path.join(conf.path, file))
.filter((file) => file === '_package').length === 1
: false;

return isValidComponent;
});
if (conf.hotReloading === false && local.components) {
return local.components;
}

const validComponents = conf.components
? conf.components
: fs.readdirSync(conf.path).filter((file) => {
const isDir = fs
.lstatSync(path.join(conf.path, file))
.isDirectory();
const isValidComponent = isDir
? fs
.readdirSync(path.join(conf.path, file))
.filter((file) => file === '_package').length === 1
: false;

return isValidComponent;
});

const components = [...validComponents, 'oc-client'];
if (conf.hotReloading === false) {
local.components = components;
}

return [...validComponents, 'oc-client'];
return components;
},
getComponentVersions(componentName: string): Promise<string[]> {
if (componentName === 'oc-client') {
Expand Down
51 changes: 37 additions & 14 deletions packages/oc/src/registry/routes/helpers/get-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,26 @@ const noopConsole = Object.fromEntries(
Object.keys(console).map((key) => [key, noop])
);

const parseTemplatesHeader = (templates: string): string[] => {
const result: string[] = [];
let start = 0;

for (let index = 0; index <= templates.length; index++) {
const char = templates[index];
if (char === ',' || char === ';' || index === templates.length) {
result.push(templates.slice(start, index));
start = templates.indexOf(';', index);
if (start === -1) {
break;
}
index = start;
start++;
}
}

return result;
};

/**
* Converts the plugins to a function that returns a record of plugins with the context applied
* Caches the plugins without context and applies the context to the plugins that need it
Expand Down Expand Up @@ -306,23 +326,29 @@ export default function getComponent(conf: Config, repository: Repository) {
});
}

const customHeadersToSkip = conf.customHeadersToSkipOnWeakVersion;
const customHeadersToSkipSet = customHeadersToSkip?.length
? new Set(customHeadersToSkip)
: undefined;

const filterCustomHeaders = (
headers: Record<string, string>,
requestedVersion: string,
actualVersion: string
) => {
const needFiltering =
Object.keys(headers).length > 0 &&
conf.customHeadersToSkipOnWeakVersion?.length > 0 &&
requestedVersion !== actualVersion;
if (!customHeadersToSkipSet || requestedVersion === actualVersion) {
return headers;
}

if (!needFiltering) return headers;
let filteredHeaders: Record<string, string> | undefined;
for (const key in headers) {
if (!customHeadersToSkipSet.has(key)) {
filteredHeaders = filteredHeaders || {};
filteredHeaders[key] = headers[key]!;
}
}

return Object.fromEntries(
Object.entries(headers).filter(
([key]) => !conf.customHeadersToSkipOnWeakVersion.includes(key)
)
);
return filteredHeaders || {};
};

const returnComponent = async (err: any, data: any) => {
Expand All @@ -347,11 +373,8 @@ export default function getComponent(conf: Config, repository: Repository) {
options.headers['user-agent'] &&
!!options.headers['user-agent'].match('oc-client-');

const parseTemplatesHeader = (t: string) =>
t.split(';').map((t) => t.split(',')[0]);
const supportedTemplates = options.headers['templates']
? (parseTemplatesHeader(options.headers['templates'] as string) ??
[])
? parseTemplatesHeader(options.headers['templates'] as string)
: [];

const isTemplateSupportedByClient = Boolean(
Expand Down
Loading