File
Blob: samples/nodejs-compat-fs/worker.js
| 1 | import { readFileSync, writeFileSync, existsSync } from 'node:fs'; |
| 2 | import { ok, strictEqual } from 'node:assert'; |
| 3 | |
| 4 | // File system operations at the top level scope are allowed |
| 5 | const config = readFileSync('/bundle/config', 'utf8'); |
| 6 | strictEqual(config, 'config file'); |
| 7 | |
| 8 | export default { |
| 9 | async fetch(request) { |
| 10 | // All files in the /tmp directory are always per-request. |
| 11 | // When the request ends, the files are deleted. |
| 12 | ok(!existsSync('/tmp/hello.txt')); |
| 13 | writeFileSync('/tmp/hello.txt', 'Hello, World!', 'utf8'); |
| 14 | return new Response(readFileSync('/tmp/hello.txt')); |
| 15 | } |
| 16 | }; |