-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathipns-record.spec.ts
92 lines (79 loc) · 2.66 KB
/
ipns-record.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { dagCbor } from '@helia/dag-cbor'
import { ipns } from '@helia/ipns'
import { generateKeyPair } from '@libp2p/crypto/keys'
import { stop } from '@libp2p/interface'
import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { expect } from 'aegir/chai'
import { marshalIPNSRecord, unmarshalIPNSRecord } from 'ipns'
import { VerifiedFetch } from '../src/verified-fetch.js'
import { createHelia } from './fixtures/create-offline-helia.js'
import type { Helia } from '@helia/interface'
import type { IPNS } from '@helia/ipns'
describe('ipns records', () => {
let helia: Helia
let name: IPNS
let verifiedFetch: VerifiedFetch
beforeEach(async () => {
helia = await createHelia()
name = ipns(helia)
verifiedFetch = new VerifiedFetch({
helia
})
})
afterEach(async () => {
await stop(helia, verifiedFetch)
})
it('should support fetching a raw IPNS record', async () => {
const obj = {
hello: 'world'
}
const c = dagCbor(helia)
const cid = await c.add(obj)
const key = await generateKeyPair('Ed25519')
const peerId = peerIdFromPrivateKey(key)
const record = await name.publish(key, cid)
const resp = await verifiedFetch.fetch(`ipns://${peerId}`, {
headers: {
accept: 'application/vnd.ipfs.ipns-record'
}
})
expect(resp.status).to.equal(200)
expect(resp.headers.get('content-type')).to.equal('application/vnd.ipfs.ipns-record')
const buf = new Uint8Array(await resp.arrayBuffer())
expect(marshalIPNSRecord(record)).to.equalBytes(buf)
const output = unmarshalIPNSRecord(buf)
expect(output.value).to.deep.equal(`/ipfs/${cid}`)
})
it('should reject a request for non-IPNS url', async () => {
const resp = await verifiedFetch.fetch('ipfs://QmbxpRxwKXxnJQjnPqm1kzDJSJ8YgkLxH23mcZURwPHjGv', {
headers: {
accept: 'application/vnd.ipfs.ipns-record'
}
})
expect(resp.status).to.equal(400)
})
it('should reject a request for a DNSLink url', async () => {
const resp = await verifiedFetch.fetch('ipns://ipfs.io', {
headers: {
accept: 'application/vnd.ipfs.ipns-record'
}
})
expect(resp.status).to.equal(400)
})
it('should reject a request for a url with a path component', async () => {
const obj = {
hello: 'world'
}
const c = dagCbor(helia)
const cid = await c.add(obj)
const key = await generateKeyPair('Ed25519')
const peerId = peerIdFromPrivateKey(key)
await name.publish(key, cid)
const resp = await verifiedFetch.fetch(`ipns://${peerId}/hello`, {
headers: {
accept: 'application/vnd.ipfs.ipns-record'
}
})
expect(resp.status).to.equal(400)
})
})