Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 19 additions & 23 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,42 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
env:
CI: true
steps:
- uses: actions/checkout@v4

- name: Setup Deno
- &setup-deno
name: Setup Deno
uses: denoland/setup-deno@v2
with:
deno-version: v2.x

- name: Setup Node.js
- &setup-node
name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '24.x'

# WHY: deno.json is the Deno-side source of truth; package.json feeds the npm build.
# A stale pin (historically esbuild ^0.25.4 vs 0.25.12) silently tests a different compiler than we publish with.
- name: Check manifest pin drift
run: >-
deno eval --no-config 'const read=(f)=>JSON.parse(Deno.readTextFileSync(f));const d=read("deno.json"),p=read("package.json");for(const n of["esbuild","@deno/esbuild-plugin"]){const s=d.imports[n];const want=s.slice(s.lastIndexOf("@")+1);const raw=p.dependencies?.[n]??p.devDependencies?.[n]??"";const got=(raw.includes("@")?raw.slice(raw.lastIndexOf("@")+1):raw).replace(/^[~^]/,"");if(got!==want){console.error(n+" pin drift: deno.json="+want+" package.json="+raw);Deno.exit(1);}}'

- name: Run Deno tests
run: deno task test --runtime=deno
env:
CI: true

- name: Run Node.js tests
run: deno task test --runtime=node
env:
CI: true

docs-build:
name: Docs Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Setup Deno
uses: denoland/setup-deno@v2
with:
deno-version: v2.x
- *setup-deno

- name: Setup Node.js
uses: actions/setup-node@v4
Expand All @@ -69,10 +72,7 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '24.x'
- *setup-node

- name: Install npm dependencies
run: npm install
Expand All @@ -86,15 +86,9 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Setup Deno
uses: denoland/setup-deno@v2
with:
deno-version: v2.x
- *setup-deno

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '24.x'
- *setup-node

- name: Cache Playwright browsers
uses: actions/cache@v4
Expand All @@ -105,7 +99,9 @@ jobs:
playwright-

- name: Install Playwright browsers and system dependencies
run: deno run -A npm:playwright@^1.48.0 install --with-deps chromium
run: |
PLAYWRIGHT=$(deno eval --no-config "console.log(JSON.parse(Deno.readTextFileSync('deno.json')).imports.playwright)")
deno run -A "$PLAYWRIGHT" install --with-deps chromium

- name: Run browser tests
run: deno task test --runtime=browser
Expand Down
39 changes: 39 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,45 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),

## [Unreleased]

### Known Quirks

- `iterableFilter` is not applied during ref collection. See
`TODO(#58-followup)` in `base/core-types/type-ops.ts` and the refs matrix
characterization tests. Final JSON encoding still applies the filter.
- The final `iterableFilter` pass can drop a referenced object from `__r`
while `__d` keeps an `__rId` that points to it. Decoding such an `__rId`
yields `undefined` without an error. The wire output is pinned by the refs
matrix tests; see `TODO(#58-followup)`.
- `JSONEncoder` passes non-`Encodable` class instances through unchanged:
`isNativeValue` treats them as native, so the `must implement Encodable` error
is unreachable from the JSON encode paths for these values. The emitted value
can violate the JSON value type. Hash and checksum paths still throw for the
same values: `encodableValueHash` raises the error, so the ClassObject encode
throw must stay. Alignment is a follow-up outside #58.

### Breaking Changes

- `coreValueHash` alias removed. Use `encodableValueHash`.
- `newInstance` removed from `base/common.ts`. Encoders construct their own
concrete class through an abstract factory method, so no caller can reach a
subclass constructor through a cast.
- Clone aliasing: `coreValueClone`/`coreObjectClone` now preserve shared-ref
aliasing across array/set siblings. Aliases within object fields were already
preserved.

### Changed

- Encoder `convert*` methods (`BaseEncoder`, `JSONBaseEncoder`, `ChecksumEncoder`)
are public so the type table can dispatch to them. External subclasses must
widen `protected` overrides to public.

### Fixed

- `coreValueEquals` applies `objectFilterFields` to both objects. A field
participates when either object selects it, so equality is symmetric for
filters that inspect values. The previous second loop passed the wrong
object to the filter.

### Added

- `DBInstanceConfig.repoInactivityTimeoutMs` and
Expand Down
4 changes: 0 additions & 4 deletions base/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,6 @@ export function* unionIter<T>(...args: Iterable<T>[]): Iterable<T> {
}
}

export function newInstance<T = any>(instance: any, ...args: any[]): T {
return new (instance.constructor as any)(...args) as T;
}

/**
* Maps values from an iterable to new values using a mapping function.
*
Expand Down
2 changes: 1 addition & 1 deletion base/comparisons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export function isFunction(v: any): v is Function {

export function isEmptyObject(obj: any): boolean {
for (const k in obj) {
if (obj.hasOwnProperty(k)) {
if (Object.hasOwn(obj, k)) {
return false;
}
}
Expand Down
139 changes: 11 additions & 128 deletions base/core-types/clone.ts
Original file line number Diff line number Diff line change
@@ -1,140 +1,23 @@
import { notReached } from '../error.ts';
import { Dictionary } from '../collections/dict.ts';
import {
CoreKey,
CoreObject,
CoreType,
CoreValue,
CoreValueCloneOpts,
ReadonlyCoreArray,
} from './base.ts';
import { getCoreType, isClonable } from './utils.ts';
import type { CoreValue, CoreValueCloneOpts } from './base.ts';
import { getCoreType } from './utils.ts';
import { kTypeOps } from './type-ops.ts';

export function coreValueClone<T extends CoreValue>(
e: T,
opts?: CoreValueCloneOpts,
): T {
return internalCoreValueClone(e, undefined, opts);
// One shared context per top-level clone; the Object entry allocates
// `objectsCache` into it so shared refs stay aliased (see type-ops.ts).
return kTypeOps[getCoreType(e)].clone(e, {}, opts) as T;
}

export function coreObjectClone<T extends Object>(
obj: T,
opts?: CoreValueCloneOpts,
) {
return internalCoreValueClone(obj as any, undefined, opts);
}

function internalCoreValueClone<T extends CoreValue>(
e: T,
objectsCache: Map<CoreObject, CoreObject> | undefined,
opts?: CoreValueCloneOpts,
): T {
switch (getCoreType(e)) {
case CoreType.String:
case CoreType.Number:
case CoreType.Boolean:
case CoreType.Null:
case CoreType.Undefined:
return e;

case CoreType.Date:
return new Date((e as Date).getTime()) as T;

case CoreType.Array: {
let arr = e as ReadonlyCoreArray;
if (opts !== undefined && opts.iterableFilter !== undefined) {
arr = arr.filter(opts.iterableFilter);
}
return arr.map((v) => internalCoreValueClone(v, objectsCache, opts)) as T;
}

case CoreType.Set: {
const set = e as Set<CoreValue>;
const result = new Set<CoreValue>();
for (const v of set) {
if (opts !== undefined && opts.iterableFilter !== undefined) {
if (!opts.iterableFilter(v)) continue;
}
result.add(internalCoreValueClone(v, objectsCache, opts));
}
return result as T;
}

case CoreType.Object: {
if (objectsCache === undefined) {
objectsCache = new Map<CoreObject, CoreObject>();
}
const obj = e as { [key: string]: CoreValue };
const cached = objectsCache.get(obj);
if (cached !== undefined) return cached as T;

if (opts && opts.objectOverride) {
const [result, newObj] = opts.objectOverride(obj);
if (result) {
objectsCache.set(obj, newObj as CoreObject);
return newObj as T;
}
}

const result: { [key: string]: CoreValue } = {};
for (const k in obj) {
if (
obj.hasOwnProperty(k) &&
(opts === undefined ||
opts.objectFilterFields === undefined ||
opts.objectFilterFields(k, obj))
) {
if (opts !== undefined && opts.fieldCloneOverride !== undefined) {
result[k] = opts.fieldCloneOverride(obj, k, opts);
} else {
result[k] = internalCoreValueClone(obj[k], objectsCache, opts);
}
}
}

objectsCache.set(obj, result);

return result as T;
}

case CoreType.Dictionary: {
const newMap = new Map<CoreKey, CoreValue>();
for (const [k, v] of e as Dictionary<CoreKey, CoreValue>) {
if (opts !== undefined && opts.fieldCloneOverride !== undefined) {
newMap.set(k, opts.fieldCloneOverride(e as Dictionary, k, opts));
} else {
newMap.set(k, internalCoreValueClone(v, objectsCache, opts));
}
}
return newMap as unknown as T;
}

case CoreType.Generator: {
const arr: CoreValue[] = [];
for (const item of e as Generator<CoreValue>) {
if (opts !== undefined && opts.iterableFilter !== undefined) {
if (opts.iterableFilter(item)) {
arr.push(internalCoreValueClone(item, objectsCache, opts));
}
} else {
arr.push(internalCoreValueClone(item, objectsCache, opts));
}
}

return arr as T;
}

case CoreType.ClassObject:
if (isClonable(e)) {
return e.clone(opts) as T;
}
if (opts && opts.notClonableExt) {
const fRes = opts.notClonableExt(e as Object);
if (fRes !== undefined) return fRes as T;
}
throw new Error(`${e?.constructor.name} must be Clonable`);

default:
notReached('clone failed');
}
return kTypeOps[getCoreType(obj as unknown as CoreValue)].clone(
obj,
{},
opts,
) as T;
}
3 changes: 1 addition & 2 deletions base/core-types/comparable.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { notReached } from '../error.ts';
import { compareStrings } from '../string.ts';
import { CoreOptions, CoreType, CoreValue } from './base.ts';
import { type CoreOptions, CoreType, type CoreValue } from './base.ts';
import { encodableValueHash } from './encoding/hash.ts';
import { Comparable } from './index.ts';
import { getCoreType, isComparable, isEquatable } from './utils.ts';

export const MinComparableValue = {};
Expand Down
Loading
Loading