@shard-for-obsidian/lib 0.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.
Files changed (48) hide show
  1. package/README.md +77 -0
  2. package/dist/client/FetchAdapter.d.ts +14 -0
  3. package/dist/client/FetchAdapter.d.ts.map +1 -0
  4. package/dist/client/FetchAdapter.js +1 -0
  5. package/dist/client/OciRegistryClient.d.ts +196 -0
  6. package/dist/client/OciRegistryClient.d.ts.map +1 -0
  7. package/dist/client/OciRegistryClient.js +704 -0
  8. package/dist/client/RegistryClientOptions.d.ts +18 -0
  9. package/dist/client/RegistryClientOptions.d.ts.map +1 -0
  10. package/dist/client/RegistryClientOptions.js +1 -0
  11. package/dist/errors/RegistryErrors.d.ts +39 -0
  12. package/dist/errors/RegistryErrors.d.ts.map +1 -0
  13. package/dist/errors/RegistryErrors.js +52 -0
  14. package/dist/ghcr/GhcrConstants.d.ts +5 -0
  15. package/dist/ghcr/GhcrConstants.d.ts.map +1 -0
  16. package/dist/ghcr/GhcrConstants.js +4 -0
  17. package/dist/index.d.ts +15 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.js +13 -0
  20. package/dist/index.js.map +7 -0
  21. package/dist/parsing/IndexParser.d.ts +26 -0
  22. package/dist/parsing/IndexParser.d.ts.map +1 -0
  23. package/dist/parsing/IndexParser.js +106 -0
  24. package/dist/parsing/LinkHeaderParser.d.ts +8 -0
  25. package/dist/parsing/LinkHeaderParser.d.ts.map +1 -0
  26. package/dist/parsing/LinkHeaderParser.js +34 -0
  27. package/dist/parsing/RepoParser.d.ts +54 -0
  28. package/dist/parsing/RepoParser.d.ts.map +1 -0
  29. package/dist/parsing/RepoParser.js +186 -0
  30. package/dist/types/AuthTypes.d.ts +14 -0
  31. package/dist/types/AuthTypes.d.ts.map +1 -0
  32. package/dist/types/AuthTypes.js +4 -0
  33. package/dist/types/ManifestTypes.d.ts +98 -0
  34. package/dist/types/ManifestTypes.d.ts.map +1 -0
  35. package/dist/types/ManifestTypes.js +7 -0
  36. package/dist/types/RegistryTypes.d.ts +48 -0
  37. package/dist/types/RegistryTypes.d.ts.map +1 -0
  38. package/dist/types/RegistryTypes.js +4 -0
  39. package/dist/types/RequestTypes.d.ts +23 -0
  40. package/dist/types/RequestTypes.d.ts.map +1 -0
  41. package/dist/types/RequestTypes.js +4 -0
  42. package/dist/utils/DigestUtils.d.ts +9 -0
  43. package/dist/utils/DigestUtils.d.ts.map +1 -0
  44. package/dist/utils/DigestUtils.js +26 -0
  45. package/dist/utils/ValidationUtils.d.ts +2 -0
  46. package/dist/utils/ValidationUtils.d.ts.map +1 -0
  47. package/dist/utils/ValidationUtils.js +6 -0
  48. package/package.json +50 -0
package/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # shard-lib
2
+
3
+ OCI-compatible container registry client library for TypeScript/JavaScript.
4
+
5
+ ## Features
6
+
7
+ - Parse and validate Docker/OCI repository references
8
+ - Interact with OCI registries (Docker Hub, GHCR, etc.)
9
+ - Support for Docker v2 and OCI manifest formats
10
+ - Bearer token and basic authentication
11
+ - Link header parsing for pagination
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ pnpm add shard-lib
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ```typescript
22
+ import { OciRegistryClient, parseRepoAndRef } from 'shard-lib';
23
+
24
+ // Parse a repository reference
25
+ const repo = parseRepoAndRef('ghcr.io/owner/repo:latest');
26
+ console.log(repo.index.name); // 'ghcr.io'
27
+ console.log(repo.tag); // 'latest'
28
+
29
+ // Create a registry client
30
+ const client = new OciRegistryClient({
31
+ repo: repo,
32
+ adapter: { fetch },
33
+ username: 'user',
34
+ password: 'token',
35
+ });
36
+
37
+ // List tags
38
+ const tags = await client.listTags();
39
+
40
+ // Get manifest
41
+ const manifest = await client.getManifest('latest');
42
+ ```
43
+
44
+ ## Project Structure
45
+
46
+ ```
47
+ src/
48
+ client/ # OCI registry client and adapters
49
+ errors/ # Custom error classes
50
+ ghcr/ # GitHub Container Registry constants
51
+ parsing/ # Repository and index parsing
52
+ types/ # TypeScript type definitions
53
+ utils/ # Utility functions
54
+ __tests__/ # Test files
55
+ ```
56
+
57
+ ## API
58
+
59
+ ### Parsing
60
+
61
+ - `parseIndex(arg?: string): RegistryIndex` - Parse registry index/URL
62
+ - `parseRepo(arg: string, defaultIndex?: string | RegistryIndex): RegistryRepo` - Parse repository
63
+ - `parseRepoAndRef(arg: string, defaultIndex?: string | RegistryIndex): RegistryImage` - Parse repository with tag/digest
64
+ - `urlFromIndex(index: RegistryIndex, scheme?: 'http' | 'https'): string` - Generate URL from index
65
+
66
+ ### Client
67
+
68
+ - `OciRegistryClient` - Main registry client class
69
+ - `listTags()` - List repository tags
70
+ - `getManifest(ref: string)` - Get image manifest
71
+ - `getBlob(digest: string)` - Download blob by digest
72
+
73
+ ### Types
74
+
75
+ See TypeScript definitions for complete type information.
76
+
77
+
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Adapter interface for HTTP requests.
3
+ * Allows OciRegistryClient to work with different fetch implementations.
4
+ */
5
+ export interface FetchAdapter {
6
+ /**
7
+ * Perform an HTTP request using fetch semantics.
8
+ * @param input URL string or Request object
9
+ * @param init Optional request initialization
10
+ * @returns Promise resolving to Response
11
+ */
12
+ fetch(input: string | Request, init?: RequestInit): Promise<Response>;
13
+ }
14
+ //# sourceMappingURL=FetchAdapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FetchAdapter.d.ts","sourceRoot":"","sources":["../../src/client/FetchAdapter.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,YAAY;IAC3B;;;;;OAKG;IACH,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,EAAE,IAAI,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;CACvE"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,196 @@
1
+ import type { Manifest, ManifestOCI, ManifestOCIDescriptor, ObsidianManifest } from "../types/ManifestTypes.js";
2
+ import type { RegistryRepo, TagList } from "../types/RegistryTypes.js";
3
+ import type { AuthInfo } from "../types/AuthTypes.js";
4
+ import type { RegistryClientOptions } from "./RegistryClientOptions.js";
5
+ export declare class OciRegistryClient {
6
+ readonly version = 2;
7
+ insecure: boolean;
8
+ repo: RegistryRepo;
9
+ acceptOCIManifests: boolean;
10
+ acceptManifestLists: boolean;
11
+ username?: string;
12
+ password?: string;
13
+ scopes: string[];
14
+ private _loggedIn;
15
+ private _loggedInScope?;
16
+ private _authInfo?;
17
+ private _headers;
18
+ private _url;
19
+ private _userAgent;
20
+ private readonly _adapter;
21
+ /**
22
+ * Create a new GHCR client for a particular repository.
23
+ *
24
+ * @param opts.insecure {Boolean} Optional. Default false. Set to true
25
+ * to *not* fail on an invalid or this-signed server certificate.
26
+ * @param opts.adapter {FetchAdapter} Required. HTTP adapter for making requests.
27
+ * ... TODO: lots more to document
28
+ *
29
+ */
30
+ constructor(opts: RegistryClientOptions);
31
+ /**
32
+ * Login V2
33
+ *
34
+ * Typically one does not need to call this function directly because most
35
+ * methods of a `GHCRClient` will automatically login as necessary.
36
+ *
37
+ * @param opts {Object}
38
+ * - opts.scope {String} Optional. A scope string passed in for
39
+ * bearer/token auth. If this is just a login request where the token
40
+ * won't be used, then the empty string (the default) is sufficient.
41
+ * // JSSTYLED
42
+ * See <https://github.com/docker/distribution/blob/master/docs/spec/auth/token.md#requesting-a-token>
43
+ * @return an object with authentication info
44
+ */
45
+ performLogin(opts: {
46
+ scope?: string;
47
+ }): Promise<AuthInfo>;
48
+ /**
49
+ * Get an auth token.
50
+ *
51
+ * See: docker/docker.git:registry/token.go
52
+ */
53
+ _getToken(opts: {
54
+ realm: string;
55
+ service?: string;
56
+ scopes?: string[];
57
+ }): Promise<string>;
58
+ /**
59
+ * Get a registry session (i.e. login to the registry).
60
+ *
61
+ * Typically one does not need to call this method directly because most
62
+ * methods of a client will automatically login as necessary.
63
+ *
64
+ * @param opts {Object} Optional.
65
+ * - opts.scope {String} Optional. Scope to use in the auth Bearer token.
66
+ *
67
+ * Side-effects:
68
+ * - On success, all of `this._loggedIn*`, `this._authInfo`, and
69
+ * `this._headers.authorization` are set.
70
+ */
71
+ login(opts?: {
72
+ scope?: string;
73
+ }): Promise<void>;
74
+ listTags(props?: {
75
+ pageSize?: number;
76
+ startingAfter?: string;
77
+ }): Promise<TagList>;
78
+ listAllTags(props?: {
79
+ pageSize?: number;
80
+ }): Promise<TagList>;
81
+ listTagsPaginated(props?: {
82
+ pageSize?: number;
83
+ }): AsyncGenerator<TagList>;
84
+ getManifest(opts: {
85
+ ref: string;
86
+ acceptManifestLists?: boolean;
87
+ acceptOCIManifests?: boolean;
88
+ followRedirects?: boolean;
89
+ }): Promise<{
90
+ resp: Response;
91
+ manifest: Manifest;
92
+ }>;
93
+ /**
94
+ * Makes a http request to the given url, following any redirects, then fires
95
+ * the callback(err, req, responses) with the result.
96
+ *
97
+ * Note that 'responses' is an *array* of Response objects, with
98
+ * the last response being at the end of the array. When there is more than
99
+ * one response, it means a redirect has been followed.
100
+ */
101
+ _makeHttpRequest(opts: {
102
+ method: string;
103
+ path: string;
104
+ headers?: Record<string, string>;
105
+ followRedirects?: boolean;
106
+ maxRedirects?: number;
107
+ }): Promise<Response[]>;
108
+ _headOrGetBlob(method: "GET" | "HEAD", digest: string): Promise<Response[]>;
109
+ headBlob(opts: {
110
+ digest: string;
111
+ }): Promise<Response[]>;
112
+ /**
113
+ * Download a blob and return its ArrayBuffer.
114
+ * <https://docs.docker.com/registry/spec/api/#get-blob>
115
+ *
116
+ * @return
117
+ * The `buffer` is the blob's content as an ArrayBuffer.
118
+ * `ress` (plural of 'res') is an array of responses
119
+ * after following redirects. The full set of responses are returned mainly because
120
+ * headers on both the first, e.g. 'Docker-Content-Digest', and last,
121
+ * e.g. 'Content-Length', might be interesting.
122
+ */
123
+ downloadBlob(opts: {
124
+ digest: string;
125
+ }): Promise<{
126
+ ress: Response[];
127
+ buffer: ArrayBuffer;
128
+ }>;
129
+ /**
130
+ * Upload a blob using POST then PUT workflow.
131
+ * <https://github.com/opencontainers/distribution-spec/blob/main/spec.md#post-then-put>
132
+ *
133
+ * @param opts.data The blob data as ArrayBuffer or Uint8Array
134
+ * @param opts.digest Optional digest. If not provided, it will be calculated.
135
+ * @returns Object with digest and size of the uploaded blob
136
+ */
137
+ pushBlob(opts: {
138
+ data: ArrayBuffer | Uint8Array;
139
+ }): Promise<{
140
+ digest: string;
141
+ size: number;
142
+ }>;
143
+ /**
144
+ * Upload an image manifest.
145
+ * <https://github.com/opencontainers/distribution-spec/blob/main/spec.md#pushing-manifests>
146
+ *
147
+ * @param opts.ref The tag or digest to push to
148
+ * @param opts.manifest The manifest object to upload
149
+ * @param opts.mediaType Optional media type (defaults to OCI manifest type)
150
+ * @returns Object with digest and size of the uploaded manifest
151
+ */
152
+ pushManifest(opts: {
153
+ ref: string;
154
+ manifest: Manifest;
155
+ mediaType?: string;
156
+ }): Promise<{
157
+ digest: string;
158
+ size: number;
159
+ }>;
160
+ /**
161
+ * Push a plugin manifest as a config blob and create an OCI manifest.
162
+ * This follows the OCI spec where the Obsidian manifest is stored as the config.
163
+ *
164
+ * @param opts.ref The tag or digest to push to
165
+ * @param opts.pluginManifest The Obsidian plugin manifest
166
+ * @param opts.layers The layer descriptors (main.js, styles.css, etc.)
167
+ * @param opts.annotations Optional annotations for the OCI manifest
168
+ * @returns Object with digest, configDigest, and the created manifest
169
+ */
170
+ pushPluginManifest(opts: {
171
+ ref: string;
172
+ pluginManifest: ObsidianManifest;
173
+ layers: ManifestOCIDescriptor[];
174
+ annotations?: Record<string, string>;
175
+ }): Promise<{
176
+ digest: string;
177
+ configDigest: string;
178
+ manifest: ManifestOCI;
179
+ }>;
180
+ /**
181
+ * Pull a plugin manifest by extracting it from the OCI config blob.
182
+ * This follows the OCI spec where the Obsidian manifest is stored as the config.
183
+ *
184
+ * @param opts.ref The tag or digest to pull
185
+ * @returns Object with the plugin manifest, OCI manifest, and digests
186
+ */
187
+ pullPluginManifest(opts: {
188
+ ref: string;
189
+ }): Promise<{
190
+ pluginManifest: ObsidianManifest;
191
+ manifest: ManifestOCI;
192
+ manifestDigest: string;
193
+ configDigest: string;
194
+ }>;
195
+ }
196
+ //# sourceMappingURL=OciRegistryClient.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"OciRegistryClient.d.ts","sourceRoot":"","sources":["../../src/client/OciRegistryClient.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EACV,QAAQ,EACR,WAAW,EACX,qBAAqB,EACrB,gBAAgB,EACjB,MAAM,2BAA2B,CAAC;AACnC,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,2BAA2B,CAAC;AACvE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AA6JxE,qBAAa,iBAAiB;IAC5B,QAAQ,CAAC,OAAO,KAAK;IACrB,QAAQ,EAAE,OAAO,CAAC;IAClB,IAAI,EAAE,YAAY,CAAC;IACnB,kBAAkB,EAAE,OAAO,CAAC;IAC5B,mBAAmB,EAAE,OAAO,CAAC;IAC7B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,OAAO,CAAC,SAAS,CAAU;IAC3B,OAAO,CAAC,cAAc,CAAC,CAAgB;IACvC,OAAO,CAAC,SAAS,CAAC,CAAkB;IACpC,OAAO,CAAC,QAAQ,CAAyB;IACzC,OAAO,CAAC,IAAI,CAAS;IACrB,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAEvB;IAEF;;;;;;;;OAQG;gBACS,IAAI,EAAE,qBAAqB;IAwCvC;;;;;;;;;;;;;OAaG;IACG,YAAY,CAAC,IAAI,EAAE;QAAE,KAAK,CA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