@wwchen/stack 0.2.2 → 0.3.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.
@@ -1,4 +1,6 @@
1
+ import { createRequire } from "node:module";
1
2
  import { createServer } from "node:net";
3
+ import { join } from "node:path";
2
4
  /**
3
5
  * PGlite served over the Postgres wire protocol, so the app's `pg` pool talks to it like any other
4
6
  * Postgres. Used by the test helper and a template's dev server.
@@ -25,11 +27,16 @@ async function freePort(host) {
25
27
  });
26
28
  }
27
29
  /**
28
- * PGlite and pglite-socket are optional peers, loaded only here, on the PGlite path: a
29
- * Postgres-only setup (`TEST_DATABASE_URL`) need not install them.
30
+ * PGlite and pglite-socket are optional peers, loaded only here, on the PGlite path: a Postgres-only setup (`TEST_DATABASE_URL`) need not install them. With `from` (an app's root), both come from that app's own dependencies instead of this package's resolution, as one pair: pglite-socket then loads the same PGlite build it is handed.
30
31
  */
31
- async function loadPglite() {
32
+ async function loadPglite(from) {
32
33
  try {
34
+ if (from !== undefined) {
35
+ const require = createRequire(join(from, "package.json"));
36
+ const { PGlite } = require("@electric-sql/pglite");
37
+ const { PGLiteSocketServer } = require("@electric-sql/pglite-socket");
38
+ return { PGlite, PGLiteSocketServer };
39
+ }
33
40
  const [{ PGlite }, { PGLiteSocketServer }] = await Promise.all([
34
41
  import("@electric-sql/pglite"),
35
42
  import("@electric-sql/pglite-socket"),
@@ -37,13 +44,18 @@ async function loadPglite() {
37
44
  return { PGlite, PGLiteSocketServer };
38
45
  }
39
46
  catch (error) {
40
- throw new Error("PGlite could not be loaded: install @electric-sql/pglite and @electric-sql/pglite-socket " +
41
- "as devDependencies, or set TEST_DATABASE_URL to a Postgres server for the tests to use.", { cause: error });
47
+ throw new Error(from !== undefined
48
+ ? "PGlite could not be loaded from the app: add @electric-sql/pglite and " +
49
+ "@electric-sql/pglite-socket to its devDependencies and run `pnpm install`."
50
+ : "PGlite could not be loaded: install @electric-sql/pglite and @electric-sql/pglite-socket " +
51
+ "as devDependencies, or set TEST_DATABASE_URL to a Postgres server for the tests to use.", { cause: error });
42
52
  }
43
53
  }
44
- /** In-memory when `dataDir` is absent. `port` 0 (or absent) picks a free one. */
54
+ /**
55
+ * In-memory when `dataDir` is absent. `port` 0 (or absent) picks a free one. `resolveFrom` loads PGlite from that directory's dependencies (an app's root) rather than this package's.
56
+ */
45
57
  export async function startPgliteServer(options = {}) {
46
- const { PGlite, PGLiteSocketServer } = await loadPglite();
58
+ const { PGlite, PGLiteSocketServer } = await loadPglite(options.resolveFrom);
47
59
  const host = options.host ?? "127.0.0.1";
48
60
  const port = options.port ? options.port : await freePort(host);
49
61
  const db = await PGlite.create(options.dataDir);
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Helpers for scheduled jobs that work for many people (ifttt syncs everyone's check-ins).
2
+ * Helpers for scheduled jobs that work for many people (one run syncs everyone).
3
3
  *
4
4
  * **Alert on new failures only.** An uptime monitor that calls the job alerts when it answers 500.
5
5
  * If every failure answered 500, one person whose sync is broken would hold the monitor down for
@@ -28,10 +28,7 @@ export declare function failingSince(run: {
28
28
  /** A failed run whose streak began less than `alertForHours` ago. */
29
29
  export declare function isNewFailure(run: RunOutcome, now?: number, alertForHours?: number): boolean;
30
30
  /**
31
- * `fn` over `items`, at most `limit` at a time, results in input order. A job that works for many
32
- * people shares one external quota (ifttt: one Google service account) and one function timeout,
33
- * so it neither runs them one by one nor all at once. A rejection rejects the whole call: `fn`
34
- * catches its own per-person failures.
31
+ * `fn` over `items`, at most `limit` at a time, results in input order. A job that works for many people shares one external quota and one function timeout, so it neither runs them one by one nor all at once. A rejection rejects the whole call: `fn` catches its own per-person failures.
35
32
  */
36
33
  export declare function mapLimit<T, R>(items: readonly T[], limit: number, fn: (item: T, index: number) => Promise<R>): Promise<R[]>;
37
34
  export {};
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Helpers for scheduled jobs that work for many people (ifttt syncs everyone's check-ins).
2
+ * Helpers for scheduled jobs that work for many people (one run syncs everyone).
3
3
  *
4
4
  * **Alert on new failures only.** An uptime monitor that calls the job alerts when it answers 500.
5
5
  * If every failure answered 500, one person whose sync is broken would hold the monitor down for
@@ -30,10 +30,7 @@ export function isNewFailure(run, now = Date.now(), alertForHours = ALERT_FOR_HO
30
30
  return now - ms(run.failingSince ?? run.startedAt) < alertForHours * 3_600_000;
31
31
  }
32
32
  /**
33
- * `fn` over `items`, at most `limit` at a time, results in input order. A job that works for many
34
- * people shares one external quota (ifttt: one Google service account) and one function timeout,
35
- * so it neither runs them one by one nor all at once. A rejection rejects the whole call: `fn`
36
- * catches its own per-person failures.
33
+ * `fn` over `items`, at most `limit` at a time, results in input order. A job that works for many people shares one external quota and one function timeout, so it neither runs them one by one nor all at once. A rejection rejects the whole call: `fn` catches its own per-person failures.
37
34
  */
38
35
  export async function mapLimit(items, limit, fn) {
39
36
  if (!Number.isInteger(limit) || limit < 1)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wwchen/stack",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "description": "Shared Next.js, Neon, Better Auth, Resend and lew-ds building blocks.",
5
5
  "license": "MIT",
6
6
  "repository": {