workers-start 0.3.0 → 0.5.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/src/index.ts CHANGED
@@ -1,6 +1,11 @@
1
1
  export type { MediaRange } from "./accept.js";
2
2
  export { parseAccept, quality } from "./accept.js";
3
- export type { EntryOptions, FetchHandler, MarkdownPage, MarkdownResult } from "./entry.js";
3
+ export type {
4
+ EntryOptions,
5
+ FetchHandler,
6
+ MarkdownPage,
7
+ MarkdownResult,
8
+ } from "./entry.js";
4
9
  export {
5
10
  acceptsHtml,
6
11
  createEntry,
@@ -11,9 +16,24 @@ export {
11
16
  underPrefix,
12
17
  wantsMarkdown,
13
18
  } from "./entry.js";
14
- export type { HeadLink, HeadScript, HeadTags, MetaTag, PageImage, PageSeo, SiteHead } from "./head.js";
19
+ export type {
20
+ HeadLink,
21
+ HeadScript,
22
+ HeadTags,
23
+ MetaTag,
24
+ PageImage,
25
+ PageSeo,
26
+ SiteHead,
27
+ } from "./head.js";
15
28
  export { createPageHead, jsonLdScript } from "./head.js";
16
29
  export type { AnalyticsScriptOptions, ThemeScriptOptions } from "./scripts.js";
17
30
  export { analyticsScript, inlineJson, themeScript } from "./scripts.js";
18
31
  export type { WorkersModule } from "./workers.js";
19
- export { afterResponse, getBinding, getEnvString, isKvNamespace, loadWorkers, setWorkersModule } from "./workers.js";
32
+ export {
33
+ afterResponse,
34
+ getBinding,
35
+ getEnvString,
36
+ isKvNamespace,
37
+ loadWorkers,
38
+ setWorkersModule,
39
+ } from "./workers.js";
package/src/scripts.ts CHANGED
@@ -66,17 +66,16 @@ export interface AnalyticsScriptOptions {
66
66
  consentCookie?: { name: string; value?: string };
67
67
  }
68
68
 
69
- /**
70
- * Google Analytics 4 bootstrap gated on `location.hostname`, so local dev and
71
- * preview deploys never fetch gtag.js or land in the production property.
72
- */
73
- export function analyticsScript(options: AnalyticsScriptOptions): string {
74
- if (!/^G-[A-Z0-9]+$/.test(options.measurementId)) {
75
- throw new Error(`Not a GA4 measurement id: ${options.measurementId}`);
76
- }
77
- const id = inlineJson(options.measurementId);
69
+ /** The hostname, path and consent checks both analytics bootstraps start with. */
70
+ function gateChecks(options: {
71
+ hosts: string[];
72
+ skipPaths?: string[];
73
+ consentCookie?: { name: string; value?: string };
74
+ }): string {
78
75
  const hosts = inlineJson(options.hosts.map((h) => h.toLowerCase()));
79
- const checks = [`if(${hosts}.indexOf(location.hostname.toLowerCase())===-1)return;`];
76
+ const checks = [
77
+ `if(${hosts}.indexOf(location.hostname.toLowerCase())===-1)return;`,
78
+ ];
80
79
  if (options.skipPaths?.length) {
81
80
  checks.push(
82
81
  `var p=location.pathname;if(${inlineJson(options.skipPaths)}.some(function(s){return p===s||p.indexOf(s.replace(/\\/$/,"")+"/")===0}))return;`,
@@ -84,10 +83,42 @@ export function analyticsScript(options: AnalyticsScriptOptions): string {
84
83
  }
85
84
  if (options.consentCookie) {
86
85
  const pair =
87
- options.consentCookie.value === undefined ? null : `${options.consentCookie.name}=${options.consentCookie.value}`;
86
+ options.consentCookie.value === undefined
87
+ ? null
88
+ : `${options.consentCookie.name}=${options.consentCookie.value}`;
88
89
  checks.push(
89
90
  `var k=document.cookie.split(/;\\s*/);if(!k.some(function(c){return ${pair === null ? `c.split("=")[0]===${inlineJson(options.consentCookie.name)}` : `c===${inlineJson(pair)}`}}))return;`,
90
91
  );
91
92
  }
92
- return `(function(){${checks.join("")}var s=document.createElement("script");s.async=true;s.src="https://www.googletagmanager.com/gtag/js?id="+${id};document.head.appendChild(s);window.dataLayer=window.dataLayer||[];function gtag(){window.dataLayer.push(arguments);}window.gtag=gtag;gtag("js",new Date());gtag("config",${id});})();`;
93
+ return checks.join("");
94
+ }
95
+
96
+ /**
97
+ * Google Analytics 4 bootstrap gated on `location.hostname`, so local dev and
98
+ * preview deploys never fetch gtag.js or land in the production property.
99
+ */
100
+ export function analyticsScript(options: AnalyticsScriptOptions): string {
101
+ if (!/^G-[A-Z0-9]+$/.test(options.measurementId)) {
102
+ throw new Error(`Not a GA4 measurement id: ${options.measurementId}`);
103
+ }
104
+ const id = inlineJson(options.measurementId);
105
+ return `(function(){${gateChecks(options)}var s=document.createElement("script");s.async=true;s.src="https://www.googletagmanager.com/gtag/js?id="+${id};document.head.appendChild(s);window.dataLayer=window.dataLayer||[];function gtag(){window.dataLayer.push(arguments);}window.gtag=gtag;gtag("js",new Date());gtag("config",${id});})();`;
106
+ }
107
+
108
+ export interface TagManagerScriptOptions
109
+ extends Omit<AnalyticsScriptOptions, "measurementId"> {
110
+ /** Google Tag Manager container id, `GTM-…`. Public; safe in the bundle. */
111
+ containerId: string;
112
+ }
113
+
114
+ /**
115
+ * Google Tag Manager bootstrap with the same gates as `analyticsScript`: only
116
+ * the listed hosts load gtm.js, so local dev and previews never fire tags.
117
+ */
118
+ export function tagManagerScript(options: TagManagerScriptOptions): string {
119
+ if (!/^GTM-[A-Z0-9]+$/.test(options.containerId)) {
120
+ throw new Error(`Not a Tag Manager container id: ${options.containerId}`);
121
+ }
122
+ const id = inlineJson(options.containerId);
123
+ return `(function(){${gateChecks(options)}window.dataLayer=window.dataLayer||[];window.dataLayer.push({"gtm.start":new Date().getTime(),event:"gtm.js"});var s=document.createElement("script");s.async=true;s.src="https://www.googletagmanager.com/gtm.js?id="+${id};document.head.appendChild(s);})();`;
93
124
  }
package/src/workers.ts CHANGED
@@ -21,7 +21,10 @@ let modulePromise: Promise<WorkersModule | null> | null = null;
21
21
  let override: WorkersModule | null | undefined;
22
22
 
23
23
  function processEnv(): Record<string, string | undefined> {
24
- return (globalThis as { process?: { env?: Record<string, string | undefined> } }).process?.env ?? {};
24
+ return (
25
+ (globalThis as { process?: { env?: Record<string, string | undefined> } })
26
+ .process?.env ?? {}
27
+ );
25
28
  }
26
29
 
27
30
  /** The `cloudflare:workers` module, or null outside workerd. */
@@ -47,7 +50,10 @@ export function setWorkersModule(mod: WorkersModule | null | undefined): void {
47
50
  }
48
51
 
49
52
  /** A binding by name, or null when it is missing or fails the guard. */
50
- export async function getBinding<T>(name: string, guard?: (value: unknown) => value is T): Promise<T | null> {
53
+ export async function getBinding<T>(
54
+ name: string,
55
+ guard?: (value: unknown) => value is T,
56
+ ): Promise<T | null> {
51
57
  const mod = await loadWorkers();
52
58
  const value = mod?.env[name];
53
59
  if (value === undefined || value === null) {
@@ -91,12 +97,20 @@ export async function afterResponse(work: Promise<unknown>): Promise<void> {
91
97
  /** True for a KV namespace (it has `getWithMetadata`, which R2 buckets do not). Use as a `getBinding` guard. */
92
98
  export function isKvNamespace(value: unknown): value is {
93
99
  get(key: string): Promise<string | null>;
94
- put(key: string, value: string, options?: { expirationTtl?: number }): Promise<void>;
100
+ put(
101
+ key: string,
102
+ value: string,
103
+ options?: { expirationTtl?: number },
104
+ ): Promise<void>;
95
105
  } {
96
106
  if (typeof value !== "object" || value === null) {
97
107
  return false;
98
108
  }
99
- const candidate = value as { get?: unknown; put?: unknown; getWithMetadata?: unknown };
109
+ const candidate = value as {
110
+ get?: unknown;
111
+ put?: unknown;
112
+ getWithMetadata?: unknown;
113
+ };
100
114
  return (
101
115
  typeof candidate.get === "function" &&
102
116
  typeof candidate.put === "function" &&
package/biome.preset.json DELETED
@@ -1,47 +0,0 @@
1
- {
2
- "$schema": "https://biomejs.dev/schemas/2.6.8/schema.json",
3
- "formatter": {
4
- "indentStyle": "space",
5
- "indentWidth": 2,
6
- "lineEnding": "lf",
7
- "lineWidth": 80
8
- },
9
- "javascript": {
10
- "formatter": {
11
- "arrowParentheses": "always",
12
- "jsxQuoteStyle": "double",
13
- "quoteProperties": "asNeeded",
14
- "semicolons": "always",
15
- "trailingCommas": "es5"
16
- }
17
- },
18
- "json": {
19
- "formatter": {
20
- "trailingCommas": "none"
21
- }
22
- },
23
- "css": {
24
- "parser": {
25
- "tailwindDirectives": true
26
- }
27
- },
28
- "assist": {
29
- "actions": {
30
- "source": {
31
- "organizeImports": "on",
32
- "useSortedAttributes": "on"
33
- }
34
- }
35
- },
36
- "linter": {
37
- "rules": {
38
- "preset": "recommended",
39
- "security": {
40
- "noDangerouslySetInnerHtml": "off"
41
- },
42
- "performance": {
43
- "useTopLevelRegex": "error"
44
- }
45
- }
46
- }
47
- }