@yingyeothon/s3-cache-bridge-client
v2.2.0
Published
HTTP client for the s3-cache-bridge server: get/put/delete, append, lock/unlock, and cache sync helpers.
Readme
@yingyeothon/s3-cache-bridge-client
HTTP client for the s3-cache-bridge server: read, write, delete, and append cached objects, patch JSON documents in place, control per-key locks, and trigger cache sync or invalidation — all over plain HTTP with optional basic auth. Built on the global fetch with zero runtime dependencies.
The bridge answers from its cache when it can, and reaches S3 when it cannot.
sequenceDiagram
participant C as createS3cbClient
participant B as the bridge server
participant S as S3
C->>B: read a key
alt cached
B-->>C: the value
else not cached
B->>S: GET
S-->>B: the object
B-->>C: the value
endInstall
npm install @yingyeothon/s3-cache-bridge-clientUsage
ESM:
import { createS3cbClient } from "@yingyeothon/s3-cache-bridge-client";
const cb = createS3cbClient({
apiUrl: "https://cache.example.com/",
apiId: "my-id",
apiPassword: "my-password",
});
await cb.put("greeting", "WORLD");
const text = await cb.get("greeting"); // "WORLD"
await cb.append("greeting", "!");
const exists = await cb.exists("greeting"); // true
// JSON modification protocol.
const fetched = await cb.patch<{ a: { b: { c: number } } }>(
"doc",
{ operation: "append", path: "a.b", value: { c: 10 } },
{ fetch: true },
);
// Manual locking.
await cb.lock("greeting");
await cb.put("greeting", "LOCKED WRITE", { noLock: true });
await cb.unlock("greeting");
// Binary and file transfer.
const bytes = await cb.getBuffer("image");
await cb.download("image", "/tmp/image.png");
await cb.del("greeting");CJS:
const { createS3cbClient } = require("@yingyeothon/s3-cache-bridge-client");
const cb = createS3cbClient({ apiUrl: "http://localhost:3000/" });
cb.get("key").then(console.log);Public API
createS3cbClient(options)— factory that binds every method below to one server environmentS3cbClientOptions—{ apiUrl, apiId?, apiPassword? }; credentials become aBasicAuthorization header (type)S3cbClient— the object returned bycreateS3cbClient(type)s3cbClientOptionsFromEnv()— buildsS3cbClientOptionsfrom theS3CB_URL,S3CB_ID, andS3CB_PASSWORDenvironment variables; throws ifS3CB_URLis unset. Calling it is the caller's choice — the library itself never readsprocess.envJSONModificationRequest—append/modify/remove/fetchoperations forpatch(type)LockOptions—{ noLock?: boolean }(type)SyncOptions—{ sync?: boolean }(type)FetchOptions—{ fetch?: boolean }(type)
Client methods:
get(key, options?)— GET the object as a UTF-8 stringput(key, body, options?)— PUT a string,Buffer, orUint8Arraydel(key, options?)— DELETE the objectappend(key, body, options?)— PUT withappend=1to append to the objectsync(key)— POST withsync=1to flush the key to S3invalidate(key)— DELETE withcache=1to drop the cached copylock(key)/unlock(key)— POST withlock=acquire/lock=releasepatch<T>(key, modRequest, options?)— PATCH a JSON document; resolves the fetched value whenfetchis on (defaults to on for thefetchoperation), otherwisenullgetBuffer(key, options?)— GET the object as aBufferdownload(key, downloadPath, options?)— stream the object into a local file, resolving the pathexists(key, options?)— HEAD the object;falseon 404, throws on other errors
Every method rejects with an Error whose message is "<status> <statusText>" (for example "404 Not Found") when the server does not answer 200.
Migrating from the legacy package
- The default export is gone and the factory was renamed:
S3cb→createS3cbClient(named export,import { createS3cbClient } from ...), and theS3cbEnvtype →S3cbClientOptions. Every method is unchanged. - The package now ships dual ESM/CJS with types; deep imports (
.../lib/...) are no longer supported — import everything from the package root. - Implemented on the global
fetch(Node >= 20) instead ofnode-fetch/https;get-streamis no longer a dependency. putno longer sets a manualContent-Lengthheader;fetchderives the identical value from the buffered body automatically.- The
DEBUG=1console tracing of the legacy HTTP layer was removed.
