diff --git a/packages/graph/src/-private.ts b/packages/graph/src/-private.ts index 60cb2947c98..86c5605b838 100644 --- a/packages/graph/src/-private.ts +++ b/packages/graph/src/-private.ts @@ -1,6 +1,3 @@ -export { graphFor, peekGraph } from './-private/index'; -export { isBelongsTo } from './-private/-utils'; - /** *

extends RecordArray { [MUTATE]( target: StableRecordIdentifier[], - receiver: typeof Proxy, + receiver: typeof NativeProxy, prop: string, args: unknown[], _SIGNAL: Signal diff --git a/packages/store/src/-private.ts b/packages/store/src/-private.ts index 3a50ff42290..90b5483bb6e 100644 --- a/packages/store/src/-private.ts +++ b/packages/store/src/-private.ts @@ -1 +1,41 @@ -export * from './-private/index'; +/** + @module @ember-data/store +*/ + +export { default as Store, storeFor } from './-private/store-service'; + +export { recordIdentifierFor } from './-private/caches/instance-cache'; + +export { CacheHandler, type LifetimesService } from './-private/cache-handler'; + +export { + setIdentifierGenerationMethod, + setIdentifierUpdateMethod, + setIdentifierForgetMethod, + setIdentifierResetMethod, + isStableIdentifier, +} from './-private/caches/identifier-cache'; + +// TODO this should be a deprecated helper but we have so much usage of it +// to also eliminate +export { default as coerceId } from './-private/utils/coerce-id'; +export type { NativeProxy } from './-private/record-arrays/native-proxy-type-fix'; +export { + default as RecordArray, + default as IdentifierArray, + Collection as AdapterPopulatedRecordArray, + notifyArray, + SOURCE, + MUTATE, + ARRAY_SIGNAL, +} from './-private/record-arrays/identifier-array'; +export { default as RecordArrayManager, fastPush } from './-private/managers/record-array-manager'; + +// leaked for private use / test use, should investigate removing +export { _clearCaches } from './-private/caches/instance-cache'; +export { default as peekCache, removeRecordDataFor } from './-private/caches/cache-utils'; + +// @ember-data/model needs these temporarily +export { setRecordIdentifier, StoreMap } from './-private/caches/instance-cache'; +export { setCacheFor } from './-private/caches/cache-utils'; +export { default as _deprecatingNormalize } from './-private/utils/normalize-model-name'; diff --git a/packages/store/src/-private/index.ts b/packages/store/src/-private/index.ts deleted file mode 100644 index ea0fdd369b1..00000000000 --- a/packages/store/src/-private/index.ts +++ /dev/null @@ -1,41 +0,0 @@ -/** - @module @ember-data/store -*/ - -export { default as Store, storeFor } from './store-service'; - -export { recordIdentifierFor } from './caches/instance-cache'; - -export { CacheHandler, type LifetimesService } from './cache-handler'; - -export { - setIdentifierGenerationMethod, - setIdentifierUpdateMethod, - setIdentifierForgetMethod, - setIdentifierResetMethod, - isStableIdentifier, -} from './caches/identifier-cache'; - -// TODO this should be a deprecated helper but we have so much usage of it -// to also eliminate -export { default as coerceId } from './utils/coerce-id'; - -export { - default as RecordArray, - default as IdentifierArray, - Collection as AdapterPopulatedRecordArray, - notifyArray, - SOURCE, - MUTATE, - ARRAY_SIGNAL, -} from './record-arrays/identifier-array'; -export { default as RecordArrayManager, fastPush } from './managers/record-array-manager'; - -// leaked for private use / test use, should investigate removing -export { _clearCaches } from './caches/instance-cache'; -export { default as peekCache, removeRecordDataFor } from './caches/cache-utils'; - -// @ember-data/model needs these temporarily -export { setRecordIdentifier, StoreMap } from './caches/instance-cache'; -export { setCacheFor } from './caches/cache-utils'; -export { default as _deprecatingNormalize } from './utils/normalize-model-name'; diff --git a/packages/store/src/-private/record-arrays/identifier-array.ts b/packages/store/src/-private/record-arrays/identifier-array.ts index 57168a1c23b..c6b8193aaad 100644 --- a/packages/store/src/-private/record-arrays/identifier-array.ts +++ b/packages/store/src/-private/record-arrays/identifier-array.ts @@ -22,6 +22,7 @@ import { isStableIdentifier } from '../caches/identifier-cache'; import { recordIdentifierFor } from '../caches/instance-cache'; import type RecordArrayManager from '../managers/record-array-manager'; import type Store from '../store-service'; +import { NativeProxy } from './native-proxy-type-fix'; type KeyType = string | symbol | number; const ARRAY_GETTER_METHODS = new Set([ @@ -81,11 +82,6 @@ function convertToInt(prop: KeyType): number | null { } type ProxiedMethod = (...args: unknown[]) => unknown; -declare global { - interface ProxyConstructor { - new (target: TSource, handler: ProxyHandler): TTarget; - } -} export type IdentifierArrayCreateOptions = { identifiers: StableRecordIdentifier[]; @@ -101,9 +97,9 @@ interface PrivateState { links: Links | PaginationLinks | null; meta: Record | null; } -type ForEachCB = (record: T, index: number, context: typeof Proxy) => void; +type ForEachCB = (record: T, index: number, context: typeof NativeProxy) => void; function safeForEach( - instance: typeof Proxy, + instance: typeof NativeProxy, arr: StableRecordIdentifier[], store: Store, callback: ForEachCB, @@ -144,7 +140,7 @@ function safeForEach( interface IdentifierArray extends Omit, '[]'> { [MUTATE]?( target: StableRecordIdentifier[], - receiver: typeof Proxy, + receiver: typeof NativeProxy, prop: string, args: unknown[], _SIGNAL: Signal @@ -231,8 +227,8 @@ class IdentifierArray { // we track all mutations within the call // and forward them as one - const proxy = new Proxy(this[SOURCE], { - get>( + const proxy = new NativeProxy(this[SOURCE], { + get>( target: StableRecordIdentifier[], prop: keyof R, receiver: R @@ -344,7 +340,7 @@ class IdentifierArray { target: StableRecordIdentifier[], prop: KeyType, value: unknown, - receiver: typeof Proxy + receiver: typeof NativeProxy ): boolean { if (prop === 'length') { if (!transaction && value === 0) { diff --git a/packages/store/src/-private/record-arrays/native-proxy-type-fix.ts b/packages/store/src/-private/record-arrays/native-proxy-type-fix.ts new file mode 100644 index 00000000000..e78e384d3ad --- /dev/null +++ b/packages/store/src/-private/record-arrays/native-proxy-type-fix.ts @@ -0,0 +1,134 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +/* + We redefine Proxy because the native Proxy type treats the `target` and + `receiver` as the same type incorrectly. + + We ported this from Typescript's own Proxy types on 3/10/2024. +*/ +interface ProxyHandler { + /** + * A trap method for a function call. + * @param target The original callable object which is being proxied. + * @internal + */ + apply?(target: T, thisArg: any, argArray: any[]): any; + + /** + * A trap for the `new` operator. + * @param target The original object which is being proxied. + * @param newTarget The constructor that was originally called. + * @internal + */ + // eslint-disable-next-line @typescript-eslint/ban-types + construct?(target: T, argArray: any[], newTarget: Function): object; + + /** + * A trap for `Object.defineProperty()`. + * @param target The original object which is being proxied. + * @returns A `Boolean` indicating whether or not the property has been defined. + * @internal + */ + defineProperty?(target: T, property: string | symbol, attributes: PropertyDescriptor): boolean; + + /** + * A trap for the `delete` operator. + * @param target The original object which is being proxied. + * @param p The name or `Symbol` of the property to delete. + * @returns A `Boolean` indicating whether or not the property was deleted. + * @internal + */ + deleteProperty?(target: T, p: string | symbol): boolean; + + /** + * A trap for getting a property value. + * @param target The original object which is being proxied. + * @param p The name or `Symbol` of the property to get. + * @param receiver The proxy or an object that inherits from the proxy. + * @internal + */ + get?(target: T, p: string | symbol, receiver: any): any; + + /** + * A trap for `Object.getOwnPropertyDescriptor()`. + * @param target The original object which is being proxied. + * @param p The name of the property whose description should be retrieved. + * @internal + */ + getOwnPropertyDescriptor?(target: T, p: string | symbol): PropertyDescriptor | undefined; + + /** + * A trap for the `[[GetPrototypeOf]]` internal method. + * @param target The original object which is being proxied. + * @internal + */ + getPrototypeOf?(target: T): object | null; + + /** + * A trap for the `in` operator. + * @param target The original object which is being proxied. + * @param p The name or `Symbol` of the property to check for existence. + * @internal + */ + has?(target: T, p: string | symbol): boolean; + + /** + * A trap for `Object.isExtensible()`. + * @param target The original object which is being proxied. + * @internal + */ + isExtensible?(target: T): boolean; + + /** + * A trap for `Reflect.ownKeys()`. + * @param target The original object which is being proxied. + * @internal + */ + ownKeys?(target: T): ArrayLike; + + /** + * A trap for `Object.preventExtensions()`. + * @param target The original object which is being proxied. + * @internal + */ + preventExtensions?(target: T): boolean; + + /** + * A trap for setting a property value. + * @param target The original object which is being proxied. + * @param p The name or `Symbol` of the property to set. + * @param receiver The object to which the assignment was originally directed. + * @returns A `Boolean` indicating whether or not the property was set. + * @internal + */ + set?(target: T, p: string | symbol, newValue: any, receiver: any): boolean; + + /** + * A trap for `Object.setPrototypeOf()`. + * @param target The original object which is being proxied. + * @param newPrototype The object's new prototype or `null`. + * @internal + */ + setPrototypeOf?(target: T, v: object | null): boolean; +} + +interface ProxyConstructor { + /** + * Creates a revocable Proxy object. + * @param target A target object to wrap with Proxy. + * @param handler An object whose properties define the behavior of Proxy when an operation is attempted on it. + * @internal + */ + revocable(target: T, handler: ProxyHandler): { proxy: T; revoke: () => void }; + + /** + * Creates a Proxy object. The Proxy object allows you to create an object that can be used in place of the + * original object, but which may redefine fundamental Object operations like getting, setting, and defining + * properties. Proxy objects are commonly used to log property accesses, validate, format, or sanitize inputs. + * @param target A target object to wrap with Proxy. + * @param handler An object whose properties define the behavior of Proxy when an operation is attempted on it. + * @internal + */ + new (target: TSource, handler: ProxyHandler): TTarget; +} + +export const NativeProxy: ProxyConstructor = Proxy as unknown as ProxyConstructor; diff --git a/release/core/publish/steps/generate-tarballs.ts b/release/core/publish/steps/generate-tarballs.ts index 75304357687..f25ac0f2414 100644 --- a/release/core/publish/steps/generate-tarballs.ts +++ b/release/core/publish/steps/generate-tarballs.ts @@ -170,13 +170,10 @@ async function convertFileToModule(fileData: string, relativePath: string, pkgNa const lines = fileData.split('\n'); const maybeModuleName = pkgName + '/' + relativePath.replace(/\.d\.ts$/, ''); const moduleDir = pkgName + '/' + path.dirname(relativePath); - const moduleName = - maybeModuleName.endsWith('/index') && !maybeModuleName.endsWith('/-private/index') - ? maybeModuleName.slice(0, -6) - : maybeModuleName; + const moduleName = maybeModuleName.endsWith('/index') ? maybeModuleName.slice(0, -6) : maybeModuleName; for (let i = 0; i < lines.length; i++) { - const line = lines[i]; + const line = lines[i].replace(/^declare /, '').replaceAll(' declare ', ''); if (line.startsWith('import ')) { if (!line.includes(`'`)) { throw new Error(`Unhandled import in ${relativePath}`); @@ -214,7 +211,7 @@ async function convertFileToModule(fileData: string, relativePath: string, pkgNa // insert 2 spaces at the beginning of each line // to account for module wrapper - lines[i] = ' ' + lines[i]; + if (!lines[i].startsWith('//# sourceMappingURL=')) lines[i] = ' ' + lines[i]; } lines.unshift(`declare module '${moduleName}' {`);