@leadertechie/r2tohtml 0.1.0-alpha.1
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/LICENSE +21 -0
- package/README.md +125 -0
- package/dist/cache.d.ts +18 -0
- package/dist/cache.d.ts.map +1 -0
- package/dist/cache.js +46 -0
- package/dist/frontmatter.d.ts +7 -0
- package/dist/frontmatter.d.ts.map +1 -0
- package/dist/frontmatter.js +42 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/loader.d.ts +35 -0
- package/dist/loader.d.ts.map +1 -0
- package/dist/loader.js +123 -0
- package/dist/types.d.ts +52 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/package.json +44 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 leadertechie
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# @leadertechie/r2tohtml
|
|
2
|
+
|
|
3
|
+
A generic, configuration-driven R2 content loader for Cloudflare Workers. Fetch, cache, and parse content from R2 without hardcoded paths.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Generic R2 operations** - No hardcoded content structure
|
|
8
|
+
- **In-memory caching** - Configurable TTL with cache invalidation
|
|
9
|
+
- **Frontmatter parsing** - Extract metadata from markdown files
|
|
10
|
+
- **TypeScript support** - Full type definitions included
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @leadertechie/r2tohtml
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
### Basic Usage
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { R2ContentLoader } from '@leadertechie/r2tohtml';
|
|
24
|
+
|
|
25
|
+
const loader = new R2ContentLoader({
|
|
26
|
+
bucket: env.CONTENT_BUCKET, // Cloudflare R2 binding
|
|
27
|
+
cacheTTL: 5 * 60 * 1000, // 5 minutes (default)
|
|
28
|
+
cacheEnabled: true // Enable caching (default: true)
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// Fetch a file
|
|
32
|
+
const content = await loader.get('about.md');
|
|
33
|
+
|
|
34
|
+
// Fetch with metadata (parses YAML frontmatter)
|
|
35
|
+
const { metadata, content } = await loader.getWithMetadata('blog/post.md');
|
|
36
|
+
|
|
37
|
+
// List files in a prefix
|
|
38
|
+
const result = await loader.list('blogs/');
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Configuration
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
import { R2ContentLoader } from '@leadertechie/r2tohtml';
|
|
45
|
+
|
|
46
|
+
const loader = new R2ContentLoader({
|
|
47
|
+
// R2 bucket binding (required)
|
|
48
|
+
bucket: env.CONTENT_BUCKET,
|
|
49
|
+
|
|
50
|
+
// Optional prefix for all operations
|
|
51
|
+
prefix: 'content/',
|
|
52
|
+
|
|
53
|
+
// Cache TTL in milliseconds (default: 5 minutes)
|
|
54
|
+
cacheTTL: 5 * 60 * 1000,
|
|
55
|
+
|
|
56
|
+
// Enable/disable caching (default: true)
|
|
57
|
+
cacheEnabled: true
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### API
|
|
62
|
+
|
|
63
|
+
#### `R2ContentLoader`
|
|
64
|
+
|
|
65
|
+
| Method | Description |
|
|
66
|
+
|--------|-------------|
|
|
67
|
+
| `get(path: string): Promise<string | null>` | Fetch file content as string |
|
|
68
|
+
| `getObject(path: string): Promise<R2Object | null>` | Fetch raw R2 object |
|
|
69
|
+
| `getWithMetadata(path: string): Promise<ParsedContent | null>` | Fetch and parse frontmatter |
|
|
70
|
+
| `list(prefix: string): Promise<R2ListResult>` | List files with prefix |
|
|
71
|
+
| `exists(path: string): Promise<boolean>` | Check if file exists |
|
|
72
|
+
| `invalidate(path: string): void` | Invalidate cache for specific path |
|
|
73
|
+
| `invalidatePrefix(prefix: string): void` | Invalidate cache for prefix |
|
|
74
|
+
| `clearCache(): void` | Clear all cached data |
|
|
75
|
+
| `disableCache(): void` | Disable caching |
|
|
76
|
+
| `enableCache(): void` | Enable caching |
|
|
77
|
+
|
|
78
|
+
### Types
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
interface R2LoaderConfig {
|
|
82
|
+
bucket: R2Bucket;
|
|
83
|
+
prefix?: string;
|
|
84
|
+
cacheTTL?: number;
|
|
85
|
+
cacheEnabled?: boolean;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
interface ParsedContent {
|
|
89
|
+
metadata: ContentMetadata;
|
|
90
|
+
content: string;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
interface ContentMetadata {
|
|
94
|
+
[key: string]: string | string[] | undefined;
|
|
95
|
+
title?: string;
|
|
96
|
+
description?: string;
|
|
97
|
+
date?: string;
|
|
98
|
+
tags?: string[];
|
|
99
|
+
author?: string;
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Example: Using with md2html
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
import { R2ContentLoader } from '@leadertechie/r2tohtml';
|
|
107
|
+
import { MarkdownPipeline } from '@leadertechie/md2html';
|
|
108
|
+
|
|
109
|
+
const r2 = new R2ContentLoader({
|
|
110
|
+
bucket: env.CONTENT_BUCKET,
|
|
111
|
+
prefix: 'content/'
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
const pipeline = new MarkdownPipeline({
|
|
115
|
+
imagePathPrefix: 'images/'
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
// Fetch and render markdown content
|
|
119
|
+
const content = await r2.get('home.md');
|
|
120
|
+
const html = pipeline.renderMarkdown(content);
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## License
|
|
124
|
+
|
|
125
|
+
MIT
|
package/dist/cache.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface CacheEntry<T> {
|
|
2
|
+
data: T;
|
|
3
|
+
timestamp: number;
|
|
4
|
+
}
|
|
5
|
+
export declare class ContentCache {
|
|
6
|
+
private cache;
|
|
7
|
+
private ttl;
|
|
8
|
+
private enabled;
|
|
9
|
+
constructor(ttl?: number, enabled?: boolean);
|
|
10
|
+
get<T>(key: string): T | null;
|
|
11
|
+
set<T>(key: string, data: T): void;
|
|
12
|
+
delete(key: string): void;
|
|
13
|
+
clear(): void;
|
|
14
|
+
clearPrefix(prefix: string): void;
|
|
15
|
+
setTTL(ttl: number): void;
|
|
16
|
+
setEnabled(enabled: boolean): void;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=cache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,UAAU,CAAC,CAAC;IAC3B,IAAI,EAAE,CAAC,CAAC;IACR,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,qBAAa,YAAY;IACvB,OAAO,CAAC,KAAK,CAAmC;IAChD,OAAO,CAAC,GAAG,CAAS;IACpB,OAAO,CAAC,OAAO,CAAU;gBAEb,GAAG,GAAE,MAAsB,EAAE,OAAO,GAAE,OAAc;IAMhE,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,CAAC,GAAG,IAAI;IAc7B,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,GAAG,IAAI;IASlC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAIzB,KAAK,IAAI,IAAI;IAIb,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAQjC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAIzB,UAAU,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI;CAGnC"}
|
package/dist/cache.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export class ContentCache {
|
|
2
|
+
constructor(ttl = 5 * 60 * 1000, enabled = true) {
|
|
3
|
+
this.cache = new Map();
|
|
4
|
+
this.ttl = ttl;
|
|
5
|
+
this.enabled = enabled;
|
|
6
|
+
}
|
|
7
|
+
get(key) {
|
|
8
|
+
if (!this.enabled)
|
|
9
|
+
return null;
|
|
10
|
+
const entry = this.cache.get(key);
|
|
11
|
+
if (!entry)
|
|
12
|
+
return null;
|
|
13
|
+
if (Date.now() - entry.timestamp > this.ttl) {
|
|
14
|
+
this.cache.delete(key);
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
return entry.data;
|
|
18
|
+
}
|
|
19
|
+
set(key, data) {
|
|
20
|
+
if (!this.enabled)
|
|
21
|
+
return;
|
|
22
|
+
this.cache.set(key, {
|
|
23
|
+
data,
|
|
24
|
+
timestamp: Date.now()
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
delete(key) {
|
|
28
|
+
this.cache.delete(key);
|
|
29
|
+
}
|
|
30
|
+
clear() {
|
|
31
|
+
this.cache.clear();
|
|
32
|
+
}
|
|
33
|
+
clearPrefix(prefix) {
|
|
34
|
+
for (const key of this.cache.keys()) {
|
|
35
|
+
if (key.startsWith(prefix)) {
|
|
36
|
+
this.cache.delete(key);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
setTTL(ttl) {
|
|
41
|
+
this.ttl = ttl;
|
|
42
|
+
}
|
|
43
|
+
setEnabled(enabled) {
|
|
44
|
+
this.enabled = enabled;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ContentMetadata } from './types';
|
|
2
|
+
export declare function parseFrontmatter(content: string): {
|
|
3
|
+
metadata: ContentMetadata;
|
|
4
|
+
content: string;
|
|
5
|
+
};
|
|
6
|
+
export declare function stringifyFrontmatter(metadata: ContentMetadata): string;
|
|
7
|
+
//# sourceMappingURL=frontmatter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"frontmatter.d.ts","sourceRoot":"","sources":["../src/frontmatter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE1C,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG;IAAE,QAAQ,EAAE,eAAe,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CA8BhG;AAED,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,eAAe,GAAG,MAAM,CAatE"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export function parseFrontmatter(content) {
|
|
2
|
+
const lines = content.split('\n');
|
|
3
|
+
const metadata = {};
|
|
4
|
+
let contentStart = 0;
|
|
5
|
+
if (lines[0]?.trim() === '---') {
|
|
6
|
+
for (let i = 1; i < lines.length; i++) {
|
|
7
|
+
if (lines[i]?.trim() === '---') {
|
|
8
|
+
contentStart = i + 1;
|
|
9
|
+
break;
|
|
10
|
+
}
|
|
11
|
+
const colonIdx = lines[i].indexOf(':');
|
|
12
|
+
if (colonIdx > 0) {
|
|
13
|
+
const key = lines[i].slice(0, colonIdx).trim();
|
|
14
|
+
let value = lines[i].slice(colonIdx + 1).trim();
|
|
15
|
+
if (value.startsWith('[') && value.endsWith(']')) {
|
|
16
|
+
value = value.slice(1, -1);
|
|
17
|
+
metadata[key] = value.split(',').map(v => v.trim());
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
metadata[key] = value;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return {
|
|
26
|
+
metadata,
|
|
27
|
+
content: lines.slice(contentStart).join('\n').trim()
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
export function stringifyFrontmatter(metadata) {
|
|
31
|
+
const lines = ['---'];
|
|
32
|
+
for (const [key, value] of Object.entries(metadata)) {
|
|
33
|
+
if (Array.isArray(value)) {
|
|
34
|
+
lines.push(`${key}: [${value.join(', ')}]`);
|
|
35
|
+
}
|
|
36
|
+
else if (value !== undefined) {
|
|
37
|
+
lines.push(`${key}: ${value}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
lines.push('---');
|
|
41
|
+
return lines.join('\n');
|
|
42
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,eAAe,CAAC;AAC9B,cAAc,UAAU,CAAC"}
|
package/dist/index.js
ADDED
package/dist/loader.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { R2LoaderConfig, R2Object, R2ListResult, ContentMetadata, ParsedContent } from './types';
|
|
2
|
+
import type { ContentNode, PipelineConfig } from '@leadertechie/md2html';
|
|
3
|
+
export interface RenderedContent {
|
|
4
|
+
metadata: ContentMetadata;
|
|
5
|
+
content: string;
|
|
6
|
+
}
|
|
7
|
+
export interface ASTContent {
|
|
8
|
+
metadata: ContentMetadata;
|
|
9
|
+
contentNodes: ContentNode[];
|
|
10
|
+
}
|
|
11
|
+
export interface R2LoaderOptions {
|
|
12
|
+
md2html?: PipelineConfig;
|
|
13
|
+
}
|
|
14
|
+
export declare class R2ContentLoader {
|
|
15
|
+
private bucket;
|
|
16
|
+
private prefix;
|
|
17
|
+
private cache;
|
|
18
|
+
private pipeline;
|
|
19
|
+
constructor(config: R2LoaderConfig, options?: R2LoaderOptions);
|
|
20
|
+
private getKey;
|
|
21
|
+
get(path: string): Promise<string | null>;
|
|
22
|
+
getObject(path: string): Promise<R2Object | null>;
|
|
23
|
+
getWithMetadata(path: string): Promise<ParsedContent | null>;
|
|
24
|
+
getWithAST(path: string): Promise<ASTContent | null>;
|
|
25
|
+
getRendered(path: string): Promise<RenderedContent | null>;
|
|
26
|
+
list(prefix?: string): Promise<R2ListResult>;
|
|
27
|
+
exists(path: string): Promise<boolean>;
|
|
28
|
+
invalidate(path: string): void;
|
|
29
|
+
invalidatePrefix(prefix: string): void;
|
|
30
|
+
clearCache(): void;
|
|
31
|
+
setCacheTTL(ttl: number): void;
|
|
32
|
+
disableCache(): void;
|
|
33
|
+
enableCache(): void;
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=loader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loader.d.ts","sourceRoot":"","sources":["../src/loader.ts"],"names":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AM,GAAG,IAAI;IAS9B,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAKtC,UAAU,IAAI,IAAI;IAIlB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAI9B,YAAY,IAAI,IAAI;IAIpB,WAAW,IAAI,IAAI;CAGpB"}
|
package/dist/loader.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { ContentCache } from './cache';
|
|
2
|
+
import { parseFrontmatter } from './frontmatter';
|
|
3
|
+
import { MarkdownPipeline } from '@leadertechie/md2html';
|
|
4
|
+
export class R2ContentLoader {
|
|
5
|
+
constructor(config, options) {
|
|
6
|
+
this.bucket = config.bucket;
|
|
7
|
+
this.prefix = config.prefix || '';
|
|
8
|
+
this.cache = new ContentCache(config.cacheTTL || 5 * 60 * 1000, config.cacheEnabled !== false);
|
|
9
|
+
this.pipeline = new MarkdownPipeline(options?.md2html);
|
|
10
|
+
}
|
|
11
|
+
getKey(path) {
|
|
12
|
+
return this.prefix ? `${this.prefix}${path}` : path;
|
|
13
|
+
}
|
|
14
|
+
async get(path) {
|
|
15
|
+
const cacheKey = this.getKey(path);
|
|
16
|
+
const cached = this.cache.get(cacheKey);
|
|
17
|
+
if (cached !== null) {
|
|
18
|
+
return cached;
|
|
19
|
+
}
|
|
20
|
+
const obj = await this.bucket.get(cacheKey);
|
|
21
|
+
if (!obj) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
const content = await obj.text();
|
|
25
|
+
this.cache.set(cacheKey, content);
|
|
26
|
+
return content;
|
|
27
|
+
}
|
|
28
|
+
async getObject(path) {
|
|
29
|
+
const cacheKey = `obj:${this.getKey(path)}`;
|
|
30
|
+
const cached = this.cache.get(cacheKey);
|
|
31
|
+
if (cached !== null) {
|
|
32
|
+
return cached;
|
|
33
|
+
}
|
|
34
|
+
const obj = await this.bucket.get(this.getKey(path));
|
|
35
|
+
if (obj) {
|
|
36
|
+
this.cache.set(cacheKey, obj);
|
|
37
|
+
}
|
|
38
|
+
return obj;
|
|
39
|
+
}
|
|
40
|
+
async getWithMetadata(path) {
|
|
41
|
+
const cacheKey = `meta:${this.getKey(path)}`;
|
|
42
|
+
const cached = this.cache.get(cacheKey);
|
|
43
|
+
if (cached !== null) {
|
|
44
|
+
return cached;
|
|
45
|
+
}
|
|
46
|
+
const content = await this.get(path);
|
|
47
|
+
if (!content) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
const { metadata, content: body } = parseFrontmatter(content);
|
|
51
|
+
const result = { metadata, content: body };
|
|
52
|
+
this.cache.set(cacheKey, result);
|
|
53
|
+
return result;
|
|
54
|
+
}
|
|
55
|
+
async getWithAST(path) {
|
|
56
|
+
const cacheKey = `ast:${this.getKey(path)}`;
|
|
57
|
+
const cached = this.cache.get(cacheKey);
|
|
58
|
+
if (cached !== null) {
|
|
59
|
+
return cached;
|
|
60
|
+
}
|
|
61
|
+
const withMetadata = await this.getWithMetadata(path);
|
|
62
|
+
if (!withMetadata) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
const contentNodes = this.pipeline.parse(withMetadata.content);
|
|
66
|
+
const result = {
|
|
67
|
+
metadata: withMetadata.metadata,
|
|
68
|
+
contentNodes
|
|
69
|
+
};
|
|
70
|
+
this.cache.set(cacheKey, result);
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
async getRendered(path) {
|
|
74
|
+
const cacheKey = `rendered:${this.getKey(path)}`;
|
|
75
|
+
const cached = this.cache.get(cacheKey);
|
|
76
|
+
if (cached !== null) {
|
|
77
|
+
return cached;
|
|
78
|
+
}
|
|
79
|
+
const withMetadata = await this.getWithMetadata(path);
|
|
80
|
+
if (!withMetadata) {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
const html = this.pipeline.renderMarkdown(withMetadata.content);
|
|
84
|
+
const result = {
|
|
85
|
+
metadata: withMetadata.metadata,
|
|
86
|
+
content: html
|
|
87
|
+
};
|
|
88
|
+
this.cache.set(cacheKey, result);
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
async list(prefix = '') {
|
|
92
|
+
const fullPrefix = this.prefix ? `${this.prefix}${prefix}` : prefix;
|
|
93
|
+
return this.bucket.list({ prefix: fullPrefix });
|
|
94
|
+
}
|
|
95
|
+
async exists(path) {
|
|
96
|
+
const obj = await this.getObject(path);
|
|
97
|
+
return obj !== null;
|
|
98
|
+
}
|
|
99
|
+
invalidate(path) {
|
|
100
|
+
const key = this.getKey(path);
|
|
101
|
+
this.cache.delete(key);
|
|
102
|
+
this.cache.delete(`obj:${key}`);
|
|
103
|
+
this.cache.delete(`meta:${key}`);
|
|
104
|
+
this.cache.delete(`ast:${key}`);
|
|
105
|
+
this.cache.delete(`rendered:${key}`);
|
|
106
|
+
}
|
|
107
|
+
invalidatePrefix(prefix) {
|
|
108
|
+
const fullPrefix = this.getKey(prefix);
|
|
109
|
+
this.cache.clearPrefix(fullPrefix);
|
|
110
|
+
}
|
|
111
|
+
clearCache() {
|
|
112
|
+
this.cache.clear();
|
|
113
|
+
}
|
|
114
|
+
setCacheTTL(ttl) {
|
|
115
|
+
this.cache.setTTL(ttl);
|
|
116
|
+
}
|
|
117
|
+
disableCache() {
|
|
118
|
+
this.cache.setEnabled(false);
|
|
119
|
+
}
|
|
120
|
+
enableCache() {
|
|
121
|
+
this.cache.setEnabled(true);
|
|
122
|
+
}
|
|
123
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export interface R2Bucket {
|
|
2
|
+
get(key: string): Promise<R2Object | null>;
|
|
3
|
+
put(key: string, value: string | ReadableStream<Uint8Array>): Promise<void>;
|
|
4
|
+
delete(key: string): Promise<void>;
|
|
5
|
+
list(options?: {
|
|
6
|
+
prefix?: string;
|
|
7
|
+
delimiter?: string;
|
|
8
|
+
limit?: number;
|
|
9
|
+
cursor?: string;
|
|
10
|
+
}): Promise<R2ListResult>;
|
|
11
|
+
}
|
|
12
|
+
export interface R2Object {
|
|
13
|
+
key: string;
|
|
14
|
+
version: string;
|
|
15
|
+
size: number;
|
|
16
|
+
httpMetadata?: Record<string, unknown>;
|
|
17
|
+
customMetadata?: Record<string, unknown>;
|
|
18
|
+
writeHttpMetadata(headers: Headers): void;
|
|
19
|
+
body: ReadableStream<Uint8Array> | null;
|
|
20
|
+
text(): Promise<string>;
|
|
21
|
+
json<T>(): Promise<T>;
|
|
22
|
+
}
|
|
23
|
+
export interface R2ListResult {
|
|
24
|
+
objects: R2ListObject[];
|
|
25
|
+
truncated: boolean;
|
|
26
|
+
cursor?: string;
|
|
27
|
+
}
|
|
28
|
+
export interface R2ListObject {
|
|
29
|
+
key: string;
|
|
30
|
+
size: number;
|
|
31
|
+
httpMetadata?: Record<string, unknown>;
|
|
32
|
+
customMetadata?: Record<string, unknown>;
|
|
33
|
+
}
|
|
34
|
+
export interface R2LoaderConfig {
|
|
35
|
+
bucket: R2Bucket;
|
|
36
|
+
prefix?: string;
|
|
37
|
+
cacheTTL?: number;
|
|
38
|
+
cacheEnabled?: boolean;
|
|
39
|
+
}
|
|
40
|
+
export interface ContentMetadata {
|
|
41
|
+
[key: string]: string | string[] | undefined;
|
|
42
|
+
title?: string;
|
|
43
|
+
description?: string;
|
|
44
|
+
date?: string;
|
|
45
|
+
tags?: string[];
|
|
46
|
+
author?: string;
|
|
47
|
+
}
|
|
48
|
+
export interface ParsedContent {
|
|
49
|
+
metadata: ContentMetadata;
|
|
50
|
+
content: string;
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,QAAQ;IACvB,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC,CAAC;IAC3C,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,E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c;IAC7B,MAAM,EAAE,QAAQ,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,YAAY,CAAC,EAAE,OAAO,CAAC;CACxB;AAED,MAAM,WAAW,eAAe;IAC9B,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAAC;IAC7C,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,aAAa;IAC5B,QAAQ,EAAE,eAAe,CAAC;IAC1B,OAAO,EAAE,MAAM,CAAC;CACjB"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@leadertechie/r2tohtml",
|
|
3
|
+
"version": "0.1.0-alpha.1",
|
|
4
|
+
"description": "Generic R2 content loader - fetch and cache content from Cloudflare R2",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsc",
|
|
17
|
+
"test": "vitest run"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@leadertechie/md2html": "^0.1.0-alpha.3"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@types/node": "^20.0.0",
|
|
24
|
+
"typescript": "^5.0.0",
|
|
25
|
+
"vitest": "^2.0.0"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"r2",
|
|
29
|
+
"cloudflare",
|
|
30
|
+
"content",
|
|
31
|
+
"loader",
|
|
32
|
+
"cache"
|
|
33
|
+
],
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"files": [
|
|
36
|
+
"dist",
|
|
37
|
+
"README.md",
|
|
38
|
+
"LICENSE"
|
|
39
|
+
],
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "git+https://github.com/leadertechie/r2Tohtml.git"
|
|
43
|
+
}
|
|
44
|
+
}
|