Skip to content

Commit

Permalink
Add temp fix for blobref validation issue (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
devinivy committed May 19, 2023
1 parent 8d5c1b1 commit 9395b18
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/util/subscription.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Subscription } from '@atproto/xrpc-server'
import { cborToLexRecord, readCar } from '@atproto/repo'
import { BlobRef } from '@atproto/lexicon'
import { ids, lexicons } from '../lexicon/lexicons'
import { Record as PostRecord } from '../lexicon/types/app/bsky/feed/post'
import { Record as RepostRecord } from '../lexicon/types/app/bsky/feed/repost'
Expand Down Expand Up @@ -156,9 +157,29 @@ export const isFollow = (obj: unknown): obj is FollowRecord => {

const isType = (obj: unknown, nsid: string) => {
try {
lexicons.assertValidRecord(nsid, obj)
lexicons.assertValidRecord(nsid, fixBlobRefs(obj))
return true
} catch (err) {
return false
}
}

// @TODO right now record validation fails on BlobRefs
// simply because multiple packages have their own copy
// of the BlobRef class, causing instanceof checks to fail.
// This is a temporary solution.
const fixBlobRefs = (obj: unknown): unknown => {
if (Array.isArray(obj)) {
return obj.map(fixBlobRefs)
}
if (obj && typeof obj === 'object') {
if (obj.constructor.name === 'BlobRef') {
const blob = obj as BlobRef
return new BlobRef(blob.ref, blob.mimeType, blob.size, blob.original)
}
return Object.entries(obj).reduce((acc, [key, val]) => {
return Object.assign(acc, { [key]: fixBlobRefs(val) })
}, {} as Record<string, unknown>)
}
return obj
}

0 comments on commit 9395b18

Please sign in to comment.