Code Monkey home page Code Monkey logo

link-preview's Introduction

link-preview

What is this?

This is a light-weight, no-auth "link-preview" service powered by Cloudflare Workers and node-html-parser

  • For small- or single-purpose services, Cloudflare Workers are very good environment to work with.
    • It enables small-start from free tier, with generous feature offerings
    • It allows us exactly what we want in this context: writing in TypeScript, serving public HTTP APIs to the world
    • It has well-made development experience: rich documents, wrangler CLI, easy deploy from GitHub Actions, consice user-land directory structure, etc.
  • node-html-parser fits here as well.
    • Reasonably small set of dependencies
    • Sufficiently fast (subjectively though, not benchmarked)
    • Well-known querySelector API

Why a Service, not a Library?

There are libraries like link-preview-js and they serve the purpose as long as you know what you are doing.

However, there are gotchas:

  • When you attempt to fetch a website of another origin from JavaScript run on a web browser, the browser ask the targeted website for cross-origin request allowances (CORS; Cross-Origin Resource Sharing) due to the same-origin policies
  • However, not all websites allow CORS. Or rather, most websites just don't (default behavior of ordinary web servers)
  • In this scenario, non-browser agent must fetch the website, and pass the result to the initiating script
  • This service exactly does that
    • Fetch websites of another domain on behalf of browser-run script
    • Extract essential info for preview purpose from the website (title and url, optional description and image)
    • Give it back to the requester in JSON format

This service itself allows CORS, so you can just pitch requests from whatever environment and they should work.

Also it comes in rescue when your preferred language does not have well-maintained link-preview libraries. You just need some HTTP client capability and JSON handling.

How to deploy

  1. Prepare your Cloudflare Account
  2. Click: Deploy to Cloudflare Workers
    • It should fork this repository and set up your Cloudflare Account, then deploys the service

Develop locally

npm install
npm start

Then, from another terminal:

$ curl 'http://localhost:8787?q=https://cloudflare.com' | jq .
{
  "title": "Cloudflare - The Web Performance & Security Company",
  "description": "Here at Cloudflare, we make the Internet work the way it should. Offering CDN, DNS, DDoS protection and security, find out how we can help your site.",
  "url": "https://www.cloudflare.com/",
  "image": "https://www.cloudflare.com/static/b30a57477bde900ba55c0b5f98c4e524/Cloudflare_default_OG_.png"
}

link-preview's People

Contributors

ymtszw avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.