Skip to content
This repository was archived by the owner on Feb 9, 2024. It is now read-only.

Conversation

@kentonv
Copy link
Member

@kentonv kentonv commented Jun 4, 2020

This switch/typeof example isn't valid JavaScript AFAICT -- JS doesn't have "type switches" like Go does.

NOTE: Unfortunately this example still won't work, because NotFoundError and MethodNotAllowedError are not imported. It doesn't look like kv-asset-handler exports these types currently, so I guess a code change is needed to export them?

See #93 (not fully fixed by this PR)

This switch/typeof example isn't valid JavaScript AFAICT -- JS doesn't have "type switches" like Go does.

NOTE: Unfortunately this example still won't work, because NotFoundError and MethodNotAllowedError are not imported. It doesn't look like kv-asset-handler exports these types currently, so I guess a code change is needed to export them?

See cloudflare#93 (not fully fixed by this PR)
@exvuma
Copy link
Contributor

exvuma commented Jun 4, 2020

I tested importing in the sites-template and it did work for me. (For reference https://gist.github.com/victoriabernard92/c55bb17e7e15dfa6612e7675286c9422) Is it not working for you? Either way, I think we should fix it to use import instead.

@kentonv
Copy link
Member Author

kentonv commented Jun 4, 2020

I didn't try it, but I looked at the code, and it doesn't appear to export the error types, so I assumed that wouldn't work...

https://github.com/cloudflare/kv-asset-handler/blob/master/src/index.ts#L193

I'm not sure what happens if you import a symbol that wasn't exported. Maybe it produces undefined rather than a hard error, and that's why the code appears to execute?

Have you tested that the catch block in your code works as you expect?

@kentonv
Copy link
Member Author

kentonv commented Jun 4, 2020

FWIW it looks like switch (typeof e) { case NotFoundError: ... } is not actually invalid, but the effect is nothing like it appears. typeof e evaluates to the string "object". The switch statement then looks for a case where the case value is === "object". None of the case values satisfy this condition, so the default branch should end up being taken...

Another problem I just realized is that the switch is on typeof resp, not typeof e. resp doesn't appear to be defined so I would expect this to throw ReferenceError...

@kentonv
Copy link
Member Author

kentonv commented Jun 4, 2020

Wow, actually, typeof anything where anything is undefined resolves to the string "undefined". So the switch is looking for a case "undefined"...

@kentonv
Copy link
Member Author

kentonv commented Jun 4, 2020

... I wonder if importing NotFoundError when NotFoundError is not actually exported ends up causing NotFoundError to match the string "undefined"...

@SukkaW
Copy link
Contributor

SukkaW commented Jun 5, 2020

... I wonder if importing NotFoundError when NotFoundError is not actually exported ends up causing NotFoundError to match the string "undefined"...

Yes, it does. undefined error will be thrown for current code changes.

@kentonv
Copy link
Member Author

kentonv commented Jun 5, 2020

@SukkaW Sorry, I think you misunderstood what I meant. I was wondering if it was possible that the original code -- if only the missing import was fixed -- would accidentally always execute the NotFoundError case, because typeof resp produces "undefined", and NotFoundError evaluated to undefined. I was only wondering about this because I thought it would be funny. JavaScript has a lot of ridiculous obscure behavior along these lines so I thought it was possible.

However, I tested this and it does not appear to be the case, probably because typeof resp produces the string "undefined" which does not compare === to undefined. So it always produces the response "An unexpected error occurred".

@exvuma
Copy link
Contributor

exvuma commented Jun 5, 2020

Is there anything wrong with just adding the export for the types?

@kentonv
Copy link
Member Author

kentonv commented Jun 5, 2020

@victoriabernard92 We need to do all of:

  1. Export the types in kv-asset-handler's index.js.
  2. Import the types in the example code.
  3. Make the change to the example code in this PR. (The previous code doesn't work, even with the imports and exports corrected.)

@EverlastingBugstopper
Copy link
Contributor

would probably be good to just move the example out of the readme and put it in /examples and run it as an integration test so we dont break it in the future

default:
return new Response("An unexpected error occurred", { status: 500 })
}
if (e instanceof NotFoundError) {
Copy link
Contributor

@SukkaW SukkaW Jun 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The correct case should be:

import { getAssetFromKV } from '@cloudflare/kv-asset-handler'
import { NotFoundError, MethodNotAllowedError } from '@cloudflare/kv-asset-handler/dist/error'

And error.js should export as well. Otherwise the instanceof won't work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if /dist/error is intended to be imported from outside the package. I think we need to re-export the error types from index.js.

ispivey added a commit that referenced this pull request Jun 13, 2020
In order to publish types, the minimal change required is:
* Update `index.ts` to also export `Options, CacheControl,
  MethodNotAllowedError, NotFoundError, InternalError` `npm run build` (which
  runs `tsc -d`, where the `-d` flag indicates to generate type definition
  files)

I observed that this generates a `dist/index.d.ts` type definition file that
contains all types except those defined in `src/mock.ts`, which are only
required for package tests and thus don’t need to be published.

`package.json` already specifies these as the package’s types:
```json
"main": "dist/index.js",
"types": "dist/index.d.ts",
```

As a side note, I’m not sure why `index.d.ts` (even with a subset of types)
isn’t included in the `0.0.10` release. If I check out the `0.0.10` tag and run
`npm publish —dry-run` I observe that it includes `dist/index.d.ts` in the
package.

Finally, I tested that this works using `npm link`. I created a new typescript
project containing the updated sample code from @kentonv’s PR against
`README.md` in [Fix invalid JavaScript in readme example by kentonv · Pull
Request #102 · cloudflare/kv-asset-handler ·
GitHub](https://github.com/cloudflare/kv-asset-handler/pull/102/files#diff-04c6e90faac2675aa89e2176d2eec7d8),
then ran:

```bash
npm i @cloudflare/kv-asset-handler --save
npm link @cloudflare/kv-asset-handler
```

This symlinks to my local copy of `kv-asset-handler` instead of the 0.0.10
release on npm. After doing so, I was able to do the following in said new
project:

```typescript
import { getAssetFromKV, NotFoundError, MethodNotAllowedError } from '@cloudflare/kv-asset-handler'
```

I validated that the new test code transpiles and runs when using these typings.
@ispivey
Copy link
Contributor

ispivey commented Jun 15, 2020

@kentonv 's new snippet should work now that I merged #106 . I think we should just merge this to make it work, and include tested examples as a follow-up improvement (good idea @EverlastingBugstopper ).

@ispivey ispivey self-requested a review June 15, 2020 22:33
Copy link
Contributor

@ispivey ispivey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh, I didn't pay attention. @kentonv would you mind importing the needed error types in the snippet, and then we can merge?

@kentonv
Copy link
Member Author

kentonv commented Jun 16, 2020

@ispivey Done

@ispivey ispivey merged commit 3b88848 into cloudflare:master Jun 20, 2020
@ispivey ispivey added this to the 0.0.11 milestone Jun 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants