From cf9e4ea1f47fe98355e106109ba9026a78a5a97b Mon Sep 17 00:00:00 2001 From: Luiz Ferraz Date: Thu, 26 Dec 2024 15:33:09 -0300 Subject: [PATCH] WIP --- .changeset/stupid-deers-think.md | 5 +++++ package/src/renderFileStore.ts | 36 +++++++------------------------- 2 files changed, 12 insertions(+), 29 deletions(-) create mode 100644 .changeset/stupid-deers-think.md diff --git a/.changeset/stupid-deers-think.md b/.changeset/stupid-deers-think.md new file mode 100644 index 0000000..0f6f61b --- /dev/null +++ b/.changeset/stupid-deers-think.md @@ -0,0 +1,5 @@ +--- +"@domain-expansion/astro": minor +--- + +Eagerly normalize loaded chunks diff --git a/package/src/renderFileStore.ts b/package/src/renderFileStore.ts index 0b8bedc..2ff1582 100644 --- a/package/src/renderFileStore.ts +++ b/package/src/renderFileStore.ts @@ -1,12 +1,11 @@ import { createResolver } from "astro-integration-kit"; -import type { RenderDestination, RenderDestinationChunk } from "astro/runtime/server/render/common.js"; +import type { RenderDestinationChunk } from "astro/runtime/server/render/common.js"; import { rootDebug } from "./debug.js"; import * as fs from 'node:fs'; import type { AstroFactoryReturnValue } from "astro/runtime/server/render/astro/factory.js"; import { Either, runtime, type Thunk } from "./utils.js"; import type { SSRMetadata } from "astro"; import type { RenderInstruction } from "astro/runtime/server/render/instruction.js"; -import type { RenderTemplateResult } from "astro/runtime/server/render/astro/render-template.js"; import * as zlib from "node:zlib"; import { promisify } from "node:util"; import { fsCacheHit, fsCacheMiss, trackLoadedCompressedData, trackLoadedData, trackStoredCompressedData, trackStoredData } from "./metrics.js"; @@ -297,16 +296,16 @@ export class RenderFileStore { private static normalizeValue(value: SerializedValue): ValueThunk { switch (value.type) { case "headAndContent": { - const seminormalChunks = value.chunks.map(Either.right); + const normalChunks = value.chunks.map(RenderFileStore.normalizeChunk); return () => runtime.createHeadAndContent( // SAFETY: Astro core is wrong new runtime.HTMLString(value.head) as unknown as string, - RenderFileStore.renderTemplateFromSeminormalizedChunks(seminormalChunks), + FactoryValueClone.renderTemplateFromChunks(normalChunks), ); } case "templateResult": { - const seminormalChunks = value.chunks.map(Either.right); - return () => RenderFileStore.renderTemplateFromSeminormalizedChunks(seminormalChunks); + const normalChunks = value.chunks.map(RenderFileStore.normalizeChunk); + return () => FactoryValueClone.renderTemplateFromChunks(normalChunks); } case "response": return () => RenderFileStore.normalizeResponse(value); @@ -359,7 +358,8 @@ export class RenderFileStore { } if (chunk instanceof Response) { - return RenderFileStore.denormalizeResponse(chunk); + const { denormalized } = await RenderFileStore.denormalizeResponse(chunk) + return denormalized!; } if ('buffer' in chunk) return { @@ -455,28 +455,6 @@ export class RenderFileStore { }); } - private static renderTemplateFromSeminormalizedChunks(chunks: Either[]): RenderTemplateResult { - const template = runtime.renderTemplate(Object.assign([], { raw: [] })); - - return Object.assign(template, { - render: (destination: RenderDestination) => { - return new Promise(resolve => { - setImmediate(() => { - for (const chunk of chunks) { - if (Either.isLeft(chunk)) { - destination.write(chunk.value); - } else { - destination.write(RenderFileStore.normalizeChunk(chunk.value)); - } - } - - resolve(); - }); - }); - } - }) - } - private static isSerializableRenderInstruction( instruction: RenderInstruction ): instruction is SerializableRenderInstruction {