Skip to content

Commit

Permalink
fix: fix arethetypesworking errors in all packages (#1004)
Browse files Browse the repository at this point in the history
Motivation:
* #982

Learned While Devving
* #982 indicated 3 errors coming from atty. Here is how each error type
was resolved
1. `ESM (dynamic import only)` - this is more of a warning than an
error. It's warning, rightly, that versions of node that only use
CommonJS requires (e.g. `require("@web3-storage/did-mailto")`) won't be
able to load these libraries. i.e. these libraries are "ESM-only" in
that they only work on runtimes that can do ESM-style `import()`. By
default, attw warns on this, but this PR configures attw to ignore
instead (via .attw.json
https://github.com/web3-storage/w3up/pull/1004/files#diff-a3be4137698f4bdc5777d3831fac4e67d11c102c5e57dd54906ff333cb551aff
).
2. [internal resolution
error](https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/InternalResolutionError.md)
* Insight via
https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/InternalResolutionError.md#declarations-and-javascript-match-but-are-not-checked-with-correct-settings
> This problem can occur with tsc alone if the library author compiles
with --moduleResolution node (also known as node10) or bundler,
especially in combination with a package.json that has "type": "module"
* This attw error was resolved by this PR but this PR changing all
packages `tsconfig.json` to use moduleResolution=NodeNext (not `Node`).
To do that, many type imports had to change so the imported module
identifier includes a file path suffix
3. [no
types](https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/UntypedResolution.md)
- Most of these were due to the relevant package having sub packabe
submodule listed in package.json `exports` but without a corresponding
js file existing (e.g. it was an old package submodule that has since
been removed)

Also
* added an npm script `attw` to run attw for the package. Now can run
attw for all packages like `pnpm -r attw` from monorepo root
* gitlab ci workflows for the two packages shown in #982 now run attw
  • Loading branch information
gobengo authored Oct 25, 2023
1 parent 06e0ca9 commit 2e2936a
Show file tree
Hide file tree
Showing 89 changed files with 2,125 additions and 1,733 deletions.
3 changes: 3 additions & 0 deletions .attw.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ignoreRules": ["cjs-resolves-to-esm"]
}
1 change: 1 addition & 0 deletions .github/workflows/access-client.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ jobs:
- run: pnpm -r --filter @web3-storage/access run lint
- run: pnpm -r --filter @web3-storage/access run test
- run: pnpm -r --filter @web3-storage/access exec depcheck
- run: pnpm --filter '@web3-storage/access' attw
1 change: 1 addition & 0 deletions .github/workflows/w3up-client.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ jobs:
registry-url: https://registry.npmjs.org/
cache: 'pnpm'
- run: pnpm --filter '@web3-storage/w3up-client...' install
- run: pnpm --filter '@web3-storage/w3up-client' attw
- uses: ./packages/w3up-client/.github/actions/test
with:
w3up-client-dir: ./packages/w3up-client/
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"docs:markdown": "pnpm run build && docusaurus generate-typedoc"
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.12.2",
"@docusaurus/core": "^2.3.1",
"docusaurus-plugin-typedoc": "^0.18.0",
"lint-staged": "^13.2.0",
Expand Down
1 change: 1 addition & 0 deletions packages/access-client/.attw.json
17 changes: 9 additions & 8 deletions packages/access-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"types": "dist/src/index.d.ts",
"main": "src/index.js",
"scripts": {
"attw": "attw --pack .",
"lint": "tsc --build && eslint '**/*.{js,ts}' && prettier --check '**/*.{js,ts,yml,json}' --ignore-path ../../.gitignore",
"build": "tsc --build",
"check": "tsc --build",
Expand All @@ -24,12 +25,12 @@
"rc": "npm version prerelease --preid rc"
},
"exports": {
".": "./src/index.js",
"./agent": "./src/agent.js",
"./drivers/*": "./src/drivers/*.js",
"./stores/*": "./src/stores/*.js",
"./types": "./src/types.js",
"./encoding": "./src/encoding.js"
".": "./dist/src/index.js",
"./agent": "./dist/src/agent.js",
"./drivers/*": "./dist/src/drivers/*.js",
"./stores/*": "./dist/src/stores/*.js",
"./types": "./dist/src/types.js",
"./encoding": "./dist/src/encoding.js"
},
"typesVersions": {
"*": {
Expand Down Expand Up @@ -67,9 +68,9 @@
"@web3-storage/capabilities": "workspace:^",
"@web3-storage/did-mailto": "workspace:^",
"bigint-mod-arith": "^3.1.2",
"conf": "10.2.0",
"conf": "11.0.2",
"multiformats": "^12.1.2",
"one-webcrypto": "^1.0.3",
"one-webcrypto": "git://github.com/web3-storage/one-webcrypto",
"p-defer": "^4.0.0",
"type-fest": "^3.3.0",
"uint8arrays": "^4.0.6"
Expand Down
26 changes: 13 additions & 13 deletions packages/access-client/src/agent-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import { CID } from 'multiformats'
import { Access } from '@web3-storage/capabilities'
import { isExpired } from './delegations.js'

/** @typedef {import('./types').AgentDataModel} AgentDataModel */
/** @typedef {import('./types.js').AgentDataModel} AgentDataModel */

/** @implements {AgentDataModel} */
export class AgentData {
/** @type {(data: import('./types').AgentDataExport) => Promise<void> | void} */
/** @type {(data: import('./types.js').AgentDataExport) => Promise<void> | void} */
#save

/**
* @param {import('./types').AgentDataModel} data
* @param {import('./types').AgentDataOptions} [options]
* @param {import('./types.js').AgentDataModel} data
* @param {import('./types.js').AgentDataOptions} [options]
*/
constructor(data, options = {}) {
this.meta = data.meta
Expand All @@ -30,8 +30,8 @@ export class AgentData {
/**
* Create a new AgentData instance from the passed initialization data.
*
* @param {Partial<import('./types').AgentDataModel>} [init]
* @param {import('./types').AgentDataOptions} [options]
* @param {Partial<import('./types.js').AgentDataModel>} [init]
* @param {import('./types.js').AgentDataOptions} [options]
*/
static async create(init = {}, options = {}) {
const agentData = new AgentData(
Expand All @@ -53,11 +53,11 @@ export class AgentData {
/**
* Instantiate AgentData from previously exported data.
*
* @param {import('./types').AgentDataExport} raw
* @param {import('./types').AgentDataOptions} [options]
* @param {import('./types.js').AgentDataExport} raw
* @param {import('./types.js').AgentDataOptions} [options]
*/
static fromExport(raw, options) {
/** @type {import('./types').AgentDataModel['delegations']} */
/** @type {import('./types.js').AgentDataModel['delegations']} */
const dels = new Map()

for (const [key, value] of raw.delegations) {
Expand Down Expand Up @@ -89,7 +89,7 @@ export class AgentData {
* Export data in a format safe to pass to `structuredClone()`.
*/
export() {
/** @type {import('./types').AgentDataExport} */
/** @type {import('./types.js').AgentDataExport} */
const raw = {
meta: this.meta,
principal: this.principal.toArchive(),
Expand All @@ -112,7 +112,7 @@ export class AgentData {
/**
* @deprecated
* @param {import('@ucanto/interface').DID} did
* @param {import('./types').SpaceMeta} meta
* @param {import('./types.js').SpaceMeta} meta
* @param {import('@ucanto/interface').Delegation} [proof]
*/
async addSpace(did, meta, proof) {
Expand All @@ -131,7 +131,7 @@ export class AgentData {

/**
* @param {import('@ucanto/interface').Delegation} delegation
* @param {import('./types').DelegationMeta} [meta]
* @param {import('./types.js').DelegationMeta} [meta]
*/
async addDelegation(delegation, meta) {
this.delegations.set(delegation.cid.toString(), {
Expand Down Expand Up @@ -162,7 +162,7 @@ const isSessionCapability = (cap) => cap.can === Access.session.can
* Is the given delegation a session proof?
*
* @param {Ucanto.Delegation} delegation
* @returns {delegation is Ucanto.Delegation<[import('./types').AccessSession]>}
* @returns {delegation is Ucanto.Delegation<[import('./types.js').AccessSession]>}
*/
export const isSessionProof = (delegation) =>
delegation.capabilities.some((cap) => isSessionCapability(cap))
Expand Down
30 changes: 15 additions & 15 deletions packages/access-client/src/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const PRINCIPAL = DID.parse('did:web:web3.storage')
const agentToData = new WeakMap()

/**
* @typedef {import('./types').Service} Service
* @typedef {import('./types.js').Service} Service
* @typedef {import('@ucanto/interface').Receipt<any, any>} Receipt
*/

Expand Down Expand Up @@ -87,12 +87,12 @@ export function connection(options = {}) {
* @template {Record<string, any>} [S=Service]
*/
export class Agent {
/** @type {import('./agent-data').AgentData} */
/** @type {import('./agent-data.js').AgentData} */
#data

/**
* @param {import('./agent-data').AgentData} data - Agent data
* @param {import('./types').AgentOptions<S>} [options]
* @param {import('./agent-data.js').AgentData} data - Agent data
* @param {import('./types.js').AgentOptions<S>} [options]
*/
constructor(data, options = {}) {
/** @type { Client.Channel<S> & { url?: URL } | undefined } */
Expand All @@ -112,8 +112,8 @@ export class Agent {
* Create a new Agent instance, optionally with the passed initialization data.
*
* @template {Record<string, any>} [R=Service]
* @param {Partial<import('./types').AgentDataModel>} [init]
* @param {import('./types').AgentOptions<R> & import('./types').AgentDataOptions} [options]
* @param {Partial<import('./types.js').AgentDataModel>} [init]
* @param {import('./types.js').AgentOptions<R> & import('./types.js').AgentDataOptions} [options]
*/
static async create(init, options = {}) {
const data = await AgentData.create(init, options)
Expand All @@ -124,8 +124,8 @@ export class Agent {
* Instantiate an Agent from pre-exported agent data.
*
* @template {Record<string, any>} [R=Service]
* @param {import('./types').AgentDataExport} raw
* @param {import('./types').AgentOptions<R> & import('./types').AgentDataOptions} [options]
* @param {import('./types.js').AgentDataExport} raw
* @param {import('./types.js').AgentOptions<R> & import('./types.js').AgentDataOptions} [options]
*/
static from(raw, options = {}) {
const data = AgentData.fromExport(raw, options)
Expand Down Expand Up @@ -171,7 +171,7 @@ export class Agent {
*/
#delegations(caps) {
const _caps = new Set(caps)
/** @type {Array<{ delegation: Ucanto.Delegation, meta: import('./types').DelegationMeta }>} */
/** @type {Array<{ delegation: Ucanto.Delegation, meta: import('./types.js').DelegationMeta }>} */
const values = []
for (const [, value] of this.#data.delegations) {
// check expiration
Expand Down Expand Up @@ -320,7 +320,7 @@ export class Agent {
expiration: Infinity,
})

/** @type {import('./types').SpaceMeta} */
/** @type {import('./types.js').SpaceMeta} */
const meta = { isRegistered: false }
// eslint-disable-next-line eqeqeq
if (name != undefined) {
Expand All @@ -345,7 +345,7 @@ export class Agent {
* @param {Ucanto.Delegation} delegation
*/
async importSpaceFromDelegation(delegation) {
const meta = /** @type {import('./types').SpaceMeta} */ (
const meta = /** @type {import('./types.js').SpaceMeta} */ (
delegation.facts[0]?.space ?? { isRegistered: false }
)
// @ts-ignore
Expand Down Expand Up @@ -434,7 +434,7 @@ export class Agent {

/**
*
* @param {import('./types').DelegationOptions} options
* @param {import('./types.js').DelegationOptions} options
*/
async delegate(options) {
const space = this.currentSpaceWithMeta()
Expand Down Expand Up @@ -501,7 +501,7 @@ export class Agent {
* @template {Ucanto.URI} R
* @template {Ucanto.Caveats} C
* @param {Ucanto.TheCapabilityParser<Ucanto.CapabilityMatch<A, R, C>>} cap
* @param {import('./types').InvokeOptions<A, R, Ucanto.TheCapabilityParser<Ucanto.CapabilityMatch<A, R, C>>>} options
* @param {import('./types.js').InvokeOptions<A, R, Ucanto.TheCapabilityParser<Ucanto.CapabilityMatch<A, R, C>>>} options
* @returns {Promise<Ucanto.InferReceipt<Ucanto.Capability<A, R, C>, S>>}
*/
async invokeAndExecute(cap, options) {
Expand Down Expand Up @@ -554,7 +554,7 @@ export class Agent {
* @template {Ucanto.TheCapabilityParser<Ucanto.CapabilityMatch<A, R, C>>} CAP
* @template {Ucanto.Caveats} [C={}]
* @param {CAP} cap
* @param {import('./types').InvokeOptions<A, R, CAP>} options
* @param {import('./types.js').InvokeOptions<A, R, CAP>} options
*/
async invoke(cap, options) {
const space = options.with || this.currentSpace()
Expand Down Expand Up @@ -614,7 +614,7 @@ export class Agent {
throw inv.out.error
}

return /** @type {import('./types').SpaceInfoResult} */ (inv.out.ok)
return /** @type {import('./types.js').SpaceInfoResult} */ (inv.out.ok)
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/access-client/src/crypto/aes-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as uint8arrays from 'uint8arrays'
import { randomIV } from './encoding.js'

/**
* @typedef {import('./types').EncryptionKeypair} EncryptionKeypair
* @typedef {import('./types.js').EncryptionKeypair} EncryptionKeypair
* @implements {EncryptionKeypair}
*/
export class AesKey {
Expand Down
2 changes: 1 addition & 1 deletion packages/access-client/src/crypto/p256-ecdh.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export async function pubkeyBytesFromDID(did) {
}

/**
* @typedef {import('./types').KeyExchangeKeypair} SharedKey
* @typedef {import('./types.js').KeyExchangeKeypair} SharedKey
* @implements {SharedKey}
*/
export class EcdhKeypair {
Expand Down
2 changes: 1 addition & 1 deletion packages/access-client/src/drivers/conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as JSON from '../utils/json.js'

/**
* @template T
* @typedef {import('./types').Driver<T>} Driver
* @typedef {import('./types.js').Driver<T>} Driver
*/

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/access-client/src/drivers/indexeddb.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import defer from 'p-defer'

/**
* @template T
* @typedef {import('./types').Driver<T>} Driver
* @typedef {import('./types.js').Driver<T>} Driver
*/

const STORE_NAME = 'AccessStore'
Expand Down
2 changes: 1 addition & 1 deletion packages/access-client/src/drivers/memory.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @template T
* @typedef {import('./types').Driver<T>} Driver
* @typedef {import('./types.js').Driver<T>} Driver
*/

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/access-client/src/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function delegationsToBytes(delegations) {
* Decode bytes into Delegations
*
* @template {Types.Capabilities} [T=Types.Capabilities]
* @param {import('./types').BytesDelegation<T>} bytes
* @param {import('./types.js').BytesDelegation<T>} bytes
*/
export function bytesToDelegations(bytes) {
if (!(bytes instanceof Uint8Array) || bytes.length === 0) {
Expand Down Expand Up @@ -122,7 +122,7 @@ export function delegationToString(delegation, encoding) {
* Decode string into {@link Types.Delegation Delegation}
*
* @template {Types.Capabilities} [T=Types.Capabilities]
* @param {import('./types').EncodedDelegation<T>} raw
* @param {import('./types.js').EncodedDelegation<T>} raw
* @param {import('uint8arrays/to-string').SupportedEncodings} [encoding]
*/
export function stringToDelegations(raw, encoding = 'base64url') {
Expand All @@ -135,7 +135,7 @@ export function stringToDelegations(raw, encoding = 'base64url') {
* Decode string into a {@link Types.Delegation Delegation}
*
* @template {Types.Capabilities} [T=Types.Capabilities]
* @param {import('./types').EncodedDelegation<T>} raw
* @param {import('./types.js').EncodedDelegation<T>} raw
* @param {import('uint8arrays/to-string').SupportedEncodings} [encoding]
*/
export function stringToDelegation(raw, encoding) {
Expand Down
2 changes: 1 addition & 1 deletion packages/access-client/src/stores/store-conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import { ConfDriver } from '../drivers/conf.js'
* import { StoreConf } from '@web3-storage/access/stores/store-conf'
* ```
*
* @extends {ConfDriver<import('../types').AgentDataExport>}
* @extends {ConfDriver<import('../types.js').AgentDataExport>}
*/
export class StoreConf extends ConfDriver {}
2 changes: 1 addition & 1 deletion packages/access-client/src/stores/store-indexeddb.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import { IndexedDBDriver } from '../drivers/indexeddb.js'
* import { StoreIndexedDB } from '@web3-storage/access/stores/store-indexeddb'
* ```
*
* @extends {IndexedDBDriver<import('../types').AgentDataExport>}
* @extends {IndexedDBDriver<import('../types.js').AgentDataExport>}
*/
export class StoreIndexedDB extends IndexedDBDriver {}
2 changes: 1 addition & 1 deletion packages/access-client/src/stores/store-memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import { MemoryDriver } from '../drivers/memory.js'
* import { StoreMemory } from '@web3-storage/access/stores/store-memory'
* ```
*
* @extends {MemoryDriver<import('../types').AgentDataExport>}
* @extends {MemoryDriver<import('../types.js').AgentDataExport>}
*/
export class StoreMemory extends MemoryDriver {}
3 changes: 1 addition & 2 deletions packages/access-client/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"lib": ["ESNext", "DOM"],
"emitDeclarationOnly": true
"lib": ["ESNext", "DOM"]
},
"include": ["src", "scripts", "test", "package.json"],
"exclude": ["**/node_modules/**"],
Expand Down
1 change: 1 addition & 0 deletions packages/capabilities/.attw.json
1 change: 1 addition & 0 deletions packages/capabilities/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"types": "dist/src/index.d.ts",
"main": "src/index.js",
"scripts": {
"attw": "attw --pack .",
"lint": "tsc && eslint '**/*.{js,ts}' && prettier --check '**/*.{js,ts,yml,json}' --ignore-path ../../.gitignore",
"build": "tsc --build",
"check": "tsc --build",
Expand Down
2 changes: 1 addition & 1 deletion packages/capabilities/src/filecoin/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const FR32_SHA2_256_TRUNC254_PADDED_BINARY_TREE = /** @type {const} */ (0x1011)
*/
const RAW_CODE = /** @type {const} */ (0x55)

export const PieceLink = /** @type {import('../types').PieceLinkSchema} */ (
export const PieceLink = /** @type {import('../types.js').PieceLinkSchema} */ (
Schema.link({
code: RAW_CODE,
version: 1,
Expand Down
1 change: 1 addition & 0 deletions packages/did-mailto/.attw.json
Loading

0 comments on commit 2e2936a

Please sign in to comment.