Skip to content

Commit

Permalink
feat: add memes exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Shaw committed Nov 15, 2023
1 parent 85d0445 commit 4e7150e
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 1 deletion.
3 changes: 2 additions & 1 deletion bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as SpaceOutProblem from './problems/1-space-out/index.js'
import * as CatGifsProblem from './problems/2-cat-gifs/index.js'
import * as DelegationProblem from './problems/3-delegation/index.js'
import * as InfiniteCompressionProblem from './problems/4-infinite-compression/index.js'
import * as MemesProblem from './problems/5-memes/index.js'
// import * as PlaceholderProblem from './problems/placeholder/index.js'

const shop = adventure({
Expand All @@ -18,7 +19,7 @@ shop.add('Lets space out', () => SpaceOutProblem)
shop.add('Storing cat gifs for fun and gossip', () => CatGifsProblem)
shop.add('Delegation, invocation and procrastination', () => DelegationProblem)
shop.add('Infinite avatar compression!', () => InfiniteCompressionProblem)
// shop.add('Memes for my eyes', () => PlaceholderProblem)
shop.add('Memes for my eyes', () => MemesProblem)
// shop.add('DUDE, WHERE\'S MY CAR?', () => PlaceholderProblem)
// shop.add('CommP at the edge or die trying', () => PlaceholderProblem)
// shop.add('Revocation station', () => PlaceholderProblem)
Expand Down
7 changes: 7 additions & 0 deletions problems/5-memes/cat.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/\_____/\
/ o o \
( == ^ == )
) (
( )
( ( ) ( ) )
(__(__)___(__)__)
35 changes: 35 additions & 0 deletions problems/5-memes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import fs from 'node:fs'
import path from 'node:path'
import { Buffer } from 'node:buffer'
import { execaNode } from 'execa'
import msee from 'msee'
import * as Link from 'multiformats/link'

const cat = Link.parse('bafkreicn5krrora5gx47hrbj6kbfwrrskh7iawngpdudmzqp72i6jaalyu')
const catData = fs.readFileSync(new URL('./cat.txt', import.meta.url), 'utf8')

export const problem = msee.parse(fs.readFileSync(new URL('./problem.md', import.meta.url), 'utf8'))

export const solution = msee.parse(fs.readFileSync(new URL('./solution.md', import.meta.url), 'utf8'))

/**
* @param {string[]} args
* @param {(success: boolean) => void} cb
*/
export const verify = (args, cb) => {
(async () => {
const filepath = path.resolve(args[0])
console.log(`Verifying ${filepath}...\n`)
const { stdout, all } = await execaNode(filepath, [], {
input: Buffer.from(cat.bytes),
all: true
})
console.log(all ?? '')

if (stdout !== catData) {
return cb(false)
}

cb(true)
})()
}
22 changes: 22 additions & 0 deletions problems/5-memes/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Memes for my eyes

Any meme you upload is almost instantly available for retrieval. Other files also. Content is made available by root CID and by CAR shard CID. In this exercise you are going to fetch a text file from the public gateway by it's root CID and log it to the console.

You can use the `w3s.link` gateway to retrieve the data but the magic of decentralized peer to peer networking means that you should be able to use any gateway.

To retrieve data from a gateway you should format the request like:

```
https://w3s.link/ipfs/<CID>
```

The CID of the data to fetch will be provided to `process.stdin`.

You should use the `multiformats` library to `decode` the CID from it's compact binary form and then format it as a string in the URL.

Finally send a HTTP request to fetch the content and write the data directly to `process.stdout`.

─────────────────────────────────────────────────────────────────────────────
* To print these instructions again, run: `$ADVENTURE_NAME print`
* To verify your program, run: `$ADVENTURE_NAME verify program.js`
* For help run: `$ADVENTURE_NAME help`
6 changes: 6 additions & 0 deletions problems/5-memes/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import fs from 'node:fs'
import { CID } from 'multiformats'

const cid = CID.decode(fs.readFileSync(process.stdin.fd))
const res = await fetch(`https://w3s.link/ipfs/${cid}`)
process.stdout.write(await res.text())
14 changes: 14 additions & 0 deletions problems/5-memes/solution.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Here's the official solution in case you want to compare notes:

```js
import fs from 'node:fs'
import { CID } from 'multiformats'

const cid = CID.decode(fs.readFileSync(process.stdin.fd))
const res = await fetch(`https://w3s.link/ipfs/${cid}`)
process.stdout.write(await res.text())
```

# YOU DID IT!

Type `$ADVENTURE_NAME` to show the menu.

0 comments on commit 4e7150e

Please sign in to comment.