Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Cloudflare Snippets (alpha) を使う

Posted at

2023年9月22日時点の情報です。
alpha のため、諸々の変更の可能性はあります。

Cloudflare Snippets

Cloudflare Snippetsで最もプログラム可能なSupercloudを実現

  • JavaScript(モダン)、VCL(レガシー)、Apache .htaccessファイル(レガシー)など、様々な言語で書かれたコードのインポートをサポート
  • Workers なしで、軽量な Workers 処理を柔軟に書ける
  • すべてのプランレベルで追加料金なしで利用できる

アルファプログラムに参加したい方は、以下の waitlist にご参加ください。

動作

Cloudflare Snippets (alpha) · Cloudflare Rules docs

  • Snippet Rule に対して Snippet を紐付ける
  • 複数ルールマッチすれば、同じリクエストに対して Snippet が複数回、順次実行される
  • 最大実行時間は5msで最大メモリは2MB、パッケージの合計サイズは32KB
  • Origin Rules > Cache Rules > Configuration Rules > Single Redirects > Bulk Redirects > Snippets の順で実行

Snippet 作成

以下の手順で作成できます。

image.png

image.png

image.png

image.png

image.png

デフォルト Snippet サンプル

まさに Workers の手前で動く Workers という形ですが、
Snippet Rule によって複数フィールドを使ったマッチ実行条件を設定できたりと、ルール群ならではの面もあります。

(Workers は カスタムドメインやルートトリガー等のホスト名条件で起動してしまう)

sample_snippet.js
// Enter Snippet code bellow

export default {
    async fetch(request) {
        const response = await fetch(request);

        // Clone the response so that it's no longer immutable
        const newResponse = new Response(response.body, response);

        // Add a custom header with a value
        newResponse.headers.append(
            "x-snippets-hello",
            "Hello from Cloudflare Snippets"
        );

        // Delete headers
        newResponse.headers.delete("x-header-to-delete");
        newResponse.headers.delete("x-header2-to-delete");

        // Adjust the value for an existing header
        newResponse.headers.set("x-header-to-change", "NewValue");
        return new Response('Welcome to Cloudflare Snippets!');
    },
};

動作確認

デフォルトのサンプルコードだと、最後の行だけが実質有効なので、以下の結果となります。

% curl -s https://snippet.example.com/     
Welcome to Cloudflare Snippets!

Google Cloud Storage のプライベートオブジェクトにアクセス

以下の記事を参考にして、認証ヘッダをつける処理が Snippet で動くかを確認します。

環境変数が使えないため、以下のパラメータを直打ちします。

  • バケット名 YOUR_GCS_BUCKET_NAME
  • リージョン asia-northeast1
  • accessKeyId YOUR_GCS_ACCESS_KEY_ID
  • secretAccessKey YOUR_GCS_SECRET_ACCESS_KEY

今後は Cloudflare Secrets Store にパラメータを保管して Snippet から利用できることが期待されます。

また、外部ライブラリの import が使えないため、ネイティブで書ける範囲で処理を行う必要があります。

上記 Snippet を適用した上で、以下のようにアクセスできることが確認できました。

image.png

まとめ

Workers のようで Workers でない Snippets でしたが、この程度の処理なら Snippets を使っていけそうです。

詳細なログの確認や Secrets Store との連携は今後に期待です。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?