|
| 1 | +import {createMiniOxygen} from '@shopify/mini-oxygen'; |
| 2 | + |
| 3 | +/** |
| 4 | + * This is script is a simple runner for our example app. This script will run |
| 5 | + * the compiled example on a local worker implementation to emulate a Oxygen worker runtime. |
| 6 | + * |
| 7 | + * For the actual example implementation, see the src/index.ts file. |
| 8 | + */ |
| 9 | + |
| 10 | +const printValueAndBanner = (flagKey, flagValue) => { |
| 11 | + console.log(`*** The '${flagKey}' feature flag evaluates to ${flagValue}.`); |
| 12 | + |
| 13 | + if (flagValue) { |
| 14 | + console.log( |
| 15 | + ` ██ |
| 16 | + ██ |
| 17 | + ████████ |
| 18 | + ███████ |
| 19 | + ██ LAUNCHDARKLY █ |
| 20 | + ███████ |
| 21 | + ████████ |
| 22 | + ██ |
| 23 | + ██ |
| 24 | + `, |
| 25 | + ); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +const main = async () => { |
| 30 | + // NOTE: you will see logging coming from mini-oxygen's default request hook. |
| 31 | + // https://github.com/Shopify/hydrogen/blob/5a38948133766e358c5f357f52562f6fdcfe7969/packages/mini-oxygen/src/worker/index.ts#L225 |
| 32 | + const miniOxygen = createMiniOxygen({ |
| 33 | + debug: false, |
| 34 | + workers: [ |
| 35 | + { |
| 36 | + name: 'main', |
| 37 | + modules: true, |
| 38 | + scriptPath: 'dist/index.js' |
| 39 | + }, |
| 40 | + ], |
| 41 | + }); |
| 42 | + |
| 43 | + miniOxygen.ready.then(() => { |
| 44 | + console.log('Oxygen worker is started...'); |
| 45 | + console.log('Dispatching fetch every 5 seconds. Press "q" or Ctrl+C to quit...'); |
| 46 | + |
| 47 | + // Dispatch fetch every 5 seconds |
| 48 | + const interval = setInterval(() => { |
| 49 | + // NOTE: This is a bogus URL and will not be used in the actual fetch handler. |
| 50 | + // please see the src/index.ts file for the actual fetch handler. |
| 51 | + miniOxygen.dispatchFetch('https://localhost:8000') |
| 52 | + .then(d => d.json()) |
| 53 | + .then(({flagValue, flagKey}) => { |
| 54 | + console.clear(); |
| 55 | + printValueAndBanner(flagKey, flagValue); |
| 56 | + console.log('Press "q" or Ctrl+C to quit...') |
| 57 | + }).catch((err) => { |
| 58 | + console.log('Error dispatching fetch:', err.message); |
| 59 | + console.log('Press "q" or Ctrl+C to quit...') |
| 60 | + }); |
| 61 | + }, 1000); |
| 62 | + |
| 63 | + // Handle keypresses for cleanup |
| 64 | + process.stdin.setRawMode(true); |
| 65 | + process.stdin.resume(); |
| 66 | + process.stdin.setEncoding('utf8'); |
| 67 | + |
| 68 | + process.stdin.on('data', async (key) => { |
| 69 | + // Handle Ctrl+C |
| 70 | + if (key === '\u0003') { |
| 71 | + clearInterval(interval); |
| 72 | + await miniOxygen.dispose(); |
| 73 | + process.exit(); |
| 74 | + } |
| 75 | + |
| 76 | + // Handle 'q' key |
| 77 | + if (key === 'q' || key === 'Q') { |
| 78 | + clearInterval(interval); |
| 79 | + await miniOxygen.dispose(); |
| 80 | + process.exit(); |
| 81 | + } |
| 82 | + }); |
| 83 | + }); |
| 84 | +} |
| 85 | + |
| 86 | +main().catch(console.error); |
0 commit comments