Skip to content

Commit

Permalink
feat: polyfill AbortController, so that Ceramic node works on Node.js…
Browse files Browse the repository at this point in the history
… v14
  • Loading branch information
ukstv committed Mar 21, 2022
1 parent af28f57 commit 3305f52
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 15,058 deletions.
15,075 changes: 35 additions & 15,040 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/3id-did-resolver/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('3ID DID Resolver', () => {

test.each(vectors[v1].queries)('resolves 3id documents correctly %#', async (query) => {
const ThreeIdResolver = await import('../index.js')
const threeIdResolver = ThreeIdResolver.getResolver(ceramic)
const threeIdResolver = ThreeIdResolver.gwetResolver(ceramic)
const resolver = new Resolver(threeIdResolver)
const did = vectors[v1].did + query.params[0]
expect(await resolver.resolve(did)).toEqual(query.result)
Expand Down
12 changes: 7 additions & 5 deletions packages/3id-did-resolver/src/error-representation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ export const errorRepresentation = (metadata: DIDResolutionMetadata): DIDResolut
* Report a thrown error as a DID resolution result.
*/
export const withResolutionError = (f: WrappedResolver): Promise<DIDResolutionResult> => {
return f().catch((e) =>
errorRepresentation({
error: 'invalidDid',
message: e.toString(),
})
return f().catch((e) => {
console.error(e)
return errorRepresentation({
error: 'invalidDid',
message: e.toString()
})
}
)
}
1 change: 1 addition & 0 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"lodash.clonedeep": "^4.5.0",
"logfmt": "^1.3.2",
"multiformats": "^9.5.8",
"abort-controller": "^3.0.0",
"rxjs": "^7.5.2",
"uint8arrays": "^3.0.0"
},
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export * from './unreachable-case-error.js'
export * from './running-state-like.js'
export * from './stream-state-subject.js'
export * from './subscription-set.js'
export * from './polyfill-abort-controller.js'

import type { IPFS } from 'ipfs-core-types'
export type IpfsApi = IPFS
11 changes: 11 additions & 0 deletions packages/common/src/polyfill-abort-controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as polyfill from 'abort-controller'

export function polyfillAbortController() {
if (!globalThis.AbortController) {
globalThis.AbortController = polyfill.AbortController
}

if (!globalThis.AbortSignal) {
globalThis.AbortSignal = polyfill.AbortSignal
}
}
3 changes: 3 additions & 0 deletions packages/common/src/utils/abort-signal-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { timer, fromEvent, merge, Subscription } from 'rxjs'
import { first } from 'rxjs/operators'
import { polyfillAbortController } from '../polyfill-abort-controller.js'

polyfillAbortController()

export function mergeAbortSignals(signals: AbortSignal[]): AbortSignal {
const controller = new AbortController()
Expand Down
12 changes: 10 additions & 2 deletions packages/core/src/__tests__/dispatcher-mock-ipfs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@ import { jest } from '@jest/globals'
import { Dispatcher } from '../dispatcher.js'
import { CID } from 'multiformats/cid'
import { StreamID } from '@ceramicnetwork/streamid'
import { CommitType, StreamState, LoggerProvider, IpfsApi, TestUtils } from '@ceramicnetwork/common'
import {
CommitType,
StreamState,
LoggerProvider,
IpfsApi,
TestUtils,
polyfillAbortController,
} from '@ceramicnetwork/common'
import { serialize, MsgType } from '../pubsub/pubsub-message.js'
import { Repository, RepositoryDependencies } from '../state-management/repository.js'
import { delay } from './delay.js'
import tmp from 'tmp-promise'
import { LevelStateStore } from '../store/level-state-store.js'
import { PinStore } from '../store/pin-store.js'
import { RunningState } from '../state-management/running-state.js'
import { StateManager } from '../state-management/state-manager.js'

polyfillAbortController()

const TOPIC = '/ceramic'
const FAKE_CID = CID.parse('bafybeig6xv5nwphfmvcnektpnojts33jqcuam7bmye2pb54adnrtccjlsu')
const FAKE_CID2 = CID.parse('bafybeig6xv5nwphfmvcnektpnojts44jqcuam7bmye2pb54adnrtccjlsu')
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/__tests__/initialization.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { jest } from '@jest/globals'
import { Ceramic } from '../ceramic.js'
import tmp from 'tmp-promise'
import { IpfsApi } from '@ceramicnetwork/common'
import type { IpfsApi } from '@ceramicnetwork/common'
import { createIPFS } from '@ceramicnetwork/ipfs-daemon'
import { InMemoryAnchorService } from '../anchor/memory/in-memory-anchor-service.js'
import { delay } from './delay.js'
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/ceramic.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { polyfillAbortController } from '@ceramicnetwork/common'

polyfillAbortController()

import { Dispatcher } from './dispatcher.js'
import { StreamID, CommitID, StreamRef } from '@ceramicnetwork/streamid'
import { IpfsTopology } from '@ceramicnetwork/ipfs-topology'
Expand Down
4 changes: 4 additions & 0 deletions packages/ipfs-daemon/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { polyfillAbortController } from '@ceramicnetwork/common'

polyfillAbortController()

export * from './create-ipfs.js'
export * from './ipfs-daemon.js'
export * from './healthcheck-server.js'
18 changes: 9 additions & 9 deletions packages/key-did-resolver/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ describe('Index mapper', () => {
didUrl: 'did:key:zruuPojWkzGPb8sVc42f2YxcTXKUTpAUbdrzVovaTBmGGNyK6cGFaA4Kp7SSLKecrxYz8Sc9d77Rss7rayYt1oFCaNJ/some/path',
path: '/some/path'
}

doc = await resolve('did:key:zruuPojWkzGPb8sVc42f2YxcTXKUTpAUbdrzVovaTBmGGNyK6cGFaA4Kp7SSLKecrxYz8Sc9d77Rss7rayYt1oFCaNJ', parsedDid, {}, { accept: 'application/did+ld+json' })
expect(doc).toMatchSnapshot()

doc = await resolve('did:key:zruuPojWkzGPb8sVc42f2YxcTXKUTpAUbdrzVovaTBmGGNyK6cGFaA4Kp7SSLKecrxYz8Sc9d77Rss7rayYt1oFCaNJ', parsedDid, {}, { accept: 'application/did+json' })
expect(doc).toMatchSnapshot()

Expand All @@ -58,10 +58,10 @@ describe('Index mapper', () => {
didUrl: 'did:key:zDnaeUKTWUXc1HDpGfKbEK31nKLN19yX5aunFd7VK1CUMeyJu/some/path',
path: '/some/path'
}

doc = await resolve('did:key:zDnaeUKTWUXc1HDpGfKbEK31nKLN19yX5aunFd7VK1CUMeyJu', parsedDid, {}, { accept: 'application/did+ld+json' })
expect(doc).toMatchSnapshot()

doc = await resolve('did:key:zDnaeUKTWUXc1HDpGfKbEK31nKLN19yX5aunFd7VK1CUMeyJu', parsedDid, {}, { accept: 'application/did+json' })
expect(doc).toMatchSnapshot()

Expand All @@ -72,10 +72,10 @@ describe('Index mapper', () => {
didUrl: 'did:key:z4oJ8emo5e6mGPCUS5wncFZXAyuVzGRyJZvoduwq7FrdZYPd1LZQbDKsp1YAMX8x14zBwy3yHMSpfecJCMDeRFUgFqYsY/some/path',
path: '/some/path'
}

doc = await resolve('did:key:z4oJ8emo5e6mGPCUS5wncFZXAyuVzGRyJZvoduwq7FrdZYPd1LZQbDKsp1YAMX8x14zBwy3yHMSpfecJCMDeRFUgFqYsY', parsedDid, {}, { accept: 'application/did+ld+json' })
expect(doc).toMatchSnapshot()

doc = await resolve('did:key:z4oJ8emo5e6mGPCUS5wncFZXAyuVzGRyJZvoduwq7FrdZYPd1LZQbDKsp1YAMX8x14zBwy3yHMSpfecJCMDeRFUgFqYsY', parsedDid, {}, { accept: 'application/did+json' })
expect(doc).toMatchSnapshot()

Expand All @@ -100,7 +100,7 @@ describe('Index mapper', () => {
didUrl: 'did:key:z82LkvCwHNreneWpsgPEbV3gu1C6NFJEBg4srfJ5gdxEsMGRJUz2sG9FE42shbn2xkZJh54/some/path',
path: '/some/path'
}

doc = await resolve('did:key:z82LkvCwHNreneWpsgPEbV3gu1C6NFJEBg4srfJ5gdxEsMGRJUz2sG9FE42shbn2xkZJh54', parsedDid, {}, { accept: 'application/did+ld+json' })
expect(doc).toMatchSnapshot()

Expand All @@ -127,9 +127,9 @@ describe('Exception mapper + default fpr options.accept', () => {
didUrl: 'did:key:z6LSeu9HkTHSfLLeUs2nnzUSNedgDUevfNQgQjQC23ZCit6F/some/path',
path: '/some/path'
}
await expect(resolve('did:key:z6LSeu9HkTHSfLLeUs2nnzUSNedgDUevfNQgQjQC23ZCit6F', parsedDided25519, {}, { accept: 'application/did+ld+json' })).resolves.toEqual({"didDocument": null, "didDocumentMetadata": {}, "didResolutionMetadata": {"contentType": "application/did+ld+json", "error": "invalidDid", "message": "TypeError: Cannot read properties of undefined (reading 'keyToDidDoc')"}})
await expect(resolve('did:key:z6LSeu9HkTHSfLLeUs2nnzUSNedgDUevfNQgQjQC23ZCit6F', parsedDided25519, {}, { accept: 'application/did+ld+json' })).resolves.toEqual({"didDocument": null, "didDocumentMetadata": {}, "didResolutionMetadata": {"contentType": "application/did+ld+json", "error": "invalidDid", "message": "TypeError: Cannot read property 'keyToDidDoc' of undefined"}})
})

it('expect index.js to throw an error for an unsupported media type', async () => {
const resolverRegistry = index.getResolver()
expect(resolverRegistry).not.toBeUndefined()
Expand Down

0 comments on commit 3305f52

Please sign in to comment.