janja 4.0.0 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -165,6 +165,15 @@ declare class BlockNode extends Traversal {
165
165
  constructor(val: IdExp, body: SyntaxNode[], loc: Loc, strip: Strip);
166
166
  }
167
167
 
168
+ /** CSS co-located with a section/snippet (Shopify `{% stylesheet %}` analogue). */
169
+ declare class StylesheetNode extends Traversal {
170
+ readonly body: SyntaxNode[];
171
+ readonly loc: Loc;
172
+ readonly strip: Strip;
173
+ readonly type = "STYLESHEET";
174
+ constructor(body: SyntaxNode[], loc: Loc, strip: Strip);
175
+ }
176
+
168
177
  interface CompiledTemplate {
169
178
  code: string;
170
179
  mappings: Mapping[];
@@ -185,6 +194,10 @@ declare class Compiler extends Context {
185
194
  private rootNode;
186
195
  private partials;
187
196
  private blocks;
197
+ /** Stylesheet bodies keyed by include path (or `\0root` for the root template). */
198
+ private stylesheets;
199
+ /** Key used when collecting stylesheets from the template currently compiling. */
200
+ private stylesheetKey;
188
201
  private sources;
189
202
  private cache?;
190
203
  private compiled?;
@@ -194,10 +207,15 @@ declare class Compiler extends Context {
194
207
  */
195
208
  flushable: boolean;
196
209
  constructor(options: Required<CompilerOptions>, cache?: LRUCache<CompiledTemplate>, revalidate?: boolean, stream?: boolean);
197
- compile(template: string, root?: boolean, partials?: Map<string, string>, blocks?: Map<string, BlockNode[]>, sources?: Map<string, string>): Promise<this>;
210
+ compile(template: string, root?: boolean, partials?: Map<string, string>, blocks?: Map<string, BlockNode[]>, sources?: Map<string, string>, stylesheets?: Map<string, StylesheetNode[]>, stylesheetKey?: string): Promise<this>;
198
211
  get script(): Script;
199
212
  private generateCacheKey;
200
213
  private fingerprint;
214
+ /**
215
+ * Shared counters for optional security limits. Declared once on the root
216
+ * template so nested includes, macros and loops all see the same budget.
217
+ */
218
+ private pushResourceLimitCounters;
201
219
  private isFresh;
202
220
  private parseTemplates;
203
221
  private collectPartialsAndBlocks;
@@ -242,7 +260,46 @@ interface SecurityOptions {
242
260
  deepEscape?: boolean;
243
261
  cspNonce?: string;
244
262
  cspHeaders?: Record<string, string>;
245
- }
263
+ /**
264
+ * Maximum nest depth of `include` while rendering. Defaults to 100.
265
+ * Cycles that never end become an error instead of a stack overflow.
266
+ */
267
+ maxIncludeDepth?: number;
268
+ /**
269
+ * Maximum number of `for` loop iterations across one render. Off by default.
270
+ * Enable when templates may be untrusted.
271
+ */
272
+ maxLoopIterations?: number;
273
+ /**
274
+ * Maximum nest depth of macro invocations across one render. Off by default.
275
+ */
276
+ maxCallDepth?: number;
277
+ /**
278
+ * Wall-clock budget for one `render` / `renderCompiled` call, in
279
+ * milliseconds. Off by default. The running script is not aborted; the
280
+ * promise rejects when the budget is exceeded. Prefer a separate process
281
+ * for truly hostile templates.
282
+ */
283
+ timeoutMs?: number;
284
+ }
285
+ /** Defaults and resolved limits used by the compiler and renderer. */
286
+ declare function resolveSecurityLimits(security?: SecurityOptions): {
287
+ maxIncludeDepth: number;
288
+ maxLoopIterations: number | undefined;
289
+ maxCallDepth: number | undefined;
290
+ timeoutMs: number | undefined;
291
+ };
292
+ declare class SecuritySandbox {
293
+ private allowedGlobals;
294
+ private cspNonce?;
295
+ constructor(options?: SecurityOptions);
296
+ sanitizeGlobals(globals: ObjectType): ObjectType;
297
+ validateExpression(expression: string): boolean;
298
+ createSecureContext(globals: ObjectType): ObjectType;
299
+ getCSPNonce(): string | undefined;
300
+ generateCSPHeaders(): Record<string, string>;
301
+ }
302
+ declare function deepEscape(value: unknown, seen?: WeakSet<object>): string;
246
303
  interface SecurityAuditResult {
247
304
  issues: SecurityIssue[];
248
305
  severity: 'low' | 'medium' | 'high' | 'critical';
@@ -253,6 +310,10 @@ interface SecurityIssue {
253
310
  location?: string;
254
311
  severity: 'low' | 'medium' | 'high' | 'critical';
255
312
  }
313
+ declare class SecurityAuditor {
314
+ auditTemplate(template: string): SecurityAuditResult;
315
+ private calculateOverallSeverity;
316
+ }
256
317
 
257
318
  type MaybePromise<T> = T | Promise<T>;
258
319
  type ObjectType<T = any> = Record<string, T>;
@@ -927,17 +988,30 @@ declare function generateDeclarations({ variables }: Pick<TemplateAnalysis, 'var
927
988
  includeRuntime?: boolean | undefined;
928
989
  }): string;
929
990
 
930
- type FormatOptions = Pick<ParserOptions, 'commentOpen' | 'commentClose' | 'directiveOpen' | 'directiveClose' | 'outputOpen' | 'outputClose'>;
991
+ type FormatOptions = Pick<ParserOptions, 'commentOpen' | 'commentClose' | 'directiveOpen' | 'directiveClose' | 'outputOpen' | 'outputClose'> & {
992
+ /** Spaces per indent level when reformatting HTML (default 2). */
993
+ tabWidth?: number;
994
+ /**
995
+ * Reindent HTML and block directives. Default `true`. Pass `false` to only
996
+ * normalise spacing inside tags (legacy behaviour).
997
+ */
998
+ html?: boolean;
999
+ };
931
1000
  /**
932
- * Normalise the tags of a template:
1001
+ * Format a template:
1002
+ *
1003
+ * 1. Normalise spacing inside tags:
1004
+ *
1005
+ * {{=name|upper}} -> {{= name | upper }}
1006
+ * {{if a eq 1}} -> {{ if a eq 1 }}
1007
+ * {{#note#}} -> {{# note #}}
933
1008
  *
934
- * {{=name|upper}} -> {{= name | upper }}
935
- * {{if a eq 1}} -> {{ if a eq 1 }}
936
- * {{#note#}} -> {{# note #}}
1009
+ * 2. Unless `html: false`, reindent HTML elements and block directives
1010
+ * (`if` / `for` / …). Only leading whitespace on each line changes;
1011
+ * attribute values (including embedded tags) and whitespace-sensitive
1012
+ * elements (`pre`, `script`, …) are left alone.
937
1013
  *
938
- * Only what is inside tags changes. Text between tags, where whitespace may
939
- * matter, is kept byte for byte, as are whitespace-control markers. A tag is
940
- * left exactly as written whenever its expression does not parse or
1014
+ * A tag is left exactly as written whenever its expression does not parse or
941
1015
  * reformatting would not provably keep its meaning. Formatting twice gives
942
1016
  * the same result as formatting once.
943
1017
  *
@@ -958,7 +1032,7 @@ interface FileLoaderOptions {
958
1032
  * Paths are resolved against `root`, with or without an extension, and may
959
1033
  * not lead outside of it, so that a template cannot include arbitrary files.
960
1034
  */
961
- declare function createFileLoader(root: string, { extensions }?: FileLoaderOptions): (name: string) => Promise<string>;
1035
+ declare function createFileLoader(root: string, { extensions }?: FileLoaderOptions): (name: string) => Promise<any>;
962
1036
 
963
1037
  interface ErrorRecoveryOptions {
964
1038
  /**
@@ -1028,5 +1102,5 @@ declare function renderCompiled(script: Script, data?: ObjectType, options?: Ren
1028
1102
  */
1029
1103
  declare function renderStream(template: string, data?: ObjectType, options?: RendererOptions): ReadableStream<string>;
1030
1104
 
1031
- export { CompileError, Compiler, ErrorRecovery, ExpCompiler, ExpError, ExpParser, ExpTokenizer, I18n, MAX_INCLUDE_DEPTH, MixinRegistry, Parser, RenderError, Renderer, RootNode, Safe, TemplatePipeline, TemplateValidator, TokenType, Tokenizer, Traversal, analyzeTemplate, applyMixin, applyMixins, applyWhitespaceControl, baseOptions, compilerOptions, composeLayout, composeTemplates, composeTemplatesWithData, composeWithSlots, createErrorRecovery, createFileLoader, createMixin, defaultErrorHandler, defaultWhitespaceOptions, defineFilters, definePlugin, expTokenTypes, extendTemplateWithMixin, filters, formatTemplate, generateDeclarations, generateSourceMap, getWhitespaceCompilerOptions, getWhitespaceRendererOptions, globalMixinRegistry, i18n, isForbiddenIdentifier, mergeOptions, mergeWhitespaceOptions, optionsFingerprint, parserOptions, precompile, precompileTemplate, render, renderCompiled, renderFile, renderOptions, renderStream, templateCacheKey, validateTemplate };
1032
- export type { BaseOptions, BinaryExp, BinaryExpTokenType, BinaryExpType, CacheOptions, Checker, CommentToken, CompiledTemplate, CompilerFn, CompilerMap, CompilerOptions, CompositionOptions, DirectiveExpression, DirectiveToken, ErrorContext, ErrorRecoveryOptions, Exp, ExpBase, ExpToken, ExpTokenType, ExpType, FileLoaderOptions, Filter, Filters, FormatOptions, FormatterFn, FormatterMap, FormatterOptions, I18nOptions, IdExp, IfExp, LitExp, Loc, MaybePromise, MixinDefinition, MixinOptions, NotExp, ObjectType, OutputToken, ParserFn, ParserMap, ParserOptions, Plugin, PluralRules, Pos, PrecompileOptions, PrecompileResult, Primitive, RendererOptions, Script, SeqExp, SourceMapOptions, SourceMapV3, Strip, SyntaxNode, TemplateAnalysis, TernaryExpTokenType, TernaryExpType, TextToken, Token, TokenBase, TranslationDict, UnaryExpTokenType, UnaryExpType, ValidationError, ValidationResult, WhitespaceOptions };
1105
+ export { CompileError, Compiler, ErrorRecovery, ExpCompiler, ExpError, ExpParser, ExpTokenizer, I18n, MAX_INCLUDE_DEPTH, MixinRegistry, Parser, RenderError, Renderer, RootNode, Safe, SecurityAuditor, SecuritySandbox, TemplatePipeline, TemplateValidator, TokenType, Tokenizer, Traversal, analyzeTemplate, applyMixin, applyMixins, applyWhitespaceControl, baseOptions, compilerOptions, composeLayout, composeTemplates, composeTemplatesWithData, composeWithSlots, createErrorRecovery, createFileLoader, createMixin, deepEscape, defaultErrorHandler, defaultWhitespaceOptions, defineFilters, definePlugin, expTokenTypes, extendTemplateWithMixin, filters, formatTemplate, generateDeclarations, generateSourceMap, getWhitespaceCompilerOptions, getWhitespaceRendererOptions, globalMixinRegistry, i18n, isForbiddenIdentifier, mergeOptions, mergeWhitespaceOptions, optionsFingerprint, parserOptions, precompile, precompileTemplate, render, renderCompiled, renderFile, renderOptions, renderStream, resolveSecurityLimits, templateCacheKey, validateTemplate };
1106
+ export type { BaseOptions, BinaryExp, BinaryExpTokenType, BinaryExpType, CacheOptions, Checker, CommentToken, CompiledTemplate, CompilerFn, CompilerMap, CompilerOptions, CompositionOptions, DirectiveExpression, DirectiveToken, ErrorContext, ErrorRecoveryOptions, Exp, ExpBase, ExpToken, ExpTokenType, ExpType, FileLoaderOptions, Filter, Filters, FormatOptions, FormatterFn, FormatterMap, FormatterOptions, I18nOptions, IdExp, IfExp, LitExp, Loc, MaybePromise, MixinDefinition, MixinOptions, NotExp, ObjectType, OutputToken, ParserFn, ParserMap, ParserOptions, Plugin, PluralRules, Pos, PrecompileOptions, PrecompileResult, Primitive, RendererOptions, Script, SecurityAuditResult, SecurityIssue, SecurityOptions, SeqExp, SourceMapOptions, SourceMapV3, Strip, SyntaxNode, TemplateAnalysis, TernaryExpTokenType, TernaryExpType, TextToken, Token, TokenBase, TranslationDict, UnaryExpTokenType, UnaryExpType, ValidationError, ValidationResult, WhitespaceOptions };