Skip to content

Instantly share code, notes, and snippets.

View alanshaw's full-sized avatar
🫀
https://web3.storage

Alan Shaw alanshaw

🫀
https://web3.storage
View GitHub Profile
@alanshaw
alanshaw / react-uploader.jsx
Last active September 28, 2022 20:36
Simplified examples for the W3UI website
import { useState } from 'react'
import { useUploader } from '@w3ui/react-uploader'
export default function Component () {
const { uploader } = useUploader()
const [file, setFile] = useState(null)
const [cid, setCid] = useState('')
const handleUploadSubmit = async e => {
e.preventDefault()
@alanshaw
alanshaw / linkdex.md
Created August 24, 2022 07:50
Linkdex idea

Linkdex

An index mapping block CID to linked block CID.

Characteristics

  • Small - just the link blocks - no data for raw leaves. The majority of files are small files, so the DAGs are not super deep.
  • Root CID independent - it doesn’t matter what the root CID is, we can just iteratively decode each block in a CAR and index the links it has

What is it useful for?

@alanshaw
alanshaw / sns2s3.js
Created June 10, 2022 13:44
SNSEvent to S3Event
/**
* Extract an S3Event from the passed SNSEvent.
* @param {import('aws-lambda').SNSEvent} snsEvent
* @returns {import('aws-lambda').S3Event}
*/
function toS3Event (snsEvent) {
const s3Event = { Records: [] }
for (const snsRec of snsEvent.Records) {
try {
for (const s3Rec of JSON.parse(snsRec.Sns.Message).Records || []) {
@alanshaw
alanshaw / filter.txt
Created June 7, 2022 14:16
Filter github PRs you need to review
review-requested:alanshaw
// Lambda index CAR ////////////////////////////////////////////////////////////
const blocks = Set<CID>()
const links = Set<CID>()
const car = readCar(path)
for (const block in car) {
blocks.add(block.cid)
getLinks(block).forEach(cid => links.add(cid))
}
@alanshaw
alanshaw / custom-bs-path.mjs
Last active April 19, 2022 15:03
Store to NFT.Storage using a custom blockstore path
import { NFTStorage } from 'nft.storage'
import { getFilesFromPath } from 'files-from-path'
import { FsBlockStore as Blockstore } from 'ipfs-car/blockstore/fs'
import Path from 'path'
const token = 'YOUR_API_TOKEN'
// ///////////////////////////////////////////////////////////////////////////
// Save DAG to a custom location by changing this path...
const bsPath = './blockstore'
@alanshaw
alanshaw / setulimit.sh
Created March 22, 2022 17:25
ulimit maxfiles on MacOS Monterey
sudo sysctl kern.maxfilesperproc=256
@alanshaw
alanshaw / sample.mjs
Created March 7, 2022 12:04
Sample dotStorage CIDs
/**
* Create IPFS unixfs directories of random CIDs from the data source. CIDs are
* grouped by DAG size:
* 0-1MB
* 1MB-5MB
* 5MB-25MB
* 25MB-100MB
* 100MB-500MB
* 500MB-1GB
* 1GB-10GB
import path from 'path'
type ImportCandidate = x|y|z|{ path: string, content: x|y|z }
type UnixFsEntry = { path: string, content: AsyncIterable<Uint8Array> }
// extends Blob?
class Web3File extends Blob {
constructor (filename: string, content: AsyncIterable<Uint8Array>, options?: Web3FileOpts)
// Generate from content?
const deadline = async function * (source, deadineMs, onDeadline) {
class DeadlineError extends Error {}
while (true) {
let timeoutID
try {
const next = await Promise.race([
new Promise((resolve, reject) => {
timeoutID = setTimeout(() => reject(new DeadlineError()), deadineMs)
}),
source.next()