|
| 1 | +// Flags: --experimental-raw-imports |
| 2 | +import '../common/index.mjs'; |
| 3 | +import assert from 'assert'; |
| 4 | +import { Buffer } from 'buffer'; |
| 5 | + |
| 6 | +import staticText from '../fixtures/file-to-read-without-bom.txt' with { type: 'text' }; |
| 7 | +import staticTextWithBOM from '../fixtures/file-to-read-with-bom.txt' with { type: 'text' }; |
| 8 | +import staticBytes from '../fixtures/file-to-read-without-bom.txt' with { type: 'bytes' }; |
| 9 | + |
| 10 | +const expectedText = 'abc\ndef\nghi\n'; |
| 11 | +const expectedBytes = Buffer.from(expectedText); |
| 12 | + |
| 13 | +assert.strictEqual(staticText, expectedText); |
| 14 | +assert.strictEqual(staticTextWithBOM, expectedText); |
| 15 | + |
| 16 | +assert.ok(staticBytes instanceof Uint8Array); |
| 17 | +assert.deepStrictEqual(Buffer.from(staticBytes), expectedBytes); |
| 18 | + |
| 19 | +const dynamicText = await import('../fixtures/file-to-read-without-bom.txt', { |
| 20 | + with: { type: 'text' }, |
| 21 | +}); |
| 22 | +assert.strictEqual(dynamicText.default, expectedText); |
| 23 | + |
| 24 | +const dynamicBytes = await import('../fixtures/file-to-read-without-bom.txt', { |
| 25 | + with: { type: 'bytes' }, |
| 26 | +}); |
| 27 | +assert.deepStrictEqual(Buffer.from(dynamicBytes.default), expectedBytes); |
| 28 | + |
| 29 | +const dataText = await import('data:text/plain,hello%20world', { |
| 30 | + with: { type: 'text' }, |
| 31 | +}); |
| 32 | +assert.strictEqual(dataText.default, 'hello world'); |
| 33 | + |
| 34 | +const dataBytes = await import('data:text/plain,hello%20world', { |
| 35 | + with: { type: 'bytes' }, |
| 36 | +}); |
| 37 | +assert.strictEqual(Buffer.from(dataBytes.default).toString(), 'hello world'); |
| 38 | + |
| 39 | +const dataJsAsText = await import('data:text/javascript,export{}', { |
| 40 | + with: { type: 'text' }, |
| 41 | +}); |
| 42 | +assert.strictEqual(dataJsAsText.default, 'export{}'); |
| 43 | + |
| 44 | +const dataJsAsBytes = await import('data:text/javascript,export{}', { |
| 45 | + with: { type: 'bytes' }, |
| 46 | +}); |
| 47 | +assert.strictEqual(Buffer.from(dataJsAsBytes.default).toString(), 'export{}'); |
| 48 | + |
| 49 | +const dataInvalidUtf8 = await import('data:text/plain,%66%6f%80%6f', { |
| 50 | + with: { type: 'text' }, |
| 51 | +}); |
| 52 | +assert.strictEqual(dataInvalidUtf8.default, 'fo\ufffdo'); |
| 53 | + |
| 54 | +const jsAsText = await import('../fixtures/syntax/bad_syntax.js', { |
| 55 | + with: { type: 'text' }, |
| 56 | +}); |
| 57 | +assert.match(jsAsText.default, /^var foo bar;/); |
| 58 | + |
| 59 | +const jsAsBytes = await import('../fixtures/syntax/bad_syntax.js', { |
| 60 | + with: { type: 'bytes' }, |
| 61 | +}); |
| 62 | +assert.match(Buffer.from(jsAsBytes.default).toString(), /^var foo bar;/); |
| 63 | + |
| 64 | +const jsonAsText = await import('../fixtures/invalid.json', { |
| 65 | + with: { type: 'text' }, |
| 66 | +}); |
| 67 | +assert.match(jsonAsText.default, /"im broken"/); |
| 68 | + |
| 69 | +await assert.rejects( |
| 70 | + import('data:text/plain,hello%20world'), |
| 71 | + { code: 'ERR_UNKNOWN_MODULE_FORMAT' }, |
| 72 | +); |
| 73 | + |
| 74 | +await assert.rejects( |
| 75 | + import('../fixtures/file-to-read-without-bom.txt'), |
| 76 | + { code: 'ERR_UNKNOWN_FILE_EXTENSION' }, |
| 77 | +); |
0 commit comments