@parrel/ledger 0.0.0-stage → 5.0.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 +793 -2
- package/dist/cli/commands.d.ts +51 -0
- package/dist/cli/commands.d.ts.map +1 -0
- package/dist/cli/commands.js +968 -0
- package/dist/cli/commands.js.map +1 -0
- package/dist/cli/index.d.ts +24 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +35 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/parrel.d.ts +3 -0
- package/dist/cli/parrel.d.ts.map +1 -0
- package/dist/cli/parrel.js +5 -0
- package/dist/cli/parrel.js.map +1 -0
- package/dist/cli/utils/angular-setup.d.ts +14 -0
- package/dist/cli/utils/angular-setup.d.ts.map +1 -0
- package/dist/cli/utils/angular-setup.js +96 -0
- package/dist/cli/utils/angular-setup.js.map +1 -0
- package/dist/cli/utils/api-tree-builder.d.ts +24 -0
- package/dist/cli/utils/api-tree-builder.d.ts.map +1 -0
- package/dist/cli/utils/api-tree-builder.js +146 -0
- package/dist/cli/utils/api-tree-builder.js.map +1 -0
- package/dist/cli/utils/bundle-reader.d.ts +185 -0
- package/dist/cli/utils/bundle-reader.d.ts.map +1 -0
- package/dist/cli/utils/bundle-reader.js +518 -0
- package/dist/cli/utils/bundle-reader.js.map +1 -0
- package/dist/cli/utils/collision-detector.d.ts +21 -0
- package/dist/cli/utils/collision-detector.d.ts.map +1 -0
- package/dist/cli/utils/collision-detector.js +53 -0
- package/dist/cli/utils/collision-detector.js.map +1 -0
- package/dist/cli/utils/map-fields.d.ts +42 -0
- package/dist/cli/utils/map-fields.d.ts.map +1 -0
- package/dist/cli/utils/map-fields.js +90 -0
- package/dist/cli/utils/map-fields.js.map +1 -0
- package/dist/cli/utils/map-validator.d.ts +42 -0
- package/dist/cli/utils/map-validator.d.ts.map +1 -0
- package/dist/cli/utils/map-validator.js +119 -0
- package/dist/cli/utils/map-validator.js.map +1 -0
- package/dist/cli/utils/media-file.d.ts +41 -0
- package/dist/cli/utils/media-file.d.ts.map +1 -0
- package/dist/cli/utils/media-file.js +72 -0
- package/dist/cli/utils/media-file.js.map +1 -0
- package/dist/cli/utils/media-validator.d.ts +10 -0
- package/dist/cli/utils/media-validator.d.ts.map +1 -0
- package/dist/cli/utils/media-validator.js +85 -0
- package/dist/cli/utils/media-validator.js.map +1 -0
- package/dist/cli/utils/mock-augmenter.d.ts +19 -0
- package/dist/cli/utils/mock-augmenter.d.ts.map +1 -0
- package/dist/cli/utils/mock-augmenter.js +102 -0
- package/dist/cli/utils/mock-augmenter.js.map +1 -0
- package/dist/cli/utils/mock-validator.d.ts +27 -0
- package/dist/cli/utils/mock-validator.d.ts.map +1 -0
- package/dist/cli/utils/mock-validator.js +214 -0
- package/dist/cli/utils/mock-validator.js.map +1 -0
- package/dist/cli/utils/project-dir.d.ts +24 -0
- package/dist/cli/utils/project-dir.d.ts.map +1 -0
- package/dist/cli/utils/project-dir.js +32 -0
- package/dist/cli/utils/project-dir.js.map +1 -0
- package/dist/cli/utils/schema-loader.d.ts +19 -0
- package/dist/cli/utils/schema-loader.d.ts.map +1 -0
- package/dist/cli/utils/schema-loader.js +101 -0
- package/dist/cli/utils/schema-loader.js.map +1 -0
- package/dist/cli/utils/server-entry.d.ts +34 -0
- package/dist/cli/utils/server-entry.d.ts.map +1 -0
- package/dist/cli/utils/server-entry.js +111 -0
- package/dist/cli/utils/server-entry.js.map +1 -0
- package/dist/cli/utils/sync.d.ts +116 -0
- package/dist/cli/utils/sync.d.ts.map +1 -0
- package/dist/cli/utils/sync.js +134 -0
- package/dist/cli/utils/sync.js.map +1 -0
- package/dist/client/index.d.ts +39 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +88 -0
- package/dist/client/index.js.map +1 -0
- package/dist/core/browser-session.d.ts +5 -0
- package/dist/core/browser-session.d.ts.map +1 -0
- package/dist/core/browser-session.js +12 -0
- package/dist/core/browser-session.js.map +1 -0
- package/dist/core/cache.d.ts +80 -0
- package/dist/core/cache.d.ts.map +1 -0
- package/dist/core/cache.js +95 -0
- package/dist/core/cache.js.map +1 -0
- package/dist/core/client.d.ts +64 -0
- package/dist/core/client.d.ts.map +1 -0
- package/dist/core/client.js +281 -0
- package/dist/core/client.js.map +1 -0
- package/dist/core/config.d.ts +20 -0
- package/dist/core/config.d.ts.map +1 -0
- package/dist/core/config.js +32 -0
- package/dist/core/config.js.map +1 -0
- package/dist/core/copy-read.d.ts +25 -0
- package/dist/core/copy-read.d.ts.map +1 -0
- package/dist/core/copy-read.js +123 -0
- package/dist/core/copy-read.js.map +1 -0
- package/dist/core/define-ledger.d.ts +38 -0
- package/dist/core/define-ledger.d.ts.map +1 -0
- package/dist/core/define-ledger.js +65 -0
- package/dist/core/define-ledger.js.map +1 -0
- package/dist/core/deprecated.d.ts +16 -0
- package/dist/core/deprecated.d.ts.map +1 -0
- package/dist/core/deprecated.js +14 -0
- package/dist/core/deprecated.js.map +1 -0
- package/dist/core/env.d.ts +103 -0
- package/dist/core/env.d.ts.map +1 -0
- package/dist/core/env.js +208 -0
- package/dist/core/env.js.map +1 -0
- package/dist/core/error-codes.generated.d.ts +12 -0
- package/dist/core/error-codes.generated.d.ts.map +1 -0
- package/dist/core/error-codes.generated.js +3 -0
- package/dist/core/error-codes.generated.js.map +1 -0
- package/dist/core/get-content.d.ts +62 -0
- package/dist/core/get-content.d.ts.map +1 -0
- package/dist/core/get-content.js +86 -0
- package/dist/core/get-content.js.map +1 -0
- package/dist/core/get-media.d.ts +58 -0
- package/dist/core/get-media.d.ts.map +1 -0
- package/dist/core/get-media.js +89 -0
- package/dist/core/get-media.js.map +1 -0
- package/dist/core/manifest.d.ts +38 -0
- package/dist/core/manifest.d.ts.map +1 -0
- package/dist/core/manifest.js +93 -0
- package/dist/core/manifest.js.map +1 -0
- package/dist/core/media-proxy.d.ts +41 -0
- package/dist/core/media-proxy.d.ts.map +1 -0
- package/dist/core/media-proxy.js +219 -0
- package/dist/core/media-proxy.js.map +1 -0
- package/dist/core/package-info.d.ts +34 -0
- package/dist/core/package-info.d.ts.map +1 -0
- package/dist/core/package-info.js +39 -0
- package/dist/core/package-info.js.map +1 -0
- package/dist/core/parrel-error.d.ts +160 -0
- package/dist/core/parrel-error.d.ts.map +1 -0
- package/dist/core/parrel-error.js +198 -0
- package/dist/core/parrel-error.js.map +1 -0
- package/dist/core/transform-content.d.ts +42 -0
- package/dist/core/transform-content.d.ts.map +1 -0
- package/dist/core/transform-content.js +185 -0
- package/dist/core/transform-content.js.map +1 -0
- package/dist/core/types.d.ts +406 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +11 -0
- package/dist/core/types.js.map +1 -0
- package/dist/errors/error-messages.generated.d.ts +5 -0
- package/dist/errors/error-messages.generated.d.ts.map +1 -0
- package/dist/errors/error-messages.generated.js +971 -0
- package/dist/errors/error-messages.generated.js.map +1 -0
- package/dist/errors/get-error-info.d.ts +29 -0
- package/dist/errors/get-error-info.d.ts.map +1 -0
- package/dist/errors/get-error-info.js +43 -0
- package/dist/errors/get-error-info.js.map +1 -0
- package/dist/errors/index.d.ts +4 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +4 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/errors/types.d.ts +19 -0
- package/dist/errors/types.d.ts.map +1 -0
- package/dist/errors/types.js +2 -0
- package/dist/errors/types.js.map +1 -0
- package/dist/generated.d.ts +29 -0
- package/dist/generated.d.ts.map +1 -0
- package/dist/generated.js +29 -0
- package/dist/generated.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/mock/dev-media.d.ts +15 -0
- package/dist/mock/dev-media.d.ts.map +1 -0
- package/dist/mock/dev-media.js +52 -0
- package/dist/mock/dev-media.js.map +1 -0
- package/dist/mock/file-store.d.ts +44 -0
- package/dist/mock/file-store.d.ts.map +1 -0
- package/dist/mock/file-store.js +170 -0
- package/dist/mock/file-store.js.map +1 -0
- package/dist/mock/index.d.ts +4 -0
- package/dist/mock/index.d.ts.map +1 -0
- package/dist/mock/index.js +3 -0
- package/dist/mock/index.js.map +1 -0
- package/dist/mock/media-fields.d.ts +25 -0
- package/dist/mock/media-fields.d.ts.map +1 -0
- package/dist/mock/media-fields.js +54 -0
- package/dist/mock/media-fields.js.map +1 -0
- package/dist/mock/mock-adapter.d.ts +65 -0
- package/dist/mock/mock-adapter.d.ts.map +1 -0
- package/dist/mock/mock-adapter.js +431 -0
- package/dist/mock/mock-adapter.js.map +1 -0
- package/dist/mock/mock-errors.d.ts +18 -0
- package/dist/mock/mock-errors.d.ts.map +1 -0
- package/dist/mock/mock-errors.js +56 -0
- package/dist/mock/mock-errors.js.map +1 -0
- package/dist/mock/node-import.d.ts +13 -0
- package/dist/mock/node-import.d.ts.map +1 -0
- package/dist/mock/node-import.js +16 -0
- package/dist/mock/node-import.js.map +1 -0
- package/dist/mock/seed-mock-store.d.ts +14 -0
- package/dist/mock/seed-mock-store.d.ts.map +1 -0
- package/dist/mock/seed-mock-store.js +106 -0
- package/dist/mock/seed-mock-store.js.map +1 -0
- package/dist/mock/shared-memory-store.d.ts +35 -0
- package/dist/mock/shared-memory-store.d.ts.map +1 -0
- package/dist/mock/shared-memory-store.js +46 -0
- package/dist/mock/shared-memory-store.js.map +1 -0
- package/dist/next/dev-rendering-helpers.d.ts +78 -0
- package/dist/next/dev-rendering-helpers.d.ts.map +1 -0
- package/dist/next/dev-rendering-helpers.js +103 -0
- package/dist/next/dev-rendering-helpers.js.map +1 -0
- package/dist/next/index.d.ts +35 -0
- package/dist/next/index.d.ts.map +1 -0
- package/dist/next/index.js +33 -0
- package/dist/next/index.js.map +1 -0
- package/dist/next/next-cache-adapter.d.ts +11 -0
- package/dist/next/next-cache-adapter.d.ts.map +1 -0
- package/dist/next/next-cache-adapter.js +12 -0
- package/dist/next/next-cache-adapter.js.map +1 -0
- package/dist/nuxt/index.d.ts +22 -0
- package/dist/nuxt/index.d.ts.map +1 -0
- package/dist/nuxt/index.js +57 -0
- package/dist/nuxt/index.js.map +1 -0
- package/dist/sveltekit/index.d.ts +35 -0
- package/dist/sveltekit/index.d.ts.map +1 -0
- package/dist/sveltekit/index.js +56 -0
- package/dist/sveltekit/index.js.map +1 -0
- package/dist/ui/content-editor.d.ts +9 -0
- package/dist/ui/content-editor.d.ts.map +1 -0
- package/dist/ui/content-editor.js +27 -0
- package/dist/ui/content-editor.js.map +1 -0
- package/dist/ui/index.d.ts +8 -0
- package/dist/ui/index.d.ts.map +1 -0
- package/dist/ui/index.js +8 -0
- package/dist/ui/index.js.map +1 -0
- package/dist/ui/ledger-overlay.d.ts +10 -0
- package/dist/ui/ledger-overlay.d.ts.map +1 -0
- package/dist/ui/ledger-overlay.js +27 -0
- package/dist/ui/ledger-overlay.js.map +1 -0
- package/dist/ui/media-uploader.d.ts +9 -0
- package/dist/ui/media-uploader.d.ts.map +1 -0
- package/dist/ui/media-uploader.js +27 -0
- package/dist/ui/media-uploader.js.map +1 -0
- package/package.json +134 -6
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-codes.generated.js","sourceRoot":"","sources":["../../src/core/error-codes.generated.ts"],"names":[],"mappings":"AAAA,qHAAqH"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { LedgerContentPath, ContentAccessOptions } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Retrieves content by path with caching and change detection.
|
|
4
|
+
*
|
|
5
|
+
* Supports:
|
|
6
|
+
* - Simple paths: "home.header"
|
|
7
|
+
* - Array access: "home.items[0]", "home.items[last]", "home.items[0..2]"
|
|
8
|
+
*
|
|
9
|
+
* Cache modes:
|
|
10
|
+
* - "default": the package keeps a copy of what it reads (rulebook LIM-13). A copy is fresh for `cache.ttlSeconds`
|
|
11
|
+
* (default 300) and is then returned without any request. After that the manifest of the service is read (one read
|
|
12
|
+
* serves every copy for `cache.manifestTtlSeconds`, default 300) and the hash of the resource is compared: unchanged,
|
|
13
|
+
* the copy is fresh for another `ttlSeconds`; changed, the resource is downloaded again. A copy is downloaded again at
|
|
14
|
+
* the latest `cache.maxAgeSeconds` (default 86400, 24 hours) after its download, also when the hash did not change.
|
|
15
|
+
* Without a usable manifest a copy is downloaded again when it is no longer fresh. Reads of one path at the same
|
|
16
|
+
* moment share one download.
|
|
17
|
+
* - "force": `force` asks the service on every call. The copy is updated - best for admin/preview
|
|
18
|
+
* - "no-store": `no-store` skips the copy for this call: it asks the service and keeps nothing - best for a read after a write
|
|
19
|
+
*
|
|
20
|
+
* `cache.enabled: false` in `defineParrelLedger` makes every call behave like "no-store".
|
|
21
|
+
*
|
|
22
|
+
* @param path The content path to retrieve
|
|
23
|
+
* @param options Cache behavior options (default: { cache: "default" })
|
|
24
|
+
* @returns The content value (type depends on what was stored)
|
|
25
|
+
* @throws {ParrelError} `SD00100001` if Ledger is not configured
|
|
26
|
+
* @throws {ParrelError} `SD02000001` if a custom adapter cannot read content
|
|
27
|
+
* @throws {ParrelError} the code and status that Ledger returned, for example `LE02000003` (404) when the resource
|
|
28
|
+
* does not exist, or `LE02000018` (404) when the path is not in the resource
|
|
29
|
+
* @throws {ParrelError} `SD01000001` if Ledger cannot be reached, with the original error as `cause`
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* const title = await getContent("homepage.title");
|
|
34
|
+
* const fresh = await getContent("homepage", { cache: "force" });
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare function getContent(path: LedgerContentPath, options?: ContentAccessOptions): Promise<unknown>;
|
|
38
|
+
/**
|
|
39
|
+
* Uploads or updates content at the given path.
|
|
40
|
+
*
|
|
41
|
+
* Returns void on success.
|
|
42
|
+
* Throws a {@link ParrelError} on failure.
|
|
43
|
+
*
|
|
44
|
+
* After a successful write, the package drops its copies of the content and the media URLs of the same resource, so the
|
|
45
|
+
* next `getContent` downloads them again. The manifest is not read again for it.
|
|
46
|
+
*
|
|
47
|
+
* @param path The content path
|
|
48
|
+
* @param value The content value to upload
|
|
49
|
+
* @throws {ParrelError} `SD00100001` if Ledger is not configured
|
|
50
|
+
* @throws {ParrelError} `SD02000002` if a custom adapter cannot save content
|
|
51
|
+
* @throws {ParrelError} the code and status that Ledger returned, for example `LE04000018` (401) when the access
|
|
52
|
+
* token is missing, or `LE02000012` (400) when a required field is missing
|
|
53
|
+
* @throws {ParrelError} `SD01000002` if Ledger cannot be reached, with the original error as `cause`
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* await uploadContent("homepage.title", "Welcome back");
|
|
58
|
+
* const title = await getContent("homepage.title", { cache: "force" });
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export declare function uploadContent(path: LedgerContentPath, value: unknown): Promise<void>;
|
|
62
|
+
//# sourceMappingURL=get-content.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-content.d.ts","sourceRoot":"","sources":["../../src/core/get-content.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAE1E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,wBAAsB,UAAU,CAAC,IAAI,EAAE,iBAAiB,EAAE,OAAO,CAAC,EAAE,oBAAoB,GAAG,OAAO,CAAC,OAAO,CAAC,CAiB1G;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAsB,aAAa,CAAC,IAAI,EAAE,iBAAiB,EAAE,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAe1F"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { requireParrelLedgerConfig } from "./config.js";
|
|
2
|
+
import { dropCopiesOfResource, readWithCopy, resourceOf } from "./copy-read.js";
|
|
3
|
+
import { ParrelError } from "./parrel-error.js";
|
|
4
|
+
/**
|
|
5
|
+
* Retrieves content by path with caching and change detection.
|
|
6
|
+
*
|
|
7
|
+
* Supports:
|
|
8
|
+
* - Simple paths: "home.header"
|
|
9
|
+
* - Array access: "home.items[0]", "home.items[last]", "home.items[0..2]"
|
|
10
|
+
*
|
|
11
|
+
* Cache modes:
|
|
12
|
+
* - "default": the package keeps a copy of what it reads (rulebook LIM-13). A copy is fresh for `cache.ttlSeconds`
|
|
13
|
+
* (default 300) and is then returned without any request. After that the manifest of the service is read (one read
|
|
14
|
+
* serves every copy for `cache.manifestTtlSeconds`, default 300) and the hash of the resource is compared: unchanged,
|
|
15
|
+
* the copy is fresh for another `ttlSeconds`; changed, the resource is downloaded again. A copy is downloaded again at
|
|
16
|
+
* the latest `cache.maxAgeSeconds` (default 86400, 24 hours) after its download, also when the hash did not change.
|
|
17
|
+
* Without a usable manifest a copy is downloaded again when it is no longer fresh. Reads of one path at the same
|
|
18
|
+
* moment share one download.
|
|
19
|
+
* - "force": `force` asks the service on every call. The copy is updated - best for admin/preview
|
|
20
|
+
* - "no-store": `no-store` skips the copy for this call: it asks the service and keeps nothing - best for a read after a write
|
|
21
|
+
*
|
|
22
|
+
* `cache.enabled: false` in `defineParrelLedger` makes every call behave like "no-store".
|
|
23
|
+
*
|
|
24
|
+
* @param path The content path to retrieve
|
|
25
|
+
* @param options Cache behavior options (default: { cache: "default" })
|
|
26
|
+
* @returns The content value (type depends on what was stored)
|
|
27
|
+
* @throws {ParrelError} `SD00100001` if Ledger is not configured
|
|
28
|
+
* @throws {ParrelError} `SD02000001` if a custom adapter cannot read content
|
|
29
|
+
* @throws {ParrelError} the code and status that Ledger returned, for example `LE02000003` (404) when the resource
|
|
30
|
+
* does not exist, or `LE02000018` (404) when the path is not in the resource
|
|
31
|
+
* @throws {ParrelError} `SD01000001` if Ledger cannot be reached, with the original error as `cause`
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* const title = await getContent("homepage.title");
|
|
36
|
+
* const fresh = await getContent("homepage", { cache: "force" });
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export async function getContent(path, options) {
|
|
40
|
+
const config = requireParrelLedgerConfig();
|
|
41
|
+
if (!config.adapter.getContent) {
|
|
42
|
+
throw new ParrelError("SD02000001", "The Ledger adapter you configured cannot read content. Add getContent to your adapter, or remove the custom adapter from defineParrelLedger.");
|
|
43
|
+
}
|
|
44
|
+
const getAdapterContent = config.adapter.getContent;
|
|
45
|
+
return readWithCopy({
|
|
46
|
+
config,
|
|
47
|
+
key: `cms-content:${config.slug}:${path}`,
|
|
48
|
+
resource: resourceOf(path),
|
|
49
|
+
options,
|
|
50
|
+
download: (fetchOptions) => getAdapterContent.call(config.adapter, path, fetchOptions),
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Uploads or updates content at the given path.
|
|
55
|
+
*
|
|
56
|
+
* Returns void on success.
|
|
57
|
+
* Throws a {@link ParrelError} on failure.
|
|
58
|
+
*
|
|
59
|
+
* After a successful write, the package drops its copies of the content and the media URLs of the same resource, so the
|
|
60
|
+
* next `getContent` downloads them again. The manifest is not read again for it.
|
|
61
|
+
*
|
|
62
|
+
* @param path The content path
|
|
63
|
+
* @param value The content value to upload
|
|
64
|
+
* @throws {ParrelError} `SD00100001` if Ledger is not configured
|
|
65
|
+
* @throws {ParrelError} `SD02000002` if a custom adapter cannot save content
|
|
66
|
+
* @throws {ParrelError} the code and status that Ledger returned, for example `LE04000018` (401) when the access
|
|
67
|
+
* token is missing, or `LE02000012` (400) when a required field is missing
|
|
68
|
+
* @throws {ParrelError} `SD01000002` if Ledger cannot be reached, with the original error as `cause`
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* await uploadContent("homepage.title", "Welcome back");
|
|
73
|
+
* const title = await getContent("homepage.title", { cache: "force" });
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export async function uploadContent(path, value) {
|
|
77
|
+
const config = requireParrelLedgerConfig();
|
|
78
|
+
// Upload via adapter
|
|
79
|
+
if (!config.adapter.uploadContent) {
|
|
80
|
+
throw new ParrelError("SD02000002", "The Ledger adapter you configured cannot save content. Add uploadContent to your adapter, or remove the custom adapter from defineParrelLedger.");
|
|
81
|
+
}
|
|
82
|
+
await config.adapter.uploadContent(path, value);
|
|
83
|
+
// The write made the copies of this resource old (rulebook LIM-13). The manifest stays.
|
|
84
|
+
dropCopiesOfResource(config.slug, resourceOf(path));
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=get-content.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-content.js","sourceRoot":"","sources":["../../src/core/get-content.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,EAAE,oBAAoB,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAChF,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,IAAuB,EAAE,OAA8B;IACvF,MAAM,MAAM,GAAG,yBAAyB,EAAE,CAAC;IAC3C,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,UAAU,EAAE,CAAC;QAChC,MAAM,IAAI,WAAW,CACpB,YAAY,EACZ,8IAA8I,CAC9I,CAAC;IACH,CAAC;IACD,MAAM,iBAAiB,GAAG,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC;IAEpD,OAAO,YAAY,CAAC;QACnB,MAAM;QACN,GAAG,EAAE,eAAe,MAAM,CAAC,IAAI,IAAI,IAAI,EAAE;QACzC,QAAQ,EAAE,UAAU,CAAC,IAAI,CAAC;QAC1B,OAAO;QACP,QAAQ,EAAE,CAAC,YAAY,EAAE,EAAE,CAAC,iBAAiB,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,IAAI,EAAE,YAAY,CAAC;KACtF,CAAC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,IAAuB,EAAE,KAAc;IAC1E,MAAM,MAAM,GAAG,yBAAyB,EAAE,CAAC;IAE3C,qBAAqB;IACrB,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,aAAa,EAAE,CAAC;QACnC,MAAM,IAAI,WAAW,CACpB,YAAY,EACZ,iJAAiJ,CACjJ,CAAC;IACH,CAAC;IAED,MAAM,MAAM,CAAC,OAAO,CAAC,aAAa,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IAEhD,wFAAwF;IACxF,oBAAoB,CAAC,MAAM,CAAC,IAAI,EAAE,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC;AACrD,CAAC"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { LedgerMediaPath, ContentAccessOptions } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Retrieves media URL by path with caching.
|
|
4
|
+
* Always returns a URL string (e.g., "/mockMedia/hero.png" or "https://cdn.../hero.png")
|
|
5
|
+
*
|
|
6
|
+
* Cache modes:
|
|
7
|
+
* - "default": the same rule as `getContent` (rulebook LIM-13): the copy of the URL is fresh for `cache.ttlSeconds`
|
|
8
|
+
* (default 300), is then checked against the manifest hash of the resource that holds the field (the path
|
|
9
|
+
* `homepage-heroImage` belongs to the resource `homepage`), and is downloaded again when that hash changed or at the
|
|
10
|
+
* latest after `cache.maxAgeSeconds` (default 24 hours). The manifest lists media by file name, while a media URL is
|
|
11
|
+
* asked by field path, so the URL follows its resource.
|
|
12
|
+
* - "force": Always fetch fresh from adapter, update the copy - best for admin/preview
|
|
13
|
+
* - "no-store": Always fetch fresh, keep nothing - best for a read after a write
|
|
14
|
+
*
|
|
15
|
+
* `cache.enabled: false` in `defineParrelLedger` makes every call behave like "no-store".
|
|
16
|
+
*
|
|
17
|
+
* @param path The media path to retrieve
|
|
18
|
+
* @param options Cache behavior options (default: { cache: "default" })
|
|
19
|
+
* @returns The media URL as a string
|
|
20
|
+
* @throws {ParrelError} `SD00100001` if Ledger is not configured
|
|
21
|
+
* @throws {ParrelError} `SD02000003` if a custom adapter cannot read media
|
|
22
|
+
* @throws {ParrelError} `SD02000005` if a custom adapter returns a media URL that is not a text
|
|
23
|
+
* @throws {ParrelError} `SD02000006` if Ledger answers without a usable URL
|
|
24
|
+
* @throws {ParrelError} the code and status that Ledger returned, for example `LE02000003` (404) for a resource
|
|
25
|
+
* that does not exist
|
|
26
|
+
* @throws {ParrelError} `SD01000003` if Ledger cannot be reached, with the original error as `cause`
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* const heroUrl = await getMedia("homepage-heroImage");
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export declare function getMedia(path: LedgerMediaPath, options?: ContentAccessOptions): Promise<string>;
|
|
34
|
+
/**
|
|
35
|
+
* Uploads or updates media at the given path.
|
|
36
|
+
*
|
|
37
|
+
* Returns void on success.
|
|
38
|
+
* Throws a {@link ParrelError} on failure.
|
|
39
|
+
*
|
|
40
|
+
* After a successful upload, the package drops its copies of the media URLs and the content of the same resource, so
|
|
41
|
+
* the next `getMedia` downloads the URL again.
|
|
42
|
+
*
|
|
43
|
+
* @param path The media path
|
|
44
|
+
* @param file The file to upload
|
|
45
|
+
* @throws {ParrelError} `SD00100001` if Ledger is not configured
|
|
46
|
+
* @throws {ParrelError} `SD02000004` if a custom adapter cannot upload media
|
|
47
|
+
* @throws {ParrelError} the code and status that Ledger returned, for example `LE02500009` (400) for an empty file
|
|
48
|
+
* or `LE02500007` (400) for an extension that is not allowed
|
|
49
|
+
* @throws {ParrelError} `SD01000004` if Ledger cannot be reached, with the original error as `cause`
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* await uploadMedia("homepage-heroImage", file);
|
|
54
|
+
* const heroUrl = await getMedia("homepage-heroImage", { cache: "force" });
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export declare function uploadMedia(path: LedgerMediaPath, file: File): Promise<void>;
|
|
58
|
+
//# sourceMappingURL=get-media.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-media.d.ts","sourceRoot":"","sources":["../../src/core/get-media.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,eAAe,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAExE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAsB,QAAQ,CAAC,IAAI,EAAE,eAAe,EAAE,OAAO,CAAC,EAAE,oBAAoB,GAAG,OAAO,CAAC,MAAM,CAAC,CA2BrG;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAsB,WAAW,CAAC,IAAI,EAAE,eAAe,EAAE,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAelF"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { requireParrelLedgerConfig } from "./config.js";
|
|
2
|
+
import { dropCopiesOfResource, mediaResourceOf, readWithCopy } from "./copy-read.js";
|
|
3
|
+
import { ParrelError } from "./parrel-error.js";
|
|
4
|
+
/**
|
|
5
|
+
* Retrieves media URL by path with caching.
|
|
6
|
+
* Always returns a URL string (e.g., "/mockMedia/hero.png" or "https://cdn.../hero.png")
|
|
7
|
+
*
|
|
8
|
+
* Cache modes:
|
|
9
|
+
* - "default": the same rule as `getContent` (rulebook LIM-13): the copy of the URL is fresh for `cache.ttlSeconds`
|
|
10
|
+
* (default 300), is then checked against the manifest hash of the resource that holds the field (the path
|
|
11
|
+
* `homepage-heroImage` belongs to the resource `homepage`), and is downloaded again when that hash changed or at the
|
|
12
|
+
* latest after `cache.maxAgeSeconds` (default 24 hours). The manifest lists media by file name, while a media URL is
|
|
13
|
+
* asked by field path, so the URL follows its resource.
|
|
14
|
+
* - "force": Always fetch fresh from adapter, update the copy - best for admin/preview
|
|
15
|
+
* - "no-store": Always fetch fresh, keep nothing - best for a read after a write
|
|
16
|
+
*
|
|
17
|
+
* `cache.enabled: false` in `defineParrelLedger` makes every call behave like "no-store".
|
|
18
|
+
*
|
|
19
|
+
* @param path The media path to retrieve
|
|
20
|
+
* @param options Cache behavior options (default: { cache: "default" })
|
|
21
|
+
* @returns The media URL as a string
|
|
22
|
+
* @throws {ParrelError} `SD00100001` if Ledger is not configured
|
|
23
|
+
* @throws {ParrelError} `SD02000003` if a custom adapter cannot read media
|
|
24
|
+
* @throws {ParrelError} `SD02000005` if a custom adapter returns a media URL that is not a text
|
|
25
|
+
* @throws {ParrelError} `SD02000006` if Ledger answers without a usable URL
|
|
26
|
+
* @throws {ParrelError} the code and status that Ledger returned, for example `LE02000003` (404) for a resource
|
|
27
|
+
* that does not exist
|
|
28
|
+
* @throws {ParrelError} `SD01000003` if Ledger cannot be reached, with the original error as `cause`
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* const heroUrl = await getMedia("homepage-heroImage");
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export async function getMedia(path, options) {
|
|
36
|
+
const config = requireParrelLedgerConfig();
|
|
37
|
+
if (!config.adapter.getMedia) {
|
|
38
|
+
throw new ParrelError("SD02000003", "The Ledger adapter you configured cannot read media. Add getMedia to your adapter, or remove the custom adapter from defineParrelLedger.");
|
|
39
|
+
}
|
|
40
|
+
const getAdapterMedia = config.adapter.getMedia;
|
|
41
|
+
return readWithCopy({
|
|
42
|
+
config,
|
|
43
|
+
key: `cms-media:${config.slug}:${path}`,
|
|
44
|
+
resource: mediaResourceOf(path),
|
|
45
|
+
options,
|
|
46
|
+
download: async (fetchOptions) => {
|
|
47
|
+
const media = await getAdapterMedia.call(config.adapter, path, fetchOptions);
|
|
48
|
+
// Ensure we return a string URL
|
|
49
|
+
if (typeof media !== "string" || media.trim() === "") {
|
|
50
|
+
throw new ParrelError("SD02000005", "The Ledger adapter returned a media URL that is not valid. Make getMedia of your adapter return a full URL as text.");
|
|
51
|
+
}
|
|
52
|
+
return media;
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Uploads or updates media at the given path.
|
|
58
|
+
*
|
|
59
|
+
* Returns void on success.
|
|
60
|
+
* Throws a {@link ParrelError} on failure.
|
|
61
|
+
*
|
|
62
|
+
* After a successful upload, the package drops its copies of the media URLs and the content of the same resource, so
|
|
63
|
+
* the next `getMedia` downloads the URL again.
|
|
64
|
+
*
|
|
65
|
+
* @param path The media path
|
|
66
|
+
* @param file The file to upload
|
|
67
|
+
* @throws {ParrelError} `SD00100001` if Ledger is not configured
|
|
68
|
+
* @throws {ParrelError} `SD02000004` if a custom adapter cannot upload media
|
|
69
|
+
* @throws {ParrelError} the code and status that Ledger returned, for example `LE02500009` (400) for an empty file
|
|
70
|
+
* or `LE02500007` (400) for an extension that is not allowed
|
|
71
|
+
* @throws {ParrelError} `SD01000004` if Ledger cannot be reached, with the original error as `cause`
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* await uploadMedia("homepage-heroImage", file);
|
|
76
|
+
* const heroUrl = await getMedia("homepage-heroImage", { cache: "force" });
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export async function uploadMedia(path, file) {
|
|
80
|
+
const config = requireParrelLedgerConfig();
|
|
81
|
+
// Upload via adapter
|
|
82
|
+
if (!config.adapter.uploadMedia) {
|
|
83
|
+
throw new ParrelError("SD02000004", "The Ledger adapter you configured cannot upload media. Add uploadMedia to your adapter, or remove the custom adapter from defineParrelLedger.");
|
|
84
|
+
}
|
|
85
|
+
await config.adapter.uploadMedia(path, file);
|
|
86
|
+
// The upload made the copies of this resource old (rulebook LIM-13). The manifest stays.
|
|
87
|
+
dropCopiesOfResource(config.slug, mediaResourceOf(path));
|
|
88
|
+
}
|
|
89
|
+
//# sourceMappingURL=get-media.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-media.js","sourceRoot":"","sources":["../../src/core/get-media.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,EAAE,oBAAoB,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AACrF,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,IAAqB,EAAE,OAA8B;IACnF,MAAM,MAAM,GAAG,yBAAyB,EAAE,CAAC;IAC3C,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;QAC9B,MAAM,IAAI,WAAW,CACpB,YAAY,EACZ,0IAA0I,CAC1I,CAAC;IACH,CAAC;IACD,MAAM,eAAe,GAAG,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC;IAEhD,OAAO,YAAY,CAAC;QACnB,MAAM;QACN,GAAG,EAAE,aAAa,MAAM,CAAC,IAAI,IAAI,IAAI,EAAE;QACvC,QAAQ,EAAE,eAAe,CAAC,IAAI,CAAC;QAC/B,OAAO;QACP,QAAQ,EAAE,KAAK,EAAE,YAAY,EAAE,EAAE;YAChC,MAAM,KAAK,GAAG,MAAM,eAAe,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,IAAI,EAAE,YAAY,CAAC,CAAC;YAC7E,gCAAgC;YAChC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC;gBACtD,MAAM,IAAI,WAAW,CACpB,YAAY,EACZ,qHAAqH,CACrH,CAAC;YACH,CAAC;YACD,OAAO,KAAK,CAAC;QACd,CAAC;KACD,CAAC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,IAAqB,EAAE,IAAU;IAClE,MAAM,MAAM,GAAG,yBAAyB,EAAE,CAAC;IAE3C,qBAAqB;IACrB,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC;QACjC,MAAM,IAAI,WAAW,CACpB,YAAY,EACZ,+IAA+I,CAC/I,CAAC;IACH,CAAC;IAED,MAAM,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IAE7C,yFAAyF;IACzF,oBAAoB,CAAC,MAAM,CAAC,IAAI,EAAE,eAAe,CAAC,IAAI,CAAC,CAAC,CAAC;AAC1D,CAAC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { ManifestDto } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* How long a manifest is kept before it is read again, in seconds: 300, the fresh time of a copy, so one read of the
|
|
4
|
+
* manifest serves every copy that is checked within those 300 seconds (rulebook LIM-13).
|
|
5
|
+
*/
|
|
6
|
+
export declare const DEFAULT_MANIFEST_TTL_SECONDS = 300;
|
|
7
|
+
/**
|
|
8
|
+
* In-memory cache for the manifest of a service. The manifest lists the hash of every resource and media file, so
|
|
9
|
+
* a reader that holds a recent manifest knows which content changed without asking for the content itself.
|
|
10
|
+
*
|
|
11
|
+
* The manifest is read at most once per `ttlSeconds` (default 300) for each `key` (the slug of the service), and
|
|
12
|
+
* reads that happen at the same time share one request. `getContent`, `getMedia` and the media proxy read it only for
|
|
13
|
+
* a copy that is no longer fresh, so a site reads it at most once per 300 seconds for each service.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const manifest = await ManifestCache.getManifest(() => adapter.getManifest(), { key: "my-site", ttlSeconds: 300 });
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare class ManifestCache {
|
|
21
|
+
static getManifest(fetcher: () => Promise<ManifestDto>, options?: {
|
|
22
|
+
key?: string;
|
|
23
|
+
ttlSeconds?: number;
|
|
24
|
+
}): Promise<ManifestDto>;
|
|
25
|
+
/** Forgets the manifest of `key`, or of every service when no key is given. The next read asks the service. */
|
|
26
|
+
static invalidate(key?: string): void;
|
|
27
|
+
/** The manifest that is in memory, without asking the service and whatever its age. */
|
|
28
|
+
static get(key?: string): ManifestDto | null;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Detect content changes by comparing manifest hashes
|
|
32
|
+
*/
|
|
33
|
+
export declare function hasContentChanged(path: string, oldManifest: ManifestDto | null, newManifest: ManifestDto): boolean;
|
|
34
|
+
/** The hash of a resource in the manifest, or `undefined` when the manifest does not list it. */
|
|
35
|
+
export declare function resourceHash(manifest: ManifestDto, resource: string): string | undefined;
|
|
36
|
+
/** The hash of a media file in the manifest, or `undefined` when the manifest does not list it. */
|
|
37
|
+
export declare function mediaHash(manifest: ManifestDto, name: string): string | undefined;
|
|
38
|
+
//# sourceMappingURL=manifest.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"manifest.d.ts","sourceRoot":"","sources":["../../src/core/manifest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C;;;GAGG;AACH,eAAO,MAAM,4BAA4B,MAAM,CAAC;AAoBhD;;;;;;;;;;;;GAYG;AACH,qBAAa,aAAa;WACZ,WAAW,CACvB,OAAO,EAAE,MAAM,OAAO,CAAC,WAAW,CAAC,EACnC,OAAO,GAAE;QAAE,GAAG,CAAC,EAAE,MAAM,CAAC;QAAC,UAAU,CAAC,EAAE,MAAM,CAAA;KAAO,GACjD,OAAO,CAAC,WAAW,CAAC;IA2BvB,+GAA+G;IAC/G,MAAM,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI;IAQrC,uFAAuF;IACvF,MAAM,CAAC,GAAG,CAAC,GAAG,GAAE,MAAoB,GAAG,WAAW,GAAG,IAAI;CAGzD;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,WAAW,GAAG,IAAI,EAAE,WAAW,EAAE,WAAW,GAAG,OAAO,CAUlH;AAED,iGAAiG;AACjG,wBAAgB,YAAY,CAAC,QAAQ,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAExF;AAED,mGAAmG;AACnG,wBAAgB,SAAS,CAAC,QAAQ,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAEjF"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How long a manifest is kept before it is read again, in seconds: 300, the fresh time of a copy, so one read of the
|
|
3
|
+
* manifest serves every copy that is checked within those 300 seconds (rulebook LIM-13).
|
|
4
|
+
*/
|
|
5
|
+
export const DEFAULT_MANIFEST_TTL_SECONDS = 300;
|
|
6
|
+
const DEFAULT_KEY = "default";
|
|
7
|
+
const slots = new Map();
|
|
8
|
+
function slotFor(key) {
|
|
9
|
+
let slot = slots.get(key);
|
|
10
|
+
if (!slot) {
|
|
11
|
+
slot = { manifest: null, fetchedAt: 0, pending: null };
|
|
12
|
+
slots.set(key, slot);
|
|
13
|
+
}
|
|
14
|
+
return slot;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* In-memory cache for the manifest of a service. The manifest lists the hash of every resource and media file, so
|
|
18
|
+
* a reader that holds a recent manifest knows which content changed without asking for the content itself.
|
|
19
|
+
*
|
|
20
|
+
* The manifest is read at most once per `ttlSeconds` (default 300) for each `key` (the slug of the service), and
|
|
21
|
+
* reads that happen at the same time share one request. `getContent`, `getMedia` and the media proxy read it only for
|
|
22
|
+
* a copy that is no longer fresh, so a site reads it at most once per 300 seconds for each service.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* const manifest = await ManifestCache.getManifest(() => adapter.getManifest(), { key: "my-site", ttlSeconds: 300 });
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export class ManifestCache {
|
|
30
|
+
static async getManifest(fetcher, options = {}) {
|
|
31
|
+
const slot = slotFor(options.key ?? DEFAULT_KEY);
|
|
32
|
+
const ttlMs = (options.ttlSeconds ?? DEFAULT_MANIFEST_TTL_SECONDS) * 1000;
|
|
33
|
+
if (slot.manifest && Date.now() - slot.fetchedAt < ttlMs) {
|
|
34
|
+
return slot.manifest;
|
|
35
|
+
}
|
|
36
|
+
if (slot.pending) {
|
|
37
|
+
return slot.pending;
|
|
38
|
+
}
|
|
39
|
+
const pending = fetcher().then((manifest) => {
|
|
40
|
+
slot.manifest = manifest;
|
|
41
|
+
slot.fetchedAt = Date.now();
|
|
42
|
+
slot.pending = null;
|
|
43
|
+
return manifest;
|
|
44
|
+
}, (error) => {
|
|
45
|
+
slot.pending = null;
|
|
46
|
+
throw error;
|
|
47
|
+
});
|
|
48
|
+
slot.pending = pending;
|
|
49
|
+
return pending;
|
|
50
|
+
}
|
|
51
|
+
/** Forgets the manifest of `key`, or of every service when no key is given. The next read asks the service. */
|
|
52
|
+
static invalidate(key) {
|
|
53
|
+
if (key === undefined) {
|
|
54
|
+
slots.clear();
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
slots.delete(key);
|
|
58
|
+
}
|
|
59
|
+
/** The manifest that is in memory, without asking the service and whatever its age. */
|
|
60
|
+
static get(key = DEFAULT_KEY) {
|
|
61
|
+
return slots.get(key)?.manifest ?? null;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Detect content changes by comparing manifest hashes
|
|
66
|
+
*/
|
|
67
|
+
export function hasContentChanged(path, oldManifest, newManifest) {
|
|
68
|
+
if (!oldManifest)
|
|
69
|
+
return true; // First fetch
|
|
70
|
+
const oldEntry = findManifestEntry(oldManifest, path);
|
|
71
|
+
const newEntry = findManifestEntry(newManifest, path);
|
|
72
|
+
if (!oldEntry && !newEntry)
|
|
73
|
+
return false;
|
|
74
|
+
if (!oldEntry || !newEntry)
|
|
75
|
+
return true;
|
|
76
|
+
return oldEntry.hash !== newEntry.hash;
|
|
77
|
+
}
|
|
78
|
+
/** The hash of a resource in the manifest, or `undefined` when the manifest does not list it. */
|
|
79
|
+
export function resourceHash(manifest, resource) {
|
|
80
|
+
return manifest.resources?.find((entry) => entry.name === resource)?.hash;
|
|
81
|
+
}
|
|
82
|
+
/** The hash of a media file in the manifest, or `undefined` when the manifest does not list it. */
|
|
83
|
+
export function mediaHash(manifest, name) {
|
|
84
|
+
return manifest.media?.find((entry) => entry.name === name)?.hash;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Find a manifest entry by resource/media path
|
|
88
|
+
*/
|
|
89
|
+
function findManifestEntry(manifest, path) {
|
|
90
|
+
const allEntries = [...(manifest.resources || []), ...(manifest.media || [])];
|
|
91
|
+
return allEntries.find((e) => e.name === path) ?? null;
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=manifest.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"manifest.js","sourceRoot":"","sources":["../../src/core/manifest.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/** The most bytes of media that the proxy keeps in memory unless `cache.mediaCacheMaxBytes` says otherwise: 50 MB. */
|
|
2
|
+
export declare const DEFAULT_MEDIA_CACHE_MAX_BYTES = 52428800;
|
|
3
|
+
/** Empties the memory cache of the media proxy. Meant for tests. */
|
|
4
|
+
export declare function clearLedgerMediaCache(): void;
|
|
5
|
+
/**
|
|
6
|
+
* Framework-neutral production media proxy.
|
|
7
|
+
*
|
|
8
|
+
* Fetches a media asset from the locked Ledger backend server-side and returns a
|
|
9
|
+
* Web `Response` with headers that let the browser embed every media type
|
|
10
|
+
* (pdf, svg, image, video) same-origin — stripping `X-Frame-Options`/CORS
|
|
11
|
+
* problems. Used by the per-framework media routes (`@parrel/ledger/next`,
|
|
12
|
+
* `/sveltekit`, `/nuxt`). The upstream origin is the Parrel API origin: `options.apiOrigin`, the `apiOrigin`
|
|
13
|
+
* of `defineParrelLedger`, `PARREL_API_ORIGIN` or `https://api.parrel.dev`, in this order.
|
|
14
|
+
*
|
|
15
|
+
* @param segments The path segments after the proxy route
|
|
16
|
+
* Media that the manifest lists is kept in memory by name and hash (up to `cache.mediaCacheMaxBytes` of
|
|
17
|
+
* `defineParrelLedger`, default 50 MB, `0` turns it off, the oldest file goes first). The manifest is read at most once per
|
|
18
|
+
* `cache.manifestTtlSeconds`. While the hash of a file is unchanged the file is served from memory and the service is not
|
|
19
|
+
* asked again. The answer carries `ETag: "<hash>"`, and a request with a matching `If-None-Match` gets `304`.
|
|
20
|
+
*
|
|
21
|
+
* @param options `apiOrigin` overrides the origin for this call. `request` is the request of the route: its `If-None-Match`
|
|
22
|
+
* header is checked against the hash of the file, and `ifNoneMatch` does the same for a framework that has no `Request`.
|
|
23
|
+
* @returns The file. When Ledger answers with an error, the same status, the same Parrel error body and the same
|
|
24
|
+
* content type, so the browser sees the real error code (for example `LE02500001`). When Ledger cannot be reached, a `502` with a JSON
|
|
25
|
+
* body in the Parrel error shape and the code `SD01000003`. A path that is not a media path answers `400`.
|
|
26
|
+
* @throws {ParrelError} `SD00100008` when the origin is not an allowed origin
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* // app/api/cms-media/[...path]/route.ts
|
|
31
|
+
* export async function GET(_request: Request, { params }: { params: Promise<{ path: string[] }> }) {
|
|
32
|
+
* return proxyLedgerMedia((await params).path, { apiOrigin: "http://localhost:18700" });
|
|
33
|
+
* }
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare function proxyLedgerMedia(segments: string[], options?: {
|
|
37
|
+
apiOrigin?: string;
|
|
38
|
+
request?: Request;
|
|
39
|
+
ifNoneMatch?: string | null;
|
|
40
|
+
}): Promise<Response>;
|
|
41
|
+
//# sourceMappingURL=media-proxy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"media-proxy.d.ts","sourceRoot":"","sources":["../../src/core/media-proxy.ts"],"names":[],"mappings":"AAQA,sHAAsH;AACtH,eAAO,MAAM,6BAA6B,WAAa,CAAC;AAgBxD,oEAAoE;AACpE,wBAAgB,qBAAqB,IAAI,IAAI,CAG5C;AAgFD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAsB,gBAAgB,CACrC,QAAQ,EAAE,MAAM,EAAE,EAClB,OAAO,GAAE;IAAE,SAAS,CAAC,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,OAAO,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;CAAO,GAClF,OAAO,CAAC,QAAQ,CAAC,CAmFnB"}
|