diff --git a/.github/workflows/deno.yml b/.github/workflows/deno.yml new file mode 100644 index 0000000..607d4f0 --- /dev/null +++ b/.github/workflows/deno.yml @@ -0,0 +1,62 @@ +name: Deno + +on: + push: + branches: ["main"] + tags: + - '*' + pull_request: + branches: ["main"] + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - name: Setup repo + uses: actions/checkout@v3 + + - name: Setup Deno + # uses: denoland/setup-deno@v1 + uses: denoland/setup-deno@61fe2df320078202e33d7d5ad347e7dcfa0e8f31 # v1.1.2 + with: + deno-version: v1.x + + - name: Verify formatting + run: deno fmt --check + + - name: Run linter + run: deno lint + + - name: Run tests + run: deno test -A + + npm_publish: + name: Publish to npm + needs: test + if: startsWith(github.ref, 'refs/tags/') + runs-on: ubuntu-latest + steps: + - name: Get tag version + id: get_tag_version + run: echo TAG_VERSION=${GITHUB_REF/refs\/tags\//} >> $GITHUB_OUTPUT + + - name: Setup Deno + # uses: denoland/setup-deno@v1 + uses: denoland/setup-deno@61fe2df320078202e33d7d5ad347e7dcfa0e8f31 # v1.1.2 + with: + deno-version: v1.x + + - name: Setup Node + uses: actions/setup-node@v3 + with: + node-version: '18.x' + registry-url: 'https://registry.npmjs.org' + + - name: npm build + run: deno task build ${{steps.get_tag_version.outputs.TAG_VERSION}} + + - name: npm publish + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} + run: cd npm && npm publish diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..50d08d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +npm \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..fdbe9bd --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "deno.enable": true, + "editor.defaultFormatter": "denoland.vscode-deno" +} diff --git a/README.md b/README.md index a8d2ddd..564f727 100644 --- a/README.md +++ b/README.md @@ -1 +1,43 @@ -# file-reader-promise \ No newline at end of file +# file-reader-promise + +Promise-based [`FileReader`](https://developer.mozilla.org/docs/Web/API/FileReader) implementation. + +## Installation + +node + +```sh +npm install @myrear/file-reader-promise +``` + +deno + +```ts +import { + FileReader, + readAs, +} from 'https://deno.land/x/file-reader-promise/mod.ts' +``` + +## Usage + +This library provides 2 modules: + +- [`readAs`](#readas) +- [`FileReader`](#filereader) + + Thin wrapper class for the web standard [`FileReader`](https://developer.mozilla.org/docs/Web/API/FileReader) class + +### `readAs` + +```ts +const result: string = await readAs(reader, file, 'DataURL') +const result: ArrayBuffer = await readAs(reader, file, 'ArrayBuffer') +``` + +### `FileReader` + +```ts +const result: string = await new FileReader().readAsDataURL(file) +const result: ArrayBuffer = await new FileReader().readAsArrayBuffer(file) +``` diff --git a/__tests__/FileReader.test.ts b/__tests__/FileReader.test.ts new file mode 100644 index 0000000..4e1d358 --- /dev/null +++ b/__tests__/FileReader.test.ts @@ -0,0 +1,40 @@ +import './setup.ts' +import { FileReader } from '../src/FileReader.ts' +import { assertEquals } from 'std/assert/assert_equals.ts' +import { assertInstanceOf } from 'std/assert/assert_instance_of.ts' + +Deno.test('read blob', async (t) => { + const blob = new Blob(['foo'], { type: 'text/plain' }) + + await t.step('as `ArrayBuffer`', async () => { + const reader = new FileReader() + const result = await reader.readAsArrayBuffer(blob) + + assertEquals(result, reader.result) + assertInstanceOf(result, ArrayBuffer) + }) + + await t.step('as `BinaryString`', async () => { + const reader = new FileReader() + const result = await reader.readAsBinaryString(blob) + + assertEquals(result, reader.result) + assertEquals(result, 'foo') + }) + + await t.step('as `DataURL`', async () => { + const reader = new FileReader() + const result = await reader.readAsDataURL(blob) + + assertEquals(result, reader.result) + assertEquals(result, 'data:text/plain;base64,Zm9v') + }) + + await t.step('as `Text` with encoding utf-8', async () => { + const reader = new FileReader() + const result = await reader.readAsText(blob, 'utf-8') + + assertEquals(result, reader.result) + assertEquals(result, 'foo') + }) +}) diff --git a/__tests__/readAs.test.ts b/__tests__/readAs.test.ts new file mode 100644 index 0000000..0b69447 --- /dev/null +++ b/__tests__/readAs.test.ts @@ -0,0 +1,60 @@ +import './setup.ts' +import { assertEquals, assertInstanceOf } from 'std/assert/mod.ts' +import { assertSpyCall, spy } from 'std/testing/mock.ts' +import { readAs } from '../src/readAs.ts' + +Deno.test('read blob', async (t) => { + const blob = new Blob(['foo'], { type: 'text/plain' }) + + await t.step('as `ArrayBuffer`', async () => { + const reader = new FileReader() + const readAsArrayBufferSpy = spy(reader, 'readAsArrayBuffer') + const result = await readAs(reader, blob, 'ArrayBuffer') + + assertEquals(result, reader.result) + assertInstanceOf(result, ArrayBuffer) + assertSpyCall(readAsArrayBufferSpy, 0, { + self: reader, + args: [blob, undefined], + }) + }) + + await t.step('as `BinaryString`', async () => { + const reader = new FileReader() + const readAsBinaryStringSpy = spy(reader, 'readAsBinaryString') + const result = await readAs(reader, blob, 'BinaryString') + + assertEquals(result, reader.result) + assertEquals(result, 'foo') + assertSpyCall(readAsBinaryStringSpy, 0, { + self: reader, + args: [blob, undefined], + }) + }) + + await t.step('as `DataURL`', async () => { + const reader = new FileReader() + const readAsDataURLSpy = spy(reader, 'readAsDataURL') + const result = await readAs(reader, blob, 'DataURL') + + assertEquals(result, reader.result) + assertEquals(result, 'data:text/plain;base64,Zm9v') + assertSpyCall(readAsDataURLSpy, 0, { + self: reader, + args: [blob, undefined], + }) + }) + + await t.step('as `Text` with encoding utf-8', async () => { + const reader = new FileReader() + const readAsTextSpy = spy(reader, 'readAsText') + const result = await readAs(reader, blob, 'Text', 'utf-8') + + assertEquals(result, reader.result) + assertEquals(typeof result, 'string') + assertSpyCall(readAsTextSpy, 0, { + self: reader, + args: [blob, 'utf-8'], + }) + }) +}) diff --git a/__tests__/setup.ts b/__tests__/setup.ts new file mode 100644 index 0000000..e582d82 --- /dev/null +++ b/__tests__/setup.ts @@ -0,0 +1,8 @@ +import { JSDOM } from 'jsdom' +import { isNode } from 'which_runtime/mod.ts' + +if (isNode) { + const { window: { Blob, FileReader } } = new JSDOM() + globalThis.Blob = Blob + globalThis.FileReader = FileReader +} diff --git a/deno.jsonc b/deno.jsonc new file mode 100644 index 0000000..ae359d4 --- /dev/null +++ b/deno.jsonc @@ -0,0 +1,17 @@ +{ + "tasks": { + "build": "deno run -A npm_build.ts" + }, + "fmt": { + "semiColons": false, + "singleQuote": true, + "exclude": ["*.md"] + }, + "exclude": ["npm/**/*"], + "imports": { + "dnt/": "https://deno.land/x/dnt@0.38.1/", + "std/": "https://deno.land/std@0.199.0/", + "jsdom": "https://esm.sh/jsdom@22.1.0", + "which_runtime/": "https://deno.land/x/which_runtime@0.2.0/" + } +} diff --git a/deno.lock b/deno.lock new file mode 100644 index 0000000..d2e6c69 --- /dev/null +++ b/deno.lock @@ -0,0 +1,181 @@ +{ + "version": "2", + "remote": { + "https://deno.land/std@0.140.0/_util/assert.ts": "e94f2eb37cebd7f199952e242c77654e43333c1ac4c5c700e929ea3aa5489f74", + "https://deno.land/std@0.140.0/_util/os.ts": "3b4c6e27febd119d36a416d7a97bd3b0251b77c88942c8f16ee5953ea13e2e49", + "https://deno.land/std@0.140.0/bytes/bytes_list.ts": "67eb118e0b7891d2f389dad4add35856f4ad5faab46318ff99653456c23b025d", + "https://deno.land/std@0.140.0/bytes/equals.ts": "fc16dff2090cced02497f16483de123dfa91e591029f985029193dfaa9d894c9", + "https://deno.land/std@0.140.0/bytes/mod.ts": "763f97d33051cc3f28af1a688dfe2830841192a9fea0cbaa55f927b49d49d0bf", + "https://deno.land/std@0.140.0/fmt/colors.ts": "30455035d6d728394781c10755351742dd731e3db6771b1843f9b9e490104d37", + "https://deno.land/std@0.140.0/fs/_util.ts": "0fb24eb4bfebc2c194fb1afdb42b9c3dda12e368f43e8f2321f84fc77d42cb0f", + "https://deno.land/std@0.140.0/fs/ensure_dir.ts": "9dc109c27df4098b9fc12d949612ae5c9c7169507660dcf9ad90631833209d9d", + "https://deno.land/std@0.140.0/io/buffer.ts": "bd0c4bf53db4b4be916ca5963e454bddfd3fcd45039041ea161dbf826817822b", + "https://deno.land/std@0.140.0/path/_constants.ts": "df1db3ffa6dd6d1252cc9617e5d72165cd2483df90e93833e13580687b6083c3", + "https://deno.land/std@0.140.0/path/_interface.ts": "ee3b431a336b80cf445441109d089b70d87d5e248f4f90ff906820889ecf8d09", + "https://deno.land/std@0.140.0/path/_util.ts": "c1e9686d0164e29f7d880b2158971d805b6e0efc3110d0b3e24e4b8af2190d2b", + "https://deno.land/std@0.140.0/path/common.ts": "bee563630abd2d97f99d83c96c2fa0cca7cee103e8cb4e7699ec4d5db7bd2633", + "https://deno.land/std@0.140.0/path/glob.ts": "cb5255638de1048973c3e69e420c77dc04f75755524cb3b2e160fe9277d939ee", + "https://deno.land/std@0.140.0/path/mod.ts": "d3e68d0abb393fb0bf94a6d07c46ec31dc755b544b13144dee931d8d5f06a52d", + "https://deno.land/std@0.140.0/path/posix.ts": "293cdaec3ecccec0a9cc2b534302dfe308adb6f10861fa183275d6695faace44", + "https://deno.land/std@0.140.0/path/separator.ts": "fe1816cb765a8068afb3e8f13ad272351c85cbc739af56dacfc7d93d710fe0f9", + "https://deno.land/std@0.140.0/path/win32.ts": "31811536855e19ba37a999cd8d1b62078235548d67902ece4aa6b814596dd757", + "https://deno.land/std@0.140.0/streams/conversion.ts": "712585bfa0172a97fb68dd46e784ae8ad59d11b88079d6a4ab098ff42e697d21", + "https://deno.land/std@0.181.0/_util/asserts.ts": "178dfc49a464aee693a7e285567b3d0b555dc805ff490505a8aae34f9cfb1462", + "https://deno.land/std@0.181.0/_util/os.ts": "d932f56d41e4f6a6093d56044e29ce637f8dcc43c5a90af43504a889cf1775e3", + "https://deno.land/std@0.181.0/fs/_util.ts": "65381f341af1ff7f40198cee15c20f59951ac26e51ddc651c5293e24f9ce6f32", + "https://deno.land/std@0.181.0/fs/ensure_dir.ts": "dc64c4c75c64721d4e3fb681f1382f803ff3d2868f08563ff923fdd20d071c40", + "https://deno.land/std@0.181.0/fs/expand_glob.ts": "e4f56259a0a70fe23f05215b00de3ac5e6ba46646ab2a06ebbe9b010f81c972a", + "https://deno.land/std@0.181.0/fs/walk.ts": "ea95ffa6500c1eda6b365be488c056edc7c883a1db41ef46ec3bf057b1c0fe32", + "https://deno.land/std@0.181.0/path/_constants.ts": "e49961f6f4f48039c0dfed3c3f93e963ca3d92791c9d478ac5b43183413136e0", + "https://deno.land/std@0.181.0/path/_interface.ts": "6471159dfbbc357e03882c2266d21ef9afdb1e4aa771b0545e90db58a0ba314b", + "https://deno.land/std@0.181.0/path/_util.ts": "d7abb1e0dea065f427b89156e28cdeb32b045870acdf865833ba808a73b576d0", + "https://deno.land/std@0.181.0/path/common.ts": "ee7505ab01fd22de3963b64e46cff31f40de34f9f8de1fff6a1bd2fe79380000", + "https://deno.land/std@0.181.0/path/glob.ts": "d479e0a695621c94d3fd7fe7abd4f9499caf32a8de13f25073451c6ef420a4e1", + "https://deno.land/std@0.181.0/path/mod.ts": "bf718f19a4fdd545aee1b06409ca0805bd1b68ecf876605ce632e932fe54510c", + "https://deno.land/std@0.181.0/path/posix.ts": "8b7c67ac338714b30c816079303d0285dd24af6b284f7ad63da5b27372a2c94d", + "https://deno.land/std@0.181.0/path/separator.ts": "0fb679739d0d1d7bf45b68dacfb4ec7563597a902edbaf3c59b50d5bcadd93b1", + "https://deno.land/std@0.181.0/path/win32.ts": "d186344e5583bcbf8b18af416d13d82b35a317116e6460a5a3953508c3de5bba", + "https://deno.land/std@0.182.0/_util/asserts.ts": "178dfc49a464aee693a7e285567b3d0b555dc805ff490505a8aae34f9cfb1462", + "https://deno.land/std@0.182.0/_util/os.ts": "d932f56d41e4f6a6093d56044e29ce637f8dcc43c5a90af43504a889cf1775e3", + "https://deno.land/std@0.182.0/fmt/colors.ts": "d67e3cd9f472535241a8e410d33423980bec45047e343577554d3356e1f0ef4e", + "https://deno.land/std@0.182.0/fs/_util.ts": "65381f341af1ff7f40198cee15c20f59951ac26e51ddc651c5293e24f9ce6f32", + "https://deno.land/std@0.182.0/fs/empty_dir.ts": "c3d2da4c7352fab1cf144a1ecfef58090769e8af633678e0f3fabaef98594688", + "https://deno.land/std@0.182.0/fs/expand_glob.ts": "e4f56259a0a70fe23f05215b00de3ac5e6ba46646ab2a06ebbe9b010f81c972a", + "https://deno.land/std@0.182.0/fs/walk.ts": "920be35a7376db6c0b5b1caf1486fb962925e38c9825f90367f8f26b5e5d0897", + "https://deno.land/std@0.182.0/path/_constants.ts": "e49961f6f4f48039c0dfed3c3f93e963ca3d92791c9d478ac5b43183413136e0", + "https://deno.land/std@0.182.0/path/_interface.ts": "6471159dfbbc357e03882c2266d21ef9afdb1e4aa771b0545e90db58a0ba314b", + "https://deno.land/std@0.182.0/path/_util.ts": "d7abb1e0dea065f427b89156e28cdeb32b045870acdf865833ba808a73b576d0", + "https://deno.land/std@0.182.0/path/common.ts": "ee7505ab01fd22de3963b64e46cff31f40de34f9f8de1fff6a1bd2fe79380000", + "https://deno.land/std@0.182.0/path/glob.ts": "d479e0a695621c94d3fd7fe7abd4f9499caf32a8de13f25073451c6ef420a4e1", + "https://deno.land/std@0.182.0/path/mod.ts": "bf718f19a4fdd545aee1b06409ca0805bd1b68ecf876605ce632e932fe54510c", + "https://deno.land/std@0.182.0/path/posix.ts": "8b7c67ac338714b30c816079303d0285dd24af6b284f7ad63da5b27372a2c94d", + "https://deno.land/std@0.182.0/path/separator.ts": "0fb679739d0d1d7bf45b68dacfb4ec7563597a902edbaf3c59b50d5bcadd93b1", + "https://deno.land/std@0.182.0/path/win32.ts": "d186344e5583bcbf8b18af416d13d82b35a317116e6460a5a3953508c3de5bba", + "https://deno.land/std@0.199.0/_util/diff.ts": "1a3c044aedf77647d6cac86b798c6417603361b66b54c53331b312caeb447aea", + "https://deno.land/std@0.199.0/assert/_constants.ts": "8a9da298c26750b28b326b297316cdde860bc237533b07e1337c021379e6b2a9", + "https://deno.land/std@0.199.0/assert/_format.ts": "a69126e8a469009adf4cf2a50af889aca364c349797e63174884a52ff75cf4c7", + "https://deno.land/std@0.199.0/assert/assert.ts": "9a97dad6d98c238938e7540736b826440ad8c1c1e54430ca4c4e623e585607ee", + "https://deno.land/std@0.199.0/assert/assert_almost_equals.ts": "e15ca1f34d0d5e0afae63b3f5d975cbd18335a132e42b0c747d282f62ad2cd6c", + "https://deno.land/std@0.199.0/assert/assert_array_includes.ts": "6856d7f2c3544bc6e62fb4646dfefa3d1df5ff14744d1bca19f0cbaf3b0d66c9", + "https://deno.land/std@0.199.0/assert/assert_equals.ts": "a0ee60574e437bcab2dcb79af9d48dc88845f8fd559468d9c21b15fd638ef943", + "https://deno.land/std@0.199.0/assert/assert_exists.ts": "407cb6b9fb23a835cd8d5ad804e2e2edbbbf3870e322d53f79e1c7a512e2efd7", + "https://deno.land/std@0.199.0/assert/assert_false.ts": "a9962749f4bf5844e3fa494257f1de73d69e4fe0e82c34d0099287552163a2dc", + "https://deno.land/std@0.199.0/assert/assert_instance_of.ts": "09fd297352a5b5bbb16da2b5e1a0d8c6c44da5447772648622dcc7df7af1ddb8", + "https://deno.land/std@0.199.0/assert/assert_is_error.ts": "b4eae4e5d182272efc172bf28e2e30b86bb1650cd88aea059e5d2586d4160fb9", + "https://deno.land/std@0.199.0/assert/assert_match.ts": "c4083f80600bc190309903c95e397a7c9257ff8b5ae5c7ef91e834704e672e9b", + "https://deno.land/std@0.199.0/assert/assert_not_equals.ts": "9f1acab95bd1f5fc9a1b17b8027d894509a745d91bac1718fdab51dc76831754", + "https://deno.land/std@0.199.0/assert/assert_not_instance_of.ts": "0c14d3dfd9ab7a5276ed8ed0b18c703d79a3d106102077ec437bfe7ed912bd22", + "https://deno.land/std@0.199.0/assert/assert_not_match.ts": "3796a5b0c57a1ce6c1c57883dd4286be13a26f715ea662318ab43a8491a13ab0", + "https://deno.land/std@0.199.0/assert/assert_not_strict_equals.ts": "ca6c6d645e95fbc873d25320efeb8c4c6089a9a5e09f92d7c1c4b6e935c2a6ad", + "https://deno.land/std@0.199.0/assert/assert_object_match.ts": "d8fc2867cfd92eeacf9cea621e10336b666de1874a6767b5ec48988838370b54", + "https://deno.land/std@0.199.0/assert/assert_rejects.ts": "45c59724de2701e3b1f67c391d6c71c392363635aad3f68a1b3408f9efca0057", + "https://deno.land/std@0.199.0/assert/assert_strict_equals.ts": "5cf29b38b3f8dece95287325723272aa04e04dbf158d886d662fa594fddc9ed3", + "https://deno.land/std@0.199.0/assert/assert_string_includes.ts": "b821d39ebf5cb0200a348863c86d8c4c4b398e02012ce74ad15666fc4b631b0c", + "https://deno.land/std@0.199.0/assert/assert_throws.ts": "63784e951475cb7bdfd59878cd25a0931e18f6dc32a6077c454b2cd94f4f4bcd", + "https://deno.land/std@0.199.0/assert/assertion_error.ts": "4d0bde9b374dfbcbe8ac23f54f567b77024fb67dbb1906a852d67fe050d42f56", + "https://deno.land/std@0.199.0/assert/equal.ts": "9f1a46d5993966d2596c44e5858eec821859b45f783a5ee2f7a695dfc12d8ece", + "https://deno.land/std@0.199.0/assert/fail.ts": "c36353d7ae6e1f7933d45f8ea51e358c8c4b67d7e7502028598fe1fea062e278", + "https://deno.land/std@0.199.0/assert/mod.ts": "08d55a652c22c5da0215054b21085cec25a5da47ce4a6f9de7d9ad36df35bdee", + "https://deno.land/std@0.199.0/assert/unimplemented.ts": "d56fbeecb1f108331a380f72e3e010a1f161baa6956fd0f7cf3e095ae1a4c75a", + "https://deno.land/std@0.199.0/assert/unreachable.ts": "4600dc0baf7d9c15a7f7d234f00c23bca8f3eba8b140286aaca7aa998cf9a536", + "https://deno.land/std@0.199.0/fmt/colors.ts": "a7eecffdf3d1d54db890723b303847b6e0a1ab4b528ba6958b8f2e754cf1b3bc", + "https://deno.land/std@0.199.0/testing/mock.ts": "6576b4aa55ee20b1990d656a78fff83599e190948c00e9f25a7f3ac5e9d6492d", + "https://deno.land/x/code_block_writer@12.0.0/mod.ts": "2c3448060e47c9d08604c8f40dee34343f553f33edcdfebbf648442be33205e5", + "https://deno.land/x/code_block_writer@12.0.0/utils/string_utils.ts": "60cb4ec8bd335bf241ef785ccec51e809d576ff8e8d29da43d2273b69ce2a6ff", + "https://deno.land/x/deno_cache@0.5.2/auth_tokens.ts": "5d1d56474c54a9d152e44d43ea17c2e6a398dd1e9682c69811a313567c01ee1e", + "https://deno.land/x/deno_cache@0.5.2/cache.ts": "92ce8511e1e5c00fdf53a41619aa77d632ea8e0fc711324322e4d5ebf8133911", + "https://deno.land/x/deno_cache@0.5.2/deno_dir.ts": "1ea355b8ba11c630d076b222b197cfc937dd81e5a4a260938997da99e8ff93a0", + "https://deno.land/x/deno_cache@0.5.2/deps.ts": "26a75905652510b76e54b6d5ef3cf824d1062031e00782efcd768978419224e7", + "https://deno.land/x/deno_cache@0.5.2/dirs.ts": "009c6f54e0b610914d6ce9f72f6f6ccfffd2d47a79a19061e0a9eb4253836069", + "https://deno.land/x/deno_cache@0.5.2/disk_cache.ts": "66a1e604a8d564b6dd0500326cac33d08b561d331036bf7272def80f2f7952aa", + "https://deno.land/x/deno_cache@0.5.2/file_fetcher.ts": "89616c50b6df73fb04e73d0b7cd99e5f2ed7967386913d65b9e8baa4238501f7", + "https://deno.land/x/deno_cache@0.5.2/http_cache.ts": "407135eaf2802809ed373c230d57da7ef8dff923c4abf205410b9b99886491fd", + "https://deno.land/x/deno_cache@0.5.2/lib/deno_cache_dir.generated.js": "18b6526d0c50791a73dd0eb894e99de1ac05ee79dcbd53298ff5b5b6b0757fe6", + "https://deno.land/x/deno_cache@0.5.2/lib/snippets/deno_cache_dir-77bed54ace8005e0/fs.js": "cbe3a976ed63c72c7cb34ef845c27013033a3b11f9d8d3e2c4aa5dda2c0c7af6", + "https://deno.land/x/deno_cache@0.5.2/mod.ts": "0b4d071ad095128bdc2b1bc6e5d2095222dcbae08287261690ee9757e6300db6", + "https://deno.land/x/deno_cache@0.5.2/util.ts": "f3f5a0cfc60051f09162942fb0ee87a0e27b11a12aec4c22076e3006be4cc1e2", + "https://deno.land/x/dir@1.5.1/data_local_dir/mod.ts": "91eb1c4bfadfbeda30171007bac6d85aadacd43224a5ed721bbe56bc64e9eb66", + "https://deno.land/x/dnt@0.38.1/lib/compiler.ts": "209ad2e1b294f93f87ec02ade9a0821f942d2e524104552d0aa8ff87021050a5", + "https://deno.land/x/dnt@0.38.1/lib/compiler_transforms.ts": "f21aba052f5dcf0b0595c734450842855c7f572e96165d3d34f8fed2fc1f7ba1", + "https://deno.land/x/dnt@0.38.1/lib/mod.deps.ts": "30367fc68bcd2acf3b7020cf5cdd26f817f7ac9ac35c4bfb6c4551475f91bc3e", + "https://deno.land/x/dnt@0.38.1/lib/npm_ignore.ts": "57fbb7e7b935417d225eec586c6aa240288905eb095847d3f6a88e290209df4e", + "https://deno.land/x/dnt@0.38.1/lib/package_json.ts": "61f35b06e374ed39ca776d29d67df4be7ee809d0bca29a8239687556c6d027c2", + "https://deno.land/x/dnt@0.38.1/lib/pkg/dnt_wasm.generated.js": "cfb352ae839865f5698c9b35099d4c783510195a1e3c9f9b04d94fac86394ed9", + "https://deno.land/x/dnt@0.38.1/lib/pkg/snippets/dnt-wasm-a15ef721fa5290c5/helpers.js": "45f74f00472b3a399bc16e5dc056966b55dcdd8fa2bd61505c6dfd2f5d33b9f4", + "https://deno.land/x/dnt@0.38.1/lib/shims.ts": "df1bd4d9a196dca4b2d512b1564fff64ac6c945189a273d706391f87f210d7e6", + "https://deno.land/x/dnt@0.38.1/lib/test_runner/get_test_runner_code.ts": "4dc7a73a13b027341c0688df2b29a4ef102f287c126f134c33f69f0339b46968", + "https://deno.land/x/dnt@0.38.1/lib/test_runner/test_runner.ts": "4d0da0500ec427d5f390d9a8d42fb882fbeccc92c92d66b6f2e758606dbd40e6", + "https://deno.land/x/dnt@0.38.1/lib/transform.deps.ts": "e42f2bdef46d098453bdba19261a67cf90b583f5d868f7fe83113c1380d9b85c", + "https://deno.land/x/dnt@0.38.1/lib/types.ts": "b8e228b2fac44c2ae902fbb73b1689f6ab889915bd66486c8a85c0c24255f5fb", + "https://deno.land/x/dnt@0.38.1/lib/utils.ts": "878b7ac7003a10c16e6061aa49dbef9b42bd43174853ebffc9b67ea47eeb11d8", + "https://deno.land/x/dnt@0.38.1/mod.ts": "b13349fe77847cf58e26b40bcd58797a8cec5d71b31a1ca567071329c8489de1", + "https://deno.land/x/dnt@0.38.1/transform.ts": "f68743a14cf9bf53bfc9c81073871d69d447a7f9e3453e0447ca2fb78926bb1d", + "https://deno.land/x/ts_morph@18.0.0/bootstrap/mod.ts": "b53aad517f106c4079971fcd4a81ab79fadc40b50061a3ab2b741a09119d51e9", + "https://deno.land/x/ts_morph@18.0.0/bootstrap/ts_morph_bootstrap.js": "6645ac03c5e6687dfa8c78109dc5df0250b811ecb3aea2d97c504c35e8401c06", + "https://deno.land/x/ts_morph@18.0.0/common/DenoRuntime.ts": "6a7180f0c6e90dcf23ccffc86aa8271c20b1c4f34c570588d08a45880b7e172d", + "https://deno.land/x/ts_morph@18.0.0/common/mod.ts": "01985d2ee7da8d1caee318a9d07664774fbee4e31602bc2bb6bb62c3489555ed", + "https://deno.land/x/ts_morph@18.0.0/common/ts_morph_common.js": "845671ca951073400ce142f8acefa2d39ea9a51e29ca80928642f3f8cf2b7700", + "https://deno.land/x/ts_morph@18.0.0/common/typescript.js": "d5c598b6a2db2202d0428fca5fd79fc9a301a71880831a805d778797d2413c59", + "https://deno.land/x/wasmbuild@0.15.0/cache.ts": "89eea5f3ce6035a1164b3e655c95f21300498920575ade23161421f5b01967f4", + "https://deno.land/x/wasmbuild@0.15.0/loader.ts": "d98d195a715f823151cbc8baa3f32127337628379a02d9eb2a3c5902dbccfc02", + "https://deno.land/x/which_runtime@0.2.0/mod.ts": "3e8f1ddb3a043818264a8035c0132aaf72d94977d45cd23ecfb03b2156f06126", + "https://esm.sh/jsdom@22.1.0": "1009ac01c86a11436868b8ffaeb38320d844bff81e37e30644cff913bbe0c21a", + "https://esm.sh/v131/@tootallnate/once@2.0.0/denonext/once.mjs": "2c69d53dd1de1d7e5f9a78d46041f5d869f7d058f22d72e7f25821e61dddd147", + "https://esm.sh/v131/abab@2.0.6/denonext/abab.mjs": "7101c07a728882d686222e5f5c5a6fa956a558e4f406c521f1c0b61bc60cd07d", + "https://esm.sh/v131/agent-base@6.0.2/denonext/agent-base.mjs": "caaff3fb4461840397578ea87216582b7f0fa6f83861bbddf6df80f38bc3c550", + "https://esm.sh/v131/bufferutil@4.0.7/denonext/bufferutil.mjs": "abe42a54dfdc6365872850cd4395be09f2198b84a1d46022b88c98a6196f6e1f", + "https://esm.sh/v131/canvas@2.11.2/denonext/canvas.mjs": "4245b1d01d91b5e807b85e40e98efe28c93634260bd8cb5ac0da71c42098a1a4", + "https://esm.sh/v131/cssstyle@3.0.0/denonext/cssstyle.mjs": "1ad2b2ecc78759e2a62872b6d6e37ddaf89e17c3400bbf3e8910932c1fa2d62b", + "https://esm.sh/v131/data-urls@4.0.0/denonext/data-urls.mjs": "4cd3d59427626d61769d44c0ca01291fc708b315bdce57482e4930f82c9893e7", + "https://esm.sh/v131/debug@4.3.4/denonext/debug.mjs": "892826bb4505deb6337df2f0f72b1e355e5377e702dd739b78774539d7482f5c", + "https://esm.sh/v131/decimal.js@10.4.3/denonext/decimal.mjs": "ece864b80732112b324aed7fd89ec03128627206e1e4b30057651f495b594dee", + "https://esm.sh/v131/domexception@4.0.0/denonext/webidl2js-wrapper.js": "8676cb19f114d3833c8c06061cc53ca1cc9e9899e3b3967bcdd514224cafd9ef", + "https://esm.sh/v131/entities@4.5.0/denonext/lib/decode.js": "7fea6d8bd725edbbf7ea05031d2ea1bbbc1166dc11e3345d541198dd2dc16f1e", + "https://esm.sh/v131/entities@4.5.0/denonext/lib/escape.js": "7ebdc622bf3618bab25db40da4a49e2b9d03f044745f125f0bc3359f2d060def", + "https://esm.sh/v131/form-data@4.0.0/denonext/form-data.mjs": "48e84ac3b590bc364839367938d7e48ca37615a0c66e56dcc7356c3172ec7790", + "https://esm.sh/v131/html-encoding-sniffer@3.0.0/denonext/html-encoding-sniffer.mjs": "8cc45dfcd2643dbcaf57487cf208565621776b5f8a2ea8d946c975fadb7de666", + "https://esm.sh/v131/http-proxy-agent@5.0.0/denonext/http-proxy-agent.mjs": "d9bef2e49c05f0f2b5aab5896ae928638dade2f4fca63b43b929c76ba89deb1e", + "https://esm.sh/v131/https-proxy-agent@5.0.1/denonext/https-proxy-agent.mjs": "589559c972488b2a81bf294ecea55ae6d81da57bd4dc11a79d818ebbbf468774", + "https://esm.sh/v131/iconv-lite@0.6.3/denonext/iconv-lite.mjs": "9bef42f23ec243d276745ff25d5b4e060ec99c2ff6bac2560d097bffaada16b5", + "https://esm.sh/v131/is-potential-custom-element-name@1.0.1/denonext/is-potential-custom-element-name.mjs": "de2781ef99795b662f43c0840c3dcfdc303f9e60a75e66924370f902133469ed", + "https://esm.sh/v131/jsdom@22.1.0/denonext/jsdom.mjs": "a6d7dd8c21eb482f50206802b538764ae8e50264d756e6abf1092a88c6975f2d", + "https://esm.sh/v131/ms@2.1.2/denonext/ms.mjs": "aa4dc45ba72554c5011168f8910cc646c37af53cfff1a15a4decced838b8eb14", + "https://esm.sh/v131/node-gyp-build@4.6.0/denonext/node-gyp-build.mjs": "58fc8e41b3ffd2f8a6b2a1694292976e6e12768d6e3895b9c8c13239562ffe64", + "https://esm.sh/v131/nwsapi@2.2.7/denonext/nwsapi.mjs": "0fbf1a70adddb4cea42e3ec72655fdf732ceedb4efc3b7b9c04c1ee5a3dce537", + "https://esm.sh/v131/parse5@7.1.2/denonext/parse5.mjs": "ec23d1c6250ca1254e8b091b10132e5f9e9250d4bdcc7b37fd814c53a2adbc8e", + "https://esm.sh/v131/psl@1.9.0/denonext/psl.mjs": "f40e9bcba5f6602eeb677f85f8e2b5c24b706bade58ced5b272044ebea609d9b", + "https://esm.sh/v131/querystringify@2.2.0/denonext/querystringify.mjs": "4f0f639f99ec4a7ddaffb886bd7e6f3fe4b088e1fcd60336dd10d447d2093ef6", + "https://esm.sh/v131/requires-port@1.0.0/denonext/requires-port.mjs": "c4f20b71539d08fc2662d75dfd79881fce985a0e4592268f18ac13bf53679efa", + "https://esm.sh/v131/rrweb-cssom@0.6.0/denonext/rrweb-cssom.mjs": "e2a14692d801a24edadd3fd4ecdf017382494258064134e33390a6bf1146950d", + "https://esm.sh/v131/safer-buffer@2.1.2/denonext/safer-buffer.mjs": "ce0e787812c668ba082ad5b75958490c714b6e05836bd5b6013d9f75727c006f", + "https://esm.sh/v131/saxes@6.0.0/denonext/saxes.mjs": "47f161ce2d3448de2fb92f5379387da90d93c1ae507f7f6148175967173e5d1c", + "https://esm.sh/v131/symbol-tree@3.2.4/denonext/symbol-tree.mjs": "67199d1e47bd6e5b7d2715dd04d25658061c95fc4464f7d200b6aab9e439b5f4", + "https://esm.sh/v131/tough-cookie@4.1.3/denonext/tough-cookie.mjs": "b34e5c2b0bf2a5e27a86da871bcf04da3e6fce2d63a93054d84e7b5ca2d3c641", + "https://esm.sh/v131/tr46@4.1.1/denonext/tr46.mjs": "2042307b546151186c6912a27265e444331e3d0bd6820c92a750aa67b3e5fdfc", + "https://esm.sh/v131/universalify@0.2.0/denonext/universalify.mjs": "6ebb2a9b372d4ae89f494d9b0044942dc76a5b5fb366ab3018e2bf0aeb8eae94", + "https://esm.sh/v131/url-parse@1.5.10/denonext/url-parse.mjs": "7948bd2cdcf69518c1a85e24086eb8694c0c29089d95eeff2d78675054405064", + "https://esm.sh/v131/utf-8-validate@6.0.3/denonext/utf-8-validate.mjs": "6197c86d1731c0c56002eac5d14d7dc6a23d7f8de06623eeef5587aa63aa968b", + "https://esm.sh/v131/w3c-xmlserializer@4.0.0/denonext/w3c-xmlserializer.mjs": "e63fb8f147bc8c1861a5cad917da7a1d8c9fdf5aeb239734a0a63b8dc2d60646", + "https://esm.sh/v131/webidl-conversions@7.0.0/denonext/webidl-conversions.mjs": "04e3e6917179380727c6f65cd16a5a89836fb5a104fe5524c10a0a697f88d552", + "https://esm.sh/v131/whatwg-encoding@2.0.0/denonext/whatwg-encoding.mjs": "8abfd161fae9842388510932319186567969e2fed9f96b96fad286b05c675f65", + "https://esm.sh/v131/whatwg-mimetype@3.0.0/denonext/whatwg-mimetype.mjs": "59446370c8333e5956416535e9773585645c22e7ea3420c8df5d78d1e9127c59", + "https://esm.sh/v131/whatwg-url@12.0.1/denonext/webidl2js-wrapper.js": "c9948ea44212dc0e4e3160b0f98c3fb396b81c43fcb6b6a5fda8f215bf798cd2", + "https://esm.sh/v131/whatwg-url@12.0.1/denonext/whatwg-url.mjs": "97a4bbd27c34284eedcebd8d0bad1dbd1286076632150438139e5e59fec7b7a7", + "https://esm.sh/v131/ws@8.13.0/denonext/ws.mjs": "ed9425cc1b9c9b9987590c15646b9adcd8e7d4c4cfff745fdc273a46cbc2b7cc", + "https://esm.sh/v131/xml-name-validator@4.0.0/denonext/xml-name-validator.mjs": "69af66c891312f304a8be720961ad5cce5c49c85a3bba03275b56a70dec7a21e", + "https://esm.sh/v131/xmlchars@2.2.0/denonext/xml/1.0/ed5.js": "60f8f018eb1d79d69a41324155b7d9f52f1058b37060b28acc1dfc49446e549d", + "https://esm.sh/v131/xmlchars@2.2.0/denonext/xml/1.1/ed2.js": "ba7d1fe5694f62469c4b293a1fadad332c637cbcfbc74147a296475c2ff8ad3d", + "https://esm.sh/v131/xmlchars@2.2.0/denonext/xmlns/1.0/ed3.js": "929d15ffc72d56c8909f87e7df8288f060bda0256622e8e95c24f0decb06adc7" + }, + "npm": { + "specifiers": { + "@types/node": "@types/node@18.16.19" + }, + "packages": { + "@types/node@18.16.19": { + "integrity": "sha512-IXl7o+R9iti9eBW4Wg2hx1xQDig183jj7YLn8F7udNceyfkbn1ZxmzZXuak20gR40D7pIkIY1kYGx5VIGbaHKA==", + "dependencies": {} + } + } + } +} diff --git a/npm_build.ts b/npm_build.ts new file mode 100644 index 0000000..dbc67b8 --- /dev/null +++ b/npm_build.ts @@ -0,0 +1,37 @@ +import { build, emptyDir } from 'dnt/mod.ts' + +await emptyDir('./npm') + +await build({ + entryPoints: ['./src/mod.ts'], + outDir: './npm', + shims: { + deno: { test: 'dev' }, + }, + compilerOptions: { + lib: ['ESNext', 'DOM'], + }, + typeCheck: 'both', + importMap: 'deno.jsonc', + package: { + name: '@myrear/file-reader-promise', + description: 'Promise-based FileReader implementation.', + author: 'myrear', + license: 'MIT', + version: Deno.args[0], + repository: { + type: 'git', + url: 'https://github.com/myrear/file-reader-promise.git', + }, + bugs: { + url: 'https://github.com/myrear/file-reader-promise/issues', + }, + devDependencies: { + '@types/jsdom': '^21.1.1', + }, + }, + postBuild() { + Deno.copyFileSync('LICENSE', 'npm/LICENSE') + Deno.copyFileSync('README.md', 'npm/README.md') + }, +}) diff --git a/src/FileReader.ts b/src/FileReader.ts new file mode 100644 index 0000000..697236c --- /dev/null +++ b/src/FileReader.ts @@ -0,0 +1,27 @@ +import { promisifyReader } from './promisifyReader.ts' + +export class FileReader extends globalThis.FileReader { + readAsArrayBuffer(blob: Blob): Promise + readAsArrayBuffer(blob: Blob) { + super.readAsArrayBuffer(blob) + return promisifyReader(this) + } + + readAsBinaryString(blob: Blob): Promise + readAsBinaryString(blob: Blob) { + super.readAsBinaryString(blob) + return promisifyReader(this) + } + + readAsDataURL(blob: Blob): Promise + readAsDataURL(blob: Blob) { + super.readAsDataURL(blob) + return promisifyReader(this) + } + + readAsText(blob: Blob, encoding?: string): Promise + readAsText(blob: Blob, encoding?: string) { + super.readAsText(blob, encoding) + return promisifyReader(this) + } +} diff --git a/src/mod.ts b/src/mod.ts new file mode 100644 index 0000000..2a79fe5 --- /dev/null +++ b/src/mod.ts @@ -0,0 +1,2 @@ +export * from './readAs.ts' +export * from './FileReader.ts' diff --git a/src/promisifyReader.ts b/src/promisifyReader.ts new file mode 100644 index 0000000..4b146bc --- /dev/null +++ b/src/promisifyReader.ts @@ -0,0 +1,18 @@ +export const promisifyReader = (reader: FileReader) => + new Promise((resolve, reject) => { + reader.addEventListener('load', loadListener) + reader.addEventListener('error', reject) + reader.addEventListener('abort', reject) + reader.addEventListener('loadend', removeEventListener) + + function removeEventListener() { + reader.removeEventListener('load', loadListener) + reader.removeEventListener('error', reject) + reader.removeEventListener('abort', reject) + reader.removeEventListener('loadend', removeEventListener) + } + + function loadListener() { + resolve(reader.result) + } + }) diff --git a/src/readAs.ts b/src/readAs.ts new file mode 100644 index 0000000..b043e07 --- /dev/null +++ b/src/readAs.ts @@ -0,0 +1,30 @@ +import { promisifyReader } from './promisifyReader.ts' + +export function readAs( + reader: FileReader, + blob: Blob, + as: 'ArrayBuffer', +): Promise + +export function readAs( + reader: FileReader, + blob: Blob, + as: 'BinaryString' | 'DataURL', +): Promise + +export function readAs( + reader: FileReader, + blob: Blob, + as: 'Text', + encoding?: string, +): Promise + +export function readAs( + reader: FileReader, + blob: Blob, + as: 'ArrayBuffer' | 'DataURL' | 'Text' | 'BinaryString', + encoding?: string, +) { + reader[`readAs${as}`](blob, encoding) + return promisifyReader(reader) +}