Merge branch 'main' into pe/onboarding-card-refactor

This commit is contained in:
Patrick Erichsen 2025-06-08 10:18:53 -07:00
commit fc2df6ce58
7 changed files with 733 additions and 32 deletions

View file

@ -117,10 +117,15 @@ async function loadConfigYaml(options: {
// `Loading config.yaml from ${JSON.stringify(packageIdentifier)} with root path ${rootPath}`,
// );
let config =
overrideConfigYaml ??
const errors: ConfigValidationError[] = [];
let config: AssistantUnrolled | undefined;
if (overrideConfigYaml) {
config = overrideConfigYaml;
} else {
// This is how we allow use of blocks locally
(await unrollAssistant(
const unrollResult = await unrollAssistant(
packageIdentifier,
new RegistryClient({
accessToken: await controlPlaneClient.getAccessToken(),
@ -139,17 +144,23 @@ async function loadConfigYaml(options: {
),
renderSecrets: true,
injectBlocks: allLocalBlocks,
asConfigResult: true,
},
));
);
config = unrollResult.config;
if (unrollResult.errors) {
errors.push(...unrollResult.errors);
}
}
const errors = isAssistantUnrolledNonNullable(config)
? validateConfigYaml(config)
: [
{
if (config) {
isAssistantUnrolledNonNullable(config)
? errors.push(...validateConfigYaml(config))
: errors.push({
fatal: true,
message: "Assistant includes blocks that don't exist",
},
];
});
}
if (errors?.some((error) => error.fatal)) {
return {

View file

@ -24,9 +24,9 @@ export function ErrorSection() {
}`}
>
{error.fatal ? (
<ExclamationCircleIcon className="mr-2 mt-1 h-3.5 w-3.5" />
<ExclamationCircleIcon className="mr-2 mt-1 h-3.5 w-3.5 shrink-0" />
) : (
<ExclamationTriangleIcon className="mr-2 mt-1 h-3.5 w-3.5" />
<ExclamationTriangleIcon className="mr-2 mt-1 h-3.5 w-3.5 shrink-0" />
)}
<p
className="m-0 whitespace-pre-wrap text-wrap"

View file

@ -5,7 +5,7 @@ scipy==1.7.1
# Web development
flask==2.0.2
django==3.2.7
django==4.2.22
# Request handling
requests==2.26.0

View file

@ -1,4 +1,5 @@
import * as YAML from "yaml";
import { ZodError } from "zod";
import { PlatformClient, Registry } from "../interfaces/index.js";
import { encodeSecretLocation } from "../interfaces/SecretResult.js";
import {
@ -17,6 +18,7 @@ import {
configYamlSchema,
Rule,
} from "../schemas/index.js";
import { ConfigResult, ConfigValidationError } from "../validation.js";
import {
packageIdentifierToShorthandSlug,
useProxyForUnrenderedSecrets,
@ -30,16 +32,25 @@ export function parseConfigYaml(configYaml: string): ConfigYaml {
if (result.success) {
return result.data;
}
throw new Error(
result.error.errors
.map((e) => `${e.path.join(".")}: ${e.message}`)
.join(""),
);
throw new Error(formatZodError(result.error), {
cause: "result.success was false",
});
} catch (e) {
console.log("Failed to parse rolled assistant:", configYaml);
throw new Error(
`Failed to parse assistant:\n${e instanceof Error ? e.message : e}`,
);
console.error("Failed to parse rolled assistant:", configYaml);
if (
e instanceof Error &&
"cause" in e &&
e.cause === "result.success was false"
) {
throw new Error(`Failed to parse assistant: ${e.message}`);
} else if (e instanceof ZodError) {
throw new Error(`Failed to parse assistant: ${formatZodError(e)}`);
} else {
throw new Error(
`Failed to parse assistant: ${e instanceof Error ? e.message : e}`,
);
}
}
}
@ -49,9 +60,10 @@ export function parseAssistantUnrolled(configYaml: string): AssistantUnrolled {
const result = assistantUnrolledSchema.parse(parsed);
return result;
} catch (e: any) {
throw new Error(
console.error(
`Failed to parse unrolled assistant: ${e.message}\n\n${configYaml}`,
);
throw new Error(`Failed to parse unrolled assistant: ${formatZodError(e)}`);
}
}
@ -61,7 +73,7 @@ export function parseBlock(configYaml: string): Block {
const result = blockSchema.parse(parsed);
return result;
} catch (e: any) {
throw new Error(`Failed to parse block: ${e.message}`);
throw new Error(`Failed to parse block: ${formatZodError(e)}`);
}
}
@ -183,6 +195,7 @@ async function extractRenderedSecretsMap(
export interface BaseUnrollAssistantOptions {
renderSecrets: boolean;
injectBlocks?: PackageIdentifier[];
asConfigResult?: true;
}
export interface DoNotRenderSecretsUnrollAssistantOptions
@ -204,14 +217,30 @@ export type UnrollAssistantOptions =
| DoNotRenderSecretsUnrollAssistantOptions
| RenderSecretsUnrollAssistantOptions;
// Overload to satisfy existing consumers of unrollAssistant.
export async function unrollAssistant(
id: PackageIdentifier,
registry: Registry,
options: UnrollAssistantOptions & { asConfigResult: true },
): Promise<ConfigResult<AssistantUnrolled>>;
export async function unrollAssistant(
id: PackageIdentifier,
registry: Registry,
options: UnrollAssistantOptions,
): Promise<AssistantUnrolled> {
): Promise<AssistantUnrolled>;
export async function unrollAssistant(
id: PackageIdentifier,
registry: Registry,
options: UnrollAssistantOptions,
): Promise<AssistantUnrolled | ConfigResult<AssistantUnrolled>> {
// Request the content from the registry
const rawContent = await registry.getContent(id);
return unrollAssistantFromContent(id, rawContent, registry, options);
const result = unrollAssistantFromContent(id, rawContent, registry, options);
return result;
}
function renderTemplateData(
@ -236,7 +265,7 @@ export async function unrollAssistantFromContent(
rawYaml: string,
registry: Registry,
options: UnrollAssistantOptions,
): Promise<AssistantUnrolled> {
): Promise<AssistantUnrolled | ConfigResult<AssistantUnrolled>> {
// Parse string to Zod-validated YAML
let parsedYaml = parseConfigYaml(rawYaml);
@ -245,10 +274,15 @@ export async function unrollAssistantFromContent(
parsedYaml,
registry,
options.injectBlocks,
options.asConfigResult ?? false,
);
// Back to a string so we can fill in template variables
const rawUnrolledYaml = YAML.stringify(unrolledAssistant);
const rawUnrolledYaml = options.asConfigResult
? YAML.stringify(
(unrolledAssistant as ConfigResult<AssistantUnrolled>).config,
)
: YAML.stringify(unrolledAssistant);
// Convert all of the template variables to FQSNs
// Secrets from the block will have the assistant slug prepended to the FQSN
@ -277,6 +311,17 @@ export async function unrollAssistantFromContent(
options.orgScopeId,
options.onPremProxyUrl,
);
if (options.asConfigResult) {
return {
config: finalConfig,
errors: (unrolledAssistant as ConfigResult<AssistantUnrolled>).errors,
configLoadInterrupted: (
unrolledAssistant as ConfigResult<AssistantUnrolled>
).configLoadInterrupted,
};
}
return finalConfig;
}
@ -284,7 +329,10 @@ export async function unrollBlocks(
assistant: ConfigYaml,
registry: Registry,
injectBlocks: PackageIdentifier[] | undefined,
): Promise<AssistantUnrolled> {
asConfigError: boolean,
): Promise<AssistantUnrolled | ConfigResult<AssistantUnrolled>> {
const errors: ConfigValidationError[] = [];
const unrolledAssistant: AssistantUnrolled = {
name: assistant.name,
version: assistant.version,
@ -316,8 +364,23 @@ export async function unrollBlocks(
);
}
} catch (err) {
let msg = "";
if (
typeof unrolledBlock.uses !== "string" &&
"filePath" in unrolledBlock.uses
) {
msg = `${(err as Error).message}.\n> ${unrolledBlock.uses.filePath}`;
} else {
msg = `${(err as Error).message}.\n> ${JSON.stringify(unrolledBlock.uses)}`;
}
errors.push({
fatal: false,
message: msg,
});
console.error(
`Failed to unroll block ${unrolledBlock.uses}: ${(err as Error).message}`,
`Failed to unroll block ${JSON.stringify(unrolledBlock.uses)}: ${(err as Error).message}`,
);
sectionBlocks.push(null);
}
@ -349,6 +412,11 @@ export async function unrollBlocks(
rules.push(block);
}
} catch (err) {
errors.push({
fatal: false,
message: `${(err as Error).message}:\n${rule.uses}`,
});
console.error(
`Failed to unroll block ${rule.uses}: ${(err as Error).message}`,
);
@ -381,12 +449,36 @@ export async function unrollBlocks(
);
}
} catch (err) {
let msg = "";
if (injectBlock.uriType === "file") {
msg = `${(err as Error).message}.\n> ${injectBlock.filePath}`;
} else {
msg = `${(err as Error).message}.\n> ${injectBlock.fullSlug}`;
}
errors.push({
fatal: false,
message: msg,
});
console.error(
`Failed to unroll block ${injectBlock}: ${(err as Error).message}`,
`Failed to unroll block ${JSON.stringify(injectBlock)}: ${(err as Error).message}`,
);
}
}
if (asConfigError) {
const configResult: ConfigResult<AssistantUnrolled> = {
config: undefined,
errors: undefined,
configLoadInterrupted: false,
};
configResult.config = unrolledAssistant;
if (errors.length > 0) {
configResult.errors = errors;
}
return configResult;
}
return unrolledAssistant;
}
@ -439,3 +531,15 @@ export function mergeOverrides<T extends Record<string, any>>(
}
return block;
}
function formatZodError(error: any): string {
if (error.errors && Array.isArray(error.errors)) {
return error.errors
.map((e: any) => {
const path = e.path.length > 0 ? `${e.path.join(".")}: ` : "";
return `${path}${e.message}`;
})
.join(", ");
}
return error.message || "Validation failed";
}

View file

@ -2,8 +2,9 @@ import { RequestOptions } from "@continuedev/config-types";
import * as followRedirects from "follow-redirects";
import { HttpProxyAgent } from "http-proxy-agent";
import { HttpsProxyAgent } from "https-proxy-agent";
import fetch, { BodyInit, RequestInit, Response } from "node-fetch";
import { BodyInit, RequestInit, Response } from "node-fetch";
import { getAgentOptions } from "./getAgentOptions.js";
import fetch from "./node-fetch-patch.js";
import { getProxyFromEnv, shouldBypassProxy } from "./util.js";
const { http, https } = (followRedirects as any).default;

View file

@ -0,0 +1,518 @@
/*
This is a patched version of index.js from node-fetch 3.3.2 (see packages/fetch/node_modules/node-fetch/src/index.js)
This is to fix "Premature close" errors caused by chunked encoding assumptions - see https://github.com/continuedev/continue/issues/5502
The patch adjusts boundary chunk detection logic (see "PATCH" comment next to patch)
And is based on https://github.com/node-fetch/node-fetch/issues/1576
*/
/**
* Index.js
*
* a request API compatible with window.fetch
*
* All spec algorithm step numbers are based on https://fetch.spec.whatwg.org/commit-snapshots/ae716822cb3a61843226cd090eefc6589446c1d2/.
*/
import { Buffer } from "node:buffer";
import http from "node:http";
import https from "node:https";
import Stream, { PassThrough, pipeline as pump } from "node:stream";
import zlib from "node:zlib";
import dataUriToBuffer from "data-uri-to-buffer";
import {
Blob,
blobFrom,
blobFromSync,
File,
fileFrom,
fileFromSync,
} from "fetch-blob/from.js";
import { FormData } from "formdata-polyfill/esm.min.js";
import { clone, writeToStream } from "node-fetch/src/body.js";
import { AbortError } from "node-fetch/src/errors/abort-error.js";
import { FetchError } from "node-fetch/src/errors/fetch-error.js";
import Headers, { fromRawHeaders } from "node-fetch/src/headers.js";
import Request, { getNodeRequestOptions } from "node-fetch/src/request.js";
import Response from "node-fetch/src/response.js";
import { isRedirect } from "node-fetch/src/utils/is-redirect.js";
import {
isDomainOrSubdomain,
isSameProtocol,
} from "node-fetch/src/utils/is.js";
import { parseReferrerPolicyFromHeader } from "node-fetch/src/utils/referrer.js";
export {
AbortError,
Blob,
blobFrom,
blobFromSync,
FetchError,
File,
fileFrom,
fileFromSync,
FormData,
Headers,
isRedirect,
Request,
Response,
};
const supportedSchemas = new Set(["data:", "http:", "https:"]);
/**
* Fetch function
*
* @param {string | URL | import('./request').default} url - Absolute url or Request instance
* @param {*} [options_] - Fetch options
* @return {Promise<import('./response').default>}
*/
export default async function fetch(url, options_) {
return new Promise((resolve, reject) => {
// Build request object
const request = new Request(url, options_);
const { parsedURL, options } = getNodeRequestOptions(request);
if (!supportedSchemas.has(parsedURL.protocol)) {
throw new TypeError(
`node-fetch cannot load ${url}. URL scheme "${parsedURL.protocol.replace(/:$/, "")}" is not supported.`,
);
}
if (parsedURL.protocol === "data:") {
const data = dataUriToBuffer(request.url);
const response = new Response(data, {
headers: { "Content-Type": data.typeFull },
});
resolve(response);
return;
}
// Wrap http.request into fetch
const send = (parsedURL.protocol === "https:" ? https : http).request;
const { signal } = request;
let response = null;
const abort = () => {
const error = new AbortError("The operation was aborted.");
reject(error);
if (request.body && request.body instanceof Stream.Readable) {
request.body.destroy(error);
}
if (!response || !response.body) {
return;
}
response.body.emit("error", error);
};
if (signal && signal.aborted) {
abort();
return;
}
const abortAndFinalize = () => {
abort();
finalize();
};
// Send request
const request_ = send(parsedURL.toString(), options);
if (signal) {
signal.addEventListener("abort", abortAndFinalize);
}
const finalize = () => {
request_.abort();
if (signal) {
signal.removeEventListener("abort", abortAndFinalize);
}
};
request_.on("error", (error) => {
reject(
new FetchError(
`request to ${request.url} failed, reason: ${error.message}`,
"system",
error,
),
);
finalize();
});
fixResponseChunkedTransferBadEnding(request_, (error) => {
if (response && response.body) {
response.body.destroy(error);
}
});
/* c8 ignore next 18 */
if (process.version < "v14") {
// Before Node.js 14, pipeline() does not fully support async iterators and does not always
// properly handle when the socket close/end events are out of order.
request_.on("socket", (s) => {
let endedWithEventsCount;
s.prependListener("end", () => {
endedWithEventsCount = s._eventsCount;
});
s.prependListener("close", (hadError) => {
// if end happened before close but the socket didn't emit an error, do it now
if (response && endedWithEventsCount < s._eventsCount && !hadError) {
const error = new Error("Premature close");
error.code = "ERR_STREAM_PREMATURE_CLOSE";
response.body.emit("error", error);
}
});
});
}
request_.on("response", (response_) => {
request_.setTimeout(0);
const headers = fromRawHeaders(response_.rawHeaders);
// HTTP fetch step 5
if (isRedirect(response_.statusCode)) {
// HTTP fetch step 5.2
const location = headers.get("Location");
// HTTP fetch step 5.3
let locationURL = null;
try {
locationURL =
location === null ? null : new URL(location, request.url);
} catch {
// error here can only be invalid URL in Location: header
// do not throw when options.redirect == manual
// let the user extract the errorneous redirect URL
if (request.redirect !== "manual") {
reject(
new FetchError(
`uri requested responds with an invalid redirect URL: ${location}`,
"invalid-redirect",
),
);
finalize();
return;
}
}
// HTTP fetch step 5.5
switch (request.redirect) {
case "error":
reject(
new FetchError(
`uri requested responds with a redirect, redirect mode is set to error: ${request.url}`,
"no-redirect",
),
);
finalize();
return;
case "manual":
// Nothing to do
break;
case "follow": {
// HTTP-redirect fetch step 2
if (locationURL === null) {
break;
}
// HTTP-redirect fetch step 5
if (request.counter >= request.follow) {
reject(
new FetchError(
`maximum redirect reached at: ${request.url}`,
"max-redirect",
),
);
finalize();
return;
}
// HTTP-redirect fetch step 6 (counter increment)
// Create a new Request object.
const requestOptions = {
headers: new Headers(request.headers),
follow: request.follow,
counter: request.counter + 1,
agent: request.agent,
compress: request.compress,
method: request.method,
body: clone(request),
signal: request.signal,
size: request.size,
referrer: request.referrer,
referrerPolicy: request.referrerPolicy,
};
// when forwarding sensitive headers like "Authorization",
// "WWW-Authenticate", and "Cookie" to untrusted targets,
// headers will be ignored when following a redirect to a domain
// that is not a subdomain match or exact match of the initial domain.
// For example, a redirect from "foo.com" to either "foo.com" or "sub.foo.com"
// will forward the sensitive headers, but a redirect to "bar.com" will not.
// headers will also be ignored when following a redirect to a domain using
// a different protocol. For example, a redirect from "https://foo.com" to "http://foo.com"
// will not forward the sensitive headers
if (
!isDomainOrSubdomain(request.url, locationURL) ||
!isSameProtocol(request.url, locationURL)
) {
for (const name of [
"authorization",
"www-authenticate",
"cookie",
"cookie2",
]) {
requestOptions.headers.delete(name);
}
}
// HTTP-redirect fetch step 9
if (
response_.statusCode !== 303 &&
request.body &&
options_.body instanceof Stream.Readable
) {
reject(
new FetchError(
"Cannot follow redirect with body being a readable stream",
"unsupported-redirect",
),
);
finalize();
return;
}
// HTTP-redirect fetch step 11
if (
response_.statusCode === 303 ||
((response_.statusCode === 301 || response_.statusCode === 302) &&
request.method === "POST")
) {
requestOptions.method = "GET";
requestOptions.body = undefined;
requestOptions.headers.delete("content-length");
}
// HTTP-redirect fetch step 14
const responseReferrerPolicy =
parseReferrerPolicyFromHeader(headers);
if (responseReferrerPolicy) {
requestOptions.referrerPolicy = responseReferrerPolicy;
}
// HTTP-redirect fetch step 15
resolve(fetch(new Request(locationURL, requestOptions)));
finalize();
return;
}
default:
return reject(
new TypeError(
`Redirect option '${request.redirect}' is not a valid value of RequestRedirect`,
),
);
}
}
// Prepare response
if (signal) {
response_.once("end", () => {
signal.removeEventListener("abort", abortAndFinalize);
});
}
let body = pump(response_, new PassThrough(), (error) => {
if (error) {
reject(error);
}
});
// see https://github.com/nodejs/node/pull/29376
/* c8 ignore next 3 */
if (process.version < "v12.10") {
response_.on("aborted", abortAndFinalize);
}
const responseOptions = {
url: request.url,
status: response_.statusCode,
statusText: response_.statusMessage,
headers,
size: request.size,
counter: request.counter,
highWaterMark: request.highWaterMark,
};
// HTTP-network fetch step 12.1.1.3
const codings = headers.get("Content-Encoding");
// HTTP-network fetch step 12.1.1.4: handle content codings
// in following scenarios we ignore compression support
// 1. compression support is disabled
// 2. HEAD request
// 3. no Content-Encoding header
// 4. no content response (204)
// 5. content not modified response (304)
if (
!request.compress ||
request.method === "HEAD" ||
codings === null ||
response_.statusCode === 204 ||
response_.statusCode === 304
) {
response = new Response(body, responseOptions);
resolve(response);
return;
}
// For Node v6+
// Be less strict when decoding compressed responses, since sometimes
// servers send slightly invalid responses that are still accepted
// by common browsers.
// Always using Z_SYNC_FLUSH is what cURL does.
const zlibOptions = {
flush: zlib.Z_SYNC_FLUSH,
finishFlush: zlib.Z_SYNC_FLUSH,
};
// For gzip
if (codings === "gzip" || codings === "x-gzip") {
body = pump(body, zlib.createGunzip(zlibOptions), (error) => {
if (error) {
reject(error);
}
});
response = new Response(body, responseOptions);
resolve(response);
return;
}
// For deflate
if (codings === "deflate" || codings === "x-deflate") {
// Handle the infamous raw deflate response from old servers
// a hack for old IIS and Apache servers
const raw = pump(response_, new PassThrough(), (error) => {
if (error) {
reject(error);
}
});
raw.once("data", (chunk) => {
// See http://stackoverflow.com/questions/37519828
if ((chunk[0] & 0x0f) === 0x08) {
body = pump(body, zlib.createInflate(), (error) => {
if (error) {
reject(error);
}
});
} else {
body = pump(body, zlib.createInflateRaw(), (error) => {
if (error) {
reject(error);
}
});
}
response = new Response(body, responseOptions);
resolve(response);
});
raw.once("end", () => {
// Some old IIS servers return zero-length OK deflate responses, so
// 'data' is never emitted. See https://github.com/node-fetch/node-fetch/pull/903
if (!response) {
response = new Response(body, responseOptions);
resolve(response);
}
});
return;
}
// For br
if (codings === "br") {
body = pump(body, zlib.createBrotliDecompress(), (error) => {
if (error) {
reject(error);
}
});
response = new Response(body, responseOptions);
resolve(response);
return;
}
// Otherwise, use response as-is
response = new Response(body, responseOptions);
resolve(response);
});
// eslint-disable-next-line promise/prefer-await-to-then
writeToStream(request_, request).catch(reject);
});
}
function fixResponseChunkedTransferBadEnding(request, errorCallback) {
const LAST_CHUNK = Buffer.from("0\r\n\r\n");
let isChunkedTransfer = false;
let properLastChunkReceived = false;
let previousChunk;
request.on("response", (response) => {
const { headers } = response;
isChunkedTransfer =
headers["transfer-encoding"] === "chunked" && !headers["content-length"];
});
request.on("socket", (socket) => {
const onSocketClose = () => {
if (isChunkedTransfer && !properLastChunkReceived) {
const error = new Error("Premature close");
error.code = "ERR_STREAM_PREMATURE_CLOSE";
errorCallback(error);
}
};
// const onData = buf => {
// properLastChunkReceived = Buffer.compare(buf.slice(-5), LAST_CHUNK) === 0;
// // Sometimes final 0-length chunk and end of message code are in separate packets
// if (!properLastChunkReceived && previousChunk) {
// properLastChunkReceived = (
// Buffer.compare(previousChunk.slice(-3), LAST_CHUNK.slice(0, 3)) === 0 &&
// Buffer.compare(buf.slice(-2), LAST_CHUNK.slice(3)) === 0
// );
// }
// previousChunk = buf;
// };
// PATCH - THIS IS WHERE THE PATCH IS - FIXES BOUNDARY CHUNKING ISSUE
// See https://github.com/node-fetch/node-fetch/issues/1576
const onData = (buf) => {
properLastChunkReceived = Buffer.compare(buf.slice(-5), LAST_CHUNK) === 0;
// Sometimes final 0-length chunk and end of message code are in separate packets
if (!properLastChunkReceived && previousChunk) {
if (buf.length < 5) {
properLastChunkReceived =
Buffer.compare(
Buffer.from([...previousChunk.slice(-5), ...buf]).slice(-5),
LAST_CHUNK,
) === 0;
}
}
previousChunk = buf;
};
socket.prependListener("close", onSocketClose);
socket.on("data", onData);
request.on("close", () => {
socket.removeListener("close", onSocketClose);
socket.removeListener("data", onData);
});
});
}

View file

@ -0,0 +1,67 @@
import { Buffer } from "node:buffer";
import { describe, expect, it } from "vitest";
// Tests the boundary chunking patch to node fetch made in node-fetch-patch.js (see that file for details).
describe("Chunked Transfer Encoding Patch Logic", () => {
// The LAST_CHUNK marker "0\r\n\r\n"
const LAST_CHUNK = Buffer.from("0\r\n\r\n");
it("should detect complete marker at the end (normal case)", () => {
const normalCase = Buffer.from("data data data 0\r\n\r\n");
const result = Buffer.compare(normalCase.slice(-5), LAST_CHUNK) === 0;
expect(result).toBe(true);
});
it("should handle chunks split across original pattern (0\\r\\n | \\r\\n)", () => {
const previousChunk = Buffer.from("data data 0\r\n");
const lastChunk = Buffer.from("\r\n");
const result =
Buffer.compare(previousChunk.slice(-3), LAST_CHUNK.slice(0, 3)) === 0 &&
Buffer.compare(lastChunk.slice(-2), LAST_CHUNK.slice(3)) === 0;
expect(result).toBe(true);
});
it("should handle problematic case from issue #1576 with new patch", () => {
// The problematic case: data ends with "0\r\n0\r" and the next chunk is "\n\r\n"
const previousChunk = Buffer.from([
...Array(10).fill(42),
48,
13,
10,
48,
13,
]); // some data + "0\r\n0\r"
const lastChunk = Buffer.from([10, 13, 10]); // "\n\r\n"
// Check 1: Direct check for complete marker (should fail)
const check1 = Buffer.compare(lastChunk.slice(-5), LAST_CHUNK) === 0;
expect(check1).toBe(false);
// Check 2: Original split pattern check (should fail)
const check2 =
Buffer.compare(previousChunk.slice(-3), LAST_CHUNK.slice(0, 3)) === 0 &&
Buffer.compare(lastChunk.slice(-2), LAST_CHUNK.slice(3)) === 0;
expect(check2).toBe(false);
// Check 3: New patch check (should pass)
const lastChunkLength = lastChunk.length;
const newPatchCheck =
lastChunkLength < 5 &&
Buffer.compare(
Buffer.from([...previousChunk.slice(-5), ...lastChunk]).slice(-5),
LAST_CHUNK,
) === 0;
expect(newPatchCheck).toBe(true);
// Verification of the combined buffer
const combinedBuffer = Buffer.from([
...previousChunk.slice(-5),
...lastChunk,
]);
const combinedResult =
Buffer.compare(combinedBuffer.slice(-5), LAST_CHUNK) === 0;
expect(combinedResult).toBe(true);
});
});