把 Blog 上的 PNG 圖片換成 WebP 格式

WebP 格式的大小比起 JPEG 或是 PNG 都小不少,支援度也都還行,但 Safari 不支援是個大問題,因為在行動裝置裡面 iOS 還是大宗...

目前想到的方法是只對 Imgur 的圖片使用 WebP (.webp),當遇到不支援的 WebP 的平台時透過 JavaScript 改用 PNG (.png)。

這邊有判斷有沒有支援 WebP 的程式碼出自「Detect WEBP Support with JavaScript」,用 createImageBitmap() 建看看有沒有成功:

(() => {
  let supportsWebP = async () => {
    if (!self.createImageBitmap) return false;
    const webpData = 'data:image/webp;base64,UklGRh4AAABXRUJQVlA4TBEAAAAvAAAAAAfQ//73v/+BiOh/AAA=';
    const blob = await fetch(webpData).then(r => r.blob());
    return createImageBitmap(blob).then(() => true, () => false);
  };

  (async () => {
    if (!await supportsWebP()) {
      document.addEventListener('DOMContentLoaded', () => {
        for (let el of document.getElementsByTagName('img')) {
          let src = el.getAttribute('src');
          if (src.match(/\.webp/)) {
            el.setAttribute('src', src.replace(/\.webp/, '.png'));
          }
        }
      });
    }
  })();
})();

這邊比較有趣的是網路上的文件 (MDNCanIuse) 都說 Safari 不支援 createImageBitmap(),但實際上好像沒問題 :o

然後再用 WordPress 的延伸套件「Search Regex」把所有文章理出現 /https:\/\/i\.imgur\.com/(\w+)\.png/ 的字串換成 https://i.gslin.com/imgur/$1.webp,接下來就可以拿 Safari 測試了,這樣有點 hack 但看起來還行...

加快 ls 的速度

看到「When setting an environment variable gives you a 40x speedup」這篇在講 ls 的速度。

文章是由 StanfordSherlock 發出來的,不過看起來跟電視劇沒關係,從網站上的標語「The HPC cluster for all your computing needs」可以看出是 HPC 相關的單位。

在 HPC 環境裡面可以預期單一目錄裡會有很多檔案,所以使用者跑來抱怨 ls 的速度就不算太意外了。不過這次使用者有提到在他自己的 laptop 上跑 ls 反而很快:

It all started from a support question, from a user reporting a usability problem with ls taking several minutes to list the contents of a 15,000+ entries directory on $SCRATCH.

Having thousands of files in a single directory is usually not very file system-friendly, and definitely not recommended. The user knew this already and admitted that wasn’t great, but when he mentioned his laptop was 1,000x faster than Sherlock to list this directory’s contents, of course, it stung. So we looked deeper.

直接跳到後面的結論... 原因是出自於因為需要顯示不同顏色,而需要透過 lstat() 查詢額外的檔案性質 (可執行、setuid 以及 setgid 這些資料),導致速度變慢:

From 13s with the default settings, to 0.3s with a small LS_COLORS tweak, that’s a 40x speedup right there, for the cheap price of not having setuid/setgid or executable files colorized differently.

Of course, this is now setup on Sherlock, for every user’s benefit.

透過設定 LS_COLORS='ex=00:su=00:sg=00:ca=00:',可以讓 lstat() 消失,所以被放進 Sherlock 的預設值了... 而沒有遇到這個問題的環境 (像是有設計好對應的目錄結構),或是想要維持原來的樣子的人,則可以 unset 掉這個值讓輸出還是有色彩差異 :o

PostgreSQL 的 Don't Do This

Hacker News Daily 上看到的資料,整理了 PostgreSQL 上不要使用的功能:「Don't Do This」,而且是放在官方網域 wiki.postgresql.org 上。

裡面這些想法不知道出處是哪邊... 有不少功能算是 PostgreSQL 特有的功能 (以 open source RDBMS 這個領域來看),而且大概也還想的到用的場景,你卻在上面叫大家不要用,再寫的時候大概是吸了一批很純的,已經不知道要從哪邊開始吐槽...

要看的話連同 Hacker News 上的留言一起看會比較有前因後果:「https://news.ycombinator.com/item?id=19817531」。

Pornhub 想買 Tumblr?

看到 Pornhub 想要買 Tumblr 的新聞:「Pornhub wants to buy Tumblr and restore site to former porn-filled glory」。

如果是其他家買可能還沒感覺,但如果是 Pornhub 買的話真的有機會恢復往日榮光的感覺?當然本來已經被迫離開的那些人應該是不會回來...

針對 JavaScript 時代調整網頁的效能評估指標

早期網頁的效能評估指標都沒有考慮 JavaScript 的情況,大多都是 TTFB (Time to First Byte) 或是網頁大小以及 DOMContentLoaded 或是 load 這類 DOM event 為主,但因為 Goodhart's law,現代的網頁設計會故意將許多 JavaScript 要做的事情搬到 load 以後開始做,以降低 load 被延遲的問題,讓前端的「KPI」比較好看:

When a measure becomes a target, it ceases to be a good measure.

但在 load 之後整個網站還是不能用,使用者的體驗其實很差,這個評估方式的價值變低不少。所以「Measuring Jank and UX」這篇就再找出一些新的指標,來評估 JavaScript 造成的問題。

可以看到文章裡面評估了很多關於 CPU loading 與操作時間的指標,也許這一兩年還會有用,不過我覺得還是會遇到 Goodhart's law 描述的問題... XD

Amazon S3 要拿掉 Path-style 存取方式

Hacker News 上翻的時候翻到的公告:「Announcement: Amazon S3 will no longer support path-style API requests starting September 30th, 2020」。

現有的兩種方法,一種是把 bucket name 放在 path (V1),另外一種是把 bucket name 放在 hostname (V2):

Amazon S3 currently supports two request URI styles in all regions: path-style (also known as V1) that includes bucket name in the path of the URI (example: //s3.amazonaws.com/<bucketname>/key), and virtual-hosted style (also known as V2) which uses the bucket name as part of the domain name (example: //<bucketname>.s3.amazonaws.com/key).

這次要淘汰的是 V1 的方式,預定在 2020 年十月停止服務 (服務到九月底):

Customers should update their applications to use the virtual-hosted style request format when making S3 API requests before September 30th, 2020 to avoid any service disruptions. Customers using the AWS SDK can upgrade to the most recent version of the SDK to ensure their applications are using the virtual-hosted style request format.

Virtual-hosted style requests are supported for all S3 endpoints in all AWS regions. S3 will stop accepting requests made using the path-style request format in all regions starting September 30th, 2020. Any requests using the path-style request format made after this time will fail.

詩篇的作者抱怨不知道自己詩篇考題的答案...

2017 年的文章,最近不知道為什麼冒出來,但還蠻有趣的...

看到「I Can’t Answer These Texas Standardized Test Questions About My Own Poems」這篇,Sara Holbrook 收到信件跑來問問題 (節錄前面的部份):

Hello Mrs. Holbrook. My name is Sean, and I’m an 8th grade English teacher in Texas. I’m attempting to decipher the number of stanzas in your poem, ‘Midnight’. This isn’t clear from the formatting in our most recent benchmark. The assessment asks the following question:

作者最後的抱怨也很有趣:

My final reflection is this: any test that questions the motivations of the author without asking the author is a big baloney sandwich. Mostly test makers do this to dead people who can’t protest. But I’m not dead.

I protest.

這邊其實也是在偷戳「作者之死」現象... 另外一則也有類似的情況,發生的早一點的台灣 (2016) XDDD

文學的過度解讀現象 XDDD

SpaceX 得到 FCC 的同意架設家用的衛星網路

在「FCC approves SpaceX’s plans to fly internet-beaming satellites in a lower orbit」這邊看到的:

The Federal Communications Commission has approved SpaceX’s request to fly a large swath of its future internet-beaming satellites at a lower orbit than originally planned.

預定打四千顆低軌道衛星上去:

Under SpaceX’s original agreement with the commission, the company had permission to launch 4,425 Starlink satellites into orbits that ranged between 1,110 to 1,325 kilometers up. But then SpaceX decided it wanted to fly 1,584 of those satellites in different orbits, thanks to what it had learned from its first two test satellites, TinTin A and B. Instead of flying them at 1,150 kilometers, the company now wants to fly them much lower at 550 kilometers.

不知道價位會落在那個區塊... 如果價位夠低的話,也許是可以考慮當作偏鄉地區的通訊方案?至少是個備用方式...

從 Homebrew 換用 MacPorts...

看到「Thoughts on macOS Package Managers」這篇的介紹,文章作者在裡面提到從 Homebrew 換用 MacPorts 的幾個原因...

裡面有提到 root 權限的問題 (Homebrew 的 workaround),以及軟體豐富度的問題 (常用的應該都有,這邊的差異會是冷門一些的軟體)。不過這些大概都是之前都已經知道的... 比較新的是目前維護者在 integrity 上的問題:

用「How to uninstall Homebrew?」這邊的方法移除 Homebrew 後,再去 MacPorts 官網上下載檔案安裝,跑一陣子看看會不會有什麼問題...

目前看起來比較大的問題都是出自 /opt/local/{bin,sbin} 架構,這個可以靠在 /etc/profile 裡面設定解決 (不是很愛這個方法,但至少這樣會動...)。