achingbrain / mortice

Isomorphic read/write lock that works in single processes, node clusters and web workers
Other
8 stars 6 forks source link

bug: does not work in service workers #67

Open SgtPooki opened 1 year ago

SgtPooki commented 1 year ago

When testing some code for ipfs-shipyard/helia-service-worker-gateway, we saw the following error:

heliaFetch.ts:106 fs.stat error for cid 'QmbSa3mkSjwTX32feHXC9yMZ6TVH2JzJdR5sHyzssJueLB' and path '/' TypeError: globalThis.postMessage is not a function
    at Object.readLock (browser.ts:67:16)
    at storage.ts:108:41
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (pins.ts:189:4)
    at _next (pins.ts:189:4)
    at pins.ts:189:4
    at new Promise (<anonymous>)
    at pins.ts:189:4
    at BlockStorage.get (storage.ts:125:6)
    at unixFsResolver (index.ts:32:34)

coming from https://github.com/achingbrain/mortice/blob/0eab826e744c8bc23ac65d17ac3a44e5f4694a3d/src/browser.ts#L67

We should move away from postMessage to BroadcastChannel and MessageChannel which should be supported in all browser contexts and node after v15.

talentlessguy commented 1 year ago

because of it, this also errors on Deno cuz globalThis.postMessage is undefined there